2 * MUSB OTG driver host support
4 * Copyright 2005 Mentor Graphics Corporation
5 * Copyright (C) 2005-2006 by Texas Instruments
6 * Copyright (C) 2006-2007 Nokia Corporation
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * version 2 as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
22 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
23 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
24 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
25 * NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
28 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
29 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 #include <linux/module.h>
36 #include <linux/kernel.h>
37 #include <linux/delay.h>
38 #include <linux/sched.h>
39 #include <linux/slab.h>
40 #include <linux/errno.h>
41 #include <linux/init.h>
42 #include <linux/list.h>
45 #include "musb_host.h"
48 /* MUSB HOST status 22-mar-2006
50 * - There's still lots of partial code duplication for fault paths, so
51 * they aren't handled as consistently as they need to be.
53 * - PIO mostly behaved when last tested.
54 * + including ep0, with all usbtest cases 9, 10
55 * + usbtest 14 (ep0out) doesn't seem to run at all
56 * + double buffered OUT/TX endpoints saw stalls(!) with certain usbtest
57 * configurations, but otherwise double buffering passes basic tests.
58 * + for 2.6.N, for N > ~10, needs API changes for hcd framework.
60 * - DMA (CPPI) ... partially behaves, not currently recommended
61 * + about 1/15 the speed of typical EHCI implementations (PCI)
62 * + RX, all too often reqpkt seems to misbehave after tx
63 * + TX, no known issues (other than evident silicon issue)
65 * - DMA (Mentor/OMAP) ...has at least toggle update problems
67 * - Still no traffic scheduling code to make NAKing for bulk or control
68 * transfers unable to starve other requests; or to make efficient use
69 * of hardware with periodic transfers. (Note that network drivers
70 * commonly post bulk reads that stay pending for a long time; these
71 * would make very visible trouble.)
73 * - Not tested with HNP, but some SRP paths seem to behave.
77 * - Bulk traffic finally uses both sides of hardware ep1, freeing up an
78 * extra endpoint for periodic use enabling hub + keybd + mouse. That
79 * mostly works, except that with "usbnet" it's easy to trigger cases
80 * with "ping" where RX loses. (a) ping to davinci, even "ping -f",
81 * fine; but (b) ping _from_ davinci, even "ping -c 1", ICMP RX loses
82 * although ARP RX wins. (That test was done with a full speed link.)
87 * NOTE on endpoint usage:
89 * CONTROL transfers all go through ep0. BULK ones go through dedicated IN
90 * and OUT endpoints ... hardware is dedicated for those "async" queue(s).
92 * (Yes, bulk _could_ use more of the endpoints than that, and would even
93 * benefit from it ... one remote device may easily be NAKing while others
94 * need to perform transfers in that same direction. The same thing could
95 * be done in software though, assuming dma cooperates.)
97 * INTERUPPT and ISOCHRONOUS transfers are scheduled to the other endpoints.
98 * So far that scheduling is both dumb and optimistic: the endpoint will be
99 * "claimed" until its software queue is no longer refilled. No multiplexing
100 * of transfers between endpoints, or anything clever.
104 /*************************** Forwards ***************************/
106 static void musb_ep_program(struct musb *musb, u8 epnum,
107 struct urb *urb, unsigned int nOut,
111 * Clear TX fifo. Needed to avoid BABBLE errors.
113 static inline void musb_h_tx_flush_fifo(struct musb_hw_ep *ep)
115 void __iomem *epio = ep->regs;
119 csr = musb_readw(epio, MUSB_TXCSR);
120 while (csr & MUSB_TXCSR_FIFONOTEMPTY) {
121 DBG(5, "Host TX FIFONOTEMPTY csr: %02x\n", csr);
122 csr |= MUSB_TXCSR_FLUSHFIFO;
123 musb_writew(epio, MUSB_TXCSR, csr);
124 csr = musb_readw(epio, MUSB_TXCSR);
126 ERR("Could not flush host TX fifo: csr: %04x\n", csr);
134 * Start transmit. Caller is responsible for locking shared resources.
135 * musb must be locked.
137 static inline void musb_h_tx_start(struct musb_hw_ep *ep)
141 /* NOTE: no locks here; caller should lock and select EP */
143 txcsr = musb_readw(ep->regs, MUSB_TXCSR);
144 txcsr |= MUSB_TXCSR_TXPKTRDY | MUSB_TXCSR_H_WZC_BITS;
145 musb_writew(ep->regs, MUSB_TXCSR, txcsr);
147 txcsr = MUSB_CSR0_H_SETUPPKT | MUSB_CSR0_TXPKTRDY;
148 musb_writew(ep->regs, MUSB_CSR0, txcsr);
153 static inline void cppi_host_txdma_start(struct musb_hw_ep *ep)
157 /* NOTE: no locks here; caller should lock and select EP */
158 txcsr = musb_readw(ep->regs, MUSB_TXCSR);
159 txcsr |= MUSB_TXCSR_DMAENAB | MUSB_TXCSR_H_WZC_BITS;
160 musb_writew(ep->regs, MUSB_TXCSR, txcsr);
164 * Start the URB at the front of an endpoint's queue
165 * end must be claimed from the caller.
167 * Context: controller locked, irqs blocked
170 musb_start_urb(struct musb *musb, int is_in, struct musb_qh *qh)
175 void __iomem *mbase = musb->mregs;
176 struct urb *urb = next_urb(qh);
177 struct musb_hw_ep *hw_ep = qh->hw_ep;
178 unsigned pipe = urb->pipe;
179 u8 address = usb_pipedevice(pipe);
180 int epnum = hw_ep->epnum;
182 /* initialize software qh state */
186 /* gather right source of data */
188 case USB_ENDPOINT_XFER_CONTROL:
189 /* control transfers always start with SETUP */
192 musb->ep0_stage = MGC_END0_START;
193 buf = urb->setup_packet;
196 case USB_ENDPOINT_XFER_ISOC:
199 buf = urb->transfer_buffer + urb->iso_frame_desc[0].offset;
200 len = urb->iso_frame_desc[0].length;
202 default: /* bulk, interrupt */
203 buf = urb->transfer_buffer;
204 len = urb->transfer_buffer_length;
207 DBG(4, "qh %p urb %p dev%d ep%d%s%s, hw_ep %d, %p/%d\n",
208 qh, urb, address, qh->epnum,
209 is_in ? "in" : "out",
210 ({char *s; switch (qh->type) {
211 case USB_ENDPOINT_XFER_CONTROL: s = ""; break;
212 case USB_ENDPOINT_XFER_BULK: s = "-bulk"; break;
213 case USB_ENDPOINT_XFER_ISOC: s = "-iso"; break;
214 default: s = "-intr"; break;
218 /* Configure endpoint */
219 if (is_in || hw_ep->is_shared_fifo)
223 musb_ep_program(musb, epnum, urb, !is_in, buf, len);
225 /* transmit may have more work: start it when it is time */
229 /* determine if the time is right for a periodic transfer */
231 case USB_ENDPOINT_XFER_ISOC:
232 case USB_ENDPOINT_XFER_INT:
233 DBG(3, "check whether there's still time for periodic Tx\n");
235 frame = musb_readw(mbase, MUSB_FRAME);
236 /* FIXME this doesn't implement that scheduling policy ...
237 * or handle framecounter wrapping
239 if ((urb->transfer_flags & URB_ISO_ASAP)
240 || (frame >= urb->start_frame)) {
241 /* REVISIT the SOF irq handler shouldn't duplicate
242 * this code; and we don't init urb->start_frame...
247 qh->frame = urb->start_frame;
248 /* enable SOF interrupt so we can count down */
249 DBG(1,"SOF for %d\n", epnum);
250 #if 1 // ifndef CONFIG_ARCH_DAVINCI
251 musb_writeb(mbase, MUSB_INTRUSBE, 0xff);
257 DBG(4, "Start TX%d %s\n", epnum,
258 hw_ep->tx_channel ? "dma" : "pio");
260 if (!hw_ep->tx_channel)
261 musb_h_tx_start(hw_ep);
262 else if (is_cppi_enabled() || tusb_dma_omap())
263 cppi_host_txdma_start(hw_ep);
267 /* caller owns controller lock, irqs are blocked */
269 __musb_giveback(struct musb *musb, struct urb *urb, int status)
270 __releases(musb->lock)
271 __acquires(musb->lock)
273 if ((urb->transfer_flags & URB_SHORT_NOT_OK)
274 && (urb->actual_length < urb->transfer_buffer_length)
276 && usb_pipein(urb->pipe))
279 spin_lock(&urb->lock);
281 if (urb->status == -EINPROGRESS)
282 urb->status = status;
283 spin_unlock(&urb->lock);
285 DBG(({ int level; switch (urb->status) {
289 /* common/boring faults */
300 "complete %p (%d), dev%d ep%d%s, %d/%d\n",
302 usb_pipedevice(urb->pipe),
303 usb_pipeendpoint(urb->pipe),
304 usb_pipein(urb->pipe) ? "in" : "out",
305 urb->actual_length, urb->transfer_buffer_length
308 spin_unlock(&musb->lock);
309 usb_hcd_giveback_urb(musb_to_hcd(musb), urb);
310 spin_lock(&musb->lock);
313 /* for bulk/interrupt endpoints only */
314 static inline void musb_save_toggle(struct musb_hw_ep *ep, int is_in, struct urb *urb)
316 struct usb_device *udev = urb->dev;
318 void __iomem *epio = ep->regs;
321 /* FIXME: the current Mentor DMA code seems to have
322 * problems getting toggle correct.
325 if (is_in || ep->is_shared_fifo)
331 csr = musb_readw(epio, MUSB_TXCSR);
332 usb_settoggle(udev, qh->epnum, 1,
333 (csr & MUSB_TXCSR_H_DATATOGGLE)
336 csr = musb_readw(epio, MUSB_RXCSR);
337 usb_settoggle(udev, qh->epnum, 0,
338 (csr & MUSB_RXCSR_H_DATATOGGLE)
343 /* caller owns controller lock, irqs are blocked */
344 static struct musb_qh *
345 musb_giveback(struct musb_qh *qh, struct urb *urb, int status)
348 struct musb_hw_ep *ep = qh->hw_ep;
349 struct musb *musb = ep->musb;
350 int ready = qh->is_ready;
352 if (ep->is_shared_fifo)
355 is_in = usb_pipein(urb->pipe);
357 /* save toggle eagerly, for paranoia */
359 case USB_ENDPOINT_XFER_BULK:
360 case USB_ENDPOINT_XFER_INT:
361 musb_save_toggle(ep, is_in, urb);
363 case USB_ENDPOINT_XFER_ISOC:
364 if (status == 0 && urb->error_count)
370 __musb_giveback(musb, urb, status);
371 qh->is_ready = ready;
373 /* reclaim resources (and bandwidth) ASAP; deschedule it, and
374 * invalidate qh as soon as list_empty(&hep->urb_list)
376 if (list_empty(&qh->hep->urb_list)) {
377 struct list_head *head;
384 /* clobber old pointers to this qh */
385 if (is_in || ep->is_shared_fifo)
389 qh->hep->hcpriv = NULL;
393 case USB_ENDPOINT_XFER_ISOC:
394 case USB_ENDPOINT_XFER_INT:
395 /* this is where periodic bandwidth should be
396 * de-allocated if it's tracked and allocated;
397 * and where we'd update the schedule tree...
399 musb->periodic[ep->epnum] = NULL;
404 case USB_ENDPOINT_XFER_CONTROL:
405 case USB_ENDPOINT_XFER_BULK:
406 /* fifo policy for these lists, except that NAKing
407 * should rotate a qh to the end (for fairness).
409 head = qh->ring.prev;
420 * Advance this hardware endpoint's queue, completing the specified urb and
421 * advancing to either the next urb queued to that qh, or else invalidating
422 * that qh and advancing to the next qh scheduled after the current one.
424 * Context: caller owns controller lock, irqs are blocked
427 musb_advance_schedule(struct musb *musb, struct urb *urb,
428 struct musb_hw_ep *hw_ep, int is_in)
432 if (is_in || hw_ep->is_shared_fifo)
436 qh = musb_giveback(qh, urb, 0);
438 if (qh && qh->is_ready && !list_empty(&qh->hep->urb_list)) {
439 DBG(4, "... next ep%d %cX urb %p\n",
440 hw_ep->epnum, is_in ? 'R' : 'T',
442 musb_start_urb(musb, is_in, qh);
446 static inline u16 musb_h_flush_rxfifo(struct musb_hw_ep *hw_ep, u16 csr)
448 /* we don't want fifo to fill itself again;
449 * ignore dma (various models),
450 * leave toggle alone (may not have been saved yet)
452 csr |= MUSB_RXCSR_FLUSHFIFO | MUSB_RXCSR_RXPKTRDY;
453 csr &= ~( MUSB_RXCSR_H_REQPKT
454 | MUSB_RXCSR_H_AUTOREQ
455 | MUSB_RXCSR_AUTOCLEAR
458 /* write 2x to allow double buffering */
459 musb_writew(hw_ep->regs, MUSB_RXCSR, csr);
460 musb_writew(hw_ep->regs, MUSB_RXCSR, csr);
462 /* flush writebuffer */
463 return musb_readw(hw_ep->regs, MUSB_RXCSR);
467 * PIO RX for a packet (or part of it).
469 static u8 musb_host_packet_rx(struct musb *musb, struct urb *urb,
470 u8 epnum, u8 iso_err)
478 struct musb_hw_ep *hw_ep = musb->endpoints + epnum;
479 void __iomem *epio = hw_ep->regs;
480 struct musb_qh *qh = hw_ep->in_qh;
481 int pipe = urb->pipe;
482 void *buffer = urb->transfer_buffer;
484 // musb_ep_select(mbase, epnum);
485 rx_count = musb_readw(epio, MUSB_RXCOUNT);
486 DBG(3, "RX%d count %d, buffer %p len %d/%d\n", epnum, rx_count,
487 urb->transfer_buffer, qh->offset,
488 urb->transfer_buffer_length);
491 if (usb_pipeisoc(pipe)) {
493 struct usb_iso_packet_descriptor *d;
500 d = urb->iso_frame_desc + qh->iso_idx;
501 buf = buffer + d->offset;
503 if (rx_count > length) {
508 DBG(2, "** OVERFLOW %d into %d\n", rx_count, length);
512 urb->actual_length += length;
513 d->actual_length = length;
517 /* see if we are done */
518 done = (++qh->iso_idx >= urb->number_of_packets);
521 buf = buffer + qh->offset;
522 length = urb->transfer_buffer_length - qh->offset;
523 if (rx_count > length) {
524 if (urb->status == -EINPROGRESS)
525 urb->status = -EOVERFLOW;
526 DBG(2, "** OVERFLOW %d into %d\n", rx_count, length);
530 urb->actual_length += length;
531 qh->offset += length;
533 /* see if we are done */
534 done = (urb->actual_length == urb->transfer_buffer_length)
535 || (rx_count < qh->maxpacket)
536 || (urb->status != -EINPROGRESS);
538 && (urb->status == -EINPROGRESS)
539 && (urb->transfer_flags & URB_SHORT_NOT_OK)
540 && (urb->actual_length
541 < urb->transfer_buffer_length))
542 urb->status = -EREMOTEIO;
545 musb_read_fifo(hw_ep, length, buf);
547 csr = musb_readw(epio, MUSB_RXCSR);
548 csr |= MUSB_RXCSR_H_WZC_BITS;
549 if (unlikely(do_flush))
550 musb_h_flush_rxfifo(hw_ep, csr);
552 /* REVISIT this assumes AUTOCLEAR is never set */
553 csr &= ~(MUSB_RXCSR_RXPKTRDY | MUSB_RXCSR_H_REQPKT);
555 csr |= MUSB_RXCSR_H_REQPKT;
556 musb_writew(epio, MUSB_RXCSR, csr);
562 /* we don't always need to reinit a given side of an endpoint...
563 * when we do, use tx/rx reinit routine and then construct a new CSR
564 * to address data toggle, NYET, and DMA or PIO.
566 * it's possible that driver bugs (especially for DMA) or aborting a
567 * transfer might have left the endpoint busier than it should be.
568 * the busy/not-empty tests are basically paranoia.
571 musb_rx_reinit(struct musb *musb, struct musb_qh *qh, struct musb_hw_ep *ep)
575 /* NOTE: we know the "rx" fifo reinit never triggers for ep0.
576 * That always uses tx_reinit since ep0 repurposes TX register
577 * offsets; the initial SETUP packet is also a kind of OUT.
580 /* if programmed for Tx, put it in RX mode */
581 if (ep->is_shared_fifo) {
582 csr = musb_readw(ep->regs, MUSB_TXCSR);
583 if (csr & MUSB_TXCSR_MODE) {
584 musb_h_tx_flush_fifo(ep);
585 musb_writew(ep->regs, MUSB_TXCSR,
586 MUSB_TXCSR_FRCDATATOG);
588 /* clear mode (and everything else) to enable Rx */
589 musb_writew(ep->regs, MUSB_TXCSR, 0);
591 /* scrub all previous state, clearing toggle */
593 csr = musb_readw(ep->regs, MUSB_RXCSR);
594 if (csr & MUSB_RXCSR_RXPKTRDY)
595 WARN("rx%d, packet/%d ready?\n", ep->epnum,
596 musb_readw(ep->regs, MUSB_RXCOUNT));
598 musb_h_flush_rxfifo(ep, MUSB_RXCSR_CLRDATATOG);
601 /* target addr and (for multipoint) hub addr/port */
602 if (musb->is_multipoint) {
603 musb_writeb(ep->target_regs, MUSB_RXFUNCADDR,
605 musb_writeb(ep->target_regs, MUSB_RXHUBADDR,
607 musb_writeb(ep->target_regs, MUSB_RXHUBPORT,
610 musb_writeb(musb->mregs, MUSB_FADDR, qh->addr_reg);
612 /* protocol/endpoint, interval/NAKlimit, i/o size */
613 musb_writeb(ep->regs, MUSB_RXTYPE, qh->type_reg);
614 musb_writeb(ep->regs, MUSB_RXINTERVAL, qh->intv_reg);
615 /* NOTE: bulk combining rewrites high bits of maxpacket */
616 musb_writew(ep->regs, MUSB_RXMAXP, qh->maxpacket);
623 * Program an HDRC endpoint as per the given URB
624 * Context: irqs blocked, controller lock held
626 static void musb_ep_program(struct musb *musb, u8 epnum,
627 struct urb *urb, unsigned int is_out,
630 struct dma_controller *dma_controller;
631 struct dma_channel *dma_channel;
633 void __iomem *mbase = musb->mregs;
634 struct musb_hw_ep *hw_ep = musb->endpoints + epnum;
635 void __iomem *epio = hw_ep->regs;
639 if (!is_out || hw_ep->is_shared_fifo)
644 packet_sz = qh->maxpacket;
646 DBG(3, "%s hw%d urb %p spd%d dev%d ep%d%s "
647 "h_addr%02x h_port%02x bytes %d\n",
648 is_out ? "-->" : "<--",
649 epnum, urb, urb->dev->speed,
650 qh->addr_reg, qh->epnum, is_out ? "out" : "in",
651 qh->h_addr_reg, qh->h_port_reg,
654 musb_ep_select(mbase, epnum);
656 /* candidate for DMA? */
657 dma_controller = musb->dma_controller;
658 if (is_dma_capable() && epnum && dma_controller) {
659 dma_channel = is_out ? hw_ep->tx_channel : hw_ep->rx_channel;
661 dma_channel = dma_controller->channel_alloc(
662 dma_controller, hw_ep, is_out);
664 hw_ep->tx_channel = dma_channel;
666 hw_ep->rx_channel = dma_channel;
671 /* make sure we clear DMAEnab, autoSet bits from previous run */
673 /* OUT/transmit/EP0 or IN/receive? */
679 csr = musb_readw(epio, MUSB_TXCSR);
681 /* disable interrupt in case we flush */
682 int_txe = musb_readw(mbase, MUSB_INTRTXE);
683 musb_writew(mbase, MUSB_INTRTXE, int_txe & ~(1 << epnum));
685 /* general endpoint setup */
689 /* ASSERT: TXCSR_DMAENAB was already cleared */
691 /* flush all old state, set default */
692 musb_h_tx_flush_fifo(hw_ep);
693 csr &= ~(MUSB_TXCSR_H_NAKTIMEOUT
695 | MUSB_TXCSR_FRCDATATOG
696 | MUSB_TXCSR_H_RXSTALL
698 | MUSB_TXCSR_TXPKTRDY
700 csr |= MUSB_TXCSR_MODE;
702 if (usb_gettoggle(urb->dev,
704 csr |= MUSB_TXCSR_H_WR_DATATOGGLE
705 | MUSB_TXCSR_H_DATATOGGLE;
707 csr |= MUSB_TXCSR_CLRDATATOG;
709 /* twice in case of double packet buffering */
710 musb_writew(epio, MUSB_TXCSR, csr);
711 /* REVISIT may need to clear FLUSHFIFO ... */
712 musb_writew(epio, MUSB_TXCSR, csr);
713 csr = musb_readw(epio, MUSB_TXCSR);
715 /* endpoint 0: just flush */
716 musb_writew(epio, MUSB_CSR0,
717 csr | MUSB_CSR0_FLUSHFIFO);
718 musb_writew(epio, MUSB_CSR0,
719 csr | MUSB_CSR0_FLUSHFIFO);
722 /* target addr and (for multipoint) hub addr/port */
723 if (musb->is_multipoint) {
725 MUSB_BUSCTL_OFFSET(epnum, MUSB_TXFUNCADDR),
728 MUSB_BUSCTL_OFFSET(epnum, MUSB_TXHUBADDR),
731 MUSB_BUSCTL_OFFSET(epnum, MUSB_TXHUBPORT),
733 /* FIXME if !epnum, do the same for RX ... */
735 musb_writeb(mbase, MUSB_FADDR, qh->addr_reg);
737 /* protocol/endpoint/interval/NAKlimit */
739 musb_writeb(epio, MUSB_TXTYPE, qh->type_reg);
740 if (can_bulk_split(musb, qh->type))
741 musb_writew(epio, MUSB_TXMAXP,
743 | ((hw_ep->max_packet_sz_tx /
744 packet_sz) - 1) << 11);
746 musb_writew(epio, MUSB_TXMAXP,
748 musb_writeb(epio, MUSB_TXINTERVAL, qh->intv_reg);
750 musb_writeb(epio, MUSB_NAKLIMIT0, qh->intv_reg);
751 if (musb->is_multipoint)
752 musb_writeb(epio, MUSB_TYPE0,
756 if (can_bulk_split(musb, qh->type))
757 load_count = min((u32) hw_ep->max_packet_sz_tx,
760 load_count = min((u32) packet_sz, len);
762 #ifdef CONFIG_USB_INVENTRA_DMA
765 /* clear previous state */
766 csr = musb_readw(epio, MUSB_TXCSR);
767 csr &= ~(MUSB_TXCSR_AUTOSET
769 | MUSB_TXCSR_DMAENAB);
770 csr |= MUSB_TXCSR_MODE;
771 musb_writew(epio, MUSB_TXCSR,
772 csr | MUSB_TXCSR_MODE);
774 qh->segsize = min(len, dma_channel->max_len);
776 if (qh->segsize <= packet_sz)
777 dma_channel->desired_mode = 0;
779 dma_channel->desired_mode = 1;
782 if (dma_channel->desired_mode == 0) {
783 csr &= ~(MUSB_TXCSR_AUTOSET
784 | MUSB_TXCSR_DMAMODE);
785 csr |= (MUSB_TXCSR_DMAENAB);
786 // against programming guide
788 csr |= (MUSB_TXCSR_AUTOSET
790 | MUSB_TXCSR_DMAMODE);
792 musb_writew(epio, MUSB_TXCSR, csr);
794 dma_ok = dma_controller->channel_program(
795 dma_channel, packet_sz,
796 dma_channel->desired_mode,
802 dma_controller->channel_release(dma_channel);
804 hw_ep->tx_channel = NULL;
806 hw_ep->rx_channel = NULL;
812 /* candidate for DMA */
813 if ((is_cppi_enabled() || tusb_dma_omap()) && dma_channel) {
815 /* program endpoint CSRs first, then setup DMA.
816 * assume CPPI setup succeeds.
817 * defer enabling dma.
819 csr = musb_readw(epio, MUSB_TXCSR);
820 csr &= ~(MUSB_TXCSR_AUTOSET
822 | MUSB_TXCSR_DMAENAB);
823 csr |= MUSB_TXCSR_MODE;
824 musb_writew(epio, MUSB_TXCSR,
825 csr | MUSB_TXCSR_MODE);
827 dma_channel->actual_len = 0L;
830 /* TX uses "rndis" mode automatically, but needs help
831 * to identify the zero-length-final-packet case.
833 dma_ok = dma_controller->channel_program(
834 dma_channel, packet_sz,
843 dma_controller->channel_release(dma_channel);
844 dma_channel = hw_ep->tx_channel = NULL;
846 /* REVISIT there's an error path here that
847 * needs handling: can't do dma, but
848 * there's no pio buffer address...
854 /* ASSERT: TXCSR_DMAENAB was already cleared */
856 /* PIO to load FIFO */
857 qh->segsize = load_count;
858 musb_write_fifo(hw_ep, load_count, buf);
859 csr = musb_readw(epio, MUSB_TXCSR);
860 csr &= ~(MUSB_TXCSR_DMAENAB
862 | MUSB_TXCSR_AUTOSET);
864 csr |= MUSB_TXCSR_MODE;
867 musb_writew(epio, MUSB_TXCSR, csr);
870 /* re-enable interrupt */
871 musb_writew(mbase, MUSB_INTRTXE, int_txe);
877 if (hw_ep->rx_reinit) {
878 musb_rx_reinit(musb, qh, hw_ep);
880 /* init new state: toggle and NYET, maybe DMA later */
881 if (usb_gettoggle(urb->dev, qh->epnum, 0))
882 csr = MUSB_RXCSR_H_WR_DATATOGGLE
883 | MUSB_RXCSR_H_DATATOGGLE;
886 if (qh->type == USB_ENDPOINT_XFER_INT)
887 csr |= MUSB_RXCSR_DISNYET;
890 csr = musb_readw(hw_ep->regs, MUSB_RXCSR);
892 if (csr & (MUSB_RXCSR_RXPKTRDY
894 | MUSB_RXCSR_H_REQPKT))
895 ERR("broken !rx_reinit, ep%d csr %04x\n",
898 /* scrub any stale state, leaving toggle alone */
899 csr &= MUSB_RXCSR_DISNYET;
902 /* kick things off */
904 if ((is_cppi_enabled() || tusb_dma_omap()) && dma_channel) {
905 /* candidate for DMA */
907 dma_channel->actual_len = 0L;
910 /* AUTOREQ is in a DMA register */
911 musb_writew(hw_ep->regs, MUSB_RXCSR, csr);
912 csr = musb_readw(hw_ep->regs,
915 /* unless caller treats short rx transfers as
916 * errors, we dare not queue multiple transfers.
918 dma_ok = dma_controller->channel_program(
919 dma_channel, packet_sz,
920 !(urb->transfer_flags
925 dma_controller->channel_release(
927 dma_channel = hw_ep->rx_channel = NULL;
929 csr |= MUSB_RXCSR_DMAENAB;
933 csr |= MUSB_RXCSR_H_REQPKT;
934 DBG(7, "RXCSR%d := %04x\n", epnum, csr);
935 musb_writew(hw_ep->regs, MUSB_RXCSR, csr);
936 csr = musb_readw(hw_ep->regs, MUSB_RXCSR);
942 * Service the default endpoint (ep0) as host.
943 * Return TRUE until it's time to start the status stage.
945 static int musb_h_ep0_continue(struct musb *musb,
946 u16 len, struct urb *urb)
949 u8 *fifo_dest = NULL;
951 struct musb_hw_ep *hw_ep = musb->control_ep;
952 struct musb_qh *qh = hw_ep->in_qh;
953 struct usb_ctrlrequest *request;
955 switch (musb->ep0_stage) {
957 fifo_dest = urb->transfer_buffer + urb->actual_length;
958 fifo_count = min(len, ((u16) (urb->transfer_buffer_length
959 - urb->actual_length)));
960 if (fifo_count < len)
961 urb->status = -EOVERFLOW;
963 musb_read_fifo(hw_ep, fifo_count, fifo_dest);
965 urb->actual_length += fifo_count;
966 if (len < qh->maxpacket) {
967 /* always terminate on short read; it's
968 * rarely reported as an error.
970 } else if (urb->actual_length <
971 urb->transfer_buffer_length)
975 request = (struct usb_ctrlrequest *) urb->setup_packet;
977 if (!request->wLength) {
978 DBG(4, "start no-DATA\n");
980 } else if (request->bRequestType & USB_DIR_IN) {
981 DBG(4, "start IN-DATA\n");
982 musb->ep0_stage = MGC_END0_IN;
986 DBG(4, "start OUT-DATA\n");
987 musb->ep0_stage = MGC_END0_OUT;
992 fifo_count = min(qh->maxpacket, ((u16)
993 (urb->transfer_buffer_length
994 - urb->actual_length)));
997 fifo_dest = (u8 *) (urb->transfer_buffer
998 + urb->actual_length);
999 DBG(3, "Sending %d bytes to %p\n",
1000 fifo_count, fifo_dest);
1001 musb_write_fifo(hw_ep, fifo_count, fifo_dest);
1003 urb->actual_length += fifo_count;
1008 ERR("bogus ep0 stage %d\n", musb->ep0_stage);
1016 * Handle default endpoint interrupt as host. Only called in IRQ time
1017 * from the LinuxIsr() interrupt service routine.
1019 * called with controller irqlocked
1021 irqreturn_t musb_h_ep0_irq(struct musb *musb)
1026 void __iomem *mbase = musb->mregs;
1027 struct musb_hw_ep *hw_ep = musb->control_ep;
1028 void __iomem *epio = hw_ep->regs;
1029 struct musb_qh *qh = hw_ep->in_qh;
1030 u8 complete = FALSE;
1031 irqreturn_t retval = IRQ_NONE;
1033 /* ep0 only has one queue, "in" */
1036 musb_ep_select(mbase, 0);
1037 csr = musb_readw(epio, MUSB_CSR0);
1038 len = (csr & MUSB_CSR0_RXPKTRDY)
1039 ? musb_readb(epio, MUSB_COUNT0)
1042 DBG(4, "<== csr0 %04x, qh %p, count %d, urb %p, stage %d\n",
1043 csr, qh, len, urb, musb->ep0_stage);
1045 /* if we just did status stage, we are done */
1046 if (MGC_END0_STATUS == musb->ep0_stage) {
1047 retval = IRQ_HANDLED;
1051 /* prepare status */
1052 if (csr & MUSB_CSR0_H_RXSTALL) {
1053 DBG(6, "STALLING ENDPOINT\n");
1056 } else if (csr & MUSB_CSR0_H_ERROR) {
1057 DBG(2, "no response, csr0 %04x\n", csr);
1060 } else if (csr & MUSB_CSR0_H_NAKTIMEOUT) {
1061 DBG(2, "control NAK timeout\n");
1063 /* NOTE: this code path would be a good place to PAUSE a
1064 * control transfer, if another one is queued, so that
1065 * ep0 is more likely to stay busy.
1067 * if (qh->ring.next != &musb->control), then
1068 * we have a candidate... NAKing is *NOT* an error
1070 musb_writew(epio, MUSB_CSR0, 0);
1071 retval = IRQ_HANDLED;
1075 DBG(6, "aborting\n");
1076 retval = IRQ_HANDLED;
1078 urb->status = status;
1081 /* use the proper sequence to abort the transfer */
1082 if (csr & MUSB_CSR0_H_REQPKT) {
1083 csr &= ~MUSB_CSR0_H_REQPKT;
1084 musb_writew(epio, MUSB_CSR0, csr);
1085 csr &= ~MUSB_CSR0_H_NAKTIMEOUT;
1086 musb_writew(epio, MUSB_CSR0, csr);
1088 csr |= MUSB_CSR0_FLUSHFIFO;
1089 musb_writew(epio, MUSB_CSR0, csr);
1090 musb_writew(epio, MUSB_CSR0, csr);
1091 csr &= ~MUSB_CSR0_H_NAKTIMEOUT;
1092 musb_writew(epio, MUSB_CSR0, csr);
1095 musb_writeb(epio, MUSB_NAKLIMIT0, 0);
1098 musb_writew(epio, MUSB_CSR0, 0);
1101 if (unlikely(!urb)) {
1102 /* stop endpoint since we have no place for its data, this
1103 * SHOULD NEVER HAPPEN! */
1104 ERR("no URB for end 0\n");
1106 musb_writew(epio, MUSB_CSR0, MUSB_CSR0_FLUSHFIFO);
1107 musb_writew(epio, MUSB_CSR0, MUSB_CSR0_FLUSHFIFO);
1108 musb_writew(epio, MUSB_CSR0, 0);
1114 /* call common logic and prepare response */
1115 if (musb_h_ep0_continue(musb, len, urb)) {
1116 /* more packets required */
1117 csr = (MGC_END0_IN == musb->ep0_stage)
1118 ? MUSB_CSR0_H_REQPKT : MUSB_CSR0_TXPKTRDY;
1120 /* data transfer complete; perform status phase */
1121 if (usb_pipeout(urb->pipe)
1122 || !urb->transfer_buffer_length)
1123 csr = MUSB_CSR0_H_STATUSPKT
1124 | MUSB_CSR0_H_REQPKT;
1126 csr = MUSB_CSR0_H_STATUSPKT
1127 | MUSB_CSR0_TXPKTRDY;
1129 /* flag status stage */
1130 musb->ep0_stage = MGC_END0_STATUS;
1132 DBG(5, "ep0 STATUS, csr %04x\n", csr);
1135 musb_writew(epio, MUSB_CSR0, csr);
1136 retval = IRQ_HANDLED;
1138 musb->ep0_stage = MGC_END0_IDLE;
1140 /* call completion handler if done */
1142 musb_advance_schedule(musb, urb, hw_ep, 1);
1148 #ifdef CONFIG_USB_INVENTRA_DMA
1150 /* Host side TX (OUT) using Mentor DMA works as follows:
1152 - if queue was empty, Program Endpoint
1153 - ... which starts DMA to fifo in mode 1 or 0
1155 DMA Isr (transfer complete) -> TxAvail()
1156 - Stop DMA (~DmaEnab) (<--- Alert ... currently happens
1157 only in musb_cleanup_urb)
1158 - TxPktRdy has to be set in mode 0 or for
1159 short packets in mode 1.
1164 /* Service a Tx-Available or dma completion irq for the endpoint */
1165 void musb_host_tx(struct musb *musb, u8 epnum)
1173 struct musb_hw_ep *hw_ep = musb->endpoints + epnum;
1174 void __iomem *epio = hw_ep->regs;
1175 struct musb_qh *qh = hw_ep->out_qh;
1177 void __iomem *mbase = musb->mregs;
1178 struct dma_channel *dma;
1182 musb_ep_select(mbase, epnum);
1183 tx_csr = musb_readw(epio, MUSB_TXCSR);
1185 /* with CPPI, DMA sometimes triggers "extra" irqs */
1187 DBG(4, "extra TX%d ready, csr %04x\n", epnum, tx_csr);
1192 dma = is_dma_capable() ? hw_ep->tx_channel : NULL;
1193 DBG(4, "OUT/TX%d end, csr %04x%s\n", epnum, tx_csr,
1194 dma ? ", dma" : "");
1196 /* check for errors */
1197 if (tx_csr & MUSB_TXCSR_H_RXSTALL) {
1198 /* dma was disabled, fifo flushed */
1199 DBG(3, "TX end %d stall\n", epnum);
1201 /* stall; record URB status */
1204 } else if (tx_csr & MUSB_TXCSR_H_ERROR) {
1205 /* (NON-ISO) dma was disabled, fifo flushed */
1206 DBG(3, "TX 3strikes on ep=%d\n", epnum);
1208 status = -ETIMEDOUT;
1210 } else if (tx_csr & MUSB_TXCSR_H_NAKTIMEOUT) {
1211 DBG(6, "TX end=%d device not responding\n", epnum);
1213 /* NOTE: this code path would be a good place to PAUSE a
1214 * transfer, if there's some other (nonperiodic) tx urb
1215 * that could use this fifo. (dma complicates it...)
1217 * if (bulk && qh->ring.next != &musb->out_bulk), then
1218 * we have a candidate... NAKing is *NOT* an error
1220 musb_ep_select(mbase, epnum);
1221 musb_writew(epio, MUSB_CSR0,
1222 MUSB_TXCSR_H_WZC_BITS
1223 | MUSB_TXCSR_TXPKTRDY);
1228 if (dma_channel_status(dma) == MGC_DMA_STATUS_BUSY) {
1229 dma->status = MGC_DMA_STATUS_CORE_ABORT;
1230 (void) musb->dma_controller->channel_abort(dma);
1233 /* do the proper sequence to abort the transfer in the
1234 * usb core; the dma engine should already be stopped.
1236 musb_h_tx_flush_fifo(hw_ep);
1237 tx_csr &= ~(MUSB_TXCSR_AUTOSET
1238 | MUSB_TXCSR_DMAENAB
1239 | MUSB_TXCSR_H_ERROR
1240 | MUSB_TXCSR_H_RXSTALL
1241 | MUSB_TXCSR_H_NAKTIMEOUT
1244 musb_ep_select(mbase, epnum);
1245 musb_writew(epio, MUSB_TXCSR, tx_csr);
1246 /* REVISIT may need to clear FLUSHFIFO ... */
1247 musb_writew(epio, MUSB_TXCSR, tx_csr);
1248 musb_writeb(epio, MUSB_TXINTERVAL, 0);
1253 /* second cppi case */
1254 if (dma_channel_status(dma) == MGC_DMA_STATUS_BUSY) {
1255 DBG(4, "extra TX%d ready, csr %04x\n", epnum, tx_csr);
1260 /* REVISIT this looks wrong... */
1261 if (!status || dma || usb_pipeisoc(pipe)) {
1263 wLength = dma->actual_len;
1265 wLength = qh->segsize;
1266 qh->offset += wLength;
1268 if (usb_pipeisoc(pipe)) {
1269 struct usb_iso_packet_descriptor *d;
1271 d = urb->iso_frame_desc + qh->iso_idx;
1272 d->actual_length = qh->segsize;
1273 if (++qh->iso_idx >= urb->number_of_packets) {
1277 buf = urb->transfer_buffer + d->offset;
1278 wLength = d->length;
1283 /* see if we need to send more data, or ZLP */
1284 if (qh->segsize < qh->maxpacket)
1286 else if (qh->offset == urb->transfer_buffer_length
1287 && !(urb-> transfer_flags
1291 buf = urb->transfer_buffer
1293 wLength = urb->transfer_buffer_length
1299 /* urb->status != -EINPROGRESS means request has been faulted,
1300 * so we must abort this transfer after cleanup
1302 if (urb->status != -EINPROGRESS) {
1305 status = urb->status;
1310 urb->status = status;
1311 urb->actual_length = qh->offset;
1312 musb_advance_schedule(musb, urb, hw_ep, USB_DIR_OUT);
1314 } else if (!(tx_csr & MUSB_TXCSR_DMAENAB)) {
1317 /* REVISIT: some docs say that when hw_ep->tx_double_buffered,
1318 * (and presumably, fifo is not half-full) we should write TWO
1319 * packets before updating TXCSR ... other docs disagree ...
1321 /* PIO: start next packet in this URB */
1322 wLength = min(qh->maxpacket, (u16) wLength);
1323 musb_write_fifo(hw_ep, wLength, buf);
1324 qh->segsize = wLength;
1326 musb_ep_select(mbase, epnum);
1327 musb_writew(epio, MUSB_TXCSR,
1328 MUSB_TXCSR_H_WZC_BITS | MUSB_TXCSR_TXPKTRDY);
1330 DBG(1, "not complete, but dma enabled?\n");
1337 #ifdef CONFIG_USB_INVENTRA_DMA
1339 /* Host side RX (IN) using Mentor DMA works as follows:
1341 - if queue was empty, ProgramEndpoint
1342 - first IN token is sent out (by setting ReqPkt)
1343 LinuxIsr -> RxReady()
1344 /\ => first packet is received
1345 | - Set in mode 0 (DmaEnab, ~ReqPkt)
1346 | -> DMA Isr (transfer complete) -> RxReady()
1347 | - Ack receive (~RxPktRdy), turn off DMA (~DmaEnab)
1348 | - if urb not complete, send next IN token (ReqPkt)
1349 | | else complete urb.
1351 ---------------------------
1353 * Nuances of mode 1:
1354 * For short packets, no ack (+RxPktRdy) is sent automatically
1355 * (even if AutoClear is ON)
1356 * For full packets, ack (~RxPktRdy) and next IN token (+ReqPkt) is sent
1357 * automatically => major problem, as collecting the next packet becomes
1358 * difficult. Hence mode 1 is not used.
1361 * All we care about at this driver level is that
1362 * (a) all URBs terminate with REQPKT cleared and fifo(s) empty;
1363 * (b) termination conditions are: short RX, or buffer full;
1364 * (c) fault modes include
1365 * - iff URB_SHORT_NOT_OK, short RX status is -EREMOTEIO.
1366 * (and that endpoint's dma queue stops immediately)
1367 * - overflow (full, PLUS more bytes in the terminal packet)
1369 * So for example, usb-storage sets URB_SHORT_NOT_OK, and would
1370 * thus be a great candidate for using mode 1 ... for all but the
1371 * last packet of one URB's transfer.
1377 * Service an RX interrupt for the given IN endpoint; docs cover bulk, iso,
1378 * and high-bandwidth IN transfer cases.
1380 void musb_host_rx(struct musb *musb, u8 epnum)
1383 struct musb_hw_ep *hw_ep = musb->endpoints + epnum;
1384 void __iomem *epio = hw_ep->regs;
1385 struct musb_qh *qh = hw_ep->in_qh;
1387 void __iomem *mbase = musb->mregs;
1393 struct dma_channel *dma;
1395 musb_ep_select(mbase, epnum);
1398 dma = is_dma_capable() ? hw_ep->rx_channel : NULL;
1402 val = rx_csr = musb_readw(epio, MUSB_RXCSR);
1404 if (unlikely(!urb)) {
1405 /* REVISIT -- THIS SHOULD NEVER HAPPEN ... but, at least
1406 * usbtest #11 (unlinks) triggers it regularly, sometimes
1407 * with fifo full. (Only with DMA??)
1409 DBG(3, "BOGUS RX%d ready, csr %04x, count %d\n", epnum, val,
1410 musb_readw(epio, MUSB_RXCOUNT));
1411 musb_h_flush_rxfifo(hw_ep, MUSB_RXCSR_CLRDATATOG);
1417 DBG(5, "<== hw %d rxcsr %04x, urb actual %d (+dma %zd)\n",
1418 epnum, rx_csr, urb->actual_length,
1419 dma ? dma->actual_len : 0);
1421 /* check for errors, concurrent stall & unlink is not really
1423 if (rx_csr & MUSB_RXCSR_H_RXSTALL) {
1424 DBG(3, "RX end %d STALL\n", epnum);
1426 /* stall; record URB status */
1429 } else if (rx_csr & MUSB_RXCSR_H_ERROR) {
1430 DBG(3, "end %d RX proto error\n", epnum);
1433 musb_writeb(epio, MUSB_RXINTERVAL, 0);
1435 } else if (rx_csr & MUSB_RXCSR_DATAERROR) {
1437 if (USB_ENDPOINT_XFER_ISOC != qh->type) {
1438 /* NOTE this code path would be a good place to PAUSE a
1439 * transfer, if there's some other (nonperiodic) rx urb
1440 * that could use this fifo. (dma complicates it...)
1442 * if (bulk && qh->ring.next != &musb->in_bulk), then
1443 * we have a candidate... NAKing is *NOT* an error
1445 DBG(6, "RX end %d NAK timeout\n", epnum);
1446 musb_ep_select(mbase, epnum);
1447 musb_writew(epio, MUSB_RXCSR,
1448 MUSB_RXCSR_H_WZC_BITS
1449 | MUSB_RXCSR_H_REQPKT);
1453 DBG(4, "RX end %d ISO data error\n", epnum);
1454 /* packet error reported later */
1459 /* faults abort the transfer */
1461 /* clean up dma and collect transfer count */
1462 if (dma_channel_status(dma) == MGC_DMA_STATUS_BUSY) {
1463 dma->status = MGC_DMA_STATUS_CORE_ABORT;
1464 (void) musb->dma_controller->channel_abort(dma);
1465 xfer_len = dma->actual_len;
1467 musb_h_flush_rxfifo(hw_ep, 0);
1468 musb_writeb(epio, MUSB_RXINTERVAL, 0);
1473 if (unlikely(dma_channel_status(dma) == MGC_DMA_STATUS_BUSY)) {
1474 /* SHOULD NEVER HAPPEN ... but at least DaVinci has done it */
1475 ERR("RX%d dma busy, csr %04x\n", epnum, rx_csr);
1479 /* thorough shutdown for now ... given more precise fault handling
1480 * and better queueing support, we might keep a DMA pipeline going
1481 * while processing this irq for earlier completions.
1484 /* FIXME this is _way_ too much in-line logic for Mentor DMA */
1486 #ifndef CONFIG_USB_INVENTRA_DMA
1487 if (rx_csr & MUSB_RXCSR_H_REQPKT) {
1488 /* REVISIT this happened for a while on some short reads...
1489 * the cleanup still needs investigation... looks bad...
1490 * and also duplicates dma cleanup code above ... plus,
1491 * shouldn't this be the "half full" double buffer case?
1493 if (dma_channel_status(dma) == MGC_DMA_STATUS_BUSY) {
1494 dma->status = MGC_DMA_STATUS_CORE_ABORT;
1495 (void) musb->dma_controller->channel_abort(dma);
1496 xfer_len = dma->actual_len;
1500 DBG(2, "RXCSR%d %04x, reqpkt, len %zd%s\n", epnum, rx_csr,
1501 xfer_len, dma ? ", dma" : "");
1502 rx_csr &= ~MUSB_RXCSR_H_REQPKT;
1504 musb_ep_select(mbase, epnum);
1505 musb_writew(epio, MUSB_RXCSR,
1506 MUSB_RXCSR_H_WZC_BITS | rx_csr);
1509 if (dma && (rx_csr & MUSB_RXCSR_DMAENAB)) {
1510 xfer_len = dma->actual_len;
1512 val &= ~(MUSB_RXCSR_DMAENAB
1513 | MUSB_RXCSR_H_AUTOREQ
1514 | MUSB_RXCSR_AUTOCLEAR
1515 | MUSB_RXCSR_RXPKTRDY);
1516 musb_writew(hw_ep->regs, MUSB_RXCSR, val);
1518 #ifdef CONFIG_USB_INVENTRA_DMA
1519 /* done if urb buffer is full or short packet is recd */
1520 done = ((urb->actual_length + xfer_len) >=
1521 urb->transfer_buffer_length)
1522 || (dma->actual_len & (qh->maxpacket - 1));
1524 /* send IN token for next packet, without AUTOREQ */
1526 val |= MUSB_RXCSR_H_REQPKT;
1527 musb_writew(epio, MUSB_RXCSR,
1528 MUSB_RXCSR_H_WZC_BITS | val);
1531 DBG(4, "ep %d dma %s, rxcsr %04x, rxcount %d\n", epnum,
1532 done ? "off" : "reset",
1533 musb_readw(epio, MUSB_RXCSR),
1534 musb_readw(epio, MUSB_RXCOUNT));
1538 } else if (urb->status == -EINPROGRESS) {
1539 /* if no errors, be sure a packet is ready for unloading */
1540 if (unlikely(!(rx_csr & MUSB_RXCSR_RXPKTRDY))) {
1542 ERR("Rx interrupt with no errors or packet!\n");
1544 // FIXME this is another "SHOULD NEVER HAPPEN"
1547 /* do the proper sequence to abort the transfer */
1548 musb_ep_select(mbase, epnum);
1549 val &= ~MUSB_RXCSR_H_REQPKT;
1550 musb_writew(epio, MUSB_RXCSR, val);
1554 /* we are expecting IN packets */
1555 #ifdef CONFIG_USB_INVENTRA_DMA
1557 struct dma_controller *c;
1561 rx_count = musb_readw(epio, MUSB_RXCOUNT);
1563 DBG(2, "RX%d count %d, buffer 0x%x len %d/%d\n",
1566 + urb->actual_length,
1568 urb->transfer_buffer_length);
1570 c = musb->dma_controller;
1572 dma->desired_mode = 0;
1574 /* because of the issue below, mode 1 will
1575 * only rarely behave with correct semantics.
1577 if ((urb->transfer_flags &
1579 && (urb->transfer_buffer_length -
1582 dma->desired_mode = 1;
1585 /* Disadvantage of using mode 1:
1586 * It's basically usable only for mass storage class; essentially all
1587 * other protocols also terminate transfers on short packets.
1590 * An extra IN token is sent at the end of the transfer (due to AUTOREQ)
1591 * If you try to use mode 1 for (transfer_buffer_length - 512), and try
1592 * to use the extra IN token to grab the last packet using mode 0, then
1593 * the problem is that you cannot be sure when the device will send the
1594 * last packet and RxPktRdy set. Sometimes the packet is recd too soon
1595 * such that it gets lost when RxCSR is re-set at the end of the mode 1
1596 * transfer, while sometimes it is recd just a little late so that if you
1597 * try to configure for mode 0 soon after the mode 1 transfer is
1598 * completed, you will find rxcount 0. Okay, so you might think why not
1599 * wait for an interrupt when the pkt is recd. Well, you won't get any!
1602 val = musb_readw(epio, MUSB_RXCSR);
1603 val &= ~MUSB_RXCSR_H_REQPKT;
1605 if (dma->desired_mode == 0)
1606 val &= ~MUSB_RXCSR_H_AUTOREQ;
1608 val |= MUSB_RXCSR_H_AUTOREQ;
1609 val |= MUSB_RXCSR_AUTOCLEAR | MUSB_RXCSR_DMAENAB;
1611 musb_writew(epio, MUSB_RXCSR,
1612 MUSB_RXCSR_H_WZC_BITS | val);
1614 /* REVISIT if when actual_length != 0,
1615 * transfer_buffer_length needs to be
1618 status = c->channel_program(
1622 + urb->actual_length,
1623 (dma->desired_mode == 0)
1625 : urb->transfer_buffer_length);
1628 c->channel_release(dma);
1629 dma = hw_ep->rx_channel = NULL;
1630 /* REVISIT reset CSR */
1633 #endif /* Mentor DMA */
1636 done = musb_host_packet_rx(musb, urb,
1638 DBG(6, "read %spacket\n", done ? "last " : "");
1643 urb->actual_length += xfer_len;
1644 qh->offset += xfer_len;
1646 if (urb->status == -EINPROGRESS)
1647 urb->status = status;
1648 musb_advance_schedule(musb, urb, hw_ep, USB_DIR_IN);
1652 /* schedule nodes correspond to peripheral endpoints, like an OHCI QH.
1653 * the software schedule associates multiple such nodes with a given
1654 * host side hardware endpoint + direction; scheduling may activate
1655 * that hardware endpoint.
1657 static int musb_schedule(
1664 int best_end, epnum;
1665 struct musb_hw_ep *hw_ep = NULL;
1666 struct list_head *head = NULL;
1668 /* use fixed hardware for control and bulk */
1670 case USB_ENDPOINT_XFER_CONTROL:
1671 head = &musb->control;
1672 hw_ep = musb->control_ep;
1674 case USB_ENDPOINT_XFER_BULK:
1675 hw_ep = musb->bulk_ep;
1677 head = &musb->in_bulk;
1679 head = &musb->out_bulk;
1683 idle = list_empty(head);
1684 list_add_tail(&qh->ring, head);
1688 /* else, periodic transfers get muxed to other endpoints */
1690 /* FIXME this doesn't consider direction, so it can only
1691 * work for one half of the endpoint hardware, and assumes
1692 * the previous cases handled all non-shared endpoints...
1695 /* we know this qh hasn't been scheduled, so all we need to do
1696 * is choose which hardware endpoint to put it on ...
1698 * REVISIT what we really want here is a regular schedule tree
1699 * like e.g. OHCI uses, but for now musb->periodic is just an
1700 * array of the _single_ logical endpoint associated with a
1701 * given physical one (identity mapping logical->physical).
1703 * that simplistic approach makes TT scheduling a lot simpler;
1704 * there is none, and thus none of its complexity...
1709 for (epnum = 1; epnum < musb->nr_endpoints; epnum++) {
1712 if (musb->periodic[epnum])
1714 hw_ep = &musb->endpoints[epnum];
1715 if (hw_ep == musb->bulk_ep)
1719 diff = hw_ep->max_packet_sz_rx - qh->maxpacket;
1721 diff = hw_ep->max_packet_sz_tx - qh->maxpacket;
1723 if (diff > 0 && best_diff > diff) {
1732 hw_ep = musb->endpoints + best_end;
1733 musb->periodic[best_end] = qh;
1734 DBG(4, "qh %p periodic slot %d\n", qh, best_end);
1737 qh->hep->hcpriv = qh;
1739 musb_start_urb(musb, is_in, qh);
1743 static int musb_urb_enqueue(
1744 struct usb_hcd *hcd,
1745 struct usb_host_endpoint *hep,
1749 unsigned long flags;
1750 struct musb *musb = hcd_to_musb(hcd);
1751 struct musb_qh *qh = hep->hcpriv;
1752 struct usb_endpoint_descriptor *epd = &hep->desc;
1757 /* host role must be active */
1758 if (!is_host_active(musb) || !musb->is_active)
1761 /* DMA mapping was already done, if needed, and this urb is on
1762 * hep->urb_list ... so there's little to do unless hep wasn't
1763 * yet scheduled onto a live qh.
1765 * REVISIT best to keep hep->hcpriv valid until the endpoint gets
1766 * disabled, testing for empty qh->ring and avoiding qh setup costs
1767 * except for the first urb queued after a config change.
1774 /* Allocate and initialize qh, minimizing the work done each time
1775 * hw_ep gets reprogrammed, or with irqs blocked. Then schedule it.
1777 * REVISIT consider a dedicated qh kmem_cache, so it's harder
1778 * for bugs in other kernel code to break this driver...
1780 qh = kzalloc(sizeof *qh, mem_flags);
1786 INIT_LIST_HEAD(&qh->ring);
1789 qh->maxpacket = le16_to_cpu(epd->wMaxPacketSize);
1791 /* no high bandwidth support yet */
1792 if (qh->maxpacket & ~0x7ff) {
1797 qh->epnum = epd->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
1798 qh->type = epd->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
1800 /* NOTE: urb->dev->devnum is wrong during SET_ADDRESS */
1801 qh->addr_reg = (u8) usb_pipedevice(urb->pipe);
1803 /* precompute rxtype/txtype/type0 register */
1804 type_reg = (qh->type << 4) | qh->epnum;
1805 switch (urb->dev->speed) {
1809 case USB_SPEED_FULL:
1815 qh->type_reg = type_reg;
1817 /* precompute rxinterval/txinterval register */
1818 interval = min((u8)16, epd->bInterval); /* log encoding */
1820 case USB_ENDPOINT_XFER_INT:
1821 /* fullspeed uses linear encoding */
1822 if (USB_SPEED_FULL == urb->dev->speed) {
1823 interval = epd->bInterval;
1828 case USB_ENDPOINT_XFER_ISOC:
1829 /* iso always uses log encoding */
1832 /* REVISIT we actually want to use NAK limits, hinting to the
1833 * transfer scheduling logic to try some other qh, e.g. try
1836 * interval = (USB_SPEED_HIGH == urb->dev->speed) ? 16 : 2;
1838 * The downside of disabling this is that transfer scheduling
1839 * gets VERY unfair for nonperiodic transfers; a misbehaving
1840 * peripheral could make that hurt. Or for reads, one that's
1841 * perfectly normal: network and other drivers keep reads
1842 * posted at all times, having one pending for a week should
1843 * be perfectly safe.
1845 * The upside of disabling it is avoidng transfer scheduling
1846 * code to put this aside for while.
1850 qh->intv_reg = interval;
1852 /* precompute addressing for external hub/tt ports */
1853 if (musb->is_multipoint) {
1854 struct usb_device *parent = urb->dev->parent;
1856 if (parent != hcd->self.root_hub) {
1857 qh->h_addr_reg = (u8) parent->devnum;
1859 /* set up tt info if needed */
1861 qh->h_port_reg = (u8) urb->dev->ttport;
1862 qh->h_addr_reg |= 0x80;
1867 /* invariant: hep->hcpriv is null OR the qh that's already scheduled.
1868 * until we get real dma queues (with an entry for each urb/buffer),
1869 * we only have work to do in the former case.
1871 spin_lock_irqsave(&musb->lock, flags);
1873 /* some concurrent activity submitted another urb to hep...
1874 * odd, rare, error prone, but legal.
1879 status = musb_schedule(musb, qh,
1880 epd->bEndpointAddress & USB_ENDPOINT_DIR_MASK);
1884 /* FIXME set urb->start_frame for iso/intr, it's tested in
1885 * musb_start_urb(), but otherwise only konicawc cares ...
1888 spin_unlock_irqrestore(&musb->lock, flags);
1898 * abort a transfer that's at the head of a hardware queue.
1899 * called with controller locked, irqs blocked
1900 * that hardware queue advances to the next transfer, unless prevented
1902 static int musb_cleanup_urb(struct urb *urb, struct musb_qh *qh, int is_in)
1904 struct musb_hw_ep *ep = qh->hw_ep;
1905 void __iomem *epio = ep->regs;
1906 unsigned hw_end = ep->epnum;
1907 void __iomem *regs = ep->musb->mregs;
1911 musb_ep_select(regs, hw_end);
1913 if (is_dma_capable()) {
1914 struct dma_channel *dma;
1916 dma = is_in ? ep->rx_channel : ep->tx_channel;
1918 status = ep->musb->dma_controller->channel_abort(dma);
1920 "abort %cX%d DMA for urb %p --> %d\n",
1921 is_in ? 'R' : 'T', ep->epnum,
1923 urb->actual_length += dma->actual_len;
1927 /* turn off DMA requests, discard state, stop polling ... */
1929 /* giveback saves bulk toggle */
1930 csr = musb_h_flush_rxfifo(ep, 0);
1932 /* REVISIT we still get an irq; should likely clear the
1933 * endpoint's irq status here to avoid bogus irqs.
1934 * clearing that status is platform-specific...
1937 musb_h_tx_flush_fifo(ep);
1938 csr = musb_readw(epio, MUSB_TXCSR);
1939 csr &= ~( MUSB_TXCSR_AUTOSET
1940 | MUSB_TXCSR_DMAENAB
1941 | MUSB_TXCSR_H_RXSTALL
1942 | MUSB_TXCSR_H_NAKTIMEOUT
1943 | MUSB_TXCSR_H_ERROR
1944 | MUSB_TXCSR_TXPKTRDY
1946 musb_writew(epio, MUSB_TXCSR, csr);
1947 /* REVISIT may need to clear FLUSHFIFO ... */
1948 musb_writew(epio, MUSB_TXCSR, csr);
1949 /* flush cpu writebuffer */
1950 csr = musb_readw(epio, MUSB_TXCSR);
1953 musb_advance_schedule(ep->musb, urb, ep, is_in);
1957 static int musb_urb_dequeue(struct usb_hcd *hcd, struct urb *urb)
1959 struct musb *musb = hcd_to_musb(hcd);
1961 struct list_head *sched;
1963 unsigned long flags;
1964 int status = -ENOENT;
1966 DBG(4, "urb=%p, dev%d ep%d%s\n", urb,
1967 usb_pipedevice(urb->pipe),
1968 usb_pipeendpoint(urb->pipe),
1969 usb_pipein(urb->pipe) ? "in" : "out");
1971 spin_lock_irqsave(&musb->lock, flags);
1973 /* make sure the urb is still queued and not completed */
1974 spin_lock(&urb->lock);
1977 struct usb_host_endpoint *hep;
1980 list_for_each_entry(tmp, &hep->urb_list, urb_list) {
1987 spin_unlock(&urb->lock);
1989 /* already completed */
1995 /* still queued but not found on the list */
1999 /* Any URB not actively programmed into endpoint hardware can be
2000 * immediately given back. Such an URB must be at the head of its
2001 * endpoint queue, unless someday we get real DMA queues. And even
2002 * then, it might not be known to the hardware...
2004 * Otherwise abort current transfer, pending dma, etc.; urb->status
2005 * has already been updated. This is a synchronous abort; it'd be
2006 * OK to hold off until after some IRQ, though.
2008 if (!qh->is_ready || urb->urb_list.prev != &qh->hep->urb_list)
2009 status = -EINPROGRESS;
2012 case USB_ENDPOINT_XFER_CONTROL:
2013 sched = &musb->control;
2015 case USB_ENDPOINT_XFER_BULK:
2016 if (usb_pipein(urb->pipe))
2017 sched = &musb->in_bulk;
2019 sched = &musb->out_bulk;
2022 /* REVISIT when we get a schedule tree, periodic
2023 * transfers won't always be at the head of a
2024 * singleton queue...
2031 /* NOTE: qh is invalid unless !list_empty(&hep->urb_list) */
2032 if (status < 0 || (sched && qh != first_qh(sched))) {
2033 int ready = qh->is_ready;
2037 __musb_giveback(musb, urb, 0);
2038 qh->is_ready = ready;
2040 status = musb_cleanup_urb(urb, qh, urb->pipe & USB_DIR_IN);
2042 spin_unlock_irqrestore(&musb->lock, flags);
2046 /* disable an endpoint */
2048 musb_h_disable(struct usb_hcd *hcd, struct usb_host_endpoint *hep)
2050 u8 epnum = hep->desc.bEndpointAddress;
2051 unsigned long flags;
2052 struct musb *musb = hcd_to_musb(hcd);
2053 u8 is_in = epnum & USB_DIR_IN;
2054 struct musb_qh *qh = hep->hcpriv;
2055 struct urb *urb, *tmp;
2056 struct list_head *sched;
2061 spin_lock_irqsave(&musb->lock, flags);
2064 case USB_ENDPOINT_XFER_CONTROL:
2065 sched = &musb->control;
2067 case USB_ENDPOINT_XFER_BULK:
2069 sched = &musb->in_bulk;
2071 sched = &musb->out_bulk;
2074 /* REVISIT when we get a schedule tree, periodic transfers
2075 * won't always be at the head of a singleton queue...
2081 /* NOTE: qh is invalid unless !list_empty(&hep->urb_list) */
2083 /* kick first urb off the hardware, if needed */
2085 if (!sched || qh == first_qh(sched)) {
2088 /* make software (then hardware) stop ASAP */
2089 spin_lock(&urb->lock);
2090 if (urb->status == -EINPROGRESS)
2091 urb->status = -ESHUTDOWN;
2092 spin_unlock(&urb->lock);
2095 musb_cleanup_urb(urb, qh, urb->pipe & USB_DIR_IN);
2099 /* then just nuke all the others */
2100 list_for_each_entry_safe_from(urb, tmp, &hep->urb_list, urb_list)
2101 musb_giveback(qh, urb, -ESHUTDOWN);
2103 spin_unlock_irqrestore(&musb->lock, flags);
2106 static int musb_h_get_frame_number(struct usb_hcd *hcd)
2108 struct musb *musb = hcd_to_musb(hcd);
2110 return musb_readw(musb->mregs, MUSB_FRAME);
2113 static int musb_h_start(struct usb_hcd *hcd)
2115 struct musb *musb = hcd_to_musb(hcd);
2117 /* NOTE: musb_start() is called when the hub driver turns
2118 * on port power, or when (OTG) peripheral starts.
2120 hcd->state = HC_STATE_RUNNING;
2121 musb->port1_status = 0;
2125 static void musb_h_stop(struct usb_hcd *hcd)
2127 musb_stop(hcd_to_musb(hcd));
2128 hcd->state = HC_STATE_HALT;
2131 static int musb_bus_suspend(struct usb_hcd *hcd)
2133 struct musb *musb = hcd_to_musb(hcd);
2135 if (is_host_active(musb) && musb->is_active)
2141 static int musb_bus_resume(struct usb_hcd *hcd)
2143 /* resuming child port does the work */
2147 const struct hc_driver musb_hc_driver = {
2148 .description = "musb-hcd",
2149 .product_desc = "MUSB HDRC host driver",
2150 .hcd_priv_size = sizeof (struct musb),
2151 .flags = HCD_USB2 | HCD_MEMORY,
2153 /* not using irq handler or reset hooks from usbcore, since
2154 * those must be shared with peripheral code for OTG configs
2157 .start = musb_h_start,
2158 .stop = musb_h_stop,
2160 .get_frame_number = musb_h_get_frame_number,
2162 .urb_enqueue = musb_urb_enqueue,
2163 .urb_dequeue = musb_urb_dequeue,
2164 .endpoint_disable = musb_h_disable,
2166 .hub_status_data = musb_hub_status_data,
2167 .hub_control = musb_hub_control,
2168 .bus_suspend = musb_bus_suspend,
2169 .bus_resume = musb_bus_resume,
2170 // .start_port_reset = NULL,
2171 // .hub_irq_enable = NULL,