3 * Gerry Hamel, geh@ti.com, Texas Instruments
6 * linux/drivers/usb/device/bi/omap.c
7 * TI OMAP1510 USB bus interface driver
9 * Author: MontaVista Software, Inc.
13 * SPDX-License-Identifier: GPL-2.0+
18 #include <usbdevice.h>
19 #include <usb/omap1510_udc.h>
25 #define UDC_INIT_MDELAY 80 /* Device settle delay */
26 #define UDC_MAX_ENDPOINTS 31 /* Number of endpoints on this UDC */
28 /* Some kind of debugging output... */
31 #define UDCDBGA(fmt,args...)
32 #else /* The bugs still exists... */
33 #define UDCDBG(str) serial_printf("[%s] %s:%d: " str "\n", __FILE__,__FUNCTION__,__LINE__)
34 #define UDCDBGA(fmt,args...) serial_printf("[%s] %s:%d: " fmt "\n", __FILE__,__FUNCTION__,__LINE__, ##args)
40 #else /* The bugs still exists... */
41 #define UDCREG(name) serial_printf("%s():%d: %s[%08x]=%.4x\n",__FUNCTION__,__LINE__, (#name), name, inw(name)) /* For 16-bit regs */
42 #define UDCREGL(name) serial_printf("%s():%d: %s[%08x]=%.8x\n",__FUNCTION__,__LINE__, (#name), name, inl(name)) /* For 32-bit regs */
46 static struct urb *ep0_urb = NULL;
48 static struct usb_device_instance *udc_device; /* Used in interrupt handler */
49 static u16 udc_devstat = 0; /* UDC status (DEVSTAT) */
50 static u32 udc_interrupts = 0;
52 static void udc_stall_ep (unsigned int ep_addr);
55 static struct usb_endpoint_instance *omap1510_find_ep (int ep)
59 for (i = 0; i < udc_device->bus->max_endpoints; i++) {
60 if (udc_device->bus->endpoint_array[i].endpoint_address == ep)
61 return &udc_device->bus->endpoint_array[i];
66 /* ************************************************************************** */
71 * omap1510_prepare_endpoint_for_rx
73 * This function implements TRM Figure 14-11.
75 * The endpoint to prepare for transfer is specified as a physical endpoint
76 * number. For OUT (rx) endpoints 1 through 15, the corresponding endpoint
77 * configuration register is checked to see if the endpoint is ISO or not.
78 * If the OUT endpoint is valid and is non-ISO then its FIFO is enabled.
79 * No action is taken for endpoint 0 or for IN (tx) endpoints 16 through 30.
81 static void omap1510_prepare_endpoint_for_rx (int ep_addr)
83 int ep_num = ep_addr & USB_ENDPOINT_NUMBER_MASK;
85 UDCDBGA ("omap1510_prepare_endpoint %x", ep_addr);
86 if (((ep_addr & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT)) {
87 if ((inw (UDC_EP_RX (ep_num)) &
88 (UDC_EPn_RX_Valid | UDC_EPn_RX_Iso)) ==
90 /* rx endpoint is valid, non-ISO, so enable its FIFO */
91 outw (UDC_EP_Sel | ep_num, UDC_EP_NUM);
92 outw (UDC_Set_FIFO_En, UDC_CTRL);
98 /* omap1510_configure_endpoints
100 * This function implements TRM Figure 14-10.
102 static void omap1510_configure_endpoints (struct usb_device_instance *device)
105 struct usb_bus_instance *bus;
106 struct usb_endpoint_instance *endpoint;
107 unsigned short ep_ptr;
108 unsigned short ep_size;
109 unsigned short ep_isoc;
110 unsigned short ep_doublebuffer;
118 /* There is a dedicated 2048 byte buffer for USB packets that may be
119 * arbitrarily partitioned among the endpoints on 8-byte boundaries.
120 * The first 8 bytes are reserved for receiving setup packets on
123 ep_ptr = 8; /* reserve the first 8 bytes for the setup fifo */
125 for (ep = 0; ep < bus->max_endpoints; ep++) {
126 endpoint = bus->endpoint_array + ep;
127 ep_addr = endpoint->endpoint_address;
128 if ((ep_addr & USB_ENDPOINT_DIR_MASK) == USB_DIR_IN) {
130 packet_size = endpoint->tx_packetSize;
131 attributes = endpoint->tx_attributes;
134 packet_size = endpoint->rcv_packetSize;
135 attributes = endpoint->rcv_attributes;
138 switch (packet_size) {
164 UDCDBGA ("ep 0x%02x has bad packet size %d",
165 ep_addr, packet_size);
171 switch (attributes & USB_ENDPOINT_XFERTYPE_MASK) {
172 case USB_ENDPOINT_XFER_CONTROL:
173 case USB_ENDPOINT_XFER_BULK:
174 case USB_ENDPOINT_XFER_INT:
176 /* A non-isochronous endpoint may optionally be
177 * double-buffered. For now we disable
182 if (packet_size > 64)
184 if (!ep || !ep_doublebuffer)
185 buffer_size = packet_size;
187 buffer_size = packet_size * 2;
189 case USB_ENDPOINT_XFER_ISOC:
190 /* Isochronous endpoints are always double-
191 * buffered, but the double-buffering bit
192 * in the endpoint configuration register
193 * becomes the msb of the endpoint size so we
194 * set the double-buffering flag to zero.
198 buffer_size = packet_size * 2;
202 /* check to see if our packet buffer RAM is exhausted */
203 if ((ep_ptr + buffer_size) > 2048) {
204 UDCDBGA ("out of packet RAM for ep 0x%02x buf size %d", ep_addr, buffer_size);
205 buffer_size = packet_size = 0;
208 /* force a default configuration for endpoint 0 since it is
211 if (!ep && ((packet_size < 8) || (packet_size > 64))) {
212 buffer_size = packet_size = 64;
217 /* configure endpoint 0 */
218 outw ((ep_size << 12) | (ep_ptr >> 3), UDC_EP0);
219 /*UDCDBGA("ep 0 buffer offset 0x%03x packet size 0x%03x", */
220 /* ep_ptr, packet_size); */
221 } else if ((ep_addr & USB_ENDPOINT_DIR_MASK) == USB_DIR_IN) {
224 outw ((1 << 15) | (ep_doublebuffer << 14) |
225 (ep_size << 12) | (ep_isoc << 11) |
228 USB_ENDPOINT_NUMBER_MASK));
229 UDCDBGA ("IN ep %d buffer offset 0x%03x"
230 " packet size 0x%03x",
231 ep_addr & USB_ENDPOINT_NUMBER_MASK,
232 ep_ptr, packet_size);
236 USB_ENDPOINT_NUMBER_MASK));
241 outw ((1 << 15) | (ep_doublebuffer << 14) |
242 (ep_size << 12) | (ep_isoc << 11) |
245 USB_ENDPOINT_NUMBER_MASK));
246 UDCDBGA ("OUT ep %d buffer offset 0x%03x"
247 " packet size 0x%03x",
248 ep_addr & USB_ENDPOINT_NUMBER_MASK,
249 ep_ptr, packet_size);
253 USB_ENDPOINT_NUMBER_MASK));
256 ep_ptr += buffer_size;
260 /* omap1510_deconfigure_device
262 * This function balances omap1510_configure_device.
264 static void omap1510_deconfigure_device (void)
268 UDCDBG ("clear Cfg_Lock");
269 outw (inw (UDC_SYSCON1) & ~UDC_Cfg_Lock, UDC_SYSCON1);
270 UDCREG (UDC_SYSCON1);
272 /* deconfigure all endpoints */
273 for (epnum = 1; epnum <= 15; epnum++) {
274 outw (0, UDC_EP_RX (epnum));
275 outw (0, UDC_EP_TX (epnum));
279 /* omap1510_configure_device
281 * This function implements TRM Figure 14-9.
283 static void omap1510_configure_device (struct usb_device_instance *device)
285 omap1510_configure_endpoints (device);
288 /* Figure 14-9 indicates we should enable interrupts here, but we have
289 * other routines (udc_all_interrupts, udc_suspended_interrupts) to
293 UDCDBG ("set Cfg_Lock");
294 outw (inw (UDC_SYSCON1) | UDC_Cfg_Lock, UDC_SYSCON1);
295 UDCREG (UDC_SYSCON1);
298 /* omap1510_write_noniso_tx_fifo
300 * This function implements TRM Figure 14-30.
302 * If the endpoint has an active tx_urb, then the next packet of data from the
303 * URB is written to the tx FIFO. The total amount of data in the urb is given
304 * by urb->actual_length. The maximum amount of data that can be sent in any
305 * one packet is given by endpoint->tx_packetSize. The number of data bytes
306 * from this URB that have already been transmitted is given by endpoint->sent.
307 * endpoint->last is updated by this routine with the number of data bytes
308 * transmitted in this packet.
310 * In accordance with Figure 14-30, the EP_NUM register must already have been
311 * written with the value to select the appropriate tx FIFO before this routine
314 static void omap1510_write_noniso_tx_fifo (struct usb_endpoint_instance
317 struct urb *urb = endpoint->tx_urb;
320 unsigned int last, i;
322 UDCDBGA ("urb->buffer %p, buffer_length %d, actual_length %d",
323 urb->buffer, urb->buffer_length, urb->actual_length);
325 MIN (urb->actual_length - endpoint->sent,
326 endpoint->tx_packetSize))) {
327 u8 *cp = urb->buffer + endpoint->sent;
329 UDCDBGA ("endpoint->sent %d, tx_packetSize %d, last %d", endpoint->sent, endpoint->tx_packetSize, last);
331 if (((u32) cp & 1) == 0) { /* word aligned? */
332 outsw (UDC_DATA, cp, last >> 1);
333 } else { /* byte aligned. */
334 for (i = 0; i < (last >> 1); i++) {
335 u16 w = ((u16) cp[2 * i + 1] << 8) |
341 outb (*(cp + last - 1), UDC_DATA);
344 endpoint->last = last;
348 /* omap1510_read_noniso_rx_fifo
350 * This function implements TRM Figure 14-28.
352 * If the endpoint has an active rcv_urb, then the next packet of data is read
353 * from the rcv FIFO and written to rcv_urb->buffer at offset
354 * rcv_urb->actual_length to append the packet data to the data from any
355 * previous packets for this transfer. We assume that there is sufficient room
356 * left in the buffer to hold an entire packet of data.
358 * The return value is the number of bytes read from the FIFO for this packet.
360 * In accordance with Figure 14-28, the EP_NUM register must already have been
361 * written with the value to select the appropriate rcv FIFO before this routine
364 static int omap1510_read_noniso_rx_fifo (struct usb_endpoint_instance
367 struct urb *urb = endpoint->rcv_urb;
371 len = inw (UDC_RXFSTAT);
374 unsigned char *cp = urb->buffer + urb->actual_length;
376 insw (UDC_DATA, cp, len >> 1);
378 *(cp + len - 1) = inb (UDC_DATA);
384 /* omap1510_prepare_for_control_write_status
386 * This function implements TRM Figure 14-17.
388 * We have to deal here with non-autodecoded control writes that haven't already
389 * been dealt with by ep0_recv_setup. The non-autodecoded standard control
390 * write requests are: set/clear endpoint feature, set configuration, set
391 * interface, and set descriptor. ep0_recv_setup handles set/clear requests for
392 * ENDPOINT_HALT by halting the endpoint for a set request and resetting the
393 * endpoint for a clear request. ep0_recv_setup returns an error for
394 * SET_DESCRIPTOR requests which causes them to be terminated with a stall by
395 * the setup handler. A SET_INTERFACE request is handled by ep0_recv_setup by
396 * generating a DEVICE_SET_INTERFACE event. This leaves only the
397 * SET_CONFIGURATION event for us to deal with here.
400 static void omap1510_prepare_for_control_write_status (struct urb *urb)
402 struct usb_device_request *request = &urb->device_request;;
404 /* check for a SET_CONFIGURATION request */
405 if (request->bRequest == USB_REQ_SET_CONFIGURATION) {
406 int configuration = le16_to_cpu (request->wValue) & 0xff;
407 unsigned short devstat = inw (UDC_DEVSTAT);
409 if ((devstat & (UDC_ADD | UDC_CFG)) == UDC_ADD) {
410 /* device is currently in ADDRESSED state */
412 /* Assume the specified non-zero configuration
413 * value is valid and switch to the CONFIGURED
416 outw (UDC_Dev_Cfg, UDC_SYSCON2);
418 } else if ((devstat & UDC_CFG) == UDC_CFG) {
419 /* device is currently in CONFIGURED state */
420 if (!configuration) {
421 /* Switch to ADDRESSED state. */
422 outw (UDC_Clr_Cfg, UDC_SYSCON2);
427 /* select EP0 tx FIFO */
428 outw (UDC_EP_Dir | UDC_EP_Sel, UDC_EP_NUM);
429 /* clear endpoint (no data bytes in status stage) */
430 outw (UDC_Clr_EP, UDC_CTRL);
431 /* enable the EP0 tx FIFO */
432 outw (UDC_Set_FIFO_En, UDC_CTRL);
433 /* deselect the endpoint */
434 outw (UDC_EP_Dir, UDC_EP_NUM);
437 /* udc_state_transition_up
438 * udc_state_transition_down
440 * Helper functions to implement device state changes. The device states and
441 * the events that transition between them are:
446 * DEVICE_HUB_CONFIGURED DEVICE_HUB_RESET
452 * DEVICE_RESET DEVICE_POWER_INTERRUPTION
458 * DEVICE_ADDRESS_ASSIGNED DEVICE_RESET
464 * DEVICE_CONFIGURED DEVICE_DE_CONFIGURED
469 * udc_state_transition_up transitions up (in the direction from STATE_ATTACHED
470 * to STATE_CONFIGURED) from the specified initial state to the specified final
471 * state, passing through each intermediate state on the way. If the initial
472 * state is at or above (i.e. nearer to STATE_CONFIGURED) the final state, then
473 * no state transitions will take place.
475 * udc_state_transition_down transitions down (in the direction from
476 * STATE_CONFIGURED to STATE_ATTACHED) from the specified initial state to the
477 * specified final state, passing through each intermediate state on the way.
478 * If the initial state is at or below (i.e. nearer to STATE_ATTACHED) the final
479 * state, then no state transitions will take place.
481 * These functions must only be called with interrupts disabled.
483 static void udc_state_transition_up (usb_device_state_t initial,
484 usb_device_state_t final)
486 if (initial < final) {
489 usbd_device_event_irq (udc_device,
490 DEVICE_HUB_CONFIGURED, 0);
491 if (final == STATE_POWERED)
494 usbd_device_event_irq (udc_device, DEVICE_RESET, 0);
495 if (final == STATE_DEFAULT)
498 usbd_device_event_irq (udc_device,
499 DEVICE_ADDRESS_ASSIGNED, 0);
500 if (final == STATE_ADDRESSED)
502 case STATE_ADDRESSED:
503 usbd_device_event_irq (udc_device, DEVICE_CONFIGURED,
505 case STATE_CONFIGURED:
513 static void udc_state_transition_down (usb_device_state_t initial,
514 usb_device_state_t final)
516 if (initial > final) {
518 case STATE_CONFIGURED:
519 usbd_device_event_irq (udc_device, DEVICE_DE_CONFIGURED, 0);
520 if (final == STATE_ADDRESSED)
522 case STATE_ADDRESSED:
523 usbd_device_event_irq (udc_device, DEVICE_RESET, 0);
524 if (final == STATE_DEFAULT)
527 usbd_device_event_irq (udc_device, DEVICE_POWER_INTERRUPTION, 0);
528 if (final == STATE_POWERED)
531 usbd_device_event_irq (udc_device, DEVICE_HUB_RESET, 0);
540 /* Handle all device state changes.
541 * This function implements TRM Figure 14-21.
543 static void omap1510_udc_state_changed (void)
546 u16 devstat = inw (UDC_DEVSTAT);
548 UDCDBGA ("state changed, devstat %x, old %x", devstat, udc_devstat);
550 bits = devstat ^ udc_devstat;
552 if (bits & UDC_ATT) {
553 if (devstat & UDC_ATT) {
554 UDCDBG ("device attached and powered");
555 udc_state_transition_up (udc_device->device_state, STATE_POWERED);
557 UDCDBG ("device detached or unpowered");
558 udc_state_transition_down (udc_device->device_state, STATE_ATTACHED);
561 if (bits & UDC_USB_Reset) {
562 if (devstat & UDC_USB_Reset) {
563 UDCDBG ("device reset in progess");
564 udc_state_transition_down (udc_device->device_state, STATE_POWERED);
566 UDCDBG ("device reset completed");
569 if (bits & UDC_DEF) {
570 if (devstat & UDC_DEF) {
571 UDCDBG ("device entering default state");
572 udc_state_transition_up (udc_device->device_state, STATE_DEFAULT);
574 UDCDBG ("device leaving default state");
575 udc_state_transition_down (udc_device->device_state, STATE_POWERED);
578 if (bits & UDC_SUS) {
579 if (devstat & UDC_SUS) {
580 UDCDBG ("entering suspended state");
581 usbd_device_event_irq (udc_device, DEVICE_BUS_INACTIVE, 0);
583 UDCDBG ("leaving suspended state");
584 usbd_device_event_irq (udc_device, DEVICE_BUS_ACTIVITY, 0);
587 if (bits & UDC_R_WK_OK) {
588 UDCDBGA ("remote wakeup %s", (devstat & UDC_R_WK_OK)
589 ? "enabled" : "disabled");
591 if (bits & UDC_ADD) {
592 if (devstat & UDC_ADD) {
593 UDCDBG ("default -> addressed");
594 udc_state_transition_up (udc_device->device_state, STATE_ADDRESSED);
596 UDCDBG ("addressed -> default");
597 udc_state_transition_down (udc_device->device_state, STATE_DEFAULT);
600 if (bits & UDC_CFG) {
601 if (devstat & UDC_CFG) {
602 UDCDBG ("device configured");
603 /* The ep0_recv_setup function generates the
604 * DEVICE_CONFIGURED event when a
605 * USB_REQ_SET_CONFIGURATION setup packet is
606 * received, so we should already be in the
607 * state STATE_CONFIGURED.
609 udc_state_transition_up (udc_device->device_state, STATE_CONFIGURED);
611 UDCDBG ("device deconfigured");
612 udc_state_transition_down (udc_device->device_state, STATE_ADDRESSED);
617 /* Clear interrupt source */
618 outw (UDC_DS_Chg, UDC_IRQ_SRC);
620 /* Save current DEVSTAT */
621 udc_devstat = devstat;
624 /* Handle SETUP USB interrupt.
625 * This function implements TRM Figure 14-14.
627 static void omap1510_udc_setup (struct usb_endpoint_instance *endpoint)
629 UDCDBG ("-> Entering device setup");
632 const int setup_pktsize = 8;
633 unsigned char *datap =
634 (unsigned char *) &ep0_urb->device_request;
636 /* Gain access to EP 0 setup FIFO */
637 outw (UDC_Setup_Sel, UDC_EP_NUM);
639 /* Read control request data */
640 insb (UDC_DATA, datap, setup_pktsize);
642 UDCDBGA ("EP0 setup read [%x %x %x %x %x %x %x %x]",
643 *(datap + 0), *(datap + 1), *(datap + 2),
644 *(datap + 3), *(datap + 4), *(datap + 5),
645 *(datap + 6), *(datap + 7));
647 /* Reset EP0 setup FIFO */
648 outw (0, UDC_EP_NUM);
649 } while (inw (UDC_IRQ_SRC) & UDC_Setup);
651 /* Try to process setup packet */
652 if (ep0_recv_setup (ep0_urb)) {
653 /* Not a setup packet, stall next EP0 transaction */
655 UDCDBG ("can't parse setup packet, still waiting for setup");
659 /* Check direction */
660 if ((ep0_urb->device_request.bmRequestType & USB_REQ_DIRECTION_MASK)
661 == USB_REQ_HOST2DEVICE) {
662 UDCDBG ("control write on EP0");
663 if (le16_to_cpu (ep0_urb->device_request.wLength)) {
664 /* We don't support control write data stages.
665 * The only standard control write request with a data
666 * stage is SET_DESCRIPTOR, and ep0_recv_setup doesn't
667 * support that so we just stall those requests. A
668 * function driver might support a non-standard
669 * write request with a data stage, but it isn't
670 * obvious what we would do with the data if we read it
671 * so we'll just stall it. It seems like the API isn't
675 /* Here is what we would do if we did support control
678 ep0_urb->actual_length = 0;
679 outw (0, UDC_EP_NUM);
680 /* enable the EP0 rx FIFO */
681 outw (UDC_Set_FIFO_En, UDC_CTRL);
683 /* Stall this request */
684 UDCDBG ("Stalling unsupported EP0 control write data "
689 omap1510_prepare_for_control_write_status (ep0_urb);
692 UDCDBG ("control read on EP0");
693 /* The ep0_recv_setup function has already placed our response
694 * packet data in ep0_urb->buffer and the packet length in
695 * ep0_urb->actual_length.
697 endpoint->tx_urb = ep0_urb;
699 /* select the EP0 tx FIFO */
700 outw (UDC_EP_Dir | UDC_EP_Sel, UDC_EP_NUM);
701 /* Write packet data to the FIFO. omap1510_write_noniso_tx_fifo
702 * will update endpoint->last with the number of bytes written
705 omap1510_write_noniso_tx_fifo (endpoint);
706 /* enable the FIFO to start the packet transmission */
707 outw (UDC_Set_FIFO_En, UDC_CTRL);
708 /* deselect the EP0 tx FIFO */
709 outw (UDC_EP_Dir, UDC_EP_NUM);
712 UDCDBG ("<- Leaving device setup");
715 /* Handle endpoint 0 RX interrupt
716 * This routine implements TRM Figure 14-16.
718 static void omap1510_udc_ep0_rx (struct usb_endpoint_instance *endpoint)
720 unsigned short status;
722 UDCDBG ("RX on EP0");
723 /* select EP0 rx FIFO */
724 outw (UDC_EP_Sel, UDC_EP_NUM);
726 status = inw (UDC_STAT_FLG);
728 if (status & UDC_ACK) {
729 /* Check direction */
730 if ((ep0_urb->device_request.bmRequestType
731 & USB_REQ_DIRECTION_MASK) == USB_REQ_HOST2DEVICE) {
732 /* This rx interrupt must be for a control write data
735 * We don't support control write data stages.
736 * We should never end up here.
739 /* clear the EP0 rx FIFO */
740 outw (UDC_Clr_EP, UDC_CTRL);
742 /* deselect the EP0 rx FIFO */
743 outw (0, UDC_EP_NUM);
745 UDCDBG ("Stalling unexpected EP0 control write "
746 "data stage packet");
749 /* This rx interrupt must be for a control read status
752 UDCDBG ("ACK on EP0 control read status stage packet");
753 /* deselect EP0 rx FIFO */
754 outw (0, UDC_EP_NUM);
756 } else if (status & UDC_STALL) {
757 UDCDBG ("EP0 stall during RX");
758 /* deselect EP0 rx FIFO */
759 outw (0, UDC_EP_NUM);
761 /* deselect EP0 rx FIFO */
762 outw (0, UDC_EP_NUM);
766 /* Handle endpoint 0 TX interrupt
767 * This routine implements TRM Figure 14-18.
769 static void omap1510_udc_ep0_tx (struct usb_endpoint_instance *endpoint)
771 unsigned short status;
772 struct usb_device_request *request = &ep0_urb->device_request;
774 UDCDBG ("TX on EP0");
775 /* select EP0 TX FIFO */
776 outw (UDC_EP_Dir | UDC_EP_Sel, UDC_EP_NUM);
778 status = inw (UDC_STAT_FLG);
779 if (status & UDC_ACK) {
780 /* Check direction */
781 if ((request->bmRequestType & USB_REQ_DIRECTION_MASK) ==
782 USB_REQ_HOST2DEVICE) {
783 /* This tx interrupt must be for a control write status
786 UDCDBG ("ACK on EP0 control write status stage packet");
787 /* deselect EP0 TX FIFO */
788 outw (UDC_EP_Dir, UDC_EP_NUM);
790 /* This tx interrupt must be for a control read data
793 int wLength = le16_to_cpu (request->wLength);
795 /* Update our count of bytes sent so far in this
798 endpoint->sent += endpoint->last;
800 /* We are finished with this transfer if we have sent
801 * all of the bytes in our tx urb (urb->actual_length)
802 * unless we need a zero-length terminating packet. We
803 * need a zero-length terminating packet if we returned
804 * fewer bytes than were requested (wLength) by the host,
805 * and the number of bytes we returned is an exact
806 * multiple of the packet size endpoint->tx_packetSize.
808 if ((endpoint->sent == ep0_urb->actual_length)
809 && ((ep0_urb->actual_length == wLength)
810 || (endpoint->last !=
811 endpoint->tx_packetSize))) {
812 /* Done with control read data stage. */
813 UDCDBG ("control read data stage complete");
814 /* deselect EP0 TX FIFO */
815 outw (UDC_EP_Dir, UDC_EP_NUM);
816 /* select EP0 RX FIFO to prepare for control
819 outw (UDC_EP_Sel, UDC_EP_NUM);
820 /* clear the EP0 RX FIFO */
821 outw (UDC_Clr_EP, UDC_CTRL);
822 /* enable the EP0 RX FIFO */
823 outw (UDC_Set_FIFO_En, UDC_CTRL);
824 /* deselect the EP0 RX FIFO */
825 outw (0, UDC_EP_NUM);
827 /* We still have another packet of data to send
828 * in this control read data stage or else we
829 * need a zero-length terminating packet.
831 UDCDBG ("ACK control read data stage packet");
832 omap1510_write_noniso_tx_fifo (endpoint);
833 /* enable the EP0 tx FIFO to start transmission */
834 outw (UDC_Set_FIFO_En, UDC_CTRL);
835 /* deselect EP0 TX FIFO */
836 outw (UDC_EP_Dir, UDC_EP_NUM);
839 } else if (status & UDC_STALL) {
840 UDCDBG ("EP0 stall during TX");
841 /* deselect EP0 TX FIFO */
842 outw (UDC_EP_Dir, UDC_EP_NUM);
844 /* deselect EP0 TX FIFO */
845 outw (UDC_EP_Dir, UDC_EP_NUM);
849 /* Handle RX transaction on non-ISO endpoint.
850 * This function implements TRM Figure 14-27.
851 * The ep argument is a physical endpoint number for a non-ISO OUT endpoint
852 * in the range 1 to 15.
854 static void omap1510_udc_epn_rx (int ep)
856 unsigned short status;
858 /* Check endpoint status */
859 status = inw (UDC_STAT_FLG);
861 if (status & UDC_ACK) {
863 struct usb_endpoint_instance *endpoint =
864 omap1510_find_ep (ep);
866 nbytes = omap1510_read_noniso_rx_fifo (endpoint);
867 usbd_rcv_complete (endpoint, nbytes, 0);
869 /* enable rx FIFO to prepare for next packet */
870 outw (UDC_Set_FIFO_En, UDC_CTRL);
871 } else if (status & UDC_STALL) {
872 UDCDBGA ("STALL on RX endpoint %d", ep);
873 } else if (status & UDC_NAK) {
874 UDCDBGA ("NAK on RX ep %d", ep);
876 serial_printf ("omap-bi: RX on ep %d with status %x", ep,
881 /* Handle TX transaction on non-ISO endpoint.
882 * This function implements TRM Figure 14-29.
883 * The ep argument is a physical endpoint number for a non-ISO IN endpoint
884 * in the range 16 to 30.
886 static void omap1510_udc_epn_tx (int ep)
888 unsigned short status;
890 /*serial_printf("omap1510_udc_epn_tx( %x )\n",ep); */
892 /* Check endpoint status */
893 status = inw (UDC_STAT_FLG);
895 if (status & UDC_ACK) {
896 struct usb_endpoint_instance *endpoint =
897 omap1510_find_ep (ep);
899 /* We need to transmit a terminating zero-length packet now if
900 * we have sent all of the data in this URB and the transfer
901 * size was an exact multiple of the packet size.
904 && (endpoint->last == endpoint->tx_packetSize)
905 && (endpoint->tx_urb->actual_length - endpoint->sent -
906 endpoint->last == 0)) {
907 /* Prepare to transmit a zero-length packet. */
908 endpoint->sent += endpoint->last;
909 /* write 0 bytes of data to FIFO */
910 omap1510_write_noniso_tx_fifo (endpoint);
911 /* enable tx FIFO to start transmission */
912 outw (UDC_Set_FIFO_En, UDC_CTRL);
913 } else if (endpoint->tx_urb
914 && endpoint->tx_urb->actual_length) {
915 /* retire the data that was just sent */
916 usbd_tx_complete (endpoint);
917 /* Check to see if we have more data ready to transmit
921 && endpoint->tx_urb->actual_length) {
922 /* write data to FIFO */
923 omap1510_write_noniso_tx_fifo (endpoint);
924 /* enable tx FIFO to start transmission */
925 outw (UDC_Set_FIFO_En, UDC_CTRL);
928 } else if (status & UDC_STALL) {
929 UDCDBGA ("STALL on TX endpoint %d", ep);
930 } else if (status & UDC_NAK) {
931 UDCDBGA ("NAK on TX endpoint %d", ep);
933 /*serial_printf("omap-bi: TX on ep %d with status %x\n", ep, status); */
939 -------------------------------------------------------------------------------
942 /* Handle general USB interrupts and dispatch according to type.
943 * This function implements TRM Figure 14-13.
945 void omap1510_udc_irq (void)
947 u16 irq_src = inw (UDC_IRQ_SRC);
950 if (!(irq_src & ~UDC_SOF_Flg)) /* ignore SOF interrupts ) */
953 UDCDBGA ("< IRQ #%d start >- %x", udc_interrupts, irq_src);
954 /*serial_printf("< IRQ #%d start >- %x\n", udc_interrupts, irq_src); */
956 if (irq_src & UDC_DS_Chg) {
957 /* Device status changed */
958 omap1510_udc_state_changed ();
961 if (irq_src & UDC_EP0_RX) {
962 /* Endpoint 0 receive */
963 outw (UDC_EP0_RX, UDC_IRQ_SRC); /* ack interrupt */
964 omap1510_udc_ep0_rx (udc_device->bus->endpoint_array + 0);
967 if (irq_src & UDC_EP0_TX) {
968 /* Endpoint 0 transmit */
969 outw (UDC_EP0_TX, UDC_IRQ_SRC); /* ack interrupt */
970 omap1510_udc_ep0_tx (udc_device->bus->endpoint_array + 0);
973 if (irq_src & UDC_Setup) {
975 omap1510_udc_setup (udc_device->bus->endpoint_array + 0);
979 /* serial_printf("unknown interrupt, IRQ_SRC %.4x\n", irq_src); */
980 UDCDBGA ("< IRQ #%d end >", udc_interrupts);
984 /* This function implements TRM Figure 14-26. */
985 void omap1510_udc_noniso_irq (void)
987 unsigned short epnum;
988 unsigned short irq_src = inw (UDC_IRQ_SRC);
991 if (!(irq_src & (UDC_EPn_RX | UDC_EPn_TX)))
994 UDCDBGA ("non-ISO IRQ, IRQ_SRC %x", inw (UDC_IRQ_SRC));
996 if (irq_src & UDC_EPn_RX) { /* Endpoint N OUT transaction */
997 /* Determine the endpoint number for this interrupt */
998 epnum = (inw (UDC_EPN_STAT) & 0x0f00) >> 8;
999 UDCDBGA ("RX on ep %x", epnum);
1001 /* acknowledge interrupt */
1002 outw (UDC_EPn_RX, UDC_IRQ_SRC);
1005 /* select the endpoint FIFO */
1006 outw (UDC_EP_Sel | epnum, UDC_EP_NUM);
1008 omap1510_udc_epn_rx (epnum);
1010 /* deselect the endpoint FIFO */
1011 outw (epnum, UDC_EP_NUM);
1015 if (irq_src & UDC_EPn_TX) { /* Endpoint N IN transaction */
1016 /* Determine the endpoint number for this interrupt */
1017 epnum = (inw (UDC_EPN_STAT) & 0x000f) | USB_DIR_IN;
1018 UDCDBGA ("TX on ep %x", epnum);
1020 /* acknowledge interrupt */
1021 outw (UDC_EPn_TX, UDC_IRQ_SRC);
1024 /* select the endpoint FIFO */
1025 outw (UDC_EP_Sel | UDC_EP_Dir | epnum, UDC_EP_NUM);
1027 omap1510_udc_epn_tx (epnum);
1029 /* deselect the endpoint FIFO */
1030 outw (UDC_EP_Dir | epnum, UDC_EP_NUM);
1035 serial_printf (": unknown non-ISO interrupt, IRQ_SRC %.4x\n",
1040 -------------------------------------------------------------------------------
1045 * Start of public functions.
1048 /* Called to start packet transmission. */
1049 int udc_endpoint_write (struct usb_endpoint_instance *endpoint)
1051 unsigned short epnum =
1052 endpoint->endpoint_address & USB_ENDPOINT_NUMBER_MASK;
1054 UDCDBGA ("Starting transmit on ep %x", epnum);
1056 if (endpoint->tx_urb) {
1057 /* select the endpoint FIFO */
1058 outw (UDC_EP_Sel | UDC_EP_Dir | epnum, UDC_EP_NUM);
1059 /* write data to FIFO */
1060 omap1510_write_noniso_tx_fifo (endpoint);
1061 /* enable tx FIFO to start transmission */
1062 outw (UDC_Set_FIFO_En, UDC_CTRL);
1063 /* deselect the endpoint FIFO */
1064 outw (UDC_EP_Dir | epnum, UDC_EP_NUM);
1070 /* Start to initialize h/w stuff */
1078 /* Let the device settle down before we start */
1079 for (i = 0; i < UDC_INIT_MDELAY; i++) udelay(1000);
1083 UDCDBG ("starting");
1085 /* Check peripheral reset. Must be 1 to make sure
1086 MPU TIPB peripheral reset is inactive */
1087 UDCREG (ARM_RSTCT2);
1089 /* Set and check clock control.
1090 * We might ought to be using the clock control API to do
1091 * this instead of fiddling with the clock registers directly
1094 outw ((1 << 4) | (1 << 5), CLOCK_CTRL);
1095 UDCREG (CLOCK_CTRL);
1097 /* Set and check SOFT
1098 * The below line of code has been changed to perform a
1099 * read-modify-write instead of a simple write for
1100 * configuring the SOFT_REQ register. This allows the code
1101 * to be compatible with OMAP5912 and OMAP16xx devices
1103 outw ((1 << 4) | (1 << 3) | 1 | (inw(SOFT_REQ)), SOFT_REQ);
1105 /* Short delay to wait for DPLL */
1108 /* Print banner with device revision */
1109 udc_rev = inw (UDC_REV) & 0xff;
1111 #ifdef CONFIG_OMAP1610
1112 printf ("USB: TI OMAP5912 USB function module rev %d.%d\n",
1113 udc_rev >> 4, udc_rev & 0xf);
1116 /* The VBUS_MODE bit selects whether VBUS detection is done via
1117 * software (1) or hardware (0). When software detection is
1118 * selected, VBUS_CTRL selects whether USB is not connected (0)
1121 outl (inl (FUNC_MUX_CTRL_0) | UDC_VBUS_MODE, FUNC_MUX_CTRL_0);
1122 outl (inl (FUNC_MUX_CTRL_0) & ~UDC_VBUS_CTRL, FUNC_MUX_CTRL_0);
1123 UDCREGL (FUNC_MUX_CTRL_0);
1126 * At this point, device is ready for configuration...
1129 UDCDBG ("disable USB interrupts");
1130 outw (0, UDC_IRQ_EN);
1131 UDCREG (UDC_IRQ_EN);
1133 UDCDBG ("disable USB DMA");
1134 outw (0, UDC_DMA_IRQ_EN);
1135 UDCREG (UDC_DMA_IRQ_EN);
1137 UDCDBG ("initialize SYSCON1");
1138 outw (UDC_Self_Pwr | UDC_Pullup_En, UDC_SYSCON1);
1139 UDCREG (UDC_SYSCON1);
1144 /* Stall endpoint */
1145 static void udc_stall_ep (unsigned int ep_addr)
1147 /*int ep_addr = PHYS_EP_TO_EP_ADDR(ep); */
1148 int ep_num = ep_addr & USB_ENDPOINT_NUMBER_MASK;
1150 UDCDBGA ("stall ep_addr %d", ep_addr);
1153 * The OMAP TRM section 14.2.4.2 says we must check that the FIFO
1154 * is empty before halting the endpoint. The current implementation
1155 * doesn't check that the FIFO is empty.
1159 outw (UDC_Stall_Cmd, UDC_SYSCON2);
1160 } else if ((ep_addr & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT) {
1161 if (inw (UDC_EP_RX (ep_num)) & UDC_EPn_RX_Valid) {
1162 /* we have a valid rx endpoint, so halt it */
1163 outw (UDC_EP_Sel | ep_num, UDC_EP_NUM);
1164 outw (UDC_Set_Halt, UDC_CTRL);
1165 outw (ep_num, UDC_EP_NUM);
1168 if (inw (UDC_EP_TX (ep_num)) & UDC_EPn_TX_Valid) {
1169 /* we have a valid tx endpoint, so halt it */
1170 outw (UDC_EP_Sel | UDC_EP_Dir | ep_num, UDC_EP_NUM);
1171 outw (UDC_Set_Halt, UDC_CTRL);
1172 outw (ep_num, UDC_EP_NUM);
1177 /* Reset endpoint */
1179 static void udc_reset_ep (unsigned int ep_addr)
1181 /*int ep_addr = PHYS_EP_TO_EP_ADDR(ep); */
1182 int ep_num = ep_addr & USB_ENDPOINT_NUMBER_MASK;
1184 UDCDBGA ("reset ep_addr %d", ep_addr);
1187 /* control endpoint 0 can't be reset */
1188 } else if ((ep_addr & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT) {
1189 UDCDBGA ("UDC_EP_RX(%d) = 0x%04x", ep_num,
1190 inw (UDC_EP_RX (ep_num)));
1191 if (inw (UDC_EP_RX (ep_num)) & UDC_EPn_RX_Valid) {
1192 /* we have a valid rx endpoint, so reset it */
1193 outw (ep_num | UDC_EP_Sel, UDC_EP_NUM);
1194 outw (UDC_Reset_EP, UDC_CTRL);
1195 outw (ep_num, UDC_EP_NUM);
1196 UDCDBGA ("OUT endpoint %d reset", ep_num);
1199 UDCDBGA ("UDC_EP_TX(%d) = 0x%04x", ep_num,
1200 inw (UDC_EP_TX (ep_num)));
1201 /* Resetting of tx endpoints seems to be causing the USB function
1202 * module to fail, which causes problems when the driver is
1203 * uninstalled. We'll skip resetting tx endpoints for now until
1204 * we figure out what the problem is.
1207 if (inw (UDC_EP_TX (ep_num)) & UDC_EPn_TX_Valid) {
1208 /* we have a valid tx endpoint, so reset it */
1209 outw (ep_num | UDC_EP_Dir | UDC_EP_Sel, UDC_EP_NUM);
1210 outw (UDC_Reset_EP, UDC_CTRL);
1211 outw (ep_num | UDC_EP_Dir, UDC_EP_NUM);
1212 UDCDBGA ("IN endpoint %d reset", ep_num);
1219 /* ************************************************************************** */
1222 * udc_check_ep - check logical endpoint
1224 * Return physical endpoint number to use for this logical endpoint or zero if not valid.
1227 int udc_check_ep (int logical_endpoint, int packetsize)
1229 if ((logical_endpoint == 0x80) ||
1230 ((logical_endpoint & 0x8f) != logical_endpoint)) {
1234 switch (packetsize) {
1247 return EP_ADDR_TO_PHYS_EP (logical_endpoint);
1252 * udc_setup_ep - setup endpoint
1254 * Associate a physical endpoint with endpoint_instance
1256 void udc_setup_ep (struct usb_device_instance *device,
1257 unsigned int ep, struct usb_endpoint_instance *endpoint)
1259 UDCDBGA ("setting up endpoint addr %x", endpoint->endpoint_address);
1261 /* This routine gets called by bi_modinit for endpoint 0 and from
1262 * bi_config for all of the other endpoints. bi_config gets called
1263 * during the DEVICE_CREATE, DEVICE_CONFIGURED, and
1264 * DEVICE_SET_INTERFACE events. We need to reconfigure the OMAP packet
1265 * RAM after bi_config scans the selected device configuration and
1266 * initializes the endpoint structures, but before this routine enables
1267 * the OUT endpoint FIFOs. Since bi_config calls this routine in a
1268 * loop for endpoints 1 through UDC_MAX_ENDPOINTS, we reconfigure our
1269 * packet RAM here when ep==1.
1270 * I really hate to do this here, but it seems like the API exported
1271 * by the USB bus interface controller driver to the usbd-bi module
1272 * isn't quite right so there is no good place to do this.
1275 omap1510_deconfigure_device ();
1276 omap1510_configure_device (device);
1279 if (endpoint && (ep < UDC_MAX_ENDPOINTS)) {
1280 int ep_addr = endpoint->endpoint_address;
1283 /* nothing to do for endpoint 0 */
1284 } else if ((ep_addr & USB_ENDPOINT_DIR_MASK) == USB_DIR_IN) {
1285 /* nothing to do for IN (tx) endpoints */
1286 } else { /* OUT (rx) endpoint */
1287 if (endpoint->rcv_packetSize) {
1288 /*struct urb* urb = &(urb_out_array[ep&0xFF]); */
1289 /*urb->endpoint = endpoint; */
1290 /*urb->device = device; */
1291 /*urb->buffer_length = sizeof(urb->buffer); */
1293 /*endpoint->rcv_urb = urb; */
1294 omap1510_prepare_endpoint_for_rx (ep_addr);
1301 * udc_disable_ep - disable endpoint
1304 * Disable specified endpoint
1307 void udc_disable_ep (unsigned int ep_addr)
1309 /*int ep_addr = PHYS_EP_TO_EP_ADDR(ep); */
1310 int ep_num = ep_addr & USB_ENDPOINT_NUMBER_MASK;
1311 struct usb_endpoint_instance *endpoint = omap1510_find_ep (ep_addr); /*udc_device->bus->endpoint_array + ep; */
1313 UDCDBGA ("disable ep_addr %d", ep_addr);
1316 /* nothing to do for endpoint 0 */ ;
1317 } else if ((ep_addr & USB_ENDPOINT_DIR_MASK) == USB_DIR_IN) {
1318 if (endpoint->tx_packetSize) {
1319 /* we have a valid tx endpoint */
1320 /*usbd_flush_tx(endpoint); */
1321 endpoint->tx_urb = NULL;
1324 if (endpoint->rcv_packetSize) {
1325 /* we have a valid rx endpoint */
1326 /*usbd_flush_rcv(endpoint); */
1327 endpoint->rcv_urb = NULL;
1333 /* ************************************************************************** */
1336 * udc_connected - is the USB cable connected
1338 * Return non-zero if cable is connected.
1341 int udc_connected (void)
1343 return ((inw (UDC_DEVSTAT) & UDC_ATT) == UDC_ATT);
1347 /* Turn on the USB connection by enabling the pullup resistor */
1348 void udc_connect (void)
1350 UDCDBG ("connect, enable Pullup");
1351 outl (0x00000018, FUNC_MUX_CTRL_D);
1354 /* Turn off the USB connection by disabling the pullup resistor */
1355 void udc_disconnect (void)
1357 UDCDBG ("disconnect, disable Pullup");
1358 outl (0x00000000, FUNC_MUX_CTRL_D);
1361 /* ************************************************************************** */
1365 * udc_disable_interrupts - disable interrupts
1366 * switch off interrupts
1369 void udc_disable_interrupts (struct usb_device_instance *device)
1371 UDCDBG ("disabling all interrupts");
1372 outw (0, UDC_IRQ_EN);
1376 /* ************************************************************************** */
1379 * udc_ep0_packetsize - return ep0 packetsize
1382 int udc_ep0_packetsize (void)
1384 return EP0_PACKETSIZE;
1388 /* Switch on the UDC */
1389 void udc_enable (struct usb_device_instance *device)
1391 UDCDBGA ("enable device %p, status %d", device, device->status);
1393 /* initialize driver state variables */
1396 /* Save the device structure pointer */
1397 udc_device = device;
1402 usbd_alloc_urb (udc_device,
1403 udc_device->bus->endpoint_array);
1405 serial_printf ("udc_enable: ep0_urb already allocated %p\n",
1409 UDCDBG ("Check clock status");
1410 UDCREG (STATUS_REQ);
1412 /* The VBUS_MODE bit selects whether VBUS detection is done via
1413 * software (1) or hardware (0). When software detection is
1414 * selected, VBUS_CTRL selects whether USB is not connected (0)
1417 outl (inl (FUNC_MUX_CTRL_0) | UDC_VBUS_CTRL | UDC_VBUS_MODE,
1419 UDCREGL (FUNC_MUX_CTRL_0);
1421 omap1510_configure_device (device);
1424 /* Switch off the UDC */
1425 void udc_disable (void)
1427 UDCDBG ("disable UDC");
1429 omap1510_deconfigure_device ();
1431 /* The VBUS_MODE bit selects whether VBUS detection is done via
1432 * software (1) or hardware (0). When software detection is
1433 * selected, VBUS_CTRL selects whether USB is not connected (0)
1436 outl (inl (FUNC_MUX_CTRL_0) | UDC_VBUS_MODE, FUNC_MUX_CTRL_0);
1437 outl (inl (FUNC_MUX_CTRL_0) & ~UDC_VBUS_CTRL, FUNC_MUX_CTRL_0);
1438 UDCREGL (FUNC_MUX_CTRL_0);
1442 /*usbd_dealloc_urb(ep0_urb); */
1446 /* Reset device pointer.
1447 * We ought to do this here to balance the initialization of udc_device
1448 * in udc_enable, but some of our other exported functions get called
1449 * by the bus interface driver after udc_disable, so we have to hang on
1450 * to the device pointer to avoid a null pointer dereference. */
1451 /* udc_device = NULL; */
1455 * udc_startup - allow udc code to do any additional startup
1457 void udc_startup_events (struct usb_device_instance *device)
1459 /* The DEVICE_INIT event puts the USB device in the state STATE_INIT. */
1460 usbd_device_event_irq (device, DEVICE_INIT, 0);
1462 /* The DEVICE_CREATE event puts the USB device in the state
1465 usbd_device_event_irq (device, DEVICE_CREATE, 0);
1467 /* Some USB controller driver implementations signal
1468 * DEVICE_HUB_CONFIGURED and DEVICE_RESET events here.
1469 * DEVICE_HUB_CONFIGURED causes a transition to the state STATE_POWERED,
1470 * and DEVICE_RESET causes a transition to the state STATE_DEFAULT.
1471 * The OMAP USB client controller has the capability to detect when the
1472 * USB cable is connected to a powered USB bus via the ATT bit in the
1473 * DEVSTAT register, so we will defer the DEVICE_HUB_CONFIGURED and
1474 * DEVICE_RESET events until later.
1477 udc_enable (device);
1481 * udc_irq - do pseudo interrupts
1485 /* Loop while we have interrupts.
1486 * If we don't do this, the input chain
1487 * polling delay is likely to miss
1490 while (inw (UDC_IRQ_SRC) & ~UDC_SOF_Flg) {
1491 /* Handle any new IRQs */
1492 omap1510_udc_irq ();
1493 omap1510_udc_noniso_irq ();
1498 void udc_set_nak(int epid)
1500 /* TODO: implement this functionality in omap1510 */
1503 void udc_unset_nak (int epid)
1505 /* TODO: implement this functionality in omap1510 */