1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (C) 2014 Gateworks Corporation
4  * Author: Tim Harvey <tharvey@gateworks.com>
5  */
6 
7 #include <common.h>
8 #include <command.h>
9 #include <errno.h>
10 #include <hexdump.h>
11 #include <i2c.h>
12 #include <log.h>
13 #include <malloc.h>
14 #include <asm/bitops.h>
15 #include <linux/delay.h>
16 
17 #include "gsc.h"
18 #include "ventana_eeprom.h"
19 
20 /* read ventana EEPROM, check for validity, and return baseboard type */
21 int
read_eeprom(int bus,struct ventana_board_info * info)22 read_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 	mdelay(10);
46 	if (gsc_i2c_read(GSC_EEPROM_ADDR, 0x00, 1, buf, sizeof(*info))) {
47 		puts("EEPROM: Failed to read EEPROM\n");
48 		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");
54 		print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, buf,
55 				     sizeof(*info));
56 		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");
65 		print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, buf,
66 				     sizeof(*info));
67 		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 
75 	type = GW_UNKNOWN;
76 	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;
92 	case '5':
93 		if (info->model[4] == '1') {
94 			type = GW551x;
95 			break;
96 		} else if (info->model[4] == '2') {
97 			type = GW552x;
98 			break;
99 		} else if (info->model[4] == '3') {
100 			type = GW553x;
101 			break;
102 		}
103 		break;
104 	case '6':
105 		if (info->model[4] == '0')
106 			type = GW560x;
107 		break;
108 	case '9':
109 		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')
114 			type = GW5903;
115 		else if (info->model[4] == '0' && info->model[5] == '4')
116 			type = GW5904;
117 		else if (info->model[4] == '0' && info->model[5] == '5')
118 			type = GW5905;
119 		else if (info->model[4] == '0' && info->model[5] == '6')
120 			type = GW5906;
121 		else if (info->model[4] == '0' && info->model[5] == '7')
122 			type = GW5907;
123 		else if (info->model[4] == '0' && info->model[5] == '8')
124 			type = GW5908;
125 		else if (info->model[4] == '0' && info->model[5] == '9')
126 			type = GW5909;
127 		break;
128 	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;
133 	}
134 	return type;
135 }
136 
137 /* list of config bits that the bootloader will remove from dtb if not set */
138 struct ventana_eeprom_config econfig[] = {
139 	{ "eth0", "ethernet0", EECONFIG_ETH0 },
140 	{ "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 },
146 	{ /* Sentinel */ }
147 };
148 
149 #if defined(CONFIG_CMD_EECONFIG) && !defined(CONFIG_SPL_BUILD)
get_config(const char * name)150 static 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 
162 static u8 econfig_bytes[sizeof(ventana_info.config)];
163 static int econfig_init = -1;
164 
do_econfig(struct cmd_tbl * cmdtp,int flag,int argc,char * const argv[])165 static int do_econfig(struct cmd_tbl *cmdtp, int flag, int argc,
166 		      char *const argv[])
167 {
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 
258 U_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 */
268