Add support to buildzip.pl for Lua scripts
[kugel-rb.git] / firmware / drivers / m66591.c
blob2ce121a26347fce61dcc0c680a64bf464720c1c8
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2009 by Karl Kurbjun
11 * Portions Copyright (C) 2007 by Catalin Patulea
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License
15 * as published by the Free Software Foundation; either version 2
16 * of the License, or (at your option) any later version.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ****************************************************************************/
22 #define LOGF_ENABLE
24 #include "system.h"
25 #include "config.h"
26 #include "string.h"
27 #include "usb_ch9.h"
28 #include "usb_core.h"
29 #include "kernel.h"
30 #include "panic.h"
31 #include "usb_drv.h"
32 #include "logf.h"
34 #include "cpu.h"
35 #include "ata.h"
36 #include "usb.h"
37 #include "usb-target.h"
38 #include "m66591.h"
40 /*******************************************************************************
41 * These are the driver specific defines.
42 ******************************************************************************/
44 /* This define is primarily intended for testing, using HISPEED all the time
45 * should be acceptable since the defice should down-train if the host does not
46 * support HISPEED.
48 #define HISPEED
50 /* Right now sending blocks till the full transfer has completed, this needs to
51 * be fixed so that it does not require a block. (USB_TRAN_LOCK ideally would
52 * not be set).
54 #define USB_TRAN_BLOCK
56 /*******************************************************************************
57 * The following functions are all helpers which should not be called directly
58 * from the USB stack. They should only be called by eachother, or the USB
59 * stack visible functions.
60 ******************************************************************************/
62 static volatile unsigned short * pipe_ctrl_addr(int pipe);
63 static void pipe_handshake(int pipe, int handshake);
64 static void pipe_c_select (int pipe, bool dir);
65 #if !defined(USB_TRAN_BLOCK)
66 static int pipe_buffer_size (int pipe);
67 #endif
68 static int pipe_maxpack_size (int pipe);
69 static void control_received(void);
70 static void transfer_complete(int endpoint);
71 static int mxx_transmit_receive(int endpoint);
72 static int mxx_queue(int endpoint, void * ptr, int length, bool send);
74 struct M66591_epstat {
75 unsigned char dir; /* endpoint direction */
76 char *buf; /* user buffer to store data */
77 int length; /* how match data will fit */
78 volatile int count; /* actual data count */
79 bool waiting; /* is there data to transfer? */
80 bool busy; /* has the pipe been requested for use? */
81 } ;
83 static struct M66591_epstat M66591_eps[USB_NUM_ENDPOINTS];
85 /* This function is used to return the control address for each pipe */
86 static volatile unsigned short * pipe_ctrl_addr(int pipe) {
87 if(pipe==0) {
88 return &M66591_DCPCTRL;
89 } else {
90 return &M66591_PIPECTRL1 + (pipe-1);
94 /* This function sets the pipe/endpoint handshake */
95 static void pipe_handshake(int pipe, int handshake) {
96 handshake&=0x03;
98 if(handshake == PIPE_SHAKE_STALL) {
99 if( *(pipe_ctrl_addr(pipe)) & 0x03 ) {
100 *(pipe_ctrl_addr(pipe)) = 0x03;
101 } else {
102 *(pipe_ctrl_addr(pipe)) = 0x02;
104 } else {
105 *(pipe_ctrl_addr(pipe)) = handshake;
109 /* This function chooses the pipe desired and waits the required time before
110 * warites/reads are valid */
111 static void pipe_c_select (int pipe, bool dir) {
112 M66591_CPORT_CTRL0 = pipe | (1<<10) | (dir<<5);
114 // Wait for the Pipe to be valid;
115 udelay(2);
118 #if !defined(USB_TRAN_BLOCK)
119 /* This returns the maximum buffer size of each pipe. On this device the size
120 * is fixed.
122 static int pipe_buffer_size (int pipe) {
123 switch(pipe) {
124 case 0:
125 return 256;
126 case 1:
127 case 2:
128 return 1024;
129 case 3:
130 case 4:
131 return 512;
132 case 5:
133 case 6:
134 return 64;
135 default:
136 return 0;
139 #endif
141 /* This function returns the maximum packet size for each endpoint/pipe. The
142 * max packet size is dependent on whether the device is running High or Full
143 * speed.
145 static int pipe_maxpack_size (int pipe) {
146 if( (M66591_HSFS & 0xFF) == 0x03 ) { /* Device is running Highspeed */
147 switch(pipe) {
148 case 0:
149 /* DCP max packet size is configurable */
150 return M66591_DCP_MXPKSZ;
151 case 1:
152 case 2:
153 case 3:
154 case 4:
155 return 512;
156 case 5:
157 case 6:
158 return 64;
159 default:
160 return 0;
162 } else { /* Device is running Full speed */
163 switch(pipe) {
164 case 0:
165 /* DCP max packet size is configurable */
166 return M66591_DCP_MXPKSZ;
167 case 1:
168 case 2:
169 case 3:
170 case 4:
171 return 64;
172 case 5:
173 case 6:
174 return 64;
175 default:
176 return 0;
181 /* This is a helper function that is only called from the interupt handler. It
182 * copies the control packet information from the PHY and notifies the stack.
184 static void control_received(void) {
185 /* copy setup data from packet */
186 static struct usb_ctrlrequest temp;
188 memcpy(&temp, (unsigned char*)&M66591_USB_REQ0, 8);
190 logf("mxx: bReqType=0x%02x bReq=0x%02x wVal=0x%04x"
191 " wIdx=0x%04x wLen=0x%04x",
192 temp.bRequestType, temp.bRequest, temp.wValue,
193 temp.wIndex, temp.wLength);
195 /* acknowledge packet recieved (clear valid) */
196 M66591_INTSTAT_MAIN &= ~(1<<3);
198 usb_core_control_request(&temp);
201 /* This is a helper function, it is used to notife the stack that a transfer is
202 * done.
204 static void transfer_complete(int endpoint) {
205 M66591_INTCFG_EMP &= ~(1 << endpoint);
206 logf("mxx: ep %d transfer complete", endpoint);
207 int temp=M66591_eps[endpoint].dir ? USB_DIR_IN : USB_DIR_OUT;
208 usb_core_transfer_complete(endpoint, temp, 0,
209 M66591_eps[endpoint].length);
212 /* This is the main transmit routine that is typically called from the interrupt
213 * handler (the queue function calls it in some situations)
215 static int mxx_transmit_receive(int endpoint) {
216 logf("mxx: do start");
218 /* Only the lower 15 bits of the endpoint correlate to the pipe number.
219 * For example pipe 2 will corelate to endpoint 0x82, so the upper bits
220 * need to be masked out.
222 endpoint &= 0x7F;
224 int i; /* Used as a loop counter */
225 int length; /* Used in transfers to determine the amount to send/receive */
227 bool send=M66591_eps[endpoint].dir;
229 /* This is used as the internal buffer pointer */
230 unsigned short *ptrs;
232 /* Choose the pipe that data is being transfered on */
233 pipe_c_select(endpoint, send);
235 /* Check to see if the endpoint is ready and give it some time to become
236 * ready. If it runs out of time exit out as an error.
238 i = 0;
239 while (!(M66591_CPORT_CTRL1&(1<<13))) {
240 if (i++ > 100000) {
241 logf("mxx: FIFO %d not ready", endpoint);
242 return -1;
246 /* Write to FIFO */
247 if(send) {
248 int maxpack=pipe_maxpack_size(endpoint);
249 #if defined(USB_TRAN_BLOCK)
250 length = M66591_eps[endpoint].length;
251 #else
252 int bufsize=pipe_buffer_size(endpoint);
253 length=MIN(M66591_eps[endpoint].length, bufsize);
254 #endif
256 /* Calculate the position in the buffer, all transfers should be 2-byte
257 * aligned till the last packet or short packet.
259 ptrs = (unsigned short *)(M66591_eps[endpoint].buf
260 + M66591_eps[endpoint].count);
262 /* Start sending data in 16-bit words */
263 for (i = 0; i < (length>>1); i++) {
264 /* This wait is dangerous in the event that something happens to
265 * the PHY pipe where it never becomes ready again, should probably
266 * add a timeout, and ideally completely remove.
268 while(!(M66591_CPORT_CTRL1&(1<<13))){};
270 M66591_CPORT = *ptrs++;
271 M66591_eps[endpoint].count+=2;
274 /* If the length is odd, send the last byte after setting the byte width
275 * of the FIFO.
277 if(length & 0x01) {
278 /* Unset MBW (8-bit transfer) */
279 M66591_CPORT_CTRL0 &= ~(1<<10);
280 M66591_CPORT = *((unsigned char *)ptrs - 1);
281 M66591_eps[endpoint].count++;
284 /* Set BVAL if length is not a multiple of the maximum packet size */
285 if( (length == 0) || (length % maxpack != 0) ) {
286 logf("mxx: do set BVAL");
287 M66591_CPORT_CTRL1 |= (1<<15);
290 /* If the transfer is complete set up interrupts to notify when FIFO is
291 * EMPTY, disable READY and let the handler know that there is nothing
292 * left to transfer on this pipe.
294 if(M66591_eps[endpoint].count == M66591_eps[endpoint].length) {
295 /* Enable Empty flag */
296 M66591_INTCFG_EMP |= 1 << endpoint;
297 /* Disable ready flag */
298 M66591_INTCFG_RDY &= ~(1 << endpoint);
299 /* Nothing left to transfer */
300 M66591_eps[endpoint].waiting=false;
301 } else {
302 /* There is still data to transfer, make sure READY is enabled */
303 M66591_INTCFG_RDY |= 1 << endpoint;
305 } else {
306 /* Read data from FIFO */
308 /* Read the number of bytes that the PHY received */
309 int receive_length=M66591_CPORT_CTRL1 & 0x03FF;
311 /* The number of bytes to actually read is either what's left of the
312 * amount requested, or the amount that the PHY received. Choose the
313 * smaller of the two.
315 length = MIN(M66591_eps[endpoint].length - M66591_eps[endpoint].count,
316 receive_length);
318 /* If the length is zero, just clear the buffer as specified in the
319 * datasheet. Otherwise read in the data (in 16-bit pieces */
320 if(length==0) {
321 /* Set the BCLR bit */
322 M66591_CPORT_CTRL1 |= 1<<14;
323 } else {
324 /* Set the position in the buffer */
325 ptrs = (unsigned short *)(M66591_eps[endpoint].buf
326 + M66591_eps[endpoint].count);
328 /* Read in the data (buffer size should be even). The PHY cannot
329 * switch from 16-bit mode to 8-bit mode on an OUT buffer.
331 for (i = 0; i < ((length+1)>>1); i++) {
332 *ptrs++ = M66591_CPORT;
333 M66591_eps[endpoint].count+=2;
337 /* If the length was odd subtract 1 from the count */
338 M66591_eps[endpoint].count -= (length&0x01);
340 /* If the requested size of data was received, or the data received was
341 * less than the maximum packet size end the transfer.
343 if( (M66591_eps[endpoint].count == M66591_eps[endpoint].length)
344 || (length % pipe_maxpack_size(endpoint)) ) {
346 /* If the host tries to send anything else the FIFO is not ready/
347 * enabled yet (NAK).
349 pipe_handshake(endpoint, PIPE_SHAKE_NAK);
350 /* Tell the interrupt handler that transfer is complete. */
351 M66591_eps[endpoint].waiting=false;
352 /* Disable ready */
353 M66591_INTCFG_RDY &= ~(1 << endpoint);
355 /* Let the stack know that the transfer is complete */
356 if(endpoint!=0)
357 transfer_complete(endpoint);
361 logf("mxx: do done ep %d %s len: %d cnt: %d", endpoint,
362 send ? "out" : "in", length, M66591_eps[endpoint].count);
364 return 0;
367 /* This function is used to start transfers. It is a helper function for the
368 * usb_drv_send_nonblocking, usb_drv_send, and usb_drv_receive functions.
370 static int mxx_queue(int endpoint, void * ptr, int length, bool send) {
371 /* Disable IRQs */
372 int flags = disable_irq_save();
374 /* Only the lower 15 bits of the endpoint correlate to the pipe number.
375 * For example pipe 2 will corelate to endpoint 0x82, so the upper bits
376 * need to be masked out.
378 endpoint &= 0x7F;
380 /* Initialize the enpoint status registers used for the transfer */
381 M66591_eps[endpoint].buf=ptr;
382 M66591_eps[endpoint].length=length;
383 M66591_eps[endpoint].count=0;
384 M66591_eps[endpoint].dir=send;
385 M66591_eps[endpoint].waiting=true;
387 logf("mxx: queue ep %d %s, len: %d", endpoint, send ? "out" : "in", length);
389 /* Pick the pipe that communications are happening on */
390 pipe_c_select(endpoint, send);
392 /* All transfers start with a BUF handshake */
393 pipe_handshake(endpoint, PIPE_SHAKE_BUF);
395 /* This USB PHY takes care of control completion packets by setting the
396 * CCPL bit in EP0 (endpoint 0, or DCP). If the control state is "write no
397 * data tranfer" then we just need to set the CCPL bit (hopefully)
398 * regardless of what the stack said to send.
400 int control_state = (M66591_INTSTAT_MAIN & 0x07);
401 if(endpoint==0 && control_state==CTRL_WTND) {
402 logf("mxx: queue ep 0 ctls: 5, set ccpl");
404 /* Set CCPL */
405 M66591_DCPCTRL |= 1<<2;
406 } else {
407 /* This is the standard case for transmitting data */
408 if(send) {
409 /* If the pipe is not ready don't try and send right away; instead
410 * just set the READY interrupt so that the handler can initiate
411 * the transfer.
413 if((M66591_CPORT_CTRL1&(1<<13))) {
414 mxx_transmit_receive(endpoint);
415 } else {
416 M66591_INTCFG_RDY |= 1 << endpoint;
419 if(length==0) {
420 transfer_complete(endpoint);
422 } else {
423 /* When receiving data, just enable the ready interrupt, the PHY
424 * will trigger it and then the reads can start.
426 M66591_INTCFG_RDY |= 1 << endpoint;
430 /* Re-enable IRQs */
431 restore_irq(flags);
432 return 0;
435 /*******************************************************************************
436 * This is the interrupt handler for this driver. It should be called from the
437 * target interrupt handler routine (eg. GPIO3 on M:Robe 500).
438 ******************************************************************************/
439 void USB_DEVICE(void) {
440 int pipe_restore=M66591_CPORT_CTRL0;
441 logf("mxx: INT BEGIN tick: %d\n", (int) current_tick);
443 logf("mxx: sMAIN0: 0x%04x, sRDY: 0x%04x",
444 M66591_INTSTAT_MAIN, M66591_INTSTAT_RDY);
445 logf("mxx: sNRDY: 0x%04x, sEMP: 0x%04x",
446 M66591_INTSTAT_NRDY, M66591_INTSTAT_EMP);
448 /* VBUS (connected) interrupt */
449 while ( M66591_INTSTAT_MAIN & (1<<15) ) {
450 M66591_INTSTAT_MAIN &= ~(1<<15);
452 /* If device is not clocked, interrupt flag must be set manually */
453 if ( !(M66591_TRN_CTRL & (1<<10)) ) {
454 M66591_INTSTAT_MAIN |= (1<<15);
458 /* Resume interrupt: This is not used. Extra logic needs to be added similar
459 * to the VBUS interrupt incase the PHY clock is not running.
461 if(M66591_INTSTAT_MAIN & (1<<14)) {
462 M66591_INTSTAT_MAIN &= ~(1<<14);
463 logf("mxx: RESUME");
466 /* Device state transition interrupt: Not used, but useful for debugging */
467 if(M66591_INTSTAT_MAIN & (1<<12)) {
468 M66591_INTSTAT_MAIN &= ~(1<<12);
469 logf("mxx: DEV state CHANGE=%d",
470 ((M66591_INTSTAT_MAIN & (0x07<<4)) >> 4) );
473 /* Control transfer stage interrupt */
474 if(M66591_INTSTAT_MAIN & (1<<11)) {
475 M66591_INTSTAT_MAIN &= ~(1<<11);
476 int control_state = (M66591_INTSTAT_MAIN & 0x07);
478 logf("mxx: CTRT with CTSQ=%d", control_state);
480 switch ( control_state ) {
481 case CTRL_IDLE:
482 transfer_complete(0);
483 break;
484 case CTRL_RTDS:
485 case CTRL_WTDS:
486 case CTRL_WTND:
487 /* If data is not valid stop */
488 if(!(M66591_INTSTAT_MAIN & (1<<3)) ) {
489 logf("mxx: CTRT interrupt but VALID is false");
490 break;
492 control_received();
493 break;
494 case CTRL_RTSS:
495 case CTRL_WTSS:
496 pipe_handshake(0, PIPE_SHAKE_BUF);
497 M66591_DCPCTRL |= 1<<2; /* Set CCPL */
498 break;
499 default:
500 logf("mxx: CTRT with unknown CTSQ");
501 break;
505 /* FIFO EMPTY interrupt: when this happens the transfer should be complete.
506 * When the interrupt occurs notify the stack.
508 if(M66591_INTSTAT_MAIN & (1<<10)) {
509 int i;
510 logf("mxx: INT EMPTY: 0x%04x", M66591_INTSTAT_EMP);
512 for(i=0; i<USB_NUM_ENDPOINTS; i++) {
513 if(M66591_INTSTAT_EMP&(1<<i)) {
514 /* Clear the empty flag */
515 M66591_INTSTAT_EMP=~(1<<i);
516 /* Notify the stack */
517 transfer_complete(i);
522 /* FIFO NOT READY interrupt: This is not used, but included incase the
523 * interrupt is endabled.
525 if(M66591_INTSTAT_MAIN & (1<<9)) {
526 logf("mxx: INT NOT READY: 0x%04x", M66591_INTSTAT_NRDY);
527 M66591_INTSTAT_NRDY = 0;
530 /* FIFO READY interrupt: This just initiates transfers if they are needed */
531 if(M66591_INTSTAT_MAIN & (1<<8)) {
532 int i;
533 logf("mxx: INT READY: 0x%04x", M66591_INTSTAT_RDY);
535 for(i=0; i<USB_NUM_ENDPOINTS; i++) {
536 /* Was this endpoint ready and waiting */
537 if(M66591_INTSTAT_RDY&(1<<i) && M66591_eps[i].waiting) {
538 /* Clear the ready flag */
539 M66591_INTSTAT_RDY=~(1<<i);
540 /* It was ready and waiting so start a transfer */
541 mxx_transmit_receive(i);
546 /* Make sure that the INTStatus register is completely cleared. */
547 M66591_INTSTAT_MAIN = 0;
549 /* Restore the pipe state before the interrupt occured */
550 M66591_CPORT_CTRL0=pipe_restore;
551 logf("\nmxx: INT END");
554 /*******************************************************************************
555 * The following functions are all called by and visible to the USB stack.
556 ******************************************************************************/
558 /* The M55691 handles this automatically, nothing to do */
559 void usb_drv_set_address(int address) {
560 (void) address;
563 /* This function sets the standard test modes, it is not required, but might as
564 * well implement it since the hardware supports it
566 void usb_drv_set_test_mode(int mode) {
567 /* This sets the test bits and assumes that mode is from 0 to 0x04 */
568 M66591_TESTMODE &= 0x0007;
569 M66591_TESTMODE |= mode;
572 /* Request an unused endpoint, support for interrupt endpoints needs addition */
573 int usb_drv_request_endpoint(int type, int dir) {
574 int ep;
575 int pipecfg = 0;
577 if (type == USB_ENDPOINT_XFER_BULK) {
578 /* Enable double buffer mode (only used for ep 1 and 2) */
579 pipecfg |= 1<<9;
581 /* Bulk endpoints must be between 1 and 4 inclusive */
582 ep=1;
584 while(M66591_eps[ep].busy && ep++<5);
586 /* If this reached 5 the endpoints were all busy */
587 if(ep==5) {
588 logf("mxx: ep %d busy", ep);
589 return -1;
591 } else if (type == USB_ENDPOINT_XFER_INT) {
592 ep=5;
594 while(M66591_eps[ep].busy && ep++<7);
596 /* If this reached 7 the endpoints were all busy */
597 if(ep==7) {
598 logf("mxx: ep %d busy", ep);
599 return -1;
601 } else {
602 /* Not a supported type */
603 return -1;
606 if (dir == USB_DIR_IN) {
607 pipecfg |= (1<<4);
610 M66591_eps[ep].busy = true;
611 M66591_eps[ep].dir = dir;
613 M66591_PIPE_CFGSEL=ep;
615 /* Enable pipe (15) and continuous transfer mode (8) */
616 pipecfg |= 1<<15 | 1<<8;
618 pipe_handshake(ep, PIPE_SHAKE_NAK);
620 /* Setup the flags */
621 M66591_PIPE_CFGWND=pipecfg;
623 logf("mxx: ep req ep#: %d config: 0x%04x", ep, M66591_PIPE_CFGWND);
625 return ep | dir;
628 /* Used by stack to tell the helper functions that the pipe is not in use */
629 void usb_drv_release_endpoint(int ep) {
630 int flags;
631 ep &= 0x7f;
633 if (ep < 1 || ep > USB_NUM_ENDPOINTS || M66591_eps[ep].busy == false)
634 return ;
636 flags = disable_irq_save();
638 logf("mxx: ep %d release", ep);
640 M66591_eps[ep].busy = false;
641 M66591_eps[ep].dir = -1;
643 restore_irq(flags);
646 /* Periodically called to check if a cable was plugged into the device */
647 inline int usb_detect(void)
649 if(M66591_INTSTAT_MAIN&(1<<7))
650 return USB_INSERTED;
651 else
652 return USB_EXTRACTED;
655 void usb_enable(bool on) {
656 logf("mxx: %s: %s", __FUNCTION__, on ? "true" : "false");
657 if (on)
658 usb_core_init();
659 else
660 usb_core_exit();
663 /* This is where the driver stuff starts */
664 void usb_drv_init(void) {
665 logf("mxx: Device Init");
667 /* State left behind by m:robe 500i original firmware */
668 M66591_TRN_CTRL = 0x8001; /* External 48 MHz clock */
669 M66591_TRN_LNSTAT = 0x0040; /* "Reserved. Set it to '1'." */
671 M66591_PIN_CFG0 = 0x0000;
672 M66591_PIN_CFG1 = 0x8000; /* Drive Current: 3.3V setting */
673 M66591_PIN_CFG2 = 0x0000;
675 M66591_INTCFG_MAIN = 0x0000; /* All Interrupts Disable for now */
676 M66591_INTCFG_OUT = 0x0000; /* Sense is edge, polarity is low */
677 M66591_INTCFG_RDY = 0x0000;
678 M66591_INTCFG_NRDY = 0x0000;
679 M66591_INTCFG_EMP = 0x0000;
681 M66591_INTSTAT_MAIN = 0;
682 M66591_INTSTAT_RDY = 0;
683 M66591_INTSTAT_NRDY = 0;
684 M66591_INTSTAT_EMP = 0;
687 /* fully enable driver */
688 void usb_attach(void) {
689 int i;
691 /* Reset Endpoint states */
692 for(i=0; i<USB_NUM_ENDPOINTS; i++) {
693 M66591_eps[i].dir = -1;
694 M66591_eps[i].buf = (char *) 0;
695 M66591_eps[i].length = 0;
696 M66591_eps[i].count = 0;
697 M66591_eps[i].waiting = false;
698 M66591_eps[i].busy = false;
701 /* Issue a h/w reset */
702 usb_init_device();
703 usb_core_init();
705 /* USB Attach Process: This follows the flow diagram in the M66591GP
706 * Reference Manual Rev 1.00, p. 77 */
708 #if defined(HISPEED)
709 /* Run Hi-Speed */
710 M66591_TRN_CTRL |= 1<<7;
711 #else
712 /* Run Full-Speed */
713 M66591_TRN_CTRL &= ~(1<<7);
714 #endif
716 /* Enable oscillation buffer */
717 M66591_TRN_CTRL |= (1<<13);
719 udelay(1500);
721 /* Enable reference clock, PLL */
722 M66591_TRN_CTRL |= (3<<11);
724 udelay(9);
726 /* Enable internal clock supply */
727 M66591_TRN_CTRL |= (1<<10);
729 /* Disable PIPE ready interrupts */
730 M66591_INTCFG_RDY = 0;
732 /* Disable PIPE not-ready interrupts */
733 M66591_INTCFG_NRDY = 0;
735 /* Disable PIPE empyt/size error interrupts */
736 M66591_INTCFG_EMP = 0;
738 /* Enable all interrupts except NOT READY, RESUME, and VBUS */
739 M66591_INTCFG_MAIN = 0x1DFF;
741 pipe_c_select(0, false);
743 /* Enable continuous transfer mode on the DCP */
744 M66591_DCP_CNTMD |= (1<<8);
746 /* Set the threshold that the PHY will automatically transmit from EP0 */
747 M66591_DCP_CTRLEN = 128;
749 pipe_handshake(0, PIPE_SHAKE_NAK);
751 /* Set the Max packet size to 64 */
752 M66591_DCP_MXPKSZ = 64;
754 /* Attach notification to PC (D+ pull-up) */
755 M66591_TRN_CTRL |= (1<<4);
757 logf("mxx: attached");
760 void usb_drv_exit(void) {
761 /* USB Detach Process: This follows the flow diagram in the M66591GP
762 * Reference Manual Rev 1.00, p. 78.
765 /* Detach notification to PC (disable D+ pull-up) */
766 M66591_TRN_CTRL &= ~(1<<4);
768 /* Software reset */
769 M66591_TRN_CTRL &= ~0x01;
771 /* Disable internal clock supply */
772 M66591_TRN_CTRL &= ~(1<<10);
773 udelay(3);
775 /* Disable PLL */
776 M66591_TRN_CTRL &= ~(1<<11);
777 udelay(3);
779 /* Disable internal reference clock */
780 M66591_TRN_CTRL &= ~(1<<12);
781 udelay(3);
783 /* Disable oscillation buffer, reenable USB operation */
784 M66591_TRN_CTRL &= ~(1<<13);
786 M66591_TRN_CTRL |= 0x01;
788 logf("mxx: detached");
791 /* This function begins a transmit (on an IN endpoint), it should not block
792 * so the actual transmit is done in the interrupt handler.
794 int usb_drv_send_nonblocking(int endpoint, void* ptr, int length)
796 /* The last arguement for queue specifies the dir of data (true==send) */
797 return mxx_queue(endpoint, ptr, length, true);
800 /* This function begins a transmit (on an IN endpoint), it does not block
801 * so the actual transmit is done in the interrupt handler.
803 int usb_drv_send(int endpoint, void* ptr, int length)
805 /* The last arguement for queue specifies the dir of data (true==send) */
806 return mxx_queue(endpoint, ptr, length, true);
809 /* This function begins a receive (on an OUT endpoint), it should not block
810 * so the actual receive is done in the interrupt handler.
812 int usb_drv_recv(int endpoint, void* ptr, int length)
814 /* Last arguement for queue specifies the dir of data (false==receive) */
815 return mxx_queue(endpoint, ptr, length, false);
818 /* This function checks the reset handshake speed status
819 * (Fullspeed or Highspeed)
821 int usb_drv_port_speed(void)
823 int handshake = (M66591_HSFS & 0xFF);
825 if( handshake == 0x02) {
826 return 0; /* Handshook at Full-Speed */
827 } else if( handshake == 0x03) {
828 return 1; /* Handshook at Hi-Speed */
829 } else {
830 return -1; /* Error, handshake may not be complete */
834 /* This function checks if the endpoint is stalled (error). I am not sure what
835 * the "in" variable is intended for.
837 bool usb_drv_stalled(int endpoint,bool in)
839 (void) in;
841 bool stalled = (*(pipe_ctrl_addr(endpoint)) & (0x02)) ? true : false;
843 logf("mxx: stall?: %s ep: %d", stalled ? "true" : "false", endpoint);
845 if(stalled) {
846 return true;
847 } else {
848 return false;
852 /* This function stalls/unstalls the endpoint. Stalls only happen on error so
853 * if the endpoint is functioning properly this should not be called. I am
854 * not sure what the "in" variable is intended for.
856 void usb_drv_stall(int endpoint, bool stall,bool in)
858 (void) in;
860 logf("mxx: stall - ep: %d", endpoint);
862 if(stall) {
863 /* Stall the pipe (host needs to intervene/error) */
864 pipe_handshake(endpoint, PIPE_SHAKE_STALL);
865 } else {
866 /* Setting this to a NAK, not sure if it is appropriate */
867 pipe_handshake(endpoint, PIPE_SHAKE_NAK);
871 /* !!!!!!!!!!This function is likely incomplete!!!!!!!!!!!!!! */
872 void usb_drv_cancel_all_transfers(void)
874 int endpoint;
875 int flags;
877 logf("mxx: %s", __func__);
879 flags = disable_irq_save();
880 for (endpoint = 0; endpoint < USB_NUM_ENDPOINTS; endpoint++) {
881 if (M66591_eps[endpoint].buf) {
882 M66591_eps[endpoint].buf = NULL;
886 restore_irq(flags);