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