diff --git a/components/esp_hw_support/include/esp_private/sleep_retention.h b/components/esp_hw_support/include/esp_private/sleep_retention.h index 00872695d10..7290c165443 100644 --- a/components/esp_hw_support/include/esp_private/sleep_retention.h +++ b/components/esp_hw_support/include/esp_private/sleep_retention.h @@ -30,6 +30,17 @@ typedef struct { #define RETENTION_MODULE_BITMAP_INIT(module) { .bitmap[(SLEEP_RETENTION_MODULE_ ## module) >> 5] = BIT((SLEEP_RETENTION_MODULE_ ## module) % 32) } uint32_t bitmap[SLEEP_RETENTION_MODULE_BITMAP_SZ]; } sleep_retention_module_bitmap_t; + +/** + * @brief Set a bit in the retention module bitmap + * + * @param bitmap_ptr Pointer to the bitmap structure + * @param module Module number (e.g., SLEEP_RETENTION_MODULE_SYS_PERIPH) + */ + #define RETENTION_MODULE_BITMAP_SET(bitmap_ptr, module) \ + do { \ + (bitmap_ptr)->bitmap[(module) >> 5] |= BIT((module) % 32); \ + } while (0) typedef regdma_entry_buf_t sleep_retention_entries_t; typedef regdma_entries_config_t sleep_retention_entries_config_t; diff --git a/components/esp_phy/esp32c6/phy_init_data.c b/components/esp_phy/esp32c6/phy_init_data.c index d7142ccf9c1..69781b7c45e 100644 --- a/components/esp_phy/esp32c6/phy_init_data.c +++ b/components/esp_phy/esp32c6/phy_init_data.c @@ -148,12 +148,15 @@ const esp_phy_init_data_t phy_init_data= { { const char __attribute__((section(".rodata"))) phy_init_magic_post[] = PHY_INIT_MAGIC; -#if SOC_PM_MODEM_RETENTION_BY_REGDMA && CONFIG_MAC_BB_PD - +#if SOC_PM_MODEM_RETENTION_BY_REGDMA && (CONFIG_MAC_BB_PD || CONFIG_ESP_PHY_HW_SWITCH_RF) #include "esp_private/sleep_retention.h" static const char* TAG = "phy_sleep"; +static _lock_t s_phy_fe_retention_lock; +uint8_t s_phy_fe_retention_ref = 0; +#endif // SOC_PM_MODEM_RETENTION_BY_REGDMA && (CONFIG_MAC_BB_PD || CONFIG_ESP_PHY_HW_SWITCH_RF) +#if SOC_PM_MODEM_RETENTION_BY_REGDMA && CONFIG_MAC_BB_PD static esp_err_t sleep_retention_wifi_bb_init(void *arg) { #define N_REGS_WIFI_AGC() (121) @@ -204,7 +207,9 @@ void esp_phy_sleep_data_deinit(void) ESP_LOGW(TAG, "WiFi BB sleep retention deinit failed"); } } +#endif // SOC_PM_MODEM_RETENTION_BY_REGDMA && CONFIG_MAC_BB_PD +#if SOC_PM_MODEM_RETENTION_BY_REGDMA && (CONFIG_MAC_BB_PD || CONFIG_ESP_PHY_HW_SWITCH_RF) static esp_err_t sleep_retention_phy_fe_init(void *arg) { #define N_REGS_FE_COEX() (58) @@ -219,25 +224,40 @@ static esp_err_t sleep_retention_phy_fe_init(void *arg) return ESP_OK; } -void esp_phy_fe_sleep_data_init(void) +esp_err_t esp_phy_fe_sleep_data_init(void) { - sleep_retention_module_init_param_t init_param = { - .cbs = { .create = { .handle = sleep_retention_phy_fe_init, .arg = NULL } }, - .attribute = SLEEP_RETENTION_MODULE_ATTR_PASSIVE | SLEEP_RETENTION_MODULE_ATTR_ATTACH - }; - esp_err_t err = sleep_retention_module_init(SLEEP_RETENTION_MODULE_PHY_FE, &init_param); - if (err != ESP_OK) { - ESP_LOGW(TAG, "PHY FE sleep retention init failed"); - return; + _lock_acquire(&s_phy_fe_retention_lock); + if (s_phy_fe_retention_ref++ == 0) { + sleep_retention_module_init_param_t init_param = { + .cbs = { .create = { .handle = sleep_retention_phy_fe_init, .arg = NULL } }, + .attribute = SLEEP_RETENTION_MODULE_ATTR_PASSIVE + }; + esp_err_t err = sleep_retention_module_init(SLEEP_RETENTION_MODULE_PHY_FE, &init_param); + if (err != ESP_OK) { + ESP_LOGW(TAG, "PHY FE sleep retention init failed"); + s_phy_fe_retention_ref --; + _lock_release(&s_phy_fe_retention_lock); + return err; + } } + _lock_release(&s_phy_fe_retention_lock); + return ESP_OK; } void esp_phy_fe_sleep_data_deinit(void) { - esp_err_t err = sleep_retention_module_deinit(SLEEP_RETENTION_MODULE_PHY_FE); - if (err != ESP_OK) { - ESP_LOGW(TAG, "PHY FE sleep retention deinit failed"); + _lock_acquire(&s_phy_fe_retention_lock); + if (s_phy_fe_retention_ref == 0) { + _lock_release(&s_phy_fe_retention_lock); return; } + + if (--s_phy_fe_retention_ref == 0) { + esp_err_t err = sleep_retention_module_deinit(SLEEP_RETENTION_MODULE_PHY_FE); + if (err != ESP_OK) { + ESP_LOGW(TAG, "PHY FE sleep retention deinit failed"); + } + } + _lock_release(&s_phy_fe_retention_lock); } -#endif // SOC_PM_MODEM_RETENTION_BY_REGDMA && CONFIG_MAC_BB_PD +#endif // SOC_PM_MODEM_RETENTION_BY_REGDMA && (CONFIG_MAC_BB_PD || CONFIG_ESP_PHY_HW_SWITCH_RF) diff --git a/components/esp_phy/include/esp_private/phy.h b/components/esp_phy/include/esp_private/phy.h index 511e8dbd596..3ada790bac3 100644 --- a/components/esp_phy/include/esp_private/phy.h +++ b/components/esp_phy/include/esp_private/phy.h @@ -281,19 +281,19 @@ void phy_wait_freq_hw_hop_done(void); */ void phy_track_temp_debug(uint8_t debug_flag, uint8_t track_temp); #endif -#if SOC_PM_MODEM_RETENTION_BY_REGDMA && CONFIG_MAC_BB_PD +#if SOC_PM_MODEM_RETENTION_BY_REGDMA && (CONFIG_MAC_BB_PD || CONFIG_ESP_PHY_HW_SWITCH_RF) /** * @brief PHY module common memory (FE) initialize * */ -void esp_phy_fe_sleep_data_init(void); +esp_err_t esp_phy_fe_sleep_data_init(void); /** * @brief PHY module common memory (FE) de-initialize * */ void esp_phy_fe_sleep_data_deinit(void); -#endif // SOC_PM_MODEM_RETENTION_BY_REGDMA && CONFIG_MAC_BB_PD +#endif // SOC_PM_MODEM_RETENTION_BY_REGDMA && (CONFIG_MAC_BB_PD || CONFIG_ESP_PHY_HW_SWITCH_RF) #ifdef __cplusplus } diff --git a/components/esp_phy/src/btbb_init.c b/components/esp_phy/src/btbb_init.c index 2aeee38f7a1..0c9b7ee1461 100644 --- a/components/esp_phy/src/btbb_init.c +++ b/components/esp_phy/src/btbb_init.c @@ -67,14 +67,28 @@ void esp_btbb_enable(void) if (s_btbb_access_ref == 0) { bt_bb_v2_init_cmplx(BTBB_ENABLE_VERSION_PRINT); #if SOC_PM_MODEM_RETENTION_BY_REGDMA && CONFIG_FREERTOS_USE_TICKLESS_IDLE + esp_err_t err = ESP_OK; +#if CONFIG_ESP_PHY_HW_SWITCH_RF + err = esp_phy_fe_sleep_data_init(); + if (err != ESP_OK) { + _lock_release(&s_btbb_access_lock); + return; + } +#endif // CONFIG_ESP_PHY_HW_SWITCH_RF sleep_retention_module_init_param_t init_param = { .cbs = { .create = { .handle = btbb_sleep_retention_init, .arg = NULL } }, .depends = RETENTION_MODULE_BITMAP_INIT(CLOCK_MODEM) }; - esp_err_t err = sleep_retention_module_init(SLEEP_RETENTION_MODULE_BT_BB, &init_param); +#if CONFIG_ESP_PHY_HW_SWITCH_RF + init_param.depends.bitmap[SLEEP_RETENTION_MODULE_PHY_FE >> 5] |= BIT(SLEEP_RETENTION_MODULE_PHY_FE % 32); +#endif // CONFIG_ESP_PHY_HW_SWITCH_RF + err = sleep_retention_module_init(SLEEP_RETENTION_MODULE_BT_BB, &init_param); if (err == ESP_OK) { err = sleep_retention_module_allocate(SLEEP_RETENTION_MODULE_BT_BB); if (err != ESP_OK) { +#if CONFIG_ESP_PHY_HW_SWITCH_RF + esp_phy_fe_sleep_data_deinit(); +#endif // CONFIG_ESP_PHY_HW_SWITCH_RF ESP_LOGW(TAG, "failed to allocate sleep retention linked list for btbb retention"); } } else { @@ -92,6 +106,9 @@ void esp_btbb_disable(void) if (s_btbb_access_ref && (--s_btbb_access_ref == 0)) { #if SOC_PM_MODEM_RETENTION_BY_REGDMA && CONFIG_FREERTOS_USE_TICKLESS_IDLE btbb_sleep_retention_deinit(); +#if CONFIG_ESP_PHY_HW_SWITCH_RF + esp_phy_fe_sleep_data_deinit(); +#endif // CONFIG_ESP_PHY_HW_SWITCH_RF #endif // SOC_PM_MODEM_RETENTION_BY_REGDMA && CONFIG_FREERTOS_USE_TICKLESS_IDLE } _lock_release(&s_btbb_access_lock);