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)
169 power = readb(&musbr->power);
170 power &= ~MUSB_POWER_SOFTCONN;
171 writeb(power, &musbr->power);
173 /* Read intr to clear */
174 intrusb = readb(&musbr->intrusb);
175 intrrx = readw(&musbr->intrrx);
176 intrtx = readw(&musbr->intrtx);
178 udelay(1000 * 1000); /* 1 sec */
181 power = readb(&musbr->power);
182 power |= MUSB_POWER_SOFTCONN;
184 * The usb device interface is usb 1.1
185 * Disable 2.0 high speed by clearring the hsenable bit.
187 power &= ~MUSB_POWER_HSENAB;
188 writeb(power, &musbr->power);
190 /* Check if device is in b-peripheral mode */
191 devctl = readb(&musbr->devctl);
192 if (!(devctl & MUSB_DEVCTL_BDEVICE) ||
193 (devctl & MUSB_DEVCTL_HM)) {
194 serial_printf("ERROR : Unsupport USB mode\n");
195 serial_printf("Check that mini-B USB cable is attached "
199 if (debug_setup && (debug_level > 1))
203 static void musb_peri_reset(void)
205 if ((debug_setup) && (debug_level > 1))
206 serial_printf("INFO : %s reset\n", __PRETTY_FUNCTION__);
209 ep0_endpoint->endpoint_address = 0xff;
211 /* Sync sw and hw addresses */
212 writeb(udc_device->address, &musbr->faddr);
217 static void musb_peri_resume(void)
222 static void musb_peri_ep0_stall(void)
226 csr0 = readw(&musbr->ep[0].ep0.csr0);
227 csr0 |= MUSB_CSR0_P_SENDSTALL;
228 writew(csr0, &musbr->ep[0].ep0.csr0);
229 if ((debug_setup) && (debug_level > 1))
230 serial_printf("INFO : %s stall\n", __PRETTY_FUNCTION__);
233 static void musb_peri_ep0_ack_req(void)
237 csr0 = readw(&musbr->ep[0].ep0.csr0);
238 csr0 |= MUSB_CSR0_P_SVDRXPKTRDY;
239 writew(csr0, &musbr->ep[0].ep0.csr0);
242 static void musb_ep0_tx_ready(void)
246 csr0 = readw(&musbr->ep[0].ep0.csr0);
247 csr0 |= MUSB_CSR0_TXPKTRDY;
248 writew(csr0, &musbr->ep[0].ep0.csr0);
251 static void musb_ep0_tx_ready_and_last(void)
255 csr0 = readw(&musbr->ep[0].ep0.csr0);
256 csr0 |= (MUSB_CSR0_TXPKTRDY | MUSB_CSR0_P_DATAEND);
257 writew(csr0, &musbr->ep[0].ep0.csr0);
260 static void musb_peri_ep0_last(void)
264 csr0 = readw(&musbr->ep[0].ep0.csr0);
265 csr0 |= MUSB_CSR0_P_DATAEND;
266 writew(csr0, &musbr->ep[0].ep0.csr0);
269 static void musb_peri_ep0_set_address(void)
272 writeb(udc_device->address, &musbr->faddr);
275 faddr = readb(&musbr->faddr);
276 if (udc_device->address == faddr) {
278 usbd_device_event_irq(udc_device, DEVICE_ADDRESS_ASSIGNED, 0);
279 if ((debug_setup) && (debug_level > 1))
280 serial_printf("INFO : %s Address set to %d\n",
281 __PRETTY_FUNCTION__, udc_device->address);
284 serial_printf("ERROR : %s Address missmatch "
287 udc_device->address, faddr);
291 static void musb_peri_rx_ack(unsigned int ep)
295 peri_rxcsr = readw(&musbr->ep[ep].epN.rxcsr);
296 peri_rxcsr &= ~MUSB_RXCSR_RXPKTRDY;
297 writew(peri_rxcsr, &musbr->ep[ep].epN.rxcsr);
300 static void musb_peri_tx_ready(unsigned int ep)
304 peri_txcsr = readw(&musbr->ep[ep].epN.txcsr);
305 peri_txcsr |= MUSB_TXCSR_TXPKTRDY;
306 writew(peri_txcsr, &musbr->ep[ep].epN.txcsr);
309 static void musb_peri_ep0_zero_data_request(int err)
311 musb_peri_ep0_ack_req();
314 musb_peri_ep0_stall();
318 musb_peri_ep0_last();
321 switch (ep0_urb->device_request.bRequest) {
322 case USB_REQ_SET_ADDRESS:
323 if ((debug_setup) && (debug_level > 1))
324 serial_printf("INFO : %s received set "
325 "address\n", __PRETTY_FUNCTION__);
328 case USB_REQ_SET_CONFIGURATION:
329 if ((debug_setup) && (debug_level > 1))
330 serial_printf("INFO : %s Configured\n",
331 __PRETTY_FUNCTION__);
332 usbd_device_event_irq(udc_device, DEVICE_CONFIGURED, 0);
337 if (USB_REQ_SET_ADDRESS == ep0_urb->device_request.bRequest) {
338 SET_EP0_STATE(SET_ADDRESS);
345 static void musb_peri_ep0_rx_data_request(void)
348 * This is the completion of the data OUT / RX
350 * Host is sending data to ep0 that is not
351 * part of setup. This comes from the cdc_recv_setup
352 * op that is device specific.
355 musb_peri_ep0_ack_req();
357 ep0_endpoint->rcv_urb = ep0_urb;
358 ep0_urb->actual_length = 0;
362 static void musb_peri_ep0_tx_data_request(int err)
365 musb_peri_ep0_stall();
368 musb_peri_ep0_ack_req();
370 ep0_endpoint->tx_urb = ep0_urb;
371 ep0_endpoint->sent = 0;
376 static void musb_peri_ep0_idle(void)
384 * A lot of confusion can be caused if the address
385 * in software, udc layer, does not agree with the
386 * hardware. Since the setting of the hardware address
387 * must be set after the set address request, the
388 * usb state machine is out of sync for a few frame.
389 * It is a good idea to run this check when changes
390 * are made to the state machine.
392 if ((debug_level > 0) &&
393 (ep0_state != SET_ADDRESS)) {
396 faddr = readb(&musbr->faddr);
397 if (udc_device->address != faddr) {
398 serial_printf("ERROR : %s addresses do not"
399 "match sw %d vs hw %d\n",
401 udc_device->address, faddr);
407 csr0 = readw(&musbr->ep[0].ep0.csr0);
409 if (!(MUSB_CSR0_RXPKTRDY & csr0))
412 count0 = readw(&musbr->ep[0].ep0.count0);
417 if ((debug_setup) && (debug_level > 1))
418 serial_printf("WARN : %s SETUP incorrect size %d\n",
419 __PRETTY_FUNCTION__, count0);
420 musb_peri_ep0_stall();
424 read_fifo(0, count0, &ep0_urb->device_request);
427 print_usb_device_request(&ep0_urb->device_request);
429 if (ep0_urb->device_request.wLength == 0) {
430 err = ep0_recv_setup(ep0_urb);
432 /* Zero data request */
433 musb_peri_ep0_zero_data_request(err);
435 /* Is data coming or going ? */
436 u8 reqType = ep0_urb->device_request.bmRequestType;
438 if (USB_REQ_DEVICE2HOST == (reqType & USB_REQ_DIRECTION_MASK)) {
439 err = ep0_recv_setup(ep0_urb);
441 musb_peri_ep0_tx_data_request(err);
446 * The RX routine will call ep0_recv_setup
447 * when the data packet has arrived.
449 musb_peri_ep0_rx_data_request();
457 static void musb_peri_ep0_rx(void)
460 * This is the completion of the data OUT / RX
462 * Host is sending data to ep0 that is not
463 * part of setup. This comes from the cdc_recv_setup
464 * op that is device specific.
466 * Pass the data back to driver ep0_recv_setup which
467 * should give the cdc_recv_setup the chance to handle
473 if (debug_level > 3) {
474 if (0 != ep0_urb->actual_length) {
475 serial_printf("%s finished ? %d of %d\n",
477 ep0_urb->actual_length,
478 ep0_urb->device_request.wLength);
482 if (ep0_urb->device_request.wLength == ep0_urb->actual_length) {
483 musb_peri_ep0_last();
485 ep0_recv_setup(ep0_urb);
489 csr0 = readw(&musbr->ep[0].ep0.csr0);
490 if (!(MUSB_CSR0_RXPKTRDY & csr0))
493 count0 = readw(&musbr->ep[0].ep0.count0);
496 struct usb_endpoint_instance *endpoint;
500 endpoint = ep0_endpoint;
501 if (endpoint && endpoint->rcv_urb) {
502 struct urb *urb = endpoint->rcv_urb;
503 unsigned int remaining_space = urb->buffer_length -
506 if (remaining_space) {
507 int urb_bad = 0; /* urb is good */
509 if (count0 > remaining_space)
510 length = remaining_space;
514 data = (u8 *) urb->buffer_data;
515 data += urb->actual_length;
517 /* The common musb fifo reader */
518 read_fifo(0, length, data);
520 musb_peri_ep0_ack_req();
523 * urb's actual_length is updated in
526 usbd_rcv_complete(endpoint, length, urb_bad);
530 serial_printf("ERROR : %s no space in "
532 __PRETTY_FUNCTION__);
536 serial_printf("ERROR : %s problem with "
538 __PRETTY_FUNCTION__);
542 serial_printf("ERROR : %s with nothing to do\n",
543 __PRETTY_FUNCTION__);
547 static void musb_peri_ep0_tx(void)
550 int transfer_size = 0;
553 csr0 = readw(&musbr->ep[0].ep0.csr0);
555 /* Check for pending tx */
556 if (csr0 & MUSB_CSR0_TXPKTRDY)
559 /* Check if this is the last packet sent */
560 if (ep0_endpoint->sent >= ep0_urb->actual_length) {
565 transfer_size = ep0_urb->actual_length - ep0_endpoint->sent;
566 /* Is the transfer size negative ? */
567 if (transfer_size <= 0) {
569 serial_printf("ERROR : %s problem with the"
570 " transfer size %d\n",
577 /* Truncate large transfers to the fifo size */
578 if (transfer_size > ep0_endpoint->tx_packetSize)
579 transfer_size = ep0_endpoint->tx_packetSize;
581 write_fifo(0, transfer_size, &ep0_urb->buffer[ep0_endpoint->sent]);
582 ep0_endpoint->sent += transfer_size;
584 /* Done or more to send ? */
585 if (ep0_endpoint->sent >= ep0_urb->actual_length)
586 musb_ep0_tx_ready_and_last();
592 for (p = 0; p < pm; p++) {
593 csr0 = readw(&musbr->ep[0].ep0.csr0);
594 if (!(csr0 & MUSB_CSR0_TXPKTRDY))
597 /* Double the delay. */
601 if ((ep0_endpoint->sent >= ep0_urb->actual_length) && (p < pm))
608 static void musb_peri_ep0(void)
612 if (SET_ADDRESS == ep0_state)
615 csr0 = readw(&musbr->ep[0].ep0.csr0);
617 /* Error conditions */
618 if (MUSB_CSR0_P_SENTSTALL & csr0) {
619 csr0 &= ~MUSB_CSR0_P_SENTSTALL;
620 writew(csr0, &musbr->ep[0].ep0.csr0);
623 if (MUSB_CSR0_P_SETUPEND & csr0) {
624 csr0 |= MUSB_CSR0_P_SVDSETUPEND;
625 writew(csr0, &musbr->ep[0].ep0.csr0);
627 if ((debug_setup) && (debug_level > 1))
628 serial_printf("WARN: %s SETUPEND\n",
629 __PRETTY_FUNCTION__);
633 if (IDLE == ep0_state)
634 musb_peri_ep0_idle();
643 static void musb_peri_rx_ep(unsigned int ep)
645 u16 peri_rxcount = readw(&musbr->ep[ep].epN.rxcount);
648 struct usb_endpoint_instance *endpoint;
652 endpoint = GET_ENDPOINT(udc_device, ep);
653 if (endpoint && endpoint->rcv_urb) {
654 struct urb *urb = endpoint->rcv_urb;
655 unsigned int remaining_space = urb->buffer_length -
658 if (remaining_space) {
659 int urb_bad = 0; /* urb is good */
661 if (peri_rxcount > remaining_space)
662 length = remaining_space;
664 length = peri_rxcount;
666 data = (u8 *) urb->buffer_data;
667 data += urb->actual_length;
669 /* The common musb fifo reader */
670 read_fifo(ep, length, data);
672 musb_peri_rx_ack(ep);
675 * urb's actual_length is updated in
678 usbd_rcv_complete(endpoint, length, urb_bad);
682 serial_printf("ERROR : %s %d no space "
684 __PRETTY_FUNCTION__, ep);
688 serial_printf("ERROR : %s %d problem with "
690 __PRETTY_FUNCTION__, ep);
695 serial_printf("ERROR : %s %d with nothing to do\n",
696 __PRETTY_FUNCTION__, ep);
700 static void musb_peri_rx(u16 intr)
708 for (ep = 1; ep < 16; ep++) {
709 if ((1 << ep) & intr)
714 static void musb_peri_tx(u16 intr)
721 * Use this in the future when handling epN tx
725 * for (ep = 1; ep < 16; ep++) {
726 * if ((1 << ep) & intr) {
727 * / * handle tx for this endpoint * /
735 /* This is a high freq called function */
739 intrusb = readb(&musbr->intrusb);
742 * See drivers/usb/gadget/mpc8xx_udc.c for
743 * state diagram going from detached through
746 if (MUSB_INTR_RESUME & intrusb) {
747 usbd_device_event_irq(udc_device,
748 DEVICE_BUS_ACTIVITY, 0);
754 if (MUSB_INTR_RESET & intrusb) {
755 usbd_device_event_irq(udc_device, DEVICE_RESET, 0);
759 if (MUSB_INTR_DISCONNECT & intrusb) {
760 /* cable unplugged from hub/host */
761 usbd_device_event_irq(udc_device, DEVICE_RESET, 0);
763 usbd_device_event_irq(udc_device, DEVICE_HUB_RESET, 0);
766 if (MUSB_INTR_SOF & intrusb) {
767 usbd_device_event_irq(udc_device,
768 DEVICE_BUS_ACTIVITY, 0);
772 if (MUSB_INTR_SUSPEND & intrusb) {
773 usbd_device_event_irq(udc_device,
774 DEVICE_BUS_INACTIVE, 0);
777 if (ep0_state != SET_ADDRESS) {
780 intrrx = readw(&musbr->intrrx);
781 intrtx = readw(&musbr->intrtx);
784 musb_peri_rx(intrrx);
787 musb_peri_tx(intrtx);
789 if (MUSB_INTR_SOF & intrusb) {
791 faddr = readb(&musbr->faddr);
793 * Setting of the address can fail.
794 * Normally it succeeds the second time.
796 if (udc_device->address != faddr)
797 musb_peri_ep0_set_address();
803 void udc_set_nak(int ep_num)
808 void udc_unset_nak(int ep_num)
813 int udc_endpoint_write(struct usb_endpoint_instance *endpoint)
817 /* Transmit only if the hardware is available */
818 if (endpoint->tx_urb && endpoint->state == 0) {
819 unsigned int ep = endpoint->endpoint_address &
820 USB_ENDPOINT_NUMBER_MASK;
822 u16 peri_txcsr = readw(&musbr->ep[ep].epN.txcsr);
824 /* Error conditions */
825 if (peri_txcsr & MUSB_TXCSR_P_UNDERRUN) {
826 peri_txcsr &= ~MUSB_TXCSR_P_UNDERRUN;
827 writew(peri_txcsr, &musbr->ep[ep].epN.txcsr);
831 musb_print_txcsr(peri_txcsr);
833 /* Check if a packet is waiting to be sent */
834 if (!(peri_txcsr & MUSB_TXCSR_TXPKTRDY)) {
837 struct urb *urb = endpoint->tx_urb;
838 unsigned int remaining_packet = urb->actual_length -
841 if (endpoint->tx_packetSize < remaining_packet)
842 length = endpoint->tx_packetSize;
844 length = remaining_packet;
846 data = (u8 *) urb->buffer;
847 data += endpoint->sent;
849 /* common musb fifo function */
850 write_fifo(ep, length, data);
852 musb_peri_tx_ready(ep);
854 endpoint->last = length;
855 /* usbd_tx_complete will take care of updating 'sent' */
856 usbd_tx_complete(endpoint);
860 serial_printf("ERROR : %s Problem with urb %p "
863 endpoint->tx_urb, endpoint->state);
869 void udc_setup_ep(struct usb_device_instance *device, unsigned int id,
870 struct usb_endpoint_instance *endpoint)
874 ep0_endpoint = endpoint;
875 ep0_endpoint->endpoint_address = 0xff;
876 ep0_urb = usbd_alloc_urb(device, endpoint);
877 } else if (MAX_ENDPOINT >= id) {
880 /* Check the direction */
881 ep_addr = endpoint->endpoint_address;
882 if (USB_DIR_IN == (ep_addr & USB_ENDPOINT_DIR_MASK)) {
884 epinfo[(id * 2) + 1].epsize = endpoint->tx_packetSize;
887 epinfo[id * 2].epsize = endpoint->rcv_packetSize;
890 musb_configure_ep(&epinfo[0],
891 sizeof(epinfo) / sizeof(struct musb_epinfo));
894 serial_printf("ERROR : %s endpoint request %d "
895 "exceeds maximum %d\n",
896 __PRETTY_FUNCTION__, id, MAX_ENDPOINT);
900 void udc_connect(void)
905 void udc_disconnect(void)
910 void udc_enable(struct usb_device_instance *device)
912 /* Save the device structure pointer */
918 void udc_disable(void)
923 void udc_startup_events(struct usb_device_instance *device)
925 /* The DEVICE_INIT event puts the USB device in the state STATE_INIT. */
926 usbd_device_event_irq(device, DEVICE_INIT, 0);
929 * The DEVICE_CREATE event puts the USB device in the state
932 usbd_device_event_irq(device, DEVICE_CREATE, 0);
934 /* Resets the address to 0 */
935 usbd_device_event_irq(device, DEVICE_RESET, 0);
945 ret = musb_platform_init();
949 /* Configure all the endpoint FIFO's and start usb controller */
950 musbr = musb_cfg.regs;
952 /* Initialize the endpoints */
953 for (ep_loop = 0; ep_loop < MAX_ENDPOINT * 2; ep_loop++) {
954 epinfo[ep_loop].epnum = (ep_loop / 2) + 1;
955 epinfo[ep_loop].epdir = ep_loop % 2; /* OUT, IN */
956 epinfo[ep_loop].epsize = 0;
959 musb_peri_softconnect();