2 * omap_udc.c -- for OMAP full speed udc; most chips support OTG.
4 * Copyright (C) 2004 Texas Instruments, Inc.
5 * Copyright (C) 2004-2005 David Brownell
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include <linux/config.h>
26 #include <linux/module.h>
27 #include <linux/kernel.h>
28 #include <linux/ioport.h>
29 #include <linux/types.h>
30 #include <linux/errno.h>
31 #include <linux/delay.h>
32 #include <linux/sched.h>
33 #include <linux/slab.h>
34 #include <linux/init.h>
35 #include <linux/timer.h>
36 #include <linux/list.h>
37 #include <linux/interrupt.h>
38 #include <linux/proc_fs.h>
40 #include <linux/moduleparam.h>
41 #include <linux/device.h>
42 #include <linux/usb_ch9.h>
43 #include <linux/usb_gadget.h>
44 #include <linux/usb_otg.h>
45 #include <linux/dma-mapping.h>
47 #include <asm/byteorder.h>
50 #include <asm/system.h>
51 #include <asm/unaligned.h>
52 #include <asm/mach-types.h>
54 #include <asm/arch/dma.h>
55 #include <asm/arch/usb.h>
61 /* bulk DMA seems to be behaving for both IN and OUT */
67 #define DRIVER_DESC "OMAP UDC driver"
68 #define DRIVER_VERSION "4 October 2004"
70 #define DMA_ADDR_INVALID (~(dma_addr_t)0)
74 * The OMAP UDC needs _very_ early endpoint setup: before enabling the
75 * D+ pullup to allow enumeration. That's too early for the gadget
76 * framework to use from usb_endpoint_enable(), which happens after
77 * enumeration as part of activating an interface. (But if we add an
78 * optional new "UDC not yet running" state to the gadget driver model,
79 * even just during driver binding, the endpoint autoconfig logic is the
80 * natural spot to manufacture new endpoints.)
82 * So instead of using endpoint enable calls to control the hardware setup,
83 * this driver defines a "fifo mode" parameter. It's used during driver
84 * initialization to choose among a set of pre-defined endpoint configs.
85 * See omap_udc_setup() for available modes, or to add others. That code
86 * lives in an init section, so use this driver as a module if you need
87 * to change the fifo mode after the kernel boots.
89 * Gadget drivers normally ignore endpoints they don't care about, and
90 * won't include them in configuration descriptors. That means only
91 * misbehaving hosts would even notice they exist.
94 static unsigned fifo_mode = 3;
96 static unsigned fifo_mode = 0;
99 /* "modprobe omap_udc fifo_mode=42", or else as a kernel
100 * boot parameter "omap_udc:fifo_mode=42"
102 module_param (fifo_mode, uint, 0);
103 MODULE_PARM_DESC (fifo_mode, "endpoint setup (0 == default)");
106 static unsigned use_dma = 1;
108 /* "modprobe omap_udc use_dma=y", or else as a kernel
109 * boot parameter "omap_udc:use_dma=y"
111 module_param (use_dma, bool, 0);
112 MODULE_PARM_DESC (use_dma, "enable/disable DMA");
115 /* save a bit of code */
117 #endif /* !USE_DMA */
120 static const char driver_name [] = "omap_udc";
121 static const char driver_desc [] = DRIVER_DESC;
123 /*-------------------------------------------------------------------------*/
125 /* there's a notion of "current endpoint" for modifying endpoint
126 * state, and PIO access to its FIFO.
129 static void use_ep(struct omap_ep *ep, u16 select)
131 u16 num = ep->bEndpointAddress & 0x0f;
133 if (ep->bEndpointAddress & USB_DIR_IN)
135 UDC_EP_NUM_REG = num | select;
136 /* when select, MUST deselect later !! */
139 static inline void deselect_ep(void)
141 UDC_EP_NUM_REG &= ~UDC_EP_SEL;
142 /* 6 wait states before TX will happen */
145 static void dma_channel_claim(struct omap_ep *ep, unsigned preferred);
147 /*-------------------------------------------------------------------------*/
149 static int omap_ep_enable(struct usb_ep *_ep,
150 const struct usb_endpoint_descriptor *desc)
152 struct omap_ep *ep = container_of(_ep, struct omap_ep, ep);
153 struct omap_udc *udc;
157 /* catch various bogus parameters */
158 if (!_ep || !desc || ep->desc
159 || desc->bDescriptorType != USB_DT_ENDPOINT
160 || ep->bEndpointAddress != desc->bEndpointAddress
161 || ep->maxpacket < le16_to_cpu
162 (desc->wMaxPacketSize)) {
163 DBG("%s, bad ep or descriptor\n", __FUNCTION__);
166 maxp = le16_to_cpu (desc->wMaxPacketSize);
167 if ((desc->bmAttributes == USB_ENDPOINT_XFER_BULK
168 && maxp != ep->maxpacket)
169 || desc->wMaxPacketSize > ep->maxpacket
170 || !desc->wMaxPacketSize) {
171 DBG("%s, bad %s maxpacket\n", __FUNCTION__, _ep->name);
176 if ((desc->bmAttributes == USB_ENDPOINT_XFER_ISOC
177 && desc->bInterval != 1)) {
178 /* hardware wants period = 1; USB allows 2^(Interval-1) */
179 DBG("%s, unsupported ISO period %dms\n", _ep->name,
180 1 << (desc->bInterval - 1));
184 if (desc->bmAttributes == USB_ENDPOINT_XFER_ISOC) {
185 DBG("%s, ISO nyet\n", _ep->name);
190 /* xfer types must match, except that interrupt ~= bulk */
191 if (ep->bmAttributes != desc->bmAttributes
192 && ep->bmAttributes != USB_ENDPOINT_XFER_BULK
193 && desc->bmAttributes != USB_ENDPOINT_XFER_INT) {
194 DBG("%s, %s type mismatch\n", __FUNCTION__, _ep->name);
199 if (!udc->driver || udc->gadget.speed == USB_SPEED_UNKNOWN) {
200 DBG("%s, bogus device state\n", __FUNCTION__);
204 spin_lock_irqsave(&udc->lock, flags);
209 ep->ep.maxpacket = maxp;
211 /* set endpoint to initial state */
215 use_ep(ep, UDC_EP_SEL);
216 UDC_CTRL_REG = UDC_RESET_EP;
220 if (ep->bmAttributes == USB_ENDPOINT_XFER_ISOC)
221 list_add(&ep->iso, &udc->iso);
223 /* maybe assign a DMA channel to this endpoint */
224 if (use_dma && desc->bmAttributes == USB_ENDPOINT_XFER_BULK)
225 /* FIXME ISO can dma, but prefers first channel */
226 dma_channel_claim(ep, 0);
228 /* PIO OUT may RX packets */
229 if (desc->bmAttributes != USB_ENDPOINT_XFER_ISOC
231 && !(ep->bEndpointAddress & USB_DIR_IN)) {
232 UDC_CTRL_REG = UDC_SET_FIFO_EN;
233 ep->ackwait = 1 + ep->double_buf;
236 spin_unlock_irqrestore(&udc->lock, flags);
237 VDBG("%s enabled\n", _ep->name);
241 static void nuke(struct omap_ep *, int status);
243 static int omap_ep_disable(struct usb_ep *_ep)
245 struct omap_ep *ep = container_of(_ep, struct omap_ep, ep);
248 if (!_ep || !ep->desc) {
249 DBG("%s, %s not enabled\n", __FUNCTION__,
250 _ep ? ep->ep.name : NULL);
254 spin_lock_irqsave(&ep->udc->lock, flags);
256 nuke (ep, -ESHUTDOWN);
257 ep->ep.maxpacket = ep->maxpacket;
259 UDC_CTRL_REG = UDC_SET_HALT;
260 list_del_init(&ep->iso);
261 del_timer(&ep->timer);
263 spin_unlock_irqrestore(&ep->udc->lock, flags);
265 VDBG("%s disabled\n", _ep->name);
269 /*-------------------------------------------------------------------------*/
271 static struct usb_request *
272 omap_alloc_request(struct usb_ep *ep, int gfp_flags)
274 struct omap_req *req;
276 req = kmalloc(sizeof *req, gfp_flags);
278 memset (req, 0, sizeof *req);
279 req->req.dma = DMA_ADDR_INVALID;
280 INIT_LIST_HEAD (&req->queue);
286 omap_free_request(struct usb_ep *ep, struct usb_request *_req)
288 struct omap_req *req = container_of(_req, struct omap_req, req);
294 /*-------------------------------------------------------------------------*/
307 ep = container_of(_ep, struct omap_ep, ep);
308 if (use_dma && ep->has_dma) {
310 if (!warned && bytes < PAGE_SIZE) {
311 dev_warn(ep->udc->gadget.dev.parent,
312 "using dma_alloc_coherent for "
313 "small allocations wastes memory\n");
316 return dma_alloc_coherent(ep->udc->gadget.dev.parent,
317 bytes, dma, gfp_flags);
320 retval = kmalloc(bytes, gfp_flags);
322 *dma = virt_to_phys(retval);
326 static void omap_free_buffer(
335 ep = container_of(_ep, struct omap_ep, ep);
336 if (use_dma && _ep && ep->has_dma)
337 dma_free_coherent(ep->udc->gadget.dev.parent, bytes, buf, dma);
342 /*-------------------------------------------------------------------------*/
345 done(struct omap_ep *ep, struct omap_req *req, int status)
347 unsigned stopped = ep->stopped;
349 list_del_init(&req->queue);
351 if (req->req.status == -EINPROGRESS)
352 req->req.status = status;
354 status = req->req.status;
356 if (use_dma && ep->has_dma) {
358 dma_unmap_single(ep->udc->gadget.dev.parent,
359 req->req.dma, req->req.length,
360 (ep->bEndpointAddress & USB_DIR_IN)
363 req->req.dma = DMA_ADDR_INVALID;
366 dma_sync_single_for_cpu(ep->udc->gadget.dev.parent,
367 req->req.dma, req->req.length,
368 (ep->bEndpointAddress & USB_DIR_IN)
374 if (status && status != -ESHUTDOWN)
376 VDBG("complete %s req %p stat %d len %u/%u\n",
377 ep->ep.name, &req->req, status,
378 req->req.actual, req->req.length);
380 /* don't modify queue heads during completion callback */
382 spin_unlock(&ep->udc->lock);
383 req->req.complete(&ep->ep, &req->req);
384 spin_lock(&ep->udc->lock);
385 ep->stopped = stopped;
388 /*-------------------------------------------------------------------------*/
390 #define UDC_FIFO_FULL (UDC_NON_ISO_FIFO_FULL | UDC_ISO_FIFO_FULL)
391 #define UDC_FIFO_UNWRITABLE (UDC_EP_HALTED | UDC_FIFO_FULL)
393 #define FIFO_EMPTY (UDC_NON_ISO_FIFO_EMPTY | UDC_ISO_FIFO_EMPTY)
394 #define FIFO_UNREADABLE (UDC_EP_HALTED | FIFO_EMPTY)
397 write_packet(u8 *buf, struct omap_req *req, unsigned max)
402 len = min(req->req.length - req->req.actual, max);
403 req->req.actual += len;
406 if (likely((((int)buf) & 1) == 0)) {
409 UDC_DATA_REG = *wp++;
415 *(volatile u8 *)&UDC_DATA_REG = *buf++;
419 // FIXME change r/w fifo calling convention
422 // return: 0 = still running, 1 = completed, negative = errno
423 static int write_fifo(struct omap_ep *ep, struct omap_req *req)
430 buf = req->req.buf + req->req.actual;
433 /* PIO-IN isn't double buffered except for iso */
434 ep_stat = UDC_STAT_FLG_REG;
435 if (ep_stat & UDC_FIFO_UNWRITABLE)
438 count = ep->ep.maxpacket;
439 count = write_packet(buf, req, count);
440 UDC_CTRL_REG = UDC_SET_FIFO_EN;
443 /* last packet is often short (sometimes a zlp) */
444 if (count != ep->ep.maxpacket)
446 else if (req->req.length == req->req.actual
452 /* NOTE: requests complete when all IN data is in a
453 * FIFO (or sometimes later, if a zlp was needed).
454 * Use usb_ep_fifo_status() where needed.
462 read_packet(u8 *buf, struct omap_req *req, unsigned avail)
467 len = min(req->req.length - req->req.actual, avail);
468 req->req.actual += len;
471 if (likely((((int)buf) & 1) == 0)) {
474 *wp++ = UDC_DATA_REG;
480 *buf++ = *(volatile u8 *)&UDC_DATA_REG;
484 // return: 0 = still running, 1 = queue empty, negative = errno
485 static int read_fifo(struct omap_ep *ep, struct omap_req *req)
488 unsigned count, avail;
491 buf = req->req.buf + req->req.actual;
495 u16 ep_stat = UDC_STAT_FLG_REG;
498 if (ep_stat & FIFO_EMPTY) {
503 if (ep_stat & UDC_EP_HALTED)
506 if (ep_stat & UDC_FIFO_FULL)
507 avail = ep->ep.maxpacket;
509 avail = UDC_RXFSTAT_REG;
510 ep->fnf = ep->double_buf;
512 count = read_packet(buf, req, avail);
514 /* partial packet reads may not be errors */
515 if (count < ep->ep.maxpacket) {
517 /* overflowed this request? flush extra data */
518 if (count != avail) {
519 req->req.status = -EOVERFLOW;
522 (void) *(volatile u8 *)&UDC_DATA_REG;
524 } else if (req->req.length == req->req.actual)
529 if (!ep->bEndpointAddress)
538 /*-------------------------------------------------------------------------*/
540 static u16 dma_src_len(struct omap_ep *ep, dma_addr_t start)
544 /* IN-DMA needs this on fault/cancel paths, so 15xx misreports
545 * the last transfer's bytecount by more than a FIFO's worth.
547 if (cpu_is_omap15xx())
550 end = omap_readw(OMAP_DMA_CSAC(ep->lch));
551 if (end == ep->dma_counter)
554 end |= start & (0xffff << 16);
560 #define DMA_DEST_LAST(x) (cpu_is_omap15xx() \
561 ? OMAP_DMA_CSAC(x) /* really: CPC */ \
564 static u16 dma_dest_len(struct omap_ep *ep, dma_addr_t start)
568 end = omap_readw(DMA_DEST_LAST(ep->lch));
569 if (end == ep->dma_counter)
572 end |= start & (0xffff << 16);
573 if (cpu_is_omap15xx())
581 /* Each USB transfer request using DMA maps to one or more DMA transfers.
582 * When DMA completion isn't request completion, the UDC continues with
583 * the next DMA transfer for that USB transfer.
586 static void next_in_dma(struct omap_ep *ep, struct omap_req *req)
589 unsigned length = req->req.length - req->req.actual;
590 const int sync_mode = cpu_is_omap15xx()
591 ? OMAP_DMA_SYNC_FRAME
592 : OMAP_DMA_SYNC_ELEMENT;
594 /* measure length in either bytes or packets */
595 if ((cpu_is_omap16xx() && length <= (UDC_TXN_TSC + 1))
596 || (cpu_is_omap15xx() && length < ep->maxpacket)) {
597 txdma_ctrl = UDC_TXN_EOT | length;
598 omap_set_dma_transfer_params(ep->lch, OMAP_DMA_DATA_TYPE_S8,
599 length, 1, sync_mode);
601 length = min(length / ep->maxpacket,
602 (unsigned) UDC_TXN_TSC + 1);
604 omap_set_dma_transfer_params(ep->lch, OMAP_DMA_DATA_TYPE_S8,
605 ep->ep.maxpacket, length, sync_mode);
606 length *= ep->maxpacket;
608 omap_set_dma_src_params(ep->lch, OMAP_DMA_PORT_EMIFF,
609 OMAP_DMA_AMODE_POST_INC, req->req.dma + req->req.actual);
611 omap_start_dma(ep->lch);
612 ep->dma_counter = omap_readw(OMAP_DMA_CSAC(ep->lch));
613 UDC_DMA_IRQ_EN_REG |= UDC_TX_DONE_IE(ep->dma_channel);
614 UDC_TXDMA_REG(ep->dma_channel) = UDC_TXN_START | txdma_ctrl;
615 req->dma_bytes = length;
618 static void finish_in_dma(struct omap_ep *ep, struct omap_req *req, int status)
621 req->req.actual += req->dma_bytes;
623 /* return if this request needs to send data or zlp */
624 if (req->req.actual < req->req.length)
627 && req->dma_bytes != 0
628 && (req->req.actual % ep->maxpacket) == 0)
631 req->req.actual += dma_src_len(ep, req->req.dma
635 omap_stop_dma(ep->lch);
636 UDC_DMA_IRQ_EN_REG &= ~UDC_TX_DONE_IE(ep->dma_channel);
637 done(ep, req, status);
640 static void next_out_dma(struct omap_ep *ep, struct omap_req *req)
644 /* NOTE: we filtered out "short reads" before, so we know
645 * the buffer has only whole numbers of packets.
648 /* set up this DMA transfer, enable the fifo, start */
649 packets = (req->req.length - req->req.actual) / ep->ep.maxpacket;
650 packets = min(packets, (unsigned)UDC_RXN_TC + 1);
651 req->dma_bytes = packets * ep->ep.maxpacket;
652 omap_set_dma_transfer_params(ep->lch, OMAP_DMA_DATA_TYPE_S8,
653 ep->ep.maxpacket, packets,
654 OMAP_DMA_SYNC_ELEMENT);
655 omap_set_dma_dest_params(ep->lch, OMAP_DMA_PORT_EMIFF,
656 OMAP_DMA_AMODE_POST_INC, req->req.dma + req->req.actual);
657 ep->dma_counter = omap_readw(DMA_DEST_LAST(ep->lch));
659 UDC_RXDMA_REG(ep->dma_channel) = UDC_RXN_STOP | (packets - 1);
660 UDC_DMA_IRQ_EN_REG |= UDC_RX_EOT_IE(ep->dma_channel);
661 UDC_EP_NUM_REG = (ep->bEndpointAddress & 0xf);
662 UDC_CTRL_REG = UDC_SET_FIFO_EN;
664 omap_start_dma(ep->lch);
668 finish_out_dma(struct omap_ep *ep, struct omap_req *req, int status)
673 ep->dma_counter = (u16) (req->req.dma + req->req.actual);
674 count = dma_dest_len(ep, req->req.dma + req->req.actual);
675 count += req->req.actual;
676 if (count <= req->req.length)
677 req->req.actual = count;
679 if (count != req->dma_bytes || status)
680 omap_stop_dma(ep->lch);
682 /* if this wasn't short, request may need another transfer */
683 else if (req->req.actual < req->req.length)
687 UDC_DMA_IRQ_EN_REG &= ~UDC_RX_EOT_IE(ep->dma_channel);
688 done(ep, req, status);
691 static void dma_irq(struct omap_udc *udc, u16 irq_src)
693 u16 dman_stat = UDC_DMAN_STAT_REG;
695 struct omap_req *req;
697 /* IN dma: tx to host */
698 if (irq_src & UDC_TXN_DONE) {
699 ep = &udc->ep[16 + UDC_DMA_TX_SRC(dman_stat)];
701 /* can see TXN_DONE after dma abort */
702 if (!list_empty(&ep->queue)) {
703 req = container_of(ep->queue.next,
704 struct omap_req, queue);
705 finish_in_dma(ep, req, 0);
707 UDC_IRQ_SRC_REG = UDC_TXN_DONE;
709 if (!list_empty (&ep->queue)) {
710 req = container_of(ep->queue.next,
711 struct omap_req, queue);
712 next_in_dma(ep, req);
716 /* OUT dma: rx from host */
717 if (irq_src & UDC_RXN_EOT) {
718 ep = &udc->ep[UDC_DMA_RX_SRC(dman_stat)];
720 /* can see RXN_EOT after dma abort */
721 if (!list_empty(&ep->queue)) {
722 req = container_of(ep->queue.next,
723 struct omap_req, queue);
724 finish_out_dma(ep, req, 0);
726 UDC_IRQ_SRC_REG = UDC_RXN_EOT;
728 if (!list_empty (&ep->queue)) {
729 req = container_of(ep->queue.next,
730 struct omap_req, queue);
731 next_out_dma(ep, req);
735 if (irq_src & UDC_RXN_CNT) {
736 ep = &udc->ep[UDC_DMA_RX_SRC(dman_stat)];
738 /* omap15xx does this unasked... */
739 VDBG("%s, RX_CNT irq?\n", ep->ep.name);
740 UDC_IRQ_SRC_REG = UDC_RXN_CNT;
744 static void dma_error(int lch, u16 ch_status, void *data)
746 struct omap_ep *ep = data;
748 /* if ch_status & OMAP_DMA_DROP_IRQ ... */
749 /* if ch_status & OMAP_DMA_TOUT_IRQ ... */
750 ERR("%s dma error, lch %d status %02x\n", ep->ep.name, lch, ch_status);
752 /* complete current transfer ... */
755 static void dma_channel_claim(struct omap_ep *ep, unsigned channel)
758 int status, restart, is_in;
760 is_in = ep->bEndpointAddress & USB_DIR_IN;
762 reg = UDC_TXDMA_CFG_REG;
764 reg = UDC_RXDMA_CFG_REG;
765 reg |= 1 << 12; /* "pulse" activated */
769 if (channel == 0 || channel > 3) {
770 if ((reg & 0x0f00) == 0)
772 else if ((reg & 0x00f0) == 0)
774 else if ((reg & 0x000f) == 0) /* preferred for ISO */
781 reg |= (0x0f & ep->bEndpointAddress) << (4 * (channel - 1));
782 ep->dma_channel = channel;
785 status = omap_request_dma(OMAP_DMA_USB_W2FC_TX0 - 1 + channel,
786 ep->ep.name, dma_error, ep, &ep->lch);
788 UDC_TXDMA_CFG_REG = reg;
789 omap_set_dma_dest_params(ep->lch,
791 OMAP_DMA_AMODE_CONSTANT,
792 (unsigned long) io_v2p((u32)&UDC_DATA_DMA_REG));
795 status = omap_request_dma(OMAP_DMA_USB_W2FC_RX0 - 1 + channel,
796 ep->ep.name, dma_error, ep, &ep->lch);
798 UDC_RXDMA_CFG_REG = reg;
799 omap_set_dma_src_params(ep->lch,
801 OMAP_DMA_AMODE_CONSTANT,
802 (unsigned long) io_v2p((u32)&UDC_DATA_DMA_REG));
809 omap_disable_dma_irq(ep->lch, OMAP_DMA_BLOCK_IRQ);
811 /* channel type P: hw synch (fifo) */
812 if (!cpu_is_omap15xx())
813 omap_writew(2, OMAP_DMA_LCH_CTRL(ep->lch));
817 /* restart any queue, even if the claim failed */
818 restart = !ep->stopped && !list_empty(&ep->queue);
821 DBG("%s no dma channel: %d%s\n", ep->ep.name, status,
822 restart ? " (restart)" : "");
824 DBG("%s claimed %cxdma%d lch %d%s\n", ep->ep.name,
826 ep->dma_channel - 1, ep->lch,
827 restart ? " (restart)" : "");
830 struct omap_req *req;
831 req = container_of(ep->queue.next, struct omap_req, queue);
833 (is_in ? next_in_dma : next_out_dma)(ep, req);
835 use_ep(ep, UDC_EP_SEL);
836 (is_in ? write_fifo : read_fifo)(ep, req);
839 UDC_CTRL_REG = UDC_SET_FIFO_EN;
840 ep->ackwait = 1 + ep->double_buf;
842 /* IN: 6 wait states before it'll tx */
847 static void dma_channel_release(struct omap_ep *ep)
849 int shift = 4 * (ep->dma_channel - 1);
850 u16 mask = 0x0f << shift;
851 struct omap_req *req;
854 /* abort any active usb transfer request */
855 if (!list_empty(&ep->queue))
856 req = container_of(ep->queue.next, struct omap_req, queue);
860 active = ((1 << 7) & omap_readl(OMAP_DMA_CCR(ep->lch))) != 0;
862 DBG("%s release %s %cxdma%d %p\n", ep->ep.name,
863 active ? "active" : "idle",
864 (ep->bEndpointAddress & USB_DIR_IN) ? 't' : 'r',
865 ep->dma_channel - 1, req);
867 /* wait till current packet DMA finishes, and fifo empties */
868 if (ep->bEndpointAddress & USB_DIR_IN) {
869 UDC_TXDMA_CFG_REG &= ~mask;
872 finish_in_dma(ep, req, -ECONNRESET);
874 /* clear FIFO; hosts probably won't empty it */
875 use_ep(ep, UDC_EP_SEL);
876 UDC_CTRL_REG = UDC_CLR_EP;
879 while (UDC_TXDMA_CFG_REG & mask)
882 UDC_RXDMA_CFG_REG &= ~mask;
884 /* dma empties the fifo */
885 while (UDC_RXDMA_CFG_REG & mask)
888 finish_out_dma(ep, req, -ECONNRESET);
890 omap_free_dma(ep->lch);
893 /* has_dma still set, till endpoint is fully quiesced */
897 /*-------------------------------------------------------------------------*/
900 omap_ep_queue(struct usb_ep *_ep, struct usb_request *_req, int gfp_flags)
902 struct omap_ep *ep = container_of(_ep, struct omap_ep, ep);
903 struct omap_req *req = container_of(_req, struct omap_req, req);
904 struct omap_udc *udc;
908 /* catch various bogus parameters */
909 if (!_req || !req->req.complete || !req->req.buf
910 || !list_empty(&req->queue)) {
911 DBG("%s, bad params\n", __FUNCTION__);
914 if (!_ep || (!ep->desc && ep->bEndpointAddress)) {
915 DBG("%s, bad ep\n", __FUNCTION__);
918 if (ep->bmAttributes == USB_ENDPOINT_XFER_ISOC) {
919 if (req->req.length > ep->ep.maxpacket)
924 /* this isn't bogus, but OMAP DMA isn't the only hardware to
925 * have a hard time with partial packet reads... reject it.
929 && ep->bEndpointAddress != 0
930 && (ep->bEndpointAddress & USB_DIR_IN) == 0
931 && (req->req.length % ep->ep.maxpacket) != 0) {
932 DBG("%s, no partial packet OUT reads\n", __FUNCTION__);
937 if (!udc->driver || udc->gadget.speed == USB_SPEED_UNKNOWN)
940 if (use_dma && ep->has_dma) {
941 if (req->req.dma == DMA_ADDR_INVALID) {
942 req->req.dma = dma_map_single(
943 ep->udc->gadget.dev.parent,
946 (ep->bEndpointAddress & USB_DIR_IN)
951 dma_sync_single_for_device(
952 ep->udc->gadget.dev.parent,
953 req->req.dma, req->req.length,
954 (ep->bEndpointAddress & USB_DIR_IN)
961 VDBG("%s queue req %p, len %d buf %p\n",
962 ep->ep.name, _req, _req->length, _req->buf);
964 spin_lock_irqsave(&udc->lock, flags);
966 req->req.status = -EINPROGRESS;
969 /* maybe kickstart non-iso i/o queues */
971 UDC_IRQ_EN_REG |= UDC_SOF_IE;
972 else if (list_empty(&ep->queue) && !ep->stopped && !ep->ackwait) {
975 if (ep->bEndpointAddress == 0) {
976 if (!udc->ep0_pending || !list_empty (&ep->queue)) {
977 spin_unlock_irqrestore(&udc->lock, flags);
981 /* empty DATA stage? */
983 if (!req->req.length) {
985 /* chip became CONFIGURED or ADDRESSED
986 * earlier; drivers may already have queued
987 * requests to non-control endpoints
989 if (udc->ep0_set_config) {
990 u16 irq_en = UDC_IRQ_EN_REG;
992 irq_en |= UDC_DS_CHG_IE | UDC_EP0_IE;
993 if (!udc->ep0_reset_config)
994 irq_en |= UDC_EPN_RX_IE
996 UDC_IRQ_EN_REG = irq_en;
999 /* STATUS for zero length DATA stages is
1000 * always an IN ... even for IN transfers,
1001 * a wierd case which seem to stall OMAP.
1003 UDC_EP_NUM_REG = (UDC_EP_SEL|UDC_EP_DIR);
1004 UDC_CTRL_REG = UDC_CLR_EP;
1005 UDC_CTRL_REG = UDC_SET_FIFO_EN;
1006 UDC_EP_NUM_REG = UDC_EP_DIR;
1009 udc->ep0_pending = 0;
1013 /* non-empty DATA stage */
1015 UDC_EP_NUM_REG = UDC_EP_SEL|UDC_EP_DIR;
1019 UDC_EP_NUM_REG = UDC_EP_SEL;
1022 is_in = ep->bEndpointAddress & USB_DIR_IN;
1024 use_ep(ep, UDC_EP_SEL);
1025 /* if ISO: SOF IRQs must be enabled/disabled! */
1029 (is_in ? next_in_dma : next_out_dma)(ep, req);
1031 if ((is_in ? write_fifo : read_fifo)(ep, req) == 1)
1035 UDC_CTRL_REG = UDC_SET_FIFO_EN;
1036 ep->ackwait = 1 + ep->double_buf;
1038 /* IN: 6 wait states before it'll tx */
1043 /* irq handler advances the queue */
1045 list_add_tail(&req->queue, &ep->queue);
1046 spin_unlock_irqrestore(&udc->lock, flags);
1051 static int omap_ep_dequeue(struct usb_ep *_ep, struct usb_request *_req)
1053 struct omap_ep *ep = container_of(_ep, struct omap_ep, ep);
1054 struct omap_req *req;
1055 unsigned long flags;
1060 spin_lock_irqsave(&ep->udc->lock, flags);
1062 /* make sure it's actually queued on this endpoint */
1063 list_for_each_entry (req, &ep->queue, queue) {
1064 if (&req->req == _req)
1067 if (&req->req != _req) {
1068 spin_unlock_irqrestore(&ep->udc->lock, flags);
1072 if (use_dma && ep->dma_channel && ep->queue.next == &req->queue) {
1073 int channel = ep->dma_channel;
1075 /* releasing the channel cancels the request,
1076 * reclaiming the channel restarts the queue
1078 dma_channel_release(ep);
1079 dma_channel_claim(ep, channel);
1081 done(ep, req, -ECONNRESET);
1082 spin_unlock_irqrestore(&ep->udc->lock, flags);
1086 /*-------------------------------------------------------------------------*/
1088 static int omap_ep_set_halt(struct usb_ep *_ep, int value)
1090 struct omap_ep *ep = container_of(_ep, struct omap_ep, ep);
1091 unsigned long flags;
1092 int status = -EOPNOTSUPP;
1094 spin_lock_irqsave(&ep->udc->lock, flags);
1096 /* just use protocol stalls for ep0; real halts are annoying */
1097 if (ep->bEndpointAddress == 0) {
1098 if (!ep->udc->ep0_pending)
1101 if (ep->udc->ep0_set_config) {
1102 WARN("error changing config?\n");
1103 UDC_SYSCON2_REG = UDC_CLR_CFG;
1105 UDC_SYSCON2_REG = UDC_STALL_CMD;
1106 ep->udc->ep0_pending = 0;
1111 /* otherwise, all active non-ISO endpoints can halt */
1112 } else if (ep->bmAttributes != USB_ENDPOINT_XFER_ISOC && ep->desc) {
1114 /* IN endpoints must already be idle */
1115 if ((ep->bEndpointAddress & USB_DIR_IN)
1116 && !list_empty(&ep->queue)) {
1124 if (use_dma && ep->dma_channel
1125 && !list_empty(&ep->queue)) {
1126 channel = ep->dma_channel;
1127 dma_channel_release(ep);
1131 use_ep(ep, UDC_EP_SEL);
1132 if (UDC_STAT_FLG_REG & UDC_NON_ISO_FIFO_EMPTY) {
1133 UDC_CTRL_REG = UDC_SET_HALT;
1140 dma_channel_claim(ep, channel);
1143 UDC_CTRL_REG = UDC_RESET_EP;
1145 if (!(ep->bEndpointAddress & USB_DIR_IN)) {
1146 UDC_CTRL_REG = UDC_SET_FIFO_EN;
1147 ep->ackwait = 1 + ep->double_buf;
1152 VDBG("%s %s halt stat %d\n", ep->ep.name,
1153 value ? "set" : "clear", status);
1155 spin_unlock_irqrestore(&ep->udc->lock, flags);
1159 static struct usb_ep_ops omap_ep_ops = {
1160 .enable = omap_ep_enable,
1161 .disable = omap_ep_disable,
1163 .alloc_request = omap_alloc_request,
1164 .free_request = omap_free_request,
1166 .alloc_buffer = omap_alloc_buffer,
1167 .free_buffer = omap_free_buffer,
1169 .queue = omap_ep_queue,
1170 .dequeue = omap_ep_dequeue,
1172 .set_halt = omap_ep_set_halt,
1173 // fifo_status ... report bytes in fifo
1174 // fifo_flush ... flush fifo
1177 /*-------------------------------------------------------------------------*/
1179 static int omap_get_frame(struct usb_gadget *gadget)
1181 u16 sof = UDC_SOF_REG;
1182 return (sof & UDC_TS_OK) ? (sof & UDC_TS) : -EL2NSYNC;
1185 static int omap_wakeup(struct usb_gadget *gadget)
1187 struct omap_udc *udc;
1188 unsigned long flags;
1189 int retval = -EHOSTUNREACH;
1191 udc = container_of(gadget, struct omap_udc, gadget);
1193 spin_lock_irqsave(&udc->lock, flags);
1194 if (udc->devstat & UDC_SUS) {
1195 /* NOTE: OTG spec erratum says that OTG devices may
1196 * issue wakeups without host enable.
1198 if (udc->devstat & (UDC_B_HNP_ENABLE|UDC_R_WK_OK)) {
1199 DBG("remote wakeup...\n");
1200 UDC_SYSCON2_REG = UDC_RMT_WKP;
1204 /* NOTE: non-OTG systems may use SRP TOO... */
1205 } else if (!(udc->devstat & UDC_ATT)) {
1206 if (udc->transceiver)
1207 retval = otg_start_srp(udc->transceiver);
1209 spin_unlock_irqrestore(&udc->lock, flags);
1215 omap_set_selfpowered(struct usb_gadget *gadget, int is_selfpowered)
1217 struct omap_udc *udc;
1218 unsigned long flags;
1221 udc = container_of(gadget, struct omap_udc, gadget);
1222 spin_lock_irqsave(&udc->lock, flags);
1223 syscon1 = UDC_SYSCON1_REG;
1225 syscon1 |= UDC_SELF_PWR;
1227 syscon1 &= ~UDC_SELF_PWR;
1228 UDC_SYSCON1_REG = syscon1;
1229 spin_unlock_irqrestore(&udc->lock, flags);
1234 static int can_pullup(struct omap_udc *udc)
1236 return udc->driver && udc->softconnect && udc->vbus_active;
1239 static void pullup_enable(struct omap_udc *udc)
1241 udc->gadget.dev.parent->power.power_state = PMSG_ON;
1242 udc->gadget.dev.power.power_state = PMSG_ON;
1243 UDC_SYSCON1_REG |= UDC_PULLUP_EN;
1244 #ifndef CONFIG_USB_OTG
1245 if (!cpu_is_omap15xx())
1246 OTG_CTRL_REG |= OTG_BSESSVLD;
1248 UDC_IRQ_EN_REG = UDC_DS_CHG_IE;
1251 static void pullup_disable(struct omap_udc *udc)
1253 #ifndef CONFIG_USB_OTG
1254 if (!cpu_is_omap15xx())
1255 OTG_CTRL_REG &= ~OTG_BSESSVLD;
1257 UDC_IRQ_EN_REG = UDC_DS_CHG_IE;
1258 UDC_SYSCON1_REG &= ~UDC_PULLUP_EN;
1262 * Called by whatever detects VBUS sessions: external transceiver
1263 * driver, or maybe GPIO0 VBUS IRQ. May request 48 MHz clock.
1265 static int omap_vbus_session(struct usb_gadget *gadget, int is_active)
1267 struct omap_udc *udc;
1268 unsigned long flags;
1270 udc = container_of(gadget, struct omap_udc, gadget);
1271 spin_lock_irqsave(&udc->lock, flags);
1272 VDBG("VBUS %s\n", is_active ? "on" : "off");
1273 udc->vbus_active = (is_active != 0);
1274 if (cpu_is_omap15xx()) {
1275 /* "software" detect, ignored if !VBUS_MODE_1510 */
1277 FUNC_MUX_CTRL_0_REG |= VBUS_CTRL_1510;
1279 FUNC_MUX_CTRL_0_REG &= ~VBUS_CTRL_1510;
1281 if (can_pullup(udc))
1284 pullup_disable(udc);
1285 spin_unlock_irqrestore(&udc->lock, flags);
1289 static int omap_vbus_draw(struct usb_gadget *gadget, unsigned mA)
1291 struct omap_udc *udc;
1293 udc = container_of(gadget, struct omap_udc, gadget);
1294 if (udc->transceiver)
1295 return otg_set_power(udc->transceiver, mA);
1299 static int omap_pullup(struct usb_gadget *gadget, int is_on)
1301 struct omap_udc *udc;
1302 unsigned long flags;
1304 udc = container_of(gadget, struct omap_udc, gadget);
1305 spin_lock_irqsave(&udc->lock, flags);
1306 udc->softconnect = (is_on != 0);
1307 if (can_pullup(udc))
1310 pullup_disable(udc);
1311 spin_unlock_irqrestore(&udc->lock, flags);
1315 static struct usb_gadget_ops omap_gadget_ops = {
1316 .get_frame = omap_get_frame,
1317 .wakeup = omap_wakeup,
1318 .set_selfpowered = omap_set_selfpowered,
1319 .vbus_session = omap_vbus_session,
1320 .vbus_draw = omap_vbus_draw,
1321 .pullup = omap_pullup,
1324 /*-------------------------------------------------------------------------*/
1326 /* dequeue ALL requests; caller holds udc->lock */
1327 static void nuke(struct omap_ep *ep, int status)
1329 struct omap_req *req;
1333 if (use_dma && ep->dma_channel)
1334 dma_channel_release(ep);
1337 UDC_CTRL_REG = UDC_CLR_EP;
1338 if (ep->bEndpointAddress && ep->bmAttributes != USB_ENDPOINT_XFER_ISOC)
1339 UDC_CTRL_REG = UDC_SET_HALT;
1341 while (!list_empty(&ep->queue)) {
1342 req = list_entry(ep->queue.next, struct omap_req, queue);
1343 done(ep, req, status);
1347 /* caller holds udc->lock */
1348 static void udc_quiesce(struct omap_udc *udc)
1352 udc->gadget.speed = USB_SPEED_UNKNOWN;
1353 nuke(&udc->ep[0], -ESHUTDOWN);
1354 list_for_each_entry (ep, &udc->gadget.ep_list, ep.ep_list)
1355 nuke(ep, -ESHUTDOWN);
1358 /*-------------------------------------------------------------------------*/
1360 static void update_otg(struct omap_udc *udc)
1364 if (!udc->gadget.is_otg)
1367 if (OTG_CTRL_REG & OTG_ID)
1368 devstat = UDC_DEVSTAT_REG;
1372 udc->gadget.b_hnp_enable = !!(devstat & UDC_B_HNP_ENABLE);
1373 udc->gadget.a_hnp_support = !!(devstat & UDC_A_HNP_SUPPORT);
1374 udc->gadget.a_alt_hnp_support = !!(devstat & UDC_A_ALT_HNP_SUPPORT);
1376 /* Enable HNP early, avoiding races on suspend irq path.
1377 * ASSUMES OTG state machine B_BUS_REQ input is true.
1379 if (udc->gadget.b_hnp_enable)
1380 OTG_CTRL_REG = (OTG_CTRL_REG | OTG_B_HNPEN | OTG_B_BUSREQ)
1384 static void ep0_irq(struct omap_udc *udc, u16 irq_src)
1386 struct omap_ep *ep0 = &udc->ep[0];
1387 struct omap_req *req = NULL;
1391 /* Clear any pending requests and then scrub any rx/tx state
1392 * before starting to handle the SETUP request.
1394 if (irq_src & UDC_SETUP) {
1395 u16 ack = irq_src & (UDC_EP0_TX|UDC_EP0_RX);
1399 UDC_IRQ_SRC_REG = ack;
1400 irq_src = UDC_SETUP;
1404 /* IN/OUT packets mean we're in the DATA or STATUS stage.
1405 * This driver uses only uses protocol stalls (ep0 never halts),
1406 * and if we got this far the gadget driver already had a
1407 * chance to stall. Tries to be forgiving of host oddities.
1409 * NOTE: the last chance gadget drivers have to stall control
1410 * requests is during their request completion callback.
1412 if (!list_empty(&ep0->queue))
1413 req = container_of(ep0->queue.next, struct omap_req, queue);
1415 /* IN == TX to host */
1416 if (irq_src & UDC_EP0_TX) {
1419 UDC_IRQ_SRC_REG = UDC_EP0_TX;
1420 UDC_EP_NUM_REG = UDC_EP_SEL|UDC_EP_DIR;
1421 stat = UDC_STAT_FLG_REG;
1422 if (stat & UDC_ACK) {
1424 /* write next IN packet from response,
1425 * or set up the status stage.
1428 stat = write_fifo(ep0, req);
1429 UDC_EP_NUM_REG = UDC_EP_DIR;
1430 if (!req && udc->ep0_pending) {
1431 UDC_EP_NUM_REG = UDC_EP_SEL;
1432 UDC_CTRL_REG = UDC_CLR_EP;
1433 UDC_CTRL_REG = UDC_SET_FIFO_EN;
1435 udc->ep0_pending = 0;
1436 } /* else: 6 wait states before it'll tx */
1438 /* ack status stage of OUT transfer */
1439 UDC_EP_NUM_REG = UDC_EP_DIR;
1444 } else if (stat & UDC_STALL) {
1445 UDC_CTRL_REG = UDC_CLR_HALT;
1446 UDC_EP_NUM_REG = UDC_EP_DIR;
1448 UDC_EP_NUM_REG = UDC_EP_DIR;
1452 /* OUT == RX from host */
1453 if (irq_src & UDC_EP0_RX) {
1456 UDC_IRQ_SRC_REG = UDC_EP0_RX;
1457 UDC_EP_NUM_REG = UDC_EP_SEL;
1458 stat = UDC_STAT_FLG_REG;
1459 if (stat & UDC_ACK) {
1462 /* read next OUT packet of request, maybe
1463 * reactiviting the fifo; stall on errors.
1465 if (!req || (stat = read_fifo(ep0, req)) < 0) {
1466 UDC_SYSCON2_REG = UDC_STALL_CMD;
1467 udc->ep0_pending = 0;
1469 } else if (stat == 0)
1470 UDC_CTRL_REG = UDC_SET_FIFO_EN;
1473 /* activate status stage */
1476 /* that may have STALLed ep0... */
1477 UDC_EP_NUM_REG = UDC_EP_SEL|UDC_EP_DIR;
1478 UDC_CTRL_REG = UDC_CLR_EP;
1479 UDC_CTRL_REG = UDC_SET_FIFO_EN;
1480 UDC_EP_NUM_REG = UDC_EP_DIR;
1481 udc->ep0_pending = 0;
1484 /* ack status stage of IN transfer */
1489 } else if (stat & UDC_STALL) {
1490 UDC_CTRL_REG = UDC_CLR_HALT;
1497 /* SETUP starts all control transfers */
1498 if (irq_src & UDC_SETUP) {
1501 struct usb_ctrlrequest r;
1503 int status = -EINVAL;
1506 /* read the (latest) SETUP message */
1508 UDC_EP_NUM_REG = UDC_SETUP_SEL;
1509 /* two bytes at a time */
1510 u.word[0] = UDC_DATA_REG;
1511 u.word[1] = UDC_DATA_REG;
1512 u.word[2] = UDC_DATA_REG;
1513 u.word[3] = UDC_DATA_REG;
1515 } while (UDC_IRQ_SRC_REG & UDC_SETUP);
1517 /* Delegate almost all control requests to the gadget driver,
1518 * except for a handful of ch9 status/feature requests that
1519 * hardware doesn't autodecode _and_ the gadget API hides.
1521 udc->ep0_in = (u.r.bRequestType & USB_DIR_IN) != 0;
1522 udc->ep0_set_config = 0;
1523 udc->ep0_pending = 1;
1526 switch (u.r.bRequest) {
1527 case USB_REQ_SET_CONFIGURATION:
1528 /* udc needs to know when ep != 0 is valid */
1529 if (u.r.bRequestType != USB_RECIP_DEVICE)
1531 if (u.r.wLength != 0)
1533 udc->ep0_set_config = 1;
1534 udc->ep0_reset_config = (u.r.wValue == 0);
1535 VDBG("set config %d\n", u.r.wValue);
1537 /* update udc NOW since gadget driver may start
1538 * queueing requests immediately; clear config
1539 * later if it fails the request.
1541 if (udc->ep0_reset_config)
1542 UDC_SYSCON2_REG = UDC_CLR_CFG;
1544 UDC_SYSCON2_REG = UDC_DEV_CFG;
1547 case USB_REQ_CLEAR_FEATURE:
1548 /* clear endpoint halt */
1549 if (u.r.bRequestType != USB_RECIP_ENDPOINT)
1551 if (u.r.wValue != USB_ENDPOINT_HALT
1552 || u.r.wLength != 0)
1554 ep = &udc->ep[u.r.wIndex & 0xf];
1556 if (u.r.wIndex & USB_DIR_IN)
1558 if (ep->bmAttributes == USB_ENDPOINT_XFER_ISOC
1562 UDC_CTRL_REG = UDC_RESET_EP;
1564 if (!(ep->bEndpointAddress & USB_DIR_IN)) {
1565 UDC_CTRL_REG = UDC_SET_FIFO_EN;
1566 ep->ackwait = 1 + ep->double_buf;
1568 /* NOTE: assumes the host behaves sanely,
1569 * only clearing real halts. Else we may
1570 * need to kill pending transfers and then
1571 * restart the queue... very messy for DMA!
1574 VDBG("%s halt cleared by host\n", ep->name);
1575 goto ep0out_status_stage;
1576 case USB_REQ_SET_FEATURE:
1577 /* set endpoint halt */
1578 if (u.r.bRequestType != USB_RECIP_ENDPOINT)
1580 if (u.r.wValue != USB_ENDPOINT_HALT
1581 || u.r.wLength != 0)
1583 ep = &udc->ep[u.r.wIndex & 0xf];
1584 if (u.r.wIndex & USB_DIR_IN)
1586 if (ep->bmAttributes == USB_ENDPOINT_XFER_ISOC
1587 || ep == ep0 || !ep->desc)
1589 if (use_dma && ep->has_dma) {
1590 /* this has rude side-effects (aborts) and
1591 * can't really work if DMA-IN is active
1593 DBG("%s host set_halt, NYET \n", ep->name);
1597 /* can't halt if fifo isn't empty... */
1598 UDC_CTRL_REG = UDC_CLR_EP;
1599 UDC_CTRL_REG = UDC_SET_HALT;
1600 VDBG("%s halted by host\n", ep->name);
1601 ep0out_status_stage:
1603 UDC_EP_NUM_REG = UDC_EP_SEL|UDC_EP_DIR;
1604 UDC_CTRL_REG = UDC_CLR_EP;
1605 UDC_CTRL_REG = UDC_SET_FIFO_EN;
1606 UDC_EP_NUM_REG = UDC_EP_DIR;
1607 udc->ep0_pending = 0;
1609 case USB_REQ_GET_STATUS:
1610 /* return interface status. if we were pedantic,
1611 * we'd detect non-existent interfaces, and stall.
1613 if (u.r.bRequestType
1614 != (USB_DIR_IN|USB_RECIP_INTERFACE))
1616 /* return two zero bytes */
1617 UDC_EP_NUM_REG = UDC_EP_SEL|UDC_EP_DIR;
1619 UDC_CTRL_REG = UDC_SET_FIFO_EN;
1620 UDC_EP_NUM_REG = UDC_EP_DIR;
1622 VDBG("GET_STATUS, interface %d\n", u.r.wIndex);
1623 /* next, status stage */
1627 /* activate the ep0out fifo right away */
1628 if (!udc->ep0_in && u.r.wLength) {
1630 UDC_CTRL_REG = UDC_SET_FIFO_EN;
1633 /* gadget drivers see class/vendor specific requests,
1634 * {SET,GET}_{INTERFACE,DESCRIPTOR,CONFIGURATION},
1637 VDBG("SETUP %02x.%02x v%04x i%04x l%04x\n",
1638 u.r.bRequestType, u.r.bRequest,
1639 u.r.wValue, u.r.wIndex, u.r.wLength);
1641 /* The gadget driver may return an error here,
1642 * causing an immediate protocol stall.
1644 * Else it must issue a response, either queueing a
1645 * response buffer for the DATA stage, or halting ep0
1646 * (causing a protocol stall, not a real halt). A
1647 * zero length buffer means no DATA stage.
1649 * It's fine to issue that response after the setup()
1650 * call returns, and this IRQ was handled.
1653 spin_unlock(&udc->lock);
1654 status = udc->driver->setup (&udc->gadget, &u.r);
1655 spin_lock(&udc->lock);
1661 VDBG("req %02x.%02x protocol STALL; stat %d\n",
1662 u.r.bRequestType, u.r.bRequest, status);
1663 if (udc->ep0_set_config) {
1664 if (udc->ep0_reset_config)
1665 WARN("error resetting config?\n");
1667 UDC_SYSCON2_REG = UDC_CLR_CFG;
1669 UDC_SYSCON2_REG = UDC_STALL_CMD;
1670 udc->ep0_pending = 0;
1675 /*-------------------------------------------------------------------------*/
1677 #define OTG_FLAGS (UDC_B_HNP_ENABLE|UDC_A_HNP_SUPPORT|UDC_A_ALT_HNP_SUPPORT)
1679 static void devstate_irq(struct omap_udc *udc, u16 irq_src)
1681 u16 devstat, change;
1683 devstat = UDC_DEVSTAT_REG;
1684 change = devstat ^ udc->devstat;
1685 udc->devstat = devstat;
1687 if (change & (UDC_USB_RESET|UDC_ATT)) {
1690 if (change & UDC_ATT) {
1691 /* driver for any external transceiver will
1692 * have called omap_vbus_session() already
1694 if (devstat & UDC_ATT) {
1695 udc->gadget.speed = USB_SPEED_FULL;
1697 if (!udc->transceiver)
1699 // if (driver->connect) call it
1700 } else if (udc->gadget.speed != USB_SPEED_UNKNOWN) {
1701 udc->gadget.speed = USB_SPEED_UNKNOWN;
1702 if (!udc->transceiver)
1703 pullup_disable(udc);
1704 DBG("disconnect, gadget %s\n",
1705 udc->driver->driver.name);
1706 if (udc->driver->disconnect) {
1707 spin_unlock(&udc->lock);
1708 udc->driver->disconnect(&udc->gadget);
1709 spin_lock(&udc->lock);
1715 if (change & UDC_USB_RESET) {
1716 if (devstat & UDC_USB_RESET) {
1719 udc->gadget.speed = USB_SPEED_FULL;
1720 INFO("USB reset done, gadget %s\n",
1721 udc->driver->driver.name);
1722 /* ep0 traffic is legal from now on */
1723 UDC_IRQ_EN_REG = UDC_DS_CHG_IE | UDC_EP0_IE;
1725 change &= ~UDC_USB_RESET;
1728 if (change & UDC_SUS) {
1729 if (udc->gadget.speed != USB_SPEED_UNKNOWN) {
1730 // FIXME tell isp1301 to suspend/resume (?)
1731 if (devstat & UDC_SUS) {
1734 /* HNP could be under way already */
1735 if (udc->gadget.speed == USB_SPEED_FULL
1736 && udc->driver->suspend) {
1737 spin_unlock(&udc->lock);
1738 udc->driver->suspend(&udc->gadget);
1739 spin_lock(&udc->lock);
1743 if (udc->gadget.speed == USB_SPEED_FULL
1744 && udc->driver->resume) {
1745 spin_unlock(&udc->lock);
1746 udc->driver->resume(&udc->gadget);
1747 spin_lock(&udc->lock);
1753 if (!cpu_is_omap15xx() && (change & OTG_FLAGS)) {
1755 change &= ~OTG_FLAGS;
1758 change &= ~(UDC_CFG|UDC_DEF|UDC_ADD);
1760 VDBG("devstat %03x, ignore change %03x\n",
1763 UDC_IRQ_SRC_REG = UDC_DS_CHG;
1767 omap_udc_irq(int irq, void *_udc, struct pt_regs *r)
1769 struct omap_udc *udc = _udc;
1771 irqreturn_t status = IRQ_NONE;
1772 unsigned long flags;
1774 spin_lock_irqsave(&udc->lock, flags);
1775 irq_src = UDC_IRQ_SRC_REG;
1777 /* Device state change (usb ch9 stuff) */
1778 if (irq_src & UDC_DS_CHG) {
1779 devstate_irq(_udc, irq_src);
1780 status = IRQ_HANDLED;
1781 irq_src &= ~UDC_DS_CHG;
1784 /* EP0 control transfers */
1785 if (irq_src & (UDC_EP0_RX|UDC_SETUP|UDC_EP0_TX)) {
1786 ep0_irq(_udc, irq_src);
1787 status = IRQ_HANDLED;
1788 irq_src &= ~(UDC_EP0_RX|UDC_SETUP|UDC_EP0_TX);
1791 /* DMA transfer completion */
1792 if (use_dma && (irq_src & (UDC_TXN_DONE|UDC_RXN_CNT|UDC_RXN_EOT))) {
1793 dma_irq(_udc, irq_src);
1794 status = IRQ_HANDLED;
1795 irq_src &= ~(UDC_TXN_DONE|UDC_RXN_CNT|UDC_RXN_EOT);
1798 irq_src &= ~(UDC_SOF|UDC_EPN_TX|UDC_EPN_RX);
1800 DBG("udc_irq, unhandled %03x\n", irq_src);
1801 spin_unlock_irqrestore(&udc->lock, flags);
1806 /* workaround for seemingly-lost IRQs for RX ACKs... */
1807 #define PIO_OUT_TIMEOUT (jiffies + HZ/3)
1808 #define HALF_FULL(f) (!((f)&(UDC_NON_ISO_FIFO_FULL|UDC_NON_ISO_FIFO_EMPTY)))
1810 static void pio_out_timer(unsigned long _ep)
1812 struct omap_ep *ep = (void *) _ep;
1813 unsigned long flags;
1816 spin_lock_irqsave(&ep->udc->lock, flags);
1817 if (!list_empty(&ep->queue) && ep->ackwait) {
1819 stat_flg = UDC_STAT_FLG_REG;
1821 if ((stat_flg & UDC_ACK) && (!(stat_flg & UDC_FIFO_EN)
1822 || (ep->double_buf && HALF_FULL(stat_flg)))) {
1823 struct omap_req *req;
1825 VDBG("%s: lose, %04x\n", ep->ep.name, stat_flg);
1826 req = container_of(ep->queue.next,
1827 struct omap_req, queue);
1828 UDC_EP_NUM_REG = ep->bEndpointAddress | UDC_EP_SEL;
1829 (void) read_fifo(ep, req);
1830 UDC_EP_NUM_REG = ep->bEndpointAddress;
1831 UDC_CTRL_REG = UDC_SET_FIFO_EN;
1832 ep->ackwait = 1 + ep->double_buf;
1835 mod_timer(&ep->timer, PIO_OUT_TIMEOUT);
1836 spin_unlock_irqrestore(&ep->udc->lock, flags);
1840 omap_udc_pio_irq(int irq, void *_dev, struct pt_regs *r)
1842 u16 epn_stat, irq_src;
1843 irqreturn_t status = IRQ_NONE;
1846 struct omap_udc *udc = _dev;
1847 struct omap_req *req;
1848 unsigned long flags;
1850 spin_lock_irqsave(&udc->lock, flags);
1851 epn_stat = UDC_EPN_STAT_REG;
1852 irq_src = UDC_IRQ_SRC_REG;
1854 /* handle OUT first, to avoid some wasteful NAKs */
1855 if (irq_src & UDC_EPN_RX) {
1856 epnum = (epn_stat >> 8) & 0x0f;
1857 UDC_IRQ_SRC_REG = UDC_EPN_RX;
1858 status = IRQ_HANDLED;
1859 ep = &udc->ep[epnum];
1862 UDC_EP_NUM_REG = epnum | UDC_EP_SEL;
1864 if ((UDC_STAT_FLG_REG & UDC_ACK)) {
1866 if (!list_empty(&ep->queue)) {
1868 req = container_of(ep->queue.next,
1869 struct omap_req, queue);
1870 stat = read_fifo(ep, req);
1871 if (!ep->double_buf)
1875 /* min 6 clock delay before clearing EP_SEL ... */
1876 epn_stat = UDC_EPN_STAT_REG;
1877 epn_stat = UDC_EPN_STAT_REG;
1878 UDC_EP_NUM_REG = epnum;
1880 /* enabling fifo _after_ clearing ACK, contrary to docs,
1881 * reduces lossage; timer still needed though (sigh).
1884 UDC_CTRL_REG = UDC_SET_FIFO_EN;
1885 ep->ackwait = 1 + ep->double_buf;
1887 mod_timer(&ep->timer, PIO_OUT_TIMEOUT);
1890 /* then IN transfers */
1891 else if (irq_src & UDC_EPN_TX) {
1892 epnum = epn_stat & 0x0f;
1893 UDC_IRQ_SRC_REG = UDC_EPN_TX;
1894 status = IRQ_HANDLED;
1895 ep = &udc->ep[16 + epnum];
1898 UDC_EP_NUM_REG = epnum | UDC_EP_DIR | UDC_EP_SEL;
1899 if ((UDC_STAT_FLG_REG & UDC_ACK)) {
1901 if (!list_empty(&ep->queue)) {
1902 req = container_of(ep->queue.next,
1903 struct omap_req, queue);
1904 (void) write_fifo(ep, req);
1907 /* min 6 clock delay before clearing EP_SEL ... */
1908 epn_stat = UDC_EPN_STAT_REG;
1909 epn_stat = UDC_EPN_STAT_REG;
1910 UDC_EP_NUM_REG = epnum | UDC_EP_DIR;
1911 /* then 6 clocks before it'd tx */
1914 spin_unlock_irqrestore(&udc->lock, flags);
1920 omap_udc_iso_irq(int irq, void *_dev, struct pt_regs *r)
1922 struct omap_udc *udc = _dev;
1925 unsigned long flags;
1927 spin_lock_irqsave(&udc->lock, flags);
1929 /* handle all non-DMA ISO transfers */
1930 list_for_each_entry (ep, &udc->iso, iso) {
1932 struct omap_req *req;
1934 if (ep->has_dma || list_empty(&ep->queue))
1936 req = list_entry(ep->queue.next, struct omap_req, queue);
1938 use_ep(ep, UDC_EP_SEL);
1939 stat = UDC_STAT_FLG_REG;
1941 /* NOTE: like the other controller drivers, this isn't
1942 * currently reporting lost or damaged frames.
1944 if (ep->bEndpointAddress & USB_DIR_IN) {
1945 if (stat & UDC_MISS_IN)
1946 /* done(ep, req, -EPROTO) */;
1948 write_fifo(ep, req);
1952 if (stat & UDC_NO_RXPACKET)
1953 status = -EREMOTEIO;
1954 else if (stat & UDC_ISO_ERR)
1956 else if (stat & UDC_DATA_FLUSH)
1960 /* done(ep, req, status) */;
1965 /* 6 wait states before next EP */
1968 if (!list_empty(&ep->queue))
1972 UDC_IRQ_EN_REG &= ~UDC_SOF_IE;
1973 UDC_IRQ_SRC_REG = UDC_SOF;
1975 spin_unlock_irqrestore(&udc->lock, flags);
1980 /*-------------------------------------------------------------------------*/
1982 static struct omap_udc *udc;
1984 int usb_gadget_register_driver (struct usb_gadget_driver *driver)
1986 int status = -ENODEV;
1988 unsigned long flags;
1990 /* basic sanity tests */
1994 // FIXME if otg, check: driver->is_otg
1995 || driver->speed < USB_SPEED_FULL
2001 spin_lock_irqsave(&udc->lock, flags);
2003 spin_unlock_irqrestore(&udc->lock, flags);
2008 list_for_each_entry (ep, &udc->gadget.ep_list, ep.ep_list) {
2010 if (ep->bmAttributes == USB_ENDPOINT_XFER_ISOC)
2013 UDC_CTRL_REG = UDC_SET_HALT;
2015 udc->ep0_pending = 0;
2016 udc->ep[0].irqs = 0;
2017 udc->softconnect = 1;
2019 /* hook up the driver */
2020 driver->driver.bus = NULL;
2021 udc->driver = driver;
2022 udc->gadget.dev.driver = &driver->driver;
2023 spin_unlock_irqrestore(&udc->lock, flags);
2025 status = driver->bind (&udc->gadget);
2027 DBG("bind to %s --> %d\n", driver->driver.name, status);
2028 udc->gadget.dev.driver = NULL;
2032 DBG("bound to driver %s\n", driver->driver.name);
2034 UDC_IRQ_SRC_REG = UDC_IRQ_SRC_MASK;
2036 /* connect to bus through transceiver */
2037 if (udc->transceiver) {
2038 status = otg_set_peripheral(udc->transceiver, &udc->gadget);
2040 ERR("can't bind to transceiver\n");
2041 driver->unbind (&udc->gadget);
2042 udc->gadget.dev.driver = NULL;
2047 if (can_pullup(udc))
2048 pullup_enable (udc);
2050 pullup_disable (udc);
2053 /* boards that don't have VBUS sensing can't autogate 48MHz;
2054 * can't enter deep sleep while a gadget driver is active.
2056 if (machine_is_omap_innovator() || machine_is_omap_osk())
2057 omap_vbus_session(&udc->gadget, 1);
2062 EXPORT_SYMBOL(usb_gadget_register_driver);
2064 int usb_gadget_unregister_driver (struct usb_gadget_driver *driver)
2066 unsigned long flags;
2067 int status = -ENODEV;
2071 if (!driver || driver != udc->driver)
2074 if (machine_is_omap_innovator() || machine_is_omap_osk())
2075 omap_vbus_session(&udc->gadget, 0);
2077 if (udc->transceiver)
2078 (void) otg_set_peripheral(udc->transceiver, NULL);
2080 pullup_disable(udc);
2082 spin_lock_irqsave(&udc->lock, flags);
2084 spin_unlock_irqrestore(&udc->lock, flags);
2086 driver->unbind(&udc->gadget);
2087 udc->gadget.dev.driver = NULL;
2090 DBG("unregistered driver '%s'\n", driver->driver.name);
2093 EXPORT_SYMBOL(usb_gadget_unregister_driver);
2096 /*-------------------------------------------------------------------------*/
2098 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
2100 #include <linux/seq_file.h>
2102 static const char proc_filename[] = "driver/udc";
2104 #define FOURBITS "%s%s%s%s"
2105 #define EIGHTBITS FOURBITS FOURBITS
2107 static void proc_ep_show(struct seq_file *s, struct omap_ep *ep)
2110 struct omap_req *req;
2115 if (use_dma && ep->has_dma)
2116 snprintf(buf, sizeof buf, "(%cxdma%d lch%d) ",
2117 (ep->bEndpointAddress & USB_DIR_IN) ? 't' : 'r',
2118 ep->dma_channel - 1, ep->lch);
2122 stat_flg = UDC_STAT_FLG_REG;
2124 "\n%s %s%s%sirqs %ld stat %04x " EIGHTBITS FOURBITS "%s\n",
2126 ep->double_buf ? "dbuf " : "",
2127 ({char *s; switch(ep->ackwait){
2128 case 0: s = ""; break;
2129 case 1: s = "(ackw) "; break;
2130 case 2: s = "(ackw2) "; break;
2131 default: s = "(?) "; break;
2134 (stat_flg & UDC_NO_RXPACKET) ? "no_rxpacket " : "",
2135 (stat_flg & UDC_MISS_IN) ? "miss_in " : "",
2136 (stat_flg & UDC_DATA_FLUSH) ? "data_flush " : "",
2137 (stat_flg & UDC_ISO_ERR) ? "iso_err " : "",
2138 (stat_flg & UDC_ISO_FIFO_EMPTY) ? "iso_fifo_empty " : "",
2139 (stat_flg & UDC_ISO_FIFO_FULL) ? "iso_fifo_full " : "",
2140 (stat_flg & UDC_EP_HALTED) ? "HALT " : "",
2141 (stat_flg & UDC_STALL) ? "STALL " : "",
2142 (stat_flg & UDC_NAK) ? "NAK " : "",
2143 (stat_flg & UDC_ACK) ? "ACK " : "",
2144 (stat_flg & UDC_FIFO_EN) ? "fifo_en " : "",
2145 (stat_flg & UDC_NON_ISO_FIFO_EMPTY) ? "fifo_empty " : "",
2146 (stat_flg & UDC_NON_ISO_FIFO_FULL) ? "fifo_full " : "");
2148 if (list_empty (&ep->queue))
2149 seq_printf(s, "\t(queue empty)\n");
2151 list_for_each_entry (req, &ep->queue, queue) {
2152 unsigned length = req->req.actual;
2154 if (use_dma && buf[0]) {
2155 length += ((ep->bEndpointAddress & USB_DIR_IN)
2156 ? dma_src_len : dma_dest_len)
2157 (ep, req->req.dma + length);
2160 seq_printf(s, "\treq %p len %d/%d buf %p\n",
2162 req->req.length, req->req.buf);
2166 static char *trx_mode(unsigned m, int enabled)
2169 case 0: return enabled ? "*6wire" : "unused";
2170 case 1: return "4wire";
2171 case 2: return "3wire";
2172 case 3: return "6wire";
2173 default: return "unknown";
2177 static int proc_otg_show(struct seq_file *s)
2183 trans = USB_TRANSCEIVER_CTRL_REG;
2184 seq_printf(s, "\nOTG rev %d.%d, transceiver_ctrl %03x\n",
2185 tmp >> 4, tmp & 0xf, trans);
2186 tmp = OTG_SYSCON_1_REG;
2187 seq_printf(s, "otg_syscon1 %08x usb2 %s, usb1 %s, usb0 %s,"
2189 trx_mode(USB2_TRX_MODE(tmp), trans & CONF_USB2_UNI_R),
2190 trx_mode(USB1_TRX_MODE(tmp), trans & CONF_USB1_UNI_R),
2191 (USB0_TRX_MODE(tmp) == 0)
2193 : trx_mode(USB0_TRX_MODE(tmp), 1),
2194 (tmp & OTG_IDLE_EN) ? " !otg" : "",
2195 (tmp & HST_IDLE_EN) ? " !host" : "",
2196 (tmp & DEV_IDLE_EN) ? " !dev" : "",
2197 (tmp & OTG_RESET_DONE) ? " reset_done" : " reset_active");
2198 tmp = OTG_SYSCON_2_REG;
2199 seq_printf(s, "otg_syscon2 %08x%s" EIGHTBITS
2200 " b_ase_brst=%d hmc=%d\n", tmp,
2201 (tmp & OTG_EN) ? " otg_en" : "",
2202 (tmp & USBX_SYNCHRO) ? " synchro" : "",
2203 // much more SRP stuff
2204 (tmp & SRP_DATA) ? " srp_data" : "",
2205 (tmp & SRP_VBUS) ? " srp_vbus" : "",
2206 (tmp & OTG_PADEN) ? " otg_paden" : "",
2207 (tmp & HMC_PADEN) ? " hmc_paden" : "",
2208 (tmp & UHOST_EN) ? " uhost_en" : "",
2209 (tmp & HMC_TLLSPEED) ? " tllspeed" : "",
2210 (tmp & HMC_TLLATTACH) ? " tllattach" : "",
2214 seq_printf(s, "otg_ctrl %06x" EIGHTBITS EIGHTBITS "%s\n", tmp,
2215 (tmp & OTG_ASESSVLD) ? " asess" : "",
2216 (tmp & OTG_BSESSEND) ? " bsess_end" : "",
2217 (tmp & OTG_BSESSVLD) ? " bsess" : "",
2218 (tmp & OTG_VBUSVLD) ? " vbus" : "",
2219 (tmp & OTG_ID) ? " id" : "",
2220 (tmp & OTG_DRIVER_SEL) ? " DEVICE" : " HOST",
2221 (tmp & OTG_A_SETB_HNPEN) ? " a_setb_hnpen" : "",
2222 (tmp & OTG_A_BUSREQ) ? " a_bus" : "",
2223 (tmp & OTG_B_HNPEN) ? " b_hnpen" : "",
2224 (tmp & OTG_B_BUSREQ) ? " b_bus" : "",
2225 (tmp & OTG_BUSDROP) ? " busdrop" : "",
2226 (tmp & OTG_PULLDOWN) ? " down" : "",
2227 (tmp & OTG_PULLUP) ? " up" : "",
2228 (tmp & OTG_DRV_VBUS) ? " drv" : "",
2229 (tmp & OTG_PD_VBUS) ? " pd_vb" : "",
2230 (tmp & OTG_PU_VBUS) ? " pu_vb" : "",
2231 (tmp & OTG_PU_ID) ? " pu_id" : ""
2233 tmp = OTG_IRQ_EN_REG;
2234 seq_printf(s, "otg_irq_en %04x" "\n", tmp);
2235 tmp = OTG_IRQ_SRC_REG;
2236 seq_printf(s, "otg_irq_src %04x" "\n", tmp);
2237 tmp = OTG_OUTCTRL_REG;
2238 seq_printf(s, "otg_outctrl %04x" "\n", tmp);
2240 seq_printf(s, "otg_test %04x" "\n", tmp);
2244 static int proc_udc_show(struct seq_file *s, void *_)
2248 unsigned long flags;
2250 spin_lock_irqsave(&udc->lock, flags);
2252 seq_printf(s, "%s, version: " DRIVER_VERSION
2258 use_dma ? " (dma)" : "");
2260 tmp = UDC_REV_REG & 0xff;
2262 "UDC rev %d.%d, fifo mode %d, gadget %s\n"
2263 "hmc %d, transceiver %s\n",
2264 tmp >> 4, tmp & 0xf,
2266 udc->driver ? udc->driver->driver.name : "(none)",
2268 udc->transceiver ? udc->transceiver->label : "(none)");
2269 seq_printf(s, "ULPD control %04x req %04x status %04x\n",
2270 __REG16(ULPD_CLOCK_CTRL),
2271 __REG16(ULPD_SOFT_REQ),
2272 __REG16(ULPD_STATUS_REQ));
2274 /* OTG controller registers */
2275 if (!cpu_is_omap15xx())
2278 tmp = UDC_SYSCON1_REG;
2279 seq_printf(s, "\nsyscon1 %04x" EIGHTBITS "\n", tmp,
2280 (tmp & UDC_CFG_LOCK) ? " cfg_lock" : "",
2281 (tmp & UDC_DATA_ENDIAN) ? " data_endian" : "",
2282 (tmp & UDC_DMA_ENDIAN) ? " dma_endian" : "",
2283 (tmp & UDC_NAK_EN) ? " nak" : "",
2284 (tmp & UDC_AUTODECODE_DIS) ? " autodecode_dis" : "",
2285 (tmp & UDC_SELF_PWR) ? " self_pwr" : "",
2286 (tmp & UDC_SOFF_DIS) ? " soff_dis" : "",
2287 (tmp & UDC_PULLUP_EN) ? " PULLUP" : "");
2288 // syscon2 is write-only
2290 /* UDC controller registers */
2291 if (!(tmp & UDC_PULLUP_EN)) {
2292 seq_printf(s, "(suspended)\n");
2293 spin_unlock_irqrestore(&udc->lock, flags);
2297 tmp = UDC_DEVSTAT_REG;
2298 seq_printf(s, "devstat %04x" EIGHTBITS "%s%s\n", tmp,
2299 (tmp & UDC_B_HNP_ENABLE) ? " b_hnp" : "",
2300 (tmp & UDC_A_HNP_SUPPORT) ? " a_hnp" : "",
2301 (tmp & UDC_A_ALT_HNP_SUPPORT) ? " a_alt_hnp" : "",
2302 (tmp & UDC_R_WK_OK) ? " r_wk_ok" : "",
2303 (tmp & UDC_USB_RESET) ? " usb_reset" : "",
2304 (tmp & UDC_SUS) ? " SUS" : "",
2305 (tmp & UDC_CFG) ? " CFG" : "",
2306 (tmp & UDC_ADD) ? " ADD" : "",
2307 (tmp & UDC_DEF) ? " DEF" : "",
2308 (tmp & UDC_ATT) ? " ATT" : "");
2309 seq_printf(s, "sof %04x\n", UDC_SOF_REG);
2310 tmp = UDC_IRQ_EN_REG;
2311 seq_printf(s, "irq_en %04x" FOURBITS "%s\n", tmp,
2312 (tmp & UDC_SOF_IE) ? " sof" : "",
2313 (tmp & UDC_EPN_RX_IE) ? " epn_rx" : "",
2314 (tmp & UDC_EPN_TX_IE) ? " epn_tx" : "",
2315 (tmp & UDC_DS_CHG_IE) ? " ds_chg" : "",
2316 (tmp & UDC_EP0_IE) ? " ep0" : "");
2317 tmp = UDC_IRQ_SRC_REG;
2318 seq_printf(s, "irq_src %04x" EIGHTBITS "%s%s\n", tmp,
2319 (tmp & UDC_TXN_DONE) ? " txn_done" : "",
2320 (tmp & UDC_RXN_CNT) ? " rxn_cnt" : "",
2321 (tmp & UDC_RXN_EOT) ? " rxn_eot" : "",
2322 (tmp & UDC_SOF) ? " sof" : "",
2323 (tmp & UDC_EPN_RX) ? " epn_rx" : "",
2324 (tmp & UDC_EPN_TX) ? " epn_tx" : "",
2325 (tmp & UDC_DS_CHG) ? " ds_chg" : "",
2326 (tmp & UDC_SETUP) ? " setup" : "",
2327 (tmp & UDC_EP0_RX) ? " ep0out" : "",
2328 (tmp & UDC_EP0_TX) ? " ep0in" : "");
2332 tmp = UDC_DMA_IRQ_EN_REG;
2333 seq_printf(s, "dma_irq_en %04x%s" EIGHTBITS "\n", tmp,
2334 (tmp & UDC_TX_DONE_IE(3)) ? " tx2_done" : "",
2335 (tmp & UDC_RX_CNT_IE(3)) ? " rx2_cnt" : "",
2336 (tmp & UDC_RX_EOT_IE(3)) ? " rx2_eot" : "",
2338 (tmp & UDC_TX_DONE_IE(2)) ? " tx1_done" : "",
2339 (tmp & UDC_RX_CNT_IE(2)) ? " rx1_cnt" : "",
2340 (tmp & UDC_RX_EOT_IE(2)) ? " rx1_eot" : "",
2342 (tmp & UDC_TX_DONE_IE(1)) ? " tx0_done" : "",
2343 (tmp & UDC_RX_CNT_IE(1)) ? " rx0_cnt" : "",
2344 (tmp & UDC_RX_EOT_IE(1)) ? " rx0_eot" : "");
2346 tmp = UDC_RXDMA_CFG_REG;
2347 seq_printf(s, "rxdma_cfg %04x\n", tmp);
2349 for (i = 0; i < 3; i++) {
2350 if ((tmp & (0x0f << (i * 4))) == 0)
2352 seq_printf(s, "rxdma[%d] %04x\n", i,
2353 UDC_RXDMA_REG(i + 1));
2356 tmp = UDC_TXDMA_CFG_REG;
2357 seq_printf(s, "txdma_cfg %04x\n", tmp);
2359 for (i = 0; i < 3; i++) {
2360 if (!(tmp & (0x0f << (i * 4))))
2362 seq_printf(s, "txdma[%d] %04x\n", i,
2363 UDC_TXDMA_REG(i + 1));
2368 tmp = UDC_DEVSTAT_REG;
2369 if (tmp & UDC_ATT) {
2370 proc_ep_show(s, &udc->ep[0]);
2371 if (tmp & UDC_ADD) {
2372 list_for_each_entry (ep, &udc->gadget.ep_list,
2375 proc_ep_show(s, ep);
2379 spin_unlock_irqrestore(&udc->lock, flags);
2383 static int proc_udc_open(struct inode *inode, struct file *file)
2385 return single_open(file, proc_udc_show, NULL);
2388 static struct file_operations proc_ops = {
2389 .open = proc_udc_open,
2391 .llseek = seq_lseek,
2392 .release = single_release,
2395 static void create_proc_file(void)
2397 struct proc_dir_entry *pde;
2399 pde = create_proc_entry (proc_filename, 0, NULL);
2401 pde->proc_fops = &proc_ops;
2404 static void remove_proc_file(void)
2406 remove_proc_entry(proc_filename, NULL);
2411 static inline void create_proc_file(void) {}
2412 static inline void remove_proc_file(void) {}
2416 /*-------------------------------------------------------------------------*/
2418 /* Before this controller can enumerate, we need to pick an endpoint
2419 * configuration, or "fifo_mode" That involves allocating 2KB of packet
2420 * buffer space among the endpoints we'll be operating.
2422 static unsigned __init
2423 omap_ep_setup(char *name, u8 addr, u8 type,
2424 unsigned buf, unsigned maxp, int dbuf)
2429 /* OUT endpoints first, then IN */
2430 ep = &udc->ep[addr & 0xf];
2431 if (addr & USB_DIR_IN)
2434 /* in case of ep init table bugs */
2435 BUG_ON(ep->name[0]);
2437 /* chip setup ... bit values are same for IN, OUT */
2438 if (type == USB_ENDPOINT_XFER_ISOC) {
2440 case 8: epn_rxtx = 0 << 12; break;
2441 case 16: epn_rxtx = 1 << 12; break;
2442 case 32: epn_rxtx = 2 << 12; break;
2443 case 64: epn_rxtx = 3 << 12; break;
2444 case 128: epn_rxtx = 4 << 12; break;
2445 case 256: epn_rxtx = 5 << 12; break;
2446 case 512: epn_rxtx = 6 << 12; break;
2449 epn_rxtx |= UDC_EPN_RX_ISO;
2452 /* double-buffering "not supported" on 15xx,
2453 * and ignored for PIO-IN on 16xx
2455 if (!use_dma || cpu_is_omap15xx())
2459 case 8: epn_rxtx = 0 << 12; break;
2460 case 16: epn_rxtx = 1 << 12; break;
2461 case 32: epn_rxtx = 2 << 12; break;
2462 case 64: epn_rxtx = 3 << 12; break;
2466 epn_rxtx |= UDC_EPN_RX_DB;
2467 init_timer(&ep->timer);
2468 ep->timer.function = pio_out_timer;
2469 ep->timer.data = (unsigned long) ep;
2472 epn_rxtx |= UDC_EPN_RX_VALID;
2474 epn_rxtx |= buf >> 3;
2476 DBG("%s addr %02x rxtx %04x maxp %d%s buf %d\n",
2477 name, addr, epn_rxtx, maxp, dbuf ? "x2" : "", buf);
2479 if (addr & USB_DIR_IN)
2480 UDC_EP_TX_REG(addr & 0xf) = epn_rxtx;
2482 UDC_EP_RX_REG(addr) = epn_rxtx;
2484 /* next endpoint's buffer starts after this one's */
2490 /* set up driver data structures */
2491 BUG_ON(strlen(name) >= sizeof ep->name);
2492 strlcpy(ep->name, name, sizeof ep->name);
2493 INIT_LIST_HEAD(&ep->queue);
2494 INIT_LIST_HEAD(&ep->iso);
2495 ep->bEndpointAddress = addr;
2496 ep->bmAttributes = type;
2497 ep->double_buf = dbuf;
2500 ep->ep.name = ep->name;
2501 ep->ep.ops = &omap_ep_ops;
2502 ep->ep.maxpacket = ep->maxpacket = maxp;
2503 list_add_tail (&ep->ep.ep_list, &udc->gadget.ep_list);
2508 static void omap_udc_release(struct device *dev)
2510 complete(udc->done);
2516 omap_udc_setup(struct platform_device *odev, struct otg_transceiver *xceiv)
2520 /* abolish any previous hardware state */
2521 UDC_SYSCON1_REG = 0;
2523 UDC_IRQ_SRC_REG = UDC_IRQ_SRC_MASK;
2524 UDC_DMA_IRQ_EN_REG = 0;
2525 UDC_RXDMA_CFG_REG = 0;
2526 UDC_TXDMA_CFG_REG = 0;
2528 /* UDC_PULLUP_EN gates the chip clock */
2529 // OTG_SYSCON_1_REG |= DEV_IDLE_EN;
2531 udc = kmalloc (sizeof *udc, SLAB_KERNEL);
2535 memset(udc, 0, sizeof *udc);
2536 spin_lock_init (&udc->lock);
2538 udc->gadget.ops = &omap_gadget_ops;
2539 udc->gadget.ep0 = &udc->ep[0].ep;
2540 INIT_LIST_HEAD(&udc->gadget.ep_list);
2541 INIT_LIST_HEAD(&udc->iso);
2542 udc->gadget.speed = USB_SPEED_UNKNOWN;
2543 udc->gadget.name = driver_name;
2545 device_initialize(&udc->gadget.dev);
2546 strcpy (udc->gadget.dev.bus_id, "gadget");
2547 udc->gadget.dev.release = omap_udc_release;
2548 udc->gadget.dev.parent = &odev->dev;
2550 udc->gadget.dev.dma_mask = odev->dev.dma_mask;
2552 udc->transceiver = xceiv;
2554 /* ep0 is special; put it right after the SETUP buffer */
2555 buf = omap_ep_setup("ep0", 0, USB_ENDPOINT_XFER_CONTROL,
2556 8 /* after SETUP */, 64 /* maxpacket */, 0);
2557 list_del_init(&udc->ep[0].ep.ep_list);
2559 /* initially disable all non-ep0 endpoints */
2560 for (tmp = 1; tmp < 15; tmp++) {
2561 UDC_EP_RX_REG(tmp) = 0;
2562 UDC_EP_TX_REG(tmp) = 0;
2565 #define OMAP_BULK_EP(name,addr) \
2566 buf = omap_ep_setup(name "-bulk", addr, \
2567 USB_ENDPOINT_XFER_BULK, buf, 64, 1);
2568 #define OMAP_INT_EP(name,addr, maxp) \
2569 buf = omap_ep_setup(name "-int", addr, \
2570 USB_ENDPOINT_XFER_INT, buf, maxp, 0);
2571 #define OMAP_ISO_EP(name,addr, maxp) \
2572 buf = omap_ep_setup(name "-iso", addr, \
2573 USB_ENDPOINT_XFER_ISOC, buf, maxp, 1);
2575 switch (fifo_mode) {
2577 OMAP_BULK_EP("ep1in", USB_DIR_IN | 1);
2578 OMAP_BULK_EP("ep2out", USB_DIR_OUT | 2);
2579 OMAP_INT_EP("ep3in", USB_DIR_IN | 3, 16);
2582 OMAP_BULK_EP("ep1in", USB_DIR_IN | 1);
2583 OMAP_BULK_EP("ep2out", USB_DIR_OUT | 2);
2584 OMAP_INT_EP("ep9in", USB_DIR_IN | 9, 16);
2586 OMAP_BULK_EP("ep3in", USB_DIR_IN | 3);
2587 OMAP_BULK_EP("ep4out", USB_DIR_OUT | 4);
2588 OMAP_INT_EP("ep10in", USB_DIR_IN | 10, 16);
2590 OMAP_BULK_EP("ep5in", USB_DIR_IN | 5);
2591 OMAP_BULK_EP("ep5out", USB_DIR_OUT | 5);
2592 OMAP_INT_EP("ep11in", USB_DIR_IN | 11, 16);
2594 OMAP_BULK_EP("ep6in", USB_DIR_IN | 6);
2595 OMAP_BULK_EP("ep6out", USB_DIR_OUT | 6);
2596 OMAP_INT_EP("ep12in", USB_DIR_IN | 12, 16);
2598 OMAP_BULK_EP("ep7in", USB_DIR_IN | 7);
2599 OMAP_BULK_EP("ep7out", USB_DIR_OUT | 7);
2600 OMAP_INT_EP("ep13in", USB_DIR_IN | 13, 16);
2601 OMAP_INT_EP("ep13out", USB_DIR_OUT | 13, 16);
2603 OMAP_BULK_EP("ep8in", USB_DIR_IN | 8);
2604 OMAP_BULK_EP("ep8out", USB_DIR_OUT | 8);
2605 OMAP_INT_EP("ep14in", USB_DIR_IN | 14, 16);
2606 OMAP_INT_EP("ep14out", USB_DIR_OUT | 14, 16);
2608 OMAP_BULK_EP("ep15in", USB_DIR_IN | 15);
2609 OMAP_BULK_EP("ep15out", USB_DIR_OUT | 15);
2614 case 2: /* mixed iso/bulk */
2615 OMAP_ISO_EP("ep1in", USB_DIR_IN | 1, 256);
2616 OMAP_ISO_EP("ep2out", USB_DIR_OUT | 2, 256);
2617 OMAP_ISO_EP("ep3in", USB_DIR_IN | 3, 128);
2618 OMAP_ISO_EP("ep4out", USB_DIR_OUT | 4, 128);
2620 OMAP_INT_EP("ep5in", USB_DIR_IN | 5, 16);
2622 OMAP_BULK_EP("ep6in", USB_DIR_IN | 6);
2623 OMAP_BULK_EP("ep7out", USB_DIR_OUT | 7);
2624 OMAP_INT_EP("ep8in", USB_DIR_IN | 8, 16);
2626 case 3: /* mixed bulk/iso */
2627 OMAP_BULK_EP("ep1in", USB_DIR_IN | 1);
2628 OMAP_BULK_EP("ep2out", USB_DIR_OUT | 2);
2629 OMAP_INT_EP("ep3in", USB_DIR_IN | 3, 16);
2631 OMAP_BULK_EP("ep4in", USB_DIR_IN | 4);
2632 OMAP_BULK_EP("ep5out", USB_DIR_OUT | 5);
2633 OMAP_INT_EP("ep6in", USB_DIR_IN | 6, 16);
2635 OMAP_ISO_EP("ep7in", USB_DIR_IN | 7, 256);
2636 OMAP_ISO_EP("ep8out", USB_DIR_OUT | 8, 256);
2637 OMAP_INT_EP("ep9in", USB_DIR_IN | 9, 16);
2641 /* add more modes as needed */
2644 ERR("unsupported fifo_mode #%d\n", fifo_mode);
2647 UDC_SYSCON1_REG = UDC_CFG_LOCK|UDC_SELF_PWR;
2648 INFO("fifo mode %d, %d bytes not used\n", fifo_mode, 2048 - buf);
2652 static int __init omap_udc_probe(struct device *dev)
2654 struct platform_device *odev = to_platform_device(dev);
2655 int status = -ENODEV;
2657 struct otg_transceiver *xceiv = NULL;
2658 const char *type = NULL;
2659 struct omap_usb_config *config = dev->platform_data;
2661 /* NOTE: "knows" the order of the resources! */
2662 if (!request_mem_region(odev->resource[0].start,
2663 odev->resource[0].end - odev->resource[0].start + 1,
2665 DBG("request_mem_region failed\n");
2669 INFO("OMAP UDC rev %d.%d%s\n",
2670 UDC_REV_REG >> 4, UDC_REV_REG & 0xf,
2671 config->otg ? ", Mini-AB" : "");
2673 /* use the mode given to us by board init code */
2674 if (cpu_is_omap15xx()) {
2678 if (machine_is_omap_innovator()) {
2679 /* just set up software VBUS detect, and then
2680 * later rig it so we always report VBUS.
2681 * FIXME without really sensing VBUS, we can't
2682 * know when to turn PULLUP_EN on/off; and that
2683 * means we always "need" the 48MHz clock.
2685 u32 tmp = FUNC_MUX_CTRL_0_REG;
2687 FUNC_MUX_CTRL_0_REG &= ~VBUS_CTRL_1510;
2688 tmp |= VBUS_MODE_1510;
2689 tmp &= ~VBUS_CTRL_1510;
2690 FUNC_MUX_CTRL_0_REG = tmp;
2695 case 0: /* POWERUP DEFAULT == 0 */
2699 if (!cpu_is_omap1710()) {
2700 type = "integrated";
2709 xceiv = otg_get_transceiver();
2711 DBG("external transceiver not registered!\n");
2716 type = xceiv->label;
2718 case 21: /* internal loopback */
2721 case 14: /* transceiverless */
2726 ERR("unrecognized UDC HMC mode %d\n", hmc);
2730 INFO("hmc mode %d, %s transceiver\n", hmc, type);
2732 /* a "gadget" abstracts/virtualizes the controller */
2733 status = omap_udc_setup(odev, xceiv);
2738 // "udc" is now valid
2739 pullup_disable(udc);
2740 #if defined(CONFIG_USB_OHCI_HCD) || defined(CONFIG_USB_OHCI_HCD_MODULE)
2741 udc->gadget.is_otg = (config->otg != 0);
2744 /* USB general purpose IRQ: ep0, state changes, dma, etc */
2745 status = request_irq(odev->resource[1].start, omap_udc_irq,
2746 SA_SAMPLE_RANDOM, driver_name, udc);
2748 ERR( "can't get irq %ld, err %d\n",
2749 odev->resource[1].start, status);
2753 /* USB "non-iso" IRQ (PIO for all but ep0) */
2754 status = request_irq(odev->resource[2].start, omap_udc_pio_irq,
2755 SA_SAMPLE_RANDOM, "omap_udc pio", udc);
2757 ERR( "can't get irq %ld, err %d\n",
2758 odev->resource[2].start, status);
2762 status = request_irq(odev->resource[3].start, omap_udc_iso_irq,
2763 SA_INTERRUPT, "omap_udc iso", udc);
2765 ERR("can't get irq %ld, err %d\n",
2766 odev->resource[3].start, status);
2772 device_add(&udc->gadget.dev);
2777 free_irq(odev->resource[2].start, udc);
2781 free_irq(odev->resource[1].start, udc);
2789 put_device(xceiv->dev);
2790 release_mem_region(odev->resource[0].start,
2791 odev->resource[0].end - odev->resource[0].start + 1);
2795 static int __exit omap_udc_remove(struct device *dev)
2797 struct platform_device *odev = to_platform_device(dev);
2798 DECLARE_COMPLETION(done);
2805 pullup_disable(udc);
2806 if (udc->transceiver) {
2807 put_device(udc->transceiver->dev);
2808 udc->transceiver = NULL;
2810 UDC_SYSCON1_REG = 0;
2815 free_irq(odev->resource[3].start, udc);
2817 free_irq(odev->resource[2].start, udc);
2818 free_irq(odev->resource[1].start, udc);
2820 release_mem_region(odev->resource[0].start,
2821 odev->resource[0].end - odev->resource[0].start + 1);
2823 device_unregister(&udc->gadget.dev);
2824 wait_for_completion(&done);
2829 /* suspend/resume/wakeup from sysfs (echo > power/state) or when the
2830 * system is forced into deep sleep
2832 * REVISIT we should probably reject suspend requests when there's a host
2833 * session active, rather than disconnecting, at least on boards that can
2834 * report VBUS irqs (UDC_DEVSTAT_REG.UDC_ATT). And in any case, we need to
2835 * make host resumes and VBUS detection trigger OMAP wakeup events; that
2836 * may involve talking to an external transceiver (e.g. isp1301).
2838 static int omap_udc_suspend(struct device *dev, pm_message_t message, u32 level)
2842 if (level != SUSPEND_POWER_DOWN)
2844 devstat = UDC_DEVSTAT_REG;
2846 /* we're requesting 48 MHz clock if the pullup is enabled
2847 * (== we're attached to the host) and we're not suspended,
2848 * which would prevent entry to deep sleep...
2850 if ((devstat & UDC_ATT) != 0 && (devstat & UDC_SUS) == 0) {
2851 WARN("session active; suspend requires disconnect\n");
2852 omap_pullup(&udc->gadget, 0);
2855 udc->gadget.dev.power.power_state = PMSG_SUSPEND;
2856 udc->gadget.dev.parent->power.power_state = PMSG_SUSPEND;
2860 static int omap_udc_resume(struct device *dev, u32 level)
2862 if (level != RESUME_POWER_ON)
2865 DBG("resume + wakeup/SRP\n");
2866 udc->gadget.dev.parent->power.power_state = PMSG_ON;
2867 udc->gadget.dev.power.power_state = PMSG_ON;
2868 omap_pullup(&udc->gadget, 1);
2870 /* maybe the host would enumerate us if we nudged it */
2872 return omap_wakeup(&udc->gadget);
2875 /*-------------------------------------------------------------------------*/
2877 static struct device_driver udc_driver = {
2878 .name = (char *) driver_name,
2879 .bus = &platform_bus_type,
2880 .probe = omap_udc_probe,
2881 .remove = __exit_p(omap_udc_remove),
2882 .suspend = omap_udc_suspend,
2883 .resume = omap_udc_resume,
2886 static int __init udc_init(void)
2888 INFO("%s, version: " DRIVER_VERSION
2892 "%s\n", driver_desc,
2893 use_dma ? " (dma)" : "");
2894 return driver_register(&udc_driver);
2896 module_init(udc_init);
2898 static void __exit udc_exit(void)
2900 driver_unregister(&udc_driver);
2902 module_exit(udc_exit);
2904 MODULE_DESCRIPTION(DRIVER_DESC);
2905 MODULE_LICENSE("GPL");