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