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>
18 #include "pf0100_otp.inc"
21 /* define for PMIC register dump */
24 #define WARNBAR "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n"
26 /* use GPIO: EXT_IO1 to switch on VPGM, ON: 1 */
27 static __maybe_unused iomux_v3_cfg_t const pmic_prog_pads[] = {
28 MX6_PAD_NANDF_D3__GPIO2_IO03 | MUX_PAD_CTRL(NO_PAD_CTRL),
29 # define PMIC_PROG_VOLTAGE IMX_GPIO_NR(2, 3)
32 unsigned pmic_init(void)
35 struct udevice *dev = NULL;
36 unsigned programmed = 0;
38 uchar devid, revid, val;
41 rc = i2c_get_chip_for_busnum(bus, PFUZE100_I2C_ADDR, 1, &dev);
43 printf("failed to get device for PMIC at address 0x%x\n",
48 /* check for errors in PMIC fuses */
49 if (dm_i2c_read(dev, PFUZE100_INTSTAT3, &val, 1) < 0) {
50 puts("i2c pmic INTSTAT3 register read failed\n");
53 if (val & PFUZE100_BIT_OTP_ECCI) {
55 puts("WARNING: ecc errors found in pmic fuse banks\n");
58 if (dm_i2c_read(dev, PFUZE100_OTP_ECC_SE1, &val, 1) < 0) {
59 puts("i2c pmic ECC_SE1 register read failed\n");
62 if (val & PFUZE100_BITS_ECC_SE1) {
64 puts("WARNING: ecc has made bit corrections in banks 1 to 5\n");
67 if (dm_i2c_read(dev, PFUZE100_OTP_ECC_SE2, &val, 1) < 0) {
68 puts("i2c pmic ECC_SE2 register read failed\n");
71 if (val & PFUZE100_BITS_ECC_SE2) {
73 puts("WARNING: ecc has made bit corrections in banks 6 to 10\n"
77 if (dm_i2c_read(dev, PFUZE100_OTP_ECC_DE1, &val, 1) < 0) {
78 puts("i2c pmic ECC_DE register read failed\n");
81 if (val & PFUZE100_BITS_ECC_DE1) {
83 puts("ERROR: banks 1 to 5 have uncorrectable bits\n");
86 if (dm_i2c_read(dev, PFUZE100_OTP_ECC_DE2, &val, 1) < 0) {
87 puts("i2c pmic ECC_DE register read failed\n");
90 if (val & PFUZE100_BITS_ECC_DE2) {
92 puts("ERROR: banks 6 to 10 have uncorrectable bits\n");
96 /* get device ident */
97 if (dm_i2c_read(dev, PFUZE100_DEVICEID, &devid, 1) < 0) {
98 puts("i2c pmic devid read failed\n");
101 if (dm_i2c_read(dev, PFUZE100_REVID, &revid, 1) < 0) {
102 puts("i2c pmic revid read failed\n");
105 printf("device id: 0x%.2x, revision id: 0x%.2x, ", devid, revid);
107 /* get device programmed state */
108 val = PFUZE100_PAGE_REGISTER_PAGE1;
109 if (dm_i2c_write(dev, PFUZE100_PAGE_REGISTER, &val, 1)) {
110 puts("i2c write failed\n");
113 if (dm_i2c_read(dev, PFUZE100_FUSE_POR1, &val, 1) < 0) {
114 puts("i2c fuse_por read failed\n");
117 if (val & PFUZE100_FUSE_POR_M)
120 if (dm_i2c_read(dev, PFUZE100_FUSE_POR2, &val, 1) < 0) {
121 puts("i2c fuse_por read failed\n");
124 if (val & PFUZE100_FUSE_POR_M)
127 if (dm_i2c_read(dev, PFUZE100_FUSE_POR3, &val, 1) < 0) {
128 puts("i2c fuse_por read failed\n");
131 if (val & PFUZE100_FUSE_POR_M)
134 switch (programmed) {
136 puts("not programmed\n");
139 puts("programmed\n");
142 puts("undefined programming state\n");
150 for (i = 0; i < 16; i++)
152 for (j = 0; j < 0x80; ) {
154 for (i = 0; i < 16; i++) {
155 dm_i2c_read(dev, j + i, &val, 1);
156 printf("\t%2x", val);
160 printf("\nEXT Page 1");
162 val = PFUZE100_PAGE_REGISTER_PAGE1;
163 if (dm_i2c_write(dev, PFUZE100_PAGE_REGISTER, &val, 1)) {
164 puts("i2c write failed\n");
168 for (j = 0x80; j < 0x100; ) {
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 2");
178 val = PFUZE100_PAGE_REGISTER_PAGE2;
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);
199 #ifndef CONFIG_SPL_BUILD
200 static int pf0100_prog(void)
203 struct udevice *dev = NULL;
204 unsigned char bus = 1;
208 if (pmic_init() == 3) {
209 puts("PMIC already programmed, exiting\n");
210 return CMD_RET_FAILURE;
212 /* set up gpio to manipulate vprog, initially off */
213 imx_iomux_v3_setup_multiple_pads(pmic_prog_pads,
214 ARRAY_SIZE(pmic_prog_pads));
215 gpio_direction_output(PMIC_PROG_VOLTAGE, 0);
217 rc = i2c_get_chip_for_busnum(bus, PFUZE100_I2C_ADDR, 1, &dev);
219 printf("failed to get device for PMIC at address 0x%x\n",
221 return CMD_RET_FAILURE;
224 for (i = 0; i < ARRAY_SIZE(pmic_otp_prog); i++) {
225 switch (pmic_otp_prog[i].cmd) {
227 val = (unsigned char) (pmic_otp_prog[i].value & 0xff);
228 if (dm_i2c_write(dev, pmic_otp_prog[i].reg, &val, 1)) {
229 printf("i2c write failed, reg 0x%2x, value 0x%2x\n",
230 pmic_otp_prog[i].reg, val);
231 return CMD_RET_FAILURE;
235 udelay(pmic_otp_prog[i].value * 1000);
238 gpio_direction_output(PMIC_PROG_VOLTAGE,
239 pmic_otp_prog[i].value);
246 return CMD_RET_SUCCESS;
249 static int do_pf0100_prog(cmd_tbl_t *cmdtp, int flag, int argc,
253 puts("Programming PMIC OTP...");
255 if (ret == CMD_RET_SUCCESS)
263 pf0100_otp_prog, 1, 0, do_pf0100_prog,
264 "Program the OTP fuses on the PMIC PF0100",
267 #endif /* CONFIG_SPL_BUILD */