drivers: flash: stm32: add a weak flash_stm32_valid_range()

Most implementations have the same logic, with only a different write
block size. Now that we are using write-block-size from the device tree,
it is possible to use a default implementation that can be overridden if
necessary.

Signed-off-by: Florian Vaussard <florian.vaussard@gmail.com>
This commit is contained in:
Florian Vaussard 2023-07-25 22:47:17 +02:00 committed by Fabio Baltieri
parent 4c9e42925e
commit 8bac51be1e
7 changed files with 13 additions and 60 deletions

View file

@ -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;

View file

@ -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)

View file

@ -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) {

View file

@ -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)

View file

@ -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) {

View file

@ -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);

View file

@ -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
*/