From ae4d38e98a2685c4f7c32e8b45a1b985a3e17653 Mon Sep 17 00:00:00 2001 From: Mateusz Michalek Date: Fri, 21 Nov 2025 15:41:15 +0100 Subject: [PATCH] drivers: flash: RRAM nonblocking writes removes while loop in driver. Signed-off-by: Mateusz Michalek --- drivers/flash/soc_flash_nrf_rram.c | 50 +++++++++++++------------ subsys/softdevice_handler/nrf_sdh_soc.c | 7 ++++ 2 files changed, 33 insertions(+), 24 deletions(-) diff --git a/drivers/flash/soc_flash_nrf_rram.c b/drivers/flash/soc_flash_nrf_rram.c index 373d00e4ec..f4f005105c 100644 --- a/drivers/flash/soc_flash_nrf_rram.c +++ b/drivers/flash/soc_flash_nrf_rram.c @@ -56,6 +56,8 @@ LOG_MODULE_REGISTER(flash_nrf_rram, CONFIG_FLASH_LOG_LEVEL); #define ERASE_VALUE 0xFF #define WRITE_LINE_SIZE WRITE_BLOCK_SIZE_FROM_DT +static volatile bool write_busy; + static inline bool is_aligned_32(uint32_t data) { return (data & 0x3) ? false : true; @@ -88,6 +90,18 @@ static int nrf_rram_read(const struct device *dev, off_t addr, void *data, size_ return 0; } +void rrw_done(void) +{ + uint32_t pm = __get_PRIMASK(); + + __disable_irq(); + write_busy = false; + if (!pm) { + __enable_irq(); + } + barrier_dmem_fence_full(); /* Barrier following our last write. */ +} + static int nrf_rram_write(const struct device *dev, off_t addr, const void *data, size_t len) { int ret = 0; @@ -113,6 +127,18 @@ static int nrf_rram_write(const struct device *dev, off_t addr, const void *data return 0; } + uint32_t pm = __get_PRIMASK(); + + __disable_irq(); + if (write_busy) { + if (!pm) { + __enable_irq(); + } + return -EBUSY; + } + if (!pm) { + __enable_irq(); + } LOG_DBG("Write: %p: %zu", (void *)addr, len); ret = sd_flash_write((uint32_t *)addr, data, len / sizeof(uint32_t)); @@ -121,30 +147,6 @@ static int nrf_rram_write(const struct device *dev, off_t addr, const void *data return -EIO; } - while (1) { - int taskid; - - /* Wait for an event. */ - __WFE(); - - /* Clear Event Register */ - __SEV(); - __WFE(); - - ret = sd_evt_get(&taskid); - - if (!ret && (taskid == NRF_EVT_FLASH_OPERATION_SUCCESS || - taskid == NRF_EVT_FLASH_OPERATION_ERROR)) { - if (taskid != NRF_EVT_FLASH_OPERATION_SUCCESS) { - ret = -EIO; - } - - break; - } - } - - barrier_dmem_fence_full(); /* Barrier following our last write. */ - return ret; } diff --git a/subsys/softdevice_handler/nrf_sdh_soc.c b/subsys/softdevice_handler/nrf_sdh_soc.c index ba35c54aee..32df5589de 100644 --- a/subsys/softdevice_handler/nrf_sdh_soc.c +++ b/subsys/softdevice_handler/nrf_sdh_soc.c @@ -51,6 +51,8 @@ const char *nrf_sdh_soc_evt_to_str(uint32_t evt) } } +void rrw_done(void); + static void soc_evt_poll(void *context) { uint32_t nrf_err; @@ -64,6 +66,11 @@ static void soc_evt_poll(void *context) LOG_DBG("%s", nrf_sdh_soc_evt_to_str(evt_id)); + if (evt_id == NRF_EVT_FLASH_OPERATION_SUCCESS) { +#if defined(CONFIG_SOC_FLASH_NRF_RRAM_BM) + rrw_done(); +#endif + } /* Forward the event to SoC observers. */ TYPE_SECTION_FOREACH( struct nrf_sdh_soc_evt_observer, nrf_sdh_soc_evt_observers, obs) {