1 /*
2  * Copyright 2021 NXP
3  * All rights reserved.
4  *
5  * SPDX-License-Identifier: BSD-3-Clause
6  */
7 #include "fsl_rtd_cmc.h"
8 
9 /* Component ID definition, used by tools. */
10 #ifndef FSL_COMPONENT_ID
11 #define FSL_COMPONENT_ID "platform.drivers.rtd_cmc"
12 #endif
13 
14 /*******************************************************************************
15  * Definitions
16  ******************************************************************************/
17 
18 /*******************************************************************************
19  * Prototypes
20  ******************************************************************************/
21 
22 /*******************************************************************************
23  * Variables
24  ******************************************************************************/
25 
26 /*******************************************************************************
27  * Code
28  ******************************************************************************/
29 
30 /*!
31  * brief Sets clock mode.
32  *
33  * This function configs the amount of clock gating when the core asserts
34  * Sleeping due to WFI, WFE or SLEEPONEXIT.
35  *
36  * param base CMC peripheral base address.
37  * param mode System clock mode.
38  */
RTDCMC_SetClockMode(CMC_Type * base,cmc_clock_mode_t mode)39 void RTDCMC_SetClockMode(CMC_Type *base, cmc_clock_mode_t mode)
40 {
41     uint32_t reg;
42 
43     reg = base->CKCTRL;
44     reg &= ~CMC_CKCTRL_CKMODE_MASK;
45     reg |= CMC_CKCTRL_CKMODE((mode));
46     base->CKCTRL = reg;
47 }
48 
49 /*!
50  * brief Configures all power mode protection settings.
51  *
52  * This function configures the power mode protection settings for
53  * supported power modes. This should be done before setting the lowPower mode
54  * for each power doamin.
55  *
56  * The allowed lowpower modes are passed as bit map. For example, to allow
57  * Sleep and DeepSleep, use CMC_SetPowerModeProtection(CMC_base, kCMC_AllowSleepMode|kCMC_AllowDeepSleepMode).
58  * To allow all low power modes, use CMC_SetPowerModeProtection(CMC_base, kCMC_AllowAllLowPowerModes).
59  *
60  * param base CMC peripheral base address.
61  * param allowedModes Bitmaps of the allowed power modes.
62  */
RTDCMC_SetPowerModeProtection(CMC_Type * base,uint8_t allowedModes)63 void RTDCMC_SetPowerModeProtection(CMC_Type *base, uint8_t allowedModes)
64 {
65     uint32_t reg;
66 
67     reg = base->RTD_PMPROT;
68     reg &= ~(CMC_RTD_PMPROT_AS_MASK | CMC_RTD_PMPROT_ADS_MASK | CMC_RTD_PMPROT_APD_MASK | CMC_RTD_PMPROT_ADPD_MASK |
69              CMC_RTD_PMPROT_AHLD_MASK);
70     reg |= allowedModes;
71 
72     base->RTD_PMPROT = reg;
73 }
74 
75 /*!
76  * brief Enter into the selected low power mode, please make sure the selected power mode has been enabled in the
77  * protection level.
78  *
79  * param base CMC peripheral base address.
80  * param lowPowerMode The desired lowPower mode. See cmc_low_power_mode_t for details.
81  */
RTDCMC_EnterLowPowerMode(CMC_Type * base,cmc_low_power_mode_t lowPowerMode)82 void RTDCMC_EnterLowPowerMode(CMC_Type *base, cmc_low_power_mode_t lowPowerMode)
83 {
84     base->RTD_PMCTRL = CMC_RTD_PMCTRL_RTD_LPMODE(lowPowerMode);
85 
86     /* Before executing WFI instruction read back the last register to
87      * ensure all registers writes have completed. */
88     (void)base->RTD_PMCTRL;
89 
90     SCB->SCR |= SCB_SCR_SLEEPDEEP_Msk;
91     __DSB();
92     __WFI();
93     __ISB();
94 }
95