2021-05-09 22:35:07 -04:00
|
|
|
/*
|
2024-03-13 04:58:13 -04:00
|
|
|
* SPDX-FileCopyrightText: 2017-2024 Espressif Systems (Shanghai) CO LTD
|
2021-05-09 22:35:07 -04:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
2019-08-08 01:27:22 -04:00
|
|
|
#include "sdkconfig.h"
|
2017-12-14 18:32:53 -05:00
|
|
|
#include "soc/soc.h"
|
|
|
|
#include "soc/rtc.h"
|
2022-03-17 09:58:15 -04:00
|
|
|
#include "soc/chip_revision.h"
|
2021-12-22 09:18:43 -05:00
|
|
|
#include "hal/efuse_hal.h"
|
2022-07-08 04:46:11 -04:00
|
|
|
|
2024-03-13 02:24:43 -04:00
|
|
|
#if !CONFIG_IDF_TARGET_ESP32C6 && !CONFIG_IDF_TARGET_ESP32H2 && !CONFIG_IDF_TARGET_ESP32P4 && !CONFIG_IDF_TARGET_ESP32C5 &&! CONFIG_IDF_TARGET_ESP32C61 // TODO: IDF-5645
|
2019-12-26 02:25:24 -05:00
|
|
|
#include "soc/rtc_cntl_reg.h"
|
2022-07-08 04:46:11 -04:00
|
|
|
#else
|
|
|
|
#include "soc/lp_wdt_reg.h"
|
|
|
|
#include "soc/lp_timer_reg.h"
|
|
|
|
#include "soc/lp_analog_peri_reg.h"
|
|
|
|
#include "soc/pmu_reg.h"
|
|
|
|
#endif
|
|
|
|
|
2023-12-11 07:10:38 -05:00
|
|
|
#if CONFIG_IDF_TARGET_ESP32 || CONFIG_IDF_TARGET_ESP32C5
|
2022-05-26 14:23:17 -04:00
|
|
|
#include "hal/clk_tree_ll.h"
|
2019-08-08 01:27:22 -04:00
|
|
|
#endif
|
2021-07-12 22:45:06 -04:00
|
|
|
#include "esp_rom_sys.h"
|
2020-07-13 09:33:23 -04:00
|
|
|
#include "esp_rom_uart.h"
|
2019-08-08 01:27:22 -04:00
|
|
|
|
2020-07-29 10:03:46 -04:00
|
|
|
__attribute__((weak)) void bootloader_clock_configure(void)
|
2017-12-14 18:32:53 -05:00
|
|
|
{
|
|
|
|
// ROM bootloader may have put a lot of text into UART0 FIFO.
|
|
|
|
// Wait for it to be printed.
|
|
|
|
// This is not needed on power on reset, when ROM bootloader is running at
|
|
|
|
// 40 MHz. But in case of TG WDT reset, CPU may still be running at >80 MHZ,
|
|
|
|
// and will be done with the bootloader much earlier than UART FIFO is empty.
|
2024-01-17 04:19:49 -05:00
|
|
|
esp_rom_output_tx_wait_idle(0);
|
2017-12-14 18:32:53 -05:00
|
|
|
|
2023-02-09 02:39:38 -05:00
|
|
|
/* Set CPU to a higher certain frequency. Keep other clocks unmodified. */
|
|
|
|
int cpu_freq_mhz = CPU_CLK_FREQ_MHZ_BTLD;
|
2020-07-29 10:03:46 -04:00
|
|
|
|
2020-02-12 07:56:59 -05:00
|
|
|
#if CONFIG_IDF_TARGET_ESP32
|
2018-11-29 02:18:11 -05:00
|
|
|
/* On ESP32 rev 0, switching to 80/160 MHz if clock was previously set to
|
2017-12-14 18:32:53 -05:00
|
|
|
* 240 MHz may cause the chip to lock up (see section 3.5 of the errata
|
2018-11-29 02:18:11 -05:00
|
|
|
* document). For rev. 0, switch to 240 instead if it has been enabled
|
|
|
|
* previously.
|
2017-12-14 18:32:53 -05:00
|
|
|
*/
|
2022-03-17 09:58:15 -04:00
|
|
|
if (!ESP_CHIP_REV_ABOVE(efuse_hal_chip_revision(), 100) &&
|
2022-05-26 14:23:17 -04:00
|
|
|
clk_ll_cpu_get_freq_mhz_from_pll() == CLK_LL_PLL_240M_FREQ_MHZ) {
|
2018-07-29 03:51:02 -04:00
|
|
|
cpu_freq_mhz = 240;
|
2017-12-14 18:32:53 -05:00
|
|
|
}
|
2019-05-27 02:29:43 -04:00
|
|
|
#endif
|
2020-07-29 10:03:46 -04:00
|
|
|
|
2022-05-26 14:23:17 -04:00
|
|
|
if (esp_rom_get_reset_reason(0) != RESET_REASON_CPU0_SW || rtc_clk_apb_freq_get() < APB_CLK_FREQ) {
|
2020-04-30 10:50:41 -04:00
|
|
|
rtc_clk_config_t clk_cfg = RTC_CLK_CONFIG_DEFAULT();
|
2022-11-18 04:59:05 -05:00
|
|
|
|
2020-04-30 10:50:41 -04:00
|
|
|
clk_cfg.cpu_freq_mhz = cpu_freq_mhz;
|
2022-11-18 04:59:05 -05:00
|
|
|
|
2023-12-19 00:49:26 -05:00
|
|
|
#if CONFIG_IDF_TARGET_ESP32C5
|
2024-01-02 09:19:49 -05:00
|
|
|
// TODO: [ESP32C5] IDF-9009 Check whether SOC_RTC_SLOW_CLK_SRC_RC_SLOW can be used on C5 MP
|
|
|
|
// RC150K can't do calibrate on ESP32C5MPW so not use it
|
2023-12-19 00:49:26 -05:00
|
|
|
clk_cfg.slow_clk_src = SOC_RTC_SLOW_CLK_SRC_RC32K;
|
|
|
|
#else
|
2022-11-18 04:59:05 -05:00
|
|
|
// Use RTC_SLOW clock source sel register field's default value, RC_SLOW, for 2nd stage bootloader
|
|
|
|
// RTC_SLOW clock source will be switched according to Kconfig selection at application startup
|
2022-04-21 06:24:03 -04:00
|
|
|
clk_cfg.slow_clk_src = rtc_clk_slow_src_get();
|
2022-05-26 14:23:17 -04:00
|
|
|
if (clk_cfg.slow_clk_src == SOC_RTC_SLOW_CLK_SRC_INVALID) {
|
|
|
|
clk_cfg.slow_clk_src = SOC_RTC_SLOW_CLK_SRC_RC_SLOW;
|
|
|
|
}
|
2023-12-11 07:10:38 -05:00
|
|
|
#endif
|
|
|
|
|
2024-03-13 02:24:43 -04:00
|
|
|
//TODO: [ESP32C61] IDF-9274, basic rtc support
|
|
|
|
#if CONFIG_IDF_TARGET_ESP32C6 || CONFIG_IDF_TARGET_ESP32C5 || CONFIG_IDF_TARGET_ESP32C61
|
2022-11-18 04:59:05 -05:00
|
|
|
// TODO: IDF-5781 Some of esp32c6 SOC_RTC_FAST_CLK_SRC_XTAL_D2 rtc_fast clock has timing issue
|
|
|
|
// Force to use SOC_RTC_FAST_CLK_SRC_RC_FAST since 2nd stage bootloader
|
|
|
|
clk_cfg.fast_clk_src = SOC_RTC_FAST_CLK_SRC_RC_FAST;
|
|
|
|
#else
|
|
|
|
// Use RTC_FAST clock source sel register field's default value, XTAL_DIV, for 2nd stage bootloader
|
|
|
|
// RTC_FAST clock source will be switched to RC_FAST at application startup
|
2022-04-21 06:24:03 -04:00
|
|
|
clk_cfg.fast_clk_src = rtc_clk_fast_src_get();
|
2022-05-26 14:23:17 -04:00
|
|
|
if (clk_cfg.fast_clk_src == SOC_RTC_FAST_CLK_SRC_INVALID) {
|
|
|
|
clk_cfg.fast_clk_src = SOC_RTC_FAST_CLK_SRC_XTAL_DIV;
|
|
|
|
}
|
2022-11-18 04:59:05 -05:00
|
|
|
#endif
|
2020-04-30 10:50:41 -04:00
|
|
|
rtc_clk_init(clk_cfg);
|
|
|
|
}
|
2020-02-12 12:09:17 -05:00
|
|
|
|
2020-07-29 10:03:46 -04:00
|
|
|
/* As a slight optimization, if 32k XTAL was enabled in sdkconfig, we enable
|
|
|
|
* it here. Usually it needs some time to start up, so we amortize at least
|
|
|
|
* part of the start up time by enabling 32k XTAL early.
|
|
|
|
* App startup code will wait until the oscillator has started up.
|
|
|
|
*/
|
2020-07-14 08:39:30 -04:00
|
|
|
#if CONFIG_ESP_SYSTEM_RTC_EXT_XTAL
|
2017-12-14 18:32:53 -05:00
|
|
|
if (!rtc_clk_32k_enabled()) {
|
2020-07-14 08:39:30 -04:00
|
|
|
rtc_clk_32k_bootstrap(CONFIG_ESP_SYSTEM_RTC_EXT_XTAL_BOOTSTRAP_CYCLES);
|
2017-12-14 18:32:53 -05:00
|
|
|
}
|
2020-07-14 08:39:30 -04:00
|
|
|
#endif // CONFIG_ESP_SYSTEM_RTC_EXT_XTAL
|
2021-03-24 07:58:12 -04:00
|
|
|
|
2023-12-15 03:19:44 -05:00
|
|
|
// TODO: IDF-8938 Need refactor! Does not belong to clock configuration.
|
2024-03-13 02:24:43 -04:00
|
|
|
#if CONFIG_IDF_TARGET_ESP32C6 || CONFIG_IDF_TARGET_ESP32C5 || CONFIG_IDF_TARGET_ESP32C61
|
|
|
|
#if CONFIG_IDF_TARGET_ESP32C5 || CONFIG_IDF_TARGET_ESP32C61
|
2023-12-11 07:10:38 -05:00
|
|
|
#define LP_ANALOG_PERI_LP_ANA_LP_INT_ENA_REG LP_ANA_LP_INT_ENA_REG
|
|
|
|
#define LP_ANALOG_PERI_LP_ANA_BOD_MODE0_LP_INT_ENA LP_ANA_BOD_MODE0_LP_INT_ENA
|
|
|
|
#define LP_ANALOG_PERI_LP_ANA_LP_INT_CLR_REG LP_ANA_LP_INT_CLR_REG
|
|
|
|
#define LP_ANALOG_PERI_LP_ANA_BOD_MODE0_LP_INT_CLR LP_ANA_BOD_MODE0_LP_INT_CLR
|
|
|
|
#endif
|
2022-07-08 04:46:11 -04:00
|
|
|
// CLR ENA
|
|
|
|
CLEAR_PERI_REG_MASK(LP_WDT_INT_ENA_REG, LP_WDT_SUPER_WDT_INT_ENA); /* SWD */
|
|
|
|
CLEAR_PERI_REG_MASK(LP_TIMER_LP_INT_ENA_REG, LP_TIMER_MAIN_TIMER_LP_INT_ENA); /* MAIN_TIMER */
|
|
|
|
CLEAR_PERI_REG_MASK(LP_ANALOG_PERI_LP_ANA_LP_INT_ENA_REG, LP_ANALOG_PERI_LP_ANA_BOD_MODE0_LP_INT_ENA); /* BROWN_OUT */
|
|
|
|
CLEAR_PERI_REG_MASK(LP_WDT_INT_ENA_REG, LP_WDT_LP_WDT_INT_ENA); /* WDT */
|
2023-01-28 04:36:45 -05:00
|
|
|
CLEAR_PERI_REG_MASK(PMU_HP_INT_ENA_REG, PMU_SOC_WAKEUP_INT_ENA); /* SLP_REJECT */
|
|
|
|
CLEAR_PERI_REG_MASK(PMU_HP_INT_ENA_REG, PMU_SOC_SLEEP_REJECT_INT_ENA); /* SLP_WAKEUP */
|
2022-07-08 04:46:11 -04:00
|
|
|
// SET CLR
|
|
|
|
SET_PERI_REG_MASK(LP_WDT_INT_CLR_REG, LP_WDT_SUPER_WDT_INT_CLR); /* SWD */
|
|
|
|
SET_PERI_REG_MASK(LP_TIMER_LP_INT_CLR_REG, LP_TIMER_MAIN_TIMER_LP_INT_CLR); /* MAIN_TIMER */
|
|
|
|
SET_PERI_REG_MASK(LP_ANALOG_PERI_LP_ANA_LP_INT_CLR_REG, LP_ANALOG_PERI_LP_ANA_BOD_MODE0_LP_INT_CLR); /* BROWN_OUT */
|
|
|
|
SET_PERI_REG_MASK(LP_WDT_INT_CLR_REG, LP_WDT_LP_WDT_INT_CLR); /* WDT */
|
2022-12-28 22:01:13 -05:00
|
|
|
#elif CONFIG_IDF_TARGET_ESP32H2
|
|
|
|
// CLR ENA
|
|
|
|
CLEAR_PERI_REG_MASK(LP_WDT_INT_ENA_REG, LP_WDT_SUPER_WDT_INT_ENA); /* SWD */
|
|
|
|
CLEAR_PERI_REG_MASK(LP_ANALOG_PERI_LP_ANA_LP_INT_ENA_REG, LP_ANALOG_PERI_LP_ANA_BOD_MODE0_LP_INT_ENA); /* BROWN_OUT */
|
|
|
|
CLEAR_PERI_REG_MASK(LP_WDT_INT_ENA_REG, LP_WDT_LP_WDT_INT_ENA); /* WDT */
|
|
|
|
CLEAR_PERI_REG_MASK(PMU_HP_INT_ENA_REG, PMU_SOC_WAKEUP_INT_ENA); /* SLP_REJECT */
|
2023-01-28 04:36:45 -05:00
|
|
|
CLEAR_PERI_REG_MASK(PMU_HP_INT_ENA_REG, PMU_SOC_SLEEP_REJECT_INT_ENA); /* SLP_WAKEUP */
|
2022-12-28 22:01:13 -05:00
|
|
|
// SET CLR
|
|
|
|
SET_PERI_REG_MASK(LP_WDT_INT_CLR_REG, LP_WDT_SUPER_WDT_INT_CLR); /* SWD */
|
|
|
|
SET_PERI_REG_MASK(LP_ANALOG_PERI_LP_ANA_LP_INT_CLR_REG, LP_ANALOG_PERI_LP_ANA_BOD_MODE0_LP_INT_CLR); /* BROWN_OUT */
|
|
|
|
SET_PERI_REG_MASK(LP_WDT_INT_CLR_REG, LP_WDT_LP_WDT_INT_CLR); /* WDT */
|
2023-01-28 04:36:45 -05:00
|
|
|
SET_PERI_REG_MASK(PMU_HP_INT_CLR_REG, PMU_SOC_WAKEUP_INT_CLR); /* SLP_REJECT */
|
2023-02-09 02:39:38 -05:00
|
|
|
SET_PERI_REG_MASK(PMU_HP_INT_CLR_REG, PMU_SOC_SLEEP_REJECT_INT_CLR); /* SLP_WAKEUP */
|
2023-07-27 03:10:50 -04:00
|
|
|
#elif CONFIG_IDF_TARGET_ESP32P4
|
2023-12-15 03:19:44 -05:00
|
|
|
// CLR ENA
|
|
|
|
CLEAR_PERI_REG_MASK(LP_WDT_INT_ENA_REG, LP_WDT_SUPER_WDT_INT_ENA); /* SWD */
|
|
|
|
CLEAR_PERI_REG_MASK(LP_TIMER_LP_INT_ENA_REG, LP_TIMER_MAIN_TIMER_LP_INT_ENA); /* MAIN_TIMER */
|
|
|
|
CLEAR_PERI_REG_MASK(LP_ANALOG_PERI_LP_INT_ENA_REG, LP_ANALOG_PERI_BOD_MODE0_LP_INT_ENA); /* BROWN_OUT */
|
|
|
|
CLEAR_PERI_REG_MASK(LP_WDT_INT_ENA_REG, LP_WDT_LP_WDT_INT_ENA); /* WDT */
|
|
|
|
CLEAR_PERI_REG_MASK(PMU_HP_INT_ENA_REG, PMU_SOC_WAKEUP_INT_ENA); /* SLP_REJECT */
|
|
|
|
CLEAR_PERI_REG_MASK(PMU_HP_INT_ENA_REG, PMU_SOC_SLEEP_REJECT_INT_ENA); /* SLP_WAKEUP */
|
|
|
|
// SET CLR
|
|
|
|
SET_PERI_REG_MASK(LP_WDT_INT_CLR_REG, LP_WDT_SUPER_WDT_INT_CLR); /* SWD */
|
|
|
|
SET_PERI_REG_MASK(LP_TIMER_LP_INT_CLR_REG, LP_TIMER_MAIN_TIMER_LP_INT_CLR); /* MAIN_TIMER */
|
|
|
|
SET_PERI_REG_MASK(LP_ANALOG_PERI_LP_INT_CLR_REG, LP_ANALOG_PERI_LP_INT_CLR_REG); /* BROWN_OUT */
|
|
|
|
SET_PERI_REG_MASK(LP_WDT_INT_CLR_REG, LP_WDT_LP_WDT_INT_CLR); /* WDT */
|
2022-07-08 04:46:11 -04:00
|
|
|
#else
|
2021-03-24 07:58:12 -04:00
|
|
|
REG_WRITE(RTC_CNTL_INT_ENA_REG, 0);
|
|
|
|
REG_WRITE(RTC_CNTL_INT_CLR_REG, UINT32_MAX);
|
2022-07-08 04:46:11 -04:00
|
|
|
#endif
|
2017-12-14 18:32:53 -05:00
|
|
|
}
|