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 <asm/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_DESC "OMAP UDC driver"
60 #define DRIVER_VERSION "4 October 2004"
62 #define OMAP_DMA_USB_W2FC_TX0 29
63 #define OMAP_DMA_USB_W2FC_RX0 26
66 * The OMAP UDC needs _very_ early endpoint setup: before enabling the
67 * D+ pullup to allow enumeration. That's too early for the gadget
68 * framework to use from usb_endpoint_enable(), which happens after
69 * enumeration as part of activating an interface. (But if we add an
70 * optional new "UDC not yet running" state to the gadget driver model,
71 * even just during driver binding, the endpoint autoconfig logic is the
72 * natural spot to manufacture new endpoints.)
74 * So instead of using endpoint enable calls to control the hardware setup,
75 * this driver defines a "fifo mode" parameter. It's used during driver
76 * initialization to choose among a set of pre-defined endpoint configs.
77 * See omap_udc_setup() for available modes, or to add others. That code
78 * lives in an init section, so use this driver as a module if you need
79 * to change the fifo mode after the kernel boots.
81 * Gadget drivers normally ignore endpoints they don't care about, and
82 * won't include them in configuration descriptors. That means only
83 * misbehaving hosts would even notice they exist.
86 static unsigned fifo_mode = 3;
88 static unsigned fifo_mode;
91 /* "modprobe omap_udc fifo_mode=42", or else as a kernel
92 * boot parameter "omap_udc:fifo_mode=42"
94 module_param(fifo_mode, uint, 0);
95 MODULE_PARM_DESC(fifo_mode, "endpoint configuration");
98 static bool use_dma = 1;
100 /* "modprobe omap_udc use_dma=y", or else as a kernel
101 * boot parameter "omap_udc:use_dma=y"
103 module_param(use_dma, bool, 0);
104 MODULE_PARM_DESC(use_dma, "enable/disable DMA");
107 /* save a bit of code */
109 #endif /* !USE_DMA */
112 static const char driver_name[] = "omap_udc";
113 static const char driver_desc[] = DRIVER_DESC;
115 /*-------------------------------------------------------------------------*/
117 /* there's a notion of "current endpoint" for modifying endpoint
118 * state, and PIO access to its FIFO.
121 static void use_ep(struct omap_ep *ep, u16 select)
123 u16 num = ep->bEndpointAddress & 0x0f;
125 if (ep->bEndpointAddress & USB_DIR_IN)
127 omap_writew(num | select, UDC_EP_NUM);
128 /* when select, MUST deselect later !! */
131 static inline void deselect_ep(void)
135 w = omap_readw(UDC_EP_NUM);
137 omap_writew(w, UDC_EP_NUM);
138 /* 6 wait states before TX will happen */
141 static void dma_channel_claim(struct omap_ep *ep, unsigned preferred);
143 /*-------------------------------------------------------------------------*/
145 static int omap_ep_enable(struct usb_ep *_ep,
146 const struct usb_endpoint_descriptor *desc)
148 struct omap_ep *ep = container_of(_ep, struct omap_ep, ep);
149 struct omap_udc *udc;
153 /* catch various bogus parameters */
155 || desc->bDescriptorType != USB_DT_ENDPOINT
156 || ep->bEndpointAddress != desc->bEndpointAddress
157 || ep->maxpacket < usb_endpoint_maxp(desc)) {
158 DBG("%s, bad ep or descriptor\n", __func__);
161 maxp = usb_endpoint_maxp(desc);
162 if ((desc->bmAttributes == USB_ENDPOINT_XFER_BULK
163 && maxp != ep->maxpacket)
164 || usb_endpoint_maxp(desc) > ep->maxpacket
165 || !desc->wMaxPacketSize) {
166 DBG("%s, bad %s maxpacket\n", __func__, _ep->name);
171 if ((desc->bmAttributes == USB_ENDPOINT_XFER_ISOC
172 && desc->bInterval != 1)) {
173 /* hardware wants period = 1; USB allows 2^(Interval-1) */
174 DBG("%s, unsupported ISO period %dms\n", _ep->name,
175 1 << (desc->bInterval - 1));
179 if (desc->bmAttributes == USB_ENDPOINT_XFER_ISOC) {
180 DBG("%s, ISO nyet\n", _ep->name);
185 /* xfer types must match, except that interrupt ~= bulk */
186 if (ep->bmAttributes != desc->bmAttributes
187 && ep->bmAttributes != USB_ENDPOINT_XFER_BULK
188 && desc->bmAttributes != USB_ENDPOINT_XFER_INT) {
189 DBG("%s, %s type mismatch\n", __func__, _ep->name);
194 if (!udc->driver || udc->gadget.speed == USB_SPEED_UNKNOWN) {
195 DBG("%s, bogus device state\n", __func__);
199 spin_lock_irqsave(&udc->lock, flags);
204 ep->ep.maxpacket = maxp;
206 /* set endpoint to initial state */
210 use_ep(ep, UDC_EP_SEL);
211 omap_writew(udc->clr_halt, UDC_CTRL);
215 if (ep->bmAttributes == USB_ENDPOINT_XFER_ISOC)
216 list_add(&ep->iso, &udc->iso);
218 /* maybe assign a DMA channel to this endpoint */
219 if (use_dma && desc->bmAttributes == USB_ENDPOINT_XFER_BULK)
220 /* FIXME ISO can dma, but prefers first channel */
221 dma_channel_claim(ep, 0);
223 /* PIO OUT may RX packets */
224 if (desc->bmAttributes != USB_ENDPOINT_XFER_ISOC
226 && !(ep->bEndpointAddress & USB_DIR_IN)) {
227 omap_writew(UDC_SET_FIFO_EN, UDC_CTRL);
228 ep->ackwait = 1 + ep->double_buf;
231 spin_unlock_irqrestore(&udc->lock, flags);
232 VDBG("%s enabled\n", _ep->name);
236 static void nuke(struct omap_ep *, int status);
238 static int omap_ep_disable(struct usb_ep *_ep)
240 struct omap_ep *ep = container_of(_ep, struct omap_ep, ep);
243 if (!_ep || !ep->ep.desc) {
244 DBG("%s, %s not enabled\n", __func__,
245 _ep ? ep->ep.name : NULL);
249 spin_lock_irqsave(&ep->udc->lock, flags);
251 nuke(ep, -ESHUTDOWN);
252 ep->ep.maxpacket = ep->maxpacket;
254 omap_writew(UDC_SET_HALT, UDC_CTRL);
255 list_del_init(&ep->iso);
256 del_timer(&ep->timer);
258 spin_unlock_irqrestore(&ep->udc->lock, flags);
260 VDBG("%s disabled\n", _ep->name);
264 /*-------------------------------------------------------------------------*/
266 static struct usb_request *
267 omap_alloc_request(struct usb_ep *ep, gfp_t gfp_flags)
269 struct omap_req *req;
271 req = kzalloc(sizeof(*req), gfp_flags);
275 INIT_LIST_HEAD(&req->queue);
281 omap_free_request(struct usb_ep *ep, struct usb_request *_req)
283 struct omap_req *req = container_of(_req, struct omap_req, req);
288 /*-------------------------------------------------------------------------*/
291 done(struct omap_ep *ep, struct omap_req *req, int status)
293 struct omap_udc *udc = ep->udc;
294 unsigned stopped = ep->stopped;
296 list_del_init(&req->queue);
298 if (req->req.status == -EINPROGRESS)
299 req->req.status = status;
301 status = req->req.status;
303 if (use_dma && ep->has_dma)
304 usb_gadget_unmap_request(&udc->gadget, &req->req,
305 (ep->bEndpointAddress & USB_DIR_IN));
308 if (status && status != -ESHUTDOWN)
310 VDBG("complete %s req %p stat %d len %u/%u\n",
311 ep->ep.name, &req->req, status,
312 req->req.actual, req->req.length);
314 /* don't modify queue heads during completion callback */
316 spin_unlock(&ep->udc->lock);
317 usb_gadget_giveback_request(&ep->ep, &req->req);
318 spin_lock(&ep->udc->lock);
319 ep->stopped = stopped;
322 /*-------------------------------------------------------------------------*/
324 #define UDC_FIFO_FULL (UDC_NON_ISO_FIFO_FULL | UDC_ISO_FIFO_FULL)
325 #define UDC_FIFO_UNWRITABLE (UDC_EP_HALTED | UDC_FIFO_FULL)
327 #define FIFO_EMPTY (UDC_NON_ISO_FIFO_EMPTY | UDC_ISO_FIFO_EMPTY)
328 #define FIFO_UNREADABLE (UDC_EP_HALTED | FIFO_EMPTY)
331 write_packet(u8 *buf, struct omap_req *req, unsigned max)
336 len = min(req->req.length - req->req.actual, max);
337 req->req.actual += len;
340 if (likely((((int)buf) & 1) == 0)) {
343 omap_writew(*wp++, UDC_DATA);
349 omap_writeb(*buf++, UDC_DATA);
353 /* FIXME change r/w fifo calling convention */
356 /* return: 0 = still running, 1 = completed, negative = errno */
357 static int write_fifo(struct omap_ep *ep, struct omap_req *req)
364 buf = req->req.buf + req->req.actual;
367 /* PIO-IN isn't double buffered except for iso */
368 ep_stat = omap_readw(UDC_STAT_FLG);
369 if (ep_stat & UDC_FIFO_UNWRITABLE)
372 count = ep->ep.maxpacket;
373 count = write_packet(buf, req, count);
374 omap_writew(UDC_SET_FIFO_EN, UDC_CTRL);
377 /* last packet is often short (sometimes a zlp) */
378 if (count != ep->ep.maxpacket)
380 else if (req->req.length == req->req.actual
386 /* NOTE: requests complete when all IN data is in a
387 * FIFO (or sometimes later, if a zlp was needed).
388 * Use usb_ep_fifo_status() where needed.
396 read_packet(u8 *buf, struct omap_req *req, unsigned avail)
401 len = min(req->req.length - req->req.actual, avail);
402 req->req.actual += len;
405 if (likely((((int)buf) & 1) == 0)) {
408 *wp++ = omap_readw(UDC_DATA);
414 *buf++ = omap_readb(UDC_DATA);
418 /* return: 0 = still running, 1 = queue empty, negative = errno */
419 static int read_fifo(struct omap_ep *ep, struct omap_req *req)
422 unsigned count, avail;
425 buf = req->req.buf + req->req.actual;
429 u16 ep_stat = omap_readw(UDC_STAT_FLG);
432 if (ep_stat & FIFO_EMPTY) {
437 if (ep_stat & UDC_EP_HALTED)
440 if (ep_stat & UDC_FIFO_FULL)
441 avail = ep->ep.maxpacket;
443 avail = omap_readw(UDC_RXFSTAT);
444 ep->fnf = ep->double_buf;
446 count = read_packet(buf, req, avail);
448 /* partial packet reads may not be errors */
449 if (count < ep->ep.maxpacket) {
451 /* overflowed this request? flush extra data */
452 if (count != avail) {
453 req->req.status = -EOVERFLOW;
456 omap_readw(UDC_DATA);
458 } else if (req->req.length == req->req.actual)
463 if (!ep->bEndpointAddress)
472 /*-------------------------------------------------------------------------*/
474 static u16 dma_src_len(struct omap_ep *ep, dma_addr_t start)
478 /* IN-DMA needs this on fault/cancel paths, so 15xx misreports
479 * the last transfer's bytecount by more than a FIFO's worth.
481 if (cpu_is_omap15xx())
484 end = omap_get_dma_src_pos(ep->lch);
485 if (end == ep->dma_counter)
488 end |= start & (0xffff << 16);
494 static u16 dma_dest_len(struct omap_ep *ep, dma_addr_t start)
498 end = omap_get_dma_dst_pos(ep->lch);
499 if (end == ep->dma_counter)
502 end |= start & (0xffff << 16);
503 if (cpu_is_omap15xx())
511 /* Each USB transfer request using DMA maps to one or more DMA transfers.
512 * When DMA completion isn't request completion, the UDC continues with
513 * the next DMA transfer for that USB transfer.
516 static void next_in_dma(struct omap_ep *ep, struct omap_req *req)
519 unsigned length = req->req.length - req->req.actual;
520 const int sync_mode = cpu_is_omap15xx()
521 ? OMAP_DMA_SYNC_FRAME
522 : OMAP_DMA_SYNC_ELEMENT;
525 /* measure length in either bytes or packets */
526 if ((cpu_is_omap16xx() && length <= UDC_TXN_TSC)
527 || (cpu_is_omap15xx() && length < ep->maxpacket)) {
528 txdma_ctrl = UDC_TXN_EOT | length;
529 omap_set_dma_transfer_params(ep->lch, OMAP_DMA_DATA_TYPE_S8,
530 length, 1, sync_mode, dma_trigger, 0);
532 length = min(length / ep->maxpacket,
533 (unsigned) UDC_TXN_TSC + 1);
535 omap_set_dma_transfer_params(ep->lch, OMAP_DMA_DATA_TYPE_S16,
536 ep->ep.maxpacket >> 1, length, sync_mode,
538 length *= ep->maxpacket;
540 omap_set_dma_src_params(ep->lch, OMAP_DMA_PORT_EMIFF,
541 OMAP_DMA_AMODE_POST_INC, req->req.dma + req->req.actual,
544 omap_start_dma(ep->lch);
545 ep->dma_counter = omap_get_dma_src_pos(ep->lch);
546 w = omap_readw(UDC_DMA_IRQ_EN);
547 w |= UDC_TX_DONE_IE(ep->dma_channel);
548 omap_writew(w, UDC_DMA_IRQ_EN);
549 omap_writew(UDC_TXN_START | txdma_ctrl, UDC_TXDMA(ep->dma_channel));
550 req->dma_bytes = length;
553 static void finish_in_dma(struct omap_ep *ep, struct omap_req *req, int status)
558 req->req.actual += req->dma_bytes;
560 /* return if this request needs to send data or zlp */
561 if (req->req.actual < req->req.length)
564 && req->dma_bytes != 0
565 && (req->req.actual % ep->maxpacket) == 0)
568 req->req.actual += dma_src_len(ep, req->req.dma
572 omap_stop_dma(ep->lch);
573 w = omap_readw(UDC_DMA_IRQ_EN);
574 w &= ~UDC_TX_DONE_IE(ep->dma_channel);
575 omap_writew(w, UDC_DMA_IRQ_EN);
576 done(ep, req, status);
579 static void next_out_dma(struct omap_ep *ep, struct omap_req *req)
581 unsigned packets = req->req.length - req->req.actual;
585 /* set up this DMA transfer, enable the fifo, start */
586 packets /= ep->ep.maxpacket;
587 packets = min(packets, (unsigned)UDC_RXN_TC + 1);
588 req->dma_bytes = packets * ep->ep.maxpacket;
589 omap_set_dma_transfer_params(ep->lch, OMAP_DMA_DATA_TYPE_S16,
590 ep->ep.maxpacket >> 1, packets,
591 OMAP_DMA_SYNC_ELEMENT,
593 omap_set_dma_dest_params(ep->lch, OMAP_DMA_PORT_EMIFF,
594 OMAP_DMA_AMODE_POST_INC, req->req.dma + req->req.actual,
596 ep->dma_counter = omap_get_dma_dst_pos(ep->lch);
598 omap_writew(UDC_RXN_STOP | (packets - 1), UDC_RXDMA(ep->dma_channel));
599 w = omap_readw(UDC_DMA_IRQ_EN);
600 w |= UDC_RX_EOT_IE(ep->dma_channel);
601 omap_writew(w, UDC_DMA_IRQ_EN);
602 omap_writew(ep->bEndpointAddress & 0xf, UDC_EP_NUM);
603 omap_writew(UDC_SET_FIFO_EN, UDC_CTRL);
605 omap_start_dma(ep->lch);
609 finish_out_dma(struct omap_ep *ep, struct omap_req *req, int status, int one)
614 ep->dma_counter = (u16) (req->req.dma + req->req.actual);
615 count = dma_dest_len(ep, req->req.dma + req->req.actual);
616 count += req->req.actual;
619 if (count <= req->req.length)
620 req->req.actual = count;
622 if (count != req->dma_bytes || status)
623 omap_stop_dma(ep->lch);
625 /* if this wasn't short, request may need another transfer */
626 else if (req->req.actual < req->req.length)
630 w = omap_readw(UDC_DMA_IRQ_EN);
631 w &= ~UDC_RX_EOT_IE(ep->dma_channel);
632 omap_writew(w, UDC_DMA_IRQ_EN);
633 done(ep, req, status);
636 static void dma_irq(struct omap_udc *udc, u16 irq_src)
638 u16 dman_stat = omap_readw(UDC_DMAN_STAT);
640 struct omap_req *req;
642 /* IN dma: tx to host */
643 if (irq_src & UDC_TXN_DONE) {
644 ep = &udc->ep[16 + UDC_DMA_TX_SRC(dman_stat)];
646 /* can see TXN_DONE after dma abort */
647 if (!list_empty(&ep->queue)) {
648 req = container_of(ep->queue.next,
649 struct omap_req, queue);
650 finish_in_dma(ep, req, 0);
652 omap_writew(UDC_TXN_DONE, UDC_IRQ_SRC);
654 if (!list_empty(&ep->queue)) {
655 req = container_of(ep->queue.next,
656 struct omap_req, queue);
657 next_in_dma(ep, req);
661 /* OUT dma: rx from host */
662 if (irq_src & UDC_RXN_EOT) {
663 ep = &udc->ep[UDC_DMA_RX_SRC(dman_stat)];
665 /* can see RXN_EOT after dma abort */
666 if (!list_empty(&ep->queue)) {
667 req = container_of(ep->queue.next,
668 struct omap_req, queue);
669 finish_out_dma(ep, req, 0, dman_stat & UDC_DMA_RX_SB);
671 omap_writew(UDC_RXN_EOT, UDC_IRQ_SRC);
673 if (!list_empty(&ep->queue)) {
674 req = container_of(ep->queue.next,
675 struct omap_req, queue);
676 next_out_dma(ep, req);
680 if (irq_src & UDC_RXN_CNT) {
681 ep = &udc->ep[UDC_DMA_RX_SRC(dman_stat)];
683 /* omap15xx does this unasked... */
684 VDBG("%s, RX_CNT irq?\n", ep->ep.name);
685 omap_writew(UDC_RXN_CNT, UDC_IRQ_SRC);
689 static void dma_error(int lch, u16 ch_status, void *data)
691 struct omap_ep *ep = data;
693 /* if ch_status & OMAP_DMA_DROP_IRQ ... */
694 /* if ch_status & OMAP1_DMA_TOUT_IRQ ... */
695 ERR("%s dma error, lch %d status %02x\n", ep->ep.name, lch, ch_status);
697 /* complete current transfer ... */
700 static void dma_channel_claim(struct omap_ep *ep, unsigned channel)
703 int status, restart, is_in;
706 is_in = ep->bEndpointAddress & USB_DIR_IN;
708 reg = omap_readw(UDC_TXDMA_CFG);
710 reg = omap_readw(UDC_RXDMA_CFG);
711 reg |= UDC_DMA_REQ; /* "pulse" activated */
715 if (channel == 0 || channel > 3) {
716 if ((reg & 0x0f00) == 0)
718 else if ((reg & 0x00f0) == 0)
720 else if ((reg & 0x000f) == 0) /* preferred for ISO */
727 reg |= (0x0f & ep->bEndpointAddress) << (4 * (channel - 1));
728 ep->dma_channel = channel;
731 dma_channel = OMAP_DMA_USB_W2FC_TX0 - 1 + channel;
732 status = omap_request_dma(dma_channel,
733 ep->ep.name, dma_error, ep, &ep->lch);
735 omap_writew(reg, UDC_TXDMA_CFG);
737 omap_set_dma_src_burst_mode(ep->lch,
738 OMAP_DMA_DATA_BURST_4);
739 omap_set_dma_src_data_pack(ep->lch, 1);
741 omap_set_dma_dest_params(ep->lch,
743 OMAP_DMA_AMODE_CONSTANT,
748 dma_channel = OMAP_DMA_USB_W2FC_RX0 - 1 + channel;
749 status = omap_request_dma(dma_channel,
750 ep->ep.name, dma_error, ep, &ep->lch);
752 omap_writew(reg, UDC_RXDMA_CFG);
754 omap_set_dma_src_params(ep->lch,
756 OMAP_DMA_AMODE_CONSTANT,
760 omap_set_dma_dest_burst_mode(ep->lch,
761 OMAP_DMA_DATA_BURST_4);
762 omap_set_dma_dest_data_pack(ep->lch, 1);
769 omap_disable_dma_irq(ep->lch, OMAP_DMA_BLOCK_IRQ);
771 /* channel type P: hw synch (fifo) */
772 if (!cpu_is_omap15xx())
773 omap_set_dma_channel_mode(ep->lch, OMAP_DMA_LCH_P);
777 /* restart any queue, even if the claim failed */
778 restart = !ep->stopped && !list_empty(&ep->queue);
781 DBG("%s no dma channel: %d%s\n", ep->ep.name, status,
782 restart ? " (restart)" : "");
784 DBG("%s claimed %cxdma%d lch %d%s\n", ep->ep.name,
786 ep->dma_channel - 1, ep->lch,
787 restart ? " (restart)" : "");
790 struct omap_req *req;
791 req = container_of(ep->queue.next, struct omap_req, queue);
793 (is_in ? next_in_dma : next_out_dma)(ep, req);
795 use_ep(ep, UDC_EP_SEL);
796 (is_in ? write_fifo : read_fifo)(ep, req);
799 omap_writew(UDC_SET_FIFO_EN, UDC_CTRL);
800 ep->ackwait = 1 + ep->double_buf;
802 /* IN: 6 wait states before it'll tx */
807 static void dma_channel_release(struct omap_ep *ep)
809 int shift = 4 * (ep->dma_channel - 1);
810 u16 mask = 0x0f << shift;
811 struct omap_req *req;
814 /* abort any active usb transfer request */
815 if (!list_empty(&ep->queue))
816 req = container_of(ep->queue.next, struct omap_req, queue);
820 active = omap_get_dma_active_status(ep->lch);
822 DBG("%s release %s %cxdma%d %p\n", ep->ep.name,
823 active ? "active" : "idle",
824 (ep->bEndpointAddress & USB_DIR_IN) ? 't' : 'r',
825 ep->dma_channel - 1, req);
827 /* NOTE: re-setting RX_REQ/TX_REQ because of a chip bug (before
828 * OMAP 1710 ES2.0) where reading the DMA_CFG can clear them.
831 /* wait till current packet DMA finishes, and fifo empties */
832 if (ep->bEndpointAddress & USB_DIR_IN) {
833 omap_writew((omap_readw(UDC_TXDMA_CFG) & ~mask) | UDC_DMA_REQ,
837 finish_in_dma(ep, req, -ECONNRESET);
839 /* clear FIFO; hosts probably won't empty it */
840 use_ep(ep, UDC_EP_SEL);
841 omap_writew(UDC_CLR_EP, UDC_CTRL);
844 while (omap_readw(UDC_TXDMA_CFG) & mask)
847 omap_writew((omap_readw(UDC_RXDMA_CFG) & ~mask) | UDC_DMA_REQ,
850 /* dma empties the fifo */
851 while (omap_readw(UDC_RXDMA_CFG) & mask)
854 finish_out_dma(ep, req, -ECONNRESET, 0);
856 omap_free_dma(ep->lch);
859 /* has_dma still set, till endpoint is fully quiesced */
863 /*-------------------------------------------------------------------------*/
866 omap_ep_queue(struct usb_ep *_ep, struct usb_request *_req, gfp_t gfp_flags)
868 struct omap_ep *ep = container_of(_ep, struct omap_ep, ep);
869 struct omap_req *req = container_of(_req, struct omap_req, req);
870 struct omap_udc *udc;
874 /* catch various bogus parameters */
875 if (!_req || !req->req.complete || !req->req.buf
876 || !list_empty(&req->queue)) {
877 DBG("%s, bad params\n", __func__);
880 if (!_ep || (!ep->ep.desc && ep->bEndpointAddress)) {
881 DBG("%s, bad ep\n", __func__);
884 if (ep->bmAttributes == USB_ENDPOINT_XFER_ISOC) {
885 if (req->req.length > ep->ep.maxpacket)
890 /* this isn't bogus, but OMAP DMA isn't the only hardware to
891 * have a hard time with partial packet reads... reject it.
895 && ep->bEndpointAddress != 0
896 && (ep->bEndpointAddress & USB_DIR_IN) == 0
897 && (req->req.length % ep->ep.maxpacket) != 0) {
898 DBG("%s, no partial packet OUT reads\n", __func__);
903 if (!udc->driver || udc->gadget.speed == USB_SPEED_UNKNOWN)
906 if (use_dma && ep->has_dma)
907 usb_gadget_map_request(&udc->gadget, &req->req,
908 (ep->bEndpointAddress & USB_DIR_IN));
910 VDBG("%s queue req %p, len %d buf %p\n",
911 ep->ep.name, _req, _req->length, _req->buf);
913 spin_lock_irqsave(&udc->lock, flags);
915 req->req.status = -EINPROGRESS;
918 /* maybe kickstart non-iso i/o queues */
922 w = omap_readw(UDC_IRQ_EN);
924 omap_writew(w, UDC_IRQ_EN);
925 } else if (list_empty(&ep->queue) && !ep->stopped && !ep->ackwait) {
928 if (ep->bEndpointAddress == 0) {
929 if (!udc->ep0_pending || !list_empty(&ep->queue)) {
930 spin_unlock_irqrestore(&udc->lock, flags);
934 /* empty DATA stage? */
936 if (!req->req.length) {
938 /* chip became CONFIGURED or ADDRESSED
939 * earlier; drivers may already have queued
940 * requests to non-control endpoints
942 if (udc->ep0_set_config) {
943 u16 irq_en = omap_readw(UDC_IRQ_EN);
945 irq_en |= UDC_DS_CHG_IE | UDC_EP0_IE;
946 if (!udc->ep0_reset_config)
947 irq_en |= UDC_EPN_RX_IE
949 omap_writew(irq_en, UDC_IRQ_EN);
952 /* STATUS for zero length DATA stages is
953 * always an IN ... even for IN transfers,
954 * a weird case which seem to stall OMAP.
956 omap_writew(UDC_EP_SEL | UDC_EP_DIR,
958 omap_writew(UDC_CLR_EP, UDC_CTRL);
959 omap_writew(UDC_SET_FIFO_EN, UDC_CTRL);
960 omap_writew(UDC_EP_DIR, UDC_EP_NUM);
963 udc->ep0_pending = 0;
967 /* non-empty DATA stage */
969 omap_writew(UDC_EP_SEL | UDC_EP_DIR,
974 omap_writew(UDC_EP_SEL, UDC_EP_NUM);
977 is_in = ep->bEndpointAddress & USB_DIR_IN;
979 use_ep(ep, UDC_EP_SEL);
980 /* if ISO: SOF IRQs must be enabled/disabled! */
984 (is_in ? next_in_dma : next_out_dma)(ep, req);
986 if ((is_in ? write_fifo : read_fifo)(ep, req) == 1)
990 omap_writew(UDC_SET_FIFO_EN, UDC_CTRL);
991 ep->ackwait = 1 + ep->double_buf;
993 /* IN: 6 wait states before it'll tx */
998 /* irq handler advances the queue */
1000 list_add_tail(&req->queue, &ep->queue);
1001 spin_unlock_irqrestore(&udc->lock, flags);
1006 static int omap_ep_dequeue(struct usb_ep *_ep, struct usb_request *_req)
1008 struct omap_ep *ep = container_of(_ep, struct omap_ep, ep);
1009 struct omap_req *req = NULL, *iter;
1010 unsigned long flags;
1015 spin_lock_irqsave(&ep->udc->lock, flags);
1017 /* make sure it's actually queued on this endpoint */
1018 list_for_each_entry(iter, &ep->queue, queue) {
1019 if (&iter->req != _req)
1025 spin_unlock_irqrestore(&ep->udc->lock, flags);
1029 if (use_dma && ep->dma_channel && ep->queue.next == &req->queue) {
1030 int channel = ep->dma_channel;
1032 /* releasing the channel cancels the request,
1033 * reclaiming the channel restarts the queue
1035 dma_channel_release(ep);
1036 dma_channel_claim(ep, channel);
1038 done(ep, req, -ECONNRESET);
1039 spin_unlock_irqrestore(&ep->udc->lock, flags);
1043 /*-------------------------------------------------------------------------*/
1045 static int omap_ep_set_halt(struct usb_ep *_ep, int value)
1047 struct omap_ep *ep = container_of(_ep, struct omap_ep, ep);
1048 unsigned long flags;
1049 int status = -EOPNOTSUPP;
1051 spin_lock_irqsave(&ep->udc->lock, flags);
1053 /* just use protocol stalls for ep0; real halts are annoying */
1054 if (ep->bEndpointAddress == 0) {
1055 if (!ep->udc->ep0_pending)
1058 if (ep->udc->ep0_set_config) {
1059 WARNING("error changing config?\n");
1060 omap_writew(UDC_CLR_CFG, UDC_SYSCON2);
1062 omap_writew(UDC_STALL_CMD, UDC_SYSCON2);
1063 ep->udc->ep0_pending = 0;
1068 /* otherwise, all active non-ISO endpoints can halt */
1069 } else if (ep->bmAttributes != USB_ENDPOINT_XFER_ISOC && ep->ep.desc) {
1071 /* IN endpoints must already be idle */
1072 if ((ep->bEndpointAddress & USB_DIR_IN)
1073 && !list_empty(&ep->queue)) {
1081 if (use_dma && ep->dma_channel
1082 && !list_empty(&ep->queue)) {
1083 channel = ep->dma_channel;
1084 dma_channel_release(ep);
1088 use_ep(ep, UDC_EP_SEL);
1089 if (omap_readw(UDC_STAT_FLG) & UDC_NON_ISO_FIFO_EMPTY) {
1090 omap_writew(UDC_SET_HALT, UDC_CTRL);
1097 dma_channel_claim(ep, channel);
1100 omap_writew(ep->udc->clr_halt, UDC_CTRL);
1102 if (!(ep->bEndpointAddress & USB_DIR_IN)) {
1103 omap_writew(UDC_SET_FIFO_EN, UDC_CTRL);
1104 ep->ackwait = 1 + ep->double_buf;
1109 VDBG("%s %s halt stat %d\n", ep->ep.name,
1110 value ? "set" : "clear", status);
1112 spin_unlock_irqrestore(&ep->udc->lock, flags);
1116 static const struct usb_ep_ops omap_ep_ops = {
1117 .enable = omap_ep_enable,
1118 .disable = omap_ep_disable,
1120 .alloc_request = omap_alloc_request,
1121 .free_request = omap_free_request,
1123 .queue = omap_ep_queue,
1124 .dequeue = omap_ep_dequeue,
1126 .set_halt = omap_ep_set_halt,
1127 /* fifo_status ... report bytes in fifo */
1128 /* fifo_flush ... flush fifo */
1131 /*-------------------------------------------------------------------------*/
1133 static int omap_get_frame(struct usb_gadget *gadget)
1135 u16 sof = omap_readw(UDC_SOF);
1136 return (sof & UDC_TS_OK) ? (sof & UDC_TS) : -EL2NSYNC;
1139 static int omap_wakeup(struct usb_gadget *gadget)
1141 struct omap_udc *udc;
1142 unsigned long flags;
1143 int retval = -EHOSTUNREACH;
1145 udc = container_of(gadget, struct omap_udc, gadget);
1147 spin_lock_irqsave(&udc->lock, flags);
1148 if (udc->devstat & UDC_SUS) {
1149 /* NOTE: OTG spec erratum says that OTG devices may
1150 * issue wakeups without host enable.
1152 if (udc->devstat & (UDC_B_HNP_ENABLE|UDC_R_WK_OK)) {
1153 DBG("remote wakeup...\n");
1154 omap_writew(UDC_RMT_WKP, UDC_SYSCON2);
1158 /* NOTE: non-OTG systems may use SRP TOO... */
1159 } else if (!(udc->devstat & UDC_ATT)) {
1160 if (!IS_ERR_OR_NULL(udc->transceiver))
1161 retval = otg_start_srp(udc->transceiver->otg);
1163 spin_unlock_irqrestore(&udc->lock, flags);
1169 omap_set_selfpowered(struct usb_gadget *gadget, int is_selfpowered)
1171 struct omap_udc *udc;
1172 unsigned long flags;
1175 gadget->is_selfpowered = (is_selfpowered != 0);
1176 udc = container_of(gadget, struct omap_udc, gadget);
1177 spin_lock_irqsave(&udc->lock, flags);
1178 syscon1 = omap_readw(UDC_SYSCON1);
1180 syscon1 |= UDC_SELF_PWR;
1182 syscon1 &= ~UDC_SELF_PWR;
1183 omap_writew(syscon1, UDC_SYSCON1);
1184 spin_unlock_irqrestore(&udc->lock, flags);
1189 static int can_pullup(struct omap_udc *udc)
1191 return udc->driver && udc->softconnect && udc->vbus_active;
1194 static void pullup_enable(struct omap_udc *udc)
1198 w = omap_readw(UDC_SYSCON1);
1200 omap_writew(w, UDC_SYSCON1);
1201 if (!gadget_is_otg(&udc->gadget) && !cpu_is_omap15xx()) {
1204 l = omap_readl(OTG_CTRL);
1206 omap_writel(l, OTG_CTRL);
1208 omap_writew(UDC_DS_CHG_IE, UDC_IRQ_EN);
1211 static void pullup_disable(struct omap_udc *udc)
1215 if (!gadget_is_otg(&udc->gadget) && !cpu_is_omap15xx()) {
1218 l = omap_readl(OTG_CTRL);
1220 omap_writel(l, OTG_CTRL);
1222 omap_writew(UDC_DS_CHG_IE, UDC_IRQ_EN);
1223 w = omap_readw(UDC_SYSCON1);
1224 w &= ~UDC_PULLUP_EN;
1225 omap_writew(w, UDC_SYSCON1);
1228 static struct omap_udc *udc;
1230 static void omap_udc_enable_clock(int enable)
1232 if (udc == NULL || udc->dc_clk == NULL || udc->hhc_clk == NULL)
1236 clk_enable(udc->dc_clk);
1237 clk_enable(udc->hhc_clk);
1240 clk_disable(udc->hhc_clk);
1241 clk_disable(udc->dc_clk);
1246 * Called by whatever detects VBUS sessions: external transceiver
1247 * driver, or maybe GPIO0 VBUS IRQ. May request 48 MHz clock.
1249 static int omap_vbus_session(struct usb_gadget *gadget, int is_active)
1251 struct omap_udc *udc;
1252 unsigned long flags;
1255 udc = container_of(gadget, struct omap_udc, gadget);
1256 spin_lock_irqsave(&udc->lock, flags);
1257 VDBG("VBUS %s\n", is_active ? "on" : "off");
1258 udc->vbus_active = (is_active != 0);
1259 if (cpu_is_omap15xx()) {
1260 /* "software" detect, ignored if !VBUS_MODE_1510 */
1261 l = omap_readl(FUNC_MUX_CTRL_0);
1263 l |= VBUS_CTRL_1510;
1265 l &= ~VBUS_CTRL_1510;
1266 omap_writel(l, FUNC_MUX_CTRL_0);
1268 if (udc->dc_clk != NULL && is_active) {
1269 if (!udc->clk_requested) {
1270 omap_udc_enable_clock(1);
1271 udc->clk_requested = 1;
1274 if (can_pullup(udc))
1277 pullup_disable(udc);
1278 if (udc->dc_clk != NULL && !is_active) {
1279 if (udc->clk_requested) {
1280 omap_udc_enable_clock(0);
1281 udc->clk_requested = 0;
1284 spin_unlock_irqrestore(&udc->lock, flags);
1288 static int omap_vbus_draw(struct usb_gadget *gadget, unsigned mA)
1290 struct omap_udc *udc;
1292 udc = container_of(gadget, struct omap_udc, gadget);
1293 if (!IS_ERR_OR_NULL(udc->transceiver))
1294 return usb_phy_set_power(udc->transceiver, mA);
1298 static int omap_pullup(struct usb_gadget *gadget, int is_on)
1300 struct omap_udc *udc;
1301 unsigned long flags;
1303 udc = container_of(gadget, struct omap_udc, gadget);
1304 spin_lock_irqsave(&udc->lock, flags);
1305 udc->softconnect = (is_on != 0);
1306 if (can_pullup(udc))
1309 pullup_disable(udc);
1310 spin_unlock_irqrestore(&udc->lock, flags);
1314 static int omap_udc_start(struct usb_gadget *g,
1315 struct usb_gadget_driver *driver);
1316 static int omap_udc_stop(struct usb_gadget *g);
1318 static const struct usb_gadget_ops omap_gadget_ops = {
1319 .get_frame = omap_get_frame,
1320 .wakeup = omap_wakeup,
1321 .set_selfpowered = omap_set_selfpowered,
1322 .vbus_session = omap_vbus_session,
1323 .vbus_draw = omap_vbus_draw,
1324 .pullup = omap_pullup,
1325 .udc_start = omap_udc_start,
1326 .udc_stop = omap_udc_stop,
1329 /*-------------------------------------------------------------------------*/
1331 /* dequeue ALL requests; caller holds udc->lock */
1332 static void nuke(struct omap_ep *ep, int status)
1334 struct omap_req *req;
1338 if (use_dma && ep->dma_channel)
1339 dma_channel_release(ep);
1342 omap_writew(UDC_CLR_EP, UDC_CTRL);
1343 if (ep->bEndpointAddress && ep->bmAttributes != USB_ENDPOINT_XFER_ISOC)
1344 omap_writew(UDC_SET_HALT, UDC_CTRL);
1346 while (!list_empty(&ep->queue)) {
1347 req = list_entry(ep->queue.next, struct omap_req, queue);
1348 done(ep, req, status);
1352 /* caller holds udc->lock */
1353 static void udc_quiesce(struct omap_udc *udc)
1357 udc->gadget.speed = USB_SPEED_UNKNOWN;
1358 nuke(&udc->ep[0], -ESHUTDOWN);
1359 list_for_each_entry(ep, &udc->gadget.ep_list, ep.ep_list)
1360 nuke(ep, -ESHUTDOWN);
1363 /*-------------------------------------------------------------------------*/
1365 static void update_otg(struct omap_udc *udc)
1369 if (!gadget_is_otg(&udc->gadget))
1372 if (omap_readl(OTG_CTRL) & OTG_ID)
1373 devstat = omap_readw(UDC_DEVSTAT);
1377 udc->gadget.b_hnp_enable = !!(devstat & UDC_B_HNP_ENABLE);
1378 udc->gadget.a_hnp_support = !!(devstat & UDC_A_HNP_SUPPORT);
1379 udc->gadget.a_alt_hnp_support = !!(devstat & UDC_A_ALT_HNP_SUPPORT);
1381 /* Enable HNP early, avoiding races on suspend irq path.
1382 * ASSUMES OTG state machine B_BUS_REQ input is true.
1384 if (udc->gadget.b_hnp_enable) {
1387 l = omap_readl(OTG_CTRL);
1388 l |= OTG_B_HNPEN | OTG_B_BUSREQ;
1390 omap_writel(l, OTG_CTRL);
1394 static void ep0_irq(struct omap_udc *udc, u16 irq_src)
1396 struct omap_ep *ep0 = &udc->ep[0];
1397 struct omap_req *req = NULL;
1401 /* Clear any pending requests and then scrub any rx/tx state
1402 * before starting to handle the SETUP request.
1404 if (irq_src & UDC_SETUP) {
1405 u16 ack = irq_src & (UDC_EP0_TX|UDC_EP0_RX);
1409 omap_writew(ack, UDC_IRQ_SRC);
1410 irq_src = UDC_SETUP;
1414 /* IN/OUT packets mean we're in the DATA or STATUS stage.
1415 * This driver uses only uses protocol stalls (ep0 never halts),
1416 * and if we got this far the gadget driver already had a
1417 * chance to stall. Tries to be forgiving of host oddities.
1419 * NOTE: the last chance gadget drivers have to stall control
1420 * requests is during their request completion callback.
1422 if (!list_empty(&ep0->queue))
1423 req = container_of(ep0->queue.next, struct omap_req, queue);
1425 /* IN == TX to host */
1426 if (irq_src & UDC_EP0_TX) {
1429 omap_writew(UDC_EP0_TX, UDC_IRQ_SRC);
1430 omap_writew(UDC_EP_SEL|UDC_EP_DIR, UDC_EP_NUM);
1431 stat = omap_readw(UDC_STAT_FLG);
1432 if (stat & UDC_ACK) {
1434 /* write next IN packet from response,
1435 * or set up the status stage.
1438 stat = write_fifo(ep0, req);
1439 omap_writew(UDC_EP_DIR, UDC_EP_NUM);
1440 if (!req && udc->ep0_pending) {
1441 omap_writew(UDC_EP_SEL, UDC_EP_NUM);
1442 omap_writew(UDC_CLR_EP, UDC_CTRL);
1443 omap_writew(UDC_SET_FIFO_EN, UDC_CTRL);
1444 omap_writew(0, UDC_EP_NUM);
1445 udc->ep0_pending = 0;
1446 } /* else: 6 wait states before it'll tx */
1448 /* ack status stage of OUT transfer */
1449 omap_writew(UDC_EP_DIR, UDC_EP_NUM);
1454 } else if (stat & UDC_STALL) {
1455 omap_writew(UDC_CLR_HALT, UDC_CTRL);
1456 omap_writew(UDC_EP_DIR, UDC_EP_NUM);
1458 omap_writew(UDC_EP_DIR, UDC_EP_NUM);
1462 /* OUT == RX from host */
1463 if (irq_src & UDC_EP0_RX) {
1466 omap_writew(UDC_EP0_RX, UDC_IRQ_SRC);
1467 omap_writew(UDC_EP_SEL, UDC_EP_NUM);
1468 stat = omap_readw(UDC_STAT_FLG);
1469 if (stat & UDC_ACK) {
1472 /* read next OUT packet of request, maybe
1473 * reactivating the fifo; stall on errors.
1475 stat = read_fifo(ep0, req);
1476 if (!req || stat < 0) {
1477 omap_writew(UDC_STALL_CMD, UDC_SYSCON2);
1478 udc->ep0_pending = 0;
1480 } else if (stat == 0)
1481 omap_writew(UDC_SET_FIFO_EN, UDC_CTRL);
1482 omap_writew(0, UDC_EP_NUM);
1484 /* activate status stage */
1487 /* that may have STALLed ep0... */
1488 omap_writew(UDC_EP_SEL | UDC_EP_DIR,
1490 omap_writew(UDC_CLR_EP, UDC_CTRL);
1491 omap_writew(UDC_SET_FIFO_EN, UDC_CTRL);
1492 omap_writew(UDC_EP_DIR, UDC_EP_NUM);
1493 udc->ep0_pending = 0;
1496 /* ack status stage of IN transfer */
1497 omap_writew(0, UDC_EP_NUM);
1501 } else if (stat & UDC_STALL) {
1502 omap_writew(UDC_CLR_HALT, UDC_CTRL);
1503 omap_writew(0, UDC_EP_NUM);
1505 omap_writew(0, UDC_EP_NUM);
1509 /* SETUP starts all control transfers */
1510 if (irq_src & UDC_SETUP) {
1513 struct usb_ctrlrequest r;
1515 int status = -EINVAL;
1518 /* read the (latest) SETUP message */
1520 omap_writew(UDC_SETUP_SEL, UDC_EP_NUM);
1521 /* two bytes at a time */
1522 u.word[0] = omap_readw(UDC_DATA);
1523 u.word[1] = omap_readw(UDC_DATA);
1524 u.word[2] = omap_readw(UDC_DATA);
1525 u.word[3] = omap_readw(UDC_DATA);
1526 omap_writew(0, UDC_EP_NUM);
1527 } while (omap_readw(UDC_IRQ_SRC) & UDC_SETUP);
1529 #define w_value le16_to_cpu(u.r.wValue)
1530 #define w_index le16_to_cpu(u.r.wIndex)
1531 #define w_length le16_to_cpu(u.r.wLength)
1533 /* Delegate almost all control requests to the gadget driver,
1534 * except for a handful of ch9 status/feature requests that
1535 * hardware doesn't autodecode _and_ the gadget API hides.
1537 udc->ep0_in = (u.r.bRequestType & USB_DIR_IN) != 0;
1538 udc->ep0_set_config = 0;
1539 udc->ep0_pending = 1;
1542 switch (u.r.bRequest) {
1543 case USB_REQ_SET_CONFIGURATION:
1544 /* udc needs to know when ep != 0 is valid */
1545 if (u.r.bRequestType != USB_RECIP_DEVICE)
1549 udc->ep0_set_config = 1;
1550 udc->ep0_reset_config = (w_value == 0);
1551 VDBG("set config %d\n", w_value);
1553 /* update udc NOW since gadget driver may start
1554 * queueing requests immediately; clear config
1555 * later if it fails the request.
1557 if (udc->ep0_reset_config)
1558 omap_writew(UDC_CLR_CFG, UDC_SYSCON2);
1560 omap_writew(UDC_DEV_CFG, UDC_SYSCON2);
1563 case USB_REQ_CLEAR_FEATURE:
1564 /* clear endpoint halt */
1565 if (u.r.bRequestType != USB_RECIP_ENDPOINT)
1567 if (w_value != USB_ENDPOINT_HALT
1570 ep = &udc->ep[w_index & 0xf];
1572 if (w_index & USB_DIR_IN)
1574 if (ep->bmAttributes == USB_ENDPOINT_XFER_ISOC
1578 omap_writew(udc->clr_halt, UDC_CTRL);
1580 if (!(ep->bEndpointAddress & USB_DIR_IN)) {
1581 omap_writew(UDC_SET_FIFO_EN, UDC_CTRL);
1582 ep->ackwait = 1 + ep->double_buf;
1584 /* NOTE: assumes the host behaves sanely,
1585 * only clearing real halts. Else we may
1586 * need to kill pending transfers and then
1587 * restart the queue... very messy for DMA!
1590 VDBG("%s halt cleared by host\n", ep->name);
1591 goto ep0out_status_stage;
1592 case USB_REQ_SET_FEATURE:
1593 /* set endpoint halt */
1594 if (u.r.bRequestType != USB_RECIP_ENDPOINT)
1596 if (w_value != USB_ENDPOINT_HALT
1599 ep = &udc->ep[w_index & 0xf];
1600 if (w_index & USB_DIR_IN)
1602 if (ep->bmAttributes == USB_ENDPOINT_XFER_ISOC
1603 || ep == ep0 || !ep->ep.desc)
1605 if (use_dma && ep->has_dma) {
1606 /* this has rude side-effects (aborts) and
1607 * can't really work if DMA-IN is active
1609 DBG("%s host set_halt, NYET\n", ep->name);
1613 /* can't halt if fifo isn't empty... */
1614 omap_writew(UDC_CLR_EP, UDC_CTRL);
1615 omap_writew(UDC_SET_HALT, UDC_CTRL);
1616 VDBG("%s halted by host\n", ep->name);
1617 ep0out_status_stage:
1619 omap_writew(UDC_EP_SEL|UDC_EP_DIR, UDC_EP_NUM);
1620 omap_writew(UDC_CLR_EP, UDC_CTRL);
1621 omap_writew(UDC_SET_FIFO_EN, UDC_CTRL);
1622 omap_writew(UDC_EP_DIR, UDC_EP_NUM);
1623 udc->ep0_pending = 0;
1625 case USB_REQ_GET_STATUS:
1626 /* USB_ENDPOINT_HALT status? */
1627 if (u.r.bRequestType != (USB_DIR_IN|USB_RECIP_ENDPOINT))
1630 /* ep0 never stalls */
1631 if (!(w_index & 0xf))
1634 /* only active endpoints count */
1635 ep = &udc->ep[w_index & 0xf];
1636 if (w_index & USB_DIR_IN)
1641 /* iso never stalls */
1642 if (ep->bmAttributes == USB_ENDPOINT_XFER_ISOC)
1645 /* FIXME don't assume non-halted endpoints!! */
1646 ERR("%s status, can't report\n", ep->ep.name);
1650 /* return interface status. if we were pedantic,
1651 * we'd detect non-existent interfaces, and stall.
1653 if (u.r.bRequestType
1654 != (USB_DIR_IN|USB_RECIP_INTERFACE))
1658 /* return two zero bytes */
1659 omap_writew(UDC_EP_SEL|UDC_EP_DIR, UDC_EP_NUM);
1660 omap_writew(0, UDC_DATA);
1661 omap_writew(UDC_SET_FIFO_EN, UDC_CTRL);
1662 omap_writew(UDC_EP_DIR, UDC_EP_NUM);
1664 VDBG("GET_STATUS, interface %d\n", w_index);
1665 /* next, status stage */
1669 /* activate the ep0out fifo right away */
1670 if (!udc->ep0_in && w_length) {
1671 omap_writew(0, UDC_EP_NUM);
1672 omap_writew(UDC_SET_FIFO_EN, UDC_CTRL);
1675 /* gadget drivers see class/vendor specific requests,
1676 * {SET,GET}_{INTERFACE,DESCRIPTOR,CONFIGURATION},
1679 VDBG("SETUP %02x.%02x v%04x i%04x l%04x\n",
1680 u.r.bRequestType, u.r.bRequest,
1681 w_value, w_index, w_length);
1687 /* The gadget driver may return an error here,
1688 * causing an immediate protocol stall.
1690 * Else it must issue a response, either queueing a
1691 * response buffer for the DATA stage, or halting ep0
1692 * (causing a protocol stall, not a real halt). A
1693 * zero length buffer means no DATA stage.
1695 * It's fine to issue that response after the setup()
1696 * call returns, and this IRQ was handled.
1699 spin_unlock(&udc->lock);
1700 status = udc->driver->setup(&udc->gadget, &u.r);
1701 spin_lock(&udc->lock);
1707 VDBG("req %02x.%02x protocol STALL; stat %d\n",
1708 u.r.bRequestType, u.r.bRequest, status);
1709 if (udc->ep0_set_config) {
1710 if (udc->ep0_reset_config)
1711 WARNING("error resetting config?\n");
1713 omap_writew(UDC_CLR_CFG, UDC_SYSCON2);
1715 omap_writew(UDC_STALL_CMD, UDC_SYSCON2);
1716 udc->ep0_pending = 0;
1721 /*-------------------------------------------------------------------------*/
1723 #define OTG_FLAGS (UDC_B_HNP_ENABLE|UDC_A_HNP_SUPPORT|UDC_A_ALT_HNP_SUPPORT)
1725 static void devstate_irq(struct omap_udc *udc, u16 irq_src)
1727 u16 devstat, change;
1729 devstat = omap_readw(UDC_DEVSTAT);
1730 change = devstat ^ udc->devstat;
1731 udc->devstat = devstat;
1733 if (change & (UDC_USB_RESET|UDC_ATT)) {
1736 if (change & UDC_ATT) {
1737 /* driver for any external transceiver will
1738 * have called omap_vbus_session() already
1740 if (devstat & UDC_ATT) {
1741 udc->gadget.speed = USB_SPEED_FULL;
1743 if (IS_ERR_OR_NULL(udc->transceiver))
1745 /* if (driver->connect) call it */
1746 } else if (udc->gadget.speed != USB_SPEED_UNKNOWN) {
1747 udc->gadget.speed = USB_SPEED_UNKNOWN;
1748 if (IS_ERR_OR_NULL(udc->transceiver))
1749 pullup_disable(udc);
1750 DBG("disconnect, gadget %s\n",
1751 udc->driver->driver.name);
1752 if (udc->driver->disconnect) {
1753 spin_unlock(&udc->lock);
1754 udc->driver->disconnect(&udc->gadget);
1755 spin_lock(&udc->lock);
1761 if (change & UDC_USB_RESET) {
1762 if (devstat & UDC_USB_RESET) {
1765 udc->gadget.speed = USB_SPEED_FULL;
1766 INFO("USB reset done, gadget %s\n",
1767 udc->driver->driver.name);
1768 /* ep0 traffic is legal from now on */
1769 omap_writew(UDC_DS_CHG_IE | UDC_EP0_IE,
1772 change &= ~UDC_USB_RESET;
1775 if (change & UDC_SUS) {
1776 if (udc->gadget.speed != USB_SPEED_UNKNOWN) {
1777 /* FIXME tell isp1301 to suspend/resume (?) */
1778 if (devstat & UDC_SUS) {
1781 /* HNP could be under way already */
1782 if (udc->gadget.speed == USB_SPEED_FULL
1783 && udc->driver->suspend) {
1784 spin_unlock(&udc->lock);
1785 udc->driver->suspend(&udc->gadget);
1786 spin_lock(&udc->lock);
1788 if (!IS_ERR_OR_NULL(udc->transceiver))
1789 usb_phy_set_suspend(
1790 udc->transceiver, 1);
1793 if (!IS_ERR_OR_NULL(udc->transceiver))
1794 usb_phy_set_suspend(
1795 udc->transceiver, 0);
1796 if (udc->gadget.speed == USB_SPEED_FULL
1797 && udc->driver->resume) {
1798 spin_unlock(&udc->lock);
1799 udc->driver->resume(&udc->gadget);
1800 spin_lock(&udc->lock);
1806 if (!cpu_is_omap15xx() && (change & OTG_FLAGS)) {
1808 change &= ~OTG_FLAGS;
1811 change &= ~(UDC_CFG|UDC_DEF|UDC_ADD);
1813 VDBG("devstat %03x, ignore change %03x\n",
1816 omap_writew(UDC_DS_CHG, UDC_IRQ_SRC);
1819 static irqreturn_t omap_udc_irq(int irq, void *_udc)
1821 struct omap_udc *udc = _udc;
1823 irqreturn_t status = IRQ_NONE;
1824 unsigned long flags;
1826 spin_lock_irqsave(&udc->lock, flags);
1827 irq_src = omap_readw(UDC_IRQ_SRC);
1829 /* Device state change (usb ch9 stuff) */
1830 if (irq_src & UDC_DS_CHG) {
1831 devstate_irq(_udc, irq_src);
1832 status = IRQ_HANDLED;
1833 irq_src &= ~UDC_DS_CHG;
1836 /* EP0 control transfers */
1837 if (irq_src & (UDC_EP0_RX|UDC_SETUP|UDC_EP0_TX)) {
1838 ep0_irq(_udc, irq_src);
1839 status = IRQ_HANDLED;
1840 irq_src &= ~(UDC_EP0_RX|UDC_SETUP|UDC_EP0_TX);
1843 /* DMA transfer completion */
1844 if (use_dma && (irq_src & (UDC_TXN_DONE|UDC_RXN_CNT|UDC_RXN_EOT))) {
1845 dma_irq(_udc, irq_src);
1846 status = IRQ_HANDLED;
1847 irq_src &= ~(UDC_TXN_DONE|UDC_RXN_CNT|UDC_RXN_EOT);
1850 irq_src &= ~(UDC_IRQ_SOF | UDC_EPN_TX|UDC_EPN_RX);
1852 DBG("udc_irq, unhandled %03x\n", irq_src);
1853 spin_unlock_irqrestore(&udc->lock, flags);
1858 /* workaround for seemingly-lost IRQs for RX ACKs... */
1859 #define PIO_OUT_TIMEOUT (jiffies + HZ/3)
1860 #define HALF_FULL(f) (!((f)&(UDC_NON_ISO_FIFO_FULL|UDC_NON_ISO_FIFO_EMPTY)))
1862 static void pio_out_timer(struct timer_list *t)
1864 struct omap_ep *ep = from_timer(ep, t, timer);
1865 unsigned long flags;
1868 spin_lock_irqsave(&ep->udc->lock, flags);
1869 if (!list_empty(&ep->queue) && ep->ackwait) {
1870 use_ep(ep, UDC_EP_SEL);
1871 stat_flg = omap_readw(UDC_STAT_FLG);
1873 if ((stat_flg & UDC_ACK) && (!(stat_flg & UDC_FIFO_EN)
1874 || (ep->double_buf && HALF_FULL(stat_flg)))) {
1875 struct omap_req *req;
1877 VDBG("%s: lose, %04x\n", ep->ep.name, stat_flg);
1878 req = container_of(ep->queue.next,
1879 struct omap_req, queue);
1880 (void) read_fifo(ep, req);
1881 omap_writew(ep->bEndpointAddress, UDC_EP_NUM);
1882 omap_writew(UDC_SET_FIFO_EN, UDC_CTRL);
1883 ep->ackwait = 1 + ep->double_buf;
1887 mod_timer(&ep->timer, PIO_OUT_TIMEOUT);
1888 spin_unlock_irqrestore(&ep->udc->lock, flags);
1891 static irqreturn_t omap_udc_pio_irq(int irq, void *_dev)
1893 u16 epn_stat, irq_src;
1894 irqreturn_t status = IRQ_NONE;
1897 struct omap_udc *udc = _dev;
1898 struct omap_req *req;
1899 unsigned long flags;
1901 spin_lock_irqsave(&udc->lock, flags);
1902 epn_stat = omap_readw(UDC_EPN_STAT);
1903 irq_src = omap_readw(UDC_IRQ_SRC);
1905 /* handle OUT first, to avoid some wasteful NAKs */
1906 if (irq_src & UDC_EPN_RX) {
1907 epnum = (epn_stat >> 8) & 0x0f;
1908 omap_writew(UDC_EPN_RX, UDC_IRQ_SRC);
1909 status = IRQ_HANDLED;
1910 ep = &udc->ep[epnum];
1913 omap_writew(epnum | UDC_EP_SEL, UDC_EP_NUM);
1915 if (omap_readw(UDC_STAT_FLG) & UDC_ACK) {
1917 if (!list_empty(&ep->queue)) {
1919 req = container_of(ep->queue.next,
1920 struct omap_req, queue);
1921 stat = read_fifo(ep, req);
1922 if (!ep->double_buf)
1926 /* min 6 clock delay before clearing EP_SEL ... */
1927 epn_stat = omap_readw(UDC_EPN_STAT);
1928 epn_stat = omap_readw(UDC_EPN_STAT);
1929 omap_writew(epnum, UDC_EP_NUM);
1931 /* enabling fifo _after_ clearing ACK, contrary to docs,
1932 * reduces lossage; timer still needed though (sigh).
1935 omap_writew(UDC_SET_FIFO_EN, UDC_CTRL);
1936 ep->ackwait = 1 + ep->double_buf;
1938 mod_timer(&ep->timer, PIO_OUT_TIMEOUT);
1941 /* then IN transfers */
1942 else if (irq_src & UDC_EPN_TX) {
1943 epnum = epn_stat & 0x0f;
1944 omap_writew(UDC_EPN_TX, UDC_IRQ_SRC);
1945 status = IRQ_HANDLED;
1946 ep = &udc->ep[16 + epnum];
1949 omap_writew(epnum | UDC_EP_DIR | UDC_EP_SEL, UDC_EP_NUM);
1950 if (omap_readw(UDC_STAT_FLG) & UDC_ACK) {
1952 if (!list_empty(&ep->queue)) {
1953 req = container_of(ep->queue.next,
1954 struct omap_req, queue);
1955 (void) write_fifo(ep, req);
1958 /* min 6 clock delay before clearing EP_SEL ... */
1959 epn_stat = omap_readw(UDC_EPN_STAT);
1960 epn_stat = omap_readw(UDC_EPN_STAT);
1961 omap_writew(epnum | UDC_EP_DIR, UDC_EP_NUM);
1962 /* then 6 clocks before it'd tx */
1965 spin_unlock_irqrestore(&udc->lock, flags);
1970 static irqreturn_t omap_udc_iso_irq(int irq, void *_dev)
1972 struct omap_udc *udc = _dev;
1975 unsigned long flags;
1977 spin_lock_irqsave(&udc->lock, flags);
1979 /* handle all non-DMA ISO transfers */
1980 list_for_each_entry(ep, &udc->iso, iso) {
1982 struct omap_req *req;
1984 if (ep->has_dma || list_empty(&ep->queue))
1986 req = list_entry(ep->queue.next, struct omap_req, queue);
1988 use_ep(ep, UDC_EP_SEL);
1989 stat = omap_readw(UDC_STAT_FLG);
1991 /* NOTE: like the other controller drivers, this isn't
1992 * currently reporting lost or damaged frames.
1994 if (ep->bEndpointAddress & USB_DIR_IN) {
1995 if (stat & UDC_MISS_IN)
1996 /* done(ep, req, -EPROTO) */;
1998 write_fifo(ep, req);
2002 if (stat & UDC_NO_RXPACKET)
2003 status = -EREMOTEIO;
2004 else if (stat & UDC_ISO_ERR)
2006 else if (stat & UDC_DATA_FLUSH)
2010 /* done(ep, req, status) */;
2015 /* 6 wait states before next EP */
2018 if (!list_empty(&ep->queue))
2024 w = omap_readw(UDC_IRQ_EN);
2026 omap_writew(w, UDC_IRQ_EN);
2028 omap_writew(UDC_IRQ_SOF, UDC_IRQ_SRC);
2030 spin_unlock_irqrestore(&udc->lock, flags);
2035 /*-------------------------------------------------------------------------*/
2037 static inline int machine_without_vbus_sense(void)
2039 return machine_is_omap_osk() || machine_is_sx1();
2042 static int omap_udc_start(struct usb_gadget *g,
2043 struct usb_gadget_driver *driver)
2047 unsigned long flags;
2050 spin_lock_irqsave(&udc->lock, flags);
2052 list_for_each_entry(ep, &udc->gadget.ep_list, ep.ep_list) {
2054 if (ep->bmAttributes == USB_ENDPOINT_XFER_ISOC)
2057 omap_writew(UDC_SET_HALT, UDC_CTRL);
2059 udc->ep0_pending = 0;
2060 udc->ep[0].irqs = 0;
2061 udc->softconnect = 1;
2063 /* hook up the driver */
2064 udc->driver = driver;
2065 spin_unlock_irqrestore(&udc->lock, flags);
2067 if (udc->dc_clk != NULL)
2068 omap_udc_enable_clock(1);
2070 omap_writew(UDC_IRQ_SRC_MASK, UDC_IRQ_SRC);
2072 /* connect to bus through transceiver */
2073 if (!IS_ERR_OR_NULL(udc->transceiver)) {
2074 status = otg_set_peripheral(udc->transceiver->otg,
2077 ERR("can't bind to transceiver\n");
2083 if (can_pullup(udc))
2086 pullup_disable(udc);
2089 /* boards that don't have VBUS sensing can't autogate 48MHz;
2090 * can't enter deep sleep while a gadget driver is active.
2092 if (machine_without_vbus_sense())
2093 omap_vbus_session(&udc->gadget, 1);
2096 if (udc->dc_clk != NULL)
2097 omap_udc_enable_clock(0);
2102 static int omap_udc_stop(struct usb_gadget *g)
2104 unsigned long flags;
2106 if (udc->dc_clk != NULL)
2107 omap_udc_enable_clock(1);
2109 if (machine_without_vbus_sense())
2110 omap_vbus_session(&udc->gadget, 0);
2112 if (!IS_ERR_OR_NULL(udc->transceiver))
2113 (void) otg_set_peripheral(udc->transceiver->otg, NULL);
2115 pullup_disable(udc);
2117 spin_lock_irqsave(&udc->lock, flags);
2119 spin_unlock_irqrestore(&udc->lock, flags);
2123 if (udc->dc_clk != NULL)
2124 omap_udc_enable_clock(0);
2129 /*-------------------------------------------------------------------------*/
2131 #ifdef CONFIG_USB_GADGET_DEBUG_FILES
2133 #include <linux/seq_file.h>
2135 static const char proc_filename[] = "driver/udc";
2137 #define FOURBITS "%s%s%s%s"
2138 #define EIGHTBITS "%s%s%s%s%s%s%s%s"
2140 static void proc_ep_show(struct seq_file *s, struct omap_ep *ep)
2143 struct omap_req *req;
2148 if (use_dma && ep->has_dma)
2149 snprintf(buf, sizeof buf, "(%cxdma%d lch%d) ",
2150 (ep->bEndpointAddress & USB_DIR_IN) ? 't' : 'r',
2151 ep->dma_channel - 1, ep->lch);
2155 stat_flg = omap_readw(UDC_STAT_FLG);
2157 "\n%s %s%s%sirqs %ld stat %04x " EIGHTBITS FOURBITS "%s\n",
2159 ep->double_buf ? "dbuf " : "",
2161 switch (ep->ackwait) {
2176 (stat_flg & UDC_NO_RXPACKET) ? "no_rxpacket " : "",
2177 (stat_flg & UDC_MISS_IN) ? "miss_in " : "",
2178 (stat_flg & UDC_DATA_FLUSH) ? "data_flush " : "",
2179 (stat_flg & UDC_ISO_ERR) ? "iso_err " : "",
2180 (stat_flg & UDC_ISO_FIFO_EMPTY) ? "iso_fifo_empty " : "",
2181 (stat_flg & UDC_ISO_FIFO_FULL) ? "iso_fifo_full " : "",
2182 (stat_flg & UDC_EP_HALTED) ? "HALT " : "",
2183 (stat_flg & UDC_STALL) ? "STALL " : "",
2184 (stat_flg & UDC_NAK) ? "NAK " : "",
2185 (stat_flg & UDC_ACK) ? "ACK " : "",
2186 (stat_flg & UDC_FIFO_EN) ? "fifo_en " : "",
2187 (stat_flg & UDC_NON_ISO_FIFO_EMPTY) ? "fifo_empty " : "",
2188 (stat_flg & UDC_NON_ISO_FIFO_FULL) ? "fifo_full " : "");
2190 if (list_empty(&ep->queue))
2191 seq_printf(s, "\t(queue empty)\n");
2193 list_for_each_entry(req, &ep->queue, queue) {
2194 unsigned length = req->req.actual;
2196 if (use_dma && buf[0]) {
2197 length += ((ep->bEndpointAddress & USB_DIR_IN)
2198 ? dma_src_len : dma_dest_len)
2199 (ep, req->req.dma + length);
2202 seq_printf(s, "\treq %p len %d/%d buf %p\n",
2204 req->req.length, req->req.buf);
2208 static char *trx_mode(unsigned m, int enabled)
2212 return enabled ? "*6wire" : "unused";
2224 static int proc_otg_show(struct seq_file *s)
2228 char *ctrl_name = "(UNKNOWN)";
2230 tmp = omap_readl(OTG_REV);
2231 ctrl_name = "transceiver_ctrl";
2232 trans = omap_readw(USB_TRANSCEIVER_CTRL);
2233 seq_printf(s, "\nOTG rev %d.%d, %s %05x\n",
2234 tmp >> 4, tmp & 0xf, ctrl_name, trans);
2235 tmp = omap_readw(OTG_SYSCON_1);
2236 seq_printf(s, "otg_syscon1 %08x usb2 %s, usb1 %s, usb0 %s,"
2238 trx_mode(USB2_TRX_MODE(tmp), trans & CONF_USB2_UNI_R),
2239 trx_mode(USB1_TRX_MODE(tmp), trans & CONF_USB1_UNI_R),
2240 (USB0_TRX_MODE(tmp) == 0 && !cpu_is_omap1710())
2242 : trx_mode(USB0_TRX_MODE(tmp), 1),
2243 (tmp & OTG_IDLE_EN) ? " !otg" : "",
2244 (tmp & HST_IDLE_EN) ? " !host" : "",
2245 (tmp & DEV_IDLE_EN) ? " !dev" : "",
2246 (tmp & OTG_RESET_DONE) ? " reset_done" : " reset_active");
2247 tmp = omap_readl(OTG_SYSCON_2);
2248 seq_printf(s, "otg_syscon2 %08x%s" EIGHTBITS
2249 " b_ase_brst=%d hmc=%d\n", tmp,
2250 (tmp & OTG_EN) ? " otg_en" : "",
2251 (tmp & USBX_SYNCHRO) ? " synchro" : "",
2252 /* much more SRP stuff */
2253 (tmp & SRP_DATA) ? " srp_data" : "",
2254 (tmp & SRP_VBUS) ? " srp_vbus" : "",
2255 (tmp & OTG_PADEN) ? " otg_paden" : "",
2256 (tmp & HMC_PADEN) ? " hmc_paden" : "",
2257 (tmp & UHOST_EN) ? " uhost_en" : "",
2258 (tmp & HMC_TLLSPEED) ? " tllspeed" : "",
2259 (tmp & HMC_TLLATTACH) ? " tllattach" : "",
2262 tmp = omap_readl(OTG_CTRL);
2263 seq_printf(s, "otg_ctrl %06x" EIGHTBITS EIGHTBITS "%s\n", tmp,
2264 (tmp & OTG_ASESSVLD) ? " asess" : "",
2265 (tmp & OTG_BSESSEND) ? " bsess_end" : "",
2266 (tmp & OTG_BSESSVLD) ? " bsess" : "",
2267 (tmp & OTG_VBUSVLD) ? " vbus" : "",
2268 (tmp & OTG_ID) ? " id" : "",
2269 (tmp & OTG_DRIVER_SEL) ? " DEVICE" : " HOST",
2270 (tmp & OTG_A_SETB_HNPEN) ? " a_setb_hnpen" : "",
2271 (tmp & OTG_A_BUSREQ) ? " a_bus" : "",
2272 (tmp & OTG_B_HNPEN) ? " b_hnpen" : "",
2273 (tmp & OTG_B_BUSREQ) ? " b_bus" : "",
2274 (tmp & OTG_BUSDROP) ? " busdrop" : "",
2275 (tmp & OTG_PULLDOWN) ? " down" : "",
2276 (tmp & OTG_PULLUP) ? " up" : "",
2277 (tmp & OTG_DRV_VBUS) ? " drv" : "",
2278 (tmp & OTG_PD_VBUS) ? " pd_vb" : "",
2279 (tmp & OTG_PU_VBUS) ? " pu_vb" : "",
2280 (tmp & OTG_PU_ID) ? " pu_id" : ""
2282 tmp = omap_readw(OTG_IRQ_EN);
2283 seq_printf(s, "otg_irq_en %04x" "\n", tmp);
2284 tmp = omap_readw(OTG_IRQ_SRC);
2285 seq_printf(s, "otg_irq_src %04x" "\n", tmp);
2286 tmp = omap_readw(OTG_OUTCTRL);
2287 seq_printf(s, "otg_outctrl %04x" "\n", tmp);
2288 tmp = omap_readw(OTG_TEST);
2289 seq_printf(s, "otg_test %04x" "\n", tmp);
2293 static int proc_udc_show(struct seq_file *s, void *_)
2297 unsigned long flags;
2299 spin_lock_irqsave(&udc->lock, flags);
2301 seq_printf(s, "%s, version: " DRIVER_VERSION
2307 use_dma ? " (dma)" : "");
2309 tmp = omap_readw(UDC_REV) & 0xff;
2311 "UDC rev %d.%d, fifo mode %d, gadget %s\n"
2312 "hmc %d, transceiver %s\n",
2313 tmp >> 4, tmp & 0xf,
2315 udc->driver ? udc->driver->driver.name : "(none)",
2318 ? udc->transceiver->label
2319 : (cpu_is_omap1710()
2320 ? "external" : "(none)"));
2321 seq_printf(s, "ULPD control %04x req %04x status %04x\n",
2322 omap_readw(ULPD_CLOCK_CTRL),
2323 omap_readw(ULPD_SOFT_REQ),
2324 omap_readw(ULPD_STATUS_REQ));
2326 /* OTG controller registers */
2327 if (!cpu_is_omap15xx())
2330 tmp = omap_readw(UDC_SYSCON1);
2331 seq_printf(s, "\nsyscon1 %04x" EIGHTBITS "\n", tmp,
2332 (tmp & UDC_CFG_LOCK) ? " cfg_lock" : "",
2333 (tmp & UDC_DATA_ENDIAN) ? " data_endian" : "",
2334 (tmp & UDC_DMA_ENDIAN) ? " dma_endian" : "",
2335 (tmp & UDC_NAK_EN) ? " nak" : "",
2336 (tmp & UDC_AUTODECODE_DIS) ? " autodecode_dis" : "",
2337 (tmp & UDC_SELF_PWR) ? " self_pwr" : "",
2338 (tmp & UDC_SOFF_DIS) ? " soff_dis" : "",
2339 (tmp & UDC_PULLUP_EN) ? " PULLUP" : "");
2340 /* syscon2 is write-only */
2342 /* UDC controller registers */
2343 if (!(tmp & UDC_PULLUP_EN)) {
2344 seq_printf(s, "(suspended)\n");
2345 spin_unlock_irqrestore(&udc->lock, flags);
2349 tmp = omap_readw(UDC_DEVSTAT);
2350 seq_printf(s, "devstat %04x" EIGHTBITS "%s%s\n", tmp,
2351 (tmp & UDC_B_HNP_ENABLE) ? " b_hnp" : "",
2352 (tmp & UDC_A_HNP_SUPPORT) ? " a_hnp" : "",
2353 (tmp & UDC_A_ALT_HNP_SUPPORT) ? " a_alt_hnp" : "",
2354 (tmp & UDC_R_WK_OK) ? " r_wk_ok" : "",
2355 (tmp & UDC_USB_RESET) ? " usb_reset" : "",
2356 (tmp & UDC_SUS) ? " SUS" : "",
2357 (tmp & UDC_CFG) ? " CFG" : "",
2358 (tmp & UDC_ADD) ? " ADD" : "",
2359 (tmp & UDC_DEF) ? " DEF" : "",
2360 (tmp & UDC_ATT) ? " ATT" : "");
2361 seq_printf(s, "sof %04x\n", omap_readw(UDC_SOF));
2362 tmp = omap_readw(UDC_IRQ_EN);
2363 seq_printf(s, "irq_en %04x" FOURBITS "%s\n", tmp,
2364 (tmp & UDC_SOF_IE) ? " sof" : "",
2365 (tmp & UDC_EPN_RX_IE) ? " epn_rx" : "",
2366 (tmp & UDC_EPN_TX_IE) ? " epn_tx" : "",
2367 (tmp & UDC_DS_CHG_IE) ? " ds_chg" : "",
2368 (tmp & UDC_EP0_IE) ? " ep0" : "");
2369 tmp = omap_readw(UDC_IRQ_SRC);
2370 seq_printf(s, "irq_src %04x" EIGHTBITS "%s%s\n", tmp,
2371 (tmp & UDC_TXN_DONE) ? " txn_done" : "",
2372 (tmp & UDC_RXN_CNT) ? " rxn_cnt" : "",
2373 (tmp & UDC_RXN_EOT) ? " rxn_eot" : "",
2374 (tmp & UDC_IRQ_SOF) ? " sof" : "",
2375 (tmp & UDC_EPN_RX) ? " epn_rx" : "",
2376 (tmp & UDC_EPN_TX) ? " epn_tx" : "",
2377 (tmp & UDC_DS_CHG) ? " ds_chg" : "",
2378 (tmp & UDC_SETUP) ? " setup" : "",
2379 (tmp & UDC_EP0_RX) ? " ep0out" : "",
2380 (tmp & UDC_EP0_TX) ? " ep0in" : "");
2384 tmp = omap_readw(UDC_DMA_IRQ_EN);
2385 seq_printf(s, "dma_irq_en %04x%s" EIGHTBITS "\n", tmp,
2386 (tmp & UDC_TX_DONE_IE(3)) ? " tx2_done" : "",
2387 (tmp & UDC_RX_CNT_IE(3)) ? " rx2_cnt" : "",
2388 (tmp & UDC_RX_EOT_IE(3)) ? " rx2_eot" : "",
2390 (tmp & UDC_TX_DONE_IE(2)) ? " tx1_done" : "",
2391 (tmp & UDC_RX_CNT_IE(2)) ? " rx1_cnt" : "",
2392 (tmp & UDC_RX_EOT_IE(2)) ? " rx1_eot" : "",
2394 (tmp & UDC_TX_DONE_IE(1)) ? " tx0_done" : "",
2395 (tmp & UDC_RX_CNT_IE(1)) ? " rx0_cnt" : "",
2396 (tmp & UDC_RX_EOT_IE(1)) ? " rx0_eot" : "");
2398 tmp = omap_readw(UDC_RXDMA_CFG);
2399 seq_printf(s, "rxdma_cfg %04x\n", tmp);
2401 for (i = 0; i < 3; i++) {
2402 if ((tmp & (0x0f << (i * 4))) == 0)
2404 seq_printf(s, "rxdma[%d] %04x\n", i,
2405 omap_readw(UDC_RXDMA(i + 1)));
2408 tmp = omap_readw(UDC_TXDMA_CFG);
2409 seq_printf(s, "txdma_cfg %04x\n", tmp);
2411 for (i = 0; i < 3; i++) {
2412 if (!(tmp & (0x0f << (i * 4))))
2414 seq_printf(s, "txdma[%d] %04x\n", i,
2415 omap_readw(UDC_TXDMA(i + 1)));
2420 tmp = omap_readw(UDC_DEVSTAT);
2421 if (tmp & UDC_ATT) {
2422 proc_ep_show(s, &udc->ep[0]);
2423 if (tmp & UDC_ADD) {
2424 list_for_each_entry(ep, &udc->gadget.ep_list,
2427 proc_ep_show(s, ep);
2431 spin_unlock_irqrestore(&udc->lock, flags);
2435 static void create_proc_file(void)
2437 proc_create_single(proc_filename, 0, NULL, proc_udc_show);
2440 static void remove_proc_file(void)
2442 remove_proc_entry(proc_filename, NULL);
2447 static inline void create_proc_file(void) {}
2448 static inline void remove_proc_file(void) {}
2452 /*-------------------------------------------------------------------------*/
2454 /* Before this controller can enumerate, we need to pick an endpoint
2455 * configuration, or "fifo_mode" That involves allocating 2KB of packet
2456 * buffer space among the endpoints we'll be operating.
2458 * NOTE: as of OMAP 1710 ES2.0, writing a new endpoint config when
2459 * UDC_SYSCON_1.CFG_LOCK is set can now work. We won't use that
2460 * capability yet though.
2463 omap_ep_setup(char *name, u8 addr, u8 type,
2464 unsigned buf, unsigned maxp, int dbuf)
2469 /* OUT endpoints first, then IN */
2470 ep = &udc->ep[addr & 0xf];
2471 if (addr & USB_DIR_IN)
2474 /* in case of ep init table bugs */
2475 BUG_ON(ep->name[0]);
2477 /* chip setup ... bit values are same for IN, OUT */
2478 if (type == USB_ENDPOINT_XFER_ISOC) {
2504 epn_rxtx |= UDC_EPN_RX_ISO;
2507 /* double-buffering "not supported" on 15xx,
2508 * and ignored for PIO-IN on newer chips
2509 * (for more reliable behavior)
2511 if (!use_dma || cpu_is_omap15xx())
2531 epn_rxtx |= UDC_EPN_RX_DB;
2532 timer_setup(&ep->timer, pio_out_timer, 0);
2535 epn_rxtx |= UDC_EPN_RX_VALID;
2537 epn_rxtx |= buf >> 3;
2539 DBG("%s addr %02x rxtx %04x maxp %d%s buf %d\n",
2540 name, addr, epn_rxtx, maxp, dbuf ? "x2" : "", buf);
2542 if (addr & USB_DIR_IN)
2543 omap_writew(epn_rxtx, UDC_EP_TX(addr & 0xf));
2545 omap_writew(epn_rxtx, UDC_EP_RX(addr));
2547 /* next endpoint's buffer starts after this one's */
2553 /* set up driver data structures */
2554 BUG_ON(strlen(name) >= sizeof ep->name);
2555 strscpy(ep->name, name, sizeof(ep->name));
2556 INIT_LIST_HEAD(&ep->queue);
2557 INIT_LIST_HEAD(&ep->iso);
2558 ep->bEndpointAddress = addr;
2559 ep->bmAttributes = type;
2560 ep->double_buf = dbuf;
2564 case USB_ENDPOINT_XFER_CONTROL:
2565 ep->ep.caps.type_control = true;
2566 ep->ep.caps.dir_in = true;
2567 ep->ep.caps.dir_out = true;
2569 case USB_ENDPOINT_XFER_ISOC:
2570 ep->ep.caps.type_iso = true;
2572 case USB_ENDPOINT_XFER_BULK:
2573 ep->ep.caps.type_bulk = true;
2575 case USB_ENDPOINT_XFER_INT:
2576 ep->ep.caps.type_int = true;
2580 if (addr & USB_DIR_IN)
2581 ep->ep.caps.dir_in = true;
2583 ep->ep.caps.dir_out = true;
2585 ep->ep.name = ep->name;
2586 ep->ep.ops = &omap_ep_ops;
2587 ep->maxpacket = maxp;
2588 usb_ep_set_maxpacket_limit(&ep->ep, ep->maxpacket);
2589 list_add_tail(&ep->ep.ep_list, &udc->gadget.ep_list);
2594 static void omap_udc_release(struct device *dev)
2596 pullup_disable(udc);
2597 if (!IS_ERR_OR_NULL(udc->transceiver)) {
2598 usb_put_phy(udc->transceiver);
2599 udc->transceiver = NULL;
2601 omap_writew(0, UDC_SYSCON1);
2604 if (udc->clk_requested)
2605 omap_udc_enable_clock(0);
2606 clk_unprepare(udc->hhc_clk);
2607 clk_unprepare(udc->dc_clk);
2608 clk_put(udc->hhc_clk);
2609 clk_put(udc->dc_clk);
2612 complete(udc->done);
2617 omap_udc_setup(struct platform_device *odev, struct usb_phy *xceiv)
2621 /* abolish any previous hardware state */
2622 omap_writew(0, UDC_SYSCON1);
2623 omap_writew(0, UDC_IRQ_EN);
2624 omap_writew(UDC_IRQ_SRC_MASK, UDC_IRQ_SRC);
2625 omap_writew(0, UDC_DMA_IRQ_EN);
2626 omap_writew(0, UDC_RXDMA_CFG);
2627 omap_writew(0, UDC_TXDMA_CFG);
2629 /* UDC_PULLUP_EN gates the chip clock */
2630 /* OTG_SYSCON_1 |= DEV_IDLE_EN; */
2632 udc = kzalloc(sizeof(*udc), GFP_KERNEL);
2636 spin_lock_init(&udc->lock);
2638 udc->gadget.ops = &omap_gadget_ops;
2639 udc->gadget.ep0 = &udc->ep[0].ep;
2640 INIT_LIST_HEAD(&udc->gadget.ep_list);
2641 INIT_LIST_HEAD(&udc->iso);
2642 udc->gadget.speed = USB_SPEED_UNKNOWN;
2643 udc->gadget.max_speed = USB_SPEED_FULL;
2644 udc->gadget.name = driver_name;
2645 udc->gadget.quirk_ep_out_aligned_size = 1;
2646 udc->transceiver = xceiv;
2648 /* ep0 is special; put it right after the SETUP buffer */
2649 buf = omap_ep_setup("ep0", 0, USB_ENDPOINT_XFER_CONTROL,
2650 8 /* after SETUP */, 64 /* maxpacket */, 0);
2651 list_del_init(&udc->ep[0].ep.ep_list);
2653 /* initially disable all non-ep0 endpoints */
2654 for (tmp = 1; tmp < 15; tmp++) {
2655 omap_writew(0, UDC_EP_RX(tmp));
2656 omap_writew(0, UDC_EP_TX(tmp));
2659 #define OMAP_BULK_EP(name, addr) \
2660 buf = omap_ep_setup(name "-bulk", addr, \
2661 USB_ENDPOINT_XFER_BULK, buf, 64, 1);
2662 #define OMAP_INT_EP(name, addr, maxp) \
2663 buf = omap_ep_setup(name "-int", addr, \
2664 USB_ENDPOINT_XFER_INT, buf, maxp, 0);
2665 #define OMAP_ISO_EP(name, addr, maxp) \
2666 buf = omap_ep_setup(name "-iso", addr, \
2667 USB_ENDPOINT_XFER_ISOC, buf, maxp, 1);
2669 switch (fifo_mode) {
2671 OMAP_BULK_EP("ep1in", USB_DIR_IN | 1);
2672 OMAP_BULK_EP("ep2out", USB_DIR_OUT | 2);
2673 OMAP_INT_EP("ep3in", USB_DIR_IN | 3, 16);
2676 OMAP_BULK_EP("ep1in", USB_DIR_IN | 1);
2677 OMAP_BULK_EP("ep2out", USB_DIR_OUT | 2);
2678 OMAP_INT_EP("ep9in", USB_DIR_IN | 9, 16);
2680 OMAP_BULK_EP("ep3in", USB_DIR_IN | 3);
2681 OMAP_BULK_EP("ep4out", USB_DIR_OUT | 4);
2682 OMAP_INT_EP("ep10in", USB_DIR_IN | 10, 16);
2684 OMAP_BULK_EP("ep5in", USB_DIR_IN | 5);
2685 OMAP_BULK_EP("ep5out", USB_DIR_OUT | 5);
2686 OMAP_INT_EP("ep11in", USB_DIR_IN | 11, 16);
2688 OMAP_BULK_EP("ep6in", USB_DIR_IN | 6);
2689 OMAP_BULK_EP("ep6out", USB_DIR_OUT | 6);
2690 OMAP_INT_EP("ep12in", USB_DIR_IN | 12, 16);
2692 OMAP_BULK_EP("ep7in", USB_DIR_IN | 7);
2693 OMAP_BULK_EP("ep7out", USB_DIR_OUT | 7);
2694 OMAP_INT_EP("ep13in", USB_DIR_IN | 13, 16);
2695 OMAP_INT_EP("ep13out", USB_DIR_OUT | 13, 16);
2697 OMAP_BULK_EP("ep8in", USB_DIR_IN | 8);
2698 OMAP_BULK_EP("ep8out", USB_DIR_OUT | 8);
2699 OMAP_INT_EP("ep14in", USB_DIR_IN | 14, 16);
2700 OMAP_INT_EP("ep14out", USB_DIR_OUT | 14, 16);
2702 OMAP_BULK_EP("ep15in", USB_DIR_IN | 15);
2703 OMAP_BULK_EP("ep15out", USB_DIR_OUT | 15);
2708 case 2: /* mixed iso/bulk */
2709 OMAP_ISO_EP("ep1in", USB_DIR_IN | 1, 256);
2710 OMAP_ISO_EP("ep2out", USB_DIR_OUT | 2, 256);
2711 OMAP_ISO_EP("ep3in", USB_DIR_IN | 3, 128);
2712 OMAP_ISO_EP("ep4out", USB_DIR_OUT | 4, 128);
2714 OMAP_INT_EP("ep5in", USB_DIR_IN | 5, 16);
2716 OMAP_BULK_EP("ep6in", USB_DIR_IN | 6);
2717 OMAP_BULK_EP("ep7out", USB_DIR_OUT | 7);
2718 OMAP_INT_EP("ep8in", USB_DIR_IN | 8, 16);
2720 case 3: /* mixed bulk/iso */
2721 OMAP_BULK_EP("ep1in", USB_DIR_IN | 1);
2722 OMAP_BULK_EP("ep2out", USB_DIR_OUT | 2);
2723 OMAP_INT_EP("ep3in", USB_DIR_IN | 3, 16);
2725 OMAP_BULK_EP("ep4in", USB_DIR_IN | 4);
2726 OMAP_BULK_EP("ep5out", USB_DIR_OUT | 5);
2727 OMAP_INT_EP("ep6in", USB_DIR_IN | 6, 16);
2729 OMAP_ISO_EP("ep7in", USB_DIR_IN | 7, 256);
2730 OMAP_ISO_EP("ep8out", USB_DIR_OUT | 8, 256);
2731 OMAP_INT_EP("ep9in", USB_DIR_IN | 9, 16);
2735 /* add more modes as needed */
2738 ERR("unsupported fifo_mode #%d\n", fifo_mode);
2741 omap_writew(UDC_CFG_LOCK|UDC_SELF_PWR, UDC_SYSCON1);
2742 INFO("fifo mode %d, %d bytes not used\n", fifo_mode, 2048 - buf);
2746 static int omap_udc_probe(struct platform_device *pdev)
2748 int status = -ENODEV;
2750 struct usb_phy *xceiv = NULL;
2751 const char *type = NULL;
2752 struct omap_usb_config *config = dev_get_platdata(&pdev->dev);
2753 struct clk *dc_clk = NULL;
2754 struct clk *hhc_clk = NULL;
2756 /* NOTE: "knows" the order of the resources! */
2757 if (!request_mem_region(pdev->resource[0].start,
2758 resource_size(&pdev->resource[0]),
2760 DBG("request_mem_region failed\n");
2764 if (cpu_is_omap16xx()) {
2765 dc_clk = clk_get(&pdev->dev, "usb_dc_ck");
2766 hhc_clk = clk_get(&pdev->dev, "usb_hhc_ck");
2767 BUG_ON(IS_ERR(dc_clk) || IS_ERR(hhc_clk));
2768 /* can't use omap_udc_enable_clock yet */
2769 clk_prepare_enable(dc_clk);
2770 clk_prepare_enable(hhc_clk);
2774 INFO("OMAP UDC rev %d.%d%s\n",
2775 omap_readw(UDC_REV) >> 4, omap_readw(UDC_REV) & 0xf,
2776 config->otg ? ", Mini-AB" : "");
2778 /* use the mode given to us by board init code */
2779 if (cpu_is_omap15xx()) {
2783 if (machine_without_vbus_sense()) {
2784 /* just set up software VBUS detect, and then
2785 * later rig it so we always report VBUS.
2786 * FIXME without really sensing VBUS, we can't
2787 * know when to turn PULLUP_EN on/off; and that
2788 * means we always "need" the 48MHz clock.
2790 u32 tmp = omap_readl(FUNC_MUX_CTRL_0);
2791 tmp &= ~VBUS_CTRL_1510;
2792 omap_writel(tmp, FUNC_MUX_CTRL_0);
2793 tmp |= VBUS_MODE_1510;
2794 tmp &= ~VBUS_CTRL_1510;
2795 omap_writel(tmp, FUNC_MUX_CTRL_0);
2798 /* The transceiver may package some GPIO logic or handle
2799 * loopback and/or transceiverless setup; if we find one,
2800 * use it. Except for OTG, we don't _need_ to talk to one;
2801 * but not having one probably means no VBUS detection.
2803 xceiv = usb_get_phy(USB_PHY_TYPE_USB2);
2804 if (!IS_ERR_OR_NULL(xceiv))
2805 type = xceiv->label;
2806 else if (config->otg) {
2807 DBG("OTG requires external transceiver!\n");
2814 case 0: /* POWERUP DEFAULT == 0 */
2818 if (!cpu_is_omap1710()) {
2819 type = "integrated";
2828 if (IS_ERR_OR_NULL(xceiv)) {
2829 DBG("external transceiver not registered!\n");
2833 case 21: /* internal loopback */
2836 case 14: /* transceiverless */
2837 if (cpu_is_omap1710())
2847 ERR("unrecognized UDC HMC mode %d\n", hmc);
2852 INFO("hmc mode %d, %s transceiver\n", hmc, type);
2854 /* a "gadget" abstracts/virtualizes the controller */
2855 status = omap_udc_setup(pdev, xceiv);
2860 /* "udc" is now valid */
2861 pullup_disable(udc);
2862 #if IS_ENABLED(CONFIG_USB_OHCI_HCD)
2863 udc->gadget.is_otg = (config->otg != 0);
2866 /* starting with omap1710 es2.0, clear toggle is a separate bit */
2867 if (omap_readw(UDC_REV) >= 0x61)
2868 udc->clr_halt = UDC_RESET_EP | UDC_CLRDATA_TOGGLE;
2870 udc->clr_halt = UDC_RESET_EP;
2872 /* USB general purpose IRQ: ep0, state changes, dma, etc */
2873 status = devm_request_irq(&pdev->dev, pdev->resource[1].start,
2874 omap_udc_irq, 0, driver_name, udc);
2876 ERR("can't get irq %d, err %d\n",
2877 (int) pdev->resource[1].start, status);
2881 /* USB "non-iso" IRQ (PIO for all but ep0) */
2882 status = devm_request_irq(&pdev->dev, pdev->resource[2].start,
2883 omap_udc_pio_irq, 0, "omap_udc pio", udc);
2885 ERR("can't get irq %d, err %d\n",
2886 (int) pdev->resource[2].start, status);
2890 status = devm_request_irq(&pdev->dev, pdev->resource[3].start,
2891 omap_udc_iso_irq, 0, "omap_udc iso", udc);
2893 ERR("can't get irq %d, err %d\n",
2894 (int) pdev->resource[3].start, status);
2898 if (cpu_is_omap16xx()) {
2899 udc->dc_clk = dc_clk;
2900 udc->hhc_clk = hhc_clk;
2901 clk_disable(hhc_clk);
2902 clk_disable(dc_clk);
2906 return usb_add_gadget_udc_release(&pdev->dev, &udc->gadget,
2914 if (!IS_ERR_OR_NULL(xceiv))
2917 if (cpu_is_omap16xx()) {
2918 clk_disable_unprepare(hhc_clk);
2919 clk_disable_unprepare(dc_clk);
2924 release_mem_region(pdev->resource[0].start,
2925 resource_size(&pdev->resource[0]));
2930 static int omap_udc_remove(struct platform_device *pdev)
2932 DECLARE_COMPLETION_ONSTACK(done);
2936 usb_del_gadget_udc(&udc->gadget);
2938 wait_for_completion(&done);
2940 release_mem_region(pdev->resource[0].start,
2941 resource_size(&pdev->resource[0]));
2946 /* suspend/resume/wakeup from sysfs (echo > power/state) or when the
2947 * system is forced into deep sleep
2949 * REVISIT we should probably reject suspend requests when there's a host
2950 * session active, rather than disconnecting, at least on boards that can
2951 * report VBUS irqs (UDC_DEVSTAT.UDC_ATT). And in any case, we need to
2952 * make host resumes and VBUS detection trigger OMAP wakeup events; that
2953 * may involve talking to an external transceiver (e.g. isp1301).
2956 static int omap_udc_suspend(struct platform_device *dev, pm_message_t message)
2960 devstat = omap_readw(UDC_DEVSTAT);
2962 /* we're requesting 48 MHz clock if the pullup is enabled
2963 * (== we're attached to the host) and we're not suspended,
2964 * which would prevent entry to deep sleep...
2966 if ((devstat & UDC_ATT) != 0 && (devstat & UDC_SUS) == 0) {
2967 WARNING("session active; suspend requires disconnect\n");
2968 omap_pullup(&udc->gadget, 0);
2974 static int omap_udc_resume(struct platform_device *dev)
2976 DBG("resume + wakeup/SRP\n");
2977 omap_pullup(&udc->gadget, 1);
2979 /* maybe the host would enumerate us if we nudged it */
2981 return omap_wakeup(&udc->gadget);
2984 /*-------------------------------------------------------------------------*/
2986 static struct platform_driver udc_driver = {
2987 .probe = omap_udc_probe,
2988 .remove = omap_udc_remove,
2989 .suspend = omap_udc_suspend,
2990 .resume = omap_udc_resume,
2992 .name = driver_name,
2996 module_platform_driver(udc_driver);
2998 MODULE_DESCRIPTION(DRIVER_DESC);
2999 MODULE_LICENSE("GPL");
3000 MODULE_ALIAS("platform:omap_udc");