1 /*
2  * Copyright (c) 2018 - 2020, Nordic Semiconductor ASA
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice, this
9  *    list of conditions and the following disclaimer.
10  *
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * 3. Neither the name of the copyright holder nor the names of its
16  *    contributors may be used to endorse or promote products derived from this
17  *    software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
23  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #ifndef NRF_ACL_H__
33 #define NRF_ACL_H__
34 
35 #include <nrfx.h>
36 #include <hal/nrf_ficr.h>
37 
38 #ifdef __cplusplus
39 extern "C" {
40 #endif
41 
42 #define NRF_ACL_REGION_SIZE_MAX \
43     (nrf_ficr_codepagesize_get(NRF_FICR) * nrf_ficr_codesize_get(NRF_FICR))
44 
45 /**
46  * @defgroup nrf_acl_hal ACL HAL
47  * @{
48  * @ingroup nrf_acl
49  * @brief   Hardware access layer for managing the Access Control List (ACL) peripheral.
50  */
51 
52 /** @brief ACL permissions. */
53 typedef enum
54 {
55     NRF_ACL_PERM_READ_NO_WRITE    = ACL_ACL_PERM_WRITE_Msk,                        /**< Read allowed, write disallowed. */
56     NRF_ACL_PERM_NO_READ_WRITE    = ACL_ACL_PERM_READ_Msk,                         /**< Read disallowed, write allowed. */
57     NRF_ACL_PERM_NO_READ_NO_WRITE = ACL_ACL_PERM_READ_Msk | ACL_ACL_PERM_WRITE_Msk /**< Read disallowed, write disallowed. */
58 } nrf_acl_perm_t;
59 
60 /**
61  * @brief Function for setting region parameters for given ACL region.
62  *
63  * Address must be word and page aligned. Size must be page aligned.
64  *
65  * @param[in] p_reg     Pointer to the structure of registers of the peripheral.
66  * @param[in] region_id ACL region index.
67  * @param[in] address   Start address.
68  * @param[in] size      Size of region to protect in bytes.
69  * @param[in] perm      Permissions to set for region to protect.
70  */
71 NRF_STATIC_INLINE void nrf_acl_region_set(NRF_ACL_Type * p_reg,
72                                           uint32_t       region_id,
73                                           uint32_t       address,
74                                           size_t         size,
75                                           nrf_acl_perm_t perm);
76 
77 /**
78  * @brief Function for getting the configured region address of a specific ACL region.
79  *
80  * @param[in] p_reg     Pointer to the structure of registers of the peripheral.
81  * @param[in] region_id ACL region index.
82  *
83  * @return Configured region address of given ACL region.
84  */
85 NRF_STATIC_INLINE uint32_t nrf_acl_region_address_get(NRF_ACL_Type const * p_reg,
86                                                       uint32_t             region_id);
87 
88 /**
89  * @brief Function for getting the configured region size of a specific ACL region.
90  *
91  * @param[in] p_reg     Pointer to the structure of registers of the peripheral.
92  * @param[in] region_id ACL region index.
93  *
94  * @return Configured region size of given ACL region.
95  */
96 NRF_STATIC_INLINE size_t nrf_acl_region_size_get(NRF_ACL_Type const * p_reg, uint32_t region_id);
97 
98 /**
99  * @brief Function for getting the configured region permissions of a specific ACL region.
100  *
101  * @param[in] p_reg     Pointer to the structure of registers of the peripheral.
102  * @param[in] region_id ACL region index.
103  *
104  * @return Configured region permissions of given ACL region.
105  */
106 NRF_STATIC_INLINE nrf_acl_perm_t nrf_acl_region_perm_get(NRF_ACL_Type const * p_reg,
107                                                          uint32_t             region_id);
108 
109 #ifndef NRF_DECLARE_ONLY
110 
nrf_acl_region_set(NRF_ACL_Type * p_reg,uint32_t region_id,uint32_t address,size_t size,nrf_acl_perm_t perm)111 NRF_STATIC_INLINE void nrf_acl_region_set(NRF_ACL_Type * p_reg,
112                                           uint32_t       region_id,
113                                           uint32_t       address,
114                                           size_t         size,
115                                           nrf_acl_perm_t perm)
116 {
117     NRFX_ASSERT(region_id < ACL_REGIONS_COUNT);
118     NRFX_ASSERT(address % nrf_ficr_codepagesize_get(NRF_FICR) == 0);
119     NRFX_ASSERT(size <= NRF_ACL_REGION_SIZE_MAX);
120     NRFX_ASSERT(size != 0);
121     NRFX_ASSERT(size % nrf_ficr_codepagesize_get(NRF_FICR) == 0);
122 
123     p_reg->ACL[region_id].ADDR = address;
124     p_reg->ACL[region_id].SIZE = size;
125     p_reg->ACL[region_id].PERM = perm;
126 }
127 
nrf_acl_region_address_get(NRF_ACL_Type const * p_reg,uint32_t region_id)128 NRF_STATIC_INLINE uint32_t nrf_acl_region_address_get(NRF_ACL_Type const * p_reg,
129                                                       uint32_t             region_id)
130 {
131     return (uint32_t)p_reg->ACL[region_id].ADDR;
132 }
133 
nrf_acl_region_size_get(NRF_ACL_Type const * p_reg,uint32_t region_id)134 NRF_STATIC_INLINE size_t nrf_acl_region_size_get(NRF_ACL_Type const * p_reg, uint32_t region_id)
135 {
136     return (size_t)p_reg->ACL[region_id].SIZE;
137 }
138 
nrf_acl_region_perm_get(NRF_ACL_Type const * p_reg,uint32_t region_id)139 NRF_STATIC_INLINE nrf_acl_perm_t nrf_acl_region_perm_get(NRF_ACL_Type const * p_reg,
140                                                          uint32_t             region_id)
141 {
142     return (nrf_acl_perm_t)p_reg->ACL[region_id].PERM;
143 }
144 
145 #endif // NRF_DECLARE_ONLY
146 
147 /** @} */
148 
149 #ifdef __cplusplus
150 }
151 #endif
152 
153 #endif // NRF_ACL_H__
154