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