2 * MUSB OTG driver host support
4 * Copyright 2005 Mentor Graphics Corporation
5 * Copyright (C) 2005-2006 by Texas Instruments
6 * Copyright (C) 2006-2007 Nokia Corporation
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * version 2 as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
22 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
23 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
24 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
25 * NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
28 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
29 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 #include <linux/module.h>
36 #include <linux/kernel.h>
37 #include <linux/delay.h>
38 #include <linux/sched.h>
39 #include <linux/slab.h>
40 #include <linux/errno.h>
41 #include <linux/init.h>
42 #include <linux/list.h>
44 #include "musb_core.h"
45 #include "musb_host.h"
48 /* MUSB HOST status 22-mar-2006
50 * - There's still lots of partial code duplication for fault paths, so
51 * they aren't handled as consistently as they need to be.
53 * - PIO mostly behaved when last tested.
54 * + including ep0, with all usbtest cases 9, 10
55 * + usbtest 14 (ep0out) doesn't seem to run at all
56 * + double buffered OUT/TX endpoints saw stalls(!) with certain usbtest
57 * configurations, but otherwise double buffering passes basic tests.
58 * + for 2.6.N, for N > ~10, needs API changes for hcd framework.
60 * - DMA (CPPI) ... partially behaves, not currently recommended
61 * + about 1/15 the speed of typical EHCI implementations (PCI)
62 * + RX, all too often reqpkt seems to misbehave after tx
63 * + TX, no known issues (other than evident silicon issue)
65 * - DMA (Mentor/OMAP) ...has at least toggle update problems
67 * - [23-feb-2009] minimal traffic scheduling to avoid bulk RX packet
68 * starvation ... nothing yet for TX, interrupt, or bulk.
70 * - Not tested with HNP, but some SRP paths seem to behave.
72 * NOTE 24-August-2006:
74 * - Bulk traffic finally uses both sides of hardware ep1, freeing up an
75 * extra endpoint for periodic use enabling hub + keybd + mouse. That
76 * mostly works, except that with "usbnet" it's easy to trigger cases
77 * with "ping" where RX loses. (a) ping to davinci, even "ping -f",
78 * fine; but (b) ping _from_ davinci, even "ping -c 1", ICMP RX loses
79 * although ARP RX wins. (That test was done with a full speed link.)
84 * NOTE on endpoint usage:
86 * CONTROL transfers all go through ep0. BULK ones go through dedicated IN
87 * and OUT endpoints ... hardware is dedicated for those "async" queue(s).
88 * (Yes, bulk _could_ use more of the endpoints than that, and would even
91 * INTERUPPT and ISOCHRONOUS transfers are scheduled to the other endpoints.
92 * So far that scheduling is both dumb and optimistic: the endpoint will be
93 * "claimed" until its software queue is no longer refilled. No multiplexing
94 * of transfers between endpoints, or anything clever.
98 static void musb_ep_program(struct musb *musb, u8 epnum,
99 struct urb *urb, unsigned int nOut,
103 * Clear TX fifo. Needed to avoid BABBLE errors.
105 static void musb_h_tx_flush_fifo(struct musb_hw_ep *ep)
107 void __iomem *epio = ep->regs;
112 csr = musb_readw(epio, MUSB_TXCSR);
113 while (csr & MUSB_TXCSR_FIFONOTEMPTY) {
115 DBG(3, "Host TX FIFONOTEMPTY csr: %02x\n", csr);
117 csr |= MUSB_TXCSR_FLUSHFIFO;
118 musb_writew(epio, MUSB_TXCSR, csr);
119 csr = musb_readw(epio, MUSB_TXCSR);
120 if (WARN(retries-- < 1,
121 "Could not flush host TX%d fifo: csr: %04x\n",
129 * Start transmit. Caller is responsible for locking shared resources.
130 * musb must be locked.
132 static inline void musb_h_tx_start(struct musb_hw_ep *ep)
136 /* NOTE: no locks here; caller should lock and select EP */
138 txcsr = musb_readw(ep->regs, MUSB_TXCSR);
139 txcsr |= MUSB_TXCSR_TXPKTRDY | MUSB_TXCSR_H_WZC_BITS;
140 musb_writew(ep->regs, MUSB_TXCSR, txcsr);
142 txcsr = MUSB_CSR0_H_SETUPPKT | MUSB_CSR0_TXPKTRDY;
143 musb_writew(ep->regs, MUSB_CSR0, txcsr);
148 static inline void cppi_host_txdma_start(struct musb_hw_ep *ep)
152 /* NOTE: no locks here; caller should lock and select EP */
153 txcsr = musb_readw(ep->regs, MUSB_TXCSR);
154 txcsr |= MUSB_TXCSR_DMAENAB | MUSB_TXCSR_H_WZC_BITS;
155 musb_writew(ep->regs, MUSB_TXCSR, txcsr);
159 * Start the URB at the front of an endpoint's queue
160 * end must be claimed from the caller.
162 * Context: controller locked, irqs blocked
165 musb_start_urb(struct musb *musb, int is_in, struct musb_qh *qh)
170 void __iomem *mbase = musb->mregs;
171 struct urb *urb = next_urb(qh);
172 struct musb_hw_ep *hw_ep = qh->hw_ep;
173 unsigned pipe = urb->pipe;
174 u8 address = usb_pipedevice(pipe);
175 int epnum = hw_ep->epnum;
177 /* initialize software qh state */
181 /* gather right source of data */
183 case USB_ENDPOINT_XFER_CONTROL:
184 /* control transfers always start with SETUP */
187 musb->ep0_stage = MUSB_EP0_START;
188 buf = urb->setup_packet;
191 case USB_ENDPOINT_XFER_ISOC:
194 buf = urb->transfer_buffer + urb->iso_frame_desc[0].offset;
195 len = urb->iso_frame_desc[0].length;
197 default: /* bulk, interrupt */
198 /* actual_length may be nonzero on retry paths */
199 buf = urb->transfer_buffer + urb->actual_length;
200 len = urb->transfer_buffer_length - urb->actual_length;
203 DBG(4, "qh %p urb %p dev%d ep%d%s%s, hw_ep %d, %p/%d\n",
204 qh, urb, address, qh->epnum,
205 is_in ? "in" : "out",
206 ({char *s; switch (qh->type) {
207 case USB_ENDPOINT_XFER_CONTROL: s = ""; break;
208 case USB_ENDPOINT_XFER_BULK: s = "-bulk"; break;
209 case USB_ENDPOINT_XFER_ISOC: s = "-iso"; break;
210 default: s = "-intr"; break;
214 /* Configure endpoint */
215 if (is_in || hw_ep->is_shared_fifo)
219 musb_ep_program(musb, epnum, urb, !is_in, buf, len);
221 /* transmit may have more work: start it when it is time */
225 /* determine if the time is right for a periodic transfer */
227 case USB_ENDPOINT_XFER_ISOC:
228 case USB_ENDPOINT_XFER_INT:
229 DBG(3, "check whether there's still time for periodic Tx\n");
231 frame = musb_readw(mbase, MUSB_FRAME);
232 /* FIXME this doesn't implement that scheduling policy ...
233 * or handle framecounter wrapping
235 if ((urb->transfer_flags & URB_ISO_ASAP)
236 || (frame >= urb->start_frame)) {
237 /* REVISIT the SOF irq handler shouldn't duplicate
238 * this code; and we don't init urb->start_frame...
243 qh->frame = urb->start_frame;
244 /* enable SOF interrupt so we can count down */
245 DBG(1, "SOF for %d\n", epnum);
246 #if 1 /* ifndef CONFIG_ARCH_DAVINCI */
247 musb_writeb(mbase, MUSB_INTRUSBE, 0xff);
253 DBG(4, "Start TX%d %s\n", epnum,
254 hw_ep->tx_channel ? "dma" : "pio");
256 if (!hw_ep->tx_channel)
257 musb_h_tx_start(hw_ep);
258 else if (is_cppi_enabled() || tusb_dma_omap())
259 cppi_host_txdma_start(hw_ep);
263 /* caller owns controller lock, irqs are blocked */
265 __musb_giveback(struct musb *musb, struct urb *urb, int status)
266 __releases(musb->lock)
267 __acquires(musb->lock)
269 DBG(({ int level; switch (status) {
273 /* common/boring faults */
284 "complete %p %pF (%d), dev%d ep%d%s, %d/%d\n",
285 urb, urb->complete, status,
286 usb_pipedevice(urb->pipe),
287 usb_pipeendpoint(urb->pipe),
288 usb_pipein(urb->pipe) ? "in" : "out",
289 urb->actual_length, urb->transfer_buffer_length
292 usb_hcd_unlink_urb_from_ep(musb_to_hcd(musb), urb);
293 spin_unlock(&musb->lock);
294 usb_hcd_giveback_urb(musb_to_hcd(musb), urb, status);
295 spin_lock(&musb->lock);
298 /* for bulk/interrupt endpoints only */
300 musb_save_toggle(struct musb_hw_ep *ep, int is_in, struct urb *urb)
302 struct usb_device *udev = urb->dev;
304 void __iomem *epio = ep->regs;
307 /* FIXME: the current Mentor DMA code seems to have
308 * problems getting toggle correct.
311 if (is_in || ep->is_shared_fifo)
317 csr = musb_readw(epio, MUSB_TXCSR);
318 usb_settoggle(udev, qh->epnum, 1,
319 (csr & MUSB_TXCSR_H_DATATOGGLE)
322 csr = musb_readw(epio, MUSB_RXCSR);
323 usb_settoggle(udev, qh->epnum, 0,
324 (csr & MUSB_RXCSR_H_DATATOGGLE)
329 /* caller owns controller lock, irqs are blocked */
330 static struct musb_qh *
331 musb_giveback(struct musb_qh *qh, struct urb *urb, int status)
333 struct musb_hw_ep *ep = qh->hw_ep;
334 struct musb *musb = ep->musb;
335 int is_in = usb_pipein(urb->pipe);
336 int ready = qh->is_ready;
338 /* save toggle eagerly, for paranoia */
340 case USB_ENDPOINT_XFER_BULK:
341 case USB_ENDPOINT_XFER_INT:
342 musb_save_toggle(ep, is_in, urb);
344 case USB_ENDPOINT_XFER_ISOC:
345 if (status == 0 && urb->error_count)
351 __musb_giveback(musb, urb, status);
352 qh->is_ready = ready;
354 /* reclaim resources (and bandwidth) ASAP; deschedule it, and
355 * invalidate qh as soon as list_empty(&hep->urb_list)
357 if (list_empty(&qh->hep->urb_list)) {
358 struct list_head *head;
365 /* clobber old pointers to this qh */
366 if (is_in || ep->is_shared_fifo)
370 qh->hep->hcpriv = NULL;
374 case USB_ENDPOINT_XFER_CONTROL:
375 case USB_ENDPOINT_XFER_BULK:
376 /* fifo policy for these lists, except that NAKing
377 * should rotate a qh to the end (for fairness).
380 head = qh->ring.prev;
387 case USB_ENDPOINT_XFER_ISOC:
388 case USB_ENDPOINT_XFER_INT:
389 /* this is where periodic bandwidth should be
390 * de-allocated if it's tracked and allocated;
391 * and where we'd update the schedule tree...
402 * Advance this hardware endpoint's queue, completing the specified urb and
403 * advancing to either the next urb queued to that qh, or else invalidating
404 * that qh and advancing to the next qh scheduled after the current one.
406 * Context: caller owns controller lock, irqs are blocked
409 musb_advance_schedule(struct musb *musb, struct urb *urb,
410 struct musb_hw_ep *hw_ep, int is_in)
414 if (is_in || hw_ep->is_shared_fifo)
419 if (urb->status == -EINPROGRESS)
420 qh = musb_giveback(qh, urb, 0);
422 qh = musb_giveback(qh, urb, urb->status);
424 if (qh != NULL && qh->is_ready) {
425 DBG(4, "... next ep%d %cX urb %p\n",
426 hw_ep->epnum, is_in ? 'R' : 'T',
428 musb_start_urb(musb, is_in, qh);
432 static u16 musb_h_flush_rxfifo(struct musb_hw_ep *hw_ep, u16 csr)
434 /* we don't want fifo to fill itself again;
435 * ignore dma (various models),
436 * leave toggle alone (may not have been saved yet)
438 csr |= MUSB_RXCSR_FLUSHFIFO | MUSB_RXCSR_RXPKTRDY;
439 csr &= ~(MUSB_RXCSR_H_REQPKT
440 | MUSB_RXCSR_H_AUTOREQ
441 | MUSB_RXCSR_AUTOCLEAR);
443 /* write 2x to allow double buffering */
444 musb_writew(hw_ep->regs, MUSB_RXCSR, csr);
445 musb_writew(hw_ep->regs, MUSB_RXCSR, csr);
447 /* flush writebuffer */
448 return musb_readw(hw_ep->regs, MUSB_RXCSR);
452 * PIO RX for a packet (or part of it).
455 musb_host_packet_rx(struct musb *musb, struct urb *urb, u8 epnum, u8 iso_err)
463 struct musb_hw_ep *hw_ep = musb->endpoints + epnum;
464 void __iomem *epio = hw_ep->regs;
465 struct musb_qh *qh = hw_ep->in_qh;
466 int pipe = urb->pipe;
467 void *buffer = urb->transfer_buffer;
469 /* musb_ep_select(mbase, epnum); */
470 rx_count = musb_readw(epio, MUSB_RXCOUNT);
471 DBG(3, "RX%d count %d, buffer %p len %d/%d\n", epnum, rx_count,
472 urb->transfer_buffer, qh->offset,
473 urb->transfer_buffer_length);
476 if (usb_pipeisoc(pipe)) {
478 struct usb_iso_packet_descriptor *d;
485 d = urb->iso_frame_desc + qh->iso_idx;
486 buf = buffer + d->offset;
488 if (rx_count > length) {
493 DBG(2, "** OVERFLOW %d into %d\n", rx_count, length);
497 urb->actual_length += length;
498 d->actual_length = length;
502 /* see if we are done */
503 done = (++qh->iso_idx >= urb->number_of_packets);
506 buf = buffer + qh->offset;
507 length = urb->transfer_buffer_length - qh->offset;
508 if (rx_count > length) {
509 if (urb->status == -EINPROGRESS)
510 urb->status = -EOVERFLOW;
511 DBG(2, "** OVERFLOW %d into %d\n", rx_count, length);
515 urb->actual_length += length;
516 qh->offset += length;
518 /* see if we are done */
519 done = (urb->actual_length == urb->transfer_buffer_length)
520 || (rx_count < qh->maxpacket)
521 || (urb->status != -EINPROGRESS);
523 && (urb->status == -EINPROGRESS)
524 && (urb->transfer_flags & URB_SHORT_NOT_OK)
525 && (urb->actual_length
526 < urb->transfer_buffer_length))
527 urb->status = -EREMOTEIO;
530 musb_read_fifo(hw_ep, length, buf);
532 csr = musb_readw(epio, MUSB_RXCSR);
533 csr |= MUSB_RXCSR_H_WZC_BITS;
534 if (unlikely(do_flush))
535 musb_h_flush_rxfifo(hw_ep, csr);
537 /* REVISIT this assumes AUTOCLEAR is never set */
538 csr &= ~(MUSB_RXCSR_RXPKTRDY | MUSB_RXCSR_H_REQPKT);
540 csr |= MUSB_RXCSR_H_REQPKT;
541 musb_writew(epio, MUSB_RXCSR, csr);
547 /* we don't always need to reinit a given side of an endpoint...
548 * when we do, use tx/rx reinit routine and then construct a new CSR
549 * to address data toggle, NYET, and DMA or PIO.
551 * it's possible that driver bugs (especially for DMA) or aborting a
552 * transfer might have left the endpoint busier than it should be.
553 * the busy/not-empty tests are basically paranoia.
556 musb_rx_reinit(struct musb *musb, struct musb_qh *qh, struct musb_hw_ep *ep)
560 /* NOTE: we know the "rx" fifo reinit never triggers for ep0.
561 * That always uses tx_reinit since ep0 repurposes TX register
562 * offsets; the initial SETUP packet is also a kind of OUT.
565 /* if programmed for Tx, put it in RX mode */
566 if (ep->is_shared_fifo) {
567 csr = musb_readw(ep->regs, MUSB_TXCSR);
568 if (csr & MUSB_TXCSR_MODE) {
569 musb_h_tx_flush_fifo(ep);
570 musb_writew(ep->regs, MUSB_TXCSR,
571 MUSB_TXCSR_FRCDATATOG);
573 /* clear mode (and everything else) to enable Rx */
574 musb_writew(ep->regs, MUSB_TXCSR, 0);
576 /* scrub all previous state, clearing toggle */
578 csr = musb_readw(ep->regs, MUSB_RXCSR);
579 if (csr & MUSB_RXCSR_RXPKTRDY)
580 WARNING("rx%d, packet/%d ready?\n", ep->epnum,
581 musb_readw(ep->regs, MUSB_RXCOUNT));
583 musb_h_flush_rxfifo(ep, MUSB_RXCSR_CLRDATATOG);
586 /* target addr and (for multipoint) hub addr/port */
587 if (musb->is_multipoint) {
588 musb_write_rxfunaddr(ep->target_regs, qh->addr_reg);
589 musb_write_rxhubaddr(ep->target_regs, qh->h_addr_reg);
590 musb_write_rxhubport(ep->target_regs, qh->h_port_reg);
593 musb_writeb(musb->mregs, MUSB_FADDR, qh->addr_reg);
595 /* protocol/endpoint, interval/NAKlimit, i/o size */
596 musb_writeb(ep->regs, MUSB_RXTYPE, qh->type_reg);
597 musb_writeb(ep->regs, MUSB_RXINTERVAL, qh->intv_reg);
598 /* NOTE: bulk combining rewrites high bits of maxpacket */
599 musb_writew(ep->regs, MUSB_RXMAXP, qh->maxpacket);
606 * Program an HDRC endpoint as per the given URB
607 * Context: irqs blocked, controller lock held
609 static void musb_ep_program(struct musb *musb, u8 epnum,
610 struct urb *urb, unsigned int is_out,
613 struct dma_controller *dma_controller;
614 struct dma_channel *dma_channel;
616 void __iomem *mbase = musb->mregs;
617 struct musb_hw_ep *hw_ep = musb->endpoints + epnum;
618 void __iomem *epio = hw_ep->regs;
622 if (!is_out || hw_ep->is_shared_fifo)
627 packet_sz = qh->maxpacket;
629 DBG(3, "%s hw%d urb %p spd%d dev%d ep%d%s "
630 "h_addr%02x h_port%02x bytes %d\n",
631 is_out ? "-->" : "<--",
632 epnum, urb, urb->dev->speed,
633 qh->addr_reg, qh->epnum, is_out ? "out" : "in",
634 qh->h_addr_reg, qh->h_port_reg,
637 musb_ep_select(mbase, epnum);
639 /* candidate for DMA? */
640 dma_controller = musb->dma_controller;
641 if (is_dma_capable() && epnum && dma_controller) {
642 dma_channel = is_out ? hw_ep->tx_channel : hw_ep->rx_channel;
644 dma_channel = dma_controller->channel_alloc(
645 dma_controller, hw_ep, is_out);
647 hw_ep->tx_channel = dma_channel;
649 hw_ep->rx_channel = dma_channel;
654 /* make sure we clear DMAEnab, autoSet bits from previous run */
656 /* OUT/transmit/EP0 or IN/receive? */
662 csr = musb_readw(epio, MUSB_TXCSR);
664 /* disable interrupt in case we flush */
665 int_txe = musb_readw(mbase, MUSB_INTRTXE);
666 musb_writew(mbase, MUSB_INTRTXE, int_txe & ~(1 << epnum));
668 /* general endpoint setup */
670 /* ASSERT: TXCSR_DMAENAB was already cleared */
672 /* flush all old state, set default */
673 musb_h_tx_flush_fifo(hw_ep);
674 csr &= ~(MUSB_TXCSR_H_NAKTIMEOUT
676 | MUSB_TXCSR_FRCDATATOG
677 | MUSB_TXCSR_H_RXSTALL
679 | MUSB_TXCSR_TXPKTRDY
681 csr |= MUSB_TXCSR_MODE;
683 if (usb_gettoggle(urb->dev,
685 csr |= MUSB_TXCSR_H_WR_DATATOGGLE
686 | MUSB_TXCSR_H_DATATOGGLE;
688 csr |= MUSB_TXCSR_CLRDATATOG;
690 /* twice in case of double packet buffering */
691 musb_writew(epio, MUSB_TXCSR, csr);
692 /* REVISIT may need to clear FLUSHFIFO ... */
693 musb_writew(epio, MUSB_TXCSR, csr);
694 csr = musb_readw(epio, MUSB_TXCSR);
696 /* endpoint 0: just flush */
697 musb_writew(epio, MUSB_CSR0,
698 csr | MUSB_CSR0_FLUSHFIFO);
699 musb_writew(epio, MUSB_CSR0,
700 csr | MUSB_CSR0_FLUSHFIFO);
703 /* target addr and (for multipoint) hub addr/port */
704 if (musb->is_multipoint) {
705 musb_write_txfunaddr(mbase, epnum, qh->addr_reg);
706 musb_write_txhubaddr(mbase, epnum, qh->h_addr_reg);
707 musb_write_txhubport(mbase, epnum, qh->h_port_reg);
708 /* FIXME if !epnum, do the same for RX ... */
710 musb_writeb(mbase, MUSB_FADDR, qh->addr_reg);
712 /* protocol/endpoint/interval/NAKlimit */
714 musb_writeb(epio, MUSB_TXTYPE, qh->type_reg);
715 if (can_bulk_split(musb, qh->type))
716 musb_writew(epio, MUSB_TXMAXP,
718 | ((hw_ep->max_packet_sz_tx /
719 packet_sz) - 1) << 11);
721 musb_writew(epio, MUSB_TXMAXP,
723 musb_writeb(epio, MUSB_TXINTERVAL, qh->intv_reg);
725 musb_writeb(epio, MUSB_NAKLIMIT0, qh->intv_reg);
726 if (musb->is_multipoint)
727 musb_writeb(epio, MUSB_TYPE0,
731 if (can_bulk_split(musb, qh->type))
732 load_count = min((u32) hw_ep->max_packet_sz_tx,
735 load_count = min((u32) packet_sz, len);
737 #ifdef CONFIG_USB_INVENTRA_DMA
740 /* clear previous state */
741 csr = musb_readw(epio, MUSB_TXCSR);
742 csr &= ~(MUSB_TXCSR_AUTOSET
744 | MUSB_TXCSR_DMAENAB);
745 csr |= MUSB_TXCSR_MODE;
746 musb_writew(epio, MUSB_TXCSR,
747 csr | MUSB_TXCSR_MODE);
749 qh->segsize = min(len, dma_channel->max_len);
751 if (qh->segsize <= packet_sz)
752 dma_channel->desired_mode = 0;
754 dma_channel->desired_mode = 1;
757 if (dma_channel->desired_mode == 0) {
758 csr &= ~(MUSB_TXCSR_AUTOSET
759 | MUSB_TXCSR_DMAMODE);
760 csr |= (MUSB_TXCSR_DMAENAB);
761 /* against programming guide */
763 csr |= (MUSB_TXCSR_AUTOSET
765 | MUSB_TXCSR_DMAMODE);
767 musb_writew(epio, MUSB_TXCSR, csr);
769 dma_ok = dma_controller->channel_program(
770 dma_channel, packet_sz,
771 dma_channel->desired_mode,
777 dma_controller->channel_release(dma_channel);
779 hw_ep->tx_channel = NULL;
781 hw_ep->rx_channel = NULL;
787 /* candidate for DMA */
788 if ((is_cppi_enabled() || tusb_dma_omap()) && dma_channel) {
790 /* program endpoint CSRs first, then setup DMA.
791 * assume CPPI setup succeeds.
792 * defer enabling dma.
794 csr = musb_readw(epio, MUSB_TXCSR);
795 csr &= ~(MUSB_TXCSR_AUTOSET
797 | MUSB_TXCSR_DMAENAB);
798 csr |= MUSB_TXCSR_MODE;
799 musb_writew(epio, MUSB_TXCSR,
800 csr | MUSB_TXCSR_MODE);
802 dma_channel->actual_len = 0L;
805 /* TX uses "rndis" mode automatically, but needs help
806 * to identify the zero-length-final-packet case.
808 dma_ok = dma_controller->channel_program(
809 dma_channel, packet_sz,
818 dma_controller->channel_release(dma_channel);
819 hw_ep->tx_channel = NULL;
822 /* REVISIT there's an error path here that
823 * needs handling: can't do dma, but
824 * there's no pio buffer address...
830 /* ASSERT: TXCSR_DMAENAB was already cleared */
832 /* PIO to load FIFO */
833 qh->segsize = load_count;
834 musb_write_fifo(hw_ep, load_count, buf);
835 csr = musb_readw(epio, MUSB_TXCSR);
836 csr &= ~(MUSB_TXCSR_DMAENAB
838 | MUSB_TXCSR_AUTOSET);
840 csr |= MUSB_TXCSR_MODE;
843 musb_writew(epio, MUSB_TXCSR, csr);
846 /* re-enable interrupt */
847 musb_writew(mbase, MUSB_INTRTXE, int_txe);
853 if (hw_ep->rx_reinit) {
854 musb_rx_reinit(musb, qh, hw_ep);
856 /* init new state: toggle and NYET, maybe DMA later */
857 if (usb_gettoggle(urb->dev, qh->epnum, 0))
858 csr = MUSB_RXCSR_H_WR_DATATOGGLE
859 | MUSB_RXCSR_H_DATATOGGLE;
862 if (qh->type == USB_ENDPOINT_XFER_INT)
863 csr |= MUSB_RXCSR_DISNYET;
866 csr = musb_readw(hw_ep->regs, MUSB_RXCSR);
868 if (csr & (MUSB_RXCSR_RXPKTRDY
870 | MUSB_RXCSR_H_REQPKT))
871 ERR("broken !rx_reinit, ep%d csr %04x\n",
874 /* scrub any stale state, leaving toggle alone */
875 csr &= MUSB_RXCSR_DISNYET;
878 /* kick things off */
880 if ((is_cppi_enabled() || tusb_dma_omap()) && dma_channel) {
881 /* candidate for DMA */
883 dma_channel->actual_len = 0L;
886 /* AUTOREQ is in a DMA register */
887 musb_writew(hw_ep->regs, MUSB_RXCSR, csr);
888 csr = musb_readw(hw_ep->regs,
891 /* unless caller treats short rx transfers as
892 * errors, we dare not queue multiple transfers.
894 dma_ok = dma_controller->channel_program(
895 dma_channel, packet_sz,
896 !(urb->transfer_flags
901 dma_controller->channel_release(
903 hw_ep->rx_channel = NULL;
906 csr |= MUSB_RXCSR_DMAENAB;
910 csr |= MUSB_RXCSR_H_REQPKT;
911 DBG(7, "RXCSR%d := %04x\n", epnum, csr);
912 musb_writew(hw_ep->regs, MUSB_RXCSR, csr);
913 csr = musb_readw(hw_ep->regs, MUSB_RXCSR);
919 * Service the default endpoint (ep0) as host.
920 * Return true until it's time to start the status stage.
922 static bool musb_h_ep0_continue(struct musb *musb, u16 len, struct urb *urb)
925 u8 *fifo_dest = NULL;
927 struct musb_hw_ep *hw_ep = musb->control_ep;
928 struct musb_qh *qh = hw_ep->in_qh;
929 struct usb_ctrlrequest *request;
931 switch (musb->ep0_stage) {
933 fifo_dest = urb->transfer_buffer + urb->actual_length;
934 fifo_count = min_t(size_t, len, urb->transfer_buffer_length -
936 if (fifo_count < len)
937 urb->status = -EOVERFLOW;
939 musb_read_fifo(hw_ep, fifo_count, fifo_dest);
941 urb->actual_length += fifo_count;
942 if (len < qh->maxpacket) {
943 /* always terminate on short read; it's
944 * rarely reported as an error.
946 } else if (urb->actual_length <
947 urb->transfer_buffer_length)
951 request = (struct usb_ctrlrequest *) urb->setup_packet;
953 if (!request->wLength) {
954 DBG(4, "start no-DATA\n");
956 } else if (request->bRequestType & USB_DIR_IN) {
957 DBG(4, "start IN-DATA\n");
958 musb->ep0_stage = MUSB_EP0_IN;
962 DBG(4, "start OUT-DATA\n");
963 musb->ep0_stage = MUSB_EP0_OUT;
968 fifo_count = min_t(size_t, qh->maxpacket,
969 urb->transfer_buffer_length -
972 fifo_dest = (u8 *) (urb->transfer_buffer
973 + urb->actual_length);
974 DBG(3, "Sending %d byte%s to ep0 fifo %p\n",
976 (fifo_count == 1) ? "" : "s",
978 musb_write_fifo(hw_ep, fifo_count, fifo_dest);
980 urb->actual_length += fifo_count;
985 ERR("bogus ep0 stage %d\n", musb->ep0_stage);
993 * Handle default endpoint interrupt as host. Only called in IRQ time
994 * from musb_interrupt().
996 * called with controller irqlocked
998 irqreturn_t musb_h_ep0_irq(struct musb *musb)
1003 void __iomem *mbase = musb->mregs;
1004 struct musb_hw_ep *hw_ep = musb->control_ep;
1005 void __iomem *epio = hw_ep->regs;
1006 struct musb_qh *qh = hw_ep->in_qh;
1007 bool complete = false;
1008 irqreturn_t retval = IRQ_NONE;
1010 /* ep0 only has one queue, "in" */
1013 musb_ep_select(mbase, 0);
1014 csr = musb_readw(epio, MUSB_CSR0);
1015 len = (csr & MUSB_CSR0_RXPKTRDY)
1016 ? musb_readb(epio, MUSB_COUNT0)
1019 DBG(4, "<== csr0 %04x, qh %p, count %d, urb %p, stage %d\n",
1020 csr, qh, len, urb, musb->ep0_stage);
1022 /* if we just did status stage, we are done */
1023 if (MUSB_EP0_STATUS == musb->ep0_stage) {
1024 retval = IRQ_HANDLED;
1028 /* prepare status */
1029 if (csr & MUSB_CSR0_H_RXSTALL) {
1030 DBG(6, "STALLING ENDPOINT\n");
1033 } else if (csr & MUSB_CSR0_H_ERROR) {
1034 DBG(2, "no response, csr0 %04x\n", csr);
1037 } else if (csr & MUSB_CSR0_H_NAKTIMEOUT) {
1038 DBG(2, "control NAK timeout\n");
1040 /* NOTE: this code path would be a good place to PAUSE a
1041 * control transfer, if another one is queued, so that
1042 * ep0 is more likely to stay busy. That's already done
1043 * for bulk RX transfers.
1045 * if (qh->ring.next != &musb->control), then
1046 * we have a candidate... NAKing is *NOT* an error
1048 musb_writew(epio, MUSB_CSR0, 0);
1049 retval = IRQ_HANDLED;
1053 DBG(6, "aborting\n");
1054 retval = IRQ_HANDLED;
1056 urb->status = status;
1059 /* use the proper sequence to abort the transfer */
1060 if (csr & MUSB_CSR0_H_REQPKT) {
1061 csr &= ~MUSB_CSR0_H_REQPKT;
1062 musb_writew(epio, MUSB_CSR0, csr);
1063 csr &= ~MUSB_CSR0_H_NAKTIMEOUT;
1064 musb_writew(epio, MUSB_CSR0, csr);
1066 csr |= MUSB_CSR0_FLUSHFIFO;
1067 musb_writew(epio, MUSB_CSR0, csr);
1068 musb_writew(epio, MUSB_CSR0, csr);
1069 csr &= ~MUSB_CSR0_H_NAKTIMEOUT;
1070 musb_writew(epio, MUSB_CSR0, csr);
1073 musb_writeb(epio, MUSB_NAKLIMIT0, 0);
1076 musb_writew(epio, MUSB_CSR0, 0);
1079 if (unlikely(!urb)) {
1080 /* stop endpoint since we have no place for its data, this
1081 * SHOULD NEVER HAPPEN! */
1082 ERR("no URB for end 0\n");
1084 musb_writew(epio, MUSB_CSR0, MUSB_CSR0_FLUSHFIFO);
1085 musb_writew(epio, MUSB_CSR0, MUSB_CSR0_FLUSHFIFO);
1086 musb_writew(epio, MUSB_CSR0, 0);
1092 /* call common logic and prepare response */
1093 if (musb_h_ep0_continue(musb, len, urb)) {
1094 /* more packets required */
1095 csr = (MUSB_EP0_IN == musb->ep0_stage)
1096 ? MUSB_CSR0_H_REQPKT : MUSB_CSR0_TXPKTRDY;
1098 /* data transfer complete; perform status phase */
1099 if (usb_pipeout(urb->pipe)
1100 || !urb->transfer_buffer_length)
1101 csr = MUSB_CSR0_H_STATUSPKT
1102 | MUSB_CSR0_H_REQPKT;
1104 csr = MUSB_CSR0_H_STATUSPKT
1105 | MUSB_CSR0_TXPKTRDY;
1107 /* flag status stage */
1108 musb->ep0_stage = MUSB_EP0_STATUS;
1110 DBG(5, "ep0 STATUS, csr %04x\n", csr);
1113 musb_writew(epio, MUSB_CSR0, csr);
1114 retval = IRQ_HANDLED;
1116 musb->ep0_stage = MUSB_EP0_IDLE;
1118 /* call completion handler if done */
1120 musb_advance_schedule(musb, urb, hw_ep, 1);
1126 #ifdef CONFIG_USB_INVENTRA_DMA
1128 /* Host side TX (OUT) using Mentor DMA works as follows:
1130 - if queue was empty, Program Endpoint
1131 - ... which starts DMA to fifo in mode 1 or 0
1133 DMA Isr (transfer complete) -> TxAvail()
1134 - Stop DMA (~DmaEnab) (<--- Alert ... currently happens
1135 only in musb_cleanup_urb)
1136 - TxPktRdy has to be set in mode 0 or for
1137 short packets in mode 1.
1142 /* Service a Tx-Available or dma completion irq for the endpoint */
1143 void musb_host_tx(struct musb *musb, u8 epnum)
1151 struct musb_hw_ep *hw_ep = musb->endpoints + epnum;
1152 void __iomem *epio = hw_ep->regs;
1153 struct musb_qh *qh = hw_ep->is_shared_fifo ? hw_ep->in_qh
1156 void __iomem *mbase = musb->mregs;
1157 struct dma_channel *dma;
1161 musb_ep_select(mbase, epnum);
1162 tx_csr = musb_readw(epio, MUSB_TXCSR);
1164 /* with CPPI, DMA sometimes triggers "extra" irqs */
1166 DBG(4, "extra TX%d ready, csr %04x\n", epnum, tx_csr);
1171 dma = is_dma_capable() ? hw_ep->tx_channel : NULL;
1172 DBG(4, "OUT/TX%d end, csr %04x%s\n", epnum, tx_csr,
1173 dma ? ", dma" : "");
1175 /* check for errors */
1176 if (tx_csr & MUSB_TXCSR_H_RXSTALL) {
1177 /* dma was disabled, fifo flushed */
1178 DBG(3, "TX end %d stall\n", epnum);
1180 /* stall; record URB status */
1183 } else if (tx_csr & MUSB_TXCSR_H_ERROR) {
1184 /* (NON-ISO) dma was disabled, fifo flushed */
1185 DBG(3, "TX 3strikes on ep=%d\n", epnum);
1187 status = -ETIMEDOUT;
1189 } else if (tx_csr & MUSB_TXCSR_H_NAKTIMEOUT) {
1190 DBG(6, "TX end=%d device not responding\n", epnum);
1192 /* NOTE: this code path would be a good place to PAUSE a
1193 * transfer, if there's some other (nonperiodic) tx urb
1194 * that could use this fifo. (dma complicates it...)
1195 * That's already done for bulk RX transfers.
1197 * if (bulk && qh->ring.next != &musb->out_bulk), then
1198 * we have a candidate... NAKing is *NOT* an error
1200 musb_ep_select(mbase, epnum);
1201 musb_writew(epio, MUSB_TXCSR,
1202 MUSB_TXCSR_H_WZC_BITS
1203 | MUSB_TXCSR_TXPKTRDY);
1208 if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) {
1209 dma->status = MUSB_DMA_STATUS_CORE_ABORT;
1210 (void) musb->dma_controller->channel_abort(dma);
1213 /* do the proper sequence to abort the transfer in the
1214 * usb core; the dma engine should already be stopped.
1216 musb_h_tx_flush_fifo(hw_ep);
1217 tx_csr &= ~(MUSB_TXCSR_AUTOSET
1218 | MUSB_TXCSR_DMAENAB
1219 | MUSB_TXCSR_H_ERROR
1220 | MUSB_TXCSR_H_RXSTALL
1221 | MUSB_TXCSR_H_NAKTIMEOUT
1224 musb_ep_select(mbase, epnum);
1225 musb_writew(epio, MUSB_TXCSR, tx_csr);
1226 /* REVISIT may need to clear FLUSHFIFO ... */
1227 musb_writew(epio, MUSB_TXCSR, tx_csr);
1228 musb_writeb(epio, MUSB_TXINTERVAL, 0);
1233 /* second cppi case */
1234 if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) {
1235 DBG(4, "extra TX%d ready, csr %04x\n", epnum, tx_csr);
1240 /* REVISIT this looks wrong... */
1241 if (!status || dma || usb_pipeisoc(pipe)) {
1243 wLength = dma->actual_len;
1245 wLength = qh->segsize;
1246 qh->offset += wLength;
1248 if (usb_pipeisoc(pipe)) {
1249 struct usb_iso_packet_descriptor *d;
1251 d = urb->iso_frame_desc + qh->iso_idx;
1252 d->actual_length = qh->segsize;
1253 if (++qh->iso_idx >= urb->number_of_packets) {
1257 buf = urb->transfer_buffer + d->offset;
1258 wLength = d->length;
1263 /* see if we need to send more data, or ZLP */
1264 if (qh->segsize < qh->maxpacket)
1266 else if (qh->offset == urb->transfer_buffer_length
1267 && !(urb->transfer_flags
1271 buf = urb->transfer_buffer
1273 wLength = urb->transfer_buffer_length
1279 /* urb->status != -EINPROGRESS means request has been faulted,
1280 * so we must abort this transfer after cleanup
1282 if (urb->status != -EINPROGRESS) {
1285 status = urb->status;
1290 urb->status = status;
1291 urb->actual_length = qh->offset;
1292 musb_advance_schedule(musb, urb, hw_ep, USB_DIR_OUT);
1294 } else if (!(tx_csr & MUSB_TXCSR_DMAENAB)) {
1295 /* WARN_ON(!buf); */
1297 /* REVISIT: some docs say that when hw_ep->tx_double_buffered,
1298 * (and presumably, fifo is not half-full) we should write TWO
1299 * packets before updating TXCSR ... other docs disagree ...
1301 /* PIO: start next packet in this URB */
1302 if (wLength > qh->maxpacket)
1303 wLength = qh->maxpacket;
1304 musb_write_fifo(hw_ep, wLength, buf);
1305 qh->segsize = wLength;
1307 musb_ep_select(mbase, epnum);
1308 musb_writew(epio, MUSB_TXCSR,
1309 MUSB_TXCSR_H_WZC_BITS | MUSB_TXCSR_TXPKTRDY);
1311 DBG(1, "not complete, but dma enabled?\n");
1318 #ifdef CONFIG_USB_INVENTRA_DMA
1320 /* Host side RX (IN) using Mentor DMA works as follows:
1322 - if queue was empty, ProgramEndpoint
1323 - first IN token is sent out (by setting ReqPkt)
1324 LinuxIsr -> RxReady()
1325 /\ => first packet is received
1326 | - Set in mode 0 (DmaEnab, ~ReqPkt)
1327 | -> DMA Isr (transfer complete) -> RxReady()
1328 | - Ack receive (~RxPktRdy), turn off DMA (~DmaEnab)
1329 | - if urb not complete, send next IN token (ReqPkt)
1330 | | else complete urb.
1332 ---------------------------
1334 * Nuances of mode 1:
1335 * For short packets, no ack (+RxPktRdy) is sent automatically
1336 * (even if AutoClear is ON)
1337 * For full packets, ack (~RxPktRdy) and next IN token (+ReqPkt) is sent
1338 * automatically => major problem, as collecting the next packet becomes
1339 * difficult. Hence mode 1 is not used.
1342 * All we care about at this driver level is that
1343 * (a) all URBs terminate with REQPKT cleared and fifo(s) empty;
1344 * (b) termination conditions are: short RX, or buffer full;
1345 * (c) fault modes include
1346 * - iff URB_SHORT_NOT_OK, short RX status is -EREMOTEIO.
1347 * (and that endpoint's dma queue stops immediately)
1348 * - overflow (full, PLUS more bytes in the terminal packet)
1350 * So for example, usb-storage sets URB_SHORT_NOT_OK, and would
1351 * thus be a great candidate for using mode 1 ... for all but the
1352 * last packet of one URB's transfer.
1357 /* Schedule next QH from musb->in_bulk and move the current qh to
1358 * the end; avoids starvation for other endpoints.
1360 static void musb_bulk_rx_nak_timeout(struct musb *musb, struct musb_hw_ep *ep)
1362 struct dma_channel *dma;
1364 void __iomem *mbase = musb->mregs;
1365 void __iomem *epio = ep->regs;
1366 struct musb_qh *cur_qh, *next_qh;
1369 musb_ep_select(mbase, ep->epnum);
1370 dma = is_dma_capable() ? ep->rx_channel : NULL;
1372 /* clear nak timeout bit */
1373 rx_csr = musb_readw(epio, MUSB_RXCSR);
1374 rx_csr |= MUSB_RXCSR_H_WZC_BITS;
1375 rx_csr &= ~MUSB_RXCSR_DATAERROR;
1376 musb_writew(epio, MUSB_RXCSR, rx_csr);
1378 cur_qh = first_qh(&musb->in_bulk);
1380 urb = next_urb(cur_qh);
1381 if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) {
1382 dma->status = MUSB_DMA_STATUS_CORE_ABORT;
1383 musb->dma_controller->channel_abort(dma);
1384 urb->actual_length += dma->actual_len;
1385 dma->actual_len = 0L;
1387 musb_save_toggle(ep, 1, urb);
1389 /* move cur_qh to end of queue */
1390 list_move_tail(&cur_qh->ring, &musb->in_bulk);
1392 /* get the next qh from musb->in_bulk */
1393 next_qh = first_qh(&musb->in_bulk);
1395 /* set rx_reinit and schedule the next qh */
1397 musb_start_urb(musb, 1, next_qh);
1402 * Service an RX interrupt for the given IN endpoint; docs cover bulk, iso,
1403 * and high-bandwidth IN transfer cases.
1405 void musb_host_rx(struct musb *musb, u8 epnum)
1408 struct musb_hw_ep *hw_ep = musb->endpoints + epnum;
1409 void __iomem *epio = hw_ep->regs;
1410 struct musb_qh *qh = hw_ep->in_qh;
1412 void __iomem *mbase = musb->mregs;
1415 bool iso_err = false;
1418 struct dma_channel *dma;
1420 musb_ep_select(mbase, epnum);
1423 dma = is_dma_capable() ? hw_ep->rx_channel : NULL;
1427 rx_csr = musb_readw(epio, MUSB_RXCSR);
1430 if (unlikely(!urb)) {
1431 /* REVISIT -- THIS SHOULD NEVER HAPPEN ... but, at least
1432 * usbtest #11 (unlinks) triggers it regularly, sometimes
1433 * with fifo full. (Only with DMA??)
1435 DBG(3, "BOGUS RX%d ready, csr %04x, count %d\n", epnum, val,
1436 musb_readw(epio, MUSB_RXCOUNT));
1437 musb_h_flush_rxfifo(hw_ep, MUSB_RXCSR_CLRDATATOG);
1443 DBG(5, "<== hw %d rxcsr %04x, urb actual %d (+dma %zu)\n",
1444 epnum, rx_csr, urb->actual_length,
1445 dma ? dma->actual_len : 0);
1447 /* check for errors, concurrent stall & unlink is not really
1449 if (rx_csr & MUSB_RXCSR_H_RXSTALL) {
1450 DBG(3, "RX end %d STALL\n", epnum);
1452 /* stall; record URB status */
1455 } else if (rx_csr & MUSB_RXCSR_H_ERROR) {
1456 DBG(3, "end %d RX proto error\n", epnum);
1459 musb_writeb(epio, MUSB_RXINTERVAL, 0);
1461 } else if (rx_csr & MUSB_RXCSR_DATAERROR) {
1463 if (USB_ENDPOINT_XFER_ISOC != qh->type) {
1464 DBG(6, "RX end %d NAK timeout\n", epnum);
1466 /* NOTE: NAKing is *NOT* an error, so we want to
1467 * continue. Except ... if there's a request for
1468 * another QH, use that instead of starving it.
1470 * Devices like Ethernet and serial adapters keep
1471 * reads posted at all times, which will starve
1472 * other devices without this logic.
1474 if (usb_pipebulk(urb->pipe)
1476 && !list_is_singular(&musb->in_bulk)) {
1477 musb_bulk_rx_nak_timeout(musb, hw_ep);
1480 musb_ep_select(mbase, epnum);
1481 rx_csr |= MUSB_RXCSR_H_WZC_BITS;
1482 rx_csr &= ~MUSB_RXCSR_DATAERROR;
1483 musb_writew(epio, MUSB_RXCSR, rx_csr);
1487 DBG(4, "RX end %d ISO data error\n", epnum);
1488 /* packet error reported later */
1493 /* faults abort the transfer */
1495 /* clean up dma and collect transfer count */
1496 if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) {
1497 dma->status = MUSB_DMA_STATUS_CORE_ABORT;
1498 (void) musb->dma_controller->channel_abort(dma);
1499 xfer_len = dma->actual_len;
1501 musb_h_flush_rxfifo(hw_ep, MUSB_RXCSR_CLRDATATOG);
1502 musb_writeb(epio, MUSB_RXINTERVAL, 0);
1507 if (unlikely(dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY)) {
1508 /* SHOULD NEVER HAPPEN ... but at least DaVinci has done it */
1509 ERR("RX%d dma busy, csr %04x\n", epnum, rx_csr);
1513 /* thorough shutdown for now ... given more precise fault handling
1514 * and better queueing support, we might keep a DMA pipeline going
1515 * while processing this irq for earlier completions.
1518 /* FIXME this is _way_ too much in-line logic for Mentor DMA */
1520 #ifndef CONFIG_USB_INVENTRA_DMA
1521 if (rx_csr & MUSB_RXCSR_H_REQPKT) {
1522 /* REVISIT this happened for a while on some short reads...
1523 * the cleanup still needs investigation... looks bad...
1524 * and also duplicates dma cleanup code above ... plus,
1525 * shouldn't this be the "half full" double buffer case?
1527 if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) {
1528 dma->status = MUSB_DMA_STATUS_CORE_ABORT;
1529 (void) musb->dma_controller->channel_abort(dma);
1530 xfer_len = dma->actual_len;
1534 DBG(2, "RXCSR%d %04x, reqpkt, len %zu%s\n", epnum, rx_csr,
1535 xfer_len, dma ? ", dma" : "");
1536 rx_csr &= ~MUSB_RXCSR_H_REQPKT;
1538 musb_ep_select(mbase, epnum);
1539 musb_writew(epio, MUSB_RXCSR,
1540 MUSB_RXCSR_H_WZC_BITS | rx_csr);
1543 if (dma && (rx_csr & MUSB_RXCSR_DMAENAB)) {
1544 xfer_len = dma->actual_len;
1546 val &= ~(MUSB_RXCSR_DMAENAB
1547 | MUSB_RXCSR_H_AUTOREQ
1548 | MUSB_RXCSR_AUTOCLEAR
1549 | MUSB_RXCSR_RXPKTRDY);
1550 musb_writew(hw_ep->regs, MUSB_RXCSR, val);
1552 #ifdef CONFIG_USB_INVENTRA_DMA
1553 if (usb_pipeisoc(pipe)) {
1554 struct usb_iso_packet_descriptor *d;
1556 d = urb->iso_frame_desc + qh->iso_idx;
1557 d->actual_length = xfer_len;
1559 /* even if there was an error, we did the dma
1560 * for iso_frame_desc->length
1562 if (d->status != EILSEQ && d->status != -EOVERFLOW)
1565 if (++qh->iso_idx >= urb->number_of_packets)
1571 /* done if urb buffer is full or short packet is recd */
1572 done = (urb->actual_length + xfer_len >=
1573 urb->transfer_buffer_length
1574 || dma->actual_len < qh->maxpacket);
1577 /* send IN token for next packet, without AUTOREQ */
1579 val |= MUSB_RXCSR_H_REQPKT;
1580 musb_writew(epio, MUSB_RXCSR,
1581 MUSB_RXCSR_H_WZC_BITS | val);
1584 DBG(4, "ep %d dma %s, rxcsr %04x, rxcount %d\n", epnum,
1585 done ? "off" : "reset",
1586 musb_readw(epio, MUSB_RXCSR),
1587 musb_readw(epio, MUSB_RXCOUNT));
1591 } else if (urb->status == -EINPROGRESS) {
1592 /* if no errors, be sure a packet is ready for unloading */
1593 if (unlikely(!(rx_csr & MUSB_RXCSR_RXPKTRDY))) {
1595 ERR("Rx interrupt with no errors or packet!\n");
1597 /* FIXME this is another "SHOULD NEVER HAPPEN" */
1600 /* do the proper sequence to abort the transfer */
1601 musb_ep_select(mbase, epnum);
1602 val &= ~MUSB_RXCSR_H_REQPKT;
1603 musb_writew(epio, MUSB_RXCSR, val);
1607 /* we are expecting IN packets */
1608 #ifdef CONFIG_USB_INVENTRA_DMA
1610 struct dma_controller *c;
1615 rx_count = musb_readw(epio, MUSB_RXCOUNT);
1617 DBG(2, "RX%d count %d, buffer 0x%x len %d/%d\n",
1620 + urb->actual_length,
1622 urb->transfer_buffer_length);
1624 c = musb->dma_controller;
1626 if (usb_pipeisoc(pipe)) {
1628 struct usb_iso_packet_descriptor *d;
1630 d = urb->iso_frame_desc + qh->iso_idx;
1636 if (rx_count > d->length) {
1638 status = -EOVERFLOW;
1641 DBG(2, "** OVERFLOW %d into %d\n",\
1642 rx_count, d->length);
1648 buf = urb->transfer_dma + d->offset;
1651 buf = urb->transfer_dma +
1655 dma->desired_mode = 0;
1657 /* because of the issue below, mode 1 will
1658 * only rarely behave with correct semantics.
1660 if ((urb->transfer_flags &
1662 && (urb->transfer_buffer_length -
1665 dma->desired_mode = 1;
1666 if (rx_count < hw_ep->max_packet_sz_rx) {
1668 dma->bDesiredMode = 0;
1670 length = urb->transfer_buffer_length;
1674 /* Disadvantage of using mode 1:
1675 * It's basically usable only for mass storage class; essentially all
1676 * other protocols also terminate transfers on short packets.
1679 * An extra IN token is sent at the end of the transfer (due to AUTOREQ)
1680 * If you try to use mode 1 for (transfer_buffer_length - 512), and try
1681 * to use the extra IN token to grab the last packet using mode 0, then
1682 * the problem is that you cannot be sure when the device will send the
1683 * last packet and RxPktRdy set. Sometimes the packet is recd too soon
1684 * such that it gets lost when RxCSR is re-set at the end of the mode 1
1685 * transfer, while sometimes it is recd just a little late so that if you
1686 * try to configure for mode 0 soon after the mode 1 transfer is
1687 * completed, you will find rxcount 0. Okay, so you might think why not
1688 * wait for an interrupt when the pkt is recd. Well, you won't get any!
1691 val = musb_readw(epio, MUSB_RXCSR);
1692 val &= ~MUSB_RXCSR_H_REQPKT;
1694 if (dma->desired_mode == 0)
1695 val &= ~MUSB_RXCSR_H_AUTOREQ;
1697 val |= MUSB_RXCSR_H_AUTOREQ;
1698 val |= MUSB_RXCSR_AUTOCLEAR | MUSB_RXCSR_DMAENAB;
1700 musb_writew(epio, MUSB_RXCSR,
1701 MUSB_RXCSR_H_WZC_BITS | val);
1703 /* REVISIT if when actual_length != 0,
1704 * transfer_buffer_length needs to be
1707 ret = c->channel_program(
1709 dma->desired_mode, buf, length);
1712 c->channel_release(dma);
1713 hw_ep->rx_channel = NULL;
1715 /* REVISIT reset CSR */
1718 #endif /* Mentor DMA */
1721 done = musb_host_packet_rx(musb, urb,
1723 DBG(6, "read %spacket\n", done ? "last " : "");
1728 urb->actual_length += xfer_len;
1729 qh->offset += xfer_len;
1731 if (urb->status == -EINPROGRESS)
1732 urb->status = status;
1733 musb_advance_schedule(musb, urb, hw_ep, USB_DIR_IN);
1737 /* schedule nodes correspond to peripheral endpoints, like an OHCI QH.
1738 * the software schedule associates multiple such nodes with a given
1739 * host side hardware endpoint + direction; scheduling may activate
1740 * that hardware endpoint.
1742 static int musb_schedule(
1749 int best_end, epnum;
1750 struct musb_hw_ep *hw_ep = NULL;
1751 struct list_head *head = NULL;
1753 /* use fixed hardware for control and bulk */
1754 if (qh->type == USB_ENDPOINT_XFER_CONTROL) {
1755 head = &musb->control;
1756 hw_ep = musb->control_ep;
1760 /* else, periodic transfers get muxed to other endpoints */
1763 * We know this qh hasn't been scheduled, so all we need to do
1764 * is choose which hardware endpoint to put it on ...
1766 * REVISIT what we really want here is a regular schedule tree
1767 * like e.g. OHCI uses.
1772 for (epnum = 1, hw_ep = musb->endpoints + 1;
1773 epnum < musb->nr_endpoints;
1777 if (is_in || hw_ep->is_shared_fifo) {
1778 if (hw_ep->in_qh != NULL)
1780 } else if (hw_ep->out_qh != NULL)
1783 if (hw_ep == musb->bulk_ep)
1787 diff = hw_ep->max_packet_sz_rx - qh->maxpacket;
1789 diff = hw_ep->max_packet_sz_tx - qh->maxpacket;
1791 if (diff >= 0 && best_diff > diff) {
1796 /* use bulk reserved ep1 if no other ep is free */
1797 if (best_end < 0 && qh->type == USB_ENDPOINT_XFER_BULK) {
1798 hw_ep = musb->bulk_ep;
1800 head = &musb->in_bulk;
1802 head = &musb->out_bulk;
1804 /* Enable bulk RX NAK timeout scheme when bulk requests are
1805 * multiplexed. This scheme doen't work in high speed to full
1806 * speed scenario as NAK interrupts are not coming from a
1807 * full speed device connected to a high speed device.
1808 * NAK timeout interval is 8 (128 uframe or 16ms) for HS and
1809 * 4 (8 frame or 8ms) for FS device.
1811 if (is_in && qh->dev)
1813 (USB_SPEED_HIGH == qh->dev->speed) ? 8 : 4;
1815 } else if (best_end < 0) {
1821 hw_ep = musb->endpoints + best_end;
1822 DBG(4, "qh %p periodic slot %d\n", qh, best_end);
1825 idle = list_empty(head);
1826 list_add_tail(&qh->ring, head);
1830 qh->hep->hcpriv = qh;
1832 musb_start_urb(musb, is_in, qh);
1836 static int musb_urb_enqueue(
1837 struct usb_hcd *hcd,
1841 unsigned long flags;
1842 struct musb *musb = hcd_to_musb(hcd);
1843 struct usb_host_endpoint *hep = urb->ep;
1844 struct musb_qh *qh = hep->hcpriv;
1845 struct usb_endpoint_descriptor *epd = &hep->desc;
1850 /* host role must be active */
1851 if (!is_host_active(musb) || !musb->is_active)
1854 spin_lock_irqsave(&musb->lock, flags);
1855 ret = usb_hcd_link_urb_to_ep(hcd, urb);
1856 spin_unlock_irqrestore(&musb->lock, flags);
1860 /* DMA mapping was already done, if needed, and this urb is on
1861 * hep->urb_list ... so there's little to do unless hep wasn't
1862 * yet scheduled onto a live qh.
1864 * REVISIT best to keep hep->hcpriv valid until the endpoint gets
1865 * disabled, testing for empty qh->ring and avoiding qh setup costs
1866 * except for the first urb queued after a config change.
1873 /* Allocate and initialize qh, minimizing the work done each time
1874 * hw_ep gets reprogrammed, or with irqs blocked. Then schedule it.
1876 * REVISIT consider a dedicated qh kmem_cache, so it's harder
1877 * for bugs in other kernel code to break this driver...
1879 qh = kzalloc(sizeof *qh, mem_flags);
1881 spin_lock_irqsave(&musb->lock, flags);
1882 usb_hcd_unlink_urb_from_ep(hcd, urb);
1883 spin_unlock_irqrestore(&musb->lock, flags);
1889 INIT_LIST_HEAD(&qh->ring);
1892 qh->maxpacket = le16_to_cpu(epd->wMaxPacketSize);
1894 /* no high bandwidth support yet */
1895 if (qh->maxpacket & ~0x7ff) {
1900 qh->epnum = usb_endpoint_num(epd);
1901 qh->type = usb_endpoint_type(epd);
1903 /* NOTE: urb->dev->devnum is wrong during SET_ADDRESS */
1904 qh->addr_reg = (u8) usb_pipedevice(urb->pipe);
1906 /* precompute rxtype/txtype/type0 register */
1907 type_reg = (qh->type << 4) | qh->epnum;
1908 switch (urb->dev->speed) {
1912 case USB_SPEED_FULL:
1918 qh->type_reg = type_reg;
1920 /* Precompute RXINTERVAL/TXINTERVAL register */
1922 case USB_ENDPOINT_XFER_INT:
1924 * Full/low speeds use the linear encoding,
1925 * high speed uses the logarithmic encoding.
1927 if (urb->dev->speed <= USB_SPEED_FULL) {
1928 interval = max_t(u8, epd->bInterval, 1);
1932 case USB_ENDPOINT_XFER_ISOC:
1933 /* ISO always uses logarithmic encoding */
1934 interval = min_t(u8, epd->bInterval, 16);
1937 /* REVISIT we actually want to use NAK limits, hinting to the
1938 * transfer scheduling logic to try some other qh, e.g. try
1941 * interval = (USB_SPEED_HIGH == urb->dev->speed) ? 16 : 2;
1943 * The downside of disabling this is that transfer scheduling
1944 * gets VERY unfair for nonperiodic transfers; a misbehaving
1945 * peripheral could make that hurt. That's perfectly normal
1946 * for reads from network or serial adapters ... so we have
1947 * partial NAKlimit support for bulk RX.
1949 * The upside of disabling it is simpler transfer scheduling.
1953 qh->intv_reg = interval;
1955 /* precompute addressing for external hub/tt ports */
1956 if (musb->is_multipoint) {
1957 struct usb_device *parent = urb->dev->parent;
1959 if (parent != hcd->self.root_hub) {
1960 qh->h_addr_reg = (u8) parent->devnum;
1962 /* set up tt info if needed */
1964 qh->h_port_reg = (u8) urb->dev->ttport;
1965 if (urb->dev->tt->hub)
1967 (u8) urb->dev->tt->hub->devnum;
1968 if (urb->dev->tt->multi)
1969 qh->h_addr_reg |= 0x80;
1974 /* invariant: hep->hcpriv is null OR the qh that's already scheduled.
1975 * until we get real dma queues (with an entry for each urb/buffer),
1976 * we only have work to do in the former case.
1978 spin_lock_irqsave(&musb->lock, flags);
1980 /* some concurrent activity submitted another urb to hep...
1981 * odd, rare, error prone, but legal.
1986 ret = musb_schedule(musb, qh,
1987 epd->bEndpointAddress & USB_ENDPOINT_DIR_MASK);
1991 /* FIXME set urb->start_frame for iso/intr, it's tested in
1992 * musb_start_urb(), but otherwise only konicawc cares ...
1995 spin_unlock_irqrestore(&musb->lock, flags);
1999 spin_lock_irqsave(&musb->lock, flags);
2000 usb_hcd_unlink_urb_from_ep(hcd, urb);
2001 spin_unlock_irqrestore(&musb->lock, flags);
2009 * abort a transfer that's at the head of a hardware queue.
2010 * called with controller locked, irqs blocked
2011 * that hardware queue advances to the next transfer, unless prevented
2013 static int musb_cleanup_urb(struct urb *urb, struct musb_qh *qh, int is_in)
2015 struct musb_hw_ep *ep = qh->hw_ep;
2016 void __iomem *epio = ep->regs;
2017 unsigned hw_end = ep->epnum;
2018 void __iomem *regs = ep->musb->mregs;
2022 musb_ep_select(regs, hw_end);
2024 if (is_dma_capable()) {
2025 struct dma_channel *dma;
2027 dma = is_in ? ep->rx_channel : ep->tx_channel;
2029 status = ep->musb->dma_controller->channel_abort(dma);
2031 "abort %cX%d DMA for urb %p --> %d\n",
2032 is_in ? 'R' : 'T', ep->epnum,
2034 urb->actual_length += dma->actual_len;
2038 /* turn off DMA requests, discard state, stop polling ... */
2040 /* giveback saves bulk toggle */
2041 csr = musb_h_flush_rxfifo(ep, 0);
2043 /* REVISIT we still get an irq; should likely clear the
2044 * endpoint's irq status here to avoid bogus irqs.
2045 * clearing that status is platform-specific...
2048 musb_h_tx_flush_fifo(ep);
2049 csr = musb_readw(epio, MUSB_TXCSR);
2050 csr &= ~(MUSB_TXCSR_AUTOSET
2051 | MUSB_TXCSR_DMAENAB
2052 | MUSB_TXCSR_H_RXSTALL
2053 | MUSB_TXCSR_H_NAKTIMEOUT
2054 | MUSB_TXCSR_H_ERROR
2055 | MUSB_TXCSR_TXPKTRDY);
2056 musb_writew(epio, MUSB_TXCSR, csr);
2057 /* REVISIT may need to clear FLUSHFIFO ... */
2058 musb_writew(epio, MUSB_TXCSR, csr);
2059 /* flush cpu writebuffer */
2060 csr = musb_readw(epio, MUSB_TXCSR);
2063 musb_advance_schedule(ep->musb, urb, ep, is_in);
2067 static int musb_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
2069 struct musb *musb = hcd_to_musb(hcd);
2071 struct list_head *sched;
2072 unsigned long flags;
2075 DBG(4, "urb=%p, dev%d ep%d%s\n", urb,
2076 usb_pipedevice(urb->pipe),
2077 usb_pipeendpoint(urb->pipe),
2078 usb_pipein(urb->pipe) ? "in" : "out");
2080 spin_lock_irqsave(&musb->lock, flags);
2081 ret = usb_hcd_check_unlink_urb(hcd, urb, status);
2089 /* Any URB not actively programmed into endpoint hardware can be
2090 * immediately given back; that's any URB not at the head of an
2091 * endpoint queue, unless someday we get real DMA queues. And even
2092 * if it's at the head, it might not be known to the hardware...
2094 * Otherwise abort current transfer, pending dma, etc.; urb->status
2095 * has already been updated. This is a synchronous abort; it'd be
2096 * OK to hold off until after some IRQ, though.
2098 if (!qh->is_ready || urb->urb_list.prev != &qh->hep->urb_list)
2102 case USB_ENDPOINT_XFER_CONTROL:
2103 sched = &musb->control;
2105 case USB_ENDPOINT_XFER_BULK:
2107 if (usb_pipein(urb->pipe))
2108 sched = &musb->in_bulk;
2110 sched = &musb->out_bulk;
2114 /* REVISIT when we get a schedule tree, periodic
2115 * transfers won't always be at the head of a
2116 * singleton queue...
2123 /* NOTE: qh is invalid unless !list_empty(&hep->urb_list) */
2124 if (ret < 0 || (sched && qh != first_qh(sched))) {
2125 int ready = qh->is_ready;
2129 __musb_giveback(musb, urb, 0);
2130 qh->is_ready = ready;
2132 /* If nothing else (usually musb_giveback) is using it
2133 * and its URB list has emptied, recycle this qh.
2135 if (ready && list_empty(&qh->hep->urb_list)) {
2136 qh->hep->hcpriv = NULL;
2137 list_del(&qh->ring);
2141 ret = musb_cleanup_urb(urb, qh, urb->pipe & USB_DIR_IN);
2143 spin_unlock_irqrestore(&musb->lock, flags);
2147 /* disable an endpoint */
2149 musb_h_disable(struct usb_hcd *hcd, struct usb_host_endpoint *hep)
2151 u8 epnum = hep->desc.bEndpointAddress;
2152 unsigned long flags;
2153 struct musb *musb = hcd_to_musb(hcd);
2154 u8 is_in = epnum & USB_DIR_IN;
2157 struct list_head *sched;
2159 spin_lock_irqsave(&musb->lock, flags);
2166 case USB_ENDPOINT_XFER_CONTROL:
2167 sched = &musb->control;
2169 case USB_ENDPOINT_XFER_BULK:
2172 sched = &musb->in_bulk;
2174 sched = &musb->out_bulk;
2178 /* REVISIT when we get a schedule tree, periodic transfers
2179 * won't always be at the head of a singleton queue...
2185 /* NOTE: qh is invalid unless !list_empty(&hep->urb_list) */
2187 /* kick first urb off the hardware, if needed */
2189 if (!sched || qh == first_qh(sched)) {
2192 /* make software (then hardware) stop ASAP */
2194 urb->status = -ESHUTDOWN;
2197 musb_cleanup_urb(urb, qh, urb->pipe & USB_DIR_IN);
2199 /* Then nuke all the others ... and advance the
2200 * queue on hw_ep (e.g. bulk ring) when we're done.
2202 while (!list_empty(&hep->urb_list)) {
2204 urb->status = -ESHUTDOWN;
2205 musb_advance_schedule(musb, urb, qh->hw_ep, is_in);
2208 /* Just empty the queue; the hardware is busy with
2209 * other transfers, and since !qh->is_ready nothing
2210 * will activate any of these as it advances.
2212 while (!list_empty(&hep->urb_list))
2213 __musb_giveback(musb, next_urb(qh), -ESHUTDOWN);
2216 list_del(&qh->ring);
2220 spin_unlock_irqrestore(&musb->lock, flags);
2223 static int musb_h_get_frame_number(struct usb_hcd *hcd)
2225 struct musb *musb = hcd_to_musb(hcd);
2227 return musb_readw(musb->mregs, MUSB_FRAME);
2230 static int musb_h_start(struct usb_hcd *hcd)
2232 struct musb *musb = hcd_to_musb(hcd);
2234 /* NOTE: musb_start() is called when the hub driver turns
2235 * on port power, or when (OTG) peripheral starts.
2237 hcd->state = HC_STATE_RUNNING;
2238 musb->port1_status = 0;
2242 static void musb_h_stop(struct usb_hcd *hcd)
2244 musb_stop(hcd_to_musb(hcd));
2245 hcd->state = HC_STATE_HALT;
2248 static int musb_bus_suspend(struct usb_hcd *hcd)
2250 struct musb *musb = hcd_to_musb(hcd);
2252 if (musb->xceiv.state == OTG_STATE_A_SUSPEND)
2255 if (is_host_active(musb) && musb->is_active) {
2256 WARNING("trying to suspend as %s is_active=%i\n",
2257 otg_state_string(musb), musb->is_active);
2263 static int musb_bus_resume(struct usb_hcd *hcd)
2265 /* resuming child port does the work */
2269 const struct hc_driver musb_hc_driver = {
2270 .description = "musb-hcd",
2271 .product_desc = "MUSB HDRC host driver",
2272 .hcd_priv_size = sizeof(struct musb),
2273 .flags = HCD_USB2 | HCD_MEMORY,
2275 /* not using irq handler or reset hooks from usbcore, since
2276 * those must be shared with peripheral code for OTG configs
2279 .start = musb_h_start,
2280 .stop = musb_h_stop,
2282 .get_frame_number = musb_h_get_frame_number,
2284 .urb_enqueue = musb_urb_enqueue,
2285 .urb_dequeue = musb_urb_dequeue,
2286 .endpoint_disable = musb_h_disable,
2288 .hub_status_data = musb_hub_status_data,
2289 .hub_control = musb_hub_control,
2290 .bus_suspend = musb_bus_suspend,
2291 .bus_resume = musb_bus_resume,
2292 /* .start_port_reset = NULL, */
2293 /* .hub_irq_enable = NULL, */