1 /******************************************************************************
2 *  Filename:       pwr_ctrl.h
3 *
4 *  Description:    Defines and prototypes for the System Power Control.
5 *
6 *  Copyright (c) 2015 - 2022, Texas Instruments Incorporated
7 *  All rights reserved.
8 *
9 *  Redistribution and use in source and binary forms, with or without
10 *  modification, are permitted provided that the following conditions are met:
11 *
12 *  1) Redistributions of source code must retain the above copyright notice,
13 *     this list of conditions and the following disclaimer.
14 *
15 *  2) Redistributions in binary form must reproduce the above copyright notice,
16 *     this list of conditions and the following disclaimer in the documentation
17 *     and/or other materials provided with the distribution.
18 *
19 *  3) Neither the name of the ORGANIZATION nor the names of its contributors may
20 *     be used to endorse or promote products derived from this software without
21 *     specific prior written permission.
22 *
23 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24 *  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 *  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
27 *  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29 *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30 *  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31 *  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32 *  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 *  POSSIBILITY OF SUCH DAMAGE.
34 *
35 ******************************************************************************/
36 
37 //*****************************************************************************
38 //
39 //! \addtogroup system_control_group
40 //! @{
41 //! \addtogroup pwrctrl_api
42 //! @{
43 //
44 //*****************************************************************************
45 
46 #ifndef __PWR_CTRL_H__
47 #define __PWR_CTRL_H__
48 
49 //*****************************************************************************
50 //
51 // If building with a C++ compiler, make all of the definitions in this header
52 // have a C binding.
53 //
54 //*****************************************************************************
55 #ifdef __cplusplus
56 extern "C"
57 {
58 #endif
59 
60 #include <stdbool.h>
61 #include <stdint.h>
62 #include "../inc/hw_types.h"
63 #include "../inc/hw_memmap.h"
64 #include "../inc/hw_ints.h"
65 #include "../inc/hw_aon_pmctl.h"
66 #include "../inc/hw_aon_rtc.h"
67 #include "../inc/hw_adi_2_refsys.h"
68 #include "debug.h"
69 #include "interrupt.h"
70 #include "osc.h"
71 #include "cpu.h"
72 #include "prcm.h"
73 #include "aon_ioc.h"
74 #include "adi.h"
75 
76 //*****************************************************************************
77 //
78 // Support for DriverLib in ROM:
79 // This section renames all functions that are not "static inline", so that
80 // calling these functions will default to implementation in flash. At the end
81 // of this file a second renaming will change the defaults to implementation in
82 // ROM for available functions.
83 //
84 // To force use of the implementation in flash, e.g. for debugging:
85 // - Globally: Define DRIVERLIB_NOROM at project level
86 // - Per function: Use prefix "NOROM_" when calling the function
87 //
88 //*****************************************************************************
89 #if !defined(DOXYGEN)
90     #define PowerCtrlSourceSet              NOROM_PowerCtrlSourceSet
91 #endif
92 
93 //*****************************************************************************
94 //
95 // Defines for the system power states
96 //
97 //*****************************************************************************
98 #define PWRCTRL_ACTIVE          0x00000001
99 #define PWRCTRL_STANDBY         0x00000002
100 #define PWRCTRL_POWER_DOWN      0x00000004
101 #define PWRCTRL_SHUTDOWN        0x00000008
102 
103 //*****************************************************************************
104 //
105 // Defines for the power configuration in the AON System Control 1.2 V
106 //
107 //*****************************************************************************
108 #define PWRCTRL_IOSEG3_ENABLE   0x00000800
109 #define PWRCTRL_IOSEG2_ENABLE   0x00000400
110 #define PWRCTRL_IOSEG3_DISABLE  0x00000200
111 #define PWRCTRL_IOSEG2_DISABLE  0x00000100
112 #define PWRCTRL_PWRSRC_DCDC     0x00000001
113 #define PWRCTRL_PWRSRC_GLDO     0x00000000
114 #define PWRCTRL_PWRSRC_ULDO     0x00000002
115 
116 //*****************************************************************************
117 //
118 // The following are defines for the various reset source for the device.
119 //
120 //*****************************************************************************
121 #define PWRCTRL_RST_POWER_ON    0x00000000  // Reset by power on
122 #define PWRCTRL_RST_PIN         0x00000001  // Pin reset
123 #define PWRCTRL_RST_VDDS_BOD    0x00000002  // VDDS Brown Out Detect
124 #define PWRCTRL_RST_VDD_BOD     0x00000003  // VDD Brown Out Detect
125 #define PWRCTRL_RST_VDDR_BOD    0x00000004  // VDDR Brown Out Detect
126 #define PWRCTRL_RST_CLK_LOSS    0x00000005  // Clock loss Reset
127 #define PWRCTRL_RST_SW_PIN      0x00000006  // SYSRESET or pin reset
128 #define PWRCTRL_RST_WARM        0x00000007  // Reset via PRCM warm reset request
129 
130 //*****************************************************************************
131 //
132 // API Functions and prototypes
133 //
134 //*****************************************************************************
135 
136 //*****************************************************************************
137 //
138 //! \brief Set (Request) the main power source.
139 //!
140 //! \note The system will never allow a switch to the \ref PWRCTRL_PWRSRC_ULDO
141 //! when in active mode. This is only allowed when the system is in lower power
142 //! mode where no code is executing and no peripherals are active.
143 //! Assuming that there is an external capacitor available for the
144 //! \ref PWRCTRL_PWRSRC_DCDC the system can dynamically switch back and forth
145 //! between the two when in active mode.
146 //!
147 //! \note The system will automatically switch to the GLDO / DCDC when waking
148 //! up from a low power mode.
149 //!
150 //! \param ui32PowerConfig is a bitmask indicating the target power source.
151 //! - \ref PWRCTRL_PWRSRC_DCDC
152 //! - \ref PWRCTRL_PWRSRC_GLDO
153 //! - \ref PWRCTRL_PWRSRC_ULDO
154 //!
155 //! \return None
156 //
157 //*****************************************************************************
158 extern void PowerCtrlSourceSet(uint32_t ui32PowerConfig);
159 
160 //*****************************************************************************
161 //
162 //! \brief Get the main power source.
163 //!
164 //! Use this function to retrieve the current active power source.
165 //!
166 //! When the System CPU is active it can never be powered by uLDO as this
167 //! is too weak a power source.
168 //!
169 //! \note Using the DCDC power supply requires an external inductor.
170 //!
171 //! \return Returns the main power source.
172 //! - \ref PWRCTRL_PWRSRC_DCDC
173 //! - \ref PWRCTRL_PWRSRC_GLDO
174 //
175 //*****************************************************************************
176 __STATIC_INLINE uint32_t
PowerCtrlSourceGet(void)177 PowerCtrlSourceGet(void)
178 {
179     uint32_t ui32PowerConfig;
180 
181     // Return the current power source
182     ui32PowerConfig = HWREG(AON_PMCTL_BASE + AON_PMCTL_O_PWRCTL);
183     if(ui32PowerConfig & AON_PMCTL_PWRCTL_DCDC_ACTIVE)
184     {
185         return (PWRCTRL_PWRSRC_DCDC);
186     }
187     else
188     {
189         return (PWRCTRL_PWRSRC_GLDO);
190     }
191 }
192 
193 //*****************************************************************************
194 //
195 //! \brief OBSOLETE: Get the last known reset source of the system.
196 //!
197 //! \deprecated This function will be removed in a future release.
198 //! Use \ref SysCtrlResetSourceGet() instead.
199 //!
200 //! This function returns reset source but does not cover if waking up from shutdown.
201 //! This function can be seen as a subset of function \ref SysCtrlResetSourceGet()
202 //! and will be removed in a future release.
203 //!
204 //! \return Returns one of the known reset values.
205 //! The possible reset sources are:
206 //! - \ref PWRCTRL_RST_POWER_ON
207 //! - \ref PWRCTRL_RST_PIN
208 //! - \ref PWRCTRL_RST_VDDS_BOD
209 //! - \ref PWRCTRL_RST_VDD_BOD
210 //! - \ref PWRCTRL_RST_VDDR_BOD
211 //! - \ref PWRCTRL_RST_CLK_LOSS
212 //! - \ref PWRCTRL_RST_SW_PIN
213 //! - \ref PWRCTRL_RST_WARM
214 //!
215 //! \sa \ref SysCtrlResetSourceGet()
216 //
217 //*****************************************************************************
218 __STATIC_INLINE uint32_t
PowerCtrlResetSourceGet(void)219 PowerCtrlResetSourceGet(void)
220 {
221     //  Get the reset source.
222     return (( HWREG( AON_PMCTL_BASE + AON_PMCTL_O_RESETCTL ) &
223         AON_PMCTL_RESETCTL_RESET_SRC_M ) >>
224         AON_PMCTL_RESETCTL_RESET_SRC_S ) ;
225 }
226 
227 //*****************************************************************************
228 //
229 //! \brief Enables pad sleep in order to latch device outputs before shutdown.
230 //!
231 //! See \ref SysCtrlShutdown() for more information about how to enter
232 //! shutdown and how to wake up from shutdown.
233 //!
234 //! \return None
235 //!
236 //! \sa \ref PowerCtrlPadSleepDisable()
237 //
238 //*****************************************************************************
239 __STATIC_INLINE void
PowerCtrlPadSleepEnable(void)240 PowerCtrlPadSleepEnable(void)
241 {
242     HWREG(AON_PMCTL_BASE + AON_PMCTL_O_SLEEPCTL) = 0;
243     HWREG(AON_RTC_BASE + AON_RTC_O_SYNC);
244 }
245 
246 //*****************************************************************************
247 //
248 //! \brief Disables pad sleep in order to unlatch device outputs after wakeup from shutdown.
249 //!
250 //! This function must be called by the application after the device wakes up
251 //! from shutdown.
252 //!
253 //! See \ref SysCtrlShutdown() for more information about how to enter
254 //! shutdown and how to wake up from shutdown.
255 //!
256 //! \return None
257 //!
258 //! \sa \ref PowerCtrlPadSleepEnable()
259 //
260 //*****************************************************************************
261 __STATIC_INLINE void
PowerCtrlPadSleepDisable(void)262 PowerCtrlPadSleepDisable(void)
263 {
264     HWREG(AON_PMCTL_BASE + AON_PMCTL_O_SLEEPCTL) = 1;
265     HWREG(AON_RTC_BASE + AON_RTC_O_SYNC);
266 }
267 
268 //*****************************************************************************
269 //
270 // Support for DriverLib in ROM:
271 // Redirect to implementation in ROM when available.
272 //
273 //*****************************************************************************
274 #if !defined(DRIVERLIB_NOROM) && !defined(DOXYGEN)
275     #include "../driverlib/rom.h"
276     #ifdef ROM_PowerCtrlSourceSet
277         #undef  PowerCtrlSourceSet
278         #define PowerCtrlSourceSet              ROM_PowerCtrlSourceSet
279     #endif
280 #endif
281 
282 //*****************************************************************************
283 //
284 // Mark the end of the C bindings section for C++ compilers.
285 //
286 //*****************************************************************************
287 #ifdef __cplusplus
288 }
289 #endif
290 
291 #endif // __PWR_CTRL_H__
292 
293 //*****************************************************************************
294 //
295 //! Close the Doxygen group.
296 //! @}
297 //! @}
298 //
299 //*****************************************************************************
300