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>
17 #include "ventana_eeprom.h"
19 /* read ventana EEPROM, check for validity, and return baseboard type */
21 read_eeprom(int bus, struct ventana_board_info *info)
27 unsigned char *buf = (unsigned char *)info;
29 memset(info, 0, sizeof(*info));
32 * On a board with a missing/depleted backup battery for GSC, the
33 * board may be ready to probe the GSC before its firmware is
34 * running. We will wait here indefinately for the GSC/EEPROM.
37 if (0 == i2c_set_bus_num(bus) &&
38 0 == i2c_probe(GSC_EEPROM_ADDR))
43 /* read eeprom config section */
44 if (gsc_i2c_read(GSC_EEPROM_ADDR, 0x00, 1, buf, sizeof(*info))) {
45 puts("EEPROM: Failed to read EEPROM\n");
50 if (info->model[0] != 'G' || info->model[1] != 'W') {
51 puts("EEPROM: Invalid Model in EEPROM\n");
52 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, buf,
57 /* validate checksum */
58 for (chksum = 0, i = 0; i < sizeof(*info)-2; i++)
60 if ((info->chksum[0] != chksum>>8) ||
61 (info->chksum[1] != (chksum&0xff))) {
62 puts("EEPROM: Failed EEPROM checksum\n");
63 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, buf,
68 /* original GW5400-A prototype */
69 baseboard = info->model[3];
70 if (strncasecmp((const char *)info->model, "GW5400-A", 8) == 0)
75 case '0': /* original GW5400-A prototype */
91 if (info->model[4] == '1') {
94 } else if (info->model[4] == '2') {
97 } else if (info->model[4] == '3') {
103 if (info->model[4] == '0')
107 if (info->model[4] == '0' && info->model[5] == '1')
109 else if (info->model[4] == '0' && info->model[5] == '2')
111 else if (info->model[4] == '0' && info->model[5] == '3')
113 else if (info->model[4] == '0' && info->model[5] == '4')
115 else if (info->model[4] == '0' && info->model[5] == '5')
117 else if (info->model[4] == '0' && info->model[5] == '6')
119 else if (info->model[4] == '0' && info->model[5] == '7')
121 else if (info->model[4] == '0' && info->model[5] == '8')
123 else if (info->model[4] == '0' && info->model[5] == '9')
127 printf("EEPROM: Unknown model in EEPROM: %s\n", info->model);
128 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, buf,
135 /* list of config bits that the bootloader will remove from dtb if not set */
136 struct ventana_eeprom_config econfig[] = {
137 { "eth0", "ethernet0", EECONFIG_ETH0 },
138 { "usb0", NULL, EECONFIG_USB0 },
139 { "usb1", NULL, EECONFIG_USB1 },
140 { "mmc0", NULL, EECONFIG_SD0 },
141 { "mmc1", NULL, EECONFIG_SD1 },
142 { "mmc2", NULL, EECONFIG_SD2 },
143 { "mmc3", NULL, EECONFIG_SD3 },
147 #if defined(CONFIG_CMD_EECONFIG) && !defined(CONFIG_SPL_BUILD)
148 static struct ventana_eeprom_config *get_config(const char *name)
150 struct ventana_eeprom_config *cfg = econfig;
153 if (0 == strcmp(name, cfg->name))
160 static u8 econfig_bytes[sizeof(ventana_info.config)];
161 static int econfig_init = -1;
163 static int do_econfig(struct cmd_tbl *cmdtp, int flag, int argc,
166 struct ventana_eeprom_config *cfg;
167 struct ventana_board_info *info = &ventana_info;
171 return CMD_RET_USAGE;
174 if (econfig_init != 1) {
175 memcpy(econfig_bytes, info->config, sizeof(econfig_bytes));
180 if ((strncmp(argv[1], "list", 4) == 0)) {
183 printf("%s: %d\n", cfg->name,
184 test_bit(cfg->bit, econfig_bytes) ? 1 : 0);
190 else if ((strncmp(argv[1], "save", 4) == 0)) {
191 unsigned char *buf = (unsigned char *)info;
194 /* calculate new checksum */
195 memcpy(info->config, econfig_bytes, sizeof(econfig_bytes));
196 for (chksum = 0, i = 0; i < sizeof(*info)-2; i++)
198 debug("old chksum:0x%04x\n",
199 (info->chksum[0] << 8) | info->chksum[1]);
200 debug("new chksum:0x%04x\n", chksum);
201 info->chksum[0] = chksum >> 8;
202 info->chksum[1] = chksum & 0xff;
204 /* write new config data */
205 if (gsc_i2c_write(GSC_EEPROM_ADDR, info->config - (u8 *)info,
206 1, econfig_bytes, sizeof(econfig_bytes))) {
207 printf("EEPROM: Failed updating config\n");
208 return CMD_RET_FAILURE;
211 /* write new config data */
212 if (gsc_i2c_write(GSC_EEPROM_ADDR, info->chksum - (u8 *)info,
213 1, info->chksum, 2)) {
214 printf("EEPROM: Failed updating checksum\n");
215 return CMD_RET_FAILURE;
218 printf("Config saved to EEPROM\n");
222 else if (argc == 2) {
223 cfg = get_config(argv[1]);
225 printf("%s: %d\n", cfg->name,
226 test_bit(cfg->bit, econfig_bytes) ? 1 : 0);
228 printf("invalid config: %s\n", argv[1]);
229 return CMD_RET_FAILURE;
234 else if (argc == 3) {
235 cfg = get_config(argv[1]);
237 if (simple_strtol(argv[2], NULL, 10)) {
238 test_and_set_bit(cfg->bit, econfig_bytes);
239 printf("Enabled %s\n", cfg->name);
241 test_and_clear_bit(cfg->bit, econfig_bytes);
242 printf("Disabled %s\n", cfg->name);
245 printf("invalid config: %s\n", argv[1]);
246 return CMD_RET_FAILURE;
251 return CMD_RET_USAGE;
253 return CMD_RET_SUCCESS;
257 econfig, 3, 0, do_econfig,
258 "EEPROM configuration",
259 "list - list config\n"
260 "save - save config to EEPROM\n"
261 "<name> - get config 'name'\n"
262 "<name> [0|1] - set config 'name' to value\n"
265 #endif /* CONFIG_CMD_EECONFIG */