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