Switch from GPLv2 to GPLv2+
[fw/altos] / src / drivers / ao_trng_send.c
1 /*
2  * Copyright © 2015 Keith Packard <keithp@keithp.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17  */
18
19 #include <ao.h>
20 #include <ao_adc_fast.h>
21 #include <ao_crc.h>
22 #include <ao_trng_send.h>
23 #include <ao_exti.h>
24 #include <ao_power.h>
25
26 static struct ao_task   ao_trng_send_task;
27 static uint8_t          trng_running;
28 static AO_TICK_TYPE     trng_power_time;
29
30 #define TRNG_ENABLE_DELAY       AO_MS_TO_TICKS(100)
31
32 static uint8_t          random_mutex;
33
34 #if AO_USB_HAS_IN2
35
36 static struct ao_task   ao_trng_send_raw_task;
37
38 static void
39 ao_trng_get_raw(uint16_t *buf)
40 {
41         uint16_t        i;
42         uint16_t        t;
43         uint16_t        v;
44
45         t = ao_adc_get(AO_USB_IN_SIZE>>1);      /* one 16-bit value per two output bytes */
46         for (i = 0; i < AO_USB_IN_SIZE / sizeof (uint16_t); i++) {
47                 v = ao_adc_ring[t];
48                 *buf++ = v;
49                 t = (t + 1) & (AO_ADC_RING_SIZE - 1);
50         }
51         ao_adc_ack(AO_USB_IN_SIZE>>1);
52 }
53
54 static void
55 ao_trng_send_raw(void)
56 {
57         static uint16_t *buffer[2];
58         int             usb_buf_id;
59
60         if (!buffer[0]) {
61                 buffer[0] = ao_usb_alloc();
62                 buffer[1] = ao_usb_alloc();
63                 if (!buffer[0])
64                         ao_exit();
65         }
66
67         usb_buf_id = 0;
68
69         for (;;) {
70                 ao_mutex_get(&random_mutex);
71                 if (!trng_running) {
72                         AO_TICK_TYPE    delay;
73
74                         delay = trng_power_time + TRNG_ENABLE_DELAY - ao_time();
75                         if (delay > TRNG_ENABLE_DELAY)
76                                 delay = TRNG_ENABLE_DELAY;
77
78                         /* Delay long enough for the HV power supply
79                          * to stabilize so that the first bits we read
80                          * aren't of poor quality
81                          */
82                         ao_delay(delay);
83                         trng_running = TRUE;
84                 }
85 #ifdef AO_LED_TRNG_RAW
86                 ao_led_on(AO_LED_TRNG_RAW);
87 #endif
88                 ao_trng_get_raw(buffer[usb_buf_id]);
89 #ifdef AO_LED_TRNG_RAW
90                 ao_led_off(AO_LED_TRNG_RAW);
91 #endif
92                 ao_mutex_put(&random_mutex);
93                 ao_usb_write2(buffer[usb_buf_id], AO_USB_IN_SIZE);
94                 usb_buf_id = 1-usb_buf_id;
95         }
96 }
97
98 #endif
99
100 /* Make sure there's at least 8 bits of variance in the samples */
101 #define MIN_VARIANCE            (128 * 128)
102
103 /* Make sure the signal is spread around a bit */
104 #define MAX_VARIANCE            (512 * 512)
105
106 #define ADD_STATS(value) do {                   \
107                 sum += (value);                 \
108                 sum2 += (value) * (value);      \
109         } while(0)
110
111 #define VARIANCE(n)     ((sum2 - (sum / (n) * sum)) / (n))
112
113 static int
114 ao_trng_get_cooked(uint16_t *buf)
115 {
116         uint16_t        i;
117         uint16_t        t;
118         uint32_t        *rnd = (uint32_t *) ao_adc_ring;
119         int32_t         sum, sum2, var;
120
121         sum = sum2 = 0;
122         t = ao_adc_get(AO_USB_IN_SIZE) >> 1;            /* one 16-bit value per output byte */
123         for (i = 0; i < AO_USB_IN_SIZE / sizeof (uint16_t); i++) {
124                 uint32_t        v;
125                 uint16_t        v1, v2;
126
127                 /* Fetch two values in one operation */
128                 v = rnd[t];
129                 t = (t + 1) & ((AO_ADC_RING_SIZE >> 1) - 1);
130
131                 *buf++ = ao_crc_in_32_out_16(v);
132
133                 v1 = v;
134                 v2 = v >> 16;
135
136                 ADD_STATS(v1);
137                 ADD_STATS(v2);
138         }
139         ao_adc_ack(AO_USB_IN_SIZE);
140         var = VARIANCE(2 * AO_USB_IN_SIZE / sizeof (uint16_t));
141         return var >= MIN_VARIANCE && var <= MAX_VARIANCE;
142 }
143
144 #define AO_TRNG_START_WAIT      1024
145 #define AO_TRNG_START_CHECK     32
146
147 static void
148 ao_trng_send(void)
149 {
150         static uint16_t *buffer[2];
151         int     usb_buf_id;
152         int     good_bits;
153         int     failed;
154         int     s;
155
156         if (!buffer[0]) {
157                 buffer[0] = ao_usb_alloc();
158                 buffer[1] = ao_usb_alloc();
159                 if (!buffer[0])
160                         ao_exit();
161         }
162
163         usb_buf_id = 0;
164
165 #ifdef AO_TRNG_ENABLE_PORT
166         ao_gpio_set(AO_TRNG_ENABLE_PORT, AO_TRNG_ENABLE_BIT, AO_TRNG_ENABLE_PIN, 1);
167 #endif
168         trng_power_time = ao_time();
169
170         ao_crc_reset();
171
172         ao_delay(TRNG_ENABLE_DELAY);
173
174         for (s = 0; s < AO_TRNG_START_WAIT; s++) {
175                 if (ao_trng_get_cooked(buffer[0]))
176                         break;
177                 ao_delay(AO_MS_TO_TICKS(10));
178         }
179
180         /* Validate the hardware before enabling USB */
181         failed = 0;
182         for (s = 0; s < AO_TRNG_START_CHECK; s++) {
183                 if (!ao_trng_get_cooked(buffer[0])) {
184                         failed++;
185                         ao_delay(AO_MS_TO_TICKS(10));
186                 }
187         }
188         if (failed > AO_TRNG_START_CHECK / 4)
189                 ao_panic(AO_PANIC_DMA);
190
191 #if AO_USB_HAS_IN2
192         ao_add_task(&ao_trng_send_raw_task, ao_trng_send_raw, "trng_send_raw");
193 #endif
194
195 #ifdef AO_USB_START_DISABLED
196         ao_usb_enable();
197 #endif
198
199         for (;;) {
200                 ao_mutex_get(&random_mutex);
201                 if (!trng_running) {
202                         AO_TICK_TYPE    delay;
203
204                         delay = trng_power_time + TRNG_ENABLE_DELAY - ao_time();
205                         if (delay > TRNG_ENABLE_DELAY)
206                                 delay = TRNG_ENABLE_DELAY;
207
208                         /* Delay long enough for the HV power supply
209                          * to stabilize so that the first bits we read
210                          * aren't of poor quality
211                          */
212                         ao_delay(delay);
213                         trng_running = TRUE;
214                 }
215 #ifdef AO_LED_TRNG_COOKED
216                 ao_led_on(AO_LED_TRNG_COOKED);
217 #endif
218                 good_bits = ao_trng_get_cooked(buffer[usb_buf_id]);
219 #ifdef AO_LED_TRNG_COOKED
220                 ao_led_off(AO_LED_TRNG_COOKED);
221 #endif
222                 ao_mutex_put(&random_mutex);
223                 if (good_bits) {
224                         ao_usb_write(buffer[usb_buf_id], AO_USB_IN_SIZE);
225                         usb_buf_id = 1-usb_buf_id;
226                         failed = 0;
227                 } else {
228                         failed++;
229                         ao_delay(AO_MS_TO_TICKS(10));
230                         if (failed > 10) {
231                                 ao_usb_disable();
232                                 ao_panic(AO_PANIC_DMA);
233                         }
234                 }
235         }
236 }
237
238 #if AO_POWER_MANAGEMENT
239
240 static void ao_trng_suspend(void *arg)
241 {
242         (void) arg;
243 #ifdef AO_TRNG_ENABLE_PORT
244         ao_gpio_set(AO_TRNG_ENABLE_PORT, AO_TRNG_ENABLE_BIT, AO_TRNG_ENABLE_PIN, 0);
245 #endif
246         trng_running = FALSE;
247 }
248
249 static void ao_trng_resume(void *arg)
250 {
251         (void) arg;
252 #ifdef AO_TRNG_ENABLE_PORT
253         ao_gpio_set(AO_TRNG_ENABLE_PORT, AO_TRNG_ENABLE_BIT, AO_TRNG_ENABLE_PIN, 1);
254 #endif
255         trng_power_time = ao_time();
256 }
257
258 static struct ao_power ao_trng_power = {
259         .suspend = ao_trng_suspend,
260         .resume = ao_trng_resume
261 };
262
263 #endif
264
265 void
266 ao_trng_send_init(void)
267 {
268 #ifdef AO_TRNG_ENABLE_PORT
269         ao_enable_output(AO_TRNG_ENABLE_PORT, AO_TRNG_ENABLE_BIT, AO_TRNG_ENABLE_PIN, 0);
270         ao_power_register(&ao_trng_power);
271 #endif
272         ao_enable_input(AO_RAW_PORT, AO_RAW_BIT, AO_EXTI_MODE_PULL_UP);
273         ao_add_task(&ao_trng_send_task, ao_trng_send, "trng_send");
274 }