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