chaoskey: Wait for input data to stabilize before using it
[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 static uint32_t previous[AO_USB_IN_SIZE / sizeof (uint16_t)];
101
102 static int
103 ao_trng_get_cooked(uint16_t *buf)
104 {
105         uint16_t        i;
106         uint16_t        t;
107         uint32_t        *rnd = (uint32_t *) (void *) ao_adc_ring;
108         uint8_t         mismatch = 0;
109
110         t = ao_adc_get(AO_USB_IN_SIZE) >> 1;            /* one 16-bit value per output byte */
111         for (i = 0; i < AO_USB_IN_SIZE / sizeof (uint16_t); i++) {
112                 uint32_t        v;
113
114                 /* Fetch two values in one operation */
115                 v = rnd[t];
116                 if (v != previous[i]) {
117                         mismatch = 1;
118                         previous[i] = v;
119                 }
120                 t = (t + 1) & ((AO_ADC_RING_SIZE >> 1) - 1);
121
122                 *buf++ = ao_crc_in_32_out_16(v);
123         }
124         ao_adc_ack(AO_USB_IN_SIZE);
125         return mismatch;
126 }
127
128 #define AO_TRNG_START_WAIT      1024
129 #define AO_TRNG_START_CHECK     32
130
131 static void
132 ao_trng_send(void)
133 {
134         static uint16_t *buffer[2];
135         int     usb_buf_id;
136         int     good_bits;
137         int     failed;
138         int     s;
139
140         if (!buffer[0]) {
141                 buffer[0] = ao_usb_alloc();
142                 buffer[1] = ao_usb_alloc();
143                 if (!buffer[0])
144                         ao_exit();
145         }
146
147         usb_buf_id = 0;
148
149 #ifdef AO_TRNG_ENABLE_PORT
150         ao_gpio_set(AO_TRNG_ENABLE_PORT, AO_TRNG_ENABLE_BIT, AO_TRNG_ENABLE_PIN, 1);
151 #endif
152         trng_power_time = ao_time();
153
154         ao_crc_reset();
155
156         for (s = 0; s < AO_TRNG_START_WAIT; s++) {
157                 int i;
158                 uint16_t        min, max;
159                 uint16_t        buf[AO_USB_IN_SIZE>>1];
160
161                 ao_trng_get_raw(buf);
162                 min = max = buf[0];
163                 for (i = 1; i < (AO_USB_IN_SIZE>>1); i++) {
164                         uint16_t v = buf[i];
165                         if (v < min) min = v;
166                         if (v > max) max = v;
167                 }
168                 /* Wait for at least 10 bits of range */
169                 if ((uint16_t) (max - min) >= 1024)
170                         break;
171                 ao_delay(AO_MS_TO_TICKS(10));
172         }
173
174         /* Validate the hardware before enabling USB */
175         failed = 0;
176         for (s = 0; s < AO_TRNG_START_CHECK; s++) {
177                 if (!ao_trng_get_cooked(buffer[0])) {
178                         failed++;
179                         ao_delay(AO_MS_TO_TICKS(10));
180                 }
181         }
182         if (failed > AO_TRNG_START_CHECK / 4)
183                 ao_panic(AO_PANIC_DMA);
184
185 #if AO_USB_HAS_IN2
186         ao_add_task(&ao_trng_send_raw_task, ao_trng_send_raw, "trng_send_raw");
187 #endif
188
189 #ifdef AO_USB_START_DISABLED
190         ao_usb_enable();
191 #endif
192
193         for (;;) {
194                 ao_mutex_get(&random_mutex);
195                 if (!trng_running) {
196                         AO_TICK_TYPE    delay;
197
198                         delay = trng_power_time + TRNG_ENABLE_DELAY - ao_time();
199                         if (delay > TRNG_ENABLE_DELAY)
200                                 delay = TRNG_ENABLE_DELAY;
201
202                         /* Delay long enough for the HV power supply
203                          * to stabilize so that the first bits we read
204                          * aren't of poor quality
205                          */
206                         ao_delay(delay);
207                         trng_running = TRUE;
208                 }
209 #ifdef AO_LED_TRNG_COOKED
210                 ao_led_on(AO_LED_TRNG_COOKED);
211 #endif
212                 good_bits = ao_trng_get_cooked(buffer[usb_buf_id]);
213 #ifdef AO_LED_TRNG_COOKED
214                 ao_led_off(AO_LED_TRNG_COOKED);
215 #endif
216                 ao_mutex_put(&random_mutex);
217                 if (good_bits) {
218                         ao_usb_write(buffer[usb_buf_id], AO_USB_IN_SIZE);
219                         usb_buf_id = 1-usb_buf_id;
220                         failed = 0;
221                 } else {
222                         failed++;
223                         ao_delay(AO_MS_TO_TICKS(10));
224                         if (failed > 10) {
225                                 ao_usb_disable();
226                                 ao_panic(AO_PANIC_DMA);
227                         }
228                 }
229         }
230 }
231
232 #if AO_POWER_MANAGEMENT
233
234 static void ao_trng_suspend(void *arg)
235 {
236         (void) arg;
237 #ifdef AO_TRNG_ENABLE_PORT
238         ao_gpio_set(AO_TRNG_ENABLE_PORT, AO_TRNG_ENABLE_BIT, AO_TRNG_ENABLE_PIN, 0);
239 #endif
240         trng_running = FALSE;
241 }
242
243 static void ao_trng_resume(void *arg)
244 {
245         (void) arg;
246 #ifdef AO_TRNG_ENABLE_PORT
247         ao_gpio_set(AO_TRNG_ENABLE_PORT, AO_TRNG_ENABLE_BIT, AO_TRNG_ENABLE_PIN, 1);
248 #endif
249         trng_power_time = ao_time();
250 }
251
252 static struct ao_power ao_trng_power = {
253         .suspend = ao_trng_suspend,
254         .resume = ao_trng_resume
255 };
256
257 #endif
258
259 void
260 ao_trng_send_init(void)
261 {
262 #ifdef AO_TRNG_ENABLE_PORT
263         ao_enable_output(AO_TRNG_ENABLE_PORT, AO_TRNG_ENABLE_BIT, AO_TRNG_ENABLE_PIN, 0);
264         ao_power_register(&ao_trng_power);
265 #endif
266         ao_enable_input(AO_RAW_PORT, AO_RAW_BIT, AO_EXTI_MODE_PULL_UP);
267         ao_add_task(&ao_trng_send_task, ao_trng_send, "trng_send");
268 }