public inbox for u-boot@lists.denx.de
 help / color / mirror / Atom feed
From: Detlev Casanova <detlev.casanova@collabora.com>
To: u-boot@lists.denx.de
Cc: Joe Hershberger <joe.hershberger@ni.com>,
	Ramon Fried <rfried.dev@gmail.com>, Tom Rini <trini@konsulko.com>,
	Philipp Tomsich <philipp.tomsich@vrull.eu>,
	Kever Yang <kever.yang@rock-chips.com>,
	Marek Vasut <marek.vasut+renesas@mailbox.org>,
	Jonas Karlman <jonas@kwiboo.se>, Simon Glass <sjg@chromium.org>,
	Fabio Estevam <festevam@denx.de>,
	Detlev Casanova <detlev.casanova@collabora.com>
Subject: [PATCH 2/3] rockchip: Add a board_gen_ethaddr() function
Date: Thu, 14 Mar 2024 10:43:47 -0400	[thread overview]
Message-ID: <20240314144403.491850-3-detlev.casanova@collabora.com> (raw)
In-Reply-To: <20240314144403.491850-1-detlev.casanova@collabora.com>

Set the MAC address based on the CPU ID only if the ethernet device has
no ROM or DT address set.

Signed-off-by: Detlev Casanova <detlev.casanova@collabora.com>
---
 arch/arm/Kconfig                          |  1 +
 arch/arm/include/asm/arch-rockchip/misc.h |  1 +
 arch/arm/mach-rockchip/board.c            | 30 ++++++++++++++++++-----
 arch/arm/mach-rockchip/misc.c             | 22 ++++++++++++-----
 4 files changed, 42 insertions(+), 12 deletions(-)

diff --git a/arch/arm/Kconfig b/arch/arm/Kconfig
index 01d6556c42b..21b41675ef6 100644
--- a/arch/arm/Kconfig
+++ b/arch/arm/Kconfig
@@ -2003,6 +2003,7 @@ config ARCH_ROCKCHIP
 	select DM_SPI_FLASH
 	select DM_USB_GADGET if USB_DWC3_GADGET
 	select ENABLE_ARM_SOC_BOOT0_HOOK
+	select NET_BOARD_ETHADDR
 	select OF_CONTROL
 	select MTD
 	select SPI
diff --git a/arch/arm/include/asm/arch-rockchip/misc.h b/arch/arm/include/asm/arch-rockchip/misc.h
index 4155af8c3b0..6e972de6279 100644
--- a/arch/arm/include/asm/arch-rockchip/misc.h
+++ b/arch/arm/include/asm/arch-rockchip/misc.h
@@ -10,5 +10,6 @@ int rockchip_cpuid_from_efuse(const u32 cpuid_offset,
 			      const u32 cpuid_length,
 			      u8 *cpuid);
 int rockchip_cpuid_set(const u8 *cpuid, const u32 cpuid_length);
+int rockchip_gen_macaddr(int dev_num, u8 *mac_addr);
 int rockchip_setup_macaddr(void);
 void rockchip_capsule_update_board_setup(void);
diff --git a/arch/arm/mach-rockchip/board.c b/arch/arm/mach-rockchip/board.c
index 2620530e03f..283d3b9ed3a 100644
--- a/arch/arm/mach-rockchip/board.c
+++ b/arch/arm/mach-rockchip/board.c
@@ -296,8 +296,8 @@ int fastboot_set_reboot_flag(enum fastboot_reboot_reason reason)
 }
 #endif
 
-#ifdef CONFIG_MISC_INIT_R
-__weak int misc_init_r(void)
+#if IS_ENABLED(CONFIG_MISC_INIT_R) || IS_ENABLED(CONFIG_NET_BOARD_ETHADDR)
+static int set_cpuid(void)
 {
 	const u32 cpuid_offset = CFG_CPUID_OFFSET;
 	const u32 cpuid_length = 0x10;
@@ -309,10 +309,6 @@ __weak int misc_init_r(void)
 		return ret;
 
 	ret = rockchip_cpuid_set(cpuid, cpuid_length);
-	if (ret)
-		return ret;
-
-	ret = rockchip_setup_macaddr();
 
 	return ret;
 }
@@ -349,3 +345,25 @@ __weak int board_rng_seed(struct abuf *buf)
 	return 0;
 }
 #endif
+
+#if IS_ENABLED(CONFIG_MISC_INIT_R)
+__weak int misc_init_r(void)
+{
+	return set_cpuid();
+}
+#endif
+
+int board_gen_ethaddr(int dev_num, u8 *mac_addr)
+{
+	if (!IS_ENABLED(CONFIG_NET_BOARD_ETHADDR))
+		return 0;
+
+	if (!env_get("cpuid#")) {
+		int err = set_cpuid();
+
+		if (err)
+			return err;
+	}
+
+	return rockchip_gen_macaddr(dev_num, mac_addr);
+}
diff --git a/arch/arm/mach-rockchip/misc.c b/arch/arm/mach-rockchip/misc.c
index 7d03f0c2b67..9c7b04ee5a8 100644
--- a/arch/arm/mach-rockchip/misc.c
+++ b/arch/arm/mach-rockchip/misc.c
@@ -21,14 +21,13 @@
 
 #include <asm/arch-rockchip/misc.h>
 
-int rockchip_setup_macaddr(void)
+int rockchip_gen_macaddr(int dev_num, u8 *mac_addr)
 {
 #if CONFIG_IS_ENABLED(HASH) && CONFIG_IS_ENABLED(SHA256)
 	int ret;
 	const char *cpuid = env_get("cpuid#");
 	u8 hash[SHA256_SUM_LEN];
 	int size = sizeof(hash);
-	u8 mac_addr[6];
 
 	/* Only generate a MAC address, if none is set in the environment */
 	if (env_get("ethaddr"))
@@ -51,15 +50,26 @@ int rockchip_setup_macaddr(void)
 	/* Make this a valid MAC address and set it */
 	mac_addr[0] &= 0xfe;  /* clear multicast bit */
 	mac_addr[0] |= 0x02;  /* set local assignment bit (IEEE802) */
-	eth_env_set_enetaddr("ethaddr", mac_addr);
 
-	/* Make a valid MAC address for ethernet1 */
-	mac_addr[5] ^= 0x01;
-	eth_env_set_enetaddr("eth1addr", mac_addr);
+	/* Make a valid MAC address for the given device number */
+	mac_addr[5] ^= dev_num;
 #endif
 	return 0;
 }
 
+int rockchip_setup_macaddr(void)
+{
+	u8 mac_addr[6];
+
+	if (rockchip_gen_macaddr(0, mac_addr) == 0)
+		eth_env_set_enetaddr("ethaddr", mac_addr);
+
+	if (rockchip_gen_macaddr(1, mac_addr) == 0)
+		eth_env_set_enetaddr("eth1addr", mac_addr);
+
+	return 0;
+}
+
 int rockchip_cpuid_from_efuse(const u32 cpuid_offset,
 			      const u32 cpuid_length,
 			      u8 *cpuid)
-- 
2.43.2


  parent reply	other threads:[~2024-03-14 14:44 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-03-14 14:43 [PATCH 0/3] net: Add a CONFIG_NET_BOARD_ETHADDR Detlev Casanova
2024-03-14 14:43 ` [PATCH 1/3] " Detlev Casanova
2024-03-14 17:36   ` Marek Vasut
2024-03-14 14:43 ` Detlev Casanova [this message]
2024-03-14 17:37   ` [PATCH 2/3] rockchip: Add a board_gen_ethaddr() function Marek Vasut
2024-03-14 18:38   ` Jonas Karlman
2024-04-02 15:38     ` Detlev Casanova
2024-03-14 14:43 ` [PATCH 3/3] net: eth-uclass: Add driver source possibility Detlev Casanova

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240314144403.491850-3-detlev.casanova@collabora.com \
    --to=detlev.casanova@collabora.com \
    --cc=festevam@denx.de \
    --cc=joe.hershberger@ni.com \
    --cc=jonas@kwiboo.se \
    --cc=kever.yang@rock-chips.com \
    --cc=marek.vasut+renesas@mailbox.org \
    --cc=philipp.tomsich@vrull.eu \
    --cc=rfried.dev@gmail.com \
    --cc=sjg@chromium.org \
    --cc=trini@konsulko.com \
    --cc=u-boot@lists.denx.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox