diff --git a/drivers/flash/flash_stm32.c b/drivers/flash/flash_stm32.c index f6daea97fe3..cba4a76a969 100644 --- a/drivers/flash/flash_stm32.c +++ b/drivers/flash/flash_stm32.c @@ -45,6 +45,15 @@ static const struct flash_parameters flash_stm32_parameters = { static int flash_stm32_write_protection(const struct device *dev, bool enable); +bool __weak flash_stm32_valid_range(const struct device *dev, off_t offset, + uint32_t len, bool write) +{ + if (write && !flash_stm32_valid_write(offset, len)) { + return false; + } + return flash_stm32_range_exists(dev, offset, len); +} + int __weak flash_stm32_check_configuration(void) { return 0; diff --git a/drivers/flash/flash_stm32f1x.c b/drivers/flash/flash_stm32f1x.c index 71d5ffbeece..0c54d3f69fe 100644 --- a/drivers/flash/flash_stm32f1x.c +++ b/drivers/flash/flash_stm32f1x.c @@ -163,17 +163,6 @@ static int write_value(const struct device *dev, off_t offset, return rc; } -/* offset and len must be aligned on 2 for write - * positive and not beyond end of flash - */ -bool flash_stm32_valid_range(const struct device *dev, off_t offset, - uint32_t len, - bool write) -{ - return (!write || (offset % 2 == 0 && len % 2 == 0U)) && - flash_stm32_range_exists(dev, offset, len); -} - int flash_stm32_block_erase_loop(const struct device *dev, unsigned int offset, unsigned int len) diff --git a/drivers/flash/flash_stm32g0x.c b/drivers/flash/flash_stm32g0x.c index a3be761f391..07eea9ce32b 100644 --- a/drivers/flash/flash_stm32g0x.c +++ b/drivers/flash/flash_stm32g0x.c @@ -40,20 +40,6 @@ LOG_MODULE_REGISTER(LOG_DOMAIN); #define STM32G0_PAGES_PER_BANK \ ((STM32G0_FLASH_SIZE / STM32G0_FLASH_PAGE_SIZE) / STM32G0_BANK_COUNT) -/* - * offset and len must be aligned on 8 for write, - * positive and not beyond end of flash - * On dual-bank SoCs memory accesses starting on the first bank and continuing - * beyond the first bank into the second bank are allowed. - */ -bool flash_stm32_valid_range(const struct device *dev, off_t offset, - uint32_t len, - bool write) -{ - return (!write || (offset % 8 == 0 && len % 8 == 0)) && - flash_stm32_range_exists(dev, offset, len); -} - static inline void flush_cache(FLASH_TypeDef *regs) { if (regs->ACR & FLASH_ACR_ICEN) { diff --git a/drivers/flash/flash_stm32g4x.c b/drivers/flash/flash_stm32g4x.c index af253584064..e1f012c254b 100644 --- a/drivers/flash/flash_stm32g4x.c +++ b/drivers/flash/flash_stm32g4x.c @@ -42,8 +42,10 @@ bool flash_stm32_valid_range(const struct device *dev, off_t offset, } #endif - 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 inline void flush_cache(FLASH_TypeDef *regs) diff --git a/drivers/flash/flash_stm32l4x.c b/drivers/flash/flash_stm32l4x.c index 56fe85e1fad..24644aba95e 100644 --- a/drivers/flash/flash_stm32l4x.c +++ b/drivers/flash/flash_stm32l4x.c @@ -32,16 +32,6 @@ LOG_MODULE_REGISTER(LOG_DOMAIN); #define CONTROL_DCACHE #endif -/* offset and len must be aligned on 8 for write - * , positive and not beyond end of flash */ -bool flash_stm32_valid_range(const struct device *dev, off_t offset, - uint32_t len, - bool write) -{ - return (!write || (offset % 8 == 0 && len % 8 == 0U)) && - flash_stm32_range_exists(dev, offset, len); -} - static inline void flush_cache(FLASH_TypeDef *regs) { if (regs->ACR & FLASH_ACR_DCEN) { diff --git a/drivers/flash/flash_stm32wbax.c b/drivers/flash/flash_stm32wbax.c index 605b727ddfb..0796b9a5be4 100644 --- a/drivers/flash/flash_stm32wbax.c +++ b/drivers/flash/flash_stm32wbax.c @@ -105,18 +105,6 @@ static int icache_wait_for_invalidate_complete(void) return status; } -/* - * offset and len must be aligned on 16 for write, - * positive and not beyond end of flash - */ -bool flash_stm32_valid_range(const struct device *dev, off_t offset, - uint32_t len, - bool write) -{ - return (!write || (offset % 16 == 0 && len % 16 == 0U)) && - flash_stm32_range_exists(dev, offset, len); -} - static int write_qword(const struct device *dev, off_t offset, const uint32_t *buff) { FLASH_TypeDef *regs = FLASH_STM32_REGS(dev); diff --git a/drivers/flash/flash_stm32wbx.c b/drivers/flash/flash_stm32wbx.c index 90746d10b17..e72fddf756f 100644 --- a/drivers/flash/flash_stm32wbx.c +++ b/drivers/flash/flash_stm32wbx.c @@ -26,17 +26,6 @@ LOG_MODULE_REGISTER(LOG_DOMAIN); #define STM32WBX_PAGE_SHIFT 12 -/* offset and len must be aligned on 8 for write, - * positive and not beyond end of flash - */ -bool flash_stm32_valid_range(const struct device *dev, off_t offset, - uint32_t len, - bool write) -{ - return (!write || (offset % 8 == 0 && len % 8 == 0U)) && - flash_stm32_range_exists(dev, offset, len); -} - /* * Up to 255 4K pages */