blob: ee227320c05049a1656e67d9ce947fd1effe0cac [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>
Tim Harvey0da2c522014-08-07 22:35:45 -07008#include <errno.h>
Tim Harvey0dccde72014-06-02 16:13:25 -07009#include <i2c.h>
Tim Harvey0da2c522014-08-07 22:35:45 -070010#include <malloc.h>
11#include <asm/bitops.h>
Tim Harvey0dccde72014-06-02 16:13:25 -070012
13#include "gsc.h"
14#include "ventana_eeprom.h"
15
16/* read ventana EEPROM, check for validity, and return baseboard type */
17int
18read_eeprom(int bus, struct ventana_board_info *info)
19{
20 int i;
21 int chksum;
22 char baseboard;
23 int type;
24 unsigned char *buf = (unsigned char *)info;
25
26 memset(info, 0, sizeof(*info));
27
28 /*
29 * On a board with a missing/depleted backup battery for GSC, the
30 * board may be ready to probe the GSC before its firmware is
31 * running. We will wait here indefinately for the GSC/EEPROM.
32 */
33 while (1) {
34 if (0 == i2c_set_bus_num(bus) &&
35 0 == i2c_probe(GSC_EEPROM_ADDR))
36 break;
37 mdelay(1);
38 }
39
40 /* read eeprom config section */
41 if (gsc_i2c_read(GSC_EEPROM_ADDR, 0x00, 1, buf, sizeof(*info))) {
42 puts("EEPROM: Failed to read EEPROM\n");
Tim Harvey0dccde72014-06-02 16:13:25 -070043 return GW_UNKNOWN;
44 }
45
46 /* sanity checks */
47 if (info->model[0] != 'G' || info->model[1] != 'W') {
48 puts("EEPROM: Invalid Model in EEPROM\n");
Tim Harvey0dccde72014-06-02 16:13:25 -070049 return GW_UNKNOWN;
50 }
51
52 /* validate checksum */
53 for (chksum = 0, i = 0; i < sizeof(*info)-2; i++)
54 chksum += buf[i];
55 if ((info->chksum[0] != chksum>>8) ||
56 (info->chksum[1] != (chksum&0xff))) {
57 puts("EEPROM: Failed EEPROM checksum\n");
Tim Harvey0dccde72014-06-02 16:13:25 -070058 return GW_UNKNOWN;
59 }
60
61 /* original GW5400-A prototype */
62 baseboard = info->model[3];
63 if (strncasecmp((const char *)info->model, "GW5400-A", 8) == 0)
64 baseboard = '0';
65
Tim Harvey63537792017-03-17 07:30:38 -070066 type = GW_UNKNOWN;
Tim Harvey0dccde72014-06-02 16:13:25 -070067 switch (baseboard) {
68 case '0': /* original GW5400-A prototype */
69 type = GW54proto;
70 break;
71 case '1':
72 type = GW51xx;
73 break;
74 case '2':
75 type = GW52xx;
76 break;
77 case '3':
78 type = GW53xx;
79 break;
80 case '4':
81 type = GW54xx;
82 break;
Tim Harvey50581832014-08-20 23:35:14 -070083 case '5':
Tim Harveyb6de3b22015-04-08 12:54:45 -070084 if (info->model[4] == '1') {
85 type = GW551x;
86 break;
87 } else if (info->model[4] == '2') {
88 type = GW552x;
89 break;
Tim Harvey892068c2016-05-24 11:03:58 -070090 } else if (info->model[4] == '3') {
91 type = GW553x;
92 break;
Tim Harveyb6de3b22015-04-08 12:54:45 -070093 }
Tim Harvey63537792017-03-17 07:30:38 -070094 break;
Tim Harvey659441b2017-03-17 07:31:02 -070095 case '6':
96 if (info->model[4] == '0')
97 type = GW560x;
98 break;
Tim Harvey63537792017-03-17 07:30:38 -070099 case '9':
Tim Harvey5852a332019-02-04 13:10:58 -0800100 if (info->model[4] == '0' && info->model[5] == '1')
101 type = GW5901;
102 else if (info->model[4] == '0' && info->model[5] == '2')
103 type = GW5902;
104 else if (info->model[4] == '0' && info->model[5] == '3')
Tim Harvey4533c902017-03-17 07:32:21 -0700105 type = GW5903;
Tim Harveya2d24c92019-02-04 13:10:50 -0800106 else if (info->model[4] == '0' && info->model[5] == '4')
Tim Harvey63537792017-03-17 07:30:38 -0700107 type = GW5904;
Tim Harveya2d24c92019-02-04 13:10:50 -0800108 else if (info->model[4] == '0' && info->model[5] == '5')
109 type = GW5905;
Tim Harveyb7c48a92019-02-04 13:10:54 -0800110 else if (info->model[4] == '0' && info->model[5] == '6')
111 type = GW5906;
Tim Harvey83cad802019-02-04 13:10:55 -0800112 else if (info->model[4] == '0' && info->model[5] == '7')
113 type = GW5907;
Tim Harveyc2625402019-02-04 13:10:56 -0800114 else if (info->model[4] == '0' && info->model[5] == '8')
115 type = GW5908;
Tim Harvey2df50462019-02-04 13:10:57 -0800116 else if (info->model[4] == '0' && info->model[5] == '9')
117 type = GW5909;
Tim Harvey0dccde72014-06-02 16:13:25 -0700118 break;
119 }
120 return type;
121}
Tim Harvey0da2c522014-08-07 22:35:45 -0700122
123/* list of config bits that the bootloader will remove from dtb if not set */
124struct ventana_eeprom_config econfig[] = {
125 { "eth0", "ethernet0", EECONFIG_ETH0 },
Tim Harvey0da2c522014-08-07 22:35:45 -0700126 { "usb0", NULL, EECONFIG_USB0 },
127 { "usb1", NULL, EECONFIG_USB1 },
128 { "mmc0", NULL, EECONFIG_SD0 },
129 { "mmc1", NULL, EECONFIG_SD1 },
130 { "mmc2", NULL, EECONFIG_SD2 },
131 { "mmc3", NULL, EECONFIG_SD3 },
Tim Harvey0da2c522014-08-07 22:35:45 -0700132 { /* Sentinel */ }
133};
134
Tom Rini3ac68752018-01-03 09:15:22 -0500135#if defined(CONFIG_CMD_EECONFIG) && !defined(CONFIG_SPL_BUILD)
Tim Harvey0da2c522014-08-07 22:35:45 -0700136static struct ventana_eeprom_config *get_config(const char *name)
137{
138 struct ventana_eeprom_config *cfg = econfig;
139
140 while (cfg->name) {
141 if (0 == strcmp(name, cfg->name))
142 return cfg;
143 cfg++;
144 }
145 return NULL;
146}
147
148static u8 econfig_bytes[sizeof(ventana_info.config)];
149static int econfig_init = -1;
150
Tom Rini3ac68752018-01-03 09:15:22 -0500151static int do_econfig(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Tim Harvey0da2c522014-08-07 22:35:45 -0700152{
153 struct ventana_eeprom_config *cfg;
154 struct ventana_board_info *info = &ventana_info;
155 int i;
156
157 if (argc < 2)
158 return CMD_RET_USAGE;
159
160 /* initialize */
161 if (econfig_init != 1) {
162 memcpy(econfig_bytes, info->config, sizeof(econfig_bytes));
163 econfig_init = 1;
164 }
165
166 /* list configs */
167 if ((strncmp(argv[1], "list", 4) == 0)) {
168 cfg = econfig;
169 while (cfg->name) {
170 printf("%s: %d\n", cfg->name,
171 test_bit(cfg->bit, econfig_bytes) ? 1 : 0);
172 cfg++;
173 }
174 }
175
176 /* save */
177 else if ((strncmp(argv[1], "save", 4) == 0)) {
178 unsigned char *buf = (unsigned char *)info;
179 int chksum;
180
181 /* calculate new checksum */
182 memcpy(info->config, econfig_bytes, sizeof(econfig_bytes));
183 for (chksum = 0, i = 0; i < sizeof(*info)-2; i++)
184 chksum += buf[i];
185 debug("old chksum:0x%04x\n",
186 (info->chksum[0] << 8) | info->chksum[1]);
187 debug("new chksum:0x%04x\n", chksum);
188 info->chksum[0] = chksum >> 8;
189 info->chksum[1] = chksum & 0xff;
190
191 /* write new config data */
192 if (gsc_i2c_write(GSC_EEPROM_ADDR, info->config - (u8 *)info,
193 1, econfig_bytes, sizeof(econfig_bytes))) {
194 printf("EEPROM: Failed updating config\n");
195 return CMD_RET_FAILURE;
196 }
197
198 /* write new config data */
199 if (gsc_i2c_write(GSC_EEPROM_ADDR, info->chksum - (u8 *)info,
200 1, info->chksum, 2)) {
201 printf("EEPROM: Failed updating checksum\n");
202 return CMD_RET_FAILURE;
203 }
204
205 printf("Config saved to EEPROM\n");
206 }
207
208 /* get config */
209 else if (argc == 2) {
210 cfg = get_config(argv[1]);
211 if (cfg) {
212 printf("%s: %d\n", cfg->name,
213 test_bit(cfg->bit, econfig_bytes) ? 1 : 0);
214 } else {
215 printf("invalid config: %s\n", argv[1]);
216 return CMD_RET_FAILURE;
217 }
218 }
219
220 /* set config */
221 else if (argc == 3) {
222 cfg = get_config(argv[1]);
223 if (cfg) {
224 if (simple_strtol(argv[2], NULL, 10)) {
225 test_and_set_bit(cfg->bit, econfig_bytes);
226 printf("Enabled %s\n", cfg->name);
227 } else {
228 test_and_clear_bit(cfg->bit, econfig_bytes);
229 printf("Disabled %s\n", cfg->name);
230 }
231 } else {
232 printf("invalid config: %s\n", argv[1]);
233 return CMD_RET_FAILURE;
234 }
235 }
236
237 else
238 return CMD_RET_USAGE;
239
240 return CMD_RET_SUCCESS;
241}
242
243U_BOOT_CMD(
244 econfig, 3, 0, do_econfig,
245 "EEPROM configuration",
246 "list - list config\n"
247 "save - save config to EEPROM\n"
248 "<name> - get config 'name'\n"
249 "<name> [0|1] - set config 'name' to value\n"
250);
251
252#endif /* CONFIG_CMD_EECONFIG */