[PATCH v2 3/8] soc: renesas: Add R-Car fuse driver
From: Geert Uytterhoeven
Date: Wed May 29 2024 - 05:31:01 EST
R-Car Gen3/Gen4 SoCs contain fuses indicating hardware support or
hardware parameters. Add a driver to access the state of the fuses.
This supports three type of hardware fuse provides:
1. E-FUSE non-volatile memory accessible through the Pin Function
Controller on R-Car V3U and S4-8,
2. E-FUSE non-volatile memory accessible through OTP_MEM on R-Car V4H
and V4M,
3. Fuses tightly integrated with the Pin Function Controller on R-Car
Gen3 SoCs.
Types 1 and 2 use hardware descriptions in DT.
Type 3 relies on a platform device with accompanying platform data, to
be provided by the PFC driver.
Two APIs are provided to read the state of the fuses:
- An kernelspace API (rcar_fuse_read()), to be used by e.g. the
Renesas UFSHCD driver,
Symbolic register indices are provided for convenience.
- A userspace API, using the NVMEM framework.
Signed-off-by: Geert Uytterhoeven <geert+renesas@xxxxxxxxx>
Reviewed-by: Yoshihiro Shimoda <yoshihiro.shimoda.uh@xxxxxxxxxxx>
---
v2:
- Add Reviewed-by.
Questions:
- Should the NVMEM support be made optional, relaxing the dependency
on NVMEM?
- Currently, the NVMEM access is restricted to the root user.
Should his be relaxed?
- On R-Car S4, the full space from RCAR_FUSE_MON0 to RCAR_LTM0_MON2 is
exposed. Should this be split into separate blocks?
---
drivers/soc/renesas/Kconfig | 8 +
drivers/soc/renesas/Makefile | 1 +
drivers/soc/renesas/rcar-fuse.c | 201 ++++++++++++++++++++++++
include/linux/platform_data/rcar_fuse.h | 11 ++
include/linux/soc/renesas/rcar-fuse.h | 41 +++++
5 files changed, 262 insertions(+)
create mode 100644 drivers/soc/renesas/rcar-fuse.c
create mode 100644 include/linux/platform_data/rcar_fuse.h
create mode 100644 include/linux/soc/renesas/rcar-fuse.h
diff --git a/drivers/soc/renesas/Kconfig b/drivers/soc/renesas/Kconfig
index 3125ab575b60b8d1..13aadcf1dec7b8b8 100644
--- a/drivers/soc/renesas/Kconfig
+++ b/drivers/soc/renesas/Kconfig
@@ -376,6 +376,14 @@ config ARCH_R9A07G043
endif # RISCV
+config FUSE_RCAR
+ tristate "R-Car Fuse support"
+ depends on (ARCH_RENESAS && ARM64) || COMPILE_TEST
+ depends on NVMEM
+ help
+ Enable support for reading the fuses in the PFC, E-FUSE or OTP
+ non-volatile memory block on R-Car Gen3/Gen4 SoCs.
+
config PWC_RZV2M
bool "Renesas RZ/V2M PWC support" if COMPILE_TEST
diff --git a/drivers/soc/renesas/Makefile b/drivers/soc/renesas/Makefile
index 725eedd9d73ceae7..8ff382a82ac94496 100644
--- a/drivers/soc/renesas/Makefile
+++ b/drivers/soc/renesas/Makefile
@@ -8,5 +8,6 @@ obj-$(CONFIG_ARCH_R9A06G032) += r9a06g032-smp.o
endif
# Family
+obj-$(CONFIG_FUSE_RCAR) += rcar-fuse.o
obj-$(CONFIG_PWC_RZV2M) += pwc-rzv2m.o
obj-$(CONFIG_RST_RCAR) += rcar-rst.o
diff --git a/drivers/soc/renesas/rcar-fuse.c b/drivers/soc/renesas/rcar-fuse.c
new file mode 100644
index 0000000000000000..8fb2374f919b83e5
--- /dev/null
+++ b/drivers/soc/renesas/rcar-fuse.c
@@ -0,0 +1,201 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * R-Car Gen3/Gen4 E-FUSE/OTP Driver
+ *
+ * Copyright (C) 2024 Glider bv
+ */
+
+#include <linux/cleanup.h>
+#include <linux/device.h>
+#include <linux/export.h>
+#include <linux/io.h>
+#include <linux/mod_devicetable.h>
+#include <linux/mutex.h>
+#include <linux/nvmem-provider.h>
+#include <linux/platform_data/rcar_fuse.h>
+#include <linux/platform_device.h>
+#include <linux/pm_runtime.h>
+#include <linux/property.h>
+#include <linux/soc/renesas/rcar-fuse.h>
+
+struct rcar_fuse {
+ struct device *dev;
+ void __iomem *base;
+ unsigned int offset;
+ unsigned int nregs;
+ struct nvmem_device *nvmem;
+};
+
+struct rcar_fuse_data {
+ unsigned int bank; /* 0: PFC + E-FUSE, 1: OPT_MEM + E-FUSE */
+ unsigned int offset;
+ unsigned int nregs;
+};
+
+/* NVMEM access must not use the rcar_fuse singleton */
+static int rcar_fuse_nvmem_read(void *priv, unsigned int offset, void *val,
+ size_t bytes)
+{
+ struct rcar_fuse *fuse = priv;
+ u32 *buf = val;
+ int ret;
+
+ ret = pm_runtime_resume_and_get(fuse->dev);
+ if (ret < 0)
+ return ret;
+
+ for (; bytes >= 4; bytes -= 4, offset += 4)
+ *buf++ = readl(fuse->base + fuse->offset + offset);
+
+ pm_runtime_put(fuse->dev);
+
+ return 0;
+}
+
+static struct rcar_fuse *rcar_fuse;
+static DEFINE_MUTEX(rcar_fuse_lock); /* Protects rcar_fuse singleton */
+
+int rcar_fuse_read(unsigned int idx, u32 *val)
+{
+ int ret;
+
+ guard(mutex)(&rcar_fuse_lock);
+
+ if (!rcar_fuse)
+ return -EPROBE_DEFER;
+
+ if (idx >= rcar_fuse->nregs)
+ return -EINVAL;
+
+ ret = pm_runtime_resume_and_get(rcar_fuse->dev);
+ if (ret < 0)
+ return ret;
+
+ *val = readl(rcar_fuse->base + rcar_fuse->offset + idx * sizeof(u32));
+
+ pm_runtime_put(rcar_fuse->dev);
+
+ return 0;
+}
+EXPORT_SYMBOL_GPL(rcar_fuse_read);
+
+static int rcar_fuse_probe(struct platform_device *pdev)
+{
+ const struct rcar_fuse_platform_data *pdata;
+ const struct rcar_fuse_data *data;
+ struct device *dev = &pdev->dev;
+ struct nvmem_config nvmem;
+ struct rcar_fuse *fuse;
+ int ret;
+
+ guard(mutex)(&rcar_fuse_lock);
+
+ if (rcar_fuse)
+ return -EEXIST;
+
+ ret = devm_pm_runtime_enable(dev);
+ if (ret < 0)
+ return ret;
+
+ fuse = devm_kzalloc(dev, sizeof(*fuse), GFP_KERNEL);
+ if (!fuse)
+ return -ENOMEM;
+
+ fuse->dev = dev;
+
+ data = device_get_match_data(dev);
+ if (!data) {
+ /* Fuse block integrated into PFC */
+ pdata = dev->platform_data;
+ if (!pdata)
+ return -EINVAL;
+
+ fuse->base = pdata->base;
+ fuse->offset = pdata->offset;
+ fuse->nregs = pdata->nregs;
+ } else {
+ /* PFC + E-FUSE or OTP_MEM + E-FUSE */
+ fuse->base = devm_platform_ioremap_resource(pdev, data->bank);
+ if (IS_ERR(fuse->base))
+ return PTR_ERR(fuse->base);
+
+ fuse->offset = data->offset;
+ fuse->nregs = data->nregs;
+ };
+
+ memset(&nvmem, 0, sizeof(nvmem));
+ nvmem.dev = dev;
+ nvmem.name = "fuse";
+ nvmem.id = -1;
+ nvmem.owner = THIS_MODULE;
+ nvmem.type = NVMEM_TYPE_OTP;
+ nvmem.read_only = true;
+ nvmem.root_only = true;
+ nvmem.reg_read = rcar_fuse_nvmem_read;
+ nvmem.size = fuse->nregs * 4;
+ nvmem.word_size = 4;
+ nvmem.stride = 4;
+ nvmem.priv = fuse;
+
+ fuse->nvmem = devm_nvmem_register(dev, &nvmem);
+ if (IS_ERR(fuse->nvmem))
+ return dev_err_probe(dev, PTR_ERR(fuse->nvmem),
+ "failed to register NVMEM device\n");
+
+ rcar_fuse = fuse;
+
+ return 0;
+}
+
+static void rcar_fuse_remove(struct platform_device *pdev)
+{
+ guard(mutex)(&rcar_fuse_lock);
+
+ rcar_fuse = NULL;
+}
+
+static const struct rcar_fuse_data rcar_fuse_v3u = {
+ .bank = 0,
+ .offset = 0xc0,
+ .nregs = 10,
+};
+
+static const struct rcar_fuse_data rcar_fuse_s4 = {
+ .bank = 0,
+ .offset = 0xc0,
+ .nregs = 35,
+};
+
+static const struct rcar_fuse_data rcar_fuse_v4h = {
+ .bank = 1,
+ .offset = 0x100,
+ .nregs = 40
+};
+
+static const struct rcar_fuse_data rcar_fuse_v4m = {
+ .bank = 1,
+ .offset = 0x100,
+ .nregs = 4,
+};
+
+static const struct of_device_id rcar_fuse_match[] = {
+ { .compatible = "renesas,r8a779a0-efuse", .data = &rcar_fuse_v3u },
+ { .compatible = "renesas,r8a779f0-efuse", .data = &rcar_fuse_s4 },
+ { .compatible = "renesas,r8a779g0-otp", .data = &rcar_fuse_v4h },
+ { .compatible = "renesas,r8a779h0-otp", .data = &rcar_fuse_v4m },
+ { /* sentinel */ }
+};
+
+static struct platform_driver rcar_fuse_driver = {
+ .probe = rcar_fuse_probe,
+ .remove_new = rcar_fuse_remove,
+ .driver = {
+ .name = "rcar_fuse",
+ .of_match_table = rcar_fuse_match,
+ },
+};
+module_platform_driver(rcar_fuse_driver);
+
+MODULE_DESCRIPTION("Renesas R-Car E-FUSE/OTP driver");
+MODULE_LICENSE("GPL");
+MODULE_AUTHOR("Geert Uytterhoeven");
diff --git a/include/linux/platform_data/rcar_fuse.h b/include/linux/platform_data/rcar_fuse.h
new file mode 100644
index 0000000000000000..07933336b5f4e668
--- /dev/null
+++ b/include/linux/platform_data/rcar_fuse.h
@@ -0,0 +1,11 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+#ifndef __LINUX_PLATFORM_DATA_RCAR_FUSE_H__
+#define __LINUX_PLATFORM_DATA_RCAR_FUSE_H__
+
+struct rcar_fuse_platform_data {
+ void __iomem *base;
+ unsigned int offset;
+ unsigned int nregs;
+};
+
+#endif /* __LINUX_PLATFORM_DATA_RCAR_FUSE_H__ */
diff --git a/include/linux/soc/renesas/rcar-fuse.h b/include/linux/soc/renesas/rcar-fuse.h
new file mode 100644
index 0000000000000000..37fc23ef74057066
--- /dev/null
+++ b/include/linux/soc/renesas/rcar-fuse.h
@@ -0,0 +1,41 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+#ifndef __LINUX_SOC_RENESAS_RCAR_FUSE_H__
+#define __LINUX_SOC_RENESAS_RCAR_FUSE_H__
+
+#define RCAR_FUSE_MON0 0
+#define RCAR_FUSE_MON1 1
+#define RCAR_FUSE_MON2 2
+#define RCAR_FUSE_MON3 3
+#define RCAR_FUSE_MON4 4
+#define RCAR_FUSE_MON5 5
+#define RCAR_FUSE_MON6 6
+#define RCAR_FUSE_MON7 7
+#define RCAR_FUSE_MON8 8
+#define RCAR_FUSE_MON9 9
+
+#define RCAR_LTM0_MON0 32
+#define RCAR_LTM0_MON1 33
+#define RCAR_LTM0_MON2 34
+
+#define RCAR_OTPMONITOR0 0
+#define RCAR_OTPMONITOR3 3
+#define RCAR_OTPMONITOR28 28
+#define RCAR_OTPMONITOR32 32
+#define RCAR_OTPMONITOR33 33
+#define RCAR_OTPMONITOR34 34
+#define RCAR_OTPMONITOR35 35
+#define RCAR_OTPMONITOR36 36
+#define RCAR_OTPMONITOR37 37
+#define RCAR_OTPMONITOR38 38
+#define RCAR_OTPMONITOR39 39
+
+#if IS_ENABLED(CONFIG_FUSE_RCAR)
+int rcar_fuse_read(unsigned int idx, u32 *val);
+#else
+static inline int rcar_fuse_read(unsigned int idx, u32 *val)
+{
+ return -ENODEV;
+}
+#endif
+
+#endif /* __LINUX_SOC_RENESAS_RCAR_FUSE_H__ */
--
2.34.1