Skip to content

Commit ad9b10d

Browse files
Ansuelmiquelraynal
authored andcommitted
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
1 parent dd63820 commit ad9b10d

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed

drivers/mtd/mtdcore.c

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -546,6 +546,66 @@ static int mtd_nvmem_add(struct mtd_info *mtd)
546546
return 0;
547547
}
548548

549+
static void mtd_check_of_node(struct mtd_info *mtd)
550+
{
551+
struct device_node *partitions, *parent_dn, *mtd_dn = NULL;
552+
const char *pname, *prefix = "partition-";
553+
int plen, mtd_name_len, offset, prefix_len;
554+
struct mtd_info *parent;
555+
bool found = false;
556+
557+
/* Check if MTD already has a device node */
558+
if (dev_of_node(&mtd->dev))
559+
return;
560+
561+
/* Check if a partitions node exist */
562+
parent = mtd->parent;
563+
parent_dn = dev_of_node(&parent->dev);
564+
if (!parent_dn)
565+
return;
566+
567+
partitions = of_get_child_by_name(parent_dn, "partitions");
568+
if (!partitions)
569+
goto exit_parent;
570+
571+
prefix_len = strlen(prefix);
572+
mtd_name_len = strlen(mtd->name);
573+
574+
/* Search if a partition is defined with the same name */
575+
for_each_child_of_node(partitions, mtd_dn) {
576+
offset = 0;
577+
578+
/* Skip partition with no/wrong prefix */
579+
if (!of_node_name_prefix(mtd_dn, "partition-"))
580+
continue;
581+
582+
/* Label have priority. Check that first */
583+
if (of_property_read_string(mtd_dn, "label", &pname)) {
584+
of_property_read_string(mtd_dn, "name", &pname);
585+
offset = prefix_len;
586+
}
587+
588+
plen = strlen(pname) - offset;
589+
if (plen == mtd_name_len &&
590+
!strncmp(mtd->name, pname + offset, plen)) {
591+
found = true;
592+
break;
593+
}
594+
}
595+
596+
if (!found)
597+
goto exit_partitions;
598+
599+
/* Set of_node only for nvmem */
600+
if (of_device_is_compatible(mtd_dn, "nvmem-cells"))
601+
mtd_set_of_node(mtd, mtd_dn);
602+
603+
exit_partitions:
604+
of_node_put(partitions);
605+
exit_parent:
606+
of_node_put(parent_dn);
607+
}
608+
549609
/**
550610
* add_mtd_device - register an MTD device
551611
* @mtd: pointer to new MTD device info structure
@@ -658,6 +718,7 @@ int add_mtd_device(struct mtd_info *mtd)
658718
mtd->dev.devt = MTD_DEVT(i);
659719
dev_set_name(&mtd->dev, "mtd%d", i);
660720
dev_set_drvdata(&mtd->dev, mtd);
721+
mtd_check_of_node(mtd);
661722
of_node_get(mtd_get_of_node(mtd));
662723
error = device_register(&mtd->dev);
663724
if (error)

0 commit comments

Comments
 (0)