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