|
| 1 | +/* |
| 2 | + * Copyright (c) 2025 STMicroelectronics. |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | +/** |
| 7 | + * STM32WB0 Deepstop implementation for Power Management framework |
| 8 | + * |
| 9 | + * TODO: |
| 10 | + * - document the control flow on PM transitions |
| 11 | + * - assertions around system configuration |
| 12 | + * (e.g., valid slow clock selected, RTC enabled, ...) |
| 13 | + * - ... |
| 14 | + */ |
| 15 | +#include <zephyr/kernel.h> |
| 16 | +#include <zephyr/pm/pm.h> |
| 17 | +#include <zephyr/sys_clock.h> |
| 18 | +#include <zephyr/init.h> |
| 19 | +#include <zephyr/arch/common/pm_s2ram.h> |
| 20 | + |
| 21 | +/* Private headers in zephyr/drivers/... */ |
| 22 | +#include <clock_control/clock_stm32_ll_common.h> |
| 23 | + |
| 24 | +#include <soc.h> |
| 25 | +#include <stm32_ll_pwr.h> |
| 26 | +#include <stm32_ll_rcc.h> |
| 27 | +#include <stm32_ll_cortex.h> |
| 28 | +#include <stm32_ll_system.h> |
| 29 | + |
| 30 | +#include <zephyr/logging/log.h> |
| 31 | +LOG_MODULE_DECLARE(soc, CONFIG_SOC_LOG_LEVEL); |
| 32 | + |
| 33 | +#if defined(CONFIG_SOC_STM32WB05XX) || defined(CONFIG_SOC_STM32WB09XX) |
| 34 | +#define HAS_GPIO_RETENTION 1 |
| 35 | +#else |
| 36 | +#define HAS_GPIO_RETENTION 0 |
| 37 | +#endif /* CONFIG_SOC_STM32WB05XX || CONFIG_SOC_STM32WB09XX */ |
| 38 | + |
| 39 | +/** |
| 40 | + * System-level state managed by PM callbacks |
| 41 | + * |
| 42 | + * Things that need to be preserved across Deepstop, but |
| 43 | + * have no associated driver to backup and restore them. |
| 44 | + */ |
| 45 | +#define SRAM DT_CHOSEN(zephyr_sram) |
| 46 | +#define BL_STK_SIZ (20 * 4) /* in bytes */ |
| 47 | +#define BL_STK_TOP ((void *)(DT_REG_ADDR(SRAM) + DT_REG_SIZE(SRAM) - BL_STK_SIZ)) |
| 48 | +static uint8_t bl_stk_area_backup[BL_STK_SIZ]; |
| 49 | + |
| 50 | +uint32_t RCC_APB1ENR_vr, RCC_AHBENR_vr; |
| 51 | + |
| 52 | +static void save_system_level_state(void) |
| 53 | +{ |
| 54 | + /** |
| 55 | + * The STM32WB0 bootloader uses the end of SRAM as stack. |
| 56 | + * Since it is executed on every reset, including wakeup |
| 57 | + * from Deepstop, any data placed at the end of SRAM would |
| 58 | + * be corrupted. |
| 59 | + * |
| 60 | + * Backup these words for later restoration to avoid data |
| 61 | + * corruption. A much better solution would mark this part |
| 62 | + * of SRAM as unusable, but no easy solution was found to |
| 63 | + * achieve this. |
| 64 | + */ |
| 65 | + memcpy(bl_stk_area_backup, BL_STK_TOP, BL_STK_SIZ); |
| 66 | +} |
| 67 | + |
| 68 | +static void restore_system_level_state(void) |
| 69 | +{ |
| 70 | + /* Restore bootloader stack area */ |
| 71 | + memcpy(BL_STK_TOP, bl_stk_area_backup, BL_STK_SIZ); |
| 72 | +} |
| 73 | + |
| 74 | +/* Callback for arch_pm_s2ram_suspend */ |
| 75 | +static int suspend_system_to_deepstop(void) |
| 76 | +{ |
| 77 | + /* Enable SLEEPDEEP to allow entry in Deepstop */ |
| 78 | + LL_LPM_EnableDeepSleep(); |
| 79 | + |
| 80 | + /* Complete all memory transactions */ |
| 81 | + __DSB(); |
| 82 | + |
| 83 | + |
| 84 | + /* Attempt entry in Deepstop */ |
| 85 | + __WFI(); |
| 86 | + |
| 87 | + /** |
| 88 | + * Make sure no meaningful instruction is |
| 89 | + * executed during the two cycles latency |
| 90 | + * it takes to power-gate the CPU. |
| 91 | + */ |
| 92 | + __NOP(); |
| 93 | + __NOP(); |
| 94 | + |
| 95 | + /** |
| 96 | + * This code is reached only if the device did not |
| 97 | + * enter Deepstop mode (e.g., because an interrupt |
| 98 | + * became pending during preparatory work). |
| 99 | + * |
| 100 | + * Disable SLEEPDEEP and return the appropriate error. |
| 101 | + */ |
| 102 | + LL_LPM_EnableSleep(); |
| 103 | + |
| 104 | + return -EBUSY; |
| 105 | +} |
| 106 | + |
| 107 | +/** |
| 108 | + * Backup system state to save and configure power |
| 109 | + * controller before entry in Deepstop mode |
| 110 | + */ |
| 111 | +static void prepare_for_deepstop_entry(void) |
| 112 | +{ |
| 113 | + /** |
| 114 | + * DEEPSTOP2 configuration is performed in familiy-wide code |
| 115 | + * instead of here (see `soc/st/stm32/common/soc_config.c`). |
| 116 | + * |
| 117 | + * RAMRET configuration is performed once during SoC init, |
| 118 | + * since it is retained across Deepstop (see `soc.c`). |
| 119 | + **/ |
| 120 | + |
| 121 | + /* Save the clock configuration. */ |
| 122 | + RCC_APB1ENR_vr = RCC->APB1ENR; |
| 123 | + RCC_AHBENR_vr = RCC->AHBENR; |
| 124 | + |
| 125 | + /* Clear wakeup reason flags (which inhibit Deepstop) */ |
| 126 | + LL_PWR_ClearWakeupSource(LL_PWR_WAKEUP_ALL); |
| 127 | + LL_SYSCFG_PWRC_ClearIT(LL_SYSCFG_PWRC_WKUP); |
| 128 | + LL_PWR_ClearDeepstopSeqFlag(); |
| 129 | + LL_PWR_EnableWU_EWBLEHCPU(); |
| 130 | + |
| 131 | +#if HAS_GPIO_RETENTION |
| 132 | + /** |
| 133 | + * Enable GPIO state retention in Deepstop if available. |
| 134 | + * |
| 135 | + * Do not enable this if low-power mode debugging has been |
| 136 | + * enabled via Kconfig, because it prevents the debugger |
| 137 | + * from staying connected to the SoC. |
| 138 | + */ |
| 139 | + if (!IS_ENABLED(CONFIG_STM32_ENABLE_DEBUG_SLEEP_STOP)) { |
| 140 | + LL_PWR_EnableGPIORET(); |
| 141 | + LL_PWR_EnableDBGRET(); |
| 142 | + } |
| 143 | +#endif /* HAS_GPIO_RETENTION */ |
| 144 | + save_system_level_state(); |
| 145 | +} |
| 146 | + |
| 147 | +/** |
| 148 | + * @brief Restore SoC-level configuration lost in Deepstop |
| 149 | + * @note This function must be called right after wakeup. |
| 150 | + */ |
| 151 | +static void post_resume_configuration(void) |
| 152 | +{ |
| 153 | + __ASSERT_NO_MSG(LL_PWR_GetDeepstopSeqFlag() == 1); |
| 154 | + |
| 155 | + /** |
| 156 | + * VTOR has been reset to its default value: restore it. |
| 157 | + * (Note that RAM_VR.AppBase was filled during SoC init) |
| 158 | + */ |
| 159 | + SCB->VTOR = RAM_VR.AppBase; |
| 160 | + |
| 161 | + /* Restore the clock configuration. */ |
| 162 | + RCC->AHBENR = RCC_AHBENR_vr; |
| 163 | + RCC->APB1ENR = RCC_APB1ENR_vr; |
| 164 | + |
| 165 | + /** |
| 166 | + * Restore other miscellanous system-level things. |
| 167 | + */ |
| 168 | + restore_system_level_state(); |
| 169 | +} |
| 170 | + |
| 171 | +/** |
| 172 | + * Power Management subsystem callbacks |
| 173 | + */ |
| 174 | +void pm_state_set(enum pm_state state, uint8_t substate_id) |
| 175 | +{ |
| 176 | + /* Ignore substate: STM32WB0 has only one low-power mode */ |
| 177 | + ARG_UNUSED(substate_id); |
| 178 | + |
| 179 | + int res; |
| 180 | + |
| 181 | + if (state != PM_STATE_SUSPEND_TO_RAM) { |
| 182 | + /** |
| 183 | + * Deepstop is a suspend-to-RAM state. |
| 184 | + * Something is wrong if a different |
| 185 | + * power state has been requested. |
| 186 | + */ |
| 187 | + LOG_ERR("Unsupported power state %u", state); |
| 188 | + } |
| 189 | + prepare_for_deepstop_entry(); |
| 190 | + |
| 191 | + /* Select Deepstop low-power mode and suspend system */ |
| 192 | + LL_PWR_SetPowerMode(LL_PWR_MODE_DEEPSTOP); |
| 193 | + |
| 194 | + res = arch_pm_s2ram_suspend(suspend_system_to_deepstop); |
| 195 | + |
| 196 | + if (res >= 0) { |
| 197 | + /** |
| 198 | + * Restore system configuration only if the SoC actually |
| 199 | + * entered Deepstop - otherwise, no state has been lost |
| 200 | + * and it would be a waste of time to do so. |
| 201 | + */ |
| 202 | + post_resume_configuration(); |
| 203 | + } |
| 204 | +} |
| 205 | + |
| 206 | +void pm_state_exit_post_ops(enum pm_state state, uint8_t substate_id) |
| 207 | +{ |
| 208 | + ARG_UNUSED(state); |
| 209 | + ARG_UNUSED(substate_id); |
| 210 | + |
| 211 | + /** |
| 212 | + * We restore system state in @ref{post_resume_configuration}. |
| 213 | + * The only thing we may have to do is release GPIO retention, |
| 214 | + * which we have not done yet because we wanted the driver to |
| 215 | + * restore all configuration first. |
| 216 | + * |
| 217 | + * We also need to enable IRQs to fullfill the API contract. |
| 218 | + */ |
| 219 | +#if HAS_GPIO_RETENTION |
| 220 | + LL_PWR_DisableGPIORET(); |
| 221 | + LL_PWR_DisableDBGRET(); |
| 222 | +#endif /* HAS_GPIO_RETENTION */ |
| 223 | + |
| 224 | +#if defined(CONFIG_SOC_STM32WB06) || defined(CONFIG_SOC_STM32WB07) |
| 225 | + irq_enable(RADIO_TIMER_TXRX_WKUP_IRQn); |
| 226 | +#endif /* CONFIG_SOC_STM32WB06 || CONFIG_SOC_STM32WB07 */ |
| 227 | + irq_enable(RADIO_TIMER_CPU_WKUP_IRQn); |
| 228 | + irq_enable(RADIO_TIMER_ERROR_IRQn); |
| 229 | + __enable_irq(); |
| 230 | +} |
0 commit comments