From 7d16868e32157e0bc034b3c6ad05e8aa4d9cda89 Mon Sep 17 00:00:00 2001 From: liuning Date: Thu, 24 Nov 2022 20:18:27 +0800 Subject: [PATCH] phy: only set phy_init_flag at power domain off, when all modems deinit --- components/bt/controller/esp32/bt.c | 4 ++-- components/bt/controller/esp32c2/bt.c | 4 ++-- components/bt/controller/esp32c3/bt.c | 9 ++------- components/bt/controller/esp32s3/bt.c | 4 ++-- components/esp_phy/include/esp_phy_init.h | 5 +++-- components/esp_phy/src/phy_init.c | 18 ++++++++++++------ components/esp_wifi/src/wifi_init.c | 7 ++----- 7 files changed, 25 insertions(+), 26 deletions(-) diff --git a/components/bt/controller/esp32/bt.c b/components/bt/controller/esp32/bt.c index 1f2a81bd08cb..83f1011ae626 100644 --- a/components/bt/controller/esp32/bt.c +++ b/components/bt/controller/esp32/bt.c @@ -1479,7 +1479,7 @@ esp_err_t esp_bt_controller_init(esp_bt_controller_config_t *cfg) goto error; } - esp_phy_pd_mem_init(); + esp_phy_modem_init(); esp_bt_power_domain_on(); @@ -1641,7 +1641,7 @@ esp_err_t esp_bt_controller_deinit(void) esp_bt_power_domain_off(); - esp_phy_pd_mem_deinit(); + esp_phy_modem_deinit(); return ESP_OK; } diff --git a/components/bt/controller/esp32c2/bt.c b/components/bt/controller/esp32c2/bt.c index 3a91aebedac0..59d6fb638840 100644 --- a/components/bt/controller/esp32c2/bt.c +++ b/components/bt/controller/esp32c2/bt.c @@ -650,7 +650,7 @@ esp_err_t esp_bt_controller_init(esp_bt_controller_config_t *cfg) /* Initialize default event queue */ ble_npl_eventq_init(nimble_port_get_dflt_eventq()); #endif - esp_phy_pd_mem_init(); + esp_phy_modem_init(); periph_module_enable(PERIPH_BT_MODULE); // init phy @@ -741,7 +741,7 @@ esp_err_t esp_bt_controller_deinit(void) npl_freertos_mempool_deinit(); - esp_phy_pd_mem_deinit(); + esp_phy_modem_deinit(); ble_controller_status = ESP_BT_CONTROLLER_STATUS_IDLE; diff --git a/components/bt/controller/esp32c3/bt.c b/components/bt/controller/esp32c3/bt.c index 738a07f673e4..410f63f6f06a 100644 --- a/components/bt/controller/esp32c3/bt.c +++ b/components/bt/controller/esp32c3/bt.c @@ -1094,7 +1094,7 @@ esp_err_t esp_bt_controller_init(esp_bt_controller_config_t *cfg) #if CONFIG_MAC_BB_PD esp_mac_bb_pd_mem_init(); #endif - esp_phy_pd_mem_init(); + esp_phy_modem_init(); esp_bt_power_domain_on(); btdm_controller_mem_init(); @@ -1411,16 +1411,11 @@ esp_err_t esp_bt_controller_deinit(void) esp_unregister_mac_bb_pu_callback(btdm_mac_bb_power_up_cb); #endif - /* Fix the issue caused by the power off the bt power domain. - * This issue is only on ESP32C3. - */ - phy_init_flag(); - esp_bt_power_domain_off(); #if CONFIG_MAC_BB_PD esp_mac_bb_pd_mem_deinit(); #endif - esp_phy_pd_mem_deinit(); + esp_phy_modem_deinit(); free(osi_funcs_p); osi_funcs_p = NULL; diff --git a/components/bt/controller/esp32s3/bt.c b/components/bt/controller/esp32s3/bt.c index 6e1e3b818aa2..49414877006d 100644 --- a/components/bt/controller/esp32s3/bt.c +++ b/components/bt/controller/esp32s3/bt.c @@ -1140,7 +1140,7 @@ esp_err_t esp_bt_controller_init(esp_bt_controller_config_t *cfg) #if CONFIG_MAC_BB_PD esp_mac_bb_pd_mem_init(); #endif - esp_phy_pd_mem_init(); + esp_phy_modem_init(); esp_bt_power_domain_on(); btdm_controller_mem_init(); @@ -1462,7 +1462,7 @@ esp_err_t esp_bt_controller_deinit(void) #if CONFIG_MAC_BB_PD esp_mac_bb_pd_mem_deinit(); #endif - esp_phy_pd_mem_deinit(); + esp_phy_modem_deinit(); free(osi_funcs_p); osi_funcs_p = NULL; diff --git a/components/esp_phy/include/esp_phy_init.h b/components/esp_phy/include/esp_phy_init.h index efefd114d4ff..bcf3e289d3d6 100644 --- a/components/esp_phy/include/esp_phy_init.h +++ b/components/esp_phy/include/esp_phy_init.h @@ -178,12 +178,13 @@ void esp_phy_load_cal_and_init(void); /** * @brief Initialize backup memory for Phy power up/down */ -void esp_phy_pd_mem_init(void); +void esp_phy_modem_init(void); /** * @brief Deinitialize backup memory for Phy power up/down + * Set phy_init_flag if all modems deinit on ESP32C3 */ -void esp_phy_pd_mem_deinit(void); +void esp_phy_modem_deinit(void); #if CONFIG_MAC_BB_PD /** diff --git a/components/esp_phy/src/phy_init.c b/components/esp_phy/src/phy_init.c index 9187e5a7509f..52e1a20240eb 100644 --- a/components/esp_phy/src/phy_init.c +++ b/components/esp_phy/src/phy_init.c @@ -76,7 +76,7 @@ static DRAM_ATTR portMUX_TYPE s_phy_int_mux = portMUX_INITIALIZER_UNLOCKED; /* Memory to store PHY digital registers */ static uint32_t* s_phy_digital_regs_mem = NULL; -static uint8_t s_phy_backup_mem_ref = 0; +static uint8_t s_phy_modem_init_ref = 0; #if CONFIG_MAC_BB_PD uint32_t* s_mac_bb_pd_mem = NULL; @@ -306,11 +306,11 @@ void esp_wifi_bt_power_domain_off(void) #endif } -void esp_phy_pd_mem_init(void) +void esp_phy_modem_init(void) { _lock_acquire(&s_phy_access_lock); - s_phy_backup_mem_ref++; + s_phy_modem_init_ref++; if (s_phy_digital_regs_mem == NULL) { s_phy_digital_regs_mem = (uint32_t *)heap_caps_malloc(SOC_PHY_DIG_REGS_MEM_SIZE, MALLOC_CAP_DMA|MALLOC_CAP_INTERNAL); } @@ -319,15 +319,21 @@ void esp_phy_pd_mem_init(void) } -void esp_phy_pd_mem_deinit(void) +void esp_phy_modem_deinit(void) { _lock_acquire(&s_phy_access_lock); - s_phy_backup_mem_ref--; - if (s_phy_backup_mem_ref == 0) { + s_phy_modem_init_ref--; + if (s_phy_modem_init_ref == 0) { s_is_phy_reg_stored = false; free(s_phy_digital_regs_mem); s_phy_digital_regs_mem = NULL; + /* Fix the issue caused by the power domain off. + * This issue is only on ESP32C3. + */ +#if CONFIG_IDF_TARGET_ESP32C3 + phy_init_flag(); +#endif } _lock_release(&s_phy_access_lock); diff --git a/components/esp_wifi/src/wifi_init.c b/components/esp_wifi/src/wifi_init.c index b02e0bbe6148..e14f399087b6 100644 --- a/components/esp_wifi/src/wifi_init.c +++ b/components/esp_wifi/src/wifi_init.c @@ -128,15 +128,12 @@ esp_err_t esp_wifi_deinit(void) #if CONFIG_MAC_BB_PD esp_unregister_mac_bb_pd_callback(pm_mac_sleep); esp_unregister_mac_bb_pu_callback(pm_mac_wakeup); -#endif -#if CONFIG_IDF_TARGET_ESP32C3 - phy_init_flag(); #endif esp_wifi_power_domain_off(); #if CONFIG_MAC_BB_PD esp_mac_bb_pd_mem_deinit(); #endif - esp_phy_pd_mem_deinit(); + esp_phy_modem_deinit(); return err; } @@ -255,7 +252,7 @@ esp_err_t esp_wifi_init(const wifi_init_config_t *config) esp_mac_bb_pd_mem_init(); esp_wifi_internal_set_mac_sleep(true); #endif - esp_phy_pd_mem_init(); + esp_phy_modem_init(); #if CONFIG_IDF_TARGET_ESP32 s_wifi_mac_time_update_cb = esp_wifi_internal_update_mac_time; #endif