nrf_kmu.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. /**
  2. * Copyright (c) 2018 - 2019, Nordic Semiconductor ASA
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without modification,
  7. * are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form, except as embedded into a Nordic
  13. * Semiconductor ASA integrated circuit in a product or a software update for
  14. * such product, must reproduce the above copyright notice, this list of
  15. * conditions and the following disclaimer in the documentation and/or other
  16. * materials provided with the distribution.
  17. *
  18. * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * 4. This software, with or without modification, must only be used with a
  23. * Nordic Semiconductor ASA integrated circuit.
  24. *
  25. * 5. Any software provided in binary form under this license must not be reverse
  26. * engineered, decompiled, modified and/or disassembled.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
  29. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  30. * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
  31. * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
  32. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  33. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  34. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  36. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  37. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. */
  40. #ifndef NRF_KMU_H__
  41. #define NRF_KMU_H__
  42. #include <nrfx.h>
  43. #ifdef __cplusplus
  44. extern "C" {
  45. #endif
  46. /**
  47. * @defgroup nrf_kmu_hal KMU HAL
  48. * @{
  49. * @ingroup nrf_kmu
  50. * @brief Hardware access layer for managing the Key Management Unit (KMU) peripheral.
  51. */
  52. /** @brief KMU tasks. */
  53. typedef enum
  54. {
  55. NRF_KMU_TASK_PUSH_KEYSLOT = offsetof(NRF_KMU_Type, TASKS_PUSH_KEYSLOT), ///< Push a key slot over secure APB.
  56. } nrf_kmu_task_t;
  57. /** @brief KMU events. */
  58. typedef enum
  59. {
  60. NRF_KMU_EVENT_KEYSLOT_PUSHED = offsetof(NRF_KMU_Type, EVENTS_KEYSLOT_PUSHED), ///< Key successfully pushed over secure APB.
  61. NRF_KMU_EVENT_KEYSLOT_REVOKED = offsetof(NRF_KMU_Type, EVENTS_KEYSLOT_REVOKED), ///< Key has been revoked and cannot be tasked for selection.
  62. NRF_KMU_EVENT_KEYSLOT_ERROR = offsetof(NRF_KMU_Type, EVENTS_KEYSLOT_ERROR) ///< No key slot selected or no destination address defined or error during push mechanism.
  63. } nrf_kmu_event_t;
  64. /** @brief KMU interrupts. */
  65. typedef enum
  66. {
  67. NRF_KMU_INT_PUSHED_MASK = KMU_INTEN_KEYSLOT_PUSHED_Msk, ///< Interrupt on KEYSLOT_PUSHED event.
  68. NRF_KMU_INT_REVOKED_MASK = KMU_INTEN_KEYSLOT_REVOKED_Msk, ///< Interrupt on KEYSLOT_REVOKED event.
  69. NRF_KMU_INT_ERROR_MASK = KMU_INTEN_KEYSLOT_ERROR_Msk ///< Interrupt on KEYSLOT_ERROR event.
  70. } nrf_kmu_int_mask_t;
  71. /** @brief KMU operation status. */
  72. typedef enum
  73. {
  74. NRF_KMU_STATUS_BLOCKED_MASK = KMU_STATUS_BLOCKED_Msk, ///< Access violation detected and blocked.
  75. NRF_KMU_STATUS_SELECTED_MASK = KMU_STATUS_SELECTED_Msk, ///< Key slot ID successfully selected by KMU
  76. } nrf_kmu_status_t;
  77. /**
  78. * @brief Function for activating a specific KMU task.
  79. *
  80. * @param[in] p_reg Pointer to the peripheral registers structure.
  81. * @param[in] task Task to activate.
  82. */
  83. __STATIC_INLINE void nrf_kmu_task_trigger(NRF_KMU_Type * p_reg, nrf_kmu_task_t task);
  84. /**
  85. * @brief Function for getting the address of a specific KMU task register.
  86. *
  87. * @param[in] p_reg Pointer to the peripheral registers structure.
  88. * @param[in] task Requested task.
  89. *
  90. * @return Address of the specified task register.
  91. */
  92. __STATIC_INLINE uint32_t nrf_kmu_task_address_get(NRF_KMU_Type const * p_reg, nrf_kmu_task_t task);
  93. /**
  94. * @brief Function for clearing a specific KMU event.
  95. *
  96. * @param[in] p_reg Pointer to the peripheral registers structure.
  97. * @param[in] event Event to clear.
  98. */
  99. __STATIC_INLINE void nrf_kmu_event_clear(NRF_KMU_Type * p_reg, nrf_kmu_event_t event);
  100. /**
  101. * @brief Function for checking the state of a specific KMU event.
  102. *
  103. * @param[in] p_reg Pointer to the peripheral registers structure.
  104. * @param[in] event Event to check.
  105. *
  106. * @retval true If the event is set.
  107. * @retval false If the event is not set.
  108. */
  109. __STATIC_INLINE bool nrf_kmu_event_check(NRF_KMU_Type const * p_reg, nrf_kmu_event_t event);
  110. /**
  111. * @brief Function for getting the address of a specific KMU event register.
  112. *
  113. * @param[in] p_reg Pointer to the peripheral registers structure.
  114. * @param[in] event Requested event.
  115. *
  116. * @return Address of the specified event register.
  117. */
  118. __STATIC_INLINE uint32_t nrf_kmu_event_address_get(NRF_KMU_Type const * p_reg,
  119. nrf_kmu_event_t event);
  120. /**
  121. * @brief Function for enabling specified interrupts.
  122. *
  123. * @param[in] p_reg Pointer to the peripheral registers structure.
  124. * @param[in] mask Interrupts to enable.
  125. */
  126. __STATIC_INLINE void nrf_kmu_int_enable(NRF_KMU_Type * p_reg, uint32_t mask);
  127. /**
  128. * @brief Function for disabling specified interrupts.
  129. *
  130. * @param[in] p_reg Pointer to the peripheral registers structure.
  131. * @param[in] mask Interrupts to disable.
  132. */
  133. __STATIC_INLINE void nrf_kmu_int_disable(NRF_KMU_Type * p_reg, uint32_t mask);
  134. /**
  135. * @brief Function for retrieving the state of a given interrupt.
  136. *
  137. * @param[in] p_reg Pointer to the peripheral registers structure.
  138. * @param[in] kmu_int Interrupt to check.
  139. *
  140. * @retval true If the interrupt is enabled.
  141. * @retval false If the interrupt is not enabled.
  142. */
  143. __STATIC_INLINE bool nrf_kmu_int_enable_check(NRF_KMU_Type const * p_reg,
  144. nrf_kmu_int_mask_t kmu_int);
  145. /**
  146. * @brief Function for retrieving the state of interrupts.
  147. *
  148. * Function returns bitmask. Please use @ref nrf_kmu_int_mask_t to check interrupts status.
  149. *
  150. * @param[in] p_reg Pointer to the peripheral registers structure.
  151. *
  152. * @return Bitmask with pending interrupts bits.
  153. */
  154. __STATIC_INLINE uint32_t nrf_kmu_intpend_get(NRF_KMU_Type const * p_reg);
  155. /**
  156. * @brief Function for getting status bits of the KMU operation.
  157. *
  158. * Function returns bitmask. Please use @ref nrf_kmu_status_t to check operations status.
  159. *
  160. * @param[in] p_reg Pointer to the peripheral registers structure.
  161. *
  162. * @return Bitmask with operation status bits.
  163. */
  164. __STATIC_INLINE uint32_t nrf_kmu_status_get(NRF_KMU_Type const * p_reg);
  165. /**
  166. * @brief Function for selecting the key slot ID.
  167. *
  168. * @param[in] p_reg Pointer to the peripheral registers structure.
  169. * @param[in] keyslot_id Key slot ID to be read over AHB or pushed over
  170. * secure APB when TASKS_PUSH_KEYSLOT is started.
  171. */
  172. __STATIC_INLINE void nrf_kmu_keyslot_set(NRF_KMU_Type * p_reg, uint8_t keyslot_id);
  173. /**
  174. * @brief Function for getting the key slot ID.
  175. *
  176. * @param[in] p_reg Pointer to the peripheral registers structure.
  177. *
  178. * @return Key slot ID.
  179. */
  180. __STATIC_INLINE uint8_t nrf_kmu_keyslot_get(NRF_KMU_Type const * p_reg);
  181. #ifndef SUPPRESS_INLINE_IMPLEMENTATION
  182. __STATIC_INLINE void nrf_kmu_task_trigger(NRF_KMU_Type * p_reg, nrf_kmu_task_t task)
  183. {
  184. *((volatile uint32_t *)((uint8_t *)p_reg + (uint32_t)task)) = 0x1UL;
  185. }
  186. __STATIC_INLINE uint32_t nrf_kmu_task_address_get(NRF_KMU_Type const * p_reg, nrf_kmu_task_t task)
  187. {
  188. return ((uint32_t)p_reg + (uint32_t)task);
  189. }
  190. __STATIC_INLINE void nrf_kmu_event_clear(NRF_KMU_Type * p_reg, nrf_kmu_event_t event)
  191. {
  192. *((volatile uint32_t *)((uint8_t *)p_reg + (uint32_t)event)) = 0x0UL;
  193. volatile uint32_t dummy = *((volatile uint32_t *)((uint8_t *)p_reg + (uint32_t)event));
  194. (void)dummy;
  195. }
  196. __STATIC_INLINE bool nrf_kmu_event_check(NRF_KMU_Type const * p_reg, nrf_kmu_event_t event)
  197. {
  198. return (bool)*(volatile uint32_t *)((uint8_t *)p_reg + (uint32_t)event);
  199. }
  200. __STATIC_INLINE uint32_t nrf_kmu_event_address_get(NRF_KMU_Type const * p_reg,
  201. nrf_kmu_event_t event)
  202. {
  203. return ((uint32_t)p_reg + (uint32_t)event);
  204. }
  205. __STATIC_INLINE void nrf_kmu_int_enable(NRF_KMU_Type * p_reg, uint32_t mask)
  206. {
  207. p_reg->INTENSET = mask;
  208. }
  209. __STATIC_INLINE void nrf_kmu_int_disable(NRF_KMU_Type * p_reg, uint32_t mask)
  210. {
  211. p_reg->INTENCLR = mask;
  212. }
  213. __STATIC_INLINE bool nrf_kmu_int_enable_check(NRF_KMU_Type const * p_reg,
  214. nrf_kmu_int_mask_t kmu_int)
  215. {
  216. return (bool)(p_reg->INTENSET & kmu_int);
  217. }
  218. __STATIC_INLINE uint32_t nrf_kmu_intpend_get(NRF_KMU_Type const * p_reg)
  219. {
  220. return p_reg->INTPEND;
  221. }
  222. __STATIC_INLINE uint32_t nrf_kmu_status_get(NRF_KMU_Type const * p_reg)
  223. {
  224. return p_reg->STATUS;
  225. }
  226. __STATIC_INLINE void nrf_kmu_keyslot_set(NRF_KMU_Type * p_reg, uint8_t keyslot_id)
  227. {
  228. p_reg->SELECTKEYSLOT = (uint32_t) keyslot_id;
  229. }
  230. __STATIC_INLINE uint8_t nrf_kmu_keyslot_get(NRF_KMU_Type const * p_reg)
  231. {
  232. return (uint8_t) p_reg->SELECTKEYSLOT;
  233. }
  234. #endif // SUPPRESS_INLINE_IMPLEMENTATION
  235. /** @} */
  236. #ifdef __cplusplus
  237. }
  238. #endif
  239. #endif // NRF_KMU_H__