1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2014-2019, Toradex AG
7 * Helpers for Freescale PMIC PF0100
13 #include <asm/arch/imx-regs.h>
14 #include <asm/arch/iomux.h>
15 #include <asm/arch/mx6-pins.h>
17 #include <asm/mach-imx/iomux-v3.h>
18 #include <linux/delay.h>
20 #include "pf0100_otp.inc"
23 /* define for PMIC register dump */
26 #define WARNBAR "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n"
28 /* use GPIO: EXT_IO1 to switch on VPGM, ON: 1 */
29 static __maybe_unused iomux_v3_cfg_t const pmic_prog_pads[] = {
30 MX6_PAD_NANDF_D3__GPIO2_IO03 | MUX_PAD_CTRL(NO_PAD_CTRL),
31 # define PMIC_PROG_VOLTAGE IMX_GPIO_NR(2, 3)
34 unsigned pmic_init(void)
37 struct udevice *dev = NULL;
38 unsigned programmed = 0;
40 uchar devid, revid, val;
43 rc = i2c_get_chip_for_busnum(bus, PFUZE100_I2C_ADDR, 1, &dev);
45 printf("failed to get device for PMIC at address 0x%x\n",
50 /* check for errors in PMIC fuses */
51 if (dm_i2c_read(dev, PFUZE100_INTSTAT3, &val, 1) < 0) {
52 puts("i2c pmic INTSTAT3 register read failed\n");
55 if (val & PFUZE100_BIT_OTP_ECCI) {
57 puts("WARNING: ecc errors found in pmic fuse banks\n");
60 if (dm_i2c_read(dev, PFUZE100_OTP_ECC_SE1, &val, 1) < 0) {
61 puts("i2c pmic ECC_SE1 register read failed\n");
64 if (val & PFUZE100_BITS_ECC_SE1) {
66 puts("WARNING: ecc has made bit corrections in banks 1 to 5\n");
69 if (dm_i2c_read(dev, PFUZE100_OTP_ECC_SE2, &val, 1) < 0) {
70 puts("i2c pmic ECC_SE2 register read failed\n");
73 if (val & PFUZE100_BITS_ECC_SE2) {
75 puts("WARNING: ecc has made bit corrections in banks 6 to 10\n"
79 if (dm_i2c_read(dev, PFUZE100_OTP_ECC_DE1, &val, 1) < 0) {
80 puts("i2c pmic ECC_DE register read failed\n");
83 if (val & PFUZE100_BITS_ECC_DE1) {
85 puts("ERROR: banks 1 to 5 have uncorrectable bits\n");
88 if (dm_i2c_read(dev, PFUZE100_OTP_ECC_DE2, &val, 1) < 0) {
89 puts("i2c pmic ECC_DE register read failed\n");
92 if (val & PFUZE100_BITS_ECC_DE2) {
94 puts("ERROR: banks 6 to 10 have uncorrectable bits\n");
98 /* get device ident */
99 if (dm_i2c_read(dev, PFUZE100_DEVICEID, &devid, 1) < 0) {
100 puts("i2c pmic devid read failed\n");
103 if (dm_i2c_read(dev, PFUZE100_REVID, &revid, 1) < 0) {
104 puts("i2c pmic revid read failed\n");
107 printf("device id: 0x%.2x, revision id: 0x%.2x, ", devid, revid);
109 /* get device programmed state */
110 val = PFUZE100_PAGE_REGISTER_PAGE1;
111 if (dm_i2c_write(dev, PFUZE100_PAGE_REGISTER, &val, 1)) {
112 puts("i2c write failed\n");
115 if (dm_i2c_read(dev, PFUZE100_FUSE_POR1, &val, 1) < 0) {
116 puts("i2c fuse_por read failed\n");
119 if (val & PFUZE100_FUSE_POR_M)
122 if (dm_i2c_read(dev, PFUZE100_FUSE_POR2, &val, 1) < 0) {
123 puts("i2c fuse_por read failed\n");
126 if (val & PFUZE100_FUSE_POR_M)
129 if (dm_i2c_read(dev, PFUZE100_FUSE_POR3, &val, 1) < 0) {
130 puts("i2c fuse_por read failed\n");
133 if (val & PFUZE100_FUSE_POR_M)
136 switch (programmed) {
138 puts("not programmed\n");
141 puts("programmed\n");
144 puts("undefined programming state\n");
152 for (i = 0; i < 16; i++)
154 for (j = 0; j < 0x80; ) {
156 for (i = 0; i < 16; i++) {
157 dm_i2c_read(dev, j + i, &val, 1);
158 printf("\t%2x", val);
162 printf("\nEXT Page 1");
164 val = PFUZE100_PAGE_REGISTER_PAGE1;
165 if (dm_i2c_write(dev, PFUZE100_PAGE_REGISTER, &val, 1)) {
166 puts("i2c write failed\n");
170 for (j = 0x80; j < 0x100; ) {
172 for (i = 0; i < 16; i++) {
173 dm_i2c_read(dev, j + i, &val, 1);
174 printf("\t%2x", val);
178 printf("\nEXT Page 2");
180 val = PFUZE100_PAGE_REGISTER_PAGE2;
181 if (dm_i2c_write(dev, PFUZE100_PAGE_REGISTER, &val, 1)) {
182 puts("i2c write failed\n");
186 for (j = 0x80; j < 0x100; ) {
188 for (i = 0; i < 16; i++) {
189 dm_i2c_read(dev, j + i, &val, 1);
190 printf("\t%2x", val);
201 #ifndef CONFIG_SPL_BUILD
202 static int pf0100_prog(void)
205 struct udevice *dev = NULL;
206 unsigned char bus = 1;
210 if (pmic_init() == 3) {
211 puts("PMIC already programmed, exiting\n");
212 return CMD_RET_FAILURE;
214 /* set up gpio to manipulate vprog, initially off */
215 imx_iomux_v3_setup_multiple_pads(pmic_prog_pads,
216 ARRAY_SIZE(pmic_prog_pads));
217 gpio_direction_output(PMIC_PROG_VOLTAGE, 0);
219 rc = i2c_get_chip_for_busnum(bus, PFUZE100_I2C_ADDR, 1, &dev);
221 printf("failed to get device for PMIC at address 0x%x\n",
223 return CMD_RET_FAILURE;
226 for (i = 0; i < ARRAY_SIZE(pmic_otp_prog); i++) {
227 switch (pmic_otp_prog[i].cmd) {
229 val = (unsigned char) (pmic_otp_prog[i].value & 0xff);
230 if (dm_i2c_write(dev, pmic_otp_prog[i].reg, &val, 1)) {
231 printf("i2c write failed, reg 0x%2x, value 0x%2x\n",
232 pmic_otp_prog[i].reg, val);
233 return CMD_RET_FAILURE;
237 udelay(pmic_otp_prog[i].value * 1000);
240 gpio_direction_output(PMIC_PROG_VOLTAGE,
241 pmic_otp_prog[i].value);
248 return CMD_RET_SUCCESS;
251 static int do_pf0100_prog(struct cmd_tbl *cmdtp, int flag, int argc,
255 puts("Programming PMIC OTP...");
257 if (ret == CMD_RET_SUCCESS)
265 pf0100_otp_prog, 1, 0, do_pf0100_prog,
266 "Program the OTP fuses on the PMIC PF0100",
269 #endif /* CONFIG_SPL_BUILD */