1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2014 Gateworks Corporation
4 * Author: Tim Harvey <tharvey@gateworks.com>
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>
21 * EEPROM board info struct populated by read_eeprom so that we only have to
24 struct ventana_board_info ventana_info;
27 #if CONFIG_IS_ENABLED(DM_I2C)
28 struct udevice *i2c_get_dev(int busno, int slave)
30 struct udevice *dev, *bus;
33 ret = uclass_get_device_by_seq(UCLASS_I2C, busno, &bus);
36 ret = dm_i2c_probe(bus, slave, 0, &dev);
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
51 int gsc_i2c_read(uchar chip, uint addr, int alen, uchar *buf, int len)
56 #if CONFIG_IS_ENABLED(DM_I2C)
59 dev = i2c_get_dev(BOARD_EEPROM_BUSNO, chip);
62 ret = i2c_set_chip_offset_len(dev, alen);
64 puts("EEPROM: Failed to set alen\n");
68 i2c_set_bus_num(BOARD_EEPROM_BUSNO);
72 #if CONFIG_IS_ENABLED(DM_I2C)
73 ret = dm_i2c_read(dev, addr, buf, len);
75 ret = i2c_read(chip, addr, alen, buf, len);
79 debug("%s: 0x%02x 0x%02x retry%d: %d\n", __func__, chip, addr,
88 int gsc_i2c_write(uchar chip, uint addr, int alen, uchar *buf, int len)
93 #if CONFIG_IS_ENABLED(DM_I2C)
96 dev = i2c_get_dev(BOARD_EEPROM_BUSNO, chip);
99 ret = i2c_set_chip_offset_len(dev, alen);
101 puts("EEPROM: Failed to set alen\n");
106 while (n++ < retry) {
107 #if CONFIG_IS_ENABLED(DM_I2C)
108 ret = dm_i2c_write(dev, addr, buf, len);
110 ret = i2c_write(chip, addr, alen, buf, len);
114 debug("%s: 0x%02x 0x%02x retry%d: %d\n", __func__, chip, addr,
124 /* determine BOM revision from model */
125 int get_bom_rev(const char *str)
130 for (i = strlen(str) - 1; i > 0; i--) {
133 if (str[i] >= '1' && str[i] <= '9') {
134 rev_bom = str[i] - '0';
141 /* determine PCB revision from model */
142 char get_pcb_rev(const char *str)
147 for (i = strlen(str) - 1; i > 0; i--) {
159 * get dt name based on model and detail level:
161 const char *gsc_get_dtb_name(int level, char *buf, int sz)
163 const char *model = (const char *)ventana_info.model;
164 const char *pre = is_mx6dq() ? "imx6q-" : "imx6dl-";
165 int modelno, rev_pcb, rev_bom;
167 /* a few board models are dt equivalents to other models */
168 if (strncasecmp(model, "gw5906", 6) == 0)
170 else if (strncasecmp(model, "gw5908", 6) == 0)
172 else if (strncasecmp(model, "gw5905", 6) == 0)
175 modelno = ((model[2] - '0') * 1000)
176 + ((model[3] - '0') * 100)
177 + ((model[4] - '0') * 10)
179 rev_pcb = tolower(get_pcb_rev(model));
180 rev_bom = get_bom_rev(model);
182 /* compare model/rev/bom in order of most specific to least */
183 snprintf(buf, sz, "%s%04d", pre, modelno);
185 case 0: /* full model first (ie gw5400-a1) */
187 snprintf(buf, sz, "%sgw%04d-%c%d", pre, modelno, rev_pcb, rev_bom);
191 case 1: /* don't care about bom rev (ie gw5400-a) */
192 snprintf(buf, sz, "%sgw%04d-%c", pre, modelno, rev_pcb);
194 case 2: /* don't care about the pcb rev (ie gw5400) */
195 snprintf(buf, sz, "%sgw%04d", pre, modelno);
197 case 3: /* look for generic model (ie gw540x) */
198 snprintf(buf, sz, "%sgw%03dx", pre, modelno / 10);
200 case 4: /* look for more generic model (ie gw54xx) */
201 snprintf(buf, sz, "%sgw%02dxx", pre, modelno / 100);
203 default: /* give up */
209 /* read ventana EEPROM, check for validity, and return baseboard type */
211 read_eeprom(struct ventana_board_info *info)
217 unsigned char *buf = (unsigned char *)info;
219 memset(info, 0, sizeof(*info));
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");
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,
235 /* validate checksum */
236 for (chksum = 0, i = 0; i < sizeof(*info)-2; 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,
246 /* original GW5400-A prototype */
247 baseboard = info->model[3];
248 if (strncasecmp((const char *)info->model, "GW5400-A", 8) == 0)
253 case '0': /* original GW5400-A prototype */
269 if (info->model[4] == '1') {
272 } else if (info->model[4] == '2') {
275 } else if (info->model[4] == '3') {
281 if (info->model[4] == '0')
285 if (info->model[4] == '0' && info->model[5] == '1')
287 else if (info->model[4] == '0' && info->model[5] == '2')
289 else if (info->model[4] == '0' && info->model[5] == '3')
291 else if (info->model[4] == '0' && info->model[5] == '4')
293 else if (info->model[4] == '0' && info->model[5] == '5')
295 else if (info->model[4] == '0' && info->model[5] == '6')
297 else if (info->model[4] == '0' && info->model[5] == '7')
299 else if (info->model[4] == '0' && info->model[5] == '8')
301 else if (info->model[4] == '0' && info->model[5] == '9')
303 else if (info->model[4] == '1' && info->model[5] == '0')
305 else if (info->model[4] == '1' && info->model[5] == '2')
307 else if (info->model[4] == '1' && info->model[5] == '3')
311 printf("EEPROM: Unknown model in EEPROM: %s\n", info->model);
312 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, buf,
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 },
331 #if defined(CONFIG_CMD_EECONFIG) && !defined(CONFIG_SPL_BUILD)
332 static struct ventana_eeprom_config *get_config(const char *name)
334 struct ventana_eeprom_config *cfg = econfig;
337 if (0 == strcmp(name, cfg->name))
344 static u8 econfig_bytes[sizeof(ventana_info.config)];
345 static int econfig_init = -1;
347 static int do_econfig(struct cmd_tbl *cmdtp, int flag, int argc,
350 struct ventana_eeprom_config *cfg;
351 struct ventana_board_info *info = &ventana_info;
355 return CMD_RET_USAGE;
358 if (econfig_init != 1) {
359 memcpy(econfig_bytes, info->config, sizeof(econfig_bytes));
364 if ((strncmp(argv[1], "list", 4) == 0)) {
367 printf("%s: %d\n", cfg->name,
368 test_bit(cfg->bit, econfig_bytes) ? 1 : 0);
374 else if ((strncmp(argv[1], "save", 4) == 0)) {
375 unsigned char *buf = (unsigned char *)info;
378 /* calculate new checksum */
379 memcpy(info->config, econfig_bytes, sizeof(econfig_bytes));
380 for (chksum = 0, i = 0; i < sizeof(*info)-2; 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;
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;
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;
402 printf("Config saved to EEPROM\n");
406 else if (argc == 2) {
407 cfg = get_config(argv[1]);
409 printf("%s: %d\n", cfg->name,
410 test_bit(cfg->bit, econfig_bytes) ? 1 : 0);
412 printf("invalid config: %s\n", argv[1]);
413 return CMD_RET_FAILURE;
418 else if (argc == 3) {
419 cfg = get_config(argv[1]);
421 if (simple_strtol(argv[2], NULL, 10)) {
422 test_and_set_bit(cfg->bit, econfig_bytes);
423 printf("Enabled %s\n", cfg->name);
425 test_and_clear_bit(cfg->bit, econfig_bytes);
426 printf("Disabled %s\n", cfg->name);
429 printf("invalid config: %s\n", argv[1]);
430 return CMD_RET_FAILURE;
435 return CMD_RET_USAGE;
437 return CMD_RET_SUCCESS;
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"
449 #endif /* CONFIG_CMD_EECONFIG */