blob: 2f2fc7c1359b39e115a53118c867484cd592b2e6 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0
Ilya Yanok06bb9202012-11-06 13:48:21 +00002/*
3 * MUSB OTG driver host support
4 *
5 * Copyright 2005 Mentor Graphics Corporation
6 * Copyright (C) 2005-2006 by Texas Instruments
7 * Copyright (C) 2006-2007 Nokia Corporation
8 * Copyright (C) 2008-2009 MontaVista Software, Inc. <source@mvista.com>
Ilya Yanok06bb9202012-11-06 13:48:21 +00009 */
10
Ilya Yanok06bb9202012-11-06 13:48:21 +000011#ifndef __UBOOT__
Simon Glass9bc15642020-02-03 07:36:16 -070012#include <dm/device_compat.h>
Simon Glassd66c5f72020-02-03 07:36:15 -070013#include <dm/devres.h>
Ilya Yanok06bb9202012-11-06 13:48:21 +000014#include <linux/module.h>
15#include <linux/kernel.h>
16#include <linux/delay.h>
17#include <linux/sched.h>
18#include <linux/slab.h>
19#include <linux/errno.h>
20#include <linux/init.h>
21#include <linux/list.h>
22#include <linux/dma-mapping.h>
23#else
Sean Anderson3e464862020-10-04 21:39:54 -040024#include <dm.h>
25#include <dm/device_compat.h>
Ilya Yanok06bb9202012-11-06 13:48:21 +000026#include <usb.h>
Simon Glassc06c1be2020-05-10 11:40:08 -060027#include <linux/bug.h>
Rui Miguel Silvafb0c70c2022-06-29 11:06:14 +010028#include <linux/usb/usb_urb_compat.h>
Ilya Yanok06bb9202012-11-06 13:48:21 +000029#include "linux-compat.h"
Ilya Yanok06bb9202012-11-06 13:48:21 +000030#endif
31
32#include "musb_core.h"
33#include "musb_host.h"
34
35
36/* MUSB HOST status 22-mar-2006
37 *
38 * - There's still lots of partial code duplication for fault paths, so
39 * they aren't handled as consistently as they need to be.
40 *
41 * - PIO mostly behaved when last tested.
42 * + including ep0, with all usbtest cases 9, 10
43 * + usbtest 14 (ep0out) doesn't seem to run at all
44 * + double buffered OUT/TX endpoints saw stalls(!) with certain usbtest
45 * configurations, but otherwise double buffering passes basic tests.
46 * + for 2.6.N, for N > ~10, needs API changes for hcd framework.
47 *
48 * - DMA (CPPI) ... partially behaves, not currently recommended
49 * + about 1/15 the speed of typical EHCI implementations (PCI)
50 * + RX, all too often reqpkt seems to misbehave after tx
51 * + TX, no known issues (other than evident silicon issue)
52 *
53 * - DMA (Mentor/OMAP) ...has at least toggle update problems
54 *
55 * - [23-feb-2009] minimal traffic scheduling to avoid bulk RX packet
56 * starvation ... nothing yet for TX, interrupt, or bulk.
57 *
58 * - Not tested with HNP, but some SRP paths seem to behave.
59 *
60 * NOTE 24-August-2006:
61 *
62 * - Bulk traffic finally uses both sides of hardware ep1, freeing up an
63 * extra endpoint for periodic use enabling hub + keybd + mouse. That
64 * mostly works, except that with "usbnet" it's easy to trigger cases
65 * with "ping" where RX loses. (a) ping to davinci, even "ping -f",
66 * fine; but (b) ping _from_ davinci, even "ping -c 1", ICMP RX loses
67 * although ARP RX wins. (That test was done with a full speed link.)
68 */
69
70
71/*
72 * NOTE on endpoint usage:
73 *
74 * CONTROL transfers all go through ep0. BULK ones go through dedicated IN
75 * and OUT endpoints ... hardware is dedicated for those "async" queue(s).
76 * (Yes, bulk _could_ use more of the endpoints than that, and would even
77 * benefit from it.)
78 *
79 * INTERUPPT and ISOCHRONOUS transfers are scheduled to the other endpoints.
80 * So far that scheduling is both dumb and optimistic: the endpoint will be
81 * "claimed" until its software queue is no longer refilled. No multiplexing
82 * of transfers between endpoints, or anything clever.
83 */
84
85
86static void musb_ep_program(struct musb *musb, u8 epnum,
87 struct urb *urb, int is_out,
88 u8 *buf, u32 offset, u32 len);
89
90/*
91 * Clear TX fifo. Needed to avoid BABBLE errors.
92 */
93static void musb_h_tx_flush_fifo(struct musb_hw_ep *ep)
94{
95 struct musb *musb = ep->musb;
96 void __iomem *epio = ep->regs;
97 u16 csr;
98 u16 lastcsr = 0;
99 int retries = 1000;
100
101 csr = musb_readw(epio, MUSB_TXCSR);
102 while (csr & MUSB_TXCSR_FIFONOTEMPTY) {
103 if (csr != lastcsr)
104 dev_dbg(musb->controller, "Host TX FIFONOTEMPTY csr: %02x\n", csr);
105 lastcsr = csr;
106 csr |= MUSB_TXCSR_FLUSHFIFO;
107 musb_writew(epio, MUSB_TXCSR, csr);
108 csr = musb_readw(epio, MUSB_TXCSR);
109 if (WARN(retries-- < 1,
110 "Could not flush host TX%d fifo: csr: %04x\n",
111 ep->epnum, csr))
112 return;
113 mdelay(1);
114 }
115}
116
117static void musb_h_ep0_flush_fifo(struct musb_hw_ep *ep)
118{
119 void __iomem *epio = ep->regs;
120 u16 csr;
121 int retries = 5;
122
123 /* scrub any data left in the fifo */
124 do {
125 csr = musb_readw(epio, MUSB_TXCSR);
126 if (!(csr & (MUSB_CSR0_TXPKTRDY | MUSB_CSR0_RXPKTRDY)))
127 break;
128 musb_writew(epio, MUSB_TXCSR, MUSB_CSR0_FLUSHFIFO);
129 csr = musb_readw(epio, MUSB_TXCSR);
130 udelay(10);
131 } while (--retries);
132
133 WARN(!retries, "Could not flush host TX%d fifo: csr: %04x\n",
134 ep->epnum, csr);
135
136 /* and reset for the next transfer */
137 musb_writew(epio, MUSB_TXCSR, 0);
138}
139
140/*
141 * Start transmit. Caller is responsible for locking shared resources.
142 * musb must be locked.
143 */
144static inline void musb_h_tx_start(struct musb_hw_ep *ep)
145{
146 u16 txcsr;
147
148 /* NOTE: no locks here; caller should lock and select EP */
149 if (ep->epnum) {
150 txcsr = musb_readw(ep->regs, MUSB_TXCSR);
151 txcsr |= MUSB_TXCSR_TXPKTRDY | MUSB_TXCSR_H_WZC_BITS;
152 musb_writew(ep->regs, MUSB_TXCSR, txcsr);
153 } else {
154 txcsr = MUSB_CSR0_H_SETUPPKT | MUSB_CSR0_TXPKTRDY;
155 musb_writew(ep->regs, MUSB_CSR0, txcsr);
156 }
157
158}
159
160static inline void musb_h_tx_dma_start(struct musb_hw_ep *ep)
161{
162 u16 txcsr;
163
164 /* NOTE: no locks here; caller should lock and select EP */
165 txcsr = musb_readw(ep->regs, MUSB_TXCSR);
166 txcsr |= MUSB_TXCSR_DMAENAB | MUSB_TXCSR_H_WZC_BITS;
167 if (is_cppi_enabled())
168 txcsr |= MUSB_TXCSR_DMAMODE;
169 musb_writew(ep->regs, MUSB_TXCSR, txcsr);
170}
171
172static void musb_ep_set_qh(struct musb_hw_ep *ep, int is_in, struct musb_qh *qh)
173{
174 if (is_in != 0 || ep->is_shared_fifo)
175 ep->in_qh = qh;
176 if (is_in == 0 || ep->is_shared_fifo)
177 ep->out_qh = qh;
178}
179
180static struct musb_qh *musb_ep_get_qh(struct musb_hw_ep *ep, int is_in)
181{
182 return is_in ? ep->in_qh : ep->out_qh;
183}
184
185/*
186 * Start the URB at the front of an endpoint's queue
187 * end must be claimed from the caller.
188 *
189 * Context: controller locked, irqs blocked
190 */
191static void
192musb_start_urb(struct musb *musb, int is_in, struct musb_qh *qh)
193{
194 u16 frame;
195 u32 len;
196 void __iomem *mbase = musb->mregs;
197 struct urb *urb = next_urb(qh);
198 void *buf = urb->transfer_buffer;
199 u32 offset = 0;
200 struct musb_hw_ep *hw_ep = qh->hw_ep;
201 unsigned pipe = urb->pipe;
202 u8 address = usb_pipedevice(pipe);
203 int epnum = hw_ep->epnum;
204
205 /* initialize software qh state */
206 qh->offset = 0;
207 qh->segsize = 0;
208
209 /* gather right source of data */
210 switch (qh->type) {
211 case USB_ENDPOINT_XFER_CONTROL:
212 /* control transfers always start with SETUP */
213 is_in = 0;
214 musb->ep0_stage = MUSB_EP0_START;
215 buf = urb->setup_packet;
216 len = 8;
217 break;
218#ifndef __UBOOT__
219 case USB_ENDPOINT_XFER_ISOC:
220 qh->iso_idx = 0;
221 qh->frame = 0;
222 offset = urb->iso_frame_desc[0].offset;
223 len = urb->iso_frame_desc[0].length;
224 break;
225#endif
226 default: /* bulk, interrupt */
227 /* actual_length may be nonzero on retry paths */
228 buf = urb->transfer_buffer + urb->actual_length;
229 len = urb->transfer_buffer_length - urb->actual_length;
230 }
231
232 dev_dbg(musb->controller, "qh %p urb %p dev%d ep%d%s%s, hw_ep %d, %p/%d\n",
233 qh, urb, address, qh->epnum,
234 is_in ? "in" : "out",
235 ({char *s; switch (qh->type) {
236 case USB_ENDPOINT_XFER_CONTROL: s = ""; break;
237 case USB_ENDPOINT_XFER_BULK: s = "-bulk"; break;
238#ifndef __UBOOT__
239 case USB_ENDPOINT_XFER_ISOC: s = "-iso"; break;
240#endif
241 default: s = "-intr"; break;
242 }; s; }),
243 epnum, buf + offset, len);
244
245 /* Configure endpoint */
246 musb_ep_set_qh(hw_ep, is_in, qh);
247 musb_ep_program(musb, epnum, urb, !is_in, buf, offset, len);
248
249 /* transmit may have more work: start it when it is time */
250 if (is_in)
251 return;
252
253 /* determine if the time is right for a periodic transfer */
254 switch (qh->type) {
255#ifndef __UBOOT__
256 case USB_ENDPOINT_XFER_ISOC:
257#endif
258 case USB_ENDPOINT_XFER_INT:
259 dev_dbg(musb->controller, "check whether there's still time for periodic Tx\n");
260 frame = musb_readw(mbase, MUSB_FRAME);
261 /* FIXME this doesn't implement that scheduling policy ...
262 * or handle framecounter wrapping
263 */
264#ifndef __UBOOT__
265 if ((urb->transfer_flags & URB_ISO_ASAP)
266 || (frame >= urb->start_frame)) {
267 /* REVISIT the SOF irq handler shouldn't duplicate
268 * this code; and we don't init urb->start_frame...
269 */
270 qh->frame = 0;
271 goto start;
272 } else {
273#endif
274 qh->frame = urb->start_frame;
275 /* enable SOF interrupt so we can count down */
276 dev_dbg(musb->controller, "SOF for %d\n", epnum);
277#if 1 /* ifndef CONFIG_ARCH_DAVINCI */
278 musb_writeb(mbase, MUSB_INTRUSBE, 0xff);
279#endif
280#ifndef __UBOOT__
281 }
282#endif
283 break;
284 default:
285start:
286 dev_dbg(musb->controller, "Start TX%d %s\n", epnum,
287 hw_ep->tx_channel ? "dma" : "pio");
288
289 if (!hw_ep->tx_channel)
290 musb_h_tx_start(hw_ep);
291 else if (is_cppi_enabled() || tusb_dma_omap())
292 musb_h_tx_dma_start(hw_ep);
293 }
294}
295
296/* Context: caller owns controller lock, IRQs are blocked */
297static void musb_giveback(struct musb *musb, struct urb *urb, int status)
298__releases(musb->lock)
299__acquires(musb->lock)
300{
301 dev_dbg(musb->controller,
302 "complete %p %pF (%d), dev%d ep%d%s, %d/%d\n",
303 urb, urb->complete, status,
304 usb_pipedevice(urb->pipe),
305 usb_pipeendpoint(urb->pipe),
306 usb_pipein(urb->pipe) ? "in" : "out",
307 urb->actual_length, urb->transfer_buffer_length
308 );
309
310 usb_hcd_unlink_urb_from_ep(musb_to_hcd(musb), urb);
311 spin_unlock(&musb->lock);
312 usb_hcd_giveback_urb(musb_to_hcd(musb), urb, status);
313 spin_lock(&musb->lock);
314}
315
316/* For bulk/interrupt endpoints only */
317static inline void musb_save_toggle(struct musb_qh *qh, int is_in,
318 struct urb *urb)
319{
320 void __iomem *epio = qh->hw_ep->regs;
321 u16 csr;
322
323 /*
324 * FIXME: the current Mentor DMA code seems to have
325 * problems getting toggle correct.
326 */
327
328 if (is_in)
329 csr = musb_readw(epio, MUSB_RXCSR) & MUSB_RXCSR_H_DATATOGGLE;
330 else
331 csr = musb_readw(epio, MUSB_TXCSR) & MUSB_TXCSR_H_DATATOGGLE;
332
333 usb_settoggle(urb->dev, qh->epnum, !is_in, csr ? 1 : 0);
334}
335
336/*
337 * Advance this hardware endpoint's queue, completing the specified URB and
338 * advancing to either the next URB queued to that qh, or else invalidating
339 * that qh and advancing to the next qh scheduled after the current one.
340 *
341 * Context: caller owns controller lock, IRQs are blocked
342 */
343static void musb_advance_schedule(struct musb *musb, struct urb *urb,
344 struct musb_hw_ep *hw_ep, int is_in)
345{
346 struct musb_qh *qh = musb_ep_get_qh(hw_ep, is_in);
347 struct musb_hw_ep *ep = qh->hw_ep;
348 int ready = qh->is_ready;
349 int status;
350
351 status = (urb->status == -EINPROGRESS) ? 0 : urb->status;
352
353 /* save toggle eagerly, for paranoia */
354 switch (qh->type) {
355 case USB_ENDPOINT_XFER_BULK:
356 case USB_ENDPOINT_XFER_INT:
357 musb_save_toggle(qh, is_in, urb);
358 break;
359#ifndef __UBOOT__
360 case USB_ENDPOINT_XFER_ISOC:
361 if (status == 0 && urb->error_count)
362 status = -EXDEV;
363 break;
364#endif
365 }
366
367 qh->is_ready = 0;
368 musb_giveback(musb, urb, status);
369 qh->is_ready = ready;
370
371 /* reclaim resources (and bandwidth) ASAP; deschedule it, and
372 * invalidate qh as soon as list_empty(&hep->urb_list)
373 */
374 if (list_empty(&qh->hep->urb_list)) {
375 struct list_head *head;
376 struct dma_controller *dma = musb->dma_controller;
377
378 if (is_in) {
379 ep->rx_reinit = 1;
380 if (ep->rx_channel) {
381 dma->channel_release(ep->rx_channel);
382 ep->rx_channel = NULL;
383 }
384 } else {
385 ep->tx_reinit = 1;
386 if (ep->tx_channel) {
387 dma->channel_release(ep->tx_channel);
388 ep->tx_channel = NULL;
389 }
390 }
391
392 /* Clobber old pointers to this qh */
393 musb_ep_set_qh(ep, is_in, NULL);
394 qh->hep->hcpriv = NULL;
395
396 switch (qh->type) {
397
398 case USB_ENDPOINT_XFER_CONTROL:
399 case USB_ENDPOINT_XFER_BULK:
400 /* fifo policy for these lists, except that NAKing
401 * should rotate a qh to the end (for fairness).
402 */
403 if (qh->mux == 1) {
404 head = qh->ring.prev;
405 list_del(&qh->ring);
406 kfree(qh);
407 qh = first_qh(head);
408 break;
409 }
410
411 case USB_ENDPOINT_XFER_ISOC:
412 case USB_ENDPOINT_XFER_INT:
413 /* this is where periodic bandwidth should be
414 * de-allocated if it's tracked and allocated;
415 * and where we'd update the schedule tree...
416 */
417 kfree(qh);
418 qh = NULL;
419 break;
420 }
421 }
422
423 if (qh != NULL && qh->is_ready) {
424 dev_dbg(musb->controller, "... next ep%d %cX urb %p\n",
425 hw_ep->epnum, is_in ? 'R' : 'T', next_urb(qh));
426 musb_start_urb(musb, is_in, qh);
427 }
428}
429
430static u16 musb_h_flush_rxfifo(struct musb_hw_ep *hw_ep, u16 csr)
431{
432 /* we don't want fifo to fill itself again;
433 * ignore dma (various models),
434 * leave toggle alone (may not have been saved yet)
435 */
436 csr |= MUSB_RXCSR_FLUSHFIFO | MUSB_RXCSR_RXPKTRDY;
437 csr &= ~(MUSB_RXCSR_H_REQPKT
438 | MUSB_RXCSR_H_AUTOREQ
439 | MUSB_RXCSR_AUTOCLEAR);
440
441 /* write 2x to allow double buffering */
442 musb_writew(hw_ep->regs, MUSB_RXCSR, csr);
443 musb_writew(hw_ep->regs, MUSB_RXCSR, csr);
444
445 /* flush writebuffer */
446 return musb_readw(hw_ep->regs, MUSB_RXCSR);
447}
448
449/*
450 * PIO RX for a packet (or part of it).
451 */
452static bool
453musb_host_packet_rx(struct musb *musb, struct urb *urb, u8 epnum, u8 iso_err)
454{
455 u16 rx_count;
456 u8 *buf;
457 u16 csr;
458 bool done = false;
459 u32 length;
460 int do_flush = 0;
461 struct musb_hw_ep *hw_ep = musb->endpoints + epnum;
462 void __iomem *epio = hw_ep->regs;
463 struct musb_qh *qh = hw_ep->in_qh;
464 int pipe = urb->pipe;
465 void *buffer = urb->transfer_buffer;
466
467 /* musb_ep_select(mbase, epnum); */
468 rx_count = musb_readw(epio, MUSB_RXCOUNT);
469 dev_dbg(musb->controller, "RX%d count %d, buffer %p len %d/%d\n", epnum, rx_count,
470 urb->transfer_buffer, qh->offset,
471 urb->transfer_buffer_length);
472
473 /* unload FIFO */
474#ifndef __UBOOT__
475 if (usb_pipeisoc(pipe)) {
476 int status = 0;
477 struct usb_iso_packet_descriptor *d;
478
479 if (iso_err) {
480 status = -EILSEQ;
481 urb->error_count++;
482 }
483
484 d = urb->iso_frame_desc + qh->iso_idx;
485 buf = buffer + d->offset;
486 length = d->length;
487 if (rx_count > length) {
488 if (status == 0) {
489 status = -EOVERFLOW;
490 urb->error_count++;
491 }
492 dev_dbg(musb->controller, "** OVERFLOW %d into %d\n", rx_count, length);
493 do_flush = 1;
494 } else
495 length = rx_count;
496 urb->actual_length += length;
497 d->actual_length = length;
498
499 d->status = status;
500
501 /* see if we are done */
502 done = (++qh->iso_idx >= urb->number_of_packets);
503 } else {
504#endif
505 /* non-isoch */
506 buf = buffer + qh->offset;
507 length = urb->transfer_buffer_length - qh->offset;
508 if (rx_count > length) {
509 if (urb->status == -EINPROGRESS)
510 urb->status = -EOVERFLOW;
511 dev_dbg(musb->controller, "** OVERFLOW %d into %d\n", rx_count, length);
512 do_flush = 1;
513 } else
514 length = rx_count;
515 urb->actual_length += length;
516 qh->offset += length;
517
518 /* see if we are done */
519 done = (urb->actual_length == urb->transfer_buffer_length)
520 || (rx_count < qh->maxpacket)
521 || (urb->status != -EINPROGRESS);
522 if (done
523 && (urb->status == -EINPROGRESS)
524 && (urb->transfer_flags & URB_SHORT_NOT_OK)
525 && (urb->actual_length
526 < urb->transfer_buffer_length))
527 urb->status = -EREMOTEIO;
528#ifndef __UBOOT__
529 }
530#endif
531
532 musb_read_fifo(hw_ep, length, buf);
533
534 csr = musb_readw(epio, MUSB_RXCSR);
535 csr |= MUSB_RXCSR_H_WZC_BITS;
536 if (unlikely(do_flush))
537 musb_h_flush_rxfifo(hw_ep, csr);
538 else {
539 /* REVISIT this assumes AUTOCLEAR is never set */
540 csr &= ~(MUSB_RXCSR_RXPKTRDY | MUSB_RXCSR_H_REQPKT);
541 if (!done)
542 csr |= MUSB_RXCSR_H_REQPKT;
543 musb_writew(epio, MUSB_RXCSR, csr);
544 }
545
546 return done;
547}
548
549/* we don't always need to reinit a given side of an endpoint...
550 * when we do, use tx/rx reinit routine and then construct a new CSR
551 * to address data toggle, NYET, and DMA or PIO.
552 *
553 * it's possible that driver bugs (especially for DMA) or aborting a
554 * transfer might have left the endpoint busier than it should be.
555 * the busy/not-empty tests are basically paranoia.
556 */
557static void
558musb_rx_reinit(struct musb *musb, struct musb_qh *qh, struct musb_hw_ep *ep)
559{
560 u16 csr;
561
562 /* NOTE: we know the "rx" fifo reinit never triggers for ep0.
563 * That always uses tx_reinit since ep0 repurposes TX register
564 * offsets; the initial SETUP packet is also a kind of OUT.
565 */
566
567 /* if programmed for Tx, put it in RX mode */
568 if (ep->is_shared_fifo) {
569 csr = musb_readw(ep->regs, MUSB_TXCSR);
570 if (csr & MUSB_TXCSR_MODE) {
571 musb_h_tx_flush_fifo(ep);
572 csr = musb_readw(ep->regs, MUSB_TXCSR);
573 musb_writew(ep->regs, MUSB_TXCSR,
574 csr | MUSB_TXCSR_FRCDATATOG);
575 }
576
577 /*
578 * Clear the MODE bit (and everything else) to enable Rx.
579 * NOTE: we mustn't clear the DMAMODE bit before DMAENAB.
580 */
581 if (csr & MUSB_TXCSR_DMAMODE)
582 musb_writew(ep->regs, MUSB_TXCSR, MUSB_TXCSR_DMAMODE);
583 musb_writew(ep->regs, MUSB_TXCSR, 0);
584
585 /* scrub all previous state, clearing toggle */
586 } else {
587 csr = musb_readw(ep->regs, MUSB_RXCSR);
588 if (csr & MUSB_RXCSR_RXPKTRDY)
589 WARNING("rx%d, packet/%d ready?\n", ep->epnum,
590 musb_readw(ep->regs, MUSB_RXCOUNT));
591
592 musb_h_flush_rxfifo(ep, MUSB_RXCSR_CLRDATATOG);
593 }
594
595 /* target addr and (for multipoint) hub addr/port */
596 if (musb->is_multipoint) {
597 musb_write_rxfunaddr(ep->target_regs, qh->addr_reg);
598 musb_write_rxhubaddr(ep->target_regs, qh->h_addr_reg);
599 musb_write_rxhubport(ep->target_regs, qh->h_port_reg);
600
601 } else
602 musb_writeb(musb->mregs, MUSB_FADDR, qh->addr_reg);
603
604 /* protocol/endpoint, interval/NAKlimit, i/o size */
605 musb_writeb(ep->regs, MUSB_RXTYPE, qh->type_reg);
606 musb_writeb(ep->regs, MUSB_RXINTERVAL, qh->intv_reg);
607 /* NOTE: bulk combining rewrites high bits of maxpacket */
608 /* Set RXMAXP with the FIFO size of the endpoint
609 * to disable double buffer mode.
610 */
611 if (musb->double_buffer_not_ok)
612 musb_writew(ep->regs, MUSB_RXMAXP, ep->max_packet_sz_rx);
613 else
614 musb_writew(ep->regs, MUSB_RXMAXP,
615 qh->maxpacket | ((qh->hb_mult - 1) << 11));
616
617 ep->rx_reinit = 0;
618}
619
620static bool musb_tx_dma_program(struct dma_controller *dma,
621 struct musb_hw_ep *hw_ep, struct musb_qh *qh,
622 struct urb *urb, u32 offset, u32 length)
623{
624 struct dma_channel *channel = hw_ep->tx_channel;
625 void __iomem *epio = hw_ep->regs;
626 u16 pkt_size = qh->maxpacket;
627 u16 csr;
628 u8 mode;
629
630#ifdef CONFIG_USB_INVENTRA_DMA
631 if (length > channel->max_len)
632 length = channel->max_len;
633
634 csr = musb_readw(epio, MUSB_TXCSR);
635 if (length > pkt_size) {
636 mode = 1;
637 csr |= MUSB_TXCSR_DMAMODE | MUSB_TXCSR_DMAENAB;
638 /* autoset shouldn't be set in high bandwidth */
639 if (qh->hb_mult == 1)
640 csr |= MUSB_TXCSR_AUTOSET;
641 } else {
642 mode = 0;
643 csr &= ~(MUSB_TXCSR_AUTOSET | MUSB_TXCSR_DMAMODE);
644 csr |= MUSB_TXCSR_DMAENAB; /* against programmer's guide */
645 }
646 channel->desired_mode = mode;
647 musb_writew(epio, MUSB_TXCSR, csr);
648#else
649 if (!is_cppi_enabled() && !tusb_dma_omap())
650 return false;
651
652 channel->actual_len = 0;
653
654 /*
655 * TX uses "RNDIS" mode automatically but needs help
656 * to identify the zero-length-final-packet case.
657 */
658 mode = (urb->transfer_flags & URB_ZERO_PACKET) ? 1 : 0;
659#endif
660
661 qh->segsize = length;
662
663 /*
664 * Ensure the data reaches to main memory before starting
665 * DMA transfer
666 */
667 wmb();
668
669 if (!dma->channel_program(channel, pkt_size, mode,
670 urb->transfer_dma + offset, length)) {
671 dma->channel_release(channel);
672 hw_ep->tx_channel = NULL;
673
674 csr = musb_readw(epio, MUSB_TXCSR);
675 csr &= ~(MUSB_TXCSR_AUTOSET | MUSB_TXCSR_DMAENAB);
676 musb_writew(epio, MUSB_TXCSR, csr | MUSB_TXCSR_H_WZC_BITS);
677 return false;
678 }
679 return true;
680}
681
682/*
683 * Program an HDRC endpoint as per the given URB
684 * Context: irqs blocked, controller lock held
685 */
686static void musb_ep_program(struct musb *musb, u8 epnum,
687 struct urb *urb, int is_out,
688 u8 *buf, u32 offset, u32 len)
689{
690 struct dma_controller *dma_controller;
691 struct dma_channel *dma_channel;
692 u8 dma_ok;
693 void __iomem *mbase = musb->mregs;
694 struct musb_hw_ep *hw_ep = musb->endpoints + epnum;
695 void __iomem *epio = hw_ep->regs;
696 struct musb_qh *qh = musb_ep_get_qh(hw_ep, !is_out);
697 u16 packet_sz = qh->maxpacket;
698
699 dev_dbg(musb->controller, "%s hw%d urb %p spd%d dev%d ep%d%s "
700 "h_addr%02x h_port%02x bytes %d\n",
701 is_out ? "-->" : "<--",
702 epnum, urb, urb->dev->speed,
703 qh->addr_reg, qh->epnum, is_out ? "out" : "in",
704 qh->h_addr_reg, qh->h_port_reg,
705 len);
706
707 musb_ep_select(mbase, epnum);
708
709 /* candidate for DMA? */
710 dma_controller = musb->dma_controller;
711 if (is_dma_capable() && epnum && dma_controller) {
712 dma_channel = is_out ? hw_ep->tx_channel : hw_ep->rx_channel;
713 if (!dma_channel) {
714 dma_channel = dma_controller->channel_alloc(
715 dma_controller, hw_ep, is_out);
716 if (is_out)
717 hw_ep->tx_channel = dma_channel;
718 else
719 hw_ep->rx_channel = dma_channel;
720 }
721 } else
722 dma_channel = NULL;
723
724 /* make sure we clear DMAEnab, autoSet bits from previous run */
725
726 /* OUT/transmit/EP0 or IN/receive? */
727 if (is_out) {
728 u16 csr;
729 u16 int_txe;
730 u16 load_count;
731
732 csr = musb_readw(epio, MUSB_TXCSR);
733
734 /* disable interrupt in case we flush */
735 int_txe = musb_readw(mbase, MUSB_INTRTXE);
736 musb_writew(mbase, MUSB_INTRTXE, int_txe & ~(1 << epnum));
737
738 /* general endpoint setup */
739 if (epnum) {
740 /* flush all old state, set default */
741 musb_h_tx_flush_fifo(hw_ep);
742
743 /*
744 * We must not clear the DMAMODE bit before or in
745 * the same cycle with the DMAENAB bit, so we clear
746 * the latter first...
747 */
748 csr &= ~(MUSB_TXCSR_H_NAKTIMEOUT
749 | MUSB_TXCSR_AUTOSET
750 | MUSB_TXCSR_DMAENAB
751 | MUSB_TXCSR_FRCDATATOG
752 | MUSB_TXCSR_H_RXSTALL
753 | MUSB_TXCSR_H_ERROR
754 | MUSB_TXCSR_TXPKTRDY
755 );
756 csr |= MUSB_TXCSR_MODE;
757
758 if (usb_gettoggle(urb->dev, qh->epnum, 1))
759 csr |= MUSB_TXCSR_H_WR_DATATOGGLE
760 | MUSB_TXCSR_H_DATATOGGLE;
761 else
762 csr |= MUSB_TXCSR_CLRDATATOG;
763
764 musb_writew(epio, MUSB_TXCSR, csr);
765 /* REVISIT may need to clear FLUSHFIFO ... */
766 csr &= ~MUSB_TXCSR_DMAMODE;
767 musb_writew(epio, MUSB_TXCSR, csr);
768 csr = musb_readw(epio, MUSB_TXCSR);
769 } else {
770 /* endpoint 0: just flush */
771 musb_h_ep0_flush_fifo(hw_ep);
772 }
773
774 /* target addr and (for multipoint) hub addr/port */
775 if (musb->is_multipoint) {
776 musb_write_txfunaddr(mbase, epnum, qh->addr_reg);
777 musb_write_txhubaddr(mbase, epnum, qh->h_addr_reg);
778 musb_write_txhubport(mbase, epnum, qh->h_port_reg);
779/* FIXME if !epnum, do the same for RX ... */
780 } else
781 musb_writeb(mbase, MUSB_FADDR, qh->addr_reg);
782
783 /* protocol/endpoint/interval/NAKlimit */
784 if (epnum) {
785 musb_writeb(epio, MUSB_TXTYPE, qh->type_reg);
786 if (musb->double_buffer_not_ok)
787 musb_writew(epio, MUSB_TXMAXP,
788 hw_ep->max_packet_sz_tx);
789 else if (can_bulk_split(musb, qh->type))
790 musb_writew(epio, MUSB_TXMAXP, packet_sz
791 | ((hw_ep->max_packet_sz_tx /
792 packet_sz) - 1) << 11);
793 else
794 musb_writew(epio, MUSB_TXMAXP,
795 qh->maxpacket |
796 ((qh->hb_mult - 1) << 11));
797 musb_writeb(epio, MUSB_TXINTERVAL, qh->intv_reg);
798 } else {
799 musb_writeb(epio, MUSB_NAKLIMIT0, qh->intv_reg);
800 if (musb->is_multipoint)
801 musb_writeb(epio, MUSB_TYPE0,
802 qh->type_reg);
803 }
804
805 if (can_bulk_split(musb, qh->type))
806 load_count = min((u32) hw_ep->max_packet_sz_tx,
807 len);
808 else
809 load_count = min((u32) packet_sz, len);
810
811 if (dma_channel && musb_tx_dma_program(dma_controller,
812 hw_ep, qh, urb, offset, len))
813 load_count = 0;
814
815 if (load_count) {
816 /* PIO to load FIFO */
817 qh->segsize = load_count;
818 musb_write_fifo(hw_ep, load_count, buf);
819 }
820
821 /* re-enable interrupt */
822 musb_writew(mbase, MUSB_INTRTXE, int_txe);
823
824 /* IN/receive */
825 } else {
826 u16 csr;
827
828 if (hw_ep->rx_reinit) {
829 musb_rx_reinit(musb, qh, hw_ep);
830
831 /* init new state: toggle and NYET, maybe DMA later */
832 if (usb_gettoggle(urb->dev, qh->epnum, 0))
833 csr = MUSB_RXCSR_H_WR_DATATOGGLE
834 | MUSB_RXCSR_H_DATATOGGLE;
835 else
836 csr = 0;
837 if (qh->type == USB_ENDPOINT_XFER_INT)
838 csr |= MUSB_RXCSR_DISNYET;
839
840 } else {
841 csr = musb_readw(hw_ep->regs, MUSB_RXCSR);
842
843 if (csr & (MUSB_RXCSR_RXPKTRDY
844 | MUSB_RXCSR_DMAENAB
845 | MUSB_RXCSR_H_REQPKT))
846 ERR("broken !rx_reinit, ep%d csr %04x\n",
847 hw_ep->epnum, csr);
848
849 /* scrub any stale state, leaving toggle alone */
850 csr &= MUSB_RXCSR_DISNYET;
851 }
852
853 /* kick things off */
854
855 if ((is_cppi_enabled() || tusb_dma_omap()) && dma_channel) {
856 /* Candidate for DMA */
857 dma_channel->actual_len = 0L;
858 qh->segsize = len;
859
860 /* AUTOREQ is in a DMA register */
861 musb_writew(hw_ep->regs, MUSB_RXCSR, csr);
862 csr = musb_readw(hw_ep->regs, MUSB_RXCSR);
863
864 /*
865 * Unless caller treats short RX transfers as
866 * errors, we dare not queue multiple transfers.
867 */
868 dma_ok = dma_controller->channel_program(dma_channel,
869 packet_sz, !(urb->transfer_flags &
870 URB_SHORT_NOT_OK),
871 urb->transfer_dma + offset,
872 qh->segsize);
873 if (!dma_ok) {
874 dma_controller->channel_release(dma_channel);
875 hw_ep->rx_channel = dma_channel = NULL;
876 } else
877 csr |= MUSB_RXCSR_DMAENAB;
878 }
879
880 csr |= MUSB_RXCSR_H_REQPKT;
881 dev_dbg(musb->controller, "RXCSR%d := %04x\n", epnum, csr);
882 musb_writew(hw_ep->regs, MUSB_RXCSR, csr);
883 csr = musb_readw(hw_ep->regs, MUSB_RXCSR);
884 }
885}
886
887
888/*
889 * Service the default endpoint (ep0) as host.
890 * Return true until it's time to start the status stage.
891 */
892static bool musb_h_ep0_continue(struct musb *musb, u16 len, struct urb *urb)
893{
894 bool more = false;
895 u8 *fifo_dest = NULL;
896 u16 fifo_count = 0;
897 struct musb_hw_ep *hw_ep = musb->control_ep;
898 struct musb_qh *qh = hw_ep->in_qh;
899 struct usb_ctrlrequest *request;
900
901 switch (musb->ep0_stage) {
902 case MUSB_EP0_IN:
903 fifo_dest = urb->transfer_buffer + urb->actual_length;
904 fifo_count = min_t(size_t, len, urb->transfer_buffer_length -
905 urb->actual_length);
906 if (fifo_count < len)
907 urb->status = -EOVERFLOW;
908
909 musb_read_fifo(hw_ep, fifo_count, fifo_dest);
910
911 urb->actual_length += fifo_count;
912 if (len < qh->maxpacket) {
913 /* always terminate on short read; it's
914 * rarely reported as an error.
915 */
916 } else if (urb->actual_length <
917 urb->transfer_buffer_length)
918 more = true;
919 break;
920 case MUSB_EP0_START:
921 request = (struct usb_ctrlrequest *) urb->setup_packet;
922
923 if (!request->wLength) {
924 dev_dbg(musb->controller, "start no-DATA\n");
925 break;
926 } else if (request->bRequestType & USB_DIR_IN) {
927 dev_dbg(musb->controller, "start IN-DATA\n");
928 musb->ep0_stage = MUSB_EP0_IN;
929 more = true;
930 break;
931 } else {
932 dev_dbg(musb->controller, "start OUT-DATA\n");
933 musb->ep0_stage = MUSB_EP0_OUT;
934 more = true;
935 }
936 /* FALLTHROUGH */
937 case MUSB_EP0_OUT:
938 fifo_count = min_t(size_t, qh->maxpacket,
939 urb->transfer_buffer_length -
940 urb->actual_length);
941 if (fifo_count) {
942 fifo_dest = (u8 *) (urb->transfer_buffer
943 + urb->actual_length);
944 dev_dbg(musb->controller, "Sending %d byte%s to ep0 fifo %p\n",
945 fifo_count,
946 (fifo_count == 1) ? "" : "s",
947 fifo_dest);
948 musb_write_fifo(hw_ep, fifo_count, fifo_dest);
949
950 urb->actual_length += fifo_count;
951 more = true;
952 }
953 break;
954 default:
955 ERR("bogus ep0 stage %d\n", musb->ep0_stage);
956 break;
957 }
958
959 return more;
960}
961
962/*
963 * Handle default endpoint interrupt as host. Only called in IRQ time
964 * from musb_interrupt().
965 *
966 * called with controller irqlocked
967 */
968irqreturn_t musb_h_ep0_irq(struct musb *musb)
969{
970 struct urb *urb;
971 u16 csr, len;
972 int status = 0;
973 void __iomem *mbase = musb->mregs;
974 struct musb_hw_ep *hw_ep = musb->control_ep;
975 void __iomem *epio = hw_ep->regs;
976 struct musb_qh *qh = hw_ep->in_qh;
977 bool complete = false;
978 irqreturn_t retval = IRQ_NONE;
979
980 /* ep0 only has one queue, "in" */
981 urb = next_urb(qh);
982
983 musb_ep_select(mbase, 0);
984 csr = musb_readw(epio, MUSB_CSR0);
985 len = (csr & MUSB_CSR0_RXPKTRDY)
986 ? musb_readb(epio, MUSB_COUNT0)
987 : 0;
988
989 dev_dbg(musb->controller, "<== csr0 %04x, qh %p, count %d, urb %p, stage %d\n",
990 csr, qh, len, urb, musb->ep0_stage);
991
992 /* if we just did status stage, we are done */
993 if (MUSB_EP0_STATUS == musb->ep0_stage) {
994 retval = IRQ_HANDLED;
995 complete = true;
996 }
997
998 /* prepare status */
999 if (csr & MUSB_CSR0_H_RXSTALL) {
1000 dev_dbg(musb->controller, "STALLING ENDPOINT\n");
1001 status = -EPIPE;
1002
1003 } else if (csr & MUSB_CSR0_H_ERROR) {
1004 dev_dbg(musb->controller, "no response, csr0 %04x\n", csr);
1005 status = -EPROTO;
1006
1007 } else if (csr & MUSB_CSR0_H_NAKTIMEOUT) {
1008 dev_dbg(musb->controller, "control NAK timeout\n");
1009
1010 /* NOTE: this code path would be a good place to PAUSE a
1011 * control transfer, if another one is queued, so that
1012 * ep0 is more likely to stay busy. That's already done
1013 * for bulk RX transfers.
1014 *
1015 * if (qh->ring.next != &musb->control), then
1016 * we have a candidate... NAKing is *NOT* an error
1017 */
1018 musb_writew(epio, MUSB_CSR0, 0);
1019 retval = IRQ_HANDLED;
1020 }
1021
1022 if (status) {
1023 dev_dbg(musb->controller, "aborting\n");
1024 retval = IRQ_HANDLED;
1025 if (urb)
1026 urb->status = status;
1027 complete = true;
1028
1029 /* use the proper sequence to abort the transfer */
1030 if (csr & MUSB_CSR0_H_REQPKT) {
1031 csr &= ~MUSB_CSR0_H_REQPKT;
1032 musb_writew(epio, MUSB_CSR0, csr);
1033 csr &= ~MUSB_CSR0_H_NAKTIMEOUT;
1034 musb_writew(epio, MUSB_CSR0, csr);
1035 } else {
1036 musb_h_ep0_flush_fifo(hw_ep);
1037 }
1038
1039 musb_writeb(epio, MUSB_NAKLIMIT0, 0);
1040
1041 /* clear it */
1042 musb_writew(epio, MUSB_CSR0, 0);
1043 }
1044
1045 if (unlikely(!urb)) {
1046 /* stop endpoint since we have no place for its data, this
1047 * SHOULD NEVER HAPPEN! */
1048 ERR("no URB for end 0\n");
1049
1050 musb_h_ep0_flush_fifo(hw_ep);
1051 goto done;
1052 }
1053
1054 if (!complete) {
1055 /* call common logic and prepare response */
1056 if (musb_h_ep0_continue(musb, len, urb)) {
1057 /* more packets required */
1058 csr = (MUSB_EP0_IN == musb->ep0_stage)
1059 ? MUSB_CSR0_H_REQPKT : MUSB_CSR0_TXPKTRDY;
1060 } else {
1061 /* data transfer complete; perform status phase */
1062 if (usb_pipeout(urb->pipe)
1063 || !urb->transfer_buffer_length)
1064 csr = MUSB_CSR0_H_STATUSPKT
1065 | MUSB_CSR0_H_REQPKT;
1066 else
1067 csr = MUSB_CSR0_H_STATUSPKT
1068 | MUSB_CSR0_TXPKTRDY;
1069
1070 /* flag status stage */
1071 musb->ep0_stage = MUSB_EP0_STATUS;
1072
1073 dev_dbg(musb->controller, "ep0 STATUS, csr %04x\n", csr);
1074
1075 }
1076 musb_writew(epio, MUSB_CSR0, csr);
1077 retval = IRQ_HANDLED;
1078 } else
1079 musb->ep0_stage = MUSB_EP0_IDLE;
1080
1081 /* call completion handler if done */
1082 if (complete)
1083 musb_advance_schedule(musb, urb, hw_ep, 1);
1084done:
1085 return retval;
1086}
1087
1088
1089#ifdef CONFIG_USB_INVENTRA_DMA
1090
1091/* Host side TX (OUT) using Mentor DMA works as follows:
1092 submit_urb ->
1093 - if queue was empty, Program Endpoint
1094 - ... which starts DMA to fifo in mode 1 or 0
1095
1096 DMA Isr (transfer complete) -> TxAvail()
1097 - Stop DMA (~DmaEnab) (<--- Alert ... currently happens
1098 only in musb_cleanup_urb)
1099 - TxPktRdy has to be set in mode 0 or for
1100 short packets in mode 1.
1101*/
1102
1103#endif
1104
1105/* Service a Tx-Available or dma completion irq for the endpoint */
1106void musb_host_tx(struct musb *musb, u8 epnum)
1107{
1108 int pipe;
1109 bool done = false;
1110 u16 tx_csr;
1111 size_t length = 0;
1112 size_t offset = 0;
1113 struct musb_hw_ep *hw_ep = musb->endpoints + epnum;
1114 void __iomem *epio = hw_ep->regs;
1115 struct musb_qh *qh = hw_ep->out_qh;
1116 struct urb *urb = next_urb(qh);
1117 u32 status = 0;
1118 void __iomem *mbase = musb->mregs;
1119 struct dma_channel *dma;
1120 bool transfer_pending = false;
1121
1122 musb_ep_select(mbase, epnum);
1123 tx_csr = musb_readw(epio, MUSB_TXCSR);
1124
1125 /* with CPPI, DMA sometimes triggers "extra" irqs */
1126 if (!urb) {
1127 dev_dbg(musb->controller, "extra TX%d ready, csr %04x\n", epnum, tx_csr);
1128 return;
1129 }
1130
1131 pipe = urb->pipe;
1132 dma = is_dma_capable() ? hw_ep->tx_channel : NULL;
1133 dev_dbg(musb->controller, "OUT/TX%d end, csr %04x%s\n", epnum, tx_csr,
1134 dma ? ", dma" : "");
1135
1136 /* check for errors */
1137 if (tx_csr & MUSB_TXCSR_H_RXSTALL) {
1138 /* dma was disabled, fifo flushed */
1139 dev_dbg(musb->controller, "TX end %d stall\n", epnum);
1140
1141 /* stall; record URB status */
1142 status = -EPIPE;
1143
1144 } else if (tx_csr & MUSB_TXCSR_H_ERROR) {
1145 /* (NON-ISO) dma was disabled, fifo flushed */
1146 dev_dbg(musb->controller, "TX 3strikes on ep=%d\n", epnum);
1147
1148 status = -ETIMEDOUT;
1149
1150 } else if (tx_csr & MUSB_TXCSR_H_NAKTIMEOUT) {
1151 dev_dbg(musb->controller, "TX end=%d device not responding\n", epnum);
1152
1153 /* NOTE: this code path would be a good place to PAUSE a
1154 * transfer, if there's some other (nonperiodic) tx urb
1155 * that could use this fifo. (dma complicates it...)
1156 * That's already done for bulk RX transfers.
1157 *
1158 * if (bulk && qh->ring.next != &musb->out_bulk), then
1159 * we have a candidate... NAKing is *NOT* an error
1160 */
1161 musb_ep_select(mbase, epnum);
1162 musb_writew(epio, MUSB_TXCSR,
1163 MUSB_TXCSR_H_WZC_BITS
1164 | MUSB_TXCSR_TXPKTRDY);
1165 return;
1166 }
1167
1168 if (status) {
1169 if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) {
1170 dma->status = MUSB_DMA_STATUS_CORE_ABORT;
1171 (void) musb->dma_controller->channel_abort(dma);
1172 }
1173
1174 /* do the proper sequence to abort the transfer in the
1175 * usb core; the dma engine should already be stopped.
1176 */
1177 musb_h_tx_flush_fifo(hw_ep);
1178 tx_csr &= ~(MUSB_TXCSR_AUTOSET
1179 | MUSB_TXCSR_DMAENAB
1180 | MUSB_TXCSR_H_ERROR
1181 | MUSB_TXCSR_H_RXSTALL
1182 | MUSB_TXCSR_H_NAKTIMEOUT
1183 );
1184
1185 musb_ep_select(mbase, epnum);
1186 musb_writew(epio, MUSB_TXCSR, tx_csr);
1187 /* REVISIT may need to clear FLUSHFIFO ... */
1188 musb_writew(epio, MUSB_TXCSR, tx_csr);
1189 musb_writeb(epio, MUSB_TXINTERVAL, 0);
1190
1191 done = true;
1192 }
1193
1194 /* second cppi case */
1195 if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) {
1196 dev_dbg(musb->controller, "extra TX%d ready, csr %04x\n", epnum, tx_csr);
1197 return;
1198 }
1199
1200 if (is_dma_capable() && dma && !status) {
1201 /*
1202 * DMA has completed. But if we're using DMA mode 1 (multi
1203 * packet DMA), we need a terminal TXPKTRDY interrupt before
1204 * we can consider this transfer completed, lest we trash
1205 * its last packet when writing the next URB's data. So we
1206 * switch back to mode 0 to get that interrupt; we'll come
1207 * back here once it happens.
1208 */
1209 if (tx_csr & MUSB_TXCSR_DMAMODE) {
1210 /*
1211 * We shouldn't clear DMAMODE with DMAENAB set; so
1212 * clear them in a safe order. That should be OK
1213 * once TXPKTRDY has been set (and I've never seen
1214 * it being 0 at this moment -- DMA interrupt latency
1215 * is significant) but if it hasn't been then we have
1216 * no choice but to stop being polite and ignore the
1217 * programmer's guide... :-)
1218 *
1219 * Note that we must write TXCSR with TXPKTRDY cleared
1220 * in order not to re-trigger the packet send (this bit
1221 * can't be cleared by CPU), and there's another caveat:
1222 * TXPKTRDY may be set shortly and then cleared in the
1223 * double-buffered FIFO mode, so we do an extra TXCSR
1224 * read for debouncing...
1225 */
1226 tx_csr &= musb_readw(epio, MUSB_TXCSR);
1227 if (tx_csr & MUSB_TXCSR_TXPKTRDY) {
1228 tx_csr &= ~(MUSB_TXCSR_DMAENAB |
1229 MUSB_TXCSR_TXPKTRDY);
1230 musb_writew(epio, MUSB_TXCSR,
1231 tx_csr | MUSB_TXCSR_H_WZC_BITS);
1232 }
1233 tx_csr &= ~(MUSB_TXCSR_DMAMODE |
1234 MUSB_TXCSR_TXPKTRDY);
1235 musb_writew(epio, MUSB_TXCSR,
1236 tx_csr | MUSB_TXCSR_H_WZC_BITS);
1237
1238 /*
1239 * There is no guarantee that we'll get an interrupt
1240 * after clearing DMAMODE as we might have done this
1241 * too late (after TXPKTRDY was cleared by controller).
1242 * Re-read TXCSR as we have spoiled its previous value.
1243 */
1244 tx_csr = musb_readw(epio, MUSB_TXCSR);
1245 }
1246
1247 /*
1248 * We may get here from a DMA completion or TXPKTRDY interrupt.
1249 * In any case, we must check the FIFO status here and bail out
1250 * only if the FIFO still has data -- that should prevent the
1251 * "missed" TXPKTRDY interrupts and deal with double-buffered
1252 * FIFO mode too...
1253 */
1254 if (tx_csr & (MUSB_TXCSR_FIFONOTEMPTY | MUSB_TXCSR_TXPKTRDY)) {
1255 dev_dbg(musb->controller, "DMA complete but packet still in FIFO, "
1256 "CSR %04x\n", tx_csr);
1257 return;
1258 }
1259 }
1260
1261 if (!status || dma || usb_pipeisoc(pipe)) {
1262 if (dma)
1263 length = dma->actual_len;
1264 else
1265 length = qh->segsize;
1266 qh->offset += length;
1267
1268 if (usb_pipeisoc(pipe)) {
1269#ifndef __UBOOT__
1270 struct usb_iso_packet_descriptor *d;
1271
1272 d = urb->iso_frame_desc + qh->iso_idx;
1273 d->actual_length = length;
1274 d->status = status;
1275 if (++qh->iso_idx >= urb->number_of_packets) {
1276 done = true;
1277 } else {
1278 d++;
1279 offset = d->offset;
1280 length = d->length;
1281 }
1282#endif
1283 } else if (dma && urb->transfer_buffer_length == qh->offset) {
1284 done = true;
1285 } else {
1286 /* see if we need to send more data, or ZLP */
1287 if (qh->segsize < qh->maxpacket)
1288 done = true;
1289 else if (qh->offset == urb->transfer_buffer_length
1290 && !(urb->transfer_flags
1291 & URB_ZERO_PACKET))
1292 done = true;
1293 if (!done) {
1294 offset = qh->offset;
1295 length = urb->transfer_buffer_length - offset;
1296 transfer_pending = true;
1297 }
1298 }
1299 }
1300
1301 /* urb->status != -EINPROGRESS means request has been faulted,
1302 * so we must abort this transfer after cleanup
1303 */
1304 if (urb->status != -EINPROGRESS) {
1305 done = true;
1306 if (status == 0)
1307 status = urb->status;
1308 }
1309
1310 if (done) {
1311 /* set status */
1312 urb->status = status;
1313 urb->actual_length = qh->offset;
1314 musb_advance_schedule(musb, urb, hw_ep, USB_DIR_OUT);
1315 return;
1316 } else if ((usb_pipeisoc(pipe) || transfer_pending) && dma) {
1317 if (musb_tx_dma_program(musb->dma_controller, hw_ep, qh, urb,
1318 offset, length)) {
1319 if (is_cppi_enabled() || tusb_dma_omap())
1320 musb_h_tx_dma_start(hw_ep);
1321 return;
1322 }
1323 } else if (tx_csr & MUSB_TXCSR_DMAENAB) {
1324 dev_dbg(musb->controller, "not complete, but DMA enabled?\n");
1325 return;
1326 }
1327
1328 /*
1329 * PIO: start next packet in this URB.
1330 *
1331 * REVISIT: some docs say that when hw_ep->tx_double_buffered,
1332 * (and presumably, FIFO is not half-full) we should write *two*
1333 * packets before updating TXCSR; other docs disagree...
1334 */
1335 if (length > qh->maxpacket)
1336 length = qh->maxpacket;
1337 /* Unmap the buffer so that CPU can use it */
1338 usb_hcd_unmap_urb_for_dma(musb_to_hcd(musb), urb);
1339 musb_write_fifo(hw_ep, length, urb->transfer_buffer + offset);
1340 qh->segsize = length;
1341
1342 musb_ep_select(mbase, epnum);
1343 musb_writew(epio, MUSB_TXCSR,
1344 MUSB_TXCSR_H_WZC_BITS | MUSB_TXCSR_TXPKTRDY);
1345}
1346
1347
1348#ifdef CONFIG_USB_INVENTRA_DMA
1349
1350/* Host side RX (IN) using Mentor DMA works as follows:
1351 submit_urb ->
1352 - if queue was empty, ProgramEndpoint
1353 - first IN token is sent out (by setting ReqPkt)
1354 LinuxIsr -> RxReady()
1355 /\ => first packet is received
1356 | - Set in mode 0 (DmaEnab, ~ReqPkt)
1357 | -> DMA Isr (transfer complete) -> RxReady()
1358 | - Ack receive (~RxPktRdy), turn off DMA (~DmaEnab)
1359 | - if urb not complete, send next IN token (ReqPkt)
1360 | | else complete urb.
1361 | |
1362 ---------------------------
1363 *
1364 * Nuances of mode 1:
1365 * For short packets, no ack (+RxPktRdy) is sent automatically
1366 * (even if AutoClear is ON)
1367 * For full packets, ack (~RxPktRdy) and next IN token (+ReqPkt) is sent
1368 * automatically => major problem, as collecting the next packet becomes
1369 * difficult. Hence mode 1 is not used.
1370 *
1371 * REVISIT
1372 * All we care about at this driver level is that
1373 * (a) all URBs terminate with REQPKT cleared and fifo(s) empty;
1374 * (b) termination conditions are: short RX, or buffer full;
1375 * (c) fault modes include
1376 * - iff URB_SHORT_NOT_OK, short RX status is -EREMOTEIO.
1377 * (and that endpoint's dma queue stops immediately)
1378 * - overflow (full, PLUS more bytes in the terminal packet)
1379 *
1380 * So for example, usb-storage sets URB_SHORT_NOT_OK, and would
1381 * thus be a great candidate for using mode 1 ... for all but the
1382 * last packet of one URB's transfer.
1383 */
1384
1385#endif
1386
1387/* Schedule next QH from musb->in_bulk and move the current qh to
1388 * the end; avoids starvation for other endpoints.
1389 */
1390static void musb_bulk_rx_nak_timeout(struct musb *musb, struct musb_hw_ep *ep)
1391{
1392 struct dma_channel *dma;
1393 struct urb *urb;
1394 void __iomem *mbase = musb->mregs;
1395 void __iomem *epio = ep->regs;
1396 struct musb_qh *cur_qh, *next_qh;
1397 u16 rx_csr;
1398
1399 musb_ep_select(mbase, ep->epnum);
1400 dma = is_dma_capable() ? ep->rx_channel : NULL;
1401
1402 /* clear nak timeout bit */
1403 rx_csr = musb_readw(epio, MUSB_RXCSR);
1404 rx_csr |= MUSB_RXCSR_H_WZC_BITS;
1405 rx_csr &= ~MUSB_RXCSR_DATAERROR;
1406 musb_writew(epio, MUSB_RXCSR, rx_csr);
1407
1408 cur_qh = first_qh(&musb->in_bulk);
1409 if (cur_qh) {
1410 urb = next_urb(cur_qh);
1411 if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) {
1412 dma->status = MUSB_DMA_STATUS_CORE_ABORT;
1413 musb->dma_controller->channel_abort(dma);
1414 urb->actual_length += dma->actual_len;
1415 dma->actual_len = 0L;
1416 }
1417 musb_save_toggle(cur_qh, 1, urb);
1418
1419 /* move cur_qh to end of queue */
1420 list_move_tail(&cur_qh->ring, &musb->in_bulk);
1421
1422 /* get the next qh from musb->in_bulk */
1423 next_qh = first_qh(&musb->in_bulk);
1424
1425 /* set rx_reinit and schedule the next qh */
1426 ep->rx_reinit = 1;
1427 musb_start_urb(musb, 1, next_qh);
1428 }
1429}
1430
1431/*
1432 * Service an RX interrupt for the given IN endpoint; docs cover bulk, iso,
1433 * and high-bandwidth IN transfer cases.
1434 */
1435void musb_host_rx(struct musb *musb, u8 epnum)
1436{
1437 struct urb *urb;
1438 struct musb_hw_ep *hw_ep = musb->endpoints + epnum;
1439 void __iomem *epio = hw_ep->regs;
1440 struct musb_qh *qh = hw_ep->in_qh;
1441 size_t xfer_len;
1442 void __iomem *mbase = musb->mregs;
1443 int pipe;
1444 u16 rx_csr, val;
1445 bool iso_err = false;
1446 bool done = false;
1447 u32 status;
1448 struct dma_channel *dma;
1449
1450 musb_ep_select(mbase, epnum);
1451
1452 urb = next_urb(qh);
1453 dma = is_dma_capable() ? hw_ep->rx_channel : NULL;
1454 status = 0;
1455 xfer_len = 0;
1456
1457 rx_csr = musb_readw(epio, MUSB_RXCSR);
1458 val = rx_csr;
1459
1460 if (unlikely(!urb)) {
1461 /* REVISIT -- THIS SHOULD NEVER HAPPEN ... but, at least
1462 * usbtest #11 (unlinks) triggers it regularly, sometimes
1463 * with fifo full. (Only with DMA??)
1464 */
1465 dev_dbg(musb->controller, "BOGUS RX%d ready, csr %04x, count %d\n", epnum, val,
1466 musb_readw(epio, MUSB_RXCOUNT));
1467 musb_h_flush_rxfifo(hw_ep, MUSB_RXCSR_CLRDATATOG);
1468 return;
1469 }
1470
1471 pipe = urb->pipe;
1472
1473 dev_dbg(musb->controller, "<== hw %d rxcsr %04x, urb actual %d (+dma %zu)\n",
1474 epnum, rx_csr, urb->actual_length,
1475 dma ? dma->actual_len : 0);
1476
1477 /* check for errors, concurrent stall & unlink is not really
1478 * handled yet! */
1479 if (rx_csr & MUSB_RXCSR_H_RXSTALL) {
1480 dev_dbg(musb->controller, "RX end %d STALL\n", epnum);
1481
1482 /* stall; record URB status */
1483 status = -EPIPE;
1484
1485 } else if (rx_csr & MUSB_RXCSR_H_ERROR) {
1486 dev_dbg(musb->controller, "end %d RX proto error\n", epnum);
1487
1488 status = -EPROTO;
1489 musb_writeb(epio, MUSB_RXINTERVAL, 0);
1490
1491 } else if (rx_csr & MUSB_RXCSR_DATAERROR) {
1492
1493 if (USB_ENDPOINT_XFER_ISOC != qh->type) {
1494 dev_dbg(musb->controller, "RX end %d NAK timeout\n", epnum);
1495
1496 /* NOTE: NAKing is *NOT* an error, so we want to
1497 * continue. Except ... if there's a request for
1498 * another QH, use that instead of starving it.
1499 *
1500 * Devices like Ethernet and serial adapters keep
1501 * reads posted at all times, which will starve
1502 * other devices without this logic.
1503 */
1504 if (usb_pipebulk(urb->pipe)
1505 && qh->mux == 1
1506 && !list_is_singular(&musb->in_bulk)) {
1507 musb_bulk_rx_nak_timeout(musb, hw_ep);
1508 return;
1509 }
1510 musb_ep_select(mbase, epnum);
1511 rx_csr |= MUSB_RXCSR_H_WZC_BITS;
1512 rx_csr &= ~MUSB_RXCSR_DATAERROR;
1513 musb_writew(epio, MUSB_RXCSR, rx_csr);
1514
1515 goto finish;
1516 } else {
1517 dev_dbg(musb->controller, "RX end %d ISO data error\n", epnum);
1518 /* packet error reported later */
1519 iso_err = true;
1520 }
1521 } else if (rx_csr & MUSB_RXCSR_INCOMPRX) {
1522 dev_dbg(musb->controller, "end %d high bandwidth incomplete ISO packet RX\n",
1523 epnum);
1524 status = -EPROTO;
1525 }
1526
1527 /* faults abort the transfer */
1528 if (status) {
1529 /* clean up dma and collect transfer count */
1530 if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) {
1531 dma->status = MUSB_DMA_STATUS_CORE_ABORT;
1532 (void) musb->dma_controller->channel_abort(dma);
1533 xfer_len = dma->actual_len;
1534 }
1535 musb_h_flush_rxfifo(hw_ep, MUSB_RXCSR_CLRDATATOG);
1536 musb_writeb(epio, MUSB_RXINTERVAL, 0);
1537 done = true;
1538 goto finish;
1539 }
1540
1541 if (unlikely(dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY)) {
1542 /* SHOULD NEVER HAPPEN ... but at least DaVinci has done it */
1543 ERR("RX%d dma busy, csr %04x\n", epnum, rx_csr);
1544 goto finish;
1545 }
1546
1547 /* thorough shutdown for now ... given more precise fault handling
1548 * and better queueing support, we might keep a DMA pipeline going
1549 * while processing this irq for earlier completions.
1550 */
1551
1552 /* FIXME this is _way_ too much in-line logic for Mentor DMA */
1553
1554#ifndef CONFIG_USB_INVENTRA_DMA
1555 if (rx_csr & MUSB_RXCSR_H_REQPKT) {
1556 /* REVISIT this happened for a while on some short reads...
1557 * the cleanup still needs investigation... looks bad...
1558 * and also duplicates dma cleanup code above ... plus,
1559 * shouldn't this be the "half full" double buffer case?
1560 */
1561 if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) {
1562 dma->status = MUSB_DMA_STATUS_CORE_ABORT;
1563 (void) musb->dma_controller->channel_abort(dma);
1564 xfer_len = dma->actual_len;
1565 done = true;
1566 }
1567
1568 dev_dbg(musb->controller, "RXCSR%d %04x, reqpkt, len %zu%s\n", epnum, rx_csr,
1569 xfer_len, dma ? ", dma" : "");
1570 rx_csr &= ~MUSB_RXCSR_H_REQPKT;
1571
1572 musb_ep_select(mbase, epnum);
1573 musb_writew(epio, MUSB_RXCSR,
1574 MUSB_RXCSR_H_WZC_BITS | rx_csr);
1575 }
1576#endif
1577 if (dma && (rx_csr & MUSB_RXCSR_DMAENAB)) {
1578 xfer_len = dma->actual_len;
1579
1580 val &= ~(MUSB_RXCSR_DMAENAB
1581 | MUSB_RXCSR_H_AUTOREQ
1582 | MUSB_RXCSR_AUTOCLEAR
1583 | MUSB_RXCSR_RXPKTRDY);
1584 musb_writew(hw_ep->regs, MUSB_RXCSR, val);
1585
1586#ifdef CONFIG_USB_INVENTRA_DMA
1587 if (usb_pipeisoc(pipe)) {
1588 struct usb_iso_packet_descriptor *d;
1589
1590 d = urb->iso_frame_desc + qh->iso_idx;
1591 d->actual_length = xfer_len;
1592
1593 /* even if there was an error, we did the dma
1594 * for iso_frame_desc->length
1595 */
1596 if (d->status != -EILSEQ && d->status != -EOVERFLOW)
1597 d->status = 0;
1598
1599 if (++qh->iso_idx >= urb->number_of_packets)
1600 done = true;
1601 else
1602 done = false;
1603
1604 } else {
1605 /* done if urb buffer is full or short packet is recd */
1606 done = (urb->actual_length + xfer_len >=
1607 urb->transfer_buffer_length
1608 || dma->actual_len < qh->maxpacket);
1609 }
1610
1611 /* send IN token for next packet, without AUTOREQ */
1612 if (!done) {
1613 val |= MUSB_RXCSR_H_REQPKT;
1614 musb_writew(epio, MUSB_RXCSR,
1615 MUSB_RXCSR_H_WZC_BITS | val);
1616 }
1617
1618 dev_dbg(musb->controller, "ep %d dma %s, rxcsr %04x, rxcount %d\n", epnum,
1619 done ? "off" : "reset",
1620 musb_readw(epio, MUSB_RXCSR),
1621 musb_readw(epio, MUSB_RXCOUNT));
1622#else
1623 done = true;
1624#endif
1625 } else if (urb->status == -EINPROGRESS) {
1626 /* if no errors, be sure a packet is ready for unloading */
1627 if (unlikely(!(rx_csr & MUSB_RXCSR_RXPKTRDY))) {
1628 status = -EPROTO;
1629 ERR("Rx interrupt with no errors or packet!\n");
1630
1631 /* FIXME this is another "SHOULD NEVER HAPPEN" */
1632
1633/* SCRUB (RX) */
1634 /* do the proper sequence to abort the transfer */
1635 musb_ep_select(mbase, epnum);
1636 val &= ~MUSB_RXCSR_H_REQPKT;
1637 musb_writew(epio, MUSB_RXCSR, val);
1638 goto finish;
1639 }
1640
1641 /* we are expecting IN packets */
1642#ifdef CONFIG_USB_INVENTRA_DMA
1643 if (dma) {
1644 struct dma_controller *c;
1645 u16 rx_count;
1646 int ret, length;
1647 dma_addr_t buf;
1648
1649 rx_count = musb_readw(epio, MUSB_RXCOUNT);
1650
1651 dev_dbg(musb->controller, "RX%d count %d, buffer 0x%x len %d/%d\n",
1652 epnum, rx_count,
1653 urb->transfer_dma
1654 + urb->actual_length,
1655 qh->offset,
1656 urb->transfer_buffer_length);
1657
1658 c = musb->dma_controller;
1659
1660 if (usb_pipeisoc(pipe)) {
1661 int d_status = 0;
1662 struct usb_iso_packet_descriptor *d;
1663
1664 d = urb->iso_frame_desc + qh->iso_idx;
1665
1666 if (iso_err) {
1667 d_status = -EILSEQ;
1668 urb->error_count++;
1669 }
1670 if (rx_count > d->length) {
1671 if (d_status == 0) {
1672 d_status = -EOVERFLOW;
1673 urb->error_count++;
1674 }
1675 dev_dbg(musb->controller, "** OVERFLOW %d into %d\n",\
1676 rx_count, d->length);
1677
1678 length = d->length;
1679 } else
1680 length = rx_count;
1681 d->status = d_status;
1682 buf = urb->transfer_dma + d->offset;
1683 } else {
1684 length = rx_count;
1685 buf = urb->transfer_dma +
1686 urb->actual_length;
1687 }
1688
1689 dma->desired_mode = 0;
1690#ifdef USE_MODE1
1691 /* because of the issue below, mode 1 will
1692 * only rarely behave with correct semantics.
1693 */
1694 if ((urb->transfer_flags &
1695 URB_SHORT_NOT_OK)
1696 && (urb->transfer_buffer_length -
1697 urb->actual_length)
1698 > qh->maxpacket)
1699 dma->desired_mode = 1;
1700 if (rx_count < hw_ep->max_packet_sz_rx) {
1701 length = rx_count;
1702 dma->desired_mode = 0;
1703 } else {
1704 length = urb->transfer_buffer_length;
1705 }
1706#endif
1707
1708/* Disadvantage of using mode 1:
1709 * It's basically usable only for mass storage class; essentially all
1710 * other protocols also terminate transfers on short packets.
1711 *
1712 * Details:
1713 * An extra IN token is sent at the end of the transfer (due to AUTOREQ)
1714 * If you try to use mode 1 for (transfer_buffer_length - 512), and try
1715 * to use the extra IN token to grab the last packet using mode 0, then
1716 * the problem is that you cannot be sure when the device will send the
1717 * last packet and RxPktRdy set. Sometimes the packet is recd too soon
1718 * such that it gets lost when RxCSR is re-set at the end of the mode 1
1719 * transfer, while sometimes it is recd just a little late so that if you
1720 * try to configure for mode 0 soon after the mode 1 transfer is
1721 * completed, you will find rxcount 0. Okay, so you might think why not
1722 * wait for an interrupt when the pkt is recd. Well, you won't get any!
1723 */
1724
1725 val = musb_readw(epio, MUSB_RXCSR);
1726 val &= ~MUSB_RXCSR_H_REQPKT;
1727
1728 if (dma->desired_mode == 0)
1729 val &= ~MUSB_RXCSR_H_AUTOREQ;
1730 else
1731 val |= MUSB_RXCSR_H_AUTOREQ;
1732 val |= MUSB_RXCSR_DMAENAB;
1733
1734 /* autoclear shouldn't be set in high bandwidth */
1735 if (qh->hb_mult == 1)
1736 val |= MUSB_RXCSR_AUTOCLEAR;
1737
1738 musb_writew(epio, MUSB_RXCSR,
1739 MUSB_RXCSR_H_WZC_BITS | val);
1740
1741 /* REVISIT if when actual_length != 0,
1742 * transfer_buffer_length needs to be
1743 * adjusted first...
1744 */
1745 ret = c->channel_program(
1746 dma, qh->maxpacket,
1747 dma->desired_mode, buf, length);
1748
1749 if (!ret) {
1750 c->channel_release(dma);
1751 hw_ep->rx_channel = NULL;
1752 dma = NULL;
1753 val = musb_readw(epio, MUSB_RXCSR);
1754 val &= ~(MUSB_RXCSR_DMAENAB
1755 | MUSB_RXCSR_H_AUTOREQ
1756 | MUSB_RXCSR_AUTOCLEAR);
1757 musb_writew(epio, MUSB_RXCSR, val);
1758 }
1759 }
1760#endif /* Mentor DMA */
1761
1762 if (!dma) {
1763 /* Unmap the buffer so that CPU can use it */
1764 usb_hcd_unmap_urb_for_dma(musb_to_hcd(musb), urb);
1765 done = musb_host_packet_rx(musb, urb,
1766 epnum, iso_err);
1767 dev_dbg(musb->controller, "read %spacket\n", done ? "last " : "");
1768 }
1769 }
1770
1771finish:
1772 urb->actual_length += xfer_len;
1773 qh->offset += xfer_len;
1774 if (done) {
1775 if (urb->status == -EINPROGRESS)
1776 urb->status = status;
1777 musb_advance_schedule(musb, urb, hw_ep, USB_DIR_IN);
1778 }
1779}
1780
1781/* schedule nodes correspond to peripheral endpoints, like an OHCI QH.
1782 * the software schedule associates multiple such nodes with a given
1783 * host side hardware endpoint + direction; scheduling may activate
1784 * that hardware endpoint.
1785 */
1786static int musb_schedule(
1787 struct musb *musb,
1788 struct musb_qh *qh,
1789 int is_in)
1790{
1791 int idle;
1792 int best_diff;
1793 int best_end, epnum;
1794 struct musb_hw_ep *hw_ep = NULL;
1795 struct list_head *head = NULL;
1796 u8 toggle;
1797 u8 txtype;
1798 struct urb *urb = next_urb(qh);
1799
1800 /* use fixed hardware for control and bulk */
1801 if (qh->type == USB_ENDPOINT_XFER_CONTROL) {
1802 head = &musb->control;
1803 hw_ep = musb->control_ep;
1804 goto success;
1805 }
1806
1807 /* else, periodic transfers get muxed to other endpoints */
1808
1809 /*
1810 * We know this qh hasn't been scheduled, so all we need to do
1811 * is choose which hardware endpoint to put it on ...
1812 *
1813 * REVISIT what we really want here is a regular schedule tree
1814 * like e.g. OHCI uses.
1815 */
1816 best_diff = 4096;
1817 best_end = -1;
1818
1819 for (epnum = 1, hw_ep = musb->endpoints + 1;
1820 epnum < musb->nr_endpoints;
1821 epnum++, hw_ep++) {
1822 int diff;
1823
1824 if (musb_ep_get_qh(hw_ep, is_in) != NULL)
1825 continue;
1826
1827 if (hw_ep == musb->bulk_ep)
1828 continue;
1829
1830 if (is_in)
1831 diff = hw_ep->max_packet_sz_rx;
1832 else
1833 diff = hw_ep->max_packet_sz_tx;
1834 diff -= (qh->maxpacket * qh->hb_mult);
1835
1836 if (diff >= 0 && best_diff > diff) {
1837
1838 /*
1839 * Mentor controller has a bug in that if we schedule
1840 * a BULK Tx transfer on an endpoint that had earlier
1841 * handled ISOC then the BULK transfer has to start on
1842 * a zero toggle. If the BULK transfer starts on a 1
1843 * toggle then this transfer will fail as the mentor
1844 * controller starts the Bulk transfer on a 0 toggle
1845 * irrespective of the programming of the toggle bits
1846 * in the TXCSR register. Check for this condition
1847 * while allocating the EP for a Tx Bulk transfer. If
1848 * so skip this EP.
1849 */
1850 hw_ep = musb->endpoints + epnum;
1851 toggle = usb_gettoggle(urb->dev, qh->epnum, !is_in);
1852 txtype = (musb_readb(hw_ep->regs, MUSB_TXTYPE)
1853 >> 4) & 0x3;
1854 if (!is_in && (qh->type == USB_ENDPOINT_XFER_BULK) &&
1855 toggle && (txtype == USB_ENDPOINT_XFER_ISOC))
1856 continue;
1857
1858 best_diff = diff;
1859 best_end = epnum;
1860 }
1861 }
1862 /* use bulk reserved ep1 if no other ep is free */
1863 if (best_end < 0 && qh->type == USB_ENDPOINT_XFER_BULK) {
1864 hw_ep = musb->bulk_ep;
1865 if (is_in)
1866 head = &musb->in_bulk;
1867 else
1868 head = &musb->out_bulk;
1869
1870 /* Enable bulk RX NAK timeout scheme when bulk requests are
1871 * multiplexed. This scheme doen't work in high speed to full
1872 * speed scenario as NAK interrupts are not coming from a
1873 * full speed device connected to a high speed device.
1874 * NAK timeout interval is 8 (128 uframe or 16ms) for HS and
1875 * 4 (8 frame or 8ms) for FS device.
1876 */
1877 if (is_in && qh->dev)
1878 qh->intv_reg =
1879 (USB_SPEED_HIGH == qh->dev->speed) ? 8 : 4;
1880 goto success;
1881 } else if (best_end < 0) {
1882 return -ENOSPC;
1883 }
1884
1885 idle = 1;
1886 qh->mux = 0;
1887 hw_ep = musb->endpoints + best_end;
1888 dev_dbg(musb->controller, "qh %p periodic slot %d\n", qh, best_end);
1889success:
1890 if (head) {
1891 idle = list_empty(head);
1892 list_add_tail(&qh->ring, head);
1893 qh->mux = 1;
1894 }
1895 qh->hw_ep = hw_ep;
1896 qh->hep->hcpriv = qh;
1897 if (idle)
1898 musb_start_urb(musb, is_in, qh);
1899 return 0;
1900}
1901
1902#ifdef __UBOOT__
1903/* check if transaction translator is needed for device */
1904static int tt_needed(struct musb *musb, struct usb_device *dev)
1905{
1906 if ((musb_readb(musb->mregs, MUSB_POWER) & MUSB_POWER_HSMODE) &&
1907 (dev->speed < USB_SPEED_HIGH))
1908 return 1;
1909 return 0;
1910}
1911#endif
1912
1913#ifndef __UBOOT__
1914static int musb_urb_enqueue(
1915#else
1916int musb_urb_enqueue(
1917#endif
1918 struct usb_hcd *hcd,
1919 struct urb *urb,
1920 gfp_t mem_flags)
1921{
1922 unsigned long flags;
1923 struct musb *musb = hcd_to_musb(hcd);
1924 struct usb_host_endpoint *hep = urb->ep;
1925 struct musb_qh *qh;
1926 struct usb_endpoint_descriptor *epd = &hep->desc;
1927 int ret;
1928 unsigned type_reg;
1929 unsigned interval;
1930
1931 /* host role must be active */
1932 if (!is_host_active(musb) || !musb->is_active)
1933 return -ENODEV;
1934
1935 spin_lock_irqsave(&musb->lock, flags);
1936 ret = usb_hcd_link_urb_to_ep(hcd, urb);
1937 qh = ret ? NULL : hep->hcpriv;
1938 if (qh)
1939 urb->hcpriv = qh;
1940 spin_unlock_irqrestore(&musb->lock, flags);
1941
1942 /* DMA mapping was already done, if needed, and this urb is on
1943 * hep->urb_list now ... so we're done, unless hep wasn't yet
1944 * scheduled onto a live qh.
1945 *
1946 * REVISIT best to keep hep->hcpriv valid until the endpoint gets
1947 * disabled, testing for empty qh->ring and avoiding qh setup costs
1948 * except for the first urb queued after a config change.
1949 */
1950 if (qh || ret)
1951 return ret;
1952
1953 /* Allocate and initialize qh, minimizing the work done each time
1954 * hw_ep gets reprogrammed, or with irqs blocked. Then schedule it.
1955 *
1956 * REVISIT consider a dedicated qh kmem_cache, so it's harder
1957 * for bugs in other kernel code to break this driver...
1958 */
1959 qh = kzalloc(sizeof *qh, mem_flags);
1960 if (!qh) {
1961 spin_lock_irqsave(&musb->lock, flags);
1962 usb_hcd_unlink_urb_from_ep(hcd, urb);
1963 spin_unlock_irqrestore(&musb->lock, flags);
1964 return -ENOMEM;
1965 }
1966
1967 qh->hep = hep;
1968 qh->dev = urb->dev;
1969 INIT_LIST_HEAD(&qh->ring);
1970 qh->is_ready = 1;
1971
1972 qh->maxpacket = usb_endpoint_maxp(epd);
1973 qh->type = usb_endpoint_type(epd);
1974
1975 /* Bits 11 & 12 of wMaxPacketSize encode high bandwidth multiplier.
1976 * Some musb cores don't support high bandwidth ISO transfers; and
1977 * we don't (yet!) support high bandwidth interrupt transfers.
1978 */
1979 qh->hb_mult = 1 + ((qh->maxpacket >> 11) & 0x03);
1980 if (qh->hb_mult > 1) {
1981 int ok = (qh->type == USB_ENDPOINT_XFER_ISOC);
1982
1983 if (ok)
1984 ok = (usb_pipein(urb->pipe) && musb->hb_iso_rx)
1985 || (usb_pipeout(urb->pipe) && musb->hb_iso_tx);
1986 if (!ok) {
1987 ret = -EMSGSIZE;
1988 goto done;
1989 }
1990 qh->maxpacket &= 0x7ff;
1991 }
1992
1993 qh->epnum = usb_endpoint_num(epd);
1994
1995 /* NOTE: urb->dev->devnum is wrong during SET_ADDRESS */
1996 qh->addr_reg = (u8) usb_pipedevice(urb->pipe);
1997
1998 /* precompute rxtype/txtype/type0 register */
1999 type_reg = (qh->type << 4) | qh->epnum;
2000 switch (urb->dev->speed) {
2001 case USB_SPEED_LOW:
2002 type_reg |= 0xc0;
2003 break;
2004 case USB_SPEED_FULL:
2005 type_reg |= 0x80;
2006 break;
2007 default:
2008 type_reg |= 0x40;
2009 }
2010 qh->type_reg = type_reg;
2011
2012 /* Precompute RXINTERVAL/TXINTERVAL register */
2013 switch (qh->type) {
2014 case USB_ENDPOINT_XFER_INT:
2015 /*
2016 * Full/low speeds use the linear encoding,
2017 * high speed uses the logarithmic encoding.
2018 */
2019 if (urb->dev->speed <= USB_SPEED_FULL) {
2020 interval = max_t(u8, epd->bInterval, 1);
2021 break;
2022 }
2023 /* FALLTHROUGH */
2024 case USB_ENDPOINT_XFER_ISOC:
2025 /* ISO always uses logarithmic encoding */
2026 interval = min_t(u8, epd->bInterval, 16);
2027 break;
2028 default:
2029 /* REVISIT we actually want to use NAK limits, hinting to the
2030 * transfer scheduling logic to try some other qh, e.g. try
2031 * for 2 msec first:
2032 *
2033 * interval = (USB_SPEED_HIGH == urb->dev->speed) ? 16 : 2;
2034 *
2035 * The downside of disabling this is that transfer scheduling
2036 * gets VERY unfair for nonperiodic transfers; a misbehaving
2037 * peripheral could make that hurt. That's perfectly normal
2038 * for reads from network or serial adapters ... so we have
2039 * partial NAKlimit support for bulk RX.
2040 *
2041 * The upside of disabling it is simpler transfer scheduling.
2042 */
2043 interval = 0;
2044 }
2045 qh->intv_reg = interval;
2046
2047 /* precompute addressing for external hub/tt ports */
2048 if (musb->is_multipoint) {
Hans de Goede4d8b8df2015-06-17 21:33:55 +02002049#ifndef __UBOOT__
Ilya Yanok06bb9202012-11-06 13:48:21 +00002050 struct usb_device *parent = urb->dev->parent;
Hans de Goede4d8b8df2015-06-17 21:33:55 +02002051#else
2052 struct usb_device *parent = usb_dev_get_parent(urb->dev);
2053#endif
Ilya Yanok06bb9202012-11-06 13:48:21 +00002054
2055#ifndef __UBOOT__
2056 if (parent != hcd->self.root_hub) {
2057#else
2058 if (parent) {
2059#endif
2060 qh->h_addr_reg = (u8) parent->devnum;
2061
2062#ifndef __UBOOT__
2063 /* set up tt info if needed */
2064 if (urb->dev->tt) {
2065 qh->h_port_reg = (u8) urb->dev->ttport;
2066 if (urb->dev->tt->hub)
2067 qh->h_addr_reg =
2068 (u8) urb->dev->tt->hub->devnum;
2069 if (urb->dev->tt->multi)
2070 qh->h_addr_reg |= 0x80;
2071 }
2072#else
2073 if (tt_needed(musb, urb->dev)) {
Stefan Brünsa0105682015-12-22 01:21:03 +01002074 uint8_t portnr = 0;
2075 uint8_t hubaddr = 0;
2076 usb_find_usb2_hub_address_port(urb->dev,
2077 &hubaddr,
2078 &portnr);
2079 qh->h_addr_reg = hubaddr;
Stefan Brüns8f53bf02015-12-22 01:21:04 +01002080 qh->h_port_reg = portnr;
Ilya Yanok06bb9202012-11-06 13:48:21 +00002081 }
2082#endif
2083 }
2084 }
2085
2086 /* invariant: hep->hcpriv is null OR the qh that's already scheduled.
2087 * until we get real dma queues (with an entry for each urb/buffer),
2088 * we only have work to do in the former case.
2089 */
2090 spin_lock_irqsave(&musb->lock, flags);
2091 if (hep->hcpriv) {
2092 /* some concurrent activity submitted another urb to hep...
2093 * odd, rare, error prone, but legal.
2094 */
2095 kfree(qh);
2096 qh = NULL;
2097 ret = 0;
2098 } else
2099 ret = musb_schedule(musb, qh,
2100 epd->bEndpointAddress & USB_ENDPOINT_DIR_MASK);
2101
2102 if (ret == 0) {
2103 urb->hcpriv = qh;
2104 /* FIXME set urb->start_frame for iso/intr, it's tested in
2105 * musb_start_urb(), but otherwise only konicawc cares ...
2106 */
2107 }
2108 spin_unlock_irqrestore(&musb->lock, flags);
2109
2110done:
2111 if (ret != 0) {
2112 spin_lock_irqsave(&musb->lock, flags);
2113 usb_hcd_unlink_urb_from_ep(hcd, urb);
2114 spin_unlock_irqrestore(&musb->lock, flags);
2115 kfree(qh);
2116 }
2117 return ret;
2118}
2119
Ilya Yanok06bb9202012-11-06 13:48:21 +00002120/*
2121 * abort a transfer that's at the head of a hardware queue.
2122 * called with controller locked, irqs blocked
2123 * that hardware queue advances to the next transfer, unless prevented
2124 */
2125static int musb_cleanup_urb(struct urb *urb, struct musb_qh *qh)
2126{
2127 struct musb_hw_ep *ep = qh->hw_ep;
2128 struct musb *musb = ep->musb;
2129 void __iomem *epio = ep->regs;
2130 unsigned hw_end = ep->epnum;
2131 void __iomem *regs = ep->musb->mregs;
2132 int is_in = usb_pipein(urb->pipe);
2133 int status = 0;
2134 u16 csr;
2135
2136 musb_ep_select(regs, hw_end);
2137
2138 if (is_dma_capable()) {
2139 struct dma_channel *dma;
2140
2141 dma = is_in ? ep->rx_channel : ep->tx_channel;
2142 if (dma) {
2143 status = ep->musb->dma_controller->channel_abort(dma);
2144 dev_dbg(musb->controller,
2145 "abort %cX%d DMA for urb %p --> %d\n",
2146 is_in ? 'R' : 'T', ep->epnum,
2147 urb, status);
2148 urb->actual_length += dma->actual_len;
2149 }
2150 }
2151
2152 /* turn off DMA requests, discard state, stop polling ... */
2153 if (ep->epnum && is_in) {
2154 /* giveback saves bulk toggle */
2155 csr = musb_h_flush_rxfifo(ep, 0);
2156
2157 /* REVISIT we still get an irq; should likely clear the
2158 * endpoint's irq status here to avoid bogus irqs.
2159 * clearing that status is platform-specific...
2160 */
2161 } else if (ep->epnum) {
2162 musb_h_tx_flush_fifo(ep);
2163 csr = musb_readw(epio, MUSB_TXCSR);
2164 csr &= ~(MUSB_TXCSR_AUTOSET
2165 | MUSB_TXCSR_DMAENAB
2166 | MUSB_TXCSR_H_RXSTALL
2167 | MUSB_TXCSR_H_NAKTIMEOUT
2168 | MUSB_TXCSR_H_ERROR
2169 | MUSB_TXCSR_TXPKTRDY);
2170 musb_writew(epio, MUSB_TXCSR, csr);
2171 /* REVISIT may need to clear FLUSHFIFO ... */
2172 musb_writew(epio, MUSB_TXCSR, csr);
2173 /* flush cpu writebuffer */
2174 csr = musb_readw(epio, MUSB_TXCSR);
2175 } else {
2176 musb_h_ep0_flush_fifo(ep);
2177 }
2178 if (status == 0)
2179 musb_advance_schedule(ep->musb, urb, ep, is_in);
2180 return status;
2181}
2182
Hans de Goede0e4da952015-01-11 20:34:52 +01002183#ifndef __UBOOT__
2184static int musb_urb_dequeue(
2185#else
2186int musb_urb_dequeue(
2187#endif
2188 struct usb_hcd *hcd,
2189 struct urb *urb,
2190 int status)
Ilya Yanok06bb9202012-11-06 13:48:21 +00002191{
2192 struct musb *musb = hcd_to_musb(hcd);
2193 struct musb_qh *qh;
2194 unsigned long flags;
2195 int is_in = usb_pipein(urb->pipe);
2196 int ret;
2197
2198 dev_dbg(musb->controller, "urb=%p, dev%d ep%d%s\n", urb,
2199 usb_pipedevice(urb->pipe),
2200 usb_pipeendpoint(urb->pipe),
2201 is_in ? "in" : "out");
2202
2203 spin_lock_irqsave(&musb->lock, flags);
2204 ret = usb_hcd_check_unlink_urb(hcd, urb, status);
2205 if (ret)
2206 goto done;
2207
2208 qh = urb->hcpriv;
2209 if (!qh)
2210 goto done;
2211
2212 /*
2213 * Any URB not actively programmed into endpoint hardware can be
2214 * immediately given back; that's any URB not at the head of an
2215 * endpoint queue, unless someday we get real DMA queues. And even
2216 * if it's at the head, it might not be known to the hardware...
2217 *
2218 * Otherwise abort current transfer, pending DMA, etc.; urb->status
2219 * has already been updated. This is a synchronous abort; it'd be
2220 * OK to hold off until after some IRQ, though.
2221 *
2222 * NOTE: qh is invalid unless !list_empty(&hep->urb_list)
2223 */
2224 if (!qh->is_ready
2225 || urb->urb_list.prev != &qh->hep->urb_list
2226 || musb_ep_get_qh(qh->hw_ep, is_in) != qh) {
2227 int ready = qh->is_ready;
2228
2229 qh->is_ready = 0;
2230 musb_giveback(musb, urb, 0);
2231 qh->is_ready = ready;
2232
2233 /* If nothing else (usually musb_giveback) is using it
2234 * and its URB list has emptied, recycle this qh.
2235 */
2236 if (ready && list_empty(&qh->hep->urb_list)) {
2237 qh->hep->hcpriv = NULL;
2238 list_del(&qh->ring);
2239 kfree(qh);
2240 }
2241 } else
2242 ret = musb_cleanup_urb(urb, qh);
2243done:
2244 spin_unlock_irqrestore(&musb->lock, flags);
2245 return ret;
2246}
2247
Hans de Goede0e4da952015-01-11 20:34:52 +01002248#ifndef __UBOOT__
Ilya Yanok06bb9202012-11-06 13:48:21 +00002249/* disable an endpoint */
2250static void
2251musb_h_disable(struct usb_hcd *hcd, struct usb_host_endpoint *hep)
2252{
2253 u8 is_in = hep->desc.bEndpointAddress & USB_DIR_IN;
2254 unsigned long flags;
2255 struct musb *musb = hcd_to_musb(hcd);
2256 struct musb_qh *qh;
2257 struct urb *urb;
2258
2259 spin_lock_irqsave(&musb->lock, flags);
2260
2261 qh = hep->hcpriv;
2262 if (qh == NULL)
2263 goto exit;
2264
2265 /* NOTE: qh is invalid unless !list_empty(&hep->urb_list) */
2266
2267 /* Kick the first URB off the hardware, if needed */
2268 qh->is_ready = 0;
2269 if (musb_ep_get_qh(qh->hw_ep, is_in) == qh) {
2270 urb = next_urb(qh);
2271
2272 /* make software (then hardware) stop ASAP */
2273 if (!urb->unlinked)
2274 urb->status = -ESHUTDOWN;
2275
2276 /* cleanup */
2277 musb_cleanup_urb(urb, qh);
2278
2279 /* Then nuke all the others ... and advance the
2280 * queue on hw_ep (e.g. bulk ring) when we're done.
2281 */
2282 while (!list_empty(&hep->urb_list)) {
2283 urb = next_urb(qh);
2284 urb->status = -ESHUTDOWN;
2285 musb_advance_schedule(musb, urb, qh->hw_ep, is_in);
2286 }
2287 } else {
2288 /* Just empty the queue; the hardware is busy with
2289 * other transfers, and since !qh->is_ready nothing
2290 * will activate any of these as it advances.
2291 */
2292 while (!list_empty(&hep->urb_list))
2293 musb_giveback(musb, next_urb(qh), -ESHUTDOWN);
2294
2295 hep->hcpriv = NULL;
2296 list_del(&qh->ring);
2297 kfree(qh);
2298 }
2299exit:
2300 spin_unlock_irqrestore(&musb->lock, flags);
2301}
2302
2303static int musb_h_get_frame_number(struct usb_hcd *hcd)
2304{
2305 struct musb *musb = hcd_to_musb(hcd);
2306
2307 return musb_readw(musb->mregs, MUSB_FRAME);
2308}
2309
2310static int musb_h_start(struct usb_hcd *hcd)
2311{
2312 struct musb *musb = hcd_to_musb(hcd);
2313
2314 /* NOTE: musb_start() is called when the hub driver turns
2315 * on port power, or when (OTG) peripheral starts.
2316 */
2317 hcd->state = HC_STATE_RUNNING;
2318 musb->port1_status = 0;
2319 return 0;
2320}
2321
2322static void musb_h_stop(struct usb_hcd *hcd)
2323{
2324 musb_stop(hcd_to_musb(hcd));
2325 hcd->state = HC_STATE_HALT;
2326}
2327
2328static int musb_bus_suspend(struct usb_hcd *hcd)
2329{
2330 struct musb *musb = hcd_to_musb(hcd);
2331 u8 devctl;
2332
2333 if (!is_host_active(musb))
2334 return 0;
2335
2336 switch (musb->xceiv->state) {
2337 case OTG_STATE_A_SUSPEND:
2338 return 0;
2339 case OTG_STATE_A_WAIT_VRISE:
2340 /* ID could be grounded even if there's no device
2341 * on the other end of the cable. NOTE that the
2342 * A_WAIT_VRISE timers are messy with MUSB...
2343 */
2344 devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
2345 if ((devctl & MUSB_DEVCTL_VBUS) == MUSB_DEVCTL_VBUS)
2346 musb->xceiv->state = OTG_STATE_A_WAIT_BCON;
2347 break;
2348 default:
2349 break;
2350 }
2351
2352 if (musb->is_active) {
2353 WARNING("trying to suspend as %s while active\n",
2354 otg_state_string(musb->xceiv->state));
2355 return -EBUSY;
2356 } else
2357 return 0;
2358}
2359
2360static int musb_bus_resume(struct usb_hcd *hcd)
2361{
2362 /* resuming child port does the work */
2363 return 0;
2364}
2365
2366const struct hc_driver musb_hc_driver = {
2367 .description = "musb-hcd",
2368 .product_desc = "MUSB HDRC host driver",
2369 .hcd_priv_size = sizeof(struct musb),
2370 .flags = HCD_USB2 | HCD_MEMORY,
2371
2372 /* not using irq handler or reset hooks from usbcore, since
2373 * those must be shared with peripheral code for OTG configs
2374 */
2375
2376 .start = musb_h_start,
2377 .stop = musb_h_stop,
2378
2379 .get_frame_number = musb_h_get_frame_number,
2380
2381 .urb_enqueue = musb_urb_enqueue,
2382 .urb_dequeue = musb_urb_dequeue,
2383 .endpoint_disable = musb_h_disable,
2384
2385 .hub_status_data = musb_hub_status_data,
2386 .hub_control = musb_hub_control,
2387 .bus_suspend = musb_bus_suspend,
2388 .bus_resume = musb_bus_resume,
2389 /* .start_port_reset = NULL, */
2390 /* .hub_irq_enable = NULL, */
2391};
2392#endif