1 /*
2  * Copyright (C) 2014 Gateworks Corporation
3  * Author: Tim Harvey <tharvey@gateworks.com>
4  *
5  * SPDX-License-Identifier: GPL-2.0+
6  */
7 
8 #include <common.h>
9 #include <errno.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
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 		return GW_UNKNOWN;
51 	}
52 
53 	/* validate checksum */
54 	for (chksum = 0, i = 0; i < sizeof(*info)-2; i++)
55 		chksum += buf[i];
56 	if ((info->chksum[0] != chksum>>8) ||
57 	    (info->chksum[1] != (chksum&0xff))) {
58 		puts("EEPROM: Failed EEPROM checksum\n");
59 		return GW_UNKNOWN;
60 	}
61 
62 	/* original GW5400-A prototype */
63 	baseboard = info->model[3];
64 	if (strncasecmp((const char *)info->model, "GW5400-A", 8) == 0)
65 		baseboard = '0';
66 
67 	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;
83 	case '5':
84 		if (info->model[4] == '1') {
85 			type = GW551x;
86 			break;
87 		} else if (info->model[4] == '2') {
88 			type = GW552x;
89 			break;
90 		} else if (info->model[4] == '3') {
91 			type = GW553x;
92 			break;
93 		}
94 		/* fall through */
95 	default:
96 		printf("EEPROM: Unknown model in EEPROM: %s\n", info->model);
97 		type = GW_UNKNOWN;
98 		break;
99 	}
100 	return type;
101 }
102 
103 /* list of config bits that the bootloader will remove from dtb if not set */
104 struct ventana_eeprom_config econfig[] = {
105 	{ "eth0", "ethernet0", EECONFIG_ETH0 },
106 	{ "usb0", NULL, EECONFIG_USB0 },
107 	{ "usb1", NULL, EECONFIG_USB1 },
108 	{ "mmc0", NULL, EECONFIG_SD0 },
109 	{ "mmc1", NULL, EECONFIG_SD1 },
110 	{ "mmc2", NULL, EECONFIG_SD2 },
111 	{ "mmc3", NULL, EECONFIG_SD3 },
112 	{ /* Sentinel */ }
113 };
114 
115 #ifdef CONFIG_CMD_EECONFIG
116 static struct ventana_eeprom_config *get_config(const char *name)
117 {
118 	struct ventana_eeprom_config *cfg = econfig;
119 
120 	while (cfg->name) {
121 		if (0 == strcmp(name, cfg->name))
122 			return cfg;
123 		cfg++;
124 	}
125 	return NULL;
126 }
127 
128 static u8 econfig_bytes[sizeof(ventana_info.config)];
129 static int econfig_init = -1;
130 
131 int do_econfig(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
132 {
133 	struct ventana_eeprom_config *cfg;
134 	struct ventana_board_info *info = &ventana_info;
135 	int i;
136 
137 	if (argc < 2)
138 		return CMD_RET_USAGE;
139 
140 	/* initialize */
141 	if (econfig_init != 1) {
142 		memcpy(econfig_bytes, info->config, sizeof(econfig_bytes));
143 		econfig_init = 1;
144 	}
145 
146 	/* list configs */
147 	if ((strncmp(argv[1], "list", 4) == 0)) {
148 		cfg = econfig;
149 		while (cfg->name) {
150 			printf("%s: %d\n", cfg->name,
151 			       test_bit(cfg->bit, econfig_bytes) ?  1 : 0);
152 			cfg++;
153 		}
154 	}
155 
156 	/* save */
157 	else if ((strncmp(argv[1], "save", 4) == 0)) {
158 		unsigned char *buf = (unsigned char *)info;
159 		int chksum;
160 
161 		/* calculate new checksum */
162 		memcpy(info->config, econfig_bytes, sizeof(econfig_bytes));
163 		for (chksum = 0, i = 0; i < sizeof(*info)-2; i++)
164 			chksum += buf[i];
165 		debug("old chksum:0x%04x\n",
166 		      (info->chksum[0] << 8) | info->chksum[1]);
167 		debug("new chksum:0x%04x\n", chksum);
168 		info->chksum[0] = chksum >> 8;
169 		info->chksum[1] = chksum & 0xff;
170 
171 		/* write new config data */
172 		if (gsc_i2c_write(GSC_EEPROM_ADDR, info->config - (u8 *)info,
173 				  1, econfig_bytes, sizeof(econfig_bytes))) {
174 			printf("EEPROM: Failed updating config\n");
175 			return CMD_RET_FAILURE;
176 		}
177 
178 		/* write new config data */
179 		if (gsc_i2c_write(GSC_EEPROM_ADDR, info->chksum - (u8 *)info,
180 				  1, info->chksum, 2)) {
181 			printf("EEPROM: Failed updating checksum\n");
182 			return CMD_RET_FAILURE;
183 		}
184 
185 		printf("Config saved to EEPROM\n");
186 	}
187 
188 	/* get config */
189 	else if (argc == 2) {
190 		cfg = get_config(argv[1]);
191 		if (cfg) {
192 			printf("%s: %d\n", cfg->name,
193 			       test_bit(cfg->bit, econfig_bytes) ? 1 : 0);
194 		} else {
195 			printf("invalid config: %s\n", argv[1]);
196 			return CMD_RET_FAILURE;
197 		}
198 	}
199 
200 	/* set config */
201 	else if (argc == 3) {
202 		cfg = get_config(argv[1]);
203 		if (cfg) {
204 			if (simple_strtol(argv[2], NULL, 10)) {
205 				test_and_set_bit(cfg->bit, econfig_bytes);
206 				printf("Enabled %s\n", cfg->name);
207 			} else {
208 				test_and_clear_bit(cfg->bit, econfig_bytes);
209 				printf("Disabled %s\n", cfg->name);
210 			}
211 		} else {
212 			printf("invalid config: %s\n", argv[1]);
213 			return CMD_RET_FAILURE;
214 		}
215 	}
216 
217 	else
218 		return CMD_RET_USAGE;
219 
220 	return CMD_RET_SUCCESS;
221 }
222 
223 U_BOOT_CMD(
224 	econfig, 3, 0, do_econfig,
225 	"EEPROM configuration",
226 	"list - list config\n"
227 	"save - save config to EEPROM\n"
228 	"<name> - get config 'name'\n"
229 	"<name> [0|1] - set config 'name' to value\n"
230 );
231 
232 #endif /* CONFIG_CMD_EECONFIG */
233