blob: e622a9ba9e4daa97d2403ea7bfce9fcf0dfade16 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Tim Harvey0dccde72014-06-02 16:13:25 -07002/*
3 * Copyright (C) 2014 Gateworks Corporation
4 * Author: Tim Harvey <tharvey@gateworks.com>
Tim Harvey0dccde72014-06-02 16:13:25 -07005 */
6
Simon Glassed38aef2020-05-10 11:40:03 -06007#include <command.h>
Tim Harvey41377852022-04-13 09:29:16 -07008#include <common.h>
9#include <gsc.h>
Tim Harveyfc93d3b2019-02-21 08:48:48 -080010#include <hexdump.h>
Tim Harvey0dccde72014-06-02 16:13:25 -070011#include <i2c.h>
Tim Harvey41377852022-04-13 09:29:16 -070012#include <asm/arch/sys_proto.h>
13#include <dm/device.h>
Tim Harvey895aace2022-03-07 16:24:00 -080014#include <dm/uclass.h>
Tim Harvey41377852022-04-13 09:29:16 -070015#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 */
24struct ventana_board_info ventana_info;
25int board_type;
26
27#if CONFIG_IS_ENABLED(DM_I2C)
28struct 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 */
51int 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
88int 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 */
125int 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 */
142char 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}
Tim Harvey0dccde72014-06-02 16:13:25 -0700157
Tim Harvey41377852022-04-13 09:29:16 -0700158/*
159 * get dt name based on model and detail level:
160 */
161const 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 }
Tim Harvey0dccde72014-06-02 16:13:25 -0700206
Tim Harvey41377852022-04-13 09:29:16 -0700207 return buf;
208}
Tim Harvey0dccde72014-06-02 16:13:25 -0700209/* read ventana EEPROM, check for validity, and return baseboard type */
210int
Tim Harvey41377852022-04-13 09:29:16 -0700211read_eeprom(struct ventana_board_info *info)
Tim Harvey0dccde72014-06-02 16:13:25 -0700212{
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
Tim Harvey0dccde72014-06-02 16:13:25 -0700221 /* read eeprom config section */
Tim Harvey41377852022-04-13 09:29:16 -0700222 if (gsc_i2c_read(BOARD_EEPROM_ADDR, 0x00, 1, buf, sizeof(*info))) {
Tim Harvey0dccde72014-06-02 16:13:25 -0700223 puts("EEPROM: Failed to read EEPROM\n");
Tim Harvey0dccde72014-06-02 16:13:25 -0700224 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");
Tim Harveyfc93d3b2019-02-21 08:48:48 -0800230 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, buf,
231 sizeof(*info));
Tim Harvey0dccde72014-06-02 16:13:25 -0700232 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");
Tim Harveyfc93d3b2019-02-21 08:48:48 -0800241 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, buf,
242 sizeof(*info));
Tim Harvey0dccde72014-06-02 16:13:25 -0700243 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
Tim Harvey63537792017-03-17 07:30:38 -0700251 type = GW_UNKNOWN;
Tim Harvey0dccde72014-06-02 16:13:25 -0700252 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;
Tim Harvey50581832014-08-20 23:35:14 -0700268 case '5':
Tim Harveyb6de3b22015-04-08 12:54:45 -0700269 if (info->model[4] == '1') {
270 type = GW551x;
271 break;
272 } else if (info->model[4] == '2') {
273 type = GW552x;
274 break;
Tim Harvey892068c2016-05-24 11:03:58 -0700275 } else if (info->model[4] == '3') {
276 type = GW553x;
277 break;
Tim Harveyb6de3b22015-04-08 12:54:45 -0700278 }
Tim Harvey63537792017-03-17 07:30:38 -0700279 break;
Tim Harvey659441b2017-03-17 07:31:02 -0700280 case '6':
281 if (info->model[4] == '0')
282 type = GW560x;
283 break;
Tim Harvey63537792017-03-17 07:30:38 -0700284 case '9':
Tim Harvey5852a332019-02-04 13:10:58 -0800285 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')
Tim Harvey4533c902017-03-17 07:32:21 -0700290 type = GW5903;
Tim Harveya2d24c92019-02-04 13:10:50 -0800291 else if (info->model[4] == '0' && info->model[5] == '4')
Tim Harvey63537792017-03-17 07:30:38 -0700292 type = GW5904;
Tim Harveya2d24c92019-02-04 13:10:50 -0800293 else if (info->model[4] == '0' && info->model[5] == '5')
294 type = GW5905;
Tim Harveyb7c48a92019-02-04 13:10:54 -0800295 else if (info->model[4] == '0' && info->model[5] == '6')
296 type = GW5906;
Tim Harvey83cad802019-02-04 13:10:55 -0800297 else if (info->model[4] == '0' && info->model[5] == '7')
298 type = GW5907;
Tim Harveyc2625402019-02-04 13:10:56 -0800299 else if (info->model[4] == '0' && info->model[5] == '8')
300 type = GW5908;
Tim Harvey2df50462019-02-04 13:10:57 -0800301 else if (info->model[4] == '0' && info->model[5] == '9')
302 type = GW5909;
Tim Harvey08aec662021-07-24 10:40:42 -0700303 else if (info->model[4] == '1' && info->model[5] == '0')
304 type = GW5910;
Tim Harveyd67ad6e2021-07-24 10:40:43 -0700305 else if (info->model[4] == '1' && info->model[5] == '2')
306 type = GW5912;
Tim Harveyb7c9f362021-07-24 10:40:44 -0700307 else if (info->model[4] == '1' && info->model[5] == '3')
308 type = GW5913;
Tim Harvey0dccde72014-06-02 16:13:25 -0700309 break;
Tim Harveyfc93d3b2019-02-21 08:48:48 -0800310 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;
Tim Harvey0dccde72014-06-02 16:13:25 -0700315 }
316 return type;
317}
Tim Harvey0da2c522014-08-07 22:35:45 -0700318
319/* list of config bits that the bootloader will remove from dtb if not set */
320struct ventana_eeprom_config econfig[] = {
321 { "eth0", "ethernet0", EECONFIG_ETH0 },
Tim Harvey0da2c522014-08-07 22:35:45 -0700322 { "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 },
Tim Harvey0da2c522014-08-07 22:35:45 -0700328 { /* Sentinel */ }
329};
330
Tom Rini3ac68752018-01-03 09:15:22 -0500331#if defined(CONFIG_CMD_EECONFIG) && !defined(CONFIG_SPL_BUILD)
Tim Harvey0da2c522014-08-07 22:35:45 -0700332static 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
344static u8 econfig_bytes[sizeof(ventana_info.config)];
345static int econfig_init = -1;
346
Simon Glassed38aef2020-05-10 11:40:03 -0600347static int do_econfig(struct cmd_tbl *cmdtp, int flag, int argc,
348 char *const argv[])
Tim Harvey0da2c522014-08-07 22:35:45 -0700349{
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 */
Tim Harvey41377852022-04-13 09:29:16 -0700389 if (gsc_i2c_write(BOARD_EEPROM_ADDR, info->config - (u8 *)info,
Tim Harvey0da2c522014-08-07 22:35:45 -0700390 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 */
Tim Harvey41377852022-04-13 09:29:16 -0700396 if (gsc_i2c_write(BOARD_EEPROM_ADDR, info->chksum - (u8 *)info,
Tim Harvey0da2c522014-08-07 22:35:45 -0700397 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
440U_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 */