Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Tim Harvey | 552c358 | 2014-03-06 07:46:30 -0800 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2013 Gateworks Corporation |
| 4 | * |
| 5 | * Author: Tim Harvey <tharvey@gateworks.com> |
Tim Harvey | 552c358 | 2014-03-06 07:46:30 -0800 | [diff] [blame] | 6 | */ |
| 7 | |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 8 | #include <common.h> |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 9 | #include <command.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 10 | #include <log.h> |
Masahiro Yamada | 56a931c | 2016-09-21 11:28:55 +0900 | [diff] [blame] | 11 | #include <linux/errno.h> |
Tim Harvey | 552c358 | 2014-03-06 07:46:30 -0800 | [diff] [blame] | 12 | #include <common.h> |
| 13 | #include <i2c.h> |
| 14 | #include <linux/ctype.h> |
| 15 | |
Tim Harvey | d15181c | 2016-05-23 08:25:27 -0700 | [diff] [blame] | 16 | #include "ventana_eeprom.h" |
Tim Harvey | 552c358 | 2014-03-06 07:46:30 -0800 | [diff] [blame] | 17 | #include "gsc.h" |
| 18 | |
Tim Harvey | 552c358 | 2014-03-06 07:46:30 -0800 | [diff] [blame] | 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 | } |
Tim Harvey | 8d68c8e | 2014-08-07 22:35:44 -0700 | [diff] [blame] | 61 | mdelay(100); |
Tim Harvey | 552c358 | 2014-03-06 07:46:30 -0800 | [diff] [blame] | 62 | return ret; |
| 63 | } |
| 64 | |
Tim Harvey | bd80451 | 2015-04-08 12:54:50 -0700 | [diff] [blame] | 65 | static void read_hwmon(const char *name, uint reg, uint size) |
Tim Harvey | 552c358 | 2014-03-06 07:46:30 -0800 | [diff] [blame] | 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); |
Tim Harvey | a2d24c9 | 2019-02-04 13:10:50 -0800 | [diff] [blame] | 76 | if (size == 2 && ui > 0x8000) |
Tim Harvey | c37682f | 2016-05-24 11:03:51 -0700 | [diff] [blame] | 77 | ui -= 0xffff; |
Tim Harvey | 552c358 | 2014-03-06 07:46:30 -0800 | [diff] [blame] | 78 | if (ui == 0xffffff) |
Tim Harvey | bd80451 | 2015-04-08 12:54:50 -0700 | [diff] [blame] | 79 | puts("invalid\n"); |
Tim Harvey | 552c358 | 2014-03-06 07:46:30 -0800 | [diff] [blame] | 80 | else |
Tim Harvey | bd80451 | 2015-04-08 12:54:50 -0700 | [diff] [blame] | 81 | printf("%d\n", ui); |
Tim Harvey | 552c358 | 2014-03-06 07:46:30 -0800 | [diff] [blame] | 82 | } |
Tim Harvey | 552c358 | 2014-03-06 07:46:30 -0800 | [diff] [blame] | 83 | } |
| 84 | |
Tim Harvey | 92e3d84 | 2015-04-08 12:54:59 -0700 | [diff] [blame] | 85 | int gsc_info(int verbose) |
Tim Harvey | 552c358 | 2014-03-06 07:46:30 -0800 | [diff] [blame] | 86 | { |
Tim Harvey | 92e3d84 | 2015-04-08 12:54:59 -0700 | [diff] [blame] | 87 | unsigned char buf[16]; |
Tim Harvey | 552c358 | 2014-03-06 07:46:30 -0800 | [diff] [blame] | 88 | |
| 89 | i2c_set_bus_num(0); |
Tim Harvey | 92e3d84 | 2015-04-08 12:54:59 -0700 | [diff] [blame] | 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 | } |
Tim Harvey | 2453510 | 2016-05-24 11:03:52 -0700 | [diff] [blame] | 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 | } |
Tim Harvey | 92e3d84 | 2015-04-08 12:54:59 -0700 | [diff] [blame] | 109 | puts("\n"); |
| 110 | if (!verbose) |
| 111 | return CMD_RET_SUCCESS; |
| 112 | |
Tim Harvey | bd80451 | 2015-04-08 12:54:50 -0700 | [diff] [blame] | 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); |
Tim Harvey | 1ce5f7f | 2015-04-08 12:54:52 -0700 | [diff] [blame] | 117 | read_hwmon("VDD_ARM", GSC_HWMON_VDD_CORE, 3); |
| 118 | read_hwmon("VDD_SOC", GSC_HWMON_VDD_SOC, 3); |
Tim Harvey | bd80451 | 2015-04-08 12:54:50 -0700 | [diff] [blame] | 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); |
Tim Harvey | 892068c | 2016-05-24 11:03:58 -0700 | [diff] [blame] | 122 | if (strncasecmp((const char*) ventana_info.model, "GW553", 5)) |
| 123 | read_hwmon("VDD_2P5", GSC_HWMON_VDD_2P5, 3); |
Tim Harvey | bd80451 | 2015-04-08 12:54:50 -0700 | [diff] [blame] | 124 | read_hwmon("VDD_1P8", GSC_HWMON_VDD_1P8, 3); |
Tim Harvey | 1ce5f7f | 2015-04-08 12:54:52 -0700 | [diff] [blame] | 125 | read_hwmon("VDD_IO2", GSC_HWMON_VDD_IO2, 3); |
Tim Harvey | d15181c | 2016-05-23 08:25:27 -0700 | [diff] [blame] | 126 | switch (ventana_info.model[3]) { |
Tim Harvey | 552c358 | 2014-03-06 07:46:30 -0800 | [diff] [blame] | 127 | case '1': /* GW51xx */ |
Tim Harvey | 1ce5f7f | 2015-04-08 12:54:52 -0700 | [diff] [blame] | 128 | read_hwmon("VDD_IO3", GSC_HWMON_VDD_IO4, 3); /* -C rev */ |
Tim Harvey | 552c358 | 2014-03-06 07:46:30 -0800 | [diff] [blame] | 129 | break; |
| 130 | case '2': /* GW52xx */ |
Tim Harvey | 1ce5f7f | 2015-04-08 12:54:52 -0700 | [diff] [blame] | 131 | break; |
Tim Harvey | 552c358 | 2014-03-06 07:46:30 -0800 | [diff] [blame] | 132 | case '3': /* GW53xx */ |
Tim Harvey | 1ce5f7f | 2015-04-08 12:54:52 -0700 | [diff] [blame] | 133 | read_hwmon("VDD_IO4", GSC_HWMON_VDD_IO4, 3); /* -C rev */ |
| 134 | read_hwmon("VDD_GPS", GSC_HWMON_VDD_IO3, 3); |
Tim Harvey | 552c358 | 2014-03-06 07:46:30 -0800 | [diff] [blame] | 135 | break; |
| 136 | case '4': /* GW54xx */ |
Tim Harvey | 1ce5f7f | 2015-04-08 12:54:52 -0700 | [diff] [blame] | 137 | read_hwmon("VDD_IO3", GSC_HWMON_VDD_IO4, 3); /* -C rev */ |
| 138 | read_hwmon("VDD_GPS", GSC_HWMON_VDD_IO3, 3); |
Tim Harvey | 552c358 | 2014-03-06 07:46:30 -0800 | [diff] [blame] | 139 | break; |
Tim Harvey | 5058183 | 2014-08-20 23:35:14 -0700 | [diff] [blame] | 140 | case '5': /* GW55xx */ |
Tim Harvey | 5058183 | 2014-08-20 23:35:14 -0700 | [diff] [blame] | 141 | break; |
Tim Harvey | 659441b | 2017-03-17 07:31:02 -0700 | [diff] [blame] | 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; |
Tim Harvey | a2d24c9 | 2019-02-04 13:10:50 -0800 | [diff] [blame] | 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); |
Tim Harvey | 552c358 | 2014-03-06 07:46:30 -0800 | [diff] [blame] | 150 | } |
| 151 | return 0; |
| 152 | } |
| 153 | |
Tim Harvey | 40feabb | 2015-05-08 18:28:36 -0700 | [diff] [blame] | 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, ®, 1)) { |
| 173 | reg |= (1 << GSC_SC_CTRL1_WDDIS); |
| 174 | if (!gsc_i2c_write(GSC_SC_ADDR, GSC_SC_CTRL1, 1, ®, 1)) |
| 175 | return 0; |
| 176 | } |
| 177 | puts("Error: could not disable GSC Watchdog\n"); |
| 178 | return 1; |
| 179 | } |
| 180 | |
Tom Rini | b6f576a | 2018-01-03 09:16:29 -0500 | [diff] [blame] | 181 | #if defined(CONFIG_CMD_GSC) && !defined(CONFIG_SPL_BUILD) |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 182 | static int do_gsc_sleep(struct cmd_tbl *cmdtp, int flag, int argc, |
| 183 | char *const argv[]) |
Tim Harvey | 30ea7ee | 2016-05-24 11:03:50 -0700 | [diff] [blame] | 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, ®, 1)) |
| 197 | goto error; |
| 198 | reg = (secs >> 16) & 0xff; |
| 199 | if (gsc_i2c_write(GSC_SC_ADDR, 8, 1, ®, 1)) |
| 200 | goto error; |
| 201 | reg = (secs >> 8) & 0xff; |
| 202 | if (gsc_i2c_write(GSC_SC_ADDR, 7, 1, ®, 1)) |
| 203 | goto error; |
| 204 | reg = secs & 0xff; |
| 205 | if (gsc_i2c_write(GSC_SC_ADDR, 6, 1, ®, 1)) |
| 206 | goto error; |
| 207 | if (gsc_i2c_read(GSC_SC_ADDR, GSC_SC_CTRL1, 1, ®, 1)) |
| 208 | goto error; |
| 209 | reg |= (1 << 2); |
| 210 | if (gsc_i2c_write(GSC_SC_ADDR, GSC_SC_CTRL1, 1, ®, 1)) |
| 211 | goto error; |
| 212 | reg &= ~(1 << 2); |
| 213 | reg |= 0x3; |
| 214 | if (gsc_i2c_write(GSC_SC_ADDR, GSC_SC_CTRL1, 1, ®, 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 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 224 | static int do_gsc_wd(struct cmd_tbl *cmdtp, int flag, int argc, |
| 225 | char *const argv[]) |
Tim Harvey | 92e3d84 | 2015-04-08 12:54:59 -0700 | [diff] [blame] | 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, ®, 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, ®, 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, ®, 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, ®, 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 Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 264 | static int do_gsc(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) |
Tim Harvey | 92e3d84 | 2015-04-08 12:54:59 -0700 | [diff] [blame] | 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); |
Tim Harvey | 30ea7ee | 2016-05-24 11:03:50 -0700 | [diff] [blame] | 271 | else if (strcasecmp(argv[1], "sleep") == 0) |
| 272 | return do_gsc_sleep(cmdtp, flag, --argc, ++argv); |
Tim Harvey | 92e3d84 | 2015-04-08 12:54:59 -0700 | [diff] [blame] | 273 | |
| 274 | return CMD_RET_USAGE; |
| 275 | } |
| 276 | |
| 277 | U_BOOT_CMD( |
| 278 | gsc, 4, 1, do_gsc, "GSC configuration", |
Tim Harvey | 30ea7ee | 2016-05-24 11:03:50 -0700 | [diff] [blame] | 279 | "[wd enable [30|60]]|[wd disable]|[sleep <secs>]\n" |
Tim Harvey | 92e3d84 | 2015-04-08 12:54:59 -0700 | [diff] [blame] | 280 | ); |
Tim Harvey | 552c358 | 2014-03-06 07:46:30 -0800 | [diff] [blame] | 281 | |
| 282 | #endif /* CONFIG_CMD_GSC */ |