1 /*
2 
3 Copyright (c) 2009-2024 ARM Limited. All rights reserved.
4 
5     SPDX-License-Identifier: Apache-2.0
6 
7 Licensed under the Apache License, Version 2.0 (the License); you may
8 not use this file except in compliance with the License.
9 You may obtain a copy of the License at
10 
11     www.apache.org/licenses/LICENSE-2.0
12 
13 Unless required by applicable law or agreed to in writing, software
14 distributed under the License is distributed on an AS IS BASIS, WITHOUT
15 WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 See the License for the specific language governing permissions and
17 limitations under the License.
18 
19 NOTICE: This file has been modified by Nordic Semiconductor ASA.
20 
21 */
22 
23 #ifndef SYSTEM_NRF52_APPROTECT_H
24 #define SYSTEM_NRF52_APPROTECT_H
25 
26 #include "nrf.h"
27 #include "nrf52_erratas.h"
28 
29 #ifdef __cplusplus
30 extern "C" {
31 #endif
32 
33 
34 /* Function that handles firmware-driven enabling or disabling of APPROTECT on devices where it is supported.
35         If ENABLE_APPROTECT is defined, the FW will lock the fw branch of the APPROTECT mechanism,
36                             preventing it from being opened.
37         Otherwise, the fw branch state is loaded from UICR, emulating the legacy APPROTECT behavior.
38 
39          The same mechanism is implemented for SECURE APPROTECT, with the macros
40          ENABLE_SECURE_APPROTECT and ENABLE_SECURE_APPROTECT_USER_HANDLING. */
nrf52_handle_approtect(void)41 static inline void nrf52_handle_approtect(void)
42 {
43     #if NRF52_CONFIGURATION_249_PRESENT
44         #if defined (ENABLE_APPROTECT)
45             if (nrf52_configuration_249())
46             {
47                 /* Prevent processor from unlocking APPROTECT soft branch after this point. */
48                 NRF_APPROTECT->FORCEPROTECT = APPROTECT_FORCEPROTECT_FORCEPROTECT_Force;
49             }
50         #else
51             if (nrf52_configuration_249())
52             {
53                 /* Load APPROTECT soft branch from UICR.
54                    If UICR->APPROTECT is disabled, POWER->APPROTECT will be disabled. */
55                 NRF_APPROTECT->DISABLE = NRF_UICR->APPROTECT;
56             }
57         #endif
58     #endif
59 }
60 
61 #ifdef __cplusplus
62 }
63 #endif
64 
65 #endif /* SYSTEM_NRF52_APPROTECT_H */
66