fix(pm): switch root clk src to PLL for modem reg opt and added callback

This commit is contained in:
cjin 2023-10-31 17:19:53 +08:00
parent 3292ee7039
commit e98e291601
8 changed files with 108 additions and 40 deletions

View File

@ -564,12 +564,21 @@ esp_err_t controller_sleep_init(void)
if (rc != ESP_OK) {
goto error;
}
#if SOC_PM_RETENTION_HAS_CLOCK_BUG && CONFIG_MAC_BB_PD
esp_pm_register_mac_bb_module_prepare_callback(mac_bb_power_down_prepare,
mac_bb_power_up_prepare);
#endif // SOC_PM_RETENTION_HAS_CLOCK_BUG && CONFIG_MAC_BB_PD
#endif /* CONFIG_FREERTOS_USE_TICKLESS_IDLE */
return rc;
error:
#if CONFIG_FREERTOS_USE_TICKLESS_IDLE
#if SOC_PM_RETENTION_HAS_CLOCK_BUG && CONFIG_MAC_BB_PD
esp_pm_unregister_mac_bb_module_prepare_callback(mac_bb_power_down_prepare,
mac_bb_power_up_prepare);
#endif // SOC_PM_RETENTION_HAS_CLOCK_BUG && CONFIG_MAC_BB_PD
esp_sleep_disable_bt_wakeup();
esp_pm_unregister_inform_out_light_sleep_overhead_callback(sleep_modem_light_sleep_overhead_set);
#endif /* CONFIG_FREERTOS_USE_TICKLESS_IDLE */
@ -586,6 +595,10 @@ error:
void controller_sleep_deinit(void)
{
#if CONFIG_FREERTOS_USE_TICKLESS_IDLE
#if SOC_PM_RETENTION_HAS_CLOCK_BUG && CONFIG_MAC_BB_PD
esp_pm_unregister_mac_bb_module_prepare_callback(mac_bb_power_down_prepare,
mac_bb_power_up_prepare);
#endif // SOC_PM_RETENTION_HAS_CLOCK_BUG && CONFIG_MAC_BB_PD
r_ble_rtc_wake_up_state_clr();
esp_sleep_disable_bt_wakeup();
sleep_modem_ble_mac_modem_state_deinit();

View File

@ -9,6 +9,7 @@
#include <stdbool.h>
#include "sdkconfig.h"
#include "esp_err.h"
#include "esp_sleep.h"
#ifdef __cplusplus
extern "C" {
@ -40,9 +41,27 @@ void mac_bb_power_up_cb_execute(void);
#endif // CONFIG_MAC_BB_PD
#if SOC_PM_RETENTION_HAS_CLOCK_BUG
#if SOC_PM_RETENTION_HAS_CLOCK_BUG && CONFIG_MAC_BB_PD
/**
* @brief MAC and baseband power down operation
* @brief Register sleep prepare callback for Bluetooth/IEEE802154 MAC and baseband
*
* @param pd_cb function to call when power down
* @param pu_cb function to call when power up
*/
void esp_pm_register_mac_bb_module_prepare_callback(mac_bb_power_down_cb_t pd_cb,
mac_bb_power_up_cb_t pu_cb);
/**
* @brief Unregister sleep prepare callback for Bluetooth/IEEE802154 MAC and baseband
*
* @param pd_cb function to call when power down
* @param pu_cb function to call when power up
*/
void esp_pm_unregister_mac_bb_module_prepare_callback(mac_bb_power_down_cb_t pd_cb,
mac_bb_power_up_cb_t pu_cb);
/**
* @brief MAC and baseband power up operation
*
* In light sleep mode, execute IEEE802154/Bluetooth module MAC and baseband
* power down and backup prepare operations.
@ -56,7 +75,7 @@ void mac_bb_power_down_prepare(void);
* power up and restore prepare operations.
*/
void mac_bb_power_up_prepare(void);
#endif // SOC_PM_RETENTION_HAS_CLOCK_BUG
#endif // SOC_PM_RETENTION_HAS_CLOCK_BUG && CONFIG_MAC_BB_PD
#if SOC_PM_SUPPORT_PMU_MODEM_STATE

View File

@ -425,3 +425,20 @@ bool rtc_dig_8m_enabled(void)
* TODO: update the library to use rtc_clk_xtal_freq_get
*/
rtc_xtal_freq_t rtc_get_xtal(void) __attribute__((alias("rtc_clk_xtal_freq_get")));
#if SOC_PM_RETENTION_HAS_CLOCK_BUG
void rtc_clk_cpu_freq_to_pll_mhz_and_do_retention(bool backup, int cpu_freq_mhz, void (*do_retention)(bool))
{
rtc_cpu_freq_config_t config, pll_config;
rtc_clk_cpu_freq_get_config(&config);
rtc_clk_cpu_freq_mhz_to_config(cpu_freq_mhz, &pll_config);
rtc_clk_cpu_freq_set_config(&pll_config);
if (do_retention) {
(*do_retention)(backup);
}
rtc_clk_cpu_freq_set_config(&config);
}
#endif // SOC_PM_RETENTION_HAS_CLOCK_BUG

View File

@ -28,6 +28,7 @@
#if SOC_PM_SUPPORT_PMU_MODEM_STATE
#include "soc/pmu_reg.h"
#include "esp_private/esp_pau.h"
#include "esp_private/esp_pmu.h"
#endif
static __attribute__((unused)) const char *TAG = "sleep_modem";
@ -36,9 +37,15 @@ static __attribute__((unused)) const char *TAG = "sleep_modem";
static void esp_pm_light_sleep_default_params_config(int min_freq_mhz, int max_freq_mhz);
#endif
#if SOC_PM_RETENTION_HAS_CLOCK_BUG && CONFIG_MAC_BB_PD
static bool s_modem_sleep = false;
static uint8_t s_modem_prepare_ref = 0;
static _lock_t s_modem_prepare_lock;
#endif // SOC_PM_RETENTION_HAS_CLOCK_BUG && CONFIG_MAC_BB_PD
#if CONFIG_MAC_BB_PD
#define MAC_BB_POWER_DOWN_CB_NO (2)
#define MAC_BB_POWER_UP_CB_NO (2)
#define MAC_BB_POWER_DOWN_CB_NO (3)
#define MAC_BB_POWER_UP_CB_NO (3)
static DRAM_ATTR mac_bb_power_down_cb_t s_mac_bb_power_down_cb[MAC_BB_POWER_DOWN_CB_NO];
static DRAM_ATTR mac_bb_power_up_cb_t s_mac_bb_power_up_cb[MAC_BB_POWER_UP_CB_NO];
@ -391,12 +398,37 @@ static void esp_pm_light_sleep_default_params_config(int min_freq_mhz, int max_f
}
#endif
#if SOC_PM_RETENTION_HAS_CLOCK_BUG
static bool s_modem_sleep = false;
#if SOC_PM_RETENTION_HAS_CLOCK_BUG && CONFIG_MAC_BB_PD
void esp_pm_register_mac_bb_module_prepare_callback(mac_bb_power_down_cb_t pd_cb,
mac_bb_power_up_cb_t pu_cb)
{
_lock_acquire(&s_modem_prepare_lock);
if (s_modem_prepare_ref++ == 0) {
esp_register_mac_bb_pd_callback(pd_cb);
esp_register_mac_bb_pu_callback(pu_cb);
}
_lock_release(&s_modem_prepare_lock);
}
void esp_pm_unregister_mac_bb_module_prepare_callback(mac_bb_power_down_cb_t pd_cb,
mac_bb_power_up_cb_t pu_cb)
{
_lock_acquire(&s_modem_prepare_lock);
assert(s_modem_prepare_ref);
if (--s_modem_prepare_ref == 0) {
esp_unregister_mac_bb_pd_callback(pd_cb);
esp_unregister_mac_bb_pu_callback(pu_cb);
}
_lock_release(&s_modem_prepare_lock);
}
void IRAM_ATTR mac_bb_power_down_prepare(void)
{
if (s_modem_sleep == false) {
sleep_retention_do_extra_retention(true); // backup
rtc_clk_cpu_freq_to_pll_mhz_and_do_retention(true,
CONFIG_ESP_DEFAULT_CPU_FREQ_MHZ,
sleep_retention_do_extra_retention);
s_modem_sleep = true;
}
}
@ -404,8 +436,10 @@ void IRAM_ATTR mac_bb_power_down_prepare(void)
void IRAM_ATTR mac_bb_power_up_prepare(void)
{
if (s_modem_sleep) {
sleep_retention_do_extra_retention(false); // restore
rtc_clk_cpu_freq_to_pll_mhz_and_do_retention(false,
CONFIG_ESP_DEFAULT_CPU_FREQ_MHZ,
sleep_retention_do_extra_retention);
s_modem_sleep = false;
}
}
#endif /* SOC_PM_RETENTION_HAS_CLOCK_BUG */
#endif /* SOC_PM_RETENTION_HAS_CLOCK_BUG && CONFIG_MAC_BB_PD */

View File

@ -196,9 +196,6 @@ static const char* TAG = "pm";
static void do_switch(pm_mode_t new_mode);
static void leave_idle(void);
static void on_freq_update(uint32_t old_ticks_per_us, uint32_t ticks_per_us);
#if SOC_PM_RETENTION_HAS_CLOCK_BUG
static size_t esp_pm_get_total_lock_count(void);
#endif // SOC_PM_RETENTION_HAS_CLOCK_BUG
pm_mode_t esp_pm_impl_get_mode(esp_pm_lock_type_t type, int arg)
{
@ -970,15 +967,6 @@ void esp_pm_impl_idle_hook(void)
&& !periph_should_skip_light_sleep()
#endif
) {
/*
Since the modem requires a PLL clock to access modem REG,
it is necessary to back up the mac/bb REG before disabling the PLL clock.
*/
#if SOC_PM_RETENTION_HAS_CLOCK_BUG
if (esp_pm_get_total_lock_count() <= portNUM_PROCESSORS) {
mac_bb_power_down_prepare();
}
#endif // SOC_PM_RETENTION_HAS_CLOCK_BUG
esp_pm_lock_release(s_rtos_lock_handle[core_id]);
s_core_idle[core_id] = true;
}
@ -1011,11 +999,6 @@ void IRAM_ATTR esp_pm_impl_isr_hook(void)
}
#else
leave_idle();
/* it is necessary to restore the mac/bb REG before scheduling other tasks in FreeRTOS.*/
#if SOC_PM_RETENTION_HAS_CLOCK_BUG
mac_bb_power_up_prepare();
#endif // SOC_PM_RETENTION_HAS_CLOCK_BUG
#endif // CONFIG_FREERTOS_SYSTICK_USES_CCOUNT && portNUM_PROCESSORS == 2
#if CONFIG_FREERTOS_SMP
portRESTORE_INTERRUPTS(state);
@ -1042,16 +1025,3 @@ void esp_pm_impl_waiti(void)
esp_cpu_wait_for_intr();
#endif // CONFIG_FREERTOS_USE_TICKLESS_IDLE
}
#if SOC_PM_RETENTION_HAS_CLOCK_BUG
static size_t esp_pm_get_total_lock_count(void)
{
size_t all_mode_lock_counts = 0;
portENTER_CRITICAL(&s_switch_lock);
for (int i = 0; i < ARRAY_SIZE(s_mode_lock_counts); i++) {
all_mode_lock_counts += s_mode_lock_counts[i];
}
portEXIT_CRITICAL(&s_switch_lock);
return all_mode_lock_counts;
}
#endif // SOC_PM_RETENTION_HAS_CLOCK_BUG

View File

@ -481,6 +481,19 @@ bool rtc_dig_8m_enabled(void);
uint32_t rtc_clk_freq_cal(uint32_t cal_val);
#if SOC_PM_RETENTION_HAS_CLOCK_BUG
/**
* @brief Switch root clock source to PLL do retention and switch back
*
* This function is used when Bluetooth/IEEE802154 module requires register backup/restore, this function
* is called ONLY when SOC_PM_RETENTION_HAS_CLOCK_BUG is set.
* @param backup true for backup, false for restore
* @param cpu_freq_mhz cpu frequency to do retention
* @param do_retention function for retention
*/
void rtc_clk_cpu_freq_to_pll_mhz_and_do_retention(bool backup, int cpu_freq_mhz, void (*do_retention)(bool));
#endif // SOC_PM_RETENTION_HAS_CLOCK_BUG
// -------------------------- CLOCK TREE DEFS ALIAS ----------------------------
// **WARNING**: The following are only for backwards compatibility.
// Please use the declarations in soc/clk_tree_defs.h instead.

View File

@ -21,4 +21,5 @@ CONFIG_FREERTOS_USE_TICKLESS_IDLE=y
# Sleep Config
#
CONFIG_ESP_SLEEP_POWER_DOWN_FLASH=y
CONFIG_ESP_PHY_MAC_BB_PD=y
# end of Sleep Config

View File

@ -21,6 +21,7 @@ CONFIG_FREERTOS_USE_TICKLESS_IDLE=y
# Sleep Config
#
CONFIG_ESP_SLEEP_POWER_DOWN_FLASH=y
CONFIG_ESP_PHY_MAC_BB_PD=y
# end of Sleep Config
#