blob: a5a151d85b43ca1d62c9db6fdfcc1b3993f0e9fc [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 */
45 if (gsc_i2c_read(GSC_EEPROM_ADDR, 0x00, 1, buf, sizeof(*info))) {
46 puts("EEPROM: Failed to read EEPROM\n");
Tim Harvey0dccde72014-06-02 16:13:25 -070047 return GW_UNKNOWN;
48 }
49
50 /* sanity checks */
51 if (info->model[0] != 'G' || info->model[1] != 'W') {
52 puts("EEPROM: Invalid Model in EEPROM\n");
Tim Harveyfc93d3b2019-02-21 08:48:48 -080053 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, buf,
54 sizeof(*info));
Tim Harvey0dccde72014-06-02 16:13:25 -070055 return GW_UNKNOWN;
56 }
57
58 /* validate checksum */
59 for (chksum = 0, i = 0; i < sizeof(*info)-2; i++)
60 chksum += buf[i];
61 if ((info->chksum[0] != chksum>>8) ||
62 (info->chksum[1] != (chksum&0xff))) {
63 puts("EEPROM: Failed EEPROM checksum\n");
Tim Harveyfc93d3b2019-02-21 08:48:48 -080064 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, buf,
65 sizeof(*info));
Tim Harvey0dccde72014-06-02 16:13:25 -070066 return GW_UNKNOWN;
67 }
68
69 /* original GW5400-A prototype */
70 baseboard = info->model[3];
71 if (strncasecmp((const char *)info->model, "GW5400-A", 8) == 0)
72 baseboard = '0';
73
Tim Harvey63537792017-03-17 07:30:38 -070074 type = GW_UNKNOWN;
Tim Harvey0dccde72014-06-02 16:13:25 -070075 switch (baseboard) {
76 case '0': /* original GW5400-A prototype */
77 type = GW54proto;
78 break;
79 case '1':
80 type = GW51xx;
81 break;
82 case '2':
83 type = GW52xx;
84 break;
85 case '3':
86 type = GW53xx;
87 break;
88 case '4':
89 type = GW54xx;
90 break;
Tim Harvey50581832014-08-20 23:35:14 -070091 case '5':
Tim Harveyb6de3b22015-04-08 12:54:45 -070092 if (info->model[4] == '1') {
93 type = GW551x;
94 break;
95 } else if (info->model[4] == '2') {
96 type = GW552x;
97 break;
Tim Harvey892068c2016-05-24 11:03:58 -070098 } else if (info->model[4] == '3') {
99 type = GW553x;
100 break;
Tim Harveyb6de3b22015-04-08 12:54:45 -0700101 }
Tim Harvey63537792017-03-17 07:30:38 -0700102 break;
Tim Harvey659441b2017-03-17 07:31:02 -0700103 case '6':
104 if (info->model[4] == '0')
105 type = GW560x;
106 break;
Tim Harvey63537792017-03-17 07:30:38 -0700107 case '9':
Tim Harvey5852a332019-02-04 13:10:58 -0800108 if (info->model[4] == '0' && info->model[5] == '1')
109 type = GW5901;
110 else if (info->model[4] == '0' && info->model[5] == '2')
111 type = GW5902;
112 else if (info->model[4] == '0' && info->model[5] == '3')
Tim Harvey4533c902017-03-17 07:32:21 -0700113 type = GW5903;
Tim Harveya2d24c92019-02-04 13:10:50 -0800114 else if (info->model[4] == '0' && info->model[5] == '4')
Tim Harvey63537792017-03-17 07:30:38 -0700115 type = GW5904;
Tim Harveya2d24c92019-02-04 13:10:50 -0800116 else if (info->model[4] == '0' && info->model[5] == '5')
117 type = GW5905;
Tim Harveyb7c48a92019-02-04 13:10:54 -0800118 else if (info->model[4] == '0' && info->model[5] == '6')
119 type = GW5906;
Tim Harvey83cad802019-02-04 13:10:55 -0800120 else if (info->model[4] == '0' && info->model[5] == '7')
121 type = GW5907;
Tim Harveyc2625402019-02-04 13:10:56 -0800122 else if (info->model[4] == '0' && info->model[5] == '8')
123 type = GW5908;
Tim Harvey2df50462019-02-04 13:10:57 -0800124 else if (info->model[4] == '0' && info->model[5] == '9')
125 type = GW5909;
Tim Harvey0dccde72014-06-02 16:13:25 -0700126 break;
Tim Harveyfc93d3b2019-02-21 08:48:48 -0800127 default:
128 printf("EEPROM: Unknown model in EEPROM: %s\n", info->model);
129 print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, buf,
130 sizeof(*info));
131 break;
Tim Harvey0dccde72014-06-02 16:13:25 -0700132 }
133 return type;
134}
Tim Harvey0da2c522014-08-07 22:35:45 -0700135
136/* list of config bits that the bootloader will remove from dtb if not set */
137struct ventana_eeprom_config econfig[] = {
138 { "eth0", "ethernet0", EECONFIG_ETH0 },
Tim Harvey0da2c522014-08-07 22:35:45 -0700139 { "usb0", NULL, EECONFIG_USB0 },
140 { "usb1", NULL, EECONFIG_USB1 },
141 { "mmc0", NULL, EECONFIG_SD0 },
142 { "mmc1", NULL, EECONFIG_SD1 },
143 { "mmc2", NULL, EECONFIG_SD2 },
144 { "mmc3", NULL, EECONFIG_SD3 },
Tim Harvey0da2c522014-08-07 22:35:45 -0700145 { /* Sentinel */ }
146};
147
Tom Rini3ac68752018-01-03 09:15:22 -0500148#if defined(CONFIG_CMD_EECONFIG) && !defined(CONFIG_SPL_BUILD)
Tim Harvey0da2c522014-08-07 22:35:45 -0700149static struct ventana_eeprom_config *get_config(const char *name)
150{
151 struct ventana_eeprom_config *cfg = econfig;
152
153 while (cfg->name) {
154 if (0 == strcmp(name, cfg->name))
155 return cfg;
156 cfg++;
157 }
158 return NULL;
159}
160
161static u8 econfig_bytes[sizeof(ventana_info.config)];
162static int econfig_init = -1;
163
Simon Glassed38aef2020-05-10 11:40:03 -0600164static int do_econfig(struct cmd_tbl *cmdtp, int flag, int argc,
165 char *const argv[])
Tim Harvey0da2c522014-08-07 22:35:45 -0700166{
167 struct ventana_eeprom_config *cfg;
168 struct ventana_board_info *info = &ventana_info;
169 int i;
170
171 if (argc < 2)
172 return CMD_RET_USAGE;
173
174 /* initialize */
175 if (econfig_init != 1) {
176 memcpy(econfig_bytes, info->config, sizeof(econfig_bytes));
177 econfig_init = 1;
178 }
179
180 /* list configs */
181 if ((strncmp(argv[1], "list", 4) == 0)) {
182 cfg = econfig;
183 while (cfg->name) {
184 printf("%s: %d\n", cfg->name,
185 test_bit(cfg->bit, econfig_bytes) ? 1 : 0);
186 cfg++;
187 }
188 }
189
190 /* save */
191 else if ((strncmp(argv[1], "save", 4) == 0)) {
192 unsigned char *buf = (unsigned char *)info;
193 int chksum;
194
195 /* calculate new checksum */
196 memcpy(info->config, econfig_bytes, sizeof(econfig_bytes));
197 for (chksum = 0, i = 0; i < sizeof(*info)-2; i++)
198 chksum += buf[i];
199 debug("old chksum:0x%04x\n",
200 (info->chksum[0] << 8) | info->chksum[1]);
201 debug("new chksum:0x%04x\n", chksum);
202 info->chksum[0] = chksum >> 8;
203 info->chksum[1] = chksum & 0xff;
204
205 /* write new config data */
206 if (gsc_i2c_write(GSC_EEPROM_ADDR, info->config - (u8 *)info,
207 1, econfig_bytes, sizeof(econfig_bytes))) {
208 printf("EEPROM: Failed updating config\n");
209 return CMD_RET_FAILURE;
210 }
211
212 /* write new config data */
213 if (gsc_i2c_write(GSC_EEPROM_ADDR, info->chksum - (u8 *)info,
214 1, info->chksum, 2)) {
215 printf("EEPROM: Failed updating checksum\n");
216 return CMD_RET_FAILURE;
217 }
218
219 printf("Config saved to EEPROM\n");
220 }
221
222 /* get config */
223 else if (argc == 2) {
224 cfg = get_config(argv[1]);
225 if (cfg) {
226 printf("%s: %d\n", cfg->name,
227 test_bit(cfg->bit, econfig_bytes) ? 1 : 0);
228 } else {
229 printf("invalid config: %s\n", argv[1]);
230 return CMD_RET_FAILURE;
231 }
232 }
233
234 /* set config */
235 else if (argc == 3) {
236 cfg = get_config(argv[1]);
237 if (cfg) {
238 if (simple_strtol(argv[2], NULL, 10)) {
239 test_and_set_bit(cfg->bit, econfig_bytes);
240 printf("Enabled %s\n", cfg->name);
241 } else {
242 test_and_clear_bit(cfg->bit, econfig_bytes);
243 printf("Disabled %s\n", cfg->name);
244 }
245 } else {
246 printf("invalid config: %s\n", argv[1]);
247 return CMD_RET_FAILURE;
248 }
249 }
250
251 else
252 return CMD_RET_USAGE;
253
254 return CMD_RET_SUCCESS;
255}
256
257U_BOOT_CMD(
258 econfig, 3, 0, do_econfig,
259 "EEPROM configuration",
260 "list - list config\n"
261 "save - save config to EEPROM\n"
262 "<name> - get config 'name'\n"
263 "<name> [0|1] - set config 'name' to value\n"
264);
265
266#endif /* CONFIG_CMD_EECONFIG */