1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2008 by Maurus Cuelenaere
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
23 /*#define LOGF_ENABLE*/
29 #include "usb-target.h"
34 The Jz4740 USB controller is called MUSBHSFC in the datasheet.
35 It also seems to be a more generic controller, with support for
36 up to 15 endpoints (the Jz4740 only has 5).
39 #define EP_BUF_LEFT(ep) ((ep)->length - (ep)->sent)
40 #define EP_PTR(ep) ((void*)((unsigned int)(ep)->buf + (ep)->sent))
41 #define EP_NUMBER(ep) (((int)(ep) - (int)&endpoints[0])/sizeof(struct usb_endpoint))
42 #define EP_NUMBER2(ep) (EP_NUMBER((ep))/2)
43 #define TOTAL_EP() (sizeof(endpoints)/sizeof(struct usb_endpoint))
44 #define EP_IS_IN(ep) (EP_NUMBER((ep))%2)
65 const enum ep_type type
;
69 unsigned short fifo_size
;
75 static unsigned char ep0_rx_buf
[64];
76 static struct usb_endpoint endpoints
[] =
78 { .type
= ep_control
, .fifo_addr
= USB_FIFO_EP0
, .fifo_size
= 64 },
79 { .type
= ep_control
, .fifo_addr
= USB_FIFO_EP0
, .buf
= &ep0_rx_buf
},
80 { .type
= ep_bulk
, .fifo_addr
= USB_FIFO_EP1
, .fifo_size
= 512 },
81 { .type
= ep_bulk
, .fifo_addr
= USB_FIFO_EP1
, .fifo_size
= 512 },
82 { .type
= ep_interrupt
, .fifo_addr
= USB_FIFO_EP2
, .fifo_size
= 64 },
85 static inline void select_endpoint(int ep
)
87 REG_USB_REG_INDEX
= ep
;
90 static void readFIFO(struct usb_endpoint
*ep
, unsigned int size
)
92 logf("%s(EP%d, %d)", __func__
, EP_NUMBER2(ep
), size
);
94 register unsigned char *ptr
= (unsigned char*)EP_PTR(ep
);
95 register unsigned int *ptr32
= (unsigned int*)ptr
;
96 register unsigned int s
= size
>> 2;
97 register unsigned int x
;
101 if( ((unsigned int)ptr
& 3) == 0 )
104 *ptr32
++ = REG32(ep
->fifo_addr
);
106 ptr
= (unsigned char*)ptr32
;
112 x
= REG32(ep
->fifo_addr
);
113 *ptr
++ = x
& 0xFF; x
>>= 8;
114 *ptr
++ = x
& 0xFF; x
>>= 8;
115 *ptr
++ = x
& 0xFF; x
>>= 8;
122 *ptr
++ = REG8(ep
->fifo_addr
);
126 static void writeFIFO(struct usb_endpoint
*ep
, size_t size
)
128 logf("%s(EP%d, %d)", __func__
, EP_NUMBER2(ep
), size
);
130 register unsigned int *d32
= (unsigned int *)EP_PTR(ep
);
131 register size_t s
= size
>> 2;
136 REG32(ep
->fifo_addr
) = *d32
++;
140 register unsigned char *d8
= (unsigned char *)d32
;
142 REG8(ep
->fifo_addr
) = *d8
++;
147 static void flushFIFO(struct usb_endpoint
*ep
)
149 logf("%s(%d)", __func__
, EP_NUMBER(ep
));
160 REG_USB_REG_INCSR
|= (USB_INCSR_FF
| USB_INCSR_CDT
);
162 REG_USB_REG_OUTCSR
|= (USB_OUTCSR_FF
| USB_OUTCSR_CDT
);
167 static inline void ep_transfer_completed(struct usb_endpoint
* ep
)
174 wakeup_signal(&ep
->wakeup
);
177 static void EP0_send(void)
179 struct usb_endpoint
* ep
= &endpoints
[0];
184 csr0
= REG_USB_REG_CSR0
;
187 length
= MIN(ep
->length
, ep
->fifo_size
);
189 length
= MIN(EP_BUF_LEFT(ep
), ep
->fifo_size
);
191 writeFIFO(ep
, length
);
194 if(ep
->sent
>= ep
->length
)
196 REG_USB_REG_CSR0
= (csr0
| USB_CSR0_INPKTRDY
| USB_CSR0_DATAEND
); /* Set data end! */
197 usb_core_transfer_complete(0, USB_DIR_IN
, 0, ep
->sent
);
198 ep_transfer_completed(ep
);
201 REG_USB_REG_CSR0
= (csr0
| USB_CSR0_INPKTRDY
);
204 static void EP0_handler(void)
206 logf("%s()", __func__
);
209 struct usb_endpoint
*ep_send
= &endpoints
[0];
210 struct usb_endpoint
*ep_recv
= &endpoints
[1];
214 csr0
= REG_USB_REG_CSR0
;
216 /* Check for SentStall:
217 This bit is set when a STALL handshake is transmitted. The CPU should clear this bit.
219 if(csr0
& USB_CSR0_SENTSTALL
)
221 REG_USB_REG_CSR0
= csr0
& ~USB_CSR0_SENTSTALL
;
225 /* Check for SetupEnd:
226 This bit will be set when a control transaction ends before the DataEnd bit has been set.
227 An interrupt will be generated and the FIFO flushed at this time.
228 The bit is cleared by the CPU writing a 1 to the ServicedSetupEnd bit.
230 if(csr0
& USB_CSR0_SETUPEND
)
232 REG_USB_REG_CSR0
= csr0
| USB_CSR0_SVDSETUPEND
;
236 /* Call relevant routines for endpoint 0 state */
239 else if(csr0
& USB_CSR0_OUTPKTRDY
) /* There is a packet in the fifo */
241 readFIFO(ep_recv
, REG_USB_REG_COUNT0
);
242 REG_USB_REG_CSR0
= csr0
| USB_CSR0_SVDOUTPKTRDY
; /* clear OUTPKTRDY bit */
243 usb_core_control_request((struct usb_ctrlrequest
*)ep_recv
->buf
);
247 static void EPIN_handler(unsigned int endpoint
)
249 struct usb_endpoint
* ep
= &endpoints
[endpoint
*2];
250 unsigned int length
, csr
;
252 select_endpoint(endpoint
);
253 csr
= REG_USB_REG_INCSR
;
254 logf("%s(%d): 0x%x", __func__
, endpoint
, csr
);
258 logf("Entered EPIN handler without work!");
262 if(csr
& USB_INCSR_SENTSTALL
)
264 REG_USB_REG_INCSR
= csr
& ~USB_INCSR_SENTSTALL
;
271 if(csr
& USB_INCSR_FFNOTEMPT
)
273 logf("FIFO is not empty! 0x%x", csr
);
277 logf("EP%d: %d -> %d", endpoint
, ep
->sent
, ep
->length
);
280 length
= MIN(ep
->length
, ep
->fifo_size
);
282 length
= MIN(EP_BUF_LEFT(ep
), ep
->fifo_size
);
284 writeFIFO(ep
, length
);
285 REG_USB_REG_INCSR
= csr
| USB_INCSR_INPKTRDY
;
288 if(ep
->sent
>= ep
->length
)
290 usb_core_transfer_complete(endpoint
, USB_DIR_IN
, 0, ep
->sent
);
291 ep_transfer_completed(ep
);
292 logf("sent complete");
296 static void EPOUT_handler(unsigned int endpoint
)
298 struct usb_endpoint
* ep
= &endpoints
[endpoint
*2+1];
299 unsigned int size
, csr
;
303 logf("Entered EPOUT handler without work!");
307 select_endpoint(endpoint
);
308 while((csr
= REG_USB_REG_OUTCSR
) & (USB_OUTCSR_SENTSTALL
|USB_OUTCSR_OUTPKTRDY
))
310 logf("%s(%d): 0x%x", __func__
, endpoint
, csr
);
311 if(csr
& USB_OUTCSR_SENTSTALL
)
313 logf("stall sent, flushing fifo..");
315 REG_USB_REG_OUTCSR
= csr
& ~USB_OUTCSR_SENTSTALL
;
322 if(csr
& USB_OUTCSR_OUTPKTRDY
) /* There is a packet in the fifo */
324 size
= REG_USB_REG_OUTCOUNT
;
327 ep
->received
+= size
;
329 /*if(csr & USB_OUTCSR_FFFULL)
330 csr &= ~USB_OUTCSR_FFFULL;*/
332 REG_USB_REG_OUTCSR
= csr
& ~USB_OUTCSR_OUTPKTRDY
;
334 logf("received: %d max length: %d", ep
->received
, ep
->length
);
336 if(size
< ep
->fifo_size
|| ep
->received
>= ep
->length
)
338 usb_core_transfer_complete(endpoint
, USB_DIR_OUT
, 0, ep
->received
);
339 ep_transfer_completed(ep
);
340 logf("receive transfer_complete");
346 static void EPDMA_handler(int number
)
349 unsigned int size
= 0;
351 if(number
== USB_INTR_DMA_BULKIN
)
352 endpoint
= (REG_USB_REG_CNTL1
>> 4) & 0xF;
353 else if(number
== USB_INTR_DMA_BULKOUT
)
354 endpoint
= (REG_USB_REG_CNTL2
>> 4) & 0xF;
356 struct usb_endpoint
* ep
= &endpoints
[endpoint
];
357 logf("DMA_BULK%d %d", number
, endpoint
);
359 if(number
== USB_INTR_DMA_BULKIN
)
360 size
= (unsigned int)ep
->buf
- REG_USB_REG_ADDR1
;
361 else if(number
== USB_INTR_DMA_BULKOUT
)
362 size
= (unsigned int)ep
->buf
- REG_USB_REG_ADDR2
;
364 if(number
== USB_INTR_DMA_BULKOUT
)
367 REG_USB_REG_CNTL2
= 0;
369 __dcache_invalidate_all();
371 select_endpoint(endpoint
);
372 /* Read out last packet manually */
373 unsigned int lpack_size
= REG_USB_REG_OUTCOUNT
;
376 ep
->buf
+= ep
->length
- lpack_size
;
377 readFIFO(ep
, lpack_size
);
378 REG_USB_REG_OUTCSR
&= ~USB_OUTCSR_OUTPKTRDY
;
381 else if(number
== USB_INTR_DMA_BULKIN
&& size
% ep
->fifo_size
)
383 /* If the last packet is less than MAXP, set INPKTRDY manually */
384 REG_USB_REG_INCSR
|= USB_INCSR_INPKTRDY
;
387 usb_core_transfer_complete(endpoint
, EP_IS_IN(ep
) ? USB_DIR_IN
: USB_DIR_OUT
,
389 ep_transfer_completed(ep
);
392 static void setup_endpoint(struct usb_endpoint
*ep
)
396 select_endpoint(EP_NUMBER2(ep
));
403 if(ep
->type
== ep_bulk
)
405 if(REG_USB_REG_POWER
& USB_POWER_HSMODE
)
413 csr
= (USB_INCSR_FF
| USB_INCSR_CDT
);
414 csrh
= USB_INCSRH_MODE
;
417 csrh
|= (USB_INCSRH_DMAREQENAB
| USB_INCSRH_AUTOSET
| USB_INCSRH_DMAREQMODE
);
419 if(ep
->type
== ep_interrupt
)
420 csrh
|= USB_INCSRH_FRCDATATOG
;
422 REG_USB_REG_INMAXP
= ep
->fifo_size
;
423 REG_USB_REG_INCSR
= csr
;
424 REG_USB_REG_INCSRH
= csrh
;
425 REG_USB_REG_INTRINE
|= USB_INTR_EP(EP_NUMBER2(ep
));
429 csr
= (USB_OUTCSR_FF
| USB_OUTCSR_CDT
);
432 if(ep
->type
== ep_interrupt
)
433 csrh
|= USB_OUTCSRH_DNYT
;
436 csrh
|= (USB_OUTCSRH_DMAREQENAB
| USB_OUTCSRH_AUTOCLR
| USB_OUTCSRH_DMAREQMODE
);
438 REG_USB_REG_OUTMAXP
= ep
->fifo_size
;
439 REG_USB_REG_OUTCSR
= csr
;
440 REG_USB_REG_OUTCSRH
= csrh
;
441 REG_USB_REG_INTROUTE
|= USB_INTR_EP(EP_NUMBER2(ep
));
445 static void udc_reset(void)
447 /* From the datasheet:
449 When a reset condition is detected on the USB, the controller performs the following actions:
452 * Flushes all endpoint FIFOs.
453 * Clears all control/status registers.
454 * Enables all endpoint interrupts.
455 * Generates a Reset interrupt.
458 logf("%s()", __func__
);
462 /* Disable interrupts */
463 REG_USB_REG_INTRINE
= 0;
464 REG_USB_REG_INTROUTE
= 0;
465 REG_USB_REG_INTRUSBE
= 0;
468 REG_USB_REG_CNTL1
= 0;
469 REG_USB_REG_CNTL2
= 0;
471 /* High speed, softconnect and suspend/resume */
472 REG_USB_REG_POWER
= (USB_POWER_SOFTCONN
| USB_POWER_HSENAB
| USB_POWER_SUSPENDM
);
476 REG_USB_REG_CSR0
= (USB_CSR0_SVDOUTPKTRDY
| USB_CSR0_SVDSETUPEND
);
478 /* Reset other endpoints */
479 for(i
=2; i
<TOTAL_EP(); i
++)
480 setup_endpoint(&endpoints
[i
]);
482 /* Enable interrupts */
483 REG_USB_REG_INTRINE
|= USB_INTR_EP0
;
484 REG_USB_REG_INTRUSBE
|= USB_INTR_RESET
;
486 usb_core_bus_reset();
489 /* Interrupt handler */
492 /* Read interrupt registers */
493 unsigned char intrUSB
= REG_USB_REG_INTRUSB
& 0x07; /* Mask SOF */
494 unsigned short intrIn
= REG_USB_REG_INTRIN
;
495 unsigned short intrOut
= REG_USB_REG_INTROUT
;
496 unsigned char intrDMA
= REG_USB_REG_INTR
;
498 logf("%x %x %x %x", intrUSB
, intrIn
, intrOut
, intrDMA
);
500 /* EPIN & EPOUT are all handled in DMA */
501 if(intrIn
& USB_INTR_EP0
)
503 if(intrIn
& USB_INTR_INEP1
)
505 if(intrIn
& USB_INTR_INEP2
)
507 if(intrOut
& USB_INTR_OUTEP1
)
509 if(intrOut
& USB_INTR_OUTEP2
)
511 if(intrUSB
& USB_INTR_RESET
)
513 if(intrUSB
& USB_INTR_SUSPEND
)
517 if(intrUSB
& USB_INTR_RESUME
)
521 if(intrDMA
& USB_INTR_DMA_BULKIN
)
522 EPDMA_handler(USB_INTR_DMA_BULKIN
);
523 if(intrDMA
& USB_INTR_DMA_BULKOUT
)
524 EPDMA_handler(USB_INTR_DMA_BULKOUT
);
527 bool usb_drv_stalled(int endpoint
, bool in
)
531 logf("%s(%d, %s)", __func__
, endpoint
, in
?"IN":"OUT");
533 select_endpoint(endpoint
);
535 if(endpoint
== EP_CONTROL
)
536 return (REG_USB_REG_CSR0
& USB_CSR0_SENDSTALL
) != 0;
540 return (REG_USB_REG_INCSR
& USB_INCSR_SENDSTALL
) != 0;
542 return (REG_USB_REG_OUTCSR
& USB_OUTCSR_SENDSTALL
) != 0;
546 void usb_drv_stall(int endpoint
, bool stall
, bool in
)
550 logf("%s(%d,%s,%s)", __func__
, endpoint
, stall
?"Y":"N", in
?"IN":"OUT");
552 select_endpoint(endpoint
);
554 if(endpoint
== EP_CONTROL
)
557 REG_USB_REG_CSR0
|= USB_CSR0_SENDSTALL
;
559 REG_USB_REG_CSR0
&= ~USB_CSR0_SENDSTALL
;
566 REG_USB_REG_INCSR
|= USB_INCSR_SENDSTALL
;
568 REG_USB_REG_INCSR
= (REG_USB_REG_INCSR
& ~USB_INCSR_SENDSTALL
) | USB_INCSR_CDT
;
573 REG_USB_REG_OUTCSR
|= USB_OUTCSR_SENDSTALL
;
575 REG_USB_REG_OUTCSR
= (REG_USB_REG_OUTCSR
& ~USB_OUTCSR_SENDSTALL
) | USB_OUTCSR_CDT
;
580 bool usb_drv_connected(void)
582 return USB_DRV_CONNECTED();
587 return usb_drv_connected() ? USB_INSERTED
: USB_EXTRACTED
;
590 void usb_init_device(void)
596 system_enable_irq(IRQ_GPIO_UDC_DETE
);
598 system_enable_irq(IRQ_UDC
);
600 for(i
=0; i
<TOTAL_EP(); i
++)
601 wakeup_init(&endpoints
[i
].wakeup
);
605 static unsigned long last_tick
;
606 void USB_GPIO_IRQ(void)
608 /* Prevent enabled-disabled bouncing */
609 if(current_tick
- last_tick
> HZ
/16)
611 usb_status_event(usb_detect());
612 last_tick
= current_tick
;
617 void usb_enable(bool on
)
625 void usb_attach(void)
630 void usb_drv_init(void)
632 logf("%s()", __func__
);
634 /* Set this bit to allow the UDC entering low-power mode when
635 * there are no actions on the USB bus.
636 * UDC still works during this bit was set.
641 /* Enable the USB PHY */
642 REG_CPM_SCR
|= CPM_SCR_USBPHY_ENABLE
;
644 /* Dis- and reconnect from USB */
645 REG_USB_REG_POWER
&= ~USB_POWER_SOFTCONN
;
646 REG_USB_REG_POWER
|= USB_POWER_SOFTCONN
;
651 void usb_drv_exit(void)
653 logf("%s()", __func__
);
655 /* Disable interrupts */
656 REG_USB_REG_INTRINE
= 0;
657 REG_USB_REG_INTROUTE
= 0;
658 REG_USB_REG_INTRUSBE
= 0;
661 REG_USB_REG_CNTL1
= 0;
662 REG_USB_REG_CNTL2
= 0;
664 /* Disconnect from USB */
665 REG_USB_REG_POWER
&= ~USB_POWER_SOFTCONN
;
667 /* Disable the USB PHY */
668 REG_CPM_SCR
&= ~CPM_SCR_USBPHY_ENABLE
;
673 void usb_drv_set_address(int address
)
675 logf("%s(%d)", __func__
, address
);
677 REG_USB_REG_FADDR
= address
;
680 static void usb_drv_send_internal(struct usb_endpoint
* ep
, void* ptr
, int length
, bool blocking
)
682 if(ep
->type
== ep_control
&& ptr
== NULL
&& length
== 0)
683 return; /* ACK request, handled in the ISR */
685 int flags
= disable_irq_save();
694 if(ep
->type
== ep_control
)
702 //dma_cache_wback_inv((unsigned long)ptr, length);
703 __dcache_writeback_all();
704 REG_USB_REG_ADDR1
= PHYSADDR((unsigned long)ptr
);
705 REG_USB_REG_COUNT1
= length
;
706 REG_USB_REG_CNTL1
= (USB_CNTL_INTR_EN
| USB_CNTL_MODE_1
|
707 USB_CNTL_DIR_IN
| USB_CNTL_ENA
|
708 USB_CNTL_EP(EP_NUMBER2(ep
)) | USB_CNTL_BURST_16
);
711 EPIN_handler(EP_NUMBER2(ep
));
718 wakeup_wait(&ep
->wakeup
, TIMEOUT_BLOCK
);
723 int usb_drv_send_nonblocking(int endpoint
, void* ptr
, int length
)
725 logf("%s(%d, 0x%x, %d)", __func__
, endpoint
, (int)ptr
, length
);
727 usb_drv_send_internal(&endpoints
[(endpoint
& 0x7F)*2], ptr
, length
, false);
732 int usb_drv_send(int endpoint
, void* ptr
, int length
)
734 logf("%s(%d, 0x%x, %d)", __func__
, endpoint
, (int)ptr
, length
);
736 usb_drv_send_internal(&endpoints
[(endpoint
& 0x7F)*2], ptr
, length
, true);
741 int usb_drv_recv(int endpoint
, void* ptr
, int length
)
744 struct usb_endpoint
*ep
;
747 logf("%s(%d, 0x%x, %d)", __func__
, endpoint
, (int)ptr
, length
);
749 if(endpoint
== EP_CONTROL
)
750 return 0; /* all EP0 OUT transactions are handled within the ISR */
753 flags
= disable_irq_save();
754 ep
= &endpoints
[endpoint
*2+1];
762 //dma_cache_wback_inv((unsigned long)ptr, length);
763 __dcache_writeback_all();
764 REG_USB_REG_ADDR2
= PHYSADDR((unsigned long)ptr
);
765 REG_USB_REG_COUNT2
= length
;
766 REG_USB_REG_CNTL2
= (USB_CNTL_INTR_EN
| USB_CNTL_MODE_1
|
767 USB_CNTL_ENA
| USB_CNTL_EP(endpoint
) |
771 EPOUT_handler(endpoint
);
778 void usb_drv_set_test_mode(int mode
)
780 logf("%s(%d)", __func__
, mode
);
785 REG_USB_REG_TESTMODE
&= ~USB_TEST_ALL
;
788 REG_USB_REG_TESTMODE
|= USB_TEST_J
;
791 REG_USB_REG_TESTMODE
|= USB_TEST_K
;
794 REG_USB_REG_TESTMODE
|= USB_TEST_SE0NAK
;
797 REG_USB_REG_TESTMODE
|= USB_TEST_PACKET
;
802 int usb_drv_port_speed(void)
804 return (REG_USB_REG_POWER
& USB_POWER_HSMODE
) ? 1 : 0;
807 void usb_drv_cancel_all_transfers(void)
809 logf("%s()", __func__
);
811 unsigned int i
, flags
;
812 flags
= disable_irq_save();
814 for(i
=0; i
<TOTAL_EP(); i
++)
816 if(i
!= 1) /* ep0 out needs special handling */
817 endpoints
[i
].buf
= NULL
;
819 endpoints
[i
].sent
= 0;
820 endpoints
[i
].length
= 0;
822 select_endpoint(i
/2);
823 flushFIFO(&endpoints
[i
]);
828 void usb_drv_release_endpoint(int ep
)
831 logf("%s(%d, %s)", __func__
, (ep
& 0x7F), (ep
>> 7) ? "IN" : "OUT");
834 int usb_drv_request_endpoint(int type
, int dir
)
836 logf("%s(%d, %s)", __func__
, type
, (dir
== USB_DIR_IN
) ? "IN" : "OUT");
838 dir
&= USB_ENDPOINT_DIR_MASK
;
839 type
&= USB_ENDPOINT_XFERTYPE_MASK
;
841 /* There are only 3+2 endpoints, so hardcode this ... */
844 case USB_ENDPOINT_XFER_BULK
:
845 if(dir
== USB_DIR_IN
)
846 return (1 | USB_DIR_IN
);
848 return (1 | USB_DIR_OUT
);
850 case USB_ENDPOINT_XFER_INT
:
851 if(dir
== USB_DIR_IN
)
852 return (2 | USB_DIR_IN
);