1 /*
2  * Copyright 2019-2021 NXP
3  * All rights reserved.
4  *
5  * SPDX-License-Identifier: BSD-3-Clause
6  */
7 
8 #include "fsl_soc_src.h"
9 
10 /* Component ID definition, used by tools. */
11 #ifndef FSL_COMPONENT_ID
12 #define FSL_COMPONENT_ID "platform.drivers.soc_src"
13 #endif
14 
15 /*******************************************************************************
16  * Definitions
17  ******************************************************************************/
18 #define SRC_GLOBAL_SYSTEM_RESET_BEHAVIOR_MASK (0x3U)
19 #define SRC_GLOBAL_SYSTEM_RESET_BEHAVIOR_CONFIG(resetSource, resetMode) \
20     ((uint32_t)(resetMode) << (uint32_t)(resetSource))
21 
22 #define SRC_SLICE_CTRL_SW_RESET_MASK (0x1U)
23 
24 /*******************************************************************************
25  * Prototypes
26  ******************************************************************************/
27 
28 /*******************************************************************************
29  * Variables
30  ******************************************************************************/
31 
32 /*******************************************************************************
33  * Code
34  ******************************************************************************/
35 
36 /*!
37  * brief Release related core reset operation.
38  *
39  * The core reset will be held until boot core to release it.
40  *
41  * param base SRC peripheral base address.
42  * param coreName The name of the reset core to be released.
43  */
SRC_ReleaseCoreReset(SRC_Type * base,src_core_name_t coreName)44 void SRC_ReleaseCoreReset(SRC_Type *base, src_core_name_t coreName)
45 {
46     uint32_t coreMaskArray[] = {SRC_SCR_BT_RELEASE_M7_MASK, SRC_SCR_BT_RELEASE_M4_MASK};
47     uint32_t regValue;
48 
49     regValue = base->SCR;
50 
51     if ((regValue & coreMaskArray[((uint32_t)coreName) - 1UL]) == 0UL)
52     {
53         base->SCR |= coreMaskArray[((uint32_t)coreName) - 1UL];
54     }
55 }
56 
57 /*!
58  * brief Sets the reset mode of global system reset source.
59  *
60  * This function sets the selected mode of the input global system reset sources. This function will return as soon as
61  * the reset if finished.
62  *
63  * param base SRC peripheral base address.
64  * param resetSource The global system reset source. See @ref src_global_system_reset_source_t for more details.
65  * param resetMode The reset mode of each reset source. See @ref src_global_system_reset_mode_t for more details.
66  */
SRC_SetGlobalSystemResetMode(SRC_Type * base,src_global_system_reset_source_t resetSource,src_global_system_reset_mode_t resetMode)67 void SRC_SetGlobalSystemResetMode(SRC_Type *base,
68                                   src_global_system_reset_source_t resetSource,
69                                   src_global_system_reset_mode_t resetMode)
70 {
71     uint32_t regValue;
72 
73     regValue = base->SRMR;
74     regValue &= ~SRC_GLOBAL_SYSTEM_RESET_BEHAVIOR_CONFIG(resetSource, SRC_GLOBAL_SYSTEM_RESET_BEHAVIOR_MASK);
75     regValue |= SRC_GLOBAL_SYSTEM_RESET_BEHAVIOR_CONFIG(resetSource, resetMode);
76 
77     base->SRMR = regValue;
78 }
79 
80 /*!
81  * brief Asserts software reset for the selected slice.
82  *
83  * param base SRC peripheral base address.
84  * param sliceName The selected reset slice. See @ref src_reset_slice_name_t for more details.
85  */
SRC_AssertSliceSoftwareReset(SRC_Type * base,src_reset_slice_name_t sliceName)86 void SRC_AssertSliceSoftwareReset(SRC_Type *base, src_reset_slice_name_t sliceName)
87 {
88     uint32_t regAddress;
89     uint32_t sliceStatusRegAddress;
90 
91     regAddress            = SRC_GET_SLICE_REGISTER_ADDRESS(base, sliceName, SRC_SLICE_CONTROL_REGISTER_OFFSET);
92     sliceStatusRegAddress = SRC_GET_SLICE_REGISTER_ADDRESS(base, sliceName, SRC_SLICE_STATUS_REGISTER_OFFSET);
93 
94     *(volatile uint32_t *)regAddress |= SRC_SLICE_CTRL_SW_RESET_MASK;
95 
96     while (((*(volatile uint32_t *)sliceStatusRegAddress) & SRC_SLICE_STAT_UNDER_RST_MASK) != 0UL)
97     {
98         ;
99     }
100 }
101 
102 /*!
103  * brief Locks the value of SETPOINT_MODE and DOMAIN_MODE for the selected reset slice.
104  *
105  * param base SRC peripheral base address.
106  * param sliceName The selected reset slice. See @ref src_reset_slice_name_t for more details.
107  */
SRC_LockSliceMode(SRC_Type * base,src_reset_slice_name_t sliceName)108 void SRC_LockSliceMode(SRC_Type *base, src_reset_slice_name_t sliceName)
109 {
110     uint32_t authenticationRegAddress;
111 
112     authenticationRegAddress =
113         SRC_GET_SLICE_REGISTER_ADDRESS(base, sliceName, SRC_SLICE_AUTHENTICATION_REGISTER_OFFSET);
114 
115     *(volatile uint32_t *)authenticationRegAddress |= SRC_SLICE_AUTHEN_LOCK_MODE_MASK;
116 }
117 
118 /*!
119  * brief Sets setpoint configuration for the selected reset slice.
120  *
121  * param base SRC peripheral base address.
122  * param sliceName The selected reset slice. See @ref src_reset_slice_name_t for more details.
123  * param setPointConfig The logic OR'ed value of @ref _src_setpoint_selection. When the system in the selected setpoint
124  * slice reset will be assert.
125  */
SRC_SetSliceSetPointConfig(SRC_Type * base,src_reset_slice_name_t sliceName,uint32_t setpointConfig)126 void SRC_SetSliceSetPointConfig(SRC_Type *base, src_reset_slice_name_t sliceName, uint32_t setpointConfig)
127 {
128     uint32_t setpointConfigRegAddress;
129 
130     setpointConfigRegAddress =
131         SRC_GET_SLICE_REGISTER_ADDRESS(base, sliceName, SRC_SLICE_SETPOINT_CONFIG_REGISTER_OFFSET);
132 
133     if (setpointConfig != 0UL)
134     {
135         *(volatile uint32_t *)setpointConfigRegAddress = setpointConfig;
136     }
137 }
138 
139 /*!
140  * brief Sets domain mode configuration for the selected reset slice.
141  *
142  * param base SRC peripheral base address.
143  * param sliceName The selected reset slice. See src_reset_slice_name_t for more details.
144  * param domainConfig The logic OR'ed value of _src_domain_mode_selection.
145  */
SRC_SetSliceDomainModeConfig(SRC_Type * base,src_reset_slice_name_t sliceName,uint32_t domainConfig)146 void SRC_SetSliceDomainModeConfig(SRC_Type *base, src_reset_slice_name_t sliceName, uint32_t domainConfig)
147 {
148     uint32_t domainConfigRegAddress;
149 
150     domainConfigRegAddress = SRC_GET_SLICE_REGISTER_ADDRESS(base, sliceName, SRC_SLICE_DOMAIN_CONFIG_REGISTER_OFFSET);
151 
152     if (domainConfig != 0UL)
153     {
154         *(volatile uint32_t *)domainConfigRegAddress = domainConfig;
155     }
156 }
157 
158 /*!
159  * brief Gets the reset state of the selected slice.
160  *
161  * param base SRC peripheral base address.
162  * param sliceName The selected slice. See @ref src_reset_slice_name_t for more details.
163  * retval kSRC_SliceResetInProcess The reset is in process.
164  * retval kSRC_SliceResetFinished  The reset is finished.
165  */
SRC_GetSliceResetState(SRC_Type * base,src_reset_slice_name_t sliceName)166 src_slice_reset_state_t SRC_GetSliceResetState(SRC_Type *base, src_reset_slice_name_t sliceName)
167 {
168     uint32_t statusRegAddress;
169     src_slice_reset_state_t ret;
170 
171     statusRegAddress = SRC_GET_SLICE_REGISTER_ADDRESS(base, sliceName, SRC_SLICE_STATUS_REGISTER_OFFSET);
172 
173     if (((*(uint32_t *)statusRegAddress) & SRC_SLICE_STAT_UNDER_RST_MASK) != 0UL)
174     {
175         ret = kSRC_SliceResetInProcess;
176     }
177     else
178     {
179         ret = kSRC_SliceResetFinished;
180     }
181 
182     return ret;
183 }
184