/* * Copyright (C) 2013 Gateworks Corporation * * Author: Tim Harvey * * SPDX-License-Identifier: GPL-2.0+ */ #include #include #include #include #include "gsc.h" /* * The Gateworks System Controller will fail to ACK a master transaction if * it is busy, which can occur during its 1HZ timer tick while reading ADC's. * When this does occur, it will never be busy long enough to fail more than * 2 back-to-back transfers. Thus we wrap i2c_read and i2c_write with * 3 retries. */ int gsc_i2c_read(uchar chip, uint addr, int alen, uchar *buf, int len) { int retry = 3; int n = 0; int ret; while (n++ < retry) { ret = i2c_read(chip, addr, alen, buf, len); if (!ret) break; debug("%s: 0x%02x 0x%02x retry%d: %d\n", __func__, chip, addr, n, ret); if (ret != -ENODEV) break; mdelay(10); } return ret; } int gsc_i2c_write(uchar chip, uint addr, int alen, uchar *buf, int len) { int retry = 3; int n = 0; int ret; while (n++ < retry) { ret = i2c_write(chip, addr, alen, buf, len); if (!ret) break; debug("%s: 0x%02x 0x%02x retry%d: %d\n", __func__, chip, addr, n, ret); if (ret != -ENODEV) break; mdelay(10); } mdelay(100); return ret; } static void read_hwmon(const char *name, uint reg, uint size) { unsigned char buf[3]; uint ui; printf("%-8s:", name); memset(buf, 0, sizeof(buf)); if (gsc_i2c_read(GSC_HWMON_ADDR, reg, 1, buf, size)) { puts("fRD\n"); } else { ui = buf[0] | (buf[1]<<8) | (buf[2]<<16); if (ui == 0xffffff) puts("invalid\n"); else printf("%d\n", ui); } } int gsc_info(int verbose) { const char *model = getenv("model"); unsigned char buf[16]; i2c_set_bus_num(0); if (gsc_i2c_read(GSC_SC_ADDR, 0, 1, buf, 16)) return CMD_RET_FAILURE; printf("GSC: v%d", buf[GSC_SC_FWVER]); printf(" 0x%04x", buf[GSC_SC_FWCRC] | buf[GSC_SC_FWCRC+1]<<8); printf(" WDT:%sabled", (buf[GSC_SC_CTRL1] & (1< 2) timeout = simple_strtoul(argv[2], NULL, 10); i2c_set_bus_num(0); if (gsc_i2c_read(GSC_SC_ADDR, GSC_SC_CTRL1, 1, ®, 1)) return CMD_RET_FAILURE; reg &= ~((1 << GSC_SC_CTRL1_WDEN) | (1 << GSC_SC_CTRL1_WDTIME)); if (timeout == 60) reg |= (1 << GSC_SC_CTRL1_WDTIME); else timeout = 30; reg |= (1 << GSC_SC_CTRL1_WDEN); if (gsc_i2c_write(GSC_SC_ADDR, GSC_SC_CTRL1, 1, ®, 1)) return CMD_RET_FAILURE; printf("GSC Watchdog enabled with timeout=%d seconds\n", timeout); } else if (strcasecmp(argv[1], "disable") == 0) { i2c_set_bus_num(0); if (gsc_i2c_read(GSC_SC_ADDR, GSC_SC_CTRL1, 1, ®, 1)) return CMD_RET_FAILURE; reg &= ~((1 << GSC_SC_CTRL1_WDEN) | (1 << GSC_SC_CTRL1_WDTIME)); if (gsc_i2c_write(GSC_SC_ADDR, GSC_SC_CTRL1, 1, ®, 1)) return CMD_RET_FAILURE; printf("GSC Watchdog disabled\n"); } else { return CMD_RET_USAGE; } return CMD_RET_SUCCESS; } static int do_gsc(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) { if (argc < 2) return gsc_info(1); if (strcasecmp(argv[1], "wd") == 0) return do_gsc_wd(cmdtp, flag, --argc, ++argv); return CMD_RET_USAGE; } U_BOOT_CMD( gsc, 4, 1, do_gsc, "GSC configuration", "[wd enable [30|60]]|[wd disable]\n" ); #endif /* CONFIG_CMD_GSC */