1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2014 Gateworks Corporation
4 * Author: Tim Harvey <tharvey@gateworks.com>
13 #include <asm/bitops.h>
16 #include "ventana_eeprom.h"
18 /* read ventana EEPROM, check for validity, and return baseboard type */
20 read_eeprom(int bus, struct ventana_board_info *info)
26 unsigned char *buf = (unsigned char *)info;
28 memset(info, 0, sizeof(*info));
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.
36 if (0 == i2c_set_bus_num(bus) &&
37 0 == i2c_probe(GSC_EEPROM_ADDR))
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");
49 if (info->model[0] != 'G' || info->model[1] != 'W') {
50 puts("EEPROM: Invalid Model in EEPROM\n");
51 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, buf,
56 /* validate checksum */
57 for (chksum = 0, i = 0; i < sizeof(*info)-2; i++)
59 if ((info->chksum[0] != chksum>>8) ||
60 (info->chksum[1] != (chksum&0xff))) {
61 puts("EEPROM: Failed EEPROM checksum\n");
62 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, buf,
67 /* original GW5400-A prototype */
68 baseboard = info->model[3];
69 if (strncasecmp((const char *)info->model, "GW5400-A", 8) == 0)
74 case '0': /* original GW5400-A prototype */
90 if (info->model[4] == '1') {
93 } else if (info->model[4] == '2') {
96 } else if (info->model[4] == '3') {
102 if (info->model[4] == '0')
106 if (info->model[4] == '0' && info->model[5] == '1')
108 else if (info->model[4] == '0' && info->model[5] == '2')
110 else if (info->model[4] == '0' && info->model[5] == '3')
112 else if (info->model[4] == '0' && info->model[5] == '4')
114 else if (info->model[4] == '0' && info->model[5] == '5')
116 else if (info->model[4] == '0' && info->model[5] == '6')
118 else if (info->model[4] == '0' && info->model[5] == '7')
120 else if (info->model[4] == '0' && info->model[5] == '8')
122 else if (info->model[4] == '0' && info->model[5] == '9')
126 printf("EEPROM: Unknown model in EEPROM: %s\n", info->model);
127 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, buf,
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 },
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 },
146 #if defined(CONFIG_CMD_EECONFIG) && !defined(CONFIG_SPL_BUILD)
147 static struct ventana_eeprom_config *get_config(const char *name)
149 struct ventana_eeprom_config *cfg = econfig;
152 if (0 == strcmp(name, cfg->name))
159 static u8 econfig_bytes[sizeof(ventana_info.config)];
160 static int econfig_init = -1;
162 static int do_econfig(struct cmd_tbl *cmdtp, int flag, int argc,
165 struct ventana_eeprom_config *cfg;
166 struct ventana_board_info *info = &ventana_info;
170 return CMD_RET_USAGE;
173 if (econfig_init != 1) {
174 memcpy(econfig_bytes, info->config, sizeof(econfig_bytes));
179 if ((strncmp(argv[1], "list", 4) == 0)) {
182 printf("%s: %d\n", cfg->name,
183 test_bit(cfg->bit, econfig_bytes) ? 1 : 0);
189 else if ((strncmp(argv[1], "save", 4) == 0)) {
190 unsigned char *buf = (unsigned char *)info;
193 /* calculate new checksum */
194 memcpy(info->config, econfig_bytes, sizeof(econfig_bytes));
195 for (chksum = 0, i = 0; i < sizeof(*info)-2; 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;
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;
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;
217 printf("Config saved to EEPROM\n");
221 else if (argc == 2) {
222 cfg = get_config(argv[1]);
224 printf("%s: %d\n", cfg->name,
225 test_bit(cfg->bit, econfig_bytes) ? 1 : 0);
227 printf("invalid config: %s\n", argv[1]);
228 return CMD_RET_FAILURE;
233 else if (argc == 3) {
234 cfg = get_config(argv[1]);
236 if (simple_strtol(argv[2], NULL, 10)) {
237 test_and_set_bit(cfg->bit, econfig_bytes);
238 printf("Enabled %s\n", cfg->name);
240 test_and_clear_bit(cfg->bit, econfig_bytes);
241 printf("Disabled %s\n", cfg->name);
244 printf("invalid config: %s\n", argv[1]);
245 return CMD_RET_FAILURE;
250 return CMD_RET_USAGE;
252 return CMD_RET_SUCCESS;
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"
264 #endif /* CONFIG_CMD_EECONFIG */