Full support for Ginger Console
[linux-ginger.git] / drivers / usb / musb / musb_host.c
blob67cbe24209651b00bf1e15bf49e9706eccc9f488
1 /*
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
21 * 02110-1301 USA
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/init.h>
43 #include <linux/list.h>
45 #include "musb_core.h"
46 #include "musb_host.h"
49 /* MUSB HOST status 22-mar-2006
51 * - There's still lots of partial code duplication for fault paths, so
52 * they aren't handled as consistently as they need to be.
54 * - PIO mostly behaved when last tested.
55 * + including ep0, with all usbtest cases 9, 10
56 * + usbtest 14 (ep0out) doesn't seem to run at all
57 * + double buffered OUT/TX endpoints saw stalls(!) with certain usbtest
58 * configurations, but otherwise double buffering passes basic tests.
59 * + for 2.6.N, for N > ~10, needs API changes for hcd framework.
61 * - DMA (CPPI) ... partially behaves, not currently recommended
62 * + about 1/15 the speed of typical EHCI implementations (PCI)
63 * + RX, all too often reqpkt seems to misbehave after tx
64 * + TX, no known issues (other than evident silicon issue)
66 * - DMA (Mentor/OMAP) ...has at least toggle update problems
68 * - [23-feb-2009] minimal traffic scheduling to avoid bulk RX packet
69 * starvation ... nothing yet for TX, interrupt, or bulk.
71 * - Not tested with HNP, but some SRP paths seem to behave.
73 * NOTE 24-August-2006:
75 * - Bulk traffic finally uses both sides of hardware ep1, freeing up an
76 * extra endpoint for periodic use enabling hub + keybd + mouse. That
77 * mostly works, except that with "usbnet" it's easy to trigger cases
78 * with "ping" where RX loses. (a) ping to davinci, even "ping -f",
79 * fine; but (b) ping _from_ davinci, even "ping -c 1", ICMP RX loses
80 * although ARP RX wins. (That test was done with a full speed link.)
85 * NOTE on endpoint usage:
87 * CONTROL transfers all go through ep0. BULK ones go through dedicated IN
88 * and OUT endpoints ... hardware is dedicated for those "async" queue(s).
89 * (Yes, bulk _could_ use more of the endpoints than that, and would even
90 * benefit from it.)
92 * INTERUPPT and ISOCHRONOUS transfers are scheduled to the other endpoints.
93 * So far that scheduling is both dumb and optimistic: the endpoint will be
94 * "claimed" until its software queue is no longer refilled. No multiplexing
95 * of transfers between endpoints, or anything clever.
99 static void musb_ep_program(struct musb *musb, u8 epnum,
100 struct urb *urb, int is_out,
101 u8 *buf, u32 offset, u32 len);
104 * Clear TX fifo. Needed to avoid BABBLE errors.
106 static void musb_h_tx_flush_fifo(struct musb_hw_ep *ep)
108 void __iomem *epio = ep->regs;
109 u16 csr;
110 u16 lastcsr = 0;
111 int retries = 1000;
113 csr = musb_readw(epio, MUSB_TXCSR);
114 while (csr & MUSB_TXCSR_FIFONOTEMPTY) {
115 if (csr != lastcsr)
116 DBG(3, "Host TX FIFONOTEMPTY csr: %02x\n", csr);
117 lastcsr = csr;
118 csr |= MUSB_TXCSR_FLUSHFIFO;
119 musb_writew(epio, MUSB_TXCSR, csr);
120 csr = musb_readw(epio, MUSB_TXCSR);
121 if (WARN(retries-- < 1,
122 "Could not flush host TX%d fifo: csr: %04x\n",
123 ep->epnum, csr))
124 return;
125 mdelay(1);
129 static void musb_h_ep0_flush_fifo(struct musb_hw_ep *ep)
131 void __iomem *epio = ep->regs;
132 u16 csr;
133 int retries = 5;
135 /* scrub any data left in the fifo */
136 do {
137 csr = musb_readw(epio, MUSB_TXCSR);
138 if (!(csr & (MUSB_CSR0_TXPKTRDY | MUSB_CSR0_RXPKTRDY)))
139 break;
140 musb_writew(epio, MUSB_TXCSR, MUSB_CSR0_FLUSHFIFO);
141 csr = musb_readw(epio, MUSB_TXCSR);
142 udelay(10);
143 } while (--retries);
145 WARN(!retries, "Could not flush host TX%d fifo: csr: %04x\n",
146 ep->epnum, csr);
148 /* and reset for the next transfer */
149 musb_writew(epio, MUSB_TXCSR, 0);
153 * Start transmit. Caller is responsible for locking shared resources.
154 * musb must be locked.
156 static inline void musb_h_tx_start(struct musb_hw_ep *ep)
158 u16 txcsr;
160 /* NOTE: no locks here; caller should lock and select EP */
161 if (ep->epnum) {
162 txcsr = musb_readw(ep->regs, MUSB_TXCSR);
163 txcsr |= MUSB_TXCSR_TXPKTRDY | MUSB_TXCSR_H_WZC_BITS;
164 musb_writew(ep->regs, MUSB_TXCSR, txcsr);
165 } else {
166 txcsr = MUSB_CSR0_H_SETUPPKT | MUSB_CSR0_TXPKTRDY;
167 musb_writew(ep->regs, MUSB_CSR0, txcsr);
172 static inline void musb_h_tx_dma_start(struct musb_hw_ep *ep)
174 u16 txcsr;
176 /* NOTE: no locks here; caller should lock and select EP */
177 txcsr = musb_readw(ep->regs, MUSB_TXCSR);
178 txcsr |= MUSB_TXCSR_DMAENAB | MUSB_TXCSR_H_WZC_BITS;
179 if (is_cppi_enabled() || is_cppi41_enabled())
180 txcsr |= MUSB_TXCSR_DMAMODE;
181 musb_writew(ep->regs, MUSB_TXCSR, txcsr);
184 static void musb_ep_set_qh(struct musb_hw_ep *ep, int is_in, struct musb_qh *qh)
186 if (is_in != 0 || ep->is_shared_fifo)
187 ep->in_qh = qh;
188 if (is_in == 0 || ep->is_shared_fifo)
189 ep->out_qh = qh;
192 static struct musb_qh *musb_ep_get_qh(struct musb_hw_ep *ep, int is_in)
194 return is_in ? ep->in_qh : ep->out_qh;
198 * Start the URB at the front of an endpoint's queue
199 * end must be claimed from the caller.
201 * Context: controller locked, irqs blocked
203 static void
204 musb_start_urb(struct musb *musb, int is_in, struct musb_qh *qh)
206 u16 frame;
207 u32 len;
208 void __iomem *mbase = musb->mregs;
209 struct urb *urb = next_urb(qh);
210 void *buf = urb->transfer_buffer;
211 u32 offset = 0;
212 struct musb_hw_ep *hw_ep = qh->hw_ep;
213 unsigned pipe = urb->pipe;
214 u8 address = usb_pipedevice(pipe);
215 int epnum = hw_ep->epnum;
217 /* initialize software qh state */
218 qh->offset = 0;
219 qh->segsize = 0;
221 /* gather right source of data */
222 switch (qh->type) {
223 case USB_ENDPOINT_XFER_CONTROL:
224 /* control transfers always start with SETUP */
225 is_in = 0;
226 musb->ep0_stage = MUSB_EP0_START;
227 buf = urb->setup_packet;
228 len = 8;
229 break;
230 case USB_ENDPOINT_XFER_ISOC:
231 qh->iso_idx = 0;
232 qh->frame = 0;
233 offset = urb->iso_frame_desc[0].offset;
234 len = urb->iso_frame_desc[0].length;
235 break;
236 default: /* bulk, interrupt */
237 /* actual_length may be nonzero on retry paths */
238 buf = urb->transfer_buffer + urb->actual_length;
239 len = urb->transfer_buffer_length - urb->actual_length;
242 DBG(4, "qh %p urb %p dev%d ep%d%s%s, hw_ep %d, %p/%d\n",
243 qh, urb, address, qh->epnum,
244 is_in ? "in" : "out",
245 ({char *s; switch (qh->type) {
246 case USB_ENDPOINT_XFER_CONTROL: s = ""; break;
247 case USB_ENDPOINT_XFER_BULK: s = "-bulk"; break;
248 case USB_ENDPOINT_XFER_ISOC: s = "-iso"; break;
249 default: s = "-intr"; break;
250 }; s; }),
251 epnum, buf + offset, len);
253 /* Configure endpoint */
254 musb_ep_set_qh(hw_ep, is_in, qh);
255 musb_ep_program(musb, epnum, urb, !is_in, buf, offset, len);
257 /* transmit may have more work: start it when it is time */
258 if (is_in)
259 return;
261 /* determine if the time is right for a periodic transfer */
262 switch (qh->type) {
263 case USB_ENDPOINT_XFER_ISOC:
264 case USB_ENDPOINT_XFER_INT:
265 DBG(3, "check whether there's still time for periodic Tx\n");
266 frame = musb_readw(mbase, MUSB_FRAME);
267 /* FIXME this doesn't implement that scheduling policy ...
268 * or handle framecounter wrapping
270 if ((urb->transfer_flags & URB_ISO_ASAP)
271 || (frame >= urb->start_frame)) {
272 /* REVISIT the SOF irq handler shouldn't duplicate
273 * this code; and we don't init urb->start_frame...
275 qh->frame = 0;
276 goto start;
277 } else {
278 qh->frame = urb->start_frame;
279 /* enable SOF interrupt so we can count down */
280 DBG(1, "SOF for %d\n", epnum);
281 #if 1 /* ifndef CONFIG_ARCH_DAVINCI */
282 musb_writeb(mbase, MUSB_INTRUSBE, 0xff);
283 #endif
285 break;
286 default:
287 start:
288 DBG(4, "Start TX%d %s\n", epnum,
289 hw_ep->tx_channel ? "dma" : "pio");
291 if (!hw_ep->tx_channel)
292 musb_h_tx_start(hw_ep);
293 else if (is_cppi_enabled() ||
294 tusb_dma_omap() || is_cppi41_enabled())
295 musb_h_tx_dma_start(hw_ep);
299 /* Context: caller owns controller lock, IRQs are blocked */
300 static void musb_giveback(struct musb *musb, struct urb *urb, int status)
301 __releases(musb->lock)
302 __acquires(musb->lock)
304 DBG(({ int level; switch (status) {
305 case 0:
306 level = 4;
307 break;
308 /* common/boring faults */
309 case -EREMOTEIO:
310 case -ESHUTDOWN:
311 case -ECONNRESET:
312 case -EPIPE:
313 level = 3;
314 break;
315 default:
316 level = 2;
317 break;
318 }; level; }),
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_to_hcd(musb), urb);
328 spin_unlock(&musb->lock);
329 usb_hcd_giveback_urb(musb_to_hcd(musb), 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,
335 struct urb *urb)
337 void __iomem *epio = qh->hw_ep->regs;
338 u16 csr;
341 * FIXME: the current Mentor DMA code seems to have
342 * problems getting toggle correct.
345 if (is_in)
346 csr = musb_readw(epio, MUSB_RXCSR) & MUSB_RXCSR_H_DATATOGGLE;
347 else
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;
366 int status;
368 status = (urb->status == -EINPROGRESS) ? 0 : urb->status;
370 /* save toggle eagerly, for paranoia */
371 switch (qh->type) {
372 case USB_ENDPOINT_XFER_BULK:
373 case USB_ENDPOINT_XFER_INT:
374 musb_save_toggle(qh, is_in, urb);
375 break;
376 case USB_ENDPOINT_XFER_ISOC:
377 if (status == 0 && urb->error_count)
378 status = -EXDEV;
379 break;
382 qh->is_ready = 0;
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;
392 if (is_in)
393 ep->rx_reinit = 1;
394 else
395 ep->tx_reinit = 1;
397 /* Clobber old pointers to this qh */
398 musb_ep_set_qh(ep, is_in, NULL);
399 qh->hep->hcpriv = NULL;
401 switch (qh->type) {
403 case USB_ENDPOINT_XFER_CONTROL:
404 case USB_ENDPOINT_XFER_BULK:
405 /* fifo policy for these lists, except that NAKing
406 * should rotate a qh to the end (for fairness).
408 if (qh->mux == 1) {
409 head = qh->ring.prev;
410 list_del(&qh->ring);
411 kfree(qh);
412 qh = first_qh(head);
413 break;
416 case USB_ENDPOINT_XFER_ISOC:
417 case USB_ENDPOINT_XFER_INT:
418 /* this is where periodic bandwidth should be
419 * de-allocated if it's tracked and allocated;
420 * and where we'd update the schedule tree...
422 kfree(qh);
423 qh = NULL;
424 break;
428 if (qh != NULL && qh->is_ready) {
429 DBG(4, "... next ep%d %cX urb %p\n",
430 hw_ep->epnum, is_in ? 'R' : 'T', next_urb(qh));
431 musb_start_urb(musb, is_in, qh);
435 static u16 musb_h_flush_rxfifo(struct musb_hw_ep *hw_ep, u16 csr)
437 /* we don't want fifo to fill itself again;
438 * ignore dma (various models),
439 * leave toggle alone (may not have been saved yet)
441 csr |= MUSB_RXCSR_FLUSHFIFO | MUSB_RXCSR_RXPKTRDY;
442 csr &= ~(MUSB_RXCSR_H_REQPKT
443 | MUSB_RXCSR_H_AUTOREQ
444 | MUSB_RXCSR_AUTOCLEAR);
446 /* write 2x to allow double buffering */
447 musb_writew(hw_ep->regs, MUSB_RXCSR, csr);
448 musb_writew(hw_ep->regs, MUSB_RXCSR, csr);
450 /* flush writebuffer */
451 return musb_readw(hw_ep->regs, MUSB_RXCSR);
455 * PIO RX for a packet (or part of it).
457 static bool
458 musb_host_packet_rx(struct musb *musb, struct urb *urb, u8 epnum, u8 iso_err)
460 u16 rx_count;
461 u8 *buf;
462 u16 csr;
463 bool done = false;
464 u32 length;
465 int do_flush = 0;
466 struct musb_hw_ep *hw_ep = musb->endpoints + epnum;
467 void __iomem *epio = hw_ep->regs;
468 struct musb_qh *qh = hw_ep->in_qh;
469 int pipe = urb->pipe;
470 void *buffer = urb->transfer_buffer;
472 /* musb_ep_select(mbase, epnum); */
473 rx_count = musb_readw(epio, MUSB_RXCOUNT);
474 DBG(3, "RX%d count %d, buffer %p len %d/%d\n", epnum, rx_count,
475 urb->transfer_buffer, qh->offset,
476 urb->transfer_buffer_length);
478 /* unload FIFO */
479 if (usb_pipeisoc(pipe)) {
480 int status = 0;
481 struct usb_iso_packet_descriptor *d;
483 if (iso_err) {
484 status = -EILSEQ;
485 urb->error_count++;
488 d = urb->iso_frame_desc + qh->iso_idx;
489 buf = buffer + d->offset;
490 length = d->length;
491 if (rx_count > length) {
492 if (status == 0) {
493 status = -EOVERFLOW;
494 urb->error_count++;
496 DBG(2, "** OVERFLOW %d into %d\n", rx_count, length);
497 do_flush = 1;
498 } else
499 length = rx_count;
500 urb->actual_length += length;
501 d->actual_length = length;
503 d->status = status;
505 /* see if we are done */
506 done = (++qh->iso_idx >= urb->number_of_packets);
507 } else {
508 /* non-isoch */
509 buf = buffer + qh->offset;
510 length = urb->transfer_buffer_length - qh->offset;
511 if (rx_count > length) {
512 if (urb->status == -EINPROGRESS)
513 urb->status = -EOVERFLOW;
514 DBG(2, "** OVERFLOW %d into %d\n", rx_count, length);
515 do_flush = 1;
516 } else
517 length = rx_count;
518 urb->actual_length += length;
519 qh->offset += length;
521 /* see if we are done */
522 done = (urb->actual_length == urb->transfer_buffer_length)
523 || (rx_count < qh->maxpacket)
524 || (urb->status != -EINPROGRESS);
525 if (done
526 && (urb->status == -EINPROGRESS)
527 && (urb->transfer_flags & URB_SHORT_NOT_OK)
528 && (urb->actual_length
529 < urb->transfer_buffer_length))
530 urb->status = -EREMOTEIO;
533 musb_read_fifo(hw_ep, length, buf);
535 csr = musb_readw(epio, MUSB_RXCSR);
536 csr |= MUSB_RXCSR_H_WZC_BITS;
537 if (unlikely(do_flush))
538 musb_h_flush_rxfifo(hw_ep, csr);
539 else {
540 /* REVISIT this assumes AUTOCLEAR is never set */
541 csr &= ~(MUSB_RXCSR_RXPKTRDY | MUSB_RXCSR_H_REQPKT);
542 if (!done)
543 csr |= MUSB_RXCSR_H_REQPKT;
544 musb_writew(epio, MUSB_RXCSR, csr);
547 return done;
550 /* we don't always need to reinit a given side of an endpoint...
551 * when we do, use tx/rx reinit routine and then construct a new CSR
552 * to address data toggle, NYET, and DMA or PIO.
554 * it's possible that driver bugs (especially for DMA) or aborting a
555 * transfer might have left the endpoint busier than it should be.
556 * the busy/not-empty tests are basically paranoia.
558 static void
559 musb_rx_reinit(struct musb *musb, struct musb_qh *qh, struct musb_hw_ep *ep)
561 u16 csr;
563 /* NOTE: we know the "rx" fifo reinit never triggers for ep0.
564 * That always uses tx_reinit since ep0 repurposes TX register
565 * offsets; the initial SETUP packet is also a kind of OUT.
568 /* if programmed for Tx, put it in RX mode */
569 if (ep->is_shared_fifo) {
570 csr = musb_readw(ep->regs, MUSB_TXCSR);
571 if (csr & MUSB_TXCSR_MODE) {
572 musb_h_tx_flush_fifo(ep);
573 csr = musb_readw(ep->regs, MUSB_TXCSR);
574 musb_writew(ep->regs, MUSB_TXCSR,
575 csr | MUSB_TXCSR_FRCDATATOG);
579 * Clear the MODE bit (and everything else) to enable Rx.
580 * NOTE: we mustn't clear the DMAMODE bit before DMAENAB.
582 if (csr & MUSB_TXCSR_DMAMODE)
583 musb_writew(ep->regs, MUSB_TXCSR, MUSB_TXCSR_DMAMODE);
584 musb_writew(ep->regs, MUSB_TXCSR, 0);
586 /* scrub all previous state, clearing toggle */
587 } else {
588 csr = musb_readw(ep->regs, MUSB_RXCSR);
589 if (csr & MUSB_RXCSR_RXPKTRDY)
590 WARNING("rx%d, packet/%d ready?\n", ep->epnum,
591 musb_readw(ep->regs, MUSB_RXCOUNT));
593 musb_h_flush_rxfifo(ep, MUSB_RXCSR_CLRDATATOG);
596 /* target addr and (for multipoint) hub addr/port */
597 if (musb->is_multipoint) {
598 musb_write_rxfunaddr(ep->target_regs, qh->addr_reg);
599 musb_write_rxhubaddr(ep->target_regs, qh->h_addr_reg);
600 musb_write_rxhubport(ep->target_regs, qh->h_port_reg);
602 } else
603 musb_writeb(musb->mregs, MUSB_FADDR, qh->addr_reg);
605 /* protocol/endpoint, interval/NAKlimit, i/o size */
606 musb_writeb(ep->regs, MUSB_RXTYPE, qh->type_reg);
607 musb_writeb(ep->regs, MUSB_RXINTERVAL, qh->intv_reg);
608 /* NOTE: bulk combining rewrites high bits of maxpacket */
609 musb_writew(ep->regs, MUSB_RXMAXP,
610 qh->maxpacket | ((qh->hb_mult - 1) << 11));
612 ep->rx_reinit = 0;
615 static bool musb_tx_dma_program(struct dma_controller *dma,
616 struct musb_hw_ep *hw_ep, struct musb_qh *qh,
617 struct urb *urb, u32 offset, u32 length)
619 struct dma_channel *channel = hw_ep->tx_channel;
620 void __iomem *epio = hw_ep->regs;
621 u16 pkt_size = qh->maxpacket;
622 u16 csr;
623 u8 mode;
625 #ifdef CONFIG_USB_INVENTRA_DMA
626 if (length > channel->max_len)
627 length = channel->max_len;
629 csr = musb_readw(epio, MUSB_TXCSR);
630 if (length > pkt_size) {
631 mode = 1;
632 csr |= MUSB_TXCSR_DMAMODE | MUSB_TXCSR_DMAENAB;
633 /* autoset shouldn't be set in high bandwidth */
634 if (qh->hb_mult == 1)
635 csr |= MUSB_TXCSR_AUTOSET;
636 } else {
637 mode = 0;
638 csr &= ~(MUSB_TXCSR_AUTOSET | MUSB_TXCSR_DMAMODE);
639 csr |= MUSB_TXCSR_DMAENAB; /* against programmer's guide */
641 channel->desired_mode = mode;
642 musb_writew(epio, MUSB_TXCSR, csr);
643 #else
644 if (!is_cppi_enabled() && !tusb_dma_omap() && !is_cppi41_enabled())
645 return false;
647 channel->actual_len = 0;
650 * TX uses "RNDIS" mode automatically but needs help
651 * to identify the zero-length-final-packet case.
653 mode = (urb->transfer_flags & URB_ZERO_PACKET) ? 1 : 0;
654 #endif
656 qh->segsize = length;
658 if (!dma->channel_program(channel, pkt_size, mode,
659 urb->transfer_dma + offset, length)) {
660 dma->channel_release(channel);
661 hw_ep->tx_channel = NULL;
663 csr = musb_readw(epio, MUSB_TXCSR);
664 csr &= ~(MUSB_TXCSR_AUTOSET | MUSB_TXCSR_DMAENAB);
665 musb_writew(epio, MUSB_TXCSR, csr | MUSB_TXCSR_H_WZC_BITS);
666 return false;
668 return true;
672 * Program an HDRC endpoint as per the given URB
673 * Context: irqs blocked, controller lock held
675 static void musb_ep_program(struct musb *musb, u8 epnum,
676 struct urb *urb, int is_out,
677 u8 *buf, u32 offset, u32 len)
679 struct dma_controller *dma_controller;
680 struct dma_channel *dma_channel;
681 u8 dma_ok;
682 void __iomem *mbase = musb->mregs;
683 struct musb_hw_ep *hw_ep = musb->endpoints + epnum;
684 void __iomem *epio = hw_ep->regs;
685 struct musb_qh *qh = musb_ep_get_qh(hw_ep, !is_out);
686 u16 packet_sz = qh->maxpacket;
688 DBG(3, "%s hw%d urb %p spd%d dev%d ep%d%s "
689 "h_addr%02x h_port%02x bytes %d\n",
690 is_out ? "-->" : "<--",
691 epnum, urb, urb->dev->speed,
692 qh->addr_reg, qh->epnum, is_out ? "out" : "in",
693 qh->h_addr_reg, qh->h_port_reg,
694 len);
696 musb_ep_select(mbase, epnum);
698 /* candidate for DMA? */
699 dma_controller = musb->dma_controller;
700 if (is_dma_capable() && epnum && dma_controller) {
701 dma_channel = is_out ? hw_ep->tx_channel : hw_ep->rx_channel;
702 if (!dma_channel) {
703 dma_channel = dma_controller->channel_alloc(
704 dma_controller, hw_ep, is_out);
705 if (is_out)
706 hw_ep->tx_channel = dma_channel;
707 else
708 hw_ep->rx_channel = dma_channel;
710 } else
711 dma_channel = NULL;
713 /* make sure we clear DMAEnab, autoSet bits from previous run */
715 /* OUT/transmit/EP0 or IN/receive? */
716 if (is_out) {
717 u16 csr;
718 u16 int_txe;
719 u16 load_count;
721 csr = musb_readw(epio, MUSB_TXCSR);
723 /* disable interrupt in case we flush */
724 int_txe = musb_readw(mbase, MUSB_INTRTXE);
725 musb_writew(mbase, MUSB_INTRTXE, int_txe & ~(1 << epnum));
727 /* general endpoint setup */
728 if (epnum) {
729 /* flush all old state, set default */
730 musb_h_tx_flush_fifo(hw_ep);
733 * We must not clear the DMAMODE bit before or in
734 * the same cycle with the DMAENAB bit, so we clear
735 * the latter first...
737 csr &= ~(MUSB_TXCSR_H_NAKTIMEOUT
738 | MUSB_TXCSR_AUTOSET
739 | MUSB_TXCSR_DMAENAB
740 | MUSB_TXCSR_FRCDATATOG
741 | MUSB_TXCSR_H_RXSTALL
742 | MUSB_TXCSR_H_ERROR
743 | MUSB_TXCSR_TXPKTRDY
745 csr |= MUSB_TXCSR_MODE;
747 if (usb_gettoggle(urb->dev, qh->epnum, 1))
748 csr |= MUSB_TXCSR_H_WR_DATATOGGLE
749 | MUSB_TXCSR_H_DATATOGGLE;
750 else
751 csr |= MUSB_TXCSR_CLRDATATOG;
753 musb_writew(epio, MUSB_TXCSR, csr);
754 /* REVISIT may need to clear FLUSHFIFO ... */
755 csr &= ~MUSB_TXCSR_DMAMODE;
756 musb_writew(epio, MUSB_TXCSR, csr);
757 csr = musb_readw(epio, MUSB_TXCSR);
758 } else {
759 /* endpoint 0: just flush */
760 musb_h_ep0_flush_fifo(hw_ep);
763 /* target addr and (for multipoint) hub addr/port */
764 if (musb->is_multipoint) {
765 musb_write_txfunaddr(mbase, epnum, qh->addr_reg);
766 musb_write_txhubaddr(mbase, epnum, qh->h_addr_reg);
767 musb_write_txhubport(mbase, epnum, qh->h_port_reg);
768 /* FIXME if !epnum, do the same for RX ... */
769 } else
770 musb_writeb(mbase, MUSB_FADDR, qh->addr_reg);
772 /* protocol/endpoint/interval/NAKlimit */
773 if (epnum) {
774 musb_writeb(epio, MUSB_TXTYPE, qh->type_reg);
775 if (can_bulk_split(musb, qh->type))
776 musb_writew(epio, MUSB_TXMAXP,
777 packet_sz
778 | ((hw_ep->max_packet_sz_tx /
779 packet_sz) - 1) << 11);
780 else
781 musb_writew(epio, MUSB_TXMAXP,
782 packet_sz);
783 musb_writeb(epio, MUSB_TXINTERVAL, qh->intv_reg);
784 } else {
785 musb_writeb(epio, MUSB_NAKLIMIT0, qh->intv_reg);
786 if (musb->is_multipoint)
787 musb_writeb(epio, MUSB_TYPE0,
788 qh->type_reg);
791 if (can_bulk_split(musb, qh->type))
792 load_count = min((u32) hw_ep->max_packet_sz_tx,
793 len);
794 else
795 load_count = min((u32) packet_sz, len);
797 if (dma_channel && musb_tx_dma_program(dma_controller,
798 hw_ep, qh, urb, offset, len))
799 load_count = 0;
801 if (load_count) {
802 /* PIO to load FIFO */
803 qh->segsize = load_count;
804 musb_write_fifo(hw_ep, load_count, buf);
807 /* re-enable interrupt */
808 musb_writew(mbase, MUSB_INTRTXE, int_txe);
810 /* IN/receive */
811 } else {
812 u16 csr;
814 if (hw_ep->rx_reinit) {
815 musb_rx_reinit(musb, qh, hw_ep);
817 /* init new state: toggle and NYET, maybe DMA later */
818 if (usb_gettoggle(urb->dev, qh->epnum, 0))
819 csr = MUSB_RXCSR_H_WR_DATATOGGLE
820 | MUSB_RXCSR_H_DATATOGGLE;
821 else
822 csr = 0;
823 if (qh->type == USB_ENDPOINT_XFER_INT)
824 csr |= MUSB_RXCSR_DISNYET;
826 } else {
827 csr = musb_readw(hw_ep->regs, MUSB_RXCSR);
829 if (csr & (MUSB_RXCSR_RXPKTRDY
830 | (is_cppi_enabled() || is_cppi41_enabled())
831 ? 0 : MUSB_RXCSR_DMAENAB
832 | MUSB_RXCSR_H_REQPKT))
833 ERR("broken !rx_reinit, ep%d csr %04x\n",
834 hw_ep->epnum, csr);
836 /* scrub any stale state, leaving toggle alone */
837 csr &= MUSB_RXCSR_DISNYET;
840 /* kick things off */
842 if ((is_cppi_enabled() || is_cppi41_enabled() ||
843 tusb_dma_omap()) && dma_channel) {
844 /* candidate for DMA */
845 if (dma_channel) {
846 dma_channel->actual_len = 0L;
847 qh->segsize = len;
849 /* AUTOREQ is in a DMA register */
850 musb_writew(hw_ep->regs, MUSB_RXCSR, csr);
851 csr = musb_readw(hw_ep->regs,
852 MUSB_RXCSR);
854 /* unless caller treats short rx transfers as
855 * errors, we dare not queue multiple transfers.
857 dma_ok = dma_controller->channel_program(
858 dma_channel, packet_sz,
859 !(urb->transfer_flags
860 & URB_SHORT_NOT_OK),
861 urb->transfer_dma + offset,
862 qh->segsize);
863 if (!dma_ok) {
864 dma_controller->channel_release(
865 dma_channel);
866 hw_ep->rx_channel = NULL;
867 dma_channel = NULL;
868 } else
869 csr |= MUSB_RXCSR_DMAENAB;
873 csr |= MUSB_RXCSR_H_REQPKT;
874 DBG(7, "RXCSR%d := %04x\n", epnum, csr);
875 musb_writew(hw_ep->regs, MUSB_RXCSR, csr);
876 csr = musb_readw(hw_ep->regs, MUSB_RXCSR);
882 * Service the default endpoint (ep0) as host.
883 * Return true until it's time to start the status stage.
885 static bool musb_h_ep0_continue(struct musb *musb, u16 len, struct urb *urb)
887 bool more = false;
888 u8 *fifo_dest = NULL;
889 u16 fifo_count = 0;
890 struct musb_hw_ep *hw_ep = musb->control_ep;
891 struct musb_qh *qh = hw_ep->in_qh;
892 struct usb_ctrlrequest *request;
894 switch (musb->ep0_stage) {
895 case MUSB_EP0_IN:
896 fifo_dest = urb->transfer_buffer + urb->actual_length;
897 fifo_count = min_t(size_t, len, urb->transfer_buffer_length -
898 urb->actual_length);
899 if (fifo_count < len)
900 urb->status = -EOVERFLOW;
902 musb_read_fifo(hw_ep, fifo_count, fifo_dest);
904 urb->actual_length += fifo_count;
905 if (len < qh->maxpacket) {
906 /* always terminate on short read; it's
907 * rarely reported as an error.
909 } else if (urb->actual_length <
910 urb->transfer_buffer_length)
911 more = true;
912 break;
913 case MUSB_EP0_START:
914 request = (struct usb_ctrlrequest *) urb->setup_packet;
916 if (!request->wLength) {
917 DBG(4, "start no-DATA\n");
918 break;
919 } else if (request->bRequestType & USB_DIR_IN) {
920 DBG(4, "start IN-DATA\n");
921 musb->ep0_stage = MUSB_EP0_IN;
922 more = true;
923 break;
924 } else {
925 DBG(4, "start OUT-DATA\n");
926 musb->ep0_stage = MUSB_EP0_OUT;
927 more = true;
929 /* FALLTHROUGH */
930 case MUSB_EP0_OUT:
931 fifo_count = min_t(size_t, qh->maxpacket,
932 urb->transfer_buffer_length -
933 urb->actual_length);
934 if (fifo_count) {
935 fifo_dest = (u8 *) (urb->transfer_buffer
936 + urb->actual_length);
937 DBG(3, "Sending %d byte%s to ep0 fifo %p\n",
938 fifo_count,
939 (fifo_count == 1) ? "" : "s",
940 fifo_dest);
941 musb_write_fifo(hw_ep, fifo_count, fifo_dest);
943 urb->actual_length += fifo_count;
944 more = true;
946 break;
947 default:
948 ERR("bogus ep0 stage %d\n", musb->ep0_stage);
949 break;
952 return more;
956 * Handle default endpoint interrupt as host. Only called in IRQ time
957 * from musb_interrupt().
959 * called with controller irqlocked
961 irqreturn_t musb_h_ep0_irq(struct musb *musb)
963 struct urb *urb;
964 u16 csr, len;
965 int status = 0;
966 void __iomem *mbase = musb->mregs;
967 struct musb_hw_ep *hw_ep = musb->control_ep;
968 void __iomem *epio = hw_ep->regs;
969 struct musb_qh *qh = hw_ep->in_qh;
970 bool complete = false;
971 irqreturn_t retval = IRQ_NONE;
973 /* ep0 only has one queue, "in" */
974 urb = next_urb(qh);
976 musb_ep_select(mbase, 0);
977 csr = musb_readw(epio, MUSB_CSR0);
978 len = (csr & MUSB_CSR0_RXPKTRDY)
979 ? musb_readb(epio, MUSB_COUNT0)
980 : 0;
982 DBG(4, "<== csr0 %04x, qh %p, count %d, urb %p, stage %d\n",
983 csr, qh, len, urb, musb->ep0_stage);
985 /* if we just did status stage, we are done */
986 if (MUSB_EP0_STATUS == musb->ep0_stage) {
987 retval = IRQ_HANDLED;
988 complete = true;
991 /* prepare status */
992 if (csr & MUSB_CSR0_H_RXSTALL) {
993 DBG(6, "STALLING ENDPOINT\n");
994 status = -EPIPE;
996 } else if (csr & MUSB_CSR0_H_ERROR) {
997 DBG(2, "no response, csr0 %04x\n", csr);
998 status = -EPROTO;
1000 } else if (csr & MUSB_CSR0_H_NAKTIMEOUT) {
1001 DBG(2, "control NAK timeout\n");
1003 /* NOTE: this code path would be a good place to PAUSE a
1004 * control transfer, if another one is queued, so that
1005 * ep0 is more likely to stay busy. That's already done
1006 * for bulk RX transfers.
1008 * if (qh->ring.next != &musb->control), then
1009 * we have a candidate... NAKing is *NOT* an error
1011 musb_writew(epio, MUSB_CSR0, 0);
1012 retval = IRQ_HANDLED;
1015 if (status) {
1016 DBG(6, "aborting\n");
1017 retval = IRQ_HANDLED;
1018 if (urb)
1019 urb->status = status;
1020 complete = true;
1022 /* use the proper sequence to abort the transfer */
1023 if (csr & MUSB_CSR0_H_REQPKT) {
1024 csr &= ~MUSB_CSR0_H_REQPKT;
1025 musb_writew(epio, MUSB_CSR0, csr);
1026 csr &= ~MUSB_CSR0_H_NAKTIMEOUT;
1027 musb_writew(epio, MUSB_CSR0, csr);
1028 } else {
1029 musb_h_ep0_flush_fifo(hw_ep);
1032 musb_writeb(epio, MUSB_NAKLIMIT0, 0);
1034 /* clear it */
1035 musb_writew(epio, MUSB_CSR0, 0);
1038 if (unlikely(!urb)) {
1039 /* stop endpoint since we have no place for its data, this
1040 * SHOULD NEVER HAPPEN! */
1041 ERR("no URB for end 0\n");
1043 musb_h_ep0_flush_fifo(hw_ep);
1044 goto done;
1047 if (!complete) {
1048 /* call common logic and prepare response */
1049 if (musb_h_ep0_continue(musb, len, urb)) {
1050 /* more packets required */
1051 csr = (MUSB_EP0_IN == musb->ep0_stage)
1052 ? MUSB_CSR0_H_REQPKT : MUSB_CSR0_TXPKTRDY;
1053 } else {
1054 /* data transfer complete; perform status phase */
1055 if (usb_pipeout(urb->pipe)
1056 || !urb->transfer_buffer_length)
1057 csr = MUSB_CSR0_H_STATUSPKT
1058 | MUSB_CSR0_H_REQPKT;
1059 else
1060 csr = MUSB_CSR0_H_STATUSPKT
1061 | MUSB_CSR0_TXPKTRDY;
1062 /* disable ping token in status phase */
1063 csr |= MUSB_CSR0_H_DIS_PING;
1065 /* flag status stage */
1066 musb->ep0_stage = MUSB_EP0_STATUS;
1068 DBG(5, "ep0 STATUS, csr %04x\n", csr);
1071 musb_writew(epio, MUSB_CSR0, csr);
1072 retval = IRQ_HANDLED;
1073 } else
1074 musb->ep0_stage = MUSB_EP0_IDLE;
1076 /* call completion handler if done */
1077 if (complete)
1078 musb_advance_schedule(musb, urb, hw_ep, 1);
1079 done:
1080 return retval;
1084 #ifdef CONFIG_USB_INVENTRA_DMA
1086 /* Host side TX (OUT) using Mentor DMA works as follows:
1087 submit_urb ->
1088 - if queue was empty, Program Endpoint
1089 - ... which starts DMA to fifo in mode 1 or 0
1091 DMA Isr (transfer complete) -> TxAvail()
1092 - Stop DMA (~DmaEnab) (<--- Alert ... currently happens
1093 only in musb_cleanup_urb)
1094 - TxPktRdy has to be set in mode 0 or for
1095 short packets in mode 1.
1098 #endif
1100 /* Service a Tx-Available or dma completion irq for the endpoint */
1101 void musb_host_tx(struct musb *musb, u8 epnum)
1103 int pipe;
1104 bool done = false;
1105 u16 tx_csr;
1106 size_t length = 0;
1107 size_t offset = 0;
1108 struct musb_hw_ep *hw_ep = musb->endpoints + epnum;
1109 void __iomem *epio = hw_ep->regs;
1110 struct musb_qh *qh = hw_ep->out_qh;
1111 struct urb *urb = next_urb(qh);
1112 u32 status = 0;
1113 void __iomem *mbase = musb->mregs;
1114 struct dma_channel *dma;
1116 musb_ep_select(mbase, epnum);
1117 tx_csr = musb_readw(epio, MUSB_TXCSR);
1119 /* with CPPI, DMA sometimes triggers "extra" irqs */
1120 if (!urb) {
1121 DBG(4, "extra TX%d ready, csr %04x\n", epnum, tx_csr);
1122 return;
1125 pipe = urb->pipe;
1126 dma = is_dma_capable() ? hw_ep->tx_channel : NULL;
1127 DBG(4, "OUT/TX%d end, csr %04x%s\n", epnum, tx_csr,
1128 dma ? ", dma" : "");
1130 /* check for errors */
1131 if (tx_csr & MUSB_TXCSR_H_RXSTALL) {
1132 /* dma was disabled, fifo flushed */
1133 DBG(3, "TX end %d stall\n", epnum);
1135 /* stall; record URB status */
1136 status = -EPIPE;
1138 } else if (tx_csr & MUSB_TXCSR_H_ERROR) {
1139 /* (NON-ISO) dma was disabled, fifo flushed */
1140 DBG(3, "TX 3strikes on ep=%d\n", epnum);
1142 status = -ETIMEDOUT;
1144 } else if (tx_csr & MUSB_TXCSR_H_NAKTIMEOUT) {
1145 DBG(6, "TX end=%d device not responding\n", epnum);
1147 /* NOTE: this code path would be a good place to PAUSE a
1148 * transfer, if there's some other (nonperiodic) tx urb
1149 * that could use this fifo. (dma complicates it...)
1150 * That's already done for bulk RX transfers.
1152 * if (bulk && qh->ring.next != &musb->out_bulk), then
1153 * we have a candidate... NAKing is *NOT* an error
1155 musb_ep_select(mbase, epnum);
1156 musb_writew(epio, MUSB_TXCSR,
1157 MUSB_TXCSR_H_WZC_BITS
1158 | MUSB_TXCSR_TXPKTRDY);
1159 return;
1162 if (status) {
1163 if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) {
1164 dma->status = MUSB_DMA_STATUS_CORE_ABORT;
1165 (void) musb->dma_controller->channel_abort(dma);
1168 /* do the proper sequence to abort the transfer in the
1169 * usb core; the dma engine should already be stopped.
1171 musb_h_tx_flush_fifo(hw_ep);
1172 tx_csr &= ~(MUSB_TXCSR_AUTOSET
1173 | MUSB_TXCSR_DMAENAB
1174 | MUSB_TXCSR_H_ERROR
1175 | MUSB_TXCSR_H_RXSTALL
1176 | MUSB_TXCSR_H_NAKTIMEOUT
1179 musb_ep_select(mbase, epnum);
1180 musb_writew(epio, MUSB_TXCSR, tx_csr);
1181 /* REVISIT may need to clear FLUSHFIFO ... */
1182 musb_writew(epio, MUSB_TXCSR, tx_csr);
1183 musb_writeb(epio, MUSB_TXINTERVAL, 0);
1185 done = true;
1188 /* second cppi case */
1189 if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) {
1190 DBG(4, "extra TX%d ready, csr %04x\n", epnum, tx_csr);
1191 return;
1194 if (is_dma_capable() && dma && !status) {
1196 * DMA has completed. But if we're using DMA mode 1 (multi
1197 * packet DMA), we need a terminal TXPKTRDY interrupt before
1198 * we can consider this transfer completed, lest we trash
1199 * its last packet when writing the next URB's data. So we
1200 * switch back to mode 0 to get that interrupt; we'll come
1201 * back here once it happens.
1203 if (tx_csr & MUSB_TXCSR_DMAMODE) {
1205 * We shouldn't clear DMAMODE with DMAENAB set; so
1206 * clear them in a safe order. That should be OK
1207 * once TXPKTRDY has been set (and I've never seen
1208 * it being 0 at this moment -- DMA interrupt latency
1209 * is significant) but if it hasn't been then we have
1210 * no choice but to stop being polite and ignore the
1211 * programmer's guide... :-)
1213 * Note that we must write TXCSR with TXPKTRDY cleared
1214 * in order not to re-trigger the packet send (this bit
1215 * can't be cleared by CPU), and there's another caveat:
1216 * TXPKTRDY may be set shortly and then cleared in the
1217 * double-buffered FIFO mode, so we do an extra TXCSR
1218 * read for debouncing...
1220 tx_csr &= musb_readw(epio, MUSB_TXCSR);
1221 if (tx_csr & MUSB_TXCSR_TXPKTRDY) {
1222 tx_csr &= ~(MUSB_TXCSR_DMAENAB |
1223 MUSB_TXCSR_TXPKTRDY);
1224 musb_writew(epio, MUSB_TXCSR,
1225 tx_csr | MUSB_TXCSR_H_WZC_BITS);
1227 tx_csr &= ~(MUSB_TXCSR_DMAMODE |
1228 MUSB_TXCSR_TXPKTRDY);
1229 musb_writew(epio, MUSB_TXCSR,
1230 tx_csr | MUSB_TXCSR_H_WZC_BITS);
1233 * There is no guarantee that we'll get an interrupt
1234 * after clearing DMAMODE as we might have done this
1235 * too late (after TXPKTRDY was cleared by controller).
1236 * Re-read TXCSR as we have spoiled its previous value.
1238 tx_csr = musb_readw(epio, MUSB_TXCSR);
1242 * We may get here from a DMA completion or TXPKTRDY interrupt.
1243 * In any case, we must check the FIFO status here and bail out
1244 * only if the FIFO still has data -- that should prevent the
1245 * "missed" TXPKTRDY interrupts and deal with double-buffered
1246 * FIFO mode too...
1248 if (tx_csr & (MUSB_TXCSR_FIFONOTEMPTY | MUSB_TXCSR_TXPKTRDY)) {
1249 DBG(2, "DMA complete but packet still in FIFO, "
1250 "CSR %04x\n", tx_csr);
1251 return;
1255 if (!status || dma || usb_pipeisoc(pipe)) {
1256 if (dma)
1257 length = dma->actual_len;
1258 else
1259 length = qh->segsize;
1260 qh->offset += length;
1262 if (usb_pipeisoc(pipe)) {
1263 struct usb_iso_packet_descriptor *d;
1265 d = urb->iso_frame_desc + qh->iso_idx;
1266 d->actual_length = length;
1267 d->status = status;
1268 if (++qh->iso_idx >= urb->number_of_packets) {
1269 done = true;
1270 } else {
1271 d++;
1272 offset = d->offset;
1273 length = d->length;
1275 } else if (dma) {
1276 done = true;
1277 } else {
1278 /* see if we need to send more data, or ZLP */
1279 if (qh->segsize < qh->maxpacket)
1280 done = true;
1281 else if (qh->offset == urb->transfer_buffer_length
1282 && !(urb->transfer_flags
1283 & URB_ZERO_PACKET))
1284 done = true;
1285 if (!done) {
1286 offset = qh->offset;
1287 length = urb->transfer_buffer_length - offset;
1292 /* urb->status != -EINPROGRESS means request has been faulted,
1293 * so we must abort this transfer after cleanup
1295 if (urb->status != -EINPROGRESS) {
1296 done = true;
1297 if (status == 0)
1298 status = urb->status;
1301 if (done) {
1302 /* set status */
1303 urb->status = status;
1304 urb->actual_length = qh->offset;
1305 musb_advance_schedule(musb, urb, hw_ep, USB_DIR_OUT);
1306 return;
1307 } else if (usb_pipeisoc(pipe) && dma) {
1308 if (musb_tx_dma_program(musb->dma_controller, hw_ep, qh, urb,
1309 offset, length)) {
1310 if (is_cppi_enabled() || tusb_dma_omap() ||
1311 is_cppi41_enabled())
1312 musb_h_tx_dma_start(hw_ep);
1313 return;
1315 } else if (tx_csr & MUSB_TXCSR_DMAENAB) {
1316 DBG(1, "not complete, but DMA enabled?\n");
1317 return;
1321 * PIO: start next packet in this URB.
1323 * REVISIT: some docs say that when hw_ep->tx_double_buffered,
1324 * (and presumably, FIFO is not half-full) we should write *two*
1325 * packets before updating TXCSR; other docs disagree...
1327 if (length > qh->maxpacket)
1328 length = qh->maxpacket;
1329 musb_write_fifo(hw_ep, length, urb->transfer_buffer + offset);
1330 qh->segsize = length;
1332 musb_ep_select(mbase, epnum);
1333 musb_writew(epio, MUSB_TXCSR,
1334 MUSB_TXCSR_H_WZC_BITS | MUSB_TXCSR_TXPKTRDY);
1338 #ifdef CONFIG_USB_INVENTRA_DMA
1340 /* Host side RX (IN) using Mentor DMA works as follows:
1341 submit_urb ->
1342 - if queue was empty, ProgramEndpoint
1343 - first IN token is sent out (by setting ReqPkt)
1344 LinuxIsr -> RxReady()
1345 /\ => first packet is received
1346 | - Set in mode 0 (DmaEnab, ~ReqPkt)
1347 | -> DMA Isr (transfer complete) -> RxReady()
1348 | - Ack receive (~RxPktRdy), turn off DMA (~DmaEnab)
1349 | - if urb not complete, send next IN token (ReqPkt)
1350 | | else complete urb.
1352 ---------------------------
1354 * Nuances of mode 1:
1355 * For short packets, no ack (+RxPktRdy) is sent automatically
1356 * (even if AutoClear is ON)
1357 * For full packets, ack (~RxPktRdy) and next IN token (+ReqPkt) is sent
1358 * automatically => major problem, as collecting the next packet becomes
1359 * difficult. Hence mode 1 is not used.
1361 * REVISIT
1362 * All we care about at this driver level is that
1363 * (a) all URBs terminate with REQPKT cleared and fifo(s) empty;
1364 * (b) termination conditions are: short RX, or buffer full;
1365 * (c) fault modes include
1366 * - iff URB_SHORT_NOT_OK, short RX status is -EREMOTEIO.
1367 * (and that endpoint's dma queue stops immediately)
1368 * - overflow (full, PLUS more bytes in the terminal packet)
1370 * So for example, usb-storage sets URB_SHORT_NOT_OK, and would
1371 * thus be a great candidate for using mode 1 ... for all but the
1372 * last packet of one URB's transfer.
1375 #endif
1377 /* Schedule next QH from musb->in_bulk and move the current qh to
1378 * the end; avoids starvation for other endpoints.
1380 static void musb_bulk_rx_nak_timeout(struct musb *musb, struct musb_hw_ep *ep)
1382 struct dma_channel *dma;
1383 struct urb *urb;
1384 void __iomem *mbase = musb->mregs;
1385 void __iomem *epio = ep->regs;
1386 struct musb_qh *cur_qh, *next_qh;
1387 u16 rx_csr;
1389 musb_ep_select(mbase, ep->epnum);
1390 dma = is_dma_capable() ? ep->rx_channel : NULL;
1392 /* clear nak timeout bit */
1393 rx_csr = musb_readw(epio, MUSB_RXCSR);
1394 rx_csr |= MUSB_RXCSR_H_WZC_BITS;
1395 rx_csr &= ~MUSB_RXCSR_DATAERROR;
1396 musb_writew(epio, MUSB_RXCSR, rx_csr);
1398 cur_qh = first_qh(&musb->in_bulk);
1399 if (cur_qh) {
1400 urb = next_urb(cur_qh);
1401 if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) {
1402 dma->status = MUSB_DMA_STATUS_CORE_ABORT;
1403 musb->dma_controller->channel_abort(dma);
1404 urb->actual_length += dma->actual_len;
1405 dma->actual_len = 0L;
1407 musb_save_toggle(cur_qh, 1, urb);
1409 /* move cur_qh to end of queue */
1410 list_move_tail(&cur_qh->ring, &musb->in_bulk);
1412 /* get the next qh from musb->in_bulk */
1413 next_qh = first_qh(&musb->in_bulk);
1415 /* set rx_reinit and schedule the next qh */
1416 ep->rx_reinit = 1;
1417 musb_start_urb(musb, 1, next_qh);
1422 * Service an RX interrupt for the given IN endpoint; docs cover bulk, iso,
1423 * and high-bandwidth IN transfer cases.
1425 void musb_host_rx(struct musb *musb, u8 epnum)
1427 struct urb *urb;
1428 struct musb_hw_ep *hw_ep = musb->endpoints + epnum;
1429 void __iomem *epio = hw_ep->regs;
1430 struct musb_qh *qh = hw_ep->in_qh;
1431 size_t xfer_len;
1432 void __iomem *mbase = musb->mregs;
1433 int pipe;
1434 u16 rx_csr, val;
1435 bool iso_err = false;
1436 bool done = false;
1437 u32 status;
1438 struct dma_channel *dma;
1440 musb_ep_select(mbase, epnum);
1442 urb = next_urb(qh);
1443 dma = is_dma_capable() ? hw_ep->rx_channel : NULL;
1444 status = 0;
1445 xfer_len = 0;
1447 rx_csr = musb_readw(epio, MUSB_RXCSR);
1448 val = rx_csr;
1450 if (unlikely(!urb)) {
1451 /* REVISIT -- THIS SHOULD NEVER HAPPEN ... but, at least
1452 * usbtest #11 (unlinks) triggers it regularly, sometimes
1453 * with fifo full. (Only with DMA??)
1455 DBG(3, "BOGUS RX%d ready, csr %04x, count %d\n", epnum, val,
1456 musb_readw(epio, MUSB_RXCOUNT));
1457 musb_h_flush_rxfifo(hw_ep, MUSB_RXCSR_CLRDATATOG);
1458 return;
1461 pipe = urb->pipe;
1463 DBG(5, "<== hw %d rxcsr %04x, urb actual %d (+dma %zu)\n",
1464 epnum, rx_csr, urb->actual_length,
1465 dma ? dma->actual_len : 0);
1467 /* check for errors, concurrent stall & unlink is not really
1468 * handled yet! */
1469 if (rx_csr & MUSB_RXCSR_H_RXSTALL) {
1470 DBG(3, "RX end %d STALL\n", epnum);
1472 /* stall; record URB status */
1473 status = -EPIPE;
1475 } else if (rx_csr & MUSB_RXCSR_H_ERROR) {
1476 DBG(3, "end %d RX proto error\n", epnum);
1478 status = -EPROTO;
1479 musb_writeb(epio, MUSB_RXINTERVAL, 0);
1481 } else if (rx_csr & MUSB_RXCSR_DATAERROR) {
1483 if (USB_ENDPOINT_XFER_ISOC != qh->type) {
1484 DBG(6, "RX end %d NAK timeout\n", epnum);
1486 /* NOTE: NAKing is *NOT* an error, so we want to
1487 * continue. Except ... if there's a request for
1488 * another QH, use that instead of starving it.
1490 * Devices like Ethernet and serial adapters keep
1491 * reads posted at all times, which will starve
1492 * other devices without this logic.
1494 if (usb_pipebulk(urb->pipe)
1495 && qh->mux == 1
1496 && !list_is_singular(&musb->in_bulk)) {
1497 musb_bulk_rx_nak_timeout(musb, hw_ep);
1498 return;
1500 musb_ep_select(mbase, epnum);
1501 rx_csr |= MUSB_RXCSR_H_WZC_BITS;
1502 rx_csr &= ~MUSB_RXCSR_DATAERROR;
1503 musb_writew(epio, MUSB_RXCSR, rx_csr);
1505 goto finish;
1506 } else {
1507 DBG(4, "RX end %d ISO data error\n", epnum);
1508 /* packet error reported later */
1509 iso_err = true;
1511 } else if (rx_csr & MUSB_RXCSR_INCOMPRX) {
1512 DBG(3, "end %d high bandwidth incomplete ISO packet RX\n",
1513 epnum);
1514 status = -EPROTO;
1517 /* faults abort the transfer */
1518 if (status) {
1519 /* clean up dma and collect transfer count */
1520 if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) {
1521 dma->status = MUSB_DMA_STATUS_CORE_ABORT;
1522 (void) musb->dma_controller->channel_abort(dma);
1523 xfer_len = dma->actual_len;
1525 musb_h_flush_rxfifo(hw_ep, MUSB_RXCSR_CLRDATATOG);
1526 musb_writeb(epio, MUSB_RXINTERVAL, 0);
1527 done = true;
1528 goto finish;
1531 if (unlikely(dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY)) {
1532 /* SHOULD NEVER HAPPEN ... but at least DaVinci has done it */
1533 ERR("RX%d dma busy, csr %04x\n", epnum, rx_csr);
1534 goto finish;
1537 /* thorough shutdown for now ... given more precise fault handling
1538 * and better queueing support, we might keep a DMA pipeline going
1539 * while processing this irq for earlier completions.
1542 /* FIXME this is _way_ too much in-line logic for Mentor DMA */
1544 #ifndef CONFIG_USB_INVENTRA_DMA
1545 if (rx_csr & MUSB_RXCSR_H_REQPKT) {
1546 /* REVISIT this happened for a while on some short reads...
1547 * the cleanup still needs investigation... looks bad...
1548 * and also duplicates dma cleanup code above ... plus,
1549 * shouldn't this be the "half full" double buffer case?
1551 if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) {
1552 dma->status = MUSB_DMA_STATUS_CORE_ABORT;
1553 (void) musb->dma_controller->channel_abort(dma);
1554 xfer_len = dma->actual_len;
1555 done = true;
1558 DBG(2, "RXCSR%d %04x, reqpkt, len %zu%s\n", epnum, rx_csr,
1559 xfer_len, dma ? ", dma" : "");
1560 rx_csr &= ~MUSB_RXCSR_H_REQPKT;
1562 musb_ep_select(mbase, epnum);
1563 musb_writew(epio, MUSB_RXCSR,
1564 MUSB_RXCSR_H_WZC_BITS | rx_csr);
1566 #endif
1567 if (dma && (rx_csr & MUSB_RXCSR_DMAENAB)) {
1568 xfer_len = dma->actual_len;
1570 val &= ~(MUSB_RXCSR_DMAENAB
1571 | MUSB_RXCSR_H_AUTOREQ
1572 | MUSB_RXCSR_AUTOCLEAR
1573 | MUSB_RXCSR_RXPKTRDY);
1575 if (is_cppi_enabled() || is_cppi41_enabled())
1576 val |= MUSB_RXCSR_DMAENAB;
1578 musb_writew(hw_ep->regs, MUSB_RXCSR, val);
1580 if (usb_pipeisoc(pipe)) {
1581 struct usb_iso_packet_descriptor *d;
1583 d = urb->iso_frame_desc + qh->iso_idx;
1584 d->actual_length = xfer_len;
1586 /* even if there was an error, we did the dma
1587 * for iso_frame_desc->length
1589 if (d->status != EILSEQ && d->status != -EOVERFLOW)
1590 d->status = 0;
1592 if (++qh->iso_idx >= urb->number_of_packets)
1593 done = true;
1594 else if (is_cppi_enabled() || is_cppi41_enabled()) {
1595 struct dma_controller *c;
1596 void *buf;
1597 u32 length, ret;
1599 c = musb->dma_controller;
1600 buf = (void *)
1601 urb->iso_frame_desc[qh->iso_idx].offset
1602 + (u32)urb->transfer_dma;
1604 length =
1605 urb->iso_frame_desc[qh->iso_idx].length;
1607 ret = c->channel_program(dma, qh->maxpacket,
1608 0, (u32) buf, length);
1609 done = false;
1610 } else {
1611 done = false;
1614 } else {
1615 /* done if urb buffer is full or short packet is recd */
1616 done = (urb->actual_length + xfer_len >=
1617 urb->transfer_buffer_length
1618 || dma->actual_len < qh->maxpacket);
1621 /* send IN token for next packet, without AUTOREQ */
1622 if (!done) {
1623 val |= MUSB_RXCSR_H_REQPKT;
1624 musb_writew(epio, MUSB_RXCSR,
1625 MUSB_RXCSR_H_WZC_BITS | val);
1628 DBG(4, "ep %d dma %s, rxcsr %04x, rxcount %d\n", epnum,
1629 done ? "off" : "reset",
1630 musb_readw(epio, MUSB_RXCSR),
1631 musb_readw(epio, MUSB_RXCOUNT));
1632 } else if (urb->status == -EINPROGRESS) {
1633 /* if no errors, be sure a packet is ready for unloading */
1634 if (unlikely(!(rx_csr & MUSB_RXCSR_RXPKTRDY))) {
1635 status = -EPROTO;
1636 ERR("Rx interrupt with no errors or packet!\n");
1638 /* FIXME this is another "SHOULD NEVER HAPPEN" */
1640 /* SCRUB (RX) */
1641 /* do the proper sequence to abort the transfer */
1642 musb_ep_select(mbase, epnum);
1643 val &= ~MUSB_RXCSR_H_REQPKT;
1644 musb_writew(epio, MUSB_RXCSR, val);
1645 goto finish;
1648 /* we are expecting IN packets */
1649 if (dma) {
1650 struct dma_controller *c;
1651 u16 rx_count;
1652 int ret, length;
1653 dma_addr_t buf;
1655 rx_count = musb_readw(epio, MUSB_RXCOUNT);
1657 DBG(2, "RX%d count %d, buffer 0x%x len %d/%d\n",
1658 epnum, rx_count,
1659 urb->transfer_dma
1660 + urb->actual_length,
1661 qh->offset,
1662 urb->transfer_buffer_length);
1664 c = musb->dma_controller;
1666 if (usb_pipeisoc(pipe)) {
1667 int status = 0;
1668 struct usb_iso_packet_descriptor *d;
1670 d = urb->iso_frame_desc + qh->iso_idx;
1672 if (iso_err) {
1673 status = -EILSEQ;
1674 urb->error_count++;
1676 if (rx_count > d->length) {
1677 if (status == 0) {
1678 status = -EOVERFLOW;
1679 urb->error_count++;
1681 DBG(2, "** OVERFLOW %d into %d\n",\
1682 rx_count, d->length);
1684 length = d->length;
1685 } else
1686 length = rx_count;
1687 d->status = status;
1688 buf = urb->transfer_dma + d->offset;
1689 } else {
1690 length = rx_count;
1691 buf = urb->transfer_dma +
1692 urb->actual_length;
1695 dma->desired_mode = 0;
1696 #ifdef USE_MODE1
1697 /* because of the issue below, mode 1 will
1698 * only rarely behave with correct semantics.
1700 if ((urb->transfer_flags &
1701 URB_SHORT_NOT_OK)
1702 && (urb->transfer_buffer_length -
1703 urb->actual_length)
1704 > qh->maxpacket)
1705 dma->desired_mode = 1;
1706 if (rx_count < hw_ep->max_packet_sz_rx) {
1707 length = rx_count;
1708 dma->bDesiredMode = 0;
1709 } else {
1710 length = urb->transfer_buffer_length;
1712 #endif
1714 /* Disadvantage of using mode 1:
1715 * It's basically usable only for mass storage class; essentially all
1716 * other protocols also terminate transfers on short packets.
1718 * Details:
1719 * An extra IN token is sent at the end of the transfer (due to AUTOREQ)
1720 * If you try to use mode 1 for (transfer_buffer_length - 512), and try
1721 * to use the extra IN token to grab the last packet using mode 0, then
1722 * the problem is that you cannot be sure when the device will send the
1723 * last packet and RxPktRdy set. Sometimes the packet is recd too soon
1724 * such that it gets lost when RxCSR is re-set at the end of the mode 1
1725 * transfer, while sometimes it is recd just a little late so that if you
1726 * try to configure for mode 0 soon after the mode 1 transfer is
1727 * completed, you will find rxcount 0. Okay, so you might think why not
1728 * wait for an interrupt when the pkt is recd. Well, you won't get any!
1731 val = musb_readw(epio, MUSB_RXCSR);
1732 val &= ~MUSB_RXCSR_H_REQPKT;
1734 if (dma->desired_mode == 0)
1735 val &= ~MUSB_RXCSR_H_AUTOREQ;
1736 else
1737 val |= MUSB_RXCSR_H_AUTOREQ;
1738 val |= MUSB_RXCSR_DMAENAB;
1740 /* autoclear shouldn't be set in high bandwidth */
1741 if (qh->hb_mult == 1)
1742 val |= MUSB_RXCSR_AUTOCLEAR;
1744 musb_writew(epio, MUSB_RXCSR,
1745 MUSB_RXCSR_H_WZC_BITS | val);
1747 /* REVISIT if when actual_length != 0,
1748 * transfer_buffer_length needs to be
1749 * adjusted first...
1751 ret = c->channel_program(
1752 dma, qh->maxpacket,
1753 dma->desired_mode, buf, length);
1755 if (!ret) {
1756 c->channel_release(dma);
1757 hw_ep->rx_channel = NULL;
1758 dma = NULL;
1759 /* REVISIT reset CSR */
1763 if (!dma) {
1764 done = musb_host_packet_rx(musb, urb,
1765 epnum, iso_err);
1766 DBG(6, "read %spacket\n", done ? "last " : "");
1770 finish:
1771 urb->actual_length += xfer_len;
1772 qh->offset += xfer_len;
1773 if (done) {
1774 if (urb->status == -EINPROGRESS)
1775 urb->status = status;
1776 musb_advance_schedule(musb, urb, hw_ep, USB_DIR_IN);
1780 /* schedule nodes correspond to peripheral endpoints, like an OHCI QH.
1781 * the software schedule associates multiple such nodes with a given
1782 * host side hardware endpoint + direction; scheduling may activate
1783 * that hardware endpoint.
1785 static int musb_schedule(
1786 struct musb *musb,
1787 struct musb_qh *qh,
1788 int is_in)
1790 int idle;
1791 int best_diff;
1792 int best_end, epnum;
1793 struct musb_hw_ep *hw_ep = NULL;
1794 struct list_head *head = NULL;
1796 /* use fixed hardware for control and bulk */
1797 if (qh->type == USB_ENDPOINT_XFER_CONTROL) {
1798 head = &musb->control;
1799 hw_ep = musb->control_ep;
1800 goto success;
1803 /* else, periodic transfers get muxed to other endpoints */
1806 * We know this qh hasn't been scheduled, so all we need to do
1807 * is choose which hardware endpoint to put it on ...
1809 * REVISIT what we really want here is a regular schedule tree
1810 * like e.g. OHCI uses.
1812 best_diff = 4096;
1813 best_end = -1;
1815 for (epnum = 1, hw_ep = musb->endpoints + 1;
1816 epnum < musb->nr_endpoints;
1817 epnum++, hw_ep++) {
1818 int diff;
1820 if (musb_ep_get_qh(hw_ep, is_in) != NULL)
1821 continue;
1823 if (hw_ep == musb->bulk_ep)
1824 continue;
1826 if (is_in)
1827 diff = hw_ep->max_packet_sz_rx;
1828 else
1829 diff = hw_ep->max_packet_sz_tx;
1830 diff -= (qh->maxpacket * qh->hb_mult);
1832 if (diff >= 0 && best_diff > diff) {
1833 best_diff = diff;
1834 best_end = epnum;
1837 /* use bulk reserved ep1 if no other ep is free */
1838 if (best_end < 0 && qh->type == USB_ENDPOINT_XFER_BULK) {
1839 hw_ep = musb->bulk_ep;
1840 if (is_in)
1841 head = &musb->in_bulk;
1842 else
1843 head = &musb->out_bulk;
1845 /* Enable bulk RX NAK timeout scheme when bulk requests are
1846 * multiplexed. This scheme doen't work in high speed to full
1847 * speed scenario as NAK interrupts are not coming from a
1848 * full speed device connected to a high speed device.
1849 * NAK timeout interval is 8 (128 uframe or 16ms) for HS and
1850 * 4 (8 frame or 8ms) for FS device.
1852 if (is_in && qh->dev)
1853 qh->intv_reg =
1854 (USB_SPEED_HIGH == qh->dev->speed) ? 8 : 4;
1855 goto success;
1856 } else if (best_end < 0) {
1857 return -ENOSPC;
1860 idle = 1;
1861 qh->mux = 0;
1862 hw_ep = musb->endpoints + best_end;
1863 DBG(4, "qh %p periodic slot %d\n", qh, best_end);
1864 success:
1865 if (head) {
1866 idle = list_empty(head);
1867 list_add_tail(&qh->ring, head);
1868 qh->mux = 1;
1870 qh->hw_ep = hw_ep;
1871 qh->hep->hcpriv = qh;
1872 if (idle)
1873 musb_start_urb(musb, is_in, qh);
1874 return 0;
1877 static int musb_urb_enqueue(
1878 struct usb_hcd *hcd,
1879 struct urb *urb,
1880 gfp_t mem_flags)
1882 unsigned long flags;
1883 struct musb *musb = hcd_to_musb(hcd);
1884 struct usb_host_endpoint *hep = urb->ep;
1885 struct musb_qh *qh;
1886 struct usb_endpoint_descriptor *epd = &hep->desc;
1887 int ret;
1888 unsigned type_reg;
1889 unsigned interval;
1891 /* host role must be active */
1892 if (!is_host_active(musb) || !musb->is_active)
1893 return -ENODEV;
1895 spin_lock_irqsave(&musb->lock, flags);
1896 ret = usb_hcd_link_urb_to_ep(hcd, urb);
1897 qh = ret ? NULL : hep->hcpriv;
1898 if (qh)
1899 urb->hcpriv = qh;
1900 spin_unlock_irqrestore(&musb->lock, flags);
1902 /* DMA mapping was already done, if needed, and this urb is on
1903 * hep->urb_list now ... so we're done, unless hep wasn't yet
1904 * scheduled onto a live qh.
1906 * REVISIT best to keep hep->hcpriv valid until the endpoint gets
1907 * disabled, testing for empty qh->ring and avoiding qh setup costs
1908 * except for the first urb queued after a config change.
1910 if (qh || ret)
1911 return ret;
1913 /* Allocate and initialize qh, minimizing the work done each time
1914 * hw_ep gets reprogrammed, or with irqs blocked. Then schedule it.
1916 * REVISIT consider a dedicated qh kmem_cache, so it's harder
1917 * for bugs in other kernel code to break this driver...
1919 qh = kzalloc(sizeof *qh, mem_flags);
1920 if (!qh) {
1921 spin_lock_irqsave(&musb->lock, flags);
1922 usb_hcd_unlink_urb_from_ep(hcd, urb);
1923 spin_unlock_irqrestore(&musb->lock, flags);
1924 return -ENOMEM;
1927 qh->hep = hep;
1928 qh->dev = urb->dev;
1929 INIT_LIST_HEAD(&qh->ring);
1930 qh->is_ready = 1;
1932 qh->maxpacket = le16_to_cpu(epd->wMaxPacketSize);
1933 qh->type = usb_endpoint_type(epd);
1935 /* Bits 11 & 12 of wMaxPacketSize encode high bandwidth multiplier.
1936 * Some musb cores don't support high bandwidth ISO transfers; and
1937 * we don't (yet!) support high bandwidth interrupt transfers.
1939 qh->hb_mult = 1 + ((qh->maxpacket >> 11) & 0x03);
1940 if (qh->hb_mult > 1) {
1941 int ok = (qh->type == USB_ENDPOINT_XFER_ISOC);
1943 if (ok)
1944 ok = (usb_pipein(urb->pipe) && musb->hb_iso_rx)
1945 || (usb_pipeout(urb->pipe) && musb->hb_iso_tx);
1946 if (!ok) {
1947 ret = -EMSGSIZE;
1948 goto done;
1950 qh->maxpacket &= 0x7ff;
1953 qh->epnum = usb_endpoint_num(epd);
1955 /* NOTE: urb->dev->devnum is wrong during SET_ADDRESS */
1956 qh->addr_reg = (u8) usb_pipedevice(urb->pipe);
1958 /* precompute rxtype/txtype/type0 register */
1959 type_reg = (qh->type << 4) | qh->epnum;
1960 switch (urb->dev->speed) {
1961 case USB_SPEED_LOW:
1962 type_reg |= 0xc0;
1963 break;
1964 case USB_SPEED_FULL:
1965 type_reg |= 0x80;
1966 break;
1967 default:
1968 type_reg |= 0x40;
1970 qh->type_reg = type_reg;
1972 /* Precompute RXINTERVAL/TXINTERVAL register */
1973 switch (qh->type) {
1974 case USB_ENDPOINT_XFER_INT:
1976 * Full/low speeds use the linear encoding,
1977 * high speed uses the logarithmic encoding.
1979 if (urb->dev->speed <= USB_SPEED_FULL) {
1980 interval = max_t(u8, epd->bInterval, 1);
1981 break;
1983 /* FALLTHROUGH */
1984 case USB_ENDPOINT_XFER_ISOC:
1985 /* ISO always uses logarithmic encoding */
1986 interval = min_t(u8, epd->bInterval, 16);
1987 break;
1988 default:
1989 /* REVISIT we actually want to use NAK limits, hinting to the
1990 * transfer scheduling logic to try some other qh, e.g. try
1991 * for 2 msec first:
1993 * interval = (USB_SPEED_HIGH == urb->dev->speed) ? 16 : 2;
1995 * The downside of disabling this is that transfer scheduling
1996 * gets VERY unfair for nonperiodic transfers; a misbehaving
1997 * peripheral could make that hurt. That's perfectly normal
1998 * for reads from network or serial adapters ... so we have
1999 * partial NAKlimit support for bulk RX.
2001 * The upside of disabling it is simpler transfer scheduling.
2003 interval = 0;
2005 qh->intv_reg = interval;
2007 /* precompute addressing for external hub/tt ports */
2008 if (musb->is_multipoint) {
2009 struct usb_device *parent = urb->dev->parent;
2011 if (parent != hcd->self.root_hub) {
2012 qh->h_addr_reg = (u8) parent->devnum;
2014 /* set up tt info if needed */
2015 if (urb->dev->tt) {
2016 qh->h_port_reg = (u8) urb->dev->ttport;
2017 if (urb->dev->tt->hub)
2018 qh->h_addr_reg =
2019 (u8) urb->dev->tt->hub->devnum;
2020 if (urb->dev->tt->multi)
2021 qh->h_addr_reg |= 0x80;
2026 /* invariant: hep->hcpriv is null OR the qh that's already scheduled.
2027 * until we get real dma queues (with an entry for each urb/buffer),
2028 * we only have work to do in the former case.
2030 spin_lock_irqsave(&musb->lock, flags);
2031 if (hep->hcpriv) {
2032 /* some concurrent activity submitted another urb to hep...
2033 * odd, rare, error prone, but legal.
2035 kfree(qh);
2036 ret = 0;
2037 } else
2038 ret = musb_schedule(musb, qh,
2039 epd->bEndpointAddress & USB_ENDPOINT_DIR_MASK);
2041 if (ret == 0) {
2042 urb->hcpriv = qh;
2043 /* FIXME set urb->start_frame for iso/intr, it's tested in
2044 * musb_start_urb(), but otherwise only konicawc cares ...
2047 spin_unlock_irqrestore(&musb->lock, flags);
2049 done:
2050 if (ret != 0) {
2051 spin_lock_irqsave(&musb->lock, flags);
2052 usb_hcd_unlink_urb_from_ep(hcd, urb);
2053 spin_unlock_irqrestore(&musb->lock, flags);
2054 kfree(qh);
2056 return ret;
2061 * abort a transfer that's at the head of a hardware queue.
2062 * called with controller locked, irqs blocked
2063 * that hardware queue advances to the next transfer, unless prevented
2065 static int musb_cleanup_urb(struct urb *urb, struct musb_qh *qh)
2067 struct musb_hw_ep *ep = qh->hw_ep;
2068 void __iomem *epio = ep->regs;
2069 unsigned hw_end = ep->epnum;
2070 void __iomem *regs = ep->musb->mregs;
2071 int is_in = usb_pipein(urb->pipe);
2072 int status = 0;
2073 u16 csr;
2075 musb_ep_select(regs, hw_end);
2077 if (is_dma_capable()) {
2078 struct dma_channel *dma;
2080 dma = is_in ? ep->rx_channel : ep->tx_channel;
2081 if (dma) {
2082 status = ep->musb->dma_controller->channel_abort(dma);
2083 DBG(status ? 1 : 3,
2084 "abort %cX%d DMA for urb %p --> %d\n",
2085 is_in ? 'R' : 'T', ep->epnum,
2086 urb, status);
2087 urb->actual_length += dma->actual_len;
2091 /* turn off DMA requests, discard state, stop polling ... */
2092 if (is_in) {
2093 /* giveback saves bulk toggle */
2094 csr = musb_h_flush_rxfifo(ep, 0);
2096 /* REVISIT we still get an irq; should likely clear the
2097 * endpoint's irq status here to avoid bogus irqs.
2098 * clearing that status is platform-specific...
2100 } else if (ep->epnum) {
2101 musb_h_tx_flush_fifo(ep);
2102 csr = musb_readw(epio, MUSB_TXCSR);
2103 csr &= ~(MUSB_TXCSR_AUTOSET
2104 | MUSB_TXCSR_DMAENAB
2105 | MUSB_TXCSR_H_RXSTALL
2106 | MUSB_TXCSR_H_NAKTIMEOUT
2107 | MUSB_TXCSR_H_ERROR
2108 | MUSB_TXCSR_TXPKTRDY);
2109 musb_writew(epio, MUSB_TXCSR, csr);
2110 /* REVISIT may need to clear FLUSHFIFO ... */
2111 musb_writew(epio, MUSB_TXCSR, csr);
2112 /* flush cpu writebuffer */
2113 csr = musb_readw(epio, MUSB_TXCSR);
2114 } else {
2115 musb_h_ep0_flush_fifo(ep);
2117 if (status == 0)
2118 musb_advance_schedule(ep->musb, urb, ep, is_in);
2119 return status;
2122 static int musb_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
2124 struct musb *musb = hcd_to_musb(hcd);
2125 struct musb_qh *qh;
2126 unsigned long flags;
2127 int is_in = usb_pipein(urb->pipe);
2128 int ret;
2130 DBG(4, "urb=%p, dev%d ep%d%s\n", urb,
2131 usb_pipedevice(urb->pipe),
2132 usb_pipeendpoint(urb->pipe),
2133 is_in ? "in" : "out");
2135 spin_lock_irqsave(&musb->lock, flags);
2136 ret = usb_hcd_check_unlink_urb(hcd, urb, status);
2137 if (ret)
2138 goto done;
2140 qh = urb->hcpriv;
2141 if (!qh)
2142 goto done;
2145 * Any URB not actively programmed into endpoint hardware can be
2146 * immediately given back; that's any URB not at the head of an
2147 * endpoint queue, unless someday we get real DMA queues. And even
2148 * if it's at the head, it might not be known to the hardware...
2150 * Otherwise abort current transfer, pending DMA, etc.; urb->status
2151 * has already been updated. This is a synchronous abort; it'd be
2152 * OK to hold off until after some IRQ, though.
2154 * NOTE: qh is invalid unless !list_empty(&hep->urb_list)
2156 if (!qh->is_ready
2157 || urb->urb_list.prev != &qh->hep->urb_list
2158 || musb_ep_get_qh(qh->hw_ep, is_in) != qh) {
2159 int ready = qh->is_ready;
2161 qh->is_ready = 0;
2162 musb_giveback(musb, urb, 0);
2163 qh->is_ready = ready;
2165 /* If nothing else (usually musb_giveback) is using it
2166 * and its URB list has emptied, recycle this qh.
2168 if (ready && list_empty(&qh->hep->urb_list)) {
2169 qh->hep->hcpriv = NULL;
2170 list_del(&qh->ring);
2171 kfree(qh);
2173 } else
2174 ret = musb_cleanup_urb(urb, qh);
2175 done:
2176 spin_unlock_irqrestore(&musb->lock, flags);
2177 return ret;
2180 /* disable an endpoint */
2181 static void
2182 musb_h_disable(struct usb_hcd *hcd, struct usb_host_endpoint *hep)
2184 u8 is_in = hep->desc.bEndpointAddress & USB_DIR_IN;
2185 unsigned long flags;
2186 struct musb *musb = hcd_to_musb(hcd);
2187 struct musb_qh *qh;
2188 struct urb *urb;
2190 spin_lock_irqsave(&musb->lock, flags);
2192 qh = hep->hcpriv;
2193 if (qh == NULL)
2194 goto exit;
2196 /* NOTE: qh is invalid unless !list_empty(&hep->urb_list) */
2198 /* Kick the first URB off the hardware, if needed */
2199 qh->is_ready = 0;
2200 if (musb_ep_get_qh(qh->hw_ep, is_in) == qh) {
2201 urb = next_urb(qh);
2203 /* make software (then hardware) stop ASAP */
2204 if (!urb->unlinked)
2205 urb->status = -ESHUTDOWN;
2207 /* cleanup */
2208 musb_cleanup_urb(urb, qh);
2210 /* Then nuke all the others ... and advance the
2211 * queue on hw_ep (e.g. bulk ring) when we're done.
2213 while (!list_empty(&hep->urb_list)) {
2214 urb = next_urb(qh);
2215 urb->status = -ESHUTDOWN;
2216 musb_advance_schedule(musb, urb, qh->hw_ep, is_in);
2218 } else {
2219 /* Just empty the queue; the hardware is busy with
2220 * other transfers, and since !qh->is_ready nothing
2221 * will activate any of these as it advances.
2223 while (!list_empty(&hep->urb_list))
2224 musb_giveback(musb, next_urb(qh), -ESHUTDOWN);
2226 hep->hcpriv = NULL;
2227 list_del(&qh->ring);
2228 kfree(qh);
2230 exit:
2231 spin_unlock_irqrestore(&musb->lock, flags);
2234 static int musb_h_get_frame_number(struct usb_hcd *hcd)
2236 struct musb *musb = hcd_to_musb(hcd);
2238 return musb_readw(musb->mregs, MUSB_FRAME);
2241 static int musb_h_start(struct usb_hcd *hcd)
2243 struct musb *musb = hcd_to_musb(hcd);
2245 /* NOTE: musb_start() is called when the hub driver turns
2246 * on port power, or when (OTG) peripheral starts.
2248 hcd->state = HC_STATE_RUNNING;
2249 musb->port1_status = 0;
2250 return 0;
2253 static void musb_h_stop(struct usb_hcd *hcd)
2255 musb_stop(hcd_to_musb(hcd));
2256 hcd->state = HC_STATE_HALT;
2259 static int musb_bus_suspend(struct usb_hcd *hcd)
2261 struct musb *musb = hcd_to_musb(hcd);
2262 u8 devctl;
2264 if (!is_host_active(musb))
2265 return 0;
2267 switch (musb->xceiv->state) {
2268 case OTG_STATE_A_SUSPEND:
2269 return 0;
2270 case OTG_STATE_A_WAIT_VRISE:
2271 /* ID could be grounded even if there's no device
2272 * on the other end of the cable. NOTE that the
2273 * A_WAIT_VRISE timers are messy with MUSB...
2275 devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
2276 if ((devctl & MUSB_DEVCTL_VBUS) == MUSB_DEVCTL_VBUS)
2277 musb->xceiv->state = OTG_STATE_A_WAIT_BCON;
2278 break;
2279 default:
2280 break;
2283 if (musb->is_active) {
2284 DBG(3, "trying to suspend as %s while active\n",
2285 otg_state_string(musb));
2286 return -EBUSY;
2287 } else
2288 return 0;
2291 static int musb_bus_resume(struct usb_hcd *hcd)
2293 /* resuming child port does the work */
2294 return 0;
2297 const struct hc_driver musb_hc_driver = {
2298 .description = "musb-hcd",
2299 .product_desc = "MUSB HDRC host driver",
2300 .hcd_priv_size = sizeof(struct musb),
2301 .flags = HCD_USB2 | HCD_MEMORY,
2303 /* not using irq handler or reset hooks from usbcore, since
2304 * those must be shared with peripheral code for OTG configs
2307 .start = musb_h_start,
2308 .stop = musb_h_stop,
2310 .get_frame_number = musb_h_get_frame_number,
2312 .urb_enqueue = musb_urb_enqueue,
2313 .urb_dequeue = musb_urb_dequeue,
2314 .endpoint_disable = musb_h_disable,
2316 .hub_status_data = musb_hub_status_data,
2317 .hub_control = musb_hub_control,
2318 .bus_suspend = musb_bus_suspend,
2319 .bus_resume = musb_bus_resume,
2320 /* .start_port_reset = NULL, */
2321 /* .hub_irq_enable = NULL, */