|
| 1 | +/* |
| 2 | + * Copyright 2025 NXP |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | +#include <zephyr/kernel.h> |
| 7 | +#include <zephyr/device.h> |
| 8 | +#include <zephyr/drivers/firmware/scmi/nxp/cpu.h> |
| 9 | +#include <zephyr/dt-bindings/power/imx943_power.h> |
| 10 | +#include <pm_mcore.h> |
| 11 | + |
| 12 | +static void pm_state_before(void) |
| 13 | +{ |
| 14 | + struct scmi_cpu_pd_lpm_config cpu_pd_lpm_cfg; |
| 15 | + struct scmi_cpu_irq_mask_config cpu_irq_mask_cfg; |
| 16 | + |
| 17 | + /* |
| 18 | + * 1. Set CPU mix as power on state in suspend mode |
| 19 | + * 2. Keep wakeupmix power on whatever low power mode, as lpuart(console) in there. |
| 20 | + */ |
| 21 | + |
| 22 | + cpu_pd_lpm_cfg.cpu_id = cpu_idx; |
| 23 | + cpu_pd_lpm_cfg.num_cfg = 2; |
| 24 | + cpu_pd_lpm_cfg.cfgs[0].domain_id = pwr_mix_idx; |
| 25 | + cpu_pd_lpm_cfg.cfgs[0].lpm_setting = SCMI_CPU_LPM_SETTING_ON_ALWAYS; |
| 26 | + cpu_pd_lpm_cfg.cfgs[0].ret_mask = 1U << pwr_mem_idx; |
| 27 | + cpu_pd_lpm_cfg.cfgs[1].domain_id = PWR_MIX_SLICE_IDX_WAKEUP; |
| 28 | + cpu_pd_lpm_cfg.cfgs[1].lpm_setting = SCMI_CPU_LPM_SETTING_ON_ALWAYS; |
| 29 | + cpu_pd_lpm_cfg.cfgs[1].ret_mask = 0; |
| 30 | + |
| 31 | + scmi_cpu_pd_lpm_set(&cpu_pd_lpm_cfg); |
| 32 | + |
| 33 | + /* Set wakeup mask */ |
| 34 | + uint32_t wake_mask[GPC_CPU_CTRL_CMC_IRQ_WAKEUP_MASK_COUNT] = { |
| 35 | + [0 ... GPC_CPU_CTRL_CMC_IRQ_WAKEUP_MASK_COUNT - 1] = 0xFFFFFFFFU |
| 36 | + }; |
| 37 | + |
| 38 | + /* IRQs enabled at NVIC level become GPC wake sources */ |
| 39 | + for (uint32_t idx = 0; idx < nvic_iser_nb; idx++) { |
| 40 | + wake_mask[idx] = ~(NVIC->ISER[idx]); |
| 41 | + } |
| 42 | + |
| 43 | + cpu_irq_mask_cfg.cpu_id = cpu_idx; |
| 44 | + cpu_irq_mask_cfg.mask_idx = 0; |
| 45 | + cpu_irq_mask_cfg.num_mask = GPC_CPU_CTRL_CMC_IRQ_WAKEUP_MASK_COUNT; |
| 46 | + |
| 47 | + for (uint8_t val = 0; val < GPC_CPU_CTRL_CMC_IRQ_WAKEUP_MASK_COUNT; val++) { |
| 48 | + cpu_irq_mask_cfg.mask[val] = wake_mask[val]; |
| 49 | + } |
| 50 | + |
| 51 | + scmi_cpu_set_irq_mask(&cpu_irq_mask_cfg); |
| 52 | +} |
| 53 | + |
| 54 | +static void pm_state_resume(void) |
| 55 | +{ |
| 56 | + struct scmi_cpu_irq_mask_config cpu_irq_mask_cfg; |
| 57 | + |
| 58 | + /* Restore scmi cpu wake mask */ |
| 59 | + uint32_t wake_mask[GPC_CPU_CTRL_CMC_IRQ_WAKEUP_MASK_COUNT] = { |
| 60 | + [0 ... GPC_CPU_CTRL_CMC_IRQ_WAKEUP_MASK_COUNT - 1] = 0x0U |
| 61 | + }; |
| 62 | + |
| 63 | + cpu_irq_mask_cfg.cpu_id = cpu_idx; |
| 64 | + cpu_irq_mask_cfg.mask_idx = 0; |
| 65 | + cpu_irq_mask_cfg.num_mask = GPC_CPU_CTRL_CMC_IRQ_WAKEUP_MASK_COUNT; |
| 66 | + |
| 67 | + for (uint8_t val = 0; val < GPC_CPU_CTRL_CMC_IRQ_WAKEUP_MASK_COUNT; val++) { |
| 68 | + cpu_irq_mask_cfg.mask[val] = wake_mask[val]; |
| 69 | + } |
| 70 | + |
| 71 | + scmi_cpu_set_irq_mask(&cpu_irq_mask_cfg); |
| 72 | +} |
| 73 | + |
| 74 | +void pm_state_set(enum pm_state state, uint8_t substate_id) |
| 75 | +{ |
| 76 | + struct scmi_cpu_sleep_mode_config cpu_cfg = {0}; |
| 77 | + |
| 78 | + pm_state_before(); |
| 79 | + |
| 80 | + __disable_irq(); |
| 81 | + /* Set BASEPRI to 0 */ |
| 82 | + irq_unlock(0); |
| 83 | + |
| 84 | + switch (state) { |
| 85 | + case PM_STATE_RUNTIME_IDLE: |
| 86 | + cpu_cfg.cpu_id = cpu_idx; |
| 87 | + cpu_cfg.sleep_mode = CPU_SLEEP_MODE_WAIT; |
| 88 | + scmi_cpu_sleep_mode_set(&cpu_cfg); |
| 89 | + __DSB(); |
| 90 | + __WFI(); |
| 91 | + break; |
| 92 | + case PM_STATE_SUSPEND_TO_IDLE: |
| 93 | + cpu_cfg.cpu_id = cpu_idx; |
| 94 | + cpu_cfg.sleep_mode = CPU_SLEEP_MODE_STOP; |
| 95 | + scmi_cpu_sleep_mode_set(&cpu_cfg); |
| 96 | + __DSB(); |
| 97 | + __WFI(); |
| 98 | + break; |
| 99 | + case PM_STATE_STANDBY: |
| 100 | + cpu_cfg.cpu_id = cpu_idx; |
| 101 | + cpu_cfg.sleep_mode = CPU_SLEEP_MODE_SUSPEND; |
| 102 | + scmi_cpu_sleep_mode_set(&cpu_cfg); |
| 103 | + __DSB(); |
| 104 | + __WFI(); |
| 105 | + break; |
| 106 | + default: |
| 107 | + break; |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +/* Handle SOC specific activity after Low Power Mode Exit */ |
| 112 | +void pm_state_exit_post_ops(enum pm_state state, uint8_t substate_id) |
| 113 | +{ |
| 114 | + struct scmi_cpu_sleep_mode_config cpu_cfg = {0}; |
| 115 | + |
| 116 | + pm_state_resume(); |
| 117 | + |
| 118 | + /* restore Mcore state into ACTIVE. */ |
| 119 | + cpu_cfg.cpu_id = cpu_idx; |
| 120 | + cpu_cfg.sleep_mode = CPU_SLEEP_MODE_RUN; |
| 121 | + scmi_cpu_sleep_mode_set(&cpu_cfg); |
| 122 | + |
| 123 | + /* Clear PRIMASK */ |
| 124 | + __enable_irq(); |
| 125 | +} |
0 commit comments