1 /******************************************************************************
2 *  Filename:       trng.h
3 *
4 *  Description:    Defines and prototypes for the true random number gen.
5 *
6 *  Copyright (c) 2015 - 2022, Texas Instruments Incorporated
7 *  All rights reserved.
8 *
9 *  Redistribution and use in source and binary forms, with or without
10 *  modification, are permitted provided that the following conditions are met:
11 *
12 *  1) Redistributions of source code must retain the above copyright notice,
13 *     this list of conditions and the following disclaimer.
14 *
15 *  2) Redistributions in binary form must reproduce the above copyright notice,
16 *     this list of conditions and the following disclaimer in the documentation
17 *     and/or other materials provided with the distribution.
18 *
19 *  3) Neither the name of the ORGANIZATION nor the names of its contributors may
20 *     be used to endorse or promote products derived from this software without
21 *     specific prior written permission.
22 *
23 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24 *  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 *  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
27 *  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29 *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30 *  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31 *  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32 *  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 *  POSSIBILITY OF SUCH DAMAGE.
34 *
35 ******************************************************************************/
36 
37 //*****************************************************************************
38 //
39 //! \addtogroup peripheral_group
40 //! @{
41 //! \addtogroup trng_api
42 //! @{
43 //
44 //*****************************************************************************
45 
46 #ifndef __TRNG_H__
47 #define __TRNG_H__
48 
49 //*****************************************************************************
50 //
51 // If building with a C++ compiler, make all of the definitions in this header
52 // have a C binding.
53 //
54 //*****************************************************************************
55 #ifdef __cplusplus
56 extern "C"
57 {
58 #endif
59 
60 #include <stdbool.h>
61 #include <stdint.h>
62 #include "../inc/hw_types.h"
63 #include "../inc/hw_trng.h"
64 #include "../inc/hw_memmap.h"
65 #include "../inc/hw_ints.h"
66 #include "debug.h"
67 #include "interrupt.h"
68 #include "cpu.h"
69 
70 //*****************************************************************************
71 //
72 // Support for DriverLib in ROM:
73 // This section renames all functions that are not "static inline", so that
74 // calling these functions will default to implementation in flash. At the end
75 // of this file a second renaming will change the defaults to implementation in
76 // ROM for available functions.
77 //
78 // To force use of the implementation in flash, e.g. for debugging:
79 // - Globally: Define DRIVERLIB_NOROM at project level
80 // - Per function: Use prefix "NOROM_" when calling the function
81 //
82 //*****************************************************************************
83 #if !defined(DOXYGEN)
84     #define TRNGConfigure                   NOROM_TRNGConfigure
85     #define TRNGNumberGet                   NOROM_TRNGNumberGet
86 #endif
87 
88 //*****************************************************************************
89 //
90 //
91 //
92 //*****************************************************************************
93 #define TRNG_NUMBER_READY       0x00000001  //
94 #define TRNG_FRO_SHUTDOWN       0x00000002  //
95 #define TRNG_NEED_CLOCK         0x80000000  //
96 
97 #define TRNG_HI_WORD            0x00000001
98 #define TRNG_LOW_WORD           0x00000002
99 
100 //*****************************************************************************
101 //
102 // API Function and prototypes
103 //
104 //*****************************************************************************
105 
106 //*****************************************************************************
107 //
108 //! \brief Configure the true random number generator.
109 //!
110 //! Use this function to set the minimum and maximum number of samples required
111 //! in each generation of a new random number.
112 //!
113 //! \param ui32MinSamplesPerCycle is the minimum number of samples per each
114 //! generated random number. Constraints:
115 //! - Value must be bigger than or equal to 2^6 and less than 2^14.
116 //! - The 6 LSBs of the argument are truncated.
117 //! - If the value is zero, the number of samples is fixed to the value determined
118 //!   by ui32MaxSamplesPerCycle. To ensure same entropy in all generated random
119 //!   numbers the value 0 should be used.
120 //! \param ui32MaxSamplesPerCycle is the maximum number of samples per each
121 //! generated random number. Constraints:
122 //! - Value must be between 2^8 and 2^24 (both included).
123 //! - The 8 LSBs of the argument are truncated.
124 //! - Value 0 and 2^24 both give the highest possible value.
125 //! \param ui32ClocksPerSample is the number of clock cycles for each time
126 //! a new sample is generated from the FROs.
127 //! - 0  : Every sample.
128 //! - 1  : Every second sample.
129 //! - ...
130 //! - 15 : Every 16. sample.
131 //!
132 //! \return None
133 //
134 //*****************************************************************************
135 extern void TRNGConfigure(uint32_t ui32MinSamplesPerCycle,
136                           uint32_t ui32MaxSamplesPerCycle,
137                           uint32_t ui32ClocksPerSample);
138 
139 //*****************************************************************************
140 //
141 //! \brief Enable the TRNG.
142 //!
143 //! Enable the TRNG to start preparing a random number.
144 //!
145 //! \return None
146 //
147 //*****************************************************************************
148 __STATIC_INLINE void
TRNGEnable(void)149 TRNGEnable(void)
150 {
151     // Enable the TRNG.
152     HWREGBITW(TRNG_BASE + TRNG_O_CTL, TRNG_CTL_TRNG_EN_BITN) = 1;
153 }
154 
155 //*****************************************************************************
156 //
157 //! \brief Disable the TRNG module.
158 //!
159 //! \return None
160 //
161 //*****************************************************************************
162 __STATIC_INLINE void
TRNGDisable(void)163 TRNGDisable(void)
164 {
165     // Enable the TRNG
166     HWREGBITW(TRNG_BASE + TRNG_O_CTL, TRNG_CTL_TRNG_EN_BITN) = 0;
167 }
168 
169 //*****************************************************************************
170 //
171 //! \brief Get a random number from the generator.
172 //!
173 //! Use this function to get either the high or low part of the 64 bit
174 //! generated number.
175 //!
176 //! \note Data from this register is only valid if the TRNG has produced a
177 //! number. Use \ref TRNGStatusGet() to poll the for status. After calling this
178 //! function a new random number will be generated.
179 //!
180 //! \param ui32Word determines if whether to return the high or low 32 bits.
181 //! - \ref TRNG_HI_WORD
182 //! - \ref TRNG_LOW_WORD
183 //!
184 //! \return Return either the high or low part of the 64 bit generated random
185 //! number.
186 //
187 //*****************************************************************************
188 extern uint32_t TRNGNumberGet(uint32_t ui32Word);
189 
190 //*****************************************************************************
191 //
192 //! \brief Get the status of the TRNG.
193 //!
194 //! Use this function to retrieve the status of the TRNG.
195 //!
196 //! \return Returns the current status of the TRNG module.
197 //! The returned status is a bitwise OR'ed combination of:
198 //! - \ref TRNG_NUMBER_READY
199 //! - \ref TRNG_FRO_SHUTDOWN
200 //! - \ref TRNG_NEED_CLOCK
201 //
202 //*****************************************************************************
203 __STATIC_INLINE uint32_t
TRNGStatusGet(void)204 TRNGStatusGet(void)
205 {
206     // Return the status.
207     return (HWREG(TRNG_BASE + TRNG_O_IRQFLAGSTAT));
208 }
209 
210 //*****************************************************************************
211 //
212 //! \brief Reset the TRNG.
213 //!
214 //! Use this function to reset the TRNG module. Reset will be low for
215 //! approximately 5 clock cycles.
216 //!
217 //! \return None
218 //
219 //*****************************************************************************
220 __STATIC_INLINE void
TRNGReset(void)221 TRNGReset(void)
222 {
223     // Reset the TRNG.
224     HWREG(TRNG_BASE + TRNG_O_SWRESET) = 1;
225 }
226 
227 //*****************************************************************************
228 //
229 //! \brief Enables individual TRNG interrupt sources.
230 //!
231 //! This function enables the indicated TRNG interrupt sources. Only the
232 //! sources that are enabled can be reflected to the processor interrupt;
233 //! disabled sources have no effect on the processor.
234 //!
235 //! \param ui32IntFlags is the bit mask of the interrupt sources to be enabled.
236 //! The parameter is the bitwise OR of any of the following:
237 //! - \ref TRNG_NUMBER_READY
238 //! - \ref TRNG_FRO_SHUTDOWN
239 //!
240 //! \return None
241 //
242 //*****************************************************************************
243 __STATIC_INLINE void
TRNGIntEnable(uint32_t ui32IntFlags)244 TRNGIntEnable(uint32_t ui32IntFlags)
245 {
246     // Check the arguments.
247     ASSERT((ui32IntFlags & TRNG_NUMBER_READY) ||
248            (ui32IntFlags & TRNG_FRO_SHUTDOWN));
249 
250     // Enable the specified interrupts.
251     HWREG(TRNG_BASE + TRNG_O_IRQFLAGMASK) |= ui32IntFlags;
252 }
253 
254 //*****************************************************************************
255 //
256 //! \brief Disables individual TRNG interrupt sources.
257 //!
258 //! This function disables the indicated TRNG interrupt sources. Only the
259 //! sources that are enabled can be reflected to the processor interrupt;
260 //! disabled sources have no effect on the processor.
261 //!
262 //! \param ui32IntFlags is the bit mask of the interrupt sources to be disabled.
263 //! The parameter is the bitwise OR of any of the following:
264 //! - \ref TRNG_NUMBER_READY
265 //! - \ref TRNG_FRO_SHUTDOWN
266 //!
267 //! \return None
268 //
269 //*****************************************************************************
270 __STATIC_INLINE void
TRNGIntDisable(uint32_t ui32IntFlags)271 TRNGIntDisable(uint32_t ui32IntFlags)
272 {
273     // Check the arguments.
274     ASSERT((ui32IntFlags & TRNG_NUMBER_READY) ||
275            (ui32IntFlags & TRNG_FRO_SHUTDOWN));
276 
277     // Disable the specified interrupts.
278     HWREG(TRNG_BASE + TRNG_O_IRQFLAGMASK) &= ~ui32IntFlags;
279 }
280 
281 //*****************************************************************************
282 //
283 //! \brief Gets the current interrupt status of the TRNG module.
284 //!
285 //! This function returns the interrupt status for the specified TRNG. Either
286 //! the raw interrupt status or the status of interrupts that are allowed to
287 //! reflect to the processor can be returned.
288 //!
289 //! \param bMasked selects either raw or masked interrupt status.
290 //! - \c true  : Masked interrupt.
291 //! - \c false : Raw interrupt.
292 //!
293 //! \return Returns the current interrupt status, enumerated as:
294 //! - \ref TRNG_NUMBER_READY
295 //! - \ref TRNG_FRO_SHUTDOWN
296 //
297 //*****************************************************************************
298 __STATIC_INLINE uint32_t
TRNGIntStatus(bool bMasked)299 TRNGIntStatus(bool bMasked)
300 {
301     uint32_t ui32Mask;
302 
303     // Return either the interrupt status or the raw interrupt status as
304     // requested.
305     if(bMasked)
306     {
307         ui32Mask = HWREG(TRNG_BASE + TRNG_O_IRQFLAGMASK);
308         return(ui32Mask & HWREG(TRNG_BASE + TRNG_O_IRQFLAGSTAT));
309     }
310     else
311     {
312         return(HWREG(TRNG_BASE + TRNG_O_IRQFLAGSTAT) & 0x00000003);
313     }
314 }
315 
316 //*****************************************************************************
317 //
318 //! \brief Clears TRNG interrupt sources.
319 //!
320 //! The specified TRNG interrupt sources are cleared, so that they no longer
321 //! assert. This function must be called in the interrupt handler to keep the
322 //! interrupt from being recognized again immediately upon exit.
323 //!
324 //! \note Due to write buffers and synchronizers in the system it may take several
325 //! clock cycles from a register write clearing an event in a module and until the
326 //! event is actually cleared in the NVIC of the system CPU. It is recommended to
327 //! clear the event source early in the interrupt service routine (ISR) to allow
328 //! the event clear to propagate to the NVIC before returning from the ISR.
329 //! At the same time, an early event clear allows new events of the same type to be
330 //! pended instead of ignored if the event is cleared later in the ISR.
331 //! It is the responsibility of the programmer to make sure that enough time has passed
332 //! before returning from the ISR to avoid false re-triggering of the cleared event.
333 //! A simple, although not necessarily optimal, way of clearing an event before
334 //! returning from the ISR is:
335 //! -# Write to clear event (interrupt source). (buffered write)
336 //! -# Dummy read from the event source module. (making sure the write has propagated)
337 //! -# Wait two system CPU clock cycles (user code or two NOPs). (allowing cleared event to propagate through any synchronizers)
338 //!
339 //! \param ui32IntFlags is a bit mask of the interrupt sources to be cleared.
340 //! The parameter is the bitwise OR of any of the following:
341 //! - \ref TRNG_NUMBER_READY
342 //! - \ref TRNG_FRO_SHUTDOWN
343 //!
344 //! \return None
345 //
346 //*****************************************************************************
347 __STATIC_INLINE void
TRNGIntClear(uint32_t ui32IntFlags)348 TRNGIntClear(uint32_t ui32IntFlags)
349 {
350     // Check the arguments.
351     ASSERT((ui32IntFlags & TRNG_NUMBER_READY) ||
352            (ui32IntFlags & TRNG_FRO_SHUTDOWN));
353 
354     // Clear the requested interrupt sources.
355     HWREG(TRNG_BASE + TRNG_O_IRQFLAGCLR) = ui32IntFlags;
356 }
357 
358 //*****************************************************************************
359 //
360 //! \brief Registers an interrupt handler for a TRNG interrupt in the dynamic interrupt table.
361 //!
362 //! \note Only use this function if you want to use the dynamic vector table (in SRAM)!
363 //!
364 //! This function registers a function as the interrupt handler for a specific
365 //! interrupt and enables the corresponding interrupt in the interrupt controller.
366 //!
367 //! Specific TRNG interrupts must be enabled via \ref TRNGIntEnable(). It is the interrupt
368 //! handler's responsibility to clear the interrupt source.
369 //!
370 //! \param pfnHandler is a pointer to the function to be called when the
371 //! TRNG interrupt occurs.
372 //!
373 //! \return None
374 //!
375 //! \sa \ref IntRegister() for important information about registering interrupt
376 //! handlers.
377 //
378 //*****************************************************************************
379 __STATIC_INLINE void
TRNGIntRegister(void (* pfnHandler)(void))380 TRNGIntRegister(void (*pfnHandler)(void))
381 {
382     // Register the interrupt handler.
383     IntRegister(INT_TRNG_IRQ, pfnHandler);
384 
385     // Enable the TRNG interrupt.
386     IntEnable(INT_TRNG_IRQ);
387 }
388 
389 //*****************************************************************************
390 //
391 //! \brief Unregisters an interrupt handler for a TRNG interrupt in the dynamic interrupt table.
392 //!
393 //! This function does the actual unregistering of the interrupt handler. It
394 //! clears the handler to be called when a Crypto interrupt occurs. This
395 //! function also masks off the interrupt in the interrupt controller so that
396 //! the interrupt handler no longer is called.
397 //!
398 //! \return None
399 //!
400 //! \sa \ref IntRegister() for important information about registering interrupt
401 //! handlers.
402 //
403 //*****************************************************************************
404 __STATIC_INLINE void
TRNGIntUnregister(void)405 TRNGIntUnregister(void)
406 {
407     // Disable the interrupt.
408     IntDisable(INT_TRNG_IRQ);
409 
410     // Unregister the interrupt handler.
411     IntUnregister(INT_TRNG_IRQ);
412 }
413 
414 //*****************************************************************************
415 //
416 // Support for DriverLib in ROM:
417 // Redirect to implementation in ROM when available.
418 //
419 //*****************************************************************************
420 #if !defined(DRIVERLIB_NOROM) && !defined(DOXYGEN)
421     #include "../driverlib/rom.h"
422     #ifdef ROM_TRNGConfigure
423         #undef  TRNGConfigure
424         #define TRNGConfigure                   ROM_TRNGConfigure
425     #endif
426     #ifdef ROM_TRNGNumberGet
427         #undef  TRNGNumberGet
428         #define TRNGNumberGet                   ROM_TRNGNumberGet
429     #endif
430 #endif
431 
432 //*****************************************************************************
433 //
434 // Mark the end of the C bindings section for C++ compilers.
435 //
436 //*****************************************************************************
437 #ifdef __cplusplus
438 }
439 #endif
440 
441 #endif  // __TRNG_H__
442 
443 //*****************************************************************************
444 //
445 //! Close the Doxygen group.
446 //! @}
447 //! @}
448 //
449 //*****************************************************************************
450