blob: a833657b15242cfb647440be3c91cad5509f21c6 [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>
Tim Harvey0da2c522014-08-07 22:35:45 -070012#include <malloc.h>
13#include <asm/bitops.h>
Tim Harvey0dccde72014-06-02 16:13:25 -070014
15#include "gsc.h"
16#include "ventana_eeprom.h"
17
18/* read ventana EEPROM, check for validity, and return baseboard type */
19int
20read_eeprom(int bus, struct ventana_board_info *info)
21{
22 int i;
23 int chksum;
24 char baseboard;
25 int type;
26 unsigned char *buf = (unsigned char *)info;
27
28 memset(info, 0, sizeof(*info));
29
30 /*
31 * On a board with a missing/depleted backup battery for GSC, the
32 * board may be ready to probe the GSC before its firmware is
33 * running. We will wait here indefinately for the GSC/EEPROM.
34 */
35 while (1) {
36 if (0 == i2c_set_bus_num(bus) &&
37 0 == i2c_probe(GSC_EEPROM_ADDR))
38 break;
39 mdelay(1);
40 }
41
42 /* read eeprom config section */
43 if (gsc_i2c_read(GSC_EEPROM_ADDR, 0x00, 1, buf, sizeof(*info))) {
44 puts("EEPROM: Failed to read EEPROM\n");
Tim Harvey0dccde72014-06-02 16:13:25 -070045 return GW_UNKNOWN;
46 }
47
48 /* sanity checks */
49 if (info->model[0] != 'G' || info->model[1] != 'W') {
50 puts("EEPROM: Invalid Model in EEPROM\n");
Tim Harveyfc93d3b2019-02-21 08:48:48 -080051 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, buf,
52 sizeof(*info));
Tim Harvey0dccde72014-06-02 16:13:25 -070053 return GW_UNKNOWN;
54 }
55
56 /* validate checksum */
57 for (chksum = 0, i = 0; i < sizeof(*info)-2; i++)
58 chksum += buf[i];
59 if ((info->chksum[0] != chksum>>8) ||
60 (info->chksum[1] != (chksum&0xff))) {
61 puts("EEPROM: Failed EEPROM checksum\n");
Tim Harveyfc93d3b2019-02-21 08:48:48 -080062 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, buf,
63 sizeof(*info));
Tim Harvey0dccde72014-06-02 16:13:25 -070064 return GW_UNKNOWN;
65 }
66
67 /* original GW5400-A prototype */
68 baseboard = info->model[3];
69 if (strncasecmp((const char *)info->model, "GW5400-A", 8) == 0)
70 baseboard = '0';
71
Tim Harvey63537792017-03-17 07:30:38 -070072 type = GW_UNKNOWN;
Tim Harvey0dccde72014-06-02 16:13:25 -070073 switch (baseboard) {
74 case '0': /* original GW5400-A prototype */
75 type = GW54proto;
76 break;
77 case '1':
78 type = GW51xx;
79 break;
80 case '2':
81 type = GW52xx;
82 break;
83 case '3':
84 type = GW53xx;
85 break;
86 case '4':
87 type = GW54xx;
88 break;
Tim Harvey50581832014-08-20 23:35:14 -070089 case '5':
Tim Harveyb6de3b22015-04-08 12:54:45 -070090 if (info->model[4] == '1') {
91 type = GW551x;
92 break;
93 } else if (info->model[4] == '2') {
94 type = GW552x;
95 break;
Tim Harvey892068c2016-05-24 11:03:58 -070096 } else if (info->model[4] == '3') {
97 type = GW553x;
98 break;
Tim Harveyb6de3b22015-04-08 12:54:45 -070099 }
Tim Harvey63537792017-03-17 07:30:38 -0700100 break;
Tim Harvey659441b2017-03-17 07:31:02 -0700101 case '6':
102 if (info->model[4] == '0')
103 type = GW560x;
104 break;
Tim Harvey63537792017-03-17 07:30:38 -0700105 case '9':
Tim Harvey5852a332019-02-04 13:10:58 -0800106 if (info->model[4] == '0' && info->model[5] == '1')
107 type = GW5901;
108 else if (info->model[4] == '0' && info->model[5] == '2')
109 type = GW5902;
110 else if (info->model[4] == '0' && info->model[5] == '3')
Tim Harvey4533c902017-03-17 07:32:21 -0700111 type = GW5903;
Tim Harveya2d24c92019-02-04 13:10:50 -0800112 else if (info->model[4] == '0' && info->model[5] == '4')
Tim Harvey63537792017-03-17 07:30:38 -0700113 type = GW5904;
Tim Harveya2d24c92019-02-04 13:10:50 -0800114 else if (info->model[4] == '0' && info->model[5] == '5')
115 type = GW5905;
Tim Harveyb7c48a92019-02-04 13:10:54 -0800116 else if (info->model[4] == '0' && info->model[5] == '6')
117 type = GW5906;
Tim Harvey83cad802019-02-04 13:10:55 -0800118 else if (info->model[4] == '0' && info->model[5] == '7')
119 type = GW5907;
Tim Harveyc2625402019-02-04 13:10:56 -0800120 else if (info->model[4] == '0' && info->model[5] == '8')
121 type = GW5908;
Tim Harvey2df50462019-02-04 13:10:57 -0800122 else if (info->model[4] == '0' && info->model[5] == '9')
123 type = GW5909;
Tim Harvey0dccde72014-06-02 16:13:25 -0700124 break;
Tim Harveyfc93d3b2019-02-21 08:48:48 -0800125 default:
126 printf("EEPROM: Unknown model in EEPROM: %s\n", info->model);
127 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, buf,
128 sizeof(*info));
129 break;
Tim Harvey0dccde72014-06-02 16:13:25 -0700130 }
131 return type;
132}
Tim Harvey0da2c522014-08-07 22:35:45 -0700133
134/* list of config bits that the bootloader will remove from dtb if not set */
135struct ventana_eeprom_config econfig[] = {
136 { "eth0", "ethernet0", EECONFIG_ETH0 },
Tim Harvey0da2c522014-08-07 22:35:45 -0700137 { "usb0", NULL, EECONFIG_USB0 },
138 { "usb1", NULL, EECONFIG_USB1 },
139 { "mmc0", NULL, EECONFIG_SD0 },
140 { "mmc1", NULL, EECONFIG_SD1 },
141 { "mmc2", NULL, EECONFIG_SD2 },
142 { "mmc3", NULL, EECONFIG_SD3 },
Tim Harvey0da2c522014-08-07 22:35:45 -0700143 { /* Sentinel */ }
144};
145
Tom Rini3ac68752018-01-03 09:15:22 -0500146#if defined(CONFIG_CMD_EECONFIG) && !defined(CONFIG_SPL_BUILD)
Tim Harvey0da2c522014-08-07 22:35:45 -0700147static struct ventana_eeprom_config *get_config(const char *name)
148{
149 struct ventana_eeprom_config *cfg = econfig;
150
151 while (cfg->name) {
152 if (0 == strcmp(name, cfg->name))
153 return cfg;
154 cfg++;
155 }
156 return NULL;
157}
158
159static u8 econfig_bytes[sizeof(ventana_info.config)];
160static int econfig_init = -1;
161
Simon Glassed38aef2020-05-10 11:40:03 -0600162static int do_econfig(struct cmd_tbl *cmdtp, int flag, int argc,
163 char *const argv[])
Tim Harvey0da2c522014-08-07 22:35:45 -0700164{
165 struct ventana_eeprom_config *cfg;
166 struct ventana_board_info *info = &ventana_info;
167 int i;
168
169 if (argc < 2)
170 return CMD_RET_USAGE;
171
172 /* initialize */
173 if (econfig_init != 1) {
174 memcpy(econfig_bytes, info->config, sizeof(econfig_bytes));
175 econfig_init = 1;
176 }
177
178 /* list configs */
179 if ((strncmp(argv[1], "list", 4) == 0)) {
180 cfg = econfig;
181 while (cfg->name) {
182 printf("%s: %d\n", cfg->name,
183 test_bit(cfg->bit, econfig_bytes) ? 1 : 0);
184 cfg++;
185 }
186 }
187
188 /* save */
189 else if ((strncmp(argv[1], "save", 4) == 0)) {
190 unsigned char *buf = (unsigned char *)info;
191 int chksum;
192
193 /* calculate new checksum */
194 memcpy(info->config, econfig_bytes, sizeof(econfig_bytes));
195 for (chksum = 0, i = 0; i < sizeof(*info)-2; i++)
196 chksum += buf[i];
197 debug("old chksum:0x%04x\n",
198 (info->chksum[0] << 8) | info->chksum[1]);
199 debug("new chksum:0x%04x\n", chksum);
200 info->chksum[0] = chksum >> 8;
201 info->chksum[1] = chksum & 0xff;
202
203 /* write new config data */
204 if (gsc_i2c_write(GSC_EEPROM_ADDR, info->config - (u8 *)info,
205 1, econfig_bytes, sizeof(econfig_bytes))) {
206 printf("EEPROM: Failed updating config\n");
207 return CMD_RET_FAILURE;
208 }
209
210 /* write new config data */
211 if (gsc_i2c_write(GSC_EEPROM_ADDR, info->chksum - (u8 *)info,
212 1, info->chksum, 2)) {
213 printf("EEPROM: Failed updating checksum\n");
214 return CMD_RET_FAILURE;
215 }
216
217 printf("Config saved to EEPROM\n");
218 }
219
220 /* get config */
221 else if (argc == 2) {
222 cfg = get_config(argv[1]);
223 if (cfg) {
224 printf("%s: %d\n", cfg->name,
225 test_bit(cfg->bit, econfig_bytes) ? 1 : 0);
226 } else {
227 printf("invalid config: %s\n", argv[1]);
228 return CMD_RET_FAILURE;
229 }
230 }
231
232 /* set config */
233 else if (argc == 3) {
234 cfg = get_config(argv[1]);
235 if (cfg) {
236 if (simple_strtol(argv[2], NULL, 10)) {
237 test_and_set_bit(cfg->bit, econfig_bytes);
238 printf("Enabled %s\n", cfg->name);
239 } else {
240 test_and_clear_bit(cfg->bit, econfig_bytes);
241 printf("Disabled %s\n", cfg->name);
242 }
243 } else {
244 printf("invalid config: %s\n", argv[1]);
245 return CMD_RET_FAILURE;
246 }
247 }
248
249 else
250 return CMD_RET_USAGE;
251
252 return CMD_RET_SUCCESS;
253}
254
255U_BOOT_CMD(
256 econfig, 3, 0, do_econfig,
257 "EEPROM configuration",
258 "list - list config\n"
259 "save - save config to EEPROM\n"
260 "<name> - get config 'name'\n"
261 "<name> [0|1] - set config 'name' to value\n"
262);
263
264#endif /* CONFIG_CMD_EECONFIG */