[PATCH v5 2/5] nvmem: layouts: add Qualcomm eFuse Ethernet MAC address layout driver
From: Hongmei Xu
Date: Fri Dec 04 2026 - 01:22:47 EST
Add an NVMEM layout driver for reading Ethernet MAC addresses from
Qualcomm eFuse (QFPROM). The driver supports two cell layouts:
- Contiguous: six consecutive eFuse bytes form the MAC directly.
- Non-contiguous: a byte-mask device tree property selects which
physical bytes within the eFuse span contribute to the output MAC.
Bit i set means byte i is included; selected bytes are compacted
in ascending bit-index order into a 6-byte output.
QFPROM hardware requires 4-byte word-aligned reads. For non-contiguous
cells the actual read length is ALIGN(fls(byte_mask), 4); for
contiguous cells it is ALIGN(ETH_ALEN, 4). Both paths are validated
against the eFuse device size at layout probe time to prevent
out-of-bounds hardware accesses.
Signed-off-by: Hongmei Xu <hongmei.xu@xxxxxxxxxxxxxxxx>
---
drivers/nvmem/layouts/Kconfig | 11 ++
drivers/nvmem/layouts/Makefile | 1 +
drivers/nvmem/layouts/qcom-eth-mac.c | 203 +++++++++++++++++++++++++++++++++++
3 files changed, 215 insertions(+)
diff --git a/drivers/nvmem/layouts/Kconfig b/drivers/nvmem/layouts/Kconfig
index 5e586dfebe47..d60bce199b8e 100644
--- a/drivers/nvmem/layouts/Kconfig
+++ b/drivers/nvmem/layouts/Kconfig
@@ -37,6 +37,17 @@ config NVMEM_LAYOUT_U_BOOT_ENV
If unsure, say N.
+config NVMEM_LAYOUT_QCOM_ETH_MAC
+ tristate "Qualcomm Ethernet MAC address layout"
+ depends on OF
+ help
+ Reads Ethernet MAC addresses from Qualcomm eFuse (QFPROM).
+ Supports cells with non-contiguous byte layouts via an optional
+ byte-mask property that selects and compacts physical eFuse bytes
+ into the 6-byte output MAC address.
+
+ Say Y if your Qualcomm SoC stores Ethernet MAC addresses in eFuse.
+
endmenu
endif
diff --git a/drivers/nvmem/layouts/Makefile b/drivers/nvmem/layouts/Makefile
index dd6c6c70b1a9..127a4aae420d 100644
--- a/drivers/nvmem/layouts/Makefile
+++ b/drivers/nvmem/layouts/Makefile
@@ -7,3 +7,4 @@ obj-$(CONFIG_NVMEM_LAYOUTS) += fixed-layout.o
obj-$(CONFIG_NVMEM_LAYOUT_SL28_VPD) += sl28vpd.o
obj-$(CONFIG_NVMEM_LAYOUT_ONIE_TLV) += onie-tlv.o
obj-$(CONFIG_NVMEM_LAYOUT_U_BOOT_ENV) += u-boot-env.o
+obj-$(CONFIG_NVMEM_LAYOUT_QCOM_ETH_MAC) += qcom-eth-mac.o
diff --git a/drivers/nvmem/layouts/qcom-eth-mac.c b/drivers/nvmem/layouts/qcom-eth-mac.c
new file mode 100644
index 000000000000..990b096a0a5f
--- /dev/null
+++ b/drivers/nvmem/layouts/qcom-eth-mac.c
@@ -0,0 +1,203 @@
+// SPDX-License-Identifier: GPL-2.0-only
+// Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries.
+/*
+ * Qualcomm Ethernet MAC address nvmem layout driver
+ *
+ * Each cell node carries an optional "byte-mask" — a bitmask where bit i
+ * means physical byte i in the eFuse span contributes to the output MAC.
+ * Valid bytes are compacted in ascending bit-index order into output
+ * positions 0..hweight(byte-mask)-1; remaining output bytes are zeroed.
+ * Without "byte-mask" the cell is read as a plain contiguous ETH_ALEN block.
+ *
+ * Example (SA8797P eth1 — physical bytes 0-4,6 are the MAC, byte 5 belongs
+ * to eth0 and must be skipped):
+ * reg = <0x0 0x7>; // physical span: bytes 0-6
+ * byte-mask = <0x5f>; // bits 0,1,2,3,4,6 → output[0..5]
+ *
+ * QFPROM reads are 4-byte word-aligned; raw_len is rounded up accordingly.
+ */
+#include <linux/bitops.h>
+#include <linux/etherdevice.h>
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/nvmem-consumer.h>
+#include <linux/nvmem-provider.h>
+#include <linux/of.h>
+#include <linux/slab.h>
+
+/* QFPROM register reads are 4-byte word aligned */
+#define QCOM_QFPROM_WORD_SIZE 4U
+
+struct qcom_eth_mac_priv {
+ u32 byte_mask;
+ u32 raw_len;
+ struct device *dev;
+};
+
+static int qcom_eth_mac_post_process(void *priv, const char *id, int index,
+ unsigned int offset, void *buf, size_t bytes)
+{
+ struct qcom_eth_mac_priv *p = priv;
+ u8 *out = buf;
+ u32 out_idx = 0;
+ u8 *raw;
+ u32 i;
+
+ raw = kmemdup(buf, p->raw_len, GFP_KERNEL);
+ if (!raw)
+ return -ENOMEM;
+
+ memset(out, 0, bytes);
+
+ for (i = 0; i < p->raw_len && out_idx < bytes; i++) {
+ if (p->byte_mask & BIT(i))
+ out[out_idx++] = raw[i];
+ }
+
+ kfree(raw);
+ dev_dbg(p->dev, "post_process %s: mask=0x%02x out=%pM\n",
+ id, p->byte_mask, out);
+ return 0;
+}
+
+static int qcom_eth_mac_add_cells(struct nvmem_layout *layout)
+{
+ struct nvmem_device *nvmem = layout->nvmem;
+ struct device *dev = &layout->dev;
+ struct device_node *layout_node;
+ struct device_node *child;
+ int ret = 0;
+
+ layout_node = of_nvmem_layout_get_container(nvmem);
+ if (!layout_node) {
+ dev_err(dev, "add_cells: no nvmem-layout container node\n");
+ return -ENOENT;
+ }
+
+ for_each_child_of_node(layout_node, child) {
+ struct nvmem_cell_info info = {};
+ u32 reg[2];
+ u32 byte_mask = 0;
+
+ if (of_property_read_u32_array(child, "reg", reg, 2)) {
+ dev_err(dev, "missing reg in %pOF\n", child);
+ ret = -EINVAL;
+ goto out;
+ }
+
+ info.name = child->name;
+ info.offset = reg[0];
+ info.bytes = ETH_ALEN;
+ info.np = of_node_get(child);
+
+ of_property_read_u32(child, "byte-mask", &byte_mask);
+
+ if (byte_mask) {
+ struct qcom_eth_mac_priv *priv;
+ u32 raw_len;
+
+ if (hweight32(byte_mask) > ETH_ALEN) {
+ dev_err(dev, "%pOF: byte-mask 0x%x has more than %d bits set\n",
+ child, byte_mask, ETH_ALEN);
+ of_node_put(info.np);
+ ret = -EINVAL;
+ goto out;
+ }
+
+ raw_len = ALIGN((u32)fls(byte_mask), QCOM_QFPROM_WORD_SIZE);
+
+ dev_dbg(dev, "%pOF: byte_mask=0x%x hweight=%d raw_len=%u\n",
+ child, byte_mask, hweight32(byte_mask), raw_len);
+
+ if (info.offset + raw_len > nvmem_dev_size(nvmem)) {
+ dev_err(dev, "%pOF: offset %u + raw_len %u exceeds device size %zu\n",
+ child, info.offset, raw_len,
+ nvmem_dev_size(nvmem));
+ of_node_put(info.np);
+ ret = -EINVAL;
+ goto out;
+ }
+
+ priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
+ if (!priv) {
+ of_node_put(info.np);
+ ret = -ENOMEM;
+ goto out;
+ }
+
+ priv->byte_mask = byte_mask;
+ priv->raw_len = raw_len;
+ priv->dev = dev;
+ info.raw_len = raw_len;
+ info.read_post_process = qcom_eth_mac_post_process;
+ info.priv = priv;
+ } else {
+ /* contiguous cell: word-align the read to avoid read overlap */
+ info.raw_len = ALIGN(ETH_ALEN, QCOM_QFPROM_WORD_SIZE);
+
+ if (info.offset + info.raw_len > nvmem_dev_size(nvmem)) {
+ dev_err(dev, "%pOF: offset %u + %zu exceeds device size %zu\n",
+ child, info.offset, info.raw_len,
+ nvmem_dev_size(nvmem));
+ of_node_put(info.np);
+ ret = -EINVAL;
+ goto out;
+ }
+ }
+
+ ret = nvmem_add_one_cell(nvmem, &info);
+ if (ret) {
+ of_node_put(info.np);
+ dev_err(dev, "failed to add cell %s: %d\n", info.name, ret);
+ goto out;
+ }
+
+ dev_dbg(dev, "%pOF: added cell '%s' offset=%u bytes=%u raw_len=%zu\n",
+ child, info.name, info.offset, info.bytes, info.raw_len);
+
+ /*
+ * Mark the cell fwnode as not-a-device so fw_devlink stops
+ * blocking ethernet on this supplier link. Must be called
+ * after nvmem_add_one_cell() so the fwnode exists.
+ * fw_devlink_refresh_fwnode() cannot be used here because the
+ * efuse device is still in probe and not yet DL_DEV_DRIVER_BOUND.
+ */
+ fw_devlink_purge_absent_suppliers(of_fwnode_handle(child));
+ }
+
+out:
+ of_node_put(child);
+ of_node_put(layout_node);
+ return ret;
+}
+
+static int qcom_eth_mac_probe(struct nvmem_layout *layout)
+{
+ layout->add_cells = qcom_eth_mac_add_cells;
+
+ return nvmem_layout_register(layout);
+}
+
+static void qcom_eth_mac_remove(struct nvmem_layout *layout)
+{
+ nvmem_layout_unregister(layout);
+}
+
+static const struct of_device_id qcom_eth_mac_of_match[] = {
+ { .compatible = "qcom,efuse-eth-mac" },
+ { }
+};
+MODULE_DEVICE_TABLE(of, qcom_eth_mac_of_match);
+
+static struct nvmem_layout_driver qcom_eth_mac_layout = {
+ .driver = {
+ .name = "qcom-eth-mac-layout",
+ .of_match_table = qcom_eth_mac_of_match,
+ },
+ .probe = qcom_eth_mac_probe,
+ .remove = qcom_eth_mac_remove,
+};
+module_nvmem_layout_driver(qcom_eth_mac_layout);
+
+MODULE_DESCRIPTION("Qualcomm Ethernet MAC nvmem layout driver");
+MODULE_LICENSE("GPL");
--
2.34.1