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 */
87 printf("EEPROM: Unknown model in EEPROM: %s\n", info->model);
94 /* list of config bits that the bootloader will remove from dtb if not set */
95 struct ventana_eeprom_config econfig[] = {
96 { "eth0", "ethernet0", EECONFIG_ETH0 },
97 { "eth1", "ethernet1", EECONFIG_ETH1 },
98 { "sata", "ahci0", EECONFIG_SATA },
99 { "pcie", NULL, EECONFIG_PCIE},
100 { "lvds0", NULL, EECONFIG_LVDS0 },
101 { "lvds1", NULL, EECONFIG_LVDS1 },
102 { "usb0", NULL, EECONFIG_USB0 },
103 { "usb1", NULL, EECONFIG_USB1 },
104 { "mmc0", NULL, EECONFIG_SD0 },
105 { "mmc1", NULL, EECONFIG_SD1 },
106 { "mmc2", NULL, EECONFIG_SD2 },
107 { "mmc3", NULL, EECONFIG_SD3 },
108 { "uart0", NULL, EECONFIG_UART0 },
109 { "uart1", NULL, EECONFIG_UART1 },
110 { "uart2", NULL, EECONFIG_UART2 },
111 { "uart3", NULL, EECONFIG_UART3 },
112 { "uart4", NULL, EECONFIG_UART4 },
113 { "ipu0", NULL, EECONFIG_IPU0 },
114 { "ipu1", NULL, EECONFIG_IPU1 },
115 { "can0", NULL, EECONFIG_FLEXCAN },
116 { "i2c0", NULL, EECONFIG_I2C0 },
117 { "i2c1", NULL, EECONFIG_I2C1 },
118 { "i2c2", NULL, EECONFIG_I2C2 },
119 { "vpu", NULL, EECONFIG_VPU },
120 { "csi0", NULL, EECONFIG_CSI0 },
121 { "csi1", NULL, EECONFIG_CSI1 },
122 { "spi0", NULL, EECONFIG_ESPCI0 },
123 { "spi1", NULL, EECONFIG_ESPCI1 },
124 { "spi2", NULL, EECONFIG_ESPCI2 },
125 { "spi3", NULL, EECONFIG_ESPCI3 },
126 { "spi4", NULL, EECONFIG_ESPCI4 },
127 { "spi5", NULL, EECONFIG_ESPCI5 },
128 { "gps", "pps", EECONFIG_GPS },
129 { "hdmi_in", NULL, EECONFIG_HDMI_IN },
130 { "hdmi_out", NULL, EECONFIG_HDMI_OUT },
131 { "cvbs_in", NULL, EECONFIG_VID_IN },
132 { "cvbs_out", NULL, EECONFIG_VID_OUT },
133 { "nand", NULL, EECONFIG_NAND },
137 #ifdef CONFIG_CMD_EECONFIG
138 static struct ventana_eeprom_config *get_config(const char *name)
140 struct ventana_eeprom_config *cfg = econfig;
143 if (0 == strcmp(name, cfg->name))
150 static u8 econfig_bytes[sizeof(ventana_info.config)];
151 static int econfig_init = -1;
153 int do_econfig(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
155 struct ventana_eeprom_config *cfg;
156 struct ventana_board_info *info = &ventana_info;
160 return CMD_RET_USAGE;
163 if (econfig_init != 1) {
164 memcpy(econfig_bytes, info->config, sizeof(econfig_bytes));
169 if ((strncmp(argv[1], "list", 4) == 0)) {
172 printf("%s: %d\n", cfg->name,
173 test_bit(cfg->bit, econfig_bytes) ? 1 : 0);
179 else if ((strncmp(argv[1], "save", 4) == 0)) {
180 unsigned char *buf = (unsigned char *)info;
183 /* calculate new checksum */
184 memcpy(info->config, econfig_bytes, sizeof(econfig_bytes));
185 for (chksum = 0, i = 0; i < sizeof(*info)-2; i++)
187 debug("old chksum:0x%04x\n",
188 (info->chksum[0] << 8) | info->chksum[1]);
189 debug("new chksum:0x%04x\n", chksum);
190 info->chksum[0] = chksum >> 8;
191 info->chksum[1] = chksum & 0xff;
193 /* write new config data */
194 if (gsc_i2c_write(GSC_EEPROM_ADDR, info->config - (u8 *)info,
195 1, econfig_bytes, sizeof(econfig_bytes))) {
196 printf("EEPROM: Failed updating config\n");
197 return CMD_RET_FAILURE;
200 /* write new config data */
201 if (gsc_i2c_write(GSC_EEPROM_ADDR, info->chksum - (u8 *)info,
202 1, info->chksum, 2)) {
203 printf("EEPROM: Failed updating checksum\n");
204 return CMD_RET_FAILURE;
207 printf("Config saved to EEPROM\n");
211 else if (argc == 2) {
212 cfg = get_config(argv[1]);
214 printf("%s: %d\n", cfg->name,
215 test_bit(cfg->bit, econfig_bytes) ? 1 : 0);
217 printf("invalid config: %s\n", argv[1]);
218 return CMD_RET_FAILURE;
223 else if (argc == 3) {
224 cfg = get_config(argv[1]);
226 if (simple_strtol(argv[2], NULL, 10)) {
227 test_and_set_bit(cfg->bit, econfig_bytes);
228 printf("Enabled %s\n", cfg->name);
230 test_and_clear_bit(cfg->bit, econfig_bytes);
231 printf("Disabled %s\n", cfg->name);
234 printf("invalid config: %s\n", argv[1]);
235 return CMD_RET_FAILURE;
240 return CMD_RET_USAGE;
242 return CMD_RET_SUCCESS;
246 econfig, 3, 0, do_econfig,
247 "EEPROM configuration",
248 "list - list config\n"
249 "save - save config to EEPROM\n"
250 "<name> - get config 'name'\n"
251 "<name> [0|1] - set config 'name' to value\n"
254 #endif /* CONFIG_CMD_EECONFIG */