blob: d21aa3c38ff2066f52bbf7282e598af2f2ca71a7 [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
7#include <common.h>
Simon Glassed38aef2020-05-10 11:40:03 -06008#include <command.h>
Tim Harvey0da2c522014-08-07 22:35:45 -07009#include <errno.h>
Tim Harveyfc93d3b2019-02-21 08:48:48 -080010#include <hexdump.h>
Tim Harvey0dccde72014-06-02 16:13:25 -070011#include <i2c.h>
Simon Glass0f2af882020-05-10 11:40:05 -060012#include <log.h>
Tim Harvey0da2c522014-08-07 22:35:45 -070013#include <malloc.h>
14#include <asm/bitops.h>
Simon Glassdbd79542020-05-10 11:40:11 -060015#include <linux/delay.h>
Tim Harvey0dccde72014-06-02 16:13:25 -070016
17#include "gsc.h"
18#include "ventana_eeprom.h"
19
20/* read ventana EEPROM, check for validity, and return baseboard type */
21int
22read_eeprom(int bus, struct ventana_board_info *info)
23{
24 int i;
25 int chksum;
26 char baseboard;
27 int type;
28 unsigned char *buf = (unsigned char *)info;
29
30 memset(info, 0, sizeof(*info));
31
32 /*
33 * On a board with a missing/depleted backup battery for GSC, the
34 * board may be ready to probe the GSC before its firmware is
35 * running. We will wait here indefinately for the GSC/EEPROM.
36 */
37 while (1) {
38 if (0 == i2c_set_bus_num(bus) &&
39 0 == i2c_probe(GSC_EEPROM_ADDR))
40 break;
41 mdelay(1);
42 }
43
44 /* read eeprom config section */
Tim Harveydf2613a2021-04-16 15:05:59 -070045 mdelay(10);
Tim Harvey0dccde72014-06-02 16:13:25 -070046 if (gsc_i2c_read(GSC_EEPROM_ADDR, 0x00, 1, buf, sizeof(*info))) {
47 puts("EEPROM: Failed to read EEPROM\n");
Tim Harvey0dccde72014-06-02 16:13:25 -070048 return GW_UNKNOWN;
49 }
50
51 /* sanity checks */
52 if (info->model[0] != 'G' || info->model[1] != 'W') {
53 puts("EEPROM: Invalid Model in EEPROM\n");
Tim Harveyfc93d3b2019-02-21 08:48:48 -080054 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, buf,
55 sizeof(*info));
Tim Harvey0dccde72014-06-02 16:13:25 -070056 return GW_UNKNOWN;
57 }
58
59 /* validate checksum */
60 for (chksum = 0, i = 0; i < sizeof(*info)-2; i++)
61 chksum += buf[i];
62 if ((info->chksum[0] != chksum>>8) ||
63 (info->chksum[1] != (chksum&0xff))) {
64 puts("EEPROM: Failed EEPROM checksum\n");
Tim Harveyfc93d3b2019-02-21 08:48:48 -080065 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, buf,
66 sizeof(*info));
Tim Harvey0dccde72014-06-02 16:13:25 -070067 return GW_UNKNOWN;
68 }
69
70 /* original GW5400-A prototype */
71 baseboard = info->model[3];
72 if (strncasecmp((const char *)info->model, "GW5400-A", 8) == 0)
73 baseboard = '0';
74
Tim Harvey63537792017-03-17 07:30:38 -070075 type = GW_UNKNOWN;
Tim Harvey0dccde72014-06-02 16:13:25 -070076 switch (baseboard) {
77 case '0': /* original GW5400-A prototype */
78 type = GW54proto;
79 break;
80 case '1':
81 type = GW51xx;
82 break;
83 case '2':
84 type = GW52xx;
85 break;
86 case '3':
87 type = GW53xx;
88 break;
89 case '4':
90 type = GW54xx;
91 break;
Tim Harvey50581832014-08-20 23:35:14 -070092 case '5':
Tim Harveyb6de3b22015-04-08 12:54:45 -070093 if (info->model[4] == '1') {
94 type = GW551x;
95 break;
96 } else if (info->model[4] == '2') {
97 type = GW552x;
98 break;
Tim Harvey892068c2016-05-24 11:03:58 -070099 } else if (info->model[4] == '3') {
100 type = GW553x;
101 break;
Tim Harveyb6de3b22015-04-08 12:54:45 -0700102 }
Tim Harvey63537792017-03-17 07:30:38 -0700103 break;
Tim Harvey659441b2017-03-17 07:31:02 -0700104 case '6':
105 if (info->model[4] == '0')
106 type = GW560x;
107 break;
Tim Harvey63537792017-03-17 07:30:38 -0700108 case '9':
Tim Harvey5852a332019-02-04 13:10:58 -0800109 if (info->model[4] == '0' && info->model[5] == '1')
110 type = GW5901;
111 else if (info->model[4] == '0' && info->model[5] == '2')
112 type = GW5902;
113 else if (info->model[4] == '0' && info->model[5] == '3')
Tim Harvey4533c902017-03-17 07:32:21 -0700114 type = GW5903;
Tim Harveya2d24c92019-02-04 13:10:50 -0800115 else if (info->model[4] == '0' && info->model[5] == '4')
Tim Harvey63537792017-03-17 07:30:38 -0700116 type = GW5904;
Tim Harveya2d24c92019-02-04 13:10:50 -0800117 else if (info->model[4] == '0' && info->model[5] == '5')
118 type = GW5905;
Tim Harveyb7c48a92019-02-04 13:10:54 -0800119 else if (info->model[4] == '0' && info->model[5] == '6')
120 type = GW5906;
Tim Harvey83cad802019-02-04 13:10:55 -0800121 else if (info->model[4] == '0' && info->model[5] == '7')
122 type = GW5907;
Tim Harveyc2625402019-02-04 13:10:56 -0800123 else if (info->model[4] == '0' && info->model[5] == '8')
124 type = GW5908;
Tim Harvey2df50462019-02-04 13:10:57 -0800125 else if (info->model[4] == '0' && info->model[5] == '9')
126 type = GW5909;
Tim Harvey08aec662021-07-24 10:40:42 -0700127 else if (info->model[4] == '1' && info->model[5] == '0')
128 type = GW5910;
Tim Harveyd67ad6e2021-07-24 10:40:43 -0700129 else if (info->model[4] == '1' && info->model[5] == '2')
130 type = GW5912;
Tim Harveyb7c9f362021-07-24 10:40:44 -0700131 else if (info->model[4] == '1' && info->model[5] == '3')
132 type = GW5913;
Tim Harvey0dccde72014-06-02 16:13:25 -0700133 break;
Tim Harveyfc93d3b2019-02-21 08:48:48 -0800134 default:
135 printf("EEPROM: Unknown model in EEPROM: %s\n", info->model);
136 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, buf,
137 sizeof(*info));
138 break;
Tim Harvey0dccde72014-06-02 16:13:25 -0700139 }
140 return type;
141}
Tim Harvey0da2c522014-08-07 22:35:45 -0700142
143/* list of config bits that the bootloader will remove from dtb if not set */
144struct ventana_eeprom_config econfig[] = {
145 { "eth0", "ethernet0", EECONFIG_ETH0 },
Tim Harvey0da2c522014-08-07 22:35:45 -0700146 { "usb0", NULL, EECONFIG_USB0 },
147 { "usb1", NULL, EECONFIG_USB1 },
148 { "mmc0", NULL, EECONFIG_SD0 },
149 { "mmc1", NULL, EECONFIG_SD1 },
150 { "mmc2", NULL, EECONFIG_SD2 },
151 { "mmc3", NULL, EECONFIG_SD3 },
Tim Harvey0da2c522014-08-07 22:35:45 -0700152 { /* Sentinel */ }
153};
154
Tom Rini3ac68752018-01-03 09:15:22 -0500155#if defined(CONFIG_CMD_EECONFIG) && !defined(CONFIG_SPL_BUILD)
Tim Harvey0da2c522014-08-07 22:35:45 -0700156static struct ventana_eeprom_config *get_config(const char *name)
157{
158 struct ventana_eeprom_config *cfg = econfig;
159
160 while (cfg->name) {
161 if (0 == strcmp(name, cfg->name))
162 return cfg;
163 cfg++;
164 }
165 return NULL;
166}
167
168static u8 econfig_bytes[sizeof(ventana_info.config)];
169static int econfig_init = -1;
170
Simon Glassed38aef2020-05-10 11:40:03 -0600171static int do_econfig(struct cmd_tbl *cmdtp, int flag, int argc,
172 char *const argv[])
Tim Harvey0da2c522014-08-07 22:35:45 -0700173{
174 struct ventana_eeprom_config *cfg;
175 struct ventana_board_info *info = &ventana_info;
176 int i;
177
178 if (argc < 2)
179 return CMD_RET_USAGE;
180
181 /* initialize */
182 if (econfig_init != 1) {
183 memcpy(econfig_bytes, info->config, sizeof(econfig_bytes));
184 econfig_init = 1;
185 }
186
187 /* list configs */
188 if ((strncmp(argv[1], "list", 4) == 0)) {
189 cfg = econfig;
190 while (cfg->name) {
191 printf("%s: %d\n", cfg->name,
192 test_bit(cfg->bit, econfig_bytes) ? 1 : 0);
193 cfg++;
194 }
195 }
196
197 /* save */
198 else if ((strncmp(argv[1], "save", 4) == 0)) {
199 unsigned char *buf = (unsigned char *)info;
200 int chksum;
201
202 /* calculate new checksum */
203 memcpy(info->config, econfig_bytes, sizeof(econfig_bytes));
204 for (chksum = 0, i = 0; i < sizeof(*info)-2; i++)
205 chksum += buf[i];
206 debug("old chksum:0x%04x\n",
207 (info->chksum[0] << 8) | info->chksum[1]);
208 debug("new chksum:0x%04x\n", chksum);
209 info->chksum[0] = chksum >> 8;
210 info->chksum[1] = chksum & 0xff;
211
212 /* write new config data */
213 if (gsc_i2c_write(GSC_EEPROM_ADDR, info->config - (u8 *)info,
214 1, econfig_bytes, sizeof(econfig_bytes))) {
215 printf("EEPROM: Failed updating config\n");
216 return CMD_RET_FAILURE;
217 }
218
219 /* write new config data */
220 if (gsc_i2c_write(GSC_EEPROM_ADDR, info->chksum - (u8 *)info,
221 1, info->chksum, 2)) {
222 printf("EEPROM: Failed updating checksum\n");
223 return CMD_RET_FAILURE;
224 }
225
226 printf("Config saved to EEPROM\n");
227 }
228
229 /* get config */
230 else if (argc == 2) {
231 cfg = get_config(argv[1]);
232 if (cfg) {
233 printf("%s: %d\n", cfg->name,
234 test_bit(cfg->bit, econfig_bytes) ? 1 : 0);
235 } else {
236 printf("invalid config: %s\n", argv[1]);
237 return CMD_RET_FAILURE;
238 }
239 }
240
241 /* set config */
242 else if (argc == 3) {
243 cfg = get_config(argv[1]);
244 if (cfg) {
245 if (simple_strtol(argv[2], NULL, 10)) {
246 test_and_set_bit(cfg->bit, econfig_bytes);
247 printf("Enabled %s\n", cfg->name);
248 } else {
249 test_and_clear_bit(cfg->bit, econfig_bytes);
250 printf("Disabled %s\n", cfg->name);
251 }
252 } else {
253 printf("invalid config: %s\n", argv[1]);
254 return CMD_RET_FAILURE;
255 }
256 }
257
258 else
259 return CMD_RET_USAGE;
260
261 return CMD_RET_SUCCESS;
262}
263
264U_BOOT_CMD(
265 econfig, 3, 0, do_econfig,
266 "EEPROM configuration",
267 "list - list config\n"
268 "save - save config to EEPROM\n"
269 "<name> - get config 'name'\n"
270 "<name> [0|1] - set config 'name' to value\n"
271);
272
273#endif /* CONFIG_CMD_EECONFIG */