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>
19 #include "pf0100_otp.inc"
22 /* define for PMIC register dump */
25 #define WARNBAR "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n"
27 /* use GPIO: EXT_IO1 to switch on VPGM, ON: 1 */
28 static __maybe_unused iomux_v3_cfg_t const pmic_prog_pads[] = {
29 MX6_PAD_NANDF_D3__GPIO2_IO03 | MUX_PAD_CTRL(NO_PAD_CTRL),
30 # define PMIC_PROG_VOLTAGE IMX_GPIO_NR(2, 3)
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");
151 for (i = 0; i < 16; i++)
153 for (j = 0; j < 0x80; ) {
155 for (i = 0; i < 16; i++) {
156 dm_i2c_read(dev, j + i, &val, 1);
157 printf("\t%2x", val);
161 printf("\nEXT Page 1");
163 val = PFUZE100_PAGE_REGISTER_PAGE1;
164 if (dm_i2c_write(dev, PFUZE100_PAGE_REGISTER, &val, 1)) {
165 puts("i2c write failed\n");
169 for (j = 0x80; j < 0x100; ) {
171 for (i = 0; i < 16; i++) {
172 dm_i2c_read(dev, j + i, &val, 1);
173 printf("\t%2x", val);
177 printf("\nEXT Page 2");
179 val = PFUZE100_PAGE_REGISTER_PAGE2;
180 if (dm_i2c_write(dev, PFUZE100_PAGE_REGISTER, &val, 1)) {
181 puts("i2c write failed\n");
185 for (j = 0x80; j < 0x100; ) {
187 for (i = 0; i < 16; i++) {
188 dm_i2c_read(dev, j + i, &val, 1);
189 printf("\t%2x", val);
200 #ifndef CONFIG_SPL_BUILD
201 static int pf0100_prog(void)
204 struct udevice *dev = NULL;
205 unsigned char bus = 1;
209 if (pmic_init() == 3) {
210 puts("PMIC already programmed, exiting\n");
211 return CMD_RET_FAILURE;
213 /* set up gpio to manipulate vprog, initially off */
214 imx_iomux_v3_setup_multiple_pads(pmic_prog_pads,
215 ARRAY_SIZE(pmic_prog_pads));
216 gpio_direction_output(PMIC_PROG_VOLTAGE, 0);
218 rc = i2c_get_chip_for_busnum(bus, PFUZE100_I2C_ADDR, 1, &dev);
220 printf("failed to get device for PMIC at address 0x%x\n",
222 return CMD_RET_FAILURE;
225 for (i = 0; i < ARRAY_SIZE(pmic_otp_prog); i++) {
226 switch (pmic_otp_prog[i].cmd) {
228 val = (unsigned char) (pmic_otp_prog[i].value & 0xff);
229 if (dm_i2c_write(dev, pmic_otp_prog[i].reg, &val, 1)) {
230 printf("i2c write failed, reg 0x%2x, value 0x%2x\n",
231 pmic_otp_prog[i].reg, val);
232 return CMD_RET_FAILURE;
236 udelay(pmic_otp_prog[i].value * 1000);
239 gpio_direction_output(PMIC_PROG_VOLTAGE,
240 pmic_otp_prog[i].value);
247 return CMD_RET_SUCCESS;
250 static int do_pf0100_prog(struct cmd_tbl *cmdtp, int flag, int argc,
254 puts("Programming PMIC OTP...");
256 if (ret == CMD_RET_SUCCESS)
264 pf0100_otp_prog, 1, 0, do_pf0100_prog,
265 "Program the OTP fuses on the PMIC PF0100",
268 #endif /* CONFIG_SPL_BUILD */