2021-09-09 15:34:42 +08:00
/*
2024-01-02 11:16:55 +08:00
* SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD
2021-09-09 15:34:42 +08:00
*
* SPDX-License-Identifier: Apache-2.0
*/
2020-03-03 12:22:41 +08:00
# include <stdint.h>
# include <string.h>
# include <stdbool.h>
# include "esp_attr.h"
# include "esp_err.h"
# include "esp_log.h"
2022-01-12 12:23:47 +05:30
# include "esp_chip_info.h"
2024-10-22 18:21:40 +05:30
# include "esp_app_format.h"
2020-03-03 12:22:41 +08:00
2021-11-23 20:11:33 +08:00
# include "esp_private/cache_err_int.h"
2020-03-03 12:22:41 +08:00
# include "esp_clk_internal.h"
2021-01-26 21:07:22 +08:00
# include "esp_rom_uart.h"
2020-07-21 13:07:34 +08:00
# include "esp_rom_sys.h"
2022-07-04 14:17:54 +08:00
# include "esp_rom_caps.h"
2020-03-03 12:22:41 +08:00
# include "sdkconfig.h"
# if CONFIG_IDF_TARGET_ESP32
2020-11-26 19:56:13 +11:00
# include "soc/dport_reg.h"
2020-03-03 12:22:41 +08:00
# include "esp32/rom/cache.h"
2022-05-23 12:36:02 +08:00
# include "esp32/rom/secure_boot.h"
2020-03-03 12:22:41 +08:00
# elif CONFIG_IDF_TARGET_ESP32S2
# include "esp32s2/rom/cache.h"
2022-05-23 12:36:02 +08:00
# include "esp32s2/rom/secure_boot.h"
2020-03-03 12:22:41 +08:00
# include "esp32s2/memprot.h"
2020-07-29 13:13:51 +08:00
# elif CONFIG_IDF_TARGET_ESP32S3
# include "esp32s3/rom/cache.h"
2022-05-23 12:36:02 +08:00
# include "esp32s3/rom/secure_boot.h"
2021-09-06 10:22:40 +02:00
# include "esp_memprot.h"
2020-07-29 13:13:51 +08:00
# include "soc/assist_debug_reg.h"
2020-11-26 19:56:13 +11:00
# include "soc/system_reg.h"
2021-04-15 17:13:48 +08:00
# include "esp32s3/rom/opi_flash.h"
2020-11-26 19:56:13 +11:00
# elif CONFIG_IDF_TARGET_ESP32C3
2021-04-23 18:10:45 +08:00
# include "esp32c3/rom/cache.h"
2022-05-23 12:36:02 +08:00
# include "esp32c3/rom/secure_boot.h"
2021-09-06 10:22:40 +02:00
# include "esp_memprot.h"
2022-07-12 20:42:28 +08:00
# elif CONFIG_IDF_TARGET_ESP32C6
# include "esp32c6/rom/cache.h"
# include "esp_memprot.h"
2024-03-14 16:27:29 +08:00
# elif CONFIG_IDF_TARGET_ESP32C61
# include "esp_memprot.h"
2024-01-02 11:16:55 +08:00
# elif CONFIG_IDF_TARGET_ESP32C5
# include "esp32c5/rom/cache.h"
# include "esp_memprot.h"
2022-12-06 13:46:03 +08:00
# elif CONFIG_IDF_TARGET_ESP32H2
# include "esp32h2/rom/cache.h"
# include "esp_memprot.h"
2022-01-18 10:32:56 +08:00
# elif CONFIG_IDF_TARGET_ESP32C2
# include "esp32c2/rom/cache.h"
2022-05-23 12:36:02 +08:00
# include "esp32c2/rom/secure_boot.h"
2023-07-21 12:36:57 +08:00
# elif CONFIG_IDF_TARGET_ESP32P4
# include "soc/hp_sys_clkrst_reg.h"
2024-12-18 20:38:44 +08:00
# elif CONFIG_IDF_TARGET_ESP32H21
# include "esp_memprot.h"
2023-10-30 23:48:46 +05:30
# endif
2023-12-22 18:44:51 +08:00
# include "esp_private/rtc_clk.h"
2024-12-27 15:40:46 +08:00
# include "esp_rtc_time.h"
# include "rom/rtc.h"
2023-12-29 10:59:55 +08:00
# if SOC_INT_CLIC_SUPPORTED
# include "hal/interrupt_clic_ll.h"
# endif // SOC_INT_CLIC_SUPPORTED
2022-11-02 19:11:45 +08:00
# include "esp_private/esp_mmu_map_private.h"
2024-05-10 16:11:39 +08:00
# include "esp_private/image_process.h"
2022-05-11 10:32:56 +08:00
# if CONFIG_SPIRAM
# include "esp_psram.h"
2023-03-06 17:55:16 +08:00
# include "esp_private/mmu_psram_flash.h"
2022-05-11 10:32:56 +08:00
# include "esp_private/esp_psram_extram.h"
# endif
2021-09-09 15:34:42 +08:00
# include "esp_private/spi_flash_os.h"
2023-01-05 15:24:08 +08:00
# include "esp_private/mspi_timing_tuning.h"
2020-03-03 12:22:41 +08:00
# include "bootloader_flash_config.h"
2021-05-07 15:25:06 +08:00
# include "bootloader_flash.h"
2020-03-03 12:22:41 +08:00
# include "esp_private/crosscore_int.h"
2022-10-27 15:09:34 +08:00
# include "esp_private/sleep_gpio.h"
2020-07-27 19:50:23 +08:00
# include "hal/wdt_hal.h"
2020-07-29 13:13:51 +08:00
# include "soc/rtc.h"
2024-12-16 11:53:33 +08:00
# include "hal/cache_hal.h"
2023-03-09 03:03:53 +08:00
# include "hal/cache_ll.h"
2021-12-22 22:18:43 +08:00
# include "hal/efuse_ll.h"
2020-07-29 13:13:51 +08:00
# include "soc/periph_defs.h"
2021-12-14 10:08:15 +05:30
# include "esp_cpu.h"
2022-03-25 18:41:25 +08:00
# include "esp_private/esp_clk.h"
2020-03-03 12:22:41 +08:00
2021-10-22 23:32:36 +03:00
# if CONFIG_ESP32_TRAX || CONFIG_ESP32S2_TRAX || CONFIG_ESP32S3_TRAX
2021-11-23 20:11:33 +08:00
# include "esp_private/trax.h"
2020-11-06 15:00:07 +11:00
# endif
2020-03-03 12:22:41 +08:00
# include "bootloader_mem.h"
2023-01-30 18:03:41 +08:00
# if CONFIG_APP_BUILD_TYPE_RAM
2021-09-28 14:12:56 +08:00
# include "esp_rom_spiflash.h"
2023-01-30 18:03:41 +08:00
# include "bootloader_init.h"
2023-08-26 22:08:23 +08:00
# include "esp_private/bootloader_flash_internal.h"
2024-05-07 14:57:26 +05:30
# include "spi_flash_mmap.h"
2023-01-30 18:03:41 +08:00
# endif // CONFIG_APP_BUILD_TYPE_RAM
2020-03-03 12:22:41 +08:00
2022-02-11 15:30:54 +08:00
//This dependency will be removed in the future
# include "soc/ext_mem_defs.h"
2021-01-26 21:07:22 +08:00
2020-02-16 18:29:29 +05:00
# include "esp_private/startup_internal.h"
2020-06-23 16:46:06 +08:00
# include "esp_private/system_internal.h"
2020-02-13 17:43:23 +05:00
2024-02-12 09:51:25 +04:00
# if SOC_MEM_NON_CONTIGUOUS_SRAM
extern int _bss_start_low , _bss_start_high ;
extern int _bss_end_low , _bss_end_high ;
# else
2020-03-03 12:22:41 +08:00
extern int _bss_start ;
extern int _bss_end ;
2024-02-12 09:51:25 +04:00
# endif // SOC_MEM_NON_CONTIGUOUS_SRAM
2020-03-03 12:22:41 +08:00
extern int _rtc_bss_start ;
extern int _rtc_bss_end ;
2023-10-08 16:08:47 +08:00
# if CONFIG_BT_LE_RELEASE_IRAM_SUPPORTED
extern int _bss_bt_start ;
extern int _bss_bt_end ;
# endif // CONFIG_BT_LE_RELEASE_IRAM_SUPPORTED
2024-09-12 09:52:52 +08:00
/**
* If using `int`, then for CLANG, with enabled optimization when inlined function is provided with the address of external symbol, the two least bits of the constant used inside that function get cleared.
* Optimizer assumes that address of external symbol should be aligned to 4-bytes and therefore aligns constant value used for bitwise AND operation with that address.
*
* This means `extern int _instruction_reserved_start;` can be unaligned to 4 bytes, whereas using `char` can solve this issue.
*
* As we only use these symbol address, we declare them as `char` here
*/
extern char _instruction_reserved_start ;
extern char _instruction_reserved_end ;
extern char _rodata_reserved_start ;
extern char _rodata_reserved_end ;
2023-03-03 02:21:31 -08:00
2020-12-22 18:41:43 +11:00
extern int _vector_table ;
2023-07-21 11:54:29 +08:00
# if SOC_INT_CLIC_SUPPORTED
extern int _mtvt_table ;
# endif
2020-03-03 12:22:41 +08:00
static const char * TAG = " cpu_start " ;
# ifdef CONFIG_ESP32_IRAM_AS_8BIT_ACCESSIBLE_MEMORY
extern int _iram_bss_start ;
extern int _iram_bss_end ;
# endif
2020-02-13 17:43:23 +05:00
# if !CONFIG_ESP_SYSTEM_SINGLE_CORE_MODE
2020-02-05 22:40:15 +08:00
static volatile bool s_cpu_up [ SOC_CPU_CORES_NUM ] = { false } ;
static volatile bool s_cpu_inited [ SOC_CPU_CORES_NUM ] = { false } ;
2020-02-13 17:43:23 +05:00
2020-02-05 22:40:15 +08:00
static volatile bool s_resume_cores ;
2020-02-13 17:43:23 +05:00
# endif
2020-03-03 12:22:41 +08:00
2021-10-14 15:21:00 +05:30
static void core_intr_matrix_clear ( void )
2021-10-11 20:47:27 +05:30
{
2022-07-21 19:24:42 +08:00
uint32_t core_id = esp_cpu_get_core_id ( ) ;
2021-10-11 20:47:27 +05:30
for ( int i = 0 ; i < ETS_MAX_INTR_SOURCE ; i + + ) {
2023-12-29 10:59:55 +08:00
# if SOC_INT_CLIC_SUPPORTED
interrupt_clic_ll_route ( core_id , i , ETS_INVALID_INUM ) ;
2023-07-21 11:54:29 +08:00
# else
2022-01-17 17:44:25 +08:00
esp_rom_route_intr_matrix ( core_id , i , ETS_INVALID_INUM ) ;
2023-12-29 10:59:55 +08:00
# endif // SOC_INT_CLIC_SUPPORTED
2021-10-11 20:47:27 +05:30
}
2023-08-14 15:44:24 +08:00
# if SOC_INT_CLIC_SUPPORTED
for ( int i = 0 ; i < 32 ; i + + ) {
/* Set all the CPU interrupt lines to vectored by default, as it is on other RISC-V targets */
2023-12-29 10:59:55 +08:00
esprv_int_set_vectored ( i , true ) ;
2023-08-14 15:44:24 +08:00
}
# endif // SOC_INT_CLIC_SUPPORTED
2021-10-11 20:47:27 +05:30
}
2020-02-13 17:43:23 +05:00
# if !CONFIG_ESP_SYSTEM_SINGLE_CORE_MODE
2020-02-05 22:40:15 +08:00
void startup_resume_other_cores ( void )
{
s_resume_cores = true ;
}
2020-03-03 12:22:41 +08:00
void IRAM_ATTR call_start_cpu1 ( void )
{
2023-07-21 12:36:57 +08:00
# ifdef __riscv
// Configure the global pointer register
// (This should be the first thing IDF app does, as any other piece of code could be
// relaxed by the linker to access something relative to __global_pointer$)
2024-01-30 09:40:10 +08:00
__asm__ __volatile__ (
2023-07-21 12:36:57 +08:00
" .option push \n "
" .option norelax \n "
" la gp, __global_pointer$ \n "
" .option pop "
) ;
# endif //#ifdef __riscv
2023-07-21 11:51:38 +08:00
# if SOC_BRANCH_PREDICTOR_SUPPORTED
esp_cpu_branch_prediction_enable ( ) ;
# endif //#if SOC_BRANCH_PREDICTOR_SUPPORTED
2022-07-21 19:24:42 +08:00
esp_cpu_intr_set_ivt_addr ( & _vector_table ) ;
2023-07-21 11:54:29 +08:00
# if SOC_INT_CLIC_SUPPORTED
2023-09-01 17:18:39 +08:00
/* When hardware vectored interrupts are enabled in CLIC,
* the CPU jumps to this base address + 4 * interrupt_id.
*/
2023-07-21 11:54:29 +08:00
esp_cpu_intr_set_mtvt_addr ( & _mtvt_table ) ;
# endif
2020-03-03 12:22:41 +08:00
ets_set_appcpu_boot_addr ( 0 ) ;
bootloader_init_mem ( ) ;
2022-03-15 16:28:53 +08:00
# if CONFIG_ESP_CONSOLE_NONE
2020-07-29 13:13:51 +08:00
esp_rom_install_channel_putc ( 1 , NULL ) ;
esp_rom_install_channel_putc ( 2 , NULL ) ;
2024-02-05 20:03:40 +08:00
# elif !CONFIG_ESP_CONSOLE_USB_CDC
2020-12-02 11:43:13 +08:00
esp_rom_install_uart_printf ( ) ;
2024-01-17 17:19:49 +08:00
esp_rom_output_set_as_console ( CONFIG_ESP_CONSOLE_ROM_SERIAL_PORT_NUM ) ;
2020-03-03 12:22:41 +08:00
# endif
2020-07-29 13:13:51 +08:00
# if CONFIG_IDF_TARGET_ESP32
2020-02-05 22:40:15 +08:00
DPORT_REG_SET_BIT ( DPORT_APP_CPU_RECORD_CTRL_REG , DPORT_APP_CPU_PDEBUG_ENABLE | DPORT_APP_CPU_RECORD_ENABLE ) ;
DPORT_REG_CLR_BIT ( DPORT_APP_CPU_RECORD_CTRL_REG , DPORT_APP_CPU_RECORD_ENABLE ) ;
2023-07-21 12:36:57 +08:00
# elif CONFIG_IDF_TARGET_ESP32P4
2024-08-20 13:32:02 +08:00
REG_SET_BIT ( ASSIST_DEBUG_CORE_1_RCD_EN_REG , ASSIST_DEBUG_CORE_1_RCD_PDEBUGEN ) ;
REG_SET_BIT ( ASSIST_DEBUG_CORE_1_RCD_EN_REG , ASSIST_DEBUG_CORE_1_RCD_RECORDEN ) ;
2020-07-29 13:13:51 +08:00
# else
REG_WRITE ( ASSIST_DEBUG_CORE_1_RCD_PDEBUGENABLE_REG , 1 ) ;
REG_WRITE ( ASSIST_DEBUG_CORE_1_RCD_RECORDING_REG , 1 ) ;
# endif
2020-02-05 22:40:15 +08:00
s_cpu_up [ 1 ] = true ;
2023-09-15 23:10:41 +08:00
ESP_EARLY_LOGD ( TAG , " App cpu up " ) ;
2020-03-03 12:22:41 +08:00
2021-10-11 20:47:27 +05:30
// Clear interrupt matrix for APP CPU core
2021-10-14 15:21:00 +05:30
core_intr_matrix_clear ( ) ;
2021-10-11 20:47:27 +05:30
2023-01-30 18:03:41 +08:00
# if !CONFIG_APP_BUILD_TYPE_PURE_RAM_APP
2020-03-03 12:22:41 +08:00
//Take care putting stuff here: if asked, FreeRTOS will happily tell you the scheduler
//has started, but it isn't active *on this CPU* yet.
esp_cache_err_int_init ( ) ;
2023-01-30 18:03:41 +08:00
# endif
2020-03-03 12:22:41 +08:00
2021-10-22 23:32:36 +03:00
# if (CONFIG_IDF_TARGET_ESP32 && CONFIG_ESP32_TRAX_TWOBANKS) || \
(CONFIG_IDF_TARGET_ESP32S3 && CONFIG_ESP32S3_TRAX_TWOBANKS)
2020-03-03 12:22:41 +08:00
trax_start_trace ( TRAX_DOWNCOUNT_WORDS ) ;
# endif
2020-02-05 22:40:15 +08:00
s_cpu_inited [ 1 ] = true ;
2020-06-09 08:17:48 +08:00
while ( ! s_resume_cores ) {
2020-07-21 13:07:34 +08:00
esp_rom_delay_us ( 100 ) ;
2020-02-05 22:40:15 +08:00
}
SYS_STARTUP_FN ( ) ;
2020-03-03 12:22:41 +08:00
}
2020-02-13 17:43:23 +05:00
static void start_other_core ( void )
{
2020-10-05 18:50:09 +08:00
esp_chip_info_t chip_info ;
esp_chip_info ( & chip_info ) ;
// If not the single core variant of a target - check this since there is
2020-02-13 17:43:23 +05:00
// no separate soc_caps.h for the single core variant.
2020-10-05 18:50:09 +08:00
if ( ! ( chip_info . cores > 1 ) ) {
ESP_EARLY_LOGE ( TAG , " Running on single core variant of a chip, but app is built with multi-core support. " ) ;
ESP_EARLY_LOGE ( TAG , " Check that CONFIG_FREERTOS_UNICORE is enabled in menuconfig " ) ;
abort ( ) ;
}
2023-09-15 23:10:41 +08:00
ESP_EARLY_LOGD ( TAG , " Starting app cpu, entry point is %p " , call_start_cpu1 ) ;
2020-02-13 17:43:23 +05:00
2023-01-30 18:03:41 +08:00
# if CONFIG_IDF_TARGET_ESP32 && !CONFIG_APP_BUILD_TYPE_PURE_RAM_APP
2020-10-05 18:50:09 +08:00
Cache_Flush ( 1 ) ;
Cache_Read_Enable ( 1 ) ;
2023-01-30 18:03:41 +08:00
# endif // #if CONFIG_IDF_TARGET_ESP32 && !CONFIG_APP_BUILD_TYPE_PURE_RAM_APP
2020-02-13 17:43:23 +05:00
2020-10-05 18:50:09 +08:00
esp_cpu_unstall ( 1 ) ;
// Enable clock and reset APP CPU. Note that OpenOCD may have already
// enabled clock and taken APP CPU out of reset. In this case don't reset
// APP CPU again, as that will clear the breakpoints which may have already
// been set.
2020-07-29 13:13:51 +08:00
# if CONFIG_IDF_TARGET_ESP32
2020-10-05 18:50:09 +08:00
if ( ! DPORT_GET_PERI_REG_MASK ( DPORT_APPCPU_CTRL_B_REG , DPORT_APPCPU_CLKGATE_EN ) ) {
DPORT_SET_PERI_REG_MASK ( DPORT_APPCPU_CTRL_B_REG , DPORT_APPCPU_CLKGATE_EN ) ;
DPORT_CLEAR_PERI_REG_MASK ( DPORT_APPCPU_CTRL_C_REG , DPORT_APPCPU_RUNSTALL ) ;
DPORT_SET_PERI_REG_MASK ( DPORT_APPCPU_CTRL_A_REG , DPORT_APPCPU_RESETTING ) ;
DPORT_CLEAR_PERI_REG_MASK ( DPORT_APPCPU_CTRL_A_REG , DPORT_APPCPU_RESETTING ) ;
}
2020-07-29 13:13:51 +08:00
# elif CONFIG_IDF_TARGET_ESP32S3
2020-10-05 18:50:09 +08:00
if ( ! REG_GET_BIT ( SYSTEM_CORE_1_CONTROL_0_REG , SYSTEM_CONTROL_CORE_1_CLKGATE_EN ) ) {
REG_SET_BIT ( SYSTEM_CORE_1_CONTROL_0_REG , SYSTEM_CONTROL_CORE_1_CLKGATE_EN ) ;
REG_CLR_BIT ( SYSTEM_CORE_1_CONTROL_0_REG , SYSTEM_CONTROL_CORE_1_RUNSTALL ) ;
REG_SET_BIT ( SYSTEM_CORE_1_CONTROL_0_REG , SYSTEM_CONTROL_CORE_1_RESETING ) ;
REG_CLR_BIT ( SYSTEM_CORE_1_CONTROL_0_REG , SYSTEM_CONTROL_CORE_1_RESETING ) ;
}
2023-07-21 12:36:57 +08:00
# elif CONFIG_IDF_TARGET_ESP32P4
if ( ! REG_GET_BIT ( HP_SYS_CLKRST_SOC_CLK_CTRL0_REG , HP_SYS_CLKRST_REG_CORE1_CPU_CLK_EN ) ) {
REG_SET_BIT ( HP_SYS_CLKRST_SOC_CLK_CTRL0_REG , HP_SYS_CLKRST_REG_CORE1_CPU_CLK_EN ) ;
}
2024-01-30 09:40:10 +08:00
if ( REG_GET_BIT ( HP_SYS_CLKRST_HP_RST_EN0_REG , HP_SYS_CLKRST_REG_RST_EN_CORE1_GLOBAL ) ) {
2023-07-21 12:36:57 +08:00
REG_CLR_BIT ( HP_SYS_CLKRST_HP_RST_EN0_REG , HP_SYS_CLKRST_REG_RST_EN_CORE1_GLOBAL ) ;
}
2023-10-30 23:48:46 +05:30
# endif
2020-10-05 18:50:09 +08:00
ets_set_appcpu_boot_addr ( ( uint32_t ) call_start_cpu1 ) ;
2020-02-13 17:43:23 +05:00
2020-10-05 18:50:09 +08:00
bool cpus_up = false ;
2020-02-13 17:43:23 +05:00
2020-10-05 18:50:09 +08:00
while ( ! cpus_up ) {
cpus_up = true ;
for ( int i = 0 ; i < SOC_CPU_CORES_NUM ; i + + ) {
cpus_up & = s_cpu_up [ i ] ;
2020-02-13 17:43:23 +05:00
}
2020-10-05 18:50:09 +08:00
esp_rom_delay_us ( 100 ) ;
2020-02-13 17:43:23 +05:00
}
}
2023-03-09 03:03:53 +08:00
2023-09-18 23:37:31 +08:00
# if !SOC_CACHE_INTERNAL_MEM_VIA_L1CACHE
2024-08-10 16:53:51 +08:00
# if CONFIG_IDF_TARGET_ESP32
static void restore_app_mmu_from_pro_mmu ( void )
{
const int mmu_reg_num = 2048 ;
volatile uint32_t * from = ( uint32_t * ) DR_REG_FLASH_MMU_TABLE_PRO ;
volatile uint32_t * to = ( uint32_t * ) DR_REG_FLASH_MMU_TABLE_APP ;
for ( int i = 0 ; i < mmu_reg_num ; i + + ) {
* ( to + + ) = * ( from + + ) ;
}
}
# endif
2023-03-09 03:03:53 +08:00
// This function is needed to make the multicore app runnable on a unicore bootloader (built with FREERTOS UNICORE).
// It does some cache settings for other CPUs.
void IRAM_ATTR do_multicore_settings ( void )
{
// We intentionally do not check the cache settings before changing them,
// because it helps to get the application to run on older bootloaders.
# ifdef CONFIG_IDF_TARGET_ESP32
if ( ! efuse_ll_get_disable_app_cpu ( ) ) {
Cache_Read_Disable ( 1 ) ;
Cache_Flush ( 1 ) ;
DPORT_REG_SET_BIT ( DPORT_APP_CACHE_CTRL1_REG , DPORT_APP_CACHE_MMU_IA_CLR ) ;
2024-08-10 16:53:51 +08:00
mmu_init ( 1 ) ;
2023-03-09 03:03:53 +08:00
DPORT_REG_CLR_BIT ( DPORT_APP_CACHE_CTRL1_REG , DPORT_APP_CACHE_MMU_IA_CLR ) ;
// We do not enable cache for CPU1 now because it will be done later in start_other_core().
}
2024-08-10 16:53:51 +08:00
restore_app_mmu_from_pro_mmu ( ) ;
2023-03-09 03:03:53 +08:00
# endif
cache_bus_mask_t cache_bus_mask_core0 = cache_ll_l1_get_enabled_bus ( 0 ) ;
# ifndef CONFIG_IDF_TARGET_ESP32
// 1. disable the cache before changing its settings.
2023-09-15 20:11:52 +08:00
cache_hal_disable ( CACHE_LL_LEVEL_EXT_MEM , CACHE_TYPE_ALL ) ;
2023-03-09 03:03:53 +08:00
# endif
for ( unsigned core = 1 ; core < SOC_CPU_CORES_NUM ; core + + ) {
// 2. change cache settings. All cores must have the same settings.
cache_ll_l1_enable_bus ( core , cache_bus_mask_core0 ) ;
}
# ifndef CONFIG_IDF_TARGET_ESP32
// 3. enable the cache after changing its settings.
2023-09-15 20:11:52 +08:00
cache_hal_enable ( CACHE_LL_LEVEL_EXT_MEM , CACHE_TYPE_ALL ) ;
2023-03-09 03:03:53 +08:00
# endif
}
2023-09-18 23:37:31 +08:00
# endif // !SOC_CACHE_INTERNAL_MEM_VIA_L1CACHE
2020-02-13 17:43:23 +05:00
# endif // !CONFIG_ESP_SYSTEM_SINGLE_CORE_MODE
2020-03-03 12:22:41 +08:00
/*
* We arrive here after the bootloader finished loading the program from flash. The hardware is mostly uninitialized,
* and the app CPU is in reset. We do have a stack, so we can do the initialization in C.
*/
void IRAM_ATTR call_start_cpu0 ( void )
{
2020-02-13 17:43:23 +05:00
# if !CONFIG_ESP_SYSTEM_SINGLE_CORE_MODE
2021-07-13 10:45:06 +08:00
soc_reset_reason_t rst_reas [ SOC_CPU_CORES_NUM ] ;
2020-02-13 17:43:23 +05:00
# else
2021-11-06 17:24:45 +08:00
soc_reset_reason_t __attribute__ ( ( unused ) ) rst_reas [ 1 ] ;
2020-02-13 17:43:23 +05:00
# endif
2020-03-03 12:22:41 +08:00
2020-11-06 15:00:07 +11:00
# ifdef __riscv
2022-07-21 19:24:42 +08:00
if ( esp_cpu_dbgr_is_attached ( ) ) {
2021-05-29 00:20:02 +03:00
/* Let debugger some time to detect that target started, halt it, enable ebreaks and resume.
500ms should be enough. */
for ( uint32_t ms_num = 0 ; ms_num < 2 ; ms_num + + ) {
esp_rom_delay_us ( 100000 ) ;
}
}
2020-11-06 15:00:07 +11:00
// Configure the global pointer register
// (This should be the first thing IDF app does, as any other piece of code could be
// relaxed by the linker to access something relative to __global_pointer$)
2024-01-30 09:40:10 +08:00
__asm__ __volatile__ (
2021-01-04 19:38:10 +01:00
" .option push \n "
2020-11-06 15:00:07 +11:00
" .option norelax \n "
" la gp, __global_pointer$ \n "
" .option pop "
2021-01-04 19:38:10 +01:00
) ;
2020-11-06 15:00:07 +11:00
# endif
2023-07-21 11:51:38 +08:00
# if SOC_BRANCH_PREDICTOR_SUPPORTED
esp_cpu_branch_prediction_enable ( ) ;
# endif
2020-03-03 12:22:41 +08:00
// Move exception vectors to IRAM
2022-07-21 19:24:42 +08:00
esp_cpu_intr_set_ivt_addr ( & _vector_table ) ;
2023-07-21 11:54:29 +08:00
# if SOC_INT_CLIC_SUPPORTED
2023-09-01 17:18:39 +08:00
/* When hardware vectored interrupts are enabled in CLIC,
* the CPU jumps to this base address + 4 * interrupt_id.
*/
2023-07-21 11:54:29 +08:00
esp_cpu_intr_set_mtvt_addr ( & _mtvt_table ) ;
# endif
2020-03-03 12:22:41 +08:00
2024-07-01 14:42:08 +05:30
/* NOTE: When ESP-TEE is enabled, this sets up the callback function
* which redirects all the interrupt management for the REE (user app)
* to the TEE by raising the appropriate service calls.
*/
# if CONFIG_SECURE_ENABLE_TEE
extern uint32_t esp_tee_service_call ( int argc , . . . ) ;
esprv_int_setup_mgmt_cb ( ( void * ) esp_tee_service_call ) ;
# endif
2021-07-13 10:45:06 +08:00
rst_reas [ 0 ] = esp_rom_get_reset_reason ( 0 ) ;
2020-02-13 17:43:23 +05:00
# if !CONFIG_ESP_SYSTEM_SINGLE_CORE_MODE
2021-07-13 10:45:06 +08:00
rst_reas [ 1 ] = esp_rom_get_reset_reason ( 1 ) ;
2020-03-03 12:22:41 +08:00
# endif
2023-01-30 18:03:41 +08:00
//Clear BSS. Please do not attempt to do any complex stuff (like early logging) before this.
2024-02-12 09:51:25 +04:00
# if SOC_MEM_NON_CONTIGUOUS_SRAM
memset ( & _bss_start_low , 0 , ( & _bss_end_low - & _bss_start_low ) * sizeof ( _bss_start_low ) ) ;
memset ( & _bss_start_high , 0 , ( & _bss_end_high - & _bss_start_high ) * sizeof ( _bss_start_high ) ) ;
# else
2023-01-30 18:03:41 +08:00
memset ( & _bss_start , 0 , ( & _bss_end - & _bss_start ) * sizeof ( _bss_start ) ) ;
2024-02-12 09:51:25 +04:00
# endif // SOC_MEM_NON_CONTIGUOUS_SRAM
2023-01-30 18:03:41 +08:00
2023-10-08 16:08:47 +08:00
# if CONFIG_BT_LE_RELEASE_IRAM_SUPPORTED
// Clear Bluetooth bss
memset ( & _bss_bt_start , 0 , ( & _bss_bt_end - & _bss_bt_start ) * sizeof ( _bss_bt_start ) ) ;
# endif // CONFIG_BT_LE_RELEASE_IRAM_SUPPORTED
2023-01-30 18:03:41 +08:00
# if defined(CONFIG_IDF_TARGET_ESP32) && defined(CONFIG_ESP32_IRAM_AS_8BIT_ACCESSIBLE_MEMORY)
// Clear IRAM BSS
memset ( & _iram_bss_start , 0 , ( & _iram_bss_end - & _iram_bss_start ) * sizeof ( _iram_bss_start ) ) ;
# endif
# if SOC_RTC_FAST_MEM_SUPPORTED || SOC_RTC_SLOW_MEM_SUPPORTED
/* Unless waking from deep sleep (implying RTC memory is intact), clear RTC bss */
if ( rst_reas [ 0 ] ! = RESET_REASON_CORE_DEEP_SLEEP ) {
memset ( & _rtc_bss_start , 0 , ( & _rtc_bss_end - & _rtc_bss_start ) * sizeof ( _rtc_bss_start ) ) ;
}
# endif
2023-11-03 12:05:11 +08:00
# if !CONFIG_APP_BUILD_TYPE_PURE_RAM_APP && !CONFIG_ESP_SYSTEM_SINGLE_CORE_MODE && !SOC_CACHE_INTERNAL_MEM_VIA_L1CACHE
2023-03-09 03:03:53 +08:00
// It helps to fix missed cache settings for other cores. It happens when bootloader is unicore.
do_multicore_settings ( ) ;
# endif
2024-12-16 11:53:33 +08:00
# if !CONFIG_APP_BUILD_TYPE_PURE_RAM_APP
//cache hal ctx needs to be initialised
cache_hal_init ( ) ;
# endif
2023-01-30 18:03:41 +08:00
// When the APP is loaded into ram for execution, some hardware initialization behaviors
// in the bootloader are still necessary
# if CONFIG_APP_BUILD_TYPE_RAM
2023-08-26 22:08:23 +08:00
bootloader_init ( ) ;
2023-03-15 10:46:13 +08:00
# if !CONFIG_APP_BUILD_TYPE_PURE_RAM_APP
2023-08-26 22:08:23 +08:00
bootloader_flash_hardware_init ( ) ;
2023-03-15 10:46:13 +08:00
# endif //#if !CONFIG_APP_BUILD_TYPE_PURE_RAM_APP
# endif //#if CONFIG_APP_BUILD_TYPE_RAM
2023-01-30 18:03:41 +08:00
2024-01-19 09:53:56 +08:00
# if CONFIG_IDF_TARGET_ESP32P4
# define RWDT_RESET RESET_REASON_CORE_RWDT
# define MWDT_RESET RESET_REASON_CORE_MWDT
# else
# define RWDT_RESET RESET_REASON_CORE_RTC_WDT
# define MWDT_RESET RESET_REASON_CORE_MWDT0
# endif
2020-02-05 22:40:15 +08:00
# ifndef CONFIG_BOOTLOADER_WDT_ENABLE
2020-03-03 12:22:41 +08:00
// from panic handler we can be reset by RWDT or TG0WDT
2024-01-19 09:53:56 +08:00
if ( rst_reas [ 0 ] = = RWDT_RESET | | rst_reas [ 0 ] = = MWDT_RESET
2020-02-13 17:43:23 +05:00
# if !CONFIG_ESP_SYSTEM_SINGLE_CORE_MODE
2024-01-19 09:53:56 +08:00
| | rst_reas [ 1 ] = = RWDT_RESET | | rst_reas [ 1 ] = = MWDT_RESET
2020-03-03 12:22:41 +08:00
# endif
2020-07-13 21:33:23 +08:00
) {
2023-02-15 11:54:00 +08:00
wdt_hal_context_t rtc_wdt_ctx = RWDT_HAL_CONTEXT_DEFAULT ( ) ;
2020-03-03 12:22:41 +08:00
wdt_hal_write_protect_disable ( & rtc_wdt_ctx ) ;
wdt_hal_disable ( & rtc_wdt_ctx ) ;
wdt_hal_write_protect_enable ( & rtc_wdt_ctx ) ;
}
2020-02-05 22:40:15 +08:00
# endif
2020-03-03 12:22:41 +08:00
2023-01-30 18:03:41 +08:00
# if !CONFIG_APP_BUILD_TYPE_PURE_RAM_APP
2020-03-03 12:22:41 +08:00
# if CONFIG_IDF_TARGET_ESP32S2
/* Configure the mode of instruction cache : cache size, cache associated ways, cache line size. */
extern void esp_config_instruction_cache_mode ( void ) ;
esp_config_instruction_cache_mode ( ) ;
/* If we need use SPIRAM, we should use data cache, or if we want to access rodata, we also should use data cache.
Configure the mode of data : cache size, cache associated ways, cache line size.
Enable data cache, so if we don't use SPIRAM, it just works. */
extern void esp_config_data_cache_mode ( void ) ;
esp_config_data_cache_mode ( ) ;
Cache_Enable_DCache ( 0 ) ;
# endif
2020-07-29 13:13:51 +08:00
# if CONFIG_IDF_TARGET_ESP32S3
/* Configure the mode of instruction cache : cache size, cache line size. */
extern void rom_config_instruction_cache_mode ( uint32_t cfg_cache_size , uint8_t cfg_cache_ways , uint8_t cfg_cache_line_size ) ;
rom_config_instruction_cache_mode ( CONFIG_ESP32S3_INSTRUCTION_CACHE_SIZE , CONFIG_ESP32S3_ICACHE_ASSOCIATED_WAYS , CONFIG_ESP32S3_INSTRUCTION_CACHE_LINE_SIZE ) ;
/* If we need use SPIRAM, we should use data cache.
Configure the mode of data : cache size, cache line size.*/
Cache_Suspend_DCache ( ) ;
extern void rom_config_data_cache_mode ( uint32_t cfg_cache_size , uint8_t cfg_cache_ways , uint8_t cfg_cache_line_size ) ;
rom_config_data_cache_mode ( CONFIG_ESP32S3_DATA_CACHE_SIZE , CONFIG_ESP32S3_DCACHE_ASSOCIATED_WAYS , CONFIG_ESP32S3_DATA_CACHE_LINE_SIZE ) ;
Cache_Resume_DCache ( 0 ) ;
2020-12-22 18:38:37 +11:00
# endif // CONFIG_IDF_TARGET_ESP32S3
2020-08-28 11:53:28 +08:00
2023-07-21 12:37:06 +08:00
# if CONFIG_IDF_TARGET_ESP32P4
2023-09-14 12:14:08 +08:00
//TODO: IDF-5670, add cache init API
2023-07-27 15:10:50 +08:00
extern void esp_config_l2_cache_mode ( void ) ;
esp_config_l2_cache_mode ( ) ;
2023-07-21 12:37:06 +08:00
# endif
2021-08-11 19:06:48 +05:00
2023-08-31 12:45:08 +08:00
# if ESP_ROM_NEEDS_SET_CACHE_MMU_SIZE
2023-03-17 09:41:54 +05:30
# if CONFIG_APP_BUILD_TYPE_ELF_RAM
// For RAM loadable ELF case, we don't need to reserve IROM/DROM as instructions and data
// are all in internal RAM. If the RAM loadable ELF has any requirement to memory map the
// external flash then it should use flash or partition mmap APIs.
uint32_t cache_mmu_irom_size = 0 ;
__attribute__ ( ( unused ) ) uint32_t cache_mmu_drom_size = 0 ;
# else // CONFIG_APP_BUILD_TYPE_ELF_RAM
2023-03-03 02:21:31 -08:00
uint32_t _instruction_size = ( uint32_t ) & _instruction_reserved_end - ( uint32_t ) & _instruction_reserved_start ;
uint32_t cache_mmu_irom_size = ( ( _instruction_size + SPI_FLASH_MMU_PAGE_SIZE - 1 ) / SPI_FLASH_MMU_PAGE_SIZE ) * sizeof ( uint32_t ) ;
2020-10-10 16:22:49 +08:00
2023-03-06 17:20:56 +08:00
uint32_t _rodata_size = ( uint32_t ) & _rodata_reserved_end - ( uint32_t ) & _rodata_reserved_start ;
2023-03-06 17:55:16 +08:00
__attribute__ ( ( unused ) ) uint32_t cache_mmu_drom_size = ( ( _rodata_size + SPI_FLASH_MMU_PAGE_SIZE - 1 ) / SPI_FLASH_MMU_PAGE_SIZE ) * sizeof ( uint32_t ) ;
2023-03-17 09:41:54 +05:30
# endif // !CONFIG_APP_BUILD_TYPE_ELF_RAM
2020-09-28 10:19:56 +08:00
2023-03-03 02:21:31 -08:00
/* Configure the Cache MMU size for instruction and rodata in flash. */
2020-08-28 11:53:28 +08:00
Cache_Set_IDROM_MMU_Size ( cache_mmu_irom_size , CACHE_DROM_MMU_MAX_END - cache_mmu_irom_size ) ;
2023-08-31 12:45:08 +08:00
# endif // ESP_ROM_NEEDS_SET_CACHE_MMU_SIZE
2020-07-29 13:13:51 +08:00
2022-09-23 17:32:16 +08:00
# if CONFIG_ESPTOOLPY_OCT_FLASH && !CONFIG_ESPTOOLPY_FLASH_MODE_AUTO_DETECT
2021-12-22 22:18:43 +08:00
bool efuse_opflash_en = efuse_ll_get_flash_type ( ) ;
2021-04-15 17:13:48 +08:00
if ( ! efuse_opflash_en ) {
2024-05-10 16:11:39 +08:00
ESP_DRAM_LOGE ( TAG , " Octal Flash option selected, but EFUSE not configured! " ) ;
2021-04-15 17:13:48 +08:00
abort ( ) ;
}
2021-08-13 11:30:54 +08:00
# endif
2023-12-22 18:44:51 +08:00
2021-09-09 15:34:42 +08:00
esp_mspi_pin_init ( ) ;
// For Octal flash, it's hard to implement a read_id function in OPI mode for all vendors.
// So we have to read it here in SPI mode, before entering the OPI mode.
bootloader_flash_update_id ( ) ;
2024-05-30 18:54:35 +08:00
// Configure the power related stuff. After this the MSPI timing tuning can be done.
esp_rtc_init ( ) ;
2021-09-09 15:34:42 +08:00
/**
* This function initialise the Flash chip to the user-defined settings.
*
* In bootloader, we only init Flash (and MSPI) to a preliminary state, for being flexible to
* different chips.
* In this stage, we re-configure the Flash (and MSPI) to required configuration
*/
spi_flash_init_chip_state ( ) ;
2024-11-19 11:32:59 +08:00
# if SOC_MEMSPI_SRC_FREQ_120M_SUPPORTED
2024-05-30 18:54:35 +08:00
// This function needs to be called when PLL is enabled. Needs to be called after spi_flash_init_chip_state in case
// some state of flash is modified.
2023-01-05 15:24:08 +08:00
mspi_timing_flash_tuning ( ) ;
2021-04-15 17:13:48 +08:00
# endif
2022-11-02 19:11:45 +08:00
esp_mmu_map_init ( ) ;
2024-05-10 16:11:39 +08:00
# if !CONFIG_APP_BUILD_TYPE_ELF_RAM
# if CONFIG_SPIRAM_FLASH_LOAD_TO_PSRAM
ESP_ERROR_CHECK ( image_process ( ) ) ;
# endif
# endif
2020-03-03 12:22:41 +08:00
# if CONFIG_SPIRAM_BOOT_INIT
2022-05-11 10:32:56 +08:00
if ( esp_psram_init ( ) ! = ESP_OK ) {
2020-03-03 12:22:41 +08:00
# if CONFIG_SPIRAM_ALLOW_BSS_SEG_EXTERNAL_MEMORY
2024-05-10 16:11:39 +08:00
ESP_DRAM_LOGE ( TAG , " Failed to init external RAM, needed for external .bss segment " ) ;
2020-03-03 12:22:41 +08:00
abort ( ) ;
# endif
# if CONFIG_SPIRAM_IGNORE_NOTFOUND
ESP_EARLY_LOGI ( TAG , " Failed to init external RAM; continuing without it. " ) ;
# else
2024-05-10 16:11:39 +08:00
ESP_DRAM_LOGE ( TAG , " Failed to init external RAM! " ) ;
2020-03-03 12:22:41 +08:00
abort ( ) ;
# endif
}
# endif
2023-11-03 12:14:06 +08:00
//----------------------------------Separator-----------------------------//
/**
* @note
2024-05-10 16:11:39 +08:00
* After this stage, you can access the flash through the cache, i.e. run code which is not placed in IRAM
* or print string which locates on flash
2023-11-03 12:14:06 +08:00
*/
2024-05-10 16:11:39 +08:00
esp_mspi_pin_reserve ( ) ;
2023-11-03 12:14:06 +08:00
2023-01-30 18:03:41 +08:00
# endif // !CONFIG_APP_BUILD_TYPE_PURE_RAM_APP
2023-11-03 12:05:11 +08:00
# if CONFIG_ESP_SYSTEM_SINGLE_CORE_MODE
ESP_EARLY_LOGI ( TAG , " Unicore app " ) ;
# else
ESP_EARLY_LOGI ( TAG , " Multicore app " ) ;
# endif
2024-07-01 14:42:08 +05:30
/* NOTE: When ESP-TEE is enabled, it configures its own memory protection
* scheme using the CPU-inherent features PMP and PMA and the APM peripheral.
*/
# if !CONFIG_SECURE_ENABLE_TEE
2023-01-30 18:03:41 +08:00
bootloader_init_mem ( ) ;
2024-07-01 14:42:08 +05:30
# endif
2020-03-03 12:22:41 +08:00
2020-02-13 17:43:23 +05:00
# if !CONFIG_ESP_SYSTEM_SINGLE_CORE_MODE
2020-02-05 22:40:15 +08:00
s_cpu_up [ 0 ] = true ;
2020-08-14 23:42:57 +08:00
# endif
2020-07-29 13:13:51 +08:00
2023-09-15 23:10:41 +08:00
ESP_EARLY_LOGD ( TAG , " Pro cpu up " ) ;
2020-03-03 12:22:41 +08:00
2020-06-17 17:13:55 +08:00
# if SOC_CPU_CORES_NUM > 1 // there is no 'single-core mode' for natively single-core processors
2020-02-13 17:43:23 +05:00
# if !CONFIG_ESP_SYSTEM_SINGLE_CORE_MODE
start_other_core ( ) ;
2020-06-17 17:13:55 +08:00
# else
ESP_EARLY_LOGI ( TAG , " Single core mode " ) ;
2020-07-29 13:13:51 +08:00
# if CONFIG_IDF_TARGET_ESP32
2020-06-17 17:13:55 +08:00
DPORT_CLEAR_PERI_REG_MASK ( DPORT_APPCPU_CTRL_B_REG , DPORT_APPCPU_CLKGATE_EN ) ; // stop the other core
2020-07-29 13:13:51 +08:00
# elif CONFIG_IDF_TARGET_ESP32S3
REG_CLR_BIT ( SYSTEM_CORE_1_CONTROL_0_REG , SYSTEM_CONTROL_CORE_1_CLKGATE_EN ) ;
2021-06-30 20:33:07 +08:00
# if SOC_APPCPU_HAS_CLOCK_GATING_BUG
2024-01-19 09:53:56 +08:00
/* The clock gating signal of the App core is invalid. We use RUNSTALL and RESETTING
2021-06-30 20:33:07 +08:00
signals to ensure that the App core stops running in single-core mode. */
REG_SET_BIT ( SYSTEM_CORE_1_CONTROL_0_REG , SYSTEM_CONTROL_CORE_1_RUNSTALL ) ;
REG_CLR_BIT ( SYSTEM_CORE_1_CONTROL_0_REG , SYSTEM_CONTROL_CORE_1_RESETING ) ;
2020-07-29 13:13:51 +08:00
# endif
2024-11-07 13:46:10 +01:00
# elif CONFIG_IDF_TARGET_ESP32P4
REG_CLR_BIT ( HP_SYS_CLKRST_SOC_CLK_CTRL0_REG , HP_SYS_CLKRST_REG_CORE1_CPU_CLK_EN ) ;
REG_SET_BIT ( HP_SYS_CLKRST_HP_RST_EN0_REG , HP_SYS_CLKRST_REG_RST_EN_CORE1_GLOBAL ) ;
2021-06-30 20:33:07 +08:00
# endif // CONFIG_IDF_TARGET_ESP32
2020-06-17 17:13:55 +08:00
# endif // !CONFIG_ESP_SYSTEM_SINGLE_CORE_MODE
# endif // SOC_CPU_CORES_NUM > 1
2020-03-03 12:22:41 +08:00
# if CONFIG_SPIRAM_MEMTEST
2022-05-26 22:48:49 +08:00
if ( esp_psram_is_initialized ( ) ) {
2022-05-11 10:32:56 +08:00
bool ext_ram_ok = esp_psram_extram_test ( ) ;
2020-03-03 12:22:41 +08:00
if ( ! ext_ram_ok ) {
ESP_EARLY_LOGE ( TAG , " External RAM failed memory test! " ) ;
abort ( ) ;
}
}
2022-01-12 15:03:50 +08:00
# endif //CONFIG_SPIRAM_MEMTEST
2020-03-03 12:22:41 +08:00
2023-01-30 18:03:41 +08:00
# if !CONFIG_APP_BUILD_TYPE_PURE_RAM_APP
2022-05-11 10:32:56 +08:00
//TODO: IDF-5023, replace with MMU driver
2022-01-12 15:03:50 +08:00
# if CONFIG_IDF_TARGET_ESP32S3
2020-10-10 16:22:49 +08:00
int s_instr_flash2spiram_off = 0 ;
int s_rodata_flash2spiram_off = 0 ;
# if CONFIG_SPIRAM_FETCH_INSTRUCTIONS
s_instr_flash2spiram_off = instruction_flash2spiram_offset ( ) ;
# endif
# if CONFIG_SPIRAM_RODATA
s_rodata_flash2spiram_off = rodata_flash2spiram_offset ( ) ;
# endif
2021-09-06 10:22:40 +02:00
Cache_Set_IDROM_MMU_Info ( cache_mmu_irom_size / sizeof ( uint32_t ) , \
cache_mmu_drom_size / sizeof ( uint32_t ) , \
( uint32_t ) & _rodata_reserved_start , \
( uint32_t ) & _rodata_reserved_end , \
s_instr_flash2spiram_off , \
s_rodata_flash2spiram_off ) ;
2023-03-06 17:20:56 +08:00
# endif // CONFIG_IDF_TARGET_ESP32S3
2020-03-03 12:22:41 +08:00
2020-10-10 16:22:49 +08:00
# if CONFIG_ESP32S2_INSTRUCTION_CACHE_WRAP || CONFIG_ESP32S2_DATA_CACHE_WRAP || \
CONFIG_ESP32S3_INSTRUCTION_CACHE_WRAP || CONFIG_ESP32S3_DATA_CACHE_WRAP
2020-03-03 12:22:41 +08:00
uint32_t icache_wrap_enable = 0 , dcache_wrap_enable = 0 ;
2020-10-10 16:22:49 +08:00
# if CONFIG_ESP32S2_INSTRUCTION_CACHE_WRAP || CONFIG_ESP32S3_INSTRUCTION_CACHE_WRAP
2020-03-03 12:22:41 +08:00
icache_wrap_enable = 1 ;
# endif
2020-10-10 16:22:49 +08:00
# if CONFIG_ESP32S2_DATA_CACHE_WRAP || CONFIG_ESP32S3_DATA_CACHE_WRAP
2020-03-03 12:22:41 +08:00
dcache_wrap_enable = 1 ;
# endif
extern void esp_enable_cache_wrap ( uint32_t icache_wrap_enable , uint32_t dcache_wrap_enable ) ;
esp_enable_cache_wrap ( icache_wrap_enable , dcache_wrap_enable ) ;
# endif
2020-10-10 16:22:49 +08:00
# if CONFIG_ESP32S3_DATA_CACHE_16KB
Cache_Invalidate_DCache_All ( ) ;
Cache_Occupy_Addr ( SOC_DROM_LOW , 0x4000 ) ;
# endif
2022-01-18 10:32:56 +08:00
# if CONFIG_IDF_TARGET_ESP32C2
2022-07-12 20:42:28 +08:00
// TODO : IDF-5020
2022-01-18 10:32:56 +08:00
# if CONFIG_ESP32C2_INSTRUCTION_CACHE_WRAP
2021-11-06 17:24:45 +08:00
extern void esp_enable_cache_wrap ( uint32_t icache_wrap_enable ) ;
esp_enable_cache_wrap ( 1 ) ;
# endif
# endif
2023-01-30 18:03:41 +08:00
# endif // !CONFIG_APP_BUILD_TYPE_PURE_RAM_APP
2021-11-06 17:24:45 +08:00
2020-03-03 12:22:41 +08:00
# if CONFIG_SPIRAM_ALLOW_BSS_SEG_EXTERNAL_MEMORY
2024-07-10 10:01:22 +08:00
esp_psram_bss_init ( ) ;
2020-03-03 12:22:41 +08:00
# endif
//Enable trace memory and immediately start trace.
2021-10-22 23:32:36 +03:00
# if CONFIG_ESP32_TRAX || CONFIG_ESP32S2_TRAX || CONFIG_ESP32S3_TRAX
# if CONFIG_IDF_TARGET_ESP32 || CONFIG_IDF_TARGET_ESP32S3
# if CONFIG_ESP32_TRAX_TWOBANKS || CONFIG_ESP32S3_TRAX_TWOBANKS
2020-07-13 21:33:23 +08:00
trax_enable ( TRAX_ENA_PRO_APP ) ;
# else
trax_enable ( TRAX_ENA_PRO ) ;
# endif
2020-02-16 18:29:29 +05:00
# elif CONFIG_IDF_TARGET_ESP32S2
2020-03-03 12:22:41 +08:00
trax_enable ( TRAX_ENA_PRO ) ;
# endif
trax_start_trace ( TRAX_DOWNCOUNT_WORDS ) ;
2021-10-22 23:32:36 +03:00
# endif // CONFIG_ESP32_TRAX || CONFIG_ESP32S2_TRAX || CONFIG_ESP32S3_TRAX
2020-03-03 12:22:41 +08:00
esp_clk_init ( ) ;
esp_perip_clk_init ( ) ;
2020-06-23 16:46:06 +08:00
// Now that the clocks have been set-up, set the startup time from RTC
// and default RTC-backed system time provider.
g_startup_time = esp_rtc_get_time_us ( ) ;
2021-10-11 20:47:27 +05:30
// Clear interrupt matrix for PRO CPU core
2021-10-14 15:21:00 +05:30
core_intr_matrix_clear ( ) ;
2020-03-03 12:22:41 +08:00
2020-05-01 18:09:20 +10:00
# ifndef CONFIG_IDF_ENV_FPGA // TODO: on FPGA it should be possible to configure this, not currently working with APB_CLK_FREQ changed
2020-04-30 16:48:49 +02:00
# ifdef CONFIG_ESP_CONSOLE_UART
2022-03-25 18:41:25 +08:00
uint32_t clock_hz = esp_clk_apb_freq ( ) ;
2022-07-04 14:17:54 +08:00
# if ESP_ROM_UART_CLK_IS_XTAL
clock_hz = esp_clk_xtal_freq ( ) ; // From esp32-s3 on, UART clock source is selected to XTAL in ROM
2020-07-29 13:13:51 +08:00
# endif
2024-01-17 17:19:49 +08:00
esp_rom_output_tx_wait_idle ( CONFIG_ESP_CONSOLE_ROM_SERIAL_PORT_NUM ) ;
2023-09-25 11:12:09 +08:00
// In a single thread mode, the freertos is not started yet. So don't have to use a critical section.
2024-01-30 09:40:10 +08:00
int __DECLARE_RCC_ATOMIC_ENV __attribute__ ( ( unused ) ) ; // To avoid build errors about spinlock's __DECLARE_RCC_ATOMIC_ENV
2024-01-17 17:19:49 +08:00
esp_rom_uart_set_clock_baudrate ( CONFIG_ESP_CONSOLE_ROM_SERIAL_PORT_NUM , clock_hz , CONFIG_ESP_CONSOLE_UART_BAUDRATE ) ;
2020-03-03 12:22:41 +08:00
# endif
2020-05-01 18:09:20 +10:00
# endif
2020-03-03 12:22:41 +08:00
2024-09-13 20:22:11 +08:00
# if SOC_DEEP_SLEEP_SUPPORTED
2022-10-27 15:09:34 +08:00
// Need to unhold the IOs that were hold right before entering deep sleep, which are used as wakeup pins
if ( rst_reas [ 0 ] = = RESET_REASON_CORE_DEEP_SLEEP ) {
esp_deep_sleep_wakeup_io_reset ( ) ;
}
2024-03-14 16:27:29 +08:00
# endif //#if SOC_DEEP_SLEEP_SUPPORTED
2020-03-03 12:22:41 +08:00
2023-01-30 18:03:41 +08:00
# if !CONFIG_APP_BUILD_TYPE_PURE_RAM_APP
2020-03-03 12:22:41 +08:00
esp_cache_err_int_init ( ) ;
2023-01-30 18:03:41 +08:00
# endif
2020-03-03 12:22:41 +08:00
2021-09-06 10:22:40 +02:00
# if CONFIG_ESP_SYSTEM_MEMPROT_FEATURE && !CONFIG_ESP_SYSTEM_MEMPROT_TEST
2021-06-01 00:07:09 +02:00
// Memprot cannot be locked during OS startup as the lock-on prevents any PMS changes until a next reboot
// If such a situation appears, it is likely an malicious attempt to bypass the system safety setup -> print error & reset
2021-09-06 10:22:40 +02:00
# if CONFIG_IDF_TARGET_ESP32S2
2021-08-18 13:31:35 +02:00
if ( esp_memprot_is_locked_any ( ) ) {
2021-09-06 10:22:40 +02:00
# else
bool is_locked = false ;
if ( esp_mprot_is_conf_locked_any ( & is_locked ) ! = ESP_OK | | is_locked ) {
# endif
2021-06-01 00:07:09 +02:00
ESP_EARLY_LOGE ( TAG , " Memprot feature locked after the system reset! Potential safety corruption, rebooting. " ) ;
2023-04-25 10:28:10 +02:00
esp_restart_noos ( ) ;
2021-06-01 00:07:09 +02:00
}
2021-09-06 10:22:40 +02:00
//default configuration of PMS Memprot
2021-08-18 13:31:35 +02:00
esp_err_t memp_err = ESP_OK ;
# if CONFIG_IDF_TARGET_ESP32S2 //specific for ESP32S2 unless IDF-3024 is merged
2021-09-06 10:22:40 +02:00
# if CONFIG_ESP_SYSTEM_MEMPROT_FEATURE_LOCK
memp_err = esp_memprot_set_prot ( PANIC_HNDL_ON , MEMPROT_LOCK , NULL ) ;
2020-02-05 22:40:15 +08:00
# else
2021-09-06 10:22:40 +02:00
memp_err = esp_memprot_set_prot ( PANIC_HNDL_ON , MEMPROT_UNLOCK , NULL ) ;
2020-02-05 22:40:15 +08:00
# endif
2021-09-06 10:22:40 +02:00
# else //CONFIG_IDF_TARGET_ESP32S2 specific end
esp_memp_config_t memp_cfg = ESP_MEMPROT_DEFAULT_CONFIG ( ) ;
# if !CONFIG_ESP_SYSTEM_MEMPROT_FEATURE_LOCK
memp_cfg . lock_feature = false ;
2021-08-18 13:31:35 +02:00
# endif
2021-09-06 10:22:40 +02:00
memp_err = esp_mprot_set_prot ( & memp_cfg ) ;
# endif //other IDF_TARGETS end
2021-08-18 13:31:35 +02:00
if ( memp_err ! = ESP_OK ) {
2021-09-06 10:22:40 +02:00
ESP_EARLY_LOGE ( TAG , " Failed to set Memprot feature (0x%08X: %s), rebooting. " , memp_err , esp_err_to_name ( memp_err ) ) ;
2023-04-25 10:28:10 +02:00
esp_restart_noos ( ) ;
2021-08-18 13:31:35 +02:00
}
2021-09-06 10:22:40 +02:00
# endif //CONFIG_ESP_SYSTEM_MEMPROT_FEATURE && !CONFIG_ESP_SYSTEM_MEMPROT_TEST
2020-03-03 12:22:41 +08:00
2023-08-26 22:08:23 +08:00
# if !CONFIG_APP_BUILD_TYPE_PURE_RAM_APP
// External devices (including SPI0/1, cache) should be initialized
# if !CONFIG_APP_BUILD_TYPE_RAM
// Normal startup flow. We arrive here with the help of 1st, 2nd bootloader. There are valid headers (app/bootloader)
2020-03-03 12:22:41 +08:00
// Read the application binary image header. This will also decrypt the header if the image is encrypted.
2020-08-24 11:09:33 +08:00
__attribute__ ( ( unused ) ) esp_image_header_t fhdr = { 0 } ;
2020-03-03 12:22:41 +08:00
2024-10-22 18:21:40 +05:30
// We can access the image header through the cache by reading from the memory-mapped virtual DROM start offset
uint32_t fhdr_src_addr = ( uint32_t ) ( & _rodata_reserved_start ) - sizeof ( esp_image_header_t ) - sizeof ( esp_image_segment_header_t ) ;
hal_memcpy ( & fhdr , ( void * ) fhdr_src_addr , sizeof ( fhdr ) ) ;
if ( fhdr . magic ! = ESP_IMAGE_HEADER_MAGIC ) {
ESP_EARLY_LOGE ( TAG , " Invalid app image header " ) ;
abort ( ) ;
}
2022-05-30 12:13:23 +07:00
2020-08-24 11:09:33 +08:00
# if CONFIG_IDF_TARGET_ESP32
# if !CONFIG_SPIRAM_BOOT_INIT
2020-03-03 12:22:41 +08:00
// If psram is uninitialized, we need to improve some flash configuration.
bootloader_flash_clock_config ( & fhdr ) ;
bootloader_flash_gpio_config ( & fhdr ) ;
bootloader_flash_dummy_config ( & fhdr ) ;
bootloader_flash_cs_timing_config ( ) ;
# endif //!CONFIG_SPIRAM_BOOT_INIT
2020-08-24 11:09:33 +08:00
# endif //CONFIG_IDF_TARGET_ESP32
# if CONFIG_SPI_FLASH_SIZE_OVERRIDE
int app_flash_size = esp_image_get_flash_size ( fhdr . spi_size ) ;
if ( app_flash_size < 1 * 1024 * 1024 ) {
2022-08-01 11:12:14 +08:00
ESP_EARLY_LOGE ( TAG , " Invalid flash size in app image header. " ) ;
2020-08-24 11:09:33 +08:00
abort ( ) ;
}
bootloader_flash_update_size ( app_flash_size ) ;
# endif //CONFIG_SPI_FLASH_SIZE_OVERRIDE
2023-08-26 22:08:23 +08:00
# else
// CONFIG_APP_BUILD_TYPE_RAM && !CONFIG_APP_BUILD_TYPE_PURE_RAM_APP
bootloader_flash_unlock ( ) ;
# endif
2023-01-30 18:03:41 +08:00
# endif //!CONFIG_APP_BUILD_TYPE_PURE_RAM_APP
2020-03-03 12:22:41 +08:00
2020-02-13 17:43:23 +05:00
# if !CONFIG_ESP_SYSTEM_SINGLE_CORE_MODE
2020-02-05 22:40:15 +08:00
s_cpu_inited [ 0 ] = true ;
volatile bool cpus_inited = false ;
2020-06-09 08:17:48 +08:00
while ( ! cpus_inited ) {
2020-02-05 22:40:15 +08:00
cpus_inited = true ;
for ( int i = 0 ; i < SOC_CPU_CORES_NUM ; i + + ) {
cpus_inited & = s_cpu_inited [ i ] ;
}
2020-07-21 13:07:34 +08:00
esp_rom_delay_us ( 100 ) ;
2020-02-05 22:40:15 +08:00
}
2020-02-13 17:43:23 +05:00
# endif
2020-02-05 22:40:15 +08:00
SYS_STARTUP_FN ( ) ;
2020-08-14 23:42:57 +08:00
}