blob: 3a944b3699e4d6ec51e281e4dce96d538f940fa6 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Max Krummenacher3b74ccf2016-11-30 19:43:08 +01002/*
Marcel Ziswilera22d71c2019-02-08 18:12:12 +01003 * Copyright (C) 2014-2019, Toradex AG
Max Krummenacher3b74ccf2016-11-30 19:43:08 +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 Krummenacher3b74ccf2016-11-30 19:43:08 +010017
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 Rinic9515392018-01-03 09:19:17 -050025static __maybe_unused iomux_v3_cfg_t const pmic_prog_pads[] = {
Max Krummenacher3b74ccf2016-11-30 19:43:08 +010026 MX6_PAD_NANDF_D4__GPIO2_IO04 | MUX_PAD_CTRL(NO_PAD_CTRL),
27# define PMIC_PROG_VOLTAGE IMX_GPIO_NR(2, 4)
28};
29
30unsigned pmic_init(void)
31{
Marcel Ziswilera22d71c2019-02-08 18:12:12 +010032 int rc;
33 struct udevice *dev = NULL;
Max Krummenacher3b74ccf2016-11-30 19:43:08 +010034 unsigned programmed = 0;
35 uchar bus = 1;
36 uchar devid, revid, val;
37
38 puts("PMIC: ");
Marcel Ziswilera22d71c2019-02-08 18:12: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 Krummenacher3b74ccf2016-11-30 19:43:08 +010043 return 0;
44 }
45 /* get device ident */
Marcel Ziswilera22d71c2019-02-08 18:12:12 +010046 if (dm_i2c_read(dev, PFUZE100_DEVICEID, &devid, 1) < 0) {
Max Krummenacher3b74ccf2016-11-30 19:43:08 +010047 puts("i2c pmic devid read failed\n");
48 return 0;
49 }
Marcel Ziswilera22d71c2019-02-08 18:12:12 +010050 if (dm_i2c_read(dev, PFUZE100_REVID, &revid, 1) < 0) {
Max Krummenacher3b74ccf2016-11-30 19:43:08 +010051 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 Ziswilera22d71c2019-02-08 18:12:12 +010065 dm_i2c_read(dev, j + i, &val, 1);
Max Krummenacher3b74ccf2016-11-30 19:43:08 +010066 printf("\t%2x", val);
67 }
68 j += 0x10;
69 }
70 printf("\nEXT Page 1");
71
72 val = PFUZE100_PAGE_REGISTER_PAGE1;
Marcel Ziswilera22d71c2019-02-08 18:12:12 +010073 if (dm_i2c_write(dev, PFUZE100_PAGE_REGISTER, &val, 1)) {
Max Krummenacher3b74ccf2016-11-30 19:43:08 +010074 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 Ziswilera22d71c2019-02-08 18:12:12 +010081 dm_i2c_read(dev, j + i, &val, 1);
Max Krummenacher3b74ccf2016-11-30 19:43:08 +010082 printf("\t%2x", val);
83 }
84 j += 0x10;
85 }
86 printf("\nEXT Page 2");
87
88 val = PFUZE100_PAGE_REGISTER_PAGE2;
Marcel Ziswilera22d71c2019-02-08 18:12:12 +010089 if (dm_i2c_write(dev, PFUZE100_PAGE_REGISTER, &val, 1)) {
Max Krummenacher3b74ccf2016-11-30 19:43:08 +010090 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 Ziswilera22d71c2019-02-08 18:12:12 +010097 dm_i2c_read(dev, j + i, &val, 1);
Max Krummenacher3b74ccf2016-11-30 19:43:08 +010098 printf("\t%2x", val);
99 }
100 j += 0x10;
101 }
102 printf("\n");
103 }
Marcel Ziswilera22d71c2019-02-08 18:12:12 +0100104#endif /* DEBUG */
Max Krummenacher3b74ccf2016-11-30 19:43:08 +0100105 /* get device programmed state */
106 val = PFUZE100_PAGE_REGISTER_PAGE1;
Marcel Ziswilera22d71c2019-02-08 18:12:12 +0100107 if (dm_i2c_write(dev, PFUZE100_PAGE_REGISTER, &val, 1)) {
Max Krummenacher3b74ccf2016-11-30 19:43:08 +0100108 puts("i2c write failed\n");
109 return 0;
110 }
Marcel Ziswilera22d71c2019-02-08 18:12:12 +0100111 if (dm_i2c_read(dev, PFUZE100_FUSE_POR1, &val, 1) < 0) {
Max Krummenacher3b74ccf2016-11-30 19:43:08 +0100112 puts("i2c fuse_por read failed\n");
113 return 0;
114 }
115 if (val & PFUZE100_FUSE_POR_M)
116 programmed++;
117
Marcel Ziswilera22d71c2019-02-08 18:12:12 +0100118 if (dm_i2c_read(dev, PFUZE100_FUSE_POR2, &val, 1) < 0) {
Max Krummenacher3b74ccf2016-11-30 19:43:08 +0100119 puts("i2c fuse_por read failed\n");
120 return programmed;
121 }
122 if (val & PFUZE100_FUSE_POR_M)
123 programmed++;
124
Marcel Ziswilera22d71c2019-02-08 18:12:12 +0100125 if (dm_i2c_read(dev, PFUZE100_FUSE_POR3, &val, 1) < 0) {
Max Krummenacher3b74ccf2016-11-30 19:43:08 +0100126 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 Ziswilera22d71c2019-02-08 18:12:12 +0100148 if (dm_i2c_write(dev, PFUZE100_VGEN1CTL, &val, 1)) {
Max Krummenacher3b74ccf2016-11-30 19:43:08 +0100149 puts("i2c write failed\n");
150 return programmed;
151 }
152
153 /* set SWBST to 5.0V */
154 val = PFUZE100_SWBST_VAL;
Marcel Ziswilera22d71c2019-02-08 18:12:12 +0100155 if (dm_i2c_write(dev, PFUZE100_SWBSTCTL, &val, 1))
Max Krummenacher3b74ccf2016-11-30 19:43:08 +0100156 puts("i2c write failed\n");
Max Krummenacher3b74ccf2016-11-30 19:43:08 +0100157 }
158 return programmed;
159}
160
Tom Rinic9515392018-01-03 09:19:17 -0500161#ifndef CONFIG_SPL_BUILD
162static int pf0100_prog(void)
Max Krummenacher3b74ccf2016-11-30 19:43:08 +0100163{
Marcel Ziswilera22d71c2019-02-08 18:12:12 +0100164 int rc;
165 struct udevice *dev = NULL;
Max Krummenacher3b74ccf2016-11-30 19:43:08 +0100166 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 Ziswilera22d71c2019-02-08 18:12:12 +0100179 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 Krummenacher3b74ccf2016-11-30 19:43:08 +0100183 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 Ziswilera22d71c2019-02-08 18:12:12 +0100190 if (dm_i2c_write(dev, pmic_otp_prog[i].reg, &val, 1)) {
Max Krummenacher3b74ccf2016-11-30 19:43:08 +0100191 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 Rinic9515392018-01-03 09:19:17 -0500211static int do_pf0100_prog(cmd_tbl_t *cmdtp, int flag, int argc,
Max Krummenacher3b74ccf2016-11-30 19:43:08 +0100212 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
224U_BOOT_CMD(
225 pf0100_otp_prog, 1, 0, do_pf0100_prog,
226 "Program the OTP fuses on the PMIC PF0100",
227 ""
228);
Marcel Ziswilera22d71c2019-02-08 18:12:12 +0100229#endif /* CONFIG_SPL_BUILD */