1 /**
2  * \file
3  *
4  * \brief SAM True Random Number Generator (TRNG) Driver
5  *
6  * Copyright (C) 2014-2016 Atmel Corporation. All rights reserved.
7  *
8  * \asf_license_start
9  *
10  * \page License
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions are met:
14  *
15  * 1. Redistributions of source code must retain the above copyright notice,
16  *    this list of conditions and the following disclaimer.
17  *
18  * 2. Redistributions in binary form must reproduce the above copyright notice,
19  *    this list of conditions and the following disclaimer in the documentation
20  *    and/or other materials provided with the distribution.
21  *
22  * 3. The name of Atmel may not be used to endorse or promote products derived
23  *    from this software without specific prior written permission.
24  *
25  * 4. This software may only be redistributed and used in connection with an
26  *    Atmel microcontroller product.
27  *
28  * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
29  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
30  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
31  * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
32  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
36  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
37  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38  * POSSIBILITY OF SUCH DAMAGE.
39  *
40  * \asf_license_stop
41  *
42  */
43 /*
44  * Support and FAQ: visit <a href="http://www.atmel.com/design-support/">Atmel Support</a>
45  */
46 
47 #ifndef TRNG_H_INCLUDED
48 #define TRNG_H_INCLUDED
49 
50 /**
51  * \defgroup asfdoc_sam0_trng_group SAM True Random Number Generator (TRNG) Driver
52  *
53  * This driver for Atmel&reg; | SMART ARM&reg;-based microcontrollers provides an interface for the configuration
54  * and management of the device's True Random Number Generator functionality.
55  *
56  * The following driver API modes are covered by this manual:
57  * - Polled APIs
58  * \if TRNG_CALLBACK_MODE
59  * - Callback APIs
60  * \endif
61  *
62  * The following peripheral is used by this module:
63  *  - TRNG (True Random Number Generator)
64  *
65  * The following devices can use this module:
66  *  - Atmel | SMART SAM L21/L22
67  *
68  * The outline of this documentation is as follows:
69  *  - \ref asfdoc_sam0_trng_prerequisites
70  *  - \ref asfdoc_sam0_trng_module_overview
71  *  - \ref asfdoc_sam0_trng_special_considerations
72  *  - \ref asfdoc_sam0_trng_extra_info
73  *  - \ref asfdoc_sam0_trng_examples
74  *  - \ref asfdoc_sam0_trng_api_overview
75  *
76  *
77  * \section asfdoc_sam0_trng_prerequisites Prerequisites
78  *
79  * There are no prerequisites for this module.
80  *
81  *
82  * \section asfdoc_sam0_trng_module_overview Module Overview
83  *
84  * This driver provides an interface for the TRNG functions on the device.
85  *
86  * As soon as the TRNG is enabled, the module provides a new 32-bit random
87  * data, for every 84 CLK_TRNG_APB clock cycles.
88  *
89  *
90  * \section asfdoc_sam0_trng_special_considerations Special Considerations
91  *
92  * There are no special considerations for this module.
93  *
94  *
95  * \section asfdoc_sam0_trng_extra_info Extra Information
96  *
97  * For extra information, see \ref asfdoc_sam0_trng_extra. This includes:
98  *  - \ref asfdoc_sam0_trng_extra_acronyms
99  *  - \ref asfdoc_sam0_trng_extra_dependencies
100  *  - \ref asfdoc_sam0_trng_extra_errata
101  *  - \ref asfdoc_sam0_trng_extra_history
102  *
103  *
104  * \section asfdoc_sam0_trng_examples Examples
105  *
106  * For a list of examples related to this driver, see
107  * \ref asfdoc_sam0_trng_exqsg.
108  *
109  *
110  * \section asfdoc_sam0_trng_api_overview API Overview
111  * @{
112  */
113 
114 #include <compiler.h>
115 #include <system.h>
116 
117 #ifdef __cplusplus
118 extern "C" {
119 #endif
120 
121 #if TRNG_CALLBACK_MODE == true
122 /* Forward declaration of struct */
123 struct trng_module;
124 
125 extern struct trng_module *_trng_instance;
126 
127 /** Type definition for a TRNG module callback function. */
128 typedef void (*trng_callback_t)(struct trng_module *const module_inst);
129 
130 /** Enum for possible callback types for the TRNG module. */
131 enum trng_callback {
132 	/** Callback for specific number of random data ready */
133 	TRNG_CALLBACK_READ_BUFFER = 0,
134 	/** Number of available callbacks */
135 #if !defined(__DOXYGEN__)
136 	TRNG_CALLBACK_N,
137 #endif
138 };
139 #endif
140 
141 /**
142  * \brief TRNG software device instance structure.
143  *
144  * TRNG software instance structure, used to retain software state information
145  * of an associated hardware module instance.
146  *
147  * \note The fields of this structure should not be altered by the user
148  *       application; they are reserved for module-internal use only.
149  */
150 struct trng_module {
151 #if !defined(__DOXYGEN__)
152 	/** Hardware module pointer of the associated TRNG peripheral */
153 	Trng *hw;
154 #  if TRNG_CALLBACK_MODE == true
155 	/** Array of callbacks */
156 	trng_callback_t callback[TRNG_CALLBACK_N];
157 	/** Bit mask for callbacks registered */
158 	uint8_t register_callback_mask;
159 	/** Bit mask for callbacks enabled */
160 	uint8_t enable_callback_mask;
161 	/** Holds the status of the ongoing or last read job */
162 	volatile enum status_code job_status;
163 	/** Pointer to buffer used for TRNG results */
164 	volatile uint32_t *job_buffer;
165 	/** Remaining number of TRNG results in current job */
166 	volatile uint32_t remaining_number;
167 #  endif
168 #endif
169 };
170 
171 /**
172  * \brief TRNG module configuration structure.
173  *
174  *  Configuration structure for a True Random Number Generator.
175  */
176 struct trng_config {
177 	/** If \c true, the True Random Number Generator will not be stopped in
178 	 *  standby sleep mode */
179 	bool run_in_standby;
180 };
181 
182 /**
183  * \brief TRNG event enable/disable structure.
184  *
185  * Event flags for the TRNG module. This is used to enable and
186  * disable events via \ref trng_enable_events() and \ref trng_disable_events().
187  */
188 struct trng_events {
189 	/** Enable event generation on random data ready */
190 	bool generate_event_on_data_ready;
191 };
192 
193 
194 /**
195  * \name Driver Initialization and Configuration
196  * @{
197  */
198 enum status_code trng_init(
199 		struct trng_module *const module_inst,
200 		Trng *const hw,
201 		struct trng_config *const config);
202 
203 /**
204  * \brief Initializes all members of a TRNG configuration structure
205  *  to safe defaults.
206  *
207  *  Initializes all members of a given True Random Number Generator configuration
208  *  structure to safe known default values. This function should be called on
209  *  all new instances of these configuration structures before being modified
210  *  by the user application.
211  *
212  *  The default configuration is as follows:
213  *   \li True Random Number Generator will not be stopped in standby sleep mode
214  *
215  *  \param[out] config  Configuration structure to initialize to default values
216  */
trng_get_config_defaults(struct trng_config * const config)217 static inline void trng_get_config_defaults(
218 		struct trng_config *const config)
219 {
220 	/* Sanity check arguments */
221 	Assert(config);
222 
223 	/* Default configuration values */
224 	config->run_in_standby = false;
225 }
226 
227 /**
228  * \brief Enables a TRNG that was previously configured.
229  *
230  * Enables True Random Number Generator that was previously configured via a
231  * call to \ref trng_init().
232  *
233  * \param[in] module_inst  Software instance for the True Random Number
234  *                         Generator peripheral
235  */
trng_enable(struct trng_module * const module_inst)236 static inline void trng_enable(
237 		struct trng_module *const module_inst)
238 {
239 	/* Sanity check arguments */
240 	Assert(module_inst);
241 	Assert(module_inst->hw);
242 
243 	Trng *const trng_module = module_inst->hw;
244 
245 	/* Enable TRNG */
246 	trng_module->CTRLA.reg |= TRNG_CTRLA_ENABLE;
247 }
248 
249 /**
250  * \brief Disables a TRNG that was previously enabled.
251  *
252  * Disables True Random Number Generator that was previously started via a call
253  * to \ref trng_enable().
254  *
255  * \param[in] module_inst  Software instance for the True Random Number
256  *                         Generator peripheral
257  */
trng_disable(struct trng_module * const module_inst)258 static inline void trng_disable(
259 		struct trng_module *const module_inst)
260 {
261 	/* Sanity check arguments */
262 	Assert(module_inst);
263 	Assert(module_inst->hw);
264 
265 	Trng *const trng_module = module_inst->hw;
266 
267 	/* Disbale interrupt */
268 	trng_module->INTENCLR.reg = TRNG_INTENCLR_MASK;
269 	/* Clear interrupt flag */
270 	trng_module->INTFLAG.reg = TRNG_INTFLAG_MASK;
271 
272 	/* Disable TRNG */
273 	trng_module->CTRLA.reg &= ~TRNG_CTRLA_ENABLE;
274 }
275 
276 /**
277  * \brief Enables a TRNG event output.
278  *
279  *  Enables output events from the True Random Number Generator
280  *  module. See \ref Section Struct trng_events for a list of events this module
281  *  supports.
282  *
283  *  \note Events cannot be altered while the module is enabled.
284  *
285  *  \param[in] module_inst  Software instance for the TRNG peripheral
286  *  \param[in] events       Struct containing flags of events to enable
287  */
trng_enable_events(struct trng_module * const module_inst,struct trng_events * const events)288 static inline void trng_enable_events(
289 		struct trng_module *const module_inst,
290 		struct trng_events *const events)
291 {
292 	/* Sanity check arguments */
293 	Assert(module_inst);
294 	Assert(module_inst->hw);
295 
296 	Trng *const trng_module = module_inst->hw;
297 
298 	if (events->generate_event_on_data_ready) {
299 		/* Enable data ready event output */
300 		trng_module->EVCTRL.reg |= TRNG_EVCTRL_DATARDYEO;
301 	}
302 }
303 
304 /**
305  * \brief Disables a TRNG event output.
306  *
307  *  Disables output events from the True Random Number Generator
308  *  module. See \ref Section Struct trng_events for a list of events
309  *  this module supports.
310  *
311  *  \note Events cannot be altered while the module is enabled.
312  *
313  *  \param[in] module_inst  Software instance for the TRNG peripheral
314  *  \param[in] events       Struct containing flags of events to disable
315  */
trng_disable_events(struct trng_module * const module_inst,struct trng_events * const events)316 static inline void trng_disable_events(
317 		struct trng_module *const module_inst,
318 		struct trng_events *const events)
319 {
320 	/* Sanity check arguments */
321 	Assert(module_inst);
322 	Assert(module_inst->hw);
323 
324 	Trng *const trng_module = module_inst->hw;
325 
326 	if (events->generate_event_on_data_ready) {
327 		/* Disable data ready event output */
328 		trng_module->EVCTRL.reg &= ~TRNG_EVCTRL_DATARDYEO;
329 	}
330 }
331 /** @} */
332 
333 /**
334  * \name Read TRNG Result
335  * @{
336  */
337 
338 /**
339  * \brief Read the random data result
340  *
341  * Reads the random data result.
342  *
343  * \param[in]  module_inst  Pointer to the TRNG software instance struct
344  * \param[out] result       Pointer to store the result value in
345  *
346  * \return Status of the TRNG read request.
347  * \retval STATUS_OK           The result was retrieved successfully
348  * \retval STATUS_BUSY         A random result was not ready
349  */
trng_read(struct trng_module * const module_inst,uint32_t * result)350 static inline enum status_code trng_read(
351 		struct trng_module *const module_inst,
352 		uint32_t *result)
353 {
354 	/* Sanity check arguments */
355 	Assert(module_inst);
356 	Assert(module_inst->hw);
357 	Assert(result);
358 
359 	Trng *const trng_hw = module_inst->hw;
360 
361 	if (!(trng_hw->INTFLAG.reg & TRNG_INTFLAG_DATARDY)) {
362 		/* Result not ready */
363 		return STATUS_BUSY;
364 	}
365 
366 	/* Get randomly generated output data (it will clear data ready flag) */
367 	*result = trng_hw->DATA.reg;
368 
369 	return STATUS_OK;
370 }
371 /** @} */
372 
373 #ifdef __cplusplus
374 }
375 #endif
376 
377 /** @} */
378 
379 /**
380  * \page asfdoc_sam0_trng_extra Extra Information for TRNG Driver
381  *
382  * \section asfdoc_sam0_trng_extra_acronyms Acronyms
383  *
384  * <table>
385  *	<tr>
386  *		<th>Acronym</th>
387  *		<th>Description</th>
388  *	</tr>
389  *  <tr>
390  *		<td>TRNG</td>
391  *		<td>True Random Number Generator</td>
392  *	</tr>
393  * </table>
394  *
395  *
396  * \section asfdoc_sam0_trng_extra_dependencies Dependencies
397  * This driver has no dependencies.
398  *
399  *
400  * \section asfdoc_sam0_trng_extra_errata Errata
401  * There are no errata related to this driver.
402  *
403  *
404  * \section asfdoc_sam0_trng_extra_history Module History
405  * An overview of the module history is presented in the table below, with
406  * details on the enhancements and fixes made to the module since its first
407  * release. The current version of this corresponds to the newest version in
408  * the table.
409  *
410  * <table>
411  *	<tr>
412  *		<th>Changelog</th>
413  *	</tr>
414   *	<tr>
415  *		<td>Initial Release</td>
416  *	</tr>
417  * </table>
418  */
419 
420 /**
421  * \page asfdoc_sam0_trng_exqsg Examples for TRNG Driver
422  *
423  * This is a list of the available Quick Start guides (QSGs) and example
424  * applications for \ref asfdoc_sam0_trng_group. QSGs are simple examples with
425  * step-by-step instructions to configure and use this driver in a selection of
426  * use cases. Note that QSGs can be compiled as a standalone application or be
427  * added to the user application.
428  *
429  *  - \subpage asfdoc_sam0_trng_basic_use_case
430  * \if TRNG_CALLBACK_MODE
431  *  - \subpage asfdoc_sam0_trng_basic_use_case_callback
432  * \endif
433  *
434  * \page asfdoc_sam0_trng_document_revision_history Document Revision History
435  *
436  * <table>
437  *	<tr>
438  *		<th>Doc. Rev.</td>
439  *		<th>Date</td>
440  *		<th>Comments</td>
441  *	</tr>
442  *	<tr>
443  *		<td>42444B</td>
444  *		<td>01/2016</td>
445  *		<td>Added support for SAM L22</td>
446  *	</tr>
447  *	<tr>
448  *		<td>42444A</td>
449  *		<td>06/2015</td>
450  *		<td>Initial document release</td>
451  *	</tr>
452  * </table>
453  */
454 
455 #endif /* TRNG_H_INCLUDED */
456 
457