rp23xx: Add an MTD driver over the unused QSPI flash.

The rp2350 executes in place from external QSPI flash, and a NuttX image
normally leaves most of that flash unused.  This exposes the unused region
as an MTD device so it can carry a filesystem, mirroring what the rp2040
port already provides with rp2040_flash_mtd.c.

The region is given by RP23XX_FLASH_MTD_OFFSET and RP23XX_FLASH_MTD_SIZE,
both multiples of the 4096 byte erase sector.  Initialization fails rather
than corrupting the running image if the region would overlap the NuttX
binary, checked against __flash_binary_end.

Erase and program use the bootrom flash routines.  Those stall instruction
fetch from the same flash, so they run from SRAM with interrupts disabled
and, on SMP builds, the other core parked; afterwards the QSPI interface is
returned to execute-in-place mode.  By default that restores the fast read
mode the bootrom configured at boot; RP23XX_FLASH_MTD_SAFE_XIP instead
always uses the bootrom flash_enter_cmd_xip routine, which is slower to
execute from but depends only on the documented bootrom entry point.

The driver answers BIOC_XIPBASE with the memory-mapped address of the
region, so a filesystem supporting execute in place can hand out real flash
pointers rather than copying into RAM.

The common board bringup registers the device as /dev/rpflash.

Assisted-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
Signed-off-by: Marco Casaroli <marco.casaroli@gmail.com>
This commit is contained in:
Marco Casaroli 2026-07-25 10:40:13 +02:00 committed by Xiang Xiao
parent 6da4269c46
commit 8c94a72657
7 changed files with 946 additions and 0 deletions

View file

@ -49,6 +49,11 @@
# include "rp23xx_wdt.h"
#endif
#ifdef CONFIG_RP23XX_FLASH_MTD
# include <nuttx/mtd/mtd.h>
# include "rp23xx_flash_mtd.h"
#endif
#if defined(CONFIG_RP23XX_ROMFS_ROMDISK_DEVNAME)
# include <rp23xx_romfsimg.h>
#endif
@ -405,6 +410,30 @@ int rp23xx_common_bringup(void)
}
#endif
#ifdef CONFIG_RP23XX_FLASH_MTD
/* Expose the unused tail of the QSPI flash as an MTD device. This one
* answers BIOC_XIPBASE, so a filesystem layered on it can serve
* execute-in-place mappings straight out of flash.
*/
{
struct mtd_dev_s *mtd = rp23xx_flash_mtd_initialize();
if (mtd == NULL)
{
serr("ERROR: Failed to initialize the flash MTD device\n");
}
else
{
ret = register_mtddriver("/dev/rpflash", mtd, 0755, NULL);
if (ret < 0)
{
serr("ERROR: Failed to register /dev/rpflash: %d\n", ret);
}
}
}
#endif
#ifdef CONFIG_RP23XX_I2S
ret = board_i2sdev_initialize(0);
if (ret < 0)