blob: b20af95d13d9f9cc54db5a1b6e09531bba782d81 [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
Simon Glass5e6201b2019-08-01 09:46:51 -060010#include <env.h>
Enrico Letofce91792024-01-24 15:43:54 +010011#include <g_dnl.h>
12#include <net.h>
Heiko Schocher499c4982013-08-19 16:39:01 +020013#include <asm/io.h>
Anatolij Gustschin49234a32020-01-07 16:37:42 +010014#if !CONFIG_IS_ENABLED(TARGET_GIEDI) && !CONFIG_IS_ENABLED(TARGET_DENEB)
Heiko Schocher499c4982013-08-19 16:39:01 +020015#include <asm/arch/cpu.h>
Anatolij Gustschin49234a32020-01-07 16:37:42 +010016#endif
Enrico Leto096bfdc2024-01-24 15:43:49 +010017#include "eeprom.h"
Heiko Schocher499c4982013-08-19 16:39:01 +020018#include "factoryset.h"
19
Enrico Leto096bfdc2024-01-24 15:43:49 +010020#define EEPR_PG_SZ 0x80
21#define OFF_PG (SIEMENS_EE_ADDR_FACTORYSET / EEPR_PG_SZ)
Heiko Schocher499c4982013-08-19 16:39:01 +020022
23/* Global variable that contains necessary information from FactorySet */
24struct factorysetcontainer factory_dat;
25
26#define fact_get_char(i) *((char *)&eeprom_buf[i])
27
28static int fact_match(unsigned char *eeprom_buf, uchar *s1, int i2)
29{
30 if (s1 == NULL)
31 return -1;
32
33 while (*s1 == fact_get_char(i2++))
34 if (*s1++ == '=')
35 return i2;
36
37 if (*s1 == '\0' && fact_get_char(i2-1) == '=')
38 return i2;
39
40 return -1;
41}
42
43static int get_factory_val(unsigned char *eeprom_buf, int size, uchar *name,
44 uchar *buf, int len)
45{
46 int i, nxt = 0;
47
48 for (i = 0; fact_get_char(i) != '\0'; i = nxt + 1) {
49 int val, n;
50
51 for (nxt = i; fact_get_char(nxt) != '\0'; ++nxt) {
52 if (nxt >= size)
53 return -1;
54 }
55
56 val = fact_match(eeprom_buf, (uchar *)name, i);
57 if (val < 0)
58 continue;
59
60 /* found; copy out */
61 for (n = 0; n < len; ++n, ++buf) {
62 *buf = fact_get_char(val++);
63 if (*buf == '\0')
64 return n;
65 }
66
67 if (n)
68 *--buf = '\0';
69
70 printf("env_buf [%d bytes] too small for value of \"%s\"\n",
71 len, name);
72
73 return n;
74 }
75 return -1;
76}
77
78static
79int get_factory_record_val(unsigned char *eeprom_buf, int size, uchar *record,
80 uchar *name, uchar *buf, int len)
81{
82 int ret = -1;
83 int i, nxt = 0;
84 int c;
85 unsigned char end = 0xff;
Heiko Schocher6596b852014-11-18 11:51:04 +010086 unsigned char tmp;
Heiko Schocher499c4982013-08-19 16:39:01 +020087
88 for (i = 0; fact_get_char(i) != end; i = nxt) {
89 nxt = i + 1;
90 if (fact_get_char(i) == '>') {
91 int pos;
92 int endpos;
93 int z;
Heiko Schocher6596b852014-11-18 11:51:04 +010094 int level = 0;
Heiko Schocher499c4982013-08-19 16:39:01 +020095
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++) {
Heiko Schocher6596b852014-11-18 11:51:04 +0100105 if (fact_get_char(z) == '<') {
106 if (level == 0) {
107 endpos = z;
108 nxt = endpos;
109 c = 0;
110 break;
111 } else {
112 level--;
113 }
Heiko Schocher499c4982013-08-19 16:39:01 +0200114 }
Heiko Schocher6596b852014-11-18 11:51:04 +0100115 if (fact_get_char(z) == '>')
116 level++;
Heiko Schocher499c4982013-08-19 16:39:01 +0200117 }
Heiko Schocher6596b852014-11-18 11:51:04 +0100118 } else {
119 continue;
Heiko Schocher499c4982013-08-19 16:39:01 +0200120 }
121 if (c == 0) {
122 /* end found -> call get_factory_val */
Heiko Schocher6596b852014-11-18 11:51:04 +0100123 tmp = eeprom_buf[endpos];
Heiko Schocher499c4982013-08-19 16:39:01 +0200124 eeprom_buf[endpos] = end;
125 ret = get_factory_val(&eeprom_buf[pos],
Heiko Schocher6596b852014-11-18 11:51:04 +0100126 endpos - pos, name, buf, len);
Heiko Schocher499c4982013-08-19 16:39:01 +0200127 /* fix buffer */
Heiko Schocher6596b852014-11-18 11:51:04 +0100128 eeprom_buf[endpos] = tmp;
Heiko Schocher499c4982013-08-19 16:39:01 +0200129 debug("%s: %s.%s = %s\n",
130 __func__, record, name, buf);
131 return ret;
132 }
133 }
134 }
135 return ret;
136}
137
138int factoryset_read_eeprom(int i2c_addr)
139{
140 int i, pages = 0, size = 0;
141 unsigned char eeprom_buf[0x3c00], hdr[4], buf[MAX_STRING_LENGTH];
142 unsigned char *cp, *cp1;
143
Marek Vasut7f8d4362018-02-16 16:41:18 +0100144#if defined(CONFIG_DFU_OVER_USB)
Maxime Ripard7f78b9d2017-09-07 08:58:08 +0200145 factory_dat.usb_vendor_id = CONFIG_USB_GADGET_VENDOR_NUM;
146 factory_dat.usb_product_id = CONFIG_USB_GADGET_PRODUCT_NUM;
Heiko Schocher499c4982013-08-19 16:39:01 +0200147#endif
Anatolij Gustschind0d75f52019-07-10 19:44:15 +0200148
Enrico Leto32f433f2024-01-24 15:43:50 +0100149 if (siemens_ee_read_data(SIEMENS_EE_ADDR_FACTORYSET, hdr, sizeof(hdr)))
Anatolij Gustschind0d75f52019-07-10 19:44:15 +0200150 goto err;
151
Heiko Schocher499c4982013-08-19 16:39:01 +0200152 if ((hdr[0] != 0x99) || (hdr[1] != 0x80)) {
153 printf("FactorySet is not right in eeprom.\n");
154 return 1;
155 }
156
157 /* get FactorySet size */
158 size = (hdr[2] << 8) + hdr[3] + sizeof(hdr);
159 if (size > 0x3bfa)
160 size = 0x3bfa;
161
162 pages = size / EEPR_PG_SZ;
163
164 /*
165 * read the eeprom using i2c
166 * I can not read entire eeprom in once, so separate into several
167 * times. Furthermore, fetch eeprom take longer time, so we fetch
168 * data after every time we got a record from eeprom
169 */
170 debug("Read eeprom page :\n");
Enrico Leto32f433f2024-01-24 15:43:50 +0100171 for (i = 0; i < pages; i++)
172 if (siemens_ee_read_data((OFF_PG + i) * EEPR_PG_SZ,
173 eeprom_buf + (i * EEPR_PG_SZ), EEPR_PG_SZ))
Anatolij Gustschind0d75f52019-07-10 19:44:15 +0200174 goto err;
Heiko Schocher499c4982013-08-19 16:39:01 +0200175
Enrico Leto32f433f2024-01-24 15:43:50 +0100176 if (size % EEPR_PG_SZ)
177 if (siemens_ee_read_data((OFF_PG + pages) * EEPR_PG_SZ,
178 eeprom_buf + (pages * EEPR_PG_SZ),
179 size % EEPR_PG_SZ))
Heiko Schocher499c4982013-08-19 16:39:01 +0200180 goto err;
181
182 /* we do below just for eeprom align */
183 for (i = 0; i < size; i++)
184 if (eeprom_buf[i] == '\n')
185 eeprom_buf[i] = 0;
186
187 /* skip header */
188 size -= sizeof(hdr);
189 cp = (uchar *)eeprom_buf + sizeof(hdr);
190
191 /* get mac address */
192 get_factory_record_val(cp, size, (uchar *)"ETH1", (uchar *)"mac",
193 buf, MAX_STRING_LENGTH);
194 cp1 = buf;
195 for (i = 0; i < 6; i++) {
Simon Glass3ff49ec2021-07-24 09:03:29 -0600196 factory_dat.mac[i] = hextoul((char *)cp1, NULL);
Heiko Schocher499c4982013-08-19 16:39:01 +0200197 cp1 += 3;
198 }
199
Anatolij Gustschin8e9821e2019-07-10 19:44:16 +0200200#if CONFIG_IS_ENABLED(TARGET_GIEDI) || CONFIG_IS_ENABLED(TARGET_DENEB)
201 /* get mac address for WLAN */
Enrico Leto32f433f2024-01-24 15:43:50 +0100202 if (get_factory_record_val(cp, size, (uchar *)"WLAN1", (uchar *)"mac",
203 buf, MAX_STRING_LENGTH) > 0) {
Anatolij Gustschin8e9821e2019-07-10 19:44:16 +0200204 cp1 = buf;
205 for (i = 0; i < 6; i++) {
Simon Glass3ff49ec2021-07-24 09:03:29 -0600206 factory_dat.mac_wlan[i] = hextoul((char *)cp1, NULL);
Anatolij Gustschin8e9821e2019-07-10 19:44:16 +0200207 cp1 += 3;
208 }
209 }
210#endif
211
Marek Vasut7f8d4362018-02-16 16:41:18 +0100212#if defined(CONFIG_DFU_OVER_USB)
Heiko Schocher499c4982013-08-19 16:39:01 +0200213 /* read vid and pid for dfu mode */
214 if (0 <= get_factory_record_val(cp, size, (uchar *)"USBD1",
215 (uchar *)"vid", buf,
216 MAX_STRING_LENGTH)) {
Simon Glass3ff49ec2021-07-24 09:03:29 -0600217 factory_dat.usb_vendor_id = hextoul((char *)buf, NULL);
Heiko Schocher499c4982013-08-19 16:39:01 +0200218 }
219
220 if (0 <= get_factory_record_val(cp, size, (uchar *)"USBD1",
221 (uchar *)"pid", buf,
222 MAX_STRING_LENGTH)) {
Simon Glass3ff49ec2021-07-24 09:03:29 -0600223 factory_dat.usb_product_id = hextoul((char *)buf, NULL);
Heiko Schocher499c4982013-08-19 16:39:01 +0200224 }
225 printf("DFU USB: VID = 0x%4x, PID = 0x%4x\n", factory_dat.usb_vendor_id,
226 factory_dat.usb_product_id);
227#endif
Samuel Egli8069bfe2013-11-04 14:05:03 +0100228 if (0 <= get_factory_record_val(cp, size, (uchar *)"DEV",
229 (uchar *)"num", factory_dat.serial,
230 MAX_STRING_LENGTH)) {
231 debug("serial number: %s\n", factory_dat.serial);
232 }
233 if (0 <= get_factory_record_val(cp, size, (uchar *)"DEV",
234 (uchar *)"ver", buf,
235 MAX_STRING_LENGTH)) {
Simon Glass3ff49ec2021-07-24 09:03:29 -0600236 factory_dat.version = hextoul((char *)buf, NULL);
Samuel Egli8069bfe2013-11-04 14:05:03 +0100237 debug("version number: %d\n", factory_dat.version);
238 }
Heiko Schocherfaf2dc62014-11-18 11:51:06 +0100239 /* Get ASN from factory set if available */
240 if (0 <= get_factory_record_val(cp, size, (uchar *)"DEV",
241 (uchar *)"id", factory_dat.asn,
242 MAX_STRING_LENGTH)) {
243 debug("factoryset asn: %s\n", factory_dat.asn);
244 } else {
245 factory_dat.asn[0] = 0;
246 }
Heiko Schocher7b905652014-11-18 11:51:05 +0100247 /* Get COMP/ver from factory set if available */
248 if (0 <= get_factory_record_val(cp, size, (uchar *)"COMP",
249 (uchar *)"ver",
250 factory_dat.comp_version,
251 MAX_STRING_LENGTH)) {
252 debug("factoryset COMP/ver: %s\n", factory_dat.comp_version);
253 } else {
254 strcpy((char *)factory_dat.comp_version, "1.0");
255 }
256
Heiko Schocher499c4982013-08-19 16:39:01 +0200257 return 0;
258
259err:
260 printf("Could not read the EEPROM; something fundamentally wrong on the I2C bus.\n");
261 return 1;
262}
263
Anatolij Gustschin8e9821e2019-07-10 19:44:16 +0200264static int get_mac_from_efuse(uint8_t mac[6])
265{
266#ifdef CONFIG_AM33XX
267 struct ctrl_dev *cdev = (struct ctrl_dev *)CTRL_DEVICE_BASE;
268 uint32_t mac_hi, mac_lo;
269
270 mac_lo = readl(&cdev->macid0l);
271 mac_hi = readl(&cdev->macid0h);
272
273 mac[0] = mac_hi & 0xFF;
274 mac[1] = (mac_hi & 0xFF00) >> 8;
275 mac[2] = (mac_hi & 0xFF0000) >> 16;
276 mac[3] = (mac_hi & 0xFF000000) >> 24;
277 mac[4] = mac_lo & 0xFF;
278 mac[5] = (mac_lo & 0xFF00) >> 8;
279#else
280 /* unhandled */
281 memset(mac, 0, 6);
282#endif
283 if (!is_valid_ethaddr(mac)) {
284 puts("Warning: ethaddr not set by FactorySet or E-fuse. ");
285 puts("Set <ethaddr> variable to overcome this.\n");
286 return -1;
287 }
288 return 0;
289}
Heiko Schocher499c4982013-08-19 16:39:01 +0200290
Simon Glass6a38e412017-08-03 12:22:09 -0600291static int factoryset_mac_env_set(void)
Heiko Schocher499c4982013-08-19 16:39:01 +0200292{
293 uint8_t mac_addr[6];
294
Anatolij Gustschin8e9821e2019-07-10 19:44:16 +0200295 /* Set mac from factoryset or try reading E-fuse */
Heiko Schocher499c4982013-08-19 16:39:01 +0200296 debug("FactorySet: Set mac address\n");
Joe Hershberger8ecdbed2015-04-08 01:41:04 -0500297 if (is_valid_ethaddr(factory_dat.mac)) {
Heiko Schocher499c4982013-08-19 16:39:01 +0200298 memcpy(mac_addr, factory_dat.mac, 6);
299 } else {
Heiko Schocher499c4982013-08-19 16:39:01 +0200300 debug("Warning: FactorySet: <ethaddr> not set. Fallback to E-fuse\n");
Anatolij Gustschin8e9821e2019-07-10 19:44:16 +0200301 if (get_mac_from_efuse(mac_addr) < 0)
Heiko Schocher499c4982013-08-19 16:39:01 +0200302 return -1;
Heiko Schocher499c4982013-08-19 16:39:01 +0200303 }
304
Simon Glass8551d552017-08-03 12:22:11 -0600305 eth_env_set_enetaddr("ethaddr", mac_addr);
Anatolij Gustschin8e9821e2019-07-10 19:44:16 +0200306
307#if CONFIG_IS_ENABLED(TARGET_GIEDI) || CONFIG_IS_ENABLED(TARGET_DENEB)
308 eth_env_set_enetaddr("eth1addr", mac_addr);
309
310 /* wlan mac */
311 if (is_valid_ethaddr(factory_dat.mac_wlan))
312 eth_env_set_enetaddr("eth2addr", factory_dat.mac_wlan);
313#endif
Heiko Schocher499c4982013-08-19 16:39:01 +0200314 return 0;
315}
316
Anatolij Gustschin8e9821e2019-07-10 19:44:16 +0200317static void factoryset_dtb_env_set(void)
318{
319 /* Set ASN in environment*/
320 if (factory_dat.asn[0] != 0) {
321 env_set("dtb_name", (char *)factory_dat.asn);
322 } else {
323 /* dtb suffix gets added in load script */
324 env_set("dtb_name", "default");
325 }
326}
327
Simon Glass6a38e412017-08-03 12:22:09 -0600328int factoryset_env_set(void)
Heiko Schocher499c4982013-08-19 16:39:01 +0200329{
330 int ret = 0;
331
Anatolij Gustschin8e9821e2019-07-10 19:44:16 +0200332 factoryset_dtb_env_set();
333
Simon Glass6a38e412017-08-03 12:22:09 -0600334 if (factoryset_mac_env_set() < 0)
Heiko Schocher499c4982013-08-19 16:39:01 +0200335 ret = -1;
336
337 return ret;
338}
339
Lukasz Majewski44b5b382013-10-08 14:30:41 +0200340int g_dnl_bind_fixup(struct usb_device_descriptor *dev, const char *name)
Heiko Schocher499c4982013-08-19 16:39:01 +0200341{
342 put_unaligned(factory_dat.usb_vendor_id, &dev->idVendor);
343 put_unaligned(factory_dat.usb_product_id, &dev->idProduct);
Samuel Egli8069bfe2013-11-04 14:05:03 +0100344 g_dnl_set_serialnumber((char *)factory_dat.serial);
345
Heiko Schocher499c4982013-08-19 16:39:01 +0200346 return 0;
347}
Samuel Egli8069bfe2013-11-04 14:05:03 +0100348
349int g_dnl_get_board_bcd_device_number(int gcnum)
350{
351 return factory_dat.version;
352}
Heiko Schocher499c4982013-08-19 16:39:01 +0200353#endif /* defined(CONFIG_SPL_BUILD) */