2525#include "fsl_flash.h"
2626#include "fsl_ftfx_cache.h"
2727
28+ static flash_config_t pflash ;
29+ static ftfx_cache_config_t pcache ;
30+ static int flash_init = 0 ;
2831
32+ #ifdef __WOLFBOOT
2933#define CPU_CORE_CLOCK 120000000U
3034
3135static void CLOCK_CONFIG_SetFllExtRefDiv (uint8_t frdiv )
3236{
3337 MCG -> C1 = ((MCG -> C1 & ~MCG_C1_FRDIV_MASK ) | MCG_C1_FRDIV (frdiv ));
3438}
3539
36- static flash_config_t pflash ;
37- static ftfx_cache_config_t pcache ;
38- static int flash_init = 0 ;
40+ static void do_flash_init (void );
3941
4042/* This are the registers for the NV flash configuration area.
4143 * Access these field by setting the relative flags in NV_Flash_Config.
@@ -45,7 +47,7 @@ static int flash_init = 0;
4547const uint8_t __attribute__((section (".flash_config" ))) NV_Flash_Config [NVTYPE_LEN ] = {
4648 /* Backdoor comparison key (2 words) */
4749 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF , 0xFF ,
48-
50+
4951 /* P-Flash protection 1 */
5052 0xFF , 0xFF ,
5153 /* P-Flash protection 2 */
@@ -120,6 +122,43 @@ const osc_config_t oscConfig_BOARD_BootClockRUN =
120122 }
121123};
122124
125+ void hal_init (void )
126+ {
127+ // Disable Watchdog
128+ // Write 0xC520 to watchdog unlock register
129+ * ((volatile unsigned short * )0x4005200E ) = 0xC520 ;
130+ // Followed by 0xD928 to complete the unlock
131+ * ((volatile unsigned short * )0x4005200E ) = 0xD928 ;
132+ // Now disable watchdog via STCTRLH register
133+ * ((volatile unsigned short * )0x40052000 ) = 0x01D2u ;
134+
135+ /* Set the system clock dividers in SIM to safe value. */
136+ CLOCK_SetSimSafeDivs ();
137+ /* Initializes OSC0 according to board configuration. */
138+ CLOCK_InitOsc0 (& oscConfig_BOARD_BootClockRUN );
139+ CLOCK_SetXtal0Freq (oscConfig_BOARD_BootClockRUN .freq );
140+ /* Configure the Internal Reference clock (MCGIRCLK). */
141+ CLOCK_SetInternalRefClkConfig (mcgConfig_BOARD_BootClockRUN .irclkEnableMode ,
142+ mcgConfig_BOARD_BootClockRUN .ircs ,
143+ mcgConfig_BOARD_BootClockRUN .fcrdiv );
144+ /* Configure FLL external reference divider (FRDIV). */
145+ CLOCK_CONFIG_SetFllExtRefDiv (mcgConfig_BOARD_BootClockRUN .frdiv );
146+ /* Set MCG to PEE mode. */
147+ CLOCK_BootToPeeMode (mcgConfig_BOARD_BootClockRUN .oscsel ,
148+ kMCG_PllClkSelPll0 ,
149+ & mcgConfig_BOARD_BootClockRUN .pll0Config );
150+ /* Set the clock configuration in SIM module. */
151+ CLOCK_SetSimConfig (& simConfig_BOARD_BootClockRUN );
152+ do_flash_init ();
153+ }
154+
155+ void hal_prepare_boot (void )
156+ {
157+ }
158+
159+
160+ #endif
161+
123162static void do_flash_init (void )
124163{
125164 if (flash_init )
@@ -161,6 +200,7 @@ int hal_flash_write(uint32_t address, const uint8_t *data, int len)
161200 address += len_align ;
162201 }
163202 }
203+ FTFx_CACHE_ClearCachePrefetchSpeculation (& pcache , 1 );
164204 return 0 ;
165205}
166206
@@ -183,33 +223,9 @@ int hal_flash_erase(uint32_t address, int len)
183223 len -= WOLFBOOT_SECTOR_SIZE ;
184224 idx ++ ;
185225 } while (len > 0 );
226+ FTFx_CACHE_ClearCachePrefetchSpeculation (& pcache , 1 );
186227 return 0 ;
187228}
188229
189- void hal_init (void )
190- {
191- /* Set the system clock dividers in SIM to safe value. */
192- CLOCK_SetSimSafeDivs ();
193- /* Initializes OSC0 according to board configuration. */
194- CLOCK_InitOsc0 (& oscConfig_BOARD_BootClockRUN );
195- CLOCK_SetXtal0Freq (oscConfig_BOARD_BootClockRUN .freq );
196- /* Configure the Internal Reference clock (MCGIRCLK). */
197- CLOCK_SetInternalRefClkConfig (mcgConfig_BOARD_BootClockRUN .irclkEnableMode ,
198- mcgConfig_BOARD_BootClockRUN .ircs ,
199- mcgConfig_BOARD_BootClockRUN .fcrdiv );
200- /* Configure FLL external reference divider (FRDIV). */
201- CLOCK_CONFIG_SetFllExtRefDiv (mcgConfig_BOARD_BootClockRUN .frdiv );
202- /* Set MCG to PEE mode. */
203- CLOCK_BootToPeeMode (mcgConfig_BOARD_BootClockRUN .oscsel ,
204- kMCG_PllClkSelPll0 ,
205- & mcgConfig_BOARD_BootClockRUN .pll0Config );
206- /* Set the clock configuration in SIM module. */
207- CLOCK_SetSimConfig (& simConfig_BOARD_BootClockRUN );
208- do_flash_init ();
209- }
210-
211- void hal_prepare_boot (void )
212- {
213- }
214230
215231
0 commit comments