Switch from GPLv2 to GPLv2+
[fw/altos] / src / stm / 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
23 #define USB_DEBUG       0
24 #define USB_DEBUG_DATA  0
25 #define USB_ECHO        0
26
27 #ifndef USE_USB_STDIO
28 #define USE_USB_STDIO   1
29 #endif
30
31 #if USE_USB_STDIO
32 #define AO_USB_OUT_SLEEP_ADDR   (&ao_stdin_ready)
33 #else
34 #define AO_USB_OUT_SLEEP_ADDR   (&ao_usb_out_avail)
35 #endif
36
37 #if USB_DEBUG
38 #define debug(format, args...)  printf(format, ## args);
39 #else
40 #define debug(format, args...)
41 #endif
42
43 #if USB_DEBUG_DATA
44 #define debug_data(format, args...)     printf(format, ## args);
45 #else
46 #define debug_data(format, args...)
47 #endif
48
49 struct ao_usb_setup {
50         uint8_t         dir_type_recip;
51         uint8_t         request;
52         uint16_t        value;
53         uint16_t        index;
54         uint16_t        length;
55 } ao_usb_setup;
56
57 static uint8_t  ao_usb_ep0_state;
58
59 /* Pending EP0 IN data */
60 static const uint8_t    *ao_usb_ep0_in_data;    /* Remaining data */
61 static uint8_t          ao_usb_ep0_in_len;      /* Remaining amount */
62
63 /* Temp buffer for smaller EP0 in data */
64 static uint8_t  ao_usb_ep0_in_buf[2];
65
66 /* Pending EP0 OUT data */
67 static uint8_t *ao_usb_ep0_out_data;
68 static uint8_t  ao_usb_ep0_out_len;
69
70 /*
71  * Objects allocated in special USB memory
72  */
73
74 /* Buffer description tables */
75 static union stm_usb_bdt        *ao_usb_bdt;
76 /* USB address of end of allocated storage */
77 static uint16_t ao_usb_sram_addr;
78
79 /* Pointer to ep0 tx/rx buffers in USB memory */
80 static uint32_t *ao_usb_ep0_tx_buffer;
81 static uint32_t *ao_usb_ep0_rx_buffer;
82
83 /* Pointer to bulk data tx/rx buffers in USB memory */
84 static uint32_t *ao_usb_in_tx_buffer;
85 static uint32_t *ao_usb_out_rx_buffer;
86
87 /* System ram shadow of USB buffer; writing individual bytes is
88  * too much of a pain (sigh) */
89 static uint8_t  ao_usb_tx_buffer[AO_USB_IN_SIZE];
90 static uint8_t  ao_usb_tx_count;
91
92 static uint8_t  ao_usb_rx_buffer[AO_USB_OUT_SIZE];
93 static uint8_t  ao_usb_rx_count, ao_usb_rx_pos;
94
95 /*
96  * End point register indices
97  */
98
99 #define AO_USB_CONTROL_EPR      0
100 #define AO_USB_INT_EPR          1
101 #define AO_USB_OUT_EPR          2
102 #define AO_USB_IN_EPR           3
103
104 /* Marks when we don't need to send an IN packet.
105  * This happens only when the last IN packet is not full,
106  * otherwise the host will expect to keep seeing packets.
107  * Send a zero-length packet as required
108  */
109 static uint8_t  ao_usb_in_flushed;
110
111 /* Marks when we have delivered an IN packet to the hardware
112  * and it has not been received yet. ao_sleep on this address
113  * to wait for it to be delivered.
114  */
115 static uint8_t  ao_usb_in_pending;
116
117 /* Marks when an OUT packet has been received by the hardware
118  * but not pulled to the shadow buffer.
119  */
120 static uint8_t  ao_usb_out_avail;
121 uint8_t         ao_usb_running;
122 static uint8_t  ao_usb_configuration;
123
124 #define AO_USB_EP0_GOT_RESET    1
125 #define AO_USB_EP0_GOT_SETUP    2
126 #define AO_USB_EP0_GOT_RX_DATA  4
127 #define AO_USB_EP0_GOT_TX_ACK   8
128
129 static uint8_t  ao_usb_ep0_receive;
130 static uint8_t  ao_usb_address;
131 static uint8_t  ao_usb_address_pending;
132
133 static inline uint32_t set_toggle(uint32_t      current_value,
134                                    uint32_t     mask,
135                                    uint32_t     desired_value)
136 {
137         return (current_value ^ desired_value) & mask;
138 }
139
140 static inline uint32_t *ao_usb_packet_buffer_addr(uint16_t sram_addr)
141 {
142         return (uint32_t *) (stm_usb_sram + 2 * sram_addr);
143 }
144
145 static inline uint32_t ao_usb_epr_stat_rx(uint32_t epr) {
146         return (epr >> STM_USB_EPR_STAT_RX) & STM_USB_EPR_STAT_RX_MASK;
147 }
148
149 static inline uint32_t ao_usb_epr_stat_tx(uint32_t epr) {
150         return (epr >> STM_USB_EPR_STAT_TX) & STM_USB_EPR_STAT_TX_MASK;
151 }
152
153 static inline uint32_t ao_usb_epr_ctr_rx(uint32_t epr) {
154         return (epr >> STM_USB_EPR_CTR_RX) & 1;
155 }
156
157 static inline uint32_t ao_usb_epr_ctr_tx(uint32_t epr) {
158         return (epr >> STM_USB_EPR_CTR_TX) & 1;
159 }
160
161 static inline uint32_t ao_usb_epr_setup(uint32_t epr) {
162         return (epr >> STM_USB_EPR_SETUP) & 1;
163 }
164
165 static inline uint32_t ao_usb_epr_dtog_rx(uint32_t epr) {
166         return (epr >> STM_USB_EPR_DTOG_RX) & 1;
167 }
168
169 static inline uint32_t ao_usb_epr_dtog_tx(uint32_t epr) {
170         return (epr >> STM_USB_EPR_DTOG_TX) & 1;
171 }
172
173 /*
174  * Set current device address and mark the
175  * interface as active
176  */
177 void
178 ao_usb_set_address(uint8_t address)
179 {
180         debug("ao_usb_set_address %02x\n", address);
181         stm_usb.daddr = (1 << STM_USB_DADDR_EF) | address;
182         ao_usb_address_pending = 0;
183 }
184
185 /*
186  * Write these values to preserve register contents under HW changes
187  */
188
189 #define STM_USB_EPR_INVARIANT   ((1 << STM_USB_EPR_CTR_RX) |            \
190                                  (STM_USB_EPR_DTOG_RX_WRITE_INVARIANT << STM_USB_EPR_DTOG_RX) | \
191                                  (STM_USB_EPR_STAT_RX_WRITE_INVARIANT << STM_USB_EPR_STAT_RX) | \
192                                  (1 << STM_USB_EPR_CTR_TX) |            \
193                                  (STM_USB_EPR_DTOG_TX_WRITE_INVARIANT << STM_USB_EPR_DTOG_TX) | \
194                                  (STM_USB_EPR_STAT_TX_WRITE_INVARIANT << STM_USB_EPR_STAT_TX))
195
196 #define STM_USB_EPR_INVARIANT_MASK      ((1 << STM_USB_EPR_CTR_RX) |    \
197                                          (STM_USB_EPR_DTOG_RX_MASK << STM_USB_EPR_DTOG_RX) | \
198                                          (STM_USB_EPR_STAT_RX_MASK << STM_USB_EPR_STAT_RX) | \
199                                          (1 << STM_USB_EPR_CTR_TX) |    \
200                                          (STM_USB_EPR_DTOG_TX_MASK << STM_USB_EPR_DTOG_TX) | \
201                                          (STM_USB_EPR_STAT_TX_MASK << STM_USB_EPR_STAT_TX))
202
203 /*
204  * These bits are purely under sw control, so preserve them in the
205  * register by re-writing what was read
206  */
207 #define STM_USB_EPR_PRESERVE_MASK       ((STM_USB_EPR_EP_TYPE_MASK << STM_USB_EPR_EP_TYPE) | \
208                                          (1 << STM_USB_EPR_EP_KIND) |   \
209                                          (STM_USB_EPR_EA_MASK << STM_USB_EPR_EA))
210
211 #define TX_DBG 0
212 #define RX_DBG 0
213
214 #if TX_DBG
215 #define _tx_dbg0(msg) _dbg(__LINE__,msg,0)
216 #define _tx_dbg1(msg,value) _dbg(__LINE__,msg,value)
217 #else
218 #define _tx_dbg0(msg)
219 #define _tx_dbg1(msg,value)
220 #endif
221
222 #if RX_DBG
223 #define _rx_dbg0(msg) _dbg(__LINE__,msg,0)
224 #define _rx_dbg1(msg,value) _dbg(__LINE__,msg,value)
225 #else
226 #define _rx_dbg0(msg)
227 #define _rx_dbg1(msg,value)
228 #endif
229
230 #if TX_DBG || RX_DBG
231 static void _dbg(int line, char *msg, uint32_t value);
232 #endif
233
234 /*
235  * Set the state of the specified endpoint register to a new
236  * value. This is tricky because the bits toggle where the new
237  * value is one, and we need to write invariant values in other
238  * spots of the register. This hardware is strange...
239  */
240 static void
241 _ao_usb_set_stat_tx(int ep, uint32_t stat_tx)
242 {
243         uint32_t        epr_write, epr_old;
244
245         _tx_dbg1("set_stat_tx top", stat_tx);
246         epr_old = epr_write = stm_usb.epr[ep];
247         epr_write &= STM_USB_EPR_PRESERVE_MASK;
248         epr_write |= STM_USB_EPR_INVARIANT;
249         epr_write |= set_toggle(epr_old,
250                               STM_USB_EPR_STAT_TX_MASK << STM_USB_EPR_STAT_TX,
251                               stat_tx << STM_USB_EPR_STAT_TX);
252         stm_usb.epr[ep] = epr_write;
253         _tx_dbg1("set_stat_tx bottom", epr_write);
254 }
255
256 static void
257 ao_usb_set_stat_tx(int ep, uint32_t stat_tx)
258 {
259         ao_arch_block_interrupts();
260         _ao_usb_set_stat_tx(ep, stat_tx);
261         ao_arch_release_interrupts();
262 }
263
264 static void
265 _ao_usb_set_stat_rx(int ep, uint32_t stat_rx) {
266         uint32_t        epr_write, epr_old;
267
268         epr_write = epr_old = stm_usb.epr[ep];
269         epr_write &= STM_USB_EPR_PRESERVE_MASK;
270         epr_write |= STM_USB_EPR_INVARIANT;
271         epr_write |= set_toggle(epr_old,
272                               STM_USB_EPR_STAT_RX_MASK << STM_USB_EPR_STAT_RX,
273                               stat_rx << STM_USB_EPR_STAT_RX);
274         stm_usb.epr[ep] = epr_write;
275 }
276
277 static void
278 ao_usb_set_stat_rx(int ep, uint32_t stat_rx) {
279         ao_arch_block_interrupts();
280         _ao_usb_set_stat_rx(ep, stat_rx);
281         ao_arch_release_interrupts();
282 }
283
284 /*
285  * Set just endpoint 0, for use during startup
286  */
287
288 static void
289 ao_usb_init_ep(uint8_t ep, uint32_t addr, uint32_t type, uint32_t stat_rx, uint32_t stat_tx)
290 {
291         uint32_t                epr;
292         ao_arch_block_interrupts();
293         epr = stm_usb.epr[ep];
294         epr = ((0 << STM_USB_EPR_CTR_RX) |
295                (epr & (1 << STM_USB_EPR_DTOG_RX)) |
296                set_toggle(epr,
297                           (STM_USB_EPR_STAT_RX_MASK << STM_USB_EPR_STAT_RX),
298                           (stat_rx << STM_USB_EPR_STAT_RX)) |
299                (type << STM_USB_EPR_EP_TYPE) |
300                (0 << STM_USB_EPR_EP_KIND) |
301                (0 << STM_USB_EPR_CTR_TX) |
302                (epr & (1 << STM_USB_EPR_DTOG_TX)) |
303                set_toggle(epr,
304                           (STM_USB_EPR_STAT_TX_MASK << STM_USB_EPR_STAT_TX),
305                           (stat_tx << STM_USB_EPR_STAT_TX)) |
306                (addr << STM_USB_EPR_EA));
307         stm_usb.epr[ep] = epr;
308         ao_arch_release_interrupts();
309         debug ("writing epr[%d] 0x%08x wrote 0x%08x\n",
310                ep, epr, stm_usb.epr[ep]);
311 }
312
313 static void
314 ao_usb_set_ep0(void)
315 {
316         int                     e;
317
318         ao_usb_sram_addr = 0;
319
320         /* buffer table is at the start of USB memory */
321         stm_usb.btable = 0;
322         ao_usb_bdt = (void *) stm_usb_sram;
323
324         ao_usb_sram_addr += 8 * STM_USB_BDT_SIZE;
325
326         /* Set up EP 0 - a Control end point with 32 bytes of in and out buffers */
327
328         ao_usb_bdt[0].single.addr_tx = ao_usb_sram_addr;
329         ao_usb_bdt[0].single.count_tx = 0;
330         ao_usb_ep0_tx_buffer = ao_usb_packet_buffer_addr(ao_usb_sram_addr);
331         ao_usb_sram_addr += AO_USB_CONTROL_SIZE;
332
333         ao_usb_bdt[0].single.addr_rx = ao_usb_sram_addr;
334         ao_usb_bdt[0].single.count_rx = ((1 << STM_USB_BDT_COUNT_RX_BL_SIZE) |
335                                   (((AO_USB_CONTROL_SIZE / 32) - 1) << STM_USB_BDT_COUNT_RX_NUM_BLOCK));
336         ao_usb_ep0_rx_buffer = ao_usb_packet_buffer_addr(ao_usb_sram_addr);
337         ao_usb_sram_addr += AO_USB_CONTROL_SIZE;
338
339         ao_usb_init_ep(AO_USB_CONTROL_EPR, AO_USB_CONTROL_EP,
340                        STM_USB_EPR_EP_TYPE_CONTROL,
341                        STM_USB_EPR_STAT_RX_VALID,
342                        STM_USB_EPR_STAT_TX_NAK);
343
344         /* Clear all of the other endpoints */
345         for (e = 1; e < 8; e++) {
346                 ao_usb_init_ep(e, 0,
347                                STM_USB_EPR_EP_TYPE_CONTROL,
348                                STM_USB_EPR_STAT_RX_DISABLED,
349                                STM_USB_EPR_STAT_TX_DISABLED);
350         }
351
352         ao_usb_set_address(0);
353
354         ao_usb_running = 0;
355
356         /* Reset our internal state
357          */
358
359         ao_usb_ep0_state = AO_USB_EP0_IDLE;
360
361         ao_usb_ep0_in_data = NULL;
362         ao_usb_ep0_in_len = 0;
363
364         ao_usb_ep0_out_data = 0;
365         ao_usb_ep0_out_len = 0;
366 }
367
368 static void
369 ao_usb_set_configuration(void)
370 {
371         debug ("ao_usb_set_configuration\n");
372
373         /* Set up the INT end point */
374         ao_usb_bdt[AO_USB_INT_EPR].single.addr_tx = ao_usb_sram_addr;
375         ao_usb_bdt[AO_USB_INT_EPR].single.count_tx = 0;
376         ao_usb_in_tx_buffer = ao_usb_packet_buffer_addr(ao_usb_sram_addr);
377         ao_usb_sram_addr += AO_USB_INT_SIZE;
378
379         ao_usb_init_ep(AO_USB_INT_EPR,
380                        AO_USB_INT_EP,
381                        STM_USB_EPR_EP_TYPE_INTERRUPT,
382                        STM_USB_EPR_STAT_RX_DISABLED,
383                        STM_USB_EPR_STAT_TX_NAK);
384
385         /* Set up the OUT end point */
386         ao_usb_bdt[AO_USB_OUT_EPR].single.addr_rx = ao_usb_sram_addr;
387         ao_usb_bdt[AO_USB_OUT_EPR].single.count_rx = ((1 << STM_USB_BDT_COUNT_RX_BL_SIZE) |
388                                                       (((AO_USB_OUT_SIZE / 32) - 1) << STM_USB_BDT_COUNT_RX_NUM_BLOCK));
389         ao_usb_out_rx_buffer = ao_usb_packet_buffer_addr(ao_usb_sram_addr);
390         ao_usb_sram_addr += AO_USB_OUT_SIZE;
391
392         ao_usb_init_ep(AO_USB_OUT_EPR,
393                        AO_USB_OUT_EP,
394                        STM_USB_EPR_EP_TYPE_BULK,
395                        STM_USB_EPR_STAT_RX_VALID,
396                        STM_USB_EPR_STAT_TX_DISABLED);
397
398         /* Set up the IN end point */
399         ao_usb_bdt[AO_USB_IN_EPR].single.addr_tx = ao_usb_sram_addr;
400         ao_usb_bdt[AO_USB_IN_EPR].single.count_tx = 0;
401         ao_usb_in_tx_buffer = ao_usb_packet_buffer_addr(ao_usb_sram_addr);
402         ao_usb_sram_addr += AO_USB_IN_SIZE;
403
404         ao_usb_init_ep(AO_USB_IN_EPR,
405                        AO_USB_IN_EP,
406                        STM_USB_EPR_EP_TYPE_BULK,
407                        STM_USB_EPR_STAT_RX_DISABLED,
408                        STM_USB_EPR_STAT_TX_NAK);
409
410         ao_usb_in_flushed = 0;
411         ao_usb_in_pending = 0;
412         ao_wakeup(&ao_usb_in_pending);
413
414         ao_usb_out_avail = 0;
415         ao_usb_configuration = 0;
416
417         ao_usb_running = 1;
418         ao_wakeup(&ao_usb_running);
419 }
420
421 static uint16_t control_count;
422 static uint16_t int_count;
423 static uint16_t in_count;
424 static uint16_t out_count;
425 static uint16_t reset_count;
426
427 /* The USB memory holds 16 bit values on 32 bit boundaries
428  * and must be accessed only in 32 bit units. Sigh.
429  */
430
431 static inline void
432 ao_usb_write_byte(uint8_t byte, uint32_t *base, uint16_t offset)
433 {
434         base += offset >> 1;
435         if (offset & 1) {
436                 *base = (*base & 0xff) | ((uint32_t) byte << 8);
437         } else {
438                 *base = (*base & 0xff00) | byte;
439         }
440 }
441
442 static inline void
443 ao_usb_write_short(uint16_t data, uint32_t *base, uint16_t offset)
444 {
445         base[offset>>1] = data;
446 }
447
448 static void
449 ao_usb_write(const uint8_t *src, uint32_t *base, uint16_t offset, uint16_t bytes)
450 {
451         if (!bytes)
452                 return;
453         if (offset & 1) {
454                 debug_data (" %02x", src[0]);
455                 ao_usb_write_byte(*src++, base, offset++);
456                 bytes--;
457         }
458         while (bytes >= 2) {
459                 debug_data (" %02x %02x", src[0], src[1]);
460                 ao_usb_write_short((src[1] << 8) | src[0], base, offset);
461                 offset += 2;
462                 src += 2;
463                 bytes -= 2;
464         }
465         if (bytes) {
466                 debug_data (" %02x", src[0]);
467                 ao_usb_write_byte(*src, base, offset);
468         }
469 }
470
471 static inline uint8_t
472 ao_usb_read_byte(uint32_t *base, uint16_t offset)
473 {
474         base += offset >> 1;
475         if (offset & 1)
476                 return (*base >> 8) & 0xff;
477         else
478                 return *base & 0xff;
479 }
480
481 static inline uint16_t
482 ao_usb_read_short(uint32_t *base, uint16_t offset)
483 {
484         return base[offset>>1];
485 }
486
487 static void
488 ao_usb_read(uint8_t *dst, uint32_t *base, uint16_t offset, uint16_t bytes)
489 {
490         if (!bytes)
491                 return;
492         if (offset & 1) {
493                 *dst++ = ao_usb_read_byte(base, offset++);
494                 debug_data (" %02x", dst[-1]);
495                 bytes--;
496         }
497         while (bytes >= 2) {
498                 uint16_t        s = ao_usb_read_short(base, offset);
499                 dst[0] = s;
500                 dst[1] = s >> 8;
501                 debug_data (" %02x %02x", dst[0], dst[1]);
502                 offset += 2;
503                 dst += 2;
504                 bytes -= 2;
505         }
506         if (bytes) {
507                 *dst = ao_usb_read_byte(base, offset);
508                 debug_data (" %02x", dst[0]);
509         }
510 }
511
512 /* Send an IN data packet */
513 static void
514 ao_usb_ep0_flush(void)
515 {
516         uint8_t this_len;
517
518         /* Check to see if the endpoint is still busy */
519         if (ao_usb_epr_stat_tx(stm_usb.epr[0]) == STM_USB_EPR_STAT_TX_VALID) {
520                 debug("EP0 not accepting IN data\n");
521                 return;
522         }
523
524         this_len = ao_usb_ep0_in_len;
525         if (this_len > AO_USB_CONTROL_SIZE)
526                 this_len = AO_USB_CONTROL_SIZE;
527
528         if (this_len < AO_USB_CONTROL_SIZE)
529                 ao_usb_ep0_state = AO_USB_EP0_IDLE;
530
531         ao_usb_ep0_in_len -= this_len;
532
533         debug_data ("Flush EP0 len %d:", this_len);
534         ao_usb_write(ao_usb_ep0_in_data, ao_usb_ep0_tx_buffer, 0, this_len);
535         debug_data ("\n");
536         ao_usb_ep0_in_data += this_len;
537
538         /* Mark the endpoint as TX valid to send the packet */
539         ao_usb_bdt[AO_USB_CONTROL_EPR].single.count_tx = this_len;
540         ao_usb_set_stat_tx(AO_USB_CONTROL_EPR, STM_USB_EPR_STAT_TX_VALID);
541         debug ("queue tx. epr 0 now %08x\n", stm_usb.epr[AO_USB_CONTROL_EPR]);
542 }
543
544 /* Read data from the ep0 OUT fifo */
545 static void
546 ao_usb_ep0_fill(void)
547 {
548         uint16_t        len = ao_usb_bdt[0].single.count_rx & STM_USB_BDT_COUNT_RX_COUNT_RX_MASK;
549
550         if (len > ao_usb_ep0_out_len)
551                 len = ao_usb_ep0_out_len;
552         ao_usb_ep0_out_len -= len;
553
554         /* Pull all of the data out of the packet */
555         debug_data ("Fill EP0 len %d:", len);
556         ao_usb_read(ao_usb_ep0_out_data, ao_usb_ep0_rx_buffer, 0, len);
557         debug_data ("\n");
558         ao_usb_ep0_out_data += len;
559
560         /* ACK the packet */
561         ao_usb_set_stat_rx(0, STM_USB_EPR_STAT_RX_VALID);
562 }
563
564 static void
565 ao_usb_ep0_in_reset(void)
566 {
567         ao_usb_ep0_in_data = ao_usb_ep0_in_buf;
568         ao_usb_ep0_in_len = 0;
569 }
570
571 static void
572 ao_usb_ep0_in_queue_byte(uint8_t a)
573 {
574         if (ao_usb_ep0_in_len < sizeof (ao_usb_ep0_in_buf))
575                 ao_usb_ep0_in_buf[ao_usb_ep0_in_len++] = a;
576 }
577
578 static void
579 ao_usb_ep0_in_set(const uint8_t *data, uint8_t len)
580 {
581         ao_usb_ep0_in_data = data;
582         ao_usb_ep0_in_len = len;
583 }
584
585 static void
586 ao_usb_ep0_out_set(uint8_t *data, uint8_t len)
587 {
588         ao_usb_ep0_out_data = data;
589         ao_usb_ep0_out_len = len;
590 }
591
592 static void
593 ao_usb_ep0_in_start(uint16_t max)
594 {
595         /* Don't send more than asked for */
596         if (ao_usb_ep0_in_len > max)
597                 ao_usb_ep0_in_len = max;
598         ao_usb_ep0_flush();
599 }
600
601 static struct ao_usb_line_coding ao_usb_line_coding = {115200, 0, 0, 8};
602
603 /* Walk through the list of descriptors and find a match
604  */
605 static void
606 ao_usb_get_descriptor(uint16_t value, uint16_t length)
607 {
608         const uint8_t           *descriptor;
609         uint8_t         type = value >> 8;
610         uint8_t         index = value;
611
612         descriptor = ao_usb_descriptors;
613         while (descriptor[0] != 0) {
614                 if (descriptor[1] == type && index-- == 0) {
615                         uint8_t len;
616                         if (type == AO_USB_DESC_CONFIGURATION)
617                                 len = descriptor[2];
618                         else
619                                 len = descriptor[0];
620                         if (len > length)
621                                 len = length;
622                         ao_usb_ep0_in_set(descriptor, len);
623                         break;
624                 }
625                 descriptor += descriptor[0];
626         }
627 }
628
629 static void
630 ao_usb_ep0_setup(void)
631 {
632         /* Pull the setup packet out of the fifo */
633         ao_usb_ep0_out_set((uint8_t *) &ao_usb_setup, 8);
634         ao_usb_ep0_fill();
635         if (ao_usb_ep0_out_len != 0) {
636                 debug ("invalid setup packet length\n");
637                 return;
638         }
639
640         if ((ao_usb_setup.dir_type_recip & AO_USB_DIR_IN) || ao_usb_setup.length == 0)
641                 ao_usb_ep0_state = AO_USB_EP0_DATA_IN;
642         else
643                 ao_usb_ep0_state = AO_USB_EP0_DATA_OUT;
644
645         ao_usb_ep0_in_reset();
646
647         switch(ao_usb_setup.dir_type_recip & AO_USB_SETUP_TYPE_MASK) {
648         case AO_USB_TYPE_STANDARD:
649                 debug ("Standard setup packet\n");
650                 switch(ao_usb_setup.dir_type_recip & AO_USB_SETUP_RECIP_MASK) {
651                 case AO_USB_RECIP_DEVICE:
652                         debug ("Device setup packet\n");
653                         switch(ao_usb_setup.request) {
654                         case AO_USB_REQ_GET_STATUS:
655                                 debug ("get status\n");
656                                 ao_usb_ep0_in_queue_byte(0);
657                                 ao_usb_ep0_in_queue_byte(0);
658                                 break;
659                         case AO_USB_REQ_SET_ADDRESS:
660                                 debug ("set address %d\n", ao_usb_setup.value);
661                                 ao_usb_address = ao_usb_setup.value;
662                                 ao_usb_address_pending = 1;
663                                 break;
664                         case AO_USB_REQ_GET_DESCRIPTOR:
665                                 debug ("get descriptor %d\n", ao_usb_setup.value);
666                                 ao_usb_get_descriptor(ao_usb_setup.value, ao_usb_setup.length);
667                                 break;
668                         case AO_USB_REQ_GET_CONFIGURATION:
669                                 debug ("get configuration %d\n", ao_usb_configuration);
670                                 ao_usb_ep0_in_queue_byte(ao_usb_configuration);
671                                 break;
672                         case AO_USB_REQ_SET_CONFIGURATION:
673                                 ao_usb_configuration = ao_usb_setup.value;
674                                 debug ("set configuration %d\n", ao_usb_configuration);
675                                 ao_usb_set_configuration();
676                                 break;
677                         }
678                         break;
679                 case AO_USB_RECIP_INTERFACE:
680                         debug ("Interface setup packet\n");
681                         switch(ao_usb_setup.request) {
682                         case AO_USB_REQ_GET_STATUS:
683                                 ao_usb_ep0_in_queue_byte(0);
684                                 ao_usb_ep0_in_queue_byte(0);
685                                 break;
686                         case AO_USB_REQ_GET_INTERFACE:
687                                 ao_usb_ep0_in_queue_byte(0);
688                                 break;
689                         case AO_USB_REQ_SET_INTERFACE:
690                                 break;
691                         }
692                         break;
693                 case AO_USB_RECIP_ENDPOINT:
694                         debug ("Endpoint setup packet\n");
695                         switch(ao_usb_setup.request) {
696                         case AO_USB_REQ_GET_STATUS:
697                                 ao_usb_ep0_in_queue_byte(0);
698                                 ao_usb_ep0_in_queue_byte(0);
699                                 break;
700                         }
701                         break;
702                 }
703                 break;
704         case AO_USB_TYPE_CLASS:
705                 debug ("Class setup packet\n");
706                 switch (ao_usb_setup.request) {
707                 case AO_USB_SET_LINE_CODING:
708                         debug ("set line coding\n");
709                         ao_usb_ep0_out_set((uint8_t *) &ao_usb_line_coding, 7);
710                         break;
711                 case AO_USB_GET_LINE_CODING:
712                         debug ("get line coding\n");
713                         ao_usb_ep0_in_set((const uint8_t *) &ao_usb_line_coding, 7);
714                         break;
715                 case AO_USB_SET_CONTROL_LINE_STATE:
716                         break;
717                 }
718                 break;
719         }
720
721         /* If we're not waiting to receive data from the host,
722          * queue an IN response
723          */
724         if (ao_usb_ep0_state == AO_USB_EP0_DATA_IN)
725                 ao_usb_ep0_in_start(ao_usb_setup.length);
726 }
727
728 static void
729 ao_usb_ep0_handle(uint8_t receive)
730 {
731         ao_usb_ep0_receive = 0;
732         if (receive & AO_USB_EP0_GOT_RESET) {
733                 debug ("\treset\n");
734                 ao_usb_set_ep0();
735                 return;
736         }
737         if (receive & AO_USB_EP0_GOT_SETUP) {
738                 debug ("\tsetup\n");
739                 ao_usb_ep0_setup();
740         }
741         if (receive & AO_USB_EP0_GOT_RX_DATA) {
742                 debug ("\tgot rx data\n");
743                 if (ao_usb_ep0_state == AO_USB_EP0_DATA_OUT) {
744                         ao_usb_ep0_fill();
745                         if (ao_usb_ep0_out_len == 0) {
746                                 ao_usb_ep0_state = AO_USB_EP0_DATA_IN;
747                                 ao_usb_ep0_in_start(0);
748                         }
749                 }
750         }
751         if (receive & AO_USB_EP0_GOT_TX_ACK) {
752                 debug ("\tgot tx ack\n");
753
754 #if HAS_FLIGHT && AO_USB_FORCE_IDLE
755                 ao_flight_force_idle = 1;
756 #endif
757                 /* Wait until the IN packet is received from addr 0
758                  * before assigning our local address
759                  */
760                 if (ao_usb_address_pending)
761                         ao_usb_set_address(ao_usb_address);
762                 if (ao_usb_ep0_state == AO_USB_EP0_DATA_IN)
763                         ao_usb_ep0_flush();
764         }
765 }
766
767 void
768 stm_usb_lp_isr(void)
769 {
770         uint32_t        istr = stm_usb.istr;
771
772         if (istr & (1 << STM_USB_ISTR_CTR)) {
773                 uint8_t         ep = istr & STM_USB_ISTR_EP_ID_MASK;
774                 uint32_t        epr, epr_write;
775
776                 /* Preserve the SW write bits, don't mess with most HW writable bits,
777                  * clear the CTR_RX and CTR_TX bits
778                  */
779                 epr = stm_usb.epr[ep];
780                 epr_write = epr;
781                 epr_write &= STM_USB_EPR_PRESERVE_MASK;
782                 epr_write |= STM_USB_EPR_INVARIANT;
783                 epr_write &= ~(1 << STM_USB_EPR_CTR_RX);
784                 epr_write &= ~(1 << STM_USB_EPR_CTR_TX);
785                 stm_usb.epr[ep] = epr_write;
786
787                 switch (ep) {
788                 case 0:
789                         ++control_count;
790                         if (ao_usb_epr_ctr_rx(epr)) {
791                                 if (ao_usb_epr_setup(epr))
792                                         ao_usb_ep0_receive |= AO_USB_EP0_GOT_SETUP;
793                                 else
794                                         ao_usb_ep0_receive |= AO_USB_EP0_GOT_RX_DATA;
795                         }
796                         if (ao_usb_epr_ctr_tx(epr))
797                                 ao_usb_ep0_receive |= AO_USB_EP0_GOT_TX_ACK;
798                         ao_usb_ep0_handle(ao_usb_ep0_receive);
799                         break;
800                 case AO_USB_OUT_EPR:
801                         ++out_count;
802                         if (ao_usb_epr_ctr_rx(epr)) {
803                                 _rx_dbg1("RX ISR", epr);
804                                 ao_usb_out_avail = 1;
805                                 _rx_dbg0("out avail set");
806                                 ao_wakeup(AO_USB_OUT_SLEEP_ADDR);
807                                 _rx_dbg0("stdin awoken");
808                         }
809                         break;
810                 case AO_USB_IN_EPR:
811                         ++in_count;
812                         _tx_dbg1("TX ISR", epr);
813                         if (ao_usb_epr_ctr_tx(epr)) {
814                                 ao_usb_in_pending = 0;
815                                 ao_wakeup(&ao_usb_in_pending);
816                         }
817                         break;
818                 case AO_USB_INT_EPR:
819                         ++int_count;
820                         if (ao_usb_epr_ctr_tx(epr))
821                                 _ao_usb_set_stat_tx(AO_USB_INT_EPR, STM_USB_EPR_STAT_TX_NAK);
822                         break;
823                 }
824                 return;
825         }
826
827         if (istr & (1 << STM_USB_ISTR_RESET)) {
828                 ++reset_count;
829                 stm_usb.istr &= ~(1 << STM_USB_ISTR_RESET);
830                 ao_usb_ep0_receive |= AO_USB_EP0_GOT_RESET;
831                 ao_usb_ep0_handle(ao_usb_ep0_receive);
832         }
833 }
834
835 void
836 stm_usb_fs_wkup(void)
837 {
838         /* USB wakeup, just clear the bit for now */
839         stm_usb.istr &= ~(1 << STM_USB_ISTR_WKUP);
840 }
841
842 /* Queue the current IN buffer for transmission */
843 static void
844 _ao_usb_in_send(void)
845 {
846         _tx_dbg0("in_send start");
847         debug ("send %d\n", ao_usb_tx_count);
848         while (ao_usb_in_pending)
849                 ao_sleep(&ao_usb_in_pending);
850         ao_usb_in_pending = 1;
851         if (ao_usb_tx_count != AO_USB_IN_SIZE)
852                 ao_usb_in_flushed = 1;
853         ao_usb_write(ao_usb_tx_buffer, ao_usb_in_tx_buffer, 0, ao_usb_tx_count);
854         ao_usb_bdt[AO_USB_IN_EPR].single.count_tx = ao_usb_tx_count;
855         ao_usb_tx_count = 0;
856         _ao_usb_set_stat_tx(AO_USB_IN_EPR, STM_USB_EPR_STAT_TX_VALID);
857         _tx_dbg0("in_send end");
858 }
859
860 /* Wait for a free IN buffer. Interrupts are blocked */
861 static void
862 _ao_usb_in_wait(void)
863 {
864         for (;;) {
865                 /* Check if the current buffer is writable */
866                 if (ao_usb_tx_count < AO_USB_IN_SIZE)
867                         break;
868
869                 _tx_dbg0("in_wait top");
870                 /* Wait for an IN buffer to be ready */
871                 while (ao_usb_in_pending)
872                         ao_sleep(&ao_usb_in_pending);
873                 _tx_dbg0("in_wait bottom");
874         }
875 }
876
877 void
878 ao_usb_flush(void)
879 {
880         if (!ao_usb_running)
881                 return;
882
883         /* Anytime we've sent a character since
884          * the last time we flushed, we'll need
885          * to send a packet -- the only other time
886          * we would send a packet is when that
887          * packet was full, in which case we now
888          * want to send an empty packet
889          */
890         ao_arch_block_interrupts();
891         while (!ao_usb_in_flushed) {
892                 _tx_dbg0("flush top");
893                 _ao_usb_in_send();
894                 _tx_dbg0("flush end");
895         }
896         ao_arch_release_interrupts();
897 }
898
899 void
900 ao_usb_putchar(char c)
901 {
902         if (!ao_usb_running)
903                 return;
904
905         ao_arch_block_interrupts();
906         _ao_usb_in_wait();
907
908         ao_usb_in_flushed = 0;
909         ao_usb_tx_buffer[ao_usb_tx_count++] = (uint8_t) c;
910
911         /* Send the packet when full */
912         if (ao_usb_tx_count == AO_USB_IN_SIZE) {
913                 _tx_dbg0("putchar full");
914                 _ao_usb_in_send();
915                 _tx_dbg0("putchar flushed");
916         }
917         ao_arch_release_interrupts();
918 }
919
920 static void
921 _ao_usb_out_recv(void)
922 {
923         _rx_dbg0("out_recv top");
924         ao_usb_out_avail = 0;
925
926         ao_usb_rx_count = ao_usb_bdt[AO_USB_OUT_EPR].single.count_rx & STM_USB_BDT_COUNT_RX_COUNT_RX_MASK;
927
928         _rx_dbg1("out_recv count", ao_usb_rx_count);
929         debug ("recv %d\n", ao_usb_rx_count);
930         debug_data("Fill OUT len %d:", ao_usb_rx_count);
931         ao_usb_read(ao_usb_rx_buffer, ao_usb_out_rx_buffer, 0, ao_usb_rx_count);
932         debug_data("\n");
933         ao_usb_rx_pos = 0;
934
935         /* ACK the packet */
936         _ao_usb_set_stat_rx(AO_USB_OUT_EPR, STM_USB_EPR_STAT_RX_VALID);
937 }
938
939 int
940 _ao_usb_pollchar(void)
941 {
942         uint8_t c;
943
944         if (!ao_usb_running)
945                 return AO_READ_AGAIN;
946
947         for (;;) {
948                 if (ao_usb_rx_pos != ao_usb_rx_count)
949                         break;
950
951                 _rx_dbg0("poll check");
952                 /* Check to see if a packet has arrived */
953                 if (!ao_usb_out_avail) {
954                         _rx_dbg0("poll none");
955                         return AO_READ_AGAIN;
956                 }
957                 _ao_usb_out_recv();
958         }
959
960         /* Pull a character out of the fifo */
961         c = ao_usb_rx_buffer[ao_usb_rx_pos++];
962         return c;
963 }
964
965 char
966 ao_usb_getchar(void)
967 {
968         int     c;
969
970         ao_arch_block_interrupts();
971         while ((c = _ao_usb_pollchar()) == AO_READ_AGAIN)
972                 ao_sleep(AO_USB_OUT_SLEEP_ADDR);
973         ao_arch_release_interrupts();
974         return c;
975 }
976
977 void
978 ao_usb_disable(void)
979 {
980         ao_arch_block_interrupts();
981         stm_usb.cntr = (1 << STM_USB_CNTR_FRES);
982         stm_usb.istr = 0;
983
984         /* Disable USB pull-up */
985         stm_syscfg.pmc &= ~(1 << STM_SYSCFG_PMC_USB_PU);
986
987         /* Switch off the device */
988         stm_usb.cntr = (1 << STM_USB_CNTR_PDWN) | (1 << STM_USB_CNTR_FRES);
989
990         /* Disable the interface */
991         stm_rcc.apb1enr &= ~(1 << STM_RCC_APB1ENR_USBEN);
992         ao_arch_release_interrupts();
993 }
994
995 void
996 ao_usb_enable(void)
997 {
998         int     t;
999
1000         /* Enable SYSCFG */
1001         stm_rcc.apb2enr |= (1 << STM_RCC_APB2ENR_SYSCFGEN);
1002
1003         /* Disable USB pull-up */
1004         stm_syscfg.pmc &= ~(1 << STM_SYSCFG_PMC_USB_PU);
1005
1006         /* Enable USB device */
1007         stm_rcc.apb1enr |= (1 << STM_RCC_APB1ENR_USBEN);
1008
1009         /* Do not touch the GPIOA configuration; USB takes priority
1010          * over GPIO on pins A11 and A12, but if you select alternate
1011          * input 10 (the documented correct selection), then USB is
1012          * pulled low and doesn't work at all
1013          */
1014
1015         ao_arch_block_interrupts();
1016
1017         /* Route interrupts */
1018         stm_nvic_set_priority(STM_ISR_USB_LP_POS, 3);
1019         stm_nvic_set_enable(STM_ISR_USB_LP_POS);
1020
1021         ao_usb_configuration = 0;
1022
1023         stm_usb.cntr = (1 << STM_USB_CNTR_FRES);
1024
1025         /* Clear the power down bit */
1026         stm_usb.cntr = 0;
1027
1028         /* Clear any spurious interrupts */
1029         stm_usb.istr = 0;
1030
1031         debug ("ao_usb_enable\n");
1032
1033         /* Enable interrupts */
1034         stm_usb.cntr = ((1 << STM_USB_CNTR_CTRM) |
1035                         (0 << STM_USB_CNTR_PMAOVRM) |
1036                         (0 << STM_USB_CNTR_ERRM) |
1037                         (0 << STM_USB_CNTR_WKUPM) |
1038                         (0 << STM_USB_CNTR_SUSPM) |
1039                         (1 << STM_USB_CNTR_RESETM) |
1040                         (0 << STM_USB_CNTR_SOFM) |
1041                         (0 << STM_USB_CNTR_ESOFM) |
1042                         (0 << STM_USB_CNTR_RESUME) |
1043                         (0 << STM_USB_CNTR_FSUSP) |
1044                         (0 << STM_USB_CNTR_LP_MODE) |
1045                         (0 << STM_USB_CNTR_PDWN) |
1046                         (0 << STM_USB_CNTR_FRES));
1047
1048         ao_arch_release_interrupts();
1049
1050         for (t = 0; t < 1000; t++)
1051                 ao_arch_nop();
1052         /* Enable USB pull-up */
1053         stm_syscfg.pmc |= (1 << STM_SYSCFG_PMC_USB_PU);
1054 }
1055
1056 #if USB_ECHO
1057 struct ao_task ao_usb_echo_task;
1058
1059 static void
1060 ao_usb_echo(void)
1061 {
1062         char    c;
1063
1064         for (;;) {
1065                 c = ao_usb_getchar();
1066                 ao_usb_putchar(c);
1067                 ao_usb_flush();
1068         }
1069 }
1070 #endif
1071
1072 #if USB_DEBUG
1073 static void
1074 ao_usb_irq(void)
1075 {
1076         printf ("control: %d out: %d in: %d int: %d reset: %d\n",
1077                 control_count, out_count, in_count, int_count, reset_count);
1078 }
1079
1080 __code struct ao_cmds ao_usb_cmds[] = {
1081         { ao_usb_irq, "I\0Show USB interrupt counts" },
1082         { 0, NULL }
1083 };
1084 #endif
1085
1086 void
1087 ao_usb_init(void)
1088 {
1089         ao_usb_enable();
1090
1091         debug ("ao_usb_init\n");
1092         ao_usb_ep0_state = AO_USB_EP0_IDLE;
1093 #if USB_ECHO
1094         ao_add_task(&ao_usb_echo_task, ao_usb_echo, "usb echo");
1095 #endif
1096 #if USB_DEBUG
1097         ao_cmd_register(&ao_usb_cmds[0]);
1098 #endif
1099 #if !USB_ECHO
1100 #if USE_USB_STDIO
1101         ao_add_stdio(_ao_usb_pollchar, ao_usb_putchar, ao_usb_flush);
1102 #endif
1103 #endif
1104 }
1105
1106 #if TX_DBG || RX_DBG
1107
1108 struct ao_usb_dbg {
1109         int             line;
1110         char            *msg;
1111         uint32_t        value;
1112         uint32_t        primask;
1113 #if TX_DBG
1114         uint16_t        in_count;
1115         uint32_t        in_epr;
1116         uint32_t        in_pending;
1117         uint32_t        tx_count;
1118         uint32_t        in_flushed;
1119 #endif
1120 #if RX_DBG
1121         uint8_t         rx_count;
1122         uint8_t         rx_pos;
1123         uint8_t         out_avail;
1124         uint32_t        out_epr;
1125 #endif
1126 };
1127
1128 #define NUM_USB_DBG     128
1129
1130 static struct ao_usb_dbg dbg[128];
1131 static int dbg_i;
1132
1133 static void _dbg(int line, char *msg, uint32_t value)
1134 {
1135         uint32_t        primask;
1136         dbg[dbg_i].line = line;
1137         dbg[dbg_i].msg = msg;
1138         dbg[dbg_i].value = value;
1139         asm("mrs %0,primask" : "=&r" (primask));
1140         dbg[dbg_i].primask = primask;
1141 #if TX_DBG
1142         dbg[dbg_i].in_count = in_count;
1143         dbg[dbg_i].in_epr = stm_usb.epr[AO_USB_IN_EPR];
1144         dbg[dbg_i].in_pending = ao_usb_in_pending;
1145         dbg[dbg_i].tx_count = ao_usb_tx_count;
1146         dbg[dbg_i].in_flushed = ao_usb_in_flushed;
1147 #endif
1148 #if RX_DBG
1149         dbg[dbg_i].rx_count = ao_usb_rx_count;
1150         dbg[dbg_i].rx_pos = ao_usb_rx_pos;
1151         dbg[dbg_i].out_avail = ao_usb_out_avail;
1152         dbg[dbg_i].out_epr = stm_usb.epr[AO_USB_OUT_EPR];
1153 #endif
1154         if (++dbg_i == NUM_USB_DBG)
1155                 dbg_i = 0;
1156 }
1157 #endif