1 // SPDX-License-Identifier: GPL-2.0+
4 * Read FactorySet information from EEPROM into global structure.
5 * (C) Copyright 2013 Siemens Schweiz AG
8 #if !defined(CONFIG_SPL_BUILD)
13 #include <env_internal.h>
17 #if !CONFIG_IS_ENABLED(TARGET_GIEDI) && !CONFIG_IS_ENABLED(TARGET_DENEB)
18 #include <asm/arch/cpu.h>
20 #include <asm/arch/sys_proto.h>
21 #include <asm/unaligned.h>
25 #include "factoryset.h"
27 #define EEPR_PG_SZ 0x80
28 #define EEPROM_FATORYSET_OFFSET 0x400
29 #define OFF_PG EEPROM_FATORYSET_OFFSET/EEPR_PG_SZ
31 /* Global variable that contains necessary information from FactorySet */
32 struct factorysetcontainer factory_dat;
34 #define fact_get_char(i) *((char *)&eeprom_buf[i])
36 static int fact_match(unsigned char *eeprom_buf, uchar *s1, int i2)
41 while (*s1 == fact_get_char(i2++))
45 if (*s1 == '\0' && fact_get_char(i2-1) == '=')
51 static int get_factory_val(unsigned char *eeprom_buf, int size, uchar *name,
56 for (i = 0; fact_get_char(i) != '\0'; i = nxt + 1) {
59 for (nxt = i; fact_get_char(nxt) != '\0'; ++nxt) {
64 val = fact_match(eeprom_buf, (uchar *)name, i);
69 for (n = 0; n < len; ++n, ++buf) {
70 *buf = fact_get_char(val++);
78 printf("env_buf [%d bytes] too small for value of \"%s\"\n",
87 int get_factory_record_val(unsigned char *eeprom_buf, int size, uchar *record,
88 uchar *name, uchar *buf, int len)
93 unsigned char end = 0xff;
96 for (i = 0; fact_get_char(i) != end; i = nxt) {
98 if (fact_get_char(i) == '>') {
104 c = strncmp((char *)&eeprom_buf[i + 1], (char *)record,
105 strlen((char *)record));
108 pos = i + strlen((char *)record) + 2;
112 for (z = pos; fact_get_char(z) != end; z++) {
113 if (fact_get_char(z) == '<') {
123 if (fact_get_char(z) == '>')
130 /* end found -> call get_factory_val */
131 tmp = eeprom_buf[endpos];
132 eeprom_buf[endpos] = end;
133 ret = get_factory_val(&eeprom_buf[pos],
134 endpos - pos, name, buf, len);
136 eeprom_buf[endpos] = tmp;
137 debug("%s: %s.%s = %s\n",
138 __func__, record, name, buf);
146 int factoryset_read_eeprom(int i2c_addr)
148 int i, pages = 0, size = 0;
149 unsigned char eeprom_buf[0x3c00], hdr[4], buf[MAX_STRING_LENGTH];
150 unsigned char *cp, *cp1;
151 #if CONFIG_IS_ENABLED(DM_I2C)
152 struct udevice *bus, *dev;
156 #if defined(CONFIG_DFU_OVER_USB)
157 factory_dat.usb_vendor_id = CONFIG_USB_GADGET_VENDOR_NUM;
158 factory_dat.usb_product_id = CONFIG_USB_GADGET_PRODUCT_NUM;
161 #if CONFIG_IS_ENABLED(DM_I2C)
162 ret = uclass_get_device_by_seq(UCLASS_I2C, EEPROM_I2C_BUS, &bus);
166 ret = dm_i2c_probe(bus, i2c_addr, 0, &dev);
170 ret = i2c_set_chip_offset_len(dev, 2);
174 ret = dm_i2c_read(dev, EEPROM_FATORYSET_OFFSET, hdr, sizeof(hdr));
178 if (i2c_probe(i2c_addr))
181 if (i2c_read(i2c_addr, EEPROM_FATORYSET_OFFSET, 2, hdr, sizeof(hdr)))
185 if ((hdr[0] != 0x99) || (hdr[1] != 0x80)) {
186 printf("FactorySet is not right in eeprom.\n");
190 /* get FactorySet size */
191 size = (hdr[2] << 8) + hdr[3] + sizeof(hdr);
195 pages = size / EEPR_PG_SZ;
198 * read the eeprom using i2c
199 * I can not read entire eeprom in once, so separate into several
200 * times. Furthermore, fetch eeprom take longer time, so we fetch
201 * data after every time we got a record from eeprom
203 debug("Read eeprom page :\n");
204 for (i = 0; i < pages; i++) {
205 #if CONFIG_IS_ENABLED(DM_I2C)
206 ret = dm_i2c_read(dev, (OFF_PG + i) * EEPR_PG_SZ,
207 eeprom_buf + (i * EEPR_PG_SZ), EEPR_PG_SZ);
211 if (i2c_read(i2c_addr, (OFF_PG + i) * EEPR_PG_SZ, 2,
212 eeprom_buf + (i * EEPR_PG_SZ), EEPR_PG_SZ))
217 if (size % EEPR_PG_SZ) {
218 #if CONFIG_IS_ENABLED(DM_I2C)
219 ret = dm_i2c_read(dev, (OFF_PG + pages) * EEPR_PG_SZ,
220 eeprom_buf + (pages * EEPR_PG_SZ),
225 if (i2c_read(i2c_addr, (OFF_PG + pages) * EEPR_PG_SZ, 2,
226 eeprom_buf + (pages * EEPR_PG_SZ),
227 (size % EEPR_PG_SZ)))
232 /* we do below just for eeprom align */
233 for (i = 0; i < size; i++)
234 if (eeprom_buf[i] == '\n')
239 cp = (uchar *)eeprom_buf + sizeof(hdr);
241 /* get mac address */
242 get_factory_record_val(cp, size, (uchar *)"ETH1", (uchar *)"mac",
243 buf, MAX_STRING_LENGTH);
245 for (i = 0; i < 6; i++) {
246 factory_dat.mac[i] = hextoul((char *)cp1, NULL);
250 #if CONFIG_IS_ENABLED(TARGET_GIEDI) || CONFIG_IS_ENABLED(TARGET_DENEB)
251 /* get mac address for WLAN */
252 ret = get_factory_record_val(cp, size, (uchar *)"WLAN1", (uchar *)"mac",
253 buf, MAX_STRING_LENGTH);
256 for (i = 0; i < 6; i++) {
257 factory_dat.mac_wlan[i] = hextoul((char *)cp1, NULL);
263 #if defined(CONFIG_DFU_OVER_USB)
264 /* read vid and pid for dfu mode */
265 if (0 <= get_factory_record_val(cp, size, (uchar *)"USBD1",
267 MAX_STRING_LENGTH)) {
268 factory_dat.usb_vendor_id = hextoul((char *)buf, NULL);
271 if (0 <= get_factory_record_val(cp, size, (uchar *)"USBD1",
273 MAX_STRING_LENGTH)) {
274 factory_dat.usb_product_id = hextoul((char *)buf, NULL);
276 printf("DFU USB: VID = 0x%4x, PID = 0x%4x\n", factory_dat.usb_vendor_id,
277 factory_dat.usb_product_id);
279 #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);
286 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);
291 if (0 <= get_factory_record_val(cp, size, (uchar *)"DEV",
293 MAX_STRING_LENGTH)) {
294 factory_dat.version = hextoul((char *)buf, NULL);
295 debug("version number: %d\n", factory_dat.version);
297 /* Get ASN from factory set if available */
298 if (0 <= get_factory_record_val(cp, size, (uchar *)"DEV",
299 (uchar *)"id", factory_dat.asn,
300 MAX_STRING_LENGTH)) {
301 debug("factoryset asn: %s\n", factory_dat.asn);
303 factory_dat.asn[0] = 0;
305 /* Get COMP/ver from factory set if available */
306 if (0 <= get_factory_record_val(cp, size, (uchar *)"COMP",
308 factory_dat.comp_version,
309 MAX_STRING_LENGTH)) {
310 debug("factoryset COMP/ver: %s\n", factory_dat.comp_version);
312 strcpy((char *)factory_dat.comp_version, "1.0");
318 printf("Could not read the EEPROM; something fundamentally wrong on the I2C bus.\n");
322 static int get_mac_from_efuse(uint8_t mac[6])
325 struct ctrl_dev *cdev = (struct ctrl_dev *)CTRL_DEVICE_BASE;
326 uint32_t mac_hi, mac_lo;
328 mac_lo = readl(&cdev->macid0l);
329 mac_hi = readl(&cdev->macid0h);
331 mac[0] = mac_hi & 0xFF;
332 mac[1] = (mac_hi & 0xFF00) >> 8;
333 mac[2] = (mac_hi & 0xFF0000) >> 16;
334 mac[3] = (mac_hi & 0xFF000000) >> 24;
335 mac[4] = mac_lo & 0xFF;
336 mac[5] = (mac_lo & 0xFF00) >> 8;
341 if (!is_valid_ethaddr(mac)) {
342 puts("Warning: ethaddr not set by FactorySet or E-fuse. ");
343 puts("Set <ethaddr> variable to overcome this.\n");
349 static int factoryset_mac_env_set(void)
353 /* Set mac from factoryset or try reading E-fuse */
354 debug("FactorySet: Set mac address\n");
355 if (is_valid_ethaddr(factory_dat.mac)) {
356 memcpy(mac_addr, factory_dat.mac, 6);
358 debug("Warning: FactorySet: <ethaddr> not set. Fallback to E-fuse\n");
359 if (get_mac_from_efuse(mac_addr) < 0)
363 eth_env_set_enetaddr("ethaddr", mac_addr);
365 #if CONFIG_IS_ENABLED(TARGET_GIEDI) || CONFIG_IS_ENABLED(TARGET_DENEB)
366 eth_env_set_enetaddr("eth1addr", mac_addr);
369 if (is_valid_ethaddr(factory_dat.mac_wlan))
370 eth_env_set_enetaddr("eth2addr", factory_dat.mac_wlan);
375 static void factoryset_dtb_env_set(void)
377 /* Set ASN in environment*/
378 if (factory_dat.asn[0] != 0) {
379 env_set("dtb_name", (char *)factory_dat.asn);
381 /* dtb suffix gets added in load script */
382 env_set("dtb_name", "default");
386 int factoryset_env_set(void)
390 factoryset_dtb_env_set();
392 if (factoryset_mac_env_set() < 0)
398 int g_dnl_bind_fixup(struct usb_device_descriptor *dev, const char *name)
400 put_unaligned(factory_dat.usb_vendor_id, &dev->idVendor);
401 put_unaligned(factory_dat.usb_product_id, &dev->idProduct);
402 g_dnl_set_serialnumber((char *)factory_dat.serial);
407 int g_dnl_get_board_bcd_device_number(int gcnum)
409 return factory_dat.version;
411 #endif /* defined(CONFIG_SPL_BUILD) */