blob: f02361dbfd52ea10d4102eb7e13399552715b5c9 [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 *
134 * Better to turn on dev_dbg() in musb_cleanup_urb() with
135 * 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
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300257 dev_dbg(musb->controller, "qh %p urb %p dev%d ep%d%s%s, hw_ep %d, %p/%d\n",
Felipe Balbi550a7372008-07-24 12:27:36 +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:
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300280 dev_dbg(musb->controller, "check whether there's still time for periodic Tx\n");
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 */
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300294 dev_dbg(musb->controller, "SOF for %d\n", 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:
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300302 dev_dbg(musb->controller, "Start TX%d %s\n", 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{
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300317 dev_dbg(musb->controller,
David Brownellbb1c9ef2008-11-24 13:06:50 +0200318 "complete %p %pF (%d), dev%d ep%d%s, %d/%d\n",
319 urb, urb->complete, status,
Felipe Balbi550a7372008-07-24 12:27:36 +0300320 usb_pipedevice(urb->pipe),
321 usb_pipeendpoint(urb->pipe),
322 usb_pipein(urb->pipe) ? "in" : "out",
323 urb->actual_length, urb->transfer_buffer_length
324 );
325
Daniel Mack8b125df2013-04-10 21:55:50 +0200326 usb_hcd_unlink_urb_from_ep(musb->hcd, urb);
Felipe Balbi550a7372008-07-24 12:27:36 +0300327 spin_unlock(&musb->lock);
Daniel Mack8b125df2013-04-10 21:55:50 +0200328 usb_hcd_giveback_urb(musb->hcd, urb, status);
Felipe Balbi550a7372008-07-24 12:27:36 +0300329 spin_lock(&musb->lock);
330}
331
Sergei Shtylyov846099a2009-03-27 12:54:21 -0700332/* For bulk/interrupt endpoints only */
333static inline void musb_save_toggle(struct musb_qh *qh, int is_in,
334 struct urb *urb)
Felipe Balbi550a7372008-07-24 12:27:36 +0300335{
Sergei Shtylyov846099a2009-03-27 12:54:21 -0700336 void __iomem *epio = qh->hw_ep->regs;
Felipe Balbi550a7372008-07-24 12:27:36 +0300337 u16 csr;
Felipe Balbi550a7372008-07-24 12:27:36 +0300338
Sergei Shtylyov846099a2009-03-27 12:54:21 -0700339 /*
340 * FIXME: the current Mentor DMA code seems to have
Felipe Balbi550a7372008-07-24 12:27:36 +0300341 * problems getting toggle correct.
342 */
343
Sergei Shtylyov846099a2009-03-27 12:54:21 -0700344 if (is_in)
345 csr = musb_readw(epio, MUSB_RXCSR) & MUSB_RXCSR_H_DATATOGGLE;
Felipe Balbi550a7372008-07-24 12:27:36 +0300346 else
Sergei Shtylyov846099a2009-03-27 12:54:21 -0700347 csr = musb_readw(epio, MUSB_TXCSR) & MUSB_TXCSR_H_DATATOGGLE;
Felipe Balbi550a7372008-07-24 12:27:36 +0300348
Sergei Shtylyov846099a2009-03-27 12:54:21 -0700349 usb_settoggle(urb->dev, qh->epnum, !is_in, csr ? 1 : 0);
Felipe Balbi550a7372008-07-24 12:27:36 +0300350}
351
Sergei Shtylyovc9cd06b2009-03-27 12:58:31 -0700352/*
353 * Advance this hardware endpoint's queue, completing the specified URB and
354 * advancing to either the next URB queued to that qh, or else invalidating
355 * that qh and advancing to the next qh scheduled after the current one.
356 *
357 * Context: caller owns controller lock, IRQs are blocked
358 */
359static void musb_advance_schedule(struct musb *musb, struct urb *urb,
360 struct musb_hw_ep *hw_ep, int is_in)
Felipe Balbi550a7372008-07-24 12:27:36 +0300361{
Sergei Shtylyovc9cd06b2009-03-27 12:58:31 -0700362 struct musb_qh *qh = musb_ep_get_qh(hw_ep, is_in);
Felipe Balbi550a7372008-07-24 12:27:36 +0300363 struct musb_hw_ep *ep = qh->hw_ep;
Felipe Balbi550a7372008-07-24 12:27:36 +0300364 int ready = qh->is_ready;
Sergei Shtylyovc9cd06b2009-03-27 12:58:31 -0700365 int status;
366
367 status = (urb->status == -EINPROGRESS) ? 0 : urb->status;
Felipe Balbi550a7372008-07-24 12:27:36 +0300368
Felipe Balbi550a7372008-07-24 12:27:36 +0300369 /* save toggle eagerly, for paranoia */
370 switch (qh->type) {
371 case USB_ENDPOINT_XFER_BULK:
372 case USB_ENDPOINT_XFER_INT:
Sergei Shtylyov846099a2009-03-27 12:54:21 -0700373 musb_save_toggle(qh, is_in, urb);
Felipe Balbi550a7372008-07-24 12:27:36 +0300374 break;
375 case USB_ENDPOINT_XFER_ISOC:
Sergei Shtylyov1fe975f2009-07-10 20:02:44 +0300376 if (status == 0 && urb->error_count)
Felipe Balbi550a7372008-07-24 12:27:36 +0300377 status = -EXDEV;
378 break;
379 }
380
Felipe Balbi550a7372008-07-24 12:27:36 +0300381 qh->is_ready = 0;
Sergei Shtylyovc9cd06b2009-03-27 12:58:31 -0700382 musb_giveback(musb, urb, status);
Felipe Balbi550a7372008-07-24 12:27:36 +0300383 qh->is_ready = ready;
384
385 /* reclaim resources (and bandwidth) ASAP; deschedule it, and
386 * invalidate qh as soon as list_empty(&hep->urb_list)
387 */
388 if (list_empty(&qh->hep->urb_list)) {
389 struct list_head *head;
Ajay Kumar Gupta8c778db2012-06-21 17:18:12 +0530390 struct dma_controller *dma = musb->dma_controller;
Felipe Balbi550a7372008-07-24 12:27:36 +0300391
Ajay Kumar Gupta8c778db2012-06-21 17:18:12 +0530392 if (is_in) {
Felipe Balbi550a7372008-07-24 12:27:36 +0300393 ep->rx_reinit = 1;
Ajay Kumar Gupta8c778db2012-06-21 17:18:12 +0530394 if (ep->rx_channel) {
395 dma->channel_release(ep->rx_channel);
396 ep->rx_channel = NULL;
397 }
398 } else {
Felipe Balbi550a7372008-07-24 12:27:36 +0300399 ep->tx_reinit = 1;
Ajay Kumar Gupta8c778db2012-06-21 17:18:12 +0530400 if (ep->tx_channel) {
401 dma->channel_release(ep->tx_channel);
402 ep->tx_channel = NULL;
403 }
404 }
Felipe Balbi550a7372008-07-24 12:27:36 +0300405
Sergei Shtylyov3e5c6dc2009-03-27 12:55:16 -0700406 /* Clobber old pointers to this qh */
407 musb_ep_set_qh(ep, is_in, NULL);
Felipe Balbi550a7372008-07-24 12:27:36 +0300408 qh->hep->hcpriv = NULL;
409
410 switch (qh->type) {
411
Ajay Kumar Gupta23d15e02008-10-29 15:10:35 +0200412 case USB_ENDPOINT_XFER_CONTROL:
413 case USB_ENDPOINT_XFER_BULK:
414 /* fifo policy for these lists, except that NAKing
415 * should rotate a qh to the end (for fairness).
416 */
417 if (qh->mux == 1) {
418 head = qh->ring.prev;
419 list_del(&qh->ring);
420 kfree(qh);
421 qh = first_qh(head);
422 break;
423 }
424
Felipe Balbi550a7372008-07-24 12:27:36 +0300425 case USB_ENDPOINT_XFER_ISOC:
426 case USB_ENDPOINT_XFER_INT:
427 /* this is where periodic bandwidth should be
428 * de-allocated if it's tracked and allocated;
429 * and where we'd update the schedule tree...
430 */
Felipe Balbi550a7372008-07-24 12:27:36 +0300431 kfree(qh);
432 qh = NULL;
433 break;
Felipe Balbi550a7372008-07-24 12:27:36 +0300434 }
435 }
Felipe Balbi550a7372008-07-24 12:27:36 +0300436
Bin Liudbac5d02016-05-31 10:05:04 -0500437 /*
438 * The pipe must be broken if current urb->status is set, so don't
439 * start next urb.
440 * TODO: to minimize the risk of regression, only check urb->status
441 * for RX, until we have a test case to understand the behavior of TX.
442 */
443 if ((!status || !is_in) && qh && qh->is_ready) {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300444 dev_dbg(musb->controller, "... next ep%d %cX urb %p\n",
Sergei Shtylyovc9cd06b2009-03-27 12:58:31 -0700445 hw_ep->epnum, is_in ? 'R' : 'T', next_urb(qh));
Felipe Balbi550a7372008-07-24 12:27:36 +0300446 musb_start_urb(musb, is_in, qh);
447 }
448}
449
David Brownellc767c1c2008-09-11 11:53:23 +0300450static u16 musb_h_flush_rxfifo(struct musb_hw_ep *hw_ep, u16 csr)
Felipe Balbi550a7372008-07-24 12:27:36 +0300451{
452 /* we don't want fifo to fill itself again;
453 * ignore dma (various models),
454 * leave toggle alone (may not have been saved yet)
455 */
456 csr |= MUSB_RXCSR_FLUSHFIFO | MUSB_RXCSR_RXPKTRDY;
457 csr &= ~(MUSB_RXCSR_H_REQPKT
458 | MUSB_RXCSR_H_AUTOREQ
459 | MUSB_RXCSR_AUTOCLEAR);
460
461 /* write 2x to allow double buffering */
462 musb_writew(hw_ep->regs, MUSB_RXCSR, csr);
463 musb_writew(hw_ep->regs, MUSB_RXCSR, csr);
464
465 /* flush writebuffer */
466 return musb_readw(hw_ep->regs, MUSB_RXCSR);
467}
468
469/*
470 * PIO RX for a packet (or part of it).
471 */
472static bool
473musb_host_packet_rx(struct musb *musb, struct urb *urb, u8 epnum, u8 iso_err)
474{
475 u16 rx_count;
476 u8 *buf;
477 u16 csr;
478 bool done = false;
479 u32 length;
480 int do_flush = 0;
481 struct musb_hw_ep *hw_ep = musb->endpoints + epnum;
482 void __iomem *epio = hw_ep->regs;
483 struct musb_qh *qh = hw_ep->in_qh;
484 int pipe = urb->pipe;
485 void *buffer = urb->transfer_buffer;
486
487 /* musb_ep_select(mbase, epnum); */
488 rx_count = musb_readw(epio, MUSB_RXCOUNT);
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300489 dev_dbg(musb->controller, "RX%d count %d, buffer %p len %d/%d\n", epnum, rx_count,
Felipe Balbi550a7372008-07-24 12:27:36 +0300490 urb->transfer_buffer, qh->offset,
491 urb->transfer_buffer_length);
492
493 /* unload FIFO */
494 if (usb_pipeisoc(pipe)) {
495 int status = 0;
496 struct usb_iso_packet_descriptor *d;
497
498 if (iso_err) {
499 status = -EILSEQ;
500 urb->error_count++;
501 }
502
503 d = urb->iso_frame_desc + qh->iso_idx;
504 buf = buffer + d->offset;
505 length = d->length;
506 if (rx_count > length) {
507 if (status == 0) {
508 status = -EOVERFLOW;
509 urb->error_count++;
510 }
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300511 dev_dbg(musb->controller, "** OVERFLOW %d into %d\n", rx_count, length);
Felipe Balbi550a7372008-07-24 12:27:36 +0300512 do_flush = 1;
513 } else
514 length = rx_count;
515 urb->actual_length += length;
516 d->actual_length = length;
517
518 d->status = status;
519
520 /* see if we are done */
521 done = (++qh->iso_idx >= urb->number_of_packets);
522 } else {
523 /* non-isoch */
524 buf = buffer + qh->offset;
525 length = urb->transfer_buffer_length - qh->offset;
526 if (rx_count > length) {
527 if (urb->status == -EINPROGRESS)
528 urb->status = -EOVERFLOW;
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300529 dev_dbg(musb->controller, "** OVERFLOW %d into %d\n", rx_count, length);
Felipe Balbi550a7372008-07-24 12:27:36 +0300530 do_flush = 1;
531 } else
532 length = rx_count;
533 urb->actual_length += length;
534 qh->offset += length;
535
536 /* see if we are done */
537 done = (urb->actual_length == urb->transfer_buffer_length)
538 || (rx_count < qh->maxpacket)
539 || (urb->status != -EINPROGRESS);
540 if (done
541 && (urb->status == -EINPROGRESS)
542 && (urb->transfer_flags & URB_SHORT_NOT_OK)
543 && (urb->actual_length
544 < urb->transfer_buffer_length))
545 urb->status = -EREMOTEIO;
546 }
547
548 musb_read_fifo(hw_ep, length, buf);
549
550 csr = musb_readw(epio, MUSB_RXCSR);
551 csr |= MUSB_RXCSR_H_WZC_BITS;
552 if (unlikely(do_flush))
553 musb_h_flush_rxfifo(hw_ep, csr);
554 else {
555 /* REVISIT this assumes AUTOCLEAR is never set */
556 csr &= ~(MUSB_RXCSR_RXPKTRDY | MUSB_RXCSR_H_REQPKT);
557 if (!done)
558 csr |= MUSB_RXCSR_H_REQPKT;
559 musb_writew(epio, MUSB_RXCSR, csr);
560 }
561
562 return done;
563}
564
565/* we don't always need to reinit a given side of an endpoint...
566 * when we do, use tx/rx reinit routine and then construct a new CSR
567 * to address data toggle, NYET, and DMA or PIO.
568 *
569 * it's possible that driver bugs (especially for DMA) or aborting a
570 * transfer might have left the endpoint busier than it should be.
571 * the busy/not-empty tests are basically paranoia.
572 */
573static void
Hans de Goede0cb74b32015-03-20 20:11:11 +0100574musb_rx_reinit(struct musb *musb, struct musb_qh *qh, u8 epnum)
Felipe Balbi550a7372008-07-24 12:27:36 +0300575{
Hans de Goede0cb74b32015-03-20 20:11:11 +0100576 struct musb_hw_ep *ep = musb->endpoints + epnum;
Felipe Balbi550a7372008-07-24 12:27:36 +0300577 u16 csr;
578
579 /* NOTE: we know the "rx" fifo reinit never triggers for ep0.
580 * That always uses tx_reinit since ep0 repurposes TX register
581 * offsets; the initial SETUP packet is also a kind of OUT.
582 */
583
584 /* if programmed for Tx, put it in RX mode */
585 if (ep->is_shared_fifo) {
586 csr = musb_readw(ep->regs, MUSB_TXCSR);
587 if (csr & MUSB_TXCSR_MODE) {
588 musb_h_tx_flush_fifo(ep);
Sergei Shtylyovb6e434a2009-03-26 18:27:47 -0700589 csr = musb_readw(ep->regs, MUSB_TXCSR);
Felipe Balbi550a7372008-07-24 12:27:36 +0300590 musb_writew(ep->regs, MUSB_TXCSR,
Sergei Shtylyovb6e434a2009-03-26 18:27:47 -0700591 csr | MUSB_TXCSR_FRCDATATOG);
Felipe Balbi550a7372008-07-24 12:27:36 +0300592 }
Sergei Shtylyovb6e434a2009-03-26 18:27:47 -0700593
594 /*
595 * Clear the MODE bit (and everything else) to enable Rx.
596 * NOTE: we mustn't clear the DMAMODE bit before DMAENAB.
597 */
598 if (csr & MUSB_TXCSR_DMAMODE)
599 musb_writew(ep->regs, MUSB_TXCSR, MUSB_TXCSR_DMAMODE);
Felipe Balbi550a7372008-07-24 12:27:36 +0300600 musb_writew(ep->regs, MUSB_TXCSR, 0);
601
602 /* scrub all previous state, clearing toggle */
603 } else {
604 csr = musb_readw(ep->regs, MUSB_RXCSR);
605 if (csr & MUSB_RXCSR_RXPKTRDY)
606 WARNING("rx%d, packet/%d ready?\n", ep->epnum,
607 musb_readw(ep->regs, MUSB_RXCOUNT));
608
609 musb_h_flush_rxfifo(ep, MUSB_RXCSR_CLRDATATOG);
610 }
611
612 /* target addr and (for multipoint) hub addr/port */
613 if (musb->is_multipoint) {
Hans de Goede6cc2af62015-03-20 20:11:12 +0100614 musb_write_rxfunaddr(musb, epnum, qh->addr_reg);
615 musb_write_rxhubaddr(musb, epnum, qh->h_addr_reg);
616 musb_write_rxhubport(musb, epnum, qh->h_port_reg);
Felipe Balbi550a7372008-07-24 12:27:36 +0300617 } else
618 musb_writeb(musb->mregs, MUSB_FADDR, qh->addr_reg);
619
620 /* protocol/endpoint, interval/NAKlimit, i/o size */
621 musb_writeb(ep->regs, MUSB_RXTYPE, qh->type_reg);
622 musb_writeb(ep->regs, MUSB_RXINTERVAL, qh->intv_reg);
623 /* NOTE: bulk combining rewrites high bits of maxpacket */
Cliff Cai9f445cb2010-01-28 20:44:18 -0500624 /* Set RXMAXP with the FIFO size of the endpoint
625 * to disable double buffer mode.
626 */
Felipe Balbi06624812011-01-21 13:39:20 +0800627 if (musb->double_buffer_not_ok)
Cliff Cai9f445cb2010-01-28 20:44:18 -0500628 musb_writew(ep->regs, MUSB_RXMAXP, ep->max_packet_sz_rx);
629 else
630 musb_writew(ep->regs, MUSB_RXMAXP,
631 qh->maxpacket | ((qh->hb_mult - 1) << 11));
Felipe Balbi550a7372008-07-24 12:27:36 +0300632
633 ep->rx_reinit = 0;
634}
635
Sergei Shtylyovb6a66312016-05-31 10:05:06 -0500636static void musb_tx_dma_set_mode_mentor(struct dma_controller *dma,
Sergei Shtylyov6b6e9712009-03-26 18:29:19 -0700637 struct musb_hw_ep *hw_ep, struct musb_qh *qh,
Tony Lindgren754fe4a2015-05-01 12:29:32 -0700638 struct urb *urb, u32 offset,
639 u32 *length, u8 *mode)
Sergei Shtylyov6b6e9712009-03-26 18:29:19 -0700640{
641 struct dma_channel *channel = hw_ep->tx_channel;
642 void __iomem *epio = hw_ep->regs;
643 u16 pkt_size = qh->maxpacket;
644 u16 csr;
Sergei Shtylyov6b6e9712009-03-26 18:29:19 -0700645
Tony Lindgren754fe4a2015-05-01 12:29:32 -0700646 if (*length > channel->max_len)
647 *length = channel->max_len;
Sergei Shtylyov6b6e9712009-03-26 18:29:19 -0700648
649 csr = musb_readw(epio, MUSB_TXCSR);
Tony Lindgren754fe4a2015-05-01 12:29:32 -0700650 if (*length > pkt_size) {
651 *mode = 1;
Ajay Kumar Guptaa483d702009-04-03 16:16:17 -0700652 csr |= MUSB_TXCSR_DMAMODE | MUSB_TXCSR_DMAENAB;
653 /* autoset shouldn't be set in high bandwidth */
supriya karanthf2786282012-12-06 11:16:23 +0530654 /*
655 * Enable Autoset according to table
656 * below
657 * bulk_split hb_mult Autoset_Enable
658 * 0 1 Yes(Normal)
659 * 0 >1 No(High BW ISO)
660 * 1 1 Yes(HS bulk)
661 * 1 >1 Yes(FS bulk)
662 */
663 if (qh->hb_mult == 1 || (qh->hb_mult > 1 &&
664 can_bulk_split(hw_ep->musb, qh->type)))
Ajay Kumar Guptaa483d702009-04-03 16:16:17 -0700665 csr |= MUSB_TXCSR_AUTOSET;
Sergei Shtylyov6b6e9712009-03-26 18:29:19 -0700666 } else {
Tony Lindgren754fe4a2015-05-01 12:29:32 -0700667 *mode = 0;
Sergei Shtylyov6b6e9712009-03-26 18:29:19 -0700668 csr &= ~(MUSB_TXCSR_AUTOSET | MUSB_TXCSR_DMAMODE);
669 csr |= MUSB_TXCSR_DMAENAB; /* against programmer's guide */
670 }
Cristian Birsanbba40e62016-02-11 08:58:17 -0700671 channel->desired_mode = *mode;
Sergei Shtylyov6b6e9712009-03-26 18:29:19 -0700672 musb_writew(epio, MUSB_TXCSR, csr);
Tony Lindgren754fe4a2015-05-01 12:29:32 -0700673}
674
Sergei Shtylyovb6a66312016-05-31 10:05:06 -0500675static void musb_tx_dma_set_mode_cppi_tusb(struct dma_controller *dma,
676 struct musb_hw_ep *hw_ep,
677 struct musb_qh *qh,
678 struct urb *urb,
679 u32 offset,
680 u32 *length,
681 u8 *mode)
Tony Lindgren754fe4a2015-05-01 12:29:32 -0700682{
683 struct dma_channel *channel = hw_ep->tx_channel;
684
Sergei Shtylyov6b6e9712009-03-26 18:29:19 -0700685 channel->actual_len = 0;
686
687 /*
688 * TX uses "RNDIS" mode automatically but needs help
689 * to identify the zero-length-final-packet case.
690 */
Tony Lindgren754fe4a2015-05-01 12:29:32 -0700691 *mode = (urb->transfer_flags & URB_ZERO_PACKET) ? 1 : 0;
Tony Lindgren754fe4a2015-05-01 12:29:32 -0700692}
693
694static bool musb_tx_dma_program(struct dma_controller *dma,
695 struct musb_hw_ep *hw_ep, struct musb_qh *qh,
696 struct urb *urb, u32 offset, u32 length)
697{
698 struct dma_channel *channel = hw_ep->tx_channel;
699 u16 pkt_size = qh->maxpacket;
700 u8 mode;
Tony Lindgren754fe4a2015-05-01 12:29:32 -0700701
702 if (musb_dma_inventra(hw_ep->musb) || musb_dma_ux500(hw_ep->musb))
Sergei Shtylyovb6a66312016-05-31 10:05:06 -0500703 musb_tx_dma_set_mode_mentor(dma, hw_ep, qh, urb, offset,
704 &length, &mode);
Sergei Shtylyov858b9be2016-05-31 10:05:05 -0500705 else if (is_cppi_enabled(hw_ep->musb) || tusb_dma_omap(hw_ep->musb))
Sergei Shtylyovb6a66312016-05-31 10:05:06 -0500706 musb_tx_dma_set_mode_cppi_tusb(dma, hw_ep, qh, urb, offset,
707 &length, &mode);
Sergei Shtylyov858b9be2016-05-31 10:05:05 -0500708 else
709 return false;
Sergei Shtylyov6b6e9712009-03-26 18:29:19 -0700710
711 qh->segsize = length;
712
Santosh Shilimkar4c647332010-09-20 10:32:07 +0300713 /*
714 * Ensure the data reaches to main memory before starting
715 * DMA transfer
716 */
717 wmb();
718
Sergei Shtylyov6b6e9712009-03-26 18:29:19 -0700719 if (!dma->channel_program(channel, pkt_size, mode,
720 urb->transfer_dma + offset, length)) {
Tony Lindgren754fe4a2015-05-01 12:29:32 -0700721 void __iomem *epio = hw_ep->regs;
722 u16 csr;
723
Sergei Shtylyov6b6e9712009-03-26 18:29:19 -0700724 dma->channel_release(channel);
725 hw_ep->tx_channel = NULL;
726
727 csr = musb_readw(epio, MUSB_TXCSR);
728 csr &= ~(MUSB_TXCSR_AUTOSET | MUSB_TXCSR_DMAENAB);
729 musb_writew(epio, MUSB_TXCSR, csr | MUSB_TXCSR_H_WZC_BITS);
730 return false;
731 }
732 return true;
733}
Felipe Balbi550a7372008-07-24 12:27:36 +0300734
735/*
736 * Program an HDRC endpoint as per the given URB
737 * Context: irqs blocked, controller lock held
738 */
739static void musb_ep_program(struct musb *musb, u8 epnum,
Sergei Shtylyov6b6e9712009-03-26 18:29:19 -0700740 struct urb *urb, int is_out,
741 u8 *buf, u32 offset, u32 len)
Felipe Balbi550a7372008-07-24 12:27:36 +0300742{
743 struct dma_controller *dma_controller;
744 struct dma_channel *dma_channel;
745 u8 dma_ok;
746 void __iomem *mbase = musb->mregs;
747 struct musb_hw_ep *hw_ep = musb->endpoints + epnum;
748 void __iomem *epio = hw_ep->regs;
Sergei Shtylyov3e5c6dc2009-03-27 12:55:16 -0700749 struct musb_qh *qh = musb_ep_get_qh(hw_ep, !is_out);
750 u16 packet_sz = qh->maxpacket;
Ajay Kumar Gupta31321222012-07-20 11:07:22 +0530751 u8 use_dma = 1;
752 u16 csr;
Felipe Balbi550a7372008-07-24 12:27:36 +0300753
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300754 dev_dbg(musb->controller, "%s hw%d urb %p spd%d dev%d ep%d%s "
Felipe Balbi550a7372008-07-24 12:27:36 +0300755 "h_addr%02x h_port%02x bytes %d\n",
756 is_out ? "-->" : "<--",
757 epnum, urb, urb->dev->speed,
758 qh->addr_reg, qh->epnum, is_out ? "out" : "in",
759 qh->h_addr_reg, qh->h_port_reg,
760 len);
761
762 musb_ep_select(mbase, epnum);
763
Ajay Kumar Gupta31321222012-07-20 11:07:22 +0530764 if (is_out && !len) {
765 use_dma = 0;
766 csr = musb_readw(epio, MUSB_TXCSR);
767 csr &= ~MUSB_TXCSR_DMAENAB;
768 musb_writew(epio, MUSB_TXCSR, csr);
769 hw_ep->tx_channel = NULL;
770 }
771
Felipe Balbi550a7372008-07-24 12:27:36 +0300772 /* candidate for DMA? */
773 dma_controller = musb->dma_controller;
Ajay Kumar Gupta31321222012-07-20 11:07:22 +0530774 if (use_dma && is_dma_capable() && epnum && dma_controller) {
Felipe Balbi550a7372008-07-24 12:27:36 +0300775 dma_channel = is_out ? hw_ep->tx_channel : hw_ep->rx_channel;
776 if (!dma_channel) {
777 dma_channel = dma_controller->channel_alloc(
778 dma_controller, hw_ep, is_out);
779 if (is_out)
780 hw_ep->tx_channel = dma_channel;
781 else
782 hw_ep->rx_channel = dma_channel;
783 }
784 } else
785 dma_channel = NULL;
786
787 /* make sure we clear DMAEnab, autoSet bits from previous run */
788
789 /* OUT/transmit/EP0 or IN/receive? */
790 if (is_out) {
791 u16 csr;
792 u16 int_txe;
793 u16 load_count;
794
795 csr = musb_readw(epio, MUSB_TXCSR);
796
797 /* disable interrupt in case we flush */
Sebastian Andrzej Siewiorb18d26f2012-10-30 19:52:26 +0100798 int_txe = musb->intrtxe;
Felipe Balbi550a7372008-07-24 12:27:36 +0300799 musb_writew(mbase, MUSB_INTRTXE, int_txe & ~(1 << epnum));
800
801 /* general endpoint setup */
802 if (epnum) {
Felipe Balbi550a7372008-07-24 12:27:36 +0300803 /* flush all old state, set default */
supriya karantha70b8442013-01-04 17:10:33 +0530804 /*
805 * We could be flushing valid
806 * packets in double buffering
807 * case
808 */
809 if (!hw_ep->tx_double_buffered)
810 musb_h_tx_flush_fifo(hw_ep);
Sergei Shtylyovb6e434a2009-03-26 18:27:47 -0700811
812 /*
813 * We must not clear the DMAMODE bit before or in
814 * the same cycle with the DMAENAB bit, so we clear
815 * the latter first...
816 */
Felipe Balbi550a7372008-07-24 12:27:36 +0300817 csr &= ~(MUSB_TXCSR_H_NAKTIMEOUT
Sergei Shtylyovb6e434a2009-03-26 18:27:47 -0700818 | MUSB_TXCSR_AUTOSET
819 | MUSB_TXCSR_DMAENAB
Felipe Balbi550a7372008-07-24 12:27:36 +0300820 | MUSB_TXCSR_FRCDATATOG
821 | MUSB_TXCSR_H_RXSTALL
822 | MUSB_TXCSR_H_ERROR
823 | MUSB_TXCSR_TXPKTRDY
824 );
825 csr |= MUSB_TXCSR_MODE;
826
supriya karantha70b8442013-01-04 17:10:33 +0530827 if (!hw_ep->tx_double_buffered) {
828 if (usb_gettoggle(urb->dev, qh->epnum, 1))
829 csr |= MUSB_TXCSR_H_WR_DATATOGGLE
830 | MUSB_TXCSR_H_DATATOGGLE;
831 else
832 csr |= MUSB_TXCSR_CLRDATATOG;
833 }
Felipe Balbi550a7372008-07-24 12:27:36 +0300834
Felipe Balbi550a7372008-07-24 12:27:36 +0300835 musb_writew(epio, MUSB_TXCSR, csr);
836 /* REVISIT may need to clear FLUSHFIFO ... */
Sergei Shtylyovb6e434a2009-03-26 18:27:47 -0700837 csr &= ~MUSB_TXCSR_DMAMODE;
Felipe Balbi550a7372008-07-24 12:27:36 +0300838 musb_writew(epio, MUSB_TXCSR, csr);
839 csr = musb_readw(epio, MUSB_TXCSR);
840 } else {
841 /* endpoint 0: just flush */
David Brownell78322c12009-03-26 17:38:30 -0700842 musb_h_ep0_flush_fifo(hw_ep);
Felipe Balbi550a7372008-07-24 12:27:36 +0300843 }
844
845 /* target addr and (for multipoint) hub addr/port */
846 if (musb->is_multipoint) {
Hans de Goede6cc2af62015-03-20 20:11:12 +0100847 musb_write_txfunaddr(musb, epnum, qh->addr_reg);
848 musb_write_txhubaddr(musb, epnum, qh->h_addr_reg);
849 musb_write_txhubport(musb, epnum, qh->h_port_reg);
Felipe Balbi550a7372008-07-24 12:27:36 +0300850/* FIXME if !epnum, do the same for RX ... */
851 } else
852 musb_writeb(mbase, MUSB_FADDR, qh->addr_reg);
853
854 /* protocol/endpoint/interval/NAKlimit */
855 if (epnum) {
856 musb_writeb(epio, MUSB_TXTYPE, qh->type_reg);
supriya karanthf2786282012-12-06 11:16:23 +0530857 if (musb->double_buffer_not_ok) {
Felipe Balbi550a7372008-07-24 12:27:36 +0300858 musb_writew(epio, MUSB_TXMAXP,
Felipe Balbi06624812011-01-21 13:39:20 +0800859 hw_ep->max_packet_sz_tx);
supriya karanthf2786282012-12-06 11:16:23 +0530860 } else if (can_bulk_split(musb, qh->type)) {
861 qh->hb_mult = hw_ep->max_packet_sz_tx
862 / packet_sz;
Ajay Kumar Guptaccc080c2011-12-13 10:32:42 +0530863 musb_writew(epio, MUSB_TXMAXP, packet_sz
supriya karanthf2786282012-12-06 11:16:23 +0530864 | ((qh->hb_mult) - 1) << 11);
865 } else {
Felipe Balbi550a7372008-07-24 12:27:36 +0300866 musb_writew(epio, MUSB_TXMAXP,
Felipe Balbi06624812011-01-21 13:39:20 +0800867 qh->maxpacket |
868 ((qh->hb_mult - 1) << 11));
supriya karanthf2786282012-12-06 11:16:23 +0530869 }
Felipe Balbi550a7372008-07-24 12:27:36 +0300870 musb_writeb(epio, MUSB_TXINTERVAL, qh->intv_reg);
871 } else {
872 musb_writeb(epio, MUSB_NAKLIMIT0, qh->intv_reg);
873 if (musb->is_multipoint)
874 musb_writeb(epio, MUSB_TYPE0,
875 qh->type_reg);
876 }
877
878 if (can_bulk_split(musb, qh->type))
879 load_count = min((u32) hw_ep->max_packet_sz_tx,
880 len);
881 else
882 load_count = min((u32) packet_sz, len);
883
Sergei Shtylyov6b6e9712009-03-26 18:29:19 -0700884 if (dma_channel && musb_tx_dma_program(dma_controller,
885 hw_ep, qh, urb, offset, len))
886 load_count = 0;
Felipe Balbi550a7372008-07-24 12:27:36 +0300887
888 if (load_count) {
Felipe Balbi550a7372008-07-24 12:27:36 +0300889 /* PIO to load FIFO */
890 qh->segsize = load_count;
Virupax Sadashivpetimath8e8a5512012-08-07 14:46:20 +0530891 if (!buf) {
892 sg_miter_start(&qh->sg_miter, urb->sg, 1,
893 SG_MITER_ATOMIC
894 | SG_MITER_FROM_SG);
895 if (!sg_miter_next(&qh->sg_miter)) {
896 dev_err(musb->controller,
897 "error: sg"
898 "list empty\n");
899 sg_miter_stop(&qh->sg_miter);
900 goto finish;
901 }
902 buf = qh->sg_miter.addr + urb->sg->offset +
903 urb->actual_length;
904 load_count = min_t(u32, load_count,
905 qh->sg_miter.length);
906 musb_write_fifo(hw_ep, load_count, buf);
907 qh->sg_miter.consumed = load_count;
908 sg_miter_stop(&qh->sg_miter);
909 } else
910 musb_write_fifo(hw_ep, load_count, buf);
Felipe Balbi550a7372008-07-24 12:27:36 +0300911 }
Virupax Sadashivpetimath8e8a5512012-08-07 14:46:20 +0530912finish:
Felipe Balbi550a7372008-07-24 12:27:36 +0300913 /* re-enable interrupt */
914 musb_writew(mbase, MUSB_INTRTXE, int_txe);
915
916 /* IN/receive */
917 } else {
918 u16 csr;
919
920 if (hw_ep->rx_reinit) {
Hans de Goede0cb74b32015-03-20 20:11:11 +0100921 musb_rx_reinit(musb, qh, epnum);
Felipe Balbi550a7372008-07-24 12:27:36 +0300922
923 /* init new state: toggle and NYET, maybe DMA later */
924 if (usb_gettoggle(urb->dev, qh->epnum, 0))
925 csr = MUSB_RXCSR_H_WR_DATATOGGLE
926 | MUSB_RXCSR_H_DATATOGGLE;
927 else
928 csr = 0;
929 if (qh->type == USB_ENDPOINT_XFER_INT)
930 csr |= MUSB_RXCSR_DISNYET;
931
932 } else {
933 csr = musb_readw(hw_ep->regs, MUSB_RXCSR);
934
935 if (csr & (MUSB_RXCSR_RXPKTRDY
936 | MUSB_RXCSR_DMAENAB
937 | MUSB_RXCSR_H_REQPKT))
938 ERR("broken !rx_reinit, ep%d csr %04x\n",
939 hw_ep->epnum, csr);
940
941 /* scrub any stale state, leaving toggle alone */
942 csr &= MUSB_RXCSR_DISNYET;
943 }
944
945 /* kick things off */
946
Tony Lindgrenf8e9f34f2015-05-01 12:29:27 -0700947 if ((is_cppi_enabled(musb) || tusb_dma_omap(musb)) && dma_channel) {
Sergei Shtylyovc51e36d2011-05-07 19:44:13 +0400948 /* Candidate for DMA */
949 dma_channel->actual_len = 0L;
950 qh->segsize = len;
Felipe Balbi550a7372008-07-24 12:27:36 +0300951
Sergei Shtylyovc51e36d2011-05-07 19:44:13 +0400952 /* AUTOREQ is in a DMA register */
953 musb_writew(hw_ep->regs, MUSB_RXCSR, csr);
954 csr = musb_readw(hw_ep->regs, MUSB_RXCSR);
Felipe Balbi550a7372008-07-24 12:27:36 +0300955
Sergei Shtylyovc51e36d2011-05-07 19:44:13 +0400956 /*
957 * Unless caller treats short RX transfers as
958 * errors, we dare not queue multiple transfers.
959 */
960 dma_ok = dma_controller->channel_program(dma_channel,
961 packet_sz, !(urb->transfer_flags &
962 URB_SHORT_NOT_OK),
963 urb->transfer_dma + offset,
964 qh->segsize);
965 if (!dma_ok) {
966 dma_controller->channel_release(dma_channel);
967 hw_ep->rx_channel = dma_channel = NULL;
968 } else
969 csr |= MUSB_RXCSR_DMAENAB;
Felipe Balbi550a7372008-07-24 12:27:36 +0300970 }
971
972 csr |= MUSB_RXCSR_H_REQPKT;
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300973 dev_dbg(musb->controller, "RXCSR%d := %04x\n", epnum, csr);
Felipe Balbi550a7372008-07-24 12:27:36 +0300974 musb_writew(hw_ep->regs, MUSB_RXCSR, csr);
975 csr = musb_readw(hw_ep->regs, MUSB_RXCSR);
976 }
977}
978
Ajay Kumar Guptaf2838622012-07-19 13:41:59 +0530979/* Schedule next QH from musb->in_bulk/out_bulk and move the current qh to
980 * the end; avoids starvation for other endpoints.
981 */
982static void musb_bulk_nak_timeout(struct musb *musb, struct musb_hw_ep *ep,
983 int is_in)
984{
985 struct dma_channel *dma;
986 struct urb *urb;
987 void __iomem *mbase = musb->mregs;
988 void __iomem *epio = ep->regs;
989 struct musb_qh *cur_qh, *next_qh;
990 u16 rx_csr, tx_csr;
991
992 musb_ep_select(mbase, ep->epnum);
993 if (is_in) {
994 dma = is_dma_capable() ? ep->rx_channel : NULL;
995
996 /* clear nak timeout bit */
997 rx_csr = musb_readw(epio, MUSB_RXCSR);
998 rx_csr |= MUSB_RXCSR_H_WZC_BITS;
999 rx_csr &= ~MUSB_RXCSR_DATAERROR;
1000 musb_writew(epio, MUSB_RXCSR, rx_csr);
1001
1002 cur_qh = first_qh(&musb->in_bulk);
1003 } else {
1004 dma = is_dma_capable() ? ep->tx_channel : NULL;
1005
1006 /* clear nak timeout bit */
1007 tx_csr = musb_readw(epio, MUSB_TXCSR);
1008 tx_csr |= MUSB_TXCSR_H_WZC_BITS;
1009 tx_csr &= ~MUSB_TXCSR_H_NAKTIMEOUT;
1010 musb_writew(epio, MUSB_TXCSR, tx_csr);
1011
1012 cur_qh = first_qh(&musb->out_bulk);
1013 }
1014 if (cur_qh) {
1015 urb = next_urb(cur_qh);
1016 if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) {
1017 dma->status = MUSB_DMA_STATUS_CORE_ABORT;
1018 musb->dma_controller->channel_abort(dma);
1019 urb->actual_length += dma->actual_len;
1020 dma->actual_len = 0L;
1021 }
1022 musb_save_toggle(cur_qh, is_in, urb);
1023
1024 if (is_in) {
1025 /* move cur_qh to end of queue */
1026 list_move_tail(&cur_qh->ring, &musb->in_bulk);
1027
1028 /* get the next qh from musb->in_bulk */
1029 next_qh = first_qh(&musb->in_bulk);
1030
1031 /* set rx_reinit and schedule the next qh */
1032 ep->rx_reinit = 1;
1033 } else {
1034 /* move cur_qh to end of queue */
1035 list_move_tail(&cur_qh->ring, &musb->out_bulk);
1036
1037 /* get the next qh from musb->out_bulk */
1038 next_qh = first_qh(&musb->out_bulk);
1039
1040 /* set tx_reinit and schedule the next qh */
1041 ep->tx_reinit = 1;
1042 }
1043 musb_start_urb(musb, is_in, next_qh);
1044 }
1045}
Felipe Balbi550a7372008-07-24 12:27:36 +03001046
1047/*
1048 * Service the default endpoint (ep0) as host.
1049 * Return true until it's time to start the status stage.
1050 */
1051static bool musb_h_ep0_continue(struct musb *musb, u16 len, struct urb *urb)
1052{
1053 bool more = false;
1054 u8 *fifo_dest = NULL;
1055 u16 fifo_count = 0;
1056 struct musb_hw_ep *hw_ep = musb->control_ep;
1057 struct musb_qh *qh = hw_ep->in_qh;
1058 struct usb_ctrlrequest *request;
1059
1060 switch (musb->ep0_stage) {
1061 case MUSB_EP0_IN:
1062 fifo_dest = urb->transfer_buffer + urb->actual_length;
Sergei Shtylyov3ecdb9a2009-02-21 15:31:23 -08001063 fifo_count = min_t(size_t, len, urb->transfer_buffer_length -
1064 urb->actual_length);
Felipe Balbi550a7372008-07-24 12:27:36 +03001065 if (fifo_count < len)
1066 urb->status = -EOVERFLOW;
1067
1068 musb_read_fifo(hw_ep, fifo_count, fifo_dest);
1069
1070 urb->actual_length += fifo_count;
1071 if (len < qh->maxpacket) {
1072 /* always terminate on short read; it's
1073 * rarely reported as an error.
1074 */
1075 } else if (urb->actual_length <
1076 urb->transfer_buffer_length)
1077 more = true;
1078 break;
1079 case MUSB_EP0_START:
1080 request = (struct usb_ctrlrequest *) urb->setup_packet;
1081
1082 if (!request->wLength) {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001083 dev_dbg(musb->controller, "start no-DATA\n");
Felipe Balbi550a7372008-07-24 12:27:36 +03001084 break;
1085 } else if (request->bRequestType & USB_DIR_IN) {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001086 dev_dbg(musb->controller, "start IN-DATA\n");
Felipe Balbi550a7372008-07-24 12:27:36 +03001087 musb->ep0_stage = MUSB_EP0_IN;
1088 more = true;
1089 break;
1090 } else {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001091 dev_dbg(musb->controller, "start OUT-DATA\n");
Felipe Balbi550a7372008-07-24 12:27:36 +03001092 musb->ep0_stage = MUSB_EP0_OUT;
1093 more = true;
1094 }
1095 /* FALLTHROUGH */
1096 case MUSB_EP0_OUT:
Sergei Shtylyov3ecdb9a2009-02-21 15:31:23 -08001097 fifo_count = min_t(size_t, qh->maxpacket,
1098 urb->transfer_buffer_length -
1099 urb->actual_length);
Felipe Balbi550a7372008-07-24 12:27:36 +03001100 if (fifo_count) {
1101 fifo_dest = (u8 *) (urb->transfer_buffer
1102 + urb->actual_length);
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001103 dev_dbg(musb->controller, "Sending %d byte%s to ep0 fifo %p\n",
David Brownellbb1c9ef2008-11-24 13:06:50 +02001104 fifo_count,
1105 (fifo_count == 1) ? "" : "s",
1106 fifo_dest);
Felipe Balbi550a7372008-07-24 12:27:36 +03001107 musb_write_fifo(hw_ep, fifo_count, fifo_dest);
1108
1109 urb->actual_length += fifo_count;
1110 more = true;
1111 }
1112 break;
1113 default:
1114 ERR("bogus ep0 stage %d\n", musb->ep0_stage);
1115 break;
1116 }
1117
1118 return more;
1119}
1120
1121/*
1122 * Handle default endpoint interrupt as host. Only called in IRQ time
David Brownellc767c1c2008-09-11 11:53:23 +03001123 * from musb_interrupt().
Felipe Balbi550a7372008-07-24 12:27:36 +03001124 *
1125 * called with controller irqlocked
1126 */
1127irqreturn_t musb_h_ep0_irq(struct musb *musb)
1128{
1129 struct urb *urb;
1130 u16 csr, len;
1131 int status = 0;
1132 void __iomem *mbase = musb->mregs;
1133 struct musb_hw_ep *hw_ep = musb->control_ep;
1134 void __iomem *epio = hw_ep->regs;
1135 struct musb_qh *qh = hw_ep->in_qh;
1136 bool complete = false;
1137 irqreturn_t retval = IRQ_NONE;
1138
1139 /* ep0 only has one queue, "in" */
1140 urb = next_urb(qh);
1141
1142 musb_ep_select(mbase, 0);
1143 csr = musb_readw(epio, MUSB_CSR0);
1144 len = (csr & MUSB_CSR0_RXPKTRDY)
1145 ? musb_readb(epio, MUSB_COUNT0)
1146 : 0;
1147
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001148 dev_dbg(musb->controller, "<== csr0 %04x, qh %p, count %d, urb %p, stage %d\n",
Felipe Balbi550a7372008-07-24 12:27:36 +03001149 csr, qh, len, urb, musb->ep0_stage);
1150
1151 /* if we just did status stage, we are done */
1152 if (MUSB_EP0_STATUS == musb->ep0_stage) {
1153 retval = IRQ_HANDLED;
1154 complete = true;
1155 }
1156
1157 /* prepare status */
1158 if (csr & MUSB_CSR0_H_RXSTALL) {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001159 dev_dbg(musb->controller, "STALLING ENDPOINT\n");
Felipe Balbi550a7372008-07-24 12:27:36 +03001160 status = -EPIPE;
1161
1162 } else if (csr & MUSB_CSR0_H_ERROR) {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001163 dev_dbg(musb->controller, "no response, csr0 %04x\n", csr);
Felipe Balbi550a7372008-07-24 12:27:36 +03001164 status = -EPROTO;
1165
1166 } else if (csr & MUSB_CSR0_H_NAKTIMEOUT) {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001167 dev_dbg(musb->controller, "control NAK timeout\n");
Felipe Balbi550a7372008-07-24 12:27:36 +03001168
1169 /* NOTE: this code path would be a good place to PAUSE a
1170 * control transfer, if another one is queued, so that
Ajay Kumar Gupta1e0320f2009-02-24 15:26:13 -08001171 * ep0 is more likely to stay busy. That's already done
1172 * for bulk RX transfers.
Felipe Balbi550a7372008-07-24 12:27:36 +03001173 *
1174 * if (qh->ring.next != &musb->control), then
1175 * we have a candidate... NAKing is *NOT* an error
1176 */
1177 musb_writew(epio, MUSB_CSR0, 0);
1178 retval = IRQ_HANDLED;
1179 }
1180
1181 if (status) {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001182 dev_dbg(musb->controller, "aborting\n");
Felipe Balbi550a7372008-07-24 12:27:36 +03001183 retval = IRQ_HANDLED;
1184 if (urb)
1185 urb->status = status;
1186 complete = true;
1187
1188 /* use the proper sequence to abort the transfer */
1189 if (csr & MUSB_CSR0_H_REQPKT) {
1190 csr &= ~MUSB_CSR0_H_REQPKT;
1191 musb_writew(epio, MUSB_CSR0, csr);
1192 csr &= ~MUSB_CSR0_H_NAKTIMEOUT;
1193 musb_writew(epio, MUSB_CSR0, csr);
1194 } else {
David Brownell78322c12009-03-26 17:38:30 -07001195 musb_h_ep0_flush_fifo(hw_ep);
Felipe Balbi550a7372008-07-24 12:27:36 +03001196 }
1197
1198 musb_writeb(epio, MUSB_NAKLIMIT0, 0);
1199
1200 /* clear it */
1201 musb_writew(epio, MUSB_CSR0, 0);
1202 }
1203
1204 if (unlikely(!urb)) {
1205 /* stop endpoint since we have no place for its data, this
1206 * SHOULD NEVER HAPPEN! */
1207 ERR("no URB for end 0\n");
1208
David Brownell78322c12009-03-26 17:38:30 -07001209 musb_h_ep0_flush_fifo(hw_ep);
Felipe Balbi550a7372008-07-24 12:27:36 +03001210 goto done;
1211 }
1212
1213 if (!complete) {
1214 /* call common logic and prepare response */
1215 if (musb_h_ep0_continue(musb, len, urb)) {
1216 /* more packets required */
1217 csr = (MUSB_EP0_IN == musb->ep0_stage)
1218 ? MUSB_CSR0_H_REQPKT : MUSB_CSR0_TXPKTRDY;
1219 } else {
1220 /* data transfer complete; perform status phase */
1221 if (usb_pipeout(urb->pipe)
1222 || !urb->transfer_buffer_length)
1223 csr = MUSB_CSR0_H_STATUSPKT
1224 | MUSB_CSR0_H_REQPKT;
1225 else
1226 csr = MUSB_CSR0_H_STATUSPKT
1227 | MUSB_CSR0_TXPKTRDY;
1228
Ajay Kumar Gupta3c4653c2014-02-04 15:28:06 +02001229 /* disable ping token in status phase */
1230 csr |= MUSB_CSR0_H_DIS_PING;
1231
Felipe Balbi550a7372008-07-24 12:27:36 +03001232 /* flag status stage */
1233 musb->ep0_stage = MUSB_EP0_STATUS;
1234
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001235 dev_dbg(musb->controller, "ep0 STATUS, csr %04x\n", csr);
Felipe Balbi550a7372008-07-24 12:27:36 +03001236
1237 }
1238 musb_writew(epio, MUSB_CSR0, csr);
1239 retval = IRQ_HANDLED;
1240 } else
1241 musb->ep0_stage = MUSB_EP0_IDLE;
1242
1243 /* call completion handler if done */
1244 if (complete)
1245 musb_advance_schedule(musb, urb, hw_ep, 1);
1246done:
1247 return retval;
1248}
1249
1250
1251#ifdef CONFIG_USB_INVENTRA_DMA
1252
1253/* Host side TX (OUT) using Mentor DMA works as follows:
1254 submit_urb ->
1255 - if queue was empty, Program Endpoint
1256 - ... which starts DMA to fifo in mode 1 or 0
1257
1258 DMA Isr (transfer complete) -> TxAvail()
1259 - Stop DMA (~DmaEnab) (<--- Alert ... currently happens
1260 only in musb_cleanup_urb)
1261 - TxPktRdy has to be set in mode 0 or for
1262 short packets in mode 1.
1263*/
1264
1265#endif
1266
1267/* Service a Tx-Available or dma completion irq for the endpoint */
1268void musb_host_tx(struct musb *musb, u8 epnum)
1269{
1270 int pipe;
1271 bool done = false;
1272 u16 tx_csr;
Sergei Shtylyov6b6e9712009-03-26 18:29:19 -07001273 size_t length = 0;
1274 size_t offset = 0;
Felipe Balbi550a7372008-07-24 12:27:36 +03001275 struct musb_hw_ep *hw_ep = musb->endpoints + epnum;
1276 void __iomem *epio = hw_ep->regs;
Sergei Shtylyov3e5c6dc2009-03-27 12:55:16 -07001277 struct musb_qh *qh = hw_ep->out_qh;
1278 struct urb *urb = next_urb(qh);
Felipe Balbi550a7372008-07-24 12:27:36 +03001279 u32 status = 0;
1280 void __iomem *mbase = musb->mregs;
1281 struct dma_channel *dma;
T. S., Anil Kumarf8afbf7f2010-09-24 13:44:09 +03001282 bool transfer_pending = false;
Felipe Balbi550a7372008-07-24 12:27:36 +03001283
Felipe Balbi550a7372008-07-24 12:27:36 +03001284 musb_ep_select(mbase, epnum);
1285 tx_csr = musb_readw(epio, MUSB_TXCSR);
1286
1287 /* with CPPI, DMA sometimes triggers "extra" irqs */
1288 if (!urb) {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001289 dev_dbg(musb->controller, "extra TX%d ready, csr %04x\n", epnum, tx_csr);
Sergei Shtylyov6b6e9712009-03-26 18:29:19 -07001290 return;
Felipe Balbi550a7372008-07-24 12:27:36 +03001291 }
1292
1293 pipe = urb->pipe;
1294 dma = is_dma_capable() ? hw_ep->tx_channel : NULL;
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001295 dev_dbg(musb->controller, "OUT/TX%d end, csr %04x%s\n", epnum, tx_csr,
Felipe Balbi550a7372008-07-24 12:27:36 +03001296 dma ? ", dma" : "");
1297
1298 /* check for errors */
1299 if (tx_csr & MUSB_TXCSR_H_RXSTALL) {
1300 /* dma was disabled, fifo flushed */
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001301 dev_dbg(musb->controller, "TX end %d stall\n", epnum);
Felipe Balbi550a7372008-07-24 12:27:36 +03001302
1303 /* stall; record URB status */
1304 status = -EPIPE;
1305
1306 } else if (tx_csr & MUSB_TXCSR_H_ERROR) {
1307 /* (NON-ISO) dma was disabled, fifo flushed */
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001308 dev_dbg(musb->controller, "TX 3strikes on ep=%d\n", epnum);
Felipe Balbi550a7372008-07-24 12:27:36 +03001309
1310 status = -ETIMEDOUT;
1311
1312 } else if (tx_csr & MUSB_TXCSR_H_NAKTIMEOUT) {
Ajay Kumar Guptaf2838622012-07-19 13:41:59 +05301313 if (USB_ENDPOINT_XFER_BULK == qh->type && qh->mux == 1
1314 && !list_is_singular(&musb->out_bulk)) {
1315 dev_dbg(musb->controller,
1316 "NAK timeout on TX%d ep\n", epnum);
1317 musb_bulk_nak_timeout(musb, hw_ep, 0);
1318 } else {
1319 dev_dbg(musb->controller,
1320 "TX end=%d device not responding\n", epnum);
1321 /* NOTE: this code path would be a good place to PAUSE a
1322 * transfer, if there's some other (nonperiodic) tx urb
1323 * that could use this fifo. (dma complicates it...)
1324 * That's already done for bulk RX transfers.
1325 *
1326 * if (bulk && qh->ring.next != &musb->out_bulk), then
1327 * we have a candidate... NAKing is *NOT* an error
1328 */
1329 musb_ep_select(mbase, epnum);
1330 musb_writew(epio, MUSB_TXCSR,
1331 MUSB_TXCSR_H_WZC_BITS
1332 | MUSB_TXCSR_TXPKTRDY);
1333 }
1334 return;
Felipe Balbi550a7372008-07-24 12:27:36 +03001335 }
1336
Virupax Sadashivpetimath8e8a5512012-08-07 14:46:20 +05301337done:
Felipe Balbi550a7372008-07-24 12:27:36 +03001338 if (status) {
1339 if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) {
1340 dma->status = MUSB_DMA_STATUS_CORE_ABORT;
Daniel Mack9c547692014-05-26 14:52:35 +02001341 musb->dma_controller->channel_abort(dma);
Felipe Balbi550a7372008-07-24 12:27:36 +03001342 }
1343
1344 /* do the proper sequence to abort the transfer in the
1345 * usb core; the dma engine should already be stopped.
1346 */
1347 musb_h_tx_flush_fifo(hw_ep);
1348 tx_csr &= ~(MUSB_TXCSR_AUTOSET
1349 | MUSB_TXCSR_DMAENAB
1350 | MUSB_TXCSR_H_ERROR
1351 | MUSB_TXCSR_H_RXSTALL
1352 | MUSB_TXCSR_H_NAKTIMEOUT
1353 );
1354
1355 musb_ep_select(mbase, epnum);
1356 musb_writew(epio, MUSB_TXCSR, tx_csr);
1357 /* REVISIT may need to clear FLUSHFIFO ... */
1358 musb_writew(epio, MUSB_TXCSR, tx_csr);
1359 musb_writeb(epio, MUSB_TXINTERVAL, 0);
1360
1361 done = true;
1362 }
1363
1364 /* second cppi case */
1365 if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001366 dev_dbg(musb->controller, "extra TX%d ready, csr %04x\n", epnum, tx_csr);
Sergei Shtylyov6b6e9712009-03-26 18:29:19 -07001367 return;
Felipe Balbi550a7372008-07-24 12:27:36 +03001368 }
1369
Sergei Shtylyovc7bbc052009-03-26 18:26:40 -07001370 if (is_dma_capable() && dma && !status) {
1371 /*
1372 * DMA has completed. But if we're using DMA mode 1 (multi
1373 * packet DMA), we need a terminal TXPKTRDY interrupt before
1374 * we can consider this transfer completed, lest we trash
1375 * its last packet when writing the next URB's data. So we
1376 * switch back to mode 0 to get that interrupt; we'll come
1377 * back here once it happens.
1378 */
1379 if (tx_csr & MUSB_TXCSR_DMAMODE) {
1380 /*
1381 * We shouldn't clear DMAMODE with DMAENAB set; so
1382 * clear them in a safe order. That should be OK
1383 * once TXPKTRDY has been set (and I've never seen
1384 * it being 0 at this moment -- DMA interrupt latency
1385 * is significant) but if it hasn't been then we have
1386 * no choice but to stop being polite and ignore the
1387 * programmer's guide... :-)
1388 *
1389 * Note that we must write TXCSR with TXPKTRDY cleared
1390 * in order not to re-trigger the packet send (this bit
1391 * can't be cleared by CPU), and there's another caveat:
1392 * TXPKTRDY may be set shortly and then cleared in the
1393 * double-buffered FIFO mode, so we do an extra TXCSR
1394 * read for debouncing...
1395 */
1396 tx_csr &= musb_readw(epio, MUSB_TXCSR);
1397 if (tx_csr & MUSB_TXCSR_TXPKTRDY) {
1398 tx_csr &= ~(MUSB_TXCSR_DMAENAB |
1399 MUSB_TXCSR_TXPKTRDY);
1400 musb_writew(epio, MUSB_TXCSR,
1401 tx_csr | MUSB_TXCSR_H_WZC_BITS);
1402 }
1403 tx_csr &= ~(MUSB_TXCSR_DMAMODE |
1404 MUSB_TXCSR_TXPKTRDY);
1405 musb_writew(epio, MUSB_TXCSR,
1406 tx_csr | MUSB_TXCSR_H_WZC_BITS);
1407
1408 /*
1409 * There is no guarantee that we'll get an interrupt
1410 * after clearing DMAMODE as we might have done this
1411 * too late (after TXPKTRDY was cleared by controller).
1412 * Re-read TXCSR as we have spoiled its previous value.
1413 */
1414 tx_csr = musb_readw(epio, MUSB_TXCSR);
1415 }
1416
1417 /*
1418 * We may get here from a DMA completion or TXPKTRDY interrupt.
1419 * In any case, we must check the FIFO status here and bail out
1420 * only if the FIFO still has data -- that should prevent the
1421 * "missed" TXPKTRDY interrupts and deal with double-buffered
1422 * FIFO mode too...
1423 */
1424 if (tx_csr & (MUSB_TXCSR_FIFONOTEMPTY | MUSB_TXCSR_TXPKTRDY)) {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001425 dev_dbg(musb->controller, "DMA complete but packet still in FIFO, "
Sergei Shtylyovc7bbc052009-03-26 18:26:40 -07001426 "CSR %04x\n", tx_csr);
1427 return;
1428 }
1429 }
1430
Felipe Balbi550a7372008-07-24 12:27:36 +03001431 if (!status || dma || usb_pipeisoc(pipe)) {
1432 if (dma)
Sergei Shtylyov6b6e9712009-03-26 18:29:19 -07001433 length = dma->actual_len;
Felipe Balbi550a7372008-07-24 12:27:36 +03001434 else
Sergei Shtylyov6b6e9712009-03-26 18:29:19 -07001435 length = qh->segsize;
1436 qh->offset += length;
Felipe Balbi550a7372008-07-24 12:27:36 +03001437
1438 if (usb_pipeisoc(pipe)) {
1439 struct usb_iso_packet_descriptor *d;
1440
1441 d = urb->iso_frame_desc + qh->iso_idx;
Sergei Shtylyov6b6e9712009-03-26 18:29:19 -07001442 d->actual_length = length;
1443 d->status = status;
Felipe Balbi550a7372008-07-24 12:27:36 +03001444 if (++qh->iso_idx >= urb->number_of_packets) {
1445 done = true;
1446 } else {
1447 d++;
Sergei Shtylyov6b6e9712009-03-26 18:29:19 -07001448 offset = d->offset;
1449 length = d->length;
Felipe Balbi550a7372008-07-24 12:27:36 +03001450 }
T. S., Anil Kumarf8afbf7f2010-09-24 13:44:09 +03001451 } else if (dma && urb->transfer_buffer_length == qh->offset) {
Felipe Balbi550a7372008-07-24 12:27:36 +03001452 done = true;
1453 } else {
1454 /* see if we need to send more data, or ZLP */
1455 if (qh->segsize < qh->maxpacket)
1456 done = true;
1457 else if (qh->offset == urb->transfer_buffer_length
1458 && !(urb->transfer_flags
1459 & URB_ZERO_PACKET))
1460 done = true;
1461 if (!done) {
Sergei Shtylyov6b6e9712009-03-26 18:29:19 -07001462 offset = qh->offset;
1463 length = urb->transfer_buffer_length - offset;
T. S., Anil Kumarf8afbf7f2010-09-24 13:44:09 +03001464 transfer_pending = true;
Felipe Balbi550a7372008-07-24 12:27:36 +03001465 }
1466 }
1467 }
1468
1469 /* urb->status != -EINPROGRESS means request has been faulted,
1470 * so we must abort this transfer after cleanup
1471 */
1472 if (urb->status != -EINPROGRESS) {
1473 done = true;
1474 if (status == 0)
1475 status = urb->status;
1476 }
1477
1478 if (done) {
1479 /* set status */
1480 urb->status = status;
1481 urb->actual_length = qh->offset;
1482 musb_advance_schedule(musb, urb, hw_ep, USB_DIR_OUT);
Sergei Shtylyov6b6e9712009-03-26 18:29:19 -07001483 return;
T. S., Anil Kumarf8afbf7f2010-09-24 13:44:09 +03001484 } else if ((usb_pipeisoc(pipe) || transfer_pending) && dma) {
Sergei Shtylyov6b6e9712009-03-26 18:29:19 -07001485 if (musb_tx_dma_program(musb->dma_controller, hw_ep, qh, urb,
Ajay Kumar Guptadfeffa52009-11-17 15:22:55 +05301486 offset, length)) {
Tony Lindgrenf8e9f34f2015-05-01 12:29:27 -07001487 if (is_cppi_enabled(musb) || tusb_dma_omap(musb))
Ajay Kumar Guptadfeffa52009-11-17 15:22:55 +05301488 musb_h_tx_dma_start(hw_ep);
Sergei Shtylyov6b6e9712009-03-26 18:29:19 -07001489 return;
Ajay Kumar Guptadfeffa52009-11-17 15:22:55 +05301490 }
Sergei Shtylyov6b6e9712009-03-26 18:29:19 -07001491 } else if (tx_csr & MUSB_TXCSR_DMAENAB) {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001492 dev_dbg(musb->controller, "not complete, but DMA enabled?\n");
Sergei Shtylyov6b6e9712009-03-26 18:29:19 -07001493 return;
1494 }
Felipe Balbi550a7372008-07-24 12:27:36 +03001495
Sergei Shtylyov6b6e9712009-03-26 18:29:19 -07001496 /*
1497 * PIO: start next packet in this URB.
1498 *
1499 * REVISIT: some docs say that when hw_ep->tx_double_buffered,
1500 * (and presumably, FIFO is not half-full) we should write *two*
1501 * packets before updating TXCSR; other docs disagree...
1502 */
1503 if (length > qh->maxpacket)
1504 length = qh->maxpacket;
Maulik Mankad496dda72010-09-24 13:44:06 +03001505 /* Unmap the buffer so that CPU can use it */
Daniel Mack8b125df2013-04-10 21:55:50 +02001506 usb_hcd_unmap_urb_for_dma(musb->hcd, urb);
Virupax Sadashivpetimath8e8a5512012-08-07 14:46:20 +05301507
1508 /*
1509 * We need to map sg if the transfer_buffer is
1510 * NULL.
1511 */
1512 if (!urb->transfer_buffer)
Virupax Sadashivpetimathed74df12013-04-24 08:38:48 +02001513 qh->use_sg = true;
Virupax Sadashivpetimath8e8a5512012-08-07 14:46:20 +05301514
Virupax Sadashivpetimathed74df12013-04-24 08:38:48 +02001515 if (qh->use_sg) {
Virupax Sadashivpetimath8e8a5512012-08-07 14:46:20 +05301516 /* sg_miter_start is already done in musb_ep_program */
1517 if (!sg_miter_next(&qh->sg_miter)) {
1518 dev_err(musb->controller, "error: sg list empty\n");
1519 sg_miter_stop(&qh->sg_miter);
1520 status = -EINVAL;
1521 goto done;
1522 }
1523 urb->transfer_buffer = qh->sg_miter.addr;
1524 length = min_t(u32, length, qh->sg_miter.length);
1525 musb_write_fifo(hw_ep, length, urb->transfer_buffer);
1526 qh->sg_miter.consumed = length;
1527 sg_miter_stop(&qh->sg_miter);
1528 } else {
1529 musb_write_fifo(hw_ep, length, urb->transfer_buffer + offset);
1530 }
1531
Sergei Shtylyov6b6e9712009-03-26 18:29:19 -07001532 qh->segsize = length;
Felipe Balbi550a7372008-07-24 12:27:36 +03001533
Virupax Sadashivpetimathed74df12013-04-24 08:38:48 +02001534 if (qh->use_sg) {
Virupax Sadashivpetimath8e8a5512012-08-07 14:46:20 +05301535 if (offset + length >= urb->transfer_buffer_length)
Virupax Sadashivpetimathed74df12013-04-24 08:38:48 +02001536 qh->use_sg = false;
Virupax Sadashivpetimath8e8a5512012-08-07 14:46:20 +05301537 }
1538
Sergei Shtylyov6b6e9712009-03-26 18:29:19 -07001539 musb_ep_select(mbase, epnum);
1540 musb_writew(epio, MUSB_TXCSR,
1541 MUSB_TXCSR_H_WZC_BITS | MUSB_TXCSR_TXPKTRDY);
Felipe Balbi550a7372008-07-24 12:27:36 +03001542}
1543
Tony Lindgren069a3fd2015-05-01 12:29:33 -07001544#ifdef CONFIG_USB_TI_CPPI41_DMA
1545/* Seems to set up ISO for cppi41 and not advance len. See commit c57c41d */
1546static int musb_rx_dma_iso_cppi41(struct dma_controller *dma,
1547 struct musb_hw_ep *hw_ep,
1548 struct musb_qh *qh,
1549 struct urb *urb,
1550 size_t len)
1551{
1552 struct dma_channel *channel = hw_ep->tx_channel;
1553 void __iomem *epio = hw_ep->regs;
1554 dma_addr_t *buf;
1555 u32 length, res;
1556 u16 val;
1557
1558 buf = (void *)urb->iso_frame_desc[qh->iso_idx].offset +
1559 (u32)urb->transfer_dma;
1560
1561 length = urb->iso_frame_desc[qh->iso_idx].length;
1562
1563 val = musb_readw(epio, MUSB_RXCSR);
1564 val |= MUSB_RXCSR_DMAENAB;
1565 musb_writew(hw_ep->regs, MUSB_RXCSR, val);
1566
1567 res = dma->channel_program(channel, qh->maxpacket, 0,
1568 (u32)buf, length);
1569
1570 return res;
1571}
1572#else
1573static inline int musb_rx_dma_iso_cppi41(struct dma_controller *dma,
1574 struct musb_hw_ep *hw_ep,
1575 struct musb_qh *qh,
1576 struct urb *urb,
1577 size_t len)
1578{
1579 return false;
1580}
1581#endif
Felipe Balbi550a7372008-07-24 12:27:36 +03001582
Tony Lindgrencff84bd2015-05-01 12:29:35 -07001583#if defined(CONFIG_USB_INVENTRA_DMA) || defined(CONFIG_USB_UX500_DMA) || \
1584 defined(CONFIG_USB_TI_CPPI41_DMA)
Felipe Balbi550a7372008-07-24 12:27:36 +03001585/* Host side RX (IN) using Mentor DMA works as follows:
1586 submit_urb ->
1587 - if queue was empty, ProgramEndpoint
1588 - first IN token is sent out (by setting ReqPkt)
1589 LinuxIsr -> RxReady()
1590 /\ => first packet is received
1591 | - Set in mode 0 (DmaEnab, ~ReqPkt)
1592 | -> DMA Isr (transfer complete) -> RxReady()
1593 | - Ack receive (~RxPktRdy), turn off DMA (~DmaEnab)
1594 | - if urb not complete, send next IN token (ReqPkt)
1595 | | else complete urb.
1596 | |
1597 ---------------------------
1598 *
1599 * Nuances of mode 1:
1600 * For short packets, no ack (+RxPktRdy) is sent automatically
1601 * (even if AutoClear is ON)
1602 * For full packets, ack (~RxPktRdy) and next IN token (+ReqPkt) is sent
1603 * automatically => major problem, as collecting the next packet becomes
1604 * difficult. Hence mode 1 is not used.
1605 *
1606 * REVISIT
1607 * All we care about at this driver level is that
1608 * (a) all URBs terminate with REQPKT cleared and fifo(s) empty;
1609 * (b) termination conditions are: short RX, or buffer full;
1610 * (c) fault modes include
1611 * - iff URB_SHORT_NOT_OK, short RX status is -EREMOTEIO.
1612 * (and that endpoint's dma queue stops immediately)
1613 * - overflow (full, PLUS more bytes in the terminal packet)
1614 *
1615 * So for example, usb-storage sets URB_SHORT_NOT_OK, and would
1616 * thus be a great candidate for using mode 1 ... for all but the
1617 * last packet of one URB's transfer.
1618 */
Tony Lindgrencff84bd2015-05-01 12:29:35 -07001619static int musb_rx_dma_inventra_cppi41(struct dma_controller *dma,
1620 struct musb_hw_ep *hw_ep,
1621 struct musb_qh *qh,
1622 struct urb *urb,
1623 size_t len)
1624{
1625 struct dma_channel *channel = hw_ep->rx_channel;
1626 void __iomem *epio = hw_ep->regs;
1627 u16 val;
1628 int pipe;
1629 bool done;
Felipe Balbi550a7372008-07-24 12:27:36 +03001630
Tony Lindgrencff84bd2015-05-01 12:29:35 -07001631 pipe = urb->pipe;
1632
1633 if (usb_pipeisoc(pipe)) {
1634 struct usb_iso_packet_descriptor *d;
1635
1636 d = urb->iso_frame_desc + qh->iso_idx;
1637 d->actual_length = len;
1638
1639 /* even if there was an error, we did the dma
1640 * for iso_frame_desc->length
1641 */
1642 if (d->status != -EILSEQ && d->status != -EOVERFLOW)
1643 d->status = 0;
1644
1645 if (++qh->iso_idx >= urb->number_of_packets) {
1646 done = true;
1647 } else {
1648 /* REVISIT: Why ignore return value here? */
1649 if (musb_dma_cppi41(hw_ep->musb))
1650 done = musb_rx_dma_iso_cppi41(dma, hw_ep, qh,
1651 urb, len);
1652 done = false;
1653 }
1654
1655 } else {
1656 /* done if urb buffer is full or short packet is recd */
1657 done = (urb->actual_length + len >=
1658 urb->transfer_buffer_length
1659 || channel->actual_len < qh->maxpacket
1660 || channel->rx_packet_done);
1661 }
1662
1663 /* send IN token for next packet, without AUTOREQ */
1664 if (!done) {
1665 val = musb_readw(epio, MUSB_RXCSR);
1666 val |= MUSB_RXCSR_H_REQPKT;
1667 musb_writew(epio, MUSB_RXCSR, MUSB_RXCSR_H_WZC_BITS | val);
1668 }
1669
1670 return done;
1671}
Tony Lindgrenac33cdb2015-05-01 12:29:37 -07001672
1673/* Disadvantage of using mode 1:
1674 * It's basically usable only for mass storage class; essentially all
1675 * other protocols also terminate transfers on short packets.
1676 *
1677 * Details:
1678 * An extra IN token is sent at the end of the transfer (due to AUTOREQ)
1679 * If you try to use mode 1 for (transfer_buffer_length - 512), and try
1680 * to use the extra IN token to grab the last packet using mode 0, then
1681 * the problem is that you cannot be sure when the device will send the
1682 * last packet and RxPktRdy set. Sometimes the packet is recd too soon
1683 * such that it gets lost when RxCSR is re-set at the end of the mode 1
1684 * transfer, while sometimes it is recd just a little late so that if you
1685 * try to configure for mode 0 soon after the mode 1 transfer is
1686 * completed, you will find rxcount 0. Okay, so you might think why not
1687 * wait for an interrupt when the pkt is recd. Well, you won't get any!
1688 */
1689static int musb_rx_dma_in_inventra_cppi41(struct dma_controller *dma,
1690 struct musb_hw_ep *hw_ep,
1691 struct musb_qh *qh,
1692 struct urb *urb,
1693 size_t len,
1694 u8 iso_err)
1695{
1696 struct musb *musb = hw_ep->musb;
1697 void __iomem *epio = hw_ep->regs;
1698 struct dma_channel *channel = hw_ep->rx_channel;
1699 u16 rx_count, val;
1700 int length, pipe, done;
1701 dma_addr_t buf;
1702
1703 rx_count = musb_readw(epio, MUSB_RXCOUNT);
1704 pipe = urb->pipe;
1705
1706 if (usb_pipeisoc(pipe)) {
1707 int d_status = 0;
1708 struct usb_iso_packet_descriptor *d;
1709
1710 d = urb->iso_frame_desc + qh->iso_idx;
1711
1712 if (iso_err) {
1713 d_status = -EILSEQ;
1714 urb->error_count++;
1715 }
1716 if (rx_count > d->length) {
1717 if (d_status == 0) {
1718 d_status = -EOVERFLOW;
1719 urb->error_count++;
1720 }
1721 dev_dbg(musb->controller, "** OVERFLOW %d into %d\n",
1722 rx_count, d->length);
1723
1724 length = d->length;
1725 } else
1726 length = rx_count;
1727 d->status = d_status;
1728 buf = urb->transfer_dma + d->offset;
1729 } else {
1730 length = rx_count;
1731 buf = urb->transfer_dma + urb->actual_length;
1732 }
1733
1734 channel->desired_mode = 0;
1735#ifdef USE_MODE1
1736 /* because of the issue below, mode 1 will
1737 * only rarely behave with correct semantics.
1738 */
1739 if ((urb->transfer_flags & URB_SHORT_NOT_OK)
1740 && (urb->transfer_buffer_length - urb->actual_length)
1741 > qh->maxpacket)
1742 channel->desired_mode = 1;
1743 if (rx_count < hw_ep->max_packet_sz_rx) {
1744 length = rx_count;
1745 channel->desired_mode = 0;
1746 } else {
1747 length = urb->transfer_buffer_length;
1748 }
1749#endif
1750
1751 /* See comments above on disadvantages of using mode 1 */
1752 val = musb_readw(epio, MUSB_RXCSR);
1753 val &= ~MUSB_RXCSR_H_REQPKT;
1754
1755 if (channel->desired_mode == 0)
1756 val &= ~MUSB_RXCSR_H_AUTOREQ;
1757 else
1758 val |= MUSB_RXCSR_H_AUTOREQ;
1759 val |= MUSB_RXCSR_DMAENAB;
1760
1761 /* autoclear shouldn't be set in high bandwidth */
1762 if (qh->hb_mult == 1)
1763 val |= MUSB_RXCSR_AUTOCLEAR;
1764
1765 musb_writew(epio, MUSB_RXCSR, MUSB_RXCSR_H_WZC_BITS | val);
1766
1767 /* REVISIT if when actual_length != 0,
1768 * transfer_buffer_length needs to be
1769 * adjusted first...
1770 */
1771 done = dma->channel_program(channel, qh->maxpacket,
1772 channel->desired_mode,
1773 buf, length);
1774
1775 if (!done) {
1776 dma->channel_release(channel);
1777 hw_ep->rx_channel = NULL;
1778 channel = NULL;
1779 val = musb_readw(epio, MUSB_RXCSR);
1780 val &= ~(MUSB_RXCSR_DMAENAB
1781 | MUSB_RXCSR_H_AUTOREQ
1782 | MUSB_RXCSR_AUTOCLEAR);
1783 musb_writew(epio, MUSB_RXCSR, val);
1784 }
1785
1786 return done;
1787}
Tony Lindgrencff84bd2015-05-01 12:29:35 -07001788#else
1789static inline int musb_rx_dma_inventra_cppi41(struct dma_controller *dma,
1790 struct musb_hw_ep *hw_ep,
1791 struct musb_qh *qh,
1792 struct urb *urb,
1793 size_t len)
1794{
1795 return false;
1796}
Tony Lindgrenac33cdb2015-05-01 12:29:37 -07001797
1798static inline int musb_rx_dma_in_inventra_cppi41(struct dma_controller *dma,
1799 struct musb_hw_ep *hw_ep,
1800 struct musb_qh *qh,
1801 struct urb *urb,
1802 size_t len,
1803 u8 iso_err)
1804{
1805 return false;
1806}
Felipe Balbi550a7372008-07-24 12:27:36 +03001807#endif
1808
1809/*
1810 * Service an RX interrupt for the given IN endpoint; docs cover bulk, iso,
1811 * and high-bandwidth IN transfer cases.
1812 */
1813void musb_host_rx(struct musb *musb, u8 epnum)
1814{
1815 struct urb *urb;
1816 struct musb_hw_ep *hw_ep = musb->endpoints + epnum;
Tony Lindgrencff84bd2015-05-01 12:29:35 -07001817 struct dma_controller *c = musb->dma_controller;
Felipe Balbi550a7372008-07-24 12:27:36 +03001818 void __iomem *epio = hw_ep->regs;
1819 struct musb_qh *qh = hw_ep->in_qh;
1820 size_t xfer_len;
1821 void __iomem *mbase = musb->mregs;
1822 int pipe;
1823 u16 rx_csr, val;
1824 bool iso_err = false;
1825 bool done = false;
1826 u32 status;
1827 struct dma_channel *dma;
Virupax Sadashivpetimath8e8a5512012-08-07 14:46:20 +05301828 unsigned int sg_flags = SG_MITER_ATOMIC | SG_MITER_TO_SG;
Felipe Balbi550a7372008-07-24 12:27:36 +03001829
1830 musb_ep_select(mbase, epnum);
1831
1832 urb = next_urb(qh);
1833 dma = is_dma_capable() ? hw_ep->rx_channel : NULL;
1834 status = 0;
1835 xfer_len = 0;
1836
1837 rx_csr = musb_readw(epio, MUSB_RXCSR);
1838 val = rx_csr;
1839
1840 if (unlikely(!urb)) {
1841 /* REVISIT -- THIS SHOULD NEVER HAPPEN ... but, at least
1842 * usbtest #11 (unlinks) triggers it regularly, sometimes
1843 * with fifo full. (Only with DMA??)
1844 */
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001845 dev_dbg(musb->controller, "BOGUS RX%d ready, csr %04x, count %d\n", epnum, val,
Felipe Balbi550a7372008-07-24 12:27:36 +03001846 musb_readw(epio, MUSB_RXCOUNT));
1847 musb_h_flush_rxfifo(hw_ep, MUSB_RXCSR_CLRDATATOG);
1848 return;
1849 }
1850
1851 pipe = urb->pipe;
1852
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001853 dev_dbg(musb->controller, "<== hw %d rxcsr %04x, urb actual %d (+dma %zu)\n",
Felipe Balbi550a7372008-07-24 12:27:36 +03001854 epnum, rx_csr, urb->actual_length,
1855 dma ? dma->actual_len : 0);
1856
1857 /* check for errors, concurrent stall & unlink is not really
1858 * handled yet! */
1859 if (rx_csr & MUSB_RXCSR_H_RXSTALL) {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001860 dev_dbg(musb->controller, "RX end %d STALL\n", epnum);
Felipe Balbi550a7372008-07-24 12:27:36 +03001861
1862 /* stall; record URB status */
1863 status = -EPIPE;
1864
1865 } else if (rx_csr & MUSB_RXCSR_H_ERROR) {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001866 dev_dbg(musb->controller, "end %d RX proto error\n", epnum);
Felipe Balbi550a7372008-07-24 12:27:36 +03001867
1868 status = -EPROTO;
1869 musb_writeb(epio, MUSB_RXINTERVAL, 0);
1870
Bin Liub5801212016-05-31 10:05:03 -05001871 rx_csr &= ~MUSB_RXCSR_H_ERROR;
1872 musb_writew(epio, MUSB_RXCSR, rx_csr);
1873
Felipe Balbi550a7372008-07-24 12:27:36 +03001874 } else if (rx_csr & MUSB_RXCSR_DATAERROR) {
1875
1876 if (USB_ENDPOINT_XFER_ISOC != qh->type) {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001877 dev_dbg(musb->controller, "RX end %d NAK timeout\n", epnum);
Ajay Kumar Gupta1e0320f2009-02-24 15:26:13 -08001878
1879 /* NOTE: NAKing is *NOT* an error, so we want to
1880 * continue. Except ... if there's a request for
1881 * another QH, use that instead of starving it.
1882 *
1883 * Devices like Ethernet and serial adapters keep
1884 * reads posted at all times, which will starve
1885 * other devices without this logic.
1886 */
1887 if (usb_pipebulk(urb->pipe)
1888 && qh->mux == 1
1889 && !list_is_singular(&musb->in_bulk)) {
Ajay Kumar Guptaf2838622012-07-19 13:41:59 +05301890 musb_bulk_nak_timeout(musb, hw_ep, 1);
Ajay Kumar Gupta1e0320f2009-02-24 15:26:13 -08001891 return;
1892 }
Felipe Balbi550a7372008-07-24 12:27:36 +03001893 musb_ep_select(mbase, epnum);
Ajay Kumar Gupta1e0320f2009-02-24 15:26:13 -08001894 rx_csr |= MUSB_RXCSR_H_WZC_BITS;
1895 rx_csr &= ~MUSB_RXCSR_DATAERROR;
1896 musb_writew(epio, MUSB_RXCSR, rx_csr);
Felipe Balbi550a7372008-07-24 12:27:36 +03001897
1898 goto finish;
1899 } else {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001900 dev_dbg(musb->controller, "RX end %d ISO data error\n", epnum);
Felipe Balbi550a7372008-07-24 12:27:36 +03001901 /* packet error reported later */
1902 iso_err = true;
1903 }
Ajay Kumar Guptaa483d702009-04-03 16:16:17 -07001904 } else if (rx_csr & MUSB_RXCSR_INCOMPRX) {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001905 dev_dbg(musb->controller, "end %d high bandwidth incomplete ISO packet RX\n",
Ajay Kumar Guptaa483d702009-04-03 16:16:17 -07001906 epnum);
1907 status = -EPROTO;
Felipe Balbi550a7372008-07-24 12:27:36 +03001908 }
1909
1910 /* faults abort the transfer */
1911 if (status) {
1912 /* clean up dma and collect transfer count */
1913 if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) {
1914 dma->status = MUSB_DMA_STATUS_CORE_ABORT;
Daniel Mack9c547692014-05-26 14:52:35 +02001915 musb->dma_controller->channel_abort(dma);
Felipe Balbi550a7372008-07-24 12:27:36 +03001916 xfer_len = dma->actual_len;
1917 }
1918 musb_h_flush_rxfifo(hw_ep, MUSB_RXCSR_CLRDATATOG);
1919 musb_writeb(epio, MUSB_RXINTERVAL, 0);
1920 done = true;
1921 goto finish;
1922 }
1923
1924 if (unlikely(dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY)) {
1925 /* SHOULD NEVER HAPPEN ... but at least DaVinci has done it */
1926 ERR("RX%d dma busy, csr %04x\n", epnum, rx_csr);
1927 goto finish;
1928 }
1929
1930 /* thorough shutdown for now ... given more precise fault handling
1931 * and better queueing support, we might keep a DMA pipeline going
1932 * while processing this irq for earlier completions.
1933 */
1934
1935 /* FIXME this is _way_ too much in-line logic for Mentor DMA */
Tony Lindgren557d5432015-05-01 12:29:34 -07001936 if (!musb_dma_inventra(musb) && !musb_dma_ux500(musb) &&
1937 (rx_csr & MUSB_RXCSR_H_REQPKT)) {
Felipe Balbi550a7372008-07-24 12:27:36 +03001938 /* REVISIT this happened for a while on some short reads...
1939 * the cleanup still needs investigation... looks bad...
1940 * and also duplicates dma cleanup code above ... plus,
1941 * shouldn't this be the "half full" double buffer case?
1942 */
1943 if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) {
1944 dma->status = MUSB_DMA_STATUS_CORE_ABORT;
Daniel Mack9c547692014-05-26 14:52:35 +02001945 musb->dma_controller->channel_abort(dma);
Felipe Balbi550a7372008-07-24 12:27:36 +03001946 xfer_len = dma->actual_len;
1947 done = true;
1948 }
1949
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001950 dev_dbg(musb->controller, "RXCSR%d %04x, reqpkt, len %zu%s\n", epnum, rx_csr,
Felipe Balbi550a7372008-07-24 12:27:36 +03001951 xfer_len, dma ? ", dma" : "");
1952 rx_csr &= ~MUSB_RXCSR_H_REQPKT;
1953
1954 musb_ep_select(mbase, epnum);
1955 musb_writew(epio, MUSB_RXCSR,
1956 MUSB_RXCSR_H_WZC_BITS | rx_csr);
1957 }
Tony Lindgren557d5432015-05-01 12:29:34 -07001958
Felipe Balbi550a7372008-07-24 12:27:36 +03001959 if (dma && (rx_csr & MUSB_RXCSR_DMAENAB)) {
1960 xfer_len = dma->actual_len;
1961
1962 val &= ~(MUSB_RXCSR_DMAENAB
1963 | MUSB_RXCSR_H_AUTOREQ
1964 | MUSB_RXCSR_AUTOCLEAR
1965 | MUSB_RXCSR_RXPKTRDY);
1966 musb_writew(hw_ep->regs, MUSB_RXCSR, val);
1967
Tony Lindgrencff84bd2015-05-01 12:29:35 -07001968 if (musb_dma_inventra(musb) || musb_dma_ux500(musb) ||
1969 musb_dma_cppi41(musb)) {
1970 done = musb_rx_dma_inventra_cppi41(c, hw_ep, qh, urb, xfer_len);
1971 dev_dbg(hw_ep->musb->controller,
1972 "ep %d dma %s, rxcsr %04x, rxcount %d\n",
1973 epnum, done ? "off" : "reset",
1974 musb_readw(epio, MUSB_RXCSR),
1975 musb_readw(epio, MUSB_RXCOUNT));
1976 } else {
1977 done = true;
Ajay Kumar Guptaf82a6892008-10-29 15:10:31 +02001978 }
Felipe Balbi550a7372008-07-24 12:27:36 +03001979
Felipe Balbi550a7372008-07-24 12:27:36 +03001980 } else if (urb->status == -EINPROGRESS) {
1981 /* if no errors, be sure a packet is ready for unloading */
1982 if (unlikely(!(rx_csr & MUSB_RXCSR_RXPKTRDY))) {
1983 status = -EPROTO;
1984 ERR("Rx interrupt with no errors or packet!\n");
1985
1986 /* FIXME this is another "SHOULD NEVER HAPPEN" */
1987
1988/* SCRUB (RX) */
1989 /* do the proper sequence to abort the transfer */
1990 musb_ep_select(mbase, epnum);
1991 val &= ~MUSB_RXCSR_H_REQPKT;
1992 musb_writew(epio, MUSB_RXCSR, val);
1993 goto finish;
1994 }
1995
1996 /* we are expecting IN packets */
Tony Lindgrene530bb82015-05-01 12:29:36 -07001997 if ((musb_dma_inventra(musb) || musb_dma_ux500(musb) ||
1998 musb_dma_cppi41(musb)) && dma) {
Tony Lindgrenac33cdb2015-05-01 12:29:37 -07001999 dev_dbg(hw_ep->musb->controller,
2000 "RX%d count %d, buffer 0x%llx len %d/%d\n",
2001 epnum, musb_readw(epio, MUSB_RXCOUNT),
2002 (unsigned long long) urb->transfer_dma
2003 + urb->actual_length,
2004 qh->offset,
2005 urb->transfer_buffer_length);
Felipe Balbi550a7372008-07-24 12:27:36 +03002006
Cristian Birsan4c2ba0c2016-02-19 10:11:56 +02002007 if (musb_rx_dma_in_inventra_cppi41(c, hw_ep, qh, urb,
2008 xfer_len, iso_err))
Tony Lindgrenac33cdb2015-05-01 12:29:37 -07002009 goto finish;
Felipe Balbi550a7372008-07-24 12:27:36 +03002010 else
Tony Lindgrenac33cdb2015-05-01 12:29:37 -07002011 dev_err(musb->controller, "error: rx_dma failed\n");
Felipe Balbi550a7372008-07-24 12:27:36 +03002012 }
Felipe Balbi550a7372008-07-24 12:27:36 +03002013
2014 if (!dma) {
Virupax Sadashivpetimath8e8a5512012-08-07 14:46:20 +05302015 unsigned int received_len;
2016
Maulik Mankad496dda72010-09-24 13:44:06 +03002017 /* Unmap the buffer so that CPU can use it */
Daniel Mack8b125df2013-04-10 21:55:50 +02002018 usb_hcd_unmap_urb_for_dma(musb->hcd, urb);
Virupax Sadashivpetimath8e8a5512012-08-07 14:46:20 +05302019
2020 /*
2021 * We need to map sg if the transfer_buffer is
2022 * NULL.
2023 */
2024 if (!urb->transfer_buffer) {
Virupax Sadashivpetimathed74df12013-04-24 08:38:48 +02002025 qh->use_sg = true;
Virupax Sadashivpetimath8e8a5512012-08-07 14:46:20 +05302026 sg_miter_start(&qh->sg_miter, urb->sg, 1,
2027 sg_flags);
2028 }
2029
Virupax Sadashivpetimathed74df12013-04-24 08:38:48 +02002030 if (qh->use_sg) {
Virupax Sadashivpetimath8e8a5512012-08-07 14:46:20 +05302031 if (!sg_miter_next(&qh->sg_miter)) {
2032 dev_err(musb->controller, "error: sg list empty\n");
2033 sg_miter_stop(&qh->sg_miter);
2034 status = -EINVAL;
2035 done = true;
2036 goto finish;
2037 }
2038 urb->transfer_buffer = qh->sg_miter.addr;
2039 received_len = urb->actual_length;
2040 qh->offset = 0x0;
2041 done = musb_host_packet_rx(musb, urb, epnum,
2042 iso_err);
2043 /* Calculate the number of bytes received */
2044 received_len = urb->actual_length -
2045 received_len;
2046 qh->sg_miter.consumed = received_len;
2047 sg_miter_stop(&qh->sg_miter);
2048 } else {
2049 done = musb_host_packet_rx(musb, urb,
2050 epnum, iso_err);
2051 }
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03002052 dev_dbg(musb->controller, "read %spacket\n", done ? "last " : "");
Felipe Balbi550a7372008-07-24 12:27:36 +03002053 }
2054 }
2055
Felipe Balbi550a7372008-07-24 12:27:36 +03002056finish:
2057 urb->actual_length += xfer_len;
2058 qh->offset += xfer_len;
2059 if (done) {
Virupax Sadashivpetimathed74df12013-04-24 08:38:48 +02002060 if (qh->use_sg)
2061 qh->use_sg = false;
Virupax Sadashivpetimath8e8a5512012-08-07 14:46:20 +05302062
Felipe Balbi550a7372008-07-24 12:27:36 +03002063 if (urb->status == -EINPROGRESS)
2064 urb->status = status;
2065 musb_advance_schedule(musb, urb, hw_ep, USB_DIR_IN);
2066 }
2067}
2068
2069/* schedule nodes correspond to peripheral endpoints, like an OHCI QH.
2070 * the software schedule associates multiple such nodes with a given
2071 * host side hardware endpoint + direction; scheduling may activate
2072 * that hardware endpoint.
2073 */
2074static int musb_schedule(
2075 struct musb *musb,
2076 struct musb_qh *qh,
2077 int is_in)
2078{
Rickard Strandqvisteac44dc2014-06-01 15:48:12 +02002079 int idle = 0;
Felipe Balbi550a7372008-07-24 12:27:36 +03002080 int best_diff;
2081 int best_end, epnum;
2082 struct musb_hw_ep *hw_ep = NULL;
2083 struct list_head *head = NULL;
Swaminathan S5274dab2009-12-28 13:40:37 +02002084 u8 toggle;
2085 u8 txtype;
2086 struct urb *urb = next_urb(qh);
Felipe Balbi550a7372008-07-24 12:27:36 +03002087
2088 /* use fixed hardware for control and bulk */
Ajay Kumar Gupta23d15e02008-10-29 15:10:35 +02002089 if (qh->type == USB_ENDPOINT_XFER_CONTROL) {
Felipe Balbi550a7372008-07-24 12:27:36 +03002090 head = &musb->control;
2091 hw_ep = musb->control_ep;
Felipe Balbi550a7372008-07-24 12:27:36 +03002092 goto success;
2093 }
2094
2095 /* else, periodic transfers get muxed to other endpoints */
2096
Sergei Shtylyov5d67a852009-02-24 15:23:34 -08002097 /*
2098 * We know this qh hasn't been scheduled, so all we need to do
Felipe Balbi550a7372008-07-24 12:27:36 +03002099 * is choose which hardware endpoint to put it on ...
2100 *
2101 * REVISIT what we really want here is a regular schedule tree
Sergei Shtylyov5d67a852009-02-24 15:23:34 -08002102 * like e.g. OHCI uses.
Felipe Balbi550a7372008-07-24 12:27:36 +03002103 */
2104 best_diff = 4096;
2105 best_end = -1;
2106
Sergei Shtylyov5d67a852009-02-24 15:23:34 -08002107 for (epnum = 1, hw_ep = musb->endpoints + 1;
2108 epnum < musb->nr_endpoints;
2109 epnum++, hw_ep++) {
Felipe Balbi550a7372008-07-24 12:27:36 +03002110 int diff;
2111
Sergei Shtylyov3e5c6dc2009-03-27 12:55:16 -07002112 if (musb_ep_get_qh(hw_ep, is_in) != NULL)
Felipe Balbi550a7372008-07-24 12:27:36 +03002113 continue;
Sergei Shtylyov5d67a852009-02-24 15:23:34 -08002114
Felipe Balbi550a7372008-07-24 12:27:36 +03002115 if (hw_ep == musb->bulk_ep)
2116 continue;
2117
2118 if (is_in)
Ajay Kumar Guptaa483d702009-04-03 16:16:17 -07002119 diff = hw_ep->max_packet_sz_rx;
Felipe Balbi550a7372008-07-24 12:27:36 +03002120 else
Ajay Kumar Guptaa483d702009-04-03 16:16:17 -07002121 diff = hw_ep->max_packet_sz_tx;
2122 diff -= (qh->maxpacket * qh->hb_mult);
Felipe Balbi550a7372008-07-24 12:27:36 +03002123
Ajay Kumar Gupta23d15e02008-10-29 15:10:35 +02002124 if (diff >= 0 && best_diff > diff) {
Swaminathan S5274dab2009-12-28 13:40:37 +02002125
2126 /*
2127 * Mentor controller has a bug in that if we schedule
2128 * a BULK Tx transfer on an endpoint that had earlier
2129 * handled ISOC then the BULK transfer has to start on
2130 * a zero toggle. If the BULK transfer starts on a 1
2131 * toggle then this transfer will fail as the mentor
2132 * controller starts the Bulk transfer on a 0 toggle
2133 * irrespective of the programming of the toggle bits
2134 * in the TXCSR register. Check for this condition
2135 * while allocating the EP for a Tx Bulk transfer. If
2136 * so skip this EP.
2137 */
2138 hw_ep = musb->endpoints + epnum;
2139 toggle = usb_gettoggle(urb->dev, qh->epnum, !is_in);
2140 txtype = (musb_readb(hw_ep->regs, MUSB_TXTYPE)
2141 >> 4) & 0x3;
2142 if (!is_in && (qh->type == USB_ENDPOINT_XFER_BULK) &&
2143 toggle && (txtype == USB_ENDPOINT_XFER_ISOC))
2144 continue;
2145
Felipe Balbi550a7372008-07-24 12:27:36 +03002146 best_diff = diff;
2147 best_end = epnum;
2148 }
2149 }
Ajay Kumar Gupta23d15e02008-10-29 15:10:35 +02002150 /* use bulk reserved ep1 if no other ep is free */
Felipe Balbiaa5cbbe2008-11-17 09:08:16 +02002151 if (best_end < 0 && qh->type == USB_ENDPOINT_XFER_BULK) {
Ajay Kumar Gupta23d15e02008-10-29 15:10:35 +02002152 hw_ep = musb->bulk_ep;
2153 if (is_in)
2154 head = &musb->in_bulk;
2155 else
2156 head = &musb->out_bulk;
Ajay Kumar Gupta1e0320f2009-02-24 15:26:13 -08002157
Ajay Kumar Guptaf2838622012-07-19 13:41:59 +05302158 /* Enable bulk RX/TX NAK timeout scheme when bulk requests are
Rahul Bedarkar5ae477b2014-01-02 19:27:47 +05302159 * multiplexed. This scheme does not work in high speed to full
Ajay Kumar Gupta1e0320f2009-02-24 15:26:13 -08002160 * speed scenario as NAK interrupts are not coming from a
2161 * full speed device connected to a high speed device.
2162 * NAK timeout interval is 8 (128 uframe or 16ms) for HS and
2163 * 4 (8 frame or 8ms) for FS device.
2164 */
Ajay Kumar Guptaf2838622012-07-19 13:41:59 +05302165 if (qh->dev)
Ajay Kumar Gupta1e0320f2009-02-24 15:26:13 -08002166 qh->intv_reg =
2167 (USB_SPEED_HIGH == qh->dev->speed) ? 8 : 4;
Ajay Kumar Gupta23d15e02008-10-29 15:10:35 +02002168 goto success;
2169 } else if (best_end < 0) {
Felipe Balbi550a7372008-07-24 12:27:36 +03002170 return -ENOSPC;
Ajay Kumar Gupta23d15e02008-10-29 15:10:35 +02002171 }
Felipe Balbi550a7372008-07-24 12:27:36 +03002172
2173 idle = 1;
Ajay Kumar Gupta23d15e02008-10-29 15:10:35 +02002174 qh->mux = 0;
Felipe Balbi550a7372008-07-24 12:27:36 +03002175 hw_ep = musb->endpoints + best_end;
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03002176 dev_dbg(musb->controller, "qh %p periodic slot %d\n", qh, best_end);
Felipe Balbi550a7372008-07-24 12:27:36 +03002177success:
Ajay Kumar Gupta23d15e02008-10-29 15:10:35 +02002178 if (head) {
2179 idle = list_empty(head);
2180 list_add_tail(&qh->ring, head);
2181 qh->mux = 1;
2182 }
Felipe Balbi550a7372008-07-24 12:27:36 +03002183 qh->hw_ep = hw_ep;
2184 qh->hep->hcpriv = qh;
2185 if (idle)
2186 musb_start_urb(musb, is_in, qh);
2187 return 0;
2188}
2189
2190static int musb_urb_enqueue(
2191 struct usb_hcd *hcd,
2192 struct urb *urb,
2193 gfp_t mem_flags)
2194{
2195 unsigned long flags;
2196 struct musb *musb = hcd_to_musb(hcd);
2197 struct usb_host_endpoint *hep = urb->ep;
David Brownell74bb3502009-03-26 17:36:57 -07002198 struct musb_qh *qh;
Felipe Balbi550a7372008-07-24 12:27:36 +03002199 struct usb_endpoint_descriptor *epd = &hep->desc;
2200 int ret;
2201 unsigned type_reg;
2202 unsigned interval;
2203
2204 /* host role must be active */
2205 if (!is_host_active(musb) || !musb->is_active)
2206 return -ENODEV;
2207
2208 spin_lock_irqsave(&musb->lock, flags);
2209 ret = usb_hcd_link_urb_to_ep(hcd, urb);
David Brownell74bb3502009-03-26 17:36:57 -07002210 qh = ret ? NULL : hep->hcpriv;
2211 if (qh)
2212 urb->hcpriv = qh;
Felipe Balbi550a7372008-07-24 12:27:36 +03002213 spin_unlock_irqrestore(&musb->lock, flags);
Felipe Balbi550a7372008-07-24 12:27:36 +03002214
2215 /* DMA mapping was already done, if needed, and this urb is on
David Brownell74bb3502009-03-26 17:36:57 -07002216 * hep->urb_list now ... so we're done, unless hep wasn't yet
2217 * scheduled onto a live qh.
Felipe Balbi550a7372008-07-24 12:27:36 +03002218 *
2219 * REVISIT best to keep hep->hcpriv valid until the endpoint gets
2220 * disabled, testing for empty qh->ring and avoiding qh setup costs
2221 * except for the first urb queued after a config change.
2222 */
David Brownell74bb3502009-03-26 17:36:57 -07002223 if (qh || ret)
2224 return ret;
Felipe Balbi550a7372008-07-24 12:27:36 +03002225
2226 /* Allocate and initialize qh, minimizing the work done each time
2227 * hw_ep gets reprogrammed, or with irqs blocked. Then schedule it.
2228 *
2229 * REVISIT consider a dedicated qh kmem_cache, so it's harder
2230 * for bugs in other kernel code to break this driver...
2231 */
2232 qh = kzalloc(sizeof *qh, mem_flags);
2233 if (!qh) {
Ajay Kumar Gupta2492e672008-09-11 11:53:21 +03002234 spin_lock_irqsave(&musb->lock, flags);
Felipe Balbi550a7372008-07-24 12:27:36 +03002235 usb_hcd_unlink_urb_from_ep(hcd, urb);
Ajay Kumar Gupta2492e672008-09-11 11:53:21 +03002236 spin_unlock_irqrestore(&musb->lock, flags);
Felipe Balbi550a7372008-07-24 12:27:36 +03002237 return -ENOMEM;
2238 }
2239
2240 qh->hep = hep;
2241 qh->dev = urb->dev;
2242 INIT_LIST_HEAD(&qh->ring);
2243 qh->is_ready = 1;
2244
Kuninori Morimoto29cc8892011-08-23 03:12:03 -07002245 qh->maxpacket = usb_endpoint_maxp(epd);
Ajay Kumar Guptaa483d702009-04-03 16:16:17 -07002246 qh->type = usb_endpoint_type(epd);
Felipe Balbi550a7372008-07-24 12:27:36 +03002247
Ajay Kumar Guptaa483d702009-04-03 16:16:17 -07002248 /* Bits 11 & 12 of wMaxPacketSize encode high bandwidth multiplier.
2249 * Some musb cores don't support high bandwidth ISO transfers; and
2250 * we don't (yet!) support high bandwidth interrupt transfers.
2251 */
2252 qh->hb_mult = 1 + ((qh->maxpacket >> 11) & 0x03);
2253 if (qh->hb_mult > 1) {
2254 int ok = (qh->type == USB_ENDPOINT_XFER_ISOC);
2255
2256 if (ok)
2257 ok = (usb_pipein(urb->pipe) && musb->hb_iso_rx)
2258 || (usb_pipeout(urb->pipe) && musb->hb_iso_tx);
2259 if (!ok) {
2260 ret = -EMSGSIZE;
2261 goto done;
2262 }
2263 qh->maxpacket &= 0x7ff;
Felipe Balbi550a7372008-07-24 12:27:36 +03002264 }
2265
Julia Lawall96bcd092009-01-24 17:57:24 -08002266 qh->epnum = usb_endpoint_num(epd);
Felipe Balbi550a7372008-07-24 12:27:36 +03002267
2268 /* NOTE: urb->dev->devnum is wrong during SET_ADDRESS */
2269 qh->addr_reg = (u8) usb_pipedevice(urb->pipe);
2270
2271 /* precompute rxtype/txtype/type0 register */
2272 type_reg = (qh->type << 4) | qh->epnum;
2273 switch (urb->dev->speed) {
2274 case USB_SPEED_LOW:
2275 type_reg |= 0xc0;
2276 break;
2277 case USB_SPEED_FULL:
2278 type_reg |= 0x80;
2279 break;
2280 default:
2281 type_reg |= 0x40;
2282 }
2283 qh->type_reg = type_reg;
2284
Sergei Shtylyov136733d2009-02-21 15:31:35 -08002285 /* Precompute RXINTERVAL/TXINTERVAL register */
Felipe Balbi550a7372008-07-24 12:27:36 +03002286 switch (qh->type) {
2287 case USB_ENDPOINT_XFER_INT:
Sergei Shtylyov136733d2009-02-21 15:31:35 -08002288 /*
2289 * Full/low speeds use the linear encoding,
2290 * high speed uses the logarithmic encoding.
2291 */
2292 if (urb->dev->speed <= USB_SPEED_FULL) {
2293 interval = max_t(u8, epd->bInterval, 1);
2294 break;
Felipe Balbi550a7372008-07-24 12:27:36 +03002295 }
2296 /* FALLTHROUGH */
2297 case USB_ENDPOINT_XFER_ISOC:
Sergei Shtylyov136733d2009-02-21 15:31:35 -08002298 /* ISO always uses logarithmic encoding */
2299 interval = min_t(u8, epd->bInterval, 16);
Felipe Balbi550a7372008-07-24 12:27:36 +03002300 break;
2301 default:
2302 /* REVISIT we actually want to use NAK limits, hinting to the
2303 * transfer scheduling logic to try some other qh, e.g. try
2304 * for 2 msec first:
2305 *
2306 * interval = (USB_SPEED_HIGH == urb->dev->speed) ? 16 : 2;
2307 *
2308 * The downside of disabling this is that transfer scheduling
2309 * gets VERY unfair for nonperiodic transfers; a misbehaving
Ajay Kumar Gupta1e0320f2009-02-24 15:26:13 -08002310 * peripheral could make that hurt. That's perfectly normal
2311 * for reads from network or serial adapters ... so we have
2312 * partial NAKlimit support for bulk RX.
Felipe Balbi550a7372008-07-24 12:27:36 +03002313 *
Ajay Kumar Gupta1e0320f2009-02-24 15:26:13 -08002314 * The upside of disabling it is simpler transfer scheduling.
Felipe Balbi550a7372008-07-24 12:27:36 +03002315 */
2316 interval = 0;
2317 }
2318 qh->intv_reg = interval;
2319
2320 /* precompute addressing for external hub/tt ports */
2321 if (musb->is_multipoint) {
2322 struct usb_device *parent = urb->dev->parent;
2323
2324 if (parent != hcd->self.root_hub) {
2325 qh->h_addr_reg = (u8) parent->devnum;
2326
2327 /* set up tt info if needed */
2328 if (urb->dev->tt) {
2329 qh->h_port_reg = (u8) urb->dev->ttport;
Ajay Kumar Guptaae5ad292008-09-11 11:53:20 +03002330 if (urb->dev->tt->hub)
2331 qh->h_addr_reg =
2332 (u8) urb->dev->tt->hub->devnum;
2333 if (urb->dev->tt->multi)
2334 qh->h_addr_reg |= 0x80;
Felipe Balbi550a7372008-07-24 12:27:36 +03002335 }
2336 }
2337 }
2338
2339 /* invariant: hep->hcpriv is null OR the qh that's already scheduled.
2340 * until we get real dma queues (with an entry for each urb/buffer),
2341 * we only have work to do in the former case.
2342 */
2343 spin_lock_irqsave(&musb->lock, flags);
yuzheng ma30677792012-08-15 16:11:40 +08002344 if (hep->hcpriv || !next_urb(qh)) {
Felipe Balbi550a7372008-07-24 12:27:36 +03002345 /* some concurrent activity submitted another urb to hep...
2346 * odd, rare, error prone, but legal.
2347 */
2348 kfree(qh);
Dan Carpenter714bc5e2010-03-25 13:14:27 +02002349 qh = NULL;
Felipe Balbi550a7372008-07-24 12:27:36 +03002350 ret = 0;
2351 } else
2352 ret = musb_schedule(musb, qh,
2353 epd->bEndpointAddress & USB_ENDPOINT_DIR_MASK);
2354
2355 if (ret == 0) {
2356 urb->hcpriv = qh;
2357 /* FIXME set urb->start_frame for iso/intr, it's tested in
2358 * musb_start_urb(), but otherwise only konicawc cares ...
2359 */
2360 }
2361 spin_unlock_irqrestore(&musb->lock, flags);
2362
2363done:
2364 if (ret != 0) {
Ajay Kumar Gupta2492e672008-09-11 11:53:21 +03002365 spin_lock_irqsave(&musb->lock, flags);
Felipe Balbi550a7372008-07-24 12:27:36 +03002366 usb_hcd_unlink_urb_from_ep(hcd, urb);
Ajay Kumar Gupta2492e672008-09-11 11:53:21 +03002367 spin_unlock_irqrestore(&musb->lock, flags);
Felipe Balbi550a7372008-07-24 12:27:36 +03002368 kfree(qh);
2369 }
2370 return ret;
2371}
2372
2373
2374/*
2375 * abort a transfer that's at the head of a hardware queue.
2376 * called with controller locked, irqs blocked
2377 * that hardware queue advances to the next transfer, unless prevented
2378 */
Sergei Shtylyov81ec4e42009-03-27 12:57:50 -07002379static int musb_cleanup_urb(struct urb *urb, struct musb_qh *qh)
Felipe Balbi550a7372008-07-24 12:27:36 +03002380{
2381 struct musb_hw_ep *ep = qh->hw_ep;
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03002382 struct musb *musb = ep->musb;
Felipe Balbi550a7372008-07-24 12:27:36 +03002383 void __iomem *epio = ep->regs;
2384 unsigned hw_end = ep->epnum;
2385 void __iomem *regs = ep->musb->mregs;
Sergei Shtylyov81ec4e42009-03-27 12:57:50 -07002386 int is_in = usb_pipein(urb->pipe);
Felipe Balbi550a7372008-07-24 12:27:36 +03002387 int status = 0;
Sergei Shtylyov81ec4e42009-03-27 12:57:50 -07002388 u16 csr;
Felipe Balbi550a7372008-07-24 12:27:36 +03002389
2390 musb_ep_select(regs, hw_end);
2391
2392 if (is_dma_capable()) {
2393 struct dma_channel *dma;
2394
2395 dma = is_in ? ep->rx_channel : ep->tx_channel;
2396 if (dma) {
2397 status = ep->musb->dma_controller->channel_abort(dma);
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03002398 dev_dbg(musb->controller,
Felipe Balbi550a7372008-07-24 12:27:36 +03002399 "abort %cX%d DMA for urb %p --> %d\n",
2400 is_in ? 'R' : 'T', ep->epnum,
2401 urb, status);
2402 urb->actual_length += dma->actual_len;
2403 }
2404 }
2405
2406 /* turn off DMA requests, discard state, stop polling ... */
Ajay Kumar Gupta692933b2012-03-14 17:33:35 +05302407 if (ep->epnum && is_in) {
Felipe Balbi550a7372008-07-24 12:27:36 +03002408 /* giveback saves bulk toggle */
2409 csr = musb_h_flush_rxfifo(ep, 0);
2410
2411 /* REVISIT we still get an irq; should likely clear the
2412 * endpoint's irq status here to avoid bogus irqs.
2413 * clearing that status is platform-specific...
2414 */
David Brownell78322c12009-03-26 17:38:30 -07002415 } else if (ep->epnum) {
Felipe Balbi550a7372008-07-24 12:27:36 +03002416 musb_h_tx_flush_fifo(ep);
2417 csr = musb_readw(epio, MUSB_TXCSR);
2418 csr &= ~(MUSB_TXCSR_AUTOSET
2419 | MUSB_TXCSR_DMAENAB
2420 | MUSB_TXCSR_H_RXSTALL
2421 | MUSB_TXCSR_H_NAKTIMEOUT
2422 | MUSB_TXCSR_H_ERROR
2423 | MUSB_TXCSR_TXPKTRDY);
2424 musb_writew(epio, MUSB_TXCSR, csr);
2425 /* REVISIT may need to clear FLUSHFIFO ... */
2426 musb_writew(epio, MUSB_TXCSR, csr);
2427 /* flush cpu writebuffer */
2428 csr = musb_readw(epio, MUSB_TXCSR);
David Brownell78322c12009-03-26 17:38:30 -07002429 } else {
2430 musb_h_ep0_flush_fifo(ep);
Felipe Balbi550a7372008-07-24 12:27:36 +03002431 }
2432 if (status == 0)
2433 musb_advance_schedule(ep->musb, urb, ep, is_in);
2434 return status;
2435}
2436
2437static int musb_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
2438{
2439 struct musb *musb = hcd_to_musb(hcd);
2440 struct musb_qh *qh;
Felipe Balbi550a7372008-07-24 12:27:36 +03002441 unsigned long flags;
Sergei Shtylyov22a0d6f2009-03-27 12:56:26 -07002442 int is_in = usb_pipein(urb->pipe);
Felipe Balbi550a7372008-07-24 12:27:36 +03002443 int ret;
2444
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03002445 dev_dbg(musb->controller, "urb=%p, dev%d ep%d%s\n", urb,
Felipe Balbi550a7372008-07-24 12:27:36 +03002446 usb_pipedevice(urb->pipe),
2447 usb_pipeendpoint(urb->pipe),
Sergei Shtylyov22a0d6f2009-03-27 12:56:26 -07002448 is_in ? "in" : "out");
Felipe Balbi550a7372008-07-24 12:27:36 +03002449
2450 spin_lock_irqsave(&musb->lock, flags);
2451 ret = usb_hcd_check_unlink_urb(hcd, urb, status);
2452 if (ret)
2453 goto done;
2454
2455 qh = urb->hcpriv;
2456 if (!qh)
2457 goto done;
2458
Sergei Shtylyov22a0d6f2009-03-27 12:56:26 -07002459 /*
2460 * Any URB not actively programmed into endpoint hardware can be
Sergei Shtylyova2fd8142009-02-21 15:30:45 -08002461 * immediately given back; that's any URB not at the head of an
Felipe Balbi550a7372008-07-24 12:27:36 +03002462 * endpoint queue, unless someday we get real DMA queues. And even
Sergei Shtylyova2fd8142009-02-21 15:30:45 -08002463 * if it's at the head, it might not be known to the hardware...
Felipe Balbi550a7372008-07-24 12:27:36 +03002464 *
Sergei Shtylyov22a0d6f2009-03-27 12:56:26 -07002465 * Otherwise abort current transfer, pending DMA, etc.; urb->status
Felipe Balbi550a7372008-07-24 12:27:36 +03002466 * has already been updated. This is a synchronous abort; it'd be
2467 * OK to hold off until after some IRQ, though.
Sergei Shtylyov22a0d6f2009-03-27 12:56:26 -07002468 *
2469 * NOTE: qh is invalid unless !list_empty(&hep->urb_list)
Felipe Balbi550a7372008-07-24 12:27:36 +03002470 */
Sergei Shtylyov22a0d6f2009-03-27 12:56:26 -07002471 if (!qh->is_ready
2472 || urb->urb_list.prev != &qh->hep->urb_list
2473 || musb_ep_get_qh(qh->hw_ep, is_in) != qh) {
Felipe Balbi550a7372008-07-24 12:27:36 +03002474 int ready = qh->is_ready;
2475
Felipe Balbi550a7372008-07-24 12:27:36 +03002476 qh->is_ready = 0;
Sergei Shtylyovc9cd06b2009-03-27 12:58:31 -07002477 musb_giveback(musb, urb, 0);
Felipe Balbi550a7372008-07-24 12:27:36 +03002478 qh->is_ready = ready;
Sergei Shtylyova2fd8142009-02-21 15:30:45 -08002479
2480 /* If nothing else (usually musb_giveback) is using it
2481 * and its URB list has emptied, recycle this qh.
2482 */
2483 if (ready && list_empty(&qh->hep->urb_list)) {
2484 qh->hep->hcpriv = NULL;
2485 list_del(&qh->ring);
2486 kfree(qh);
2487 }
Felipe Balbi550a7372008-07-24 12:27:36 +03002488 } else
Sergei Shtylyov81ec4e42009-03-27 12:57:50 -07002489 ret = musb_cleanup_urb(urb, qh);
Felipe Balbi550a7372008-07-24 12:27:36 +03002490done:
2491 spin_unlock_irqrestore(&musb->lock, flags);
2492 return ret;
2493}
2494
2495/* disable an endpoint */
2496static void
2497musb_h_disable(struct usb_hcd *hcd, struct usb_host_endpoint *hep)
2498{
Sergei Shtylyov22a0d6f2009-03-27 12:56:26 -07002499 u8 is_in = hep->desc.bEndpointAddress & USB_DIR_IN;
Felipe Balbi550a7372008-07-24 12:27:36 +03002500 unsigned long flags;
2501 struct musb *musb = hcd_to_musb(hcd);
Sergei Shtylyovdc61d232009-02-21 15:31:01 -08002502 struct musb_qh *qh;
2503 struct urb *urb;
Felipe Balbi550a7372008-07-24 12:27:36 +03002504
Felipe Balbi550a7372008-07-24 12:27:36 +03002505 spin_lock_irqsave(&musb->lock, flags);
2506
Sergei Shtylyovdc61d232009-02-21 15:31:01 -08002507 qh = hep->hcpriv;
2508 if (qh == NULL)
2509 goto exit;
2510
Sergei Shtylyov22a0d6f2009-03-27 12:56:26 -07002511 /* NOTE: qh is invalid unless !list_empty(&hep->urb_list) */
Felipe Balbi550a7372008-07-24 12:27:36 +03002512
Sergei Shtylyov22a0d6f2009-03-27 12:56:26 -07002513 /* Kick the first URB off the hardware, if needed */
Felipe Balbi550a7372008-07-24 12:27:36 +03002514 qh->is_ready = 0;
Sergei Shtylyov22a0d6f2009-03-27 12:56:26 -07002515 if (musb_ep_get_qh(qh->hw_ep, is_in) == qh) {
Felipe Balbi550a7372008-07-24 12:27:36 +03002516 urb = next_urb(qh);
2517
2518 /* make software (then hardware) stop ASAP */
2519 if (!urb->unlinked)
2520 urb->status = -ESHUTDOWN;
2521
2522 /* cleanup */
Sergei Shtylyov81ec4e42009-03-27 12:57:50 -07002523 musb_cleanup_urb(urb, qh);
Felipe Balbi550a7372008-07-24 12:27:36 +03002524
Sergei Shtylyovdc61d232009-02-21 15:31:01 -08002525 /* Then nuke all the others ... and advance the
2526 * queue on hw_ep (e.g. bulk ring) when we're done.
2527 */
2528 while (!list_empty(&hep->urb_list)) {
2529 urb = next_urb(qh);
2530 urb->status = -ESHUTDOWN;
2531 musb_advance_schedule(musb, urb, qh->hw_ep, is_in);
2532 }
2533 } else {
2534 /* Just empty the queue; the hardware is busy with
2535 * other transfers, and since !qh->is_ready nothing
2536 * will activate any of these as it advances.
2537 */
2538 while (!list_empty(&hep->urb_list))
Sergei Shtylyovc9cd06b2009-03-27 12:58:31 -07002539 musb_giveback(musb, next_urb(qh), -ESHUTDOWN);
Felipe Balbi550a7372008-07-24 12:27:36 +03002540
Sergei Shtylyovdc61d232009-02-21 15:31:01 -08002541 hep->hcpriv = NULL;
2542 list_del(&qh->ring);
2543 kfree(qh);
2544 }
2545exit:
Felipe Balbi550a7372008-07-24 12:27:36 +03002546 spin_unlock_irqrestore(&musb->lock, flags);
2547}
2548
2549static int musb_h_get_frame_number(struct usb_hcd *hcd)
2550{
2551 struct musb *musb = hcd_to_musb(hcd);
2552
2553 return musb_readw(musb->mregs, MUSB_FRAME);
2554}
2555
2556static int musb_h_start(struct usb_hcd *hcd)
2557{
2558 struct musb *musb = hcd_to_musb(hcd);
2559
2560 /* NOTE: musb_start() is called when the hub driver turns
2561 * on port power, or when (OTG) peripheral starts.
2562 */
2563 hcd->state = HC_STATE_RUNNING;
2564 musb->port1_status = 0;
2565 return 0;
2566}
2567
2568static void musb_h_stop(struct usb_hcd *hcd)
2569{
2570 musb_stop(hcd_to_musb(hcd));
2571 hcd->state = HC_STATE_HALT;
2572}
2573
2574static int musb_bus_suspend(struct usb_hcd *hcd)
2575{
2576 struct musb *musb = hcd_to_musb(hcd);
David Brownell89368d32009-07-01 03:36:16 -07002577 u8 devctl;
Felipe Balbi550a7372008-07-24 12:27:36 +03002578
Daniel Mack94f72132013-11-25 22:26:41 +01002579 musb_port_suspend(musb, true);
2580
David Brownell89368d32009-07-01 03:36:16 -07002581 if (!is_host_active(musb))
Felipe Balbi550a7372008-07-24 12:27:36 +03002582 return 0;
2583
Antoine Tenarte47d9252014-10-30 18:41:13 +01002584 switch (musb->xceiv->otg->state) {
David Brownell89368d32009-07-01 03:36:16 -07002585 case OTG_STATE_A_SUSPEND:
2586 return 0;
2587 case OTG_STATE_A_WAIT_VRISE:
2588 /* ID could be grounded even if there's no device
2589 * on the other end of the cable. NOTE that the
2590 * A_WAIT_VRISE timers are messy with MUSB...
2591 */
2592 devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
2593 if ((devctl & MUSB_DEVCTL_VBUS) == MUSB_DEVCTL_VBUS)
Antoine Tenarte47d9252014-10-30 18:41:13 +01002594 musb->xceiv->otg->state = OTG_STATE_A_WAIT_BCON;
David Brownell89368d32009-07-01 03:36:16 -07002595 break;
2596 default:
2597 break;
2598 }
2599
2600 if (musb->is_active) {
2601 WARNING("trying to suspend as %s while active\n",
Antoine Tenarte47d9252014-10-30 18:41:13 +01002602 usb_otg_state_string(musb->xceiv->otg->state));
Felipe Balbi550a7372008-07-24 12:27:36 +03002603 return -EBUSY;
2604 } else
2605 return 0;
2606}
2607
2608static int musb_bus_resume(struct usb_hcd *hcd)
2609{
Daniel Mack869c5972013-11-26 13:31:14 +01002610 struct musb *musb = hcd_to_musb(hcd);
2611
2612 if (musb->config &&
2613 musb->config->host_port_deassert_reset_at_resume)
2614 musb_port_reset(musb, false);
2615
Felipe Balbi550a7372008-07-24 12:27:36 +03002616 return 0;
2617}
2618
Ruslan Bilovol8408fd12013-03-29 19:15:21 +02002619#ifndef CONFIG_MUSB_PIO_ONLY
2620
2621#define MUSB_USB_DMA_ALIGN 4
2622
2623struct musb_temp_buffer {
2624 void *kmalloc_ptr;
2625 void *old_xfer_buffer;
2626 u8 data[0];
2627};
2628
2629static void musb_free_temp_buffer(struct urb *urb)
2630{
2631 enum dma_data_direction dir;
2632 struct musb_temp_buffer *temp;
Johan Hovoldd72348f2015-04-23 16:06:50 +02002633 size_t length;
Ruslan Bilovol8408fd12013-03-29 19:15:21 +02002634
2635 if (!(urb->transfer_flags & URB_ALIGNED_TEMP_BUFFER))
2636 return;
2637
2638 dir = usb_urb_dir_in(urb) ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
2639
2640 temp = container_of(urb->transfer_buffer, struct musb_temp_buffer,
2641 data);
2642
2643 if (dir == DMA_FROM_DEVICE) {
Johan Hovoldd72348f2015-04-23 16:06:50 +02002644 if (usb_pipeisoc(urb->pipe))
2645 length = urb->transfer_buffer_length;
2646 else
2647 length = urb->actual_length;
2648
2649 memcpy(temp->old_xfer_buffer, temp->data, length);
Ruslan Bilovol8408fd12013-03-29 19:15:21 +02002650 }
2651 urb->transfer_buffer = temp->old_xfer_buffer;
2652 kfree(temp->kmalloc_ptr);
2653
2654 urb->transfer_flags &= ~URB_ALIGNED_TEMP_BUFFER;
2655}
2656
2657static int musb_alloc_temp_buffer(struct urb *urb, gfp_t mem_flags)
2658{
2659 enum dma_data_direction dir;
2660 struct musb_temp_buffer *temp;
2661 void *kmalloc_ptr;
2662 size_t kmalloc_size;
2663
2664 if (urb->num_sgs || urb->sg ||
2665 urb->transfer_buffer_length == 0 ||
2666 !((uintptr_t)urb->transfer_buffer & (MUSB_USB_DMA_ALIGN - 1)))
2667 return 0;
2668
2669 dir = usb_urb_dir_in(urb) ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
2670
2671 /* Allocate a buffer with enough padding for alignment */
2672 kmalloc_size = urb->transfer_buffer_length +
2673 sizeof(struct musb_temp_buffer) + MUSB_USB_DMA_ALIGN - 1;
2674
2675 kmalloc_ptr = kmalloc(kmalloc_size, mem_flags);
2676 if (!kmalloc_ptr)
2677 return -ENOMEM;
2678
2679 /* Position our struct temp_buffer such that data is aligned */
2680 temp = PTR_ALIGN(kmalloc_ptr, MUSB_USB_DMA_ALIGN);
2681
2682
2683 temp->kmalloc_ptr = kmalloc_ptr;
2684 temp->old_xfer_buffer = urb->transfer_buffer;
2685 if (dir == DMA_TO_DEVICE)
2686 memcpy(temp->data, urb->transfer_buffer,
2687 urb->transfer_buffer_length);
2688 urb->transfer_buffer = temp->data;
2689
2690 urb->transfer_flags |= URB_ALIGNED_TEMP_BUFFER;
2691
2692 return 0;
2693}
2694
2695static int musb_map_urb_for_dma(struct usb_hcd *hcd, struct urb *urb,
2696 gfp_t mem_flags)
2697{
2698 struct musb *musb = hcd_to_musb(hcd);
2699 int ret;
2700
2701 /*
2702 * The DMA engine in RTL1.8 and above cannot handle
2703 * DMA addresses that are not aligned to a 4 byte boundary.
2704 * For such engine implemented (un)map_urb_for_dma hooks.
2705 * Do not use these hooks for RTL<1.8
2706 */
2707 if (musb->hwvers < MUSB_HWVERS_1800)
2708 return usb_hcd_map_urb_for_dma(hcd, urb, mem_flags);
2709
2710 ret = musb_alloc_temp_buffer(urb, mem_flags);
2711 if (ret)
2712 return ret;
2713
2714 ret = usb_hcd_map_urb_for_dma(hcd, urb, mem_flags);
2715 if (ret)
2716 musb_free_temp_buffer(urb);
2717
2718 return ret;
2719}
2720
2721static void musb_unmap_urb_for_dma(struct usb_hcd *hcd, struct urb *urb)
2722{
2723 struct musb *musb = hcd_to_musb(hcd);
2724
2725 usb_hcd_unmap_urb_for_dma(hcd, urb);
2726
2727 /* Do not use this hook for RTL<1.8 (see description above) */
2728 if (musb->hwvers < MUSB_HWVERS_1800)
2729 return;
2730
2731 musb_free_temp_buffer(urb);
2732}
2733#endif /* !CONFIG_MUSB_PIO_ONLY */
2734
Daniel Mack74c2e932013-04-10 21:55:45 +02002735static const struct hc_driver musb_hc_driver = {
Felipe Balbi550a7372008-07-24 12:27:36 +03002736 .description = "musb-hcd",
2737 .product_desc = "MUSB HDRC host driver",
Daniel Mack74c2e932013-04-10 21:55:45 +02002738 .hcd_priv_size = sizeof(struct musb *),
Bin Liuf551e132016-04-25 15:53:30 -05002739 .flags = HCD_USB2 | HCD_MEMORY,
Felipe Balbi550a7372008-07-24 12:27:36 +03002740
2741 /* not using irq handler or reset hooks from usbcore, since
2742 * those must be shared with peripheral code for OTG configs
2743 */
2744
2745 .start = musb_h_start,
2746 .stop = musb_h_stop,
2747
2748 .get_frame_number = musb_h_get_frame_number,
2749
2750 .urb_enqueue = musb_urb_enqueue,
2751 .urb_dequeue = musb_urb_dequeue,
2752 .endpoint_disable = musb_h_disable,
2753
Ruslan Bilovol8408fd12013-03-29 19:15:21 +02002754#ifndef CONFIG_MUSB_PIO_ONLY
2755 .map_urb_for_dma = musb_map_urb_for_dma,
2756 .unmap_urb_for_dma = musb_unmap_urb_for_dma,
2757#endif
2758
Felipe Balbi550a7372008-07-24 12:27:36 +03002759 .hub_status_data = musb_hub_status_data,
2760 .hub_control = musb_hub_control,
2761 .bus_suspend = musb_bus_suspend,
2762 .bus_resume = musb_bus_resume,
2763 /* .start_port_reset = NULL, */
2764 /* .hub_irq_enable = NULL, */
2765};
Daniel Mack0b3eba42013-04-10 21:55:42 +02002766
Daniel Mack74c2e932013-04-10 21:55:45 +02002767int musb_host_alloc(struct musb *musb)
2768{
2769 struct device *dev = musb->controller;
2770
2771 /* usbcore sets dev->driver_data to hcd, and sometimes uses that... */
2772 musb->hcd = usb_create_hcd(&musb_hc_driver, dev, dev_name(dev));
2773 if (!musb->hcd)
2774 return -EINVAL;
2775
2776 *musb->hcd->hcd_priv = (unsigned long) musb;
2777 musb->hcd->self.uses_pio_for_control = 1;
2778 musb->hcd->uses_new_polling = 1;
2779 musb->hcd->has_tt = 1;
2780
2781 return 0;
2782}
2783
2784void musb_host_cleanup(struct musb *musb)
2785{
Sebastian Andrzej Siewior90474282013-08-20 18:35:44 +02002786 if (musb->port_mode == MUSB_PORT_MODE_GADGET)
2787 return;
Daniel Mack74c2e932013-04-10 21:55:45 +02002788 usb_remove_hcd(musb->hcd);
Daniel Mack74c2e932013-04-10 21:55:45 +02002789}
2790
2791void musb_host_free(struct musb *musb)
2792{
2793 usb_put_hcd(musb->hcd);
2794}
2795
Daniel Mack2cc65fe2013-04-10 21:55:47 +02002796int musb_host_setup(struct musb *musb, int power_budget)
2797{
2798 int ret;
2799 struct usb_hcd *hcd = musb->hcd;
2800
2801 MUSB_HST_MODE(musb);
2802 musb->xceiv->otg->default_a = 1;
Antoine Tenarte47d9252014-10-30 18:41:13 +01002803 musb->xceiv->otg->state = OTG_STATE_A_IDLE;
Daniel Mack2cc65fe2013-04-10 21:55:47 +02002804
2805 otg_set_host(musb->xceiv->otg, &hcd->self);
2806 hcd->self.otg_port = 1;
2807 musb->xceiv->otg->host = &hcd->self;
2808 hcd->power_budget = 2 * (power_budget ? : 250);
2809
2810 ret = usb_add_hcd(hcd, 0, 0);
2811 if (ret < 0)
2812 return ret;
2813
Peter Chen3c9740a2013-11-05 10:46:02 +08002814 device_wakeup_enable(hcd->self.controller);
Daniel Mack2cc65fe2013-04-10 21:55:47 +02002815 return 0;
2816}
2817
Daniel Mack0b3eba42013-04-10 21:55:42 +02002818void musb_host_resume_root_hub(struct musb *musb)
2819{
Daniel Mack74c2e932013-04-10 21:55:45 +02002820 usb_hcd_resume_root_hub(musb->hcd);
Daniel Mack0b3eba42013-04-10 21:55:42 +02002821}
2822
2823void musb_host_poke_root_hub(struct musb *musb)
2824{
2825 MUSB_HST_MODE(musb);
Daniel Mack74c2e932013-04-10 21:55:45 +02002826 if (musb->hcd->status_urb)
2827 usb_hcd_poll_rh_status(musb->hcd);
Daniel Mack0b3eba42013-04-10 21:55:42 +02002828 else
Daniel Mack74c2e932013-04-10 21:55:45 +02002829 usb_hcd_resume_root_hub(musb->hcd);
Daniel Mack0b3eba42013-04-10 21:55:42 +02002830}