1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2014 Gateworks Corporation
4 * Author: Tim Harvey <tharvey@gateworks.com>
14 #include <asm/bitops.h>
15 #include <linux/delay.h>
18 #include "ventana_eeprom.h"
20 /* read ventana EEPROM, check for validity, and return baseboard type */
22 read_eeprom(int bus, struct ventana_board_info *info)
28 unsigned char *buf = (unsigned char *)info;
30 memset(info, 0, sizeof(*info));
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.
38 if (0 == i2c_set_bus_num(bus) &&
39 0 == i2c_probe(GSC_EEPROM_ADDR))
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");
51 if (info->model[0] != 'G' || info->model[1] != 'W') {
52 puts("EEPROM: Invalid Model in EEPROM\n");
53 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, buf,
58 /* validate checksum */
59 for (chksum = 0, i = 0; i < sizeof(*info)-2; i++)
61 if ((info->chksum[0] != chksum>>8) ||
62 (info->chksum[1] != (chksum&0xff))) {
63 puts("EEPROM: Failed EEPROM checksum\n");
64 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, buf,
69 /* original GW5400-A prototype */
70 baseboard = info->model[3];
71 if (strncasecmp((const char *)info->model, "GW5400-A", 8) == 0)
76 case '0': /* original GW5400-A prototype */
92 if (info->model[4] == '1') {
95 } else if (info->model[4] == '2') {
98 } else if (info->model[4] == '3') {
104 if (info->model[4] == '0')
108 if (info->model[4] == '0' && info->model[5] == '1')
110 else if (info->model[4] == '0' && info->model[5] == '2')
112 else if (info->model[4] == '0' && info->model[5] == '3')
114 else if (info->model[4] == '0' && info->model[5] == '4')
116 else if (info->model[4] == '0' && info->model[5] == '5')
118 else if (info->model[4] == '0' && info->model[5] == '6')
120 else if (info->model[4] == '0' && info->model[5] == '7')
122 else if (info->model[4] == '0' && info->model[5] == '8')
124 else if (info->model[4] == '0' && info->model[5] == '9')
128 printf("EEPROM: Unknown model in EEPROM: %s\n", info->model);
129 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, buf,
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 },
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 },
148 #if defined(CONFIG_CMD_EECONFIG) && !defined(CONFIG_SPL_BUILD)
149 static struct ventana_eeprom_config *get_config(const char *name)
151 struct ventana_eeprom_config *cfg = econfig;
154 if (0 == strcmp(name, cfg->name))
161 static u8 econfig_bytes[sizeof(ventana_info.config)];
162 static int econfig_init = -1;
164 static int do_econfig(struct cmd_tbl *cmdtp, int flag, int argc,
167 struct ventana_eeprom_config *cfg;
168 struct ventana_board_info *info = &ventana_info;
172 return CMD_RET_USAGE;
175 if (econfig_init != 1) {
176 memcpy(econfig_bytes, info->config, sizeof(econfig_bytes));
181 if ((strncmp(argv[1], "list", 4) == 0)) {
184 printf("%s: %d\n", cfg->name,
185 test_bit(cfg->bit, econfig_bytes) ? 1 : 0);
191 else if ((strncmp(argv[1], "save", 4) == 0)) {
192 unsigned char *buf = (unsigned char *)info;
195 /* calculate new checksum */
196 memcpy(info->config, econfig_bytes, sizeof(econfig_bytes));
197 for (chksum = 0, i = 0; i < sizeof(*info)-2; 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;
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;
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;
219 printf("Config saved to EEPROM\n");
223 else if (argc == 2) {
224 cfg = get_config(argv[1]);
226 printf("%s: %d\n", cfg->name,
227 test_bit(cfg->bit, econfig_bytes) ? 1 : 0);
229 printf("invalid config: %s\n", argv[1]);
230 return CMD_RET_FAILURE;
235 else if (argc == 3) {
236 cfg = get_config(argv[1]);
238 if (simple_strtol(argv[2], NULL, 10)) {
239 test_and_set_bit(cfg->bit, econfig_bytes);
240 printf("Enabled %s\n", cfg->name);
242 test_and_clear_bit(cfg->bit, econfig_bytes);
243 printf("Disabled %s\n", cfg->name);
246 printf("invalid config: %s\n", argv[1]);
247 return CMD_RET_FAILURE;
252 return CMD_RET_USAGE;
254 return CMD_RET_SUCCESS;
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"
266 #endif /* CONFIG_CMD_EECONFIG */