fsl_bee.c 6.8 KB

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