1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2014-2019, Toradex AG
7 * Helpers for Freescale PMIC PF0100
12 #include <asm/arch/imx-regs.h>
13 #include <asm/arch/iomux.h>
14 #include <asm/arch/mx6-pins.h>
16 #include <asm/mach-imx/iomux-v3.h>
17 #include <linux/delay.h>
19 #include "pf0100_otp.inc"
22 /* define for PMIC register dump */
25 #define WARNBAR "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n"
27 /* use Apalis GPIO1 to switch on VPGM, ON: 1 */
28 static __maybe_unused iomux_v3_cfg_t const pmic_prog_pads[] = {
29 MX6_PAD_NANDF_D4__GPIO2_IO04 | MUX_PAD_CTRL(NO_PAD_CTRL),
30 # define PMIC_PROG_VOLTAGE IMX_GPIO_NR(2, 4)
33 unsigned pmic_init(void)
36 struct udevice *dev = NULL;
37 unsigned programmed = 0;
39 uchar devid, revid, val;
42 rc = i2c_get_chip_for_busnum(bus, PFUZE100_I2C_ADDR, 1, &dev);
44 printf("failed to get device for PMIC at address 0x%x\n",
49 /* check for errors in PMIC fuses */
50 if (dm_i2c_read(dev, PFUZE100_INTSTAT3, &val, 1) < 0) {
51 puts("i2c pmic INTSTAT3 register read failed\n");
54 if (val & PFUZE100_BIT_OTP_ECCI) {
56 puts("WARNING: ecc errors found in pmic fuse banks\n");
59 if (dm_i2c_read(dev, PFUZE100_OTP_ECC_SE1, &val, 1) < 0) {
60 puts("i2c pmic ECC_SE1 register read failed\n");
63 if (val & PFUZE100_BITS_ECC_SE1) {
65 puts("WARNING: ecc has made bit corrections in banks 1 to 5\n");
68 if (dm_i2c_read(dev, PFUZE100_OTP_ECC_SE2, &val, 1) < 0) {
69 puts("i2c pmic ECC_SE2 register read failed\n");
72 if (val & PFUZE100_BITS_ECC_SE2) {
74 puts("WARNING: ecc has made bit corrections in banks 6 to 10\n"
78 if (dm_i2c_read(dev, PFUZE100_OTP_ECC_DE1, &val, 1) < 0) {
79 puts("i2c pmic ECC_DE register read failed\n");
82 if (val & PFUZE100_BITS_ECC_DE1) {
84 puts("ERROR: banks 1 to 5 have uncorrectable bits\n");
87 if (dm_i2c_read(dev, PFUZE100_OTP_ECC_DE2, &val, 1) < 0) {
88 puts("i2c pmic ECC_DE register read failed\n");
91 if (val & PFUZE100_BITS_ECC_DE2) {
93 puts("ERROR: banks 6 to 10 have uncorrectable bits\n");
97 /* get device ident */
98 if (dm_i2c_read(dev, PFUZE100_DEVICEID, &devid, 1) < 0) {
99 puts("i2c pmic devid read failed\n");
102 if (dm_i2c_read(dev, PFUZE100_REVID, &revid, 1) < 0) {
103 puts("i2c pmic revid read failed\n");
106 printf("device id: 0x%.2x, revision id: 0x%.2x, ", devid, revid);
108 /* get device programmed state */
109 val = PFUZE100_PAGE_REGISTER_PAGE1;
110 if (dm_i2c_write(dev, PFUZE100_PAGE_REGISTER, &val, 1)) {
111 puts("i2c write failed\n");
114 if (dm_i2c_read(dev, PFUZE100_FUSE_POR1, &val, 1) < 0) {
115 puts("i2c fuse_por read failed\n");
118 if (val & PFUZE100_FUSE_POR_M)
121 if (dm_i2c_read(dev, PFUZE100_FUSE_POR2, &val, 1) < 0) {
122 puts("i2c fuse_por read failed\n");
125 if (val & PFUZE100_FUSE_POR_M)
128 if (dm_i2c_read(dev, PFUZE100_FUSE_POR3, &val, 1) < 0) {
129 puts("i2c fuse_por read failed\n");
132 if (val & PFUZE100_FUSE_POR_M)
135 switch (programmed) {
137 puts("not programmed\n");
140 puts("programmed\n");
143 puts("undefined programming state\n");
147 /* The following is needed during production */
148 if (programmed != 3) {
149 /* set VGEN1 to 1.2V */
150 val = PFUZE100_VGEN1_VAL;
151 if (dm_i2c_write(dev, PFUZE100_VGEN1CTL, &val, 1)) {
152 puts("i2c write failed\n");
156 /* set SWBST to 5.0V */
157 val = PFUZE100_SWBST_VAL;
158 if (dm_i2c_write(dev, PFUZE100_SWBSTCTL, &val, 1))
159 puts("i2c write failed\n");
166 for (i = 0; i < 16; i++)
168 for (j = 0; j < 0x80; ) {
170 for (i = 0; i < 16; i++) {
171 dm_i2c_read(dev, j + i, &val, 1);
172 printf("\t%2x", val);
176 printf("\nEXT Page 1");
178 val = PFUZE100_PAGE_REGISTER_PAGE1;
179 if (dm_i2c_write(dev, PFUZE100_PAGE_REGISTER, &val, 1)) {
180 puts("i2c write failed\n");
184 for (j = 0x80; j < 0x100; ) {
186 for (i = 0; i < 16; i++) {
187 dm_i2c_read(dev, j + i, &val, 1);
188 printf("\t%2x", val);
192 printf("\nEXT Page 2");
194 val = PFUZE100_PAGE_REGISTER_PAGE2;
195 if (dm_i2c_write(dev, PFUZE100_PAGE_REGISTER, &val, 1)) {
196 puts("i2c write failed\n");
200 for (j = 0x80; j < 0x100; ) {
202 for (i = 0; i < 16; i++) {
203 dm_i2c_read(dev, j + i, &val, 1);
204 printf("\t%2x", val);
215 #ifndef CONFIG_SPL_BUILD
216 static int pf0100_prog(void)
219 struct udevice *dev = NULL;
220 unsigned char bus = 1;
224 if (pmic_init() == 3) {
225 puts("PMIC already programmed, exiting\n");
226 return CMD_RET_FAILURE;
228 /* set up gpio to manipulate vprog, initially off */
229 imx_iomux_v3_setup_multiple_pads(pmic_prog_pads,
230 ARRAY_SIZE(pmic_prog_pads));
231 gpio_direction_output(PMIC_PROG_VOLTAGE, 0);
233 rc = i2c_get_chip_for_busnum(bus, PFUZE100_I2C_ADDR, 1, &dev);
235 printf("failed to get device for PMIC at address 0x%x\n",
237 return CMD_RET_FAILURE;
240 for (i = 0; i < ARRAY_SIZE(pmic_otp_prog); i++) {
241 switch (pmic_otp_prog[i].cmd) {
243 val = (unsigned char) (pmic_otp_prog[i].value & 0xff);
244 if (dm_i2c_write(dev, pmic_otp_prog[i].reg, &val, 1)) {
245 printf("i2c write failed, reg 0x%2x, value 0x%2x\n",
246 pmic_otp_prog[i].reg, val);
247 return CMD_RET_FAILURE;
251 udelay(pmic_otp_prog[i].value * 1000);
254 gpio_direction_output(PMIC_PROG_VOLTAGE,
255 pmic_otp_prog[i].value);
262 return CMD_RET_SUCCESS;
265 static int do_pf0100_prog(struct cmd_tbl *cmdtp, int flag, int argc,
269 puts("Programming PMIC OTP...");
271 if (ret == CMD_RET_SUCCESS)
279 pf0100_otp_prog, 1, 0, do_pf0100_prog,
280 "Program the OTP fuses on the PMIC PF0100",
283 #endif /* CONFIG_SPL_BUILD */