The qnap-mcu also has an eeprom connected to it, that contains some
specific product-information like the mac addresses for the network
interfaces.
Add a nvmem driver for it.
Signed-off-by: Heiko Stuebner <heiko@sntech.de>
Acked-by: Srinivas Kandagatla <srini@kernel.org>
---
drivers/nvmem/Kconfig | 9 +++
drivers/nvmem/Makefile | 2 +
drivers/nvmem/qnap-mcu-eeprom.c | 110 ++++++++++++++++++++++++++++++++
3 files changed, 121 insertions(+)
create mode 100644 drivers/nvmem/qnap-mcu-eeprom.c
diff --git a/drivers/nvmem/Kconfig b/drivers/nvmem/Kconfig
index e0d88d3199c1..bf47a982cf62 100644
--- a/drivers/nvmem/Kconfig
+++ b/drivers/nvmem/Kconfig
@@ -285,6 +285,15 @@ config NVMEM_QCOM_SEC_QFPROM
This driver can also be built as a module. If so, the module will be called
nvmem_sec_qfprom.
+config NVMEM_QNAP_MCU_EEPROM
+ tristate "QNAP MCU EEPROM Support"
+ depends on MFD_QNAP_MCU
+ help
+ Say y here to enable support for accessing the EEPROM attached to
+ QNAP MCU devices. This EEPROM contains additional runtime device
+ information, like MAC addresses for ethernet devices that do not
+ contain their own mac storage.
+
config NVMEM_RAVE_SP_EEPROM
tristate "Rave SP EEPROM Support"
depends on RAVE_SP_CORE
diff --git a/drivers/nvmem/Makefile b/drivers/nvmem/Makefile
index 70a4464dcb1e..7252b8ec88d4 100644
--- a/drivers/nvmem/Makefile
+++ b/drivers/nvmem/Makefile
@@ -56,6 +56,8 @@ obj-$(CONFIG_NVMEM_QCOM_QFPROM) += nvmem_qfprom.o
nvmem_qfprom-y := qfprom.o
obj-$(CONFIG_NVMEM_QCOM_SEC_QFPROM) += nvmem_sec_qfprom.o
nvmem_sec_qfprom-y := sec-qfprom.o
+obj-$(CONFIG_NVMEM_QNAP_MCU_EEPROM) += nvmem-qnap-mcu-eeprom.o
+nvmem-qnap-mcu-eeprom-y := qnap-mcu-eeprom.o
obj-$(CONFIG_NVMEM_RAVE_SP_EEPROM) += nvmem-rave-sp-eeprom.o
nvmem-rave-sp-eeprom-y := rave-sp-eeprom.o
obj-$(CONFIG_NVMEM_RCAR_EFUSE) += nvmem-rcar-efuse.o
diff --git a/drivers/nvmem/qnap-mcu-eeprom.c b/drivers/nvmem/qnap-mcu-eeprom.c
new file mode 100644
index 000000000000..fea1e7b91764
--- /dev/null
+++ b/drivers/nvmem/qnap-mcu-eeprom.c
@@ -0,0 +1,110 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * ee1004 - driver for DDR4 SPD EEPROMs
+ *
+ * Copyright (C) 2017-2019 Jean Delvare
+ *
+ * Based on the at24 driver:
+ * Copyright (C) 2005-2007 David Brownell
+ * Copyright (C) 2008 Wolfram Sang, Pengutronix
+ */
+
+#include <linux/mfd/qnap-mcu.h>
+#include <linux/module.h>
+#include <linux/nvmem-provider.h>
+#include <linux/platform_device.h>
+
+/* Determined by trial and error until read anomalies appeared */
+#define QNAP_MCU_EEPROM_SIZE 256
+#define QNAP_MCU_EEPROM_BLOCK_SIZE 32
+
+static int qnap_mcu_eeprom_read_block(struct qnap_mcu *mcu, unsigned int offset,
+ void *val, size_t bytes)
+{
+ const u8 cmd[] = { 0xf7, 0xa1, offset, bytes };
+ u8 *reply;
+ int ret = 0;
+
+ reply = kzalloc(bytes + sizeof(cmd), GFP_KERNEL);
+ if (!reply)
+ return -ENOMEM;
+
+ ret = qnap_mcu_exec(mcu, cmd, sizeof(cmd), reply, bytes + sizeof(cmd));
+ if (ret)
+ goto out;
+
+ /* First bytes must mirror the sent command */
+ if (memcmp(cmd, reply, sizeof(cmd))) {
+ ret = -EIO;
+ goto out;
+ }
+
+ memcpy(val, reply + sizeof(cmd), bytes);
+
+out:
+ kfree(reply);
+ return ret;
+}
+
+static int qnap_mcu_eeprom_read(void *priv, unsigned int offset, void *val, size_t bytes)
+{
+ struct qnap_mcu *mcu = priv;
+ int pos = 0, ret;
+ u8 *buf = val;
+
+ if (unlikely(!bytes))
+ return 0;
+
+ while (bytes > 0) {
+ size_t to_read = (bytes > QNAP_MCU_EEPROM_BLOCK_SIZE) ?
+ QNAP_MCU_EEPROM_BLOCK_SIZE : bytes;
+
+ ret = qnap_mcu_eeprom_read_block(mcu, offset + pos, &buf[pos], to_read);
+ if (ret < 0)
+ return ret;
+
+ pos += to_read;
+ bytes -= to_read;
+ }
+
+ return 0;
+}
+
+static int qnap_mcu_eeprom_probe(struct platform_device *pdev)
+{
+ struct qnap_mcu *mcu = dev_get_drvdata(pdev->dev.parent);
+ struct nvmem_config nvcfg = {};
+ struct nvmem_device *ndev;
+
+ nvcfg.dev = &pdev->dev;
+ nvcfg.of_node = pdev->dev.parent->of_node;
+ nvcfg.name = dev_name(&pdev->dev);
+ nvcfg.id = NVMEM_DEVID_NONE;
+ nvcfg.owner = THIS_MODULE;
+ nvcfg.type = NVMEM_TYPE_EEPROM;
+ nvcfg.read_only = true;
+ nvcfg.root_only = false;
+ nvcfg.reg_read = qnap_mcu_eeprom_read;
+ nvcfg.size = QNAP_MCU_EEPROM_SIZE,
+ nvcfg.word_size = 1,
+ nvcfg.stride = 1,
+ nvcfg.priv = mcu,
+
+ ndev = devm_nvmem_register(&pdev->dev, &nvcfg);
+ if (IS_ERR(ndev))
+ return PTR_ERR(ndev);
+
+ return 0;
+}
+
+static struct platform_driver qnap_mcu_eeprom_driver = {
+ .probe = qnap_mcu_eeprom_probe,
+ .driver = {
+ .name = "qnap-mcu-eeprom",
+ },
+};
+module_platform_driver(qnap_mcu_eeprom_driver);
+
+MODULE_AUTHOR("Heiko Stuebner <heiko@sntech.de>");
+MODULE_DESCRIPTION("QNAP MCU EEPROM driver");
+MODULE_LICENSE("GPL");
--
2.47.2
Hi Heiko,
kernel test robot noticed the following build errors:
[auto build test ERROR on lee-leds/for-leds-next]
[also build test ERROR on lee-mfd/for-mfd-next lee-mfd/for-mfd-fixes linus/master v6.18-rc4 next-20251103]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]
url: https://github.com/intel-lab-lkp/linux/commits/Heiko-Stuebner/nvmem-Add-driver-for-the-eeprom-in-qnap-mcu-controllers/20251103-004523
base: https://git.kernel.org/pub/scm/linux/kernel/git/lee/leds.git for-leds-next
patch link: https://lore.kernel.org/r/20251102163955.294427-2-heiko%40sntech.de
patch subject: [PATCH v3 1/2] nvmem: Add driver for the eeprom in qnap-mcu controllers
config: hexagon-allmodconfig (https://download.01.org/0day-ci/archive/20251104/202511040659.c2R6OqbJ-lkp@intel.com/config)
compiler: clang version 17.0.6 (https://github.com/llvm/llvm-project 6009708b4367171ccdbf4b5905cb6a803753fe18)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20251104/202511040659.c2R6OqbJ-lkp@intel.com/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <lkp@intel.com>
| Closes: https://lore.kernel.org/oe-kbuild-all/202511040659.c2R6OqbJ-lkp@intel.com/
All errors (new ones prefixed by >>):
>> drivers/nvmem/qnap-mcu-eeprom.c:28:10: error: call to undeclared function 'kzalloc'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
28 | reply = kzalloc(bytes + sizeof(cmd), GFP_KERNEL);
| ^
>> drivers/nvmem/qnap-mcu-eeprom.c:28:8: error: incompatible integer to pointer conversion assigning to 'u8 *' (aka 'unsigned char *') from 'int' [-Wint-conversion]
28 | reply = kzalloc(bytes + sizeof(cmd), GFP_KERNEL);
| ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>> drivers/nvmem/qnap-mcu-eeprom.c:45:2: error: call to undeclared function 'kfree'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
45 | kfree(reply);
| ^
3 errors generated.
vim +/kzalloc +28 drivers/nvmem/qnap-mcu-eeprom.c
20
21 static int qnap_mcu_eeprom_read_block(struct qnap_mcu *mcu, unsigned int offset,
22 void *val, size_t bytes)
23 {
24 const u8 cmd[] = { 0xf7, 0xa1, offset, bytes };
25 u8 *reply;
26 int ret = 0;
27
> 28 reply = kzalloc(bytes + sizeof(cmd), GFP_KERNEL);
29 if (!reply)
30 return -ENOMEM;
31
32 ret = qnap_mcu_exec(mcu, cmd, sizeof(cmd), reply, bytes + sizeof(cmd));
33 if (ret)
34 goto out;
35
36 /* First bytes must mirror the sent command */
37 if (memcmp(cmd, reply, sizeof(cmd))) {
38 ret = -EIO;
39 goto out;
40 }
41
42 memcpy(val, reply + sizeof(cmd), bytes);
43
44 out:
> 45 kfree(reply);
46 return ret;
47 }
48
--
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
Hi Heiko,
kernel test robot noticed the following build errors:
[auto build test ERROR on lee-leds/for-leds-next]
[also build test ERROR on lee-mfd/for-mfd-next lee-mfd/for-mfd-fixes linus/master v6.18-rc4 next-20251103]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]
url: https://github.com/intel-lab-lkp/linux/commits/Heiko-Stuebner/nvmem-Add-driver-for-the-eeprom-in-qnap-mcu-controllers/20251103-004523
base: https://git.kernel.org/pub/scm/linux/kernel/git/lee/leds.git for-leds-next
patch link: https://lore.kernel.org/r/20251102163955.294427-2-heiko%40sntech.de
patch subject: [PATCH v3 1/2] nvmem: Add driver for the eeprom in qnap-mcu controllers
config: sh-allmodconfig (https://download.01.org/0day-ci/archive/20251103/202511031906.3aAxVbZO-lkp@intel.com/config)
compiler: sh4-linux-gcc (GCC) 15.1.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20251103/202511031906.3aAxVbZO-lkp@intel.com/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <lkp@intel.com>
| Closes: https://lore.kernel.org/oe-kbuild-all/202511031906.3aAxVbZO-lkp@intel.com/
All errors (new ones prefixed by >>):
drivers/nvmem/qnap-mcu-eeprom.c: In function 'qnap_mcu_eeprom_read_block':
>> drivers/nvmem/qnap-mcu-eeprom.c:28:17: error: implicit declaration of function 'kzalloc' [-Wimplicit-function-declaration]
28 | reply = kzalloc(bytes + sizeof(cmd), GFP_KERNEL);
| ^~~~~~~
>> drivers/nvmem/qnap-mcu-eeprom.c:28:15: error: assignment to 'u8 *' {aka 'unsigned char *'} from 'int' makes pointer from integer without a cast [-Wint-conversion]
28 | reply = kzalloc(bytes + sizeof(cmd), GFP_KERNEL);
| ^
>> drivers/nvmem/qnap-mcu-eeprom.c:45:9: error: implicit declaration of function 'kfree' [-Wimplicit-function-declaration]
45 | kfree(reply);
| ^~~~~
vim +/kzalloc +28 drivers/nvmem/qnap-mcu-eeprom.c
20
21 static int qnap_mcu_eeprom_read_block(struct qnap_mcu *mcu, unsigned int offset,
22 void *val, size_t bytes)
23 {
24 const u8 cmd[] = { 0xf7, 0xa1, offset, bytes };
25 u8 *reply;
26 int ret = 0;
27
> 28 reply = kzalloc(bytes + sizeof(cmd), GFP_KERNEL);
29 if (!reply)
30 return -ENOMEM;
31
32 ret = qnap_mcu_exec(mcu, cmd, sizeof(cmd), reply, bytes + sizeof(cmd));
33 if (ret)
34 goto out;
35
36 /* First bytes must mirror the sent command */
37 if (memcmp(cmd, reply, sizeof(cmd))) {
38 ret = -EIO;
39 goto out;
40 }
41
42 memcpy(val, reply + sizeof(cmd), bytes);
43
44 out:
> 45 kfree(reply);
46 return ret;
47 }
48
--
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
© 2016 - 2026 Red Hat, Inc.