Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 

1936 рядки
79 KiB

  1. /*
  2. * Copyright (c) 2015, Freescale Semiconductor, Inc.
  3. * Copyright 2016-2017, 2020 NXP
  4. * All rights reserved.
  5. *
  6. * SPDX-License-Identifier: BSD-3-Clause
  7. */
  8. #include "fsl_trng.h"
  9. #if defined(FSL_FEATURE_SOC_TRNG_COUNT) && FSL_FEATURE_SOC_TRNG_COUNT
  10. /*******************************************************************************
  11. * Definitions
  12. *******************************************************************************/
  13. /* Component ID definition, used by tools. */
  14. #ifndef FSL_COMPONENT_ID
  15. #define FSL_COMPONENT_ID "platform.drivers.trng"
  16. #endif
  17. /* Default values for user configuration structure.*/
  18. #if (defined(KW40Z4_SERIES) || defined(KW41Z4_SERIES) || defined(KW31Z4_SERIES) || defined(KW21Z4_SERIES) || \
  19. defined(MCIMX7U5_M4_SERIES) || defined(KW36Z4_SERIES) || defined(KW37A4_SERIES) || defined(KW37Z4_SERIES) || \
  20. defined(KW38A4_SERIES) || defined(KW38Z4_SERIES) || defined(KW39A4_SERIES) || defined(KW35Z4_SERIES))
  21. #define TRNG_USER_CONFIG_DEFAULT_OSC_DIV kTRNG_RingOscDiv8
  22. #elif (defined(KV56F24_SERIES) || defined(KV58F24_SERIES) || defined(KL28Z7_SERIES) || defined(KL81Z7_SERIES) || \
  23. defined(KL82Z7_SERIES) || defined(K32L2A41A_SERIES))
  24. #define TRNG_USER_CONFIG_DEFAULT_OSC_DIV kTRNG_RingOscDiv4
  25. #elif defined(K81F25615_SERIES)
  26. #define TRNG_USER_CONFIG_DEFAULT_OSC_DIV kTRNG_RingOscDiv2
  27. #else
  28. /* Default value for the TRNG user configuration structure can be optionally
  29. defined by device specific preprocessor macros. */
  30. #if defined(FSL_FEATURE_TRNG_FORCE_USER_CONFIG_DEFAULT_OSC_DIV) && \
  31. (FSL_FEATURE_TRNG_FORCE_USER_CONFIG_DEFAULT_OSC_DIV > 0)
  32. #define TRNG_USER_CONFIG_DEFAULT_OSC_DIV (FSL_FEATURE_TRNG_USER_CONFIG_DEFAULT_OSC_DIV_VALUE)
  33. #else
  34. #define TRNG_USER_CONFIG_DEFAULT_OSC_DIV kTRNG_RingOscDiv0
  35. #endif
  36. #endif
  37. #define TRNG_USER_CONFIG_DEFAULT_LOCK 0
  38. #define TRNG_USER_CONFIG_DEFAULT_ENTROPY_DELAY 3200
  39. #define TRNG_USER_CONFIG_DEFAULT_SAMPLE_SIZE 2500
  40. #define TRNG_USER_CONFIG_DEFAULT_SPARSE_BIT_LIMIT 63
  41. #define TRNG_USER_CONFIG_DEFAULT_RETRY_COUNT 1
  42. #define TRNG_USER_CONFIG_DEFAULT_RUN_MAX_LIMIT 34
  43. #define TRNG_USER_CONFIG_DEFAULT_MONOBIT_MAXIMUM 1384
  44. #define TRNG_USER_CONFIG_DEFAULT_MONOBIT_MINIMUM (TRNG_USER_CONFIG_DEFAULT_MONOBIT_MAXIMUM - 268)
  45. #define TRNG_USER_CONFIG_DEFAULT_RUNBIT1_MAXIMUM 405
  46. #define TRNG_USER_CONFIG_DEFAULT_RUNBIT1_MINIMUM (TRNG_USER_CONFIG_DEFAULT_RUNBIT1_MAXIMUM - 178)
  47. #define TRNG_USER_CONFIG_DEFAULT_RUNBIT2_MAXIMUM 220
  48. #define TRNG_USER_CONFIG_DEFAULT_RUNBIT2_MINIMUM (TRNG_USER_CONFIG_DEFAULT_RUNBIT2_MAXIMUM - 122)
  49. #define TRNG_USER_CONFIG_DEFAULT_RUNBIT3_MAXIMUM 125
  50. #define TRNG_USER_CONFIG_DEFAULT_RUNBIT3_MINIMUM (TRNG_USER_CONFIG_DEFAULT_RUNBIT3_MAXIMUM - 88)
  51. #define TRNG_USER_CONFIG_DEFAULT_RUNBIT4_MAXIMUM 75
  52. #define TRNG_USER_CONFIG_DEFAULT_RUNBIT4_MINIMUM (TRNG_USER_CONFIG_DEFAULT_RUNBIT4_MAXIMUM - 64)
  53. #define TRNG_USER_CONFIG_DEFAULT_RUNBIT5_MAXIMUM 47
  54. #define TRNG_USER_CONFIG_DEFAULT_RUNBIT5_MINIMUM (TRNG_USER_CONFIG_DEFAULT_RUNBIT5_MAXIMUM - 46)
  55. #define TRNG_USER_CONFIG_DEFAULT_RUNBIT6PLUS_MAXIMUM 47
  56. #define TRNG_USER_CONFIG_DEFAULT_RUNBIT6PLUS_MINIMUM (TRNG_USER_CONFIG_DEFAULT_RUNBIT6PLUS_MAXIMUM - 46)
  57. #define TRNG_USER_CONFIG_DEFAULT_POKER_MAXIMUM 26912
  58. #define TRNG_USER_CONFIG_DEFAULT_POKER_MINIMUM (TRNG_USER_CONFIG_DEFAULT_POKER_MAXIMUM - 2467)
  59. #if defined(FSL_FEATURE_TRNG_FORCE_USER_CONFIG_DEFAULT_FREQUENCY_MAXIMUM) && \
  60. (FSL_FEATURE_TRNG_FORCE_USER_CONFIG_DEFAULT_FREQUENCY_MAXIMUM > 0)
  61. #define TRNG_USER_CONFIG_DEFAULT_FREQUENCY_MAXIMUM (FSL_FEATURE_TRNG_USER_CONFIG_DEFAULT_FREQUENCY_MAXIMUM_VALUE)
  62. #else
  63. #define TRNG_USER_CONFIG_DEFAULT_FREQUENCY_MAXIMUM 25600
  64. #endif
  65. #if defined(FSL_FEATURE_TRNG_FORCE_USER_CONFIG_DEFAULT_FREQUENCY_MINIMUM) && \
  66. (FSL_FEATURE_TRNG_FORCE_USER_CONFIG_DEFAULT_FREQUENCY_MINIMUM > 0)
  67. #define TRNG_USER_CONFIG_DEFAULT_FREQUENCY_MINIMUM (FSL_FEATURE_TRNG_USER_CONFIG_DEFAULT_FREQUENCY_MINIMUM_VALUE)
  68. #else
  69. #define TRNG_USER_CONFIG_DEFAULT_FREQUENCY_MINIMUM 1600
  70. #endif
  71. /*! @brief TRNG work mode */
  72. typedef enum _trng_work_mode
  73. {
  74. kTRNG_WorkModeRun = 0U, /*!< Run Mode. */
  75. kTRNG_WorkModeProgram = 1U /*!< Program Mode. */
  76. } trng_work_mode_t;
  77. /*! @brief TRNG statistical check type*/
  78. typedef enum _trng_statistical_check
  79. {
  80. kTRNG_StatisticalCheckMonobit =
  81. 1U, /*!< Statistical check of number of ones/zero detected during entropy generation. */
  82. kTRNG_StatisticalCheckRunBit1, /*!< Statistical check of number of runs of length 1 detected during entropy
  83. generation. */
  84. kTRNG_StatisticalCheckRunBit2, /*!< Statistical check of number of runs of length 2 detected during entropy
  85. generation. */
  86. kTRNG_StatisticalCheckRunBit3, /*!< Statistical check of number of runs of length 3 detected during entropy
  87. generation. */
  88. kTRNG_StatisticalCheckRunBit4, /*!< Statistical check of number of runs of length 4 detected during entropy
  89. generation. */
  90. kTRNG_StatisticalCheckRunBit5, /*!< Statistical check of number of runs of length 5 detected during entropy
  91. generation. */
  92. kTRNG_StatisticalCheckRunBit6Plus, /*!< Statistical check of number of runs of length 6 or more detected during
  93. entropy generation. */
  94. kTRNG_StatisticalCheckPoker, /*!< Statistical check of "Poker Test". */
  95. kTRNG_StatisticalCheckFrequencyCount /*!< Statistical check of entropy sample frequency count. */
  96. } trng_statistical_check_t;
  97. /*******************************************************************************
  98. * TRNG_SCMISC - RNG Statistical Check Miscellaneous Register
  99. ******************************************************************************/
  100. /*!
  101. * @name Register TRNG_SCMISC, field RTY_CT[19:16] (RW)
  102. *
  103. * RETRY COUNT. If a statistical check fails during the TRNG Entropy Generation,
  104. * the RTY_CT value indicates the number of times a retry should occur before
  105. * generating an error. This field is writable only if MCTL[PRGM] bit is 1. This
  106. * field will read zeroes if MCTL[PRGM] = 0. This field is cleared to 1h by writing
  107. * the MCTL[RST_DEF] bit to 1.
  108. */
  109. /*@{*/
  110. /*! @brief Read current value of the TRNG_SCMISC_RTY_CT field. */
  111. #define TRNG_RD_SCMISC_RTY_CT(base) ((TRNG_SCMISC_REG(base) & TRNG_SCMISC_RTY_CT_MASK) >> TRNG_SCMISC_RTY_CT_SHIFT)
  112. /*! @brief Set the RTY_CT field to a new value. */
  113. #define TRNG_WR_SCMISC_RTY_CT(base, value) (TRNG_RMW_SCMISC(base, TRNG_SCMISC_RTY_CT_MASK, TRNG_SCMISC_RTY_CT(value)))
  114. /*@}*/
  115. /*******************************************************************************
  116. * TRNG_SCML - RNG Statistical Check Monobit Limit Register
  117. ******************************************************************************/
  118. /*!
  119. * @brief TRNG_SCML - RNG Statistical Check Monobit Limit Register (RW)
  120. *
  121. * Reset value: 0x010C0568U
  122. *
  123. * The RNG Statistical Check Monobit Limit Register defines the allowable
  124. * maximum and minimum number of ones/zero detected during entropy generation. To pass
  125. * the test, the number of ones/zeroes generated must be less than the programmed
  126. * maximum value, and the number of ones/zeroes generated must be greater than
  127. * (maximum - range). If this test fails, the Retry Counter in SCMISC will be
  128. * decremented, and a retry will occur if the Retry Count has not reached zero. If
  129. * the Retry Count has reached zero, an error will be generated. Note that this
  130. * offset (0xBASE_0620) is used as SCML only if MCTL[PRGM] is 1. If MCTL[PRGM] is 0,
  131. * this offset is used as SCMC readback register.
  132. */
  133. /*!
  134. * @name Constants and macros for entire TRNG_SCML register
  135. */
  136. /*@{*/
  137. #define TRNG_SCML_REG(base) ((base)->SCML)
  138. #define TRNG_RD_SCML(base) (TRNG_SCML_REG(base))
  139. #define TRNG_WR_SCML(base, value) (TRNG_SCML_REG(base) = (value))
  140. #define TRNG_RMW_SCML(base, mask, value) (TRNG_WR_SCML(base, (TRNG_RD_SCML(base) & ~(mask)) | (value)))
  141. /*@}*/
  142. /*!
  143. * @name Register TRNG_SCML, field MONO_MAX[15:0] (RW)
  144. *
  145. * Monobit Maximum Limit. Defines the maximum allowable count taken during
  146. * entropy generation. The number of ones/zeroes detected during entropy generation
  147. * must be less than MONO_MAX, else a retry or error will occur. This register is
  148. * cleared to 00056Bh (decimal 1387) by writing the MCTL[RST_DEF] bit to 1.
  149. */
  150. /*@{*/
  151. /*! @brief Read current value of the TRNG_SCML_MONO_MAX field. */
  152. #define TRNG_RD_SCML_MONO_MAX(base) ((TRNG_SCML_REG(base) & TRNG_SCML_MONO_MAX_MASK) >> TRNG_SCML_MONO_MAX_SHIFT)
  153. /*! @brief Set the MONO_MAX field to a new value. */
  154. #define TRNG_WR_SCML_MONO_MAX(base, value) (TRNG_RMW_SCML(base, TRNG_SCML_MONO_MAX_MASK, TRNG_SCML_MONO_MAX(value)))
  155. /*@}*/
  156. /*!
  157. * @name Register TRNG_SCML, field MONO_RNG[31:16] (RW)
  158. *
  159. * Monobit Range. The number of ones/zeroes detected during entropy generation
  160. * must be greater than MONO_MAX - MONO_RNG, else a retry or error will occur.
  161. * This register is cleared to 000112h (decimal 274) by writing the MCTL[RST_DEF]
  162. * bit to 1.
  163. */
  164. /*@{*/
  165. /*! @brief Read current value of the TRNG_SCML_MONO_RNG field. */
  166. #define TRNG_RD_SCML_MONO_RNG(base) ((TRNG_SCML_REG(base) & TRNG_SCML_MONO_RNG_MASK) >> TRNG_SCML_MONO_RNG_SHIFT)
  167. /*! @brief Set the MONO_RNG field to a new value. */
  168. #define TRNG_WR_SCML_MONO_RNG(base, value) (TRNG_RMW_SCML(base, TRNG_SCML_MONO_RNG_MASK, TRNG_SCML_MONO_RNG(value)))
  169. /*@}*/
  170. /*******************************************************************************
  171. * TRNG_SCR1L - RNG Statistical Check Run Length 1 Limit Register
  172. ******************************************************************************/
  173. /*!
  174. * @brief TRNG_SCR1L - RNG Statistical Check Run Length 1 Limit Register (RW)
  175. *
  176. * Reset value: 0x00B20195U
  177. *
  178. * The RNG Statistical Check Run Length 1 Limit Register defines the allowable
  179. * maximum and minimum number of runs of length 1 detected during entropy
  180. * generation. To pass the test, the number of runs of length 1 (for samples of both 0
  181. * and 1) must be less than the programmed maximum value, and the number of runs of
  182. * length 1 must be greater than (maximum - range). If this test fails, the
  183. * Retry Counter in SCMISC will be decremented, and a retry will occur if the Retry
  184. * Count has not reached zero. If the Retry Count has reached zero, an error will
  185. * be generated. Note that this address (0xBASE_0624) is used as SCR1L only if
  186. * MCTL[PRGM] is 1. If MCTL[PRGM] is 0, this address is used as SCR1C readback
  187. * register.
  188. */
  189. /*!
  190. * @name Constants and macros for entire TRNG_SCR1L register
  191. */
  192. /*@{*/
  193. #define TRNG_SCR1L_REG(base) ((base)->SCR1L)
  194. #define TRNG_RD_SCR1L(base) (TRNG_SCR1L_REG(base))
  195. #define TRNG_WR_SCR1L(base, value) (TRNG_SCR1L_REG(base) = (value))
  196. #define TRNG_RMW_SCR1L(base, mask, value) (TRNG_WR_SCR1L(base, (TRNG_RD_SCR1L(base) & ~(mask)) | (value)))
  197. /*@}*/
  198. /*!
  199. * @name Register TRNG_SCR1L, field RUN1_MAX[14:0] (RW)
  200. *
  201. * Run Length 1 Maximum Limit. Defines the maximum allowable runs of length 1
  202. * (for both 0 and 1) detected during entropy generation. The number of runs of
  203. * length 1 detected during entropy generation must be less than RUN1_MAX, else a
  204. * retry or error will occur. This register is cleared to 01E5h (decimal 485) by
  205. * writing the MCTL[RST_DEF] bit to 1.
  206. */
  207. /*@{*/
  208. /*! @brief Read current value of the TRNG_SCR1L_RUN1_MAX field. */
  209. #define TRNG_RD_SCR1L_RUN1_MAX(base) ((TRNG_SCR1L_REG(base) & TRNG_SCR1L_RUN1_MAX_MASK) >> TRNG_SCR1L_RUN1_MAX_SHIFT)
  210. /*! @brief Set the RUN1_MAX field to a new value. */
  211. #define TRNG_WR_SCR1L_RUN1_MAX(base, value) (TRNG_RMW_SCR1L(base, TRNG_SCR1L_RUN1_MAX_MASK, TRNG_SCR1L_RUN1_MAX(value)))
  212. /*@}*/
  213. /*!
  214. * @name Register TRNG_SCR1L, field RUN1_RNG[30:16] (RW)
  215. *
  216. * Run Length 1 Range. The number of runs of length 1 (for both 0 and 1)
  217. * detected during entropy generation must be greater than RUN1_MAX - RUN1_RNG, else a
  218. * retry or error will occur. This register is cleared to 0102h (decimal 258) by
  219. * writing the MCTL[RST_DEF] bit to 1.
  220. */
  221. /*@{*/
  222. /*! @brief Read current value of the TRNG_SCR1L_RUN1_RNG field. */
  223. #define TRNG_RD_SCR1L_RUN1_RNG(base) ((TRNG_SCR1L_REG(base) & TRNG_SCR1L_RUN1_RNG_MASK) >> TRNG_SCR1L_RUN1_RNG_SHIFT)
  224. /*! @brief Set the RUN1_RNG field to a new value. */
  225. #define TRNG_WR_SCR1L_RUN1_RNG(base, value) (TRNG_RMW_SCR1L(base, TRNG_SCR1L_RUN1_RNG_MASK, TRNG_SCR1L_RUN1_RNG(value)))
  226. /*@}*/
  227. /*******************************************************************************
  228. * TRNG_SCR2L - RNG Statistical Check Run Length 2 Limit Register
  229. ******************************************************************************/
  230. /*!
  231. * @brief TRNG_SCR2L - RNG Statistical Check Run Length 2 Limit Register (RW)
  232. *
  233. * Reset value: 0x007A00DCU
  234. *
  235. * The RNG Statistical Check Run Length 2 Limit Register defines the allowable
  236. * maximum and minimum number of runs of length 2 detected during entropy
  237. * generation. To pass the test, the number of runs of length 2 (for samples of both 0
  238. * and 1) must be less than the programmed maximum value, and the number of runs of
  239. * length 2 must be greater than (maximum - range). If this test fails, the
  240. * Retry Counter in SCMISC will be decremented, and a retry will occur if the Retry
  241. * Count has not reached zero. If the Retry Count has reached zero, an error will
  242. * be generated. Note that this address (0xBASE_0628) is used as SCR2L only if
  243. * MCTL[PRGM] is 1. If MCTL[PRGM] is 0, this address is used as SCR2C readback
  244. * register.
  245. */
  246. /*!
  247. * @name Constants and macros for entire TRNG_SCR2L register
  248. */
  249. /*@{*/
  250. #define TRNG_SCR2L_REG(base) ((base)->SCR2L)
  251. #define TRNG_RD_SCR2L(base) (TRNG_SCR2L_REG(base))
  252. #define TRNG_WR_SCR2L(base, value) (TRNG_SCR2L_REG(base) = (value))
  253. #define TRNG_RMW_SCR2L(base, mask, value) (TRNG_WR_SCR2L(base, (TRNG_RD_SCR2L(base) & ~(mask)) | (value)))
  254. /*@}*/
  255. /*
  256. * Constants & macros for individual TRNG_SCR2L bitfields
  257. */
  258. /*!
  259. * @name Register TRNG_SCR2L, field RUN2_MAX[13:0] (RW)
  260. *
  261. * Run Length 2 Maximum Limit. Defines the maximum allowable runs of length 2
  262. * (for both 0 and 1) detected during entropy generation. The number of runs of
  263. * length 2 detected during entropy generation must be less than RUN2_MAX, else a
  264. * retry or error will occur. This register is cleared to 00DCh (decimal 220) by
  265. * writing the MCTL[RST_DEF] bit to 1.
  266. */
  267. /*@{*/
  268. /*! @brief Read current value of the TRNG_SCR2L_RUN2_MAX field. */
  269. #define TRNG_RD_SCR2L_RUN2_MAX(base) ((TRNG_SCR2L_REG(base) & TRNG_SCR2L_RUN2_MAX_MASK) >> TRNG_SCR2L_RUN2_MAX_SHIFT)
  270. /*! @brief Set the RUN2_MAX field to a new value. */
  271. #define TRNG_WR_SCR2L_RUN2_MAX(base, value) (TRNG_RMW_SCR2L(base, TRNG_SCR2L_RUN2_MAX_MASK, TRNG_SCR2L_RUN2_MAX(value)))
  272. /*@}*/
  273. /*!
  274. * @name Register TRNG_SCR2L, field RUN2_RNG[29:16] (RW)
  275. *
  276. * Run Length 2 Range. The number of runs of length 2 (for both 0 and 1)
  277. * detected during entropy generation must be greater than RUN2_MAX - RUN2_RNG, else a
  278. * retry or error will occur. This register is cleared to 007Ah (decimal 122) by
  279. * writing the MCTL[RST_DEF] bit to 1.
  280. */
  281. /*@{*/
  282. /*! @brief Read current value of the TRNG_SCR2L_RUN2_RNG field. */
  283. #define TRNG_RD_SCR2L_RUN2_RNG(base) ((TRNG_SCR2L_REG(base) & TRNG_SCR2L_RUN2_RNG_MASK) >> TRNG_SCR2L_RUN2_RNG_SHIFT)
  284. /*! @brief Set the RUN2_RNG field to a new value. */
  285. #define TRNG_WR_SCR2L_RUN2_RNG(base, value) (TRNG_RMW_SCR2L(base, TRNG_SCR2L_RUN2_RNG_MASK, TRNG_SCR2L_RUN2_RNG(value)))
  286. /*@}*/
  287. /*******************************************************************************
  288. * TRNG_SCR3L - RNG Statistical Check Run Length 3 Limit Register
  289. ******************************************************************************/
  290. /*!
  291. * @brief TRNG_SCR3L - RNG Statistical Check Run Length 3 Limit Register (RW)
  292. *
  293. * Reset value: 0x0058007DU
  294. *
  295. * The RNG Statistical Check Run Length 3 Limit Register defines the allowable
  296. * maximum and minimum number of runs of length 3 detected during entropy
  297. * generation. To pass the test, the number of runs of length 3 (for samples of both 0
  298. * and 1) must be less than the programmed maximum value, and the number of runs of
  299. * length 3 must be greater than (maximum - range). If this test fails, the
  300. * Retry Counter in SCMISC will be decremented, and a retry will occur if the Retry
  301. * Count has not reached zero. If the Retry Count has reached zero, an error will
  302. * be generated. Note that this address (0xBASE_062C) is used as SCR3L only if
  303. * MCTL[PRGM] is 1. If MCTL[PRGM] is 0, this address is used as SCR3C readback
  304. * register.
  305. */
  306. /*!
  307. * @name Constants and macros for entire TRNG_SCR3L register
  308. */
  309. /*@{*/
  310. #define TRNG_SCR3L_REG(base) ((base)->SCR3L)
  311. #define TRNG_RD_SCR3L(base) (TRNG_SCR3L_REG(base))
  312. #define TRNG_WR_SCR3L(base, value) (TRNG_SCR3L_REG(base) = (value))
  313. #define TRNG_RMW_SCR3L(base, mask, value) (TRNG_WR_SCR3L(base, (TRNG_RD_SCR3L(base) & ~(mask)) | (value)))
  314. /*@}*/
  315. /*
  316. * Constants & macros for individual TRNG_SCR3L bitfields
  317. */
  318. /*!
  319. * @name Register TRNG_SCR3L, field RUN3_MAX[12:0] (RW)
  320. *
  321. * Run Length 3 Maximum Limit. Defines the maximum allowable runs of length 3
  322. * (for both 0 and 1) detected during entropy generation. The number of runs of
  323. * length 3 detected during entropy generation must be less than RUN3_MAX, else a
  324. * retry or error will occur. This register is cleared to 007Dh (decimal 125) by
  325. * writing the MCTL[RST_DEF] bit to 1.
  326. */
  327. /*@{*/
  328. /*! @brief Read current value of the TRNG_SCR3L_RUN3_MAX field. */
  329. #define TRNG_RD_SCR3L_RUN3_MAX(base) ((TRNG_SCR3L_REG(base) & TRNG_SCR3L_RUN3_MAX_MASK) >> TRNG_SCR3L_RUN3_MAX_SHIFT)
  330. /*! @brief Set the RUN3_MAX field to a new value. */
  331. #define TRNG_WR_SCR3L_RUN3_MAX(base, value) (TRNG_RMW_SCR3L(base, TRNG_SCR3L_RUN3_MAX_MASK, TRNG_SCR3L_RUN3_MAX(value)))
  332. /*@}*/
  333. /*!
  334. * @name Register TRNG_SCR3L, field RUN3_RNG[28:16] (RW)
  335. *
  336. * Run Length 3 Range. The number of runs of length 3 (for both 0 and 1)
  337. * detected during entropy generation must be greater than RUN3_MAX - RUN3_RNG, else a
  338. * retry or error will occur. This register is cleared to 0058h (decimal 88) by
  339. * writing the MCTL[RST_DEF] bit to 1.
  340. */
  341. /*@{*/
  342. /*! @brief Read current value of the TRNG_SCR3L_RUN3_RNG field. */
  343. #define TRNG_RD_SCR3L_RUN3_RNG(base) ((TRNG_SCR3L_REG(base) & TRNG_SCR3L_RUN3_RNG_MASK) >> TRNG_SCR3L_RUN3_RNG_SHIFT)
  344. /*! @brief Set the RUN3_RNG field to a new value. */
  345. #define TRNG_WR_SCR3L_RUN3_RNG(base, value) (TRNG_RMW_SCR3L(base, TRNG_SCR3L_RUN3_RNG_MASK, TRNG_SCR3L_RUN3_RNG(value)))
  346. /*@}*/
  347. /*******************************************************************************
  348. * TRNG_SCR4L - RNG Statistical Check Run Length 4 Limit Register
  349. ******************************************************************************/
  350. /*!
  351. * @brief TRNG_SCR4L - RNG Statistical Check Run Length 4 Limit Register (RW)
  352. *
  353. * Reset value: 0x0040004BU
  354. *
  355. * The RNG Statistical Check Run Length 4 Limit Register defines the allowable
  356. * maximum and minimum number of runs of length 4 detected during entropy
  357. * generation. To pass the test, the number of runs of length 4 (for samples of both 0
  358. * and 1) must be less than the programmed maximum value, and the number of runs of
  359. * length 4 must be greater than (maximum - range). If this test fails, the
  360. * Retry Counter in SCMISC will be decremented, and a retry will occur if the Retry
  361. * Count has not reached zero. If the Retry Count has reached zero, an error will
  362. * be generated. Note that this address (0xBASE_0630) is used as SCR4L only if
  363. * MCTL[PRGM] is 1. If MCTL[PRGM] is 0, this address is used as SCR4C readback
  364. * register.
  365. */
  366. /*!
  367. * @name Constants and macros for entire TRNG_SCR4L register
  368. */
  369. /*@{*/
  370. #define TRNG_SCR4L_REG(base) ((base)->SCR4L)
  371. #define TRNG_RD_SCR4L(base) (TRNG_SCR4L_REG(base))
  372. #define TRNG_WR_SCR4L(base, value) (TRNG_SCR4L_REG(base) = (value))
  373. #define TRNG_RMW_SCR4L(base, mask, value) (TRNG_WR_SCR4L(base, (TRNG_RD_SCR4L(base) & ~(mask)) | (value)))
  374. /*@}*/
  375. /*
  376. * Constants & macros for individual TRNG_SCR4L bitfields
  377. */
  378. /*!
  379. * @name Register TRNG_SCR4L, field RUN4_MAX[11:0] (RW)
  380. *
  381. * Run Length 4 Maximum Limit. Defines the maximum allowable runs of length 4
  382. * (for both 0 and 1) detected during entropy generation. The number of runs of
  383. * length 4 detected during entropy generation must be less than RUN4_MAX, else a
  384. * retry or error will occur. This register is cleared to 004Bh (decimal 75) by
  385. * writing the MCTL[RST_DEF] bit to 1.
  386. */
  387. /*@{*/
  388. /*! @brief Read current value of the TRNG_SCR4L_RUN4_MAX field. */
  389. #define TRNG_RD_SCR4L_RUN4_MAX(base) ((TRNG_SCR4L_REG(base) & TRNG_SCR4L_RUN4_MAX_MASK) >> TRNG_SCR4L_RUN4_MAX_SHIFT)
  390. /*! @brief Set the RUN4_MAX field to a new value. */
  391. #define TRNG_WR_SCR4L_RUN4_MAX(base, value) (TRNG_RMW_SCR4L(base, TRNG_SCR4L_RUN4_MAX_MASK, TRNG_SCR4L_RUN4_MAX(value)))
  392. /*@}*/
  393. /*!
  394. * @name Register TRNG_SCR4L, field RUN4_RNG[27:16] (RW)
  395. *
  396. * Run Length 4 Range. The number of runs of length 4 (for both 0 and 1)
  397. * detected during entropy generation must be greater than RUN4_MAX - RUN4_RNG, else a
  398. * retry or error will occur. This register is cleared to 0040h (decimal 64) by
  399. * writing the MCTL[RST_DEF] bit to 1.
  400. */
  401. /*@{*/
  402. /*! @brief Read current value of the TRNG_SCR4L_RUN4_RNG field. */
  403. #define TRNG_RD_SCR4L_RUN4_RNG(base) ((TRNG_SCR4L_REG(base) & TRNG_SCR4L_RUN4_RNG_MASK) >> TRNG_SCR4L_RUN4_RNG_SHIFT)
  404. /*! @brief Set the RUN4_RNG field to a new value. */
  405. #define TRNG_WR_SCR4L_RUN4_RNG(base, value) (TRNG_RMW_SCR4L(base, TRNG_SCR4L_RUN4_RNG_MASK, TRNG_SCR4L_RUN4_RNG(value)))
  406. /*@}*/
  407. /*******************************************************************************
  408. * TRNG_SCR5L - RNG Statistical Check Run Length 5 Limit Register
  409. ******************************************************************************/
  410. /*!
  411. * @brief TRNG_SCR5L - RNG Statistical Check Run Length 5 Limit Register (RW)
  412. *
  413. * Reset value: 0x002E002FU
  414. *
  415. * The RNG Statistical Check Run Length 5 Limit Register defines the allowable
  416. * maximum and minimum number of runs of length 5 detected during entropy
  417. * generation. To pass the test, the number of runs of length 5 (for samples of both 0
  418. * and 1) must be less than the programmed maximum value, and the number of runs of
  419. * length 5 must be greater than (maximum - range). If this test fails, the
  420. * Retry Counter in SCMISC will be decremented, and a retry will occur if the Retry
  421. * Count has not reached zero. If the Retry Count has reached zero, an error will
  422. * be generated. Note that this address (0xBASE_0634) is used as SCR5L only if
  423. * MCTL[PRGM] is 1. If MCTL[PRGM] is 0, this address is used as SCR5C readback
  424. * register.
  425. */
  426. /*!
  427. * @name Constants and macros for entire TRNG_SCR5L register
  428. */
  429. /*@{*/
  430. #define TRNG_SCR5L_REG(base) ((base)->SCR5L)
  431. #define TRNG_RD_SCR5L(base) (TRNG_SCR5L_REG(base))
  432. #define TRNG_WR_SCR5L(base, value) (TRNG_SCR5L_REG(base) = (value))
  433. #define TRNG_RMW_SCR5L(base, mask, value) (TRNG_WR_SCR5L(base, (TRNG_RD_SCR5L(base) & ~(mask)) | (value)))
  434. /*@}*/
  435. /*
  436. * Constants & macros for individual TRNG_SCR5L bitfields
  437. */
  438. /*!
  439. * @name Register TRNG_SCR5L, field RUN5_MAX[10:0] (RW)
  440. *
  441. * Run Length 5 Maximum Limit. Defines the maximum allowable runs of length 5
  442. * (for both 0 and 1) detected during entropy generation. The number of runs of
  443. * length 5 detected during entropy generation must be less than RUN5_MAX, else a
  444. * retry or error will occur. This register is cleared to 002Fh (decimal 47) by
  445. * writing the MCTL[RST_DEF] bit to 1.
  446. */
  447. /*@{*/
  448. /*! @brief Read current value of the TRNG_SCR5L_RUN5_MAX field. */
  449. #define TRNG_RD_SCR5L_RUN5_MAX(base) ((TRNG_SCR5L_REG(base) & TRNG_SCR5L_RUN5_MAX_MASK) >> TRNG_SCR5L_RUN5_MAX_SHIFT)
  450. /*! @brief Set the RUN5_MAX field to a new value. */
  451. #define TRNG_WR_SCR5L_RUN5_MAX(base, value) (TRNG_RMW_SCR5L(base, TRNG_SCR5L_RUN5_MAX_MASK, TRNG_SCR5L_RUN5_MAX(value)))
  452. /*@}*/
  453. /*!
  454. * @name Register TRNG_SCR5L, field RUN5_RNG[26:16] (RW)
  455. *
  456. * Run Length 5 Range. The number of runs of length 5 (for both 0 and 1)
  457. * detected during entropy generation must be greater than RUN5_MAX - RUN5_RNG, else a
  458. * retry or error will occur. This register is cleared to 002Eh (decimal 46) by
  459. * writing the MCTL[RST_DEF] bit to 1.
  460. */
  461. /*@{*/
  462. /*! @brief Read current value of the TRNG_SCR5L_RUN5_RNG field. */
  463. #define TRNG_RD_SCR5L_RUN5_RNG(base) ((TRNG_SCR5L_REG(base) & TRNG_SCR5L_RUN5_RNG_MASK) >> TRNG_SCR5L_RUN5_RNG_SHIFT)
  464. /*! @brief Set the RUN5_RNG field to a new value. */
  465. #define TRNG_WR_SCR5L_RUN5_RNG(base, value) (TRNG_RMW_SCR5L(base, TRNG_SCR5L_RUN5_RNG_MASK, TRNG_SCR5L_RUN5_RNG(value)))
  466. /*@}*/
  467. /*******************************************************************************
  468. * TRNG_SCR6PL - RNG Statistical Check Run Length 6+ Limit Register
  469. ******************************************************************************/
  470. /*!
  471. * @brief TRNG_SCR6PL - RNG Statistical Check Run Length 6+ Limit Register (RW)
  472. *
  473. * Reset value: 0x002E002FU
  474. *
  475. * The RNG Statistical Check Run Length 6+ Limit Register defines the allowable
  476. * maximum and minimum number of runs of length 6 or more detected during entropy
  477. * generation. To pass the test, the number of runs of length 6 or more (for
  478. * samples of both 0 and 1) must be less than the programmed maximum value, and the
  479. * number of runs of length 6 or more must be greater than (maximum - range). If
  480. * this test fails, the Retry Counter in SCMISC will be decremented, and a retry
  481. * will occur if the Retry Count has not reached zero. If the Retry Count has
  482. * reached zero, an error will be generated. Note that this offset (0xBASE_0638) is
  483. * used as SCR6PL only if MCTL[PRGM] is 1. If MCTL[PRGM] is 0, this offset is
  484. * used as SCR6PC readback register.
  485. */
  486. /*!
  487. * @name Constants and macros for entire TRNG_SCR6PL register
  488. */
  489. /*@{*/
  490. #define TRNG_SCR6PL_REG(base) ((base)->SCR6PL)
  491. #define TRNG_RD_SCR6PL(base) (TRNG_SCR6PL_REG(base))
  492. #define TRNG_WR_SCR6PL(base, value) (TRNG_SCR6PL_REG(base) = (value))
  493. #define TRNG_RMW_SCR6PL(base, mask, value) (TRNG_WR_SCR6PL(base, (TRNG_RD_SCR6PL(base) & ~(mask)) | (value)))
  494. /*@}*/
  495. /*
  496. * Constants & macros for individual TRNG_SCR6PL bitfields
  497. */
  498. /*!
  499. * @name Register TRNG_SCR6PL, field RUN6P_MAX[10:0] (RW)
  500. *
  501. * Run Length 6+ Maximum Limit. Defines the maximum allowable runs of length 6
  502. * or more (for both 0 and 1) detected during entropy generation. The number of
  503. * runs of length 6 or more detected during entropy generation must be less than
  504. * RUN6P_MAX, else a retry or error will occur. This register is cleared to 002Fh
  505. * (decimal 47) by writing the MCTL[RST_DEF] bit to 1.
  506. */
  507. /*@{*/
  508. /*! @brief Read current value of the TRNG_SCR6PL_RUN6P_MAX field. */
  509. #define TRNG_RD_SCR6PL_RUN6P_MAX(base) \
  510. ((TRNG_SCR6PL_REG(base) & TRNG_SCR6PL_RUN6P_MAX_MASK) >> TRNG_SCR6PL_RUN6P_MAX_SHIFT)
  511. /*! @brief Set the RUN6P_MAX field to a new value. */
  512. #define TRNG_WR_SCR6PL_RUN6P_MAX(base, value) \
  513. (TRNG_RMW_SCR6PL(base, TRNG_SCR6PL_RUN6P_MAX_MASK, TRNG_SCR6PL_RUN6P_MAX(value)))
  514. /*@}*/
  515. /*!
  516. * @name Register TRNG_SCR6PL, field RUN6P_RNG[26:16] (RW)
  517. *
  518. * Run Length 6+ Range. The number of runs of length 6 or more (for both 0 and
  519. * 1) detected during entropy generation must be greater than RUN6P_MAX -
  520. * RUN6P_RNG, else a retry or error will occur. This register is cleared to 002Eh
  521. * (decimal 46) by writing the MCTL[RST_DEF] bit to 1.
  522. */
  523. /*@{*/
  524. /*! @brief Read current value of the TRNG_SCR6PL_RUN6P_RNG field. */
  525. #define TRNG_RD_SCR6PL_RUN6P_RNG(base) \
  526. ((TRNG_SCR6PL_REG(base) & TRNG_SCR6PL_RUN6P_RNG_MASK) >> TRNG_SCR6PL_RUN6P_RNG_SHIFT)
  527. /*! @brief Set the RUN6P_RNG field to a new value. */
  528. #define TRNG_WR_SCR6PL_RUN6P_RNG(base, value) \
  529. (TRNG_RMW_SCR6PL(base, TRNG_SCR6PL_RUN6P_RNG_MASK, TRNG_SCR6PL_RUN6P_RNG(value)))
  530. /*@}*/
  531. /*******************************************************************************
  532. * TRNG_PKRMAX - RNG Poker Maximum Limit Register
  533. ******************************************************************************/
  534. /*!
  535. * @brief TRNG_PKRMAX - RNG Poker Maximum Limit Register (RW)
  536. *
  537. * Reset value: 0x00006920U
  538. *
  539. * The RNG Poker Maximum Limit Register defines Maximum Limit allowable during
  540. * the TRNG Statistical Check Poker Test. Note that this offset (0xBASE_060C) is
  541. * used as PKRMAX only if MCTL[PRGM] is 1. If MCTL[PRGM] is 0, this offset is used
  542. * as the PKRSQ readback register.
  543. */
  544. /*!
  545. * @name Constants and macros for entire TRNG_PKRMAX register
  546. */
  547. /*@{*/
  548. #define TRNG_PKRMAX_REG(base) ((base)->PKRMAX)
  549. #define TRNG_RD_PKRMAX(base) (TRNG_PKRMAX_REG(base))
  550. #define TRNG_WR_PKRMAX(base, value) (TRNG_PKRMAX_REG(base) = (value))
  551. #define TRNG_RMW_PKRMAX(base, mask, value) (TRNG_WR_PKRMAX(base, (TRNG_RD_PKRMAX(base) & ~(mask)) | (value)))
  552. /*@}*/
  553. /*
  554. * Constants & macros for individual TRNG_PKRMAX bitfields
  555. */
  556. /*!
  557. * @name Register TRNG_PKRMAX, field PKR_MAX[23:0] (RW)
  558. *
  559. * Poker Maximum Limit. During the TRNG Statistical Checks, a "Poker Test" is
  560. * run which requires a maximum and minimum limit. The maximum allowable result is
  561. * programmed in the PKRMAX[PKR_MAX] register. This field is writable only if
  562. * MCTL[PRGM] bit is 1. This register is cleared to 006920h (decimal 26912) by
  563. * writing the MCTL[RST_DEF] bit to 1. Note that the PKRMAX and PKRRNG registers
  564. * combined are used to define the minimum allowable Poker result, which is PKR_MAX -
  565. * PKR_RNG + 1. Note that if MCTL[PRGM] bit is 0, this register address is used
  566. * to read the Poker Test Square Calculation result in register PKRSQ, as defined
  567. * in the following section.
  568. */
  569. /*@{*/
  570. /*! @brief Read current value of the TRNG_PKRMAX_PKR_MAX field. */
  571. #define TRNG_RD_PKRMAX_PKR_MAX(base) ((TRNG_PKRMAX_REG(base) & TRNG_PKRMAX_PKR_MAX_MASK) >> TRNG_PKRMAX_PKR_MAX_SHIFT)
  572. /*! @brief Set the PKR_MAX field to a new value. */
  573. #define TRNG_WR_PKRMAX_PKR_MAX(base, value) \
  574. (TRNG_RMW_PKRMAX(base, TRNG_PKRMAX_PKR_MAX_MASK, TRNG_PKRMAX_PKR_MAX(value)))
  575. /*@}*/
  576. /*******************************************************************************
  577. * TRNG_PKRRNG - RNG Poker Range Register
  578. ******************************************************************************/
  579. /*!
  580. * @brief TRNG_PKRRNG - RNG Poker Range Register (RW)
  581. *
  582. * Reset value: 0x000009A3U
  583. *
  584. * The RNG Poker Range Register defines the difference between the TRNG Poker
  585. * Maximum Limit and the minimum limit. These limits are used during the TRNG
  586. * Statistical Check Poker Test.
  587. */
  588. /*!
  589. * @name Constants and macros for entire TRNG_PKRRNG register
  590. */
  591. /*@{*/
  592. #define TRNG_PKRRNG_REG(base) ((base)->PKRRNG)
  593. #define TRNG_RD_PKRRNG(base) (TRNG_PKRRNG_REG(base))
  594. #define TRNG_WR_PKRRNG(base, value) (TRNG_PKRRNG_REG(base) = (value))
  595. #define TRNG_RMW_PKRRNG(base, mask, value) (TRNG_WR_PKRRNG(base, (TRNG_RD_PKRRNG(base) & ~(mask)) | (value)))
  596. /*@}*/
  597. /*
  598. * Constants & macros for individual TRNG_PKRRNG bitfields
  599. */
  600. /*!
  601. * @name Register TRNG_PKRRNG, field PKR_RNG[15:0] (RW)
  602. *
  603. * Poker Range. During the TRNG Statistical Checks, a "Poker Test" is run which
  604. * requires a maximum and minimum limit. The maximum is programmed in the
  605. * RTPKRMAX[PKR_MAX] register, and the minimum is derived by subtracting the PKR_RNG
  606. * value from the programmed maximum value. This field is writable only if
  607. * MCTL[PRGM] bit is 1. This field will read zeroes if MCTL[PRGM] = 0. This field is
  608. * cleared to 09A3h (decimal 2467) by writing the MCTL[RST_DEF] bit to 1. Note that
  609. * the minimum allowable Poker result is PKR_MAX - PKR_RNG + 1.
  610. */
  611. /*@{*/
  612. /*! @brief Read current value of the TRNG_PKRRNG_PKR_RNG field. */
  613. #define TRNG_RD_PKRRNG_PKR_RNG(base) ((TRNG_PKRRNG_REG(base) & TRNG_PKRRNG_PKR_RNG_MASK) >> TRNG_PKRRNG_PKR_RNG_SHIFT)
  614. /*! @brief Set the PKR_RNG field to a new value. */
  615. #define TRNG_WR_PKRRNG_PKR_RNG(base, value) \
  616. (TRNG_RMW_PKRRNG(base, TRNG_PKRRNG_PKR_RNG_MASK, TRNG_PKRRNG_PKR_RNG(value)))
  617. /*@}*/
  618. /*******************************************************************************
  619. * TRNG_FRQMAX - RNG Frequency Count Maximum Limit Register
  620. ******************************************************************************/
  621. /*!
  622. * @brief TRNG_FRQMAX - RNG Frequency Count Maximum Limit Register (RW)
  623. *
  624. * Reset value: 0x00006400U
  625. *
  626. * The RNG Frequency Count Maximum Limit Register defines the maximum allowable
  627. * count taken by the Entropy sample counter during each Entropy sample. During
  628. * any sample period, if the count is greater than this programmed maximum, a
  629. * Frequency Count Fail is flagged in MCTL[FCT_FAIL] and an error is generated. Note
  630. * that this address (061C) is used as FRQMAX only if MCTL[PRGM] is 1. If
  631. * MCTL[PRGM] is 0, this address is used as FRQCNT readback register.
  632. */
  633. /*!
  634. * @name Constants and macros for entire TRNG_FRQMAX register
  635. */
  636. /*@{*/
  637. #define TRNG_FRQMAX_REG(base) ((base)->FRQMAX)
  638. #define TRNG_RD_FRQMAX(base) (TRNG_FRQMAX_REG(base))
  639. #define TRNG_WR_FRQMAX(base, value) (TRNG_FRQMAX_REG(base) = (value))
  640. #define TRNG_RMW_FRQMAX(base, mask, value) (TRNG_WR_FRQMAX(base, (TRNG_RD_FRQMAX(base) & ~(mask)) | (value)))
  641. /*@}*/
  642. /*
  643. * Constants & macros for individual TRNG_FRQMAX bitfields
  644. */
  645. /*!
  646. * @name Register TRNG_FRQMAX, field FRQ_MAX[21:0] (RW)
  647. *
  648. * Frequency Counter Maximum Limit. Defines the maximum allowable count taken
  649. * during each entropy sample. This field is writable only if MCTL[PRGM] bit is 1.
  650. * This register is cleared to 000640h by writing the MCTL[RST_DEF] bit to 1.
  651. * Note that if MCTL[PRGM] bit is 0, this register address is used to read the
  652. * Frequency Count result in register FRQCNT, as defined in the following section.
  653. */
  654. /*@{*/
  655. /*! @brief Read current value of the TRNG_FRQMAX_FRQ_MAX field. */
  656. #define TRNG_RD_FRQMAX_FRQ_MAX(base) ((TRNG_FRQMAX_REG(base) & TRNG_FRQMAX_FRQ_MAX_MASK) >> TRNG_FRQMAX_FRQ_MAX_SHIFT)
  657. /*! @brief Set the FRQ_MAX field to a new value. */
  658. #define TRNG_WR_FRQMAX_FRQ_MAX(base, value) \
  659. (TRNG_RMW_FRQMAX(base, TRNG_FRQMAX_FRQ_MAX_MASK, TRNG_FRQMAX_FRQ_MAX(value)))
  660. /*@}*/
  661. /*******************************************************************************
  662. * TRNG_FRQMIN - RNG Frequency Count Minimum Limit Register
  663. ******************************************************************************/
  664. /*!
  665. * @brief TRNG_FRQMIN - RNG Frequency Count Minimum Limit Register (RW)
  666. *
  667. * Reset value: 0x00000640U
  668. *
  669. * The RNG Frequency Count Minimum Limit Register defines the minimum allowable
  670. * count taken by the Entropy sample counter during each Entropy sample. During
  671. * any sample period, if the count is less than this programmed minimum, a
  672. * Frequency Count Fail is flagged in MCTL[FCT_FAIL] and an error is generated.
  673. */
  674. /*!
  675. * @name Constants and macros for entire TRNG_FRQMIN register
  676. */
  677. /*@{*/
  678. #define TRNG_FRQMIN_REG(base) ((base)->FRQMIN)
  679. #define TRNG_RD_FRQMIN(base) (TRNG_FRQMIN_REG(base))
  680. #define TRNG_WR_FRQMIN(base, value) (TRNG_FRQMIN_REG(base) = (value))
  681. #define TRNG_RMW_FRQMIN(base, mask, value) (TRNG_WR_FRQMIN(base, (TRNG_RD_FRQMIN(base) & ~(mask)) | (value)))
  682. /*@}*/
  683. /*
  684. * Constants & macros for individual TRNG_FRQMIN bitfields
  685. */
  686. /*!
  687. * @name Register TRNG_FRQMIN, field FRQ_MIN[21:0] (RW)
  688. *
  689. * Frequency Count Minimum Limit. Defines the minimum allowable count taken
  690. * during each entropy sample. This field is writable only if MCTL[PRGM] bit is 1.
  691. * This field will read zeroes if MCTL[PRGM] = 0. This field is cleared to 0000h64
  692. * by writing the MCTL[RST_DEF] bit to 1.
  693. */
  694. /*@{*/
  695. /*! @brief Read current value of the TRNG_FRQMIN_FRQ_MIN field. */
  696. #define TRNG_RD_FRQMIN_FRQ_MIN(base) ((TRNG_FRQMIN_REG(base) & TRNG_FRQMIN_FRQ_MIN_MASK) >> TRNG_FRQMIN_FRQ_MIN_SHIFT)
  697. /*! @brief Set the FRQ_MIN field to a new value. */
  698. #define TRNG_WR_FRQMIN_FRQ_MIN(base, value) \
  699. (TRNG_RMW_FRQMIN(base, TRNG_FRQMIN_FRQ_MIN_MASK, TRNG_FRQMIN_FRQ_MIN(value)))
  700. /*@}*/
  701. /*******************************************************************************
  702. * TRNG_MCTL - RNG Miscellaneous Control Register
  703. ******************************************************************************/
  704. /*!
  705. * @brief TRNG_MCTL - RNG Miscellaneous Control Register (RW)
  706. *
  707. * Reset value: 0x00012001U
  708. *
  709. * This register is intended to be used for programming, configuring and testing
  710. * the RNG. It is the main register to read/write, in order to enable Entropy
  711. * generation, to stop entropy generation and to block access to entropy registers.
  712. * This is done via the special TRNG_ACC and PRGM bits below. The RNG
  713. * Miscellaneous Control Register is a read/write register used to control the RNG's True
  714. * Random Number Generator (TRNG) access, operation and test. Note that in many
  715. * cases two RNG registers share the same address, and a particular register at the
  716. * shared address is selected based upon the value in the PRGM field of the MCTL
  717. * register.
  718. */
  719. /*!
  720. * @name Constants and macros for entire TRNG_MCTL register
  721. */
  722. /*@{*/
  723. #define TRNG_MCTL_REG(base) ((base)->MCTL)
  724. #define TRNG_RD_MCTL(base) (TRNG_MCTL_REG(base))
  725. #define TRNG_WR_MCTL(base, value) (TRNG_MCTL_REG(base) = (value))
  726. #define TRNG_RMW_MCTL(base, mask, value) (TRNG_WR_MCTL(base, (TRNG_RD_MCTL(base) & ~(mask)) | (value)))
  727. /*@}*/
  728. /*!
  729. * @name Register TRNG_MCTL, field FOR_SCLK[7] (RW)
  730. *
  731. * Force System Clock. If set, the system clock is used to operate the TRNG,
  732. * instead of the ring oscillator. This is for test use only, and indeterminate
  733. * results may occur. This bit is writable only if PRGM bit is 1, or PRGM bit is
  734. * being written to 1 simultaneously to writing this bit. This bit is cleared by
  735. * writing the RST_DEF bit to 1.
  736. */
  737. /*@{*/
  738. /*! @brief Read current value of the TRNG_MCTL_FOR_SCLK field. */
  739. #define TRNG_RD_MCTL_FOR_SCLK(base) ((TRNG_MCTL_REG(base) & TRNG_MCTL_FOR_SCLK_MASK) >> TRNG_MCTL_FOR_SCLK_SHIFT)
  740. /*! @brief Set the FOR_SCLK field to a new value. */
  741. #define TRNG_WR_MCTL_FOR_SCLK(base, value) \
  742. (TRNG_RMW_MCTL(base, (TRNG_MCTL_FOR_SCLK_MASK | TRNG_MCTL_ERR_MASK), TRNG_MCTL_FOR_SCLK(value)))
  743. /*@}*/
  744. /*!
  745. * @name Register TRNG_MCTL, field OSC_DIV[3:2] (RW)
  746. *
  747. * Oscillator Divide. Determines the amount of dividing done to the ring
  748. * oscillator before it is used by the TRNG.This field is writable only if PRGM bit is
  749. * 1, or PRGM bit is being written to 1 simultaneously to writing this field. This
  750. * field is cleared to 00 by writing the RST_DEF bit to 1.
  751. *
  752. * Values:
  753. * - 0b00 - use ring oscillator with no divide
  754. * - 0b01 - use ring oscillator divided-by-2
  755. * - 0b10 - use ring oscillator divided-by-4
  756. * - 0b11 - use ring oscillator divided-by-8
  757. */
  758. /*@{*/
  759. /*! @brief Read current value of the TRNG_MCTL_OSC_DIV field. */
  760. #define TRNG_RD_MCTL_OSC_DIV(base) ((TRNG_MCTL_REG(base) & TRNG_MCTL_OSC_DIV_MASK) >> TRNG_MCTL_OSC_DIV_SHIFT)
  761. /*! @brief Set the OSC_DIV field to a new value. */
  762. #define TRNG_WR_MCTL_OSC_DIV(base, value) \
  763. (TRNG_RMW_MCTL(base, (TRNG_MCTL_OSC_DIV_MASK | TRNG_MCTL_ERR_MASK), TRNG_MCTL_OSC_DIV(value)))
  764. /*@}*/
  765. /*!
  766. * @name Register TRNG_MCTL, field SAMP_MODE[1:0] (RW)
  767. *
  768. * Sample Mode. Determines the method of sampling the ring oscillator while
  769. * generating the Entropy value:This field is writable only if PRGM bit is 1, or PRGM
  770. * bit is being written to 1 simultaneously with writing this field. This field
  771. * is cleared to 01 by writing the RST_DEF bit to 1.
  772. *
  773. * Values:
  774. * - 0b00 - use Von Neumann data into both Entropy shifter and Statistical
  775. * Checker
  776. * - 0b01 - use raw data into both Entropy shifter and Statistical Checker
  777. * - 0b10 - use Von Neumann data into Entropy shifter. Use raw data into
  778. * Statistical Checker
  779. * - 0b11 - reserved.
  780. */
  781. /*@{*/
  782. /*! @brief Read current value of the TRNG_MCTL_SAMP_MODE field. */
  783. #define TRNG_RD_MCTL_SAMP_MODE(base) ((TRNG_MCTL_REG(base) & TRNG_MCTL_SAMP_MODE_MASK) >> TRNG_MCTL_SAMP_MODE_SHIFT)
  784. /*! @brief Set the SAMP_MODE field to a new value. */
  785. #define TRNG_WR_MCTL_SAMP_MODE(base, value) \
  786. (TRNG_RMW_MCTL(base, (TRNG_MCTL_SAMP_MODE_MASK | TRNG_MCTL_ERR_MASK), TRNG_MCTL_SAMP_MODE(value)))
  787. /*@}*/
  788. /*!
  789. * @name Register TRNG_MCTL, field PRGM[16] (RW)
  790. *
  791. * Programming Mode Select. When this bit is 1, the TRNG is in Program Mode,
  792. * otherwise it is in Run Mode. No Entropy value will be generated while the TRNG is
  793. * in Program Mode. Note that different RNG registers are accessible at the same
  794. * address depending on whether PRGM is set to 1 or 0. This is noted in the RNG
  795. * register descriptions.
  796. */
  797. /*@{*/
  798. /*! @brief Read current value of the TRNG_MCTL_PRGM field. */
  799. #define TRNG_RD_MCTL_PRGM(base) ((TRNG_MCTL_REG(base) & TRNG_MCTL_PRGM_MASK) >> TRNG_MCTL_PRGM_SHIFT)
  800. /*! @brief Set the PRGM field to a new value. */
  801. #define TRNG_WR_MCTL_PRGM(base, value) \
  802. (TRNG_RMW_MCTL(base, (TRNG_MCTL_PRGM_MASK | TRNG_MCTL_ERR_MASK), TRNG_MCTL_PRGM(value)))
  803. /*@}*/
  804. /*!
  805. * @name Register TRNG_MCTL, field RST_DEF[6] (WO)
  806. *
  807. * Reset Defaults. Writing a 1 to this bit clears various TRNG registers, and
  808. * bits within registers, to their default state. This bit is writable only if PRGM
  809. * bit is 1, or PRGM bit is being written to 1 simultaneously to writing this
  810. * bit. Reading this bit always produces a 0.
  811. */
  812. /*@{*/
  813. /*! @brief Set the RST_DEF field to a new value. */
  814. #define TRNG_WR_MCTL_RST_DEF(base, value) \
  815. (TRNG_RMW_MCTL(base, (TRNG_MCTL_RST_DEF_MASK | TRNG_MCTL_ERR_MASK), TRNG_MCTL_RST_DEF(value)))
  816. /*@}*/
  817. #if !(defined(FSL_FEATURE_TRNG_HAS_NO_TRNG_ACC) && (FSL_FEATURE_TRNG_HAS_NO_TRNG_ACC > 0))
  818. /*!
  819. * @name Register TRNG_MCTL, field TRNG_ACC[5] (RW)
  820. *
  821. * TRNG Access Mode. If this bit is set to 1, the TRNG will generate an Entropy
  822. * value that can be read via the ENT0-ENT15 registers. The Entropy value may be
  823. * read once the ENT VAL bit is asserted. Also see ENTa register descriptions
  824. * (For a = 0 to 15).
  825. */
  826. /*@{*/
  827. /*! @brief Read current value of the TRNG_MCTL_TRNG_ACC field. */
  828. #define TRNG_RD_MCTL_TRNG_ACC(base) ((TRNG_MCTL_REG(base) & TRNG_MCTL_TRNG_ACC_MASK) >> TRNG_MCTL_TRNG_ACC_SHIFT)
  829. /*! @brief Set the TRNG_ACC field to a new value. */
  830. #define TRNG_WR_MCTL_TRNG_ACC(base, value) \
  831. (TRNG_RMW_MCTL(base, (TRNG_MCTL_TRNG_ACC_MASK | TRNG_MCTL_ERR_MASK), TRNG_MCTL_TRNG_ACC(value)))
  832. /*@}*/
  833. #endif
  834. /*!
  835. * @name Register TRNG_MCTL, field TSTOP_OK[13] (RO)
  836. *
  837. * TRNG_OK_TO_STOP. Software should check that this bit is a 1 before
  838. * transitioning RNG to low power mode (RNG clock stopped). RNG turns on the TRNG
  839. * free-running ring oscillator whenever new entropy is being generated and turns off the
  840. * ring oscillator when entropy generation is complete. If the RNG clock is
  841. * stopped while the TRNG ring oscillator is running, the oscillator will continue
  842. * running even though the RNG clock is stopped. TSTOP_OK is asserted when the TRNG
  843. * ring oscillator is not running. and therefore it is ok to stop the RNG clock.
  844. */
  845. /*@{*/
  846. /*! @brief Read current value of the TRNG_MCTL_TSTOP_OK field. */
  847. #define TRNG_RD_MCTL_TSTOP_OK(base) ((TRNG_MCTL_REG(base) & TRNG_MCTL_TSTOP_OK_MASK) >> TRNG_MCTL_TSTOP_OK_SHIFT)
  848. /*@}*/
  849. /*!
  850. * @name Register TRNG_MCTL, field ENT_VAL[10] (RO)
  851. *
  852. * Read only: Entropy Valid. Will assert only if TRNG ACC bit is set, and then
  853. * after an entropy value is generated. Will be cleared when ENT15 is read. (ENT0
  854. * through ENT14 should be read before reading ENT15).
  855. */
  856. /*@{*/
  857. /*! @brief Read current value of the TRNG_MCTL_ENT_VAL field. */
  858. #define TRNG_RD_MCTL_ENT_VAL(base) ((TRNG_MCTL_REG(base) & TRNG_MCTL_ENT_VAL_MASK) >> TRNG_MCTL_ENT_VAL_SHIFT)
  859. /*@}*/
  860. /*!
  861. * @name Register TRNG_MCTL, field ERR[12] (W1C)
  862. *
  863. * Read: Error status. 1 = error detected. 0 = no error.Write: Write 1 to clear
  864. * errors. Writing 0 has no effect.
  865. */
  866. /*@{*/
  867. /*! @brief Read current value of the TRNG_MCTL_ERR field. */
  868. #define TRNG_RD_MCTL_ERR(base) ((TRNG_MCTL_REG(base) & TRNG_MCTL_ERR_MASK) >> TRNG_MCTL_ERR_SHIFT)
  869. /*! @brief Set the ERR field to a new value. */
  870. #define TRNG_WR_MCTL_ERR(base, value) (TRNG_RMW_MCTL(base, TRNG_MCTL_ERR_MASK, TRNG_MCTL_ERR(value)))
  871. /*@}*/
  872. /*******************************************************************************
  873. * TRNG_SDCTL - RNG Seed Control Register
  874. ******************************************************************************/
  875. /*!
  876. * @brief TRNG_SDCTL - RNG Seed Control Register (RW)
  877. *
  878. * Reset value: 0x0C8009C4U
  879. *
  880. * The RNG Seed Control Register contains two fields. One field defines the
  881. * length (in system clocks) of each Entropy sample (ENT_DLY), and the other field
  882. * indicates the number of samples that will taken during each TRNG Entropy
  883. * generation (SAMP_SIZE).
  884. */
  885. /*!
  886. * @name Constants and macros for entire TRNG_SDCTL register
  887. */
  888. /*@{*/
  889. #define TRNG_SDCTL_REG(base) ((base)->SDCTL)
  890. #define TRNG_RD_SDCTL(base) (TRNG_SDCTL_REG(base))
  891. #define TRNG_WR_SDCTL(base, value) (TRNG_SDCTL_REG(base) = (value))
  892. #define TRNG_RMW_SDCTL(base, mask, value) (TRNG_WR_SDCTL(base, (TRNG_RD_SDCTL(base) & ~(mask)) | (value)))
  893. /*@}*/
  894. /*
  895. * Constants & macros for individual TRNG_SDCTL bitfields
  896. */
  897. /*!
  898. * @name Register TRNG_SDCTL, field SAMP_SIZE[15:0] (RW)
  899. *
  900. * Sample Size. Defines the total number of Entropy samples that will be taken
  901. * during Entropy generation. This field is writable only if MCTL[PRGM] bit is 1.
  902. * This field will read zeroes if MCTL[PRGM] = 0. This field is cleared to 09C4h
  903. * (decimal 2500) by writing the MCTL[RST_DEF] bit to 1.
  904. */
  905. /*@{*/
  906. /*! @brief Read current value of the TRNG_SDCTL_SAMP_SIZE field. */
  907. #define TRNG_RD_SDCTL_SAMP_SIZE(base) ((TRNG_SDCTL_REG(base) & TRNG_SDCTL_SAMP_SIZE_MASK) >> TRNG_SDCTL_SAMP_SIZE_SHIFT)
  908. /*! @brief Set the SAMP_SIZE field to a new value. */
  909. #define TRNG_WR_SDCTL_SAMP_SIZE(base, value) \
  910. (TRNG_RMW_SDCTL(base, TRNG_SDCTL_SAMP_SIZE_MASK, TRNG_SDCTL_SAMP_SIZE(value)))
  911. /*@}*/
  912. /*!
  913. * @name Register TRNG_SDCTL, field ENT_DLY[31:16] (RW)
  914. *
  915. * Entropy Delay. Defines the length (in system clocks) of each Entropy sample
  916. * taken. This field is writable only if MCTL[PRGM] bit is 1. This field will read
  917. * zeroes if MCTL[PRGM] = 0. This field is cleared to 0C80h (decimal 3200) by
  918. * writing the MCTL[RST_DEF] bit to 1.
  919. */
  920. /*@{*/
  921. /*! @brief Read current value of the TRNG_SDCTL_ENT_DLY field. */
  922. #define TRNG_RD_SDCTL_ENT_DLY(base) ((TRNG_SDCTL_REG(base) & TRNG_SDCTL_ENT_DLY_MASK) >> TRNG_SDCTL_ENT_DLY_SHIFT)
  923. /*! @brief Set the ENT_DLY field to a new value. */
  924. #define TRNG_WR_SDCTL_ENT_DLY(base, value) (TRNG_RMW_SDCTL(base, TRNG_SDCTL_ENT_DLY_MASK, TRNG_SDCTL_ENT_DLY(value)))
  925. /*@}*/
  926. /*******************************************************************************
  927. * TRNG_SBLIM - RNG Sparse Bit Limit Register
  928. ******************************************************************************/
  929. /*!
  930. * @brief TRNG_SBLIM - RNG Sparse Bit Limit Register (RW)
  931. *
  932. * Reset value: 0x0000003FU
  933. *
  934. * The RNG Sparse Bit Limit Register is used when Von Neumann sampling is
  935. * selected during Entropy Generation. It defines the maximum number of consecutive Von
  936. * Neumann samples which may be discarded before an error is generated. Note
  937. * that this address (0xBASE_0614) is used as SBLIM only if MCTL[PRGM] is 1. If
  938. * MCTL[PRGM] is 0, this address is used as TOTSAM readback register.
  939. */
  940. /*!
  941. * @name Constants and macros for entire TRNG_SBLIM register
  942. */
  943. /*@{*/
  944. #define TRNG_SBLIM_REG(base) ((base)->SBLIM)
  945. #define TRNG_RD_SBLIM(base) (TRNG_SBLIM_REG(base))
  946. #define TRNG_WR_SBLIM(base, value) (TRNG_SBLIM_REG(base) = (value))
  947. #define TRNG_RMW_SBLIM(base, mask, value) (TRNG_WR_SBLIM(base, (TRNG_RD_SBLIM(base) & ~(mask)) | (value)))
  948. /*@}*/
  949. /*
  950. * Constants & macros for individual TRNG_SBLIM bitfields
  951. */
  952. /*!
  953. * @name Register TRNG_SBLIM, field SB_LIM[9:0] (RW)
  954. *
  955. * Sparse Bit Limit. During Von Neumann sampling (if enabled by MCTL[SAMP_MODE],
  956. * samples are discarded if two consecutive raw samples are both 0 or both 1. If
  957. * this discarding occurs for a long period of time, it indicates that there is
  958. * insufficient Entropy. The Sparse Bit Limit defines the maximum number of
  959. * consecutive samples that may be discarded before an error is generated. This field
  960. * is writable only if MCTL[PRGM] bit is 1. This register is cleared to 03hF by
  961. * writing the MCTL[RST_DEF] bit to 1. Note that if MCTL[PRGM] bit is 0, this
  962. * register address is used to read the Total Samples count in register TOTSAM, as
  963. * defined in the following section.
  964. */
  965. /*@{*/
  966. /*! @brief Read current value of the TRNG_SBLIM_SB_LIM field. */
  967. #define TRNG_RD_SBLIM_SB_LIM(base) ((TRNG_SBLIM_REG(base) & TRNG_SBLIM_SB_LIM_MASK) >> TRNG_SBLIM_SB_LIM_SHIFT)
  968. /*! @brief Set the SB_LIM field to a new value. */
  969. #define TRNG_WR_SBLIM_SB_LIM(base, value) (TRNG_RMW_SBLIM(base, TRNG_SBLIM_SB_LIM_MASK, TRNG_SBLIM_SB_LIM(value)))
  970. /*@}*/
  971. /*******************************************************************************
  972. * TRNG_SCMISC - RNG Statistical Check Miscellaneous Register
  973. ******************************************************************************/
  974. /*!
  975. * @brief TRNG_SCMISC - RNG Statistical Check Miscellaneous Register (RW)
  976. *
  977. * Reset value: 0x0001001FU
  978. *
  979. * The RNG Statistical Check Miscellaneous Register contains the Long Run
  980. * Maximum Limit value and the Retry Count value. This register is accessible only when
  981. * the MCTL[PRGM] bit is 1, otherwise this register will read zeroes, and cannot
  982. * be written.
  983. */
  984. /*!
  985. * @name Constants and macros for entire TRNG_SCMISC register
  986. */
  987. /*@{*/
  988. #define TRNG_SCMISC_REG(base) ((base)->SCMISC)
  989. #define TRNG_RD_SCMISC(base) (TRNG_SCMISC_REG(base))
  990. #define TRNG_WR_SCMISC(base, value) (TRNG_SCMISC_REG(base) = (value))
  991. #define TRNG_RMW_SCMISC(base, mask, value) (TRNG_WR_SCMISC(base, (TRNG_RD_SCMISC(base) & ~(mask)) | (value)))
  992. /*@}*/
  993. /*
  994. * Constants & macros for individual TRNG_SCMISC bitfields
  995. */
  996. /*!
  997. * @name Register TRNG_SCMISC, field LRUN_MAX[7:0] (RW)
  998. *
  999. * LONG RUN MAX LIMIT. This value is the largest allowable number of consecutive
  1000. * samples of all 1, or all 0, that is allowed during the Entropy generation.
  1001. * This field is writable only if MCTL[PRGM] bit is 1. This field will read zeroes
  1002. * if MCTL[PRGM] = 0. This field is cleared to 22h by writing the MCTL[RST_DEF]
  1003. * bit to 1.
  1004. */
  1005. /*@{*/
  1006. /*! @brief Read current value of the TRNG_SCMISC_LRUN_MAX field. */
  1007. #define TRNG_RD_SCMISC_LRUN_MAX(base) \
  1008. ((TRNG_SCMISC_REG(base) & TRNG_SCMISC_LRUN_MAX_MASK) >> TRNG_SCMISC_LRUN_MAX_SHIFT)
  1009. /*! @brief Set the LRUN_MAX field to a new value. */
  1010. #define TRNG_WR_SCMISC_LRUN_MAX(base, value) \
  1011. (TRNG_RMW_SCMISC(base, TRNG_SCMISC_LRUN_MAX_MASK, TRNG_SCMISC_LRUN_MAX(value)))
  1012. /*@}*/
  1013. /*******************************************************************************
  1014. * TRNG_ENT - RNG TRNG Entropy Read Register
  1015. ******************************************************************************/
  1016. /*!
  1017. * @brief TRNG_ENT - RNG TRNG Entropy Read Register (RO)
  1018. *
  1019. * Reset value: 0x00000000U
  1020. *
  1021. * The RNG TRNG can be programmed to generate an entropy value that is readable
  1022. * via the SkyBlue bus. To do this, set the MCTL[TRNG_ACC] bit to 1. Once the
  1023. * entropy value has been generated, the MCTL[ENT_VAL] bit will be set to 1. At this
  1024. * point, ENT0 through ENT15 may be read to retrieve the 512-bit entropy value.
  1025. * Note that once ENT15 is read, the entropy value will be cleared and a new
  1026. * value will begin generation, so it is important that ENT15 be read last. These
  1027. * registers are readable only when MCTL[PRGM] = 0 (Run Mode), MCTL[TRNG_ACC] = 1
  1028. * (TRNG access mode) and MCTL[ENT_VAL] = 1, otherwise zeroes will be read.
  1029. */
  1030. /*!
  1031. * @name Constants and macros for entire TRNG_ENT register
  1032. */
  1033. /*@{*/
  1034. #define TRNG_ENT_REG(base, index) ((base)->ENT[index])
  1035. #define TRNG_RD_ENT(base, index) (TRNG_ENT_REG(base, index))
  1036. /*@}*/
  1037. /*******************************************************************************
  1038. * TRNG_SEC_CFG - RNG Security Configuration Register
  1039. ******************************************************************************/
  1040. /*!
  1041. * @brief TRNG_SEC_CFG - RNG Security Configuration Register (RW)
  1042. *
  1043. * Reset value: 0x00000000U
  1044. *
  1045. * The RNG Security Configuration Register is a read/write register used to
  1046. * control the test mode, programmability and state modes of the RNG. Many bits are
  1047. * place holders for this version. More configurability will be added here. Clears
  1048. * on asynchronous reset. For SA-TRNG releases before 2014/July/01, offsets 0xA0
  1049. * to 0xAC used to be 0xB0 to 0xBC respectively. So, update newer tests that use
  1050. * these registers, if hard coded.
  1051. */
  1052. /*!
  1053. * @name Constants and macros for entire TRNG_SEC_CFG register
  1054. */
  1055. /*@{*/
  1056. #define TRNG_SEC_CFG_REG(base) ((base)->SEC_CFG)
  1057. #define TRNG_RD_SEC_CFG(base) (TRNG_SEC_CFG_REG(base))
  1058. #define TRNG_WR_SEC_CFG(base, value) (TRNG_SEC_CFG_REG(base) = (value))
  1059. #define TRNG_RMW_SEC_CFG(base, mask, value) (TRNG_WR_SEC_CFG(base, (TRNG_RD_SEC_CFG(base) & ~(mask)) | (value)))
  1060. /*@}*/
  1061. /*!
  1062. * @name Register TRNG_SEC_CFG, field NO_PRGM[1] (RW)
  1063. *
  1064. * If set the TRNG registers cannot be programmed. That is, regardless of the
  1065. * TRNG access mode in the SA-TRNG Miscellaneous Control Register.
  1066. *
  1067. * Values:
  1068. * - 0b0 - Programability of registers controlled only by the RNG Miscellaneous
  1069. * Control Register's access mode bit.
  1070. * - 0b1 - Overides RNG Miscellaneous Control Register access mode and prevents
  1071. * TRNG register programming.
  1072. */
  1073. /*@{*/
  1074. /*! @brief Read current value of the TRNG_SEC_CFG_NO_PRGM field. */
  1075. #define TRNG_RD_SEC_CFG_NO_PRGM(base) \
  1076. ((TRNG_SEC_CFG_REG(base) & TRNG_SEC_CFG_NO_PRGM_MASK) >> TRNG_SEC_CFG_NO_PRGM_SHIFT)
  1077. /*! @brief Set the NO_PRGM field to a new value. */
  1078. #define TRNG_WR_SEC_CFG_NO_PRGM(base, value) \
  1079. (TRNG_RMW_SEC_CFG(base, TRNG_SEC_CFG_NO_PRGM_MASK, TRNG_SEC_CFG_NO_PRGM(value)))
  1080. /*@}*/
  1081. /*! @brief Array to map TRNG instance number to base pointer. */
  1082. static TRNG_Type *const s_trngBases[] = TRNG_BASE_PTRS;
  1083. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  1084. /*! @brief Clock array name */
  1085. static const clock_ip_name_t s_trngClock[] = TRNG_CLOCKS;
  1086. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  1087. /*******************************************************************************
  1088. * Prototypes
  1089. *******************************************************************************/
  1090. static status_t trng_ApplyUserConfig(TRNG_Type *base, const trng_config_t *userConfig);
  1091. static status_t trng_SetRetryCount(TRNG_Type *base, uint8_t retry_count);
  1092. static status_t trng_SetMonobitLimit(TRNG_Type *base, uint32_t range, uint32_t limit_maximum);
  1093. static status_t trng_SetRunBit1Limit(TRNG_Type *base, uint32_t range, uint32_t limit_maximum);
  1094. static status_t trng_SetRunBit2Limit(TRNG_Type *base, uint32_t range, uint32_t limit_maximum);
  1095. static status_t trng_SetRunBit3Limit(TRNG_Type *base, uint32_t range, uint32_t limit_maximum);
  1096. static status_t trng_SetRunBit4Limit(TRNG_Type *base, uint32_t range, uint32_t limit_maximum);
  1097. static status_t trng_SetRunBit5Limit(TRNG_Type *base, uint32_t range, uint32_t limit_maximum);
  1098. static status_t trng_SetRunBit6Limit(TRNG_Type *base, uint32_t range, uint32_t limit_maximum);
  1099. static status_t trng_SetPokerMaxLimit(TRNG_Type *base, uint32_t range, uint32_t limit_maximum);
  1100. static status_t trng_SetFrequencyCountMaxLimit(TRNG_Type *base, uint32_t limit_minimum, uint32_t limit_maximum);
  1101. static status_t trng_SetStatisticalCheckLimit(TRNG_Type *base,
  1102. trng_statistical_check_t statistical_check,
  1103. const trng_statistical_check_limit_t *limit);
  1104. static uint32_t trng_ReadEntropy(TRNG_Type *base, uint32_t index);
  1105. static uint32_t trng_GetInstance(TRNG_Type *base);
  1106. /*******************************************************************************
  1107. * Code
  1108. ******************************************************************************/
  1109. static uint32_t trng_GetInstance(TRNG_Type *base)
  1110. {
  1111. uint32_t instance;
  1112. /* Find the instance index from base address mappings. */
  1113. for (instance = 0; instance < ARRAY_SIZE(s_trngBases); instance++)
  1114. {
  1115. if (s_trngBases[instance] == base)
  1116. {
  1117. break;
  1118. }
  1119. }
  1120. assert(instance < ARRAY_SIZE(s_trngBases));
  1121. return instance;
  1122. }
  1123. /*FUNCTION*********************************************************************
  1124. *
  1125. * Function Name : TRNG_InitUserConfigDefault
  1126. * Description : Initializes user configuration structure to default settings.
  1127. *
  1128. *END*************************************************************************/
  1129. /*!
  1130. * brief Initializes the user configuration structure to default values.
  1131. *
  1132. * This function initializes the configuration structure to default values. The default
  1133. * values are as follows.
  1134. * code
  1135. * userConfig->lock = 0;
  1136. * userConfig->clockMode = kTRNG_ClockModeRingOscillator;
  1137. * userConfig->ringOscDiv = kTRNG_RingOscDiv0; Or to other kTRNG_RingOscDiv[2|8] depending on the platform.
  1138. * userConfig->sampleMode = kTRNG_SampleModeRaw;
  1139. * userConfig->entropyDelay = 3200;
  1140. * userConfig->sampleSize = 2500;
  1141. * userConfig->sparseBitLimit = TRNG_USER_CONFIG_DEFAULT_SPARSE_BIT_LIMIT;
  1142. * userConfig->retryCount = 63;
  1143. * userConfig->longRunMaxLimit = 34;
  1144. * userConfig->monobitLimit.maximum = 1384;
  1145. * userConfig->monobitLimit.minimum = 1116;
  1146. * userConfig->runBit1Limit.maximum = 405;
  1147. * userConfig->runBit1Limit.minimum = 227;
  1148. * userConfig->runBit2Limit.maximum = 220;
  1149. * userConfig->runBit2Limit.minimum = 98;
  1150. * userConfig->runBit3Limit.maximum = 125;
  1151. * userConfig->runBit3Limit.minimum = 37;
  1152. * userConfig->runBit4Limit.maximum = 75;
  1153. * userConfig->runBit4Limit.minimum = 11;
  1154. * userConfig->runBit5Limit.maximum = 47;
  1155. * userConfig->runBit5Limit.minimum = 1;
  1156. * userConfig->runBit6PlusLimit.maximum = 47;
  1157. * userConfig->runBit6PlusLimit.minimum = 1;
  1158. * userConfig->pokerLimit.maximum = 26912;
  1159. * userConfig->pokerLimit.minimum = 24445;
  1160. * userConfig->frequencyCountLimit.maximum = 25600;
  1161. * userConfig->frequencyCountLimit.minimum = 1600;
  1162. * endcode
  1163. *
  1164. * param userConfig User configuration structure.
  1165. * return If successful, returns the kStatus_TRNG_Success. Otherwise, it returns an error.
  1166. */
  1167. status_t TRNG_GetDefaultConfig(trng_config_t *userConfig)
  1168. {
  1169. status_t result;
  1170. if (userConfig != NULL)
  1171. {
  1172. /* Initializes the configuration structure to default values. */
  1173. /* Lock programmability of TRNG registers. */
  1174. userConfig->lock = (bool)TRNG_USER_CONFIG_DEFAULT_LOCK;
  1175. /* Clock settings */
  1176. userConfig->clockMode = kTRNG_ClockModeRingOscillator;
  1177. userConfig->ringOscDiv = TRNG_USER_CONFIG_DEFAULT_OSC_DIV;
  1178. userConfig->sampleMode = kTRNG_SampleModeRaw;
  1179. /* Seed control*/
  1180. userConfig->entropyDelay = TRNG_USER_CONFIG_DEFAULT_ENTROPY_DELAY;
  1181. userConfig->sampleSize = TRNG_USER_CONFIG_DEFAULT_SAMPLE_SIZE;
  1182. userConfig->sparseBitLimit = TRNG_USER_CONFIG_DEFAULT_SPARSE_BIT_LIMIT;
  1183. /* Statistical Check Parameters.*/
  1184. userConfig->retryCount = TRNG_USER_CONFIG_DEFAULT_RETRY_COUNT;
  1185. userConfig->longRunMaxLimit = TRNG_USER_CONFIG_DEFAULT_RUN_MAX_LIMIT;
  1186. userConfig->monobitLimit.maximum = TRNG_USER_CONFIG_DEFAULT_MONOBIT_MAXIMUM;
  1187. userConfig->monobitLimit.minimum = TRNG_USER_CONFIG_DEFAULT_MONOBIT_MINIMUM;
  1188. userConfig->runBit1Limit.maximum = TRNG_USER_CONFIG_DEFAULT_RUNBIT1_MAXIMUM;
  1189. userConfig->runBit1Limit.minimum = TRNG_USER_CONFIG_DEFAULT_RUNBIT1_MINIMUM;
  1190. userConfig->runBit2Limit.maximum = TRNG_USER_CONFIG_DEFAULT_RUNBIT2_MAXIMUM;
  1191. userConfig->runBit2Limit.minimum = TRNG_USER_CONFIG_DEFAULT_RUNBIT2_MINIMUM;
  1192. userConfig->runBit3Limit.maximum = TRNG_USER_CONFIG_DEFAULT_RUNBIT3_MAXIMUM;
  1193. userConfig->runBit3Limit.minimum = TRNG_USER_CONFIG_DEFAULT_RUNBIT3_MINIMUM;
  1194. userConfig->runBit4Limit.maximum = TRNG_USER_CONFIG_DEFAULT_RUNBIT4_MAXIMUM;
  1195. userConfig->runBit4Limit.minimum = TRNG_USER_CONFIG_DEFAULT_RUNBIT4_MINIMUM;
  1196. userConfig->runBit5Limit.maximum = TRNG_USER_CONFIG_DEFAULT_RUNBIT5_MAXIMUM;
  1197. userConfig->runBit5Limit.minimum = TRNG_USER_CONFIG_DEFAULT_RUNBIT5_MINIMUM;
  1198. userConfig->runBit6PlusLimit.maximum = TRNG_USER_CONFIG_DEFAULT_RUNBIT6PLUS_MAXIMUM;
  1199. userConfig->runBit6PlusLimit.minimum = TRNG_USER_CONFIG_DEFAULT_RUNBIT6PLUS_MINIMUM;
  1200. /* Limits for statistical check of "Poker Test". */
  1201. userConfig->pokerLimit.maximum = TRNG_USER_CONFIG_DEFAULT_POKER_MAXIMUM;
  1202. userConfig->pokerLimit.minimum = TRNG_USER_CONFIG_DEFAULT_POKER_MINIMUM;
  1203. /* Limits for statistical check of entropy sample frequency count. */
  1204. userConfig->frequencyCountLimit.maximum = TRNG_USER_CONFIG_DEFAULT_FREQUENCY_MAXIMUM;
  1205. userConfig->frequencyCountLimit.minimum = TRNG_USER_CONFIG_DEFAULT_FREQUENCY_MINIMUM;
  1206. result = kStatus_Success;
  1207. }
  1208. else
  1209. {
  1210. result = kStatus_InvalidArgument;
  1211. }
  1212. return result;
  1213. }
  1214. /*!
  1215. * @brief Sets the TRNG retry count.
  1216. *
  1217. * This function sets the retry counter which defines the number of times a
  1218. * statistical check may fails during the TRNG Entropy Generation before
  1219. * generating an error.
  1220. */
  1221. static status_t trng_SetRetryCount(TRNG_Type *base, uint8_t retry_count)
  1222. {
  1223. status_t status;
  1224. if ((retry_count >= 1u) && (retry_count <= 15u))
  1225. {
  1226. /* Set retry count.*/
  1227. TRNG_WR_SCMISC_RTY_CT(base, retry_count);
  1228. status = kStatus_Success;
  1229. }
  1230. else
  1231. {
  1232. status = kStatus_InvalidArgument;
  1233. }
  1234. return status;
  1235. }
  1236. /*!
  1237. * @brief Sets statistical Check Monobit Limit Register .
  1238. *
  1239. * This function set register TRNG_SCML - Statistical Check Monobit Limit Register
  1240. */
  1241. static status_t trng_SetMonobitLimit(TRNG_Type *base, uint32_t range, uint32_t limit_maximum)
  1242. {
  1243. status_t status;
  1244. /* Check input parameters*/
  1245. if ((range <= 0xffffu) && (limit_maximum <= 0xffffu))
  1246. {
  1247. /* Set TRNG_SCML register */
  1248. TRNG_WR_SCML_MONO_MAX(base, limit_maximum);
  1249. TRNG_WR_SCML_MONO_RNG(base, range);
  1250. status = kStatus_Success;
  1251. }
  1252. else
  1253. {
  1254. status = kStatus_InvalidArgument;
  1255. }
  1256. return status;
  1257. }
  1258. /*!
  1259. * @brief Sets statistical Statistical Check Run Length 1 Limit Register .
  1260. *
  1261. * This function set register TRNG_SCR1L - Statistical Check Run Length 1 Limit Register
  1262. */
  1263. static status_t trng_SetRunBit1Limit(TRNG_Type *base, uint32_t range, uint32_t limit_maximum)
  1264. {
  1265. status_t status;
  1266. /* Check input parameters*/
  1267. if ((range <= 0x7fffu) && (limit_maximum <= 0x7fffu))
  1268. {
  1269. /* Set TRNG_SCR1L register */
  1270. TRNG_WR_SCR1L_RUN1_MAX(base, limit_maximum);
  1271. TRNG_WR_SCR1L_RUN1_RNG(base, range);
  1272. status = kStatus_Success;
  1273. }
  1274. else
  1275. {
  1276. status = kStatus_InvalidArgument;
  1277. }
  1278. return status;
  1279. }
  1280. /*!
  1281. * @brief Sets statistical Statistical Check Run Length 2 Limit Register .
  1282. *
  1283. * This function set register TRNG_SCR2L - Statistical Check Run Length 2 Limit Register
  1284. */
  1285. static status_t trng_SetRunBit2Limit(TRNG_Type *base, uint32_t range, uint32_t limit_maximum)
  1286. {
  1287. status_t status;
  1288. /* Check input parameters*/
  1289. if ((range <= 0x3fffu) && (limit_maximum <= 0x3fffu))
  1290. {
  1291. /* Set TRNG_SCR2L register */
  1292. TRNG_WR_SCR2L_RUN2_MAX(base, limit_maximum);
  1293. TRNG_WR_SCR2L_RUN2_RNG(base, range);
  1294. status = kStatus_Success;
  1295. }
  1296. else
  1297. {
  1298. status = kStatus_InvalidArgument;
  1299. }
  1300. return status;
  1301. }
  1302. /*!
  1303. * @brief Sets statistical Statistical Check Run Length 3 Limit Register .
  1304. *
  1305. * This function set register TRNG_SCR3L - Statistical Check Run Length 3 Limit Register
  1306. */
  1307. static status_t trng_SetRunBit3Limit(TRNG_Type *base, uint32_t range, uint32_t limit_maximum)
  1308. {
  1309. status_t status;
  1310. /* Check input parameters*/
  1311. if ((range <= 0x1fffu) && (limit_maximum <= 0x1fffu))
  1312. {
  1313. /* Set TRNG_SCR3L register */
  1314. TRNG_WR_SCR3L_RUN3_MAX(base, limit_maximum);
  1315. TRNG_WR_SCR3L_RUN3_RNG(base, range);
  1316. status = kStatus_Success;
  1317. }
  1318. else
  1319. {
  1320. status = kStatus_InvalidArgument;
  1321. }
  1322. return status;
  1323. }
  1324. /*!
  1325. * @brief Sets statistical Statistical Check Run Length 4 Limit Register .
  1326. * This function set register TRNG_SCR4L - Statistical Check Run Length 4 Limit Register
  1327. */
  1328. static status_t trng_SetRunBit4Limit(TRNG_Type *base, uint32_t range, uint32_t limit_maximum)
  1329. {
  1330. status_t status;
  1331. /* Check input parameters*/
  1332. if ((range <= 0xfffu) && (limit_maximum <= 0xfffu))
  1333. {
  1334. /* Set TRNG_SCR4L register */
  1335. TRNG_WR_SCR4L_RUN4_MAX(base, limit_maximum);
  1336. TRNG_WR_SCR4L_RUN4_RNG(base, range);
  1337. status = kStatus_Success;
  1338. }
  1339. else
  1340. {
  1341. status = kStatus_InvalidArgument;
  1342. }
  1343. return status;
  1344. }
  1345. /*!
  1346. * @brief Sets statistical Statistical Check Run Length 5 Limit Register .
  1347. * This function set register TRNG_SCR5L - Statistical Check Run Length 5 Limit Register
  1348. */
  1349. static status_t trng_SetRunBit5Limit(TRNG_Type *base, uint32_t range, uint32_t limit_maximum)
  1350. {
  1351. status_t status;
  1352. /* Check input parameters*/
  1353. if ((range <= 0x7ffu) && (limit_maximum <= 0x7ffu))
  1354. {
  1355. /* Set TRNG_SCR5L register */
  1356. TRNG_WR_SCR5L_RUN5_MAX(base, limit_maximum);
  1357. TRNG_WR_SCR5L_RUN5_RNG(base, range);
  1358. status = kStatus_Success;
  1359. }
  1360. else
  1361. {
  1362. status = kStatus_InvalidArgument;
  1363. }
  1364. return status;
  1365. }
  1366. /*!
  1367. * @brief Sets statistical Statistical Check Run Length 6 Limit Register .
  1368. * This function set register TRNG_SCR6L - Statistical Check Run Length 6 Limit Register
  1369. */
  1370. static status_t trng_SetRunBit6Limit(TRNG_Type *base, uint32_t range, uint32_t limit_maximum)
  1371. {
  1372. status_t status;
  1373. /* Check input parameters*/
  1374. if ((range <= 0x7ffu) && (limit_maximum <= 0x7ffu))
  1375. {
  1376. /* Set TRNG_SCR6L register */
  1377. TRNG_WR_SCR6PL_RUN6P_MAX(base, limit_maximum);
  1378. TRNG_WR_SCR6PL_RUN6P_RNG(base, range);
  1379. status = kStatus_Success;
  1380. }
  1381. else
  1382. {
  1383. status = kStatus_InvalidArgument;
  1384. }
  1385. return status;
  1386. }
  1387. /*!
  1388. * @brief Sets statistical Poker Maximum Limit Register.
  1389. * This function set register TRNG_PKRMAX - Poker Maximum Limit Register
  1390. */
  1391. static status_t trng_SetPokerMaxLimit(TRNG_Type *base, uint32_t range, uint32_t limit_maximum)
  1392. {
  1393. status_t status;
  1394. /* Check input parameters*/
  1395. if ((range <= 0xffffu) && (limit_maximum <= 0xffffffu))
  1396. {
  1397. /* Set TRNG_PKRMAX register */
  1398. TRNG_WR_PKRMAX_PKR_MAX(base, limit_maximum);
  1399. TRNG_WR_PKRRNG_PKR_RNG(base, range);
  1400. status = kStatus_Success;
  1401. }
  1402. else
  1403. {
  1404. status = kStatus_InvalidArgument;
  1405. }
  1406. return status;
  1407. }
  1408. /*!
  1409. * @brief Sets statistical Frequency Count Maximum Limit Register.
  1410. * This function set register TRNG_FRQMAX - Frequency Count Maximum Limit Register
  1411. */
  1412. static status_t trng_SetFrequencyCountMaxLimit(TRNG_Type *base, uint32_t limit_minimum, uint32_t limit_maximum)
  1413. {
  1414. status_t status;
  1415. /* Check input parameters*/
  1416. if ((limit_minimum <= 0x3fffffu) && (limit_maximum <= 0x3fffffu))
  1417. {
  1418. /* Set FRQMAX register */
  1419. TRNG_WR_FRQMAX_FRQ_MAX(base, limit_maximum);
  1420. TRNG_WR_FRQMIN_FRQ_MIN(base, limit_minimum);
  1421. status = kStatus_Success;
  1422. }
  1423. else
  1424. {
  1425. status = kStatus_InvalidArgument;
  1426. }
  1427. return status;
  1428. }
  1429. /*!
  1430. * @brief Sets statistical check limits.
  1431. *
  1432. * This function is used to set minimum and maximum limits of statistical checks.
  1433. *
  1434. */
  1435. static status_t trng_SetStatisticalCheckLimit(TRNG_Type *base,
  1436. trng_statistical_check_t statistical_check,
  1437. const trng_statistical_check_limit_t *limit)
  1438. {
  1439. uint32_t range;
  1440. status_t status = kStatus_Success;
  1441. if ((NULL != limit) && (limit->maximum > limit->minimum))
  1442. {
  1443. range = limit->maximum - limit->minimum; /* Registers use range instead of minimum value.*/
  1444. if (statistical_check == kTRNG_StatisticalCheckMonobit) /* Allowable maximum and minimum number of ones/zero
  1445. detected during entropy generation. */
  1446. {
  1447. status = trng_SetMonobitLimit(base, range, limit->maximum);
  1448. }
  1449. else if (statistical_check == kTRNG_StatisticalCheckRunBit1) /* Allowable maximum and minimum number of runs of
  1450. length 1 detected during entropy generation. */
  1451. {
  1452. status = trng_SetRunBit1Limit(base, range, limit->maximum);
  1453. }
  1454. else if (statistical_check == kTRNG_StatisticalCheckRunBit2) /* Allowable maximum and minimum number of runs of
  1455. length 2 detected during entropy generation. */
  1456. {
  1457. status = trng_SetRunBit2Limit(base, range, limit->maximum);
  1458. }
  1459. else if (statistical_check == kTRNG_StatisticalCheckRunBit3) /* Allowable maximum and minimum number of runs of
  1460. length 3 detected during entropy generation. */
  1461. {
  1462. status = trng_SetRunBit3Limit(base, range, limit->maximum);
  1463. }
  1464. else if (statistical_check == kTRNG_StatisticalCheckRunBit4) /* Allowable maximum and minimum number of runs of
  1465. length 4 detected during entropy generation. */
  1466. {
  1467. status = trng_SetRunBit4Limit(base, range, limit->maximum);
  1468. }
  1469. else if (statistical_check == kTRNG_StatisticalCheckRunBit5) /* Allowable maximum and minimum number of runs of
  1470. length 5 detected during entropy generation. */
  1471. {
  1472. status = trng_SetRunBit5Limit(base, range, limit->maximum);
  1473. }
  1474. else if (statistical_check == kTRNG_StatisticalCheckRunBit6Plus) /* Allowable maximum and minimum number of
  1475. length 6 or more detected during entropy
  1476. generation */
  1477. {
  1478. status = trng_SetRunBit6Limit(base, range, limit->maximum);
  1479. }
  1480. else if (statistical_check == kTRNG_StatisticalCheckPoker) /* Allowable maximum and minimum limit of "Poker
  1481. Test" detected during entropy generation . */
  1482. {
  1483. status = trng_SetPokerMaxLimit(base, range, limit->maximum);
  1484. }
  1485. else if (statistical_check == kTRNG_StatisticalCheckFrequencyCount) /* Allowable maximum and minimum limit of
  1486. entropy sample frquency count during
  1487. entropy generation . */
  1488. {
  1489. status = trng_SetFrequencyCountMaxLimit(base, limit->minimum, limit->maximum);
  1490. }
  1491. else
  1492. {
  1493. status = kStatus_InvalidArgument;
  1494. }
  1495. }
  1496. return status;
  1497. }
  1498. /*FUNCTION*********************************************************************
  1499. *
  1500. * Function Name : trng_ApplyUserConfig
  1501. * Description : Apply user configuration settings to TRNG module.
  1502. *
  1503. *END*************************************************************************/
  1504. static status_t trng_ApplyUserConfig(TRNG_Type *base, const trng_config_t *userConfig)
  1505. {
  1506. status_t status;
  1507. /* Set retry count */
  1508. status = trng_SetRetryCount(base, userConfig->retryCount);
  1509. /* Set statistical check limit */
  1510. if (kStatus_Success == status)
  1511. {
  1512. status = trng_SetStatisticalCheckLimit(base, kTRNG_StatisticalCheckMonobit, &userConfig->monobitLimit);
  1513. }
  1514. if (kStatus_Success == status)
  1515. {
  1516. status = trng_SetStatisticalCheckLimit(base, kTRNG_StatisticalCheckRunBit1, &userConfig->runBit1Limit);
  1517. }
  1518. if (kStatus_Success == status)
  1519. {
  1520. status = trng_SetStatisticalCheckLimit(base, kTRNG_StatisticalCheckRunBit2, &userConfig->runBit2Limit);
  1521. }
  1522. if (kStatus_Success == status)
  1523. {
  1524. status = trng_SetStatisticalCheckLimit(base, kTRNG_StatisticalCheckRunBit3, &userConfig->runBit3Limit);
  1525. }
  1526. if (kStatus_Success == status)
  1527. {
  1528. status = trng_SetStatisticalCheckLimit(base, kTRNG_StatisticalCheckRunBit4, &userConfig->runBit4Limit);
  1529. }
  1530. if (kStatus_Success == status)
  1531. {
  1532. status = trng_SetStatisticalCheckLimit(base, kTRNG_StatisticalCheckRunBit5, &userConfig->runBit5Limit);
  1533. }
  1534. if (kStatus_Success == status)
  1535. {
  1536. status = trng_SetStatisticalCheckLimit(base, kTRNG_StatisticalCheckRunBit6Plus, &userConfig->runBit6PlusLimit);
  1537. }
  1538. if (kStatus_Success == status)
  1539. {
  1540. status = trng_SetStatisticalCheckLimit(base, kTRNG_StatisticalCheckPoker, &userConfig->pokerLimit);
  1541. }
  1542. if (kStatus_Success == status)
  1543. {
  1544. status =
  1545. trng_SetStatisticalCheckLimit(base, kTRNG_StatisticalCheckFrequencyCount, &userConfig->frequencyCountLimit);
  1546. }
  1547. if (kStatus_Success == status)
  1548. {
  1549. /* Set clock mode used to operate TRNG */
  1550. TRNG_WR_MCTL_FOR_SCLK(base, userConfig->clockMode);
  1551. /* Set ring oscillator divider used by TRNG */
  1552. TRNG_WR_MCTL_OSC_DIV(base, userConfig->ringOscDiv);
  1553. /* Set sample mode of the TRNG ring oscillator. */
  1554. TRNG_WR_MCTL_SAMP_MODE(base, userConfig->sampleMode);
  1555. /* Set length of each Entropy sample taken */
  1556. TRNG_WR_SDCTL_ENT_DLY(base, userConfig->entropyDelay);
  1557. /* Set number of entropy samples that will be taken during Entropy generation */
  1558. TRNG_WR_SDCTL_SAMP_SIZE(base, userConfig->sampleSize);
  1559. /* Set Sparse Bit Limit */
  1560. TRNG_WR_SBLIM_SB_LIM(base, userConfig->sparseBitLimit);
  1561. TRNG_WR_SCMISC_LRUN_MAX(base, userConfig->longRunMaxLimit);
  1562. }
  1563. return status;
  1564. }
  1565. /*!
  1566. * @brief Gets a entry data from the TRNG.
  1567. *
  1568. * This function gets an entropy data from TRNG.
  1569. * Entropy data is spread over TRNG_ENT_COUNT registers.
  1570. * Read register number is defined by index parameter.
  1571. */
  1572. static uint32_t trng_ReadEntropy(TRNG_Type *base, uint32_t index)
  1573. {
  1574. uint32_t data;
  1575. index = index % TRNG_ENT_COUNT; /* This way we can use incremental index without limit control from application.*/
  1576. data = TRNG_RD_ENT(base, index);
  1577. if (index == (TRNG_ENT_COUNT - 1u))
  1578. {
  1579. /* Dummy read. Defect workaround.
  1580. * TRNG could not clear ENT_VAL flag automatically, application
  1581. * had to do a dummy reading operation for anyone TRNG register
  1582. * to clear it firstly, then to read the RTENT0 to RTENT15 again */
  1583. index = TRNG_RD_ENT(base, 0);
  1584. }
  1585. return data;
  1586. }
  1587. /*!
  1588. * brief Initializes the TRNG.
  1589. *
  1590. * This function initializes the TRNG.
  1591. * When called, the TRNG entropy generation starts immediately.
  1592. *
  1593. * param base TRNG base address
  1594. * param userConfig Pointer to the initialization configuration structure.
  1595. * return If successful, returns the kStatus_TRNG_Success. Otherwise, it returns an error.
  1596. */
  1597. status_t TRNG_Init(TRNG_Type *base, const trng_config_t *userConfig)
  1598. {
  1599. status_t result;
  1600. /* Check input parameters.*/
  1601. if ((base != NULL) && (userConfig != NULL))
  1602. {
  1603. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  1604. /* Enable the clock gate. */
  1605. CLOCK_EnableClock(s_trngClock[trng_GetInstance(base)]);
  1606. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  1607. /* Reset the registers of TRNG module to reset state. */
  1608. /* Must be in program mode.*/
  1609. TRNG_WR_MCTL_PRGM(base, kTRNG_WorkModeProgram);
  1610. /* Reset Defaults.*/
  1611. TRNG_WR_MCTL_RST_DEF(base, 1);
  1612. /* Set configuration.*/
  1613. if ((result = trng_ApplyUserConfig(base, userConfig)) == kStatus_Success)
  1614. {
  1615. /* Start entropy generation.*/
  1616. /* Set to Run mode.*/
  1617. TRNG_WR_MCTL_PRGM(base, kTRNG_WorkModeRun);
  1618. #if !(defined(FSL_FEATURE_TRNG_HAS_NO_TRNG_ACC) && (FSL_FEATURE_TRNG_HAS_NO_TRNG_ACC > 0))
  1619. /* Enable TRNG Access Mode. To generate an Entropy
  1620. * value that can be read via the true0-true15 registers.*/
  1621. TRNG_WR_MCTL_TRNG_ACC(base, 1);
  1622. #endif /* !FSL_FEATURE_TRNG_HAS_NO_TRNG_ACC */
  1623. (void)trng_ReadEntropy(base, (TRNG_ENT_COUNT - 1u));
  1624. if (true == userConfig->lock) /* Disable programmability of TRNG registers. */
  1625. {
  1626. TRNG_WR_SEC_CFG_NO_PRGM(base, 1);
  1627. }
  1628. result = kStatus_Success;
  1629. }
  1630. }
  1631. else
  1632. {
  1633. result = kStatus_InvalidArgument;
  1634. }
  1635. return result;
  1636. }
  1637. /*!
  1638. * brief Shuts down the TRNG.
  1639. *
  1640. * This function shuts down the TRNG.
  1641. *
  1642. * param base TRNG base address.
  1643. */
  1644. void TRNG_Deinit(TRNG_Type *base)
  1645. {
  1646. /* Check input parameters.*/
  1647. if (NULL != base)
  1648. {
  1649. /* Move to program mode. Stop entropy generation.*/
  1650. TRNG_WR_MCTL_PRGM(base, kTRNG_WorkModeProgram);
  1651. /* Check before clock stop.
  1652. TRNG turns on the TRNG free-running ring oscillator whenever new entropy
  1653. is being generated and turns off the ring oscillator when entropy generation
  1654. is complete. If the TRNG clock is stopped while the TRNG ring oscillator
  1655. is running, the oscillator continues running though the RNG clock.
  1656. is stopped. */
  1657. while (TRNG_RD_MCTL_TSTOP_OK(base) == 0u)
  1658. {
  1659. }
  1660. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  1661. /* Disable Clock*/
  1662. CLOCK_DisableClock(s_trngClock[trng_GetInstance(base)]);
  1663. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  1664. }
  1665. }
  1666. /*!
  1667. * brief Gets random data.
  1668. *
  1669. * This function gets random data from the TRNG.
  1670. *
  1671. * param base TRNG base address.
  1672. * param data Pointer address used to store random data.
  1673. * param dataSize Size of the buffer pointed by the data parameter.
  1674. * return random data
  1675. */
  1676. status_t TRNG_GetRandomData(TRNG_Type *base, void *data, size_t dataSize)
  1677. {
  1678. status_t result = kStatus_Success;
  1679. uint32_t random_32;
  1680. uint8_t *random_p;
  1681. uint32_t random_size;
  1682. uint8_t *data_p = (uint8_t *)data;
  1683. uint32_t i;
  1684. uint32_t tmpValidFlag;
  1685. uint32_t tmpErrorFlag;
  1686. int index = 0;
  1687. /* Check input parameters.*/
  1688. if ((NULL != base) && (NULL != data) && (0U != dataSize))
  1689. {
  1690. do
  1691. {
  1692. /* Wait for Valid or Error flag*/
  1693. tmpValidFlag = TRNG_RD_MCTL_ENT_VAL(base);
  1694. tmpErrorFlag = TRNG_RD_MCTL_ERR(base);
  1695. while ((tmpValidFlag == 0u) && (tmpErrorFlag == 0u))
  1696. {
  1697. tmpValidFlag = TRNG_RD_MCTL_ENT_VAL(base);
  1698. tmpErrorFlag = TRNG_RD_MCTL_ERR(base);
  1699. }
  1700. /* Check HW error.*/
  1701. if (0U != TRNG_RD_MCTL_ERR(base))
  1702. {
  1703. result = kStatus_Fail; /* TRNG module error occurred */
  1704. /* Clear error.*/
  1705. TRNG_WR_MCTL_ERR(base, 1);
  1706. break; /* No sense stay here.*/
  1707. }
  1708. /* Read Entropy.*/
  1709. random_32 = trng_ReadEntropy(base, (uint32_t)index++);
  1710. random_p = (uint8_t *)&random_32;
  1711. if (dataSize < sizeof(random_32))
  1712. {
  1713. random_size = dataSize;
  1714. }
  1715. else
  1716. {
  1717. random_size = sizeof(random_32);
  1718. }
  1719. for (i = 0U; i < random_size; i++)
  1720. {
  1721. *data_p++ = *random_p++;
  1722. }
  1723. dataSize -= random_size;
  1724. } while (dataSize > 0u);
  1725. /* Start a new entropy generation.
  1726. It is done by reading of the last entropy register.*/
  1727. if (((unsigned)index % TRNG_ENT_COUNT) != (TRNG_ENT_COUNT - 1u))
  1728. {
  1729. (void)trng_ReadEntropy(base, (TRNG_ENT_COUNT - 1u));
  1730. }
  1731. }
  1732. else
  1733. {
  1734. result = kStatus_InvalidArgument;
  1735. }
  1736. return result;
  1737. }
  1738. #endif /* FSL_FEATURE_SOC_TRNG_COUNT */