1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2014 Gateworks Corporation
4 * Author: Tim Harvey <tharvey@gateworks.com>
11 #include <asm/bitops.h>
14 #include "ventana_eeprom.h"
16 /* read ventana EEPROM, check for validity, and return baseboard type */
18 read_eeprom(int bus, struct ventana_board_info *info)
24 unsigned char *buf = (unsigned char *)info;
26 memset(info, 0, sizeof(*info));
29 * On a board with a missing/depleted backup battery for GSC, the
30 * board may be ready to probe the GSC before its firmware is
31 * running. We will wait here indefinately for the GSC/EEPROM.
34 if (0 == i2c_set_bus_num(bus) &&
35 0 == i2c_probe(GSC_EEPROM_ADDR))
40 /* read eeprom config section */
41 if (gsc_i2c_read(GSC_EEPROM_ADDR, 0x00, 1, buf, sizeof(*info))) {
42 puts("EEPROM: Failed to read EEPROM\n");
47 if (info->model[0] != 'G' || info->model[1] != 'W') {
48 puts("EEPROM: Invalid Model in EEPROM\n");
52 /* validate checksum */
53 for (chksum = 0, i = 0; i < sizeof(*info)-2; i++)
55 if ((info->chksum[0] != chksum>>8) ||
56 (info->chksum[1] != (chksum&0xff))) {
57 puts("EEPROM: Failed EEPROM checksum\n");
61 /* original GW5400-A prototype */
62 baseboard = info->model[3];
63 if (strncasecmp((const char *)info->model, "GW5400-A", 8) == 0)
68 case '0': /* original GW5400-A prototype */
84 if (info->model[4] == '1') {
87 } else if (info->model[4] == '2') {
90 } else if (info->model[4] == '3') {
96 if (info->model[4] == '0')
100 if (info->model[4] == '0' && info->model[5] == '3')
102 else if (info->model[4] == '0' && info->model[5] == '4')
104 else if (info->model[4] == '0' && info->model[5] == '5')
106 else if (info->model[4] == '0' && info->model[5] == '6')
108 else if (info->model[4] == '0' && info->model[5] == '7')
115 /* list of config bits that the bootloader will remove from dtb if not set */
116 struct ventana_eeprom_config econfig[] = {
117 { "eth0", "ethernet0", EECONFIG_ETH0 },
118 { "usb0", NULL, EECONFIG_USB0 },
119 { "usb1", NULL, EECONFIG_USB1 },
120 { "mmc0", NULL, EECONFIG_SD0 },
121 { "mmc1", NULL, EECONFIG_SD1 },
122 { "mmc2", NULL, EECONFIG_SD2 },
123 { "mmc3", NULL, EECONFIG_SD3 },
127 #if defined(CONFIG_CMD_EECONFIG) && !defined(CONFIG_SPL_BUILD)
128 static struct ventana_eeprom_config *get_config(const char *name)
130 struct ventana_eeprom_config *cfg = econfig;
133 if (0 == strcmp(name, cfg->name))
140 static u8 econfig_bytes[sizeof(ventana_info.config)];
141 static int econfig_init = -1;
143 static int do_econfig(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
145 struct ventana_eeprom_config *cfg;
146 struct ventana_board_info *info = &ventana_info;
150 return CMD_RET_USAGE;
153 if (econfig_init != 1) {
154 memcpy(econfig_bytes, info->config, sizeof(econfig_bytes));
159 if ((strncmp(argv[1], "list", 4) == 0)) {
162 printf("%s: %d\n", cfg->name,
163 test_bit(cfg->bit, econfig_bytes) ? 1 : 0);
169 else if ((strncmp(argv[1], "save", 4) == 0)) {
170 unsigned char *buf = (unsigned char *)info;
173 /* calculate new checksum */
174 memcpy(info->config, econfig_bytes, sizeof(econfig_bytes));
175 for (chksum = 0, i = 0; i < sizeof(*info)-2; i++)
177 debug("old chksum:0x%04x\n",
178 (info->chksum[0] << 8) | info->chksum[1]);
179 debug("new chksum:0x%04x\n", chksum);
180 info->chksum[0] = chksum >> 8;
181 info->chksum[1] = chksum & 0xff;
183 /* write new config data */
184 if (gsc_i2c_write(GSC_EEPROM_ADDR, info->config - (u8 *)info,
185 1, econfig_bytes, sizeof(econfig_bytes))) {
186 printf("EEPROM: Failed updating config\n");
187 return CMD_RET_FAILURE;
190 /* write new config data */
191 if (gsc_i2c_write(GSC_EEPROM_ADDR, info->chksum - (u8 *)info,
192 1, info->chksum, 2)) {
193 printf("EEPROM: Failed updating checksum\n");
194 return CMD_RET_FAILURE;
197 printf("Config saved to EEPROM\n");
201 else if (argc == 2) {
202 cfg = get_config(argv[1]);
204 printf("%s: %d\n", cfg->name,
205 test_bit(cfg->bit, econfig_bytes) ? 1 : 0);
207 printf("invalid config: %s\n", argv[1]);
208 return CMD_RET_FAILURE;
213 else if (argc == 3) {
214 cfg = get_config(argv[1]);
216 if (simple_strtol(argv[2], NULL, 10)) {
217 test_and_set_bit(cfg->bit, econfig_bytes);
218 printf("Enabled %s\n", cfg->name);
220 test_and_clear_bit(cfg->bit, econfig_bytes);
221 printf("Disabled %s\n", cfg->name);
224 printf("invalid config: %s\n", argv[1]);
225 return CMD_RET_FAILURE;
230 return CMD_RET_USAGE;
232 return CMD_RET_SUCCESS;
236 econfig, 3, 0, do_econfig,
237 "EEPROM configuration",
238 "list - list config\n"
239 "save - save config to EEPROM\n"
240 "<name> - get config 'name'\n"
241 "<name> [0|1] - set config 'name' to value\n"
244 #endif /* CONFIG_CMD_EECONFIG */