blob: 7ea9bbb07ed9244e9055563ae1c92cd202eb8d75 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Max Krummenacher3b74ccf2016-11-30 19:43:08 +01002/*
Marcel Ziswilera22d71c2019-02-08 18:12:12 +01003 * Copyright (C) 2014-2019, Toradex AG
Max Krummenacher3b74ccf2016-11-30 19:43:08 +01004 */
5
6/*
7 * Helpers for Freescale PMIC PF0100
8*/
9
Simon Glassed38aef2020-05-10 11:40:03 -060010#include <command.h>
Max Krummenacher3b74ccf2016-11-30 19:43:08 +010011#include <i2c.h>
12#include <asm/arch/imx-regs.h>
13#include <asm/arch/iomux.h>
14#include <asm/arch/mx6-pins.h>
15#include <asm/gpio.h>
Stefano Babic33731bc2017-06-29 10:16:06 +020016#include <asm/mach-imx/iomux-v3.h>
Simon Glassdbd79542020-05-10 11:40:11 -060017#include <linux/delay.h>
Max Krummenacher3b74ccf2016-11-30 19:43:08 +010018
19#include "pf0100_otp.inc"
20#include "pf0100.h"
21
22/* define for PMIC register dump */
23/*#define DEBUG */
24
Gerard Salvatella60f13512019-02-08 18:12:26 +010025#define WARNBAR "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n"
26
Max Krummenacher3b74ccf2016-11-30 19:43:08 +010027/* use Apalis GPIO1 to switch on VPGM, ON: 1 */
Tom Rinic9515392018-01-03 09:19:17 -050028static __maybe_unused iomux_v3_cfg_t const pmic_prog_pads[] = {
Max Krummenacher3b74ccf2016-11-30 19:43:08 +010029 MX6_PAD_NANDF_D4__GPIO2_IO04 | MUX_PAD_CTRL(NO_PAD_CTRL),
30# define PMIC_PROG_VOLTAGE IMX_GPIO_NR(2, 4)
31};
32
33unsigned pmic_init(void)
34{
Marcel Ziswilera22d71c2019-02-08 18:12:12 +010035 int rc;
36 struct udevice *dev = NULL;
Max Krummenacher3b74ccf2016-11-30 19:43:08 +010037 unsigned programmed = 0;
38 uchar bus = 1;
39 uchar devid, revid, val;
40
Marcel Ziswilerfcaa3112019-02-08 18:12:20 +010041 puts("PMIC: ");
Marcel Ziswilera22d71c2019-02-08 18:12:12 +010042 rc = i2c_get_chip_for_busnum(bus, PFUZE100_I2C_ADDR, 1, &dev);
43 if (rc) {
44 printf("failed to get device for PMIC at address 0x%x\n",
45 PFUZE100_I2C_ADDR);
Max Krummenacher3b74ccf2016-11-30 19:43:08 +010046 return 0;
47 }
Gerard Salvatella60f13512019-02-08 18:12:26 +010048
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");
52 return 0;
53 }
54 if (val & PFUZE100_BIT_OTP_ECCI) {
55 puts("\n" WARNBAR);
56 puts("WARNING: ecc errors found in pmic fuse banks\n");
57 puts(WARNBAR);
58 }
59 if (dm_i2c_read(dev, PFUZE100_OTP_ECC_SE1, &val, 1) < 0) {
60 puts("i2c pmic ECC_SE1 register read failed\n");
61 return 0;
62 }
63 if (val & PFUZE100_BITS_ECC_SE1) {
64 puts(WARNBAR);
65 puts("WARNING: ecc has made bit corrections in banks 1 to 5\n");
66 puts(WARNBAR);
67 }
68 if (dm_i2c_read(dev, PFUZE100_OTP_ECC_SE2, &val, 1) < 0) {
69 puts("i2c pmic ECC_SE2 register read failed\n");
70 return 0;
71 }
72 if (val & PFUZE100_BITS_ECC_SE2) {
73 puts(WARNBAR);
74 puts("WARNING: ecc has made bit corrections in banks 6 to 10\n"
75 );
76 puts(WARNBAR);
77 }
78 if (dm_i2c_read(dev, PFUZE100_OTP_ECC_DE1, &val, 1) < 0) {
79 puts("i2c pmic ECC_DE register read failed\n");
80 return 0;
81 }
82 if (val & PFUZE100_BITS_ECC_DE1) {
83 puts(WARNBAR);
84 puts("ERROR: banks 1 to 5 have uncorrectable bits\n");
85 puts(WARNBAR);
86 }
87 if (dm_i2c_read(dev, PFUZE100_OTP_ECC_DE2, &val, 1) < 0) {
88 puts("i2c pmic ECC_DE register read failed\n");
89 return 0;
90 }
91 if (val & PFUZE100_BITS_ECC_DE2) {
92 puts(WARNBAR);
93 puts("ERROR: banks 6 to 10 have uncorrectable bits\n");
94 puts(WARNBAR);
95 }
96
Max Krummenacher3b74ccf2016-11-30 19:43:08 +010097 /* get device ident */
Marcel Ziswilera22d71c2019-02-08 18:12:12 +010098 if (dm_i2c_read(dev, PFUZE100_DEVICEID, &devid, 1) < 0) {
Max Krummenacher3b74ccf2016-11-30 19:43:08 +010099 puts("i2c pmic devid read failed\n");
100 return 0;
101 }
Marcel Ziswilera22d71c2019-02-08 18:12:12 +0100102 if (dm_i2c_read(dev, PFUZE100_REVID, &revid, 1) < 0) {
Max Krummenacher3b74ccf2016-11-30 19:43:08 +0100103 puts("i2c pmic revid read failed\n");
104 return 0;
105 }
Marcel Ziswilerfcaa3112019-02-08 18:12:20 +0100106 printf("device id: 0x%.2x, revision id: 0x%.2x, ", devid, revid);
Max Krummenacher3b74ccf2016-11-30 19:43:08 +0100107
Max Krummenacher3b74ccf2016-11-30 19:43:08 +0100108 /* get device programmed state */
109 val = PFUZE100_PAGE_REGISTER_PAGE1;
Marcel Ziswilera22d71c2019-02-08 18:12:12 +0100110 if (dm_i2c_write(dev, PFUZE100_PAGE_REGISTER, &val, 1)) {
Max Krummenacher3b74ccf2016-11-30 19:43:08 +0100111 puts("i2c write failed\n");
112 return 0;
113 }
Marcel Ziswilera22d71c2019-02-08 18:12:12 +0100114 if (dm_i2c_read(dev, PFUZE100_FUSE_POR1, &val, 1) < 0) {
Max Krummenacher3b74ccf2016-11-30 19:43:08 +0100115 puts("i2c fuse_por read failed\n");
116 return 0;
117 }
118 if (val & PFUZE100_FUSE_POR_M)
119 programmed++;
120
Marcel Ziswilera22d71c2019-02-08 18:12:12 +0100121 if (dm_i2c_read(dev, PFUZE100_FUSE_POR2, &val, 1) < 0) {
Max Krummenacher3b74ccf2016-11-30 19:43:08 +0100122 puts("i2c fuse_por read failed\n");
123 return programmed;
124 }
125 if (val & PFUZE100_FUSE_POR_M)
126 programmed++;
127
Marcel Ziswilera22d71c2019-02-08 18:12:12 +0100128 if (dm_i2c_read(dev, PFUZE100_FUSE_POR3, &val, 1) < 0) {
Max Krummenacher3b74ccf2016-11-30 19:43:08 +0100129 puts("i2c fuse_por read failed\n");
130 return programmed;
131 }
132 if (val & PFUZE100_FUSE_POR_M)
133 programmed++;
134
135 switch (programmed) {
136 case 0:
Marcel Ziswilerfcaa3112019-02-08 18:12:20 +0100137 puts("not programmed\n");
Max Krummenacher3b74ccf2016-11-30 19:43:08 +0100138 break;
139 case 3:
Marcel Ziswilerfcaa3112019-02-08 18:12:20 +0100140 puts("programmed\n");
Max Krummenacher3b74ccf2016-11-30 19:43:08 +0100141 break;
142 default:
Marcel Ziswilerfcaa3112019-02-08 18:12:20 +0100143 puts("undefined programming state\n");
Max Krummenacher3b74ccf2016-11-30 19:43:08 +0100144 break;
145 }
146
147 /* The following is needed during production */
148 if (programmed != 3) {
149 /* set VGEN1 to 1.2V */
150 val = PFUZE100_VGEN1_VAL;
Marcel Ziswilera22d71c2019-02-08 18:12:12 +0100151 if (dm_i2c_write(dev, PFUZE100_VGEN1CTL, &val, 1)) {
Max Krummenacher3b74ccf2016-11-30 19:43:08 +0100152 puts("i2c write failed\n");
153 return programmed;
154 }
155
156 /* set SWBST to 5.0V */
157 val = PFUZE100_SWBST_VAL;
Marcel Ziswilera22d71c2019-02-08 18:12:12 +0100158 if (dm_i2c_write(dev, PFUZE100_SWBSTCTL, &val, 1))
Max Krummenacher3b74ccf2016-11-30 19:43:08 +0100159 puts("i2c write failed\n");
Marcel Ziswilerfcaa3112019-02-08 18:12:20 +0100160 }
161
162#ifdef DEBUG
163 {
164 unsigned int i, j;
165
166 for (i = 0; i < 16; i++)
167 printf("\t%x", i);
168 for (j = 0; j < 0x80; ) {
169 printf("\n%2x", j);
170 for (i = 0; i < 16; i++) {
171 dm_i2c_read(dev, j + i, &val, 1);
172 printf("\t%2x", val);
173 }
174 j += 0x10;
175 }
176 printf("\nEXT Page 1");
177
178 val = PFUZE100_PAGE_REGISTER_PAGE1;
179 if (dm_i2c_write(dev, PFUZE100_PAGE_REGISTER, &val, 1)) {
180 puts("i2c write failed\n");
181 return 0;
182 }
183
184 for (j = 0x80; j < 0x100; ) {
185 printf("\n%2x", j);
186 for (i = 0; i < 16; i++) {
187 dm_i2c_read(dev, j + i, &val, 1);
188 printf("\t%2x", val);
189 }
190 j += 0x10;
191 }
192 printf("\nEXT Page 2");
193
194 val = PFUZE100_PAGE_REGISTER_PAGE2;
195 if (dm_i2c_write(dev, PFUZE100_PAGE_REGISTER, &val, 1)) {
196 puts("i2c write failed\n");
197 return 0;
198 }
199
200 for (j = 0x80; j < 0x100; ) {
201 printf("\n%2x", j);
202 for (i = 0; i < 16; i++) {
203 dm_i2c_read(dev, j + i, &val, 1);
204 printf("\t%2x", val);
205 }
206 j += 0x10;
207 }
208 printf("\n");
Max Krummenacher3b74ccf2016-11-30 19:43:08 +0100209 }
Marcel Ziswilerfcaa3112019-02-08 18:12:20 +0100210#endif /* DEBUG */
211
Max Krummenacher3b74ccf2016-11-30 19:43:08 +0100212 return programmed;
213}
214
Simon Glass49c24a82024-09-29 19:49:47 -0600215#ifndef CONFIG_XPL_BUILD
Tom Rinic9515392018-01-03 09:19:17 -0500216static int pf0100_prog(void)
Max Krummenacher3b74ccf2016-11-30 19:43:08 +0100217{
Marcel Ziswilera22d71c2019-02-08 18:12:12 +0100218 int rc;
219 struct udevice *dev = NULL;
Max Krummenacher3b74ccf2016-11-30 19:43:08 +0100220 unsigned char bus = 1;
221 unsigned char val;
222 unsigned int i;
223
224 if (pmic_init() == 3) {
225 puts("PMIC already programmed, exiting\n");
226 return CMD_RET_FAILURE;
227 }
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);
232
Marcel Ziswilera22d71c2019-02-08 18:12:12 +0100233 rc = i2c_get_chip_for_busnum(bus, PFUZE100_I2C_ADDR, 1, &dev);
234 if (rc) {
235 printf("failed to get device for PMIC at address 0x%x\n",
236 PFUZE100_I2C_ADDR);
Max Krummenacher3b74ccf2016-11-30 19:43:08 +0100237 return CMD_RET_FAILURE;
238 }
239
240 for (i = 0; i < ARRAY_SIZE(pmic_otp_prog); i++) {
241 switch (pmic_otp_prog[i].cmd) {
242 case pmic_i2c:
243 val = (unsigned char) (pmic_otp_prog[i].value & 0xff);
Marcel Ziswilera22d71c2019-02-08 18:12:12 +0100244 if (dm_i2c_write(dev, pmic_otp_prog[i].reg, &val, 1)) {
Max Krummenacher3b74ccf2016-11-30 19:43:08 +0100245 printf("i2c write failed, reg 0x%2x, value 0x%2x\n",
246 pmic_otp_prog[i].reg, val);
247 return CMD_RET_FAILURE;
248 }
249 break;
250 case pmic_delay:
251 udelay(pmic_otp_prog[i].value * 1000);
252 break;
253 case pmic_vpgm:
254 gpio_direction_output(PMIC_PROG_VOLTAGE,
255 pmic_otp_prog[i].value);
256 break;
257 case pmic_pwr:
258 /* TODO */
259 break;
260 }
261 }
262 return CMD_RET_SUCCESS;
263}
264
Simon Glassed38aef2020-05-10 11:40:03 -0600265static int do_pf0100_prog(struct cmd_tbl *cmdtp, int flag, int argc,
266 char *const argv[])
Max Krummenacher3b74ccf2016-11-30 19:43:08 +0100267{
268 int ret;
269 puts("Programming PMIC OTP...");
270 ret = pf0100_prog();
271 if (ret == CMD_RET_SUCCESS)
272 puts("done.\n");
273 else
274 puts("failed.\n");
275 return ret;
276}
277
278U_BOOT_CMD(
279 pf0100_otp_prog, 1, 0, do_pf0100_prog,
280 "Program the OTP fuses on the PMIC PF0100",
281 ""
282);
Simon Glass49c24a82024-09-29 19:49:47 -0600283#endif /* CONFIG_XPL_BUILD */