blob: 58b7bc3bb9ae25815b3a4f624707b73f17c0a885 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Max Krummenachereeb16b22016-11-30 19:43:09 +01002/*
Marcel Ziswilerc882cfb2019-02-08 18:42:12 +01003 * Copyright (C) 2014-2019, Toradex AG
Max Krummenachereeb16b22016-11-30 19:43:09 +01004 */
5
6/*
7 * Helpers for Freescale PMIC PF0100
8*/
9
Simon Glassed38aef2020-05-10 11:40:03 -060010#include <command.h>
Max Krummenachereeb16b22016-11-30 19:43:09 +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 Krummenachereeb16b22016-11-30 19:43:09 +010018
19#include "pf0100_otp.inc"
20#include "pf0100.h"
21
22/* define for PMIC register dump */
23/*#define DEBUG */
24
Gerard Salvatella6115ed72019-02-08 18:42:29 +010025#define WARNBAR "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n"
26
Max Krummenachereeb16b22016-11-30 19:43:09 +010027/* use GPIO: EXT_IO1 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 Krummenachereeb16b22016-11-30 19:43:09 +010029 MX6_PAD_NANDF_D3__GPIO2_IO03 | MUX_PAD_CTRL(NO_PAD_CTRL),
30# define PMIC_PROG_VOLTAGE IMX_GPIO_NR(2, 3)
31};
32
33unsigned pmic_init(void)
34{
Marcel Ziswilerc882cfb2019-02-08 18:42:12 +010035 int rc;
36 struct udevice *dev = NULL;
Max Krummenachereeb16b22016-11-30 19:43:09 +010037 unsigned programmed = 0;
38 uchar bus = 1;
39 uchar devid, revid, val;
40
Max Krummenacherc4725752019-02-08 18:42:20 +010041 puts("PMIC: ");
Marcel Ziswilerc882cfb2019-02-08 18:42: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 Krummenachereeb16b22016-11-30 19:43:09 +010046 return 0;
47 }
Gerard Salvatella6115ed72019-02-08 18:42:29 +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 Krummenachereeb16b22016-11-30 19:43:09 +010097 /* get device ident */
Marcel Ziswilerc882cfb2019-02-08 18:42:12 +010098 if (dm_i2c_read(dev, PFUZE100_DEVICEID, &devid, 1) < 0) {
Max Krummenachereeb16b22016-11-30 19:43:09 +010099 puts("i2c pmic devid read failed\n");
100 return 0;
101 }
Marcel Ziswilerc882cfb2019-02-08 18:42:12 +0100102 if (dm_i2c_read(dev, PFUZE100_REVID, &revid, 1) < 0) {
Max Krummenachereeb16b22016-11-30 19:43:09 +0100103 puts("i2c pmic revid read failed\n");
104 return 0;
105 }
Max Krummenacherc4725752019-02-08 18:42:20 +0100106 printf("device id: 0x%.2x, revision id: 0x%.2x, ", devid, revid);
107
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");
112 return 0;
113 }
114 if (dm_i2c_read(dev, PFUZE100_FUSE_POR1, &val, 1) < 0) {
115 puts("i2c fuse_por read failed\n");
116 return 0;
117 }
118 if (val & PFUZE100_FUSE_POR_M)
119 programmed++;
120
121 if (dm_i2c_read(dev, PFUZE100_FUSE_POR2, &val, 1) < 0) {
122 puts("i2c fuse_por read failed\n");
123 return programmed;
124 }
125 if (val & PFUZE100_FUSE_POR_M)
126 programmed++;
127
128 if (dm_i2c_read(dev, PFUZE100_FUSE_POR3, &val, 1) < 0) {
129 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:
137 puts("not programmed\n");
138 break;
139 case 3:
140 puts("programmed\n");
141 break;
142 default:
143 puts("undefined programming state\n");
144 break;
145 }
Max Krummenachereeb16b22016-11-30 19:43:09 +0100146
147#ifdef DEBUG
148 {
Marcel Ziswilerc882cfb2019-02-08 18:42:12 +0100149 unsigned int i, j;
Max Krummenachereeb16b22016-11-30 19:43:09 +0100150
151 for (i = 0; i < 16; i++)
152 printf("\t%x", i);
153 for (j = 0; j < 0x80; ) {
154 printf("\n%2x", j);
155 for (i = 0; i < 16; i++) {
Marcel Ziswilerc882cfb2019-02-08 18:42:12 +0100156 dm_i2c_read(dev, j + i, &val, 1);
Max Krummenachereeb16b22016-11-30 19:43:09 +0100157 printf("\t%2x", val);
158 }
159 j += 0x10;
160 }
161 printf("\nEXT Page 1");
162
163 val = PFUZE100_PAGE_REGISTER_PAGE1;
Marcel Ziswilerc882cfb2019-02-08 18:42:12 +0100164 if (dm_i2c_write(dev, PFUZE100_PAGE_REGISTER, &val, 1)) {
Max Krummenachereeb16b22016-11-30 19:43:09 +0100165 puts("i2c write failed\n");
166 return 0;
167 }
168
169 for (j = 0x80; j < 0x100; ) {
170 printf("\n%2x", j);
171 for (i = 0; i < 16; i++) {
Marcel Ziswilerc882cfb2019-02-08 18:42:12 +0100172 dm_i2c_read(dev, j + i, &val, 1);
Max Krummenachereeb16b22016-11-30 19:43:09 +0100173 printf("\t%2x", val);
174 }
175 j += 0x10;
176 }
177 printf("\nEXT Page 2");
178
179 val = PFUZE100_PAGE_REGISTER_PAGE2;
Marcel Ziswilerc882cfb2019-02-08 18:42:12 +0100180 if (dm_i2c_write(dev, PFUZE100_PAGE_REGISTER, &val, 1)) {
Max Krummenachereeb16b22016-11-30 19:43:09 +0100181 puts("i2c write failed\n");
182 return 0;
183 }
184
185 for (j = 0x80; j < 0x100; ) {
186 printf("\n%2x", j);
187 for (i = 0; i < 16; i++) {
Marcel Ziswilerc882cfb2019-02-08 18:42:12 +0100188 dm_i2c_read(dev, j + i, &val, 1);
Max Krummenachereeb16b22016-11-30 19:43:09 +0100189 printf("\t%2x", val);
190 }
191 j += 0x10;
192 }
193 printf("\n");
194 }
Marcel Ziswilerc882cfb2019-02-08 18:42:12 +0100195#endif /* DEBUG */
196
Max Krummenachereeb16b22016-11-30 19:43:09 +0100197 return programmed;
198}
199
Tom Rinic9515392018-01-03 09:19:17 -0500200#ifndef CONFIG_SPL_BUILD
201static int pf0100_prog(void)
Max Krummenachereeb16b22016-11-30 19:43:09 +0100202{
Marcel Ziswilerc882cfb2019-02-08 18:42:12 +0100203 int rc;
204 struct udevice *dev = NULL;
Max Krummenachereeb16b22016-11-30 19:43:09 +0100205 unsigned char bus = 1;
206 unsigned char val;
207 unsigned int i;
208
209 if (pmic_init() == 3) {
210 puts("PMIC already programmed, exiting\n");
211 return CMD_RET_FAILURE;
212 }
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);
217
Marcel Ziswilerc882cfb2019-02-08 18:42:12 +0100218 rc = i2c_get_chip_for_busnum(bus, PFUZE100_I2C_ADDR, 1, &dev);
219 if (rc) {
220 printf("failed to get device for PMIC at address 0x%x\n",
221 PFUZE100_I2C_ADDR);
Max Krummenachereeb16b22016-11-30 19:43:09 +0100222 return CMD_RET_FAILURE;
223 }
224
225 for (i = 0; i < ARRAY_SIZE(pmic_otp_prog); i++) {
226 switch (pmic_otp_prog[i].cmd) {
227 case pmic_i2c:
228 val = (unsigned char) (pmic_otp_prog[i].value & 0xff);
Marcel Ziswilerc882cfb2019-02-08 18:42:12 +0100229 if (dm_i2c_write(dev, pmic_otp_prog[i].reg, &val, 1)) {
Max Krummenachereeb16b22016-11-30 19:43:09 +0100230 printf("i2c write failed, reg 0x%2x, value 0x%2x\n",
231 pmic_otp_prog[i].reg, val);
232 return CMD_RET_FAILURE;
233 }
234 break;
235 case pmic_delay:
236 udelay(pmic_otp_prog[i].value * 1000);
237 break;
238 case pmic_vpgm:
239 gpio_direction_output(PMIC_PROG_VOLTAGE,
240 pmic_otp_prog[i].value);
241 break;
242 case pmic_pwr:
243 /* TODO */
244 break;
245 }
246 }
247 return CMD_RET_SUCCESS;
248}
249
Simon Glassed38aef2020-05-10 11:40:03 -0600250static int do_pf0100_prog(struct cmd_tbl *cmdtp, int flag, int argc,
251 char *const argv[])
Max Krummenachereeb16b22016-11-30 19:43:09 +0100252{
253 int ret;
254 puts("Programming PMIC OTP...");
255 ret = pf0100_prog();
256 if (ret == CMD_RET_SUCCESS)
257 puts("done.\n");
258 else
259 puts("failed.\n");
260 return ret;
261}
262
263U_BOOT_CMD(
264 pf0100_otp_prog, 1, 0, do_pf0100_prog,
265 "Program the OTP fuses on the PMIC PF0100",
266 ""
267);
Marcel Ziswilerc882cfb2019-02-08 18:42:12 +0100268#endif /* CONFIG_SPL_BUILD */