From 199486546ac93fa5d45fa82aafe7704bb8e615fe Mon Sep 17 00:00:00 2001 From: Florian Vaussard Date: Tue, 25 Jul 2023 22:47:16 +0200 Subject: [PATCH] drivers: flash: stm32l5: use write-block-size when validating STM32L5 have a write block size of 8, but STM32U5 and STM32H5 have a write block size of 16. Use write-block-size from the device tree instead of hardcoding this value when validating the range of write operations. Fixes #60724 Signed-off-by: Florian Vaussard --- drivers/flash/flash_stm32.h | 6 ++++++ drivers/flash/flash_stm32l5x.c | 8 +++++--- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/drivers/flash/flash_stm32.h b/drivers/flash/flash_stm32.h index dd8628f81d7..e0fba89a550 100644 --- a/drivers/flash/flash_stm32.h +++ b/drivers/flash/flash_stm32.h @@ -257,6 +257,12 @@ static inline bool flash_stm32_range_exists(const struct device *dev, } #endif /* CONFIG_FLASH_PAGE_LAYOUT */ +static inline bool flash_stm32_valid_write(off_t offset, uint32_t len) +{ + return ((offset % FLASH_STM32_WRITE_BLOCK_SIZE == 0) && + (len % FLASH_STM32_WRITE_BLOCK_SIZE == 0U)); +} + bool flash_stm32_valid_range(const struct device *dev, off_t offset, uint32_t len, bool write); diff --git a/drivers/flash/flash_stm32l5x.c b/drivers/flash/flash_stm32l5x.c index a0b692b45c8..b4381289ad1 100644 --- a/drivers/flash/flash_stm32l5x.c +++ b/drivers/flash/flash_stm32l5x.c @@ -128,7 +128,7 @@ static int icache_wait_for_invalidate_complete(void) #endif /* CONFIG_SOC_SERIES_STM32H5X */ /* - * offset and len must be aligned on 8 for write, + * offset and len must be aligned on write-block-size for write, * positive and not beyond end of flash */ bool flash_stm32_valid_range(const struct device *dev, off_t offset, @@ -149,8 +149,10 @@ bool flash_stm32_valid_range(const struct device *dev, off_t offset, } } - return (!write || (offset % 8 == 0 && len % 8 == 0U)) && - flash_stm32_range_exists(dev, offset, len); + if (write && !flash_stm32_valid_write(offset, len)) { + return false; + } + return flash_stm32_range_exists(dev, offset, len); } static int write_dword(const struct device *dev, off_t offset, uint64_t val)