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/slab.h>
32 #include <linux/init.h>
33 #include <linux/timer.h>
34 #include <linux/list.h>
35 #include <linux/interrupt.h>
36 #include <linux/proc_fs.h>
38 #include <linux/moduleparam.h>
39 #include <linux/platform_device.h>
40 #include <linux/usb/ch9.h>
41 #include <linux/usb_gadget.h>
42 #include <linux/usb/otg.h>
43 #include <linux/dma-mapping.h>
44 #include <linux/clk.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 */
63 /* FIXME: OMAP2 currently has some problem in DMA mode */
64 #ifdef CONFIG_ARCH_OMAP2
71 #define DRIVER_DESC "OMAP UDC driver"
72 #define DRIVER_VERSION "4 October 2004"
74 #define DMA_ADDR_INVALID (~(dma_addr_t)0)
78 * The OMAP UDC needs _very_ early endpoint setup: before enabling the
79 * D+ pullup to allow enumeration. That's too early for the gadget
80 * framework to use from usb_endpoint_enable(), which happens after
81 * enumeration as part of activating an interface. (But if we add an
82 * optional new "UDC not yet running" state to the gadget driver model,
83 * even just during driver binding, the endpoint autoconfig logic is the
84 * natural spot to manufacture new endpoints.)
86 * So instead of using endpoint enable calls to control the hardware setup,
87 * this driver defines a "fifo mode" parameter. It's used during driver
88 * initialization to choose among a set of pre-defined endpoint configs.
89 * See omap_udc_setup() for available modes, or to add others. That code
90 * lives in an init section, so use this driver as a module if you need
91 * to change the fifo mode after the kernel boots.
93 * Gadget drivers normally ignore endpoints they don't care about, and
94 * won't include them in configuration descriptors. That means only
95 * misbehaving hosts would even notice they exist.
98 static unsigned fifo_mode = 3;
100 static unsigned fifo_mode = 0;
103 /* "modprobe omap_udc fifo_mode=42", or else as a kernel
104 * boot parameter "omap_udc:fifo_mode=42"
106 module_param (fifo_mode, uint, 0);
107 MODULE_PARM_DESC (fifo_mode, "endpoint configuration");
110 static unsigned use_dma = 1;
112 /* "modprobe omap_udc use_dma=y", or else as a kernel
113 * boot parameter "omap_udc:use_dma=y"
115 module_param (use_dma, bool, 0);
116 MODULE_PARM_DESC (use_dma, "enable/disable DMA");
119 /* save a bit of code */
121 #endif /* !USE_DMA */
124 static const char driver_name [] = "omap_udc";
125 static const char driver_desc [] = DRIVER_DESC;
127 /*-------------------------------------------------------------------------*/
129 /* there's a notion of "current endpoint" for modifying endpoint
130 * state, and PIO access to its FIFO.
133 static void use_ep(struct omap_ep *ep, u16 select)
135 u16 num = ep->bEndpointAddress & 0x0f;
137 if (ep->bEndpointAddress & USB_DIR_IN)
139 UDC_EP_NUM_REG = num | select;
140 /* when select, MUST deselect later !! */
143 static inline void deselect_ep(void)
145 UDC_EP_NUM_REG &= ~UDC_EP_SEL;
146 /* 6 wait states before TX will happen */
149 static void dma_channel_claim(struct omap_ep *ep, unsigned preferred);
151 /*-------------------------------------------------------------------------*/
153 static int omap_ep_enable(struct usb_ep *_ep,
154 const struct usb_endpoint_descriptor *desc)
156 struct omap_ep *ep = container_of(_ep, struct omap_ep, ep);
157 struct omap_udc *udc;
161 /* catch various bogus parameters */
162 if (!_ep || !desc || ep->desc
163 || desc->bDescriptorType != USB_DT_ENDPOINT
164 || ep->bEndpointAddress != desc->bEndpointAddress
165 || ep->maxpacket < le16_to_cpu
166 (desc->wMaxPacketSize)) {
167 DBG("%s, bad ep or descriptor\n", __FUNCTION__);
170 maxp = le16_to_cpu (desc->wMaxPacketSize);
171 if ((desc->bmAttributes == USB_ENDPOINT_XFER_BULK
172 && maxp != ep->maxpacket)
173 || le16_to_cpu(desc->wMaxPacketSize) > ep->maxpacket
174 || !desc->wMaxPacketSize) {
175 DBG("%s, bad %s maxpacket\n", __FUNCTION__, _ep->name);
180 if ((desc->bmAttributes == USB_ENDPOINT_XFER_ISOC
181 && desc->bInterval != 1)) {
182 /* hardware wants period = 1; USB allows 2^(Interval-1) */
183 DBG("%s, unsupported ISO period %dms\n", _ep->name,
184 1 << (desc->bInterval - 1));
188 if (desc->bmAttributes == USB_ENDPOINT_XFER_ISOC) {
189 DBG("%s, ISO nyet\n", _ep->name);
194 /* xfer types must match, except that interrupt ~= bulk */
195 if (ep->bmAttributes != desc->bmAttributes
196 && ep->bmAttributes != USB_ENDPOINT_XFER_BULK
197 && desc->bmAttributes != USB_ENDPOINT_XFER_INT) {
198 DBG("%s, %s type mismatch\n", __FUNCTION__, _ep->name);
203 if (!udc->driver || udc->gadget.speed == USB_SPEED_UNKNOWN) {
204 DBG("%s, bogus device state\n", __FUNCTION__);
208 spin_lock_irqsave(&udc->lock, flags);
213 ep->ep.maxpacket = maxp;
215 /* set endpoint to initial state */
219 use_ep(ep, UDC_EP_SEL);
220 UDC_CTRL_REG = udc->clr_halt;
224 if (ep->bmAttributes == USB_ENDPOINT_XFER_ISOC)
225 list_add(&ep->iso, &udc->iso);
227 /* maybe assign a DMA channel to this endpoint */
228 if (use_dma && desc->bmAttributes == USB_ENDPOINT_XFER_BULK)
229 /* FIXME ISO can dma, but prefers first channel */
230 dma_channel_claim(ep, 0);
232 /* PIO OUT may RX packets */
233 if (desc->bmAttributes != USB_ENDPOINT_XFER_ISOC
235 && !(ep->bEndpointAddress & USB_DIR_IN)) {
236 UDC_CTRL_REG = UDC_SET_FIFO_EN;
237 ep->ackwait = 1 + ep->double_buf;
240 spin_unlock_irqrestore(&udc->lock, flags);
241 VDBG("%s enabled\n", _ep->name);
245 static void nuke(struct omap_ep *, int status);
247 static int omap_ep_disable(struct usb_ep *_ep)
249 struct omap_ep *ep = container_of(_ep, struct omap_ep, ep);
252 if (!_ep || !ep->desc) {
253 DBG("%s, %s not enabled\n", __FUNCTION__,
254 _ep ? ep->ep.name : NULL);
258 spin_lock_irqsave(&ep->udc->lock, flags);
260 nuke (ep, -ESHUTDOWN);
261 ep->ep.maxpacket = ep->maxpacket;
263 UDC_CTRL_REG = UDC_SET_HALT;
264 list_del_init(&ep->iso);
265 del_timer(&ep->timer);
267 spin_unlock_irqrestore(&ep->udc->lock, flags);
269 VDBG("%s disabled\n", _ep->name);
273 /*-------------------------------------------------------------------------*/
275 static struct usb_request *
276 omap_alloc_request(struct usb_ep *ep, gfp_t gfp_flags)
278 struct omap_req *req;
280 req = kzalloc(sizeof(*req), gfp_flags);
282 req->req.dma = DMA_ADDR_INVALID;
283 INIT_LIST_HEAD (&req->queue);
289 omap_free_request(struct usb_ep *ep, struct usb_request *_req)
291 struct omap_req *req = container_of(_req, struct omap_req, req);
297 /*-------------------------------------------------------------------------*/
300 * dma-coherent memory allocation (for dma-capable endpoints)
302 * NOTE: the dma_*_coherent() API calls suck. Most implementations are
303 * (a) page-oriented, so small buffers lose big; and (b) asymmetric with
304 * respect to calls with irqs disabled: alloc is safe, free is not.
305 * We currently work around (b), but not (a).
322 ep = container_of(_ep, struct omap_ep, ep);
323 if (use_dma && ep->has_dma) {
325 if (!warned && bytes < PAGE_SIZE) {
326 dev_warn(ep->udc->gadget.dev.parent,
327 "using dma_alloc_coherent for "
328 "small allocations wastes memory\n");
331 return dma_alloc_coherent(ep->udc->gadget.dev.parent,
332 bytes, dma, gfp_flags);
335 retval = kmalloc(bytes, gfp_flags);
337 *dma = virt_to_phys(retval);
341 static DEFINE_SPINLOCK(buflock);
342 static LIST_HEAD(buffers);
345 struct list_head list;
351 static void do_free(unsigned long ignored)
353 spin_lock_irq(&buflock);
354 while (!list_empty(&buffers)) {
355 struct free_record *buf;
357 buf = list_entry(buffers.next, struct free_record, list);
358 list_del(&buf->list);
359 spin_unlock_irq(&buflock);
361 dma_free_coherent(buf->dev, buf->bytes, buf, buf->dma);
363 spin_lock_irq(&buflock);
365 spin_unlock_irq(&buflock);
368 static DECLARE_TASKLET(deferred_free, do_free, 0);
370 static void omap_free_buffer(
382 /* free memory into the right allocator */
383 if (dma != DMA_ADDR_INVALID) {
385 struct free_record *rec = buf;
388 ep = container_of(_ep, struct omap_ep, ep);
390 rec->dev = ep->udc->gadget.dev.parent;
394 spin_lock_irqsave(&buflock, flags);
395 list_add_tail(&rec->list, &buffers);
396 tasklet_schedule(&deferred_free);
397 spin_unlock_irqrestore(&buflock, flags);
402 /*-------------------------------------------------------------------------*/
405 done(struct omap_ep *ep, struct omap_req *req, int status)
407 unsigned stopped = ep->stopped;
409 list_del_init(&req->queue);
411 if (req->req.status == -EINPROGRESS)
412 req->req.status = status;
414 status = req->req.status;
416 if (use_dma && ep->has_dma) {
418 dma_unmap_single(ep->udc->gadget.dev.parent,
419 req->req.dma, req->req.length,
420 (ep->bEndpointAddress & USB_DIR_IN)
423 req->req.dma = DMA_ADDR_INVALID;
426 dma_sync_single_for_cpu(ep->udc->gadget.dev.parent,
427 req->req.dma, req->req.length,
428 (ep->bEndpointAddress & USB_DIR_IN)
434 if (status && status != -ESHUTDOWN)
436 VDBG("complete %s req %p stat %d len %u/%u\n",
437 ep->ep.name, &req->req, status,
438 req->req.actual, req->req.length);
440 /* don't modify queue heads during completion callback */
442 spin_unlock(&ep->udc->lock);
443 req->req.complete(&ep->ep, &req->req);
444 spin_lock(&ep->udc->lock);
445 ep->stopped = stopped;
448 /*-------------------------------------------------------------------------*/
450 #define UDC_FIFO_FULL (UDC_NON_ISO_FIFO_FULL | UDC_ISO_FIFO_FULL)
451 #define UDC_FIFO_UNWRITABLE (UDC_EP_HALTED | UDC_FIFO_FULL)
453 #define FIFO_EMPTY (UDC_NON_ISO_FIFO_EMPTY | UDC_ISO_FIFO_EMPTY)
454 #define FIFO_UNREADABLE (UDC_EP_HALTED | FIFO_EMPTY)
457 write_packet(u8 *buf, struct omap_req *req, unsigned max)
462 len = min(req->req.length - req->req.actual, max);
463 req->req.actual += len;
466 if (likely((((int)buf) & 1) == 0)) {
469 UDC_DATA_REG = *wp++;
475 *(volatile u8 *)&UDC_DATA_REG = *buf++;
479 // FIXME change r/w fifo calling convention
482 // return: 0 = still running, 1 = completed, negative = errno
483 static int write_fifo(struct omap_ep *ep, struct omap_req *req)
490 buf = req->req.buf + req->req.actual;
493 /* PIO-IN isn't double buffered except for iso */
494 ep_stat = UDC_STAT_FLG_REG;
495 if (ep_stat & UDC_FIFO_UNWRITABLE)
498 count = ep->ep.maxpacket;
499 count = write_packet(buf, req, count);
500 UDC_CTRL_REG = UDC_SET_FIFO_EN;
503 /* last packet is often short (sometimes a zlp) */
504 if (count != ep->ep.maxpacket)
506 else if (req->req.length == req->req.actual
512 /* NOTE: requests complete when all IN data is in a
513 * FIFO (or sometimes later, if a zlp was needed).
514 * Use usb_ep_fifo_status() where needed.
522 read_packet(u8 *buf, struct omap_req *req, unsigned avail)
527 len = min(req->req.length - req->req.actual, avail);
528 req->req.actual += len;
531 if (likely((((int)buf) & 1) == 0)) {
534 *wp++ = UDC_DATA_REG;
540 *buf++ = *(volatile u8 *)&UDC_DATA_REG;
544 // return: 0 = still running, 1 = queue empty, negative = errno
545 static int read_fifo(struct omap_ep *ep, struct omap_req *req)
548 unsigned count, avail;
551 buf = req->req.buf + req->req.actual;
555 u16 ep_stat = UDC_STAT_FLG_REG;
558 if (ep_stat & FIFO_EMPTY) {
563 if (ep_stat & UDC_EP_HALTED)
566 if (ep_stat & UDC_FIFO_FULL)
567 avail = ep->ep.maxpacket;
569 avail = UDC_RXFSTAT_REG;
570 ep->fnf = ep->double_buf;
572 count = read_packet(buf, req, avail);
574 /* partial packet reads may not be errors */
575 if (count < ep->ep.maxpacket) {
577 /* overflowed this request? flush extra data */
578 if (count != avail) {
579 req->req.status = -EOVERFLOW;
582 (void) *(volatile u8 *)&UDC_DATA_REG;
584 } else if (req->req.length == req->req.actual)
589 if (!ep->bEndpointAddress)
598 /*-------------------------------------------------------------------------*/
600 static inline dma_addr_t dma_csac(unsigned lch)
604 /* omap 3.2/3.3 erratum: sometimes 0 is returned if CSAC/CDAC is
605 * read before the DMA controller finished disabling the channel.
607 csac = OMAP_DMA_CSAC_REG(lch);
609 csac = OMAP_DMA_CSAC_REG(lch);
613 static inline dma_addr_t dma_cdac(unsigned lch)
617 /* omap 3.2/3.3 erratum: sometimes 0 is returned if CSAC/CDAC is
618 * read before the DMA controller finished disabling the channel.
620 cdac = OMAP_DMA_CDAC_REG(lch);
622 cdac = OMAP_DMA_CDAC_REG(lch);
626 static u16 dma_src_len(struct omap_ep *ep, dma_addr_t start)
630 /* IN-DMA needs this on fault/cancel paths, so 15xx misreports
631 * the last transfer's bytecount by more than a FIFO's worth.
633 if (cpu_is_omap15xx())
636 end = dma_csac(ep->lch);
637 if (end == ep->dma_counter)
640 end |= start & (0xffff << 16);
646 #define DMA_DEST_LAST(x) (cpu_is_omap15xx() \
647 ? OMAP_DMA_CSAC_REG(x) /* really: CPC */ \
650 static u16 dma_dest_len(struct omap_ep *ep, dma_addr_t start)
654 end = DMA_DEST_LAST(ep->lch);
655 if (end == ep->dma_counter)
658 end |= start & (0xffff << 16);
659 if (cpu_is_omap15xx())
667 /* Each USB transfer request using DMA maps to one or more DMA transfers.
668 * When DMA completion isn't request completion, the UDC continues with
669 * the next DMA transfer for that USB transfer.
672 static void next_in_dma(struct omap_ep *ep, struct omap_req *req)
675 unsigned length = req->req.length - req->req.actual;
676 const int sync_mode = cpu_is_omap15xx()
677 ? OMAP_DMA_SYNC_FRAME
678 : OMAP_DMA_SYNC_ELEMENT;
680 /* measure length in either bytes or packets */
681 if ((cpu_is_omap16xx() && length <= UDC_TXN_TSC)
682 || (cpu_is_omap15xx() && length < ep->maxpacket)) {
683 txdma_ctrl = UDC_TXN_EOT | length;
684 omap_set_dma_transfer_params(ep->lch, OMAP_DMA_DATA_TYPE_S8,
685 length, 1, sync_mode, 0, 0);
687 length = min(length / ep->maxpacket,
688 (unsigned) UDC_TXN_TSC + 1);
690 omap_set_dma_transfer_params(ep->lch, OMAP_DMA_DATA_TYPE_S16,
691 ep->ep.maxpacket >> 1, length, sync_mode,
693 length *= ep->maxpacket;
695 omap_set_dma_src_params(ep->lch, OMAP_DMA_PORT_EMIFF,
696 OMAP_DMA_AMODE_POST_INC, req->req.dma + req->req.actual,
699 omap_start_dma(ep->lch);
700 ep->dma_counter = dma_csac(ep->lch);
701 UDC_DMA_IRQ_EN_REG |= UDC_TX_DONE_IE(ep->dma_channel);
702 UDC_TXDMA_REG(ep->dma_channel) = UDC_TXN_START | txdma_ctrl;
703 req->dma_bytes = length;
706 static void finish_in_dma(struct omap_ep *ep, struct omap_req *req, int status)
709 req->req.actual += req->dma_bytes;
711 /* return if this request needs to send data or zlp */
712 if (req->req.actual < req->req.length)
715 && req->dma_bytes != 0
716 && (req->req.actual % ep->maxpacket) == 0)
719 req->req.actual += dma_src_len(ep, req->req.dma
723 omap_stop_dma(ep->lch);
724 UDC_DMA_IRQ_EN_REG &= ~UDC_TX_DONE_IE(ep->dma_channel);
725 done(ep, req, status);
728 static void next_out_dma(struct omap_ep *ep, struct omap_req *req)
732 /* NOTE: we filtered out "short reads" before, so we know
733 * the buffer has only whole numbers of packets.
736 /* set up this DMA transfer, enable the fifo, start */
737 packets = (req->req.length - req->req.actual) / ep->ep.maxpacket;
738 packets = min(packets, (unsigned)UDC_RXN_TC + 1);
739 req->dma_bytes = packets * ep->ep.maxpacket;
740 omap_set_dma_transfer_params(ep->lch, OMAP_DMA_DATA_TYPE_S16,
741 ep->ep.maxpacket >> 1, packets,
742 OMAP_DMA_SYNC_ELEMENT,
744 omap_set_dma_dest_params(ep->lch, OMAP_DMA_PORT_EMIFF,
745 OMAP_DMA_AMODE_POST_INC, req->req.dma + req->req.actual,
747 ep->dma_counter = DMA_DEST_LAST(ep->lch);
749 UDC_RXDMA_REG(ep->dma_channel) = UDC_RXN_STOP | (packets - 1);
750 UDC_DMA_IRQ_EN_REG |= UDC_RX_EOT_IE(ep->dma_channel);
751 UDC_EP_NUM_REG = (ep->bEndpointAddress & 0xf);
752 UDC_CTRL_REG = UDC_SET_FIFO_EN;
754 omap_start_dma(ep->lch);
758 finish_out_dma(struct omap_ep *ep, struct omap_req *req, int status, int one)
763 ep->dma_counter = (u16) (req->req.dma + req->req.actual);
764 count = dma_dest_len(ep, req->req.dma + req->req.actual);
765 count += req->req.actual;
768 if (count <= req->req.length)
769 req->req.actual = count;
771 if (count != req->dma_bytes || status)
772 omap_stop_dma(ep->lch);
774 /* if this wasn't short, request may need another transfer */
775 else if (req->req.actual < req->req.length)
779 UDC_DMA_IRQ_EN_REG &= ~UDC_RX_EOT_IE(ep->dma_channel);
780 done(ep, req, status);
783 static void dma_irq(struct omap_udc *udc, u16 irq_src)
785 u16 dman_stat = UDC_DMAN_STAT_REG;
787 struct omap_req *req;
789 /* IN dma: tx to host */
790 if (irq_src & UDC_TXN_DONE) {
791 ep = &udc->ep[16 + UDC_DMA_TX_SRC(dman_stat)];
793 /* can see TXN_DONE after dma abort */
794 if (!list_empty(&ep->queue)) {
795 req = container_of(ep->queue.next,
796 struct omap_req, queue);
797 finish_in_dma(ep, req, 0);
799 UDC_IRQ_SRC_REG = UDC_TXN_DONE;
801 if (!list_empty (&ep->queue)) {
802 req = container_of(ep->queue.next,
803 struct omap_req, queue);
804 next_in_dma(ep, req);
808 /* OUT dma: rx from host */
809 if (irq_src & UDC_RXN_EOT) {
810 ep = &udc->ep[UDC_DMA_RX_SRC(dman_stat)];
812 /* can see RXN_EOT after dma abort */
813 if (!list_empty(&ep->queue)) {
814 req = container_of(ep->queue.next,
815 struct omap_req, queue);
816 finish_out_dma(ep, req, 0, dman_stat & UDC_DMA_RX_SB);
818 UDC_IRQ_SRC_REG = UDC_RXN_EOT;
820 if (!list_empty (&ep->queue)) {
821 req = container_of(ep->queue.next,
822 struct omap_req, queue);
823 next_out_dma(ep, req);
827 if (irq_src & UDC_RXN_CNT) {
828 ep = &udc->ep[UDC_DMA_RX_SRC(dman_stat)];
830 /* omap15xx does this unasked... */
831 VDBG("%s, RX_CNT irq?\n", ep->ep.name);
832 UDC_IRQ_SRC_REG = UDC_RXN_CNT;
836 static void dma_error(int lch, u16 ch_status, void *data)
838 struct omap_ep *ep = data;
840 /* if ch_status & OMAP_DMA_DROP_IRQ ... */
841 /* if ch_status & OMAP1_DMA_TOUT_IRQ ... */
842 ERR("%s dma error, lch %d status %02x\n", ep->ep.name, lch, ch_status);
844 /* complete current transfer ... */
847 static void dma_channel_claim(struct omap_ep *ep, unsigned channel)
850 int status, restart, is_in;
852 is_in = ep->bEndpointAddress & USB_DIR_IN;
854 reg = UDC_TXDMA_CFG_REG;
856 reg = UDC_RXDMA_CFG_REG;
857 reg |= UDC_DMA_REQ; /* "pulse" activated */
861 if (channel == 0 || channel > 3) {
862 if ((reg & 0x0f00) == 0)
864 else if ((reg & 0x00f0) == 0)
866 else if ((reg & 0x000f) == 0) /* preferred for ISO */
873 reg |= (0x0f & ep->bEndpointAddress) << (4 * (channel - 1));
874 ep->dma_channel = channel;
877 status = omap_request_dma(OMAP_DMA_USB_W2FC_TX0 - 1 + channel,
878 ep->ep.name, dma_error, ep, &ep->lch);
880 UDC_TXDMA_CFG_REG = reg;
882 omap_set_dma_src_burst_mode(ep->lch,
883 OMAP_DMA_DATA_BURST_4);
884 omap_set_dma_src_data_pack(ep->lch, 1);
886 omap_set_dma_dest_params(ep->lch,
888 OMAP_DMA_AMODE_CONSTANT,
889 (unsigned long) io_v2p((u32)&UDC_DATA_DMA_REG),
893 status = omap_request_dma(OMAP_DMA_USB_W2FC_RX0 - 1 + channel,
894 ep->ep.name, dma_error, ep, &ep->lch);
896 UDC_RXDMA_CFG_REG = reg;
898 omap_set_dma_src_params(ep->lch,
900 OMAP_DMA_AMODE_CONSTANT,
901 (unsigned long) io_v2p((u32)&UDC_DATA_DMA_REG),
904 omap_set_dma_dest_burst_mode(ep->lch,
905 OMAP_DMA_DATA_BURST_4);
906 omap_set_dma_dest_data_pack(ep->lch, 1);
913 omap_disable_dma_irq(ep->lch, OMAP_DMA_BLOCK_IRQ);
915 /* channel type P: hw synch (fifo) */
916 if (!cpu_is_omap15xx())
917 OMAP1_DMA_LCH_CTRL_REG(ep->lch) = 2;
921 /* restart any queue, even if the claim failed */
922 restart = !ep->stopped && !list_empty(&ep->queue);
925 DBG("%s no dma channel: %d%s\n", ep->ep.name, status,
926 restart ? " (restart)" : "");
928 DBG("%s claimed %cxdma%d lch %d%s\n", ep->ep.name,
930 ep->dma_channel - 1, ep->lch,
931 restart ? " (restart)" : "");
934 struct omap_req *req;
935 req = container_of(ep->queue.next, struct omap_req, queue);
937 (is_in ? next_in_dma : next_out_dma)(ep, req);
939 use_ep(ep, UDC_EP_SEL);
940 (is_in ? write_fifo : read_fifo)(ep, req);
943 UDC_CTRL_REG = UDC_SET_FIFO_EN;
944 ep->ackwait = 1 + ep->double_buf;
946 /* IN: 6 wait states before it'll tx */
951 static void dma_channel_release(struct omap_ep *ep)
953 int shift = 4 * (ep->dma_channel - 1);
954 u16 mask = 0x0f << shift;
955 struct omap_req *req;
958 /* abort any active usb transfer request */
959 if (!list_empty(&ep->queue))
960 req = container_of(ep->queue.next, struct omap_req, queue);
964 active = ((1 << 7) & OMAP_DMA_CCR_REG(ep->lch)) != 0;
966 DBG("%s release %s %cxdma%d %p\n", ep->ep.name,
967 active ? "active" : "idle",
968 (ep->bEndpointAddress & USB_DIR_IN) ? 't' : 'r',
969 ep->dma_channel - 1, req);
971 /* NOTE: re-setting RX_REQ/TX_REQ because of a chip bug (before
972 * OMAP 1710 ES2.0) where reading the DMA_CFG can clear them.
975 /* wait till current packet DMA finishes, and fifo empties */
976 if (ep->bEndpointAddress & USB_DIR_IN) {
977 UDC_TXDMA_CFG_REG = (UDC_TXDMA_CFG_REG & ~mask) | UDC_DMA_REQ;
980 finish_in_dma(ep, req, -ECONNRESET);
982 /* clear FIFO; hosts probably won't empty it */
983 use_ep(ep, UDC_EP_SEL);
984 UDC_CTRL_REG = UDC_CLR_EP;
987 while (UDC_TXDMA_CFG_REG & mask)
990 UDC_RXDMA_CFG_REG = (UDC_RXDMA_CFG_REG & ~mask) | UDC_DMA_REQ;
992 /* dma empties the fifo */
993 while (UDC_RXDMA_CFG_REG & mask)
996 finish_out_dma(ep, req, -ECONNRESET, 0);
998 omap_free_dma(ep->lch);
1001 /* has_dma still set, till endpoint is fully quiesced */
1005 /*-------------------------------------------------------------------------*/
1008 omap_ep_queue(struct usb_ep *_ep, struct usb_request *_req, gfp_t gfp_flags)
1010 struct omap_ep *ep = container_of(_ep, struct omap_ep, ep);
1011 struct omap_req *req = container_of(_req, struct omap_req, req);
1012 struct omap_udc *udc;
1013 unsigned long flags;
1016 /* catch various bogus parameters */
1017 if (!_req || !req->req.complete || !req->req.buf
1018 || !list_empty(&req->queue)) {
1019 DBG("%s, bad params\n", __FUNCTION__);
1022 if (!_ep || (!ep->desc && ep->bEndpointAddress)) {
1023 DBG("%s, bad ep\n", __FUNCTION__);
1026 if (ep->bmAttributes == USB_ENDPOINT_XFER_ISOC) {
1027 if (req->req.length > ep->ep.maxpacket)
1032 /* this isn't bogus, but OMAP DMA isn't the only hardware to
1033 * have a hard time with partial packet reads... reject it.
1037 && ep->bEndpointAddress != 0
1038 && (ep->bEndpointAddress & USB_DIR_IN) == 0
1039 && (req->req.length % ep->ep.maxpacket) != 0) {
1040 DBG("%s, no partial packet OUT reads\n", __FUNCTION__);
1045 if (!udc->driver || udc->gadget.speed == USB_SPEED_UNKNOWN)
1048 if (use_dma && ep->has_dma) {
1049 if (req->req.dma == DMA_ADDR_INVALID) {
1050 req->req.dma = dma_map_single(
1051 ep->udc->gadget.dev.parent,
1054 (ep->bEndpointAddress & USB_DIR_IN)
1059 dma_sync_single_for_device(
1060 ep->udc->gadget.dev.parent,
1061 req->req.dma, req->req.length,
1062 (ep->bEndpointAddress & USB_DIR_IN)
1069 VDBG("%s queue req %p, len %d buf %p\n",
1070 ep->ep.name, _req, _req->length, _req->buf);
1072 spin_lock_irqsave(&udc->lock, flags);
1074 req->req.status = -EINPROGRESS;
1075 req->req.actual = 0;
1077 /* maybe kickstart non-iso i/o queues */
1079 UDC_IRQ_EN_REG |= UDC_SOF_IE;
1080 else if (list_empty(&ep->queue) && !ep->stopped && !ep->ackwait) {
1083 if (ep->bEndpointAddress == 0) {
1084 if (!udc->ep0_pending || !list_empty (&ep->queue)) {
1085 spin_unlock_irqrestore(&udc->lock, flags);
1089 /* empty DATA stage? */
1090 is_in = udc->ep0_in;
1091 if (!req->req.length) {
1093 /* chip became CONFIGURED or ADDRESSED
1094 * earlier; drivers may already have queued
1095 * requests to non-control endpoints
1097 if (udc->ep0_set_config) {
1098 u16 irq_en = UDC_IRQ_EN_REG;
1100 irq_en |= UDC_DS_CHG_IE | UDC_EP0_IE;
1101 if (!udc->ep0_reset_config)
1102 irq_en |= UDC_EPN_RX_IE
1104 UDC_IRQ_EN_REG = irq_en;
1107 /* STATUS for zero length DATA stages is
1108 * always an IN ... even for IN transfers,
1109 * a wierd case which seem to stall OMAP.
1111 UDC_EP_NUM_REG = (UDC_EP_SEL|UDC_EP_DIR);
1112 UDC_CTRL_REG = UDC_CLR_EP;
1113 UDC_CTRL_REG = UDC_SET_FIFO_EN;
1114 UDC_EP_NUM_REG = UDC_EP_DIR;
1117 udc->ep0_pending = 0;
1121 /* non-empty DATA stage */
1123 UDC_EP_NUM_REG = UDC_EP_SEL|UDC_EP_DIR;
1127 UDC_EP_NUM_REG = UDC_EP_SEL;
1130 is_in = ep->bEndpointAddress & USB_DIR_IN;
1132 use_ep(ep, UDC_EP_SEL);
1133 /* if ISO: SOF IRQs must be enabled/disabled! */
1137 (is_in ? next_in_dma : next_out_dma)(ep, req);
1139 if ((is_in ? write_fifo : read_fifo)(ep, req) == 1)
1143 UDC_CTRL_REG = UDC_SET_FIFO_EN;
1144 ep->ackwait = 1 + ep->double_buf;
1146 /* IN: 6 wait states before it'll tx */
1151 /* irq handler advances the queue */
1153 list_add_tail(&req->queue, &ep->queue);
1154 spin_unlock_irqrestore(&udc->lock, flags);
1159 static int omap_ep_dequeue(struct usb_ep *_ep, struct usb_request *_req)
1161 struct omap_ep *ep = container_of(_ep, struct omap_ep, ep);
1162 struct omap_req *req;
1163 unsigned long flags;
1168 spin_lock_irqsave(&ep->udc->lock, flags);
1170 /* make sure it's actually queued on this endpoint */
1171 list_for_each_entry (req, &ep->queue, queue) {
1172 if (&req->req == _req)
1175 if (&req->req != _req) {
1176 spin_unlock_irqrestore(&ep->udc->lock, flags);
1180 if (use_dma && ep->dma_channel && ep->queue.next == &req->queue) {
1181 int channel = ep->dma_channel;
1183 /* releasing the channel cancels the request,
1184 * reclaiming the channel restarts the queue
1186 dma_channel_release(ep);
1187 dma_channel_claim(ep, channel);
1189 done(ep, req, -ECONNRESET);
1190 spin_unlock_irqrestore(&ep->udc->lock, flags);
1194 /*-------------------------------------------------------------------------*/
1196 static int omap_ep_set_halt(struct usb_ep *_ep, int value)
1198 struct omap_ep *ep = container_of(_ep, struct omap_ep, ep);
1199 unsigned long flags;
1200 int status = -EOPNOTSUPP;
1202 spin_lock_irqsave(&ep->udc->lock, flags);
1204 /* just use protocol stalls for ep0; real halts are annoying */
1205 if (ep->bEndpointAddress == 0) {
1206 if (!ep->udc->ep0_pending)
1209 if (ep->udc->ep0_set_config) {
1210 WARN("error changing config?\n");
1211 UDC_SYSCON2_REG = UDC_CLR_CFG;
1213 UDC_SYSCON2_REG = UDC_STALL_CMD;
1214 ep->udc->ep0_pending = 0;
1219 /* otherwise, all active non-ISO endpoints can halt */
1220 } else if (ep->bmAttributes != USB_ENDPOINT_XFER_ISOC && ep->desc) {
1222 /* IN endpoints must already be idle */
1223 if ((ep->bEndpointAddress & USB_DIR_IN)
1224 && !list_empty(&ep->queue)) {
1232 if (use_dma && ep->dma_channel
1233 && !list_empty(&ep->queue)) {
1234 channel = ep->dma_channel;
1235 dma_channel_release(ep);
1239 use_ep(ep, UDC_EP_SEL);
1240 if (UDC_STAT_FLG_REG & UDC_NON_ISO_FIFO_EMPTY) {
1241 UDC_CTRL_REG = UDC_SET_HALT;
1248 dma_channel_claim(ep, channel);
1251 UDC_CTRL_REG = ep->udc->clr_halt;
1253 if (!(ep->bEndpointAddress & USB_DIR_IN)) {
1254 UDC_CTRL_REG = UDC_SET_FIFO_EN;
1255 ep->ackwait = 1 + ep->double_buf;
1260 VDBG("%s %s halt stat %d\n", ep->ep.name,
1261 value ? "set" : "clear", status);
1263 spin_unlock_irqrestore(&ep->udc->lock, flags);
1267 static struct usb_ep_ops omap_ep_ops = {
1268 .enable = omap_ep_enable,
1269 .disable = omap_ep_disable,
1271 .alloc_request = omap_alloc_request,
1272 .free_request = omap_free_request,
1274 .alloc_buffer = omap_alloc_buffer,
1275 .free_buffer = omap_free_buffer,
1277 .queue = omap_ep_queue,
1278 .dequeue = omap_ep_dequeue,
1280 .set_halt = omap_ep_set_halt,
1281 // fifo_status ... report bytes in fifo
1282 // fifo_flush ... flush fifo
1285 /*-------------------------------------------------------------------------*/
1287 static int omap_get_frame(struct usb_gadget *gadget)
1289 u16 sof = UDC_SOF_REG;
1290 return (sof & UDC_TS_OK) ? (sof & UDC_TS) : -EL2NSYNC;
1293 static int omap_wakeup(struct usb_gadget *gadget)
1295 struct omap_udc *udc;
1296 unsigned long flags;
1297 int retval = -EHOSTUNREACH;
1299 udc = container_of(gadget, struct omap_udc, gadget);
1301 spin_lock_irqsave(&udc->lock, flags);
1302 if (udc->devstat & UDC_SUS) {
1303 /* NOTE: OTG spec erratum says that OTG devices may
1304 * issue wakeups without host enable.
1306 if (udc->devstat & (UDC_B_HNP_ENABLE|UDC_R_WK_OK)) {
1307 DBG("remote wakeup...\n");
1308 UDC_SYSCON2_REG = UDC_RMT_WKP;
1312 /* NOTE: non-OTG systems may use SRP TOO... */
1313 } else if (!(udc->devstat & UDC_ATT)) {
1314 if (udc->transceiver)
1315 retval = otg_start_srp(udc->transceiver);
1317 spin_unlock_irqrestore(&udc->lock, flags);
1323 omap_set_selfpowered(struct usb_gadget *gadget, int is_selfpowered)
1325 struct omap_udc *udc;
1326 unsigned long flags;
1329 udc = container_of(gadget, struct omap_udc, gadget);
1330 spin_lock_irqsave(&udc->lock, flags);
1331 syscon1 = UDC_SYSCON1_REG;
1333 syscon1 |= UDC_SELF_PWR;
1335 syscon1 &= ~UDC_SELF_PWR;
1336 UDC_SYSCON1_REG = syscon1;
1337 spin_unlock_irqrestore(&udc->lock, flags);
1342 static int can_pullup(struct omap_udc *udc)
1344 return udc->driver && udc->softconnect && udc->vbus_active;
1347 static void pullup_enable(struct omap_udc *udc)
1349 udc->gadget.dev.parent->power.power_state = PMSG_ON;
1350 udc->gadget.dev.power.power_state = PMSG_ON;
1351 UDC_SYSCON1_REG |= UDC_PULLUP_EN;
1352 #ifndef CONFIG_USB_OTG
1353 if (!cpu_is_omap15xx())
1354 OTG_CTRL_REG |= OTG_BSESSVLD;
1356 UDC_IRQ_EN_REG = UDC_DS_CHG_IE;
1359 static void pullup_disable(struct omap_udc *udc)
1361 #ifndef CONFIG_USB_OTG
1362 if (!cpu_is_omap15xx())
1363 OTG_CTRL_REG &= ~OTG_BSESSVLD;
1365 UDC_IRQ_EN_REG = UDC_DS_CHG_IE;
1366 UDC_SYSCON1_REG &= ~UDC_PULLUP_EN;
1369 static struct omap_udc *udc;
1371 static void omap_udc_enable_clock(int enable)
1373 if (udc == NULL || udc->dc_clk == NULL || udc->hhc_clk == NULL)
1377 clk_enable(udc->dc_clk);
1378 clk_enable(udc->hhc_clk);
1381 clk_disable(udc->hhc_clk);
1382 clk_disable(udc->dc_clk);
1387 * Called by whatever detects VBUS sessions: external transceiver
1388 * driver, or maybe GPIO0 VBUS IRQ. May request 48 MHz clock.
1390 static int omap_vbus_session(struct usb_gadget *gadget, int is_active)
1392 struct omap_udc *udc;
1393 unsigned long flags;
1395 udc = container_of(gadget, struct omap_udc, gadget);
1396 spin_lock_irqsave(&udc->lock, flags);
1397 VDBG("VBUS %s\n", is_active ? "on" : "off");
1398 udc->vbus_active = (is_active != 0);
1399 if (cpu_is_omap15xx()) {
1400 /* "software" detect, ignored if !VBUS_MODE_1510 */
1402 FUNC_MUX_CTRL_0_REG |= VBUS_CTRL_1510;
1404 FUNC_MUX_CTRL_0_REG &= ~VBUS_CTRL_1510;
1406 if (udc->dc_clk != NULL && is_active) {
1407 if (!udc->clk_requested) {
1408 omap_udc_enable_clock(1);
1409 udc->clk_requested = 1;
1412 if (can_pullup(udc))
1415 pullup_disable(udc);
1416 if (udc->dc_clk != NULL && !is_active) {
1417 if (udc->clk_requested) {
1418 omap_udc_enable_clock(0);
1419 udc->clk_requested = 0;
1422 spin_unlock_irqrestore(&udc->lock, flags);
1426 static int omap_vbus_draw(struct usb_gadget *gadget, unsigned mA)
1428 struct omap_udc *udc;
1430 udc = container_of(gadget, struct omap_udc, gadget);
1431 if (udc->transceiver)
1432 return otg_set_power(udc->transceiver, mA);
1436 static int omap_pullup(struct usb_gadget *gadget, int is_on)
1438 struct omap_udc *udc;
1439 unsigned long flags;
1441 udc = container_of(gadget, struct omap_udc, gadget);
1442 spin_lock_irqsave(&udc->lock, flags);
1443 udc->softconnect = (is_on != 0);
1444 if (can_pullup(udc))
1447 pullup_disable(udc);
1448 spin_unlock_irqrestore(&udc->lock, flags);
1452 static struct usb_gadget_ops omap_gadget_ops = {
1453 .get_frame = omap_get_frame,
1454 .wakeup = omap_wakeup,
1455 .set_selfpowered = omap_set_selfpowered,
1456 .vbus_session = omap_vbus_session,
1457 .vbus_draw = omap_vbus_draw,
1458 .pullup = omap_pullup,
1461 /*-------------------------------------------------------------------------*/
1463 /* dequeue ALL requests; caller holds udc->lock */
1464 static void nuke(struct omap_ep *ep, int status)
1466 struct omap_req *req;
1470 if (use_dma && ep->dma_channel)
1471 dma_channel_release(ep);
1474 UDC_CTRL_REG = UDC_CLR_EP;
1475 if (ep->bEndpointAddress && ep->bmAttributes != USB_ENDPOINT_XFER_ISOC)
1476 UDC_CTRL_REG = UDC_SET_HALT;
1478 while (!list_empty(&ep->queue)) {
1479 req = list_entry(ep->queue.next, struct omap_req, queue);
1480 done(ep, req, status);
1484 /* caller holds udc->lock */
1485 static void udc_quiesce(struct omap_udc *udc)
1489 udc->gadget.speed = USB_SPEED_UNKNOWN;
1490 nuke(&udc->ep[0], -ESHUTDOWN);
1491 list_for_each_entry (ep, &udc->gadget.ep_list, ep.ep_list)
1492 nuke(ep, -ESHUTDOWN);
1495 /*-------------------------------------------------------------------------*/
1497 static void update_otg(struct omap_udc *udc)
1501 if (!udc->gadget.is_otg)
1504 if (OTG_CTRL_REG & OTG_ID)
1505 devstat = UDC_DEVSTAT_REG;
1509 udc->gadget.b_hnp_enable = !!(devstat & UDC_B_HNP_ENABLE);
1510 udc->gadget.a_hnp_support = !!(devstat & UDC_A_HNP_SUPPORT);
1511 udc->gadget.a_alt_hnp_support = !!(devstat & UDC_A_ALT_HNP_SUPPORT);
1513 /* Enable HNP early, avoiding races on suspend irq path.
1514 * ASSUMES OTG state machine B_BUS_REQ input is true.
1516 if (udc->gadget.b_hnp_enable)
1517 OTG_CTRL_REG = (OTG_CTRL_REG | OTG_B_HNPEN | OTG_B_BUSREQ)
1521 static void ep0_irq(struct omap_udc *udc, u16 irq_src)
1523 struct omap_ep *ep0 = &udc->ep[0];
1524 struct omap_req *req = NULL;
1528 /* Clear any pending requests and then scrub any rx/tx state
1529 * before starting to handle the SETUP request.
1531 if (irq_src & UDC_SETUP) {
1532 u16 ack = irq_src & (UDC_EP0_TX|UDC_EP0_RX);
1536 UDC_IRQ_SRC_REG = ack;
1537 irq_src = UDC_SETUP;
1541 /* IN/OUT packets mean we're in the DATA or STATUS stage.
1542 * This driver uses only uses protocol stalls (ep0 never halts),
1543 * and if we got this far the gadget driver already had a
1544 * chance to stall. Tries to be forgiving of host oddities.
1546 * NOTE: the last chance gadget drivers have to stall control
1547 * requests is during their request completion callback.
1549 if (!list_empty(&ep0->queue))
1550 req = container_of(ep0->queue.next, struct omap_req, queue);
1552 /* IN == TX to host */
1553 if (irq_src & UDC_EP0_TX) {
1556 UDC_IRQ_SRC_REG = UDC_EP0_TX;
1557 UDC_EP_NUM_REG = UDC_EP_SEL|UDC_EP_DIR;
1558 stat = UDC_STAT_FLG_REG;
1559 if (stat & UDC_ACK) {
1561 /* write next IN packet from response,
1562 * or set up the status stage.
1565 stat = write_fifo(ep0, req);
1566 UDC_EP_NUM_REG = UDC_EP_DIR;
1567 if (!req && udc->ep0_pending) {
1568 UDC_EP_NUM_REG = UDC_EP_SEL;
1569 UDC_CTRL_REG = UDC_CLR_EP;
1570 UDC_CTRL_REG = UDC_SET_FIFO_EN;
1572 udc->ep0_pending = 0;
1573 } /* else: 6 wait states before it'll tx */
1575 /* ack status stage of OUT transfer */
1576 UDC_EP_NUM_REG = UDC_EP_DIR;
1581 } else if (stat & UDC_STALL) {
1582 UDC_CTRL_REG = UDC_CLR_HALT;
1583 UDC_EP_NUM_REG = UDC_EP_DIR;
1585 UDC_EP_NUM_REG = UDC_EP_DIR;
1589 /* OUT == RX from host */
1590 if (irq_src & UDC_EP0_RX) {
1593 UDC_IRQ_SRC_REG = UDC_EP0_RX;
1594 UDC_EP_NUM_REG = UDC_EP_SEL;
1595 stat = UDC_STAT_FLG_REG;
1596 if (stat & UDC_ACK) {
1599 /* read next OUT packet of request, maybe
1600 * reactiviting the fifo; stall on errors.
1602 if (!req || (stat = read_fifo(ep0, req)) < 0) {
1603 UDC_SYSCON2_REG = UDC_STALL_CMD;
1604 udc->ep0_pending = 0;
1606 } else if (stat == 0)
1607 UDC_CTRL_REG = UDC_SET_FIFO_EN;
1610 /* activate status stage */
1613 /* that may have STALLed ep0... */
1614 UDC_EP_NUM_REG = UDC_EP_SEL|UDC_EP_DIR;
1615 UDC_CTRL_REG = UDC_CLR_EP;
1616 UDC_CTRL_REG = UDC_SET_FIFO_EN;
1617 UDC_EP_NUM_REG = UDC_EP_DIR;
1618 udc->ep0_pending = 0;
1621 /* ack status stage of IN transfer */
1626 } else if (stat & UDC_STALL) {
1627 UDC_CTRL_REG = UDC_CLR_HALT;
1634 /* SETUP starts all control transfers */
1635 if (irq_src & UDC_SETUP) {
1638 struct usb_ctrlrequest r;
1640 int status = -EINVAL;
1643 /* read the (latest) SETUP message */
1645 UDC_EP_NUM_REG = UDC_SETUP_SEL;
1646 /* two bytes at a time */
1647 u.word[0] = UDC_DATA_REG;
1648 u.word[1] = UDC_DATA_REG;
1649 u.word[2] = UDC_DATA_REG;
1650 u.word[3] = UDC_DATA_REG;
1652 } while (UDC_IRQ_SRC_REG & UDC_SETUP);
1654 #define w_value le16_to_cpu(u.r.wValue)
1655 #define w_index le16_to_cpu(u.r.wIndex)
1656 #define w_length le16_to_cpu(u.r.wLength)
1658 /* Delegate almost all control requests to the gadget driver,
1659 * except for a handful of ch9 status/feature requests that
1660 * hardware doesn't autodecode _and_ the gadget API hides.
1662 udc->ep0_in = (u.r.bRequestType & USB_DIR_IN) != 0;
1663 udc->ep0_set_config = 0;
1664 udc->ep0_pending = 1;
1667 switch (u.r.bRequest) {
1668 case USB_REQ_SET_CONFIGURATION:
1669 /* udc needs to know when ep != 0 is valid */
1670 if (u.r.bRequestType != USB_RECIP_DEVICE)
1674 udc->ep0_set_config = 1;
1675 udc->ep0_reset_config = (w_value == 0);
1676 VDBG("set config %d\n", w_value);
1678 /* update udc NOW since gadget driver may start
1679 * queueing requests immediately; clear config
1680 * later if it fails the request.
1682 if (udc->ep0_reset_config)
1683 UDC_SYSCON2_REG = UDC_CLR_CFG;
1685 UDC_SYSCON2_REG = UDC_DEV_CFG;
1688 case USB_REQ_CLEAR_FEATURE:
1689 /* clear endpoint halt */
1690 if (u.r.bRequestType != USB_RECIP_ENDPOINT)
1692 if (w_value != USB_ENDPOINT_HALT
1695 ep = &udc->ep[w_index & 0xf];
1697 if (w_index & USB_DIR_IN)
1699 if (ep->bmAttributes == USB_ENDPOINT_XFER_ISOC
1703 UDC_CTRL_REG = udc->clr_halt;
1705 if (!(ep->bEndpointAddress & USB_DIR_IN)) {
1706 UDC_CTRL_REG = UDC_SET_FIFO_EN;
1707 ep->ackwait = 1 + ep->double_buf;
1709 /* NOTE: assumes the host behaves sanely,
1710 * only clearing real halts. Else we may
1711 * need to kill pending transfers and then
1712 * restart the queue... very messy for DMA!
1715 VDBG("%s halt cleared by host\n", ep->name);
1716 goto ep0out_status_stage;
1717 case USB_REQ_SET_FEATURE:
1718 /* set endpoint halt */
1719 if (u.r.bRequestType != USB_RECIP_ENDPOINT)
1721 if (w_value != USB_ENDPOINT_HALT
1724 ep = &udc->ep[w_index & 0xf];
1725 if (w_index & USB_DIR_IN)
1727 if (ep->bmAttributes == USB_ENDPOINT_XFER_ISOC
1728 || ep == ep0 || !ep->desc)
1730 if (use_dma && ep->has_dma) {
1731 /* this has rude side-effects (aborts) and
1732 * can't really work if DMA-IN is active
1734 DBG("%s host set_halt, NYET \n", ep->name);
1738 /* can't halt if fifo isn't empty... */
1739 UDC_CTRL_REG = UDC_CLR_EP;
1740 UDC_CTRL_REG = UDC_SET_HALT;
1741 VDBG("%s halted by host\n", ep->name);
1742 ep0out_status_stage:
1744 UDC_EP_NUM_REG = UDC_EP_SEL|UDC_EP_DIR;
1745 UDC_CTRL_REG = UDC_CLR_EP;
1746 UDC_CTRL_REG = UDC_SET_FIFO_EN;
1747 UDC_EP_NUM_REG = UDC_EP_DIR;
1748 udc->ep0_pending = 0;
1750 case USB_REQ_GET_STATUS:
1751 /* USB_ENDPOINT_HALT status? */
1752 if (u.r.bRequestType != (USB_DIR_IN|USB_RECIP_ENDPOINT))
1755 /* ep0 never stalls */
1756 if (!(w_index & 0xf))
1759 /* only active endpoints count */
1760 ep = &udc->ep[w_index & 0xf];
1761 if (w_index & USB_DIR_IN)
1766 /* iso never stalls */
1767 if (ep->bmAttributes == USB_ENDPOINT_XFER_ISOC)
1770 /* FIXME don't assume non-halted endpoints!! */
1771 ERR("%s status, can't report\n", ep->ep.name);
1775 /* return interface status. if we were pedantic,
1776 * we'd detect non-existent interfaces, and stall.
1778 if (u.r.bRequestType
1779 != (USB_DIR_IN|USB_RECIP_INTERFACE))
1783 /* return two zero bytes */
1784 UDC_EP_NUM_REG = UDC_EP_SEL|UDC_EP_DIR;
1786 UDC_CTRL_REG = UDC_SET_FIFO_EN;
1787 UDC_EP_NUM_REG = UDC_EP_DIR;
1789 VDBG("GET_STATUS, interface %d\n", w_index);
1790 /* next, status stage */
1794 /* activate the ep0out fifo right away */
1795 if (!udc->ep0_in && w_length) {
1797 UDC_CTRL_REG = UDC_SET_FIFO_EN;
1800 /* gadget drivers see class/vendor specific requests,
1801 * {SET,GET}_{INTERFACE,DESCRIPTOR,CONFIGURATION},
1804 VDBG("SETUP %02x.%02x v%04x i%04x l%04x\n",
1805 u.r.bRequestType, u.r.bRequest,
1806 w_value, w_index, w_length);
1812 /* The gadget driver may return an error here,
1813 * causing an immediate protocol stall.
1815 * Else it must issue a response, either queueing a
1816 * response buffer for the DATA stage, or halting ep0
1817 * (causing a protocol stall, not a real halt). A
1818 * zero length buffer means no DATA stage.
1820 * It's fine to issue that response after the setup()
1821 * call returns, and this IRQ was handled.
1824 spin_unlock(&udc->lock);
1825 status = udc->driver->setup (&udc->gadget, &u.r);
1826 spin_lock(&udc->lock);
1832 VDBG("req %02x.%02x protocol STALL; stat %d\n",
1833 u.r.bRequestType, u.r.bRequest, status);
1834 if (udc->ep0_set_config) {
1835 if (udc->ep0_reset_config)
1836 WARN("error resetting config?\n");
1838 UDC_SYSCON2_REG = UDC_CLR_CFG;
1840 UDC_SYSCON2_REG = UDC_STALL_CMD;
1841 udc->ep0_pending = 0;
1846 /*-------------------------------------------------------------------------*/
1848 #define OTG_FLAGS (UDC_B_HNP_ENABLE|UDC_A_HNP_SUPPORT|UDC_A_ALT_HNP_SUPPORT)
1850 static void devstate_irq(struct omap_udc *udc, u16 irq_src)
1852 u16 devstat, change;
1854 devstat = UDC_DEVSTAT_REG;
1855 change = devstat ^ udc->devstat;
1856 udc->devstat = devstat;
1858 if (change & (UDC_USB_RESET|UDC_ATT)) {
1861 if (change & UDC_ATT) {
1862 /* driver for any external transceiver will
1863 * have called omap_vbus_session() already
1865 if (devstat & UDC_ATT) {
1866 udc->gadget.speed = USB_SPEED_FULL;
1868 if (!udc->transceiver)
1870 // if (driver->connect) call it
1871 } else if (udc->gadget.speed != USB_SPEED_UNKNOWN) {
1872 udc->gadget.speed = USB_SPEED_UNKNOWN;
1873 if (!udc->transceiver)
1874 pullup_disable(udc);
1875 DBG("disconnect, gadget %s\n",
1876 udc->driver->driver.name);
1877 if (udc->driver->disconnect) {
1878 spin_unlock(&udc->lock);
1879 udc->driver->disconnect(&udc->gadget);
1880 spin_lock(&udc->lock);
1886 if (change & UDC_USB_RESET) {
1887 if (devstat & UDC_USB_RESET) {
1890 udc->gadget.speed = USB_SPEED_FULL;
1891 INFO("USB reset done, gadget %s\n",
1892 udc->driver->driver.name);
1893 /* ep0 traffic is legal from now on */
1894 UDC_IRQ_EN_REG = UDC_DS_CHG_IE | UDC_EP0_IE;
1896 change &= ~UDC_USB_RESET;
1899 if (change & UDC_SUS) {
1900 if (udc->gadget.speed != USB_SPEED_UNKNOWN) {
1901 // FIXME tell isp1301 to suspend/resume (?)
1902 if (devstat & UDC_SUS) {
1905 /* HNP could be under way already */
1906 if (udc->gadget.speed == USB_SPEED_FULL
1907 && udc->driver->suspend) {
1908 spin_unlock(&udc->lock);
1909 udc->driver->suspend(&udc->gadget);
1910 spin_lock(&udc->lock);
1912 if (udc->transceiver)
1913 otg_set_suspend(udc->transceiver, 1);
1916 if (udc->transceiver)
1917 otg_set_suspend(udc->transceiver, 0);
1918 if (udc->gadget.speed == USB_SPEED_FULL
1919 && udc->driver->resume) {
1920 spin_unlock(&udc->lock);
1921 udc->driver->resume(&udc->gadget);
1922 spin_lock(&udc->lock);
1928 if (!cpu_is_omap15xx() && (change & OTG_FLAGS)) {
1930 change &= ~OTG_FLAGS;
1933 change &= ~(UDC_CFG|UDC_DEF|UDC_ADD);
1935 VDBG("devstat %03x, ignore change %03x\n",
1938 UDC_IRQ_SRC_REG = UDC_DS_CHG;
1941 static irqreturn_t omap_udc_irq(int irq, void *_udc)
1943 struct omap_udc *udc = _udc;
1945 irqreturn_t status = IRQ_NONE;
1946 unsigned long flags;
1948 spin_lock_irqsave(&udc->lock, flags);
1949 irq_src = UDC_IRQ_SRC_REG;
1951 /* Device state change (usb ch9 stuff) */
1952 if (irq_src & UDC_DS_CHG) {
1953 devstate_irq(_udc, irq_src);
1954 status = IRQ_HANDLED;
1955 irq_src &= ~UDC_DS_CHG;
1958 /* EP0 control transfers */
1959 if (irq_src & (UDC_EP0_RX|UDC_SETUP|UDC_EP0_TX)) {
1960 ep0_irq(_udc, irq_src);
1961 status = IRQ_HANDLED;
1962 irq_src &= ~(UDC_EP0_RX|UDC_SETUP|UDC_EP0_TX);
1965 /* DMA transfer completion */
1966 if (use_dma && (irq_src & (UDC_TXN_DONE|UDC_RXN_CNT|UDC_RXN_EOT))) {
1967 dma_irq(_udc, irq_src);
1968 status = IRQ_HANDLED;
1969 irq_src &= ~(UDC_TXN_DONE|UDC_RXN_CNT|UDC_RXN_EOT);
1972 irq_src &= ~(UDC_SOF|UDC_EPN_TX|UDC_EPN_RX);
1974 DBG("udc_irq, unhandled %03x\n", irq_src);
1975 spin_unlock_irqrestore(&udc->lock, flags);
1980 /* workaround for seemingly-lost IRQs for RX ACKs... */
1981 #define PIO_OUT_TIMEOUT (jiffies + HZ/3)
1982 #define HALF_FULL(f) (!((f)&(UDC_NON_ISO_FIFO_FULL|UDC_NON_ISO_FIFO_EMPTY)))
1984 static void pio_out_timer(unsigned long _ep)
1986 struct omap_ep *ep = (void *) _ep;
1987 unsigned long flags;
1990 spin_lock_irqsave(&ep->udc->lock, flags);
1991 if (!list_empty(&ep->queue) && ep->ackwait) {
1992 use_ep(ep, UDC_EP_SEL);
1993 stat_flg = UDC_STAT_FLG_REG;
1995 if ((stat_flg & UDC_ACK) && (!(stat_flg & UDC_FIFO_EN)
1996 || (ep->double_buf && HALF_FULL(stat_flg)))) {
1997 struct omap_req *req;
1999 VDBG("%s: lose, %04x\n", ep->ep.name, stat_flg);
2000 req = container_of(ep->queue.next,
2001 struct omap_req, queue);
2002 (void) read_fifo(ep, req);
2003 UDC_EP_NUM_REG = ep->bEndpointAddress;
2004 UDC_CTRL_REG = UDC_SET_FIFO_EN;
2005 ep->ackwait = 1 + ep->double_buf;
2009 mod_timer(&ep->timer, PIO_OUT_TIMEOUT);
2010 spin_unlock_irqrestore(&ep->udc->lock, flags);
2013 static irqreturn_t omap_udc_pio_irq(int irq, void *_dev)
2015 u16 epn_stat, irq_src;
2016 irqreturn_t status = IRQ_NONE;
2019 struct omap_udc *udc = _dev;
2020 struct omap_req *req;
2021 unsigned long flags;
2023 spin_lock_irqsave(&udc->lock, flags);
2024 epn_stat = UDC_EPN_STAT_REG;
2025 irq_src = UDC_IRQ_SRC_REG;
2027 /* handle OUT first, to avoid some wasteful NAKs */
2028 if (irq_src & UDC_EPN_RX) {
2029 epnum = (epn_stat >> 8) & 0x0f;
2030 UDC_IRQ_SRC_REG = UDC_EPN_RX;
2031 status = IRQ_HANDLED;
2032 ep = &udc->ep[epnum];
2035 UDC_EP_NUM_REG = epnum | UDC_EP_SEL;
2037 if ((UDC_STAT_FLG_REG & UDC_ACK)) {
2039 if (!list_empty(&ep->queue)) {
2041 req = container_of(ep->queue.next,
2042 struct omap_req, queue);
2043 stat = read_fifo(ep, req);
2044 if (!ep->double_buf)
2048 /* min 6 clock delay before clearing EP_SEL ... */
2049 epn_stat = UDC_EPN_STAT_REG;
2050 epn_stat = UDC_EPN_STAT_REG;
2051 UDC_EP_NUM_REG = epnum;
2053 /* enabling fifo _after_ clearing ACK, contrary to docs,
2054 * reduces lossage; timer still needed though (sigh).
2057 UDC_CTRL_REG = UDC_SET_FIFO_EN;
2058 ep->ackwait = 1 + ep->double_buf;
2060 mod_timer(&ep->timer, PIO_OUT_TIMEOUT);
2063 /* then IN transfers */
2064 else if (irq_src & UDC_EPN_TX) {
2065 epnum = epn_stat & 0x0f;
2066 UDC_IRQ_SRC_REG = UDC_EPN_TX;
2067 status = IRQ_HANDLED;
2068 ep = &udc->ep[16 + epnum];
2071 UDC_EP_NUM_REG = epnum | UDC_EP_DIR | UDC_EP_SEL;
2072 if ((UDC_STAT_FLG_REG & UDC_ACK)) {
2074 if (!list_empty(&ep->queue)) {
2075 req = container_of(ep->queue.next,
2076 struct omap_req, queue);
2077 (void) write_fifo(ep, req);
2080 /* min 6 clock delay before clearing EP_SEL ... */
2081 epn_stat = UDC_EPN_STAT_REG;
2082 epn_stat = UDC_EPN_STAT_REG;
2083 UDC_EP_NUM_REG = epnum | UDC_EP_DIR;
2084 /* then 6 clocks before it'd tx */
2087 spin_unlock_irqrestore(&udc->lock, flags);
2092 static irqreturn_t omap_udc_iso_irq(int irq, void *_dev)
2094 struct omap_udc *udc = _dev;
2097 unsigned long flags;
2099 spin_lock_irqsave(&udc->lock, flags);
2101 /* handle all non-DMA ISO transfers */
2102 list_for_each_entry (ep, &udc->iso, iso) {
2104 struct omap_req *req;
2106 if (ep->has_dma || list_empty(&ep->queue))
2108 req = list_entry(ep->queue.next, struct omap_req, queue);
2110 use_ep(ep, UDC_EP_SEL);
2111 stat = UDC_STAT_FLG_REG;
2113 /* NOTE: like the other controller drivers, this isn't
2114 * currently reporting lost or damaged frames.
2116 if (ep->bEndpointAddress & USB_DIR_IN) {
2117 if (stat & UDC_MISS_IN)
2118 /* done(ep, req, -EPROTO) */;
2120 write_fifo(ep, req);
2124 if (stat & UDC_NO_RXPACKET)
2125 status = -EREMOTEIO;
2126 else if (stat & UDC_ISO_ERR)
2128 else if (stat & UDC_DATA_FLUSH)
2132 /* done(ep, req, status) */;
2137 /* 6 wait states before next EP */
2140 if (!list_empty(&ep->queue))
2144 UDC_IRQ_EN_REG &= ~UDC_SOF_IE;
2145 UDC_IRQ_SRC_REG = UDC_SOF;
2147 spin_unlock_irqrestore(&udc->lock, flags);
2152 /*-------------------------------------------------------------------------*/
2154 static inline int machine_without_vbus_sense(void)
2156 return (machine_is_omap_innovator()
2157 || machine_is_omap_osk()
2158 || machine_is_omap_apollon()
2159 #ifndef CONFIG_MACH_OMAP_H4_OTG
2160 || machine_is_omap_h4()
2166 int usb_gadget_register_driver (struct usb_gadget_driver *driver)
2168 int status = -ENODEV;
2170 unsigned long flags;
2172 /* basic sanity tests */
2176 // FIXME if otg, check: driver->is_otg
2177 || driver->speed < USB_SPEED_FULL
2182 spin_lock_irqsave(&udc->lock, flags);
2184 spin_unlock_irqrestore(&udc->lock, flags);
2189 list_for_each_entry (ep, &udc->gadget.ep_list, ep.ep_list) {
2191 if (ep->bmAttributes == USB_ENDPOINT_XFER_ISOC)
2194 UDC_CTRL_REG = UDC_SET_HALT;
2196 udc->ep0_pending = 0;
2197 udc->ep[0].irqs = 0;
2198 udc->softconnect = 1;
2200 /* hook up the driver */
2201 driver->driver.bus = NULL;
2202 udc->driver = driver;
2203 udc->gadget.dev.driver = &driver->driver;
2204 spin_unlock_irqrestore(&udc->lock, flags);
2206 if (udc->dc_clk != NULL)
2207 omap_udc_enable_clock(1);
2209 status = driver->bind (&udc->gadget);
2211 DBG("bind to %s --> %d\n", driver->driver.name, status);
2212 udc->gadget.dev.driver = NULL;
2216 DBG("bound to driver %s\n", driver->driver.name);
2218 UDC_IRQ_SRC_REG = UDC_IRQ_SRC_MASK;
2220 /* connect to bus through transceiver */
2221 if (udc->transceiver) {
2222 status = otg_set_peripheral(udc->transceiver, &udc->gadget);
2224 ERR("can't bind to transceiver\n");
2225 if (driver->unbind) {
2226 driver->unbind (&udc->gadget);
2227 udc->gadget.dev.driver = NULL;
2233 if (can_pullup(udc))
2234 pullup_enable (udc);
2236 pullup_disable (udc);
2239 /* boards that don't have VBUS sensing can't autogate 48MHz;
2240 * can't enter deep sleep while a gadget driver is active.
2242 if (machine_without_vbus_sense())
2243 omap_vbus_session(&udc->gadget, 1);
2246 if (udc->dc_clk != NULL)
2247 omap_udc_enable_clock(0);
2250 EXPORT_SYMBOL(usb_gadget_register_driver);
2252 int usb_gadget_unregister_driver (struct usb_gadget_driver *driver)
2254 unsigned long flags;
2255 int status = -ENODEV;
2259 if (!driver || driver != udc->driver || !driver->unbind)
2262 if (udc->dc_clk != NULL)
2263 omap_udc_enable_clock(1);
2265 if (machine_without_vbus_sense())
2266 omap_vbus_session(&udc->gadget, 0);
2268 if (udc->transceiver)
2269 (void) otg_set_peripheral(udc->transceiver, NULL);
2271 pullup_disable(udc);
2273 spin_lock_irqsave(&udc->lock, flags);
2275 spin_unlock_irqrestore(&udc->lock, flags);
2277 driver->unbind(&udc->gadget);
2278 udc->gadget.dev.driver = NULL;
2281 if (udc->dc_clk != NULL)
2282 omap_udc_enable_clock(0);
2283 DBG("unregistered driver '%s'\n", driver->driver.name);
2286 EXPORT_SYMBOL(usb_gadget_unregister_driver);
2289 /*-------------------------------------------------------------------------*/
2291 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
2293 #include <linux/seq_file.h>
2295 static const char proc_filename[] = "driver/udc";
2297 #define FOURBITS "%s%s%s%s"
2298 #define EIGHTBITS FOURBITS FOURBITS
2300 static void proc_ep_show(struct seq_file *s, struct omap_ep *ep)
2303 struct omap_req *req;
2308 if (use_dma && ep->has_dma)
2309 snprintf(buf, sizeof buf, "(%cxdma%d lch%d) ",
2310 (ep->bEndpointAddress & USB_DIR_IN) ? 't' : 'r',
2311 ep->dma_channel - 1, ep->lch);
2315 stat_flg = UDC_STAT_FLG_REG;
2317 "\n%s %s%s%sirqs %ld stat %04x " EIGHTBITS FOURBITS "%s\n",
2319 ep->double_buf ? "dbuf " : "",
2320 ({char *s; switch(ep->ackwait){
2321 case 0: s = ""; break;
2322 case 1: s = "(ackw) "; break;
2323 case 2: s = "(ackw2) "; break;
2324 default: s = "(?) "; break;
2327 (stat_flg & UDC_NO_RXPACKET) ? "no_rxpacket " : "",
2328 (stat_flg & UDC_MISS_IN) ? "miss_in " : "",
2329 (stat_flg & UDC_DATA_FLUSH) ? "data_flush " : "",
2330 (stat_flg & UDC_ISO_ERR) ? "iso_err " : "",
2331 (stat_flg & UDC_ISO_FIFO_EMPTY) ? "iso_fifo_empty " : "",
2332 (stat_flg & UDC_ISO_FIFO_FULL) ? "iso_fifo_full " : "",
2333 (stat_flg & UDC_EP_HALTED) ? "HALT " : "",
2334 (stat_flg & UDC_STALL) ? "STALL " : "",
2335 (stat_flg & UDC_NAK) ? "NAK " : "",
2336 (stat_flg & UDC_ACK) ? "ACK " : "",
2337 (stat_flg & UDC_FIFO_EN) ? "fifo_en " : "",
2338 (stat_flg & UDC_NON_ISO_FIFO_EMPTY) ? "fifo_empty " : "",
2339 (stat_flg & UDC_NON_ISO_FIFO_FULL) ? "fifo_full " : "");
2341 if (list_empty (&ep->queue))
2342 seq_printf(s, "\t(queue empty)\n");
2344 list_for_each_entry (req, &ep->queue, queue) {
2345 unsigned length = req->req.actual;
2347 if (use_dma && buf[0]) {
2348 length += ((ep->bEndpointAddress & USB_DIR_IN)
2349 ? dma_src_len : dma_dest_len)
2350 (ep, req->req.dma + length);
2353 seq_printf(s, "\treq %p len %d/%d buf %p\n",
2355 req->req.length, req->req.buf);
2359 static char *trx_mode(unsigned m, int enabled)
2362 case 0: return enabled ? "*6wire" : "unused";
2363 case 1: return "4wire";
2364 case 2: return "3wire";
2365 case 3: return "6wire";
2366 default: return "unknown";
2370 static int proc_otg_show(struct seq_file *s)
2377 if (cpu_is_omap24xx()) {
2378 ctrl_name = "control_devconf";
2379 trans = CONTROL_DEVCONF_REG;
2381 ctrl_name = "tranceiver_ctrl";
2382 trans = USB_TRANSCEIVER_CTRL_REG;
2384 seq_printf(s, "\nOTG rev %d.%d, %s %05x\n",
2385 tmp >> 4, tmp & 0xf, ctrl_name, trans);
2386 tmp = OTG_SYSCON_1_REG;
2387 seq_printf(s, "otg_syscon1 %08x usb2 %s, usb1 %s, usb0 %s,"
2389 trx_mode(USB2_TRX_MODE(tmp), trans & CONF_USB2_UNI_R),
2390 trx_mode(USB1_TRX_MODE(tmp), trans & CONF_USB1_UNI_R),
2391 (USB0_TRX_MODE(tmp) == 0 && !cpu_is_omap1710())
2393 : trx_mode(USB0_TRX_MODE(tmp), 1),
2394 (tmp & OTG_IDLE_EN) ? " !otg" : "",
2395 (tmp & HST_IDLE_EN) ? " !host" : "",
2396 (tmp & DEV_IDLE_EN) ? " !dev" : "",
2397 (tmp & OTG_RESET_DONE) ? " reset_done" : " reset_active");
2398 tmp = OTG_SYSCON_2_REG;
2399 seq_printf(s, "otg_syscon2 %08x%s" EIGHTBITS
2400 " b_ase_brst=%d hmc=%d\n", tmp,
2401 (tmp & OTG_EN) ? " otg_en" : "",
2402 (tmp & USBX_SYNCHRO) ? " synchro" : "",
2403 // much more SRP stuff
2404 (tmp & SRP_DATA) ? " srp_data" : "",
2405 (tmp & SRP_VBUS) ? " srp_vbus" : "",
2406 (tmp & OTG_PADEN) ? " otg_paden" : "",
2407 (tmp & HMC_PADEN) ? " hmc_paden" : "",
2408 (tmp & UHOST_EN) ? " uhost_en" : "",
2409 (tmp & HMC_TLLSPEED) ? " tllspeed" : "",
2410 (tmp & HMC_TLLATTACH) ? " tllattach" : "",
2414 seq_printf(s, "otg_ctrl %06x" EIGHTBITS EIGHTBITS "%s\n", tmp,
2415 (tmp & OTG_ASESSVLD) ? " asess" : "",
2416 (tmp & OTG_BSESSEND) ? " bsess_end" : "",
2417 (tmp & OTG_BSESSVLD) ? " bsess" : "",
2418 (tmp & OTG_VBUSVLD) ? " vbus" : "",
2419 (tmp & OTG_ID) ? " id" : "",
2420 (tmp & OTG_DRIVER_SEL) ? " DEVICE" : " HOST",
2421 (tmp & OTG_A_SETB_HNPEN) ? " a_setb_hnpen" : "",
2422 (tmp & OTG_A_BUSREQ) ? " a_bus" : "",
2423 (tmp & OTG_B_HNPEN) ? " b_hnpen" : "",
2424 (tmp & OTG_B_BUSREQ) ? " b_bus" : "",
2425 (tmp & OTG_BUSDROP) ? " busdrop" : "",
2426 (tmp & OTG_PULLDOWN) ? " down" : "",
2427 (tmp & OTG_PULLUP) ? " up" : "",
2428 (tmp & OTG_DRV_VBUS) ? " drv" : "",
2429 (tmp & OTG_PD_VBUS) ? " pd_vb" : "",
2430 (tmp & OTG_PU_VBUS) ? " pu_vb" : "",
2431 (tmp & OTG_PU_ID) ? " pu_id" : ""
2433 tmp = OTG_IRQ_EN_REG;
2434 seq_printf(s, "otg_irq_en %04x" "\n", tmp);
2435 tmp = OTG_IRQ_SRC_REG;
2436 seq_printf(s, "otg_irq_src %04x" "\n", tmp);
2437 tmp = OTG_OUTCTRL_REG;
2438 seq_printf(s, "otg_outctrl %04x" "\n", tmp);
2440 seq_printf(s, "otg_test %04x" "\n", tmp);
2444 static int proc_udc_show(struct seq_file *s, void *_)
2448 unsigned long flags;
2450 spin_lock_irqsave(&udc->lock, flags);
2452 seq_printf(s, "%s, version: " DRIVER_VERSION
2458 use_dma ? " (dma)" : "");
2460 tmp = UDC_REV_REG & 0xff;
2462 "UDC rev %d.%d, fifo mode %d, gadget %s\n"
2463 "hmc %d, transceiver %s\n",
2464 tmp >> 4, tmp & 0xf,
2466 udc->driver ? udc->driver->driver.name : "(none)",
2469 ? udc->transceiver->label
2470 : ((cpu_is_omap1710() || cpu_is_omap24xx())
2471 ? "external" : "(none)"));
2472 if (cpu_class_is_omap1()) {
2473 seq_printf(s, "ULPD control %04x req %04x status %04x\n",
2474 __REG16(ULPD_CLOCK_CTRL),
2475 __REG16(ULPD_SOFT_REQ),
2476 __REG16(ULPD_STATUS_REQ));
2479 /* OTG controller registers */
2480 if (!cpu_is_omap15xx())
2483 tmp = UDC_SYSCON1_REG;
2484 seq_printf(s, "\nsyscon1 %04x" EIGHTBITS "\n", tmp,
2485 (tmp & UDC_CFG_LOCK) ? " cfg_lock" : "",
2486 (tmp & UDC_DATA_ENDIAN) ? " data_endian" : "",
2487 (tmp & UDC_DMA_ENDIAN) ? " dma_endian" : "",
2488 (tmp & UDC_NAK_EN) ? " nak" : "",
2489 (tmp & UDC_AUTODECODE_DIS) ? " autodecode_dis" : "",
2490 (tmp & UDC_SELF_PWR) ? " self_pwr" : "",
2491 (tmp & UDC_SOFF_DIS) ? " soff_dis" : "",
2492 (tmp & UDC_PULLUP_EN) ? " PULLUP" : "");
2493 // syscon2 is write-only
2495 /* UDC controller registers */
2496 if (!(tmp & UDC_PULLUP_EN)) {
2497 seq_printf(s, "(suspended)\n");
2498 spin_unlock_irqrestore(&udc->lock, flags);
2502 tmp = UDC_DEVSTAT_REG;
2503 seq_printf(s, "devstat %04x" EIGHTBITS "%s%s\n", tmp,
2504 (tmp & UDC_B_HNP_ENABLE) ? " b_hnp" : "",
2505 (tmp & UDC_A_HNP_SUPPORT) ? " a_hnp" : "",
2506 (tmp & UDC_A_ALT_HNP_SUPPORT) ? " a_alt_hnp" : "",
2507 (tmp & UDC_R_WK_OK) ? " r_wk_ok" : "",
2508 (tmp & UDC_USB_RESET) ? " usb_reset" : "",
2509 (tmp & UDC_SUS) ? " SUS" : "",
2510 (tmp & UDC_CFG) ? " CFG" : "",
2511 (tmp & UDC_ADD) ? " ADD" : "",
2512 (tmp & UDC_DEF) ? " DEF" : "",
2513 (tmp & UDC_ATT) ? " ATT" : "");
2514 seq_printf(s, "sof %04x\n", UDC_SOF_REG);
2515 tmp = UDC_IRQ_EN_REG;
2516 seq_printf(s, "irq_en %04x" FOURBITS "%s\n", tmp,
2517 (tmp & UDC_SOF_IE) ? " sof" : "",
2518 (tmp & UDC_EPN_RX_IE) ? " epn_rx" : "",
2519 (tmp & UDC_EPN_TX_IE) ? " epn_tx" : "",
2520 (tmp & UDC_DS_CHG_IE) ? " ds_chg" : "",
2521 (tmp & UDC_EP0_IE) ? " ep0" : "");
2522 tmp = UDC_IRQ_SRC_REG;
2523 seq_printf(s, "irq_src %04x" EIGHTBITS "%s%s\n", tmp,
2524 (tmp & UDC_TXN_DONE) ? " txn_done" : "",
2525 (tmp & UDC_RXN_CNT) ? " rxn_cnt" : "",
2526 (tmp & UDC_RXN_EOT) ? " rxn_eot" : "",
2527 (tmp & UDC_SOF) ? " sof" : "",
2528 (tmp & UDC_EPN_RX) ? " epn_rx" : "",
2529 (tmp & UDC_EPN_TX) ? " epn_tx" : "",
2530 (tmp & UDC_DS_CHG) ? " ds_chg" : "",
2531 (tmp & UDC_SETUP) ? " setup" : "",
2532 (tmp & UDC_EP0_RX) ? " ep0out" : "",
2533 (tmp & UDC_EP0_TX) ? " ep0in" : "");
2537 tmp = UDC_DMA_IRQ_EN_REG;
2538 seq_printf(s, "dma_irq_en %04x%s" EIGHTBITS "\n", tmp,
2539 (tmp & UDC_TX_DONE_IE(3)) ? " tx2_done" : "",
2540 (tmp & UDC_RX_CNT_IE(3)) ? " rx2_cnt" : "",
2541 (tmp & UDC_RX_EOT_IE(3)) ? " rx2_eot" : "",
2543 (tmp & UDC_TX_DONE_IE(2)) ? " tx1_done" : "",
2544 (tmp & UDC_RX_CNT_IE(2)) ? " rx1_cnt" : "",
2545 (tmp & UDC_RX_EOT_IE(2)) ? " rx1_eot" : "",
2547 (tmp & UDC_TX_DONE_IE(1)) ? " tx0_done" : "",
2548 (tmp & UDC_RX_CNT_IE(1)) ? " rx0_cnt" : "",
2549 (tmp & UDC_RX_EOT_IE(1)) ? " rx0_eot" : "");
2551 tmp = UDC_RXDMA_CFG_REG;
2552 seq_printf(s, "rxdma_cfg %04x\n", tmp);
2554 for (i = 0; i < 3; i++) {
2555 if ((tmp & (0x0f << (i * 4))) == 0)
2557 seq_printf(s, "rxdma[%d] %04x\n", i,
2558 UDC_RXDMA_REG(i + 1));
2561 tmp = UDC_TXDMA_CFG_REG;
2562 seq_printf(s, "txdma_cfg %04x\n", tmp);
2564 for (i = 0; i < 3; i++) {
2565 if (!(tmp & (0x0f << (i * 4))))
2567 seq_printf(s, "txdma[%d] %04x\n", i,
2568 UDC_TXDMA_REG(i + 1));
2573 tmp = UDC_DEVSTAT_REG;
2574 if (tmp & UDC_ATT) {
2575 proc_ep_show(s, &udc->ep[0]);
2576 if (tmp & UDC_ADD) {
2577 list_for_each_entry (ep, &udc->gadget.ep_list,
2580 proc_ep_show(s, ep);
2584 spin_unlock_irqrestore(&udc->lock, flags);
2588 static int proc_udc_open(struct inode *inode, struct file *file)
2590 return single_open(file, proc_udc_show, NULL);
2593 static const struct file_operations proc_ops = {
2594 .open = proc_udc_open,
2596 .llseek = seq_lseek,
2597 .release = single_release,
2600 static void create_proc_file(void)
2602 struct proc_dir_entry *pde;
2604 pde = create_proc_entry (proc_filename, 0, NULL);
2606 pde->proc_fops = &proc_ops;
2609 static void remove_proc_file(void)
2611 remove_proc_entry(proc_filename, NULL);
2616 static inline void create_proc_file(void) {}
2617 static inline void remove_proc_file(void) {}
2621 /*-------------------------------------------------------------------------*/
2623 /* Before this controller can enumerate, we need to pick an endpoint
2624 * configuration, or "fifo_mode" That involves allocating 2KB of packet
2625 * buffer space among the endpoints we'll be operating.
2627 * NOTE: as of OMAP 1710 ES2.0, writing a new endpoint config when
2628 * UDC_SYSCON_1_REG.CFG_LOCK is set can now work. We won't use that
2629 * capability yet though.
2631 static unsigned __init
2632 omap_ep_setup(char *name, u8 addr, u8 type,
2633 unsigned buf, unsigned maxp, int dbuf)
2638 /* OUT endpoints first, then IN */
2639 ep = &udc->ep[addr & 0xf];
2640 if (addr & USB_DIR_IN)
2643 /* in case of ep init table bugs */
2644 BUG_ON(ep->name[0]);
2646 /* chip setup ... bit values are same for IN, OUT */
2647 if (type == USB_ENDPOINT_XFER_ISOC) {
2649 case 8: epn_rxtx = 0 << 12; break;
2650 case 16: epn_rxtx = 1 << 12; break;
2651 case 32: epn_rxtx = 2 << 12; break;
2652 case 64: epn_rxtx = 3 << 12; break;
2653 case 128: epn_rxtx = 4 << 12; break;
2654 case 256: epn_rxtx = 5 << 12; break;
2655 case 512: epn_rxtx = 6 << 12; break;
2658 epn_rxtx |= UDC_EPN_RX_ISO;
2661 /* double-buffering "not supported" on 15xx,
2662 * and ignored for PIO-IN on newer chips
2663 * (for more reliable behavior)
2665 if (!use_dma || cpu_is_omap15xx() || cpu_is_omap24xx())
2669 case 8: epn_rxtx = 0 << 12; break;
2670 case 16: epn_rxtx = 1 << 12; break;
2671 case 32: epn_rxtx = 2 << 12; break;
2672 case 64: epn_rxtx = 3 << 12; break;
2676 epn_rxtx |= UDC_EPN_RX_DB;
2677 init_timer(&ep->timer);
2678 ep->timer.function = pio_out_timer;
2679 ep->timer.data = (unsigned long) ep;
2682 epn_rxtx |= UDC_EPN_RX_VALID;
2684 epn_rxtx |= buf >> 3;
2686 DBG("%s addr %02x rxtx %04x maxp %d%s buf %d\n",
2687 name, addr, epn_rxtx, maxp, dbuf ? "x2" : "", buf);
2689 if (addr & USB_DIR_IN)
2690 UDC_EP_TX_REG(addr & 0xf) = epn_rxtx;
2692 UDC_EP_RX_REG(addr) = epn_rxtx;
2694 /* next endpoint's buffer starts after this one's */
2700 /* set up driver data structures */
2701 BUG_ON(strlen(name) >= sizeof ep->name);
2702 strlcpy(ep->name, name, sizeof ep->name);
2703 INIT_LIST_HEAD(&ep->queue);
2704 INIT_LIST_HEAD(&ep->iso);
2705 ep->bEndpointAddress = addr;
2706 ep->bmAttributes = type;
2707 ep->double_buf = dbuf;
2710 ep->ep.name = ep->name;
2711 ep->ep.ops = &omap_ep_ops;
2712 ep->ep.maxpacket = ep->maxpacket = maxp;
2713 list_add_tail (&ep->ep.ep_list, &udc->gadget.ep_list);
2718 static void omap_udc_release(struct device *dev)
2720 complete(udc->done);
2726 omap_udc_setup(struct platform_device *odev, struct otg_transceiver *xceiv)
2730 /* abolish any previous hardware state */
2731 UDC_SYSCON1_REG = 0;
2733 UDC_IRQ_SRC_REG = UDC_IRQ_SRC_MASK;
2734 UDC_DMA_IRQ_EN_REG = 0;
2735 UDC_RXDMA_CFG_REG = 0;
2736 UDC_TXDMA_CFG_REG = 0;
2738 /* UDC_PULLUP_EN gates the chip clock */
2739 // OTG_SYSCON_1_REG |= DEV_IDLE_EN;
2741 udc = kzalloc(sizeof(*udc), GFP_KERNEL);
2745 spin_lock_init (&udc->lock);
2747 udc->gadget.ops = &omap_gadget_ops;
2748 udc->gadget.ep0 = &udc->ep[0].ep;
2749 INIT_LIST_HEAD(&udc->gadget.ep_list);
2750 INIT_LIST_HEAD(&udc->iso);
2751 udc->gadget.speed = USB_SPEED_UNKNOWN;
2752 udc->gadget.name = driver_name;
2754 device_initialize(&udc->gadget.dev);
2755 strcpy (udc->gadget.dev.bus_id, "gadget");
2756 udc->gadget.dev.release = omap_udc_release;
2757 udc->gadget.dev.parent = &odev->dev;
2759 udc->gadget.dev.dma_mask = odev->dev.dma_mask;
2761 udc->transceiver = xceiv;
2763 /* ep0 is special; put it right after the SETUP buffer */
2764 buf = omap_ep_setup("ep0", 0, USB_ENDPOINT_XFER_CONTROL,
2765 8 /* after SETUP */, 64 /* maxpacket */, 0);
2766 list_del_init(&udc->ep[0].ep.ep_list);
2768 /* initially disable all non-ep0 endpoints */
2769 for (tmp = 1; tmp < 15; tmp++) {
2770 UDC_EP_RX_REG(tmp) = 0;
2771 UDC_EP_TX_REG(tmp) = 0;
2774 #define OMAP_BULK_EP(name,addr) \
2775 buf = omap_ep_setup(name "-bulk", addr, \
2776 USB_ENDPOINT_XFER_BULK, buf, 64, 1);
2777 #define OMAP_INT_EP(name,addr, maxp) \
2778 buf = omap_ep_setup(name "-int", addr, \
2779 USB_ENDPOINT_XFER_INT, buf, maxp, 0);
2780 #define OMAP_ISO_EP(name,addr, maxp) \
2781 buf = omap_ep_setup(name "-iso", addr, \
2782 USB_ENDPOINT_XFER_ISOC, buf, maxp, 1);
2784 switch (fifo_mode) {
2786 OMAP_BULK_EP("ep1in", USB_DIR_IN | 1);
2787 OMAP_BULK_EP("ep2out", USB_DIR_OUT | 2);
2788 OMAP_INT_EP("ep3in", USB_DIR_IN | 3, 16);
2791 OMAP_BULK_EP("ep1in", USB_DIR_IN | 1);
2792 OMAP_BULK_EP("ep2out", USB_DIR_OUT | 2);
2793 OMAP_INT_EP("ep9in", USB_DIR_IN | 9, 16);
2795 OMAP_BULK_EP("ep3in", USB_DIR_IN | 3);
2796 OMAP_BULK_EP("ep4out", USB_DIR_OUT | 4);
2797 OMAP_INT_EP("ep10in", USB_DIR_IN | 10, 16);
2799 OMAP_BULK_EP("ep5in", USB_DIR_IN | 5);
2800 OMAP_BULK_EP("ep5out", USB_DIR_OUT | 5);
2801 OMAP_INT_EP("ep11in", USB_DIR_IN | 11, 16);
2803 OMAP_BULK_EP("ep6in", USB_DIR_IN | 6);
2804 OMAP_BULK_EP("ep6out", USB_DIR_OUT | 6);
2805 OMAP_INT_EP("ep12in", USB_DIR_IN | 12, 16);
2807 OMAP_BULK_EP("ep7in", USB_DIR_IN | 7);
2808 OMAP_BULK_EP("ep7out", USB_DIR_OUT | 7);
2809 OMAP_INT_EP("ep13in", USB_DIR_IN | 13, 16);
2810 OMAP_INT_EP("ep13out", USB_DIR_OUT | 13, 16);
2812 OMAP_BULK_EP("ep8in", USB_DIR_IN | 8);
2813 OMAP_BULK_EP("ep8out", USB_DIR_OUT | 8);
2814 OMAP_INT_EP("ep14in", USB_DIR_IN | 14, 16);
2815 OMAP_INT_EP("ep14out", USB_DIR_OUT | 14, 16);
2817 OMAP_BULK_EP("ep15in", USB_DIR_IN | 15);
2818 OMAP_BULK_EP("ep15out", USB_DIR_OUT | 15);
2823 case 2: /* mixed iso/bulk */
2824 OMAP_ISO_EP("ep1in", USB_DIR_IN | 1, 256);
2825 OMAP_ISO_EP("ep2out", USB_DIR_OUT | 2, 256);
2826 OMAP_ISO_EP("ep3in", USB_DIR_IN | 3, 128);
2827 OMAP_ISO_EP("ep4out", USB_DIR_OUT | 4, 128);
2829 OMAP_INT_EP("ep5in", USB_DIR_IN | 5, 16);
2831 OMAP_BULK_EP("ep6in", USB_DIR_IN | 6);
2832 OMAP_BULK_EP("ep7out", USB_DIR_OUT | 7);
2833 OMAP_INT_EP("ep8in", USB_DIR_IN | 8, 16);
2835 case 3: /* mixed bulk/iso */
2836 OMAP_BULK_EP("ep1in", USB_DIR_IN | 1);
2837 OMAP_BULK_EP("ep2out", USB_DIR_OUT | 2);
2838 OMAP_INT_EP("ep3in", USB_DIR_IN | 3, 16);
2840 OMAP_BULK_EP("ep4in", USB_DIR_IN | 4);
2841 OMAP_BULK_EP("ep5out", USB_DIR_OUT | 5);
2842 OMAP_INT_EP("ep6in", USB_DIR_IN | 6, 16);
2844 OMAP_ISO_EP("ep7in", USB_DIR_IN | 7, 256);
2845 OMAP_ISO_EP("ep8out", USB_DIR_OUT | 8, 256);
2846 OMAP_INT_EP("ep9in", USB_DIR_IN | 9, 16);
2850 /* add more modes as needed */
2853 ERR("unsupported fifo_mode #%d\n", fifo_mode);
2856 UDC_SYSCON1_REG = UDC_CFG_LOCK|UDC_SELF_PWR;
2857 INFO("fifo mode %d, %d bytes not used\n", fifo_mode, 2048 - buf);
2861 static int __init omap_udc_probe(struct platform_device *pdev)
2863 int status = -ENODEV;
2865 struct otg_transceiver *xceiv = NULL;
2866 const char *type = NULL;
2867 struct omap_usb_config *config = pdev->dev.platform_data;
2869 struct clk *hhc_clk;
2871 /* NOTE: "knows" the order of the resources! */
2872 if (!request_mem_region(pdev->resource[0].start,
2873 pdev->resource[0].end - pdev->resource[0].start + 1,
2875 DBG("request_mem_region failed\n");
2879 if (cpu_is_omap16xx()) {
2880 dc_clk = clk_get(&pdev->dev, "usb_dc_ck");
2881 hhc_clk = clk_get(&pdev->dev, "usb_hhc_ck");
2882 BUG_ON(IS_ERR(dc_clk) || IS_ERR(hhc_clk));
2883 /* can't use omap_udc_enable_clock yet */
2885 clk_enable(hhc_clk);
2889 if (cpu_is_omap24xx()) {
2890 dc_clk = clk_get(&pdev->dev, "usb_fck");
2891 hhc_clk = clk_get(&pdev->dev, "usb_l4_ick");
2892 BUG_ON(IS_ERR(dc_clk) || IS_ERR(hhc_clk));
2893 /* can't use omap_udc_enable_clock yet */
2895 clk_enable(hhc_clk);
2899 INFO("OMAP UDC rev %d.%d%s\n",
2900 UDC_REV_REG >> 4, UDC_REV_REG & 0xf,
2901 config->otg ? ", Mini-AB" : "");
2903 /* use the mode given to us by board init code */
2904 if (cpu_is_omap15xx()) {
2908 if (machine_without_vbus_sense()) {
2909 /* just set up software VBUS detect, and then
2910 * later rig it so we always report VBUS.
2911 * FIXME without really sensing VBUS, we can't
2912 * know when to turn PULLUP_EN on/off; and that
2913 * means we always "need" the 48MHz clock.
2915 u32 tmp = FUNC_MUX_CTRL_0_REG;
2917 FUNC_MUX_CTRL_0_REG &= ~VBUS_CTRL_1510;
2918 tmp |= VBUS_MODE_1510;
2919 tmp &= ~VBUS_CTRL_1510;
2920 FUNC_MUX_CTRL_0_REG = tmp;
2923 /* The transceiver may package some GPIO logic or handle
2924 * loopback and/or transceiverless setup; if we find one,
2925 * use it. Except for OTG, we don't _need_ to talk to one;
2926 * but not having one probably means no VBUS detection.
2928 xceiv = otg_get_transceiver();
2930 type = xceiv->label;
2931 else if (config->otg) {
2932 DBG("OTG requires external transceiver!\n");
2938 if (cpu_is_omap24xx()) {
2939 /* this could be transceiverless in one of the
2940 * "we don't need to know" modes.
2947 case 0: /* POWERUP DEFAULT == 0 */
2951 if (!cpu_is_omap1710()) {
2952 type = "integrated";
2962 DBG("external transceiver not registered!\n");
2966 case 21: /* internal loopback */
2969 case 14: /* transceiverless */
2970 if (cpu_is_omap1710())
2980 ERR("unrecognized UDC HMC mode %d\n", hmc);
2985 INFO("hmc mode %d, %s transceiver\n", hmc, type);
2987 /* a "gadget" abstracts/virtualizes the controller */
2988 status = omap_udc_setup(pdev, xceiv);
2993 // "udc" is now valid
2994 pullup_disable(udc);
2995 #if defined(CONFIG_USB_OHCI_HCD) || defined(CONFIG_USB_OHCI_HCD_MODULE)
2996 udc->gadget.is_otg = (config->otg != 0);
2999 /* starting with omap1710 es2.0, clear toggle is a separate bit */
3000 if (UDC_REV_REG >= 0x61)
3001 udc->clr_halt = UDC_RESET_EP | UDC_CLRDATA_TOGGLE;
3003 udc->clr_halt = UDC_RESET_EP;
3005 /* USB general purpose IRQ: ep0, state changes, dma, etc */
3006 status = request_irq(pdev->resource[1].start, omap_udc_irq,
3007 IRQF_SAMPLE_RANDOM, driver_name, udc);
3009 ERR("can't get irq %d, err %d\n",
3010 (int) pdev->resource[1].start, status);
3014 /* USB "non-iso" IRQ (PIO for all but ep0) */
3015 status = request_irq(pdev->resource[2].start, omap_udc_pio_irq,
3016 IRQF_SAMPLE_RANDOM, "omap_udc pio", udc);
3018 ERR("can't get irq %d, err %d\n",
3019 (int) pdev->resource[2].start, status);
3023 status = request_irq(pdev->resource[3].start, omap_udc_iso_irq,
3024 IRQF_DISABLED, "omap_udc iso", udc);
3026 ERR("can't get irq %d, err %d\n",
3027 (int) pdev->resource[3].start, status);
3031 if (cpu_is_omap16xx()) {
3032 udc->dc_clk = dc_clk;
3033 udc->hhc_clk = hhc_clk;
3034 clk_disable(hhc_clk);
3035 clk_disable(dc_clk);
3038 if (cpu_is_omap24xx()) {
3039 udc->dc_clk = dc_clk;
3040 udc->hhc_clk = hhc_clk;
3041 /* FIXME OMAP2 don't release hhc & dc clock */
3043 clk_disable(hhc_clk);
3044 clk_disable(dc_clk);
3049 status = device_add(&udc->gadget.dev);
3052 /* If fail, fall through */
3055 free_irq(pdev->resource[2].start, udc);
3059 free_irq(pdev->resource[1].start, udc);
3067 put_device(xceiv->dev);
3069 if (cpu_is_omap16xx() || cpu_is_omap24xx()) {
3070 clk_disable(hhc_clk);
3071 clk_disable(dc_clk);
3076 release_mem_region(pdev->resource[0].start,
3077 pdev->resource[0].end - pdev->resource[0].start + 1);
3082 static int __exit omap_udc_remove(struct platform_device *pdev)
3084 DECLARE_COMPLETION_ONSTACK(done);
3093 pullup_disable(udc);
3094 if (udc->transceiver) {
3095 put_device(udc->transceiver->dev);
3096 udc->transceiver = NULL;
3098 UDC_SYSCON1_REG = 0;
3103 free_irq(pdev->resource[3].start, udc);
3105 free_irq(pdev->resource[2].start, udc);
3106 free_irq(pdev->resource[1].start, udc);
3109 if (udc->clk_requested)
3110 omap_udc_enable_clock(0);
3111 clk_put(udc->hhc_clk);
3112 clk_put(udc->dc_clk);
3115 release_mem_region(pdev->resource[0].start,
3116 pdev->resource[0].end - pdev->resource[0].start + 1);
3118 device_unregister(&udc->gadget.dev);
3119 wait_for_completion(&done);
3124 /* suspend/resume/wakeup from sysfs (echo > power/state) or when the
3125 * system is forced into deep sleep
3127 * REVISIT we should probably reject suspend requests when there's a host
3128 * session active, rather than disconnecting, at least on boards that can
3129 * report VBUS irqs (UDC_DEVSTAT_REG.UDC_ATT). And in any case, we need to
3130 * make host resumes and VBUS detection trigger OMAP wakeup events; that
3131 * may involve talking to an external transceiver (e.g. isp1301).
3134 static int omap_udc_suspend(struct platform_device *dev, pm_message_t message)
3138 devstat = UDC_DEVSTAT_REG;
3140 /* we're requesting 48 MHz clock if the pullup is enabled
3141 * (== we're attached to the host) and we're not suspended,
3142 * which would prevent entry to deep sleep...
3144 if ((devstat & UDC_ATT) != 0 && (devstat & UDC_SUS) == 0) {
3145 WARN("session active; suspend requires disconnect\n");
3146 omap_pullup(&udc->gadget, 0);
3149 udc->gadget.dev.power.power_state = PMSG_SUSPEND;
3150 udc->gadget.dev.parent->power.power_state = PMSG_SUSPEND;
3154 static int omap_udc_resume(struct platform_device *dev)
3156 DBG("resume + wakeup/SRP\n");
3157 omap_pullup(&udc->gadget, 1);
3159 /* maybe the host would enumerate us if we nudged it */
3161 return omap_wakeup(&udc->gadget);
3164 /*-------------------------------------------------------------------------*/
3166 static struct platform_driver udc_driver = {
3167 .probe = omap_udc_probe,
3168 .remove = __exit_p(omap_udc_remove),
3169 .suspend = omap_udc_suspend,
3170 .resume = omap_udc_resume,
3172 .owner = THIS_MODULE,
3173 .name = (char *) driver_name,
3177 static int __init udc_init(void)
3179 INFO("%s, version: " DRIVER_VERSION
3183 "%s\n", driver_desc,
3184 use_dma ? " (dma)" : "");
3185 return platform_driver_register(&udc_driver);
3187 module_init(udc_init);
3189 static void __exit udc_exit(void)
3191 platform_driver_unregister(&udc_driver);
3193 module_exit(udc_exit);
3195 MODULE_DESCRIPTION(DRIVER_DESC);
3196 MODULE_LICENSE("GPL");