2626#include <target.h>
2727#include "image.h"
2828#include "printf.h"
29+ #include "fsl_cache.h"
2930#include "fsl_common.h"
3031#include "fsl_iomuxc.h"
3132#include "fsl_nor_flash.h"
@@ -831,8 +832,6 @@ void hal_prepare_boot(void)
831832
832833#endif /* __WOLFBOOT */
833834
834- void DCACHE_InvalidateByRange (uint32_t address , uint32_t size_byte );
835-
836835static int hal_flash_init (void )
837836{
838837#ifdef USE_GET_CONFIG
@@ -858,16 +857,24 @@ int RAMFUNCTION hal_flash_write(uint32_t address, const uint8_t *data, int len)
858857 status_t status ;
859858 uint32_t wbuf [CONFIG_FLASH_PAGE_SIZE / sizeof (uint32_t )];
860859 int i ;
861- asm volatile ("cpsid i" );
862860 hal_flash_init (); /* make sure g_bootloaderTree is set */
863861#ifdef DEBUG_EXT_FLASH
864862 wolfBoot_printf ("flash write: addr 0x%x, len %d\n" ,
865863 address - FLASH_BASE , len );
866864#endif
865+ /**
866+ * Disable interrupts before accessing flash when using XIP
867+ * (note 4 p.279 in i.MX RT1060 Processor Reference Manual, Rev. 3, 07/2021)
868+ */
869+ asm volatile ("cpsid i" );
867870 for (i = 0 ; i < len ; i += CONFIG_FLASH_PAGE_SIZE ) {
868871 memcpy (wbuf , data + i , CONFIG_FLASH_PAGE_SIZE );
869872 status = g_bootloaderTree -> flexSpiNorDriver -> program (0 , FLEXSPI_CONFIG ,
870873 (address + i ) - FLASH_BASE , wbuf );
874+ /**
875+ * Flash is memory mapped, so the address range must be invalidated in data cache
876+ * to ensure coherency between flash and cache
877+ */
871878 DCACHE_InvalidateByRange (address + i , sizeof (wbuf ));
872879 if (status != kStatus_Success )
873880 {
@@ -895,9 +902,17 @@ int RAMFUNCTION hal_flash_erase(uint32_t address, int len)
895902 wolfBoot_printf ("flash erase: addr 0x%x, len %d\n" ,
896903 address - FLASH_BASE , len );
897904#endif
905+ /**
906+ * Disable interrupts before accessing flash when using XIP
907+ * (note 4 p.279 in i.MX RT1060 Processor Reference Manual, Rev. 3, 07/2021)
908+ */
898909 asm volatile ("cpsid i" );
899910 status = g_bootloaderTree -> flexSpiNorDriver -> erase (0 , FLEXSPI_CONFIG ,
900911 address - FLASH_BASE , len );
912+ /**
913+ * Flash is memory mapped, so the address range must be invalidated in data cache
914+ * to ensure coherency between flash and cache
915+ */
901916 DCACHE_InvalidateByRange (address , len );
902917 asm volatile ("cpsie i" );
903918 if (status != kStatus_Success )
0 commit comments