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