1 /*
2  * Copyright (c) 2016, Freescale Semiconductor, Inc.
3  * Copyright 2016, 2021 NXP
4  * All rights reserved.
5  *
6  *
7  * SPDX-License-Identifier: BSD-3-Clause
8  */
9 #ifndef _FSL_POWER_H_
10 #define _FSL_POWER_H_
11 
12 #include "fsl_common.h"
13 
14 /*! @addtogroup power */
15 /*! @{ */
16 
17 /*! @file */
18 
19 /*******************************************************************************
20  * Definitions
21  ******************************************************************************/
22 
23 /*! @name Driver version */
24 /*@{*/
25 /*! @brief power driver version 2.1.0. */
26 #define FSL_POWER_DRIVER_VERSION (MAKE_VERSION(2, 1, 0))
27 /*@}*/
28 
29 #define MAKE_PD_BITS(reg, slot) (((reg) << 8) | (slot))
30 #define PDRCFG0                 0x0U
31 #define PDRCFG1                 0x1U
32 
33 typedef enum pd_bits
34 {
35     kPDRUNCFG_LP_REG       = MAKE_PD_BITS(PDRCFG0, 2U),
36     kPDRUNCFG_PD_FRO_EN    = MAKE_PD_BITS(PDRCFG0, 4U),
37     kPDRUNCFG_PD_TS        = MAKE_PD_BITS(PDRCFG0, 6U),
38     kPDRUNCFG_PD_BOD_RESET = MAKE_PD_BITS(PDRCFG0, 7U),
39     kPDRUNCFG_PD_BOD_INTR  = MAKE_PD_BITS(PDRCFG0, 8U),
40     kPDRUNCFG_PD_VD2_ANA   = MAKE_PD_BITS(PDRCFG0, 9U),
41     kPDRUNCFG_PD_ADC0      = MAKE_PD_BITS(PDRCFG0, 10U),
42     kPDRUNCFG_PD_RAM0      = MAKE_PD_BITS(PDRCFG0, 13U),
43     kPDRUNCFG_PD_RAM1      = MAKE_PD_BITS(PDRCFG0, 14U),
44     kPDRUNCFG_PD_RAM2      = MAKE_PD_BITS(PDRCFG0, 15U),
45     kPDRUNCFG_PD_RAM3      = MAKE_PD_BITS(PDRCFG0, 16U),
46     kPDRUNCFG_PD_ROM       = MAKE_PD_BITS(PDRCFG0, 17U),
47     kPDRUNCFG_PD_VDDA      = MAKE_PD_BITS(PDRCFG0, 19U),
48     kPDRUNCFG_PD_WDT_OSC   = MAKE_PD_BITS(PDRCFG0, 20U),
49     kPDRUNCFG_PD_USB0_PHY  = MAKE_PD_BITS(PDRCFG0, 21U),
50     kPDRUNCFG_PD_SYS_PLL0  = MAKE_PD_BITS(PDRCFG0, 22U),
51     kPDRUNCFG_PD_VREFP     = MAKE_PD_BITS(PDRCFG0, 23U),
52     kPDRUNCFG_PD_FLASH_BG  = MAKE_PD_BITS(PDRCFG0, 25U),
53     kPDRUNCFG_PD_VD3       = MAKE_PD_BITS(PDRCFG0, 26U),
54     kPDRUNCFG_PD_VD4       = MAKE_PD_BITS(PDRCFG0, 27U),
55     kPDRUNCFG_PD_VD5       = MAKE_PD_BITS(PDRCFG0, 28U),
56     kPDRUNCFG_PD_VD6       = MAKE_PD_BITS(PDRCFG0, 29U),
57     kPDRUNCFG_REQ_DELAY    = MAKE_PD_BITS(PDRCFG0, 30U),
58     kPDRUNCFG_FORCE_RBB    = MAKE_PD_BITS(PDRCFG0, 31U),
59 
60     kPDRUNCFG_PD_USB1_PHY  = MAKE_PD_BITS(PDRCFG1, 0U),
61     kPDRUNCFG_PD_USB_PLL   = MAKE_PD_BITS(PDRCFG1, 1U),
62     kPDRUNCFG_PD_AUDIO_PLL = MAKE_PD_BITS(PDRCFG1, 2U),
63     kPDRUNCFG_PD_SYS_OSC   = MAKE_PD_BITS(PDRCFG1, 3U),
64     kPDRUNCFG_PD_EEPROM    = MAKE_PD_BITS(PDRCFG1, 5U),
65     kPDRUNCFG_PD_rng       = MAKE_PD_BITS(PDRCFG1, 6U),
66 
67     /*
68     This enum member has no practical meaning,it is used to avoid MISRA issue,
69     user should not trying to use it.
70     */
71     kPDRUNCFG_ForceUnsigned = (int)0x80000000U,
72 } pd_bit_t;
73 
74 /* Power mode configuration API parameter */
75 typedef enum _power_mode_config
76 {
77     kPmu_Sleep          = 0U,
78     kPmu_Deep_Sleep     = 1U,
79     kPmu_Deep_PowerDown = 2U,
80 } power_mode_cfg_t;
81 
82 /*!
83  * @brief The enumeration of BOD status flags.
84  */
85 typedef enum _power_bod_status
86 {
87     kBod_ResetStatusFlag     = SYSCON_BODCTRL_BODRSTSTAT_MASK, /*!< BOD reset has occurred. */
88     kBod_InterruptStatusFlag = SYSCON_BODCTRL_BODINTSTAT_MASK, /*!< BOD interrupt has occurred */
89 } power_bod_status_t;
90 
91 /*!
92  * @brief The enumeration of BOD reset level.
93  */
94 typedef enum _power_bod_reset_level
95 {
96     kBod_ResetLevel0 = 0U, /*!< Reset Level0: 1.62V. */
97     kBod_ResetLevel1,      /*!< Reset Level0: 1.68V. */
98     kBod_ResetLevel2,      /*!< Reset Level0: 2.21V. */
99     kBod_ResetLevel3,      /*!< Reset Level0: 2.85V. */
100 } power_bod_reset_level_t;
101 
102 /*!
103  * @brief The enumeration of BOD interrupt level.
104  */
105 typedef enum _power_bod_interrupt_level
106 {
107     kBod_InterruptLevel0 = 0U, /*!< Interrupt level: 1.63V. */
108     kBod_InterruptLevel1,      /*!< Interrupt level: 1.68V. */
109     kBod_InterruptLevel2,      /*!< Interrupt level: 1.95V. */
110     kBod_InterruptLevel3,      /*!< Interrupt level: 2.86V. */
111 } power_bod_interrupt_level_t;
112 
113 /*!
114  * @brief The configuration of power bod, including reset level, interrupt level, and so on.
115  */
116 typedef struct _power_bod_config
117 {
118     bool enableReset;                   /*!< Enable/disable BOD reset function. */
119     power_bod_reset_level_t resetLevel; /*!< BOD reset level, please refer to @ref power_bod_reset_level_t. */
120     bool enableInterrupt;               /*!< Enable/disable BOD interrupt function. */
121     power_bod_interrupt_level_t
122         interruptLevel; /*!< BOD interrupt level, please refer to @ref power_bod_interrupt_level_t. */
123 } power_bod_config_t;
124 
125 /*******************************************************************************
126  * API
127  ******************************************************************************/
128 
129 #ifdef __cplusplus
130 extern "C" {
131 #endif
132 
133 /*!
134  * @name Power Configuration
135  * @{
136  */
137 
138 /*!
139  * @brief API to enable PDRUNCFG bit in the Syscon. Note that enabling the bit powers down the peripheral
140  *
141  * @param en    peripheral for which to enable the PDRUNCFG bit
142  * @return none
143  */
POWER_EnablePD(pd_bit_t en)144 static inline void POWER_EnablePD(pd_bit_t en)
145 {
146     /* PDRUNCFGSET */
147     SYSCON->PDRUNCFGSET[((uint32_t)en >> 8UL)] = (1UL << ((uint32_t)en & 0xffU));
148 }
149 
150 /*!
151  * @brief API to disable PDRUNCFG bit in the Syscon. Note that disabling the bit powers up the peripheral
152  *
153  * @param en    peripheral for which to disable the PDRUNCFG bit
154  * @return none
155  */
POWER_DisablePD(pd_bit_t en)156 static inline void POWER_DisablePD(pd_bit_t en)
157 {
158     /* PDRUNCFGCLR */
159     SYSCON->PDRUNCFGCLR[((uint32_t)en >> 8UL)] = (1UL << ((uint32_t)en & 0xffU));
160 }
161 
162 /*!
163  * @brief API to enable deep sleep bit in the ARM Core.
164  *
165  * @return none
166  */
POWER_EnableDeepSleep(void)167 static inline void POWER_EnableDeepSleep(void)
168 {
169     SCB->SCR |= SCB_SCR_SLEEPDEEP_Msk;
170 }
171 
172 /*!
173  * @brief API to disable deep sleep bit in the ARM Core.
174  *
175  * @return none
176  */
POWER_DisableDeepSleep(void)177 static inline void POWER_DisableDeepSleep(void)
178 {
179     SCB->SCR |= SCB_SCR_SLEEPDEEP_Msk;
180 }
181 
182 /*!
183  * @brief Power Library API to reload OTP.
184  *        This API must be called if VD6 is power down
185  *        and power back again since FROHF TRIM value
186  *        is store in OTP. If not, when calling FROHF settng
187  *        API in clock driver then the FROHF clock out put
188  *        will be inaccurate.
189  * @return none
190  */
191 void POWER_OtpReload(void);
192 
193 /*!
194  * @brief Power Library API to power the PLLs.
195  *
196  * @return none
197  */
198 void POWER_SetPLL(void);
199 
200 /*!
201  * @brief Power Library API to power the USB PHY.
202  *
203  * @return none
204  */
205 void POWER_SetUsbPhy(void);
206 
207 /*!
208  * @brief Power Library API to enter different power modes.
209  *
210  * @param mode Power mode.
211  * @param exclude_from_pd  Bit mask of the PDRUNCFG0(low 32bits) and PDRUNCFG1(high 32bits) that needs to be powered on
212  * during power mode selected.
213  * @return none
214  */
215 void POWER_EnterPowerMode(power_mode_cfg_t mode, uint64_t exclude_from_pd);
216 
217 /*!
218  * @brief Power Library API to enter sleep mode.
219  *
220  * @return none
221  */
222 void POWER_EnterSleep(void);
223 
224 /*!
225  * @brief Power Library API to enter deep sleep mode.
226  *
227  * @param exclude_from_pd  Bit mask of the PDRUNCFG0(low 32bits) and PDRUNCFG1(high 32bits) bits that needs to be
228  * powered on during deep sleep
229  * @return none
230  */
231 void POWER_EnterDeepSleep(uint64_t exclude_from_pd);
232 
233 /*!
234  * @brief Power Library API to enter deep power down mode.
235  *
236  * @param exclude_from_pd   Bit mask of the PDRUNCFG0(low 32bits) and PDRUNCFG1(high 32bits) that needs to be powered on
237  * during deep power down mode, but this is has no effect as the voltages are cut off.
238  *
239  * @return none
240  */
241 void POWER_EnterDeepPowerDown(uint64_t exclude_from_pd);
242 
243 /*!
244  * @brief Power Library API to choose normal regulation and set the voltage for the desired operating frequency.
245  *
246  * @param freq  - The desired frequency at which the part would like to operate,
247  *                note that the voltage and flash wait states should be set before changing frequency
248  * @return none
249  */
250 void POWER_SetVoltageForFreq(uint32_t freq);
251 
252 /*!
253  * @brief Power Library API to return the library version.
254  *
255  * @return version number of the power library
256  */
257 uint32_t POWER_GetLibVersion(void);
258 
259 /*!
260  * @brief Initialize BOD, including enabling/disabling BOD interrupt, enabling/disabling BOD reset, setting BOD
261  * interrupt level, and reset level.
262  *
263  * @param bodConfig Pointer the the structure @ref power_bod_config_t.
264  */
265 void POWER_InitBod(const power_bod_config_t *bodConfig);
266 
267 /*!
268  * @brief Get default BOD configuration.
269  *
270  * @code
271  *      bodConfig->enableReset = true;
272  *      bodConfig->resetLevel = kBod_ResetLevel0;
273  *      bodConfig->enableInterrupt = false;
274  *      bodConfig->interruptLevel = kBod_InterruptLevel0;
275  * @endcode
276  *
277  * @param bodConfig Pointer the the structure @ref power_bod_config_t.
278  */
279 void POWER_GetDefaultBodConfig(power_bod_config_t *bodConfig);
280 
281 /*!
282  * @brief De-initialize BOD.
283  */
POWER_DeinitBod(void)284 static inline void POWER_DeinitBod(void)
285 {
286     SYSCON->BODCTRL &= ~(SYSCON_BODCTRL_BODRSTENA_MASK | SYSCON_BODCTRL_BODINTENA_MASK);
287 }
288 
289 /*!
290  * @brief Get Bod status flags.
291  *
292  * @return uint32_t
293  */
POWER_GetBodStatusFlags(void)294 static inline uint32_t POWER_GetBodStatusFlags(void)
295 {
296     return (uint32_t)((SYSCON->BODCTRL) & (SYSCON_BODCTRL_BODRSTSTAT_MASK | SYSCON_BODCTRL_BODINTSTAT_MASK));
297 }
298 
299 /*!
300  * @brief Clear Bod status flags.
301  *
302  * @param mask The mask of status flags to clear, should be the OR'ed value of @ref power_bod_status_t.
303  */
POWER_ClearBodStatusFlags(uint32_t mask)304 static inline void POWER_ClearBodStatusFlags(uint32_t mask)
305 {
306     SYSCON->BODCTRL = mask;
307 }
308 
309 /*@}*/
310 
311 #ifdef __cplusplus
312 }
313 #endif
314 
315 /*@}*/
316 
317 #endif /* _FSL_POWER_H_ */
318