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 	default:
84 		printf("EEPROM: Unknown model in EEPROM: %s\n", info->model);
85 		type = GW_UNKNOWN;
86 		break;
87 	}
88 	return type;
89 }
90 
91 /* list of config bits that the bootloader will remove from dtb if not set */
92 struct ventana_eeprom_config econfig[] = {
93 	{ "eth0", "ethernet0", EECONFIG_ETH0 },
94 	{ "eth1", "ethernet1", EECONFIG_ETH1 },
95 	{ "sata", "ahci0", EECONFIG_SATA },
96 	{ "pcie", NULL, EECONFIG_PCIE},
97 	{ "lvds0", NULL, EECONFIG_LVDS0 },
98 	{ "lvds1", NULL, EECONFIG_LVDS1 },
99 	{ "usb0", NULL, EECONFIG_USB0 },
100 	{ "usb1", NULL, EECONFIG_USB1 },
101 	{ "mmc0", NULL, EECONFIG_SD0 },
102 	{ "mmc1", NULL, EECONFIG_SD1 },
103 	{ "mmc2", NULL, EECONFIG_SD2 },
104 	{ "mmc3", NULL, EECONFIG_SD3 },
105 	{ "uart0", NULL, EECONFIG_UART0 },
106 	{ "uart1", NULL, EECONFIG_UART1 },
107 	{ "uart2", NULL, EECONFIG_UART2 },
108 	{ "uart3", NULL, EECONFIG_UART3 },
109 	{ "uart4", NULL, EECONFIG_UART4 },
110 	{ "ipu0", NULL, EECONFIG_IPU0 },
111 	{ "ipu1", NULL, EECONFIG_IPU1 },
112 	{ "can0", NULL, EECONFIG_FLEXCAN },
113 	{ "i2c0", NULL, EECONFIG_I2C0 },
114 	{ "i2c1", NULL, EECONFIG_I2C1 },
115 	{ "i2c2", NULL, EECONFIG_I2C2 },
116 	{ "vpu", NULL, EECONFIG_VPU },
117 	{ "csi0", NULL, EECONFIG_CSI0 },
118 	{ "csi1", NULL, EECONFIG_CSI1 },
119 	{ "spi0", NULL, EECONFIG_ESPCI0 },
120 	{ "spi1", NULL, EECONFIG_ESPCI1 },
121 	{ "spi2", NULL, EECONFIG_ESPCI2 },
122 	{ "spi3", NULL, EECONFIG_ESPCI3 },
123 	{ "spi4", NULL, EECONFIG_ESPCI4 },
124 	{ "spi5", NULL, EECONFIG_ESPCI5 },
125 	{ "gps", "pps", EECONFIG_GPS },
126 	{ "hdmi_in", NULL, EECONFIG_HDMI_IN },
127 	{ "hdmi_out", NULL, EECONFIG_HDMI_OUT },
128 	{ "cvbs_in", NULL, EECONFIG_VID_IN },
129 	{ "cvbs_out", NULL, EECONFIG_VID_OUT },
130 	{ "nand", NULL, EECONFIG_NAND },
131 	{ /* Sentinel */ }
132 };
133 
134 #ifdef CONFIG_CMD_EECONFIG
135 static struct ventana_eeprom_config *get_config(const char *name)
136 {
137 	struct ventana_eeprom_config *cfg = econfig;
138 
139 	while (cfg->name) {
140 		if (0 == strcmp(name, cfg->name))
141 			return cfg;
142 		cfg++;
143 	}
144 	return NULL;
145 }
146 
147 static u8 econfig_bytes[sizeof(ventana_info.config)];
148 static int econfig_init = -1;
149 
150 int do_econfig(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
151 {
152 	struct ventana_eeprom_config *cfg;
153 	struct ventana_board_info *info = &ventana_info;
154 	int i;
155 
156 	if (argc < 2)
157 		return CMD_RET_USAGE;
158 
159 	/* initialize */
160 	if (econfig_init != 1) {
161 		memcpy(econfig_bytes, info->config, sizeof(econfig_bytes));
162 		econfig_init = 1;
163 	}
164 
165 	/* list configs */
166 	if ((strncmp(argv[1], "list", 4) == 0)) {
167 		cfg = econfig;
168 		while (cfg->name) {
169 			printf("%s: %d\n", cfg->name,
170 			       test_bit(cfg->bit, econfig_bytes) ?  1 : 0);
171 			cfg++;
172 		}
173 	}
174 
175 	/* save */
176 	else if ((strncmp(argv[1], "save", 4) == 0)) {
177 		unsigned char *buf = (unsigned char *)info;
178 		int chksum;
179 
180 		/* calculate new checksum */
181 		memcpy(info->config, econfig_bytes, sizeof(econfig_bytes));
182 		for (chksum = 0, i = 0; i < sizeof(*info)-2; i++)
183 			chksum += buf[i];
184 		debug("old chksum:0x%04x\n",
185 		      (info->chksum[0] << 8) | info->chksum[1]);
186 		debug("new chksum:0x%04x\n", chksum);
187 		info->chksum[0] = chksum >> 8;
188 		info->chksum[1] = chksum & 0xff;
189 
190 		/* write new config data */
191 		if (gsc_i2c_write(GSC_EEPROM_ADDR, info->config - (u8 *)info,
192 				  1, econfig_bytes, sizeof(econfig_bytes))) {
193 			printf("EEPROM: Failed updating config\n");
194 			return CMD_RET_FAILURE;
195 		}
196 
197 		/* write new config data */
198 		if (gsc_i2c_write(GSC_EEPROM_ADDR, info->chksum - (u8 *)info,
199 				  1, info->chksum, 2)) {
200 			printf("EEPROM: Failed updating checksum\n");
201 			return CMD_RET_FAILURE;
202 		}
203 
204 		printf("Config saved to EEPROM\n");
205 	}
206 
207 	/* get config */
208 	else if (argc == 2) {
209 		cfg = get_config(argv[1]);
210 		if (cfg) {
211 			printf("%s: %d\n", cfg->name,
212 			       test_bit(cfg->bit, econfig_bytes) ? 1 : 0);
213 		} else {
214 			printf("invalid config: %s\n", argv[1]);
215 			return CMD_RET_FAILURE;
216 		}
217 	}
218 
219 	/* set config */
220 	else if (argc == 3) {
221 		cfg = get_config(argv[1]);
222 		if (cfg) {
223 			if (simple_strtol(argv[2], NULL, 10)) {
224 				test_and_set_bit(cfg->bit, econfig_bytes);
225 				printf("Enabled %s\n", cfg->name);
226 			} else {
227 				test_and_clear_bit(cfg->bit, econfig_bytes);
228 				printf("Disabled %s\n", cfg->name);
229 			}
230 		} else {
231 			printf("invalid config: %s\n", argv[1]);
232 			return CMD_RET_FAILURE;
233 		}
234 	}
235 
236 	else
237 		return CMD_RET_USAGE;
238 
239 	return CMD_RET_SUCCESS;
240 }
241 
242 U_BOOT_CMD(
243 	econfig, 3, 0, do_econfig,
244 	"EEPROM configuration",
245 	"list - list config\n"
246 	"save - save config to EEPROM\n"
247 	"<name> - get config 'name'\n"
248 	"<name> [0|1] - set config 'name' to value\n"
249 );
250 
251 #endif /* CONFIG_CMD_EECONFIG */
252