blob: 7601adae1ecc3cdabdcbecde56139a7f2b7784e1 [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>
Felipe Balbi550a7372008-07-24 12:27:36 +030042#include <linux/list.h>
Maulik Mankad496dda72010-09-24 13:44:06 +030043#include <linux/dma-mapping.h>
Felipe Balbi550a7372008-07-24 12:27:36 +030044
45#include "musb_core.h"
46#include "musb_host.h"
47
Felipe Balbi550a7372008-07-24 12:27:36 +030048/* MUSB HOST status 22-mar-2006
49 *
50 * - There's still lots of partial code duplication for fault paths, so
51 * they aren't handled as consistently as they need to be.
52 *
53 * - PIO mostly behaved when last tested.
54 * + including ep0, with all usbtest cases 9, 10
55 * + usbtest 14 (ep0out) doesn't seem to run at all
56 * + double buffered OUT/TX endpoints saw stalls(!) with certain usbtest
57 * configurations, but otherwise double buffering passes basic tests.
58 * + for 2.6.N, for N > ~10, needs API changes for hcd framework.
59 *
60 * - DMA (CPPI) ... partially behaves, not currently recommended
61 * + about 1/15 the speed of typical EHCI implementations (PCI)
62 * + RX, all too often reqpkt seems to misbehave after tx
63 * + TX, no known issues (other than evident silicon issue)
64 *
65 * - DMA (Mentor/OMAP) ...has at least toggle update problems
66 *
Ajay Kumar Gupta1e0320f2009-02-24 15:26:13 -080067 * - [23-feb-2009] minimal traffic scheduling to avoid bulk RX packet
68 * starvation ... nothing yet for TX, interrupt, or bulk.
Felipe Balbi550a7372008-07-24 12:27:36 +030069 *
70 * - Not tested with HNP, but some SRP paths seem to behave.
71 *
72 * NOTE 24-August-2006:
73 *
74 * - Bulk traffic finally uses both sides of hardware ep1, freeing up an
75 * extra endpoint for periodic use enabling hub + keybd + mouse. That
76 * mostly works, except that with "usbnet" it's easy to trigger cases
77 * with "ping" where RX loses. (a) ping to davinci, even "ping -f",
78 * fine; but (b) ping _from_ davinci, even "ping -c 1", ICMP RX loses
79 * although ARP RX wins. (That test was done with a full speed link.)
80 */
81
82
83/*
84 * NOTE on endpoint usage:
85 *
86 * CONTROL transfers all go through ep0. BULK ones go through dedicated IN
87 * and OUT endpoints ... hardware is dedicated for those "async" queue(s).
Felipe Balbi550a7372008-07-24 12:27:36 +030088 * (Yes, bulk _could_ use more of the endpoints than that, and would even
Ajay Kumar Gupta1e0320f2009-02-24 15:26:13 -080089 * benefit from it.)
Felipe Balbi550a7372008-07-24 12:27:36 +030090 *
91 * INTERUPPT and ISOCHRONOUS transfers are scheduled to the other endpoints.
92 * So far that scheduling is both dumb and optimistic: the endpoint will be
93 * "claimed" until its software queue is no longer refilled. No multiplexing
94 * of transfers between endpoints, or anything clever.
95 */
96
Daniel Mack74c2e932013-04-10 21:55:45 +020097struct musb *hcd_to_musb(struct usb_hcd *hcd)
98{
99 return *(struct musb **) hcd->hcd_priv;
100}
101
Felipe Balbi550a7372008-07-24 12:27:36 +0300102
103static void musb_ep_program(struct musb *musb, u8 epnum,
Sergei Shtylyov6b6e9712009-03-26 18:29:19 -0700104 struct urb *urb, int is_out,
105 u8 *buf, u32 offset, u32 len);
Felipe Balbi550a7372008-07-24 12:27:36 +0300106
107/*
108 * Clear TX fifo. Needed to avoid BABBLE errors.
109 */
David Brownellc767c1c2008-09-11 11:53:23 +0300110static void musb_h_tx_flush_fifo(struct musb_hw_ep *ep)
Felipe Balbi550a7372008-07-24 12:27:36 +0300111{
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300112 struct musb *musb = ep->musb;
Felipe Balbi550a7372008-07-24 12:27:36 +0300113 void __iomem *epio = ep->regs;
114 u16 csr;
115 int retries = 1000;
116
117 csr = musb_readw(epio, MUSB_TXCSR);
118 while (csr & MUSB_TXCSR_FIFONOTEMPTY) {
Daniel Mack2ccc6d32014-05-26 14:52:37 +0200119 csr |= MUSB_TXCSR_FLUSHFIFO | MUSB_TXCSR_TXPKTRDY;
Felipe Balbi550a7372008-07-24 12:27:36 +0300120 musb_writew(epio, MUSB_TXCSR, csr);
121 csr = musb_readw(epio, MUSB_TXCSR);
Bin Liu68fe05e2015-11-06 12:08:56 -0600122
123 /*
124 * FIXME: sometimes the tx fifo flush failed, it has been
125 * observed during device disconnect on AM335x.
126 *
127 * To reproduce the issue, ensure tx urb(s) are queued when
128 * unplug the usb device which is connected to AM335x usb
129 * host port.
130 *
131 * I found using a usb-ethernet device and running iperf
132 * (client on AM335x) has very high chance to trigger it.
133 *
Bin Liub99d3652016-06-30 12:12:22 -0500134 * Better to turn on musb_dbg() in musb_cleanup_urb() with
Bin Liu68fe05e2015-11-06 12:08:56 -0600135 * CPPI enabled to see the issue when aborting the tx channel.
136 */
137 if (dev_WARN_ONCE(musb->controller, retries-- < 1,
David Brownellbb1c9ef2008-11-24 13:06:50 +0200138 "Could not flush host TX%d fifo: csr: %04x\n",
139 ep->epnum, csr))
Felipe Balbi550a7372008-07-24 12:27:36 +0300140 return;
Felipe Balbi550a7372008-07-24 12:27:36 +0300141 }
142}
143
David Brownell78322c12009-03-26 17:38:30 -0700144static void musb_h_ep0_flush_fifo(struct musb_hw_ep *ep)
145{
146 void __iomem *epio = ep->regs;
147 u16 csr;
148 int retries = 5;
149
150 /* scrub any data left in the fifo */
151 do {
152 csr = musb_readw(epio, MUSB_TXCSR);
153 if (!(csr & (MUSB_CSR0_TXPKTRDY | MUSB_CSR0_RXPKTRDY)))
154 break;
155 musb_writew(epio, MUSB_TXCSR, MUSB_CSR0_FLUSHFIFO);
156 csr = musb_readw(epio, MUSB_TXCSR);
157 udelay(10);
158 } while (--retries);
159
160 WARN(!retries, "Could not flush host TX%d fifo: csr: %04x\n",
161 ep->epnum, csr);
162
163 /* and reset for the next transfer */
164 musb_writew(epio, MUSB_TXCSR, 0);
165}
166
Felipe Balbi550a7372008-07-24 12:27:36 +0300167/*
168 * Start transmit. Caller is responsible for locking shared resources.
169 * musb must be locked.
170 */
171static inline void musb_h_tx_start(struct musb_hw_ep *ep)
172{
173 u16 txcsr;
174
175 /* NOTE: no locks here; caller should lock and select EP */
176 if (ep->epnum) {
177 txcsr = musb_readw(ep->regs, MUSB_TXCSR);
178 txcsr |= MUSB_TXCSR_TXPKTRDY | MUSB_TXCSR_H_WZC_BITS;
179 musb_writew(ep->regs, MUSB_TXCSR, txcsr);
180 } else {
181 txcsr = MUSB_CSR0_H_SETUPPKT | MUSB_CSR0_TXPKTRDY;
182 musb_writew(ep->regs, MUSB_CSR0, txcsr);
183 }
184
185}
186
Sergei Shtylyovc7bbc052009-03-26 18:26:40 -0700187static inline void musb_h_tx_dma_start(struct musb_hw_ep *ep)
Felipe Balbi550a7372008-07-24 12:27:36 +0300188{
189 u16 txcsr;
190
191 /* NOTE: no locks here; caller should lock and select EP */
192 txcsr = musb_readw(ep->regs, MUSB_TXCSR);
193 txcsr |= MUSB_TXCSR_DMAENAB | MUSB_TXCSR_H_WZC_BITS;
Tony Lindgrenf8e9f34f2015-05-01 12:29:27 -0700194 if (is_cppi_enabled(ep->musb))
Sergei Shtylyovc7bbc052009-03-26 18:26:40 -0700195 txcsr |= MUSB_TXCSR_DMAMODE;
Felipe Balbi550a7372008-07-24 12:27:36 +0300196 musb_writew(ep->regs, MUSB_TXCSR, txcsr);
197}
198
Sergei Shtylyov3e5c6dc2009-03-27 12:55:16 -0700199static void musb_ep_set_qh(struct musb_hw_ep *ep, int is_in, struct musb_qh *qh)
200{
201 if (is_in != 0 || ep->is_shared_fifo)
202 ep->in_qh = qh;
203 if (is_in == 0 || ep->is_shared_fifo)
204 ep->out_qh = qh;
205}
206
207static struct musb_qh *musb_ep_get_qh(struct musb_hw_ep *ep, int is_in)
208{
209 return is_in ? ep->in_qh : ep->out_qh;
210}
211
Felipe Balbi550a7372008-07-24 12:27:36 +0300212/*
213 * Start the URB at the front of an endpoint's queue
214 * end must be claimed from the caller.
215 *
216 * Context: controller locked, irqs blocked
217 */
218static void
219musb_start_urb(struct musb *musb, int is_in, struct musb_qh *qh)
220{
221 u16 frame;
222 u32 len;
Felipe Balbi550a7372008-07-24 12:27:36 +0300223 void __iomem *mbase = musb->mregs;
224 struct urb *urb = next_urb(qh);
Sergei Shtylyov6b6e9712009-03-26 18:29:19 -0700225 void *buf = urb->transfer_buffer;
226 u32 offset = 0;
Felipe Balbi550a7372008-07-24 12:27:36 +0300227 struct musb_hw_ep *hw_ep = qh->hw_ep;
228 unsigned pipe = urb->pipe;
229 u8 address = usb_pipedevice(pipe);
230 int epnum = hw_ep->epnum;
231
232 /* initialize software qh state */
233 qh->offset = 0;
234 qh->segsize = 0;
235
236 /* gather right source of data */
237 switch (qh->type) {
238 case USB_ENDPOINT_XFER_CONTROL:
239 /* control transfers always start with SETUP */
240 is_in = 0;
Felipe Balbi550a7372008-07-24 12:27:36 +0300241 musb->ep0_stage = MUSB_EP0_START;
242 buf = urb->setup_packet;
243 len = 8;
244 break;
245 case USB_ENDPOINT_XFER_ISOC:
246 qh->iso_idx = 0;
247 qh->frame = 0;
Sergei Shtylyov6b6e9712009-03-26 18:29:19 -0700248 offset = urb->iso_frame_desc[0].offset;
Felipe Balbi550a7372008-07-24 12:27:36 +0300249 len = urb->iso_frame_desc[0].length;
250 break;
251 default: /* bulk, interrupt */
Ajay Kumar Gupta1e0320f2009-02-24 15:26:13 -0800252 /* actual_length may be nonzero on retry paths */
253 buf = urb->transfer_buffer + urb->actual_length;
254 len = urb->transfer_buffer_length - urb->actual_length;
Felipe Balbi550a7372008-07-24 12:27:36 +0300255 }
256
Bin Liub99d3652016-06-30 12:12:22 -0500257 musb_dbg(musb, "qh %p urb %p dev%d ep%d%s%s, hw_ep %d, %p/%d",
Felipe Balbi550a7372008-07-24 12:27:36 +0300258 qh, urb, address, qh->epnum,
259 is_in ? "in" : "out",
260 ({char *s; switch (qh->type) {
261 case USB_ENDPOINT_XFER_CONTROL: s = ""; break;
262 case USB_ENDPOINT_XFER_BULK: s = "-bulk"; break;
263 case USB_ENDPOINT_XFER_ISOC: s = "-iso"; break;
264 default: s = "-intr"; break;
Joe Perches2b84f922013-10-08 16:01:37 -0700265 } s; }),
Sergei Shtylyov6b6e9712009-03-26 18:29:19 -0700266 epnum, buf + offset, len);
Felipe Balbi550a7372008-07-24 12:27:36 +0300267
268 /* Configure endpoint */
Sergei Shtylyov3e5c6dc2009-03-27 12:55:16 -0700269 musb_ep_set_qh(hw_ep, is_in, qh);
Sergei Shtylyov6b6e9712009-03-26 18:29:19 -0700270 musb_ep_program(musb, epnum, urb, !is_in, buf, offset, len);
Felipe Balbi550a7372008-07-24 12:27:36 +0300271
272 /* transmit may have more work: start it when it is time */
273 if (is_in)
274 return;
275
276 /* determine if the time is right for a periodic transfer */
277 switch (qh->type) {
278 case USB_ENDPOINT_XFER_ISOC:
279 case USB_ENDPOINT_XFER_INT:
Bin Liub99d3652016-06-30 12:12:22 -0500280 musb_dbg(musb, "check whether there's still time for periodic Tx");
Felipe Balbi550a7372008-07-24 12:27:36 +0300281 frame = musb_readw(mbase, MUSB_FRAME);
282 /* FIXME this doesn't implement that scheduling policy ...
283 * or handle framecounter wrapping
284 */
Alan Stern8a1ea512013-05-29 13:21:01 -0400285 if (1) { /* Always assume URB_ISO_ASAP */
Felipe Balbi550a7372008-07-24 12:27:36 +0300286 /* REVISIT the SOF irq handler shouldn't duplicate
287 * this code; and we don't init urb->start_frame...
288 */
289 qh->frame = 0;
290 goto start;
291 } else {
292 qh->frame = urb->start_frame;
293 /* enable SOF interrupt so we can count down */
Bin Liub99d3652016-06-30 12:12:22 -0500294 musb_dbg(musb, "SOF for %d", epnum);
Felipe Balbi550a7372008-07-24 12:27:36 +0300295#if 1 /* ifndef CONFIG_ARCH_DAVINCI */
296 musb_writeb(mbase, MUSB_INTRUSBE, 0xff);
297#endif
298 }
299 break;
300 default:
301start:
Bin Liub99d3652016-06-30 12:12:22 -0500302 musb_dbg(musb, "Start TX%d %s", epnum,
Felipe Balbi550a7372008-07-24 12:27:36 +0300303 hw_ep->tx_channel ? "dma" : "pio");
304
305 if (!hw_ep->tx_channel)
306 musb_h_tx_start(hw_ep);
Tony Lindgrenf8e9f34f2015-05-01 12:29:27 -0700307 else if (is_cppi_enabled(musb) || tusb_dma_omap(musb))
Sergei Shtylyovc7bbc052009-03-26 18:26:40 -0700308 musb_h_tx_dma_start(hw_ep);
Felipe Balbi550a7372008-07-24 12:27:36 +0300309 }
310}
311
Sergei Shtylyovc9cd06b2009-03-27 12:58:31 -0700312/* Context: caller owns controller lock, IRQs are blocked */
313static void musb_giveback(struct musb *musb, struct urb *urb, int status)
Felipe Balbi550a7372008-07-24 12:27:36 +0300314__releases(musb->lock)
315__acquires(musb->lock)
316{
Bin Liub99d3652016-06-30 12:12:22 -0500317 musb_dbg(musb, "complete %p %pF (%d), dev%d ep%d%s, %d/%d",
David Brownellbb1c9ef2008-11-24 13:06:50 +0200318 urb, urb->complete, status,
Felipe Balbi550a7372008-07-24 12:27:36 +0300319 usb_pipedevice(urb->pipe),
320 usb_pipeendpoint(urb->pipe),
321 usb_pipein(urb->pipe) ? "in" : "out",
322 urb->actual_length, urb->transfer_buffer_length
323 );
324
Daniel Mack8b125df2013-04-10 21:55:50 +0200325 usb_hcd_unlink_urb_from_ep(musb->hcd, urb);
Felipe Balbi550a7372008-07-24 12:27:36 +0300326 spin_unlock(&musb->lock);
Daniel Mack8b125df2013-04-10 21:55:50 +0200327 usb_hcd_giveback_urb(musb->hcd, urb, status);
Felipe Balbi550a7372008-07-24 12:27:36 +0300328 spin_lock(&musb->lock);
329}
330
Sergei Shtylyov846099a2009-03-27 12:54:21 -0700331/* For bulk/interrupt endpoints only */
332static inline void musb_save_toggle(struct musb_qh *qh, int is_in,
333 struct urb *urb)
Felipe Balbi550a7372008-07-24 12:27:36 +0300334{
Sergei Shtylyov846099a2009-03-27 12:54:21 -0700335 void __iomem *epio = qh->hw_ep->regs;
Felipe Balbi550a7372008-07-24 12:27:36 +0300336 u16 csr;
Felipe Balbi550a7372008-07-24 12:27:36 +0300337
Sergei Shtylyov846099a2009-03-27 12:54:21 -0700338 /*
339 * FIXME: the current Mentor DMA code seems to have
Felipe Balbi550a7372008-07-24 12:27:36 +0300340 * problems getting toggle correct.
341 */
342
Sergei Shtylyov846099a2009-03-27 12:54:21 -0700343 if (is_in)
344 csr = musb_readw(epio, MUSB_RXCSR) & MUSB_RXCSR_H_DATATOGGLE;
Felipe Balbi550a7372008-07-24 12:27:36 +0300345 else
Sergei Shtylyov846099a2009-03-27 12:54:21 -0700346 csr = musb_readw(epio, MUSB_TXCSR) & MUSB_TXCSR_H_DATATOGGLE;
Felipe Balbi550a7372008-07-24 12:27:36 +0300347
Sergei Shtylyov846099a2009-03-27 12:54:21 -0700348 usb_settoggle(urb->dev, qh->epnum, !is_in, csr ? 1 : 0);
Felipe Balbi550a7372008-07-24 12:27:36 +0300349}
350
Sergei Shtylyovc9cd06b2009-03-27 12:58:31 -0700351/*
352 * Advance this hardware endpoint's queue, completing the specified URB and
353 * advancing to either the next URB queued to that qh, or else invalidating
354 * that qh and advancing to the next qh scheduled after the current one.
355 *
356 * Context: caller owns controller lock, IRQs are blocked
357 */
358static void musb_advance_schedule(struct musb *musb, struct urb *urb,
359 struct musb_hw_ep *hw_ep, int is_in)
Felipe Balbi550a7372008-07-24 12:27:36 +0300360{
Sergei Shtylyovc9cd06b2009-03-27 12:58:31 -0700361 struct musb_qh *qh = musb_ep_get_qh(hw_ep, is_in);
Felipe Balbi550a7372008-07-24 12:27:36 +0300362 struct musb_hw_ep *ep = qh->hw_ep;
Felipe Balbi550a7372008-07-24 12:27:36 +0300363 int ready = qh->is_ready;
Sergei Shtylyovc9cd06b2009-03-27 12:58:31 -0700364 int status;
365
366 status = (urb->status == -EINPROGRESS) ? 0 : urb->status;
Felipe Balbi550a7372008-07-24 12:27:36 +0300367
Felipe Balbi550a7372008-07-24 12:27:36 +0300368 /* save toggle eagerly, for paranoia */
369 switch (qh->type) {
370 case USB_ENDPOINT_XFER_BULK:
371 case USB_ENDPOINT_XFER_INT:
Sergei Shtylyov846099a2009-03-27 12:54:21 -0700372 musb_save_toggle(qh, is_in, urb);
Felipe Balbi550a7372008-07-24 12:27:36 +0300373 break;
374 case USB_ENDPOINT_XFER_ISOC:
Sergei Shtylyov1fe975f2009-07-10 20:02:44 +0300375 if (status == 0 && urb->error_count)
Felipe Balbi550a7372008-07-24 12:27:36 +0300376 status = -EXDEV;
377 break;
378 }
379
Felipe Balbi550a7372008-07-24 12:27:36 +0300380 qh->is_ready = 0;
Sergei Shtylyovc9cd06b2009-03-27 12:58:31 -0700381 musb_giveback(musb, urb, status);
Felipe Balbi550a7372008-07-24 12:27:36 +0300382 qh->is_ready = ready;
383
384 /* reclaim resources (and bandwidth) ASAP; deschedule it, and
385 * invalidate qh as soon as list_empty(&hep->urb_list)
386 */
387 if (list_empty(&qh->hep->urb_list)) {
388 struct list_head *head;
Ajay Kumar Gupta8c778db2012-06-21 17:18:12 +0530389 struct dma_controller *dma = musb->dma_controller;
Felipe Balbi550a7372008-07-24 12:27:36 +0300390
Ajay Kumar Gupta8c778db2012-06-21 17:18:12 +0530391 if (is_in) {
Felipe Balbi550a7372008-07-24 12:27:36 +0300392 ep->rx_reinit = 1;
Ajay Kumar Gupta8c778db2012-06-21 17:18:12 +0530393 if (ep->rx_channel) {
394 dma->channel_release(ep->rx_channel);
395 ep->rx_channel = NULL;
396 }
397 } else {
Felipe Balbi550a7372008-07-24 12:27:36 +0300398 ep->tx_reinit = 1;
Ajay Kumar Gupta8c778db2012-06-21 17:18:12 +0530399 if (ep->tx_channel) {
400 dma->channel_release(ep->tx_channel);
401 ep->tx_channel = NULL;
402 }
403 }
Felipe Balbi550a7372008-07-24 12:27:36 +0300404
Sergei Shtylyov3e5c6dc2009-03-27 12:55:16 -0700405 /* Clobber old pointers to this qh */
406 musb_ep_set_qh(ep, is_in, NULL);
Felipe Balbi550a7372008-07-24 12:27:36 +0300407 qh->hep->hcpriv = NULL;
408
409 switch (qh->type) {
410
Ajay Kumar Gupta23d15e02008-10-29 15:10:35 +0200411 case USB_ENDPOINT_XFER_CONTROL:
412 case USB_ENDPOINT_XFER_BULK:
413 /* fifo policy for these lists, except that NAKing
414 * should rotate a qh to the end (for fairness).
415 */
416 if (qh->mux == 1) {
417 head = qh->ring.prev;
418 list_del(&qh->ring);
419 kfree(qh);
420 qh = first_qh(head);
421 break;
422 }
423
Felipe Balbi550a7372008-07-24 12:27:36 +0300424 case USB_ENDPOINT_XFER_ISOC:
425 case USB_ENDPOINT_XFER_INT:
426 /* this is where periodic bandwidth should be
427 * de-allocated if it's tracked and allocated;
428 * and where we'd update the schedule tree...
429 */
Felipe Balbi550a7372008-07-24 12:27:36 +0300430 kfree(qh);
431 qh = NULL;
432 break;
Felipe Balbi550a7372008-07-24 12:27:36 +0300433 }
434 }
Felipe Balbi550a7372008-07-24 12:27:36 +0300435
Bin Liudbac5d02016-05-31 10:05:04 -0500436 /*
437 * The pipe must be broken if current urb->status is set, so don't
438 * start next urb.
439 * TODO: to minimize the risk of regression, only check urb->status
440 * for RX, until we have a test case to understand the behavior of TX.
441 */
442 if ((!status || !is_in) && qh && qh->is_ready) {
Bin Liub99d3652016-06-30 12:12:22 -0500443 musb_dbg(musb, "... next ep%d %cX urb %p",
Sergei Shtylyovc9cd06b2009-03-27 12:58:31 -0700444 hw_ep->epnum, is_in ? 'R' : 'T', next_urb(qh));
Felipe Balbi550a7372008-07-24 12:27:36 +0300445 musb_start_urb(musb, is_in, qh);
446 }
447}
448
David Brownellc767c1c2008-09-11 11:53:23 +0300449static u16 musb_h_flush_rxfifo(struct musb_hw_ep *hw_ep, u16 csr)
Felipe Balbi550a7372008-07-24 12:27:36 +0300450{
451 /* we don't want fifo to fill itself again;
452 * ignore dma (various models),
453 * leave toggle alone (may not have been saved yet)
454 */
455 csr |= MUSB_RXCSR_FLUSHFIFO | MUSB_RXCSR_RXPKTRDY;
456 csr &= ~(MUSB_RXCSR_H_REQPKT
457 | MUSB_RXCSR_H_AUTOREQ
458 | MUSB_RXCSR_AUTOCLEAR);
459
460 /* write 2x to allow double buffering */
461 musb_writew(hw_ep->regs, MUSB_RXCSR, csr);
462 musb_writew(hw_ep->regs, MUSB_RXCSR, csr);
463
464 /* flush writebuffer */
465 return musb_readw(hw_ep->regs, MUSB_RXCSR);
466}
467
468/*
469 * PIO RX for a packet (or part of it).
470 */
471static bool
472musb_host_packet_rx(struct musb *musb, struct urb *urb, u8 epnum, u8 iso_err)
473{
474 u16 rx_count;
475 u8 *buf;
476 u16 csr;
477 bool done = false;
478 u32 length;
479 int do_flush = 0;
480 struct musb_hw_ep *hw_ep = musb->endpoints + epnum;
481 void __iomem *epio = hw_ep->regs;
482 struct musb_qh *qh = hw_ep->in_qh;
483 int pipe = urb->pipe;
484 void *buffer = urb->transfer_buffer;
485
486 /* musb_ep_select(mbase, epnum); */
487 rx_count = musb_readw(epio, MUSB_RXCOUNT);
Bin Liub99d3652016-06-30 12:12:22 -0500488 musb_dbg(musb, "RX%d count %d, buffer %p len %d/%d", epnum, rx_count,
Felipe Balbi550a7372008-07-24 12:27:36 +0300489 urb->transfer_buffer, qh->offset,
490 urb->transfer_buffer_length);
491
492 /* unload FIFO */
493 if (usb_pipeisoc(pipe)) {
494 int status = 0;
495 struct usb_iso_packet_descriptor *d;
496
497 if (iso_err) {
498 status = -EILSEQ;
499 urb->error_count++;
500 }
501
502 d = urb->iso_frame_desc + qh->iso_idx;
503 buf = buffer + d->offset;
504 length = d->length;
505 if (rx_count > length) {
506 if (status == 0) {
507 status = -EOVERFLOW;
508 urb->error_count++;
509 }
Bin Liub99d3652016-06-30 12:12:22 -0500510 musb_dbg(musb, "OVERFLOW %d into %d", 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 d->actual_length = length;
516
517 d->status = status;
518
519 /* see if we are done */
520 done = (++qh->iso_idx >= urb->number_of_packets);
521 } else {
522 /* non-isoch */
523 buf = buffer + qh->offset;
524 length = urb->transfer_buffer_length - qh->offset;
525 if (rx_count > length) {
526 if (urb->status == -EINPROGRESS)
527 urb->status = -EOVERFLOW;
Bin Liub99d3652016-06-30 12:12:22 -0500528 musb_dbg(musb, "OVERFLOW %d into %d", rx_count, length);
Felipe Balbi550a7372008-07-24 12:27:36 +0300529 do_flush = 1;
530 } else
531 length = rx_count;
532 urb->actual_length += length;
533 qh->offset += length;
534
535 /* see if we are done */
536 done = (urb->actual_length == urb->transfer_buffer_length)
537 || (rx_count < qh->maxpacket)
538 || (urb->status != -EINPROGRESS);
539 if (done
540 && (urb->status == -EINPROGRESS)
541 && (urb->transfer_flags & URB_SHORT_NOT_OK)
542 && (urb->actual_length
543 < urb->transfer_buffer_length))
544 urb->status = -EREMOTEIO;
545 }
546
547 musb_read_fifo(hw_ep, length, buf);
548
549 csr = musb_readw(epio, MUSB_RXCSR);
550 csr |= MUSB_RXCSR_H_WZC_BITS;
551 if (unlikely(do_flush))
552 musb_h_flush_rxfifo(hw_ep, csr);
553 else {
554 /* REVISIT this assumes AUTOCLEAR is never set */
555 csr &= ~(MUSB_RXCSR_RXPKTRDY | MUSB_RXCSR_H_REQPKT);
556 if (!done)
557 csr |= MUSB_RXCSR_H_REQPKT;
558 musb_writew(epio, MUSB_RXCSR, csr);
559 }
560
561 return done;
562}
563
564/* we don't always need to reinit a given side of an endpoint...
565 * when we do, use tx/rx reinit routine and then construct a new CSR
566 * to address data toggle, NYET, and DMA or PIO.
567 *
568 * it's possible that driver bugs (especially for DMA) or aborting a
569 * transfer might have left the endpoint busier than it should be.
570 * the busy/not-empty tests are basically paranoia.
571 */
572static void
Hans de Goede0cb74b32015-03-20 20:11:11 +0100573musb_rx_reinit(struct musb *musb, struct musb_qh *qh, u8 epnum)
Felipe Balbi550a7372008-07-24 12:27:36 +0300574{
Hans de Goede0cb74b32015-03-20 20:11:11 +0100575 struct musb_hw_ep *ep = musb->endpoints + epnum;
Felipe Balbi550a7372008-07-24 12:27:36 +0300576 u16 csr;
577
578 /* NOTE: we know the "rx" fifo reinit never triggers for ep0.
579 * That always uses tx_reinit since ep0 repurposes TX register
580 * offsets; the initial SETUP packet is also a kind of OUT.
581 */
582
583 /* if programmed for Tx, put it in RX mode */
584 if (ep->is_shared_fifo) {
585 csr = musb_readw(ep->regs, MUSB_TXCSR);
586 if (csr & MUSB_TXCSR_MODE) {
587 musb_h_tx_flush_fifo(ep);
Sergei Shtylyovb6e434a2009-03-26 18:27:47 -0700588 csr = musb_readw(ep->regs, MUSB_TXCSR);
Felipe Balbi550a7372008-07-24 12:27:36 +0300589 musb_writew(ep->regs, MUSB_TXCSR,
Sergei Shtylyovb6e434a2009-03-26 18:27:47 -0700590 csr | MUSB_TXCSR_FRCDATATOG);
Felipe Balbi550a7372008-07-24 12:27:36 +0300591 }
Sergei Shtylyovb6e434a2009-03-26 18:27:47 -0700592
593 /*
594 * Clear the MODE bit (and everything else) to enable Rx.
595 * NOTE: we mustn't clear the DMAMODE bit before DMAENAB.
596 */
597 if (csr & MUSB_TXCSR_DMAMODE)
598 musb_writew(ep->regs, MUSB_TXCSR, MUSB_TXCSR_DMAMODE);
Felipe Balbi550a7372008-07-24 12:27:36 +0300599 musb_writew(ep->regs, MUSB_TXCSR, 0);
600
601 /* scrub all previous state, clearing toggle */
Felipe Balbi550a7372008-07-24 12:27:36 +0300602 }
Andrew Goodbodyf3eec0cf2016-05-31 10:05:26 -0500603 csr = musb_readw(ep->regs, MUSB_RXCSR);
604 if (csr & MUSB_RXCSR_RXPKTRDY)
605 WARNING("rx%d, packet/%d ready?\n", ep->epnum,
606 musb_readw(ep->regs, MUSB_RXCOUNT));
607
608 musb_h_flush_rxfifo(ep, MUSB_RXCSR_CLRDATATOG);
Felipe Balbi550a7372008-07-24 12:27:36 +0300609
610 /* target addr and (for multipoint) hub addr/port */
611 if (musb->is_multipoint) {
Hans de Goede6cc2af62015-03-20 20:11:12 +0100612 musb_write_rxfunaddr(musb, epnum, qh->addr_reg);
613 musb_write_rxhubaddr(musb, epnum, qh->h_addr_reg);
614 musb_write_rxhubport(musb, epnum, qh->h_port_reg);
Felipe Balbi550a7372008-07-24 12:27:36 +0300615 } else
616 musb_writeb(musb->mregs, MUSB_FADDR, qh->addr_reg);
617
618 /* protocol/endpoint, interval/NAKlimit, i/o size */
619 musb_writeb(ep->regs, MUSB_RXTYPE, qh->type_reg);
620 musb_writeb(ep->regs, MUSB_RXINTERVAL, qh->intv_reg);
621 /* NOTE: bulk combining rewrites high bits of maxpacket */
Cliff Cai9f445cb2010-01-28 20:44:18 -0500622 /* Set RXMAXP with the FIFO size of the endpoint
623 * to disable double buffer mode.
624 */
Felipe Balbi06624812011-01-21 13:39:20 +0800625 if (musb->double_buffer_not_ok)
Cliff Cai9f445cb2010-01-28 20:44:18 -0500626 musb_writew(ep->regs, MUSB_RXMAXP, ep->max_packet_sz_rx);
627 else
628 musb_writew(ep->regs, MUSB_RXMAXP,
629 qh->maxpacket | ((qh->hb_mult - 1) << 11));
Felipe Balbi550a7372008-07-24 12:27:36 +0300630
631 ep->rx_reinit = 0;
632}
633
Sergei Shtylyovb6a66312016-05-31 10:05:06 -0500634static void musb_tx_dma_set_mode_mentor(struct dma_controller *dma,
Sergei Shtylyov6b6e9712009-03-26 18:29:19 -0700635 struct musb_hw_ep *hw_ep, struct musb_qh *qh,
Tony Lindgren754fe4a2015-05-01 12:29:32 -0700636 struct urb *urb, u32 offset,
637 u32 *length, u8 *mode)
Sergei Shtylyov6b6e9712009-03-26 18:29:19 -0700638{
639 struct dma_channel *channel = hw_ep->tx_channel;
640 void __iomem *epio = hw_ep->regs;
641 u16 pkt_size = qh->maxpacket;
642 u16 csr;
Sergei Shtylyov6b6e9712009-03-26 18:29:19 -0700643
Tony Lindgren754fe4a2015-05-01 12:29:32 -0700644 if (*length > channel->max_len)
645 *length = channel->max_len;
Sergei Shtylyov6b6e9712009-03-26 18:29:19 -0700646
647 csr = musb_readw(epio, MUSB_TXCSR);
Tony Lindgren754fe4a2015-05-01 12:29:32 -0700648 if (*length > pkt_size) {
649 *mode = 1;
Ajay Kumar Guptaa483d702009-04-03 16:16:17 -0700650 csr |= MUSB_TXCSR_DMAMODE | MUSB_TXCSR_DMAENAB;
651 /* autoset shouldn't be set in high bandwidth */
supriya karanthf2786282012-12-06 11:16:23 +0530652 /*
653 * Enable Autoset according to table
654 * below
655 * bulk_split hb_mult Autoset_Enable
656 * 0 1 Yes(Normal)
657 * 0 >1 No(High BW ISO)
658 * 1 1 Yes(HS bulk)
659 * 1 >1 Yes(FS bulk)
660 */
661 if (qh->hb_mult == 1 || (qh->hb_mult > 1 &&
662 can_bulk_split(hw_ep->musb, qh->type)))
Ajay Kumar Guptaa483d702009-04-03 16:16:17 -0700663 csr |= MUSB_TXCSR_AUTOSET;
Sergei Shtylyov6b6e9712009-03-26 18:29:19 -0700664 } else {
Tony Lindgren754fe4a2015-05-01 12:29:32 -0700665 *mode = 0;
Sergei Shtylyov6b6e9712009-03-26 18:29:19 -0700666 csr &= ~(MUSB_TXCSR_AUTOSET | MUSB_TXCSR_DMAMODE);
667 csr |= MUSB_TXCSR_DMAENAB; /* against programmer's guide */
668 }
Cristian Birsanbba40e62016-02-11 08:58:17 -0700669 channel->desired_mode = *mode;
Sergei Shtylyov6b6e9712009-03-26 18:29:19 -0700670 musb_writew(epio, MUSB_TXCSR, csr);
Tony Lindgren754fe4a2015-05-01 12:29:32 -0700671}
672
Sergei Shtylyovb6a66312016-05-31 10:05:06 -0500673static void musb_tx_dma_set_mode_cppi_tusb(struct dma_controller *dma,
674 struct musb_hw_ep *hw_ep,
675 struct musb_qh *qh,
676 struct urb *urb,
677 u32 offset,
678 u32 *length,
679 u8 *mode)
Tony Lindgren754fe4a2015-05-01 12:29:32 -0700680{
681 struct dma_channel *channel = hw_ep->tx_channel;
682
Sergei Shtylyov6b6e9712009-03-26 18:29:19 -0700683 channel->actual_len = 0;
684
685 /*
686 * TX uses "RNDIS" mode automatically but needs help
687 * to identify the zero-length-final-packet case.
688 */
Tony Lindgren754fe4a2015-05-01 12:29:32 -0700689 *mode = (urb->transfer_flags & URB_ZERO_PACKET) ? 1 : 0;
Tony Lindgren754fe4a2015-05-01 12:29:32 -0700690}
691
692static bool musb_tx_dma_program(struct dma_controller *dma,
693 struct musb_hw_ep *hw_ep, struct musb_qh *qh,
694 struct urb *urb, u32 offset, u32 length)
695{
696 struct dma_channel *channel = hw_ep->tx_channel;
697 u16 pkt_size = qh->maxpacket;
698 u8 mode;
Tony Lindgren754fe4a2015-05-01 12:29:32 -0700699
700 if (musb_dma_inventra(hw_ep->musb) || musb_dma_ux500(hw_ep->musb))
Sergei Shtylyovb6a66312016-05-31 10:05:06 -0500701 musb_tx_dma_set_mode_mentor(dma, hw_ep, qh, urb, offset,
702 &length, &mode);
Sergei Shtylyov858b9be2016-05-31 10:05:05 -0500703 else if (is_cppi_enabled(hw_ep->musb) || tusb_dma_omap(hw_ep->musb))
Sergei Shtylyovb6a66312016-05-31 10:05:06 -0500704 musb_tx_dma_set_mode_cppi_tusb(dma, hw_ep, qh, urb, offset,
705 &length, &mode);
Sergei Shtylyov858b9be2016-05-31 10:05:05 -0500706 else
707 return false;
Sergei Shtylyov6b6e9712009-03-26 18:29:19 -0700708
709 qh->segsize = length;
710
Santosh Shilimkar4c647332010-09-20 10:32:07 +0300711 /*
712 * Ensure the data reaches to main memory before starting
713 * DMA transfer
714 */
715 wmb();
716
Sergei Shtylyov6b6e9712009-03-26 18:29:19 -0700717 if (!dma->channel_program(channel, pkt_size, mode,
718 urb->transfer_dma + offset, length)) {
Tony Lindgren754fe4a2015-05-01 12:29:32 -0700719 void __iomem *epio = hw_ep->regs;
720 u16 csr;
721
Sergei Shtylyov6b6e9712009-03-26 18:29:19 -0700722 dma->channel_release(channel);
723 hw_ep->tx_channel = NULL;
724
725 csr = musb_readw(epio, MUSB_TXCSR);
726 csr &= ~(MUSB_TXCSR_AUTOSET | MUSB_TXCSR_DMAENAB);
727 musb_writew(epio, MUSB_TXCSR, csr | MUSB_TXCSR_H_WZC_BITS);
728 return false;
729 }
730 return true;
731}
Felipe Balbi550a7372008-07-24 12:27:36 +0300732
733/*
734 * Program an HDRC endpoint as per the given URB
735 * Context: irqs blocked, controller lock held
736 */
737static void musb_ep_program(struct musb *musb, u8 epnum,
Sergei Shtylyov6b6e9712009-03-26 18:29:19 -0700738 struct urb *urb, int is_out,
739 u8 *buf, u32 offset, u32 len)
Felipe Balbi550a7372008-07-24 12:27:36 +0300740{
741 struct dma_controller *dma_controller;
742 struct dma_channel *dma_channel;
743 u8 dma_ok;
744 void __iomem *mbase = musb->mregs;
745 struct musb_hw_ep *hw_ep = musb->endpoints + epnum;
746 void __iomem *epio = hw_ep->regs;
Sergei Shtylyov3e5c6dc2009-03-27 12:55:16 -0700747 struct musb_qh *qh = musb_ep_get_qh(hw_ep, !is_out);
748 u16 packet_sz = qh->maxpacket;
Ajay Kumar Gupta31321222012-07-20 11:07:22 +0530749 u8 use_dma = 1;
750 u16 csr;
Felipe Balbi550a7372008-07-24 12:27:36 +0300751
Bin Liub99d3652016-06-30 12:12:22 -0500752 musb_dbg(musb, "%s hw%d urb %p spd%d dev%d ep%d%s "
753 "h_addr%02x h_port%02x bytes %d",
Felipe Balbi550a7372008-07-24 12:27:36 +0300754 is_out ? "-->" : "<--",
755 epnum, urb, urb->dev->speed,
756 qh->addr_reg, qh->epnum, is_out ? "out" : "in",
757 qh->h_addr_reg, qh->h_port_reg,
758 len);
759
760 musb_ep_select(mbase, epnum);
761
Ajay Kumar Gupta31321222012-07-20 11:07:22 +0530762 if (is_out && !len) {
763 use_dma = 0;
764 csr = musb_readw(epio, MUSB_TXCSR);
765 csr &= ~MUSB_TXCSR_DMAENAB;
766 musb_writew(epio, MUSB_TXCSR, csr);
767 hw_ep->tx_channel = NULL;
768 }
769
Felipe Balbi550a7372008-07-24 12:27:36 +0300770 /* candidate for DMA? */
771 dma_controller = musb->dma_controller;
Ajay Kumar Gupta31321222012-07-20 11:07:22 +0530772 if (use_dma && is_dma_capable() && epnum && dma_controller) {
Felipe Balbi550a7372008-07-24 12:27:36 +0300773 dma_channel = is_out ? hw_ep->tx_channel : hw_ep->rx_channel;
774 if (!dma_channel) {
775 dma_channel = dma_controller->channel_alloc(
776 dma_controller, hw_ep, is_out);
777 if (is_out)
778 hw_ep->tx_channel = dma_channel;
779 else
780 hw_ep->rx_channel = dma_channel;
781 }
782 } else
783 dma_channel = NULL;
784
785 /* make sure we clear DMAEnab, autoSet bits from previous run */
786
787 /* OUT/transmit/EP0 or IN/receive? */
788 if (is_out) {
789 u16 csr;
790 u16 int_txe;
791 u16 load_count;
792
793 csr = musb_readw(epio, MUSB_TXCSR);
794
795 /* disable interrupt in case we flush */
Sebastian Andrzej Siewiorb18d26f2012-10-30 19:52:26 +0100796 int_txe = musb->intrtxe;
Felipe Balbi550a7372008-07-24 12:27:36 +0300797 musb_writew(mbase, MUSB_INTRTXE, int_txe & ~(1 << epnum));
798
799 /* general endpoint setup */
800 if (epnum) {
Felipe Balbi550a7372008-07-24 12:27:36 +0300801 /* flush all old state, set default */
supriya karantha70b8442013-01-04 17:10:33 +0530802 /*
803 * We could be flushing valid
804 * packets in double buffering
805 * case
806 */
807 if (!hw_ep->tx_double_buffered)
808 musb_h_tx_flush_fifo(hw_ep);
Sergei Shtylyovb6e434a2009-03-26 18:27:47 -0700809
810 /*
811 * We must not clear the DMAMODE bit before or in
812 * the same cycle with the DMAENAB bit, so we clear
813 * the latter first...
814 */
Felipe Balbi550a7372008-07-24 12:27:36 +0300815 csr &= ~(MUSB_TXCSR_H_NAKTIMEOUT
Sergei Shtylyovb6e434a2009-03-26 18:27:47 -0700816 | MUSB_TXCSR_AUTOSET
817 | MUSB_TXCSR_DMAENAB
Felipe Balbi550a7372008-07-24 12:27:36 +0300818 | MUSB_TXCSR_FRCDATATOG
819 | MUSB_TXCSR_H_RXSTALL
820 | MUSB_TXCSR_H_ERROR
821 | MUSB_TXCSR_TXPKTRDY
822 );
823 csr |= MUSB_TXCSR_MODE;
824
supriya karantha70b8442013-01-04 17:10:33 +0530825 if (!hw_ep->tx_double_buffered) {
826 if (usb_gettoggle(urb->dev, qh->epnum, 1))
827 csr |= MUSB_TXCSR_H_WR_DATATOGGLE
828 | MUSB_TXCSR_H_DATATOGGLE;
829 else
830 csr |= MUSB_TXCSR_CLRDATATOG;
831 }
Felipe Balbi550a7372008-07-24 12:27:36 +0300832
Felipe Balbi550a7372008-07-24 12:27:36 +0300833 musb_writew(epio, MUSB_TXCSR, csr);
834 /* REVISIT may need to clear FLUSHFIFO ... */
Sergei Shtylyovb6e434a2009-03-26 18:27:47 -0700835 csr &= ~MUSB_TXCSR_DMAMODE;
Felipe Balbi550a7372008-07-24 12:27:36 +0300836 musb_writew(epio, MUSB_TXCSR, csr);
837 csr = musb_readw(epio, MUSB_TXCSR);
838 } else {
839 /* endpoint 0: just flush */
David Brownell78322c12009-03-26 17:38:30 -0700840 musb_h_ep0_flush_fifo(hw_ep);
Felipe Balbi550a7372008-07-24 12:27:36 +0300841 }
842
843 /* target addr and (for multipoint) hub addr/port */
844 if (musb->is_multipoint) {
Hans de Goede6cc2af62015-03-20 20:11:12 +0100845 musb_write_txfunaddr(musb, epnum, qh->addr_reg);
846 musb_write_txhubaddr(musb, epnum, qh->h_addr_reg);
847 musb_write_txhubport(musb, epnum, qh->h_port_reg);
Felipe Balbi550a7372008-07-24 12:27:36 +0300848/* FIXME if !epnum, do the same for RX ... */
849 } else
850 musb_writeb(mbase, MUSB_FADDR, qh->addr_reg);
851
852 /* protocol/endpoint/interval/NAKlimit */
853 if (epnum) {
854 musb_writeb(epio, MUSB_TXTYPE, qh->type_reg);
supriya karanthf2786282012-12-06 11:16:23 +0530855 if (musb->double_buffer_not_ok) {
Felipe Balbi550a7372008-07-24 12:27:36 +0300856 musb_writew(epio, MUSB_TXMAXP,
Felipe Balbi06624812011-01-21 13:39:20 +0800857 hw_ep->max_packet_sz_tx);
supriya karanthf2786282012-12-06 11:16:23 +0530858 } else if (can_bulk_split(musb, qh->type)) {
859 qh->hb_mult = hw_ep->max_packet_sz_tx
860 / packet_sz;
Ajay Kumar Guptaccc080c2011-12-13 10:32:42 +0530861 musb_writew(epio, MUSB_TXMAXP, packet_sz
supriya karanthf2786282012-12-06 11:16:23 +0530862 | ((qh->hb_mult) - 1) << 11);
863 } else {
Felipe Balbi550a7372008-07-24 12:27:36 +0300864 musb_writew(epio, MUSB_TXMAXP,
Felipe Balbi06624812011-01-21 13:39:20 +0800865 qh->maxpacket |
866 ((qh->hb_mult - 1) << 11));
supriya karanthf2786282012-12-06 11:16:23 +0530867 }
Felipe Balbi550a7372008-07-24 12:27:36 +0300868 musb_writeb(epio, MUSB_TXINTERVAL, qh->intv_reg);
869 } else {
870 musb_writeb(epio, MUSB_NAKLIMIT0, qh->intv_reg);
871 if (musb->is_multipoint)
872 musb_writeb(epio, MUSB_TYPE0,
873 qh->type_reg);
874 }
875
876 if (can_bulk_split(musb, qh->type))
877 load_count = min((u32) hw_ep->max_packet_sz_tx,
878 len);
879 else
880 load_count = min((u32) packet_sz, len);
881
Sergei Shtylyov6b6e9712009-03-26 18:29:19 -0700882 if (dma_channel && musb_tx_dma_program(dma_controller,
883 hw_ep, qh, urb, offset, len))
884 load_count = 0;
Felipe Balbi550a7372008-07-24 12:27:36 +0300885
886 if (load_count) {
Felipe Balbi550a7372008-07-24 12:27:36 +0300887 /* PIO to load FIFO */
888 qh->segsize = load_count;
Virupax Sadashivpetimath8e8a5512012-08-07 14:46:20 +0530889 if (!buf) {
890 sg_miter_start(&qh->sg_miter, urb->sg, 1,
891 SG_MITER_ATOMIC
892 | SG_MITER_FROM_SG);
893 if (!sg_miter_next(&qh->sg_miter)) {
894 dev_err(musb->controller,
895 "error: sg"
896 "list empty\n");
897 sg_miter_stop(&qh->sg_miter);
898 goto finish;
899 }
900 buf = qh->sg_miter.addr + urb->sg->offset +
901 urb->actual_length;
902 load_count = min_t(u32, load_count,
903 qh->sg_miter.length);
904 musb_write_fifo(hw_ep, load_count, buf);
905 qh->sg_miter.consumed = load_count;
906 sg_miter_stop(&qh->sg_miter);
907 } else
908 musb_write_fifo(hw_ep, load_count, buf);
Felipe Balbi550a7372008-07-24 12:27:36 +0300909 }
Virupax Sadashivpetimath8e8a5512012-08-07 14:46:20 +0530910finish:
Felipe Balbi550a7372008-07-24 12:27:36 +0300911 /* re-enable interrupt */
912 musb_writew(mbase, MUSB_INTRTXE, int_txe);
913
914 /* IN/receive */
915 } else {
916 u16 csr;
917
918 if (hw_ep->rx_reinit) {
Hans de Goede0cb74b32015-03-20 20:11:11 +0100919 musb_rx_reinit(musb, qh, epnum);
Felipe Balbi550a7372008-07-24 12:27:36 +0300920
921 /* init new state: toggle and NYET, maybe DMA later */
922 if (usb_gettoggle(urb->dev, qh->epnum, 0))
923 csr = MUSB_RXCSR_H_WR_DATATOGGLE
924 | MUSB_RXCSR_H_DATATOGGLE;
925 else
926 csr = 0;
927 if (qh->type == USB_ENDPOINT_XFER_INT)
928 csr |= MUSB_RXCSR_DISNYET;
929
930 } else {
931 csr = musb_readw(hw_ep->regs, MUSB_RXCSR);
932
933 if (csr & (MUSB_RXCSR_RXPKTRDY
934 | MUSB_RXCSR_DMAENAB
935 | MUSB_RXCSR_H_REQPKT))
936 ERR("broken !rx_reinit, ep%d csr %04x\n",
937 hw_ep->epnum, csr);
938
939 /* scrub any stale state, leaving toggle alone */
940 csr &= MUSB_RXCSR_DISNYET;
941 }
942
943 /* kick things off */
944
Tony Lindgrenf8e9f34f2015-05-01 12:29:27 -0700945 if ((is_cppi_enabled(musb) || tusb_dma_omap(musb)) && dma_channel) {
Sergei Shtylyovc51e36d2011-05-07 19:44:13 +0400946 /* Candidate for DMA */
947 dma_channel->actual_len = 0L;
948 qh->segsize = len;
Felipe Balbi550a7372008-07-24 12:27:36 +0300949
Sergei Shtylyovc51e36d2011-05-07 19:44:13 +0400950 /* AUTOREQ is in a DMA register */
951 musb_writew(hw_ep->regs, MUSB_RXCSR, csr);
952 csr = musb_readw(hw_ep->regs, MUSB_RXCSR);
Felipe Balbi550a7372008-07-24 12:27:36 +0300953
Sergei Shtylyovc51e36d2011-05-07 19:44:13 +0400954 /*
955 * Unless caller treats short RX transfers as
956 * errors, we dare not queue multiple transfers.
957 */
958 dma_ok = dma_controller->channel_program(dma_channel,
959 packet_sz, !(urb->transfer_flags &
960 URB_SHORT_NOT_OK),
961 urb->transfer_dma + offset,
962 qh->segsize);
963 if (!dma_ok) {
964 dma_controller->channel_release(dma_channel);
965 hw_ep->rx_channel = dma_channel = NULL;
966 } else
967 csr |= MUSB_RXCSR_DMAENAB;
Felipe Balbi550a7372008-07-24 12:27:36 +0300968 }
969
970 csr |= MUSB_RXCSR_H_REQPKT;
Bin Liub99d3652016-06-30 12:12:22 -0500971 musb_dbg(musb, "RXCSR%d := %04x", epnum, csr);
Felipe Balbi550a7372008-07-24 12:27:36 +0300972 musb_writew(hw_ep->regs, MUSB_RXCSR, csr);
973 csr = musb_readw(hw_ep->regs, MUSB_RXCSR);
974 }
975}
976
Ajay Kumar Guptaf2838622012-07-19 13:41:59 +0530977/* Schedule next QH from musb->in_bulk/out_bulk and move the current qh to
978 * the end; avoids starvation for other endpoints.
979 */
980static void musb_bulk_nak_timeout(struct musb *musb, struct musb_hw_ep *ep,
981 int is_in)
982{
983 struct dma_channel *dma;
984 struct urb *urb;
985 void __iomem *mbase = musb->mregs;
986 void __iomem *epio = ep->regs;
987 struct musb_qh *cur_qh, *next_qh;
988 u16 rx_csr, tx_csr;
989
990 musb_ep_select(mbase, ep->epnum);
991 if (is_in) {
992 dma = is_dma_capable() ? ep->rx_channel : NULL;
993
Andrew Goodbody7b2c17f2016-05-31 10:05:27 -0500994 /*
995 * Need to stop the transaction by clearing REQPKT first
996 * then the NAK Timeout bit ref MUSBMHDRC USB 2.0 HIGH-SPEED
997 * DUAL-ROLE CONTROLLER Programmer's Guide, section 9.2.2
998 */
Ajay Kumar Guptaf2838622012-07-19 13:41:59 +0530999 rx_csr = musb_readw(epio, MUSB_RXCSR);
1000 rx_csr |= MUSB_RXCSR_H_WZC_BITS;
Andrew Goodbody7b2c17f2016-05-31 10:05:27 -05001001 rx_csr &= ~MUSB_RXCSR_H_REQPKT;
1002 musb_writew(epio, MUSB_RXCSR, rx_csr);
Ajay Kumar Guptaf2838622012-07-19 13:41:59 +05301003 rx_csr &= ~MUSB_RXCSR_DATAERROR;
1004 musb_writew(epio, MUSB_RXCSR, rx_csr);
1005
1006 cur_qh = first_qh(&musb->in_bulk);
1007 } else {
1008 dma = is_dma_capable() ? ep->tx_channel : NULL;
1009
1010 /* clear nak timeout bit */
1011 tx_csr = musb_readw(epio, MUSB_TXCSR);
1012 tx_csr |= MUSB_TXCSR_H_WZC_BITS;
1013 tx_csr &= ~MUSB_TXCSR_H_NAKTIMEOUT;
1014 musb_writew(epio, MUSB_TXCSR, tx_csr);
1015
1016 cur_qh = first_qh(&musb->out_bulk);
1017 }
1018 if (cur_qh) {
1019 urb = next_urb(cur_qh);
1020 if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) {
1021 dma->status = MUSB_DMA_STATUS_CORE_ABORT;
1022 musb->dma_controller->channel_abort(dma);
1023 urb->actual_length += dma->actual_len;
1024 dma->actual_len = 0L;
1025 }
1026 musb_save_toggle(cur_qh, is_in, urb);
1027
1028 if (is_in) {
1029 /* move cur_qh to end of queue */
1030 list_move_tail(&cur_qh->ring, &musb->in_bulk);
1031
1032 /* get the next qh from musb->in_bulk */
1033 next_qh = first_qh(&musb->in_bulk);
1034
1035 /* set rx_reinit and schedule the next qh */
1036 ep->rx_reinit = 1;
1037 } else {
1038 /* move cur_qh to end of queue */
1039 list_move_tail(&cur_qh->ring, &musb->out_bulk);
1040
1041 /* get the next qh from musb->out_bulk */
1042 next_qh = first_qh(&musb->out_bulk);
1043
1044 /* set tx_reinit and schedule the next qh */
1045 ep->tx_reinit = 1;
1046 }
1047 musb_start_urb(musb, is_in, next_qh);
1048 }
1049}
Felipe Balbi550a7372008-07-24 12:27:36 +03001050
1051/*
1052 * Service the default endpoint (ep0) as host.
1053 * Return true until it's time to start the status stage.
1054 */
1055static bool musb_h_ep0_continue(struct musb *musb, u16 len, struct urb *urb)
1056{
1057 bool more = false;
1058 u8 *fifo_dest = NULL;
1059 u16 fifo_count = 0;
1060 struct musb_hw_ep *hw_ep = musb->control_ep;
1061 struct musb_qh *qh = hw_ep->in_qh;
1062 struct usb_ctrlrequest *request;
1063
1064 switch (musb->ep0_stage) {
1065 case MUSB_EP0_IN:
1066 fifo_dest = urb->transfer_buffer + urb->actual_length;
Sergei Shtylyov3ecdb9a2009-02-21 15:31:23 -08001067 fifo_count = min_t(size_t, len, urb->transfer_buffer_length -
1068 urb->actual_length);
Felipe Balbi550a7372008-07-24 12:27:36 +03001069 if (fifo_count < len)
1070 urb->status = -EOVERFLOW;
1071
1072 musb_read_fifo(hw_ep, fifo_count, fifo_dest);
1073
1074 urb->actual_length += fifo_count;
1075 if (len < qh->maxpacket) {
1076 /* always terminate on short read; it's
1077 * rarely reported as an error.
1078 */
1079 } else if (urb->actual_length <
1080 urb->transfer_buffer_length)
1081 more = true;
1082 break;
1083 case MUSB_EP0_START:
1084 request = (struct usb_ctrlrequest *) urb->setup_packet;
1085
1086 if (!request->wLength) {
Bin Liub99d3652016-06-30 12:12:22 -05001087 musb_dbg(musb, "start no-DATA");
Felipe Balbi550a7372008-07-24 12:27:36 +03001088 break;
1089 } else if (request->bRequestType & USB_DIR_IN) {
Bin Liub99d3652016-06-30 12:12:22 -05001090 musb_dbg(musb, "start IN-DATA");
Felipe Balbi550a7372008-07-24 12:27:36 +03001091 musb->ep0_stage = MUSB_EP0_IN;
1092 more = true;
1093 break;
1094 } else {
Bin Liub99d3652016-06-30 12:12:22 -05001095 musb_dbg(musb, "start OUT-DATA");
Felipe Balbi550a7372008-07-24 12:27:36 +03001096 musb->ep0_stage = MUSB_EP0_OUT;
1097 more = true;
1098 }
1099 /* FALLTHROUGH */
1100 case MUSB_EP0_OUT:
Sergei Shtylyov3ecdb9a2009-02-21 15:31:23 -08001101 fifo_count = min_t(size_t, qh->maxpacket,
1102 urb->transfer_buffer_length -
1103 urb->actual_length);
Felipe Balbi550a7372008-07-24 12:27:36 +03001104 if (fifo_count) {
1105 fifo_dest = (u8 *) (urb->transfer_buffer
1106 + urb->actual_length);
Bin Liub99d3652016-06-30 12:12:22 -05001107 musb_dbg(musb, "Sending %d byte%s to ep0 fifo %p",
David Brownellbb1c9ef2008-11-24 13:06:50 +02001108 fifo_count,
1109 (fifo_count == 1) ? "" : "s",
1110 fifo_dest);
Felipe Balbi550a7372008-07-24 12:27:36 +03001111 musb_write_fifo(hw_ep, fifo_count, fifo_dest);
1112
1113 urb->actual_length += fifo_count;
1114 more = true;
1115 }
1116 break;
1117 default:
1118 ERR("bogus ep0 stage %d\n", musb->ep0_stage);
1119 break;
1120 }
1121
1122 return more;
1123}
1124
1125/*
1126 * Handle default endpoint interrupt as host. Only called in IRQ time
David Brownellc767c1c2008-09-11 11:53:23 +03001127 * from musb_interrupt().
Felipe Balbi550a7372008-07-24 12:27:36 +03001128 *
1129 * called with controller irqlocked
1130 */
1131irqreturn_t musb_h_ep0_irq(struct musb *musb)
1132{
1133 struct urb *urb;
1134 u16 csr, len;
1135 int status = 0;
1136 void __iomem *mbase = musb->mregs;
1137 struct musb_hw_ep *hw_ep = musb->control_ep;
1138 void __iomem *epio = hw_ep->regs;
1139 struct musb_qh *qh = hw_ep->in_qh;
1140 bool complete = false;
1141 irqreturn_t retval = IRQ_NONE;
1142
1143 /* ep0 only has one queue, "in" */
1144 urb = next_urb(qh);
1145
1146 musb_ep_select(mbase, 0);
1147 csr = musb_readw(epio, MUSB_CSR0);
1148 len = (csr & MUSB_CSR0_RXPKTRDY)
1149 ? musb_readb(epio, MUSB_COUNT0)
1150 : 0;
1151
Bin Liub99d3652016-06-30 12:12:22 -05001152 musb_dbg(musb, "<== csr0 %04x, qh %p, count %d, urb %p, stage %d",
Felipe Balbi550a7372008-07-24 12:27:36 +03001153 csr, qh, len, urb, musb->ep0_stage);
1154
1155 /* if we just did status stage, we are done */
1156 if (MUSB_EP0_STATUS == musb->ep0_stage) {
1157 retval = IRQ_HANDLED;
1158 complete = true;
1159 }
1160
1161 /* prepare status */
1162 if (csr & MUSB_CSR0_H_RXSTALL) {
Bin Liub99d3652016-06-30 12:12:22 -05001163 musb_dbg(musb, "STALLING ENDPOINT");
Felipe Balbi550a7372008-07-24 12:27:36 +03001164 status = -EPIPE;
1165
1166 } else if (csr & MUSB_CSR0_H_ERROR) {
Bin Liub99d3652016-06-30 12:12:22 -05001167 musb_dbg(musb, "no response, csr0 %04x", csr);
Felipe Balbi550a7372008-07-24 12:27:36 +03001168 status = -EPROTO;
1169
1170 } else if (csr & MUSB_CSR0_H_NAKTIMEOUT) {
Bin Liub99d3652016-06-30 12:12:22 -05001171 musb_dbg(musb, "control NAK timeout");
Felipe Balbi550a7372008-07-24 12:27:36 +03001172
1173 /* NOTE: this code path would be a good place to PAUSE a
1174 * control transfer, if another one is queued, so that
Ajay Kumar Gupta1e0320f2009-02-24 15:26:13 -08001175 * ep0 is more likely to stay busy. That's already done
1176 * for bulk RX transfers.
Felipe Balbi550a7372008-07-24 12:27:36 +03001177 *
1178 * if (qh->ring.next != &musb->control), then
1179 * we have a candidate... NAKing is *NOT* an error
1180 */
1181 musb_writew(epio, MUSB_CSR0, 0);
1182 retval = IRQ_HANDLED;
1183 }
1184
1185 if (status) {
Bin Liub99d3652016-06-30 12:12:22 -05001186 musb_dbg(musb, "aborting");
Felipe Balbi550a7372008-07-24 12:27:36 +03001187 retval = IRQ_HANDLED;
1188 if (urb)
1189 urb->status = status;
1190 complete = true;
1191
1192 /* use the proper sequence to abort the transfer */
1193 if (csr & MUSB_CSR0_H_REQPKT) {
1194 csr &= ~MUSB_CSR0_H_REQPKT;
1195 musb_writew(epio, MUSB_CSR0, csr);
1196 csr &= ~MUSB_CSR0_H_NAKTIMEOUT;
1197 musb_writew(epio, MUSB_CSR0, csr);
1198 } else {
David Brownell78322c12009-03-26 17:38:30 -07001199 musb_h_ep0_flush_fifo(hw_ep);
Felipe Balbi550a7372008-07-24 12:27:36 +03001200 }
1201
1202 musb_writeb(epio, MUSB_NAKLIMIT0, 0);
1203
1204 /* clear it */
1205 musb_writew(epio, MUSB_CSR0, 0);
1206 }
1207
1208 if (unlikely(!urb)) {
1209 /* stop endpoint since we have no place for its data, this
1210 * SHOULD NEVER HAPPEN! */
1211 ERR("no URB for end 0\n");
1212
David Brownell78322c12009-03-26 17:38:30 -07001213 musb_h_ep0_flush_fifo(hw_ep);
Felipe Balbi550a7372008-07-24 12:27:36 +03001214 goto done;
1215 }
1216
1217 if (!complete) {
1218 /* call common logic and prepare response */
1219 if (musb_h_ep0_continue(musb, len, urb)) {
1220 /* more packets required */
1221 csr = (MUSB_EP0_IN == musb->ep0_stage)
1222 ? MUSB_CSR0_H_REQPKT : MUSB_CSR0_TXPKTRDY;
1223 } else {
1224 /* data transfer complete; perform status phase */
1225 if (usb_pipeout(urb->pipe)
1226 || !urb->transfer_buffer_length)
1227 csr = MUSB_CSR0_H_STATUSPKT
1228 | MUSB_CSR0_H_REQPKT;
1229 else
1230 csr = MUSB_CSR0_H_STATUSPKT
1231 | MUSB_CSR0_TXPKTRDY;
1232
Ajay Kumar Gupta3c4653c2014-02-04 15:28:06 +02001233 /* disable ping token in status phase */
1234 csr |= MUSB_CSR0_H_DIS_PING;
1235
Felipe Balbi550a7372008-07-24 12:27:36 +03001236 /* flag status stage */
1237 musb->ep0_stage = MUSB_EP0_STATUS;
1238
Bin Liub99d3652016-06-30 12:12:22 -05001239 musb_dbg(musb, "ep0 STATUS, csr %04x", csr);
Felipe Balbi550a7372008-07-24 12:27:36 +03001240
1241 }
1242 musb_writew(epio, MUSB_CSR0, csr);
1243 retval = IRQ_HANDLED;
1244 } else
1245 musb->ep0_stage = MUSB_EP0_IDLE;
1246
1247 /* call completion handler if done */
1248 if (complete)
1249 musb_advance_schedule(musb, urb, hw_ep, 1);
1250done:
1251 return retval;
1252}
1253
1254
1255#ifdef CONFIG_USB_INVENTRA_DMA
1256
1257/* Host side TX (OUT) using Mentor DMA works as follows:
1258 submit_urb ->
1259 - if queue was empty, Program Endpoint
1260 - ... which starts DMA to fifo in mode 1 or 0
1261
1262 DMA Isr (transfer complete) -> TxAvail()
1263 - Stop DMA (~DmaEnab) (<--- Alert ... currently happens
1264 only in musb_cleanup_urb)
1265 - TxPktRdy has to be set in mode 0 or for
1266 short packets in mode 1.
1267*/
1268
1269#endif
1270
1271/* Service a Tx-Available or dma completion irq for the endpoint */
1272void musb_host_tx(struct musb *musb, u8 epnum)
1273{
1274 int pipe;
1275 bool done = false;
1276 u16 tx_csr;
Sergei Shtylyov6b6e9712009-03-26 18:29:19 -07001277 size_t length = 0;
1278 size_t offset = 0;
Felipe Balbi550a7372008-07-24 12:27:36 +03001279 struct musb_hw_ep *hw_ep = musb->endpoints + epnum;
1280 void __iomem *epio = hw_ep->regs;
Sergei Shtylyov3e5c6dc2009-03-27 12:55:16 -07001281 struct musb_qh *qh = hw_ep->out_qh;
1282 struct urb *urb = next_urb(qh);
Felipe Balbi550a7372008-07-24 12:27:36 +03001283 u32 status = 0;
1284 void __iomem *mbase = musb->mregs;
1285 struct dma_channel *dma;
T. S., Anil Kumarf8afbf7f2010-09-24 13:44:09 +03001286 bool transfer_pending = false;
Felipe Balbi550a7372008-07-24 12:27:36 +03001287
Felipe Balbi550a7372008-07-24 12:27:36 +03001288 musb_ep_select(mbase, epnum);
1289 tx_csr = musb_readw(epio, MUSB_TXCSR);
1290
1291 /* with CPPI, DMA sometimes triggers "extra" irqs */
1292 if (!urb) {
Bin Liub99d3652016-06-30 12:12:22 -05001293 musb_dbg(musb, "extra TX%d ready, csr %04x", epnum, tx_csr);
Sergei Shtylyov6b6e9712009-03-26 18:29:19 -07001294 return;
Felipe Balbi550a7372008-07-24 12:27:36 +03001295 }
1296
1297 pipe = urb->pipe;
1298 dma = is_dma_capable() ? hw_ep->tx_channel : NULL;
Bin Liub99d3652016-06-30 12:12:22 -05001299 musb_dbg(musb, "OUT/TX%d end, csr %04x%s", epnum, tx_csr,
Felipe Balbi550a7372008-07-24 12:27:36 +03001300 dma ? ", dma" : "");
1301
1302 /* check for errors */
1303 if (tx_csr & MUSB_TXCSR_H_RXSTALL) {
1304 /* dma was disabled, fifo flushed */
Bin Liub99d3652016-06-30 12:12:22 -05001305 musb_dbg(musb, "TX end %d stall", epnum);
Felipe Balbi550a7372008-07-24 12:27:36 +03001306
1307 /* stall; record URB status */
1308 status = -EPIPE;
1309
1310 } else if (tx_csr & MUSB_TXCSR_H_ERROR) {
1311 /* (NON-ISO) dma was disabled, fifo flushed */
Bin Liub99d3652016-06-30 12:12:22 -05001312 musb_dbg(musb, "TX 3strikes on ep=%d", epnum);
Felipe Balbi550a7372008-07-24 12:27:36 +03001313
1314 status = -ETIMEDOUT;
1315
1316 } else if (tx_csr & MUSB_TXCSR_H_NAKTIMEOUT) {
Ajay Kumar Guptaf2838622012-07-19 13:41:59 +05301317 if (USB_ENDPOINT_XFER_BULK == qh->type && qh->mux == 1
1318 && !list_is_singular(&musb->out_bulk)) {
Bin Liub99d3652016-06-30 12:12:22 -05001319 musb_dbg(musb, "NAK timeout on TX%d ep", epnum);
Ajay Kumar Guptaf2838622012-07-19 13:41:59 +05301320 musb_bulk_nak_timeout(musb, hw_ep, 0);
1321 } else {
Bin Liub99d3652016-06-30 12:12:22 -05001322 musb_dbg(musb, "TX ep%d device not responding", epnum);
Ajay Kumar Guptaf2838622012-07-19 13:41:59 +05301323 /* NOTE: this code path would be a good place to PAUSE a
1324 * transfer, if there's some other (nonperiodic) tx urb
1325 * that could use this fifo. (dma complicates it...)
1326 * That's already done for bulk RX transfers.
1327 *
1328 * if (bulk && qh->ring.next != &musb->out_bulk), then
1329 * we have a candidate... NAKing is *NOT* an error
1330 */
1331 musb_ep_select(mbase, epnum);
1332 musb_writew(epio, MUSB_TXCSR,
1333 MUSB_TXCSR_H_WZC_BITS
1334 | MUSB_TXCSR_TXPKTRDY);
1335 }
1336 return;
Felipe Balbi550a7372008-07-24 12:27:36 +03001337 }
1338
Virupax Sadashivpetimath8e8a5512012-08-07 14:46:20 +05301339done:
Felipe Balbi550a7372008-07-24 12:27:36 +03001340 if (status) {
1341 if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) {
1342 dma->status = MUSB_DMA_STATUS_CORE_ABORT;
Daniel Mack9c547692014-05-26 14:52:35 +02001343 musb->dma_controller->channel_abort(dma);
Felipe Balbi550a7372008-07-24 12:27:36 +03001344 }
1345
1346 /* do the proper sequence to abort the transfer in the
1347 * usb core; the dma engine should already be stopped.
1348 */
1349 musb_h_tx_flush_fifo(hw_ep);
1350 tx_csr &= ~(MUSB_TXCSR_AUTOSET
1351 | MUSB_TXCSR_DMAENAB
1352 | MUSB_TXCSR_H_ERROR
1353 | MUSB_TXCSR_H_RXSTALL
1354 | MUSB_TXCSR_H_NAKTIMEOUT
1355 );
1356
1357 musb_ep_select(mbase, epnum);
1358 musb_writew(epio, MUSB_TXCSR, tx_csr);
1359 /* REVISIT may need to clear FLUSHFIFO ... */
1360 musb_writew(epio, MUSB_TXCSR, tx_csr);
1361 musb_writeb(epio, MUSB_TXINTERVAL, 0);
1362
1363 done = true;
1364 }
1365
1366 /* second cppi case */
1367 if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) {
Bin Liub99d3652016-06-30 12:12:22 -05001368 musb_dbg(musb, "extra TX%d ready, csr %04x", epnum, tx_csr);
Sergei Shtylyov6b6e9712009-03-26 18:29:19 -07001369 return;
Felipe Balbi550a7372008-07-24 12:27:36 +03001370 }
1371
Sergei Shtylyovc7bbc052009-03-26 18:26:40 -07001372 if (is_dma_capable() && dma && !status) {
1373 /*
1374 * DMA has completed. But if we're using DMA mode 1 (multi
1375 * packet DMA), we need a terminal TXPKTRDY interrupt before
1376 * we can consider this transfer completed, lest we trash
1377 * its last packet when writing the next URB's data. So we
1378 * switch back to mode 0 to get that interrupt; we'll come
1379 * back here once it happens.
1380 */
1381 if (tx_csr & MUSB_TXCSR_DMAMODE) {
1382 /*
1383 * We shouldn't clear DMAMODE with DMAENAB set; so
1384 * clear them in a safe order. That should be OK
1385 * once TXPKTRDY has been set (and I've never seen
1386 * it being 0 at this moment -- DMA interrupt latency
1387 * is significant) but if it hasn't been then we have
1388 * no choice but to stop being polite and ignore the
1389 * programmer's guide... :-)
1390 *
1391 * Note that we must write TXCSR with TXPKTRDY cleared
1392 * in order not to re-trigger the packet send (this bit
1393 * can't be cleared by CPU), and there's another caveat:
1394 * TXPKTRDY may be set shortly and then cleared in the
1395 * double-buffered FIFO mode, so we do an extra TXCSR
1396 * read for debouncing...
1397 */
1398 tx_csr &= musb_readw(epio, MUSB_TXCSR);
1399 if (tx_csr & MUSB_TXCSR_TXPKTRDY) {
1400 tx_csr &= ~(MUSB_TXCSR_DMAENAB |
1401 MUSB_TXCSR_TXPKTRDY);
1402 musb_writew(epio, MUSB_TXCSR,
1403 tx_csr | MUSB_TXCSR_H_WZC_BITS);
1404 }
1405 tx_csr &= ~(MUSB_TXCSR_DMAMODE |
1406 MUSB_TXCSR_TXPKTRDY);
1407 musb_writew(epio, MUSB_TXCSR,
1408 tx_csr | MUSB_TXCSR_H_WZC_BITS);
1409
1410 /*
1411 * There is no guarantee that we'll get an interrupt
1412 * after clearing DMAMODE as we might have done this
1413 * too late (after TXPKTRDY was cleared by controller).
1414 * Re-read TXCSR as we have spoiled its previous value.
1415 */
1416 tx_csr = musb_readw(epio, MUSB_TXCSR);
1417 }
1418
1419 /*
1420 * We may get here from a DMA completion or TXPKTRDY interrupt.
1421 * In any case, we must check the FIFO status here and bail out
1422 * only if the FIFO still has data -- that should prevent the
1423 * "missed" TXPKTRDY interrupts and deal with double-buffered
1424 * FIFO mode too...
1425 */
1426 if (tx_csr & (MUSB_TXCSR_FIFONOTEMPTY | MUSB_TXCSR_TXPKTRDY)) {
Bin Liub99d3652016-06-30 12:12:22 -05001427 musb_dbg(musb,
1428 "DMA complete but FIFO not empty, CSR %04x",
1429 tx_csr);
Sergei Shtylyovc7bbc052009-03-26 18:26:40 -07001430 return;
1431 }
1432 }
1433
Felipe Balbi550a7372008-07-24 12:27:36 +03001434 if (!status || dma || usb_pipeisoc(pipe)) {
1435 if (dma)
Sergei Shtylyov6b6e9712009-03-26 18:29:19 -07001436 length = dma->actual_len;
Felipe Balbi550a7372008-07-24 12:27:36 +03001437 else
Sergei Shtylyov6b6e9712009-03-26 18:29:19 -07001438 length = qh->segsize;
1439 qh->offset += length;
Felipe Balbi550a7372008-07-24 12:27:36 +03001440
1441 if (usb_pipeisoc(pipe)) {
1442 struct usb_iso_packet_descriptor *d;
1443
1444 d = urb->iso_frame_desc + qh->iso_idx;
Sergei Shtylyov6b6e9712009-03-26 18:29:19 -07001445 d->actual_length = length;
1446 d->status = status;
Felipe Balbi550a7372008-07-24 12:27:36 +03001447 if (++qh->iso_idx >= urb->number_of_packets) {
1448 done = true;
1449 } else {
1450 d++;
Sergei Shtylyov6b6e9712009-03-26 18:29:19 -07001451 offset = d->offset;
1452 length = d->length;
Felipe Balbi550a7372008-07-24 12:27:36 +03001453 }
T. S., Anil Kumarf8afbf7f2010-09-24 13:44:09 +03001454 } else if (dma && urb->transfer_buffer_length == qh->offset) {
Felipe Balbi550a7372008-07-24 12:27:36 +03001455 done = true;
1456 } else {
1457 /* see if we need to send more data, or ZLP */
1458 if (qh->segsize < qh->maxpacket)
1459 done = true;
1460 else if (qh->offset == urb->transfer_buffer_length
1461 && !(urb->transfer_flags
1462 & URB_ZERO_PACKET))
1463 done = true;
1464 if (!done) {
Sergei Shtylyov6b6e9712009-03-26 18:29:19 -07001465 offset = qh->offset;
1466 length = urb->transfer_buffer_length - offset;
T. S., Anil Kumarf8afbf7f2010-09-24 13:44:09 +03001467 transfer_pending = true;
Felipe Balbi550a7372008-07-24 12:27:36 +03001468 }
1469 }
1470 }
1471
1472 /* urb->status != -EINPROGRESS means request has been faulted,
1473 * so we must abort this transfer after cleanup
1474 */
1475 if (urb->status != -EINPROGRESS) {
1476 done = true;
1477 if (status == 0)
1478 status = urb->status;
1479 }
1480
1481 if (done) {
1482 /* set status */
1483 urb->status = status;
1484 urb->actual_length = qh->offset;
1485 musb_advance_schedule(musb, urb, hw_ep, USB_DIR_OUT);
Sergei Shtylyov6b6e9712009-03-26 18:29:19 -07001486 return;
T. S., Anil Kumarf8afbf7f2010-09-24 13:44:09 +03001487 } else if ((usb_pipeisoc(pipe) || transfer_pending) && dma) {
Sergei Shtylyov6b6e9712009-03-26 18:29:19 -07001488 if (musb_tx_dma_program(musb->dma_controller, hw_ep, qh, urb,
Ajay Kumar Guptadfeffa52009-11-17 15:22:55 +05301489 offset, length)) {
Tony Lindgrenf8e9f34f2015-05-01 12:29:27 -07001490 if (is_cppi_enabled(musb) || tusb_dma_omap(musb))
Ajay Kumar Guptadfeffa52009-11-17 15:22:55 +05301491 musb_h_tx_dma_start(hw_ep);
Sergei Shtylyov6b6e9712009-03-26 18:29:19 -07001492 return;
Ajay Kumar Guptadfeffa52009-11-17 15:22:55 +05301493 }
Sergei Shtylyov6b6e9712009-03-26 18:29:19 -07001494 } else if (tx_csr & MUSB_TXCSR_DMAENAB) {
Bin Liub99d3652016-06-30 12:12:22 -05001495 musb_dbg(musb, "not complete, but DMA enabled?");
Sergei Shtylyov6b6e9712009-03-26 18:29:19 -07001496 return;
1497 }
Felipe Balbi550a7372008-07-24 12:27:36 +03001498
Sergei Shtylyov6b6e9712009-03-26 18:29:19 -07001499 /*
1500 * PIO: start next packet in this URB.
1501 *
1502 * REVISIT: some docs say that when hw_ep->tx_double_buffered,
1503 * (and presumably, FIFO is not half-full) we should write *two*
1504 * packets before updating TXCSR; other docs disagree...
1505 */
1506 if (length > qh->maxpacket)
1507 length = qh->maxpacket;
Maulik Mankad496dda72010-09-24 13:44:06 +03001508 /* Unmap the buffer so that CPU can use it */
Daniel Mack8b125df2013-04-10 21:55:50 +02001509 usb_hcd_unmap_urb_for_dma(musb->hcd, urb);
Virupax Sadashivpetimath8e8a5512012-08-07 14:46:20 +05301510
1511 /*
1512 * We need to map sg if the transfer_buffer is
1513 * NULL.
1514 */
1515 if (!urb->transfer_buffer)
Virupax Sadashivpetimathed74df12013-04-24 08:38:48 +02001516 qh->use_sg = true;
Virupax Sadashivpetimath8e8a5512012-08-07 14:46:20 +05301517
Virupax Sadashivpetimathed74df12013-04-24 08:38:48 +02001518 if (qh->use_sg) {
Virupax Sadashivpetimath8e8a5512012-08-07 14:46:20 +05301519 /* sg_miter_start is already done in musb_ep_program */
1520 if (!sg_miter_next(&qh->sg_miter)) {
1521 dev_err(musb->controller, "error: sg list empty\n");
1522 sg_miter_stop(&qh->sg_miter);
1523 status = -EINVAL;
1524 goto done;
1525 }
1526 urb->transfer_buffer = qh->sg_miter.addr;
1527 length = min_t(u32, length, qh->sg_miter.length);
1528 musb_write_fifo(hw_ep, length, urb->transfer_buffer);
1529 qh->sg_miter.consumed = length;
1530 sg_miter_stop(&qh->sg_miter);
1531 } else {
1532 musb_write_fifo(hw_ep, length, urb->transfer_buffer + offset);
1533 }
1534
Sergei Shtylyov6b6e9712009-03-26 18:29:19 -07001535 qh->segsize = length;
Felipe Balbi550a7372008-07-24 12:27:36 +03001536
Virupax Sadashivpetimathed74df12013-04-24 08:38:48 +02001537 if (qh->use_sg) {
Virupax Sadashivpetimath8e8a5512012-08-07 14:46:20 +05301538 if (offset + length >= urb->transfer_buffer_length)
Virupax Sadashivpetimathed74df12013-04-24 08:38:48 +02001539 qh->use_sg = false;
Virupax Sadashivpetimath8e8a5512012-08-07 14:46:20 +05301540 }
1541
Sergei Shtylyov6b6e9712009-03-26 18:29:19 -07001542 musb_ep_select(mbase, epnum);
1543 musb_writew(epio, MUSB_TXCSR,
1544 MUSB_TXCSR_H_WZC_BITS | MUSB_TXCSR_TXPKTRDY);
Felipe Balbi550a7372008-07-24 12:27:36 +03001545}
1546
Tony Lindgren069a3fd2015-05-01 12:29:33 -07001547#ifdef CONFIG_USB_TI_CPPI41_DMA
1548/* Seems to set up ISO for cppi41 and not advance len. See commit c57c41d */
1549static int musb_rx_dma_iso_cppi41(struct dma_controller *dma,
1550 struct musb_hw_ep *hw_ep,
1551 struct musb_qh *qh,
1552 struct urb *urb,
1553 size_t len)
1554{
Bin Liu04471eb2016-05-31 10:05:25 -05001555 struct dma_channel *channel = hw_ep->rx_channel;
Tony Lindgren069a3fd2015-05-01 12:29:33 -07001556 void __iomem *epio = hw_ep->regs;
1557 dma_addr_t *buf;
1558 u32 length, res;
1559 u16 val;
1560
1561 buf = (void *)urb->iso_frame_desc[qh->iso_idx].offset +
1562 (u32)urb->transfer_dma;
1563
1564 length = urb->iso_frame_desc[qh->iso_idx].length;
1565
1566 val = musb_readw(epio, MUSB_RXCSR);
1567 val |= MUSB_RXCSR_DMAENAB;
1568 musb_writew(hw_ep->regs, MUSB_RXCSR, val);
1569
1570 res = dma->channel_program(channel, qh->maxpacket, 0,
1571 (u32)buf, length);
1572
1573 return res;
1574}
1575#else
1576static inline int musb_rx_dma_iso_cppi41(struct dma_controller *dma,
1577 struct musb_hw_ep *hw_ep,
1578 struct musb_qh *qh,
1579 struct urb *urb,
1580 size_t len)
1581{
1582 return false;
1583}
1584#endif
Felipe Balbi550a7372008-07-24 12:27:36 +03001585
Tony Lindgrencff84bd2015-05-01 12:29:35 -07001586#if defined(CONFIG_USB_INVENTRA_DMA) || defined(CONFIG_USB_UX500_DMA) || \
1587 defined(CONFIG_USB_TI_CPPI41_DMA)
Felipe Balbi550a7372008-07-24 12:27:36 +03001588/* Host side RX (IN) using Mentor DMA works as follows:
1589 submit_urb ->
1590 - if queue was empty, ProgramEndpoint
1591 - first IN token is sent out (by setting ReqPkt)
1592 LinuxIsr -> RxReady()
1593 /\ => first packet is received
1594 | - Set in mode 0 (DmaEnab, ~ReqPkt)
1595 | -> DMA Isr (transfer complete) -> RxReady()
1596 | - Ack receive (~RxPktRdy), turn off DMA (~DmaEnab)
1597 | - if urb not complete, send next IN token (ReqPkt)
1598 | | else complete urb.
1599 | |
1600 ---------------------------
1601 *
1602 * Nuances of mode 1:
1603 * For short packets, no ack (+RxPktRdy) is sent automatically
1604 * (even if AutoClear is ON)
1605 * For full packets, ack (~RxPktRdy) and next IN token (+ReqPkt) is sent
1606 * automatically => major problem, as collecting the next packet becomes
1607 * difficult. Hence mode 1 is not used.
1608 *
1609 * REVISIT
1610 * All we care about at this driver level is that
1611 * (a) all URBs terminate with REQPKT cleared and fifo(s) empty;
1612 * (b) termination conditions are: short RX, or buffer full;
1613 * (c) fault modes include
1614 * - iff URB_SHORT_NOT_OK, short RX status is -EREMOTEIO.
1615 * (and that endpoint's dma queue stops immediately)
1616 * - overflow (full, PLUS more bytes in the terminal packet)
1617 *
1618 * So for example, usb-storage sets URB_SHORT_NOT_OK, and would
1619 * thus be a great candidate for using mode 1 ... for all but the
1620 * last packet of one URB's transfer.
1621 */
Tony Lindgrencff84bd2015-05-01 12:29:35 -07001622static int musb_rx_dma_inventra_cppi41(struct dma_controller *dma,
1623 struct musb_hw_ep *hw_ep,
1624 struct musb_qh *qh,
1625 struct urb *urb,
1626 size_t len)
1627{
1628 struct dma_channel *channel = hw_ep->rx_channel;
1629 void __iomem *epio = hw_ep->regs;
1630 u16 val;
1631 int pipe;
1632 bool done;
Felipe Balbi550a7372008-07-24 12:27:36 +03001633
Tony Lindgrencff84bd2015-05-01 12:29:35 -07001634 pipe = urb->pipe;
1635
1636 if (usb_pipeisoc(pipe)) {
1637 struct usb_iso_packet_descriptor *d;
1638
1639 d = urb->iso_frame_desc + qh->iso_idx;
1640 d->actual_length = len;
1641
1642 /* even if there was an error, we did the dma
1643 * for iso_frame_desc->length
1644 */
1645 if (d->status != -EILSEQ && d->status != -EOVERFLOW)
1646 d->status = 0;
1647
1648 if (++qh->iso_idx >= urb->number_of_packets) {
1649 done = true;
1650 } else {
1651 /* REVISIT: Why ignore return value here? */
1652 if (musb_dma_cppi41(hw_ep->musb))
1653 done = musb_rx_dma_iso_cppi41(dma, hw_ep, qh,
1654 urb, len);
1655 done = false;
1656 }
1657
1658 } else {
1659 /* done if urb buffer is full or short packet is recd */
1660 done = (urb->actual_length + len >=
1661 urb->transfer_buffer_length
1662 || channel->actual_len < qh->maxpacket
1663 || channel->rx_packet_done);
1664 }
1665
1666 /* send IN token for next packet, without AUTOREQ */
1667 if (!done) {
1668 val = musb_readw(epio, MUSB_RXCSR);
1669 val |= MUSB_RXCSR_H_REQPKT;
1670 musb_writew(epio, MUSB_RXCSR, MUSB_RXCSR_H_WZC_BITS | val);
1671 }
1672
1673 return done;
1674}
Tony Lindgrenac33cdb2015-05-01 12:29:37 -07001675
1676/* Disadvantage of using mode 1:
1677 * It's basically usable only for mass storage class; essentially all
1678 * other protocols also terminate transfers on short packets.
1679 *
1680 * Details:
1681 * An extra IN token is sent at the end of the transfer (due to AUTOREQ)
1682 * If you try to use mode 1 for (transfer_buffer_length - 512), and try
1683 * to use the extra IN token to grab the last packet using mode 0, then
1684 * the problem is that you cannot be sure when the device will send the
1685 * last packet and RxPktRdy set. Sometimes the packet is recd too soon
1686 * such that it gets lost when RxCSR is re-set at the end of the mode 1
1687 * transfer, while sometimes it is recd just a little late so that if you
1688 * try to configure for mode 0 soon after the mode 1 transfer is
1689 * completed, you will find rxcount 0. Okay, so you might think why not
1690 * wait for an interrupt when the pkt is recd. Well, you won't get any!
1691 */
1692static int musb_rx_dma_in_inventra_cppi41(struct dma_controller *dma,
1693 struct musb_hw_ep *hw_ep,
1694 struct musb_qh *qh,
1695 struct urb *urb,
1696 size_t len,
1697 u8 iso_err)
1698{
1699 struct musb *musb = hw_ep->musb;
1700 void __iomem *epio = hw_ep->regs;
1701 struct dma_channel *channel = hw_ep->rx_channel;
1702 u16 rx_count, val;
1703 int length, pipe, done;
1704 dma_addr_t buf;
1705
1706 rx_count = musb_readw(epio, MUSB_RXCOUNT);
1707 pipe = urb->pipe;
1708
1709 if (usb_pipeisoc(pipe)) {
1710 int d_status = 0;
1711 struct usb_iso_packet_descriptor *d;
1712
1713 d = urb->iso_frame_desc + qh->iso_idx;
1714
1715 if (iso_err) {
1716 d_status = -EILSEQ;
1717 urb->error_count++;
1718 }
1719 if (rx_count > d->length) {
1720 if (d_status == 0) {
1721 d_status = -EOVERFLOW;
1722 urb->error_count++;
1723 }
Bin Liub99d3652016-06-30 12:12:22 -05001724 musb_dbg(musb, "** OVERFLOW %d into %d",
Tony Lindgrenac33cdb2015-05-01 12:29:37 -07001725 rx_count, d->length);
1726
1727 length = d->length;
1728 } else
1729 length = rx_count;
1730 d->status = d_status;
1731 buf = urb->transfer_dma + d->offset;
1732 } else {
1733 length = rx_count;
1734 buf = urb->transfer_dma + urb->actual_length;
1735 }
1736
1737 channel->desired_mode = 0;
1738#ifdef USE_MODE1
1739 /* because of the issue below, mode 1 will
1740 * only rarely behave with correct semantics.
1741 */
1742 if ((urb->transfer_flags & URB_SHORT_NOT_OK)
1743 && (urb->transfer_buffer_length - urb->actual_length)
1744 > qh->maxpacket)
1745 channel->desired_mode = 1;
1746 if (rx_count < hw_ep->max_packet_sz_rx) {
1747 length = rx_count;
1748 channel->desired_mode = 0;
1749 } else {
1750 length = urb->transfer_buffer_length;
1751 }
1752#endif
1753
1754 /* See comments above on disadvantages of using mode 1 */
1755 val = musb_readw(epio, MUSB_RXCSR);
1756 val &= ~MUSB_RXCSR_H_REQPKT;
1757
1758 if (channel->desired_mode == 0)
1759 val &= ~MUSB_RXCSR_H_AUTOREQ;
1760 else
1761 val |= MUSB_RXCSR_H_AUTOREQ;
1762 val |= MUSB_RXCSR_DMAENAB;
1763
1764 /* autoclear shouldn't be set in high bandwidth */
1765 if (qh->hb_mult == 1)
1766 val |= MUSB_RXCSR_AUTOCLEAR;
1767
1768 musb_writew(epio, MUSB_RXCSR, MUSB_RXCSR_H_WZC_BITS | val);
1769
1770 /* REVISIT if when actual_length != 0,
1771 * transfer_buffer_length needs to be
1772 * adjusted first...
1773 */
1774 done = dma->channel_program(channel, qh->maxpacket,
1775 channel->desired_mode,
1776 buf, length);
1777
1778 if (!done) {
1779 dma->channel_release(channel);
1780 hw_ep->rx_channel = NULL;
1781 channel = NULL;
1782 val = musb_readw(epio, MUSB_RXCSR);
1783 val &= ~(MUSB_RXCSR_DMAENAB
1784 | MUSB_RXCSR_H_AUTOREQ
1785 | MUSB_RXCSR_AUTOCLEAR);
1786 musb_writew(epio, MUSB_RXCSR, val);
1787 }
1788
1789 return done;
1790}
Tony Lindgrencff84bd2015-05-01 12:29:35 -07001791#else
1792static inline int musb_rx_dma_inventra_cppi41(struct dma_controller *dma,
1793 struct musb_hw_ep *hw_ep,
1794 struct musb_qh *qh,
1795 struct urb *urb,
1796 size_t len)
1797{
1798 return false;
1799}
Tony Lindgrenac33cdb2015-05-01 12:29:37 -07001800
1801static inline int musb_rx_dma_in_inventra_cppi41(struct dma_controller *dma,
1802 struct musb_hw_ep *hw_ep,
1803 struct musb_qh *qh,
1804 struct urb *urb,
1805 size_t len,
1806 u8 iso_err)
1807{
1808 return false;
1809}
Felipe Balbi550a7372008-07-24 12:27:36 +03001810#endif
1811
1812/*
1813 * Service an RX interrupt for the given IN endpoint; docs cover bulk, iso,
1814 * and high-bandwidth IN transfer cases.
1815 */
1816void musb_host_rx(struct musb *musb, u8 epnum)
1817{
1818 struct urb *urb;
1819 struct musb_hw_ep *hw_ep = musb->endpoints + epnum;
Tony Lindgrencff84bd2015-05-01 12:29:35 -07001820 struct dma_controller *c = musb->dma_controller;
Felipe Balbi550a7372008-07-24 12:27:36 +03001821 void __iomem *epio = hw_ep->regs;
1822 struct musb_qh *qh = hw_ep->in_qh;
1823 size_t xfer_len;
1824 void __iomem *mbase = musb->mregs;
1825 int pipe;
1826 u16 rx_csr, val;
1827 bool iso_err = false;
1828 bool done = false;
1829 u32 status;
1830 struct dma_channel *dma;
Virupax Sadashivpetimath8e8a5512012-08-07 14:46:20 +05301831 unsigned int sg_flags = SG_MITER_ATOMIC | SG_MITER_TO_SG;
Felipe Balbi550a7372008-07-24 12:27:36 +03001832
1833 musb_ep_select(mbase, epnum);
1834
1835 urb = next_urb(qh);
1836 dma = is_dma_capable() ? hw_ep->rx_channel : NULL;
1837 status = 0;
1838 xfer_len = 0;
1839
1840 rx_csr = musb_readw(epio, MUSB_RXCSR);
1841 val = rx_csr;
1842
1843 if (unlikely(!urb)) {
1844 /* REVISIT -- THIS SHOULD NEVER HAPPEN ... but, at least
1845 * usbtest #11 (unlinks) triggers it regularly, sometimes
1846 * with fifo full. (Only with DMA??)
1847 */
Bin Liub99d3652016-06-30 12:12:22 -05001848 musb_dbg(musb, "BOGUS RX%d ready, csr %04x, count %d",
1849 epnum, val, musb_readw(epio, MUSB_RXCOUNT));
Felipe Balbi550a7372008-07-24 12:27:36 +03001850 musb_h_flush_rxfifo(hw_ep, MUSB_RXCSR_CLRDATATOG);
1851 return;
1852 }
1853
1854 pipe = urb->pipe;
1855
Bin Liub99d3652016-06-30 12:12:22 -05001856 musb_dbg(musb, "<== hw %d rxcsr %04x, urb actual %d (+dma %zu)",
Felipe Balbi550a7372008-07-24 12:27:36 +03001857 epnum, rx_csr, urb->actual_length,
1858 dma ? dma->actual_len : 0);
1859
1860 /* check for errors, concurrent stall & unlink is not really
1861 * handled yet! */
1862 if (rx_csr & MUSB_RXCSR_H_RXSTALL) {
Bin Liub99d3652016-06-30 12:12:22 -05001863 musb_dbg(musb, "RX end %d STALL", epnum);
Felipe Balbi550a7372008-07-24 12:27:36 +03001864
1865 /* stall; record URB status */
1866 status = -EPIPE;
1867
1868 } else if (rx_csr & MUSB_RXCSR_H_ERROR) {
Bin Liub99d3652016-06-30 12:12:22 -05001869 musb_dbg(musb, "end %d RX proto error", epnum);
Felipe Balbi550a7372008-07-24 12:27:36 +03001870
1871 status = -EPROTO;
1872 musb_writeb(epio, MUSB_RXINTERVAL, 0);
1873
Bin Liub5801212016-05-31 10:05:03 -05001874 rx_csr &= ~MUSB_RXCSR_H_ERROR;
1875 musb_writew(epio, MUSB_RXCSR, rx_csr);
1876
Felipe Balbi550a7372008-07-24 12:27:36 +03001877 } else if (rx_csr & MUSB_RXCSR_DATAERROR) {
1878
1879 if (USB_ENDPOINT_XFER_ISOC != qh->type) {
Bin Liub99d3652016-06-30 12:12:22 -05001880 musb_dbg(musb, "RX end %d NAK timeout", epnum);
Ajay Kumar Gupta1e0320f2009-02-24 15:26:13 -08001881
1882 /* NOTE: NAKing is *NOT* an error, so we want to
1883 * continue. Except ... if there's a request for
1884 * another QH, use that instead of starving it.
1885 *
1886 * Devices like Ethernet and serial adapters keep
1887 * reads posted at all times, which will starve
1888 * other devices without this logic.
1889 */
1890 if (usb_pipebulk(urb->pipe)
1891 && qh->mux == 1
1892 && !list_is_singular(&musb->in_bulk)) {
Ajay Kumar Guptaf2838622012-07-19 13:41:59 +05301893 musb_bulk_nak_timeout(musb, hw_ep, 1);
Ajay Kumar Gupta1e0320f2009-02-24 15:26:13 -08001894 return;
1895 }
Felipe Balbi550a7372008-07-24 12:27:36 +03001896 musb_ep_select(mbase, epnum);
Ajay Kumar Gupta1e0320f2009-02-24 15:26:13 -08001897 rx_csr |= MUSB_RXCSR_H_WZC_BITS;
1898 rx_csr &= ~MUSB_RXCSR_DATAERROR;
1899 musb_writew(epio, MUSB_RXCSR, rx_csr);
Felipe Balbi550a7372008-07-24 12:27:36 +03001900
1901 goto finish;
1902 } else {
Bin Liub99d3652016-06-30 12:12:22 -05001903 musb_dbg(musb, "RX end %d ISO data error", epnum);
Felipe Balbi550a7372008-07-24 12:27:36 +03001904 /* packet error reported later */
1905 iso_err = true;
1906 }
Ajay Kumar Guptaa483d702009-04-03 16:16:17 -07001907 } else if (rx_csr & MUSB_RXCSR_INCOMPRX) {
Bin Liub99d3652016-06-30 12:12:22 -05001908 musb_dbg(musb, "end %d high bandwidth incomplete ISO packet RX",
Ajay Kumar Guptaa483d702009-04-03 16:16:17 -07001909 epnum);
1910 status = -EPROTO;
Felipe Balbi550a7372008-07-24 12:27:36 +03001911 }
1912
1913 /* faults abort the transfer */
1914 if (status) {
1915 /* clean up dma and collect transfer count */
1916 if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) {
1917 dma->status = MUSB_DMA_STATUS_CORE_ABORT;
Daniel Mack9c547692014-05-26 14:52:35 +02001918 musb->dma_controller->channel_abort(dma);
Felipe Balbi550a7372008-07-24 12:27:36 +03001919 xfer_len = dma->actual_len;
1920 }
1921 musb_h_flush_rxfifo(hw_ep, MUSB_RXCSR_CLRDATATOG);
1922 musb_writeb(epio, MUSB_RXINTERVAL, 0);
1923 done = true;
1924 goto finish;
1925 }
1926
1927 if (unlikely(dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY)) {
1928 /* SHOULD NEVER HAPPEN ... but at least DaVinci has done it */
1929 ERR("RX%d dma busy, csr %04x\n", epnum, rx_csr);
1930 goto finish;
1931 }
1932
1933 /* thorough shutdown for now ... given more precise fault handling
1934 * and better queueing support, we might keep a DMA pipeline going
1935 * while processing this irq for earlier completions.
1936 */
1937
1938 /* FIXME this is _way_ too much in-line logic for Mentor DMA */
Tony Lindgren557d5432015-05-01 12:29:34 -07001939 if (!musb_dma_inventra(musb) && !musb_dma_ux500(musb) &&
1940 (rx_csr & MUSB_RXCSR_H_REQPKT)) {
Felipe Balbi550a7372008-07-24 12:27:36 +03001941 /* REVISIT this happened for a while on some short reads...
1942 * the cleanup still needs investigation... looks bad...
1943 * and also duplicates dma cleanup code above ... plus,
1944 * shouldn't this be the "half full" double buffer case?
1945 */
1946 if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) {
1947 dma->status = MUSB_DMA_STATUS_CORE_ABORT;
Daniel Mack9c547692014-05-26 14:52:35 +02001948 musb->dma_controller->channel_abort(dma);
Felipe Balbi550a7372008-07-24 12:27:36 +03001949 xfer_len = dma->actual_len;
1950 done = true;
1951 }
1952
Bin Liub99d3652016-06-30 12:12:22 -05001953 musb_dbg(musb, "RXCSR%d %04x, reqpkt, len %zu%s", epnum, rx_csr,
Felipe Balbi550a7372008-07-24 12:27:36 +03001954 xfer_len, dma ? ", dma" : "");
1955 rx_csr &= ~MUSB_RXCSR_H_REQPKT;
1956
1957 musb_ep_select(mbase, epnum);
1958 musb_writew(epio, MUSB_RXCSR,
1959 MUSB_RXCSR_H_WZC_BITS | rx_csr);
1960 }
Tony Lindgren557d5432015-05-01 12:29:34 -07001961
Felipe Balbi550a7372008-07-24 12:27:36 +03001962 if (dma && (rx_csr & MUSB_RXCSR_DMAENAB)) {
1963 xfer_len = dma->actual_len;
1964
1965 val &= ~(MUSB_RXCSR_DMAENAB
1966 | MUSB_RXCSR_H_AUTOREQ
1967 | MUSB_RXCSR_AUTOCLEAR
1968 | MUSB_RXCSR_RXPKTRDY);
1969 musb_writew(hw_ep->regs, MUSB_RXCSR, val);
1970
Tony Lindgrencff84bd2015-05-01 12:29:35 -07001971 if (musb_dma_inventra(musb) || musb_dma_ux500(musb) ||
1972 musb_dma_cppi41(musb)) {
1973 done = musb_rx_dma_inventra_cppi41(c, hw_ep, qh, urb, xfer_len);
Bin Liub99d3652016-06-30 12:12:22 -05001974 musb_dbg(hw_ep->musb,
1975 "ep %d dma %s, rxcsr %04x, rxcount %d",
Tony Lindgrencff84bd2015-05-01 12:29:35 -07001976 epnum, done ? "off" : "reset",
1977 musb_readw(epio, MUSB_RXCSR),
1978 musb_readw(epio, MUSB_RXCOUNT));
1979 } else {
1980 done = true;
Ajay Kumar Guptaf82a6892008-10-29 15:10:31 +02001981 }
Felipe Balbi550a7372008-07-24 12:27:36 +03001982
Felipe Balbi550a7372008-07-24 12:27:36 +03001983 } else if (urb->status == -EINPROGRESS) {
1984 /* if no errors, be sure a packet is ready for unloading */
1985 if (unlikely(!(rx_csr & MUSB_RXCSR_RXPKTRDY))) {
1986 status = -EPROTO;
1987 ERR("Rx interrupt with no errors or packet!\n");
1988
1989 /* FIXME this is another "SHOULD NEVER HAPPEN" */
1990
1991/* SCRUB (RX) */
1992 /* do the proper sequence to abort the transfer */
1993 musb_ep_select(mbase, epnum);
1994 val &= ~MUSB_RXCSR_H_REQPKT;
1995 musb_writew(epio, MUSB_RXCSR, val);
1996 goto finish;
1997 }
1998
1999 /* we are expecting IN packets */
Tony Lindgrene530bb82015-05-01 12:29:36 -07002000 if ((musb_dma_inventra(musb) || musb_dma_ux500(musb) ||
2001 musb_dma_cppi41(musb)) && dma) {
Bin Liub99d3652016-06-30 12:12:22 -05002002 musb_dbg(hw_ep->musb,
2003 "RX%d count %d, buffer 0x%llx len %d/%d",
Tony Lindgrenac33cdb2015-05-01 12:29:37 -07002004 epnum, musb_readw(epio, MUSB_RXCOUNT),
2005 (unsigned long long) urb->transfer_dma
2006 + urb->actual_length,
2007 qh->offset,
2008 urb->transfer_buffer_length);
Felipe Balbi550a7372008-07-24 12:27:36 +03002009
Cristian Birsan4c2ba0c2016-02-19 10:11:56 +02002010 if (musb_rx_dma_in_inventra_cppi41(c, hw_ep, qh, urb,
2011 xfer_len, iso_err))
Tony Lindgrenac33cdb2015-05-01 12:29:37 -07002012 goto finish;
Felipe Balbi550a7372008-07-24 12:27:36 +03002013 else
Tony Lindgrenac33cdb2015-05-01 12:29:37 -07002014 dev_err(musb->controller, "error: rx_dma failed\n");
Felipe Balbi550a7372008-07-24 12:27:36 +03002015 }
Felipe Balbi550a7372008-07-24 12:27:36 +03002016
2017 if (!dma) {
Virupax Sadashivpetimath8e8a5512012-08-07 14:46:20 +05302018 unsigned int received_len;
2019
Maulik Mankad496dda72010-09-24 13:44:06 +03002020 /* Unmap the buffer so that CPU can use it */
Daniel Mack8b125df2013-04-10 21:55:50 +02002021 usb_hcd_unmap_urb_for_dma(musb->hcd, urb);
Virupax Sadashivpetimath8e8a5512012-08-07 14:46:20 +05302022
2023 /*
2024 * We need to map sg if the transfer_buffer is
2025 * NULL.
2026 */
2027 if (!urb->transfer_buffer) {
Virupax Sadashivpetimathed74df12013-04-24 08:38:48 +02002028 qh->use_sg = true;
Virupax Sadashivpetimath8e8a5512012-08-07 14:46:20 +05302029 sg_miter_start(&qh->sg_miter, urb->sg, 1,
2030 sg_flags);
2031 }
2032
Virupax Sadashivpetimathed74df12013-04-24 08:38:48 +02002033 if (qh->use_sg) {
Virupax Sadashivpetimath8e8a5512012-08-07 14:46:20 +05302034 if (!sg_miter_next(&qh->sg_miter)) {
2035 dev_err(musb->controller, "error: sg list empty\n");
2036 sg_miter_stop(&qh->sg_miter);
2037 status = -EINVAL;
2038 done = true;
2039 goto finish;
2040 }
2041 urb->transfer_buffer = qh->sg_miter.addr;
2042 received_len = urb->actual_length;
2043 qh->offset = 0x0;
2044 done = musb_host_packet_rx(musb, urb, epnum,
2045 iso_err);
2046 /* Calculate the number of bytes received */
2047 received_len = urb->actual_length -
2048 received_len;
2049 qh->sg_miter.consumed = received_len;
2050 sg_miter_stop(&qh->sg_miter);
2051 } else {
2052 done = musb_host_packet_rx(musb, urb,
2053 epnum, iso_err);
2054 }
Bin Liub99d3652016-06-30 12:12:22 -05002055 musb_dbg(musb, "read %spacket", done ? "last " : "");
Felipe Balbi550a7372008-07-24 12:27:36 +03002056 }
2057 }
2058
Felipe Balbi550a7372008-07-24 12:27:36 +03002059finish:
2060 urb->actual_length += xfer_len;
2061 qh->offset += xfer_len;
2062 if (done) {
Virupax Sadashivpetimathed74df12013-04-24 08:38:48 +02002063 if (qh->use_sg)
2064 qh->use_sg = false;
Virupax Sadashivpetimath8e8a5512012-08-07 14:46:20 +05302065
Felipe Balbi550a7372008-07-24 12:27:36 +03002066 if (urb->status == -EINPROGRESS)
2067 urb->status = status;
2068 musb_advance_schedule(musb, urb, hw_ep, USB_DIR_IN);
2069 }
2070}
2071
2072/* schedule nodes correspond to peripheral endpoints, like an OHCI QH.
2073 * the software schedule associates multiple such nodes with a given
2074 * host side hardware endpoint + direction; scheduling may activate
2075 * that hardware endpoint.
2076 */
2077static int musb_schedule(
2078 struct musb *musb,
2079 struct musb_qh *qh,
2080 int is_in)
2081{
Rickard Strandqvisteac44dc2014-06-01 15:48:12 +02002082 int idle = 0;
Felipe Balbi550a7372008-07-24 12:27:36 +03002083 int best_diff;
2084 int best_end, epnum;
2085 struct musb_hw_ep *hw_ep = NULL;
2086 struct list_head *head = NULL;
Swaminathan S5274dab2009-12-28 13:40:37 +02002087 u8 toggle;
2088 u8 txtype;
2089 struct urb *urb = next_urb(qh);
Felipe Balbi550a7372008-07-24 12:27:36 +03002090
2091 /* use fixed hardware for control and bulk */
Ajay Kumar Gupta23d15e02008-10-29 15:10:35 +02002092 if (qh->type == USB_ENDPOINT_XFER_CONTROL) {
Felipe Balbi550a7372008-07-24 12:27:36 +03002093 head = &musb->control;
2094 hw_ep = musb->control_ep;
Felipe Balbi550a7372008-07-24 12:27:36 +03002095 goto success;
2096 }
2097
2098 /* else, periodic transfers get muxed to other endpoints */
2099
Sergei Shtylyov5d67a852009-02-24 15:23:34 -08002100 /*
2101 * We know this qh hasn't been scheduled, so all we need to do
Felipe Balbi550a7372008-07-24 12:27:36 +03002102 * is choose which hardware endpoint to put it on ...
2103 *
2104 * REVISIT what we really want here is a regular schedule tree
Sergei Shtylyov5d67a852009-02-24 15:23:34 -08002105 * like e.g. OHCI uses.
Felipe Balbi550a7372008-07-24 12:27:36 +03002106 */
2107 best_diff = 4096;
2108 best_end = -1;
2109
Sergei Shtylyov5d67a852009-02-24 15:23:34 -08002110 for (epnum = 1, hw_ep = musb->endpoints + 1;
2111 epnum < musb->nr_endpoints;
2112 epnum++, hw_ep++) {
Felipe Balbi550a7372008-07-24 12:27:36 +03002113 int diff;
2114
Sergei Shtylyov3e5c6dc2009-03-27 12:55:16 -07002115 if (musb_ep_get_qh(hw_ep, is_in) != NULL)
Felipe Balbi550a7372008-07-24 12:27:36 +03002116 continue;
Sergei Shtylyov5d67a852009-02-24 15:23:34 -08002117
Felipe Balbi550a7372008-07-24 12:27:36 +03002118 if (hw_ep == musb->bulk_ep)
2119 continue;
2120
2121 if (is_in)
Ajay Kumar Guptaa483d702009-04-03 16:16:17 -07002122 diff = hw_ep->max_packet_sz_rx;
Felipe Balbi550a7372008-07-24 12:27:36 +03002123 else
Ajay Kumar Guptaa483d702009-04-03 16:16:17 -07002124 diff = hw_ep->max_packet_sz_tx;
2125 diff -= (qh->maxpacket * qh->hb_mult);
Felipe Balbi550a7372008-07-24 12:27:36 +03002126
Ajay Kumar Gupta23d15e02008-10-29 15:10:35 +02002127 if (diff >= 0 && best_diff > diff) {
Swaminathan S5274dab2009-12-28 13:40:37 +02002128
2129 /*
2130 * Mentor controller has a bug in that if we schedule
2131 * a BULK Tx transfer on an endpoint that had earlier
2132 * handled ISOC then the BULK transfer has to start on
2133 * a zero toggle. If the BULK transfer starts on a 1
2134 * toggle then this transfer will fail as the mentor
2135 * controller starts the Bulk transfer on a 0 toggle
2136 * irrespective of the programming of the toggle bits
2137 * in the TXCSR register. Check for this condition
2138 * while allocating the EP for a Tx Bulk transfer. If
2139 * so skip this EP.
2140 */
2141 hw_ep = musb->endpoints + epnum;
2142 toggle = usb_gettoggle(urb->dev, qh->epnum, !is_in);
2143 txtype = (musb_readb(hw_ep->regs, MUSB_TXTYPE)
2144 >> 4) & 0x3;
2145 if (!is_in && (qh->type == USB_ENDPOINT_XFER_BULK) &&
2146 toggle && (txtype == USB_ENDPOINT_XFER_ISOC))
2147 continue;
2148
Felipe Balbi550a7372008-07-24 12:27:36 +03002149 best_diff = diff;
2150 best_end = epnum;
2151 }
2152 }
Ajay Kumar Gupta23d15e02008-10-29 15:10:35 +02002153 /* use bulk reserved ep1 if no other ep is free */
Felipe Balbiaa5cbbe2008-11-17 09:08:16 +02002154 if (best_end < 0 && qh->type == USB_ENDPOINT_XFER_BULK) {
Ajay Kumar Gupta23d15e02008-10-29 15:10:35 +02002155 hw_ep = musb->bulk_ep;
2156 if (is_in)
2157 head = &musb->in_bulk;
2158 else
2159 head = &musb->out_bulk;
Ajay Kumar Gupta1e0320f2009-02-24 15:26:13 -08002160
Ajay Kumar Guptaf2838622012-07-19 13:41:59 +05302161 /* Enable bulk RX/TX NAK timeout scheme when bulk requests are
Rahul Bedarkar5ae477b2014-01-02 19:27:47 +05302162 * multiplexed. This scheme does not work in high speed to full
Ajay Kumar Gupta1e0320f2009-02-24 15:26:13 -08002163 * speed scenario as NAK interrupts are not coming from a
2164 * full speed device connected to a high speed device.
2165 * NAK timeout interval is 8 (128 uframe or 16ms) for HS and
2166 * 4 (8 frame or 8ms) for FS device.
2167 */
Ajay Kumar Guptaf2838622012-07-19 13:41:59 +05302168 if (qh->dev)
Ajay Kumar Gupta1e0320f2009-02-24 15:26:13 -08002169 qh->intv_reg =
2170 (USB_SPEED_HIGH == qh->dev->speed) ? 8 : 4;
Ajay Kumar Gupta23d15e02008-10-29 15:10:35 +02002171 goto success;
2172 } else if (best_end < 0) {
Felipe Balbi550a7372008-07-24 12:27:36 +03002173 return -ENOSPC;
Ajay Kumar Gupta23d15e02008-10-29 15:10:35 +02002174 }
Felipe Balbi550a7372008-07-24 12:27:36 +03002175
2176 idle = 1;
Ajay Kumar Gupta23d15e02008-10-29 15:10:35 +02002177 qh->mux = 0;
Felipe Balbi550a7372008-07-24 12:27:36 +03002178 hw_ep = musb->endpoints + best_end;
Bin Liub99d3652016-06-30 12:12:22 -05002179 musb_dbg(musb, "qh %p periodic slot %d", qh, best_end);
Felipe Balbi550a7372008-07-24 12:27:36 +03002180success:
Ajay Kumar Gupta23d15e02008-10-29 15:10:35 +02002181 if (head) {
2182 idle = list_empty(head);
2183 list_add_tail(&qh->ring, head);
2184 qh->mux = 1;
2185 }
Felipe Balbi550a7372008-07-24 12:27:36 +03002186 qh->hw_ep = hw_ep;
2187 qh->hep->hcpriv = qh;
2188 if (idle)
2189 musb_start_urb(musb, is_in, qh);
2190 return 0;
2191}
2192
2193static int musb_urb_enqueue(
2194 struct usb_hcd *hcd,
2195 struct urb *urb,
2196 gfp_t mem_flags)
2197{
2198 unsigned long flags;
2199 struct musb *musb = hcd_to_musb(hcd);
2200 struct usb_host_endpoint *hep = urb->ep;
David Brownell74bb3502009-03-26 17:36:57 -07002201 struct musb_qh *qh;
Felipe Balbi550a7372008-07-24 12:27:36 +03002202 struct usb_endpoint_descriptor *epd = &hep->desc;
2203 int ret;
2204 unsigned type_reg;
2205 unsigned interval;
2206
2207 /* host role must be active */
2208 if (!is_host_active(musb) || !musb->is_active)
2209 return -ENODEV;
2210
2211 spin_lock_irqsave(&musb->lock, flags);
2212 ret = usb_hcd_link_urb_to_ep(hcd, urb);
David Brownell74bb3502009-03-26 17:36:57 -07002213 qh = ret ? NULL : hep->hcpriv;
2214 if (qh)
2215 urb->hcpriv = qh;
Felipe Balbi550a7372008-07-24 12:27:36 +03002216 spin_unlock_irqrestore(&musb->lock, flags);
Felipe Balbi550a7372008-07-24 12:27:36 +03002217
2218 /* DMA mapping was already done, if needed, and this urb is on
David Brownell74bb3502009-03-26 17:36:57 -07002219 * hep->urb_list now ... so we're done, unless hep wasn't yet
2220 * scheduled onto a live qh.
Felipe Balbi550a7372008-07-24 12:27:36 +03002221 *
2222 * REVISIT best to keep hep->hcpriv valid until the endpoint gets
2223 * disabled, testing for empty qh->ring and avoiding qh setup costs
2224 * except for the first urb queued after a config change.
2225 */
David Brownell74bb3502009-03-26 17:36:57 -07002226 if (qh || ret)
2227 return ret;
Felipe Balbi550a7372008-07-24 12:27:36 +03002228
2229 /* Allocate and initialize qh, minimizing the work done each time
2230 * hw_ep gets reprogrammed, or with irqs blocked. Then schedule it.
2231 *
2232 * REVISIT consider a dedicated qh kmem_cache, so it's harder
2233 * for bugs in other kernel code to break this driver...
2234 */
2235 qh = kzalloc(sizeof *qh, mem_flags);
2236 if (!qh) {
Ajay Kumar Gupta2492e672008-09-11 11:53:21 +03002237 spin_lock_irqsave(&musb->lock, flags);
Felipe Balbi550a7372008-07-24 12:27:36 +03002238 usb_hcd_unlink_urb_from_ep(hcd, urb);
Ajay Kumar Gupta2492e672008-09-11 11:53:21 +03002239 spin_unlock_irqrestore(&musb->lock, flags);
Felipe Balbi550a7372008-07-24 12:27:36 +03002240 return -ENOMEM;
2241 }
2242
2243 qh->hep = hep;
2244 qh->dev = urb->dev;
2245 INIT_LIST_HEAD(&qh->ring);
2246 qh->is_ready = 1;
2247
Kuninori Morimoto29cc8892011-08-23 03:12:03 -07002248 qh->maxpacket = usb_endpoint_maxp(epd);
Ajay Kumar Guptaa483d702009-04-03 16:16:17 -07002249 qh->type = usb_endpoint_type(epd);
Felipe Balbi550a7372008-07-24 12:27:36 +03002250
Ajay Kumar Guptaa483d702009-04-03 16:16:17 -07002251 /* Bits 11 & 12 of wMaxPacketSize encode high bandwidth multiplier.
2252 * Some musb cores don't support high bandwidth ISO transfers; and
2253 * we don't (yet!) support high bandwidth interrupt transfers.
2254 */
2255 qh->hb_mult = 1 + ((qh->maxpacket >> 11) & 0x03);
2256 if (qh->hb_mult > 1) {
2257 int ok = (qh->type == USB_ENDPOINT_XFER_ISOC);
2258
2259 if (ok)
2260 ok = (usb_pipein(urb->pipe) && musb->hb_iso_rx)
2261 || (usb_pipeout(urb->pipe) && musb->hb_iso_tx);
2262 if (!ok) {
2263 ret = -EMSGSIZE;
2264 goto done;
2265 }
2266 qh->maxpacket &= 0x7ff;
Felipe Balbi550a7372008-07-24 12:27:36 +03002267 }
2268
Julia Lawall96bcd092009-01-24 17:57:24 -08002269 qh->epnum = usb_endpoint_num(epd);
Felipe Balbi550a7372008-07-24 12:27:36 +03002270
2271 /* NOTE: urb->dev->devnum is wrong during SET_ADDRESS */
2272 qh->addr_reg = (u8) usb_pipedevice(urb->pipe);
2273
2274 /* precompute rxtype/txtype/type0 register */
2275 type_reg = (qh->type << 4) | qh->epnum;
2276 switch (urb->dev->speed) {
2277 case USB_SPEED_LOW:
2278 type_reg |= 0xc0;
2279 break;
2280 case USB_SPEED_FULL:
2281 type_reg |= 0x80;
2282 break;
2283 default:
2284 type_reg |= 0x40;
2285 }
2286 qh->type_reg = type_reg;
2287
Sergei Shtylyov136733d2009-02-21 15:31:35 -08002288 /* Precompute RXINTERVAL/TXINTERVAL register */
Felipe Balbi550a7372008-07-24 12:27:36 +03002289 switch (qh->type) {
2290 case USB_ENDPOINT_XFER_INT:
Sergei Shtylyov136733d2009-02-21 15:31:35 -08002291 /*
2292 * Full/low speeds use the linear encoding,
2293 * high speed uses the logarithmic encoding.
2294 */
2295 if (urb->dev->speed <= USB_SPEED_FULL) {
2296 interval = max_t(u8, epd->bInterval, 1);
2297 break;
Felipe Balbi550a7372008-07-24 12:27:36 +03002298 }
2299 /* FALLTHROUGH */
2300 case USB_ENDPOINT_XFER_ISOC:
Sergei Shtylyov136733d2009-02-21 15:31:35 -08002301 /* ISO always uses logarithmic encoding */
2302 interval = min_t(u8, epd->bInterval, 16);
Felipe Balbi550a7372008-07-24 12:27:36 +03002303 break;
2304 default:
2305 /* REVISIT we actually want to use NAK limits, hinting to the
2306 * transfer scheduling logic to try some other qh, e.g. try
2307 * for 2 msec first:
2308 *
2309 * interval = (USB_SPEED_HIGH == urb->dev->speed) ? 16 : 2;
2310 *
2311 * The downside of disabling this is that transfer scheduling
2312 * gets VERY unfair for nonperiodic transfers; a misbehaving
Ajay Kumar Gupta1e0320f2009-02-24 15:26:13 -08002313 * peripheral could make that hurt. That's perfectly normal
2314 * for reads from network or serial adapters ... so we have
2315 * partial NAKlimit support for bulk RX.
Felipe Balbi550a7372008-07-24 12:27:36 +03002316 *
Ajay Kumar Gupta1e0320f2009-02-24 15:26:13 -08002317 * The upside of disabling it is simpler transfer scheduling.
Felipe Balbi550a7372008-07-24 12:27:36 +03002318 */
2319 interval = 0;
2320 }
2321 qh->intv_reg = interval;
2322
2323 /* precompute addressing for external hub/tt ports */
2324 if (musb->is_multipoint) {
2325 struct usb_device *parent = urb->dev->parent;
2326
2327 if (parent != hcd->self.root_hub) {
2328 qh->h_addr_reg = (u8) parent->devnum;
2329
2330 /* set up tt info if needed */
2331 if (urb->dev->tt) {
2332 qh->h_port_reg = (u8) urb->dev->ttport;
Ajay Kumar Guptaae5ad292008-09-11 11:53:20 +03002333 if (urb->dev->tt->hub)
2334 qh->h_addr_reg =
2335 (u8) urb->dev->tt->hub->devnum;
2336 if (urb->dev->tt->multi)
2337 qh->h_addr_reg |= 0x80;
Felipe Balbi550a7372008-07-24 12:27:36 +03002338 }
2339 }
2340 }
2341
2342 /* invariant: hep->hcpriv is null OR the qh that's already scheduled.
2343 * until we get real dma queues (with an entry for each urb/buffer),
2344 * we only have work to do in the former case.
2345 */
2346 spin_lock_irqsave(&musb->lock, flags);
yuzheng ma30677792012-08-15 16:11:40 +08002347 if (hep->hcpriv || !next_urb(qh)) {
Felipe Balbi550a7372008-07-24 12:27:36 +03002348 /* some concurrent activity submitted another urb to hep...
2349 * odd, rare, error prone, but legal.
2350 */
2351 kfree(qh);
Dan Carpenter714bc5e2010-03-25 13:14:27 +02002352 qh = NULL;
Felipe Balbi550a7372008-07-24 12:27:36 +03002353 ret = 0;
2354 } else
2355 ret = musb_schedule(musb, qh,
2356 epd->bEndpointAddress & USB_ENDPOINT_DIR_MASK);
2357
2358 if (ret == 0) {
2359 urb->hcpriv = qh;
2360 /* FIXME set urb->start_frame for iso/intr, it's tested in
2361 * musb_start_urb(), but otherwise only konicawc cares ...
2362 */
2363 }
2364 spin_unlock_irqrestore(&musb->lock, flags);
2365
2366done:
2367 if (ret != 0) {
Ajay Kumar Gupta2492e672008-09-11 11:53:21 +03002368 spin_lock_irqsave(&musb->lock, flags);
Felipe Balbi550a7372008-07-24 12:27:36 +03002369 usb_hcd_unlink_urb_from_ep(hcd, urb);
Ajay Kumar Gupta2492e672008-09-11 11:53:21 +03002370 spin_unlock_irqrestore(&musb->lock, flags);
Felipe Balbi550a7372008-07-24 12:27:36 +03002371 kfree(qh);
2372 }
2373 return ret;
2374}
2375
2376
2377/*
2378 * abort a transfer that's at the head of a hardware queue.
2379 * called with controller locked, irqs blocked
2380 * that hardware queue advances to the next transfer, unless prevented
2381 */
Sergei Shtylyov81ec4e42009-03-27 12:57:50 -07002382static int musb_cleanup_urb(struct urb *urb, struct musb_qh *qh)
Felipe Balbi550a7372008-07-24 12:27:36 +03002383{
2384 struct musb_hw_ep *ep = qh->hw_ep;
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03002385 struct musb *musb = ep->musb;
Felipe Balbi550a7372008-07-24 12:27:36 +03002386 void __iomem *epio = ep->regs;
2387 unsigned hw_end = ep->epnum;
2388 void __iomem *regs = ep->musb->mregs;
Sergei Shtylyov81ec4e42009-03-27 12:57:50 -07002389 int is_in = usb_pipein(urb->pipe);
Felipe Balbi550a7372008-07-24 12:27:36 +03002390 int status = 0;
Sergei Shtylyov81ec4e42009-03-27 12:57:50 -07002391 u16 csr;
Felipe Balbi550a7372008-07-24 12:27:36 +03002392
2393 musb_ep_select(regs, hw_end);
2394
2395 if (is_dma_capable()) {
2396 struct dma_channel *dma;
2397
2398 dma = is_in ? ep->rx_channel : ep->tx_channel;
2399 if (dma) {
2400 status = ep->musb->dma_controller->channel_abort(dma);
Bin Liub99d3652016-06-30 12:12:22 -05002401 musb_dbg(musb, "abort %cX%d DMA for urb %p --> %d",
Felipe Balbi550a7372008-07-24 12:27:36 +03002402 is_in ? 'R' : 'T', ep->epnum,
2403 urb, status);
2404 urb->actual_length += dma->actual_len;
2405 }
2406 }
2407
2408 /* turn off DMA requests, discard state, stop polling ... */
Ajay Kumar Gupta692933b2012-03-14 17:33:35 +05302409 if (ep->epnum && is_in) {
Felipe Balbi550a7372008-07-24 12:27:36 +03002410 /* giveback saves bulk toggle */
2411 csr = musb_h_flush_rxfifo(ep, 0);
2412
2413 /* REVISIT we still get an irq; should likely clear the
2414 * endpoint's irq status here to avoid bogus irqs.
2415 * clearing that status is platform-specific...
2416 */
David Brownell78322c12009-03-26 17:38:30 -07002417 } else if (ep->epnum) {
Felipe Balbi550a7372008-07-24 12:27:36 +03002418 musb_h_tx_flush_fifo(ep);
2419 csr = musb_readw(epio, MUSB_TXCSR);
2420 csr &= ~(MUSB_TXCSR_AUTOSET
2421 | MUSB_TXCSR_DMAENAB
2422 | MUSB_TXCSR_H_RXSTALL
2423 | MUSB_TXCSR_H_NAKTIMEOUT
2424 | MUSB_TXCSR_H_ERROR
2425 | MUSB_TXCSR_TXPKTRDY);
2426 musb_writew(epio, MUSB_TXCSR, csr);
2427 /* REVISIT may need to clear FLUSHFIFO ... */
2428 musb_writew(epio, MUSB_TXCSR, csr);
2429 /* flush cpu writebuffer */
2430 csr = musb_readw(epio, MUSB_TXCSR);
David Brownell78322c12009-03-26 17:38:30 -07002431 } else {
2432 musb_h_ep0_flush_fifo(ep);
Felipe Balbi550a7372008-07-24 12:27:36 +03002433 }
2434 if (status == 0)
2435 musb_advance_schedule(ep->musb, urb, ep, is_in);
2436 return status;
2437}
2438
2439static int musb_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
2440{
2441 struct musb *musb = hcd_to_musb(hcd);
2442 struct musb_qh *qh;
Felipe Balbi550a7372008-07-24 12:27:36 +03002443 unsigned long flags;
Sergei Shtylyov22a0d6f2009-03-27 12:56:26 -07002444 int is_in = usb_pipein(urb->pipe);
Felipe Balbi550a7372008-07-24 12:27:36 +03002445 int ret;
2446
Bin Liub99d3652016-06-30 12:12:22 -05002447 musb_dbg(musb, "urb=%p, dev%d ep%d%s", urb,
Felipe Balbi550a7372008-07-24 12:27:36 +03002448 usb_pipedevice(urb->pipe),
2449 usb_pipeendpoint(urb->pipe),
Sergei Shtylyov22a0d6f2009-03-27 12:56:26 -07002450 is_in ? "in" : "out");
Felipe Balbi550a7372008-07-24 12:27:36 +03002451
2452 spin_lock_irqsave(&musb->lock, flags);
2453 ret = usb_hcd_check_unlink_urb(hcd, urb, status);
2454 if (ret)
2455 goto done;
2456
2457 qh = urb->hcpriv;
2458 if (!qh)
2459 goto done;
2460
Sergei Shtylyov22a0d6f2009-03-27 12:56:26 -07002461 /*
2462 * Any URB not actively programmed into endpoint hardware can be
Sergei Shtylyova2fd8142009-02-21 15:30:45 -08002463 * immediately given back; that's any URB not at the head of an
Felipe Balbi550a7372008-07-24 12:27:36 +03002464 * endpoint queue, unless someday we get real DMA queues. And even
Sergei Shtylyova2fd8142009-02-21 15:30:45 -08002465 * if it's at the head, it might not be known to the hardware...
Felipe Balbi550a7372008-07-24 12:27:36 +03002466 *
Sergei Shtylyov22a0d6f2009-03-27 12:56:26 -07002467 * Otherwise abort current transfer, pending DMA, etc.; urb->status
Felipe Balbi550a7372008-07-24 12:27:36 +03002468 * has already been updated. This is a synchronous abort; it'd be
2469 * OK to hold off until after some IRQ, though.
Sergei Shtylyov22a0d6f2009-03-27 12:56:26 -07002470 *
2471 * NOTE: qh is invalid unless !list_empty(&hep->urb_list)
Felipe Balbi550a7372008-07-24 12:27:36 +03002472 */
Sergei Shtylyov22a0d6f2009-03-27 12:56:26 -07002473 if (!qh->is_ready
2474 || urb->urb_list.prev != &qh->hep->urb_list
2475 || musb_ep_get_qh(qh->hw_ep, is_in) != qh) {
Felipe Balbi550a7372008-07-24 12:27:36 +03002476 int ready = qh->is_ready;
2477
Felipe Balbi550a7372008-07-24 12:27:36 +03002478 qh->is_ready = 0;
Sergei Shtylyovc9cd06b2009-03-27 12:58:31 -07002479 musb_giveback(musb, urb, 0);
Felipe Balbi550a7372008-07-24 12:27:36 +03002480 qh->is_ready = ready;
Sergei Shtylyova2fd8142009-02-21 15:30:45 -08002481
2482 /* If nothing else (usually musb_giveback) is using it
2483 * and its URB list has emptied, recycle this qh.
2484 */
2485 if (ready && list_empty(&qh->hep->urb_list)) {
2486 qh->hep->hcpriv = NULL;
2487 list_del(&qh->ring);
2488 kfree(qh);
2489 }
Felipe Balbi550a7372008-07-24 12:27:36 +03002490 } else
Sergei Shtylyov81ec4e42009-03-27 12:57:50 -07002491 ret = musb_cleanup_urb(urb, qh);
Felipe Balbi550a7372008-07-24 12:27:36 +03002492done:
2493 spin_unlock_irqrestore(&musb->lock, flags);
2494 return ret;
2495}
2496
2497/* disable an endpoint */
2498static void
2499musb_h_disable(struct usb_hcd *hcd, struct usb_host_endpoint *hep)
2500{
Sergei Shtylyov22a0d6f2009-03-27 12:56:26 -07002501 u8 is_in = hep->desc.bEndpointAddress & USB_DIR_IN;
Felipe Balbi550a7372008-07-24 12:27:36 +03002502 unsigned long flags;
2503 struct musb *musb = hcd_to_musb(hcd);
Sergei Shtylyovdc61d232009-02-21 15:31:01 -08002504 struct musb_qh *qh;
2505 struct urb *urb;
Felipe Balbi550a7372008-07-24 12:27:36 +03002506
Felipe Balbi550a7372008-07-24 12:27:36 +03002507 spin_lock_irqsave(&musb->lock, flags);
2508
Sergei Shtylyovdc61d232009-02-21 15:31:01 -08002509 qh = hep->hcpriv;
2510 if (qh == NULL)
2511 goto exit;
2512
Sergei Shtylyov22a0d6f2009-03-27 12:56:26 -07002513 /* NOTE: qh is invalid unless !list_empty(&hep->urb_list) */
Felipe Balbi550a7372008-07-24 12:27:36 +03002514
Sergei Shtylyov22a0d6f2009-03-27 12:56:26 -07002515 /* Kick the first URB off the hardware, if needed */
Felipe Balbi550a7372008-07-24 12:27:36 +03002516 qh->is_ready = 0;
Sergei Shtylyov22a0d6f2009-03-27 12:56:26 -07002517 if (musb_ep_get_qh(qh->hw_ep, is_in) == qh) {
Felipe Balbi550a7372008-07-24 12:27:36 +03002518 urb = next_urb(qh);
2519
2520 /* make software (then hardware) stop ASAP */
2521 if (!urb->unlinked)
2522 urb->status = -ESHUTDOWN;
2523
2524 /* cleanup */
Sergei Shtylyov81ec4e42009-03-27 12:57:50 -07002525 musb_cleanup_urb(urb, qh);
Felipe Balbi550a7372008-07-24 12:27:36 +03002526
Sergei Shtylyovdc61d232009-02-21 15:31:01 -08002527 /* Then nuke all the others ... and advance the
2528 * queue on hw_ep (e.g. bulk ring) when we're done.
2529 */
2530 while (!list_empty(&hep->urb_list)) {
2531 urb = next_urb(qh);
2532 urb->status = -ESHUTDOWN;
2533 musb_advance_schedule(musb, urb, qh->hw_ep, is_in);
2534 }
2535 } else {
2536 /* Just empty the queue; the hardware is busy with
2537 * other transfers, and since !qh->is_ready nothing
2538 * will activate any of these as it advances.
2539 */
2540 while (!list_empty(&hep->urb_list))
Sergei Shtylyovc9cd06b2009-03-27 12:58:31 -07002541 musb_giveback(musb, next_urb(qh), -ESHUTDOWN);
Felipe Balbi550a7372008-07-24 12:27:36 +03002542
Sergei Shtylyovdc61d232009-02-21 15:31:01 -08002543 hep->hcpriv = NULL;
2544 list_del(&qh->ring);
2545 kfree(qh);
2546 }
2547exit:
Felipe Balbi550a7372008-07-24 12:27:36 +03002548 spin_unlock_irqrestore(&musb->lock, flags);
2549}
2550
2551static int musb_h_get_frame_number(struct usb_hcd *hcd)
2552{
2553 struct musb *musb = hcd_to_musb(hcd);
2554
2555 return musb_readw(musb->mregs, MUSB_FRAME);
2556}
2557
2558static int musb_h_start(struct usb_hcd *hcd)
2559{
2560 struct musb *musb = hcd_to_musb(hcd);
2561
2562 /* NOTE: musb_start() is called when the hub driver turns
2563 * on port power, or when (OTG) peripheral starts.
2564 */
2565 hcd->state = HC_STATE_RUNNING;
2566 musb->port1_status = 0;
2567 return 0;
2568}
2569
2570static void musb_h_stop(struct usb_hcd *hcd)
2571{
2572 musb_stop(hcd_to_musb(hcd));
2573 hcd->state = HC_STATE_HALT;
2574}
2575
2576static int musb_bus_suspend(struct usb_hcd *hcd)
2577{
2578 struct musb *musb = hcd_to_musb(hcd);
David Brownell89368d32009-07-01 03:36:16 -07002579 u8 devctl;
Felipe Balbi550a7372008-07-24 12:27:36 +03002580
Daniel Mack94f72132013-11-25 22:26:41 +01002581 musb_port_suspend(musb, true);
2582
David Brownell89368d32009-07-01 03:36:16 -07002583 if (!is_host_active(musb))
Felipe Balbi550a7372008-07-24 12:27:36 +03002584 return 0;
2585
Antoine Tenarte47d9252014-10-30 18:41:13 +01002586 switch (musb->xceiv->otg->state) {
David Brownell89368d32009-07-01 03:36:16 -07002587 case OTG_STATE_A_SUSPEND:
2588 return 0;
2589 case OTG_STATE_A_WAIT_VRISE:
2590 /* ID could be grounded even if there's no device
2591 * on the other end of the cable. NOTE that the
2592 * A_WAIT_VRISE timers are messy with MUSB...
2593 */
2594 devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
2595 if ((devctl & MUSB_DEVCTL_VBUS) == MUSB_DEVCTL_VBUS)
Antoine Tenarte47d9252014-10-30 18:41:13 +01002596 musb->xceiv->otg->state = OTG_STATE_A_WAIT_BCON;
David Brownell89368d32009-07-01 03:36:16 -07002597 break;
2598 default:
2599 break;
2600 }
2601
2602 if (musb->is_active) {
2603 WARNING("trying to suspend as %s while active\n",
Antoine Tenarte47d9252014-10-30 18:41:13 +01002604 usb_otg_state_string(musb->xceiv->otg->state));
Felipe Balbi550a7372008-07-24 12:27:36 +03002605 return -EBUSY;
2606 } else
2607 return 0;
2608}
2609
2610static int musb_bus_resume(struct usb_hcd *hcd)
2611{
Daniel Mack869c5972013-11-26 13:31:14 +01002612 struct musb *musb = hcd_to_musb(hcd);
2613
2614 if (musb->config &&
2615 musb->config->host_port_deassert_reset_at_resume)
2616 musb_port_reset(musb, false);
2617
Felipe Balbi550a7372008-07-24 12:27:36 +03002618 return 0;
2619}
2620
Ruslan Bilovol8408fd12013-03-29 19:15:21 +02002621#ifndef CONFIG_MUSB_PIO_ONLY
2622
2623#define MUSB_USB_DMA_ALIGN 4
2624
2625struct musb_temp_buffer {
2626 void *kmalloc_ptr;
2627 void *old_xfer_buffer;
2628 u8 data[0];
2629};
2630
2631static void musb_free_temp_buffer(struct urb *urb)
2632{
2633 enum dma_data_direction dir;
2634 struct musb_temp_buffer *temp;
Johan Hovoldd72348f2015-04-23 16:06:50 +02002635 size_t length;
Ruslan Bilovol8408fd12013-03-29 19:15:21 +02002636
2637 if (!(urb->transfer_flags & URB_ALIGNED_TEMP_BUFFER))
2638 return;
2639
2640 dir = usb_urb_dir_in(urb) ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
2641
2642 temp = container_of(urb->transfer_buffer, struct musb_temp_buffer,
2643 data);
2644
2645 if (dir == DMA_FROM_DEVICE) {
Johan Hovoldd72348f2015-04-23 16:06:50 +02002646 if (usb_pipeisoc(urb->pipe))
2647 length = urb->transfer_buffer_length;
2648 else
2649 length = urb->actual_length;
2650
2651 memcpy(temp->old_xfer_buffer, temp->data, length);
Ruslan Bilovol8408fd12013-03-29 19:15:21 +02002652 }
2653 urb->transfer_buffer = temp->old_xfer_buffer;
2654 kfree(temp->kmalloc_ptr);
2655
2656 urb->transfer_flags &= ~URB_ALIGNED_TEMP_BUFFER;
2657}
2658
2659static int musb_alloc_temp_buffer(struct urb *urb, gfp_t mem_flags)
2660{
2661 enum dma_data_direction dir;
2662 struct musb_temp_buffer *temp;
2663 void *kmalloc_ptr;
2664 size_t kmalloc_size;
2665
2666 if (urb->num_sgs || urb->sg ||
2667 urb->transfer_buffer_length == 0 ||
2668 !((uintptr_t)urb->transfer_buffer & (MUSB_USB_DMA_ALIGN - 1)))
2669 return 0;
2670
2671 dir = usb_urb_dir_in(urb) ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
2672
2673 /* Allocate a buffer with enough padding for alignment */
2674 kmalloc_size = urb->transfer_buffer_length +
2675 sizeof(struct musb_temp_buffer) + MUSB_USB_DMA_ALIGN - 1;
2676
2677 kmalloc_ptr = kmalloc(kmalloc_size, mem_flags);
2678 if (!kmalloc_ptr)
2679 return -ENOMEM;
2680
2681 /* Position our struct temp_buffer such that data is aligned */
2682 temp = PTR_ALIGN(kmalloc_ptr, MUSB_USB_DMA_ALIGN);
2683
2684
2685 temp->kmalloc_ptr = kmalloc_ptr;
2686 temp->old_xfer_buffer = urb->transfer_buffer;
2687 if (dir == DMA_TO_DEVICE)
2688 memcpy(temp->data, urb->transfer_buffer,
2689 urb->transfer_buffer_length);
2690 urb->transfer_buffer = temp->data;
2691
2692 urb->transfer_flags |= URB_ALIGNED_TEMP_BUFFER;
2693
2694 return 0;
2695}
2696
2697static int musb_map_urb_for_dma(struct usb_hcd *hcd, struct urb *urb,
2698 gfp_t mem_flags)
2699{
2700 struct musb *musb = hcd_to_musb(hcd);
2701 int ret;
2702
2703 /*
2704 * The DMA engine in RTL1.8 and above cannot handle
2705 * DMA addresses that are not aligned to a 4 byte boundary.
2706 * For such engine implemented (un)map_urb_for_dma hooks.
2707 * Do not use these hooks for RTL<1.8
2708 */
2709 if (musb->hwvers < MUSB_HWVERS_1800)
2710 return usb_hcd_map_urb_for_dma(hcd, urb, mem_flags);
2711
2712 ret = musb_alloc_temp_buffer(urb, mem_flags);
2713 if (ret)
2714 return ret;
2715
2716 ret = usb_hcd_map_urb_for_dma(hcd, urb, mem_flags);
2717 if (ret)
2718 musb_free_temp_buffer(urb);
2719
2720 return ret;
2721}
2722
2723static void musb_unmap_urb_for_dma(struct usb_hcd *hcd, struct urb *urb)
2724{
2725 struct musb *musb = hcd_to_musb(hcd);
2726
2727 usb_hcd_unmap_urb_for_dma(hcd, urb);
2728
2729 /* Do not use this hook for RTL<1.8 (see description above) */
2730 if (musb->hwvers < MUSB_HWVERS_1800)
2731 return;
2732
2733 musb_free_temp_buffer(urb);
2734}
2735#endif /* !CONFIG_MUSB_PIO_ONLY */
2736
Daniel Mack74c2e932013-04-10 21:55:45 +02002737static const struct hc_driver musb_hc_driver = {
Felipe Balbi550a7372008-07-24 12:27:36 +03002738 .description = "musb-hcd",
2739 .product_desc = "MUSB HDRC host driver",
Daniel Mack74c2e932013-04-10 21:55:45 +02002740 .hcd_priv_size = sizeof(struct musb *),
Bin Liuf551e132016-04-25 15:53:30 -05002741 .flags = HCD_USB2 | HCD_MEMORY,
Felipe Balbi550a7372008-07-24 12:27:36 +03002742
2743 /* not using irq handler or reset hooks from usbcore, since
2744 * those must be shared with peripheral code for OTG configs
2745 */
2746
2747 .start = musb_h_start,
2748 .stop = musb_h_stop,
2749
2750 .get_frame_number = musb_h_get_frame_number,
2751
2752 .urb_enqueue = musb_urb_enqueue,
2753 .urb_dequeue = musb_urb_dequeue,
2754 .endpoint_disable = musb_h_disable,
2755
Ruslan Bilovol8408fd12013-03-29 19:15:21 +02002756#ifndef CONFIG_MUSB_PIO_ONLY
2757 .map_urb_for_dma = musb_map_urb_for_dma,
2758 .unmap_urb_for_dma = musb_unmap_urb_for_dma,
2759#endif
2760
Felipe Balbi550a7372008-07-24 12:27:36 +03002761 .hub_status_data = musb_hub_status_data,
2762 .hub_control = musb_hub_control,
2763 .bus_suspend = musb_bus_suspend,
2764 .bus_resume = musb_bus_resume,
2765 /* .start_port_reset = NULL, */
2766 /* .hub_irq_enable = NULL, */
2767};
Daniel Mack0b3eba42013-04-10 21:55:42 +02002768
Daniel Mack74c2e932013-04-10 21:55:45 +02002769int musb_host_alloc(struct musb *musb)
2770{
2771 struct device *dev = musb->controller;
2772
2773 /* usbcore sets dev->driver_data to hcd, and sometimes uses that... */
2774 musb->hcd = usb_create_hcd(&musb_hc_driver, dev, dev_name(dev));
2775 if (!musb->hcd)
2776 return -EINVAL;
2777
2778 *musb->hcd->hcd_priv = (unsigned long) musb;
2779 musb->hcd->self.uses_pio_for_control = 1;
2780 musb->hcd->uses_new_polling = 1;
2781 musb->hcd->has_tt = 1;
2782
2783 return 0;
2784}
2785
2786void musb_host_cleanup(struct musb *musb)
2787{
Sebastian Andrzej Siewior90474282013-08-20 18:35:44 +02002788 if (musb->port_mode == MUSB_PORT_MODE_GADGET)
2789 return;
Daniel Mack74c2e932013-04-10 21:55:45 +02002790 usb_remove_hcd(musb->hcd);
Daniel Mack74c2e932013-04-10 21:55:45 +02002791}
2792
2793void musb_host_free(struct musb *musb)
2794{
2795 usb_put_hcd(musb->hcd);
2796}
2797
Daniel Mack2cc65fe2013-04-10 21:55:47 +02002798int musb_host_setup(struct musb *musb, int power_budget)
2799{
2800 int ret;
2801 struct usb_hcd *hcd = musb->hcd;
2802
2803 MUSB_HST_MODE(musb);
2804 musb->xceiv->otg->default_a = 1;
Antoine Tenarte47d9252014-10-30 18:41:13 +01002805 musb->xceiv->otg->state = OTG_STATE_A_IDLE;
Daniel Mack2cc65fe2013-04-10 21:55:47 +02002806
2807 otg_set_host(musb->xceiv->otg, &hcd->self);
2808 hcd->self.otg_port = 1;
2809 musb->xceiv->otg->host = &hcd->self;
2810 hcd->power_budget = 2 * (power_budget ? : 250);
2811
2812 ret = usb_add_hcd(hcd, 0, 0);
2813 if (ret < 0)
2814 return ret;
2815
Peter Chen3c9740a2013-11-05 10:46:02 +08002816 device_wakeup_enable(hcd->self.controller);
Daniel Mack2cc65fe2013-04-10 21:55:47 +02002817 return 0;
2818}
2819
Daniel Mack0b3eba42013-04-10 21:55:42 +02002820void musb_host_resume_root_hub(struct musb *musb)
2821{
Daniel Mack74c2e932013-04-10 21:55:45 +02002822 usb_hcd_resume_root_hub(musb->hcd);
Daniel Mack0b3eba42013-04-10 21:55:42 +02002823}
2824
2825void musb_host_poke_root_hub(struct musb *musb)
2826{
2827 MUSB_HST_MODE(musb);
Daniel Mack74c2e932013-04-10 21:55:45 +02002828 if (musb->hcd->status_urb)
2829 usb_hcd_poll_rh_status(musb->hcd);
Daniel Mack0b3eba42013-04-10 21:55:42 +02002830 else
Daniel Mack74c2e932013-04-10 21:55:45 +02002831 usb_hcd_resume_root_hub(musb->hcd);
Daniel Mack0b3eba42013-04-10 21:55:42 +02002832}