1 /******************************************************************************
2 * Filename: trng.c
3 *
4 * Description: Driver for the TRNG module
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 #include "trng.h"
38
39 //*****************************************************************************
40 //
41 // Handle support for DriverLib in ROM:
42 // This section will undo prototype renaming made in the header file
43 //
44 //*****************************************************************************
45 #if !defined(DOXYGEN)
46 #undef TRNGConfigure
47 #define TRNGConfigure NOROM_TRNGConfigure
48 #undef TRNGNumberGet
49 #define TRNGNumberGet NOROM_TRNGNumberGet
50 #endif
51
52 //*****************************************************************************
53 //
54 // Configure the true random number generator
55 //
56 //*****************************************************************************
57 void
TRNGConfigure(uint32_t ui32MinSamplesPerCycle,uint32_t ui32MaxSamplesPerCycle,uint32_t ui32ClocksPerSample)58 TRNGConfigure(uint32_t ui32MinSamplesPerCycle,
59 uint32_t ui32MaxSamplesPerCycle,
60 uint32_t ui32ClocksPerSample)
61 {
62 uint32_t ui32Val;
63
64 // Make sure the TRNG is disabled.
65 ui32Val = HWREG(TRNG_BASE + TRNG_O_CTL) & ~TRNG_CTL_TRNG_EN;
66 HWREG(TRNG_BASE + TRNG_O_CTL) = ui32Val;
67
68 // Configure the startup number of samples.
69 ui32Val &= ~TRNG_CTL_STARTUP_CYCLES_M;
70 ui32Val |= ((( ui32MaxSamplesPerCycle >> 8 ) << TRNG_CTL_STARTUP_CYCLES_S ) & TRNG_CTL_STARTUP_CYCLES_M );
71 HWREG(TRNG_BASE + TRNG_O_CTL) = ui32Val;
72
73 // Configure the minimum and maximum number of samples pr generated number
74 // and the number of clocks per sample.
75 HWREG(TRNG_BASE + TRNG_O_CFG0) = (
76 ((( ui32MaxSamplesPerCycle >> 8 ) << TRNG_CFG0_MAX_REFILL_CYCLES_S ) & TRNG_CFG0_MAX_REFILL_CYCLES_M ) |
77 ((( ui32ClocksPerSample ) << TRNG_CFG0_SMPL_DIV_S ) & TRNG_CFG0_SMPL_DIV_M ) |
78 ((( ui32MinSamplesPerCycle >> 6 ) << TRNG_CFG0_MIN_REFILL_CYCLES_S ) & TRNG_CFG0_MIN_REFILL_CYCLES_M ) );
79 }
80
81 //*****************************************************************************
82 //
83 // Get a random number from the generator
84 //
85 //*****************************************************************************
86 uint32_t
TRNGNumberGet(uint32_t ui32Word)87 TRNGNumberGet(uint32_t ui32Word)
88 {
89 uint32_t ui32RandomNumber;
90
91 // Check the arguments.
92 ASSERT((ui32Word == TRNG_HI_WORD) ||
93 (ui32Word == TRNG_LOW_WORD));
94
95 // Return the right requested part of the generated number.
96 if(ui32Word == TRNG_HI_WORD)
97 {
98 ui32RandomNumber = HWREG(TRNG_BASE + TRNG_O_OUT1);
99 }
100 else
101 {
102 ui32RandomNumber = HWREG(TRNG_BASE + TRNG_O_OUT0);
103 }
104
105 // Initiate generation of new number.
106 HWREG(TRNG_BASE + TRNG_O_IRQFLAGCLR) = 0x1;
107
108 // Return the random number.
109 return ui32RandomNumber;
110 }
111