Skip to contentSkip to conversation

dt-bindings: nvmem: add binding for I/O mapped NVMEM

PATCH In review Rafał Miłecki 3 participants 6 messages Mar 4, 2021 – Mar 5, 2021

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/2 dt-bindings: nvmem: add binding for I/O mapped NVMEM

From: Rafał Miłecki <[email protected]>

NVMEM on some devices can be accessed using I/O mapping. For example on Broadcom MIPS and ARM Northstar platforms NVRAM can be accessed that way.

---
 .../devicetree/bindings/nvmem/iomap.yaml      | 29 +++++++++++++++++++
 1 file changed, 29 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/nvmem/iomap.yaml
Documentation/devicetree/bindings/nvmem/iomap.yaml new file +29 -0
new file mode 100644
index 000000000000..ec8764598061
@@ -0,0 +1,29 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/nvmem/iomap.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: I/O mapped NVMEM
+
+maintainers:
+  - Rafał Miłecki <[email protected]>
+
+allOf:
+  - $ref: "nvmem.yaml#"
+
+properties:
+  compatible:
+    items:
+      - enum:
+          - brcm,nvram
+      - const: nvmem-iomap
+
+unevaluatedProperties: false
+
+examples:
+  - |
+    nvram@1eff0000 {
+            compatible = "brcm,nvram", "nvmem-iomap";
+            reg = <0x1eff0000 0x10000>;
+    };
Signature · 1 line
2.26.2
#1 lore raw

PATCH 2/2 nvmem: iomap: new driver exposing NVMEM accessible using I/O mapping

From: Rafał Miłecki <[email protected]>

This is a generic NVMEM access method used e.g. by Broadcom for their NVRAM on MIPS and Northstar devices.

---
 drivers/nvmem/Kconfig  |  7 +++
 drivers/nvmem/Makefile |  2 +
 drivers/nvmem/iomap.c  | 99 ++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 108 insertions(+)
 create mode 100644 drivers/nvmem/iomap.c
drivers/nvmem/Kconfig +7 -0
index 75d2594c16e1..3d5c5684685d 100644
@@ -278,4 +278,11 @@ config NVMEM_RMEM
 
 	  This driver can also be built as a module. If so, the module
 	  will be called nvmem-rmem.
+
+config NVMEM_IOMAP
+	tristate "I/O mapped NVMEM support"
+	depends on HAS_IOMEM
+	help
+	  This driver supports NVMEM that can be accessed using I/O mapping.
+
 endif
drivers/nvmem/Makefile +2 -0
index 5376b8e0dae5..88a3b6979c53 100644
@@ -57,3 +57,5 @@ obj-$(CONFIG_SPRD_EFUSE)	+= nvmem_sprd_efuse.o
 nvmem_sprd_efuse-y		:= sprd-efuse.o
 obj-$(CONFIG_NVMEM_RMEM) 	+= nvmem-rmem.o
 nvmem-rmem-y			:= rmem.o
+obj-$(CONFIG_NVMEM_IOMAP)	+= nvmem_iomap.o
+nvmem_iomap-y			:= iomap.o
drivers/nvmem/iomap.c new file +99 -0

99 lines not on this page · raw message

Signature · 1 line
2.26.2
#2 lore raw

Re: [PATCH 2/2] nvmem: iomap: new driver exposing NVMEM accessible using I/O mapping

Quoted 73 lines · Rafał Miłecki

From: Rafał Miłecki <[email protected]>

This is a generic NVMEM access method used e.g. by Broadcom for their NVRAM on MIPS and Northstar devices.

---
  drivers/nvmem/Kconfig  |  7 +++
  drivers/nvmem/Makefile |  2 +
  drivers/nvmem/iomap.c  | 99 ++++++++++++++++++++++++++++++++++++++++++
  3 files changed, 108 insertions(+)

create mode 100644 drivers/nvmem/iomap.c

drivers/nvmem/Kconfig +7 -0
index 75d2594c16e1..3d5c5684685d 100644
@@ -278,4 +278,11 @@ config NVMEM_RMEM
 
  	  This driver can also be built as a module. If so, the module
  	  will be called nvmem-rmem.
+
+config NVMEM_IOMAP
+	tristate "I/O mapped NVMEM support"
+	depends on HAS_IOMEM
+	help
+	  This driver supports NVMEM that can be accessed using I/O mapping.
+
  endif
drivers/nvmem/Makefile +2 -0
index 5376b8e0dae5..88a3b6979c53 100644
@@ -57,3 +57,5 @@ obj-$(CONFIG_SPRD_EFUSE)	+= nvmem_sprd_efuse.o
  nvmem_sprd_efuse-y		:= sprd-efuse.o
  obj-$(CONFIG_NVMEM_RMEM) 	+= nvmem-rmem.o
  nvmem-rmem-y			:= rmem.o
+obj-$(CONFIG_NVMEM_IOMAP)	+= nvmem_iomap.o
+nvmem_iomap-y			:= iomap.o
drivers/nvmem/iomap.c new file +28 -0
new file mode 100644
index 000000000000..ab6b40858a64
@@ -0,0 +1,99 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright (C) 2021 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>
+
+struct iomap {
+	struct device *dev;
+	void __iomem *base;
+};
+
+static int iomap_read(void *context, unsigned int offset, void *val,
+		      size_t bytes)
+{
+	struct iomap *priv = context;
+	u8 *src = priv->base + offset;
+	u8 *dst = val;
+	size_t tmp;
+
+	tmp = offset % 4;
+	memcpy_fromio(dst, src, tmp);
+	dst += tmp;
+	src += tmp;

drivers/nvmem/iomap.c line 38

Quoted 10 lines
drivers/nvmem/iomap.c new file +10 -0
+	bytes -= tmp;
+
+	tmp = rounddown(bytes, 4);
+	__ioread32_copy(dst, src, tmp / 4);
+	dst += tmp;
+	src += tmp;
+	bytes -= tmp;
+
+	memcpy_fromio(dst, src, bytes);
+

You could just do this!

	while (bytes--)
		*val++ = readb(priv->base + offset + i++);
Quoted 15 lines
drivers/nvmem/iomap.c new file +15 -0
+	return 0;
+}
+
+static int iomap_probe(struct platform_device *pdev)
+{
+	struct nvmem_config config = {
+		.name = "iomap",
+		.reg_read = iomap_read,
+	};
+	struct device *dev = &pdev->dev;
+	struct resource *res;
+	struct iomap *priv;
+
+	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
+	if (!priv)

drivers/nvmem/iomap.c line 61

Quoted 10 lines
drivers/nvmem/iomap.c new file +8 -0
+		return -ENOMEM;
+	priv->dev = dev;
+
+	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
 
 
+	if (!res) {
+		dev_err(dev, "Failed to get resource\n");
+		return -ENODEV;
+	}

This is redundant check and error message!

You can just do :
res = platform_get_resource(pdev, IORESOURCE_MEM, 0); priv->base = devm_ioremap_resource(dev, res);

drivers/nvmem/iomap.c line 65

Quoted 4 lines
drivers/nvmem/iomap.c new file +4 -0
+
+	priv->base = devm_ioremap_resource(dev, res);
+	if (IS_ERR(priv->base)) {
+		dev_err(dev, "Failed to map resource: %ld\n", PTR_ERR(priv->base));

This message is redundant!

Quoted 5 lines
drivers/nvmem/iomap.c new file +3 -0
+		return PTR_ERR(priv->base);
+	}
 
 
+

drivers/nvmem/iomap.c line 78

Quoted 10 lines
drivers/nvmem/iomap.c new file +10 -0
+	config.dev = dev;
+	config.priv = priv;
+	config.size = resource_size(res);
+
+	return PTR_ERR_OR_ZERO(devm_nvmem_register(dev, &config));
+}
+
+static const struct of_device_id iomap_of_match_table[] = {
+	{ .compatible = "brcm,nvram", },
+	{ .compatible = "nvmem-iomap", },

Generic compatible does not really makes sense at all, you can probably consider adding some generic functions rather than having a compatible, in case you want to reduce code duplication!

Quoted 10 lines
drivers/nvmem/iomap.c new file +10 -0
+	{},
+};
+
+static struct platform_driver iomap_driver = {
+	.probe = iomap_probe,
+	.driver = {
+		.name = "nvmem_iomap",
+		.of_match_table = iomap_of_match_table,
+	},
+};

drivers/nvmem/iomap.c line 98

Quoted 10 lines
drivers/nvmem/iomap.c new file +10 -0
+
+static int __init iomap_init(void)
+{
+	return platform_driver_register(&iomap_driver);
+}
+
+subsys_initcall_sync(iomap_init);
+
+MODULE_AUTHOR("Rafał Miłecki");
+MODULE_LICENSE("GPL v2");

it should be
MODULE_LICENSE("GPL");

Quoted 2 lines

+MODULE_DEVICE_TABLE(of, iomap_of_match_table);

#3 lore raw

Re: [PATCH 2/2] nvmem: iomap: new driver exposing NVMEM accessible using I/O mapping

Quoted 87 lines · Srinivas Kandagatla
Quoted 72 lines · Rafał Miłecki

From: Rafał Miłecki <[email protected]>

This is a generic NVMEM access method used e.g. by Broadcom for their NVRAM on MIPS and Northstar devices.

---
  drivers/nvmem/Kconfig  |  7 +++
  drivers/nvmem/Makefile |  2 +
  drivers/nvmem/iomap.c  | 99 ++++++++++++++++++++++++++++++++++++++++++

  3 files changed, 108 insertions(+)

  create mode 100644 drivers/nvmem/iomap.c

drivers/nvmem/Kconfig +0 -0
index 75d2594c16e1..3d5c5684685d 100644
@@ -278,4 +278,11 @@ config NVMEM_RMEM

        This driver can also be built as a module. If so, the module will be called nvmem-rmem.

drivers/nvmem/Kconfig +7 -0
+
+config NVMEM_IOMAP
+    tristate "I/O mapped NVMEM support"
+    depends on HAS_IOMEM
+    help
+      This driver supports NVMEM that can be accessed using I/O mapping.
+

  endif

drivers/nvmem/Makefile +0 -0
index 5376b8e0dae5..88a3b6979c53 100644
@@ -57,3 +57,5 @@ obj-$(CONFIG_SPRD_EFUSE)    += nvmem_sprd_efuse.o

  nvmem_sprd_efuse-y        := sprd-efuse.o
  obj-$(CONFIG_NVMEM_RMEM)     += nvmem-rmem.o
  nvmem-rmem-y            := rmem.o

drivers/nvmem/Makefile +2 -0
+obj-$(CONFIG_NVMEM_IOMAP)    += nvmem_iomap.o
+nvmem_iomap-y            := iomap.o
drivers/nvmem/iomap.c new file +28 -0
new file mode 100644
index 000000000000..ab6b40858a64
@@ -0,0 +1,99 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright (C) 2021 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>
+
+struct iomap {
+    struct device *dev;
+    void __iomem *base;
+};
+
+static int iomap_read(void *context, unsigned int offset, void *val,
+              size_t bytes)
+{
+    struct iomap *priv = context;
+    u8 *src = priv->base + offset;
+    u8 *dst = val;
+    size_t tmp;
+
+    tmp = offset % 4;
+    memcpy_fromio(dst, src, tmp);
+    dst += tmp;
+    src += tmp;

drivers/nvmem/iomap.c line 38

Quoted 10 lines
drivers/nvmem/iomap.c new file +10 -0
+    bytes -= tmp;
+
+    tmp = rounddown(bytes, 4);
+    __ioread32_copy(dst, src, tmp / 4);
+    dst += tmp;
+    src += tmp;
+    bytes -= tmp;
+
+    memcpy_fromio(dst, src, bytes);
+

You could just do this!

Quoted 2 lines

    while (bytes--)
        *val++ = readb(priv->base + offset + i++);

Do you mean that as replacement for "memcpy_fromio" or the whole function code?
The reason for using __ioread32_copy() was to improve reading performance (using aligned 32 bit access where possible). I'm not sure if that really matters?

P.S.
Please don't yell at me in every sentence :( Makes me a bit sad :(

#4 lore raw

Re: [PATCH 2/2] nvmem: iomap: new driver exposing NVMEM accessible using I/O mapping

Quoted 27 lines · Rafał Miłecki
Quoted 27 lines
Quoted 13 lines
drivers/nvmem/iomap.c new file +12 -0
+static int iomap_read(void *context, unsigned int offset, void *val,
+              size_t bytes)
+{
+    struct iomap *priv = context;
+    u8 *src = priv->base + offset;
+    u8 *dst = val;
+    size_t tmp;
+
+    tmp = offset % 4;
+    memcpy_fromio(dst, src, tmp);
+    dst += tmp;
+    src += tmp;

drivers/nvmem/iomap.c line 38

Quoted 10 lines
drivers/nvmem/iomap.c new file +10 -0
+    bytes -= tmp;
+
+    tmp = rounddown(bytes, 4);
+    __ioread32_copy(dst, src, tmp / 4);
+    dst += tmp;
+    src += tmp;
+    bytes -= tmp;
+
+    memcpy_fromio(dst, src, bytes);
+

You could just do this!

Quoted 5 lines
Quoted 2 lines

     while (bytes--)
         *val++ = readb(priv->base + offset + i++);

Do you mean that as replacement for "memcpy_fromio" or the whole function code?

Yes please!

Quoted 3 lines

The reason for using __ioread32_copy() was to improve reading performance (using aligned 32 bit access where possible). I'm not sure if that really matters?

Just simple while loop is much readable than the previous code TBH!

Quoted 4 lines

P.S.
Please don't yell at me in every sentence :( Makes me a bit sad :(

Sorry!! I did not mean anything as such! :-)

--srini

#5 lore raw

Re: [PATCH 2/2] nvmem: iomap: new driver exposing NVMEM accessible using I/O mapping

Quoted 40 lines · Srinivas Kandagatla
Quoted 27 lines · Rafał Miłecki
Quoted 27 lines
Quoted 13 lines
drivers/nvmem/iomap.c new file +12 -0
+static int iomap_read(void *context, unsigned int offset, void *val,
+              size_t bytes)
+{
+    struct iomap *priv = context;
+    u8 *src = priv->base + offset;
+    u8 *dst = val;
+    size_t tmp;
+
+    tmp = offset % 4;
+    memcpy_fromio(dst, src, tmp);
+    dst += tmp;
+    src += tmp;

drivers/nvmem/iomap.c line 38

Quoted 10 lines
drivers/nvmem/iomap.c new file +10 -0
+    bytes -= tmp;
+
+    tmp = rounddown(bytes, 4);
+    __ioread32_copy(dst, src, tmp / 4);
+    dst += tmp;
+    src += tmp;
+    bytes -= tmp;
+
+    memcpy_fromio(dst, src, bytes);
+

You could just do this!

Quoted 5 lines
Quoted 2 lines

     while (bytes--)
         *val++ = readb(priv->base + offset + i++);

Do you mean that as replacement for "memcpy_fromio" or the whole function code?

Yes please!

Quoted 3 lines

The reason for using __ioread32_copy() was to improve reading performance (using aligned 32 bit access where possible). I'm not sure if that really matters?

Quoted 7 lines

Just simple while loop is much readable than the previous code TBH!

Quoted 4 lines

P.S.
Please don't yell at me in every sentence :( Makes me a bit sad :(

Sorry!! I did not mean anything as such! :-)

All clear (I hope)! Thanks for your review!