linux-mtd.lists.infradead.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v6 0/3] Add nvmem support for dynamic partitions
@ 2022-06-22  1:06 Christian Marangi
  2022-06-22  1:06 ` [PATCH v6 1/3] dt-bindings: mtd: partitions: support label/name only partition Christian Marangi
                   ` (2 more replies)
  0 siblings, 3 replies; 8+ messages in thread
From: Christian Marangi @ 2022-06-22  1:06 UTC (permalink / raw)
  To: Miquel Raynal, Richard Weinberger, Vignesh Raghavendra,
	Rob Herring, Krzysztof Kozlowski, Jens Axboe, Stephen Boyd,
	Greg Kroah-Hartman, Christian Marangi, �ecki,
	Manivannan Sadhasivam, linux-mtd, devicetree, linux-kernel

This very small series comes to fix the very annyoing problem of
partitions declared by parser at runtime NOT supporting nvmem cells
definition.

The current implementation is very generic. The idea is to provide an of
node if defined for everyone and not strictly limit this to nvmem stuff.
But still the actual change is done only for nvmem-cells mtd. (just to
make sure) This can totally change by removing the compatible check.

The idea here is that a user can still use these dynamic parsers
instead of declaring a fixed-partition and also declare how nvmem-cells
are defined for the partition.
This live with the assumption that dynamic partition have always the
same name and they are known. (this is the case for smem-part partition
that would require a bootloader reflash to change and for parsers like
cmdlinepart where the name is always the same.)
With this assumption, it's easy to fix this problem. Just introduce a
new partition node that will declare just these special partition.
Mtdcore then will check if these special declaration are present and
connect the dynamic partition with the OF node present in the dts. Nvmem
will automagically find the OF node and cells will be works based on the
data provided by the parser.

The initial idea was to create a special nvmem driver with a special
compatible where a user would declare the mtd partition name and this
driver would search it and register the nvmem cells but that became
difficult really fast, mtd notifier system is problematic for this kind
of stuff. So here is the better implementation. A variant of this is
already tested on openwrt where we have devices that use cmdlinepart.
(that current variant have defined in the dts the exact copy of
cmdlinepart in the fixed-partition scheme and we patched the cmdlinepart
parser to scan this fixed-partition node (that is ignored as cmdlinepart
have priority) and connect the dynamic partition with the dts node)

I provided an example of this in the documentation commit.
In short it's needed to add to the partitions where the compatible parser
is declared, a partition with just the label declared (instead of the reg).
Then declare some nvmem-cells and it will all work at runtime.
Mtdcore will check if a node with the same label is present and assign an
OF node to the MTD.

I currently tested this on my device that have smem-part and the
gmac driver use nvmem to get the mac-address. This works correctly and
the same address is provided.

v6:
- Fix Sob tag
- Add review tag by Rob
- Reintroduce name support for partition
- Make the "partition-" prefix mandatory
v5:
- Simplify Documentation and move it generic partition.yaml
- Split commit and move smem example to dedicated commit.
v4:
- Make it simple. No suffix. Make label mandatory again.
- Update Documentation with new implementation.
- Rename files to a better and correct name
v3:
- Fix warning from bot (function not declared as static)
- Updated code to support also node name
- Made partition label optional
v2:
- Simplify this. Drop dynamic-partition
- Fix problem with parser with ko
- Do not pollude mtd_get_of_node
- Fix problem with Documentation

Christian Marangi (3):
  dt-bindings: mtd: partitions: support label/name only partition
  dt-bindings: mtd: partitions: add additional example for
    qcom,smem-part
  mtd: core: introduce of support for dynamic partitions

 .../bindings/mtd/partitions/partition.yaml    | 20 +++++-
 .../mtd/partitions/qcom,smem-part.yaml        | 27 ++++++++
 drivers/mtd/mtdcore.c                         | 61 +++++++++++++++++++
 3 files changed, 106 insertions(+), 2 deletions(-)

-- 
2.36.1


______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [PATCH v6 1/3] dt-bindings: mtd: partitions: support label/name only partition
  2022-06-22  1:06 [PATCH v6 0/3] Add nvmem support for dynamic partitions Christian Marangi
@ 2022-06-22  1:06 ` Christian Marangi
  2022-06-28 19:44   ` Rob Herring
  2022-06-29 11:38   ` Miquel Raynal
  2022-06-22  1:06 ` [PATCH v6 2/3] dt-bindings: mtd: partitions: add additional example for qcom,smem-part Christian Marangi
  2022-06-22  1:06 ` [PATCH v6 3/3] mtd: core: introduce of support for dynamic partitions Christian Marangi
  2 siblings, 2 replies; 8+ messages in thread
From: Christian Marangi @ 2022-06-22  1:06 UTC (permalink / raw)
  To: Miquel Raynal, Richard Weinberger, Vignesh Raghavendra,
	Rob Herring, Krzysztof Kozlowski, Jens Axboe, Stephen Boyd,
	Greg Kroah-Hartman, Christian Marangi, �ecki,
	Manivannan Sadhasivam, linux-mtd, devicetree, linux-kernel

Document new partition nodes that declare only the label/name instead
of the reg used to provide an OF node for partition registred at runtime
by parsers. This is required for nvmem system to declare and detect
nvmem-cells.

With these special partitions, the reg / offset is not required and a
'partition-' prefix is needed.
The node name with the 'partition-' prefix stripped, is used to match
the partition allocated by the parser at runtime and the parser will
provide reg and offset of the mtd.
If the partition to match contains invalid char for a node name, the
label binding can be used to declare the partition name.

NVMEM will use the data from the parser and provide the NVMEM cells
declared in the DTS, "connecting" the dynamic partition with a
static declaration of cells in them.

Signed-off-by: Christian Marangi <ansuelsmth@gmail.com>
---
 .../bindings/mtd/partitions/partition.yaml    | 20 +++++++++++++++++--
 1 file changed, 18 insertions(+), 2 deletions(-)

diff --git a/Documentation/devicetree/bindings/mtd/partitions/partition.yaml b/Documentation/devicetree/bindings/mtd/partitions/partition.yaml
index e1ac08064425..f1a02d840b12 100644
--- a/Documentation/devicetree/bindings/mtd/partitions/partition.yaml
+++ b/Documentation/devicetree/bindings/mtd/partitions/partition.yaml
@@ -11,6 +11,17 @@ description: |
   relative offset and size specified. Depending on partition function extra
   properties can be used.
 
+  A partition may be dynamically allocated by a specific parser at runtime.
+  In this specific case, a specific suffix is required to the node name.
+  Everything after 'partition-' will be used as the partition name to compare
+  with the one dynamically allocated by the specific parser.
+  If the partition contains invalid char a label can be provided that will
+  be used instead of the node name to make the comparison.
+  This is used to assign an OF node to the dynamiccally allocated partition
+  so that subsystem like NVMEM can provide an OF node and declare NVMEM cells.
+  The OF node will be assigned only if the partition label declared match the
+  one assigned by the parser at runtime.
+
 maintainers:
   - Rafał Miłecki <rafal@milecki.pl>
 
@@ -41,7 +52,12 @@ properties:
       immune to paired-pages corruptions
     type: boolean
 
-required:
-  - reg
+if:
+  not:
+    required: [ reg ]
+then:
+  properties:
+    $nodename:
+      pattern: '^partition-.*$'
 
 additionalProperties: true
-- 
2.36.1


______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/

^ permalink raw reply related	[flat|nested] 8+ messages in thread

* [PATCH v6 2/3] dt-bindings: mtd: partitions: add additional example for qcom,smem-part
  2022-06-22  1:06 [PATCH v6 0/3] Add nvmem support for dynamic partitions Christian Marangi
  2022-06-22  1:06 ` [PATCH v6 1/3] dt-bindings: mtd: partitions: support label/name only partition Christian Marangi
@ 2022-06-22  1:06 ` Christian Marangi
  2022-06-29 11:38   ` Miquel Raynal
  2022-06-22  1:06 ` [PATCH v6 3/3] mtd: core: introduce of support for dynamic partitions Christian Marangi
  2 siblings, 1 reply; 8+ messages in thread
From: Christian Marangi @ 2022-06-22  1:06 UTC (permalink / raw)
  To: Miquel Raynal, Richard Weinberger, Vignesh Raghavendra,
	Rob Herring, Krzysztof Kozlowski, Jens Axboe, Stephen Boyd,
	Greg Kroah-Hartman, Christian Marangi, �ecki,
	Manivannan Sadhasivam, linux-mtd, devicetree, linux-kernel
  Cc: Rob Herring

Add additional example for qcom,smem-part to declare a dynamic
partition to provide NVMEM cells for the commonly ART partition
provided by this parser.

Signed-off-by: Christian Marangi <ansuelsmth@gmail.com>
Reviewed-by: Rob Herring <robh@kernel.org>
---
 .../mtd/partitions/qcom,smem-part.yaml        | 27 +++++++++++++++++++
 1 file changed, 27 insertions(+)

diff --git a/Documentation/devicetree/bindings/mtd/partitions/qcom,smem-part.yaml b/Documentation/devicetree/bindings/mtd/partitions/qcom,smem-part.yaml
index cf3f8c1e035d..dc07909af023 100644
--- a/Documentation/devicetree/bindings/mtd/partitions/qcom,smem-part.yaml
+++ b/Documentation/devicetree/bindings/mtd/partitions/qcom,smem-part.yaml
@@ -19,6 +19,10 @@ properties:
   compatible:
     const: qcom,smem-part
 
+patternProperties:
+  "^partition-[0-9a-z]+$":
+    $ref: partition.yaml#
+
 required:
   - compatible
 
@@ -31,3 +35,26 @@ examples:
             compatible = "qcom,smem-part";
         };
     };
+
+  - |
+    /* Example declaring dynamic partition */
+    flash {
+      partitions {
+        compatible = "qcom,smem-part";
+
+        partition-art {
+          compatible = "nvmem-cells";
+          #address-cells = <1>;
+          #size-cells = <1>;
+          label = "0:art";
+
+          macaddr_art_0: macaddr@0 {
+            reg = <0x0 0x6>;
+          };
+
+          macaddr_art_6: macaddr@6 {
+            reg = <0x6 0x6>;
+          };
+        };
+      };
+    };
-- 
2.36.1


______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/

^ permalink raw reply related	[flat|nested] 8+ messages in thread

* [PATCH v6 3/3] mtd: core: introduce of support for dynamic partitions
  2022-06-22  1:06 [PATCH v6 0/3] Add nvmem support for dynamic partitions Christian Marangi
  2022-06-22  1:06 ` [PATCH v6 1/3] dt-bindings: mtd: partitions: support label/name only partition Christian Marangi
  2022-06-22  1:06 ` [PATCH v6 2/3] dt-bindings: mtd: partitions: add additional example for qcom,smem-part Christian Marangi
@ 2022-06-22  1:06 ` Christian Marangi
  2022-06-29 11:38   ` Miquel Raynal
  2 siblings, 1 reply; 8+ messages in thread
From: Christian Marangi @ 2022-06-22  1:06 UTC (permalink / raw)
  To: Miquel Raynal, Richard Weinberger, Vignesh Raghavendra,
	Rob Herring, Krzysztof Kozlowski, Jens Axboe, Stephen Boyd,
	Greg Kroah-Hartman, Christian Marangi, �ecki,
	Manivannan Sadhasivam, linux-mtd, devicetree, linux-kernel

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>
---
 drivers/mtd/mtdcore.c | 61 +++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 61 insertions(+)

diff --git a/drivers/mtd/mtdcore.c b/drivers/mtd/mtdcore.c
index 7731796024e0..e9d534b9971d 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
@@ -651,6 +711,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)
-- 
2.36.1


______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/

^ permalink raw reply related	[flat|nested] 8+ messages in thread

* Re: [PATCH v6 1/3] dt-bindings: mtd: partitions: support label/name only partition
  2022-06-22  1:06 ` [PATCH v6 1/3] dt-bindings: mtd: partitions: support label/name only partition Christian Marangi
@ 2022-06-28 19:44   ` Rob Herring
  2022-06-29 11:38   ` Miquel Raynal
  1 sibling, 0 replies; 8+ messages in thread
From: Rob Herring @ 2022-06-28 19:44 UTC (permalink / raw)
  To: Christian Marangi
  Cc: �ecki, Richard Weinberger, linux-mtd, Greg Kroah-Hartman,
	Krzysztof Kozlowski, Rob Herring, Manivannan Sadhasivam,
	devicetree, Jens Axboe, Miquel Raynal, Stephen Boyd,
	linux-kernel, Vignesh Raghavendra

On Wed, 22 Jun 2022 03:06:26 +0200, Christian Marangi wrote:
> Document new partition nodes that declare only the label/name instead
> of the reg used to provide an OF node for partition registred at runtime
> by parsers. This is required for nvmem system to declare and detect
> nvmem-cells.
> 
> With these special partitions, the reg / offset is not required and a
> 'partition-' prefix is needed.
> The node name with the 'partition-' prefix stripped, is used to match
> the partition allocated by the parser at runtime and the parser will
> provide reg and offset of the mtd.
> If the partition to match contains invalid char for a node name, the
> label binding can be used to declare the partition name.
> 
> NVMEM will use the data from the parser and provide the NVMEM cells
> declared in the DTS, "connecting" the dynamic partition with a
> static declaration of cells in them.
> 
> Signed-off-by: Christian Marangi <ansuelsmth@gmail.com>
> ---
>  .../bindings/mtd/partitions/partition.yaml    | 20 +++++++++++++++++--
>  1 file changed, 18 insertions(+), 2 deletions(-)
> 

Reviewed-by: Rob Herring <robh@kernel.org>

______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH v6 3/3] mtd: core: introduce of support for dynamic partitions
  2022-06-22  1:06 ` [PATCH v6 3/3] mtd: core: introduce of support for dynamic partitions Christian Marangi
@ 2022-06-29 11:38   ` Miquel Raynal
  0 siblings, 0 replies; 8+ messages in thread
From: Miquel Raynal @ 2022-06-29 11:38 UTC (permalink / raw)
  To: Christian Marangi, Miquel Raynal, Richard Weinberger,
	Vignesh Raghavendra, Rob Herring, Krzysztof Kozlowski,
	Jens Axboe, Stephen Boyd, Greg Kroah-Hartman, �ecki,
	Manivannan Sadhasivam, linux-mtd, devicetree, linux-kernel

On Wed, 2022-06-22 at 01:06:28 UTC, Christian Marangi wrote:
> 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>

Applied to https://git.kernel.org/pub/scm/linux/kernel/git/mtd/linux.git mtd/next, thanks.

Miquel

______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH v6 2/3] dt-bindings: mtd: partitions: add additional example for qcom,smem-part
  2022-06-22  1:06 ` [PATCH v6 2/3] dt-bindings: mtd: partitions: add additional example for qcom,smem-part Christian Marangi
@ 2022-06-29 11:38   ` Miquel Raynal
  0 siblings, 0 replies; 8+ messages in thread
From: Miquel Raynal @ 2022-06-29 11:38 UTC (permalink / raw)
  To: Christian Marangi, Miquel Raynal, Richard Weinberger,
	Vignesh Raghavendra, Rob Herring, Krzysztof Kozlowski,
	Jens Axboe, Stephen Boyd, Greg Kroah-Hartman, �ecki,
	Manivannan Sadhasivam, linux-mtd, devicetree, linux-kernel
  Cc: Rob Herring

On Wed, 2022-06-22 at 01:06:27 UTC, Christian Marangi wrote:
> Add additional example for qcom,smem-part to declare a dynamic
> partition to provide NVMEM cells for the commonly ART partition
> provided by this parser.
> 
> Signed-off-by: Christian Marangi <ansuelsmth@gmail.com>
> Reviewed-by: Rob Herring <robh@kernel.org>

Applied to https://git.kernel.org/pub/scm/linux/kernel/git/mtd/linux.git mtd/next, thanks.

Miquel

______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH v6 1/3] dt-bindings: mtd: partitions: support label/name only partition
  2022-06-22  1:06 ` [PATCH v6 1/3] dt-bindings: mtd: partitions: support label/name only partition Christian Marangi
  2022-06-28 19:44   ` Rob Herring
@ 2022-06-29 11:38   ` Miquel Raynal
  1 sibling, 0 replies; 8+ messages in thread
From: Miquel Raynal @ 2022-06-29 11:38 UTC (permalink / raw)
  To: Christian Marangi, Miquel Raynal, Richard Weinberger,
	Vignesh Raghavendra, Rob Herring, Krzysztof Kozlowski,
	Jens Axboe, Stephen Boyd, Greg Kroah-Hartman, �ecki,
	Manivannan Sadhasivam, linux-mtd, devicetree, linux-kernel

On Wed, 2022-06-22 at 01:06:26 UTC, Christian Marangi wrote:
> Document new partition nodes that declare only the label/name instead
> of the reg used to provide an OF node for partition registred at runtime
> by parsers. This is required for nvmem system to declare and detect
> nvmem-cells.
> 
> With these special partitions, the reg / offset is not required and a
> 'partition-' prefix is needed.
> The node name with the 'partition-' prefix stripped, is used to match
> the partition allocated by the parser at runtime and the parser will
> provide reg and offset of the mtd.
> If the partition to match contains invalid char for a node name, the
> label binding can be used to declare the partition name.
> 
> NVMEM will use the data from the parser and provide the NVMEM cells
> declared in the DTS, "connecting" the dynamic partition with a
> static declaration of cells in them.
> 
> Signed-off-by: Christian Marangi <ansuelsmth@gmail.com>
> Reviewed-by: Rob Herring <robh@kernel.org>

Applied to https://git.kernel.org/pub/scm/linux/kernel/git/mtd/linux.git mtd/next, thanks.

Miquel

______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2022-06-29 11:40 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-06-22  1:06 [PATCH v6 0/3] Add nvmem support for dynamic partitions Christian Marangi
2022-06-22  1:06 ` [PATCH v6 1/3] dt-bindings: mtd: partitions: support label/name only partition Christian Marangi
2022-06-28 19:44   ` Rob Herring
2022-06-29 11:38   ` Miquel Raynal
2022-06-22  1:06 ` [PATCH v6 2/3] dt-bindings: mtd: partitions: add additional example for qcom,smem-part Christian Marangi
2022-06-29 11:38   ` Miquel Raynal
2022-06-22  1:06 ` [PATCH v6 3/3] mtd: core: introduce of support for dynamic partitions Christian Marangi
2022-06-29 11:38   ` Miquel Raynal

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).