Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Tim Harvey | 0dccde7 | 2014-06-02 16:13:25 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2014 Gateworks Corporation |
| 4 | * Author: Tim Harvey <tharvey@gateworks.com> |
Tim Harvey | 0dccde7 | 2014-06-02 16:13:25 -0700 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <common.h> |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 8 | #include <command.h> |
Tim Harvey | 0da2c52 | 2014-08-07 22:35:45 -0700 | [diff] [blame] | 9 | #include <errno.h> |
Tim Harvey | fc93d3b | 2019-02-21 08:48:48 -0800 | [diff] [blame] | 10 | #include <hexdump.h> |
Tim Harvey | 0dccde7 | 2014-06-02 16:13:25 -0700 | [diff] [blame] | 11 | #include <i2c.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 12 | #include <log.h> |
Tim Harvey | 0da2c52 | 2014-08-07 22:35:45 -0700 | [diff] [blame] | 13 | #include <malloc.h> |
| 14 | #include <asm/bitops.h> |
Simon Glass | dbd7954 | 2020-05-10 11:40:11 -0600 | [diff] [blame] | 15 | #include <linux/delay.h> |
Tim Harvey | 0dccde7 | 2014-06-02 16:13:25 -0700 | [diff] [blame] | 16 | |
| 17 | #include "gsc.h" |
| 18 | #include "ventana_eeprom.h" |
| 19 | |
| 20 | /* read ventana EEPROM, check for validity, and return baseboard type */ |
| 21 | int |
| 22 | read_eeprom(int bus, struct ventana_board_info *info) |
| 23 | { |
| 24 | int i; |
| 25 | int chksum; |
| 26 | char baseboard; |
| 27 | int type; |
| 28 | unsigned char *buf = (unsigned char *)info; |
| 29 | |
| 30 | memset(info, 0, sizeof(*info)); |
| 31 | |
| 32 | /* |
| 33 | * On a board with a missing/depleted backup battery for GSC, the |
| 34 | * board may be ready to probe the GSC before its firmware is |
| 35 | * running. We will wait here indefinately for the GSC/EEPROM. |
| 36 | */ |
| 37 | while (1) { |
| 38 | if (0 == i2c_set_bus_num(bus) && |
| 39 | 0 == i2c_probe(GSC_EEPROM_ADDR)) |
| 40 | break; |
| 41 | mdelay(1); |
| 42 | } |
| 43 | |
| 44 | /* read eeprom config section */ |
Tim Harvey | df2613a | 2021-04-16 15:05:59 -0700 | [diff] [blame] | 45 | mdelay(10); |
Tim Harvey | 0dccde7 | 2014-06-02 16:13:25 -0700 | [diff] [blame] | 46 | if (gsc_i2c_read(GSC_EEPROM_ADDR, 0x00, 1, buf, sizeof(*info))) { |
| 47 | puts("EEPROM: Failed to read EEPROM\n"); |
Tim Harvey | 0dccde7 | 2014-06-02 16:13:25 -0700 | [diff] [blame] | 48 | return GW_UNKNOWN; |
| 49 | } |
| 50 | |
| 51 | /* sanity checks */ |
| 52 | if (info->model[0] != 'G' || info->model[1] != 'W') { |
| 53 | puts("EEPROM: Invalid Model in EEPROM\n"); |
Tim Harvey | fc93d3b | 2019-02-21 08:48:48 -0800 | [diff] [blame] | 54 | print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, buf, |
| 55 | sizeof(*info)); |
Tim Harvey | 0dccde7 | 2014-06-02 16:13:25 -0700 | [diff] [blame] | 56 | return GW_UNKNOWN; |
| 57 | } |
| 58 | |
| 59 | /* validate checksum */ |
| 60 | for (chksum = 0, i = 0; i < sizeof(*info)-2; i++) |
| 61 | chksum += buf[i]; |
| 62 | if ((info->chksum[0] != chksum>>8) || |
| 63 | (info->chksum[1] != (chksum&0xff))) { |
| 64 | puts("EEPROM: Failed EEPROM checksum\n"); |
Tim Harvey | fc93d3b | 2019-02-21 08:48:48 -0800 | [diff] [blame] | 65 | print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, buf, |
| 66 | sizeof(*info)); |
Tim Harvey | 0dccde7 | 2014-06-02 16:13:25 -0700 | [diff] [blame] | 67 | return GW_UNKNOWN; |
| 68 | } |
| 69 | |
| 70 | /* original GW5400-A prototype */ |
| 71 | baseboard = info->model[3]; |
| 72 | if (strncasecmp((const char *)info->model, "GW5400-A", 8) == 0) |
| 73 | baseboard = '0'; |
| 74 | |
Tim Harvey | 6353779 | 2017-03-17 07:30:38 -0700 | [diff] [blame] | 75 | type = GW_UNKNOWN; |
Tim Harvey | 0dccde7 | 2014-06-02 16:13:25 -0700 | [diff] [blame] | 76 | switch (baseboard) { |
| 77 | case '0': /* original GW5400-A prototype */ |
| 78 | type = GW54proto; |
| 79 | break; |
| 80 | case '1': |
| 81 | type = GW51xx; |
| 82 | break; |
| 83 | case '2': |
| 84 | type = GW52xx; |
| 85 | break; |
| 86 | case '3': |
| 87 | type = GW53xx; |
| 88 | break; |
| 89 | case '4': |
| 90 | type = GW54xx; |
| 91 | break; |
Tim Harvey | 5058183 | 2014-08-20 23:35:14 -0700 | [diff] [blame] | 92 | case '5': |
Tim Harvey | b6de3b2 | 2015-04-08 12:54:45 -0700 | [diff] [blame] | 93 | if (info->model[4] == '1') { |
| 94 | type = GW551x; |
| 95 | break; |
| 96 | } else if (info->model[4] == '2') { |
| 97 | type = GW552x; |
| 98 | break; |
Tim Harvey | 892068c | 2016-05-24 11:03:58 -0700 | [diff] [blame] | 99 | } else if (info->model[4] == '3') { |
| 100 | type = GW553x; |
| 101 | break; |
Tim Harvey | b6de3b2 | 2015-04-08 12:54:45 -0700 | [diff] [blame] | 102 | } |
Tim Harvey | 6353779 | 2017-03-17 07:30:38 -0700 | [diff] [blame] | 103 | break; |
Tim Harvey | 659441b | 2017-03-17 07:31:02 -0700 | [diff] [blame] | 104 | case '6': |
| 105 | if (info->model[4] == '0') |
| 106 | type = GW560x; |
| 107 | break; |
Tim Harvey | 6353779 | 2017-03-17 07:30:38 -0700 | [diff] [blame] | 108 | case '9': |
Tim Harvey | 5852a33 | 2019-02-04 13:10:58 -0800 | [diff] [blame] | 109 | if (info->model[4] == '0' && info->model[5] == '1') |
| 110 | type = GW5901; |
| 111 | else if (info->model[4] == '0' && info->model[5] == '2') |
| 112 | type = GW5902; |
| 113 | else if (info->model[4] == '0' && info->model[5] == '3') |
Tim Harvey | 4533c90 | 2017-03-17 07:32:21 -0700 | [diff] [blame] | 114 | type = GW5903; |
Tim Harvey | a2d24c9 | 2019-02-04 13:10:50 -0800 | [diff] [blame] | 115 | else if (info->model[4] == '0' && info->model[5] == '4') |
Tim Harvey | 6353779 | 2017-03-17 07:30:38 -0700 | [diff] [blame] | 116 | type = GW5904; |
Tim Harvey | a2d24c9 | 2019-02-04 13:10:50 -0800 | [diff] [blame] | 117 | else if (info->model[4] == '0' && info->model[5] == '5') |
| 118 | type = GW5905; |
Tim Harvey | b7c48a9 | 2019-02-04 13:10:54 -0800 | [diff] [blame] | 119 | else if (info->model[4] == '0' && info->model[5] == '6') |
| 120 | type = GW5906; |
Tim Harvey | 83cad80 | 2019-02-04 13:10:55 -0800 | [diff] [blame] | 121 | else if (info->model[4] == '0' && info->model[5] == '7') |
| 122 | type = GW5907; |
Tim Harvey | c262540 | 2019-02-04 13:10:56 -0800 | [diff] [blame] | 123 | else if (info->model[4] == '0' && info->model[5] == '8') |
| 124 | type = GW5908; |
Tim Harvey | 2df5046 | 2019-02-04 13:10:57 -0800 | [diff] [blame] | 125 | else if (info->model[4] == '0' && info->model[5] == '9') |
| 126 | type = GW5909; |
Tim Harvey | 08aec66 | 2021-07-24 10:40:42 -0700 | [diff] [blame] | 127 | else if (info->model[4] == '1' && info->model[5] == '0') |
| 128 | type = GW5910; |
Tim Harvey | d67ad6e | 2021-07-24 10:40:43 -0700 | [diff] [blame] | 129 | else if (info->model[4] == '1' && info->model[5] == '2') |
| 130 | type = GW5912; |
Tim Harvey | b7c9f36 | 2021-07-24 10:40:44 -0700 | [diff] [blame] | 131 | else if (info->model[4] == '1' && info->model[5] == '3') |
| 132 | type = GW5913; |
Tim Harvey | 0dccde7 | 2014-06-02 16:13:25 -0700 | [diff] [blame] | 133 | break; |
Tim Harvey | fc93d3b | 2019-02-21 08:48:48 -0800 | [diff] [blame] | 134 | default: |
| 135 | printf("EEPROM: Unknown model in EEPROM: %s\n", info->model); |
| 136 | print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, buf, |
| 137 | sizeof(*info)); |
| 138 | break; |
Tim Harvey | 0dccde7 | 2014-06-02 16:13:25 -0700 | [diff] [blame] | 139 | } |
| 140 | return type; |
| 141 | } |
Tim Harvey | 0da2c52 | 2014-08-07 22:35:45 -0700 | [diff] [blame] | 142 | |
| 143 | /* list of config bits that the bootloader will remove from dtb if not set */ |
| 144 | struct ventana_eeprom_config econfig[] = { |
| 145 | { "eth0", "ethernet0", EECONFIG_ETH0 }, |
Tim Harvey | 0da2c52 | 2014-08-07 22:35:45 -0700 | [diff] [blame] | 146 | { "usb0", NULL, EECONFIG_USB0 }, |
| 147 | { "usb1", NULL, EECONFIG_USB1 }, |
| 148 | { "mmc0", NULL, EECONFIG_SD0 }, |
| 149 | { "mmc1", NULL, EECONFIG_SD1 }, |
| 150 | { "mmc2", NULL, EECONFIG_SD2 }, |
| 151 | { "mmc3", NULL, EECONFIG_SD3 }, |
Tim Harvey | 0da2c52 | 2014-08-07 22:35:45 -0700 | [diff] [blame] | 152 | { /* Sentinel */ } |
| 153 | }; |
| 154 | |
Tom Rini | 3ac6875 | 2018-01-03 09:15:22 -0500 | [diff] [blame] | 155 | #if defined(CONFIG_CMD_EECONFIG) && !defined(CONFIG_SPL_BUILD) |
Tim Harvey | 0da2c52 | 2014-08-07 22:35:45 -0700 | [diff] [blame] | 156 | static struct ventana_eeprom_config *get_config(const char *name) |
| 157 | { |
| 158 | struct ventana_eeprom_config *cfg = econfig; |
| 159 | |
| 160 | while (cfg->name) { |
| 161 | if (0 == strcmp(name, cfg->name)) |
| 162 | return cfg; |
| 163 | cfg++; |
| 164 | } |
| 165 | return NULL; |
| 166 | } |
| 167 | |
| 168 | static u8 econfig_bytes[sizeof(ventana_info.config)]; |
| 169 | static int econfig_init = -1; |
| 170 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 171 | static int do_econfig(struct cmd_tbl *cmdtp, int flag, int argc, |
| 172 | char *const argv[]) |
Tim Harvey | 0da2c52 | 2014-08-07 22:35:45 -0700 | [diff] [blame] | 173 | { |
| 174 | struct ventana_eeprom_config *cfg; |
| 175 | struct ventana_board_info *info = &ventana_info; |
| 176 | int i; |
| 177 | |
| 178 | if (argc < 2) |
| 179 | return CMD_RET_USAGE; |
| 180 | |
| 181 | /* initialize */ |
| 182 | if (econfig_init != 1) { |
| 183 | memcpy(econfig_bytes, info->config, sizeof(econfig_bytes)); |
| 184 | econfig_init = 1; |
| 185 | } |
| 186 | |
| 187 | /* list configs */ |
| 188 | if ((strncmp(argv[1], "list", 4) == 0)) { |
| 189 | cfg = econfig; |
| 190 | while (cfg->name) { |
| 191 | printf("%s: %d\n", cfg->name, |
| 192 | test_bit(cfg->bit, econfig_bytes) ? 1 : 0); |
| 193 | cfg++; |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | /* save */ |
| 198 | else if ((strncmp(argv[1], "save", 4) == 0)) { |
| 199 | unsigned char *buf = (unsigned char *)info; |
| 200 | int chksum; |
| 201 | |
| 202 | /* calculate new checksum */ |
| 203 | memcpy(info->config, econfig_bytes, sizeof(econfig_bytes)); |
| 204 | for (chksum = 0, i = 0; i < sizeof(*info)-2; i++) |
| 205 | chksum += buf[i]; |
| 206 | debug("old chksum:0x%04x\n", |
| 207 | (info->chksum[0] << 8) | info->chksum[1]); |
| 208 | debug("new chksum:0x%04x\n", chksum); |
| 209 | info->chksum[0] = chksum >> 8; |
| 210 | info->chksum[1] = chksum & 0xff; |
| 211 | |
| 212 | /* write new config data */ |
| 213 | if (gsc_i2c_write(GSC_EEPROM_ADDR, info->config - (u8 *)info, |
| 214 | 1, econfig_bytes, sizeof(econfig_bytes))) { |
| 215 | printf("EEPROM: Failed updating config\n"); |
| 216 | return CMD_RET_FAILURE; |
| 217 | } |
| 218 | |
| 219 | /* write new config data */ |
| 220 | if (gsc_i2c_write(GSC_EEPROM_ADDR, info->chksum - (u8 *)info, |
| 221 | 1, info->chksum, 2)) { |
| 222 | printf("EEPROM: Failed updating checksum\n"); |
| 223 | return CMD_RET_FAILURE; |
| 224 | } |
| 225 | |
| 226 | printf("Config saved to EEPROM\n"); |
| 227 | } |
| 228 | |
| 229 | /* get config */ |
| 230 | else if (argc == 2) { |
| 231 | cfg = get_config(argv[1]); |
| 232 | if (cfg) { |
| 233 | printf("%s: %d\n", cfg->name, |
| 234 | test_bit(cfg->bit, econfig_bytes) ? 1 : 0); |
| 235 | } else { |
| 236 | printf("invalid config: %s\n", argv[1]); |
| 237 | return CMD_RET_FAILURE; |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | /* set config */ |
| 242 | else if (argc == 3) { |
| 243 | cfg = get_config(argv[1]); |
| 244 | if (cfg) { |
| 245 | if (simple_strtol(argv[2], NULL, 10)) { |
| 246 | test_and_set_bit(cfg->bit, econfig_bytes); |
| 247 | printf("Enabled %s\n", cfg->name); |
| 248 | } else { |
| 249 | test_and_clear_bit(cfg->bit, econfig_bytes); |
| 250 | printf("Disabled %s\n", cfg->name); |
| 251 | } |
| 252 | } else { |
| 253 | printf("invalid config: %s\n", argv[1]); |
| 254 | return CMD_RET_FAILURE; |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | else |
| 259 | return CMD_RET_USAGE; |
| 260 | |
| 261 | return CMD_RET_SUCCESS; |
| 262 | } |
| 263 | |
| 264 | U_BOOT_CMD( |
| 265 | econfig, 3, 0, do_econfig, |
| 266 | "EEPROM configuration", |
| 267 | "list - list config\n" |
| 268 | "save - save config to EEPROM\n" |
| 269 | "<name> - get config 'name'\n" |
| 270 | "<name> [0|1] - set config 'name' to value\n" |
| 271 | ); |
| 272 | |
| 273 | #endif /* CONFIG_CMD_EECONFIG */ |