1 /*
2  * Copyright (c) 2023 - 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 NRFX_RAM_CTRL_H
35 #define NRFX_RAM_CTRL_H
36 
37 #include <nrfx.h>
38 
39 #if defined(MEMCONF_PRESENT)
40 #include <hal/nrf_memconf.h>
41 #elif defined(NRF_VMC)
42 #include <hal/nrf_vmc.h>
43 #elif defined(POWER_PRESENT)
44 #include <hal/nrf_power.h>
45 #else
46 #error "Unsupported."
47 #endif
48 
49 #ifdef __cplusplus
50 extern "C" {
51 #endif
52 
53 /**
54  * @defgroup nrfx_ram_ctrl Generic RAM Control layer
55  * @{
56  * @ingroup nrfx
57  *
58  * @brief Helper layer that provides a uniform way of controlling the RAM power and retention settings.
59  */
60 
61 /**
62  * @brief Function for setting if the RAM sections containing specified object
63  *        are to be powered on or off.
64  *
65  * @param[in] p_object Pointer to the object.
66  * @param[in] length   Object size in bytes.
67  * @param[in] enable   True if RAM sections are to be powered on, false otherwise.
68  */
69 void nrfx_ram_ctrl_power_enable_set(void const * p_object, size_t length, bool enable);
70 
71 /**
72  * @brief Function for setting if the RAM sections containing specified object
73  *        are to be retained or not.
74  *
75  * @param[in] p_object Pointer to the object.
76  * @param[in] length   Object size in bytes.
77  * @param[in] enable   True if RAM sections are to be retained, false otherwise.
78  */
79 void nrfx_ram_ctrl_retention_enable_set(void const * p_object, size_t length, bool enable);
80 
81 /**
82  * @brief Function for setting if the specified mask of RAM sections contained within given RAM block
83  *        is to be powered on or off.
84  *
85  * @param[in] block_idx    RAM block index.
86  * @param[in] section_mask Mask of RAM sections.
87  * @param[in] enable       True if RAM sections are to be powered on, false otherwise.
88  */
nrfx_ram_ctrl_section_power_mask_enable_set(uint8_t block_idx,uint32_t section_mask,bool enable)89 __STATIC_INLINE void nrfx_ram_ctrl_section_power_mask_enable_set(uint8_t  block_idx,
90                                                                  uint32_t section_mask,
91                                                                  bool     enable)
92 {
93 #if defined(MEMCONF_PRESENT)
94     nrf_memconf_ramblock_control_mask_enable_set(NRF_MEMCONF, block_idx, section_mask, enable);
95 
96 #elif defined(NRF_VMC)
97     if (enable)
98     {
99         nrf_vmc_ram_block_power_set(NRF_VMC, block_idx, (nrf_vmc_power_t)section_mask);
100     }
101     else
102     {
103         nrf_vmc_ram_block_power_clear(NRF_VMC, block_idx, (nrf_vmc_power_t)section_mask);
104     }
105 
106 #elif defined(POWER_PRESENT)
107     section_mask <<= POWER_RAM_POWER_S0POWER_Pos;
108     if (enable)
109     {
110         nrf_power_rampower_mask_on(NRF_POWER, block_idx, section_mask);
111     }
112     else
113     {
114         nrf_power_rampower_mask_off(NRF_POWER, block_idx, section_mask);
115     }
116 
117 #endif
118 }
119 
120 /**
121  * @brief Function for setting if the specified mask of RAM sections contained within given RAM block
122  *        is to be retained or not.
123  *
124  * @param[in] block_idx    RAM block index.
125  * @param[in] section_mask Mask of RAM sections.
126  * @param[in] enable       True if RAM sections are to be retained, false otherwise.
127  */
nrfx_ram_ctrl_section_retention_mask_enable_set(uint8_t block_idx,uint32_t section_mask,bool enable)128 __STATIC_INLINE void nrfx_ram_ctrl_section_retention_mask_enable_set(uint8_t  block_idx,
129                                                                      uint32_t section_mask,
130                                                                      bool     enable)
131 {
132 #if defined(MEMCONF_PRESENT)
133     nrf_memconf_ramblock_ret_mask_enable_set(NRF_MEMCONF, block_idx, section_mask, enable);
134 
135 #elif defined(NRF_VMC)
136     if (enable)
137     {
138         nrf_vmc_ram_block_retention_set(NRF_VMC, block_idx, (nrf_vmc_retention_t)section_mask);
139     }
140     else
141     {
142         nrf_vmc_ram_block_retention_clear(NRF_VMC, block_idx, (nrf_vmc_retention_t)section_mask);
143     }
144 
145 #elif defined(POWER_PRESENT)
146     section_mask <<= POWER_RAM_POWER_S0RETENTION_Pos;
147     if (enable)
148     {
149         nrf_power_rampower_mask_on(NRF_POWER, block_idx, section_mask);
150     }
151     else
152     {
153         nrf_power_rampower_mask_off(NRF_POWER, block_idx, section_mask);
154     }
155 
156 #endif
157 }
158 
159 /** @} */
160 
161 #ifdef __cplusplus
162 }
163 #endif
164 
165 #endif // NRFX_RAM_CTRL_H
166