From b9baf49782a296a347c3d3bc662437a6174e224b Mon Sep 17 00:00:00 2001 From: xiaqilin Date: Mon, 25 Sep 2023 20:59:32 +0800 Subject: [PATCH] fix(pm): add mac/bb power down/up prepare for fix esp32c6 pll issue --- .../include/esp_private/sleep_modem.h | 18 ++++++++++++++++++ components/esp_hw_support/sleep_modem.c | 19 +++++++++++++++++++ components/esp_hw_support/sleep_modes.c | 8 ++++++++ components/esp_hw_support/sleep_retention.c | 3 --- components/esp_pm/pm_impl.c | 5 +++++ 5 files changed, 50 insertions(+), 3 deletions(-) diff --git a/components/esp_hw_support/include/esp_private/sleep_modem.h b/components/esp_hw_support/include/esp_private/sleep_modem.h index fe8e3ea220..b1df6f00e5 100644 --- a/components/esp_hw_support/include/esp_private/sleep_modem.h +++ b/components/esp_hw_support/include/esp_private/sleep_modem.h @@ -40,6 +40,24 @@ void mac_bb_power_up_cb_execute(void); #endif // CONFIG_MAC_BB_PD +#if SOC_PM_RETENTION_HAS_CLOCK_BUG +/** + * @brief MAC and baseband power down operation + * + * In light sleep mode, execute IEEE802154/Bluetooth module MAC and baseband + * power up and backup prepare operations. + */ +void mac_bb_power_down_prepare(void); + +/** + * @brief MAC and baseband power up operation + * + * In light sleep mode, execute IEEE802154/Bluetooth module MAC and baseband + * power up and restore prepare operations. + */ +void mac_bb_power_up_prepare(void); +#endif // SOC_PM_RETENTION_HAS_CLOCK_BUG + #if SOC_PM_SUPPORT_PMU_MODEM_STATE /** diff --git a/components/esp_hw_support/sleep_modem.c b/components/esp_hw_support/sleep_modem.c index 7e963aa64d..9e2e4ab366 100644 --- a/components/esp_hw_support/sleep_modem.c +++ b/components/esp_hw_support/sleep_modem.c @@ -390,3 +390,22 @@ 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; +void IRAM_ATTR mac_bb_power_down_prepare(void) +{ + if (s_modem_sleep == false) { + sleep_retention_do_extra_retention(true); // backup + s_modem_sleep = true; + } +} + +void IRAM_ATTR mac_bb_power_up_prepare(void) +{ + if (s_modem_sleep) { + sleep_retention_do_extra_retention(false); // restore + s_modem_sleep = false; + } +} +#endif /* SOC_PM_RETENTION_HAS_CLOCK_BUG */ diff --git a/components/esp_hw_support/sleep_modes.c b/components/esp_hw_support/sleep_modes.c index 4d03e5eecb..5136d6f980 100644 --- a/components/esp_hw_support/sleep_modes.c +++ b/components/esp_hw_support/sleep_modes.c @@ -624,6 +624,14 @@ static esp_err_t IRAM_ATTR esp_sleep_start(uint32_t pd_flags, esp_sleep_mode_t m // Will switch to XTAL turn down MSPI speed mspi_timing_change_speed_mode_cache_safe(true); + /* + 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 + mac_bb_power_down_prepare(); +#endif // SOC_PM_RETENTION_HAS_CLOCK_BUG + // Save current frequency and switch to XTAL rtc_cpu_freq_config_t cpu_freq_config; rtc_clk_cpu_freq_get_config(&cpu_freq_config); diff --git a/components/esp_hw_support/sleep_retention.c b/components/esp_hw_support/sleep_retention.c index 95e90d5e07..6d5cf2cfd8 100644 --- a/components/esp_hw_support/sleep_retention.c +++ b/components/esp_hw_support/sleep_retention.c @@ -505,10 +505,8 @@ uint32_t IRAM_ATTR sleep_retention_get_modules(void) #if SOC_PM_RETENTION_HAS_CLOCK_BUG void sleep_retention_do_extra_retention(bool backup_or_restore) { - _lock_acquire_recursive(&s_retention.lock); if (s_retention.highpri < SLEEP_RETENTION_REGDMA_LINK_HIGHEST_PRIORITY || s_retention.highpri > SLEEP_RETENTION_REGDMA_LINK_LOWEST_PRIORITY) { - _lock_release_recursive(&s_retention.lock); return; } const uint32_t clk_bug_modules = SLEEP_RETENTION_MODULE_BLE_MAC | SLEEP_RETENTION_MODULE_802154_MAC; @@ -525,7 +523,6 @@ void sleep_retention_do_extra_retention(bool backup_or_restore) } } int refs = s_retention.extra_refs; - _lock_release_recursive(&s_retention.lock); assert(refs >= 0 && refs <= cnt_modules); } diff --git a/components/esp_pm/pm_impl.c b/components/esp_pm/pm_impl.c index c594a3d3db..2c28e6d696 100644 --- a/components/esp_pm/pm_impl.c +++ b/components/esp_pm/pm_impl.c @@ -999,6 +999,11 @@ 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);