fsl_trng.c 79 KB

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