1 /*
2  * Copyright (c) 2021, NXP
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 #include <zephyr/kernel.h>
7 #include <zephyr/pm/pm.h>
8 #include "fsl_power.h"
9 
10 #include <zephyr/logging/log.h>
11 LOG_MODULE_DECLARE(soc, CONFIG_SOC_LOG_LEVEL);
12 
13 /*!< Power down all unnecessary blocks and enable RBB during deep sleep. */
14 #define APP_DEEPSLEEP_RUNCFG0 (SYSCTL0_PDRUNCFG0_LPOSC_PD_MASK | \
15 			SYSCTL0_PDSLEEPCFG0_RBB_PD_MASK)
16 #define APP_DEEPSLEEP_RAM_APD 0xFFFFF8U
17 #define APP_DEEPSLEEP_RAM_PPD 0x0U
18 #define APP_EXCLUDE_FROM_DEEPSLEEP			\
19 	(((const uint32_t[]){APP_DEEPSLEEP_RUNCFG0, \
20 	(SYSCTL0_PDSLEEPCFG1_FLEXSPI_SRAM_APD_MASK | \
21 	 SYSCTL0_PDSLEEPCFG1_FLEXSPI_SRAM_PPD_MASK), \
22 	APP_DEEPSLEEP_RAM_APD, APP_DEEPSLEEP_RAM_PPD}))
23 
24 /* Invoke Low Power/System Off specific Tasks */
pm_state_set(enum pm_state state,uint8_t substate_id)25 void pm_state_set(enum pm_state state, uint8_t substate_id)
26 {
27 	ARG_UNUSED(substate_id);
28 
29 	/* FIXME: When this function is entered the Kernel has disabled
30 	 * interrupts using BASEPRI register. This is incorrect as it prevents
31 	 * waking up from any interrupt which priority is not 0. Work around the
32 	 * issue and disable interrupts using PRIMASK register as recommended
33 	 * by ARM.
34 	 */
35 
36 	/* Set PRIMASK */
37 	__disable_irq();
38 	/* Set BASEPRI to 0 */
39 	irq_unlock(0);
40 
41 	switch (state) {
42 	case PM_STATE_RUNTIME_IDLE:
43 		POWER_EnterSleep();
44 		break;
45 	case PM_STATE_SUSPEND_TO_IDLE:
46 		POWER_EnterDeepSleep(APP_EXCLUDE_FROM_DEEPSLEEP);
47 		break;
48 	default:
49 		LOG_DBG("Unsupported power state %u", state);
50 		break;
51 	}
52 }
53 
54 /* Handle SOC specific activity after Low Power Mode Exit */
pm_state_exit_post_ops(enum pm_state state,uint8_t substate_id)55 void pm_state_exit_post_ops(enum pm_state state, uint8_t substate_id)
56 {
57 	ARG_UNUSED(state);
58 	ARG_UNUSED(substate_id);
59 
60 	/* Clear PRIMASK */
61 	__enable_irq();
62 }
63