diff --git a/arch/arm/core/cortex_m/reset.S b/arch/arm/core/cortex_m/reset.S index b09391aaf0b59..85002c275f7c9 100644 --- a/arch/arm/core/cortex_m/reset.S +++ b/arch/arm/core/cortex_m/reset.S @@ -35,6 +35,10 @@ GTEXT(z_arm_init_arch_hw_at_boot) #if defined(CONFIG_PM_S2RAM) GTEXT(arch_pm_s2ram_resume) #endif +#if defined(CONFIG_SOC_EARLY_RESET_HOOK) +GTEXT(soc_early_reset_hook) +#endif + /* * PACBTI Mask for CONTROL register: @@ -100,6 +104,10 @@ SECTION_SUBSEC_FUNC(TEXT,_reset_section,__start) #endif /* CONFIG_INIT_ARCH_HW_AT_BOOT */ +#if defined(CONFIG_SOC_EARLY_RESET_HOOK) + /* Call custom code that executes before any stack is set up or RAM memory is accessed */ + bl soc_early_reset_hook +#endif #if defined(CONFIG_PM_S2RAM) /* * Temporarily set MSP to interrupt stack so that arch_pm_s2ram_resume can diff --git a/include/zephyr/platform/hooks.h b/include/zephyr/platform/hooks.h index dc84d3126112f..de810747ce05c 100644 --- a/include/zephyr/platform/hooks.h +++ b/include/zephyr/platform/hooks.h @@ -19,6 +19,19 @@ * directly from application code but may be freely used within the OS. */ +#ifdef CONFIG_SOC_EARLY_RESET_HOOK +/** + * @brief SoC hook executed before data RAM initialization, at the beginning + * of the reset vector. + * + * This hook is implemented by the SoC and can be used to perform any + * SoC-specific initialization. Refer to :kconfig:option:`SOC_EARLY_RESET_HOOK` + * and relevant architecture zephyr-rtos startup implementation for more details. + */ +void soc_early_reset_hook(void); +#else +#define soc_early_reset_hook() do { } while (0) +#endif #ifdef CONFIG_SOC_RESET_HOOK /** diff --git a/kernel/Kconfig.init b/kernel/Kconfig.init index 495381638fb08..5834330fe82b6 100644 --- a/kernel/Kconfig.init +++ b/kernel/Kconfig.init @@ -6,13 +6,40 @@ menu "SoC and Board Hooks" -config SOC_RESET_HOOK - bool "Run early SoC reset hook" +config SOC_EARLY_RESET_HOOK + bool "Run SoC-specific early reset hook" help - Run an early SoC reset hook. + Run a SoC-specific hook early in the reset/startup code (__start). + A custom hook soc_early_reset_hook() will be executed at the beginning + of the architecture-specific startup code, after hardware has been + configured (as required by CONFIG_INIT_ARCH_HW_AT_BOOT) but before + architecture's own resume logic. + + Returning from this hook is not mandatory: it can be used to implement + special logic to resume execution in some scenarios (for example, when + a bootloader is used). + + The stack pointer register should be considered as not initialized upon + call to this hook. In particular, this means that the hook is NOT allowed + to "push" any data using this stack pointer value. However, the hook may + use an implementation-specific area as stack if desired; in such case, + the original value of the stack pointer needs not to be "restored" before + returning control to the hook's caller. - A custom hook soc_reset_hook() is executed at the beginning of the - startup code (__start). soc_reset_hook() must be implemented by the SoC. + Additional constraints may be imposed on the hook by the architecture. + +config SOC_RESET_HOOK + bool "Run SoC-specific reset hook" + help + Run a SoC-specific hook in the reset/startup code (__start). + + A custom hook soc_reset_hook() will be executed near the beginning + of the architecture-specific startup code, after hardware has been + configured (as required by CONFIG_INIT_ARCH_HW_AT_BOOT), a stack + pointer has been loaded and the architecture's own resume logic + has executed (if CONFIG_PM_S2RAM is enabled). Because this hook + runs after the resume logic, it is not called when the system + resumes from a suspend-to-RAM power state. config SOC_PREP_HOOK bool "Run early SoC preparation hook"