fsl_rnga.c 9.2 KB

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