diff --git a/components/esp_hw_support/include/esp_private/sleep_clock.h b/components/esp_hw_support/include/esp_private/sleep_clock.h index 82ad73781e..884947f8b8 100644 --- a/components/esp_hw_support/include/esp_private/sleep_clock.h +++ b/components/esp_hw_support/include/esp_private/sleep_clock.h @@ -42,32 +42,26 @@ extern "C" { bool clock_domain_pd_allowed(void); /** - * @brief PCR module power down initialize + * @brief SoC system clock retention initialize. * - * @return ESP_OK on success - * ESP_ERR_INVALID_ARG on invalid sleep_retention_entries_create args - * No memory for the retention link + * @return + * - ESP_OK on success + * - ESP_ERR_NO_MEM not enough memory for system clock retention + * - ESP_ERR_INVALID_ARG if either of the arguments is out of range */ -esp_err_t sleep_clock_system_retention_init(void); +esp_err_t sleep_clock_system_retention_init(void *arg); +#if CONFIG_MAC_BB_PD || CONFIG_BT_LE_SLEEP_ENABLE || CONFIG_IEEE802154_SLEEP_ENABLE /** - * @brief PCR module power down deinitialize - */ -void sleep_clock_system_retention_deinit(void); - -/** - * @brief Modem syscon module power down initialize + * @brief Modem system clock retention initialize. * - * @return ESP_OK on success - * ESP_ERR_INVALID_ARG on invalid sleep_retention_entries_create args - * No memory for the retention link + * @return + * - ESP_OK on success + * - ESP_ERR_NO_MEM not enough memory for modem clock retention + * - ESP_ERR_INVALID_ARG if either of the arguments is out of range */ -esp_err_t sleep_clock_modem_retention_init(void); - -/** - * @brief Modem syscon module power down deinitialize - */ -void sleep_clock_modem_retention_deinit(void); +esp_err_t sleep_clock_modem_retention_init(void *arg); +#endif #ifdef __cplusplus } diff --git a/components/esp_hw_support/port/esp32c5/clock_retention_init.c b/components/esp_hw_support/port/esp32c5/clock_retention_init.c index cb2a38cc5c..2809f4251e 100644 --- a/components/esp_hw_support/port/esp32c5/clock_retention_init.c +++ b/components/esp_hw_support/port/esp32c5/clock_retention_init.c @@ -10,7 +10,7 @@ static __attribute__((unused)) const char *TAG = "sleep_clock"; -esp_err_t sleep_clock_system_retention_init(void) +esp_err_t sleep_clock_system_retention_init(void *arg) { #if CONFIG_IDF_TARGET_ESP32C5_MP_VERSION #define N_REGS_PCR() (((PCR_SRAM_POWER_CONF_1_REG - DR_REG_PCR_BASE) / 4) + 1) @@ -28,13 +28,8 @@ esp_err_t sleep_clock_system_retention_init(void) return ESP_OK; } -void sleep_clock_system_retention_deinit(void) -{ - sleep_retention_entries_destroy(SLEEP_RETENTION_MODULE_CLOCK_SYSTEM); -} - #if CONFIG_MAC_BB_PD || CONFIG_BT_LE_SLEEP_ENABLE || CONFIG_IEEE802154_SLEEP_ENABLE -esp_err_t sleep_clock_modem_retention_init(void) +esp_err_t sleep_clock_modem_retention_init(void *arg) { #if CONFIG_IDF_TARGET_ESP32C5 #define N_REGS_SYSCON() (((MODEM_SYSCON_MEM_RF2_CONF_REG - MODEM_SYSCON_TEST_CONF_REG) / 4) + 1) @@ -54,9 +49,4 @@ esp_err_t sleep_clock_modem_retention_init(void) ESP_LOGI(TAG, "Modem Power, Clock and Reset sleep retention initialization"); return ESP_OK; } - -void sleep_clock_modem_retention_deinit(void) -{ - sleep_retention_entries_destroy(SLEEP_RETENTION_MODULE_CLOCK_MODEM); -} #endif diff --git a/components/esp_hw_support/port/esp32c6/clock_retention_init.c b/components/esp_hw_support/port/esp32c6/clock_retention_init.c index 9082c3c913..f79c396939 100644 --- a/components/esp_hw_support/port/esp32c6/clock_retention_init.c +++ b/components/esp_hw_support/port/esp32c6/clock_retention_init.c @@ -10,7 +10,7 @@ static __attribute__((unused)) const char *TAG = "sleep_clock"; -esp_err_t sleep_clock_system_retention_init(void) +esp_err_t sleep_clock_system_retention_init(void *arg) { #define N_REGS_PCR() (((PCR_SRAM_POWER_CONF_REG - DR_REG_PCR_BASE) / 4) + 1) @@ -24,13 +24,8 @@ esp_err_t sleep_clock_system_retention_init(void) return ESP_OK; } -void sleep_clock_system_retention_deinit(void) -{ - sleep_retention_entries_destroy(SLEEP_RETENTION_MODULE_CLOCK_SYSTEM); -} - #if CONFIG_MAC_BB_PD || CONFIG_BT_LE_SLEEP_ENABLE || CONFIG_IEEE802154_SLEEP_ENABLE -esp_err_t sleep_clock_modem_retention_init(void) +esp_err_t sleep_clock_modem_retention_init(void *arg) { #define N_REGS_SYSCON() (((MODEM_SYSCON_MEM_CONF_REG - MODEM_SYSCON_TEST_CONF_REG) / 4) + 1) @@ -46,9 +41,4 @@ esp_err_t sleep_clock_modem_retention_init(void) ESP_LOGI(TAG, "Modem Power, Clock and Reset sleep retention initialization"); return ESP_OK; } - -void sleep_clock_modem_retention_deinit(void) -{ - sleep_retention_entries_destroy(SLEEP_RETENTION_MODULE_CLOCK_MODEM); -} #endif diff --git a/components/esp_hw_support/port/esp32h2/clock_retention_init.c b/components/esp_hw_support/port/esp32h2/clock_retention_init.c index 4e29d0046a..dd11464b0c 100644 --- a/components/esp_hw_support/port/esp32h2/clock_retention_init.c +++ b/components/esp_hw_support/port/esp32h2/clock_retention_init.c @@ -12,7 +12,7 @@ static __attribute__((unused)) const char *TAG = "sleep_clock"; -esp_err_t sleep_clock_system_retention_init(void) +esp_err_t sleep_clock_system_retention_init(void *arg) { #define N_REGS_PCR() (((PCR_PWDET_SAR_CLK_CONF_REG - DR_REG_PCR_BASE) / 4) + 1) @@ -26,13 +26,8 @@ esp_err_t sleep_clock_system_retention_init(void) return ESP_OK; } -void sleep_clock_system_retention_deinit(void) -{ - sleep_retention_entries_destroy(SLEEP_RETENTION_MODULE_CLOCK_SYSTEM); -} - #if CONFIG_MAC_BB_PD || CONFIG_BT_LE_SLEEP_ENABLE || CONFIG_IEEE802154_SLEEP_ENABLE -esp_err_t sleep_clock_modem_retention_init(void) +esp_err_t sleep_clock_modem_retention_init(void *arg) { #define N_REGS_SYSCON() (((MODEM_SYSCON_MEM_CONF_REG - MODEM_SYSCON_TEST_CONF_REG) / 4) + 1) #define N_REGS_LPCON() (((MODEM_LPCON_MEM_CONF_REG - MODEM_LPCON_TEST_CONF_REG) / 4) + 1) @@ -47,9 +42,4 @@ esp_err_t sleep_clock_modem_retention_init(void) ESP_LOGI(TAG, "Modem Power, Clock and Reset sleep retention initialization"); return ESP_OK; } - -void sleep_clock_modem_retention_deinit(void) -{ - sleep_retention_entries_destroy(SLEEP_RETENTION_MODULE_CLOCK_MODEM); -} #endif diff --git a/components/esp_hw_support/port/esp32p4/clock_retention_init.c b/components/esp_hw_support/port/esp32p4/clock_retention_init.c index b9fc4d7667..bd0d66d6a8 100644 --- a/components/esp_hw_support/port/esp32p4/clock_retention_init.c +++ b/components/esp_hw_support/port/esp32p4/clock_retention_init.c @@ -9,7 +9,7 @@ static __attribute__((unused)) const char *TAG = "sleep_clock"; -esp_err_t sleep_clock_system_retention_init(void) +esp_err_t sleep_clock_system_retention_init(void *arg) { #define N_REGS_PCR() (((HP_SYS_CLKRST_HPCORE_WDT_RESET_SOURCE0_REG - DR_REG_HP_SYS_CLKRST_BASE) / 4) + 1) @@ -26,8 +26,3 @@ esp_err_t sleep_clock_system_retention_init(void) ESP_LOGI(TAG, "System Power, Clock and Reset sleep retention initialization"); return ESP_OK; } - -void sleep_clock_system_retention_deinit(void) -{ - sleep_retention_entries_destroy(SLEEP_RETENTION_MODULE_CLOCK_SYSTEM); -} diff --git a/components/esp_hw_support/sleep_clock.c b/components/esp_hw_support/sleep_clock.c index 18716bff66..0ea9345f01 100644 --- a/components/esp_hw_support/sleep_clock.c +++ b/components/esp_hw_support/sleep_clock.c @@ -8,26 +8,52 @@ bool clock_domain_pd_allowed(void) { + const uint32_t inited_modules = sleep_retention_get_inited_modules(); const uint32_t created_modules = sleep_retention_get_created_modules(); - const uint32_t mask = (const uint32_t) ( - BIT(SLEEP_RETENTION_MODULE_CLOCK_SYSTEM) -#if CONFIG_MAC_BB_PD || CONFIG_BT_LE_SLEEP_ENABLE || CONFIG_IEEE802154_SLEEP_ENABLE - | BIT(SLEEP_RETENTION_MODULE_CLOCK_MODEM) + const uint32_t sys_clk_dep_modules = (const uint32_t) (BIT(SLEEP_RETENTION_MODULE_SYS_PERIPH)); + + /* The clock and reset of MODEM (WiFi, BLE and 15.4) modules are managed + * through MODEM_SYSCON, when one or more MODEMs are initialized, it is + * necessary to check the state of CLOCK_MODEM to determine MODEM domain on + * or off. The clock and reset of digital peripherals are managed through + * PCR, with TOP domain similar to MODEM domain. */ + uint32_t modem_clk_dep_modules = 0; +#if SOC_WIFI_SUPPORTED + modem_clk_dep_modules |= BIT(SLEEP_RETENTION_MODULE_WIFI_MAC) | BIT(SLEEP_RETENTION_MODULE_WIFI_BB); #endif - ); - return ((created_modules & mask) == mask); +#if SOC_BT_SUPPORTED + modem_clk_dep_modules |= BIT(SLEEP_RETENTION_MODULE_BLE_MAC) | BIT(SLEEP_RETENTION_MODULE_BT_BB); +#endif +#if SOC_IEEE802154_SUPPORTED + modem_clk_dep_modules |= BIT(SLEEP_RETENTION_MODULE_802154_MAC) | BIT(SLEEP_RETENTION_MODULE_BT_BB); +#endif + + uint32_t mask = 0; + if (inited_modules & sys_clk_dep_modules) { + mask |= SLEEP_RETENTION_MODULE_CLOCK_SYSTEM; + } + if (inited_modules & modem_clk_dep_modules) { +#if SOC_WIFI_SUPPORTED || SOC_BT_SUPPORTED || SOC_IEEE802154_SUPPORTED + mask |= SLEEP_RETENTION_MODULE_CLOCK_MODEM; +#endif + } + return ((inited_modules & mask) == (created_modules & mask)); } -#if CONFIG_PM_POWER_DOWN_PERIPHERAL_IN_LIGHT_SLEEP || CONFIG_MAC_BB_PD || CONFIG_BT_LE_SLEEP_ENABLE || CONFIG_IEEE802154_SLEEP_ENABLE ESP_SYSTEM_INIT_FN(sleep_clock_startup_init, SECONDARY, BIT(0), 106) { -#if CONFIG_PM_POWER_DOWN_PERIPHERAL_IN_LIGHT_SLEEP - sleep_clock_system_retention_init(); -#endif + sleep_retention_module_init_param_t init_param = { + .cbs = { .create = { .handle = sleep_clock_system_retention_init, .arg = NULL } }, + .attribute = SLEEP_RETENTION_MODULE_ATTR_PASSIVE + }; + sleep_retention_module_init(SLEEP_RETENTION_MODULE_CLOCK_SYSTEM, &init_param); #if CONFIG_MAC_BB_PD || CONFIG_BT_LE_SLEEP_ENABLE || CONFIG_IEEE802154_SLEEP_ENABLE - sleep_clock_modem_retention_init(); + init_param = (sleep_retention_module_init_param_t) { + .cbs = { .create = { .handle = sleep_clock_modem_retention_init, .arg = NULL } }, + .attribute = SLEEP_RETENTION_MODULE_ATTR_PASSIVE + }; + sleep_retention_module_init(SLEEP_RETENTION_MODULE_CLOCK_MODEM, &init_param); #endif return ESP_OK; } -#endif diff --git a/components/esp_hw_support/sleep_modem.c b/components/esp_hw_support/sleep_modem.c index 156521d18c..d01de62c71 100644 --- a/components/esp_hw_support/sleep_modem.c +++ b/components/esp_hw_support/sleep_modem.c @@ -285,24 +285,21 @@ inline __attribute__((always_inline)) bool sleep_modem_wifi_modem_link_done(void bool modem_domain_pd_allowed(void) { #if SOC_PM_MODEM_RETENTION_BY_REGDMA - bool modem_domain_pd_allowed = false; + const uint32_t inited_modules = sleep_retention_get_inited_modules(); const uint32_t created_modules = sleep_retention_get_created_modules(); + + uint32_t mask = 0; #if SOC_WIFI_SUPPORTED - const uint32_t mask_wifi = (const uint32_t) (BIT(SLEEP_RETENTION_MODULE_WIFI_MAC) | - BIT(SLEEP_RETENTION_MODULE_WIFI_BB)); - modem_domain_pd_allowed |= ((created_modules & mask_wifi) == mask_wifi); + mask |= BIT(SLEEP_RETENTION_MODULE_WIFI_MAC) | BIT(SLEEP_RETENTION_MODULE_WIFI_BB); #endif #if SOC_BT_SUPPORTED - const uint32_t mask_ble = (const uint32_t) (BIT(SLEEP_RETENTION_MODULE_BLE_MAC) | - BIT(SLEEP_RETENTION_MODULE_BT_BB)); - modem_domain_pd_allowed |= ((created_modules & mask_ble) == mask_ble); + mask |= BIT(SLEEP_RETENTION_MODULE_BLE_MAC) | BIT(SLEEP_RETENTION_MODULE_BT_BB); #endif #if SOC_IEEE802154_SUPPORTED - const uint32_t mask_154 = (const uint32_t) (BIT(SLEEP_RETENTION_MODULE_802154_MAC) | - BIT(SLEEP_RETENTION_MODULE_BT_BB)); - modem_domain_pd_allowed |= ((created_modules & mask_154) == mask_154); + mask |= BIT(SLEEP_RETENTION_MODULE_802154_MAC) | BIT(SLEEP_RETENTION_MODULE_BT_BB); #endif - return modem_domain_pd_allowed; + + return ((inited_modules & mask) == (created_modules & mask)); #else return false; /* MODEM power domain is controlled by each module (WiFi, Bluetooth or 15.4) of modem */ #endif