blob: 09c3cc950aebe5886f55edcd3195303652d3f391 [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>
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 Babic33731bc2017-06-29 10:16:06 +020016#include <asm/mach-imx/iomux-v3.h>
Max Krummenachereeb16b22016-11-30 19:43:09 +010017
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 Rinic9515392018-01-03 09:19:17 -050025static __maybe_unused iomux_v3_cfg_t const pmic_prog_pads[] = {
Max Krummenachereeb16b22016-11-30 19:43:09 +010026 MX6_PAD_NANDF_D3__GPIO2_IO03 | MUX_PAD_CTRL(NO_PAD_CTRL),
27# define PMIC_PROG_VOLTAGE IMX_GPIO_NR(2, 3)
28};
29
30unsigned pmic_init(void)
31{
Marcel Ziswilerc882cfb2019-02-08 18:42:12 +010032 int rc;
33 struct udevice *dev = NULL;
Max Krummenachereeb16b22016-11-30 19:43:09 +010034 unsigned programmed = 0;
35 uchar bus = 1;
36 uchar devid, revid, val;
37
Max Krummenacherc4725752019-02-08 18:42:20 +010038 puts("PMIC: ");
Marcel Ziswilerc882cfb2019-02-08 18:42:12 +010039 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 Krummenachereeb16b22016-11-30 19:43:09 +010043 return 0;
44 }
45 /* get device ident */
Marcel Ziswilerc882cfb2019-02-08 18:42:12 +010046 if (dm_i2c_read(dev, PFUZE100_DEVICEID, &devid, 1) < 0) {
Max Krummenachereeb16b22016-11-30 19:43:09 +010047 puts("i2c pmic devid read failed\n");
48 return 0;
49 }
Marcel Ziswilerc882cfb2019-02-08 18:42:12 +010050 if (dm_i2c_read(dev, PFUZE100_REVID, &revid, 1) < 0) {
Max Krummenachereeb16b22016-11-30 19:43:09 +010051 puts("i2c pmic revid read failed\n");
52 return 0;
53 }
Max Krummenacherc4725752019-02-08 18:42:20 +010054 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 Krummenachereeb16b22016-11-30 19:43:09 +010094
95#ifdef DEBUG
96 {
Marcel Ziswilerc882cfb2019-02-08 18:42:12 +010097 unsigned int i, j;
Max Krummenachereeb16b22016-11-30 19:43:09 +010098
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 Ziswilerc882cfb2019-02-08 18:42:12 +0100104 dm_i2c_read(dev, j + i, &val, 1);
Max Krummenachereeb16b22016-11-30 19:43:09 +0100105 printf("\t%2x", val);
106 }
107 j += 0x10;
108 }
109 printf("\nEXT Page 1");
110
111 val = PFUZE100_PAGE_REGISTER_PAGE1;
Marcel Ziswilerc882cfb2019-02-08 18:42:12 +0100112 if (dm_i2c_write(dev, PFUZE100_PAGE_REGISTER, &val, 1)) {
Max Krummenachereeb16b22016-11-30 19:43:09 +0100113 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 Ziswilerc882cfb2019-02-08 18:42:12 +0100120 dm_i2c_read(dev, j + i, &val, 1);
Max Krummenachereeb16b22016-11-30 19:43:09 +0100121 printf("\t%2x", val);
122 }
123 j += 0x10;
124 }
125 printf("\nEXT Page 2");
126
127 val = PFUZE100_PAGE_REGISTER_PAGE2;
Marcel Ziswilerc882cfb2019-02-08 18:42:12 +0100128 if (dm_i2c_write(dev, PFUZE100_PAGE_REGISTER, &val, 1)) {
Max Krummenachereeb16b22016-11-30 19:43:09 +0100129 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 Ziswilerc882cfb2019-02-08 18:42:12 +0100136 dm_i2c_read(dev, j + i, &val, 1);
Max Krummenachereeb16b22016-11-30 19:43:09 +0100137 printf("\t%2x", val);
138 }
139 j += 0x10;
140 }
141 printf("\n");
142 }
Marcel Ziswilerc882cfb2019-02-08 18:42:12 +0100143#endif /* DEBUG */
144
Max Krummenachereeb16b22016-11-30 19:43:09 +0100145 return programmed;
146}
147
Tom Rinic9515392018-01-03 09:19:17 -0500148#ifndef CONFIG_SPL_BUILD
149static int pf0100_prog(void)
Max Krummenachereeb16b22016-11-30 19:43:09 +0100150{
Marcel Ziswilerc882cfb2019-02-08 18:42:12 +0100151 int rc;
152 struct udevice *dev = NULL;
Max Krummenachereeb16b22016-11-30 19:43:09 +0100153 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 Ziswilerc882cfb2019-02-08 18:42:12 +0100166 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 Krummenachereeb16b22016-11-30 19:43:09 +0100170 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 Ziswilerc882cfb2019-02-08 18:42:12 +0100177 if (dm_i2c_write(dev, pmic_otp_prog[i].reg, &val, 1)) {
Max Krummenachereeb16b22016-11-30 19:43:09 +0100178 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 Rinic9515392018-01-03 09:19:17 -0500198static int do_pf0100_prog(cmd_tbl_t *cmdtp, int flag, int argc,
Max Krummenachereeb16b22016-11-30 19:43:09 +0100199 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
211U_BOOT_CMD(
212 pf0100_otp_prog, 1, 0, do_pf0100_prog,
213 "Program the OTP fuses on the PMIC PF0100",
214 ""
215);
Marcel Ziswilerc882cfb2019-02-08 18:42:12 +0100216#endif /* CONFIG_SPL_BUILD */