1 /*--------------------------------------------------------------------------*/
2 /* Copyright 2020-2021 NXP */
3 /* */
4 /* NXP Confidential. This software is owned or controlled by NXP and may */
5 /* only be used strictly in accordance with the applicable license terms. */
6 /* By expressly accepting such terms or by downloading, installing, */
7 /* activating and/or otherwise using the software, you are agreeing that */
8 /* you have read, and that you agree to comply with and are bound by, such */
9 /* license terms. If you do not agree to be bound by the applicable license */
10 /* terms, then you may not retain, install, activate or otherwise use the */
11 /* software. */
12 /*--------------------------------------------------------------------------*/
13
14 /** @file mcuxClCss_Common.c
15 * @brief CSSv2 implementation for common functionality.
16 * This file implements the functions declared in mcuxClCss_Common.h and adds helper functions used by other implementation headers. */
17
18 #include <stdbool.h>
19 #include <platform_specific_headers.h>
20 #include <mcuxClCss_Types.h>
21 #include <mcuxClMemory.h>
22 #include <mcuxCsslFlowProtection.h>
23 #include <mcuxClCss.h>
24 #include <internal/mcuxClCss_Internal.h>
25
MCUX_CSSL_FP_FUNCTION_DEF(mcuxClCss_GetHwVersion)26 MCUX_CSSL_FP_FUNCTION_DEF(mcuxClCss_GetHwVersion)
27 MCUXCLCSS_API mcuxClCss_Status_Protected_t mcuxClCss_GetHwVersion(
28 mcuxClCss_HwVersion_t * result)
29 {
30 MCUX_CSSL_FP_FUNCTION_ENTRY(mcuxClCss_GetHwVersion);
31 result->word.value = IP_CSS->CSS_VERSION;
32 MCUX_CSSL_FP_FUNCTION_EXIT(mcuxClCss_GetHwVersion, MCUXCLCSS_STATUS_OK);
33 }
34
MCUX_CSSL_FP_FUNCTION_DEF(mcuxClCss_GetHwConfig)35 MCUX_CSSL_FP_FUNCTION_DEF(mcuxClCss_GetHwConfig)
36 MCUXCLCSS_API mcuxClCss_Status_Protected_t mcuxClCss_GetHwConfig(
37 mcuxClCss_HwConfig_t * result)
38 {
39 MCUX_CSSL_FP_FUNCTION_ENTRY(mcuxClCss_GetHwConfig);
40 result->word.value = IP_CSS->CSS_CONFIG;
41 MCUX_CSSL_FP_FUNCTION_EXIT(mcuxClCss_GetHwConfig, MCUXCLCSS_STATUS_OK);
42 }
43
MCUX_CSSL_FP_FUNCTION_DEF(mcuxClCss_GetHwState)44 MCUX_CSSL_FP_FUNCTION_DEF(mcuxClCss_GetHwState)
45 MCUXCLCSS_API mcuxClCss_Status_Protected_t mcuxClCss_GetHwState(
46 mcuxClCss_HwState_t * result)
47 {
48 MCUX_CSSL_FP_FUNCTION_ENTRY(mcuxClCss_GetHwState);
49 result->word.value = IP_CSS->CSS_STATUS;
50 MCUX_CSSL_FP_FUNCTION_EXIT(mcuxClCss_GetHwState, MCUXCLCSS_STATUS_OK);
51 }
52
MCUX_CSSL_FP_FUNCTION_DEF(mcuxClCss_Enable_Async)53 MCUX_CSSL_FP_FUNCTION_DEF(mcuxClCss_Enable_Async)
54 MCUXCLCSS_API mcuxClCss_Status_Protected_t mcuxClCss_Enable_Async(
55 void)
56 {
57 MCUX_CSSL_FP_FUNCTION_ENTRY(mcuxClCss_Enable_Async);
58 IP_CSS->CSS_CTRL_b.CSS_EN = 1U;
59 MCUX_CSSL_FP_FUNCTION_EXIT(mcuxClCss_Enable_Async, MCUXCLCSS_STATUS_OK_WAIT);
60 }
61
MCUX_CSSL_FP_FUNCTION_DEF(mcuxClCss_Disable)62 MCUX_CSSL_FP_FUNCTION_DEF(mcuxClCss_Disable)
63 MCUXCLCSS_API mcuxClCss_Status_Protected_t mcuxClCss_Disable(
64 void)
65 {
66 MCUX_CSSL_FP_FUNCTION_ENTRY(mcuxClCss_Disable);
67 IP_CSS->CSS_CTRL_b.CSS_EN = 0U;
68 MCUX_CSSL_FP_FUNCTION_EXIT(mcuxClCss_Disable, MCUXCLCSS_STATUS_OK);
69 }
70
MCUX_CSSL_FP_FUNCTION_DEF(mcuxClCss_GetErrorCode)71 MCUX_CSSL_FP_FUNCTION_DEF(mcuxClCss_GetErrorCode)
72 MCUXCLCSS_API mcuxClCss_Status_Protected_t mcuxClCss_GetErrorCode(
73 mcuxClCss_ErrorHandling_t errorHandling)
74 {
75 MCUX_CSSL_FP_FUNCTION_ENTRY(mcuxClCss_GetErrorCode);
76
77 mcuxClCss_Status_t result = MCUXCLCSS_STATUS_SW_FAULT;
78 if (1U == IP_CSS->CSS_STATUS_b.CSS_ERR)
79 {
80 if (1U == IP_CSS->CSS_ERR_STATUS_b.FLT_ERR)
81 {
82 result = MCUXCLCSS_STATUS_HW_FAULT;
83 }
84 else if (1U == IP_CSS->CSS_ERR_STATUS_b.ITG_ERR)
85 {
86 result = MCUXCLCSS_STATUS_HW_INTEGRITY;
87 }
88 else if (1U == IP_CSS->CSS_ERR_STATUS_b.OPN_ERR)
89 {
90 result = MCUXCLCSS_STATUS_HW_OPERATIONAL;
91 }
92 else if (1U == IP_CSS->CSS_ERR_STATUS_b.ALG_ERR)
93 {
94 result = MCUXCLCSS_STATUS_HW_ALGORITHM;
95 }
96 else if (1U == IP_CSS->CSS_ERR_STATUS_b.BUS_ERR)
97 {
98 result = MCUXCLCSS_STATUS_HW_BUS;
99 }
100 else if (1U == IP_CSS->CSS_ERR_STATUS_b.PRNG_ERR)
101 {
102 result = MCUXCLCSS_STATUS_HW_PRNG;
103 }
104 else if (1U == IP_CSS->CSS_ERR_STATUS_b.DTRNG_ERR)
105 {
106 result = MCUXCLCSS_STATUS_HW_DTRNG;
107 }
108 else
109 {
110 result = MCUXCLCSS_STATUS_SW_FAULT;
111 }
112 }
113 else
114 {
115 result = MCUXCLCSS_STATUS_OK;
116 }
117
118 if (MCUXCLCSS_ERROR_FLAGS_CLEAR == errorHandling){
119 (void) mcuxClCss_ResetErrorFlags(); /* always returns MCUXCLCSS_STATUS_OK. */
120 }
121
122 MCUX_CSSL_FP_FUNCTION_EXIT(mcuxClCss_GetErrorCode, result);
123 }
124
MCUX_CSSL_FP_FUNCTION_DEF(mcuxClCss_GetErrorLevel)125 MCUX_CSSL_FP_FUNCTION_DEF(mcuxClCss_GetErrorLevel)
126 MCUXCLCSS_API mcuxClCss_Status_Protected_t mcuxClCss_GetErrorLevel(
127 mcuxClCss_ErrorHandling_t errorHandling,
128 uint32_t *errorLevel)
129 {
130 MCUX_CSSL_FP_FUNCTION_ENTRY(mcuxClCss_GetErrorLevel);
131
132 *errorLevel = IP_CSS->CSS_ERR_STATUS_b.ERR_LVL;
133
134 MCUX_CSSL_FP_FUNCTION_CALL(result, mcuxClCss_GetErrorCode(errorHandling));
135
136 /* Exit function with expectation: mcuxClCss_GetErrorCode was called unconditionally */
137 MCUX_CSSL_FP_FUNCTION_EXIT(mcuxClCss_GetErrorLevel, result,
138 MCUX_CSSL_FP_FUNCTION_CALLED(mcuxClCss_GetErrorCode));
139 }
140
MCUX_CSSL_FP_FUNCTION_DEF(mcuxClCss_WaitForOperation)141 MCUX_CSSL_FP_FUNCTION_DEF(mcuxClCss_WaitForOperation)
142 MCUXCLCSS_API mcuxClCss_Status_Protected_t mcuxClCss_WaitForOperation(
143 mcuxClCss_ErrorHandling_t errorHandling)
144 {
145 /* Enter flow-protected function with expectation: mcuxClCss_GetErrorCode will be called (unconditionally) */
146 MCUX_CSSL_FP_FUNCTION_ENTRY(mcuxClCss_WaitForOperation,
147 MCUX_CSSL_FP_FUNCTION_CALLED(mcuxClCss_GetErrorCode));
148
149 while (MCUXCLCSS_ISBUSY)
150 {
151 // Do nothing
152 }
153
154 MCUX_CSSL_FP_FUNCTION_CALL(result, mcuxClCss_GetErrorCode(errorHandling));
155
156 MCUX_CSSL_FP_FUNCTION_EXIT(mcuxClCss_WaitForOperation, result);
157 }
158
MCUX_CSSL_FP_FUNCTION_DEF(mcuxClCss_LimitedWaitForOperation)159 MCUX_CSSL_FP_FUNCTION_DEF(mcuxClCss_LimitedWaitForOperation)
160 MCUXCLCSS_API mcuxClCss_Status_Protected_t mcuxClCss_LimitedWaitForOperation(
161 uint32_t counterLimit,
162 mcuxClCss_ErrorHandling_t errorHandling)
163 {
164 MCUX_CSSL_FP_FUNCTION_ENTRY(mcuxClCss_LimitedWaitForOperation);
165
166 bool counterExpired = true;
167 while (0U != counterLimit)
168 {
169 if (!MCUXCLCSS_ISBUSY)
170 {
171 counterExpired = false;
172 break;
173 }
174 counterLimit--;
175 }
176
177 if (true == counterExpired)
178 {
179 MCUX_CSSL_FP_FUNCTION_EXIT(mcuxClCss_LimitedWaitForOperation, MCUXCLCSS_STATUS_SW_COUNTER_EXPIRED);
180 }
181
182 MCUX_CSSL_FP_FUNCTION_CALL(result, mcuxClCss_GetErrorCode(errorHandling));
183
184 /* Exit function with expectation: mcuxClCss_GetErrorCode was called */
185 MCUX_CSSL_FP_FUNCTION_EXIT(mcuxClCss_LimitedWaitForOperation, result,
186 MCUX_CSSL_FP_FUNCTION_CALLED(mcuxClCss_GetErrorCode));
187 }
188
MCUX_CSSL_FP_FUNCTION_DEF(mcuxClCss_ResetErrorFlags)189 MCUX_CSSL_FP_FUNCTION_DEF(mcuxClCss_ResetErrorFlags)
190 MCUXCLCSS_API mcuxClCss_Status_Protected_t mcuxClCss_ResetErrorFlags(
191 void)
192 {
193 MCUX_CSSL_FP_FUNCTION_ENTRY(mcuxClCss_ResetErrorFlags);
194 IP_CSS->CSS_ERR_STATUS_CLR = (uint32_t) MCUXCLCSS_ERROR_FLAGS_CLEAR;
195 // Poll error bit to be sure that error bits has been cleared. Required by HW spec.
196 while (1U == IP_CSS->CSS_STATUS_b.CSS_ERR)
197 {
198 // Do nothing
199 }
200 MCUX_CSSL_FP_FUNCTION_EXIT(mcuxClCss_ResetErrorFlags, MCUXCLCSS_STATUS_OK);
201 }
202
MCUX_CSSL_FP_FUNCTION_DEF(mcuxClCss_Reset_Async)203 MCUX_CSSL_FP_FUNCTION_DEF(mcuxClCss_Reset_Async)
204 MCUXCLCSS_API mcuxClCss_Status_Protected_t mcuxClCss_Reset_Async(
205 mcuxClCss_ResetOption_t options)
206 {
207 MCUX_CSSL_FP_FUNCTION_ENTRY(mcuxClCss_Reset_Async);
208 if (MCUXCLCSS_ISBUSY && (MCUXCLCSS_RESET_DO_NOT_CANCEL == options))
209 {
210 MCUX_CSSL_FP_FUNCTION_EXIT(mcuxClCss_Reset_Async, MCUXCLCSS_STATUS_SW_CANNOT_INTERRUPT);
211 }
212
213 IP_CSS->CSS_CTRL_b.CSS_RESET = 1U;
214 MCUX_CSSL_FP_FUNCTION_EXIT(mcuxClCss_Reset_Async, MCUXCLCSS_STATUS_OK_WAIT);
215 }
216
MCUX_CSSL_FP_FUNCTION_DEF(mcuxClCss_SetIntEnableFlags)217 MCUX_CSSL_FP_FUNCTION_DEF(mcuxClCss_SetIntEnableFlags)
218 MCUXCLCSS_API mcuxClCss_Status_Protected_t mcuxClCss_SetIntEnableFlags(
219 mcuxClCss_InterruptOptionEn_t options)
220 {
221 MCUX_CSSL_FP_FUNCTION_ENTRY(mcuxClCss_SetIntEnableFlags);
222 IP_CSS->CSS_INT_ENABLE = options.word.value;
223 MCUX_CSSL_FP_FUNCTION_EXIT(mcuxClCss_SetIntEnableFlags, MCUXCLCSS_STATUS_OK);
224 }
225
MCUX_CSSL_FP_FUNCTION_DEF(mcuxClCss_GetIntEnableFlags)226 MCUX_CSSL_FP_FUNCTION_DEF(mcuxClCss_GetIntEnableFlags)
227 MCUXCLCSS_API mcuxClCss_Status_Protected_t mcuxClCss_GetIntEnableFlags(
228 mcuxClCss_InterruptOptionEn_t * result)
229 {
230 MCUX_CSSL_FP_FUNCTION_ENTRY(mcuxClCss_GetIntEnableFlags);
231 result->word.value = IP_CSS->CSS_INT_ENABLE_b.INT_EN;
232 MCUX_CSSL_FP_FUNCTION_EXIT(mcuxClCss_GetIntEnableFlags, MCUXCLCSS_STATUS_OK);
233 }
234
MCUX_CSSL_FP_FUNCTION_DEF(mcuxClCss_ResetIntFlags)235 MCUX_CSSL_FP_FUNCTION_DEF(mcuxClCss_ResetIntFlags)
236 MCUXCLCSS_API mcuxClCss_Status_Protected_t mcuxClCss_ResetIntFlags(
237 mcuxClCss_InterruptOptionRst_t options)
238 {
239 MCUX_CSSL_FP_FUNCTION_ENTRY(mcuxClCss_ResetIntFlags);
240 IP_CSS->CSS_INT_STATUS_CLR = options.word.value;
241 MCUX_CSSL_FP_FUNCTION_EXIT(mcuxClCss_ResetIntFlags, MCUXCLCSS_STATUS_OK);
242 }
243
MCUX_CSSL_FP_FUNCTION_DEF(mcuxClCss_SetIntFlags)244 MCUX_CSSL_FP_FUNCTION_DEF(mcuxClCss_SetIntFlags)
245 MCUXCLCSS_API mcuxClCss_Status_Protected_t mcuxClCss_SetIntFlags(
246 mcuxClCss_InterruptOptionSet_t options)
247 {
248 MCUX_CSSL_FP_FUNCTION_ENTRY(mcuxClCss_SetIntFlags);
249 IP_CSS->CSS_INT_STATUS_SET = options.word.value;
250 MCUX_CSSL_FP_FUNCTION_EXIT(mcuxClCss_SetIntFlags, MCUXCLCSS_STATUS_OK);
251 }
252
MCUX_CSSL_FP_FUNCTION_DEF(mcuxClCss_SetRandomStartDelay)253 MCUX_CSSL_FP_FUNCTION_DEF(mcuxClCss_SetRandomStartDelay)
254 MCUXCLCSS_API mcuxClCss_Status_Protected_t mcuxClCss_SetRandomStartDelay(
255 uint32_t delay)
256 {
257 MCUX_CSSL_FP_FUNCTION_ENTRY(mcuxClCss_SetRandomStartDelay);
258 MCUXCLCSS_INPUT_PARAM_CHECK_PROTECTED(mcuxClCss_SetRandomStartDelay, 1024u < delay);
259
260 if (MCUXCLCSS_ISBUSY)
261 {
262 MCUX_CSSL_FP_FUNCTION_EXIT(mcuxClCss_SetRandomStartDelay, MCUXCLCSS_STATUS_SW_CANNOT_INTERRUPT);
263 }
264
265 IP_CSS->CSS_CFG_b.ADCTRL = delay;
266
267 MCUX_CSSL_FP_FUNCTION_EXIT(mcuxClCss_SetRandomStartDelay, MCUXCLCSS_STATUS_OK);
268 }
269
MCUX_CSSL_FP_FUNCTION_DEF(mcuxClCss_GetRandomStartDelay)270 MCUX_CSSL_FP_FUNCTION_DEF(mcuxClCss_GetRandomStartDelay)
271 MCUXCLCSS_API mcuxClCss_Status_Protected_t mcuxClCss_GetRandomStartDelay(
272 uint32_t *delay)
273 {
274 MCUX_CSSL_FP_FUNCTION_ENTRY(mcuxClCss_GetRandomStartDelay);
275
276 * delay = IP_CSS->CSS_CFG_b.ADCTRL;
277
278 MCUX_CSSL_FP_FUNCTION_EXIT(mcuxClCss_GetRandomStartDelay, MCUXCLCSS_STATUS_OK);
279 }
280
281
282
283
284