blob: 4179f0fb5c0264e0507908f53ec38ecab4d94729 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Tim Harvey552c3582014-03-06 07:46:30 -08002/*
3 * Copyright (C) 2013 Gateworks Corporation
4 *
5 * Author: Tim Harvey <tharvey@gateworks.com>
Tim Harvey552c3582014-03-06 07:46:30 -08006 */
7
Simon Glass0f2af882020-05-10 11:40:05 -06008#include <common.h>
Simon Glassed38aef2020-05-10 11:40:03 -06009#include <command.h>
Simon Glass0f2af882020-05-10 11:40:05 -060010#include <log.h>
Masahiro Yamada56a931c2016-09-21 11:28:55 +090011#include <linux/errno.h>
Tim Harvey552c3582014-03-06 07:46:30 -080012#include <common.h>
13#include <i2c.h>
14#include <linux/ctype.h>
15
Tim Harveyd15181c2016-05-23 08:25:27 -070016#include "ventana_eeprom.h"
Tim Harvey552c3582014-03-06 07:46:30 -080017#include "gsc.h"
18
Tim Harvey552c3582014-03-06 07:46:30 -080019/*
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 */
26int 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
45int 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 }
Tim Harvey8d68c8e2014-08-07 22:35:44 -070061 mdelay(100);
Tim Harvey552c3582014-03-06 07:46:30 -080062 return ret;
63}
64
Tim Harveybd804512015-04-08 12:54:50 -070065static void read_hwmon(const char *name, uint reg, uint size)
Tim Harvey552c3582014-03-06 07:46:30 -080066{
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);
Tim Harveya2d24c92019-02-04 13:10:50 -080076 if (size == 2 && ui > 0x8000)
Tim Harveyc37682f2016-05-24 11:03:51 -070077 ui -= 0xffff;
Tim Harvey552c3582014-03-06 07:46:30 -080078 if (ui == 0xffffff)
Tim Harveybd804512015-04-08 12:54:50 -070079 puts("invalid\n");
Tim Harvey552c3582014-03-06 07:46:30 -080080 else
Tim Harveybd804512015-04-08 12:54:50 -070081 printf("%d\n", ui);
Tim Harvey552c3582014-03-06 07:46:30 -080082 }
Tim Harvey552c3582014-03-06 07:46:30 -080083}
84
Tim Harvey92e3d842015-04-08 12:54:59 -070085int gsc_info(int verbose)
Tim Harvey552c3582014-03-06 07:46:30 -080086{
Tim Harvey92e3d842015-04-08 12:54:59 -070087 unsigned char buf[16];
Tim Harvey552c3582014-03-06 07:46:30 -080088
89 i2c_set_bus_num(0);
Tim Harvey92e3d842015-04-08 12:54:59 -070090 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 }
Tim Harvey24535102016-05-24 11:03:52 -0700103 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 }
Tim Harvey92e3d842015-04-08 12:54:59 -0700109 puts("\n");
110 if (!verbose)
111 return CMD_RET_SUCCESS;
112
Tim Harveybd804512015-04-08 12:54:50 -0700113 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);
Tim Harvey1ce5f7f2015-04-08 12:54:52 -0700117 read_hwmon("VDD_ARM", GSC_HWMON_VDD_CORE, 3);
118 read_hwmon("VDD_SOC", GSC_HWMON_VDD_SOC, 3);
Tim Harveybd804512015-04-08 12:54:50 -0700119 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);
Tim Harvey892068c2016-05-24 11:03:58 -0700122 if (strncasecmp((const char*) ventana_info.model, "GW553", 5))
123 read_hwmon("VDD_2P5", GSC_HWMON_VDD_2P5, 3);
Tim Harveybd804512015-04-08 12:54:50 -0700124 read_hwmon("VDD_1P8", GSC_HWMON_VDD_1P8, 3);
Tim Harvey1ce5f7f2015-04-08 12:54:52 -0700125 read_hwmon("VDD_IO2", GSC_HWMON_VDD_IO2, 3);
Tim Harveyd15181c2016-05-23 08:25:27 -0700126 switch (ventana_info.model[3]) {
Tim Harvey552c3582014-03-06 07:46:30 -0800127 case '1': /* GW51xx */
Tim Harvey1ce5f7f2015-04-08 12:54:52 -0700128 read_hwmon("VDD_IO3", GSC_HWMON_VDD_IO4, 3); /* -C rev */
Tim Harvey552c3582014-03-06 07:46:30 -0800129 break;
130 case '2': /* GW52xx */
Tim Harvey1ce5f7f2015-04-08 12:54:52 -0700131 break;
Tim Harvey552c3582014-03-06 07:46:30 -0800132 case '3': /* GW53xx */
Tim Harvey1ce5f7f2015-04-08 12:54:52 -0700133 read_hwmon("VDD_IO4", GSC_HWMON_VDD_IO4, 3); /* -C rev */
134 read_hwmon("VDD_GPS", GSC_HWMON_VDD_IO3, 3);
Tim Harvey552c3582014-03-06 07:46:30 -0800135 break;
136 case '4': /* GW54xx */
Tim Harvey1ce5f7f2015-04-08 12:54:52 -0700137 read_hwmon("VDD_IO3", GSC_HWMON_VDD_IO4, 3); /* -C rev */
138 read_hwmon("VDD_GPS", GSC_HWMON_VDD_IO3, 3);
Tim Harvey552c3582014-03-06 07:46:30 -0800139 break;
Tim Harvey50581832014-08-20 23:35:14 -0700140 case '5': /* GW55xx */
Tim Harvey50581832014-08-20 23:35:14 -0700141 break;
Tim Harvey659441b2017-03-17 07:31:02 -0700142 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;
Tim Harveya2d24c92019-02-04 13:10:50 -0800146 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);
Tim Harvey552c3582014-03-06 07:46:30 -0800150 }
151 return 0;
152}
153
Tim Harvey40feabb2015-05-08 18:28:36 -0700154/*
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 */
167int 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
Tom Rinib6f576a2018-01-03 09:16:29 -0500181#if defined(CONFIG_CMD_GSC) && !defined(CONFIG_SPL_BUILD)
Simon Glassed38aef2020-05-10 11:40:03 -0600182static int do_gsc_sleep(struct cmd_tbl *cmdtp, int flag, int argc,
183 char *const argv[])
Tim Harvey30ea7ee2016-05-24 11:03:50 -0700184{
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
219error:
220 printf("i2c error\n");
221 return CMD_RET_FAILURE;
222}
223
Simon Glassed38aef2020-05-10 11:40:03 -0600224static int do_gsc_wd(struct cmd_tbl *cmdtp, int flag, int argc,
225 char *const argv[])
Tim Harvey92e3d842015-04-08 12:54:59 -0700226{
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
Simon Glassed38aef2020-05-10 11:40:03 -0600264static int do_gsc(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
Tim Harvey92e3d842015-04-08 12:54:59 -0700265{
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);
Tim Harvey30ea7ee2016-05-24 11:03:50 -0700271 else if (strcasecmp(argv[1], "sleep") == 0)
272 return do_gsc_sleep(cmdtp, flag, --argc, ++argv);
Tim Harvey92e3d842015-04-08 12:54:59 -0700273
274 return CMD_RET_USAGE;
275}
276
277U_BOOT_CMD(
278 gsc, 4, 1, do_gsc, "GSC configuration",
Tim Harvey30ea7ee2016-05-24 11:03:50 -0700279 "[wd enable [30|60]]|[wd disable]|[sleep <secs>]\n"
Tim Harvey92e3d842015-04-08 12:54:59 -0700280 );
Tim Harvey552c3582014-03-06 07:46:30 -0800281
282#endif /* CONFIG_CMD_GSC */