Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Max Krummenacher | eeb16b2 | 2016-11-30 19:43:09 +0100 | [diff] [blame] | 2 | /* |
Marcel Ziswiler | c882cfb | 2019-02-08 18:42:12 +0100 | [diff] [blame] | 3 | * Copyright (C) 2014-2019, Toradex AG |
Max Krummenacher | eeb16b2 | 2016-11-30 19:43:09 +0100 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | /* |
| 7 | * Helpers for Freescale PMIC PF0100 |
| 8 | */ |
| 9 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 10 | #include <command.h> |
Max Krummenacher | eeb16b2 | 2016-11-30 19:43:09 +0100 | [diff] [blame] | 11 | #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 Babic | 33731bc | 2017-06-29 10:16:06 +0200 | [diff] [blame] | 16 | #include <asm/mach-imx/iomux-v3.h> |
Simon Glass | dbd7954 | 2020-05-10 11:40:11 -0600 | [diff] [blame] | 17 | #include <linux/delay.h> |
Max Krummenacher | eeb16b2 | 2016-11-30 19:43:09 +0100 | [diff] [blame] | 18 | |
| 19 | #include "pf0100_otp.inc" |
| 20 | #include "pf0100.h" |
| 21 | |
| 22 | /* define for PMIC register dump */ |
| 23 | /*#define DEBUG */ |
| 24 | |
Gerard Salvatella | 6115ed7 | 2019-02-08 18:42:29 +0100 | [diff] [blame] | 25 | #define WARNBAR "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n" |
| 26 | |
Max Krummenacher | eeb16b2 | 2016-11-30 19:43:09 +0100 | [diff] [blame] | 27 | /* use GPIO: EXT_IO1 to switch on VPGM, ON: 1 */ |
Tom Rini | c951539 | 2018-01-03 09:19:17 -0500 | [diff] [blame] | 28 | static __maybe_unused iomux_v3_cfg_t const pmic_prog_pads[] = { |
Max Krummenacher | eeb16b2 | 2016-11-30 19:43:09 +0100 | [diff] [blame] | 29 | MX6_PAD_NANDF_D3__GPIO2_IO03 | MUX_PAD_CTRL(NO_PAD_CTRL), |
| 30 | # define PMIC_PROG_VOLTAGE IMX_GPIO_NR(2, 3) |
| 31 | }; |
| 32 | |
| 33 | unsigned pmic_init(void) |
| 34 | { |
Marcel Ziswiler | c882cfb | 2019-02-08 18:42:12 +0100 | [diff] [blame] | 35 | int rc; |
| 36 | struct udevice *dev = NULL; |
Max Krummenacher | eeb16b2 | 2016-11-30 19:43:09 +0100 | [diff] [blame] | 37 | unsigned programmed = 0; |
| 38 | uchar bus = 1; |
| 39 | uchar devid, revid, val; |
| 40 | |
Max Krummenacher | c472575 | 2019-02-08 18:42:20 +0100 | [diff] [blame] | 41 | puts("PMIC: "); |
Marcel Ziswiler | c882cfb | 2019-02-08 18:42:12 +0100 | [diff] [blame] | 42 | 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 Krummenacher | eeb16b2 | 2016-11-30 19:43:09 +0100 | [diff] [blame] | 46 | return 0; |
| 47 | } |
Gerard Salvatella | 6115ed7 | 2019-02-08 18:42:29 +0100 | [diff] [blame] | 48 | |
| 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 Krummenacher | eeb16b2 | 2016-11-30 19:43:09 +0100 | [diff] [blame] | 97 | /* get device ident */ |
Marcel Ziswiler | c882cfb | 2019-02-08 18:42:12 +0100 | [diff] [blame] | 98 | if (dm_i2c_read(dev, PFUZE100_DEVICEID, &devid, 1) < 0) { |
Max Krummenacher | eeb16b2 | 2016-11-30 19:43:09 +0100 | [diff] [blame] | 99 | puts("i2c pmic devid read failed\n"); |
| 100 | return 0; |
| 101 | } |
Marcel Ziswiler | c882cfb | 2019-02-08 18:42:12 +0100 | [diff] [blame] | 102 | if (dm_i2c_read(dev, PFUZE100_REVID, &revid, 1) < 0) { |
Max Krummenacher | eeb16b2 | 2016-11-30 19:43:09 +0100 | [diff] [blame] | 103 | puts("i2c pmic revid read failed\n"); |
| 104 | return 0; |
| 105 | } |
Max Krummenacher | c472575 | 2019-02-08 18:42:20 +0100 | [diff] [blame] | 106 | 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 Krummenacher | eeb16b2 | 2016-11-30 19:43:09 +0100 | [diff] [blame] | 146 | |
| 147 | #ifdef DEBUG |
| 148 | { |
Marcel Ziswiler | c882cfb | 2019-02-08 18:42:12 +0100 | [diff] [blame] | 149 | unsigned int i, j; |
Max Krummenacher | eeb16b2 | 2016-11-30 19:43:09 +0100 | [diff] [blame] | 150 | |
| 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 Ziswiler | c882cfb | 2019-02-08 18:42:12 +0100 | [diff] [blame] | 156 | dm_i2c_read(dev, j + i, &val, 1); |
Max Krummenacher | eeb16b2 | 2016-11-30 19:43:09 +0100 | [diff] [blame] | 157 | printf("\t%2x", val); |
| 158 | } |
| 159 | j += 0x10; |
| 160 | } |
| 161 | printf("\nEXT Page 1"); |
| 162 | |
| 163 | val = PFUZE100_PAGE_REGISTER_PAGE1; |
Marcel Ziswiler | c882cfb | 2019-02-08 18:42:12 +0100 | [diff] [blame] | 164 | if (dm_i2c_write(dev, PFUZE100_PAGE_REGISTER, &val, 1)) { |
Max Krummenacher | eeb16b2 | 2016-11-30 19:43:09 +0100 | [diff] [blame] | 165 | 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 Ziswiler | c882cfb | 2019-02-08 18:42:12 +0100 | [diff] [blame] | 172 | dm_i2c_read(dev, j + i, &val, 1); |
Max Krummenacher | eeb16b2 | 2016-11-30 19:43:09 +0100 | [diff] [blame] | 173 | printf("\t%2x", val); |
| 174 | } |
| 175 | j += 0x10; |
| 176 | } |
| 177 | printf("\nEXT Page 2"); |
| 178 | |
| 179 | val = PFUZE100_PAGE_REGISTER_PAGE2; |
Marcel Ziswiler | c882cfb | 2019-02-08 18:42:12 +0100 | [diff] [blame] | 180 | if (dm_i2c_write(dev, PFUZE100_PAGE_REGISTER, &val, 1)) { |
Max Krummenacher | eeb16b2 | 2016-11-30 19:43:09 +0100 | [diff] [blame] | 181 | 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 Ziswiler | c882cfb | 2019-02-08 18:42:12 +0100 | [diff] [blame] | 188 | dm_i2c_read(dev, j + i, &val, 1); |
Max Krummenacher | eeb16b2 | 2016-11-30 19:43:09 +0100 | [diff] [blame] | 189 | printf("\t%2x", val); |
| 190 | } |
| 191 | j += 0x10; |
| 192 | } |
| 193 | printf("\n"); |
| 194 | } |
Marcel Ziswiler | c882cfb | 2019-02-08 18:42:12 +0100 | [diff] [blame] | 195 | #endif /* DEBUG */ |
| 196 | |
Max Krummenacher | eeb16b2 | 2016-11-30 19:43:09 +0100 | [diff] [blame] | 197 | return programmed; |
| 198 | } |
| 199 | |
Tom Rini | c951539 | 2018-01-03 09:19:17 -0500 | [diff] [blame] | 200 | #ifndef CONFIG_SPL_BUILD |
| 201 | static int pf0100_prog(void) |
Max Krummenacher | eeb16b2 | 2016-11-30 19:43:09 +0100 | [diff] [blame] | 202 | { |
Marcel Ziswiler | c882cfb | 2019-02-08 18:42:12 +0100 | [diff] [blame] | 203 | int rc; |
| 204 | struct udevice *dev = NULL; |
Max Krummenacher | eeb16b2 | 2016-11-30 19:43:09 +0100 | [diff] [blame] | 205 | 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 Ziswiler | c882cfb | 2019-02-08 18:42:12 +0100 | [diff] [blame] | 218 | 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 Krummenacher | eeb16b2 | 2016-11-30 19:43:09 +0100 | [diff] [blame] | 222 | 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 Ziswiler | c882cfb | 2019-02-08 18:42:12 +0100 | [diff] [blame] | 229 | if (dm_i2c_write(dev, pmic_otp_prog[i].reg, &val, 1)) { |
Max Krummenacher | eeb16b2 | 2016-11-30 19:43:09 +0100 | [diff] [blame] | 230 | 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 Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 250 | static int do_pf0100_prog(struct cmd_tbl *cmdtp, int flag, int argc, |
| 251 | char *const argv[]) |
Max Krummenacher | eeb16b2 | 2016-11-30 19:43:09 +0100 | [diff] [blame] | 252 | { |
| 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 | |
| 263 | U_BOOT_CMD( |
| 264 | pf0100_otp_prog, 1, 0, do_pf0100_prog, |
| 265 | "Program the OTP fuses on the PMIC PF0100", |
| 266 | "" |
| 267 | ); |
Marcel Ziswiler | c882cfb | 2019-02-08 18:42:12 +0100 | [diff] [blame] | 268 | #endif /* CONFIG_SPL_BUILD */ |