2021-05-10 04:56:51 +02:00
/*
2024-08-06 13:15:30 +08:00
* SPDX - FileCopyrightText : 2020 - 2024 Espressif Systems ( Shanghai ) CO LTD
2021-05-10 04:56:51 +02:00
*
* SPDX - License - Identifier : Apache - 2.0
*/
2020-12-01 21:34:53 +08:00
# include <stdint.h>
# include "sdkconfig.h"
# include "esp_attr.h"
# include "esp_log.h"
# include "esp_image_format.h"
# include "flash_qio_mode.h"
# include "esp_rom_gpio.h"
# include "esp_rom_uart.h"
# include "esp_rom_sys.h"
2021-09-28 14:12:56 +08:00
# include "esp_rom_spiflash.h"
2020-12-01 21:34:53 +08:00
# include "soc/gpio_sig_map.h"
# include "soc/io_mux_reg.h"
# include "soc/assist_debug_reg.h"
2021-12-14 10:08:15 +05:30
# include "esp_cpu.h"
2020-12-01 21:34:53 +08:00
# include "soc/rtc.h"
2022-05-23 12:36:02 +08:00
# include "soc/rtc_cntl_reg.h"
2020-12-01 21:34:53 +08:00
# include "soc/spi_periph.h"
# include "soc/extmem_reg.h"
# include "soc/io_mux_reg.h"
# include "soc/system_reg.h"
2022-03-17 21:58:15 +08:00
# include "soc/chip_revision.h"
2020-12-01 21:34:53 +08:00
# include "esp32c3/rom/ets_sys.h"
# include "bootloader_common.h"
# include "bootloader_init.h"
# include "bootloader_clock.h"
# include "bootloader_flash_config.h"
# include "bootloader_mem.h"
2022-01-05 06:08:37 +00:00
# include "esp_private/regi2c_ctrl.h"
2022-06-24 12:12:33 +08:00
# include "soc/regi2c_lp_bias.h"
# include "soc/regi2c_bias.h"
2020-12-01 21:34:53 +08:00
# include "bootloader_console.h"
2020-12-29 13:20:24 +08:00
# include "bootloader_flash_priv.h"
2023-01-30 18:04:18 +08:00
# include "esp_private/bootloader_flash_internal.h"
2021-08-27 09:37:52 +05:30
# include "bootloader_soc.h"
2021-06-17 07:21:36 +08:00
# include "esp_efuse.h"
2022-02-11 15:30:54 +08:00
# include "hal/mmu_hal.h"
# include "hal/cache_hal.h"
2022-05-26 03:16:15 +08:00
# include "hal/efuse_hal.h"
2023-09-05 11:38:38 +08:00
# include "hal/rwdt_ll.h"
2024-08-06 13:15:30 +08:00
# include "hal/brownout_ll.h"
2020-12-01 21:34:53 +08:00
static const char * TAG = " boot.esp32c3 " ;
static void wdt_reset_cpu0_info_enable ( void )
{
REG_SET_BIT ( SYSTEM_CPU_PERI_CLK_EN_REG , SYSTEM_CLK_EN_ASSIST_DEBUG ) ;
REG_CLR_BIT ( SYSTEM_CPU_PERI_RST_EN_REG , SYSTEM_RST_EN_ASSIST_DEBUG ) ;
REG_WRITE ( ASSIST_DEBUG_CORE_0_RCD_EN_REG , ASSIST_DEBUG_CORE_0_RCD_PDEBUGEN | ASSIST_DEBUG_CORE_0_RCD_RECORDEN ) ;
}
static void wdt_reset_info_dump ( int cpu )
{
2021-03-18 02:31:28 +08:00
( void ) cpu ;
// saved PC was already printed by the ROM bootloader.
// nothing to do here.
2020-12-01 21:34:53 +08:00
}
static void bootloader_check_wdt_reset ( void )
{
int wdt_rst = 0 ;
2021-07-13 10:45:06 +08:00
soc_reset_reason_t rst_reason = esp_rom_get_reset_reason ( 0 ) ;
if ( rst_reason = = RESET_REASON_CORE_RTC_WDT | | rst_reason = = RESET_REASON_CORE_MWDT0 | | rst_reason = = RESET_REASON_CORE_MWDT1 | |
rst_reason = = RESET_REASON_CPU0_MWDT0 | | rst_reason = = RESET_REASON_CPU0_MWDT1 | | rst_reason = = RESET_REASON_CPU0_RTC_WDT ) {
2020-12-01 21:34:53 +08:00
ESP_LOGW ( TAG , " PRO CPU has been reset by WDT. " ) ;
wdt_rst = 1 ;
}
if ( wdt_rst ) {
// if reset by WDT dump info from trace port
wdt_reset_info_dump ( 0 ) ;
}
wdt_reset_cpu0_info_enable ( ) ;
}
static void bootloader_super_wdt_auto_feed ( void )
{
REG_WRITE ( RTC_CNTL_SWD_WPROTECT_REG , RTC_CNTL_SWD_WKEY_VALUE ) ;
REG_SET_BIT ( RTC_CNTL_SWD_CONF_REG , RTC_CNTL_SWD_AUTO_FEED_EN ) ;
REG_WRITE ( RTC_CNTL_SWD_WPROTECT_REG , 0 ) ;
}
static inline void bootloader_hardware_init ( void )
{
2021-04-20 14:22:14 +05:30
// This check is always included in the bootloader so it can
// print the minimum revision error message later in the boot
2022-03-17 21:58:15 +08:00
if ( ! ESP_CHIP_REV_ABOVE ( efuse_hal_chip_revision ( ) , 3 ) ) {
2021-03-26 17:24:46 +08:00
REGI2C_WRITE_MASK ( I2C_ULP , I2C_ULP_IR_FORCE_XPD_IPH , 1 ) ;
REGI2C_WRITE_MASK ( I2C_BIAS , I2C_BIAS_DREG_1P1_PVT , 12 ) ;
}
2020-12-01 21:34:53 +08:00
}
2021-08-27 09:37:52 +05:30
static inline void bootloader_ana_reset_config ( void )
2020-12-01 21:34:53 +08:00
{
2023-03-07 11:15:05 +08:00
//Enable super WDT reset.
bootloader_ana_super_wdt_reset_config ( true ) ;
2021-04-28 10:32:08 +08:00
/*
2023-03-07 11:15:05 +08:00
For origin chip & ECO1 : brownout & clock glitch reset not available
For ECO2 : fix brownout reset bug
For ECO3 : fix clock glitch reset bug
2021-04-28 10:32:08 +08:00
*/
2022-03-17 21:58:15 +08:00
switch ( efuse_hal_chip_revision ( ) ) {
2021-08-27 09:37:52 +05:30
case 0 :
case 1 :
2023-03-07 11:15:05 +08:00
//Disable BOD and GLITCH reset
2024-08-06 13:15:30 +08:00
brownout_ll_ana_reset_enable ( false ) ;
2021-08-27 09:37:52 +05:30
bootloader_ana_clock_glitch_reset_config ( false ) ;
break ;
case 2 :
2023-03-07 11:15:05 +08:00
//Enable BOD reset. Disable GLITCH reset
2024-08-06 13:15:30 +08:00
brownout_ll_ana_reset_enable ( true ) ;
2021-08-27 09:37:52 +05:30
bootloader_ana_clock_glitch_reset_config ( false ) ;
break ;
case 3 :
default :
2023-03-07 11:15:05 +08:00
//Enable BOD, and GLITCH reset
2024-08-06 13:15:30 +08:00
brownout_ll_ana_reset_enable ( true ) ;
2021-08-27 09:37:52 +05:30
bootloader_ana_clock_glitch_reset_config ( true ) ;
break ;
2021-03-26 17:24:46 +08:00
}
2020-12-01 21:34:53 +08:00
}
esp_err_t bootloader_init ( void )
{
esp_err_t ret = ESP_OK ;
2021-04-20 14:22:14 +05:30
2020-12-01 21:34:53 +08:00
bootloader_hardware_init ( ) ;
2021-08-27 09:37:52 +05:30
bootloader_ana_reset_config ( ) ;
2020-12-01 21:34:53 +08:00
bootloader_super_wdt_auto_feed ( ) ;
2023-01-30 18:03:41 +08:00
// In RAM_APP, memory will be initialized in `call_start_cpu0`
# if !CONFIG_APP_BUILD_TYPE_RAM
2020-12-01 21:34:53 +08:00
// protect memory region
bootloader_init_mem ( ) ;
/* check that static RAM is after the stack */
assert ( & _bss_start < = & _bss_end ) ;
assert ( & _data_start < = & _data_end ) ;
// clear bss section
bootloader_clear_bss_section ( ) ;
2023-01-30 18:03:41 +08:00
# endif // !CONFIG_APP_BUILD_TYPE_RAM
2021-06-17 07:21:36 +08:00
// init eFuse virtual mode (read eFuses to RAM)
# ifdef CONFIG_EFUSE_VIRTUAL
2025-03-13 15:08:09 +08:00
ESP_EARLY_LOGW ( TAG , " eFuse virtual mode is enabled. If Secure boot or Flash encryption is enabled then it does not provide any security. FOR TESTING ONLY! " ) ;
2021-06-17 07:21:36 +08:00
# ifndef CONFIG_EFUSE_VIRTUAL_KEEP_IN_FLASH
esp_efuse_init_virtual_mode_in_ram ( ) ;
# endif
# endif
2020-12-01 21:34:53 +08:00
// config clock
bootloader_clock_configure ( ) ;
// initialize console, from now on, we can use esp_log
bootloader_console_init ( ) ;
/* print 2nd bootloader banner */
bootloader_print_banner ( ) ;
2023-01-30 18:03:41 +08:00
2023-08-26 22:08:23 +08:00
# if !CONFIG_APP_BUILD_TYPE_RAM
2023-01-30 18:03:41 +08:00
//init cache hal
cache_hal_init ( ) ;
2023-03-13 11:37:23 +08:00
//init mmu
2023-01-30 18:03:41 +08:00
mmu_hal_init ( ) ;
2020-12-01 21:34:53 +08:00
// update flash ID
bootloader_flash_update_id ( ) ;
2021-08-01 14:23:36 +08:00
// Check and run XMC startup flow
if ( ( ret = bootloader_flash_xmc_startup ( ) ) ! = ESP_OK ) {
ESP_LOGE ( TAG , " failed when running XMC startup flow, reboot! " ) ;
2023-01-30 18:03:41 +08:00
return ret ;
2021-08-01 14:23:36 +08:00
}
2020-12-01 21:34:53 +08:00
// read bootloader header
if ( ( ret = bootloader_read_bootloader_header ( ) ) ! = ESP_OK ) {
2023-01-30 18:03:41 +08:00
return ret ;
2020-12-01 21:34:53 +08:00
}
// read chip revision and check if it's compatible to bootloader
if ( ( ret = bootloader_check_bootloader_validity ( ) ) ! = ESP_OK ) {
2023-01-30 18:03:41 +08:00
return ret ;
2020-12-01 21:34:53 +08:00
}
// initialize spi flash
if ( ( ret = bootloader_init_spi_flash ( ) ) ! = ESP_OK ) {
2023-01-30 18:03:41 +08:00
return ret ;
2020-12-01 21:34:53 +08:00
}
2023-08-26 22:08:23 +08:00
# endif //#if !CONFIG_APP_BUILD_TYPE_RAM
2023-01-30 18:03:41 +08:00
2024-08-06 13:15:30 +08:00
// check whether a WDT reset happened
2020-12-01 21:34:53 +08:00
bootloader_check_wdt_reset ( ) ;
// config WDT
bootloader_config_wdt ( ) ;
// enable RNG early entropy source
bootloader_enable_random ( ) ;
2023-01-30 18:03:41 +08:00
2020-12-01 21:34:53 +08:00
return ret ;
}