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/platform_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>
46 #include <linux/clk.h>
48 #include <asm/byteorder.h>
51 #include <asm/system.h>
52 #include <asm/unaligned.h>
53 #include <asm/mach-types.h>
55 #include <asm/arch/dma.h>
56 #include <asm/arch/usb.h>
62 /* bulk DMA seems to be behaving for both IN and OUT */
68 #define DRIVER_DESC "OMAP UDC driver"
69 #define DRIVER_VERSION "4 October 2004"
71 #define DMA_ADDR_INVALID (~(dma_addr_t)0)
75 * The OMAP UDC needs _very_ early endpoint setup: before enabling the
76 * D+ pullup to allow enumeration. That's too early for the gadget
77 * framework to use from usb_endpoint_enable(), which happens after
78 * enumeration as part of activating an interface. (But if we add an
79 * optional new "UDC not yet running" state to the gadget driver model,
80 * even just during driver binding, the endpoint autoconfig logic is the
81 * natural spot to manufacture new endpoints.)
83 * So instead of using endpoint enable calls to control the hardware setup,
84 * this driver defines a "fifo mode" parameter. It's used during driver
85 * initialization to choose among a set of pre-defined endpoint configs.
86 * See omap_udc_setup() for available modes, or to add others. That code
87 * lives in an init section, so use this driver as a module if you need
88 * to change the fifo mode after the kernel boots.
90 * Gadget drivers normally ignore endpoints they don't care about, and
91 * won't include them in configuration descriptors. That means only
92 * misbehaving hosts would even notice they exist.
95 static unsigned fifo_mode = 3;
97 static unsigned fifo_mode = 0;
100 /* "modprobe omap_udc fifo_mode=42", or else as a kernel
101 * boot parameter "omap_udc:fifo_mode=42"
103 module_param (fifo_mode, uint, 0);
104 MODULE_PARM_DESC (fifo_mode, "endpoint setup (0 == default)");
107 static unsigned use_dma = 1;
109 /* "modprobe omap_udc use_dma=y", or else as a kernel
110 * boot parameter "omap_udc:use_dma=y"
112 module_param (use_dma, bool, 0);
113 MODULE_PARM_DESC (use_dma, "enable/disable DMA");
116 /* save a bit of code */
118 #endif /* !USE_DMA */
121 static const char driver_name [] = "omap_udc";
122 static const char driver_desc [] = DRIVER_DESC;
124 /*-------------------------------------------------------------------------*/
126 /* there's a notion of "current endpoint" for modifying endpoint
127 * state, and PIO access to its FIFO.
130 static void use_ep(struct omap_ep *ep, u16 select)
132 u16 num = ep->bEndpointAddress & 0x0f;
134 if (ep->bEndpointAddress & USB_DIR_IN)
136 UDC_EP_NUM_REG = num | select;
137 /* when select, MUST deselect later !! */
140 static inline void deselect_ep(void)
142 UDC_EP_NUM_REG &= ~UDC_EP_SEL;
143 /* 6 wait states before TX will happen */
146 static void dma_channel_claim(struct omap_ep *ep, unsigned preferred);
148 /*-------------------------------------------------------------------------*/
150 static int omap_ep_enable(struct usb_ep *_ep,
151 const struct usb_endpoint_descriptor *desc)
153 struct omap_ep *ep = container_of(_ep, struct omap_ep, ep);
154 struct omap_udc *udc;
158 /* catch various bogus parameters */
159 if (!_ep || !desc || ep->desc
160 || desc->bDescriptorType != USB_DT_ENDPOINT
161 || ep->bEndpointAddress != desc->bEndpointAddress
162 || ep->maxpacket < le16_to_cpu
163 (desc->wMaxPacketSize)) {
164 DBG("%s, bad ep or descriptor\n", __FUNCTION__);
167 maxp = le16_to_cpu (desc->wMaxPacketSize);
168 if ((desc->bmAttributes == USB_ENDPOINT_XFER_BULK
169 && maxp != ep->maxpacket)
170 || le16_to_cpu(desc->wMaxPacketSize) > ep->maxpacket
171 || !desc->wMaxPacketSize) {
172 DBG("%s, bad %s maxpacket\n", __FUNCTION__, _ep->name);
177 if ((desc->bmAttributes == USB_ENDPOINT_XFER_ISOC
178 && desc->bInterval != 1)) {
179 /* hardware wants period = 1; USB allows 2^(Interval-1) */
180 DBG("%s, unsupported ISO period %dms\n", _ep->name,
181 1 << (desc->bInterval - 1));
185 if (desc->bmAttributes == USB_ENDPOINT_XFER_ISOC) {
186 DBG("%s, ISO nyet\n", _ep->name);
191 /* xfer types must match, except that interrupt ~= bulk */
192 if (ep->bmAttributes != desc->bmAttributes
193 && ep->bmAttributes != USB_ENDPOINT_XFER_BULK
194 && desc->bmAttributes != USB_ENDPOINT_XFER_INT) {
195 DBG("%s, %s type mismatch\n", __FUNCTION__, _ep->name);
200 if (!udc->driver || udc->gadget.speed == USB_SPEED_UNKNOWN) {
201 DBG("%s, bogus device state\n", __FUNCTION__);
205 spin_lock_irqsave(&udc->lock, flags);
210 ep->ep.maxpacket = maxp;
212 /* set endpoint to initial state */
216 use_ep(ep, UDC_EP_SEL);
217 UDC_CTRL_REG = udc->clr_halt;
221 if (ep->bmAttributes == USB_ENDPOINT_XFER_ISOC)
222 list_add(&ep->iso, &udc->iso);
224 /* maybe assign a DMA channel to this endpoint */
225 if (use_dma && desc->bmAttributes == USB_ENDPOINT_XFER_BULK)
226 /* FIXME ISO can dma, but prefers first channel */
227 dma_channel_claim(ep, 0);
229 /* PIO OUT may RX packets */
230 if (desc->bmAttributes != USB_ENDPOINT_XFER_ISOC
232 && !(ep->bEndpointAddress & USB_DIR_IN)) {
233 UDC_CTRL_REG = UDC_SET_FIFO_EN;
234 ep->ackwait = 1 + ep->double_buf;
237 spin_unlock_irqrestore(&udc->lock, flags);
238 VDBG("%s enabled\n", _ep->name);
242 static void nuke(struct omap_ep *, int status);
244 static int omap_ep_disable(struct usb_ep *_ep)
246 struct omap_ep *ep = container_of(_ep, struct omap_ep, ep);
249 if (!_ep || !ep->desc) {
250 DBG("%s, %s not enabled\n", __FUNCTION__,
251 _ep ? ep->ep.name : NULL);
255 spin_lock_irqsave(&ep->udc->lock, flags);
257 nuke (ep, -ESHUTDOWN);
258 ep->ep.maxpacket = ep->maxpacket;
260 UDC_CTRL_REG = UDC_SET_HALT;
261 list_del_init(&ep->iso);
262 del_timer(&ep->timer);
264 spin_unlock_irqrestore(&ep->udc->lock, flags);
266 VDBG("%s disabled\n", _ep->name);
270 /*-------------------------------------------------------------------------*/
272 static struct usb_request *
273 omap_alloc_request(struct usb_ep *ep, gfp_t gfp_flags)
275 struct omap_req *req;
277 req = kmalloc(sizeof *req, gfp_flags);
279 memset (req, 0, sizeof *req);
280 req->req.dma = DMA_ADDR_INVALID;
281 INIT_LIST_HEAD (&req->queue);
287 omap_free_request(struct usb_ep *ep, struct usb_request *_req)
289 struct omap_req *req = container_of(_req, struct omap_req, req);
295 /*-------------------------------------------------------------------------*/
308 ep = container_of(_ep, struct omap_ep, ep);
309 if (use_dma && ep->has_dma) {
311 if (!warned && bytes < PAGE_SIZE) {
312 dev_warn(ep->udc->gadget.dev.parent,
313 "using dma_alloc_coherent for "
314 "small allocations wastes memory\n");
317 return dma_alloc_coherent(ep->udc->gadget.dev.parent,
318 bytes, dma, gfp_flags);
321 retval = kmalloc(bytes, gfp_flags);
323 *dma = virt_to_phys(retval);
327 static void omap_free_buffer(
336 ep = container_of(_ep, struct omap_ep, ep);
337 if (use_dma && _ep && ep->has_dma)
338 dma_free_coherent(ep->udc->gadget.dev.parent, bytes, buf, dma);
343 /*-------------------------------------------------------------------------*/
346 done(struct omap_ep *ep, struct omap_req *req, int status)
348 unsigned stopped = ep->stopped;
350 list_del_init(&req->queue);
352 if (req->req.status == -EINPROGRESS)
353 req->req.status = status;
355 status = req->req.status;
357 if (use_dma && ep->has_dma) {
359 dma_unmap_single(ep->udc->gadget.dev.parent,
360 req->req.dma, req->req.length,
361 (ep->bEndpointAddress & USB_DIR_IN)
364 req->req.dma = DMA_ADDR_INVALID;
367 dma_sync_single_for_cpu(ep->udc->gadget.dev.parent,
368 req->req.dma, req->req.length,
369 (ep->bEndpointAddress & USB_DIR_IN)
375 if (status && status != -ESHUTDOWN)
377 VDBG("complete %s req %p stat %d len %u/%u\n",
378 ep->ep.name, &req->req, status,
379 req->req.actual, req->req.length);
381 /* don't modify queue heads during completion callback */
383 spin_unlock(&ep->udc->lock);
384 req->req.complete(&ep->ep, &req->req);
385 spin_lock(&ep->udc->lock);
386 ep->stopped = stopped;
389 /*-------------------------------------------------------------------------*/
391 #define UDC_FIFO_FULL (UDC_NON_ISO_FIFO_FULL | UDC_ISO_FIFO_FULL)
392 #define UDC_FIFO_UNWRITABLE (UDC_EP_HALTED | UDC_FIFO_FULL)
394 #define FIFO_EMPTY (UDC_NON_ISO_FIFO_EMPTY | UDC_ISO_FIFO_EMPTY)
395 #define FIFO_UNREADABLE (UDC_EP_HALTED | FIFO_EMPTY)
398 write_packet(u8 *buf, struct omap_req *req, unsigned max)
403 len = min(req->req.length - req->req.actual, max);
404 req->req.actual += len;
407 if (likely((((int)buf) & 1) == 0)) {
410 UDC_DATA_REG = *wp++;
416 *(volatile u8 *)&UDC_DATA_REG = *buf++;
420 // FIXME change r/w fifo calling convention
423 // return: 0 = still running, 1 = completed, negative = errno
424 static int write_fifo(struct omap_ep *ep, struct omap_req *req)
431 buf = req->req.buf + req->req.actual;
434 /* PIO-IN isn't double buffered except for iso */
435 ep_stat = UDC_STAT_FLG_REG;
436 if (ep_stat & UDC_FIFO_UNWRITABLE)
439 count = ep->ep.maxpacket;
440 count = write_packet(buf, req, count);
441 UDC_CTRL_REG = UDC_SET_FIFO_EN;
444 /* last packet is often short (sometimes a zlp) */
445 if (count != ep->ep.maxpacket)
447 else if (req->req.length == req->req.actual
453 /* NOTE: requests complete when all IN data is in a
454 * FIFO (or sometimes later, if a zlp was needed).
455 * Use usb_ep_fifo_status() where needed.
463 read_packet(u8 *buf, struct omap_req *req, unsigned avail)
468 len = min(req->req.length - req->req.actual, avail);
469 req->req.actual += len;
472 if (likely((((int)buf) & 1) == 0)) {
475 *wp++ = UDC_DATA_REG;
481 *buf++ = *(volatile u8 *)&UDC_DATA_REG;
485 // return: 0 = still running, 1 = queue empty, negative = errno
486 static int read_fifo(struct omap_ep *ep, struct omap_req *req)
489 unsigned count, avail;
492 buf = req->req.buf + req->req.actual;
496 u16 ep_stat = UDC_STAT_FLG_REG;
499 if (ep_stat & FIFO_EMPTY) {
504 if (ep_stat & UDC_EP_HALTED)
507 if (ep_stat & UDC_FIFO_FULL)
508 avail = ep->ep.maxpacket;
510 avail = UDC_RXFSTAT_REG;
511 ep->fnf = ep->double_buf;
513 count = read_packet(buf, req, avail);
515 /* partial packet reads may not be errors */
516 if (count < ep->ep.maxpacket) {
518 /* overflowed this request? flush extra data */
519 if (count != avail) {
520 req->req.status = -EOVERFLOW;
523 (void) *(volatile u8 *)&UDC_DATA_REG;
525 } else if (req->req.length == req->req.actual)
530 if (!ep->bEndpointAddress)
539 /*-------------------------------------------------------------------------*/
541 static inline dma_addr_t dma_csac(unsigned lch)
545 /* omap 3.2/3.3 erratum: sometimes 0 is returned if CSAC/CDAC is
546 * read before the DMA controller finished disabling the channel.
548 csac = OMAP_DMA_CSAC_REG(lch);
550 csac = OMAP_DMA_CSAC_REG(lch);
554 static inline dma_addr_t dma_cdac(unsigned lch)
558 /* omap 3.2/3.3 erratum: sometimes 0 is returned if CSAC/CDAC is
559 * read before the DMA controller finished disabling the channel.
561 cdac = OMAP_DMA_CDAC_REG(lch);
563 cdac = OMAP_DMA_CDAC_REG(lch);
567 static u16 dma_src_len(struct omap_ep *ep, dma_addr_t start)
571 /* IN-DMA needs this on fault/cancel paths, so 15xx misreports
572 * the last transfer's bytecount by more than a FIFO's worth.
574 if (cpu_is_omap15xx())
577 end = dma_csac(ep->lch);
578 if (end == ep->dma_counter)
581 end |= start & (0xffff << 16);
587 #define DMA_DEST_LAST(x) (cpu_is_omap15xx() \
588 ? OMAP_DMA_CSAC_REG(x) /* really: CPC */ \
591 static u16 dma_dest_len(struct omap_ep *ep, dma_addr_t start)
595 end = DMA_DEST_LAST(ep->lch);
596 if (end == ep->dma_counter)
599 end |= start & (0xffff << 16);
600 if (cpu_is_omap15xx())
608 /* Each USB transfer request using DMA maps to one or more DMA transfers.
609 * When DMA completion isn't request completion, the UDC continues with
610 * the next DMA transfer for that USB transfer.
613 static void next_in_dma(struct omap_ep *ep, struct omap_req *req)
616 unsigned length = req->req.length - req->req.actual;
617 const int sync_mode = cpu_is_omap15xx()
618 ? OMAP_DMA_SYNC_FRAME
619 : OMAP_DMA_SYNC_ELEMENT;
621 /* measure length in either bytes or packets */
622 if ((cpu_is_omap16xx() && length <= UDC_TXN_TSC)
623 || (cpu_is_omap15xx() && length < ep->maxpacket)) {
624 txdma_ctrl = UDC_TXN_EOT | length;
625 omap_set_dma_transfer_params(ep->lch, OMAP_DMA_DATA_TYPE_S8,
626 length, 1, sync_mode, 0, 0);
628 length = min(length / ep->maxpacket,
629 (unsigned) UDC_TXN_TSC + 1);
631 omap_set_dma_transfer_params(ep->lch, OMAP_DMA_DATA_TYPE_S16,
632 ep->ep.maxpacket >> 1, length, sync_mode,
634 length *= ep->maxpacket;
636 omap_set_dma_src_params(ep->lch, OMAP_DMA_PORT_EMIFF,
637 OMAP_DMA_AMODE_POST_INC, req->req.dma + req->req.actual,
640 omap_start_dma(ep->lch);
641 ep->dma_counter = dma_csac(ep->lch);
642 UDC_DMA_IRQ_EN_REG |= UDC_TX_DONE_IE(ep->dma_channel);
643 UDC_TXDMA_REG(ep->dma_channel) = UDC_TXN_START | txdma_ctrl;
644 req->dma_bytes = length;
647 static void finish_in_dma(struct omap_ep *ep, struct omap_req *req, int status)
650 req->req.actual += req->dma_bytes;
652 /* return if this request needs to send data or zlp */
653 if (req->req.actual < req->req.length)
656 && req->dma_bytes != 0
657 && (req->req.actual % ep->maxpacket) == 0)
660 req->req.actual += dma_src_len(ep, req->req.dma
664 omap_stop_dma(ep->lch);
665 UDC_DMA_IRQ_EN_REG &= ~UDC_TX_DONE_IE(ep->dma_channel);
666 done(ep, req, status);
669 static void next_out_dma(struct omap_ep *ep, struct omap_req *req)
673 /* NOTE: we filtered out "short reads" before, so we know
674 * the buffer has only whole numbers of packets.
677 /* set up this DMA transfer, enable the fifo, start */
678 packets = (req->req.length - req->req.actual) / ep->ep.maxpacket;
679 packets = min(packets, (unsigned)UDC_RXN_TC + 1);
680 req->dma_bytes = packets * ep->ep.maxpacket;
681 omap_set_dma_transfer_params(ep->lch, OMAP_DMA_DATA_TYPE_S16,
682 ep->ep.maxpacket >> 1, packets,
683 OMAP_DMA_SYNC_ELEMENT,
685 omap_set_dma_dest_params(ep->lch, OMAP_DMA_PORT_EMIFF,
686 OMAP_DMA_AMODE_POST_INC, req->req.dma + req->req.actual,
688 ep->dma_counter = DMA_DEST_LAST(ep->lch);
690 UDC_RXDMA_REG(ep->dma_channel) = UDC_RXN_STOP | (packets - 1);
691 UDC_DMA_IRQ_EN_REG |= UDC_RX_EOT_IE(ep->dma_channel);
692 UDC_EP_NUM_REG = (ep->bEndpointAddress & 0xf);
693 UDC_CTRL_REG = UDC_SET_FIFO_EN;
695 omap_start_dma(ep->lch);
699 finish_out_dma(struct omap_ep *ep, struct omap_req *req, int status, int one)
704 ep->dma_counter = (u16) (req->req.dma + req->req.actual);
705 count = dma_dest_len(ep, req->req.dma + req->req.actual);
706 count += req->req.actual;
709 if (count <= req->req.length)
710 req->req.actual = count;
712 if (count != req->dma_bytes || status)
713 omap_stop_dma(ep->lch);
715 /* if this wasn't short, request may need another transfer */
716 else if (req->req.actual < req->req.length)
720 UDC_DMA_IRQ_EN_REG &= ~UDC_RX_EOT_IE(ep->dma_channel);
721 done(ep, req, status);
724 static void dma_irq(struct omap_udc *udc, u16 irq_src)
726 u16 dman_stat = UDC_DMAN_STAT_REG;
728 struct omap_req *req;
730 /* IN dma: tx to host */
731 if (irq_src & UDC_TXN_DONE) {
732 ep = &udc->ep[16 + UDC_DMA_TX_SRC(dman_stat)];
734 /* can see TXN_DONE after dma abort */
735 if (!list_empty(&ep->queue)) {
736 req = container_of(ep->queue.next,
737 struct omap_req, queue);
738 finish_in_dma(ep, req, 0);
740 UDC_IRQ_SRC_REG = UDC_TXN_DONE;
742 if (!list_empty (&ep->queue)) {
743 req = container_of(ep->queue.next,
744 struct omap_req, queue);
745 next_in_dma(ep, req);
749 /* OUT dma: rx from host */
750 if (irq_src & UDC_RXN_EOT) {
751 ep = &udc->ep[UDC_DMA_RX_SRC(dman_stat)];
753 /* can see RXN_EOT after dma abort */
754 if (!list_empty(&ep->queue)) {
755 req = container_of(ep->queue.next,
756 struct omap_req, queue);
757 finish_out_dma(ep, req, 0, dman_stat & UDC_DMA_RX_SB);
759 UDC_IRQ_SRC_REG = UDC_RXN_EOT;
761 if (!list_empty (&ep->queue)) {
762 req = container_of(ep->queue.next,
763 struct omap_req, queue);
764 next_out_dma(ep, req);
768 if (irq_src & UDC_RXN_CNT) {
769 ep = &udc->ep[UDC_DMA_RX_SRC(dman_stat)];
771 /* omap15xx does this unasked... */
772 VDBG("%s, RX_CNT irq?\n", ep->ep.name);
773 UDC_IRQ_SRC_REG = UDC_RXN_CNT;
777 static void dma_error(int lch, u16 ch_status, void *data)
779 struct omap_ep *ep = data;
781 /* if ch_status & OMAP_DMA_DROP_IRQ ... */
782 /* if ch_status & OMAP_DMA_TOUT_IRQ ... */
783 ERR("%s dma error, lch %d status %02x\n", ep->ep.name, lch, ch_status);
785 /* complete current transfer ... */
788 static void dma_channel_claim(struct omap_ep *ep, unsigned channel)
791 int status, restart, is_in;
793 is_in = ep->bEndpointAddress & USB_DIR_IN;
795 reg = UDC_TXDMA_CFG_REG;
797 reg = UDC_RXDMA_CFG_REG;
798 reg |= UDC_DMA_REQ; /* "pulse" activated */
802 if (channel == 0 || channel > 3) {
803 if ((reg & 0x0f00) == 0)
805 else if ((reg & 0x00f0) == 0)
807 else if ((reg & 0x000f) == 0) /* preferred for ISO */
814 reg |= (0x0f & ep->bEndpointAddress) << (4 * (channel - 1));
815 ep->dma_channel = channel;
818 status = omap_request_dma(OMAP_DMA_USB_W2FC_TX0 - 1 + channel,
819 ep->ep.name, dma_error, ep, &ep->lch);
821 UDC_TXDMA_CFG_REG = reg;
823 omap_set_dma_src_burst_mode(ep->lch,
824 OMAP_DMA_DATA_BURST_4);
825 omap_set_dma_src_data_pack(ep->lch, 1);
827 omap_set_dma_dest_params(ep->lch,
829 OMAP_DMA_AMODE_CONSTANT,
830 (unsigned long) io_v2p((u32)&UDC_DATA_DMA_REG),
834 status = omap_request_dma(OMAP_DMA_USB_W2FC_RX0 - 1 + channel,
835 ep->ep.name, dma_error, ep, &ep->lch);
837 UDC_RXDMA_CFG_REG = reg;
839 omap_set_dma_src_params(ep->lch,
841 OMAP_DMA_AMODE_CONSTANT,
842 (unsigned long) io_v2p((u32)&UDC_DATA_DMA_REG),
845 omap_set_dma_dest_burst_mode(ep->lch,
846 OMAP_DMA_DATA_BURST_4);
847 omap_set_dma_dest_data_pack(ep->lch, 1);
854 omap_disable_dma_irq(ep->lch, OMAP_DMA_BLOCK_IRQ);
856 /* channel type P: hw synch (fifo) */
857 if (!cpu_is_omap15xx())
858 OMAP1_DMA_LCH_CTRL_REG(ep->lch) = 2;
862 /* restart any queue, even if the claim failed */
863 restart = !ep->stopped && !list_empty(&ep->queue);
866 DBG("%s no dma channel: %d%s\n", ep->ep.name, status,
867 restart ? " (restart)" : "");
869 DBG("%s claimed %cxdma%d lch %d%s\n", ep->ep.name,
871 ep->dma_channel - 1, ep->lch,
872 restart ? " (restart)" : "");
875 struct omap_req *req;
876 req = container_of(ep->queue.next, struct omap_req, queue);
878 (is_in ? next_in_dma : next_out_dma)(ep, req);
880 use_ep(ep, UDC_EP_SEL);
881 (is_in ? write_fifo : read_fifo)(ep, req);
884 UDC_CTRL_REG = UDC_SET_FIFO_EN;
885 ep->ackwait = 1 + ep->double_buf;
887 /* IN: 6 wait states before it'll tx */
892 static void dma_channel_release(struct omap_ep *ep)
894 int shift = 4 * (ep->dma_channel - 1);
895 u16 mask = 0x0f << shift;
896 struct omap_req *req;
899 /* abort any active usb transfer request */
900 if (!list_empty(&ep->queue))
901 req = container_of(ep->queue.next, struct omap_req, queue);
905 active = ((1 << 7) & OMAP_DMA_CCR_REG(ep->lch)) != 0;
907 DBG("%s release %s %cxdma%d %p\n", ep->ep.name,
908 active ? "active" : "idle",
909 (ep->bEndpointAddress & USB_DIR_IN) ? 't' : 'r',
910 ep->dma_channel - 1, req);
912 /* NOTE: re-setting RX_REQ/TX_REQ because of a chip bug (before
913 * OMAP 1710 ES2.0) where reading the DMA_CFG can clear them.
916 /* wait till current packet DMA finishes, and fifo empties */
917 if (ep->bEndpointAddress & USB_DIR_IN) {
918 UDC_TXDMA_CFG_REG = (UDC_TXDMA_CFG_REG & ~mask) | UDC_DMA_REQ;
921 finish_in_dma(ep, req, -ECONNRESET);
923 /* clear FIFO; hosts probably won't empty it */
924 use_ep(ep, UDC_EP_SEL);
925 UDC_CTRL_REG = UDC_CLR_EP;
928 while (UDC_TXDMA_CFG_REG & mask)
931 UDC_RXDMA_CFG_REG = (UDC_RXDMA_CFG_REG & ~mask) | UDC_DMA_REQ;
933 /* dma empties the fifo */
934 while (UDC_RXDMA_CFG_REG & mask)
937 finish_out_dma(ep, req, -ECONNRESET, 0);
939 omap_free_dma(ep->lch);
942 /* has_dma still set, till endpoint is fully quiesced */
946 /*-------------------------------------------------------------------------*/
949 omap_ep_queue(struct usb_ep *_ep, struct usb_request *_req, gfp_t gfp_flags)
951 struct omap_ep *ep = container_of(_ep, struct omap_ep, ep);
952 struct omap_req *req = container_of(_req, struct omap_req, req);
953 struct omap_udc *udc;
957 /* catch various bogus parameters */
958 if (!_req || !req->req.complete || !req->req.buf
959 || !list_empty(&req->queue)) {
960 DBG("%s, bad params\n", __FUNCTION__);
963 if (!_ep || (!ep->desc && ep->bEndpointAddress)) {
964 DBG("%s, bad ep\n", __FUNCTION__);
967 if (ep->bmAttributes == USB_ENDPOINT_XFER_ISOC) {
968 if (req->req.length > ep->ep.maxpacket)
973 /* this isn't bogus, but OMAP DMA isn't the only hardware to
974 * have a hard time with partial packet reads... reject it.
978 && ep->bEndpointAddress != 0
979 && (ep->bEndpointAddress & USB_DIR_IN) == 0
980 && (req->req.length % ep->ep.maxpacket) != 0) {
981 DBG("%s, no partial packet OUT reads\n", __FUNCTION__);
986 if (!udc->driver || udc->gadget.speed == USB_SPEED_UNKNOWN)
989 if (use_dma && ep->has_dma) {
990 if (req->req.dma == DMA_ADDR_INVALID) {
991 req->req.dma = dma_map_single(
992 ep->udc->gadget.dev.parent,
995 (ep->bEndpointAddress & USB_DIR_IN)
1000 dma_sync_single_for_device(
1001 ep->udc->gadget.dev.parent,
1002 req->req.dma, req->req.length,
1003 (ep->bEndpointAddress & USB_DIR_IN)
1010 VDBG("%s queue req %p, len %d buf %p\n",
1011 ep->ep.name, _req, _req->length, _req->buf);
1013 spin_lock_irqsave(&udc->lock, flags);
1015 req->req.status = -EINPROGRESS;
1016 req->req.actual = 0;
1018 /* maybe kickstart non-iso i/o queues */
1020 UDC_IRQ_EN_REG |= UDC_SOF_IE;
1021 else if (list_empty(&ep->queue) && !ep->stopped && !ep->ackwait) {
1024 if (ep->bEndpointAddress == 0) {
1025 if (!udc->ep0_pending || !list_empty (&ep->queue)) {
1026 spin_unlock_irqrestore(&udc->lock, flags);
1030 /* empty DATA stage? */
1031 is_in = udc->ep0_in;
1032 if (!req->req.length) {
1034 /* chip became CONFIGURED or ADDRESSED
1035 * earlier; drivers may already have queued
1036 * requests to non-control endpoints
1038 if (udc->ep0_set_config) {
1039 u16 irq_en = UDC_IRQ_EN_REG;
1041 irq_en |= UDC_DS_CHG_IE | UDC_EP0_IE;
1042 if (!udc->ep0_reset_config)
1043 irq_en |= UDC_EPN_RX_IE
1045 UDC_IRQ_EN_REG = irq_en;
1048 /* STATUS for zero length DATA stages is
1049 * always an IN ... even for IN transfers,
1050 * a wierd case which seem to stall OMAP.
1052 UDC_EP_NUM_REG = (UDC_EP_SEL|UDC_EP_DIR);
1053 UDC_CTRL_REG = UDC_CLR_EP;
1054 UDC_CTRL_REG = UDC_SET_FIFO_EN;
1055 UDC_EP_NUM_REG = UDC_EP_DIR;
1058 udc->ep0_pending = 0;
1062 /* non-empty DATA stage */
1064 UDC_EP_NUM_REG = UDC_EP_SEL|UDC_EP_DIR;
1068 UDC_EP_NUM_REG = UDC_EP_SEL;
1071 is_in = ep->bEndpointAddress & USB_DIR_IN;
1073 use_ep(ep, UDC_EP_SEL);
1074 /* if ISO: SOF IRQs must be enabled/disabled! */
1078 (is_in ? next_in_dma : next_out_dma)(ep, req);
1080 if ((is_in ? write_fifo : read_fifo)(ep, req) == 1)
1084 UDC_CTRL_REG = UDC_SET_FIFO_EN;
1085 ep->ackwait = 1 + ep->double_buf;
1087 /* IN: 6 wait states before it'll tx */
1092 /* irq handler advances the queue */
1094 list_add_tail(&req->queue, &ep->queue);
1095 spin_unlock_irqrestore(&udc->lock, flags);
1100 static int omap_ep_dequeue(struct usb_ep *_ep, struct usb_request *_req)
1102 struct omap_ep *ep = container_of(_ep, struct omap_ep, ep);
1103 struct omap_req *req;
1104 unsigned long flags;
1109 spin_lock_irqsave(&ep->udc->lock, flags);
1111 /* make sure it's actually queued on this endpoint */
1112 list_for_each_entry (req, &ep->queue, queue) {
1113 if (&req->req == _req)
1116 if (&req->req != _req) {
1117 spin_unlock_irqrestore(&ep->udc->lock, flags);
1121 if (use_dma && ep->dma_channel && ep->queue.next == &req->queue) {
1122 int channel = ep->dma_channel;
1124 /* releasing the channel cancels the request,
1125 * reclaiming the channel restarts the queue
1127 dma_channel_release(ep);
1128 dma_channel_claim(ep, channel);
1130 done(ep, req, -ECONNRESET);
1131 spin_unlock_irqrestore(&ep->udc->lock, flags);
1135 /*-------------------------------------------------------------------------*/
1137 static int omap_ep_set_halt(struct usb_ep *_ep, int value)
1139 struct omap_ep *ep = container_of(_ep, struct omap_ep, ep);
1140 unsigned long flags;
1141 int status = -EOPNOTSUPP;
1143 spin_lock_irqsave(&ep->udc->lock, flags);
1145 /* just use protocol stalls for ep0; real halts are annoying */
1146 if (ep->bEndpointAddress == 0) {
1147 if (!ep->udc->ep0_pending)
1150 if (ep->udc->ep0_set_config) {
1151 WARN("error changing config?\n");
1152 UDC_SYSCON2_REG = UDC_CLR_CFG;
1154 UDC_SYSCON2_REG = UDC_STALL_CMD;
1155 ep->udc->ep0_pending = 0;
1160 /* otherwise, all active non-ISO endpoints can halt */
1161 } else if (ep->bmAttributes != USB_ENDPOINT_XFER_ISOC && ep->desc) {
1163 /* IN endpoints must already be idle */
1164 if ((ep->bEndpointAddress & USB_DIR_IN)
1165 && !list_empty(&ep->queue)) {
1173 if (use_dma && ep->dma_channel
1174 && !list_empty(&ep->queue)) {
1175 channel = ep->dma_channel;
1176 dma_channel_release(ep);
1180 use_ep(ep, UDC_EP_SEL);
1181 if (UDC_STAT_FLG_REG & UDC_NON_ISO_FIFO_EMPTY) {
1182 UDC_CTRL_REG = UDC_SET_HALT;
1189 dma_channel_claim(ep, channel);
1192 UDC_CTRL_REG = ep->udc->clr_halt;
1194 if (!(ep->bEndpointAddress & USB_DIR_IN)) {
1195 UDC_CTRL_REG = UDC_SET_FIFO_EN;
1196 ep->ackwait = 1 + ep->double_buf;
1201 VDBG("%s %s halt stat %d\n", ep->ep.name,
1202 value ? "set" : "clear", status);
1204 spin_unlock_irqrestore(&ep->udc->lock, flags);
1208 static struct usb_ep_ops omap_ep_ops = {
1209 .enable = omap_ep_enable,
1210 .disable = omap_ep_disable,
1212 .alloc_request = omap_alloc_request,
1213 .free_request = omap_free_request,
1215 .alloc_buffer = omap_alloc_buffer,
1216 .free_buffer = omap_free_buffer,
1218 .queue = omap_ep_queue,
1219 .dequeue = omap_ep_dequeue,
1221 .set_halt = omap_ep_set_halt,
1222 // fifo_status ... report bytes in fifo
1223 // fifo_flush ... flush fifo
1226 /*-------------------------------------------------------------------------*/
1228 static int omap_get_frame(struct usb_gadget *gadget)
1230 u16 sof = UDC_SOF_REG;
1231 return (sof & UDC_TS_OK) ? (sof & UDC_TS) : -EL2NSYNC;
1234 static int omap_wakeup(struct usb_gadget *gadget)
1236 struct omap_udc *udc;
1237 unsigned long flags;
1238 int retval = -EHOSTUNREACH;
1240 udc = container_of(gadget, struct omap_udc, gadget);
1242 spin_lock_irqsave(&udc->lock, flags);
1243 if (udc->devstat & UDC_SUS) {
1244 /* NOTE: OTG spec erratum says that OTG devices may
1245 * issue wakeups without host enable.
1247 if (udc->devstat & (UDC_B_HNP_ENABLE|UDC_R_WK_OK)) {
1248 DBG("remote wakeup...\n");
1249 UDC_SYSCON2_REG = UDC_RMT_WKP;
1253 /* NOTE: non-OTG systems may use SRP TOO... */
1254 } else if (!(udc->devstat & UDC_ATT)) {
1255 if (udc->transceiver)
1256 retval = otg_start_srp(udc->transceiver);
1258 spin_unlock_irqrestore(&udc->lock, flags);
1264 omap_set_selfpowered(struct usb_gadget *gadget, int is_selfpowered)
1266 struct omap_udc *udc;
1267 unsigned long flags;
1270 udc = container_of(gadget, struct omap_udc, gadget);
1271 spin_lock_irqsave(&udc->lock, flags);
1272 syscon1 = UDC_SYSCON1_REG;
1274 syscon1 |= UDC_SELF_PWR;
1276 syscon1 &= ~UDC_SELF_PWR;
1277 UDC_SYSCON1_REG = syscon1;
1278 spin_unlock_irqrestore(&udc->lock, flags);
1283 static int can_pullup(struct omap_udc *udc)
1285 return udc->driver && udc->softconnect && udc->vbus_active;
1288 static void pullup_enable(struct omap_udc *udc)
1290 udc->gadget.dev.parent->power.power_state = PMSG_ON;
1291 udc->gadget.dev.power.power_state = PMSG_ON;
1292 UDC_SYSCON1_REG |= UDC_PULLUP_EN;
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;
1300 static void pullup_disable(struct omap_udc *udc)
1302 #ifndef CONFIG_USB_OTG
1303 if (!cpu_is_omap15xx())
1304 OTG_CTRL_REG &= ~OTG_BSESSVLD;
1306 UDC_IRQ_EN_REG = UDC_DS_CHG_IE;
1307 UDC_SYSCON1_REG &= ~UDC_PULLUP_EN;
1310 static struct omap_udc *udc;
1312 static void omap_udc_enable_clock(int enable)
1314 if (udc == NULL || udc->dc_clk == NULL || udc->hhc_clk == NULL)
1318 clk_enable(udc->dc_clk);
1319 clk_enable(udc->hhc_clk);
1322 clk_disable(udc->hhc_clk);
1323 clk_disable(udc->dc_clk);
1328 * Called by whatever detects VBUS sessions: external transceiver
1329 * driver, or maybe GPIO0 VBUS IRQ. May request 48 MHz clock.
1331 static int omap_vbus_session(struct usb_gadget *gadget, int is_active)
1333 struct omap_udc *udc;
1334 unsigned long flags;
1336 udc = container_of(gadget, struct omap_udc, gadget);
1337 spin_lock_irqsave(&udc->lock, flags);
1338 VDBG("VBUS %s\n", is_active ? "on" : "off");
1339 udc->vbus_active = (is_active != 0);
1340 if (cpu_is_omap15xx()) {
1341 /* "software" detect, ignored if !VBUS_MODE_1510 */
1343 FUNC_MUX_CTRL_0_REG |= VBUS_CTRL_1510;
1345 FUNC_MUX_CTRL_0_REG &= ~VBUS_CTRL_1510;
1347 if (udc->dc_clk != NULL && is_active) {
1348 if (!udc->clk_requested) {
1349 omap_udc_enable_clock(1);
1350 udc->clk_requested = 1;
1353 if (can_pullup(udc))
1356 pullup_disable(udc);
1357 if (udc->dc_clk != NULL && !is_active) {
1358 if (udc->clk_requested) {
1359 omap_udc_enable_clock(0);
1360 udc->clk_requested = 0;
1363 spin_unlock_irqrestore(&udc->lock, flags);
1367 static int omap_vbus_draw(struct usb_gadget *gadget, unsigned mA)
1369 struct omap_udc *udc;
1371 udc = container_of(gadget, struct omap_udc, gadget);
1372 if (udc->transceiver)
1373 return otg_set_power(udc->transceiver, mA);
1377 static int omap_pullup(struct usb_gadget *gadget, int is_on)
1379 struct omap_udc *udc;
1380 unsigned long flags;
1382 udc = container_of(gadget, struct omap_udc, gadget);
1383 spin_lock_irqsave(&udc->lock, flags);
1384 udc->softconnect = (is_on != 0);
1385 if (can_pullup(udc))
1388 pullup_disable(udc);
1389 spin_unlock_irqrestore(&udc->lock, flags);
1393 static struct usb_gadget_ops omap_gadget_ops = {
1394 .get_frame = omap_get_frame,
1395 .wakeup = omap_wakeup,
1396 .set_selfpowered = omap_set_selfpowered,
1397 .vbus_session = omap_vbus_session,
1398 .vbus_draw = omap_vbus_draw,
1399 .pullup = omap_pullup,
1402 /*-------------------------------------------------------------------------*/
1404 /* dequeue ALL requests; caller holds udc->lock */
1405 static void nuke(struct omap_ep *ep, int status)
1407 struct omap_req *req;
1411 if (use_dma && ep->dma_channel)
1412 dma_channel_release(ep);
1415 UDC_CTRL_REG = UDC_CLR_EP;
1416 if (ep->bEndpointAddress && ep->bmAttributes != USB_ENDPOINT_XFER_ISOC)
1417 UDC_CTRL_REG = UDC_SET_HALT;
1419 while (!list_empty(&ep->queue)) {
1420 req = list_entry(ep->queue.next, struct omap_req, queue);
1421 done(ep, req, status);
1425 /* caller holds udc->lock */
1426 static void udc_quiesce(struct omap_udc *udc)
1430 udc->gadget.speed = USB_SPEED_UNKNOWN;
1431 nuke(&udc->ep[0], -ESHUTDOWN);
1432 list_for_each_entry (ep, &udc->gadget.ep_list, ep.ep_list)
1433 nuke(ep, -ESHUTDOWN);
1436 /*-------------------------------------------------------------------------*/
1438 static void update_otg(struct omap_udc *udc)
1442 if (!udc->gadget.is_otg)
1445 if (OTG_CTRL_REG & OTG_ID)
1446 devstat = UDC_DEVSTAT_REG;
1450 udc->gadget.b_hnp_enable = !!(devstat & UDC_B_HNP_ENABLE);
1451 udc->gadget.a_hnp_support = !!(devstat & UDC_A_HNP_SUPPORT);
1452 udc->gadget.a_alt_hnp_support = !!(devstat & UDC_A_ALT_HNP_SUPPORT);
1454 /* Enable HNP early, avoiding races on suspend irq path.
1455 * ASSUMES OTG state machine B_BUS_REQ input is true.
1457 if (udc->gadget.b_hnp_enable)
1458 OTG_CTRL_REG = (OTG_CTRL_REG | OTG_B_HNPEN | OTG_B_BUSREQ)
1462 static void ep0_irq(struct omap_udc *udc, u16 irq_src)
1464 struct omap_ep *ep0 = &udc->ep[0];
1465 struct omap_req *req = NULL;
1469 /* Clear any pending requests and then scrub any rx/tx state
1470 * before starting to handle the SETUP request.
1472 if (irq_src & UDC_SETUP) {
1473 u16 ack = irq_src & (UDC_EP0_TX|UDC_EP0_RX);
1477 UDC_IRQ_SRC_REG = ack;
1478 irq_src = UDC_SETUP;
1482 /* IN/OUT packets mean we're in the DATA or STATUS stage.
1483 * This driver uses only uses protocol stalls (ep0 never halts),
1484 * and if we got this far the gadget driver already had a
1485 * chance to stall. Tries to be forgiving of host oddities.
1487 * NOTE: the last chance gadget drivers have to stall control
1488 * requests is during their request completion callback.
1490 if (!list_empty(&ep0->queue))
1491 req = container_of(ep0->queue.next, struct omap_req, queue);
1493 /* IN == TX to host */
1494 if (irq_src & UDC_EP0_TX) {
1497 UDC_IRQ_SRC_REG = UDC_EP0_TX;
1498 UDC_EP_NUM_REG = UDC_EP_SEL|UDC_EP_DIR;
1499 stat = UDC_STAT_FLG_REG;
1500 if (stat & UDC_ACK) {
1502 /* write next IN packet from response,
1503 * or set up the status stage.
1506 stat = write_fifo(ep0, req);
1507 UDC_EP_NUM_REG = UDC_EP_DIR;
1508 if (!req && udc->ep0_pending) {
1509 UDC_EP_NUM_REG = UDC_EP_SEL;
1510 UDC_CTRL_REG = UDC_CLR_EP;
1511 UDC_CTRL_REG = UDC_SET_FIFO_EN;
1513 udc->ep0_pending = 0;
1514 } /* else: 6 wait states before it'll tx */
1516 /* ack status stage of OUT transfer */
1517 UDC_EP_NUM_REG = UDC_EP_DIR;
1522 } else if (stat & UDC_STALL) {
1523 UDC_CTRL_REG = UDC_CLR_HALT;
1524 UDC_EP_NUM_REG = UDC_EP_DIR;
1526 UDC_EP_NUM_REG = UDC_EP_DIR;
1530 /* OUT == RX from host */
1531 if (irq_src & UDC_EP0_RX) {
1534 UDC_IRQ_SRC_REG = UDC_EP0_RX;
1535 UDC_EP_NUM_REG = UDC_EP_SEL;
1536 stat = UDC_STAT_FLG_REG;
1537 if (stat & UDC_ACK) {
1540 /* read next OUT packet of request, maybe
1541 * reactiviting the fifo; stall on errors.
1543 if (!req || (stat = read_fifo(ep0, req)) < 0) {
1544 UDC_SYSCON2_REG = UDC_STALL_CMD;
1545 udc->ep0_pending = 0;
1547 } else if (stat == 0)
1548 UDC_CTRL_REG = UDC_SET_FIFO_EN;
1551 /* activate status stage */
1554 /* that may have STALLed ep0... */
1555 UDC_EP_NUM_REG = UDC_EP_SEL|UDC_EP_DIR;
1556 UDC_CTRL_REG = UDC_CLR_EP;
1557 UDC_CTRL_REG = UDC_SET_FIFO_EN;
1558 UDC_EP_NUM_REG = UDC_EP_DIR;
1559 udc->ep0_pending = 0;
1562 /* ack status stage of IN transfer */
1567 } else if (stat & UDC_STALL) {
1568 UDC_CTRL_REG = UDC_CLR_HALT;
1575 /* SETUP starts all control transfers */
1576 if (irq_src & UDC_SETUP) {
1579 struct usb_ctrlrequest r;
1581 int status = -EINVAL;
1584 /* read the (latest) SETUP message */
1586 UDC_EP_NUM_REG = UDC_SETUP_SEL;
1587 /* two bytes at a time */
1588 u.word[0] = UDC_DATA_REG;
1589 u.word[1] = UDC_DATA_REG;
1590 u.word[2] = UDC_DATA_REG;
1591 u.word[3] = UDC_DATA_REG;
1593 } while (UDC_IRQ_SRC_REG & UDC_SETUP);
1595 #define w_value le16_to_cpup (&u.r.wValue)
1596 #define w_index le16_to_cpup (&u.r.wIndex)
1597 #define w_length le16_to_cpup (&u.r.wLength)
1599 /* Delegate almost all control requests to the gadget driver,
1600 * except for a handful of ch9 status/feature requests that
1601 * hardware doesn't autodecode _and_ the gadget API hides.
1603 udc->ep0_in = (u.r.bRequestType & USB_DIR_IN) != 0;
1604 udc->ep0_set_config = 0;
1605 udc->ep0_pending = 1;
1608 switch (u.r.bRequest) {
1609 case USB_REQ_SET_CONFIGURATION:
1610 /* udc needs to know when ep != 0 is valid */
1611 if (u.r.bRequestType != USB_RECIP_DEVICE)
1615 udc->ep0_set_config = 1;
1616 udc->ep0_reset_config = (w_value == 0);
1617 VDBG("set config %d\n", w_value);
1619 /* update udc NOW since gadget driver may start
1620 * queueing requests immediately; clear config
1621 * later if it fails the request.
1623 if (udc->ep0_reset_config)
1624 UDC_SYSCON2_REG = UDC_CLR_CFG;
1626 UDC_SYSCON2_REG = UDC_DEV_CFG;
1629 case USB_REQ_CLEAR_FEATURE:
1630 /* clear endpoint halt */
1631 if (u.r.bRequestType != USB_RECIP_ENDPOINT)
1633 if (w_value != USB_ENDPOINT_HALT
1636 ep = &udc->ep[w_index & 0xf];
1638 if (w_index & USB_DIR_IN)
1640 if (ep->bmAttributes == USB_ENDPOINT_XFER_ISOC
1644 UDC_CTRL_REG = udc->clr_halt;
1646 if (!(ep->bEndpointAddress & USB_DIR_IN)) {
1647 UDC_CTRL_REG = UDC_SET_FIFO_EN;
1648 ep->ackwait = 1 + ep->double_buf;
1650 /* NOTE: assumes the host behaves sanely,
1651 * only clearing real halts. Else we may
1652 * need to kill pending transfers and then
1653 * restart the queue... very messy for DMA!
1656 VDBG("%s halt cleared by host\n", ep->name);
1657 goto ep0out_status_stage;
1658 case USB_REQ_SET_FEATURE:
1659 /* set endpoint halt */
1660 if (u.r.bRequestType != USB_RECIP_ENDPOINT)
1662 if (w_value != USB_ENDPOINT_HALT
1665 ep = &udc->ep[w_index & 0xf];
1666 if (w_index & USB_DIR_IN)
1668 if (ep->bmAttributes == USB_ENDPOINT_XFER_ISOC
1669 || ep == ep0 || !ep->desc)
1671 if (use_dma && ep->has_dma) {
1672 /* this has rude side-effects (aborts) and
1673 * can't really work if DMA-IN is active
1675 DBG("%s host set_halt, NYET \n", ep->name);
1679 /* can't halt if fifo isn't empty... */
1680 UDC_CTRL_REG = UDC_CLR_EP;
1681 UDC_CTRL_REG = UDC_SET_HALT;
1682 VDBG("%s halted by host\n", ep->name);
1683 ep0out_status_stage:
1685 UDC_EP_NUM_REG = UDC_EP_SEL|UDC_EP_DIR;
1686 UDC_CTRL_REG = UDC_CLR_EP;
1687 UDC_CTRL_REG = UDC_SET_FIFO_EN;
1688 UDC_EP_NUM_REG = UDC_EP_DIR;
1689 udc->ep0_pending = 0;
1691 case USB_REQ_GET_STATUS:
1692 /* return interface status. if we were pedantic,
1693 * we'd detect non-existent interfaces, and stall.
1695 if (u.r.bRequestType
1696 != (USB_DIR_IN|USB_RECIP_INTERFACE))
1698 /* return two zero bytes */
1699 UDC_EP_NUM_REG = UDC_EP_SEL|UDC_EP_DIR;
1701 UDC_CTRL_REG = UDC_SET_FIFO_EN;
1702 UDC_EP_NUM_REG = UDC_EP_DIR;
1704 VDBG("GET_STATUS, interface %d\n", w_index);
1705 /* next, status stage */
1709 /* activate the ep0out fifo right away */
1710 if (!udc->ep0_in && w_length) {
1712 UDC_CTRL_REG = UDC_SET_FIFO_EN;
1715 /* gadget drivers see class/vendor specific requests,
1716 * {SET,GET}_{INTERFACE,DESCRIPTOR,CONFIGURATION},
1719 VDBG("SETUP %02x.%02x v%04x i%04x l%04x\n",
1720 u.r.bRequestType, u.r.bRequest,
1721 w_value, w_index, w_length);
1727 /* The gadget driver may return an error here,
1728 * causing an immediate protocol stall.
1730 * Else it must issue a response, either queueing a
1731 * response buffer for the DATA stage, or halting ep0
1732 * (causing a protocol stall, not a real halt). A
1733 * zero length buffer means no DATA stage.
1735 * It's fine to issue that response after the setup()
1736 * call returns, and this IRQ was handled.
1739 spin_unlock(&udc->lock);
1740 status = udc->driver->setup (&udc->gadget, &u.r);
1741 spin_lock(&udc->lock);
1747 VDBG("req %02x.%02x protocol STALL; stat %d\n",
1748 u.r.bRequestType, u.r.bRequest, status);
1749 if (udc->ep0_set_config) {
1750 if (udc->ep0_reset_config)
1751 WARN("error resetting config?\n");
1753 UDC_SYSCON2_REG = UDC_CLR_CFG;
1755 UDC_SYSCON2_REG = UDC_STALL_CMD;
1756 udc->ep0_pending = 0;
1761 /*-------------------------------------------------------------------------*/
1763 #define OTG_FLAGS (UDC_B_HNP_ENABLE|UDC_A_HNP_SUPPORT|UDC_A_ALT_HNP_SUPPORT)
1765 static void devstate_irq(struct omap_udc *udc, u16 irq_src)
1767 u16 devstat, change;
1769 devstat = UDC_DEVSTAT_REG;
1770 change = devstat ^ udc->devstat;
1771 udc->devstat = devstat;
1773 if (change & (UDC_USB_RESET|UDC_ATT)) {
1776 if (change & UDC_ATT) {
1777 /* driver for any external transceiver will
1778 * have called omap_vbus_session() already
1780 if (devstat & UDC_ATT) {
1781 udc->gadget.speed = USB_SPEED_FULL;
1783 if (!udc->transceiver)
1785 // if (driver->connect) call it
1786 } else if (udc->gadget.speed != USB_SPEED_UNKNOWN) {
1787 udc->gadget.speed = USB_SPEED_UNKNOWN;
1788 if (!udc->transceiver)
1789 pullup_disable(udc);
1790 DBG("disconnect, gadget %s\n",
1791 udc->driver->driver.name);
1792 if (udc->driver->disconnect) {
1793 spin_unlock(&udc->lock);
1794 udc->driver->disconnect(&udc->gadget);
1795 spin_lock(&udc->lock);
1801 if (change & UDC_USB_RESET) {
1802 if (devstat & UDC_USB_RESET) {
1805 udc->gadget.speed = USB_SPEED_FULL;
1806 INFO("USB reset done, gadget %s\n",
1807 udc->driver->driver.name);
1808 /* ep0 traffic is legal from now on */
1809 UDC_IRQ_EN_REG = UDC_DS_CHG_IE | UDC_EP0_IE;
1811 change &= ~UDC_USB_RESET;
1814 if (change & UDC_SUS) {
1815 if (udc->gadget.speed != USB_SPEED_UNKNOWN) {
1816 // FIXME tell isp1301 to suspend/resume (?)
1817 if (devstat & UDC_SUS) {
1820 /* HNP could be under way already */
1821 if (udc->gadget.speed == USB_SPEED_FULL
1822 && udc->driver->suspend) {
1823 spin_unlock(&udc->lock);
1824 udc->driver->suspend(&udc->gadget);
1825 spin_lock(&udc->lock);
1827 if (udc->transceiver)
1828 otg_set_suspend(udc->transceiver, 1);
1831 if (udc->transceiver)
1832 otg_set_suspend(udc->transceiver, 0);
1833 if (udc->gadget.speed == USB_SPEED_FULL
1834 && udc->driver->resume) {
1835 spin_unlock(&udc->lock);
1836 udc->driver->resume(&udc->gadget);
1837 spin_lock(&udc->lock);
1843 if (!cpu_is_omap15xx() && (change & OTG_FLAGS)) {
1845 change &= ~OTG_FLAGS;
1848 change &= ~(UDC_CFG|UDC_DEF|UDC_ADD);
1850 VDBG("devstat %03x, ignore change %03x\n",
1853 UDC_IRQ_SRC_REG = UDC_DS_CHG;
1857 omap_udc_irq(int irq, void *_udc, struct pt_regs *r)
1859 struct omap_udc *udc = _udc;
1861 irqreturn_t status = IRQ_NONE;
1862 unsigned long flags;
1864 spin_lock_irqsave(&udc->lock, flags);
1865 irq_src = UDC_IRQ_SRC_REG;
1867 /* Device state change (usb ch9 stuff) */
1868 if (irq_src & UDC_DS_CHG) {
1869 devstate_irq(_udc, irq_src);
1870 status = IRQ_HANDLED;
1871 irq_src &= ~UDC_DS_CHG;
1874 /* EP0 control transfers */
1875 if (irq_src & (UDC_EP0_RX|UDC_SETUP|UDC_EP0_TX)) {
1876 ep0_irq(_udc, irq_src);
1877 status = IRQ_HANDLED;
1878 irq_src &= ~(UDC_EP0_RX|UDC_SETUP|UDC_EP0_TX);
1881 /* DMA transfer completion */
1882 if (use_dma && (irq_src & (UDC_TXN_DONE|UDC_RXN_CNT|UDC_RXN_EOT))) {
1883 dma_irq(_udc, irq_src);
1884 status = IRQ_HANDLED;
1885 irq_src &= ~(UDC_TXN_DONE|UDC_RXN_CNT|UDC_RXN_EOT);
1888 irq_src &= ~(UDC_SOF|UDC_EPN_TX|UDC_EPN_RX);
1890 DBG("udc_irq, unhandled %03x\n", irq_src);
1891 spin_unlock_irqrestore(&udc->lock, flags);
1896 /* workaround for seemingly-lost IRQs for RX ACKs... */
1897 #define PIO_OUT_TIMEOUT (jiffies + HZ/3)
1898 #define HALF_FULL(f) (!((f)&(UDC_NON_ISO_FIFO_FULL|UDC_NON_ISO_FIFO_EMPTY)))
1900 static void pio_out_timer(unsigned long _ep)
1902 struct omap_ep *ep = (void *) _ep;
1903 unsigned long flags;
1906 spin_lock_irqsave(&ep->udc->lock, flags);
1907 if (!list_empty(&ep->queue) && ep->ackwait) {
1908 use_ep(ep, UDC_EP_SEL);
1909 stat_flg = UDC_STAT_FLG_REG;
1911 if ((stat_flg & UDC_ACK) && (!(stat_flg & UDC_FIFO_EN)
1912 || (ep->double_buf && HALF_FULL(stat_flg)))) {
1913 struct omap_req *req;
1915 VDBG("%s: lose, %04x\n", ep->ep.name, stat_flg);
1916 req = container_of(ep->queue.next,
1917 struct omap_req, queue);
1918 (void) read_fifo(ep, req);
1919 UDC_EP_NUM_REG = ep->bEndpointAddress;
1920 UDC_CTRL_REG = UDC_SET_FIFO_EN;
1921 ep->ackwait = 1 + ep->double_buf;
1927 mod_timer(&ep->timer, PIO_OUT_TIMEOUT);
1928 spin_unlock_irqrestore(&ep->udc->lock, flags);
1932 omap_udc_pio_irq(int irq, void *_dev, struct pt_regs *r)
1934 u16 epn_stat, irq_src;
1935 irqreturn_t status = IRQ_NONE;
1938 struct omap_udc *udc = _dev;
1939 struct omap_req *req;
1940 unsigned long flags;
1942 spin_lock_irqsave(&udc->lock, flags);
1943 epn_stat = UDC_EPN_STAT_REG;
1944 irq_src = UDC_IRQ_SRC_REG;
1946 /* handle OUT first, to avoid some wasteful NAKs */
1947 if (irq_src & UDC_EPN_RX) {
1948 epnum = (epn_stat >> 8) & 0x0f;
1949 UDC_IRQ_SRC_REG = UDC_EPN_RX;
1950 status = IRQ_HANDLED;
1951 ep = &udc->ep[epnum];
1954 UDC_EP_NUM_REG = epnum | UDC_EP_SEL;
1956 if ((UDC_STAT_FLG_REG & UDC_ACK)) {
1958 if (!list_empty(&ep->queue)) {
1960 req = container_of(ep->queue.next,
1961 struct omap_req, queue);
1962 stat = read_fifo(ep, req);
1963 if (!ep->double_buf)
1967 /* min 6 clock delay before clearing EP_SEL ... */
1968 epn_stat = UDC_EPN_STAT_REG;
1969 epn_stat = UDC_EPN_STAT_REG;
1970 UDC_EP_NUM_REG = epnum;
1972 /* enabling fifo _after_ clearing ACK, contrary to docs,
1973 * reduces lossage; timer still needed though (sigh).
1976 UDC_CTRL_REG = UDC_SET_FIFO_EN;
1977 ep->ackwait = 1 + ep->double_buf;
1979 mod_timer(&ep->timer, PIO_OUT_TIMEOUT);
1982 /* then IN transfers */
1983 else if (irq_src & UDC_EPN_TX) {
1984 epnum = epn_stat & 0x0f;
1985 UDC_IRQ_SRC_REG = UDC_EPN_TX;
1986 status = IRQ_HANDLED;
1987 ep = &udc->ep[16 + epnum];
1990 UDC_EP_NUM_REG = epnum | UDC_EP_DIR | UDC_EP_SEL;
1991 if ((UDC_STAT_FLG_REG & UDC_ACK)) {
1993 if (!list_empty(&ep->queue)) {
1994 req = container_of(ep->queue.next,
1995 struct omap_req, queue);
1996 (void) write_fifo(ep, req);
1999 /* min 6 clock delay before clearing EP_SEL ... */
2000 epn_stat = UDC_EPN_STAT_REG;
2001 epn_stat = UDC_EPN_STAT_REG;
2002 UDC_EP_NUM_REG = epnum | UDC_EP_DIR;
2003 /* then 6 clocks before it'd tx */
2006 spin_unlock_irqrestore(&udc->lock, flags);
2012 omap_udc_iso_irq(int irq, void *_dev, struct pt_regs *r)
2014 struct omap_udc *udc = _dev;
2017 unsigned long flags;
2019 spin_lock_irqsave(&udc->lock, flags);
2021 /* handle all non-DMA ISO transfers */
2022 list_for_each_entry (ep, &udc->iso, iso) {
2024 struct omap_req *req;
2026 if (ep->has_dma || list_empty(&ep->queue))
2028 req = list_entry(ep->queue.next, struct omap_req, queue);
2030 use_ep(ep, UDC_EP_SEL);
2031 stat = UDC_STAT_FLG_REG;
2033 /* NOTE: like the other controller drivers, this isn't
2034 * currently reporting lost or damaged frames.
2036 if (ep->bEndpointAddress & USB_DIR_IN) {
2037 if (stat & UDC_MISS_IN)
2038 /* done(ep, req, -EPROTO) */;
2040 write_fifo(ep, req);
2044 if (stat & UDC_NO_RXPACKET)
2045 status = -EREMOTEIO;
2046 else if (stat & UDC_ISO_ERR)
2048 else if (stat & UDC_DATA_FLUSH)
2052 /* done(ep, req, status) */;
2057 /* 6 wait states before next EP */
2060 if (!list_empty(&ep->queue))
2064 UDC_IRQ_EN_REG &= ~UDC_SOF_IE;
2065 UDC_IRQ_SRC_REG = UDC_SOF;
2067 spin_unlock_irqrestore(&udc->lock, flags);
2072 /*-------------------------------------------------------------------------*/
2075 int usb_gadget_register_driver (struct usb_gadget_driver *driver)
2077 int status = -ENODEV;
2079 unsigned long flags;
2081 /* basic sanity tests */
2085 // FIXME if otg, check: driver->is_otg
2086 || driver->speed < USB_SPEED_FULL
2092 spin_lock_irqsave(&udc->lock, flags);
2094 spin_unlock_irqrestore(&udc->lock, flags);
2099 list_for_each_entry (ep, &udc->gadget.ep_list, ep.ep_list) {
2101 if (ep->bmAttributes == USB_ENDPOINT_XFER_ISOC)
2104 UDC_CTRL_REG = UDC_SET_HALT;
2106 udc->ep0_pending = 0;
2107 udc->ep[0].irqs = 0;
2108 udc->softconnect = 1;
2110 /* hook up the driver */
2111 driver->driver.bus = NULL;
2112 udc->driver = driver;
2113 udc->gadget.dev.driver = &driver->driver;
2114 spin_unlock_irqrestore(&udc->lock, flags);
2116 if (udc->dc_clk != NULL)
2117 omap_udc_enable_clock(1);
2119 status = driver->bind (&udc->gadget);
2121 DBG("bind to %s --> %d\n", driver->driver.name, status);
2122 udc->gadget.dev.driver = NULL;
2126 DBG("bound to driver %s\n", driver->driver.name);
2128 UDC_IRQ_SRC_REG = UDC_IRQ_SRC_MASK;
2130 /* connect to bus through transceiver */
2131 if (udc->transceiver) {
2132 status = otg_set_peripheral(udc->transceiver, &udc->gadget);
2134 ERR("can't bind to transceiver\n");
2135 driver->unbind (&udc->gadget);
2136 udc->gadget.dev.driver = NULL;
2141 if (can_pullup(udc))
2142 pullup_enable (udc);
2144 pullup_disable (udc);
2147 /* boards that don't have VBUS sensing can't autogate 48MHz;
2148 * can't enter deep sleep while a gadget driver is active.
2150 if (machine_is_omap_innovator() || machine_is_omap_osk())
2151 omap_vbus_session(&udc->gadget, 1);
2154 if (udc->dc_clk != NULL)
2155 omap_udc_enable_clock(0);
2158 EXPORT_SYMBOL(usb_gadget_register_driver);
2160 int usb_gadget_unregister_driver (struct usb_gadget_driver *driver)
2162 unsigned long flags;
2163 int status = -ENODEV;
2167 if (!driver || driver != udc->driver)
2170 if (udc->dc_clk != NULL)
2171 omap_udc_enable_clock(1);
2173 if (machine_is_omap_innovator() || machine_is_omap_osk())
2174 omap_vbus_session(&udc->gadget, 0);
2176 if (udc->transceiver)
2177 (void) otg_set_peripheral(udc->transceiver, NULL);
2179 pullup_disable(udc);
2181 spin_lock_irqsave(&udc->lock, flags);
2183 spin_unlock_irqrestore(&udc->lock, flags);
2185 driver->unbind(&udc->gadget);
2186 udc->gadget.dev.driver = NULL;
2189 if (udc->dc_clk != NULL)
2190 omap_udc_enable_clock(0);
2191 DBG("unregistered driver '%s'\n", driver->driver.name);
2194 EXPORT_SYMBOL(usb_gadget_unregister_driver);
2197 /*-------------------------------------------------------------------------*/
2199 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
2201 #include <linux/seq_file.h>
2203 static const char proc_filename[] = "driver/udc";
2205 #define FOURBITS "%s%s%s%s"
2206 #define EIGHTBITS FOURBITS FOURBITS
2208 static void proc_ep_show(struct seq_file *s, struct omap_ep *ep)
2211 struct omap_req *req;
2216 if (use_dma && ep->has_dma)
2217 snprintf(buf, sizeof buf, "(%cxdma%d lch%d) ",
2218 (ep->bEndpointAddress & USB_DIR_IN) ? 't' : 'r',
2219 ep->dma_channel - 1, ep->lch);
2223 stat_flg = UDC_STAT_FLG_REG;
2225 "\n%s %s%s%sirqs %ld stat %04x " EIGHTBITS FOURBITS "%s\n",
2227 ep->double_buf ? "dbuf " : "",
2228 ({char *s; switch(ep->ackwait){
2229 case 0: s = ""; break;
2230 case 1: s = "(ackw) "; break;
2231 case 2: s = "(ackw2) "; break;
2232 default: s = "(?) "; break;
2235 (stat_flg & UDC_NO_RXPACKET) ? "no_rxpacket " : "",
2236 (stat_flg & UDC_MISS_IN) ? "miss_in " : "",
2237 (stat_flg & UDC_DATA_FLUSH) ? "data_flush " : "",
2238 (stat_flg & UDC_ISO_ERR) ? "iso_err " : "",
2239 (stat_flg & UDC_ISO_FIFO_EMPTY) ? "iso_fifo_empty " : "",
2240 (stat_flg & UDC_ISO_FIFO_FULL) ? "iso_fifo_full " : "",
2241 (stat_flg & UDC_EP_HALTED) ? "HALT " : "",
2242 (stat_flg & UDC_STALL) ? "STALL " : "",
2243 (stat_flg & UDC_NAK) ? "NAK " : "",
2244 (stat_flg & UDC_ACK) ? "ACK " : "",
2245 (stat_flg & UDC_FIFO_EN) ? "fifo_en " : "",
2246 (stat_flg & UDC_NON_ISO_FIFO_EMPTY) ? "fifo_empty " : "",
2247 (stat_flg & UDC_NON_ISO_FIFO_FULL) ? "fifo_full " : "");
2249 if (list_empty (&ep->queue))
2250 seq_printf(s, "\t(queue empty)\n");
2252 list_for_each_entry (req, &ep->queue, queue) {
2253 unsigned length = req->req.actual;
2255 if (use_dma && buf[0]) {
2256 length += ((ep->bEndpointAddress & USB_DIR_IN)
2257 ? dma_src_len : dma_dest_len)
2258 (ep, req->req.dma + length);
2261 seq_printf(s, "\treq %p len %d/%d buf %p\n",
2263 req->req.length, req->req.buf);
2267 static char *trx_mode(unsigned m, int enabled)
2270 case 0: return enabled ? "*6wire" : "unused";
2271 case 1: return "4wire";
2272 case 2: return "3wire";
2273 case 3: return "6wire";
2274 default: return "unknown";
2278 static int proc_otg_show(struct seq_file *s)
2284 trans = USB_TRANSCEIVER_CTRL_REG;
2285 seq_printf(s, "\nOTG rev %d.%d, transceiver_ctrl %05x\n",
2286 tmp >> 4, tmp & 0xf, trans);
2287 tmp = OTG_SYSCON_1_REG;
2288 seq_printf(s, "otg_syscon1 %08x usb2 %s, usb1 %s, usb0 %s,"
2290 trx_mode(USB2_TRX_MODE(tmp), trans & CONF_USB2_UNI_R),
2291 trx_mode(USB1_TRX_MODE(tmp), trans & CONF_USB1_UNI_R),
2292 (USB0_TRX_MODE(tmp) == 0 && !cpu_is_omap1710())
2294 : trx_mode(USB0_TRX_MODE(tmp), 1),
2295 (tmp & OTG_IDLE_EN) ? " !otg" : "",
2296 (tmp & HST_IDLE_EN) ? " !host" : "",
2297 (tmp & DEV_IDLE_EN) ? " !dev" : "",
2298 (tmp & OTG_RESET_DONE) ? " reset_done" : " reset_active");
2299 tmp = OTG_SYSCON_2_REG;
2300 seq_printf(s, "otg_syscon2 %08x%s" EIGHTBITS
2301 " b_ase_brst=%d hmc=%d\n", tmp,
2302 (tmp & OTG_EN) ? " otg_en" : "",
2303 (tmp & USBX_SYNCHRO) ? " synchro" : "",
2304 // much more SRP stuff
2305 (tmp & SRP_DATA) ? " srp_data" : "",
2306 (tmp & SRP_VBUS) ? " srp_vbus" : "",
2307 (tmp & OTG_PADEN) ? " otg_paden" : "",
2308 (tmp & HMC_PADEN) ? " hmc_paden" : "",
2309 (tmp & UHOST_EN) ? " uhost_en" : "",
2310 (tmp & HMC_TLLSPEED) ? " tllspeed" : "",
2311 (tmp & HMC_TLLATTACH) ? " tllattach" : "",
2315 seq_printf(s, "otg_ctrl %06x" EIGHTBITS EIGHTBITS "%s\n", tmp,
2316 (tmp & OTG_ASESSVLD) ? " asess" : "",
2317 (tmp & OTG_BSESSEND) ? " bsess_end" : "",
2318 (tmp & OTG_BSESSVLD) ? " bsess" : "",
2319 (tmp & OTG_VBUSVLD) ? " vbus" : "",
2320 (tmp & OTG_ID) ? " id" : "",
2321 (tmp & OTG_DRIVER_SEL) ? " DEVICE" : " HOST",
2322 (tmp & OTG_A_SETB_HNPEN) ? " a_setb_hnpen" : "",
2323 (tmp & OTG_A_BUSREQ) ? " a_bus" : "",
2324 (tmp & OTG_B_HNPEN) ? " b_hnpen" : "",
2325 (tmp & OTG_B_BUSREQ) ? " b_bus" : "",
2326 (tmp & OTG_BUSDROP) ? " busdrop" : "",
2327 (tmp & OTG_PULLDOWN) ? " down" : "",
2328 (tmp & OTG_PULLUP) ? " up" : "",
2329 (tmp & OTG_DRV_VBUS) ? " drv" : "",
2330 (tmp & OTG_PD_VBUS) ? " pd_vb" : "",
2331 (tmp & OTG_PU_VBUS) ? " pu_vb" : "",
2332 (tmp & OTG_PU_ID) ? " pu_id" : ""
2334 tmp = OTG_IRQ_EN_REG;
2335 seq_printf(s, "otg_irq_en %04x" "\n", tmp);
2336 tmp = OTG_IRQ_SRC_REG;
2337 seq_printf(s, "otg_irq_src %04x" "\n", tmp);
2338 tmp = OTG_OUTCTRL_REG;
2339 seq_printf(s, "otg_outctrl %04x" "\n", tmp);
2341 seq_printf(s, "otg_test %04x" "\n", tmp);
2345 static int proc_udc_show(struct seq_file *s, void *_)
2349 unsigned long flags;
2351 spin_lock_irqsave(&udc->lock, flags);
2353 seq_printf(s, "%s, version: " DRIVER_VERSION
2359 use_dma ? " (dma)" : "");
2361 tmp = UDC_REV_REG & 0xff;
2363 "UDC rev %d.%d, fifo mode %d, gadget %s\n"
2364 "hmc %d, transceiver %s\n",
2365 tmp >> 4, tmp & 0xf,
2367 udc->driver ? udc->driver->driver.name : "(none)",
2369 udc->transceiver ? udc->transceiver->label : "(none)");
2370 seq_printf(s, "ULPD control %04x req %04x status %04x\n",
2371 __REG16(ULPD_CLOCK_CTRL),
2372 __REG16(ULPD_SOFT_REQ),
2373 __REG16(ULPD_STATUS_REQ));
2375 /* OTG controller registers */
2376 if (!cpu_is_omap15xx())
2379 tmp = UDC_SYSCON1_REG;
2380 seq_printf(s, "\nsyscon1 %04x" EIGHTBITS "\n", tmp,
2381 (tmp & UDC_CFG_LOCK) ? " cfg_lock" : "",
2382 (tmp & UDC_DATA_ENDIAN) ? " data_endian" : "",
2383 (tmp & UDC_DMA_ENDIAN) ? " dma_endian" : "",
2384 (tmp & UDC_NAK_EN) ? " nak" : "",
2385 (tmp & UDC_AUTODECODE_DIS) ? " autodecode_dis" : "",
2386 (tmp & UDC_SELF_PWR) ? " self_pwr" : "",
2387 (tmp & UDC_SOFF_DIS) ? " soff_dis" : "",
2388 (tmp & UDC_PULLUP_EN) ? " PULLUP" : "");
2389 // syscon2 is write-only
2391 /* UDC controller registers */
2392 if (!(tmp & UDC_PULLUP_EN)) {
2393 seq_printf(s, "(suspended)\n");
2394 spin_unlock_irqrestore(&udc->lock, flags);
2398 tmp = UDC_DEVSTAT_REG;
2399 seq_printf(s, "devstat %04x" EIGHTBITS "%s%s\n", tmp,
2400 (tmp & UDC_B_HNP_ENABLE) ? " b_hnp" : "",
2401 (tmp & UDC_A_HNP_SUPPORT) ? " a_hnp" : "",
2402 (tmp & UDC_A_ALT_HNP_SUPPORT) ? " a_alt_hnp" : "",
2403 (tmp & UDC_R_WK_OK) ? " r_wk_ok" : "",
2404 (tmp & UDC_USB_RESET) ? " usb_reset" : "",
2405 (tmp & UDC_SUS) ? " SUS" : "",
2406 (tmp & UDC_CFG) ? " CFG" : "",
2407 (tmp & UDC_ADD) ? " ADD" : "",
2408 (tmp & UDC_DEF) ? " DEF" : "",
2409 (tmp & UDC_ATT) ? " ATT" : "");
2410 seq_printf(s, "sof %04x\n", UDC_SOF_REG);
2411 tmp = UDC_IRQ_EN_REG;
2412 seq_printf(s, "irq_en %04x" FOURBITS "%s\n", tmp,
2413 (tmp & UDC_SOF_IE) ? " sof" : "",
2414 (tmp & UDC_EPN_RX_IE) ? " epn_rx" : "",
2415 (tmp & UDC_EPN_TX_IE) ? " epn_tx" : "",
2416 (tmp & UDC_DS_CHG_IE) ? " ds_chg" : "",
2417 (tmp & UDC_EP0_IE) ? " ep0" : "");
2418 tmp = UDC_IRQ_SRC_REG;
2419 seq_printf(s, "irq_src %04x" EIGHTBITS "%s%s\n", tmp,
2420 (tmp & UDC_TXN_DONE) ? " txn_done" : "",
2421 (tmp & UDC_RXN_CNT) ? " rxn_cnt" : "",
2422 (tmp & UDC_RXN_EOT) ? " rxn_eot" : "",
2423 (tmp & UDC_SOF) ? " sof" : "",
2424 (tmp & UDC_EPN_RX) ? " epn_rx" : "",
2425 (tmp & UDC_EPN_TX) ? " epn_tx" : "",
2426 (tmp & UDC_DS_CHG) ? " ds_chg" : "",
2427 (tmp & UDC_SETUP) ? " setup" : "",
2428 (tmp & UDC_EP0_RX) ? " ep0out" : "",
2429 (tmp & UDC_EP0_TX) ? " ep0in" : "");
2433 tmp = UDC_DMA_IRQ_EN_REG;
2434 seq_printf(s, "dma_irq_en %04x%s" EIGHTBITS "\n", tmp,
2435 (tmp & UDC_TX_DONE_IE(3)) ? " tx2_done" : "",
2436 (tmp & UDC_RX_CNT_IE(3)) ? " rx2_cnt" : "",
2437 (tmp & UDC_RX_EOT_IE(3)) ? " rx2_eot" : "",
2439 (tmp & UDC_TX_DONE_IE(2)) ? " tx1_done" : "",
2440 (tmp & UDC_RX_CNT_IE(2)) ? " rx1_cnt" : "",
2441 (tmp & UDC_RX_EOT_IE(2)) ? " rx1_eot" : "",
2443 (tmp & UDC_TX_DONE_IE(1)) ? " tx0_done" : "",
2444 (tmp & UDC_RX_CNT_IE(1)) ? " rx0_cnt" : "",
2445 (tmp & UDC_RX_EOT_IE(1)) ? " rx0_eot" : "");
2447 tmp = UDC_RXDMA_CFG_REG;
2448 seq_printf(s, "rxdma_cfg %04x\n", tmp);
2450 for (i = 0; i < 3; i++) {
2451 if ((tmp & (0x0f << (i * 4))) == 0)
2453 seq_printf(s, "rxdma[%d] %04x\n", i,
2454 UDC_RXDMA_REG(i + 1));
2457 tmp = UDC_TXDMA_CFG_REG;
2458 seq_printf(s, "txdma_cfg %04x\n", tmp);
2460 for (i = 0; i < 3; i++) {
2461 if (!(tmp & (0x0f << (i * 4))))
2463 seq_printf(s, "txdma[%d] %04x\n", i,
2464 UDC_TXDMA_REG(i + 1));
2469 tmp = UDC_DEVSTAT_REG;
2470 if (tmp & UDC_ATT) {
2471 proc_ep_show(s, &udc->ep[0]);
2472 if (tmp & UDC_ADD) {
2473 list_for_each_entry (ep, &udc->gadget.ep_list,
2476 proc_ep_show(s, ep);
2480 spin_unlock_irqrestore(&udc->lock, flags);
2484 static int proc_udc_open(struct inode *inode, struct file *file)
2486 return single_open(file, proc_udc_show, NULL);
2489 static struct file_operations proc_ops = {
2490 .open = proc_udc_open,
2492 .llseek = seq_lseek,
2493 .release = single_release,
2496 static void create_proc_file(void)
2498 struct proc_dir_entry *pde;
2500 pde = create_proc_entry (proc_filename, 0, NULL);
2502 pde->proc_fops = &proc_ops;
2505 static void remove_proc_file(void)
2507 remove_proc_entry(proc_filename, NULL);
2512 static inline void create_proc_file(void) {}
2513 static inline void remove_proc_file(void) {}
2517 /*-------------------------------------------------------------------------*/
2519 /* Before this controller can enumerate, we need to pick an endpoint
2520 * configuration, or "fifo_mode" That involves allocating 2KB of packet
2521 * buffer space among the endpoints we'll be operating.
2523 * NOTE: as of OMAP 1710 ES2.0, writing a new endpoint config when
2524 * UDC_SYSCON_1_REG.CFG_LOCK is set can now work. We won't use that
2525 * capability yet though.
2527 static unsigned __init
2528 omap_ep_setup(char *name, u8 addr, u8 type,
2529 unsigned buf, unsigned maxp, int dbuf)
2534 /* OUT endpoints first, then IN */
2535 ep = &udc->ep[addr & 0xf];
2536 if (addr & USB_DIR_IN)
2539 /* in case of ep init table bugs */
2540 BUG_ON(ep->name[0]);
2542 /* chip setup ... bit values are same for IN, OUT */
2543 if (type == USB_ENDPOINT_XFER_ISOC) {
2545 case 8: epn_rxtx = 0 << 12; break;
2546 case 16: epn_rxtx = 1 << 12; break;
2547 case 32: epn_rxtx = 2 << 12; break;
2548 case 64: epn_rxtx = 3 << 12; break;
2549 case 128: epn_rxtx = 4 << 12; break;
2550 case 256: epn_rxtx = 5 << 12; break;
2551 case 512: epn_rxtx = 6 << 12; break;
2554 epn_rxtx |= UDC_EPN_RX_ISO;
2557 /* double-buffering "not supported" on 15xx,
2558 * and ignored for PIO-IN on 16xx
2560 if (!use_dma || cpu_is_omap15xx())
2564 case 8: epn_rxtx = 0 << 12; break;
2565 case 16: epn_rxtx = 1 << 12; break;
2566 case 32: epn_rxtx = 2 << 12; break;
2567 case 64: epn_rxtx = 3 << 12; break;
2571 epn_rxtx |= UDC_EPN_RX_DB;
2572 init_timer(&ep->timer);
2573 ep->timer.function = pio_out_timer;
2574 ep->timer.data = (unsigned long) ep;
2577 epn_rxtx |= UDC_EPN_RX_VALID;
2579 epn_rxtx |= buf >> 3;
2581 DBG("%s addr %02x rxtx %04x maxp %d%s buf %d\n",
2582 name, addr, epn_rxtx, maxp, dbuf ? "x2" : "", buf);
2584 if (addr & USB_DIR_IN)
2585 UDC_EP_TX_REG(addr & 0xf) = epn_rxtx;
2587 UDC_EP_RX_REG(addr) = epn_rxtx;
2589 /* next endpoint's buffer starts after this one's */
2595 /* set up driver data structures */
2596 BUG_ON(strlen(name) >= sizeof ep->name);
2597 strlcpy(ep->name, name, sizeof ep->name);
2598 INIT_LIST_HEAD(&ep->queue);
2599 INIT_LIST_HEAD(&ep->iso);
2600 ep->bEndpointAddress = addr;
2601 ep->bmAttributes = type;
2602 ep->double_buf = dbuf;
2605 ep->ep.name = ep->name;
2606 ep->ep.ops = &omap_ep_ops;
2607 ep->ep.maxpacket = ep->maxpacket = maxp;
2608 list_add_tail (&ep->ep.ep_list, &udc->gadget.ep_list);
2613 static void omap_udc_release(struct device *dev)
2615 complete(udc->done);
2621 omap_udc_setup(struct platform_device *odev, struct otg_transceiver *xceiv)
2625 /* abolish any previous hardware state */
2626 UDC_SYSCON1_REG = 0;
2628 UDC_IRQ_SRC_REG = UDC_IRQ_SRC_MASK;
2629 UDC_DMA_IRQ_EN_REG = 0;
2630 UDC_RXDMA_CFG_REG = 0;
2631 UDC_TXDMA_CFG_REG = 0;
2633 /* UDC_PULLUP_EN gates the chip clock */
2634 // OTG_SYSCON_1_REG |= DEV_IDLE_EN;
2636 udc = kmalloc (sizeof *udc, SLAB_KERNEL);
2640 memset(udc, 0, sizeof *udc);
2641 spin_lock_init (&udc->lock);
2643 udc->gadget.ops = &omap_gadget_ops;
2644 udc->gadget.ep0 = &udc->ep[0].ep;
2645 INIT_LIST_HEAD(&udc->gadget.ep_list);
2646 INIT_LIST_HEAD(&udc->iso);
2647 udc->gadget.speed = USB_SPEED_UNKNOWN;
2648 udc->gadget.name = driver_name;
2650 device_initialize(&udc->gadget.dev);
2651 strcpy (udc->gadget.dev.bus_id, "gadget");
2652 udc->gadget.dev.release = omap_udc_release;
2653 udc->gadget.dev.parent = &odev->dev;
2655 udc->gadget.dev.dma_mask = odev->dev.dma_mask;
2657 udc->transceiver = xceiv;
2659 /* ep0 is special; put it right after the SETUP buffer */
2660 buf = omap_ep_setup("ep0", 0, USB_ENDPOINT_XFER_CONTROL,
2661 8 /* after SETUP */, 64 /* maxpacket */, 0);
2662 list_del_init(&udc->ep[0].ep.ep_list);
2664 /* initially disable all non-ep0 endpoints */
2665 for (tmp = 1; tmp < 15; tmp++) {
2666 UDC_EP_RX_REG(tmp) = 0;
2667 UDC_EP_TX_REG(tmp) = 0;
2670 #define OMAP_BULK_EP(name,addr) \
2671 buf = omap_ep_setup(name "-bulk", addr, \
2672 USB_ENDPOINT_XFER_BULK, buf, 64, 1);
2673 #define OMAP_INT_EP(name,addr, maxp) \
2674 buf = omap_ep_setup(name "-int", addr, \
2675 USB_ENDPOINT_XFER_INT, buf, maxp, 0);
2676 #define OMAP_ISO_EP(name,addr, maxp) \
2677 buf = omap_ep_setup(name "-iso", addr, \
2678 USB_ENDPOINT_XFER_ISOC, buf, maxp, 1);
2680 switch (fifo_mode) {
2682 OMAP_BULK_EP("ep1in", USB_DIR_IN | 1);
2683 OMAP_BULK_EP("ep2out", USB_DIR_OUT | 2);
2684 OMAP_INT_EP("ep3in", USB_DIR_IN | 3, 16);
2687 OMAP_BULK_EP("ep1in", USB_DIR_IN | 1);
2688 OMAP_BULK_EP("ep2out", USB_DIR_OUT | 2);
2689 OMAP_INT_EP("ep9in", USB_DIR_IN | 9, 16);
2691 OMAP_BULK_EP("ep3in", USB_DIR_IN | 3);
2692 OMAP_BULK_EP("ep4out", USB_DIR_OUT | 4);
2693 OMAP_INT_EP("ep10in", USB_DIR_IN | 10, 16);
2695 OMAP_BULK_EP("ep5in", USB_DIR_IN | 5);
2696 OMAP_BULK_EP("ep5out", USB_DIR_OUT | 5);
2697 OMAP_INT_EP("ep11in", USB_DIR_IN | 11, 16);
2699 OMAP_BULK_EP("ep6in", USB_DIR_IN | 6);
2700 OMAP_BULK_EP("ep6out", USB_DIR_OUT | 6);
2701 OMAP_INT_EP("ep12in", USB_DIR_IN | 12, 16);
2703 OMAP_BULK_EP("ep7in", USB_DIR_IN | 7);
2704 OMAP_BULK_EP("ep7out", USB_DIR_OUT | 7);
2705 OMAP_INT_EP("ep13in", USB_DIR_IN | 13, 16);
2706 OMAP_INT_EP("ep13out", USB_DIR_OUT | 13, 16);
2708 OMAP_BULK_EP("ep8in", USB_DIR_IN | 8);
2709 OMAP_BULK_EP("ep8out", USB_DIR_OUT | 8);
2710 OMAP_INT_EP("ep14in", USB_DIR_IN | 14, 16);
2711 OMAP_INT_EP("ep14out", USB_DIR_OUT | 14, 16);
2713 OMAP_BULK_EP("ep15in", USB_DIR_IN | 15);
2714 OMAP_BULK_EP("ep15out", USB_DIR_OUT | 15);
2719 case 2: /* mixed iso/bulk */
2720 OMAP_ISO_EP("ep1in", USB_DIR_IN | 1, 256);
2721 OMAP_ISO_EP("ep2out", USB_DIR_OUT | 2, 256);
2722 OMAP_ISO_EP("ep3in", USB_DIR_IN | 3, 128);
2723 OMAP_ISO_EP("ep4out", USB_DIR_OUT | 4, 128);
2725 OMAP_INT_EP("ep5in", USB_DIR_IN | 5, 16);
2727 OMAP_BULK_EP("ep6in", USB_DIR_IN | 6);
2728 OMAP_BULK_EP("ep7out", USB_DIR_OUT | 7);
2729 OMAP_INT_EP("ep8in", USB_DIR_IN | 8, 16);
2731 case 3: /* mixed bulk/iso */
2732 OMAP_BULK_EP("ep1in", USB_DIR_IN | 1);
2733 OMAP_BULK_EP("ep2out", USB_DIR_OUT | 2);
2734 OMAP_INT_EP("ep3in", USB_DIR_IN | 3, 16);
2736 OMAP_BULK_EP("ep4in", USB_DIR_IN | 4);
2737 OMAP_BULK_EP("ep5out", USB_DIR_OUT | 5);
2738 OMAP_INT_EP("ep6in", USB_DIR_IN | 6, 16);
2740 OMAP_ISO_EP("ep7in", USB_DIR_IN | 7, 256);
2741 OMAP_ISO_EP("ep8out", USB_DIR_OUT | 8, 256);
2742 OMAP_INT_EP("ep9in", USB_DIR_IN | 9, 16);
2746 /* add more modes as needed */
2749 ERR("unsupported fifo_mode #%d\n", fifo_mode);
2752 UDC_SYSCON1_REG = UDC_CFG_LOCK|UDC_SELF_PWR;
2753 INFO("fifo mode %d, %d bytes not used\n", fifo_mode, 2048 - buf);
2757 static int __init omap_udc_probe(struct platform_device *pdev)
2759 int status = -ENODEV;
2761 struct otg_transceiver *xceiv = NULL;
2762 const char *type = NULL;
2763 struct omap_usb_config *config = pdev->dev.platform_data;
2765 struct clk *hhc_clk;
2767 /* NOTE: "knows" the order of the resources! */
2768 if (!request_mem_region(pdev->resource[0].start,
2769 pdev->resource[0].end - pdev->resource[0].start + 1,
2771 DBG("request_mem_region failed\n");
2775 if (cpu_is_omap16xx()) {
2776 dc_clk = clk_get(&pdev->dev, "usb_dc_ck");
2777 hhc_clk = clk_get(&pdev->dev, "usb_hhc_ck");
2778 BUG_ON(IS_ERR(dc_clk) || IS_ERR(hhc_clk));
2779 /* can't use omap_udc_enable_clock yet */
2781 clk_enable(hhc_clk);
2785 INFO("OMAP UDC rev %d.%d%s\n",
2786 UDC_REV_REG >> 4, UDC_REV_REG & 0xf,
2787 config->otg ? ", Mini-AB" : "");
2789 /* use the mode given to us by board init code */
2790 if (cpu_is_omap15xx()) {
2794 if (machine_is_omap_innovator()) {
2795 /* just set up software VBUS detect, and then
2796 * later rig it so we always report VBUS.
2797 * FIXME without really sensing VBUS, we can't
2798 * know when to turn PULLUP_EN on/off; and that
2799 * means we always "need" the 48MHz clock.
2801 u32 tmp = FUNC_MUX_CTRL_0_REG;
2803 FUNC_MUX_CTRL_0_REG &= ~VBUS_CTRL_1510;
2804 tmp |= VBUS_MODE_1510;
2805 tmp &= ~VBUS_CTRL_1510;
2806 FUNC_MUX_CTRL_0_REG = tmp;
2809 /* The transceiver may package some GPIO logic or handle
2810 * loopback and/or transceiverless setup; if we find one,
2811 * use it. Except for OTG, we don't _need_ to talk to one;
2812 * but not having one probably means no VBUS detection.
2814 xceiv = otg_get_transceiver();
2816 type = xceiv->label;
2817 else if (config->otg) {
2818 DBG("OTG requires external transceiver!\n");
2824 case 0: /* POWERUP DEFAULT == 0 */
2828 if (!cpu_is_omap1710()) {
2829 type = "integrated";
2839 DBG("external transceiver not registered!\n");
2843 case 21: /* internal loopback */
2846 case 14: /* transceiverless */
2847 if (cpu_is_omap1710())
2857 ERR("unrecognized UDC HMC mode %d\n", hmc);
2861 INFO("hmc mode %d, %s transceiver\n", hmc, type);
2863 /* a "gadget" abstracts/virtualizes the controller */
2864 status = omap_udc_setup(pdev, xceiv);
2869 // "udc" is now valid
2870 pullup_disable(udc);
2871 #if defined(CONFIG_USB_OHCI_HCD) || defined(CONFIG_USB_OHCI_HCD_MODULE)
2872 udc->gadget.is_otg = (config->otg != 0);
2875 /* starting with omap1710 es2.0, clear toggle is a separate bit */
2876 if (UDC_REV_REG >= 0x61)
2877 udc->clr_halt = UDC_RESET_EP | UDC_CLRDATA_TOGGLE;
2879 udc->clr_halt = UDC_RESET_EP;
2881 /* USB general purpose IRQ: ep0, state changes, dma, etc */
2882 status = request_irq(pdev->resource[1].start, omap_udc_irq,
2883 SA_SAMPLE_RANDOM, driver_name, udc);
2885 ERR( "can't get irq %ld, err %d\n",
2886 pdev->resource[1].start, status);
2890 /* USB "non-iso" IRQ (PIO for all but ep0) */
2891 status = request_irq(pdev->resource[2].start, omap_udc_pio_irq,
2892 SA_SAMPLE_RANDOM, "omap_udc pio", udc);
2894 ERR( "can't get irq %ld, err %d\n",
2895 pdev->resource[2].start, status);
2899 status = request_irq(pdev->resource[3].start, omap_udc_iso_irq,
2900 SA_INTERRUPT, "omap_udc iso", udc);
2902 ERR("can't get irq %ld, err %d\n",
2903 pdev->resource[3].start, status);
2907 if (cpu_is_omap16xx()) {
2908 udc->dc_clk = dc_clk;
2909 udc->hhc_clk = hhc_clk;
2910 clk_disable(hhc_clk);
2911 clk_disable(dc_clk);
2915 device_add(&udc->gadget.dev);
2920 free_irq(pdev->resource[2].start, udc);
2924 free_irq(pdev->resource[1].start, udc);
2932 put_device(xceiv->dev);
2934 if (cpu_is_omap16xx()) {
2935 clk_disable(hhc_clk);
2936 clk_disable(dc_clk);
2941 release_mem_region(pdev->resource[0].start,
2942 pdev->resource[0].end - pdev->resource[0].start + 1);
2947 static int __exit omap_udc_remove(struct platform_device *pdev)
2949 DECLARE_COMPLETION(done);
2956 pullup_disable(udc);
2957 if (udc->transceiver) {
2958 put_device(udc->transceiver->dev);
2959 udc->transceiver = NULL;
2961 UDC_SYSCON1_REG = 0;
2966 free_irq(pdev->resource[3].start, udc);
2968 free_irq(pdev->resource[2].start, udc);
2969 free_irq(pdev->resource[1].start, udc);
2972 if (udc->clk_requested)
2973 omap_udc_enable_clock(0);
2974 clk_put(udc->hhc_clk);
2975 clk_put(udc->dc_clk);
2978 release_mem_region(pdev->resource[0].start,
2979 pdev->resource[0].end - pdev->resource[0].start + 1);
2981 device_unregister(&udc->gadget.dev);
2982 wait_for_completion(&done);
2987 /* suspend/resume/wakeup from sysfs (echo > power/state) or when the
2988 * system is forced into deep sleep
2990 * REVISIT we should probably reject suspend requests when there's a host
2991 * session active, rather than disconnecting, at least on boards that can
2992 * report VBUS irqs (UDC_DEVSTAT_REG.UDC_ATT). And in any case, we need to
2993 * make host resumes and VBUS detection trigger OMAP wakeup events; that
2994 * may involve talking to an external transceiver (e.g. isp1301).
2997 static int omap_udc_suspend(struct platform_device *dev, pm_message_t message)
3001 devstat = UDC_DEVSTAT_REG;
3003 /* we're requesting 48 MHz clock if the pullup is enabled
3004 * (== we're attached to the host) and we're not suspended,
3005 * which would prevent entry to deep sleep...
3007 if ((devstat & UDC_ATT) != 0 && (devstat & UDC_SUS) == 0) {
3008 WARN("session active; suspend requires disconnect\n");
3009 omap_pullup(&udc->gadget, 0);
3012 udc->gadget.dev.power.power_state = PMSG_SUSPEND;
3013 udc->gadget.dev.parent->power.power_state = PMSG_SUSPEND;
3017 static int omap_udc_resume(struct platform_device *dev)
3019 DBG("resume + wakeup/SRP\n");
3020 omap_pullup(&udc->gadget, 1);
3022 /* maybe the host would enumerate us if we nudged it */
3024 return omap_wakeup(&udc->gadget);
3027 /*-------------------------------------------------------------------------*/
3029 static struct platform_driver udc_driver = {
3030 .probe = omap_udc_probe,
3031 .remove = __exit_p(omap_udc_remove),
3032 .suspend = omap_udc_suspend,
3033 .resume = omap_udc_resume,
3035 .owner = THIS_MODULE,
3036 .name = (char *) driver_name,
3040 static int __init udc_init(void)
3042 INFO("%s, version: " DRIVER_VERSION
3046 "%s\n", driver_desc,
3047 use_dma ? " (dma)" : "");
3048 return platform_driver_register(&udc_driver);
3050 module_init(udc_init);
3052 static void __exit udc_exit(void)
3054 platform_driver_unregister(&udc_driver);
3056 module_exit(udc_exit);
3058 MODULE_DESCRIPTION(DRIVER_DESC);
3059 MODULE_LICENSE("GPL");