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