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