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 * This program is free software; you can redistribute it and/or
39 * modify it under the terms of the GNU General Public License as
40 * published by the Free Software Foundation; either version 2 of
41 * the License, or (at your option) any later version.
43 * This program is distributed in the hope that it will be useful,
44 * but WITHOUT ANY WARRANTY; without even the implied warranty of
45 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
46 * GNU General Public License for more details.
48 * You should have received a copy of the GNU General Public License
49 * along with this program; if not, write to the Free Software
50 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
55 #include <usb/musb_udc.h>
56 #include "../gadget/ep0.h"
57 #include "musb_core.h"
58 #if defined(CONFIG_USB_OMAP3)
60 #elif defined(CONFIG_USB_AM35X)
62 #elif defined(CONFIG_USB_DAVINCI)
66 /* Define MUSB_DEBUG for debugging */
67 /* #define MUSB_DEBUG */
68 #include "musb_debug.h"
70 #define MAX_ENDPOINT 15
72 #define GET_ENDPOINT(dev,ep) \
73 (((struct usb_device_instance *)(dev))->bus->endpoint_array + ep)
75 #define SET_EP0_STATE(s) \
77 if ((0 <= (s)) && (SET_ADDRESS >= (s))) { \
78 if ((s) != ep0_state) { \
79 if ((debug_setup) && (debug_level > 1)) \
80 serial_printf("INFO : Changing state " \
81 "from %s to %s in %s at " \
83 ep0_state_strings[ep0_state],\
84 ep0_state_strings[s], \
85 __PRETTY_FUNCTION__, \
90 if (debug_level > 0) \
91 serial_printf("Error at %s %d with setting " \
92 "state %d is invalid\n", \
93 __PRETTY_FUNCTION__, __LINE__, s); \
97 /* static implies these initialized to 0 or NULL */
98 static int debug_setup;
99 static int debug_level;
100 static struct musb_epinfo epinfo[MAX_ENDPOINT * 2];
101 static enum ep0_state_enum {
107 static char *ep0_state_strings[4] = {
114 static struct urb *ep0_urb;
115 struct usb_endpoint_instance *ep0_endpoint;
116 static struct usb_device_instance *udc_device;
120 static void musb_db_regs(void)
125 b = readb(&musbr->faddr);
126 serial_printf("\tfaddr 0x%2.2x\n", b);
128 b = readb(&musbr->power);
131 w = readw(&musbr->ep[0].ep0.csr0);
134 b = readb(&musbr->devctl);
135 musb_print_devctl(b);
137 b = readb(&musbr->ep[0].ep0.configdata);
138 musb_print_config(b);
140 w = readw(&musbr->frame);
141 serial_printf("\tframe 0x%4.4x\n", w);
143 b = readb(&musbr->index);
144 serial_printf("\tindex 0x%2.2x\n", b);
146 w = readw(&musbr->ep[1].epN.rxmaxp);
147 musb_print_rxmaxp(w);
149 w = readw(&musbr->ep[1].epN.rxcsr);
152 w = readw(&musbr->ep[1].epN.txmaxp);
153 musb_print_txmaxp(w);
155 w = readw(&musbr->ep[1].epN.txcsr);
159 #define musb_db_regs()
160 #endif /* DEBUG_MUSB */
162 static void musb_peri_softconnect(void)
167 power = readb(&musbr->power);
168 power &= ~MUSB_POWER_SOFTCONN;
169 writeb(power, &musbr->power);
171 /* Read intr to clear */
172 readb(&musbr->intrusb);
173 readw(&musbr->intrrx);
174 readw(&musbr->intrtx);
176 udelay(1000 * 1000); /* 1 sec */
179 power = readb(&musbr->power);
180 power |= MUSB_POWER_SOFTCONN;
182 * The usb device interface is usb 1.1
183 * Disable 2.0 high speed by clearring the hsenable bit.
185 power &= ~MUSB_POWER_HSENAB;
186 writeb(power, &musbr->power);
188 /* Check if device is in b-peripheral mode */
189 devctl = readb(&musbr->devctl);
190 if (!(devctl & MUSB_DEVCTL_BDEVICE) ||
191 (devctl & MUSB_DEVCTL_HM)) {
192 serial_printf("ERROR : Unsupport USB mode\n");
193 serial_printf("Check that mini-B USB cable is attached "
197 if (debug_setup && (debug_level > 1))
201 static void musb_peri_reset(void)
203 if ((debug_setup) && (debug_level > 1))
204 serial_printf("INFO : %s reset\n", __PRETTY_FUNCTION__);
207 ep0_endpoint->endpoint_address = 0xff;
209 /* Sync sw and hw addresses */
210 writeb(udc_device->address, &musbr->faddr);
215 static void musb_peri_resume(void)
220 static void musb_peri_ep0_stall(void)
224 csr0 = readw(&musbr->ep[0].ep0.csr0);
225 csr0 |= MUSB_CSR0_P_SENDSTALL;
226 writew(csr0, &musbr->ep[0].ep0.csr0);
227 if ((debug_setup) && (debug_level > 1))
228 serial_printf("INFO : %s stall\n", __PRETTY_FUNCTION__);
231 static void musb_peri_ep0_ack_req(void)
235 csr0 = readw(&musbr->ep[0].ep0.csr0);
236 csr0 |= MUSB_CSR0_P_SVDRXPKTRDY;
237 writew(csr0, &musbr->ep[0].ep0.csr0);
240 static void musb_ep0_tx_ready(void)
244 csr0 = readw(&musbr->ep[0].ep0.csr0);
245 csr0 |= MUSB_CSR0_TXPKTRDY;
246 writew(csr0, &musbr->ep[0].ep0.csr0);
249 static void musb_ep0_tx_ready_and_last(void)
253 csr0 = readw(&musbr->ep[0].ep0.csr0);
254 csr0 |= (MUSB_CSR0_TXPKTRDY | MUSB_CSR0_P_DATAEND);
255 writew(csr0, &musbr->ep[0].ep0.csr0);
258 static void musb_peri_ep0_last(void)
262 csr0 = readw(&musbr->ep[0].ep0.csr0);
263 csr0 |= MUSB_CSR0_P_DATAEND;
264 writew(csr0, &musbr->ep[0].ep0.csr0);
267 static void musb_peri_ep0_set_address(void)
270 writeb(udc_device->address, &musbr->faddr);
273 faddr = readb(&musbr->faddr);
274 if (udc_device->address == faddr) {
276 usbd_device_event_irq(udc_device, DEVICE_ADDRESS_ASSIGNED, 0);
277 if ((debug_setup) && (debug_level > 1))
278 serial_printf("INFO : %s Address set to %d\n",
279 __PRETTY_FUNCTION__, udc_device->address);
282 serial_printf("ERROR : %s Address missmatch "
285 udc_device->address, faddr);
289 static void musb_peri_rx_ack(unsigned int ep)
293 peri_rxcsr = readw(&musbr->ep[ep].epN.rxcsr);
294 peri_rxcsr &= ~MUSB_RXCSR_RXPKTRDY;
295 writew(peri_rxcsr, &musbr->ep[ep].epN.rxcsr);
298 static void musb_peri_tx_ready(unsigned int ep)
302 peri_txcsr = readw(&musbr->ep[ep].epN.txcsr);
303 peri_txcsr |= MUSB_TXCSR_TXPKTRDY;
304 writew(peri_txcsr, &musbr->ep[ep].epN.txcsr);
307 static void musb_peri_ep0_zero_data_request(int err)
309 musb_peri_ep0_ack_req();
312 musb_peri_ep0_stall();
316 musb_peri_ep0_last();
319 switch (ep0_urb->device_request.bRequest) {
320 case USB_REQ_SET_ADDRESS:
321 if ((debug_setup) && (debug_level > 1))
322 serial_printf("INFO : %s received set "
323 "address\n", __PRETTY_FUNCTION__);
326 case USB_REQ_SET_CONFIGURATION:
327 if ((debug_setup) && (debug_level > 1))
328 serial_printf("INFO : %s Configured\n",
329 __PRETTY_FUNCTION__);
330 usbd_device_event_irq(udc_device, DEVICE_CONFIGURED, 0);
335 if (USB_REQ_SET_ADDRESS == ep0_urb->device_request.bRequest) {
336 SET_EP0_STATE(SET_ADDRESS);
343 static void musb_peri_ep0_rx_data_request(void)
346 * This is the completion of the data OUT / RX
348 * Host is sending data to ep0 that is not
349 * part of setup. This comes from the cdc_recv_setup
350 * op that is device specific.
353 musb_peri_ep0_ack_req();
355 ep0_endpoint->rcv_urb = ep0_urb;
356 ep0_urb->actual_length = 0;
360 static void musb_peri_ep0_tx_data_request(int err)
363 musb_peri_ep0_stall();
366 musb_peri_ep0_ack_req();
368 ep0_endpoint->tx_urb = ep0_urb;
369 ep0_endpoint->sent = 0;
374 static void musb_peri_ep0_idle(void)
382 * A lot of confusion can be caused if the address
383 * in software, udc layer, does not agree with the
384 * hardware. Since the setting of the hardware address
385 * must be set after the set address request, the
386 * usb state machine is out of sync for a few frame.
387 * It is a good idea to run this check when changes
388 * are made to the state machine.
390 if ((debug_level > 0) &&
391 (ep0_state != SET_ADDRESS)) {
394 faddr = readb(&musbr->faddr);
395 if (udc_device->address != faddr) {
396 serial_printf("ERROR : %s addresses do not"
397 "match sw %d vs hw %d\n",
399 udc_device->address, faddr);
405 csr0 = readw(&musbr->ep[0].ep0.csr0);
407 if (!(MUSB_CSR0_RXPKTRDY & csr0))
410 count0 = readw(&musbr->ep[0].ep0.count0);
415 if ((debug_setup) && (debug_level > 1))
416 serial_printf("WARN : %s SETUP incorrect size %d\n",
417 __PRETTY_FUNCTION__, count0);
418 musb_peri_ep0_stall();
422 read_fifo(0, count0, &ep0_urb->device_request);
425 print_usb_device_request(&ep0_urb->device_request);
427 if (ep0_urb->device_request.wLength == 0) {
428 err = ep0_recv_setup(ep0_urb);
430 /* Zero data request */
431 musb_peri_ep0_zero_data_request(err);
433 /* Is data coming or going ? */
434 u8 reqType = ep0_urb->device_request.bmRequestType;
436 if (USB_REQ_DEVICE2HOST == (reqType & USB_REQ_DIRECTION_MASK)) {
437 err = ep0_recv_setup(ep0_urb);
439 musb_peri_ep0_tx_data_request(err);
444 * The RX routine will call ep0_recv_setup
445 * when the data packet has arrived.
447 musb_peri_ep0_rx_data_request();
455 static void musb_peri_ep0_rx(void)
458 * This is the completion of the data OUT / RX
460 * Host is sending data to ep0 that is not
461 * part of setup. This comes from the cdc_recv_setup
462 * op that is device specific.
464 * Pass the data back to driver ep0_recv_setup which
465 * should give the cdc_recv_setup the chance to handle
471 if (debug_level > 3) {
472 if (0 != ep0_urb->actual_length) {
473 serial_printf("%s finished ? %d of %d\n",
475 ep0_urb->actual_length,
476 ep0_urb->device_request.wLength);
480 if (ep0_urb->device_request.wLength == ep0_urb->actual_length) {
481 musb_peri_ep0_last();
483 ep0_recv_setup(ep0_urb);
487 csr0 = readw(&musbr->ep[0].ep0.csr0);
488 if (!(MUSB_CSR0_RXPKTRDY & csr0))
491 count0 = readw(&musbr->ep[0].ep0.count0);
494 struct usb_endpoint_instance *endpoint;
498 endpoint = ep0_endpoint;
499 if (endpoint && endpoint->rcv_urb) {
500 struct urb *urb = endpoint->rcv_urb;
501 unsigned int remaining_space = urb->buffer_length -
504 if (remaining_space) {
505 int urb_bad = 0; /* urb is good */
507 if (count0 > remaining_space)
508 length = remaining_space;
512 data = (u8 *) urb->buffer_data;
513 data += urb->actual_length;
515 /* The common musb fifo reader */
516 read_fifo(0, length, data);
518 musb_peri_ep0_ack_req();
521 * urb's actual_length is updated in
524 usbd_rcv_complete(endpoint, length, urb_bad);
528 serial_printf("ERROR : %s no space in "
530 __PRETTY_FUNCTION__);
534 serial_printf("ERROR : %s problem with "
536 __PRETTY_FUNCTION__);
540 serial_printf("ERROR : %s with nothing to do\n",
541 __PRETTY_FUNCTION__);
545 static void musb_peri_ep0_tx(void)
548 int transfer_size = 0;
551 csr0 = readw(&musbr->ep[0].ep0.csr0);
553 /* Check for pending tx */
554 if (csr0 & MUSB_CSR0_TXPKTRDY)
557 /* Check if this is the last packet sent */
558 if (ep0_endpoint->sent >= ep0_urb->actual_length) {
563 transfer_size = ep0_urb->actual_length - ep0_endpoint->sent;
564 /* Is the transfer size negative ? */
565 if (transfer_size <= 0) {
567 serial_printf("ERROR : %s problem with the"
568 " transfer size %d\n",
575 /* Truncate large transfers to the fifo size */
576 if (transfer_size > ep0_endpoint->tx_packetSize)
577 transfer_size = ep0_endpoint->tx_packetSize;
579 write_fifo(0, transfer_size, &ep0_urb->buffer[ep0_endpoint->sent]);
580 ep0_endpoint->sent += transfer_size;
582 /* Done or more to send ? */
583 if (ep0_endpoint->sent >= ep0_urb->actual_length)
584 musb_ep0_tx_ready_and_last();
590 for (p = 0; p < pm; p++) {
591 csr0 = readw(&musbr->ep[0].ep0.csr0);
592 if (!(csr0 & MUSB_CSR0_TXPKTRDY))
595 /* Double the delay. */
599 if ((ep0_endpoint->sent >= ep0_urb->actual_length) && (p < pm))
606 static void musb_peri_ep0(void)
610 if (SET_ADDRESS == ep0_state)
613 csr0 = readw(&musbr->ep[0].ep0.csr0);
615 /* Error conditions */
616 if (MUSB_CSR0_P_SENTSTALL & csr0) {
617 csr0 &= ~MUSB_CSR0_P_SENTSTALL;
618 writew(csr0, &musbr->ep[0].ep0.csr0);
621 if (MUSB_CSR0_P_SETUPEND & csr0) {
622 csr0 |= MUSB_CSR0_P_SVDSETUPEND;
623 writew(csr0, &musbr->ep[0].ep0.csr0);
625 if ((debug_setup) && (debug_level > 1))
626 serial_printf("WARN: %s SETUPEND\n",
627 __PRETTY_FUNCTION__);
631 if (IDLE == ep0_state)
632 musb_peri_ep0_idle();
641 static void musb_peri_rx_ep(unsigned int ep)
643 u16 peri_rxcount = readw(&musbr->ep[ep].epN.rxcount);
646 struct usb_endpoint_instance *endpoint;
650 endpoint = GET_ENDPOINT(udc_device, ep);
651 if (endpoint && endpoint->rcv_urb) {
652 struct urb *urb = endpoint->rcv_urb;
653 unsigned int remaining_space = urb->buffer_length -
656 if (remaining_space) {
657 int urb_bad = 0; /* urb is good */
659 if (peri_rxcount > remaining_space)
660 length = remaining_space;
662 length = peri_rxcount;
664 data = (u8 *) urb->buffer_data;
665 data += urb->actual_length;
667 /* The common musb fifo reader */
668 read_fifo(ep, length, data);
670 musb_peri_rx_ack(ep);
673 * urb's actual_length is updated in
676 usbd_rcv_complete(endpoint, length, urb_bad);
680 serial_printf("ERROR : %s %d no space "
682 __PRETTY_FUNCTION__, ep);
686 serial_printf("ERROR : %s %d problem with "
688 __PRETTY_FUNCTION__, ep);
693 serial_printf("ERROR : %s %d with nothing to do\n",
694 __PRETTY_FUNCTION__, ep);
698 static void musb_peri_rx(u16 intr)
706 for (ep = 1; ep < 16; ep++) {
707 if ((1 << ep) & intr)
712 static void musb_peri_tx(u16 intr)
719 * Use this in the future when handling epN tx
723 * for (ep = 1; ep < 16; ep++) {
724 * if ((1 << ep) & intr) {
725 * / * handle tx for this endpoint * /
733 /* This is a high freq called function */
737 intrusb = readb(&musbr->intrusb);
740 * See drivers/usb/gadget/mpc8xx_udc.c for
741 * state diagram going from detached through
744 if (MUSB_INTR_RESUME & intrusb) {
745 usbd_device_event_irq(udc_device,
746 DEVICE_BUS_ACTIVITY, 0);
752 if (MUSB_INTR_RESET & intrusb) {
753 usbd_device_event_irq(udc_device, DEVICE_RESET, 0);
757 if (MUSB_INTR_DISCONNECT & intrusb) {
758 /* cable unplugged from hub/host */
759 usbd_device_event_irq(udc_device, DEVICE_RESET, 0);
761 usbd_device_event_irq(udc_device, DEVICE_HUB_RESET, 0);
764 if (MUSB_INTR_SOF & intrusb) {
765 usbd_device_event_irq(udc_device,
766 DEVICE_BUS_ACTIVITY, 0);
770 if (MUSB_INTR_SUSPEND & intrusb) {
771 usbd_device_event_irq(udc_device,
772 DEVICE_BUS_INACTIVE, 0);
775 if (ep0_state != SET_ADDRESS) {
778 intrrx = readw(&musbr->intrrx);
779 intrtx = readw(&musbr->intrtx);
782 musb_peri_rx(intrrx);
785 musb_peri_tx(intrtx);
787 if (MUSB_INTR_SOF & intrusb) {
789 faddr = readb(&musbr->faddr);
791 * Setting of the address can fail.
792 * Normally it succeeds the second time.
794 if (udc_device->address != faddr)
795 musb_peri_ep0_set_address();
801 void udc_set_nak(int ep_num)
806 void udc_unset_nak(int ep_num)
811 int udc_endpoint_write(struct usb_endpoint_instance *endpoint)
815 /* Transmit only if the hardware is available */
816 if (endpoint->tx_urb && endpoint->state == 0) {
817 unsigned int ep = endpoint->endpoint_address &
818 USB_ENDPOINT_NUMBER_MASK;
820 u16 peri_txcsr = readw(&musbr->ep[ep].epN.txcsr);
822 /* Error conditions */
823 if (peri_txcsr & MUSB_TXCSR_P_UNDERRUN) {
824 peri_txcsr &= ~MUSB_TXCSR_P_UNDERRUN;
825 writew(peri_txcsr, &musbr->ep[ep].epN.txcsr);
829 musb_print_txcsr(peri_txcsr);
831 /* Check if a packet is waiting to be sent */
832 if (!(peri_txcsr & MUSB_TXCSR_TXPKTRDY)) {
835 struct urb *urb = endpoint->tx_urb;
836 unsigned int remaining_packet = urb->actual_length -
839 if (endpoint->tx_packetSize < remaining_packet)
840 length = endpoint->tx_packetSize;
842 length = remaining_packet;
844 data = (u8 *) urb->buffer;
845 data += endpoint->sent;
847 /* common musb fifo function */
848 write_fifo(ep, length, data);
850 musb_peri_tx_ready(ep);
852 endpoint->last = length;
853 /* usbd_tx_complete will take care of updating 'sent' */
854 usbd_tx_complete(endpoint);
858 serial_printf("ERROR : %s Problem with urb %p "
861 endpoint->tx_urb, endpoint->state);
867 void udc_setup_ep(struct usb_device_instance *device, unsigned int id,
868 struct usb_endpoint_instance *endpoint)
872 ep0_endpoint = endpoint;
873 ep0_endpoint->endpoint_address = 0xff;
874 ep0_urb = usbd_alloc_urb(device, endpoint);
875 } else if (MAX_ENDPOINT >= id) {
878 /* Check the direction */
879 ep_addr = endpoint->endpoint_address;
880 if (USB_DIR_IN == (ep_addr & USB_ENDPOINT_DIR_MASK)) {
882 epinfo[(id * 2) + 1].epsize = endpoint->tx_packetSize;
885 epinfo[id * 2].epsize = endpoint->rcv_packetSize;
888 musb_configure_ep(&epinfo[0],
889 sizeof(epinfo) / sizeof(struct musb_epinfo));
892 serial_printf("ERROR : %s endpoint request %d "
893 "exceeds maximum %d\n",
894 __PRETTY_FUNCTION__, id, MAX_ENDPOINT);
898 void udc_connect(void)
903 void udc_disconnect(void)
908 void udc_enable(struct usb_device_instance *device)
910 /* Save the device structure pointer */
916 void udc_disable(void)
921 void udc_startup_events(struct usb_device_instance *device)
923 /* The DEVICE_INIT event puts the USB device in the state STATE_INIT. */
924 usbd_device_event_irq(device, DEVICE_INIT, 0);
927 * The DEVICE_CREATE event puts the USB device in the state
930 usbd_device_event_irq(device, DEVICE_CREATE, 0);
932 /* Resets the address to 0 */
933 usbd_device_event_irq(device, DEVICE_RESET, 0);
943 ret = musb_platform_init();
947 /* Configure all the endpoint FIFO's and start usb controller */
948 musbr = musb_cfg.regs;
950 /* Initialize the endpoints */
951 for (ep_loop = 0; ep_loop < MAX_ENDPOINT * 2; ep_loop++) {
952 epinfo[ep_loop].epnum = (ep_loop / 2) + 1;
953 epinfo[ep_loop].epdir = ep_loop % 2; /* OUT, IN */
954 epinfo[ep_loop].epsize = 0;
957 musb_peri_softconnect();