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