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