mail archive of the barebox mailing list
 help / color / mirror / Atom feed
* [PATCH] nvmem: rockchip-otp: implement SoC UID reading
@ 2026-03-17 13:47 Sascha Hauer
  2026-03-17 21:44 ` Marco Felsch
  2026-03-20  6:35 ` Sascha Hauer
  0 siblings, 2 replies; 4+ messages in thread
From: Sascha Hauer @ 2026-03-17 13:47 UTC (permalink / raw)
  To: Barebox List

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




^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] nvmem: rockchip-otp: implement SoC UID reading
  2026-03-17 13:47 [PATCH] nvmem: rockchip-otp: implement SoC UID reading Sascha Hauer
@ 2026-03-17 21:44 ` Marco Felsch
  2026-03-18 10:32   ` Sascha Hauer
  2026-03-20  6:35 ` Sascha Hauer
  1 sibling, 1 reply; 4+ messages in thread
From: Marco Felsch @ 2026-03-17 21:44 UTC (permalink / raw)
  To: Sascha Hauer; +Cc: Barebox List

Hi Sascha,

On 26-03-17, Sascha Hauer wrote:
> 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;
		^
Would be nice to have it as function hook, like the reg_read() in case
RK SoCs find just another way to readout the ID in the future or the ID
gets more bits (like NXP did for the i.MX8MP).

In such cases the function hook is more future proof.

Regards,
  Marco


>  };
>  
>  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
> 
> 
> 

-- 
#gernperDu 
#CallMeByMyFirstName

Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | https://www.pengutronix.de/ |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-9    |



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] nvmem: rockchip-otp: implement SoC UID reading
  2026-03-17 21:44 ` Marco Felsch
@ 2026-03-18 10:32   ` Sascha Hauer
  0 siblings, 0 replies; 4+ messages in thread
From: Sascha Hauer @ 2026-03-18 10:32 UTC (permalink / raw)
  To: Marco Felsch; +Cc: Barebox List

On Tue, Mar 17, 2026 at 10:44:32PM +0100, Marco Felsch wrote:
> Hi Sascha,
> 
> On 26-03-17, Sascha Hauer wrote:
> > 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;
> 		^
> Would be nice to have it as function hook, like the reg_read() in case
> RK SoCs find just another way to readout the ID in the future or the ID
> gets more bits (like NXP did for the i.MX8MP).
> 
> In such cases the function hook is more future proof.

I think this is easy enough to refactor once we get in this situation
and once we really know in which way we have to adjust.

Sascha


-- 
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] nvmem: rockchip-otp: implement SoC UID reading
  2026-03-17 13:47 [PATCH] nvmem: rockchip-otp: implement SoC UID reading Sascha Hauer
  2026-03-17 21:44 ` Marco Felsch
@ 2026-03-20  6:35 ` Sascha Hauer
  1 sibling, 0 replies; 4+ messages in thread
From: Sascha Hauer @ 2026-03-20  6:35 UTC (permalink / raw)
  To: Barebox List, Sascha Hauer


On Tue, 17 Mar 2026 14:47:55 +0100, Sascha Hauer wrote:
> 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.
> 
> 

Applied, thanks!

[1/1] nvmem: rockchip-otp: implement SoC UID reading
      https://git.pengutronix.de/cgit/barebox/commit/?id=101b25168f3c (link may not be stable)

Best regards,
-- 
Sascha Hauer <s.hauer@pengutronix.de>




^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-03-20  6:36 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-03-17 13:47 [PATCH] nvmem: rockchip-otp: implement SoC UID reading Sascha Hauer
2026-03-17 21:44 ` Marco Felsch
2026-03-18 10:32   ` Sascha Hauer
2026-03-20  6:35 ` Sascha Hauer

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox