1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2014 Gateworks Corporation
4 * Author: Tim Harvey <tharvey@gateworks.com>
11 #include <asm/arch/sys_proto.h>
12 #include <dm/device.h>
13 #include <dm/uclass.h>
14 #include <linux/ctype.h>
15 #include <linux/delay.h>
20 * EEPROM board info struct populated by read_eeprom so that we only have to
23 struct ventana_board_info ventana_info;
26 #if CONFIG_IS_ENABLED(DM_I2C)
27 struct udevice *i2c_get_dev(int busno, int slave)
29 struct udevice *dev, *bus;
32 ret = uclass_get_device_by_seq(UCLASS_I2C, busno, &bus);
35 ret = dm_i2c_probe(bus, slave, 0, &dev);
44 * The Gateworks System Controller will fail to ACK a master transaction if
45 * it is busy, which can occur during its 1HZ timer tick while reading ADC's.
46 * When this does occur, it will never be busy long enough to fail more than
47 * 2 back-to-back transfers. Thus we wrap i2c_read and i2c_write with
50 int gsc_i2c_read(uchar chip, uint addr, int alen, uchar *buf, int len)
55 #if CONFIG_IS_ENABLED(DM_I2C)
58 dev = i2c_get_dev(BOARD_EEPROM_BUSNO, chip);
61 ret = i2c_set_chip_offset_len(dev, alen);
63 puts("EEPROM: Failed to set alen\n");
67 i2c_set_bus_num(BOARD_EEPROM_BUSNO);
71 #if CONFIG_IS_ENABLED(DM_I2C)
72 ret = dm_i2c_read(dev, addr, buf, len);
74 ret = i2c_read(chip, addr, alen, buf, len);
78 debug("%s: 0x%02x 0x%02x retry%d: %d\n", __func__, chip, addr,
87 int gsc_i2c_write(uchar chip, uint addr, int alen, uchar *buf, int len)
92 #if CONFIG_IS_ENABLED(DM_I2C)
95 dev = i2c_get_dev(BOARD_EEPROM_BUSNO, chip);
98 ret = i2c_set_chip_offset_len(dev, alen);
100 puts("EEPROM: Failed to set alen\n");
105 while (n++ < retry) {
106 #if CONFIG_IS_ENABLED(DM_I2C)
107 ret = dm_i2c_write(dev, addr, buf, len);
109 ret = i2c_write(chip, addr, alen, buf, len);
113 debug("%s: 0x%02x 0x%02x retry%d: %d\n", __func__, chip, addr,
123 /* determine BOM revision from model */
124 int get_bom_rev(const char *str)
129 for (i = strlen(str) - 1; i > 0; i--) {
132 if (str[i] >= '1' && str[i] <= '9') {
133 rev_bom = str[i] - '0';
140 /* determine PCB revision from model */
141 char get_pcb_rev(const char *str)
146 for (i = strlen(str) - 1; i > 0; i--) {
158 * get dt name based on model and detail level:
160 const char *gsc_get_dtb_name(int level, char *buf, int sz)
162 const char *model = (const char *)ventana_info.model;
163 const char *pre = is_mx6dq() ? "imx6q-" : "imx6dl-";
164 int modelno, rev_pcb, rev_bom;
166 /* a few board models are dt equivalents to other models */
167 if (strncasecmp(model, "gw5906", 6) == 0)
169 else if (strncasecmp(model, "gw5908", 6) == 0)
171 else if (strncasecmp(model, "gw5905", 6) == 0)
174 modelno = ((model[2] - '0') * 1000)
175 + ((model[3] - '0') * 100)
176 + ((model[4] - '0') * 10)
178 rev_pcb = tolower(get_pcb_rev(model));
179 rev_bom = get_bom_rev(model);
181 /* compare model/rev/bom in order of most specific to least */
182 snprintf(buf, sz, "%s%04d", pre, modelno);
184 case 0: /* full model first (ie gw5400-a1) */
186 snprintf(buf, sz, "%sgw%04d-%c%d", pre, modelno, rev_pcb, rev_bom);
190 case 1: /* don't care about bom rev (ie gw5400-a) */
191 snprintf(buf, sz, "%sgw%04d-%c", pre, modelno, rev_pcb);
193 case 2: /* don't care about the pcb rev (ie gw5400) */
194 snprintf(buf, sz, "%sgw%04d", pre, modelno);
196 case 3: /* look for generic model (ie gw540x) */
197 snprintf(buf, sz, "%sgw%03dx", pre, modelno / 10);
199 case 4: /* look for more generic model (ie gw54xx) */
200 snprintf(buf, sz, "%sgw%02dxx", pre, modelno / 100);
202 default: /* give up */
208 /* read ventana EEPROM, check for validity, and return baseboard type */
210 read_eeprom(struct ventana_board_info *info)
216 unsigned char *buf = (unsigned char *)info;
218 memset(info, 0, sizeof(*info));
220 /* read eeprom config section */
221 if (gsc_i2c_read(BOARD_EEPROM_ADDR, 0x00, 1, buf, sizeof(*info))) {
222 puts("EEPROM: Failed to read EEPROM\n");
227 if (info->model[0] != 'G' || info->model[1] != 'W') {
228 puts("EEPROM: Invalid Model in EEPROM\n");
229 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, buf,
234 /* validate checksum */
235 for (chksum = 0, i = 0; i < sizeof(*info)-2; i++)
237 if ((info->chksum[0] != chksum>>8) ||
238 (info->chksum[1] != (chksum&0xff))) {
239 puts("EEPROM: Failed EEPROM checksum\n");
240 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, buf,
245 /* original GW5400-A prototype */
246 baseboard = info->model[3];
247 if (strncasecmp((const char *)info->model, "GW5400-A", 8) == 0)
252 case '0': /* original GW5400-A prototype */
268 if (info->model[4] == '1') {
271 } else if (info->model[4] == '2') {
274 } else if (info->model[4] == '3') {
280 if (info->model[4] == '0')
284 if (info->model[4] == '0' && info->model[5] == '1')
286 else if (info->model[4] == '0' && info->model[5] == '2')
288 else if (info->model[4] == '0' && info->model[5] == '3')
290 else if (info->model[4] == '0' && info->model[5] == '4')
292 else if (info->model[4] == '0' && info->model[5] == '5')
294 else if (info->model[4] == '0' && info->model[5] == '6')
296 else if (info->model[4] == '0' && info->model[5] == '7')
298 else if (info->model[4] == '0' && info->model[5] == '8')
300 else if (info->model[4] == '0' && info->model[5] == '9')
302 else if (info->model[4] == '1' && info->model[5] == '0')
304 else if (info->model[4] == '1' && info->model[5] == '2')
306 else if (info->model[4] == '1' && info->model[5] == '3')
310 printf("EEPROM: Unknown model in EEPROM: %s\n", info->model);
311 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, buf,
318 /* list of config bits that the bootloader will remove from dtb if not set */
319 struct ventana_eeprom_config econfig[] = {
320 { "eth0", "ethernet0", EECONFIG_ETH0 },
321 { "usb0", NULL, EECONFIG_USB0 },
322 { "usb1", NULL, EECONFIG_USB1 },
323 { "mmc0", NULL, EECONFIG_SD0 },
324 { "mmc1", NULL, EECONFIG_SD1 },
325 { "mmc2", NULL, EECONFIG_SD2 },
326 { "mmc3", NULL, EECONFIG_SD3 },
330 #if defined(CONFIG_CMD_EECONFIG) && !defined(CONFIG_SPL_BUILD)
331 static struct ventana_eeprom_config *get_config(const char *name)
333 struct ventana_eeprom_config *cfg = econfig;
336 if (0 == strcmp(name, cfg->name))
343 static u8 econfig_bytes[sizeof(ventana_info.config)];
344 static int econfig_init = -1;
346 static int do_econfig(struct cmd_tbl *cmdtp, int flag, int argc,
349 struct ventana_eeprom_config *cfg;
350 struct ventana_board_info *info = &ventana_info;
354 return CMD_RET_USAGE;
357 if (econfig_init != 1) {
358 memcpy(econfig_bytes, info->config, sizeof(econfig_bytes));
363 if ((strncmp(argv[1], "list", 4) == 0)) {
366 printf("%s: %d\n", cfg->name,
367 test_bit(cfg->bit, econfig_bytes) ? 1 : 0);
373 else if ((strncmp(argv[1], "save", 4) == 0)) {
374 unsigned char *buf = (unsigned char *)info;
377 /* calculate new checksum */
378 memcpy(info->config, econfig_bytes, sizeof(econfig_bytes));
379 for (chksum = 0, i = 0; i < sizeof(*info)-2; i++)
381 debug("old chksum:0x%04x\n",
382 (info->chksum[0] << 8) | info->chksum[1]);
383 debug("new chksum:0x%04x\n", chksum);
384 info->chksum[0] = chksum >> 8;
385 info->chksum[1] = chksum & 0xff;
387 /* write new config data */
388 if (gsc_i2c_write(BOARD_EEPROM_ADDR, info->config - (u8 *)info,
389 1, econfig_bytes, sizeof(econfig_bytes))) {
390 printf("EEPROM: Failed updating config\n");
391 return CMD_RET_FAILURE;
394 /* write new config data */
395 if (gsc_i2c_write(BOARD_EEPROM_ADDR, info->chksum - (u8 *)info,
396 1, info->chksum, 2)) {
397 printf("EEPROM: Failed updating checksum\n");
398 return CMD_RET_FAILURE;
401 printf("Config saved to EEPROM\n");
405 else if (argc == 2) {
406 cfg = get_config(argv[1]);
408 printf("%s: %d\n", cfg->name,
409 test_bit(cfg->bit, econfig_bytes) ? 1 : 0);
411 printf("invalid config: %s\n", argv[1]);
412 return CMD_RET_FAILURE;
417 else if (argc == 3) {
418 cfg = get_config(argv[1]);
420 if (simple_strtol(argv[2], NULL, 10)) {
421 test_and_set_bit(cfg->bit, econfig_bytes);
422 printf("Enabled %s\n", cfg->name);
424 test_and_clear_bit(cfg->bit, econfig_bytes);
425 printf("Disabled %s\n", cfg->name);
428 printf("invalid config: %s\n", argv[1]);
429 return CMD_RET_FAILURE;
434 return CMD_RET_USAGE;
436 return CMD_RET_SUCCESS;
440 econfig, 3, 0, do_econfig,
441 "EEPROM configuration",
442 "list - list config\n"
443 "save - save config to EEPROM\n"
444 "<name> - get config 'name'\n"
445 "<name> [0|1] - set config 'name' to value\n"
448 #endif /* CONFIG_CMD_EECONFIG */