blob: 8b977d23ddfb8f0ed522a5d33dd04af8810401e2 [file] [log] [blame]
Felipe Balbi550a7372008-07-24 12:27:36 +03001/*
2 * MUSB OTG driver host support
3 *
4 * Copyright 2005 Mentor Graphics Corporation
5 * Copyright (C) 2005-2006 by Texas Instruments
6 * Copyright (C) 2006-2007 Nokia Corporation
Sergei Shtylyovc7bbc052009-03-26 18:26:40 -07007 * Copyright (C) 2008-2009 MontaVista Software, Inc. <source@mvista.com>
Felipe Balbi550a7372008-07-24 12:27:36 +03008 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * version 2 as published by the Free Software Foundation.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
21 * 02110-1301 USA
22 *
23 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
24 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
25 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
26 * NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
29 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
30 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 *
34 */
35
36#include <linux/module.h>
37#include <linux/kernel.h>
38#include <linux/delay.h>
39#include <linux/sched.h>
40#include <linux/slab.h>
41#include <linux/errno.h>
42#include <linux/init.h>
43#include <linux/list.h>
Maulik Mankad496dda72010-09-24 13:44:06 +030044#include <linux/dma-mapping.h>
Felipe Balbi550a7372008-07-24 12:27:36 +030045
46#include "musb_core.h"
47#include "musb_host.h"
48
Felipe Balbi550a7372008-07-24 12:27:36 +030049/* MUSB HOST status 22-mar-2006
50 *
51 * - There's still lots of partial code duplication for fault paths, so
52 * they aren't handled as consistently as they need to be.
53 *
54 * - PIO mostly behaved when last tested.
55 * + including ep0, with all usbtest cases 9, 10
56 * + usbtest 14 (ep0out) doesn't seem to run at all
57 * + double buffered OUT/TX endpoints saw stalls(!) with certain usbtest
58 * configurations, but otherwise double buffering passes basic tests.
59 * + for 2.6.N, for N > ~10, needs API changes for hcd framework.
60 *
61 * - DMA (CPPI) ... partially behaves, not currently recommended
62 * + about 1/15 the speed of typical EHCI implementations (PCI)
63 * + RX, all too often reqpkt seems to misbehave after tx
64 * + TX, no known issues (other than evident silicon issue)
65 *
66 * - DMA (Mentor/OMAP) ...has at least toggle update problems
67 *
Ajay Kumar Gupta1e0320f2009-02-24 15:26:13 -080068 * - [23-feb-2009] minimal traffic scheduling to avoid bulk RX packet
69 * starvation ... nothing yet for TX, interrupt, or bulk.
Felipe Balbi550a7372008-07-24 12:27:36 +030070 *
71 * - Not tested with HNP, but some SRP paths seem to behave.
72 *
73 * NOTE 24-August-2006:
74 *
75 * - Bulk traffic finally uses both sides of hardware ep1, freeing up an
76 * extra endpoint for periodic use enabling hub + keybd + mouse. That
77 * mostly works, except that with "usbnet" it's easy to trigger cases
78 * with "ping" where RX loses. (a) ping to davinci, even "ping -f",
79 * fine; but (b) ping _from_ davinci, even "ping -c 1", ICMP RX loses
80 * although ARP RX wins. (That test was done with a full speed link.)
81 */
82
83
84/*
85 * NOTE on endpoint usage:
86 *
87 * CONTROL transfers all go through ep0. BULK ones go through dedicated IN
88 * and OUT endpoints ... hardware is dedicated for those "async" queue(s).
Felipe Balbi550a7372008-07-24 12:27:36 +030089 * (Yes, bulk _could_ use more of the endpoints than that, and would even
Ajay Kumar Gupta1e0320f2009-02-24 15:26:13 -080090 * benefit from it.)
Felipe Balbi550a7372008-07-24 12:27:36 +030091 *
92 * INTERUPPT and ISOCHRONOUS transfers are scheduled to the other endpoints.
93 * So far that scheduling is both dumb and optimistic: the endpoint will be
94 * "claimed" until its software queue is no longer refilled. No multiplexing
95 * of transfers between endpoints, or anything clever.
96 */
97
98
99static void musb_ep_program(struct musb *musb, u8 epnum,
Sergei Shtylyov6b6e9712009-03-26 18:29:19 -0700100 struct urb *urb, int is_out,
101 u8 *buf, u32 offset, u32 len);
Felipe Balbi550a7372008-07-24 12:27:36 +0300102
103/*
104 * Clear TX fifo. Needed to avoid BABBLE errors.
105 */
David Brownellc767c1c2008-09-11 11:53:23 +0300106static void musb_h_tx_flush_fifo(struct musb_hw_ep *ep)
Felipe Balbi550a7372008-07-24 12:27:36 +0300107{
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300108 struct musb *musb = ep->musb;
Felipe Balbi550a7372008-07-24 12:27:36 +0300109 void __iomem *epio = ep->regs;
110 u16 csr;
David Brownellbb1c9ef2008-11-24 13:06:50 +0200111 u16 lastcsr = 0;
Felipe Balbi550a7372008-07-24 12:27:36 +0300112 int retries = 1000;
113
114 csr = musb_readw(epio, MUSB_TXCSR);
115 while (csr & MUSB_TXCSR_FIFONOTEMPTY) {
David Brownellbb1c9ef2008-11-24 13:06:50 +0200116 if (csr != lastcsr)
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300117 dev_dbg(musb->controller, "Host TX FIFONOTEMPTY csr: %02x\n", csr);
David Brownellbb1c9ef2008-11-24 13:06:50 +0200118 lastcsr = csr;
Felipe Balbi550a7372008-07-24 12:27:36 +0300119 csr |= MUSB_TXCSR_FLUSHFIFO;
120 musb_writew(epio, MUSB_TXCSR, csr);
121 csr = musb_readw(epio, MUSB_TXCSR);
David Brownellbb1c9ef2008-11-24 13:06:50 +0200122 if (WARN(retries-- < 1,
123 "Could not flush host TX%d fifo: csr: %04x\n",
124 ep->epnum, csr))
Felipe Balbi550a7372008-07-24 12:27:36 +0300125 return;
Felipe Balbi550a7372008-07-24 12:27:36 +0300126 mdelay(1);
127 }
128}
129
David Brownell78322c12009-03-26 17:38:30 -0700130static void musb_h_ep0_flush_fifo(struct musb_hw_ep *ep)
131{
132 void __iomem *epio = ep->regs;
133 u16 csr;
134 int retries = 5;
135
136 /* scrub any data left in the fifo */
137 do {
138 csr = musb_readw(epio, MUSB_TXCSR);
139 if (!(csr & (MUSB_CSR0_TXPKTRDY | MUSB_CSR0_RXPKTRDY)))
140 break;
141 musb_writew(epio, MUSB_TXCSR, MUSB_CSR0_FLUSHFIFO);
142 csr = musb_readw(epio, MUSB_TXCSR);
143 udelay(10);
144 } while (--retries);
145
146 WARN(!retries, "Could not flush host TX%d fifo: csr: %04x\n",
147 ep->epnum, csr);
148
149 /* and reset for the next transfer */
150 musb_writew(epio, MUSB_TXCSR, 0);
151}
152
Felipe Balbi550a7372008-07-24 12:27:36 +0300153/*
154 * Start transmit. Caller is responsible for locking shared resources.
155 * musb must be locked.
156 */
157static inline void musb_h_tx_start(struct musb_hw_ep *ep)
158{
159 u16 txcsr;
160
161 /* NOTE: no locks here; caller should lock and select EP */
162 if (ep->epnum) {
163 txcsr = musb_readw(ep->regs, MUSB_TXCSR);
164 txcsr |= MUSB_TXCSR_TXPKTRDY | MUSB_TXCSR_H_WZC_BITS;
165 musb_writew(ep->regs, MUSB_TXCSR, txcsr);
166 } else {
167 txcsr = MUSB_CSR0_H_SETUPPKT | MUSB_CSR0_TXPKTRDY;
168 musb_writew(ep->regs, MUSB_CSR0, txcsr);
169 }
170
171}
172
Sergei Shtylyovc7bbc052009-03-26 18:26:40 -0700173static inline void musb_h_tx_dma_start(struct musb_hw_ep *ep)
Felipe Balbi550a7372008-07-24 12:27:36 +0300174{
175 u16 txcsr;
176
177 /* NOTE: no locks here; caller should lock and select EP */
178 txcsr = musb_readw(ep->regs, MUSB_TXCSR);
179 txcsr |= MUSB_TXCSR_DMAENAB | MUSB_TXCSR_H_WZC_BITS;
Sergei Shtylyovc7bbc052009-03-26 18:26:40 -0700180 if (is_cppi_enabled())
181 txcsr |= MUSB_TXCSR_DMAMODE;
Felipe Balbi550a7372008-07-24 12:27:36 +0300182 musb_writew(ep->regs, MUSB_TXCSR, txcsr);
183}
184
Sergei Shtylyov3e5c6dc2009-03-27 12:55:16 -0700185static void musb_ep_set_qh(struct musb_hw_ep *ep, int is_in, struct musb_qh *qh)
186{
187 if (is_in != 0 || ep->is_shared_fifo)
188 ep->in_qh = qh;
189 if (is_in == 0 || ep->is_shared_fifo)
190 ep->out_qh = qh;
191}
192
193static struct musb_qh *musb_ep_get_qh(struct musb_hw_ep *ep, int is_in)
194{
195 return is_in ? ep->in_qh : ep->out_qh;
196}
197
Felipe Balbi550a7372008-07-24 12:27:36 +0300198/*
199 * Start the URB at the front of an endpoint's queue
200 * end must be claimed from the caller.
201 *
202 * Context: controller locked, irqs blocked
203 */
204static void
205musb_start_urb(struct musb *musb, int is_in, struct musb_qh *qh)
206{
207 u16 frame;
208 u32 len;
Felipe Balbi550a7372008-07-24 12:27:36 +0300209 void __iomem *mbase = musb->mregs;
210 struct urb *urb = next_urb(qh);
Sergei Shtylyov6b6e9712009-03-26 18:29:19 -0700211 void *buf = urb->transfer_buffer;
212 u32 offset = 0;
Felipe Balbi550a7372008-07-24 12:27:36 +0300213 struct musb_hw_ep *hw_ep = qh->hw_ep;
214 unsigned pipe = urb->pipe;
215 u8 address = usb_pipedevice(pipe);
216 int epnum = hw_ep->epnum;
217
218 /* initialize software qh state */
219 qh->offset = 0;
220 qh->segsize = 0;
221
222 /* gather right source of data */
223 switch (qh->type) {
224 case USB_ENDPOINT_XFER_CONTROL:
225 /* control transfers always start with SETUP */
226 is_in = 0;
Felipe Balbi550a7372008-07-24 12:27:36 +0300227 musb->ep0_stage = MUSB_EP0_START;
228 buf = urb->setup_packet;
229 len = 8;
230 break;
231 case USB_ENDPOINT_XFER_ISOC:
232 qh->iso_idx = 0;
233 qh->frame = 0;
Sergei Shtylyov6b6e9712009-03-26 18:29:19 -0700234 offset = urb->iso_frame_desc[0].offset;
Felipe Balbi550a7372008-07-24 12:27:36 +0300235 len = urb->iso_frame_desc[0].length;
236 break;
237 default: /* bulk, interrupt */
Ajay Kumar Gupta1e0320f2009-02-24 15:26:13 -0800238 /* actual_length may be nonzero on retry paths */
239 buf = urb->transfer_buffer + urb->actual_length;
240 len = urb->transfer_buffer_length - urb->actual_length;
Felipe Balbi550a7372008-07-24 12:27:36 +0300241 }
242
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300243 dev_dbg(musb->controller, "qh %p urb %p dev%d ep%d%s%s, hw_ep %d, %p/%d\n",
Felipe Balbi550a7372008-07-24 12:27:36 +0300244 qh, urb, address, qh->epnum,
245 is_in ? "in" : "out",
246 ({char *s; switch (qh->type) {
247 case USB_ENDPOINT_XFER_CONTROL: s = ""; break;
248 case USB_ENDPOINT_XFER_BULK: s = "-bulk"; break;
249 case USB_ENDPOINT_XFER_ISOC: s = "-iso"; break;
250 default: s = "-intr"; break;
251 }; s; }),
Sergei Shtylyov6b6e9712009-03-26 18:29:19 -0700252 epnum, buf + offset, len);
Felipe Balbi550a7372008-07-24 12:27:36 +0300253
254 /* Configure endpoint */
Sergei Shtylyov3e5c6dc2009-03-27 12:55:16 -0700255 musb_ep_set_qh(hw_ep, is_in, qh);
Sergei Shtylyov6b6e9712009-03-26 18:29:19 -0700256 musb_ep_program(musb, epnum, urb, !is_in, buf, offset, len);
Felipe Balbi550a7372008-07-24 12:27:36 +0300257
258 /* transmit may have more work: start it when it is time */
259 if (is_in)
260 return;
261
262 /* determine if the time is right for a periodic transfer */
263 switch (qh->type) {
264 case USB_ENDPOINT_XFER_ISOC:
265 case USB_ENDPOINT_XFER_INT:
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300266 dev_dbg(musb->controller, "check whether there's still time for periodic Tx\n");
Felipe Balbi550a7372008-07-24 12:27:36 +0300267 frame = musb_readw(mbase, MUSB_FRAME);
268 /* FIXME this doesn't implement that scheduling policy ...
269 * or handle framecounter wrapping
270 */
271 if ((urb->transfer_flags & URB_ISO_ASAP)
272 || (frame >= urb->start_frame)) {
273 /* REVISIT the SOF irq handler shouldn't duplicate
274 * this code; and we don't init urb->start_frame...
275 */
276 qh->frame = 0;
277 goto start;
278 } else {
279 qh->frame = urb->start_frame;
280 /* enable SOF interrupt so we can count down */
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300281 dev_dbg(musb->controller, "SOF for %d\n", epnum);
Felipe Balbi550a7372008-07-24 12:27:36 +0300282#if 1 /* ifndef CONFIG_ARCH_DAVINCI */
283 musb_writeb(mbase, MUSB_INTRUSBE, 0xff);
284#endif
285 }
286 break;
287 default:
288start:
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300289 dev_dbg(musb->controller, "Start TX%d %s\n", epnum,
Felipe Balbi550a7372008-07-24 12:27:36 +0300290 hw_ep->tx_channel ? "dma" : "pio");
291
292 if (!hw_ep->tx_channel)
293 musb_h_tx_start(hw_ep);
294 else if (is_cppi_enabled() || tusb_dma_omap())
Sergei Shtylyovc7bbc052009-03-26 18:26:40 -0700295 musb_h_tx_dma_start(hw_ep);
Felipe Balbi550a7372008-07-24 12:27:36 +0300296 }
297}
298
Sergei Shtylyovc9cd06b2009-03-27 12:58:31 -0700299/* Context: caller owns controller lock, IRQs are blocked */
300static void musb_giveback(struct musb *musb, struct urb *urb, int status)
Felipe Balbi550a7372008-07-24 12:27:36 +0300301__releases(musb->lock)
302__acquires(musb->lock)
303{
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300304 dev_dbg(musb->controller,
David Brownellbb1c9ef2008-11-24 13:06:50 +0200305 "complete %p %pF (%d), dev%d ep%d%s, %d/%d\n",
306 urb, urb->complete, status,
Felipe Balbi550a7372008-07-24 12:27:36 +0300307 usb_pipedevice(urb->pipe),
308 usb_pipeendpoint(urb->pipe),
309 usb_pipein(urb->pipe) ? "in" : "out",
310 urb->actual_length, urb->transfer_buffer_length
311 );
312
Ajay Kumar Gupta2492e672008-09-11 11:53:21 +0300313 usb_hcd_unlink_urb_from_ep(musb_to_hcd(musb), urb);
Felipe Balbi550a7372008-07-24 12:27:36 +0300314 spin_unlock(&musb->lock);
315 usb_hcd_giveback_urb(musb_to_hcd(musb), urb, status);
316 spin_lock(&musb->lock);
317}
318
Sergei Shtylyov846099a2009-03-27 12:54:21 -0700319/* For bulk/interrupt endpoints only */
320static inline void musb_save_toggle(struct musb_qh *qh, int is_in,
321 struct urb *urb)
Felipe Balbi550a7372008-07-24 12:27:36 +0300322{
Sergei Shtylyov846099a2009-03-27 12:54:21 -0700323 void __iomem *epio = qh->hw_ep->regs;
Felipe Balbi550a7372008-07-24 12:27:36 +0300324 u16 csr;
Felipe Balbi550a7372008-07-24 12:27:36 +0300325
Sergei Shtylyov846099a2009-03-27 12:54:21 -0700326 /*
327 * FIXME: the current Mentor DMA code seems to have
Felipe Balbi550a7372008-07-24 12:27:36 +0300328 * problems getting toggle correct.
329 */
330
Sergei Shtylyov846099a2009-03-27 12:54:21 -0700331 if (is_in)
332 csr = musb_readw(epio, MUSB_RXCSR) & MUSB_RXCSR_H_DATATOGGLE;
Felipe Balbi550a7372008-07-24 12:27:36 +0300333 else
Sergei Shtylyov846099a2009-03-27 12:54:21 -0700334 csr = musb_readw(epio, MUSB_TXCSR) & MUSB_TXCSR_H_DATATOGGLE;
Felipe Balbi550a7372008-07-24 12:27:36 +0300335
Sergei Shtylyov846099a2009-03-27 12:54:21 -0700336 usb_settoggle(urb->dev, qh->epnum, !is_in, csr ? 1 : 0);
Felipe Balbi550a7372008-07-24 12:27:36 +0300337}
338
Sergei Shtylyovc9cd06b2009-03-27 12:58:31 -0700339/*
340 * Advance this hardware endpoint's queue, completing the specified URB and
341 * advancing to either the next URB queued to that qh, or else invalidating
342 * that qh and advancing to the next qh scheduled after the current one.
343 *
344 * Context: caller owns controller lock, IRQs are blocked
345 */
346static void musb_advance_schedule(struct musb *musb, struct urb *urb,
347 struct musb_hw_ep *hw_ep, int is_in)
Felipe Balbi550a7372008-07-24 12:27:36 +0300348{
Sergei Shtylyovc9cd06b2009-03-27 12:58:31 -0700349 struct musb_qh *qh = musb_ep_get_qh(hw_ep, is_in);
Felipe Balbi550a7372008-07-24 12:27:36 +0300350 struct musb_hw_ep *ep = qh->hw_ep;
Felipe Balbi550a7372008-07-24 12:27:36 +0300351 int ready = qh->is_ready;
Sergei Shtylyovc9cd06b2009-03-27 12:58:31 -0700352 int status;
353
354 status = (urb->status == -EINPROGRESS) ? 0 : urb->status;
Felipe Balbi550a7372008-07-24 12:27:36 +0300355
Felipe Balbi550a7372008-07-24 12:27:36 +0300356 /* save toggle eagerly, for paranoia */
357 switch (qh->type) {
358 case USB_ENDPOINT_XFER_BULK:
359 case USB_ENDPOINT_XFER_INT:
Sergei Shtylyov846099a2009-03-27 12:54:21 -0700360 musb_save_toggle(qh, is_in, urb);
Felipe Balbi550a7372008-07-24 12:27:36 +0300361 break;
362 case USB_ENDPOINT_XFER_ISOC:
Sergei Shtylyov1fe975f2009-07-10 20:02:44 +0300363 if (status == 0 && urb->error_count)
Felipe Balbi550a7372008-07-24 12:27:36 +0300364 status = -EXDEV;
365 break;
366 }
367
Felipe Balbi550a7372008-07-24 12:27:36 +0300368 qh->is_ready = 0;
Sergei Shtylyovc9cd06b2009-03-27 12:58:31 -0700369 musb_giveback(musb, urb, status);
Felipe Balbi550a7372008-07-24 12:27:36 +0300370 qh->is_ready = ready;
371
372 /* reclaim resources (and bandwidth) ASAP; deschedule it, and
373 * invalidate qh as soon as list_empty(&hep->urb_list)
374 */
375 if (list_empty(&qh->hep->urb_list)) {
376 struct list_head *head;
Ajay Kumar Gupta8c778db2012-06-21 17:18:12 +0530377 struct dma_controller *dma = musb->dma_controller;
Felipe Balbi550a7372008-07-24 12:27:36 +0300378
Ajay Kumar Gupta8c778db2012-06-21 17:18:12 +0530379 if (is_in) {
Felipe Balbi550a7372008-07-24 12:27:36 +0300380 ep->rx_reinit = 1;
Ajay Kumar Gupta8c778db2012-06-21 17:18:12 +0530381 if (ep->rx_channel) {
382 dma->channel_release(ep->rx_channel);
383 ep->rx_channel = NULL;
384 }
385 } else {
Felipe Balbi550a7372008-07-24 12:27:36 +0300386 ep->tx_reinit = 1;
Ajay Kumar Gupta8c778db2012-06-21 17:18:12 +0530387 if (ep->tx_channel) {
388 dma->channel_release(ep->tx_channel);
389 ep->tx_channel = NULL;
390 }
391 }
Felipe Balbi550a7372008-07-24 12:27:36 +0300392
Sergei Shtylyov3e5c6dc2009-03-27 12:55:16 -0700393 /* Clobber old pointers to this qh */
394 musb_ep_set_qh(ep, is_in, NULL);
Felipe Balbi550a7372008-07-24 12:27:36 +0300395 qh->hep->hcpriv = NULL;
396
397 switch (qh->type) {
398
Ajay Kumar Gupta23d15e02008-10-29 15:10:35 +0200399 case USB_ENDPOINT_XFER_CONTROL:
400 case USB_ENDPOINT_XFER_BULK:
401 /* fifo policy for these lists, except that NAKing
402 * should rotate a qh to the end (for fairness).
403 */
404 if (qh->mux == 1) {
405 head = qh->ring.prev;
406 list_del(&qh->ring);
407 kfree(qh);
408 qh = first_qh(head);
409 break;
410 }
411
Felipe Balbi550a7372008-07-24 12:27:36 +0300412 case USB_ENDPOINT_XFER_ISOC:
413 case USB_ENDPOINT_XFER_INT:
414 /* this is where periodic bandwidth should be
415 * de-allocated if it's tracked and allocated;
416 * and where we'd update the schedule tree...
417 */
Felipe Balbi550a7372008-07-24 12:27:36 +0300418 kfree(qh);
419 qh = NULL;
420 break;
Felipe Balbi550a7372008-07-24 12:27:36 +0300421 }
422 }
Felipe Balbi550a7372008-07-24 12:27:36 +0300423
Sergei Shtylyova2fd8142009-02-21 15:30:45 -0800424 if (qh != NULL && qh->is_ready) {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300425 dev_dbg(musb->controller, "... next ep%d %cX urb %p\n",
Sergei Shtylyovc9cd06b2009-03-27 12:58:31 -0700426 hw_ep->epnum, is_in ? 'R' : 'T', next_urb(qh));
Felipe Balbi550a7372008-07-24 12:27:36 +0300427 musb_start_urb(musb, is_in, qh);
428 }
429}
430
David Brownellc767c1c2008-09-11 11:53:23 +0300431static u16 musb_h_flush_rxfifo(struct musb_hw_ep *hw_ep, u16 csr)
Felipe Balbi550a7372008-07-24 12:27:36 +0300432{
433 /* we don't want fifo to fill itself again;
434 * ignore dma (various models),
435 * leave toggle alone (may not have been saved yet)
436 */
437 csr |= MUSB_RXCSR_FLUSHFIFO | MUSB_RXCSR_RXPKTRDY;
438 csr &= ~(MUSB_RXCSR_H_REQPKT
439 | MUSB_RXCSR_H_AUTOREQ
440 | MUSB_RXCSR_AUTOCLEAR);
441
442 /* write 2x to allow double buffering */
443 musb_writew(hw_ep->regs, MUSB_RXCSR, csr);
444 musb_writew(hw_ep->regs, MUSB_RXCSR, csr);
445
446 /* flush writebuffer */
447 return musb_readw(hw_ep->regs, MUSB_RXCSR);
448}
449
450/*
451 * PIO RX for a packet (or part of it).
452 */
453static bool
454musb_host_packet_rx(struct musb *musb, struct urb *urb, u8 epnum, u8 iso_err)
455{
456 u16 rx_count;
457 u8 *buf;
458 u16 csr;
459 bool done = false;
460 u32 length;
461 int do_flush = 0;
462 struct musb_hw_ep *hw_ep = musb->endpoints + epnum;
463 void __iomem *epio = hw_ep->regs;
464 struct musb_qh *qh = hw_ep->in_qh;
465 int pipe = urb->pipe;
466 void *buffer = urb->transfer_buffer;
467
468 /* musb_ep_select(mbase, epnum); */
469 rx_count = musb_readw(epio, MUSB_RXCOUNT);
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300470 dev_dbg(musb->controller, "RX%d count %d, buffer %p len %d/%d\n", epnum, rx_count,
Felipe Balbi550a7372008-07-24 12:27:36 +0300471 urb->transfer_buffer, qh->offset,
472 urb->transfer_buffer_length);
473
474 /* unload FIFO */
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 }
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300492 dev_dbg(musb->controller, "** OVERFLOW %d into %d\n", rx_count, length);
Felipe Balbi550a7372008-07-24 12:27:36 +0300493 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 /* non-isoch */
505 buf = buffer + qh->offset;
506 length = urb->transfer_buffer_length - qh->offset;
507 if (rx_count > length) {
508 if (urb->status == -EINPROGRESS)
509 urb->status = -EOVERFLOW;
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300510 dev_dbg(musb->controller, "** OVERFLOW %d into %d\n", rx_count, length);
Felipe Balbi550a7372008-07-24 12:27:36 +0300511 do_flush = 1;
512 } else
513 length = rx_count;
514 urb->actual_length += length;
515 qh->offset += length;
516
517 /* see if we are done */
518 done = (urb->actual_length == urb->transfer_buffer_length)
519 || (rx_count < qh->maxpacket)
520 || (urb->status != -EINPROGRESS);
521 if (done
522 && (urb->status == -EINPROGRESS)
523 && (urb->transfer_flags & URB_SHORT_NOT_OK)
524 && (urb->actual_length
525 < urb->transfer_buffer_length))
526 urb->status = -EREMOTEIO;
527 }
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);
Sergei Shtylyovb6e434a2009-03-26 18:27:47 -0700569 csr = musb_readw(ep->regs, MUSB_TXCSR);
Felipe Balbi550a7372008-07-24 12:27:36 +0300570 musb_writew(ep->regs, MUSB_TXCSR,
Sergei Shtylyovb6e434a2009-03-26 18:27:47 -0700571 csr | MUSB_TXCSR_FRCDATATOG);
Felipe Balbi550a7372008-07-24 12:27:36 +0300572 }
Sergei Shtylyovb6e434a2009-03-26 18:27:47 -0700573
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);
Felipe Balbi550a7372008-07-24 12:27:36 +0300580 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) {
Bryan Wuc6cf8b02008-12-02 21:33:48 +0200594 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
Felipe Balbi550a7372008-07-24 12:27:36 +0300598 } 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 */
Cliff Cai9f445cb2010-01-28 20:44:18 -0500605 /* Set RXMAXP with the FIFO size of the endpoint
606 * to disable double buffer mode.
607 */
Felipe Balbi06624812011-01-21 13:39:20 +0800608 if (musb->double_buffer_not_ok)
Cliff Cai9f445cb2010-01-28 20:44:18 -0500609 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));
Felipe Balbi550a7372008-07-24 12:27:36 +0300613
614 ep->rx_reinit = 0;
615}
616
Sergei Shtylyov6b6e9712009-03-26 18:29:19 -0700617static 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;
Ajay Kumar Guptaa483d702009-04-03 16:16:17 -0700634 csr |= MUSB_TXCSR_DMAMODE | MUSB_TXCSR_DMAENAB;
635 /* autoset shouldn't be set in high bandwidth */
supriya karanthf2786282012-12-06 11:16:23 +0530636 /*
637 * Enable Autoset according to table
638 * below
639 * bulk_split hb_mult Autoset_Enable
640 * 0 1 Yes(Normal)
641 * 0 >1 No(High BW ISO)
642 * 1 1 Yes(HS bulk)
643 * 1 >1 Yes(FS bulk)
644 */
645 if (qh->hb_mult == 1 || (qh->hb_mult > 1 &&
646 can_bulk_split(hw_ep->musb, qh->type)))
Ajay Kumar Guptaa483d702009-04-03 16:16:17 -0700647 csr |= MUSB_TXCSR_AUTOSET;
Sergei Shtylyov6b6e9712009-03-26 18:29:19 -0700648 } else {
649 mode = 0;
650 csr &= ~(MUSB_TXCSR_AUTOSET | MUSB_TXCSR_DMAMODE);
651 csr |= MUSB_TXCSR_DMAENAB; /* against programmer's guide */
652 }
653 channel->desired_mode = mode;
654 musb_writew(epio, MUSB_TXCSR, csr);
655#else
656 if (!is_cppi_enabled() && !tusb_dma_omap())
657 return false;
658
659 channel->actual_len = 0;
660
661 /*
662 * TX uses "RNDIS" mode automatically but needs help
663 * to identify the zero-length-final-packet case.
664 */
665 mode = (urb->transfer_flags & URB_ZERO_PACKET) ? 1 : 0;
666#endif
667
668 qh->segsize = length;
669
Santosh Shilimkar4c647332010-09-20 10:32:07 +0300670 /*
671 * Ensure the data reaches to main memory before starting
672 * DMA transfer
673 */
674 wmb();
675
Sergei Shtylyov6b6e9712009-03-26 18:29:19 -0700676 if (!dma->channel_program(channel, pkt_size, mode,
677 urb->transfer_dma + offset, length)) {
678 dma->channel_release(channel);
679 hw_ep->tx_channel = NULL;
680
681 csr = musb_readw(epio, MUSB_TXCSR);
682 csr &= ~(MUSB_TXCSR_AUTOSET | MUSB_TXCSR_DMAENAB);
683 musb_writew(epio, MUSB_TXCSR, csr | MUSB_TXCSR_H_WZC_BITS);
684 return false;
685 }
686 return true;
687}
Felipe Balbi550a7372008-07-24 12:27:36 +0300688
689/*
690 * Program an HDRC endpoint as per the given URB
691 * Context: irqs blocked, controller lock held
692 */
693static void musb_ep_program(struct musb *musb, u8 epnum,
Sergei Shtylyov6b6e9712009-03-26 18:29:19 -0700694 struct urb *urb, int is_out,
695 u8 *buf, u32 offset, u32 len)
Felipe Balbi550a7372008-07-24 12:27:36 +0300696{
697 struct dma_controller *dma_controller;
698 struct dma_channel *dma_channel;
699 u8 dma_ok;
700 void __iomem *mbase = musb->mregs;
701 struct musb_hw_ep *hw_ep = musb->endpoints + epnum;
702 void __iomem *epio = hw_ep->regs;
Sergei Shtylyov3e5c6dc2009-03-27 12:55:16 -0700703 struct musb_qh *qh = musb_ep_get_qh(hw_ep, !is_out);
704 u16 packet_sz = qh->maxpacket;
Ajay Kumar Gupta31321222012-07-20 11:07:22 +0530705 u8 use_dma = 1;
706 u16 csr;
Felipe Balbi550a7372008-07-24 12:27:36 +0300707
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300708 dev_dbg(musb->controller, "%s hw%d urb %p spd%d dev%d ep%d%s "
Felipe Balbi550a7372008-07-24 12:27:36 +0300709 "h_addr%02x h_port%02x bytes %d\n",
710 is_out ? "-->" : "<--",
711 epnum, urb, urb->dev->speed,
712 qh->addr_reg, qh->epnum, is_out ? "out" : "in",
713 qh->h_addr_reg, qh->h_port_reg,
714 len);
715
716 musb_ep_select(mbase, epnum);
717
Ajay Kumar Gupta31321222012-07-20 11:07:22 +0530718 if (is_out && !len) {
719 use_dma = 0;
720 csr = musb_readw(epio, MUSB_TXCSR);
721 csr &= ~MUSB_TXCSR_DMAENAB;
722 musb_writew(epio, MUSB_TXCSR, csr);
723 hw_ep->tx_channel = NULL;
724 }
725
Felipe Balbi550a7372008-07-24 12:27:36 +0300726 /* candidate for DMA? */
727 dma_controller = musb->dma_controller;
Ajay Kumar Gupta31321222012-07-20 11:07:22 +0530728 if (use_dma && is_dma_capable() && epnum && dma_controller) {
Felipe Balbi550a7372008-07-24 12:27:36 +0300729 dma_channel = is_out ? hw_ep->tx_channel : hw_ep->rx_channel;
730 if (!dma_channel) {
731 dma_channel = dma_controller->channel_alloc(
732 dma_controller, hw_ep, is_out);
733 if (is_out)
734 hw_ep->tx_channel = dma_channel;
735 else
736 hw_ep->rx_channel = dma_channel;
737 }
738 } else
739 dma_channel = NULL;
740
741 /* make sure we clear DMAEnab, autoSet bits from previous run */
742
743 /* OUT/transmit/EP0 or IN/receive? */
744 if (is_out) {
745 u16 csr;
746 u16 int_txe;
747 u16 load_count;
748
749 csr = musb_readw(epio, MUSB_TXCSR);
750
751 /* disable interrupt in case we flush */
Sebastian Andrzej Siewiorb18d26f2012-10-30 19:52:26 +0100752 int_txe = musb->intrtxe;
Felipe Balbi550a7372008-07-24 12:27:36 +0300753 musb_writew(mbase, MUSB_INTRTXE, int_txe & ~(1 << epnum));
754
755 /* general endpoint setup */
756 if (epnum) {
Felipe Balbi550a7372008-07-24 12:27:36 +0300757 /* flush all old state, set default */
supriya karantha70b8442013-01-04 17:10:33 +0530758 /*
759 * We could be flushing valid
760 * packets in double buffering
761 * case
762 */
763 if (!hw_ep->tx_double_buffered)
764 musb_h_tx_flush_fifo(hw_ep);
Sergei Shtylyovb6e434a2009-03-26 18:27:47 -0700765
766 /*
767 * We must not clear the DMAMODE bit before or in
768 * the same cycle with the DMAENAB bit, so we clear
769 * the latter first...
770 */
Felipe Balbi550a7372008-07-24 12:27:36 +0300771 csr &= ~(MUSB_TXCSR_H_NAKTIMEOUT
Sergei Shtylyovb6e434a2009-03-26 18:27:47 -0700772 | MUSB_TXCSR_AUTOSET
773 | MUSB_TXCSR_DMAENAB
Felipe Balbi550a7372008-07-24 12:27:36 +0300774 | MUSB_TXCSR_FRCDATATOG
775 | MUSB_TXCSR_H_RXSTALL
776 | MUSB_TXCSR_H_ERROR
777 | MUSB_TXCSR_TXPKTRDY
778 );
779 csr |= MUSB_TXCSR_MODE;
780
supriya karantha70b8442013-01-04 17:10:33 +0530781 if (!hw_ep->tx_double_buffered) {
782 if (usb_gettoggle(urb->dev, qh->epnum, 1))
783 csr |= MUSB_TXCSR_H_WR_DATATOGGLE
784 | MUSB_TXCSR_H_DATATOGGLE;
785 else
786 csr |= MUSB_TXCSR_CLRDATATOG;
787 }
Felipe Balbi550a7372008-07-24 12:27:36 +0300788
Felipe Balbi550a7372008-07-24 12:27:36 +0300789 musb_writew(epio, MUSB_TXCSR, csr);
790 /* REVISIT may need to clear FLUSHFIFO ... */
Sergei Shtylyovb6e434a2009-03-26 18:27:47 -0700791 csr &= ~MUSB_TXCSR_DMAMODE;
Felipe Balbi550a7372008-07-24 12:27:36 +0300792 musb_writew(epio, MUSB_TXCSR, csr);
793 csr = musb_readw(epio, MUSB_TXCSR);
794 } else {
795 /* endpoint 0: just flush */
David Brownell78322c12009-03-26 17:38:30 -0700796 musb_h_ep0_flush_fifo(hw_ep);
Felipe Balbi550a7372008-07-24 12:27:36 +0300797 }
798
799 /* target addr and (for multipoint) hub addr/port */
800 if (musb->is_multipoint) {
Bryan Wuc6cf8b02008-12-02 21:33:48 +0200801 musb_write_txfunaddr(mbase, epnum, qh->addr_reg);
802 musb_write_txhubaddr(mbase, epnum, qh->h_addr_reg);
803 musb_write_txhubport(mbase, epnum, qh->h_port_reg);
Felipe Balbi550a7372008-07-24 12:27:36 +0300804/* FIXME if !epnum, do the same for RX ... */
805 } else
806 musb_writeb(mbase, MUSB_FADDR, qh->addr_reg);
807
808 /* protocol/endpoint/interval/NAKlimit */
809 if (epnum) {
810 musb_writeb(epio, MUSB_TXTYPE, qh->type_reg);
supriya karanthf2786282012-12-06 11:16:23 +0530811 if (musb->double_buffer_not_ok) {
Felipe Balbi550a7372008-07-24 12:27:36 +0300812 musb_writew(epio, MUSB_TXMAXP,
Felipe Balbi06624812011-01-21 13:39:20 +0800813 hw_ep->max_packet_sz_tx);
supriya karanthf2786282012-12-06 11:16:23 +0530814 } else if (can_bulk_split(musb, qh->type)) {
815 qh->hb_mult = hw_ep->max_packet_sz_tx
816 / packet_sz;
Ajay Kumar Guptaccc080c2011-12-13 10:32:42 +0530817 musb_writew(epio, MUSB_TXMAXP, packet_sz
supriya karanthf2786282012-12-06 11:16:23 +0530818 | ((qh->hb_mult) - 1) << 11);
819 } else {
Felipe Balbi550a7372008-07-24 12:27:36 +0300820 musb_writew(epio, MUSB_TXMAXP,
Felipe Balbi06624812011-01-21 13:39:20 +0800821 qh->maxpacket |
822 ((qh->hb_mult - 1) << 11));
supriya karanthf2786282012-12-06 11:16:23 +0530823 }
Felipe Balbi550a7372008-07-24 12:27:36 +0300824 musb_writeb(epio, MUSB_TXINTERVAL, qh->intv_reg);
825 } else {
826 musb_writeb(epio, MUSB_NAKLIMIT0, qh->intv_reg);
827 if (musb->is_multipoint)
828 musb_writeb(epio, MUSB_TYPE0,
829 qh->type_reg);
830 }
831
832 if (can_bulk_split(musb, qh->type))
833 load_count = min((u32) hw_ep->max_packet_sz_tx,
834 len);
835 else
836 load_count = min((u32) packet_sz, len);
837
Sergei Shtylyov6b6e9712009-03-26 18:29:19 -0700838 if (dma_channel && musb_tx_dma_program(dma_controller,
839 hw_ep, qh, urb, offset, len))
840 load_count = 0;
Felipe Balbi550a7372008-07-24 12:27:36 +0300841
842 if (load_count) {
Felipe Balbi550a7372008-07-24 12:27:36 +0300843 /* PIO to load FIFO */
844 qh->segsize = load_count;
Virupax Sadashivpetimath8e8a5512012-08-07 14:46:20 +0530845 if (!buf) {
846 sg_miter_start(&qh->sg_miter, urb->sg, 1,
847 SG_MITER_ATOMIC
848 | SG_MITER_FROM_SG);
849 if (!sg_miter_next(&qh->sg_miter)) {
850 dev_err(musb->controller,
851 "error: sg"
852 "list empty\n");
853 sg_miter_stop(&qh->sg_miter);
854 goto finish;
855 }
856 buf = qh->sg_miter.addr + urb->sg->offset +
857 urb->actual_length;
858 load_count = min_t(u32, load_count,
859 qh->sg_miter.length);
860 musb_write_fifo(hw_ep, load_count, buf);
861 qh->sg_miter.consumed = load_count;
862 sg_miter_stop(&qh->sg_miter);
863 } else
864 musb_write_fifo(hw_ep, load_count, buf);
Felipe Balbi550a7372008-07-24 12:27:36 +0300865 }
Virupax Sadashivpetimath8e8a5512012-08-07 14:46:20 +0530866finish:
Felipe Balbi550a7372008-07-24 12:27:36 +0300867 /* re-enable interrupt */
868 musb_writew(mbase, MUSB_INTRTXE, int_txe);
869
870 /* IN/receive */
871 } else {
872 u16 csr;
873
874 if (hw_ep->rx_reinit) {
875 musb_rx_reinit(musb, qh, hw_ep);
876
877 /* init new state: toggle and NYET, maybe DMA later */
878 if (usb_gettoggle(urb->dev, qh->epnum, 0))
879 csr = MUSB_RXCSR_H_WR_DATATOGGLE
880 | MUSB_RXCSR_H_DATATOGGLE;
881 else
882 csr = 0;
883 if (qh->type == USB_ENDPOINT_XFER_INT)
884 csr |= MUSB_RXCSR_DISNYET;
885
886 } else {
887 csr = musb_readw(hw_ep->regs, MUSB_RXCSR);
888
889 if (csr & (MUSB_RXCSR_RXPKTRDY
890 | MUSB_RXCSR_DMAENAB
891 | MUSB_RXCSR_H_REQPKT))
892 ERR("broken !rx_reinit, ep%d csr %04x\n",
893 hw_ep->epnum, csr);
894
895 /* scrub any stale state, leaving toggle alone */
896 csr &= MUSB_RXCSR_DISNYET;
897 }
898
899 /* kick things off */
900
901 if ((is_cppi_enabled() || tusb_dma_omap()) && dma_channel) {
Sergei Shtylyovc51e36d2011-05-07 19:44:13 +0400902 /* Candidate for DMA */
903 dma_channel->actual_len = 0L;
904 qh->segsize = len;
Felipe Balbi550a7372008-07-24 12:27:36 +0300905
Sergei Shtylyovc51e36d2011-05-07 19:44:13 +0400906 /* AUTOREQ is in a DMA register */
907 musb_writew(hw_ep->regs, MUSB_RXCSR, csr);
908 csr = musb_readw(hw_ep->regs, MUSB_RXCSR);
Felipe Balbi550a7372008-07-24 12:27:36 +0300909
Sergei Shtylyovc51e36d2011-05-07 19:44:13 +0400910 /*
911 * Unless caller treats short RX transfers as
912 * errors, we dare not queue multiple transfers.
913 */
914 dma_ok = dma_controller->channel_program(dma_channel,
915 packet_sz, !(urb->transfer_flags &
916 URB_SHORT_NOT_OK),
917 urb->transfer_dma + offset,
918 qh->segsize);
919 if (!dma_ok) {
920 dma_controller->channel_release(dma_channel);
921 hw_ep->rx_channel = dma_channel = NULL;
922 } else
923 csr |= MUSB_RXCSR_DMAENAB;
Felipe Balbi550a7372008-07-24 12:27:36 +0300924 }
925
926 csr |= MUSB_RXCSR_H_REQPKT;
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300927 dev_dbg(musb->controller, "RXCSR%d := %04x\n", epnum, csr);
Felipe Balbi550a7372008-07-24 12:27:36 +0300928 musb_writew(hw_ep->regs, MUSB_RXCSR, csr);
929 csr = musb_readw(hw_ep->regs, MUSB_RXCSR);
930 }
931}
932
Ajay Kumar Guptaf2838622012-07-19 13:41:59 +0530933/* Schedule next QH from musb->in_bulk/out_bulk and move the current qh to
934 * the end; avoids starvation for other endpoints.
935 */
936static void musb_bulk_nak_timeout(struct musb *musb, struct musb_hw_ep *ep,
937 int is_in)
938{
939 struct dma_channel *dma;
940 struct urb *urb;
941 void __iomem *mbase = musb->mregs;
942 void __iomem *epio = ep->regs;
943 struct musb_qh *cur_qh, *next_qh;
944 u16 rx_csr, tx_csr;
945
946 musb_ep_select(mbase, ep->epnum);
947 if (is_in) {
948 dma = is_dma_capable() ? ep->rx_channel : NULL;
949
950 /* clear nak timeout bit */
951 rx_csr = musb_readw(epio, MUSB_RXCSR);
952 rx_csr |= MUSB_RXCSR_H_WZC_BITS;
953 rx_csr &= ~MUSB_RXCSR_DATAERROR;
954 musb_writew(epio, MUSB_RXCSR, rx_csr);
955
956 cur_qh = first_qh(&musb->in_bulk);
957 } else {
958 dma = is_dma_capable() ? ep->tx_channel : NULL;
959
960 /* clear nak timeout bit */
961 tx_csr = musb_readw(epio, MUSB_TXCSR);
962 tx_csr |= MUSB_TXCSR_H_WZC_BITS;
963 tx_csr &= ~MUSB_TXCSR_H_NAKTIMEOUT;
964 musb_writew(epio, MUSB_TXCSR, tx_csr);
965
966 cur_qh = first_qh(&musb->out_bulk);
967 }
968 if (cur_qh) {
969 urb = next_urb(cur_qh);
970 if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) {
971 dma->status = MUSB_DMA_STATUS_CORE_ABORT;
972 musb->dma_controller->channel_abort(dma);
973 urb->actual_length += dma->actual_len;
974 dma->actual_len = 0L;
975 }
976 musb_save_toggle(cur_qh, is_in, urb);
977
978 if (is_in) {
979 /* move cur_qh to end of queue */
980 list_move_tail(&cur_qh->ring, &musb->in_bulk);
981
982 /* get the next qh from musb->in_bulk */
983 next_qh = first_qh(&musb->in_bulk);
984
985 /* set rx_reinit and schedule the next qh */
986 ep->rx_reinit = 1;
987 } else {
988 /* move cur_qh to end of queue */
989 list_move_tail(&cur_qh->ring, &musb->out_bulk);
990
991 /* get the next qh from musb->out_bulk */
992 next_qh = first_qh(&musb->out_bulk);
993
994 /* set tx_reinit and schedule the next qh */
995 ep->tx_reinit = 1;
996 }
997 musb_start_urb(musb, is_in, next_qh);
998 }
999}
Felipe Balbi550a7372008-07-24 12:27:36 +03001000
1001/*
1002 * Service the default endpoint (ep0) as host.
1003 * Return true until it's time to start the status stage.
1004 */
1005static bool musb_h_ep0_continue(struct musb *musb, u16 len, struct urb *urb)
1006{
1007 bool more = false;
1008 u8 *fifo_dest = NULL;
1009 u16 fifo_count = 0;
1010 struct musb_hw_ep *hw_ep = musb->control_ep;
1011 struct musb_qh *qh = hw_ep->in_qh;
1012 struct usb_ctrlrequest *request;
1013
1014 switch (musb->ep0_stage) {
1015 case MUSB_EP0_IN:
1016 fifo_dest = urb->transfer_buffer + urb->actual_length;
Sergei Shtylyov3ecdb9a2009-02-21 15:31:23 -08001017 fifo_count = min_t(size_t, len, urb->transfer_buffer_length -
1018 urb->actual_length);
Felipe Balbi550a7372008-07-24 12:27:36 +03001019 if (fifo_count < len)
1020 urb->status = -EOVERFLOW;
1021
1022 musb_read_fifo(hw_ep, fifo_count, fifo_dest);
1023
1024 urb->actual_length += fifo_count;
1025 if (len < qh->maxpacket) {
1026 /* always terminate on short read; it's
1027 * rarely reported as an error.
1028 */
1029 } else if (urb->actual_length <
1030 urb->transfer_buffer_length)
1031 more = true;
1032 break;
1033 case MUSB_EP0_START:
1034 request = (struct usb_ctrlrequest *) urb->setup_packet;
1035
1036 if (!request->wLength) {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001037 dev_dbg(musb->controller, "start no-DATA\n");
Felipe Balbi550a7372008-07-24 12:27:36 +03001038 break;
1039 } else if (request->bRequestType & USB_DIR_IN) {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001040 dev_dbg(musb->controller, "start IN-DATA\n");
Felipe Balbi550a7372008-07-24 12:27:36 +03001041 musb->ep0_stage = MUSB_EP0_IN;
1042 more = true;
1043 break;
1044 } else {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001045 dev_dbg(musb->controller, "start OUT-DATA\n");
Felipe Balbi550a7372008-07-24 12:27:36 +03001046 musb->ep0_stage = MUSB_EP0_OUT;
1047 more = true;
1048 }
1049 /* FALLTHROUGH */
1050 case MUSB_EP0_OUT:
Sergei Shtylyov3ecdb9a2009-02-21 15:31:23 -08001051 fifo_count = min_t(size_t, qh->maxpacket,
1052 urb->transfer_buffer_length -
1053 urb->actual_length);
Felipe Balbi550a7372008-07-24 12:27:36 +03001054 if (fifo_count) {
1055 fifo_dest = (u8 *) (urb->transfer_buffer
1056 + urb->actual_length);
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001057 dev_dbg(musb->controller, "Sending %d byte%s to ep0 fifo %p\n",
David Brownellbb1c9ef2008-11-24 13:06:50 +02001058 fifo_count,
1059 (fifo_count == 1) ? "" : "s",
1060 fifo_dest);
Felipe Balbi550a7372008-07-24 12:27:36 +03001061 musb_write_fifo(hw_ep, fifo_count, fifo_dest);
1062
1063 urb->actual_length += fifo_count;
1064 more = true;
1065 }
1066 break;
1067 default:
1068 ERR("bogus ep0 stage %d\n", musb->ep0_stage);
1069 break;
1070 }
1071
1072 return more;
1073}
1074
1075/*
1076 * Handle default endpoint interrupt as host. Only called in IRQ time
David Brownellc767c1c2008-09-11 11:53:23 +03001077 * from musb_interrupt().
Felipe Balbi550a7372008-07-24 12:27:36 +03001078 *
1079 * called with controller irqlocked
1080 */
1081irqreturn_t musb_h_ep0_irq(struct musb *musb)
1082{
1083 struct urb *urb;
1084 u16 csr, len;
1085 int status = 0;
1086 void __iomem *mbase = musb->mregs;
1087 struct musb_hw_ep *hw_ep = musb->control_ep;
1088 void __iomem *epio = hw_ep->regs;
1089 struct musb_qh *qh = hw_ep->in_qh;
1090 bool complete = false;
1091 irqreturn_t retval = IRQ_NONE;
1092
1093 /* ep0 only has one queue, "in" */
1094 urb = next_urb(qh);
1095
1096 musb_ep_select(mbase, 0);
1097 csr = musb_readw(epio, MUSB_CSR0);
1098 len = (csr & MUSB_CSR0_RXPKTRDY)
1099 ? musb_readb(epio, MUSB_COUNT0)
1100 : 0;
1101
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001102 dev_dbg(musb->controller, "<== csr0 %04x, qh %p, count %d, urb %p, stage %d\n",
Felipe Balbi550a7372008-07-24 12:27:36 +03001103 csr, qh, len, urb, musb->ep0_stage);
1104
1105 /* if we just did status stage, we are done */
1106 if (MUSB_EP0_STATUS == musb->ep0_stage) {
1107 retval = IRQ_HANDLED;
1108 complete = true;
1109 }
1110
1111 /* prepare status */
1112 if (csr & MUSB_CSR0_H_RXSTALL) {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001113 dev_dbg(musb->controller, "STALLING ENDPOINT\n");
Felipe Balbi550a7372008-07-24 12:27:36 +03001114 status = -EPIPE;
1115
1116 } else if (csr & MUSB_CSR0_H_ERROR) {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001117 dev_dbg(musb->controller, "no response, csr0 %04x\n", csr);
Felipe Balbi550a7372008-07-24 12:27:36 +03001118 status = -EPROTO;
1119
1120 } else if (csr & MUSB_CSR0_H_NAKTIMEOUT) {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001121 dev_dbg(musb->controller, "control NAK timeout\n");
Felipe Balbi550a7372008-07-24 12:27:36 +03001122
1123 /* NOTE: this code path would be a good place to PAUSE a
1124 * control transfer, if another one is queued, so that
Ajay Kumar Gupta1e0320f2009-02-24 15:26:13 -08001125 * ep0 is more likely to stay busy. That's already done
1126 * for bulk RX transfers.
Felipe Balbi550a7372008-07-24 12:27:36 +03001127 *
1128 * if (qh->ring.next != &musb->control), then
1129 * we have a candidate... NAKing is *NOT* an error
1130 */
1131 musb_writew(epio, MUSB_CSR0, 0);
1132 retval = IRQ_HANDLED;
1133 }
1134
1135 if (status) {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001136 dev_dbg(musb->controller, "aborting\n");
Felipe Balbi550a7372008-07-24 12:27:36 +03001137 retval = IRQ_HANDLED;
1138 if (urb)
1139 urb->status = status;
1140 complete = true;
1141
1142 /* use the proper sequence to abort the transfer */
1143 if (csr & MUSB_CSR0_H_REQPKT) {
1144 csr &= ~MUSB_CSR0_H_REQPKT;
1145 musb_writew(epio, MUSB_CSR0, csr);
1146 csr &= ~MUSB_CSR0_H_NAKTIMEOUT;
1147 musb_writew(epio, MUSB_CSR0, csr);
1148 } else {
David Brownell78322c12009-03-26 17:38:30 -07001149 musb_h_ep0_flush_fifo(hw_ep);
Felipe Balbi550a7372008-07-24 12:27:36 +03001150 }
1151
1152 musb_writeb(epio, MUSB_NAKLIMIT0, 0);
1153
1154 /* clear it */
1155 musb_writew(epio, MUSB_CSR0, 0);
1156 }
1157
1158 if (unlikely(!urb)) {
1159 /* stop endpoint since we have no place for its data, this
1160 * SHOULD NEVER HAPPEN! */
1161 ERR("no URB for end 0\n");
1162
David Brownell78322c12009-03-26 17:38:30 -07001163 musb_h_ep0_flush_fifo(hw_ep);
Felipe Balbi550a7372008-07-24 12:27:36 +03001164 goto done;
1165 }
1166
1167 if (!complete) {
1168 /* call common logic and prepare response */
1169 if (musb_h_ep0_continue(musb, len, urb)) {
1170 /* more packets required */
1171 csr = (MUSB_EP0_IN == musb->ep0_stage)
1172 ? MUSB_CSR0_H_REQPKT : MUSB_CSR0_TXPKTRDY;
1173 } else {
1174 /* data transfer complete; perform status phase */
1175 if (usb_pipeout(urb->pipe)
1176 || !urb->transfer_buffer_length)
1177 csr = MUSB_CSR0_H_STATUSPKT
1178 | MUSB_CSR0_H_REQPKT;
1179 else
1180 csr = MUSB_CSR0_H_STATUSPKT
1181 | MUSB_CSR0_TXPKTRDY;
1182
1183 /* flag status stage */
1184 musb->ep0_stage = MUSB_EP0_STATUS;
1185
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001186 dev_dbg(musb->controller, "ep0 STATUS, csr %04x\n", csr);
Felipe Balbi550a7372008-07-24 12:27:36 +03001187
1188 }
1189 musb_writew(epio, MUSB_CSR0, csr);
1190 retval = IRQ_HANDLED;
1191 } else
1192 musb->ep0_stage = MUSB_EP0_IDLE;
1193
1194 /* call completion handler if done */
1195 if (complete)
1196 musb_advance_schedule(musb, urb, hw_ep, 1);
1197done:
1198 return retval;
1199}
1200
1201
1202#ifdef CONFIG_USB_INVENTRA_DMA
1203
1204/* Host side TX (OUT) using Mentor DMA works as follows:
1205 submit_urb ->
1206 - if queue was empty, Program Endpoint
1207 - ... which starts DMA to fifo in mode 1 or 0
1208
1209 DMA Isr (transfer complete) -> TxAvail()
1210 - Stop DMA (~DmaEnab) (<--- Alert ... currently happens
1211 only in musb_cleanup_urb)
1212 - TxPktRdy has to be set in mode 0 or for
1213 short packets in mode 1.
1214*/
1215
1216#endif
1217
1218/* Service a Tx-Available or dma completion irq for the endpoint */
1219void musb_host_tx(struct musb *musb, u8 epnum)
1220{
1221 int pipe;
1222 bool done = false;
1223 u16 tx_csr;
Sergei Shtylyov6b6e9712009-03-26 18:29:19 -07001224 size_t length = 0;
1225 size_t offset = 0;
Felipe Balbi550a7372008-07-24 12:27:36 +03001226 struct musb_hw_ep *hw_ep = musb->endpoints + epnum;
1227 void __iomem *epio = hw_ep->regs;
Sergei Shtylyov3e5c6dc2009-03-27 12:55:16 -07001228 struct musb_qh *qh = hw_ep->out_qh;
1229 struct urb *urb = next_urb(qh);
Felipe Balbi550a7372008-07-24 12:27:36 +03001230 u32 status = 0;
1231 void __iomem *mbase = musb->mregs;
1232 struct dma_channel *dma;
T. S., Anil Kumarf8afbf7f2010-09-24 13:44:09 +03001233 bool transfer_pending = false;
Virupax Sadashivpetimath8e8a5512012-08-07 14:46:20 +05301234 static bool use_sg;
Felipe Balbi550a7372008-07-24 12:27:36 +03001235
Felipe Balbi550a7372008-07-24 12:27:36 +03001236 musb_ep_select(mbase, epnum);
1237 tx_csr = musb_readw(epio, MUSB_TXCSR);
1238
1239 /* with CPPI, DMA sometimes triggers "extra" irqs */
1240 if (!urb) {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001241 dev_dbg(musb->controller, "extra TX%d ready, csr %04x\n", epnum, tx_csr);
Sergei Shtylyov6b6e9712009-03-26 18:29:19 -07001242 return;
Felipe Balbi550a7372008-07-24 12:27:36 +03001243 }
1244
1245 pipe = urb->pipe;
1246 dma = is_dma_capable() ? hw_ep->tx_channel : NULL;
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001247 dev_dbg(musb->controller, "OUT/TX%d end, csr %04x%s\n", epnum, tx_csr,
Felipe Balbi550a7372008-07-24 12:27:36 +03001248 dma ? ", dma" : "");
1249
1250 /* check for errors */
1251 if (tx_csr & MUSB_TXCSR_H_RXSTALL) {
1252 /* dma was disabled, fifo flushed */
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001253 dev_dbg(musb->controller, "TX end %d stall\n", epnum);
Felipe Balbi550a7372008-07-24 12:27:36 +03001254
1255 /* stall; record URB status */
1256 status = -EPIPE;
1257
1258 } else if (tx_csr & MUSB_TXCSR_H_ERROR) {
1259 /* (NON-ISO) dma was disabled, fifo flushed */
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001260 dev_dbg(musb->controller, "TX 3strikes on ep=%d\n", epnum);
Felipe Balbi550a7372008-07-24 12:27:36 +03001261
1262 status = -ETIMEDOUT;
1263
1264 } else if (tx_csr & MUSB_TXCSR_H_NAKTIMEOUT) {
Ajay Kumar Guptaf2838622012-07-19 13:41:59 +05301265 if (USB_ENDPOINT_XFER_BULK == qh->type && qh->mux == 1
1266 && !list_is_singular(&musb->out_bulk)) {
1267 dev_dbg(musb->controller,
1268 "NAK timeout on TX%d ep\n", epnum);
1269 musb_bulk_nak_timeout(musb, hw_ep, 0);
1270 } else {
1271 dev_dbg(musb->controller,
1272 "TX end=%d device not responding\n", epnum);
1273 /* NOTE: this code path would be a good place to PAUSE a
1274 * transfer, if there's some other (nonperiodic) tx urb
1275 * that could use this fifo. (dma complicates it...)
1276 * That's already done for bulk RX transfers.
1277 *
1278 * if (bulk && qh->ring.next != &musb->out_bulk), then
1279 * we have a candidate... NAKing is *NOT* an error
1280 */
1281 musb_ep_select(mbase, epnum);
1282 musb_writew(epio, MUSB_TXCSR,
1283 MUSB_TXCSR_H_WZC_BITS
1284 | MUSB_TXCSR_TXPKTRDY);
1285 }
1286 return;
Felipe Balbi550a7372008-07-24 12:27:36 +03001287 }
1288
Virupax Sadashivpetimath8e8a5512012-08-07 14:46:20 +05301289done:
Felipe Balbi550a7372008-07-24 12:27:36 +03001290 if (status) {
1291 if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) {
1292 dma->status = MUSB_DMA_STATUS_CORE_ABORT;
1293 (void) musb->dma_controller->channel_abort(dma);
1294 }
1295
1296 /* do the proper sequence to abort the transfer in the
1297 * usb core; the dma engine should already be stopped.
1298 */
1299 musb_h_tx_flush_fifo(hw_ep);
1300 tx_csr &= ~(MUSB_TXCSR_AUTOSET
1301 | MUSB_TXCSR_DMAENAB
1302 | MUSB_TXCSR_H_ERROR
1303 | MUSB_TXCSR_H_RXSTALL
1304 | MUSB_TXCSR_H_NAKTIMEOUT
1305 );
1306
1307 musb_ep_select(mbase, epnum);
1308 musb_writew(epio, MUSB_TXCSR, tx_csr);
1309 /* REVISIT may need to clear FLUSHFIFO ... */
1310 musb_writew(epio, MUSB_TXCSR, tx_csr);
1311 musb_writeb(epio, MUSB_TXINTERVAL, 0);
1312
1313 done = true;
1314 }
1315
1316 /* second cppi case */
1317 if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001318 dev_dbg(musb->controller, "extra TX%d ready, csr %04x\n", epnum, tx_csr);
Sergei Shtylyov6b6e9712009-03-26 18:29:19 -07001319 return;
Felipe Balbi550a7372008-07-24 12:27:36 +03001320 }
1321
Sergei Shtylyovc7bbc052009-03-26 18:26:40 -07001322 if (is_dma_capable() && dma && !status) {
1323 /*
1324 * DMA has completed. But if we're using DMA mode 1 (multi
1325 * packet DMA), we need a terminal TXPKTRDY interrupt before
1326 * we can consider this transfer completed, lest we trash
1327 * its last packet when writing the next URB's data. So we
1328 * switch back to mode 0 to get that interrupt; we'll come
1329 * back here once it happens.
1330 */
1331 if (tx_csr & MUSB_TXCSR_DMAMODE) {
1332 /*
1333 * We shouldn't clear DMAMODE with DMAENAB set; so
1334 * clear them in a safe order. That should be OK
1335 * once TXPKTRDY has been set (and I've never seen
1336 * it being 0 at this moment -- DMA interrupt latency
1337 * is significant) but if it hasn't been then we have
1338 * no choice but to stop being polite and ignore the
1339 * programmer's guide... :-)
1340 *
1341 * Note that we must write TXCSR with TXPKTRDY cleared
1342 * in order not to re-trigger the packet send (this bit
1343 * can't be cleared by CPU), and there's another caveat:
1344 * TXPKTRDY may be set shortly and then cleared in the
1345 * double-buffered FIFO mode, so we do an extra TXCSR
1346 * read for debouncing...
1347 */
1348 tx_csr &= musb_readw(epio, MUSB_TXCSR);
1349 if (tx_csr & MUSB_TXCSR_TXPKTRDY) {
1350 tx_csr &= ~(MUSB_TXCSR_DMAENAB |
1351 MUSB_TXCSR_TXPKTRDY);
1352 musb_writew(epio, MUSB_TXCSR,
1353 tx_csr | MUSB_TXCSR_H_WZC_BITS);
1354 }
1355 tx_csr &= ~(MUSB_TXCSR_DMAMODE |
1356 MUSB_TXCSR_TXPKTRDY);
1357 musb_writew(epio, MUSB_TXCSR,
1358 tx_csr | MUSB_TXCSR_H_WZC_BITS);
1359
1360 /*
1361 * There is no guarantee that we'll get an interrupt
1362 * after clearing DMAMODE as we might have done this
1363 * too late (after TXPKTRDY was cleared by controller).
1364 * Re-read TXCSR as we have spoiled its previous value.
1365 */
1366 tx_csr = musb_readw(epio, MUSB_TXCSR);
1367 }
1368
1369 /*
1370 * We may get here from a DMA completion or TXPKTRDY interrupt.
1371 * In any case, we must check the FIFO status here and bail out
1372 * only if the FIFO still has data -- that should prevent the
1373 * "missed" TXPKTRDY interrupts and deal with double-buffered
1374 * FIFO mode too...
1375 */
1376 if (tx_csr & (MUSB_TXCSR_FIFONOTEMPTY | MUSB_TXCSR_TXPKTRDY)) {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001377 dev_dbg(musb->controller, "DMA complete but packet still in FIFO, "
Sergei Shtylyovc7bbc052009-03-26 18:26:40 -07001378 "CSR %04x\n", tx_csr);
1379 return;
1380 }
1381 }
1382
Felipe Balbi550a7372008-07-24 12:27:36 +03001383 if (!status || dma || usb_pipeisoc(pipe)) {
1384 if (dma)
Sergei Shtylyov6b6e9712009-03-26 18:29:19 -07001385 length = dma->actual_len;
Felipe Balbi550a7372008-07-24 12:27:36 +03001386 else
Sergei Shtylyov6b6e9712009-03-26 18:29:19 -07001387 length = qh->segsize;
1388 qh->offset += length;
Felipe Balbi550a7372008-07-24 12:27:36 +03001389
1390 if (usb_pipeisoc(pipe)) {
1391 struct usb_iso_packet_descriptor *d;
1392
1393 d = urb->iso_frame_desc + qh->iso_idx;
Sergei Shtylyov6b6e9712009-03-26 18:29:19 -07001394 d->actual_length = length;
1395 d->status = status;
Felipe Balbi550a7372008-07-24 12:27:36 +03001396 if (++qh->iso_idx >= urb->number_of_packets) {
1397 done = true;
1398 } else {
1399 d++;
Sergei Shtylyov6b6e9712009-03-26 18:29:19 -07001400 offset = d->offset;
1401 length = d->length;
Felipe Balbi550a7372008-07-24 12:27:36 +03001402 }
T. S., Anil Kumarf8afbf7f2010-09-24 13:44:09 +03001403 } else if (dma && urb->transfer_buffer_length == qh->offset) {
Felipe Balbi550a7372008-07-24 12:27:36 +03001404 done = true;
1405 } else {
1406 /* see if we need to send more data, or ZLP */
1407 if (qh->segsize < qh->maxpacket)
1408 done = true;
1409 else if (qh->offset == urb->transfer_buffer_length
1410 && !(urb->transfer_flags
1411 & URB_ZERO_PACKET))
1412 done = true;
1413 if (!done) {
Sergei Shtylyov6b6e9712009-03-26 18:29:19 -07001414 offset = qh->offset;
1415 length = urb->transfer_buffer_length - offset;
T. S., Anil Kumarf8afbf7f2010-09-24 13:44:09 +03001416 transfer_pending = true;
Felipe Balbi550a7372008-07-24 12:27:36 +03001417 }
1418 }
1419 }
1420
1421 /* urb->status != -EINPROGRESS means request has been faulted,
1422 * so we must abort this transfer after cleanup
1423 */
1424 if (urb->status != -EINPROGRESS) {
1425 done = true;
1426 if (status == 0)
1427 status = urb->status;
1428 }
1429
1430 if (done) {
1431 /* set status */
1432 urb->status = status;
1433 urb->actual_length = qh->offset;
1434 musb_advance_schedule(musb, urb, hw_ep, USB_DIR_OUT);
Sergei Shtylyov6b6e9712009-03-26 18:29:19 -07001435 return;
T. S., Anil Kumarf8afbf7f2010-09-24 13:44:09 +03001436 } else if ((usb_pipeisoc(pipe) || transfer_pending) && dma) {
Sergei Shtylyov6b6e9712009-03-26 18:29:19 -07001437 if (musb_tx_dma_program(musb->dma_controller, hw_ep, qh, urb,
Ajay Kumar Guptadfeffa52009-11-17 15:22:55 +05301438 offset, length)) {
1439 if (is_cppi_enabled() || tusb_dma_omap())
1440 musb_h_tx_dma_start(hw_ep);
Sergei Shtylyov6b6e9712009-03-26 18:29:19 -07001441 return;
Ajay Kumar Guptadfeffa52009-11-17 15:22:55 +05301442 }
Sergei Shtylyov6b6e9712009-03-26 18:29:19 -07001443 } else if (tx_csr & MUSB_TXCSR_DMAENAB) {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001444 dev_dbg(musb->controller, "not complete, but DMA enabled?\n");
Sergei Shtylyov6b6e9712009-03-26 18:29:19 -07001445 return;
1446 }
Felipe Balbi550a7372008-07-24 12:27:36 +03001447
Sergei Shtylyov6b6e9712009-03-26 18:29:19 -07001448 /*
1449 * PIO: start next packet in this URB.
1450 *
1451 * REVISIT: some docs say that when hw_ep->tx_double_buffered,
1452 * (and presumably, FIFO is not half-full) we should write *two*
1453 * packets before updating TXCSR; other docs disagree...
1454 */
1455 if (length > qh->maxpacket)
1456 length = qh->maxpacket;
Maulik Mankad496dda72010-09-24 13:44:06 +03001457 /* Unmap the buffer so that CPU can use it */
Robert Morellc8cf2032011-01-26 19:06:47 -08001458 usb_hcd_unmap_urb_for_dma(musb_to_hcd(musb), urb);
Virupax Sadashivpetimath8e8a5512012-08-07 14:46:20 +05301459
1460 /*
1461 * We need to map sg if the transfer_buffer is
1462 * NULL.
1463 */
1464 if (!urb->transfer_buffer)
1465 use_sg = true;
1466
1467 if (use_sg) {
1468 /* sg_miter_start is already done in musb_ep_program */
1469 if (!sg_miter_next(&qh->sg_miter)) {
1470 dev_err(musb->controller, "error: sg list empty\n");
1471 sg_miter_stop(&qh->sg_miter);
1472 status = -EINVAL;
1473 goto done;
1474 }
1475 urb->transfer_buffer = qh->sg_miter.addr;
1476 length = min_t(u32, length, qh->sg_miter.length);
1477 musb_write_fifo(hw_ep, length, urb->transfer_buffer);
1478 qh->sg_miter.consumed = length;
1479 sg_miter_stop(&qh->sg_miter);
1480 } else {
1481 musb_write_fifo(hw_ep, length, urb->transfer_buffer + offset);
1482 }
1483
Sergei Shtylyov6b6e9712009-03-26 18:29:19 -07001484 qh->segsize = length;
Felipe Balbi550a7372008-07-24 12:27:36 +03001485
Virupax Sadashivpetimath8e8a5512012-08-07 14:46:20 +05301486 if (use_sg) {
1487 if (offset + length >= urb->transfer_buffer_length)
1488 use_sg = false;
1489 }
1490
Sergei Shtylyov6b6e9712009-03-26 18:29:19 -07001491 musb_ep_select(mbase, epnum);
1492 musb_writew(epio, MUSB_TXCSR,
1493 MUSB_TXCSR_H_WZC_BITS | MUSB_TXCSR_TXPKTRDY);
Felipe Balbi550a7372008-07-24 12:27:36 +03001494}
1495
1496
1497#ifdef CONFIG_USB_INVENTRA_DMA
1498
1499/* Host side RX (IN) using Mentor DMA works as follows:
1500 submit_urb ->
1501 - if queue was empty, ProgramEndpoint
1502 - first IN token is sent out (by setting ReqPkt)
1503 LinuxIsr -> RxReady()
1504 /\ => first packet is received
1505 | - Set in mode 0 (DmaEnab, ~ReqPkt)
1506 | -> DMA Isr (transfer complete) -> RxReady()
1507 | - Ack receive (~RxPktRdy), turn off DMA (~DmaEnab)
1508 | - if urb not complete, send next IN token (ReqPkt)
1509 | | else complete urb.
1510 | |
1511 ---------------------------
1512 *
1513 * Nuances of mode 1:
1514 * For short packets, no ack (+RxPktRdy) is sent automatically
1515 * (even if AutoClear is ON)
1516 * For full packets, ack (~RxPktRdy) and next IN token (+ReqPkt) is sent
1517 * automatically => major problem, as collecting the next packet becomes
1518 * difficult. Hence mode 1 is not used.
1519 *
1520 * REVISIT
1521 * All we care about at this driver level is that
1522 * (a) all URBs terminate with REQPKT cleared and fifo(s) empty;
1523 * (b) termination conditions are: short RX, or buffer full;
1524 * (c) fault modes include
1525 * - iff URB_SHORT_NOT_OK, short RX status is -EREMOTEIO.
1526 * (and that endpoint's dma queue stops immediately)
1527 * - overflow (full, PLUS more bytes in the terminal packet)
1528 *
1529 * So for example, usb-storage sets URB_SHORT_NOT_OK, and would
1530 * thus be a great candidate for using mode 1 ... for all but the
1531 * last packet of one URB's transfer.
1532 */
1533
1534#endif
1535
1536/*
1537 * Service an RX interrupt for the given IN endpoint; docs cover bulk, iso,
1538 * and high-bandwidth IN transfer cases.
1539 */
1540void musb_host_rx(struct musb *musb, u8 epnum)
1541{
1542 struct urb *urb;
1543 struct musb_hw_ep *hw_ep = musb->endpoints + epnum;
1544 void __iomem *epio = hw_ep->regs;
1545 struct musb_qh *qh = hw_ep->in_qh;
1546 size_t xfer_len;
1547 void __iomem *mbase = musb->mregs;
1548 int pipe;
1549 u16 rx_csr, val;
1550 bool iso_err = false;
1551 bool done = false;
1552 u32 status;
1553 struct dma_channel *dma;
Virupax Sadashivpetimath8e8a5512012-08-07 14:46:20 +05301554 static bool use_sg;
1555 unsigned int sg_flags = SG_MITER_ATOMIC | SG_MITER_TO_SG;
Felipe Balbi550a7372008-07-24 12:27:36 +03001556
1557 musb_ep_select(mbase, epnum);
1558
1559 urb = next_urb(qh);
1560 dma = is_dma_capable() ? hw_ep->rx_channel : NULL;
1561 status = 0;
1562 xfer_len = 0;
1563
1564 rx_csr = musb_readw(epio, MUSB_RXCSR);
1565 val = rx_csr;
1566
1567 if (unlikely(!urb)) {
1568 /* REVISIT -- THIS SHOULD NEVER HAPPEN ... but, at least
1569 * usbtest #11 (unlinks) triggers it regularly, sometimes
1570 * with fifo full. (Only with DMA??)
1571 */
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001572 dev_dbg(musb->controller, "BOGUS RX%d ready, csr %04x, count %d\n", epnum, val,
Felipe Balbi550a7372008-07-24 12:27:36 +03001573 musb_readw(epio, MUSB_RXCOUNT));
1574 musb_h_flush_rxfifo(hw_ep, MUSB_RXCSR_CLRDATATOG);
1575 return;
1576 }
1577
1578 pipe = urb->pipe;
1579
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001580 dev_dbg(musb->controller, "<== hw %d rxcsr %04x, urb actual %d (+dma %zu)\n",
Felipe Balbi550a7372008-07-24 12:27:36 +03001581 epnum, rx_csr, urb->actual_length,
1582 dma ? dma->actual_len : 0);
1583
1584 /* check for errors, concurrent stall & unlink is not really
1585 * handled yet! */
1586 if (rx_csr & MUSB_RXCSR_H_RXSTALL) {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001587 dev_dbg(musb->controller, "RX end %d STALL\n", epnum);
Felipe Balbi550a7372008-07-24 12:27:36 +03001588
1589 /* stall; record URB status */
1590 status = -EPIPE;
1591
1592 } else if (rx_csr & MUSB_RXCSR_H_ERROR) {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001593 dev_dbg(musb->controller, "end %d RX proto error\n", epnum);
Felipe Balbi550a7372008-07-24 12:27:36 +03001594
1595 status = -EPROTO;
1596 musb_writeb(epio, MUSB_RXINTERVAL, 0);
1597
1598 } else if (rx_csr & MUSB_RXCSR_DATAERROR) {
1599
1600 if (USB_ENDPOINT_XFER_ISOC != qh->type) {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001601 dev_dbg(musb->controller, "RX end %d NAK timeout\n", epnum);
Ajay Kumar Gupta1e0320f2009-02-24 15:26:13 -08001602
1603 /* NOTE: NAKing is *NOT* an error, so we want to
1604 * continue. Except ... if there's a request for
1605 * another QH, use that instead of starving it.
1606 *
1607 * Devices like Ethernet and serial adapters keep
1608 * reads posted at all times, which will starve
1609 * other devices without this logic.
1610 */
1611 if (usb_pipebulk(urb->pipe)
1612 && qh->mux == 1
1613 && !list_is_singular(&musb->in_bulk)) {
Ajay Kumar Guptaf2838622012-07-19 13:41:59 +05301614 musb_bulk_nak_timeout(musb, hw_ep, 1);
Ajay Kumar Gupta1e0320f2009-02-24 15:26:13 -08001615 return;
1616 }
Felipe Balbi550a7372008-07-24 12:27:36 +03001617 musb_ep_select(mbase, epnum);
Ajay Kumar Gupta1e0320f2009-02-24 15:26:13 -08001618 rx_csr |= MUSB_RXCSR_H_WZC_BITS;
1619 rx_csr &= ~MUSB_RXCSR_DATAERROR;
1620 musb_writew(epio, MUSB_RXCSR, rx_csr);
Felipe Balbi550a7372008-07-24 12:27:36 +03001621
1622 goto finish;
1623 } else {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001624 dev_dbg(musb->controller, "RX end %d ISO data error\n", epnum);
Felipe Balbi550a7372008-07-24 12:27:36 +03001625 /* packet error reported later */
1626 iso_err = true;
1627 }
Ajay Kumar Guptaa483d702009-04-03 16:16:17 -07001628 } else if (rx_csr & MUSB_RXCSR_INCOMPRX) {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001629 dev_dbg(musb->controller, "end %d high bandwidth incomplete ISO packet RX\n",
Ajay Kumar Guptaa483d702009-04-03 16:16:17 -07001630 epnum);
1631 status = -EPROTO;
Felipe Balbi550a7372008-07-24 12:27:36 +03001632 }
1633
1634 /* faults abort the transfer */
1635 if (status) {
1636 /* clean up dma and collect transfer count */
1637 if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) {
1638 dma->status = MUSB_DMA_STATUS_CORE_ABORT;
1639 (void) musb->dma_controller->channel_abort(dma);
1640 xfer_len = dma->actual_len;
1641 }
1642 musb_h_flush_rxfifo(hw_ep, MUSB_RXCSR_CLRDATATOG);
1643 musb_writeb(epio, MUSB_RXINTERVAL, 0);
1644 done = true;
1645 goto finish;
1646 }
1647
1648 if (unlikely(dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY)) {
1649 /* SHOULD NEVER HAPPEN ... but at least DaVinci has done it */
1650 ERR("RX%d dma busy, csr %04x\n", epnum, rx_csr);
1651 goto finish;
1652 }
1653
1654 /* thorough shutdown for now ... given more precise fault handling
1655 * and better queueing support, we might keep a DMA pipeline going
1656 * while processing this irq for earlier completions.
1657 */
1658
1659 /* FIXME this is _way_ too much in-line logic for Mentor DMA */
1660
1661#ifndef CONFIG_USB_INVENTRA_DMA
1662 if (rx_csr & MUSB_RXCSR_H_REQPKT) {
1663 /* REVISIT this happened for a while on some short reads...
1664 * the cleanup still needs investigation... looks bad...
1665 * and also duplicates dma cleanup code above ... plus,
1666 * shouldn't this be the "half full" double buffer case?
1667 */
1668 if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) {
1669 dma->status = MUSB_DMA_STATUS_CORE_ABORT;
1670 (void) musb->dma_controller->channel_abort(dma);
1671 xfer_len = dma->actual_len;
1672 done = true;
1673 }
1674
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001675 dev_dbg(musb->controller, "RXCSR%d %04x, reqpkt, len %zu%s\n", epnum, rx_csr,
Felipe Balbi550a7372008-07-24 12:27:36 +03001676 xfer_len, dma ? ", dma" : "");
1677 rx_csr &= ~MUSB_RXCSR_H_REQPKT;
1678
1679 musb_ep_select(mbase, epnum);
1680 musb_writew(epio, MUSB_RXCSR,
1681 MUSB_RXCSR_H_WZC_BITS | rx_csr);
1682 }
1683#endif
1684 if (dma && (rx_csr & MUSB_RXCSR_DMAENAB)) {
1685 xfer_len = dma->actual_len;
1686
1687 val &= ~(MUSB_RXCSR_DMAENAB
1688 | MUSB_RXCSR_H_AUTOREQ
1689 | MUSB_RXCSR_AUTOCLEAR
1690 | MUSB_RXCSR_RXPKTRDY);
1691 musb_writew(hw_ep->regs, MUSB_RXCSR, val);
1692
1693#ifdef CONFIG_USB_INVENTRA_DMA
Ajay Kumar Guptaf82a6892008-10-29 15:10:31 +02001694 if (usb_pipeisoc(pipe)) {
1695 struct usb_iso_packet_descriptor *d;
1696
1697 d = urb->iso_frame_desc + qh->iso_idx;
1698 d->actual_length = xfer_len;
1699
1700 /* even if there was an error, we did the dma
1701 * for iso_frame_desc->length
1702 */
Márton Németh72887c82011-05-30 20:45:42 +02001703 if (d->status != -EILSEQ && d->status != -EOVERFLOW)
Ajay Kumar Guptaf82a6892008-10-29 15:10:31 +02001704 d->status = 0;
1705
1706 if (++qh->iso_idx >= urb->number_of_packets)
1707 done = true;
1708 else
1709 done = false;
1710
1711 } else {
Felipe Balbi550a7372008-07-24 12:27:36 +03001712 /* done if urb buffer is full or short packet is recd */
1713 done = (urb->actual_length + xfer_len >=
1714 urb->transfer_buffer_length
1715 || dma->actual_len < qh->maxpacket);
Ajay Kumar Guptaf82a6892008-10-29 15:10:31 +02001716 }
Felipe Balbi550a7372008-07-24 12:27:36 +03001717
1718 /* send IN token for next packet, without AUTOREQ */
1719 if (!done) {
1720 val |= MUSB_RXCSR_H_REQPKT;
1721 musb_writew(epio, MUSB_RXCSR,
1722 MUSB_RXCSR_H_WZC_BITS | val);
1723 }
1724
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001725 dev_dbg(musb->controller, "ep %d dma %s, rxcsr %04x, rxcount %d\n", epnum,
Felipe Balbi550a7372008-07-24 12:27:36 +03001726 done ? "off" : "reset",
1727 musb_readw(epio, MUSB_RXCSR),
1728 musb_readw(epio, MUSB_RXCOUNT));
1729#else
1730 done = true;
1731#endif
1732 } else if (urb->status == -EINPROGRESS) {
1733 /* if no errors, be sure a packet is ready for unloading */
1734 if (unlikely(!(rx_csr & MUSB_RXCSR_RXPKTRDY))) {
1735 status = -EPROTO;
1736 ERR("Rx interrupt with no errors or packet!\n");
1737
1738 /* FIXME this is another "SHOULD NEVER HAPPEN" */
1739
1740/* SCRUB (RX) */
1741 /* do the proper sequence to abort the transfer */
1742 musb_ep_select(mbase, epnum);
1743 val &= ~MUSB_RXCSR_H_REQPKT;
1744 musb_writew(epio, MUSB_RXCSR, val);
1745 goto finish;
1746 }
1747
1748 /* we are expecting IN packets */
1749#ifdef CONFIG_USB_INVENTRA_DMA
1750 if (dma) {
1751 struct dma_controller *c;
1752 u16 rx_count;
Ajay Kumar Guptaf82a6892008-10-29 15:10:31 +02001753 int ret, length;
1754 dma_addr_t buf;
Felipe Balbi550a7372008-07-24 12:27:36 +03001755
1756 rx_count = musb_readw(epio, MUSB_RXCOUNT);
1757
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001758 dev_dbg(musb->controller, "RX%d count %d, buffer 0x%x len %d/%d\n",
Felipe Balbi550a7372008-07-24 12:27:36 +03001759 epnum, rx_count,
1760 urb->transfer_dma
1761 + urb->actual_length,
1762 qh->offset,
1763 urb->transfer_buffer_length);
1764
1765 c = musb->dma_controller;
1766
Ajay Kumar Guptaf82a6892008-10-29 15:10:31 +02001767 if (usb_pipeisoc(pipe)) {
Felipe Balbi8b4959d2009-12-04 15:47:47 +02001768 int d_status = 0;
Ajay Kumar Guptaf82a6892008-10-29 15:10:31 +02001769 struct usb_iso_packet_descriptor *d;
1770
1771 d = urb->iso_frame_desc + qh->iso_idx;
1772
1773 if (iso_err) {
Felipe Balbi8b4959d2009-12-04 15:47:47 +02001774 d_status = -EILSEQ;
Ajay Kumar Guptaf82a6892008-10-29 15:10:31 +02001775 urb->error_count++;
1776 }
1777 if (rx_count > d->length) {
Felipe Balbi8b4959d2009-12-04 15:47:47 +02001778 if (d_status == 0) {
1779 d_status = -EOVERFLOW;
Ajay Kumar Guptaf82a6892008-10-29 15:10:31 +02001780 urb->error_count++;
1781 }
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001782 dev_dbg(musb->controller, "** OVERFLOW %d into %d\n",\
Ajay Kumar Guptaf82a6892008-10-29 15:10:31 +02001783 rx_count, d->length);
1784
1785 length = d->length;
1786 } else
1787 length = rx_count;
Felipe Balbi8b4959d2009-12-04 15:47:47 +02001788 d->status = d_status;
Ajay Kumar Guptaf82a6892008-10-29 15:10:31 +02001789 buf = urb->transfer_dma + d->offset;
1790 } else {
1791 length = rx_count;
1792 buf = urb->transfer_dma +
1793 urb->actual_length;
1794 }
1795
Felipe Balbi550a7372008-07-24 12:27:36 +03001796 dma->desired_mode = 0;
1797#ifdef USE_MODE1
1798 /* because of the issue below, mode 1 will
1799 * only rarely behave with correct semantics.
1800 */
1801 if ((urb->transfer_flags &
1802 URB_SHORT_NOT_OK)
1803 && (urb->transfer_buffer_length -
1804 urb->actual_length)
1805 > qh->maxpacket)
1806 dma->desired_mode = 1;
Ajay Kumar Guptaf82a6892008-10-29 15:10:31 +02001807 if (rx_count < hw_ep->max_packet_sz_rx) {
1808 length = rx_count;
Sonic Zhangae926972010-03-08 11:26:01 -05001809 dma->desired_mode = 0;
Ajay Kumar Guptaf82a6892008-10-29 15:10:31 +02001810 } else {
1811 length = urb->transfer_buffer_length;
1812 }
Felipe Balbi550a7372008-07-24 12:27:36 +03001813#endif
1814
1815/* Disadvantage of using mode 1:
1816 * It's basically usable only for mass storage class; essentially all
1817 * other protocols also terminate transfers on short packets.
1818 *
1819 * Details:
1820 * An extra IN token is sent at the end of the transfer (due to AUTOREQ)
1821 * If you try to use mode 1 for (transfer_buffer_length - 512), and try
1822 * to use the extra IN token to grab the last packet using mode 0, then
1823 * the problem is that you cannot be sure when the device will send the
1824 * last packet and RxPktRdy set. Sometimes the packet is recd too soon
1825 * such that it gets lost when RxCSR is re-set at the end of the mode 1
1826 * transfer, while sometimes it is recd just a little late so that if you
1827 * try to configure for mode 0 soon after the mode 1 transfer is
1828 * completed, you will find rxcount 0. Okay, so you might think why not
1829 * wait for an interrupt when the pkt is recd. Well, you won't get any!
1830 */
1831
1832 val = musb_readw(epio, MUSB_RXCSR);
1833 val &= ~MUSB_RXCSR_H_REQPKT;
1834
1835 if (dma->desired_mode == 0)
1836 val &= ~MUSB_RXCSR_H_AUTOREQ;
1837 else
1838 val |= MUSB_RXCSR_H_AUTOREQ;
Ajay Kumar Guptaa483d702009-04-03 16:16:17 -07001839 val |= MUSB_RXCSR_DMAENAB;
1840
1841 /* autoclear shouldn't be set in high bandwidth */
1842 if (qh->hb_mult == 1)
1843 val |= MUSB_RXCSR_AUTOCLEAR;
Felipe Balbi550a7372008-07-24 12:27:36 +03001844
1845 musb_writew(epio, MUSB_RXCSR,
1846 MUSB_RXCSR_H_WZC_BITS | val);
1847
1848 /* REVISIT if when actual_length != 0,
1849 * transfer_buffer_length needs to be
1850 * adjusted first...
1851 */
1852 ret = c->channel_program(
1853 dma, qh->maxpacket,
Ajay Kumar Guptaf82a6892008-10-29 15:10:31 +02001854 dma->desired_mode, buf, length);
Felipe Balbi550a7372008-07-24 12:27:36 +03001855
1856 if (!ret) {
1857 c->channel_release(dma);
1858 hw_ep->rx_channel = NULL;
1859 dma = NULL;
Mantesh Sarasetti2ed91272012-06-01 14:54:30 +03001860 val = musb_readw(epio, MUSB_RXCSR);
1861 val &= ~(MUSB_RXCSR_DMAENAB
1862 | MUSB_RXCSR_H_AUTOREQ
1863 | MUSB_RXCSR_AUTOCLEAR);
1864 musb_writew(epio, MUSB_RXCSR, val);
Felipe Balbi550a7372008-07-24 12:27:36 +03001865 }
1866 }
1867#endif /* Mentor DMA */
1868
1869 if (!dma) {
Virupax Sadashivpetimath8e8a5512012-08-07 14:46:20 +05301870 unsigned int received_len;
1871
Maulik Mankad496dda72010-09-24 13:44:06 +03001872 /* Unmap the buffer so that CPU can use it */
Robert Morellc8cf2032011-01-26 19:06:47 -08001873 usb_hcd_unmap_urb_for_dma(musb_to_hcd(musb), urb);
Virupax Sadashivpetimath8e8a5512012-08-07 14:46:20 +05301874
1875 /*
1876 * We need to map sg if the transfer_buffer is
1877 * NULL.
1878 */
1879 if (!urb->transfer_buffer) {
1880 use_sg = true;
1881 sg_miter_start(&qh->sg_miter, urb->sg, 1,
1882 sg_flags);
1883 }
1884
1885 if (use_sg) {
1886 if (!sg_miter_next(&qh->sg_miter)) {
1887 dev_err(musb->controller, "error: sg list empty\n");
1888 sg_miter_stop(&qh->sg_miter);
1889 status = -EINVAL;
1890 done = true;
1891 goto finish;
1892 }
1893 urb->transfer_buffer = qh->sg_miter.addr;
1894 received_len = urb->actual_length;
1895 qh->offset = 0x0;
1896 done = musb_host_packet_rx(musb, urb, epnum,
1897 iso_err);
1898 /* Calculate the number of bytes received */
1899 received_len = urb->actual_length -
1900 received_len;
1901 qh->sg_miter.consumed = received_len;
1902 sg_miter_stop(&qh->sg_miter);
1903 } else {
1904 done = musb_host_packet_rx(musb, urb,
1905 epnum, iso_err);
1906 }
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001907 dev_dbg(musb->controller, "read %spacket\n", done ? "last " : "");
Felipe Balbi550a7372008-07-24 12:27:36 +03001908 }
1909 }
1910
Felipe Balbi550a7372008-07-24 12:27:36 +03001911finish:
1912 urb->actual_length += xfer_len;
1913 qh->offset += xfer_len;
1914 if (done) {
Virupax Sadashivpetimath8e8a5512012-08-07 14:46:20 +05301915 if (use_sg)
1916 use_sg = false;
1917
Felipe Balbi550a7372008-07-24 12:27:36 +03001918 if (urb->status == -EINPROGRESS)
1919 urb->status = status;
1920 musb_advance_schedule(musb, urb, hw_ep, USB_DIR_IN);
1921 }
1922}
1923
1924/* schedule nodes correspond to peripheral endpoints, like an OHCI QH.
1925 * the software schedule associates multiple such nodes with a given
1926 * host side hardware endpoint + direction; scheduling may activate
1927 * that hardware endpoint.
1928 */
1929static int musb_schedule(
1930 struct musb *musb,
1931 struct musb_qh *qh,
1932 int is_in)
1933{
1934 int idle;
1935 int best_diff;
1936 int best_end, epnum;
1937 struct musb_hw_ep *hw_ep = NULL;
1938 struct list_head *head = NULL;
Swaminathan S5274dab2009-12-28 13:40:37 +02001939 u8 toggle;
1940 u8 txtype;
1941 struct urb *urb = next_urb(qh);
Felipe Balbi550a7372008-07-24 12:27:36 +03001942
1943 /* use fixed hardware for control and bulk */
Ajay Kumar Gupta23d15e02008-10-29 15:10:35 +02001944 if (qh->type == USB_ENDPOINT_XFER_CONTROL) {
Felipe Balbi550a7372008-07-24 12:27:36 +03001945 head = &musb->control;
1946 hw_ep = musb->control_ep;
Felipe Balbi550a7372008-07-24 12:27:36 +03001947 goto success;
1948 }
1949
1950 /* else, periodic transfers get muxed to other endpoints */
1951
Sergei Shtylyov5d67a852009-02-24 15:23:34 -08001952 /*
1953 * We know this qh hasn't been scheduled, so all we need to do
Felipe Balbi550a7372008-07-24 12:27:36 +03001954 * is choose which hardware endpoint to put it on ...
1955 *
1956 * REVISIT what we really want here is a regular schedule tree
Sergei Shtylyov5d67a852009-02-24 15:23:34 -08001957 * like e.g. OHCI uses.
Felipe Balbi550a7372008-07-24 12:27:36 +03001958 */
1959 best_diff = 4096;
1960 best_end = -1;
1961
Sergei Shtylyov5d67a852009-02-24 15:23:34 -08001962 for (epnum = 1, hw_ep = musb->endpoints + 1;
1963 epnum < musb->nr_endpoints;
1964 epnum++, hw_ep++) {
Felipe Balbi550a7372008-07-24 12:27:36 +03001965 int diff;
1966
Sergei Shtylyov3e5c6dc2009-03-27 12:55:16 -07001967 if (musb_ep_get_qh(hw_ep, is_in) != NULL)
Felipe Balbi550a7372008-07-24 12:27:36 +03001968 continue;
Sergei Shtylyov5d67a852009-02-24 15:23:34 -08001969
Felipe Balbi550a7372008-07-24 12:27:36 +03001970 if (hw_ep == musb->bulk_ep)
1971 continue;
1972
1973 if (is_in)
Ajay Kumar Guptaa483d702009-04-03 16:16:17 -07001974 diff = hw_ep->max_packet_sz_rx;
Felipe Balbi550a7372008-07-24 12:27:36 +03001975 else
Ajay Kumar Guptaa483d702009-04-03 16:16:17 -07001976 diff = hw_ep->max_packet_sz_tx;
1977 diff -= (qh->maxpacket * qh->hb_mult);
Felipe Balbi550a7372008-07-24 12:27:36 +03001978
Ajay Kumar Gupta23d15e02008-10-29 15:10:35 +02001979 if (diff >= 0 && best_diff > diff) {
Swaminathan S5274dab2009-12-28 13:40:37 +02001980
1981 /*
1982 * Mentor controller has a bug in that if we schedule
1983 * a BULK Tx transfer on an endpoint that had earlier
1984 * handled ISOC then the BULK transfer has to start on
1985 * a zero toggle. If the BULK transfer starts on a 1
1986 * toggle then this transfer will fail as the mentor
1987 * controller starts the Bulk transfer on a 0 toggle
1988 * irrespective of the programming of the toggle bits
1989 * in the TXCSR register. Check for this condition
1990 * while allocating the EP for a Tx Bulk transfer. If
1991 * so skip this EP.
1992 */
1993 hw_ep = musb->endpoints + epnum;
1994 toggle = usb_gettoggle(urb->dev, qh->epnum, !is_in);
1995 txtype = (musb_readb(hw_ep->regs, MUSB_TXTYPE)
1996 >> 4) & 0x3;
1997 if (!is_in && (qh->type == USB_ENDPOINT_XFER_BULK) &&
1998 toggle && (txtype == USB_ENDPOINT_XFER_ISOC))
1999 continue;
2000
Felipe Balbi550a7372008-07-24 12:27:36 +03002001 best_diff = diff;
2002 best_end = epnum;
2003 }
2004 }
Ajay Kumar Gupta23d15e02008-10-29 15:10:35 +02002005 /* use bulk reserved ep1 if no other ep is free */
Felipe Balbiaa5cbbe2008-11-17 09:08:16 +02002006 if (best_end < 0 && qh->type == USB_ENDPOINT_XFER_BULK) {
Ajay Kumar Gupta23d15e02008-10-29 15:10:35 +02002007 hw_ep = musb->bulk_ep;
2008 if (is_in)
2009 head = &musb->in_bulk;
2010 else
2011 head = &musb->out_bulk;
Ajay Kumar Gupta1e0320f2009-02-24 15:26:13 -08002012
Ajay Kumar Guptaf2838622012-07-19 13:41:59 +05302013 /* Enable bulk RX/TX NAK timeout scheme when bulk requests are
Ajay Kumar Gupta1e0320f2009-02-24 15:26:13 -08002014 * multiplexed. This scheme doen't work in high speed to full
2015 * speed scenario as NAK interrupts are not coming from a
2016 * full speed device connected to a high speed device.
2017 * NAK timeout interval is 8 (128 uframe or 16ms) for HS and
2018 * 4 (8 frame or 8ms) for FS device.
2019 */
Ajay Kumar Guptaf2838622012-07-19 13:41:59 +05302020 if (qh->dev)
Ajay Kumar Gupta1e0320f2009-02-24 15:26:13 -08002021 qh->intv_reg =
2022 (USB_SPEED_HIGH == qh->dev->speed) ? 8 : 4;
Ajay Kumar Gupta23d15e02008-10-29 15:10:35 +02002023 goto success;
2024 } else if (best_end < 0) {
Felipe Balbi550a7372008-07-24 12:27:36 +03002025 return -ENOSPC;
Ajay Kumar Gupta23d15e02008-10-29 15:10:35 +02002026 }
Felipe Balbi550a7372008-07-24 12:27:36 +03002027
2028 idle = 1;
Ajay Kumar Gupta23d15e02008-10-29 15:10:35 +02002029 qh->mux = 0;
Felipe Balbi550a7372008-07-24 12:27:36 +03002030 hw_ep = musb->endpoints + best_end;
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03002031 dev_dbg(musb->controller, "qh %p periodic slot %d\n", qh, best_end);
Felipe Balbi550a7372008-07-24 12:27:36 +03002032success:
Ajay Kumar Gupta23d15e02008-10-29 15:10:35 +02002033 if (head) {
2034 idle = list_empty(head);
2035 list_add_tail(&qh->ring, head);
2036 qh->mux = 1;
2037 }
Felipe Balbi550a7372008-07-24 12:27:36 +03002038 qh->hw_ep = hw_ep;
2039 qh->hep->hcpriv = qh;
2040 if (idle)
2041 musb_start_urb(musb, is_in, qh);
2042 return 0;
2043}
2044
2045static int musb_urb_enqueue(
2046 struct usb_hcd *hcd,
2047 struct urb *urb,
2048 gfp_t mem_flags)
2049{
2050 unsigned long flags;
2051 struct musb *musb = hcd_to_musb(hcd);
2052 struct usb_host_endpoint *hep = urb->ep;
David Brownell74bb3502009-03-26 17:36:57 -07002053 struct musb_qh *qh;
Felipe Balbi550a7372008-07-24 12:27:36 +03002054 struct usb_endpoint_descriptor *epd = &hep->desc;
2055 int ret;
2056 unsigned type_reg;
2057 unsigned interval;
2058
2059 /* host role must be active */
2060 if (!is_host_active(musb) || !musb->is_active)
2061 return -ENODEV;
2062
2063 spin_lock_irqsave(&musb->lock, flags);
2064 ret = usb_hcd_link_urb_to_ep(hcd, urb);
David Brownell74bb3502009-03-26 17:36:57 -07002065 qh = ret ? NULL : hep->hcpriv;
2066 if (qh)
2067 urb->hcpriv = qh;
Felipe Balbi550a7372008-07-24 12:27:36 +03002068 spin_unlock_irqrestore(&musb->lock, flags);
Felipe Balbi550a7372008-07-24 12:27:36 +03002069
2070 /* DMA mapping was already done, if needed, and this urb is on
David Brownell74bb3502009-03-26 17:36:57 -07002071 * hep->urb_list now ... so we're done, unless hep wasn't yet
2072 * scheduled onto a live qh.
Felipe Balbi550a7372008-07-24 12:27:36 +03002073 *
2074 * REVISIT best to keep hep->hcpriv valid until the endpoint gets
2075 * disabled, testing for empty qh->ring and avoiding qh setup costs
2076 * except for the first urb queued after a config change.
2077 */
David Brownell74bb3502009-03-26 17:36:57 -07002078 if (qh || ret)
2079 return ret;
Felipe Balbi550a7372008-07-24 12:27:36 +03002080
2081 /* Allocate and initialize qh, minimizing the work done each time
2082 * hw_ep gets reprogrammed, or with irqs blocked. Then schedule it.
2083 *
2084 * REVISIT consider a dedicated qh kmem_cache, so it's harder
2085 * for bugs in other kernel code to break this driver...
2086 */
2087 qh = kzalloc(sizeof *qh, mem_flags);
2088 if (!qh) {
Ajay Kumar Gupta2492e672008-09-11 11:53:21 +03002089 spin_lock_irqsave(&musb->lock, flags);
Felipe Balbi550a7372008-07-24 12:27:36 +03002090 usb_hcd_unlink_urb_from_ep(hcd, urb);
Ajay Kumar Gupta2492e672008-09-11 11:53:21 +03002091 spin_unlock_irqrestore(&musb->lock, flags);
Felipe Balbi550a7372008-07-24 12:27:36 +03002092 return -ENOMEM;
2093 }
2094
2095 qh->hep = hep;
2096 qh->dev = urb->dev;
2097 INIT_LIST_HEAD(&qh->ring);
2098 qh->is_ready = 1;
2099
Kuninori Morimoto29cc8892011-08-23 03:12:03 -07002100 qh->maxpacket = usb_endpoint_maxp(epd);
Ajay Kumar Guptaa483d702009-04-03 16:16:17 -07002101 qh->type = usb_endpoint_type(epd);
Felipe Balbi550a7372008-07-24 12:27:36 +03002102
Ajay Kumar Guptaa483d702009-04-03 16:16:17 -07002103 /* Bits 11 & 12 of wMaxPacketSize encode high bandwidth multiplier.
2104 * Some musb cores don't support high bandwidth ISO transfers; and
2105 * we don't (yet!) support high bandwidth interrupt transfers.
2106 */
2107 qh->hb_mult = 1 + ((qh->maxpacket >> 11) & 0x03);
2108 if (qh->hb_mult > 1) {
2109 int ok = (qh->type == USB_ENDPOINT_XFER_ISOC);
2110
2111 if (ok)
2112 ok = (usb_pipein(urb->pipe) && musb->hb_iso_rx)
2113 || (usb_pipeout(urb->pipe) && musb->hb_iso_tx);
2114 if (!ok) {
2115 ret = -EMSGSIZE;
2116 goto done;
2117 }
2118 qh->maxpacket &= 0x7ff;
Felipe Balbi550a7372008-07-24 12:27:36 +03002119 }
2120
Julia Lawall96bcd092009-01-24 17:57:24 -08002121 qh->epnum = usb_endpoint_num(epd);
Felipe Balbi550a7372008-07-24 12:27:36 +03002122
2123 /* NOTE: urb->dev->devnum is wrong during SET_ADDRESS */
2124 qh->addr_reg = (u8) usb_pipedevice(urb->pipe);
2125
2126 /* precompute rxtype/txtype/type0 register */
2127 type_reg = (qh->type << 4) | qh->epnum;
2128 switch (urb->dev->speed) {
2129 case USB_SPEED_LOW:
2130 type_reg |= 0xc0;
2131 break;
2132 case USB_SPEED_FULL:
2133 type_reg |= 0x80;
2134 break;
2135 default:
2136 type_reg |= 0x40;
2137 }
2138 qh->type_reg = type_reg;
2139
Sergei Shtylyov136733d2009-02-21 15:31:35 -08002140 /* Precompute RXINTERVAL/TXINTERVAL register */
Felipe Balbi550a7372008-07-24 12:27:36 +03002141 switch (qh->type) {
2142 case USB_ENDPOINT_XFER_INT:
Sergei Shtylyov136733d2009-02-21 15:31:35 -08002143 /*
2144 * Full/low speeds use the linear encoding,
2145 * high speed uses the logarithmic encoding.
2146 */
2147 if (urb->dev->speed <= USB_SPEED_FULL) {
2148 interval = max_t(u8, epd->bInterval, 1);
2149 break;
Felipe Balbi550a7372008-07-24 12:27:36 +03002150 }
2151 /* FALLTHROUGH */
2152 case USB_ENDPOINT_XFER_ISOC:
Sergei Shtylyov136733d2009-02-21 15:31:35 -08002153 /* ISO always uses logarithmic encoding */
2154 interval = min_t(u8, epd->bInterval, 16);
Felipe Balbi550a7372008-07-24 12:27:36 +03002155 break;
2156 default:
2157 /* REVISIT we actually want to use NAK limits, hinting to the
2158 * transfer scheduling logic to try some other qh, e.g. try
2159 * for 2 msec first:
2160 *
2161 * interval = (USB_SPEED_HIGH == urb->dev->speed) ? 16 : 2;
2162 *
2163 * The downside of disabling this is that transfer scheduling
2164 * gets VERY unfair for nonperiodic transfers; a misbehaving
Ajay Kumar Gupta1e0320f2009-02-24 15:26:13 -08002165 * peripheral could make that hurt. That's perfectly normal
2166 * for reads from network or serial adapters ... so we have
2167 * partial NAKlimit support for bulk RX.
Felipe Balbi550a7372008-07-24 12:27:36 +03002168 *
Ajay Kumar Gupta1e0320f2009-02-24 15:26:13 -08002169 * The upside of disabling it is simpler transfer scheduling.
Felipe Balbi550a7372008-07-24 12:27:36 +03002170 */
2171 interval = 0;
2172 }
2173 qh->intv_reg = interval;
2174
2175 /* precompute addressing for external hub/tt ports */
2176 if (musb->is_multipoint) {
2177 struct usb_device *parent = urb->dev->parent;
2178
2179 if (parent != hcd->self.root_hub) {
2180 qh->h_addr_reg = (u8) parent->devnum;
2181
2182 /* set up tt info if needed */
2183 if (urb->dev->tt) {
2184 qh->h_port_reg = (u8) urb->dev->ttport;
Ajay Kumar Guptaae5ad292008-09-11 11:53:20 +03002185 if (urb->dev->tt->hub)
2186 qh->h_addr_reg =
2187 (u8) urb->dev->tt->hub->devnum;
2188 if (urb->dev->tt->multi)
2189 qh->h_addr_reg |= 0x80;
Felipe Balbi550a7372008-07-24 12:27:36 +03002190 }
2191 }
2192 }
2193
2194 /* invariant: hep->hcpriv is null OR the qh that's already scheduled.
2195 * until we get real dma queues (with an entry for each urb/buffer),
2196 * we only have work to do in the former case.
2197 */
2198 spin_lock_irqsave(&musb->lock, flags);
yuzheng ma30677792012-08-15 16:11:40 +08002199 if (hep->hcpriv || !next_urb(qh)) {
Felipe Balbi550a7372008-07-24 12:27:36 +03002200 /* some concurrent activity submitted another urb to hep...
2201 * odd, rare, error prone, but legal.
2202 */
2203 kfree(qh);
Dan Carpenter714bc5e2010-03-25 13:14:27 +02002204 qh = NULL;
Felipe Balbi550a7372008-07-24 12:27:36 +03002205 ret = 0;
2206 } else
2207 ret = musb_schedule(musb, qh,
2208 epd->bEndpointAddress & USB_ENDPOINT_DIR_MASK);
2209
2210 if (ret == 0) {
2211 urb->hcpriv = qh;
2212 /* FIXME set urb->start_frame for iso/intr, it's tested in
2213 * musb_start_urb(), but otherwise only konicawc cares ...
2214 */
2215 }
2216 spin_unlock_irqrestore(&musb->lock, flags);
2217
2218done:
2219 if (ret != 0) {
Ajay Kumar Gupta2492e672008-09-11 11:53:21 +03002220 spin_lock_irqsave(&musb->lock, flags);
Felipe Balbi550a7372008-07-24 12:27:36 +03002221 usb_hcd_unlink_urb_from_ep(hcd, urb);
Ajay Kumar Gupta2492e672008-09-11 11:53:21 +03002222 spin_unlock_irqrestore(&musb->lock, flags);
Felipe Balbi550a7372008-07-24 12:27:36 +03002223 kfree(qh);
2224 }
2225 return ret;
2226}
2227
2228
2229/*
2230 * abort a transfer that's at the head of a hardware queue.
2231 * called with controller locked, irqs blocked
2232 * that hardware queue advances to the next transfer, unless prevented
2233 */
Sergei Shtylyov81ec4e42009-03-27 12:57:50 -07002234static int musb_cleanup_urb(struct urb *urb, struct musb_qh *qh)
Felipe Balbi550a7372008-07-24 12:27:36 +03002235{
2236 struct musb_hw_ep *ep = qh->hw_ep;
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03002237 struct musb *musb = ep->musb;
Felipe Balbi550a7372008-07-24 12:27:36 +03002238 void __iomem *epio = ep->regs;
2239 unsigned hw_end = ep->epnum;
2240 void __iomem *regs = ep->musb->mregs;
Sergei Shtylyov81ec4e42009-03-27 12:57:50 -07002241 int is_in = usb_pipein(urb->pipe);
Felipe Balbi550a7372008-07-24 12:27:36 +03002242 int status = 0;
Sergei Shtylyov81ec4e42009-03-27 12:57:50 -07002243 u16 csr;
Felipe Balbi550a7372008-07-24 12:27:36 +03002244
2245 musb_ep_select(regs, hw_end);
2246
2247 if (is_dma_capable()) {
2248 struct dma_channel *dma;
2249
2250 dma = is_in ? ep->rx_channel : ep->tx_channel;
2251 if (dma) {
2252 status = ep->musb->dma_controller->channel_abort(dma);
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03002253 dev_dbg(musb->controller,
Felipe Balbi550a7372008-07-24 12:27:36 +03002254 "abort %cX%d DMA for urb %p --> %d\n",
2255 is_in ? 'R' : 'T', ep->epnum,
2256 urb, status);
2257 urb->actual_length += dma->actual_len;
2258 }
2259 }
2260
2261 /* turn off DMA requests, discard state, stop polling ... */
Ajay Kumar Gupta692933b2012-03-14 17:33:35 +05302262 if (ep->epnum && is_in) {
Felipe Balbi550a7372008-07-24 12:27:36 +03002263 /* giveback saves bulk toggle */
2264 csr = musb_h_flush_rxfifo(ep, 0);
2265
2266 /* REVISIT we still get an irq; should likely clear the
2267 * endpoint's irq status here to avoid bogus irqs.
2268 * clearing that status is platform-specific...
2269 */
David Brownell78322c12009-03-26 17:38:30 -07002270 } else if (ep->epnum) {
Felipe Balbi550a7372008-07-24 12:27:36 +03002271 musb_h_tx_flush_fifo(ep);
2272 csr = musb_readw(epio, MUSB_TXCSR);
2273 csr &= ~(MUSB_TXCSR_AUTOSET
2274 | MUSB_TXCSR_DMAENAB
2275 | MUSB_TXCSR_H_RXSTALL
2276 | MUSB_TXCSR_H_NAKTIMEOUT
2277 | MUSB_TXCSR_H_ERROR
2278 | MUSB_TXCSR_TXPKTRDY);
2279 musb_writew(epio, MUSB_TXCSR, csr);
2280 /* REVISIT may need to clear FLUSHFIFO ... */
2281 musb_writew(epio, MUSB_TXCSR, csr);
2282 /* flush cpu writebuffer */
2283 csr = musb_readw(epio, MUSB_TXCSR);
David Brownell78322c12009-03-26 17:38:30 -07002284 } else {
2285 musb_h_ep0_flush_fifo(ep);
Felipe Balbi550a7372008-07-24 12:27:36 +03002286 }
2287 if (status == 0)
2288 musb_advance_schedule(ep->musb, urb, ep, is_in);
2289 return status;
2290}
2291
2292static int musb_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
2293{
2294 struct musb *musb = hcd_to_musb(hcd);
2295 struct musb_qh *qh;
Felipe Balbi550a7372008-07-24 12:27:36 +03002296 unsigned long flags;
Sergei Shtylyov22a0d6f2009-03-27 12:56:26 -07002297 int is_in = usb_pipein(urb->pipe);
Felipe Balbi550a7372008-07-24 12:27:36 +03002298 int ret;
2299
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03002300 dev_dbg(musb->controller, "urb=%p, dev%d ep%d%s\n", urb,
Felipe Balbi550a7372008-07-24 12:27:36 +03002301 usb_pipedevice(urb->pipe),
2302 usb_pipeendpoint(urb->pipe),
Sergei Shtylyov22a0d6f2009-03-27 12:56:26 -07002303 is_in ? "in" : "out");
Felipe Balbi550a7372008-07-24 12:27:36 +03002304
2305 spin_lock_irqsave(&musb->lock, flags);
2306 ret = usb_hcd_check_unlink_urb(hcd, urb, status);
2307 if (ret)
2308 goto done;
2309
2310 qh = urb->hcpriv;
2311 if (!qh)
2312 goto done;
2313
Sergei Shtylyov22a0d6f2009-03-27 12:56:26 -07002314 /*
2315 * Any URB not actively programmed into endpoint hardware can be
Sergei Shtylyova2fd8142009-02-21 15:30:45 -08002316 * immediately given back; that's any URB not at the head of an
Felipe Balbi550a7372008-07-24 12:27:36 +03002317 * endpoint queue, unless someday we get real DMA queues. And even
Sergei Shtylyova2fd8142009-02-21 15:30:45 -08002318 * if it's at the head, it might not be known to the hardware...
Felipe Balbi550a7372008-07-24 12:27:36 +03002319 *
Sergei Shtylyov22a0d6f2009-03-27 12:56:26 -07002320 * Otherwise abort current transfer, pending DMA, etc.; urb->status
Felipe Balbi550a7372008-07-24 12:27:36 +03002321 * has already been updated. This is a synchronous abort; it'd be
2322 * OK to hold off until after some IRQ, though.
Sergei Shtylyov22a0d6f2009-03-27 12:56:26 -07002323 *
2324 * NOTE: qh is invalid unless !list_empty(&hep->urb_list)
Felipe Balbi550a7372008-07-24 12:27:36 +03002325 */
Sergei Shtylyov22a0d6f2009-03-27 12:56:26 -07002326 if (!qh->is_ready
2327 || urb->urb_list.prev != &qh->hep->urb_list
2328 || musb_ep_get_qh(qh->hw_ep, is_in) != qh) {
Felipe Balbi550a7372008-07-24 12:27:36 +03002329 int ready = qh->is_ready;
2330
Felipe Balbi550a7372008-07-24 12:27:36 +03002331 qh->is_ready = 0;
Sergei Shtylyovc9cd06b2009-03-27 12:58:31 -07002332 musb_giveback(musb, urb, 0);
Felipe Balbi550a7372008-07-24 12:27:36 +03002333 qh->is_ready = ready;
Sergei Shtylyova2fd8142009-02-21 15:30:45 -08002334
2335 /* If nothing else (usually musb_giveback) is using it
2336 * and its URB list has emptied, recycle this qh.
2337 */
2338 if (ready && list_empty(&qh->hep->urb_list)) {
2339 qh->hep->hcpriv = NULL;
2340 list_del(&qh->ring);
2341 kfree(qh);
2342 }
Felipe Balbi550a7372008-07-24 12:27:36 +03002343 } else
Sergei Shtylyov81ec4e42009-03-27 12:57:50 -07002344 ret = musb_cleanup_urb(urb, qh);
Felipe Balbi550a7372008-07-24 12:27:36 +03002345done:
2346 spin_unlock_irqrestore(&musb->lock, flags);
2347 return ret;
2348}
2349
2350/* disable an endpoint */
2351static void
2352musb_h_disable(struct usb_hcd *hcd, struct usb_host_endpoint *hep)
2353{
Sergei Shtylyov22a0d6f2009-03-27 12:56:26 -07002354 u8 is_in = hep->desc.bEndpointAddress & USB_DIR_IN;
Felipe Balbi550a7372008-07-24 12:27:36 +03002355 unsigned long flags;
2356 struct musb *musb = hcd_to_musb(hcd);
Sergei Shtylyovdc61d232009-02-21 15:31:01 -08002357 struct musb_qh *qh;
2358 struct urb *urb;
Felipe Balbi550a7372008-07-24 12:27:36 +03002359
Felipe Balbi550a7372008-07-24 12:27:36 +03002360 spin_lock_irqsave(&musb->lock, flags);
2361
Sergei Shtylyovdc61d232009-02-21 15:31:01 -08002362 qh = hep->hcpriv;
2363 if (qh == NULL)
2364 goto exit;
2365
Sergei Shtylyov22a0d6f2009-03-27 12:56:26 -07002366 /* NOTE: qh is invalid unless !list_empty(&hep->urb_list) */
Felipe Balbi550a7372008-07-24 12:27:36 +03002367
Sergei Shtylyov22a0d6f2009-03-27 12:56:26 -07002368 /* Kick the first URB off the hardware, if needed */
Felipe Balbi550a7372008-07-24 12:27:36 +03002369 qh->is_ready = 0;
Sergei Shtylyov22a0d6f2009-03-27 12:56:26 -07002370 if (musb_ep_get_qh(qh->hw_ep, is_in) == qh) {
Felipe Balbi550a7372008-07-24 12:27:36 +03002371 urb = next_urb(qh);
2372
2373 /* make software (then hardware) stop ASAP */
2374 if (!urb->unlinked)
2375 urb->status = -ESHUTDOWN;
2376
2377 /* cleanup */
Sergei Shtylyov81ec4e42009-03-27 12:57:50 -07002378 musb_cleanup_urb(urb, qh);
Felipe Balbi550a7372008-07-24 12:27:36 +03002379
Sergei Shtylyovdc61d232009-02-21 15:31:01 -08002380 /* Then nuke all the others ... and advance the
2381 * queue on hw_ep (e.g. bulk ring) when we're done.
2382 */
2383 while (!list_empty(&hep->urb_list)) {
2384 urb = next_urb(qh);
2385 urb->status = -ESHUTDOWN;
2386 musb_advance_schedule(musb, urb, qh->hw_ep, is_in);
2387 }
2388 } else {
2389 /* Just empty the queue; the hardware is busy with
2390 * other transfers, and since !qh->is_ready nothing
2391 * will activate any of these as it advances.
2392 */
2393 while (!list_empty(&hep->urb_list))
Sergei Shtylyovc9cd06b2009-03-27 12:58:31 -07002394 musb_giveback(musb, next_urb(qh), -ESHUTDOWN);
Felipe Balbi550a7372008-07-24 12:27:36 +03002395
Sergei Shtylyovdc61d232009-02-21 15:31:01 -08002396 hep->hcpriv = NULL;
2397 list_del(&qh->ring);
2398 kfree(qh);
2399 }
2400exit:
Felipe Balbi550a7372008-07-24 12:27:36 +03002401 spin_unlock_irqrestore(&musb->lock, flags);
2402}
2403
2404static int musb_h_get_frame_number(struct usb_hcd *hcd)
2405{
2406 struct musb *musb = hcd_to_musb(hcd);
2407
2408 return musb_readw(musb->mregs, MUSB_FRAME);
2409}
2410
2411static int musb_h_start(struct usb_hcd *hcd)
2412{
2413 struct musb *musb = hcd_to_musb(hcd);
2414
2415 /* NOTE: musb_start() is called when the hub driver turns
2416 * on port power, or when (OTG) peripheral starts.
2417 */
2418 hcd->state = HC_STATE_RUNNING;
2419 musb->port1_status = 0;
2420 return 0;
2421}
2422
2423static void musb_h_stop(struct usb_hcd *hcd)
2424{
2425 musb_stop(hcd_to_musb(hcd));
2426 hcd->state = HC_STATE_HALT;
2427}
2428
2429static int musb_bus_suspend(struct usb_hcd *hcd)
2430{
2431 struct musb *musb = hcd_to_musb(hcd);
David Brownell89368d32009-07-01 03:36:16 -07002432 u8 devctl;
Felipe Balbi550a7372008-07-24 12:27:36 +03002433
David Brownell89368d32009-07-01 03:36:16 -07002434 if (!is_host_active(musb))
Felipe Balbi550a7372008-07-24 12:27:36 +03002435 return 0;
2436
David Brownell89368d32009-07-01 03:36:16 -07002437 switch (musb->xceiv->state) {
2438 case OTG_STATE_A_SUSPEND:
2439 return 0;
2440 case OTG_STATE_A_WAIT_VRISE:
2441 /* ID could be grounded even if there's no device
2442 * on the other end of the cable. NOTE that the
2443 * A_WAIT_VRISE timers are messy with MUSB...
2444 */
2445 devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
2446 if ((devctl & MUSB_DEVCTL_VBUS) == MUSB_DEVCTL_VBUS)
2447 musb->xceiv->state = OTG_STATE_A_WAIT_BCON;
2448 break;
2449 default:
2450 break;
2451 }
2452
2453 if (musb->is_active) {
2454 WARNING("trying to suspend as %s while active\n",
Felipe Balbi42c0bf12013-03-07 10:39:57 +02002455 usb_otg_state_string(musb->xceiv->state));
Felipe Balbi550a7372008-07-24 12:27:36 +03002456 return -EBUSY;
2457 } else
2458 return 0;
2459}
2460
2461static int musb_bus_resume(struct usb_hcd *hcd)
2462{
2463 /* resuming child port does the work */
2464 return 0;
2465}
2466
Ruslan Bilovol8408fd12013-03-29 19:15:21 +02002467
2468#ifndef CONFIG_MUSB_PIO_ONLY
2469
2470#define MUSB_USB_DMA_ALIGN 4
2471
2472struct musb_temp_buffer {
2473 void *kmalloc_ptr;
2474 void *old_xfer_buffer;
2475 u8 data[0];
2476};
2477
2478static void musb_free_temp_buffer(struct urb *urb)
2479{
2480 enum dma_data_direction dir;
2481 struct musb_temp_buffer *temp;
2482
2483 if (!(urb->transfer_flags & URB_ALIGNED_TEMP_BUFFER))
2484 return;
2485
2486 dir = usb_urb_dir_in(urb) ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
2487
2488 temp = container_of(urb->transfer_buffer, struct musb_temp_buffer,
2489 data);
2490
2491 if (dir == DMA_FROM_DEVICE) {
2492 memcpy(temp->old_xfer_buffer, temp->data,
2493 urb->transfer_buffer_length);
2494 }
2495 urb->transfer_buffer = temp->old_xfer_buffer;
2496 kfree(temp->kmalloc_ptr);
2497
2498 urb->transfer_flags &= ~URB_ALIGNED_TEMP_BUFFER;
2499}
2500
2501static int musb_alloc_temp_buffer(struct urb *urb, gfp_t mem_flags)
2502{
2503 enum dma_data_direction dir;
2504 struct musb_temp_buffer *temp;
2505 void *kmalloc_ptr;
2506 size_t kmalloc_size;
2507
2508 if (urb->num_sgs || urb->sg ||
2509 urb->transfer_buffer_length == 0 ||
2510 !((uintptr_t)urb->transfer_buffer & (MUSB_USB_DMA_ALIGN - 1)))
2511 return 0;
2512
2513 dir = usb_urb_dir_in(urb) ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
2514
2515 /* Allocate a buffer with enough padding for alignment */
2516 kmalloc_size = urb->transfer_buffer_length +
2517 sizeof(struct musb_temp_buffer) + MUSB_USB_DMA_ALIGN - 1;
2518
2519 kmalloc_ptr = kmalloc(kmalloc_size, mem_flags);
2520 if (!kmalloc_ptr)
2521 return -ENOMEM;
2522
2523 /* Position our struct temp_buffer such that data is aligned */
2524 temp = PTR_ALIGN(kmalloc_ptr, MUSB_USB_DMA_ALIGN);
2525
2526
2527 temp->kmalloc_ptr = kmalloc_ptr;
2528 temp->old_xfer_buffer = urb->transfer_buffer;
2529 if (dir == DMA_TO_DEVICE)
2530 memcpy(temp->data, urb->transfer_buffer,
2531 urb->transfer_buffer_length);
2532 urb->transfer_buffer = temp->data;
2533
2534 urb->transfer_flags |= URB_ALIGNED_TEMP_BUFFER;
2535
2536 return 0;
2537}
2538
2539static int musb_map_urb_for_dma(struct usb_hcd *hcd, struct urb *urb,
2540 gfp_t mem_flags)
2541{
2542 struct musb *musb = hcd_to_musb(hcd);
2543 int ret;
2544
2545 /*
2546 * The DMA engine in RTL1.8 and above cannot handle
2547 * DMA addresses that are not aligned to a 4 byte boundary.
2548 * For such engine implemented (un)map_urb_for_dma hooks.
2549 * Do not use these hooks for RTL<1.8
2550 */
2551 if (musb->hwvers < MUSB_HWVERS_1800)
2552 return usb_hcd_map_urb_for_dma(hcd, urb, mem_flags);
2553
2554 ret = musb_alloc_temp_buffer(urb, mem_flags);
2555 if (ret)
2556 return ret;
2557
2558 ret = usb_hcd_map_urb_for_dma(hcd, urb, mem_flags);
2559 if (ret)
2560 musb_free_temp_buffer(urb);
2561
2562 return ret;
2563}
2564
2565static void musb_unmap_urb_for_dma(struct usb_hcd *hcd, struct urb *urb)
2566{
2567 struct musb *musb = hcd_to_musb(hcd);
2568
2569 usb_hcd_unmap_urb_for_dma(hcd, urb);
2570
2571 /* Do not use this hook for RTL<1.8 (see description above) */
2572 if (musb->hwvers < MUSB_HWVERS_1800)
2573 return;
2574
2575 musb_free_temp_buffer(urb);
2576}
2577#endif /* !CONFIG_MUSB_PIO_ONLY */
2578
Felipe Balbi550a7372008-07-24 12:27:36 +03002579const struct hc_driver musb_hc_driver = {
2580 .description = "musb-hcd",
2581 .product_desc = "MUSB HDRC host driver",
2582 .hcd_priv_size = sizeof(struct musb),
2583 .flags = HCD_USB2 | HCD_MEMORY,
2584
2585 /* not using irq handler or reset hooks from usbcore, since
2586 * those must be shared with peripheral code for OTG configs
2587 */
2588
2589 .start = musb_h_start,
2590 .stop = musb_h_stop,
2591
2592 .get_frame_number = musb_h_get_frame_number,
2593
2594 .urb_enqueue = musb_urb_enqueue,
2595 .urb_dequeue = musb_urb_dequeue,
2596 .endpoint_disable = musb_h_disable,
2597
Ruslan Bilovol8408fd12013-03-29 19:15:21 +02002598#ifndef CONFIG_MUSB_PIO_ONLY
2599 .map_urb_for_dma = musb_map_urb_for_dma,
2600 .unmap_urb_for_dma = musb_unmap_urb_for_dma,
2601#endif
2602
Felipe Balbi550a7372008-07-24 12:27:36 +03002603 .hub_status_data = musb_hub_status_data,
2604 .hub_control = musb_hub_control,
2605 .bus_suspend = musb_bus_suspend,
2606 .bus_resume = musb_bus_resume,
2607 /* .start_port_reset = NULL, */
2608 /* .hub_irq_enable = NULL, */
2609};
Daniel Mack0b3eba42013-04-10 21:55:42 +02002610
2611void musb_host_resume_root_hub(struct musb *musb)
2612{
2613 usb_hcd_resume_root_hub(musb_to_hcd(musb));
2614}
2615
2616void musb_host_poke_root_hub(struct musb *musb)
2617{
2618 MUSB_HST_MODE(musb);
2619 if (musb_to_hcd(musb)->status_urb)
2620 usb_hcd_poll_rh_status(musb_to_hcd(musb));
2621 else
2622 usb_hcd_resume_root_hub(musb_to_hcd(musb));
2623}