fsl_bee.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /*
  2. * Copyright 2017, 2019 NXP
  3. * All rights reserved.
  4. *
  5. *
  6. * SPDX-License-Identifier: BSD-3-Clause
  7. */
  8. #ifndef _FSL_BEE_H_
  9. #define _FSL_BEE_H_
  10. #include "fsl_common.h"
  11. /*!
  12. * @addtogroup bee
  13. * @{
  14. */
  15. /*******************************************************************************
  16. * Definitions
  17. *******************************************************************************/
  18. /*! @name Driver version */
  19. /*@{*/
  20. /*! @brief BEE driver version. Version 2.0.2.
  21. *
  22. * Current version: 2.0.2
  23. *
  24. * Change log:
  25. *
  26. * - 2.0.2
  27. * - Bug Fixes
  28. * - Fixed MISRA issue.
  29. *
  30. * - 2.0.1
  31. * - Bug Fixes
  32. * - Fixed bug in key user key loading sequence. BEE must be enabled during loading of user key.
  33. * - Fixed typos in comments.
  34. * - New Features
  35. * - Added configuration setting for endian swap, access permission and region security level.
  36. * - Improvements
  37. * - Setting of AES nonce was moved from BEE_SetRegionKey() into separate BEE_SetRegionNonce() function.
  38. * - Changed handling of region settings. Both regions are configured simultaneously by BEE_SetConfig() function.
  39. * Configuration of FAC start and end address using IOMUXC_GPRs was moved to application.
  40. * - Default value for region address offset was changed to 0.
  41. *
  42. * - 2.0.0
  43. * - Initial version
  44. */
  45. #define FSL_BEE_DRIVER_VERSION (MAKE_VERSION(2, 0, 2))
  46. /*@}*/
  47. /*! @brief BEE aes mode. */
  48. typedef enum _bee_aes_mode
  49. {
  50. kBEE_AesEcbMode = 0U, /*!< AES ECB Mode */
  51. kBEE_AesCtrMode = 1U /*!< AES CTR Mode */
  52. } bee_aes_mode_t;
  53. /*! @brief BEE region. */
  54. typedef enum _bee_region
  55. {
  56. kBEE_Region0 = 0U, /*!< BEE region 0 */
  57. kBEE_Region1 = 1U /*!< BEE region 1 */
  58. } bee_region_t;
  59. /*! @brief BEE ac prot enable. */
  60. typedef enum _bee_ac_prot_enable
  61. {
  62. kBEE_AccessProtDisabled = 0U, /*!< BEE access permission control disabled */
  63. kBEE_AccessProtEnabled = 1U /*!< BEE access permission control enabled */
  64. } bee_ac_prot_enable;
  65. /*! @brief BEE endian swap enable. */
  66. typedef enum _bee_endian_swap_enable
  67. {
  68. kBEE_EndianSwapDisabled = 1U, /*!< BEE endian swap disabled */
  69. kBEE_EndianSwapEnabled = 0U /*!< BEE endian swap enabled */
  70. } bee_endian_swap_enable;
  71. /*! @brief BEE security level. */
  72. typedef enum _bee_security_level
  73. {
  74. kBEE_SecurityLevel0 = 0U, /*!< BEE security level 0 */
  75. kBEE_SecurityLevel1 = 1U, /*!< BEE security level 1 */
  76. kBEE_SecurityLevel2 = 2U, /*!< BEE security level 2 */
  77. kBEE_SecurityLevel3 = 3U /*!< BEE security level 3 */
  78. } bee_security_level;
  79. /*! @brief BEE status flags. */
  80. typedef enum _bee_status_flags
  81. {
  82. kBEE_DisableAbortFlag = 1U, /*!< Disable abort flag. */
  83. kBEE_Reg0ReadSecViolation = 2U, /*!< Region-0 read channel security violation */
  84. kBEE_ReadIllegalAccess = 4U, /*!< Read channel illegal access detected */
  85. kBEE_Reg1ReadSecViolation = 8U, /*!< Region-1 read channel security violation */
  86. kBEE_Reg0AccessViolation = 16U, /*!< Protected region-0 access violation */
  87. kBEE_Reg1AccessViolation = 32U, /*!< Protected region-1 access violation */
  88. kBEE_IdleFlag = BEE_STATUS_BEE_IDLE_MASK /*!< Idle flag */
  89. } bee_status_flags_t;
  90. /*! @brief BEE region configuration structure. */
  91. typedef struct _bee_region_config
  92. {
  93. bee_aes_mode_t region0Mode; /*!< AES mode used for encryption/decryption for region 0 */
  94. bee_aes_mode_t region1Mode; /*!< AES mode used for encryption/decryption for region 1 */
  95. uint32_t region0AddrOffset; /*!< Region 0 address offset */
  96. uint32_t region1AddrOffset; /*!< Region 1 address offset */
  97. bee_security_level region0SecLevel; /*!< Region 0 security level */
  98. bee_security_level region1SecLevel; /*!< Region 1 security level */
  99. uint32_t region1Bot; /*!< Region 1 bottom address */
  100. uint32_t region1Top; /*!< Region 1 top address */
  101. bee_ac_prot_enable accessPermission; /*!< Access permission control enable/disable */
  102. bee_endian_swap_enable endianSwapEn; /*!< Endian swap enable/disable */
  103. } bee_region_config_t;
  104. /*******************************************************************************
  105. * API
  106. ******************************************************************************/
  107. #if defined(__cplusplus)
  108. extern "C" {
  109. #endif
  110. /*!
  111. * @brief Resets BEE module to factory default values.
  112. *
  113. * This function performs hardware reset of BEE module. Attributes and keys from software for both regions are cleared.
  114. *
  115. * @param base BEE peripheral address.
  116. */
  117. void BEE_Init(BEE_Type *base);
  118. /*!
  119. * @brief Resets BEE module, clears keys for both regions and disables clock to the BEE.
  120. *
  121. * This function performs hardware reset of BEE module and disables clocks. Attributes and keys from software for both
  122. * regions are cleared.
  123. *
  124. * @param base BEE peripheral address.
  125. */
  126. void BEE_Deinit(BEE_Type *base);
  127. /*!
  128. * @brief Enables BEE decryption.
  129. *
  130. * This function enables decryption using BEE.
  131. *
  132. * @param base BEE peripheral address.
  133. */
  134. static inline void BEE_Enable(BEE_Type *base)
  135. {
  136. base->CTRL |= BEE_CTRL_BEE_ENABLE_MASK;
  137. }
  138. /*!
  139. * @brief Disables BEE decryption.
  140. *
  141. * This function disables decryption using BEE.
  142. *
  143. * @param base BEE peripheral address.
  144. */
  145. static inline void BEE_Disable(BEE_Type *base)
  146. {
  147. base->CTRL &= ~BEE_CTRL_BEE_ENABLE_MASK;
  148. }
  149. /*!
  150. * @brief Loads default values to the BEE region configuration structure.
  151. *
  152. * Loads default values to the BEE region configuration structure. The default values are as follows:
  153. * @code
  154. * config->region0Mode = kBEE_AesCtrMode;
  155. * config->region1Mode = kBEE_AesCtrMode;
  156. * config->region0AddrOffset = 0U;
  157. * config->region1AddrOffset = 0U;
  158. * config->region0SecLevel = kBEE_SecurityLevel3;
  159. * config->region1SecLevel = kBEE_SecurityLevel3;
  160. * config->region1Bot = 0U;
  161. * config->region1Top = 0U;
  162. * config->accessPermission = kBEE_AccessProtDisabled;
  163. * config->endianSwapEn = kBEE_EndianSwapEnabled;
  164. * @endcode
  165. *
  166. * @param config Configuration structure for BEE peripheral.
  167. */
  168. void BEE_GetDefaultConfig(bee_region_config_t *config);
  169. /*!
  170. * @brief Sets BEE configuration.
  171. *
  172. * This function sets BEE peripheral and BEE region settings accorging to given configuration structure.
  173. *
  174. * @param base BEE peripheral address.
  175. * @param config Configuration structure for BEE.
  176. */
  177. void BEE_SetConfig(BEE_Type *base, const bee_region_config_t *config);
  178. /*!
  179. * @brief Loads the AES key for selected region into BEE key registers.
  180. *
  181. * This function loads given AES key to BEE register for the given region.
  182. * The key must be 32-bit aligned and stored in little-endian format.
  183. *
  184. * Please note, that eFuse BEE_KEYx_SEL must be set accordingly to be able to load and use key loaded in BEE registers.
  185. * Otherwise, key cannot loaded and BEE will use key from OTPMK or SW_GP2.
  186. *
  187. * @param base BEE peripheral address.
  188. * @param region Selection of the BEE region to be configured.
  189. * @param key AES key (in little-endian format).
  190. * @param keySize Size of AES key.
  191. */
  192. status_t BEE_SetRegionKey(BEE_Type *base, bee_region_t region, const uint8_t *key, size_t keySize);
  193. /*!
  194. * @brief Loads the nonce for selected region into BEE nonce registers.
  195. *
  196. * This function loads given nonce(only AES CTR mode) to BEE register for the given region.
  197. * The nonce must be 32-bit aligned and stored in little-endian format.
  198. *
  199. * @param base BEE peripheral address.
  200. * @param region Selection of the BEE region to be configured.
  201. * @param nonce AES nonce (in little-endian format).
  202. * @param nonceSize Size of AES nonce.
  203. */
  204. status_t BEE_SetRegionNonce(BEE_Type *base, bee_region_t region, const uint8_t *nonce, size_t nonceSize);
  205. /*!
  206. * @brief Gets the BEE status flags.
  207. *
  208. * This function returns status of BEE peripheral.
  209. *
  210. * @param base BEE peripheral address.
  211. *
  212. * @return The status flags. This is the logical OR of members of the
  213. * enumeration ::bee_status_flags_t
  214. */
  215. uint32_t BEE_GetStatusFlags(BEE_Type *base);
  216. /*!
  217. * @brief Clears the BEE status flags.
  218. *
  219. * @param base BEE peripheral base address.
  220. * @param mask The status flags to clear. This is a logical OR of members of the
  221. * enumeration ::bee_status_flags_t
  222. */
  223. void BEE_ClearStatusFlags(BEE_Type *base, uint32_t mask);
  224. #if defined(__cplusplus)
  225. }
  226. #endif
  227. /*@}*/
  228. #endif /* _FSL_BEE_H_ */