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 Apalis GPIO1 to switch on VPGM, ON: 1 */
27 static __maybe_unused iomux_v3_cfg_t const pmic_prog_pads[] = {
28 MX6_PAD_NANDF_D4__GPIO2_IO04 | MUX_PAD_CTRL(NO_PAD_CTRL),
29 # define PMIC_PROG_VOLTAGE IMX_GPIO_NR(2, 4)
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");
146 /* The following is needed during production */
147 if (programmed != 3) {
148 /* set VGEN1 to 1.2V */
149 val = PFUZE100_VGEN1_VAL;
150 if (dm_i2c_write(dev, PFUZE100_VGEN1CTL, &val, 1)) {
151 puts("i2c write failed\n");
155 /* set SWBST to 5.0V */
156 val = PFUZE100_SWBST_VAL;
157 if (dm_i2c_write(dev, PFUZE100_SWBSTCTL, &val, 1))
158 puts("i2c write failed\n");
165 for (i = 0; i < 16; i++)
167 for (j = 0; j < 0x80; ) {
169 for (i = 0; i < 16; i++) {
170 dm_i2c_read(dev, j + i, &val, 1);
171 printf("\t%2x", val);
175 printf("\nEXT Page 1");
177 val = PFUZE100_PAGE_REGISTER_PAGE1;
178 if (dm_i2c_write(dev, PFUZE100_PAGE_REGISTER, &val, 1)) {
179 puts("i2c write failed\n");
183 for (j = 0x80; j < 0x100; ) {
185 for (i = 0; i < 16; i++) {
186 dm_i2c_read(dev, j + i, &val, 1);
187 printf("\t%2x", val);
191 printf("\nEXT Page 2");
193 val = PFUZE100_PAGE_REGISTER_PAGE2;
194 if (dm_i2c_write(dev, PFUZE100_PAGE_REGISTER, &val, 1)) {
195 puts("i2c write failed\n");
199 for (j = 0x80; j < 0x100; ) {
201 for (i = 0; i < 16; i++) {
202 dm_i2c_read(dev, j + i, &val, 1);
203 printf("\t%2x", val);
214 #ifndef CONFIG_SPL_BUILD
215 static int pf0100_prog(void)
218 struct udevice *dev = NULL;
219 unsigned char bus = 1;
223 if (pmic_init() == 3) {
224 puts("PMIC already programmed, exiting\n");
225 return CMD_RET_FAILURE;
227 /* set up gpio to manipulate vprog, initially off */
228 imx_iomux_v3_setup_multiple_pads(pmic_prog_pads,
229 ARRAY_SIZE(pmic_prog_pads));
230 gpio_direction_output(PMIC_PROG_VOLTAGE, 0);
232 rc = i2c_get_chip_for_busnum(bus, PFUZE100_I2C_ADDR, 1, &dev);
234 printf("failed to get device for PMIC at address 0x%x\n",
236 return CMD_RET_FAILURE;
239 for (i = 0; i < ARRAY_SIZE(pmic_otp_prog); i++) {
240 switch (pmic_otp_prog[i].cmd) {
242 val = (unsigned char) (pmic_otp_prog[i].value & 0xff);
243 if (dm_i2c_write(dev, pmic_otp_prog[i].reg, &val, 1)) {
244 printf("i2c write failed, reg 0x%2x, value 0x%2x\n",
245 pmic_otp_prog[i].reg, val);
246 return CMD_RET_FAILURE;
250 udelay(pmic_otp_prog[i].value * 1000);
253 gpio_direction_output(PMIC_PROG_VOLTAGE,
254 pmic_otp_prog[i].value);
261 return CMD_RET_SUCCESS;
264 static int do_pf0100_prog(cmd_tbl_t *cmdtp, int flag, int argc,
268 puts("Programming PMIC OTP...");
270 if (ret == CMD_RET_SUCCESS)
278 pf0100_otp_prog, 1, 0, do_pf0100_prog,
279 "Program the OTP fuses on the PMIC PF0100",
282 #endif /* CONFIG_SPL_BUILD */