fix(startup): move rtc initialization before MSPI timing tuning to improve stability

This commit is contained in:
Xiao Xufeng 2024-05-30 18:54:35 +08:00 committed by Michael (XIAO Xufeng)
parent f59e219457
commit dbed93dce8
15 changed files with 157 additions and 98 deletions

View File

@ -44,12 +44,6 @@ void rtc_clk_bbpll_add_consumer(void);
*/ */
void rtc_clk_bbpll_remove_consumer(void); void rtc_clk_bbpll_remove_consumer(void);
/**
* @brief Workaround for C2, S3, C6, H2. Trigger the calibration of PLL. Should be called when the bootloader doesn't provide a good enough PLL accuracy.
*/
void rtc_clk_recalib_bbpll(void);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

View File

@ -358,24 +358,6 @@ bool rtc_dig_8m_enabled(void)
return clk_ll_rc_fast_digi_is_enabled(); return clk_ll_rc_fast_digi_is_enabled();
} }
// Workaround for bootloader not calibrated well issue.
// Placed in IRAM because disabling BBPLL may influence the cache
void rtc_clk_recalib_bbpll(void)
{
rtc_cpu_freq_config_t old_config;
rtc_clk_cpu_freq_get_config(&old_config);
// There are two paths we arrive here: 1. CPU reset. 2. Other reset reasons.
// - For other reasons, the bootloader will set CPU source to BBPLL and enable it. But there are calibration issues.
// Turn off the BBPLL and do calibration again to fix the issue.
// - For CPU reset, the CPU source will be set to XTAL, while the BBPLL is kept to meet USB Serial JTAG's
// requirements. In this case, we don't touch BBPLL to avoid USJ disconnection.
if (old_config.source == SOC_CPU_CLK_SRC_PLL) {
rtc_clk_cpu_freq_set_xtal();
rtc_clk_cpu_freq_set_config(&old_config);
}
}
/* Name used in libphy.a:phy_chip_v7.o /* Name used in libphy.a:phy_chip_v7.o
* TODO: update the library to use rtc_clk_xtal_freq_get * TODO: update the library to use rtc_clk_xtal_freq_get
*/ */

View File

@ -423,25 +423,6 @@ bool rtc_dig_8m_enabled(void)
return clk_ll_rc_fast_digi_is_enabled(); return clk_ll_rc_fast_digi_is_enabled();
} }
// Workaround for bootloader not calibrated well issue.
// Placed in IRAM because disabling BBPLL may influence the cache
void rtc_clk_recalib_bbpll(void)
{
rtc_cpu_freq_config_t old_config;
rtc_clk_cpu_freq_get_config(&old_config);
// There are two paths we arrive here: 1. CPU reset. 2. Other reset reasons.
// - For other reasons, the bootloader will set CPU source to BBPLL and enable it. But there are calibration issues.
// Turn off the BBPLL and do calibration again to fix the issue.
// - For CPU reset, the CPU source will be set to XTAL, while the BBPLL is kept to meet USB Serial JTAG's
// requirements. In this case, we don't touch BBPLL to avoid USJ disconnection.
if (old_config.source == SOC_CPU_CLK_SRC_PLL) {
rtc_clk_cpu_freq_set_xtal();
rtc_clk_cpu_freq_set_config(&old_config);
}
}
/* Name used in libphy.a:phy_chip_v7.o /* Name used in libphy.a:phy_chip_v7.o
* TODO: update the library to use rtc_clk_xtal_freq_get * TODO: update the library to use rtc_clk_xtal_freq_get
*/ */

View File

@ -239,7 +239,7 @@ static void rtc_clk_cpu_freq_to_pll_mhz(int cpu_freq_mhz)
/** /**
* Switch to FLASH_PLL as cpu clock source. * Switch to FLASH_PLL as cpu clock source.
* On ESP32H2, FLASH_PLL frequency is 64MHz. * On ESP32H2, FLASH_PLL frequency is 64MHz.
* PLL must alreay be enabled. * PLL must already be enabled.
*/ */
static void rtc_clk_cpu_freq_to_flash_pll(uint32_t cpu_freq_mhz, uint32_t cpu_divider) static void rtc_clk_cpu_freq_to_flash_pll(uint32_t cpu_freq_mhz, uint32_t cpu_divider)
{ {
@ -475,21 +475,3 @@ bool rtc_dig_8m_enabled(void)
{ {
return clk_ll_rc_fast_digi_is_enabled(); return clk_ll_rc_fast_digi_is_enabled();
} }
// Workaround for bootloader not calibrated well issue.
// Placed in IRAM because disabling BBPLL may influence the cache
void rtc_clk_recalib_bbpll(void)
{
rtc_cpu_freq_config_t old_config;
rtc_clk_cpu_freq_get_config(&old_config);
// There are two paths we arrive here: 1. CPU reset. 2. Other reset reasons.
// - For other reasons, the bootloader will set CPU source to BBPLL and enable it. But there are calibration issues.
// Turn off the BBPLL and do calibration again to fix the issue. Flash_PLL comes from the same source as PLL.
// - For CPU reset, the CPU source will be set to XTAL, while the BBPLL is kept to meet USB Serial JTAG's
// requirements. In this case, we don't touch BBPLL to avoid USJ disconnection.
if (old_config.source == SOC_CPU_CLK_SRC_PLL || old_config.source == SOC_CPU_CLK_SRC_FLASH_PLL) {
rtc_clk_cpu_freq_set_xtal();
rtc_clk_cpu_freq_set_config(&old_config);
}
}

View File

@ -189,7 +189,7 @@ static void rtc_clk_bbpll_configure(rtc_xtal_freq_t xtal_freq, int pll_freq)
*/ */
static void rtc_clk_cpu_freq_to_pll_mhz(int cpu_freq_mhz) static void rtc_clk_cpu_freq_to_pll_mhz(int cpu_freq_mhz)
{ {
/* There are totally 6 LDO slaves(all on by default). At the moment of swithing LDO slave, LDO voltage will also change instantaneously. /* There are totally 6 LDO slaves(all on by default). At the moment of switching LDO slave, LDO voltage will also change instantaneously.
* LDO slave can reduce the voltage change caused by switching frequency. * LDO slave can reduce the voltage change caused by switching frequency.
* CPU frequency <= 40M : just open 3 LDO slaves; CPU frequency = 80M : open 4 LDO slaves; CPU frequency = 160M : open 5 LDO slaves; CPU frequency = 240M : open 6 LDO slaves; * CPU frequency <= 40M : just open 3 LDO slaves; CPU frequency = 80M : open 4 LDO slaves; CPU frequency = 160M : open 5 LDO slaves; CPU frequency = 240M : open 6 LDO slaves;
* *
@ -463,25 +463,6 @@ bool rtc_dig_8m_enabled(void)
return clk_ll_rc_fast_digi_is_enabled(); return clk_ll_rc_fast_digi_is_enabled();
} }
// Workaround for bootloader not calibrated well issue.
// Placed in IRAM because disabling BBPLL may influence the cache
void rtc_clk_recalib_bbpll(void)
{
rtc_cpu_freq_config_t old_config;
rtc_clk_cpu_freq_get_config(&old_config);
// There are two paths we arrive here: 1. CPU reset. 2. Other reset reasons.
// - For other reasons, the bootloader will set CPU source to BBPLL and enable it. But there are calibration issues.
// Turn off the BBPLL and do calibration again to fix the issue.
// - For CPU reset, the CPU source will be set to XTAL, while the BBPLL is kept to meet USB Serial JTAG's
// requirements. In this case, we don't touch BBPLL to avoid USJ disconnection.
if (old_config.source == SOC_CPU_CLK_SRC_PLL) {
rtc_clk_cpu_freq_set_xtal();
rtc_clk_cpu_freq_set_config(&old_config);
}
}
/* Name used in libphy.a:phy_chip_v7.o /* Name used in libphy.a:phy_chip_v7.o
* TODO: update the library to use rtc_clk_xtal_freq_get * TODO: update the library to use rtc_clk_xtal_freq_get
*/ */

View File

@ -64,14 +64,18 @@ void IRAM_ATTR bootloader_fill_random(void *buffer, size_t length)
} }
} }
void esp_clk_init(void) void esp_rtc_init(void)
{ {
s_warn();
#if SOC_PMU_SUPPORTED #if SOC_PMU_SUPPORTED
pmu_init(); pmu_init();
#endif #endif
} }
void esp_clk_init(void)
{
s_warn();
}
void esp_perip_clk_init(void) void esp_perip_clk_init(void)
{ {

View File

@ -444,6 +444,10 @@ void IRAM_ATTR call_start_cpu0(void)
// For Octal flash, it's hard to implement a read_id function in OPI mode for all vendors. // 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. // So we have to read it here in SPI mode, before entering the OPI mode.
bootloader_flash_update_id(); bootloader_flash_update_id();
// Configure the power related stuff. After this the MSPI timing tuning can be done.
esp_rtc_init();
/** /**
* This function initialise the Flash chip to the user-defined settings. * This function initialise the Flash chip to the user-defined settings.
* *
@ -452,14 +456,9 @@ void IRAM_ATTR call_start_cpu0(void)
* In this stage, we re-configure the Flash (and MSPI) to required configuration * In this stage, we re-configure the Flash (and MSPI) to required configuration
*/ */
spi_flash_init_chip_state(); spi_flash_init_chip_state();
// In earlier version of ESP-IDF, the PLL provided by bootloader is not stable enough.
// Do calibration again here so that we can use better clock for the timing tuning.
#if CONFIG_ESP_SYSTEM_BBPLL_RECALIB
rtc_clk_recalib_bbpll();
#endif
#if SOC_MEMSPI_SRC_FREQ_120M #if SOC_MEMSPI_SRC_FREQ_120M
// This function needs to be called when PLL is enabled // 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.
mspi_timing_flash_tuning(); mspi_timing_flash_tuning();
#endif #endif

View File

@ -18,6 +18,15 @@ extern "C" {
* Private clock-related functions * Private clock-related functions
*/ */
/**
* @brief Initialize rtc-related settings
*
* Called from cpu_start.c, not intended to be called from other places.
* This function configures the power related stuff.
* After this the MSPI timing tuning can be done.
*/
void esp_rtc_init(void);
/** /**
* @brief Initialize clock-related settings * @brief Initialize clock-related settings
* *

View File

@ -106,11 +106,14 @@ static void select_rtc_slow_clk(slow_clk_sel_t slow_clk)
esp_clk_slowclk_cal_set(cal_val); esp_clk_slowclk_cal_set(cal_val);
} }
__attribute__((weak)) void esp_clk_init(void) void esp_rtc_init(void)
{ {
rtc_config_t cfg = RTC_CONFIG_DEFAULT(); rtc_config_t cfg = RTC_CONFIG_DEFAULT();
rtc_init(cfg); rtc_init(cfg);
}
__attribute__((weak)) void esp_clk_init(void)
{
#if (CONFIG_APP_COMPATIBLE_PRE_V2_1_BOOTLOADERS || CONFIG_APP_INIT_CLK) #if (CONFIG_APP_COMPATIBLE_PRE_V2_1_BOOTLOADERS || CONFIG_APP_INIT_CLK)
/* Check the bootloader set the XTAL frequency. /* Check the bootloader set the XTAL frequency.

View File

@ -51,12 +51,19 @@ typedef enum {
} slow_clk_sel_t; } slow_clk_sel_t;
static void select_rtc_slow_clk(slow_clk_sel_t slow_clk); static void select_rtc_slow_clk(slow_clk_sel_t slow_clk);
static __attribute__((unused)) void recalib_bbpll(void);
static const char *TAG = "clk"; static const char *TAG = "clk";
__attribute__((weak)) void esp_clk_init(void) void esp_rtc_init(void)
{ {
#if CONFIG_ESP_SYSTEM_BBPLL_RECALIB
// In earlier version of ESP-IDF, the PLL provided by bootloader is not stable enough.
// Do calibration again here so that we can use better clock for the timing tuning.
recalib_bbpll();
#endif
#if !CONFIG_IDF_ENV_FPGA #if !CONFIG_IDF_ENV_FPGA
rtc_config_t cfg = RTC_CONFIG_DEFAULT(); rtc_config_t cfg = RTC_CONFIG_DEFAULT();
soc_reset_reason_t rst_reas; soc_reset_reason_t rst_reas;
@ -65,7 +72,12 @@ static const char *TAG = "clk";
cfg.cali_ocode = 1; cfg.cali_ocode = 1;
} }
rtc_init(cfg); rtc_init(cfg);
#endif
}
__attribute__((weak)) void esp_clk_init(void)
{
#if !CONFIG_IDF_ENV_FPGA
#ifndef CONFIG_XTAL_FREQ_AUTO #ifndef CONFIG_XTAL_FREQ_AUTO
assert(rtc_clk_xtal_freq_get() == CONFIG_XTAL_FREQ); assert(rtc_clk_xtal_freq_get() == CONFIG_XTAL_FREQ);
#endif #endif
@ -266,3 +278,21 @@ __attribute__((weak)) void esp_perip_clk_init(void)
*/ */
periph_module_enable(PERIPH_TIMG0_MODULE); periph_module_enable(PERIPH_TIMG0_MODULE);
} }
// Workaround for bootloader not calibrated well issue.
// Placed in IRAM because disabling BBPLL may influence the cache
static void IRAM_ATTR NOINLINE_ATTR recalib_bbpll(void)
{
rtc_cpu_freq_config_t old_config;
rtc_clk_cpu_freq_get_config(&old_config);
// There are two paths we arrive here: 1. CPU reset. 2. Other reset reasons.
// - For other reasons, the bootloader will set CPU source to BBPLL and enable it. But there are calibration issues.
// Turn off the BBPLL and do calibration again to fix the issue.
// - For CPU reset, the CPU source will be set to XTAL, while the BBPLL is kept to meet USB Serial JTAG's
// requirements. In this case, we don't touch BBPLL to avoid USJ disconnection.
if (old_config.source == SOC_CPU_CLK_SRC_PLL) {
rtc_clk_cpu_freq_set_xtal();
rtc_clk_cpu_freq_set_config(&old_config);
}
}

View File

@ -56,7 +56,7 @@ static void select_rtc_slow_clk(slow_clk_sel_t slow_clk);
static const char *TAG = "clk"; static const char *TAG = "clk";
__attribute__((weak)) void esp_clk_init(void) void esp_rtc_init(void)
{ {
#if !CONFIG_IDF_ENV_FPGA #if !CONFIG_IDF_ENV_FPGA
rtc_config_t cfg = RTC_CONFIG_DEFAULT(); rtc_config_t cfg = RTC_CONFIG_DEFAULT();
@ -70,7 +70,10 @@ static const char *TAG = "clk";
cfg.cali_ocode = 1; cfg.cali_ocode = 1;
} }
rtc_init(cfg); rtc_init(cfg);
}
__attribute__((weak)) void esp_clk_init(void)
{
assert(rtc_clk_xtal_freq_get() == RTC_XTAL_FREQ_40M); assert(rtc_clk_xtal_freq_get() == RTC_XTAL_FREQ_40M);
bool rc_fast_d256_is_enabled = rtc_clk_8md256_enabled(); bool rc_fast_d256_is_enabled = rtc_clk_8md256_enabled();

View File

@ -37,18 +37,30 @@
#define MHZ (1000000) #define MHZ (1000000)
static void select_rtc_slow_clk(soc_rtc_slow_clk_src_t rtc_slow_clk_src); static void select_rtc_slow_clk(soc_rtc_slow_clk_src_t rtc_slow_clk_src);
static __attribute__((unused)) void recalib_bbpll(void);
static const char *TAG = "clk"; static const char *TAG = "clk";
__attribute__((weak)) void esp_clk_init(void) void esp_rtc_init(void)
{ {
#if CONFIG_ESP_SYSTEM_BBPLL_RECALIB
// In earlier version of ESP-IDF, the PLL provided by bootloader is not stable enough.
// Do calibration again here so that we can use better clock for the timing tuning.
recalib_bbpll();
#endif
#if !CONFIG_IDF_ENV_FPGA #if !CONFIG_IDF_ENV_FPGA
pmu_init(); pmu_init();
if (esp_rom_get_reset_reason(0) == RESET_REASON_CHIP_POWER_ON) { if (esp_rom_get_reset_reason(0) == RESET_REASON_CHIP_POWER_ON) {
esp_ocode_calib_init(); esp_ocode_calib_init();
} }
#endif
}
__attribute__((weak)) void esp_clk_init(void)
{
#if !CONFIG_IDF_ENV_FPGA
assert(rtc_clk_xtal_freq_get() == RTC_XTAL_FREQ_40M); assert(rtc_clk_xtal_freq_get() == RTC_XTAL_FREQ_40M);
rtc_clk_8m_enable(true); rtc_clk_8m_enable(true);
@ -302,3 +314,21 @@ __attribute__((weak)) void esp_perip_clk_init(void)
*/ */
periph_module_enable(PERIPH_TIMG0_MODULE); periph_module_enable(PERIPH_TIMG0_MODULE);
} }
// Workaround for bootloader not calibrated well issue.
// Placed in IRAM because disabling BBPLL may influence the cache
static void IRAM_ATTR NOINLINE_ATTR recalib_bbpll(void)
{
rtc_cpu_freq_config_t old_config;
rtc_clk_cpu_freq_get_config(&old_config);
// There are two paths we arrive here: 1. CPU reset. 2. Other reset reasons.
// - For other reasons, the bootloader will set CPU source to BBPLL and enable it. But there are calibration issues.
// Turn off the BBPLL and do calibration again to fix the issue.
// - For CPU reset, the CPU source will be set to XTAL, while the BBPLL is kept to meet USB Serial JTAG's
// requirements. In this case, we don't touch BBPLL to avoid USJ disconnection.
if (old_config.source == SOC_CPU_CLK_SRC_PLL) {
rtc_clk_cpu_freq_set_xtal();
rtc_clk_cpu_freq_set_config(&old_config);
}
}

View File

@ -37,15 +37,27 @@
#define MHZ (1000000) #define MHZ (1000000)
static void select_rtc_slow_clk(soc_rtc_slow_clk_src_t rtc_slow_clk_src); static void select_rtc_slow_clk(soc_rtc_slow_clk_src_t rtc_slow_clk_src);
static __attribute__((unused)) void recalib_bbpll(void);
static const char *TAG = "clk"; static const char *TAG = "clk";
void esp_rtc_init(void)
{
#if CONFIG_ESP_SYSTEM_BBPLL_RECALIB
// In earlier version of ESP-IDF, the PLL provided by bootloader is not stable enough.
// Do calibration again here so that we can use better clock for the timing tuning.
recalib_bbpll();
#endif
#if !CONFIG_IDF_ENV_FPGA
pmu_init();
#endif
}
__attribute__((weak)) void esp_clk_init(void) __attribute__((weak)) void esp_clk_init(void)
{ {
#if !CONFIG_IDF_ENV_FPGA #if !CONFIG_IDF_ENV_FPGA
pmu_init();
assert(rtc_clk_xtal_freq_get() == RTC_XTAL_FREQ_32M); assert(rtc_clk_xtal_freq_get() == RTC_XTAL_FREQ_32M);
rtc_clk_8m_enable(true); rtc_clk_8m_enable(true);
@ -296,3 +308,21 @@ __attribute__((weak)) void esp_perip_clk_init(void)
*/ */
periph_module_enable(PERIPH_TIMG0_MODULE); periph_module_enable(PERIPH_TIMG0_MODULE);
} }
// Workaround for bootloader not calibrated well issue.
// Placed in IRAM because disabling BBPLL may influence the cache
static void IRAM_ATTR NOINLINE_ATTR recalib_bbpll(void)
{
rtc_cpu_freq_config_t old_config;
rtc_clk_cpu_freq_get_config(&old_config);
// There are two paths we arrive here: 1. CPU reset. 2. Other reset reasons.
// - For other reasons, the bootloader will set CPU source to BBPLL and enable it. But there are calibration issues.
// Turn off the BBPLL and do calibration again to fix the issue. Flash_PLL comes from the same source as PLL.
// - For CPU reset, the CPU source will be set to XTAL, while the BBPLL is kept to meet USB Serial JTAG's
// requirements. In this case, we don't touch BBPLL to avoid USJ disconnection.
if (old_config.source == SOC_CPU_CLK_SRC_PLL || old_config.source == SOC_CPU_CLK_SRC_FLASH_PLL) {
rtc_clk_cpu_freq_set_xtal();
rtc_clk_cpu_freq_set_config(&old_config);
}
}

View File

@ -60,7 +60,7 @@ typedef enum {
static void select_rtc_slow_clk(slow_clk_sel_t slow_clk); static void select_rtc_slow_clk(slow_clk_sel_t slow_clk);
__attribute__((weak)) void esp_clk_init(void) void esp_rtc_init(void)
{ {
rtc_config_t cfg = RTC_CONFIG_DEFAULT(); rtc_config_t cfg = RTC_CONFIG_DEFAULT();
soc_reset_reason_t rst_reas = esp_rom_get_reset_reason(0); soc_reset_reason_t rst_reas = esp_rom_get_reset_reason(0);
@ -74,7 +74,10 @@ static void select_rtc_slow_clk(slow_clk_sel_t slow_clk);
} }
} }
rtc_init(cfg); rtc_init(cfg);
}
__attribute__((weak)) void esp_clk_init(void)
{
bool rc_fast_d256_is_enabled = rtc_clk_8md256_enabled(); bool rc_fast_d256_is_enabled = rtc_clk_8md256_enabled();
rtc_clk_8m_enable(true, rc_fast_d256_is_enabled); rtc_clk_8m_enable(true, rc_fast_d256_is_enabled);
rtc_clk_fast_src_set(SOC_RTC_FAST_CLK_SRC_RC_FAST); rtc_clk_fast_src_set(SOC_RTC_FAST_CLK_SRC_RC_FAST);

View File

@ -53,9 +53,16 @@ typedef enum {
} slow_clk_sel_t; } slow_clk_sel_t;
static void select_rtc_slow_clk(slow_clk_sel_t slow_clk); static void select_rtc_slow_clk(slow_clk_sel_t slow_clk);
static __attribute__((unused)) void recalib_bbpll(void);
__attribute__((weak)) void esp_clk_init(void) void esp_rtc_init(void)
{ {
#if CONFIG_ESP_SYSTEM_BBPLL_RECALIB
// In earlier version of ESP-IDF, the PLL provided by bootloader is not stable enough.
// Do calibration again here so that we can use better clock for the timing tuning.
recalib_bbpll();
#endif
rtc_config_t cfg = RTC_CONFIG_DEFAULT(); rtc_config_t cfg = RTC_CONFIG_DEFAULT();
soc_reset_reason_t rst_reas; soc_reset_reason_t rst_reas;
rst_reas = esp_rom_get_reset_reason(0); rst_reas = esp_rom_get_reset_reason(0);
@ -64,7 +71,10 @@ static void select_rtc_slow_clk(slow_clk_sel_t slow_clk);
cfg.cali_ocode = 1; cfg.cali_ocode = 1;
} }
rtc_init(cfg); rtc_init(cfg);
}
__attribute__((weak)) void esp_clk_init(void)
{
assert(rtc_clk_xtal_freq_get() == RTC_XTAL_FREQ_40M); assert(rtc_clk_xtal_freq_get() == RTC_XTAL_FREQ_40M);
bool rc_fast_d256_is_enabled = rtc_clk_8md256_enabled(); bool rc_fast_d256_is_enabled = rtc_clk_8md256_enabled();
@ -324,3 +334,21 @@ __attribute__((weak)) void esp_perip_clk_init(void)
*/ */
periph_module_enable(PERIPH_TIMG0_MODULE); periph_module_enable(PERIPH_TIMG0_MODULE);
} }
// Workaround for bootloader not calibrated well issue.
// Placed in IRAM because disabling BBPLL may influence the cache
static void IRAM_ATTR NOINLINE_ATTR recalib_bbpll(void)
{
rtc_cpu_freq_config_t old_config;
rtc_clk_cpu_freq_get_config(&old_config);
// There are two paths we arrive here: 1. CPU reset. 2. Other reset reasons.
// - For other reasons, the bootloader will set CPU source to BBPLL and enable it. But there are calibration issues.
// Turn off the BBPLL and do calibration again to fix the issue.
// - For CPU reset, the CPU source will be set to XTAL, while the BBPLL is kept to meet USB Serial JTAG's
// requirements. In this case, we don't touch BBPLL to avoid USJ disconnection.
if (old_config.source == SOC_CPU_CLK_SRC_PLL) {
rtc_clk_cpu_freq_set_xtal();
rtc_clk_cpu_freq_set_config(&old_config);
}
}