4179f0fb5c0264e0507908f53ec38ecab4d94729
[oweals/u-boot.git] / board / gateworks / gw_ventana / gsc.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (C) 2013 Gateworks Corporation
4  *
5  * Author: Tim Harvey <tharvey@gateworks.com>
6  */
7
8 #include <common.h>
9 #include <command.h>
10 #include <log.h>
11 #include <linux/errno.h>
12 #include <common.h>
13 #include <i2c.h>
14 #include <linux/ctype.h>
15
16 #include "ventana_eeprom.h"
17 #include "gsc.h"
18
19 /*
20  * The Gateworks System Controller will fail to ACK a master transaction if
21  * it is busy, which can occur during its 1HZ timer tick while reading ADC's.
22  * When this does occur, it will never be busy long enough to fail more than
23  * 2 back-to-back transfers.  Thus we wrap i2c_read and i2c_write with
24  * 3 retries.
25  */
26 int gsc_i2c_read(uchar chip, uint addr, int alen, uchar *buf, int len)
27 {
28         int retry = 3;
29         int n = 0;
30         int ret;
31
32         while (n++ < retry) {
33                 ret = i2c_read(chip, addr, alen, buf, len);
34                 if (!ret)
35                         break;
36                 debug("%s: 0x%02x 0x%02x retry%d: %d\n", __func__, chip, addr,
37                       n, ret);
38                 if (ret != -ENODEV)
39                         break;
40                 mdelay(10);
41         }
42         return ret;
43 }
44
45 int gsc_i2c_write(uchar chip, uint addr, int alen, uchar *buf, int len)
46 {
47         int retry = 3;
48         int n = 0;
49         int ret;
50
51         while (n++ < retry) {
52                 ret = i2c_write(chip, addr, alen, buf, len);
53                 if (!ret)
54                         break;
55                 debug("%s: 0x%02x 0x%02x retry%d: %d\n", __func__, chip, addr,
56                       n, ret);
57                 if (ret != -ENODEV)
58                         break;
59                 mdelay(10);
60         }
61         mdelay(100);
62         return ret;
63 }
64
65 static void read_hwmon(const char *name, uint reg, uint size)
66 {
67         unsigned char buf[3];
68         uint ui;
69
70         printf("%-8s:", name);
71         memset(buf, 0, sizeof(buf));
72         if (gsc_i2c_read(GSC_HWMON_ADDR, reg, 1, buf, size)) {
73                 puts("fRD\n");
74         } else {
75                 ui = buf[0] | (buf[1]<<8) | (buf[2]<<16);
76                 if (size == 2 && ui > 0x8000)
77                         ui -= 0xffff;
78                 if (ui == 0xffffff)
79                         puts("invalid\n");
80                 else
81                         printf("%d\n", ui);
82         }
83 }
84
85 int gsc_info(int verbose)
86 {
87         unsigned char buf[16];
88
89         i2c_set_bus_num(0);
90         if (gsc_i2c_read(GSC_SC_ADDR, 0, 1, buf, 16))
91                 return CMD_RET_FAILURE;
92
93         printf("GSC:   v%d", buf[GSC_SC_FWVER]);
94         printf(" 0x%04x", buf[GSC_SC_FWCRC] | buf[GSC_SC_FWCRC+1]<<8);
95         printf(" WDT:%sabled", (buf[GSC_SC_CTRL1] & (1<<GSC_SC_CTRL1_WDEN))
96                 ? "en" : "dis");
97         if (buf[GSC_SC_STATUS] & (1 << GSC_SC_IRQ_WATCHDOG)) {
98                 buf[GSC_SC_STATUS] &= ~(1 << GSC_SC_IRQ_WATCHDOG);
99                 puts(" WDT_RESET");
100                 gsc_i2c_write(GSC_SC_ADDR, GSC_SC_STATUS, 1,
101                               &buf[GSC_SC_STATUS], 1);
102         }
103         if (!gsc_i2c_read(GSC_HWMON_ADDR, GSC_HWMON_TEMP, 1, buf, 2)) {
104                 int ui = buf[0] | buf[1]<<8;
105                 if (ui > 0x8000)
106                         ui -= 0xffff;
107                 printf(" board temp at %dC", ui / 10);
108         }
109         puts("\n");
110         if (!verbose)
111                 return CMD_RET_SUCCESS;
112
113         read_hwmon("Temp",     GSC_HWMON_TEMP, 2);
114         read_hwmon("VIN",      GSC_HWMON_VIN, 3);
115         read_hwmon("VBATT",    GSC_HWMON_VBATT, 3);
116         read_hwmon("VDD_3P3",  GSC_HWMON_VDD_3P3, 3);
117         read_hwmon("VDD_ARM",  GSC_HWMON_VDD_CORE, 3);
118         read_hwmon("VDD_SOC",  GSC_HWMON_VDD_SOC, 3);
119         read_hwmon("VDD_HIGH", GSC_HWMON_VDD_HIGH, 3);
120         read_hwmon("VDD_DDR",  GSC_HWMON_VDD_DDR, 3);
121         read_hwmon("VDD_5P0",  GSC_HWMON_VDD_5P0, 3);
122         if (strncasecmp((const char*) ventana_info.model, "GW553", 5))
123                 read_hwmon("VDD_2P5",  GSC_HWMON_VDD_2P5, 3);
124         read_hwmon("VDD_1P8",  GSC_HWMON_VDD_1P8, 3);
125         read_hwmon("VDD_IO2",  GSC_HWMON_VDD_IO2, 3);
126         switch (ventana_info.model[3]) {
127         case '1': /* GW51xx */
128                 read_hwmon("VDD_IO3",  GSC_HWMON_VDD_IO4, 3); /* -C rev */
129                 break;
130         case '2': /* GW52xx */
131                 break;
132         case '3': /* GW53xx */
133                 read_hwmon("VDD_IO4",  GSC_HWMON_VDD_IO4, 3); /* -C rev */
134                 read_hwmon("VDD_GPS",  GSC_HWMON_VDD_IO3, 3);
135                 break;
136         case '4': /* GW54xx */
137                 read_hwmon("VDD_IO3",  GSC_HWMON_VDD_IO4, 3); /* -C rev */
138                 read_hwmon("VDD_GPS",  GSC_HWMON_VDD_IO3, 3);
139                 break;
140         case '5': /* GW55xx */
141                 break;
142         case '6': /* GW560x */
143                 read_hwmon("VDD_IO4",  GSC_HWMON_VDD_IO4, 3);
144                 read_hwmon("VDD_GPS",  GSC_HWMON_VDD_IO3, 3);
145                 break;
146         case '9': /* GW590x */
147                 read_hwmon("AMONBMON",  GSC_HWMON_VDD_IO3, 3);
148                 read_hwmon("BAT_VOLT",  GSC_HWMON_VDD_EXT, 3);
149                 read_hwmon("BAT_TEMP",  GSC_HWMON_VDD_IO4, 2);
150         }
151         return 0;
152 }
153
154 /*
155  *  The Gateworks System Controller implements a boot
156  *  watchdog (always enabled) as a workaround for IMX6 boot related
157  *  errata such as:
158  *    ERR005768 - no fix scheduled
159  *    ERR006282 - fixed in silicon r1.2
160  *    ERR007117 - fixed in silicon r1.3
161  *    ERR007220 - fixed in silicon r1.3
162  *    ERR007926 - no fix scheduled
163  *  see http://cache.freescale.com/files/32bit/doc/errata/IMX6DQCE.pdf
164  *
165  * Disable the boot watchdog
166  */
167 int gsc_boot_wd_disable(void)
168 {
169         u8 reg;
170
171         i2c_set_bus_num(CONFIG_I2C_GSC);
172         if (!gsc_i2c_read(GSC_SC_ADDR, GSC_SC_CTRL1, 1, &reg, 1)) {
173                 reg |= (1 << GSC_SC_CTRL1_WDDIS);
174                 if (!gsc_i2c_write(GSC_SC_ADDR, GSC_SC_CTRL1, 1, &reg, 1))
175                         return 0;
176         }
177         puts("Error: could not disable GSC Watchdog\n");
178         return 1;
179 }
180
181 #if defined(CONFIG_CMD_GSC) && !defined(CONFIG_SPL_BUILD)
182 static int do_gsc_sleep(struct cmd_tbl *cmdtp, int flag, int argc,
183                         char *const argv[])
184 {
185         unsigned char reg;
186         unsigned long secs = 0;
187
188         if (argc < 2)
189                 return CMD_RET_USAGE;
190
191         secs = simple_strtoul(argv[1], NULL, 10);
192         printf("GSC Sleeping for %ld seconds\n", secs);
193
194         i2c_set_bus_num(0);
195         reg = (secs >> 24) & 0xff;
196         if (gsc_i2c_write(GSC_SC_ADDR, 9, 1, &reg, 1))
197                 goto error;
198         reg = (secs >> 16) & 0xff;
199         if (gsc_i2c_write(GSC_SC_ADDR, 8, 1, &reg, 1))
200                 goto error;
201         reg = (secs >> 8) & 0xff;
202         if (gsc_i2c_write(GSC_SC_ADDR, 7, 1, &reg, 1))
203                 goto error;
204         reg = secs & 0xff;
205         if (gsc_i2c_write(GSC_SC_ADDR, 6, 1, &reg, 1))
206                 goto error;
207         if (gsc_i2c_read(GSC_SC_ADDR, GSC_SC_CTRL1, 1, &reg, 1))
208                 goto error;
209         reg |= (1 << 2);
210         if (gsc_i2c_write(GSC_SC_ADDR, GSC_SC_CTRL1, 1, &reg, 1))
211                 goto error;
212         reg &= ~(1 << 2);
213         reg |= 0x3;
214         if (gsc_i2c_write(GSC_SC_ADDR, GSC_SC_CTRL1, 1, &reg, 1))
215                 goto error;
216
217         return CMD_RET_SUCCESS;
218
219 error:
220         printf("i2c error\n");
221         return CMD_RET_FAILURE;
222 }
223
224 static int do_gsc_wd(struct cmd_tbl *cmdtp, int flag, int argc,
225                      char *const argv[])
226 {
227         unsigned char reg;
228
229         if (argc < 2)
230                 return CMD_RET_USAGE;
231
232         if (strcasecmp(argv[1], "enable") == 0) {
233                 int timeout = 0;
234
235                 if (argc > 2)
236                         timeout = simple_strtoul(argv[2], NULL, 10);
237                 i2c_set_bus_num(0);
238                 if (gsc_i2c_read(GSC_SC_ADDR, GSC_SC_CTRL1, 1, &reg, 1))
239                         return CMD_RET_FAILURE;
240                 reg &= ~((1 << GSC_SC_CTRL1_WDEN) | (1 << GSC_SC_CTRL1_WDTIME));
241                 if (timeout == 60)
242                         reg |= (1 << GSC_SC_CTRL1_WDTIME);
243                 else
244                         timeout = 30;
245                 reg |= (1 << GSC_SC_CTRL1_WDEN);
246                 if (gsc_i2c_write(GSC_SC_ADDR, GSC_SC_CTRL1, 1, &reg, 1))
247                         return CMD_RET_FAILURE;
248                 printf("GSC Watchdog enabled with timeout=%d seconds\n",
249                        timeout);
250         } else if (strcasecmp(argv[1], "disable") == 0) {
251                 i2c_set_bus_num(0);
252                 if (gsc_i2c_read(GSC_SC_ADDR, GSC_SC_CTRL1, 1, &reg, 1))
253                         return CMD_RET_FAILURE;
254                 reg &= ~((1 << GSC_SC_CTRL1_WDEN) | (1 << GSC_SC_CTRL1_WDTIME));
255                 if (gsc_i2c_write(GSC_SC_ADDR, GSC_SC_CTRL1, 1, &reg, 1))
256                         return CMD_RET_FAILURE;
257                 printf("GSC Watchdog disabled\n");
258         } else {
259                 return CMD_RET_USAGE;
260         }
261         return CMD_RET_SUCCESS;
262 }
263
264 static int do_gsc(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
265 {
266         if (argc < 2)
267                 return gsc_info(1);
268
269         if (strcasecmp(argv[1], "wd") == 0)
270                 return do_gsc_wd(cmdtp, flag, --argc, ++argv);
271         else if (strcasecmp(argv[1], "sleep") == 0)
272                 return do_gsc_sleep(cmdtp, flag, --argc, ++argv);
273
274         return CMD_RET_USAGE;
275 }
276
277 U_BOOT_CMD(
278         gsc, 4, 1, do_gsc, "GSC configuration",
279         "[wd enable [30|60]]|[wd disable]|[sleep <secs>]\n"
280         );
281
282 #endif /* CONFIG_CMD_GSC */