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
7 * Copyright (C) 2008-2009 MontaVista Software, Inc. <source@mvista.com>
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * version 2 as published by the Free Software Foundation.
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
23 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
24 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
25 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
26 * NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
29 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
30 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 #include <linux/module.h>
37 #include <linux/kernel.h>
38 #include <linux/delay.h>
39 #include <linux/sched.h>
40 #include <linux/slab.h>
41 #include <linux/errno.h>
42 #include <linux/list.h>
43 #include <linux/dma-mapping.h>
45 #include "musb_core.h"
46 #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.
97 struct musb *hcd_to_musb(struct usb_hcd *hcd)
99 return *(struct musb **) hcd->hcd_priv;
103 static void musb_ep_program(struct musb *musb, u8 epnum,
104 struct urb *urb, int is_out,
105 u8 *buf, u32 offset, u32 len);
108 * Clear TX fifo. Needed to avoid BABBLE errors.
110 static void musb_h_tx_flush_fifo(struct musb_hw_ep *ep)
112 struct musb *musb = ep->musb;
113 void __iomem *epio = ep->regs;
117 csr = musb_readw(epio, MUSB_TXCSR);
118 while (csr & MUSB_TXCSR_FIFONOTEMPTY) {
119 csr |= MUSB_TXCSR_FLUSHFIFO | MUSB_TXCSR_TXPKTRDY;
120 musb_writew(epio, MUSB_TXCSR, csr);
121 csr = musb_readw(epio, MUSB_TXCSR);
124 * FIXME: sometimes the tx fifo flush failed, it has been
125 * observed during device disconnect on AM335x.
127 * To reproduce the issue, ensure tx urb(s) are queued when
128 * unplug the usb device which is connected to AM335x usb
131 * I found using a usb-ethernet device and running iperf
132 * (client on AM335x) has very high chance to trigger it.
134 * Better to turn on dev_dbg() in musb_cleanup_urb() with
135 * CPPI enabled to see the issue when aborting the tx channel.
137 if (dev_WARN_ONCE(musb->controller, retries-- < 1,
138 "Could not flush host TX%d fifo: csr: %04x\n",
144 static void musb_h_ep0_flush_fifo(struct musb_hw_ep *ep)
146 void __iomem *epio = ep->regs;
150 /* scrub any data left in the fifo */
152 csr = musb_readw(epio, MUSB_TXCSR);
153 if (!(csr & (MUSB_CSR0_TXPKTRDY | MUSB_CSR0_RXPKTRDY)))
155 musb_writew(epio, MUSB_TXCSR, MUSB_CSR0_FLUSHFIFO);
156 csr = musb_readw(epio, MUSB_TXCSR);
160 WARN(!retries, "Could not flush host TX%d fifo: csr: %04x\n",
163 /* and reset for the next transfer */
164 musb_writew(epio, MUSB_TXCSR, 0);
168 * Start transmit. Caller is responsible for locking shared resources.
169 * musb must be locked.
171 static inline void musb_h_tx_start(struct musb_hw_ep *ep)
175 /* NOTE: no locks here; caller should lock and select EP */
177 txcsr = musb_readw(ep->regs, MUSB_TXCSR);
178 txcsr |= MUSB_TXCSR_TXPKTRDY | MUSB_TXCSR_H_WZC_BITS;
179 musb_writew(ep->regs, MUSB_TXCSR, txcsr);
181 txcsr = MUSB_CSR0_H_SETUPPKT | MUSB_CSR0_TXPKTRDY;
182 musb_writew(ep->regs, MUSB_CSR0, txcsr);
187 static inline void musb_h_tx_dma_start(struct musb_hw_ep *ep)
191 /* NOTE: no locks here; caller should lock and select EP */
192 txcsr = musb_readw(ep->regs, MUSB_TXCSR);
193 txcsr |= MUSB_TXCSR_DMAENAB | MUSB_TXCSR_H_WZC_BITS;
194 if (is_cppi_enabled(ep->musb))
195 txcsr |= MUSB_TXCSR_DMAMODE;
196 musb_writew(ep->regs, MUSB_TXCSR, txcsr);
199 static void musb_ep_set_qh(struct musb_hw_ep *ep, int is_in, struct musb_qh *qh)
201 if (is_in != 0 || ep->is_shared_fifo)
203 if (is_in == 0 || ep->is_shared_fifo)
207 static struct musb_qh *musb_ep_get_qh(struct musb_hw_ep *ep, int is_in)
209 return is_in ? ep->in_qh : ep->out_qh;
213 * Start the URB at the front of an endpoint's queue
214 * end must be claimed from the caller.
216 * Context: controller locked, irqs blocked
219 musb_start_urb(struct musb *musb, int is_in, struct musb_qh *qh)
223 void __iomem *mbase = musb->mregs;
224 struct urb *urb = next_urb(qh);
225 void *buf = urb->transfer_buffer;
227 struct musb_hw_ep *hw_ep = qh->hw_ep;
228 unsigned pipe = urb->pipe;
229 u8 address = usb_pipedevice(pipe);
230 int epnum = hw_ep->epnum;
232 /* initialize software qh state */
236 /* gather right source of data */
238 case USB_ENDPOINT_XFER_CONTROL:
239 /* control transfers always start with SETUP */
241 musb->ep0_stage = MUSB_EP0_START;
242 buf = urb->setup_packet;
245 case USB_ENDPOINT_XFER_ISOC:
248 offset = urb->iso_frame_desc[0].offset;
249 len = urb->iso_frame_desc[0].length;
251 default: /* bulk, interrupt */
252 /* actual_length may be nonzero on retry paths */
253 buf = urb->transfer_buffer + urb->actual_length;
254 len = urb->transfer_buffer_length - urb->actual_length;
257 dev_dbg(musb->controller, "qh %p urb %p dev%d ep%d%s%s, hw_ep %d, %p/%d\n",
258 qh, urb, address, qh->epnum,
259 is_in ? "in" : "out",
260 ({char *s; switch (qh->type) {
261 case USB_ENDPOINT_XFER_CONTROL: s = ""; break;
262 case USB_ENDPOINT_XFER_BULK: s = "-bulk"; break;
263 case USB_ENDPOINT_XFER_ISOC: s = "-iso"; break;
264 default: s = "-intr"; break;
266 epnum, buf + offset, len);
268 /* Configure endpoint */
269 musb_ep_set_qh(hw_ep, is_in, qh);
270 musb_ep_program(musb, epnum, urb, !is_in, buf, offset, len);
272 /* transmit may have more work: start it when it is time */
276 /* determine if the time is right for a periodic transfer */
278 case USB_ENDPOINT_XFER_ISOC:
279 case USB_ENDPOINT_XFER_INT:
280 dev_dbg(musb->controller, "check whether there's still time for periodic Tx\n");
281 frame = musb_readw(mbase, MUSB_FRAME);
282 /* FIXME this doesn't implement that scheduling policy ...
283 * or handle framecounter wrapping
285 if (1) { /* Always assume URB_ISO_ASAP */
286 /* REVISIT the SOF irq handler shouldn't duplicate
287 * this code; and we don't init urb->start_frame...
292 qh->frame = urb->start_frame;
293 /* enable SOF interrupt so we can count down */
294 dev_dbg(musb->controller, "SOF for %d\n", epnum);
295 #if 1 /* ifndef CONFIG_ARCH_DAVINCI */
296 musb_writeb(mbase, MUSB_INTRUSBE, 0xff);
302 dev_dbg(musb->controller, "Start TX%d %s\n", epnum,
303 hw_ep->tx_channel ? "dma" : "pio");
305 if (!hw_ep->tx_channel)
306 musb_h_tx_start(hw_ep);
307 else if (is_cppi_enabled(musb) || tusb_dma_omap(musb))
308 musb_h_tx_dma_start(hw_ep);
312 /* Context: caller owns controller lock, IRQs are blocked */
313 static void musb_giveback(struct musb *musb, struct urb *urb, int status)
314 __releases(musb->lock)
315 __acquires(musb->lock)
317 dev_dbg(musb->controller,
318 "complete %p %pF (%d), dev%d ep%d%s, %d/%d\n",
319 urb, urb->complete, status,
320 usb_pipedevice(urb->pipe),
321 usb_pipeendpoint(urb->pipe),
322 usb_pipein(urb->pipe) ? "in" : "out",
323 urb->actual_length, urb->transfer_buffer_length
326 usb_hcd_unlink_urb_from_ep(musb->hcd, urb);
327 spin_unlock(&musb->lock);
328 usb_hcd_giveback_urb(musb->hcd, urb, status);
329 spin_lock(&musb->lock);
332 /* For bulk/interrupt endpoints only */
333 static inline void musb_save_toggle(struct musb_qh *qh, int is_in,
336 void __iomem *epio = qh->hw_ep->regs;
340 * FIXME: the current Mentor DMA code seems to have
341 * problems getting toggle correct.
345 csr = musb_readw(epio, MUSB_RXCSR) & MUSB_RXCSR_H_DATATOGGLE;
347 csr = musb_readw(epio, MUSB_TXCSR) & MUSB_TXCSR_H_DATATOGGLE;
349 usb_settoggle(urb->dev, qh->epnum, !is_in, csr ? 1 : 0);
353 * Advance this hardware endpoint's queue, completing the specified URB and
354 * advancing to either the next URB queued to that qh, or else invalidating
355 * that qh and advancing to the next qh scheduled after the current one.
357 * Context: caller owns controller lock, IRQs are blocked
359 static void musb_advance_schedule(struct musb *musb, struct urb *urb,
360 struct musb_hw_ep *hw_ep, int is_in)
362 struct musb_qh *qh = musb_ep_get_qh(hw_ep, is_in);
363 struct musb_hw_ep *ep = qh->hw_ep;
364 int ready = qh->is_ready;
367 status = (urb->status == -EINPROGRESS) ? 0 : urb->status;
369 /* save toggle eagerly, for paranoia */
371 case USB_ENDPOINT_XFER_BULK:
372 case USB_ENDPOINT_XFER_INT:
373 musb_save_toggle(qh, is_in, urb);
375 case USB_ENDPOINT_XFER_ISOC:
376 if (status == 0 && urb->error_count)
382 musb_giveback(musb, urb, status);
383 qh->is_ready = ready;
385 /* reclaim resources (and bandwidth) ASAP; deschedule it, and
386 * invalidate qh as soon as list_empty(&hep->urb_list)
388 if (list_empty(&qh->hep->urb_list)) {
389 struct list_head *head;
390 struct dma_controller *dma = musb->dma_controller;
394 if (ep->rx_channel) {
395 dma->channel_release(ep->rx_channel);
396 ep->rx_channel = NULL;
400 if (ep->tx_channel) {
401 dma->channel_release(ep->tx_channel);
402 ep->tx_channel = NULL;
406 /* Clobber old pointers to this qh */
407 musb_ep_set_qh(ep, is_in, NULL);
408 qh->hep->hcpriv = NULL;
412 case USB_ENDPOINT_XFER_CONTROL:
413 case USB_ENDPOINT_XFER_BULK:
414 /* fifo policy for these lists, except that NAKing
415 * should rotate a qh to the end (for fairness).
418 head = qh->ring.prev;
425 case USB_ENDPOINT_XFER_ISOC:
426 case USB_ENDPOINT_XFER_INT:
427 /* this is where periodic bandwidth should be
428 * de-allocated if it's tracked and allocated;
429 * and where we'd update the schedule tree...
438 * The pipe must be broken if current urb->status is set, so don't
440 * TODO: to minimize the risk of regression, only check urb->status
441 * for RX, until we have a test case to understand the behavior of TX.
443 if ((!status || !is_in) && qh && qh->is_ready) {
444 dev_dbg(musb->controller, "... next ep%d %cX urb %p\n",
445 hw_ep->epnum, is_in ? 'R' : 'T', next_urb(qh));
446 musb_start_urb(musb, is_in, qh);
450 static u16 musb_h_flush_rxfifo(struct musb_hw_ep *hw_ep, u16 csr)
452 /* we don't want fifo to fill itself again;
453 * ignore dma (various models),
454 * leave toggle alone (may not have been saved yet)
456 csr |= MUSB_RXCSR_FLUSHFIFO | MUSB_RXCSR_RXPKTRDY;
457 csr &= ~(MUSB_RXCSR_H_REQPKT
458 | MUSB_RXCSR_H_AUTOREQ
459 | MUSB_RXCSR_AUTOCLEAR);
461 /* write 2x to allow double buffering */
462 musb_writew(hw_ep->regs, MUSB_RXCSR, csr);
463 musb_writew(hw_ep->regs, MUSB_RXCSR, csr);
465 /* flush writebuffer */
466 return musb_readw(hw_ep->regs, MUSB_RXCSR);
470 * PIO RX for a packet (or part of it).
473 musb_host_packet_rx(struct musb *musb, struct urb *urb, u8 epnum, u8 iso_err)
481 struct musb_hw_ep *hw_ep = musb->endpoints + epnum;
482 void __iomem *epio = hw_ep->regs;
483 struct musb_qh *qh = hw_ep->in_qh;
484 int pipe = urb->pipe;
485 void *buffer = urb->transfer_buffer;
487 /* musb_ep_select(mbase, epnum); */
488 rx_count = musb_readw(epio, MUSB_RXCOUNT);
489 dev_dbg(musb->controller, "RX%d count %d, buffer %p len %d/%d\n", epnum, rx_count,
490 urb->transfer_buffer, qh->offset,
491 urb->transfer_buffer_length);
494 if (usb_pipeisoc(pipe)) {
496 struct usb_iso_packet_descriptor *d;
503 d = urb->iso_frame_desc + qh->iso_idx;
504 buf = buffer + d->offset;
506 if (rx_count > length) {
511 dev_dbg(musb->controller, "** OVERFLOW %d into %d\n", rx_count, length);
515 urb->actual_length += length;
516 d->actual_length = length;
520 /* see if we are done */
521 done = (++qh->iso_idx >= urb->number_of_packets);
524 buf = buffer + qh->offset;
525 length = urb->transfer_buffer_length - qh->offset;
526 if (rx_count > length) {
527 if (urb->status == -EINPROGRESS)
528 urb->status = -EOVERFLOW;
529 dev_dbg(musb->controller, "** OVERFLOW %d into %d\n", rx_count, length);
533 urb->actual_length += length;
534 qh->offset += length;
536 /* see if we are done */
537 done = (urb->actual_length == urb->transfer_buffer_length)
538 || (rx_count < qh->maxpacket)
539 || (urb->status != -EINPROGRESS);
541 && (urb->status == -EINPROGRESS)
542 && (urb->transfer_flags & URB_SHORT_NOT_OK)
543 && (urb->actual_length
544 < urb->transfer_buffer_length))
545 urb->status = -EREMOTEIO;
548 musb_read_fifo(hw_ep, length, buf);
550 csr = musb_readw(epio, MUSB_RXCSR);
551 csr |= MUSB_RXCSR_H_WZC_BITS;
552 if (unlikely(do_flush))
553 musb_h_flush_rxfifo(hw_ep, csr);
555 /* REVISIT this assumes AUTOCLEAR is never set */
556 csr &= ~(MUSB_RXCSR_RXPKTRDY | MUSB_RXCSR_H_REQPKT);
558 csr |= MUSB_RXCSR_H_REQPKT;
559 musb_writew(epio, MUSB_RXCSR, csr);
565 /* we don't always need to reinit a given side of an endpoint...
566 * when we do, use tx/rx reinit routine and then construct a new CSR
567 * to address data toggle, NYET, and DMA or PIO.
569 * it's possible that driver bugs (especially for DMA) or aborting a
570 * transfer might have left the endpoint busier than it should be.
571 * the busy/not-empty tests are basically paranoia.
574 musb_rx_reinit(struct musb *musb, struct musb_qh *qh, u8 epnum)
576 struct musb_hw_ep *ep = musb->endpoints + epnum;
579 /* NOTE: we know the "rx" fifo reinit never triggers for ep0.
580 * That always uses tx_reinit since ep0 repurposes TX register
581 * offsets; the initial SETUP packet is also a kind of OUT.
584 /* if programmed for Tx, put it in RX mode */
585 if (ep->is_shared_fifo) {
586 csr = musb_readw(ep->regs, MUSB_TXCSR);
587 if (csr & MUSB_TXCSR_MODE) {
588 musb_h_tx_flush_fifo(ep);
589 csr = musb_readw(ep->regs, MUSB_TXCSR);
590 musb_writew(ep->regs, MUSB_TXCSR,
591 csr | MUSB_TXCSR_FRCDATATOG);
595 * Clear the MODE bit (and everything else) to enable Rx.
596 * NOTE: we mustn't clear the DMAMODE bit before DMAENAB.
598 if (csr & MUSB_TXCSR_DMAMODE)
599 musb_writew(ep->regs, MUSB_TXCSR, MUSB_TXCSR_DMAMODE);
600 musb_writew(ep->regs, MUSB_TXCSR, 0);
602 /* scrub all previous state, clearing toggle */
604 csr = musb_readw(ep->regs, MUSB_RXCSR);
605 if (csr & MUSB_RXCSR_RXPKTRDY)
606 WARNING("rx%d, packet/%d ready?\n", ep->epnum,
607 musb_readw(ep->regs, MUSB_RXCOUNT));
609 musb_h_flush_rxfifo(ep, MUSB_RXCSR_CLRDATATOG);
612 /* target addr and (for multipoint) hub addr/port */
613 if (musb->is_multipoint) {
614 musb_write_rxfunaddr(musb, epnum, qh->addr_reg);
615 musb_write_rxhubaddr(musb, epnum, qh->h_addr_reg);
616 musb_write_rxhubport(musb, epnum, qh->h_port_reg);
618 musb_writeb(musb->mregs, MUSB_FADDR, qh->addr_reg);
620 /* protocol/endpoint, interval/NAKlimit, i/o size */
621 musb_writeb(ep->regs, MUSB_RXTYPE, qh->type_reg);
622 musb_writeb(ep->regs, MUSB_RXINTERVAL, qh->intv_reg);
623 /* NOTE: bulk combining rewrites high bits of maxpacket */
624 /* Set RXMAXP with the FIFO size of the endpoint
625 * to disable double buffer mode.
627 if (musb->double_buffer_not_ok)
628 musb_writew(ep->regs, MUSB_RXMAXP, ep->max_packet_sz_rx);
630 musb_writew(ep->regs, MUSB_RXMAXP,
631 qh->maxpacket | ((qh->hb_mult - 1) << 11));
636 static void musb_tx_dma_set_mode_mentor(struct dma_controller *dma,
637 struct musb_hw_ep *hw_ep, struct musb_qh *qh,
638 struct urb *urb, u32 offset,
639 u32 *length, u8 *mode)
641 struct dma_channel *channel = hw_ep->tx_channel;
642 void __iomem *epio = hw_ep->regs;
643 u16 pkt_size = qh->maxpacket;
646 if (*length > channel->max_len)
647 *length = channel->max_len;
649 csr = musb_readw(epio, MUSB_TXCSR);
650 if (*length > pkt_size) {
652 csr |= MUSB_TXCSR_DMAMODE | MUSB_TXCSR_DMAENAB;
653 /* autoset shouldn't be set in high bandwidth */
655 * Enable Autoset according to table
657 * bulk_split hb_mult Autoset_Enable
659 * 0 >1 No(High BW ISO)
663 if (qh->hb_mult == 1 || (qh->hb_mult > 1 &&
664 can_bulk_split(hw_ep->musb, qh->type)))
665 csr |= MUSB_TXCSR_AUTOSET;
668 csr &= ~(MUSB_TXCSR_AUTOSET | MUSB_TXCSR_DMAMODE);
669 csr |= MUSB_TXCSR_DMAENAB; /* against programmer's guide */
671 channel->desired_mode = *mode;
672 musb_writew(epio, MUSB_TXCSR, csr);
675 static void musb_tx_dma_set_mode_cppi_tusb(struct dma_controller *dma,
676 struct musb_hw_ep *hw_ep,
683 struct dma_channel *channel = hw_ep->tx_channel;
685 channel->actual_len = 0;
688 * TX uses "RNDIS" mode automatically but needs help
689 * to identify the zero-length-final-packet case.
691 *mode = (urb->transfer_flags & URB_ZERO_PACKET) ? 1 : 0;
694 static bool musb_tx_dma_program(struct dma_controller *dma,
695 struct musb_hw_ep *hw_ep, struct musb_qh *qh,
696 struct urb *urb, u32 offset, u32 length)
698 struct dma_channel *channel = hw_ep->tx_channel;
699 u16 pkt_size = qh->maxpacket;
702 if (musb_dma_inventra(hw_ep->musb) || musb_dma_ux500(hw_ep->musb))
703 musb_tx_dma_set_mode_mentor(dma, hw_ep, qh, urb, offset,
705 else if (is_cppi_enabled(hw_ep->musb) || tusb_dma_omap(hw_ep->musb))
706 musb_tx_dma_set_mode_cppi_tusb(dma, hw_ep, qh, urb, offset,
711 qh->segsize = length;
714 * Ensure the data reaches to main memory before starting
719 if (!dma->channel_program(channel, pkt_size, mode,
720 urb->transfer_dma + offset, length)) {
721 void __iomem *epio = hw_ep->regs;
724 dma->channel_release(channel);
725 hw_ep->tx_channel = NULL;
727 csr = musb_readw(epio, MUSB_TXCSR);
728 csr &= ~(MUSB_TXCSR_AUTOSET | MUSB_TXCSR_DMAENAB);
729 musb_writew(epio, MUSB_TXCSR, csr | MUSB_TXCSR_H_WZC_BITS);
736 * Program an HDRC endpoint as per the given URB
737 * Context: irqs blocked, controller lock held
739 static void musb_ep_program(struct musb *musb, u8 epnum,
740 struct urb *urb, int is_out,
741 u8 *buf, u32 offset, u32 len)
743 struct dma_controller *dma_controller;
744 struct dma_channel *dma_channel;
746 void __iomem *mbase = musb->mregs;
747 struct musb_hw_ep *hw_ep = musb->endpoints + epnum;
748 void __iomem *epio = hw_ep->regs;
749 struct musb_qh *qh = musb_ep_get_qh(hw_ep, !is_out);
750 u16 packet_sz = qh->maxpacket;
754 dev_dbg(musb->controller, "%s hw%d urb %p spd%d dev%d ep%d%s "
755 "h_addr%02x h_port%02x bytes %d\n",
756 is_out ? "-->" : "<--",
757 epnum, urb, urb->dev->speed,
758 qh->addr_reg, qh->epnum, is_out ? "out" : "in",
759 qh->h_addr_reg, qh->h_port_reg,
762 musb_ep_select(mbase, epnum);
764 if (is_out && !len) {
766 csr = musb_readw(epio, MUSB_TXCSR);
767 csr &= ~MUSB_TXCSR_DMAENAB;
768 musb_writew(epio, MUSB_TXCSR, csr);
769 hw_ep->tx_channel = NULL;
772 /* candidate for DMA? */
773 dma_controller = musb->dma_controller;
774 if (use_dma && is_dma_capable() && epnum && dma_controller) {
775 dma_channel = is_out ? hw_ep->tx_channel : hw_ep->rx_channel;
777 dma_channel = dma_controller->channel_alloc(
778 dma_controller, hw_ep, is_out);
780 hw_ep->tx_channel = dma_channel;
782 hw_ep->rx_channel = dma_channel;
787 /* make sure we clear DMAEnab, autoSet bits from previous run */
789 /* OUT/transmit/EP0 or IN/receive? */
795 csr = musb_readw(epio, MUSB_TXCSR);
797 /* disable interrupt in case we flush */
798 int_txe = musb->intrtxe;
799 musb_writew(mbase, MUSB_INTRTXE, int_txe & ~(1 << epnum));
801 /* general endpoint setup */
803 /* flush all old state, set default */
805 * We could be flushing valid
806 * packets in double buffering
809 if (!hw_ep->tx_double_buffered)
810 musb_h_tx_flush_fifo(hw_ep);
813 * We must not clear the DMAMODE bit before or in
814 * the same cycle with the DMAENAB bit, so we clear
815 * the latter first...
817 csr &= ~(MUSB_TXCSR_H_NAKTIMEOUT
820 | MUSB_TXCSR_FRCDATATOG
821 | MUSB_TXCSR_H_RXSTALL
823 | MUSB_TXCSR_TXPKTRDY
825 csr |= MUSB_TXCSR_MODE;
827 if (!hw_ep->tx_double_buffered) {
828 if (usb_gettoggle(urb->dev, qh->epnum, 1))
829 csr |= MUSB_TXCSR_H_WR_DATATOGGLE
830 | MUSB_TXCSR_H_DATATOGGLE;
832 csr |= MUSB_TXCSR_CLRDATATOG;
835 musb_writew(epio, MUSB_TXCSR, csr);
836 /* REVISIT may need to clear FLUSHFIFO ... */
837 csr &= ~MUSB_TXCSR_DMAMODE;
838 musb_writew(epio, MUSB_TXCSR, csr);
839 csr = musb_readw(epio, MUSB_TXCSR);
841 /* endpoint 0: just flush */
842 musb_h_ep0_flush_fifo(hw_ep);
845 /* target addr and (for multipoint) hub addr/port */
846 if (musb->is_multipoint) {
847 musb_write_txfunaddr(musb, epnum, qh->addr_reg);
848 musb_write_txhubaddr(musb, epnum, qh->h_addr_reg);
849 musb_write_txhubport(musb, epnum, qh->h_port_reg);
850 /* FIXME if !epnum, do the same for RX ... */
852 musb_writeb(mbase, MUSB_FADDR, qh->addr_reg);
854 /* protocol/endpoint/interval/NAKlimit */
856 musb_writeb(epio, MUSB_TXTYPE, qh->type_reg);
857 if (musb->double_buffer_not_ok) {
858 musb_writew(epio, MUSB_TXMAXP,
859 hw_ep->max_packet_sz_tx);
860 } else if (can_bulk_split(musb, qh->type)) {
861 qh->hb_mult = hw_ep->max_packet_sz_tx
863 musb_writew(epio, MUSB_TXMAXP, packet_sz
864 | ((qh->hb_mult) - 1) << 11);
866 musb_writew(epio, MUSB_TXMAXP,
868 ((qh->hb_mult - 1) << 11));
870 musb_writeb(epio, MUSB_TXINTERVAL, qh->intv_reg);
872 musb_writeb(epio, MUSB_NAKLIMIT0, qh->intv_reg);
873 if (musb->is_multipoint)
874 musb_writeb(epio, MUSB_TYPE0,
878 if (can_bulk_split(musb, qh->type))
879 load_count = min((u32) hw_ep->max_packet_sz_tx,
882 load_count = min((u32) packet_sz, len);
884 if (dma_channel && musb_tx_dma_program(dma_controller,
885 hw_ep, qh, urb, offset, len))
889 /* PIO to load FIFO */
890 qh->segsize = load_count;
892 sg_miter_start(&qh->sg_miter, urb->sg, 1,
895 if (!sg_miter_next(&qh->sg_miter)) {
896 dev_err(musb->controller,
899 sg_miter_stop(&qh->sg_miter);
902 buf = qh->sg_miter.addr + urb->sg->offset +
904 load_count = min_t(u32, load_count,
905 qh->sg_miter.length);
906 musb_write_fifo(hw_ep, load_count, buf);
907 qh->sg_miter.consumed = load_count;
908 sg_miter_stop(&qh->sg_miter);
910 musb_write_fifo(hw_ep, load_count, buf);
913 /* re-enable interrupt */
914 musb_writew(mbase, MUSB_INTRTXE, int_txe);
920 if (hw_ep->rx_reinit) {
921 musb_rx_reinit(musb, qh, epnum);
923 /* init new state: toggle and NYET, maybe DMA later */
924 if (usb_gettoggle(urb->dev, qh->epnum, 0))
925 csr = MUSB_RXCSR_H_WR_DATATOGGLE
926 | MUSB_RXCSR_H_DATATOGGLE;
929 if (qh->type == USB_ENDPOINT_XFER_INT)
930 csr |= MUSB_RXCSR_DISNYET;
933 csr = musb_readw(hw_ep->regs, MUSB_RXCSR);
935 if (csr & (MUSB_RXCSR_RXPKTRDY
937 | MUSB_RXCSR_H_REQPKT))
938 ERR("broken !rx_reinit, ep%d csr %04x\n",
941 /* scrub any stale state, leaving toggle alone */
942 csr &= MUSB_RXCSR_DISNYET;
945 /* kick things off */
947 if ((is_cppi_enabled(musb) || tusb_dma_omap(musb)) && dma_channel) {
948 /* Candidate for DMA */
949 dma_channel->actual_len = 0L;
952 /* AUTOREQ is in a DMA register */
953 musb_writew(hw_ep->regs, MUSB_RXCSR, csr);
954 csr = musb_readw(hw_ep->regs, MUSB_RXCSR);
957 * Unless caller treats short RX transfers as
958 * errors, we dare not queue multiple transfers.
960 dma_ok = dma_controller->channel_program(dma_channel,
961 packet_sz, !(urb->transfer_flags &
963 urb->transfer_dma + offset,
966 dma_controller->channel_release(dma_channel);
967 hw_ep->rx_channel = dma_channel = NULL;
969 csr |= MUSB_RXCSR_DMAENAB;
972 csr |= MUSB_RXCSR_H_REQPKT;
973 dev_dbg(musb->controller, "RXCSR%d := %04x\n", epnum, csr);
974 musb_writew(hw_ep->regs, MUSB_RXCSR, csr);
975 csr = musb_readw(hw_ep->regs, MUSB_RXCSR);
979 /* Schedule next QH from musb->in_bulk/out_bulk and move the current qh to
980 * the end; avoids starvation for other endpoints.
982 static void musb_bulk_nak_timeout(struct musb *musb, struct musb_hw_ep *ep,
985 struct dma_channel *dma;
987 void __iomem *mbase = musb->mregs;
988 void __iomem *epio = ep->regs;
989 struct musb_qh *cur_qh, *next_qh;
992 musb_ep_select(mbase, ep->epnum);
994 dma = is_dma_capable() ? ep->rx_channel : NULL;
996 /* clear nak timeout bit */
997 rx_csr = musb_readw(epio, MUSB_RXCSR);
998 rx_csr |= MUSB_RXCSR_H_WZC_BITS;
999 rx_csr &= ~MUSB_RXCSR_DATAERROR;
1000 musb_writew(epio, MUSB_RXCSR, rx_csr);
1002 cur_qh = first_qh(&musb->in_bulk);
1004 dma = is_dma_capable() ? ep->tx_channel : NULL;
1006 /* clear nak timeout bit */
1007 tx_csr = musb_readw(epio, MUSB_TXCSR);
1008 tx_csr |= MUSB_TXCSR_H_WZC_BITS;
1009 tx_csr &= ~MUSB_TXCSR_H_NAKTIMEOUT;
1010 musb_writew(epio, MUSB_TXCSR, tx_csr);
1012 cur_qh = first_qh(&musb->out_bulk);
1015 urb = next_urb(cur_qh);
1016 if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) {
1017 dma->status = MUSB_DMA_STATUS_CORE_ABORT;
1018 musb->dma_controller->channel_abort(dma);
1019 urb->actual_length += dma->actual_len;
1020 dma->actual_len = 0L;
1022 musb_save_toggle(cur_qh, is_in, urb);
1025 /* move cur_qh to end of queue */
1026 list_move_tail(&cur_qh->ring, &musb->in_bulk);
1028 /* get the next qh from musb->in_bulk */
1029 next_qh = first_qh(&musb->in_bulk);
1031 /* set rx_reinit and schedule the next qh */
1034 /* move cur_qh to end of queue */
1035 list_move_tail(&cur_qh->ring, &musb->out_bulk);
1037 /* get the next qh from musb->out_bulk */
1038 next_qh = first_qh(&musb->out_bulk);
1040 /* set tx_reinit and schedule the next qh */
1043 musb_start_urb(musb, is_in, next_qh);
1048 * Service the default endpoint (ep0) as host.
1049 * Return true until it's time to start the status stage.
1051 static bool musb_h_ep0_continue(struct musb *musb, u16 len, struct urb *urb)
1054 u8 *fifo_dest = NULL;
1056 struct musb_hw_ep *hw_ep = musb->control_ep;
1057 struct musb_qh *qh = hw_ep->in_qh;
1058 struct usb_ctrlrequest *request;
1060 switch (musb->ep0_stage) {
1062 fifo_dest = urb->transfer_buffer + urb->actual_length;
1063 fifo_count = min_t(size_t, len, urb->transfer_buffer_length -
1064 urb->actual_length);
1065 if (fifo_count < len)
1066 urb->status = -EOVERFLOW;
1068 musb_read_fifo(hw_ep, fifo_count, fifo_dest);
1070 urb->actual_length += fifo_count;
1071 if (len < qh->maxpacket) {
1072 /* always terminate on short read; it's
1073 * rarely reported as an error.
1075 } else if (urb->actual_length <
1076 urb->transfer_buffer_length)
1079 case MUSB_EP0_START:
1080 request = (struct usb_ctrlrequest *) urb->setup_packet;
1082 if (!request->wLength) {
1083 dev_dbg(musb->controller, "start no-DATA\n");
1085 } else if (request->bRequestType & USB_DIR_IN) {
1086 dev_dbg(musb->controller, "start IN-DATA\n");
1087 musb->ep0_stage = MUSB_EP0_IN;
1091 dev_dbg(musb->controller, "start OUT-DATA\n");
1092 musb->ep0_stage = MUSB_EP0_OUT;
1097 fifo_count = min_t(size_t, qh->maxpacket,
1098 urb->transfer_buffer_length -
1099 urb->actual_length);
1101 fifo_dest = (u8 *) (urb->transfer_buffer
1102 + urb->actual_length);
1103 dev_dbg(musb->controller, "Sending %d byte%s to ep0 fifo %p\n",
1105 (fifo_count == 1) ? "" : "s",
1107 musb_write_fifo(hw_ep, fifo_count, fifo_dest);
1109 urb->actual_length += fifo_count;
1114 ERR("bogus ep0 stage %d\n", musb->ep0_stage);
1122 * Handle default endpoint interrupt as host. Only called in IRQ time
1123 * from musb_interrupt().
1125 * called with controller irqlocked
1127 irqreturn_t musb_h_ep0_irq(struct musb *musb)
1132 void __iomem *mbase = musb->mregs;
1133 struct musb_hw_ep *hw_ep = musb->control_ep;
1134 void __iomem *epio = hw_ep->regs;
1135 struct musb_qh *qh = hw_ep->in_qh;
1136 bool complete = false;
1137 irqreturn_t retval = IRQ_NONE;
1139 /* ep0 only has one queue, "in" */
1142 musb_ep_select(mbase, 0);
1143 csr = musb_readw(epio, MUSB_CSR0);
1144 len = (csr & MUSB_CSR0_RXPKTRDY)
1145 ? musb_readb(epio, MUSB_COUNT0)
1148 dev_dbg(musb->controller, "<== csr0 %04x, qh %p, count %d, urb %p, stage %d\n",
1149 csr, qh, len, urb, musb->ep0_stage);
1151 /* if we just did status stage, we are done */
1152 if (MUSB_EP0_STATUS == musb->ep0_stage) {
1153 retval = IRQ_HANDLED;
1157 /* prepare status */
1158 if (csr & MUSB_CSR0_H_RXSTALL) {
1159 dev_dbg(musb->controller, "STALLING ENDPOINT\n");
1162 } else if (csr & MUSB_CSR0_H_ERROR) {
1163 dev_dbg(musb->controller, "no response, csr0 %04x\n", csr);
1166 } else if (csr & MUSB_CSR0_H_NAKTIMEOUT) {
1167 dev_dbg(musb->controller, "control NAK timeout\n");
1169 /* NOTE: this code path would be a good place to PAUSE a
1170 * control transfer, if another one is queued, so that
1171 * ep0 is more likely to stay busy. That's already done
1172 * for bulk RX transfers.
1174 * if (qh->ring.next != &musb->control), then
1175 * we have a candidate... NAKing is *NOT* an error
1177 musb_writew(epio, MUSB_CSR0, 0);
1178 retval = IRQ_HANDLED;
1182 dev_dbg(musb->controller, "aborting\n");
1183 retval = IRQ_HANDLED;
1185 urb->status = status;
1188 /* use the proper sequence to abort the transfer */
1189 if (csr & MUSB_CSR0_H_REQPKT) {
1190 csr &= ~MUSB_CSR0_H_REQPKT;
1191 musb_writew(epio, MUSB_CSR0, csr);
1192 csr &= ~MUSB_CSR0_H_NAKTIMEOUT;
1193 musb_writew(epio, MUSB_CSR0, csr);
1195 musb_h_ep0_flush_fifo(hw_ep);
1198 musb_writeb(epio, MUSB_NAKLIMIT0, 0);
1201 musb_writew(epio, MUSB_CSR0, 0);
1204 if (unlikely(!urb)) {
1205 /* stop endpoint since we have no place for its data, this
1206 * SHOULD NEVER HAPPEN! */
1207 ERR("no URB for end 0\n");
1209 musb_h_ep0_flush_fifo(hw_ep);
1214 /* call common logic and prepare response */
1215 if (musb_h_ep0_continue(musb, len, urb)) {
1216 /* more packets required */
1217 csr = (MUSB_EP0_IN == musb->ep0_stage)
1218 ? MUSB_CSR0_H_REQPKT : MUSB_CSR0_TXPKTRDY;
1220 /* data transfer complete; perform status phase */
1221 if (usb_pipeout(urb->pipe)
1222 || !urb->transfer_buffer_length)
1223 csr = MUSB_CSR0_H_STATUSPKT
1224 | MUSB_CSR0_H_REQPKT;
1226 csr = MUSB_CSR0_H_STATUSPKT
1227 | MUSB_CSR0_TXPKTRDY;
1229 /* disable ping token in status phase */
1230 csr |= MUSB_CSR0_H_DIS_PING;
1232 /* flag status stage */
1233 musb->ep0_stage = MUSB_EP0_STATUS;
1235 dev_dbg(musb->controller, "ep0 STATUS, csr %04x\n", csr);
1238 musb_writew(epio, MUSB_CSR0, csr);
1239 retval = IRQ_HANDLED;
1241 musb->ep0_stage = MUSB_EP0_IDLE;
1243 /* call completion handler if done */
1245 musb_advance_schedule(musb, urb, hw_ep, 1);
1251 #ifdef CONFIG_USB_INVENTRA_DMA
1253 /* Host side TX (OUT) using Mentor DMA works as follows:
1255 - if queue was empty, Program Endpoint
1256 - ... which starts DMA to fifo in mode 1 or 0
1258 DMA Isr (transfer complete) -> TxAvail()
1259 - Stop DMA (~DmaEnab) (<--- Alert ... currently happens
1260 only in musb_cleanup_urb)
1261 - TxPktRdy has to be set in mode 0 or for
1262 short packets in mode 1.
1267 /* Service a Tx-Available or dma completion irq for the endpoint */
1268 void musb_host_tx(struct musb *musb, u8 epnum)
1275 struct musb_hw_ep *hw_ep = musb->endpoints + epnum;
1276 void __iomem *epio = hw_ep->regs;
1277 struct musb_qh *qh = hw_ep->out_qh;
1278 struct urb *urb = next_urb(qh);
1280 void __iomem *mbase = musb->mregs;
1281 struct dma_channel *dma;
1282 bool transfer_pending = false;
1284 musb_ep_select(mbase, epnum);
1285 tx_csr = musb_readw(epio, MUSB_TXCSR);
1287 /* with CPPI, DMA sometimes triggers "extra" irqs */
1289 dev_dbg(musb->controller, "extra TX%d ready, csr %04x\n", epnum, tx_csr);
1294 dma = is_dma_capable() ? hw_ep->tx_channel : NULL;
1295 dev_dbg(musb->controller, "OUT/TX%d end, csr %04x%s\n", epnum, tx_csr,
1296 dma ? ", dma" : "");
1298 /* check for errors */
1299 if (tx_csr & MUSB_TXCSR_H_RXSTALL) {
1300 /* dma was disabled, fifo flushed */
1301 dev_dbg(musb->controller, "TX end %d stall\n", epnum);
1303 /* stall; record URB status */
1306 } else if (tx_csr & MUSB_TXCSR_H_ERROR) {
1307 /* (NON-ISO) dma was disabled, fifo flushed */
1308 dev_dbg(musb->controller, "TX 3strikes on ep=%d\n", epnum);
1310 status = -ETIMEDOUT;
1312 } else if (tx_csr & MUSB_TXCSR_H_NAKTIMEOUT) {
1313 if (USB_ENDPOINT_XFER_BULK == qh->type && qh->mux == 1
1314 && !list_is_singular(&musb->out_bulk)) {
1315 dev_dbg(musb->controller,
1316 "NAK timeout on TX%d ep\n", epnum);
1317 musb_bulk_nak_timeout(musb, hw_ep, 0);
1319 dev_dbg(musb->controller,
1320 "TX end=%d device not responding\n", epnum);
1321 /* NOTE: this code path would be a good place to PAUSE a
1322 * transfer, if there's some other (nonperiodic) tx urb
1323 * that could use this fifo. (dma complicates it...)
1324 * That's already done for bulk RX transfers.
1326 * if (bulk && qh->ring.next != &musb->out_bulk), then
1327 * we have a candidate... NAKing is *NOT* an error
1329 musb_ep_select(mbase, epnum);
1330 musb_writew(epio, MUSB_TXCSR,
1331 MUSB_TXCSR_H_WZC_BITS
1332 | MUSB_TXCSR_TXPKTRDY);
1339 if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) {
1340 dma->status = MUSB_DMA_STATUS_CORE_ABORT;
1341 musb->dma_controller->channel_abort(dma);
1344 /* do the proper sequence to abort the transfer in the
1345 * usb core; the dma engine should already be stopped.
1347 musb_h_tx_flush_fifo(hw_ep);
1348 tx_csr &= ~(MUSB_TXCSR_AUTOSET
1349 | MUSB_TXCSR_DMAENAB
1350 | MUSB_TXCSR_H_ERROR
1351 | MUSB_TXCSR_H_RXSTALL
1352 | MUSB_TXCSR_H_NAKTIMEOUT
1355 musb_ep_select(mbase, epnum);
1356 musb_writew(epio, MUSB_TXCSR, tx_csr);
1357 /* REVISIT may need to clear FLUSHFIFO ... */
1358 musb_writew(epio, MUSB_TXCSR, tx_csr);
1359 musb_writeb(epio, MUSB_TXINTERVAL, 0);
1364 /* second cppi case */
1365 if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) {
1366 dev_dbg(musb->controller, "extra TX%d ready, csr %04x\n", epnum, tx_csr);
1370 if (is_dma_capable() && dma && !status) {
1372 * DMA has completed. But if we're using DMA mode 1 (multi
1373 * packet DMA), we need a terminal TXPKTRDY interrupt before
1374 * we can consider this transfer completed, lest we trash
1375 * its last packet when writing the next URB's data. So we
1376 * switch back to mode 0 to get that interrupt; we'll come
1377 * back here once it happens.
1379 if (tx_csr & MUSB_TXCSR_DMAMODE) {
1381 * We shouldn't clear DMAMODE with DMAENAB set; so
1382 * clear them in a safe order. That should be OK
1383 * once TXPKTRDY has been set (and I've never seen
1384 * it being 0 at this moment -- DMA interrupt latency
1385 * is significant) but if it hasn't been then we have
1386 * no choice but to stop being polite and ignore the
1387 * programmer's guide... :-)
1389 * Note that we must write TXCSR with TXPKTRDY cleared
1390 * in order not to re-trigger the packet send (this bit
1391 * can't be cleared by CPU), and there's another caveat:
1392 * TXPKTRDY may be set shortly and then cleared in the
1393 * double-buffered FIFO mode, so we do an extra TXCSR
1394 * read for debouncing...
1396 tx_csr &= musb_readw(epio, MUSB_TXCSR);
1397 if (tx_csr & MUSB_TXCSR_TXPKTRDY) {
1398 tx_csr &= ~(MUSB_TXCSR_DMAENAB |
1399 MUSB_TXCSR_TXPKTRDY);
1400 musb_writew(epio, MUSB_TXCSR,
1401 tx_csr | MUSB_TXCSR_H_WZC_BITS);
1403 tx_csr &= ~(MUSB_TXCSR_DMAMODE |
1404 MUSB_TXCSR_TXPKTRDY);
1405 musb_writew(epio, MUSB_TXCSR,
1406 tx_csr | MUSB_TXCSR_H_WZC_BITS);
1409 * There is no guarantee that we'll get an interrupt
1410 * after clearing DMAMODE as we might have done this
1411 * too late (after TXPKTRDY was cleared by controller).
1412 * Re-read TXCSR as we have spoiled its previous value.
1414 tx_csr = musb_readw(epio, MUSB_TXCSR);
1418 * We may get here from a DMA completion or TXPKTRDY interrupt.
1419 * In any case, we must check the FIFO status here and bail out
1420 * only if the FIFO still has data -- that should prevent the
1421 * "missed" TXPKTRDY interrupts and deal with double-buffered
1424 if (tx_csr & (MUSB_TXCSR_FIFONOTEMPTY | MUSB_TXCSR_TXPKTRDY)) {
1425 dev_dbg(musb->controller, "DMA complete but packet still in FIFO, "
1426 "CSR %04x\n", tx_csr);
1431 if (!status || dma || usb_pipeisoc(pipe)) {
1433 length = dma->actual_len;
1435 length = qh->segsize;
1436 qh->offset += length;
1438 if (usb_pipeisoc(pipe)) {
1439 struct usb_iso_packet_descriptor *d;
1441 d = urb->iso_frame_desc + qh->iso_idx;
1442 d->actual_length = length;
1444 if (++qh->iso_idx >= urb->number_of_packets) {
1451 } else if (dma && urb->transfer_buffer_length == qh->offset) {
1454 /* see if we need to send more data, or ZLP */
1455 if (qh->segsize < qh->maxpacket)
1457 else if (qh->offset == urb->transfer_buffer_length
1458 && !(urb->transfer_flags
1462 offset = qh->offset;
1463 length = urb->transfer_buffer_length - offset;
1464 transfer_pending = true;
1469 /* urb->status != -EINPROGRESS means request has been faulted,
1470 * so we must abort this transfer after cleanup
1472 if (urb->status != -EINPROGRESS) {
1475 status = urb->status;
1480 urb->status = status;
1481 urb->actual_length = qh->offset;
1482 musb_advance_schedule(musb, urb, hw_ep, USB_DIR_OUT);
1484 } else if ((usb_pipeisoc(pipe) || transfer_pending) && dma) {
1485 if (musb_tx_dma_program(musb->dma_controller, hw_ep, qh, urb,
1487 if (is_cppi_enabled(musb) || tusb_dma_omap(musb))
1488 musb_h_tx_dma_start(hw_ep);
1491 } else if (tx_csr & MUSB_TXCSR_DMAENAB) {
1492 dev_dbg(musb->controller, "not complete, but DMA enabled?\n");
1497 * PIO: start next packet in this URB.
1499 * REVISIT: some docs say that when hw_ep->tx_double_buffered,
1500 * (and presumably, FIFO is not half-full) we should write *two*
1501 * packets before updating TXCSR; other docs disagree...
1503 if (length > qh->maxpacket)
1504 length = qh->maxpacket;
1505 /* Unmap the buffer so that CPU can use it */
1506 usb_hcd_unmap_urb_for_dma(musb->hcd, urb);
1509 * We need to map sg if the transfer_buffer is
1512 if (!urb->transfer_buffer)
1516 /* sg_miter_start is already done in musb_ep_program */
1517 if (!sg_miter_next(&qh->sg_miter)) {
1518 dev_err(musb->controller, "error: sg list empty\n");
1519 sg_miter_stop(&qh->sg_miter);
1523 urb->transfer_buffer = qh->sg_miter.addr;
1524 length = min_t(u32, length, qh->sg_miter.length);
1525 musb_write_fifo(hw_ep, length, urb->transfer_buffer);
1526 qh->sg_miter.consumed = length;
1527 sg_miter_stop(&qh->sg_miter);
1529 musb_write_fifo(hw_ep, length, urb->transfer_buffer + offset);
1532 qh->segsize = length;
1535 if (offset + length >= urb->transfer_buffer_length)
1539 musb_ep_select(mbase, epnum);
1540 musb_writew(epio, MUSB_TXCSR,
1541 MUSB_TXCSR_H_WZC_BITS | MUSB_TXCSR_TXPKTRDY);
1544 #ifdef CONFIG_USB_TI_CPPI41_DMA
1545 /* Seems to set up ISO for cppi41 and not advance len. See commit c57c41d */
1546 static int musb_rx_dma_iso_cppi41(struct dma_controller *dma,
1547 struct musb_hw_ep *hw_ep,
1552 struct dma_channel *channel = hw_ep->tx_channel;
1553 void __iomem *epio = hw_ep->regs;
1558 buf = (void *)urb->iso_frame_desc[qh->iso_idx].offset +
1559 (u32)urb->transfer_dma;
1561 length = urb->iso_frame_desc[qh->iso_idx].length;
1563 val = musb_readw(epio, MUSB_RXCSR);
1564 val |= MUSB_RXCSR_DMAENAB;
1565 musb_writew(hw_ep->regs, MUSB_RXCSR, val);
1567 res = dma->channel_program(channel, qh->maxpacket, 0,
1573 static inline int musb_rx_dma_iso_cppi41(struct dma_controller *dma,
1574 struct musb_hw_ep *hw_ep,
1583 #if defined(CONFIG_USB_INVENTRA_DMA) || defined(CONFIG_USB_UX500_DMA) || \
1584 defined(CONFIG_USB_TI_CPPI41_DMA)
1585 /* Host side RX (IN) using Mentor DMA works as follows:
1587 - if queue was empty, ProgramEndpoint
1588 - first IN token is sent out (by setting ReqPkt)
1589 LinuxIsr -> RxReady()
1590 /\ => first packet is received
1591 | - Set in mode 0 (DmaEnab, ~ReqPkt)
1592 | -> DMA Isr (transfer complete) -> RxReady()
1593 | - Ack receive (~RxPktRdy), turn off DMA (~DmaEnab)
1594 | - if urb not complete, send next IN token (ReqPkt)
1595 | | else complete urb.
1597 ---------------------------
1599 * Nuances of mode 1:
1600 * For short packets, no ack (+RxPktRdy) is sent automatically
1601 * (even if AutoClear is ON)
1602 * For full packets, ack (~RxPktRdy) and next IN token (+ReqPkt) is sent
1603 * automatically => major problem, as collecting the next packet becomes
1604 * difficult. Hence mode 1 is not used.
1607 * All we care about at this driver level is that
1608 * (a) all URBs terminate with REQPKT cleared and fifo(s) empty;
1609 * (b) termination conditions are: short RX, or buffer full;
1610 * (c) fault modes include
1611 * - iff URB_SHORT_NOT_OK, short RX status is -EREMOTEIO.
1612 * (and that endpoint's dma queue stops immediately)
1613 * - overflow (full, PLUS more bytes in the terminal packet)
1615 * So for example, usb-storage sets URB_SHORT_NOT_OK, and would
1616 * thus be a great candidate for using mode 1 ... for all but the
1617 * last packet of one URB's transfer.
1619 static int musb_rx_dma_inventra_cppi41(struct dma_controller *dma,
1620 struct musb_hw_ep *hw_ep,
1625 struct dma_channel *channel = hw_ep->rx_channel;
1626 void __iomem *epio = hw_ep->regs;
1633 if (usb_pipeisoc(pipe)) {
1634 struct usb_iso_packet_descriptor *d;
1636 d = urb->iso_frame_desc + qh->iso_idx;
1637 d->actual_length = len;
1639 /* even if there was an error, we did the dma
1640 * for iso_frame_desc->length
1642 if (d->status != -EILSEQ && d->status != -EOVERFLOW)
1645 if (++qh->iso_idx >= urb->number_of_packets) {
1648 /* REVISIT: Why ignore return value here? */
1649 if (musb_dma_cppi41(hw_ep->musb))
1650 done = musb_rx_dma_iso_cppi41(dma, hw_ep, qh,
1656 /* done if urb buffer is full or short packet is recd */
1657 done = (urb->actual_length + len >=
1658 urb->transfer_buffer_length
1659 || channel->actual_len < qh->maxpacket
1660 || channel->rx_packet_done);
1663 /* send IN token for next packet, without AUTOREQ */
1665 val = musb_readw(epio, MUSB_RXCSR);
1666 val |= MUSB_RXCSR_H_REQPKT;
1667 musb_writew(epio, MUSB_RXCSR, MUSB_RXCSR_H_WZC_BITS | val);
1673 /* Disadvantage of using mode 1:
1674 * It's basically usable only for mass storage class; essentially all
1675 * other protocols also terminate transfers on short packets.
1678 * An extra IN token is sent at the end of the transfer (due to AUTOREQ)
1679 * If you try to use mode 1 for (transfer_buffer_length - 512), and try
1680 * to use the extra IN token to grab the last packet using mode 0, then
1681 * the problem is that you cannot be sure when the device will send the
1682 * last packet and RxPktRdy set. Sometimes the packet is recd too soon
1683 * such that it gets lost when RxCSR is re-set at the end of the mode 1
1684 * transfer, while sometimes it is recd just a little late so that if you
1685 * try to configure for mode 0 soon after the mode 1 transfer is
1686 * completed, you will find rxcount 0. Okay, so you might think why not
1687 * wait for an interrupt when the pkt is recd. Well, you won't get any!
1689 static int musb_rx_dma_in_inventra_cppi41(struct dma_controller *dma,
1690 struct musb_hw_ep *hw_ep,
1696 struct musb *musb = hw_ep->musb;
1697 void __iomem *epio = hw_ep->regs;
1698 struct dma_channel *channel = hw_ep->rx_channel;
1700 int length, pipe, done;
1703 rx_count = musb_readw(epio, MUSB_RXCOUNT);
1706 if (usb_pipeisoc(pipe)) {
1708 struct usb_iso_packet_descriptor *d;
1710 d = urb->iso_frame_desc + qh->iso_idx;
1716 if (rx_count > d->length) {
1717 if (d_status == 0) {
1718 d_status = -EOVERFLOW;
1721 dev_dbg(musb->controller, "** OVERFLOW %d into %d\n",
1722 rx_count, d->length);
1727 d->status = d_status;
1728 buf = urb->transfer_dma + d->offset;
1731 buf = urb->transfer_dma + urb->actual_length;
1734 channel->desired_mode = 0;
1736 /* because of the issue below, mode 1 will
1737 * only rarely behave with correct semantics.
1739 if ((urb->transfer_flags & URB_SHORT_NOT_OK)
1740 && (urb->transfer_buffer_length - urb->actual_length)
1742 channel->desired_mode = 1;
1743 if (rx_count < hw_ep->max_packet_sz_rx) {
1745 channel->desired_mode = 0;
1747 length = urb->transfer_buffer_length;
1751 /* See comments above on disadvantages of using mode 1 */
1752 val = musb_readw(epio, MUSB_RXCSR);
1753 val &= ~MUSB_RXCSR_H_REQPKT;
1755 if (channel->desired_mode == 0)
1756 val &= ~MUSB_RXCSR_H_AUTOREQ;
1758 val |= MUSB_RXCSR_H_AUTOREQ;
1759 val |= MUSB_RXCSR_DMAENAB;
1761 /* autoclear shouldn't be set in high bandwidth */
1762 if (qh->hb_mult == 1)
1763 val |= MUSB_RXCSR_AUTOCLEAR;
1765 musb_writew(epio, MUSB_RXCSR, MUSB_RXCSR_H_WZC_BITS | val);
1767 /* REVISIT if when actual_length != 0,
1768 * transfer_buffer_length needs to be
1771 done = dma->channel_program(channel, qh->maxpacket,
1772 channel->desired_mode,
1776 dma->channel_release(channel);
1777 hw_ep->rx_channel = NULL;
1779 val = musb_readw(epio, MUSB_RXCSR);
1780 val &= ~(MUSB_RXCSR_DMAENAB
1781 | MUSB_RXCSR_H_AUTOREQ
1782 | MUSB_RXCSR_AUTOCLEAR);
1783 musb_writew(epio, MUSB_RXCSR, val);
1789 static inline int musb_rx_dma_inventra_cppi41(struct dma_controller *dma,
1790 struct musb_hw_ep *hw_ep,
1798 static inline int musb_rx_dma_in_inventra_cppi41(struct dma_controller *dma,
1799 struct musb_hw_ep *hw_ep,
1810 * Service an RX interrupt for the given IN endpoint; docs cover bulk, iso,
1811 * and high-bandwidth IN transfer cases.
1813 void musb_host_rx(struct musb *musb, u8 epnum)
1816 struct musb_hw_ep *hw_ep = musb->endpoints + epnum;
1817 struct dma_controller *c = musb->dma_controller;
1818 void __iomem *epio = hw_ep->regs;
1819 struct musb_qh *qh = hw_ep->in_qh;
1821 void __iomem *mbase = musb->mregs;
1824 bool iso_err = false;
1827 struct dma_channel *dma;
1828 unsigned int sg_flags = SG_MITER_ATOMIC | SG_MITER_TO_SG;
1830 musb_ep_select(mbase, epnum);
1833 dma = is_dma_capable() ? hw_ep->rx_channel : NULL;
1837 rx_csr = musb_readw(epio, MUSB_RXCSR);
1840 if (unlikely(!urb)) {
1841 /* REVISIT -- THIS SHOULD NEVER HAPPEN ... but, at least
1842 * usbtest #11 (unlinks) triggers it regularly, sometimes
1843 * with fifo full. (Only with DMA??)
1845 dev_dbg(musb->controller, "BOGUS RX%d ready, csr %04x, count %d\n", epnum, val,
1846 musb_readw(epio, MUSB_RXCOUNT));
1847 musb_h_flush_rxfifo(hw_ep, MUSB_RXCSR_CLRDATATOG);
1853 dev_dbg(musb->controller, "<== hw %d rxcsr %04x, urb actual %d (+dma %zu)\n",
1854 epnum, rx_csr, urb->actual_length,
1855 dma ? dma->actual_len : 0);
1857 /* check for errors, concurrent stall & unlink is not really
1859 if (rx_csr & MUSB_RXCSR_H_RXSTALL) {
1860 dev_dbg(musb->controller, "RX end %d STALL\n", epnum);
1862 /* stall; record URB status */
1865 } else if (rx_csr & MUSB_RXCSR_H_ERROR) {
1866 dev_dbg(musb->controller, "end %d RX proto error\n", epnum);
1869 musb_writeb(epio, MUSB_RXINTERVAL, 0);
1871 rx_csr &= ~MUSB_RXCSR_H_ERROR;
1872 musb_writew(epio, MUSB_RXCSR, rx_csr);
1874 } else if (rx_csr & MUSB_RXCSR_DATAERROR) {
1876 if (USB_ENDPOINT_XFER_ISOC != qh->type) {
1877 dev_dbg(musb->controller, "RX end %d NAK timeout\n", epnum);
1879 /* NOTE: NAKing is *NOT* an error, so we want to
1880 * continue. Except ... if there's a request for
1881 * another QH, use that instead of starving it.
1883 * Devices like Ethernet and serial adapters keep
1884 * reads posted at all times, which will starve
1885 * other devices without this logic.
1887 if (usb_pipebulk(urb->pipe)
1889 && !list_is_singular(&musb->in_bulk)) {
1890 musb_bulk_nak_timeout(musb, hw_ep, 1);
1893 musb_ep_select(mbase, epnum);
1894 rx_csr |= MUSB_RXCSR_H_WZC_BITS;
1895 rx_csr &= ~MUSB_RXCSR_DATAERROR;
1896 musb_writew(epio, MUSB_RXCSR, rx_csr);
1900 dev_dbg(musb->controller, "RX end %d ISO data error\n", epnum);
1901 /* packet error reported later */
1904 } else if (rx_csr & MUSB_RXCSR_INCOMPRX) {
1905 dev_dbg(musb->controller, "end %d high bandwidth incomplete ISO packet RX\n",
1910 /* faults abort the transfer */
1912 /* clean up dma and collect transfer count */
1913 if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) {
1914 dma->status = MUSB_DMA_STATUS_CORE_ABORT;
1915 musb->dma_controller->channel_abort(dma);
1916 xfer_len = dma->actual_len;
1918 musb_h_flush_rxfifo(hw_ep, MUSB_RXCSR_CLRDATATOG);
1919 musb_writeb(epio, MUSB_RXINTERVAL, 0);
1924 if (unlikely(dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY)) {
1925 /* SHOULD NEVER HAPPEN ... but at least DaVinci has done it */
1926 ERR("RX%d dma busy, csr %04x\n", epnum, rx_csr);
1930 /* thorough shutdown for now ... given more precise fault handling
1931 * and better queueing support, we might keep a DMA pipeline going
1932 * while processing this irq for earlier completions.
1935 /* FIXME this is _way_ too much in-line logic for Mentor DMA */
1936 if (!musb_dma_inventra(musb) && !musb_dma_ux500(musb) &&
1937 (rx_csr & MUSB_RXCSR_H_REQPKT)) {
1938 /* REVISIT this happened for a while on some short reads...
1939 * the cleanup still needs investigation... looks bad...
1940 * and also duplicates dma cleanup code above ... plus,
1941 * shouldn't this be the "half full" double buffer case?
1943 if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) {
1944 dma->status = MUSB_DMA_STATUS_CORE_ABORT;
1945 musb->dma_controller->channel_abort(dma);
1946 xfer_len = dma->actual_len;
1950 dev_dbg(musb->controller, "RXCSR%d %04x, reqpkt, len %zu%s\n", epnum, rx_csr,
1951 xfer_len, dma ? ", dma" : "");
1952 rx_csr &= ~MUSB_RXCSR_H_REQPKT;
1954 musb_ep_select(mbase, epnum);
1955 musb_writew(epio, MUSB_RXCSR,
1956 MUSB_RXCSR_H_WZC_BITS | rx_csr);
1959 if (dma && (rx_csr & MUSB_RXCSR_DMAENAB)) {
1960 xfer_len = dma->actual_len;
1962 val &= ~(MUSB_RXCSR_DMAENAB
1963 | MUSB_RXCSR_H_AUTOREQ
1964 | MUSB_RXCSR_AUTOCLEAR
1965 | MUSB_RXCSR_RXPKTRDY);
1966 musb_writew(hw_ep->regs, MUSB_RXCSR, val);
1968 if (musb_dma_inventra(musb) || musb_dma_ux500(musb) ||
1969 musb_dma_cppi41(musb)) {
1970 done = musb_rx_dma_inventra_cppi41(c, hw_ep, qh, urb, xfer_len);
1971 dev_dbg(hw_ep->musb->controller,
1972 "ep %d dma %s, rxcsr %04x, rxcount %d\n",
1973 epnum, done ? "off" : "reset",
1974 musb_readw(epio, MUSB_RXCSR),
1975 musb_readw(epio, MUSB_RXCOUNT));
1980 } else if (urb->status == -EINPROGRESS) {
1981 /* if no errors, be sure a packet is ready for unloading */
1982 if (unlikely(!(rx_csr & MUSB_RXCSR_RXPKTRDY))) {
1984 ERR("Rx interrupt with no errors or packet!\n");
1986 /* FIXME this is another "SHOULD NEVER HAPPEN" */
1989 /* do the proper sequence to abort the transfer */
1990 musb_ep_select(mbase, epnum);
1991 val &= ~MUSB_RXCSR_H_REQPKT;
1992 musb_writew(epio, MUSB_RXCSR, val);
1996 /* we are expecting IN packets */
1997 if ((musb_dma_inventra(musb) || musb_dma_ux500(musb) ||
1998 musb_dma_cppi41(musb)) && dma) {
1999 dev_dbg(hw_ep->musb->controller,
2000 "RX%d count %d, buffer 0x%llx len %d/%d\n",
2001 epnum, musb_readw(epio, MUSB_RXCOUNT),
2002 (unsigned long long) urb->transfer_dma
2003 + urb->actual_length,
2005 urb->transfer_buffer_length);
2007 if (musb_rx_dma_in_inventra_cppi41(c, hw_ep, qh, urb,
2011 dev_err(musb->controller, "error: rx_dma failed\n");
2015 unsigned int received_len;
2017 /* Unmap the buffer so that CPU can use it */
2018 usb_hcd_unmap_urb_for_dma(musb->hcd, urb);
2021 * We need to map sg if the transfer_buffer is
2024 if (!urb->transfer_buffer) {
2026 sg_miter_start(&qh->sg_miter, urb->sg, 1,
2031 if (!sg_miter_next(&qh->sg_miter)) {
2032 dev_err(musb->controller, "error: sg list empty\n");
2033 sg_miter_stop(&qh->sg_miter);
2038 urb->transfer_buffer = qh->sg_miter.addr;
2039 received_len = urb->actual_length;
2041 done = musb_host_packet_rx(musb, urb, epnum,
2043 /* Calculate the number of bytes received */
2044 received_len = urb->actual_length -
2046 qh->sg_miter.consumed = received_len;
2047 sg_miter_stop(&qh->sg_miter);
2049 done = musb_host_packet_rx(musb, urb,
2052 dev_dbg(musb->controller, "read %spacket\n", done ? "last " : "");
2057 urb->actual_length += xfer_len;
2058 qh->offset += xfer_len;
2063 if (urb->status == -EINPROGRESS)
2064 urb->status = status;
2065 musb_advance_schedule(musb, urb, hw_ep, USB_DIR_IN);
2069 /* schedule nodes correspond to peripheral endpoints, like an OHCI QH.
2070 * the software schedule associates multiple such nodes with a given
2071 * host side hardware endpoint + direction; scheduling may activate
2072 * that hardware endpoint.
2074 static int musb_schedule(
2081 int best_end, epnum;
2082 struct musb_hw_ep *hw_ep = NULL;
2083 struct list_head *head = NULL;
2086 struct urb *urb = next_urb(qh);
2088 /* use fixed hardware for control and bulk */
2089 if (qh->type == USB_ENDPOINT_XFER_CONTROL) {
2090 head = &musb->control;
2091 hw_ep = musb->control_ep;
2095 /* else, periodic transfers get muxed to other endpoints */
2098 * We know this qh hasn't been scheduled, so all we need to do
2099 * is choose which hardware endpoint to put it on ...
2101 * REVISIT what we really want here is a regular schedule tree
2102 * like e.g. OHCI uses.
2107 for (epnum = 1, hw_ep = musb->endpoints + 1;
2108 epnum < musb->nr_endpoints;
2112 if (musb_ep_get_qh(hw_ep, is_in) != NULL)
2115 if (hw_ep == musb->bulk_ep)
2119 diff = hw_ep->max_packet_sz_rx;
2121 diff = hw_ep->max_packet_sz_tx;
2122 diff -= (qh->maxpacket * qh->hb_mult);
2124 if (diff >= 0 && best_diff > diff) {
2127 * Mentor controller has a bug in that if we schedule
2128 * a BULK Tx transfer on an endpoint that had earlier
2129 * handled ISOC then the BULK transfer has to start on
2130 * a zero toggle. If the BULK transfer starts on a 1
2131 * toggle then this transfer will fail as the mentor
2132 * controller starts the Bulk transfer on a 0 toggle
2133 * irrespective of the programming of the toggle bits
2134 * in the TXCSR register. Check for this condition
2135 * while allocating the EP for a Tx Bulk transfer. If
2138 hw_ep = musb->endpoints + epnum;
2139 toggle = usb_gettoggle(urb->dev, qh->epnum, !is_in);
2140 txtype = (musb_readb(hw_ep->regs, MUSB_TXTYPE)
2142 if (!is_in && (qh->type == USB_ENDPOINT_XFER_BULK) &&
2143 toggle && (txtype == USB_ENDPOINT_XFER_ISOC))
2150 /* use bulk reserved ep1 if no other ep is free */
2151 if (best_end < 0 && qh->type == USB_ENDPOINT_XFER_BULK) {
2152 hw_ep = musb->bulk_ep;
2154 head = &musb->in_bulk;
2156 head = &musb->out_bulk;
2158 /* Enable bulk RX/TX NAK timeout scheme when bulk requests are
2159 * multiplexed. This scheme does not work in high speed to full
2160 * speed scenario as NAK interrupts are not coming from a
2161 * full speed device connected to a high speed device.
2162 * NAK timeout interval is 8 (128 uframe or 16ms) for HS and
2163 * 4 (8 frame or 8ms) for FS device.
2167 (USB_SPEED_HIGH == qh->dev->speed) ? 8 : 4;
2169 } else if (best_end < 0) {
2175 hw_ep = musb->endpoints + best_end;
2176 dev_dbg(musb->controller, "qh %p periodic slot %d\n", qh, best_end);
2179 idle = list_empty(head);
2180 list_add_tail(&qh->ring, head);
2184 qh->hep->hcpriv = qh;
2186 musb_start_urb(musb, is_in, qh);
2190 static int musb_urb_enqueue(
2191 struct usb_hcd *hcd,
2195 unsigned long flags;
2196 struct musb *musb = hcd_to_musb(hcd);
2197 struct usb_host_endpoint *hep = urb->ep;
2199 struct usb_endpoint_descriptor *epd = &hep->desc;
2204 /* host role must be active */
2205 if (!is_host_active(musb) || !musb->is_active)
2208 spin_lock_irqsave(&musb->lock, flags);
2209 ret = usb_hcd_link_urb_to_ep(hcd, urb);
2210 qh = ret ? NULL : hep->hcpriv;
2213 spin_unlock_irqrestore(&musb->lock, flags);
2215 /* DMA mapping was already done, if needed, and this urb is on
2216 * hep->urb_list now ... so we're done, unless hep wasn't yet
2217 * scheduled onto a live qh.
2219 * REVISIT best to keep hep->hcpriv valid until the endpoint gets
2220 * disabled, testing for empty qh->ring and avoiding qh setup costs
2221 * except for the first urb queued after a config change.
2226 /* Allocate and initialize qh, minimizing the work done each time
2227 * hw_ep gets reprogrammed, or with irqs blocked. Then schedule it.
2229 * REVISIT consider a dedicated qh kmem_cache, so it's harder
2230 * for bugs in other kernel code to break this driver...
2232 qh = kzalloc(sizeof *qh, mem_flags);
2234 spin_lock_irqsave(&musb->lock, flags);
2235 usb_hcd_unlink_urb_from_ep(hcd, urb);
2236 spin_unlock_irqrestore(&musb->lock, flags);
2242 INIT_LIST_HEAD(&qh->ring);
2245 qh->maxpacket = usb_endpoint_maxp(epd);
2246 qh->type = usb_endpoint_type(epd);
2248 /* Bits 11 & 12 of wMaxPacketSize encode high bandwidth multiplier.
2249 * Some musb cores don't support high bandwidth ISO transfers; and
2250 * we don't (yet!) support high bandwidth interrupt transfers.
2252 qh->hb_mult = 1 + ((qh->maxpacket >> 11) & 0x03);
2253 if (qh->hb_mult > 1) {
2254 int ok = (qh->type == USB_ENDPOINT_XFER_ISOC);
2257 ok = (usb_pipein(urb->pipe) && musb->hb_iso_rx)
2258 || (usb_pipeout(urb->pipe) && musb->hb_iso_tx);
2263 qh->maxpacket &= 0x7ff;
2266 qh->epnum = usb_endpoint_num(epd);
2268 /* NOTE: urb->dev->devnum is wrong during SET_ADDRESS */
2269 qh->addr_reg = (u8) usb_pipedevice(urb->pipe);
2271 /* precompute rxtype/txtype/type0 register */
2272 type_reg = (qh->type << 4) | qh->epnum;
2273 switch (urb->dev->speed) {
2277 case USB_SPEED_FULL:
2283 qh->type_reg = type_reg;
2285 /* Precompute RXINTERVAL/TXINTERVAL register */
2287 case USB_ENDPOINT_XFER_INT:
2289 * Full/low speeds use the linear encoding,
2290 * high speed uses the logarithmic encoding.
2292 if (urb->dev->speed <= USB_SPEED_FULL) {
2293 interval = max_t(u8, epd->bInterval, 1);
2297 case USB_ENDPOINT_XFER_ISOC:
2298 /* ISO always uses logarithmic encoding */
2299 interval = min_t(u8, epd->bInterval, 16);
2302 /* REVISIT we actually want to use NAK limits, hinting to the
2303 * transfer scheduling logic to try some other qh, e.g. try
2306 * interval = (USB_SPEED_HIGH == urb->dev->speed) ? 16 : 2;
2308 * The downside of disabling this is that transfer scheduling
2309 * gets VERY unfair for nonperiodic transfers; a misbehaving
2310 * peripheral could make that hurt. That's perfectly normal
2311 * for reads from network or serial adapters ... so we have
2312 * partial NAKlimit support for bulk RX.
2314 * The upside of disabling it is simpler transfer scheduling.
2318 qh->intv_reg = interval;
2320 /* precompute addressing for external hub/tt ports */
2321 if (musb->is_multipoint) {
2322 struct usb_device *parent = urb->dev->parent;
2324 if (parent != hcd->self.root_hub) {
2325 qh->h_addr_reg = (u8) parent->devnum;
2327 /* set up tt info if needed */
2329 qh->h_port_reg = (u8) urb->dev->ttport;
2330 if (urb->dev->tt->hub)
2332 (u8) urb->dev->tt->hub->devnum;
2333 if (urb->dev->tt->multi)
2334 qh->h_addr_reg |= 0x80;
2339 /* invariant: hep->hcpriv is null OR the qh that's already scheduled.
2340 * until we get real dma queues (with an entry for each urb/buffer),
2341 * we only have work to do in the former case.
2343 spin_lock_irqsave(&musb->lock, flags);
2344 if (hep->hcpriv || !next_urb(qh)) {
2345 /* some concurrent activity submitted another urb to hep...
2346 * odd, rare, error prone, but legal.
2352 ret = musb_schedule(musb, qh,
2353 epd->bEndpointAddress & USB_ENDPOINT_DIR_MASK);
2357 /* FIXME set urb->start_frame for iso/intr, it's tested in
2358 * musb_start_urb(), but otherwise only konicawc cares ...
2361 spin_unlock_irqrestore(&musb->lock, flags);
2365 spin_lock_irqsave(&musb->lock, flags);
2366 usb_hcd_unlink_urb_from_ep(hcd, urb);
2367 spin_unlock_irqrestore(&musb->lock, flags);
2375 * abort a transfer that's at the head of a hardware queue.
2376 * called with controller locked, irqs blocked
2377 * that hardware queue advances to the next transfer, unless prevented
2379 static int musb_cleanup_urb(struct urb *urb, struct musb_qh *qh)
2381 struct musb_hw_ep *ep = qh->hw_ep;
2382 struct musb *musb = ep->musb;
2383 void __iomem *epio = ep->regs;
2384 unsigned hw_end = ep->epnum;
2385 void __iomem *regs = ep->musb->mregs;
2386 int is_in = usb_pipein(urb->pipe);
2390 musb_ep_select(regs, hw_end);
2392 if (is_dma_capable()) {
2393 struct dma_channel *dma;
2395 dma = is_in ? ep->rx_channel : ep->tx_channel;
2397 status = ep->musb->dma_controller->channel_abort(dma);
2398 dev_dbg(musb->controller,
2399 "abort %cX%d DMA for urb %p --> %d\n",
2400 is_in ? 'R' : 'T', ep->epnum,
2402 urb->actual_length += dma->actual_len;
2406 /* turn off DMA requests, discard state, stop polling ... */
2407 if (ep->epnum && is_in) {
2408 /* giveback saves bulk toggle */
2409 csr = musb_h_flush_rxfifo(ep, 0);
2411 /* REVISIT we still get an irq; should likely clear the
2412 * endpoint's irq status here to avoid bogus irqs.
2413 * clearing that status is platform-specific...
2415 } else if (ep->epnum) {
2416 musb_h_tx_flush_fifo(ep);
2417 csr = musb_readw(epio, MUSB_TXCSR);
2418 csr &= ~(MUSB_TXCSR_AUTOSET
2419 | MUSB_TXCSR_DMAENAB
2420 | MUSB_TXCSR_H_RXSTALL
2421 | MUSB_TXCSR_H_NAKTIMEOUT
2422 | MUSB_TXCSR_H_ERROR
2423 | MUSB_TXCSR_TXPKTRDY);
2424 musb_writew(epio, MUSB_TXCSR, csr);
2425 /* REVISIT may need to clear FLUSHFIFO ... */
2426 musb_writew(epio, MUSB_TXCSR, csr);
2427 /* flush cpu writebuffer */
2428 csr = musb_readw(epio, MUSB_TXCSR);
2430 musb_h_ep0_flush_fifo(ep);
2433 musb_advance_schedule(ep->musb, urb, ep, is_in);
2437 static int musb_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
2439 struct musb *musb = hcd_to_musb(hcd);
2441 unsigned long flags;
2442 int is_in = usb_pipein(urb->pipe);
2445 dev_dbg(musb->controller, "urb=%p, dev%d ep%d%s\n", urb,
2446 usb_pipedevice(urb->pipe),
2447 usb_pipeendpoint(urb->pipe),
2448 is_in ? "in" : "out");
2450 spin_lock_irqsave(&musb->lock, flags);
2451 ret = usb_hcd_check_unlink_urb(hcd, urb, status);
2460 * Any URB not actively programmed into endpoint hardware can be
2461 * immediately given back; that's any URB not at the head of an
2462 * endpoint queue, unless someday we get real DMA queues. And even
2463 * if it's at the head, it might not be known to the hardware...
2465 * Otherwise abort current transfer, pending DMA, etc.; urb->status
2466 * has already been updated. This is a synchronous abort; it'd be
2467 * OK to hold off until after some IRQ, though.
2469 * NOTE: qh is invalid unless !list_empty(&hep->urb_list)
2472 || urb->urb_list.prev != &qh->hep->urb_list
2473 || musb_ep_get_qh(qh->hw_ep, is_in) != qh) {
2474 int ready = qh->is_ready;
2477 musb_giveback(musb, urb, 0);
2478 qh->is_ready = ready;
2480 /* If nothing else (usually musb_giveback) is using it
2481 * and its URB list has emptied, recycle this qh.
2483 if (ready && list_empty(&qh->hep->urb_list)) {
2484 qh->hep->hcpriv = NULL;
2485 list_del(&qh->ring);
2489 ret = musb_cleanup_urb(urb, qh);
2491 spin_unlock_irqrestore(&musb->lock, flags);
2495 /* disable an endpoint */
2497 musb_h_disable(struct usb_hcd *hcd, struct usb_host_endpoint *hep)
2499 u8 is_in = hep->desc.bEndpointAddress & USB_DIR_IN;
2500 unsigned long flags;
2501 struct musb *musb = hcd_to_musb(hcd);
2505 spin_lock_irqsave(&musb->lock, flags);
2511 /* NOTE: qh is invalid unless !list_empty(&hep->urb_list) */
2513 /* Kick the first URB off the hardware, if needed */
2515 if (musb_ep_get_qh(qh->hw_ep, is_in) == qh) {
2518 /* make software (then hardware) stop ASAP */
2520 urb->status = -ESHUTDOWN;
2523 musb_cleanup_urb(urb, qh);
2525 /* Then nuke all the others ... and advance the
2526 * queue on hw_ep (e.g. bulk ring) when we're done.
2528 while (!list_empty(&hep->urb_list)) {
2530 urb->status = -ESHUTDOWN;
2531 musb_advance_schedule(musb, urb, qh->hw_ep, is_in);
2534 /* Just empty the queue; the hardware is busy with
2535 * other transfers, and since !qh->is_ready nothing
2536 * will activate any of these as it advances.
2538 while (!list_empty(&hep->urb_list))
2539 musb_giveback(musb, next_urb(qh), -ESHUTDOWN);
2542 list_del(&qh->ring);
2546 spin_unlock_irqrestore(&musb->lock, flags);
2549 static int musb_h_get_frame_number(struct usb_hcd *hcd)
2551 struct musb *musb = hcd_to_musb(hcd);
2553 return musb_readw(musb->mregs, MUSB_FRAME);
2556 static int musb_h_start(struct usb_hcd *hcd)
2558 struct musb *musb = hcd_to_musb(hcd);
2560 /* NOTE: musb_start() is called when the hub driver turns
2561 * on port power, or when (OTG) peripheral starts.
2563 hcd->state = HC_STATE_RUNNING;
2564 musb->port1_status = 0;
2568 static void musb_h_stop(struct usb_hcd *hcd)
2570 musb_stop(hcd_to_musb(hcd));
2571 hcd->state = HC_STATE_HALT;
2574 static int musb_bus_suspend(struct usb_hcd *hcd)
2576 struct musb *musb = hcd_to_musb(hcd);
2579 musb_port_suspend(musb, true);
2581 if (!is_host_active(musb))
2584 switch (musb->xceiv->otg->state) {
2585 case OTG_STATE_A_SUSPEND:
2587 case OTG_STATE_A_WAIT_VRISE:
2588 /* ID could be grounded even if there's no device
2589 * on the other end of the cable. NOTE that the
2590 * A_WAIT_VRISE timers are messy with MUSB...
2592 devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
2593 if ((devctl & MUSB_DEVCTL_VBUS) == MUSB_DEVCTL_VBUS)
2594 musb->xceiv->otg->state = OTG_STATE_A_WAIT_BCON;
2600 if (musb->is_active) {
2601 WARNING("trying to suspend as %s while active\n",
2602 usb_otg_state_string(musb->xceiv->otg->state));
2608 static int musb_bus_resume(struct usb_hcd *hcd)
2610 struct musb *musb = hcd_to_musb(hcd);
2613 musb->config->host_port_deassert_reset_at_resume)
2614 musb_port_reset(musb, false);
2619 #ifndef CONFIG_MUSB_PIO_ONLY
2621 #define MUSB_USB_DMA_ALIGN 4
2623 struct musb_temp_buffer {
2625 void *old_xfer_buffer;
2629 static void musb_free_temp_buffer(struct urb *urb)
2631 enum dma_data_direction dir;
2632 struct musb_temp_buffer *temp;
2635 if (!(urb->transfer_flags & URB_ALIGNED_TEMP_BUFFER))
2638 dir = usb_urb_dir_in(urb) ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
2640 temp = container_of(urb->transfer_buffer, struct musb_temp_buffer,
2643 if (dir == DMA_FROM_DEVICE) {
2644 if (usb_pipeisoc(urb->pipe))
2645 length = urb->transfer_buffer_length;
2647 length = urb->actual_length;
2649 memcpy(temp->old_xfer_buffer, temp->data, length);
2651 urb->transfer_buffer = temp->old_xfer_buffer;
2652 kfree(temp->kmalloc_ptr);
2654 urb->transfer_flags &= ~URB_ALIGNED_TEMP_BUFFER;
2657 static int musb_alloc_temp_buffer(struct urb *urb, gfp_t mem_flags)
2659 enum dma_data_direction dir;
2660 struct musb_temp_buffer *temp;
2662 size_t kmalloc_size;
2664 if (urb->num_sgs || urb->sg ||
2665 urb->transfer_buffer_length == 0 ||
2666 !((uintptr_t)urb->transfer_buffer & (MUSB_USB_DMA_ALIGN - 1)))
2669 dir = usb_urb_dir_in(urb) ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
2671 /* Allocate a buffer with enough padding for alignment */
2672 kmalloc_size = urb->transfer_buffer_length +
2673 sizeof(struct musb_temp_buffer) + MUSB_USB_DMA_ALIGN - 1;
2675 kmalloc_ptr = kmalloc(kmalloc_size, mem_flags);
2679 /* Position our struct temp_buffer such that data is aligned */
2680 temp = PTR_ALIGN(kmalloc_ptr, MUSB_USB_DMA_ALIGN);
2683 temp->kmalloc_ptr = kmalloc_ptr;
2684 temp->old_xfer_buffer = urb->transfer_buffer;
2685 if (dir == DMA_TO_DEVICE)
2686 memcpy(temp->data, urb->transfer_buffer,
2687 urb->transfer_buffer_length);
2688 urb->transfer_buffer = temp->data;
2690 urb->transfer_flags |= URB_ALIGNED_TEMP_BUFFER;
2695 static int musb_map_urb_for_dma(struct usb_hcd *hcd, struct urb *urb,
2698 struct musb *musb = hcd_to_musb(hcd);
2702 * The DMA engine in RTL1.8 and above cannot handle
2703 * DMA addresses that are not aligned to a 4 byte boundary.
2704 * For such engine implemented (un)map_urb_for_dma hooks.
2705 * Do not use these hooks for RTL<1.8
2707 if (musb->hwvers < MUSB_HWVERS_1800)
2708 return usb_hcd_map_urb_for_dma(hcd, urb, mem_flags);
2710 ret = musb_alloc_temp_buffer(urb, mem_flags);
2714 ret = usb_hcd_map_urb_for_dma(hcd, urb, mem_flags);
2716 musb_free_temp_buffer(urb);
2721 static void musb_unmap_urb_for_dma(struct usb_hcd *hcd, struct urb *urb)
2723 struct musb *musb = hcd_to_musb(hcd);
2725 usb_hcd_unmap_urb_for_dma(hcd, urb);
2727 /* Do not use this hook for RTL<1.8 (see description above) */
2728 if (musb->hwvers < MUSB_HWVERS_1800)
2731 musb_free_temp_buffer(urb);
2733 #endif /* !CONFIG_MUSB_PIO_ONLY */
2735 static const struct hc_driver musb_hc_driver = {
2736 .description = "musb-hcd",
2737 .product_desc = "MUSB HDRC host driver",
2738 .hcd_priv_size = sizeof(struct musb *),
2739 .flags = HCD_USB2 | HCD_MEMORY,
2741 /* not using irq handler or reset hooks from usbcore, since
2742 * those must be shared with peripheral code for OTG configs
2745 .start = musb_h_start,
2746 .stop = musb_h_stop,
2748 .get_frame_number = musb_h_get_frame_number,
2750 .urb_enqueue = musb_urb_enqueue,
2751 .urb_dequeue = musb_urb_dequeue,
2752 .endpoint_disable = musb_h_disable,
2754 #ifndef CONFIG_MUSB_PIO_ONLY
2755 .map_urb_for_dma = musb_map_urb_for_dma,
2756 .unmap_urb_for_dma = musb_unmap_urb_for_dma,
2759 .hub_status_data = musb_hub_status_data,
2760 .hub_control = musb_hub_control,
2761 .bus_suspend = musb_bus_suspend,
2762 .bus_resume = musb_bus_resume,
2763 /* .start_port_reset = NULL, */
2764 /* .hub_irq_enable = NULL, */
2767 int musb_host_alloc(struct musb *musb)
2769 struct device *dev = musb->controller;
2771 /* usbcore sets dev->driver_data to hcd, and sometimes uses that... */
2772 musb->hcd = usb_create_hcd(&musb_hc_driver, dev, dev_name(dev));
2776 *musb->hcd->hcd_priv = (unsigned long) musb;
2777 musb->hcd->self.uses_pio_for_control = 1;
2778 musb->hcd->uses_new_polling = 1;
2779 musb->hcd->has_tt = 1;
2784 void musb_host_cleanup(struct musb *musb)
2786 if (musb->port_mode == MUSB_PORT_MODE_GADGET)
2788 usb_remove_hcd(musb->hcd);
2791 void musb_host_free(struct musb *musb)
2793 usb_put_hcd(musb->hcd);
2796 int musb_host_setup(struct musb *musb, int power_budget)
2799 struct usb_hcd *hcd = musb->hcd;
2801 MUSB_HST_MODE(musb);
2802 musb->xceiv->otg->default_a = 1;
2803 musb->xceiv->otg->state = OTG_STATE_A_IDLE;
2805 otg_set_host(musb->xceiv->otg, &hcd->self);
2806 hcd->self.otg_port = 1;
2807 musb->xceiv->otg->host = &hcd->self;
2808 hcd->power_budget = 2 * (power_budget ? : 250);
2810 ret = usb_add_hcd(hcd, 0, 0);
2814 device_wakeup_enable(hcd->self.controller);
2818 void musb_host_resume_root_hub(struct musb *musb)
2820 usb_hcd_resume_root_hub(musb->hcd);
2823 void musb_host_poke_root_hub(struct musb *musb)
2825 MUSB_HST_MODE(musb);
2826 if (musb->hcd->status_urb)
2827 usb_hcd_poll_rh_status(musb->hcd);
2829 usb_hcd_resume_root_hub(musb->hcd);