Skip to content

Commit 37a441a

Browse files
committed
drivers: flash: mcux flexspi nor: copy data to RAM buffer on write
This feature prevents issues when trying to write data to the flash device that is located on or addressed at the same flash device. An example is the mcuboot logic that writes a magic number to the secondary partition header. Signed-off-by: Pieter De Gendt <[email protected]>
1 parent b0d75d7 commit 37a441a

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

drivers/flash/Kconfig.mcux

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,15 @@ config FLASH_MCUX_FLEXSPI_NOR
3434
select MEMC
3535
select MEMC_MCUX_FLEXSPI
3636

37+
config FLASH_MCUX_FLEXSPI_NOR_WRITE_BUFFER
38+
bool "MCUX FlexSPI NOR write RAM buffer"
39+
default y
40+
depends on FLASH_MCUX_FLEXSPI_NOR
41+
help
42+
Copy the data to a RAM buffer before writing it to the flash.
43+
This prevents faults when the data to write would be located on the
44+
flash itself.
45+
3746
config FLASH_MCUX_FLEXSPI_XIP
3847
bool "MCUX FlexSPI flash access with xip"
3948
depends on MEMC_MCUX_FLEXSPI

drivers/flash/flash_mcux_flexspi_nor.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@
1919
#define NOR_WRITE_SIZE 1
2020
#define NOR_ERASE_VALUE 0xff
2121

22+
#ifdef CONFIG_FLASH_MCUX_FLEXSPI_NOR_WRITE_BUFFER
23+
static uint8_t nor_write_buf[SPI_NOR_PAGE_SIZE];
24+
#endif
25+
2226
LOG_MODULE_REGISTER(flash_flexspi_nor, CONFIG_FLASH_LOG_LEVEL);
2327

2428
enum {
@@ -346,8 +350,15 @@ static int flash_flexspi_nor_write(const struct device *dev, off_t offset,
346350

347351
while (len) {
348352
i = MIN(SPI_NOR_PAGE_SIZE, len);
353+
#ifdef CONFIG_FLASH_MCUX_FLEXSPI_NOR_WRITE_BUFFER
354+
memcpy(nor_write_buf, src, i);
355+
#endif
349356
flash_flexspi_nor_write_enable(dev);
357+
#ifdef CONFIG_FLASH_MCUX_FLEXSPI_NOR_WRITE_BUFFER
358+
flash_flexspi_nor_page_program(dev, offset, nor_write_buf, i);
359+
#else
350360
flash_flexspi_nor_page_program(dev, offset, src, i);
361+
#endif
351362
flash_flexspi_nor_wait_bus_busy(dev);
352363
memc_flexspi_reset(data->controller);
353364
src += i;

0 commit comments

Comments
 (0)