Skip to main content

esp_hal/rtc_cntl/sleep/
esp32p4.rs

1//! Light- and deep-sleep support for the ESP32-P4.
2
3use core::ops::Not;
4
5use crate::{
6    peripherals::{HP_SYS_CLKRST, LP_AON_CLKRST, PMU, USB_DEVICE},
7    rtc_cntl::{
8        Rtc,
9        rtc::{HpAnalog, HpSysCntlReg, HpSysPower, LpAnalog, LpSysPower},
10        sleep::{
11            SleepKind,
12            pmu_common::{SleepTimeConfig, request_sleep},
13        },
14    },
15    soc::clocks::{self, ClockTree, CpuRootClkConfig},
16};
17
18// ----------------------------------------------------------------------------
19// USB-Serial-JTAG pad handling across light sleep.
20//
21// In light sleep the HP peripheral domain stays powered, so the USJ PHY keeps
22// driving its pads (including the D+ pull-up) while the peripheral is clock
23// gated. The host therefore keeps the device enumerated but unresponsive, and
24// the link does not recover after wake. Mirroring esp-idf `sleep_console.c`,
25// disable the USJ pad (releasing the pull-up so the host sees a clean
26// disconnect) on light-sleep entry and restore it on wake. ESP32-P4 does not
27// support keeping USJ alive across light sleep, so this is unconditional.
28// ----------------------------------------------------------------------------
29
30/// The USJ state that light sleep changes.
31struct UsjPadBackup {
32    clock_was_enabled: bool,
33    conf0: u32,
34}
35
36fn usj_module_is_enabled() -> bool {
37    let clkrst = HP_SYS_CLKRST::regs();
38    let aon = LP_AON_CLKRST::regs();
39    clkrst
40        .soc_clk_ctrl2()
41        .read()
42        .usb_device_apb_clk_en()
43        .bit_is_set()
44        && aon
45            .lp_aonclkrst_hp_usb_clkrst_ctrl1()
46            .read()
47            .lp_aonclkrst_rst_en_usb_device()
48            .bit_is_clear()
49}
50
51fn usj_enable_bus_clock(enable: bool) {
52    HP_SYS_CLKRST::regs()
53        .soc_clk_ctrl2()
54        .modify(|_, w| w.usb_device_apb_clk_en().bit(enable));
55    // PHY 48 MHz clock for USB FSLS PHY 0.
56    LP_AON_CLKRST::regs()
57        .lp_aonclkrst_hp_usb_clkrst_ctrl0()
58        .modify(|_, w| w.lp_aonclkrst_usb_device_48m_clk_en().bit(enable));
59}
60
61fn usj_reset_register() {
62    let aon = LP_AON_CLKRST::regs();
63    aon.lp_aonclkrst_hp_usb_clkrst_ctrl1()
64        .modify(|_, w| w.lp_aonclkrst_rst_en_usb_device().set_bit());
65    aon.lp_aonclkrst_hp_usb_clkrst_ctrl1()
66        .modify(|_, w| w.lp_aonclkrst_rst_en_usb_device().clear_bit());
67}
68
69fn usj_set_pad_enable(enable: bool) {
70    USB_DEVICE::regs()
71        .conf0()
72        .modify(|_, w| w.usb_pad_enable().bit(enable));
73}
74
75/// Backup and disable the USJ pad on light-sleep entry. esp-idf
76/// `sleep_console_usj_pad_backup_and_disable`.
77fn usj_pad_backup_and_disable() -> UsjPadBackup {
78    let clock_was_enabled = usj_module_is_enabled();
79    if !clock_was_enabled {
80        // Bring the register block up so we can touch the pad enable.
81        usj_enable_bus_clock(true);
82        usj_reset_register();
83        // The register reset enables the pad, but the pad was off before the sleep.
84        usj_set_pad_enable(false);
85    }
86    // Clearing `usb_pad_enable` alone keeps the D+ pull-up, so the host never sees a
87    // disconnect and USJ stops responding after the sleep.
88    let conf0 = USB_DEVICE::regs().conf0();
89    let saved_conf0 = conf0.read().bits();
90    conf0.modify(|_, w| {
91        w.pad_pull_override().set_bit();
92        w.dp_pullup().clear_bit();
93        w.dm_pullup().clear_bit()
94    });
95    usj_set_pad_enable(false);
96    usj_enable_bus_clock(false);
97
98    UsjPadBackup {
99        clock_was_enabled,
100        conf0: saved_conf0,
101    }
102}
103
104/// Restores the USJ pad on wake. esp-idf `sleep_console_usj_pad_restore`.
105fn usj_pad_restore(backup: UsjPadBackup) {
106    usj_enable_bus_clock(true);
107    USB_DEVICE::regs()
108        .conf0()
109        .write(|w| unsafe { w.bits(backup.conf0) });
110    if !backup.clock_was_enabled {
111        usj_enable_bus_clock(false);
112    }
113}
114
115/// LP SPM RAM base. On rev-3.0 the deep-sleep wake reset vector can be
116/// redirected here (see [`install_mspi_workaround_stub`]).
117const P4_LP_RAM_BOOT_ADDR: usize = 0x5010_8000;
118
119/// Returns whether this silicon is ESP32-P4 rev 3.0 (ECO5), the only revision
120/// affected by the "MSPI crash after power up" deep-sleep erratum (fixed in
121/// rev 3.1). Mirrors esp-idf's `efuse_hal_chip_revision() == 300` gate.
122fn is_rev3_mspi_workaround_needed() -> bool {
123    crate::efuse::chip_revision() == crate::efuse::ChipRevision::from_combined(300)
124}
125
126// ESP32-P4 rev-3.0 deep-sleep wake stub (esp-idf
127// `p4_rev3_mspi_workaround.S`). On wake the chip powers up and would crash on
128// the first flash fetch; this stub runs from LP RAM, stabilizes the MSPI/flash
129// cache, resets the MSPI AXI/APB interfaces and then jumps to the HP ROM
130// first-stage boot. It is position-independent (only absolute `li`/`jr` and a
131// PC-relative delay loop), so it can be copied to `P4_LP_RAM_BOOT_ADDR` and run
132// from there. Absolute register addresses are computed from the rev-3.x
133// `reg_base.h` (HPPERIPH0 = 0x5000_0000, HPPERIPH1 = 0x500C_0000,
134// LPAON = 0x5011_0000).
135core::arch::global_asm!(
136    ".pushsection .rodata.p4_rev3_mspi_wa, \"a\"",
137    ".option push",
138    ".option norelax",
139    ".option norvc",
140    ".balign 4",
141    ".global _p4_rev3_mspi_wa_start",
142    ".global _p4_rev3_mspi_wa_end",
143    "_p4_rev3_mspi_wa_start:",
144    // Recover the reset vector to HP ROM: LP_CLKRST_HPCPU_RESET_CTRL0 |= STAT_VECTOR_SEL
145    "li   a0, 0x50111014",
146    "li   a1, 0x8000",
147    "lw   a2, 0(a0)",
148    "or   a2, a2, a1",
149    "sw   a2, 0(a0)",
150    // SPI_MEM_C_CACHE_FCTRL &= ~CLOSE_AXI_INF_EN
151    "li   a0, 0x5008C03C",
152    "li   a1, 0x80000000",
153    "not  a1, a1",
154    "lw   a2, 0(a0)",
155    "and  a2, a2, a1",
156    "sw   a2, 0(a0)",
157    // SPI_MEM_C_CACHE_FCTRL |= AXI_REQ_EN
158    "li   a1, 0x1",
159    "lw   a2, 0(a0)",
160    "or   a2, a2, a1",
161    "sw   a2, 0(a0)",
162    // One MSPI MMU entry mapping AXI addr -> flash addr.
163    "li   a0, 0x5008C380", // MMU_ITEM_INDEX = 0
164    "sw   zero, 0(a0)",
165    "li   a0, 0x5008C37C", // MMU_ITEM_CONTENT = 0x1000
166    "li   a1, 0x1000",
167    "sw   a1, 0(a0)",
168    // Disable cpu error response: CORE_ERR_RESP_DIS = 0x7
169    "li   a0, 0x500E51A4",
170    "li   a1, 0x7",
171    "sw   a1, 0(a0)",
172    // Two dummy flash reads to stabilize MSPI.
173    "li   a0, 0x80000000",
174    "lw   a1, 0(a0)",
175    "li   a0, 0x80000080",
176    "lw   a1, 0(a0)",
177    // Delay ~1us (CPU runs at 40 MHz right after reset).
178    "li   t3, 40",
179    "csrr t0, cycle",
180    "add  t1, t0, t3",
181    "100:",
182    "csrr t2, cycle",
183    "blt  t2, t1, 100b",
184    // Re-enable cpu error response: CORE_ERR_RESP_DIS = 0
185    "li   a0, 0x500E51A4",
186    "sw   zero, 0(a0)",
187    // Reset MSPI AXI + APB interfaces, then release.
188    "li   a0, 0x500E60C0",
189    "li   a1, 0x400000", // RST_EN_MSPI_AXI
190    "lw   a2, 0(a0)",
191    "or   a2, a2, a1",
192    "sw   a2, 0(a0)",
193    "li   a1, 0x1000000", // RST_EN_MSPI_APB
194    "lw   a2, 0(a0)",
195    "or   a2, a2, a1",
196    "sw   a2, 0(a0)",
197    "li   a1, 0x400000",
198    "not  a1, a1",
199    "lw   a2, 0(a0)",
200    "and  a2, a2, a1",
201    "sw   a2, 0(a0)",
202    "li   a1, 0x1000000",
203    "not  a1, a1",
204    "lw   a2, 0(a0)",
205    "and  a2, a2, a1",
206    "sw   a2, 0(a0)",
207    // Jump to HP ROM first-stage boot.
208    "li   a5, 0x4FC00000",
209    "jr   a5",
210    "_p4_rev3_mspi_wa_end:",
211    ".option pop",
212    ".popsection",
213);
214
215/// Copies the rev-3.0 MSPI workaround wake stub into LP RAM at
216/// [`P4_LP_RAM_BOOT_ADDR`]. The stub is position-independent, so a plain word
217/// copy is sufficient. The first 0x100 bytes of `RTC_FAST` are reserved for it
218/// in the linker script.
219fn install_mspi_workaround_stub() {
220    unsafe extern "C" {
221        static _p4_rev3_mspi_wa_start: u8;
222        static _p4_rev3_mspi_wa_end: u8;
223    }
224
225    let src = &raw const _p4_rev3_mspi_wa_start;
226    let end = &raw const _p4_rev3_mspi_wa_end;
227    let len = end as usize - src as usize;
228    let words = len.div_ceil(4);
229
230    let src = src as *const u32;
231    let dst = P4_LP_RAM_BOOT_ADDR as *mut u32;
232    for i in 0..words {
233        unsafe { dst.add(i).write_volatile(src.add(i).read_volatile()) };
234    }
235}
236
237/// Redirects (or restores) the HP-core wake reset vector.
238///
239/// `lp_clkrst_ll_boot_from_lp_ram`: `hpcore0_stat_vector_sel = !boot_from_lp_ram`
240/// (0 -> boot from LP SPM RAM 0x50108000, 1 -> boot from HP ROM 0x4FC00000).
241fn set_boot_from_lp_ram(boot_from_lp_ram: bool) {
242    LP_AON_CLKRST::regs()
243        .lp_aonclkrst_hpcpu_reset_ctrl0()
244        .modify(|_, w| {
245            w.lp_aonclkrst_hpcore0_stat_vector_sel()
246                .bit(!boot_from_lp_ram)
247        });
248}
249
250/// ESP32-P4 deep-sleep DCDC -> LDO supply handover.
251///
252/// The HP digital rail is normally supplied by the on-chip DCDC converter. On
253/// deep-sleep entry the PMU FSM powers down the DCDC switch; if the DCDC is
254/// still actively regulating at that point, the rail glitches when the LDO has
255/// to take over on wake-up and the chip fails to reboot (it appears to "never
256/// wake"). esp-idf avoids this by raising the HP LDO so it can take over
257/// (`pmu_sleep_increase_ldo_volt`), pre-lowering the DCDC set-point to limit the
258/// hand-over overshoot, waiting for the LDO to settle, then disabling the DCDC
259/// (`pmu_sleep_shutdown_dcdc`). The DCDC is re-enabled by the bootloader on
260/// wake. C-series parts have no DCDC and do not need this.
261fn pmu_sleep_dcdc_to_ldo_handover() {
262    // esp-idf: LDO_POWER_TAKEOVER_PREPARATION_TIME_US.
263    const LDO_TAKEOVER_PREPARATION_TIME_US: u32 = 185;
264    // esp-idf: HP_CALI_ACTIVE_DBIAS_DEFAULT.
265    const HP_CALI_ACTIVE_DBIAS: u8 = 24;
266    // esp-idf pmu_sleep_increase_ldo_volt() constants.
267    const LDO_TAKEOVER_DBIAS: u8 = 30;
268    const LDO_TAKEOVER_DCM_VSET: u8 = 24;
269
270    // pmu_sleep_increase_ldo_volt(): raise the HP LDO and pre-lower the DCDC
271    // voltage so the LDO can take over without overshoot.
272    PMU::regs()
273        .hp_active_hp_regulator0()
274        .modify(|_, w| unsafe { w.hp_active_hp_regulator_dbias().bits(LDO_TAKEOVER_DBIAS) });
275    PMU::regs()
276        .hp_active_hp_regulator0()
277        .modify(|_, w| w.hp_active_hp_regulator_xpd().set_bit());
278    PMU::regs()
279        .hp_active_bias()
280        .modify(|_, w| unsafe { w.hp_active_dcm_vset().bits(LDO_TAKEOVER_DCM_VSET) });
281
282    crate::rom::ets_delay_us(LDO_TAKEOVER_PREPARATION_TIME_US);
283
284    // pmu_sleep_shutdown_dcdc(): request the DCDC off (done_force latches it off,
285    // the dcdc_switch stays on and is disabled by the PMU when sleep is entered)
286    // and drop the HP LDO back to the active default voltage.
287    PMU::regs().dcm_ctrl().modify(|_, w| {
288        w.dcdc_off_req().set_bit();
289        w.dcdc_done_force().set_bit()
290    });
291    PMU::regs()
292        .hp_active_hp_regulator0()
293        .modify(|_, w| unsafe { w.hp_active_hp_regulator_dbias().bits(HP_CALI_ACTIVE_DBIAS) });
294}
295
296/// Configuration controlling the analog behavior during sleep.
297#[derive(Clone, Copy)]
298// pmu_sleep_analog_config_t
299pub struct AnalogSleepConfig {
300    /// High-power system analog configuration.
301    pub hp_sys: HpAnalog,
302    /// Low-power system analog configuration (LP_SLEEP).
303    pub lp_sys_sleep: LpAnalog,
304}
305
306impl AnalogSleepConfig {
307    fn defaults_deep_sleep() -> Self {
308        // PMU_SLEEP_ANALOG_DSLP_CONFIG_DEFAULT
309        Self {
310            hp_sys: {
311                let mut cfg = HpAnalog::default();
312                cfg.bias.set_dcm_mode(0);
313                cfg.bias.set_pd_cur(true); // PMU_PD_CUR_SLEEP_DEFAULT
314                cfg.bias.set_bias_sleep(true); // PMU_BIASSLP_SLEEP_DEFAULT
315                cfg.regulator0.set_xpd(false); // PMU_HP_XPD_DEEPSLEEP
316                cfg.bias.set_dbg_atten(0); // PMU_DBG_HP_DEEPSLEEP
317                cfg
318            },
319            lp_sys_sleep: {
320                let mut cfg = LpAnalog::default();
321                cfg.regulator1.set_drv_b(0);
322                cfg.bias.set_pd_cur(true);
323                cfg.bias.set_bias_sleep(true);
324                cfg.regulator0.set_slp_xpd(false);
325                cfg.regulator0.set_slp_dbias(0);
326                cfg.regulator0.set_xpd(true);
327                cfg.bias.set_dbg_atten(12); // PMU_DBG_ATTEN_DEEPSLEEP_DEFAULT
328                cfg.regulator0.set_dbias(23); // PMU_LP_DBIAS_DEEPSLEEP_0V7
329                cfg
330            },
331        }
332    }
333
334    fn defaults_light_sleep(pd_flags: PowerDownFlags) -> Self {
335        // PMU_SLEEP_ANALOG_LSLP_CONFIG_DEFAULT
336        let mut this = Self {
337            hp_sys: {
338                let mut cfg = HpAnalog::default();
339                cfg.bias.set_dcm_mode(1);
340                cfg.bias.set_dcm_vset(DCM_VSET_IN_SLEEP);
341                cfg.regulator1.set_drv_b(0); // PMU_HP_DRVB_LIGHTSLEEP
342                cfg.bias.set_pd_cur(true); // PMU_PD_CUR_SLEEP_DEFAULT
343                cfg.bias.set_bias_sleep(true); // PMU_BIASSLP_SLEEP_DEFAULT
344                cfg.regulator0.set_xpd(false); // PMU_HP_XPD_LIGHTSLEEP (use DCDC)
345                cfg.bias.set_dbg_atten(0); // PMU_DBG_ATTEN_LIGHTSLEEP_DEFAULT
346                cfg.regulator0.set_dbias(1); // PMU_HP_DBIAS_LIGHTSLEEP_0V6
347                cfg
348            },
349            lp_sys_sleep: {
350                let mut cfg = LpAnalog::default();
351                cfg.regulator1.set_drv_b(0);
352                cfg.bias.set_pd_cur(true);
353                cfg.bias.set_bias_sleep(true);
354                cfg.regulator0.set_slp_xpd(false);
355                cfg.regulator0.set_slp_dbias(0);
356                cfg.regulator0.set_xpd(true);
357                cfg.bias.set_dbg_atten(0);
358                cfg.regulator0.set_dbias(12); // PMU_LP_DBIAS_LIGHTSLEEP_0V7
359                cfg
360            },
361        };
362
363        // When the main XTAL stays powered during sleep, the analog domain must
364        // be kept in its active operating point (esp-idf pmu_sleep_config_default).
365        if !pd_flags.pd_xtal() {
366            this.hp_sys.bias.set_pd_cur(false);
367            this.hp_sys.bias.set_bias_sleep(false);
368            this.hp_sys.bias.set_dbg_atten(0);
369            this.hp_sys.regulator0.set_dbias(HP_CALI_ACTIVE_DBIAS);
370
371            this.lp_sys_sleep.bias.set_pd_cur(false);
372            this.lp_sys_sleep.bias.set_bias_sleep(false);
373            this.lp_sys_sleep.bias.set_dbg_atten(0);
374        }
375
376        this
377    }
378
379    fn apply(&self, dslp: bool) {
380        // pmu_sleep_analog_init
381
382        // HP_ACTIVE dcm_mode (deep sleep forces 0, otherwise 1).
383        PMU::regs()
384            .hp_active_bias()
385            .modify(|_, w| unsafe { w.hp_active_dcm_mode().bits(if dslp { 0 } else { 1 }) });
386
387        PMU::regs().hp_sleep_bias().modify(|_, w| unsafe {
388            w.hp_sleep_dcm_mode().bits(self.hp_sys.bias.dcm_mode());
389            w.hp_sleep_dcm_vset().bits(self.hp_sys.bias.dcm_vset());
390            w.hp_sleep_dbg_atten().bits(self.hp_sys.bias.dbg_atten());
391            w.hp_sleep_pd_cur().bit(self.hp_sys.bias.pd_cur());
392            w.sleep().bit(self.hp_sys.bias.bias_sleep())
393        });
394        PMU::regs().hp_sleep_hp_regulator0().modify(|_, w| unsafe {
395            w.hp_sleep_hp_regulator_slp_mem_xpd()
396                .bit(self.hp_sys.regulator0.slp_mem_xpd());
397            w.hp_sleep_hp_regulator_slp_logic_xpd()
398                .bit(self.hp_sys.regulator0.slp_logic_xpd());
399            w.hp_sleep_hp_regulator_xpd()
400                .bit(self.hp_sys.regulator0.xpd());
401            w.hp_sleep_hp_regulator_slp_logic_dbias()
402                .bits(self.hp_sys.regulator0.slp_logic_dbias());
403            w.hp_sleep_hp_regulator_dbias()
404                .bits(self.hp_sys.regulator0.dbias())
405        });
406        PMU::regs().hp_sleep_hp_regulator1().modify(|_, w| unsafe {
407            w.hp_sleep_hp_regulator_drv_b()
408                .bits(self.hp_sys.regulator1.drv_b())
409        });
410
411        // LP_SLEEP
412        PMU::regs().lp_sleep_bias().modify(|_, w| unsafe {
413            w.lp_sleep_dbg_atten()
414                .bits(self.lp_sys_sleep.bias.dbg_atten());
415            w.lp_sleep_pd_cur().bit(self.lp_sys_sleep.bias.pd_cur());
416            w.sleep().bit(self.lp_sys_sleep.bias.bias_sleep())
417        });
418        PMU::regs().lp_sleep_lp_regulator0().modify(|_, w| unsafe {
419            w.lp_sleep_lp_regulator_slp_xpd()
420                .bit(self.lp_sys_sleep.regulator0.slp_xpd());
421            w.lp_sleep_lp_regulator_xpd()
422                .bit(self.lp_sys_sleep.regulator0.xpd());
423            w.lp_sleep_lp_regulator_slp_dbias()
424                .bits(self.lp_sys_sleep.regulator0.slp_dbias());
425            w.lp_sleep_lp_regulator_dbias()
426                .bits(self.lp_sys_sleep.regulator0.dbias())
427        });
428        PMU::regs().lp_sleep_lp_regulator1().modify(|_, w| unsafe {
429            w.lp_sleep_lp_regulator_drv_b()
430                .bits(self.lp_sys_sleep.regulator1.drv_b())
431        });
432    }
433}
434
435/// Configuration controlling digital peripherals during sleep.
436#[derive(Clone, Copy)]
437// pmu_sleep_digital_config_t
438pub struct DigitalSleepConfig {
439    /// High-power system control register configuration.
440    pub syscntl: HpSysCntlReg,
441}
442
443impl DigitalSleepConfig {
444    fn defaults_deep_sleep(pd_flags: PowerDownFlags) -> Self {
445        let mut syscntl = HpSysCntlReg::default();
446        syscntl.set_dig_pad_slp_sel(false);
447        syscntl.set_lp_pad_hold_all(pd_flags.pd_lp_periph());
448
449        Self { syscntl }
450    }
451
452    fn defaults_light_sleep(pd_flags: PowerDownFlags) -> Self {
453        // PMU_SLEEP_DIGITAL_LSLP_CONFIG_DEFAULT
454        Self {
455            syscntl: {
456                let mut cfg = HpSysCntlReg::default();
457                cfg.set_dig_pad_slp_sel(false);
458                // Hold the LP pads if the LP peripheral domain is powered down.
459                cfg.set_lp_pad_hold_all(pd_flags.pd_lp_periph());
460                cfg.set_dig_pause_wdt(true);
461                cfg
462            },
463        }
464    }
465
466    fn apply(&self) {
467        // pmu_sleep_digital_init
468
469        PMU::regs().hp_sleep_hp_sys_cntl().modify(|_, w| {
470            w.hp_sleep_dig_pad_slp_sel()
471                .bit(self.syscntl.dig_pad_slp_sel());
472            w.hp_sleep_lp_pad_hold_all()
473                .bit(self.syscntl.lp_pad_hold_all());
474            w.hp_sleep_dig_pause_wdt().bit(self.syscntl.dig_pause_wdt());
475            w.hp_sleep_dig_cpu_stall().bit(true)
476        });
477    }
478}
479
480/// Configuration controlling the power state of the HP and LP systems during
481/// sleep.
482#[derive(Clone, Copy)]
483// pmu_sleep_power_config_t
484pub struct PowerSleepConfig {
485    /// Power configuration for the high-power system during sleep.
486    pub hp_sys: HpSysPower,
487    /// Power configuration for the low-power system when active.
488    pub lp_sys_active: LpSysPower,
489    /// Power configuration for the low-power system during sleep.
490    pub lp_sys_sleep: LpSysPower,
491}
492
493impl PowerSleepConfig {
494    fn defaults(pd_flags: PowerDownFlags) -> Self {
495        let mut this = Self {
496            hp_sys: HpSysPower::default(),
497            lp_sys_active: LpSysPower::default(),
498            lp_sys_sleep: LpSysPower::default(),
499        };
500        this.apply_flags(pd_flags);
501        this
502    }
503
504    fn apply_flags(&mut self, pd_flags: PowerDownFlags) {
505        // PMU_HP_SLEEP_POWER_CONFIG_DEFAULT + flag overrides.
506        // `dcdc_switch_pd_en` is powered down only in deep sleep (which sets
507        // `pd_vddsdio`); light sleep keeps the DCDC switch so the DCDC can supply
508        // the HP domain at the light-sleep voltage.
509        self.hp_sys
510            .dig_power
511            .set_dcdc_switch_pd_en(pd_flags.pd_vddsdio());
512        self.hp_sys.dig_power.set_cnnt_pd_en(pd_flags.pd_modem());
513        self.hp_sys.dig_power.set_cpu_pd_en(pd_flags.pd_cpu());
514        self.hp_sys.dig_power.set_top_pd_en(pd_flags.pd_top());
515        self.hp_sys.dig_power.set_mem_pd_en(pd_flags.pd_mem());
516
517        self.hp_sys.clk.set_i2c_iso_en(true);
518        self.hp_sys.clk.set_i2c_retention(true);
519        self.hp_sys.clk.set_xpd_pll_i2c(0);
520        self.hp_sys.clk.set_xpd_pll(0);
521
522        self.hp_sys.xtal.set_xpd_xtal(pd_flags.pd_xtal().not());
523
524        self.lp_sys_active.clk_power.set_xpd_lppll(true);
525        self.lp_sys_active.clk_power.set_xpd_xtal32k(true);
526        self.lp_sys_active.clk_power.set_xpd_rc32k(true);
527        self.lp_sys_active.clk_power.set_xpd_fosc(true);
528
529        self.lp_sys_sleep
530            .dig_power
531            .set_peri_pd_en(pd_flags.pd_lp_periph());
532
533        self.lp_sys_sleep
534            .clk_power
535            .set_xpd_xtal32k(pd_flags.pd_xtal32k().not());
536        self.lp_sys_sleep
537            .clk_power
538            .set_xpd_rc32k(pd_flags.pd_rc32k().not());
539        self.lp_sys_sleep
540            .clk_power
541            .set_xpd_fosc(pd_flags.pd_rc_fast().not());
542
543        self.lp_sys_sleep
544            .xtal
545            .set_xpd_xtal(pd_flags.pd_xtal().not());
546    }
547
548    fn apply(&self) {
549        // pmu_sleep_power_init
550
551        // HP_SLEEP
552        PMU::regs()
553            .hp_sleep_dig_power()
554            .modify(|_, w| unsafe { w.bits(self.hp_sys.dig_power.0) });
555        PMU::regs()
556            .hp_sleep_hp_ck_power()
557            .modify(|_, w| unsafe { w.bits(self.hp_sys.clk.0) });
558        PMU::regs()
559            .hp_sleep_xtal()
560            .modify(|_, w| w.hp_sleep_xpd_xtal().bit(self.hp_sys.xtal.xpd_xtal()));
561
562        // LP_ACTIVE (hp_sleep_lp_*)
563        PMU::regs()
564            .hp_sleep_lp_dig_power()
565            .modify(|_, w| unsafe { w.bits(self.lp_sys_active.dig_power.0) });
566        PMU::regs()
567            .hp_sleep_lp_ck_power()
568            .modify(|_, w| unsafe { w.bits(self.lp_sys_active.clk_power.0) });
569
570        // LP_SLEEP
571        PMU::regs()
572            .lp_sleep_lp_dig_power()
573            .modify(|_, w| unsafe { w.bits(self.lp_sys_sleep.dig_power.0) });
574        PMU::regs()
575            .lp_sleep_lp_ck_power()
576            .modify(|_, w| unsafe { w.bits(self.lp_sys_sleep.clk_power.0) });
577        PMU::regs()
578            .lp_sleep_xtal()
579            .modify(|_, w| w.lp_sleep_xpd_xtal().bit(self.lp_sys_sleep.xtal.xpd_xtal()));
580    }
581}
582
583/// High-power system sleep timing parameters (pmu_hp_param_t subset).
584#[derive(Clone, Copy, Default)]
585pub struct HpParam {
586    analog_wait_target_cycle: u16,
587    digital_power_supply_wait_cycle: u16,
588    digital_power_up_wait_cycle: u16,
589    pll_stable_wait_cycle: u16,
590    min_slp_slow_clk_cycle: u8,
591}
592
593/// Low-power system sleep timing parameters (pmu_lp_param_t subset).
594#[derive(Clone, Copy, Default)]
595pub struct LpParam {
596    digital_power_supply_wait_cycle: u16,
597    min_slp_slow_clk_cycle: u8,
598    analog_wait_target_cycle: u8,
599    digital_power_up_wait_cycle: u16,
600}
601
602/// Shared HP/LP sleep timing parameters.
603#[derive(Clone, Copy, Default)]
604pub struct HpLpParam {
605    xtal_stable_wait_cycle: u16,
606}
607
608/// Sleep timing parameter configuration (pmu_sleep_param_config_t).
609#[derive(Clone, Copy)]
610pub struct ParamSleepConfig {
611    hp_sys: HpParam,
612    lp_sys: LpParam,
613    hp_lp: HpLpParam,
614}
615
616impl ParamSleepConfig {
617    fn apply(&self) {
618        // pmu_sleep_param_init
619        PMU::regs().slp_wakeup_cntl3().modify(|_, w| unsafe {
620            w.hp_min_slp_val().bits(self.hp_sys.min_slp_slow_clk_cycle);
621            w.lp_min_slp_val().bits(self.lp_sys.min_slp_slow_clk_cycle)
622        });
623
624        PMU::regs().slp_wakeup_cntl7().modify(|_, w| unsafe {
625            w.ana_wait_target()
626                .bits(self.hp_sys.analog_wait_target_cycle)
627        });
628
629        PMU::regs().power_wait_timer0().modify(|_, w| unsafe {
630            w.dg_hp_wait_timer()
631                .bits(self.hp_sys.digital_power_supply_wait_cycle);
632            w.dg_hp_powerup_timer()
633                .bits(self.hp_sys.digital_power_up_wait_cycle)
634        });
635
636        PMU::regs().power_wait_timer1().modify(|_, w| unsafe {
637            w.dg_lp_wait_timer()
638                .bits(self.lp_sys.digital_power_supply_wait_cycle);
639            w.dg_lp_powerup_timer()
640                .bits(self.lp_sys.digital_power_up_wait_cycle)
641        });
642
643        PMU::regs().slp_wakeup_cntl5().modify(|_, w| unsafe {
644            w.lp_ana_wait_target()
645                .bits(self.lp_sys.analog_wait_target_cycle)
646        });
647
648        PMU::regs().power_ck_wait_cntl().modify(|_, w| unsafe {
649            w.pmu_wait_xtl_stable()
650                .bits(self.hp_lp.xtal_stable_wait_cycle);
651            w.pmu_wait_pll_stable()
652                .bits(self.hp_sys.pll_stable_wait_cycle)
653        });
654    }
655
656    fn defaults(config: SleepTimeConfig, pd_flags: PowerDownFlags, pd_xtal: bool) -> Self {
657        // pmu_sleep_param_config_default
658        let hp_analog_wait_time_us = if pd_flags.pd_top() {
659            MachineConstants::HP_ANA_WAIT_TIME_PD_TOP_US
660        } else {
661            MachineConstants::HP_ANA_WAIT_TIME_PU_TOP_US
662        };
663
664        let hp_sys = HpParam {
665            min_slp_slow_clk_cycle: config.us_to_slowclk(MachineConstants::HP_MIN_SLP_TIME_US)
666                as u8,
667            analog_wait_target_cycle: config.us_to_slowclk(hp_analog_wait_time_us) as u16,
668            digital_power_supply_wait_cycle: config
669                .us_to_fastclk(MachineConstants::HP_POWER_SUPPLY_WAIT_TIME_US)
670                as u16,
671            digital_power_up_wait_cycle: config
672                .us_to_fastclk(MachineConstants::HP_POWER_UP_WAIT_TIME_US)
673                as u16,
674            pll_stable_wait_cycle: config
675                .us_to_fastclk(MachineConstants::HP_PLL_WAIT_STABLE_TIME_US)
676                as u16,
677        };
678
679        let lp_sys = LpParam {
680            min_slp_slow_clk_cycle: config.us_to_slowclk(MachineConstants::LP_MIN_SLP_TIME_US)
681                as u8,
682            analog_wait_target_cycle: config.us_to_slowclk(MachineConstants::LP_ANALOG_WAIT_TIME_US)
683                as u8,
684            digital_power_supply_wait_cycle: config
685                .us_to_fastclk(MachineConstants::LP_POWER_SUPPLY_WAIT_TIME_US)
686                as u16,
687            digital_power_up_wait_cycle: config
688                .us_to_fastclk(MachineConstants::LP_POWER_UP_WAIT_TIME_US)
689                as u16,
690        };
691
692        let xtal_stable_wait_cycle = if pd_xtal {
693            config.us_to_slowclk(MachineConstants::LP_XTAL_WAIT_STABLE_TIME_US) as u16
694        } else {
695            config.us_to_fastclk(MachineConstants::HP_XTAL_WAIT_STABLE_TIME_US) as u16
696        };
697
698        Self {
699            hp_sys,
700            lp_sys,
701            hp_lp: HpLpParam {
702                xtal_stable_wait_cycle,
703            },
704        }
705    }
706}
707
708impl SleepTimeConfig {
709    pub(crate) const CONFIG_ESP_DEFAULT_CPU_FREQ_MHZ: u32 = 360;
710    pub(crate) const LIGHT_SLEEP_TIME_OVERHEAD_US: u32 = 56;
711
712    pub(crate) fn pmu_sleep_calculate_hw_wait_time(&self, pd_flags: PowerDownFlags) -> u32 {
713        // LP core hardware wait time, microseconds.
714        let lp_wakeup_wait_time_us = self.slowclk_to_us(MachineConstants::LP_WAKEUP_WAIT_CYCLE);
715        let lp_clk_switch_time_us = self.slowclk_to_us(MachineConstants::LP_CLK_SWITCH_CYCLE);
716        // XTAL is not used as the RTC_FAST source here, so the clock power-on
717        // wait is measured in slow-clock cycles.
718        let lp_clk_power_on_wait_time_us =
719            self.slowclk_to_us(MachineConstants::LP_CLK_POWER_ON_WAIT_CYCLE);
720
721        let lp_hw_wait_time_us = MachineConstants::LP_MIN_SLP_TIME_US
722            + MachineConstants::LP_ANALOG_WAIT_TIME_US
723            + lp_clk_power_on_wait_time_us
724            + lp_wakeup_wait_time_us
725            + lp_clk_switch_time_us
726            + MachineConstants::LP_POWER_SUPPLY_WAIT_TIME_US
727            + MachineConstants::LP_POWER_UP_WAIT_TIME_US;
728
729        // HP core hardware wait time, microseconds.
730        let hp_analog_wait_time_us = if pd_flags.pd_top() {
731            MachineConstants::HP_ANA_WAIT_TIME_PD_TOP_US
732        } else {
733            MachineConstants::HP_ANA_WAIT_TIME_PU_TOP_US
734        };
735        let hp_digital_power_up_wait_time_us = MachineConstants::HP_POWER_SUPPLY_WAIT_TIME_US
736            + MachineConstants::HP_POWER_UP_WAIT_TIME_US;
737        // No regdma retention in v1, so the regdma wait time is 0.
738        let hp_regdma_wait_time_us = 0;
739        // XTAL is powered down but not used as RTC_FAST, so wait for it to
740        // stabilize on wake along with the PLL.
741        let hp_clock_wait_time_us = if pd_flags.pd_xtal() {
742            MachineConstants::HP_XTAL_WAIT_STABLE_TIME_US
743                + MachineConstants::HP_PLL_WAIT_STABLE_TIME_US
744        } else {
745            MachineConstants::HP_PLL_WAIT_STABLE_TIME_US
746        };
747
748        let hp_hw_wait_time_us = hp_analog_wait_time_us
749            + hp_digital_power_up_wait_time_us
750            + hp_regdma_wait_time_us
751            + hp_clock_wait_time_us;
752
753        lp_hw_wait_time_us + hp_hw_wait_time_us
754    }
755}
756
757/// Configuration for the RTC sleep behavior.
758#[derive(Clone, Copy)]
759pub struct RtcSleepConfig {
760    /// Deep sleep flag.
761    pub deep: bool,
762    /// Power-down flags.
763    pub pd_flags: PowerDownFlags,
764}
765
766impl Default for RtcSleepConfig {
767    fn default() -> Self {
768        Self {
769            deep: false,
770            pd_flags: PowerDownFlags(0),
771        }
772    }
773}
774
775bitfield::bitfield! {
776    #[derive(Clone, Copy)]
777    /// Power domains to be powered down during sleep.
778    pub struct PowerDownFlags(u32);
779
780    /// Controls the power-down status of the top power domain.
781    pub u32, pd_top      , set_pd_top      : 0;
782    /// Controls the power-down status of the VDD_SDIO / DCDC switch.
783    pub u32, pd_vddsdio  , set_pd_vddsdio  : 1;
784    /// Controls the power-down status of the connectivity power domain.
785    pub u32, pd_modem    , set_pd_modem    : 2;
786    /// Controls the power-down status of the high-performance peripheral domain.
787    pub u32, pd_hp_periph, set_pd_hp_periph: 3;
788    /// Controls the power-down status of the CPU power domain.
789    ///
790    /// Crate-private, because a light sleep needs CPU retention to power this domain down. A
791    /// power-down without retention loses the CPU state.
792    pub(crate) u32, pd_cpu, set_pd_cpu: 4;
793    /// Controls the power-down status of the high-performance always-on domain.
794    pub u32, pd_hp_aon   , set_pd_hp_aon   : 5;
795    /// Controls the power-down status of memory group 0.
796    pub u32, pd_mem_g0   , set_pd_mem_g0   : 6;
797    /// Controls the power-down status of memory group 1.
798    pub u32, pd_mem_g1   , set_pd_mem_g1   : 7;
799    /// Controls the power-down status of memory group 2.
800    pub u32, pd_mem_g2   , set_pd_mem_g2   : 8;
801    /// Controls the power-down status of memory group 3.
802    pub u32, pd_mem_g3   , set_pd_mem_g3   : 9;
803    /// Controls the power-down status of the crystal oscillator.
804    pub u32, pd_xtal     , set_pd_xtal     : 10;
805    /// Controls the power-down status of the fast RC oscillator.
806    pub u32, pd_rc_fast  , set_pd_rc_fast  : 11;
807    /// Controls the power-down status of the 32 kHz crystal oscillator.
808    pub u32, pd_xtal32k  , set_pd_xtal32k  : 12;
809    /// Controls the power-down status of the 32 kHz RC oscillator.
810    pub u32, pd_rc32k    , set_pd_rc32k    : 13;
811    /// Controls the power-down status of the low-power peripheral domain.
812    pub u32, pd_lp_periph, set_pd_lp_periph: 14;
813}
814
815impl PowerDownFlags {
816    /// Returns whether all memory groups are powered down.
817    pub fn pd_mem(self) -> bool {
818        self.pd_mem_g0() && self.pd_mem_g1() && self.pd_mem_g2() && self.pd_mem_g3()
819    }
820
821    /// Sets the power-down status for all memory groups at once.
822    pub fn set_pd_mem(&mut self, value: bool) {
823        self.set_pd_mem_g0(value);
824        self.set_pd_mem_g1(value);
825        self.set_pd_mem_g2(value);
826        self.set_pd_mem_g3(value);
827    }
828}
829
830// Default DCDC voltage parameter during sleep (Kconfig
831// CONFIG_ESP_SLEEP_DCM_VSET_VAL_IN_SLEEP default).
832const DCM_VSET_IN_SLEEP: u8 = 14;
833// HP active calibration dbias (esp-idf HP_CALI_ACTIVE_DBIAS_DEFAULT).
834const HP_CALI_ACTIVE_DBIAS: u8 = 24;
835
836// Constants from `PMU_SLEEP_MC_DEFAULT()` in esp-idf pmu_param.h.
837struct MachineConstants;
838impl MachineConstants {
839    const LP_MIN_SLP_TIME_US: u32 = 450;
840    const LP_WAKEUP_WAIT_CYCLE: u32 = 4;
841    const LP_ANALOG_WAIT_TIME_US: u32 = 154;
842    const LP_XTAL_WAIT_STABLE_TIME_US: u32 = 250;
843    const LP_CLK_SWITCH_CYCLE: u32 = 1;
844    const LP_CLK_POWER_ON_WAIT_CYCLE: u32 = 1;
845    const LP_POWER_SUPPLY_WAIT_TIME_US: u32 = 2;
846    const LP_POWER_UP_WAIT_TIME_US: u32 = 2;
847
848    const HP_MIN_SLP_TIME_US: u32 = 450;
849    // analog_wait_time depends on whether TOP is powered down.
850    const HP_ANA_WAIT_TIME_PD_TOP_US: u32 = 260;
851    const HP_REGDMA_S2A_WORK_TIME_US: u32 = 685;
852    const HP_ANA_WAIT_TIME_PU_TOP_US: u32 =
853        Self::HP_ANA_WAIT_TIME_PD_TOP_US + Self::HP_REGDMA_S2A_WORK_TIME_US;
854    const HP_POWER_SUPPLY_WAIT_TIME_US: u32 = 2;
855    const HP_POWER_UP_WAIT_TIME_US: u32 = 26;
856    const HP_XTAL_WAIT_STABLE_TIME_US: u32 = 250;
857    const HP_PLL_WAIT_STABLE_TIME_US: u32 = 50;
858}
859
860impl RtcSleepConfig {
861    /// Returns whether the device is in deep sleep mode.
862    pub fn deep_slp(&self) -> bool {
863        self.deep
864    }
865
866    /// Configures the device for deep sleep mode.
867    pub fn deep() -> Self {
868        Self {
869            deep: true,
870            ..Self::default()
871        }
872    }
873
874    pub(crate) fn is_deep_sleep(&self) -> bool {
875        self.deep_slp()
876    }
877
878    pub(crate) fn base_settings(_rtc: &Rtc<'_>) {}
879
880    /// Selects the kind of the sleep, and what the sleep powers down.
881    ///
882    /// Sleep entry calls this before the hooks of the wakeup sources, so that a source can keep
883    /// powered what it needs.
884    pub(crate) fn set_sleep_kind(&mut self, kind: SleepKind) {
885        self.deep = kind == SleepKind::Deep;
886
887        let lp_slow_uses_xtal32k = cfg_select! {
888            use_xtal32k => ClockTree::with(|clocks| {
889                matches!(
890                    clocks::lp_slow_clk_config(clocks),
891                    Some(clocks::LpSlowClkConfig::Xtal32k)
892                )
893            }),
894            _ => false,
895        };
896
897        if self.deep {
898            self.pd_flags.set_pd_top(true);
899            self.pd_flags.set_pd_vddsdio(true);
900            self.pd_flags.set_pd_modem(true);
901            self.pd_flags.set_pd_hp_periph(true);
902            self.pd_flags.set_pd_cpu(true);
903            self.pd_flags.set_pd_mem(true);
904            self.pd_flags.set_pd_xtal(true);
905            self.pd_flags.set_pd_hp_aon(true);
906            self.pd_flags.set_pd_lp_periph(true);
907            self.pd_flags.set_pd_xtal32k(!lp_slow_uses_xtal32k);
908            self.pd_flags.set_pd_rc32k(true);
909            self.pd_flags.set_pd_rc_fast(true);
910        } else {
911            // Light sleep: the digital domain stays powered (DCDC-supplied at the
912            // light-sleep voltage) and only clock-gated, so execution resumes in
913            // place. Power down the analog clock sources nothing needs while the
914            // core is gated. Powering down XTAL also makes the analog config use
915            // the 0.6 V light-sleep operating point.
916            self.pd_flags.set_pd_xtal(true);
917            self.pd_flags.set_pd_rc_fast(true);
918            self.pd_flags.set_pd_xtal32k(!lp_slow_uses_xtal32k);
919        }
920    }
921
922    /// Configures the wakeup and reject sources of the sleep.
923    ///
924    /// [`Self::enter_sleep`] requests the sleep after this call. The caller calls
925    /// [`SavedState::restore`] on the return value after [`Self::finish_sleep`].
926    #[crate::ram]
927    pub(crate) fn start_sleep(&self, wakeup_mask: u32, reject_mask: u32) -> SavedState {
928        let cpu_root_clk = ClockTree::with(|clocks| {
929            let old = clocks.cpu_root_clk();
930            clocks::configure_cpu_root_clk(clocks, CpuRootClkConfig::Xtal);
931            old
932        });
933
934        let power = PowerSleepConfig::defaults(self.pd_flags);
935        power.apply();
936
937        let config = if self.deep {
938            SleepTimeConfig::deep_sleep()
939        } else {
940            SleepTimeConfig::light_sleep(self.pd_flags)
941        };
942
943        // `pd_xtal` here means "the main XTAL is powered down during sleep", which
944        // selects the slow-clock xtal-stable wait on wake. That is exactly
945        // `pd_flags.pd_xtal()`; passing `xpd_xtal` (its inverse) used the fast-clock
946        // wait and produced a ~120x too-long wake-up xtal wait.
947        let mut param = ParamSleepConfig::defaults(config, self.pd_flags, self.pd_flags.pd_xtal());
948
949        if self.deep {
950            const PMU_LP_ANALOG_WAIT_TARGET_TIME_DSLP_US: u32 = 500;
951            param.lp_sys.analog_wait_target_cycle =
952                config.us_to_slowclk(PMU_LP_ANALOG_WAIT_TARGET_TIME_DSLP_US) as u8;
953
954            DigitalSleepConfig::defaults_deep_sleep(self.pd_flags).apply();
955            AnalogSleepConfig::defaults_deep_sleep().apply(true);
956        } else {
957            AnalogSleepConfig::defaults_light_sleep(self.pd_flags).apply(false);
958            DigitalSleepConfig::defaults_light_sleep(self.pd_flags).apply();
959        }
960
961        param.apply();
962
963        // ESP32-P4 rev 3.0 (ECO5) "MSPI crash after power up" deep-sleep
964        // erratum: redirect the wake reset vector to a stub in LP RAM that
965        // recovers MSPI before the first flash fetch (esp-idf pmu_sleep.c).
966        let boot_from_lp_ram = self.deep && is_rev3_mspi_workaround_needed();
967        if boot_from_lp_ram {
968            install_mspi_workaround_stub();
969            set_boot_from_lp_ram(true);
970        }
971
972        // like esp-idf pmu_sleep_start()
973
974        // lp_aon_hal_inform_wakeup_type: on P4 RTC_SLEEP_MODE_REG is
975        // LP_SYSTEM_REG_LP_STORE8 (bit0 = run deep-sleep wake stub). The ROM
976        // reads this on wake to pick the deep vs light wake path.
977        crate::peripherals::LP_AON::regs()
978            .lp_store8()
979            .modify(|r, w| unsafe { w.bits(r.bits() & !0x01 | self.deep as u32) });
980
981        // The wakeup enable field is bits 30:0 here, unlike the other PMU chips where it is the
982        // whole register. Bit 31 is reserved and reads 0, so a whole-register write is correct and
983        // saves the read.
984        PMU::regs()
985            .slp_wakeup_cntl2()
986            .write(|w| unsafe { w.bits(wakeup_mask) });
987
988        PMU::regs().slp_wakeup_cntl1().modify(|_, w| unsafe {
989            w.slp_reject_en().bit(reject_mask != 0);
990            w.sleep_reject_ena().bits(reject_mask)
991        });
992
993        PMU::regs()
994            .slp_wakeup_cntl4()
995            .write(|w| w.slp_reject_cause_clr().bit(true));
996
997        PMU::regs().int_clr().write(|w| {
998            w.sw().clear_bit_by_one();
999            w.soc_sleep_reject().clear_bit_by_one();
1000            w.soc_wakeup().clear_bit_by_one()
1001        });
1002
1003        // ESP32-P4 deep-sleep DCDC -> LDO supply handover. The HP digital rail
1004        // is normally fed by the on-chip DCDC; if it is left running while the
1005        // PMU powers down the DCDC switch on deep-sleep entry, the rail glitches
1006        // when the LDO takes over on wake-up and the chip fails to reboot (it
1007        // looks like it "never wakes"). esp-idf raises the HP LDO so it can take
1008        // over, waits for it to settle, then disables the DCDC before entering
1009        // deep sleep (pmu_sleep_increase_ldo_volt + pmu_sleep_shutdown_dcdc).
1010        // C-series parts have no DCDC and skip this.
1011        if self.deep {
1012            pmu_sleep_dcdc_to_ldo_handover();
1013        }
1014
1015        // Light sleep keeps the HP domain (and thus the USJ PHY) powered, so
1016        // de-enumerate USB-Serial-JTAG cleanly before sleeping. Deep sleep powers
1017        // the PHY off on its own.
1018        let usj = (!self.deep).then(usj_pad_backup_and_disable);
1019
1020        // The PMU FSM switches the pads to their sleep setting and holds IOs
1021        // at the same stage; trigger the pad sleep selection first so the IOs
1022        // do not get held in an indeterminate state.
1023
1024        PMU::regs()
1025            .imm_pad_hold_all()
1026            .write(|w| w.tie_high_pad_slp_sel().set_bit());
1027
1028        // FIXME HERE
1029
1030        // Start entry into sleep mode.
1031
1032        SavedState {
1033            usj,
1034            cpu_root_clk,
1035            boot_from_lp_ram,
1036        }
1037    }
1038
1039    /// Requests the sleep.
1040    ///
1041    /// The caller waits for the result of the request.
1042    #[inline(always)]
1043    pub(crate) fn enter_sleep(&self) -> bool {
1044        request_sleep()
1045    }
1046
1047    /// Cleans up after sleep.
1048    #[crate::ram]
1049    pub(crate) fn finish_sleep(&self) {
1050        // A rejected deep sleep ran the DCDC -> LDO handover, and the hardware wake that turns
1051        // the DCDC back on did not run. Switch back to the DCDC, as the bootloader leaves it.
1052        if self.deep {
1053            const DCDC_STARTUP_TIME_US: u32 = 950;
1054            const HP_CALI_ACTIVE_DCM_VSET: u8 = 27;
1055
1056            PMU::regs().power_dcdc_switch().modify(|_, w| {
1057                w.force_dcdc_switch_pu().clear_bit();
1058                w.force_dcdc_switch_pd().clear_bit()
1059            });
1060            PMU::regs().dcm_ctrl().modify(|_, w| {
1061                w.dcdc_done_force().clear_bit();
1062                w.dcdc_on_req().set_bit()
1063            });
1064            PMU::regs()
1065                .hp_active_bias()
1066                .modify(|_, w| unsafe { w.hp_active_dcm_vset().bits(HP_CALI_ACTIVE_DCM_VSET) });
1067
1068            crate::rom::ets_delay_us(DCDC_STARTUP_TIME_US);
1069
1070            PMU::regs()
1071                .hp_active_hp_regulator0()
1072                .modify(|_, w| w.hp_active_hp_regulator_xpd().clear_bit());
1073        }
1074
1075        // Switch the pad configuration back from the sleep state to the active state.
1076        PMU::regs()
1077            .imm_pad_hold_all()
1078            .write(|w| w.tie_low_pad_slp_sel().set_bit());
1079
1080        super::pmu_common::wait_for_efuse_idle();
1081
1082        // The post-wake hook of the GPIO driver releases the pads that the sleep armed. Only that
1083        // driver knows which pads it prepared.
1084    }
1085}
1086
1087/// The state that [`RtcSleepConfig::start_sleep`] changes for the length of the sleep.
1088#[must_use]
1089pub(crate) struct SavedState {
1090    usj: Option<UsjPadBackup>,
1091    cpu_root_clk: Option<CpuRootClkConfig>,
1092    boot_from_lp_ram: bool,
1093}
1094
1095impl SavedState {
1096    /// Restores the state. Runs after a wakeup and after a rejected sleep.
1097    pub(crate) fn restore(self) {
1098        if let Some(usj) = self.usj {
1099            usj_pad_restore(usj);
1100        }
1101        ClockTree::with(|clocks| {
1102            if let Some(cpu_root_clk) = self.cpu_root_clk {
1103                clocks::configure_cpu_root_clk(clocks, cpu_root_clk);
1104            }
1105        });
1106        // The wake stub restores the vector on a real wake, so this runs only after a rejected
1107        // sleep. A later reset must boot from the HP ROM.
1108        if self.boot_from_lp_ram {
1109            set_boot_from_lp_ram(false);
1110        }
1111    }
1112}