Recently, nvmem cell and MTD partition bindings were made to coexist:
Partitions can now be compatible = "nvmem-cells"; which registers a
NVMEM provider and interprets its child nodes as cells. Teach barebox
about this. This allows fetching NVMEM cells from MTD partitions and
hostfiles.

Signed-off-by: Ahmad Fatoum <a.fat...@pengutronix.de>
---
 drivers/nvmem/Makefile         |  2 +-
 drivers/nvmem/core.c           | 12 +++++-----
 drivers/nvmem/partition.c      | 40 ++++++++++++++++++++++++++++++++++
 drivers/of/partition.c         |  7 ++++++
 include/linux/nvmem-provider.h |  8 +++++++
 5 files changed, 63 insertions(+), 6 deletions(-)
 create mode 100644 drivers/nvmem/partition.c

diff --git a/drivers/nvmem/Makefile b/drivers/nvmem/Makefile
index 6330f3d6e681..53c02dc7850c 100644
--- a/drivers/nvmem/Makefile
+++ b/drivers/nvmem/Makefile
@@ -3,7 +3,7 @@
 #
 
 obj-$(CONFIG_NVMEM)            += nvmem_core.o
-nvmem_core-y                   := core.o regmap.o
+nvmem_core-y                   := core.o regmap.o partition.o
 
 obj-$(CONFIG_NVMEM_RMEM)       += rmem.o
 
diff --git a/drivers/nvmem/core.c b/drivers/nvmem/core.c
index 3c14e390de39..c060e627db4f 100644
--- a/drivers/nvmem/core.c
+++ b/drivers/nvmem/core.c
@@ -205,7 +205,7 @@ struct nvmem_device *nvmem_register(const struct 
nvmem_config *config)
        nvmem->size = config->size;
        nvmem->dev.parent = config->dev;
        nvmem->bus = config->bus;
-       np = config->dev->device_node;
+       np = config->cdev ? config->cdev->device_node : 
config->dev->device_node;
        nvmem->dev.device_node = np;
        nvmem->priv = config->priv;
 
@@ -223,10 +223,12 @@ struct nvmem_device *nvmem_register(const struct 
nvmem_config *config)
                return ERR_PTR(rval);
        }
 
-       rval = nvmem_register_cdev(nvmem, config->name);
-       if (rval) {
-               kfree(nvmem);
-               return ERR_PTR(rval);
+       if (!config->cdev) {
+               rval = nvmem_register_cdev(nvmem, config->name);
+               if (rval) {
+                       kfree(nvmem);
+                       return ERR_PTR(rval);
+               }
        }
 
        list_add_tail(&nvmem->node, &nvmem_devs);
diff --git a/drivers/nvmem/partition.c b/drivers/nvmem/partition.c
new file mode 100644
index 000000000000..3f0bdc58de40
--- /dev/null
+++ b/drivers/nvmem/partition.c
@@ -0,0 +1,40 @@
+// SPDX-License-Identifier: GPL-2.0
+#include <common.h>
+#include <driver.h>
+#include <malloc.h>
+#include <xfuncs.h>
+#include <errno.h>
+#include <init.h>
+#include <io.h>
+#include <linux/nvmem-provider.h>
+
+static int nvmem_cdev_write(void *ctx, unsigned offset, const void *val, 
size_t bytes)
+{
+       return cdev_write(ctx, val, bytes, offset, 0);
+}
+
+static int nvmem_cdev_read(void *ctx, unsigned offset, void *buf, size_t bytes)
+{
+       return cdev_read(ctx, buf, bytes, offset, 0);
+}
+
+static struct nvmem_bus nvmem_cdev_bus = {
+       .read = nvmem_cdev_read,
+       .write = nvmem_cdev_write,
+};
+
+struct nvmem_device *nvmem_partition_register(struct cdev *cdev)
+{
+       struct nvmem_config config = {};
+
+       config.name = cdev->name;
+       config.dev = cdev->dev;
+       config.cdev = cdev;
+       config.priv = cdev;
+       config.stride = 1;
+       config.word_size = 1;
+       config.size = cdev->size;
+       config.bus = &nvmem_cdev_bus;
+
+       return nvmem_register(&config);
+}
diff --git a/drivers/of/partition.c b/drivers/of/partition.c
index b71716218b44..b6d0523fd960 100644
--- a/drivers/of/partition.c
+++ b/drivers/of/partition.c
@@ -20,6 +20,7 @@
 #include <linux/mtd/mtd.h>
 #include <linux/err.h>
 #include <nand.h>
+#include <linux/nvmem-provider.h>
 #include <init.h>
 #include <globalvar.h>
 
@@ -83,6 +84,12 @@ struct cdev *of_parse_partition(struct cdev *cdev, struct 
device_node *node)
        if (new)
                new->device_node = node;;
 
+       if (IS_ENABLED(CONFIG_NVMEM) && of_device_is_compatible(node, 
"nvmem-cells")) {
+               struct nvmem_device *nvmem = nvmem_partition_register(new);
+               if (IS_ERR(nvmem))
+                       dev_warn(cdev->dev, "nvmem registeration failed: 
%pe\n", nvmem);
+       }
+
        free(filename);
 
        return new;
diff --git a/include/linux/nvmem-provider.h b/include/linux/nvmem-provider.h
index 2d738983736e..a293f60c1ef3 100644
--- a/include/linux/nvmem-provider.h
+++ b/include/linux/nvmem-provider.h
@@ -26,6 +26,7 @@ struct nvmem_config {
        struct device_d         *dev;
        const char              *name;
        bool                    read_only;
+       struct cdev             *cdev;
        int                     stride;
        int                     word_size;
        int                     size;
@@ -34,11 +35,13 @@ struct nvmem_config {
 };
 
 struct regmap;
+struct cdev;
 
 #if IS_ENABLED(CONFIG_NVMEM)
 
 struct nvmem_device *nvmem_register(const struct nvmem_config *cfg);
 struct nvmem_device *nvmem_regmap_register(struct regmap *regmap, const char 
*name);
+struct nvmem_device *nvmem_partition_register(struct cdev *cdev);
 
 #else
 
@@ -52,5 +55,10 @@ static inline struct nvmem_device 
*nvmem_regmap_register(struct regmap *regmap,
        return ERR_PTR(-ENOSYS);
 }
 
+static inline struct nvmem_device *nvmem_partition_register(struct cdev *cdev)
+{
+       return ERR_PTR(-ENOSYS);
+}
+
 #endif /* CONFIG_NVMEM */
 #endif  /* ifndef _LINUX_NVMEM_PROVIDER_H */
-- 
2.29.2


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

Reply via email to