generic: backport mtd dynamic partition patch
authorChristian Marangi <ansuelsmth@gmail.com>
Wed, 29 Jun 2022 15:34:51 +0000 (17:34 +0200)
committerChristian Marangi <ansuelsmth@gmail.com>
Wed, 29 Jun 2022 16:16:42 +0000 (18:16 +0200)
Backport upstream solution that permits to declare nvmem cells with
dynamic partition defined by special parser.

This provide an OF node for NVMEM and connect it to the defined dynamic
partition.

Drop the hack patch for cmdlinepart now that we have a proper solution
to the problem.

Signed-off-by: Christian Marangi <ansuelsmth@gmail.com>
target/linux/generic/backport-5.10/403-mtd-next-mtd-core-introduce-of-support-for-dynamic-partitions.patch [new file with mode: 0644]
target/linux/generic/backport-5.15/403-mtd-next-mtd-core-introduce-of-support-for-dynamic-partitions.patch [new file with mode: 0644]
target/linux/generic/hack-5.10/421-drivers-mtd-parsers-add-nvmem-support-to-cmdlinepart.patch [deleted file]
target/linux/generic/hack-5.15/421-drivers-mtd-parsers-add-nvmem-support-to-cmdlinepart.patch [deleted file]

diff --git a/target/linux/generic/backport-5.10/403-mtd-next-mtd-core-introduce-of-support-for-dynamic-partitions.patch b/target/linux/generic/backport-5.10/403-mtd-next-mtd-core-introduce-of-support-for-dynamic-partitions.patch
new file mode 100644 (file)
index 0000000..83b301c
--- /dev/null
@@ -0,0 +1,111 @@
+From ad9b10d1eaada169bd764abcab58f08538877e26 Mon Sep 17 00:00:00 2001
+From: Christian Marangi <ansuelsmth@gmail.com>
+Date: Wed, 22 Jun 2022 03:06:28 +0200
+Subject: mtd: core: introduce of support for dynamic partitions
+
+We have many parser that register mtd partitions at runtime. One example
+is the cmdlinepart or the smem-part parser where the compatible is defined
+in the dts and the partitions gets detected and registered by the
+parser. This is problematic for the NVMEM subsystem that requires an OF
+node to detect NVMEM cells.
+
+To fix this problem, introduce an additional logic that will try to
+assign an OF node to the MTD if declared.
+
+On MTD addition, it will be checked if the MTD has an OF node and if
+not declared will check if a partition with the same label / node name is
+declared in DTS. If an exact match is found, the partition dynamically
+allocated by the parser will have a connected OF node.
+
+The NVMEM subsystem will detect the OF node and register any NVMEM cells
+declared statically in the DTS.
+
+Signed-off-by: Christian Marangi <ansuelsmth@gmail.com>
+Signed-off-by: Miquel Raynal <miquel.raynal@bootlin.com>
+Link: https://lore.kernel.org/linux-mtd/20220622010628.30414-4-ansuelsmth@gmail.com
+---
+ drivers/mtd/mtdcore.c | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++
+ 1 file changed, 61 insertions(+)
+
+diff --git a/drivers/mtd/mtdcore.c b/drivers/mtd/mtdcore.c
+index 9eb0680db312f..6fafea80fd984 100644
+--- a/drivers/mtd/mtdcore.c
++++ b/drivers/mtd/mtdcore.c
+@@ -546,6 +546,66 @@ static int mtd_nvmem_add(struct mtd_info *mtd)
+       return 0;
+ }
++static void mtd_check_of_node(struct mtd_info *mtd)
++{
++      struct device_node *partitions, *parent_dn, *mtd_dn = NULL;
++      const char *pname, *prefix = "partition-";
++      int plen, mtd_name_len, offset, prefix_len;
++      struct mtd_info *parent;
++      bool found = false;
++
++      /* Check if MTD already has a device node */
++      if (dev_of_node(&mtd->dev))
++              return;
++
++      /* Check if a partitions node exist */
++      parent = mtd->parent;
++      parent_dn = dev_of_node(&parent->dev);
++      if (!parent_dn)
++              return;
++
++      partitions = of_get_child_by_name(parent_dn, "partitions");
++      if (!partitions)
++              goto exit_parent;
++
++      prefix_len = strlen(prefix);
++      mtd_name_len = strlen(mtd->name);
++
++      /* Search if a partition is defined with the same name */
++      for_each_child_of_node(partitions, mtd_dn) {
++              offset = 0;
++
++              /* Skip partition with no/wrong prefix */
++              if (!of_node_name_prefix(mtd_dn, "partition-"))
++                      continue;
++
++              /* Label have priority. Check that first */
++              if (of_property_read_string(mtd_dn, "label", &pname)) {
++                      of_property_read_string(mtd_dn, "name", &pname);
++                      offset = prefix_len;
++              }
++
++              plen = strlen(pname) - offset;
++              if (plen == mtd_name_len &&
++                  !strncmp(mtd->name, pname + offset, plen)) {
++                      found = true;
++                      break;
++              }
++      }
++
++      if (!found)
++              goto exit_partitions;
++
++      /* Set of_node only for nvmem */
++      if (of_device_is_compatible(mtd_dn, "nvmem-cells"))
++              mtd_set_of_node(mtd, mtd_dn);
++
++exit_partitions:
++      of_node_put(partitions);
++exit_parent:
++      of_node_put(parent_dn);
++}
++
+ /**
+  *    add_mtd_device - register an MTD device
+  *    @mtd: pointer to new MTD device info structure
+@@ -658,6 +718,7 @@ int add_mtd_device(struct mtd_info *mtd)
+       mtd->dev.devt = MTD_DEVT(i);
+       dev_set_name(&mtd->dev, "mtd%d", i);
+       dev_set_drvdata(&mtd->dev, mtd);
++      mtd_check_of_node(mtd);
+       of_node_get(mtd_get_of_node(mtd));
+       error = device_register(&mtd->dev);
+       if (error)
+-- 
+cgit 
+
diff --git a/target/linux/generic/backport-5.15/403-mtd-next-mtd-core-introduce-of-support-for-dynamic-partitions.patch b/target/linux/generic/backport-5.15/403-mtd-next-mtd-core-introduce-of-support-for-dynamic-partitions.patch
new file mode 100644 (file)
index 0000000..83b301c
--- /dev/null
@@ -0,0 +1,111 @@
+From ad9b10d1eaada169bd764abcab58f08538877e26 Mon Sep 17 00:00:00 2001
+From: Christian Marangi <ansuelsmth@gmail.com>
+Date: Wed, 22 Jun 2022 03:06:28 +0200
+Subject: mtd: core: introduce of support for dynamic partitions
+
+We have many parser that register mtd partitions at runtime. One example
+is the cmdlinepart or the smem-part parser where the compatible is defined
+in the dts and the partitions gets detected and registered by the
+parser. This is problematic for the NVMEM subsystem that requires an OF
+node to detect NVMEM cells.
+
+To fix this problem, introduce an additional logic that will try to
+assign an OF node to the MTD if declared.
+
+On MTD addition, it will be checked if the MTD has an OF node and if
+not declared will check if a partition with the same label / node name is
+declared in DTS. If an exact match is found, the partition dynamically
+allocated by the parser will have a connected OF node.
+
+The NVMEM subsystem will detect the OF node and register any NVMEM cells
+declared statically in the DTS.
+
+Signed-off-by: Christian Marangi <ansuelsmth@gmail.com>
+Signed-off-by: Miquel Raynal <miquel.raynal@bootlin.com>
+Link: https://lore.kernel.org/linux-mtd/20220622010628.30414-4-ansuelsmth@gmail.com
+---
+ drivers/mtd/mtdcore.c | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++
+ 1 file changed, 61 insertions(+)
+
+diff --git a/drivers/mtd/mtdcore.c b/drivers/mtd/mtdcore.c
+index 9eb0680db312f..6fafea80fd984 100644
+--- a/drivers/mtd/mtdcore.c
++++ b/drivers/mtd/mtdcore.c
+@@ -546,6 +546,66 @@ static int mtd_nvmem_add(struct mtd_info *mtd)
+       return 0;
+ }
++static void mtd_check_of_node(struct mtd_info *mtd)
++{
++      struct device_node *partitions, *parent_dn, *mtd_dn = NULL;
++      const char *pname, *prefix = "partition-";
++      int plen, mtd_name_len, offset, prefix_len;
++      struct mtd_info *parent;
++      bool found = false;
++
++      /* Check if MTD already has a device node */
++      if (dev_of_node(&mtd->dev))
++              return;
++
++      /* Check if a partitions node exist */
++      parent = mtd->parent;
++      parent_dn = dev_of_node(&parent->dev);
++      if (!parent_dn)
++              return;
++
++      partitions = of_get_child_by_name(parent_dn, "partitions");
++      if (!partitions)
++              goto exit_parent;
++
++      prefix_len = strlen(prefix);
++      mtd_name_len = strlen(mtd->name);
++
++      /* Search if a partition is defined with the same name */
++      for_each_child_of_node(partitions, mtd_dn) {
++              offset = 0;
++
++              /* Skip partition with no/wrong prefix */
++              if (!of_node_name_prefix(mtd_dn, "partition-"))
++                      continue;
++
++              /* Label have priority. Check that first */
++              if (of_property_read_string(mtd_dn, "label", &pname)) {
++                      of_property_read_string(mtd_dn, "name", &pname);
++                      offset = prefix_len;
++              }
++
++              plen = strlen(pname) - offset;
++              if (plen == mtd_name_len &&
++                  !strncmp(mtd->name, pname + offset, plen)) {
++                      found = true;
++                      break;
++              }
++      }
++
++      if (!found)
++              goto exit_partitions;
++
++      /* Set of_node only for nvmem */
++      if (of_device_is_compatible(mtd_dn, "nvmem-cells"))
++              mtd_set_of_node(mtd, mtd_dn);
++
++exit_partitions:
++      of_node_put(partitions);
++exit_parent:
++      of_node_put(parent_dn);
++}
++
+ /**
+  *    add_mtd_device - register an MTD device
+  *    @mtd: pointer to new MTD device info structure
+@@ -658,6 +718,7 @@ int add_mtd_device(struct mtd_info *mtd)
+       mtd->dev.devt = MTD_DEVT(i);
+       dev_set_name(&mtd->dev, "mtd%d", i);
+       dev_set_drvdata(&mtd->dev, mtd);
++      mtd_check_of_node(mtd);
+       of_node_get(mtd_get_of_node(mtd));
+       error = device_register(&mtd->dev);
+       if (error)
+-- 
+cgit 
+
diff --git a/target/linux/generic/hack-5.10/421-drivers-mtd-parsers-add-nvmem-support-to-cmdlinepart.patch b/target/linux/generic/hack-5.10/421-drivers-mtd-parsers-add-nvmem-support-to-cmdlinepart.patch
deleted file mode 100644 (file)
index 965a331..0000000
+++ /dev/null
@@ -1,120 +0,0 @@
-From 6fa9e3678eb002246df1280322b6a024853950a5 Mon Sep 17 00:00:00 2001
-From: Ansuel Smith <ansuelsmth@gmail.com>
-Date: Mon, 11 Oct 2021 00:53:14 +0200
-Subject: [PATCH] drivers: mtd: parsers: add nvmem support to cmdlinepart
-
-Assuming cmdlinepart is only one level deep partition scheme and that
-static partition are also defined in DTS, we can assign an of_node for
-partition declared from bootargs. cmdlinepart have priority than
-fiexed-partition parser so in this specific case the parser doesn't
-assign an of_node. Fix this by searching a defined of_node using a
-similar fixed_partition parser and if a partition is found with the same
-label, check that it has the same offset and size and return the DT
-of_node to correctly use NVMEM cells.
-
-Signed-off-by: Ansuel Smith <ansuelsmth@gmail.com>
----
- drivers/mtd/parsers/cmdlinepart.c | 71 +++++++++++++++++++++++++++++++
- 1 file changed, 71 insertions(+)
-
---- a/drivers/mtd/parsers/cmdlinepart.c
-+++ b/drivers/mtd/parsers/cmdlinepart.c
-@@ -43,6 +43,7 @@
- #include <linux/mtd/partitions.h>
- #include <linux/module.h>
- #include <linux/err.h>
-+#include <linux/of.h>
- /* debug macro */
- #if 0
-@@ -323,6 +324,68 @@ static int mtdpart_setup_real(char *s)
-       return 0;
- }
-+static int search_fixed_partition(struct mtd_info *master,
-+                                struct mtd_partition *target_part,
-+                                struct mtd_partition *fixed_part)
-+{
-+      struct device_node *mtd_node;
-+      struct device_node *ofpart_node;
-+      struct device_node *pp;
-+      struct mtd_partition part;
-+      const char *partname;
-+
-+      mtd_node = mtd_get_of_node(master);
-+      if (!mtd_node)
-+              return -EINVAL;
-+
-+      ofpart_node = of_get_child_by_name(mtd_node, "partitions");
-+
-+      for_each_child_of_node(ofpart_node,  pp) {
-+              const __be32 *reg;
-+              int len;
-+              int a_cells, s_cells;
-+
-+              reg = of_get_property(pp, "reg", &len);
-+              if (!reg) {
-+                      pr_debug("%s: ofpart partition %pOF (%pOF) missing reg property.\n",
-+                               master->name, pp,
-+                               mtd_node);
-+                      continue;
-+              }
-+
-+              a_cells = of_n_addr_cells(pp);
-+              s_cells = of_n_size_cells(pp);
-+              if (len / 4 != a_cells + s_cells) {
-+                      pr_debug("%s: ofpart partition %pOF (%pOF) error parsing reg property.\n",
-+                               master->name, pp,
-+                               mtd_node);
-+                      continue;
-+              }
-+
-+              part.offset = of_read_number(reg, a_cells);
-+              part.size = of_read_number(reg + a_cells, s_cells);
-+              part.of_node = pp;
-+
-+              partname = of_get_property(pp, "label", &len);
-+              if (!partname)
-+                      partname = of_get_property(pp, "name", &len);
-+              part.name = partname;
-+
-+              if (!strncmp(target_part->name, part.name, len)) {
-+                      if (part.offset != target_part->offset)
-+                              return -EINVAL;
-+
-+                      if (part.size != target_part->size)
-+                              return -EINVAL;
-+
-+                      memcpy(fixed_part, &part, sizeof(struct mtd_partition));
-+                      return 0;
-+              }
-+      }
-+
-+      return -EINVAL;
-+}
-+
- /*
-  * Main function to be called from the MTD mapping driver/device to
-  * obtain the partitioning information. At this point the command line
-@@ -338,6 +401,7 @@ static int parse_cmdline_partitions(stru
-       int i, err;
-       struct cmdline_mtd_partition *part;
-       const char *mtd_id = master->name;
-+      struct mtd_partition fixed_part;
-       /* parse command line */
-       if (!cmdline_parsed) {
-@@ -382,6 +446,13 @@ static int parse_cmdline_partitions(stru
-                               sizeof(*part->parts) * (part->num_parts - i));
-                       i--;
-               }
-+
-+              err = search_fixed_partition(master, &part->parts[i], &fixed_part);
-+              if (!err) {
-+                      part->parts[i].of_node = fixed_part.of_node;
-+                      pr_info("Found partition defined in DT for %s. Assigning OF node to support nvmem.",
-+                              part->parts[i].name);
-+              }
-       }
-       *pparts = kmemdup(part->parts, sizeof(*part->parts) * part->num_parts,
diff --git a/target/linux/generic/hack-5.15/421-drivers-mtd-parsers-add-nvmem-support-to-cmdlinepart.patch b/target/linux/generic/hack-5.15/421-drivers-mtd-parsers-add-nvmem-support-to-cmdlinepart.patch
deleted file mode 100644 (file)
index 965a331..0000000
+++ /dev/null
@@ -1,120 +0,0 @@
-From 6fa9e3678eb002246df1280322b6a024853950a5 Mon Sep 17 00:00:00 2001
-From: Ansuel Smith <ansuelsmth@gmail.com>
-Date: Mon, 11 Oct 2021 00:53:14 +0200
-Subject: [PATCH] drivers: mtd: parsers: add nvmem support to cmdlinepart
-
-Assuming cmdlinepart is only one level deep partition scheme and that
-static partition are also defined in DTS, we can assign an of_node for
-partition declared from bootargs. cmdlinepart have priority than
-fiexed-partition parser so in this specific case the parser doesn't
-assign an of_node. Fix this by searching a defined of_node using a
-similar fixed_partition parser and if a partition is found with the same
-label, check that it has the same offset and size and return the DT
-of_node to correctly use NVMEM cells.
-
-Signed-off-by: Ansuel Smith <ansuelsmth@gmail.com>
----
- drivers/mtd/parsers/cmdlinepart.c | 71 +++++++++++++++++++++++++++++++
- 1 file changed, 71 insertions(+)
-
---- a/drivers/mtd/parsers/cmdlinepart.c
-+++ b/drivers/mtd/parsers/cmdlinepart.c
-@@ -43,6 +43,7 @@
- #include <linux/mtd/partitions.h>
- #include <linux/module.h>
- #include <linux/err.h>
-+#include <linux/of.h>
- /* debug macro */
- #if 0
-@@ -323,6 +324,68 @@ static int mtdpart_setup_real(char *s)
-       return 0;
- }
-+static int search_fixed_partition(struct mtd_info *master,
-+                                struct mtd_partition *target_part,
-+                                struct mtd_partition *fixed_part)
-+{
-+      struct device_node *mtd_node;
-+      struct device_node *ofpart_node;
-+      struct device_node *pp;
-+      struct mtd_partition part;
-+      const char *partname;
-+
-+      mtd_node = mtd_get_of_node(master);
-+      if (!mtd_node)
-+              return -EINVAL;
-+
-+      ofpart_node = of_get_child_by_name(mtd_node, "partitions");
-+
-+      for_each_child_of_node(ofpart_node,  pp) {
-+              const __be32 *reg;
-+              int len;
-+              int a_cells, s_cells;
-+
-+              reg = of_get_property(pp, "reg", &len);
-+              if (!reg) {
-+                      pr_debug("%s: ofpart partition %pOF (%pOF) missing reg property.\n",
-+                               master->name, pp,
-+                               mtd_node);
-+                      continue;
-+              }
-+
-+              a_cells = of_n_addr_cells(pp);
-+              s_cells = of_n_size_cells(pp);
-+              if (len / 4 != a_cells + s_cells) {
-+                      pr_debug("%s: ofpart partition %pOF (%pOF) error parsing reg property.\n",
-+                               master->name, pp,
-+                               mtd_node);
-+                      continue;
-+              }
-+
-+              part.offset = of_read_number(reg, a_cells);
-+              part.size = of_read_number(reg + a_cells, s_cells);
-+              part.of_node = pp;
-+
-+              partname = of_get_property(pp, "label", &len);
-+              if (!partname)
-+                      partname = of_get_property(pp, "name", &len);
-+              part.name = partname;
-+
-+              if (!strncmp(target_part->name, part.name, len)) {
-+                      if (part.offset != target_part->offset)
-+                              return -EINVAL;
-+
-+                      if (part.size != target_part->size)
-+                              return -EINVAL;
-+
-+                      memcpy(fixed_part, &part, sizeof(struct mtd_partition));
-+                      return 0;
-+              }
-+      }
-+
-+      return -EINVAL;
-+}
-+
- /*
-  * Main function to be called from the MTD mapping driver/device to
-  * obtain the partitioning information. At this point the command line
-@@ -338,6 +401,7 @@ static int parse_cmdline_partitions(stru
-       int i, err;
-       struct cmdline_mtd_partition *part;
-       const char *mtd_id = master->name;
-+      struct mtd_partition fixed_part;
-       /* parse command line */
-       if (!cmdline_parsed) {
-@@ -382,6 +446,13 @@ static int parse_cmdline_partitions(stru
-                               sizeof(*part->parts) * (part->num_parts - i));
-                       i--;
-               }
-+
-+              err = search_fixed_partition(master, &part->parts[i], &fixed_part);
-+              if (!err) {
-+                      part->parts[i].of_node = fixed_part.of_node;
-+                      pr_info("Found partition defined in DT for %s. Assigning OF node to support nvmem.",
-+                              part->parts[i].name);
-+              }
-       }
-       *pparts = kmemdup(part->parts, sizeof(*part->parts) * part->num_parts,