1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * Copyright (C) 2014 Gateworks Corporation
4 * Author: Tim Harvey <tharvey@gateworks.com>
5 */
6
7 #include <common.h>
8 #include <command.h>
9 #include <errno.h>
10 #include <hexdump.h>
11 #include <i2c.h>
12 #include <log.h>
13 #include <malloc.h>
14 #include <asm/bitops.h>
15 #include <linux/delay.h>
16
17 #include "gsc.h"
18 #include "ventana_eeprom.h"
19
20 /* read ventana EEPROM, check for validity, and return baseboard type */
21 int
read_eeprom(int bus,struct ventana_board_info * info)22 read_eeprom(int bus, struct ventana_board_info *info)
23 {
24 int i;
25 int chksum;
26 char baseboard;
27 int type;
28 unsigned char *buf = (unsigned char *)info;
29
30 memset(info, 0, sizeof(*info));
31
32 /*
33 * On a board with a missing/depleted backup battery for GSC, the
34 * board may be ready to probe the GSC before its firmware is
35 * running. We will wait here indefinately for the GSC/EEPROM.
36 */
37 while (1) {
38 if (0 == i2c_set_bus_num(bus) &&
39 0 == i2c_probe(GSC_EEPROM_ADDR))
40 break;
41 mdelay(1);
42 }
43
44 /* read eeprom config section */
45 if (gsc_i2c_read(GSC_EEPROM_ADDR, 0x00, 1, buf, sizeof(*info))) {
46 puts("EEPROM: Failed to read EEPROM\n");
47 return GW_UNKNOWN;
48 }
49
50 /* sanity checks */
51 if (info->model[0] != 'G' || info->model[1] != 'W') {
52 puts("EEPROM: Invalid Model in EEPROM\n");
53 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, buf,
54 sizeof(*info));
55 return GW_UNKNOWN;
56 }
57
58 /* validate checksum */
59 for (chksum = 0, i = 0; i < sizeof(*info)-2; i++)
60 chksum += buf[i];
61 if ((info->chksum[0] != chksum>>8) ||
62 (info->chksum[1] != (chksum&0xff))) {
63 puts("EEPROM: Failed EEPROM checksum\n");
64 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, buf,
65 sizeof(*info));
66 return GW_UNKNOWN;
67 }
68
69 /* original GW5400-A prototype */
70 baseboard = info->model[3];
71 if (strncasecmp((const char *)info->model, "GW5400-A", 8) == 0)
72 baseboard = '0';
73
74 type = GW_UNKNOWN;
75 switch (baseboard) {
76 case '0': /* original GW5400-A prototype */
77 type = GW54proto;
78 break;
79 case '1':
80 type = GW51xx;
81 break;
82 case '2':
83 type = GW52xx;
84 break;
85 case '3':
86 type = GW53xx;
87 break;
88 case '4':
89 type = GW54xx;
90 break;
91 case '5':
92 if (info->model[4] == '1') {
93 type = GW551x;
94 break;
95 } else if (info->model[4] == '2') {
96 type = GW552x;
97 break;
98 } else if (info->model[4] == '3') {
99 type = GW553x;
100 break;
101 }
102 break;
103 case '6':
104 if (info->model[4] == '0')
105 type = GW560x;
106 break;
107 case '9':
108 if (info->model[4] == '0' && info->model[5] == '1')
109 type = GW5901;
110 else if (info->model[4] == '0' && info->model[5] == '2')
111 type = GW5902;
112 else if (info->model[4] == '0' && info->model[5] == '3')
113 type = GW5903;
114 else if (info->model[4] == '0' && info->model[5] == '4')
115 type = GW5904;
116 else if (info->model[4] == '0' && info->model[5] == '5')
117 type = GW5905;
118 else if (info->model[4] == '0' && info->model[5] == '6')
119 type = GW5906;
120 else if (info->model[4] == '0' && info->model[5] == '7')
121 type = GW5907;
122 else if (info->model[4] == '0' && info->model[5] == '8')
123 type = GW5908;
124 else if (info->model[4] == '0' && info->model[5] == '9')
125 type = GW5909;
126 break;
127 default:
128 printf("EEPROM: Unknown model in EEPROM: %s\n", info->model);
129 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, buf,
130 sizeof(*info));
131 break;
132 }
133 return type;
134 }
135
136 /* list of config bits that the bootloader will remove from dtb if not set */
137 struct ventana_eeprom_config econfig[] = {
138 { "eth0", "ethernet0", EECONFIG_ETH0 },
139 { "usb0", NULL, EECONFIG_USB0 },
140 { "usb1", NULL, EECONFIG_USB1 },
141 { "mmc0", NULL, EECONFIG_SD0 },
142 { "mmc1", NULL, EECONFIG_SD1 },
143 { "mmc2", NULL, EECONFIG_SD2 },
144 { "mmc3", NULL, EECONFIG_SD3 },
145 { /* Sentinel */ }
146 };
147
148 #if defined(CONFIG_CMD_EECONFIG) && !defined(CONFIG_SPL_BUILD)
get_config(const char * name)149 static struct ventana_eeprom_config *get_config(const char *name)
150 {
151 struct ventana_eeprom_config *cfg = econfig;
152
153 while (cfg->name) {
154 if (0 == strcmp(name, cfg->name))
155 return cfg;
156 cfg++;
157 }
158 return NULL;
159 }
160
161 static u8 econfig_bytes[sizeof(ventana_info.config)];
162 static int econfig_init = -1;
163
do_econfig(struct cmd_tbl * cmdtp,int flag,int argc,char * const argv[])164 static int do_econfig(struct cmd_tbl *cmdtp, int flag, int argc,
165 char *const argv[])
166 {
167 struct ventana_eeprom_config *cfg;
168 struct ventana_board_info *info = &ventana_info;
169 int i;
170
171 if (argc < 2)
172 return CMD_RET_USAGE;
173
174 /* initialize */
175 if (econfig_init != 1) {
176 memcpy(econfig_bytes, info->config, sizeof(econfig_bytes));
177 econfig_init = 1;
178 }
179
180 /* list configs */
181 if ((strncmp(argv[1], "list", 4) == 0)) {
182 cfg = econfig;
183 while (cfg->name) {
184 printf("%s: %d\n", cfg->name,
185 test_bit(cfg->bit, econfig_bytes) ? 1 : 0);
186 cfg++;
187 }
188 }
189
190 /* save */
191 else if ((strncmp(argv[1], "save", 4) == 0)) {
192 unsigned char *buf = (unsigned char *)info;
193 int chksum;
194
195 /* calculate new checksum */
196 memcpy(info->config, econfig_bytes, sizeof(econfig_bytes));
197 for (chksum = 0, i = 0; i < sizeof(*info)-2; i++)
198 chksum += buf[i];
199 debug("old chksum:0x%04x\n",
200 (info->chksum[0] << 8) | info->chksum[1]);
201 debug("new chksum:0x%04x\n", chksum);
202 info->chksum[0] = chksum >> 8;
203 info->chksum[1] = chksum & 0xff;
204
205 /* write new config data */
206 if (gsc_i2c_write(GSC_EEPROM_ADDR, info->config - (u8 *)info,
207 1, econfig_bytes, sizeof(econfig_bytes))) {
208 printf("EEPROM: Failed updating config\n");
209 return CMD_RET_FAILURE;
210 }
211
212 /* write new config data */
213 if (gsc_i2c_write(GSC_EEPROM_ADDR, info->chksum - (u8 *)info,
214 1, info->chksum, 2)) {
215 printf("EEPROM: Failed updating checksum\n");
216 return CMD_RET_FAILURE;
217 }
218
219 printf("Config saved to EEPROM\n");
220 }
221
222 /* get config */
223 else if (argc == 2) {
224 cfg = get_config(argv[1]);
225 if (cfg) {
226 printf("%s: %d\n", cfg->name,
227 test_bit(cfg->bit, econfig_bytes) ? 1 : 0);
228 } else {
229 printf("invalid config: %s\n", argv[1]);
230 return CMD_RET_FAILURE;
231 }
232 }
233
234 /* set config */
235 else if (argc == 3) {
236 cfg = get_config(argv[1]);
237 if (cfg) {
238 if (simple_strtol(argv[2], NULL, 10)) {
239 test_and_set_bit(cfg->bit, econfig_bytes);
240 printf("Enabled %s\n", cfg->name);
241 } else {
242 test_and_clear_bit(cfg->bit, econfig_bytes);
243 printf("Disabled %s\n", cfg->name);
244 }
245 } else {
246 printf("invalid config: %s\n", argv[1]);
247 return CMD_RET_FAILURE;
248 }
249 }
250
251 else
252 return CMD_RET_USAGE;
253
254 return CMD_RET_SUCCESS;
255 }
256
257 U_BOOT_CMD(
258 econfig, 3, 0, do_econfig,
259 "EEPROM configuration",
260 "list - list config\n"
261 "save - save config to EEPROM\n"
262 "<name> - get config 'name'\n"
263 "<name> [0|1] - set config 'name' to value\n"
264 );
265
266 #endif /* CONFIG_CMD_EECONFIG */
267