Skip to content

Commit

Permalink
drivers: flash: mcux flexspi nor: copy data to RAM buffer on write
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
pdgendt authored and MaureenHelm committed Apr 15, 2021
1 parent bd67c23 commit 3ee5f42
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
9 changes: 9 additions & 0 deletions drivers/flash/Kconfig.mcux
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,15 @@ config FLASH_MCUX_FLEXSPI_NOR
select MEMC
select MEMC_MCUX_FLEXSPI

config FLASH_MCUX_FLEXSPI_NOR_WRITE_BUFFER
bool "MCUX FlexSPI NOR write RAM buffer"
default y
depends on FLASH_MCUX_FLEXSPI_NOR
help
Copy the data to a RAM buffer before writing it to the flash.
This prevents faults when the data to write would be located on the
flash itself.

config FLASH_MCUX_FLEXSPI_XIP
bool "MCUX FlexSPI flash access with xip"
depends on MEMC_MCUX_FLEXSPI
Expand Down
11 changes: 11 additions & 0 deletions drivers/flash/flash_mcux_flexspi_nor.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
#define NOR_WRITE_SIZE 1
#define NOR_ERASE_VALUE 0xff

#ifdef CONFIG_FLASH_MCUX_FLEXSPI_NOR_WRITE_BUFFER
static uint8_t nor_write_buf[SPI_NOR_PAGE_SIZE];
#endif

LOG_MODULE_REGISTER(flash_flexspi_nor, CONFIG_FLASH_LOG_LEVEL);

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

while (len) {
i = MIN(SPI_NOR_PAGE_SIZE, len);
#ifdef CONFIG_FLASH_MCUX_FLEXSPI_NOR_WRITE_BUFFER
memcpy(nor_write_buf, src, i);
#endif
flash_flexspi_nor_write_enable(dev);
#ifdef CONFIG_FLASH_MCUX_FLEXSPI_NOR_WRITE_BUFFER
flash_flexspi_nor_page_program(dev, offset, nor_write_buf, i);
#else
flash_flexspi_nor_page_program(dev, offset, src, i);
#endif
flash_flexspi_nor_wait_bus_busy(dev);
memc_flexspi_reset(data->controller);
src += i;
Expand Down

0 comments on commit 3ee5f42

Please sign in to comment.