mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH v4 0/5] nvmem: Derive Rockchip MAC addresses from the OTP CPU ID
@ 2026-11-24 12:04 Alexey Charkov
  2026-11-24 12:04 ` [PATCH v4 1/5] dt-bindings: nvmem: fixed-cell: Add Rockchip " Alexey Charkov
                   ` (4 more replies)
  0 siblings, 5 replies; 14+ messages in thread
From: Alexey Charkov @ 2026-11-24 12:04 UTC (permalink / raw)
  To: Srinivas Kandagatla, Rob Herring, Krzysztof Kozlowski,
	Conor Dooley, Heiko Stuebner, Rafał Miłecki,
	Andrew Lunn, Heiner Kallweit, Russell King, David S. Miller,
	Eric Dumazet, Jakub Kicinski, Paolo Abeni, Willem de Bruijn,
	Simon Horman
  Cc: Ryan Leung, Ricardo Pardini, devicetree, linux-arm-kernel,
	linux-rockchip, netdev, linux-kernel, Alexey Charkov

Rockchip SoCs are shipped with a unique CPU ID in their internal OTP
memory, and Rockchip bootloaders use it to give boards which have no
dedicated storage for a MAC address a stable one anyway: they hash the CPU
ID and patch the resulting addresses into the device tree they hand over.

Kernels started without that fixup, e.g. straight from the SPL in Falcon
mode or by any other loader which does not implement Rockchip's derivation,
fall back to random MAC addresses which change on every boot.

Formalize the derivation in the DT binding and implement it in the
Rockchip OTP driver, so that a Linux image can use the same stable
addresses regardless of the boot flow.

v3 is a redesign following the v2 discussion. Instead of a separate
SoC-specific NVMEM layout, which could not coexist with the fixed layouts
already used by RK3528, RK3562 and RK356x, the existing CPU ID cell gets
a "rockchip,cpuid" compatible. Referencing it as a "mac-address" cell
selects the derived address, while any other reference keeps getting the
raw CPU ID. The CPU ID location comes from the cell's reg property, so
no per-SoC data is needed, and other SoCs only need the compatible on
their CPU ID cell plus the GMAC references. This is similar to the
behavior already present upstream in drivers/nvmem/imx-ocotp.c.

Plain "mac-base" does not fit here: its argument is an offset added to
the base address, whereas the existing Rockchip derivation used by the
bootloaders XORs the index into the last octet.

Patch 2 lets the generic NVMEM MAC address helpers accept a cell longer
than six bytes and use its first six. NVMEM cells have a fixed size, so
the OTP driver writes the derived address into the start of the 16-byte
CPU ID cell for MAC address consumers, while the cell keeps its full
size for everyone else.

Patch 4 moves the RK3576 OTP cells from the deprecated direct-child
syntax into a fixed layout, as the former does not allow a compatible on
a cell.

Only RK3576 is wired up here, that being the SoC I can test on.

Cross-checked on an RK3576 board: the addresses fixed up into the FDT by
U-Boot match the ones derived by the driver, and the driver correctly
assigns them to the network interfaces when the kernel is booted without
U-Boot proper at all (via Falcon mode). The raw CPU ID cell remains
readable alongside (e.g. via sysfs).

Signed-off-by: Alexey Charkov <alchark@flipper.net>
---
Changes in v4:
- Dropped the NVMEM core change letting post-processing shorten the
  output, as cells have a fixed size (Srinivas)
- Added a net change letting the NVMEM MAC address helpers take the
  first six bytes of a longer cell instead
- Made the OTP post-processor write the address into the first six
  bytes of the CPU ID cell, keeping its size
- Link to v3: https://patch.msgid.link/20261123-rk3576-otp-cpuid-mac-v3-0-40c756dcf36f@flipper.net

Changes in v3:
- Dropped the OTP read serialization patch, applied separately
- Replaced the SoC-specific NVMEM layout with a "rockchip,cpuid"
  compatible on the existing CPU ID fixed cell, so that it works
  alongside other fixed cells (Ricardo) and needs no per-SoC table (Ryan)
- Moved the derivation into the rockchip-otp driver's cell fixup hook,
  which also makes the layout module unload issue moot (Sashiko)
- Made #nvmem-cell-cells required for the new compatible (Sashiko)
- Dropped the dead match data and address validity checks (Srinivas)
- Allowed MAC address indices up to 255 rather than just 0 and 1
- Added an NVMEM core change letting post-processing shorten the output
- Moved the RK3576 OTP cells into a fixed layout
- Dropped Miquel's Reviewed-by from the driver patch, as the code moved
- Link to v2: https://patch.msgid.link/20260902-rk3576-otp-cpuid-mac-v2-0-e4b7fe2ab13f@flipper.net

Changes in v2:
- Switched from a scope-based guard to explicit lock/unlock calls in the
  OTP driver to avoid mixing styles in a function using goto error
  handling (Sashiko)
- Link to v1: https://patch.msgid.link/20260901-rk3576-otp-cpuid-mac-v1-0-ea9135270fc2@flipper.net

---
Alexey Charkov (5):
      dt-bindings: nvmem: fixed-cell: Add Rockchip CPU ID
      net: Accept MAC addresses from longer NVMEM cells
      nvmem: rockchip-otp: Derive MAC addresses from the CPU ID
      arm64: dts: rockchip: Move RK3576 OTP cells into a fixed layout
      arm64: dts: rockchip: Derive GMAC MAC addresses from OTP on RK3576

 .../bindings/nvmem/layouts/fixed-cell.yaml         |  33 ++++++
 .../bindings/nvmem/layouts/fixed-layout.yaml       |   6 ++
 arch/arm64/boot/dts/rockchip/rk3576.dtsi           | 111 +++++++++++----------
 drivers/nvmem/Kconfig                              |   4 +-
 drivers/nvmem/rockchip-otp.c                       |  48 +++++++++
 net/core/of_net.c                                  |   4 +-
 net/ethernet/eth.c                                 |   5 +-
 7 files changed, 155 insertions(+), 56 deletions(-)
---
base-commit: 3191063649dbf7c0d0b768e1f3655e4c5fc6edb5
change-id: 20260901-rk3576-otp-cpuid-mac-3c90243d0884

Best regards,
--  
Alexey Charkov <alchark@flipper.net>


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

* [PATCH v4 1/5] dt-bindings: nvmem: fixed-cell: Add Rockchip CPU ID
  2026-11-24 12:04 [PATCH v4 0/5] nvmem: Derive Rockchip MAC addresses from the OTP CPU ID Alexey Charkov
@ 2026-11-24 12:04 ` Alexey Charkov
  2026-11-24 12:04 ` [PATCH v4 2/5] net: Accept MAC addresses from longer NVMEM cells Alexey Charkov
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 14+ messages in thread
From: Alexey Charkov @ 2026-11-24 12:04 UTC (permalink / raw)
  To: Srinivas Kandagatla, Rob Herring, Krzysztof Kozlowski,
	Conor Dooley, Heiko Stuebner, Rafał Miłecki,
	Andrew Lunn, Heiner Kallweit, Russell King, David S. Miller,
	Eric Dumazet, Jakub Kicinski, Paolo Abeni, Willem de Bruijn,
	Simon Horman
  Cc: Ryan Leung, Ricardo Pardini, devicetree, linux-arm-kernel,
	linux-rockchip, netdev, linux-kernel, Alexey Charkov

Rockchip SoCs ship with a unique CPU ID programmed into their OTP memory,
and compatible bootloaders derive Ethernet MAC addresses from it, so that
boards with no dedicated storage for one still get a stable address.
Whether a board ends up with those addresses then depends on the
bootloader patching them into the device tree.

Describe the CPU ID cell with a compatible which also defines that
derivation, so that the addresses are available to anything which reads
the device tree, whatever the boot flow. Referencing the cell as a
"mac-address" selects the derived address, keeping the raw CPU ID
available to any other consumer from the same cell. Keep the compatible
vendor-wide, as only the location of the CPU ID differs between SoCs,
and that is given by the reg property already.

Plain "mac-base" does not fit: the CPU ID is not a MAC address in either
of the formats it allows, and its offset argument is added to the base,
whereas the bootloaders XOR the index into the last octet.

Signed-off-by: Alexey Charkov <alchark@flipper.net>
---
 .../bindings/nvmem/layouts/fixed-cell.yaml         | 33 ++++++++++++++++++++++
 .../bindings/nvmem/layouts/fixed-layout.yaml       |  6 ++++
 2 files changed, 39 insertions(+)

diff --git a/Documentation/devicetree/bindings/nvmem/layouts/fixed-cell.yaml b/Documentation/devicetree/bindings/nvmem/layouts/fixed-cell.yaml
index 38e3ad50ff4f..ad38954cfb59 100644
--- a/Documentation/devicetree/bindings/nvmem/layouts/fixed-cell.yaml
+++ b/Documentation/devicetree/bindings/nvmem/layouts/fixed-cell.yaml
@@ -19,6 +19,18 @@ properties:
           addresses.
           It can be stored in a plain binary format (cell length 6) or as an
           ASCII text like "00:11:22:33:44:55" (cell length 17).
+      - const: rockchip,cpuid
+        description: >
+          Cell with the unique CPU ID of a Rockchip SoC (cell length 16).
+          A consumer referencing it as its "mac-address" NVMEM cell, as
+          Ethernet controllers do, refers to a stable MAC address derived from
+          the CPU ID rather than to the CPU ID itself. The base of such
+          addresses consists of the first six octets of the SHA-256 digest of
+          the 32-character string of lowercase hexadecimal digits representing
+          the 16 CPU ID bytes in stored order, without separators or a
+          terminator. In the first octet, the multicast bit (bit 0) is then
+          cleared and the locally administered bit (bit 1) set. Any other
+          reference to the cell refers to the CPU ID as stored.
 
   reg:
     maxItems: 1
@@ -48,6 +60,27 @@ allOf:
           const: 1
       required:
         - "#nvmem-cell-cells"
+  - if:
+      properties:
+        compatible:
+          contains:
+            const: rockchip,cpuid
+      required: [ compatible ]
+    then:
+      properties:
+        reg:
+          items:
+            - items:
+                - description: Offset of the CPU ID
+                - const: 16
+        "#nvmem-cell-cells":
+          description:
+            For a "mac-address" reference, the first argument is the index of
+            the MAC address, which is XORed into the last octet of the base
+            address. It is ignored for any other reference.
+          const: 1
+      required:
+        - "#nvmem-cell-cells"
 
 required:
   - reg
diff --git a/Documentation/devicetree/bindings/nvmem/layouts/fixed-layout.yaml b/Documentation/devicetree/bindings/nvmem/layouts/fixed-layout.yaml
index b01567f99284..f30166109a7f 100644
--- a/Documentation/devicetree/bindings/nvmem/layouts/fixed-layout.yaml
+++ b/Documentation/devicetree/bindings/nvmem/layouts/fixed-layout.yaml
@@ -56,6 +56,12 @@ examples:
             #nvmem-cell-cells = <1>;
         };
 
+        id@200 {
+            compatible = "rockchip,cpuid";
+            reg = <0x200 0x10>;
+            #nvmem-cell-cells = <1>;
+        };
+
         calibration@4000 {
             reg = <0x4000 0x100>;
         };

-- 
2.56.0


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

* [PATCH v4 2/5] net: Accept MAC addresses from longer NVMEM cells
  2026-11-24 12:04 [PATCH v4 0/5] nvmem: Derive Rockchip MAC addresses from the OTP CPU ID Alexey Charkov
  2026-11-24 12:04 ` [PATCH v4 1/5] dt-bindings: nvmem: fixed-cell: Add Rockchip " Alexey Charkov
@ 2026-11-24 12:04 ` Alexey Charkov
  2026-11-24 12:04 ` [PATCH v4 3/5] nvmem: rockchip-otp: Derive MAC addresses from the CPU ID Alexey Charkov
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 14+ messages in thread
From: Alexey Charkov @ 2026-11-24 12:04 UTC (permalink / raw)
  To: Srinivas Kandagatla, Rob Herring, Krzysztof Kozlowski,
	Conor Dooley, Heiko Stuebner, Rafał Miłecki,
	Andrew Lunn, Heiner Kallweit, Russell King, David S. Miller,
	Eric Dumazet, Jakub Kicinski, Paolo Abeni, Willem de Bruijn,
	Simon Horman
  Cc: Ryan Leung, Ricardo Pardini, devicetree, linux-arm-kernel,
	linux-rockchip, netdev, linux-kernel, Alexey Charkov

A "mac-address" NVMEM cell may contain a longer unique hardware ID,
beginning with valid MAC address bytes. Both NVMEM MAC address helpers
reject such a cell, as they insist on exactly ETH_ALEN bytes, and then
discard any tail regardless by doing a copy.

Accept a cell of at least ETH_ALEN bytes and use the first ETH_ALEN of
them, leaving it to is_valid_ether_addr() to reject unusable content.

Signed-off-by: Alexey Charkov <alchark@flipper.net>
---
 net/core/of_net.c  | 4 ++--
 net/ethernet/eth.c | 5 +++--
 2 files changed, 5 insertions(+), 4 deletions(-)

diff --git a/net/core/of_net.c b/net/core/of_net.c
index 93ea425b9248..d4a256521954 100644
--- a/net/core/of_net.c
+++ b/net/core/of_net.c
@@ -85,7 +85,7 @@ int of_get_mac_address_nvmem(struct device_node *np, u8 *addr)
 	if (IS_ERR(mac))
 		return PTR_ERR(mac);
 
-	if (len != ETH_ALEN || !is_valid_ether_addr(mac)) {
+	if (len < ETH_ALEN || !is_valid_ether_addr(mac)) {
 		kfree(mac);
 		return -EINVAL;
 	}
@@ -108,7 +108,7 @@ EXPORT_SYMBOL(of_get_mac_address_nvmem);
  * because that is the default address. If that isn't set, then the obsolete
  * 'address' is checked, just in case we're using an old device tree. If any
  * of the above isn't set, then try to get MAC address from nvmem cell named
- * 'mac-address'.
+ * 'mac-address', using its first six bytes if the cell is longer.
  *
  * Note that the 'address' property is supposed to contain a virtual address of
  * the register set, but some DTS files have redefined that property to be the
diff --git a/net/ethernet/eth.c b/net/ethernet/eth.c
index 39aab0f8035a..b69ef0eb16f8 100644
--- a/net/ethernet/eth.c
+++ b/net/ethernet/eth.c
@@ -508,7 +508,8 @@ EXPORT_SYMBOL(platform_get_ethdev_address);
 
 /**
  * nvmem_get_mac_address - Obtain the MAC address from an nvmem cell named
- * 'mac-address' associated with given device.
+ * 'mac-address' associated with given device. If the cell is longer than a MAC
+ * address, its first ETH_ALEN bytes are used.
  *
  * @dev:	Device with which the mac-address cell is associated.
  * @addrbuf:	Buffer to which the MAC address will be copied on success.
@@ -531,7 +532,7 @@ int nvmem_get_mac_address(struct device *dev, void *addrbuf)
 	if (IS_ERR(mac))
 		return PTR_ERR(mac);
 
-	if (len != ETH_ALEN || !is_valid_ether_addr(mac)) {
+	if (len < ETH_ALEN || !is_valid_ether_addr(mac)) {
 		kfree(mac);
 		return -EINVAL;
 	}

-- 
2.56.0


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

* [PATCH v4 3/5] nvmem: rockchip-otp: Derive MAC addresses from the CPU ID
  2026-11-24 12:04 [PATCH v4 0/5] nvmem: Derive Rockchip MAC addresses from the OTP CPU ID Alexey Charkov
  2026-11-24 12:04 ` [PATCH v4 1/5] dt-bindings: nvmem: fixed-cell: Add Rockchip " Alexey Charkov
  2026-11-24 12:04 ` [PATCH v4 2/5] net: Accept MAC addresses from longer NVMEM cells Alexey Charkov
@ 2026-11-24 12:04 ` Alexey Charkov
  2026-11-25 13:49   ` Ryan Leung
  2026-11-24 12:04 ` [PATCH v4 4/5] arm64: dts: rockchip: Move RK3576 OTP cells into a fixed layout Alexey Charkov
  2026-11-24 12:04 ` [PATCH v4 5/5] arm64: dts: rockchip: Derive GMAC MAC addresses from OTP on RK3576 Alexey Charkov
  4 siblings, 1 reply; 14+ messages in thread
From: Alexey Charkov @ 2026-11-24 12:04 UTC (permalink / raw)
  To: Srinivas Kandagatla, Rob Herring, Krzysztof Kozlowski,
	Conor Dooley, Heiko Stuebner, Rafał Miłecki,
	Andrew Lunn, Heiner Kallweit, Russell King, David S. Miller,
	Eric Dumazet, Jakub Kicinski, Paolo Abeni, Willem de Bruijn,
	Simon Horman
  Cc: Ryan Leung, Ricardo Pardini, devicetree, linux-arm-kernel,
	linux-rockchip, netdev, linux-kernel, Alexey Charkov

Rockchip bootloaders derive stable Ethernet MAC addresses from the CPU
ID in the OTP memory and patch them into the device tree they pass on.
Kernels started any other way, e.g. straight from the SPL in Falcon
mode, end up with random addresses which change on every boot, even
though the CPU ID is just as readable from Linux.

Implement the derivation for cells marked as "rockchip,cpuid", handing
the result to consumers which ask for the cell as "mac-address", such as
of_get_mac_address(). Leave the CPU ID unchanged for any other reader,
so that the same cell keeps serving the ID itself.

Follow U-Boot's rockchip_setup_macaddr() exactly, so that both yield the
same addresses: hash the CPU ID's lowercase hex string with SHA-256,
take the first six octets as a unicast, locally administered address,
and XOR the cell argument into the last octet, which is how U-Boot gets
eth1addr from ethaddr.

Put the address in the first six octets and keep the cell at the size
of the CPU ID, as NVMEM cells have a fixed size and MAC address
consumers accept a longer cell.

Hooking the derivation into the OTP provider rather than a separate
layout means it works with any fixed layout of the OTP cells, and needs
no per-SoC knowledge, as the cell already gives the CPU ID's location.

Signed-off-by: Alexey Charkov <alchark@flipper.net>
---
 drivers/nvmem/Kconfig        |  4 +++-
 drivers/nvmem/rockchip-otp.c | 48 ++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 51 insertions(+), 1 deletion(-)

diff --git a/drivers/nvmem/Kconfig b/drivers/nvmem/Kconfig
index d2f5bb236663..5776d4e4b391 100644
--- a/drivers/nvmem/Kconfig
+++ b/drivers/nvmem/Kconfig
@@ -448,9 +448,11 @@ config NVMEM_ROCKCHIP_OTP
 	tristate "Rockchip OTP controller support"
 	depends on ARCH_ROCKCHIP || COMPILE_TEST
 	depends on HAS_IOMEM
+	select CRYPTO_LIB_SHA256
 	help
 	  This is a simple driver to dump specified values of Rockchip SoC
-	  from OTP, such as cpu-leakage.
+	  from OTP, such as cpu-leakage. It also derives stable Ethernet MAC
+	  addresses from the CPU ID, as Rockchip bootloaders do.
 
 	  This driver can also be built as a module. If so, the module
 	  will be called nvmem_rockchip_otp.
diff --git a/drivers/nvmem/rockchip-otp.c b/drivers/nvmem/rockchip-otp.c
index 7eb834516ab4..eb91e7a34051 100644
--- a/drivers/nvmem/rockchip-otp.c
+++ b/drivers/nvmem/rockchip-otp.c
@@ -6,9 +6,12 @@
  * Author: Finley Xiao <finley.xiao@rock-chips.com>
  */
 
+#include <crypto/sha2.h>
 #include <linux/clk.h>
 #include <linux/delay.h>
 #include <linux/device.h>
+#include <linux/etherdevice.h>
+#include <linux/hex.h>
 #include <linux/io.h>
 #include <linux/iopoll.h>
 #include <linux/module.h>
@@ -67,6 +70,8 @@
 #define RK3588_AUTO_EN			BIT(0)
 #define RK3588_RD_DONE			BIT(1)
 
+#define ROCKCHIP_CPUID_LEN		16
+
 struct rockchip_data {
 	int size;
 	int read_offset;
@@ -319,6 +324,48 @@ static int rockchip_otp_read(void *context, unsigned int offset,
 	return ret;
 }
 
+/*
+ * Derive a MAC address from the CPU ID the same way Rockchip bootloaders do
+ * (see rockchip_setup_macaddr() in U-Boot's arch/arm/mach-rockchip/board.c),
+ * for consumers asking for one. Everyone else gets the CPU ID unchanged.
+ *
+ * The address takes the first six octets of the cell, which keeps the size of
+ * the CPU ID. Consumers only look at those, so the rest is left as it is.
+ *
+ * The cell argument is XORed into the last octet rather than added to the
+ * address, matching how U-Boot derives eth1addr from ethaddr.
+ */
+static int rockchip_otp_cpuid_pp(void *priv, const char *id, int index,
+				 unsigned int offset, void *buf, size_t bytes)
+{
+	char cpuid[ROCKCHIP_CPUID_LEN * 2];
+	u8 digest[SHA256_DIGEST_SIZE];
+	u8 *mac = buf;
+
+	if (!id || strcmp(id, "mac-address"))
+		return 0;
+
+	if (bytes != ROCKCHIP_CPUID_LEN || index < 0 || index > U8_MAX)
+		return -EINVAL;
+
+	bin2hex(cpuid, buf, ROCKCHIP_CPUID_LEN);
+	sha256(cpuid, sizeof(cpuid), digest);
+
+	memcpy(mac, digest, ETH_ALEN);
+	mac[0] &= 0xfe;		/* clear the multicast bit */
+	mac[0] |= 0x02;		/* set the locally administered bit */
+	mac[5] ^= index;
+
+	return 0;
+}
+
+static void rockchip_otp_fixup_dt_cell_info(struct nvmem_device *nvmem,
+					    struct nvmem_cell_info *cell)
+{
+	if (of_device_is_compatible(cell->np, "rockchip,cpuid"))
+		cell->read_post_process = rockchip_otp_cpuid_pp;
+}
+
 static struct nvmem_config otp_config = {
 	.name = "rockchip-otp",
 	.owner = THIS_MODULE,
@@ -328,6 +375,7 @@ static struct nvmem_config otp_config = {
 	.stride = 1,
 	.word_size = sizeof(u8),
 	.reg_read = rockchip_otp_read,
+	.fixup_dt_cell_info = rockchip_otp_fixup_dt_cell_info,
 };
 
 static const char * const px30_otp_clocks[] = {

-- 
2.56.0


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

* [PATCH v4 4/5] arm64: dts: rockchip: Move RK3576 OTP cells into a fixed layout
  2026-11-24 12:04 [PATCH v4 0/5] nvmem: Derive Rockchip MAC addresses from the OTP CPU ID Alexey Charkov
                   ` (2 preceding siblings ...)
  2026-11-24 12:04 ` [PATCH v4 3/5] nvmem: rockchip-otp: Derive MAC addresses from the CPU ID Alexey Charkov
@ 2026-11-24 12:04 ` Alexey Charkov
  2026-11-24 12:04 ` [PATCH v4 5/5] arm64: dts: rockchip: Derive GMAC MAC addresses from OTP on RK3576 Alexey Charkov
  4 siblings, 0 replies; 14+ messages in thread
From: Alexey Charkov @ 2026-11-24 12:04 UTC (permalink / raw)
  To: Srinivas Kandagatla, Rob Herring, Krzysztof Kozlowski,
	Conor Dooley, Heiko Stuebner, Rafał Miłecki,
	Andrew Lunn, Heiner Kallweit, Russell King, David S. Miller,
	Eric Dumazet, Jakub Kicinski, Paolo Abeni, Willem de Bruijn,
	Simon Horman
  Cc: Ryan Leung, Ricardo Pardini, devicetree, linux-arm-kernel,
	linux-rockchip, netdev, linux-kernel, Alexey Charkov

The RK3576 OTP describes its cells as direct children of the OTP node,
which is the deprecated syntax predating NVMEM layouts. It does not
allow a compatible on a cell, which describing the CPU ID cell needs.

Move the cells into a fixed layout, as already done for RK3528, RK3562
and RK356x. Labels and node names stay the same, so the cells keep their
names and existing references keep working.

Signed-off-by: Alexey Charkov <alchark@flipper.net>
---
 arch/arm64/boot/dts/rockchip/rk3576.dtsi | 105 ++++++++++++++++---------------
 1 file changed, 54 insertions(+), 51 deletions(-)

diff --git a/arch/arm64/boot/dts/rockchip/rk3576.dtsi b/arch/arm64/boot/dts/rockchip/rk3576.dtsi
index 6f5780af4192..2e83a73ddbb2 100644
--- a/arch/arm64/boot/dts/rockchip/rk3576.dtsi
+++ b/arch/arm64/boot/dts/rockchip/rk3576.dtsi
@@ -2298,63 +2298,66 @@ rng: rng@2a410000 {
 		otp: otp@2a580000 {
 			compatible = "rockchip,rk3576-otp";
 			reg = <0x0 0x2a580000 0x0 0x400>;
-			#address-cells = <1>;
-			#size-cells = <1>;
 			clocks = <&cru CLK_OTPC_NS>, <&cru PCLK_OTPC_NS>,
 				 <&cru CLK_OTP_PHY_G>;
 			clock-names = "otp", "apb_pclk", "phy";
 			resets = <&cru SRST_OTPC_NS>, <&cru SRST_P_OTPC_NS>;
 			reset-names = "otp", "apb";
 
-			/* Data cells */
-			cpu_code: cpu-code@2 {
-				reg = <0x02 0x2>;
-			};
-			otp_cpu_version: cpu-version@5 {
-				reg = <0x05 0x1>;
-				bits = <3 3>;
-			};
-			otp_id: id@a {
-				reg = <0x0a 0x10>;
-			};
-			cpub_leakage: cpub-leakage@1e {
-				reg = <0x1e 0x1>;
-			};
-			cpul_leakage: cpul-leakage@1f {
-				reg = <0x1f 0x1>;
-			};
-			npu_leakage: npu-leakage@20 {
-				reg = <0x20 0x1>;
-			};
-			gpu_leakage: gpu-leakage@21 {
-				reg = <0x21 0x1>;
-			};
-			log_leakage: log-leakage@22 {
-				reg = <0x22 0x1>;
-			};
-			bigcore_tsadc_trim: bigcore-tsadc-trim@24 {
-				reg = <0x24 0x2>;
-				bits = <0 10>;
-			};
-			litcore_tsadc_trim: litcore-tsadc-trim@26 {
-				reg = <0x26 0x2>;
-				bits = <0 10>;
-			};
-			ddr_tsadc_trim: ddr-tsadc-trim@28 {
-				reg = <0x28 0x2>;
-				bits = <0 10>;
-			};
-			npu_tsadc_trim: npu-tsadc-trim@2a {
-				reg = <0x2a 0x2>;
-				bits = <0 10>;
-			};
-			gpu_tsadc_trim: gpu-tsadc-trim@2c {
-				reg = <0x2c 0x2>;
-				bits = <0 10>;
-			};
-			soc_tsadc_trim: soc-tsadc-trim@64 {
-				reg = <0x64 0x2>;
-				bits = <0 10>;
+			nvmem-layout {
+				compatible = "fixed-layout";
+				#address-cells = <1>;
+				#size-cells = <1>;
+
+				cpu_code: cpu-code@2 {
+					reg = <0x02 0x2>;
+				};
+				otp_cpu_version: cpu-version@5 {
+					reg = <0x05 0x1>;
+					bits = <3 3>;
+				};
+				otp_id: id@a {
+					reg = <0x0a 0x10>;
+				};
+				cpub_leakage: cpub-leakage@1e {
+					reg = <0x1e 0x1>;
+				};
+				cpul_leakage: cpul-leakage@1f {
+					reg = <0x1f 0x1>;
+				};
+				npu_leakage: npu-leakage@20 {
+					reg = <0x20 0x1>;
+				};
+				gpu_leakage: gpu-leakage@21 {
+					reg = <0x21 0x1>;
+				};
+				log_leakage: log-leakage@22 {
+					reg = <0x22 0x1>;
+				};
+				bigcore_tsadc_trim: bigcore-tsadc-trim@24 {
+					reg = <0x24 0x2>;
+					bits = <0 10>;
+				};
+				litcore_tsadc_trim: litcore-tsadc-trim@26 {
+					reg = <0x26 0x2>;
+					bits = <0 10>;
+				};
+				ddr_tsadc_trim: ddr-tsadc-trim@28 {
+					reg = <0x28 0x2>;
+					bits = <0 10>;
+				};
+				npu_tsadc_trim: npu-tsadc-trim@2a {
+					reg = <0x2a 0x2>;
+					bits = <0 10>;
+				};
+				gpu_tsadc_trim: gpu-tsadc-trim@2c {
+					reg = <0x2c 0x2>;
+					bits = <0 10>;
+				};
+				soc_tsadc_trim: soc-tsadc-trim@64 {
+					reg = <0x64 0x2>;
+					bits = <0 10>;
+				};
 			};
 		};
 

-- 
2.56.0


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

* [PATCH v4 5/5] arm64: dts: rockchip: Derive GMAC MAC addresses from OTP on RK3576
  2026-11-24 12:04 [PATCH v4 0/5] nvmem: Derive Rockchip MAC addresses from the OTP CPU ID Alexey Charkov
                   ` (3 preceding siblings ...)
  2026-11-24 12:04 ` [PATCH v4 4/5] arm64: dts: rockchip: Move RK3576 OTP cells into a fixed layout Alexey Charkov
@ 2026-11-24 12:04 ` Alexey Charkov
  4 siblings, 0 replies; 14+ messages in thread
From: Alexey Charkov @ 2026-11-24 12:04 UTC (permalink / raw)
  To: Srinivas Kandagatla, Rob Herring, Krzysztof Kozlowski,
	Conor Dooley, Heiko Stuebner, Rafał Miłecki,
	Andrew Lunn, Heiner Kallweit, Russell King, David S. Miller,
	Eric Dumazet, Jakub Kicinski, Paolo Abeni, Willem de Bruijn,
	Simon Horman
  Cc: Ryan Leung, Ricardo Pardini, devicetree, linux-arm-kernel,
	linux-rockchip, netdev, linux-kernel, Alexey Charkov

RK3576 boards without dedicated storage for a MAC address only get a
stable one if the bootloader derives it from the CPU ID and patches it
into the device tree, which not every boot flow does.

Mark the CPU ID cell as such and reference it from both GMACs, so that
the addresses can be derived from the device tree alone, whatever the
bootloader. Boards which store a MAC address of their own can override
the reference with a cell of their choosing.

Note that the GMACs then depend on the OTP memory being accessible, as
the thermal sensors already do for their trim values.

Signed-off-by: Alexey Charkov <alchark@flipper.net>
---
 arch/arm64/boot/dts/rockchip/rk3576.dtsi | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/arch/arm64/boot/dts/rockchip/rk3576.dtsi b/arch/arm64/boot/dts/rockchip/rk3576.dtsi
index 2e83a73ddbb2..55892f4afe60 100644
--- a/arch/arm64/boot/dts/rockchip/rk3576.dtsi
+++ b/arch/arm64/boot/dts/rockchip/rk3576.dtsi
@@ -2070,6 +2070,8 @@ gmac0: ethernet@2a220000 {
 			interrupts = <GIC_SPI 293 IRQ_TYPE_LEVEL_HIGH>,
 				     <GIC_SPI 298 IRQ_TYPE_LEVEL_HIGH>;
 			interrupt-names = "macirq", "eth_wake_irq";
+			nvmem-cells = <&otp_id 0>;
+			nvmem-cell-names = "mac-address";
 			power-domains = <&power RK3576_PD_SDGMAC>;
 			resets = <&cru SRST_A_GMAC0>;
 			reset-names = "stmmaceth";
@@ -2118,6 +2120,8 @@ gmac1: ethernet@2a230000 {
 			interrupts = <GIC_SPI 301 IRQ_TYPE_LEVEL_HIGH>,
 				     <GIC_SPI 306 IRQ_TYPE_LEVEL_HIGH>;
 			interrupt-names = "macirq", "eth_wake_irq";
+			nvmem-cells = <&otp_id 1>;
+			nvmem-cell-names = "mac-address";
 			power-domains = <&power RK3576_PD_SDGMAC>;
 			resets = <&cru SRST_A_GMAC1>;
 			reset-names = "stmmaceth";
@@ -2317,7 +2321,9 @@ otp_cpu_version: cpu-version@5 {
 					bits = <3 3>;
 				};
 				otp_id: id@a {
+					compatible = "rockchip,cpuid";
 					reg = <0x0a 0x10>;
+					#nvmem-cell-cells = <1>;
 				};
 				cpub_leakage: cpub-leakage@1e {
 					reg = <0x1e 0x1>;

-- 
2.56.0


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

* Re: [PATCH v4 3/5] nvmem: rockchip-otp: Derive MAC addresses from the CPU ID
  2026-11-24 12:04 ` [PATCH v4 3/5] nvmem: rockchip-otp: Derive MAC addresses from the CPU ID Alexey Charkov
@ 2026-11-25 13:49   ` Ryan Leung
  2026-11-25 13:55     ` Alexey Charkov
  2026-11-25 14:21     ` Andrew Lunn
  0 siblings, 2 replies; 14+ messages in thread
From: Ryan Leung @ 2026-11-25 13:49 UTC (permalink / raw)
  To: Alexey Charkov
  Cc: Srinivas Kandagatla, Rob Herring, Krzysztof Kozlowski,
	Conor Dooley, Heiko Stuebner, Rafał Miłecki,
	Andrew Lunn, Heiner Kallweit, Russell King, David S. Miller,
	Eric Dumazet, Jakub Kicinski, Paolo Abeni, Willem de Bruijn,
	Simon Horman, Ricardo Pardini, devicetree, linux-arm-kernel,
	linux-rockchip, netdev, linux-kernel

Hi Alexey,

On Tuesday, November 24th, 2026 at 8:04 PM, Alexey Charkov <alchark@flipper.net> wrote:

> Rockchip bootloaders derive stable Ethernet MAC addresses from the CPU
> ID in the OTP memory and patch them into the device tree they pass on.
> Kernels started any other way, e.g. straight from the SPL in Falcon
> mode, end up with random addresses which change on every boot, even
> though the CPU ID is just as readable from Linux.
> 
> Implement the derivation for cells marked as "rockchip,cpuid", handing
> the result to consumers which ask for the cell as "mac-address", such as
> of_get_mac_address(). Leave the CPU ID unchanged for any other reader,
> so that the same cell keeps serving the ID itself.
> 
> Follow U-Boot's rockchip_setup_macaddr() exactly, so that both yield the
> same addresses: hash the CPU ID's lowercase hex string with SHA-256,
> take the first six octets as a unicast, locally administered address,
> and XOR the cell argument into the last octet, which is how U-Boot gets
> eth1addr from ethaddr.
> 
> Put the address in the first six octets and keep the cell at the size
> of the CPU ID, as NVMEM cells have a fixed size and MAC address
> consumers accept a longer cell.
> 
> Hooking the derivation into the OTP provider rather than a separate
> layout means it works with any fixed layout of the OTP cells, and needs
> no per-SoC knowledge, as the cell already gives the CPU ID's location.
> 
> Signed-off-by: Alexey Charkov <alchark@flipper.net>
> ---
>  drivers/nvmem/Kconfig        |  4 +++-
>  drivers/nvmem/rockchip-otp.c | 48 ++++++++++++++++++++++++++++++++++++++++++++
>  2 files changed, 51 insertions(+), 1 deletion(-)
> 
> diff --git a/drivers/nvmem/Kconfig b/drivers/nvmem/Kconfig
> index d2f5bb236663..5776d4e4b391 100644
> --- a/drivers/nvmem/Kconfig
> +++ b/drivers/nvmem/Kconfig
> @@ -448,9 +448,11 @@ config NVMEM_ROCKCHIP_OTP
>  	tristate "Rockchip OTP controller support"
>  	depends on ARCH_ROCKCHIP || COMPILE_TEST
>  	depends on HAS_IOMEM
> +	select CRYPTO_LIB_SHA256
>  	help
>  	  This is a simple driver to dump specified values of Rockchip SoC
> -	  from OTP, such as cpu-leakage.
> +	  from OTP, such as cpu-leakage. It also derives stable Ethernet MAC
> +	  addresses from the CPU ID, as Rockchip bootloaders do.
> 
>  	  This driver can also be built as a module. If so, the module
>  	  will be called nvmem_rockchip_otp.
> diff --git a/drivers/nvmem/rockchip-otp.c b/drivers/nvmem/rockchip-otp.c
> index 7eb834516ab4..eb91e7a34051 100644
> --- a/drivers/nvmem/rockchip-otp.c
> +++ b/drivers/nvmem/rockchip-otp.c
> @@ -6,9 +6,12 @@
>   * Author: Finley Xiao <finley.xiao@rock-chips.com>
>   */
> 
> +#include <crypto/sha2.h>
>  #include <linux/clk.h>
>  #include <linux/delay.h>
>  #include <linux/device.h>
> +#include <linux/etherdevice.h>
> +#include <linux/hex.h>
>  #include <linux/io.h>
>  #include <linux/iopoll.h>
>  #include <linux/module.h>
> @@ -67,6 +70,8 @@
>  #define RK3588_AUTO_EN			BIT(0)
>  #define RK3588_RD_DONE			BIT(1)
> 
> +#define ROCKCHIP_CPUID_LEN		16
> +
>  struct rockchip_data {
>  	int size;
>  	int read_offset;
> @@ -319,6 +324,48 @@ static int rockchip_otp_read(void *context, unsigned int offset,
>  	return ret;
>  }
> 
> +/*
> + * Derive a MAC address from the CPU ID the same way Rockchip bootloaders do
> + * (see rockchip_setup_macaddr() in U-Boot's arch/arm/mach-rockchip/board.c),
> + * for consumers asking for one. Everyone else gets the CPU ID unchanged.
> + *
> + * The address takes the first six octets of the cell, which keeps the size of
> + * the CPU ID. Consumers only look at those, so the rest is left as it is.
> + *
> + * The cell argument is XORed into the last octet rather than added to the
> + * address, matching how U-Boot derives eth1addr from ethaddr.
> + */
> +static int rockchip_otp_cpuid_pp(void *priv, const char *id, int index,
> +				 unsigned int offset, void *buf, size_t bytes)
> +{
> +	char cpuid[ROCKCHIP_CPUID_LEN * 2];
> +	u8 digest[SHA256_DIGEST_SIZE];
> +	u8 *mac = buf;
> +
> +	if (!id || strcmp(id, "mac-address"))
> +		return 0;
> +
> +	if (bytes != ROCKCHIP_CPUID_LEN || index < 0 || index > U8_MAX)
> +		return -EINVAL;
> +
> +	bin2hex(cpuid, buf, ROCKCHIP_CPUID_LEN);
> +	sha256(cpuid, sizeof(cpuid), digest);
> +
> +	memcpy(mac, digest, ETH_ALEN);
> +	mac[0] &= 0xfe;		/* clear the multicast bit */
> +	mac[0] |= 0x02;		/* set the locally administered bit */
> +	mac[5] ^= index;
> +
> +	return 0;
> +}
> +
> +static void rockchip_otp_fixup_dt_cell_info(struct nvmem_device *nvmem,
> +					    struct nvmem_cell_info *cell)
> +{
> +	if (of_device_is_compatible(cell->np, "rockchip,cpuid"))
> +		cell->read_post_process = rockchip_otp_cpuid_pp;
> +}
> +
>  static struct nvmem_config otp_config = {
>  	.name = "rockchip-otp",
>  	.owner = THIS_MODULE,
> @@ -328,6 +375,7 @@ static struct nvmem_config otp_config = {
>  	.stride = 1,
>  	.word_size = sizeof(u8),
>  	.reg_read = rockchip_otp_read,
> +	.fixup_dt_cell_info = rockchip_otp_fixup_dt_cell_info,
>  };
> 
>  static const char * const px30_otp_clocks[] = {
> 
> --
> 2.56.0
> 
> 

Perhaps "rockchip,cpuid" could include a mention of "mac" somewhere?

Tested-by: Ryan Leung <untilscour@protonmail.com> # RK3528 NanoPi R28S

I tested on FriendlyELEC NanoPi R28S (RK3528) with index of 100 and the MAC address was exactly as expected.

Looking forward to using this.

Best regards,
Ryan

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

* Re: [PATCH v4 3/5] nvmem: rockchip-otp: Derive MAC addresses from the CPU ID
  2026-11-25 13:49   ` Ryan Leung
@ 2026-11-25 13:55     ` Alexey Charkov
  2026-11-25 14:21     ` Andrew Lunn
  1 sibling, 0 replies; 14+ messages in thread
From: Alexey Charkov @ 2026-11-25 13:55 UTC (permalink / raw)
  To: Ryan Leung
  Cc: Srinivas Kandagatla, Rob Herring, Krzysztof Kozlowski,
	Conor Dooley, Heiko Stuebner, Rafał Miłecki,
	Andrew Lunn, Heiner Kallweit, Russell King, David S. Miller,
	Eric Dumazet, Jakub Kicinski, Paolo Abeni, Willem de Bruijn,
	Simon Horman, Ricardo Pardini, devicetree, linux-arm-kernel,
	linux-rockchip, netdev, linux-kernel

Hi Ryan,

On Wed, Nov 25, 2026 at 5:49 PM Ryan Leung <untilscour@protonmail.com> wrote:
>
> Hi Alexey,
>
> On Tuesday, November 24th, 2026 at 8:04 PM, Alexey Charkov <alchark@flipper.net> wrote:
>
> > Rockchip bootloaders derive stable Ethernet MAC addresses from the CPU
> > ID in the OTP memory and patch them into the device tree they pass on.
> > Kernels started any other way, e.g. straight from the SPL in Falcon
> > mode, end up with random addresses which change on every boot, even
> > though the CPU ID is just as readable from Linux.
> >
> > Implement the derivation for cells marked as "rockchip,cpuid", handing
> > the result to consumers which ask for the cell as "mac-address", such as
> > of_get_mac_address(). Leave the CPU ID unchanged for any other reader,
> > so that the same cell keeps serving the ID itself.
[...]
>
> Perhaps "rockchip,cpuid" could include a mention of "mac" somewhere?

I deliberately avoided that because, at the hardware level, the OTP
cell contains a CPUID, not an address. The special treatment applies
only when the CPUID is used for MAC address derivation - which IMO is
better captured in the binding description. Let's see what the
maintainers have to say though.

> Tested-by: Ryan Leung <untilscour@protonmail.com> # RK3528 NanoPi R28S
>
> I tested on FriendlyELEC NanoPi R28S (RK3528) with index of 100 and the MAC address was exactly as expected.

Thanks a lot for testing!

Best regards,
Alexey

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

* Re: [PATCH v4 3/5] nvmem: rockchip-otp: Derive MAC addresses from the CPU ID
  2026-11-25 13:49   ` Ryan Leung
  2026-11-25 13:55     ` Alexey Charkov
@ 2026-11-25 14:21     ` Andrew Lunn
  2026-11-25 14:41       ` Alexey Charkov
  1 sibling, 1 reply; 14+ messages in thread
From: Andrew Lunn @ 2026-11-25 14:21 UTC (permalink / raw)
  To: Ryan Leung
  Cc: Alexey Charkov, Srinivas Kandagatla, Rob Herring,
	Krzysztof Kozlowski, Conor Dooley, Heiko Stuebner,
	Rafał Miłecki, Heiner Kallweit, Russell King,
	David S. Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni,
	Willem de Bruijn, Simon Horman, Ricardo Pardini, devicetree,
	linux-arm-kernel, linux-rockchip, netdev, linux-kernel

> > +static int rockchip_otp_cpuid_pp(void *priv, const char *id, int index,
> > +				 unsigned int offset, void *buf, size_t bytes)
> > +{
> > +	char cpuid[ROCKCHIP_CPUID_LEN * 2];
> > +	u8 digest[SHA256_DIGEST_SIZE];
> > +	u8 *mac = buf;
> > +
> > +	if (!id || strcmp(id, "mac-address"))
> > +		return 0;

I don't know OTP drivers. But to me, a function called
rockchip_otp_cpuid_pp() which does not actually return the CPU ID is
architecturally wrong.

I would probably not mangle the CPU ID in the OTP driver, but in the
MAC driver. The MAC driver should ask for the CPU ID and then hash it.

    Andrew

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

* Re: [PATCH v4 3/5] nvmem: rockchip-otp: Derive MAC addresses from the CPU ID
  2026-11-25 14:21     ` Andrew Lunn
@ 2026-11-25 14:41       ` Alexey Charkov
  2026-11-25 15:23         ` Andrew Lunn
  0 siblings, 1 reply; 14+ messages in thread
From: Alexey Charkov @ 2026-11-25 14:41 UTC (permalink / raw)
  To: Andrew Lunn
  Cc: Ryan Leung, Srinivas Kandagatla, Rob Herring,
	Krzysztof Kozlowski, Conor Dooley, Heiko Stuebner,
	Rafał Miłecki, Heiner Kallweit, Russell King,
	David S. Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni,
	Willem de Bruijn, Simon Horman, Ricardo Pardini, devicetree,
	linux-arm-kernel, linux-rockchip, netdev, linux-kernel

On Wed, Nov 25, 2026 at 6:21 PM Andrew Lunn <andrew@lunn.ch> wrote:
>
> > > +static int rockchip_otp_cpuid_pp(void *priv, const char *id, int index,
> > > +                            unsigned int offset, void *buf, size_t bytes)
> > > +{
> > > +   char cpuid[ROCKCHIP_CPUID_LEN * 2];
> > > +   u8 digest[SHA256_DIGEST_SIZE];
> > > +   u8 *mac = buf;
> > > +
> > > +   if (!id || strcmp(id, "mac-address"))
> > > +           return 0;
>
> I don't know OTP drivers. But to me, a function called
> rockchip_otp_cpuid_pp() which does not actually return the CPU ID is
> architecturally wrong.

There is precedent for this at least in IMX [1], [2]. To me the
interpretation is "Rockchip CPU ID used as a MAC address base with
offset X", which is exactly what the derivation does. IMO, this is
less clunky than the alternatives (e.g., a custom NVMEM layout which I
used in v1-v2; a "virtual" NVMEM driver referencing the real cell and
returning the processed one, which Ryan proposed in v2 discussion, or
caller-side processing).

[1] https://elixir.bootlin.com/linux/v7.4-rc1/source/drivers/nvmem/imx-ocotp.c#L224-L238
[2] https://elixir.bootlin.com/linux/v7.4-rc1/source/drivers/nvmem/imx-ocotp-ele.c#L115-L129

> I would probably not mangle the CPU ID in the OTP driver, but in the
> MAC driver. The MAC driver should ask for the CPU ID and then hash it.

This would then tie the address derivation logic to a specific MAC
driver, rather than to the platform, and it won't work for boards
using other MACs (such as PCIe connected ones).

Best regards,
Alexey

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

* Re: [PATCH v4 3/5] nvmem: rockchip-otp: Derive MAC addresses from the CPU ID
  2026-11-25 14:41       ` Alexey Charkov
@ 2026-11-25 15:23         ` Andrew Lunn
  2026-11-25 15:58           ` Alexey Charkov
  0 siblings, 1 reply; 14+ messages in thread
From: Andrew Lunn @ 2026-11-25 15:23 UTC (permalink / raw)
  To: Alexey Charkov
  Cc: Ryan Leung, Srinivas Kandagatla, Rob Herring,
	Krzysztof Kozlowski, Conor Dooley, Heiko Stuebner,
	Rafał Miłecki, Heiner Kallweit, Russell King,
	David S. Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni,
	Willem de Bruijn, Simon Horman, Ricardo Pardini, devicetree,
	linux-arm-kernel, linux-rockchip, netdev, linux-kernel

On Wed, Nov 25, 2026 at 06:41:42PM +0400, Alexey Charkov wrote:
> On Wed, Nov 25, 2026 at 6:21 PM Andrew Lunn <andrew@lunn.ch> wrote:
> >
> > > > +static int rockchip_otp_cpuid_pp(void *priv, const char *id, int index,
> > > > +                            unsigned int offset, void *buf, size_t bytes)
> > > > +{
> > > > +   char cpuid[ROCKCHIP_CPUID_LEN * 2];
> > > > +   u8 digest[SHA256_DIGEST_SIZE];
> > > > +   u8 *mac = buf;
> > > > +
> > > > +   if (!id || strcmp(id, "mac-address"))
> > > > +           return 0;
> >
> > I don't know OTP drivers. But to me, a function called
> > rockchip_otp_cpuid_pp() which does not actually return the CPU ID is
> > architecturally wrong.
> 
> There is precedent for this at least in IMX [1], [2]. To me the
> interpretation is "Rockchip CPU ID used as a MAC address base with
> offset X", which is exactly what the derivation does. IMO, this is
> less clunky than the alternatives (e.g., a custom NVMEM layout which I
> used in v1-v2; a "virtual" NVMEM driver referencing the real cell and
> returning the processed one, which Ryan proposed in v2 discussion, or
> caller-side processing).
> 
> [1] https://elixir.bootlin.com/linux/v7.4-rc1/source/drivers/nvmem/imx-ocotp.c#L224-L238

I would say this one is different. The cell specifically contains the
MAC address. But its endianness is wrong. Nothing about CPU ID here.

> [2] https://elixir.bootlin.com/linux/v7.4-rc1/source/drivers/nvmem/imx-ocotp-ele.c#L115-L129

And this seems to be the same.

As i said, i don't like the concept that a function called
rockchip_otp_cpuid_pp() returns something which is not a CPU ID. The
two examples you give ask for a MAC address, and they get a MAC
address.

Does nvmem support aliases? Can you put two different names on the
same cell? If you access the cell via the name cpu-id, it calls a
function _cpu_id_, it gives you the CPU ID. If you access the cell via
the name mac-address, it calls a function called _mac_address_, and it
gives you a MAC address.

That way of_get_mac_address_nvmem() just works, so the MAC driver just
needs to call of_get_mac_address() or of_get_ethdev_address().

      Andrew

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

* Re: [PATCH v4 3/5] nvmem: rockchip-otp: Derive MAC addresses from the CPU ID
  2026-11-25 15:23         ` Andrew Lunn
@ 2026-11-25 15:58           ` Alexey Charkov
  2026-11-25 16:41             ` Andrew Lunn
  0 siblings, 1 reply; 14+ messages in thread
From: Alexey Charkov @ 2026-11-25 15:58 UTC (permalink / raw)
  To: Andrew Lunn
  Cc: Ryan Leung, Srinivas Kandagatla, Rob Herring,
	Krzysztof Kozlowski, Conor Dooley, Heiko Stuebner,
	Rafał Miłecki, Heiner Kallweit, Russell King,
	David S. Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni,
	Willem de Bruijn, Simon Horman, Ricardo Pardini, devicetree,
	linux-arm-kernel, linux-rockchip, netdev, linux-kernel

On Wed, Nov 25, 2026 at 7:23 PM Andrew Lunn <andrew@lunn.ch> wrote:
>
> On Wed, Nov 25, 2026 at 06:41:42PM +0400, Alexey Charkov wrote:
> > On Wed, Nov 25, 2026 at 6:21 PM Andrew Lunn <andrew@lunn.ch> wrote:
> > >
> > > > > +static int rockchip_otp_cpuid_pp(void *priv, const char *id, int index,
> > > > > +                            unsigned int offset, void *buf, size_t bytes)
> > > > > +{
> > > > > +   char cpuid[ROCKCHIP_CPUID_LEN * 2];
> > > > > +   u8 digest[SHA256_DIGEST_SIZE];
> > > > > +   u8 *mac = buf;
> > > > > +
> > > > > +   if (!id || strcmp(id, "mac-address"))
> > > > > +           return 0;
> > >
> > > I don't know OTP drivers. But to me, a function called
> > > rockchip_otp_cpuid_pp() which does not actually return the CPU ID is
> > > architecturally wrong.
> >
> > There is precedent for this at least in IMX [1], [2]. To me the
> > interpretation is "Rockchip CPU ID used as a MAC address base with
> > offset X", which is exactly what the derivation does. IMO, this is
> > less clunky than the alternatives (e.g., a custom NVMEM layout which I
> > used in v1-v2; a "virtual" NVMEM driver referencing the real cell and
> > returning the processed one, which Ryan proposed in v2 discussion, or
> > caller-side processing).
> >
> > [1] https://elixir.bootlin.com/linux/v7.4-rc1/source/drivers/nvmem/imx-ocotp.c#L224-L238
>
> I would say this one is different. The cell specifically contains the
> MAC address. But its endianness is wrong. Nothing about CPU ID here.
>
> > [2] https://elixir.bootlin.com/linux/v7.4-rc1/source/drivers/nvmem/imx-ocotp-ele.c#L115-L129
>
> And this seems to be the same.
>
> As i said, i don't like the concept that a function called
> rockchip_otp_cpuid_pp() returns something which is not a CPU ID. The
> two examples you give ask for a MAC address, and they get a MAC
> address.
>
> Does nvmem support aliases? Can you put two different names on the
> same cell? If you access the cell via the name cpu-id, it calls a
> function _cpu_id_, it gives you the CPU ID. If you access the cell via
> the name mac-address, it calls a function called _mac_address_, and it
> gives you a MAC address.

The cells themselves can't be aliased, but what you're describing is
pretty much what the current code does. The id field is exactly what
the consumer used as the cell name when referencing it, and the
postprocessor callback checks whether it was "mac-address".

Shall I perhaps just drop "cpuid" from the callback name to avoid
confusion? The special treatment is only applied to cells with the
compatible string "rockchip,cpuid" anyway, because only Rockchip SoCs
use this particular algorithm for deriving addresses from cell
contents.

> That way of_get_mac_address_nvmem() just works, so the MAC driver just
> needs to call of_get_mac_address() or of_get_ethdev_address().

They still need to relax the size check as done by patch 2/5 in this
series, because the CPUID cell is 16 bytes long. Srinivas rejected a
prior version of this series that attempted to return cell contents of
different sizes depending on the use case. Currently any cell size !=
ETH_ALEN gets rejected by the net core, not just < ETH_ALEN.

Best regards,
Alexey

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

* Re: [PATCH v4 3/5] nvmem: rockchip-otp: Derive MAC addresses from the CPU ID
  2026-11-25 15:58           ` Alexey Charkov
@ 2026-11-25 16:41             ` Andrew Lunn
  2026-11-25 18:01               ` Alexey Charkov
  0 siblings, 1 reply; 14+ messages in thread
From: Andrew Lunn @ 2026-11-25 16:41 UTC (permalink / raw)
  To: Alexey Charkov
  Cc: Ryan Leung, Srinivas Kandagatla, Rob Herring,
	Krzysztof Kozlowski, Conor Dooley, Heiko Stuebner,
	Rafał Miłecki, Heiner Kallweit, Russell King,
	David S. Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni,
	Willem de Bruijn, Simon Horman, Ricardo Pardini, devicetree,
	linux-arm-kernel, linux-rockchip, netdev, linux-kernel

> > That way of_get_mac_address_nvmem() just works, so the MAC driver just
> > needs to call of_get_mac_address() or of_get_ethdev_address().
> 
> They still need to relax the size check as done by patch 2/5 in this
> series, because the CPUID cell is 16 bytes long. Srinivas rejected a
> prior version of this series that attempted to return cell contents of
> different sizes depending on the use case. Currently any cell size !=
> ETH_ALEN gets rejected by the net core, not just < ETH_ALEN.

Ah! I had not looked at the whole series.

Having to change the core like this is an argument for doing it in the
MAC driver. It asked for a MAC address. What it got back is not a MAC
address if it does not have 6 octets.

Please throw this all away and do it in the MAC driver. If there are
multiple RealTek MAC drivers involved, you can put it into a little
library and share it across the drivers.

It is actually an interesting idea. Maybe it should be placed in
of_net.c, so other drivers can use it. How generic is it having a CPU
ID in the OTP? Are there other vendors doing it?

	Andrew




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

* Re: [PATCH v4 3/5] nvmem: rockchip-otp: Derive MAC addresses from the CPU ID
  2026-11-25 16:41             ` Andrew Lunn
@ 2026-11-25 18:01               ` Alexey Charkov
  0 siblings, 0 replies; 14+ messages in thread
From: Alexey Charkov @ 2026-11-25 18:01 UTC (permalink / raw)
  To: Andrew Lunn
  Cc: Ryan Leung, Srinivas Kandagatla, Rob Herring,
	Krzysztof Kozlowski, Conor Dooley, Heiko Stuebner,
	Rafał Miłecki, Heiner Kallweit, Russell King,
	David S. Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni,
	Willem de Bruijn, Simon Horman, Ricardo Pardini, devicetree,
	linux-arm-kernel, linux-rockchip, netdev, linux-kernel

On Wed, Nov 25, 2026 at 8:41 PM Andrew Lunn <andrew@lunn.ch> wrote:
>
> > > That way of_get_mac_address_nvmem() just works, so the MAC driver just
> > > needs to call of_get_mac_address() or of_get_ethdev_address().
> >
> > They still need to relax the size check as done by patch 2/5 in this
> > series, because the CPUID cell is 16 bytes long. Srinivas rejected a
> > prior version of this series that attempted to return cell contents of
> > different sizes depending on the use case. Currently any cell size !=
> > ETH_ALEN gets rejected by the net core, not just < ETH_ALEN.
>
> Ah! I had not looked at the whole series.
>
> Having to change the core like this is an argument for doing it in the
> MAC driver. It asked for a MAC address. What it got back is not a MAC
> address if it does not have 6 octets.
>
> Please throw this all away and do it in the MAC driver. If there are
> multiple RealTek MAC drivers involved, you can put it into a little
> library and share it across the drivers.
>
> It is actually an interesting idea. Maybe it should be placed in
> of_net.c, so other drivers can use it. How generic is it having a CPU
> ID in the OTP? Are there other vendors doing it?

From a quick overview, Allwinner does a similar thing [1] by reading a
"SID" from its eFuses, and Amlogic [2] by reading its secure-monitor
serial number - so it's a reasonably common pattern. Everyone uses
their own derivation functions though.

I think I could try and add another branch in of_net.c to parse an
NVMEM cell called e.g. "unique-id" with platform-specified derivation
functions selected by the cell's compatible string. Then derive the
MAC index from the OF alias, so that the phandle argument can go away,
and the numbering cannot drift apart. Will send a v5 with that
approach if it works out.

[1] https://elixir.bootlin.com/u-boot/v2027.01-rc2/source/board/sunxi/board.c#L783-L828
[2] https://elixir.bootlin.com/u-boot/v2027.01-rc2/source/arch/arm/mach-meson/board-common.c#L79-L107

Best regards,
Alexey

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

end of thread, other threads:[~2026-11-25 18:01 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-24 12:04 [PATCH v4 0/5] nvmem: Derive Rockchip MAC addresses from the OTP CPU ID Alexey Charkov
2026-11-24 12:04 ` [PATCH v4 1/5] dt-bindings: nvmem: fixed-cell: Add Rockchip " Alexey Charkov
2026-11-24 12:04 ` [PATCH v4 2/5] net: Accept MAC addresses from longer NVMEM cells Alexey Charkov
2026-11-24 12:04 ` [PATCH v4 3/5] nvmem: rockchip-otp: Derive MAC addresses from the CPU ID Alexey Charkov
2026-11-25 13:49   ` Ryan Leung
2026-11-25 13:55     ` Alexey Charkov
2026-11-25 14:21     ` Andrew Lunn
2026-11-25 14:41       ` Alexey Charkov
2026-11-25 15:23         ` Andrew Lunn
2026-11-25 15:58           ` Alexey Charkov
2026-11-25 16:41             ` Andrew Lunn
2026-11-25 18:01               ` Alexey Charkov
2026-11-24 12:04 ` [PATCH v4 4/5] arm64: dts: rockchip: Move RK3576 OTP cells into a fixed layout Alexey Charkov
2026-11-24 12:04 ` [PATCH v4 5/5] arm64: dts: rockchip: Derive GMAC MAC addresses from OTP on RK3576 Alexey Charkov

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

all inboxes | Powered by JetHome®