diff --git a/Makefile b/Makefile index b9f3023420..f014486ab4 100644 --- a/Makefile +++ b/Makefile @@ -306,9 +306,8 @@ CFLAGS+=$(WOLFPSA_CFLAGS) ifeq ($(USE_GCC_HEADLESS),1) CFLAGS+=-Wall -Wextra -Wno-main -ffreestanding ifneq (,$(filter aurix_tc4xx aurix_tc4xx_csrm,$(TARGET))) - # No -fdata-sections: tricore-elf-gcc 13 emits bare-named data sections - # (.varname, not .bss.varname) that escape the iLLD linker script's - # clear/copy tables, leaving statics uninitialized at boot. + # No -fdata-sections: gcc 13's bare-named data sections (.varname) escape + # the linker script's clear/copy tables and stay uninitialized at boot. CFLAGS+=-ffunction-sections -fomit-frame-pointer else ifeq ($(TARGET)-$(GCC),aurix_tc3xx-1) # Same as above for the TC3xx host built with tricore-elf-gcc (GCC=1) diff --git a/arch.mk b/arch.mk index 74c41ca426..f81b2d754f 100644 --- a/arch.mk +++ b/arch.mk @@ -3393,7 +3393,6 @@ ifeq ($(ARCH), AURIX) # wolfHSM TC4xx port root. Expected that wolfBoot is inside as a submodule. WOLFHSM_INFINEON_TC4XX?=$(abspath ..) - TC4_LLD_DIR?=$(WOLFHSM_INFINEON_TC4XX)/drivers TC4_WB_DIR?=$(WOLFHSM_INFINEON_TC4XX)/port/wolfboot # TC4 derivative device selection from the port device.mk. @@ -3409,16 +3408,6 @@ ifeq ($(ARCH), AURIX) -ffunction-sections -fno-builtin -std=gnu99 \ -DPART_BOOT_EXT -DPART_UPDATE_EXT -DPART_SWAP_EXT \ -DWOLFBOOT_LOADER_MAIN - # Vendor iLLD sources trip some -Wextra diagnostics - CFLAGS += -Wno-missing-field-initializers -Wno-unused-parameter \ - -Wno-unused-variable -Wno-sign-compare -Wno-type-limits - - # iLLD include paths. SSW configuration differs per core. - ifeq ($(TARGET), aurix_tc4xx_csrm) - CFLAGS += -I$(TC4_WB_DIR)/csrm -I$(TC4_WB_DIR)/csrm/Cfg_Ssw - else - CFLAGS += -I$(TC4_WB_DIR) -I$(TC4_WB_DIR)/Cfg_Ssw - endif # Linker scripts ship with the port, not in hal/. ifeq ($(TARGET), aurix_tc4xx_csrm) @@ -3426,73 +3415,6 @@ ifeq ($(ARCH), AURIX) else $(call LSCRIPT_IN_DEFAULT,$(TC4_WB_DIR)/aurix_tc4xx.ld) endif - CFLAGS += \ - -I$(TC4_LLD_DIR) \ - -I$(TC4_LLD_DIR)/Infra/Ssw/TC4xx/Csrm \ - -I$(TC4_LLD_DIR)/iLLD/TC4xx/Csrm \ - -I$(TC4_LLD_DIR)/iLLD/TC4xx/Csrm/Cpu/Trap \ - -I$(TC4_LLD_DIR)/Infra \ - -I$(TC4_LLD_DIR)/Infra/Platform \ - -I$(TC4_LLD_DIR)/Infra/Platform/Compilers \ - -I$(TC4_LLD_DIR)/Infra/Sfr \ - -I$(TC4_LLD_DIR)/Infra/Sfr/$(DEV_DERIV) \ - -I$(TC4_LLD_DIR)/Infra/Ssw \ - -I$(TC4_LLD_DIR)/Infra/Ssw/TC4xx \ - -I$(TC4_LLD_DIR)/Infra/Ssw/TC4xx/Tricore \ - -I$(TC4_LLD_DIR)/Service \ - -I$(TC4_LLD_DIR)/Service/CpuGeneric \ - -I$(TC4_LLD_DIR)/Service/CpuGeneric/If \ - -I$(TC4_LLD_DIR)/Service/CpuGeneric/If/Ccu6If \ - -I$(TC4_LLD_DIR)/Service/CpuGeneric/StdIf \ - -I$(TC4_LLD_DIR)/Service/CpuGeneric/SysSe \ - -I$(TC4_LLD_DIR)/Service/CpuGeneric/SysSe/Bsp \ - -I$(TC4_LLD_DIR)/Service/CpuGeneric/SysSe/General \ - -I$(TC4_LLD_DIR)/Service/CpuGeneric/SysSe/Time \ - -I$(TC4_LLD_DIR)/Service/CpuGeneric/_Utilities \ - -I$(TC4_LLD_DIR)/Service/Tricore \ - -I$(TC4_LLD_DIR)/Service/Tricore/Comm \ - -I$(TC4_LLD_DIR)/Service/Tricore/Math \ - -I$(TC4_LLD_DIR)/iLLD \ - -I$(TC4_LLD_DIR)/iLLD/TC4xx \ - -I$(TC4_LLD_DIR)/iLLD/TC4xx/CpuGeneric \ - -I$(TC4_LLD_DIR)/iLLD/TC4xx/CpuGeneric/Ap \ - -I$(TC4_LLD_DIR)/iLLD/TC4xx/CpuGeneric/Ap/Std \ - -I$(TC4_LLD_DIR)/iLLD/TC4xx/CpuGeneric/Asclin \ - -I$(TC4_LLD_DIR)/iLLD/TC4xx/CpuGeneric/Asclin/Asc \ - -I$(TC4_LLD_DIR)/iLLD/TC4xx/CpuGeneric/Asclin/Std \ - -I$(TC4_LLD_DIR)/iLLD/TC4xx/CpuGeneric/Clock \ - -I$(TC4_LLD_DIR)/iLLD/TC4xx/CpuGeneric/Clock/Std \ - -I$(TC4_LLD_DIR)/iLLD/TC4xx/CpuGeneric/Egtm \ - -I$(TC4_LLD_DIR)/iLLD/TC4xx/CpuGeneric/Egtm/Std \ - -I$(TC4_LLD_DIR)/iLLD/TC4xx/CpuGeneric/Geth \ - -I$(TC4_LLD_DIR)/iLLD/TC4xx/CpuGeneric/Geth/Std \ - -I$(TC4_LLD_DIR)/iLLD/TC4xx/CpuGeneric/Port \ - -I$(TC4_LLD_DIR)/iLLD/TC4xx/CpuGeneric/Port/Std \ - -I$(TC4_LLD_DIR)/iLLD/TC4xx/CpuGeneric/Src \ - -I$(TC4_LLD_DIR)/iLLD/TC4xx/CpuGeneric/Src/Std \ - -I$(TC4_LLD_DIR)/iLLD/TC4xx/CpuGeneric/_Impl \ - -I$(TC4_LLD_DIR)/iLLD/TC4xx/CpuGeneric/_Impl/$(DEV_DERIV) \ - -I$(TC4_LLD_DIR)/iLLD/TC4xx/CpuGeneric/_Lib \ - -I$(TC4_LLD_DIR)/iLLD/TC4xx/CpuGeneric/_Lib/DataHandling \ - -I$(TC4_LLD_DIR)/iLLD/TC4xx/CpuGeneric/_Lib/Timer \ - -I$(TC4_LLD_DIR)/iLLD/TC4xx/CpuGeneric/_PinMap \ - -I$(TC4_LLD_DIR)/iLLD/TC4xx/CpuGeneric/_PinMap/$(DEV_DERIV) \ - -I$(TC4_LLD_DIR)/iLLD/TC4xx/Tricore \ - -I$(TC4_LLD_DIR)/iLLD/TC4xx/Tricore/Cpu \ - -I$(TC4_LLD_DIR)/iLLD/TC4xx/Tricore/Cpu/Irq \ - -I$(TC4_LLD_DIR)/iLLD/TC4xx/Tricore/Cpu/Std \ - -I$(TC4_LLD_DIR)/iLLD/TC4xx/Tricore/Cpu/Trap \ - -I$(TC4_LLD_DIR)/iLLD/TC4xx/Tricore/Flash/Std \ - -I$(TC4_LLD_DIR)/iLLD/TC4xx/Tricore/Smu \ - -I$(TC4_LLD_DIR)/iLLD/TC4xx/Tricore/Smu/Std \ - -I$(TC4_LLD_DIR)/iLLD/TC4xx/Tricore/Stm \ - -I$(TC4_LLD_DIR)/iLLD/TC4xx/Tricore/Stm/Std \ - -I$(TC4_LLD_DIR)/iLLD/TC4xx/Tricore/Vmt \ - -I$(TC4_LLD_DIR)/iLLD/TC4xx/Tricore/Vmt/Std \ - -I$(TC4_LLD_DIR)/iLLD/TC4xx/Tricore/Wtu \ - -I$(TC4_LLD_DIR)/iLLD/TC4xx/Tricore/Wtu/Std \ - -I$(TC4_LLD_DIR)/iLLD/TC4xx/Tricore/_Impl \ - -I$(TC4_LLD_DIR)/iLLD/TC4xx/Tricore/_Impl/$(DEV_DERIV) # No TriCore asm in wolfCrypt MATH_OBJS+=$(WOLFBOOT_LIB_WOLFSSL)/wolfcrypt/src/sp_c32.o @@ -3545,45 +3467,11 @@ ifeq ($(ARCH), AURIX) # such that the binary image can be contiguous OBJCOPY_FLAGS+=-R '.bmhd*' -R '.usercfg*' -R '.csusercfg*' -R '.sdata4' -R '.sdata' -R '.zdata' - # iLLD startup software and drivers used by the HAL. - TC4_LLD_SRCS := \ - $(wildcard $(TC4_LLD_DIR)/Infra/Platform/Compilers/*.c) \ - $(TC4_LLD_DIR)/Infra/Ssw/TC4xx/Tricore/Ifx_Ssw_Infra.c \ - $(wildcard $(TC4_LLD_DIR)/iLLD/TC4xx/CpuGeneric/Ap/Std/*.c) \ - $(wildcard $(TC4_LLD_DIR)/iLLD/TC4xx/CpuGeneric/Asclin/Std/*.c) \ - $(wildcard $(TC4_LLD_DIR)/iLLD/TC4xx/CpuGeneric/Clock/Std/*.c) \ - $(wildcard $(TC4_LLD_DIR)/iLLD/TC4xx/CpuGeneric/Port/Std/*.c) \ - $(wildcard $(TC4_LLD_DIR)/iLLD/TC4xx/CpuGeneric/Src/Std/*.c) \ - $(wildcard $(TC4_LLD_DIR)/iLLD/TC4xx/CpuGeneric/_Impl/*.c) \ - $(wildcard $(TC4_LLD_DIR)/iLLD/TC4xx/CpuGeneric/_Impl/$(DEV_DERIV)/*.c) \ - $(wildcard $(TC4_LLD_DIR)/iLLD/TC4xx/CpuGeneric/_PinMap/IfxAsclin_PinMap*.c) \ - $(wildcard $(TC4_LLD_DIR)/iLLD/TC4xx/CpuGeneric/_PinMap/$(DEV_DERIV)/IfxAsclin_PinMap*.c) \ - $(wildcard $(TC4_LLD_DIR)/iLLD/TC4xx/Tricore/Cpu/Std/*.c) \ - $(wildcard $(TC4_LLD_DIR)/iLLD/TC4xx/Tricore/Stm/Std/*.c) \ - $(wildcard $(TC4_LLD_DIR)/iLLD/TC4xx/Tricore/Wtu/Std/*.c) \ - $(wildcard $(TC4_LLD_DIR)/iLLD/TC4xx/Tricore/_Impl/*.c) - + # wolfLLD startup and drivers, entry glue and boot mode headers ifeq ($(TARGET), aurix_tc4xx_csrm) - # CSRM (CPU6) startup software + trap table - TC4_LLD_SRCS += \ - $(TC4_LLD_DIR)/Infra/Ssw/TC4xx/Csrm/Ifx_Ssw_Tc6.c \ - $(TC4_LLD_DIR)/iLLD/TC4xx/Csrm/Cpu/Trap/IfxCpu_Trap_Cs.c - OBJS += $(TC4_LLD_SRCS:.c=.o) - OBJS += $(TC4_WB_DIR)/csrm/Cfg_Ssw/Ifx_Cfg_Ssw.o \ - $(TC4_WB_DIR)/csrm/Cfg_Ssw/Ifx_Cfg_SswBmhdCs.o \ - $(TC4_WB_DIR)/csrm/tc4_wolfboot_csrm_main.o + OBJS += $(TCHSM_WB_CSRM_LLD_OBJS) $(TCHSM_WB_CSRM_OBJS) else - # Host CPU0 startup software and trap table. - TC4_LLD_SRCS += \ - $(TC4_LLD_DIR)/Infra/Ssw/TC4xx/Tricore/Ifx_Ssw_Tc0.c \ - $(wildcard $(TC4_LLD_DIR)/iLLD/TC4xx/Tricore/Cpu/Irq/*.c) \ - $(wildcard $(TC4_LLD_DIR)/iLLD/TC4xx/Tricore/Cpu/Trap/*.c) \ - $(wildcard $(TC4_LLD_DIR)/iLLD/TC4xx/Tricore/Smu/Std/*.c) \ - $(wildcard $(TC4_LLD_DIR)/iLLD/TC4xx/Tricore/Vmt/Std/*.c) - OBJS += $(TC4_LLD_SRCS:.c=.o) - OBJS += $(TC4_WB_DIR)/Cfg_Ssw/Ifx_Cfg_Ssw.o \ - $(TC4_WB_DIR)/Cfg_Ssw/Ifx_Cfg_SswBmhd.o \ - $(TC4_WB_DIR)/tc4_wolfboot_main.o + OBJS += $(TCHSM_WB_HOST_LLD_OBJS) $(TCHSM_WB_HOST_OBJS) endif endif # TC4xx endif diff --git a/hal/aurix_tc4xx.c b/hal/aurix_tc4xx.c index 287392843d..4ea73f0d42 100644 --- a/hal/aurix_tc4xx.c +++ b/hal/aurix_tc4xx.c @@ -30,28 +30,23 @@ #include "loader.h" /* for wolfBoot_panic */ #include "printf.h" -/* iLLD headers */ -#include "Ifx_Types.h" -#include "IfxAsclin.h" -#include "IfxAsclin_PinMap.h" -#include "IfxCpu.h" -#include "IfxWtu.h" -#ifndef TARGET_aurix_tc4xx_csrm -#include "IfxClock.h" -#endif +/* wolfLLD headers */ +#include "tc4/tc4.h" +#include "tc4/tc4_clock.h" +#include "tc4/tc4_dmu.h" +#include "tc4/tc4_flash.h" +#include "tc4/tc4_uart.h" +#include "tc4/tc4_wtu.h" #if defined(WOLFBOOT_ENABLE_WOLFHSM_CLIENT) -#include "IfxApApu.h" -#include "IfxApProt.h" -#include "IfxSrc.h" - /* wolfHSM headers */ #include "wolfhsm/wh_client.h" #include "wolfhsm/wh_error.h" /* wolfHSM AURIX TC4xx port headers */ #include "tchsm_topology.h" #include "tchsm_client.h" +#include "tchsm_notify.h" #include "tchsm_spr_apu.h" /* wolfHSM client ID for the HSM server. @@ -77,245 +72,27 @@ _Static_assert(WOLFBOOT_WOLFHSM_CLIENT_ID == TCHSM_HSMHOST_CLIENT_APP0, #endif /* WOLFBOOT_ENABLE_WOLFHSM_CLIENT */ -#define TC4_PFLASH_PAGE_SIZE (32u) -#define TC4_PFLASH_BURST_SIZE (512u) #ifdef TARGET_aurix_tc4xx_csrm #define TC4_PROG_CHUNK_SIZE TC4_PFLASH_PAGE_SIZE #else #define TC4_PROG_CHUNK_SIZE TC4_PFLASH_BURST_SIZE #endif -/* Convert between the cached (0x8...) segment used by the wolfBoot - * partition configuration and the non-cached (0xA...) alias the command - * interface requires. */ -#define TC4_FLASH_NC(addr) (((uint32_t)(addr)) | 0x20000000u) - -/* Select FCI based on CPU */ -#ifdef TARGET_aurix_tc4xx_csrm -#define TC4_CMD_BASE (0xF80C0000u) -#else -#define TC4_CMD_BASE (0xF8080000u) -#endif - -#define TC4_CMD_REG(off) ((volatile uint32_t*)(TC4_CMD_BASE | (off))) -#define TC4_CMD_MODE TC4_CMD_REG(0x5554u) /* mode/status cycles */ -#define TC4_CMD_LOAD2X32 TC4_CMD_REG(0x55F4u) /* page assembly, 2x32-bit */ -#define TC4_CMD_ADDR TC4_CMD_REG(0xAA50u) /* command word 1: address */ -#define TC4_CMD_COUNT TC4_CMD_REG(0xAA58u) /* command word 2: count */ -#define TC4_CMD_CODE TC4_CMD_REG(0xAAA8u) /* command code, written twice */ - -#define TC4_MODE_CLEAR_STATUS (0xFAu) -#define TC4_MODE_PF_PAGEMODE (0x50u) -#define TC4_MODE_RESET_READ (0xF0u) - -/* DMU command interface status/error registers (CSCI mirrors HCI at +0x80) */ -#define TC4_DMU_REG(addr) ((volatile uint32_t*)(addr)) -#ifdef TARGET_aurix_tc4xx_csrm -#define TC4_HCI_STATUS TC4_DMU_REG(0xF8040084u) -#define TC4_HCI_ERR TC4_DMU_REG(0xF8040090u) -#define TC4_HCI_CLRERR TC4_DMU_REG(0xF8040094u) -#else -#define TC4_HCI_STATUS TC4_DMU_REG(0xF8040004u) -#define TC4_HCI_ERR TC4_DMU_REG(0xF8040010u) -#define TC4_HCI_CLRERR TC4_DMU_REG(0xF8040014u) -#endif -#define TC4_GP_BKALLOC TC4_DMU_REG(0xF8040A00u) +/* GP_BKALLOC bit that gives the CSRM PFLASH bank to the CSRM */ #define TC4_BKALLOC_CSRMPF (1u << 18) -/* HCI.STATUS fields */ -#define TC4_STATUS_BANKS_BUSY (0x000F0FFFu) /* per-bank busy + host DF + FSI \ - */ -#define TC4_STATUS_PFPAGE (1u << 25) -#define TC4_STATUS_REQDONE (1u << 31) - -#define TC4_ERR_OPFAIL_MASK \ - (0x00010077u) /* ADER|SQER|PROER|ABER|CLER|PVER|OPER */ -#define TC4_ERR_PVER (1u << 6) -#define TC4_ERR_EVER (1u << 7) -#define TC4_CLRERR_ALL (0xF7u) /* OPER (bit16) has no clear bit */ - -/* Erase attempts per sector. Infineon AN0019 says to repeat an erase that - * reports EVER at least once, since slow cells may need more erase time. - * A second EVER means the sector is bad. */ -#define TC4_ERASE_ATTEMPTS (2u) - -/* Program attempts per page or burst. AN0019 gives the same advice for a - * write that reports PVER: repeat it once with the same data. */ -#define TC4_PROGRAM_ATTEMPTS (2u) - -/* Bounded wait limits for flash commands */ -#define TC4_BUSY_SPIN_LIMIT (50000000u) -#define TC4_REQDONE_SPIN_LIMIT (1000000u) - #define WOLFBOOT_AURIX_RESET_REASON (0x5742) /* "WB" */ /* Helper macros for the base address of the page or sector containing addr */ #define GET_PAGE_ADDR(addr) ((uintptr_t)(addr) & ~(TC4_PFLASH_PAGE_SIZE - 1)) #define GET_SECTOR_ADDR(addr) ((uintptr_t)(addr) & ~(WOLFBOOT_SECTOR_SIZE - 1)) -#define TC4_DSYNC() __asm volatile("dsync" ::: "memory") +_Static_assert(WOLFBOOT_SECTOR_SIZE == TC4_PFLASH_SECTOR_SIZE, + "WOLFBOOT_SECTOR_SIZE must be the PFLASH erase sector size"); /* RAM buffer holding one flash sector for read-modify-write operations */ static uint32_t sectorBuffer[WOLFBOOT_SECTOR_SIZE / sizeof(uint32_t)]; -static void RAMFUNCTION flashClearStatus(void) -{ - *TC4_CMD_MODE = TC4_MODE_CLEAR_STATUS; - TC4_DSYNC(); -} - -/* Wait for all host banks idle and for REQDONE (end of sequence status). - * A timeout fails the operation. - * Returns 0 on completion, -1 on timeout. */ -static int RAMFUNCTION flashWaitDone(void) -{ - uint32_t spins; - - spins = TC4_BUSY_SPIN_LIMIT; - while (((*TC4_HCI_STATUS & TC4_STATUS_BANKS_BUSY) != 0u) && - (--spins != 0u)) { - } - if (spins == 0u) { - return -1; - } - - spins = TC4_REQDONE_SPIN_LIMIT; - while (((*TC4_HCI_STATUS & TC4_STATUS_REQDONE) == 0u) && (--spins != 0u)) { - } - if (spins == 0u) { - return -1; - } - - TC4_DSYNC(); - return 0; -} - -/* Read and clear the error flags. Returns the raw error register value. */ -static uint32_t RAMFUNCTION flashGetClearErrors(void) -{ - uint32_t err = *TC4_HCI_ERR; - *TC4_HCI_CLRERR = TC4_CLRERR_ALL; - TC4_DSYNC(); - return err; -} - -/* Issue a flash command and wait for completion. Returns the error flags on - * completion or TC4_ERR_OPFAIL_MASK on timeout. */ -static uint32_t RAMFUNCTION flashCommand(uint32_t address, uint32_t count, - uint32_t code1, uint32_t code2) -{ - flashClearStatus(); - *TC4_CMD_ADDR = TC4_FLASH_NC(address); - *TC4_CMD_COUNT = count; - *TC4_CMD_CODE = code1; - *TC4_CMD_CODE = code2; - TC4_DSYNC(); - - if (flashWaitDone() != 0) { - return TC4_ERR_OPFAIL_MASK; - } - return flashGetClearErrors(); -} - -/* Blank check one page via the hardware erase-verify command. - * Returns 1 if erased, 0 if programmed, -1 on error. */ -static int RAMFUNCTION flashIsPageErased(uint32_t pageAddr) -{ - uint32_t err = flashCommand(pageAddr, 0u, 0x80u, 0x56u); - if ((err & TC4_ERR_OPFAIL_MASK) != 0u) { - return -1; - } - return ((err & TC4_ERR_EVER) != 0u) ? 0 : 1; -} - -/* Erase one 16KB logical sector. Sectors are erased one at a time due to issues - * with multi-sector erases leaving the busy flag set. The erase command runs - * its own erase verify and reports EVER if the sector did not fully erase. The - * erase is retried on EVER. Returns 0 on success. */ -static int RAMFUNCTION flashEraseSector(uint32_t sectorAddr) -{ - uint32_t attempt; - - for (attempt = 0; attempt < TC4_ERASE_ATTEMPTS; attempt++) { - uint32_t err = flashCommand(sectorAddr, 1u, 0x80u, 0x50u); - if ((err & TC4_ERR_OPFAIL_MASK) != 0u) { - /* Sequence, protection, address or timeout: not retryable */ - return -1; - } - if ((err & TC4_ERR_EVER) == 0u) { - return 0; - } - } - /* EVER persisted: the sector is not reliably erased */ - return -1; -} - -/* Program a naturally aligned group of pages (one page or one burst) that - * is already erased. data must hold size bytes; size is either - * TC4_PFLASH_PAGE_SIZE or TC4_PFLASH_BURST_SIZE. A program verify error - * (PVER) is retried with the same data. Returns 0 on success. */ -static int RAMFUNCTION flashProgramAligned(uint32_t addr, const uint32_t* data, - uint32_t size) -{ - uint32_t attempt; - - for (attempt = 0; attempt < TC4_PROGRAM_ATTEMPTS; attempt++) { - uint32_t i; - uint32_t err; - uint32_t spins; - - /* Enter page mode and wait for the assembly buffer to be ready */ - flashClearStatus(); - *TC4_CMD_MODE = TC4_MODE_PF_PAGEMODE; - TC4_DSYNC(); - spins = TC4_REQDONE_SPIN_LIMIT; - while (((*TC4_HCI_STATUS & TC4_STATUS_PFPAGE) == 0u) && - (--spins != 0u)) { - } - if (spins == 0u) { - return -1; - } - - /* Fill the page assembly buffer, two 32-bit words per cycle */ - for (i = 0; i < (size / sizeof(uint32_t)); i += 2u) { - *TC4_CMD_LOAD2X32 = data[i]; - *TC4_CMD_LOAD2X32 = data[i + 1u]; - } - TC4_DSYNC(); - - /* Write Page (0xAA) or Write Burst (0xA6) */ - err = flashCommand(addr, 0u, 0xA0u, - (size == TC4_PFLASH_BURST_SIZE) ? 0xA6u : 0xAAu); - if ((err & TC4_ERR_OPFAIL_MASK) == 0u) { - return 0; - } - - /* Leave page mode so the interface is not stuck */ - *TC4_CMD_MODE = TC4_MODE_RESET_READ; - TC4_DSYNC(); - - if ((err & (TC4_ERR_OPFAIL_MASK & ~TC4_ERR_PVER)) != 0u) { - /* Sequence, protection, address or timeout: not retryable */ - return -1; - } - } - /* PVER persisted: the page did not program reliably */ - return -1; -} - -/* Read len bytes at address, which must not span an erased page (callers - * blank-check first). Reads go through the non-cached alias so no stale - * cache lines are involved. */ -static void RAMFUNCTION flashRead(uint32_t address, uint8_t* data, uint32_t len) -{ - const volatile uint8_t* src = - (const volatile uint8_t*)TC4_FLASH_NC(address); - uint32_t i; - for (i = 0; i < len; i++) { - data[i] = src[i]; - } -} - /* Read an entire sector into sectorBuffer, substituting the erased-byte * value for erased pages so no erased cell is ever read. */ static void RAMFUNCTION cacheSector(uint32_t sectorAddress) @@ -323,7 +100,7 @@ static void RAMFUNCTION cacheSector(uint32_t sectorAddress) uint32_t page; for (page = 0; page < WOLFBOOT_SECTOR_SIZE; page += TC4_PFLASH_PAGE_SIZE) { uint32_t* dst = sectorBuffer + (page / sizeof(uint32_t)); - int erased = flashIsPageErased(sectorAddress + page); + int erased = tc4_flash_IsPageErased(sectorAddress + page); if (erased < 0) { wolfBoot_panic(); @@ -335,8 +112,8 @@ static void RAMFUNCTION cacheSector(uint32_t sectorAddress) } } else { - flashRead(sectorAddress + page, (uint8_t*)dst, - TC4_PFLASH_PAGE_SIZE); + tc4_flash_Read(sectorAddress + page, (uint8_t*)dst, + TC4_PFLASH_PAGE_SIZE); } } } @@ -347,9 +124,9 @@ static void RAMFUNCTION programCachedSector(uint32_t sectorAddress) { uint32_t off; for (off = 0; off < WOLFBOOT_SECTOR_SIZE; off += TC4_PROG_CHUNK_SIZE) { - if (flashProgramAligned(sectorAddress + off, - sectorBuffer + (off / sizeof(uint32_t)), - TC4_PROG_CHUNK_SIZE) != 0) { + if (tc4_flash_Program(sectorAddress + off, + sectorBuffer + (off / sizeof(uint32_t)), + TC4_PROG_CHUNK_SIZE) != 0) { wolfBoot_panic(); } } @@ -376,8 +153,8 @@ static int RAMFUNCTION programBytesToErasedFlash(uint32_t address, } memcpy((uint8_t*)pageBuffer + offset, data, toWrite); - if (flashProgramAligned(pageAddress, pageBuffer, - TC4_PFLASH_PAGE_SIZE) != 0) { + if (tc4_flash_Program(pageAddress, pageBuffer, + TC4_PFLASH_PAGE_SIZE) != 0) { return -1; } @@ -390,66 +167,53 @@ static int RAMFUNCTION programBytesToErasedFlash(uint32_t address, return 0; } -#if defined(DEBUG_UART) || defined(UART_FLASH) +#ifdef UART_FLASH +#error "AURIX TC4xx does not support UART_FLASH" +#endif + +#ifdef DEBUG_UART -#define TC4_UART (&MODULE_ASCLIN0) #define TC4_UART_BAUD (115200u) -#define TC4_UART_FIFO_SIZE (16u) + +/* ASCLIN0 on P14.0 (TX) and P14.1 (RX). The CSRM may run before the host + * has set up the clock tree, so the divider follows the live fASCLINF. */ +static tc4_uartpin_t tc4Uart = { + .asclin = 0u, + .txport = 14u, + .txpin = 0u, + .txalt = 2u, + .rxport = 14u, + .rxpin = 1u, + .rxsel = 0u, +}; int uart_tx(const uint8_t c); -int uart_rx(uint8_t* c); void uart_init(void); void uart_write(const char* buf, unsigned int sz); +static int tc4UartReady = 0; + void uart_init(void) { - Ifx_ASCLIN* u = TC4_UART; - - IfxAsclin_enableModule(u); - IfxAsclin_setClockSource(u, IfxAsclin_ClockSource_noClock); - IfxAsclin_setFrameMode(u, IfxAsclin_FrameMode_initialise); - IfxAsclin_setPrescaler(u, 1); - IfxAsclin_setClockSource(u, IfxAsclin_ClockSource_ascFastClock); - (void)IfxAsclin_setBitTiming( - u, (float32)TC4_UART_BAUD, IfxAsclin_OversamplingFactor_16, - IfxAsclin_SamplePointPosition_8, IfxAsclin_SamplesPerBit_three); - IfxAsclin_setClockSource(u, IfxAsclin_ClockSource_noClock); - - IfxAsclin_enableParity(u, FALSE); - IfxAsclin_setStopBit(u, IfxAsclin_StopBit_1); - IfxAsclin_setShiftDirection(u, IfxAsclin_ShiftDirection_lsbFirst); - IfxAsclin_setDataLength(u, IfxAsclin_DataLength_8); - IfxAsclin_setTxFifoInletWidth(u, IfxAsclin_TxFifoInletWidth_1); - IfxAsclin_setRxFifoOutletWidth(u, IfxAsclin_RxFifoOutletWidth_1); - IfxAsclin_setFrameMode(u, IfxAsclin_FrameMode_asc); - - IfxAsclin_initTxPin(&IfxAsclin0_TX_F_P14_0_OUT, IfxPort_OutputMode_pushPull, - IfxPort_PadDriver_cmosAutomotiveSpeed1); - IfxAsclin_initRxPin(&IfxAsclin0_RXA_F_P14_1_IN, IfxPort_InputMode_pullUp, - IfxPort_PadDriver_cmosAutomotiveSpeed1); - - IfxAsclin_setClockSource(u, IfxAsclin_ClockSource_ascFastClock); - IfxAsclin_disableAllFlags(u); - IfxAsclin_clearAllFlags(u); - IfxAsclin_flushTxFifo(u); - IfxAsclin_enableTxFifoOutlet(u, TRUE); + tc4UartReady = + (tc4_uart_SetBaud(&tc4Uart, tc4_clock_GetAsclinFastFrequency(), + TC4_UART_BAUD) == 0) && + (tc4_uart_Init(&tc4Uart) == 0); } int uart_tx(const uint8_t c) { - Ifx_ASCLIN* u = TC4_UART; - while (IfxAsclin_getTxFifoFillLevel(u) >= TC4_UART_FIFO_SIZE) { + if (!tc4UartReady) { + return 0; + } + /* A stalled transmitter takes the console down after one timeout */ + if (tc4_uart_Write8(&tc4Uart, c) != 0) { + tc4UartReady = 0; + return 0; } - IfxAsclin_writeTxData(u, c); return 1; } -int uart_rx(uint8_t* c) -{ - (void)c; - return 0; -} - void uart_write(const char* buf, unsigned int sz) { while (sz > 0) { @@ -461,21 +225,7 @@ void uart_write(const char* buf, unsigned int sz) } } -/* Block until the TX FIFO has fully drained onto the wire */ -static void uart_flush(void) -{ - Ifx_ASCLIN* u = TC4_UART; - volatile uint32_t i; - - while (IfxAsclin_getTxFifoFillLevel(u) != 0u) { - } - /* Delay in case TC flag is still set from an earlier idle - * period while the last frame sits in the shift reg. */ - for (i = 0; i < 200000u; i++) { - } -} - -#endif /* DEBUG_UART || UART_FLASH */ +#endif /* DEBUG_UART */ /* * wolfBoot HAL entry points @@ -484,19 +234,23 @@ static void uart_flush(void) void hal_init(void) { #ifdef TARGET_aurix_tc4xx_csrm - IfxWtu_disableSecurityWatchdog(IfxWtu_getSecurityWatchdogPassword()); + if (tc4_wtu_Disable(TC4_WTU_WDTSEC) != 0) { + wolfBoot_panic(); + } /* The CSRM PFLASH bank must be allocated to the CSRM or the CSCI * command writes will fail silently */ - if ((*TC4_GP_BKALLOC & TC4_BKALLOC_CSRMPF) == 0u) { + if ((TC4_REG32(TC4_DMU_GP_BKALLOC) & TC4_BKALLOC_CSRMPF) == 0u) { wolfBoot_panic(); } #else - IfxWtu_disableCpuWatchdog(IfxWtu_getCpuWatchdogPassword()); - IfxWtu_disableSystemWatchdog(IfxWtu_getSystemWatchdogPassword()); + if ((tc4_wtu_Disable(TC4_WTU_WDTCPU(0u)) != 0) || + (tc4_wtu_Disable(TC4_WTU_WDTSYS) != 0)) { + wolfBoot_panic(); + } /* Refuse to run if a host PFLASH bank has been reallocated to CSRM. */ - if ((*TC4_GP_BKALLOC & 0xFu) != 0u) { + if ((TC4_REG32(TC4_DMU_GP_BKALLOC) & 0xFu) != 0u) { wolfBoot_panic(); } #endif @@ -527,15 +281,16 @@ void hal_prepare_boot(void) /* Final print, then drain so the clock switch below cannot corrupt * in-flight characters */ wolfBoot_printf("hal_prepare_boot\n"); - uart_flush(); - IfxAsclin_setClockSource(TC4_UART, IfxAsclin_ClockSource_noClock); - IfxAsclin_disableModule(TC4_UART); + if (tc4UartReady) { + (void)tc4_uart_BlockOnTC(&tc4Uart); + } #endif #ifndef TARGET_aurix_tc4xx_csrm - /* Host cores must return the clock tree to the backup clock and power the - * PLLs down, as the iLLD SSW can't handle already initialized PLLs. */ - (void)IfxClock_switchToBackupClock(&IfxClock_defaultClockConfig); + /* The application starts on the backup clock; the PLLs stay powered */ + if (tc4_clock_SwitchToBackup() != 0) { + wolfBoot_panic(); + } #endif } @@ -552,9 +307,7 @@ void RAMFUNCTION arch_reboot(void) while (1) { } #else - IfxCpu_triggerSwReset(); - while (1) { - } + tc4_TriggerSwReset(); #endif } @@ -588,7 +341,7 @@ int RAMFUNCTION hal_flash_write(uint32_t address, const uint8_t* data, int size) const uint32_t endPage = GET_PAGE_ADDR(currentAddress + bytesInThisSector - 1); for (page = startPage; page <= endPage; page += TC4_PFLASH_PAGE_SIZE) { - int erased = flashIsPageErased(page); + int erased = tc4_flash_IsPageErased(page); if (erased < 0) { return -1; } @@ -672,7 +425,7 @@ int RAMFUNCTION hal_flash_erase(uint32_t address, int len) FLASH_BYTE_ERASED; } - if (flashEraseSector(currentSectorAddr) != 0) { + if (tc4_flash_EraseSector(currentSectorAddr) != 0) { ret = -1; break; } @@ -680,7 +433,7 @@ int RAMFUNCTION hal_flash_erase(uint32_t address, int len) programCachedSector(currentSectorAddr); } else { - if (flashEraseSector(currentSectorAddr) != 0) { + if (tc4_flash_EraseSector(currentSectorAddr) != 0) { ret = -1; break; } @@ -716,7 +469,7 @@ int RAMFUNCTION ext_flash_read(uintptr_t address, uint8_t* data, int len) bytesInThisPage = len - bytesRead; } - erased = flashIsPageErased(pageAddress); + erased = tc4_flash_IsPageErased(pageAddress); if (erased < 0) { return -1; } @@ -728,7 +481,7 @@ int RAMFUNCTION ext_flash_read(uintptr_t address, uint8_t* data, int len) } } else { - flashRead(address, data + bytesRead, bytesInThisPage); + tc4_flash_Read(address, data + bytesRead, bytesInThisPage); } address += bytesInThisPage; @@ -749,7 +502,7 @@ int RAMFUNCTION hal_flash_is_erased(uint32_t address, int len) } for (page = GET_PAGE_ADDR(address); page < address + (uint32_t)len; page += TC4_PFLASH_PAGE_SIZE) { - int erased = flashIsPageErased(page); + int erased = tc4_flash_IsPageErased(page); if (erased != 1) { return erased; } @@ -787,23 +540,12 @@ volatile int g_tc4_hsmc_hw_rc; /* CPU0 hardware setup for HsmHost transport. */ static int tc4_hsmc_hw_init(void) { - { - IfxApApu_ApuConfig apConfig; - IfxApApu_initConfig(&apConfig); - const unsigned long srcTagMask = - (1UL << IfxApProt_TagId_cpu0d) | (1UL << IfxApProt_TagId_cpu0ds) | - (1UL << IfxApProt_TagId_cpu1d) | (1UL << IfxApProt_TagId_cpu1ds) | - (1UL << IfxApProt_TagId_cpucsd) | (1UL << IfxApProt_TagId_cpucsds); - apConfig.wraTagId = srcTagMask; - apConfig.rdaTagId = srcTagMask; - apConfig.wrbTagId = 0U; - apConfig.rdbTagId = 0U; - IfxSrc_configureAccessToSrcs(&apConfig); - } + const uint32_t srcTagMask = + TCHSM_APPROT_TAGS_FOR_CORE(0u) | TCHSM_APPROT_TAGS_FOR_CORE(1u) | + TCHSM_APPROT_TAGS_FOR_CORE(TC4_CORE_ID_CPUCS); - /* Probe SR0 to confirm SRC ACCEN writes are enabled. */ - SRC_GPSR4_SR0.U = 0x00006014u; - if (((SRC_GPSR4_SR0.U >> 12) & 0xFu) != 6u) { + if (tchsm_Notify_OpenSrcs(TCHSM_HSMHOST_CLIENT_APP0, srcTagMask) != + WH_ERROR_OK) { return -1; } diff --git a/test-app/Makefile b/test-app/Makefile index 7235faefec..40a8dbb918 100644 --- a/test-app/Makefile +++ b/test-app/Makefile @@ -1672,18 +1672,13 @@ ifneq (,$(filter aurix_tc4xx aurix_tc4xx_csrm,$(TARGET))) # Limit __WOLFBOOT to objects that define RAMFUNCTION helpers. hal_$(TARGET).o: CFLAGS+=-DRAM_CODE -D__WOLFBOOT ../test-app/libwolfboot.o: CFLAGS+=-DRAM_CODE -D__WOLFBOOT + $(TCHSM_WB_WOLFLLD)/src/tc4_flash.o: CFLAGS+=-DRAM_CODE -D__WOLFBOOT ifeq ($(TARGET),aurix_tc4xx_csrm) - # CSRM app uses the shared startup and driver objects. - APP_OBJS += $(TC4_LLD_SRCS:.c=.o) - APP_OBJS += $(TC4_WB_DIR)/csrm/Cfg_Ssw/Ifx_Cfg_Ssw.o + APP_OBJS += $(TCHSM_WB_CSRM_LLD_OBJS) # CSRM app linker script ships with the port. LSCRIPT_TEMPLATE = $(TC4_WB_DIR)/csrm/tc4cs_app.ld else - # Host test-app uses patched iLLD startup and drivers from the TC4 port. - # Drop Ifx_Ssw_Tc0.o and replace with patched aurix_tc4xx_ssw_tc0.o below - APP_OBJS += $(filter-out %/Ifx_Ssw_Tc0.o,$(TC4_LLD_SRCS:.c=.o)) - APP_OBJS += $(TC4_WB_DIR)/Cfg_Ssw/Ifx_Cfg_Ssw.o - APP_OBJS += aurix_tc4xx_ssw_tc0.o + APP_OBJS += $(TCHSM_WB_HOST_LLD_OBJS) # Host app linker script ships with the port. LSCRIPT_TEMPLATE = $(TC4_WB_DIR)/tc4tc_app.ld endif @@ -1694,14 +1689,6 @@ ifneq (,$(filter aurix_tc4xx aurix_tc4xx_csrm,$(TARGET))) $(WOLFBOOT_LIB_WOLFHSM)/src/wh_transport_mem.o endif -# Build Ifx_Ssw_Tc0.c a second time as aurix_tc4xx_ssw_tc0.o with the -# hypervisor exit compiled out, since wolfBoot already ran it before -# jumping to the app. The new name keeps it from conflicting with the -# bootloader's Ifx_Ssw_Tc0.o, which is built from the same file -aurix_tc4xx_ssw_tc0.o: $(TC4_LLD_DIR)/Infra/Ssw/TC4xx/Tricore/Ifx_Ssw_Tc0.c - @echo "\t[CC-APP] $@" - $(Q)$(CC) $(CFLAGS) -DIFX_CFG_SSW_SKIP_DISABLE_VIRTUALIZATION -c -o $@ $< - # Default goal for argument-less sub-makes. .DEFAULT_GOAL := image.bin endif diff --git a/test-app/app_aurix_tc4xx.c b/test-app/app_aurix_tc4xx.c index e80ed5edd9..0a3bff3d34 100644 --- a/test-app/app_aurix_tc4xx.c +++ b/test-app/app_aurix_tc4xx.c @@ -40,9 +40,6 @@ extern whClientContext hsmClientCtx; #define BASE_FW_VERSION 1 -/* Empty _init for the SSW C init hook. */ -void _init(void) {} - /* Hang on flash HAL faults. */ void wolfBoot_panic(void) { diff --git a/tools/scripts/tc4xx/wolfBoot-wolfHSM-keys.nvminit b/tools/scripts/tc4xx/wolfBoot-wolfHSM-keys.nvminit index 834c01192f..77b36c7c52 100644 --- a/tools/scripts/tc4xx/wolfBoot-wolfHSM-keys.nvminit +++ b/tools/scripts/tc4xx/wolfBoot-wolfHSM-keys.nvminit @@ -2,8 +2,9 @@ # # Key format # key