1 // SPDX-License-Identifier: GPL-2.0+
3 * omap_udc.c -- for OMAP full speed udc; most chips support OTG.
5 * Copyright (C) 2004 Texas Instruments, Inc.
6 * Copyright (C) 2004-2005 David Brownell
14 #include <linux/module.h>
15 #include <linux/kernel.h>
16 #include <linux/ioport.h>
17 #include <linux/types.h>
18 #include <linux/errno.h>
19 #include <linux/delay.h>
20 #include <linux/slab.h>
21 #include <linux/timer.h>
22 #include <linux/list.h>
23 #include <linux/interrupt.h>
24 #include <linux/proc_fs.h>
26 #include <linux/moduleparam.h>
27 #include <linux/platform_device.h>
28 #include <linux/usb/ch9.h>
29 #include <linux/usb/gadget.h>
30 #include <linux/usb/otg.h>
31 #include <linux/dma-mapping.h>
32 #include <linux/clk.h>
33 #include <linux/err.h>
34 #include <linux/prefetch.h>
37 #include <asm/byteorder.h>
39 #include <linux/unaligned.h>
40 #include <asm/mach-types.h>
42 #include <linux/omap-dma.h>
43 #include <linux/platform_data/usb-omap1.h>
45 #include <linux/soc/ti/omap1-usb.h>
46 #include <linux/soc/ti/omap1-soc.h>
47 #include <linux/soc/ti/omap1-io.h>
53 /* bulk DMA seems to be behaving for both IN and OUT */
59 #define DRIVER_VERSION "4 October 2004"
61 #define OMAP_DMA_USB_W2FC_TX0 29
62 #define OMAP_DMA_USB_W2FC_RX0 26
65 * The OMAP UDC needs _very_ early endpoint setup: before enabling the
66 * D+ pullup to allow enumeration. That's too early for the gadget
67 * framework to use from usb_endpoint_enable(), which happens after
68 * enumeration as part of activating an interface. (But if we add an
69 * optional new "UDC not yet running" state to the gadget driver model,
70 * even just during driver binding, the endpoint autoconfig logic is the
71 * natural spot to manufacture new endpoints.)
73 * So instead of using endpoint enable calls to control the hardware setup,
74 * this driver defines a "fifo mode" parameter. It's used during driver
75 * initialization to choose among a set of pre-defined endpoint configs.
76 * See omap_udc_setup() for available modes, or to add others. That code
77 * lives in an init section, so use this driver as a module if you need
78 * to change the fifo mode after the kernel boots.
80 * Gadget drivers normally ignore endpoints they don't care about, and
81 * won't include them in configuration descriptors. That means only
82 * misbehaving hosts would even notice they exist.
85 static unsigned fifo_mode = 3;
87 static unsigned fifo_mode;
90 /* "modprobe omap_udc fifo_mode=42", or else as a kernel
91 * boot parameter "omap_udc:fifo_mode=42"
93 module_param(fifo_mode, uint, 0);
94 MODULE_PARM_DESC(fifo_mode, "endpoint configuration");
97 static bool use_dma = 1;
99 /* "modprobe omap_udc use_dma=y", or else as a kernel
100 * boot parameter "omap_udc:use_dma=y"
102 module_param(use_dma, bool, 0);
103 MODULE_PARM_DESC(use_dma, "enable/disable DMA");
106 /* save a bit of code */
108 #endif /* !USE_DMA */
111 static const char driver_name[] = "omap_udc";
113 /*-------------------------------------------------------------------------*/
115 /* there's a notion of "current endpoint" for modifying endpoint
116 * state, and PIO access to its FIFO.
119 static void use_ep(struct omap_ep *ep, u16 select)
121 u16 num = ep->bEndpointAddress & 0x0f;
123 if (ep->bEndpointAddress & USB_DIR_IN)
125 omap_writew(num | select, UDC_EP_NUM);
126 /* when select, MUST deselect later !! */
129 static inline void deselect_ep(void)
133 w = omap_readw(UDC_EP_NUM);
135 omap_writew(w, UDC_EP_NUM);
136 /* 6 wait states before TX will happen */
139 static void dma_channel_claim(struct omap_ep *ep, unsigned preferred);
141 /*-------------------------------------------------------------------------*/
143 static int omap_ep_enable(struct usb_ep *_ep,
144 const struct usb_endpoint_descriptor *desc)
146 struct omap_ep *ep = container_of(_ep, struct omap_ep, ep);
147 struct omap_udc *udc;
151 /* catch various bogus parameters */
153 || desc->bDescriptorType != USB_DT_ENDPOINT
154 || ep->bEndpointAddress != desc->bEndpointAddress
155 || ep->maxpacket < usb_endpoint_maxp(desc)) {
156 DBG("%s, bad ep or descriptor\n", __func__);
159 maxp = usb_endpoint_maxp(desc);
160 if ((desc->bmAttributes == USB_ENDPOINT_XFER_BULK
161 && maxp != ep->maxpacket)
162 || usb_endpoint_maxp(desc) > ep->maxpacket
163 || !desc->wMaxPacketSize) {
164 DBG("%s, bad %s maxpacket\n", __func__, _ep->name);
169 if ((desc->bmAttributes == USB_ENDPOINT_XFER_ISOC
170 && desc->bInterval != 1)) {
171 /* hardware wants period = 1; USB allows 2^(Interval-1) */
172 DBG("%s, unsupported ISO period %dms\n", _ep->name,
173 1 << (desc->bInterval - 1));
177 if (desc->bmAttributes == USB_ENDPOINT_XFER_ISOC) {
178 DBG("%s, ISO nyet\n", _ep->name);
183 /* xfer types must match, except that interrupt ~= bulk */
184 if (ep->bmAttributes != desc->bmAttributes
185 && ep->bmAttributes != USB_ENDPOINT_XFER_BULK
186 && desc->bmAttributes != USB_ENDPOINT_XFER_INT) {
187 DBG("%s, %s type mismatch\n", __func__, _ep->name);
192 if (!udc->driver || udc->gadget.speed == USB_SPEED_UNKNOWN) {
193 DBG("%s, bogus device state\n", __func__);
197 spin_lock_irqsave(&udc->lock, flags);
202 ep->ep.maxpacket = maxp;
204 /* set endpoint to initial state */
208 use_ep(ep, UDC_EP_SEL);
209 omap_writew(udc->clr_halt, UDC_CTRL);
213 if (ep->bmAttributes == USB_ENDPOINT_XFER_ISOC)
214 list_add(&ep->iso, &udc->iso);
216 /* maybe assign a DMA channel to this endpoint */
217 if (use_dma && desc->bmAttributes == USB_ENDPOINT_XFER_BULK)
218 /* FIXME ISO can dma, but prefers first channel */
219 dma_channel_claim(ep, 0);
221 /* PIO OUT may RX packets */
222 if (desc->bmAttributes != USB_ENDPOINT_XFER_ISOC
224 && !(ep->bEndpointAddress & USB_DIR_IN)) {
225 omap_writew(UDC_SET_FIFO_EN, UDC_CTRL);
226 ep->ackwait = 1 + ep->double_buf;
229 spin_unlock_irqrestore(&udc->lock, flags);
230 VDBG("%s enabled\n", _ep->name);
234 static void nuke(struct omap_ep *, int status);
236 static int omap_ep_disable(struct usb_ep *_ep)
238 struct omap_ep *ep = container_of(_ep, struct omap_ep, ep);
241 if (!_ep || !ep->ep.desc) {
242 DBG("%s, %s not enabled\n", __func__,
243 _ep ? ep->ep.name : NULL);
247 spin_lock_irqsave(&ep->udc->lock, flags);
249 nuke(ep, -ESHUTDOWN);
250 ep->ep.maxpacket = ep->maxpacket;
252 omap_writew(UDC_SET_HALT, UDC_CTRL);
253 list_del_init(&ep->iso);
254 del_timer(&ep->timer);
256 spin_unlock_irqrestore(&ep->udc->lock, flags);
258 VDBG("%s disabled\n", _ep->name);
262 /*-------------------------------------------------------------------------*/
264 static struct usb_request *
265 omap_alloc_request(struct usb_ep *ep, gfp_t gfp_flags)
267 struct omap_req *req;
269 req = kzalloc(sizeof(*req), gfp_flags);
273 INIT_LIST_HEAD(&req->queue);
279 omap_free_request(struct usb_ep *ep, struct usb_request *_req)
281 struct omap_req *req = container_of(_req, struct omap_req, req);
286 /*-------------------------------------------------------------------------*/
289 done(struct omap_ep *ep, struct omap_req *req, int status)
291 struct omap_udc *udc = ep->udc;
292 unsigned stopped = ep->stopped;
294 list_del_init(&req->queue);
296 if (req->req.status == -EINPROGRESS)
297 req->req.status = status;
299 status = req->req.status;
301 if (use_dma && ep->has_dma)
302 usb_gadget_unmap_request(&udc->gadget, &req->req,
303 (ep->bEndpointAddress & USB_DIR_IN));
306 if (status && status != -ESHUTDOWN)
308 VDBG("complete %s req %p stat %d len %u/%u\n",
309 ep->ep.name, &req->req, status,
310 req->req.actual, req->req.length);
312 /* don't modify queue heads during completion callback */
314 spin_unlock(&ep->udc->lock);
315 usb_gadget_giveback_request(&ep->ep, &req->req);
316 spin_lock(&ep->udc->lock);
317 ep->stopped = stopped;
320 /*-------------------------------------------------------------------------*/
322 #define UDC_FIFO_FULL (UDC_NON_ISO_FIFO_FULL | UDC_ISO_FIFO_FULL)
323 #define UDC_FIFO_UNWRITABLE (UDC_EP_HALTED | UDC_FIFO_FULL)
325 #define FIFO_EMPTY (UDC_NON_ISO_FIFO_EMPTY | UDC_ISO_FIFO_EMPTY)
326 #define FIFO_UNREADABLE (UDC_EP_HALTED | FIFO_EMPTY)
329 write_packet(u8 *buf, struct omap_req *req, unsigned max)
334 len = min(req->req.length - req->req.actual, max);
335 req->req.actual += len;
338 if (likely((((int)buf) & 1) == 0)) {
341 omap_writew(*wp++, UDC_DATA);
347 omap_writeb(*buf++, UDC_DATA);
351 /* FIXME change r/w fifo calling convention */
354 /* return: 0 = still running, 1 = completed, negative = errno */
355 static int write_fifo(struct omap_ep *ep, struct omap_req *req)
362 buf = req->req.buf + req->req.actual;
365 /* PIO-IN isn't double buffered except for iso */
366 ep_stat = omap_readw(UDC_STAT_FLG);
367 if (ep_stat & UDC_FIFO_UNWRITABLE)
370 count = ep->ep.maxpacket;
371 count = write_packet(buf, req, count);
372 omap_writew(UDC_SET_FIFO_EN, UDC_CTRL);
375 /* last packet is often short (sometimes a zlp) */
376 if (count != ep->ep.maxpacket)
378 else if (req->req.length == req->req.actual
384 /* NOTE: requests complete when all IN data is in a
385 * FIFO (or sometimes later, if a zlp was needed).
386 * Use usb_ep_fifo_status() where needed.
394 read_packet(u8 *buf, struct omap_req *req, unsigned avail)
399 len = min(req->req.length - req->req.actual, avail);
400 req->req.actual += len;
403 if (likely((((int)buf) & 1) == 0)) {
406 *wp++ = omap_readw(UDC_DATA);
412 *buf++ = omap_readb(UDC_DATA);
416 /* return: 0 = still running, 1 = queue empty, negative = errno */
417 static int read_fifo(struct omap_ep *ep, struct omap_req *req)
420 unsigned count, avail;
423 buf = req->req.buf + req->req.actual;
427 u16 ep_stat = omap_readw(UDC_STAT_FLG);
430 if (ep_stat & FIFO_EMPTY) {
435 if (ep_stat & UDC_EP_HALTED)
438 if (ep_stat & UDC_FIFO_FULL)
439 avail = ep->ep.maxpacket;
441 avail = omap_readw(UDC_RXFSTAT);
442 ep->fnf = ep->double_buf;
444 count = read_packet(buf, req, avail);
446 /* partial packet reads may not be errors */
447 if (count < ep->ep.maxpacket) {
449 /* overflowed this request? flush extra data */
450 if (count != avail) {
451 req->req.status = -EOVERFLOW;
454 omap_readw(UDC_DATA);
456 } else if (req->req.length == req->req.actual)
461 if (!ep->bEndpointAddress)
470 /*-------------------------------------------------------------------------*/
472 static u16 dma_src_len(struct omap_ep *ep, dma_addr_t start)
476 /* IN-DMA needs this on fault/cancel paths, so 15xx misreports
477 * the last transfer's bytecount by more than a FIFO's worth.
479 if (cpu_is_omap15xx())
482 end = omap_get_dma_src_pos(ep->lch);
483 if (end == ep->dma_counter)
486 end |= start & (0xffff << 16);
492 static u16 dma_dest_len(struct omap_ep *ep, dma_addr_t start)
496 end = omap_get_dma_dst_pos(ep->lch);
497 if (end == ep->dma_counter)
500 end |= start & (0xffff << 16);
501 if (cpu_is_omap15xx())
509 /* Each USB transfer request using DMA maps to one or more DMA transfers.
510 * When DMA completion isn't request completion, the UDC continues with
511 * the next DMA transfer for that USB transfer.
514 static void next_in_dma(struct omap_ep *ep, struct omap_req *req)
517 unsigned length = req->req.length - req->req.actual;
518 const int sync_mode = cpu_is_omap15xx()
519 ? OMAP_DMA_SYNC_FRAME
520 : OMAP_DMA_SYNC_ELEMENT;
523 /* measure length in either bytes or packets */
524 if ((cpu_is_omap16xx() && length <= UDC_TXN_TSC)
525 || (cpu_is_omap15xx() && length < ep->maxpacket)) {
526 txdma_ctrl = UDC_TXN_EOT | length;
527 omap_set_dma_transfer_params(ep->lch, OMAP_DMA_DATA_TYPE_S8,
528 length, 1, sync_mode, dma_trigger, 0);
530 length = min(length / ep->maxpacket,
531 (unsigned) UDC_TXN_TSC + 1);
533 omap_set_dma_transfer_params(ep->lch, OMAP_DMA_DATA_TYPE_S16,
534 ep->ep.maxpacket >> 1, length, sync_mode,
536 length *= ep->maxpacket;
538 omap_set_dma_src_params(ep->lch, OMAP_DMA_PORT_EMIFF,
539 OMAP_DMA_AMODE_POST_INC, req->req.dma + req->req.actual,
542 omap_start_dma(ep->lch);
543 ep->dma_counter = omap_get_dma_src_pos(ep->lch);
544 w = omap_readw(UDC_DMA_IRQ_EN);
545 w |= UDC_TX_DONE_IE(ep->dma_channel);
546 omap_writew(w, UDC_DMA_IRQ_EN);
547 omap_writew(UDC_TXN_START | txdma_ctrl, UDC_TXDMA(ep->dma_channel));
548 req->dma_bytes = length;
551 static void finish_in_dma(struct omap_ep *ep, struct omap_req *req, int status)
556 req->req.actual += req->dma_bytes;
558 /* return if this request needs to send data or zlp */
559 if (req->req.actual < req->req.length)
562 && req->dma_bytes != 0
563 && (req->req.actual % ep->maxpacket) == 0)
566 req->req.actual += dma_src_len(ep, req->req.dma
570 omap_stop_dma(ep->lch);
571 w = omap_readw(UDC_DMA_IRQ_EN);
572 w &= ~UDC_TX_DONE_IE(ep->dma_channel);
573 omap_writew(w, UDC_DMA_IRQ_EN);
574 done(ep, req, status);
577 static void next_out_dma(struct omap_ep *ep, struct omap_req *req)
579 unsigned int packets = req->req.length - req->req.actual;
583 /* set up this DMA transfer, enable the fifo, start */
584 packets /= ep->ep.maxpacket;
585 packets = min_t(unsigned int, packets, UDC_RXN_TC + 1);
586 req->dma_bytes = packets * ep->ep.maxpacket;
587 omap_set_dma_transfer_params(ep->lch, OMAP_DMA_DATA_TYPE_S16,
588 ep->ep.maxpacket >> 1, packets,
589 OMAP_DMA_SYNC_ELEMENT,
591 omap_set_dma_dest_params(ep->lch, OMAP_DMA_PORT_EMIFF,
592 OMAP_DMA_AMODE_POST_INC, req->req.dma + req->req.actual,
594 ep->dma_counter = omap_get_dma_dst_pos(ep->lch);
596 omap_writew(UDC_RXN_STOP | (packets - 1), UDC_RXDMA(ep->dma_channel));
597 w = omap_readw(UDC_DMA_IRQ_EN);
598 w |= UDC_RX_EOT_IE(ep->dma_channel);
599 omap_writew(w, UDC_DMA_IRQ_EN);
600 omap_writew(ep->bEndpointAddress & 0xf, UDC_EP_NUM);
601 omap_writew(UDC_SET_FIFO_EN, UDC_CTRL);
603 omap_start_dma(ep->lch);
607 finish_out_dma(struct omap_ep *ep, struct omap_req *req, int status, int one)
612 ep->dma_counter = (u16) (req->req.dma + req->req.actual);
613 count = dma_dest_len(ep, req->req.dma + req->req.actual);
614 count += req->req.actual;
617 if (count <= req->req.length)
618 req->req.actual = count;
620 if (count != req->dma_bytes || status)
621 omap_stop_dma(ep->lch);
623 /* if this wasn't short, request may need another transfer */
624 else if (req->req.actual < req->req.length)
628 w = omap_readw(UDC_DMA_IRQ_EN);
629 w &= ~UDC_RX_EOT_IE(ep->dma_channel);
630 omap_writew(w, UDC_DMA_IRQ_EN);
631 done(ep, req, status);
634 static void dma_irq(struct omap_udc *udc, u16 irq_src)
636 u16 dman_stat = omap_readw(UDC_DMAN_STAT);
638 struct omap_req *req;
640 /* IN dma: tx to host */
641 if (irq_src & UDC_TXN_DONE) {
642 ep = &udc->ep[16 + UDC_DMA_TX_SRC(dman_stat)];
644 /* can see TXN_DONE after dma abort */
645 if (!list_empty(&ep->queue)) {
646 req = container_of(ep->queue.next,
647 struct omap_req, queue);
648 finish_in_dma(ep, req, 0);
650 omap_writew(UDC_TXN_DONE, UDC_IRQ_SRC);
652 if (!list_empty(&ep->queue)) {
653 req = container_of(ep->queue.next,
654 struct omap_req, queue);
655 next_in_dma(ep, req);
659 /* OUT dma: rx from host */
660 if (irq_src & UDC_RXN_EOT) {
661 ep = &udc->ep[UDC_DMA_RX_SRC(dman_stat)];
663 /* can see RXN_EOT after dma abort */
664 if (!list_empty(&ep->queue)) {
665 req = container_of(ep->queue.next,
666 struct omap_req, queue);
667 finish_out_dma(ep, req, 0, dman_stat & UDC_DMA_RX_SB);
669 omap_writew(UDC_RXN_EOT, UDC_IRQ_SRC);
671 if (!list_empty(&ep->queue)) {
672 req = container_of(ep->queue.next,
673 struct omap_req, queue);
674 next_out_dma(ep, req);
678 if (irq_src & UDC_RXN_CNT) {
679 ep = &udc->ep[UDC_DMA_RX_SRC(dman_stat)];
681 /* omap15xx does this unasked... */
682 VDBG("%s, RX_CNT irq?\n", ep->ep.name);
683 omap_writew(UDC_RXN_CNT, UDC_IRQ_SRC);
687 static void dma_error(int lch, u16 ch_status, void *data)
689 struct omap_ep *ep = data;
691 /* if ch_status & OMAP_DMA_DROP_IRQ ... */
692 /* if ch_status & OMAP1_DMA_TOUT_IRQ ... */
693 ERR("%s dma error, lch %d status %02x\n", ep->ep.name, lch, ch_status);
695 /* complete current transfer ... */
698 static void dma_channel_claim(struct omap_ep *ep, unsigned channel)
701 int status, restart, is_in;
704 is_in = ep->bEndpointAddress & USB_DIR_IN;
706 reg = omap_readw(UDC_TXDMA_CFG);
708 reg = omap_readw(UDC_RXDMA_CFG);
709 reg |= UDC_DMA_REQ; /* "pulse" activated */
713 if (channel == 0 || channel > 3) {
714 if ((reg & 0x0f00) == 0)
716 else if ((reg & 0x00f0) == 0)
718 else if ((reg & 0x000f) == 0) /* preferred for ISO */
725 reg |= (0x0f & ep->bEndpointAddress) << (4 * (channel - 1));
726 ep->dma_channel = channel;
729 dma_channel = OMAP_DMA_USB_W2FC_TX0 - 1 + channel;
730 status = omap_request_dma(dma_channel,
731 ep->ep.name, dma_error, ep, &ep->lch);
733 omap_writew(reg, UDC_TXDMA_CFG);
735 omap_set_dma_src_burst_mode(ep->lch,
736 OMAP_DMA_DATA_BURST_4);
737 omap_set_dma_src_data_pack(ep->lch, 1);
739 omap_set_dma_dest_params(ep->lch,
741 OMAP_DMA_AMODE_CONSTANT,
746 dma_channel = OMAP_DMA_USB_W2FC_RX0 - 1 + channel;
747 status = omap_request_dma(dma_channel,
748 ep->ep.name, dma_error, ep, &ep->lch);
750 omap_writew(reg, UDC_RXDMA_CFG);
752 omap_set_dma_src_params(ep->lch,
754 OMAP_DMA_AMODE_CONSTANT,
758 omap_set_dma_dest_burst_mode(ep->lch,
759 OMAP_DMA_DATA_BURST_4);
760 omap_set_dma_dest_data_pack(ep->lch, 1);
767 omap_disable_dma_irq(ep->lch, OMAP_DMA_BLOCK_IRQ);
769 /* channel type P: hw synch (fifo) */
770 if (!cpu_is_omap15xx())
771 omap_set_dma_channel_mode(ep->lch, OMAP_DMA_LCH_P);
775 /* restart any queue, even if the claim failed */
776 restart = !ep->stopped && !list_empty(&ep->queue);
779 DBG("%s no dma channel: %d%s\n", ep->ep.name, status,
780 restart ? " (restart)" : "");
782 DBG("%s claimed %cxdma%d lch %d%s\n", ep->ep.name,
784 ep->dma_channel - 1, ep->lch,
785 restart ? " (restart)" : "");
788 struct omap_req *req;
789 req = container_of(ep->queue.next, struct omap_req, queue);
791 (is_in ? next_in_dma : next_out_dma)(ep, req);
793 use_ep(ep, UDC_EP_SEL);
794 (is_in ? write_fifo : read_fifo)(ep, req);
797 omap_writew(UDC_SET_FIFO_EN, UDC_CTRL);
798 ep->ackwait = 1 + ep->double_buf;
800 /* IN: 6 wait states before it'll tx */
805 static void dma_channel_release(struct omap_ep *ep)
807 int shift = 4 * (ep->dma_channel - 1);
808 u16 mask = 0x0f << shift;
809 struct omap_req *req;
812 /* abort any active usb transfer request */
813 if (!list_empty(&ep->queue))
814 req = container_of(ep->queue.next, struct omap_req, queue);
818 active = omap_get_dma_active_status(ep->lch);
820 DBG("%s release %s %cxdma%d %p\n", ep->ep.name,
821 active ? "active" : "idle",
822 (ep->bEndpointAddress & USB_DIR_IN) ? 't' : 'r',
823 ep->dma_channel - 1, req);
825 /* NOTE: re-setting RX_REQ/TX_REQ because of a chip bug (before
826 * OMAP 1710 ES2.0) where reading the DMA_CFG can clear them.
829 /* wait till current packet DMA finishes, and fifo empties */
830 if (ep->bEndpointAddress & USB_DIR_IN) {
831 omap_writew((omap_readw(UDC_TXDMA_CFG) & ~mask) | UDC_DMA_REQ,
835 finish_in_dma(ep, req, -ECONNRESET);
837 /* clear FIFO; hosts probably won't empty it */
838 use_ep(ep, UDC_EP_SEL);
839 omap_writew(UDC_CLR_EP, UDC_CTRL);
842 while (omap_readw(UDC_TXDMA_CFG) & mask)
845 omap_writew((omap_readw(UDC_RXDMA_CFG) & ~mask) | UDC_DMA_REQ,
848 /* dma empties the fifo */
849 while (omap_readw(UDC_RXDMA_CFG) & mask)
852 finish_out_dma(ep, req, -ECONNRESET, 0);
854 omap_free_dma(ep->lch);
857 /* has_dma still set, till endpoint is fully quiesced */
861 /*-------------------------------------------------------------------------*/
864 omap_ep_queue(struct usb_ep *_ep, struct usb_request *_req, gfp_t gfp_flags)
866 struct omap_ep *ep = container_of(_ep, struct omap_ep, ep);
867 struct omap_req *req = container_of(_req, struct omap_req, req);
868 struct omap_udc *udc;
872 /* catch various bogus parameters */
873 if (!_req || !req->req.complete || !req->req.buf
874 || !list_empty(&req->queue)) {
875 DBG("%s, bad params\n", __func__);
878 if (!_ep || (!ep->ep.desc && ep->bEndpointAddress)) {
879 DBG("%s, bad ep\n", __func__);
882 if (ep->bmAttributes == USB_ENDPOINT_XFER_ISOC) {
883 if (req->req.length > ep->ep.maxpacket)
888 /* this isn't bogus, but OMAP DMA isn't the only hardware to
889 * have a hard time with partial packet reads... reject it.
893 && ep->bEndpointAddress != 0
894 && (ep->bEndpointAddress & USB_DIR_IN) == 0
895 && (req->req.length % ep->ep.maxpacket) != 0) {
896 DBG("%s, no partial packet OUT reads\n", __func__);
901 if (!udc->driver || udc->gadget.speed == USB_SPEED_UNKNOWN)
904 if (use_dma && ep->has_dma)
905 usb_gadget_map_request(&udc->gadget, &req->req,
906 (ep->bEndpointAddress & USB_DIR_IN));
908 VDBG("%s queue req %p, len %d buf %p\n",
909 ep->ep.name, _req, _req->length, _req->buf);
911 spin_lock_irqsave(&udc->lock, flags);
913 req->req.status = -EINPROGRESS;
916 /* maybe kickstart non-iso i/o queues */
920 w = omap_readw(UDC_IRQ_EN);
922 omap_writew(w, UDC_IRQ_EN);
923 } else if (list_empty(&ep->queue) && !ep->stopped && !ep->ackwait) {
926 if (ep->bEndpointAddress == 0) {
927 if (!udc->ep0_pending || !list_empty(&ep->queue)) {
928 spin_unlock_irqrestore(&udc->lock, flags);
932 /* empty DATA stage? */
934 if (!req->req.length) {
936 /* chip became CONFIGURED or ADDRESSED
937 * earlier; drivers may already have queued
938 * requests to non-control endpoints
940 if (udc->ep0_set_config) {
941 u16 irq_en = omap_readw(UDC_IRQ_EN);
943 irq_en |= UDC_DS_CHG_IE | UDC_EP0_IE;
944 if (!udc->ep0_reset_config)
945 irq_en |= UDC_EPN_RX_IE
947 omap_writew(irq_en, UDC_IRQ_EN);
950 /* STATUS for zero length DATA stages is
951 * always an IN ... even for IN transfers,
952 * a weird case which seem to stall OMAP.
954 omap_writew(UDC_EP_SEL | UDC_EP_DIR,
956 omap_writew(UDC_CLR_EP, UDC_CTRL);
957 omap_writew(UDC_SET_FIFO_EN, UDC_CTRL);
958 omap_writew(UDC_EP_DIR, UDC_EP_NUM);
961 udc->ep0_pending = 0;
965 /* non-empty DATA stage */
967 omap_writew(UDC_EP_SEL | UDC_EP_DIR,
972 omap_writew(UDC_EP_SEL, UDC_EP_NUM);
975 is_in = ep->bEndpointAddress & USB_DIR_IN;
977 use_ep(ep, UDC_EP_SEL);
978 /* if ISO: SOF IRQs must be enabled/disabled! */
982 (is_in ? next_in_dma : next_out_dma)(ep, req);
984 if ((is_in ? write_fifo : read_fifo)(ep, req) == 1)
988 omap_writew(UDC_SET_FIFO_EN, UDC_CTRL);
989 ep->ackwait = 1 + ep->double_buf;
991 /* IN: 6 wait states before it'll tx */
996 /* irq handler advances the queue */
998 list_add_tail(&req->queue, &ep->queue);
999 spin_unlock_irqrestore(&udc->lock, flags);
1004 static int omap_ep_dequeue(struct usb_ep *_ep, struct usb_request *_req)
1006 struct omap_ep *ep = container_of(_ep, struct omap_ep, ep);
1007 struct omap_req *req = NULL, *iter;
1008 unsigned long flags;
1013 spin_lock_irqsave(&ep->udc->lock, flags);
1015 /* make sure it's actually queued on this endpoint */
1016 list_for_each_entry(iter, &ep->queue, queue) {
1017 if (&iter->req != _req)
1023 spin_unlock_irqrestore(&ep->udc->lock, flags);
1027 if (use_dma && ep->dma_channel && ep->queue.next == &req->queue) {
1028 int channel = ep->dma_channel;
1030 /* releasing the channel cancels the request,
1031 * reclaiming the channel restarts the queue
1033 dma_channel_release(ep);
1034 dma_channel_claim(ep, channel);
1036 done(ep, req, -ECONNRESET);
1037 spin_unlock_irqrestore(&ep->udc->lock, flags);
1041 /*-------------------------------------------------------------------------*/
1043 static int omap_ep_set_halt(struct usb_ep *_ep, int value)
1045 struct omap_ep *ep = container_of(_ep, struct omap_ep, ep);
1046 unsigned long flags;
1047 int status = -EOPNOTSUPP;
1049 spin_lock_irqsave(&ep->udc->lock, flags);
1051 /* just use protocol stalls for ep0; real halts are annoying */
1052 if (ep->bEndpointAddress == 0) {
1053 if (!ep->udc->ep0_pending)
1056 if (ep->udc->ep0_set_config) {
1057 WARNING("error changing config?\n");
1058 omap_writew(UDC_CLR_CFG, UDC_SYSCON2);
1060 omap_writew(UDC_STALL_CMD, UDC_SYSCON2);
1061 ep->udc->ep0_pending = 0;
1066 /* otherwise, all active non-ISO endpoints can halt */
1067 } else if (ep->bmAttributes != USB_ENDPOINT_XFER_ISOC && ep->ep.desc) {
1069 /* IN endpoints must already be idle */
1070 if ((ep->bEndpointAddress & USB_DIR_IN)
1071 && !list_empty(&ep->queue)) {
1079 if (use_dma && ep->dma_channel
1080 && !list_empty(&ep->queue)) {
1081 channel = ep->dma_channel;
1082 dma_channel_release(ep);
1086 use_ep(ep, UDC_EP_SEL);
1087 if (omap_readw(UDC_STAT_FLG) & UDC_NON_ISO_FIFO_EMPTY) {
1088 omap_writew(UDC_SET_HALT, UDC_CTRL);
1095 dma_channel_claim(ep, channel);
1098 omap_writew(ep->udc->clr_halt, UDC_CTRL);
1100 if (!(ep->bEndpointAddress & USB_DIR_IN)) {
1101 omap_writew(UDC_SET_FIFO_EN, UDC_CTRL);
1102 ep->ackwait = 1 + ep->double_buf;
1107 VDBG("%s %s halt stat %d\n", ep->ep.name,
1108 value ? "set" : "clear", status);
1110 spin_unlock_irqrestore(&ep->udc->lock, flags);
1114 static const struct usb_ep_ops omap_ep_ops = {
1115 .enable = omap_ep_enable,
1116 .disable = omap_ep_disable,
1118 .alloc_request = omap_alloc_request,
1119 .free_request = omap_free_request,
1121 .queue = omap_ep_queue,
1122 .dequeue = omap_ep_dequeue,
1124 .set_halt = omap_ep_set_halt,
1125 /* fifo_status ... report bytes in fifo */
1126 /* fifo_flush ... flush fifo */
1129 /*-------------------------------------------------------------------------*/
1131 static int omap_get_frame(struct usb_gadget *gadget)
1133 u16 sof = omap_readw(UDC_SOF);
1134 return (sof & UDC_TS_OK) ? (sof & UDC_TS) : -EL2NSYNC;
1137 static int omap_wakeup(struct usb_gadget *gadget)
1139 struct omap_udc *udc;
1140 unsigned long flags;
1141 int retval = -EHOSTUNREACH;
1143 udc = container_of(gadget, struct omap_udc, gadget);
1145 spin_lock_irqsave(&udc->lock, flags);
1146 if (udc->devstat & UDC_SUS) {
1147 /* NOTE: OTG spec erratum says that OTG devices may
1148 * issue wakeups without host enable.
1150 if (udc->devstat & (UDC_B_HNP_ENABLE|UDC_R_WK_OK)) {
1151 DBG("remote wakeup...\n");
1152 omap_writew(UDC_RMT_WKP, UDC_SYSCON2);
1156 /* NOTE: non-OTG systems may use SRP TOO... */
1157 } else if (!(udc->devstat & UDC_ATT)) {
1158 if (!IS_ERR_OR_NULL(udc->transceiver))
1159 retval = otg_start_srp(udc->transceiver->otg);
1161 spin_unlock_irqrestore(&udc->lock, flags);
1167 omap_set_selfpowered(struct usb_gadget *gadget, int is_selfpowered)
1169 struct omap_udc *udc;
1170 unsigned long flags;
1173 gadget->is_selfpowered = (is_selfpowered != 0);
1174 udc = container_of(gadget, struct omap_udc, gadget);
1175 spin_lock_irqsave(&udc->lock, flags);
1176 syscon1 = omap_readw(UDC_SYSCON1);
1178 syscon1 |= UDC_SELF_PWR;
1180 syscon1 &= ~UDC_SELF_PWR;
1181 omap_writew(syscon1, UDC_SYSCON1);
1182 spin_unlock_irqrestore(&udc->lock, flags);
1187 static int can_pullup(struct omap_udc *udc)
1189 return udc->driver && udc->softconnect && udc->vbus_active;
1192 static void pullup_enable(struct omap_udc *udc)
1196 w = omap_readw(UDC_SYSCON1);
1198 omap_writew(w, UDC_SYSCON1);
1199 if (!gadget_is_otg(&udc->gadget) && !cpu_is_omap15xx()) {
1202 l = omap_readl(OTG_CTRL);
1204 omap_writel(l, OTG_CTRL);
1206 omap_writew(UDC_DS_CHG_IE, UDC_IRQ_EN);
1209 static void pullup_disable(struct omap_udc *udc)
1213 if (!gadget_is_otg(&udc->gadget) && !cpu_is_omap15xx()) {
1216 l = omap_readl(OTG_CTRL);
1218 omap_writel(l, OTG_CTRL);
1220 omap_writew(UDC_DS_CHG_IE, UDC_IRQ_EN);
1221 w = omap_readw(UDC_SYSCON1);
1222 w &= ~UDC_PULLUP_EN;
1223 omap_writew(w, UDC_SYSCON1);
1226 static struct omap_udc *udc;
1228 static void omap_udc_enable_clock(int enable)
1230 if (udc == NULL || udc->dc_clk == NULL || udc->hhc_clk == NULL)
1234 clk_enable(udc->dc_clk);
1235 clk_enable(udc->hhc_clk);
1238 clk_disable(udc->hhc_clk);
1239 clk_disable(udc->dc_clk);
1244 * Called by whatever detects VBUS sessions: external transceiver
1245 * driver, or maybe GPIO0 VBUS IRQ. May request 48 MHz clock.
1247 static int omap_vbus_session(struct usb_gadget *gadget, int is_active)
1249 struct omap_udc *udc;
1250 unsigned long flags;
1253 udc = container_of(gadget, struct omap_udc, gadget);
1254 spin_lock_irqsave(&udc->lock, flags);
1255 VDBG("VBUS %s\n", is_active ? "on" : "off");
1256 udc->vbus_active = (is_active != 0);
1257 if (cpu_is_omap15xx()) {
1258 /* "software" detect, ignored if !VBUS_MODE_1510 */
1259 l = omap_readl(FUNC_MUX_CTRL_0);
1261 l |= VBUS_CTRL_1510;
1263 l &= ~VBUS_CTRL_1510;
1264 omap_writel(l, FUNC_MUX_CTRL_0);
1266 if (udc->dc_clk != NULL && is_active) {
1267 if (!udc->clk_requested) {
1268 omap_udc_enable_clock(1);
1269 udc->clk_requested = 1;
1272 if (can_pullup(udc))
1275 pullup_disable(udc);
1276 if (udc->dc_clk != NULL && !is_active) {
1277 if (udc->clk_requested) {
1278 omap_udc_enable_clock(0);
1279 udc->clk_requested = 0;
1282 spin_unlock_irqrestore(&udc->lock, flags);
1286 static int omap_vbus_draw(struct usb_gadget *gadget, unsigned mA)
1288 struct omap_udc *udc;
1290 udc = container_of(gadget, struct omap_udc, gadget);
1291 if (!IS_ERR_OR_NULL(udc->transceiver))
1292 return usb_phy_set_power(udc->transceiver, mA);
1296 static int omap_pullup(struct usb_gadget *gadget, int is_on)
1298 struct omap_udc *udc;
1299 unsigned long flags;
1301 udc = container_of(gadget, struct omap_udc, gadget);
1302 spin_lock_irqsave(&udc->lock, flags);
1303 udc->softconnect = (is_on != 0);
1304 if (can_pullup(udc))
1307 pullup_disable(udc);
1308 spin_unlock_irqrestore(&udc->lock, flags);
1312 static int omap_udc_start(struct usb_gadget *g,
1313 struct usb_gadget_driver *driver);
1314 static int omap_udc_stop(struct usb_gadget *g);
1316 static const struct usb_gadget_ops omap_gadget_ops = {
1317 .get_frame = omap_get_frame,
1318 .wakeup = omap_wakeup,
1319 .set_selfpowered = omap_set_selfpowered,
1320 .vbus_session = omap_vbus_session,
1321 .vbus_draw = omap_vbus_draw,
1322 .pullup = omap_pullup,
1323 .udc_start = omap_udc_start,
1324 .udc_stop = omap_udc_stop,
1327 /*-------------------------------------------------------------------------*/
1329 /* dequeue ALL requests; caller holds udc->lock */
1330 static void nuke(struct omap_ep *ep, int status)
1332 struct omap_req *req;
1336 if (use_dma && ep->dma_channel)
1337 dma_channel_release(ep);
1340 omap_writew(UDC_CLR_EP, UDC_CTRL);
1341 if (ep->bEndpointAddress && ep->bmAttributes != USB_ENDPOINT_XFER_ISOC)
1342 omap_writew(UDC_SET_HALT, UDC_CTRL);
1344 while (!list_empty(&ep->queue)) {
1345 req = list_entry(ep->queue.next, struct omap_req, queue);
1346 done(ep, req, status);
1350 /* caller holds udc->lock */
1351 static void udc_quiesce(struct omap_udc *udc)
1355 udc->gadget.speed = USB_SPEED_UNKNOWN;
1356 nuke(&udc->ep[0], -ESHUTDOWN);
1357 list_for_each_entry(ep, &udc->gadget.ep_list, ep.ep_list)
1358 nuke(ep, -ESHUTDOWN);
1361 /*-------------------------------------------------------------------------*/
1363 static void update_otg(struct omap_udc *udc)
1367 if (!gadget_is_otg(&udc->gadget))
1370 if (omap_readl(OTG_CTRL) & OTG_ID)
1371 devstat = omap_readw(UDC_DEVSTAT);
1375 udc->gadget.b_hnp_enable = !!(devstat & UDC_B_HNP_ENABLE);
1376 udc->gadget.a_hnp_support = !!(devstat & UDC_A_HNP_SUPPORT);
1377 udc->gadget.a_alt_hnp_support = !!(devstat & UDC_A_ALT_HNP_SUPPORT);
1379 /* Enable HNP early, avoiding races on suspend irq path.
1380 * ASSUMES OTG state machine B_BUS_REQ input is true.
1382 if (udc->gadget.b_hnp_enable) {
1385 l = omap_readl(OTG_CTRL);
1386 l |= OTG_B_HNPEN | OTG_B_BUSREQ;
1388 omap_writel(l, OTG_CTRL);
1392 static void ep0_irq(struct omap_udc *udc, u16 irq_src)
1394 struct omap_ep *ep0 = &udc->ep[0];
1395 struct omap_req *req = NULL;
1399 /* Clear any pending requests and then scrub any rx/tx state
1400 * before starting to handle the SETUP request.
1402 if (irq_src & UDC_SETUP) {
1403 u16 ack = irq_src & (UDC_EP0_TX|UDC_EP0_RX);
1407 omap_writew(ack, UDC_IRQ_SRC);
1408 irq_src = UDC_SETUP;
1412 /* IN/OUT packets mean we're in the DATA or STATUS stage.
1413 * This driver uses only uses protocol stalls (ep0 never halts),
1414 * and if we got this far the gadget driver already had a
1415 * chance to stall. Tries to be forgiving of host oddities.
1417 * NOTE: the last chance gadget drivers have to stall control
1418 * requests is during their request completion callback.
1420 if (!list_empty(&ep0->queue))
1421 req = container_of(ep0->queue.next, struct omap_req, queue);
1423 /* IN == TX to host */
1424 if (irq_src & UDC_EP0_TX) {
1427 omap_writew(UDC_EP0_TX, UDC_IRQ_SRC);
1428 omap_writew(UDC_EP_SEL|UDC_EP_DIR, UDC_EP_NUM);
1429 stat = omap_readw(UDC_STAT_FLG);
1430 if (stat & UDC_ACK) {
1432 /* write next IN packet from response,
1433 * or set up the status stage.
1436 stat = write_fifo(ep0, req);
1437 omap_writew(UDC_EP_DIR, UDC_EP_NUM);
1438 if (!req && udc->ep0_pending) {
1439 omap_writew(UDC_EP_SEL, UDC_EP_NUM);
1440 omap_writew(UDC_CLR_EP, UDC_CTRL);
1441 omap_writew(UDC_SET_FIFO_EN, UDC_CTRL);
1442 omap_writew(0, UDC_EP_NUM);
1443 udc->ep0_pending = 0;
1444 } /* else: 6 wait states before it'll tx */
1446 /* ack status stage of OUT transfer */
1447 omap_writew(UDC_EP_DIR, UDC_EP_NUM);
1452 } else if (stat & UDC_STALL) {
1453 omap_writew(UDC_CLR_HALT, UDC_CTRL);
1454 omap_writew(UDC_EP_DIR, UDC_EP_NUM);
1456 omap_writew(UDC_EP_DIR, UDC_EP_NUM);
1460 /* OUT == RX from host */
1461 if (irq_src & UDC_EP0_RX) {
1464 omap_writew(UDC_EP0_RX, UDC_IRQ_SRC);
1465 omap_writew(UDC_EP_SEL, UDC_EP_NUM);
1466 stat = omap_readw(UDC_STAT_FLG);
1467 if (stat & UDC_ACK) {
1470 /* read next OUT packet of request, maybe
1471 * reactivating the fifo; stall on errors.
1473 stat = read_fifo(ep0, req);
1474 if (!req || stat < 0) {
1475 omap_writew(UDC_STALL_CMD, UDC_SYSCON2);
1476 udc->ep0_pending = 0;
1478 } else if (stat == 0)
1479 omap_writew(UDC_SET_FIFO_EN, UDC_CTRL);
1480 omap_writew(0, UDC_EP_NUM);
1482 /* activate status stage */
1485 /* that may have STALLed ep0... */
1486 omap_writew(UDC_EP_SEL | UDC_EP_DIR,
1488 omap_writew(UDC_CLR_EP, UDC_CTRL);
1489 omap_writew(UDC_SET_FIFO_EN, UDC_CTRL);
1490 omap_writew(UDC_EP_DIR, UDC_EP_NUM);
1491 udc->ep0_pending = 0;
1494 /* ack status stage of IN transfer */
1495 omap_writew(0, UDC_EP_NUM);
1499 } else if (stat & UDC_STALL) {
1500 omap_writew(UDC_CLR_HALT, UDC_CTRL);
1501 omap_writew(0, UDC_EP_NUM);
1503 omap_writew(0, UDC_EP_NUM);
1507 /* SETUP starts all control transfers */
1508 if (irq_src & UDC_SETUP) {
1511 struct usb_ctrlrequest r;
1513 int status = -EINVAL;
1516 /* read the (latest) SETUP message */
1518 omap_writew(UDC_SETUP_SEL, UDC_EP_NUM);
1519 /* two bytes at a time */
1520 u.word[0] = omap_readw(UDC_DATA);
1521 u.word[1] = omap_readw(UDC_DATA);
1522 u.word[2] = omap_readw(UDC_DATA);
1523 u.word[3] = omap_readw(UDC_DATA);
1524 omap_writew(0, UDC_EP_NUM);
1525 } while (omap_readw(UDC_IRQ_SRC) & UDC_SETUP);
1527 #define w_value le16_to_cpu(u.r.wValue)
1528 #define w_index le16_to_cpu(u.r.wIndex)
1529 #define w_length le16_to_cpu(u.r.wLength)
1531 /* Delegate almost all control requests to the gadget driver,
1532 * except for a handful of ch9 status/feature requests that
1533 * hardware doesn't autodecode _and_ the gadget API hides.
1535 udc->ep0_in = (u.r.bRequestType & USB_DIR_IN) != 0;
1536 udc->ep0_set_config = 0;
1537 udc->ep0_pending = 1;
1540 switch (u.r.bRequest) {
1541 case USB_REQ_SET_CONFIGURATION:
1542 /* udc needs to know when ep != 0 is valid */
1543 if (u.r.bRequestType != USB_RECIP_DEVICE)
1547 udc->ep0_set_config = 1;
1548 udc->ep0_reset_config = (w_value == 0);
1549 VDBG("set config %d\n", w_value);
1551 /* update udc NOW since gadget driver may start
1552 * queueing requests immediately; clear config
1553 * later if it fails the request.
1555 if (udc->ep0_reset_config)
1556 omap_writew(UDC_CLR_CFG, UDC_SYSCON2);
1558 omap_writew(UDC_DEV_CFG, UDC_SYSCON2);
1561 case USB_REQ_CLEAR_FEATURE:
1562 /* clear endpoint halt */
1563 if (u.r.bRequestType != USB_RECIP_ENDPOINT)
1565 if (w_value != USB_ENDPOINT_HALT
1568 ep = &udc->ep[w_index & 0xf];
1570 if (w_index & USB_DIR_IN)
1572 if (ep->bmAttributes == USB_ENDPOINT_XFER_ISOC
1576 omap_writew(udc->clr_halt, UDC_CTRL);
1578 if (!(ep->bEndpointAddress & USB_DIR_IN)) {
1579 omap_writew(UDC_SET_FIFO_EN, UDC_CTRL);
1580 ep->ackwait = 1 + ep->double_buf;
1582 /* NOTE: assumes the host behaves sanely,
1583 * only clearing real halts. Else we may
1584 * need to kill pending transfers and then
1585 * restart the queue... very messy for DMA!
1588 VDBG("%s halt cleared by host\n", ep->name);
1589 goto ep0out_status_stage;
1590 case USB_REQ_SET_FEATURE:
1591 /* set endpoint halt */
1592 if (u.r.bRequestType != USB_RECIP_ENDPOINT)
1594 if (w_value != USB_ENDPOINT_HALT
1597 ep = &udc->ep[w_index & 0xf];
1598 if (w_index & USB_DIR_IN)
1600 if (ep->bmAttributes == USB_ENDPOINT_XFER_ISOC
1601 || ep == ep0 || !ep->ep.desc)
1603 if (use_dma && ep->has_dma) {
1604 /* this has rude side-effects (aborts) and
1605 * can't really work if DMA-IN is active
1607 DBG("%s host set_halt, NYET\n", ep->name);
1611 /* can't halt if fifo isn't empty... */
1612 omap_writew(UDC_CLR_EP, UDC_CTRL);
1613 omap_writew(UDC_SET_HALT, UDC_CTRL);
1614 VDBG("%s halted by host\n", ep->name);
1615 ep0out_status_stage:
1617 omap_writew(UDC_EP_SEL|UDC_EP_DIR, UDC_EP_NUM);
1618 omap_writew(UDC_CLR_EP, UDC_CTRL);
1619 omap_writew(UDC_SET_FIFO_EN, UDC_CTRL);
1620 omap_writew(UDC_EP_DIR, UDC_EP_NUM);
1621 udc->ep0_pending = 0;
1623 case USB_REQ_GET_STATUS:
1624 /* USB_ENDPOINT_HALT status? */
1625 if (u.r.bRequestType != (USB_DIR_IN|USB_RECIP_ENDPOINT))
1628 /* ep0 never stalls */
1629 if (!(w_index & 0xf))
1632 /* only active endpoints count */
1633 ep = &udc->ep[w_index & 0xf];
1634 if (w_index & USB_DIR_IN)
1639 /* iso never stalls */
1640 if (ep->bmAttributes == USB_ENDPOINT_XFER_ISOC)
1643 /* FIXME don't assume non-halted endpoints!! */
1644 ERR("%s status, can't report\n", ep->ep.name);
1648 /* return interface status. if we were pedantic,
1649 * we'd detect non-existent interfaces, and stall.
1651 if (u.r.bRequestType
1652 != (USB_DIR_IN|USB_RECIP_INTERFACE))
1656 /* return two zero bytes */
1657 omap_writew(UDC_EP_SEL|UDC_EP_DIR, UDC_EP_NUM);
1658 omap_writew(0, UDC_DATA);
1659 omap_writew(UDC_SET_FIFO_EN, UDC_CTRL);
1660 omap_writew(UDC_EP_DIR, UDC_EP_NUM);
1662 VDBG("GET_STATUS, interface %d\n", w_index);
1663 /* next, status stage */
1667 /* activate the ep0out fifo right away */
1668 if (!udc->ep0_in && w_length) {
1669 omap_writew(0, UDC_EP_NUM);
1670 omap_writew(UDC_SET_FIFO_EN, UDC_CTRL);
1673 /* gadget drivers see class/vendor specific requests,
1674 * {SET,GET}_{INTERFACE,DESCRIPTOR,CONFIGURATION},
1677 VDBG("SETUP %02x.%02x v%04x i%04x l%04x\n",
1678 u.r.bRequestType, u.r.bRequest,
1679 w_value, w_index, w_length);
1685 /* The gadget driver may return an error here,
1686 * causing an immediate protocol stall.
1688 * Else it must issue a response, either queueing a
1689 * response buffer for the DATA stage, or halting ep0
1690 * (causing a protocol stall, not a real halt). A
1691 * zero length buffer means no DATA stage.
1693 * It's fine to issue that response after the setup()
1694 * call returns, and this IRQ was handled.
1697 spin_unlock(&udc->lock);
1698 status = udc->driver->setup(&udc->gadget, &u.r);
1699 spin_lock(&udc->lock);
1705 VDBG("req %02x.%02x protocol STALL; stat %d\n",
1706 u.r.bRequestType, u.r.bRequest, status);
1707 if (udc->ep0_set_config) {
1708 if (udc->ep0_reset_config)
1709 WARNING("error resetting config?\n");
1711 omap_writew(UDC_CLR_CFG, UDC_SYSCON2);
1713 omap_writew(UDC_STALL_CMD, UDC_SYSCON2);
1714 udc->ep0_pending = 0;
1719 /*-------------------------------------------------------------------------*/
1721 #define OTG_FLAGS (UDC_B_HNP_ENABLE|UDC_A_HNP_SUPPORT|UDC_A_ALT_HNP_SUPPORT)
1723 static void devstate_irq(struct omap_udc *udc, u16 irq_src)
1725 u16 devstat, change;
1727 devstat = omap_readw(UDC_DEVSTAT);
1728 change = devstat ^ udc->devstat;
1729 udc->devstat = devstat;
1731 if (change & (UDC_USB_RESET|UDC_ATT)) {
1734 if (change & UDC_ATT) {
1735 /* driver for any external transceiver will
1736 * have called omap_vbus_session() already
1738 if (devstat & UDC_ATT) {
1739 udc->gadget.speed = USB_SPEED_FULL;
1741 if (IS_ERR_OR_NULL(udc->transceiver))
1743 /* if (driver->connect) call it */
1744 } else if (udc->gadget.speed != USB_SPEED_UNKNOWN) {
1745 udc->gadget.speed = USB_SPEED_UNKNOWN;
1746 if (IS_ERR_OR_NULL(udc->transceiver))
1747 pullup_disable(udc);
1748 DBG("disconnect, gadget %s\n",
1749 udc->driver->driver.name);
1750 if (udc->driver->disconnect) {
1751 spin_unlock(&udc->lock);
1752 udc->driver->disconnect(&udc->gadget);
1753 spin_lock(&udc->lock);
1759 if (change & UDC_USB_RESET) {
1760 if (devstat & UDC_USB_RESET) {
1763 udc->gadget.speed = USB_SPEED_FULL;
1764 INFO("USB reset done, gadget %s\n",
1765 udc->driver->driver.name);
1766 /* ep0 traffic is legal from now on */
1767 omap_writew(UDC_DS_CHG_IE | UDC_EP0_IE,
1770 change &= ~UDC_USB_RESET;
1773 if (change & UDC_SUS) {
1774 if (udc->gadget.speed != USB_SPEED_UNKNOWN) {
1775 /* FIXME tell isp1301 to suspend/resume (?) */
1776 if (devstat & UDC_SUS) {
1779 /* HNP could be under way already */
1780 if (udc->gadget.speed == USB_SPEED_FULL
1781 && udc->driver->suspend) {
1782 spin_unlock(&udc->lock);
1783 udc->driver->suspend(&udc->gadget);
1784 spin_lock(&udc->lock);
1786 if (!IS_ERR_OR_NULL(udc->transceiver))
1787 usb_phy_set_suspend(
1788 udc->transceiver, 1);
1791 if (!IS_ERR_OR_NULL(udc->transceiver))
1792 usb_phy_set_suspend(
1793 udc->transceiver, 0);
1794 if (udc->gadget.speed == USB_SPEED_FULL
1795 && udc->driver->resume) {
1796 spin_unlock(&udc->lock);
1797 udc->driver->resume(&udc->gadget);
1798 spin_lock(&udc->lock);
1804 if (!cpu_is_omap15xx() && (change & OTG_FLAGS)) {
1806 change &= ~OTG_FLAGS;
1809 change &= ~(UDC_CFG|UDC_DEF|UDC_ADD);
1811 VDBG("devstat %03x, ignore change %03x\n",
1814 omap_writew(UDC_DS_CHG, UDC_IRQ_SRC);
1817 static irqreturn_t omap_udc_irq(int irq, void *_udc)
1819 struct omap_udc *udc = _udc;
1821 irqreturn_t status = IRQ_NONE;
1822 unsigned long flags;
1824 spin_lock_irqsave(&udc->lock, flags);
1825 irq_src = omap_readw(UDC_IRQ_SRC);
1827 /* Device state change (usb ch9 stuff) */
1828 if (irq_src & UDC_DS_CHG) {
1829 devstate_irq(_udc, irq_src);
1830 status = IRQ_HANDLED;
1831 irq_src &= ~UDC_DS_CHG;
1834 /* EP0 control transfers */
1835 if (irq_src & (UDC_EP0_RX|UDC_SETUP|UDC_EP0_TX)) {
1836 ep0_irq(_udc, irq_src);
1837 status = IRQ_HANDLED;
1838 irq_src &= ~(UDC_EP0_RX|UDC_SETUP|UDC_EP0_TX);
1841 /* DMA transfer completion */
1842 if (use_dma && (irq_src & (UDC_TXN_DONE|UDC_RXN_CNT|UDC_RXN_EOT))) {
1843 dma_irq(_udc, irq_src);
1844 status = IRQ_HANDLED;
1845 irq_src &= ~(UDC_TXN_DONE|UDC_RXN_CNT|UDC_RXN_EOT);
1848 irq_src &= ~(UDC_IRQ_SOF | UDC_EPN_TX|UDC_EPN_RX);
1850 DBG("udc_irq, unhandled %03x\n", irq_src);
1851 spin_unlock_irqrestore(&udc->lock, flags);
1856 /* workaround for seemingly-lost IRQs for RX ACKs... */
1857 #define PIO_OUT_TIMEOUT (jiffies + HZ/3)
1858 #define HALF_FULL(f) (!((f)&(UDC_NON_ISO_FIFO_FULL|UDC_NON_ISO_FIFO_EMPTY)))
1860 static void pio_out_timer(struct timer_list *t)
1862 struct omap_ep *ep = from_timer(ep, t, timer);
1863 unsigned long flags;
1866 spin_lock_irqsave(&ep->udc->lock, flags);
1867 if (!list_empty(&ep->queue) && ep->ackwait) {
1868 use_ep(ep, UDC_EP_SEL);
1869 stat_flg = omap_readw(UDC_STAT_FLG);
1871 if ((stat_flg & UDC_ACK) && (!(stat_flg & UDC_FIFO_EN)
1872 || (ep->double_buf && HALF_FULL(stat_flg)))) {
1873 struct omap_req *req;
1875 VDBG("%s: lose, %04x\n", ep->ep.name, stat_flg);
1876 req = container_of(ep->queue.next,
1877 struct omap_req, queue);
1878 (void) read_fifo(ep, req);
1879 omap_writew(ep->bEndpointAddress, UDC_EP_NUM);
1880 omap_writew(UDC_SET_FIFO_EN, UDC_CTRL);
1881 ep->ackwait = 1 + ep->double_buf;
1885 mod_timer(&ep->timer, PIO_OUT_TIMEOUT);
1886 spin_unlock_irqrestore(&ep->udc->lock, flags);
1889 static irqreturn_t omap_udc_pio_irq(int irq, void *_dev)
1891 u16 epn_stat, irq_src;
1892 irqreturn_t status = IRQ_NONE;
1895 struct omap_udc *udc = _dev;
1896 struct omap_req *req;
1897 unsigned long flags;
1899 spin_lock_irqsave(&udc->lock, flags);
1900 epn_stat = omap_readw(UDC_EPN_STAT);
1901 irq_src = omap_readw(UDC_IRQ_SRC);
1903 /* handle OUT first, to avoid some wasteful NAKs */
1904 if (irq_src & UDC_EPN_RX) {
1905 epnum = (epn_stat >> 8) & 0x0f;
1906 omap_writew(UDC_EPN_RX, UDC_IRQ_SRC);
1907 status = IRQ_HANDLED;
1908 ep = &udc->ep[epnum];
1911 omap_writew(epnum | UDC_EP_SEL, UDC_EP_NUM);
1913 if (omap_readw(UDC_STAT_FLG) & UDC_ACK) {
1915 if (!list_empty(&ep->queue)) {
1917 req = container_of(ep->queue.next,
1918 struct omap_req, queue);
1919 stat = read_fifo(ep, req);
1920 if (!ep->double_buf)
1924 /* min 6 clock delay before clearing EP_SEL ... */
1925 epn_stat = omap_readw(UDC_EPN_STAT);
1926 epn_stat = omap_readw(UDC_EPN_STAT);
1927 omap_writew(epnum, UDC_EP_NUM);
1929 /* enabling fifo _after_ clearing ACK, contrary to docs,
1930 * reduces lossage; timer still needed though (sigh).
1933 omap_writew(UDC_SET_FIFO_EN, UDC_CTRL);
1934 ep->ackwait = 1 + ep->double_buf;
1936 mod_timer(&ep->timer, PIO_OUT_TIMEOUT);
1939 /* then IN transfers */
1940 else if (irq_src & UDC_EPN_TX) {
1941 epnum = epn_stat & 0x0f;
1942 omap_writew(UDC_EPN_TX, UDC_IRQ_SRC);
1943 status = IRQ_HANDLED;
1944 ep = &udc->ep[16 + epnum];
1947 omap_writew(epnum | UDC_EP_DIR | UDC_EP_SEL, UDC_EP_NUM);
1948 if (omap_readw(UDC_STAT_FLG) & UDC_ACK) {
1950 if (!list_empty(&ep->queue)) {
1951 req = container_of(ep->queue.next,
1952 struct omap_req, queue);
1953 (void) write_fifo(ep, req);
1956 /* min 6 clock delay before clearing EP_SEL ... */
1957 epn_stat = omap_readw(UDC_EPN_STAT);
1958 epn_stat = omap_readw(UDC_EPN_STAT);
1959 omap_writew(epnum | UDC_EP_DIR, UDC_EP_NUM);
1960 /* then 6 clocks before it'd tx */
1963 spin_unlock_irqrestore(&udc->lock, flags);
1968 static irqreturn_t omap_udc_iso_irq(int irq, void *_dev)
1970 struct omap_udc *udc = _dev;
1973 unsigned long flags;
1975 spin_lock_irqsave(&udc->lock, flags);
1977 /* handle all non-DMA ISO transfers */
1978 list_for_each_entry(ep, &udc->iso, iso) {
1980 struct omap_req *req;
1982 if (ep->has_dma || list_empty(&ep->queue))
1984 req = list_entry(ep->queue.next, struct omap_req, queue);
1986 use_ep(ep, UDC_EP_SEL);
1987 stat = omap_readw(UDC_STAT_FLG);
1989 /* NOTE: like the other controller drivers, this isn't
1990 * currently reporting lost or damaged frames.
1992 if (ep->bEndpointAddress & USB_DIR_IN) {
1993 if (stat & UDC_MISS_IN)
1994 /* done(ep, req, -EPROTO) */;
1996 write_fifo(ep, req);
2000 if (stat & UDC_NO_RXPACKET)
2001 status = -EREMOTEIO;
2002 else if (stat & UDC_ISO_ERR)
2004 else if (stat & UDC_DATA_FLUSH)
2008 /* done(ep, req, status) */;
2013 /* 6 wait states before next EP */
2016 if (!list_empty(&ep->queue))
2022 w = omap_readw(UDC_IRQ_EN);
2024 omap_writew(w, UDC_IRQ_EN);
2026 omap_writew(UDC_IRQ_SOF, UDC_IRQ_SRC);
2028 spin_unlock_irqrestore(&udc->lock, flags);
2033 /*-------------------------------------------------------------------------*/
2035 static inline int machine_without_vbus_sense(void)
2037 return machine_is_omap_osk() || machine_is_omap_palmte() ||
2041 static int omap_udc_start(struct usb_gadget *g,
2042 struct usb_gadget_driver *driver)
2046 unsigned long flags;
2049 spin_lock_irqsave(&udc->lock, flags);
2051 list_for_each_entry(ep, &udc->gadget.ep_list, ep.ep_list) {
2053 if (ep->bmAttributes == USB_ENDPOINT_XFER_ISOC)
2056 omap_writew(UDC_SET_HALT, UDC_CTRL);
2058 udc->ep0_pending = 0;
2059 udc->ep[0].irqs = 0;
2060 udc->softconnect = 1;
2062 /* hook up the driver */
2063 udc->driver = driver;
2064 spin_unlock_irqrestore(&udc->lock, flags);
2066 if (udc->dc_clk != NULL)
2067 omap_udc_enable_clock(1);
2069 omap_writew(UDC_IRQ_SRC_MASK, UDC_IRQ_SRC);
2071 /* connect to bus through transceiver */
2072 if (!IS_ERR_OR_NULL(udc->transceiver)) {
2073 status = otg_set_peripheral(udc->transceiver->otg,
2076 ERR("can't bind to transceiver\n");
2082 if (can_pullup(udc))
2085 pullup_disable(udc);
2088 /* boards that don't have VBUS sensing can't autogate 48MHz;
2089 * can't enter deep sleep while a gadget driver is active.
2091 if (machine_without_vbus_sense())
2092 omap_vbus_session(&udc->gadget, 1);
2095 if (udc->dc_clk != NULL)
2096 omap_udc_enable_clock(0);
2101 static int omap_udc_stop(struct usb_gadget *g)
2103 unsigned long flags;
2105 if (udc->dc_clk != NULL)
2106 omap_udc_enable_clock(1);
2108 if (machine_without_vbus_sense())
2109 omap_vbus_session(&udc->gadget, 0);
2111 if (!IS_ERR_OR_NULL(udc->transceiver))
2112 (void) otg_set_peripheral(udc->transceiver->otg, NULL);
2114 pullup_disable(udc);
2116 spin_lock_irqsave(&udc->lock, flags);
2118 spin_unlock_irqrestore(&udc->lock, flags);
2122 if (udc->dc_clk != NULL)
2123 omap_udc_enable_clock(0);
2128 /*-------------------------------------------------------------------------*/
2130 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
2132 #include <linux/seq_file.h>
2134 static const char proc_filename[] = "driver/udc";
2136 #define FOURBITS "%s%s%s%s"
2137 #define EIGHTBITS "%s%s%s%s%s%s%s%s"
2139 static void proc_ep_show(struct seq_file *s, struct omap_ep *ep)
2142 struct omap_req *req;
2147 if (use_dma && ep->has_dma)
2148 snprintf(buf, sizeof buf, "(%cxdma%d lch%d) ",
2149 (ep->bEndpointAddress & USB_DIR_IN) ? 't' : 'r',
2150 ep->dma_channel - 1, ep->lch);
2154 stat_flg = omap_readw(UDC_STAT_FLG);
2156 "\n%s %s%s%sirqs %ld stat %04x " EIGHTBITS FOURBITS "%s\n",
2158 ep->double_buf ? "dbuf " : "",
2160 switch (ep->ackwait) {
2175 (stat_flg & UDC_NO_RXPACKET) ? "no_rxpacket " : "",
2176 (stat_flg & UDC_MISS_IN) ? "miss_in " : "",
2177 (stat_flg & UDC_DATA_FLUSH) ? "data_flush " : "",
2178 (stat_flg & UDC_ISO_ERR) ? "iso_err " : "",
2179 (stat_flg & UDC_ISO_FIFO_EMPTY) ? "iso_fifo_empty " : "",
2180 (stat_flg & UDC_ISO_FIFO_FULL) ? "iso_fifo_full " : "",
2181 (stat_flg & UDC_EP_HALTED) ? "HALT " : "",
2182 (stat_flg & UDC_STALL) ? "STALL " : "",
2183 (stat_flg & UDC_NAK) ? "NAK " : "",
2184 (stat_flg & UDC_ACK) ? "ACK " : "",
2185 (stat_flg & UDC_FIFO_EN) ? "fifo_en " : "",
2186 (stat_flg & UDC_NON_ISO_FIFO_EMPTY) ? "fifo_empty " : "",
2187 (stat_flg & UDC_NON_ISO_FIFO_FULL) ? "fifo_full " : "");
2189 if (list_empty(&ep->queue))
2190 seq_printf(s, "\t(queue empty)\n");
2192 list_for_each_entry(req, &ep->queue, queue) {
2193 unsigned length = req->req.actual;
2195 if (use_dma && buf[0]) {
2196 length += ((ep->bEndpointAddress & USB_DIR_IN)
2197 ? dma_src_len : dma_dest_len)
2198 (ep, req->req.dma + length);
2201 seq_printf(s, "\treq %p len %d/%d buf %p\n",
2203 req->req.length, req->req.buf);
2207 static char *trx_mode(unsigned m, int enabled)
2211 return enabled ? "*6wire" : "unused";
2223 static int proc_otg_show(struct seq_file *s)
2227 char *ctrl_name = "(UNKNOWN)";
2229 tmp = omap_readl(OTG_REV);
2230 ctrl_name = "transceiver_ctrl";
2231 trans = omap_readw(USB_TRANSCEIVER_CTRL);
2232 seq_printf(s, "\nOTG rev %d.%d, %s %05x\n",
2233 tmp >> 4, tmp & 0xf, ctrl_name, trans);
2234 tmp = omap_readw(OTG_SYSCON_1);
2235 seq_printf(s, "otg_syscon1 %08x usb2 %s, usb1 %s, usb0 %s,"
2237 trx_mode(USB2_TRX_MODE(tmp), trans & CONF_USB2_UNI_R),
2238 trx_mode(USB1_TRX_MODE(tmp), trans & CONF_USB1_UNI_R),
2239 (USB0_TRX_MODE(tmp) == 0 && !cpu_is_omap1710())
2241 : trx_mode(USB0_TRX_MODE(tmp), 1),
2242 (tmp & OTG_IDLE_EN) ? " !otg" : "",
2243 (tmp & HST_IDLE_EN) ? " !host" : "",
2244 (tmp & DEV_IDLE_EN) ? " !dev" : "",
2245 (tmp & OTG_RESET_DONE) ? " reset_done" : " reset_active");
2246 tmp = omap_readl(OTG_SYSCON_2);
2247 seq_printf(s, "otg_syscon2 %08x%s" EIGHTBITS
2248 " b_ase_brst=%d hmc=%d\n", tmp,
2249 (tmp & OTG_EN) ? " otg_en" : "",
2250 (tmp & USBX_SYNCHRO) ? " synchro" : "",
2251 /* much more SRP stuff */
2252 (tmp & SRP_DATA) ? " srp_data" : "",
2253 (tmp & SRP_VBUS) ? " srp_vbus" : "",
2254 (tmp & OTG_PADEN) ? " otg_paden" : "",
2255 (tmp & HMC_PADEN) ? " hmc_paden" : "",
2256 (tmp & UHOST_EN) ? " uhost_en" : "",
2257 (tmp & HMC_TLLSPEED) ? " tllspeed" : "",
2258 (tmp & HMC_TLLATTACH) ? " tllattach" : "",
2261 tmp = omap_readl(OTG_CTRL);
2262 seq_printf(s, "otg_ctrl %06x" EIGHTBITS EIGHTBITS "%s\n", tmp,
2263 (tmp & OTG_ASESSVLD) ? " asess" : "",
2264 (tmp & OTG_BSESSEND) ? " bsess_end" : "",
2265 (tmp & OTG_BSESSVLD) ? " bsess" : "",
2266 (tmp & OTG_VBUSVLD) ? " vbus" : "",
2267 (tmp & OTG_ID) ? " id" : "",
2268 (tmp & OTG_DRIVER_SEL) ? " DEVICE" : " HOST",
2269 (tmp & OTG_A_SETB_HNPEN) ? " a_setb_hnpen" : "",
2270 (tmp & OTG_A_BUSREQ) ? " a_bus" : "",
2271 (tmp & OTG_B_HNPEN) ? " b_hnpen" : "",
2272 (tmp & OTG_B_BUSREQ) ? " b_bus" : "",
2273 (tmp & OTG_BUSDROP) ? " busdrop" : "",
2274 (tmp & OTG_PULLDOWN) ? " down" : "",
2275 (tmp & OTG_PULLUP) ? " up" : "",
2276 (tmp & OTG_DRV_VBUS) ? " drv" : "",
2277 (tmp & OTG_PD_VBUS) ? " pd_vb" : "",
2278 (tmp & OTG_PU_VBUS) ? " pu_vb" : "",
2279 (tmp & OTG_PU_ID) ? " pu_id" : ""
2281 tmp = omap_readw(OTG_IRQ_EN);
2282 seq_printf(s, "otg_irq_en %04x" "\n", tmp);
2283 tmp = omap_readw(OTG_IRQ_SRC);
2284 seq_printf(s, "otg_irq_src %04x" "\n", tmp);
2285 tmp = omap_readw(OTG_OUTCTRL);
2286 seq_printf(s, "otg_outctrl %04x" "\n", tmp);
2287 tmp = omap_readw(OTG_TEST);
2288 seq_printf(s, "otg_test %04x" "\n", tmp);
2292 static int proc_udc_show(struct seq_file *s, void *_)
2296 unsigned long flags;
2298 spin_lock_irqsave(&udc->lock, flags);
2300 seq_printf(s, "OMAP UDC driver, version: " DRIVER_VERSION
2304 "%s\n", use_dma ? " (dma)" : "");
2306 tmp = omap_readw(UDC_REV) & 0xff;
2308 "UDC rev %d.%d, fifo mode %d, gadget %s\n"
2309 "hmc %d, transceiver %s\n",
2310 tmp >> 4, tmp & 0xf,
2312 udc->driver ? udc->driver->driver.name : "(none)",
2315 ? udc->transceiver->label
2316 : (cpu_is_omap1710()
2317 ? "external" : "(none)"));
2318 seq_printf(s, "ULPD control %04x req %04x status %04x\n",
2319 omap_readw(ULPD_CLOCK_CTRL),
2320 omap_readw(ULPD_SOFT_REQ),
2321 omap_readw(ULPD_STATUS_REQ));
2323 /* OTG controller registers */
2324 if (!cpu_is_omap15xx())
2327 tmp = omap_readw(UDC_SYSCON1);
2328 seq_printf(s, "\nsyscon1 %04x" EIGHTBITS "\n", tmp,
2329 (tmp & UDC_CFG_LOCK) ? " cfg_lock" : "",
2330 (tmp & UDC_DATA_ENDIAN) ? " data_endian" : "",
2331 (tmp & UDC_DMA_ENDIAN) ? " dma_endian" : "",
2332 (tmp & UDC_NAK_EN) ? " nak" : "",
2333 (tmp & UDC_AUTODECODE_DIS) ? " autodecode_dis" : "",
2334 (tmp & UDC_SELF_PWR) ? " self_pwr" : "",
2335 (tmp & UDC_SOFF_DIS) ? " soff_dis" : "",
2336 (tmp & UDC_PULLUP_EN) ? " PULLUP" : "");
2337 /* syscon2 is write-only */
2339 /* UDC controller registers */
2340 if (!(tmp & UDC_PULLUP_EN)) {
2341 seq_printf(s, "(suspended)\n");
2342 spin_unlock_irqrestore(&udc->lock, flags);
2346 tmp = omap_readw(UDC_DEVSTAT);
2347 seq_printf(s, "devstat %04x" EIGHTBITS "%s%s\n", tmp,
2348 (tmp & UDC_B_HNP_ENABLE) ? " b_hnp" : "",
2349 (tmp & UDC_A_HNP_SUPPORT) ? " a_hnp" : "",
2350 (tmp & UDC_A_ALT_HNP_SUPPORT) ? " a_alt_hnp" : "",
2351 (tmp & UDC_R_WK_OK) ? " r_wk_ok" : "",
2352 (tmp & UDC_USB_RESET) ? " usb_reset" : "",
2353 (tmp & UDC_SUS) ? " SUS" : "",
2354 (tmp & UDC_CFG) ? " CFG" : "",
2355 (tmp & UDC_ADD) ? " ADD" : "",
2356 (tmp & UDC_DEF) ? " DEF" : "",
2357 (tmp & UDC_ATT) ? " ATT" : "");
2358 seq_printf(s, "sof %04x\n", omap_readw(UDC_SOF));
2359 tmp = omap_readw(UDC_IRQ_EN);
2360 seq_printf(s, "irq_en %04x" FOURBITS "%s\n", tmp,
2361 (tmp & UDC_SOF_IE) ? " sof" : "",
2362 (tmp & UDC_EPN_RX_IE) ? " epn_rx" : "",
2363 (tmp & UDC_EPN_TX_IE) ? " epn_tx" : "",
2364 (tmp & UDC_DS_CHG_IE) ? " ds_chg" : "",
2365 (tmp & UDC_EP0_IE) ? " ep0" : "");
2366 tmp = omap_readw(UDC_IRQ_SRC);
2367 seq_printf(s, "irq_src %04x" EIGHTBITS "%s%s\n", tmp,
2368 (tmp & UDC_TXN_DONE) ? " txn_done" : "",
2369 (tmp & UDC_RXN_CNT) ? " rxn_cnt" : "",
2370 (tmp & UDC_RXN_EOT) ? " rxn_eot" : "",
2371 (tmp & UDC_IRQ_SOF) ? " sof" : "",
2372 (tmp & UDC_EPN_RX) ? " epn_rx" : "",
2373 (tmp & UDC_EPN_TX) ? " epn_tx" : "",
2374 (tmp & UDC_DS_CHG) ? " ds_chg" : "",
2375 (tmp & UDC_SETUP) ? " setup" : "",
2376 (tmp & UDC_EP0_RX) ? " ep0out" : "",
2377 (tmp & UDC_EP0_TX) ? " ep0in" : "");
2381 tmp = omap_readw(UDC_DMA_IRQ_EN);
2382 seq_printf(s, "dma_irq_en %04x%s" EIGHTBITS "\n", tmp,
2383 (tmp & UDC_TX_DONE_IE(3)) ? " tx2_done" : "",
2384 (tmp & UDC_RX_CNT_IE(3)) ? " rx2_cnt" : "",
2385 (tmp & UDC_RX_EOT_IE(3)) ? " rx2_eot" : "",
2387 (tmp & UDC_TX_DONE_IE(2)) ? " tx1_done" : "",
2388 (tmp & UDC_RX_CNT_IE(2)) ? " rx1_cnt" : "",
2389 (tmp & UDC_RX_EOT_IE(2)) ? " rx1_eot" : "",
2391 (tmp & UDC_TX_DONE_IE(1)) ? " tx0_done" : "",
2392 (tmp & UDC_RX_CNT_IE(1)) ? " rx0_cnt" : "",
2393 (tmp & UDC_RX_EOT_IE(1)) ? " rx0_eot" : "");
2395 tmp = omap_readw(UDC_RXDMA_CFG);
2396 seq_printf(s, "rxdma_cfg %04x\n", tmp);
2398 for (i = 0; i < 3; i++) {
2399 if ((tmp & (0x0f << (i * 4))) == 0)
2401 seq_printf(s, "rxdma[%d] %04x\n", i,
2402 omap_readw(UDC_RXDMA(i + 1)));
2405 tmp = omap_readw(UDC_TXDMA_CFG);
2406 seq_printf(s, "txdma_cfg %04x\n", tmp);
2408 for (i = 0; i < 3; i++) {
2409 if (!(tmp & (0x0f << (i * 4))))
2411 seq_printf(s, "txdma[%d] %04x\n", i,
2412 omap_readw(UDC_TXDMA(i + 1)));
2417 tmp = omap_readw(UDC_DEVSTAT);
2418 if (tmp & UDC_ATT) {
2419 proc_ep_show(s, &udc->ep[0]);
2420 if (tmp & UDC_ADD) {
2421 list_for_each_entry(ep, &udc->gadget.ep_list,
2424 proc_ep_show(s, ep);
2428 spin_unlock_irqrestore(&udc->lock, flags);
2432 static void create_proc_file(void)
2434 proc_create_single(proc_filename, 0, NULL, proc_udc_show);
2437 static void remove_proc_file(void)
2439 remove_proc_entry(proc_filename, NULL);
2444 static inline void create_proc_file(void) {}
2445 static inline void remove_proc_file(void) {}
2449 /*-------------------------------------------------------------------------*/
2451 /* Before this controller can enumerate, we need to pick an endpoint
2452 * configuration, or "fifo_mode" That involves allocating 2KB of packet
2453 * buffer space among the endpoints we'll be operating.
2455 * NOTE: as of OMAP 1710 ES2.0, writing a new endpoint config when
2456 * UDC_SYSCON_1.CFG_LOCK is set can now work. We won't use that
2457 * capability yet though.
2460 omap_ep_setup(char *name, u8 addr, u8 type,
2461 unsigned buf, unsigned maxp, int dbuf)
2466 /* OUT endpoints first, then IN */
2467 ep = &udc->ep[addr & 0xf];
2468 if (addr & USB_DIR_IN)
2471 /* in case of ep init table bugs */
2472 BUG_ON(ep->name[0]);
2474 /* chip setup ... bit values are same for IN, OUT */
2475 if (type == USB_ENDPOINT_XFER_ISOC) {
2501 epn_rxtx |= UDC_EPN_RX_ISO;
2504 /* double-buffering "not supported" on 15xx,
2505 * and ignored for PIO-IN on newer chips
2506 * (for more reliable behavior)
2508 if (!use_dma || cpu_is_omap15xx())
2528 epn_rxtx |= UDC_EPN_RX_DB;
2529 timer_setup(&ep->timer, pio_out_timer, 0);
2532 epn_rxtx |= UDC_EPN_RX_VALID;
2534 epn_rxtx |= buf >> 3;
2536 DBG("%s addr %02x rxtx %04x maxp %d%s buf %d\n",
2537 name, addr, epn_rxtx, maxp, dbuf ? "x2" : "", buf);
2539 if (addr & USB_DIR_IN)
2540 omap_writew(epn_rxtx, UDC_EP_TX(addr & 0xf));
2542 omap_writew(epn_rxtx, UDC_EP_RX(addr));
2544 /* next endpoint's buffer starts after this one's */
2550 /* set up driver data structures */
2551 BUG_ON(strlen(name) >= sizeof ep->name);
2552 strscpy(ep->name, name, sizeof(ep->name));
2553 INIT_LIST_HEAD(&ep->queue);
2554 INIT_LIST_HEAD(&ep->iso);
2555 ep->bEndpointAddress = addr;
2556 ep->bmAttributes = type;
2557 ep->double_buf = dbuf;
2561 case USB_ENDPOINT_XFER_CONTROL:
2562 ep->ep.caps.type_control = true;
2563 ep->ep.caps.dir_in = true;
2564 ep->ep.caps.dir_out = true;
2566 case USB_ENDPOINT_XFER_ISOC:
2567 ep->ep.caps.type_iso = true;
2569 case USB_ENDPOINT_XFER_BULK:
2570 ep->ep.caps.type_bulk = true;
2572 case USB_ENDPOINT_XFER_INT:
2573 ep->ep.caps.type_int = true;
2577 if (addr & USB_DIR_IN)
2578 ep->ep.caps.dir_in = true;
2580 ep->ep.caps.dir_out = true;
2582 ep->ep.name = ep->name;
2583 ep->ep.ops = &omap_ep_ops;
2584 ep->maxpacket = maxp;
2585 usb_ep_set_maxpacket_limit(&ep->ep, ep->maxpacket);
2586 list_add_tail(&ep->ep.ep_list, &udc->gadget.ep_list);
2591 static void omap_udc_release(struct device *dev)
2593 pullup_disable(udc);
2594 if (!IS_ERR_OR_NULL(udc->transceiver)) {
2595 usb_put_phy(udc->transceiver);
2596 udc->transceiver = NULL;
2598 omap_writew(0, UDC_SYSCON1);
2601 if (udc->clk_requested)
2602 omap_udc_enable_clock(0);
2603 clk_unprepare(udc->hhc_clk);
2604 clk_unprepare(udc->dc_clk);
2605 clk_put(udc->hhc_clk);
2606 clk_put(udc->dc_clk);
2609 complete(udc->done);
2614 omap_udc_setup(struct platform_device *odev, struct usb_phy *xceiv)
2618 /* abolish any previous hardware state */
2619 omap_writew(0, UDC_SYSCON1);
2620 omap_writew(0, UDC_IRQ_EN);
2621 omap_writew(UDC_IRQ_SRC_MASK, UDC_IRQ_SRC);
2622 omap_writew(0, UDC_DMA_IRQ_EN);
2623 omap_writew(0, UDC_RXDMA_CFG);
2624 omap_writew(0, UDC_TXDMA_CFG);
2626 /* UDC_PULLUP_EN gates the chip clock */
2627 /* OTG_SYSCON_1 |= DEV_IDLE_EN; */
2629 udc = kzalloc(sizeof(*udc), GFP_KERNEL);
2633 spin_lock_init(&udc->lock);
2635 udc->gadget.ops = &omap_gadget_ops;
2636 udc->gadget.ep0 = &udc->ep[0].ep;
2637 INIT_LIST_HEAD(&udc->gadget.ep_list);
2638 INIT_LIST_HEAD(&udc->iso);
2639 udc->gadget.speed = USB_SPEED_UNKNOWN;
2640 udc->gadget.max_speed = USB_SPEED_FULL;
2641 udc->gadget.name = driver_name;
2642 udc->gadget.quirk_ep_out_aligned_size = 1;
2643 udc->transceiver = xceiv;
2645 /* ep0 is special; put it right after the SETUP buffer */
2646 buf = omap_ep_setup("ep0", 0, USB_ENDPOINT_XFER_CONTROL,
2647 8 /* after SETUP */, 64 /* maxpacket */, 0);
2648 list_del_init(&udc->ep[0].ep.ep_list);
2650 /* initially disable all non-ep0 endpoints */
2651 for (tmp = 1; tmp < 15; tmp++) {
2652 omap_writew(0, UDC_EP_RX(tmp));
2653 omap_writew(0, UDC_EP_TX(tmp));
2656 #define OMAP_BULK_EP(name, addr) \
2657 buf = omap_ep_setup(name "-bulk", addr, \
2658 USB_ENDPOINT_XFER_BULK, buf, 64, 1);
2659 #define OMAP_INT_EP(name, addr, maxp) \
2660 buf = omap_ep_setup(name "-int", addr, \
2661 USB_ENDPOINT_XFER_INT, buf, maxp, 0);
2662 #define OMAP_ISO_EP(name, addr, maxp) \
2663 buf = omap_ep_setup(name "-iso", addr, \
2664 USB_ENDPOINT_XFER_ISOC, buf, maxp, 1);
2666 switch (fifo_mode) {
2668 OMAP_BULK_EP("ep1in", USB_DIR_IN | 1);
2669 OMAP_BULK_EP("ep2out", USB_DIR_OUT | 2);
2670 OMAP_INT_EP("ep3in", USB_DIR_IN | 3, 16);
2673 OMAP_BULK_EP("ep1in", USB_DIR_IN | 1);
2674 OMAP_BULK_EP("ep2out", USB_DIR_OUT | 2);
2675 OMAP_INT_EP("ep9in", USB_DIR_IN | 9, 16);
2677 OMAP_BULK_EP("ep3in", USB_DIR_IN | 3);
2678 OMAP_BULK_EP("ep4out", USB_DIR_OUT | 4);
2679 OMAP_INT_EP("ep10in", USB_DIR_IN | 10, 16);
2681 OMAP_BULK_EP("ep5in", USB_DIR_IN | 5);
2682 OMAP_BULK_EP("ep5out", USB_DIR_OUT | 5);
2683 OMAP_INT_EP("ep11in", USB_DIR_IN | 11, 16);
2685 OMAP_BULK_EP("ep6in", USB_DIR_IN | 6);
2686 OMAP_BULK_EP("ep6out", USB_DIR_OUT | 6);
2687 OMAP_INT_EP("ep12in", USB_DIR_IN | 12, 16);
2689 OMAP_BULK_EP("ep7in", USB_DIR_IN | 7);
2690 OMAP_BULK_EP("ep7out", USB_DIR_OUT | 7);
2691 OMAP_INT_EP("ep13in", USB_DIR_IN | 13, 16);
2692 OMAP_INT_EP("ep13out", USB_DIR_OUT | 13, 16);
2694 OMAP_BULK_EP("ep8in", USB_DIR_IN | 8);
2695 OMAP_BULK_EP("ep8out", USB_DIR_OUT | 8);
2696 OMAP_INT_EP("ep14in", USB_DIR_IN | 14, 16);
2697 OMAP_INT_EP("ep14out", USB_DIR_OUT | 14, 16);
2699 OMAP_BULK_EP("ep15in", USB_DIR_IN | 15);
2700 OMAP_BULK_EP("ep15out", USB_DIR_OUT | 15);
2705 case 2: /* mixed iso/bulk */
2706 OMAP_ISO_EP("ep1in", USB_DIR_IN | 1, 256);
2707 OMAP_ISO_EP("ep2out", USB_DIR_OUT | 2, 256);
2708 OMAP_ISO_EP("ep3in", USB_DIR_IN | 3, 128);
2709 OMAP_ISO_EP("ep4out", USB_DIR_OUT | 4, 128);
2711 OMAP_INT_EP("ep5in", USB_DIR_IN | 5, 16);
2713 OMAP_BULK_EP("ep6in", USB_DIR_IN | 6);
2714 OMAP_BULK_EP("ep7out", USB_DIR_OUT | 7);
2715 OMAP_INT_EP("ep8in", USB_DIR_IN | 8, 16);
2717 case 3: /* mixed bulk/iso */
2718 OMAP_BULK_EP("ep1in", USB_DIR_IN | 1);
2719 OMAP_BULK_EP("ep2out", USB_DIR_OUT | 2);
2720 OMAP_INT_EP("ep3in", USB_DIR_IN | 3, 16);
2722 OMAP_BULK_EP("ep4in", USB_DIR_IN | 4);
2723 OMAP_BULK_EP("ep5out", USB_DIR_OUT | 5);
2724 OMAP_INT_EP("ep6in", USB_DIR_IN | 6, 16);
2726 OMAP_ISO_EP("ep7in", USB_DIR_IN | 7, 256);
2727 OMAP_ISO_EP("ep8out", USB_DIR_OUT | 8, 256);
2728 OMAP_INT_EP("ep9in", USB_DIR_IN | 9, 16);
2732 /* add more modes as needed */
2735 ERR("unsupported fifo_mode #%d\n", fifo_mode);
2738 omap_writew(UDC_CFG_LOCK|UDC_SELF_PWR, UDC_SYSCON1);
2739 INFO("fifo mode %d, %d bytes not used\n", fifo_mode, 2048 - buf);
2743 static int omap_udc_probe(struct platform_device *pdev)
2745 int status = -ENODEV;
2747 struct usb_phy *xceiv = NULL;
2748 const char *type = NULL;
2749 struct omap_usb_config *config = dev_get_platdata(&pdev->dev);
2750 struct clk *dc_clk = NULL;
2751 struct clk *hhc_clk = NULL;
2753 /* NOTE: "knows" the order of the resources! */
2754 if (!request_mem_region(pdev->resource[0].start,
2755 resource_size(&pdev->resource[0]),
2757 DBG("request_mem_region failed\n");
2761 if (cpu_is_omap16xx()) {
2762 dc_clk = clk_get(&pdev->dev, "usb_dc_ck");
2763 hhc_clk = clk_get(&pdev->dev, "usb_hhc_ck");
2764 BUG_ON(IS_ERR(dc_clk) || IS_ERR(hhc_clk));
2765 /* can't use omap_udc_enable_clock yet */
2766 clk_prepare_enable(dc_clk);
2767 clk_prepare_enable(hhc_clk);
2771 INFO("OMAP UDC rev %d.%d%s\n",
2772 omap_readw(UDC_REV) >> 4, omap_readw(UDC_REV) & 0xf,
2773 config->otg ? ", Mini-AB" : "");
2775 /* use the mode given to us by board init code */
2776 if (cpu_is_omap15xx()) {
2780 if (machine_without_vbus_sense()) {
2781 /* just set up software VBUS detect, and then
2782 * later rig it so we always report VBUS.
2783 * FIXME without really sensing VBUS, we can't
2784 * know when to turn PULLUP_EN on/off; and that
2785 * means we always "need" the 48MHz clock.
2787 u32 tmp = omap_readl(FUNC_MUX_CTRL_0);
2788 tmp &= ~VBUS_CTRL_1510;
2789 omap_writel(tmp, FUNC_MUX_CTRL_0);
2790 tmp |= VBUS_MODE_1510;
2791 tmp &= ~VBUS_CTRL_1510;
2792 omap_writel(tmp, FUNC_MUX_CTRL_0);
2795 /* The transceiver may package some GPIO logic or handle
2796 * loopback and/or transceiverless setup; if we find one,
2797 * use it. Except for OTG, we don't _need_ to talk to one;
2798 * but not having one probably means no VBUS detection.
2800 xceiv = usb_get_phy(USB_PHY_TYPE_USB2);
2801 if (!IS_ERR_OR_NULL(xceiv))
2802 type = xceiv->label;
2803 else if (config->otg) {
2804 DBG("OTG requires external transceiver!\n");
2811 case 0: /* POWERUP DEFAULT == 0 */
2815 if (!cpu_is_omap1710()) {
2816 type = "integrated";
2825 if (IS_ERR_OR_NULL(xceiv)) {
2826 DBG("external transceiver not registered!\n");
2830 case 21: /* internal loopback */
2833 case 14: /* transceiverless */
2834 if (cpu_is_omap1710())
2844 ERR("unrecognized UDC HMC mode %d\n", hmc);
2849 INFO("hmc mode %d, %s transceiver\n", hmc, type);
2851 /* a "gadget" abstracts/virtualizes the controller */
2852 status = omap_udc_setup(pdev, xceiv);
2857 /* "udc" is now valid */
2858 pullup_disable(udc);
2859 #if IS_ENABLED(CONFIG_USB_OHCI_HCD)
2860 udc->gadget.is_otg = (config->otg != 0);
2863 /* starting with omap1710 es2.0, clear toggle is a separate bit */
2864 if (omap_readw(UDC_REV) >= 0x61)
2865 udc->clr_halt = UDC_RESET_EP | UDC_CLRDATA_TOGGLE;
2867 udc->clr_halt = UDC_RESET_EP;
2869 /* USB general purpose IRQ: ep0, state changes, dma, etc */
2870 status = devm_request_irq(&pdev->dev, pdev->resource[1].start,
2871 omap_udc_irq, 0, driver_name, udc);
2873 ERR("can't get irq %d, err %d\n",
2874 (int) pdev->resource[1].start, status);
2878 /* USB "non-iso" IRQ (PIO for all but ep0) */
2879 status = devm_request_irq(&pdev->dev, pdev->resource[2].start,
2880 omap_udc_pio_irq, 0, "omap_udc pio", udc);
2882 ERR("can't get irq %d, err %d\n",
2883 (int) pdev->resource[2].start, status);
2887 status = devm_request_irq(&pdev->dev, pdev->resource[3].start,
2888 omap_udc_iso_irq, 0, "omap_udc iso", udc);
2890 ERR("can't get irq %d, err %d\n",
2891 (int) pdev->resource[3].start, status);
2895 if (cpu_is_omap16xx()) {
2896 udc->dc_clk = dc_clk;
2897 udc->hhc_clk = hhc_clk;
2898 clk_disable(hhc_clk);
2899 clk_disable(dc_clk);
2903 return usb_add_gadget_udc_release(&pdev->dev, &udc->gadget,
2911 if (!IS_ERR_OR_NULL(xceiv))
2914 if (cpu_is_omap16xx()) {
2915 clk_disable_unprepare(hhc_clk);
2916 clk_disable_unprepare(dc_clk);
2921 release_mem_region(pdev->resource[0].start,
2922 resource_size(&pdev->resource[0]));
2927 static void omap_udc_remove(struct platform_device *pdev)
2929 DECLARE_COMPLETION_ONSTACK(done);
2933 usb_del_gadget_udc(&udc->gadget);
2935 wait_for_completion(&done);
2937 release_mem_region(pdev->resource[0].start,
2938 resource_size(&pdev->resource[0]));
2941 /* suspend/resume/wakeup from sysfs (echo > power/state) or when the
2942 * system is forced into deep sleep
2944 * REVISIT we should probably reject suspend requests when there's a host
2945 * session active, rather than disconnecting, at least on boards that can
2946 * report VBUS irqs (UDC_DEVSTAT.UDC_ATT). And in any case, we need to
2947 * make host resumes and VBUS detection trigger OMAP wakeup events; that
2948 * may involve talking to an external transceiver (e.g. isp1301).
2951 static int omap_udc_suspend(struct platform_device *dev, pm_message_t message)
2955 devstat = omap_readw(UDC_DEVSTAT);
2957 /* we're requesting 48 MHz clock if the pullup is enabled
2958 * (== we're attached to the host) and we're not suspended,
2959 * which would prevent entry to deep sleep...
2961 if ((devstat & UDC_ATT) != 0 && (devstat & UDC_SUS) == 0) {
2962 WARNING("session active; suspend requires disconnect\n");
2963 omap_pullup(&udc->gadget, 0);
2969 static int omap_udc_resume(struct platform_device *dev)
2971 DBG("resume + wakeup/SRP\n");
2972 omap_pullup(&udc->gadget, 1);
2974 /* maybe the host would enumerate us if we nudged it */
2976 return omap_wakeup(&udc->gadget);
2979 /*-------------------------------------------------------------------------*/
2981 static struct platform_driver udc_driver = {
2982 .probe = omap_udc_probe,
2983 .remove = omap_udc_remove,
2984 .suspend = omap_udc_suspend,
2985 .resume = omap_udc_resume,
2987 .name = driver_name,
2991 module_platform_driver(udc_driver);
2993 MODULE_DESCRIPTION("OMAP UDC driver");
2994 MODULE_LICENSE("GPL");
2995 MODULE_ALIAS("platform:omap_udc");