blob: 7715ddf307a4458c2f2baf06c4009452965bc527 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Heiko Schocher499c4982013-08-19 16:39:01 +02002/*
3 *
4 * Read FactorySet information from EEPROM into global structure.
5 * (C) Copyright 2013 Siemens Schweiz AG
Heiko Schocher499c4982013-08-19 16:39:01 +02006 */
7
8#if !defined(CONFIG_SPL_BUILD)
9
10#include <common.h>
Simon Glass5e6201b2019-08-01 09:46:51 -060011#include <env.h>
Anatolij Gustschind0d75f52019-07-10 19:44:15 +020012#include <dm.h>
Tom Rinifb768072019-08-12 18:47:53 -040013#include <env_internal.h>
Heiko Schocher499c4982013-08-19 16:39:01 +020014#include <i2c.h>
15#include <asm/io.h>
16#include <asm/arch/cpu.h>
17#include <asm/arch/sys_proto.h>
18#include <asm/unaligned.h>
19#include <net.h>
Samuel Egli8069bfe2013-11-04 14:05:03 +010020#include <errno.h>
21#include <g_dnl.h>
Heiko Schocher499c4982013-08-19 16:39:01 +020022#include "factoryset.h"
23
24#define EEPR_PG_SZ 0x80
25#define EEPROM_FATORYSET_OFFSET 0x400
26#define OFF_PG EEPROM_FATORYSET_OFFSET/EEPR_PG_SZ
27
28/* Global variable that contains necessary information from FactorySet */
29struct factorysetcontainer factory_dat;
30
31#define fact_get_char(i) *((char *)&eeprom_buf[i])
32
33static int fact_match(unsigned char *eeprom_buf, uchar *s1, int i2)
34{
35 if (s1 == NULL)
36 return -1;
37
38 while (*s1 == fact_get_char(i2++))
39 if (*s1++ == '=')
40 return i2;
41
42 if (*s1 == '\0' && fact_get_char(i2-1) == '=')
43 return i2;
44
45 return -1;
46}
47
48static int get_factory_val(unsigned char *eeprom_buf, int size, uchar *name,
49 uchar *buf, int len)
50{
51 int i, nxt = 0;
52
53 for (i = 0; fact_get_char(i) != '\0'; i = nxt + 1) {
54 int val, n;
55
56 for (nxt = i; fact_get_char(nxt) != '\0'; ++nxt) {
57 if (nxt >= size)
58 return -1;
59 }
60
61 val = fact_match(eeprom_buf, (uchar *)name, i);
62 if (val < 0)
63 continue;
64
65 /* found; copy out */
66 for (n = 0; n < len; ++n, ++buf) {
67 *buf = fact_get_char(val++);
68 if (*buf == '\0')
69 return n;
70 }
71
72 if (n)
73 *--buf = '\0';
74
75 printf("env_buf [%d bytes] too small for value of \"%s\"\n",
76 len, name);
77
78 return n;
79 }
80 return -1;
81}
82
83static
84int get_factory_record_val(unsigned char *eeprom_buf, int size, uchar *record,
85 uchar *name, uchar *buf, int len)
86{
87 int ret = -1;
88 int i, nxt = 0;
89 int c;
90 unsigned char end = 0xff;
Heiko Schocher6596b852014-11-18 11:51:04 +010091 unsigned char tmp;
Heiko Schocher499c4982013-08-19 16:39:01 +020092
93 for (i = 0; fact_get_char(i) != end; i = nxt) {
94 nxt = i + 1;
95 if (fact_get_char(i) == '>') {
96 int pos;
97 int endpos;
98 int z;
Heiko Schocher6596b852014-11-18 11:51:04 +010099 int level = 0;
Heiko Schocher499c4982013-08-19 16:39:01 +0200100
101 c = strncmp((char *)&eeprom_buf[i + 1], (char *)record,
102 strlen((char *)record));
103 if (c == 0) {
104 /* record found */
105 pos = i + strlen((char *)record) + 2;
106 nxt = pos;
107 /* search for "<" */
108 c = -1;
109 for (z = pos; fact_get_char(z) != end; z++) {
Heiko Schocher6596b852014-11-18 11:51:04 +0100110 if (fact_get_char(z) == '<') {
111 if (level == 0) {
112 endpos = z;
113 nxt = endpos;
114 c = 0;
115 break;
116 } else {
117 level--;
118 }
Heiko Schocher499c4982013-08-19 16:39:01 +0200119 }
Heiko Schocher6596b852014-11-18 11:51:04 +0100120 if (fact_get_char(z) == '>')
121 level++;
Heiko Schocher499c4982013-08-19 16:39:01 +0200122 }
Heiko Schocher6596b852014-11-18 11:51:04 +0100123 } else {
124 continue;
Heiko Schocher499c4982013-08-19 16:39:01 +0200125 }
126 if (c == 0) {
127 /* end found -> call get_factory_val */
Heiko Schocher6596b852014-11-18 11:51:04 +0100128 tmp = eeprom_buf[endpos];
Heiko Schocher499c4982013-08-19 16:39:01 +0200129 eeprom_buf[endpos] = end;
130 ret = get_factory_val(&eeprom_buf[pos],
Heiko Schocher6596b852014-11-18 11:51:04 +0100131 endpos - pos, name, buf, len);
Heiko Schocher499c4982013-08-19 16:39:01 +0200132 /* fix buffer */
Heiko Schocher6596b852014-11-18 11:51:04 +0100133 eeprom_buf[endpos] = tmp;
Heiko Schocher499c4982013-08-19 16:39:01 +0200134 debug("%s: %s.%s = %s\n",
135 __func__, record, name, buf);
136 return ret;
137 }
138 }
139 }
140 return ret;
141}
142
143int factoryset_read_eeprom(int i2c_addr)
144{
145 int i, pages = 0, size = 0;
146 unsigned char eeprom_buf[0x3c00], hdr[4], buf[MAX_STRING_LENGTH];
147 unsigned char *cp, *cp1;
Anatolij Gustschind0d75f52019-07-10 19:44:15 +0200148#if CONFIG_IS_ENABLED(DM_I2C)
149 struct udevice *bus, *dev;
150 int ret;
151#endif
Heiko Schocher499c4982013-08-19 16:39:01 +0200152
Marek Vasut7f8d4362018-02-16 16:41:18 +0100153#if defined(CONFIG_DFU_OVER_USB)
Maxime Ripard7f78b9d2017-09-07 08:58:08 +0200154 factory_dat.usb_vendor_id = CONFIG_USB_GADGET_VENDOR_NUM;
155 factory_dat.usb_product_id = CONFIG_USB_GADGET_PRODUCT_NUM;
Heiko Schocher499c4982013-08-19 16:39:01 +0200156#endif
Anatolij Gustschind0d75f52019-07-10 19:44:15 +0200157
158#if CONFIG_IS_ENABLED(DM_I2C)
159 ret = uclass_get_device_by_seq(UCLASS_I2C, EEPROM_I2C_BUS, &bus);
160 if (ret)
161 goto err;
162
163 ret = dm_i2c_probe(bus, i2c_addr, 0, &dev);
164 if (ret)
165 goto err;
166
167 ret = i2c_set_chip_offset_len(dev, 2);
168 if (ret)
169 goto err;
170
171 ret = dm_i2c_read(dev, EEPROM_FATORYSET_OFFSET, hdr, sizeof(hdr));
172 if (ret)
173 goto err;
174#else
Heiko Schocher499c4982013-08-19 16:39:01 +0200175 if (i2c_probe(i2c_addr))
176 goto err;
177
178 if (i2c_read(i2c_addr, EEPROM_FATORYSET_OFFSET, 2, hdr, sizeof(hdr)))
179 goto err;
Anatolij Gustschind0d75f52019-07-10 19:44:15 +0200180#endif
Heiko Schocher499c4982013-08-19 16:39:01 +0200181
182 if ((hdr[0] != 0x99) || (hdr[1] != 0x80)) {
183 printf("FactorySet is not right in eeprom.\n");
184 return 1;
185 }
186
187 /* get FactorySet size */
188 size = (hdr[2] << 8) + hdr[3] + sizeof(hdr);
189 if (size > 0x3bfa)
190 size = 0x3bfa;
191
192 pages = size / EEPR_PG_SZ;
193
194 /*
195 * read the eeprom using i2c
196 * I can not read entire eeprom in once, so separate into several
197 * times. Furthermore, fetch eeprom take longer time, so we fetch
198 * data after every time we got a record from eeprom
199 */
200 debug("Read eeprom page :\n");
Anatolij Gustschind0d75f52019-07-10 19:44:15 +0200201 for (i = 0; i < pages; i++) {
202#if CONFIG_IS_ENABLED(DM_I2C)
203 ret = dm_i2c_read(dev, (OFF_PG + i) * EEPR_PG_SZ,
204 eeprom_buf + (i * EEPR_PG_SZ), EEPR_PG_SZ);
205 if (ret)
206 goto err;
207#else
Heiko Schocher499c4982013-08-19 16:39:01 +0200208 if (i2c_read(i2c_addr, (OFF_PG + i) * EEPR_PG_SZ, 2,
209 eeprom_buf + (i * EEPR_PG_SZ), EEPR_PG_SZ))
210 goto err;
Anatolij Gustschind0d75f52019-07-10 19:44:15 +0200211#endif
212 }
Heiko Schocher499c4982013-08-19 16:39:01 +0200213
Anatolij Gustschind0d75f52019-07-10 19:44:15 +0200214 if (size % EEPR_PG_SZ) {
215#if CONFIG_IS_ENABLED(DM_I2C)
216 ret = dm_i2c_read(dev, (OFF_PG + pages) * EEPR_PG_SZ,
217 eeprom_buf + (pages * EEPR_PG_SZ),
218 size % EEPR_PG_SZ);
219 if (ret)
220 goto err;
221#else
Heiko Schocher499c4982013-08-19 16:39:01 +0200222 if (i2c_read(i2c_addr, (OFF_PG + pages) * EEPR_PG_SZ, 2,
223 eeprom_buf + (pages * EEPR_PG_SZ),
224 (size % EEPR_PG_SZ)))
225 goto err;
Anatolij Gustschind0d75f52019-07-10 19:44:15 +0200226#endif
227 }
Heiko Schocher499c4982013-08-19 16:39:01 +0200228
229 /* we do below just for eeprom align */
230 for (i = 0; i < size; i++)
231 if (eeprom_buf[i] == '\n')
232 eeprom_buf[i] = 0;
233
234 /* skip header */
235 size -= sizeof(hdr);
236 cp = (uchar *)eeprom_buf + sizeof(hdr);
237
238 /* get mac address */
239 get_factory_record_val(cp, size, (uchar *)"ETH1", (uchar *)"mac",
240 buf, MAX_STRING_LENGTH);
241 cp1 = buf;
242 for (i = 0; i < 6; i++) {
243 factory_dat.mac[i] = simple_strtoul((char *)cp1, NULL, 16);
244 cp1 += 3;
245 }
246
Anatolij Gustschin8e9821e2019-07-10 19:44:16 +0200247#if CONFIG_IS_ENABLED(TARGET_GIEDI) || CONFIG_IS_ENABLED(TARGET_DENEB)
248 /* get mac address for WLAN */
249 ret = get_factory_record_val(cp, size, (uchar *)"WLAN1", (uchar *)"mac",
250 buf, MAX_STRING_LENGTH);
251 if (ret > 0) {
252 cp1 = buf;
253 for (i = 0; i < 6; i++) {
254 factory_dat.mac_wlan[i] = simple_strtoul((char *)cp1,
255 NULL, 16);
256 cp1 += 3;
257 }
258 }
259#endif
260
Marek Vasut7f8d4362018-02-16 16:41:18 +0100261#if defined(CONFIG_DFU_OVER_USB)
Heiko Schocher499c4982013-08-19 16:39:01 +0200262 /* read vid and pid for dfu mode */
263 if (0 <= get_factory_record_val(cp, size, (uchar *)"USBD1",
264 (uchar *)"vid", buf,
265 MAX_STRING_LENGTH)) {
266 factory_dat.usb_vendor_id = simple_strtoul((char *)buf,
267 NULL, 16);
268 }
269
270 if (0 <= get_factory_record_val(cp, size, (uchar *)"USBD1",
271 (uchar *)"pid", buf,
272 MAX_STRING_LENGTH)) {
273 factory_dat.usb_product_id = simple_strtoul((char *)buf,
274 NULL, 16);
275 }
276 printf("DFU USB: VID = 0x%4x, PID = 0x%4x\n", factory_dat.usb_vendor_id,
277 factory_dat.usb_product_id);
278#endif
Heiko Schocher499c4982013-08-19 16:39:01 +0200279#if defined(CONFIG_VIDEO)
280 if (0 <= get_factory_record_val(cp, size, (uchar *)"DISP1",
281 (uchar *)"name", factory_dat.disp_name,
282 MAX_STRING_LENGTH)) {
283 debug("display name: %s\n", factory_dat.disp_name);
284 }
Heiko Schocher499c4982013-08-19 16:39:01 +0200285#endif
Samuel Egli8069bfe2013-11-04 14:05:03 +0100286 if (0 <= get_factory_record_val(cp, size, (uchar *)"DEV",
287 (uchar *)"num", factory_dat.serial,
288 MAX_STRING_LENGTH)) {
289 debug("serial number: %s\n", factory_dat.serial);
290 }
291 if (0 <= get_factory_record_val(cp, size, (uchar *)"DEV",
292 (uchar *)"ver", buf,
293 MAX_STRING_LENGTH)) {
294 factory_dat.version = simple_strtoul((char *)buf,
295 NULL, 16);
296 debug("version number: %d\n", factory_dat.version);
297 }
Heiko Schocherfaf2dc62014-11-18 11:51:06 +0100298 /* Get ASN from factory set if available */
299 if (0 <= get_factory_record_val(cp, size, (uchar *)"DEV",
300 (uchar *)"id", factory_dat.asn,
301 MAX_STRING_LENGTH)) {
302 debug("factoryset asn: %s\n", factory_dat.asn);
303 } else {
304 factory_dat.asn[0] = 0;
305 }
Heiko Schocher7b905652014-11-18 11:51:05 +0100306 /* Get COMP/ver from factory set if available */
307 if (0 <= get_factory_record_val(cp, size, (uchar *)"COMP",
308 (uchar *)"ver",
309 factory_dat.comp_version,
310 MAX_STRING_LENGTH)) {
311 debug("factoryset COMP/ver: %s\n", factory_dat.comp_version);
312 } else {
313 strcpy((char *)factory_dat.comp_version, "1.0");
314 }
315
Heiko Schocher499c4982013-08-19 16:39:01 +0200316 return 0;
317
318err:
319 printf("Could not read the EEPROM; something fundamentally wrong on the I2C bus.\n");
320 return 1;
321}
322
Anatolij Gustschin8e9821e2019-07-10 19:44:16 +0200323static int get_mac_from_efuse(uint8_t mac[6])
324{
325#ifdef CONFIG_AM33XX
326 struct ctrl_dev *cdev = (struct ctrl_dev *)CTRL_DEVICE_BASE;
327 uint32_t mac_hi, mac_lo;
328
329 mac_lo = readl(&cdev->macid0l);
330 mac_hi = readl(&cdev->macid0h);
331
332 mac[0] = mac_hi & 0xFF;
333 mac[1] = (mac_hi & 0xFF00) >> 8;
334 mac[2] = (mac_hi & 0xFF0000) >> 16;
335 mac[3] = (mac_hi & 0xFF000000) >> 24;
336 mac[4] = mac_lo & 0xFF;
337 mac[5] = (mac_lo & 0xFF00) >> 8;
338#else
339 /* unhandled */
340 memset(mac, 0, 6);
341#endif
342 if (!is_valid_ethaddr(mac)) {
343 puts("Warning: ethaddr not set by FactorySet or E-fuse. ");
344 puts("Set <ethaddr> variable to overcome this.\n");
345 return -1;
346 }
347 return 0;
348}
Heiko Schocher499c4982013-08-19 16:39:01 +0200349
Simon Glass6a38e412017-08-03 12:22:09 -0600350static int factoryset_mac_env_set(void)
Heiko Schocher499c4982013-08-19 16:39:01 +0200351{
352 uint8_t mac_addr[6];
353
Anatolij Gustschin8e9821e2019-07-10 19:44:16 +0200354 /* Set mac from factoryset or try reading E-fuse */
Heiko Schocher499c4982013-08-19 16:39:01 +0200355 debug("FactorySet: Set mac address\n");
Joe Hershberger8ecdbed2015-04-08 01:41:04 -0500356 if (is_valid_ethaddr(factory_dat.mac)) {
Heiko Schocher499c4982013-08-19 16:39:01 +0200357 memcpy(mac_addr, factory_dat.mac, 6);
358 } else {
Heiko Schocher499c4982013-08-19 16:39:01 +0200359 debug("Warning: FactorySet: <ethaddr> not set. Fallback to E-fuse\n");
Anatolij Gustschin8e9821e2019-07-10 19:44:16 +0200360 if (get_mac_from_efuse(mac_addr) < 0)
Heiko Schocher499c4982013-08-19 16:39:01 +0200361 return -1;
Heiko Schocher499c4982013-08-19 16:39:01 +0200362 }
363
Simon Glass8551d552017-08-03 12:22:11 -0600364 eth_env_set_enetaddr("ethaddr", mac_addr);
Anatolij Gustschin8e9821e2019-07-10 19:44:16 +0200365
366#if CONFIG_IS_ENABLED(TARGET_GIEDI) || CONFIG_IS_ENABLED(TARGET_DENEB)
367 eth_env_set_enetaddr("eth1addr", mac_addr);
368
369 /* wlan mac */
370 if (is_valid_ethaddr(factory_dat.mac_wlan))
371 eth_env_set_enetaddr("eth2addr", factory_dat.mac_wlan);
372#endif
Heiko Schocher499c4982013-08-19 16:39:01 +0200373 return 0;
374}
375
Anatolij Gustschin8e9821e2019-07-10 19:44:16 +0200376static void factoryset_dtb_env_set(void)
377{
378 /* Set ASN in environment*/
379 if (factory_dat.asn[0] != 0) {
380 env_set("dtb_name", (char *)factory_dat.asn);
381 } else {
382 /* dtb suffix gets added in load script */
383 env_set("dtb_name", "default");
384 }
385}
386
Simon Glass6a38e412017-08-03 12:22:09 -0600387int factoryset_env_set(void)
Heiko Schocher499c4982013-08-19 16:39:01 +0200388{
389 int ret = 0;
390
Anatolij Gustschin8e9821e2019-07-10 19:44:16 +0200391 factoryset_dtb_env_set();
392
Simon Glass6a38e412017-08-03 12:22:09 -0600393 if (factoryset_mac_env_set() < 0)
Heiko Schocher499c4982013-08-19 16:39:01 +0200394 ret = -1;
395
396 return ret;
397}
398
Lukasz Majewski44b5b382013-10-08 14:30:41 +0200399int g_dnl_bind_fixup(struct usb_device_descriptor *dev, const char *name)
Heiko Schocher499c4982013-08-19 16:39:01 +0200400{
401 put_unaligned(factory_dat.usb_vendor_id, &dev->idVendor);
402 put_unaligned(factory_dat.usb_product_id, &dev->idProduct);
Samuel Egli8069bfe2013-11-04 14:05:03 +0100403 g_dnl_set_serialnumber((char *)factory_dat.serial);
404
Heiko Schocher499c4982013-08-19 16:39:01 +0200405 return 0;
406}
Samuel Egli8069bfe2013-11-04 14:05:03 +0100407
408int g_dnl_get_board_bcd_device_number(int gcnum)
409{
410 return factory_dat.version;
411}
Heiko Schocher499c4982013-08-19 16:39:01 +0200412#endif /* defined(CONFIG_SPL_BUILD) */