4e02c0ce75d8c1bd04f68b3527c9e9e6dee42624
[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 static void
35 ao_trng_start(void)
36 {
37         if (!trng_running) {
38                 ao_mutex_get(&random_mutex);
39                 if (!trng_running) {
40                         AO_TICK_TYPE    delay;
41
42                         delay = trng_power_time + TRNG_ENABLE_DELAY - ao_time();
43                         if (delay > TRNG_ENABLE_DELAY)
44                                 delay = TRNG_ENABLE_DELAY;
45
46                         /* Delay long enough for the HV power supply
47                          * to stabilize so that the first bits we read
48                          * aren't of poor quality
49                          */
50                         ao_delay(delay);
51                         trng_running = TRUE;
52                 }
53                 ao_mutex_put(&random_mutex);
54         }
55 }
56
57 #if AO_USB_HAS_IN2
58
59 static struct ao_task   ao_trng_send_raw_task;
60
61 static void
62 ao_trng_get_raw(uint16_t *buf)
63 {
64         uint16_t        i;
65         uint16_t        t;
66         uint16_t        v;
67
68         t = ao_adc_get(AO_USB_IN_SIZE>>1);      /* one 16-bit value per two output bytes */
69         for (i = 0; i < AO_USB_IN_SIZE / sizeof (uint16_t); i++) {
70                 v = ao_adc_ring[t];
71                 *buf++ = v;
72                 t = (t + 1) & (AO_ADC_RING_SIZE - 1);
73         }
74         ao_adc_ack(AO_USB_IN_SIZE>>1);
75 }
76
77 static void
78 ao_trng_send_raw(void)
79 {
80         uint16_t        *buffer[2];
81         int             usb_buf_id;
82
83         usb_buf_id = ao_usb_alloc2(buffer);
84
85         for (;;) {
86                 ao_trng_start();
87 #ifdef AO_LED_TRNG_RAW
88                 ao_led_on(AO_LED_TRNG_RAW);
89 #endif
90                 ao_trng_get_raw(buffer[usb_buf_id]);
91 #ifdef AO_LED_TRNG_RAW
92                 ao_led_off(AO_LED_TRNG_RAW);
93 #endif
94                 usb_buf_id = ao_usb_write2(AO_USB_IN_SIZE);
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 = 1;
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         uint16_t        *buffer[2];
135         int             usb_buf_id;
136         int             good_bits;
137         int             failed;
138         int             s;
139
140         usb_buf_id = ao_usb_alloc(buffer);
141
142 #ifdef AO_TRNG_ENABLE_PORT
143         ao_gpio_set(AO_TRNG_ENABLE_PORT, AO_TRNG_ENABLE_BIT, AO_TRNG_ENABLE_PIN, 1);
144 #endif
145         trng_power_time = ao_time();
146
147         ao_crc_reset();
148
149         for (s = 0; s < AO_TRNG_START_WAIT; s++) {
150                 int i;
151                 uint16_t        min, max;
152                 uint16_t        buf[AO_USB_IN_SIZE>>1];
153
154                 ao_trng_get_raw(buf);
155                 min = max = buf[0];
156                 for (i = 1; i < (AO_USB_IN_SIZE>>1); i++) {
157                         uint16_t v = buf[i];
158                         if (v < min) min = v;
159                         if (v > max) max = v;
160                 }
161                 /* Wait for at least 10 bits of range */
162                 if ((uint16_t) (max - min) >= 1024)
163                         break;
164                 ao_delay(AO_MS_TO_TICKS(10));
165         }
166
167         /* Validate the hardware before enabling USB */
168         failed = 0;
169         for (s = 0; s < AO_TRNG_START_CHECK; s++) {
170                 if (!ao_trng_get_cooked(buffer[0])) {
171                         failed++;
172                         ao_delay(AO_MS_TO_TICKS(10));
173                 }
174         }
175         if (failed > AO_TRNG_START_CHECK / 4)
176                 ao_panic(AO_PANIC_DMA);
177
178 #if AO_USB_HAS_IN2
179         ao_add_task(&ao_trng_send_raw_task, ao_trng_send_raw, "trng_send_raw");
180 #endif
181
182 #ifdef AO_USB_START_DISABLED
183         ao_usb_enable();
184 #endif
185
186         for (;;) {
187                 ao_trng_start();
188 #ifdef AO_LED_TRNG_COOKED
189                 ao_led_on(AO_LED_TRNG_COOKED);
190 #endif
191                 good_bits = ao_trng_get_cooked(buffer[usb_buf_id]);
192 #ifdef AO_LED_TRNG_COOKED
193                 ao_led_off(AO_LED_TRNG_COOKED);
194 #endif
195                 if (good_bits) {
196                         usb_buf_id = ao_usb_write(AO_USB_IN_SIZE);
197                         failed = 0;
198                 } else {
199                         failed++;
200                         if (failed > 10) {
201                                 ao_usb_disable();
202                                 ao_panic(AO_PANIC_DMA);
203                         }
204                 }
205         }
206 }
207
208 #if AO_POWER_MANAGEMENT
209
210 static void ao_trng_suspend(void *arg)
211 {
212         (void) arg;
213 #ifdef AO_TRNG_ENABLE_PORT
214         ao_gpio_set(AO_TRNG_ENABLE_PORT, AO_TRNG_ENABLE_BIT, AO_TRNG_ENABLE_PIN, 0);
215 #endif
216         trng_running = FALSE;
217 }
218
219 static void ao_trng_resume(void *arg)
220 {
221         (void) arg;
222 #ifdef AO_TRNG_ENABLE_PORT
223         ao_gpio_set(AO_TRNG_ENABLE_PORT, AO_TRNG_ENABLE_BIT, AO_TRNG_ENABLE_PIN, 1);
224 #endif
225         trng_power_time = ao_time();
226 }
227
228 static struct ao_power ao_trng_power = {
229         .suspend = ao_trng_suspend,
230         .resume = ao_trng_resume
231 };
232
233 #endif
234
235 void
236 ao_trng_send_init(void)
237 {
238 #ifdef AO_TRNG_ENABLE_PORT
239         ao_enable_output(AO_TRNG_ENABLE_PORT, AO_TRNG_ENABLE_BIT, AO_TRNG_ENABLE_PIN, 0);
240         ao_power_register(&ao_trng_power);
241 #endif
242         ao_enable_input(AO_RAW_PORT, AO_RAW_BIT, AO_EXTI_MODE_PULL_UP);
243         ao_add_task(&ao_trng_send_task, ao_trng_send, "trng_send");
244 }