From 490e6d68597dc41d6d2a767927652470b19c2bd9 Mon Sep 17 00:00:00 2001
From: Anatolij Gustschin <agust@denx.de>
Date: Wed, 10 Jul 2019 19:44:16 +0200
Subject: [PATCH] board: siemens: extend factoryset reading for giedi and deneb
 boards

giedi and deneb are i.MX8X based and have additional data with
WLAN MAC in factoryset container. Enable building for these
boards and adapt factoryset functions to set WLAN MAC and dtb
name in environment.

Signed-off-by: Anatolij Gustschin <agust@denx.de>
---
 board/siemens/common/factoryset.c | 78 +++++++++++++++++++++++++------
 board/siemens/common/factoryset.h |  3 ++
 2 files changed, 66 insertions(+), 15 deletions(-)

diff --git a/board/siemens/common/factoryset.c b/board/siemens/common/factoryset.c
index 1dda897f18..40456a5392 100644
--- a/board/siemens/common/factoryset.c
+++ b/board/siemens/common/factoryset.c
@@ -243,6 +243,20 @@ int factoryset_read_eeprom(int i2c_addr)
 		cp1 += 3;
 	}
 
+#if CONFIG_IS_ENABLED(TARGET_GIEDI) || CONFIG_IS_ENABLED(TARGET_DENEB)
+	/* get mac address for WLAN */
+	ret = get_factory_record_val(cp, size, (uchar *)"WLAN1", (uchar *)"mac",
+				     buf, MAX_STRING_LENGTH);
+	if (ret > 0) {
+		cp1 = buf;
+		for (i = 0; i < 6; i++) {
+			factory_dat.mac_wlan[i] = simple_strtoul((char *)cp1,
+								 NULL, 16);
+			cp1 += 3;
+		}
+	}
+#endif
+
 #if defined(CONFIG_DFU_OVER_USB)
 	/* read vid and pid for dfu mode */
 	if (0 <= get_factory_record_val(cp, size, (uchar *)"USBD1",
@@ -305,42 +319,76 @@ err:
 	return 1;
 }
 
-static struct ctrl_dev *cdev = (struct ctrl_dev *)CTRL_DEVICE_BASE;
+static int get_mac_from_efuse(uint8_t mac[6])
+{
+#ifdef CONFIG_AM33XX
+	struct ctrl_dev *cdev = (struct ctrl_dev *)CTRL_DEVICE_BASE;
+	uint32_t mac_hi, mac_lo;
+
+	mac_lo = readl(&cdev->macid0l);
+	mac_hi = readl(&cdev->macid0h);
+
+	mac[0] = mac_hi & 0xFF;
+	mac[1] = (mac_hi & 0xFF00) >> 8;
+	mac[2] = (mac_hi & 0xFF0000) >> 16;
+	mac[3] = (mac_hi & 0xFF000000) >> 24;
+	mac[4] = mac_lo & 0xFF;
+	mac[5] = (mac_lo & 0xFF00) >> 8;
+#else
+	/* unhandled */
+	memset(mac, 0, 6);
+#endif
+	if (!is_valid_ethaddr(mac)) {
+		puts("Warning: ethaddr not set by FactorySet or E-fuse. ");
+		puts("Set <ethaddr> variable to overcome this.\n");
+		return -1;
+	}
+	return 0;
+}
 
 static int factoryset_mac_env_set(void)
 {
 	uint8_t mac_addr[6];
 
+	/* Set mac from factoryset or try reading E-fuse */
 	debug("FactorySet: Set mac address\n");
 	if (is_valid_ethaddr(factory_dat.mac)) {
 		memcpy(mac_addr, factory_dat.mac, 6);
 	} else {
-		uint32_t mac_hi, mac_lo;
-
 		debug("Warning: FactorySet: <ethaddr> not set. Fallback to E-fuse\n");
-		mac_lo = readl(&cdev->macid0l);
-		mac_hi = readl(&cdev->macid0h);
-
-		mac_addr[0] = mac_hi & 0xFF;
-		mac_addr[1] = (mac_hi & 0xFF00) >> 8;
-		mac_addr[2] = (mac_hi & 0xFF0000) >> 16;
-		mac_addr[3] = (mac_hi & 0xFF000000) >> 24;
-		mac_addr[4] = mac_lo & 0xFF;
-		mac_addr[5] = (mac_lo & 0xFF00) >> 8;
-		if (!is_valid_ethaddr(mac_addr)) {
-			printf("Warning: ethaddr not set by FactorySet or E-fuse. Set <ethaddr> variable to overcome this.\n");
+		if (get_mac_from_efuse(mac_addr) < 0)
 			return -1;
-		}
 	}
 
 	eth_env_set_enetaddr("ethaddr", mac_addr);
+
+#if CONFIG_IS_ENABLED(TARGET_GIEDI) || CONFIG_IS_ENABLED(TARGET_DENEB)
+	eth_env_set_enetaddr("eth1addr", mac_addr);
+
+	/* wlan mac */
+	if (is_valid_ethaddr(factory_dat.mac_wlan))
+		eth_env_set_enetaddr("eth2addr", factory_dat.mac_wlan);
+#endif
 	return 0;
 }
 
+static void factoryset_dtb_env_set(void)
+{
+	/* Set ASN in environment*/
+	if (factory_dat.asn[0] != 0) {
+		env_set("dtb_name", (char *)factory_dat.asn);
+	} else {
+		/* dtb suffix gets added in load script */
+		env_set("dtb_name", "default");
+	}
+}
+
 int factoryset_env_set(void)
 {
 	int ret = 0;
 
+	factoryset_dtb_env_set();
+
 	if (factoryset_mac_env_set() < 0)
 		ret = -1;
 
diff --git a/board/siemens/common/factoryset.h b/board/siemens/common/factoryset.h
index d4e8353249..261a217687 100644
--- a/board/siemens/common/factoryset.h
+++ b/board/siemens/common/factoryset.h
@@ -11,6 +11,9 @@
 
 struct factorysetcontainer {
 	uchar mac[6];
+#if CONFIG_IS_ENABLED(TARGET_GIEDI) || CONFIG_IS_ENABLED(TARGET_DENEB)
+	uchar mac_wlan[6];
+#endif
 	int usb_vendor_id;
 	int usb_product_id;
 	int pxm50;
-- 
2.39.5