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