blob: 042f55ab73478e8b526139a88086ee583b5e3ef4 [file] [log] [blame]
Tim Harvey552c3582014-03-06 07:46:30 -08001/*
2 * Copyright (C) 2013 Gateworks Corporation
3 *
4 * Author: Tim Harvey <tharvey@gateworks.com>
5 *
6 * SPDX-License-Identifier: GPL-2.0+
7 */
8
9#include <asm/errno.h>
10#include <common.h>
11#include <i2c.h>
12#include <linux/ctype.h>
13
Tim Harveyd15181c2016-05-23 08:25:27 -070014#include "ventana_eeprom.h"
Tim Harvey552c3582014-03-06 07:46:30 -080015#include "gsc.h"
16
Tim Harvey552c3582014-03-06 07:46:30 -080017/*
18 * The Gateworks System Controller will fail to ACK a master transaction if
19 * it is busy, which can occur during its 1HZ timer tick while reading ADC's.
20 * When this does occur, it will never be busy long enough to fail more than
21 * 2 back-to-back transfers. Thus we wrap i2c_read and i2c_write with
22 * 3 retries.
23 */
24int gsc_i2c_read(uchar chip, uint addr, int alen, uchar *buf, int len)
25{
26 int retry = 3;
27 int n = 0;
28 int ret;
29
30 while (n++ < retry) {
31 ret = i2c_read(chip, addr, alen, buf, len);
32 if (!ret)
33 break;
34 debug("%s: 0x%02x 0x%02x retry%d: %d\n", __func__, chip, addr,
35 n, ret);
36 if (ret != -ENODEV)
37 break;
38 mdelay(10);
39 }
40 return ret;
41}
42
43int gsc_i2c_write(uchar chip, uint addr, int alen, uchar *buf, int len)
44{
45 int retry = 3;
46 int n = 0;
47 int ret;
48
49 while (n++ < retry) {
50 ret = i2c_write(chip, addr, alen, buf, len);
51 if (!ret)
52 break;
53 debug("%s: 0x%02x 0x%02x retry%d: %d\n", __func__, chip, addr,
54 n, ret);
55 if (ret != -ENODEV)
56 break;
57 mdelay(10);
58 }
Tim Harvey8d68c8e2014-08-07 22:35:44 -070059 mdelay(100);
Tim Harvey552c3582014-03-06 07:46:30 -080060 return ret;
61}
62
Tim Harveybd804512015-04-08 12:54:50 -070063static void read_hwmon(const char *name, uint reg, uint size)
Tim Harvey552c3582014-03-06 07:46:30 -080064{
65 unsigned char buf[3];
66 uint ui;
67
68 printf("%-8s:", name);
69 memset(buf, 0, sizeof(buf));
70 if (gsc_i2c_read(GSC_HWMON_ADDR, reg, 1, buf, size)) {
71 puts("fRD\n");
72 } else {
73 ui = buf[0] | (buf[1]<<8) | (buf[2]<<16);
Tim Harveyc37682f2016-05-24 11:03:51 -070074 if (reg == GSC_HWMON_TEMP && ui > 0x8000)
75 ui -= 0xffff;
Tim Harvey552c3582014-03-06 07:46:30 -080076 if (ui == 0xffffff)
Tim Harveybd804512015-04-08 12:54:50 -070077 puts("invalid\n");
Tim Harvey552c3582014-03-06 07:46:30 -080078 else
Tim Harveybd804512015-04-08 12:54:50 -070079 printf("%d\n", ui);
Tim Harvey552c3582014-03-06 07:46:30 -080080 }
Tim Harvey552c3582014-03-06 07:46:30 -080081}
82
Tim Harvey92e3d842015-04-08 12:54:59 -070083int gsc_info(int verbose)
Tim Harvey552c3582014-03-06 07:46:30 -080084{
Tim Harvey92e3d842015-04-08 12:54:59 -070085 unsigned char buf[16];
Tim Harvey552c3582014-03-06 07:46:30 -080086
87 i2c_set_bus_num(0);
Tim Harvey92e3d842015-04-08 12:54:59 -070088 if (gsc_i2c_read(GSC_SC_ADDR, 0, 1, buf, 16))
89 return CMD_RET_FAILURE;
90
91 printf("GSC: v%d", buf[GSC_SC_FWVER]);
92 printf(" 0x%04x", buf[GSC_SC_FWCRC] | buf[GSC_SC_FWCRC+1]<<8);
93 printf(" WDT:%sabled", (buf[GSC_SC_CTRL1] & (1<<GSC_SC_CTRL1_WDEN))
94 ? "en" : "dis");
95 if (buf[GSC_SC_STATUS] & (1 << GSC_SC_IRQ_WATCHDOG)) {
96 buf[GSC_SC_STATUS] &= ~(1 << GSC_SC_IRQ_WATCHDOG);
97 puts(" WDT_RESET");
98 gsc_i2c_write(GSC_SC_ADDR, GSC_SC_STATUS, 1,
99 &buf[GSC_SC_STATUS], 1);
100 }
Tim Harvey24535102016-05-24 11:03:52 -0700101 if (!gsc_i2c_read(GSC_HWMON_ADDR, GSC_HWMON_TEMP, 1, buf, 2)) {
102 int ui = buf[0] | buf[1]<<8;
103 if (ui > 0x8000)
104 ui -= 0xffff;
105 printf(" board temp at %dC", ui / 10);
106 }
Tim Harvey92e3d842015-04-08 12:54:59 -0700107 puts("\n");
108 if (!verbose)
109 return CMD_RET_SUCCESS;
110
Tim Harveybd804512015-04-08 12:54:50 -0700111 read_hwmon("Temp", GSC_HWMON_TEMP, 2);
112 read_hwmon("VIN", GSC_HWMON_VIN, 3);
113 read_hwmon("VBATT", GSC_HWMON_VBATT, 3);
114 read_hwmon("VDD_3P3", GSC_HWMON_VDD_3P3, 3);
Tim Harvey1ce5f7f2015-04-08 12:54:52 -0700115 read_hwmon("VDD_ARM", GSC_HWMON_VDD_CORE, 3);
116 read_hwmon("VDD_SOC", GSC_HWMON_VDD_SOC, 3);
Tim Harveybd804512015-04-08 12:54:50 -0700117 read_hwmon("VDD_HIGH", GSC_HWMON_VDD_HIGH, 3);
118 read_hwmon("VDD_DDR", GSC_HWMON_VDD_DDR, 3);
119 read_hwmon("VDD_5P0", GSC_HWMON_VDD_5P0, 3);
120 read_hwmon("VDD_2P5", GSC_HWMON_VDD_2P5, 3);
121 read_hwmon("VDD_1P8", GSC_HWMON_VDD_1P8, 3);
Tim Harvey1ce5f7f2015-04-08 12:54:52 -0700122 read_hwmon("VDD_IO2", GSC_HWMON_VDD_IO2, 3);
Tim Harveyd15181c2016-05-23 08:25:27 -0700123 switch (ventana_info.model[3]) {
Tim Harvey552c3582014-03-06 07:46:30 -0800124 case '1': /* GW51xx */
Tim Harvey1ce5f7f2015-04-08 12:54:52 -0700125 read_hwmon("VDD_IO3", GSC_HWMON_VDD_IO4, 3); /* -C rev */
Tim Harvey552c3582014-03-06 07:46:30 -0800126 break;
127 case '2': /* GW52xx */
Tim Harvey1ce5f7f2015-04-08 12:54:52 -0700128 break;
Tim Harvey552c3582014-03-06 07:46:30 -0800129 case '3': /* GW53xx */
Tim Harvey1ce5f7f2015-04-08 12:54:52 -0700130 read_hwmon("VDD_IO4", GSC_HWMON_VDD_IO4, 3); /* -C rev */
131 read_hwmon("VDD_GPS", GSC_HWMON_VDD_IO3, 3);
Tim Harvey552c3582014-03-06 07:46:30 -0800132 break;
133 case '4': /* GW54xx */
Tim Harvey1ce5f7f2015-04-08 12:54:52 -0700134 read_hwmon("VDD_IO3", GSC_HWMON_VDD_IO4, 3); /* -C rev */
135 read_hwmon("VDD_GPS", GSC_HWMON_VDD_IO3, 3);
Tim Harvey552c3582014-03-06 07:46:30 -0800136 break;
Tim Harvey50581832014-08-20 23:35:14 -0700137 case '5': /* GW55xx */
Tim Harvey50581832014-08-20 23:35:14 -0700138 break;
Tim Harvey552c3582014-03-06 07:46:30 -0800139 }
140 return 0;
141}
142
Tim Harvey40feabb2015-05-08 18:28:36 -0700143/*
144 * The Gateworks System Controller implements a boot
145 * watchdog (always enabled) as a workaround for IMX6 boot related
146 * errata such as:
147 * ERR005768 - no fix scheduled
148 * ERR006282 - fixed in silicon r1.2
149 * ERR007117 - fixed in silicon r1.3
150 * ERR007220 - fixed in silicon r1.3
151 * ERR007926 - no fix scheduled
152 * see http://cache.freescale.com/files/32bit/doc/errata/IMX6DQCE.pdf
153 *
154 * Disable the boot watchdog
155 */
156int gsc_boot_wd_disable(void)
157{
158 u8 reg;
159
160 i2c_set_bus_num(CONFIG_I2C_GSC);
161 if (!gsc_i2c_read(GSC_SC_ADDR, GSC_SC_CTRL1, 1, &reg, 1)) {
162 reg |= (1 << GSC_SC_CTRL1_WDDIS);
163 if (!gsc_i2c_write(GSC_SC_ADDR, GSC_SC_CTRL1, 1, &reg, 1))
164 return 0;
165 }
166 puts("Error: could not disable GSC Watchdog\n");
167 return 1;
168}
169
Tim Harvey92e3d842015-04-08 12:54:59 -0700170#ifdef CONFIG_CMD_GSC
Tim Harvey30ea7ee2016-05-24 11:03:50 -0700171static int do_gsc_sleep(cmd_tbl_t *cmdtp, int flag, int argc,
172 char * const argv[])
173{
174 unsigned char reg;
175 unsigned long secs = 0;
176
177 if (argc < 2)
178 return CMD_RET_USAGE;
179
180 secs = simple_strtoul(argv[1], NULL, 10);
181 printf("GSC Sleeping for %ld seconds\n", secs);
182
183 i2c_set_bus_num(0);
184 reg = (secs >> 24) & 0xff;
185 if (gsc_i2c_write(GSC_SC_ADDR, 9, 1, &reg, 1))
186 goto error;
187 reg = (secs >> 16) & 0xff;
188 if (gsc_i2c_write(GSC_SC_ADDR, 8, 1, &reg, 1))
189 goto error;
190 reg = (secs >> 8) & 0xff;
191 if (gsc_i2c_write(GSC_SC_ADDR, 7, 1, &reg, 1))
192 goto error;
193 reg = secs & 0xff;
194 if (gsc_i2c_write(GSC_SC_ADDR, 6, 1, &reg, 1))
195 goto error;
196 if (gsc_i2c_read(GSC_SC_ADDR, GSC_SC_CTRL1, 1, &reg, 1))
197 goto error;
198 reg |= (1 << 2);
199 if (gsc_i2c_write(GSC_SC_ADDR, GSC_SC_CTRL1, 1, &reg, 1))
200 goto error;
201 reg &= ~(1 << 2);
202 reg |= 0x3;
203 if (gsc_i2c_write(GSC_SC_ADDR, GSC_SC_CTRL1, 1, &reg, 1))
204 goto error;
205
206 return CMD_RET_SUCCESS;
207
208error:
209 printf("i2c error\n");
210 return CMD_RET_FAILURE;
211}
212
Tim Harvey92e3d842015-04-08 12:54:59 -0700213static int do_gsc_wd(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
214{
215 unsigned char reg;
216
217 if (argc < 2)
218 return CMD_RET_USAGE;
219
220 if (strcasecmp(argv[1], "enable") == 0) {
221 int timeout = 0;
222
223 if (argc > 2)
224 timeout = simple_strtoul(argv[2], NULL, 10);
225 i2c_set_bus_num(0);
226 if (gsc_i2c_read(GSC_SC_ADDR, GSC_SC_CTRL1, 1, &reg, 1))
227 return CMD_RET_FAILURE;
228 reg &= ~((1 << GSC_SC_CTRL1_WDEN) | (1 << GSC_SC_CTRL1_WDTIME));
229 if (timeout == 60)
230 reg |= (1 << GSC_SC_CTRL1_WDTIME);
231 else
232 timeout = 30;
233 reg |= (1 << GSC_SC_CTRL1_WDEN);
234 if (gsc_i2c_write(GSC_SC_ADDR, GSC_SC_CTRL1, 1, &reg, 1))
235 return CMD_RET_FAILURE;
236 printf("GSC Watchdog enabled with timeout=%d seconds\n",
237 timeout);
238 } else if (strcasecmp(argv[1], "disable") == 0) {
239 i2c_set_bus_num(0);
240 if (gsc_i2c_read(GSC_SC_ADDR, GSC_SC_CTRL1, 1, &reg, 1))
241 return CMD_RET_FAILURE;
242 reg &= ~((1 << GSC_SC_CTRL1_WDEN) | (1 << GSC_SC_CTRL1_WDTIME));
243 if (gsc_i2c_write(GSC_SC_ADDR, GSC_SC_CTRL1, 1, &reg, 1))
244 return CMD_RET_FAILURE;
245 printf("GSC Watchdog disabled\n");
246 } else {
247 return CMD_RET_USAGE;
248 }
249 return CMD_RET_SUCCESS;
250}
251
252static int do_gsc(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
253{
254 if (argc < 2)
255 return gsc_info(1);
256
257 if (strcasecmp(argv[1], "wd") == 0)
258 return do_gsc_wd(cmdtp, flag, --argc, ++argv);
Tim Harvey30ea7ee2016-05-24 11:03:50 -0700259 else if (strcasecmp(argv[1], "sleep") == 0)
260 return do_gsc_sleep(cmdtp, flag, --argc, ++argv);
Tim Harvey92e3d842015-04-08 12:54:59 -0700261
262 return CMD_RET_USAGE;
263}
264
265U_BOOT_CMD(
266 gsc, 4, 1, do_gsc, "GSC configuration",
Tim Harvey30ea7ee2016-05-24 11:03:50 -0700267 "[wd enable [30|60]]|[wd disable]|[sleep <secs>]\n"
Tim Harvey92e3d842015-04-08 12:54:59 -0700268 );
Tim Harvey552c3582014-03-06 07:46:30 -0800269
270#endif /* CONFIG_CMD_GSC */