nvmem: add and use generic MMIO NVMEM
cover nvmem: add and use generic MMIO NVMEM
From: Rafał Miłecki <[email protected]>
MMIO accessible NVMEM devices should be simple enough to allow using a single binding & driver for them.
I already replaced two existing drivers. With NVMEM layouts introduced I can also replace Broadcom's NVRAM driver but that work depends on my pending U-Boot patchset (it provides required helpers).
Rafał Miłecki (4):
dt-bindings: nvmem: mmio: new binding for MMIO accessible NVMEM
devices
nvmem: add generic driver for devices with MMIO access
nvmem: mtk-efuse: replace driver with a generic MMIO one
nvmem: uniphier-efuse: replace driver with a generic MMIO one
.../devicetree/bindings/nvmem/mmio.yaml | 46 +++++++++ drivers/nvmem/Kconfig | 24 +++-- drivers/nvmem/Makefile | 6 +- drivers/nvmem/mmio.c | 84 ++++++++++++++++ drivers/nvmem/mtk-efuse.c | 97 ------------------- drivers/nvmem/uniphier-efuse.c | 78 --------------- 6 files changed, 148 insertions(+), 187 deletions(-) create mode 100644 Documentation/devicetree/bindings/nvmem/mmio.yaml create mode 100644 drivers/nvmem/mmio.c delete mode 100644 drivers/nvmem/mtk-efuse.c delete mode 100644 drivers/nvmem/uniphier-efuse.c
Signature · 1 line
2.34.1
PATCH 1/4 dt-bindings: nvmem: mmio: new binding for MMIO accessible NVMEM devices
From: Rafał Miłecki <[email protected]>
With the NVMEM layouts binding in place we should now use:
1. NVMEM device access bindings
2. NVMEM content description bindings
This binding allows describing NVMEM devices that can be MMIO accessed.
--- .../devicetree/bindings/nvmem/mmio.yaml | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 Documentation/devicetree/bindings/nvmem/mmio.yaml
Documentation/devicetree/bindings/nvmem/mmio.yaml new file +46 -0
@@ -0,0 +1,46 @@ +# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) +%YAML 1.2 +--- +$id: http://devicetree.org/schemas/nvmem/mmio.yaml# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: MMIO access based NVMEM + +description: | + This binding describes simple NVMEM devices that can be accessed by using MMIO + (memory-mapped I/O access). + + It's a generic solution for providing NVMEM content access. The way of + handling actual content may be device specific and can be described using a + proper layout. + +maintainers: + - Rafał Miłecki <[email protected]> + +allOf: + - $ref: nvmem.yaml# + +properties: + compatible: + const: mmio-nvmem + + reg: + maxItems: 1 + + reg-io-width: + description: | + The size (in bytes) of the IO accesses that should be performed + on the device. + enum: [1, 2, 4, 8] + +required: + - reg + +unevaluatedProperties: false + +examples: + - | + nvmem@10000 { + compatible = "mmio-nvmem"; + reg = <0x10000000 0x10000>; + };
Signature · 1 line
2.34.1
PATCH 2/4 nvmem: add generic driver for devices with MMIO access
In reply to Rafał MiłeckiFrom: Rafał Miłecki <[email protected]>
With nvmem layouts in place we should now work on plain content access NVMEM drivers (e.g. MMIO one). Actual NVMEM content handling should go to layout drivers.
--- drivers/nvmem/Kconfig | 10 ++++++ drivers/nvmem/Makefile | 2 ++ drivers/nvmem/mmio.c | 80 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 92 insertions(+) create mode 100644 drivers/nvmem/mmio.c
drivers/nvmem/Kconfig +10 -0
@@ -170,6 +170,16 @@ config NVMEM_MICROCHIP_OTPC This driver enable the OTP controller available on Microchip SAMA7G5 SoCs. It controls the access to the OTP memory connected to it. +config NVMEM_MMIO + tristate "MMIO access based NVMEM support" + depends on HAS_IOMEM + help + This driver provides support for NVMEM devices that can be accessed + using MMIO. + + This driver can also be built as a module. If so, the module + will be called nvmem-mmio. + config NVMEM_MTK_EFUSE tristate "Mediatek SoCs EFUSE support" depends on ARCH_MEDIATEK || COMPILE_TEST
drivers/nvmem/Makefile +2 -0
@@ -36,6 +36,8 @@ obj-$(CONFIG_NVMEM_MESON_MX_EFUSE) += nvmem_meson_mx_efuse.o nvmem_meson_mx_efuse-y := meson-mx-efuse.o obj-$(CONFIG_NVMEM_MICROCHIP_OTPC) += nvmem-microchip-otpc.o nvmem-microchip-otpc-y := microchip-otpc.o +obj-$(CONFIG_NVMEM_MMIO) += nvmem-mmio.o +nvmem-mmio-y := mmio.o obj-$(CONFIG_NVMEM_MTK_EFUSE) += nvmem_mtk-efuse.o nvmem_mtk-efuse-y := mtk-efuse.o obj-$(CONFIG_NVMEM_MXS_OCOTP) += nvmem-mxs-ocotp.o
drivers/nvmem/mmio.c new file +80 -0
80 lines not on this page · raw message
Signature · 1 line
2.34.1
PATCH 3/4 nvmem: mtk-efuse: replace driver with a generic MMIO one
In reply to Rafał MiłeckiFrom: Rafał Miłecki <[email protected]>
Mediatek EFUSE uses a simple MMIO that can be handled with a generic driver. Replace this driver to avoid code duplication.
Keep Kconfig symbol for a release or two to help with "make oldconfig".
--- drivers/nvmem/Kconfig | 7 ++- drivers/nvmem/Makefile | 2 - drivers/nvmem/mmio.c | 3 ++ drivers/nvmem/mtk-efuse.c | 97 --------------------------------------- 4 files changed, 6 insertions(+), 103 deletions(-) delete mode 100644 drivers/nvmem/mtk-efuse.c
drivers/nvmem/Kconfig +3 -4
@@ -184,12 +184,11 @@ config NVMEM_MTK_EFUSE tristate "Mediatek SoCs EFUSE support" depends on ARCH_MEDIATEK || COMPILE_TEST depends on HAS_IOMEM + select NVMEM_MMIO help - This is a driver to access hardware related data like sensor - calibration, HDMI impedance etc. + This driver has been replaced by a generic MMIO implementation. - This driver can also be built as a module. If so, the module - will be called efuse-mtk. + Update your config as this symbol will be dropped in the next release. config NVMEM_MXS_OCOTP tristate "Freescale MXS On-Chip OTP Memory Support"
drivers/nvmem/Makefile +0 -2
@@ -38,8 +38,6 @@ obj-$(CONFIG_NVMEM_MICROCHIP_OTPC) += nvmem-microchip-otpc.o nvmem-microchip-otpc-y := microchip-otpc.o obj-$(CONFIG_NVMEM_MMIO) += nvmem-mmio.o nvmem-mmio-y := mmio.o -obj-$(CONFIG_NVMEM_MTK_EFUSE) += nvmem_mtk-efuse.o -nvmem_mtk-efuse-y := mtk-efuse.o obj-$(CONFIG_NVMEM_MXS_OCOTP) += nvmem-mxs-ocotp.o nvmem-mxs-ocotp-y := mxs-ocotp.o obj-$(CONFIG_NVMEM_NINTENDO_OTP) += nvmem-nintendo-otp.o
drivers/nvmem/mmio.c +3 -0
@@ -57,6 +57,9 @@ static int mmio_nvmem_probe(struct platform_device *pdev)static const struct of_device_id mmio_nvmem_of_match_table[] = { { .compatible ="mmio-nvmem" , }, +/* Custom bindings that were introduced before the mmio one */ + { .compatible ="mediatek,mt8173-efuse" , }, + { .compatible ="mediatek,efuse" , }, {}, };
drivers/nvmem/mtk-efuse.c deleted +0 -97
97 lines not on this page · raw message
Signature · 1 line
2.34.1
PATCH 4/4 nvmem: uniphier-efuse: replace driver with a generic MMIO one
In reply to Rafał MiłeckiFrom: Rafał Miłecki <[email protected]>
UniPhier eFuse uses a simple MMIO that can be handled with a generic driver. Replace this driver to avoid code duplication.
Keep Kconfig symbol for a release or two to help with "make oldconfig".
--- drivers/nvmem/Kconfig | 7 ++- drivers/nvmem/Makefile | 2 - drivers/nvmem/mmio.c | 1 + drivers/nvmem/uniphier-efuse.c | 78 ---------------------------------- 4 files changed, 4 insertions(+), 84 deletions(-) delete mode 100644 drivers/nvmem/uniphier-efuse.c
drivers/nvmem/Kconfig +3 -4
@@ -359,12 +359,11 @@ config NVMEM_UNIPHIER_EFUSE tristate "UniPhier SoCs eFuse support" depends on ARCH_UNIPHIER || COMPILE_TEST depends on HAS_IOMEM + select NVMEM_MMIO help - This is a simple driver to dump specified values of UniPhier SoC - from eFuse. + This driver has been replaced by a generic MMIO implementation. - This driver can also be built as a module. If so, the module - will be called nvmem-uniphier-efuse. + Update your config as this symbol will be dropped in the next release. config NVMEM_VF610_OCOTP tristate "VF610 SoC OCOTP support"
drivers/nvmem/Makefile +0 -2
@@ -67,8 +67,6 @@ obj-$(CONFIG_NVMEM_SUNPLUS_OCOTP) += nvmem_sunplus_ocotp.o nvmem_sunplus_ocotp-y := sunplus-ocotp.o obj-$(CONFIG_NVMEM_SUNXI_SID) += nvmem_sunxi_sid.o nvmem_sunxi_sid-y := sunxi_sid.o -obj-$(CONFIG_NVMEM_UNIPHIER_EFUSE) += nvmem-uniphier-efuse.o -nvmem-uniphier-efuse-y := uniphier-efuse.o obj-$(CONFIG_NVMEM_VF610_OCOTP) += nvmem-vf610-ocotp.o nvmem-vf610-ocotp-y := vf610-ocotp.o obj-$(CONFIG_NVMEM_ZYNQMP) += nvmem_zynqmp_nvmem.o
drivers/nvmem/mmio.c +1 -0
@@ -60,6 +60,7 @@ static const struct of_device_id mmio_nvmem_of_match_table[] = {/* Custom bindings that were introduced before the mmio one */ { .compatible = "mediatek,mt8173-efuse", }, { .compatible = "mediatek,efuse", }, + { .compatible = "socionext,uniphier-efuse", }, {}, };
drivers/nvmem/uniphier-efuse.c deleted +0 -78
78 lines not on this page · raw message
Signature · 1 line
2.34.1
Quoted 8 lines · Rafał Miłecki
From: Rafał Miłecki <[email protected]>
MMIO accessible NVMEM devices should be simple enough to allow using a single binding & driver for them.
I already replaced two existing drivers. With NVMEM layouts introduced I can also replace Broadcom's NVRAM driver but that work depends on my pending U-Boot patchset (it provides required helpers).
This was meant to be marked as V2.
Changes:
1. Replaced io with mmio to be clear
2. Replaced 2 existing drivers with generic implementation
Re: [PATCH 3/4] nvmem: mtk-efuse: replace driver with a generic MMIO one
In reply to Rafał MiłeckiQuoted 4 lines
From: Rafał Miłecki <[email protected]>
Mediatek EFUSE uses a simple MMIO that can be handled with a generic driver. Replace this driver to avoid code duplication.
I don't think this is the correct approach. You'll restrict that driver to being read-only. I admit that right now, it's read only, but it can be extended to also support efuse writing. With this changes, it's not possible.
drivers/nvmem/mmio.c static int mmio_nvmem_probe(struct platform_device *pdev) · line 62
Quoted 5 lines
static const struct of_device_id mmio_nvmem_of_match_table[] = { { .compatible = "mmio-nvmem", },drivers/nvmem/mmio.c +3
-0+/* Custom bindings that were introduced before the mmio one */ + { .compatible = "mediatek,mt8173-efuse", }, + { .compatible = "mediatek,efuse", },
Why do you assume that all mediatek efuses will be the same? This should rather be something like (in the dts/binding):
compatible = "mediatek,mt8173-efuse", "mmio-nvmem";
So if there is no driver for the particular efuse, it will fall back to the generic one.
-michael
Re: [PATCH 3/4] nvmem: mtk-efuse: replace driver with a generic MMIO one
Quoted 19 lines · Michael Walle
Quoted 4 lines
From: Rafał Miłecki <[email protected]>
Mediatek EFUSE uses a simple MMIO that can be handled with a generic driver. Replace this driver to avoid code duplication.
I don't think this is the correct approach. You'll restrict that driver to being read-only. I admit that right now, it's read only, but it can be extended to also support efuse writing. With this changes, it's not possible.
drivers/nvmem/mmio.c static int mmio_nvmem_probe(struct platform_device *pdev) · line 62
Quoted 5 lines
static const struct of_device_id mmio_nvmem_of_match_table[] = { { .compatible = "mmio-nvmem", },drivers/nvmem/mmio.c +3
-0+/* Custom bindings that were introduced before the mmio one */ + { .compatible = "mediatek,mt8173-efuse", }, + { .compatible = "mediatek,efuse", },Why do you assume that all mediatek efuses will be the same? This should rather be something like (in the dts/binding):
Quoted 4 lines
compatible = "mediatek,mt8173-efuse", "mmio-nvmem";
So if there is no driver for the particular efuse, it will fall back to the generic one.
Oh great, I'm making circles now.
Rob suggested I should convert existing drivers, see:
[PATCH 2/2] nvmem: add generic driver for devices with I/O based access and I thought efuse ones should be good.
Please tell me how I should handle brcm,nvram without wasting more time. I thought I had it sorted out but I just wasted 2 days.
I believe I need to make brcm,nvram NVMEM layout. Without converting it I'm afraid you'll refuse my changes adding cell post processing (that happened to my U-Boot attempts).
Before I convert brcm,nvram to NVMEM layout I need some binding & driver providing MMIO device access. How to handle that?
Kunihiko Hayashi
maintainerRe: [PATCH 2/4] nvmem: add generic driver for devices with MMIO access
In reply to Rafał MiłeckiHi,
Quoted 76 lines · Rafał Miłecki
From: Rafał Miłecki <[email protected]>
With nvmem layouts in place we should now work on plain content access NVMEM drivers (e.g. MMIO one). Actual NVMEM content handling should go to layout drivers.
Signed-off-by Rafał Miłecki <[email protected]>--- drivers/nvmem/Kconfig | 10 ++++++ drivers/nvmem/Makefile | 2 ++ drivers/nvmem/mmio.c | 80 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 92 insertions(+)create mode 100644
drivers/nvmem/mmio.cdrivers/nvmem/Kconfig +10
-0@@ -170,6 +170,16 @@ config NVMEM_MICROCHIP_OTPC This driver enable the OTP controller available on Microchip SAMA7G5 SoCs. It controls the access to the OTP memory connected to it. +config NVMEM_MMIO + tristate "MMIO access based NVMEM support" + depends on HAS_IOMEM + help + This driver provides support for NVMEM devices that can be accessed + using MMIO. + + This driver can also be built as a module. If so, the module + will be called nvmem-mmio. + config NVMEM_MTK_EFUSE tristate "Mediatek SoCs EFUSE support" depends on ARCH_MEDIATEK || COMPILE_TESTdrivers/nvmem/Makefile +0
-0@@ -36,6 +36,8 @@ obj-$(CONFIG_NVMEM_MESON_MX_EFUSE) +=nvmem_meson_mx_efuse.o
nvmem_meson_mx_efuse-y := meson-mx-efuse.o
obj-$(CONFIG_NVMEM_MICROCHIP_OTPC) += nvmem-microchip-otpc.o
nvmem-microchip-otpc-y := microchip-otpc.odrivers/nvmem/Makefile +2
-0+obj-$(CONFIG_NVMEM_MMIO) += nvmem-mmio.o +nvmem-mmio-y := mmio.o obj-$(CONFIG_NVMEM_MTK_EFUSE) += nvmem_mtk-efuse.o nvmem_mtk-efuse-y := mtk-efuse.o obj-$(CONFIG_NVMEM_MXS_OCOTP) += nvmem-mxs-ocotp.odrivers/nvmem/mmio.c new file +18
-0@@ -0,0 +1,80 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* +* Copyright (C) 2023 Rafał Miłecki <[email protected]> +*/ + +#include <linux/io.h> +#include <linux/mod_devicetable.h> +#include <linux/module.h> +#include <linux/nvmem-provider.h> +#include <linux/platform_device.h> +#include <linux/property.h> +#include <linux/slab.h> + +struct mmio_nvmem { +void __iomem *base; +}; + +static int mmio_nvmem_read(void *context, unsigned int offset, void *val,
size_tbytes)drivers/nvmem/mmio.c new file +2
-0+{ +struct mmio_nvmem *priv = context;
drivers/nvmem/mmio.c line 30
Quoted 10 lines
drivers/nvmem/mmio.c new file +10
-0+ +memcpy_fromio (val, priv->base, bytes); + +return 0 ; +} + +static int mmio_nvmem_probe (struct platform_device *pdev) +{ +struct nvmem_config config = { + .name ="mmio-nvmem" ,
The fixed name breaks sysfs for multiple nvmem instances.
sysfs: cannot create duplicate filename '/bus/nvmem/devices/mmio-nvmem0'
drivers/nvmem/mmio.c line 31
Quoted 1 line
drivers/nvmem/mmio.c new file +1
-0+ .read_only = true,
As Michael said in the mediatek patch,
I also think it's hard to make read-only fixed in the generic driver.
Quoted 12 lines
drivers/nvmem/mmio.c new file +12
-0+ .reg_read = mmio_nvmem_read, + }; +struct device *dev = &pdev->dev; +struct mmio_nvmem *priv; +struct resource *res; + + priv =devm_kzalloc (dev,sizeof (*priv),GFP_KERNEL ); +if (!priv) +return -ENOMEM ; + + priv->base =devm_platform_get_and_ioremap_resource (pdev,0 , &res); +if (IS_ERR (priv->base))
drivers/nvmem/mmio.c line 53
Quoted 10 lines
drivers/nvmem/mmio.c new file +10
-0+return PTR_ERR (priv->base); + + config.dev = dev; + config.size =resource_size (res); + config.word_size =sizeof (u8); + config.stride =sizeof (u8); + config.priv = priv; + +if (!device_property_present (dev,"read-only" )) +dev_warn (dev,"Writing is not supported yet" );
Isn't the logic opposite?
Anyway, the driver doesn't use "read-only" properties for selection.
Quoted 17 lines
drivers/nvmem/mmio.c new file +17
-0+ +return PTR_ERR_OR_ZERO (devm_nvmem_register (dev, &config)); +} + +static const struct of_device_id mmio_nvmem_of_match_table[] = { + { .compatible ="mmio-nvmem" , }, + {}, +}; + +static struct platform_driver mmio_nvmem_driver = { + .probe = mmio_nvmem_probe, + .driver = { + .name ="mmio_nvmem" , + .of_match_table = mmio_nvmem_of_match_table, + }, +}; +
drivers/nvmem/mmio.c line 80
Quoted 10 lines
drivers/nvmem/mmio.c new file +10
-0+static int __init mmio_nvmem_init (void ) +{ +return platform_driver_register (&mmio_nvmem_driver); +} + +subsys_initcall_sync (mmio_nvmem_init); + +MODULE_AUTHOR ("Rafał Miłecki" ); +MODULE_LICENSE ("GPL" ); +MODULE_DEVICE_TABLE (of, mmio_nvmem_of_match_table);
Thank you,
---
Best Regards
Kunihiko Hayashi
Re: [PATCH 3/4] nvmem: mtk-efuse: replace driver with a generic MMIO one
In reply to Rafał MiłeckiQuoted 2 lines
Before I convert brcm,nvram to NVMEM layout I need some binding & driver providing MMIO device access. How to handle that?
I'm not arguing against having the mmio nvmem driver. But I don't think we should sacrifice possible write access with other drivers. And I presume write access won't be possible with your generic driver as it probably isn't just a memcpy_toio().
It is a great fallback for some nvmem peripherals which just maps a memory region, but doesn't replace a proper driver for an nvmem device.
What bothers me the most isn't the driver change. The driver can be resurrected once someone will do proper write access, but the generic "mediatek,efuse" compatible together with the comment above the older compatible string. These imply that you should use "mediatek,efuse", but we don't know if all mediatek efuse peripherals will be the same - esp. for writing which is usually more complex than the reading.
nitpick btw: why not "nvmem-mmio"?
So it's either:
(1) compatible = "mediatek,mt8173-efuse"
(2) compatible = "mediatek,mt8173-efuse", "mmio-nvmem"
(1) will be supported any anyway for older dts and you need to add the specific compatibles to the nvmem-mmio driver - or keep the driver as is.
With (2) you wouldn't need to do that and the kernel can load the proper driver if available or fall back to the nvmem-mmio one. I'd even make that one "default y" so it will be available on future kernels and boards can already make use of the nvmem device even if there is no proper driver for them.
I'd prefer (2). Dunno what the dt maintainers agree.
-michael
Re: [PATCH 3/4] nvmem: mtk-efuse: replace driver with a generic MMIO one
Quoted 10 lines · Michael Walle
Quoted 2 lines
Before I convert brcm,nvram to NVMEM layout I need some binding & driver providing MMIO device access. How to handle that?
I'm not arguing against having the mmio nvmem driver. But I don't think we should sacrifice possible write access with other drivers. And I presume write access won't be possible with your generic driver as it probably isn't just a
memcpy_toio().It is a great fallback for some nvmem peripherals which just maps a memory region, but doesn't replace a proper driver for an nvmem device.
OK, then maybe I'll retry again with generic MMIO and without converting existing specific drivers. That is what (AFAIU) Rob asked though.
Quoted 6 lines
What bothers me the most isn't the driver change. The driver can be resurrected once someone will do proper write access, but the generic "mediatek,efuse" compatible together with the comment above the older compatible string. These imply that you should use "mediatek,efuse", but we don't know if all mediatek efuse peripherals will be the same - esp. for writing which is usually more complex than the reading.
mediatek,efuse was already there, don't blame me for it ;)
Quoted 1 line
nitpick btw: why not "nvmem-mmio"?
Because I read from left to right ;)
It's MMIO based NVMEM. Not MMIO on top of NVMEM.
Sure, we have "nvmem-cells" but that is because those are cells of NVMEM device.
Unless my English knowledge fails me.
Quoted 8 lines
So it's either:
(1) compatible = "mediatek,mt8173-efuse"
(2) compatible = "mediatek,mt8173-efuse", "mmio-nvmem"(1) will be supported any anyway for older dts and you need to add the specific compatibles to the nvmem-mmio driver - or keep the driver as is.
Quoted 7 lines
With (2) you wouldn't need to do that and the kernel can load the proper driver if available or fall back to the nvmem-mmio one. I'd even make that one "default y" so it will be available on future kernels and boards can already make use of the nvmem device even if there is no proper driver for them.
I'd prefer (2). Dunno what the dt maintainers agree.
(2) looks OK, Rob, Krzysztof?
Re: [PATCH 3/4] nvmem: mtk-efuse: replace driver with a generic MMIO one
Quoted 3 lines
Quoted 1 line
nitpick btw: why not "nvmem-mmio"?
Because I read from left to right ;)
if you have more compatibles do you prefer
a-grp
bcdef-grp
gh-grp
or
grp-a
grp-bcdef
grp-gh
To my eyes the latter looks nicer, but it's just a matter of taste ;)
-michael
Re: [PATCH 2/4] nvmem: add generic driver for devices with MMIO access
In reply to Rafał MiłeckiQuoted 66 lines · Rafał Miłecki
From: Rafał Miłecki <[email protected]>
With nvmem layouts in place we should now work on plain content access NVMEM drivers (e.g. MMIO one). Actual NVMEM content handling should go to layout drivers.
Signed-off-by Rafał Miłecki <[email protected]>--- drivers/nvmem/Kconfig | 10 ++++++ drivers/nvmem/Makefile | 2 ++ drivers/nvmem/mmio.c | 80 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 92 insertions(+)create mode 100644
drivers/nvmem/mmio.cdrivers/nvmem/Kconfig +10
-0@@ -170,6 +170,16 @@ config NVMEM_MICROCHIP_OTPC This driver enable the OTP controller available on Microchip SAMA7G5 SoCs. It controls the access to the OTP memory connected to it. +config NVMEM_MMIO + tristate "MMIO access based NVMEM support" + depends on HAS_IOMEM + help + This driver provides support for NVMEM devices that can be accessed + using MMIO. + + This driver can also be built as a module. If so, the module + will be called nvmem-mmio. + config NVMEM_MTK_EFUSE tristate "Mediatek SoCs EFUSE support" depends on ARCH_MEDIATEK || COMPILE_TESTdrivers/nvmem/Makefile +2
-0@@ -36,6 +36,8 @@ obj-$(CONFIG_NVMEM_MESON_MX_EFUSE) += nvmem_meson_mx_efuse.o nvmem_meson_mx_efuse-y := meson-mx-efuse.o obj-$(CONFIG_NVMEM_MICROCHIP_OTPC) += nvmem-microchip-otpc.o nvmem-microchip-otpc-y := microchip-otpc.o +obj-$(CONFIG_NVMEM_MMIO) += nvmem-mmio.o +nvmem-mmio-y := mmio.o obj-$(CONFIG_NVMEM_MTK_EFUSE) += nvmem_mtk-efuse.o nvmem_mtk-efuse-y := mtk-efuse.o obj-$(CONFIG_NVMEM_MXS_OCOTP) += nvmem-mxs-ocotp.odrivers/nvmem/mmio.c new file +12
-0@@ -0,0 +1,80 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* +* Copyright (C) 2023 Rafał Miłecki <[email protected]> +*/ + +#include <linux/io.h> +#include <linux/mod_devicetable.h> +#include <linux/module.h> +#include <linux/nvmem-provider.h> +#include <linux/platform_device.h> +#include <linux/property.h> +#include <linux/slab.h>
drivers/nvmem/mmio.c line 22
Quoted 10 lines
drivers/nvmem/mmio.c new file +10
-0+ +struct mmio_nvmem { +void __iomem *base; +}; + +static int mmio_nvmem_read (void *context,unsigned int offset,void *val,size_t bytes) +{ +struct mmio_nvmem *priv = context; + +memcpy_fromio (val, priv->base, bytes);
How does this work with different register strides?
drivers/nvmem/mmio.c line 30
Quoted 8 lines
drivers/nvmem/mmio.c new file +8
-0+ +return 0 ; +} + +static int mmio_nvmem_probe (struct platform_device *pdev) +{ +struct nvmem_config config = { + .name ="mmio-nvmem" ,
you could use NVMEM_DEVID_AUTO.
But this is not great, this is going to break the existing abi for converted drivers. Either we find a way to pass compatible specific data and override the default values.
Also there are going to be cases where access to registers will require additional resources like clks or pd.
--srini
Quoted 50 lines
drivers/nvmem/mmio.c new file +50
-0+ .read_only = true, + .reg_read = mmio_nvmem_read, + }; +struct device *dev = &pdev->dev; +struct mmio_nvmem *priv; +struct resource *res; + + priv =devm_kzalloc (dev,sizeof (*priv),GFP_KERNEL ); +if (!priv) +return -ENOMEM ; + + priv->base =devm_platform_get_and_ioremap_resource (pdev,0 , &res); +if (IS_ERR (priv->base)) +return PTR_ERR (priv->base); + + config.dev = dev; + config.size =resource_size (res); + config.word_size =sizeof (u8); + config.stride =sizeof (u8); + config.priv = priv; + +if (!device_property_present (dev,"read-only" )) +dev_warn (dev,"Writing is not supported yet" ); + +return PTR_ERR_OR_ZERO (devm_nvmem_register (dev, &config)); +} + +static const struct of_device_id mmio_nvmem_of_match_table[] = { + { .compatible ="mmio-nvmem" , }, + {}, +}; + +static struct platform_driver mmio_nvmem_driver = { + .probe = mmio_nvmem_probe, + .driver = { + .name ="mmio_nvmem" , + .of_match_table = mmio_nvmem_of_match_table, + }, +}; + +static int __init mmio_nvmem_init (void ) +{ +return platform_driver_register (&mmio_nvmem_driver); +} + +subsys_initcall_sync (mmio_nvmem_init); + +MODULE_AUTHOR ("Rafał Miłecki" ); +MODULE_LICENSE ("GPL" ); +MODULE_DEVICE_TABLE (of, mmio_nvmem_of_match_table);
Rob Herring
maintainerRe: [PATCH 3/4] nvmem: mtk-efuse: replace driver with a generic MMIO one
In reply to Michael WalleQuoted 11 lines · Michael Walle
Quoted 2 lines
Before I convert brcm,nvram to NVMEM layout I need some binding & driver providing MMIO device access. How to handle that?
I'm not arguing against having the mmio nvmem driver. But I don't think we should sacrifice possible write access with other drivers. And I presume write access won't be possible with your generic driver as it probably isn't just a
memcpy_toio().It is a great fallback for some nvmem peripherals which just maps a memory region, but doesn't replace a proper driver for an nvmem device.
Quoted 6 lines
What bothers me the most isn't the driver change. The driver can be resurrected once someone will do proper write access, but the generic "mediatek,efuse" compatible together with the comment above the older compatible string. These imply that you should use "mediatek,efuse", but we don't know if all mediatek efuse peripherals will be the same - esp. for writing which is usually more complex than the reading.
Because the kernel can't pick the "best" driver when there are multiple matches, it's all Mediatek platforms use the generic driver or all use the Mediatek driver.
Personally, I think it is easy enough to revive the driver if needed unless writing is a soon and likely feature.
The other way to share is providing library functions for drivers to use. Then the Mediatek driver can use the generic read functions and custom write functions.
Quoted 10 lines
nitpick btw: why not "nvmem-mmio"?
So it's either:
(1) compatible = "mediatek,mt8173-efuse"
(2) compatible = "mediatek,mt8173-efuse", "mmio-nvmem"(1) will be supported any anyway for older dts and you need to add the specific compatibles to the nvmem-mmio driver - or keep the driver as is.
Quoted 7 lines
With (2) you wouldn't need to do that and the kernel can load the proper driver if available or fall back to the nvmem-mmio one. I'd even make that one "default y" so it will be available on future kernels and boards can already make use of the nvmem device even if there is no proper driver for them.
I'd prefer (2). Dunno what the dt maintainers agree.
No because you are changing the DT. The DT can't change when you want to change drivers. This thinking is one reason why 'generic' bindings are rejected.
Rob
Re: [PATCH 3/4] nvmem: mtk-efuse: replace driver with a generic MMIO one
Am 2023-02-01 19:54, schrieb Rob Herring:
Quoted 19 lines · Michael Walle
Quoted 11 lines
Quoted 2 lines
Before I convert brcm,nvram to NVMEM layout I need some binding & driver providing MMIO device access. How to handle that?
I'm not arguing against having the mmio nvmem driver. But I don't think we should sacrifice possible write access with other drivers. And I presume write access won't be possible with your generic driver as it probably isn't just a
memcpy_toio().It is a great fallback for some nvmem peripherals which just maps a memory region, but doesn't replace a proper driver for an nvmem device.
Quoted 6 lines
What bothers me the most isn't the driver change. The driver can be resurrected once someone will do proper write access, but the generic "mediatek,efuse" compatible together with the comment above the older compatible string. These imply that you should use "mediatek,efuse", but we don't know if all mediatek efuse peripherals will be the same - esp. for writing which is usually more complex than the reading.
Quoted 3 lines
Because the kernel can't pick the "best" driver when there are multiple matches, it's all Mediatek platforms use the generic driver or all use the Mediatek driver.
Isn't that the whole point of having multiple compatible strings?
compatible = "fsl,imx27-mmc", "fsl,imx21-mmc";
The OS might either load the driver for "fsl,imx21-mmc" or one for "fsl,imx27-mmc", with the latter considered to be the preferred one.
Quoted 2 lines
Personally, I think it is easy enough to revive the driver if needed unless writing is a soon and likely feature.
That what was actually triggered my initial reply. We are planning a new board with a mediatek SoC and we'll likely need the write support.
But I thought the "mediatek,efuse" was a new compatible with this patch and the (new!) comment make it looks like these compatible are deprecated in favor of "mmio-nvmem". Which would make it impossible to distinguish between the different efuse peripherals and thus make it impossible to add write support.
Quoted 20 lines
The other way to share is providing library functions for drivers to use. Then the Mediatek driver can use the generic read functions and custom write functions.
Quoted 16 lines
nitpick btw: why not "nvmem-mmio"?
So it's either:
(1) compatible = "mediatek,mt8173-efuse"
(2) compatible = "mediatek,mt8173-efuse", "mmio-nvmem"(1) will be supported any anyway for older dts and you need to add the specific compatibles to the nvmem-mmio driver - or keep the driver as is.
With (2) you wouldn't need to do that and the kernel can load the proper driver if available or fall back to the nvmem-mmio one. I'd even make that one "default y" so it will be available on future kernels and boards can already make use of the nvmem device even if there is no proper driver for them.
Quoted 5 lines
Quoted 1 line
I'd prefer (2). Dunno what the dt maintainers agree.
No because you are changing the DT. The DT can't change when you want to change drivers. This thinking is one reason why 'generic' bindings are rejected.
There is no change in the DT. Newer bindings will have
compatible = "vendor,ip-block", "mmio-nvmem"
when the ip block is compatible with mmio-nvmem. Otherwise I don't get why there is a mmio-nvmem compatible at all. Just having
compatible = "mmio-nvmem"
looks wrong as it would just work correctly in some minor cases, i.e. when write support is just a memcpy_toio() - or we deliberately ignore any write support. But even then, you always tell people to add specific compatibles for the case when quirks are needed..
-michael
AngeloGioacchino Del Regno
maintainerRe: [PATCH 2/4] nvmem: add generic driver for devices with MMIO access
In reply to Rafał MiłeckiIl 01/02/23 07:47, Rafał Miłecki ha scritto:
Quoted 7 lines
From: Rafał Miłecki <[email protected]>
With nvmem layouts in place we should now work on plain content access NVMEM drivers (e.g. MMIO one). Actual NVMEM content handling should go to layout drivers.
Signed-off-by Rafał Miłecki <[email protected]>
I agree but if you want to really bring on this change, you should add some kind of write support... and clocks... and regulators with different voltage levels (write/fuse-blow voltage, read voltage if any)...
Describing this entire thing in device-tree should be possible, but then some SoCs may need a specific register sequence in order to enter writing mode, which is something that you can't just put in devicetree, so this driver should be a framework on its own - hence not as simple as proposed.
Regards,
Angelo
Rob Herring
maintainerRe: [PATCH 3/4] nvmem: mtk-efuse: replace driver with a generic MMIO one
In reply to Michael WalleQuoted 24 lines · Michael Walle
Am 2023-02-01 19:54, schrieb Rob Herring:
Quoted 23 lines · Michael Walle
Quoted 21 lines
Quoted 2 lines
Before I convert brcm,nvram to NVMEM layout I need some binding & driver providing MMIO device access. How to handle that?
I'm not arguing against having the mmio nvmem driver. But I don't think we should sacrifice possible write access with other drivers. And
I presume write access won't be possible with your generic driver as it
probably isn't just amemcpy_toio().It is a great fallback for some nvmem peripherals which just maps a memory region, but doesn't replace a proper driver for an nvmem device.
What bothers me the most isn't the driver change. The driver can be resurrected once someone will do proper write access, but the generic "mediatek,efuse" compatible together with the comment above the older compatible string. These imply that you should use "mediatek,efuse", but we don't know if all mediatek efuse peripherals will be the same - esp. for writing which is usually more complex than the reading.
Quoted 8 lines
Quoted 3 lines
Because the kernel can't pick the "best" driver when there are multiple matches, it's all Mediatek platforms use the generic driver or all use the Mediatek driver.
Isn't that the whole point of having multiple compatible strings?
compatible = "fsl,imx27-mmc", "fsl,imx21-mmc";
The OS might either load the driver for "fsl,imx21-mmc" or one for "fsl,imx27-mmc", with the latter considered to be the preferred one.
No, there's some assumption they would be similar enough to be served by the same driver.
While it seems like we'd want to fix this, it's been an issue I've been aware of as long as I've been involved with DT and yet I don't recall anyone really having an issue. Could just be everyone couples their kernel and dtb versions...
Quoted 5 lines
Quoted 2 lines
Personally, I think it is easy enough to revive the driver if needed unless writing is a soon and likely feature.
That what was actually triggered my initial reply. We are planning a new board with a mediatek SoC and we'll likely need the write support.
If write support is on the horizon, then I'd say keep the Mediatek driver.
Quoted 5 lines
But I thought the "mediatek,efuse" was a new compatible with this patch and the (new!) comment make it looks like these compatible are deprecated in favor of "mmio-nvmem". Which would make it impossible to distinguish between the different efuse peripherals and thus make it impossible to add write support.
No, it's in the schema and dts files.
Quoted 34 lines
Quoted 21 lines
The other way to share is providing library functions for drivers to use. Then the Mediatek driver can use the generic read functions and custom write functions.
Quoted 16 lines
nitpick btw: why not "nvmem-mmio"?
So it's either:
(1) compatible = "mediatek,mt8173-efuse"
(2) compatible = "mediatek,mt8173-efuse", "mmio-nvmem"(1) will be supported any anyway for older dts and you need to add the specific compatibles to the nvmem-mmio driver - or keep the driver as is.
With (2) you wouldn't need to do that and the kernel can load the proper driver if available or fall back to the nvmem-mmio one. I'd even make that one "default y" so it will be available on future kernels and boards can already make use of the nvmem device even if there is no proper driver for them.
Quoted 5 lines
Quoted 1 line
I'd prefer (2). Dunno what the dt maintainers agree.
No because you are changing the DT. The DT can't change when you want to change drivers. This thinking is one reason why 'generic' bindings are rejected.
There is no change in the DT. Newer bindings will have
compatible = "vendor,ip-block", "mmio-nvmem"
when the ip block is compatible with mmio-nvmem. Otherwise I don't get why there is a mmio-nvmem compatible at all. Just having
Quoted 6 lines
compatible = "mmio-nvmem"
looks wrong as it would just work correctly in some minor cases, i.e. when write support is just a
memcpy_toio()- or we deliberately ignore any write support. But even then, you always tell people to add specific compatibles for the case when quirks are needed..
Yes, I do. I was assuming these are simple cases unlikely to need platform specific support. We're just reading static bits from registers...
If you may need write support or have other complications, then "mmio-nvmem" should not be used. You can use the driver, but it should match with a platform specific compatible, not the generic one.
Rob
Rob Herring
maintainerRe: [PATCH 1/4] dt-bindings: nvmem: mmio: new binding for MMIO accessible NVMEM devices
In reply to Rafał MiłeckiQuoted 25 lines · Rafał Miłecki
From: Rafał Miłecki <[email protected]>
With the NVMEM layouts binding in place we should now use:
1. NVMEM device access bindings
2. NVMEM content description bindingsThis binding allows describing NVMEM devices that can be MMIO accessed.
Signed-off-by Rafał Miłecki <[email protected]>--- .../devicetree/bindings/nvmem/mmio.yaml | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 Documentation/devicetree/bindings/nvmem/mmio.yamlDocumentation/devicetree/bindings/nvmem/mmio.yaml new file +5
-0@@ -0,0 +1,46 @@ +# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) +%YAML 1.2 +--- +$id: http://devicetree.org/schemas/nvmem/mmio.yaml# +$schema: http://devicetree.org/meta-schemas/core.yaml#
Documentation/devicetree/bindings/nvmem/mmio.yaml line 15
Quoted 10 lines
Documentation/devicetree/bindings/nvmem/mmio.yaml new file +10
-0+ +title: MMIO access based NVMEM + +description: | + This binding describes simple NVMEM devices that can be accessed by using MMIO + (memory-mapped I/O access). + + It's a generic solution for providing NVMEM content access. The way of + handling actual content may be device specific and can be described using a + proper layout.
Please add some guidance based on the discussion about when this should and shouldn't be used. Specifically, anything with potential write accesses should use a device specific compatible and not the generic one.
Quoted 34 lines
Documentation/devicetree/bindings/nvmem/mmio.yaml new file +31
-0+ +maintainers: + - Rafał Miłecki <[email protected]> + +allOf: + - $ref: nvmem.yaml# + +properties: + compatible: + const: mmio-nvmem + + reg: + maxItems: 1 + + reg-io-width: + description: | + The size (in bytes) of the IO accesses that should be performed + on the device. + enum: [1, 2, 4, 8] + +required: + - reg + +unevaluatedProperties: false + +examples: + - | + nvmem@10000 { + compatible = "mmio-nvmem"; + reg = <0x10000000 0x10000>; + };Signature · 1 line
2.34.1