--- /dev/null
+From bcdf0315a61a29eb753a607d3a85a4032de72d94 Mon Sep 17 00:00:00 2001
+From: =?UTF-8?q?Rafa=C5=82=20Mi=C5=82ecki?= <rafal@milecki.pl>
+Date: Tue, 10 May 2022 15:12:59 +0200
+Subject: [PATCH] mtd: call of_platform_populate() for MTD partitions
+MIME-Version: 1.0
+Content-Type: text/plain; charset=UTF-8
+Content-Transfer-Encoding: 8bit
+
+Until this change MTD subsystem supported handling partitions only with
+MTD partitions parsers. That's a specific / limited API designed around
+partitions.
+
+Some MTD partitions may however require different handling. They may
+contain specific data that needs to be parsed and somehow extracted. For
+that purpose MTD subsystem should allow binding of standard platform
+drivers.
+
+An example can be U-Boot (sub)partition with environment variables.
+There exist a "u-boot,env" DT binding for MTD (sub)partition that
+requires an NVMEM driver.
+
+Ref: 5db1c2dbc04c ("dt-bindings: nvmem: add U-Boot environment variables binding")
+Signed-off-by: Rafał Miłecki <rafal@milecki.pl>
+Signed-off-by: Miquel Raynal <miquel.raynal@bootlin.com>
+Link: https://lore.kernel.org/linux-mtd/20220510131259.555-1-zajec5@gmail.com
+---
+ drivers/mtd/mtdpart.c | 9 +++++++++
+ 1 file changed, 9 insertions(+)
+
+--- a/drivers/mtd/mtdpart.c
++++ b/drivers/mtd/mtdpart.c
+@@ -17,6 +17,7 @@
+ #include <linux/mtd/partitions.h>
+ #include <linux/err.h>
+ #include <linux/of.h>
++#include <linux/of_platform.h>
+
+ #include "mtdcore.h"
+
+@@ -893,10 +894,16 @@ static int mtd_part_of_parse(struct mtd_
+ struct mtd_part_parser *parser;
+ struct device_node *np;
+ struct property *prop;
++ struct device *dev;
+ const char *compat;
+ const char *fixed = "fixed-partitions";
+ int ret, err = 0;
+
++ dev = &master->dev;
++ /* Use parent device (controller) if the top level MTD is not registered */
++ if (!IS_ENABLED(CONFIG_MTD_PARTITIONED_MASTER) && !mtd_is_partition(master))
++ dev = master->dev.parent;
++
+ np = mtd_get_of_node(master);
+ if (mtd_is_partition(master))
+ of_node_get(np);
+@@ -909,6 +916,7 @@ static int mtd_part_of_parse(struct mtd_
+ continue;
+ ret = mtd_part_do_parse(parser, master, pparts, NULL);
+ if (ret > 0) {
++ of_platform_populate(np, NULL, NULL, dev);
+ of_node_put(np);
+ return ret;
+ }
+@@ -916,6 +924,7 @@ static int mtd_part_of_parse(struct mtd_
+ if (ret < 0 && !err)
+ err = ret;
+ }
++ of_platform_populate(np, NULL, NULL, dev);
+ of_node_put(np);
+
+ /*
depends on m
--- a/drivers/mtd/mtdpart.c
+++ b/drivers/mtd/mtdpart.c
-@@ -15,10 +15,12 @@
+@@ -15,11 +15,13 @@
#include <linux/kmod.h>
#include <linux/mtd/mtd.h>
#include <linux/mtd/partitions.h>
+#include <linux/magic.h>
#include <linux/err.h>
#include <linux/of.h>
+ #include <linux/of_platform.h>
#include "mtdcore.h"
+#include "mtdsplit/mtdsplit.h"
/* Our partition linked list */
static LIST_HEAD(mtd_partitions);
-@@ -38,6 +40,8 @@ struct mtd_part {
+@@ -39,6 +41,8 @@ struct mtd_part {
struct list_head list;
};
/*
* Given a pointer to the MTD object in the mtd_part structure, we can retrieve
* the pointer to that structure.
-@@ -612,6 +616,7 @@ int mtd_add_partition(struct mtd_info *p
+@@ -613,6 +617,7 @@ int mtd_add_partition(struct mtd_info *p
if (ret)
goto err_remove_part;
mtd_add_partition_attrs(new);
return 0;
-@@ -698,6 +703,29 @@ int mtd_del_partition(struct mtd_info *m
+@@ -699,6 +704,29 @@ int mtd_del_partition(struct mtd_info *m
}
EXPORT_SYMBOL_GPL(mtd_del_partition);
/*
* This function, given a master MTD object and a partition table, creates
* and registers slave MTD objects which are bound to the master according to
-@@ -738,6 +766,7 @@ int add_mtd_partitions(struct mtd_info *
+@@ -739,6 +767,7 @@ int add_mtd_partitions(struct mtd_info *
goto err_del_partitions;
}
--- a/drivers/mtd/mtdpart.c
+++ b/drivers/mtd/mtdpart.c
-@@ -41,6 +41,10 @@ struct mtd_part {
+@@ -42,6 +42,10 @@ struct mtd_part {
};
static void mtd_partition_split(struct mtd_info *master, struct mtd_part *part);
/*
* Given a pointer to the MTD object in the mtd_part structure, we can retrieve
-@@ -703,6 +707,36 @@ int mtd_del_partition(struct mtd_info *m
+@@ -704,6 +708,36 @@ int mtd_del_partition(struct mtd_info *m
}
EXPORT_SYMBOL_GPL(mtd_del_partition);
#ifdef CONFIG_MTD_SPLIT_FIRMWARE_NAME
#define SPLIT_FIRMWARE_NAME CONFIG_MTD_SPLIT_FIRMWARE_NAME
#else
-@@ -1052,6 +1086,61 @@ void mtd_part_parser_cleanup(struct mtd_
+@@ -1061,6 +1095,61 @@ void mtd_part_parser_cleanup(struct mtd_
}
}
--- a/drivers/mtd/mtdpart.c
+++ b/drivers/mtd/mtdpart.c
-@@ -745,6 +745,7 @@ run_parsers_by_type(struct mtd_part *sla
+@@ -746,6 +746,7 @@ run_parsers_by_type(struct mtd_part *sla
static void split_firmware(struct mtd_info *master, struct mtd_part *part)
{
}
static void mtd_partition_split(struct mtd_info *master, struct mtd_part *part)
-@@ -754,6 +755,12 @@ static void mtd_partition_split(struct m
+@@ -755,6 +756,12 @@ static void mtd_partition_split(struct m
if (rootfs_found)
return;
--- a/drivers/mtd/mtdpart.c
+++ b/drivers/mtd/mtdpart.c
-@@ -1165,6 +1165,24 @@ int mtd_is_partition(const struct mtd_in
+@@ -1174,6 +1174,24 @@ int mtd_is_partition(const struct mtd_in
}
EXPORT_SYMBOL_GPL(mtd_is_partition);
--- a/drivers/mtd/mtdpart.c
+++ b/drivers/mtd/mtdpart.c
-@@ -22,6 +22,8 @@
+@@ -23,6 +23,8 @@
#include "mtdcore.h"
#include "mtdsplit/mtdsplit.h"
/* Our partition linked list */
static LIST_HEAD(mtd_partitions);
static DEFINE_MUTEX(mtd_partitions_mutex);
-@@ -206,11 +208,77 @@ static int part_erase(struct mtd_info *m
+@@ -207,11 +209,77 @@ static int part_erase(struct mtd_info *m
{
struct mtd_part *part = mtd_to_part(mtd);
int ret;
instr->addr -= part->offset;
return ret;
-@@ -525,19 +593,22 @@ static struct mtd_part *allocate_partiti
+@@ -526,19 +594,22 @@ static struct mtd_part *allocate_partiti
remainder = do_div(tmp, wr_alignment);
if ((slave->mtd.flags & MTD_WRITEABLE) && remainder) {
/* Doesn't start on a boundary of major erase size */
--- a/drivers/mtd/mtdpart.c
+++ b/drivers/mtd/mtdpart.c
-@@ -293,7 +293,16 @@ static int part_lock(struct mtd_info *mt
+@@ -294,7 +294,16 @@ static int part_lock(struct mtd_info *mt
static int part_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
{
struct mtd_part *part = mtd_to_part(mtd);