2 * Copyright (C) 2014 Gateworks Corporation
3 * Author: Tim Harvey <tharvey@gateworks.com>
5 * SPDX-License-Identifier: GPL-2.0+
12 #include <asm/bitops.h>
15 #include "ventana_eeprom.h"
17 /* read ventana EEPROM, check for validity, and return baseboard type */
19 read_eeprom(int bus, struct ventana_board_info *info)
25 unsigned char *buf = (unsigned char *)info;
27 memset(info, 0, sizeof(*info));
30 * On a board with a missing/depleted backup battery for GSC, the
31 * board may be ready to probe the GSC before its firmware is
32 * running. We will wait here indefinately for the GSC/EEPROM.
35 if (0 == i2c_set_bus_num(bus) &&
36 0 == i2c_probe(GSC_EEPROM_ADDR))
41 /* read eeprom config section */
42 if (gsc_i2c_read(GSC_EEPROM_ADDR, 0x00, 1, buf, sizeof(*info))) {
43 puts("EEPROM: Failed to read EEPROM\n");
48 if (info->model[0] != 'G' || info->model[1] != 'W') {
49 puts("EEPROM: Invalid Model in EEPROM\n");
53 /* validate checksum */
54 for (chksum = 0, i = 0; i < sizeof(*info)-2; i++)
56 if ((info->chksum[0] != chksum>>8) ||
57 (info->chksum[1] != (chksum&0xff))) {
58 puts("EEPROM: Failed EEPROM checksum\n");
62 /* original GW5400-A prototype */
63 baseboard = info->model[3];
64 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') {
93 printf("EEPROM: Unknown model in EEPROM: %s\n", info->model);
100 /* list of config bits that the bootloader will remove from dtb if not set */
101 struct ventana_eeprom_config econfig[] = {
102 { "eth0", "ethernet0", EECONFIG_ETH0 },
103 { "usb0", NULL, EECONFIG_USB0 },
104 { "usb1", NULL, EECONFIG_USB1 },
105 { "mmc0", NULL, EECONFIG_SD0 },
106 { "mmc1", NULL, EECONFIG_SD1 },
107 { "mmc2", NULL, EECONFIG_SD2 },
108 { "mmc3", NULL, EECONFIG_SD3 },
112 #ifdef CONFIG_CMD_EECONFIG
113 static struct ventana_eeprom_config *get_config(const char *name)
115 struct ventana_eeprom_config *cfg = econfig;
118 if (0 == strcmp(name, cfg->name))
125 static u8 econfig_bytes[sizeof(ventana_info.config)];
126 static int econfig_init = -1;
128 int do_econfig(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
130 struct ventana_eeprom_config *cfg;
131 struct ventana_board_info *info = &ventana_info;
135 return CMD_RET_USAGE;
138 if (econfig_init != 1) {
139 memcpy(econfig_bytes, info->config, sizeof(econfig_bytes));
144 if ((strncmp(argv[1], "list", 4) == 0)) {
147 printf("%s: %d\n", cfg->name,
148 test_bit(cfg->bit, econfig_bytes) ? 1 : 0);
154 else if ((strncmp(argv[1], "save", 4) == 0)) {
155 unsigned char *buf = (unsigned char *)info;
158 /* calculate new checksum */
159 memcpy(info->config, econfig_bytes, sizeof(econfig_bytes));
160 for (chksum = 0, i = 0; i < sizeof(*info)-2; i++)
162 debug("old chksum:0x%04x\n",
163 (info->chksum[0] << 8) | info->chksum[1]);
164 debug("new chksum:0x%04x\n", chksum);
165 info->chksum[0] = chksum >> 8;
166 info->chksum[1] = chksum & 0xff;
168 /* write new config data */
169 if (gsc_i2c_write(GSC_EEPROM_ADDR, info->config - (u8 *)info,
170 1, econfig_bytes, sizeof(econfig_bytes))) {
171 printf("EEPROM: Failed updating config\n");
172 return CMD_RET_FAILURE;
175 /* write new config data */
176 if (gsc_i2c_write(GSC_EEPROM_ADDR, info->chksum - (u8 *)info,
177 1, info->chksum, 2)) {
178 printf("EEPROM: Failed updating checksum\n");
179 return CMD_RET_FAILURE;
182 printf("Config saved to EEPROM\n");
186 else if (argc == 2) {
187 cfg = get_config(argv[1]);
189 printf("%s: %d\n", cfg->name,
190 test_bit(cfg->bit, econfig_bytes) ? 1 : 0);
192 printf("invalid config: %s\n", argv[1]);
193 return CMD_RET_FAILURE;
198 else if (argc == 3) {
199 cfg = get_config(argv[1]);
201 if (simple_strtol(argv[2], NULL, 10)) {
202 test_and_set_bit(cfg->bit, econfig_bytes);
203 printf("Enabled %s\n", cfg->name);
205 test_and_clear_bit(cfg->bit, econfig_bytes);
206 printf("Disabled %s\n", cfg->name);
209 printf("invalid config: %s\n", argv[1]);
210 return CMD_RET_FAILURE;
215 return CMD_RET_USAGE;
217 return CMD_RET_SUCCESS;
221 econfig, 3, 0, do_econfig,
222 "EEPROM configuration",
223 "list - list config\n"
224 "save - save config to EEPROM\n"
225 "<name> - get config 'name'\n"
226 "<name> [0|1] - set config 'name' to value\n"
229 #endif /* CONFIG_CMD_EECONFIG */