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