devicetree.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Rafał Miłecki" <zajec5@gmail.com>
To: Srinivas Kandagatla <srinivas.kandagatla@linaro.org>,
	Rob Herring <robh+dt@kernel.org>
Cc: devicetree@vger.kernel.org, linux-arm-kernel@lists.infradead.org,
	linux-mips@vger.kernel.org,
	"Florian Fainelli" <f.fainelli@gmail.com>,
	"Vivek Unune" <npcomplete13@gmail.com>,
	bcm-kernel-feedback-list@broadcom.com,
	linux-kernel@vger.kernel.org, "Rafał Miłecki" <rafal@milecki.pl>
Subject: [PATCH 2/2] nvmem: iomap: new driver exposing NVMEM accessible using I/O mapping
Date: Thu,  4 Mar 2021 15:41:32 +0100	[thread overview]
Message-ID: <20210304144132.24098-2-zajec5@gmail.com> (raw)
In-Reply-To: <20210304144132.24098-1-zajec5@gmail.com>

From: Rafał Miłecki <rafal@milecki.pl>

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

Signed-off-by: Rafał Miłecki <rafal@milecki.pl>
---
 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

diff --git a/drivers/nvmem/Kconfig b/drivers/nvmem/Kconfig
index 75d2594c16e1..3d5c5684685d 100644
--- a/drivers/nvmem/Kconfig
+++ b/drivers/nvmem/Kconfig
@@ -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
diff --git a/drivers/nvmem/Makefile b/drivers/nvmem/Makefile
index 5376b8e0dae5..88a3b6979c53 100644
--- a/drivers/nvmem/Makefile
+++ b/drivers/nvmem/Makefile
@@ -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
diff --git a/drivers/nvmem/iomap.c b/drivers/nvmem/iomap.c
new file mode 100644
index 000000000000..ab6b40858a64
--- /dev/null
+++ b/drivers/nvmem/iomap.c
@@ -0,0 +1,99 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright (C) 2021 Rafał Miłecki <rafal@milecki.pl>
+ */
+
+#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;
+	bytes -= tmp;
+
+	tmp = rounddown(bytes, 4);
+	__ioread32_copy(dst, src, tmp / 4);
+	dst += tmp;
+	src += tmp;
+	bytes -= tmp;
+
+	memcpy_fromio(dst, src, bytes);
+
+	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)
+		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;
+	}
+
+	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));
+		return PTR_ERR(priv->base);
+	}
+
+	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", },
+	{},
+};
+
+static struct platform_driver iomap_driver = {
+	.probe = iomap_probe,
+	.driver = {
+		.name = "nvmem_iomap",
+		.of_match_table = iomap_of_match_table,
+	},
+};
+
+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");
+MODULE_DEVICE_TABLE(of, iomap_of_match_table);
-- 
2.26.2


  reply	other threads:[~2021-03-04 14:43 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-03-04 14:41 [PATCH 1/2] dt-bindings: nvmem: add binding for I/O mapped NVMEM Rafał Miłecki
2021-03-04 14:41 ` Rafał Miłecki [this message]
2021-03-05 10:02   ` [PATCH 2/2] nvmem: iomap: new driver exposing NVMEM accessible using I/O mapping Srinivas Kandagatla
2021-03-05 10:24     ` Rafał Miłecki
2021-03-05 10:33       ` Srinivas Kandagatla
2021-03-05 10:39         ` Rafał Miłecki

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210304144132.24098-2-zajec5@gmail.com \
    --to=zajec5@gmail.com \
    --cc=bcm-kernel-feedback-list@broadcom.com \
    --cc=devicetree@vger.kernel.org \
    --cc=f.fainelli@gmail.com \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mips@vger.kernel.org \
    --cc=npcomplete13@gmail.com \
    --cc=rafal@milecki.pl \
    --cc=robh+dt@kernel.org \
    --cc=srinivas.kandagatla@linaro.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).