e71098d56334bb1a6cae382c6e37acd0a12a2c7b
[openwrt/staging/neocturne.git] /
1 From ad9b10d1eaada169bd764abcab58f08538877e26 Mon Sep 17 00:00:00 2001
2 From: Christian Marangi <ansuelsmth@gmail.com>
3 Date: Wed, 22 Jun 2022 03:06:28 +0200
4 Subject: mtd: core: introduce of support for dynamic partitions
5
6 We have many parser that register mtd partitions at runtime. One example
7 is the cmdlinepart or the smem-part parser where the compatible is defined
8 in the dts and the partitions gets detected and registered by the
9 parser. This is problematic for the NVMEM subsystem that requires an OF
10 node to detect NVMEM cells.
11
12 To fix this problem, introduce an additional logic that will try to
13 assign an OF node to the MTD if declared.
14
15 On MTD addition, it will be checked if the MTD has an OF node and if
16 not declared will check if a partition with the same label / node name is
17 declared in DTS. If an exact match is found, the partition dynamically
18 allocated by the parser will have a connected OF node.
19
20 The NVMEM subsystem will detect the OF node and register any NVMEM cells
21 declared statically in the DTS.
22
23 Signed-off-by: Christian Marangi <ansuelsmth@gmail.com>
24 Signed-off-by: Miquel Raynal <miquel.raynal@bootlin.com>
25 Link: https://lore.kernel.org/linux-mtd/20220622010628.30414-4-ansuelsmth@gmail.com
26 ---
27 drivers/mtd/mtdcore.c | 61 +++++++++++++++++++++++++++++++++++++++++++
28 1 file changed, 61 insertions(+)
29
30 --- a/drivers/mtd/mtdcore.c
31 +++ b/drivers/mtd/mtdcore.c
32 @@ -561,6 +561,66 @@ static int mtd_nvmem_add(struct mtd_info
33 return 0;
34 }
35
36 +static void mtd_check_of_node(struct mtd_info *mtd)
37 +{
38 + struct device_node *partitions, *parent_dn, *mtd_dn = NULL;
39 + const char *pname, *prefix = "partition-";
40 + int plen, mtd_name_len, offset, prefix_len;
41 + struct mtd_info *parent;
42 + bool found = false;
43 +
44 + /* Check if MTD already has a device node */
45 + if (dev_of_node(&mtd->dev))
46 + return;
47 +
48 + /* Check if a partitions node exist */
49 + parent = mtd->parent;
50 + parent_dn = dev_of_node(&parent->dev);
51 + if (!parent_dn)
52 + return;
53 +
54 + partitions = of_get_child_by_name(parent_dn, "partitions");
55 + if (!partitions)
56 + goto exit_parent;
57 +
58 + prefix_len = strlen(prefix);
59 + mtd_name_len = strlen(mtd->name);
60 +
61 + /* Search if a partition is defined with the same name */
62 + for_each_child_of_node(partitions, mtd_dn) {
63 + offset = 0;
64 +
65 + /* Skip partition with no/wrong prefix */
66 + if (!of_node_name_prefix(mtd_dn, "partition-"))
67 + continue;
68 +
69 + /* Label have priority. Check that first */
70 + if (of_property_read_string(mtd_dn, "label", &pname)) {
71 + of_property_read_string(mtd_dn, "name", &pname);
72 + offset = prefix_len;
73 + }
74 +
75 + plen = strlen(pname) - offset;
76 + if (plen == mtd_name_len &&
77 + !strncmp(mtd->name, pname + offset, plen)) {
78 + found = true;
79 + break;
80 + }
81 + }
82 +
83 + if (!found)
84 + goto exit_partitions;
85 +
86 + /* Set of_node only for nvmem */
87 + if (of_device_is_compatible(mtd_dn, "nvmem-cells"))
88 + mtd_set_of_node(mtd, mtd_dn);
89 +
90 +exit_partitions:
91 + of_node_put(partitions);
92 +exit_parent:
93 + of_node_put(parent_dn);
94 +}
95 +
96 /**
97 * add_mtd_device - register an MTD device
98 * @mtd: pointer to new MTD device info structure
99 @@ -666,6 +726,7 @@ int add_mtd_device(struct mtd_info *mtd)
100 mtd->dev.devt = MTD_DEVT(i);
101 dev_set_name(&mtd->dev, "mtd%d", i);
102 dev_set_drvdata(&mtd->dev, mtd);
103 + mtd_check_of_node(mtd);
104 of_node_get(mtd_get_of_node(mtd));
105 error = device_register(&mtd->dev);
106 if (error) {