mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Andrey Smirnov <andrew.smirnov@gmail.com>
To: barebox@lists.infradead.org
Cc: Andrey Smirnov <andrew.smirnov@gmail.com>
Subject: [PATCH 17/18] [RFC] nvmem: Add nvmem-ro-of-blob driver
Date: Tue, 16 Feb 2016 17:29:18 -0800	[thread overview]
Message-ID: <1455672559-25061-18-git-send-email-andrew.smirnov@gmail.com> (raw)
In-Reply-To: <1455672559-25061-1-git-send-email-andrew.smirnov@gmail.com>

Add a driver that implements provisions to allow creationg of fake nvmem
devices and cells whose data comes from device tree  blob.

Signed-off-by: Andrey Smirnov <andrew.smirnov@gmail.com>
---
 drivers/misc/Makefile           |   1 +
 drivers/misc/nvmem-ro-of-blob.c | 196 ++++++++++++++++++++++++++++++++++++++++
 2 files changed, 197 insertions(+)
 create mode 100644 drivers/misc/nvmem-ro-of-blob.c

diff --git a/drivers/misc/Makefile b/drivers/misc/Makefile
index 94d2a4f..c7f56f8 100644
--- a/drivers/misc/Makefile
+++ b/drivers/misc/Makefile
@@ -6,3 +6,4 @@ obj-$(CONFIG_JTAG)		+= jtag.o
 obj-$(CONFIG_SRAM)		+= sram.o
 obj-$(CONFIG_STATE_DRV)		+= state.o
 obj-y += mac-address-map.o
+obj-y += nvmem-ro-of-blob.o
diff --git a/drivers/misc/nvmem-ro-of-blob.c b/drivers/misc/nvmem-ro-of-blob.c
new file mode 100644
index 0000000..8a9f091
--- /dev/null
+++ b/drivers/misc/nvmem-ro-of-blob.c
@@ -0,0 +1,196 @@
+#include <common.h>
+#include <driver.h>
+#include <init.h>
+#include <malloc.h>
+#include <of.h>
+#include <state.h>
+#include <io.h>
+#include <fcntl.h>
+#include <net.h>
+#include <regmap.h>
+
+#include <linux/err.h>
+#include <linux/nvmem-provider.h>
+#include <linux/nvmem-consumer.h>
+
+struct nvmem_ro_of_blob_cell {
+	struct list_head node;
+
+        unsigned int start;
+	unsigned int end;
+	unsigned int size;
+
+	u8 *data;
+};
+
+struct nvmem_ro_of_blob {
+	struct regmap *map;
+	struct nvmem_config config;
+	struct nvmem_device *nvmem;
+	struct list_head cells;
+};
+
+struct nvmem_ro_of_blob_cell *
+nvmem_ro_of_blob_reg_to_cell(struct nvmem_ro_of_blob *nrob,
+			     unsigned int reg)
+{
+	struct nvmem_ro_of_blob_cell *cell;
+
+	list_for_each_entry(cell, &nrob->cells, node) {
+		if (cell->start <= reg && reg <= cell->end)
+			return cell;
+	}
+
+	return NULL;
+}
+
+
+static int nvmem_ro_of_blob_reg_write(void *ctx, unsigned int reg,
+				      unsigned int val)
+{
+	return -ENOTSUPP;
+}
+
+static int nvmem_ro_of_blob_reg_read(void *ctx, unsigned int reg,
+				     unsigned int *val)
+{
+	struct nvmem_ro_of_blob *nrob = ctx;
+	struct nvmem_ro_of_blob_cell *cell =
+		nvmem_ro_of_blob_reg_to_cell(nrob, reg);
+
+	if (!cell)
+		return -ENOENT;
+
+	*val = cell->data[reg - cell->start];
+	return 0;
+}
+
+static const struct regmap_bus nvmem_ro_of_blob_regmap_bus = {
+	.reg_write = nvmem_ro_of_blob_reg_write,
+	.reg_read  = nvmem_ro_of_blob_reg_read,
+};
+
+static const struct regmap_config nvmem_ro_of_blob_regmap_config = {
+	.reg_bits	= 8,
+	.val_bits	= 8,
+	.reg_stride	= 1,
+	.max_register	= 0xbc,
+};
+
+static int nvmem_ro_of_blob_validate_dt(struct device_node *np)
+{
+	/* struct device_node *child; */
+	/* for_each_child_of_node(np, child) { */
+	/* 	const __be32 *addr; */
+	/* 	int len; */
+
+	/* 	addr = of_get_property(child, "reg", &len); */
+	/* 	if (!addr) */
+	/* 		return -EINVAL; */
+
+	/* } */
+
+	return 0;
+}
+
+static int nvmem_ro_of_blob_probe(struct device_d *dev)
+{
+	int ret;
+	struct list_head *p, *n;
+	struct device_node *np = dev->device_node;
+	struct device_node *child;
+
+	struct nvmem_ro_of_blob *nrob;
+	struct nvmem_ro_of_blob_cell *cell;
+
+	BUG_ON(!np);
+
+	if (!of_get_next_child(np, NULL)) {
+		dev_dbg(dev, "Device node doesn't specify any cells\n");
+		return -EINVAL;
+	}
+
+	ret = nvmem_ro_of_blob_validate_dt(np);
+	if (ret < 0) {
+		dev_dbg(dev, "Device tree validation failed\n");
+		return ret;
+	}
+
+	nrob = xzalloc(sizeof(*nrob));
+	INIT_LIST_HEAD(&nrob->cells);
+
+	for_each_child_of_node(np, child) {
+		struct property *pp;
+		const __be32 *addr;
+		int len;
+
+		addr = of_get_property(child, "reg", &len);
+		BUG_ON(!addr);
+		cell = xzalloc(sizeof(*cell));
+
+		cell->start = be32_to_cpup(addr++);
+		cell->size  = be32_to_cpup(addr);
+		cell->end   = cell->start + cell->size - 1;
+
+		pp = of_find_property(child, "data", NULL);
+		BUG_ON(!pp || pp->length != cell->size);
+		cell->data = pp->value;
+
+		list_add_tail(&cell->node, &nrob->cells);
+	}
+
+	nrob->map = regmap_init(dev,
+				&nvmem_ro_of_blob_regmap_bus,
+				nrob,
+				&nvmem_ro_of_blob_regmap_config);
+	if (IS_ERR(nrob->map)) {
+		dev_dbg(dev, "Failed to initilize regmap\n");
+		ret = PTR_ERR(nrob->map);
+		goto free_resources;
+	}
+
+	nrob->config.name = "nvmem-ro-of-blob";
+	nrob->config.read_only = true;
+	nrob->config.dev = dev;
+
+	nrob->nvmem = nvmem_register(&nrob->config, nrob->map);
+	if (IS_ERR(nrob->nvmem)) {
+		dev_dbg(dev, "Filed to register nvmem device\n");
+		ret = PTR_ERR(nrob->nvmem);
+		goto free_resources;
+	}
+
+	dev_dbg(dev, "probed\n");
+	return 0;
+
+free_resources:
+	if (!IS_ERR_OR_NULL(nrob->map))
+		regmap_exit(nrob->map);
+
+	list_for_each_safe(p, n, &nrob->cells) {
+		cell = list_entry(p,
+				  struct nvmem_ro_of_blob_cell,
+				  node);
+		list_del(p);
+		free(cell->data);
+		free(cell);
+	}
+	free(nrob);
+
+	return ret;
+}
+
+static __maybe_unused struct of_device_id nvmem_ro_of_blob_ids[] = {
+	{
+		.compatible = "barebox,nvmem-ro-of-blob",
+	}, {
+		/* sentinel */
+	}
+};
+
+static struct driver_d nvmem_ro_of_blob_driver = {
+	.name = "nvmem-ro-of-blob",
+	.probe = nvmem_ro_of_blob_probe,
+	.of_compatible = DRV_OF_COMPAT(nvmem_ro_of_blob_ids),
+};
+device_platform_driver(nvmem_ro_of_blob_driver);
-- 
2.5.0


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

  parent reply	other threads:[~2016-02-17  1:30 UTC|newest]

Thread overview: 31+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-02-17  1:29 [PATCH 00/17] RFC for additional 'nvmem' plumbing Andrey Smirnov
2016-02-17  1:29 ` [PATCH 01/18] common: Add EPROBE_DEFER to strerror Andrey Smirnov
2016-02-17  1:29 ` [PATCH 02/18] base: driver.c: Coalesce error checking code Andrey Smirnov
2016-02-17  1:29 ` [PATCH 03/18] [RFC] at91: Make IS_ERR work for I/O pointers Andrey Smirnov
2016-02-17  8:34   ` Sascha Hauer
2016-02-17 20:39     ` Andrey Smirnov
2016-02-18 10:26       ` Sascha Hauer
2016-02-17  1:29 ` [PATCH 04/18] [RFC] base/driver.c: Remove dev_request_mem_region_err_null Andrey Smirnov
2016-02-17  1:29 ` [PATCH 05/18] i2c-at91: Use IS_ERR instead of checking for NULL Andrey Smirnov
2016-02-17  1:29 ` [PATCH 06/18] clk-imx6: Call clk_enable on mmdc_ch0_axi_podf Andrey Smirnov
2016-02-17  1:29 ` [PATCH 07/18] fec_imx: Deallocate clocks when probe fails Andrey Smirnov
2016-02-17  1:29 ` [PATCH 08/18] [RFC] base: Introduce dev_request_mem_resource Andrey Smirnov
2016-02-17  1:29 ` [PATCH 09/18] fec_imx: Deallocate I/O resources if probe fails Andrey Smirnov
2016-02-17  1:29 ` [PATCH 10/18] fec_imx: Free phy_reset GPIO if when " Andrey Smirnov
2016-02-17  1:29 ` [PATCH 11/18] fec_imx: Use FEC_ECNTRL_RESET instead of a magic number Andrey Smirnov
2016-02-17  1:29 ` [PATCH 12/18] fec_imx: Impelemnt reset timeout Andrey Smirnov
2016-02-17  8:43   ` Sascha Hauer
2016-02-17  1:29 ` [PATCH 13/18] fec_imx: Deallocate DMA buffers when probe fails Andrey Smirnov
2016-02-17  1:29 ` [PATCH 14/18] fec_imx: Unregister MDIO " Andrey Smirnov
2016-02-17  1:29 ` [PATCH 15/18] [RFC] net: eth: Always use DEVICE_ID_DYNAMIC Andrey Smirnov
2016-02-17  2:40   ` Trent Piepho
2016-02-17  4:16     ` Andrey Smirnov
2016-02-18 19:30       ` Trent Piepho
2016-02-19 17:17         ` Andrey Smirnov
2016-02-17  1:29 ` [PATCH 16/18] [RFC] nvmem: Add of_nvmem_cell_from_cell_np Andrey Smirnov
2016-02-17  1:29 ` Andrey Smirnov [this message]
2016-02-17  8:52   ` [PATCH 17/18] [RFC] nvmem: Add nvmem-ro-of-blob driver Sascha Hauer
2016-02-17 20:40     ` Andrey Smirnov
2016-02-17  1:29 ` [PATCH 18/18] [RFC] nvmem: Add nvmem-sg driver Andrey Smirnov
2016-02-17  3:09 ` [PATCH 00/17] RFC for additional 'nvmem' plumbing Trent Piepho
2016-02-17  4:20   ` Andrey Smirnov

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=1455672559-25061-18-git-send-email-andrew.smirnov@gmail.com \
    --to=andrew.smirnov@gmail.com \
    --cc=barebox@lists.infradead.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