blob: b9862c7dfcf1d1acbd48e4ef56e0df6c166d541f [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 Harvey0dccde72014-06-02 16:13:25 -0700127 break;
Tim Harveyfc93d3b2019-02-21 08:48:48 -0800128 default:
129 printf("EEPROM: Unknown model in EEPROM: %s\n", info->model);
130 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, buf,
131 sizeof(*info));
132 break;
Tim Harvey0dccde72014-06-02 16:13:25 -0700133 }
134 return type;
135}
Tim Harvey0da2c522014-08-07 22:35:45 -0700136
137/* list of config bits that the bootloader will remove from dtb if not set */
138struct ventana_eeprom_config econfig[] = {
139 { "eth0", "ethernet0", EECONFIG_ETH0 },
Tim Harvey0da2c522014-08-07 22:35:45 -0700140 { "usb0", NULL, EECONFIG_USB0 },
141 { "usb1", NULL, EECONFIG_USB1 },
142 { "mmc0", NULL, EECONFIG_SD0 },
143 { "mmc1", NULL, EECONFIG_SD1 },
144 { "mmc2", NULL, EECONFIG_SD2 },
145 { "mmc3", NULL, EECONFIG_SD3 },
Tim Harvey0da2c522014-08-07 22:35:45 -0700146 { /* Sentinel */ }
147};
148
Tom Rini3ac68752018-01-03 09:15:22 -0500149#if defined(CONFIG_CMD_EECONFIG) && !defined(CONFIG_SPL_BUILD)
Tim Harvey0da2c522014-08-07 22:35:45 -0700150static struct ventana_eeprom_config *get_config(const char *name)
151{
152 struct ventana_eeprom_config *cfg = econfig;
153
154 while (cfg->name) {
155 if (0 == strcmp(name, cfg->name))
156 return cfg;
157 cfg++;
158 }
159 return NULL;
160}
161
162static u8 econfig_bytes[sizeof(ventana_info.config)];
163static int econfig_init = -1;
164
Simon Glassed38aef2020-05-10 11:40:03 -0600165static int do_econfig(struct cmd_tbl *cmdtp, int flag, int argc,
166 char *const argv[])
Tim Harvey0da2c522014-08-07 22:35:45 -0700167{
168 struct ventana_eeprom_config *cfg;
169 struct ventana_board_info *info = &ventana_info;
170 int i;
171
172 if (argc < 2)
173 return CMD_RET_USAGE;
174
175 /* initialize */
176 if (econfig_init != 1) {
177 memcpy(econfig_bytes, info->config, sizeof(econfig_bytes));
178 econfig_init = 1;
179 }
180
181 /* list configs */
182 if ((strncmp(argv[1], "list", 4) == 0)) {
183 cfg = econfig;
184 while (cfg->name) {
185 printf("%s: %d\n", cfg->name,
186 test_bit(cfg->bit, econfig_bytes) ? 1 : 0);
187 cfg++;
188 }
189 }
190
191 /* save */
192 else if ((strncmp(argv[1], "save", 4) == 0)) {
193 unsigned char *buf = (unsigned char *)info;
194 int chksum;
195
196 /* calculate new checksum */
197 memcpy(info->config, econfig_bytes, sizeof(econfig_bytes));
198 for (chksum = 0, i = 0; i < sizeof(*info)-2; i++)
199 chksum += buf[i];
200 debug("old chksum:0x%04x\n",
201 (info->chksum[0] << 8) | info->chksum[1]);
202 debug("new chksum:0x%04x\n", chksum);
203 info->chksum[0] = chksum >> 8;
204 info->chksum[1] = chksum & 0xff;
205
206 /* write new config data */
207 if (gsc_i2c_write(GSC_EEPROM_ADDR, info->config - (u8 *)info,
208 1, econfig_bytes, sizeof(econfig_bytes))) {
209 printf("EEPROM: Failed updating config\n");
210 return CMD_RET_FAILURE;
211 }
212
213 /* write new config data */
214 if (gsc_i2c_write(GSC_EEPROM_ADDR, info->chksum - (u8 *)info,
215 1, info->chksum, 2)) {
216 printf("EEPROM: Failed updating checksum\n");
217 return CMD_RET_FAILURE;
218 }
219
220 printf("Config saved to EEPROM\n");
221 }
222
223 /* get config */
224 else if (argc == 2) {
225 cfg = get_config(argv[1]);
226 if (cfg) {
227 printf("%s: %d\n", cfg->name,
228 test_bit(cfg->bit, econfig_bytes) ? 1 : 0);
229 } else {
230 printf("invalid config: %s\n", argv[1]);
231 return CMD_RET_FAILURE;
232 }
233 }
234
235 /* set config */
236 else if (argc == 3) {
237 cfg = get_config(argv[1]);
238 if (cfg) {
239 if (simple_strtol(argv[2], NULL, 10)) {
240 test_and_set_bit(cfg->bit, econfig_bytes);
241 printf("Enabled %s\n", cfg->name);
242 } else {
243 test_and_clear_bit(cfg->bit, econfig_bytes);
244 printf("Disabled %s\n", cfg->name);
245 }
246 } else {
247 printf("invalid config: %s\n", argv[1]);
248 return CMD_RET_FAILURE;
249 }
250 }
251
252 else
253 return CMD_RET_USAGE;
254
255 return CMD_RET_SUCCESS;
256}
257
258U_BOOT_CMD(
259 econfig, 3, 0, do_econfig,
260 "EEPROM configuration",
261 "list - list config\n"
262 "save - save config to EEPROM\n"
263 "<name> - get config 'name'\n"
264 "<name> [0|1] - set config 'name' to value\n"
265);
266
267#endif /* CONFIG_CMD_EECONFIG */