[PATCH v3 4/4] mtd: physmap: Add support for RAM reserved memory regions

Gregory CLEMENT gregory.clement at bootlin.com
Wed Jan 7 05:04:55 PST 2026


MTD fixed-partitions can now be exposed for reserved RAM regions, not
just ROM reg regions. This is achieved by using the new compatible
property (mtd-mem or mtd-memro) previously introduced in the reserved
memory region node.

Based on the work of Muhammad Musa <muhammad.musa at intel.com>
Signed-off-by: Gregory CLEMENT <gregory.clement at bootlin.com>
---
 drivers/mtd/maps/physmap-core.c | 73 +++++++++++++++++++++++++++++++++--------
 drivers/of/platform.c           |  2 ++
 2 files changed, 62 insertions(+), 13 deletions(-)

diff --git a/drivers/mtd/maps/physmap-core.c b/drivers/mtd/maps/physmap-core.c
index 2bd7a1af898c9..cc7d08a413c78 100644
--- a/drivers/mtd/maps/physmap-core.c
+++ b/drivers/mtd/maps/physmap-core.c
@@ -39,6 +39,7 @@
 #include <linux/mtd/cfi_endian.h>
 #include <linux/io.h>
 #include <linux/of.h>
+#include <linux/of_reserved_mem.h>
 #include <linux/pm_runtime.h>
 #include <linux/gpio/consumer.h>
 
@@ -263,6 +264,14 @@ static const struct of_device_id of_flash_match[] = {
 		.type = "rom",
 		.compatible = "direct-mapped"
 	},
+	{
+		.compatible = "mtd-mem",
+		.data = "map_ram",
+	},
+	{
+		.compatible = "mtd-memro",
+		.data = "map_rom",
+	},
 	{ /* sentinel */ },
 };
 MODULE_DEVICE_TABLE(of, of_flash_match);
@@ -348,10 +357,19 @@ static int physmap_flash_of_init(struct platform_device *dev)
 
 	map_indirect = of_property_read_bool(dp, "no-unaligned-direct-access");
 
-	err = of_property_read_u32(dp, "bank-width", &bankwidth);
-	if (err) {
-		dev_err(&dev->dev, "Can't get bank width from device tree\n");
-		return err;
+	if ((of_device_is_compatible(dp, "mtd-mem")) ||
+	    (of_device_is_compatible(dp, "mtd-memro"))) {
+		/*
+		 * When using reserved memory region from DRAM we use
+		 * the defaullt 32 bits acces
+		 */
+		bankwidth = 4;
+	} else {
+		err = of_property_read_u32(dp, "bank-width", &bankwidth);
+		if (err) {
+			dev_err(&dev->dev, "Can't get bank width from device tree\n");
+			return err;
+		}
 	}
 
 	if (of_property_read_bool(dp, "big-endian"))
@@ -446,8 +464,9 @@ static int physmap_flash_pdata_init(struct platform_device *dev)
 static int physmap_flash_probe(struct platform_device *dev)
 {
 	struct physmap_flash_info *info;
-	int err = 0;
-	int i;
+	struct resource *res_array;
+	int err = 0, is_rsvd_mem = 0, nreg = 0;
+	int i, curr_reg;
 
 	if (!dev->dev.of_node && !dev_get_platdata(&dev->dev))
 		return -EINVAL;
@@ -459,9 +478,13 @@ static int physmap_flash_probe(struct platform_device *dev)
 	while (platform_get_resource(dev, IORESOURCE_MEM, info->nmaps))
 		info->nmaps++;
 
-	if (!info->nmaps)
-		return -ENODEV;
-
+	if (!info->nmaps) {
+		info->nmaps = of_reserved_mem_region_total_count(dev->dev.of_node);
+		if (info->nmaps > 0)
+			is_rsvd_mem = 1;
+		else
+			return -ENODEV;
+	}
 	info->maps = devm_kzalloc(&dev->dev,
 				  sizeof(*info->maps) * info->nmaps,
 				  GFP_KERNEL);
@@ -503,7 +526,23 @@ static int physmap_flash_probe(struct platform_device *dev)
 	for (i = 0; i < info->nmaps; i++) {
 		struct resource *res;
 
-		info->maps[i].virt = devm_platform_get_and_ioremap_resource(dev, i, &res);
+		if (is_rsvd_mem) {
+			if (nreg <= i) {
+				int cnt = of_reserved_mem_region_to_resource_array(&dev->dev,
+							   dev->dev.of_node, i, &res_array);
+				if (cnt < 0) {
+					err = cnt;
+					goto err_out;
+				}
+				nreg += cnt;
+				curr_reg = 0;
+			}
+			res = &res_array[curr_reg++];
+			info->maps[i].virt = devm_ioremap_resource(&dev->dev, res);
+		} else {
+			info->maps[i].virt = devm_platform_get_and_ioremap_resource(dev, i, &res);
+		}
+
 		if (IS_ERR(info->maps[i].virt)) {
 			err = PTR_ERR(info->maps[i].virt);
 			goto err_out;
@@ -519,9 +558,17 @@ static int physmap_flash_probe(struct platform_device *dev)
 			info->maps[i].phys = res->start;
 
 		info->win_order = fls64(resource_size(res)) - 1;
-		info->maps[i].size = BIT(info->win_order +
-					 (info->gpios ?
-					  info->gpios->ndescs : 0));
+		/* When using a memory region, the size is not necessarily a
+		 * power of 2, so win_order is not applicable. Since GPIOs are
+		 * unavailable in this context, directly using the region's size
+		 * is safe.
+		 */
+		if (is_rsvd_mem)
+			info->maps[i].size = resource_size(res);
+		else
+			info->maps[i].size = BIT(info->win_order +
+						 (info->gpios ?
+						  info->gpios->ndescs : 0));
 
 		info->maps[i].map_priv_1 = (unsigned long)dev;
 
diff --git a/drivers/of/platform.c b/drivers/of/platform.c
index f77cb19973a5d..23ea5a723aa7f 100644
--- a/drivers/of/platform.c
+++ b/drivers/of/platform.c
@@ -496,6 +496,8 @@ static const struct of_device_id reserved_mem_matches[] = {
 	{ .compatible = "ramoops" },
 	{ .compatible = "nvmem-rmem" },
 	{ .compatible = "google,open-dice" },
+	{ .compatible = "mtd-memro" },
+	{ .compatible = "mtd-mem" },
 	{}
 };
 

-- 
2.51.0




More information about the linux-mtd mailing list