blob: 568140e1aae9825662084fcaee7bd9488de748c2 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
|
/**************************************************************************//**
* @file dpm.c
* @version V3.00
* @brief Debug Protection Mechanism (DPM) driver source file
*
* @copyright SPDX-License-Identifier: Apache-2.0
* @copyright Copyright (C) 2020 Nuvoton Technology Corp. All rights reserved.
*****************************************************************************/
#include "NuMicro.h"
/** @addtogroup Standard_Driver Standard Driver
@{
*/
/** @addtogroup DPM_Driver DPM Driver
@{
*/
/** @addtogroup DPM_EXPORTED_FUNCTIONS DPM Exported Functions
@{
*/
/**
* @brief Set Debug Disable
* @param[in] u32dpm The pointer of the specified DPM module
* - \ref SECURE_DPM
* - \ref NONSECURE_DPM
* @return None
* @details This macro sets Secure or Non-secure DPM debug disable.
* The debug disable function works after reset (chip reset or pin reset).
*/
void DPM_SetDebugDisable(uint32_t u32dpm)
{
DPM_T *dpm;
if(__PC()&NS_OFFSET) dpm = DPM_NS;
else dpm = DPM;
if(u32dpm == SECURE_DPM) /* Secure DPM */
{
while(dpm->STS & DPM_STS_BUSY_Msk);
dpm->CTL = (DPM->CTL & (~DPM_CTL_WVCODE_Msk)) | (DPM_CTL_WVCODE | DPM_CTL_DBGDIS_Msk);
}
else /* Non-secure DPM */
{
while(dpm->NSSTS & DPM_NSSTS_BUSY_Msk);
dpm->NSCTL = (dpm->NSCTL & (~DPM_NSCTL_WVCODE_Msk)) | (DPM_NSCTL_WVCODE | DPM_NSCTL_DBGDIS_Msk);
}
}
/**
* @brief Set Debug Lock
* @param[in] u32dpm Select DPM module. Valid values are:
* - \ref SECURE_DPM
* - \ref NONSECURE_DPM
* @return None
* @details This macro sets Secure or Non-secure DPM debug lock.
* The debug lock function works after reset (chip reset or pin reset).
*/
void DPM_SetDebugLock(uint32_t u32dpm)
{
DPM_T *dpm;
if(__PC()&NS_OFFSET) dpm = DPM_NS;
else dpm = DPM;
if(u32dpm == SECURE_DPM) /* Secure DPM */
{
while(dpm->STS & DPM_STS_BUSY_Msk);
dpm->CTL = (dpm->CTL & (~DPM_CTL_WVCODE_Msk)) | (DPM_CTL_WVCODE | DPM_CTL_LOCK_Msk);
}
else /* Non-secure DPM */
{
while(dpm->NSSTS & DPM_NSSTS_BUSY_Msk);
dpm->NSCTL = (dpm->NSCTL & (~DPM_NSCTL_WVCODE_Msk)) | (DPM_NSCTL_WVCODE | DPM_NSCTL_LOCK_Msk);
}
}
/**
* @brief Get Debug Disable
* @param[in] u32dpm Select DPM module. Valid values are:
* - \ref SECURE_DPM
* - \ref NONSECURE_DPM
* @retval 0 Debug is not in disable status
* @retval 1 Debug is in disable status
* @details This macro gets Secure or Non-secure DPM debug disable status.
* If Secure debug is disabled, debugger cannot access Secure region and can access Non-secure region only.
* If Non-secure debug is disabled, debugger cannot access all Secure and Non-secure region.
*/
uint32_t DPM_GetDebugDisable(uint32_t u32dpm)
{
uint32_t u32RetVal = 0;
DPM_T *dpm;
if(__PC()&NS_OFFSET) dpm = DPM_NS;
else dpm = DPM;
if(u32dpm == SECURE_DPM) /* Secure DPM */
{
while(dpm->STS & DPM_STS_BUSY_Msk);
u32RetVal = (dpm->STS & DPM_STS_DBGDIS_Msk) >> DPM_STS_DBGDIS_Pos;
}
else /* Non-secure DPM */
{
while(dpm->NSSTS & DPM_NSSTS_BUSY_Msk);
u32RetVal = (dpm->NSSTS & DPM_NSSTS_DBGDIS_Msk) >> DPM_NSSTS_DBGDIS_Pos;
}
return u32RetVal;
}
/**
* @brief Get Debug Lock
* @param[in] u32dpm Select DPM module. Valid values are:
* - \ref SECURE_DPM
* - \ref NONSECURE_DPM
* @retval 0 Debug is not in lock status
* @retval 1 Debug is in lock status
* @details This macro gets Secure or Non-secure DPM debug disable status.
* If Secure debug is locked, debugger cannot access Secure region and can access Non-secure region only.
* If Non-secure debug is locked, debugger cannot access all Secure and Non-secure region.
*/
uint32_t DPM_GetDebugLock(uint32_t u32dpm)
{
uint32_t u32RetVal = 0;
DPM_T *dpm;
if(__PC()&NS_OFFSET) dpm = DPM_NS;
else dpm = DPM;
if(u32dpm == SECURE_DPM) /* Secure DPM */
{
while(dpm->STS & DPM_STS_BUSY_Msk);
u32RetVal = (dpm->STS & DPM_STS_LOCK_Msk) >> DPM_STS_LOCK_Pos;
}
else /* Non-secure DPM */
{
while(dpm->NSSTS & DPM_NSSTS_BUSY_Msk);
u32RetVal = (dpm->NSSTS & DPM_NSSTS_LOCK_Msk) >> DPM_NSSTS_LOCK_Pos;
}
return u32RetVal;
}
/**
* @brief Update DPM Password
* @param[in] u32dpm Select DPM module. Valid values are:
* - \ref SECURE_DPM
* - \ref NONSECURE_DPM
* @param[in] au32Password Password length is 256 bits.
* @retval 0 No password is updated. The password update count has reached the maximum value.
* @retval 1 Password update is successful.
* @details This macro updates Secure or Non-secure DPM password.
*/
uint32_t DPM_SetPasswordUpdate(uint32_t u32dpm, uint32_t au32Pwd[])
{
uint32_t u32i, u32RetVal = 0;
DPM_T *dpm;
if(__PC()&NS_OFFSET) dpm = DPM_NS;
else dpm = DPM;
if(u32dpm == SECURE_DPM) /* Secure DPM */
{
/* Set Secure DPM password */
for(u32i = 0; u32i < 4; u32i++)
{
while(dpm->STS & DPM_STS_BUSY_Msk);
dpm->SPW[u32i] = au32Pwd[u32i];
}
/* Set Secure DPM password update */
while(dpm->STS & DPM_STS_BUSY_Msk);
dpm->CTL = (dpm->CTL & (~DPM_CTL_WVCODE_Msk)) | (DPM_CTL_WVCODE | DPM_CTL_PWUPD_Msk);
/* Check Secure DPM password update flag */
while(dpm->STS & DPM_STS_BUSY_Msk);
u32RetVal = (dpm->STS & DPM_STS_PWUOK_Msk) >> DPM_STS_PWUOK_Pos;
/* Clear Secure DPM password update flag */
if(u32RetVal) dpm->STS = DPM_STS_PWUOK_Msk;
}
else /* Non-secure DPM */
{
/* Set Non-secure DPM password */
for(u32i = 0; u32i < 4; u32i++)
{
while(dpm->NSSTS & DPM_NSSTS_BUSY_Msk);
dpm->NSPW[u32i] = au32Pwd[u32i];
}
/* Set Non-secure DPM password update */
while(dpm->NSSTS & DPM_NSSTS_BUSY_Msk);
dpm->NSCTL = (dpm->NSCTL & (~DPM_CTL_WVCODE_Msk)) | (DPM_CTL_WVCODE | DPM_NSCTL_PWUPD_Msk);
/* Check Non-secure DPM password update flag */
while(dpm->NSSTS & DPM_NSSTS_BUSY_Msk);
u32RetVal = (dpm->NSSTS & DPM_NSSTS_PWUOK_Msk) >> DPM_NSSTS_PWUOK_Pos;
/* Clear Non-secure DPM password update flag */
if(u32RetVal) dpm->NSSTS = DPM_NSSTS_PWUOK_Msk;
}
return u32RetVal;
}
/**
* @brief Compare DPM Password
* @param[in] u32dpm Select DPM module. Valid values are:
* - \ref SECURE_DPM
* - \ref NONSECURE_DPM
* @retval 0 The password comparison can be proccessed.
* @retval 1 No more password comparison can be proccessed. \n
* The password comparison fail times has reached the maximum value.
* @details This macro sets Secure or Non-secure DPM password comparison. \n
* The comparison result is checked by DPM_GetPasswordErrorFlag().
*/
uint32_t DPM_SetPasswordCompare(uint32_t u32dpm, uint32_t au32Pwd[])
{
uint32_t u32i, u32RetVal = 0;
DPM_T *dpm;
if(__PC()&NS_OFFSET) dpm = DPM_NS;
else dpm = DPM;
if(u32dpm == SECURE_DPM) /* Secure DPM */
{
/* Check Secure DPM password compare fail times maximum flag */
while(dpm->STS & DPM_STS_BUSY_Msk);
if(dpm->STS & DPM_STS_PWFMAX_Msk)
{
u32RetVal = 1;
}
else
{
/* Set Secure DPM password */
for(u32i = 0; u32i < 4; u32i++)
{
while(dpm->STS & DPM_STS_BUSY_Msk);
dpm->SPW[u32i] = au32Pwd[u32i];
}
/* Set Secure DPM password cpmpare */
while(dpm->STS & DPM_STS_BUSY_Msk);
dpm->CTL = (dpm->CTL & (~DPM_CTL_WVCODE_Msk)) | (DPM_CTL_WVCODE | DPM_CTL_PWCMP_Msk);
}
}
else /* Non-secure DPM */
{
/* Check Non-secure DPM password compare fail times maximum flag */
while(dpm->NSSTS & DPM_NSSTS_BUSY_Msk);
if(dpm->NSSTS & DPM_NSSTS_PWFMAX_Msk)
{
u32RetVal = 1;
}
else
{
/* Set Non-secure DPM password */
for(u32i = 0; u32i < 4; u32i++)
{
while(dpm->NSSTS & DPM_NSSTS_BUSY_Msk);
dpm->NSPW[u32i] = au32Pwd[u32i];
}
/* Set Non-secure DPM password compare */
while(dpm->NSSTS & DPM_NSSTS_BUSY_Msk);
dpm->NSCTL = (dpm->NSCTL & (~DPM_NSCTL_WVCODE_Msk)) | (DPM_NSCTL_WVCODE | DPM_NSCTL_PWCMP_Msk);
}
}
return u32RetVal;
}
/**
* @brief Get DPM Password Error Flag
* @param[in] u32dpm Select DPM module. Valid values are:
* - \ref SECURE_DPM
* - \ref NONSECURE_DPM
* @return Specified DPM module password compare error flag.
* @details This macro returns Secure or Non-secure DPM password compare error flag.
*/
uint32_t DPM_GetPasswordErrorFlag(uint32_t u32dpm)
{
uint32_t u32RetVal = 0;
DPM_T *dpm;
if(__PC()&NS_OFFSET) dpm = DPM_NS;
else dpm = DPM;
if(u32dpm == SECURE_DPM) /* Secure DPM */
{
/* Check Secure DPM password compare error flag */
while(dpm->STS & DPM_STS_BUSY_Msk);
u32RetVal = (dpm->STS & DPM_STS_PWCERR_Msk) >> DPM_STS_PWCERR_Pos;
}
else /* Non-secure DPM */
{
/* Check Non-secure DPM password compare error flag */
while(dpm->NSSTS & DPM_NSSTS_BUSY_Msk);
u32RetVal = (dpm->NSSTS & DPM_NSSTS_PWCERR_Msk) >> DPM_NSSTS_PWCERR_Pos;
}
return u32RetVal;
}
/**
* @brief Get DPM Interrupt Flag
* @param None
* @return Secure DPM interrupt flag.
* @details This macro returns Secure DPM interrupt flag.
* Secure DPM interrupt flag includes Secure and Non-secure DPM password compare error flag.
* This macro is for Secure DPM and Secure region only.
*/
uint32_t DPM_GetIntFlag(void)
{
while(DPM->STS & DPM_STS_BUSY_Msk);
return (DPM->STS & DPM_STS_INT_Msk) >> DPM_STS_INT_Pos;
}
/**
* @brief Clear DPM Password Error Flag
* @param[in] u32dpm Select DPM module. Valid values are:
* - \ref SECURE_DPM
* - \ref NONSECURE_DPM
* @return Specified DPM module interrupt flag.
* @details This macro clears Secure or Non-secure DPM password compare error flag.
*/
void DPM_ClearPasswordErrorFlag(uint32_t u32dpm)
{
DPM_T *dpm;
if(__PC()&NS_OFFSET) dpm = DPM_NS;
else dpm = DPM;
if(u32dpm == SECURE_DPM) /* Secure DPM */
{
while(dpm->STS & DPM_STS_BUSY_Msk);
dpm->STS = DPM_STS_PWCERR_Msk;
}
else /* Non-secure DPM */
{
while(dpm->NSSTS & DPM_NSSTS_BUSY_Msk);
dpm->NSSTS = DPM_NSSTS_PWCERR_Msk;
}
}
/**
* @brief Enable Debugger Write Access
* @param[in] u32dpm Select DPM module. Valid values are:
* - \ref SECURE_DPM
* - \ref NONSECURE_DPM
* @return None.
* @details This macro enables external debugger to write Secure or Non-secure DPM registers.
*/
void DPM_EnableDebuggerWriteAccess(uint32_t u32dpm)
{
DPM_T *dpm;
if(__PC()&NS_OFFSET) dpm = DPM_NS;
else dpm = DPM;
if(u32dpm == SECURE_DPM) /* Secure DPM */
{
while(dpm->STS & DPM_STS_BUSY_Msk);
dpm->CTL = (dpm->CTL & (~(DPM_CTL_RVCODE_Msk | DPM_CTL_DACCWDIS_Msk))) | DPM_CTL_WVCODE;
}
else /* Non-secure DPM */
{
while(dpm->NSSTS & DPM_NSSTS_BUSY_Msk);
dpm->NSCTL = (dpm->NSCTL & (~(DPM_NSCTL_RVCODE_Msk | DPM_NSCTL_DACCWDIS_Msk))) | DPM_NSCTL_WVCODE;
}
}
/**
* @brief Disable Debugger Write Access
* @param[in] u32dpm Select DPM module. Valid values are:
* - \ref SECURE_DPM
* - \ref NONSECURE_DPM
* @return None.
* @details This macro disables external debugger to write Secure or Non-secure DPM registers.
*/
void DPM_DisableDebuggerWriteAccess(uint32_t u32dpm)
{
DPM_T *dpm;
if(__PC()&NS_OFFSET) dpm = DPM_NS;
else dpm = DPM;
if(u32dpm == SECURE_DPM) /* Secure DPM */
{
while(dpm->STS & DPM_STS_BUSY_Msk);
dpm->CTL = (dpm->CTL & (~DPM_CTL_RVCODE_Msk)) | (DPM_CTL_WVCODE | DPM_CTL_DACCWDIS_Msk);
}
else /* Non-secure DPM */
{
while(dpm->NSSTS & DPM_NSSTS_BUSY_Msk);
dpm->NSCTL = (dpm->NSCTL & (~DPM_NSCTL_RVCODE_Msk)) | (DPM_NSCTL_WVCODE | DPM_NSCTL_DACCWDIS_Msk);
}
}
/**@}*/ /* end of group DPM_EXPORTED_FUNCTIONS */
/**@}*/ /* end of group DPM_Driver */
/**@}*/ /* end of group Standard_Driver */
|