1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2009 Wind River Systems, Inc.
6 * This file is a rewrite of the usb device part of
7 * repository git.omapzoom.org/repo/u-boot.git, branch master,
8 * file cpu/omap3/fastboot.c
10 * This is the unique part of its copyright :
12 * -------------------------------------------------------------------------
14 * (C) Copyright 2008 - 2009
15 * Windriver, <www.windriver.com>
18 * -------------------------------------------------------------------------
20 * The details of connecting the device to the uboot usb device subsystem
21 * came from the old omap3 repository www.sakoman.net/u-boot-omap3.git,
22 * branch omap3-dev-usb, file drivers/usb/usbdcore_musb.c
24 * This is the unique part of its copyright :
26 * -------------------------------------------------------------------------
28 * (C) Copyright 2008 Texas Instruments Incorporated.
31 * u-boot OMAP1510 USB drivers (drivers/usbdcore_omap1510.c)
32 * twl4030 init based on linux (drivers/i2c/chips/twl4030_usb.c)
37 * -------------------------------------------------------------------------
42 #include <usbdevice.h>
43 #include <linux/delay.h>
45 #include "../gadget/ep0.h"
46 #include "musb_core.h"
47 #if defined(CONFIG_USB_OMAP3)
49 #elif defined(CONFIG_USB_AM35X)
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 + 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;
106 static u16 pending_intrrx;
109 static void musb_db_regs(void)
114 b = readb(&musbr->faddr);
115 serial_printf("\tfaddr 0x%2.2x\n", b);
117 b = readb(&musbr->power);
120 w = readw(&musbr->ep[0].ep0.csr0);
123 b = readb(&musbr->devctl);
124 musb_print_devctl(b);
126 b = readb(&musbr->ep[0].ep0.configdata);
127 musb_print_config(b);
129 w = readw(&musbr->frame);
130 serial_printf("\tframe 0x%4.4x\n", w);
132 b = readb(&musbr->index);
133 serial_printf("\tindex 0x%2.2x\n", b);
135 w = readw(&musbr->ep[1].epN.rxmaxp);
136 musb_print_rxmaxp(w);
138 w = readw(&musbr->ep[1].epN.rxcsr);
141 w = readw(&musbr->ep[1].epN.txmaxp);
142 musb_print_txmaxp(w);
144 w = readw(&musbr->ep[1].epN.txcsr);
148 #define musb_db_regs()
149 #endif /* DEBUG_MUSB */
151 static void musb_peri_softconnect(void)
156 power = readb(&musbr->power);
157 power &= ~MUSB_POWER_SOFTCONN;
158 writeb(power, &musbr->power);
160 /* Read intr to clear */
161 readb(&musbr->intrusb);
162 readw(&musbr->intrrx);
163 readw(&musbr->intrtx);
165 udelay(1000 * 1000); /* 1 sec */
168 power = readb(&musbr->power);
169 power |= MUSB_POWER_SOFTCONN;
171 * The usb device interface is usb 1.1
172 * Disable 2.0 high speed by clearring the hsenable bit.
174 power &= ~MUSB_POWER_HSENAB;
175 writeb(power, &musbr->power);
177 /* Check if device is in b-peripheral mode */
178 devctl = readb(&musbr->devctl);
179 if (!(devctl & MUSB_DEVCTL_BDEVICE) ||
180 (devctl & MUSB_DEVCTL_HM)) {
181 serial_printf("ERROR : Unsupport USB mode\n");
182 serial_printf("Check that mini-B USB cable is attached "
186 if (debug_setup && (debug_level > 1))
190 static void musb_peri_reset(void)
192 if ((debug_setup) && (debug_level > 1))
193 serial_printf("INFO : %s reset\n", __PRETTY_FUNCTION__);
196 ep0_endpoint->endpoint_address = 0xff;
198 /* Sync sw and hw addresses */
199 writeb(udc_device->address, &musbr->faddr);
204 static void musb_peri_resume(void)
209 static void musb_peri_ep0_stall(void)
213 csr0 = readw(&musbr->ep[0].ep0.csr0);
214 csr0 |= MUSB_CSR0_P_SENDSTALL;
215 writew(csr0, &musbr->ep[0].ep0.csr0);
216 if ((debug_setup) && (debug_level > 1))
217 serial_printf("INFO : %s stall\n", __PRETTY_FUNCTION__);
220 static void musb_peri_ep0_ack_req(void)
224 csr0 = readw(&musbr->ep[0].ep0.csr0);
225 csr0 |= MUSB_CSR0_P_SVDRXPKTRDY;
226 writew(csr0, &musbr->ep[0].ep0.csr0);
229 static void musb_ep0_tx_ready(void)
233 csr0 = readw(&musbr->ep[0].ep0.csr0);
234 csr0 |= MUSB_CSR0_TXPKTRDY;
235 writew(csr0, &musbr->ep[0].ep0.csr0);
238 static void musb_ep0_tx_ready_and_last(void)
242 csr0 = readw(&musbr->ep[0].ep0.csr0);
243 csr0 |= (MUSB_CSR0_TXPKTRDY | MUSB_CSR0_P_DATAEND);
244 writew(csr0, &musbr->ep[0].ep0.csr0);
247 static void musb_peri_ep0_last(void)
251 csr0 = readw(&musbr->ep[0].ep0.csr0);
252 csr0 |= MUSB_CSR0_P_DATAEND;
253 writew(csr0, &musbr->ep[0].ep0.csr0);
256 static void musb_peri_ep0_set_address(void)
259 writeb(udc_device->address, &musbr->faddr);
262 faddr = readb(&musbr->faddr);
263 if (udc_device->address == faddr) {
265 usbd_device_event_irq(udc_device, DEVICE_ADDRESS_ASSIGNED, 0);
266 if ((debug_setup) && (debug_level > 1))
267 serial_printf("INFO : %s Address set to %d\n",
268 __PRETTY_FUNCTION__, udc_device->address);
271 serial_printf("ERROR : %s Address mismatch "
274 udc_device->address, faddr);
278 static void musb_peri_rx_ack(unsigned int ep)
282 peri_rxcsr = readw(&musbr->ep[ep].epN.rxcsr);
283 peri_rxcsr &= ~MUSB_RXCSR_RXPKTRDY;
284 writew(peri_rxcsr, &musbr->ep[ep].epN.rxcsr);
287 static void musb_peri_tx_ready(unsigned int ep)
291 peri_txcsr = readw(&musbr->ep[ep].epN.txcsr);
292 peri_txcsr |= MUSB_TXCSR_TXPKTRDY;
293 writew(peri_txcsr, &musbr->ep[ep].epN.txcsr);
296 static void musb_peri_ep0_zero_data_request(int err)
298 musb_peri_ep0_ack_req();
301 musb_peri_ep0_stall();
305 musb_peri_ep0_last();
308 switch (ep0_urb->device_request.bRequest) {
309 case USB_REQ_SET_ADDRESS:
310 if ((debug_setup) && (debug_level > 1))
311 serial_printf("INFO : %s received set "
312 "address\n", __PRETTY_FUNCTION__);
315 case USB_REQ_SET_CONFIGURATION:
316 if ((debug_setup) && (debug_level > 1))
317 serial_printf("INFO : %s Configured\n",
318 __PRETTY_FUNCTION__);
319 usbd_device_event_irq(udc_device, DEVICE_CONFIGURED, 0);
324 if (USB_REQ_SET_ADDRESS == ep0_urb->device_request.bRequest) {
325 SET_EP0_STATE(SET_ADDRESS);
332 static void musb_peri_ep0_rx_data_request(void)
335 * This is the completion of the data OUT / RX
337 * Host is sending data to ep0 that is not
338 * part of setup. This comes from the cdc_recv_setup
339 * op that is device specific.
342 musb_peri_ep0_ack_req();
344 ep0_endpoint->rcv_urb = ep0_urb;
345 ep0_urb->actual_length = 0;
349 static void musb_peri_ep0_tx_data_request(int err)
352 musb_peri_ep0_stall();
355 musb_peri_ep0_ack_req();
357 ep0_endpoint->tx_urb = ep0_urb;
358 ep0_endpoint->sent = 0;
363 static void musb_peri_ep0_idle(void)
371 * A lot of confusion can be caused if the address
372 * in software, udc layer, does not agree with the
373 * hardware. Since the setting of the hardware address
374 * must be set after the set address request, the
375 * usb state machine is out of sync for a few frame.
376 * It is a good idea to run this check when changes
377 * are made to the state machine.
379 if ((debug_level > 0) &&
380 (ep0_state != SET_ADDRESS)) {
383 faddr = readb(&musbr->faddr);
384 if (udc_device->address != faddr) {
385 serial_printf("ERROR : %s addresses do not"
386 "match sw %d vs hw %d\n",
388 udc_device->address, faddr);
394 csr0 = readw(&musbr->ep[0].ep0.csr0);
396 if (!(MUSB_CSR0_RXPKTRDY & csr0))
399 count0 = readw(&musbr->ep[0].ep0.count0);
404 if ((debug_setup) && (debug_level > 1))
405 serial_printf("WARN : %s SETUP incorrect size %d\n",
406 __PRETTY_FUNCTION__, count0);
407 musb_peri_ep0_stall();
411 read_fifo(0, count0, &ep0_urb->device_request);
414 print_usb_device_request(&ep0_urb->device_request);
416 if (ep0_urb->device_request.wLength == 0) {
417 err = ep0_recv_setup(ep0_urb);
419 /* Zero data request */
420 musb_peri_ep0_zero_data_request(err);
422 /* Is data coming or going ? */
423 u8 reqType = ep0_urb->device_request.bmRequestType;
425 if (USB_REQ_DEVICE2HOST == (reqType & USB_REQ_DIRECTION_MASK)) {
426 err = ep0_recv_setup(ep0_urb);
428 musb_peri_ep0_tx_data_request(err);
433 * The RX routine will call ep0_recv_setup
434 * when the data packet has arrived.
436 musb_peri_ep0_rx_data_request();
444 static void musb_peri_ep0_rx(void)
447 * This is the completion of the data OUT / RX
449 * Host is sending data to ep0 that is not
450 * part of setup. This comes from the cdc_recv_setup
451 * op that is device specific.
453 * Pass the data back to driver ep0_recv_setup which
454 * should give the cdc_recv_setup the chance to handle
460 if (debug_level > 3) {
461 if (0 != ep0_urb->actual_length) {
462 serial_printf("%s finished ? %d of %d\n",
464 ep0_urb->actual_length,
465 ep0_urb->device_request.wLength);
469 if (ep0_urb->device_request.wLength == ep0_urb->actual_length) {
470 musb_peri_ep0_last();
472 ep0_recv_setup(ep0_urb);
476 csr0 = readw(&musbr->ep[0].ep0.csr0);
477 if (!(MUSB_CSR0_RXPKTRDY & csr0))
480 count0 = readw(&musbr->ep[0].ep0.count0);
483 struct usb_endpoint_instance *endpoint;
487 endpoint = ep0_endpoint;
488 if (endpoint && endpoint->rcv_urb) {
489 struct urb *urb = endpoint->rcv_urb;
490 unsigned int remaining_space = urb->buffer_length -
493 if (remaining_space) {
494 int urb_bad = 0; /* urb is good */
496 if (count0 > remaining_space)
497 length = remaining_space;
501 data = (u8 *) urb->buffer_data;
502 data += urb->actual_length;
504 /* The common musb fifo reader */
505 read_fifo(0, length, data);
507 musb_peri_ep0_ack_req();
510 * urb's actual_length is updated in
513 usbd_rcv_complete(endpoint, length, urb_bad);
517 serial_printf("ERROR : %s no space in "
519 __PRETTY_FUNCTION__);
523 serial_printf("ERROR : %s problem with "
525 __PRETTY_FUNCTION__);
529 serial_printf("ERROR : %s with nothing to do\n",
530 __PRETTY_FUNCTION__);
534 static void musb_peri_ep0_tx(void)
537 int transfer_size = 0;
540 csr0 = readw(&musbr->ep[0].ep0.csr0);
542 /* Check for pending tx */
543 if (csr0 & MUSB_CSR0_TXPKTRDY)
546 /* Check if this is the last packet sent */
547 if (ep0_endpoint->sent >= ep0_urb->actual_length) {
552 transfer_size = ep0_urb->actual_length - ep0_endpoint->sent;
553 /* Is the transfer size negative ? */
554 if (transfer_size <= 0) {
556 serial_printf("ERROR : %s problem with the"
557 " transfer size %d\n",
564 /* Truncate large transfers to the fifo size */
565 if (transfer_size > ep0_endpoint->tx_packetSize)
566 transfer_size = ep0_endpoint->tx_packetSize;
568 write_fifo(0, transfer_size, &ep0_urb->buffer[ep0_endpoint->sent]);
569 ep0_endpoint->sent += transfer_size;
571 /* Done or more to send ? */
572 if (ep0_endpoint->sent >= ep0_urb->actual_length)
573 musb_ep0_tx_ready_and_last();
579 for (p = 0; p < pm; p++) {
580 csr0 = readw(&musbr->ep[0].ep0.csr0);
581 if (!(csr0 & MUSB_CSR0_TXPKTRDY))
584 /* Double the delay. */
588 if ((ep0_endpoint->sent >= ep0_urb->actual_length) && (p < pm))
595 static void musb_peri_ep0(void)
599 if (SET_ADDRESS == ep0_state)
602 csr0 = readw(&musbr->ep[0].ep0.csr0);
604 /* Error conditions */
605 if (MUSB_CSR0_P_SENTSTALL & csr0) {
606 csr0 &= ~MUSB_CSR0_P_SENTSTALL;
607 writew(csr0, &musbr->ep[0].ep0.csr0);
610 if (MUSB_CSR0_P_SETUPEND & csr0) {
611 csr0 |= MUSB_CSR0_P_SVDSETUPEND;
612 writew(csr0, &musbr->ep[0].ep0.csr0);
614 if ((debug_setup) && (debug_level > 1))
615 serial_printf("WARN: %s SETUPEND\n",
616 __PRETTY_FUNCTION__);
620 if (IDLE == ep0_state)
621 musb_peri_ep0_idle();
630 static void musb_peri_rx_ep(unsigned int ep)
633 u16 peri_rxcsr = readw(&musbr->ep[ep].epN.rxcsr);
635 if (!(peri_rxcsr & MUSB_RXCSR_RXPKTRDY)) {
637 serial_printf("ERROR : %s %d without MUSB_RXCSR_RXPKTRDY set\n",
638 __PRETTY_FUNCTION__, ep);
642 peri_rxcount = readw(&musbr->ep[ep].epN.rxcount);
644 struct usb_endpoint_instance *endpoint;
648 endpoint = GET_ENDPOINT(udc_device, ep);
649 if (endpoint && endpoint->rcv_urb) {
650 struct urb *urb = endpoint->rcv_urb;
651 unsigned int remaining_space = urb->buffer_length -
654 if (remaining_space) {
655 int urb_bad = 0; /* urb is good */
657 if (peri_rxcount > remaining_space)
658 length = remaining_space;
660 length = peri_rxcount;
662 data = (u8 *) urb->buffer_data;
663 data += urb->actual_length;
665 /* The common musb fifo reader */
666 read_fifo(ep, length, data);
668 if (length == peri_rxcount)
669 musb_peri_rx_ack(ep);
671 pending_intrrx |= (1 << ep);
674 * urb's actual_length is updated in
677 usbd_rcv_complete(endpoint, length, urb_bad);
681 serial_printf("ERROR : %s %d no space "
683 __PRETTY_FUNCTION__, ep);
685 pending_intrrx |= (1 << ep);
689 serial_printf("ERROR : %s %d problem with "
691 __PRETTY_FUNCTION__, ep);
693 pending_intrrx |= (1 << ep);
698 serial_printf("ERROR : %s %d with nothing to do\n",
699 __PRETTY_FUNCTION__, ep);
701 musb_peri_rx_ack(ep);
705 static void musb_peri_rx(u16 intr)
709 /* First bit is reserved and does not indicate interrupt for EP0 */
711 for (ep = 1; ep < 16; ep++) {
712 if ((1 << ep) & intr)
717 static void musb_peri_tx(u16 intr)
721 /* Check for EP0: first bit indicates interrupt for both RX and TX */
725 for (ep = 1; ep < 16; ep++) {
726 if ((1 << ep) & intr)
727 udc_endpoint_write(GET_ENDPOINT(udc_device, ep));
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);
750 if (MUSB_INTR_RESET & intrusb) {
751 usbd_device_event_irq(udc_device, DEVICE_RESET, 0);
755 if (MUSB_INTR_DISCONNECT & intrusb) {
756 /* cable unplugged from hub/host */
757 usbd_device_event_irq(udc_device, DEVICE_RESET, 0);
759 usbd_device_event_irq(udc_device, DEVICE_HUB_RESET, 0);
762 if (MUSB_INTR_SOF & intrusb) {
763 usbd_device_event_irq(udc_device,
764 DEVICE_BUS_ACTIVITY, 0);
768 if (MUSB_INTR_SUSPEND & intrusb) {
769 usbd_device_event_irq(udc_device,
770 DEVICE_BUS_INACTIVE, 0);
773 if (ep0_state != SET_ADDRESS) {
776 intrrx = readw(&musbr->intrrx);
777 intrtx = readw(&musbr->intrtx);
779 intrrx |= pending_intrrx;
783 musb_peri_rx(intrrx);
786 musb_peri_tx(intrtx);
788 if (readw(&musbr->intrtx) & 0x1) {
790 faddr = readb(&musbr->faddr);
792 * Setting of the address can fail.
793 * Normally it succeeds the second time.
795 if (udc_device->address != faddr)
796 musb_peri_ep0_set_address();
802 void udc_set_nak(int ep_num)
807 void udc_unset_nak(int ep_num)
812 int udc_endpoint_write(struct usb_endpoint_instance *endpoint)
816 /* Transmit only if the hardware is available */
817 if (endpoint->tx_urb && endpoint->state == 0) {
818 unsigned int ep = endpoint->endpoint_address &
819 USB_ENDPOINT_NUMBER_MASK;
821 u16 peri_txcsr = readw(&musbr->ep[ep].epN.txcsr);
823 /* Error conditions */
824 if (peri_txcsr & MUSB_TXCSR_P_UNDERRUN) {
825 peri_txcsr &= ~MUSB_TXCSR_P_UNDERRUN;
826 writew(peri_txcsr, &musbr->ep[ep].epN.txcsr);
830 musb_print_txcsr(peri_txcsr);
832 /* Check if a packet is waiting to be sent */
833 if (!(peri_txcsr & MUSB_TXCSR_TXPKTRDY)) {
836 struct urb *urb = endpoint->tx_urb;
837 unsigned int remaining_packet = urb->actual_length -
840 if (endpoint->tx_packetSize < remaining_packet)
841 length = endpoint->tx_packetSize;
843 length = remaining_packet;
845 data = (u8 *) urb->buffer;
846 data += endpoint->sent;
848 /* common musb fifo function */
849 write_fifo(ep, length, data);
851 musb_peri_tx_ready(ep);
853 endpoint->last = length;
854 /* usbd_tx_complete will take care of updating 'sent' */
855 usbd_tx_complete(endpoint);
859 serial_printf("ERROR : %s Problem with urb %p "
862 endpoint->tx_urb, endpoint->state);
868 void udc_setup_ep(struct usb_device_instance *device, unsigned int id,
869 struct usb_endpoint_instance *endpoint)
873 ep0_endpoint = endpoint;
874 ep0_endpoint->endpoint_address = 0xff;
875 ep0_urb = usbd_alloc_urb(device, endpoint);
876 } else if (MAX_ENDPOINT >= id) {
877 epinfo[(id * 2) + 0].epsize = endpoint->rcv_packetSize;
878 epinfo[(id * 2) + 1].epsize = endpoint->tx_packetSize;
879 musb_configure_ep(&epinfo[0], ARRAY_SIZE(epinfo));
882 serial_printf("ERROR : %s endpoint request %d "
883 "exceeds maximum %d\n",
884 __PRETTY_FUNCTION__, id, MAX_ENDPOINT);
888 void udc_connect(void)
893 void udc_disconnect(void)
898 void udc_enable(struct usb_device_instance *device)
900 /* Save the device structure pointer */
906 void udc_disable(void)
911 void udc_startup_events(struct usb_device_instance *device)
913 /* The DEVICE_INIT event puts the USB device in the state STATE_INIT. */
914 usbd_device_event_irq(device, DEVICE_INIT, 0);
917 * The DEVICE_CREATE event puts the USB device in the state
920 usbd_device_event_irq(device, DEVICE_CREATE, 0);
922 /* Resets the address to 0 */
923 usbd_device_event_irq(device, DEVICE_RESET, 0);
933 ret = musb_platform_init();
937 /* Configure all the endpoint FIFO's and start usb controller */
938 musbr = musb_cfg.regs;
940 /* Initialize the endpoints */
941 for (ep_loop = 0; ep_loop <= MAX_ENDPOINT * 2; ep_loop++) {
942 epinfo[ep_loop].epnum = (ep_loop / 2) + 1;
943 epinfo[ep_loop].epdir = ep_loop % 2; /* OUT, IN */
944 epinfo[ep_loop].epsize = 0;
947 musb_peri_softconnect();