From 1259dde287048818686b78a7801288828b789847 Mon Sep 17 00:00:00 2001 From: Heinrich Toews Date: Tue, 7 Apr 2026 16:35:22 +0200 Subject: [PATCH] mtd: mtdchar: enable mmap() for MTD_RAM devices with a physical map The mmap() path in mtdchar has been intentionally dead since commit 9fdca4d ('mtd: kill dead code in mtdchar_mmap') with the comment 'This is broken because it assumes the MTD device is map-based'. Re-enable mmap() for MTD_RAM devices that explicitly set mtd->priv to a valid struct map_info with a known physical address (map->phys != NO_XIP). The NULL and NO_XIP guards prevent the broken assumptions that caused the original removal from applying to any other device. All other MTD types continue to return -ENODEV as before. This is a prerequisite for Everspin MRAM on Cadence OSPI (DAC mode) which exposes the memory as a directly CPU-addressable AHB window. Signed-off-by: Heinrich Toews --- drivers/mtd/mtdchar.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/drivers/mtd/mtdchar.c b/drivers/mtd/mtdchar.c index 8dc4f5c493fc..b4625cecc151 100644 --- a/drivers/mtd/mtdchar.c +++ b/drivers/mtd/mtdchar.c @@ -1381,11 +1381,12 @@ static int mtdchar_mmap(struct file *file, struct vm_area_struct *vma) struct mtd_info *mtd = mfi->mtd; struct map_info *map = mtd->priv; - /* This is broken because it assumes the MTD device is map-based - and that mtd->priv is a valid struct map_info. It should be - replaced with something that uses the mtd_get_unmapped_area() - operation properly. */ - if (0 /*mtd->type == MTD_RAM || mtd->type == MTD_ROM*/) { + /* + * Only MTD_RAM devices that have a valid map_info with a physical + * address (e.g. MRAM on a Cadence OSPI DAC window) support mmap(). + * All other MTD types return -ENODEV as before. + */ + if (mtd->type == MTD_RAM && map && map->phys != NO_XIP) { #ifdef pgprot_noncached if (file->f_flags & O_DSYNC || map->phys >= __pa(high_memory)) vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);