fsl_rnga.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. /*
  2. * Copyright (c) 2015, Freescale Semiconductor, Inc.
  3. * Copyright 2016-2017 NXP
  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_rnga.h"
  31. #if defined(FSL_FEATURE_SOC_RNG_COUNT) && FSL_FEATURE_SOC_RNG_COUNT
  32. /*******************************************************************************
  33. * Definitions
  34. *******************************************************************************/
  35. /*******************************************************************************
  36. * RNG_CR - RNGA Control Register
  37. ******************************************************************************/
  38. /*!
  39. * @brief RNG_CR - RNGA Control Register (RW)
  40. *
  41. * Reset value: 0x00000000U
  42. *
  43. * Controls the operation of RNGA.
  44. */
  45. /*!
  46. * @name Constants and macros for entire RNG_CR register
  47. */
  48. /*@{*/
  49. #define RNG_CR_REG(base) ((base)->CR)
  50. #define RNG_RD_CR(base) (RNG_CR_REG(base))
  51. #define RNG_WR_CR(base, value) (RNG_CR_REG(base) = (value))
  52. #define RNG_RMW_CR(base, mask, value) (RNG_WR_CR(base, (RNG_RD_CR(base) & ~(mask)) | (value)))
  53. /*@}*/
  54. /*!
  55. * @name Register RNG_CR, field GO[0] (RW)
  56. *
  57. * Specifies whether random-data generation and loading (into OR[RANDOUT]) is
  58. * enabled.This field is sticky. You must reset RNGA to stop RNGA from loading
  59. * OR[RANDOUT] with data.
  60. *
  61. * Values:
  62. * - 0b0 - Disabled
  63. * - 0b1 - Enabled
  64. */
  65. /*@{*/
  66. /*! @brief Read current value of the RNG_CR_GO field. */
  67. #define RNG_RD_CR_GO(base) ((RNG_CR_REG(base) & RNG_CR_GO_MASK) >> RNG_CR_GO_SHIFT)
  68. /*! @brief Set the GO field to a new value. */
  69. #define RNG_WR_CR_GO(base, value) (RNG_RMW_CR(base, RNG_CR_GO_MASK, RNG_CR_GO(value)))
  70. /*@}*/
  71. /*!
  72. * @name Register RNG_CR, field SLP[4] (RW)
  73. *
  74. * Specifies whether RNGA is in Sleep or Normal mode. You can also enter Sleep
  75. * mode by asserting the DOZE signal.
  76. *
  77. * Values:
  78. * - 0b0 - Normal mode
  79. * - 0b1 - Sleep (low-power) mode
  80. */
  81. /*@{*/
  82. /*! @brief Read current value of the RNG_CR_SLP field. */
  83. #define RNG_RD_CR_SLP(base) ((RNG_CR_REG(base) & RNG_CR_SLP_MASK) >> RNG_CR_SLP_SHIFT)
  84. /*! @brief Set the SLP field to a new value. */
  85. #define RNG_WR_CR_SLP(base, value) (RNG_RMW_CR(base, RNG_CR_SLP_MASK, RNG_CR_SLP(value)))
  86. /*@}*/
  87. /*******************************************************************************
  88. * RNG_SR - RNGA Status Register
  89. ******************************************************************************/
  90. #define RNG_SR_REG(base) ((base)->SR)
  91. /*!
  92. * @name Register RNG_SR, field OREG_LVL[15:8] (RO)
  93. *
  94. * Indicates the number of random-data words that are in OR[RANDOUT], which
  95. * indicates whether OR[RANDOUT] is valid.If you read OR[RANDOUT] when SR[OREG_LVL]
  96. * is not 0, then the contents of a random number contained in OR[RANDOUT] are
  97. * returned, and RNGA writes 0 to both OR[RANDOUT] and SR[OREG_LVL].
  98. *
  99. * Values:
  100. * - 0b00000000 - No words (empty)
  101. * - 0b00000001 - One word (valid)
  102. */
  103. /*@{*/
  104. /*! @brief Read current value of the RNG_SR_OREG_LVL field. */
  105. #define RNG_RD_SR_OREG_LVL(base) ((RNG_SR_REG(base) & RNG_SR_OREG_LVL_MASK) >> RNG_SR_OREG_LVL_SHIFT)
  106. /*@}*/
  107. /*!
  108. * @name Register RNG_SR, field SLP[4] (RO)
  109. *
  110. * Specifies whether RNGA is in Sleep or Normal mode. You can also enter Sleep
  111. * mode by asserting the DOZE signal.
  112. *
  113. * Values:
  114. * - 0b0 - Normal mode
  115. * - 0b1 - Sleep (low-power) mode
  116. */
  117. /*@{*/
  118. /*! @brief Read current value of the RNG_SR_SLP field. */
  119. #define RNG_RD_SR_SLP(base) ((RNG_SR_REG(base) & RNG_SR_SLP_MASK) >> RNG_SR_SLP_SHIFT)
  120. /*@}*/
  121. /*******************************************************************************
  122. * RNG_OR - RNGA Output Register
  123. ******************************************************************************/
  124. /*!
  125. * @brief RNG_OR - RNGA Output Register (RO)
  126. *
  127. * Reset value: 0x00000000U
  128. *
  129. * Stores a random-data word generated by RNGA.
  130. */
  131. /*!
  132. * @name Constants and macros for entire RNG_OR register
  133. */
  134. /*@{*/
  135. #define RNG_OR_REG(base) ((base)->OR)
  136. #define RNG_RD_OR(base) (RNG_OR_REG(base))
  137. /*@}*/
  138. /*******************************************************************************
  139. * RNG_ER - RNGA Entropy Register
  140. ******************************************************************************/
  141. /*!
  142. * @brief RNG_ER - RNGA Entropy Register (WORZ)
  143. *
  144. * Reset value: 0x00000000U
  145. *
  146. * Specifies an entropy value that RNGA uses in addition to its ring oscillators
  147. * to seed its pseudorandom algorithm. This is a write-only register; reads
  148. * return all zeros.
  149. */
  150. /*!
  151. * @name Constants and macros for entire RNG_ER register
  152. */
  153. /*@{*/
  154. #define RNG_ER_REG(base) ((base)->ER)
  155. #define RNG_RD_ER(base) (RNG_ER_REG(base))
  156. #define RNG_WR_ER(base, value) (RNG_ER_REG(base) = (value))
  157. /*@}*/
  158. /*******************************************************************************
  159. * Prototypes
  160. *******************************************************************************/
  161. static uint32_t rnga_ReadEntropy(RNG_Type *base);
  162. /*******************************************************************************
  163. * Code
  164. ******************************************************************************/
  165. void RNGA_Init(RNG_Type *base)
  166. {
  167. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  168. /* Enable the clock gate. */
  169. CLOCK_EnableClock(kCLOCK_Rnga0);
  170. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  171. CLOCK_DisableClock(kCLOCK_Rnga0); /* To solve the release version on twrkm43z75m */
  172. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  173. CLOCK_EnableClock(kCLOCK_Rnga0);
  174. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  175. /* Reset the registers for RNGA module to reset state. */
  176. RNG_WR_CR(base, 0);
  177. /* Enables the RNGA random data generation and loading.*/
  178. RNG_WR_CR_GO(base, 1);
  179. }
  180. void RNGA_Deinit(RNG_Type *base)
  181. {
  182. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  183. /* Disable the clock for RNGA module.*/
  184. CLOCK_DisableClock(kCLOCK_Rnga0);
  185. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  186. }
  187. /*!
  188. * @brief Get a random data from RNGA.
  189. *
  190. * @param base RNGA base address
  191. */
  192. static uint32_t rnga_ReadEntropy(RNG_Type *base)
  193. {
  194. uint32_t data = 0;
  195. if (RNGA_GetMode(base) == kRNGA_ModeNormal) /* Is in normal mode.*/
  196. {
  197. /* Wait for valid random-data.*/
  198. while (RNG_RD_SR_OREG_LVL(base) == 0)
  199. {
  200. }
  201. data = RNG_RD_OR(base);
  202. }
  203. /* Get random-data word generated by RNGA.*/
  204. return data;
  205. }
  206. status_t RNGA_GetRandomData(RNG_Type *base, void *data, size_t data_size)
  207. {
  208. status_t result = kStatus_Success;
  209. uint32_t random_32;
  210. uint8_t *random_p;
  211. uint32_t random_size;
  212. uint8_t *data_p = (uint8_t *)data;
  213. uint32_t i;
  214. /* Check input parameters.*/
  215. if (base && data && data_size)
  216. {
  217. do
  218. {
  219. /* Read Entropy.*/
  220. random_32 = rnga_ReadEntropy(base);
  221. random_p = (uint8_t *)&random_32;
  222. if (data_size < sizeof(random_32))
  223. {
  224. random_size = data_size;
  225. }
  226. else
  227. {
  228. random_size = sizeof(random_32);
  229. }
  230. for (i = 0; i < random_size; i++)
  231. {
  232. *data_p++ = *random_p++;
  233. }
  234. data_size -= random_size;
  235. } while (data_size > 0);
  236. }
  237. else
  238. {
  239. result = kStatus_InvalidArgument;
  240. }
  241. return result;
  242. }
  243. void RNGA_SetMode(RNG_Type *base, rnga_mode_t mode)
  244. {
  245. RNG_WR_CR_SLP(base, (uint32_t)mode);
  246. }
  247. rnga_mode_t RNGA_GetMode(RNG_Type *base)
  248. {
  249. return (rnga_mode_t)RNG_RD_SR_SLP(base);
  250. }
  251. void RNGA_Seed(RNG_Type *base, uint32_t seed)
  252. {
  253. /* Write to RNGA Entropy Register.*/
  254. RNG_WR_ER(base, seed);
  255. }
  256. #endif /* FSL_FEATURE_SOC_RNG_COUNT */