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",
145 static void musb_h_ep0_flush_fifo(struct musb_hw_ep *ep)
147 void __iomem *epio = ep->regs;
151 /* scrub any data left in the fifo */
153 csr = musb_readw(epio, MUSB_TXCSR);
154 if (!(csr & (MUSB_CSR0_TXPKTRDY | MUSB_CSR0_RXPKTRDY)))
156 musb_writew(epio, MUSB_TXCSR, MUSB_CSR0_FLUSHFIFO);
157 csr = musb_readw(epio, MUSB_TXCSR);
161 WARN(!retries, "Could not flush host TX%d fifo: csr: %04x\n",
164 /* and reset for the next transfer */
165 musb_writew(epio, MUSB_TXCSR, 0);
169 * Start transmit. Caller is responsible for locking shared resources.
170 * musb must be locked.
172 static inline void musb_h_tx_start(struct musb_hw_ep *ep)
176 /* NOTE: no locks here; caller should lock and select EP */
178 txcsr = musb_readw(ep->regs, MUSB_TXCSR);
179 txcsr |= MUSB_TXCSR_TXPKTRDY | MUSB_TXCSR_H_WZC_BITS;
180 musb_writew(ep->regs, MUSB_TXCSR, txcsr);
182 txcsr = MUSB_CSR0_H_SETUPPKT | MUSB_CSR0_TXPKTRDY;
183 musb_writew(ep->regs, MUSB_CSR0, txcsr);
188 static inline void musb_h_tx_dma_start(struct musb_hw_ep *ep)
192 /* NOTE: no locks here; caller should lock and select EP */
193 txcsr = musb_readw(ep->regs, MUSB_TXCSR);
194 txcsr |= MUSB_TXCSR_DMAENAB | MUSB_TXCSR_H_WZC_BITS;
195 if (is_cppi_enabled(ep->musb))
196 txcsr |= MUSB_TXCSR_DMAMODE;
197 musb_writew(ep->regs, MUSB_TXCSR, txcsr);
200 static void musb_ep_set_qh(struct musb_hw_ep *ep, int is_in, struct musb_qh *qh)
202 if (is_in != 0 || ep->is_shared_fifo)
204 if (is_in == 0 || ep->is_shared_fifo)
208 static struct musb_qh *musb_ep_get_qh(struct musb_hw_ep *ep, int is_in)
210 return is_in ? ep->in_qh : ep->out_qh;
214 * Start the URB at the front of an endpoint's queue
215 * end must be claimed from the caller.
217 * Context: controller locked, irqs blocked
220 musb_start_urb(struct musb *musb, int is_in, struct musb_qh *qh)
224 void __iomem *mbase = musb->mregs;
225 struct urb *urb = next_urb(qh);
226 void *buf = urb->transfer_buffer;
228 struct musb_hw_ep *hw_ep = qh->hw_ep;
229 unsigned pipe = urb->pipe;
230 u8 address = usb_pipedevice(pipe);
231 int epnum = hw_ep->epnum;
233 /* initialize software qh state */
237 /* gather right source of data */
239 case USB_ENDPOINT_XFER_CONTROL:
240 /* control transfers always start with SETUP */
242 musb->ep0_stage = MUSB_EP0_START;
243 buf = urb->setup_packet;
246 case USB_ENDPOINT_XFER_ISOC:
249 offset = urb->iso_frame_desc[0].offset;
250 len = urb->iso_frame_desc[0].length;
252 default: /* bulk, interrupt */
253 /* actual_length may be nonzero on retry paths */
254 buf = urb->transfer_buffer + urb->actual_length;
255 len = urb->transfer_buffer_length - urb->actual_length;
258 dev_dbg(musb->controller, "qh %p urb %p dev%d ep%d%s%s, hw_ep %d, %p/%d\n",
259 qh, urb, address, qh->epnum,
260 is_in ? "in" : "out",
261 ({char *s; switch (qh->type) {
262 case USB_ENDPOINT_XFER_CONTROL: s = ""; break;
263 case USB_ENDPOINT_XFER_BULK: s = "-bulk"; break;
264 case USB_ENDPOINT_XFER_ISOC: s = "-iso"; break;
265 default: s = "-intr"; break;
267 epnum, buf + offset, len);
269 /* Configure endpoint */
270 musb_ep_set_qh(hw_ep, is_in, qh);
271 musb_ep_program(musb, epnum, urb, !is_in, buf, offset, len);
273 /* transmit may have more work: start it when it is time */
277 /* determine if the time is right for a periodic transfer */
279 case USB_ENDPOINT_XFER_ISOC:
280 case USB_ENDPOINT_XFER_INT:
281 dev_dbg(musb->controller, "check whether there's still time for periodic Tx\n");
282 frame = musb_readw(mbase, MUSB_FRAME);
283 /* FIXME this doesn't implement that scheduling policy ...
284 * or handle framecounter wrapping
286 if (1) { /* Always assume URB_ISO_ASAP */
287 /* REVISIT the SOF irq handler shouldn't duplicate
288 * this code; and we don't init urb->start_frame...
293 qh->frame = urb->start_frame;
294 /* enable SOF interrupt so we can count down */
295 dev_dbg(musb->controller, "SOF for %d\n", epnum);
296 #if 1 /* ifndef CONFIG_ARCH_DAVINCI */
297 musb_writeb(mbase, MUSB_INTRUSBE, 0xff);
303 dev_dbg(musb->controller, "Start TX%d %s\n", epnum,
304 hw_ep->tx_channel ? "dma" : "pio");
306 if (!hw_ep->tx_channel)
307 musb_h_tx_start(hw_ep);
308 else if (is_cppi_enabled(musb) || tusb_dma_omap(musb))
309 musb_h_tx_dma_start(hw_ep);
313 /* Context: caller owns controller lock, IRQs are blocked */
314 static void musb_giveback(struct musb *musb, struct urb *urb, int status)
315 __releases(musb->lock)
316 __acquires(musb->lock)
318 dev_dbg(musb->controller,
319 "complete %p %pF (%d), dev%d ep%d%s, %d/%d\n",
320 urb, urb->complete, status,
321 usb_pipedevice(urb->pipe),
322 usb_pipeendpoint(urb->pipe),
323 usb_pipein(urb->pipe) ? "in" : "out",
324 urb->actual_length, urb->transfer_buffer_length
327 usb_hcd_unlink_urb_from_ep(musb->hcd, urb);
328 spin_unlock(&musb->lock);
329 usb_hcd_giveback_urb(musb->hcd, urb, status);
330 spin_lock(&musb->lock);
333 /* For bulk/interrupt endpoints only */
334 static inline void musb_save_toggle(struct musb_qh *qh, int is_in,
337 void __iomem *epio = qh->hw_ep->regs;
341 * FIXME: the current Mentor DMA code seems to have
342 * problems getting toggle correct.
346 csr = musb_readw(epio, MUSB_RXCSR) & MUSB_RXCSR_H_DATATOGGLE;
348 csr = musb_readw(epio, MUSB_TXCSR) & MUSB_TXCSR_H_DATATOGGLE;
350 usb_settoggle(urb->dev, qh->epnum, !is_in, csr ? 1 : 0);
354 * Advance this hardware endpoint's queue, completing the specified URB and
355 * advancing to either the next URB queued to that qh, or else invalidating
356 * that qh and advancing to the next qh scheduled after the current one.
358 * Context: caller owns controller lock, IRQs are blocked
360 static void musb_advance_schedule(struct musb *musb, struct urb *urb,
361 struct musb_hw_ep *hw_ep, int is_in)
363 struct musb_qh *qh = musb_ep_get_qh(hw_ep, is_in);
364 struct musb_hw_ep *ep = qh->hw_ep;
365 int ready = qh->is_ready;
368 status = (urb->status == -EINPROGRESS) ? 0 : urb->status;
370 /* save toggle eagerly, for paranoia */
372 case USB_ENDPOINT_XFER_BULK:
373 case USB_ENDPOINT_XFER_INT:
374 musb_save_toggle(qh, is_in, urb);
376 case USB_ENDPOINT_XFER_ISOC:
377 if (status == 0 && urb->error_count)
383 musb_giveback(musb, urb, status);
384 qh->is_ready = ready;
386 /* reclaim resources (and bandwidth) ASAP; deschedule it, and
387 * invalidate qh as soon as list_empty(&hep->urb_list)
389 if (list_empty(&qh->hep->urb_list)) {
390 struct list_head *head;
391 struct dma_controller *dma = musb->dma_controller;
395 if (ep->rx_channel) {
396 dma->channel_release(ep->rx_channel);
397 ep->rx_channel = NULL;
401 if (ep->tx_channel) {
402 dma->channel_release(ep->tx_channel);
403 ep->tx_channel = NULL;
407 /* Clobber old pointers to this qh */
408 musb_ep_set_qh(ep, is_in, NULL);
409 qh->hep->hcpriv = NULL;
413 case USB_ENDPOINT_XFER_CONTROL:
414 case USB_ENDPOINT_XFER_BULK:
415 /* fifo policy for these lists, except that NAKing
416 * should rotate a qh to the end (for fairness).
419 head = qh->ring.prev;
426 case USB_ENDPOINT_XFER_ISOC:
427 case USB_ENDPOINT_XFER_INT:
428 /* this is where periodic bandwidth should be
429 * de-allocated if it's tracked and allocated;
430 * and where we'd update the schedule tree...
438 if (qh != NULL && qh->is_ready) {
439 dev_dbg(musb->controller, "... next ep%d %cX urb %p\n",
440 hw_ep->epnum, is_in ? 'R' : 'T', next_urb(qh));
441 musb_start_urb(musb, is_in, qh);
445 static u16 musb_h_flush_rxfifo(struct musb_hw_ep *hw_ep, u16 csr)
447 /* we don't want fifo to fill itself again;
448 * ignore dma (various models),
449 * leave toggle alone (may not have been saved yet)
451 csr |= MUSB_RXCSR_FLUSHFIFO | MUSB_RXCSR_RXPKTRDY;
452 csr &= ~(MUSB_RXCSR_H_REQPKT
453 | MUSB_RXCSR_H_AUTOREQ
454 | MUSB_RXCSR_AUTOCLEAR);
456 /* write 2x to allow double buffering */
457 musb_writew(hw_ep->regs, MUSB_RXCSR, csr);
458 musb_writew(hw_ep->regs, MUSB_RXCSR, csr);
460 /* flush writebuffer */
461 return musb_readw(hw_ep->regs, MUSB_RXCSR);
465 * PIO RX for a packet (or part of it).
468 musb_host_packet_rx(struct musb *musb, struct urb *urb, u8 epnum, u8 iso_err)
476 struct musb_hw_ep *hw_ep = musb->endpoints + epnum;
477 void __iomem *epio = hw_ep->regs;
478 struct musb_qh *qh = hw_ep->in_qh;
479 int pipe = urb->pipe;
480 void *buffer = urb->transfer_buffer;
482 /* musb_ep_select(mbase, epnum); */
483 rx_count = musb_readw(epio, MUSB_RXCOUNT);
484 dev_dbg(musb->controller, "RX%d count %d, buffer %p len %d/%d\n", epnum, rx_count,
485 urb->transfer_buffer, qh->offset,
486 urb->transfer_buffer_length);
489 if (usb_pipeisoc(pipe)) {
491 struct usb_iso_packet_descriptor *d;
498 d = urb->iso_frame_desc + qh->iso_idx;
499 buf = buffer + d->offset;
501 if (rx_count > length) {
506 dev_dbg(musb->controller, "** OVERFLOW %d into %d\n", rx_count, length);
510 urb->actual_length += length;
511 d->actual_length = length;
515 /* see if we are done */
516 done = (++qh->iso_idx >= urb->number_of_packets);
519 buf = buffer + qh->offset;
520 length = urb->transfer_buffer_length - qh->offset;
521 if (rx_count > length) {
522 if (urb->status == -EINPROGRESS)
523 urb->status = -EOVERFLOW;
524 dev_dbg(musb->controller, "** OVERFLOW %d into %d\n", rx_count, length);
528 urb->actual_length += length;
529 qh->offset += length;
531 /* see if we are done */
532 done = (urb->actual_length == urb->transfer_buffer_length)
533 || (rx_count < qh->maxpacket)
534 || (urb->status != -EINPROGRESS);
536 && (urb->status == -EINPROGRESS)
537 && (urb->transfer_flags & URB_SHORT_NOT_OK)
538 && (urb->actual_length
539 < urb->transfer_buffer_length))
540 urb->status = -EREMOTEIO;
543 musb_read_fifo(hw_ep, length, buf);
545 csr = musb_readw(epio, MUSB_RXCSR);
546 csr |= MUSB_RXCSR_H_WZC_BITS;
547 if (unlikely(do_flush))
548 musb_h_flush_rxfifo(hw_ep, csr);
550 /* REVISIT this assumes AUTOCLEAR is never set */
551 csr &= ~(MUSB_RXCSR_RXPKTRDY | MUSB_RXCSR_H_REQPKT);
553 csr |= MUSB_RXCSR_H_REQPKT;
554 musb_writew(epio, MUSB_RXCSR, csr);
560 /* we don't always need to reinit a given side of an endpoint...
561 * when we do, use tx/rx reinit routine and then construct a new CSR
562 * to address data toggle, NYET, and DMA or PIO.
564 * it's possible that driver bugs (especially for DMA) or aborting a
565 * transfer might have left the endpoint busier than it should be.
566 * the busy/not-empty tests are basically paranoia.
569 musb_rx_reinit(struct musb *musb, struct musb_qh *qh, u8 epnum)
571 struct musb_hw_ep *ep = musb->endpoints + epnum;
574 /* NOTE: we know the "rx" fifo reinit never triggers for ep0.
575 * That always uses tx_reinit since ep0 repurposes TX register
576 * offsets; the initial SETUP packet is also a kind of OUT.
579 /* if programmed for Tx, put it in RX mode */
580 if (ep->is_shared_fifo) {
581 csr = musb_readw(ep->regs, MUSB_TXCSR);
582 if (csr & MUSB_TXCSR_MODE) {
583 musb_h_tx_flush_fifo(ep);
584 csr = musb_readw(ep->regs, MUSB_TXCSR);
585 musb_writew(ep->regs, MUSB_TXCSR,
586 csr | MUSB_TXCSR_FRCDATATOG);
590 * Clear the MODE bit (and everything else) to enable Rx.
591 * NOTE: we mustn't clear the DMAMODE bit before DMAENAB.
593 if (csr & MUSB_TXCSR_DMAMODE)
594 musb_writew(ep->regs, MUSB_TXCSR, MUSB_TXCSR_DMAMODE);
595 musb_writew(ep->regs, MUSB_TXCSR, 0);
597 /* scrub all previous state, clearing toggle */
599 csr = musb_readw(ep->regs, MUSB_RXCSR);
600 if (csr & MUSB_RXCSR_RXPKTRDY)
601 WARNING("rx%d, packet/%d ready?\n", ep->epnum,
602 musb_readw(ep->regs, MUSB_RXCOUNT));
604 musb_h_flush_rxfifo(ep, MUSB_RXCSR_CLRDATATOG);
606 /* target addr and (for multipoint) hub addr/port */
607 if (musb->is_multipoint) {
608 musb_write_rxfunaddr(musb, epnum, qh->addr_reg);
609 musb_write_rxhubaddr(musb, epnum, qh->h_addr_reg);
610 musb_write_rxhubport(musb, epnum, qh->h_port_reg);
612 musb_writeb(musb->mregs, MUSB_FADDR, qh->addr_reg);
614 /* protocol/endpoint, interval/NAKlimit, i/o size */
615 musb_writeb(ep->regs, MUSB_RXTYPE, qh->type_reg);
616 musb_writeb(ep->regs, MUSB_RXINTERVAL, qh->intv_reg);
617 /* NOTE: bulk combining rewrites high bits of maxpacket */
618 /* Set RXMAXP with the FIFO size of the endpoint
619 * to disable double buffer mode.
621 if (musb->double_buffer_not_ok)
622 musb_writew(ep->regs, MUSB_RXMAXP, ep->max_packet_sz_rx);
624 musb_writew(ep->regs, MUSB_RXMAXP,
625 qh->maxpacket | ((qh->hb_mult - 1) << 11));
630 static int musb_tx_dma_set_mode_mentor(struct dma_controller *dma,
631 struct musb_hw_ep *hw_ep, struct musb_qh *qh,
632 struct urb *urb, u32 offset,
633 u32 *length, u8 *mode)
635 struct dma_channel *channel = hw_ep->tx_channel;
636 void __iomem *epio = hw_ep->regs;
637 u16 pkt_size = qh->maxpacket;
640 if (*length > channel->max_len)
641 *length = channel->max_len;
643 csr = musb_readw(epio, MUSB_TXCSR);
644 if (*length > pkt_size) {
646 csr |= MUSB_TXCSR_DMAMODE | MUSB_TXCSR_DMAENAB;
647 /* autoset shouldn't be set in high bandwidth */
649 * Enable Autoset according to table
651 * bulk_split hb_mult Autoset_Enable
653 * 0 >1 No(High BW ISO)
657 if (qh->hb_mult == 1 || (qh->hb_mult > 1 &&
658 can_bulk_split(hw_ep->musb, qh->type)))
659 csr |= MUSB_TXCSR_AUTOSET;
662 csr &= ~(MUSB_TXCSR_AUTOSET | MUSB_TXCSR_DMAMODE);
663 csr |= MUSB_TXCSR_DMAENAB; /* against programmer's guide */
665 channel->desired_mode = *mode;
666 musb_writew(epio, MUSB_TXCSR, csr);
671 static int musb_tx_dma_set_mode_cppi_tusb(struct dma_controller *dma,
672 struct musb_hw_ep *hw_ep,
679 struct dma_channel *channel = hw_ep->tx_channel;
681 if (!is_cppi_enabled(hw_ep->musb) && !tusb_dma_omap(hw_ep->musb))
684 channel->actual_len = 0;
687 * TX uses "RNDIS" mode automatically but needs help
688 * to identify the zero-length-final-packet case.
690 *mode = (urb->transfer_flags & URB_ZERO_PACKET) ? 1 : 0;
695 static bool musb_tx_dma_program(struct dma_controller *dma,
696 struct musb_hw_ep *hw_ep, struct musb_qh *qh,
697 struct urb *urb, u32 offset, u32 length)
699 struct dma_channel *channel = hw_ep->tx_channel;
700 u16 pkt_size = qh->maxpacket;
704 if (musb_dma_inventra(hw_ep->musb) || musb_dma_ux500(hw_ep->musb))
705 res = musb_tx_dma_set_mode_mentor(dma, hw_ep, qh, urb,
706 offset, &length, &mode);
708 res = musb_tx_dma_set_mode_cppi_tusb(dma, hw_ep, qh, urb,
709 offset, &length, &mode);
713 qh->segsize = length;
716 * Ensure the data reaches to main memory before starting
721 if (!dma->channel_program(channel, pkt_size, mode,
722 urb->transfer_dma + offset, length)) {
723 void __iomem *epio = hw_ep->regs;
726 dma->channel_release(channel);
727 hw_ep->tx_channel = NULL;
729 csr = musb_readw(epio, MUSB_TXCSR);
730 csr &= ~(MUSB_TXCSR_AUTOSET | MUSB_TXCSR_DMAENAB);
731 musb_writew(epio, MUSB_TXCSR, csr | MUSB_TXCSR_H_WZC_BITS);
738 * Program an HDRC endpoint as per the given URB
739 * Context: irqs blocked, controller lock held
741 static void musb_ep_program(struct musb *musb, u8 epnum,
742 struct urb *urb, int is_out,
743 u8 *buf, u32 offset, u32 len)
745 struct dma_controller *dma_controller;
746 struct dma_channel *dma_channel;
748 void __iomem *mbase = musb->mregs;
749 struct musb_hw_ep *hw_ep = musb->endpoints + epnum;
750 void __iomem *epio = hw_ep->regs;
751 struct musb_qh *qh = musb_ep_get_qh(hw_ep, !is_out);
752 u16 packet_sz = qh->maxpacket;
756 dev_dbg(musb->controller, "%s hw%d urb %p spd%d dev%d ep%d%s "
757 "h_addr%02x h_port%02x bytes %d\n",
758 is_out ? "-->" : "<--",
759 epnum, urb, urb->dev->speed,
760 qh->addr_reg, qh->epnum, is_out ? "out" : "in",
761 qh->h_addr_reg, qh->h_port_reg,
764 musb_ep_select(mbase, epnum);
766 if (is_out && !len) {
768 csr = musb_readw(epio, MUSB_TXCSR);
769 csr &= ~MUSB_TXCSR_DMAENAB;
770 musb_writew(epio, MUSB_TXCSR, csr);
771 hw_ep->tx_channel = NULL;
774 /* candidate for DMA? */
775 dma_controller = musb->dma_controller;
776 if (use_dma && is_dma_capable() && epnum && dma_controller) {
777 dma_channel = is_out ? hw_ep->tx_channel : hw_ep->rx_channel;
779 dma_channel = dma_controller->channel_alloc(
780 dma_controller, hw_ep, is_out);
782 hw_ep->tx_channel = dma_channel;
784 hw_ep->rx_channel = dma_channel;
789 /* make sure we clear DMAEnab, autoSet bits from previous run */
791 /* OUT/transmit/EP0 or IN/receive? */
797 csr = musb_readw(epio, MUSB_TXCSR);
799 /* disable interrupt in case we flush */
800 int_txe = musb->intrtxe;
801 musb_writew(mbase, MUSB_INTRTXE, int_txe & ~(1 << epnum));
803 /* general endpoint setup */
805 /* flush all old state, set default */
807 * We could be flushing valid
808 * packets in double buffering
811 if (!hw_ep->tx_double_buffered)
812 musb_h_tx_flush_fifo(hw_ep);
815 * We must not clear the DMAMODE bit before or in
816 * the same cycle with the DMAENAB bit, so we clear
817 * the latter first...
819 csr &= ~(MUSB_TXCSR_H_NAKTIMEOUT
822 | MUSB_TXCSR_FRCDATATOG
823 | MUSB_TXCSR_H_RXSTALL
825 | MUSB_TXCSR_TXPKTRDY
827 csr |= MUSB_TXCSR_MODE;
829 if (!hw_ep->tx_double_buffered) {
830 if (usb_gettoggle(urb->dev, qh->epnum, 1))
831 csr |= MUSB_TXCSR_H_WR_DATATOGGLE
832 | MUSB_TXCSR_H_DATATOGGLE;
834 csr |= MUSB_TXCSR_CLRDATATOG;
837 musb_writew(epio, MUSB_TXCSR, csr);
838 /* REVISIT may need to clear FLUSHFIFO ... */
839 csr &= ~MUSB_TXCSR_DMAMODE;
840 musb_writew(epio, MUSB_TXCSR, csr);
841 csr = musb_readw(epio, MUSB_TXCSR);
843 /* endpoint 0: just flush */
844 musb_h_ep0_flush_fifo(hw_ep);
847 /* target addr and (for multipoint) hub addr/port */
848 if (musb->is_multipoint) {
849 musb_write_txfunaddr(musb, epnum, qh->addr_reg);
850 musb_write_txhubaddr(musb, epnum, qh->h_addr_reg);
851 musb_write_txhubport(musb, epnum, qh->h_port_reg);
852 /* FIXME if !epnum, do the same for RX ... */
854 musb_writeb(mbase, MUSB_FADDR, qh->addr_reg);
856 /* protocol/endpoint/interval/NAKlimit */
858 musb_writeb(epio, MUSB_TXTYPE, qh->type_reg);
859 if (musb->double_buffer_not_ok) {
860 musb_writew(epio, MUSB_TXMAXP,
861 hw_ep->max_packet_sz_tx);
862 } else if (can_bulk_split(musb, qh->type)) {
863 qh->hb_mult = hw_ep->max_packet_sz_tx
865 musb_writew(epio, MUSB_TXMAXP, packet_sz
866 | ((qh->hb_mult) - 1) << 11);
868 musb_writew(epio, MUSB_TXMAXP,
870 ((qh->hb_mult - 1) << 11));
872 musb_writeb(epio, MUSB_TXINTERVAL, qh->intv_reg);
874 musb_writeb(epio, MUSB_NAKLIMIT0, qh->intv_reg);
875 if (musb->is_multipoint)
876 musb_writeb(epio, MUSB_TYPE0,
880 if (can_bulk_split(musb, qh->type))
881 load_count = min((u32) hw_ep->max_packet_sz_tx,
884 load_count = min((u32) packet_sz, len);
886 if (dma_channel && musb_tx_dma_program(dma_controller,
887 hw_ep, qh, urb, offset, len))
891 /* PIO to load FIFO */
892 qh->segsize = load_count;
894 sg_miter_start(&qh->sg_miter, urb->sg, 1,
897 if (!sg_miter_next(&qh->sg_miter)) {
898 dev_err(musb->controller,
901 sg_miter_stop(&qh->sg_miter);
904 buf = qh->sg_miter.addr + urb->sg->offset +
906 load_count = min_t(u32, load_count,
907 qh->sg_miter.length);
908 musb_write_fifo(hw_ep, load_count, buf);
909 qh->sg_miter.consumed = load_count;
910 sg_miter_stop(&qh->sg_miter);
912 musb_write_fifo(hw_ep, load_count, buf);
915 /* re-enable interrupt */
916 musb_writew(mbase, MUSB_INTRTXE, int_txe);
922 if (hw_ep->rx_reinit) {
923 musb_rx_reinit(musb, qh, epnum);
925 /* init new state: toggle and NYET, maybe DMA later */
926 if (usb_gettoggle(urb->dev, qh->epnum, 0))
927 csr = MUSB_RXCSR_H_WR_DATATOGGLE
928 | MUSB_RXCSR_H_DATATOGGLE;
931 if (qh->type == USB_ENDPOINT_XFER_INT)
932 csr |= MUSB_RXCSR_DISNYET;
935 csr = musb_readw(hw_ep->regs, MUSB_RXCSR);
937 if (csr & (MUSB_RXCSR_RXPKTRDY
939 | MUSB_RXCSR_H_REQPKT))
940 ERR("broken !rx_reinit, ep%d csr %04x\n",
943 /* scrub any stale state, leaving toggle alone */
944 csr &= MUSB_RXCSR_DISNYET;
947 /* kick things off */
949 if ((is_cppi_enabled(musb) || tusb_dma_omap(musb)) && dma_channel) {
950 /* Candidate for DMA */
951 dma_channel->actual_len = 0L;
954 /* AUTOREQ is in a DMA register */
955 musb_writew(hw_ep->regs, MUSB_RXCSR, csr);
956 csr = musb_readw(hw_ep->regs, MUSB_RXCSR);
959 * Unless caller treats short RX transfers as
960 * errors, we dare not queue multiple transfers.
962 dma_ok = dma_controller->channel_program(dma_channel,
963 packet_sz, !(urb->transfer_flags &
965 urb->transfer_dma + offset,
968 dma_controller->channel_release(dma_channel);
969 hw_ep->rx_channel = dma_channel = NULL;
971 csr |= MUSB_RXCSR_DMAENAB;
974 csr |= MUSB_RXCSR_H_REQPKT;
975 dev_dbg(musb->controller, "RXCSR%d := %04x\n", epnum, csr);
976 musb_writew(hw_ep->regs, MUSB_RXCSR, csr);
977 csr = musb_readw(hw_ep->regs, MUSB_RXCSR);
981 /* Schedule next QH from musb->in_bulk/out_bulk and move the current qh to
982 * the end; avoids starvation for other endpoints.
984 static void musb_bulk_nak_timeout(struct musb *musb, struct musb_hw_ep *ep,
987 struct dma_channel *dma;
989 void __iomem *mbase = musb->mregs;
990 void __iomem *epio = ep->regs;
991 struct musb_qh *cur_qh, *next_qh;
994 musb_ep_select(mbase, ep->epnum);
996 dma = is_dma_capable() ? ep->rx_channel : NULL;
999 * Need to stop the transaction by clearing REQPKT first
1000 * then the NAK Timeout bit ref MUSBMHDRC USB 2.0 HIGH-SPEED
1001 * DUAL-ROLE CONTROLLER Programmer's Guide, section 9.2.2
1003 rx_csr = musb_readw(epio, MUSB_RXCSR);
1004 rx_csr |= MUSB_RXCSR_H_WZC_BITS;
1005 rx_csr &= ~MUSB_RXCSR_H_REQPKT;
1006 musb_writew(epio, MUSB_RXCSR, rx_csr);
1007 rx_csr &= ~MUSB_RXCSR_DATAERROR;
1008 musb_writew(epio, MUSB_RXCSR, rx_csr);
1010 cur_qh = first_qh(&musb->in_bulk);
1012 dma = is_dma_capable() ? ep->tx_channel : NULL;
1014 /* clear nak timeout bit */
1015 tx_csr = musb_readw(epio, MUSB_TXCSR);
1016 tx_csr |= MUSB_TXCSR_H_WZC_BITS;
1017 tx_csr &= ~MUSB_TXCSR_H_NAKTIMEOUT;
1018 musb_writew(epio, MUSB_TXCSR, tx_csr);
1020 cur_qh = first_qh(&musb->out_bulk);
1023 urb = next_urb(cur_qh);
1024 if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) {
1025 dma->status = MUSB_DMA_STATUS_CORE_ABORT;
1026 musb->dma_controller->channel_abort(dma);
1027 urb->actual_length += dma->actual_len;
1028 dma->actual_len = 0L;
1030 musb_save_toggle(cur_qh, is_in, urb);
1033 /* move cur_qh to end of queue */
1034 list_move_tail(&cur_qh->ring, &musb->in_bulk);
1036 /* get the next qh from musb->in_bulk */
1037 next_qh = first_qh(&musb->in_bulk);
1039 /* set rx_reinit and schedule the next qh */
1042 /* move cur_qh to end of queue */
1043 list_move_tail(&cur_qh->ring, &musb->out_bulk);
1045 /* get the next qh from musb->out_bulk */
1046 next_qh = first_qh(&musb->out_bulk);
1048 /* set tx_reinit and schedule the next qh */
1053 musb_start_urb(musb, is_in, next_qh);
1058 * Service the default endpoint (ep0) as host.
1059 * Return true until it's time to start the status stage.
1061 static bool musb_h_ep0_continue(struct musb *musb, u16 len, struct urb *urb)
1064 u8 *fifo_dest = NULL;
1066 struct musb_hw_ep *hw_ep = musb->control_ep;
1067 struct musb_qh *qh = hw_ep->in_qh;
1068 struct usb_ctrlrequest *request;
1070 switch (musb->ep0_stage) {
1072 fifo_dest = urb->transfer_buffer + urb->actual_length;
1073 fifo_count = min_t(size_t, len, urb->transfer_buffer_length -
1074 urb->actual_length);
1075 if (fifo_count < len)
1076 urb->status = -EOVERFLOW;
1078 musb_read_fifo(hw_ep, fifo_count, fifo_dest);
1080 urb->actual_length += fifo_count;
1081 if (len < qh->maxpacket) {
1082 /* always terminate on short read; it's
1083 * rarely reported as an error.
1085 } else if (urb->actual_length <
1086 urb->transfer_buffer_length)
1089 case MUSB_EP0_START:
1090 request = (struct usb_ctrlrequest *) urb->setup_packet;
1092 if (!request->wLength) {
1093 dev_dbg(musb->controller, "start no-DATA\n");
1095 } else if (request->bRequestType & USB_DIR_IN) {
1096 dev_dbg(musb->controller, "start IN-DATA\n");
1097 musb->ep0_stage = MUSB_EP0_IN;
1101 dev_dbg(musb->controller, "start OUT-DATA\n");
1102 musb->ep0_stage = MUSB_EP0_OUT;
1107 fifo_count = min_t(size_t, qh->maxpacket,
1108 urb->transfer_buffer_length -
1109 urb->actual_length);
1111 fifo_dest = (u8 *) (urb->transfer_buffer
1112 + urb->actual_length);
1113 dev_dbg(musb->controller, "Sending %d byte%s to ep0 fifo %p\n",
1115 (fifo_count == 1) ? "" : "s",
1117 musb_write_fifo(hw_ep, fifo_count, fifo_dest);
1119 urb->actual_length += fifo_count;
1124 ERR("bogus ep0 stage %d\n", musb->ep0_stage);
1132 * Handle default endpoint interrupt as host. Only called in IRQ time
1133 * from musb_interrupt().
1135 * called with controller irqlocked
1137 irqreturn_t musb_h_ep0_irq(struct musb *musb)
1142 void __iomem *mbase = musb->mregs;
1143 struct musb_hw_ep *hw_ep = musb->control_ep;
1144 void __iomem *epio = hw_ep->regs;
1145 struct musb_qh *qh = hw_ep->in_qh;
1146 bool complete = false;
1147 irqreturn_t retval = IRQ_NONE;
1149 /* ep0 only has one queue, "in" */
1152 musb_ep_select(mbase, 0);
1153 csr = musb_readw(epio, MUSB_CSR0);
1154 len = (csr & MUSB_CSR0_RXPKTRDY)
1155 ? musb_readb(epio, MUSB_COUNT0)
1158 dev_dbg(musb->controller, "<== csr0 %04x, qh %p, count %d, urb %p, stage %d\n",
1159 csr, qh, len, urb, musb->ep0_stage);
1161 /* if we just did status stage, we are done */
1162 if (MUSB_EP0_STATUS == musb->ep0_stage) {
1163 retval = IRQ_HANDLED;
1167 /* prepare status */
1168 if (csr & MUSB_CSR0_H_RXSTALL) {
1169 dev_dbg(musb->controller, "STALLING ENDPOINT\n");
1172 } else if (csr & MUSB_CSR0_H_ERROR) {
1173 dev_dbg(musb->controller, "no response, csr0 %04x\n", csr);
1176 } else if (csr & MUSB_CSR0_H_NAKTIMEOUT) {
1177 dev_dbg(musb->controller, "control NAK timeout\n");
1179 /* NOTE: this code path would be a good place to PAUSE a
1180 * control transfer, if another one is queued, so that
1181 * ep0 is more likely to stay busy. That's already done
1182 * for bulk RX transfers.
1184 * if (qh->ring.next != &musb->control), then
1185 * we have a candidate... NAKing is *NOT* an error
1187 musb_writew(epio, MUSB_CSR0, 0);
1188 retval = IRQ_HANDLED;
1192 dev_dbg(musb->controller, "aborting\n");
1193 retval = IRQ_HANDLED;
1195 urb->status = status;
1198 /* use the proper sequence to abort the transfer */
1199 if (csr & MUSB_CSR0_H_REQPKT) {
1200 csr &= ~MUSB_CSR0_H_REQPKT;
1201 musb_writew(epio, MUSB_CSR0, csr);
1202 csr &= ~MUSB_CSR0_H_NAKTIMEOUT;
1203 musb_writew(epio, MUSB_CSR0, csr);
1205 musb_h_ep0_flush_fifo(hw_ep);
1208 musb_writeb(epio, MUSB_NAKLIMIT0, 0);
1211 musb_writew(epio, MUSB_CSR0, 0);
1214 if (unlikely(!urb)) {
1215 /* stop endpoint since we have no place for its data, this
1216 * SHOULD NEVER HAPPEN! */
1217 ERR("no URB for end 0\n");
1219 musb_h_ep0_flush_fifo(hw_ep);
1224 /* call common logic and prepare response */
1225 if (musb_h_ep0_continue(musb, len, urb)) {
1226 /* more packets required */
1227 csr = (MUSB_EP0_IN == musb->ep0_stage)
1228 ? MUSB_CSR0_H_REQPKT : MUSB_CSR0_TXPKTRDY;
1230 /* data transfer complete; perform status phase */
1231 if (usb_pipeout(urb->pipe)
1232 || !urb->transfer_buffer_length)
1233 csr = MUSB_CSR0_H_STATUSPKT
1234 | MUSB_CSR0_H_REQPKT;
1236 csr = MUSB_CSR0_H_STATUSPKT
1237 | MUSB_CSR0_TXPKTRDY;
1239 /* disable ping token in status phase */
1240 csr |= MUSB_CSR0_H_DIS_PING;
1242 /* flag status stage */
1243 musb->ep0_stage = MUSB_EP0_STATUS;
1245 dev_dbg(musb->controller, "ep0 STATUS, csr %04x\n", csr);
1248 musb_writew(epio, MUSB_CSR0, csr);
1249 retval = IRQ_HANDLED;
1251 musb->ep0_stage = MUSB_EP0_IDLE;
1253 /* call completion handler if done */
1255 musb_advance_schedule(musb, urb, hw_ep, 1);
1261 #ifdef CONFIG_USB_INVENTRA_DMA
1263 /* Host side TX (OUT) using Mentor DMA works as follows:
1265 - if queue was empty, Program Endpoint
1266 - ... which starts DMA to fifo in mode 1 or 0
1268 DMA Isr (transfer complete) -> TxAvail()
1269 - Stop DMA (~DmaEnab) (<--- Alert ... currently happens
1270 only in musb_cleanup_urb)
1271 - TxPktRdy has to be set in mode 0 or for
1272 short packets in mode 1.
1277 /* Service a Tx-Available or dma completion irq for the endpoint */
1278 void musb_host_tx(struct musb *musb, u8 epnum)
1285 struct musb_hw_ep *hw_ep = musb->endpoints + epnum;
1286 void __iomem *epio = hw_ep->regs;
1287 struct musb_qh *qh = hw_ep->out_qh;
1288 struct urb *urb = next_urb(qh);
1290 void __iomem *mbase = musb->mregs;
1291 struct dma_channel *dma;
1292 bool transfer_pending = false;
1294 musb_ep_select(mbase, epnum);
1295 tx_csr = musb_readw(epio, MUSB_TXCSR);
1297 /* with CPPI, DMA sometimes triggers "extra" irqs */
1299 dev_dbg(musb->controller, "extra TX%d ready, csr %04x\n", epnum, tx_csr);
1304 dma = is_dma_capable() ? hw_ep->tx_channel : NULL;
1305 dev_dbg(musb->controller, "OUT/TX%d end, csr %04x%s\n", epnum, tx_csr,
1306 dma ? ", dma" : "");
1308 /* check for errors */
1309 if (tx_csr & MUSB_TXCSR_H_RXSTALL) {
1310 /* dma was disabled, fifo flushed */
1311 dev_dbg(musb->controller, "TX end %d stall\n", epnum);
1313 /* stall; record URB status */
1316 } else if (tx_csr & MUSB_TXCSR_H_ERROR) {
1317 /* (NON-ISO) dma was disabled, fifo flushed */
1318 dev_dbg(musb->controller, "TX 3strikes on ep=%d\n", epnum);
1320 status = -ETIMEDOUT;
1322 } else if (tx_csr & MUSB_TXCSR_H_NAKTIMEOUT) {
1323 if (USB_ENDPOINT_XFER_BULK == qh->type && qh->mux == 1
1324 && !list_is_singular(&musb->out_bulk)) {
1325 dev_dbg(musb->controller,
1326 "NAK timeout on TX%d ep\n", epnum);
1327 musb_bulk_nak_timeout(musb, hw_ep, 0);
1329 dev_dbg(musb->controller,
1330 "TX end=%d device not responding\n", epnum);
1331 /* NOTE: this code path would be a good place to PAUSE a
1332 * transfer, if there's some other (nonperiodic) tx urb
1333 * that could use this fifo. (dma complicates it...)
1334 * That's already done for bulk RX transfers.
1336 * if (bulk && qh->ring.next != &musb->out_bulk), then
1337 * we have a candidate... NAKing is *NOT* an error
1339 musb_ep_select(mbase, epnum);
1340 musb_writew(epio, MUSB_TXCSR,
1341 MUSB_TXCSR_H_WZC_BITS
1342 | MUSB_TXCSR_TXPKTRDY);
1349 if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) {
1350 dma->status = MUSB_DMA_STATUS_CORE_ABORT;
1351 musb->dma_controller->channel_abort(dma);
1354 /* do the proper sequence to abort the transfer in the
1355 * usb core; the dma engine should already be stopped.
1357 musb_h_tx_flush_fifo(hw_ep);
1358 tx_csr &= ~(MUSB_TXCSR_AUTOSET
1359 | MUSB_TXCSR_DMAENAB
1360 | MUSB_TXCSR_H_ERROR
1361 | MUSB_TXCSR_H_RXSTALL
1362 | MUSB_TXCSR_H_NAKTIMEOUT
1365 musb_ep_select(mbase, epnum);
1366 musb_writew(epio, MUSB_TXCSR, tx_csr);
1367 /* REVISIT may need to clear FLUSHFIFO ... */
1368 musb_writew(epio, MUSB_TXCSR, tx_csr);
1369 musb_writeb(epio, MUSB_TXINTERVAL, 0);
1374 /* second cppi case */
1375 if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) {
1376 dev_dbg(musb->controller, "extra TX%d ready, csr %04x\n", epnum, tx_csr);
1380 if (is_dma_capable() && dma && !status) {
1382 * DMA has completed. But if we're using DMA mode 1 (multi
1383 * packet DMA), we need a terminal TXPKTRDY interrupt before
1384 * we can consider this transfer completed, lest we trash
1385 * its last packet when writing the next URB's data. So we
1386 * switch back to mode 0 to get that interrupt; we'll come
1387 * back here once it happens.
1389 if (tx_csr & MUSB_TXCSR_DMAMODE) {
1391 * We shouldn't clear DMAMODE with DMAENAB set; so
1392 * clear them in a safe order. That should be OK
1393 * once TXPKTRDY has been set (and I've never seen
1394 * it being 0 at this moment -- DMA interrupt latency
1395 * is significant) but if it hasn't been then we have
1396 * no choice but to stop being polite and ignore the
1397 * programmer's guide... :-)
1399 * Note that we must write TXCSR with TXPKTRDY cleared
1400 * in order not to re-trigger the packet send (this bit
1401 * can't be cleared by CPU), and there's another caveat:
1402 * TXPKTRDY may be set shortly and then cleared in the
1403 * double-buffered FIFO mode, so we do an extra TXCSR
1404 * read for debouncing...
1406 tx_csr &= musb_readw(epio, MUSB_TXCSR);
1407 if (tx_csr & MUSB_TXCSR_TXPKTRDY) {
1408 tx_csr &= ~(MUSB_TXCSR_DMAENAB |
1409 MUSB_TXCSR_TXPKTRDY);
1410 musb_writew(epio, MUSB_TXCSR,
1411 tx_csr | MUSB_TXCSR_H_WZC_BITS);
1413 tx_csr &= ~(MUSB_TXCSR_DMAMODE |
1414 MUSB_TXCSR_TXPKTRDY);
1415 musb_writew(epio, MUSB_TXCSR,
1416 tx_csr | MUSB_TXCSR_H_WZC_BITS);
1419 * There is no guarantee that we'll get an interrupt
1420 * after clearing DMAMODE as we might have done this
1421 * too late (after TXPKTRDY was cleared by controller).
1422 * Re-read TXCSR as we have spoiled its previous value.
1424 tx_csr = musb_readw(epio, MUSB_TXCSR);
1428 * We may get here from a DMA completion or TXPKTRDY interrupt.
1429 * In any case, we must check the FIFO status here and bail out
1430 * only if the FIFO still has data -- that should prevent the
1431 * "missed" TXPKTRDY interrupts and deal with double-buffered
1434 if (tx_csr & (MUSB_TXCSR_FIFONOTEMPTY | MUSB_TXCSR_TXPKTRDY)) {
1435 dev_dbg(musb->controller, "DMA complete but packet still in FIFO, "
1436 "CSR %04x\n", tx_csr);
1441 if (!status || dma || usb_pipeisoc(pipe)) {
1443 length = dma->actual_len;
1445 length = qh->segsize;
1446 qh->offset += length;
1448 if (usb_pipeisoc(pipe)) {
1449 struct usb_iso_packet_descriptor *d;
1451 d = urb->iso_frame_desc + qh->iso_idx;
1452 d->actual_length = length;
1454 if (++qh->iso_idx >= urb->number_of_packets) {
1461 } else if (dma && urb->transfer_buffer_length == qh->offset) {
1464 /* see if we need to send more data, or ZLP */
1465 if (qh->segsize < qh->maxpacket)
1467 else if (qh->offset == urb->transfer_buffer_length
1468 && !(urb->transfer_flags
1472 offset = qh->offset;
1473 length = urb->transfer_buffer_length - offset;
1474 transfer_pending = true;
1479 /* urb->status != -EINPROGRESS means request has been faulted,
1480 * so we must abort this transfer after cleanup
1482 if (urb->status != -EINPROGRESS) {
1485 status = urb->status;
1490 urb->status = status;
1491 urb->actual_length = qh->offset;
1492 musb_advance_schedule(musb, urb, hw_ep, USB_DIR_OUT);
1494 } else if ((usb_pipeisoc(pipe) || transfer_pending) && dma) {
1495 if (musb_tx_dma_program(musb->dma_controller, hw_ep, qh, urb,
1497 if (is_cppi_enabled(musb) || tusb_dma_omap(musb))
1498 musb_h_tx_dma_start(hw_ep);
1501 } else if (tx_csr & MUSB_TXCSR_DMAENAB) {
1502 dev_dbg(musb->controller, "not complete, but DMA enabled?\n");
1507 * PIO: start next packet in this URB.
1509 * REVISIT: some docs say that when hw_ep->tx_double_buffered,
1510 * (and presumably, FIFO is not half-full) we should write *two*
1511 * packets before updating TXCSR; other docs disagree...
1513 if (length > qh->maxpacket)
1514 length = qh->maxpacket;
1515 /* Unmap the buffer so that CPU can use it */
1516 usb_hcd_unmap_urb_for_dma(musb->hcd, urb);
1519 * We need to map sg if the transfer_buffer is
1522 if (!urb->transfer_buffer) {
1523 /* sg_miter_start is already done in musb_ep_program */
1524 if (!sg_miter_next(&qh->sg_miter)) {
1525 dev_err(musb->controller, "error: sg list empty\n");
1526 sg_miter_stop(&qh->sg_miter);
1530 length = min_t(u32, length, qh->sg_miter.length);
1531 musb_write_fifo(hw_ep, length, qh->sg_miter.addr);
1532 qh->sg_miter.consumed = length;
1533 sg_miter_stop(&qh->sg_miter);
1535 musb_write_fifo(hw_ep, length, urb->transfer_buffer + offset);
1538 qh->segsize = length;
1540 musb_ep_select(mbase, epnum);
1541 musb_writew(epio, MUSB_TXCSR,
1542 MUSB_TXCSR_H_WZC_BITS | MUSB_TXCSR_TXPKTRDY);
1545 #ifdef CONFIG_USB_TI_CPPI41_DMA
1546 /* Seems to set up ISO for cppi41 and not advance len. See commit c57c41d */
1547 static int musb_rx_dma_iso_cppi41(struct dma_controller *dma,
1548 struct musb_hw_ep *hw_ep,
1553 struct dma_channel *channel = hw_ep->rx_channel;
1554 void __iomem *epio = hw_ep->regs;
1559 buf = (void *)urb->iso_frame_desc[qh->iso_idx].offset +
1560 (u32)urb->transfer_dma;
1562 length = urb->iso_frame_desc[qh->iso_idx].length;
1564 val = musb_readw(epio, MUSB_RXCSR);
1565 val |= MUSB_RXCSR_DMAENAB;
1566 musb_writew(hw_ep->regs, MUSB_RXCSR, val);
1568 res = dma->channel_program(channel, qh->maxpacket, 0,
1574 static inline int musb_rx_dma_iso_cppi41(struct dma_controller *dma,
1575 struct musb_hw_ep *hw_ep,
1584 #if defined(CONFIG_USB_INVENTRA_DMA) || defined(CONFIG_USB_UX500_DMA) || \
1585 defined(CONFIG_USB_TI_CPPI41_DMA)
1586 /* Host side RX (IN) using Mentor DMA works as follows:
1588 - if queue was empty, ProgramEndpoint
1589 - first IN token is sent out (by setting ReqPkt)
1590 LinuxIsr -> RxReady()
1591 /\ => first packet is received
1592 | - Set in mode 0 (DmaEnab, ~ReqPkt)
1593 | -> DMA Isr (transfer complete) -> RxReady()
1594 | - Ack receive (~RxPktRdy), turn off DMA (~DmaEnab)
1595 | - if urb not complete, send next IN token (ReqPkt)
1596 | | else complete urb.
1598 ---------------------------
1600 * Nuances of mode 1:
1601 * For short packets, no ack (+RxPktRdy) is sent automatically
1602 * (even if AutoClear is ON)
1603 * For full packets, ack (~RxPktRdy) and next IN token (+ReqPkt) is sent
1604 * automatically => major problem, as collecting the next packet becomes
1605 * difficult. Hence mode 1 is not used.
1608 * All we care about at this driver level is that
1609 * (a) all URBs terminate with REQPKT cleared and fifo(s) empty;
1610 * (b) termination conditions are: short RX, or buffer full;
1611 * (c) fault modes include
1612 * - iff URB_SHORT_NOT_OK, short RX status is -EREMOTEIO.
1613 * (and that endpoint's dma queue stops immediately)
1614 * - overflow (full, PLUS more bytes in the terminal packet)
1616 * So for example, usb-storage sets URB_SHORT_NOT_OK, and would
1617 * thus be a great candidate for using mode 1 ... for all but the
1618 * last packet of one URB's transfer.
1620 static int musb_rx_dma_inventra_cppi41(struct dma_controller *dma,
1621 struct musb_hw_ep *hw_ep,
1626 struct dma_channel *channel = hw_ep->rx_channel;
1627 void __iomem *epio = hw_ep->regs;
1634 if (usb_pipeisoc(pipe)) {
1635 struct usb_iso_packet_descriptor *d;
1637 d = urb->iso_frame_desc + qh->iso_idx;
1638 d->actual_length = len;
1640 /* even if there was an error, we did the dma
1641 * for iso_frame_desc->length
1643 if (d->status != -EILSEQ && d->status != -EOVERFLOW)
1646 if (++qh->iso_idx >= urb->number_of_packets) {
1649 /* REVISIT: Why ignore return value here? */
1650 if (musb_dma_cppi41(hw_ep->musb))
1651 done = musb_rx_dma_iso_cppi41(dma, hw_ep, qh,
1657 /* done if urb buffer is full or short packet is recd */
1658 done = (urb->actual_length + len >=
1659 urb->transfer_buffer_length
1660 || channel->actual_len < qh->maxpacket
1661 || channel->rx_packet_done);
1664 /* send IN token for next packet, without AUTOREQ */
1666 val = musb_readw(epio, MUSB_RXCSR);
1667 val |= MUSB_RXCSR_H_REQPKT;
1668 musb_writew(epio, MUSB_RXCSR, MUSB_RXCSR_H_WZC_BITS | val);
1674 /* Disadvantage of using mode 1:
1675 * It's basically usable only for mass storage class; essentially all
1676 * other protocols also terminate transfers on short packets.
1679 * An extra IN token is sent at the end of the transfer (due to AUTOREQ)
1680 * If you try to use mode 1 for (transfer_buffer_length - 512), and try
1681 * to use the extra IN token to grab the last packet using mode 0, then
1682 * the problem is that you cannot be sure when the device will send the
1683 * last packet and RxPktRdy set. Sometimes the packet is recd too soon
1684 * such that it gets lost when RxCSR is re-set at the end of the mode 1
1685 * transfer, while sometimes it is recd just a little late so that if you
1686 * try to configure for mode 0 soon after the mode 1 transfer is
1687 * completed, you will find rxcount 0. Okay, so you might think why not
1688 * wait for an interrupt when the pkt is recd. Well, you won't get any!
1690 static int musb_rx_dma_in_inventra_cppi41(struct dma_controller *dma,
1691 struct musb_hw_ep *hw_ep,
1697 struct musb *musb = hw_ep->musb;
1698 void __iomem *epio = hw_ep->regs;
1699 struct dma_channel *channel = hw_ep->rx_channel;
1701 int length, pipe, done;
1704 rx_count = musb_readw(epio, MUSB_RXCOUNT);
1707 if (usb_pipeisoc(pipe)) {
1709 struct usb_iso_packet_descriptor *d;
1711 d = urb->iso_frame_desc + qh->iso_idx;
1717 if (rx_count > d->length) {
1718 if (d_status == 0) {
1719 d_status = -EOVERFLOW;
1722 dev_dbg(musb->controller, "** OVERFLOW %d into %d\n",
1723 rx_count, d->length);
1728 d->status = d_status;
1729 buf = urb->transfer_dma + d->offset;
1732 buf = urb->transfer_dma + urb->actual_length;
1735 channel->desired_mode = 0;
1737 /* because of the issue below, mode 1 will
1738 * only rarely behave with correct semantics.
1740 if ((urb->transfer_flags & URB_SHORT_NOT_OK)
1741 && (urb->transfer_buffer_length - urb->actual_length)
1743 channel->desired_mode = 1;
1744 if (rx_count < hw_ep->max_packet_sz_rx) {
1746 channel->desired_mode = 0;
1748 length = urb->transfer_buffer_length;
1752 /* See comments above on disadvantages of using mode 1 */
1753 val = musb_readw(epio, MUSB_RXCSR);
1754 val &= ~MUSB_RXCSR_H_REQPKT;
1756 if (channel->desired_mode == 0)
1757 val &= ~MUSB_RXCSR_H_AUTOREQ;
1759 val |= MUSB_RXCSR_H_AUTOREQ;
1760 val |= MUSB_RXCSR_DMAENAB;
1762 /* autoclear shouldn't be set in high bandwidth */
1763 if (qh->hb_mult == 1)
1764 val |= MUSB_RXCSR_AUTOCLEAR;
1766 musb_writew(epio, MUSB_RXCSR, MUSB_RXCSR_H_WZC_BITS | val);
1768 /* REVISIT if when actual_length != 0,
1769 * transfer_buffer_length needs to be
1772 done = dma->channel_program(channel, qh->maxpacket,
1773 channel->desired_mode,
1777 dma->channel_release(channel);
1778 hw_ep->rx_channel = NULL;
1780 val = musb_readw(epio, MUSB_RXCSR);
1781 val &= ~(MUSB_RXCSR_DMAENAB
1782 | MUSB_RXCSR_H_AUTOREQ
1783 | MUSB_RXCSR_AUTOCLEAR);
1784 musb_writew(epio, MUSB_RXCSR, val);
1790 static inline int musb_rx_dma_inventra_cppi41(struct dma_controller *dma,
1791 struct musb_hw_ep *hw_ep,
1799 static inline int musb_rx_dma_in_inventra_cppi41(struct dma_controller *dma,
1800 struct musb_hw_ep *hw_ep,
1811 * Service an RX interrupt for the given IN endpoint; docs cover bulk, iso,
1812 * and high-bandwidth IN transfer cases.
1814 void musb_host_rx(struct musb *musb, u8 epnum)
1817 struct musb_hw_ep *hw_ep = musb->endpoints + epnum;
1818 struct dma_controller *c = musb->dma_controller;
1819 void __iomem *epio = hw_ep->regs;
1820 struct musb_qh *qh = hw_ep->in_qh;
1822 void __iomem *mbase = musb->mregs;
1825 bool iso_err = false;
1828 struct dma_channel *dma;
1829 unsigned int sg_flags = SG_MITER_ATOMIC | SG_MITER_TO_SG;
1831 musb_ep_select(mbase, epnum);
1834 dma = is_dma_capable() ? hw_ep->rx_channel : NULL;
1838 rx_csr = musb_readw(epio, MUSB_RXCSR);
1841 if (unlikely(!urb)) {
1842 /* REVISIT -- THIS SHOULD NEVER HAPPEN ... but, at least
1843 * usbtest #11 (unlinks) triggers it regularly, sometimes
1844 * with fifo full. (Only with DMA??)
1846 dev_dbg(musb->controller, "BOGUS RX%d ready, csr %04x, count %d\n", epnum, val,
1847 musb_readw(epio, MUSB_RXCOUNT));
1848 musb_h_flush_rxfifo(hw_ep, MUSB_RXCSR_CLRDATATOG);
1854 dev_dbg(musb->controller, "<== hw %d rxcsr %04x, urb actual %d (+dma %zu)\n",
1855 epnum, rx_csr, urb->actual_length,
1856 dma ? dma->actual_len : 0);
1858 /* check for errors, concurrent stall & unlink is not really
1860 if (rx_csr & MUSB_RXCSR_H_RXSTALL) {
1861 dev_dbg(musb->controller, "RX end %d STALL\n", epnum);
1863 /* stall; record URB status */
1866 } else if (rx_csr & MUSB_RXCSR_H_ERROR) {
1867 dev_dbg(musb->controller, "end %d RX proto error\n", epnum);
1870 musb_writeb(epio, MUSB_RXINTERVAL, 0);
1872 } else if (rx_csr & MUSB_RXCSR_DATAERROR) {
1874 if (USB_ENDPOINT_XFER_ISOC != qh->type) {
1875 dev_dbg(musb->controller, "RX end %d NAK timeout\n", epnum);
1877 /* NOTE: NAKing is *NOT* an error, so we want to
1878 * continue. Except ... if there's a request for
1879 * another QH, use that instead of starving it.
1881 * Devices like Ethernet and serial adapters keep
1882 * reads posted at all times, which will starve
1883 * other devices without this logic.
1885 if (usb_pipebulk(urb->pipe)
1887 && !list_is_singular(&musb->in_bulk)) {
1888 musb_bulk_nak_timeout(musb, hw_ep, 1);
1891 musb_ep_select(mbase, epnum);
1892 rx_csr |= MUSB_RXCSR_H_WZC_BITS;
1893 rx_csr &= ~MUSB_RXCSR_DATAERROR;
1894 musb_writew(epio, MUSB_RXCSR, rx_csr);
1898 dev_dbg(musb->controller, "RX end %d ISO data error\n", epnum);
1899 /* packet error reported later */
1902 } else if (rx_csr & MUSB_RXCSR_INCOMPRX) {
1903 dev_dbg(musb->controller, "end %d high bandwidth incomplete ISO packet RX\n",
1908 /* faults abort the transfer */
1910 /* clean up dma and collect transfer count */
1911 if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) {
1912 dma->status = MUSB_DMA_STATUS_CORE_ABORT;
1913 musb->dma_controller->channel_abort(dma);
1914 xfer_len = dma->actual_len;
1916 musb_h_flush_rxfifo(hw_ep, MUSB_RXCSR_CLRDATATOG);
1917 musb_writeb(epio, MUSB_RXINTERVAL, 0);
1922 if (unlikely(dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY)) {
1923 /* SHOULD NEVER HAPPEN ... but at least DaVinci has done it */
1924 ERR("RX%d dma busy, csr %04x\n", epnum, rx_csr);
1928 /* thorough shutdown for now ... given more precise fault handling
1929 * and better queueing support, we might keep a DMA pipeline going
1930 * while processing this irq for earlier completions.
1933 /* FIXME this is _way_ too much in-line logic for Mentor DMA */
1934 if (!musb_dma_inventra(musb) && !musb_dma_ux500(musb) &&
1935 (rx_csr & MUSB_RXCSR_H_REQPKT)) {
1936 /* REVISIT this happened for a while on some short reads...
1937 * the cleanup still needs investigation... looks bad...
1938 * and also duplicates dma cleanup code above ... plus,
1939 * shouldn't this be the "half full" double buffer case?
1941 if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) {
1942 dma->status = MUSB_DMA_STATUS_CORE_ABORT;
1943 musb->dma_controller->channel_abort(dma);
1944 xfer_len = dma->actual_len;
1948 dev_dbg(musb->controller, "RXCSR%d %04x, reqpkt, len %zu%s\n", epnum, rx_csr,
1949 xfer_len, dma ? ", dma" : "");
1950 rx_csr &= ~MUSB_RXCSR_H_REQPKT;
1952 musb_ep_select(mbase, epnum);
1953 musb_writew(epio, MUSB_RXCSR,
1954 MUSB_RXCSR_H_WZC_BITS | rx_csr);
1957 if (dma && (rx_csr & MUSB_RXCSR_DMAENAB)) {
1958 xfer_len = dma->actual_len;
1960 val &= ~(MUSB_RXCSR_DMAENAB
1961 | MUSB_RXCSR_H_AUTOREQ
1962 | MUSB_RXCSR_AUTOCLEAR
1963 | MUSB_RXCSR_RXPKTRDY);
1964 musb_writew(hw_ep->regs, MUSB_RXCSR, val);
1966 if (musb_dma_inventra(musb) || musb_dma_ux500(musb) ||
1967 musb_dma_cppi41(musb)) {
1968 done = musb_rx_dma_inventra_cppi41(c, hw_ep, qh, urb, xfer_len);
1969 dev_dbg(hw_ep->musb->controller,
1970 "ep %d dma %s, rxcsr %04x, rxcount %d\n",
1971 epnum, done ? "off" : "reset",
1972 musb_readw(epio, MUSB_RXCSR),
1973 musb_readw(epio, MUSB_RXCOUNT));
1978 } else if (urb->status == -EINPROGRESS) {
1979 /* if no errors, be sure a packet is ready for unloading */
1980 if (unlikely(!(rx_csr & MUSB_RXCSR_RXPKTRDY))) {
1982 ERR("Rx interrupt with no errors or packet!\n");
1984 /* FIXME this is another "SHOULD NEVER HAPPEN" */
1987 /* do the proper sequence to abort the transfer */
1988 musb_ep_select(mbase, epnum);
1989 val &= ~MUSB_RXCSR_H_REQPKT;
1990 musb_writew(epio, MUSB_RXCSR, val);
1994 /* we are expecting IN packets */
1995 if ((musb_dma_inventra(musb) || musb_dma_ux500(musb) ||
1996 musb_dma_cppi41(musb)) && dma) {
1997 dev_dbg(hw_ep->musb->controller,
1998 "RX%d count %d, buffer 0x%llx len %d/%d\n",
1999 epnum, musb_readw(epio, MUSB_RXCOUNT),
2000 (unsigned long long) urb->transfer_dma
2001 + urb->actual_length,
2003 urb->transfer_buffer_length);
2005 if (musb_rx_dma_in_inventra_cppi41(c, hw_ep, qh, urb,
2009 dev_err(musb->controller, "error: rx_dma failed\n");
2013 unsigned int received_len;
2015 /* Unmap the buffer so that CPU can use it */
2016 usb_hcd_unmap_urb_for_dma(musb->hcd, urb);
2019 * We need to map sg if the transfer_buffer is
2022 if (!urb->transfer_buffer) {
2024 sg_miter_start(&qh->sg_miter, urb->sg, 1,
2029 if (!sg_miter_next(&qh->sg_miter)) {
2030 dev_err(musb->controller, "error: sg list empty\n");
2031 sg_miter_stop(&qh->sg_miter);
2036 urb->transfer_buffer = qh->sg_miter.addr;
2037 received_len = urb->actual_length;
2039 done = musb_host_packet_rx(musb, urb, epnum,
2041 /* Calculate the number of bytes received */
2042 received_len = urb->actual_length -
2044 qh->sg_miter.consumed = received_len;
2045 sg_miter_stop(&qh->sg_miter);
2047 done = musb_host_packet_rx(musb, urb,
2050 dev_dbg(musb->controller, "read %spacket\n", done ? "last " : "");
2055 urb->actual_length += xfer_len;
2056 qh->offset += xfer_len;
2060 urb->transfer_buffer = NULL;
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);
2389 struct dma_channel *dma = NULL;
2391 musb_ep_select(regs, hw_end);
2393 if (is_dma_capable()) {
2394 dma = is_in ? ep->rx_channel : ep->tx_channel;
2396 status = ep->musb->dma_controller->channel_abort(dma);
2397 dev_dbg(musb->controller,
2398 "abort %cX%d DMA for urb %p --> %d\n",
2399 is_in ? 'R' : 'T', ep->epnum,
2401 urb->actual_length += dma->actual_len;
2405 /* turn off DMA requests, discard state, stop polling ... */
2406 if (ep->epnum && is_in) {
2407 /* giveback saves bulk toggle */
2408 csr = musb_h_flush_rxfifo(ep, 0);
2410 /* clear the endpoint's irq status here to avoid bogus irqs */
2411 if (is_dma_capable() && dma)
2412 musb_platform_clear_ep_rxintr(musb, ep->epnum);
2413 } else if (ep->epnum) {
2414 musb_h_tx_flush_fifo(ep);
2415 csr = musb_readw(epio, MUSB_TXCSR);
2416 csr &= ~(MUSB_TXCSR_AUTOSET
2417 | MUSB_TXCSR_DMAENAB
2418 | MUSB_TXCSR_H_RXSTALL
2419 | MUSB_TXCSR_H_NAKTIMEOUT
2420 | MUSB_TXCSR_H_ERROR
2421 | MUSB_TXCSR_TXPKTRDY);
2422 musb_writew(epio, MUSB_TXCSR, csr);
2423 /* REVISIT may need to clear FLUSHFIFO ... */
2424 musb_writew(epio, MUSB_TXCSR, csr);
2425 /* flush cpu writebuffer */
2426 csr = musb_readw(epio, MUSB_TXCSR);
2428 musb_h_ep0_flush_fifo(ep);
2431 musb_advance_schedule(ep->musb, urb, ep, is_in);
2435 static int musb_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
2437 struct musb *musb = hcd_to_musb(hcd);
2439 unsigned long flags;
2440 int is_in = usb_pipein(urb->pipe);
2443 dev_dbg(musb->controller, "urb=%p, dev%d ep%d%s\n", urb,
2444 usb_pipedevice(urb->pipe),
2445 usb_pipeendpoint(urb->pipe),
2446 is_in ? "in" : "out");
2448 spin_lock_irqsave(&musb->lock, flags);
2449 ret = usb_hcd_check_unlink_urb(hcd, urb, status);
2458 * Any URB not actively programmed into endpoint hardware can be
2459 * immediately given back; that's any URB not at the head of an
2460 * endpoint queue, unless someday we get real DMA queues. And even
2461 * if it's at the head, it might not be known to the hardware...
2463 * Otherwise abort current transfer, pending DMA, etc.; urb->status
2464 * has already been updated. This is a synchronous abort; it'd be
2465 * OK to hold off until after some IRQ, though.
2467 * NOTE: qh is invalid unless !list_empty(&hep->urb_list)
2470 || urb->urb_list.prev != &qh->hep->urb_list
2471 || musb_ep_get_qh(qh->hw_ep, is_in) != qh) {
2472 int ready = qh->is_ready;
2475 musb_giveback(musb, urb, 0);
2476 qh->is_ready = ready;
2478 /* If nothing else (usually musb_giveback) is using it
2479 * and its URB list has emptied, recycle this qh.
2481 if (ready && list_empty(&qh->hep->urb_list)) {
2482 qh->hep->hcpriv = NULL;
2483 list_del(&qh->ring);
2487 ret = musb_cleanup_urb(urb, qh);
2489 spin_unlock_irqrestore(&musb->lock, flags);
2493 /* disable an endpoint */
2495 musb_h_disable(struct usb_hcd *hcd, struct usb_host_endpoint *hep)
2497 u8 is_in = hep->desc.bEndpointAddress & USB_DIR_IN;
2498 unsigned long flags;
2499 struct musb *musb = hcd_to_musb(hcd);
2503 spin_lock_irqsave(&musb->lock, flags);
2509 /* NOTE: qh is invalid unless !list_empty(&hep->urb_list) */
2511 /* Kick the first URB off the hardware, if needed */
2513 if (musb_ep_get_qh(qh->hw_ep, is_in) == qh) {
2516 /* make software (then hardware) stop ASAP */
2518 urb->status = -ESHUTDOWN;
2521 musb_cleanup_urb(urb, qh);
2523 /* Then nuke all the others ... and advance the
2524 * queue on hw_ep (e.g. bulk ring) when we're done.
2526 while (!list_empty(&hep->urb_list)) {
2528 urb->status = -ESHUTDOWN;
2529 musb_advance_schedule(musb, urb, qh->hw_ep, is_in);
2532 /* Just empty the queue; the hardware is busy with
2533 * other transfers, and since !qh->is_ready nothing
2534 * will activate any of these as it advances.
2536 while (!list_empty(&hep->urb_list))
2537 musb_giveback(musb, next_urb(qh), -ESHUTDOWN);
2540 list_del(&qh->ring);
2544 spin_unlock_irqrestore(&musb->lock, flags);
2547 static int musb_h_get_frame_number(struct usb_hcd *hcd)
2549 struct musb *musb = hcd_to_musb(hcd);
2551 return musb_readw(musb->mregs, MUSB_FRAME);
2554 static int musb_h_start(struct usb_hcd *hcd)
2556 struct musb *musb = hcd_to_musb(hcd);
2558 /* NOTE: musb_start() is called when the hub driver turns
2559 * on port power, or when (OTG) peripheral starts.
2561 hcd->state = HC_STATE_RUNNING;
2562 musb->port1_status = 0;
2566 static void musb_h_stop(struct usb_hcd *hcd)
2568 musb_stop(hcd_to_musb(hcd));
2569 hcd->state = HC_STATE_HALT;
2572 static int musb_bus_suspend(struct usb_hcd *hcd)
2574 struct musb *musb = hcd_to_musb(hcd);
2578 ret = musb_port_suspend(musb, true);
2582 if (!is_host_active(musb))
2585 switch (musb->xceiv->otg->state) {
2586 case OTG_STATE_A_SUSPEND:
2588 case OTG_STATE_A_WAIT_VRISE:
2589 /* ID could be grounded even if there's no device
2590 * on the other end of the cable. NOTE that the
2591 * A_WAIT_VRISE timers are messy with MUSB...
2593 devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
2594 if ((devctl & MUSB_DEVCTL_VBUS) == MUSB_DEVCTL_VBUS)
2595 musb->xceiv->otg->state = OTG_STATE_A_WAIT_BCON;
2601 if (musb->is_active) {
2602 WARNING("trying to suspend as %s while active\n",
2603 usb_otg_state_string(musb->xceiv->otg->state));
2609 static int musb_bus_resume(struct usb_hcd *hcd)
2611 struct musb *musb = hcd_to_musb(hcd);
2614 musb->config->host_port_deassert_reset_at_resume)
2615 musb_port_reset(musb, false);
2620 #ifndef CONFIG_MUSB_PIO_ONLY
2622 #define MUSB_USB_DMA_ALIGN 4
2624 struct musb_temp_buffer {
2626 void *old_xfer_buffer;
2630 static void musb_free_temp_buffer(struct urb *urb)
2632 enum dma_data_direction dir;
2633 struct musb_temp_buffer *temp;
2636 if (!(urb->transfer_flags & URB_ALIGNED_TEMP_BUFFER))
2639 dir = usb_urb_dir_in(urb) ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
2641 temp = container_of(urb->transfer_buffer, struct musb_temp_buffer,
2644 if (dir == DMA_FROM_DEVICE) {
2645 if (usb_pipeisoc(urb->pipe))
2646 length = urb->transfer_buffer_length;
2648 length = urb->actual_length;
2650 memcpy(temp->old_xfer_buffer, temp->data, length);
2652 urb->transfer_buffer = temp->old_xfer_buffer;
2653 kfree(temp->kmalloc_ptr);
2655 urb->transfer_flags &= ~URB_ALIGNED_TEMP_BUFFER;
2658 static int musb_alloc_temp_buffer(struct urb *urb, gfp_t mem_flags)
2660 enum dma_data_direction dir;
2661 struct musb_temp_buffer *temp;
2663 size_t kmalloc_size;
2665 if (urb->num_sgs || urb->sg ||
2666 urb->transfer_buffer_length == 0 ||
2667 !((uintptr_t)urb->transfer_buffer & (MUSB_USB_DMA_ALIGN - 1)))
2670 dir = usb_urb_dir_in(urb) ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
2672 /* Allocate a buffer with enough padding for alignment */
2673 kmalloc_size = urb->transfer_buffer_length +
2674 sizeof(struct musb_temp_buffer) + MUSB_USB_DMA_ALIGN - 1;
2676 kmalloc_ptr = kmalloc(kmalloc_size, mem_flags);
2680 /* Position our struct temp_buffer such that data is aligned */
2681 temp = PTR_ALIGN(kmalloc_ptr, MUSB_USB_DMA_ALIGN);
2684 temp->kmalloc_ptr = kmalloc_ptr;
2685 temp->old_xfer_buffer = urb->transfer_buffer;
2686 if (dir == DMA_TO_DEVICE)
2687 memcpy(temp->data, urb->transfer_buffer,
2688 urb->transfer_buffer_length);
2689 urb->transfer_buffer = temp->data;
2691 urb->transfer_flags |= URB_ALIGNED_TEMP_BUFFER;
2696 static int musb_map_urb_for_dma(struct usb_hcd *hcd, struct urb *urb,
2699 struct musb *musb = hcd_to_musb(hcd);
2703 * The DMA engine in RTL1.8 and above cannot handle
2704 * DMA addresses that are not aligned to a 4 byte boundary.
2705 * For such engine implemented (un)map_urb_for_dma hooks.
2706 * Do not use these hooks for RTL<1.8
2708 if (musb->hwvers < MUSB_HWVERS_1800)
2709 return usb_hcd_map_urb_for_dma(hcd, urb, mem_flags);
2711 ret = musb_alloc_temp_buffer(urb, mem_flags);
2715 ret = usb_hcd_map_urb_for_dma(hcd, urb, mem_flags);
2717 musb_free_temp_buffer(urb);
2722 static void musb_unmap_urb_for_dma(struct usb_hcd *hcd, struct urb *urb)
2724 struct musb *musb = hcd_to_musb(hcd);
2726 usb_hcd_unmap_urb_for_dma(hcd, urb);
2728 /* Do not use this hook for RTL<1.8 (see description above) */
2729 if (musb->hwvers < MUSB_HWVERS_1800)
2732 musb_free_temp_buffer(urb);
2734 #endif /* !CONFIG_MUSB_PIO_ONLY */
2736 static const struct hc_driver musb_hc_driver = {
2737 .description = "musb-hcd",
2738 .product_desc = "MUSB HDRC host driver",
2739 .hcd_priv_size = sizeof(struct musb *),
2740 .flags = HCD_USB2 | HCD_MEMORY | HCD_BH,
2742 /* not using irq handler or reset hooks from usbcore, since
2743 * those must be shared with peripheral code for OTG configs
2746 .start = musb_h_start,
2747 .stop = musb_h_stop,
2749 .get_frame_number = musb_h_get_frame_number,
2751 .urb_enqueue = musb_urb_enqueue,
2752 .urb_dequeue = musb_urb_dequeue,
2753 .endpoint_disable = musb_h_disable,
2755 #ifndef CONFIG_MUSB_PIO_ONLY
2756 .map_urb_for_dma = musb_map_urb_for_dma,
2757 .unmap_urb_for_dma = musb_unmap_urb_for_dma,
2760 .hub_status_data = musb_hub_status_data,
2761 .hub_control = musb_hub_control,
2762 .bus_suspend = musb_bus_suspend,
2763 .bus_resume = musb_bus_resume,
2764 /* .start_port_reset = NULL, */
2765 /* .hub_irq_enable = NULL, */
2768 int musb_host_alloc(struct musb *musb)
2770 struct device *dev = musb->controller;
2772 /* usbcore sets dev->driver_data to hcd, and sometimes uses that... */
2773 musb->hcd = usb_create_hcd(&musb_hc_driver, dev, dev_name(dev));
2777 *musb->hcd->hcd_priv = (unsigned long) musb;
2778 musb->hcd->self.uses_pio_for_control = 1;
2779 musb->hcd->uses_new_polling = 1;
2780 musb->hcd->has_tt = 1;
2785 void musb_host_cleanup(struct musb *musb)
2787 if (musb->port_mode == MUSB_PORT_MODE_GADGET)
2789 usb_remove_hcd(musb->hcd);
2792 void musb_host_free(struct musb *musb)
2794 usb_put_hcd(musb->hcd);
2797 int musb_host_setup(struct musb *musb, int power_budget)
2800 struct usb_hcd *hcd = musb->hcd;
2802 MUSB_HST_MODE(musb);
2803 musb->xceiv->otg->default_a = 1;
2804 musb->xceiv->otg->state = OTG_STATE_A_IDLE;
2806 otg_set_host(musb->xceiv->otg, &hcd->self);
2807 hcd->self.otg_port = 1;
2808 musb->xceiv->otg->host = &hcd->self;
2809 hcd->power_budget = 2 * (power_budget ? : 250);
2811 ret = usb_add_hcd(hcd, 0, 0);
2815 device_wakeup_enable(hcd->self.controller);
2819 void musb_host_resume_root_hub(struct musb *musb)
2821 usb_hcd_resume_root_hub(musb->hcd);
2824 void musb_host_poke_root_hub(struct musb *musb)
2826 MUSB_HST_MODE(musb);
2827 if (musb->hcd->status_urb)
2828 usb_hcd_poll_rh_status(musb->hcd);
2830 usb_hcd_resume_root_hub(musb->hcd);