1 /*
2  *
3  * Read FactorySet information from EEPROM into global structure.
4  * (C) Copyright 2013 Siemens Schweiz AG
5  *
6  * SPDX-License-Identifier:	GPL-2.0+
7  */
8 
9 #if !defined(CONFIG_SPL_BUILD)
10 
11 #include <common.h>
12 #include <i2c.h>
13 #include <asm/io.h>
14 #include <asm/arch/cpu.h>
15 #include <asm/arch/sys_proto.h>
16 #include <asm/unaligned.h>
17 #include <net.h>
18 #include <usbdescriptors.h>
19 #include "factoryset.h"
20 
21 #define EEPR_PG_SZ		0x80
22 #define EEPROM_FATORYSET_OFFSET	0x400
23 #define OFF_PG            EEPROM_FATORYSET_OFFSET/EEPR_PG_SZ
24 
25 /* Global variable that contains necessary information from FactorySet */
26 struct factorysetcontainer factory_dat;
27 
28 #define fact_get_char(i) *((char *)&eeprom_buf[i])
29 
30 static int fact_match(unsigned char *eeprom_buf, uchar *s1, int i2)
31 {
32 	if (s1 == NULL)
33 		return -1;
34 
35 	while (*s1 == fact_get_char(i2++))
36 		if (*s1++ == '=')
37 			return i2;
38 
39 	if (*s1 == '\0' && fact_get_char(i2-1) == '=')
40 		return i2;
41 
42 	return -1;
43 }
44 
45 static int get_factory_val(unsigned char *eeprom_buf, int size, uchar *name,
46 			uchar *buf, int len)
47 {
48 	int i, nxt = 0;
49 
50 	for (i = 0; fact_get_char(i) != '\0'; i = nxt + 1) {
51 		int val, n;
52 
53 		for (nxt = i; fact_get_char(nxt) != '\0'; ++nxt) {
54 			if (nxt >= size)
55 				return -1;
56 		}
57 
58 		val = fact_match(eeprom_buf, (uchar *)name, i);
59 		if (val < 0)
60 			continue;
61 
62 		/* found; copy out */
63 		for (n = 0; n < len; ++n, ++buf) {
64 			*buf = fact_get_char(val++);
65 			if (*buf == '\0')
66 				return n;
67 		}
68 
69 		if (n)
70 			*--buf = '\0';
71 
72 		printf("env_buf [%d bytes] too small for value of \"%s\"\n",
73 		       len, name);
74 
75 		return n;
76 	}
77 	return -1;
78 }
79 
80 static
81 int get_factory_record_val(unsigned char *eeprom_buf, int size,	uchar *record,
82 	uchar *name, uchar *buf, int len)
83 {
84 	int ret = -1;
85 	int i, nxt = 0;
86 	int c;
87 	unsigned char end = 0xff;
88 
89 	for (i = 0; fact_get_char(i) != end; i = nxt) {
90 		nxt = i + 1;
91 		if (fact_get_char(i) == '>') {
92 			int pos;
93 			int endpos;
94 			int z;
95 
96 			c = strncmp((char *)&eeprom_buf[i + 1], (char *)record,
97 				    strlen((char *)record));
98 			if (c == 0) {
99 				/* record found */
100 				pos = i + strlen((char *)record) + 2;
101 				nxt = pos;
102 				/* search for "<" */
103 				c = -1;
104 				for (z = pos; fact_get_char(z) != end; z++) {
105 					if ((fact_get_char(z) == '<')  ||
106 					    (fact_get_char(z) == '>')) {
107 						endpos = z;
108 						nxt = endpos;
109 						c = 0;
110 						break;
111 					}
112 				}
113 			}
114 			if (c == 0) {
115 				/* end found -> call get_factory_val */
116 				eeprom_buf[endpos] = end;
117 				ret = get_factory_val(&eeprom_buf[pos],
118 					size - pos, name, buf, len);
119 				/* fix buffer */
120 				eeprom_buf[endpos] = '<';
121 				debug("%s: %s.%s = %s\n",
122 				      __func__, record, name, buf);
123 				return ret;
124 			}
125 		}
126 	}
127 	return ret;
128 }
129 
130 int factoryset_read_eeprom(int i2c_addr)
131 {
132 	int i, pages = 0, size = 0;
133 	unsigned char eeprom_buf[0x3c00], hdr[4], buf[MAX_STRING_LENGTH];
134 	unsigned char *cp, *cp1;
135 
136 #if defined(CONFIG_DFU_FUNCTION)
137 	factory_dat.usb_vendor_id = CONFIG_G_DNL_VENDOR_NUM;
138 	factory_dat.usb_product_id = CONFIG_G_DNL_PRODUCT_NUM;
139 #endif
140 	if (i2c_probe(i2c_addr))
141 		goto err;
142 
143 	if (i2c_read(i2c_addr, EEPROM_FATORYSET_OFFSET, 2, hdr, sizeof(hdr)))
144 		goto err;
145 
146 	if ((hdr[0] != 0x99) || (hdr[1] != 0x80)) {
147 		printf("FactorySet is not right in eeprom.\n");
148 		return 1;
149 	}
150 
151 	/* get FactorySet size */
152 	size = (hdr[2] << 8) + hdr[3] + sizeof(hdr);
153 	if (size > 0x3bfa)
154 		size = 0x3bfa;
155 
156 	pages = size / EEPR_PG_SZ;
157 
158 	/*
159 	 * read the eeprom using i2c
160 	 * I can not read entire eeprom in once, so separate into several
161 	 * times. Furthermore, fetch eeprom take longer time, so we fetch
162 	 * data after every time we got a record from eeprom
163 	 */
164 	debug("Read eeprom page :\n");
165 	for (i = 0; i < pages; i++)
166 		if (i2c_read(i2c_addr, (OFF_PG + i) * EEPR_PG_SZ, 2,
167 			     eeprom_buf + (i * EEPR_PG_SZ), EEPR_PG_SZ))
168 			goto err;
169 
170 	if (size % EEPR_PG_SZ)
171 		if (i2c_read(i2c_addr, (OFF_PG + pages) * EEPR_PG_SZ, 2,
172 			     eeprom_buf + (pages * EEPR_PG_SZ),
173 			     (size % EEPR_PG_SZ)))
174 			goto err;
175 
176 	/* we do below just for eeprom align */
177 	for (i = 0; i < size; i++)
178 		if (eeprom_buf[i] == '\n')
179 			eeprom_buf[i] = 0;
180 
181 	/* skip header */
182 	size -= sizeof(hdr);
183 	cp = (uchar *)eeprom_buf + sizeof(hdr);
184 
185 	/* get mac address */
186 	get_factory_record_val(cp, size, (uchar *)"ETH1", (uchar *)"mac",
187 			       buf, MAX_STRING_LENGTH);
188 	cp1 = buf;
189 	for (i = 0; i < 6; i++) {
190 		factory_dat.mac[i] = simple_strtoul((char *)cp1, NULL, 16);
191 		cp1 += 3;
192 	}
193 
194 #if defined(CONFIG_DFU_FUNCTION)
195 	/* read vid and pid for dfu mode */
196 	if (0 <= get_factory_record_val(cp, size, (uchar *)"USBD1",
197 					(uchar *)"vid", buf,
198 					MAX_STRING_LENGTH)) {
199 		factory_dat.usb_vendor_id = simple_strtoul((char *)buf,
200 							   NULL, 16);
201 	}
202 
203 	if (0 <= get_factory_record_val(cp, size, (uchar *)"USBD1",
204 					(uchar *)"pid", buf,
205 					MAX_STRING_LENGTH)) {
206 		factory_dat.usb_product_id = simple_strtoul((char *)buf,
207 							    NULL, 16);
208 	}
209 	printf("DFU USB: VID = 0x%4x, PID = 0x%4x\n", factory_dat.usb_vendor_id,
210 	       factory_dat.usb_product_id);
211 #endif
212 	if (0 <= get_factory_record_val(cp, size, (uchar *)"DEV",
213 					(uchar *)"id", buf,
214 					MAX_STRING_LENGTH)) {
215 		if (strncmp((const char *)buf, "PXM50", 5) == 0)
216 			factory_dat.pxm50 = 1;
217 		else
218 			factory_dat.pxm50 = 0;
219 	}
220 	debug("PXM50: %d\n", factory_dat.pxm50);
221 #if defined(CONFIG_VIDEO)
222 	if (0 <= get_factory_record_val(cp, size, (uchar *)"DISP1",
223 					(uchar *)"name", factory_dat.disp_name,
224 					MAX_STRING_LENGTH)) {
225 		debug("display name: %s\n", factory_dat.disp_name);
226 	}
227 
228 #endif
229 	return 0;
230 
231 err:
232 	printf("Could not read the EEPROM; something fundamentally wrong on the I2C bus.\n");
233 	return 1;
234 }
235 
236 static struct ctrl_dev *cdev = (struct ctrl_dev *)CTRL_DEVICE_BASE;
237 
238 static int factoryset_mac_setenv(void)
239 {
240 	uint8_t mac_addr[6];
241 
242 	debug("FactorySet: Set mac address\n");
243 	if (is_valid_ether_addr(factory_dat.mac)) {
244 		memcpy(mac_addr, factory_dat.mac, 6);
245 	} else {
246 		uint32_t mac_hi, mac_lo;
247 
248 		debug("Warning: FactorySet: <ethaddr> not set. Fallback to E-fuse\n");
249 		mac_lo = readl(&cdev->macid0l);
250 		mac_hi = readl(&cdev->macid0h);
251 
252 		mac_addr[0] = mac_hi & 0xFF;
253 		mac_addr[1] = (mac_hi & 0xFF00) >> 8;
254 		mac_addr[2] = (mac_hi & 0xFF0000) >> 16;
255 		mac_addr[3] = (mac_hi & 0xFF000000) >> 24;
256 		mac_addr[4] = mac_lo & 0xFF;
257 		mac_addr[5] = (mac_lo & 0xFF00) >> 8;
258 		if (!is_valid_ether_addr(mac_addr)) {
259 			printf("Warning: ethaddr not set by FactorySet or E-fuse. Set <ethaddr> variable to overcome this.\n");
260 			return -1;
261 		}
262 	}
263 
264 	eth_setenv_enetaddr("ethaddr", mac_addr);
265 	return 0;
266 }
267 
268 int factoryset_setenv(void)
269 {
270 	int ret = 0;
271 
272 	if (factoryset_mac_setenv() < 0)
273 		ret = -1;
274 
275 	return ret;
276 }
277 
278 int g_dnl_bind_fixup(struct usb_device_descriptor *dev, const char *name)
279 {
280 	put_unaligned(factory_dat.usb_vendor_id, &dev->idVendor);
281 	put_unaligned(factory_dat.usb_product_id, &dev->idProduct);
282 	return 0;
283 }
284 #endif /* defined(CONFIG_SPL_BUILD) */
285