mirror of
https://github.com/espressif/esp-idf.git
synced 2026-10-02 03:00:34 +03:00
Merge branch 'feat/support_esp32s31_clock_tree_management_v6.1' into 'release/v6.1'
feat: support esp32s31 clock tree management (v6.1) See merge request espressif/esp-idf!51205
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2022-2025 Espressif Systems (Shanghai) CO LTD
|
||||
* SPDX-FileCopyrightText: 2022-2026 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
@@ -27,6 +27,7 @@
|
||||
#include "esp_private/etm_interface.h"
|
||||
#include "esp_private/sleep_retention.h"
|
||||
#include "esp_private/critical_section.h"
|
||||
#include "esp_private/esp_clk_tree_common.h"
|
||||
|
||||
#define ETM_MEM_ALLOC_CAPS MALLOC_CAP_DEFAULT
|
||||
|
||||
@@ -63,6 +64,9 @@ struct esp_etm_channel_t {
|
||||
_Atomic etm_chan_fsm_t fsm; // record ETM channel's driver state
|
||||
esp_etm_event_handle_t event; // which event is connect to the channel
|
||||
esp_etm_task_handle_t task; // which task is connect to the channel
|
||||
#if ETM_LL_SUPPORT(CLOCK_SRC)
|
||||
etm_clock_source_t clk_src; // function clock source enabled for this channel
|
||||
#endif
|
||||
};
|
||||
|
||||
// ETM driver platform, it's always a singleton
|
||||
@@ -272,6 +276,8 @@ esp_err_t esp_etm_new_channel(const esp_etm_channel_config_t *config, esp_etm_ch
|
||||
if (clk_src == 0) {
|
||||
clk_src = ETM_CLK_SRC_DEFAULT;
|
||||
}
|
||||
ESP_GOTO_ON_ERROR(esp_clk_tree_enable_src((soc_module_clk_t)clk_src, true), err, TAG, "clock source enable failed");
|
||||
chan->clk_src = clk_src;
|
||||
etm_ll_set_clock_source(group_id, clk_src);
|
||||
#endif
|
||||
|
||||
@@ -290,6 +296,11 @@ esp_err_t esp_etm_new_channel(const esp_etm_channel_config_t *config, esp_etm_ch
|
||||
|
||||
err:
|
||||
if (chan) {
|
||||
#if ETM_LL_SUPPORT(CLOCK_SRC)
|
||||
if (chan->clk_src != 0) {
|
||||
esp_clk_tree_enable_src((soc_module_clk_t)chan->clk_src, false);
|
||||
}
|
||||
#endif
|
||||
etm_chan_destroy(chan);
|
||||
}
|
||||
return ret;
|
||||
@@ -310,6 +321,11 @@ esp_err_t esp_etm_del_channel(esp_etm_channel_handle_t chan)
|
||||
etm_ll_channel_set_task(group->hal.regs, chan_id, 0);
|
||||
|
||||
ESP_LOGD(TAG, "del etm channel (%d,%d)", group_id, chan_id);
|
||||
#if ETM_LL_SUPPORT(CLOCK_SRC)
|
||||
// Back to hardware default clock selection, otherwise it might get stuck when stopping the bus during sleep process.
|
||||
etm_ll_set_clock_source(group_id, ETM_CLK_SRC_XTAL);
|
||||
ESP_RETURN_ON_ERROR(esp_clk_tree_enable_src((soc_module_clk_t)chan->clk_src, false), TAG, "clock source disable failed");
|
||||
#endif
|
||||
// recycle memory resource
|
||||
ESP_RETURN_ON_ERROR(etm_chan_destroy(chan), TAG, "destroy etm channel failed");
|
||||
return ESP_OK;
|
||||
|
||||
@@ -124,6 +124,13 @@ bool esp_clk_tree_enable_power(soc_root_clk_circuit_t clk_circuit, bool enable);
|
||||
*/
|
||||
bool esp_clk_tree_is_power_on(soc_root_clk_circuit_t clk_circuit);
|
||||
|
||||
/**
|
||||
* @brief Chip-specific root clock circuit power status (port/esp_clk_tree.c per target).
|
||||
*
|
||||
* Handles all soc_root_clk_circuit_t values except MPLL/APLL (those are in esp_clk_tree_common.c).
|
||||
*/
|
||||
bool esp_clk_tree_port_is_power_on(soc_root_clk_circuit_t clk_circuit);
|
||||
|
||||
#if SOC_CLK_APLL_SUPPORTED
|
||||
/**
|
||||
* @brief Enable APLL power if it has not enabled
|
||||
|
||||
@@ -60,11 +60,13 @@ typedef struct {
|
||||
typedef struct {
|
||||
soc_module_clk_t clk_id; /*!< Module clock id this descriptor belongs to */
|
||||
void (*set_src)(uint8_t mux_sel); /*!< Optional: program the upstream PLL mux. NULL when the clock has no mux (e.g. ESP32-P4 PLL_F50M is fixed to MPLL). */
|
||||
void (*set_divider)(uint32_t divider); /*!< Required: program the divider register (divider value, not the raw "div_num - 1" form). */
|
||||
void (*set_divider)(uint32_t divider); /*!< Optional: program the divider register (divider value, not div_num - 1). NULL when HW divider is fixed. */
|
||||
void (*set_gate)(bool enable); /*!< Required: enable/disable the clock gate. */
|
||||
const esp_clk_tree_derived_upstream_t *upstreams; /*!< Required: candidate upstream PLLs in preference order (used by both auto-pick and explicit-upstream paths). */
|
||||
size_t upstream_count; /*!< Required: number of entries in `upstreams[]`. */
|
||||
esp_clk_tree_derived_clk_state_t *state; /*!< Required: pointer to a statically-allocated mutable state slot owned by the target. Engine reads/writes ref_cnt / cur_upstream / cur_divider through this pointer. */
|
||||
esp_err_t (*acquire_parent)(void); /*!< Optional: power upstream (first acquire or after mux/div commit). Called with s_derived_clk_spinlock held.*/
|
||||
esp_err_t (*release_parent)(void); /*!< Optional: release upstream on last release. Called with s_derived_clk_spinlock held.*/
|
||||
} esp_clk_tree_derived_clk_desc_t;
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2022-2025 Espressif Systems (Shanghai) CO LTD
|
||||
* SPDX-FileCopyrightText: 2022-2026 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
@@ -26,6 +26,7 @@ extern "C" {
|
||||
*/
|
||||
void rtc_clk_cpu_set_to_default_config(void);
|
||||
|
||||
#ifndef BOOTLOADER_BUILD
|
||||
/**
|
||||
* @brief Switch CPU clock source to XTAL, the PLL has different processing methods for different chips.
|
||||
* 1. For earlier chips without PMU, there is no PMU module that can turn off the CPU's PLL, so it has to be
|
||||
@@ -39,6 +40,7 @@ void rtc_clk_cpu_set_to_default_config(void);
|
||||
* to 40MHz to speed up the retention speed.
|
||||
*/
|
||||
void rtc_clk_cpu_freq_set_xtal_for_sleep(void);
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @brief Notify that the BBPLL has a new in-use consumer
|
||||
|
||||
@@ -13,12 +13,16 @@ entries:
|
||||
cpu: esp_cpu_compare_and_set (noflash)
|
||||
esp_memory_utils (noflash)
|
||||
clk_utils (noflash)
|
||||
# TODO: PM-630
|
||||
if IDF_TARGET_ESP32S31 != y:
|
||||
esp_clk_tree: esp_clk_tree_enable_src (noflash)
|
||||
esp_clk_tree: esp_clk_tree_enable_power (noflash)
|
||||
esp_clk_tree: esp_clk_tree_is_power_on (noflash)
|
||||
esp_clk_tree: esp_clk_tree_enable_src (noflash)
|
||||
if RTC_CLK_FUNC_IN_IRAM = y:
|
||||
esp_clk_tree:esp_clk_tree_enable_power (noflash)
|
||||
esp_clk_tree:esp_clk_tree_port_is_power_on (noflash)
|
||||
esp_clk_tree_common:esp_clk_tree_is_power_on (noflash)
|
||||
if SOC_CLK_MPLL_SUPPORTED = y:
|
||||
esp_clk_tree_common:esp_clk_tree_mpll_release (noflash)
|
||||
if SOC_CLK_APLL_SUPPORTED = y:
|
||||
esp_clk_tree_common:esp_clk_tree_apll_acquire (noflash)
|
||||
esp_clk_tree_common:esp_clk_tree_apll_release (noflash)
|
||||
rtc_clk (noflash)
|
||||
if IDF_TARGET_ESP32 = y:
|
||||
rtc_clk:rtc_clk_cpu_freq_to_pll_mhz (noflash)
|
||||
|
||||
@@ -6,6 +6,8 @@
|
||||
|
||||
#include "sdkconfig.h"
|
||||
#include "esp_attr.h"
|
||||
#include "esp_check.h"
|
||||
#include "esp_clk_tree.h"
|
||||
#include "soc/soc_caps.h"
|
||||
#include "modem/modem_clock_impl.h"
|
||||
#include "esp_private/regi2c_ctrl.h"
|
||||
@@ -212,7 +214,13 @@ static void IRAM_ATTR modem_clock_coex_configure(modem_clock_context_t *ctx, boo
|
||||
#if SOC_MODEM_CLOCK_SOC_PLL_SOURCE_CG_SUPPORTED
|
||||
static void IRAM_ATTR modem_clock_soc_pll_source_cg_configure(modem_clock_context_t *ctx, bool enable)
|
||||
{
|
||||
if (enable) {
|
||||
ESP_ERROR_CHECK(esp_clk_tree_enable_src(SOC_MOD_CLK_PLL_F160M, true));
|
||||
}
|
||||
modem_clock_hal_enable_soc_pll_source_cg(ctx->hal, enable);
|
||||
if (!enable) {
|
||||
ESP_ERROR_CHECK(esp_clk_tree_enable_src(SOC_MOD_CLK_PLL_F160M, false));
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
@@ -99,7 +99,7 @@ void esp_clk_tree_initialize(void)
|
||||
{
|
||||
}
|
||||
|
||||
bool esp_clk_tree_is_power_on(soc_root_clk_circuit_t clk_circuit)
|
||||
bool esp_clk_tree_port_is_power_on(soc_root_clk_circuit_t clk_circuit)
|
||||
{
|
||||
(void)clk_circuit;
|
||||
return false;
|
||||
|
||||
@@ -79,7 +79,7 @@ void esp_clk_tree_initialize(void)
|
||||
{
|
||||
}
|
||||
|
||||
bool esp_clk_tree_is_power_on(soc_root_clk_circuit_t clk_circuit)
|
||||
bool esp_clk_tree_port_is_power_on(soc_root_clk_circuit_t clk_circuit)
|
||||
{
|
||||
(void)clk_circuit;
|
||||
return false;
|
||||
|
||||
@@ -79,7 +79,7 @@ void esp_clk_tree_initialize(void)
|
||||
{
|
||||
}
|
||||
|
||||
bool esp_clk_tree_is_power_on(soc_root_clk_circuit_t clk_circuit)
|
||||
bool esp_clk_tree_port_is_power_on(soc_root_clk_circuit_t clk_circuit)
|
||||
{
|
||||
(void)clk_circuit;
|
||||
return false;
|
||||
|
||||
@@ -112,7 +112,7 @@ void esp_clk_tree_initialize(void)
|
||||
esp_clk_tree_initialized = true;
|
||||
}
|
||||
|
||||
bool esp_clk_tree_is_power_on(soc_root_clk_circuit_t clk_circuit)
|
||||
bool esp_clk_tree_port_is_power_on(soc_root_clk_circuit_t clk_circuit)
|
||||
{
|
||||
(void)clk_circuit;
|
||||
return false;
|
||||
|
||||
@@ -79,7 +79,7 @@ void esp_clk_tree_initialize(void)
|
||||
{
|
||||
}
|
||||
|
||||
bool esp_clk_tree_is_power_on(soc_root_clk_circuit_t clk_circuit)
|
||||
bool esp_clk_tree_port_is_power_on(soc_root_clk_circuit_t clk_circuit)
|
||||
{
|
||||
(void)clk_circuit;
|
||||
return false;
|
||||
|
||||
@@ -79,7 +79,7 @@ void esp_clk_tree_initialize(void)
|
||||
{
|
||||
}
|
||||
|
||||
bool esp_clk_tree_is_power_on(soc_root_clk_circuit_t clk_circuit)
|
||||
bool esp_clk_tree_port_is_power_on(soc_root_clk_circuit_t clk_circuit)
|
||||
{
|
||||
(void)clk_circuit;
|
||||
return false;
|
||||
|
||||
@@ -76,7 +76,7 @@ void esp_clk_tree_initialize(void)
|
||||
{
|
||||
}
|
||||
|
||||
bool esp_clk_tree_is_power_on(soc_root_clk_circuit_t clk_circuit)
|
||||
bool esp_clk_tree_port_is_power_on(soc_root_clk_circuit_t clk_circuit)
|
||||
{
|
||||
(void)clk_circuit;
|
||||
return false;
|
||||
|
||||
@@ -90,15 +90,13 @@ void esp_clk_tree_initialize(void)
|
||||
// PLL_F64M ++ for MSPI
|
||||
}
|
||||
|
||||
bool esp_clk_tree_is_power_on(soc_root_clk_circuit_t clk_circuit)
|
||||
bool esp_clk_tree_port_is_power_on(soc_root_clk_circuit_t clk_circuit)
|
||||
{
|
||||
switch (clk_circuit) {
|
||||
case SOC_ROOT_CIRCUIT_CLK_XTAL_X2:
|
||||
if (clk_circuit == SOC_ROOT_CIRCUIT_CLK_XTAL_X2) {
|
||||
return s_xtal_x2_ref_cnt > 0;
|
||||
case SOC_ROOT_CIRCUIT_CLK_BBPLL:
|
||||
}
|
||||
if (clk_circuit == SOC_ROOT_CIRCUIT_CLK_BBPLL) {
|
||||
return s_bbpll_ref_cnt > 0;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -92,15 +92,13 @@ void esp_clk_tree_initialize(void)
|
||||
// PLL_F64M ++ for MSPI
|
||||
}
|
||||
|
||||
bool esp_clk_tree_is_power_on(soc_root_clk_circuit_t clk_circuit)
|
||||
bool esp_clk_tree_port_is_power_on(soc_root_clk_circuit_t clk_circuit)
|
||||
{
|
||||
switch (clk_circuit) {
|
||||
case SOC_ROOT_CIRCUIT_CLK_XTAL_X2:
|
||||
if (clk_circuit == SOC_ROOT_CIRCUIT_CLK_XTAL_X2) {
|
||||
return s_xtal_x2_ref_cnt > 0;
|
||||
case SOC_ROOT_CIRCUIT_CLK_BBPLL:
|
||||
}
|
||||
if (clk_circuit == SOC_ROOT_CIRCUIT_CLK_BBPLL) {
|
||||
return s_bbpll_ref_cnt > 0;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -3,14 +3,14 @@ target_include_directories(${COMPONENT_LIB} PUBLIC .)
|
||||
set(srcs "rtc_clk_init.c"
|
||||
"rtc_clk.c"
|
||||
"pmu_param.c"
|
||||
"pmu_init.c"
|
||||
"pmu_sleep.c"
|
||||
"rtc_time.c"
|
||||
"chip_info.c"
|
||||
)
|
||||
|
||||
if(NOT BOOTLOADER_BUILD)
|
||||
list(APPEND srcs "sar_periph_ctrl.c")
|
||||
list(APPEND srcs "pmu_init.c"
|
||||
"pmu_sleep.c"
|
||||
"sar_periph_ctrl.c")
|
||||
|
||||
if(CONFIG_PM_SLEEP_CLK_ICG_ENABLE AND NOT CONFIG_PM_POWER_DOWN_PERIPHERAL_IN_LIGHT_SLEEP)
|
||||
list(APPEND srcs "pmu_sleep_clock_icg.c")
|
||||
|
||||
@@ -5,9 +5,9 @@
|
||||
*/
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdatomic.h>
|
||||
#include "sdkconfig.h"
|
||||
#include "esp_clk_tree.h"
|
||||
#include "esp_attr.h"
|
||||
#include "esp_err.h"
|
||||
#include "esp_check.h"
|
||||
#include "esp_log.h"
|
||||
@@ -21,6 +21,7 @@
|
||||
#include "esp_private/esp_clk_tree_common.h"
|
||||
#include "esp_private/esp_clk_tree_derived.h"
|
||||
#include "esp_private/periph_ctrl.h"
|
||||
#include "esp_private/critical_section.h"
|
||||
|
||||
ESP_LOG_ATTR_TAG(TAG, "esp_clk_tree");
|
||||
|
||||
@@ -166,8 +167,12 @@ esp_err_t esp_clk_tree_src_set_freq_hz(soc_module_clk_t clk_src, uint32_t expt_f
|
||||
return ret;
|
||||
}
|
||||
|
||||
static _Atomic int16_t s_pll_src_cg_ref_cnt[SOC_MOD_CLK_INVALID] = { 0 };
|
||||
static bool esp_clk_tree_initialized = false;
|
||||
DEFINE_CRIT_SECTION_LOCK_STATIC(s_clk_tree_spinlock);
|
||||
|
||||
/** Per soc_module_clk_t: record clock gate consumers */
|
||||
static int16_t s_mod_clk_gate_ref_cnt[SOC_MOD_CLK_INVALID] = { 0 };
|
||||
|
||||
static bool s_clk_tree_initialized = false;
|
||||
|
||||
void esp_clk_tree_initialize(void)
|
||||
{
|
||||
@@ -175,7 +180,7 @@ void esp_clk_tree_initialize(void)
|
||||
if ((rst_reason == RESET_REASON_CPU_SW) || (rst_reason == RESET_REASON_CPU_MWDT) \
|
||||
|| (rst_reason == RESET_REASON_CPU_RWDT) || (rst_reason == RESET_REASON_CPU_JTAG) \
|
||||
|| (rst_reason == RESET_REASON_CPU_LOCKUP)) {
|
||||
esp_clk_tree_initialized = true;
|
||||
s_clk_tree_initialized = true;
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -189,10 +194,10 @@ void esp_clk_tree_initialize(void)
|
||||
_clk_gate_ll_ref_160m_clk_en(false);
|
||||
#endif
|
||||
_clk_gate_ll_ref_240m_clk_en(false);
|
||||
esp_clk_tree_initialized = true;
|
||||
s_clk_tree_initialized = true;
|
||||
}
|
||||
|
||||
bool esp_clk_tree_is_power_on(soc_root_clk_circuit_t clk_circuit)
|
||||
bool esp_clk_tree_port_is_power_on(soc_root_clk_circuit_t clk_circuit)
|
||||
{
|
||||
(void)clk_circuit;
|
||||
return false;
|
||||
@@ -204,77 +209,129 @@ bool esp_clk_tree_enable_power(soc_root_clk_circuit_t clk_circuit, bool enable)
|
||||
return false; // TODO: PM-653
|
||||
}
|
||||
|
||||
#define ENABLE_CLK_GATE(clk_src_en_func, enable) \
|
||||
PERIPH_RCC_ATOMIC() { \
|
||||
clk_src_en_func(enable); \
|
||||
/* -------------------------------------------------------------------------- */
|
||||
/* Fixed ref clocks: gate only (no parent power management on ESP32-P4) */
|
||||
/* -------------------------------------------------------------------------- */
|
||||
|
||||
typedef void (*esp_clk_tree_gate_fn_t)(bool enable);
|
||||
|
||||
typedef struct {
|
||||
soc_module_clk_t clk_id;
|
||||
esp_clk_tree_gate_fn_t set_gate;
|
||||
} esp_clk_tree_gated_clk_t;
|
||||
|
||||
static void esp_clk_tree_gate_rc_fast(bool enable)
|
||||
{
|
||||
if (enable) {
|
||||
rtc_dig_clk8m_enable();
|
||||
} else {
|
||||
rtc_dig_clk8m_disable();
|
||||
}
|
||||
}
|
||||
|
||||
typedef enum {
|
||||
ESP_CLK_TREE_GATED_CLK_RC_FAST,
|
||||
ESP_CLK_TREE_GATED_CLK_PLL_F20M,
|
||||
ESP_CLK_TREE_GATED_CLK_PLL_F25M,
|
||||
ESP_CLK_TREE_GATED_CLK_PLL_F80M,
|
||||
ESP_CLK_TREE_GATED_CLK_PLL_F120M,
|
||||
ESP_CLK_TREE_GATED_CLK_PLL_F160M,
|
||||
ESP_CLK_TREE_GATED_CLK_PLL_F240M,
|
||||
ESP_CLK_TREE_GATED_CLK_NUM,
|
||||
} esp_clk_tree_gated_clk_id_t;
|
||||
|
||||
static const esp_clk_tree_gated_clk_t s_gated_ref_clks[] = {
|
||||
[ESP_CLK_TREE_GATED_CLK_RC_FAST] = { SOC_MOD_CLK_RC_FAST, esp_clk_tree_gate_rc_fast },
|
||||
[ESP_CLK_TREE_GATED_CLK_PLL_F20M] = { SOC_MOD_CLK_PLL_F20M, _clk_gate_ll_ref_20m_clk_en },
|
||||
[ESP_CLK_TREE_GATED_CLK_PLL_F25M] = { SOC_MOD_CLK_PLL_F25M, _clk_gate_ll_ref_25m_clk_en },
|
||||
[ESP_CLK_TREE_GATED_CLK_PLL_F80M] = { SOC_MOD_CLK_PLL_F80M, _clk_gate_ll_ref_80m_clk_en },
|
||||
[ESP_CLK_TREE_GATED_CLK_PLL_F120M] = { SOC_MOD_CLK_PLL_F120M, _clk_gate_ll_ref_120m_clk_en },
|
||||
[ESP_CLK_TREE_GATED_CLK_PLL_F160M] = { SOC_MOD_CLK_PLL_F160M, _clk_gate_ll_ref_160m_clk_en },
|
||||
[ESP_CLK_TREE_GATED_CLK_PLL_F240M] = { SOC_MOD_CLK_PLL_F240M, _clk_gate_ll_ref_240m_clk_en },
|
||||
};
|
||||
|
||||
#define ENABLE_CLK_GATE(clk_src_en_func, enable) \
|
||||
do { \
|
||||
if ((clk_src_en_func) != NULL) { \
|
||||
PERIPH_RCC_ATOMIC() { \
|
||||
(clk_src_en_func)(enable); \
|
||||
}; \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
FORCE_INLINE_ATTR esp_err_t esp_clk_tree_enable_gated_clk(const esp_clk_tree_gated_clk_t *entry, bool enable)
|
||||
{
|
||||
int16_t prev_ref_cnt;
|
||||
|
||||
esp_os_enter_critical(&s_clk_tree_spinlock);
|
||||
if (enable) {
|
||||
prev_ref_cnt = s_mod_clk_gate_ref_cnt[entry->clk_id]++;
|
||||
} else {
|
||||
prev_ref_cnt = s_mod_clk_gate_ref_cnt[entry->clk_id]--;
|
||||
if (prev_ref_cnt <= 0) {
|
||||
s_mod_clk_gate_ref_cnt[entry->clk_id] = 0;
|
||||
esp_os_exit_critical(&s_clk_tree_spinlock);
|
||||
ESP_EARLY_LOGW(TAG, "soc_module_clk_t %d disabled multiple times!!", entry->clk_id);
|
||||
return ESP_OK;
|
||||
}
|
||||
}
|
||||
esp_os_exit_critical(&s_clk_tree_spinlock);
|
||||
|
||||
if (prev_ref_cnt == 0 && enable) {
|
||||
ENABLE_CLK_GATE(entry->set_gate, true);
|
||||
} else if (prev_ref_cnt == 1 && !enable) {
|
||||
ENABLE_CLK_GATE(entry->set_gate, false);
|
||||
}
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
esp_err_t esp_clk_tree_enable_src(soc_module_clk_t clk_src, bool enable)
|
||||
{
|
||||
if (clk_src < 1 || clk_src >= SOC_MOD_CLK_INVALID) {
|
||||
// some conditions is legal, e.g. -1 means external clock source
|
||||
if (clk_src < 1 || clk_src >= SOC_MOD_CLK_INVALID || clk_src == SOC_MOD_CLK_XTAL) {
|
||||
/* Not managed by esp_clk_tree */
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
if (!esp_clk_tree_initialized) {
|
||||
if (!s_clk_tree_initialized) {
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
// Derived PLL clocks (PLL_F50M, ...) that participate in the shared
|
||||
// refcount/lock engine route through that engine instead of the
|
||||
// global s_pll_src_cg_ref_cnt array below.
|
||||
if (esp_clk_tree_get_derived_clk_desc(clk_src) != NULL) {
|
||||
return enable ? esp_clk_tree_derived_clk_acquire(clk_src)
|
||||
: esp_clk_tree_derived_clk_release(clk_src);
|
||||
}
|
||||
|
||||
esp_clk_tree_gated_clk_id_t gated_clk_id;
|
||||
// these clock sources have their own reference counting
|
||||
switch (clk_src) {
|
||||
case SOC_MOD_CLK_APLL:
|
||||
if (enable) {
|
||||
esp_clk_tree_apll_acquire();
|
||||
} else {
|
||||
esp_clk_tree_apll_release();
|
||||
}
|
||||
return ESP_OK;
|
||||
case SOC_MOD_CLK_MPLL:
|
||||
if (enable) {
|
||||
return esp_clk_tree_mpll_acquire();
|
||||
} else {
|
||||
esp_clk_tree_mpll_release();
|
||||
}
|
||||
return ESP_OK;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
// other clock sources use the global reference counting
|
||||
int16_t prev_ref_cnt = 0;
|
||||
if (enable) {
|
||||
prev_ref_cnt = atomic_fetch_add(&s_pll_src_cg_ref_cnt[clk_src], 1);
|
||||
} else {
|
||||
prev_ref_cnt = atomic_fetch_sub(&s_pll_src_cg_ref_cnt[clk_src], 1);
|
||||
if (prev_ref_cnt <= 0) {
|
||||
ESP_EARLY_LOGW(TAG, "soc_module_clk_t %d disabled multiple times!!", clk_src);
|
||||
atomic_store(&s_pll_src_cg_ref_cnt[clk_src], 0);
|
||||
case SOC_MOD_CLK_APLL:
|
||||
if (enable) {
|
||||
esp_clk_tree_apll_acquire();
|
||||
} else {
|
||||
esp_clk_tree_apll_release();
|
||||
}
|
||||
return ESP_OK;
|
||||
case SOC_MOD_CLK_MPLL:
|
||||
if (enable) {
|
||||
return esp_clk_tree_mpll_acquire();
|
||||
} else {
|
||||
esp_clk_tree_mpll_release();
|
||||
return ESP_OK;
|
||||
}
|
||||
}
|
||||
if ((prev_ref_cnt == 0 && enable) || (prev_ref_cnt == 1 && !enable)) {
|
||||
switch (clk_src) {
|
||||
case SOC_MOD_CLK_RC_FAST: enable ? rtc_dig_clk8m_enable() : rtc_dig_clk8m_disable(); break;
|
||||
case SOC_MOD_CLK_PLL_F20M: ENABLE_CLK_GATE(clk_gate_ll_ref_20m_clk_en, enable); break;
|
||||
case SOC_MOD_CLK_PLL_F25M: ENABLE_CLK_GATE(clk_gate_ll_ref_25m_clk_en, enable); break;
|
||||
case SOC_MOD_CLK_PLL_F80M: ENABLE_CLK_GATE(clk_gate_ll_ref_80m_clk_en, enable); break;
|
||||
case SOC_MOD_CLK_PLL_F120M: ENABLE_CLK_GATE(clk_gate_ll_ref_120m_clk_en, enable); break;
|
||||
case SOC_MOD_CLK_RC_FAST: gated_clk_id = ESP_CLK_TREE_GATED_CLK_RC_FAST; break;
|
||||
case SOC_MOD_CLK_PLL_F20M: gated_clk_id = ESP_CLK_TREE_GATED_CLK_PLL_F20M; break;
|
||||
case SOC_MOD_CLK_PLL_F25M: gated_clk_id = ESP_CLK_TREE_GATED_CLK_PLL_F25M; break;
|
||||
case SOC_MOD_CLK_PLL_F80M: gated_clk_id = ESP_CLK_TREE_GATED_CLK_PLL_F80M; break;
|
||||
case SOC_MOD_CLK_PLL_F120M: gated_clk_id = ESP_CLK_TREE_GATED_CLK_PLL_F120M; break;
|
||||
#if !CONFIG_ESP_ENABLE_PVT
|
||||
// PLL_F160M must always on if PVT is enabled.
|
||||
case SOC_MOD_CLK_PLL_F160M: ENABLE_CLK_GATE(clk_gate_ll_ref_160m_clk_en, enable); break;
|
||||
// PLL_F160M must always on if PVT is enabled.
|
||||
case SOC_MOD_CLK_PLL_F160M: gated_clk_id = ESP_CLK_TREE_GATED_CLK_PLL_F160M; break;
|
||||
#endif
|
||||
case SOC_MOD_CLK_PLL_F240M: ENABLE_CLK_GATE(clk_gate_ll_ref_240m_clk_en, enable); break;
|
||||
default: break;
|
||||
case SOC_MOD_CLK_PLL_F240M: gated_clk_id = ESP_CLK_TREE_GATED_CLK_PLL_F240M; break;
|
||||
default:
|
||||
// Derived PLL clocks (PLL_F50M, ...) that participate in the shared
|
||||
// refcount/lock engine route through that engine instead of the
|
||||
// global s_pll_src_cg_ref_cnt array below.
|
||||
if (esp_clk_tree_get_derived_clk_desc(clk_src) != NULL) {
|
||||
return enable ? esp_clk_tree_derived_clk_acquire(clk_src)
|
||||
: esp_clk_tree_derived_clk_release(clk_src);
|
||||
}
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
return ESP_OK;
|
||||
return esp_clk_tree_enable_gated_clk(&s_gated_ref_clks[gated_clk_id], enable);
|
||||
}
|
||||
|
||||
@@ -30,7 +30,9 @@ ESP_HW_LOG_ATTR_TAG(TAG, "rtc_clk");
|
||||
static int s_cur_cpll_freq = 0;
|
||||
|
||||
// MPLL frequency option, 400MHz. Zero if MPLL is not enabled.
|
||||
#ifndef BOOTLOADER_BUILD
|
||||
static SPM_DRAM_ATTR uint32_t s_cur_mpll_freq = 0;
|
||||
#endif
|
||||
|
||||
void rtc_clk_32k_enable(bool enable)
|
||||
{
|
||||
@@ -641,6 +643,7 @@ bool rtc_dig_8m_enabled(void)
|
||||
return clk_ll_rc_fast_digi_is_enabled();
|
||||
}
|
||||
|
||||
#ifndef BOOTLOADER_BUILD
|
||||
//------------------------------------MPLL-------------------------------------//
|
||||
SPM_IRAM_ATTR void rtc_clk_mpll_disable(void)
|
||||
{
|
||||
@@ -680,3 +683,4 @@ SPM_IRAM_ATTR uint32_t rtc_clk_mpll_get_freq(void)
|
||||
{
|
||||
return s_cur_mpll_freq;
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -97,7 +97,7 @@ void esp_clk_tree_initialize(void)
|
||||
{
|
||||
}
|
||||
|
||||
bool esp_clk_tree_is_power_on(soc_root_clk_circuit_t clk_circuit)
|
||||
bool esp_clk_tree_port_is_power_on(soc_root_clk_circuit_t clk_circuit)
|
||||
{
|
||||
(void)clk_circuit;
|
||||
return false;
|
||||
|
||||
@@ -89,7 +89,7 @@ void esp_clk_tree_initialize(void)
|
||||
{
|
||||
}
|
||||
|
||||
bool esp_clk_tree_is_power_on(soc_root_clk_circuit_t clk_circuit)
|
||||
bool esp_clk_tree_port_is_power_on(soc_root_clk_circuit_t clk_circuit)
|
||||
{
|
||||
(void)clk_circuit;
|
||||
return false;
|
||||
|
||||
@@ -5,13 +5,14 @@ set(srcs
|
||||
"rtc_clk.c"
|
||||
"rtc_time.c"
|
||||
"chip_info.c"
|
||||
"pmu_param.c"
|
||||
"pmu_init.c"
|
||||
"pmu_sleep.c"
|
||||
)
|
||||
|
||||
if(NOT BOOTLOADER_BUILD)
|
||||
list(APPEND srcs "sar_periph_ctrl.c")
|
||||
list(APPEND srcs "sar_periph_ctrl.c"
|
||||
"pmu_param.c"
|
||||
"pmu_init.c"
|
||||
"pmu_sleep.c"
|
||||
)
|
||||
endif()
|
||||
|
||||
add_prefix(srcs "${CMAKE_CURRENT_LIST_DIR}/" "${srcs}")
|
||||
|
||||
@@ -5,59 +5,318 @@
|
||||
*/
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdatomic.h>
|
||||
#include <assert.h>
|
||||
#include "sdkconfig.h"
|
||||
#include "esp_clk_tree.h"
|
||||
#include "esp_attr.h"
|
||||
#include "esp_err.h"
|
||||
#include "esp_check.h"
|
||||
#include "esp_log.h"
|
||||
#include "soc/clk_tree_defs.h"
|
||||
#include "esp_rom_sys.h"
|
||||
#include "soc/rtc.h"
|
||||
#include "soc/reset_reasons.h"
|
||||
#include "soc/soc_caps.h"
|
||||
#include "hal/clk_gate_ll.h"
|
||||
#include "hal/clk_tree_hal.h"
|
||||
#include "hal/clk_tree_ll.h"
|
||||
#include "hal/mspi_ll.h"
|
||||
#include "esp_private/esp_clk_tree_common.h"
|
||||
#include "esp_private/esp_clk_tree_derived.h"
|
||||
#include "esp_private/periph_ctrl.h"
|
||||
#include "esp_private/critical_section.h"
|
||||
|
||||
ESP_LOG_ATTR_TAG(TAG, "esp_clk_tree");
|
||||
|
||||
/* -------------------------------------------------------------------------- */
|
||||
/* Derived clocks: configurable divider and/or upstream mux + multi-user lock */
|
||||
/* -------------------------------------------------------------------------- */
|
||||
|
||||
enum {
|
||||
ESP_CLK_TREE_DERIVED_PLL_F25M = 0,
|
||||
ESP_CLK_TREE_DERIVED_PLL_F50M,
|
||||
ESP_CLK_TREE_DERIVED_REF_F80M,
|
||||
ESP_CLK_TREE_DERIVED_PLL_NUM,
|
||||
};
|
||||
|
||||
static esp_clk_tree_derived_clk_state_t s_derived_pll_states[ESP_CLK_TREE_DERIVED_PLL_NUM];
|
||||
|
||||
static int8_t s_ref_500m_committed_mux = -1;
|
||||
|
||||
/**
|
||||
* Power ref_25m/ref_50m shared upstream (ref_500m_sel: 0 = CPLL, 1 = MPLL).
|
||||
* @param mux_sel 0 or 1; values < 0 are ignored.
|
||||
*/
|
||||
static esp_err_t esp_clk_tree_ref_500m_parent_power(int8_t mux_sel, bool enable)
|
||||
{
|
||||
if (mux_sel < 0) {
|
||||
return ESP_OK;
|
||||
}
|
||||
if (mux_sel == 0) {
|
||||
esp_clk_tree_enable_power(SOC_ROOT_CIRCUIT_CLK_CPLL, enable);
|
||||
return ESP_OK;
|
||||
}
|
||||
if (enable) {
|
||||
return esp_clk_tree_mpll_acquire();
|
||||
}
|
||||
esp_clk_tree_mpll_release();
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
/**
|
||||
* Shared ref_500m_sel: PLL_F25M and PLL_F50M must use the same upstream when both are on.
|
||||
*/
|
||||
static void esp_clk_tree_ref_500m_set_src(uint8_t mux_sel)
|
||||
{
|
||||
if (s_derived_pll_states[ESP_CLK_TREE_DERIVED_PLL_F25M].ref_cnt > 0
|
||||
&& s_derived_pll_states[ESP_CLK_TREE_DERIVED_PLL_F50M].ref_cnt > 0
|
||||
&& s_ref_500m_committed_mux >= 0) {
|
||||
assert(mux_sel == (uint8_t)s_ref_500m_committed_mux);
|
||||
}
|
||||
clk_ll_ref_500m_set_src(mux_sel);
|
||||
}
|
||||
|
||||
static esp_err_t esp_clk_tree_ref_500m_derived_acquire_parent(void)
|
||||
{
|
||||
int8_t mux = (int8_t)clk_ll_ref_500m_get_src();
|
||||
if (mux == s_ref_500m_committed_mux) {
|
||||
return ESP_OK;
|
||||
}
|
||||
if (s_derived_pll_states[ESP_CLK_TREE_DERIVED_PLL_F25M].ref_cnt > 0
|
||||
&& s_derived_pll_states[ESP_CLK_TREE_DERIVED_PLL_F50M].ref_cnt > 0
|
||||
&& s_ref_500m_committed_mux >= 0) {
|
||||
assert(mux == s_ref_500m_committed_mux);
|
||||
}
|
||||
esp_clk_tree_ref_500m_parent_power(s_ref_500m_committed_mux, false);
|
||||
esp_err_t ret = esp_clk_tree_ref_500m_parent_power(mux, true);
|
||||
if (ret != ESP_OK) {
|
||||
esp_clk_tree_ref_500m_parent_power(s_ref_500m_committed_mux, true);
|
||||
return ret;
|
||||
}
|
||||
s_ref_500m_committed_mux = mux;
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
static esp_err_t esp_clk_tree_ref_500m_derived_release_parent(void)
|
||||
{
|
||||
if (s_derived_pll_states[ESP_CLK_TREE_DERIVED_PLL_F25M].ref_cnt > 0
|
||||
|| s_derived_pll_states[ESP_CLK_TREE_DERIVED_PLL_F50M].ref_cnt > 0) {
|
||||
return ESP_OK;
|
||||
}
|
||||
esp_clk_tree_ref_500m_parent_power(s_ref_500m_committed_mux, false);
|
||||
s_ref_500m_committed_mux = -1;
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
/** F80M parent path committed at first enable: 0 = BBPLL, 1 = XTALx2 (-1 = none). */
|
||||
static int8_t s_ref_80m_committed_sel = -1;
|
||||
|
||||
/**
|
||||
* REF_F80M enable-time source policy:
|
||||
* 1) If BBPLL is already on → BBPLL/6 path + one more BBPLL power user.
|
||||
* 2) Else → power XTALx2 and select ref_80m via clk_ll_ref_80m_set_src(1).
|
||||
*/
|
||||
static esp_err_t esp_clk_tree_ref_80m_derived_acquire_parent(void)
|
||||
{
|
||||
if (s_ref_80m_committed_sel >= 0) {
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
_clk_gate_ll_ref_80m_mux_clk_en(true);
|
||||
if (esp_clk_tree_port_is_power_on(SOC_ROOT_CIRCUIT_CLK_BBPLL)) {
|
||||
esp_clk_tree_enable_power(SOC_ROOT_CIRCUIT_CLK_BBPLL, true);
|
||||
clk_ll_ref_80m_set_src(0);
|
||||
s_ref_80m_committed_sel = 0;
|
||||
} else {
|
||||
esp_clk_tree_enable_power(SOC_ROOT_CIRCUIT_CLK_XTAL_X2, true);
|
||||
clk_ll_ref_80m_set_src(1);
|
||||
s_ref_80m_committed_sel = 1;
|
||||
}
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
static esp_err_t esp_clk_tree_ref_80m_derived_release_parent(void)
|
||||
{
|
||||
if (s_derived_pll_states[ESP_CLK_TREE_DERIVED_REF_F80M].ref_cnt > 0) {
|
||||
return ESP_OK;
|
||||
}
|
||||
if (s_ref_80m_committed_sel == 0) {
|
||||
esp_clk_tree_enable_power(SOC_ROOT_CIRCUIT_CLK_BBPLL, false);
|
||||
} else if (s_ref_80m_committed_sel == 1) {
|
||||
esp_clk_tree_enable_power(SOC_ROOT_CIRCUIT_CLK_XTAL_X2, false);
|
||||
}
|
||||
_clk_gate_ll_ref_80m_mux_clk_en(false);
|
||||
s_ref_80m_committed_sel = -1;
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
// Allowed upstreams for PLL_F50M, in preference order (used by both auto-pick
|
||||
// and explicit-upstream paths). Mux selectors match the HP_SYS_CLKRST
|
||||
// `reg_ref_500m_sel` field: 0 = CPLL, 1 = MPLL.
|
||||
static const esp_clk_tree_derived_upstream_t s_pll_f50m_upstreams[] = {
|
||||
// `reg_ref_500m_sel` field: 0 = CPLL, 1 = MPLL. (shared by PLL_F25M and PLL_F50M).
|
||||
static const esp_clk_tree_derived_upstream_t s_ref_500m_upstreams[] = {
|
||||
{ SOC_MOD_CLK_CPLL, 0 },
|
||||
{ SOC_MOD_CLK_MPLL, 1 },
|
||||
};
|
||||
|
||||
static esp_clk_tree_derived_clk_state_t s_pll_f50m_state = {
|
||||
.ref_cnt = 0,
|
||||
.cur_upstream = SOC_MOD_CLK_INVALID,
|
||||
.cur_divider = 0,
|
||||
};
|
||||
|
||||
static const esp_clk_tree_derived_clk_desc_t s_pll_f50m_desc = {
|
||||
.clk_id = SOC_MOD_CLK_PLL_F50M,
|
||||
.set_src = clk_ll_ref_500m_set_src,
|
||||
.set_divider = clk_ll_pll_f50m_set_divider,
|
||||
.set_gate = _clk_gate_ll_ref_50m_clk_en, // using RCC_ATOMIC lock free function version to avoid nesting critical sections
|
||||
.upstreams = s_pll_f50m_upstreams,
|
||||
.upstream_count = sizeof(s_pll_f50m_upstreams) / sizeof(s_pll_f50m_upstreams[0]),
|
||||
.state = &s_pll_f50m_state,
|
||||
static const esp_clk_tree_derived_clk_desc_t s_derived_pll_descs[ESP_CLK_TREE_DERIVED_PLL_NUM] = {
|
||||
[ESP_CLK_TREE_DERIVED_PLL_F25M] = {
|
||||
.clk_id = SOC_MOD_CLK_PLL_F25M,
|
||||
.set_src = esp_clk_tree_ref_500m_set_src,
|
||||
.set_divider = clk_ll_pll_f25m_set_divider,
|
||||
.set_gate = _clk_gate_ll_ref_25m_clk_en,
|
||||
.upstreams = s_ref_500m_upstreams,
|
||||
.upstream_count = sizeof(s_ref_500m_upstreams) / sizeof(s_ref_500m_upstreams[0]),
|
||||
.state = &s_derived_pll_states[ESP_CLK_TREE_DERIVED_PLL_F25M],
|
||||
.acquire_parent = esp_clk_tree_ref_500m_derived_acquire_parent,
|
||||
.release_parent = esp_clk_tree_ref_500m_derived_release_parent,
|
||||
},
|
||||
[ESP_CLK_TREE_DERIVED_PLL_F50M] = {
|
||||
.clk_id = SOC_MOD_CLK_PLL_F50M,
|
||||
.set_src = esp_clk_tree_ref_500m_set_src,
|
||||
.set_divider = clk_ll_pll_f50m_set_divider,
|
||||
.set_gate = _clk_gate_ll_ref_50m_clk_en,
|
||||
.upstreams = s_ref_500m_upstreams,
|
||||
.upstream_count = sizeof(s_ref_500m_upstreams) / sizeof(s_ref_500m_upstreams[0]),
|
||||
.state = &s_derived_pll_states[ESP_CLK_TREE_DERIVED_PLL_F50M],
|
||||
.acquire_parent = esp_clk_tree_ref_500m_derived_acquire_parent,
|
||||
.release_parent = esp_clk_tree_ref_500m_derived_release_parent,
|
||||
},
|
||||
[ESP_CLK_TREE_DERIVED_REF_F80M] = {
|
||||
.clk_id = SOC_MOD_CLK_REF_F80M,
|
||||
.set_src = NULL,
|
||||
.set_divider = NULL,
|
||||
.set_gate = _clk_gate_ll_ref_80m_clk_en,
|
||||
.upstreams = NULL,
|
||||
.upstream_count = 0,
|
||||
.state = &s_derived_pll_states[ESP_CLK_TREE_DERIVED_REF_F80M],
|
||||
.acquire_parent = esp_clk_tree_ref_80m_derived_acquire_parent,
|
||||
.release_parent = esp_clk_tree_ref_80m_derived_release_parent,
|
||||
},
|
||||
};
|
||||
|
||||
const esp_clk_tree_derived_clk_desc_t *esp_clk_tree_get_derived_clk_desc(soc_module_clk_t clk_src)
|
||||
{
|
||||
switch (clk_src) {
|
||||
case SOC_MOD_CLK_PLL_F25M:
|
||||
return &s_derived_pll_descs[ESP_CLK_TREE_DERIVED_PLL_F25M];
|
||||
case SOC_MOD_CLK_PLL_F50M:
|
||||
return &s_pll_f50m_desc;
|
||||
return &s_derived_pll_descs[ESP_CLK_TREE_DERIVED_PLL_F50M];
|
||||
case SOC_MOD_CLK_REF_F80M:
|
||||
return &s_derived_pll_descs[ESP_CLK_TREE_DERIVED_REF_F80M];
|
||||
default:
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
/* -------------------------------------------------------------------------- */
|
||||
/* Fixed ref clocks: gate + static BBPLL parent only */
|
||||
/* -------------------------------------------------------------------------- */
|
||||
|
||||
typedef void (*esp_clk_tree_gate_fn_t)(bool enable);
|
||||
typedef void (*esp_clk_tree_parent_fn_t)(bool enable);
|
||||
|
||||
typedef struct {
|
||||
soc_module_clk_t clk_id;
|
||||
esp_clk_tree_gate_fn_t set_gate;
|
||||
esp_clk_tree_parent_fn_t parent_power;
|
||||
} esp_clk_tree_gated_clk_t;
|
||||
|
||||
static void esp_clk_tree_parent_bbpll(bool enable)
|
||||
{
|
||||
esp_clk_tree_enable_power(SOC_ROOT_CIRCUIT_CLK_BBPLL, enable);
|
||||
}
|
||||
|
||||
static void esp_clk_tree_parent_rc_fast(bool enable)
|
||||
{
|
||||
if (enable) {
|
||||
rtc_dig_clk8m_enable();
|
||||
} else {
|
||||
rtc_dig_clk8m_disable();
|
||||
}
|
||||
}
|
||||
|
||||
DEFINE_CRIT_SECTION_LOCK_STATIC(s_clk_tree_spinlock);
|
||||
|
||||
/** Per soc_module_clk_t: record clock gate consumers */
|
||||
static int16_t s_mod_clk_gate_ref_cnt[SOC_MOD_CLK_INVALID] = { 0 };
|
||||
|
||||
/** Per soc_root_clk_circuit_t: record clock power consumers */
|
||||
static int16_t s_root_pll_power_ref_cnt[SOC_ROOT_CIRCUIT_CLK_MAX] = { 0 };
|
||||
|
||||
static bool s_clk_tree_initialized = false;
|
||||
|
||||
static int16_t esp_clk_tree_root_pll_power_acquire(soc_root_clk_circuit_t clk_circuit)
|
||||
{
|
||||
int16_t prev;
|
||||
|
||||
assert(clk_circuit == SOC_ROOT_CIRCUIT_CLK_BBPLL || clk_circuit == SOC_ROOT_CIRCUIT_CLK_CPLL
|
||||
|| clk_circuit == SOC_ROOT_CIRCUIT_CLK_XTAL_X2);
|
||||
|
||||
esp_os_enter_critical(&s_clk_tree_spinlock);
|
||||
prev = s_root_pll_power_ref_cnt[clk_circuit]++;
|
||||
if (prev == 0) {
|
||||
switch (clk_circuit) {
|
||||
case SOC_ROOT_CIRCUIT_CLK_BBPLL:
|
||||
clk_ll_bbpll_enable();
|
||||
break;
|
||||
case SOC_ROOT_CIRCUIT_CLK_CPLL:
|
||||
clk_ll_cpll_enable();
|
||||
break;
|
||||
case SOC_ROOT_CIRCUIT_CLK_XTAL_X2:
|
||||
clk_ll_xtalx2_enable();
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
esp_os_exit_critical(&s_clk_tree_spinlock);
|
||||
return prev;
|
||||
}
|
||||
|
||||
static int16_t esp_clk_tree_root_pll_power_release(soc_root_clk_circuit_t clk_circuit)
|
||||
{
|
||||
int16_t prev;
|
||||
|
||||
assert(clk_circuit == SOC_ROOT_CIRCUIT_CLK_BBPLL || clk_circuit == SOC_ROOT_CIRCUIT_CLK_CPLL
|
||||
|| clk_circuit == SOC_ROOT_CIRCUIT_CLK_XTAL_X2);
|
||||
|
||||
esp_os_enter_critical(&s_clk_tree_spinlock);
|
||||
prev = s_root_pll_power_ref_cnt[clk_circuit];
|
||||
if (prev <= 0) {
|
||||
esp_os_exit_critical(&s_clk_tree_spinlock);
|
||||
ESP_EARLY_LOGW(TAG, "soc_root_clk_circuit_t %d disabled multiple times!!", clk_circuit);
|
||||
return prev;
|
||||
}
|
||||
s_root_pll_power_ref_cnt[clk_circuit] = prev - 1;
|
||||
if (prev == 1) {
|
||||
switch (clk_circuit) {
|
||||
case SOC_ROOT_CIRCUIT_CLK_BBPLL:
|
||||
clk_ll_bbpll_disable();
|
||||
break;
|
||||
case SOC_ROOT_CIRCUIT_CLK_CPLL:
|
||||
clk_ll_cpll_disable();
|
||||
break;
|
||||
case SOC_ROOT_CIRCUIT_CLK_XTAL_X2:
|
||||
clk_ll_xtalx2_disable();
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
esp_os_exit_critical(&s_clk_tree_spinlock);
|
||||
return prev;
|
||||
}
|
||||
|
||||
static uint32_t esp_clk_tree_ref_500m_pll_get_freq_hz(uint32_t div_num)
|
||||
{
|
||||
uint32_t up_hz;
|
||||
|
||||
if (clk_ll_ref_500m_get_src() == 0) {
|
||||
up_hz = clk_ll_cpll_get_freq_mhz(clk_hal_xtal_get_freq_mhz()) * MHZ;
|
||||
} else {
|
||||
up_hz = clk_ll_mpll_get_freq_mhz(clk_hal_xtal_get_freq_mhz()) * MHZ;
|
||||
}
|
||||
return up_hz / div_num;
|
||||
}
|
||||
|
||||
esp_err_t esp_clk_tree_src_get_freq_hz(soc_module_clk_t clk_src, esp_clk_tree_src_freq_precision_t precision,
|
||||
uint32_t *freq_value)
|
||||
{
|
||||
@@ -79,9 +338,21 @@ esp_err_t esp_clk_tree_src_get_freq_hz(soc_module_clk_t clk_src, esp_clk_tree_sr
|
||||
case SOC_MOD_CLK_PLL_F20M:
|
||||
clk_src_freq = CLK_LL_PLL_480M_FREQ_MHZ / clk_ll_pll_f20m_get_divider() * MHZ;
|
||||
break;
|
||||
case SOC_MOD_CLK_PLL_F80M:
|
||||
case SOC_MOD_CLK_PLL_F25M:
|
||||
clk_src_freq = esp_clk_tree_ref_500m_pll_get_freq_hz(clk_ll_pll_f25m_get_divider());
|
||||
break;
|
||||
case SOC_MOD_CLK_PLL_F50M:
|
||||
clk_src_freq = esp_clk_tree_ref_500m_pll_get_freq_hz(clk_ll_pll_f50m_get_divider());
|
||||
break;
|
||||
case SOC_MOD_CLK_PLL_F60M:
|
||||
clk_src_freq = CLK_LL_PLL_60M_FREQ_MHZ * MHZ;
|
||||
break;
|
||||
case SOC_MOD_CLK_REF_F80M:
|
||||
clk_src_freq = CLK_LL_PLL_80M_FREQ_MHZ * MHZ;
|
||||
break;
|
||||
case SOC_MOD_CLK_PLL_F120M:
|
||||
clk_src_freq = CLK_LL_PLL_120M_FREQ_MHZ * MHZ;
|
||||
break;
|
||||
case SOC_MOD_CLK_PLL_F160M:
|
||||
clk_src_freq = CLK_LL_PLL_160M_FREQ_MHZ * MHZ;
|
||||
break;
|
||||
@@ -159,81 +430,157 @@ esp_err_t esp_clk_tree_src_set_freq_hz(soc_module_clk_t clk_src, uint32_t expt_f
|
||||
return ret;
|
||||
}
|
||||
|
||||
static _Atomic int16_t s_pll_src_cg_ref_cnt[SOC_MOD_CLK_INVALID] = { 0 };
|
||||
static bool s_clk_tree_initialized = false;
|
||||
static int16_t s_cpll_ref_cnt = 0;
|
||||
|
||||
void esp_clk_tree_initialize(void)
|
||||
{
|
||||
// TODO: IDF-15502
|
||||
/*soc_reset_reason_t rst_reason = esp_rom_get_reset_reason(0);
|
||||
if ((rst_reason == RESET_REASON_CPU_SW) || (rst_reason == RESET_REASON_CPU_MWDT) \
|
||||
|| (rst_reason == RESET_REASON_CPU_RWDT) || (rst_reason == RESET_REASON_CPU_JTAG) \
|
||||
|| (rst_reason == RESET_REASON_CPU_LOCKUP)) {
|
||||
s_clk_tree_initialized = true;
|
||||
return;
|
||||
}*/
|
||||
|
||||
// Power
|
||||
soc_cpu_clk_src_t cpu_clk_src_btld = clk_ll_cpu_get_src();
|
||||
if (cpu_clk_src_btld == SOC_CPU_CLK_SRC_CPLL) {
|
||||
s_cpll_ref_cnt++;
|
||||
} else if (cpu_clk_src_btld == SOC_CPU_CLK_SRC_PLL_F240M) {
|
||||
// TODO: IDF-15502
|
||||
s_pll_src_cg_ref_cnt[SOC_MOD_CLK_PLL_F240M] = 1;
|
||||
soc_reset_reason_t rst_reason = esp_rom_get_reset_reason(0);
|
||||
soc_periph_flash_clk_src_t flash_clk_src = _mspi_timing_ll_get_flash_clk_src(MSPI_TIMING_LL_MSPI_ID_0);
|
||||
soc_cpu_clk_src_t cpu_src = clk_ll_cpu_get_src();
|
||||
bool cpu_reset = (rst_reason == RESET_REASON_CPU_SW) || (rst_reason == RESET_REASON_CPU_MWDT) ||
|
||||
(rst_reason == RESET_REASON_CPU_RWDT) || (rst_reason == RESET_REASON_CPU_JTAG) ||
|
||||
(rst_reason == RESET_REASON_CPU_LOCKUP);
|
||||
if (!cpu_reset) {
|
||||
/* Cold boot only: gate / power-down clocks not in use. */
|
||||
if (cpu_src != SOC_CPU_CLK_SRC_PLL_F240M) {
|
||||
_clk_gate_ll_ref_240m_clk_en(false);
|
||||
}
|
||||
if (cpu_src != SOC_CPU_CLK_SRC_CPLL && flash_clk_src != FLASH_CLK_SRC_CPLL) {
|
||||
clk_ll_cpll_disable();
|
||||
}
|
||||
_clk_gate_ll_ref_160m_clk_en(false);
|
||||
_clk_gate_ll_ref_120m_clk_en(false);
|
||||
_clk_gate_ll_ref_80m_clk_en(false);
|
||||
_clk_gate_ll_ref_60m_clk_en(false);
|
||||
_clk_gate_ll_ref_20m_clk_en(false);
|
||||
_clk_gate_ll_ref_50m_clk_en(false);
|
||||
_clk_gate_ll_ref_25m_clk_en(false);
|
||||
clk_ll_xtalx2_disable();
|
||||
HP_ALIVE_SYS.hp_clk_ctrl.hp_audio_pll_clk_en = 0;
|
||||
HP_ALIVE_SYS.hp_clk_ctrl.hp_sdio_pll2_clk_en = 0;
|
||||
HP_ALIVE_SYS.hp_clk_ctrl.hp_sdio_pll1_clk_en = 0;
|
||||
HP_ALIVE_SYS.hp_clk_ctrl.hp_sdio_pll0_clk_en = 0;
|
||||
}
|
||||
|
||||
// flash clock source is set to BBPLL in bootloader
|
||||
s_pll_src_cg_ref_cnt[SOC_MOD_CLK_BBPLL] = 1;
|
||||
|
||||
// Gating: disable all PLL-derived reference clocks; they will be re-enabled on demand via esp_clk_tree_enable_src
|
||||
// TODO: IDF-15502
|
||||
//_clk_gate_ll_ref_20m_clk_en(false);
|
||||
//_clk_gate_ll_ref_25m_clk_en(false);
|
||||
//_clk_gate_ll_ref_50m_clk_en(false);
|
||||
//_clk_gate_ll_ref_80m_clk_en(false);
|
||||
//_clk_gate_ll_ref_160m_clk_en(false);
|
||||
//if (s_pll_src_cg_ref_cnt[SOC_MOD_CLK_PLL_F240M] == 0) _clk_gate_ll_ref_240m_clk_en(false);
|
||||
s_clk_tree_initialized = true;
|
||||
#if CONFIG_USJ_ENABLE_USB_SERIAL_JTAG || CONFIG_ESP_CONSOLE_USB_SERIAL_JTAG_ENABLED
|
||||
/* Bootloader / USJ may keep BBPLL 480M on; declare a permanent hold. */
|
||||
esp_clk_tree_enable_src(SOC_MOD_CLK_BBPLL, true);
|
||||
#endif
|
||||
/* Flash + CPU: sync clk_tree refs with HW already selected at boot. */
|
||||
esp_clk_tree_enable_src((soc_module_clk_t)flash_clk_src, true);
|
||||
if (cpu_src == SOC_CPU_CLK_SRC_CPLL) {
|
||||
esp_clk_tree_enable_src(SOC_MOD_CLK_CPLL, true);
|
||||
} else if (cpu_src == SOC_CPU_CLK_SRC_PLL_F240M) {
|
||||
esp_clk_tree_enable_src(SOC_MOD_CLK_PLL_F240M, true);
|
||||
}
|
||||
}
|
||||
|
||||
bool esp_clk_tree_enable_power(soc_root_clk_circuit_t clk_circuit, bool enable)
|
||||
{
|
||||
if (clk_circuit >= SOC_ROOT_CIRCUIT_CLK_MAX) {
|
||||
return false;
|
||||
}
|
||||
|
||||
bool toggled = false;
|
||||
switch (clk_circuit) {
|
||||
case SOC_ROOT_CIRCUIT_CLK_CPLL:
|
||||
case SOC_ROOT_CIRCUIT_CLK_BBPLL:
|
||||
case SOC_ROOT_CIRCUIT_CLK_XTAL_X2: {
|
||||
if (enable) {
|
||||
s_cpll_ref_cnt++;
|
||||
toggled = (esp_clk_tree_root_pll_power_acquire(clk_circuit) == 0);
|
||||
} else {
|
||||
s_cpll_ref_cnt--;
|
||||
toggled = (esp_clk_tree_root_pll_power_release(clk_circuit) == 1);
|
||||
}
|
||||
|
||||
// Note that a calibration is usually needed after enabling CPLL
|
||||
if (s_cpll_ref_cnt == 1) {
|
||||
clk_ll_cpll_enable();
|
||||
toggled = true;
|
||||
} else if (s_cpll_ref_cnt == 0) {
|
||||
clk_ll_cpll_disable();
|
||||
toggled = true;
|
||||
}
|
||||
|
||||
assert(s_cpll_ref_cnt >= 0);
|
||||
break;
|
||||
}
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return toggled;
|
||||
}
|
||||
|
||||
#define ENABLE_CLK_GATE(clk_src_en_func, enable) \
|
||||
PERIPH_RCC_ATOMIC() { \
|
||||
clk_src_en_func(enable); \
|
||||
bool esp_clk_tree_port_is_power_on(soc_root_clk_circuit_t clk_circuit)
|
||||
{
|
||||
if (clk_circuit >= SOC_ROOT_CIRCUIT_CLK_MAX) {
|
||||
return false;
|
||||
}
|
||||
if (clk_circuit == SOC_ROOT_CIRCUIT_CLK_BBPLL || clk_circuit == SOC_ROOT_CIRCUIT_CLK_CPLL
|
||||
|| clk_circuit == SOC_ROOT_CIRCUIT_CLK_XTAL_X2) {
|
||||
int16_t cnt;
|
||||
|
||||
esp_os_enter_critical(&s_clk_tree_spinlock);
|
||||
cnt = s_root_pll_power_ref_cnt[clk_circuit];
|
||||
esp_os_exit_critical(&s_clk_tree_spinlock);
|
||||
return cnt > 0;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
typedef enum {
|
||||
ESP_CLK_TREE_GATED_CLK_RC_FAST,
|
||||
ESP_CLK_TREE_GATED_CLK_PLL_F20M,
|
||||
ESP_CLK_TREE_GATED_CLK_PLL_F60M,
|
||||
ESP_CLK_TREE_GATED_CLK_PLL_F120M,
|
||||
ESP_CLK_TREE_GATED_CLK_PLL_F160M,
|
||||
ESP_CLK_TREE_GATED_CLK_PLL_F240M,
|
||||
ESP_CLK_TREE_GATED_CLK_NUM,
|
||||
} esp_clk_tree_gated_clk_id_t;
|
||||
|
||||
static const esp_clk_tree_gated_clk_t s_gated_ref_clks[] = {
|
||||
[ESP_CLK_TREE_GATED_CLK_RC_FAST] = { SOC_MOD_CLK_RC_FAST, NULL, esp_clk_tree_parent_rc_fast },
|
||||
[ESP_CLK_TREE_GATED_CLK_PLL_F20M] = { SOC_MOD_CLK_PLL_F20M, _clk_gate_ll_ref_20m_clk_en, esp_clk_tree_parent_bbpll },
|
||||
[ESP_CLK_TREE_GATED_CLK_PLL_F60M] = { SOC_MOD_CLK_PLL_F60M, _clk_gate_ll_ref_60m_clk_en, esp_clk_tree_parent_bbpll },
|
||||
[ESP_CLK_TREE_GATED_CLK_PLL_F120M] = { SOC_MOD_CLK_PLL_F120M, _clk_gate_ll_ref_120m_clk_en, esp_clk_tree_parent_bbpll },
|
||||
[ESP_CLK_TREE_GATED_CLK_PLL_F160M] = { SOC_MOD_CLK_PLL_F160M, _clk_gate_ll_ref_160m_clk_en, esp_clk_tree_parent_bbpll },
|
||||
[ESP_CLK_TREE_GATED_CLK_PLL_F240M] = { SOC_MOD_CLK_PLL_F240M, _clk_gate_ll_ref_240m_clk_en, esp_clk_tree_parent_bbpll },
|
||||
};
|
||||
|
||||
#define ENABLE_CLK_GATE(clk_src_en_func, enable) \
|
||||
do { \
|
||||
if ((clk_src_en_func) != NULL) { \
|
||||
PERIPH_RCC_ATOMIC() { \
|
||||
(clk_src_en_func)(enable); \
|
||||
}; \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
FORCE_INLINE_ATTR esp_err_t esp_clk_tree_enable_gated_clk(const esp_clk_tree_gated_clk_t *entry, bool enable)
|
||||
{
|
||||
int16_t prev_ref_cnt;
|
||||
bool released_too_many = false;
|
||||
|
||||
esp_os_enter_critical(&s_clk_tree_spinlock);
|
||||
if (enable) {
|
||||
prev_ref_cnt = s_mod_clk_gate_ref_cnt[entry->clk_id]++;
|
||||
if (prev_ref_cnt == 0) {
|
||||
if (entry->parent_power != NULL) {
|
||||
entry->parent_power(true);
|
||||
}
|
||||
ENABLE_CLK_GATE(entry->set_gate, true);
|
||||
}
|
||||
} else {
|
||||
prev_ref_cnt = s_mod_clk_gate_ref_cnt[entry->clk_id]--;
|
||||
if (prev_ref_cnt <= 0) {
|
||||
s_mod_clk_gate_ref_cnt[entry->clk_id] = 0;
|
||||
released_too_many = true;
|
||||
} else if (prev_ref_cnt == 1) {
|
||||
ENABLE_CLK_GATE(entry->set_gate, false);
|
||||
if (entry->parent_power != NULL) {
|
||||
entry->parent_power(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
esp_os_exit_critical(&s_clk_tree_spinlock);
|
||||
|
||||
if (released_too_many) {
|
||||
ESP_LOGW(TAG, "soc_module_clk_t %d disabled multiple times!!", entry->clk_id);
|
||||
}
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
esp_err_t esp_clk_tree_enable_src(soc_module_clk_t clk_src, bool enable)
|
||||
{
|
||||
if (clk_src < 1 || clk_src >= SOC_MOD_CLK_INVALID) {
|
||||
// some conditions is legal, e.g. -1 means external clock source
|
||||
if (clk_src < 1 || clk_src >= SOC_MOD_CLK_INVALID || clk_src == SOC_MOD_CLK_XTAL) {
|
||||
/* Not managed by esp_clk_tree*/
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
@@ -241,59 +588,42 @@ esp_err_t esp_clk_tree_enable_src(soc_module_clk_t clk_src, bool enable)
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
int16_t prev_ref_cnt = 0;
|
||||
|
||||
// Derived PLL clocks (PLL_F50M, ...) that participate in the shared
|
||||
// refcount/lock engine route through that engine instead of the
|
||||
// global s_pll_src_cg_ref_cnt array below.
|
||||
if (esp_clk_tree_get_derived_clk_desc(clk_src) != NULL) {
|
||||
return enable ? esp_clk_tree_derived_clk_acquire(clk_src)
|
||||
: esp_clk_tree_derived_clk_release(clk_src);
|
||||
}
|
||||
|
||||
esp_clk_tree_gated_clk_id_t gated_clk_id;
|
||||
// these clock sources have their own reference counting
|
||||
switch (clk_src) {
|
||||
case SOC_MOD_CLK_APLL:
|
||||
if (enable) {
|
||||
esp_clk_tree_apll_acquire();
|
||||
} else {
|
||||
esp_clk_tree_apll_release();
|
||||
}
|
||||
return ESP_OK;
|
||||
case SOC_MOD_CLK_MPLL:
|
||||
if (enable) {
|
||||
return esp_clk_tree_mpll_acquire();
|
||||
} else {
|
||||
esp_clk_tree_mpll_release();
|
||||
}
|
||||
return ESP_OK;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
// other clock sources use the global reference counting
|
||||
if (enable) {
|
||||
prev_ref_cnt = atomic_fetch_add(&s_pll_src_cg_ref_cnt[clk_src], 1);
|
||||
} else {
|
||||
prev_ref_cnt = atomic_fetch_sub(&s_pll_src_cg_ref_cnt[clk_src], 1);
|
||||
if (prev_ref_cnt <= 0) {
|
||||
ESP_EARLY_LOGW(TAG, "soc_module_clk_t %d disabled multiple times!!", clk_src);
|
||||
atomic_store(&s_pll_src_cg_ref_cnt[clk_src], 0);
|
||||
case SOC_MOD_CLK_APLL:
|
||||
if (enable) {
|
||||
esp_clk_tree_apll_acquire();
|
||||
} else {
|
||||
esp_clk_tree_apll_release();
|
||||
}
|
||||
return ESP_OK;
|
||||
case SOC_MOD_CLK_MPLL:
|
||||
if (enable) {
|
||||
return esp_clk_tree_mpll_acquire();
|
||||
} else {
|
||||
esp_clk_tree_mpll_release();
|
||||
return ESP_OK;
|
||||
}
|
||||
case SOC_MOD_CLK_BBPLL:
|
||||
esp_clk_tree_enable_power(SOC_ROOT_CIRCUIT_CLK_BBPLL, enable);
|
||||
return ESP_OK;
|
||||
case SOC_MOD_CLK_CPLL:
|
||||
esp_clk_tree_enable_power(SOC_ROOT_CIRCUIT_CLK_CPLL, enable);
|
||||
return ESP_OK;
|
||||
case SOC_MOD_CLK_RC_FAST: gated_clk_id = ESP_CLK_TREE_GATED_CLK_RC_FAST; break;
|
||||
case SOC_MOD_CLK_PLL_F20M: gated_clk_id = ESP_CLK_TREE_GATED_CLK_PLL_F20M; break;
|
||||
case SOC_MOD_CLK_PLL_F60M: gated_clk_id = ESP_CLK_TREE_GATED_CLK_PLL_F60M; break;
|
||||
case SOC_MOD_CLK_PLL_F120M: gated_clk_id = ESP_CLK_TREE_GATED_CLK_PLL_F120M; break;
|
||||
case SOC_MOD_CLK_PLL_F160M: gated_clk_id = ESP_CLK_TREE_GATED_CLK_PLL_F160M; break;
|
||||
case SOC_MOD_CLK_PLL_F240M: gated_clk_id = ESP_CLK_TREE_GATED_CLK_PLL_F240M; break;
|
||||
default:
|
||||
// Derived PLL clocks (PLL_F25M/F50M/F80M) use the shared derived-clk engine.
|
||||
if (esp_clk_tree_get_derived_clk_desc(clk_src) != NULL) {
|
||||
return enable ? esp_clk_tree_derived_clk_acquire(clk_src)
|
||||
: esp_clk_tree_derived_clk_release(clk_src);
|
||||
}
|
||||
return ESP_OK;
|
||||
}
|
||||
// TODO: IDF-15502
|
||||
//if ((prev_ref_cnt == 0 && enable) || (prev_ref_cnt == 1 && !enable)) {
|
||||
// switch (clk_src) {
|
||||
// case SOC_MOD_CLK_RC_FAST: enable ? rtc_dig_clk8m_enable() : rtc_dig_clk8m_disable(); break;
|
||||
// case SOC_MOD_CLK_PLL_F20M: ENABLE_CLK_GATE(clk_gate_ll_ref_20m_clk_en, enable); break;
|
||||
// case SOC_MOD_CLK_PLL_F25M: ENABLE_CLK_GATE(clk_gate_ll_ref_25m_clk_en, enable); break;
|
||||
// case SOC_MOD_CLK_PLL_F80M: ENABLE_CLK_GATE(clk_gate_ll_ref_80m_clk_en, enable); break;
|
||||
// case SOC_MOD_CLK_PLL_F160M: ENABLE_CLK_GATE(clk_gate_ll_ref_160m_clk_en, enable); break;
|
||||
// case SOC_MOD_CLK_PLL_F240M: ENABLE_CLK_GATE(clk_gate_ll_ref_240m_clk_en, enable); break;
|
||||
// default: break;
|
||||
// }
|
||||
//}
|
||||
|
||||
return ESP_OK;
|
||||
return esp_clk_tree_enable_gated_clk(&s_gated_ref_clks[gated_clk_id], enable);
|
||||
}
|
||||
|
||||
@@ -88,6 +88,10 @@ typedef struct {
|
||||
uint32_t slow_clk_dcap : 8; //!< RC_SLOW clock adjustment parameter (higher value leads to lower frequency)
|
||||
uint32_t clk_8m_dfreq : 8; //!< RC_FAST clock adjustment parameter (higher value leads to higher frequency)
|
||||
uint32_t rc32k_dfreq : 10; //!< Internal RC32K clock adjustment parameter (higher value leads to higher frequency)
|
||||
uint32_t disable_apll : 1; //!< Whether to disable APLL in rtc_clk_init
|
||||
uint32_t disable_mpll : 1; //!< Whether to disable MPLL in rtc_clk_init
|
||||
uint32_t disable_cpll : 1; //!< Whether to disable CPLL in rtc_clk_init
|
||||
uint32_t disable_bbpll : 1; //!< Whether to disable BBPLL in rtc_clk_init
|
||||
} rtc_clk_config_t;
|
||||
|
||||
/**
|
||||
@@ -103,6 +107,10 @@ typedef struct {
|
||||
.slow_clk_dcap = RTC_CNTL_SCK_DCAP_DEFAULT, \
|
||||
.clk_8m_dfreq = RTC_CNTL_CK8M_DFREQ_DEFAULT, \
|
||||
.rc32k_dfreq = RTC_CNTL_RC32K_DFREQ_DEFAULT, \
|
||||
.disable_apll = 1, \
|
||||
.disable_mpll = 1, \
|
||||
.disable_cpll = 0, /* Keep CPLL: Flash (bootloader) and typical CPU freqs use it */ \
|
||||
.disable_bbpll = 1, \
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -225,6 +233,7 @@ bool rtc_clk_cpu_freq_mhz_to_config(uint32_t freq_mhz, rtc_cpu_freq_config_t *ou
|
||||
*/
|
||||
void rtc_clk_cpu_freq_set_config(const rtc_cpu_freq_config_t *config);
|
||||
|
||||
#ifndef BOOTLOADER_BUILD
|
||||
/**
|
||||
* @brief Switch CPU frequency (optimized for speed)
|
||||
*
|
||||
@@ -246,6 +255,7 @@ void rtc_clk_cpu_freq_set_config(const rtc_cpu_freq_config_t *config);
|
||||
* @param config CPU frequency configuration structure
|
||||
*/
|
||||
void rtc_clk_cpu_freq_set_config_fast(const rtc_cpu_freq_config_t *config);
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @brief Get the currently used CPU frequency configuration
|
||||
@@ -253,17 +263,16 @@ void rtc_clk_cpu_freq_set_config_fast(const rtc_cpu_freq_config_t *config);
|
||||
*/
|
||||
void rtc_clk_cpu_freq_get_config(rtc_cpu_freq_config_t *out_config);
|
||||
|
||||
#ifndef BOOTLOADER_BUILD
|
||||
/**
|
||||
* @brief Switch CPU clock source to XTAL
|
||||
*
|
||||
* Short form for filling in rtc_cpu_freq_config_t structure and calling
|
||||
* rtc_clk_cpu_freq_set_config when a switch to XTAL is needed.
|
||||
* Assumes that XTAL frequency has been determined — don't call in startup code.
|
||||
*
|
||||
* @note On ESP32S31, this function always disables CPLL after switching the CPU clock source to XTAL,
|
||||
* since there is no peripheral relies on CPLL clock (except Flash/PSRAM if their clock source selects CPLL).
|
||||
*/
|
||||
void rtc_clk_cpu_freq_set_xtal(void);
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @brief Get the current APB frequency.
|
||||
|
||||
@@ -67,7 +67,13 @@ void pmu_hp_system_init(pmu_context_t *ctx, pmu_hp_mode_t mode, pmu_hp_system_pa
|
||||
assert(ctx->hal);
|
||||
/* Default configuration of hp-system power in active, modem and sleep modes */
|
||||
pmu_ll_hp_set_dig_power(ctx->hal->dev, mode, power->dig_power.val);
|
||||
pmu_ll_hp_set_clk_power(ctx->hal->dev, mode, power->clk_power.val);
|
||||
if (mode == PMU_MODE_HP_ACTIVE) {
|
||||
// In active mode the root clock circuit power (BBPLL/CPLL/MPLL/APLL/XTALx2, etc.) is owned by esp_clk_tree.
|
||||
// The analog i2c master is shared by all the PLLs and is not refcounted there, so it is still configured here.
|
||||
pmu_ll_hp_set_ana_i2c_power(ctx->hal->dev, mode, power->clk_power.xpd_bb_i2c, power->clk_power.i2c_iso_en, power->clk_power.i2c_retention);
|
||||
} else {
|
||||
pmu_ll_hp_set_clk_power(ctx->hal->dev, mode, power->clk_power.val);
|
||||
}
|
||||
pmu_ll_hp_set_xtal_xpd (ctx->hal->dev, mode, power->xtal.xpd_xtal);
|
||||
|
||||
/* Default configuration of hp-system clock in active, modem and sleep modes */
|
||||
|
||||
@@ -36,6 +36,7 @@ ESP_HW_LOG_ATTR_TAG(TAG, "pmu_param");
|
||||
.top_pd_en = 0 \
|
||||
}, \
|
||||
.clk_power = { \
|
||||
.xpd_xtalx2 = 0, \
|
||||
.i2c_iso_en = 0, \
|
||||
.i2c_retention = 0, \
|
||||
.xpd_bb_i2c = 1, \
|
||||
|
||||
@@ -20,17 +20,24 @@
|
||||
#include "esp_private/sleep_event.h"
|
||||
#include "esp_private/regi2c_ctrl.h"
|
||||
#include "esp_attr.h"
|
||||
#include "esp_private/esp_clk_tree_common.h"
|
||||
#include "hal/clk_gate_ll.h"
|
||||
|
||||
static const char *TAG = "rtc_clk";
|
||||
|
||||
// CPLL frequency option, in 320MHz. Zero if CPLL is not enabled.
|
||||
#ifndef BOOTLOADER_BUILD
|
||||
// CPLL frequency option, in 320MHz. Zero if CPLL is not enabled / needs recalibration.
|
||||
static int s_cur_cpll_freq = 0;
|
||||
// BBPLL frequency option, in 480MHz. Zero if BBPLL is not enabled / needs recalibration.
|
||||
static int s_cur_bbpll_freq = 0;
|
||||
|
||||
// MPLL frequency option, 500MHz. Zero if MPLL is not enabled.
|
||||
static uint32_t s_cur_mpll_freq = 0;
|
||||
|
||||
#if !BOOTLOADER_BUILD
|
||||
// Indicate whether the specific clock sources are acquired by the hp root clock (i.e. whether ref_cnt in esp_clk_tree.c is incremented by the hp root clock)
|
||||
/**
|
||||
* Whether HP ROOT clock currently holds a clk_tree ref on CPLL / PLL_F240M.
|
||||
* Survives DFS set_config_fast(XTAL) (keep-hot) and light-sleep (PMU restores ACTIVE XPD on wake). Cleared only on real leave via set_config/set_xtal.
|
||||
*/
|
||||
static bool s_is_cpll_acquired = (CONFIG_BOOTLOADER_CPU_CLK_FREQ_MHZ == 80 || CONFIG_BOOTLOADER_CPU_CLK_FREQ_MHZ == 160 || CONFIG_BOOTLOADER_CPU_CLK_FREQ_MHZ == 320);
|
||||
static bool s_is_pll_f240m_acquired = (CONFIG_BOOTLOADER_CPU_CLK_FREQ_MHZ == 240);
|
||||
#endif
|
||||
@@ -122,19 +129,6 @@ soc_rtc_fast_clk_src_t rtc_clk_fast_src_get(void)
|
||||
return clk_ll_rtc_fast_get_src();
|
||||
}
|
||||
|
||||
#if BOOTLOADER_BUILD
|
||||
static void rtc_clk_cpll_disable(void)
|
||||
{
|
||||
clk_ll_cpll_disable();
|
||||
s_cur_cpll_freq = 0;
|
||||
}
|
||||
|
||||
static void rtc_clk_cpll_enable(void)
|
||||
{
|
||||
clk_ll_cpll_enable();
|
||||
}
|
||||
#endif
|
||||
|
||||
static void rtc_clk_cpll_configure(soc_xtal_freq_t xtal_freq, int cpll_freq)
|
||||
{
|
||||
/* Digital part */
|
||||
@@ -152,7 +146,9 @@ static void rtc_clk_cpll_configure(soc_xtal_freq_t xtal_freq, int cpll_freq)
|
||||
clk_ll_cpll_calibration_stop();
|
||||
ANALOG_CLOCK_DISABLE();
|
||||
|
||||
#ifndef BOOTLOADER_BUILD
|
||||
s_cur_cpll_freq = cpll_freq;
|
||||
#endif
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -340,59 +336,69 @@ __attribute__((weak)) void rtc_clk_set_cpu_switch_to_pll(int event_id)
|
||||
{
|
||||
}
|
||||
|
||||
static void rtc_clk_cpu_src_clk_enable(soc_cpu_clk_src_t new_src, uint32_t new_src_freq_mhz)
|
||||
static void rtc_clk_update_pll_state_on_cpu_src_switching_start(soc_cpu_clk_src_t new_src, uint32_t new_src_freq_mhz)
|
||||
{
|
||||
#ifdef BOOTLOADER_BUILD
|
||||
if (new_src == SOC_CPU_CLK_SRC_CPLL) {
|
||||
bool truly_enabled = false;
|
||||
#if BOOTLOADER_BUILD
|
||||
rtc_clk_cpll_enable();
|
||||
truly_enabled = true;
|
||||
clk_ll_cpll_enable();
|
||||
rtc_clk_cpll_configure(rtc_clk_xtal_freq_get(), new_src_freq_mhz);
|
||||
} else if (new_src == SOC_CPU_CLK_SRC_PLL_F240M) {
|
||||
clk_ll_bbpll_enable();
|
||||
_clk_gate_ll_ref_240m_clk_en(true);
|
||||
}
|
||||
#else
|
||||
if (new_src == SOC_CPU_CLK_SRC_CPLL) {
|
||||
bool need_configure = false;
|
||||
if (!s_is_cpll_acquired) {
|
||||
truly_enabled = esp_clk_tree_enable_power(SOC_ROOT_CIRCUIT_CLK_CPLL, true);
|
||||
need_configure = !esp_clk_tree_is_power_on(SOC_ROOT_CIRCUIT_CLK_CPLL);
|
||||
esp_clk_tree_enable_src(SOC_MOD_CLK_CPLL, true);
|
||||
s_is_cpll_acquired = true;
|
||||
}
|
||||
#endif
|
||||
if (truly_enabled || (s_cur_cpll_freq != new_src_freq_mhz)) {
|
||||
if (need_configure || (s_cur_cpll_freq != (int)new_src_freq_mhz)) {
|
||||
rtc_clk_cpll_configure(rtc_clk_xtal_freq_get(), new_src_freq_mhz);
|
||||
}
|
||||
} else if (new_src == SOC_CPU_CLK_SRC_PLL_F240M) {
|
||||
#if !BOOTLOADER_BUILD
|
||||
if (!s_is_pll_f240m_acquired) {
|
||||
esp_clk_tree_enable_src(SOC_MOD_CLK_PLL_F240M, true);
|
||||
s_is_pll_f240m_acquired = true;
|
||||
}
|
||||
#endif
|
||||
s_cur_bbpll_freq = CLK_LL_PLL_480M_FREQ_MHZ;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
static void rtc_clk_cpu_src_clk_disable(soc_cpu_clk_src_t old_src)
|
||||
#ifndef BOOTLOADER_BUILD
|
||||
static void rtc_clk_update_pll_state_on_cpu_src_switching_end(soc_cpu_clk_src_t old_src)
|
||||
{
|
||||
if (old_src == SOC_CPU_CLK_SRC_CPLL) {
|
||||
#if BOOTLOADER_BUILD
|
||||
rtc_clk_cpll_disable();
|
||||
#else
|
||||
assert(s_is_cpll_acquired);
|
||||
bool truly_disabled = esp_clk_tree_enable_power(SOC_ROOT_CIRCUIT_CLK_CPLL, false);
|
||||
esp_clk_tree_enable_src(SOC_MOD_CLK_CPLL, false);
|
||||
s_is_cpll_acquired = false;
|
||||
if (truly_disabled) {
|
||||
if (!esp_clk_tree_is_power_on(SOC_ROOT_CIRCUIT_CLK_CPLL)) {
|
||||
s_cur_cpll_freq = 0;
|
||||
}
|
||||
#endif
|
||||
} else if (old_src == SOC_CPU_CLK_SRC_PLL_F240M) {
|
||||
#if !BOOTLOADER_BUILD
|
||||
assert(s_is_pll_f240m_acquired);
|
||||
s_is_pll_f240m_acquired = false;
|
||||
esp_clk_tree_enable_src(SOC_MOD_CLK_PLL_F240M, false);
|
||||
#endif
|
||||
if (!esp_clk_tree_is_power_on(SOC_ROOT_CIRCUIT_CLK_BBPLL)) {
|
||||
s_cur_bbpll_freq = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
void rtc_clk_cpu_freq_set_config(const rtc_cpu_freq_config_t *config)
|
||||
{
|
||||
#ifdef BOOTLOADER_BUILD
|
||||
// Always trigger clock source preparing in bootloader
|
||||
bool src_changed = true;
|
||||
#else
|
||||
soc_cpu_clk_src_t old_cpu_clk_src = clk_ll_cpu_get_src();
|
||||
if (old_cpu_clk_src != config->source) {
|
||||
rtc_clk_cpu_src_clk_enable(config->source, config->source_freq_mhz);
|
||||
bool src_changed = (old_cpu_clk_src != config->source);
|
||||
#endif
|
||||
if (src_changed) {
|
||||
rtc_clk_update_pll_state_on_cpu_src_switching_start(config->source, config->source_freq_mhz);
|
||||
}
|
||||
|
||||
if (config->source == SOC_CPU_CLK_SRC_XTAL) {
|
||||
@@ -407,9 +413,11 @@ void rtc_clk_cpu_freq_set_config(const rtc_cpu_freq_config_t *config)
|
||||
rtc_clk_cpu_freq_to_rc_fast();
|
||||
}
|
||||
|
||||
if (old_cpu_clk_src != config->source) {
|
||||
rtc_clk_cpu_src_clk_disable(old_cpu_clk_src);
|
||||
#ifndef BOOTLOADER_BUILD
|
||||
if (src_changed) {
|
||||
rtc_clk_update_pll_state_on_cpu_src_switching_end(old_cpu_clk_src);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
static uint32_t rtc_clk_hp_root_get_freq_mhz(soc_cpu_clk_src_t clk_src)
|
||||
@@ -459,53 +467,6 @@ void rtc_clk_cpu_freq_get_config(rtc_cpu_freq_config_t *out_config)
|
||||
};
|
||||
}
|
||||
|
||||
void rtc_clk_cpu_freq_set_config_fast(const rtc_cpu_freq_config_t *config)
|
||||
{
|
||||
if (config->source == SOC_CPU_CLK_SRC_XTAL) {
|
||||
rtc_clk_cpu_freq_to_xtal(config->freq_mhz, config->div.integer, false);
|
||||
} else if (config->source == SOC_CPU_CLK_SRC_CPLL &&
|
||||
s_cur_cpll_freq == config->source_freq_mhz) {
|
||||
rtc_clk_cpu_freq_to_cpll_mhz(config->freq_mhz, (hal_utils_clk_div_t *)&config->div);
|
||||
} else if (config->source == SOC_CPU_CLK_SRC_RC_FAST) {
|
||||
rtc_clk_cpu_freq_to_rc_fast();
|
||||
} else if (config->source == SOC_CPU_CLK_SRC_PLL_F240M
|
||||
#if !BOOTLOADER_BUILD
|
||||
&& s_is_pll_f240m_acquired
|
||||
#endif
|
||||
) {
|
||||
rtc_clk_cpu_freq_to_pll_240_mhz(config->freq_mhz);
|
||||
} else {
|
||||
/* fallback */
|
||||
rtc_clk_cpu_freq_set_config(config);
|
||||
}
|
||||
}
|
||||
|
||||
void rtc_clk_cpu_freq_set_xtal(void)
|
||||
{
|
||||
soc_cpu_clk_src_t old_cpu_clk_src = clk_ll_cpu_get_src();
|
||||
int freq_mhz = (int)rtc_clk_xtal_freq_get();
|
||||
|
||||
rtc_clk_cpu_freq_to_xtal(freq_mhz, 1, false);
|
||||
if (old_cpu_clk_src != SOC_CPU_CLK_SRC_XTAL) {
|
||||
rtc_clk_cpu_src_clk_disable(old_cpu_clk_src);
|
||||
}
|
||||
}
|
||||
|
||||
FORCE_IRAM_ATTR void rtc_clk_cpu_set_to_default_config(void)
|
||||
{
|
||||
int freq_mhz = (int)rtc_clk_xtal_freq_get();
|
||||
|
||||
rtc_clk_cpu_freq_to_xtal(freq_mhz, 1, true);
|
||||
}
|
||||
|
||||
void rtc_clk_cpu_freq_set_xtal_for_sleep(void)
|
||||
{
|
||||
int freq_mhz = (int)rtc_clk_xtal_freq_get();
|
||||
|
||||
rtc_clk_cpu_freq_to_xtal(freq_mhz, 1, false);
|
||||
s_cur_cpll_freq = 0; // no disable PLL, but set freq to 0 to trigger a PLL calibration after wake-up from sleep
|
||||
}
|
||||
|
||||
FORCE_IRAM_ATTR soc_xtal_freq_t rtc_clk_xtal_freq_get(void)
|
||||
{
|
||||
uint32_t xtal_freq_mhz = clk_ll_xtal_get_freq_mhz();
|
||||
@@ -528,6 +489,56 @@ uint32_t rtc_clk_apb_freq_get(void)
|
||||
return sys_freq_hz / clk_ll_apb_get_divider();
|
||||
}
|
||||
|
||||
#ifndef BOOTLOADER_BUILD
|
||||
void rtc_clk_cpu_freq_set_config_fast(const rtc_cpu_freq_config_t *config)
|
||||
{
|
||||
/* Mux only — Fall back to set_config when PLL must be reacquired or recalibrated (s_cur_* == 0 after sleep). */
|
||||
if (config->source == SOC_CPU_CLK_SRC_XTAL) {
|
||||
rtc_clk_cpu_freq_to_xtal(config->freq_mhz, config->div.integer, false);
|
||||
} else if (config->source == SOC_CPU_CLK_SRC_CPLL &&
|
||||
s_cur_cpll_freq == (int)config->source_freq_mhz) {
|
||||
rtc_clk_cpu_freq_to_cpll_mhz(config->freq_mhz, (hal_utils_clk_div_t *)&config->div);
|
||||
} else if (config->source == SOC_CPU_CLK_SRC_PLL_F240M &&
|
||||
s_is_pll_f240m_acquired &&
|
||||
s_cur_bbpll_freq == CLK_LL_PLL_480M_FREQ_MHZ) {
|
||||
rtc_clk_cpu_freq_to_pll_240_mhz(config->freq_mhz);
|
||||
} else if (config->source == SOC_CPU_CLK_SRC_RC_FAST) {
|
||||
rtc_clk_cpu_freq_to_rc_fast();
|
||||
} else {
|
||||
/* fallback */
|
||||
rtc_clk_cpu_freq_set_config(config);
|
||||
}
|
||||
}
|
||||
|
||||
void rtc_clk_cpu_freq_set_xtal(void)
|
||||
{
|
||||
soc_cpu_clk_src_t old_cpu_clk_src = clk_ll_cpu_get_src();
|
||||
int freq_mhz = (int)rtc_clk_xtal_freq_get();
|
||||
|
||||
rtc_clk_cpu_freq_to_xtal(freq_mhz, 1, false);
|
||||
#ifndef BOOTLOADER_BUILD
|
||||
rtc_clk_update_pll_state_on_cpu_src_switching_end(old_cpu_clk_src);
|
||||
#endif
|
||||
}
|
||||
|
||||
FORCE_IRAM_ATTR void rtc_clk_cpu_set_to_default_config(void)
|
||||
{
|
||||
int freq_mhz = (int)rtc_clk_xtal_freq_get();
|
||||
|
||||
rtc_clk_cpu_freq_to_xtal(freq_mhz, 1, true);
|
||||
}
|
||||
|
||||
void rtc_clk_cpu_freq_set_xtal_for_sleep(void)
|
||||
{
|
||||
int freq_mhz = (int)rtc_clk_xtal_freq_get();
|
||||
|
||||
/* Mux only — do not release CPU clk_tree hold. PMU restores ACTIVE XPD on
|
||||
* wake; clearing s_cur_* forces recalibration via set_config fallback. */
|
||||
rtc_clk_cpu_freq_to_xtal(freq_mhz, 1, false);
|
||||
s_cur_cpll_freq = 0;
|
||||
s_cur_bbpll_freq = 0;
|
||||
}
|
||||
|
||||
void rtc_clk_apll_enable(bool enable)
|
||||
{
|
||||
if (enable) {
|
||||
@@ -663,3 +674,4 @@ IRAM_ATTR uint32_t rtc_clk_mpll_get_freq(void)
|
||||
{
|
||||
return s_cur_mpll_freq;
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -8,6 +8,7 @@
|
||||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
#include <stdlib.h>
|
||||
#include "sdkconfig.h"
|
||||
#include "esp32s31/rom/ets_sys.h"
|
||||
#include "esp32s31/rom/rtc.h"
|
||||
#include "soc/rtc.h"
|
||||
@@ -27,6 +28,25 @@
|
||||
|
||||
ESP_HW_LOG_ATTR_TAG(TAG, "rtc_clk_init");
|
||||
|
||||
static inline void rtc_clk_pll_disable(rtc_clk_config_t cfg)
|
||||
{
|
||||
if (cfg.disable_apll) {
|
||||
clk_ll_apll_disable();
|
||||
}
|
||||
if (cfg.disable_mpll) {
|
||||
clk_ll_mpll_disable();
|
||||
}
|
||||
if (cfg.disable_cpll) {
|
||||
clk_ll_cpll_disable();
|
||||
}
|
||||
#if !CONFIG_USJ_ENABLE_USB_SERIAL_JTAG && !CONFIG_ESP_CONSOLE_USB_SERIAL_JTAG_ENABLED
|
||||
// USB-Serial-JTAG depends on bbpll480M, bypass disable bbpll if USB console is used.
|
||||
if (cfg.disable_bbpll) {
|
||||
clk_ll_bbpll_disable();
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
void rtc_clk_init(rtc_clk_config_t cfg)
|
||||
{
|
||||
rtc_cpu_freq_config_t old_config, new_config;
|
||||
@@ -50,6 +70,9 @@ void rtc_clk_init(rtc_clk_config_t cfg)
|
||||
|
||||
// No need to wait UART0 TX idle since its default clock source is XTAL, should not be affected by system clock configuration
|
||||
|
||||
/* Disable PLLs to save power, the PLLs will be enabled by the user in application code */
|
||||
rtc_clk_pll_disable(cfg);
|
||||
|
||||
/* Set CPU frequency */
|
||||
rtc_clk_cpu_freq_get_config(&old_config);
|
||||
uint32_t freq_before = old_config.freq_mhz;
|
||||
|
||||
@@ -144,6 +144,9 @@ uint32_t rtc_clk_cal_internal(soc_clk_freq_calculation_src_t cal_clk_sel, uint32
|
||||
}
|
||||
CLEAR_PERI_REG_MASK(TIMG_RTCCALICFG_REG(0), TIMG_RTC_CALI_START);
|
||||
clk_ll_freq_calculation_set_divider(1);
|
||||
// Back to always on clock source, Otherwise, if the source for this calibration is subsequently turned off,
|
||||
// the next calibration will not be able to switch to the new calibration source.
|
||||
clk_ll_freq_calulation_set_target(CLK_CAL_RC_SLOW);
|
||||
|
||||
/* if dig_32k_xtal was originally off and enabled due to calibration, then set back to off state */
|
||||
if (cal_clk_sel == CLK_CAL_32K_XTAL && !dig_32k_xtal_enabled) {
|
||||
|
||||
@@ -409,8 +409,8 @@ end:
|
||||
* dispatchers route into.
|
||||
*
|
||||
* Semantics:
|
||||
* - acquire: ref_cnt++; on first acquire enable the gate.
|
||||
* - release: ref_cnt--; on last release disable the gate.
|
||||
* - acquire: ref_cnt++; on first acquire acquire_parent then enable the gate.
|
||||
* - release: ref_cnt--; on last release disable the gate and release_parent.
|
||||
* - select_upstream: program the mux to source from `upstream`.
|
||||
* - freq_set: pick a divider for `state->cur_upstream` (if `select_upstream`
|
||||
* was called) or auto-pick an upstream that divides cleanly
|
||||
@@ -436,14 +436,21 @@ esp_err_t esp_clk_tree_derived_clk_acquire(soc_module_clk_t clk_src)
|
||||
return ESP_ERR_NOT_SUPPORTED;
|
||||
}
|
||||
esp_clk_tree_derived_clk_state_t *state = desc->state;
|
||||
|
||||
esp_err_t ret = ESP_OK;
|
||||
esp_os_enter_critical(&s_derived_clk_spinlock);
|
||||
state->ref_cnt++;
|
||||
if (state->ref_cnt == 1) {
|
||||
desc->set_gate(true);
|
||||
if (desc->acquire_parent != NULL) {
|
||||
ret = desc->acquire_parent();
|
||||
}
|
||||
if (ret == ESP_OK) {
|
||||
desc->set_gate(true);
|
||||
} else {
|
||||
state->ref_cnt--;
|
||||
}
|
||||
}
|
||||
esp_os_exit_critical(&s_derived_clk_spinlock);
|
||||
return ESP_OK;
|
||||
return ret;
|
||||
}
|
||||
|
||||
esp_err_t esp_clk_tree_derived_clk_release(soc_module_clk_t clk_src)
|
||||
@@ -455,6 +462,7 @@ esp_err_t esp_clk_tree_derived_clk_release(soc_module_clk_t clk_src)
|
||||
esp_clk_tree_derived_clk_state_t *state = desc->state;
|
||||
|
||||
bool released_too_many = false;
|
||||
esp_err_t ret = ESP_OK;
|
||||
esp_os_enter_critical(&s_derived_clk_spinlock);
|
||||
if (state->ref_cnt <= 0) {
|
||||
state->ref_cnt = 0;
|
||||
@@ -465,13 +473,17 @@ esp_err_t esp_clk_tree_derived_clk_release(soc_module_clk_t clk_src)
|
||||
desc->set_gate(false);
|
||||
state->cur_upstream = SOC_MOD_CLK_INVALID;
|
||||
state->cur_divider = 0;
|
||||
if (desc->release_parent != NULL) {
|
||||
ret = desc->release_parent();
|
||||
}
|
||||
}
|
||||
}
|
||||
esp_os_exit_critical(&s_derived_clk_spinlock);
|
||||
if (released_too_many) {
|
||||
ESP_HW_LOGW(TAG, "derived clk %d released without matching acquire", (int)clk_src);
|
||||
return ESP_OK;
|
||||
}
|
||||
return ESP_OK;
|
||||
return ret;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -521,6 +533,7 @@ esp_err_t esp_clk_tree_derived_clk_freq_set(soc_module_clk_t clk_src,
|
||||
if (desc == NULL || desc->state == NULL || desc->upstreams == NULL) {
|
||||
return ESP_ERR_NOT_SUPPORTED;
|
||||
}
|
||||
|
||||
esp_clk_tree_derived_clk_state_t *state = desc->state;
|
||||
ESP_RETURN_ON_FALSE(expt_freq_hz > 0, ESP_ERR_INVALID_ARG, TAG, "freq must be > 0");
|
||||
|
||||
@@ -551,32 +564,49 @@ esp_err_t esp_clk_tree_derived_clk_freq_set(soc_module_clk_t clk_src,
|
||||
// (e.g. another peer ran enable_src already but hasn't called freq_set
|
||||
// yet). Mirrors MPLL's `cur == 0 || ref_cnt < 2` check.
|
||||
bool first_commit = (state->cur_divider == 0);
|
||||
bool upstream_changed = false;
|
||||
if (state->ref_cnt < 2 || same_config || first_commit) {
|
||||
// First-time configuration: also program the mux. When the caller
|
||||
// already invoked `select_upstream`, `state->cur_upstream` already
|
||||
// matches `upstream` so the mux is left untouched.
|
||||
if (state->cur_upstream != upstream && desc->set_src != NULL) {
|
||||
desc->set_src(mux_sel);
|
||||
if (state->cur_upstream != upstream) {
|
||||
upstream_changed = true;
|
||||
if (desc->set_src != NULL) {
|
||||
desc->set_src(mux_sel);
|
||||
}
|
||||
}
|
||||
if (desc->set_divider != NULL) {
|
||||
desc->set_divider(divider);
|
||||
}
|
||||
desc->set_divider(divider);
|
||||
state->cur_upstream = upstream;
|
||||
state->cur_divider = divider;
|
||||
} else {
|
||||
ret = ESP_ERR_INVALID_STATE;
|
||||
}
|
||||
if (ret == ESP_OK && upstream_changed && desc->acquire_parent != NULL) {
|
||||
ret = desc->acquire_parent();
|
||||
}
|
||||
esp_os_exit_critical(&s_derived_clk_spinlock);
|
||||
reported_upstream = state->cur_upstream;
|
||||
reported_divider = state->cur_divider;
|
||||
|
||||
if (real_freq_hz != NULL) {
|
||||
uint32_t up_hz = 0;
|
||||
if (reported_upstream != SOC_MOD_CLK_INVALID && reported_divider != 0 &&
|
||||
esp_clk_tree_src_get_freq_hz(reported_upstream,
|
||||
ESP_CLK_TREE_SRC_FREQ_PRECISION_APPROX,
|
||||
&up_hz) == ESP_OK) {
|
||||
*real_freq_hz = up_hz / reported_divider;
|
||||
if (desc->set_divider == NULL) {
|
||||
if (esp_clk_tree_src_get_freq_hz(clk_src,
|
||||
ESP_CLK_TREE_SRC_FREQ_PRECISION_APPROX,
|
||||
real_freq_hz) != ESP_OK) {
|
||||
*real_freq_hz = 0;
|
||||
}
|
||||
} else {
|
||||
*real_freq_hz = 0;
|
||||
uint32_t up_hz = 0;
|
||||
if (reported_upstream != SOC_MOD_CLK_INVALID && reported_divider != 0 &&
|
||||
esp_clk_tree_src_get_freq_hz(reported_upstream,
|
||||
ESP_CLK_TREE_SRC_FREQ_PRECISION_APPROX,
|
||||
&up_hz) == ESP_OK) {
|
||||
*real_freq_hz = up_hz / reported_divider;
|
||||
} else {
|
||||
*real_freq_hz = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
@@ -626,7 +656,33 @@ esp_err_t esp_clk_tree_src_select_upstream(soc_module_clk_t clk_src,
|
||||
// Divider for the previous upstream is no longer meaningful; the next
|
||||
// `set_freq_hz` call will program a fresh divider for `upstream`.
|
||||
state->cur_divider = 0;
|
||||
if (desc->acquire_parent != NULL) {
|
||||
ret = desc->acquire_parent();
|
||||
}
|
||||
}
|
||||
esp_os_exit_critical(&s_derived_clk_spinlock);
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool esp_clk_tree_is_power_on(soc_root_clk_circuit_t clk_circuit)
|
||||
{
|
||||
#if SOC_CLK_MPLL_SUPPORTED
|
||||
if (clk_circuit == SOC_ROOT_CIRCUIT_CLK_MPLL) {
|
||||
bool on;
|
||||
esp_os_enter_critical(&s_periph_mpll_spinlock);
|
||||
on = s_mpll_ref_cnt > 0;
|
||||
esp_os_exit_critical(&s_periph_mpll_spinlock);
|
||||
return on;
|
||||
}
|
||||
#endif
|
||||
#if SOC_CLK_APLL_SUPPORTED
|
||||
if (clk_circuit == SOC_ROOT_CIRCUIT_CLK_APLL) {
|
||||
bool on;
|
||||
esp_os_enter_critical(&s_periph_apll_spinlock);
|
||||
on = s_apll_ref_cnt > 0;
|
||||
esp_os_exit_critical(&s_periph_apll_spinlock);
|
||||
return on;
|
||||
}
|
||||
#endif
|
||||
return esp_clk_tree_port_is_power_on(clk_circuit);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user