fsl_bee.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /*
  2. * The Clear BSD License
  3. * Copyright 2017 NXP
  4. * All rights reserved.
  5. *
  6. *
  7. * Redistribution and use in source and binary forms, with or without modification,
  8. * are permitted (subject to the limitations in the disclaimer below) provided
  9. * that the following conditions are met:
  10. *
  11. * o Redistributions of source code must retain the above copyright notice, this list
  12. * of conditions and the following disclaimer.
  13. *
  14. * o Redistributions in binary form must reproduce the above copyright notice, this
  15. * list of conditions and the following disclaimer in the documentation and/or
  16. * other materials provided with the distribution.
  17. *
  18. * o Neither the name of the copyright holder 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. * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY THIS LICENSE.
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  24. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  25. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  26. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
  27. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  28. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  29. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  30. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  31. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  32. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  33. */
  34. #include "fsl_bee.h"
  35. /*******************************************************************************
  36. * Definitions
  37. ******************************************************************************/
  38. /* Component ID definition, used by tools. */
  39. #ifndef FSL_COMPONENT_ID
  40. #define FSL_COMPONENT_ID "platform.drivers.bee"
  41. #endif
  42. /*******************************************************************************
  43. * Variables
  44. ******************************************************************************/
  45. /*******************************************************************************
  46. * Code
  47. ******************************************************************************/
  48. static void aligned_memcpy(void *dst, const void *src, size_t size)
  49. {
  50. register uint32_t *to32 = (uint32_t *)(uintptr_t)dst;
  51. register const uint32_t *from32 = (const uint32_t *)(uintptr_t)src;
  52. while (size >= sizeof(uint32_t))
  53. {
  54. *to32 = *from32;
  55. size -= sizeof(uint32_t);
  56. to32++;
  57. from32++;
  58. }
  59. }
  60. void BEE_Init(BEE_Type *base)
  61. {
  62. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  63. CLOCK_EnableClock(kCLOCK_Bee);
  64. #endif
  65. base->CTRL = BEE_CTRL_CTRL_SFTRST_N_MASK | BEE_CTRL_CTRL_CLK_EN_MASK;
  66. }
  67. void BEE_Deinit(BEE_Type *base)
  68. {
  69. base->CTRL &=
  70. ~(BEE_CTRL_BEE_ENABLE_MASK | BEE_CTRL_CTRL_SFTRST_N_MASK | BEE_CTRL_CTRL_CLK_EN_MASK | BEE_CTRL_KEY_VALID_MASK);
  71. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  72. CLOCK_DisableClock(kCLOCK_Bee);
  73. #endif
  74. }
  75. void BEE_GetDefaultConfig(bee_region_config_t *config)
  76. {
  77. assert(config);
  78. config->mode = kBEE_AesEcbMode;
  79. config->regionBot = 0U;
  80. config->regionTop = 0U;
  81. config->addrOffset = 0xF0000000U;
  82. config->regionEn = kBEE_RegionDisabled;
  83. }
  84. status_t BEE_SetRegionConfig(BEE_Type *base, bee_region_t region, const bee_region_config_t *config)
  85. {
  86. IOMUXC_GPR_Type *iomuxc = IOMUXC_GPR;
  87. bool reenable = false;
  88. /* Wait until BEE is in idle state */
  89. while (!(BEE_GetStatusFlags(base) & kBEE_IdleFlag))
  90. {
  91. }
  92. /* Disable BEE before region configuration in case it is enabled. */
  93. if ((base->CTRL >> BEE_CTRL_BEE_ENABLE_SHIFT) & 1)
  94. {
  95. BEE_Disable(base);
  96. reenable = true;
  97. }
  98. if (region == kBEE_Region0)
  99. {
  100. /* Region 0 config */
  101. iomuxc->GPR18 = config->regionBot;
  102. iomuxc->GPR19 = config->regionTop;
  103. base->CTRL |= BEE_CTRL_CTRL_AES_MODE_R0(config->mode);
  104. base->ADDR_OFFSET0 = BEE_ADDR_OFFSET0_ADDR_OFFSET0(config->addrOffset);
  105. }
  106. else if (region == kBEE_Region1)
  107. {
  108. /* Region 1 config */
  109. iomuxc->GPR20 = config->regionBot;
  110. iomuxc->GPR21 = config->regionTop;
  111. base->CTRL |= BEE_CTRL_CTRL_AES_MODE_R1(config->mode);
  112. base->ADDR_OFFSET1 = BEE_ADDR_OFFSET1_ADDR_OFFSET0(config->addrOffset);
  113. base->REGION1_BOT = BEE_REGION1_BOT_REGION1_BOT(config->regionBot);
  114. base->REGION1_TOP = BEE_REGION1_TOP_REGION1_TOP(config->regionTop);
  115. }
  116. else
  117. {
  118. return kStatus_InvalidArgument;
  119. }
  120. /* Enable/disable region if desired */
  121. if (config->regionEn == kBEE_RegionEnabled)
  122. {
  123. iomuxc->GPR11 |= IOMUXC_GPR_GPR11_BEE_DE_RX_EN(1 << region);
  124. }
  125. else
  126. {
  127. iomuxc->GPR11 &= ~IOMUXC_GPR_GPR11_BEE_DE_RX_EN(1 << region);
  128. }
  129. /* Reenable BEE if it was enabled before. */
  130. if (reenable)
  131. {
  132. BEE_Enable(base);
  133. }
  134. return kStatus_Success;
  135. }
  136. status_t BEE_SetRegionKey(
  137. BEE_Type *base, bee_region_t region, const uint8_t *key, size_t keySize, const uint8_t *nonce, size_t nonceSize)
  138. {
  139. bool reenable = false;
  140. /* Key must be 32-bit aligned */
  141. if (((uintptr_t)key & 0x3u) || (keySize != 16))
  142. {
  143. return kStatus_InvalidArgument;
  144. }
  145. /* Wait until BEE is in idle state */
  146. while (!(BEE_GetStatusFlags(base) & kBEE_IdleFlag))
  147. {
  148. }
  149. /* Disable BEE before region configuration in case it is enabled. */
  150. if ((base->CTRL >> BEE_CTRL_BEE_ENABLE_SHIFT) & 1)
  151. {
  152. BEE_Disable(base);
  153. reenable = true;
  154. }
  155. if (region == kBEE_Region0)
  156. {
  157. base->CTRL &= ~BEE_CTRL_KEY_REGION_SEL_MASK;
  158. if (nonce)
  159. {
  160. if (nonceSize != 16)
  161. {
  162. return kStatus_InvalidArgument;
  163. }
  164. memcpy((uint32_t *)&base->CTR_NONCE0_W0, nonce, nonceSize);
  165. }
  166. }
  167. else if (region == kBEE_Region1)
  168. {
  169. base->CTRL |= BEE_CTRL_KEY_REGION_SEL_MASK;
  170. if (nonce)
  171. {
  172. if (nonceSize != 16)
  173. {
  174. return kStatus_InvalidArgument;
  175. }
  176. memcpy((uint32_t *)&base->CTR_NONCE1_W0, nonce, nonceSize);
  177. }
  178. }
  179. else
  180. {
  181. return kStatus_InvalidArgument;
  182. }
  183. /* Try to load key. If BEE key selection fuse is programmed to use OTMP key on this device, this operation should
  184. * fail. */
  185. aligned_memcpy((uint32_t *)&base->AES_KEY0_W0, key, keySize);
  186. if (memcmp((uint32_t *)&base->AES_KEY0_W0, key, keySize) != 0)
  187. {
  188. return kStatus_Fail;
  189. }
  190. /* Reenable BEE if it was enabled before. */
  191. if (reenable)
  192. {
  193. BEE_Enable(base);
  194. }
  195. return kStatus_Success;
  196. }
  197. uint32_t BEE_GetStatusFlags(BEE_Type *base)
  198. {
  199. return base->STATUS;
  200. }
  201. void BEE_ClearStatusFlags(BEE_Type *base, uint32_t mask)
  202. {
  203. /* w1c */
  204. base->STATUS |= mask;
  205. }