blob: 8f08d8c7337c1b9e7ac7ae2afb66bec48db383dd [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
10#include <common.h>
Simon Glassed38aef2020-05-10 11:40:03 -060011#include <command.h>
Max Krummenachereeb16b22016-11-30 19:43:09 +010012#include <i2c.h>
13#include <asm/arch/imx-regs.h>
14#include <asm/arch/iomux.h>
15#include <asm/arch/mx6-pins.h>
16#include <asm/gpio.h>
Stefano Babic33731bc2017-06-29 10:16:06 +020017#include <asm/mach-imx/iomux-v3.h>
Simon Glassdbd79542020-05-10 11:40:11 -060018#include <linux/delay.h>
Max Krummenachereeb16b22016-11-30 19:43:09 +010019
20#include "pf0100_otp.inc"
21#include "pf0100.h"
22
23/* define for PMIC register dump */
24/*#define DEBUG */
25
Gerard Salvatella6115ed72019-02-08 18:42:29 +010026#define WARNBAR "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n"
27
Max Krummenachereeb16b22016-11-30 19:43:09 +010028/* use GPIO: EXT_IO1 to switch on VPGM, ON: 1 */
Tom Rinic9515392018-01-03 09:19:17 -050029static __maybe_unused iomux_v3_cfg_t const pmic_prog_pads[] = {
Max Krummenachereeb16b22016-11-30 19:43:09 +010030 MX6_PAD_NANDF_D3__GPIO2_IO03 | MUX_PAD_CTRL(NO_PAD_CTRL),
31# define PMIC_PROG_VOLTAGE IMX_GPIO_NR(2, 3)
32};
33
34unsigned pmic_init(void)
35{
Marcel Ziswilerc882cfb2019-02-08 18:42:12 +010036 int rc;
37 struct udevice *dev = NULL;
Max Krummenachereeb16b22016-11-30 19:43:09 +010038 unsigned programmed = 0;
39 uchar bus = 1;
40 uchar devid, revid, val;
41
Max Krummenacherc4725752019-02-08 18:42:20 +010042 puts("PMIC: ");
Marcel Ziswilerc882cfb2019-02-08 18:42:12 +010043 rc = i2c_get_chip_for_busnum(bus, PFUZE100_I2C_ADDR, 1, &dev);
44 if (rc) {
45 printf("failed to get device for PMIC at address 0x%x\n",
46 PFUZE100_I2C_ADDR);
Max Krummenachereeb16b22016-11-30 19:43:09 +010047 return 0;
48 }
Gerard Salvatella6115ed72019-02-08 18:42:29 +010049
50 /* check for errors in PMIC fuses */
51 if (dm_i2c_read(dev, PFUZE100_INTSTAT3, &val, 1) < 0) {
52 puts("i2c pmic INTSTAT3 register read failed\n");
53 return 0;
54 }
55 if (val & PFUZE100_BIT_OTP_ECCI) {
56 puts("\n" WARNBAR);
57 puts("WARNING: ecc errors found in pmic fuse banks\n");
58 puts(WARNBAR);
59 }
60 if (dm_i2c_read(dev, PFUZE100_OTP_ECC_SE1, &val, 1) < 0) {
61 puts("i2c pmic ECC_SE1 register read failed\n");
62 return 0;
63 }
64 if (val & PFUZE100_BITS_ECC_SE1) {
65 puts(WARNBAR);
66 puts("WARNING: ecc has made bit corrections in banks 1 to 5\n");
67 puts(WARNBAR);
68 }
69 if (dm_i2c_read(dev, PFUZE100_OTP_ECC_SE2, &val, 1) < 0) {
70 puts("i2c pmic ECC_SE2 register read failed\n");
71 return 0;
72 }
73 if (val & PFUZE100_BITS_ECC_SE2) {
74 puts(WARNBAR);
75 puts("WARNING: ecc has made bit corrections in banks 6 to 10\n"
76 );
77 puts(WARNBAR);
78 }
79 if (dm_i2c_read(dev, PFUZE100_OTP_ECC_DE1, &val, 1) < 0) {
80 puts("i2c pmic ECC_DE register read failed\n");
81 return 0;
82 }
83 if (val & PFUZE100_BITS_ECC_DE1) {
84 puts(WARNBAR);
85 puts("ERROR: banks 1 to 5 have uncorrectable bits\n");
86 puts(WARNBAR);
87 }
88 if (dm_i2c_read(dev, PFUZE100_OTP_ECC_DE2, &val, 1) < 0) {
89 puts("i2c pmic ECC_DE register read failed\n");
90 return 0;
91 }
92 if (val & PFUZE100_BITS_ECC_DE2) {
93 puts(WARNBAR);
94 puts("ERROR: banks 6 to 10 have uncorrectable bits\n");
95 puts(WARNBAR);
96 }
97
Max Krummenachereeb16b22016-11-30 19:43:09 +010098 /* get device ident */
Marcel Ziswilerc882cfb2019-02-08 18:42:12 +010099 if (dm_i2c_read(dev, PFUZE100_DEVICEID, &devid, 1) < 0) {
Max Krummenachereeb16b22016-11-30 19:43:09 +0100100 puts("i2c pmic devid read failed\n");
101 return 0;
102 }
Marcel Ziswilerc882cfb2019-02-08 18:42:12 +0100103 if (dm_i2c_read(dev, PFUZE100_REVID, &revid, 1) < 0) {
Max Krummenachereeb16b22016-11-30 19:43:09 +0100104 puts("i2c pmic revid read failed\n");
105 return 0;
106 }
Max Krummenacherc4725752019-02-08 18:42:20 +0100107 printf("device id: 0x%.2x, revision id: 0x%.2x, ", devid, revid);
108
109 /* get device programmed state */
110 val = PFUZE100_PAGE_REGISTER_PAGE1;
111 if (dm_i2c_write(dev, PFUZE100_PAGE_REGISTER, &val, 1)) {
112 puts("i2c write failed\n");
113 return 0;
114 }
115 if (dm_i2c_read(dev, PFUZE100_FUSE_POR1, &val, 1) < 0) {
116 puts("i2c fuse_por read failed\n");
117 return 0;
118 }
119 if (val & PFUZE100_FUSE_POR_M)
120 programmed++;
121
122 if (dm_i2c_read(dev, PFUZE100_FUSE_POR2, &val, 1) < 0) {
123 puts("i2c fuse_por read failed\n");
124 return programmed;
125 }
126 if (val & PFUZE100_FUSE_POR_M)
127 programmed++;
128
129 if (dm_i2c_read(dev, PFUZE100_FUSE_POR3, &val, 1) < 0) {
130 puts("i2c fuse_por read failed\n");
131 return programmed;
132 }
133 if (val & PFUZE100_FUSE_POR_M)
134 programmed++;
135
136 switch (programmed) {
137 case 0:
138 puts("not programmed\n");
139 break;
140 case 3:
141 puts("programmed\n");
142 break;
143 default:
144 puts("undefined programming state\n");
145 break;
146 }
Max Krummenachereeb16b22016-11-30 19:43:09 +0100147
148#ifdef DEBUG
149 {
Marcel Ziswilerc882cfb2019-02-08 18:42:12 +0100150 unsigned int i, j;
Max Krummenachereeb16b22016-11-30 19:43:09 +0100151
152 for (i = 0; i < 16; i++)
153 printf("\t%x", i);
154 for (j = 0; j < 0x80; ) {
155 printf("\n%2x", j);
156 for (i = 0; i < 16; i++) {
Marcel Ziswilerc882cfb2019-02-08 18:42:12 +0100157 dm_i2c_read(dev, j + i, &val, 1);
Max Krummenachereeb16b22016-11-30 19:43:09 +0100158 printf("\t%2x", val);
159 }
160 j += 0x10;
161 }
162 printf("\nEXT Page 1");
163
164 val = PFUZE100_PAGE_REGISTER_PAGE1;
Marcel Ziswilerc882cfb2019-02-08 18:42:12 +0100165 if (dm_i2c_write(dev, PFUZE100_PAGE_REGISTER, &val, 1)) {
Max Krummenachereeb16b22016-11-30 19:43:09 +0100166 puts("i2c write failed\n");
167 return 0;
168 }
169
170 for (j = 0x80; j < 0x100; ) {
171 printf("\n%2x", j);
172 for (i = 0; i < 16; i++) {
Marcel Ziswilerc882cfb2019-02-08 18:42:12 +0100173 dm_i2c_read(dev, j + i, &val, 1);
Max Krummenachereeb16b22016-11-30 19:43:09 +0100174 printf("\t%2x", val);
175 }
176 j += 0x10;
177 }
178 printf("\nEXT Page 2");
179
180 val = PFUZE100_PAGE_REGISTER_PAGE2;
Marcel Ziswilerc882cfb2019-02-08 18:42:12 +0100181 if (dm_i2c_write(dev, PFUZE100_PAGE_REGISTER, &val, 1)) {
Max Krummenachereeb16b22016-11-30 19:43:09 +0100182 puts("i2c write failed\n");
183 return 0;
184 }
185
186 for (j = 0x80; j < 0x100; ) {
187 printf("\n%2x", j);
188 for (i = 0; i < 16; i++) {
Marcel Ziswilerc882cfb2019-02-08 18:42:12 +0100189 dm_i2c_read(dev, j + i, &val, 1);
Max Krummenachereeb16b22016-11-30 19:43:09 +0100190 printf("\t%2x", val);
191 }
192 j += 0x10;
193 }
194 printf("\n");
195 }
Marcel Ziswilerc882cfb2019-02-08 18:42:12 +0100196#endif /* DEBUG */
197
Max Krummenachereeb16b22016-11-30 19:43:09 +0100198 return programmed;
199}
200
Tom Rinic9515392018-01-03 09:19:17 -0500201#ifndef CONFIG_SPL_BUILD
202static int pf0100_prog(void)
Max Krummenachereeb16b22016-11-30 19:43:09 +0100203{
Marcel Ziswilerc882cfb2019-02-08 18:42:12 +0100204 int rc;
205 struct udevice *dev = NULL;
Max Krummenachereeb16b22016-11-30 19:43:09 +0100206 unsigned char bus = 1;
207 unsigned char val;
208 unsigned int i;
209
210 if (pmic_init() == 3) {
211 puts("PMIC already programmed, exiting\n");
212 return CMD_RET_FAILURE;
213 }
214 /* set up gpio to manipulate vprog, initially off */
215 imx_iomux_v3_setup_multiple_pads(pmic_prog_pads,
216 ARRAY_SIZE(pmic_prog_pads));
217 gpio_direction_output(PMIC_PROG_VOLTAGE, 0);
218
Marcel Ziswilerc882cfb2019-02-08 18:42:12 +0100219 rc = i2c_get_chip_for_busnum(bus, PFUZE100_I2C_ADDR, 1, &dev);
220 if (rc) {
221 printf("failed to get device for PMIC at address 0x%x\n",
222 PFUZE100_I2C_ADDR);
Max Krummenachereeb16b22016-11-30 19:43:09 +0100223 return CMD_RET_FAILURE;
224 }
225
226 for (i = 0; i < ARRAY_SIZE(pmic_otp_prog); i++) {
227 switch (pmic_otp_prog[i].cmd) {
228 case pmic_i2c:
229 val = (unsigned char) (pmic_otp_prog[i].value & 0xff);
Marcel Ziswilerc882cfb2019-02-08 18:42:12 +0100230 if (dm_i2c_write(dev, pmic_otp_prog[i].reg, &val, 1)) {
Max Krummenachereeb16b22016-11-30 19:43:09 +0100231 printf("i2c write failed, reg 0x%2x, value 0x%2x\n",
232 pmic_otp_prog[i].reg, val);
233 return CMD_RET_FAILURE;
234 }
235 break;
236 case pmic_delay:
237 udelay(pmic_otp_prog[i].value * 1000);
238 break;
239 case pmic_vpgm:
240 gpio_direction_output(PMIC_PROG_VOLTAGE,
241 pmic_otp_prog[i].value);
242 break;
243 case pmic_pwr:
244 /* TODO */
245 break;
246 }
247 }
248 return CMD_RET_SUCCESS;
249}
250
Simon Glassed38aef2020-05-10 11:40:03 -0600251static int do_pf0100_prog(struct cmd_tbl *cmdtp, int flag, int argc,
252 char *const argv[])
Max Krummenachereeb16b22016-11-30 19:43:09 +0100253{
254 int ret;
255 puts("Programming PMIC OTP...");
256 ret = pf0100_prog();
257 if (ret == CMD_RET_SUCCESS)
258 puts("done.\n");
259 else
260 puts("failed.\n");
261 return ret;
262}
263
264U_BOOT_CMD(
265 pf0100_otp_prog, 1, 0, do_pf0100_prog,
266 "Program the OTP fuses on the PMIC PF0100",
267 ""
268);
Marcel Ziswilerc882cfb2019-02-08 18:42:12 +0100269#endif /* CONFIG_SPL_BUILD */