Skip to content

Commit

Permalink
drivers: flash: mcx: Clear cache after erase
Browse files Browse the repository at this point in the history
Cache needs to be cleared after erase in order to read back erased
values correctly.

Signed-off-by: Declan Snyder <declan.snyder@nxp.com>
  • Loading branch information
decsny authored and mmahadevan108 committed Nov 8, 2024
1 parent e3ec702 commit e8cf960
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions drivers/flash/soc_flash_mcux.c
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,32 @@ static status_t is_area_readable(uint32_t addr, size_t len)
}
#endif /* CONFIG_CHECK_BEFORE_READING && ! CONFIG_SOC_LPC55S36 */

#define SOC_FLASH_NEED_CLEAR_CACHES 1
#ifdef CONFIG_SOC_SERIES_MCXW
static void clear_flash_caches(void)
{
volatile uint32_t *const smscm_ocmdr0 = (volatile uint32_t *)0x40015400;
/* this bit clears the flash cache */
*smscm_ocmdr0 |= BIT(8);
volatile uint32_t *mcm_cpcr2 = (volatile uint32_t *)0xe0080034;
/* this bit clears the code cache */
*mcm_cpcr2 |= BIT(0);
}
#elif CONFIG_SOC_SERIES_MCXN
static void clear_flash_caches(void)
{
volatile uint32_t *const nvm_ctrl = (volatile uint32_t *)0x40000400;
/* this bit clears the flash cache */
*nvm_ctrl |= BIT(5);
volatile uint32_t *const lpcac_ctrl = (volatile uint32_t *)0x40000824;
/* this bit clears the code cache */
*lpcac_ctrl |= BIT(1);
}
#else
#undef SOC_FLASH_NEED_CLEAR_CACHES
#define clear_flash_caches(...)
#endif

struct flash_priv {
flash_config_t config;
/*
Expand Down Expand Up @@ -172,6 +198,11 @@ static int flash_mcux_erase(const struct device *dev, off_t offset,
(FMU_Type *) DT_INST_REG_ADDR(0),
#endif
addr, len, kFLASH_ApiEraseKey);

if (IS_ENABLED(SOC_FLASH_NEED_CLEAR_CACHES)) {
clear_flash_caches();
}

irq_unlock(key);

k_sem_give(&priv->write_lock);
Expand Down Expand Up @@ -277,6 +308,11 @@ static int flash_mcux_write(const struct device *dev, off_t offset,
(FMU_Type *) DT_INST_REG_ADDR(0),
#endif
addr, (uint8_t *) data, len);

if (IS_ENABLED(SOC_FLASH_NEED_CLEAR_CACHES)) {
clear_flash_caches();
}

irq_unlock(key);

k_sem_give(&priv->write_lock);
Expand Down

0 comments on commit e8cf960

Please sign in to comment.