594114b62ed856a8b4babc8f9ccf47f3597d9624
[fw/altos] / src / lpc / ao_usb_lpc.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 #ifndef USE_USB_STDIO
24 #define USE_USB_STDIO   1
25 #endif
26
27 #if USE_USB_STDIO
28 #define AO_USB_OUT_SLEEP_ADDR   (&ao_stdin_ready)
29 #else
30 #define AO_USB_OUT_SLEEP_ADDR   (&ao_usb_out_avail)
31 #endif
32
33 #define USB_DEBUG       0
34 #define USB_DEBUG_DATA  0
35 #define USB_ECHO        0
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 static uint16_t         ao_usb_ep0_in_max;      /* Requested amount from host */
63
64 /* Temp buffer for smaller EP0 in data */
65 static uint8_t  ao_usb_ep0_in_buf[2];
66
67 /* Pending EP0 OUT data */
68 static uint8_t *ao_usb_ep0_out_data;
69 static uint8_t  ao_usb_ep0_out_len;
70
71 /*
72  * Objects allocated in special USB memory
73  */
74
75 /* USB address of end of allocated storage */
76 static uint8_t  *ao_usb_sram;
77
78 /* Pointer to ep0 tx/rx buffers in USB memory */
79 static uint8_t  *ao_usb_ep0_tx_buffer;
80 static uint8_t  *ao_usb_ep0_setup_buffer;
81 static uint8_t  *ao_usb_ep0_rx_buffer;
82
83 /* Pointer to bulk data tx/rx buffers in USB memory */
84 static uint8_t  *ao_usb_in_tx_buffer[2];
85 static uint8_t  ao_usb_in_tx_cur;
86 static uint8_t  ao_usb_tx_count;
87
88 static uint8_t  *ao_usb_out_rx_buffer[2];
89 static uint8_t  ao_usb_out_rx_cur;
90 static uint8_t  ao_usb_rx_count, ao_usb_rx_pos;
91
92 extern struct lpc_usb_endpoint lpc_usb_endpoint;
93
94 /* Marks when we don't need to send an IN packet.
95  * This happens only when the last IN packet is not full,
96  * otherwise the host will expect to keep seeing packets.
97  * Send a zero-length packet as required
98  */
99 static uint8_t  ao_usb_in_flushed;
100
101 /* Marks when we have delivered an IN packet to the hardware
102  * and it has not been received yet. ao_sleep on this address
103  * to wait for it to be delivered.
104  */
105 static uint8_t  ao_usb_in_pending;
106
107 /* Marks when an OUT packet has been received by the hardware
108  * but not pulled to the shadow buffer.
109  */
110 static uint8_t  ao_usb_out_avail;
111 uint8_t         ao_usb_running;
112 static uint8_t  ao_usb_configuration;
113
114 #define AO_USB_EP0_GOT_RESET    1
115 #define AO_USB_EP0_GOT_SETUP    2
116 #define AO_USB_EP0_GOT_RX_DATA  4
117 #define AO_USB_EP0_GOT_TX_ACK   8
118
119 static uint8_t  ao_usb_ep0_receive;
120 static uint8_t  ao_usb_address;
121 static uint8_t  ao_usb_address_pending;
122
123 static inline uint32_t set_toggle(uint32_t      current_value,
124                                    uint32_t     mask,
125                                    uint32_t     desired_value)
126 {
127         return (current_value ^ desired_value) & mask;
128 }
129
130 /*
131  * Set current device address and mark the
132  * interface as active
133  */
134 void
135 ao_usb_set_address(uint8_t address)
136 {
137         debug("ao_usb_set_address %02x\n", address);
138         lpc_usb.devcmdstat = ((address << LPC_USB_DEVCMDSTAT_DEV_ADDR) |
139                               (1 << LPC_USB_DEVCMDSTAT_DEV_EN) |
140                               (0 << LPC_USB_DEVCMDSTAT_SETUP) |
141                               (0 << LPC_USB_DEVCMDSTAT_PLL_ON) |
142                               (0 << LPC_USB_DEVCMDSTAT_LPM_SUP) |
143                               (0 << LPC_USB_DEVCMDSTAT_INTONNAK_AO) |
144                               (0 << LPC_USB_DEVCMDSTAT_INTONNAK_AI) |
145                               (0 << LPC_USB_DEVCMDSTAT_INTONNAK_CO) |
146                               (0 << LPC_USB_DEVCMDSTAT_INTONNAK_CI) |
147                               (1 << LPC_USB_DEVCMDSTAT_DCON) |
148                               (0 << LPC_USB_DEVCMDSTAT_DSUS) |
149                               (0 << LPC_USB_DEVCMDSTAT_DCON_C) |
150                               (0 << LPC_USB_DEVCMDSTAT_DSUS_C) |
151                               (0 << LPC_USB_DEVCMDSTAT_DRES_C) |
152                               (0 << LPC_USB_DEVCMDSTAT_VBUSDEBOUNCED));
153         ao_usb_address_pending = 0;
154 }
155
156 #define TX_DBG 0
157 #define RX_DBG 0
158
159 #if TX_DBG
160 #define _tx_dbg0(msg) _dbg(__LINE__,msg,0)
161 #define _tx_dbg1(msg,value) _dbg(__LINE__,msg,value)
162 #else
163 #define _tx_dbg0(msg)
164 #define _tx_dbg1(msg,value)
165 #endif
166
167 #if RX_DBG
168 #define _rx_dbg0(msg) _dbg(__LINE__,msg,0)
169 #define _rx_dbg1(msg,value) _dbg(__LINE__,msg,value)
170 #else
171 #define _rx_dbg0(msg)
172 #define _rx_dbg1(msg,value)
173 #endif
174
175 #if TX_DBG || RX_DBG
176 static void _dbg(int line, char *msg, uint32_t value);
177 #endif
178
179 /*
180  * Set just endpoint 0, for use during startup
181  */
182
183 static uint8_t *
184 ao_usb_alloc_sram(uint16_t size)
185 {
186         uint8_t *addr = ao_usb_sram;
187
188         ao_usb_sram += (size + 63) & ~63;
189         return addr;
190 }
191
192 static uint16_t
193 ao_usb_sram_offset(uint8_t *addr)
194 {
195         return (uint16_t) ((intptr_t) addr >> 6);
196 }
197
198 static void
199 ao_usb_set_ep(vuint32_t *ep, uint8_t *addr, uint16_t nbytes)
200 {
201         *ep = ((ao_usb_sram_offset(addr) << LPC_USB_EP_OFFSET) |
202                (nbytes << LPC_USB_EP_NBYTES) |
203                (0 << LPC_USB_EP_ENDPOINT_ISO) |
204                (0 << LPC_USB_EP_RATE_FEEDBACK) |
205                (0 << LPC_USB_EP_TOGGLE_RESET) |
206                (0 << LPC_USB_EP_STALL) |
207                (0 << LPC_USB_EP_DISABLED) |
208                (1 << LPC_USB_EP_ACTIVE));
209 }
210
211 static inline uint16_t
212 ao_usb_ep_count(vuint32_t *ep)
213 {
214         return (*ep >> LPC_USB_EP_NBYTES) & LPC_USB_EP_NBYTES_MASK;
215 }
216
217 static inline uint8_t
218 ao_usb_ep_stall(vuint32_t *ep)
219 {
220         return (*ep >> LPC_USB_EP_STALL) & 1;
221 }
222
223 static inline vuint32_t *
224 ao_usb_ep0_out(void)
225 {
226         return &lpc_usb_endpoint.ep0_out;
227 }
228
229 static inline vuint32_t *
230 ao_usb_ep0_in(void)
231 {
232         return &lpc_usb_endpoint.ep0_in;
233 }
234
235 static inline vuint32_t *
236 ao_usb_epn_out(uint8_t n, uint8_t i)
237 {
238         return &lpc_usb_endpoint.epn[n-1].out[i];
239 }
240
241 static inline vuint32_t *
242 ao_usb_epn_in(uint8_t n, uint8_t i)
243 {
244         return &lpc_usb_endpoint.epn[n-1].in[i];
245 }
246
247 #if UNUSED
248 static void
249 ao_usb_set_epn_in(uint8_t n, uint8_t *addr, uint16_t nbytes)
250 {
251         ao_usb_set_ep(ao_usb_epn_in(n), addr, nbytes);
252 }
253 #endif
254
255 static void
256 ao_usb_set_epn_out(uint8_t n, uint8_t *addr, uint16_t nbytes)
257 {
258         ao_usb_set_ep(ao_usb_epn_out(n, 0), addr, nbytes);
259 }
260
261 static inline uint16_t
262 ao_usb_epn_out_count(uint8_t n)
263 {
264         return ao_usb_ep_count(ao_usb_epn_out(n, 0));
265 }
266
267 static inline uint16_t
268 ao_usb_epn_in_count(uint8_t n)
269 {
270         return ao_usb_ep_count(ao_usb_epn_in(n, 0));
271 }
272
273 static void
274 ao_usb_enable_ep(vuint32_t *ep, uint8_t *addr, uint16_t set_nbytes)
275 {
276         ao_usb_set_ep(ep, addr, set_nbytes);
277 }
278
279 static void
280 ao_usb_disable_ep(vuint32_t *ep)
281 {
282         *ep = ((0 << LPC_USB_EP_OFFSET) |
283                (0 << LPC_USB_EP_NBYTES) |
284                (0 << LPC_USB_EP_ENDPOINT_ISO) |
285                (0 << LPC_USB_EP_RATE_FEEDBACK) |
286                (0 << LPC_USB_EP_TOGGLE_RESET) |
287                (0 << LPC_USB_EP_STALL) |
288                (1 << LPC_USB_EP_DISABLED) |
289                (0 << LPC_USB_EP_ACTIVE));
290 }
291
292 static void
293 ao_usb_enable_epn(uint8_t n,
294                   uint16_t out_bytes, uint8_t *out_addr,
295                   uint8_t *in_addr)
296 {
297         ao_usb_enable_ep(ao_usb_epn_out(n, 0), out_addr, out_bytes);
298         ao_usb_disable_ep(ao_usb_epn_out(n, 1));
299
300         ao_usb_enable_ep(ao_usb_epn_in(n, 0), in_addr, 0);
301         ao_usb_disable_ep(ao_usb_epn_in(n, 1));
302 }
303
304 static void
305 ao_usb_disable_epn(uint8_t n)
306 {
307         ao_usb_disable_ep(ao_usb_epn_out(n, 0));
308         ao_usb_disable_ep(ao_usb_epn_out(n, 1));
309         ao_usb_disable_ep(ao_usb_epn_in(n, 0));
310         ao_usb_disable_ep(ao_usb_epn_in(n, 1));
311 }
312
313 static void
314 ao_usb_reset(void)
315 {
316         ao_usb_set_address(0);
317         ao_usb_configuration = 0;
318
319         ao_usb_ep0_state = AO_USB_EP0_IDLE;
320         ao_usb_ep0_in_data = NULL;
321         ao_usb_ep0_in_len = 0;
322         ao_usb_ep0_in_max = 0;
323
324         ao_usb_ep0_out_data = NULL;
325         ao_usb_ep0_out_len = 0;
326 }
327
328 static void
329 ao_usb_set_ep0(void)
330 {
331         int                     e;
332
333         /* Everything is single buffered for now */
334         lpc_usb.epbufcfg = 0;
335         lpc_usb.epinuse = 0;
336         lpc_usb.epskip = 0xffffffff;
337
338         lpc_usb.intstat = 0xc00003ff;
339
340         lpc_usb.epliststart = (uint32_t) (intptr_t) &lpc_usb_endpoint;
341         lpc_usb.databufstart = ((uint32_t) (intptr_t) ao_usb_sram) & 0xffc00000;
342
343         /* Set up EP 0 - a Control end point with 32 bytes of in and out buffers */
344
345         ao_usb_enable_ep(ao_usb_ep0_out(), ao_usb_ep0_rx_buffer, AO_USB_CONTROL_SIZE);
346         lpc_usb_endpoint.setup = ao_usb_sram_offset(ao_usb_ep0_setup_buffer);
347         ao_usb_enable_ep(ao_usb_ep0_in(), ao_usb_ep0_tx_buffer, 0);
348
349         /* Clear all of the other endpoints */
350         for (e = 1; e <= 4; e++)
351                 ao_usb_disable_epn(e);
352         ao_usb_reset();
353 }
354
355 static void
356 ao_usb_set_configuration(void)
357 {
358         debug ("ao_usb_set_configuration\n");
359
360         /* Set up the INT end point */
361         ao_usb_enable_epn(AO_USB_INT_EP, 0, NULL, NULL);
362
363         /* Set up the OUT end point */
364         ao_usb_enable_epn(AO_USB_OUT_EP, AO_USB_OUT_SIZE, ao_usb_out_rx_buffer[0], NULL);
365
366         /* Set the current RX pointer to the *other* buffer so that when buffer 0 gets
367          * data, we'll switch to it and pull bytes from there
368          */
369         ao_usb_out_rx_cur = 1;
370
371         /* Set up the IN end point */
372         ao_usb_enable_epn(AO_USB_IN_EP, 0, NULL, ao_usb_in_tx_buffer[0]);
373         ao_usb_in_tx_cur = 0;
374
375         ao_usb_in_flushed = 0;
376         ao_usb_in_pending = 0;
377         ao_wakeup(&ao_usb_in_pending);
378
379         ao_usb_out_avail = 0;
380         ao_usb_configuration = 0;
381
382         ao_usb_running = 1;
383         ao_wakeup(&ao_usb_running);
384 }
385
386 /* Send an IN data packet */
387 static void
388 ao_usb_ep0_flush(void)
389 {
390         uint8_t this_len;
391
392         this_len = ao_usb_ep0_in_len;
393         if (this_len > AO_USB_CONTROL_SIZE)
394                 this_len = AO_USB_CONTROL_SIZE;
395
396         ao_usb_ep0_in_len -= this_len;
397         ao_usb_ep0_in_max -= this_len;
398
399         if (this_len < AO_USB_CONTROL_SIZE || ao_usb_ep0_in_max == 0)
400                 ao_usb_ep0_state = AO_USB_EP0_IDLE;
401
402         debug_data ("Flush EP0 len %d:", this_len);
403         memcpy(ao_usb_ep0_tx_buffer, ao_usb_ep0_in_data, this_len);
404         debug_data ("\n");
405         ao_usb_ep0_in_data += this_len;
406
407         /* Mark the endpoint as TX valid to send the packet */
408         ao_usb_set_ep(ao_usb_ep0_in(), ao_usb_ep0_tx_buffer, this_len);
409         debug ("queue tx.  0 now %08x\n", *ao_usb_ep0_in());
410 }
411
412 /* Read data from the ep0 OUT fifo */
413 static void
414 ao_usb_ep0_fill(void)
415 {
416         uint16_t        len;
417         uint8_t         *rx_buffer;
418
419         /* Pull all of the data out of the packet */
420         if (lpc_usb.devcmdstat & (1 << LPC_USB_DEVCMDSTAT_SETUP)) {
421                 rx_buffer = ao_usb_ep0_setup_buffer;
422                 len = 8;
423         } else {
424                 rx_buffer = ao_usb_ep0_rx_buffer;
425                 len = AO_USB_CONTROL_SIZE - ao_usb_ep_count(ao_usb_ep0_out());
426         }
427
428         if (len > ao_usb_ep0_out_len)
429                 len = ao_usb_ep0_out_len;
430         ao_usb_ep0_out_len -= len;
431
432         debug_data ("Fill EP0 len %d:", len);
433         memcpy(ao_usb_ep0_out_data, rx_buffer, len);
434         debug_data ("\n");
435         ao_usb_ep0_out_data += len;
436
437         /* ACK the packet */
438         ao_usb_set_ep(ao_usb_ep0_out(), ao_usb_ep0_rx_buffer, AO_USB_CONTROL_SIZE);
439         lpc_usb.devcmdstat |= (1 << LPC_USB_DEVCMDSTAT_SETUP);
440 }
441
442 static void
443 ao_usb_ep0_in_reset(void)
444 {
445         ao_usb_ep0_in_data = ao_usb_ep0_in_buf;
446         ao_usb_ep0_in_len = 0;
447 }
448
449 static void
450 ao_usb_ep0_in_queue_byte(uint8_t a)
451 {
452         if (ao_usb_ep0_in_len < sizeof (ao_usb_ep0_in_buf))
453                 ao_usb_ep0_in_buf[ao_usb_ep0_in_len++] = a;
454 }
455
456 static void
457 ao_usb_ep0_in_set(const uint8_t *data, uint8_t len)
458 {
459         ao_usb_ep0_in_data = data;
460         ao_usb_ep0_in_len = len;
461 }
462
463 static void
464 ao_usb_ep0_out_set(uint8_t *data, uint8_t len)
465 {
466         ao_usb_ep0_out_data = data;
467         ao_usb_ep0_out_len = len;
468 }
469
470 static void
471 ao_usb_ep0_in_start(uint16_t max)
472 {
473         ao_usb_ep0_in_max = max;
474         /* Don't send more than asked for */
475         if (ao_usb_ep0_in_len > max)
476                 ao_usb_ep0_in_len = max;
477         ao_usb_ep0_flush();
478 }
479
480 struct ao_usb_line_coding ao_usb_line_coding = {115200, 0, 0, 8};
481
482 /* Walk through the list of descriptors and find a match
483  */
484 static void
485 ao_usb_get_descriptor(uint16_t value, uint16_t length)
486 {
487         const uint8_t           *descriptor;
488         uint8_t         type = value >> 8;
489         uint8_t         index = value;
490
491         descriptor = ao_usb_descriptors;
492         while (descriptor[0] != 0) {
493                 if (descriptor[1] == type && index-- == 0) {
494                         uint8_t len;
495                         if (type == AO_USB_DESC_CONFIGURATION)
496                                 len = descriptor[2];
497                         else
498                                 len = descriptor[0];
499                         if (len > length)
500                                 len = length;
501                         ao_usb_ep0_in_set(descriptor, len);
502                         break;
503                 }
504                 descriptor += descriptor[0];
505         }
506 }
507
508 static void
509 ao_usb_ep0_setup(void)
510 {
511         /* Pull the setup packet out of the fifo */
512         ao_usb_ep0_out_set((uint8_t *) &ao_usb_setup, 8);
513         ao_usb_ep0_fill();
514         if (ao_usb_ep0_out_len != 0) {
515                 debug ("invalid setup packet length\n");
516                 return;
517         }
518
519         if ((ao_usb_setup.dir_type_recip & AO_USB_DIR_IN) || ao_usb_setup.length == 0)
520                 ao_usb_ep0_state = AO_USB_EP0_DATA_IN;
521         else
522                 ao_usb_ep0_state = AO_USB_EP0_DATA_OUT;
523
524         ao_usb_ep0_in_reset();
525
526         switch(ao_usb_setup.dir_type_recip & AO_USB_SETUP_TYPE_MASK) {
527         case AO_USB_TYPE_STANDARD:
528                 debug ("Standard setup packet\n");
529                 switch(ao_usb_setup.dir_type_recip & AO_USB_SETUP_RECIP_MASK) {
530                 case AO_USB_RECIP_DEVICE:
531                         debug ("Device setup packet\n");
532                         switch(ao_usb_setup.request) {
533                         case AO_USB_REQ_GET_STATUS:
534                                 debug ("get status\n");
535                                 ao_usb_ep0_in_queue_byte(0);
536                                 ao_usb_ep0_in_queue_byte(0);
537                                 break;
538                         case AO_USB_REQ_SET_ADDRESS:
539                                 debug ("set address %d\n", ao_usb_setup.value);
540                                 ao_usb_address = ao_usb_setup.value;
541                                 ao_usb_address_pending = 1;
542                                 break;
543                         case AO_USB_REQ_GET_DESCRIPTOR:
544                                 debug ("get descriptor %d\n", ao_usb_setup.value);
545                                 ao_usb_get_descriptor(ao_usb_setup.value, ao_usb_setup.length);
546                                 break;
547                         case AO_USB_REQ_GET_CONFIGURATION:
548                                 debug ("get configuration %d\n", ao_usb_configuration);
549                                 ao_usb_ep0_in_queue_byte(ao_usb_configuration);
550                                 break;
551                         case AO_USB_REQ_SET_CONFIGURATION:
552                                 ao_usb_configuration = ao_usb_setup.value;
553                                 debug ("set configuration %d\n", ao_usb_configuration);
554                                 ao_usb_set_configuration();
555                                 break;
556                         }
557                         break;
558                 case AO_USB_RECIP_INTERFACE:
559                         debug ("Interface setup packet\n");
560                         switch(ao_usb_setup.request) {
561                         case AO_USB_REQ_GET_STATUS:
562                                 ao_usb_ep0_in_queue_byte(0);
563                                 ao_usb_ep0_in_queue_byte(0);
564                                 break;
565                         case AO_USB_REQ_GET_INTERFACE:
566                                 ao_usb_ep0_in_queue_byte(0);
567                                 break;
568                         case AO_USB_REQ_SET_INTERFACE:
569                                 break;
570                         }
571                         break;
572                 case AO_USB_RECIP_ENDPOINT:
573                         debug ("Endpoint setup packet\n");
574                         switch(ao_usb_setup.request) {
575                         case AO_USB_REQ_GET_STATUS:
576                                 ao_usb_ep0_in_queue_byte(0);
577                                 ao_usb_ep0_in_queue_byte(0);
578                                 break;
579                         }
580                         break;
581                 }
582                 break;
583         case AO_USB_TYPE_CLASS:
584                 debug ("Class setup packet\n");
585                 switch (ao_usb_setup.request) {
586                 case AO_USB_SET_LINE_CODING:
587                         debug ("set line coding\n");
588                         ao_usb_ep0_out_set((uint8_t *) &ao_usb_line_coding, 7);
589                         break;
590                 case AO_USB_GET_LINE_CODING:
591                         debug ("get line coding\n");
592                         ao_usb_ep0_in_set((const uint8_t *) &ao_usb_line_coding, 7);
593                         break;
594                 case AO_USB_SET_CONTROL_LINE_STATE:
595                         break;
596                 }
597                 break;
598         }
599
600         /* If we're not waiting to receive data from the host,
601          * queue an IN response
602          */
603         if (ao_usb_ep0_state == AO_USB_EP0_DATA_IN)
604                 ao_usb_ep0_in_start(ao_usb_setup.length);
605 }
606
607 static void
608 ao_usb_ep0_handle(uint8_t receive)
609 {
610         ao_usb_ep0_receive = 0;
611
612         if (receive & AO_USB_EP0_GOT_RESET) {
613                 debug ("\treset\n");
614                 ao_usb_reset();
615                 return;
616         }
617         if (receive & AO_USB_EP0_GOT_SETUP) {
618                 debug ("\tsetup\n");
619                 ao_usb_ep0_setup();
620         }
621         if (receive & AO_USB_EP0_GOT_RX_DATA) {
622                 debug ("\tgot rx data\n");
623                 if (ao_usb_ep0_state == AO_USB_EP0_DATA_OUT) {
624                         ao_usb_ep0_fill();
625                         if (ao_usb_ep0_out_len == 0) {
626                                 ao_usb_ep0_state = AO_USB_EP0_DATA_IN;
627                                 ao_usb_ep0_in_start(0);
628                         }
629                 }
630         }
631         if (receive & AO_USB_EP0_GOT_TX_ACK) {
632                 debug ("\tgot tx ack\n");
633
634                 /* Wait until the IN packet is received from addr 0
635                  * before assigning our local address
636                  */
637                 if (ao_usb_address_pending) {
638 #if HAS_FLIGHT
639                         /* Go to idle mode if USB is connected
640                          */
641                         ao_flight_force_idle = 1;
642 #endif
643                         ao_usb_set_address(ao_usb_address);
644                 }
645                 if (ao_usb_ep0_state == AO_USB_EP0_DATA_IN)
646                         ao_usb_ep0_flush();
647         }
648 }
649
650 #if USB_DEBUG
651 static uint16_t int_count;
652 static uint16_t in_count;
653 static uint16_t out_count;
654 static uint16_t reset_count;
655 #endif
656
657 void
658 lpc_usb_irq_isr(void)
659 {
660         uint32_t        intstat = lpc_usb.intstat & lpc_usb.inten;
661
662         lpc_usb.intstat = intstat;
663         /* Handle EP0 OUT packets */
664         if (intstat & (1 << LPC_USB_INT_EPOUT(0))) {
665                 if (lpc_usb.devcmdstat & (1 << LPC_USB_DEVCMDSTAT_SETUP))
666                         ao_usb_ep0_receive |= AO_USB_EP0_GOT_SETUP;
667                 else
668                         ao_usb_ep0_receive |= AO_USB_EP0_GOT_RX_DATA;
669
670                 ao_usb_ep0_handle(ao_usb_ep0_receive);
671         }
672
673         /* Handle EP0 IN packets */
674         if (intstat & (1 << LPC_USB_INT_EPIN(0))) {
675                 ao_usb_ep0_receive |= AO_USB_EP0_GOT_TX_ACK;
676
677                 ao_usb_ep0_handle(ao_usb_ep0_receive);
678         }
679
680
681         /* Handle OUT packets */
682         if (intstat & (1 << LPC_USB_INT_EPOUT(AO_USB_OUT_EP))) {
683 #if USB_DEBUG
684                 ++out_count;
685 #endif
686                 _rx_dbg1("RX ISR", *ao_usb_epn_out(AO_USB_OUT_EP));
687                 ao_usb_out_avail = 1;
688                 _rx_dbg0("out avail set");
689                 ao_wakeup(AO_USB_OUT_SLEEP_ADDR)
690                 _rx_dbg0("stdin awoken");
691         }
692
693         /* Handle IN packets */
694         if (intstat & (1 << LPC_USB_INT_EPIN(AO_USB_IN_EP))) {
695 #if USB_DEBUG
696                 ++in_count;
697 #endif
698                 _tx_dbg1("TX ISR", *ao_usb_epn_in(AO_USB_IN_EP));
699                 ao_usb_in_pending = 0;
700                 ao_wakeup(&ao_usb_in_pending);
701         }
702
703         /* NAK all INT EP IN packets */
704         if (intstat & (1 << LPC_USB_INT_EPIN(AO_USB_INT_EP))) {
705                 ;
706         }
707
708         /* Check for reset */
709         if (intstat & (1 << LPC_USB_INT_DEV)) {
710                 if (lpc_usb.devcmdstat & (1 << LPC_USB_DEVCMDSTAT_DRES_C))
711                 {
712                         lpc_usb.devcmdstat |= (1 << LPC_USB_DEVCMDSTAT_DRES_C);
713                         ao_usb_ep0_receive |= AO_USB_EP0_GOT_RESET;
714                         ao_usb_ep0_handle(ao_usb_ep0_receive);
715                 }
716         }
717 }
718
719
720
721 /* Queue the current IN buffer for transmission */
722 static void
723 _ao_usb_in_send(void)
724 {
725         _tx_dbg0("in_send start");
726         debug ("send %d\n", ao_usb_tx_count);
727         while (ao_usb_in_pending)
728                 ao_sleep(&ao_usb_in_pending);
729         ao_usb_in_pending = 1;
730         if (ao_usb_tx_count != AO_USB_IN_SIZE)
731                 ao_usb_in_flushed = 1;
732         ao_usb_set_ep(ao_usb_epn_in(AO_USB_IN_EP, 0), ao_usb_in_tx_buffer[ao_usb_in_tx_cur], ao_usb_tx_count);
733         ao_usb_in_tx_cur = 1 - ao_usb_in_tx_cur;
734         ao_usb_tx_count = 0;
735         _tx_dbg0("in_send end");
736 }
737
738 /* Wait for a free IN buffer. Interrupts are blocked */
739 static void
740 _ao_usb_in_wait(void)
741 {
742         for (;;) {
743                 /* Check if the current buffer is writable */
744                 if (ao_usb_tx_count < AO_USB_IN_SIZE)
745                         break;
746
747                 _tx_dbg0("in_wait top");
748                 /* Wait for an IN buffer to be ready */
749                 while (ao_usb_in_pending)
750                         ao_sleep(&ao_usb_in_pending);
751                 _tx_dbg0("in_wait bottom");
752         }
753 }
754
755 void
756 ao_usb_flush(void)
757 {
758         if (!ao_usb_running)
759                 return;
760
761         /* Anytime we've sent a character since
762          * the last time we flushed, we'll need
763          * to send a packet -- the only other time
764          * we would send a packet is when that
765          * packet was full, in which case we now
766          * want to send an empty packet
767          */
768         ao_arch_block_interrupts();
769         while (!ao_usb_in_flushed) {
770                 _tx_dbg0("flush top");
771                 _ao_usb_in_send();
772                 _tx_dbg0("flush end");
773         }
774         ao_arch_release_interrupts();
775 }
776
777 void
778 ao_usb_putchar(char c)
779 {
780         if (!ao_usb_running)
781                 return;
782
783         ao_arch_block_interrupts();
784         _ao_usb_in_wait();
785
786         ao_usb_in_flushed = 0;
787         ao_usb_in_tx_buffer[ao_usb_in_tx_cur][ao_usb_tx_count++] = (uint8_t) c;
788
789         /* Send the packet when full */
790         if (ao_usb_tx_count == AO_USB_IN_SIZE) {
791                 _tx_dbg0("putchar full");
792                 _ao_usb_in_send();
793                 _tx_dbg0("putchar flushed");
794         }
795         ao_arch_release_interrupts();
796 }
797
798 static void
799 _ao_usb_out_recv(void)
800 {
801         _rx_dbg0("out_recv top");
802         ao_usb_out_avail = 0;
803
804         ao_usb_rx_count = AO_USB_OUT_SIZE - ao_usb_epn_out_count(AO_USB_OUT_EP);
805
806         _rx_dbg1("out_recv count", ao_usb_rx_count);
807         debug ("recv %d\n", ao_usb_rx_count);
808         debug_data("Fill OUT len %d\n", ao_usb_rx_count);
809         ao_usb_rx_pos = 0;
810         ao_usb_out_rx_cur = 1 - ao_usb_out_rx_cur;
811
812         /* ACK the packet */
813         ao_usb_set_epn_out(AO_USB_OUT_EP, ao_usb_out_rx_buffer[1-ao_usb_out_rx_cur], AO_USB_OUT_SIZE);
814 }
815
816 int
817 _ao_usb_pollchar(void)
818 {
819         uint8_t c;
820
821         if (!ao_usb_running)
822                 return AO_READ_AGAIN;
823
824         for (;;) {
825                 if (ao_usb_rx_pos != ao_usb_rx_count)
826                         break;
827
828                 _rx_dbg0("poll check");
829                 /* Check to see if a packet has arrived */
830                 if (!ao_usb_out_avail) {
831                         _rx_dbg0("poll none");
832                         return AO_READ_AGAIN;
833                 }
834                 _ao_usb_out_recv();
835         }
836
837         /* Pull a character out of the fifo */
838         c = ao_usb_out_rx_buffer[ao_usb_out_rx_cur][ao_usb_rx_pos++];
839         return c;
840 }
841
842 char
843 ao_usb_getchar(void)
844 {
845         int     c;
846
847         ao_arch_block_interrupts();
848         while ((c = _ao_usb_pollchar()) == AO_READ_AGAIN)
849                 ao_sleep(AO_USB_OUT_SLEEP_ADDR);
850         ao_arch_release_interrupts();
851         return c;
852 }
853
854 void
855 ao_usb_disable(void)
856 {
857         ao_arch_block_interrupts();
858
859 #if HAS_USB_PULLUP
860         ao_gpio_set(AO_USB_PULLUP_PORT, AO_USB_PULLUP_PIN, 0);
861 #endif
862         /* Disable interrupts */
863         lpc_usb.inten = 0;
864
865         lpc_nvic_clear_enable(LPC_ISR_USB_IRQ_POS);
866
867         /* Disable the device */
868         lpc_usb.devcmdstat = 0;
869
870         /* Turn off USB clock */
871         lpc_scb.usbclkdiv = 0;
872
873         /* Disable USB PHY and PLL */
874         lpc_scb.pdruncfg |= ((1 << LPC_SCB_PDRUNCFG_USBPAD_PD) |
875                              (1 << LPC_SCB_PDRUNCFG_USBPLL_PD));
876
877         /* Disable USB registers and RAM */
878         lpc_scb.sysahbclkctrl &= ~((1 << LPC_SCB_SYSAHBCLKCTRL_USB) |
879                                    (1 << LPC_SCB_SYSAHBCLKCTRL_USBRAM));
880
881         ao_arch_release_interrupts();
882 }
883
884 void
885 ao_usb_enable(void)
886 {
887         int     t;
888
889         /* Enable USB pins */
890 #if HAS_LPC_USB_CONNECT
891         lpc_ioconf.pio0_6 = ((LPC_IOCONF_FUNC_USB_CONNECT << LPC_IOCONF_FUNC) |
892                              (LPC_IOCONF_MODE_INACTIVE << LPC_IOCONF_MODE) |
893                              (0 << LPC_IOCONF_HYS) |
894                              (0 << LPC_IOCONF_INV) |
895                              (0 << LPC_IOCONF_OD) |
896                              0x80);
897 #endif
898 #if HAS_USB_VBUS
899         lpc_ioconf.pio0_3 = ((LPC_IOCONF_FUNC_USB_VBUS << LPC_IOCONF_FUNC) |
900                              (LPC_IOCONF_MODE_INACTIVE << LPC_IOCONF_MODE) |
901                              (0 << LPC_IOCONF_HYS) |
902                              (0 << LPC_IOCONF_INV) |
903                              (0 << LPC_IOCONF_OD) |
904                              0x80);
905 #endif
906         /* Enable USB registers and RAM */
907         lpc_scb.sysahbclkctrl |= ((1 << LPC_SCB_SYSAHBCLKCTRL_USB) |
908                                   (1 << LPC_SCB_SYSAHBCLKCTRL_USBRAM));
909
910         /* Enable USB PHY */
911         lpc_scb.pdruncfg &= ~(1 << LPC_SCB_PDRUNCFG_USBPAD_PD);
912
913         /* Turn on USB PLL */
914         lpc_scb.pdruncfg &= ~(1 << LPC_SCB_PDRUNCFG_USBPLL_PD);
915
916         lpc_scb.usbpllclksel = (LPC_SCB_SYSPLLCLKSEL_SEL_SYSOSC << LPC_SCB_SYSPLLCLKSEL_SEL);
917         lpc_scb.usbpllclkuen = (0 << LPC_SCB_USBPLLCLKUEN_ENA);
918         lpc_scb.usbpllclkuen = (1 << LPC_SCB_USBPLLCLKUEN_ENA);
919         while (!(lpc_scb.usbpllclkuen & (1 << LPC_SCB_USBPLLCLKUEN_ENA)))
920                 ;
921         lpc_scb.usbpllctrl = 0x23;
922         while (!(lpc_scb.usbpllstat & 1))
923                 ;
924
925         lpc_scb.usbclksel = 0;
926         lpc_scb.usbclkuen = (0 << LPC_SCB_USBCLKUEN_ENA);
927         lpc_scb.usbclkuen = (1 << LPC_SCB_USBCLKUEN_ENA);
928         while (!(lpc_scb.usbclkuen & (1 << LPC_SCB_USBCLKUEN_ENA)))
929                 ;
930
931         /* Turn on USB clock, use 48MHz clock unchanged */
932         lpc_scb.usbclkdiv = 1;
933
934         /* Configure interrupts */
935         ao_arch_block_interrupts();
936
937         /* Route all interrupts to the main isr */
938         lpc_usb.introuting = 0;
939
940         /* Configure NVIC */
941
942         lpc_nvic_set_enable(LPC_ISR_USB_IRQ_POS);
943         lpc_nvic_set_priority(LPC_ISR_USB_IRQ_POS, 0);
944
945         /* Clear any spurious interrupts */
946         lpc_usb.intstat = 0xffffffff;
947
948         debug ("ao_usb_enable\n");
949
950         /* Enable interrupts */
951         lpc_usb.inten = ((1 << LPC_USB_INT_EPOUT(0)) |
952                          (1 << LPC_USB_INT_EPIN(0)) |
953                          (1 << LPC_USB_INT_EPIN(AO_USB_INT_EP)) |
954                          (1 << LPC_USB_INT_EPOUT(AO_USB_OUT_EP)) |
955                          (1 << LPC_USB_INT_EPIN(AO_USB_IN_EP)) |
956                          (1 << LPC_USB_INT_DEV));
957
958         ao_arch_release_interrupts();
959
960         lpc_usb.devcmdstat = 0;
961         for (t = 0; t < 1000; t++)
962                 ao_arch_nop();
963
964         ao_usb_sram = lpc_usb_sram;
965
966         ao_usb_ep0_rx_buffer    = ao_usb_alloc_sram(AO_USB_CONTROL_SIZE);
967         ao_usb_ep0_tx_buffer    = ao_usb_alloc_sram(AO_USB_CONTROL_SIZE);
968         ao_usb_ep0_setup_buffer = ao_usb_alloc_sram(AO_USB_CONTROL_SIZE);
969
970         ao_usb_out_rx_buffer[0] = ao_usb_alloc_sram(AO_USB_OUT_SIZE);
971         ao_usb_out_rx_buffer[1] = ao_usb_alloc_sram(AO_USB_OUT_SIZE);
972         ao_usb_in_tx_buffer[0]  = ao_usb_alloc_sram(AO_USB_IN_SIZE);
973         ao_usb_in_tx_buffer[1]  = ao_usb_alloc_sram(AO_USB_IN_SIZE);
974
975         ao_usb_set_ep0();
976
977 #if HAS_USB_PULLUP
978         ao_gpio_set(AO_USB_PULLUP_PORT, AO_USB_PULLUP_PIN, 1);
979 #endif
980 }
981
982 #if USB_ECHO
983 struct ao_task ao_usb_echo_task;
984
985 static void
986 ao_usb_echo(void)
987 {
988         char    c;
989
990         for (;;) {
991                 c = ao_usb_getchar();
992                 ao_usb_putchar(c);
993                 ao_usb_flush();
994         }
995 }
996 #endif
997
998 #if USB_DEBUG
999 static void
1000 ao_usb_irq(void)
1001 {
1002         printf ("control: %d out: %d in: %d int: %d reset: %d\n",
1003                 control_count, out_count, in_count, int_count, reset_count);
1004 }
1005
1006 const struct ao_cmds ao_usb_cmds[] = {
1007         { ao_usb_irq, "I\0Show USB interrupt counts" },
1008         { 0, NULL }
1009 };
1010 #endif
1011
1012 void
1013 ao_usb_init(void)
1014 {
1015 #if HAS_USB_PULLUP
1016         int     i;
1017         ao_enable_output(AO_USB_PULLUP_PORT, AO_USB_PULLUP_PIN, 0);
1018
1019         for (i = 0; i < 40000; i++)
1020                 ao_arch_nop();
1021 #endif
1022         ao_usb_enable();
1023
1024         debug ("ao_usb_init\n");
1025 #if USB_ECHO
1026         ao_add_task(&ao_usb_echo_task, ao_usb_echo, "usb echo");
1027 #endif
1028 #if USB_DEBUG
1029         ao_cmd_register(&ao_usb_cmds[0]);
1030 #endif
1031 #if USE_USB_STDIO
1032         ao_add_stdio(_ao_usb_pollchar, ao_usb_putchar, ao_usb_flush);
1033 #endif
1034 }
1035
1036 #if TX_DBG || RX_DBG
1037
1038 struct ao_usb_dbg {
1039         int             line;
1040         char            *msg;
1041         uint32_t        value;
1042         uint32_t        primask;
1043 #if TX_DBG
1044         uint16_t        in_count;
1045         uint32_t        in_ep;
1046         uint32_t        in_pending;
1047         uint32_t        tx_count;
1048         uint32_t        in_flushed;
1049 #endif
1050 #if RX_DBG
1051         uint8_t         rx_count;
1052         uint8_t         rx_pos;
1053         uint8_t         out_avail;
1054         uint32_t        out_ep;
1055 #endif
1056 };
1057
1058 #define NUM_USB_DBG     8
1059
1060 static struct ao_usb_dbg dbg[NUM_USB_DBG];
1061 static int dbg_i;
1062
1063 static void _dbg(int line, char *msg, uint32_t value)
1064 {
1065         uint32_t        primask;
1066         dbg[dbg_i].line = line;
1067         dbg[dbg_i].msg = msg;
1068         dbg[dbg_i].value = value;
1069         asm("mrs %0,primask" : "=&r" (primask));
1070         dbg[dbg_i].primask = primask;
1071 #if TX_DBG
1072         dbg[dbg_i].in_count = in_count;
1073         dbg[dbg_i].in_ep = *ao_usb_epn_in(AO_USB_IN_EP, 0);
1074         dbg[dbg_i].in_pending = ao_usb_in_pending;
1075         dbg[dbg_i].tx_count = ao_usb_tx_count;
1076         dbg[dbg_i].in_flushed = ao_usb_in_flushed;
1077 #endif
1078 #if RX_DBG
1079         dbg[dbg_i].rx_count = ao_usb_rx_count;
1080         dbg[dbg_i].rx_pos = ao_usb_rx_pos;
1081         dbg[dbg_i].out_avail = ao_usb_out_avail;
1082         dbg[dbg_i].out_ep = *ao_usb_epn_out(AO_USB_OUT_EP, 0);
1083 #endif
1084         if (++dbg_i == NUM_USB_DBG)
1085                 dbg_i = 0;
1086 }
1087 #endif