1 /*
2  * Copyright (c) 2019 - 2024, Nordic Semiconductor ASA
3  * All rights reserved.
4  *
5  * SPDX-License-Identifier: BSD-3-Clause
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright notice, this
11  *    list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * 3. Neither the name of the copyright holder nor the names of its
18  *    contributors may be used to endorse or promote products derived from this
19  *    software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
25  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31  * POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 #ifndef NRF_BPROT_H__
35 #define NRF_BPROT_H__
36 
37 #include <nrfx.h>
38 
39 #ifdef __cplusplus
40 extern "C" {
41 #endif
42 
43 /**
44  * @defgroup nrf_bprot_hal BPROT HAL
45  * @{
46  * @ingroup nrf_bprot
47  * @brief   Hardware access layer for managing the Block Protection (BPROT) mechanism.
48  */
49 
50 /**
51  * @brief Function for enabling protection for specified non-volatile memory blocks.
52  *
53  * Blocks are arranged into groups of 32 blocks each. Each block size is 4 kB.
54  * Any attempt to write or erase a protected block will result in hard fault.
55  * The memory block protection can be disabled only by resetting the device.
56  *
57  * @param[in] p_reg      Pointer to the structure of registers of the peripheral.
58  * @param[in] group_idx  Non-volatile memory group containing memory blocks to protect.
59  * @param[in] block_mask Non-volatile memory blocks to protect. Each bit in bitmask represents
60  *                       one memory block in the specified group.
61  */
62 NRF_STATIC_INLINE void nrf_bprot_nvm_blocks_protection_enable(NRF_BPROT_Type * p_reg,
63                                                               uint8_t          group_idx,
64                                                               uint32_t         block_mask);
65 
66 /**
67  * @brief Function for setting the non-volatile memory (NVM) protection during debug.
68  *
69  * NVM protection is disabled by default while debugging.
70  *
71  * @param[in] p_reg  Pointer to the structure of registers of the peripheral.
72  * @param[in] enable True if NVM protection during debug is to be enabled, false otherwise.
73  */
74 NRF_STATIC_INLINE void nrf_bprot_nvm_protection_in_debug_set(NRF_BPROT_Type * p_reg,
75                                                              bool             enable);
76 
77 #ifndef NRF_DECLARE_ONLY
78 
nrf_bprot_nvm_blocks_protection_enable(NRF_BPROT_Type * p_reg,uint8_t group_idx,uint32_t block_mask)79 NRF_STATIC_INLINE void nrf_bprot_nvm_blocks_protection_enable(NRF_BPROT_Type * p_reg,
80                                                               uint8_t          group_idx,
81                                                               uint32_t         block_mask)
82 {
83     switch (group_idx)
84     {
85         case 0:
86             p_reg->CONFIG0 = block_mask;
87             break;
88 
89         case 1:
90             p_reg->CONFIG1 = block_mask;
91             break;
92 
93 #if defined(BPROT_CONFIG2_REGION64_Pos)
94         case 2:
95             p_reg->CONFIG2 = block_mask;
96             break;
97 #endif
98 
99 #if defined(BPROT_CONFIG3_REGION96_Pos)
100         case 3:
101             p_reg->CONFIG3 = block_mask;
102             break;
103 #endif
104 
105         default:
106             NRFX_ASSERT(false);
107             break;
108     }
109 }
110 
nrf_bprot_nvm_protection_in_debug_set(NRF_BPROT_Type * p_reg,bool enable)111 NRF_STATIC_INLINE void nrf_bprot_nvm_protection_in_debug_set(NRF_BPROT_Type * p_reg,
112                                                              bool             enable)
113 {
114     p_reg->DISABLEINDEBUG =
115         (enable ? 0 : BPROT_DISABLEINDEBUG_DISABLEINDEBUG_Msk);
116 }
117 
118 #endif // NRF_DECLARE_ONLY
119 
120 /** @} */
121 
122 #ifdef __cplusplus
123 }
124 #endif
125 
126 #endif // NRF_BPROT_H__
127