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 * - Still no traffic scheduling code to make NAKing for bulk or control
68 * transfers unable to starve other requests; or to make efficient use
69 * of hardware with periodic transfers. (Note that network drivers
70 * commonly post bulk reads that stay pending for a long time; these
71 * would make very visible trouble.)
73 * - Not tested with HNP, but some SRP paths seem to behave.
75 * NOTE 24-August-2006:
77 * - Bulk traffic finally uses both sides of hardware ep1, freeing up an
78 * extra endpoint for periodic use enabling hub + keybd + mouse. That
79 * mostly works, except that with "usbnet" it's easy to trigger cases
80 * with "ping" where RX loses. (a) ping to davinci, even "ping -f",
81 * fine; but (b) ping _from_ davinci, even "ping -c 1", ICMP RX loses
82 * although ARP RX wins. (That test was done with a full speed link.)
87 * NOTE on endpoint usage:
89 * CONTROL transfers all go through ep0. BULK ones go through dedicated IN
90 * and OUT endpoints ... hardware is dedicated for those "async" queue(s).
92 * (Yes, bulk _could_ use more of the endpoints than that, and would even
93 * benefit from it ... one remote device may easily be NAKing while others
94 * need to perform transfers in that same direction. The same thing could
95 * be done in software though, assuming dma cooperates.)
97 * INTERUPPT and ISOCHRONOUS transfers are scheduled to the other endpoints.
98 * So far that scheduling is both dumb and optimistic: the endpoint will be
99 * "claimed" until its software queue is no longer refilled. No multiplexing
100 * of transfers between endpoints, or anything clever.
104 static void musb_ep_program(struct musb *musb, u8 epnum,
105 struct urb *urb, unsigned int nOut,
109 * Clear TX fifo. Needed to avoid BABBLE errors.
111 static void musb_h_tx_flush_fifo(struct musb_hw_ep *ep)
113 void __iomem *epio = ep->regs;
118 csr = musb_readw(epio, MUSB_TXCSR);
119 while (csr & MUSB_TXCSR_FIFONOTEMPTY) {
121 DBG(3, "Host TX FIFONOTEMPTY csr: %02x\n", csr);
123 csr |= MUSB_TXCSR_FLUSHFIFO;
124 musb_writew(epio, MUSB_TXCSR, csr);
125 csr = musb_readw(epio, MUSB_TXCSR);
126 if (WARN(retries-- < 1,
127 "Could not flush host TX%d fifo: csr: %04x\n",
135 * Start transmit. Caller is responsible for locking shared resources.
136 * musb must be locked.
138 static inline void musb_h_tx_start(struct musb_hw_ep *ep)
142 /* NOTE: no locks here; caller should lock and select EP */
144 txcsr = musb_readw(ep->regs, MUSB_TXCSR);
145 txcsr |= MUSB_TXCSR_TXPKTRDY | MUSB_TXCSR_H_WZC_BITS;
146 musb_writew(ep->regs, MUSB_TXCSR, txcsr);
148 txcsr = MUSB_CSR0_H_SETUPPKT | MUSB_CSR0_TXPKTRDY;
149 musb_writew(ep->regs, MUSB_CSR0, txcsr);
154 static inline void cppi_host_txdma_start(struct musb_hw_ep *ep)
158 /* NOTE: no locks here; caller should lock and select EP */
159 txcsr = musb_readw(ep->regs, MUSB_TXCSR);
160 txcsr |= MUSB_TXCSR_DMAENAB | MUSB_TXCSR_H_WZC_BITS;
161 musb_writew(ep->regs, MUSB_TXCSR, txcsr);
165 * Start the URB at the front of an endpoint's queue
166 * end must be claimed from the caller.
168 * Context: controller locked, irqs blocked
171 musb_start_urb(struct musb *musb, int is_in, struct musb_qh *qh)
176 void __iomem *mbase = musb->mregs;
177 struct urb *urb = next_urb(qh);
178 struct musb_hw_ep *hw_ep = qh->hw_ep;
179 int epnum = hw_ep->epnum;
181 /* initialize software qh state */
185 /* gather right source of data */
187 case USB_ENDPOINT_XFER_CONTROL:
188 /* control transfers always start with SETUP */
191 musb->ep0_stage = MUSB_EP0_START;
192 buf = urb->setup_packet;
195 case USB_ENDPOINT_XFER_ISOC:
198 buf = urb->transfer_buffer + urb->iso_frame_desc[0].offset;
199 len = urb->iso_frame_desc[0].length;
201 default: /* bulk, interrupt */
202 buf = urb->transfer_buffer;
203 len = urb->transfer_buffer_length;
206 DBG(4, "qh %p urb %p dev%d ep%d%s%s, hw_ep %d, %p/%d\n",
207 qh, urb, usb_pipedevice(urb->pipe), qh->epnum,
208 is_in ? "in" : "out",
209 ({char *s; switch (qh->type) {
210 case USB_ENDPOINT_XFER_CONTROL: s = ""; break;
211 case USB_ENDPOINT_XFER_BULK: s = "-bulk"; break;
212 case USB_ENDPOINT_XFER_ISOC: s = "-iso"; break;
213 default: s = "-intr"; break;
217 /* Configure endpoint */
218 if (is_in || hw_ep->is_shared_fifo)
222 musb_ep_program(musb, epnum, urb, !is_in, buf, len);
224 /* transmit may have more work: start it when it is time */
228 /* determine if the time is right for a periodic transfer */
230 case USB_ENDPOINT_XFER_ISOC:
231 case USB_ENDPOINT_XFER_INT:
232 DBG(3, "check whether there's still time for periodic Tx\n");
234 frame = musb_readw(mbase, MUSB_FRAME);
235 /* FIXME this doesn't implement that scheduling policy ...
236 * or handle framecounter wrapping
238 if ((urb->transfer_flags & URB_ISO_ASAP)
239 || (frame >= urb->start_frame)) {
240 /* REVISIT the SOF irq handler shouldn't duplicate
241 * this code; and we don't init urb->start_frame...
246 qh->frame = urb->start_frame;
247 /* enable SOF interrupt so we can count down */
248 DBG(1, "SOF for %d\n", epnum);
249 #if 1 /* ifndef CONFIG_ARCH_DAVINCI */
250 musb_writeb(mbase, MUSB_INTRUSBE, 0xff);
256 DBG(4, "Start TX%d %s\n", epnum,
257 hw_ep->tx_channel ? "dma" : "pio");
259 if (!hw_ep->tx_channel)
260 musb_h_tx_start(hw_ep);
261 else if (cppi_ti_dma() || tusb_dma_omap())
262 cppi_host_txdma_start(hw_ep);
266 /* caller owns controller lock, irqs are blocked */
268 __musb_giveback(struct musb *musb, struct urb *urb, int status)
269 __releases(musb->lock)
270 __acquires(musb->lock)
272 DBG(({ int level; switch (status) {
276 /* common/boring faults */
287 "complete %p %pF (%d), dev%d ep%d%s, %d/%d\n",
288 urb, urb->complete, status,
289 usb_pipedevice(urb->pipe),
290 usb_pipeendpoint(urb->pipe),
291 usb_pipein(urb->pipe) ? "in" : "out",
292 urb->actual_length, urb->transfer_buffer_length
295 usb_hcd_unlink_urb_from_ep(musb_to_hcd(musb), urb);
296 spin_unlock(&musb->lock);
297 usb_hcd_giveback_urb(musb_to_hcd(musb), urb, status);
298 spin_lock(&musb->lock);
301 /* for bulk/interrupt endpoints only */
303 musb_save_toggle(struct musb_hw_ep *ep, int is_in, struct urb *urb)
305 struct usb_device *udev = urb->dev;
307 void __iomem *epio = ep->regs;
310 /* FIXME: the current Mentor DMA code seems to have
311 * problems getting toggle correct.
314 if (is_in || ep->is_shared_fifo)
320 csr = musb_readw(epio, MUSB_TXCSR);
321 usb_settoggle(udev, qh->epnum, 1,
322 (csr & MUSB_TXCSR_H_DATATOGGLE)
325 csr = musb_readw(epio, MUSB_RXCSR);
326 usb_settoggle(udev, qh->epnum, 0,
327 (csr & MUSB_RXCSR_H_DATATOGGLE)
332 /* caller owns controller lock, irqs are blocked */
333 static struct musb_qh *
334 musb_giveback(struct musb_qh *qh, struct urb *urb, int status)
336 struct musb_hw_ep *ep = qh->hw_ep;
337 struct musb *musb = ep->musb;
338 int is_in = usb_pipein(urb->pipe);
339 int ready = qh->is_ready;
341 /* save toggle eagerly, for paranoia */
343 case USB_ENDPOINT_XFER_BULK:
344 case USB_ENDPOINT_XFER_INT:
345 musb_save_toggle(ep, is_in, urb);
347 case USB_ENDPOINT_XFER_ISOC:
348 if (status == 0 && urb->error_count)
354 __musb_giveback(musb, urb, status);
355 qh->is_ready = ready;
357 /* reclaim resources (and bandwidth) ASAP; deschedule it, and
358 * invalidate qh as soon as list_empty(&hep->urb_list)
360 if (list_empty(&qh->hep->urb_list)) {
361 struct list_head *head;
368 /* clobber old pointers to this qh */
369 if (is_in || ep->is_shared_fifo)
373 qh->hep->hcpriv = NULL;
377 case USB_ENDPOINT_XFER_CONTROL:
378 case USB_ENDPOINT_XFER_BULK:
379 /* fifo policy for these lists, except that NAKing
380 * should rotate a qh to the end (for fairness).
383 head = qh->ring.prev;
390 case USB_ENDPOINT_XFER_ISOC:
391 case USB_ENDPOINT_XFER_INT:
392 /* this is where periodic bandwidth should be
393 * de-allocated if it's tracked and allocated;
394 * and where we'd update the schedule tree...
396 musb->periodic[ep->epnum] = NULL;
406 * Advance this hardware endpoint's queue, completing the specified urb and
407 * advancing to either the next urb queued to that qh, or else invalidating
408 * that qh and advancing to the next qh scheduled after the current one.
410 * Context: caller owns controller lock, irqs are blocked
413 musb_advance_schedule(struct musb *musb, struct urb *urb,
414 struct musb_hw_ep *hw_ep, int is_in)
418 if (is_in || hw_ep->is_shared_fifo)
423 if (urb->status == -EINPROGRESS)
424 qh = musb_giveback(qh, urb, 0);
426 qh = musb_giveback(qh, urb, urb->status);
428 if (qh != NULL && qh->is_ready) {
429 DBG(4, "... next ep%d %cX urb %p\n",
430 hw_ep->epnum, is_in ? 'R' : 'T',
432 musb_start_urb(musb, is_in, qh);
436 static u16 musb_h_flush_rxfifo(struct musb_hw_ep *hw_ep, u16 csr)
438 /* we don't want fifo to fill itself again;
439 * ignore dma (various models),
440 * leave toggle alone (may not have been saved yet)
442 csr |= MUSB_RXCSR_FLUSHFIFO | MUSB_RXCSR_RXPKTRDY;
443 csr &= ~(MUSB_RXCSR_H_REQPKT
444 | MUSB_RXCSR_H_AUTOREQ
445 | MUSB_RXCSR_AUTOCLEAR);
447 /* write 2x to allow double buffering */
448 musb_writew(hw_ep->regs, MUSB_RXCSR, csr);
449 musb_writew(hw_ep->regs, MUSB_RXCSR, csr);
451 /* flush writebuffer */
452 return musb_readw(hw_ep->regs, MUSB_RXCSR);
456 * PIO RX for a packet (or part of it).
459 musb_host_packet_rx(struct musb *musb, struct urb *urb, u8 epnum, u8 iso_err)
467 struct musb_hw_ep *hw_ep = musb->endpoints + epnum;
468 void __iomem *epio = hw_ep->regs;
469 struct musb_qh *qh = hw_ep->in_qh;
470 int pipe = urb->pipe;
471 void *buffer = urb->transfer_buffer;
473 /* musb_ep_select(mbase, epnum); */
474 rx_count = musb_readw(epio, MUSB_RXCOUNT);
475 DBG(3, "RX%d count %d, buffer %p len %d/%d\n", epnum, rx_count,
476 urb->transfer_buffer, qh->offset,
477 urb->transfer_buffer_length);
480 if (usb_pipeisoc(pipe)) {
482 struct usb_iso_packet_descriptor *d;
489 d = urb->iso_frame_desc + qh->iso_idx;
490 buf = buffer + d->offset;
492 if (rx_count > length) {
497 DBG(2, "** OVERFLOW %d into %d\n", rx_count, length);
501 urb->actual_length += length;
502 d->actual_length = length;
506 /* see if we are done */
507 done = (++qh->iso_idx >= urb->number_of_packets);
510 buf = buffer + qh->offset;
511 length = urb->transfer_buffer_length - qh->offset;
512 if (rx_count > length) {
513 if (urb->status == -EINPROGRESS)
514 urb->status = -EOVERFLOW;
515 DBG(2, "** OVERFLOW %d into %d\n", rx_count, length);
519 urb->actual_length += length;
520 qh->offset += length;
522 /* see if we are done */
523 done = (urb->actual_length == urb->transfer_buffer_length)
524 || (rx_count < qh->maxpacket)
525 || (urb->status != -EINPROGRESS);
527 && (urb->status == -EINPROGRESS)
528 && (urb->transfer_flags & URB_SHORT_NOT_OK)
529 && (urb->actual_length
530 < urb->transfer_buffer_length))
531 urb->status = -EREMOTEIO;
534 musb_read_fifo(hw_ep, length, buf);
536 csr = musb_readw(epio, MUSB_RXCSR);
537 csr |= MUSB_RXCSR_H_WZC_BITS;
538 if (unlikely(do_flush))
539 musb_h_flush_rxfifo(hw_ep, csr);
541 /* REVISIT this assumes AUTOCLEAR is never set */
542 csr &= ~(MUSB_RXCSR_RXPKTRDY | MUSB_RXCSR_H_REQPKT);
544 csr |= MUSB_RXCSR_H_REQPKT;
545 musb_writew(epio, MUSB_RXCSR, csr);
551 /* we don't always need to reinit a given side of an endpoint...
552 * when we do, use tx/rx reinit routine and then construct a new CSR
553 * to address data toggle, NYET, and DMA or PIO.
555 * it's possible that driver bugs (especially for DMA) or aborting a
556 * transfer might have left the endpoint busier than it should be.
557 * the busy/not-empty tests are basically paranoia.
560 musb_rx_reinit(struct musb *musb, struct musb_qh *qh, struct musb_hw_ep *ep)
564 /* NOTE: we know the "rx" fifo reinit never triggers for ep0.
565 * That always uses tx_reinit since ep0 repurposes TX register
566 * offsets; the initial SETUP packet is also a kind of OUT.
569 /* if programmed for Tx, put it in RX mode */
570 if (ep->is_shared_fifo) {
571 csr = musb_readw(ep->regs, MUSB_TXCSR);
572 if (csr & MUSB_TXCSR_MODE) {
573 musb_h_tx_flush_fifo(ep);
574 musb_writew(ep->regs, MUSB_TXCSR,
575 MUSB_TXCSR_FRCDATATOG);
577 /* clear mode (and everything else) to enable Rx */
578 musb_writew(ep->regs, MUSB_TXCSR, 0);
580 /* scrub all previous state, clearing toggle */
582 csr = musb_readw(ep->regs, MUSB_RXCSR);
583 if (csr & MUSB_RXCSR_RXPKTRDY)
584 WARNING("rx%d, packet/%d ready?\n", ep->epnum,
585 musb_readw(ep->regs, MUSB_RXCOUNT));
587 musb_h_flush_rxfifo(ep, MUSB_RXCSR_CLRDATATOG);
590 /* target addr and (for multipoint) hub addr/port */
591 if (musb->is_multipoint) {
592 musb_write_rxfunaddr(ep->target_regs, qh->addr_reg);
593 musb_write_rxhubaddr(ep->target_regs, qh->h_addr_reg);
594 musb_write_rxhubport(ep->target_regs, qh->h_port_reg);
597 musb_writeb(musb->mregs, MUSB_FADDR, qh->addr_reg);
599 /* protocol/endpoint, interval/NAKlimit, i/o size */
600 musb_writeb(ep->regs, MUSB_RXTYPE, qh->type_reg);
601 musb_writeb(ep->regs, MUSB_RXINTERVAL, qh->intv_reg);
602 /* NOTE: bulk combining rewrites high bits of maxpacket */
603 musb_writew(ep->regs, MUSB_RXMAXP, qh->maxpacket);
610 * Program an HDRC endpoint as per the given URB
611 * Context: irqs blocked, controller lock held
613 static void musb_ep_program(struct musb *musb, u8 epnum,
614 struct urb *urb, unsigned int is_out,
617 struct dma_controller *dma_controller;
618 struct dma_channel *dma_channel;
620 void __iomem *mbase = musb->mregs;
621 struct musb_hw_ep *hw_ep = musb->endpoints + epnum;
622 void __iomem *epio = hw_ep->regs;
626 if (!is_out || hw_ep->is_shared_fifo)
631 packet_sz = qh->maxpacket;
633 DBG(3, "%s hw%d urb %p spd%d dev%d ep%d%s "
634 "h_addr%02x h_port%02x bytes %d\n",
635 is_out ? "-->" : "<--",
636 epnum, urb, urb->dev->speed,
637 qh->addr_reg, qh->epnum, is_out ? "out" : "in",
638 qh->h_addr_reg, qh->h_port_reg,
641 musb_ep_select(mbase, epnum);
643 /* candidate for DMA? */
644 dma_controller = musb->dma_controller;
645 if (is_dma_capable() && epnum && dma_controller) {
646 dma_channel = is_out ? hw_ep->tx_channel : hw_ep->rx_channel;
648 dma_channel = dma_controller->channel_alloc(
649 dma_controller, hw_ep, is_out);
651 hw_ep->tx_channel = dma_channel;
653 hw_ep->rx_channel = dma_channel;
658 /* make sure we clear DMAEnab, autoSet bits from previous run */
660 /* OUT/transmit/EP0 or IN/receive? */
666 csr = musb_readw(epio, MUSB_TXCSR);
668 /* disable interrupt in case we flush */
669 int_txe = musb_readw(mbase, MUSB_INTRTXE);
670 musb_writew(mbase, MUSB_INTRTXE, int_txe & ~(1 << epnum));
672 /* general endpoint setup */
674 /* ASSERT: TXCSR_DMAENAB was already cleared */
676 /* flush all old state, set default */
677 musb_h_tx_flush_fifo(hw_ep);
678 csr &= ~(MUSB_TXCSR_H_NAKTIMEOUT
680 | MUSB_TXCSR_FRCDATATOG
681 | MUSB_TXCSR_H_RXSTALL
683 | MUSB_TXCSR_TXPKTRDY
685 csr |= MUSB_TXCSR_MODE;
687 if (usb_gettoggle(urb->dev,
689 csr |= MUSB_TXCSR_H_WR_DATATOGGLE
690 | MUSB_TXCSR_H_DATATOGGLE;
692 csr |= MUSB_TXCSR_CLRDATATOG;
694 /* twice in case of double packet buffering */
695 musb_writew(epio, MUSB_TXCSR, csr);
696 /* REVISIT may need to clear FLUSHFIFO ... */
697 musb_writew(epio, MUSB_TXCSR, csr);
698 csr = musb_readw(epio, MUSB_TXCSR);
700 /* endpoint 0: just flush */
701 musb_writew(epio, MUSB_CSR0,
702 csr | MUSB_CSR0_FLUSHFIFO);
703 musb_writew(epio, MUSB_CSR0,
704 csr | MUSB_CSR0_FLUSHFIFO);
707 /* target addr and (for multipoint) hub addr/port */
708 if (musb->is_multipoint) {
709 musb_write_txfunaddr(mbase, epnum, qh->addr_reg);
710 musb_write_txhubaddr(mbase, epnum, qh->h_addr_reg);
711 musb_write_txhubport(mbase, epnum, qh->h_port_reg);
712 /* FIXME if !epnum, do the same for RX ... */
714 musb_writeb(mbase, MUSB_FADDR, qh->addr_reg);
716 /* protocol/endpoint/interval/NAKlimit */
718 musb_writeb(epio, MUSB_TXTYPE, qh->type_reg);
719 if (can_bulk_split(musb, qh->type))
720 musb_writew(epio, MUSB_TXMAXP,
722 | ((hw_ep->max_packet_sz_tx /
723 packet_sz) - 1) << 11);
725 musb_writew(epio, MUSB_TXMAXP,
727 musb_writeb(epio, MUSB_TXINTERVAL, qh->intv_reg);
729 musb_writeb(epio, MUSB_NAKLIMIT0, qh->intv_reg);
730 if (musb->is_multipoint)
731 musb_writeb(epio, MUSB_TYPE0,
735 if (can_bulk_split(musb, qh->type))
736 load_count = min((u32) hw_ep->max_packet_sz_tx,
739 load_count = min((u32) packet_sz, len);
741 if (musb_inventra_dma() && dma_channel) {
743 /* clear previous state */
744 csr = musb_readw(epio, MUSB_TXCSR);
745 csr &= ~(MUSB_TXCSR_AUTOSET
747 | MUSB_TXCSR_DMAENAB);
748 csr |= MUSB_TXCSR_MODE;
749 musb_writew(epio, MUSB_TXCSR,
750 csr | MUSB_TXCSR_MODE);
752 qh->segsize = min(len, dma_channel->max_len);
754 if (qh->segsize <= packet_sz)
755 dma_channel->desired_mode = 0;
757 dma_channel->desired_mode = 1;
760 if (dma_channel->desired_mode == 0) {
761 csr &= ~(MUSB_TXCSR_AUTOSET
762 | MUSB_TXCSR_DMAMODE);
763 csr |= (MUSB_TXCSR_DMAENAB);
764 /* against programming guide */
766 csr |= (MUSB_TXCSR_AUTOSET
768 | MUSB_TXCSR_DMAMODE);
770 musb_writew(epio, MUSB_TXCSR, csr);
772 dma_ok = dma_controller->channel_program(
773 dma_channel, packet_sz,
774 dma_channel->desired_mode,
780 dma_controller->channel_release(dma_channel);
782 hw_ep->tx_channel = NULL;
784 hw_ep->rx_channel = NULL;
789 /* candidate for DMA */
790 if ((cppi_ti_dma() || tusb_dma_omap()) && dma_channel) {
792 /* program endpoint CSRs first, then setup DMA.
793 * assume CPPI setup succeeds.
794 * defer enabling dma.
796 csr = musb_readw(epio, MUSB_TXCSR);
797 csr &= ~(MUSB_TXCSR_AUTOSET
799 | MUSB_TXCSR_DMAENAB);
800 csr |= MUSB_TXCSR_MODE;
801 musb_writew(epio, MUSB_TXCSR,
802 csr | MUSB_TXCSR_MODE);
804 dma_channel->actual_len = 0L;
807 /* TX uses "rndis" mode automatically, but needs help
808 * to identify the zero-length-final-packet case.
810 dma_ok = dma_controller->channel_program(
811 dma_channel, packet_sz,
820 dma_controller->channel_release(dma_channel);
821 hw_ep->tx_channel = NULL;
824 /* REVISIT there's an error path here that
825 * needs handling: can't do dma, but
826 * there's no pio buffer address...
832 /* ASSERT: TXCSR_DMAENAB was already cleared */
834 /* PIO to load FIFO */
835 qh->segsize = load_count;
836 musb_write_fifo(hw_ep, load_count, buf);
837 csr = musb_readw(epio, MUSB_TXCSR);
838 csr &= ~(MUSB_TXCSR_DMAENAB
840 | MUSB_TXCSR_AUTOSET);
842 csr |= MUSB_TXCSR_MODE;
845 musb_writew(epio, MUSB_TXCSR, csr);
848 /* re-enable interrupt */
849 musb_writew(mbase, MUSB_INTRTXE, int_txe);
855 if (hw_ep->rx_reinit) {
856 musb_rx_reinit(musb, qh, hw_ep);
858 /* init new state: toggle and NYET, maybe DMA later */
859 if (usb_gettoggle(urb->dev, qh->epnum, 0))
860 csr = MUSB_RXCSR_H_WR_DATATOGGLE
861 | MUSB_RXCSR_H_DATATOGGLE;
864 if (qh->type == USB_ENDPOINT_XFER_INT)
865 csr |= MUSB_RXCSR_DISNYET;
868 csr = musb_readw(hw_ep->regs, MUSB_RXCSR);
870 if (csr & (MUSB_RXCSR_RXPKTRDY
872 | MUSB_RXCSR_H_REQPKT))
873 ERR("broken !rx_reinit, ep%d csr %04x\n",
876 /* scrub any stale state, leaving toggle alone */
877 csr &= MUSB_RXCSR_DISNYET;
880 /* kick things off */
882 if ((cppi_ti_dma() || tusb_dma_omap()) && dma_channel) {
883 /* candidate for DMA */
885 dma_channel->actual_len = 0L;
888 /* AUTOREQ is in a DMA register */
889 musb_writew(hw_ep->regs, MUSB_RXCSR, csr);
890 csr = musb_readw(hw_ep->regs,
893 /* unless caller treats short rx transfers as
894 * errors, we dare not queue multiple transfers.
896 dma_ok = dma_controller->channel_program(
897 dma_channel, packet_sz,
898 !(urb->transfer_flags
903 dma_controller->channel_release(
905 hw_ep->rx_channel = NULL;
908 csr |= MUSB_RXCSR_DMAENAB;
912 csr |= MUSB_RXCSR_H_REQPKT;
913 DBG(7, "RXCSR%d := %04x\n", epnum, csr);
914 musb_writew(hw_ep->regs, MUSB_RXCSR, csr);
915 csr = musb_readw(hw_ep->regs, MUSB_RXCSR);
921 * Service the default endpoint (ep0) as host.
922 * Return true until it's time to start the status stage.
924 static bool musb_h_ep0_continue(struct musb *musb, u16 len, struct urb *urb)
927 u8 *fifo_dest = NULL;
929 struct musb_hw_ep *hw_ep = musb->control_ep;
930 struct musb_qh *qh = hw_ep->in_qh;
931 struct usb_ctrlrequest *request;
933 switch (musb->ep0_stage) {
935 fifo_dest = urb->transfer_buffer + urb->actual_length;
936 fifo_count = min_t(size_t, len, urb->transfer_buffer_length -
938 if (fifo_count < len)
939 urb->status = -EOVERFLOW;
941 musb_read_fifo(hw_ep, fifo_count, fifo_dest);
943 urb->actual_length += fifo_count;
944 if (len < qh->maxpacket) {
945 /* always terminate on short read; it's
946 * rarely reported as an error.
948 } else if (urb->actual_length <
949 urb->transfer_buffer_length)
953 request = (struct usb_ctrlrequest *) urb->setup_packet;
955 if (!request->wLength) {
956 DBG(4, "start no-DATA\n");
958 } else if (request->bRequestType & USB_DIR_IN) {
959 DBG(4, "start IN-DATA\n");
960 musb->ep0_stage = MUSB_EP0_IN;
964 DBG(4, "start OUT-DATA\n");
965 musb->ep0_stage = MUSB_EP0_OUT;
970 fifo_count = min_t(size_t, qh->maxpacket,
971 urb->transfer_buffer_length -
974 fifo_dest = (u8 *) (urb->transfer_buffer
975 + urb->actual_length);
976 DBG(3, "Sending %d byte%s to ep0 fifo %p\n",
978 (fifo_count == 1) ? "" : "s",
980 musb_write_fifo(hw_ep, fifo_count, fifo_dest);
982 urb->actual_length += fifo_count;
987 ERR("bogus ep0 stage %d\n", musb->ep0_stage);
995 * Handle default endpoint interrupt as host. Only called in IRQ time
996 * from musb_interrupt().
998 * called with controller irqlocked
1000 irqreturn_t musb_h_ep0_irq(struct musb *musb)
1005 void __iomem *mbase = musb->mregs;
1006 struct musb_hw_ep *hw_ep = musb->control_ep;
1007 void __iomem *epio = hw_ep->regs;
1008 struct musb_qh *qh = hw_ep->in_qh;
1009 bool complete = false;
1010 irqreturn_t retval = IRQ_NONE;
1012 /* ep0 only has one queue, "in" */
1015 musb_ep_select(mbase, 0);
1016 csr = musb_readw(epio, MUSB_CSR0);
1017 len = (csr & MUSB_CSR0_RXPKTRDY)
1018 ? musb_readb(epio, MUSB_COUNT0)
1021 DBG(4, "<== csr0 %04x, qh %p, count %d, urb %p, stage %d\n",
1022 csr, qh, len, urb, musb->ep0_stage);
1024 /* if we just did status stage, we are done */
1025 if (MUSB_EP0_STATUS == musb->ep0_stage) {
1026 retval = IRQ_HANDLED;
1030 /* prepare status */
1031 if (csr & MUSB_CSR0_H_RXSTALL) {
1032 DBG(6, "STALLING ENDPOINT\n");
1035 } else if (csr & MUSB_CSR0_H_ERROR) {
1036 DBG(2, "no response, csr0 %04x\n", csr);
1039 } else if (csr & MUSB_CSR0_H_NAKTIMEOUT) {
1040 DBG(2, "control NAK timeout\n");
1042 /* NOTE: this code path would be a good place to PAUSE a
1043 * control transfer, if another one is queued, so that
1044 * ep0 is more likely to stay busy.
1046 * if (qh->ring.next != &musb->control), then
1047 * we have a candidate... NAKing is *NOT* an error
1049 musb_writew(epio, MUSB_CSR0, 0);
1050 retval = IRQ_HANDLED;
1054 DBG(6, "aborting\n");
1055 retval = IRQ_HANDLED;
1057 urb->status = status;
1060 /* use the proper sequence to abort the transfer */
1061 if (csr & MUSB_CSR0_H_REQPKT) {
1062 csr &= ~MUSB_CSR0_H_REQPKT;
1063 musb_writew(epio, MUSB_CSR0, csr);
1064 csr &= ~MUSB_CSR0_H_NAKTIMEOUT;
1065 musb_writew(epio, MUSB_CSR0, csr);
1067 csr |= MUSB_CSR0_FLUSHFIFO;
1068 musb_writew(epio, MUSB_CSR0, csr);
1069 musb_writew(epio, MUSB_CSR0, csr);
1070 csr &= ~MUSB_CSR0_H_NAKTIMEOUT;
1071 musb_writew(epio, MUSB_CSR0, csr);
1074 musb_writeb(epio, MUSB_NAKLIMIT0, 0);
1077 musb_writew(epio, MUSB_CSR0, 0);
1080 if (unlikely(!urb)) {
1081 /* stop endpoint since we have no place for its data, this
1082 * SHOULD NEVER HAPPEN! */
1083 ERR("no URB for end 0\n");
1085 musb_writew(epio, MUSB_CSR0, MUSB_CSR0_FLUSHFIFO);
1086 musb_writew(epio, MUSB_CSR0, MUSB_CSR0_FLUSHFIFO);
1087 musb_writew(epio, MUSB_CSR0, 0);
1093 /* call common logic and prepare response */
1094 if (musb_h_ep0_continue(musb, len, urb)) {
1095 /* more packets required */
1096 csr = (MUSB_EP0_IN == musb->ep0_stage)
1097 ? MUSB_CSR0_H_REQPKT : MUSB_CSR0_TXPKTRDY;
1099 /* data transfer complete; perform status phase */
1100 if (usb_pipeout(urb->pipe)
1101 || !urb->transfer_buffer_length)
1102 csr = MUSB_CSR0_H_STATUSPKT
1103 | MUSB_CSR0_H_REQPKT;
1105 csr = MUSB_CSR0_H_STATUSPKT
1106 | MUSB_CSR0_TXPKTRDY;
1108 /* flag status stage */
1109 musb->ep0_stage = MUSB_EP0_STATUS;
1111 DBG(5, "ep0 STATUS, csr %04x\n", csr);
1114 musb_writew(epio, MUSB_CSR0, csr);
1115 retval = IRQ_HANDLED;
1117 musb->ep0_stage = MUSB_EP0_IDLE;
1119 /* call completion handler if done */
1121 musb_advance_schedule(musb, urb, hw_ep, 1);
1127 #ifdef CONFIG_USB_INVENTRA_DMA
1129 /* Host side TX (OUT) using Mentor DMA works as follows:
1131 - if queue was empty, Program Endpoint
1132 - ... which starts DMA to fifo in mode 1 or 0
1134 DMA Isr (transfer complete) -> TxAvail()
1135 - Stop DMA (~DmaEnab) (<--- Alert ... currently happens
1136 only in musb_cleanup_urb)
1137 - TxPktRdy has to be set in mode 0 or for
1138 short packets in mode 1.
1143 /* Service a Tx-Available or dma completion irq for the endpoint */
1144 void musb_host_tx(struct musb *musb, u8 epnum)
1152 struct musb_hw_ep *hw_ep = musb->endpoints + epnum;
1153 void __iomem *epio = hw_ep->regs;
1154 struct musb_qh *qh = hw_ep->is_shared_fifo ? hw_ep->in_qh
1157 void __iomem *mbase = musb->mregs;
1158 struct dma_channel *dma;
1162 musb_ep_select(mbase, epnum);
1163 tx_csr = musb_readw(epio, MUSB_TXCSR);
1165 /* with CPPI, DMA sometimes triggers "extra" irqs */
1167 DBG(4, "extra TX%d ready, csr %04x\n", epnum, tx_csr);
1172 dma = is_dma_capable() ? hw_ep->tx_channel : NULL;
1173 DBG(4, "OUT/TX%d end, csr %04x%s\n", epnum, tx_csr,
1174 dma ? ", dma" : "");
1176 /* check for errors */
1177 if (tx_csr & MUSB_TXCSR_H_RXSTALL) {
1178 /* dma was disabled, fifo flushed */
1179 DBG(3, "TX end %d stall\n", epnum);
1181 /* stall; record URB status */
1184 } else if (tx_csr & MUSB_TXCSR_H_ERROR) {
1185 /* (NON-ISO) dma was disabled, fifo flushed */
1186 DBG(3, "TX 3strikes on ep=%d\n", epnum);
1188 status = -ETIMEDOUT;
1190 } else if (tx_csr & MUSB_TXCSR_H_NAKTIMEOUT) {
1191 DBG(6, "TX end=%d device not responding\n", epnum);
1193 /* NOTE: this code path would be a good place to PAUSE a
1194 * transfer, if there's some other (nonperiodic) tx urb
1195 * that could use this fifo. (dma complicates it...)
1197 * if (bulk && qh->ring.next != &hw_ep->out_list), 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 ep->in_list and add the current qh at tail
1358 * to avoid endpoint starvation.
1360 static void musb_bulk_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_DATAERROR;
1375 musb_writew(epio, MUSB_RXCSR, rx_csr);
1377 cur_qh = first_qh(&ep->in_list);
1379 urb = next_urb(cur_qh);
1380 if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) {
1381 dma->status = MUSB_DMA_STATUS_CORE_ABORT;
1382 musb->dma_controller->channel_abort(dma);
1383 urb->actual_length += dma->actual_len;
1384 dma->actual_len = 0L;
1386 musb_save_toggle(ep, 1, urb);
1388 /* delete cur_qh and add to tail to ep->in_list */
1389 list_del(&cur_qh->ring);
1390 list_add_tail(&cur_qh->ring, &ep->in_list);
1392 /* get the next qh from ep->in_list */
1393 next_qh = first_qh(&ep->in_list);
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 /* NOTE this code path would be a good place to PAUSE a
1465 * transfer, if there's some other (nonperiodic) rx urb
1466 * that could use this fifo. (dma complicates it...)
1468 * if (bulk && qh->ring.next != &hw_ep->in_list), then
1469 * we have a candidate... NAKing is *NOT* an error
1471 DBG(6, "RX end %d NAK timeout\n", epnum);
1472 if (usb_pipebulk(urb->pipe) && qh->mux == 1 &&
1473 !list_is_singular(&hw_ep->in_list)) {
1474 musb_bulk_nak_timeout(musb, hw_ep);
1477 musb_ep_select(mbase, epnum);
1478 rx_csr &= ~MUSB_RXCSR_DATAERROR;
1479 musb_writew(epio, MUSB_RXCSR, rx_csr);
1483 DBG(4, "RX end %d ISO data error\n", epnum);
1484 /* packet error reported later */
1487 } else if (rx_csr & MUSB_RXCSR_INCOMPRX) {
1488 DBG(3, "end %d Highbandwidth incomplete ISO packet received\n",
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 if (!musb_inventra_dma() && (rx_csr & MUSB_RXCSR_H_REQPKT)) {
1521 /* REVISIT this happened for a while on some short reads...
1522 * the cleanup still needs investigation... looks bad...
1523 * and also duplicates dma cleanup code above ... plus,
1524 * shouldn't this be the "half full" double buffer case?
1526 if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) {
1527 dma->status = MUSB_DMA_STATUS_CORE_ABORT;
1528 (void) musb->dma_controller->channel_abort(dma);
1529 xfer_len = dma->actual_len;
1533 DBG(2, "RXCSR%d %04x, reqpkt, len %zu%s\n", epnum, rx_csr,
1534 xfer_len, dma ? ", dma" : "");
1535 rx_csr &= ~MUSB_RXCSR_H_REQPKT;
1537 musb_ep_select(mbase, epnum);
1538 musb_writew(epio, MUSB_RXCSR,
1539 MUSB_RXCSR_H_WZC_BITS | rx_csr);
1542 if (dma && (rx_csr & MUSB_RXCSR_DMAENAB)) {
1543 xfer_len = dma->actual_len;
1545 val &= ~(MUSB_RXCSR_DMAENAB
1546 | MUSB_RXCSR_H_AUTOREQ
1547 | MUSB_RXCSR_AUTOCLEAR
1548 | MUSB_RXCSR_RXPKTRDY);
1549 musb_writew(hw_ep->regs, MUSB_RXCSR, val);
1551 #ifdef CONFIG_USB_INVENTRA_DMA
1552 if (usb_pipeisoc(pipe)) {
1553 struct usb_iso_packet_descriptor *d;
1555 d = urb->iso_frame_desc + qh->iso_idx;
1556 d->actual_length = xfer_len;
1558 /* even if there was an error, we did the dma
1559 * for iso_frame_desc->length
1561 if (d->status != EILSEQ && d->status != -EOVERFLOW)
1564 if (++qh->iso_idx >= urb->number_of_packets)
1570 /* done if urb buffer is full or short packet is recd */
1571 done = (urb->actual_length + xfer_len >=
1572 urb->transfer_buffer_length
1573 || dma->actual_len < qh->maxpacket);
1576 /* send IN token for next packet, without AUTOREQ */
1578 val |= MUSB_RXCSR_H_REQPKT;
1579 musb_writew(epio, MUSB_RXCSR,
1580 MUSB_RXCSR_H_WZC_BITS | val);
1583 DBG(4, "ep %d dma %s, rxcsr %04x, rxcount %d\n", epnum,
1584 done ? "off" : "reset",
1585 musb_readw(epio, MUSB_RXCSR),
1586 musb_readw(epio, MUSB_RXCOUNT));
1590 } else if (urb->status == -EINPROGRESS) {
1591 /* if no errors, be sure a packet is ready for unloading */
1592 if (unlikely(!(rx_csr & MUSB_RXCSR_RXPKTRDY))) {
1594 ERR("Rx interrupt with no errors or packet!\n");
1596 /* FIXME this is another "SHOULD NEVER HAPPEN" */
1599 /* do the proper sequence to abort the transfer */
1600 musb_ep_select(mbase, epnum);
1601 val &= ~MUSB_RXCSR_H_REQPKT;
1602 musb_writew(epio, MUSB_RXCSR, val);
1606 /* we are expecting IN packets */
1607 if (musb_inventra_dma() && dma) {
1608 struct dma_controller *c;
1613 rx_count = musb_readw(epio, MUSB_RXCOUNT);
1615 DBG(2, "RX%d count %d, buffer 0x%x len %d/%d\n",
1618 + urb->actual_length,
1620 urb->transfer_buffer_length);
1622 c = musb->dma_controller;
1624 if (usb_pipeisoc(pipe)) {
1626 struct usb_iso_packet_descriptor *d;
1628 d = urb->iso_frame_desc + qh->iso_idx;
1634 if (rx_count > d->length) {
1636 status = -EOVERFLOW;
1639 DBG(2, "** OVERFLOW %d into %d\n",\
1640 rx_count, d->length);
1646 buf = urb->transfer_dma + d->offset;
1649 buf = urb->transfer_dma +
1653 dma->desired_mode = 0;
1655 /* because of the issue below, mode 1 will
1656 * only rarely behave with correct semantics.
1658 if ((urb->transfer_flags &
1660 && (urb->transfer_buffer_length -
1663 dma->desired_mode = 1;
1664 if (rx_count < hw_ep->max_packet_sz_rx) {
1666 dma->bDesiredMode = 0;
1668 length = urb->transfer_buffer_length;
1672 /* Disadvantage of using mode 1:
1673 * It's basically usable only for mass storage class; essentially all
1674 * other protocols also terminate transfers on short packets.
1677 * An extra IN token is sent at the end of the transfer (due to AUTOREQ)
1678 * If you try to use mode 1 for (transfer_buffer_length - 512), and try
1679 * to use the extra IN token to grab the last packet using mode 0, then
1680 * the problem is that you cannot be sure when the device will send the
1681 * last packet and RxPktRdy set. Sometimes the packet is recd too soon
1682 * such that it gets lost when RxCSR is re-set at the end of the mode 1
1683 * transfer, while sometimes it is recd just a little late so that if you
1684 * try to configure for mode 0 soon after the mode 1 transfer is
1685 * completed, you will find rxcount 0. Okay, so you might think why not
1686 * wait for an interrupt when the pkt is recd. Well, you won't get any!
1689 val = musb_readw(epio, MUSB_RXCSR);
1690 val &= ~MUSB_RXCSR_H_REQPKT;
1692 if (dma->desired_mode == 0)
1693 val &= ~MUSB_RXCSR_H_AUTOREQ;
1695 val |= MUSB_RXCSR_H_AUTOREQ;
1697 if (qh->maxpacket & ~0x7ff)
1698 /* Autoclear doesn't work in high bandwidth iso */
1699 val |= MUSB_RXCSR_DMAENAB;
1701 val |= MUSB_RXCSR_AUTOCLEAR
1702 | MUSB_RXCSR_DMAENAB;
1704 musb_writew(epio, MUSB_RXCSR,
1705 MUSB_RXCSR_H_WZC_BITS | val);
1707 /* REVISIT if when actual_length != 0,
1708 * transfer_buffer_length needs to be
1711 ret = c->channel_program(
1713 dma->desired_mode, buf, length);
1716 c->channel_release(dma);
1717 hw_ep->rx_channel = NULL;
1719 /* REVISIT reset CSR */
1724 done = musb_host_packet_rx(musb, urb,
1726 DBG(6, "read %spacket\n", done ? "last " : "");
1731 urb->actual_length += xfer_len;
1732 qh->offset += xfer_len;
1734 if (urb->status == -EINPROGRESS)
1735 urb->status = status;
1736 musb_advance_schedule(musb, urb, hw_ep, USB_DIR_IN);
1740 /* schedule nodes correspond to peripheral endpoints, like an OHCI QH.
1741 * the software schedule associates multiple such nodes with a given
1742 * host side hardware endpoint + direction; scheduling may activate
1743 * that hardware endpoint.
1745 static int musb_schedule(
1752 int best_end, epnum;
1753 struct musb_hw_ep *hw_ep = NULL;
1754 struct list_head *head = NULL;
1757 /* use fixed hardware for control and bulk */
1758 if (qh->type == USB_ENDPOINT_XFER_CONTROL) {
1759 hw_ep = musb->control_ep;
1760 head = &hw_ep->in_list;
1764 /* else, periodic transfers get muxed to other endpoints */
1766 /* FIXME this doesn't consider direction, so it can only
1767 * work for one half of the endpoint hardware, and assumes
1768 * the previous cases handled all non-shared endpoints...
1771 /* we know this qh hasn't been scheduled, so all we need to do
1772 * is choose which hardware endpoint to put it on ...
1774 * REVISIT what we really want here is a regular schedule tree
1775 * like e.g. OHCI uses, but for now musb->periodic is just an
1776 * array of the _single_ logical endpoint associated with a
1777 * given physical one (identity mapping logical->physical).
1779 * that simplistic approach makes TT scheduling a lot simpler;
1780 * there is none, and thus none of its complexity...
1785 if (qh->maxpacket & (1 << 11))
1786 maxpacket = 2 * (qh->maxpacket & 0x7ff);
1787 else if (qh->maxpacket & (1 << 12))
1788 maxpacket = 3 * (qh->maxpacket & 0x7ff);
1790 maxpacket = (qh->maxpacket & 0x7ff);
1792 for (epnum = 1; epnum < musb->nr_endpoints; epnum++) {
1795 if (musb->periodic[epnum])
1797 hw_ep = &musb->endpoints[epnum];
1798 if (hw_ep == musb->bulk_ep)
1802 diff = hw_ep->max_packet_sz_rx - maxpacket;
1804 diff = hw_ep->max_packet_sz_tx - maxpacket;
1806 if (diff >= 0 && best_diff > diff) {
1811 /* use bulk reserved ep1 if no other ep is free */
1812 if (best_end < 0 && qh->type == USB_ENDPOINT_XFER_BULK) {
1813 hw_ep = musb->bulk_ep;
1815 head = &hw_ep->in_list;
1817 head = &hw_ep->out_list;
1818 /* Enable bulk NAK time out scheme when bulk requests are
1819 * multiplxed. This scheme doesn't work in high speed to full
1820 * speed scenario as NAK interrupts are not coming from a
1821 * full speed device connected to a high speed device.
1822 * NAK timeout interval is 8 (128 uframe or 16ms) for HS and
1823 * 4 (8 frame or 8ms) for FS device.
1825 if (is_in && qh->dev)
1827 (USB_SPEED_HIGH == qh->dev->speed) ? 8 : 4;
1829 } else if (best_end < 0) {
1835 hw_ep = musb->endpoints + best_end;
1836 musb->periodic[best_end] = qh;
1837 DBG(4, "qh %p periodic slot %d\n", qh, best_end);
1840 idle = list_empty(head);
1841 list_add_tail(&qh->ring, head);
1845 qh->hep->hcpriv = qh;
1847 musb_start_urb(musb, is_in, qh);
1851 static int musb_urb_enqueue(
1852 struct usb_hcd *hcd,
1856 unsigned long flags;
1857 struct musb *musb = hcd_to_musb(hcd);
1858 struct usb_host_endpoint *hep = urb->ep;
1860 struct usb_endpoint_descriptor *epd = &hep->desc;
1865 /* host role must be active */
1866 if (!is_host_active(musb) || !musb->is_active) {
1867 printk(KERN_ERR "musb is_host_active %d is_active %d\n",is_host_active(musb),musb->is_active);
1871 spin_lock_irqsave(&musb->lock, flags);
1872 ret = usb_hcd_link_urb_to_ep(hcd, urb);
1873 qh = ret ? NULL : hep->hcpriv;
1876 spin_unlock_irqrestore(&musb->lock, flags);
1878 /* DMA mapping was already done, if needed, and this urb is on
1879 * hep->urb_list now ... so we're done, unless hep wasn't yet
1880 * scheduled onto a live qh.
1882 * REVISIT best to keep urb->hcpriv valid until the endpoint gets
1883 * disabled, testing for empty qh->ring and avoiding qh setup costs
1884 * except for the first urb queued after a config change.
1889 /* Allocate and initialize qh, minimizing the work done each time
1890 * hw_ep gets reprogrammed, or with irqs blocked. Then schedule it.
1892 * REVISIT consider a dedicated qh kmem_cache, so it's harder
1893 * for bugs in other kernel code to break this driver...
1895 qh = kzalloc(sizeof *qh, mem_flags);
1897 spin_lock_irqsave(&musb->lock, flags);
1898 usb_hcd_unlink_urb_from_ep(hcd, urb);
1899 spin_unlock_irqrestore(&musb->lock, flags);
1905 INIT_LIST_HEAD(&qh->ring);
1908 qh->maxpacket = le16_to_cpu(epd->wMaxPacketSize);
1910 qh->epnum = usb_endpoint_num(epd);
1911 qh->type = usb_endpoint_type(epd);
1913 /* NOTE: urb->dev->devnum is wrong during SET_ADDRESS */
1914 qh->addr_reg = (u8) usb_pipedevice(urb->pipe);
1916 /* precompute rxtype/txtype/type0 register */
1917 type_reg = (qh->type << 4) | qh->epnum;
1918 switch (urb->dev->speed) {
1922 case USB_SPEED_FULL:
1928 qh->type_reg = type_reg;
1930 /* Precompute RXINTERVAL/TXINTERVAL register */
1932 case USB_ENDPOINT_XFER_INT:
1934 * Full/low speeds use the linear encoding,
1935 * high speed uses the logarithmic encoding.
1937 if (urb->dev->speed <= USB_SPEED_FULL) {
1938 interval = max_t(u8, epd->bInterval, 1);
1942 case USB_ENDPOINT_XFER_ISOC:
1943 /* ISO always uses logarithmic encoding */
1944 interval = min_t(u8, epd->bInterval, 16);
1947 /* REVISIT we actually want to use NAK limits, hinting to the
1948 * transfer scheduling logic to try some other qh, e.g. try
1951 * interval = (USB_SPEED_HIGH == urb->dev->speed) ? 16 : 2;
1953 * The downside of disabling this is that transfer scheduling
1954 * gets VERY unfair for nonperiodic transfers; a misbehaving
1955 * peripheral could make that hurt. Or for reads, one that's
1956 * perfectly normal: network and other drivers keep reads
1957 * posted at all times, having one pending for a week should
1958 * be perfectly safe.
1960 * The upside of disabling it is avoidng transfer scheduling
1961 * code to put this aside for while.
1965 qh->intv_reg = interval;
1967 /* precompute addressing for external hub/tt ports */
1968 if (musb->is_multipoint) {
1969 struct usb_device *parent = urb->dev->parent;
1971 if (parent != hcd->self.root_hub) {
1972 qh->h_addr_reg = (u8) parent->devnum;
1974 /* set up tt info if needed */
1976 qh->h_port_reg = (u8) urb->dev->ttport;
1977 if (urb->dev->tt->hub)
1979 (u8) urb->dev->tt->hub->devnum;
1980 if (urb->dev->tt->multi)
1981 qh->h_addr_reg |= 0x80;
1986 /* invariant: hep->hcpriv is null OR the qh that's already scheduled.
1987 * until we get real dma queues (with an entry for each urb/buffer),
1988 * we only have work to do in the former case.
1990 spin_lock_irqsave(&musb->lock, flags);
1992 /* some concurrent activity submitted another urb to hep...
1993 * odd, rare, error prone, but legal.
1998 ret = musb_schedule(musb, qh,
1999 epd->bEndpointAddress & USB_ENDPOINT_DIR_MASK);
2003 /* FIXME set urb->start_frame for iso/intr, it's tested in
2004 * musb_start_urb(), but otherwise only konicawc cares ...
2007 spin_unlock_irqrestore(&musb->lock, flags);
2010 spin_lock_irqsave(&musb->lock, flags);
2011 usb_hcd_unlink_urb_from_ep(hcd, urb);
2012 spin_unlock_irqrestore(&musb->lock, flags);
2020 * abort a transfer that's at the head of a hardware queue.
2021 * called with controller locked, irqs blocked
2022 * that hardware queue advances to the next transfer, unless prevented
2024 static int musb_cleanup_urb(struct urb *urb, struct musb_qh *qh, int is_in)
2026 struct musb_hw_ep *ep = qh->hw_ep;
2027 void __iomem *epio = ep->regs;
2028 unsigned hw_end = ep->epnum;
2029 void __iomem *regs = ep->musb->mregs;
2033 musb_ep_select(regs, hw_end);
2035 if (is_dma_capable()) {
2036 struct dma_channel *dma;
2038 dma = is_in ? ep->rx_channel : ep->tx_channel;
2040 status = ep->musb->dma_controller->channel_abort(dma);
2042 "abort %cX%d DMA for urb %p --> %d\n",
2043 is_in ? 'R' : 'T', ep->epnum,
2045 urb->actual_length += dma->actual_len;
2049 /* turn off DMA requests, discard state, stop polling ... */
2051 /* giveback saves bulk toggle */
2052 csr = musb_h_flush_rxfifo(ep, 0);
2054 /* REVISIT we still get an irq; should likely clear the
2055 * endpoint's irq status here to avoid bogus irqs.
2056 * clearing that status is platform-specific...
2059 musb_h_tx_flush_fifo(ep);
2060 csr = musb_readw(epio, MUSB_TXCSR);
2061 csr &= ~(MUSB_TXCSR_AUTOSET
2062 | MUSB_TXCSR_DMAENAB
2063 | MUSB_TXCSR_H_RXSTALL
2064 | MUSB_TXCSR_H_NAKTIMEOUT
2065 | MUSB_TXCSR_H_ERROR
2066 | MUSB_TXCSR_TXPKTRDY);
2067 musb_writew(epio, MUSB_TXCSR, csr);
2068 /* REVISIT may need to clear FLUSHFIFO ... */
2069 musb_writew(epio, MUSB_TXCSR, csr);
2070 /* flush cpu writebuffer */
2071 csr = musb_readw(epio, MUSB_TXCSR);
2074 musb_advance_schedule(ep->musb, urb, ep, is_in);
2078 static int musb_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
2080 struct musb *musb = hcd_to_musb(hcd);
2082 struct list_head *sched;
2083 unsigned long flags;
2086 DBG(4, "urb=%p, dev%d ep%d%s\n", urb,
2087 usb_pipedevice(urb->pipe),
2088 usb_pipeendpoint(urb->pipe),
2089 usb_pipein(urb->pipe) ? "in" : "out");
2091 spin_lock_irqsave(&musb->lock, flags);
2092 ret = usb_hcd_check_unlink_urb(hcd, urb, status);
2100 /* Any URB not actively programmed into endpoint hardware can be
2101 * immediately given back; that's any URB not at the head of an
2102 * endpoint queue, unless someday we get real DMA queues. And even
2103 * if it's at the head, it might not be known to the hardware...
2105 * Otherwise abort current transfer, pending dma, etc.; urb->status
2106 * has already been updated. This is a synchronous abort; it'd be
2107 * OK to hold off until after some IRQ, though.
2109 if (!qh->is_ready || urb->urb_list.prev != &qh->hep->urb_list)
2113 case USB_ENDPOINT_XFER_CONTROL:
2114 sched = &musb->control_ep->in_list;
2116 case USB_ENDPOINT_XFER_BULK:
2118 if (usb_pipein(urb->pipe))
2119 sched = &musb->bulk_ep->in_list;
2121 sched = &musb->bulk_ep->out_list;
2125 /* REVISIT when we get a schedule tree, periodic
2126 * transfers won't always be at the head of a
2127 * singleton queue...
2134 /* NOTE: qh is invalid unless !list_empty(&hep->urb_list) */
2135 if (ret < 0 || (sched && qh != first_qh(sched))) {
2136 int ready = qh->is_ready;
2140 __musb_giveback(musb, urb, 0);
2141 qh->is_ready = ready;
2143 /* If nothing else (usually musb_giveback) is using it
2144 * and its URB list has emptied, recycle this qh.
2146 if (ready && list_empty(&qh->hep->urb_list)) {
2147 qh->hep->hcpriv = NULL;
2148 list_del(&qh->ring);
2152 ret = musb_cleanup_urb(urb, qh, urb->pipe & USB_DIR_IN);
2154 spin_unlock_irqrestore(&musb->lock, flags);
2158 /* disable an endpoint */
2160 musb_h_disable(struct usb_hcd *hcd, struct usb_host_endpoint *hep)
2162 u8 epnum = hep->desc.bEndpointAddress;
2163 unsigned long flags;
2164 struct musb *musb = hcd_to_musb(hcd);
2165 u8 is_in = epnum & USB_DIR_IN;
2168 struct list_head *sched;
2170 spin_lock_irqsave(&musb->lock, flags);
2177 case USB_ENDPOINT_XFER_CONTROL:
2178 sched = &musb->control_ep->in_list;
2180 case USB_ENDPOINT_XFER_BULK:
2183 sched = &musb->bulk_ep->in_list;
2185 sched = &musb->bulk_ep->out_list;
2189 /* REVISIT when we get a schedule tree, periodic transfers
2190 * won't always be at the head of a singleton queue...
2196 /* NOTE: qh is invalid unless !list_empty(&hep->urb_list) */
2198 /* kick first urb off the hardware, if needed */
2200 if (!sched || qh == first_qh(sched)) {
2203 /* make software (then hardware) stop ASAP */
2205 urb->status = -ESHUTDOWN;
2208 musb_cleanup_urb(urb, qh, urb->pipe & USB_DIR_IN);
2210 /* Then nuke all the others ... and advance the
2211 * queue on hw_ep (e.g. bulk ring) when we're done.
2213 while (!list_empty(&hep->urb_list)) {
2215 urb->status = -ESHUTDOWN;
2216 musb_advance_schedule(musb, urb, qh->hw_ep, is_in);
2219 /* Just empty the queue; the hardware is busy with
2220 * other transfers, and since !qh->is_ready nothing
2221 * will activate any of these as it advances.
2223 while (!list_empty(&hep->urb_list))
2224 __musb_giveback(musb, next_urb(qh), -ESHUTDOWN);
2227 list_del(&qh->ring);
2231 spin_unlock_irqrestore(&musb->lock, flags);
2234 static int musb_h_get_frame_number(struct usb_hcd *hcd)
2236 struct musb *musb = hcd_to_musb(hcd);
2238 return musb_readw(musb->mregs, MUSB_FRAME);
2241 static int musb_h_start(struct usb_hcd *hcd)
2243 struct musb *musb = hcd_to_musb(hcd);
2245 /* NOTE: musb_start() is called when the hub driver turns
2246 * on port power, or when (OTG) peripheral starts.
2248 hcd->state = HC_STATE_RUNNING;
2249 musb->port1_status = 0;
2253 static void musb_h_stop(struct usb_hcd *hcd)
2255 musb_stop(hcd_to_musb(hcd));
2256 hcd->state = HC_STATE_HALT;
2259 static int musb_bus_suspend(struct usb_hcd *hcd)
2261 struct musb *musb = hcd_to_musb(hcd);
2263 if (musb->xceiv->state == OTG_STATE_A_SUSPEND)
2266 if (is_host_active(musb) && musb->is_active) {
2267 WARNING("trying to suspend as %s is_active=%i\n",
2268 otg_state_string(musb), musb->is_active);
2274 static int musb_bus_resume(struct usb_hcd *hcd)
2276 /* resuming child port does the work */
2281 const struct hc_driver musb_hc_driver = {
2282 .description = "musb-hcd",
2283 .product_desc = "MUSB HDRC host driver",
2284 .hcd_priv_size = sizeof(struct musb),
2285 .flags = HCD_USB2 | HCD_MEMORY,
2287 /* not using irq handler or reset hooks from usbcore, since
2288 * those must be shared with peripheral code for OTG configs
2291 .start = musb_h_start,
2292 .stop = musb_h_stop,
2294 .get_frame_number = musb_h_get_frame_number,
2296 .urb_enqueue = musb_urb_enqueue,
2297 .urb_dequeue = musb_urb_dequeue,
2298 .endpoint_disable = musb_h_disable,
2300 .hub_status_data = musb_hub_status_data,
2301 .hub_control = musb_hub_control,
2302 .bus_suspend = musb_bus_suspend,
2303 .bus_resume = musb_bus_resume,
2304 /* .hub_irq_enable = NULL, */