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 <command.h>
8 #include <common.h>
9 #include <gsc.h>
10 #include <hexdump.h>
11 #include <i2c.h>
12 #include <asm/arch/sys_proto.h>
13 #include <dm/device.h>
14 #include <dm/uclass.h>
15 #include <linux/ctype.h>
16 #include <linux/delay.h>
17
18 #include "eeprom.h"
19
20 /*
21 * EEPROM board info struct populated by read_eeprom so that we only have to
22 * read it once.
23 */
24 struct ventana_board_info ventana_info;
25 int board_type;
26
27 #if CONFIG_IS_ENABLED(DM_I2C)
i2c_get_dev(int busno,int slave)28 struct udevice *i2c_get_dev(int busno, int slave)
29 {
30 struct udevice *dev, *bus;
31 int ret;
32
33 ret = uclass_get_device_by_seq(UCLASS_I2C, busno, &bus);
34 if (ret)
35 return NULL;
36 ret = dm_i2c_probe(bus, slave, 0, &dev);
37 if (ret)
38 return NULL;
39
40 return dev;
41 }
42 #endif
43
44 /*
45 * The Gateworks System Controller will fail to ACK a master transaction if
46 * it is busy, which can occur during its 1HZ timer tick while reading ADC's.
47 * When this does occur, it will never be busy long enough to fail more than
48 * 2 back-to-back transfers. Thus we wrap i2c_read and i2c_write with
49 * 3 retries.
50 */
gsc_i2c_read(uchar chip,uint addr,int alen,uchar * buf,int len)51 int gsc_i2c_read(uchar chip, uint addr, int alen, uchar *buf, int len)
52 {
53 int retry = 3;
54 int n = 0;
55 int ret;
56 #if CONFIG_IS_ENABLED(DM_I2C)
57 struct udevice *dev;
58
59 dev = i2c_get_dev(BOARD_EEPROM_BUSNO, chip);
60 if (!dev)
61 return -ENODEV;
62 ret = i2c_set_chip_offset_len(dev, alen);
63 if (ret) {
64 puts("EEPROM: Failed to set alen\n");
65 return ret;
66 }
67 #else
68 i2c_set_bus_num(BOARD_EEPROM_BUSNO);
69 #endif
70
71 while (n++ < retry) {
72 #if CONFIG_IS_ENABLED(DM_I2C)
73 ret = dm_i2c_read(dev, addr, buf, len);
74 #else
75 ret = i2c_read(chip, addr, alen, buf, len);
76 #endif
77 if (!ret)
78 break;
79 debug("%s: 0x%02x 0x%02x retry%d: %d\n", __func__, chip, addr,
80 n, ret);
81 if (ret != -ENODEV)
82 break;
83 mdelay(10);
84 }
85 return ret;
86 }
87
gsc_i2c_write(uchar chip,uint addr,int alen,uchar * buf,int len)88 int gsc_i2c_write(uchar chip, uint addr, int alen, uchar *buf, int len)
89 {
90 int retry = 3;
91 int n = 0;
92 int ret;
93 #if CONFIG_IS_ENABLED(DM_I2C)
94 struct udevice *dev;
95
96 dev = i2c_get_dev(BOARD_EEPROM_BUSNO, chip);
97 if (!dev)
98 return -ENODEV;
99 ret = i2c_set_chip_offset_len(dev, alen);
100 if (ret) {
101 puts("EEPROM: Failed to set alen\n");
102 return ret;
103 }
104 #endif
105
106 while (n++ < retry) {
107 #if CONFIG_IS_ENABLED(DM_I2C)
108 ret = dm_i2c_write(dev, addr, buf, len);
109 #else
110 ret = i2c_write(chip, addr, alen, buf, len);
111 #endif
112 if (!ret)
113 break;
114 debug("%s: 0x%02x 0x%02x retry%d: %d\n", __func__, chip, addr,
115 n, ret);
116 if (ret != -ENODEV)
117 break;
118 mdelay(10);
119 }
120 mdelay(100);
121 return ret;
122 }
123
124 /* determine BOM revision from model */
get_bom_rev(const char * str)125 int get_bom_rev(const char *str)
126 {
127 int rev_bom = 0;
128 int i;
129
130 for (i = strlen(str) - 1; i > 0; i--) {
131 if (str[i] == '-')
132 break;
133 if (str[i] >= '1' && str[i] <= '9') {
134 rev_bom = str[i] - '0';
135 break;
136 }
137 }
138 return rev_bom;
139 }
140
141 /* determine PCB revision from model */
get_pcb_rev(const char * str)142 char get_pcb_rev(const char *str)
143 {
144 char rev_pcb = 'A';
145 int i;
146
147 for (i = strlen(str) - 1; i > 0; i--) {
148 if (str[i] == '-')
149 break;
150 if (str[i] >= 'A') {
151 rev_pcb = str[i];
152 break;
153 }
154 }
155 return rev_pcb;
156 }
157
158 /*
159 * get dt name based on model and detail level:
160 */
gsc_get_dtb_name(int level,char * buf,int sz)161 const char *gsc_get_dtb_name(int level, char *buf, int sz)
162 {
163 const char *model = (const char *)ventana_info.model;
164 const char *pre = is_mx6dq() ? "imx6q-" : "imx6dl-";
165 int modelno, rev_pcb, rev_bom;
166
167 /* a few board models are dt equivalents to other models */
168 if (strncasecmp(model, "gw5906", 6) == 0)
169 model = "gw552x-d";
170 else if (strncasecmp(model, "gw5908", 6) == 0)
171 model = "gw53xx-f";
172 else if (strncasecmp(model, "gw5905", 6) == 0)
173 model = "gw5904-a";
174
175 modelno = ((model[2] - '0') * 1000)
176 + ((model[3] - '0') * 100)
177 + ((model[4] - '0') * 10)
178 + (model[5] - '0');
179 rev_pcb = tolower(get_pcb_rev(model));
180 rev_bom = get_bom_rev(model);
181
182 /* compare model/rev/bom in order of most specific to least */
183 snprintf(buf, sz, "%s%04d", pre, modelno);
184 switch (level) {
185 case 0: /* full model first (ie gw5400-a1) */
186 if (rev_bom) {
187 snprintf(buf, sz, "%sgw%04d-%c%d", pre, modelno, rev_pcb, rev_bom);
188 break;
189 }
190 fallthrough;
191 case 1: /* don't care about bom rev (ie gw5400-a) */
192 snprintf(buf, sz, "%sgw%04d-%c", pre, modelno, rev_pcb);
193 break;
194 case 2: /* don't care about the pcb rev (ie gw5400) */
195 snprintf(buf, sz, "%sgw%04d", pre, modelno);
196 break;
197 case 3: /* look for generic model (ie gw540x) */
198 snprintf(buf, sz, "%sgw%03dx", pre, modelno / 10);
199 break;
200 case 4: /* look for more generic model (ie gw54xx) */
201 snprintf(buf, sz, "%sgw%02dxx", pre, modelno / 100);
202 break;
203 default: /* give up */
204 return NULL;
205 }
206
207 return buf;
208 }
209 /* read ventana EEPROM, check for validity, and return baseboard type */
210 int
read_eeprom(struct ventana_board_info * info)211 read_eeprom(struct ventana_board_info *info)
212 {
213 int i;
214 int chksum;
215 char baseboard;
216 int type;
217 unsigned char *buf = (unsigned char *)info;
218
219 memset(info, 0, sizeof(*info));
220
221 /* read eeprom config section */
222 if (gsc_i2c_read(BOARD_EEPROM_ADDR, 0x00, 1, buf, sizeof(*info))) {
223 puts("EEPROM: Failed to read EEPROM\n");
224 return GW_UNKNOWN;
225 }
226
227 /* sanity checks */
228 if (info->model[0] != 'G' || info->model[1] != 'W') {
229 puts("EEPROM: Invalid Model in EEPROM\n");
230 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, buf,
231 sizeof(*info));
232 return GW_UNKNOWN;
233 }
234
235 /* validate checksum */
236 for (chksum = 0, i = 0; i < sizeof(*info)-2; i++)
237 chksum += buf[i];
238 if ((info->chksum[0] != chksum>>8) ||
239 (info->chksum[1] != (chksum&0xff))) {
240 puts("EEPROM: Failed EEPROM checksum\n");
241 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, buf,
242 sizeof(*info));
243 return GW_UNKNOWN;
244 }
245
246 /* original GW5400-A prototype */
247 baseboard = info->model[3];
248 if (strncasecmp((const char *)info->model, "GW5400-A", 8) == 0)
249 baseboard = '0';
250
251 type = GW_UNKNOWN;
252 switch (baseboard) {
253 case '0': /* original GW5400-A prototype */
254 type = GW54proto;
255 break;
256 case '1':
257 type = GW51xx;
258 break;
259 case '2':
260 type = GW52xx;
261 break;
262 case '3':
263 type = GW53xx;
264 break;
265 case '4':
266 type = GW54xx;
267 break;
268 case '5':
269 if (info->model[4] == '1') {
270 type = GW551x;
271 break;
272 } else if (info->model[4] == '2') {
273 type = GW552x;
274 break;
275 } else if (info->model[4] == '3') {
276 type = GW553x;
277 break;
278 }
279 break;
280 case '6':
281 if (info->model[4] == '0')
282 type = GW560x;
283 break;
284 case '9':
285 if (info->model[4] == '0' && info->model[5] == '1')
286 type = GW5901;
287 else if (info->model[4] == '0' && info->model[5] == '2')
288 type = GW5902;
289 else if (info->model[4] == '0' && info->model[5] == '3')
290 type = GW5903;
291 else if (info->model[4] == '0' && info->model[5] == '4')
292 type = GW5904;
293 else if (info->model[4] == '0' && info->model[5] == '5')
294 type = GW5905;
295 else if (info->model[4] == '0' && info->model[5] == '6')
296 type = GW5906;
297 else if (info->model[4] == '0' && info->model[5] == '7')
298 type = GW5907;
299 else if (info->model[4] == '0' && info->model[5] == '8')
300 type = GW5908;
301 else if (info->model[4] == '0' && info->model[5] == '9')
302 type = GW5909;
303 else if (info->model[4] == '1' && info->model[5] == '0')
304 type = GW5910;
305 else if (info->model[4] == '1' && info->model[5] == '2')
306 type = GW5912;
307 else if (info->model[4] == '1' && info->model[5] == '3')
308 type = GW5913;
309 break;
310 default:
311 printf("EEPROM: Unknown model in EEPROM: %s\n", info->model);
312 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, buf,
313 sizeof(*info));
314 break;
315 }
316 return type;
317 }
318
319 /* list of config bits that the bootloader will remove from dtb if not set */
320 struct ventana_eeprom_config econfig[] = {
321 { "eth0", "ethernet0", EECONFIG_ETH0 },
322 { "usb0", NULL, EECONFIG_USB0 },
323 { "usb1", NULL, EECONFIG_USB1 },
324 { "mmc0", NULL, EECONFIG_SD0 },
325 { "mmc1", NULL, EECONFIG_SD1 },
326 { "mmc2", NULL, EECONFIG_SD2 },
327 { "mmc3", NULL, EECONFIG_SD3 },
328 { /* Sentinel */ }
329 };
330
331 #if defined(CONFIG_CMD_EECONFIG) && !defined(CONFIG_SPL_BUILD)
get_config(const char * name)332 static struct ventana_eeprom_config *get_config(const char *name)
333 {
334 struct ventana_eeprom_config *cfg = econfig;
335
336 while (cfg->name) {
337 if (0 == strcmp(name, cfg->name))
338 return cfg;
339 cfg++;
340 }
341 return NULL;
342 }
343
344 static u8 econfig_bytes[sizeof(ventana_info.config)];
345 static int econfig_init = -1;
346
do_econfig(struct cmd_tbl * cmdtp,int flag,int argc,char * const argv[])347 static int do_econfig(struct cmd_tbl *cmdtp, int flag, int argc,
348 char *const argv[])
349 {
350 struct ventana_eeprom_config *cfg;
351 struct ventana_board_info *info = &ventana_info;
352 int i;
353
354 if (argc < 2)
355 return CMD_RET_USAGE;
356
357 /* initialize */
358 if (econfig_init != 1) {
359 memcpy(econfig_bytes, info->config, sizeof(econfig_bytes));
360 econfig_init = 1;
361 }
362
363 /* list configs */
364 if ((strncmp(argv[1], "list", 4) == 0)) {
365 cfg = econfig;
366 while (cfg->name) {
367 printf("%s: %d\n", cfg->name,
368 test_bit(cfg->bit, econfig_bytes) ? 1 : 0);
369 cfg++;
370 }
371 }
372
373 /* save */
374 else if ((strncmp(argv[1], "save", 4) == 0)) {
375 unsigned char *buf = (unsigned char *)info;
376 int chksum;
377
378 /* calculate new checksum */
379 memcpy(info->config, econfig_bytes, sizeof(econfig_bytes));
380 for (chksum = 0, i = 0; i < sizeof(*info)-2; i++)
381 chksum += buf[i];
382 debug("old chksum:0x%04x\n",
383 (info->chksum[0] << 8) | info->chksum[1]);
384 debug("new chksum:0x%04x\n", chksum);
385 info->chksum[0] = chksum >> 8;
386 info->chksum[1] = chksum & 0xff;
387
388 /* write new config data */
389 if (gsc_i2c_write(BOARD_EEPROM_ADDR, info->config - (u8 *)info,
390 1, econfig_bytes, sizeof(econfig_bytes))) {
391 printf("EEPROM: Failed updating config\n");
392 return CMD_RET_FAILURE;
393 }
394
395 /* write new config data */
396 if (gsc_i2c_write(BOARD_EEPROM_ADDR, info->chksum - (u8 *)info,
397 1, info->chksum, 2)) {
398 printf("EEPROM: Failed updating checksum\n");
399 return CMD_RET_FAILURE;
400 }
401
402 printf("Config saved to EEPROM\n");
403 }
404
405 /* get config */
406 else if (argc == 2) {
407 cfg = get_config(argv[1]);
408 if (cfg) {
409 printf("%s: %d\n", cfg->name,
410 test_bit(cfg->bit, econfig_bytes) ? 1 : 0);
411 } else {
412 printf("invalid config: %s\n", argv[1]);
413 return CMD_RET_FAILURE;
414 }
415 }
416
417 /* set config */
418 else if (argc == 3) {
419 cfg = get_config(argv[1]);
420 if (cfg) {
421 if (simple_strtol(argv[2], NULL, 10)) {
422 test_and_set_bit(cfg->bit, econfig_bytes);
423 printf("Enabled %s\n", cfg->name);
424 } else {
425 test_and_clear_bit(cfg->bit, econfig_bytes);
426 printf("Disabled %s\n", cfg->name);
427 }
428 } else {
429 printf("invalid config: %s\n", argv[1]);
430 return CMD_RET_FAILURE;
431 }
432 }
433
434 else
435 return CMD_RET_USAGE;
436
437 return CMD_RET_SUCCESS;
438 }
439
440 U_BOOT_CMD(
441 econfig, 3, 0, do_econfig,
442 "EEPROM configuration",
443 "list - list config\n"
444 "save - save config to EEPROM\n"
445 "<name> - get config 'name'\n"
446 "<name> [0|1] - set config 'name' to value\n"
447 );
448
449 #endif /* CONFIG_CMD_EECONFIG */
450