Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Max Krummenacher | 3b74ccf | 2016-11-30 19:43:08 +0100 | [diff] [blame] | 2 | /* |
Marcel Ziswiler | a22d71c | 2019-02-08 18:12:12 +0100 | [diff] [blame^] | 3 | * Copyright (C) 2014-2019, Toradex AG |
Max Krummenacher | 3b74ccf | 2016-11-30 19:43:08 +0100 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | /* |
| 7 | * Helpers for Freescale PMIC PF0100 |
| 8 | */ |
| 9 | |
| 10 | #include <common.h> |
| 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> |
Max Krummenacher | 3b74ccf | 2016-11-30 19:43:08 +0100 | [diff] [blame] | 17 | |
| 18 | #include "pf0100_otp.inc" |
| 19 | #include "pf0100.h" |
| 20 | |
| 21 | /* define for PMIC register dump */ |
| 22 | /*#define DEBUG */ |
| 23 | |
| 24 | /* use Apalis GPIO1 to switch on VPGM, ON: 1 */ |
Tom Rini | c951539 | 2018-01-03 09:19:17 -0500 | [diff] [blame] | 25 | static __maybe_unused iomux_v3_cfg_t const pmic_prog_pads[] = { |
Max Krummenacher | 3b74ccf | 2016-11-30 19:43:08 +0100 | [diff] [blame] | 26 | MX6_PAD_NANDF_D4__GPIO2_IO04 | MUX_PAD_CTRL(NO_PAD_CTRL), |
| 27 | # define PMIC_PROG_VOLTAGE IMX_GPIO_NR(2, 4) |
| 28 | }; |
| 29 | |
| 30 | unsigned pmic_init(void) |
| 31 | { |
Marcel Ziswiler | a22d71c | 2019-02-08 18:12:12 +0100 | [diff] [blame^] | 32 | int rc; |
| 33 | struct udevice *dev = NULL; |
Max Krummenacher | 3b74ccf | 2016-11-30 19:43:08 +0100 | [diff] [blame] | 34 | unsigned programmed = 0; |
| 35 | uchar bus = 1; |
| 36 | uchar devid, revid, val; |
| 37 | |
| 38 | puts("PMIC: "); |
Marcel Ziswiler | a22d71c | 2019-02-08 18:12:12 +0100 | [diff] [blame^] | 39 | rc = i2c_get_chip_for_busnum(bus, PFUZE100_I2C_ADDR, 1, &dev); |
| 40 | if (rc) { |
| 41 | printf("failed to get device for PMIC at address 0x%x\n", |
| 42 | PFUZE100_I2C_ADDR); |
Max Krummenacher | 3b74ccf | 2016-11-30 19:43:08 +0100 | [diff] [blame] | 43 | return 0; |
| 44 | } |
| 45 | /* get device ident */ |
Marcel Ziswiler | a22d71c | 2019-02-08 18:12:12 +0100 | [diff] [blame^] | 46 | if (dm_i2c_read(dev, PFUZE100_DEVICEID, &devid, 1) < 0) { |
Max Krummenacher | 3b74ccf | 2016-11-30 19:43:08 +0100 | [diff] [blame] | 47 | puts("i2c pmic devid read failed\n"); |
| 48 | return 0; |
| 49 | } |
Marcel Ziswiler | a22d71c | 2019-02-08 18:12:12 +0100 | [diff] [blame^] | 50 | if (dm_i2c_read(dev, PFUZE100_REVID, &revid, 1) < 0) { |
Max Krummenacher | 3b74ccf | 2016-11-30 19:43:08 +0100 | [diff] [blame] | 51 | puts("i2c pmic revid read failed\n"); |
| 52 | return 0; |
| 53 | } |
| 54 | printf("device id: 0x%.2x, revision id: 0x%.2x\n", devid, revid); |
| 55 | |
| 56 | #ifdef DEBUG |
| 57 | { |
| 58 | unsigned i, j; |
| 59 | |
| 60 | for (i = 0; i < 16; i++) |
| 61 | printf("\t%x", i); |
| 62 | for (j = 0; j < 0x80; ) { |
| 63 | printf("\n%2x", j); |
| 64 | for (i = 0; i < 16; i++) { |
Marcel Ziswiler | a22d71c | 2019-02-08 18:12:12 +0100 | [diff] [blame^] | 65 | dm_i2c_read(dev, j + i, &val, 1); |
Max Krummenacher | 3b74ccf | 2016-11-30 19:43:08 +0100 | [diff] [blame] | 66 | printf("\t%2x", val); |
| 67 | } |
| 68 | j += 0x10; |
| 69 | } |
| 70 | printf("\nEXT Page 1"); |
| 71 | |
| 72 | val = PFUZE100_PAGE_REGISTER_PAGE1; |
Marcel Ziswiler | a22d71c | 2019-02-08 18:12:12 +0100 | [diff] [blame^] | 73 | if (dm_i2c_write(dev, PFUZE100_PAGE_REGISTER, &val, 1)) { |
Max Krummenacher | 3b74ccf | 2016-11-30 19:43:08 +0100 | [diff] [blame] | 74 | puts("i2c write failed\n"); |
| 75 | return 0; |
| 76 | } |
| 77 | |
| 78 | for (j = 0x80; j < 0x100; ) { |
| 79 | printf("\n%2x", j); |
| 80 | for (i = 0; i < 16; i++) { |
Marcel Ziswiler | a22d71c | 2019-02-08 18:12:12 +0100 | [diff] [blame^] | 81 | dm_i2c_read(dev, j + i, &val, 1); |
Max Krummenacher | 3b74ccf | 2016-11-30 19:43:08 +0100 | [diff] [blame] | 82 | printf("\t%2x", val); |
| 83 | } |
| 84 | j += 0x10; |
| 85 | } |
| 86 | printf("\nEXT Page 2"); |
| 87 | |
| 88 | val = PFUZE100_PAGE_REGISTER_PAGE2; |
Marcel Ziswiler | a22d71c | 2019-02-08 18:12:12 +0100 | [diff] [blame^] | 89 | if (dm_i2c_write(dev, PFUZE100_PAGE_REGISTER, &val, 1)) { |
Max Krummenacher | 3b74ccf | 2016-11-30 19:43:08 +0100 | [diff] [blame] | 90 | puts("i2c write failed\n"); |
| 91 | return 0; |
| 92 | } |
| 93 | |
| 94 | for (j = 0x80; j < 0x100; ) { |
| 95 | printf("\n%2x", j); |
| 96 | for (i = 0; i < 16; i++) { |
Marcel Ziswiler | a22d71c | 2019-02-08 18:12:12 +0100 | [diff] [blame^] | 97 | dm_i2c_read(dev, j + i, &val, 1); |
Max Krummenacher | 3b74ccf | 2016-11-30 19:43:08 +0100 | [diff] [blame] | 98 | printf("\t%2x", val); |
| 99 | } |
| 100 | j += 0x10; |
| 101 | } |
| 102 | printf("\n"); |
| 103 | } |
Marcel Ziswiler | a22d71c | 2019-02-08 18:12:12 +0100 | [diff] [blame^] | 104 | #endif /* DEBUG */ |
Max Krummenacher | 3b74ccf | 2016-11-30 19:43:08 +0100 | [diff] [blame] | 105 | /* get device programmed state */ |
| 106 | val = PFUZE100_PAGE_REGISTER_PAGE1; |
Marcel Ziswiler | a22d71c | 2019-02-08 18:12:12 +0100 | [diff] [blame^] | 107 | if (dm_i2c_write(dev, PFUZE100_PAGE_REGISTER, &val, 1)) { |
Max Krummenacher | 3b74ccf | 2016-11-30 19:43:08 +0100 | [diff] [blame] | 108 | puts("i2c write failed\n"); |
| 109 | return 0; |
| 110 | } |
Marcel Ziswiler | a22d71c | 2019-02-08 18:12:12 +0100 | [diff] [blame^] | 111 | if (dm_i2c_read(dev, PFUZE100_FUSE_POR1, &val, 1) < 0) { |
Max Krummenacher | 3b74ccf | 2016-11-30 19:43:08 +0100 | [diff] [blame] | 112 | puts("i2c fuse_por read failed\n"); |
| 113 | return 0; |
| 114 | } |
| 115 | if (val & PFUZE100_FUSE_POR_M) |
| 116 | programmed++; |
| 117 | |
Marcel Ziswiler | a22d71c | 2019-02-08 18:12:12 +0100 | [diff] [blame^] | 118 | if (dm_i2c_read(dev, PFUZE100_FUSE_POR2, &val, 1) < 0) { |
Max Krummenacher | 3b74ccf | 2016-11-30 19:43:08 +0100 | [diff] [blame] | 119 | puts("i2c fuse_por read failed\n"); |
| 120 | return programmed; |
| 121 | } |
| 122 | if (val & PFUZE100_FUSE_POR_M) |
| 123 | programmed++; |
| 124 | |
Marcel Ziswiler | a22d71c | 2019-02-08 18:12:12 +0100 | [diff] [blame^] | 125 | if (dm_i2c_read(dev, PFUZE100_FUSE_POR3, &val, 1) < 0) { |
Max Krummenacher | 3b74ccf | 2016-11-30 19:43:08 +0100 | [diff] [blame] | 126 | puts("i2c fuse_por read failed\n"); |
| 127 | return programmed; |
| 128 | } |
| 129 | if (val & PFUZE100_FUSE_POR_M) |
| 130 | programmed++; |
| 131 | |
| 132 | switch (programmed) { |
| 133 | case 0: |
| 134 | printf("PMIC: not programmed\n"); |
| 135 | break; |
| 136 | case 3: |
| 137 | printf("PMIC: programmed\n"); |
| 138 | break; |
| 139 | default: |
| 140 | printf("PMIC: undefined programming state\n"); |
| 141 | break; |
| 142 | } |
| 143 | |
| 144 | /* The following is needed during production */ |
| 145 | if (programmed != 3) { |
| 146 | /* set VGEN1 to 1.2V */ |
| 147 | val = PFUZE100_VGEN1_VAL; |
Marcel Ziswiler | a22d71c | 2019-02-08 18:12:12 +0100 | [diff] [blame^] | 148 | if (dm_i2c_write(dev, PFUZE100_VGEN1CTL, &val, 1)) { |
Max Krummenacher | 3b74ccf | 2016-11-30 19:43:08 +0100 | [diff] [blame] | 149 | puts("i2c write failed\n"); |
| 150 | return programmed; |
| 151 | } |
| 152 | |
| 153 | /* set SWBST to 5.0V */ |
| 154 | val = PFUZE100_SWBST_VAL; |
Marcel Ziswiler | a22d71c | 2019-02-08 18:12:12 +0100 | [diff] [blame^] | 155 | if (dm_i2c_write(dev, PFUZE100_SWBSTCTL, &val, 1)) |
Max Krummenacher | 3b74ccf | 2016-11-30 19:43:08 +0100 | [diff] [blame] | 156 | puts("i2c write failed\n"); |
Max Krummenacher | 3b74ccf | 2016-11-30 19:43:08 +0100 | [diff] [blame] | 157 | } |
| 158 | return programmed; |
| 159 | } |
| 160 | |
Tom Rini | c951539 | 2018-01-03 09:19:17 -0500 | [diff] [blame] | 161 | #ifndef CONFIG_SPL_BUILD |
| 162 | static int pf0100_prog(void) |
Max Krummenacher | 3b74ccf | 2016-11-30 19:43:08 +0100 | [diff] [blame] | 163 | { |
Marcel Ziswiler | a22d71c | 2019-02-08 18:12:12 +0100 | [diff] [blame^] | 164 | int rc; |
| 165 | struct udevice *dev = NULL; |
Max Krummenacher | 3b74ccf | 2016-11-30 19:43:08 +0100 | [diff] [blame] | 166 | unsigned char bus = 1; |
| 167 | unsigned char val; |
| 168 | unsigned int i; |
| 169 | |
| 170 | if (pmic_init() == 3) { |
| 171 | puts("PMIC already programmed, exiting\n"); |
| 172 | return CMD_RET_FAILURE; |
| 173 | } |
| 174 | /* set up gpio to manipulate vprog, initially off */ |
| 175 | imx_iomux_v3_setup_multiple_pads(pmic_prog_pads, |
| 176 | ARRAY_SIZE(pmic_prog_pads)); |
| 177 | gpio_direction_output(PMIC_PROG_VOLTAGE, 0); |
| 178 | |
Marcel Ziswiler | a22d71c | 2019-02-08 18:12:12 +0100 | [diff] [blame^] | 179 | rc = i2c_get_chip_for_busnum(bus, PFUZE100_I2C_ADDR, 1, &dev); |
| 180 | if (rc) { |
| 181 | printf("failed to get device for PMIC at address 0x%x\n", |
| 182 | PFUZE100_I2C_ADDR); |
Max Krummenacher | 3b74ccf | 2016-11-30 19:43:08 +0100 | [diff] [blame] | 183 | return CMD_RET_FAILURE; |
| 184 | } |
| 185 | |
| 186 | for (i = 0; i < ARRAY_SIZE(pmic_otp_prog); i++) { |
| 187 | switch (pmic_otp_prog[i].cmd) { |
| 188 | case pmic_i2c: |
| 189 | val = (unsigned char) (pmic_otp_prog[i].value & 0xff); |
Marcel Ziswiler | a22d71c | 2019-02-08 18:12:12 +0100 | [diff] [blame^] | 190 | if (dm_i2c_write(dev, pmic_otp_prog[i].reg, &val, 1)) { |
Max Krummenacher | 3b74ccf | 2016-11-30 19:43:08 +0100 | [diff] [blame] | 191 | printf("i2c write failed, reg 0x%2x, value 0x%2x\n", |
| 192 | pmic_otp_prog[i].reg, val); |
| 193 | return CMD_RET_FAILURE; |
| 194 | } |
| 195 | break; |
| 196 | case pmic_delay: |
| 197 | udelay(pmic_otp_prog[i].value * 1000); |
| 198 | break; |
| 199 | case pmic_vpgm: |
| 200 | gpio_direction_output(PMIC_PROG_VOLTAGE, |
| 201 | pmic_otp_prog[i].value); |
| 202 | break; |
| 203 | case pmic_pwr: |
| 204 | /* TODO */ |
| 205 | break; |
| 206 | } |
| 207 | } |
| 208 | return CMD_RET_SUCCESS; |
| 209 | } |
| 210 | |
Tom Rini | c951539 | 2018-01-03 09:19:17 -0500 | [diff] [blame] | 211 | static int do_pf0100_prog(cmd_tbl_t *cmdtp, int flag, int argc, |
Max Krummenacher | 3b74ccf | 2016-11-30 19:43:08 +0100 | [diff] [blame] | 212 | char * const argv[]) |
| 213 | { |
| 214 | int ret; |
| 215 | puts("Programming PMIC OTP..."); |
| 216 | ret = pf0100_prog(); |
| 217 | if (ret == CMD_RET_SUCCESS) |
| 218 | puts("done.\n"); |
| 219 | else |
| 220 | puts("failed.\n"); |
| 221 | return ret; |
| 222 | } |
| 223 | |
| 224 | U_BOOT_CMD( |
| 225 | pf0100_otp_prog, 1, 0, do_pf0100_prog, |
| 226 | "Program the OTP fuses on the PMIC PF0100", |
| 227 | "" |
| 228 | ); |
Marcel Ziswiler | a22d71c | 2019-02-08 18:12:12 +0100 | [diff] [blame^] | 229 | #endif /* CONFIG_SPL_BUILD */ |