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/module.h>
26 #include <linux/kernel.h>
27 #include <linux/ioport.h>
28 #include <linux/types.h>
29 #include <linux/errno.h>
30 #include <linux/delay.h>
31 #include <linux/sched.h>
32 #include <linux/slab.h>
33 #include <linux/init.h>
34 #include <linux/timer.h>
35 #include <linux/list.h>
36 #include <linux/interrupt.h>
37 #include <linux/proc_fs.h>
39 #include <linux/moduleparam.h>
40 #include <linux/platform_device.h>
41 #include <linux/usb_ch9.h>
42 #include <linux/usb_gadget.h>
43 #include <linux/usb/otg.h>
44 #include <linux/dma-mapping.h>
46 #include <asm/byteorder.h>
49 #include <asm/system.h>
50 #include <asm/unaligned.h>
51 #include <asm/mach-types.h>
53 #include <asm/arch/dma.h>
54 #include <asm/arch/usb.h>
60 /* bulk DMA seems to be behaving for both IN and OUT */
66 #define DRIVER_DESC "OMAP UDC driver"
67 #define DRIVER_VERSION "4 October 2004"
69 #define DMA_ADDR_INVALID (~(dma_addr_t)0)
73 * The OMAP UDC needs _very_ early endpoint setup: before enabling the
74 * D+ pullup to allow enumeration. That's too early for the gadget
75 * framework to use from usb_endpoint_enable(), which happens after
76 * enumeration as part of activating an interface. (But if we add an
77 * optional new "UDC not yet running" state to the gadget driver model,
78 * even just during driver binding, the endpoint autoconfig logic is the
79 * natural spot to manufacture new endpoints.)
81 * So instead of using endpoint enable calls to control the hardware setup,
82 * this driver defines a "fifo mode" parameter. It's used during driver
83 * initialization to choose among a set of pre-defined endpoint configs.
84 * See omap_udc_setup() for available modes, or to add others. That code
85 * lives in an init section, so use this driver as a module if you need
86 * to change the fifo mode after the kernel boots.
88 * Gadget drivers normally ignore endpoints they don't care about, and
89 * won't include them in configuration descriptors. That means only
90 * misbehaving hosts would even notice they exist.
93 static unsigned fifo_mode = 3;
95 static unsigned fifo_mode = 0;
98 /* "modprobe omap_udc fifo_mode=42", or else as a kernel
99 * boot parameter "omap_udc:fifo_mode=42"
101 module_param (fifo_mode, uint, 0);
102 MODULE_PARM_DESC (fifo_mode, "endpoint setup (0 == default)");
105 static unsigned use_dma = 1;
107 /* "modprobe omap_udc use_dma=y", or else as a kernel
108 * boot parameter "omap_udc:use_dma=y"
110 module_param (use_dma, bool, 0);
111 MODULE_PARM_DESC (use_dma, "enable/disable DMA");
114 /* save a bit of code */
116 #endif /* !USE_DMA */
119 static const char driver_name [] = "omap_udc";
120 static const char driver_desc [] = DRIVER_DESC;
122 /*-------------------------------------------------------------------------*/
124 /* there's a notion of "current endpoint" for modifying endpoint
125 * state, and PIO access to its FIFO.
128 static void use_ep(struct omap_ep *ep, u16 select)
130 u16 num = ep->bEndpointAddress & 0x0f;
132 if (ep->bEndpointAddress & USB_DIR_IN)
134 UDC_EP_NUM_REG = num | select;
135 /* when select, MUST deselect later !! */
138 static inline void deselect_ep(void)
140 UDC_EP_NUM_REG &= ~UDC_EP_SEL;
141 /* 6 wait states before TX will happen */
144 static void dma_channel_claim(struct omap_ep *ep, unsigned preferred);
146 /*-------------------------------------------------------------------------*/
148 static int omap_ep_enable(struct usb_ep *_ep,
149 const struct usb_endpoint_descriptor *desc)
151 struct omap_ep *ep = container_of(_ep, struct omap_ep, ep);
152 struct omap_udc *udc;
156 /* catch various bogus parameters */
157 if (!_ep || !desc || ep->desc
158 || desc->bDescriptorType != USB_DT_ENDPOINT
159 || ep->bEndpointAddress != desc->bEndpointAddress
160 || ep->maxpacket < le16_to_cpu
161 (desc->wMaxPacketSize)) {
162 DBG("%s, bad ep or descriptor\n", __FUNCTION__);
165 maxp = le16_to_cpu (desc->wMaxPacketSize);
166 if ((desc->bmAttributes == USB_ENDPOINT_XFER_BULK
167 && maxp != ep->maxpacket)
168 || le16_to_cpu(desc->wMaxPacketSize) > ep->maxpacket
169 || !desc->wMaxPacketSize) {
170 DBG("%s, bad %s maxpacket\n", __FUNCTION__, _ep->name);
175 if ((desc->bmAttributes == USB_ENDPOINT_XFER_ISOC
176 && desc->bInterval != 1)) {
177 /* hardware wants period = 1; USB allows 2^(Interval-1) */
178 DBG("%s, unsupported ISO period %dms\n", _ep->name,
179 1 << (desc->bInterval - 1));
183 if (desc->bmAttributes == USB_ENDPOINT_XFER_ISOC) {
184 DBG("%s, ISO nyet\n", _ep->name);
189 /* xfer types must match, except that interrupt ~= bulk */
190 if (ep->bmAttributes != desc->bmAttributes
191 && ep->bmAttributes != USB_ENDPOINT_XFER_BULK
192 && desc->bmAttributes != USB_ENDPOINT_XFER_INT) {
193 DBG("%s, %s type mismatch\n", __FUNCTION__, _ep->name);
198 if (!udc->driver || udc->gadget.speed == USB_SPEED_UNKNOWN) {
199 DBG("%s, bogus device state\n", __FUNCTION__);
203 spin_lock_irqsave(&udc->lock, flags);
208 ep->ep.maxpacket = maxp;
210 /* set endpoint to initial state */
214 use_ep(ep, UDC_EP_SEL);
215 UDC_CTRL_REG = udc->clr_halt;
219 if (ep->bmAttributes == USB_ENDPOINT_XFER_ISOC)
220 list_add(&ep->iso, &udc->iso);
222 /* maybe assign a DMA channel to this endpoint */
223 if (use_dma && desc->bmAttributes == USB_ENDPOINT_XFER_BULK)
224 /* FIXME ISO can dma, but prefers first channel */
225 dma_channel_claim(ep, 0);
227 /* PIO OUT may RX packets */
228 if (desc->bmAttributes != USB_ENDPOINT_XFER_ISOC
230 && !(ep->bEndpointAddress & USB_DIR_IN)) {
231 UDC_CTRL_REG = UDC_SET_FIFO_EN;
232 ep->ackwait = 1 + ep->double_buf;
235 spin_unlock_irqrestore(&udc->lock, flags);
236 VDBG("%s enabled\n", _ep->name);
240 static void nuke(struct omap_ep *, int status);
242 static int omap_ep_disable(struct usb_ep *_ep)
244 struct omap_ep *ep = container_of(_ep, struct omap_ep, ep);
247 if (!_ep || !ep->desc) {
248 DBG("%s, %s not enabled\n", __FUNCTION__,
249 _ep ? ep->ep.name : NULL);
253 spin_lock_irqsave(&ep->udc->lock, flags);
255 nuke (ep, -ESHUTDOWN);
256 ep->ep.maxpacket = ep->maxpacket;
258 UDC_CTRL_REG = UDC_SET_HALT;
259 list_del_init(&ep->iso);
260 del_timer(&ep->timer);
262 spin_unlock_irqrestore(&ep->udc->lock, flags);
264 VDBG("%s disabled\n", _ep->name);
268 /*-------------------------------------------------------------------------*/
270 static struct usb_request *
271 omap_alloc_request(struct usb_ep *ep, gfp_t gfp_flags)
273 struct omap_req *req;
275 req = kzalloc(sizeof(*req), gfp_flags);
277 req->req.dma = DMA_ADDR_INVALID;
278 INIT_LIST_HEAD (&req->queue);
284 omap_free_request(struct usb_ep *ep, struct usb_request *_req)
286 struct omap_req *req = container_of(_req, struct omap_req, req);
292 /*-------------------------------------------------------------------------*/
305 ep = container_of(_ep, struct omap_ep, ep);
306 if (use_dma && ep->has_dma) {
308 if (!warned && bytes < PAGE_SIZE) {
309 dev_warn(ep->udc->gadget.dev.parent,
310 "using dma_alloc_coherent for "
311 "small allocations wastes memory\n");
314 return dma_alloc_coherent(ep->udc->gadget.dev.parent,
315 bytes, dma, gfp_flags);
318 retval = kmalloc(bytes, gfp_flags);
320 *dma = virt_to_phys(retval);
324 static void omap_free_buffer(
333 ep = container_of(_ep, struct omap_ep, ep);
334 if (use_dma && _ep && ep->has_dma)
335 dma_free_coherent(ep->udc->gadget.dev.parent, bytes, buf, dma);
340 /*-------------------------------------------------------------------------*/
343 done(struct omap_ep *ep, struct omap_req *req, int status)
345 unsigned stopped = ep->stopped;
347 list_del_init(&req->queue);
349 if (req->req.status == -EINPROGRESS)
350 req->req.status = status;
352 status = req->req.status;
354 if (use_dma && ep->has_dma) {
356 dma_unmap_single(ep->udc->gadget.dev.parent,
357 req->req.dma, req->req.length,
358 (ep->bEndpointAddress & USB_DIR_IN)
361 req->req.dma = DMA_ADDR_INVALID;
364 dma_sync_single_for_cpu(ep->udc->gadget.dev.parent,
365 req->req.dma, req->req.length,
366 (ep->bEndpointAddress & USB_DIR_IN)
372 if (status && status != -ESHUTDOWN)
374 VDBG("complete %s req %p stat %d len %u/%u\n",
375 ep->ep.name, &req->req, status,
376 req->req.actual, req->req.length);
378 /* don't modify queue heads during completion callback */
380 spin_unlock(&ep->udc->lock);
381 req->req.complete(&ep->ep, &req->req);
382 spin_lock(&ep->udc->lock);
383 ep->stopped = stopped;
386 /*-------------------------------------------------------------------------*/
388 #define UDC_FIFO_FULL (UDC_NON_ISO_FIFO_FULL | UDC_ISO_FIFO_FULL)
389 #define UDC_FIFO_UNWRITABLE (UDC_EP_HALTED | UDC_FIFO_FULL)
391 #define FIFO_EMPTY (UDC_NON_ISO_FIFO_EMPTY | UDC_ISO_FIFO_EMPTY)
392 #define FIFO_UNREADABLE (UDC_EP_HALTED | FIFO_EMPTY)
395 write_packet(u8 *buf, struct omap_req *req, unsigned max)
400 len = min(req->req.length - req->req.actual, max);
401 req->req.actual += len;
404 if (likely((((int)buf) & 1) == 0)) {
407 UDC_DATA_REG = *wp++;
413 *(volatile u8 *)&UDC_DATA_REG = *buf++;
417 // FIXME change r/w fifo calling convention
420 // return: 0 = still running, 1 = completed, negative = errno
421 static int write_fifo(struct omap_ep *ep, struct omap_req *req)
428 buf = req->req.buf + req->req.actual;
431 /* PIO-IN isn't double buffered except for iso */
432 ep_stat = UDC_STAT_FLG_REG;
433 if (ep_stat & UDC_FIFO_UNWRITABLE)
436 count = ep->ep.maxpacket;
437 count = write_packet(buf, req, count);
438 UDC_CTRL_REG = UDC_SET_FIFO_EN;
441 /* last packet is often short (sometimes a zlp) */
442 if (count != ep->ep.maxpacket)
444 else if (req->req.length == req->req.actual
450 /* NOTE: requests complete when all IN data is in a
451 * FIFO (or sometimes later, if a zlp was needed).
452 * Use usb_ep_fifo_status() where needed.
460 read_packet(u8 *buf, struct omap_req *req, unsigned avail)
465 len = min(req->req.length - req->req.actual, avail);
466 req->req.actual += len;
469 if (likely((((int)buf) & 1) == 0)) {
472 *wp++ = UDC_DATA_REG;
478 *buf++ = *(volatile u8 *)&UDC_DATA_REG;
482 // return: 0 = still running, 1 = queue empty, negative = errno
483 static int read_fifo(struct omap_ep *ep, struct omap_req *req)
486 unsigned count, avail;
489 buf = req->req.buf + req->req.actual;
493 u16 ep_stat = UDC_STAT_FLG_REG;
496 if (ep_stat & FIFO_EMPTY) {
501 if (ep_stat & UDC_EP_HALTED)
504 if (ep_stat & UDC_FIFO_FULL)
505 avail = ep->ep.maxpacket;
507 avail = UDC_RXFSTAT_REG;
508 ep->fnf = ep->double_buf;
510 count = read_packet(buf, req, avail);
512 /* partial packet reads may not be errors */
513 if (count < ep->ep.maxpacket) {
515 /* overflowed this request? flush extra data */
516 if (count != avail) {
517 req->req.status = -EOVERFLOW;
520 (void) *(volatile u8 *)&UDC_DATA_REG;
522 } else if (req->req.length == req->req.actual)
527 if (!ep->bEndpointAddress)
536 /*-------------------------------------------------------------------------*/
538 static inline dma_addr_t dma_csac(unsigned lch)
542 /* omap 3.2/3.3 erratum: sometimes 0 is returned if CSAC/CDAC is
543 * read before the DMA controller finished disabling the channel.
545 csac = omap_readw(OMAP_DMA_CSAC(lch));
547 csac = omap_readw(OMAP_DMA_CSAC(lch));
551 static inline dma_addr_t dma_cdac(unsigned lch)
555 /* omap 3.2/3.3 erratum: sometimes 0 is returned if CSAC/CDAC is
556 * read before the DMA controller finished disabling the channel.
558 cdac = omap_readw(OMAP_DMA_CDAC(lch));
560 cdac = omap_readw(OMAP_DMA_CDAC(lch));
564 static u16 dma_src_len(struct omap_ep *ep, dma_addr_t start)
568 /* IN-DMA needs this on fault/cancel paths, so 15xx misreports
569 * the last transfer's bytecount by more than a FIFO's worth.
571 if (cpu_is_omap15xx())
574 end = dma_csac(ep->lch);
575 if (end == ep->dma_counter)
578 end |= start & (0xffff << 16);
584 #define DMA_DEST_LAST(x) (cpu_is_omap15xx() \
585 ? omap_readw(OMAP_DMA_CSAC(x)) /* really: CPC */ \
588 static u16 dma_dest_len(struct omap_ep *ep, dma_addr_t start)
592 end = DMA_DEST_LAST(ep->lch);
593 if (end == ep->dma_counter)
596 end |= start & (0xffff << 16);
597 if (cpu_is_omap15xx())
605 /* Each USB transfer request using DMA maps to one or more DMA transfers.
606 * When DMA completion isn't request completion, the UDC continues with
607 * the next DMA transfer for that USB transfer.
610 static void next_in_dma(struct omap_ep *ep, struct omap_req *req)
613 unsigned length = req->req.length - req->req.actual;
614 const int sync_mode = cpu_is_omap15xx()
615 ? OMAP_DMA_SYNC_FRAME
616 : OMAP_DMA_SYNC_ELEMENT;
618 /* measure length in either bytes or packets */
619 if ((cpu_is_omap16xx() && length <= UDC_TXN_TSC)
620 || (cpu_is_omap15xx() && length < ep->maxpacket)) {
621 txdma_ctrl = UDC_TXN_EOT | length;
622 omap_set_dma_transfer_params(ep->lch, OMAP_DMA_DATA_TYPE_S8,
623 length, 1, sync_mode);
625 length = min(length / ep->maxpacket,
626 (unsigned) UDC_TXN_TSC + 1);
628 omap_set_dma_transfer_params(ep->lch, OMAP_DMA_DATA_TYPE_S16,
629 ep->ep.maxpacket >> 1, length, sync_mode);
630 length *= ep->maxpacket;
632 omap_set_dma_src_params(ep->lch, OMAP_DMA_PORT_EMIFF,
633 OMAP_DMA_AMODE_POST_INC, req->req.dma + req->req.actual);
635 omap_start_dma(ep->lch);
636 ep->dma_counter = dma_csac(ep->lch);
637 UDC_DMA_IRQ_EN_REG |= UDC_TX_DONE_IE(ep->dma_channel);
638 UDC_TXDMA_REG(ep->dma_channel) = UDC_TXN_START | txdma_ctrl;
639 req->dma_bytes = length;
642 static void finish_in_dma(struct omap_ep *ep, struct omap_req *req, int status)
645 req->req.actual += req->dma_bytes;
647 /* return if this request needs to send data or zlp */
648 if (req->req.actual < req->req.length)
651 && req->dma_bytes != 0
652 && (req->req.actual % ep->maxpacket) == 0)
655 req->req.actual += dma_src_len(ep, req->req.dma
659 omap_stop_dma(ep->lch);
660 UDC_DMA_IRQ_EN_REG &= ~UDC_TX_DONE_IE(ep->dma_channel);
661 done(ep, req, status);
664 static void next_out_dma(struct omap_ep *ep, struct omap_req *req)
668 /* NOTE: we filtered out "short reads" before, so we know
669 * the buffer has only whole numbers of packets.
672 /* set up this DMA transfer, enable the fifo, start */
673 packets = (req->req.length - req->req.actual) / ep->ep.maxpacket;
674 packets = min(packets, (unsigned)UDC_RXN_TC + 1);
675 req->dma_bytes = packets * ep->ep.maxpacket;
676 omap_set_dma_transfer_params(ep->lch, OMAP_DMA_DATA_TYPE_S16,
677 ep->ep.maxpacket >> 1, packets,
678 OMAP_DMA_SYNC_ELEMENT);
679 omap_set_dma_dest_params(ep->lch, OMAP_DMA_PORT_EMIFF,
680 OMAP_DMA_AMODE_POST_INC, req->req.dma + req->req.actual);
681 ep->dma_counter = DMA_DEST_LAST(ep->lch);
683 UDC_RXDMA_REG(ep->dma_channel) = UDC_RXN_STOP | (packets - 1);
684 UDC_DMA_IRQ_EN_REG |= UDC_RX_EOT_IE(ep->dma_channel);
685 UDC_EP_NUM_REG = (ep->bEndpointAddress & 0xf);
686 UDC_CTRL_REG = UDC_SET_FIFO_EN;
688 omap_start_dma(ep->lch);
692 finish_out_dma(struct omap_ep *ep, struct omap_req *req, int status, int one)
697 ep->dma_counter = (u16) (req->req.dma + req->req.actual);
698 count = dma_dest_len(ep, req->req.dma + req->req.actual);
699 count += req->req.actual;
702 if (count <= req->req.length)
703 req->req.actual = count;
705 if (count != req->dma_bytes || status)
706 omap_stop_dma(ep->lch);
708 /* if this wasn't short, request may need another transfer */
709 else if (req->req.actual < req->req.length)
713 UDC_DMA_IRQ_EN_REG &= ~UDC_RX_EOT_IE(ep->dma_channel);
714 done(ep, req, status);
717 static void dma_irq(struct omap_udc *udc, u16 irq_src)
719 u16 dman_stat = UDC_DMAN_STAT_REG;
721 struct omap_req *req;
723 /* IN dma: tx to host */
724 if (irq_src & UDC_TXN_DONE) {
725 ep = &udc->ep[16 + UDC_DMA_TX_SRC(dman_stat)];
727 /* can see TXN_DONE after dma abort */
728 if (!list_empty(&ep->queue)) {
729 req = container_of(ep->queue.next,
730 struct omap_req, queue);
731 finish_in_dma(ep, req, 0);
733 UDC_IRQ_SRC_REG = UDC_TXN_DONE;
735 if (!list_empty (&ep->queue)) {
736 req = container_of(ep->queue.next,
737 struct omap_req, queue);
738 next_in_dma(ep, req);
742 /* OUT dma: rx from host */
743 if (irq_src & UDC_RXN_EOT) {
744 ep = &udc->ep[UDC_DMA_RX_SRC(dman_stat)];
746 /* can see RXN_EOT after dma abort */
747 if (!list_empty(&ep->queue)) {
748 req = container_of(ep->queue.next,
749 struct omap_req, queue);
750 finish_out_dma(ep, req, 0, dman_stat & UDC_DMA_RX_SB);
752 UDC_IRQ_SRC_REG = UDC_RXN_EOT;
754 if (!list_empty (&ep->queue)) {
755 req = container_of(ep->queue.next,
756 struct omap_req, queue);
757 next_out_dma(ep, req);
761 if (irq_src & UDC_RXN_CNT) {
762 ep = &udc->ep[UDC_DMA_RX_SRC(dman_stat)];
764 /* omap15xx does this unasked... */
765 VDBG("%s, RX_CNT irq?\n", ep->ep.name);
766 UDC_IRQ_SRC_REG = UDC_RXN_CNT;
770 static void dma_error(int lch, u16 ch_status, void *data)
772 struct omap_ep *ep = data;
774 /* if ch_status & OMAP_DMA_DROP_IRQ ... */
775 /* if ch_status & OMAP1_DMA_TOUT_IRQ ... */
776 ERR("%s dma error, lch %d status %02x\n", ep->ep.name, lch, ch_status);
778 /* complete current transfer ... */
781 static void dma_channel_claim(struct omap_ep *ep, unsigned channel)
784 int status, restart, is_in;
786 is_in = ep->bEndpointAddress & USB_DIR_IN;
788 reg = UDC_TXDMA_CFG_REG;
790 reg = UDC_RXDMA_CFG_REG;
791 reg |= UDC_DMA_REQ; /* "pulse" activated */
795 if (channel == 0 || channel > 3) {
796 if ((reg & 0x0f00) == 0)
798 else if ((reg & 0x00f0) == 0)
800 else if ((reg & 0x000f) == 0) /* preferred for ISO */
807 reg |= (0x0f & ep->bEndpointAddress) << (4 * (channel - 1));
808 ep->dma_channel = channel;
811 status = omap_request_dma(OMAP_DMA_USB_W2FC_TX0 - 1 + channel,
812 ep->ep.name, dma_error, ep, &ep->lch);
814 UDC_TXDMA_CFG_REG = reg;
816 omap_set_dma_src_burst_mode(ep->lch,
817 OMAP_DMA_DATA_BURST_4);
818 omap_set_dma_src_data_pack(ep->lch, 1);
820 omap_set_dma_dest_params(ep->lch,
822 OMAP_DMA_AMODE_CONSTANT,
823 (unsigned long) io_v2p((u32)&UDC_DATA_DMA_REG));
826 status = omap_request_dma(OMAP_DMA_USB_W2FC_RX0 - 1 + channel,
827 ep->ep.name, dma_error, ep, &ep->lch);
829 UDC_RXDMA_CFG_REG = reg;
831 omap_set_dma_src_params(ep->lch,
833 OMAP_DMA_AMODE_CONSTANT,
834 (unsigned long) io_v2p((u32)&UDC_DATA_DMA_REG));
836 omap_set_dma_dest_burst_mode(ep->lch,
837 OMAP_DMA_DATA_BURST_4);
838 omap_set_dma_dest_data_pack(ep->lch, 1);
845 omap_disable_dma_irq(ep->lch, OMAP_DMA_BLOCK_IRQ);
847 /* channel type P: hw synch (fifo) */
848 if (!cpu_is_omap15xx())
849 omap_writew(2, OMAP_DMA_LCH_CTRL(ep->lch));
853 /* restart any queue, even if the claim failed */
854 restart = !ep->stopped && !list_empty(&ep->queue);
857 DBG("%s no dma channel: %d%s\n", ep->ep.name, status,
858 restart ? " (restart)" : "");
860 DBG("%s claimed %cxdma%d lch %d%s\n", ep->ep.name,
862 ep->dma_channel - 1, ep->lch,
863 restart ? " (restart)" : "");
866 struct omap_req *req;
867 req = container_of(ep->queue.next, struct omap_req, queue);
869 (is_in ? next_in_dma : next_out_dma)(ep, req);
871 use_ep(ep, UDC_EP_SEL);
872 (is_in ? write_fifo : read_fifo)(ep, req);
875 UDC_CTRL_REG = UDC_SET_FIFO_EN;
876 ep->ackwait = 1 + ep->double_buf;
878 /* IN: 6 wait states before it'll tx */
883 static void dma_channel_release(struct omap_ep *ep)
885 int shift = 4 * (ep->dma_channel - 1);
886 u16 mask = 0x0f << shift;
887 struct omap_req *req;
890 /* abort any active usb transfer request */
891 if (!list_empty(&ep->queue))
892 req = container_of(ep->queue.next, struct omap_req, queue);
896 active = ((1 << 7) & omap_readl(OMAP_DMA_CCR(ep->lch))) != 0;
898 DBG("%s release %s %cxdma%d %p\n", ep->ep.name,
899 active ? "active" : "idle",
900 (ep->bEndpointAddress & USB_DIR_IN) ? 't' : 'r',
901 ep->dma_channel - 1, req);
903 /* NOTE: re-setting RX_REQ/TX_REQ because of a chip bug (before
904 * OMAP 1710 ES2.0) where reading the DMA_CFG can clear them.
907 /* wait till current packet DMA finishes, and fifo empties */
908 if (ep->bEndpointAddress & USB_DIR_IN) {
909 UDC_TXDMA_CFG_REG = (UDC_TXDMA_CFG_REG & ~mask) | UDC_DMA_REQ;
912 finish_in_dma(ep, req, -ECONNRESET);
914 /* clear FIFO; hosts probably won't empty it */
915 use_ep(ep, UDC_EP_SEL);
916 UDC_CTRL_REG = UDC_CLR_EP;
919 while (UDC_TXDMA_CFG_REG & mask)
922 UDC_RXDMA_CFG_REG = (UDC_RXDMA_CFG_REG & ~mask) | UDC_DMA_REQ;
924 /* dma empties the fifo */
925 while (UDC_RXDMA_CFG_REG & mask)
928 finish_out_dma(ep, req, -ECONNRESET, 0);
930 omap_free_dma(ep->lch);
933 /* has_dma still set, till endpoint is fully quiesced */
937 /*-------------------------------------------------------------------------*/
940 omap_ep_queue(struct usb_ep *_ep, struct usb_request *_req, gfp_t gfp_flags)
942 struct omap_ep *ep = container_of(_ep, struct omap_ep, ep);
943 struct omap_req *req = container_of(_req, struct omap_req, req);
944 struct omap_udc *udc;
948 /* catch various bogus parameters */
949 if (!_req || !req->req.complete || !req->req.buf
950 || !list_empty(&req->queue)) {
951 DBG("%s, bad params\n", __FUNCTION__);
954 if (!_ep || (!ep->desc && ep->bEndpointAddress)) {
955 DBG("%s, bad ep\n", __FUNCTION__);
958 if (ep->bmAttributes == USB_ENDPOINT_XFER_ISOC) {
959 if (req->req.length > ep->ep.maxpacket)
964 /* this isn't bogus, but OMAP DMA isn't the only hardware to
965 * have a hard time with partial packet reads... reject it.
969 && ep->bEndpointAddress != 0
970 && (ep->bEndpointAddress & USB_DIR_IN) == 0
971 && (req->req.length % ep->ep.maxpacket) != 0) {
972 DBG("%s, no partial packet OUT reads\n", __FUNCTION__);
977 if (!udc->driver || udc->gadget.speed == USB_SPEED_UNKNOWN)
980 if (use_dma && ep->has_dma) {
981 if (req->req.dma == DMA_ADDR_INVALID) {
982 req->req.dma = dma_map_single(
983 ep->udc->gadget.dev.parent,
986 (ep->bEndpointAddress & USB_DIR_IN)
991 dma_sync_single_for_device(
992 ep->udc->gadget.dev.parent,
993 req->req.dma, req->req.length,
994 (ep->bEndpointAddress & USB_DIR_IN)
1001 VDBG("%s queue req %p, len %d buf %p\n",
1002 ep->ep.name, _req, _req->length, _req->buf);
1004 spin_lock_irqsave(&udc->lock, flags);
1006 req->req.status = -EINPROGRESS;
1007 req->req.actual = 0;
1009 /* maybe kickstart non-iso i/o queues */
1011 UDC_IRQ_EN_REG |= UDC_SOF_IE;
1012 else if (list_empty(&ep->queue) && !ep->stopped && !ep->ackwait) {
1015 if (ep->bEndpointAddress == 0) {
1016 if (!udc->ep0_pending || !list_empty (&ep->queue)) {
1017 spin_unlock_irqrestore(&udc->lock, flags);
1021 /* empty DATA stage? */
1022 is_in = udc->ep0_in;
1023 if (!req->req.length) {
1025 /* chip became CONFIGURED or ADDRESSED
1026 * earlier; drivers may already have queued
1027 * requests to non-control endpoints
1029 if (udc->ep0_set_config) {
1030 u16 irq_en = UDC_IRQ_EN_REG;
1032 irq_en |= UDC_DS_CHG_IE | UDC_EP0_IE;
1033 if (!udc->ep0_reset_config)
1034 irq_en |= UDC_EPN_RX_IE
1036 UDC_IRQ_EN_REG = irq_en;
1039 /* STATUS for zero length DATA stages is
1040 * always an IN ... even for IN transfers,
1041 * a wierd case which seem to stall OMAP.
1043 UDC_EP_NUM_REG = (UDC_EP_SEL|UDC_EP_DIR);
1044 UDC_CTRL_REG = UDC_CLR_EP;
1045 UDC_CTRL_REG = UDC_SET_FIFO_EN;
1046 UDC_EP_NUM_REG = UDC_EP_DIR;
1049 udc->ep0_pending = 0;
1053 /* non-empty DATA stage */
1055 UDC_EP_NUM_REG = UDC_EP_SEL|UDC_EP_DIR;
1059 UDC_EP_NUM_REG = UDC_EP_SEL;
1062 is_in = ep->bEndpointAddress & USB_DIR_IN;
1064 use_ep(ep, UDC_EP_SEL);
1065 /* if ISO: SOF IRQs must be enabled/disabled! */
1069 (is_in ? next_in_dma : next_out_dma)(ep, req);
1071 if ((is_in ? write_fifo : read_fifo)(ep, req) == 1)
1075 UDC_CTRL_REG = UDC_SET_FIFO_EN;
1076 ep->ackwait = 1 + ep->double_buf;
1078 /* IN: 6 wait states before it'll tx */
1083 /* irq handler advances the queue */
1085 list_add_tail(&req->queue, &ep->queue);
1086 spin_unlock_irqrestore(&udc->lock, flags);
1091 static int omap_ep_dequeue(struct usb_ep *_ep, struct usb_request *_req)
1093 struct omap_ep *ep = container_of(_ep, struct omap_ep, ep);
1094 struct omap_req *req;
1095 unsigned long flags;
1100 spin_lock_irqsave(&ep->udc->lock, flags);
1102 /* make sure it's actually queued on this endpoint */
1103 list_for_each_entry (req, &ep->queue, queue) {
1104 if (&req->req == _req)
1107 if (&req->req != _req) {
1108 spin_unlock_irqrestore(&ep->udc->lock, flags);
1112 if (use_dma && ep->dma_channel && ep->queue.next == &req->queue) {
1113 int channel = ep->dma_channel;
1115 /* releasing the channel cancels the request,
1116 * reclaiming the channel restarts the queue
1118 dma_channel_release(ep);
1119 dma_channel_claim(ep, channel);
1121 done(ep, req, -ECONNRESET);
1122 spin_unlock_irqrestore(&ep->udc->lock, flags);
1126 /*-------------------------------------------------------------------------*/
1128 static int omap_ep_set_halt(struct usb_ep *_ep, int value)
1130 struct omap_ep *ep = container_of(_ep, struct omap_ep, ep);
1131 unsigned long flags;
1132 int status = -EOPNOTSUPP;
1134 spin_lock_irqsave(&ep->udc->lock, flags);
1136 /* just use protocol stalls for ep0; real halts are annoying */
1137 if (ep->bEndpointAddress == 0) {
1138 if (!ep->udc->ep0_pending)
1141 if (ep->udc->ep0_set_config) {
1142 WARN("error changing config?\n");
1143 UDC_SYSCON2_REG = UDC_CLR_CFG;
1145 UDC_SYSCON2_REG = UDC_STALL_CMD;
1146 ep->udc->ep0_pending = 0;
1151 /* otherwise, all active non-ISO endpoints can halt */
1152 } else if (ep->bmAttributes != USB_ENDPOINT_XFER_ISOC && ep->desc) {
1154 /* IN endpoints must already be idle */
1155 if ((ep->bEndpointAddress & USB_DIR_IN)
1156 && !list_empty(&ep->queue)) {
1164 if (use_dma && ep->dma_channel
1165 && !list_empty(&ep->queue)) {
1166 channel = ep->dma_channel;
1167 dma_channel_release(ep);
1171 use_ep(ep, UDC_EP_SEL);
1172 if (UDC_STAT_FLG_REG & UDC_NON_ISO_FIFO_EMPTY) {
1173 UDC_CTRL_REG = UDC_SET_HALT;
1180 dma_channel_claim(ep, channel);
1183 UDC_CTRL_REG = ep->udc->clr_halt;
1185 if (!(ep->bEndpointAddress & USB_DIR_IN)) {
1186 UDC_CTRL_REG = UDC_SET_FIFO_EN;
1187 ep->ackwait = 1 + ep->double_buf;
1192 VDBG("%s %s halt stat %d\n", ep->ep.name,
1193 value ? "set" : "clear", status);
1195 spin_unlock_irqrestore(&ep->udc->lock, flags);
1199 static struct usb_ep_ops omap_ep_ops = {
1200 .enable = omap_ep_enable,
1201 .disable = omap_ep_disable,
1203 .alloc_request = omap_alloc_request,
1204 .free_request = omap_free_request,
1206 .alloc_buffer = omap_alloc_buffer,
1207 .free_buffer = omap_free_buffer,
1209 .queue = omap_ep_queue,
1210 .dequeue = omap_ep_dequeue,
1212 .set_halt = omap_ep_set_halt,
1213 // fifo_status ... report bytes in fifo
1214 // fifo_flush ... flush fifo
1217 /*-------------------------------------------------------------------------*/
1219 static int omap_get_frame(struct usb_gadget *gadget)
1221 u16 sof = UDC_SOF_REG;
1222 return (sof & UDC_TS_OK) ? (sof & UDC_TS) : -EL2NSYNC;
1225 static int omap_wakeup(struct usb_gadget *gadget)
1227 struct omap_udc *udc;
1228 unsigned long flags;
1229 int retval = -EHOSTUNREACH;
1231 udc = container_of(gadget, struct omap_udc, gadget);
1233 spin_lock_irqsave(&udc->lock, flags);
1234 if (udc->devstat & UDC_SUS) {
1235 /* NOTE: OTG spec erratum says that OTG devices may
1236 * issue wakeups without host enable.
1238 if (udc->devstat & (UDC_B_HNP_ENABLE|UDC_R_WK_OK)) {
1239 DBG("remote wakeup...\n");
1240 UDC_SYSCON2_REG = UDC_RMT_WKP;
1244 /* NOTE: non-OTG systems may use SRP TOO... */
1245 } else if (!(udc->devstat & UDC_ATT)) {
1246 if (udc->transceiver)
1247 retval = otg_start_srp(udc->transceiver);
1249 spin_unlock_irqrestore(&udc->lock, flags);
1255 omap_set_selfpowered(struct usb_gadget *gadget, int is_selfpowered)
1257 struct omap_udc *udc;
1258 unsigned long flags;
1261 udc = container_of(gadget, struct omap_udc, gadget);
1262 spin_lock_irqsave(&udc->lock, flags);
1263 syscon1 = UDC_SYSCON1_REG;
1265 syscon1 |= UDC_SELF_PWR;
1267 syscon1 &= ~UDC_SELF_PWR;
1268 UDC_SYSCON1_REG = syscon1;
1269 spin_unlock_irqrestore(&udc->lock, flags);
1274 static int can_pullup(struct omap_udc *udc)
1276 return udc->driver && udc->softconnect && udc->vbus_active;
1279 static void pullup_enable(struct omap_udc *udc)
1281 udc->gadget.dev.parent->power.power_state = PMSG_ON;
1282 udc->gadget.dev.power.power_state = PMSG_ON;
1283 UDC_SYSCON1_REG |= UDC_PULLUP_EN;
1284 #ifndef CONFIG_USB_OTG
1285 if (!cpu_is_omap15xx())
1286 OTG_CTRL_REG |= OTG_BSESSVLD;
1288 UDC_IRQ_EN_REG = UDC_DS_CHG_IE;
1291 static void pullup_disable(struct omap_udc *udc)
1293 #ifndef CONFIG_USB_OTG
1294 if (!cpu_is_omap15xx())
1295 OTG_CTRL_REG &= ~OTG_BSESSVLD;
1297 UDC_IRQ_EN_REG = UDC_DS_CHG_IE;
1298 UDC_SYSCON1_REG &= ~UDC_PULLUP_EN;
1302 * Called by whatever detects VBUS sessions: external transceiver
1303 * driver, or maybe GPIO0 VBUS IRQ. May request 48 MHz clock.
1305 static int omap_vbus_session(struct usb_gadget *gadget, int is_active)
1307 struct omap_udc *udc;
1308 unsigned long flags;
1310 udc = container_of(gadget, struct omap_udc, gadget);
1311 spin_lock_irqsave(&udc->lock, flags);
1312 VDBG("VBUS %s\n", is_active ? "on" : "off");
1313 udc->vbus_active = (is_active != 0);
1314 if (cpu_is_omap15xx()) {
1315 /* "software" detect, ignored if !VBUS_MODE_1510 */
1317 FUNC_MUX_CTRL_0_REG |= VBUS_CTRL_1510;
1319 FUNC_MUX_CTRL_0_REG &= ~VBUS_CTRL_1510;
1321 if (can_pullup(udc))
1324 pullup_disable(udc);
1325 spin_unlock_irqrestore(&udc->lock, flags);
1329 static int omap_vbus_draw(struct usb_gadget *gadget, unsigned mA)
1331 struct omap_udc *udc;
1333 udc = container_of(gadget, struct omap_udc, gadget);
1334 if (udc->transceiver)
1335 return otg_set_power(udc->transceiver, mA);
1339 static int omap_pullup(struct usb_gadget *gadget, int is_on)
1341 struct omap_udc *udc;
1342 unsigned long flags;
1344 udc = container_of(gadget, struct omap_udc, gadget);
1345 spin_lock_irqsave(&udc->lock, flags);
1346 udc->softconnect = (is_on != 0);
1347 if (can_pullup(udc))
1350 pullup_disable(udc);
1351 spin_unlock_irqrestore(&udc->lock, flags);
1355 static struct usb_gadget_ops omap_gadget_ops = {
1356 .get_frame = omap_get_frame,
1357 .wakeup = omap_wakeup,
1358 .set_selfpowered = omap_set_selfpowered,
1359 .vbus_session = omap_vbus_session,
1360 .vbus_draw = omap_vbus_draw,
1361 .pullup = omap_pullup,
1364 /*-------------------------------------------------------------------------*/
1366 /* dequeue ALL requests; caller holds udc->lock */
1367 static void nuke(struct omap_ep *ep, int status)
1369 struct omap_req *req;
1373 if (use_dma && ep->dma_channel)
1374 dma_channel_release(ep);
1377 UDC_CTRL_REG = UDC_CLR_EP;
1378 if (ep->bEndpointAddress && ep->bmAttributes != USB_ENDPOINT_XFER_ISOC)
1379 UDC_CTRL_REG = UDC_SET_HALT;
1381 while (!list_empty(&ep->queue)) {
1382 req = list_entry(ep->queue.next, struct omap_req, queue);
1383 done(ep, req, status);
1387 /* caller holds udc->lock */
1388 static void udc_quiesce(struct omap_udc *udc)
1392 udc->gadget.speed = USB_SPEED_UNKNOWN;
1393 nuke(&udc->ep[0], -ESHUTDOWN);
1394 list_for_each_entry (ep, &udc->gadget.ep_list, ep.ep_list)
1395 nuke(ep, -ESHUTDOWN);
1398 /*-------------------------------------------------------------------------*/
1400 static void update_otg(struct omap_udc *udc)
1404 if (!udc->gadget.is_otg)
1407 if (OTG_CTRL_REG & OTG_ID)
1408 devstat = UDC_DEVSTAT_REG;
1412 udc->gadget.b_hnp_enable = !!(devstat & UDC_B_HNP_ENABLE);
1413 udc->gadget.a_hnp_support = !!(devstat & UDC_A_HNP_SUPPORT);
1414 udc->gadget.a_alt_hnp_support = !!(devstat & UDC_A_ALT_HNP_SUPPORT);
1416 /* Enable HNP early, avoiding races on suspend irq path.
1417 * ASSUMES OTG state machine B_BUS_REQ input is true.
1419 if (udc->gadget.b_hnp_enable)
1420 OTG_CTRL_REG = (OTG_CTRL_REG | OTG_B_HNPEN | OTG_B_BUSREQ)
1424 static void ep0_irq(struct omap_udc *udc, u16 irq_src)
1426 struct omap_ep *ep0 = &udc->ep[0];
1427 struct omap_req *req = NULL;
1431 /* Clear any pending requests and then scrub any rx/tx state
1432 * before starting to handle the SETUP request.
1434 if (irq_src & UDC_SETUP) {
1435 u16 ack = irq_src & (UDC_EP0_TX|UDC_EP0_RX);
1439 UDC_IRQ_SRC_REG = ack;
1440 irq_src = UDC_SETUP;
1444 /* IN/OUT packets mean we're in the DATA or STATUS stage.
1445 * This driver uses only uses protocol stalls (ep0 never halts),
1446 * and if we got this far the gadget driver already had a
1447 * chance to stall. Tries to be forgiving of host oddities.
1449 * NOTE: the last chance gadget drivers have to stall control
1450 * requests is during their request completion callback.
1452 if (!list_empty(&ep0->queue))
1453 req = container_of(ep0->queue.next, struct omap_req, queue);
1455 /* IN == TX to host */
1456 if (irq_src & UDC_EP0_TX) {
1459 UDC_IRQ_SRC_REG = UDC_EP0_TX;
1460 UDC_EP_NUM_REG = UDC_EP_SEL|UDC_EP_DIR;
1461 stat = UDC_STAT_FLG_REG;
1462 if (stat & UDC_ACK) {
1464 /* write next IN packet from response,
1465 * or set up the status stage.
1468 stat = write_fifo(ep0, req);
1469 UDC_EP_NUM_REG = UDC_EP_DIR;
1470 if (!req && udc->ep0_pending) {
1471 UDC_EP_NUM_REG = UDC_EP_SEL;
1472 UDC_CTRL_REG = UDC_CLR_EP;
1473 UDC_CTRL_REG = UDC_SET_FIFO_EN;
1475 udc->ep0_pending = 0;
1476 } /* else: 6 wait states before it'll tx */
1478 /* ack status stage of OUT transfer */
1479 UDC_EP_NUM_REG = UDC_EP_DIR;
1484 } else if (stat & UDC_STALL) {
1485 UDC_CTRL_REG = UDC_CLR_HALT;
1486 UDC_EP_NUM_REG = UDC_EP_DIR;
1488 UDC_EP_NUM_REG = UDC_EP_DIR;
1492 /* OUT == RX from host */
1493 if (irq_src & UDC_EP0_RX) {
1496 UDC_IRQ_SRC_REG = UDC_EP0_RX;
1497 UDC_EP_NUM_REG = UDC_EP_SEL;
1498 stat = UDC_STAT_FLG_REG;
1499 if (stat & UDC_ACK) {
1502 /* read next OUT packet of request, maybe
1503 * reactiviting the fifo; stall on errors.
1505 if (!req || (stat = read_fifo(ep0, req)) < 0) {
1506 UDC_SYSCON2_REG = UDC_STALL_CMD;
1507 udc->ep0_pending = 0;
1509 } else if (stat == 0)
1510 UDC_CTRL_REG = UDC_SET_FIFO_EN;
1513 /* activate status stage */
1516 /* that may have STALLed ep0... */
1517 UDC_EP_NUM_REG = UDC_EP_SEL|UDC_EP_DIR;
1518 UDC_CTRL_REG = UDC_CLR_EP;
1519 UDC_CTRL_REG = UDC_SET_FIFO_EN;
1520 UDC_EP_NUM_REG = UDC_EP_DIR;
1521 udc->ep0_pending = 0;
1524 /* ack status stage of IN transfer */
1529 } else if (stat & UDC_STALL) {
1530 UDC_CTRL_REG = UDC_CLR_HALT;
1537 /* SETUP starts all control transfers */
1538 if (irq_src & UDC_SETUP) {
1541 struct usb_ctrlrequest r;
1543 int status = -EINVAL;
1546 /* read the (latest) SETUP message */
1548 UDC_EP_NUM_REG = UDC_SETUP_SEL;
1549 /* two bytes at a time */
1550 u.word[0] = UDC_DATA_REG;
1551 u.word[1] = UDC_DATA_REG;
1552 u.word[2] = UDC_DATA_REG;
1553 u.word[3] = UDC_DATA_REG;
1555 } while (UDC_IRQ_SRC_REG & UDC_SETUP);
1557 #define w_value le16_to_cpup (&u.r.wValue)
1558 #define w_index le16_to_cpup (&u.r.wIndex)
1559 #define w_length le16_to_cpup (&u.r.wLength)
1561 /* Delegate almost all control requests to the gadget driver,
1562 * except for a handful of ch9 status/feature requests that
1563 * hardware doesn't autodecode _and_ the gadget API hides.
1565 udc->ep0_in = (u.r.bRequestType & USB_DIR_IN) != 0;
1566 udc->ep0_set_config = 0;
1567 udc->ep0_pending = 1;
1570 switch (u.r.bRequest) {
1571 case USB_REQ_SET_CONFIGURATION:
1572 /* udc needs to know when ep != 0 is valid */
1573 if (u.r.bRequestType != USB_RECIP_DEVICE)
1577 udc->ep0_set_config = 1;
1578 udc->ep0_reset_config = (w_value == 0);
1579 VDBG("set config %d\n", w_value);
1581 /* update udc NOW since gadget driver may start
1582 * queueing requests immediately; clear config
1583 * later if it fails the request.
1585 if (udc->ep0_reset_config)
1586 UDC_SYSCON2_REG = UDC_CLR_CFG;
1588 UDC_SYSCON2_REG = UDC_DEV_CFG;
1591 case USB_REQ_CLEAR_FEATURE:
1592 /* clear endpoint halt */
1593 if (u.r.bRequestType != USB_RECIP_ENDPOINT)
1595 if (w_value != USB_ENDPOINT_HALT
1598 ep = &udc->ep[w_index & 0xf];
1600 if (w_index & USB_DIR_IN)
1602 if (ep->bmAttributes == USB_ENDPOINT_XFER_ISOC
1606 UDC_CTRL_REG = udc->clr_halt;
1608 if (!(ep->bEndpointAddress & USB_DIR_IN)) {
1609 UDC_CTRL_REG = UDC_SET_FIFO_EN;
1610 ep->ackwait = 1 + ep->double_buf;
1612 /* NOTE: assumes the host behaves sanely,
1613 * only clearing real halts. Else we may
1614 * need to kill pending transfers and then
1615 * restart the queue... very messy for DMA!
1618 VDBG("%s halt cleared by host\n", ep->name);
1619 goto ep0out_status_stage;
1620 case USB_REQ_SET_FEATURE:
1621 /* set endpoint halt */
1622 if (u.r.bRequestType != USB_RECIP_ENDPOINT)
1624 if (w_value != USB_ENDPOINT_HALT
1627 ep = &udc->ep[w_index & 0xf];
1628 if (w_index & USB_DIR_IN)
1630 if (ep->bmAttributes == USB_ENDPOINT_XFER_ISOC
1631 || ep == ep0 || !ep->desc)
1633 if (use_dma && ep->has_dma) {
1634 /* this has rude side-effects (aborts) and
1635 * can't really work if DMA-IN is active
1637 DBG("%s host set_halt, NYET \n", ep->name);
1641 /* can't halt if fifo isn't empty... */
1642 UDC_CTRL_REG = UDC_CLR_EP;
1643 UDC_CTRL_REG = UDC_SET_HALT;
1644 VDBG("%s halted by host\n", ep->name);
1645 ep0out_status_stage:
1647 UDC_EP_NUM_REG = UDC_EP_SEL|UDC_EP_DIR;
1648 UDC_CTRL_REG = UDC_CLR_EP;
1649 UDC_CTRL_REG = UDC_SET_FIFO_EN;
1650 UDC_EP_NUM_REG = UDC_EP_DIR;
1651 udc->ep0_pending = 0;
1653 case USB_REQ_GET_STATUS:
1654 /* return interface status. if we were pedantic,
1655 * we'd detect non-existent interfaces, and stall.
1657 if (u.r.bRequestType
1658 != (USB_DIR_IN|USB_RECIP_INTERFACE))
1660 /* return two zero bytes */
1661 UDC_EP_NUM_REG = UDC_EP_SEL|UDC_EP_DIR;
1663 UDC_CTRL_REG = UDC_SET_FIFO_EN;
1664 UDC_EP_NUM_REG = UDC_EP_DIR;
1666 VDBG("GET_STATUS, interface %d\n", w_index);
1667 /* next, status stage */
1671 /* activate the ep0out fifo right away */
1672 if (!udc->ep0_in && w_length) {
1674 UDC_CTRL_REG = UDC_SET_FIFO_EN;
1677 /* gadget drivers see class/vendor specific requests,
1678 * {SET,GET}_{INTERFACE,DESCRIPTOR,CONFIGURATION},
1681 VDBG("SETUP %02x.%02x v%04x i%04x l%04x\n",
1682 u.r.bRequestType, u.r.bRequest,
1683 w_value, w_index, w_length);
1689 /* The gadget driver may return an error here,
1690 * causing an immediate protocol stall.
1692 * Else it must issue a response, either queueing a
1693 * response buffer for the DATA stage, or halting ep0
1694 * (causing a protocol stall, not a real halt). A
1695 * zero length buffer means no DATA stage.
1697 * It's fine to issue that response after the setup()
1698 * call returns, and this IRQ was handled.
1701 spin_unlock(&udc->lock);
1702 status = udc->driver->setup (&udc->gadget, &u.r);
1703 spin_lock(&udc->lock);
1709 VDBG("req %02x.%02x protocol STALL; stat %d\n",
1710 u.r.bRequestType, u.r.bRequest, status);
1711 if (udc->ep0_set_config) {
1712 if (udc->ep0_reset_config)
1713 WARN("error resetting config?\n");
1715 UDC_SYSCON2_REG = UDC_CLR_CFG;
1717 UDC_SYSCON2_REG = UDC_STALL_CMD;
1718 udc->ep0_pending = 0;
1723 /*-------------------------------------------------------------------------*/
1725 #define OTG_FLAGS (UDC_B_HNP_ENABLE|UDC_A_HNP_SUPPORT|UDC_A_ALT_HNP_SUPPORT)
1727 static void devstate_irq(struct omap_udc *udc, u16 irq_src)
1729 u16 devstat, change;
1731 devstat = UDC_DEVSTAT_REG;
1732 change = devstat ^ udc->devstat;
1733 udc->devstat = devstat;
1735 if (change & (UDC_USB_RESET|UDC_ATT)) {
1738 if (change & UDC_ATT) {
1739 /* driver for any external transceiver will
1740 * have called omap_vbus_session() already
1742 if (devstat & UDC_ATT) {
1743 udc->gadget.speed = USB_SPEED_FULL;
1745 if (!udc->transceiver)
1747 // if (driver->connect) call it
1748 } else if (udc->gadget.speed != USB_SPEED_UNKNOWN) {
1749 udc->gadget.speed = USB_SPEED_UNKNOWN;
1750 if (!udc->transceiver)
1751 pullup_disable(udc);
1752 DBG("disconnect, gadget %s\n",
1753 udc->driver->driver.name);
1754 if (udc->driver->disconnect) {
1755 spin_unlock(&udc->lock);
1756 udc->driver->disconnect(&udc->gadget);
1757 spin_lock(&udc->lock);
1763 if (change & UDC_USB_RESET) {
1764 if (devstat & UDC_USB_RESET) {
1767 udc->gadget.speed = USB_SPEED_FULL;
1768 INFO("USB reset done, gadget %s\n",
1769 udc->driver->driver.name);
1770 /* ep0 traffic is legal from now on */
1771 UDC_IRQ_EN_REG = UDC_DS_CHG_IE | UDC_EP0_IE;
1773 change &= ~UDC_USB_RESET;
1776 if (change & UDC_SUS) {
1777 if (udc->gadget.speed != USB_SPEED_UNKNOWN) {
1778 // FIXME tell isp1301 to suspend/resume (?)
1779 if (devstat & UDC_SUS) {
1782 /* HNP could be under way already */
1783 if (udc->gadget.speed == USB_SPEED_FULL
1784 && udc->driver->suspend) {
1785 spin_unlock(&udc->lock);
1786 udc->driver->suspend(&udc->gadget);
1787 spin_lock(&udc->lock);
1789 if (udc->transceiver)
1790 otg_set_suspend(udc->transceiver, 1);
1793 if (udc->transceiver)
1794 otg_set_suspend(udc->transceiver, 0);
1795 if (udc->gadget.speed == USB_SPEED_FULL
1796 && udc->driver->resume) {
1797 spin_unlock(&udc->lock);
1798 udc->driver->resume(&udc->gadget);
1799 spin_lock(&udc->lock);
1805 if (!cpu_is_omap15xx() && (change & OTG_FLAGS)) {
1807 change &= ~OTG_FLAGS;
1810 change &= ~(UDC_CFG|UDC_DEF|UDC_ADD);
1812 VDBG("devstat %03x, ignore change %03x\n",
1815 UDC_IRQ_SRC_REG = UDC_DS_CHG;
1818 static irqreturn_t omap_udc_irq(int irq, void *_udc)
1820 struct omap_udc *udc = _udc;
1822 irqreturn_t status = IRQ_NONE;
1823 unsigned long flags;
1825 spin_lock_irqsave(&udc->lock, flags);
1826 irq_src = UDC_IRQ_SRC_REG;
1828 /* Device state change (usb ch9 stuff) */
1829 if (irq_src & UDC_DS_CHG) {
1830 devstate_irq(_udc, irq_src);
1831 status = IRQ_HANDLED;
1832 irq_src &= ~UDC_DS_CHG;
1835 /* EP0 control transfers */
1836 if (irq_src & (UDC_EP0_RX|UDC_SETUP|UDC_EP0_TX)) {
1837 ep0_irq(_udc, irq_src);
1838 status = IRQ_HANDLED;
1839 irq_src &= ~(UDC_EP0_RX|UDC_SETUP|UDC_EP0_TX);
1842 /* DMA transfer completion */
1843 if (use_dma && (irq_src & (UDC_TXN_DONE|UDC_RXN_CNT|UDC_RXN_EOT))) {
1844 dma_irq(_udc, irq_src);
1845 status = IRQ_HANDLED;
1846 irq_src &= ~(UDC_TXN_DONE|UDC_RXN_CNT|UDC_RXN_EOT);
1849 irq_src &= ~(UDC_SOF|UDC_EPN_TX|UDC_EPN_RX);
1851 DBG("udc_irq, unhandled %03x\n", irq_src);
1852 spin_unlock_irqrestore(&udc->lock, flags);
1857 /* workaround for seemingly-lost IRQs for RX ACKs... */
1858 #define PIO_OUT_TIMEOUT (jiffies + HZ/3)
1859 #define HALF_FULL(f) (!((f)&(UDC_NON_ISO_FIFO_FULL|UDC_NON_ISO_FIFO_EMPTY)))
1861 static void pio_out_timer(unsigned long _ep)
1863 struct omap_ep *ep = (void *) _ep;
1864 unsigned long flags;
1867 spin_lock_irqsave(&ep->udc->lock, flags);
1868 if (!list_empty(&ep->queue) && ep->ackwait) {
1870 stat_flg = UDC_STAT_FLG_REG;
1872 if ((stat_flg & UDC_ACK) && (!(stat_flg & UDC_FIFO_EN)
1873 || (ep->double_buf && HALF_FULL(stat_flg)))) {
1874 struct omap_req *req;
1876 VDBG("%s: lose, %04x\n", ep->ep.name, stat_flg);
1877 req = container_of(ep->queue.next,
1878 struct omap_req, queue);
1879 UDC_EP_NUM_REG = ep->bEndpointAddress | UDC_EP_SEL;
1880 (void) read_fifo(ep, req);
1881 UDC_EP_NUM_REG = ep->bEndpointAddress;
1882 UDC_CTRL_REG = UDC_SET_FIFO_EN;
1883 ep->ackwait = 1 + ep->double_buf;
1886 mod_timer(&ep->timer, PIO_OUT_TIMEOUT);
1887 spin_unlock_irqrestore(&ep->udc->lock, flags);
1890 static irqreturn_t omap_udc_pio_irq(int irq, void *_dev)
1892 u16 epn_stat, irq_src;
1893 irqreturn_t status = IRQ_NONE;
1896 struct omap_udc *udc = _dev;
1897 struct omap_req *req;
1898 unsigned long flags;
1900 spin_lock_irqsave(&udc->lock, flags);
1901 epn_stat = UDC_EPN_STAT_REG;
1902 irq_src = UDC_IRQ_SRC_REG;
1904 /* handle OUT first, to avoid some wasteful NAKs */
1905 if (irq_src & UDC_EPN_RX) {
1906 epnum = (epn_stat >> 8) & 0x0f;
1907 UDC_IRQ_SRC_REG = UDC_EPN_RX;
1908 status = IRQ_HANDLED;
1909 ep = &udc->ep[epnum];
1912 UDC_EP_NUM_REG = epnum | UDC_EP_SEL;
1914 if ((UDC_STAT_FLG_REG & UDC_ACK)) {
1916 if (!list_empty(&ep->queue)) {
1918 req = container_of(ep->queue.next,
1919 struct omap_req, queue);
1920 stat = read_fifo(ep, req);
1921 if (!ep->double_buf)
1925 /* min 6 clock delay before clearing EP_SEL ... */
1926 epn_stat = UDC_EPN_STAT_REG;
1927 epn_stat = UDC_EPN_STAT_REG;
1928 UDC_EP_NUM_REG = epnum;
1930 /* enabling fifo _after_ clearing ACK, contrary to docs,
1931 * reduces lossage; timer still needed though (sigh).
1934 UDC_CTRL_REG = UDC_SET_FIFO_EN;
1935 ep->ackwait = 1 + ep->double_buf;
1937 mod_timer(&ep->timer, PIO_OUT_TIMEOUT);
1940 /* then IN transfers */
1941 else if (irq_src & UDC_EPN_TX) {
1942 epnum = epn_stat & 0x0f;
1943 UDC_IRQ_SRC_REG = UDC_EPN_TX;
1944 status = IRQ_HANDLED;
1945 ep = &udc->ep[16 + epnum];
1948 UDC_EP_NUM_REG = epnum | UDC_EP_DIR | UDC_EP_SEL;
1949 if ((UDC_STAT_FLG_REG & UDC_ACK)) {
1951 if (!list_empty(&ep->queue)) {
1952 req = container_of(ep->queue.next,
1953 struct omap_req, queue);
1954 (void) write_fifo(ep, req);
1957 /* min 6 clock delay before clearing EP_SEL ... */
1958 epn_stat = UDC_EPN_STAT_REG;
1959 epn_stat = UDC_EPN_STAT_REG;
1960 UDC_EP_NUM_REG = epnum | UDC_EP_DIR;
1961 /* then 6 clocks before it'd tx */
1964 spin_unlock_irqrestore(&udc->lock, flags);
1969 static irqreturn_t omap_udc_iso_irq(int irq, void *_dev)
1971 struct omap_udc *udc = _dev;
1974 unsigned long flags;
1976 spin_lock_irqsave(&udc->lock, flags);
1978 /* handle all non-DMA ISO transfers */
1979 list_for_each_entry (ep, &udc->iso, iso) {
1981 struct omap_req *req;
1983 if (ep->has_dma || list_empty(&ep->queue))
1985 req = list_entry(ep->queue.next, struct omap_req, queue);
1987 use_ep(ep, UDC_EP_SEL);
1988 stat = UDC_STAT_FLG_REG;
1990 /* NOTE: like the other controller drivers, this isn't
1991 * currently reporting lost or damaged frames.
1993 if (ep->bEndpointAddress & USB_DIR_IN) {
1994 if (stat & UDC_MISS_IN)
1995 /* done(ep, req, -EPROTO) */;
1997 write_fifo(ep, req);
2001 if (stat & UDC_NO_RXPACKET)
2002 status = -EREMOTEIO;
2003 else if (stat & UDC_ISO_ERR)
2005 else if (stat & UDC_DATA_FLUSH)
2009 /* done(ep, req, status) */;
2014 /* 6 wait states before next EP */
2017 if (!list_empty(&ep->queue))
2021 UDC_IRQ_EN_REG &= ~UDC_SOF_IE;
2022 UDC_IRQ_SRC_REG = UDC_SOF;
2024 spin_unlock_irqrestore(&udc->lock, flags);
2029 /*-------------------------------------------------------------------------*/
2031 static struct omap_udc *udc;
2033 int usb_gadget_register_driver (struct usb_gadget_driver *driver)
2035 int status = -ENODEV;
2037 unsigned long flags;
2039 /* basic sanity tests */
2043 // FIXME if otg, check: driver->is_otg
2044 || driver->speed < USB_SPEED_FULL
2049 spin_lock_irqsave(&udc->lock, flags);
2051 spin_unlock_irqrestore(&udc->lock, flags);
2056 list_for_each_entry (ep, &udc->gadget.ep_list, ep.ep_list) {
2058 if (ep->bmAttributes == USB_ENDPOINT_XFER_ISOC)
2061 UDC_CTRL_REG = UDC_SET_HALT;
2063 udc->ep0_pending = 0;
2064 udc->ep[0].irqs = 0;
2065 udc->softconnect = 1;
2067 /* hook up the driver */
2068 driver->driver.bus = NULL;
2069 udc->driver = driver;
2070 udc->gadget.dev.driver = &driver->driver;
2071 spin_unlock_irqrestore(&udc->lock, flags);
2073 status = driver->bind (&udc->gadget);
2075 DBG("bind to %s --> %d\n", driver->driver.name, status);
2076 udc->gadget.dev.driver = NULL;
2080 DBG("bound to driver %s\n", driver->driver.name);
2082 UDC_IRQ_SRC_REG = UDC_IRQ_SRC_MASK;
2084 /* connect to bus through transceiver */
2085 if (udc->transceiver) {
2086 status = otg_set_peripheral(udc->transceiver, &udc->gadget);
2088 ERR("can't bind to transceiver\n");
2089 if (driver->unbind) {
2090 driver->unbind (&udc->gadget);
2091 udc->gadget.dev.driver = NULL;
2097 if (can_pullup(udc))
2098 pullup_enable (udc);
2100 pullup_disable (udc);
2103 /* boards that don't have VBUS sensing can't autogate 48MHz;
2104 * can't enter deep sleep while a gadget driver is active.
2106 if (machine_is_omap_innovator() || machine_is_omap_osk())
2107 omap_vbus_session(&udc->gadget, 1);
2112 EXPORT_SYMBOL(usb_gadget_register_driver);
2114 int usb_gadget_unregister_driver (struct usb_gadget_driver *driver)
2116 unsigned long flags;
2117 int status = -ENODEV;
2121 if (!driver || driver != udc->driver || !driver->unbind)
2124 if (machine_is_omap_innovator() || machine_is_omap_osk())
2125 omap_vbus_session(&udc->gadget, 0);
2127 if (udc->transceiver)
2128 (void) otg_set_peripheral(udc->transceiver, NULL);
2130 pullup_disable(udc);
2132 spin_lock_irqsave(&udc->lock, flags);
2134 spin_unlock_irqrestore(&udc->lock, flags);
2136 driver->unbind(&udc->gadget);
2137 udc->gadget.dev.driver = NULL;
2140 DBG("unregistered driver '%s'\n", driver->driver.name);
2143 EXPORT_SYMBOL(usb_gadget_unregister_driver);
2146 /*-------------------------------------------------------------------------*/
2148 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
2150 #include <linux/seq_file.h>
2152 static const char proc_filename[] = "driver/udc";
2154 #define FOURBITS "%s%s%s%s"
2155 #define EIGHTBITS FOURBITS FOURBITS
2157 static void proc_ep_show(struct seq_file *s, struct omap_ep *ep)
2160 struct omap_req *req;
2165 if (use_dma && ep->has_dma)
2166 snprintf(buf, sizeof buf, "(%cxdma%d lch%d) ",
2167 (ep->bEndpointAddress & USB_DIR_IN) ? 't' : 'r',
2168 ep->dma_channel - 1, ep->lch);
2172 stat_flg = UDC_STAT_FLG_REG;
2174 "\n%s %s%s%sirqs %ld stat %04x " EIGHTBITS FOURBITS "%s\n",
2176 ep->double_buf ? "dbuf " : "",
2177 ({char *s; switch(ep->ackwait){
2178 case 0: s = ""; break;
2179 case 1: s = "(ackw) "; break;
2180 case 2: s = "(ackw2) "; break;
2181 default: s = "(?) "; break;
2184 (stat_flg & UDC_NO_RXPACKET) ? "no_rxpacket " : "",
2185 (stat_flg & UDC_MISS_IN) ? "miss_in " : "",
2186 (stat_flg & UDC_DATA_FLUSH) ? "data_flush " : "",
2187 (stat_flg & UDC_ISO_ERR) ? "iso_err " : "",
2188 (stat_flg & UDC_ISO_FIFO_EMPTY) ? "iso_fifo_empty " : "",
2189 (stat_flg & UDC_ISO_FIFO_FULL) ? "iso_fifo_full " : "",
2190 (stat_flg & UDC_EP_HALTED) ? "HALT " : "",
2191 (stat_flg & UDC_STALL) ? "STALL " : "",
2192 (stat_flg & UDC_NAK) ? "NAK " : "",
2193 (stat_flg & UDC_ACK) ? "ACK " : "",
2194 (stat_flg & UDC_FIFO_EN) ? "fifo_en " : "",
2195 (stat_flg & UDC_NON_ISO_FIFO_EMPTY) ? "fifo_empty " : "",
2196 (stat_flg & UDC_NON_ISO_FIFO_FULL) ? "fifo_full " : "");
2198 if (list_empty (&ep->queue))
2199 seq_printf(s, "\t(queue empty)\n");
2201 list_for_each_entry (req, &ep->queue, queue) {
2202 unsigned length = req->req.actual;
2204 if (use_dma && buf[0]) {
2205 length += ((ep->bEndpointAddress & USB_DIR_IN)
2206 ? dma_src_len : dma_dest_len)
2207 (ep, req->req.dma + length);
2210 seq_printf(s, "\treq %p len %d/%d buf %p\n",
2212 req->req.length, req->req.buf);
2216 static char *trx_mode(unsigned m, int enabled)
2219 case 0: return enabled ? "*6wire" : "unused";
2220 case 1: return "4wire";
2221 case 2: return "3wire";
2222 case 3: return "6wire";
2223 default: return "unknown";
2227 static int proc_otg_show(struct seq_file *s)
2233 trans = USB_TRANSCEIVER_CTRL_REG;
2234 seq_printf(s, "\nOTG rev %d.%d, transceiver_ctrl %05x\n",
2235 tmp >> 4, tmp & 0xf, trans);
2236 tmp = OTG_SYSCON_1_REG;
2237 seq_printf(s, "otg_syscon1 %08x usb2 %s, usb1 %s, usb0 %s,"
2239 trx_mode(USB2_TRX_MODE(tmp), trans & CONF_USB2_UNI_R),
2240 trx_mode(USB1_TRX_MODE(tmp), trans & CONF_USB1_UNI_R),
2241 (USB0_TRX_MODE(tmp) == 0 && !cpu_is_omap1710())
2243 : trx_mode(USB0_TRX_MODE(tmp), 1),
2244 (tmp & OTG_IDLE_EN) ? " !otg" : "",
2245 (tmp & HST_IDLE_EN) ? " !host" : "",
2246 (tmp & DEV_IDLE_EN) ? " !dev" : "",
2247 (tmp & OTG_RESET_DONE) ? " reset_done" : " reset_active");
2248 tmp = OTG_SYSCON_2_REG;
2249 seq_printf(s, "otg_syscon2 %08x%s" EIGHTBITS
2250 " b_ase_brst=%d hmc=%d\n", tmp,
2251 (tmp & OTG_EN) ? " otg_en" : "",
2252 (tmp & USBX_SYNCHRO) ? " synchro" : "",
2253 // much more SRP stuff
2254 (tmp & SRP_DATA) ? " srp_data" : "",
2255 (tmp & SRP_VBUS) ? " srp_vbus" : "",
2256 (tmp & OTG_PADEN) ? " otg_paden" : "",
2257 (tmp & HMC_PADEN) ? " hmc_paden" : "",
2258 (tmp & UHOST_EN) ? " uhost_en" : "",
2259 (tmp & HMC_TLLSPEED) ? " tllspeed" : "",
2260 (tmp & HMC_TLLATTACH) ? " tllattach" : "",
2264 seq_printf(s, "otg_ctrl %06x" EIGHTBITS EIGHTBITS "%s\n", tmp,
2265 (tmp & OTG_ASESSVLD) ? " asess" : "",
2266 (tmp & OTG_BSESSEND) ? " bsess_end" : "",
2267 (tmp & OTG_BSESSVLD) ? " bsess" : "",
2268 (tmp & OTG_VBUSVLD) ? " vbus" : "",
2269 (tmp & OTG_ID) ? " id" : "",
2270 (tmp & OTG_DRIVER_SEL) ? " DEVICE" : " HOST",
2271 (tmp & OTG_A_SETB_HNPEN) ? " a_setb_hnpen" : "",
2272 (tmp & OTG_A_BUSREQ) ? " a_bus" : "",
2273 (tmp & OTG_B_HNPEN) ? " b_hnpen" : "",
2274 (tmp & OTG_B_BUSREQ) ? " b_bus" : "",
2275 (tmp & OTG_BUSDROP) ? " busdrop" : "",
2276 (tmp & OTG_PULLDOWN) ? " down" : "",
2277 (tmp & OTG_PULLUP) ? " up" : "",
2278 (tmp & OTG_DRV_VBUS) ? " drv" : "",
2279 (tmp & OTG_PD_VBUS) ? " pd_vb" : "",
2280 (tmp & OTG_PU_VBUS) ? " pu_vb" : "",
2281 (tmp & OTG_PU_ID) ? " pu_id" : ""
2283 tmp = OTG_IRQ_EN_REG;
2284 seq_printf(s, "otg_irq_en %04x" "\n", tmp);
2285 tmp = OTG_IRQ_SRC_REG;
2286 seq_printf(s, "otg_irq_src %04x" "\n", tmp);
2287 tmp = OTG_OUTCTRL_REG;
2288 seq_printf(s, "otg_outctrl %04x" "\n", tmp);
2290 seq_printf(s, "otg_test %04x" "\n", tmp);
2294 static int proc_udc_show(struct seq_file *s, void *_)
2298 unsigned long flags;
2300 spin_lock_irqsave(&udc->lock, flags);
2302 seq_printf(s, "%s, version: " DRIVER_VERSION
2308 use_dma ? " (dma)" : "");
2310 tmp = UDC_REV_REG & 0xff;
2312 "UDC rev %d.%d, fifo mode %d, gadget %s\n"
2313 "hmc %d, transceiver %s\n",
2314 tmp >> 4, tmp & 0xf,
2316 udc->driver ? udc->driver->driver.name : "(none)",
2318 udc->transceiver ? udc->transceiver->label : "(none)");
2319 seq_printf(s, "ULPD control %04x req %04x status %04x\n",
2320 __REG16(ULPD_CLOCK_CTRL),
2321 __REG16(ULPD_SOFT_REQ),
2322 __REG16(ULPD_STATUS_REQ));
2324 /* OTG controller registers */
2325 if (!cpu_is_omap15xx())
2328 tmp = UDC_SYSCON1_REG;
2329 seq_printf(s, "\nsyscon1 %04x" EIGHTBITS "\n", tmp,
2330 (tmp & UDC_CFG_LOCK) ? " cfg_lock" : "",
2331 (tmp & UDC_DATA_ENDIAN) ? " data_endian" : "",
2332 (tmp & UDC_DMA_ENDIAN) ? " dma_endian" : "",
2333 (tmp & UDC_NAK_EN) ? " nak" : "",
2334 (tmp & UDC_AUTODECODE_DIS) ? " autodecode_dis" : "",
2335 (tmp & UDC_SELF_PWR) ? " self_pwr" : "",
2336 (tmp & UDC_SOFF_DIS) ? " soff_dis" : "",
2337 (tmp & UDC_PULLUP_EN) ? " PULLUP" : "");
2338 // syscon2 is write-only
2340 /* UDC controller registers */
2341 if (!(tmp & UDC_PULLUP_EN)) {
2342 seq_printf(s, "(suspended)\n");
2343 spin_unlock_irqrestore(&udc->lock, flags);
2347 tmp = UDC_DEVSTAT_REG;
2348 seq_printf(s, "devstat %04x" EIGHTBITS "%s%s\n", tmp,
2349 (tmp & UDC_B_HNP_ENABLE) ? " b_hnp" : "",
2350 (tmp & UDC_A_HNP_SUPPORT) ? " a_hnp" : "",
2351 (tmp & UDC_A_ALT_HNP_SUPPORT) ? " a_alt_hnp" : "",
2352 (tmp & UDC_R_WK_OK) ? " r_wk_ok" : "",
2353 (tmp & UDC_USB_RESET) ? " usb_reset" : "",
2354 (tmp & UDC_SUS) ? " SUS" : "",
2355 (tmp & UDC_CFG) ? " CFG" : "",
2356 (tmp & UDC_ADD) ? " ADD" : "",
2357 (tmp & UDC_DEF) ? " DEF" : "",
2358 (tmp & UDC_ATT) ? " ATT" : "");
2359 seq_printf(s, "sof %04x\n", UDC_SOF_REG);
2360 tmp = UDC_IRQ_EN_REG;
2361 seq_printf(s, "irq_en %04x" FOURBITS "%s\n", tmp,
2362 (tmp & UDC_SOF_IE) ? " sof" : "",
2363 (tmp & UDC_EPN_RX_IE) ? " epn_rx" : "",
2364 (tmp & UDC_EPN_TX_IE) ? " epn_tx" : "",
2365 (tmp & UDC_DS_CHG_IE) ? " ds_chg" : "",
2366 (tmp & UDC_EP0_IE) ? " ep0" : "");
2367 tmp = UDC_IRQ_SRC_REG;
2368 seq_printf(s, "irq_src %04x" EIGHTBITS "%s%s\n", tmp,
2369 (tmp & UDC_TXN_DONE) ? " txn_done" : "",
2370 (tmp & UDC_RXN_CNT) ? " rxn_cnt" : "",
2371 (tmp & UDC_RXN_EOT) ? " rxn_eot" : "",
2372 (tmp & UDC_SOF) ? " sof" : "",
2373 (tmp & UDC_EPN_RX) ? " epn_rx" : "",
2374 (tmp & UDC_EPN_TX) ? " epn_tx" : "",
2375 (tmp & UDC_DS_CHG) ? " ds_chg" : "",
2376 (tmp & UDC_SETUP) ? " setup" : "",
2377 (tmp & UDC_EP0_RX) ? " ep0out" : "",
2378 (tmp & UDC_EP0_TX) ? " ep0in" : "");
2382 tmp = UDC_DMA_IRQ_EN_REG;
2383 seq_printf(s, "dma_irq_en %04x%s" EIGHTBITS "\n", tmp,
2384 (tmp & UDC_TX_DONE_IE(3)) ? " tx2_done" : "",
2385 (tmp & UDC_RX_CNT_IE(3)) ? " rx2_cnt" : "",
2386 (tmp & UDC_RX_EOT_IE(3)) ? " rx2_eot" : "",
2388 (tmp & UDC_TX_DONE_IE(2)) ? " tx1_done" : "",
2389 (tmp & UDC_RX_CNT_IE(2)) ? " rx1_cnt" : "",
2390 (tmp & UDC_RX_EOT_IE(2)) ? " rx1_eot" : "",
2392 (tmp & UDC_TX_DONE_IE(1)) ? " tx0_done" : "",
2393 (tmp & UDC_RX_CNT_IE(1)) ? " rx0_cnt" : "",
2394 (tmp & UDC_RX_EOT_IE(1)) ? " rx0_eot" : "");
2396 tmp = UDC_RXDMA_CFG_REG;
2397 seq_printf(s, "rxdma_cfg %04x\n", tmp);
2399 for (i = 0; i < 3; i++) {
2400 if ((tmp & (0x0f << (i * 4))) == 0)
2402 seq_printf(s, "rxdma[%d] %04x\n", i,
2403 UDC_RXDMA_REG(i + 1));
2406 tmp = UDC_TXDMA_CFG_REG;
2407 seq_printf(s, "txdma_cfg %04x\n", tmp);
2409 for (i = 0; i < 3; i++) {
2410 if (!(tmp & (0x0f << (i * 4))))
2412 seq_printf(s, "txdma[%d] %04x\n", i,
2413 UDC_TXDMA_REG(i + 1));
2418 tmp = UDC_DEVSTAT_REG;
2419 if (tmp & UDC_ATT) {
2420 proc_ep_show(s, &udc->ep[0]);
2421 if (tmp & UDC_ADD) {
2422 list_for_each_entry (ep, &udc->gadget.ep_list,
2425 proc_ep_show(s, ep);
2429 spin_unlock_irqrestore(&udc->lock, flags);
2433 static int proc_udc_open(struct inode *inode, struct file *file)
2435 return single_open(file, proc_udc_show, NULL);
2438 static const struct file_operations proc_ops = {
2439 .open = proc_udc_open,
2441 .llseek = seq_lseek,
2442 .release = single_release,
2445 static void create_proc_file(void)
2447 struct proc_dir_entry *pde;
2449 pde = create_proc_entry (proc_filename, 0, NULL);
2451 pde->proc_fops = &proc_ops;
2454 static void remove_proc_file(void)
2456 remove_proc_entry(proc_filename, NULL);
2461 static inline void create_proc_file(void) {}
2462 static inline void remove_proc_file(void) {}
2466 /*-------------------------------------------------------------------------*/
2468 /* Before this controller can enumerate, we need to pick an endpoint
2469 * configuration, or "fifo_mode" That involves allocating 2KB of packet
2470 * buffer space among the endpoints we'll be operating.
2472 * NOTE: as of OMAP 1710 ES2.0, writing a new endpoint config when
2473 * UDC_SYSCON_1_REG.CFG_LOCK is set can now work. We won't use that
2474 * capability yet though.
2476 static unsigned __init
2477 omap_ep_setup(char *name, u8 addr, u8 type,
2478 unsigned buf, unsigned maxp, int dbuf)
2483 /* OUT endpoints first, then IN */
2484 ep = &udc->ep[addr & 0xf];
2485 if (addr & USB_DIR_IN)
2488 /* in case of ep init table bugs */
2489 BUG_ON(ep->name[0]);
2491 /* chip setup ... bit values are same for IN, OUT */
2492 if (type == USB_ENDPOINT_XFER_ISOC) {
2494 case 8: epn_rxtx = 0 << 12; break;
2495 case 16: epn_rxtx = 1 << 12; break;
2496 case 32: epn_rxtx = 2 << 12; break;
2497 case 64: epn_rxtx = 3 << 12; break;
2498 case 128: epn_rxtx = 4 << 12; break;
2499 case 256: epn_rxtx = 5 << 12; break;
2500 case 512: epn_rxtx = 6 << 12; break;
2503 epn_rxtx |= UDC_EPN_RX_ISO;
2506 /* double-buffering "not supported" on 15xx,
2507 * and ignored for PIO-IN on 16xx
2509 if (!use_dma || cpu_is_omap15xx())
2513 case 8: epn_rxtx = 0 << 12; break;
2514 case 16: epn_rxtx = 1 << 12; break;
2515 case 32: epn_rxtx = 2 << 12; break;
2516 case 64: epn_rxtx = 3 << 12; break;
2520 epn_rxtx |= UDC_EPN_RX_DB;
2521 init_timer(&ep->timer);
2522 ep->timer.function = pio_out_timer;
2523 ep->timer.data = (unsigned long) ep;
2526 epn_rxtx |= UDC_EPN_RX_VALID;
2528 epn_rxtx |= buf >> 3;
2530 DBG("%s addr %02x rxtx %04x maxp %d%s buf %d\n",
2531 name, addr, epn_rxtx, maxp, dbuf ? "x2" : "", buf);
2533 if (addr & USB_DIR_IN)
2534 UDC_EP_TX_REG(addr & 0xf) = epn_rxtx;
2536 UDC_EP_RX_REG(addr) = epn_rxtx;
2538 /* next endpoint's buffer starts after this one's */
2544 /* set up driver data structures */
2545 BUG_ON(strlen(name) >= sizeof ep->name);
2546 strlcpy(ep->name, name, sizeof ep->name);
2547 INIT_LIST_HEAD(&ep->queue);
2548 INIT_LIST_HEAD(&ep->iso);
2549 ep->bEndpointAddress = addr;
2550 ep->bmAttributes = type;
2551 ep->double_buf = dbuf;
2554 ep->ep.name = ep->name;
2555 ep->ep.ops = &omap_ep_ops;
2556 ep->ep.maxpacket = ep->maxpacket = maxp;
2557 list_add_tail (&ep->ep.ep_list, &udc->gadget.ep_list);
2562 static void omap_udc_release(struct device *dev)
2564 complete(udc->done);
2570 omap_udc_setup(struct platform_device *odev, struct otg_transceiver *xceiv)
2574 /* abolish any previous hardware state */
2575 UDC_SYSCON1_REG = 0;
2577 UDC_IRQ_SRC_REG = UDC_IRQ_SRC_MASK;
2578 UDC_DMA_IRQ_EN_REG = 0;
2579 UDC_RXDMA_CFG_REG = 0;
2580 UDC_TXDMA_CFG_REG = 0;
2582 /* UDC_PULLUP_EN gates the chip clock */
2583 // OTG_SYSCON_1_REG |= DEV_IDLE_EN;
2585 udc = kzalloc(sizeof(*udc), GFP_KERNEL);
2589 spin_lock_init (&udc->lock);
2591 udc->gadget.ops = &omap_gadget_ops;
2592 udc->gadget.ep0 = &udc->ep[0].ep;
2593 INIT_LIST_HEAD(&udc->gadget.ep_list);
2594 INIT_LIST_HEAD(&udc->iso);
2595 udc->gadget.speed = USB_SPEED_UNKNOWN;
2596 udc->gadget.name = driver_name;
2598 device_initialize(&udc->gadget.dev);
2599 strcpy (udc->gadget.dev.bus_id, "gadget");
2600 udc->gadget.dev.release = omap_udc_release;
2601 udc->gadget.dev.parent = &odev->dev;
2603 udc->gadget.dev.dma_mask = odev->dev.dma_mask;
2605 udc->transceiver = xceiv;
2607 /* ep0 is special; put it right after the SETUP buffer */
2608 buf = omap_ep_setup("ep0", 0, USB_ENDPOINT_XFER_CONTROL,
2609 8 /* after SETUP */, 64 /* maxpacket */, 0);
2610 list_del_init(&udc->ep[0].ep.ep_list);
2612 /* initially disable all non-ep0 endpoints */
2613 for (tmp = 1; tmp < 15; tmp++) {
2614 UDC_EP_RX_REG(tmp) = 0;
2615 UDC_EP_TX_REG(tmp) = 0;
2618 #define OMAP_BULK_EP(name,addr) \
2619 buf = omap_ep_setup(name "-bulk", addr, \
2620 USB_ENDPOINT_XFER_BULK, buf, 64, 1);
2621 #define OMAP_INT_EP(name,addr, maxp) \
2622 buf = omap_ep_setup(name "-int", addr, \
2623 USB_ENDPOINT_XFER_INT, buf, maxp, 0);
2624 #define OMAP_ISO_EP(name,addr, maxp) \
2625 buf = omap_ep_setup(name "-iso", addr, \
2626 USB_ENDPOINT_XFER_ISOC, buf, maxp, 1);
2628 switch (fifo_mode) {
2630 OMAP_BULK_EP("ep1in", USB_DIR_IN | 1);
2631 OMAP_BULK_EP("ep2out", USB_DIR_OUT | 2);
2632 OMAP_INT_EP("ep3in", USB_DIR_IN | 3, 16);
2635 OMAP_BULK_EP("ep1in", USB_DIR_IN | 1);
2636 OMAP_BULK_EP("ep2out", USB_DIR_OUT | 2);
2637 OMAP_INT_EP("ep9in", USB_DIR_IN | 9, 16);
2639 OMAP_BULK_EP("ep3in", USB_DIR_IN | 3);
2640 OMAP_BULK_EP("ep4out", USB_DIR_OUT | 4);
2641 OMAP_INT_EP("ep10in", USB_DIR_IN | 10, 16);
2643 OMAP_BULK_EP("ep5in", USB_DIR_IN | 5);
2644 OMAP_BULK_EP("ep5out", USB_DIR_OUT | 5);
2645 OMAP_INT_EP("ep11in", USB_DIR_IN | 11, 16);
2647 OMAP_BULK_EP("ep6in", USB_DIR_IN | 6);
2648 OMAP_BULK_EP("ep6out", USB_DIR_OUT | 6);
2649 OMAP_INT_EP("ep12in", USB_DIR_IN | 12, 16);
2651 OMAP_BULK_EP("ep7in", USB_DIR_IN | 7);
2652 OMAP_BULK_EP("ep7out", USB_DIR_OUT | 7);
2653 OMAP_INT_EP("ep13in", USB_DIR_IN | 13, 16);
2654 OMAP_INT_EP("ep13out", USB_DIR_OUT | 13, 16);
2656 OMAP_BULK_EP("ep8in", USB_DIR_IN | 8);
2657 OMAP_BULK_EP("ep8out", USB_DIR_OUT | 8);
2658 OMAP_INT_EP("ep14in", USB_DIR_IN | 14, 16);
2659 OMAP_INT_EP("ep14out", USB_DIR_OUT | 14, 16);
2661 OMAP_BULK_EP("ep15in", USB_DIR_IN | 15);
2662 OMAP_BULK_EP("ep15out", USB_DIR_OUT | 15);
2667 case 2: /* mixed iso/bulk */
2668 OMAP_ISO_EP("ep1in", USB_DIR_IN | 1, 256);
2669 OMAP_ISO_EP("ep2out", USB_DIR_OUT | 2, 256);
2670 OMAP_ISO_EP("ep3in", USB_DIR_IN | 3, 128);
2671 OMAP_ISO_EP("ep4out", USB_DIR_OUT | 4, 128);
2673 OMAP_INT_EP("ep5in", USB_DIR_IN | 5, 16);
2675 OMAP_BULK_EP("ep6in", USB_DIR_IN | 6);
2676 OMAP_BULK_EP("ep7out", USB_DIR_OUT | 7);
2677 OMAP_INT_EP("ep8in", USB_DIR_IN | 8, 16);
2679 case 3: /* mixed bulk/iso */
2680 OMAP_BULK_EP("ep1in", USB_DIR_IN | 1);
2681 OMAP_BULK_EP("ep2out", USB_DIR_OUT | 2);
2682 OMAP_INT_EP("ep3in", USB_DIR_IN | 3, 16);
2684 OMAP_BULK_EP("ep4in", USB_DIR_IN | 4);
2685 OMAP_BULK_EP("ep5out", USB_DIR_OUT | 5);
2686 OMAP_INT_EP("ep6in", USB_DIR_IN | 6, 16);
2688 OMAP_ISO_EP("ep7in", USB_DIR_IN | 7, 256);
2689 OMAP_ISO_EP("ep8out", USB_DIR_OUT | 8, 256);
2690 OMAP_INT_EP("ep9in", USB_DIR_IN | 9, 16);
2694 /* add more modes as needed */
2697 ERR("unsupported fifo_mode #%d\n", fifo_mode);
2700 UDC_SYSCON1_REG = UDC_CFG_LOCK|UDC_SELF_PWR;
2701 INFO("fifo mode %d, %d bytes not used\n", fifo_mode, 2048 - buf);
2705 static int __init omap_udc_probe(struct platform_device *pdev)
2707 int status = -ENODEV;
2709 struct otg_transceiver *xceiv = NULL;
2710 const char *type = NULL;
2711 struct omap_usb_config *config = pdev->dev.platform_data;
2713 /* NOTE: "knows" the order of the resources! */
2714 if (!request_mem_region(pdev->resource[0].start,
2715 pdev->resource[0].end - pdev->resource[0].start + 1,
2717 DBG("request_mem_region failed\n");
2721 INFO("OMAP UDC rev %d.%d%s\n",
2722 UDC_REV_REG >> 4, UDC_REV_REG & 0xf,
2723 config->otg ? ", Mini-AB" : "");
2725 /* use the mode given to us by board init code */
2726 if (cpu_is_omap15xx()) {
2730 if (machine_is_omap_innovator()) {
2731 /* just set up software VBUS detect, and then
2732 * later rig it so we always report VBUS.
2733 * FIXME without really sensing VBUS, we can't
2734 * know when to turn PULLUP_EN on/off; and that
2735 * means we always "need" the 48MHz clock.
2737 u32 tmp = FUNC_MUX_CTRL_0_REG;
2739 FUNC_MUX_CTRL_0_REG &= ~VBUS_CTRL_1510;
2740 tmp |= VBUS_MODE_1510;
2741 tmp &= ~VBUS_CTRL_1510;
2742 FUNC_MUX_CTRL_0_REG = tmp;
2745 /* The transceiver may package some GPIO logic or handle
2746 * loopback and/or transceiverless setup; if we find one,
2747 * use it. Except for OTG, we don't _need_ to talk to one;
2748 * but not having one probably means no VBUS detection.
2750 xceiv = otg_get_transceiver();
2752 type = xceiv->label;
2753 else if (config->otg) {
2754 DBG("OTG requires external transceiver!\n");
2760 case 0: /* POWERUP DEFAULT == 0 */
2764 if (!cpu_is_omap1710()) {
2765 type = "integrated";
2775 DBG("external transceiver not registered!\n");
2779 case 21: /* internal loopback */
2782 case 14: /* transceiverless */
2783 if (cpu_is_omap1710())
2793 ERR("unrecognized UDC HMC mode %d\n", hmc);
2797 INFO("hmc mode %d, %s transceiver\n", hmc, type);
2799 /* a "gadget" abstracts/virtualizes the controller */
2800 status = omap_udc_setup(pdev, xceiv);
2805 // "udc" is now valid
2806 pullup_disable(udc);
2807 #if defined(CONFIG_USB_OHCI_HCD) || defined(CONFIG_USB_OHCI_HCD_MODULE)
2808 udc->gadget.is_otg = (config->otg != 0);
2811 /* starting with omap1710 es2.0, clear toggle is a separate bit */
2812 if (UDC_REV_REG >= 0x61)
2813 udc->clr_halt = UDC_RESET_EP | UDC_CLRDATA_TOGGLE;
2815 udc->clr_halt = UDC_RESET_EP;
2817 /* USB general purpose IRQ: ep0, state changes, dma, etc */
2818 status = request_irq(pdev->resource[1].start, omap_udc_irq,
2819 IRQF_SAMPLE_RANDOM, driver_name, udc);
2821 ERR( "can't get irq %ld, err %d\n",
2822 pdev->resource[1].start, status);
2826 /* USB "non-iso" IRQ (PIO for all but ep0) */
2827 status = request_irq(pdev->resource[2].start, omap_udc_pio_irq,
2828 IRQF_SAMPLE_RANDOM, "omap_udc pio", udc);
2830 ERR( "can't get irq %ld, err %d\n",
2831 pdev->resource[2].start, status);
2835 status = request_irq(pdev->resource[3].start, omap_udc_iso_irq,
2836 IRQF_DISABLED, "omap_udc iso", udc);
2838 ERR("can't get irq %ld, err %d\n",
2839 pdev->resource[3].start, status);
2845 device_add(&udc->gadget.dev);
2850 free_irq(pdev->resource[2].start, udc);
2854 free_irq(pdev->resource[1].start, udc);
2862 put_device(xceiv->dev);
2863 release_mem_region(pdev->resource[0].start,
2864 pdev->resource[0].end - pdev->resource[0].start + 1);
2868 static int __exit omap_udc_remove(struct platform_device *pdev)
2870 DECLARE_COMPLETION_ONSTACK(done);
2879 pullup_disable(udc);
2880 if (udc->transceiver) {
2881 put_device(udc->transceiver->dev);
2882 udc->transceiver = NULL;
2884 UDC_SYSCON1_REG = 0;
2889 free_irq(pdev->resource[3].start, udc);
2891 free_irq(pdev->resource[2].start, udc);
2892 free_irq(pdev->resource[1].start, udc);
2894 release_mem_region(pdev->resource[0].start,
2895 pdev->resource[0].end - pdev->resource[0].start + 1);
2897 device_unregister(&udc->gadget.dev);
2898 wait_for_completion(&done);
2903 /* suspend/resume/wakeup from sysfs (echo > power/state) or when the
2904 * system is forced into deep sleep
2906 * REVISIT we should probably reject suspend requests when there's a host
2907 * session active, rather than disconnecting, at least on boards that can
2908 * report VBUS irqs (UDC_DEVSTAT_REG.UDC_ATT). And in any case, we need to
2909 * make host resumes and VBUS detection trigger OMAP wakeup events; that
2910 * may involve talking to an external transceiver (e.g. isp1301).
2913 static int omap_udc_suspend(struct platform_device *dev, pm_message_t message)
2917 devstat = UDC_DEVSTAT_REG;
2919 /* we're requesting 48 MHz clock if the pullup is enabled
2920 * (== we're attached to the host) and we're not suspended,
2921 * which would prevent entry to deep sleep...
2923 if ((devstat & UDC_ATT) != 0 && (devstat & UDC_SUS) == 0) {
2924 WARN("session active; suspend requires disconnect\n");
2925 omap_pullup(&udc->gadget, 0);
2928 udc->gadget.dev.power.power_state = PMSG_SUSPEND;
2929 udc->gadget.dev.parent->power.power_state = PMSG_SUSPEND;
2933 static int omap_udc_resume(struct platform_device *dev)
2935 DBG("resume + wakeup/SRP\n");
2936 omap_pullup(&udc->gadget, 1);
2938 /* maybe the host would enumerate us if we nudged it */
2940 return omap_wakeup(&udc->gadget);
2943 /*-------------------------------------------------------------------------*/
2945 static struct platform_driver udc_driver = {
2946 .probe = omap_udc_probe,
2947 .remove = __exit_p(omap_udc_remove),
2948 .suspend = omap_udc_suspend,
2949 .resume = omap_udc_resume,
2951 .owner = THIS_MODULE,
2952 .name = (char *) driver_name,
2956 static int __init udc_init(void)
2958 INFO("%s, version: " DRIVER_VERSION
2962 "%s\n", driver_desc,
2963 use_dma ? " (dma)" : "");
2964 return platform_driver_register(&udc_driver);
2966 module_init(udc_init);
2968 static void __exit udc_exit(void)
2970 platform_driver_unregister(&udc_driver);
2972 module_exit(udc_exit);
2974 MODULE_DESCRIPTION(DRIVER_DESC);
2975 MODULE_LICENSE("GPL");