Skip to content

Commit a92c1b9

Browse files
dgarskedanielinux
authored andcommitted
STM32U5 cache support. Including cache invalidate on nvm_select_fresh_sector. ZD 18210
1 parent 6f83a79 commit a92c1b9

File tree

4 files changed

+63
-0
lines changed

4 files changed

+63
-0
lines changed

hal/stm32u5.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -526,3 +526,29 @@ void hal_prepare_boot(void)
526526
led_unsecure();
527527
#endif
528528
}
529+
530+
void hal_cache_enable(int way)
531+
{
532+
ICACHE_CR |= (way ? ICACHE_CR_2WAYS : ICACHE_CR_1WAY);
533+
ICACHE_CR |= ICACHE_CR_CEN;
534+
}
535+
536+
void hal_cache_disable(void)
537+
{
538+
ICACHE_CR &= ~ICACHE_CR_CEN;
539+
}
540+
541+
void hal_cache_invalidate(void)
542+
{
543+
/* Check if no ongoing operation */
544+
if ((ICACHE_SR & ICACHE_SR_BUSYF) == 0) {
545+
/* Launch cache invalidation */
546+
ICACHE_CR |= ICACHE_CR_CACHEINV;
547+
}
548+
549+
if (ICACHE_SR & ICACHE_SR_BUSYF) {
550+
while ((ICACHE_SR & ICACHE_SR_BSYENDF) == 0);
551+
}
552+
/* Clear BSYENDF */
553+
ICACHE_SR |= ICACHE_SR_BSYENDF;
554+
}

hal/stm32u5.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,3 +263,25 @@
263263
#define AIRCR_VKEY (0x05FA << 16)
264264
#define AIRCR_SYSRESETREQ (1 << 2)
265265

266+
/* Cache */
267+
#if (TZ_SECURE())
268+
#define ICACHE_BASE (0x50030400) /* RM0456 - Table 4 */
269+
#else
270+
#define ICACHE_BASE (0x40030400) /* RM0456 - Table 4 */
271+
#endif
272+
#define ICACHE_CR *(volatile uint32_t *)(ICACHE_BASE + 0x00)
273+
#define ICACHE_CR_WAYSEL (1 << 2)
274+
#define ICACHE_CR_1WAY 0U /* 1-way cache (direct mapped cache) */
275+
#define ICACHE_CR_2WAYS ICACHE_CR_WAYSEL /* 2-ways set associative cache */
276+
277+
#define ICACHE_CR_CACHEINV (1 << 1)
278+
#define ICACHE_CR_CEN (1 << 0)
279+
280+
#define ICACHE_SR *(volatile uint32_t *)(ICACHE_BASE + 0x04)
281+
#define ICACHE_SR_BUSYF (1 << 0) /* busy flag */
282+
#define ICACHE_SR_BSYENDF (1 << 1) /* busy end flag */
283+
#define ICACHE_SR_ERRF (1 << 2) /* cache error flag */
284+
285+
void hal_cache_invalidate(void);
286+
void hal_cache_enable(int way);
287+
void hal_cache_disable(void);

include/image.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,14 @@ int wolfBot_get_dts_size(void *dts_addr);
5858
#endif
5959
#endif
6060

61+
#ifndef WEAKFUNCTION
62+
# if defined(__GNUC__) || defined(__CC_ARM)
63+
# define WEAKFUNCTION __attribute__((weak))
64+
# else
65+
# define WEAKFUNCTION
66+
# endif
67+
#endif
68+
6169

6270
#ifndef WOLFBOOT_FLAGS_INVERT
6371
#define SECT_FLAG_NEW 0x0F

src/libwolfboot.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,11 @@ static uint8_t get_base_offset(uint8_t *base, uintptr_t off)
189189
#pragma GCC diagnostic pop
190190
#endif
191191

192+
void WEAKFUNCTION hal_cache_invalidate(void)
193+
{
194+
/* if cache flushing is required implement in hal */
195+
}
196+
192197
static int RAMFUNCTION nvm_select_fresh_sector(int part)
193198
{
194199
int sel;
@@ -198,6 +203,8 @@ static int RAMFUNCTION nvm_select_fresh_sector(int part)
198203
uint32_t word_0;
199204
uint32_t word_1;
200205

206+
hal_cache_invalidate();
207+
201208
/* if FLAGS_HOME check both boot and update for changes */
202209
#ifdef FLAGS_HOME
203210
base = (uint8_t *)PART_BOOT_ENDFLAGS;

0 commit comments

Comments
 (0)