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