1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2009 Wind River Systems, Inc.
4 * Tom Rix <Tom.Rix@windriver.com>
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>
16 * Tom Rix <Tom.Rix@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)
34 * Author: Diego Dompe (diego.dompe@ridgerun.com)
35 * Atin Malaviya (atin.malaviya@gmail.com)
37 * -------------------------------------------------------------------------
41 #include <usbdevice.h>
43 #include "../gadget/ep0.h"
44 #include "musb_core.h"
45 #if defined(CONFIG_USB_OMAP3)
47 #elif defined(CONFIG_USB_AM35X)
51 /* Define MUSB_DEBUG for debugging */
52 /* #define MUSB_DEBUG */
53 #include "musb_debug.h"
55 #define MAX_ENDPOINT 15
57 #define GET_ENDPOINT(dev,ep) \
58 (((struct usb_device_instance *)(dev))->bus->endpoint_array + ep)
60 #define SET_EP0_STATE(s) \
62 if ((0 <= (s)) && (SET_ADDRESS >= (s))) { \
63 if ((s) != ep0_state) { \
64 if ((debug_setup) && (debug_level > 1)) \
65 serial_printf("INFO : Changing state " \
66 "from %s to %s in %s at " \
68 ep0_state_strings[ep0_state],\
69 ep0_state_strings[s], \
70 __PRETTY_FUNCTION__, \
75 if (debug_level > 0) \
76 serial_printf("Error at %s %d with setting " \
77 "state %d is invalid\n", \
78 __PRETTY_FUNCTION__, __LINE__, s); \
82 /* static implies these initialized to 0 or NULL */
83 static int debug_setup;
84 static int debug_level;
85 static struct musb_epinfo epinfo[MAX_ENDPOINT * 2 + 2];
86 static enum ep0_state_enum {
92 static char *ep0_state_strings[4] = {
99 static struct urb *ep0_urb;
100 struct usb_endpoint_instance *ep0_endpoint;
101 static struct usb_device_instance *udc_device;
105 static void musb_db_regs(void)
110 b = readb(&musbr->faddr);
111 serial_printf("\tfaddr 0x%2.2x\n", b);
113 b = readb(&musbr->power);
116 w = readw(&musbr->ep[0].ep0.csr0);
119 b = readb(&musbr->devctl);
120 musb_print_devctl(b);
122 b = readb(&musbr->ep[0].ep0.configdata);
123 musb_print_config(b);
125 w = readw(&musbr->frame);
126 serial_printf("\tframe 0x%4.4x\n", w);
128 b = readb(&musbr->index);
129 serial_printf("\tindex 0x%2.2x\n", b);
131 w = readw(&musbr->ep[1].epN.rxmaxp);
132 musb_print_rxmaxp(w);
134 w = readw(&musbr->ep[1].epN.rxcsr);
137 w = readw(&musbr->ep[1].epN.txmaxp);
138 musb_print_txmaxp(w);
140 w = readw(&musbr->ep[1].epN.txcsr);
144 #define musb_db_regs()
145 #endif /* DEBUG_MUSB */
147 static void musb_peri_softconnect(void)
152 power = readb(&musbr->power);
153 power &= ~MUSB_POWER_SOFTCONN;
154 writeb(power, &musbr->power);
156 /* Read intr to clear */
157 readb(&musbr->intrusb);
158 readw(&musbr->intrrx);
159 readw(&musbr->intrtx);
161 udelay(1000 * 1000); /* 1 sec */
164 power = readb(&musbr->power);
165 power |= MUSB_POWER_SOFTCONN;
167 * The usb device interface is usb 1.1
168 * Disable 2.0 high speed by clearring the hsenable bit.
170 power &= ~MUSB_POWER_HSENAB;
171 writeb(power, &musbr->power);
173 /* Check if device is in b-peripheral mode */
174 devctl = readb(&musbr->devctl);
175 if (!(devctl & MUSB_DEVCTL_BDEVICE) ||
176 (devctl & MUSB_DEVCTL_HM)) {
177 serial_printf("ERROR : Unsupport USB mode\n");
178 serial_printf("Check that mini-B USB cable is attached "
182 if (debug_setup && (debug_level > 1))
186 static void musb_peri_reset(void)
188 if ((debug_setup) && (debug_level > 1))
189 serial_printf("INFO : %s reset\n", __PRETTY_FUNCTION__);
192 ep0_endpoint->endpoint_address = 0xff;
194 /* Sync sw and hw addresses */
195 writeb(udc_device->address, &musbr->faddr);
200 static void musb_peri_resume(void)
205 static void musb_peri_ep0_stall(void)
209 csr0 = readw(&musbr->ep[0].ep0.csr0);
210 csr0 |= MUSB_CSR0_P_SENDSTALL;
211 writew(csr0, &musbr->ep[0].ep0.csr0);
212 if ((debug_setup) && (debug_level > 1))
213 serial_printf("INFO : %s stall\n", __PRETTY_FUNCTION__);
216 static void musb_peri_ep0_ack_req(void)
220 csr0 = readw(&musbr->ep[0].ep0.csr0);
221 csr0 |= MUSB_CSR0_P_SVDRXPKTRDY;
222 writew(csr0, &musbr->ep[0].ep0.csr0);
225 static void musb_ep0_tx_ready(void)
229 csr0 = readw(&musbr->ep[0].ep0.csr0);
230 csr0 |= MUSB_CSR0_TXPKTRDY;
231 writew(csr0, &musbr->ep[0].ep0.csr0);
234 static void musb_ep0_tx_ready_and_last(void)
238 csr0 = readw(&musbr->ep[0].ep0.csr0);
239 csr0 |= (MUSB_CSR0_TXPKTRDY | MUSB_CSR0_P_DATAEND);
240 writew(csr0, &musbr->ep[0].ep0.csr0);
243 static void musb_peri_ep0_last(void)
247 csr0 = readw(&musbr->ep[0].ep0.csr0);
248 csr0 |= MUSB_CSR0_P_DATAEND;
249 writew(csr0, &musbr->ep[0].ep0.csr0);
252 static void musb_peri_ep0_set_address(void)
255 writeb(udc_device->address, &musbr->faddr);
258 faddr = readb(&musbr->faddr);
259 if (udc_device->address == faddr) {
261 usbd_device_event_irq(udc_device, DEVICE_ADDRESS_ASSIGNED, 0);
262 if ((debug_setup) && (debug_level > 1))
263 serial_printf("INFO : %s Address set to %d\n",
264 __PRETTY_FUNCTION__, udc_device->address);
267 serial_printf("ERROR : %s Address missmatch "
270 udc_device->address, faddr);
274 static void musb_peri_rx_ack(unsigned int ep)
278 peri_rxcsr = readw(&musbr->ep[ep].epN.rxcsr);
279 peri_rxcsr &= ~MUSB_RXCSR_RXPKTRDY;
280 writew(peri_rxcsr, &musbr->ep[ep].epN.rxcsr);
283 static void musb_peri_tx_ready(unsigned int ep)
287 peri_txcsr = readw(&musbr->ep[ep].epN.txcsr);
288 peri_txcsr |= MUSB_TXCSR_TXPKTRDY;
289 writew(peri_txcsr, &musbr->ep[ep].epN.txcsr);
292 static void musb_peri_ep0_zero_data_request(int err)
294 musb_peri_ep0_ack_req();
297 musb_peri_ep0_stall();
301 musb_peri_ep0_last();
304 switch (ep0_urb->device_request.bRequest) {
305 case USB_REQ_SET_ADDRESS:
306 if ((debug_setup) && (debug_level > 1))
307 serial_printf("INFO : %s received set "
308 "address\n", __PRETTY_FUNCTION__);
311 case USB_REQ_SET_CONFIGURATION:
312 if ((debug_setup) && (debug_level > 1))
313 serial_printf("INFO : %s Configured\n",
314 __PRETTY_FUNCTION__);
315 usbd_device_event_irq(udc_device, DEVICE_CONFIGURED, 0);
320 if (USB_REQ_SET_ADDRESS == ep0_urb->device_request.bRequest) {
321 SET_EP0_STATE(SET_ADDRESS);
328 static void musb_peri_ep0_rx_data_request(void)
331 * This is the completion of the data OUT / RX
333 * Host is sending data to ep0 that is not
334 * part of setup. This comes from the cdc_recv_setup
335 * op that is device specific.
338 musb_peri_ep0_ack_req();
340 ep0_endpoint->rcv_urb = ep0_urb;
341 ep0_urb->actual_length = 0;
345 static void musb_peri_ep0_tx_data_request(int err)
348 musb_peri_ep0_stall();
351 musb_peri_ep0_ack_req();
353 ep0_endpoint->tx_urb = ep0_urb;
354 ep0_endpoint->sent = 0;
359 static void musb_peri_ep0_idle(void)
367 * A lot of confusion can be caused if the address
368 * in software, udc layer, does not agree with the
369 * hardware. Since the setting of the hardware address
370 * must be set after the set address request, the
371 * usb state machine is out of sync for a few frame.
372 * It is a good idea to run this check when changes
373 * are made to the state machine.
375 if ((debug_level > 0) &&
376 (ep0_state != SET_ADDRESS)) {
379 faddr = readb(&musbr->faddr);
380 if (udc_device->address != faddr) {
381 serial_printf("ERROR : %s addresses do not"
382 "match sw %d vs hw %d\n",
384 udc_device->address, faddr);
390 csr0 = readw(&musbr->ep[0].ep0.csr0);
392 if (!(MUSB_CSR0_RXPKTRDY & csr0))
395 count0 = readw(&musbr->ep[0].ep0.count0);
400 if ((debug_setup) && (debug_level > 1))
401 serial_printf("WARN : %s SETUP incorrect size %d\n",
402 __PRETTY_FUNCTION__, count0);
403 musb_peri_ep0_stall();
407 read_fifo(0, count0, &ep0_urb->device_request);
410 print_usb_device_request(&ep0_urb->device_request);
412 if (ep0_urb->device_request.wLength == 0) {
413 err = ep0_recv_setup(ep0_urb);
415 /* Zero data request */
416 musb_peri_ep0_zero_data_request(err);
418 /* Is data coming or going ? */
419 u8 reqType = ep0_urb->device_request.bmRequestType;
421 if (USB_REQ_DEVICE2HOST == (reqType & USB_REQ_DIRECTION_MASK)) {
422 err = ep0_recv_setup(ep0_urb);
424 musb_peri_ep0_tx_data_request(err);
429 * The RX routine will call ep0_recv_setup
430 * when the data packet has arrived.
432 musb_peri_ep0_rx_data_request();
440 static void musb_peri_ep0_rx(void)
443 * This is the completion of the data OUT / RX
445 * Host is sending data to ep0 that is not
446 * part of setup. This comes from the cdc_recv_setup
447 * op that is device specific.
449 * Pass the data back to driver ep0_recv_setup which
450 * should give the cdc_recv_setup the chance to handle
456 if (debug_level > 3) {
457 if (0 != ep0_urb->actual_length) {
458 serial_printf("%s finished ? %d of %d\n",
460 ep0_urb->actual_length,
461 ep0_urb->device_request.wLength);
465 if (ep0_urb->device_request.wLength == ep0_urb->actual_length) {
466 musb_peri_ep0_last();
468 ep0_recv_setup(ep0_urb);
472 csr0 = readw(&musbr->ep[0].ep0.csr0);
473 if (!(MUSB_CSR0_RXPKTRDY & csr0))
476 count0 = readw(&musbr->ep[0].ep0.count0);
479 struct usb_endpoint_instance *endpoint;
483 endpoint = ep0_endpoint;
484 if (endpoint && endpoint->rcv_urb) {
485 struct urb *urb = endpoint->rcv_urb;
486 unsigned int remaining_space = urb->buffer_length -
489 if (remaining_space) {
490 int urb_bad = 0; /* urb is good */
492 if (count0 > remaining_space)
493 length = remaining_space;
497 data = (u8 *) urb->buffer_data;
498 data += urb->actual_length;
500 /* The common musb fifo reader */
501 read_fifo(0, length, data);
503 musb_peri_ep0_ack_req();
506 * urb's actual_length is updated in
509 usbd_rcv_complete(endpoint, length, urb_bad);
513 serial_printf("ERROR : %s no space in "
515 __PRETTY_FUNCTION__);
519 serial_printf("ERROR : %s problem with "
521 __PRETTY_FUNCTION__);
525 serial_printf("ERROR : %s with nothing to do\n",
526 __PRETTY_FUNCTION__);
530 static void musb_peri_ep0_tx(void)
533 int transfer_size = 0;
536 csr0 = readw(&musbr->ep[0].ep0.csr0);
538 /* Check for pending tx */
539 if (csr0 & MUSB_CSR0_TXPKTRDY)
542 /* Check if this is the last packet sent */
543 if (ep0_endpoint->sent >= ep0_urb->actual_length) {
548 transfer_size = ep0_urb->actual_length - ep0_endpoint->sent;
549 /* Is the transfer size negative ? */
550 if (transfer_size <= 0) {
552 serial_printf("ERROR : %s problem with the"
553 " transfer size %d\n",
560 /* Truncate large transfers to the fifo size */
561 if (transfer_size > ep0_endpoint->tx_packetSize)
562 transfer_size = ep0_endpoint->tx_packetSize;
564 write_fifo(0, transfer_size, &ep0_urb->buffer[ep0_endpoint->sent]);
565 ep0_endpoint->sent += transfer_size;
567 /* Done or more to send ? */
568 if (ep0_endpoint->sent >= ep0_urb->actual_length)
569 musb_ep0_tx_ready_and_last();
575 for (p = 0; p < pm; p++) {
576 csr0 = readw(&musbr->ep[0].ep0.csr0);
577 if (!(csr0 & MUSB_CSR0_TXPKTRDY))
580 /* Double the delay. */
584 if ((ep0_endpoint->sent >= ep0_urb->actual_length) && (p < pm))
591 static void musb_peri_ep0(void)
595 if (SET_ADDRESS == ep0_state)
598 csr0 = readw(&musbr->ep[0].ep0.csr0);
600 /* Error conditions */
601 if (MUSB_CSR0_P_SENTSTALL & csr0) {
602 csr0 &= ~MUSB_CSR0_P_SENTSTALL;
603 writew(csr0, &musbr->ep[0].ep0.csr0);
606 if (MUSB_CSR0_P_SETUPEND & csr0) {
607 csr0 |= MUSB_CSR0_P_SVDSETUPEND;
608 writew(csr0, &musbr->ep[0].ep0.csr0);
610 if ((debug_setup) && (debug_level > 1))
611 serial_printf("WARN: %s SETUPEND\n",
612 __PRETTY_FUNCTION__);
616 if (IDLE == ep0_state)
617 musb_peri_ep0_idle();
626 static void musb_peri_rx_ep(unsigned int ep)
629 u8 peri_rxcsr = readw(&musbr->ep[ep].epN.rxcsr);
631 if (!(peri_rxcsr & MUSB_RXCSR_RXPKTRDY)) {
633 serial_printf("ERROR : %s %d without MUSB_RXCSR_RXPKTRDY set\n",
634 __PRETTY_FUNCTION__, ep);
638 peri_rxcount = readw(&musbr->ep[ep].epN.rxcount);
640 struct usb_endpoint_instance *endpoint;
644 endpoint = GET_ENDPOINT(udc_device, ep);
645 if (endpoint && endpoint->rcv_urb) {
646 struct urb *urb = endpoint->rcv_urb;
647 unsigned int remaining_space = urb->buffer_length -
650 if (remaining_space) {
651 int urb_bad = 0; /* urb is good */
653 if (peri_rxcount > remaining_space)
654 length = remaining_space;
656 length = peri_rxcount;
658 data = (u8 *) urb->buffer_data;
659 data += urb->actual_length;
661 /* The common musb fifo reader */
662 read_fifo(ep, length, data);
664 musb_peri_rx_ack(ep);
667 * urb's actual_length is updated in
670 usbd_rcv_complete(endpoint, length, urb_bad);
674 serial_printf("ERROR : %s %d no space "
676 __PRETTY_FUNCTION__, ep);
680 serial_printf("ERROR : %s %d problem with "
682 __PRETTY_FUNCTION__, ep);
687 serial_printf("ERROR : %s %d with nothing to do\n",
688 __PRETTY_FUNCTION__, ep);
692 static void musb_peri_rx(u16 intr)
700 for (ep = 1; ep < 16; ep++) {
701 if ((1 << ep) & intr)
706 static void musb_peri_tx(u16 intr)
713 * Use this in the future when handling epN tx
717 * for (ep = 1; ep < 16; ep++) {
718 * if ((1 << ep) & intr) {
719 * / * handle tx for this endpoint * /
727 /* This is a high freq called function */
731 intrusb = readb(&musbr->intrusb);
734 * See drivers/usb/gadget/mpc8xx_udc.c for
735 * state diagram going from detached through
738 if (MUSB_INTR_RESUME & intrusb) {
739 usbd_device_event_irq(udc_device,
740 DEVICE_BUS_ACTIVITY, 0);
746 if (MUSB_INTR_RESET & intrusb) {
747 usbd_device_event_irq(udc_device, DEVICE_RESET, 0);
751 if (MUSB_INTR_DISCONNECT & intrusb) {
752 /* cable unplugged from hub/host */
753 usbd_device_event_irq(udc_device, DEVICE_RESET, 0);
755 usbd_device_event_irq(udc_device, DEVICE_HUB_RESET, 0);
758 if (MUSB_INTR_SOF & intrusb) {
759 usbd_device_event_irq(udc_device,
760 DEVICE_BUS_ACTIVITY, 0);
764 if (MUSB_INTR_SUSPEND & intrusb) {
765 usbd_device_event_irq(udc_device,
766 DEVICE_BUS_INACTIVE, 0);
769 if (ep0_state != SET_ADDRESS) {
772 intrrx = readw(&musbr->intrrx);
773 intrtx = readw(&musbr->intrtx);
776 musb_peri_rx(intrrx);
779 musb_peri_tx(intrtx);
781 if (MUSB_INTR_SOF & intrusb) {
783 faddr = readb(&musbr->faddr);
785 * Setting of the address can fail.
786 * Normally it succeeds the second time.
788 if (udc_device->address != faddr)
789 musb_peri_ep0_set_address();
795 void udc_set_nak(int ep_num)
800 void udc_unset_nak(int ep_num)
805 int udc_endpoint_write(struct usb_endpoint_instance *endpoint)
809 /* Transmit only if the hardware is available */
810 if (endpoint->tx_urb && endpoint->state == 0) {
811 unsigned int ep = endpoint->endpoint_address &
812 USB_ENDPOINT_NUMBER_MASK;
814 u16 peri_txcsr = readw(&musbr->ep[ep].epN.txcsr);
816 /* Error conditions */
817 if (peri_txcsr & MUSB_TXCSR_P_UNDERRUN) {
818 peri_txcsr &= ~MUSB_TXCSR_P_UNDERRUN;
819 writew(peri_txcsr, &musbr->ep[ep].epN.txcsr);
823 musb_print_txcsr(peri_txcsr);
825 /* Check if a packet is waiting to be sent */
826 if (!(peri_txcsr & MUSB_TXCSR_TXPKTRDY)) {
829 struct urb *urb = endpoint->tx_urb;
830 unsigned int remaining_packet = urb->actual_length -
833 if (endpoint->tx_packetSize < remaining_packet)
834 length = endpoint->tx_packetSize;
836 length = remaining_packet;
838 data = (u8 *) urb->buffer;
839 data += endpoint->sent;
841 /* common musb fifo function */
842 write_fifo(ep, length, data);
844 musb_peri_tx_ready(ep);
846 endpoint->last = length;
847 /* usbd_tx_complete will take care of updating 'sent' */
848 usbd_tx_complete(endpoint);
852 serial_printf("ERROR : %s Problem with urb %p "
855 endpoint->tx_urb, endpoint->state);
861 void udc_setup_ep(struct usb_device_instance *device, unsigned int id,
862 struct usb_endpoint_instance *endpoint)
866 ep0_endpoint = endpoint;
867 ep0_endpoint->endpoint_address = 0xff;
868 ep0_urb = usbd_alloc_urb(device, endpoint);
869 } else if (MAX_ENDPOINT >= id) {
872 /* Check the direction */
873 ep_addr = endpoint->endpoint_address;
874 if (USB_DIR_IN == (ep_addr & USB_ENDPOINT_DIR_MASK)) {
876 epinfo[(id * 2) + 1].epsize = endpoint->tx_packetSize;
879 epinfo[id * 2].epsize = endpoint->rcv_packetSize;
882 musb_configure_ep(&epinfo[0], ARRAY_SIZE(epinfo));
885 serial_printf("ERROR : %s endpoint request %d "
886 "exceeds maximum %d\n",
887 __PRETTY_FUNCTION__, id, MAX_ENDPOINT);
891 void udc_connect(void)
896 void udc_disconnect(void)
901 void udc_enable(struct usb_device_instance *device)
903 /* Save the device structure pointer */
909 void udc_disable(void)
914 void udc_startup_events(struct usb_device_instance *device)
916 /* The DEVICE_INIT event puts the USB device in the state STATE_INIT. */
917 usbd_device_event_irq(device, DEVICE_INIT, 0);
920 * The DEVICE_CREATE event puts the USB device in the state
923 usbd_device_event_irq(device, DEVICE_CREATE, 0);
925 /* Resets the address to 0 */
926 usbd_device_event_irq(device, DEVICE_RESET, 0);
936 ret = musb_platform_init();
940 /* Configure all the endpoint FIFO's and start usb controller */
941 musbr = musb_cfg.regs;
943 /* Initialize the endpoints */
944 for (ep_loop = 0; ep_loop <= MAX_ENDPOINT * 2; ep_loop++) {
945 epinfo[ep_loop].epnum = (ep_loop / 2) + 1;
946 epinfo[ep_loop].epdir = ep_loop % 2; /* OUT, IN */
947 epinfo[ep_loop].epsize = 0;
950 musb_peri_softconnect();