mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Robin van der Gracht <robin.van.der.gracht@protonic.nl>
To: barebox@lists.infradead.org
Cc: Robin van der Gracht <robin.van.der.gracht@protonic.nl>
Subject: [PATCH 9/9] ARM: boards: protonic-rk356x: Read board serial and MAC address from eMMC
Date: Wed,  4 Mar 2026 12:00:13 +0100	[thread overview]
Message-ID: <20260304110013.495725-10-robin.van.der.gracht@protonic.nl> (raw)
In-Reply-To: <20260304110013.495725-1-robin.van.der.gracht@protonic.nl>

Protonic stores the MAC address and serial at the start of the reserved
space gpt partition (see Rockchip doc for partition details).

If a board has multiple ethernet macs the mac addresses are successive.

Signed-off-by: Robin van der Gracht <robin.van.der.gracht@protonic.nl>
---
 arch/arm/boards/protonic-rk356x/board.c | 75 +++++++++++++++++++++++++
 1 file changed, 75 insertions(+)

diff --git a/arch/arm/boards/protonic-rk356x/board.c b/arch/arm/boards/protonic-rk356x/board.c
index 486c77e441..bbf273e44f 100644
--- a/arch/arm/boards/protonic-rk356x/board.c
+++ b/arch/arm/boards/protonic-rk356x/board.c
@@ -13,6 +13,8 @@
 #include <of_device.h>
 #include <aiodev.h>
 #include <globalvar.h>
+#include <net.h>
+#include <barebox-info.h>
 
 struct prt_rk356x_adc_chs {
 	int usb_boot;
@@ -24,6 +26,7 @@ struct prt_rk356x_model {
 	const char *name;
 	const char *shortname;
 	const struct prt_rk356x_adc_chs adc_channels;
+	const int mac_cnt;
 	int (*init)(void);
 };
 
@@ -97,6 +100,76 @@ static void prt_rk356x_process_adc(struct device *dev,
 	pr_info("Board id: %d, revision %d\n", prt_priv.hw_id, prt_priv.hw_rev);
 }
 
+static int prt_rk356x_inc_mac(u8 *mac)
+{
+	mac[5]++;
+	if (mac[5])
+		return 0;
+	mac[4]++;
+	if (mac[4])
+		return 0;
+	mac[3]++;
+	if (mac[3])
+		return 0;
+	pr_err("MAC address overflow!!\n");
+	return -EINVAL;
+}
+
+#define RK_GPT_RESERVED_SPACE (7680 * 512)
+
+static void prt_rk356x_board_info(void)
+{
+	struct cdev *cdev;
+	ssize_t size;
+	u8 buf[11];
+	int i;
+
+	cdev = cdev_by_name("mmc0");
+	if (!cdev) {
+		pr_warn("No eMMC device\n");
+		return;
+	}
+
+	/* Read and check TAG */
+	size = cdev_read(cdev, buf, 4, RK_GPT_RESERVED_SPACE, 0);
+	if (size != 4) {
+		pr_warn("Unable to read board data from eMMC\n");
+		return;
+	}
+	if (strncmp(buf, "PRTm", 4)) {
+		pr_warn("Board has no serial number and no MAC address\n");
+		return;
+	}
+
+	/* Read MAC at offset 4 */
+	if (prt_priv.model->mac_cnt > 0) {
+		size = cdev_read(cdev, buf, 6, RK_GPT_RESERVED_SPACE + 4, 0);
+		if (size != 6) {
+			pr_warn("Unable to read MAC address from eMMC\n");
+			return;
+		}
+		if (is_valid_ether_addr(buf)) {
+			eth_register_ethaddr(0, buf);
+			for (i = 1; i < prt_priv.model->mac_cnt; i++) {
+				prt_rk356x_inc_mac(buf);
+				eth_register_ethaddr(i, buf);
+			}
+		} else {
+			pr_warn("Board has invalid MAC address\n");
+		}
+	}
+
+	/* Read serial at offset 10 */
+	size = cdev_read(cdev, buf, 10, RK_GPT_RESERVED_SPACE + 10, 0);
+	if (size != 10) {
+		pr_warn("Unable to read serial number from eMMC\n");
+		return;
+	}
+	buf[10] = 0;
+	pr_info("Board serial number: %s\n", buf);
+	barebox_set_serial_number(buf);
+}
+
 static int mecsbc_sd_of_fixup(struct device_node *root, void *context)
 {
 	struct device *dev = context;
@@ -145,6 +218,7 @@ static int prt_rk356x_devices_init(void)
 		return 0; /* Not a prt_rk356x board! */
 
 	prt_rk356x_bbu();
+	prt_rk356x_board_info();
 
 	if (prt_priv.model->init) {
 		ret = prt_priv.model->init();
@@ -201,6 +275,7 @@ static const struct prt_rk356x_model mecsbc = {
 	.name = "Protonic MECSBC board",
 	.shortname = "mecsbc",
 	.adc_channels = {0, 1, 3},
+	.mac_cnt = 3,
 	.init = prt_rk356x_mecsbc_init,
 };
 
-- 
2.43.0




      parent reply	other threads:[~2026-03-04 11:01 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-03-04 11:00 [PATCH 0/9] ARM: boards: protonic-mecsbc: Generalization Robin van der Gracht
2026-03-04 11:00 ` [PATCH 1/9] ARM: boards: Rename protonic-mecsbc to protonic-rk356x Robin van der Gracht
2026-03-04 11:00 ` [PATCH 2/9] ARM: boards: protonic-rk356x: Remove VIN measurement Robin van der Gracht
2026-03-04 11:00 ` [PATCH 3/9] ARM: boards: protonic-rk356x: Use devicetree alias for saradc Robin van der Gracht
2026-03-04 11:00 ` [PATCH 4/9] ARM: boards: protonic-rk356x: Configure ADC and channels in model data Robin van der Gracht
2026-03-04 11:00 ` [PATCH 5/9] ARM: boards: protonic-rk356x: Move mecsbc code to model init function Robin van der Gracht
2026-03-04 11:00 ` [PATCH 6/9] ARM: boards: protonic-rk356x: Use rockchip_bbu_mmc_register Robin van der Gracht
2026-03-04 11:00 ` [PATCH 7/9] ARM: boards: protonic-rk356x: Move bbu setup to device late init Robin van der Gracht
2026-03-04 11:00 ` [PATCH 8/9] ARM: boards: protonic-rk356x: Fix crash with different boards Robin van der Gracht
2026-03-04 11:00 ` Robin van der Gracht [this message]

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=20260304110013.495725-10-robin.van.der.gracht@protonic.nl \
    --to=robin.van.der.gracht@protonic.nl \
    --cc=barebox@lists.infradead.org \
    /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