fsl_trng.c 70 KB

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