mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Sascha Hauer <s.hauer@pengutronix.de>
To: Barebox List <barebox@lists.infradead.org>
Subject: [PATCH] nvmem: rockchip-otp: implement SoC UID reading
Date: Tue, 17 Mar 2026 14:47:55 +0100	[thread overview]
Message-ID: <20260317134755.3184029-1-s.hauer@pengutronix.de> (raw)

The Rockchip SoCs supported by the rockchip-otp driver have a SoC UID in
the OTP data. Read it out and forward it to barebox. The raw SoC UID
goes through some crc32 code before it is used. This is based on U-Boot
code which itself is based on the Rockchip Downstream Linux support.

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
 drivers/nvmem/rockchip-otp.c | 54 ++++++++++++++++++++++++++++++++++--
 1 file changed, 51 insertions(+), 3 deletions(-)

diff --git a/drivers/nvmem/rockchip-otp.c b/drivers/nvmem/rockchip-otp.c
index d2145bba52..041d628480 100644
--- a/drivers/nvmem/rockchip-otp.c
+++ b/drivers/nvmem/rockchip-otp.c
@@ -9,6 +9,7 @@
 #include <common.h>
 #include <driver.h>
 #include <init.h>
+#include <crc.h>
 #include <of.h>
 #include <of_device.h>
 #include <linux/clk.h>
@@ -86,6 +87,7 @@ struct rockchip_data {
 	const char * const *clks;
 	int num_clks;
 	int (*reg_read)(void *ctx, unsigned int reg, void *val, size_t val_size);
+	int cpuid_offset;
 };
 
 struct rockchip_otp {
@@ -400,6 +402,7 @@ static const struct rockchip_data px30_data = {
 	.clks = px30_otp_clocks,
 	.num_clks = ARRAY_SIZE(px30_otp_clocks),
 	.reg_read = px30_otp_read,
+	.cpuid_offset = -1,
 };
 
 static const char * const rk3562_otp_clocks[] = {
@@ -411,6 +414,7 @@ static const struct rockchip_data rk3562_data = {
 	.clks = rk3562_otp_clocks,
 	.num_clks = ARRAY_SIZE(rk3562_otp_clocks),
 	.reg_read = rk3562_otp_read,
+	.cpuid_offset = -1,
 };
 
 static const char * const rk3568_otp_clocks[] = {
@@ -422,6 +426,7 @@ static const struct rockchip_data rk3568_data = {
 	.clks = rk3568_otp_clocks,
 	.num_clks = ARRAY_SIZE(rk3568_otp_clocks),
 	.reg_read = rk3568_otp_read,
+	.cpuid_offset = 0xa,
 };
 
 static const struct rockchip_data rk3576_data = {
@@ -429,6 +434,7 @@ static const struct rockchip_data rk3576_data = {
 	.clks = px30_otp_clocks,
 	.num_clks = ARRAY_SIZE(px30_otp_clocks),
 	.reg_read = rk3588_otp_read,
+	.cpuid_offset = 0xa,
 };
 
 static const char * const rk3588_otp_clocks[] = {
@@ -440,6 +446,7 @@ static const struct rockchip_data rk3588_data = {
 	.clks = rk3588_otp_clocks,
 	.num_clks = ARRAY_SIZE(rk3588_otp_clocks),
 	.reg_read = rk3588_otp_read,
+	.cpuid_offset = 0x7,
 };
 
 static __maybe_unused const struct of_device_id rockchip_otp_match[] = {
@@ -471,6 +478,42 @@ static __maybe_unused const struct of_device_id rockchip_otp_match[] = {
 };
 MODULE_DEVICE_TABLE(of, rockchip_otp_match);
 
+#define CPUID_SIZE 16
+
+static int rockchip_otp_read_socuid(struct rockchip_otp *otp, struct nvmem_device *nvmem)
+{
+	u8 low[CPUID_SIZE / 2], high[CPUID_SIZE / 2];
+	unsigned char cpuid[CPUID_SIZE];
+	char uidstr[CPUID_SIZE * 2 + 1];
+	u64 serialno;
+	int i, ret, offset;
+
+	offset = otp->data->cpuid_offset;
+	if (offset < 0)
+		return 0;
+
+	ret = rockchip_otp_read(otp, offset, cpuid, CPUID_SIZE);
+	if (ret < 0)
+		return ret;
+
+	/*
+	 * Do the same mangling as U-Boot and downstream Rockchip Linux does.
+	 */
+	for (i = 0; i < CPUID_SIZE / 2; i++) {
+		low[i] = cpuid[1 + (i << 1)];
+		high[i] = cpuid[i << 1];
+	}
+
+	serialno = crc32_no_comp(0, low, CPUID_SIZE / 2);
+	serialno |= (u64)crc32_no_comp(serialno, high, CPUID_SIZE / 2) << 32;
+
+	snprintf(uidstr, sizeof(uidstr), "%016llx", serialno);
+
+	barebox_set_soc_uid(uidstr, &serialno, sizeof(serialno));
+
+	return 0;
+}
+
 static int rockchip_otp_probe(struct device *dev)
 {
 	struct rockchip_otp *otp;
@@ -521,11 +564,16 @@ static int rockchip_otp_probe(struct device *dev)
 	otp_config.dev = dev;
 
 	nvmem = nvmem_register(&otp_config);
-	if (!IS_ERR(nvmem))
-		return 0;
+	if (IS_ERR(nvmem)) {
+		ret = PTR_ERR(nvmem);
+		goto err_register;
+	}
 
-	ret = PTR_ERR(nvmem);
+	rockchip_otp_read_socuid(otp, nvmem);
+
+	return 0;
 
+err_register:
 	reset_control_put(otp->rst);
 
 err_rst:
-- 
2.47.3




             reply	other threads:[~2026-03-17 13:48 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-03-17 13:47 Sascha Hauer [this message]
2026-03-17 21:44 ` Marco Felsch
2026-03-18 10:32   ` Sascha Hauer
2026-03-20  6:35 ` Sascha Hauer

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=20260317134755.3184029-1-s.hauer@pengutronix.de \
    --to=s.hauer@pengutronix.de \
    --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