blob: 72a13abaaeedb91adaa7f092e236a22d475335cd [file] [log] [blame]
Tim Harveyb8204602022-03-07 16:24:04 -08001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright 2022 Gateworks Corporation
4 */
5
6#include <command.h>
7#include <gsc.h>
8#include <i2c.h>
9#include <rtc.h>
10#include <asm/unaligned.h>
11#include <linux/delay.h>
12#include <dm/device.h>
13#include <dm/device-internal.h>
14#include <dm/ofnode.h>
15#include <dm/read.h>
16
17#define GSC_BUSNO 0
18#define GSC_SC_ADDR 0x20
19#define GSC_HWMON_ADDR 0x29
20#define GSC_RTC_ADDR 0x68
21
22/* System Controller registers */
23enum {
24 GSC_SC_CTRL0 = 0,
25 GSC_SC_CTRL1 = 1,
26 GSC_SC_TIME = 2,
27 GSC_SC_TIME_ADD = 6,
28 GSC_SC_STATUS = 10,
29 GSC_SC_FWCRC = 12,
30 GSC_SC_FWVER = 14,
31 GSC_SC_WP = 15,
32 GSC_SC_RST_CAUSE = 16,
33 GSC_SC_THERM_PROTECT = 19,
34};
35
36/* System Controller Control1 bits */
37enum {
38 GSC_SC_CTRL1_SLEEP_EN = 0, /* 1 = enable sleep */
39 GSC_SC_CTRL1_SLEEP_ACTIVATE = 1, /* 1 = activate sleep */
40 GSC_SC_CTRL1_SLEEP_ADD = 2, /* 1 = latch and add sleep time */
41 GSC_SC_CTRL1_SLEEP_NOWAKEPB = 3, /* 1 = do not wake on sleep on button press */
42 GSC_SC_CTRL1_WDTIME = 4, /* 1 = 60s timeout, 0 = 30s timeout */
43 GSC_SC_CTRL1_WDEN = 5, /* 1 = enable, 0 = disable */
44 GSC_SC_CTRL1_BOOT_CHK = 6, /* 1 = enable alt boot check */
45 GSC_SC_CTRL1_WDDIS = 7, /* 1 = disable boot watchdog */
46};
47
48/* System Controller Interrupt bits */
49enum {
50 GSC_SC_IRQ_PB = 0, /* Pushbutton switch */
51 GSC_SC_IRQ_SECURE = 1, /* Secure Key erase operation complete */
52 GSC_SC_IRQ_EEPROM_WP = 2, /* EEPROM write violation */
53 GSC_SC_IRQ_GPIO = 4, /* GPIO change */
54 GSC_SC_IRQ_TAMPER = 5, /* Tamper detect */
55 GSC_SC_IRQ_WATCHDOG = 6, /* Watchdog trip */
56 GSC_SC_IRQ_PBLONG = 7, /* Pushbutton long hold */
57};
58
59/* System Controller WP bits */
60enum {
61 GSC_SC_WP_ALL = 0, /* Write Protect All EEPROM regions */
62 GSC_SC_WP_BOARDINFO = 1, /* Write Protect Board Info region */
63};
64
65/* System Controller Reset Cause */
66enum {
67 GSC_SC_RST_CAUSE_VIN = 0,
68 GSC_SC_RST_CAUSE_PB = 1,
69 GSC_SC_RST_CAUSE_WDT = 2,
70 GSC_SC_RST_CAUSE_CPU = 3,
71 GSC_SC_RST_CAUSE_TEMP_LOCAL = 4,
72 GSC_SC_RST_CAUSE_TEMP_REMOTE = 5,
73 GSC_SC_RST_CAUSE_SLEEP = 6,
74 GSC_SC_RST_CAUSE_BOOT_WDT = 7,
75 GSC_SC_RST_CAUSE_BOOT_WDT_MAN = 8,
76 GSC_SC_RST_CAUSE_SOFT_PWR = 9,
77 GSC_SC_RST_CAUSE_MAX = 10,
78};
79
Tom Rini952cc382022-12-04 10:14:13 -050080#if CONFIG_IS_ENABLED(DM_I2C)
Tim Harveyb8204602022-03-07 16:24:04 -080081
82struct gsc_priv {
83 int gscver;
84 int fwver;
85 int fwcrc;
86 struct udevice *hwmon;
87 struct udevice *rtc;
88};
89
90/*
91 * GSCv2 will fail to ACK an I2C transaction if it is busy, which can occur
92 * during its 1HZ timer tick while reading ADC's. When this does occur,
93 * it will never be busy longer than 2 back-to-back transfers so retry 3 times.
94 */
95static int gsc_i2c_read(struct udevice *dev, uint addr, u8 *buf, int len)
96{
97 struct gsc_priv *priv = dev_get_priv(dev);
98 int retry = (priv->gscver == 3) ? 1 : 3;
99 int n = 0;
100 int ret;
101
102 while (n++ < retry) {
103 ret = dm_i2c_read(dev, addr, buf, len);
104 if (!ret)
105 break;
106 if (ret != -EREMOTEIO)
107 break;
108 mdelay(10);
109 }
110 return ret;
111}
112
113static int gsc_i2c_write(struct udevice *dev, uint addr, const u8 *buf, int len)
114{
115 struct gsc_priv *priv = dev_get_priv(dev);
116 int retry = (priv->gscver == 3) ? 1 : 3;
117 int n = 0;
118 int ret;
119
120 while (n++ < retry) {
121 ret = dm_i2c_write(dev, addr, buf, len);
122 if (!ret)
123 break;
124 if (ret != -EREMOTEIO)
125 break;
126 mdelay(10);
127 }
128 return ret;
129}
130
131static struct udevice *gsc_get_dev(int busno, int slave)
132{
133 struct udevice *dev, *bus;
134 int ret;
135
136 ret = uclass_get_device_by_seq(UCLASS_I2C, busno, &bus);
137 if (ret)
138 return NULL;
139 ret = dm_i2c_probe(bus, slave, 0, &dev);
140 if (ret)
141 return NULL;
142
143 return dev;
144}
145
146static int gsc_thermal_get_info(struct udevice *dev, u8 *outreg, int *tmax, bool *enable)
147{
148 struct gsc_priv *priv = dev_get_priv(dev);
149 int ret;
150 u8 reg;
151
152 if (priv->gscver > 2 && priv->fwver > 52) {
153 ret = gsc_i2c_read(dev, GSC_SC_THERM_PROTECT, &reg, 1);
154 if (!ret) {
155 if (outreg)
156 *outreg = reg;
157 if (tmax) {
158 *tmax = ((reg & 0xf8) >> 3) * 2;
159 if (*tmax)
160 *tmax += 70;
161 else
162 *tmax = 120;
163 }
164 if (enable)
165 *enable = reg & 1;
166 }
167 } else {
168 ret = -ENODEV;
169 }
170
171 return ret;
172}
173
174static int gsc_thermal_get_temp(struct udevice *dev)
175{
176 struct gsc_priv *priv = dev_get_priv(dev);
177 u32 reg, mode, val;
178 const char *label;
179 ofnode node;
180 u8 buf[2];
181
182 ofnode_for_each_subnode(node, dev_read_subnode(dev, "adc")) {
183 if (ofnode_read_u32(node, "reg", &reg))
184 reg = -1;
185 if (ofnode_read_u32(node, "gw,mode", &mode))
186 mode = -1;
187 label = ofnode_read_string(node, "label");
188
189 if ((reg == -1) || (mode == -1) || !label)
190 continue;
191
192 if (mode != 0 || strcmp(label, "temp"))
193 continue;
194
195 memset(buf, 0, sizeof(buf));
196 if (!gsc_i2c_read(priv->hwmon, reg, buf, sizeof(buf))) {
197 val = buf[0] | buf[1] << 8;
198 if (val > 0x8000)
199 val -= 0xffff;
200 return val;
201 }
202 }
203
204 return 0;
205}
206
207static void gsc_thermal_info(struct udevice *dev)
208{
209 struct gsc_priv *priv = dev_get_priv(dev);
210
211 switch (priv->gscver) {
212 case 2:
213 printf("board_temp:%dC ", gsc_thermal_get_temp(dev) / 10);
214 break;
215 case 3:
216 if (priv->fwver > 52) {
217 bool enabled;
218 int tmax;
219
220 if (!gsc_thermal_get_info(dev, NULL, &tmax, &enabled)) {
221 puts("Thermal protection:");
222 if (enabled)
223 printf("enabled at %dC ", tmax);
224 else
225 puts("disabled ");
226 }
227 }
228 break;
229 }
230}
231
232static void gsc_reset_info(struct udevice *dev)
233{
234 struct gsc_priv *priv = dev_get_priv(dev);
235 static const char * const names[] = {
236 "VIN",
237 "PB",
238 "WDT",
239 "CPU",
240 "TEMP_L",
241 "TEMP_R",
242 "SLEEP",
243 "BOOT_WDT1",
244 "BOOT_WDT2",
245 "SOFT_PWR",
246 };
247 u8 reg;
248
249 /* reset cause */
250 switch (priv->gscver) {
251 case 2:
252 if (!gsc_i2c_read(dev, GSC_SC_STATUS, &reg, 1)) {
253 if (reg & BIT(GSC_SC_IRQ_WATCHDOG)) {
254 puts("RST:WDT");
255 reg &= ~BIT(GSC_SC_IRQ_WATCHDOG);
256 gsc_i2c_write(dev, GSC_SC_STATUS, &reg, 1);
257 } else {
258 puts("RST:VIN");
259 }
260 printf(" WDT:%sabled ",
261 (reg & BIT(GSC_SC_CTRL1_WDEN)) ? "en" : "dis");
262 }
263 break;
264 case 3:
265 if (priv->fwver > 52 &&
266 !gsc_i2c_read(dev, GSC_SC_RST_CAUSE, &reg, 1)) {
267 puts("RST:");
268 if (reg < ARRAY_SIZE(names))
269 printf("%s ", names[reg]);
270 else
271 printf("0x%02x ", reg);
272 }
273 break;
274 }
275}
276
277/* display hardware monitor ADC channels */
278static int gsc_hwmon(struct udevice *dev)
279{
280 struct gsc_priv *priv = dev_get_priv(dev);
281 u32 reg, mode, val, offset;
282 const char *label;
283 ofnode node;
284 u8 buf[2];
285 u32 r[2];
286 int ret;
287
288 /* iterate over hwmon nodes */
289 ofnode_for_each_subnode(node, dev_read_subnode(dev, "adc")) {
290 if (ofnode_read_u32(node, "reg", &reg))
291 reg = -1;
292 if (ofnode_read_u32(node, "gw,mode", &mode))
293 mode = -1;
294 label = ofnode_read_string(node, "label");
295 if ((reg == -1) || (mode == -1) || !label)
296 continue;
297
298 memset(buf, 0, sizeof(buf));
299 ret = gsc_i2c_read(priv->hwmon, reg, buf, sizeof(buf));
300 if (ret) {
301 printf("i2c error: %d\n", ret);
302 continue;
303 }
304 val = buf[0] | buf[1] << 8;
305
306 switch (mode) {
307 case 0: /* temperature (C*10) */
308 if (val > 0x8000)
309 val -= 0xffff;
310 printf("%-8s: %d.%ldC\n", label, val / 10, abs(val % 10));
311 break;
312 case 1: /* prescaled voltage */
Tim Harvey7f08f0c2024-12-18 11:45:42 -0800313 case 3:
Tim Harveyb8204602022-03-07 16:24:04 -0800314 if (val != 0xffff)
315 printf("%-8s: %d.%03dV\n", label, val / 1000, val % 1000);
316 break;
317 case 2: /* scaled based on ref volt and resolution */
318 val *= 2500;
319 val /= 1 << 12;
320
321 /* apply pre-scaler voltage divider */
322 if (!ofnode_read_u32_index(node, "gw,voltage-divider-ohms", 0, &r[0]) &&
323 !ofnode_read_u32_index(node, "gw,voltage-divider-ohms", 1, &r[1]) &&
324 r[0] && r[1]) {
325 val *= (r[0] + r[1]);
326 val /= r[1];
327 }
328
329 /* adjust by offset */
330 val += (offset / 1000);
331
332 printf("%-8s: %d.%03dV\n", label, val / 1000, val % 1000);
333 break;
Tim Harvey9f1b36e2024-12-18 11:45:41 -0800334 case 4: /* revolutions per minute */
335 printf("%-8s: %drpm\n", label, val * 30);
336 break;
Tim Harveyb8204602022-03-07 16:24:04 -0800337 }
338 }
339
340 return 0;
341}
342
343static int gsc_banner(struct udevice *dev)
344{
345 struct gsc_priv *priv = dev_get_priv(dev);
346
347 /* banner */
348 printf("GSCv%d : v%d 0x%04x ", priv->gscver, priv->fwver, priv->fwcrc);
349 gsc_reset_info(dev);
350 gsc_thermal_info(dev);
351 puts("\n");
352
353 /* Display RTC */
354 if (priv->rtc) {
355 u8 buf[4];
356 time_t timestamp;
357 struct rtc_time tm;
358
359 if (!gsc_i2c_read(priv->rtc, 0, buf, 4)) {
360 timestamp = get_unaligned_le32(buf);
361 rtc_to_tm(timestamp, &tm);
362 printf("RTC : %4d-%02d-%02d %2d:%02d:%02d UTC\n",
363 tm.tm_year, tm.tm_mon, tm.tm_mday,
364 tm.tm_hour, tm.tm_min, tm.tm_sec);
365 }
366 }
367
368 return 0;
369}
370
371static int gsc_probe(struct udevice *dev)
372{
373 struct gsc_priv *priv = dev_get_priv(dev);
374 u8 buf[32];
375 int ret;
376
377 ret = gsc_i2c_read(dev, 0, buf, sizeof(buf));
378 if (ret)
379 return ret;
380
381 /*
382 * GSC chip version:
383 * GSCv2 has 16 registers (which overlap)
384 * GSCv3 has 32 registers
385 */
386 priv->gscver = memcmp(buf, buf + 16, 16) ? 3 : 2;
387 priv->fwver = buf[GSC_SC_FWVER];
388 priv->fwcrc = buf[GSC_SC_FWCRC] | buf[GSC_SC_FWCRC + 1] << 8;
389 priv->hwmon = gsc_get_dev(GSC_BUSNO, GSC_HWMON_ADDR);
390 if (priv->hwmon)
391 dev_set_priv(priv->hwmon, priv);
392 priv->rtc = gsc_get_dev(GSC_BUSNO, GSC_RTC_ADDR);
393 if (priv->rtc)
394 dev_set_priv(priv->rtc, priv);
395
Simon Glass7ec24132024-09-29 19:49:48 -0600396#ifdef CONFIG_XPL_BUILD
Tim Harveyb8204602022-03-07 16:24:04 -0800397 gsc_banner(dev);
398#endif
399
400 return 0;
401};
402
403static const struct udevice_id gsc_ids[] = {
404 { .compatible = "gw,gsc", },
405 { }
406};
407
408U_BOOT_DRIVER(gsc) = {
409 .name = "gsc",
410 .id = UCLASS_MISC,
411 .of_match = gsc_ids,
412 .probe = gsc_probe,
413 .priv_auto = sizeof(struct gsc_priv),
414 .flags = DM_FLAG_PRE_RELOC,
415};
416
417static int gsc_sleep(struct udevice *dev, unsigned long secs)
418{
419 u8 regs[4];
420 int ret;
421
422 printf("GSC Sleeping for %ld seconds\n", secs);
423 put_unaligned_le32(secs, regs);
424 ret = gsc_i2c_write(dev, GSC_SC_TIME_ADD, regs, sizeof(regs));
425 if (ret)
426 goto err;
427 ret = gsc_i2c_read(dev, GSC_SC_CTRL1, regs, 1);
428 if (ret)
429 goto err;
430 regs[0] |= BIT(GSC_SC_CTRL1_SLEEP_ADD);
431 ret = gsc_i2c_write(dev, GSC_SC_CTRL1, regs, 1);
432 if (ret)
433 goto err;
434 regs[0] &= ~BIT(GSC_SC_CTRL1_SLEEP_ADD);
435 regs[0] |= BIT(GSC_SC_CTRL1_SLEEP_EN) | BIT(GSC_SC_CTRL1_SLEEP_ACTIVATE);
436 ret = gsc_i2c_write(dev, GSC_SC_CTRL1, regs, 1);
437 if (ret)
438 goto err;
439
440 return 0;
441
442err:
443 printf("i2c error: %d\n", ret);
444 return ret;
445}
446
447static int gsc_wd_disable(struct udevice *dev)
448{
449 int ret;
450 u8 reg;
451
452 ret = gsc_i2c_read(dev, GSC_SC_CTRL1, &reg, 1);
453 if (ret)
454 goto err;
455 reg |= BIT(GSC_SC_CTRL1_WDDIS);
456 reg &= ~BIT(GSC_SC_CTRL1_BOOT_CHK);
457 ret = gsc_i2c_write(dev, GSC_SC_CTRL1, &reg, 1);
458 if (ret)
459 goto err;
460 puts("GSC : boot watchdog disabled\n");
461
462 return 0;
463
464err:
465 puts("i2c error");
466 return ret;
467}
468
469static int gsc_thermal(struct udevice *dev, const char *cmd, const char *val)
470{
471 struct gsc_priv *priv = dev_get_priv(dev);
472 int ret, tmax;
473 bool enabled;
474 u8 reg;
475
476 if (priv->gscver < 3 || priv->fwver < 53)
477 return -EINVAL;
478 ret = gsc_thermal_get_info(dev, &reg, &tmax, &enabled);
479 if (ret)
480 return ret;
481 if (cmd && !strcmp(cmd, "enable")) {
482 if (val && *val) {
483 tmax = clamp((int)simple_strtoul(val, NULL, 0), 72, 122);
484 reg &= ~0xf8;
485 reg |= ((tmax - 70) / 2) << 3;
486 }
487 reg |= BIT(0);
488 gsc_i2c_write(dev, GSC_SC_THERM_PROTECT, &reg, 1);
489 } else if (cmd && !strcmp(cmd, "disable")) {
490 reg &= ~BIT(0);
491 gsc_i2c_write(dev, GSC_SC_THERM_PROTECT, &reg, 1);
492 } else if (cmd) {
493 return -EINVAL;
494 }
495
496 /* show status */
497 gsc_thermal_info(dev);
498 puts("\n");
499
500 return 0;
501}
502
503/* override in board files to display additional board EEPROM info */
504__weak void board_gsc_info(void)
505{
506}
507
508static void gsc_info(struct udevice *dev)
509{
510 gsc_banner(dev);
511 board_gsc_info();
512}
513
514static int do_gsc(struct cmd_tbl *cmdtp, int flag, int argc, char * const argv[])
515{
516 struct udevice *dev;
517 int ret;
518
519 /* get/probe driver */
520 ret = uclass_get_device_by_driver(UCLASS_MISC, DM_DRIVER_GET(gsc), &dev);
521 if (ret)
522 return CMD_RET_USAGE;
523 if (argc < 2) {
524 gsc_info(dev);
525 return CMD_RET_SUCCESS;
526 } else if (strcasecmp(argv[1], "sleep") == 0) {
527 if (argc < 3)
528 return CMD_RET_USAGE;
529 if (!gsc_sleep(dev, dectoul(argv[2], NULL)))
530 return CMD_RET_SUCCESS;
531 } else if (strcasecmp(argv[1], "hwmon") == 0) {
532 if (!gsc_hwmon(dev))
533 return CMD_RET_SUCCESS;
534 } else if (strcasecmp(argv[1], "wd-disable") == 0) {
535 if (!gsc_wd_disable(dev))
536 return CMD_RET_SUCCESS;
537 } else if (strcasecmp(argv[1], "thermal") == 0) {
Simon Glass793a98e2023-11-18 14:05:20 -0700538 const char *cmd, *val;
Tim Harveyb8204602022-03-07 16:24:04 -0800539
Simon Glass793a98e2023-11-18 14:05:20 -0700540 cmd = cmd_arg2(argc, argv);
541 val = cmd_arg3(argc, argv);
Tim Harveyb8204602022-03-07 16:24:04 -0800542 if (!gsc_thermal(dev, cmd, val))
543 return CMD_RET_SUCCESS;
544 }
545
546 return CMD_RET_USAGE;
547}
548
549U_BOOT_CMD(gsc, 4, 1, do_gsc, "Gateworks System Controller",
550 "[sleep <secs>]|[hwmon]|[wd-disable][thermal [disable|enable [temp]]]\n");
551
552/* disable boot watchdog - useful for an SPL that wants to use falcon mode */
553int gsc_boot_wd_disable(void)
554{
555 struct udevice *dev;
556 int ret;
557
558 /* get/probe driver */
559 ret = uclass_get_device_by_driver(UCLASS_MISC, DM_DRIVER_GET(gsc), &dev);
560 if (!ret)
561 ret = gsc_wd_disable(dev);
562
563 return ret;
564}
565
566# else
567
568/*
569 * GSCv2 will fail to ACK an I2C transaction if it is busy, which can occur
570 * during its 1HZ timer tick while reading ADC's. When this does occur,
571 * it will never be busy longer than 2 back-to-back transfers so retry 3 times.
572 */
573static int gsc_i2c_read(uint chip, uint addr, u8 *buf, int len)
574{
575 int retry = 3;
576 int n = 0;
577 int ret;
578
579 while (n++ < retry) {
580 ret = i2c_read(chip, addr, 1, buf, len);
581 if (!ret)
582 break;
583 if (ret != -EREMOTEIO)
584 break;
585printf("%s 0x%02x retry %d\n", __func__, addr, n);
586 mdelay(10);
587 }
588 return ret;
589}
590
591static int gsc_i2c_write(uint chip, uint addr, u8 *buf, int len)
592{
593 int retry = 3;
594 int n = 0;
595 int ret;
596
597 while (n++ < retry) {
598 ret = i2c_write(chip, addr, 1, buf, len);
599 if (!ret)
600 break;
601 if (ret != -EREMOTEIO)
602 break;
603printf("%s 0x%02x retry %d\n", __func__, addr, n);
604 mdelay(10);
605 }
606 return ret;
607}
608
609/* disable boot watchdog - useful for an SPL that wants to use falcon mode */
610int gsc_boot_wd_disable(void)
611{
612 u8 buf[32];
613 int ret;
614
615 i2c_set_bus_num(GSC_BUSNO);
616 ret = gsc_i2c_read(GSC_SC_ADDR, 0, buf, sizeof(buf));
617 if (!ret) {
618 buf[GSC_SC_CTRL1] |= BIT(GSC_SC_CTRL1_WDDIS);
619 ret = gsc_i2c_write(GSC_SC_ADDR, GSC_SC_CTRL1, &buf[GSC_SC_CTRL1], 1);
620 printf("GSCv%d: v%d 0x%04x ",
621 memcmp(buf, buf + 16, 16) ? 3 : 2,
622 buf[GSC_SC_FWVER],
623 buf[GSC_SC_FWCRC] | buf[GSC_SC_FWCRC + 1] << 8);
624 if (buf[GSC_SC_STATUS] & BIT(GSC_SC_IRQ_WATCHDOG)) {
625 puts("RST:WDT ");
626 buf[GSC_SC_STATUS] &= ~BIT(GSC_SC_IRQ_WATCHDOG);
627 gsc_i2c_write(GSC_SC_ADDR, GSC_SC_STATUS, &buf[GSC_SC_STATUS], 1);
628 } else {
629 puts("RST:VIN ");
630 }
631 puts("WDT:disabled\n");
632 }
633
634 return ret;
635}
636
637#endif