altos/stmf0: Fix build without second USB IN endpoint
[fw/altos] / src / stmf0 / ao_usb_stm.c
1 /*
2  * Copyright © 2012 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; version 2 of the License.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program; if not, write to the Free Software Foundation, Inc.,
15  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
16  */
17
18 #include "ao.h"
19 #include "ao_usb.h"
20 #include "ao_product.h"
21 #include "ao_power.h"
22
23 #define USB_DEBUG       0
24 #define USB_STATUS      0
25 #define USB_DEBUG_DATA  0
26 #define USB_ECHO        0
27
28 #ifndef AO_PA11_PA12_RMP
29 #error "must define AO_PA11_PA12_RMP"
30 #endif
31
32 #ifndef AO_POWER_MANAGEMENT
33 #define AO_POWER_MANAGEMENT     0
34 #endif
35
36 #ifndef USE_USB_STDIO
37 #define USE_USB_STDIO   1
38 #endif
39
40 #if USE_USB_STDIO
41 #define AO_USB_OUT_SLEEP_ADDR   (&ao_stdin_ready)
42 #else
43 #define AO_USB_OUT_SLEEP_ADDR   (&ao_usb_out_avail)
44 #endif
45
46 #if USB_DEBUG
47 #define debug(format, args...)  printf(format, ## args);
48 #else
49 #define debug(format, args...)
50 #endif
51
52 #if USB_DEBUG_DATA
53 #define debug_data(format, args...)     printf(format, ## args);
54 #else
55 #define debug_data(format, args...)
56 #endif
57
58 struct ao_usb_setup {
59         uint8_t         dir_type_recip;
60         uint8_t         request;
61         uint16_t        value;
62         uint16_t        index;
63         uint16_t        length;
64 } ao_usb_setup;
65
66 static uint8_t  ao_usb_ep0_state;
67
68 /* Pending EP0 IN data */
69 static const uint8_t    *ao_usb_ep0_in_data;    /* Remaining data */
70 static uint8_t          ao_usb_ep0_in_len;      /* Remaining amount */
71
72 /* Temp buffer for smaller EP0 in data */
73 static uint8_t  ao_usb_ep0_in_buf[2];
74
75 /* Pending EP0 OUT data */
76 static uint8_t *ao_usb_ep0_out_data;
77 static uint8_t  ao_usb_ep0_out_len;
78
79 /*
80  * Objects allocated in special USB memory
81  */
82
83 /* Buffer description tables */
84 static union stm_usb_bdt        *ao_usb_bdt;
85 /* USB address of end of allocated storage */
86 static uint16_t ao_usb_sram_addr;
87
88 /* Pointer to ep0 tx/rx buffers in USB memory */
89 static uint16_t *ao_usb_ep0_tx_buffer;
90 static uint16_t *ao_usb_ep0_rx_buffer;
91
92 #if AO_USB_HAS_INT
93 /* Pointer to interrupt buffer in USB memory */
94 static uint16_t ao_usb_int_tx_offset;
95 #endif
96
97 /* Pointer to bulk data tx/rx buffers in USB memory */
98 #if AO_USB_HAS_IN
99 static uint16_t ao_usb_in_tx_offset;
100 static uint16_t *ao_usb_in_tx_buffer;
101
102 /* System ram shadow of USB buffer; writing individual bytes is
103  * too much of a pain (sigh) */
104 static uint8_t  ao_usb_tx_buffer[AO_USB_IN_SIZE];
105 static uint8_t  ao_usb_tx_count;
106
107 #endif
108 #if AO_USB_HAS_OUT
109 static uint16_t ao_usb_out_rx_offset;
110 static uint16_t *ao_usb_out_rx_buffer;
111
112 /* System ram shadow of USB buffer; writing individual bytes is
113  * too much of a pain (sigh) */
114 static uint8_t  ao_usb_rx_buffer[AO_USB_OUT_SIZE];
115 static uint8_t  ao_usb_rx_count, ao_usb_rx_pos;
116
117 #endif
118 #if AO_USB_HAS_IN2
119 static uint16_t ao_usb_in2_tx_offset;
120 static uint16_t *ao_usb_in2_tx_buffer;
121
122 /* System ram shadow of USB buffer; writing individual bytes is
123  * too much of a pain (sigh) */
124 static uint8_t  ao_usb_tx2_buffer[AO_USB_IN_SIZE];
125 static uint8_t  ao_usb_tx2_count;
126 #endif
127
128 /*
129  * End point register indices
130  */
131
132 #define AO_USB_CONTROL_EPR      0
133 #define AO_USB_INT_EPR          1
134 #define AO_USB_OUT_EPR          2
135 #define AO_USB_IN_EPR           3
136 #define AO_USB_IN2_EPR          4
137
138 /* Marks when we don't need to send an IN packet.
139  * This happens only when the last IN packet is not full,
140  * otherwise the host will expect to keep seeing packets.
141  * Send a zero-length packet as required
142  */
143 static uint8_t  ao_usb_in_flushed;
144
145 /* Marks when we have delivered an IN packet to the hardware
146  * and it has not been received yet. ao_sleep on this address
147  * to wait for it to be delivered.
148  */
149 static uint8_t  ao_usb_in_pending;
150
151 #if AO_USB_HAS_IN2
152 /* Marks when we have delivered an IN packet to the hardware
153  * and it has not been received yet. ao_sleep on this address
154  * to wait for it to be delivered.
155  */
156 static uint8_t  ao_usb_in2_pending;
157 static uint16_t in2_count;
158 static uint8_t  ao_usb_in2_flushed;
159 #endif
160
161 /* Marks when an OUT packet has been received by the hardware
162  * but not pulled to the shadow buffer.
163  */
164 static uint8_t  ao_usb_out_avail;
165 uint8_t         ao_usb_running;
166 static uint8_t  ao_usb_configuration;
167
168 #define AO_USB_EP0_GOT_SETUP    1
169 #define AO_USB_EP0_GOT_RX_DATA  2
170 #define AO_USB_EP0_GOT_TX_ACK   4
171
172 static uint8_t  ao_usb_ep0_receive;
173 static uint8_t  ao_usb_address;
174 static uint8_t  ao_usb_address_pending;
175
176 static inline uint32_t set_toggle(uint32_t      current_value,
177                                    uint32_t     mask,
178                                    uint32_t     desired_value)
179 {
180         return (current_value ^ desired_value) & mask;
181 }
182
183 static inline uint16_t *ao_usb_packet_buffer_addr(uint16_t sram_addr)
184 {
185         return (uint16_t *) (stm_usb_sram + sram_addr);
186 }
187
188 static inline uint16_t ao_usb_packet_buffer_offset(uint16_t *addr)
189 {
190         return (uint16_t) ((uint8_t *) addr - stm_usb_sram);
191 }
192
193 static inline uint32_t ao_usb_epr_stat_rx(uint32_t epr) {
194         return (epr >> STM_USB_EPR_STAT_RX) & STM_USB_EPR_STAT_RX_MASK;
195 }
196
197 static inline uint32_t ao_usb_epr_stat_tx(uint32_t epr) {
198         return (epr >> STM_USB_EPR_STAT_TX) & STM_USB_EPR_STAT_TX_MASK;
199 }
200
201 static inline uint32_t ao_usb_epr_ctr_rx(uint32_t epr) {
202         return (epr >> STM_USB_EPR_CTR_RX) & 1;
203 }
204
205 static inline uint32_t ao_usb_epr_ctr_tx(uint32_t epr) {
206         return (epr >> STM_USB_EPR_CTR_TX) & 1;
207 }
208
209 static inline uint32_t ao_usb_epr_setup(uint32_t epr) {
210         return (epr >> STM_USB_EPR_SETUP) & 1;
211 }
212
213 static inline uint32_t ao_usb_epr_dtog_rx(uint32_t epr) {
214         return (epr >> STM_USB_EPR_DTOG_RX) & 1;
215 }
216
217 static inline uint32_t ao_usb_epr_dtog_tx(uint32_t epr) {
218         return (epr >> STM_USB_EPR_DTOG_TX) & 1;
219 }
220
221 /*
222  * Set current device address and mark the
223  * interface as active
224  */
225 void
226 ao_usb_set_address(uint8_t address)
227 {
228         debug("ao_usb_set_address %02x\n", address);
229         stm_usb.daddr = (1 << STM_USB_DADDR_EF) | address;
230         ao_usb_address_pending = 0;
231 }
232
233 /*
234  * Write these values to preserve register contents under HW changes
235  */
236
237 #define STM_USB_EPR_INVARIANT   ((1 << STM_USB_EPR_CTR_RX) |            \
238                                  (STM_USB_EPR_DTOG_RX_WRITE_INVARIANT << STM_USB_EPR_DTOG_RX) | \
239                                  (STM_USB_EPR_STAT_RX_WRITE_INVARIANT << STM_USB_EPR_STAT_RX) | \
240                                  (1 << STM_USB_EPR_CTR_TX) |            \
241                                  (STM_USB_EPR_DTOG_TX_WRITE_INVARIANT << STM_USB_EPR_DTOG_TX) | \
242                                  (STM_USB_EPR_STAT_TX_WRITE_INVARIANT << STM_USB_EPR_STAT_TX))
243
244 #define STM_USB_EPR_INVARIANT_MASK      ((1 << STM_USB_EPR_CTR_RX) |    \
245                                          (STM_USB_EPR_DTOG_RX_MASK << STM_USB_EPR_DTOG_RX) | \
246                                          (STM_USB_EPR_STAT_RX_MASK << STM_USB_EPR_STAT_RX) | \
247                                          (1 << STM_USB_EPR_CTR_TX) |    \
248                                          (STM_USB_EPR_DTOG_TX_MASK << STM_USB_EPR_DTOG_TX) | \
249                                          (STM_USB_EPR_STAT_TX_MASK << STM_USB_EPR_STAT_TX))
250
251 /*
252  * These bits are purely under sw control, so preserve them in the
253  * register by re-writing what was read
254  */
255 #define STM_USB_EPR_PRESERVE_MASK       ((STM_USB_EPR_EP_TYPE_MASK << STM_USB_EPR_EP_TYPE) | \
256                                          (1 << STM_USB_EPR_EP_KIND) |   \
257                                          (STM_USB_EPR_EA_MASK << STM_USB_EPR_EA))
258
259 #define TX_DBG 0
260 #define RX_DBG 0
261
262 #if TX_DBG
263 #define _tx_dbg0(msg) _dbg(__LINE__,msg,0)
264 #define _tx_dbg1(msg,value) _dbg(__LINE__,msg,value)
265 #else
266 #define _tx_dbg0(msg)
267 #define _tx_dbg1(msg,value)
268 #endif
269
270 #if RX_DBG
271 #define _rx_dbg0(msg) _dbg(__LINE__,msg,0)
272 #define _rx_dbg1(msg,value) _dbg(__LINE__,msg,value)
273 #else
274 #define _rx_dbg0(msg)
275 #define _rx_dbg1(msg,value)
276 #endif
277
278 #if TX_DBG || RX_DBG
279 static void _dbg(int line, char *msg, uint32_t value);
280 #endif
281
282 /*
283  * Set the state of the specified endpoint register to a new
284  * value. This is tricky because the bits toggle where the new
285  * value is one, and we need to write invariant values in other
286  * spots of the register. This hardware is strange...
287  */
288 static void
289 _ao_usb_set_stat_tx(int ep, uint32_t stat_tx)
290 {
291         uint16_t        epr_write, epr_old;
292
293         _tx_dbg1("set_stat_tx top", stat_tx);
294         epr_old = epr_write = stm_usb.epr[ep].r;
295         epr_write &= STM_USB_EPR_PRESERVE_MASK;
296         epr_write |= STM_USB_EPR_INVARIANT;
297         epr_write |= set_toggle(epr_old,
298                               STM_USB_EPR_STAT_TX_MASK << STM_USB_EPR_STAT_TX,
299                               stat_tx << STM_USB_EPR_STAT_TX);
300         stm_usb.epr[ep].r = epr_write;
301         _tx_dbg1("set_stat_tx bottom", epr_write);
302 }
303
304 static void
305 ao_usb_set_stat_tx(int ep, uint32_t stat_tx)
306 {
307         ao_arch_block_interrupts();
308         _ao_usb_set_stat_tx(ep, stat_tx);
309         ao_arch_release_interrupts();
310 }
311
312 static void
313 _ao_usb_set_stat_rx(int ep, uint32_t stat_rx) {
314         uint16_t        epr_write, epr_old;
315
316         epr_write = epr_old = stm_usb.epr[ep].r;
317         epr_write &= STM_USB_EPR_PRESERVE_MASK;
318         epr_write |= STM_USB_EPR_INVARIANT;
319         epr_write |= set_toggle(epr_old,
320                               STM_USB_EPR_STAT_RX_MASK << STM_USB_EPR_STAT_RX,
321                               stat_rx << STM_USB_EPR_STAT_RX);
322         stm_usb.epr[ep].r = epr_write;
323 }
324
325 static void
326 ao_usb_set_stat_rx(int ep, uint32_t stat_rx) {
327         ao_arch_block_interrupts();
328         _ao_usb_set_stat_rx(ep, stat_rx);
329         ao_arch_release_interrupts();
330 }
331
332 /*
333  * Set just endpoint 0, for use during startup
334  */
335
336 static void
337 ao_usb_init_ep(uint8_t ep, uint32_t addr, uint32_t type, uint32_t stat_rx, uint32_t stat_tx)
338 {
339         uint16_t                epr;
340
341         ao_arch_block_interrupts();
342         epr = stm_usb.epr[ep].r;
343         epr = ((0 << STM_USB_EPR_CTR_RX) |
344                (epr & (1 << STM_USB_EPR_DTOG_RX)) |
345                set_toggle(epr,
346                           (STM_USB_EPR_STAT_RX_MASK << STM_USB_EPR_STAT_RX),
347                           (stat_rx << STM_USB_EPR_STAT_RX)) |
348                (type << STM_USB_EPR_EP_TYPE) |
349                (0 << STM_USB_EPR_EP_KIND) |
350                (0 << STM_USB_EPR_CTR_TX) |
351                (epr & (1 << STM_USB_EPR_DTOG_TX)) |
352                set_toggle(epr,
353                           (STM_USB_EPR_STAT_TX_MASK << STM_USB_EPR_STAT_TX),
354                           (stat_tx << STM_USB_EPR_STAT_TX)) |
355                (addr << STM_USB_EPR_EA));
356         stm_usb.epr[ep].r = epr;
357         ao_arch_release_interrupts();
358         debug ("writing epr[%d] 0x%04x wrote 0x%04x\n",
359                ep, epr, stm_usb.epr[ep].r);
360 }
361
362 static void
363 ao_usb_alloc_buffers(void)
364 {
365         ao_usb_sram_addr = 0;
366
367         ao_usb_bdt = (void *) stm_usb_sram;
368         ao_usb_sram_addr += 8 * STM_USB_BDT_SIZE;
369
370         ao_usb_ep0_tx_buffer = ao_usb_packet_buffer_addr(ao_usb_sram_addr);
371         ao_usb_sram_addr += AO_USB_CONTROL_SIZE;
372
373         ao_usb_ep0_rx_buffer = ao_usb_packet_buffer_addr(ao_usb_sram_addr);
374         ao_usb_sram_addr += AO_USB_CONTROL_SIZE;
375
376
377 #if AO_USB_HAS_INT
378         ao_usb_int_tx_offset = ao_usb_sram_addr;
379         ao_usb_sram_addr += AO_USB_INT_SIZE;
380 #endif
381
382 #if AO_USB_HAS_OUT
383         ao_usb_out_rx_buffer = ao_usb_packet_buffer_addr(ao_usb_sram_addr);
384         ao_usb_out_rx_offset = ao_usb_sram_addr;
385         ao_usb_sram_addr += AO_USB_OUT_SIZE;
386 #endif
387
388 #if AO_USB_HAS_IN
389         ao_usb_in_tx_buffer = ao_usb_packet_buffer_addr(ao_usb_sram_addr);
390         ao_usb_in_tx_offset = ao_usb_sram_addr;
391         ao_usb_sram_addr += AO_USB_IN_SIZE;
392 #endif
393
394 #if AO_USB_HAS_IN2
395         ao_usb_in2_tx_buffer = ao_usb_packet_buffer_addr(ao_usb_sram_addr);
396         ao_usb_in2_tx_offset = ao_usb_sram_addr;
397         ao_usb_sram_addr += AO_USB_IN_SIZE;
398 #endif
399 }
400
401 static void
402 ao_usb_init_btable(void)
403 {
404         /* Set up EP 0 - a Control end point with 32 bytes of in and out buffers */
405
406         ao_usb_bdt[0].single.addr_tx = ao_usb_packet_buffer_offset(ao_usb_ep0_tx_buffer);
407         ao_usb_bdt[0].single.count_tx = 0;
408
409         ao_usb_bdt[0].single.addr_rx = ao_usb_packet_buffer_offset(ao_usb_ep0_rx_buffer);
410         ao_usb_bdt[0].single.count_rx = ((1 << STM_USB_BDT_COUNT_RX_BL_SIZE) |
411                                   (((AO_USB_CONTROL_SIZE / 32) - 1) << STM_USB_BDT_COUNT_RX_NUM_BLOCK));
412 }
413
414 static void
415 ao_usb_set_ep0(void)
416 {
417         int                     e;
418
419         ao_usb_init_btable();
420
421         /* buffer table is at the start of USB memory */
422         stm_usb.btable = 0;
423
424         ao_usb_init_ep(AO_USB_CONTROL_EPR, AO_USB_CONTROL_EP,
425                        STM_USB_EPR_EP_TYPE_CONTROL,
426                        STM_USB_EPR_STAT_RX_VALID,
427                        STM_USB_EPR_STAT_TX_NAK);
428
429         /* Clear all of the other endpoints */
430         for (e = 1; e < 8; e++) {
431                 ao_usb_init_ep(e, 0,
432                                STM_USB_EPR_EP_TYPE_CONTROL,
433                                STM_USB_EPR_STAT_RX_DISABLED,
434                                STM_USB_EPR_STAT_TX_DISABLED);
435         }
436
437         ao_usb_set_address(0);
438
439         ao_usb_running = 0;
440 }
441
442 static void
443 ao_usb_set_configuration(void)
444 {
445         debug ("ao_usb_set_configuration\n");
446
447 #if AO_USB_HAS_INT
448         /* Set up the INT end point */
449         ao_usb_bdt[AO_USB_INT_EPR].single.addr_tx = ao_usb_int_tx_offset;
450         ao_usb_bdt[AO_USB_INT_EPR].single.count_tx = 0;
451
452         ao_usb_init_ep(AO_USB_INT_EPR,
453                        AO_USB_INT_EP,
454                        STM_USB_EPR_EP_TYPE_INTERRUPT,
455                        STM_USB_EPR_STAT_RX_DISABLED,
456                        STM_USB_EPR_STAT_TX_NAK);
457 #endif
458
459 #if AO_USB_HAS_OUT
460         /* Set up the OUT end point */
461         ao_usb_bdt[AO_USB_OUT_EPR].single.addr_rx = ao_usb_out_rx_offset;
462         ao_usb_bdt[AO_USB_OUT_EPR].single.count_rx = ((1 << STM_USB_BDT_COUNT_RX_BL_SIZE) |
463                                                       (((AO_USB_OUT_SIZE / 32) - 1) << STM_USB_BDT_COUNT_RX_NUM_BLOCK));
464
465         ao_usb_init_ep(AO_USB_OUT_EPR,
466                        AO_USB_OUT_EP,
467                        STM_USB_EPR_EP_TYPE_BULK,
468                        STM_USB_EPR_STAT_RX_VALID,
469                        STM_USB_EPR_STAT_TX_DISABLED);
470 #endif
471
472 #if AO_USB_HAS_IN
473         /* Set up the IN end point */
474         ao_usb_bdt[AO_USB_IN_EPR].single.addr_tx = ao_usb_in_tx_offset;
475         ao_usb_bdt[AO_USB_IN_EPR].single.count_tx = 0;
476
477         ao_usb_init_ep(AO_USB_IN_EPR,
478                        AO_USB_IN_EP,
479                        STM_USB_EPR_EP_TYPE_BULK,
480                        STM_USB_EPR_STAT_RX_DISABLED,
481                        STM_USB_EPR_STAT_TX_NAK);
482 #endif
483
484 #if AO_USB_HAS_IN2
485         /* Set up the IN2 end point */
486         ao_usb_bdt[AO_USB_IN2_EPR].single.addr_tx = ao_usb_in2_tx_offset;
487         ao_usb_bdt[AO_USB_IN2_EPR].single.count_tx = 0;
488
489         ao_usb_init_ep(AO_USB_IN2_EPR,
490                        AO_USB_IN2_EP,
491                        STM_USB_EPR_EP_TYPE_BULK,
492                        STM_USB_EPR_STAT_RX_DISABLED,
493                        STM_USB_EPR_STAT_TX_NAK);
494 #endif
495
496         ao_usb_running = 1;
497 #if AO_USB_DIRECTIO
498         ao_wakeup(&ao_usb_running);
499 #endif
500 }
501
502 static uint16_t control_count;
503 static uint16_t int_count;
504 static uint16_t in_count;
505 static uint16_t out_count;
506 static uint16_t reset_count;
507
508 /* The USB memory must be accessed in 16-bit units
509  */
510
511 static void
512 ao_usb_copy_tx(const uint8_t *src, uint16_t *base, uint16_t bytes)
513 {
514         while (bytes >= 2) {
515                 *base++ = src[0] | (src[1] << 8);
516                 src += 2;
517                 bytes -= 2;
518         }
519         if (bytes)
520                 *base = *src;
521 }
522
523 static void
524 ao_usb_copy_rx(uint8_t *dst, uint16_t *base, uint16_t bytes)
525 {
526         while (bytes >= 2) {
527                 uint16_t s = *base++;
528                 dst[0] = s;
529                 dst[1] = s >> 8;
530                 dst += 2;
531                 bytes -= 2;
532         }
533         if (bytes)
534                 *dst = *base;
535 }
536
537 /* Send an IN data packet */
538 static void
539 ao_usb_ep0_flush(void)
540 {
541         uint8_t this_len;
542
543         /* Check to see if the endpoint is still busy */
544         if (ao_usb_epr_stat_tx(stm_usb.epr[0].r) == STM_USB_EPR_STAT_TX_VALID) {
545                 debug("EP0 not accepting IN data\n");
546                 return;
547         }
548
549         this_len = ao_usb_ep0_in_len;
550         if (this_len > AO_USB_CONTROL_SIZE)
551                 this_len = AO_USB_CONTROL_SIZE;
552
553         if (this_len < AO_USB_CONTROL_SIZE)
554                 ao_usb_ep0_state = AO_USB_EP0_IDLE;
555
556         ao_usb_ep0_in_len -= this_len;
557
558         debug_data ("Flush EP0 len %d:", this_len);
559         ao_usb_copy_tx(ao_usb_ep0_in_data, ao_usb_ep0_tx_buffer, this_len);
560         debug_data ("\n");
561         ao_usb_ep0_in_data += this_len;
562
563         /* Mark the endpoint as TX valid to send the packet */
564         ao_usb_bdt[AO_USB_CONTROL_EPR].single.count_tx = this_len;
565         ao_usb_set_stat_tx(AO_USB_CONTROL_EPR, STM_USB_EPR_STAT_TX_VALID);
566         debug ("queue tx. epr 0 now %08x\n", stm_usb.epr[AO_USB_CONTROL_EPR]);
567 }
568
569 /* Read data from the ep0 OUT fifo */
570 static void
571 ao_usb_ep0_fill(void)
572 {
573         uint16_t        len = ao_usb_bdt[0].single.count_rx & STM_USB_BDT_COUNT_RX_COUNT_RX_MASK;
574
575         if (len > ao_usb_ep0_out_len)
576                 len = ao_usb_ep0_out_len;
577         ao_usb_ep0_out_len -= len;
578
579         /* Pull all of the data out of the packet */
580         debug_data ("Fill EP0 len %d:", len);
581         ao_usb_copy_rx(ao_usb_ep0_out_data, ao_usb_ep0_rx_buffer, len);
582         debug_data ("\n");
583         ao_usb_ep0_out_data += len;
584
585         /* ACK the packet */
586         ao_usb_set_stat_rx(0, STM_USB_EPR_STAT_RX_VALID);
587 }
588
589 static void
590 ao_usb_ep0_in_reset(void)
591 {
592         ao_usb_ep0_in_data = ao_usb_ep0_in_buf;
593         ao_usb_ep0_in_len = 0;
594 }
595
596 static void
597 ao_usb_ep0_in_queue_byte(uint8_t a)
598 {
599         if (ao_usb_ep0_in_len < sizeof (ao_usb_ep0_in_buf))
600                 ao_usb_ep0_in_buf[ao_usb_ep0_in_len++] = a;
601 }
602
603 static void
604 ao_usb_ep0_in_set(const uint8_t *data, uint8_t len)
605 {
606         ao_usb_ep0_in_data = data;
607         ao_usb_ep0_in_len = len;
608 }
609
610 static void
611 ao_usb_ep0_out_set(uint8_t *data, uint8_t len)
612 {
613         ao_usb_ep0_out_data = data;
614         ao_usb_ep0_out_len = len;
615 }
616
617 static void
618 ao_usb_ep0_in_start(uint16_t max)
619 {
620         /* Don't send more than asked for */
621         if (ao_usb_ep0_in_len > max)
622                 ao_usb_ep0_in_len = max;
623         ao_usb_ep0_flush();
624 }
625
626 static struct ao_usb_line_coding ao_usb_line_coding = {115200, 0, 0, 8};
627
628 #if AO_USB_DEVICE_ID_SERIAL
629 static uint8_t ao_usb_serial[2 + 48];
630
631 /* Convert a 32-bit value to 8 hexidecimal UCS2 characters */
632 static void
633 hex_to_ucs2(uint32_t in, uint8_t *out)
634 {
635         int     i;
636
637         for (i = 28; i >= 0; i -= 4) {
638                 uint8_t bits = (in >> i) & 0xf;
639                 *out++ = ((bits < 10) ? '0' : ('a' - 10)) + bits;
640                 *out++ = 0;
641         }
642 }
643
644 /* Encode the device ID (96 bits) in hexidecimal to use as a device
645  * serial number
646  */
647 static void
648 ao_usb_serial_init(void)
649 {
650         ao_usb_serial[0] = 50;  /* length */
651         ao_usb_serial[1] = AO_USB_DESC_STRING;
652         hex_to_ucs2(stm_device_id.u_id0, ao_usb_serial + 2 + 0);
653         hex_to_ucs2(stm_device_id.u_id1, ao_usb_serial + 2 + 16);
654         hex_to_ucs2(stm_device_id.u_id2, ao_usb_serial + 2 + 32);
655 }
656 #endif
657
658 /* Walk through the list of descriptors and find a match
659  */
660 static void
661 ao_usb_get_descriptor(uint16_t value)
662 {
663         const uint8_t           *descriptor;
664         uint8_t         type = value >> 8;
665         uint8_t         index = value;
666
667         descriptor = ao_usb_descriptors;
668         while (descriptor[0] != 0) {
669                 if (descriptor[1] == type && index-- == 0) {
670                         uint8_t len;
671                         if (type == AO_USB_DESC_CONFIGURATION)
672                                 len = descriptor[2];
673                         else
674                                 len = descriptor[0];
675 #if AO_USB_DEVICE_ID_SERIAL
676                         /* Slightly hacky - the serial number is string 3 */
677                         if (type == AO_USB_DESC_STRING && (value & 0xff) == 3) {
678                                 descriptor = ao_usb_serial;
679                                 len = sizeof (ao_usb_serial);
680                         }
681 #endif
682                         ao_usb_ep0_in_set(descriptor, len);
683                         break;
684                 }
685                 descriptor += descriptor[0];
686         }
687 }
688
689 static void
690 ao_usb_ep0_setup(void)
691 {
692         /* Pull the setup packet out of the fifo */
693         ao_usb_ep0_out_set((uint8_t *) &ao_usb_setup, 8);
694         ao_usb_ep0_fill();
695         if (ao_usb_ep0_out_len != 0) {
696                 debug ("invalid setup packet length\n");
697                 return;
698         }
699
700         if ((ao_usb_setup.dir_type_recip & AO_USB_DIR_IN) || ao_usb_setup.length == 0)
701                 ao_usb_ep0_state = AO_USB_EP0_DATA_IN;
702         else
703                 ao_usb_ep0_state = AO_USB_EP0_DATA_OUT;
704
705         ao_usb_ep0_in_reset();
706
707         switch(ao_usb_setup.dir_type_recip & AO_USB_SETUP_TYPE_MASK) {
708         case AO_USB_TYPE_STANDARD:
709                 debug ("Standard setup packet\n");
710                 switch(ao_usb_setup.dir_type_recip & AO_USB_SETUP_RECIP_MASK) {
711                 case AO_USB_RECIP_DEVICE:
712                         debug ("Device setup packet\n");
713                         switch(ao_usb_setup.request) {
714                         case AO_USB_REQ_GET_STATUS:
715                                 debug ("get status\n");
716                                 ao_usb_ep0_in_queue_byte(0);
717                                 ao_usb_ep0_in_queue_byte(0);
718                                 break;
719                         case AO_USB_REQ_SET_ADDRESS:
720                                 debug ("set address %d\n", ao_usb_setup.value);
721                                 ao_usb_address = ao_usb_setup.value;
722                                 ao_usb_address_pending = 1;
723                                 break;
724                         case AO_USB_REQ_GET_DESCRIPTOR:
725                                 debug ("get descriptor %d\n", ao_usb_setup.value);
726                                 ao_usb_get_descriptor(ao_usb_setup.value);
727                                 break;
728                         case AO_USB_REQ_GET_CONFIGURATION:
729                                 debug ("get configuration %d\n", ao_usb_configuration);
730                                 ao_usb_ep0_in_queue_byte(ao_usb_configuration);
731                                 break;
732                         case AO_USB_REQ_SET_CONFIGURATION:
733                                 ao_usb_configuration = ao_usb_setup.value;
734                                 debug ("set configuration %d\n", ao_usb_configuration);
735                                 ao_usb_set_configuration();
736                                 break;
737                         }
738                         break;
739                 case AO_USB_RECIP_INTERFACE:
740                         debug ("Interface setup packet\n");
741                         switch(ao_usb_setup.request) {
742                         case AO_USB_REQ_GET_STATUS:
743                                 ao_usb_ep0_in_queue_byte(0);
744                                 ao_usb_ep0_in_queue_byte(0);
745                                 break;
746                         case AO_USB_REQ_GET_INTERFACE:
747                                 ao_usb_ep0_in_queue_byte(0);
748                                 break;
749                         case AO_USB_REQ_SET_INTERFACE:
750                                 break;
751                         }
752                         break;
753                 case AO_USB_RECIP_ENDPOINT:
754                         debug ("Endpoint setup packet\n");
755                         switch(ao_usb_setup.request) {
756                         case AO_USB_REQ_GET_STATUS:
757                                 ao_usb_ep0_in_queue_byte(0);
758                                 ao_usb_ep0_in_queue_byte(0);
759                                 break;
760                         }
761                         break;
762                 }
763                 break;
764         case AO_USB_TYPE_CLASS:
765                 debug ("Class setup packet\n");
766                 switch (ao_usb_setup.request) {
767                 case AO_USB_SET_LINE_CODING:
768                         debug ("set line coding\n");
769                         ao_usb_ep0_out_set((uint8_t *) &ao_usb_line_coding, 7);
770                         break;
771                 case AO_USB_GET_LINE_CODING:
772                         debug ("get line coding\n");
773                         ao_usb_ep0_in_set((const uint8_t *) &ao_usb_line_coding, 7);
774                         break;
775                 case AO_USB_SET_CONTROL_LINE_STATE:
776                         break;
777                 }
778                 break;
779         }
780
781         /* If we're not waiting to receive data from the host,
782          * queue an IN response
783          */
784         if (ao_usb_ep0_state == AO_USB_EP0_DATA_IN)
785                 ao_usb_ep0_in_start(ao_usb_setup.length);
786 }
787
788 static void
789 ao_usb_ep0_handle(uint8_t receive)
790 {
791         ao_usb_ep0_receive = 0;
792         if (receive & AO_USB_EP0_GOT_SETUP) {
793                 debug ("\tsetup\n");
794                 ao_usb_ep0_setup();
795         }
796         if (receive & AO_USB_EP0_GOT_RX_DATA) {
797                 debug ("\tgot rx data\n");
798                 if (ao_usb_ep0_state == AO_USB_EP0_DATA_OUT) {
799                         ao_usb_ep0_fill();
800                         if (ao_usb_ep0_out_len == 0) {
801                                 ao_usb_ep0_state = AO_USB_EP0_DATA_IN;
802                                 ao_usb_ep0_in_start(0);
803                         }
804                 }
805         }
806         if (receive & AO_USB_EP0_GOT_TX_ACK) {
807                 debug ("\tgot tx ack\n");
808
809 #if HAS_FLIGHT && AO_USB_FORCE_IDLE
810                 ao_flight_force_idle = 1;
811 #endif
812                 /* Wait until the IN packet is received from addr 0
813                  * before assigning our local address
814                  */
815                 if (ao_usb_address_pending)
816                         ao_usb_set_address(ao_usb_address);
817                 if (ao_usb_ep0_state == AO_USB_EP0_DATA_IN)
818                         ao_usb_ep0_flush();
819         }
820 }
821
822 #if AO_POWER_MANAGEMENT
823 void
824 ao_usb_suspend(void)
825 {
826         stm_usb.cntr |= (1 << STM_USB_CNTR_FSUSP);
827         ao_power_suspend();
828         stm_usb.cntr |= (1 << STM_USB_CNTR_LP_MODE);
829         ao_clock_suspend();
830 }
831
832 void
833 ao_usb_wakeup(void)
834 {
835         ao_clock_resume();
836         stm_usb.cntr &= ~(1 << STM_USB_CNTR_FSUSP);
837         ao_power_resume();
838 }
839 #endif
840
841 void
842 stm_usb_isr(void)
843 {
844         uint32_t        istr = stm_usb.istr;
845
846         stm_usb.istr = ~istr;
847         if (istr & (1 << STM_USB_ISTR_CTR)) {
848                 uint8_t         ep = istr & STM_USB_ISTR_EP_ID_MASK;
849                 uint16_t        epr, epr_write;
850
851                 /* Preserve the SW write bits, don't mess with most HW writable bits,
852                  * clear the CTR_RX and CTR_TX bits
853                  */
854                 epr = stm_usb.epr[ep].r;
855                 epr_write = epr;
856                 epr_write &= STM_USB_EPR_PRESERVE_MASK;
857                 epr_write |= STM_USB_EPR_INVARIANT;
858                 epr_write &= ~(1 << STM_USB_EPR_CTR_RX);
859                 epr_write &= ~(1 << STM_USB_EPR_CTR_TX);
860                 stm_usb.epr[ep].r = epr_write;
861
862                 switch (ep) {
863                 case 0:
864                         ++control_count;
865                         if (ao_usb_epr_ctr_rx(epr)) {
866                                 if (ao_usb_epr_setup(epr))
867                                         ao_usb_ep0_receive |= AO_USB_EP0_GOT_SETUP;
868                                 else
869                                         ao_usb_ep0_receive |= AO_USB_EP0_GOT_RX_DATA;
870                         }
871                         if (ao_usb_epr_ctr_tx(epr))
872                                 ao_usb_ep0_receive |= AO_USB_EP0_GOT_TX_ACK;
873                         ao_usb_ep0_handle(ao_usb_ep0_receive);
874                         break;
875                 case AO_USB_OUT_EPR:
876                         ++out_count;
877                         if (ao_usb_epr_ctr_rx(epr)) {
878                                 _rx_dbg1("RX ISR", epr);
879                                 ao_usb_out_avail = 1;
880                                 _rx_dbg0("out avail set");
881                                 ao_wakeup(AO_USB_OUT_SLEEP_ADDR);
882                                 _rx_dbg0("stdin awoken");
883                         }
884                         break;
885                 case AO_USB_IN_EPR:
886                         ++in_count;
887                         _tx_dbg1("TX ISR", epr);
888                         if (ao_usb_epr_ctr_tx(epr)) {
889                                 ao_usb_in_pending = 0;
890                                 ao_wakeup(&ao_usb_in_pending);
891                         }
892                         break;
893 #if AO_USB_HAS_IN2
894                 case AO_USB_IN2_EPR:
895                         ++in2_count;
896                         _tx_dbg1("TX2 ISR", epr);
897                         if (ao_usb_epr_ctr_tx(epr)) {
898                                 ao_usb_in2_pending = 0;
899                                 ao_wakeup(&ao_usb_in2_pending);
900                         }
901                         break;
902 #endif
903                 case AO_USB_INT_EPR:
904                         ++int_count;
905                         if (ao_usb_epr_ctr_tx(epr))
906                                 _ao_usb_set_stat_tx(AO_USB_INT_EPR, STM_USB_EPR_STAT_TX_NAK);
907                         break;
908                 }
909                 return;
910         }
911
912         if (istr & (1 << STM_USB_ISTR_RESET)) {
913                 ++reset_count;
914                 debug ("\treset\n");
915                 ao_usb_set_ep0();
916         }
917 #if AO_POWER_MANAGEMENT
918         if (istr & (1 << STM_USB_ISTR_SUSP)) {
919                 debug ("\tsuspend\n");
920                 ao_usb_suspend();
921         }
922         if (istr & (1 << STM_USB_ISTR_WKUP)) {
923                 debug ("\twakeup\n");
924                 ao_usb_wakeup();
925         }
926 #endif
927 }
928
929 #if AO_USB_HAS_IN
930 /* Queue the current IN buffer for transmission */
931 static void
932 _ao_usb_in_send(void)
933 {
934         _tx_dbg0("in_send start");
935         debug ("send %d\n", ao_usb_tx_count);
936         while (ao_usb_in_pending)
937                 ao_sleep(&ao_usb_in_pending);
938         ao_usb_in_pending = 1;
939         if (ao_usb_tx_count != AO_USB_IN_SIZE)
940                 ao_usb_in_flushed = 1;
941         ao_usb_copy_tx(ao_usb_tx_buffer, ao_usb_in_tx_buffer, ao_usb_tx_count);
942         ao_usb_bdt[AO_USB_IN_EPR].single.addr_tx = ao_usb_in_tx_offset;
943         ao_usb_bdt[AO_USB_IN_EPR].single.count_tx = ao_usb_tx_count;
944         ao_usb_tx_count = 0;
945         _ao_usb_set_stat_tx(AO_USB_IN_EPR, STM_USB_EPR_STAT_TX_VALID);
946         _tx_dbg0("in_send end");
947 }
948
949 /* Wait for a free IN buffer. Interrupts are blocked */
950 static void
951 _ao_usb_in_wait(void)
952 {
953         for (;;) {
954                 /* Check if the current buffer is writable */
955                 if (ao_usb_tx_count < AO_USB_IN_SIZE)
956                         break;
957
958                 _tx_dbg0("in_wait top");
959                 /* Wait for an IN buffer to be ready */
960                 while (ao_usb_in_pending)
961                         ao_sleep(&ao_usb_in_pending);
962                 _tx_dbg0("in_wait bottom");
963         }
964 }
965
966 void
967 ao_usb_flush(void)
968 {
969         if (!ao_usb_running)
970                 return;
971
972         /* Anytime we've sent a character since
973          * the last time we flushed, we'll need
974          * to send a packet -- the only other time
975          * we would send a packet is when that
976          * packet was full, in which case we now
977          * want to send an empty packet
978          */
979         ao_arch_block_interrupts();
980         while (!ao_usb_in_flushed) {
981                 _tx_dbg0("flush top");
982                 _ao_usb_in_send();
983                 _tx_dbg0("flush end");
984         }
985         ao_arch_release_interrupts();
986 }
987
988 void
989 ao_usb_putchar(char c)
990 {
991         if (!ao_usb_running)
992                 return;
993
994         ao_arch_block_interrupts();
995         _ao_usb_in_wait();
996
997         ao_usb_in_flushed = 0;
998         ao_usb_tx_buffer[ao_usb_tx_count++] = (uint8_t) c;
999
1000         /* Send the packet when full */
1001         if (ao_usb_tx_count == AO_USB_IN_SIZE) {
1002                 _tx_dbg0("putchar full");
1003                 _ao_usb_in_send();
1004                 _tx_dbg0("putchar flushed");
1005         }
1006         ao_arch_release_interrupts();
1007 }
1008 #endif
1009
1010 #if AO_USB_HAS_IN2
1011 /* Queue the current IN buffer for transmission */
1012 static void
1013 _ao_usb_in2_send(void)
1014 {
1015         _tx_dbg0("in2_send start");
1016         debug ("send2 %d\n", ao_usb_tx_count);
1017         while (ao_usb_in2_pending)
1018                 ao_sleep(&ao_usb_in2_pending);
1019         ao_usb_in2_pending = 1;
1020         if (ao_usb_tx2_count != AO_USB_IN_SIZE)
1021                 ao_usb_in2_flushed = 1;
1022         ao_usb_copy_tx(ao_usb_tx2_buffer, ao_usb_in2_tx_buffer, ao_usb_tx2_count);
1023         ao_usb_bdt[AO_USB_IN2_EPR].single.addr_tx = ao_usb_in_tx_offset;
1024         ao_usb_bdt[AO_USB_IN2_EPR].single.count_tx = ao_usb_tx_count;
1025         ao_usb_tx2_count = 0;
1026         _ao_usb_set_stat_tx(AO_USB_IN2_EPR, STM_USB_EPR_STAT_TX_VALID);
1027         _tx_dbg0("in2_send end");
1028 }
1029
1030 /* Wait for a free IN buffer. Interrupts are blocked */
1031 static void
1032 _ao_usb_in2_wait(void)
1033 {
1034         for (;;) {
1035                 /* Check if the current buffer is writable */
1036                 if (ao_usb_tx2_count < AO_USB_IN_SIZE)
1037                         break;
1038
1039                 _tx_dbg0("in2_wait top");
1040                 /* Wait for an IN buffer to be ready */
1041                 while (ao_usb_in2_pending)
1042                         ao_sleep(&ao_usb_in2_pending);
1043                 _tx_dbg0("in_wait bottom");
1044         }
1045 }
1046
1047 void
1048 ao_usb_flush2(void)
1049 {
1050         if (!ao_usb_running)
1051                 return;
1052
1053         /* Anytime we've sent a character since
1054          * the last time we flushed, we'll need
1055          * to send a packet -- the only other time
1056          * we would send a packet is when that
1057          * packet was full, in which case we now
1058          * want to send an empty packet
1059          */
1060         ao_arch_block_interrupts();
1061         while (!ao_usb_in2_flushed) {
1062                 _tx_dbg0("flush2 top");
1063                 _ao_usb_in2_send();
1064                 _tx_dbg0("flush2 end");
1065         }
1066         ao_arch_release_interrupts();
1067 }
1068
1069 void
1070 ao_usb_putchar2(char c)
1071 {
1072         if (!ao_usb_running)
1073                 return;
1074
1075         ao_arch_block_interrupts();
1076         _ao_usb_in2_wait();
1077
1078         ao_usb_in2_flushed = 0;
1079         ao_usb_tx2_buffer[ao_usb_tx2_count++] = (uint8_t) c;
1080
1081         /* Send the packet when full */
1082         if (ao_usb_tx2_count == AO_USB_IN_SIZE) {
1083                 _tx_dbg0("putchar2 full");
1084                 _ao_usb_in2_send();
1085                 _tx_dbg0("putchar2 flushed");
1086         }
1087         ao_arch_release_interrupts();
1088 }
1089 #endif
1090
1091 #if AO_USB_HAS_OUT
1092 static void
1093 _ao_usb_out_recv(void)
1094 {
1095         _rx_dbg0("out_recv top");
1096         ao_usb_out_avail = 0;
1097
1098         ao_usb_rx_count = ao_usb_bdt[AO_USB_OUT_EPR].single.count_rx & STM_USB_BDT_COUNT_RX_COUNT_RX_MASK;
1099
1100         _rx_dbg1("out_recv count", ao_usb_rx_count);
1101         debug ("recv %d\n", ao_usb_rx_count);
1102         debug_data("Fill OUT len %d:", ao_usb_rx_count);
1103         ao_usb_copy_rx(ao_usb_rx_buffer, ao_usb_out_rx_buffer, ao_usb_rx_count);
1104         debug_data("\n");
1105         ao_usb_rx_pos = 0;
1106
1107         /* ACK the packet */
1108         _ao_usb_set_stat_rx(AO_USB_OUT_EPR, STM_USB_EPR_STAT_RX_VALID);
1109 }
1110
1111 int
1112 _ao_usb_pollchar(void)
1113 {
1114         uint8_t c;
1115
1116         if (!ao_usb_running)
1117                 return AO_READ_AGAIN;
1118
1119         for (;;) {
1120                 if (ao_usb_rx_pos != ao_usb_rx_count)
1121                         break;
1122
1123                 _rx_dbg0("poll check");
1124                 /* Check to see if a packet has arrived */
1125                 if (!ao_usb_out_avail) {
1126                         _rx_dbg0("poll none");
1127                         return AO_READ_AGAIN;
1128                 }
1129                 _ao_usb_out_recv();
1130         }
1131
1132         /* Pull a character out of the fifo */
1133         c = ao_usb_rx_buffer[ao_usb_rx_pos++];
1134         return c;
1135 }
1136
1137 char
1138 ao_usb_getchar(void)
1139 {
1140         int     c;
1141
1142         ao_arch_block_interrupts();
1143         while ((c = _ao_usb_pollchar()) == AO_READ_AGAIN)
1144                 ao_sleep(AO_USB_OUT_SLEEP_ADDR);
1145         ao_arch_release_interrupts();
1146         return c;
1147 }
1148 #endif
1149
1150 #if AO_USB_DIRECTIO
1151 uint16_t *
1152 ao_usb_alloc(void)
1153 {
1154         uint16_t        *buffer;
1155
1156         buffer = ao_usb_packet_buffer_addr(ao_usb_sram_addr);
1157         ao_usb_sram_addr += AO_USB_IN_SIZE;
1158         return buffer;
1159 }
1160
1161 void
1162 ao_usb_free(uint16_t *addr)
1163 {
1164         uint16_t        offset = ao_usb_packet_buffer_offset(addr);
1165         if (offset < ao_usb_sram_addr)
1166                 ao_usb_sram_addr = offset;
1167 }
1168
1169 void
1170 ao_usb_write(uint16_t *buffer, uint16_t len)
1171 {
1172         ao_arch_block_interrupts();
1173
1174         /* Wait for everything to be ready at the same time */
1175         for (;;) {
1176                 /* Make sure USB is connected */
1177                 if (!ao_usb_running) {
1178                         ao_sleep(&ao_usb_running);
1179                         continue;
1180                 }
1181
1182                 /* Flush any pending regular I/O */
1183                 if (ao_usb_tx_count) {
1184                         _ao_usb_in_send();
1185                         continue;
1186                 }
1187
1188                 /* Wait for an idle IN buffer */
1189                 if (ao_usb_in_pending) {
1190                         ao_sleep(&ao_usb_in_pending);
1191                         continue;
1192                 }
1193                 break;
1194         }
1195
1196         ao_usb_in_pending = 1;
1197         ao_usb_in_flushed = (len != AO_USB_IN_SIZE);
1198         ao_usb_bdt[AO_USB_IN_EPR].single.addr_tx = ao_usb_packet_buffer_offset(buffer);
1199         ao_usb_bdt[AO_USB_IN_EPR].single.count_tx = len;
1200         _ao_usb_set_stat_tx(AO_USB_IN_EPR, STM_USB_EPR_STAT_TX_VALID);
1201         ao_arch_release_interrupts();
1202 }
1203
1204 #if AO_USB_HAS_IN2
1205 void
1206 ao_usb_write2(uint16_t *buffer, uint16_t len)
1207 {
1208         ao_arch_block_interrupts();
1209
1210         /* Wait for everything to be ready at the same time */
1211         for (;;) {
1212                 /* Make sure USB is connected */
1213                 if (!ao_usb_running) {
1214                         ao_sleep(&ao_usb_running);
1215                         continue;
1216                 }
1217
1218                 /* Flush any pending regular I/O */
1219                 if (ao_usb_tx2_count) {
1220                         _ao_usb_in2_send();
1221                         continue;
1222                 }
1223
1224                 /* Wait for an idle IN buffer */
1225                 if (ao_usb_in2_pending) {
1226                         ao_sleep(&ao_usb_in2_pending);
1227                         continue;
1228                 }
1229                 break;
1230         }
1231
1232         ao_usb_in2_pending = 1;
1233         ao_usb_in2_flushed = (len != AO_USB_IN_SIZE);
1234         ao_usb_bdt[AO_USB_IN2_EPR].single.addr_tx = ao_usb_packet_buffer_offset(buffer);
1235         ao_usb_bdt[AO_USB_IN2_EPR].single.count_tx = len;
1236         _ao_usb_set_stat_tx(AO_USB_IN2_EPR, STM_USB_EPR_STAT_TX_VALID);
1237         ao_arch_release_interrupts();
1238 }
1239 #endif
1240 #endif
1241
1242 void
1243 ao_usb_disable(void)
1244 {
1245         ao_arch_block_interrupts();
1246         stm_usb.cntr = (1 << STM_USB_CNTR_FRES);
1247         stm_usb.istr = 0;
1248
1249         /* Disable USB pull-up */
1250         stm_usb.bcdr &= ~(1 << STM_USB_BCDR_DPPU);
1251
1252         /* Switch off the device */
1253         stm_usb.cntr = (1 << STM_USB_CNTR_PDWN) | (1 << STM_USB_CNTR_FRES);
1254
1255         /* Disable the interface */
1256         stm_rcc.apb1enr &= ~(1 << STM_RCC_APB1ENR_USBEN);
1257         ao_arch_release_interrupts();
1258 }
1259
1260 void
1261 ao_usb_enable(void)
1262 {
1263         int     t;
1264
1265         /* Select HSI48 as USB clock source */
1266         stm_rcc.cfgr3 &= ~(1 << STM_RCC_CFGR3_USBSW);
1267
1268         /* Enable USB device */
1269         stm_rcc.apb1enr |= (1 << STM_RCC_APB1ENR_USBEN);
1270
1271         /* Clear reset condition */
1272         stm_rcc.apb1rstr &= ~(1 << STM_RCC_APB1RSTR_USBRST);
1273
1274         /* Disable USB pull-up */
1275         stm_usb.bcdr &= ~(1 << STM_USB_BCDR_DPPU);
1276
1277         /* Do not touch the GPIOA configuration; USB takes priority
1278          * over GPIO on pins A11 and A12, but if you select alternate
1279          * input 10 (the documented correct selection), then USB is
1280          * pulled low and doesn't work at all
1281          */
1282
1283         ao_arch_block_interrupts();
1284
1285         /* Route interrupts */
1286         stm_nvic_set_enable(STM_ISR_USB_POS);
1287         stm_nvic_set_priority(STM_ISR_USB_POS, 3);
1288
1289         ao_usb_configuration = 0;
1290
1291         /* Set up buffer descriptors */
1292         ao_usb_init_btable();
1293
1294         /* Reset the USB controller */
1295         stm_usb.cntr = (1 << STM_USB_CNTR_FRES);
1296
1297         /* Clear the reset bit */
1298         stm_usb.cntr = 0;
1299
1300         /* Clear any spurious interrupts */
1301         stm_usb.istr = 0;
1302
1303         ao_usb_set_ep0();
1304
1305         debug ("ao_usb_enable\n");
1306
1307         /* Enable interrupts */
1308         stm_usb.cntr = ((1 << STM_USB_CNTR_CTRM) |
1309                         (0 << STM_USB_CNTR_PMAOVRM) |
1310                         (0 << STM_USB_CNTR_ERRM) |
1311                         (AO_POWER_MANAGEMENT << STM_USB_CNTR_WKUPM) |
1312                         (AO_POWER_MANAGEMENT << STM_USB_CNTR_SUSPM) |
1313                         (1 << STM_USB_CNTR_RESETM) |
1314                         (0 << STM_USB_CNTR_SOFM) |
1315                         (0 << STM_USB_CNTR_ESOFM) |
1316                         (0 << STM_USB_CNTR_RESUME) |
1317                         (0 << STM_USB_CNTR_FSUSP) |
1318                         (0 << STM_USB_CNTR_LP_MODE) |
1319                         (0 << STM_USB_CNTR_PDWN) |
1320                         (0 << STM_USB_CNTR_FRES));
1321
1322         ao_arch_release_interrupts();
1323
1324         for (t = 0; t < 1000; t++)
1325                 ao_arch_nop();
1326
1327         /* Enable USB pull-up */
1328         stm_usb.bcdr |= (1 << STM_USB_BCDR_DPPU);
1329 }
1330
1331 #if USB_ECHO
1332 struct ao_task ao_usb_echo_task;
1333
1334 static void
1335 ao_usb_echo(void)
1336 {
1337         char    c;
1338
1339         for (;;) {
1340                 c = ao_usb_getchar();
1341                 ao_usb_putchar(c);
1342                 ao_usb_flush();
1343         }
1344 }
1345 #endif
1346
1347 #if USB_STATUS
1348 static void
1349 ao_usb_irq(void)
1350 {
1351         printf ("control: %d out: %d in: %d int: %d reset: %d\n",
1352                 control_count, out_count, in_count, int_count, reset_count);
1353 }
1354
1355 __code struct ao_cmds ao_usb_cmds[] = {
1356         { ao_usb_irq, "I\0Show USB interrupt counts" },
1357         { 0, NULL }
1358 };
1359 #endif
1360
1361 void
1362 ao_usb_init(void)
1363 {
1364         /* Turn on syscfg */
1365         stm_rcc.apb2enr |= (1 << STM_RCC_APB2ENR_SYSCFGCOMPEN);
1366
1367         /* Set PA11/PA12 remapping bit */
1368         stm_syscfg.cfgr1 |= (AO_PA11_PA12_RMP << STM_SYSCFG_CFGR1_PA11_PA12_RMP);
1369
1370 #ifndef AO_USB_START_DISABLED
1371         ao_usb_enable();
1372 #endif
1373
1374 #if AO_USB_DEVICE_ID_SERIAL
1375         ao_usb_serial_init();
1376 #endif
1377
1378         debug ("ao_usb_init\n");
1379         ao_usb_ep0_state = AO_USB_EP0_IDLE;
1380
1381         ao_usb_alloc_buffers();
1382
1383 #if USB_ECHO
1384         ao_add_task(&ao_usb_echo_task, ao_usb_echo, "usb echo");
1385 #endif
1386 #if USB_STATUS
1387         ao_cmd_register(&ao_usb_cmds[0]);
1388 #endif
1389 #if !USB_ECHO
1390 #if USE_USB_STDIO
1391         ao_add_stdio(_ao_usb_pollchar, ao_usb_putchar, ao_usb_flush);
1392 #endif
1393 #endif
1394 }
1395
1396 #if TX_DBG || RX_DBG
1397
1398 struct ao_usb_dbg {
1399         int             line;
1400         char            *msg;
1401         uint32_t        value;
1402         uint32_t        primask;
1403 #if TX_DBG
1404         uint16_t        in_count;
1405         uint32_t        in_epr;
1406         uint32_t        in_pending;
1407         uint32_t        tx_count;
1408         uint32_t        in_flushed;
1409 #endif
1410 #if RX_DBG
1411         uint8_t         rx_count;
1412         uint8_t         rx_pos;
1413         uint8_t         out_avail;
1414         uint32_t        out_epr;
1415 #endif
1416 };
1417
1418 #define NUM_USB_DBG     128
1419
1420 static struct ao_usb_dbg dbg[128];
1421 static int dbg_i;
1422
1423 static void _dbg(int line, char *msg, uint32_t value)
1424 {
1425         uint32_t        primask;
1426         dbg[dbg_i].line = line;
1427         dbg[dbg_i].msg = msg;
1428         dbg[dbg_i].value = value;
1429         asm("mrs %0,primask" : "=&r" (primask));
1430         dbg[dbg_i].primask = primask;
1431 #if TX_DBG
1432         dbg[dbg_i].in_count = in_count;
1433         dbg[dbg_i].in_epr = stm_usb.epr[AO_USB_IN_EPR];
1434         dbg[dbg_i].in_pending = ao_usb_in_pending;
1435         dbg[dbg_i].tx_count = ao_usb_tx_count;
1436         dbg[dbg_i].in_flushed = ao_usb_in_flushed;
1437 #endif
1438 #if RX_DBG
1439         dbg[dbg_i].rx_count = ao_usb_rx_count;
1440         dbg[dbg_i].rx_pos = ao_usb_rx_pos;
1441         dbg[dbg_i].out_avail = ao_usb_out_avail;
1442         dbg[dbg_i].out_epr = stm_usb.epr[AO_USB_OUT_EPR];
1443 #endif
1444         if (++dbg_i == NUM_USB_DBG)
1445                 dbg_i = 0;
1446 }
1447 #endif