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 #include "trng_callback.h"
48 #include "system_interrupt.h"
49 
50 struct trng_module *_trng_instance;
51 
52 /**
53  * \brief Registers a callback
54  *
55  * Registers a callback function which is implemented by the user.
56  *
57  * \note The callback must be enabled by \ref trng_enable_callback,
58  * in order for the interrupt handler to call it when the conditions for the
59  * callback type is met.
60  *
61  * \param[in]     module        Pointer to TC software instance struct
62  * \param[in]     callback_func Pointer to callback function
63  * \param[in]     callback_type Callback type given by an enum
64  *
65  * \retval STATUS_OK  The function exited successfully
66  */
trng_register_callback(struct trng_module * const module,trng_callback_t callback_func,const enum trng_callback callback_type)67 enum status_code trng_register_callback(
68 		struct trng_module *const module,
69 		trng_callback_t callback_func,
70 		const enum trng_callback callback_type)
71 {
72 	/* Sanity check arguments */
73 	Assert(module);
74 	Assert(callback_func);
75 
76 	/* Register callback function */
77 	module->callback[callback_type] = callback_func;
78 
79 	/* Set the bit corresponding to the callback_type */
80 	module->register_callback_mask |= (1 << callback_type);
81 
82 	/* Enable interrupt for this TRNG module */
83 	system_interrupt_enable(SYSTEM_INTERRUPT_MODULE_TRNG);
84 
85 	return STATUS_OK;
86 }
87 
88 /**
89  * \brief Unregisters a callback
90  *
91  * Unregisters a callback function implemented by the user. The callback should be
92  * disabled before it is unregistered.
93  *
94  * \param[in]     module        Pointer to TC software instance struct
95  * \param[in]     callback_type Callback type given by an enum
96  *
97  * \retval STATUS_OK  The function exited successfully
98  */
trng_unregister_callback(struct trng_module * const module,const enum trng_callback callback_type)99 enum status_code trng_unregister_callback(
100 		struct trng_module *const module,
101 		const enum trng_callback callback_type)
102 {
103 	/* Sanity check arguments */
104 	Assert(module);
105 
106 	/* Unregister callback function */
107 	module->callback[callback_type] = NULL;
108 
109 	/* Clear the bit corresponding to the callback_type */
110 	module->register_callback_mask &= ~(1 << callback_type);
111 
112 	/* Disable interrupt for this TRNG module */
113 	if (module->register_callback_mask == 0) {
114 		system_interrupt_disable(SYSTEM_INTERRUPT_MODULE_TRNG);
115 	}
116 
117 	return STATUS_OK;
118 }
119 
120 /**
121  * \internal ISR handler for TRNG
122  *
123  * TRNG interrupt handler for random data ready.
124  */
TRNG_Handler(void)125 void TRNG_Handler(void)
126 {
127 	/* Temporary variable */
128 	uint8_t interrupt_and_callback_status_mask;
129 
130 	/* Get device instance from the look-up table */
131 	struct trng_module *module = _trng_instance;
132 
133 	/* Read and mask interrupt flag register */
134 	interrupt_and_callback_status_mask = module->hw->INTFLAG.reg &
135 			(module->register_callback_mask & module->enable_callback_mask);
136 
137 	/* Check if data ready needs to be serviced */
138 	if (interrupt_and_callback_status_mask & TRNG_INTFLAG_DATARDY) {
139 		/* Store random result in job buffer (it will clear data ready flag) */
140 		*(module->job_buffer++) = module->hw->DATA.reg;
141 
142 		module->remaining_number -= 1;
143 		if (module->remaining_number == 0) {
144 			if (module->job_status == STATUS_BUSY) {
145 				/* Job is complete. Update status, disable interrupt
146 				 * and call callback */
147 				module->job_status = STATUS_OK;
148 				module->hw->INTENCLR.reg = TRNG_INTENCLR_DATARDY;
149 				(module->callback[TRNG_CALLBACK_READ_BUFFER])(module);
150 			}
151 		}
152 	}
153 
154 	/* Clear interrupt flag */
155 	module->hw->INTFLAG.reg = TRNG_INTFLAG_DATARDY;
156 }
157 
158 /**
159  * \brief Read multiple random data from TRNG
160  *
161  * As soon as the TRNG is enabled, the module provides a new 32-bits
162  * random data for every 84 CLK_TRNG_APB clock cycles.
163  *
164  * \param[in]  module_inst  Pointer to the TRNG software instance struct
165  * \param[in]  number       Number of random data to get
166  * \param[out] buffer       Buffer to store the random data
167  *
168  * \return Status of the job start.
169  * \retval STATUS_OK        The read job was started successfully and is
170  *                          in progress
171  * \retval STATUS_BUSY      The TRNG is already busy with another job
172  */
trng_read_buffer_job(struct trng_module * const module_inst,uint32_t * buffer,uint32_t number)173 enum status_code trng_read_buffer_job(
174 		struct trng_module *const module_inst,
175 		uint32_t *buffer,
176 		uint32_t number)
177 {
178 	Assert(module_inst);
179 	Assert(number);
180 	Assert(buffer);
181 
182 	if (module_inst->remaining_number != 0 ||
183 			module_inst->job_status == STATUS_BUSY) {
184 		return STATUS_BUSY;
185 	}
186 
187 	module_inst->job_status = STATUS_BUSY;
188 	module_inst->remaining_number = number;
189 	module_inst->job_buffer = buffer;
190 
191 	/* Enable data ready interrupt */
192 	module_inst->hw->INTENSET.reg = TRNG_INTENSET_DATARDY;
193 
194 	return STATUS_OK;
195 }
196 
197 /**
198  * \brief Gets the status of a job
199  *
200  * Gets the status of an ongoing or the last job.
201  *
202  * \param [in]  module_inst Pointer to the TRNG software instance struct
203  * \param [in]  type        Type of job to abort
204  *
205  * \return Status of the job.
206  */
trng_get_job_status(struct trng_module * module_inst,enum trng_job_type type)207 enum status_code trng_get_job_status(
208 		struct trng_module *module_inst,
209 		enum trng_job_type type)
210 {
211 	/* Sanity check arguments */
212 	Assert(module_inst);
213 
214 	if (type == TRNG_JOB_READ_BUFFER) {
215 		return module_inst->job_status;
216 	} else {
217 		return STATUS_ERR_INVALID_ARG;
218 	}
219 }
220 
221 /**
222  * \brief Aborts an ongoing job
223  *
224  * \param [in]  module_inst Pointer to the TRNG software instance struct
225  * \param [in]  type        Type of job to abort
226  */
trng_abort_job(struct trng_module * module_inst,enum trng_job_type type)227 void trng_abort_job(
228 		struct trng_module *module_inst,
229 		enum trng_job_type type)
230 {
231 	/* Sanity check arguments */
232 	Assert(module_inst);
233 
234 	if (type == TRNG_JOB_READ_BUFFER) {
235 		/* Disable interrupt */
236 		module_inst->hw->INTENCLR.reg = TRNG_INTENCLR_DATARDY;
237 		/* Mark job as aborted */
238 		module_inst->job_status = STATUS_ABORTED;
239 		module_inst->remaining_number = 0;
240 	}
241 }
242 
243