2 * Copyright (c) 2009 Wind River Systems, Inc.
3 * Tom Rix <Tom.Rix@windriver.com>
5 * This file is a rewrite of the usb device part of
6 * repository git.omapzoom.org/repo/u-boot.git, branch master,
7 * file cpu/omap3/fastboot.c
9 * This is the unique part of its copyright :
11 * -------------------------------------------------------------------------
13 * (C) Copyright 2008 - 2009
14 * Windriver, <www.windriver.com>
15 * Tom Rix <Tom.Rix@windriver.com>
17 * -------------------------------------------------------------------------
19 * The details of connecting the device to the uboot usb device subsystem
20 * came from the old omap3 repository www.sakoman.net/u-boot-omap3.git,
21 * branch omap3-dev-usb, file drivers/usb/usbdcore_musb.c
23 * This is the unique part of its copyright :
25 * -------------------------------------------------------------------------
27 * (C) Copyright 2008 Texas Instruments Incorporated.
30 * u-boot OMAP1510 USB drivers (drivers/usbdcore_omap1510.c)
31 * twl4030 init based on linux (drivers/i2c/chips/twl4030_usb.c)
33 * Author: Diego Dompe (diego.dompe@ridgerun.com)
34 * Atin Malaviya (atin.malaviya@gmail.com)
36 * -------------------------------------------------------------------------
38 * SPDX-License-Identifier: GPL-2.0+
42 #include <usbdevice.h>
44 #include "../gadget/ep0.h"
45 #include "musb_core.h"
46 #if defined(CONFIG_USB_OMAP3)
48 #elif defined(CONFIG_USB_AM35X)
50 #elif defined(CONFIG_USB_DAVINCI)
54 /* Define MUSB_DEBUG for debugging */
55 /* #define MUSB_DEBUG */
56 #include "musb_debug.h"
58 #define MAX_ENDPOINT 15
60 #define GET_ENDPOINT(dev,ep) \
61 (((struct usb_device_instance *)(dev))->bus->endpoint_array + ep)
63 #define SET_EP0_STATE(s) \
65 if ((0 <= (s)) && (SET_ADDRESS >= (s))) { \
66 if ((s) != ep0_state) { \
67 if ((debug_setup) && (debug_level > 1)) \
68 serial_printf("INFO : Changing state " \
69 "from %s to %s in %s at " \
71 ep0_state_strings[ep0_state],\
72 ep0_state_strings[s], \
73 __PRETTY_FUNCTION__, \
78 if (debug_level > 0) \
79 serial_printf("Error at %s %d with setting " \
80 "state %d is invalid\n", \
81 __PRETTY_FUNCTION__, __LINE__, s); \
85 /* static implies these initialized to 0 or NULL */
86 static int debug_setup
;
87 static int debug_level
;
88 static struct musb_epinfo epinfo
[MAX_ENDPOINT
* 2];
89 static enum ep0_state_enum
{
95 static char *ep0_state_strings
[4] = {
102 static struct urb
*ep0_urb
;
103 struct usb_endpoint_instance
*ep0_endpoint
;
104 static struct usb_device_instance
*udc_device
;
108 static void musb_db_regs(void)
113 b
= readb(&musbr
->faddr
);
114 serial_printf("\tfaddr 0x%2.2x\n", b
);
116 b
= readb(&musbr
->power
);
119 w
= readw(&musbr
->ep
[0].ep0
.csr0
);
122 b
= readb(&musbr
->devctl
);
123 musb_print_devctl(b
);
125 b
= readb(&musbr
->ep
[0].ep0
.configdata
);
126 musb_print_config(b
);
128 w
= readw(&musbr
->frame
);
129 serial_printf("\tframe 0x%4.4x\n", w
);
131 b
= readb(&musbr
->index
);
132 serial_printf("\tindex 0x%2.2x\n", b
);
134 w
= readw(&musbr
->ep
[1].epN
.rxmaxp
);
135 musb_print_rxmaxp(w
);
137 w
= readw(&musbr
->ep
[1].epN
.rxcsr
);
140 w
= readw(&musbr
->ep
[1].epN
.txmaxp
);
141 musb_print_txmaxp(w
);
143 w
= readw(&musbr
->ep
[1].epN
.txcsr
);
147 #define musb_db_regs()
148 #endif /* DEBUG_MUSB */
150 static void musb_peri_softconnect(void)
155 power
= readb(&musbr
->power
);
156 power
&= ~MUSB_POWER_SOFTCONN
;
157 writeb(power
, &musbr
->power
);
159 /* Read intr to clear */
160 readb(&musbr
->intrusb
);
161 readw(&musbr
->intrrx
);
162 readw(&musbr
->intrtx
);
164 udelay(1000 * 1000); /* 1 sec */
167 power
= readb(&musbr
->power
);
168 power
|= MUSB_POWER_SOFTCONN
;
170 * The usb device interface is usb 1.1
171 * Disable 2.0 high speed by clearring the hsenable bit.
173 power
&= ~MUSB_POWER_HSENAB
;
174 writeb(power
, &musbr
->power
);
176 /* Check if device is in b-peripheral mode */
177 devctl
= readb(&musbr
->devctl
);
178 if (!(devctl
& MUSB_DEVCTL_BDEVICE
) ||
179 (devctl
& MUSB_DEVCTL_HM
)) {
180 serial_printf("ERROR : Unsupport USB mode\n");
181 serial_printf("Check that mini-B USB cable is attached "
185 if (debug_setup
&& (debug_level
> 1))
189 static void musb_peri_reset(void)
191 if ((debug_setup
) && (debug_level
> 1))
192 serial_printf("INFO : %s reset\n", __PRETTY_FUNCTION__
);
195 ep0_endpoint
->endpoint_address
= 0xff;
197 /* Sync sw and hw addresses */
198 writeb(udc_device
->address
, &musbr
->faddr
);
203 static void musb_peri_resume(void)
208 static void musb_peri_ep0_stall(void)
212 csr0
= readw(&musbr
->ep
[0].ep0
.csr0
);
213 csr0
|= MUSB_CSR0_P_SENDSTALL
;
214 writew(csr0
, &musbr
->ep
[0].ep0
.csr0
);
215 if ((debug_setup
) && (debug_level
> 1))
216 serial_printf("INFO : %s stall\n", __PRETTY_FUNCTION__
);
219 static void musb_peri_ep0_ack_req(void)
223 csr0
= readw(&musbr
->ep
[0].ep0
.csr0
);
224 csr0
|= MUSB_CSR0_P_SVDRXPKTRDY
;
225 writew(csr0
, &musbr
->ep
[0].ep0
.csr0
);
228 static void musb_ep0_tx_ready(void)
232 csr0
= readw(&musbr
->ep
[0].ep0
.csr0
);
233 csr0
|= MUSB_CSR0_TXPKTRDY
;
234 writew(csr0
, &musbr
->ep
[0].ep0
.csr0
);
237 static void musb_ep0_tx_ready_and_last(void)
241 csr0
= readw(&musbr
->ep
[0].ep0
.csr0
);
242 csr0
|= (MUSB_CSR0_TXPKTRDY
| MUSB_CSR0_P_DATAEND
);
243 writew(csr0
, &musbr
->ep
[0].ep0
.csr0
);
246 static void musb_peri_ep0_last(void)
250 csr0
= readw(&musbr
->ep
[0].ep0
.csr0
);
251 csr0
|= MUSB_CSR0_P_DATAEND
;
252 writew(csr0
, &musbr
->ep
[0].ep0
.csr0
);
255 static void musb_peri_ep0_set_address(void)
258 writeb(udc_device
->address
, &musbr
->faddr
);
261 faddr
= readb(&musbr
->faddr
);
262 if (udc_device
->address
== faddr
) {
264 usbd_device_event_irq(udc_device
, DEVICE_ADDRESS_ASSIGNED
, 0);
265 if ((debug_setup
) && (debug_level
> 1))
266 serial_printf("INFO : %s Address set to %d\n",
267 __PRETTY_FUNCTION__
, udc_device
->address
);
270 serial_printf("ERROR : %s Address missmatch "
273 udc_device
->address
, faddr
);
277 static void musb_peri_rx_ack(unsigned int ep
)
281 peri_rxcsr
= readw(&musbr
->ep
[ep
].epN
.rxcsr
);
282 peri_rxcsr
&= ~MUSB_RXCSR_RXPKTRDY
;
283 writew(peri_rxcsr
, &musbr
->ep
[ep
].epN
.rxcsr
);
286 static void musb_peri_tx_ready(unsigned int ep
)
290 peri_txcsr
= readw(&musbr
->ep
[ep
].epN
.txcsr
);
291 peri_txcsr
|= MUSB_TXCSR_TXPKTRDY
;
292 writew(peri_txcsr
, &musbr
->ep
[ep
].epN
.txcsr
);
295 static void musb_peri_ep0_zero_data_request(int err
)
297 musb_peri_ep0_ack_req();
300 musb_peri_ep0_stall();
304 musb_peri_ep0_last();
307 switch (ep0_urb
->device_request
.bRequest
) {
308 case USB_REQ_SET_ADDRESS
:
309 if ((debug_setup
) && (debug_level
> 1))
310 serial_printf("INFO : %s received set "
311 "address\n", __PRETTY_FUNCTION__
);
314 case USB_REQ_SET_CONFIGURATION
:
315 if ((debug_setup
) && (debug_level
> 1))
316 serial_printf("INFO : %s Configured\n",
317 __PRETTY_FUNCTION__
);
318 usbd_device_event_irq(udc_device
, DEVICE_CONFIGURED
, 0);
323 if (USB_REQ_SET_ADDRESS
== ep0_urb
->device_request
.bRequest
) {
324 SET_EP0_STATE(SET_ADDRESS
);
331 static void musb_peri_ep0_rx_data_request(void)
334 * This is the completion of the data OUT / RX
336 * Host is sending data to ep0 that is not
337 * part of setup. This comes from the cdc_recv_setup
338 * op that is device specific.
341 musb_peri_ep0_ack_req();
343 ep0_endpoint
->rcv_urb
= ep0_urb
;
344 ep0_urb
->actual_length
= 0;
348 static void musb_peri_ep0_tx_data_request(int err
)
351 musb_peri_ep0_stall();
354 musb_peri_ep0_ack_req();
356 ep0_endpoint
->tx_urb
= ep0_urb
;
357 ep0_endpoint
->sent
= 0;
362 static void musb_peri_ep0_idle(void)
370 * A lot of confusion can be caused if the address
371 * in software, udc layer, does not agree with the
372 * hardware. Since the setting of the hardware address
373 * must be set after the set address request, the
374 * usb state machine is out of sync for a few frame.
375 * It is a good idea to run this check when changes
376 * are made to the state machine.
378 if ((debug_level
> 0) &&
379 (ep0_state
!= SET_ADDRESS
)) {
382 faddr
= readb(&musbr
->faddr
);
383 if (udc_device
->address
!= faddr
) {
384 serial_printf("ERROR : %s addresses do not"
385 "match sw %d vs hw %d\n",
387 udc_device
->address
, faddr
);
393 csr0
= readw(&musbr
->ep
[0].ep0
.csr0
);
395 if (!(MUSB_CSR0_RXPKTRDY
& csr0
))
398 count0
= readw(&musbr
->ep
[0].ep0
.count0
);
403 if ((debug_setup
) && (debug_level
> 1))
404 serial_printf("WARN : %s SETUP incorrect size %d\n",
405 __PRETTY_FUNCTION__
, count0
);
406 musb_peri_ep0_stall();
410 read_fifo(0, count0
, &ep0_urb
->device_request
);
413 print_usb_device_request(&ep0_urb
->device_request
);
415 if (ep0_urb
->device_request
.wLength
== 0) {
416 err
= ep0_recv_setup(ep0_urb
);
418 /* Zero data request */
419 musb_peri_ep0_zero_data_request(err
);
421 /* Is data coming or going ? */
422 u8 reqType
= ep0_urb
->device_request
.bmRequestType
;
424 if (USB_REQ_DEVICE2HOST
== (reqType
& USB_REQ_DIRECTION_MASK
)) {
425 err
= ep0_recv_setup(ep0_urb
);
427 musb_peri_ep0_tx_data_request(err
);
432 * The RX routine will call ep0_recv_setup
433 * when the data packet has arrived.
435 musb_peri_ep0_rx_data_request();
443 static void musb_peri_ep0_rx(void)
446 * This is the completion of the data OUT / RX
448 * Host is sending data to ep0 that is not
449 * part of setup. This comes from the cdc_recv_setup
450 * op that is device specific.
452 * Pass the data back to driver ep0_recv_setup which
453 * should give the cdc_recv_setup the chance to handle
459 if (debug_level
> 3) {
460 if (0 != ep0_urb
->actual_length
) {
461 serial_printf("%s finished ? %d of %d\n",
463 ep0_urb
->actual_length
,
464 ep0_urb
->device_request
.wLength
);
468 if (ep0_urb
->device_request
.wLength
== ep0_urb
->actual_length
) {
469 musb_peri_ep0_last();
471 ep0_recv_setup(ep0_urb
);
475 csr0
= readw(&musbr
->ep
[0].ep0
.csr0
);
476 if (!(MUSB_CSR0_RXPKTRDY
& csr0
))
479 count0
= readw(&musbr
->ep
[0].ep0
.count0
);
482 struct usb_endpoint_instance
*endpoint
;
486 endpoint
= ep0_endpoint
;
487 if (endpoint
&& endpoint
->rcv_urb
) {
488 struct urb
*urb
= endpoint
->rcv_urb
;
489 unsigned int remaining_space
= urb
->buffer_length
-
492 if (remaining_space
) {
493 int urb_bad
= 0; /* urb is good */
495 if (count0
> remaining_space
)
496 length
= remaining_space
;
500 data
= (u8
*) urb
->buffer_data
;
501 data
+= urb
->actual_length
;
503 /* The common musb fifo reader */
504 read_fifo(0, length
, data
);
506 musb_peri_ep0_ack_req();
509 * urb's actual_length is updated in
512 usbd_rcv_complete(endpoint
, length
, urb_bad
);
516 serial_printf("ERROR : %s no space in "
518 __PRETTY_FUNCTION__
);
522 serial_printf("ERROR : %s problem with "
524 __PRETTY_FUNCTION__
);
528 serial_printf("ERROR : %s with nothing to do\n",
529 __PRETTY_FUNCTION__
);
533 static void musb_peri_ep0_tx(void)
536 int transfer_size
= 0;
539 csr0
= readw(&musbr
->ep
[0].ep0
.csr0
);
541 /* Check for pending tx */
542 if (csr0
& MUSB_CSR0_TXPKTRDY
)
545 /* Check if this is the last packet sent */
546 if (ep0_endpoint
->sent
>= ep0_urb
->actual_length
) {
551 transfer_size
= ep0_urb
->actual_length
- ep0_endpoint
->sent
;
552 /* Is the transfer size negative ? */
553 if (transfer_size
<= 0) {
555 serial_printf("ERROR : %s problem with the"
556 " transfer size %d\n",
563 /* Truncate large transfers to the fifo size */
564 if (transfer_size
> ep0_endpoint
->tx_packetSize
)
565 transfer_size
= ep0_endpoint
->tx_packetSize
;
567 write_fifo(0, transfer_size
, &ep0_urb
->buffer
[ep0_endpoint
->sent
]);
568 ep0_endpoint
->sent
+= transfer_size
;
570 /* Done or more to send ? */
571 if (ep0_endpoint
->sent
>= ep0_urb
->actual_length
)
572 musb_ep0_tx_ready_and_last();
578 for (p
= 0; p
< pm
; p
++) {
579 csr0
= readw(&musbr
->ep
[0].ep0
.csr0
);
580 if (!(csr0
& MUSB_CSR0_TXPKTRDY
))
583 /* Double the delay. */
587 if ((ep0_endpoint
->sent
>= ep0_urb
->actual_length
) && (p
< pm
))
594 static void musb_peri_ep0(void)
598 if (SET_ADDRESS
== ep0_state
)
601 csr0
= readw(&musbr
->ep
[0].ep0
.csr0
);
603 /* Error conditions */
604 if (MUSB_CSR0_P_SENTSTALL
& csr0
) {
605 csr0
&= ~MUSB_CSR0_P_SENTSTALL
;
606 writew(csr0
, &musbr
->ep
[0].ep0
.csr0
);
609 if (MUSB_CSR0_P_SETUPEND
& csr0
) {
610 csr0
|= MUSB_CSR0_P_SVDSETUPEND
;
611 writew(csr0
, &musbr
->ep
[0].ep0
.csr0
);
613 if ((debug_setup
) && (debug_level
> 1))
614 serial_printf("WARN: %s SETUPEND\n",
615 __PRETTY_FUNCTION__
);
619 if (IDLE
== ep0_state
)
620 musb_peri_ep0_idle();
629 static void musb_peri_rx_ep(unsigned int ep
)
632 u8 peri_rxcsr
= readw(&musbr
->ep
[ep
].epN
.rxcsr
);
634 if (!(peri_rxcsr
& MUSB_RXCSR_RXPKTRDY
)) {
636 serial_printf("ERROR : %s %d without MUSB_RXCSR_RXPKTRDY set\n",
637 __PRETTY_FUNCTION__
, ep
);
641 peri_rxcount
= readw(&musbr
->ep
[ep
].epN
.rxcount
);
643 struct usb_endpoint_instance
*endpoint
;
647 endpoint
= GET_ENDPOINT(udc_device
, ep
);
648 if (endpoint
&& endpoint
->rcv_urb
) {
649 struct urb
*urb
= endpoint
->rcv_urb
;
650 unsigned int remaining_space
= urb
->buffer_length
-
653 if (remaining_space
) {
654 int urb_bad
= 0; /* urb is good */
656 if (peri_rxcount
> remaining_space
)
657 length
= remaining_space
;
659 length
= peri_rxcount
;
661 data
= (u8
*) urb
->buffer_data
;
662 data
+= urb
->actual_length
;
664 /* The common musb fifo reader */
665 read_fifo(ep
, length
, data
);
667 musb_peri_rx_ack(ep
);
670 * urb's actual_length is updated in
673 usbd_rcv_complete(endpoint
, length
, urb_bad
);
677 serial_printf("ERROR : %s %d no space "
679 __PRETTY_FUNCTION__
, ep
);
683 serial_printf("ERROR : %s %d problem with "
685 __PRETTY_FUNCTION__
, ep
);
690 serial_printf("ERROR : %s %d with nothing to do\n",
691 __PRETTY_FUNCTION__
, ep
);
695 static void musb_peri_rx(u16 intr
)
703 for (ep
= 1; ep
< 16; ep
++) {
704 if ((1 << ep
) & intr
)
709 static void musb_peri_tx(u16 intr
)
716 * Use this in the future when handling epN tx
720 * for (ep = 1; ep < 16; ep++) {
721 * if ((1 << ep) & intr) {
722 * / * handle tx for this endpoint * /
730 /* This is a high freq called function */
734 intrusb
= readb(&musbr
->intrusb
);
737 * See drivers/usb/gadget/mpc8xx_udc.c for
738 * state diagram going from detached through
741 if (MUSB_INTR_RESUME
& intrusb
) {
742 usbd_device_event_irq(udc_device
,
743 DEVICE_BUS_ACTIVITY
, 0);
749 if (MUSB_INTR_RESET
& intrusb
) {
750 usbd_device_event_irq(udc_device
, DEVICE_RESET
, 0);
754 if (MUSB_INTR_DISCONNECT
& intrusb
) {
755 /* cable unplugged from hub/host */
756 usbd_device_event_irq(udc_device
, DEVICE_RESET
, 0);
758 usbd_device_event_irq(udc_device
, DEVICE_HUB_RESET
, 0);
761 if (MUSB_INTR_SOF
& intrusb
) {
762 usbd_device_event_irq(udc_device
,
763 DEVICE_BUS_ACTIVITY
, 0);
767 if (MUSB_INTR_SUSPEND
& intrusb
) {
768 usbd_device_event_irq(udc_device
,
769 DEVICE_BUS_INACTIVE
, 0);
772 if (ep0_state
!= SET_ADDRESS
) {
775 intrrx
= readw(&musbr
->intrrx
);
776 intrtx
= readw(&musbr
->intrtx
);
779 musb_peri_rx(intrrx
);
782 musb_peri_tx(intrtx
);
784 if (MUSB_INTR_SOF
& intrusb
) {
786 faddr
= readb(&musbr
->faddr
);
788 * Setting of the address can fail.
789 * Normally it succeeds the second time.
791 if (udc_device
->address
!= faddr
)
792 musb_peri_ep0_set_address();
798 void udc_set_nak(int ep_num
)
803 void udc_unset_nak(int ep_num
)
808 int udc_endpoint_write(struct usb_endpoint_instance
*endpoint
)
812 /* Transmit only if the hardware is available */
813 if (endpoint
->tx_urb
&& endpoint
->state
== 0) {
814 unsigned int ep
= endpoint
->endpoint_address
&
815 USB_ENDPOINT_NUMBER_MASK
;
817 u16 peri_txcsr
= readw(&musbr
->ep
[ep
].epN
.txcsr
);
819 /* Error conditions */
820 if (peri_txcsr
& MUSB_TXCSR_P_UNDERRUN
) {
821 peri_txcsr
&= ~MUSB_TXCSR_P_UNDERRUN
;
822 writew(peri_txcsr
, &musbr
->ep
[ep
].epN
.txcsr
);
826 musb_print_txcsr(peri_txcsr
);
828 /* Check if a packet is waiting to be sent */
829 if (!(peri_txcsr
& MUSB_TXCSR_TXPKTRDY
)) {
832 struct urb
*urb
= endpoint
->tx_urb
;
833 unsigned int remaining_packet
= urb
->actual_length
-
836 if (endpoint
->tx_packetSize
< remaining_packet
)
837 length
= endpoint
->tx_packetSize
;
839 length
= remaining_packet
;
841 data
= (u8
*) urb
->buffer
;
842 data
+= endpoint
->sent
;
844 /* common musb fifo function */
845 write_fifo(ep
, length
, data
);
847 musb_peri_tx_ready(ep
);
849 endpoint
->last
= length
;
850 /* usbd_tx_complete will take care of updating 'sent' */
851 usbd_tx_complete(endpoint
);
855 serial_printf("ERROR : %s Problem with urb %p "
858 endpoint
->tx_urb
, endpoint
->state
);
864 void udc_setup_ep(struct usb_device_instance
*device
, unsigned int id
,
865 struct usb_endpoint_instance
*endpoint
)
869 ep0_endpoint
= endpoint
;
870 ep0_endpoint
->endpoint_address
= 0xff;
871 ep0_urb
= usbd_alloc_urb(device
, endpoint
);
872 } else if (MAX_ENDPOINT
>= id
) {
875 /* Check the direction */
876 ep_addr
= endpoint
->endpoint_address
;
877 if (USB_DIR_IN
== (ep_addr
& USB_ENDPOINT_DIR_MASK
)) {
879 epinfo
[(id
* 2) + 1].epsize
= endpoint
->tx_packetSize
;
882 epinfo
[id
* 2].epsize
= endpoint
->rcv_packetSize
;
885 musb_configure_ep(&epinfo
[0], ARRAY_SIZE(epinfo
));
888 serial_printf("ERROR : %s endpoint request %d "
889 "exceeds maximum %d\n",
890 __PRETTY_FUNCTION__
, id
, MAX_ENDPOINT
);
894 void udc_connect(void)
899 void udc_disconnect(void)
904 void udc_enable(struct usb_device_instance
*device
)
906 /* Save the device structure pointer */
912 void udc_disable(void)
917 void udc_startup_events(struct usb_device_instance
*device
)
919 /* The DEVICE_INIT event puts the USB device in the state STATE_INIT. */
920 usbd_device_event_irq(device
, DEVICE_INIT
, 0);
923 * The DEVICE_CREATE event puts the USB device in the state
926 usbd_device_event_irq(device
, DEVICE_CREATE
, 0);
928 /* Resets the address to 0 */
929 usbd_device_event_irq(device
, DEVICE_RESET
, 0);
939 ret
= musb_platform_init();
943 /* Configure all the endpoint FIFO's and start usb controller */
944 musbr
= musb_cfg
.regs
;
946 /* Initialize the endpoints */
947 for (ep_loop
= 0; ep_loop
< MAX_ENDPOINT
* 2; ep_loop
++) {
948 epinfo
[ep_loop
].epnum
= (ep_loop
/ 2) + 1;
949 epinfo
[ep_loop
].epdir
= ep_loop
% 2; /* OUT, IN */
950 epinfo
[ep_loop
].epsize
= 0;
953 musb_peri_softconnect();