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 <usb/musb_udc.h>
43 #include "../gadget/ep0.h"
44 #include "musb_core.h"
45 #if defined(CONFIG_USB_OMAP3)
47 #elif defined(CONFIG_USB_AM35X)
49 #elif defined(CONFIG_USB_DAVINCI)
53 /* Define MUSB_DEBUG for debugging */
54 /* #define MUSB_DEBUG */
55 #include "musb_debug.h"
57 #define MAX_ENDPOINT 15
59 #define GET_ENDPOINT(dev,ep) \
60 (((struct usb_device_instance *)(dev))->bus->endpoint_array + ep)
62 #define SET_EP0_STATE(s) \
64 if ((0 <= (s)) && (SET_ADDRESS >= (s))) { \
65 if ((s) != ep0_state) { \
66 if ((debug_setup) && (debug_level > 1)) \
67 serial_printf("INFO : Changing state " \
68 "from %s to %s in %s at " \
70 ep0_state_strings[ep0_state],\
71 ep0_state_strings[s], \
72 __PRETTY_FUNCTION__, \
77 if (debug_level > 0) \
78 serial_printf("Error at %s %d with setting " \
79 "state %d is invalid\n", \
80 __PRETTY_FUNCTION__, __LINE__, s); \
84 /* static implies these initialized to 0 or NULL */
85 static int debug_setup;
86 static int debug_level;
87 static struct musb_epinfo epinfo[MAX_ENDPOINT * 2];
88 static enum ep0_state_enum {
94 static char *ep0_state_strings[4] = {
101 static struct urb *ep0_urb;
102 struct usb_endpoint_instance *ep0_endpoint;
103 static struct usb_device_instance *udc_device;
107 static void musb_db_regs(void)
112 b = readb(&musbr->faddr);
113 serial_printf("\tfaddr 0x%2.2x\n", b);
115 b = readb(&musbr->power);
118 w = readw(&musbr->ep[0].ep0.csr0);
121 b = readb(&musbr->devctl);
122 musb_print_devctl(b);
124 b = readb(&musbr->ep[0].ep0.configdata);
125 musb_print_config(b);
127 w = readw(&musbr->frame);
128 serial_printf("\tframe 0x%4.4x\n", w);
130 b = readb(&musbr->index);
131 serial_printf("\tindex 0x%2.2x\n", b);
133 w = readw(&musbr->ep[1].epN.rxmaxp);
134 musb_print_rxmaxp(w);
136 w = readw(&musbr->ep[1].epN.rxcsr);
139 w = readw(&musbr->ep[1].epN.txmaxp);
140 musb_print_txmaxp(w);
142 w = readw(&musbr->ep[1].epN.txcsr);
146 #define musb_db_regs()
147 #endif /* DEBUG_MUSB */
149 static void musb_peri_softconnect(void)
154 power = readb(&musbr->power);
155 power &= ~MUSB_POWER_SOFTCONN;
156 writeb(power, &musbr->power);
158 /* Read intr to clear */
159 readb(&musbr->intrusb);
160 readw(&musbr->intrrx);
161 readw(&musbr->intrtx);
163 udelay(1000 * 1000); /* 1 sec */
166 power = readb(&musbr->power);
167 power |= MUSB_POWER_SOFTCONN;
169 * The usb device interface is usb 1.1
170 * Disable 2.0 high speed by clearring the hsenable bit.
172 power &= ~MUSB_POWER_HSENAB;
173 writeb(power, &musbr->power);
175 /* Check if device is in b-peripheral mode */
176 devctl = readb(&musbr->devctl);
177 if (!(devctl & MUSB_DEVCTL_BDEVICE) ||
178 (devctl & MUSB_DEVCTL_HM)) {
179 serial_printf("ERROR : Unsupport USB mode\n");
180 serial_printf("Check that mini-B USB cable is attached "
184 if (debug_setup && (debug_level > 1))
188 static void musb_peri_reset(void)
190 if ((debug_setup) && (debug_level > 1))
191 serial_printf("INFO : %s reset\n", __PRETTY_FUNCTION__);
194 ep0_endpoint->endpoint_address = 0xff;
196 /* Sync sw and hw addresses */
197 writeb(udc_device->address, &musbr->faddr);
202 static void musb_peri_resume(void)
207 static void musb_peri_ep0_stall(void)
211 csr0 = readw(&musbr->ep[0].ep0.csr0);
212 csr0 |= MUSB_CSR0_P_SENDSTALL;
213 writew(csr0, &musbr->ep[0].ep0.csr0);
214 if ((debug_setup) && (debug_level > 1))
215 serial_printf("INFO : %s stall\n", __PRETTY_FUNCTION__);
218 static void musb_peri_ep0_ack_req(void)
222 csr0 = readw(&musbr->ep[0].ep0.csr0);
223 csr0 |= MUSB_CSR0_P_SVDRXPKTRDY;
224 writew(csr0, &musbr->ep[0].ep0.csr0);
227 static void musb_ep0_tx_ready(void)
231 csr0 = readw(&musbr->ep[0].ep0.csr0);
232 csr0 |= MUSB_CSR0_TXPKTRDY;
233 writew(csr0, &musbr->ep[0].ep0.csr0);
236 static void musb_ep0_tx_ready_and_last(void)
240 csr0 = readw(&musbr->ep[0].ep0.csr0);
241 csr0 |= (MUSB_CSR0_TXPKTRDY | MUSB_CSR0_P_DATAEND);
242 writew(csr0, &musbr->ep[0].ep0.csr0);
245 static void musb_peri_ep0_last(void)
249 csr0 = readw(&musbr->ep[0].ep0.csr0);
250 csr0 |= MUSB_CSR0_P_DATAEND;
251 writew(csr0, &musbr->ep[0].ep0.csr0);
254 static void musb_peri_ep0_set_address(void)
257 writeb(udc_device->address, &musbr->faddr);
260 faddr = readb(&musbr->faddr);
261 if (udc_device->address == faddr) {
263 usbd_device_event_irq(udc_device, DEVICE_ADDRESS_ASSIGNED, 0);
264 if ((debug_setup) && (debug_level > 1))
265 serial_printf("INFO : %s Address set to %d\n",
266 __PRETTY_FUNCTION__, udc_device->address);
269 serial_printf("ERROR : %s Address missmatch "
272 udc_device->address, faddr);
276 static void musb_peri_rx_ack(unsigned int ep)
280 peri_rxcsr = readw(&musbr->ep[ep].epN.rxcsr);
281 peri_rxcsr &= ~MUSB_RXCSR_RXPKTRDY;
282 writew(peri_rxcsr, &musbr->ep[ep].epN.rxcsr);
285 static void musb_peri_tx_ready(unsigned int ep)
289 peri_txcsr = readw(&musbr->ep[ep].epN.txcsr);
290 peri_txcsr |= MUSB_TXCSR_TXPKTRDY;
291 writew(peri_txcsr, &musbr->ep[ep].epN.txcsr);
294 static void musb_peri_ep0_zero_data_request(int err)
296 musb_peri_ep0_ack_req();
299 musb_peri_ep0_stall();
303 musb_peri_ep0_last();
306 switch (ep0_urb->device_request.bRequest) {
307 case USB_REQ_SET_ADDRESS:
308 if ((debug_setup) && (debug_level > 1))
309 serial_printf("INFO : %s received set "
310 "address\n", __PRETTY_FUNCTION__);
313 case USB_REQ_SET_CONFIGURATION:
314 if ((debug_setup) && (debug_level > 1))
315 serial_printf("INFO : %s Configured\n",
316 __PRETTY_FUNCTION__);
317 usbd_device_event_irq(udc_device, DEVICE_CONFIGURED, 0);
322 if (USB_REQ_SET_ADDRESS == ep0_urb->device_request.bRequest) {
323 SET_EP0_STATE(SET_ADDRESS);
330 static void musb_peri_ep0_rx_data_request(void)
333 * This is the completion of the data OUT / RX
335 * Host is sending data to ep0 that is not
336 * part of setup. This comes from the cdc_recv_setup
337 * op that is device specific.
340 musb_peri_ep0_ack_req();
342 ep0_endpoint->rcv_urb = ep0_urb;
343 ep0_urb->actual_length = 0;
347 static void musb_peri_ep0_tx_data_request(int err)
350 musb_peri_ep0_stall();
353 musb_peri_ep0_ack_req();
355 ep0_endpoint->tx_urb = ep0_urb;
356 ep0_endpoint->sent = 0;
361 static void musb_peri_ep0_idle(void)
369 * A lot of confusion can be caused if the address
370 * in software, udc layer, does not agree with the
371 * hardware. Since the setting of the hardware address
372 * must be set after the set address request, the
373 * usb state machine is out of sync for a few frame.
374 * It is a good idea to run this check when changes
375 * are made to the state machine.
377 if ((debug_level > 0) &&
378 (ep0_state != SET_ADDRESS)) {
381 faddr = readb(&musbr->faddr);
382 if (udc_device->address != faddr) {
383 serial_printf("ERROR : %s addresses do not"
384 "match sw %d vs hw %d\n",
386 udc_device->address, faddr);
392 csr0 = readw(&musbr->ep[0].ep0.csr0);
394 if (!(MUSB_CSR0_RXPKTRDY & csr0))
397 count0 = readw(&musbr->ep[0].ep0.count0);
402 if ((debug_setup) && (debug_level > 1))
403 serial_printf("WARN : %s SETUP incorrect size %d\n",
404 __PRETTY_FUNCTION__, count0);
405 musb_peri_ep0_stall();
409 read_fifo(0, count0, &ep0_urb->device_request);
412 print_usb_device_request(&ep0_urb->device_request);
414 if (ep0_urb->device_request.wLength == 0) {
415 err = ep0_recv_setup(ep0_urb);
417 /* Zero data request */
418 musb_peri_ep0_zero_data_request(err);
420 /* Is data coming or going ? */
421 u8 reqType = ep0_urb->device_request.bmRequestType;
423 if (USB_REQ_DEVICE2HOST == (reqType & USB_REQ_DIRECTION_MASK)) {
424 err = ep0_recv_setup(ep0_urb);
426 musb_peri_ep0_tx_data_request(err);
431 * The RX routine will call ep0_recv_setup
432 * when the data packet has arrived.
434 musb_peri_ep0_rx_data_request();
442 static void musb_peri_ep0_rx(void)
445 * This is the completion of the data OUT / RX
447 * Host is sending data to ep0 that is not
448 * part of setup. This comes from the cdc_recv_setup
449 * op that is device specific.
451 * Pass the data back to driver ep0_recv_setup which
452 * should give the cdc_recv_setup the chance to handle
458 if (debug_level > 3) {
459 if (0 != ep0_urb->actual_length) {
460 serial_printf("%s finished ? %d of %d\n",
462 ep0_urb->actual_length,
463 ep0_urb->device_request.wLength);
467 if (ep0_urb->device_request.wLength == ep0_urb->actual_length) {
468 musb_peri_ep0_last();
470 ep0_recv_setup(ep0_urb);
474 csr0 = readw(&musbr->ep[0].ep0.csr0);
475 if (!(MUSB_CSR0_RXPKTRDY & csr0))
478 count0 = readw(&musbr->ep[0].ep0.count0);
481 struct usb_endpoint_instance *endpoint;
485 endpoint = ep0_endpoint;
486 if (endpoint && endpoint->rcv_urb) {
487 struct urb *urb = endpoint->rcv_urb;
488 unsigned int remaining_space = urb->buffer_length -
491 if (remaining_space) {
492 int urb_bad = 0; /* urb is good */
494 if (count0 > remaining_space)
495 length = remaining_space;
499 data = (u8 *) urb->buffer_data;
500 data += urb->actual_length;
502 /* The common musb fifo reader */
503 read_fifo(0, length, data);
505 musb_peri_ep0_ack_req();
508 * urb's actual_length is updated in
511 usbd_rcv_complete(endpoint, length, urb_bad);
515 serial_printf("ERROR : %s no space in "
517 __PRETTY_FUNCTION__);
521 serial_printf("ERROR : %s problem with "
523 __PRETTY_FUNCTION__);
527 serial_printf("ERROR : %s with nothing to do\n",
528 __PRETTY_FUNCTION__);
532 static void musb_peri_ep0_tx(void)
535 int transfer_size = 0;
538 csr0 = readw(&musbr->ep[0].ep0.csr0);
540 /* Check for pending tx */
541 if (csr0 & MUSB_CSR0_TXPKTRDY)
544 /* Check if this is the last packet sent */
545 if (ep0_endpoint->sent >= ep0_urb->actual_length) {
550 transfer_size = ep0_urb->actual_length - ep0_endpoint->sent;
551 /* Is the transfer size negative ? */
552 if (transfer_size <= 0) {
554 serial_printf("ERROR : %s problem with the"
555 " transfer size %d\n",
562 /* Truncate large transfers to the fifo size */
563 if (transfer_size > ep0_endpoint->tx_packetSize)
564 transfer_size = ep0_endpoint->tx_packetSize;
566 write_fifo(0, transfer_size, &ep0_urb->buffer[ep0_endpoint->sent]);
567 ep0_endpoint->sent += transfer_size;
569 /* Done or more to send ? */
570 if (ep0_endpoint->sent >= ep0_urb->actual_length)
571 musb_ep0_tx_ready_and_last();
577 for (p = 0; p < pm; p++) {
578 csr0 = readw(&musbr->ep[0].ep0.csr0);
579 if (!(csr0 & MUSB_CSR0_TXPKTRDY))
582 /* Double the delay. */
586 if ((ep0_endpoint->sent >= ep0_urb->actual_length) && (p < pm))
593 static void musb_peri_ep0(void)
597 if (SET_ADDRESS == ep0_state)
600 csr0 = readw(&musbr->ep[0].ep0.csr0);
602 /* Error conditions */
603 if (MUSB_CSR0_P_SENTSTALL & csr0) {
604 csr0 &= ~MUSB_CSR0_P_SENTSTALL;
605 writew(csr0, &musbr->ep[0].ep0.csr0);
608 if (MUSB_CSR0_P_SETUPEND & csr0) {
609 csr0 |= MUSB_CSR0_P_SVDSETUPEND;
610 writew(csr0, &musbr->ep[0].ep0.csr0);
612 if ((debug_setup) && (debug_level > 1))
613 serial_printf("WARN: %s SETUPEND\n",
614 __PRETTY_FUNCTION__);
618 if (IDLE == ep0_state)
619 musb_peri_ep0_idle();
628 static void musb_peri_rx_ep(unsigned int ep)
631 u8 peri_rxcsr = readw(&musbr->ep[ep].epN.rxcsr);
633 if (!(peri_rxcsr & MUSB_RXCSR_RXPKTRDY)) {
635 serial_printf("ERROR : %s %d without MUSB_RXCSR_RXPKTRDY set\n",
636 __PRETTY_FUNCTION__, ep);
640 peri_rxcount = readw(&musbr->ep[ep].epN.rxcount);
642 struct usb_endpoint_instance *endpoint;
646 endpoint = GET_ENDPOINT(udc_device, ep);
647 if (endpoint && endpoint->rcv_urb) {
648 struct urb *urb = endpoint->rcv_urb;
649 unsigned int remaining_space = urb->buffer_length -
652 if (remaining_space) {
653 int urb_bad = 0; /* urb is good */
655 if (peri_rxcount > remaining_space)
656 length = remaining_space;
658 length = peri_rxcount;
660 data = (u8 *) urb->buffer_data;
661 data += urb->actual_length;
663 /* The common musb fifo reader */
664 read_fifo(ep, length, data);
666 musb_peri_rx_ack(ep);
669 * urb's actual_length is updated in
672 usbd_rcv_complete(endpoint, length, urb_bad);
676 serial_printf("ERROR : %s %d no space "
678 __PRETTY_FUNCTION__, ep);
682 serial_printf("ERROR : %s %d problem with "
684 __PRETTY_FUNCTION__, ep);
689 serial_printf("ERROR : %s %d with nothing to do\n",
690 __PRETTY_FUNCTION__, ep);
694 static void musb_peri_rx(u16 intr)
702 for (ep = 1; ep < 16; ep++) {
703 if ((1 << ep) & intr)
708 static void musb_peri_tx(u16 intr)
715 * Use this in the future when handling epN tx
719 * for (ep = 1; ep < 16; ep++) {
720 * if ((1 << ep) & intr) {
721 * / * handle tx for this endpoint * /
729 /* This is a high freq called function */
733 intrusb = readb(&musbr->intrusb);
736 * See drivers/usb/gadget/mpc8xx_udc.c for
737 * state diagram going from detached through
740 if (MUSB_INTR_RESUME & intrusb) {
741 usbd_device_event_irq(udc_device,
742 DEVICE_BUS_ACTIVITY, 0);
748 if (MUSB_INTR_RESET & intrusb) {
749 usbd_device_event_irq(udc_device, DEVICE_RESET, 0);
753 if (MUSB_INTR_DISCONNECT & intrusb) {
754 /* cable unplugged from hub/host */
755 usbd_device_event_irq(udc_device, DEVICE_RESET, 0);
757 usbd_device_event_irq(udc_device, DEVICE_HUB_RESET, 0);
760 if (MUSB_INTR_SOF & intrusb) {
761 usbd_device_event_irq(udc_device,
762 DEVICE_BUS_ACTIVITY, 0);
766 if (MUSB_INTR_SUSPEND & intrusb) {
767 usbd_device_event_irq(udc_device,
768 DEVICE_BUS_INACTIVE, 0);
771 if (ep0_state != SET_ADDRESS) {
774 intrrx = readw(&musbr->intrrx);
775 intrtx = readw(&musbr->intrtx);
778 musb_peri_rx(intrrx);
781 musb_peri_tx(intrtx);
783 if (MUSB_INTR_SOF & intrusb) {
785 faddr = readb(&musbr->faddr);
787 * Setting of the address can fail.
788 * Normally it succeeds the second time.
790 if (udc_device->address != faddr)
791 musb_peri_ep0_set_address();
797 void udc_set_nak(int ep_num)
802 void udc_unset_nak(int ep_num)
807 int udc_endpoint_write(struct usb_endpoint_instance *endpoint)
811 /* Transmit only if the hardware is available */
812 if (endpoint->tx_urb && endpoint->state == 0) {
813 unsigned int ep = endpoint->endpoint_address &
814 USB_ENDPOINT_NUMBER_MASK;
816 u16 peri_txcsr = readw(&musbr->ep[ep].epN.txcsr);
818 /* Error conditions */
819 if (peri_txcsr & MUSB_TXCSR_P_UNDERRUN) {
820 peri_txcsr &= ~MUSB_TXCSR_P_UNDERRUN;
821 writew(peri_txcsr, &musbr->ep[ep].epN.txcsr);
825 musb_print_txcsr(peri_txcsr);
827 /* Check if a packet is waiting to be sent */
828 if (!(peri_txcsr & MUSB_TXCSR_TXPKTRDY)) {
831 struct urb *urb = endpoint->tx_urb;
832 unsigned int remaining_packet = urb->actual_length -
835 if (endpoint->tx_packetSize < remaining_packet)
836 length = endpoint->tx_packetSize;
838 length = remaining_packet;
840 data = (u8 *) urb->buffer;
841 data += endpoint->sent;
843 /* common musb fifo function */
844 write_fifo(ep, length, data);
846 musb_peri_tx_ready(ep);
848 endpoint->last = length;
849 /* usbd_tx_complete will take care of updating 'sent' */
850 usbd_tx_complete(endpoint);
854 serial_printf("ERROR : %s Problem with urb %p "
857 endpoint->tx_urb, endpoint->state);
863 void udc_setup_ep(struct usb_device_instance *device, unsigned int id,
864 struct usb_endpoint_instance *endpoint)
868 ep0_endpoint = endpoint;
869 ep0_endpoint->endpoint_address = 0xff;
870 ep0_urb = usbd_alloc_urb(device, endpoint);
871 } else if (MAX_ENDPOINT >= id) {
874 /* Check the direction */
875 ep_addr = endpoint->endpoint_address;
876 if (USB_DIR_IN == (ep_addr & USB_ENDPOINT_DIR_MASK)) {
878 epinfo[(id * 2) + 1].epsize = endpoint->tx_packetSize;
881 epinfo[id * 2].epsize = endpoint->rcv_packetSize;
884 musb_configure_ep(&epinfo[0], ARRAY_SIZE(epinfo));
887 serial_printf("ERROR : %s endpoint request %d "
888 "exceeds maximum %d\n",
889 __PRETTY_FUNCTION__, id, MAX_ENDPOINT);
893 void udc_connect(void)
898 void udc_disconnect(void)
903 void udc_enable(struct usb_device_instance *device)
905 /* Save the device structure pointer */
911 void udc_disable(void)
916 void udc_startup_events(struct usb_device_instance *device)
918 /* The DEVICE_INIT event puts the USB device in the state STATE_INIT. */
919 usbd_device_event_irq(device, DEVICE_INIT, 0);
922 * The DEVICE_CREATE event puts the USB device in the state
925 usbd_device_event_irq(device, DEVICE_CREATE, 0);
927 /* Resets the address to 0 */
928 usbd_device_event_irq(device, DEVICE_RESET, 0);
938 ret = musb_platform_init();
942 /* Configure all the endpoint FIFO's and start usb controller */
943 musbr = musb_cfg.regs;
945 /* Initialize the endpoints */
946 for (ep_loop = 0; ep_loop < MAX_ENDPOINT * 2; ep_loop++) {
947 epinfo[ep_loop].epnum = (ep_loop / 2) + 1;
948 epinfo[ep_loop].epdir = ep_loop % 2; /* OUT, IN */
949 epinfo[ep_loop].epsize = 0;
952 musb_peri_softconnect();