blob: dd1ebde0278668b2f62fbdbb21cb936a59346c52 [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
Tony Lindgren754fe4a2015-05-01 12:29:32 -0700636static int 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 return 0;
675}
676
677static int musb_tx_dma_set_mode_cppi_tusb(struct dma_controller *dma,
678 struct musb_hw_ep *hw_ep,
679 struct musb_qh *qh,
680 struct urb *urb,
681 u32 offset,
682 u32 *length,
683 u8 *mode)
684{
685 struct dma_channel *channel = hw_ep->tx_channel;
686
Tony Lindgrenf8e9f34f2015-05-01 12:29:27 -0700687 if (!is_cppi_enabled(hw_ep->musb) && !tusb_dma_omap(hw_ep->musb))
Tony Lindgren754fe4a2015-05-01 12:29:32 -0700688 return -ENODEV;
Sergei Shtylyov6b6e9712009-03-26 18:29:19 -0700689
690 channel->actual_len = 0;
691
692 /*
693 * TX uses "RNDIS" mode automatically but needs help
694 * to identify the zero-length-final-packet case.
695 */
Tony Lindgren754fe4a2015-05-01 12:29:32 -0700696 *mode = (urb->transfer_flags & URB_ZERO_PACKET) ? 1 : 0;
697
698 return 0;
699}
700
701static bool musb_tx_dma_program(struct dma_controller *dma,
702 struct musb_hw_ep *hw_ep, struct musb_qh *qh,
703 struct urb *urb, u32 offset, u32 length)
704{
705 struct dma_channel *channel = hw_ep->tx_channel;
706 u16 pkt_size = qh->maxpacket;
707 u8 mode;
708 int res;
709
710 if (musb_dma_inventra(hw_ep->musb) || musb_dma_ux500(hw_ep->musb))
711 res = musb_tx_dma_set_mode_mentor(dma, hw_ep, qh, urb,
712 offset, &length, &mode);
713 else
714 res = musb_tx_dma_set_mode_cppi_tusb(dma, hw_ep, qh, urb,
715 offset, &length, &mode);
716 if (res)
717 return false;
Sergei Shtylyov6b6e9712009-03-26 18:29:19 -0700718
719 qh->segsize = length;
720
Santosh Shilimkar4c647332010-09-20 10:32:07 +0300721 /*
722 * Ensure the data reaches to main memory before starting
723 * DMA transfer
724 */
725 wmb();
726
Sergei Shtylyov6b6e9712009-03-26 18:29:19 -0700727 if (!dma->channel_program(channel, pkt_size, mode,
728 urb->transfer_dma + offset, length)) {
Tony Lindgren754fe4a2015-05-01 12:29:32 -0700729 void __iomem *epio = hw_ep->regs;
730 u16 csr;
731
Sergei Shtylyov6b6e9712009-03-26 18:29:19 -0700732 dma->channel_release(channel);
733 hw_ep->tx_channel = NULL;
734
735 csr = musb_readw(epio, MUSB_TXCSR);
736 csr &= ~(MUSB_TXCSR_AUTOSET | MUSB_TXCSR_DMAENAB);
737 musb_writew(epio, MUSB_TXCSR, csr | MUSB_TXCSR_H_WZC_BITS);
738 return false;
739 }
740 return true;
741}
Felipe Balbi550a7372008-07-24 12:27:36 +0300742
743/*
744 * Program an HDRC endpoint as per the given URB
745 * Context: irqs blocked, controller lock held
746 */
747static void musb_ep_program(struct musb *musb, u8 epnum,
Sergei Shtylyov6b6e9712009-03-26 18:29:19 -0700748 struct urb *urb, int is_out,
749 u8 *buf, u32 offset, u32 len)
Felipe Balbi550a7372008-07-24 12:27:36 +0300750{
751 struct dma_controller *dma_controller;
752 struct dma_channel *dma_channel;
753 u8 dma_ok;
754 void __iomem *mbase = musb->mregs;
755 struct musb_hw_ep *hw_ep = musb->endpoints + epnum;
756 void __iomem *epio = hw_ep->regs;
Sergei Shtylyov3e5c6dc2009-03-27 12:55:16 -0700757 struct musb_qh *qh = musb_ep_get_qh(hw_ep, !is_out);
758 u16 packet_sz = qh->maxpacket;
Ajay Kumar Gupta31321222012-07-20 11:07:22 +0530759 u8 use_dma = 1;
760 u16 csr;
Felipe Balbi550a7372008-07-24 12:27:36 +0300761
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300762 dev_dbg(musb->controller, "%s hw%d urb %p spd%d dev%d ep%d%s "
Felipe Balbi550a7372008-07-24 12:27:36 +0300763 "h_addr%02x h_port%02x bytes %d\n",
764 is_out ? "-->" : "<--",
765 epnum, urb, urb->dev->speed,
766 qh->addr_reg, qh->epnum, is_out ? "out" : "in",
767 qh->h_addr_reg, qh->h_port_reg,
768 len);
769
770 musb_ep_select(mbase, epnum);
771
Ajay Kumar Gupta31321222012-07-20 11:07:22 +0530772 if (is_out && !len) {
773 use_dma = 0;
774 csr = musb_readw(epio, MUSB_TXCSR);
775 csr &= ~MUSB_TXCSR_DMAENAB;
776 musb_writew(epio, MUSB_TXCSR, csr);
777 hw_ep->tx_channel = NULL;
778 }
779
Felipe Balbi550a7372008-07-24 12:27:36 +0300780 /* candidate for DMA? */
781 dma_controller = musb->dma_controller;
Ajay Kumar Gupta31321222012-07-20 11:07:22 +0530782 if (use_dma && is_dma_capable() && epnum && dma_controller) {
Felipe Balbi550a7372008-07-24 12:27:36 +0300783 dma_channel = is_out ? hw_ep->tx_channel : hw_ep->rx_channel;
784 if (!dma_channel) {
785 dma_channel = dma_controller->channel_alloc(
786 dma_controller, hw_ep, is_out);
787 if (is_out)
788 hw_ep->tx_channel = dma_channel;
789 else
790 hw_ep->rx_channel = dma_channel;
791 }
792 } else
793 dma_channel = NULL;
794
795 /* make sure we clear DMAEnab, autoSet bits from previous run */
796
797 /* OUT/transmit/EP0 or IN/receive? */
798 if (is_out) {
799 u16 csr;
800 u16 int_txe;
801 u16 load_count;
802
803 csr = musb_readw(epio, MUSB_TXCSR);
804
805 /* disable interrupt in case we flush */
Sebastian Andrzej Siewiorb18d26f2012-10-30 19:52:26 +0100806 int_txe = musb->intrtxe;
Felipe Balbi550a7372008-07-24 12:27:36 +0300807 musb_writew(mbase, MUSB_INTRTXE, int_txe & ~(1 << epnum));
808
809 /* general endpoint setup */
810 if (epnum) {
Felipe Balbi550a7372008-07-24 12:27:36 +0300811 /* flush all old state, set default */
supriya karantha70b8442013-01-04 17:10:33 +0530812 /*
813 * We could be flushing valid
814 * packets in double buffering
815 * case
816 */
817 if (!hw_ep->tx_double_buffered)
818 musb_h_tx_flush_fifo(hw_ep);
Sergei Shtylyovb6e434a2009-03-26 18:27:47 -0700819
820 /*
821 * We must not clear the DMAMODE bit before or in
822 * the same cycle with the DMAENAB bit, so we clear
823 * the latter first...
824 */
Felipe Balbi550a7372008-07-24 12:27:36 +0300825 csr &= ~(MUSB_TXCSR_H_NAKTIMEOUT
Sergei Shtylyovb6e434a2009-03-26 18:27:47 -0700826 | MUSB_TXCSR_AUTOSET
827 | MUSB_TXCSR_DMAENAB
Felipe Balbi550a7372008-07-24 12:27:36 +0300828 | MUSB_TXCSR_FRCDATATOG
829 | MUSB_TXCSR_H_RXSTALL
830 | MUSB_TXCSR_H_ERROR
831 | MUSB_TXCSR_TXPKTRDY
832 );
833 csr |= MUSB_TXCSR_MODE;
834
supriya karantha70b8442013-01-04 17:10:33 +0530835 if (!hw_ep->tx_double_buffered) {
836 if (usb_gettoggle(urb->dev, qh->epnum, 1))
837 csr |= MUSB_TXCSR_H_WR_DATATOGGLE
838 | MUSB_TXCSR_H_DATATOGGLE;
839 else
840 csr |= MUSB_TXCSR_CLRDATATOG;
841 }
Felipe Balbi550a7372008-07-24 12:27:36 +0300842
Felipe Balbi550a7372008-07-24 12:27:36 +0300843 musb_writew(epio, MUSB_TXCSR, csr);
844 /* REVISIT may need to clear FLUSHFIFO ... */
Sergei Shtylyovb6e434a2009-03-26 18:27:47 -0700845 csr &= ~MUSB_TXCSR_DMAMODE;
Felipe Balbi550a7372008-07-24 12:27:36 +0300846 musb_writew(epio, MUSB_TXCSR, csr);
847 csr = musb_readw(epio, MUSB_TXCSR);
848 } else {
849 /* endpoint 0: just flush */
David Brownell78322c12009-03-26 17:38:30 -0700850 musb_h_ep0_flush_fifo(hw_ep);
Felipe Balbi550a7372008-07-24 12:27:36 +0300851 }
852
853 /* target addr and (for multipoint) hub addr/port */
854 if (musb->is_multipoint) {
Hans de Goede6cc2af62015-03-20 20:11:12 +0100855 musb_write_txfunaddr(musb, epnum, qh->addr_reg);
856 musb_write_txhubaddr(musb, epnum, qh->h_addr_reg);
857 musb_write_txhubport(musb, epnum, qh->h_port_reg);
Felipe Balbi550a7372008-07-24 12:27:36 +0300858/* FIXME if !epnum, do the same for RX ... */
859 } else
860 musb_writeb(mbase, MUSB_FADDR, qh->addr_reg);
861
862 /* protocol/endpoint/interval/NAKlimit */
863 if (epnum) {
864 musb_writeb(epio, MUSB_TXTYPE, qh->type_reg);
supriya karanthf2786282012-12-06 11:16:23 +0530865 if (musb->double_buffer_not_ok) {
Felipe Balbi550a7372008-07-24 12:27:36 +0300866 musb_writew(epio, MUSB_TXMAXP,
Felipe Balbi06624812011-01-21 13:39:20 +0800867 hw_ep->max_packet_sz_tx);
supriya karanthf2786282012-12-06 11:16:23 +0530868 } else if (can_bulk_split(musb, qh->type)) {
869 qh->hb_mult = hw_ep->max_packet_sz_tx
870 / packet_sz;
Ajay Kumar Guptaccc080c2011-12-13 10:32:42 +0530871 musb_writew(epio, MUSB_TXMAXP, packet_sz
supriya karanthf2786282012-12-06 11:16:23 +0530872 | ((qh->hb_mult) - 1) << 11);
873 } else {
Felipe Balbi550a7372008-07-24 12:27:36 +0300874 musb_writew(epio, MUSB_TXMAXP,
Felipe Balbi06624812011-01-21 13:39:20 +0800875 qh->maxpacket |
876 ((qh->hb_mult - 1) << 11));
supriya karanthf2786282012-12-06 11:16:23 +0530877 }
Felipe Balbi550a7372008-07-24 12:27:36 +0300878 musb_writeb(epio, MUSB_TXINTERVAL, qh->intv_reg);
879 } else {
880 musb_writeb(epio, MUSB_NAKLIMIT0, qh->intv_reg);
881 if (musb->is_multipoint)
882 musb_writeb(epio, MUSB_TYPE0,
883 qh->type_reg);
884 }
885
886 if (can_bulk_split(musb, qh->type))
887 load_count = min((u32) hw_ep->max_packet_sz_tx,
888 len);
889 else
890 load_count = min((u32) packet_sz, len);
891
Sergei Shtylyov6b6e9712009-03-26 18:29:19 -0700892 if (dma_channel && musb_tx_dma_program(dma_controller,
893 hw_ep, qh, urb, offset, len))
894 load_count = 0;
Felipe Balbi550a7372008-07-24 12:27:36 +0300895
896 if (load_count) {
Felipe Balbi550a7372008-07-24 12:27:36 +0300897 /* PIO to load FIFO */
898 qh->segsize = load_count;
Virupax Sadashivpetimath8e8a5512012-08-07 14:46:20 +0530899 if (!buf) {
900 sg_miter_start(&qh->sg_miter, urb->sg, 1,
901 SG_MITER_ATOMIC
902 | SG_MITER_FROM_SG);
903 if (!sg_miter_next(&qh->sg_miter)) {
904 dev_err(musb->controller,
905 "error: sg"
906 "list empty\n");
907 sg_miter_stop(&qh->sg_miter);
908 goto finish;
909 }
910 buf = qh->sg_miter.addr + urb->sg->offset +
911 urb->actual_length;
912 load_count = min_t(u32, load_count,
913 qh->sg_miter.length);
914 musb_write_fifo(hw_ep, load_count, buf);
915 qh->sg_miter.consumed = load_count;
916 sg_miter_stop(&qh->sg_miter);
917 } else
918 musb_write_fifo(hw_ep, load_count, buf);
Felipe Balbi550a7372008-07-24 12:27:36 +0300919 }
Virupax Sadashivpetimath8e8a5512012-08-07 14:46:20 +0530920finish:
Felipe Balbi550a7372008-07-24 12:27:36 +0300921 /* re-enable interrupt */
922 musb_writew(mbase, MUSB_INTRTXE, int_txe);
923
924 /* IN/receive */
925 } else {
926 u16 csr;
927
928 if (hw_ep->rx_reinit) {
Hans de Goede0cb74b32015-03-20 20:11:11 +0100929 musb_rx_reinit(musb, qh, epnum);
Felipe Balbi550a7372008-07-24 12:27:36 +0300930
931 /* init new state: toggle and NYET, maybe DMA later */
932 if (usb_gettoggle(urb->dev, qh->epnum, 0))
933 csr = MUSB_RXCSR_H_WR_DATATOGGLE
934 | MUSB_RXCSR_H_DATATOGGLE;
935 else
936 csr = 0;
937 if (qh->type == USB_ENDPOINT_XFER_INT)
938 csr |= MUSB_RXCSR_DISNYET;
939
940 } else {
941 csr = musb_readw(hw_ep->regs, MUSB_RXCSR);
942
943 if (csr & (MUSB_RXCSR_RXPKTRDY
944 | MUSB_RXCSR_DMAENAB
945 | MUSB_RXCSR_H_REQPKT))
946 ERR("broken !rx_reinit, ep%d csr %04x\n",
947 hw_ep->epnum, csr);
948
949 /* scrub any stale state, leaving toggle alone */
950 csr &= MUSB_RXCSR_DISNYET;
951 }
952
953 /* kick things off */
954
Tony Lindgrenf8e9f34f2015-05-01 12:29:27 -0700955 if ((is_cppi_enabled(musb) || tusb_dma_omap(musb)) && dma_channel) {
Sergei Shtylyovc51e36d2011-05-07 19:44:13 +0400956 /* Candidate for DMA */
957 dma_channel->actual_len = 0L;
958 qh->segsize = len;
Felipe Balbi550a7372008-07-24 12:27:36 +0300959
Sergei Shtylyovc51e36d2011-05-07 19:44:13 +0400960 /* AUTOREQ is in a DMA register */
961 musb_writew(hw_ep->regs, MUSB_RXCSR, csr);
962 csr = musb_readw(hw_ep->regs, MUSB_RXCSR);
Felipe Balbi550a7372008-07-24 12:27:36 +0300963
Sergei Shtylyovc51e36d2011-05-07 19:44:13 +0400964 /*
965 * Unless caller treats short RX transfers as
966 * errors, we dare not queue multiple transfers.
967 */
968 dma_ok = dma_controller->channel_program(dma_channel,
969 packet_sz, !(urb->transfer_flags &
970 URB_SHORT_NOT_OK),
971 urb->transfer_dma + offset,
972 qh->segsize);
973 if (!dma_ok) {
974 dma_controller->channel_release(dma_channel);
975 hw_ep->rx_channel = dma_channel = NULL;
976 } else
977 csr |= MUSB_RXCSR_DMAENAB;
Felipe Balbi550a7372008-07-24 12:27:36 +0300978 }
979
980 csr |= MUSB_RXCSR_H_REQPKT;
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300981 dev_dbg(musb->controller, "RXCSR%d := %04x\n", epnum, csr);
Felipe Balbi550a7372008-07-24 12:27:36 +0300982 musb_writew(hw_ep->regs, MUSB_RXCSR, csr);
983 csr = musb_readw(hw_ep->regs, MUSB_RXCSR);
984 }
985}
986
Ajay Kumar Guptaf2838622012-07-19 13:41:59 +0530987/* Schedule next QH from musb->in_bulk/out_bulk and move the current qh to
988 * the end; avoids starvation for other endpoints.
989 */
990static void musb_bulk_nak_timeout(struct musb *musb, struct musb_hw_ep *ep,
991 int is_in)
992{
993 struct dma_channel *dma;
994 struct urb *urb;
995 void __iomem *mbase = musb->mregs;
996 void __iomem *epio = ep->regs;
997 struct musb_qh *cur_qh, *next_qh;
998 u16 rx_csr, tx_csr;
999
1000 musb_ep_select(mbase, ep->epnum);
1001 if (is_in) {
1002 dma = is_dma_capable() ? ep->rx_channel : NULL;
1003
1004 /* clear nak timeout bit */
1005 rx_csr = musb_readw(epio, MUSB_RXCSR);
1006 rx_csr |= MUSB_RXCSR_H_WZC_BITS;
1007 rx_csr &= ~MUSB_RXCSR_DATAERROR;
1008 musb_writew(epio, MUSB_RXCSR, rx_csr);
1009
1010 cur_qh = first_qh(&musb->in_bulk);
1011 } else {
1012 dma = is_dma_capable() ? ep->tx_channel : NULL;
1013
1014 /* clear nak timeout bit */
1015 tx_csr = musb_readw(epio, MUSB_TXCSR);
1016 tx_csr |= MUSB_TXCSR_H_WZC_BITS;
1017 tx_csr &= ~MUSB_TXCSR_H_NAKTIMEOUT;
1018 musb_writew(epio, MUSB_TXCSR, tx_csr);
1019
1020 cur_qh = first_qh(&musb->out_bulk);
1021 }
1022 if (cur_qh) {
1023 urb = next_urb(cur_qh);
1024 if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) {
1025 dma->status = MUSB_DMA_STATUS_CORE_ABORT;
1026 musb->dma_controller->channel_abort(dma);
1027 urb->actual_length += dma->actual_len;
1028 dma->actual_len = 0L;
1029 }
1030 musb_save_toggle(cur_qh, is_in, urb);
1031
1032 if (is_in) {
1033 /* move cur_qh to end of queue */
1034 list_move_tail(&cur_qh->ring, &musb->in_bulk);
1035
1036 /* get the next qh from musb->in_bulk */
1037 next_qh = first_qh(&musb->in_bulk);
1038
1039 /* set rx_reinit and schedule the next qh */
1040 ep->rx_reinit = 1;
1041 } else {
1042 /* move cur_qh to end of queue */
1043 list_move_tail(&cur_qh->ring, &musb->out_bulk);
1044
1045 /* get the next qh from musb->out_bulk */
1046 next_qh = first_qh(&musb->out_bulk);
1047
1048 /* set tx_reinit and schedule the next qh */
1049 ep->tx_reinit = 1;
1050 }
1051 musb_start_urb(musb, is_in, next_qh);
1052 }
1053}
Felipe Balbi550a7372008-07-24 12:27:36 +03001054
1055/*
1056 * Service the default endpoint (ep0) as host.
1057 * Return true until it's time to start the status stage.
1058 */
1059static bool musb_h_ep0_continue(struct musb *musb, u16 len, struct urb *urb)
1060{
1061 bool more = false;
1062 u8 *fifo_dest = NULL;
1063 u16 fifo_count = 0;
1064 struct musb_hw_ep *hw_ep = musb->control_ep;
1065 struct musb_qh *qh = hw_ep->in_qh;
1066 struct usb_ctrlrequest *request;
1067
1068 switch (musb->ep0_stage) {
1069 case MUSB_EP0_IN:
1070 fifo_dest = urb->transfer_buffer + urb->actual_length;
Sergei Shtylyov3ecdb9a2009-02-21 15:31:23 -08001071 fifo_count = min_t(size_t, len, urb->transfer_buffer_length -
1072 urb->actual_length);
Felipe Balbi550a7372008-07-24 12:27:36 +03001073 if (fifo_count < len)
1074 urb->status = -EOVERFLOW;
1075
1076 musb_read_fifo(hw_ep, fifo_count, fifo_dest);
1077
1078 urb->actual_length += fifo_count;
1079 if (len < qh->maxpacket) {
1080 /* always terminate on short read; it's
1081 * rarely reported as an error.
1082 */
1083 } else if (urb->actual_length <
1084 urb->transfer_buffer_length)
1085 more = true;
1086 break;
1087 case MUSB_EP0_START:
1088 request = (struct usb_ctrlrequest *) urb->setup_packet;
1089
1090 if (!request->wLength) {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001091 dev_dbg(musb->controller, "start no-DATA\n");
Felipe Balbi550a7372008-07-24 12:27:36 +03001092 break;
1093 } else if (request->bRequestType & USB_DIR_IN) {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001094 dev_dbg(musb->controller, "start IN-DATA\n");
Felipe Balbi550a7372008-07-24 12:27:36 +03001095 musb->ep0_stage = MUSB_EP0_IN;
1096 more = true;
1097 break;
1098 } else {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001099 dev_dbg(musb->controller, "start OUT-DATA\n");
Felipe Balbi550a7372008-07-24 12:27:36 +03001100 musb->ep0_stage = MUSB_EP0_OUT;
1101 more = true;
1102 }
1103 /* FALLTHROUGH */
1104 case MUSB_EP0_OUT:
Sergei Shtylyov3ecdb9a2009-02-21 15:31:23 -08001105 fifo_count = min_t(size_t, qh->maxpacket,
1106 urb->transfer_buffer_length -
1107 urb->actual_length);
Felipe Balbi550a7372008-07-24 12:27:36 +03001108 if (fifo_count) {
1109 fifo_dest = (u8 *) (urb->transfer_buffer
1110 + urb->actual_length);
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001111 dev_dbg(musb->controller, "Sending %d byte%s to ep0 fifo %p\n",
David Brownellbb1c9ef2008-11-24 13:06:50 +02001112 fifo_count,
1113 (fifo_count == 1) ? "" : "s",
1114 fifo_dest);
Felipe Balbi550a7372008-07-24 12:27:36 +03001115 musb_write_fifo(hw_ep, fifo_count, fifo_dest);
1116
1117 urb->actual_length += fifo_count;
1118 more = true;
1119 }
1120 break;
1121 default:
1122 ERR("bogus ep0 stage %d\n", musb->ep0_stage);
1123 break;
1124 }
1125
1126 return more;
1127}
1128
1129/*
1130 * Handle default endpoint interrupt as host. Only called in IRQ time
David Brownellc767c1c2008-09-11 11:53:23 +03001131 * from musb_interrupt().
Felipe Balbi550a7372008-07-24 12:27:36 +03001132 *
1133 * called with controller irqlocked
1134 */
1135irqreturn_t musb_h_ep0_irq(struct musb *musb)
1136{
1137 struct urb *urb;
1138 u16 csr, len;
1139 int status = 0;
1140 void __iomem *mbase = musb->mregs;
1141 struct musb_hw_ep *hw_ep = musb->control_ep;
1142 void __iomem *epio = hw_ep->regs;
1143 struct musb_qh *qh = hw_ep->in_qh;
1144 bool complete = false;
1145 irqreturn_t retval = IRQ_NONE;
1146
1147 /* ep0 only has one queue, "in" */
1148 urb = next_urb(qh);
1149
1150 musb_ep_select(mbase, 0);
1151 csr = musb_readw(epio, MUSB_CSR0);
1152 len = (csr & MUSB_CSR0_RXPKTRDY)
1153 ? musb_readb(epio, MUSB_COUNT0)
1154 : 0;
1155
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001156 dev_dbg(musb->controller, "<== csr0 %04x, qh %p, count %d, urb %p, stage %d\n",
Felipe Balbi550a7372008-07-24 12:27:36 +03001157 csr, qh, len, urb, musb->ep0_stage);
1158
1159 /* if we just did status stage, we are done */
1160 if (MUSB_EP0_STATUS == musb->ep0_stage) {
1161 retval = IRQ_HANDLED;
1162 complete = true;
1163 }
1164
1165 /* prepare status */
1166 if (csr & MUSB_CSR0_H_RXSTALL) {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001167 dev_dbg(musb->controller, "STALLING ENDPOINT\n");
Felipe Balbi550a7372008-07-24 12:27:36 +03001168 status = -EPIPE;
1169
1170 } else if (csr & MUSB_CSR0_H_ERROR) {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001171 dev_dbg(musb->controller, "no response, csr0 %04x\n", csr);
Felipe Balbi550a7372008-07-24 12:27:36 +03001172 status = -EPROTO;
1173
1174 } else if (csr & MUSB_CSR0_H_NAKTIMEOUT) {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001175 dev_dbg(musb->controller, "control NAK timeout\n");
Felipe Balbi550a7372008-07-24 12:27:36 +03001176
1177 /* NOTE: this code path would be a good place to PAUSE a
1178 * control transfer, if another one is queued, so that
Ajay Kumar Gupta1e0320f2009-02-24 15:26:13 -08001179 * ep0 is more likely to stay busy. That's already done
1180 * for bulk RX transfers.
Felipe Balbi550a7372008-07-24 12:27:36 +03001181 *
1182 * if (qh->ring.next != &musb->control), then
1183 * we have a candidate... NAKing is *NOT* an error
1184 */
1185 musb_writew(epio, MUSB_CSR0, 0);
1186 retval = IRQ_HANDLED;
1187 }
1188
1189 if (status) {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001190 dev_dbg(musb->controller, "aborting\n");
Felipe Balbi550a7372008-07-24 12:27:36 +03001191 retval = IRQ_HANDLED;
1192 if (urb)
1193 urb->status = status;
1194 complete = true;
1195
1196 /* use the proper sequence to abort the transfer */
1197 if (csr & MUSB_CSR0_H_REQPKT) {
1198 csr &= ~MUSB_CSR0_H_REQPKT;
1199 musb_writew(epio, MUSB_CSR0, csr);
1200 csr &= ~MUSB_CSR0_H_NAKTIMEOUT;
1201 musb_writew(epio, MUSB_CSR0, csr);
1202 } else {
David Brownell78322c12009-03-26 17:38:30 -07001203 musb_h_ep0_flush_fifo(hw_ep);
Felipe Balbi550a7372008-07-24 12:27:36 +03001204 }
1205
1206 musb_writeb(epio, MUSB_NAKLIMIT0, 0);
1207
1208 /* clear it */
1209 musb_writew(epio, MUSB_CSR0, 0);
1210 }
1211
1212 if (unlikely(!urb)) {
1213 /* stop endpoint since we have no place for its data, this
1214 * SHOULD NEVER HAPPEN! */
1215 ERR("no URB for end 0\n");
1216
David Brownell78322c12009-03-26 17:38:30 -07001217 musb_h_ep0_flush_fifo(hw_ep);
Felipe Balbi550a7372008-07-24 12:27:36 +03001218 goto done;
1219 }
1220
1221 if (!complete) {
1222 /* call common logic and prepare response */
1223 if (musb_h_ep0_continue(musb, len, urb)) {
1224 /* more packets required */
1225 csr = (MUSB_EP0_IN == musb->ep0_stage)
1226 ? MUSB_CSR0_H_REQPKT : MUSB_CSR0_TXPKTRDY;
1227 } else {
1228 /* data transfer complete; perform status phase */
1229 if (usb_pipeout(urb->pipe)
1230 || !urb->transfer_buffer_length)
1231 csr = MUSB_CSR0_H_STATUSPKT
1232 | MUSB_CSR0_H_REQPKT;
1233 else
1234 csr = MUSB_CSR0_H_STATUSPKT
1235 | MUSB_CSR0_TXPKTRDY;
1236
Ajay Kumar Gupta3c4653c2014-02-04 15:28:06 +02001237 /* disable ping token in status phase */
1238 csr |= MUSB_CSR0_H_DIS_PING;
1239
Felipe Balbi550a7372008-07-24 12:27:36 +03001240 /* flag status stage */
1241 musb->ep0_stage = MUSB_EP0_STATUS;
1242
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001243 dev_dbg(musb->controller, "ep0 STATUS, csr %04x\n", csr);
Felipe Balbi550a7372008-07-24 12:27:36 +03001244
1245 }
1246 musb_writew(epio, MUSB_CSR0, csr);
1247 retval = IRQ_HANDLED;
1248 } else
1249 musb->ep0_stage = MUSB_EP0_IDLE;
1250
1251 /* call completion handler if done */
1252 if (complete)
1253 musb_advance_schedule(musb, urb, hw_ep, 1);
1254done:
1255 return retval;
1256}
1257
1258
1259#ifdef CONFIG_USB_INVENTRA_DMA
1260
1261/* Host side TX (OUT) using Mentor DMA works as follows:
1262 submit_urb ->
1263 - if queue was empty, Program Endpoint
1264 - ... which starts DMA to fifo in mode 1 or 0
1265
1266 DMA Isr (transfer complete) -> TxAvail()
1267 - Stop DMA (~DmaEnab) (<--- Alert ... currently happens
1268 only in musb_cleanup_urb)
1269 - TxPktRdy has to be set in mode 0 or for
1270 short packets in mode 1.
1271*/
1272
1273#endif
1274
1275/* Service a Tx-Available or dma completion irq for the endpoint */
1276void musb_host_tx(struct musb *musb, u8 epnum)
1277{
1278 int pipe;
1279 bool done = false;
1280 u16 tx_csr;
Sergei Shtylyov6b6e9712009-03-26 18:29:19 -07001281 size_t length = 0;
1282 size_t offset = 0;
Felipe Balbi550a7372008-07-24 12:27:36 +03001283 struct musb_hw_ep *hw_ep = musb->endpoints + epnum;
1284 void __iomem *epio = hw_ep->regs;
Sergei Shtylyov3e5c6dc2009-03-27 12:55:16 -07001285 struct musb_qh *qh = hw_ep->out_qh;
1286 struct urb *urb = next_urb(qh);
Felipe Balbi550a7372008-07-24 12:27:36 +03001287 u32 status = 0;
1288 void __iomem *mbase = musb->mregs;
1289 struct dma_channel *dma;
T. S., Anil Kumarf8afbf7f2010-09-24 13:44:09 +03001290 bool transfer_pending = false;
Felipe Balbi550a7372008-07-24 12:27:36 +03001291
Felipe Balbi550a7372008-07-24 12:27:36 +03001292 musb_ep_select(mbase, epnum);
1293 tx_csr = musb_readw(epio, MUSB_TXCSR);
1294
1295 /* with CPPI, DMA sometimes triggers "extra" irqs */
1296 if (!urb) {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001297 dev_dbg(musb->controller, "extra TX%d ready, csr %04x\n", epnum, tx_csr);
Sergei Shtylyov6b6e9712009-03-26 18:29:19 -07001298 return;
Felipe Balbi550a7372008-07-24 12:27:36 +03001299 }
1300
1301 pipe = urb->pipe;
1302 dma = is_dma_capable() ? hw_ep->tx_channel : NULL;
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001303 dev_dbg(musb->controller, "OUT/TX%d end, csr %04x%s\n", epnum, tx_csr,
Felipe Balbi550a7372008-07-24 12:27:36 +03001304 dma ? ", dma" : "");
1305
1306 /* check for errors */
1307 if (tx_csr & MUSB_TXCSR_H_RXSTALL) {
1308 /* dma was disabled, fifo flushed */
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001309 dev_dbg(musb->controller, "TX end %d stall\n", epnum);
Felipe Balbi550a7372008-07-24 12:27:36 +03001310
1311 /* stall; record URB status */
1312 status = -EPIPE;
1313
1314 } else if (tx_csr & MUSB_TXCSR_H_ERROR) {
1315 /* (NON-ISO) dma was disabled, fifo flushed */
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001316 dev_dbg(musb->controller, "TX 3strikes on ep=%d\n", epnum);
Felipe Balbi550a7372008-07-24 12:27:36 +03001317
1318 status = -ETIMEDOUT;
1319
1320 } else if (tx_csr & MUSB_TXCSR_H_NAKTIMEOUT) {
Ajay Kumar Guptaf2838622012-07-19 13:41:59 +05301321 if (USB_ENDPOINT_XFER_BULK == qh->type && qh->mux == 1
1322 && !list_is_singular(&musb->out_bulk)) {
1323 dev_dbg(musb->controller,
1324 "NAK timeout on TX%d ep\n", epnum);
1325 musb_bulk_nak_timeout(musb, hw_ep, 0);
1326 } else {
1327 dev_dbg(musb->controller,
1328 "TX end=%d device not responding\n", epnum);
1329 /* NOTE: this code path would be a good place to PAUSE a
1330 * transfer, if there's some other (nonperiodic) tx urb
1331 * that could use this fifo. (dma complicates it...)
1332 * That's already done for bulk RX transfers.
1333 *
1334 * if (bulk && qh->ring.next != &musb->out_bulk), then
1335 * we have a candidate... NAKing is *NOT* an error
1336 */
1337 musb_ep_select(mbase, epnum);
1338 musb_writew(epio, MUSB_TXCSR,
1339 MUSB_TXCSR_H_WZC_BITS
1340 | MUSB_TXCSR_TXPKTRDY);
1341 }
1342 return;
Felipe Balbi550a7372008-07-24 12:27:36 +03001343 }
1344
Virupax Sadashivpetimath8e8a5512012-08-07 14:46:20 +05301345done:
Felipe Balbi550a7372008-07-24 12:27:36 +03001346 if (status) {
1347 if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) {
1348 dma->status = MUSB_DMA_STATUS_CORE_ABORT;
Daniel Mack9c547692014-05-26 14:52:35 +02001349 musb->dma_controller->channel_abort(dma);
Felipe Balbi550a7372008-07-24 12:27:36 +03001350 }
1351
1352 /* do the proper sequence to abort the transfer in the
1353 * usb core; the dma engine should already be stopped.
1354 */
1355 musb_h_tx_flush_fifo(hw_ep);
1356 tx_csr &= ~(MUSB_TXCSR_AUTOSET
1357 | MUSB_TXCSR_DMAENAB
1358 | MUSB_TXCSR_H_ERROR
1359 | MUSB_TXCSR_H_RXSTALL
1360 | MUSB_TXCSR_H_NAKTIMEOUT
1361 );
1362
1363 musb_ep_select(mbase, epnum);
1364 musb_writew(epio, MUSB_TXCSR, tx_csr);
1365 /* REVISIT may need to clear FLUSHFIFO ... */
1366 musb_writew(epio, MUSB_TXCSR, tx_csr);
1367 musb_writeb(epio, MUSB_TXINTERVAL, 0);
1368
1369 done = true;
1370 }
1371
1372 /* second cppi case */
1373 if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001374 dev_dbg(musb->controller, "extra TX%d ready, csr %04x\n", epnum, tx_csr);
Sergei Shtylyov6b6e9712009-03-26 18:29:19 -07001375 return;
Felipe Balbi550a7372008-07-24 12:27:36 +03001376 }
1377
Sergei Shtylyovc7bbc052009-03-26 18:26:40 -07001378 if (is_dma_capable() && dma && !status) {
1379 /*
1380 * DMA has completed. But if we're using DMA mode 1 (multi
1381 * packet DMA), we need a terminal TXPKTRDY interrupt before
1382 * we can consider this transfer completed, lest we trash
1383 * its last packet when writing the next URB's data. So we
1384 * switch back to mode 0 to get that interrupt; we'll come
1385 * back here once it happens.
1386 */
1387 if (tx_csr & MUSB_TXCSR_DMAMODE) {
1388 /*
1389 * We shouldn't clear DMAMODE with DMAENAB set; so
1390 * clear them in a safe order. That should be OK
1391 * once TXPKTRDY has been set (and I've never seen
1392 * it being 0 at this moment -- DMA interrupt latency
1393 * is significant) but if it hasn't been then we have
1394 * no choice but to stop being polite and ignore the
1395 * programmer's guide... :-)
1396 *
1397 * Note that we must write TXCSR with TXPKTRDY cleared
1398 * in order not to re-trigger the packet send (this bit
1399 * can't be cleared by CPU), and there's another caveat:
1400 * TXPKTRDY may be set shortly and then cleared in the
1401 * double-buffered FIFO mode, so we do an extra TXCSR
1402 * read for debouncing...
1403 */
1404 tx_csr &= musb_readw(epio, MUSB_TXCSR);
1405 if (tx_csr & MUSB_TXCSR_TXPKTRDY) {
1406 tx_csr &= ~(MUSB_TXCSR_DMAENAB |
1407 MUSB_TXCSR_TXPKTRDY);
1408 musb_writew(epio, MUSB_TXCSR,
1409 tx_csr | MUSB_TXCSR_H_WZC_BITS);
1410 }
1411 tx_csr &= ~(MUSB_TXCSR_DMAMODE |
1412 MUSB_TXCSR_TXPKTRDY);
1413 musb_writew(epio, MUSB_TXCSR,
1414 tx_csr | MUSB_TXCSR_H_WZC_BITS);
1415
1416 /*
1417 * There is no guarantee that we'll get an interrupt
1418 * after clearing DMAMODE as we might have done this
1419 * too late (after TXPKTRDY was cleared by controller).
1420 * Re-read TXCSR as we have spoiled its previous value.
1421 */
1422 tx_csr = musb_readw(epio, MUSB_TXCSR);
1423 }
1424
1425 /*
1426 * We may get here from a DMA completion or TXPKTRDY interrupt.
1427 * In any case, we must check the FIFO status here and bail out
1428 * only if the FIFO still has data -- that should prevent the
1429 * "missed" TXPKTRDY interrupts and deal with double-buffered
1430 * FIFO mode too...
1431 */
1432 if (tx_csr & (MUSB_TXCSR_FIFONOTEMPTY | MUSB_TXCSR_TXPKTRDY)) {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001433 dev_dbg(musb->controller, "DMA complete but packet still in FIFO, "
Sergei Shtylyovc7bbc052009-03-26 18:26:40 -07001434 "CSR %04x\n", tx_csr);
1435 return;
1436 }
1437 }
1438
Felipe Balbi550a7372008-07-24 12:27:36 +03001439 if (!status || dma || usb_pipeisoc(pipe)) {
1440 if (dma)
Sergei Shtylyov6b6e9712009-03-26 18:29:19 -07001441 length = dma->actual_len;
Felipe Balbi550a7372008-07-24 12:27:36 +03001442 else
Sergei Shtylyov6b6e9712009-03-26 18:29:19 -07001443 length = qh->segsize;
1444 qh->offset += length;
Felipe Balbi550a7372008-07-24 12:27:36 +03001445
1446 if (usb_pipeisoc(pipe)) {
1447 struct usb_iso_packet_descriptor *d;
1448
1449 d = urb->iso_frame_desc + qh->iso_idx;
Sergei Shtylyov6b6e9712009-03-26 18:29:19 -07001450 d->actual_length = length;
1451 d->status = status;
Felipe Balbi550a7372008-07-24 12:27:36 +03001452 if (++qh->iso_idx >= urb->number_of_packets) {
1453 done = true;
1454 } else {
1455 d++;
Sergei Shtylyov6b6e9712009-03-26 18:29:19 -07001456 offset = d->offset;
1457 length = d->length;
Felipe Balbi550a7372008-07-24 12:27:36 +03001458 }
T. S., Anil Kumarf8afbf7f2010-09-24 13:44:09 +03001459 } else if (dma && urb->transfer_buffer_length == qh->offset) {
Felipe Balbi550a7372008-07-24 12:27:36 +03001460 done = true;
1461 } else {
1462 /* see if we need to send more data, or ZLP */
1463 if (qh->segsize < qh->maxpacket)
1464 done = true;
1465 else if (qh->offset == urb->transfer_buffer_length
1466 && !(urb->transfer_flags
1467 & URB_ZERO_PACKET))
1468 done = true;
1469 if (!done) {
Sergei Shtylyov6b6e9712009-03-26 18:29:19 -07001470 offset = qh->offset;
1471 length = urb->transfer_buffer_length - offset;
T. S., Anil Kumarf8afbf7f2010-09-24 13:44:09 +03001472 transfer_pending = true;
Felipe Balbi550a7372008-07-24 12:27:36 +03001473 }
1474 }
1475 }
1476
1477 /* urb->status != -EINPROGRESS means request has been faulted,
1478 * so we must abort this transfer after cleanup
1479 */
1480 if (urb->status != -EINPROGRESS) {
1481 done = true;
1482 if (status == 0)
1483 status = urb->status;
1484 }
1485
1486 if (done) {
1487 /* set status */
1488 urb->status = status;
1489 urb->actual_length = qh->offset;
1490 musb_advance_schedule(musb, urb, hw_ep, USB_DIR_OUT);
Sergei Shtylyov6b6e9712009-03-26 18:29:19 -07001491 return;
T. S., Anil Kumarf8afbf7f2010-09-24 13:44:09 +03001492 } else if ((usb_pipeisoc(pipe) || transfer_pending) && dma) {
Sergei Shtylyov6b6e9712009-03-26 18:29:19 -07001493 if (musb_tx_dma_program(musb->dma_controller, hw_ep, qh, urb,
Ajay Kumar Guptadfeffa52009-11-17 15:22:55 +05301494 offset, length)) {
Tony Lindgrenf8e9f34f2015-05-01 12:29:27 -07001495 if (is_cppi_enabled(musb) || tusb_dma_omap(musb))
Ajay Kumar Guptadfeffa52009-11-17 15:22:55 +05301496 musb_h_tx_dma_start(hw_ep);
Sergei Shtylyov6b6e9712009-03-26 18:29:19 -07001497 return;
Ajay Kumar Guptadfeffa52009-11-17 15:22:55 +05301498 }
Sergei Shtylyov6b6e9712009-03-26 18:29:19 -07001499 } else if (tx_csr & MUSB_TXCSR_DMAENAB) {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001500 dev_dbg(musb->controller, "not complete, but DMA enabled?\n");
Sergei Shtylyov6b6e9712009-03-26 18:29:19 -07001501 return;
1502 }
Felipe Balbi550a7372008-07-24 12:27:36 +03001503
Sergei Shtylyov6b6e9712009-03-26 18:29:19 -07001504 /*
1505 * PIO: start next packet in this URB.
1506 *
1507 * REVISIT: some docs say that when hw_ep->tx_double_buffered,
1508 * (and presumably, FIFO is not half-full) we should write *two*
1509 * packets before updating TXCSR; other docs disagree...
1510 */
1511 if (length > qh->maxpacket)
1512 length = qh->maxpacket;
Maulik Mankad496dda72010-09-24 13:44:06 +03001513 /* Unmap the buffer so that CPU can use it */
Daniel Mack8b125df2013-04-10 21:55:50 +02001514 usb_hcd_unmap_urb_for_dma(musb->hcd, urb);
Virupax Sadashivpetimath8e8a5512012-08-07 14:46:20 +05301515
1516 /*
1517 * We need to map sg if the transfer_buffer is
1518 * NULL.
1519 */
1520 if (!urb->transfer_buffer)
Virupax Sadashivpetimathed74df12013-04-24 08:38:48 +02001521 qh->use_sg = true;
Virupax Sadashivpetimath8e8a5512012-08-07 14:46:20 +05301522
Virupax Sadashivpetimathed74df12013-04-24 08:38:48 +02001523 if (qh->use_sg) {
Virupax Sadashivpetimath8e8a5512012-08-07 14:46:20 +05301524 /* sg_miter_start is already done in musb_ep_program */
1525 if (!sg_miter_next(&qh->sg_miter)) {
1526 dev_err(musb->controller, "error: sg list empty\n");
1527 sg_miter_stop(&qh->sg_miter);
1528 status = -EINVAL;
1529 goto done;
1530 }
1531 urb->transfer_buffer = qh->sg_miter.addr;
1532 length = min_t(u32, length, qh->sg_miter.length);
1533 musb_write_fifo(hw_ep, length, urb->transfer_buffer);
1534 qh->sg_miter.consumed = length;
1535 sg_miter_stop(&qh->sg_miter);
1536 } else {
1537 musb_write_fifo(hw_ep, length, urb->transfer_buffer + offset);
1538 }
1539
Sergei Shtylyov6b6e9712009-03-26 18:29:19 -07001540 qh->segsize = length;
Felipe Balbi550a7372008-07-24 12:27:36 +03001541
Virupax Sadashivpetimathed74df12013-04-24 08:38:48 +02001542 if (qh->use_sg) {
Virupax Sadashivpetimath8e8a5512012-08-07 14:46:20 +05301543 if (offset + length >= urb->transfer_buffer_length)
Virupax Sadashivpetimathed74df12013-04-24 08:38:48 +02001544 qh->use_sg = false;
Virupax Sadashivpetimath8e8a5512012-08-07 14:46:20 +05301545 }
1546
Sergei Shtylyov6b6e9712009-03-26 18:29:19 -07001547 musb_ep_select(mbase, epnum);
1548 musb_writew(epio, MUSB_TXCSR,
1549 MUSB_TXCSR_H_WZC_BITS | MUSB_TXCSR_TXPKTRDY);
Felipe Balbi550a7372008-07-24 12:27:36 +03001550}
1551
Tony Lindgren069a3fd2015-05-01 12:29:33 -07001552#ifdef CONFIG_USB_TI_CPPI41_DMA
1553/* Seems to set up ISO for cppi41 and not advance len. See commit c57c41d */
1554static int musb_rx_dma_iso_cppi41(struct dma_controller *dma,
1555 struct musb_hw_ep *hw_ep,
1556 struct musb_qh *qh,
1557 struct urb *urb,
1558 size_t len)
1559{
1560 struct dma_channel *channel = hw_ep->tx_channel;
1561 void __iomem *epio = hw_ep->regs;
1562 dma_addr_t *buf;
1563 u32 length, res;
1564 u16 val;
1565
1566 buf = (void *)urb->iso_frame_desc[qh->iso_idx].offset +
1567 (u32)urb->transfer_dma;
1568
1569 length = urb->iso_frame_desc[qh->iso_idx].length;
1570
1571 val = musb_readw(epio, MUSB_RXCSR);
1572 val |= MUSB_RXCSR_DMAENAB;
1573 musb_writew(hw_ep->regs, MUSB_RXCSR, val);
1574
1575 res = dma->channel_program(channel, qh->maxpacket, 0,
1576 (u32)buf, length);
1577
1578 return res;
1579}
1580#else
1581static inline int musb_rx_dma_iso_cppi41(struct dma_controller *dma,
1582 struct musb_hw_ep *hw_ep,
1583 struct musb_qh *qh,
1584 struct urb *urb,
1585 size_t len)
1586{
1587 return false;
1588}
1589#endif
Felipe Balbi550a7372008-07-24 12:27:36 +03001590
Tony Lindgrencff84bd2015-05-01 12:29:35 -07001591#if defined(CONFIG_USB_INVENTRA_DMA) || defined(CONFIG_USB_UX500_DMA) || \
1592 defined(CONFIG_USB_TI_CPPI41_DMA)
Felipe Balbi550a7372008-07-24 12:27:36 +03001593/* Host side RX (IN) using Mentor DMA works as follows:
1594 submit_urb ->
1595 - if queue was empty, ProgramEndpoint
1596 - first IN token is sent out (by setting ReqPkt)
1597 LinuxIsr -> RxReady()
1598 /\ => first packet is received
1599 | - Set in mode 0 (DmaEnab, ~ReqPkt)
1600 | -> DMA Isr (transfer complete) -> RxReady()
1601 | - Ack receive (~RxPktRdy), turn off DMA (~DmaEnab)
1602 | - if urb not complete, send next IN token (ReqPkt)
1603 | | else complete urb.
1604 | |
1605 ---------------------------
1606 *
1607 * Nuances of mode 1:
1608 * For short packets, no ack (+RxPktRdy) is sent automatically
1609 * (even if AutoClear is ON)
1610 * For full packets, ack (~RxPktRdy) and next IN token (+ReqPkt) is sent
1611 * automatically => major problem, as collecting the next packet becomes
1612 * difficult. Hence mode 1 is not used.
1613 *
1614 * REVISIT
1615 * All we care about at this driver level is that
1616 * (a) all URBs terminate with REQPKT cleared and fifo(s) empty;
1617 * (b) termination conditions are: short RX, or buffer full;
1618 * (c) fault modes include
1619 * - iff URB_SHORT_NOT_OK, short RX status is -EREMOTEIO.
1620 * (and that endpoint's dma queue stops immediately)
1621 * - overflow (full, PLUS more bytes in the terminal packet)
1622 *
1623 * So for example, usb-storage sets URB_SHORT_NOT_OK, and would
1624 * thus be a great candidate for using mode 1 ... for all but the
1625 * last packet of one URB's transfer.
1626 */
Tony Lindgrencff84bd2015-05-01 12:29:35 -07001627static int musb_rx_dma_inventra_cppi41(struct dma_controller *dma,
1628 struct musb_hw_ep *hw_ep,
1629 struct musb_qh *qh,
1630 struct urb *urb,
1631 size_t len)
1632{
1633 struct dma_channel *channel = hw_ep->rx_channel;
1634 void __iomem *epio = hw_ep->regs;
1635 u16 val;
1636 int pipe;
1637 bool done;
Felipe Balbi550a7372008-07-24 12:27:36 +03001638
Tony Lindgrencff84bd2015-05-01 12:29:35 -07001639 pipe = urb->pipe;
1640
1641 if (usb_pipeisoc(pipe)) {
1642 struct usb_iso_packet_descriptor *d;
1643
1644 d = urb->iso_frame_desc + qh->iso_idx;
1645 d->actual_length = len;
1646
1647 /* even if there was an error, we did the dma
1648 * for iso_frame_desc->length
1649 */
1650 if (d->status != -EILSEQ && d->status != -EOVERFLOW)
1651 d->status = 0;
1652
1653 if (++qh->iso_idx >= urb->number_of_packets) {
1654 done = true;
1655 } else {
1656 /* REVISIT: Why ignore return value here? */
1657 if (musb_dma_cppi41(hw_ep->musb))
1658 done = musb_rx_dma_iso_cppi41(dma, hw_ep, qh,
1659 urb, len);
1660 done = false;
1661 }
1662
1663 } else {
1664 /* done if urb buffer is full or short packet is recd */
1665 done = (urb->actual_length + len >=
1666 urb->transfer_buffer_length
1667 || channel->actual_len < qh->maxpacket
1668 || channel->rx_packet_done);
1669 }
1670
1671 /* send IN token for next packet, without AUTOREQ */
1672 if (!done) {
1673 val = musb_readw(epio, MUSB_RXCSR);
1674 val |= MUSB_RXCSR_H_REQPKT;
1675 musb_writew(epio, MUSB_RXCSR, MUSB_RXCSR_H_WZC_BITS | val);
1676 }
1677
1678 return done;
1679}
Tony Lindgrenac33cdb2015-05-01 12:29:37 -07001680
1681/* Disadvantage of using mode 1:
1682 * It's basically usable only for mass storage class; essentially all
1683 * other protocols also terminate transfers on short packets.
1684 *
1685 * Details:
1686 * An extra IN token is sent at the end of the transfer (due to AUTOREQ)
1687 * If you try to use mode 1 for (transfer_buffer_length - 512), and try
1688 * to use the extra IN token to grab the last packet using mode 0, then
1689 * the problem is that you cannot be sure when the device will send the
1690 * last packet and RxPktRdy set. Sometimes the packet is recd too soon
1691 * such that it gets lost when RxCSR is re-set at the end of the mode 1
1692 * transfer, while sometimes it is recd just a little late so that if you
1693 * try to configure for mode 0 soon after the mode 1 transfer is
1694 * completed, you will find rxcount 0. Okay, so you might think why not
1695 * wait for an interrupt when the pkt is recd. Well, you won't get any!
1696 */
1697static int musb_rx_dma_in_inventra_cppi41(struct dma_controller *dma,
1698 struct musb_hw_ep *hw_ep,
1699 struct musb_qh *qh,
1700 struct urb *urb,
1701 size_t len,
1702 u8 iso_err)
1703{
1704 struct musb *musb = hw_ep->musb;
1705 void __iomem *epio = hw_ep->regs;
1706 struct dma_channel *channel = hw_ep->rx_channel;
1707 u16 rx_count, val;
1708 int length, pipe, done;
1709 dma_addr_t buf;
1710
1711 rx_count = musb_readw(epio, MUSB_RXCOUNT);
1712 pipe = urb->pipe;
1713
1714 if (usb_pipeisoc(pipe)) {
1715 int d_status = 0;
1716 struct usb_iso_packet_descriptor *d;
1717
1718 d = urb->iso_frame_desc + qh->iso_idx;
1719
1720 if (iso_err) {
1721 d_status = -EILSEQ;
1722 urb->error_count++;
1723 }
1724 if (rx_count > d->length) {
1725 if (d_status == 0) {
1726 d_status = -EOVERFLOW;
1727 urb->error_count++;
1728 }
1729 dev_dbg(musb->controller, "** OVERFLOW %d into %d\n",
1730 rx_count, d->length);
1731
1732 length = d->length;
1733 } else
1734 length = rx_count;
1735 d->status = d_status;
1736 buf = urb->transfer_dma + d->offset;
1737 } else {
1738 length = rx_count;
1739 buf = urb->transfer_dma + urb->actual_length;
1740 }
1741
1742 channel->desired_mode = 0;
1743#ifdef USE_MODE1
1744 /* because of the issue below, mode 1 will
1745 * only rarely behave with correct semantics.
1746 */
1747 if ((urb->transfer_flags & URB_SHORT_NOT_OK)
1748 && (urb->transfer_buffer_length - urb->actual_length)
1749 > qh->maxpacket)
1750 channel->desired_mode = 1;
1751 if (rx_count < hw_ep->max_packet_sz_rx) {
1752 length = rx_count;
1753 channel->desired_mode = 0;
1754 } else {
1755 length = urb->transfer_buffer_length;
1756 }
1757#endif
1758
1759 /* See comments above on disadvantages of using mode 1 */
1760 val = musb_readw(epio, MUSB_RXCSR);
1761 val &= ~MUSB_RXCSR_H_REQPKT;
1762
1763 if (channel->desired_mode == 0)
1764 val &= ~MUSB_RXCSR_H_AUTOREQ;
1765 else
1766 val |= MUSB_RXCSR_H_AUTOREQ;
1767 val |= MUSB_RXCSR_DMAENAB;
1768
1769 /* autoclear shouldn't be set in high bandwidth */
1770 if (qh->hb_mult == 1)
1771 val |= MUSB_RXCSR_AUTOCLEAR;
1772
1773 musb_writew(epio, MUSB_RXCSR, MUSB_RXCSR_H_WZC_BITS | val);
1774
1775 /* REVISIT if when actual_length != 0,
1776 * transfer_buffer_length needs to be
1777 * adjusted first...
1778 */
1779 done = dma->channel_program(channel, qh->maxpacket,
1780 channel->desired_mode,
1781 buf, length);
1782
1783 if (!done) {
1784 dma->channel_release(channel);
1785 hw_ep->rx_channel = NULL;
1786 channel = NULL;
1787 val = musb_readw(epio, MUSB_RXCSR);
1788 val &= ~(MUSB_RXCSR_DMAENAB
1789 | MUSB_RXCSR_H_AUTOREQ
1790 | MUSB_RXCSR_AUTOCLEAR);
1791 musb_writew(epio, MUSB_RXCSR, val);
1792 }
1793
1794 return done;
1795}
Tony Lindgrencff84bd2015-05-01 12:29:35 -07001796#else
1797static inline int musb_rx_dma_inventra_cppi41(struct dma_controller *dma,
1798 struct musb_hw_ep *hw_ep,
1799 struct musb_qh *qh,
1800 struct urb *urb,
1801 size_t len)
1802{
1803 return false;
1804}
Tony Lindgrenac33cdb2015-05-01 12:29:37 -07001805
1806static inline int musb_rx_dma_in_inventra_cppi41(struct dma_controller *dma,
1807 struct musb_hw_ep *hw_ep,
1808 struct musb_qh *qh,
1809 struct urb *urb,
1810 size_t len,
1811 u8 iso_err)
1812{
1813 return false;
1814}
Felipe Balbi550a7372008-07-24 12:27:36 +03001815#endif
1816
1817/*
1818 * Service an RX interrupt for the given IN endpoint; docs cover bulk, iso,
1819 * and high-bandwidth IN transfer cases.
1820 */
1821void musb_host_rx(struct musb *musb, u8 epnum)
1822{
1823 struct urb *urb;
1824 struct musb_hw_ep *hw_ep = musb->endpoints + epnum;
Tony Lindgrencff84bd2015-05-01 12:29:35 -07001825 struct dma_controller *c = musb->dma_controller;
Felipe Balbi550a7372008-07-24 12:27:36 +03001826 void __iomem *epio = hw_ep->regs;
1827 struct musb_qh *qh = hw_ep->in_qh;
1828 size_t xfer_len;
1829 void __iomem *mbase = musb->mregs;
1830 int pipe;
1831 u16 rx_csr, val;
1832 bool iso_err = false;
1833 bool done = false;
1834 u32 status;
1835 struct dma_channel *dma;
Virupax Sadashivpetimath8e8a5512012-08-07 14:46:20 +05301836 unsigned int sg_flags = SG_MITER_ATOMIC | SG_MITER_TO_SG;
Felipe Balbi550a7372008-07-24 12:27:36 +03001837
1838 musb_ep_select(mbase, epnum);
1839
1840 urb = next_urb(qh);
1841 dma = is_dma_capable() ? hw_ep->rx_channel : NULL;
1842 status = 0;
1843 xfer_len = 0;
1844
1845 rx_csr = musb_readw(epio, MUSB_RXCSR);
1846 val = rx_csr;
1847
1848 if (unlikely(!urb)) {
1849 /* REVISIT -- THIS SHOULD NEVER HAPPEN ... but, at least
1850 * usbtest #11 (unlinks) triggers it regularly, sometimes
1851 * with fifo full. (Only with DMA??)
1852 */
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001853 dev_dbg(musb->controller, "BOGUS RX%d ready, csr %04x, count %d\n", epnum, val,
Felipe Balbi550a7372008-07-24 12:27:36 +03001854 musb_readw(epio, MUSB_RXCOUNT));
1855 musb_h_flush_rxfifo(hw_ep, MUSB_RXCSR_CLRDATATOG);
1856 return;
1857 }
1858
1859 pipe = urb->pipe;
1860
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001861 dev_dbg(musb->controller, "<== hw %d rxcsr %04x, urb actual %d (+dma %zu)\n",
Felipe Balbi550a7372008-07-24 12:27:36 +03001862 epnum, rx_csr, urb->actual_length,
1863 dma ? dma->actual_len : 0);
1864
1865 /* check for errors, concurrent stall & unlink is not really
1866 * handled yet! */
1867 if (rx_csr & MUSB_RXCSR_H_RXSTALL) {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001868 dev_dbg(musb->controller, "RX end %d STALL\n", epnum);
Felipe Balbi550a7372008-07-24 12:27:36 +03001869
1870 /* stall; record URB status */
1871 status = -EPIPE;
1872
1873 } else if (rx_csr & MUSB_RXCSR_H_ERROR) {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001874 dev_dbg(musb->controller, "end %d RX proto error\n", epnum);
Felipe Balbi550a7372008-07-24 12:27:36 +03001875
1876 status = -EPROTO;
1877 musb_writeb(epio, MUSB_RXINTERVAL, 0);
1878
Bin Liub5801212016-05-31 10:05:03 -05001879 rx_csr &= ~MUSB_RXCSR_H_ERROR;
1880 musb_writew(epio, MUSB_RXCSR, rx_csr);
1881
Felipe Balbi550a7372008-07-24 12:27:36 +03001882 } else if (rx_csr & MUSB_RXCSR_DATAERROR) {
1883
1884 if (USB_ENDPOINT_XFER_ISOC != qh->type) {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001885 dev_dbg(musb->controller, "RX end %d NAK timeout\n", epnum);
Ajay Kumar Gupta1e0320f2009-02-24 15:26:13 -08001886
1887 /* NOTE: NAKing is *NOT* an error, so we want to
1888 * continue. Except ... if there's a request for
1889 * another QH, use that instead of starving it.
1890 *
1891 * Devices like Ethernet and serial adapters keep
1892 * reads posted at all times, which will starve
1893 * other devices without this logic.
1894 */
1895 if (usb_pipebulk(urb->pipe)
1896 && qh->mux == 1
1897 && !list_is_singular(&musb->in_bulk)) {
Ajay Kumar Guptaf2838622012-07-19 13:41:59 +05301898 musb_bulk_nak_timeout(musb, hw_ep, 1);
Ajay Kumar Gupta1e0320f2009-02-24 15:26:13 -08001899 return;
1900 }
Felipe Balbi550a7372008-07-24 12:27:36 +03001901 musb_ep_select(mbase, epnum);
Ajay Kumar Gupta1e0320f2009-02-24 15:26:13 -08001902 rx_csr |= MUSB_RXCSR_H_WZC_BITS;
1903 rx_csr &= ~MUSB_RXCSR_DATAERROR;
1904 musb_writew(epio, MUSB_RXCSR, rx_csr);
Felipe Balbi550a7372008-07-24 12:27:36 +03001905
1906 goto finish;
1907 } else {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001908 dev_dbg(musb->controller, "RX end %d ISO data error\n", epnum);
Felipe Balbi550a7372008-07-24 12:27:36 +03001909 /* packet error reported later */
1910 iso_err = true;
1911 }
Ajay Kumar Guptaa483d702009-04-03 16:16:17 -07001912 } else if (rx_csr & MUSB_RXCSR_INCOMPRX) {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001913 dev_dbg(musb->controller, "end %d high bandwidth incomplete ISO packet RX\n",
Ajay Kumar Guptaa483d702009-04-03 16:16:17 -07001914 epnum);
1915 status = -EPROTO;
Felipe Balbi550a7372008-07-24 12:27:36 +03001916 }
1917
1918 /* faults abort the transfer */
1919 if (status) {
1920 /* clean up dma and collect transfer count */
1921 if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) {
1922 dma->status = MUSB_DMA_STATUS_CORE_ABORT;
Daniel Mack9c547692014-05-26 14:52:35 +02001923 musb->dma_controller->channel_abort(dma);
Felipe Balbi550a7372008-07-24 12:27:36 +03001924 xfer_len = dma->actual_len;
1925 }
1926 musb_h_flush_rxfifo(hw_ep, MUSB_RXCSR_CLRDATATOG);
1927 musb_writeb(epio, MUSB_RXINTERVAL, 0);
1928 done = true;
1929 goto finish;
1930 }
1931
1932 if (unlikely(dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY)) {
1933 /* SHOULD NEVER HAPPEN ... but at least DaVinci has done it */
1934 ERR("RX%d dma busy, csr %04x\n", epnum, rx_csr);
1935 goto finish;
1936 }
1937
1938 /* thorough shutdown for now ... given more precise fault handling
1939 * and better queueing support, we might keep a DMA pipeline going
1940 * while processing this irq for earlier completions.
1941 */
1942
1943 /* FIXME this is _way_ too much in-line logic for Mentor DMA */
Tony Lindgren557d5432015-05-01 12:29:34 -07001944 if (!musb_dma_inventra(musb) && !musb_dma_ux500(musb) &&
1945 (rx_csr & MUSB_RXCSR_H_REQPKT)) {
Felipe Balbi550a7372008-07-24 12:27:36 +03001946 /* REVISIT this happened for a while on some short reads...
1947 * the cleanup still needs investigation... looks bad...
1948 * and also duplicates dma cleanup code above ... plus,
1949 * shouldn't this be the "half full" double buffer case?
1950 */
1951 if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) {
1952 dma->status = MUSB_DMA_STATUS_CORE_ABORT;
Daniel Mack9c547692014-05-26 14:52:35 +02001953 musb->dma_controller->channel_abort(dma);
Felipe Balbi550a7372008-07-24 12:27:36 +03001954 xfer_len = dma->actual_len;
1955 done = true;
1956 }
1957
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001958 dev_dbg(musb->controller, "RXCSR%d %04x, reqpkt, len %zu%s\n", epnum, rx_csr,
Felipe Balbi550a7372008-07-24 12:27:36 +03001959 xfer_len, dma ? ", dma" : "");
1960 rx_csr &= ~MUSB_RXCSR_H_REQPKT;
1961
1962 musb_ep_select(mbase, epnum);
1963 musb_writew(epio, MUSB_RXCSR,
1964 MUSB_RXCSR_H_WZC_BITS | rx_csr);
1965 }
Tony Lindgren557d5432015-05-01 12:29:34 -07001966
Felipe Balbi550a7372008-07-24 12:27:36 +03001967 if (dma && (rx_csr & MUSB_RXCSR_DMAENAB)) {
1968 xfer_len = dma->actual_len;
1969
1970 val &= ~(MUSB_RXCSR_DMAENAB
1971 | MUSB_RXCSR_H_AUTOREQ
1972 | MUSB_RXCSR_AUTOCLEAR
1973 | MUSB_RXCSR_RXPKTRDY);
1974 musb_writew(hw_ep->regs, MUSB_RXCSR, val);
1975
Tony Lindgrencff84bd2015-05-01 12:29:35 -07001976 if (musb_dma_inventra(musb) || musb_dma_ux500(musb) ||
1977 musb_dma_cppi41(musb)) {
1978 done = musb_rx_dma_inventra_cppi41(c, hw_ep, qh, urb, xfer_len);
1979 dev_dbg(hw_ep->musb->controller,
1980 "ep %d dma %s, rxcsr %04x, rxcount %d\n",
1981 epnum, done ? "off" : "reset",
1982 musb_readw(epio, MUSB_RXCSR),
1983 musb_readw(epio, MUSB_RXCOUNT));
1984 } else {
1985 done = true;
Ajay Kumar Guptaf82a6892008-10-29 15:10:31 +02001986 }
Felipe Balbi550a7372008-07-24 12:27:36 +03001987
Felipe Balbi550a7372008-07-24 12:27:36 +03001988 } else if (urb->status == -EINPROGRESS) {
1989 /* if no errors, be sure a packet is ready for unloading */
1990 if (unlikely(!(rx_csr & MUSB_RXCSR_RXPKTRDY))) {
1991 status = -EPROTO;
1992 ERR("Rx interrupt with no errors or packet!\n");
1993
1994 /* FIXME this is another "SHOULD NEVER HAPPEN" */
1995
1996/* SCRUB (RX) */
1997 /* do the proper sequence to abort the transfer */
1998 musb_ep_select(mbase, epnum);
1999 val &= ~MUSB_RXCSR_H_REQPKT;
2000 musb_writew(epio, MUSB_RXCSR, val);
2001 goto finish;
2002 }
2003
2004 /* we are expecting IN packets */
Tony Lindgrene530bb82015-05-01 12:29:36 -07002005 if ((musb_dma_inventra(musb) || musb_dma_ux500(musb) ||
2006 musb_dma_cppi41(musb)) && dma) {
Tony Lindgrenac33cdb2015-05-01 12:29:37 -07002007 dev_dbg(hw_ep->musb->controller,
2008 "RX%d count %d, buffer 0x%llx len %d/%d\n",
2009 epnum, musb_readw(epio, MUSB_RXCOUNT),
2010 (unsigned long long) urb->transfer_dma
2011 + urb->actual_length,
2012 qh->offset,
2013 urb->transfer_buffer_length);
Felipe Balbi550a7372008-07-24 12:27:36 +03002014
Cristian Birsan4c2ba0c2016-02-19 10:11:56 +02002015 if (musb_rx_dma_in_inventra_cppi41(c, hw_ep, qh, urb,
2016 xfer_len, iso_err))
Tony Lindgrenac33cdb2015-05-01 12:29:37 -07002017 goto finish;
Felipe Balbi550a7372008-07-24 12:27:36 +03002018 else
Tony Lindgrenac33cdb2015-05-01 12:29:37 -07002019 dev_err(musb->controller, "error: rx_dma failed\n");
Felipe Balbi550a7372008-07-24 12:27:36 +03002020 }
Felipe Balbi550a7372008-07-24 12:27:36 +03002021
2022 if (!dma) {
Virupax Sadashivpetimath8e8a5512012-08-07 14:46:20 +05302023 unsigned int received_len;
2024
Maulik Mankad496dda72010-09-24 13:44:06 +03002025 /* Unmap the buffer so that CPU can use it */
Daniel Mack8b125df2013-04-10 21:55:50 +02002026 usb_hcd_unmap_urb_for_dma(musb->hcd, urb);
Virupax Sadashivpetimath8e8a5512012-08-07 14:46:20 +05302027
2028 /*
2029 * We need to map sg if the transfer_buffer is
2030 * NULL.
2031 */
2032 if (!urb->transfer_buffer) {
Virupax Sadashivpetimathed74df12013-04-24 08:38:48 +02002033 qh->use_sg = true;
Virupax Sadashivpetimath8e8a5512012-08-07 14:46:20 +05302034 sg_miter_start(&qh->sg_miter, urb->sg, 1,
2035 sg_flags);
2036 }
2037
Virupax Sadashivpetimathed74df12013-04-24 08:38:48 +02002038 if (qh->use_sg) {
Virupax Sadashivpetimath8e8a5512012-08-07 14:46:20 +05302039 if (!sg_miter_next(&qh->sg_miter)) {
2040 dev_err(musb->controller, "error: sg list empty\n");
2041 sg_miter_stop(&qh->sg_miter);
2042 status = -EINVAL;
2043 done = true;
2044 goto finish;
2045 }
2046 urb->transfer_buffer = qh->sg_miter.addr;
2047 received_len = urb->actual_length;
2048 qh->offset = 0x0;
2049 done = musb_host_packet_rx(musb, urb, epnum,
2050 iso_err);
2051 /* Calculate the number of bytes received */
2052 received_len = urb->actual_length -
2053 received_len;
2054 qh->sg_miter.consumed = received_len;
2055 sg_miter_stop(&qh->sg_miter);
2056 } else {
2057 done = musb_host_packet_rx(musb, urb,
2058 epnum, iso_err);
2059 }
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03002060 dev_dbg(musb->controller, "read %spacket\n", done ? "last " : "");
Felipe Balbi550a7372008-07-24 12:27:36 +03002061 }
2062 }
2063
Felipe Balbi550a7372008-07-24 12:27:36 +03002064finish:
2065 urb->actual_length += xfer_len;
2066 qh->offset += xfer_len;
2067 if (done) {
Virupax Sadashivpetimathed74df12013-04-24 08:38:48 +02002068 if (qh->use_sg)
2069 qh->use_sg = false;
Virupax Sadashivpetimath8e8a5512012-08-07 14:46:20 +05302070
Felipe Balbi550a7372008-07-24 12:27:36 +03002071 if (urb->status == -EINPROGRESS)
2072 urb->status = status;
2073 musb_advance_schedule(musb, urb, hw_ep, USB_DIR_IN);
2074 }
2075}
2076
2077/* schedule nodes correspond to peripheral endpoints, like an OHCI QH.
2078 * the software schedule associates multiple such nodes with a given
2079 * host side hardware endpoint + direction; scheduling may activate
2080 * that hardware endpoint.
2081 */
2082static int musb_schedule(
2083 struct musb *musb,
2084 struct musb_qh *qh,
2085 int is_in)
2086{
Rickard Strandqvisteac44dc2014-06-01 15:48:12 +02002087 int idle = 0;
Felipe Balbi550a7372008-07-24 12:27:36 +03002088 int best_diff;
2089 int best_end, epnum;
2090 struct musb_hw_ep *hw_ep = NULL;
2091 struct list_head *head = NULL;
Swaminathan S5274dab2009-12-28 13:40:37 +02002092 u8 toggle;
2093 u8 txtype;
2094 struct urb *urb = next_urb(qh);
Felipe Balbi550a7372008-07-24 12:27:36 +03002095
2096 /* use fixed hardware for control and bulk */
Ajay Kumar Gupta23d15e02008-10-29 15:10:35 +02002097 if (qh->type == USB_ENDPOINT_XFER_CONTROL) {
Felipe Balbi550a7372008-07-24 12:27:36 +03002098 head = &musb->control;
2099 hw_ep = musb->control_ep;
Felipe Balbi550a7372008-07-24 12:27:36 +03002100 goto success;
2101 }
2102
2103 /* else, periodic transfers get muxed to other endpoints */
2104
Sergei Shtylyov5d67a852009-02-24 15:23:34 -08002105 /*
2106 * We know this qh hasn't been scheduled, so all we need to do
Felipe Balbi550a7372008-07-24 12:27:36 +03002107 * is choose which hardware endpoint to put it on ...
2108 *
2109 * REVISIT what we really want here is a regular schedule tree
Sergei Shtylyov5d67a852009-02-24 15:23:34 -08002110 * like e.g. OHCI uses.
Felipe Balbi550a7372008-07-24 12:27:36 +03002111 */
2112 best_diff = 4096;
2113 best_end = -1;
2114
Sergei Shtylyov5d67a852009-02-24 15:23:34 -08002115 for (epnum = 1, hw_ep = musb->endpoints + 1;
2116 epnum < musb->nr_endpoints;
2117 epnum++, hw_ep++) {
Felipe Balbi550a7372008-07-24 12:27:36 +03002118 int diff;
2119
Sergei Shtylyov3e5c6dc2009-03-27 12:55:16 -07002120 if (musb_ep_get_qh(hw_ep, is_in) != NULL)
Felipe Balbi550a7372008-07-24 12:27:36 +03002121 continue;
Sergei Shtylyov5d67a852009-02-24 15:23:34 -08002122
Felipe Balbi550a7372008-07-24 12:27:36 +03002123 if (hw_ep == musb->bulk_ep)
2124 continue;
2125
2126 if (is_in)
Ajay Kumar Guptaa483d702009-04-03 16:16:17 -07002127 diff = hw_ep->max_packet_sz_rx;
Felipe Balbi550a7372008-07-24 12:27:36 +03002128 else
Ajay Kumar Guptaa483d702009-04-03 16:16:17 -07002129 diff = hw_ep->max_packet_sz_tx;
2130 diff -= (qh->maxpacket * qh->hb_mult);
Felipe Balbi550a7372008-07-24 12:27:36 +03002131
Ajay Kumar Gupta23d15e02008-10-29 15:10:35 +02002132 if (diff >= 0 && best_diff > diff) {
Swaminathan S5274dab2009-12-28 13:40:37 +02002133
2134 /*
2135 * Mentor controller has a bug in that if we schedule
2136 * a BULK Tx transfer on an endpoint that had earlier
2137 * handled ISOC then the BULK transfer has to start on
2138 * a zero toggle. If the BULK transfer starts on a 1
2139 * toggle then this transfer will fail as the mentor
2140 * controller starts the Bulk transfer on a 0 toggle
2141 * irrespective of the programming of the toggle bits
2142 * in the TXCSR register. Check for this condition
2143 * while allocating the EP for a Tx Bulk transfer. If
2144 * so skip this EP.
2145 */
2146 hw_ep = musb->endpoints + epnum;
2147 toggle = usb_gettoggle(urb->dev, qh->epnum, !is_in);
2148 txtype = (musb_readb(hw_ep->regs, MUSB_TXTYPE)
2149 >> 4) & 0x3;
2150 if (!is_in && (qh->type == USB_ENDPOINT_XFER_BULK) &&
2151 toggle && (txtype == USB_ENDPOINT_XFER_ISOC))
2152 continue;
2153
Felipe Balbi550a7372008-07-24 12:27:36 +03002154 best_diff = diff;
2155 best_end = epnum;
2156 }
2157 }
Ajay Kumar Gupta23d15e02008-10-29 15:10:35 +02002158 /* use bulk reserved ep1 if no other ep is free */
Felipe Balbiaa5cbbe2008-11-17 09:08:16 +02002159 if (best_end < 0 && qh->type == USB_ENDPOINT_XFER_BULK) {
Ajay Kumar Gupta23d15e02008-10-29 15:10:35 +02002160 hw_ep = musb->bulk_ep;
2161 if (is_in)
2162 head = &musb->in_bulk;
2163 else
2164 head = &musb->out_bulk;
Ajay Kumar Gupta1e0320f2009-02-24 15:26:13 -08002165
Ajay Kumar Guptaf2838622012-07-19 13:41:59 +05302166 /* Enable bulk RX/TX NAK timeout scheme when bulk requests are
Rahul Bedarkar5ae477b2014-01-02 19:27:47 +05302167 * multiplexed. This scheme does not work in high speed to full
Ajay Kumar Gupta1e0320f2009-02-24 15:26:13 -08002168 * speed scenario as NAK interrupts are not coming from a
2169 * full speed device connected to a high speed device.
2170 * NAK timeout interval is 8 (128 uframe or 16ms) for HS and
2171 * 4 (8 frame or 8ms) for FS device.
2172 */
Ajay Kumar Guptaf2838622012-07-19 13:41:59 +05302173 if (qh->dev)
Ajay Kumar Gupta1e0320f2009-02-24 15:26:13 -08002174 qh->intv_reg =
2175 (USB_SPEED_HIGH == qh->dev->speed) ? 8 : 4;
Ajay Kumar Gupta23d15e02008-10-29 15:10:35 +02002176 goto success;
2177 } else if (best_end < 0) {
Felipe Balbi550a7372008-07-24 12:27:36 +03002178 return -ENOSPC;
Ajay Kumar Gupta23d15e02008-10-29 15:10:35 +02002179 }
Felipe Balbi550a7372008-07-24 12:27:36 +03002180
2181 idle = 1;
Ajay Kumar Gupta23d15e02008-10-29 15:10:35 +02002182 qh->mux = 0;
Felipe Balbi550a7372008-07-24 12:27:36 +03002183 hw_ep = musb->endpoints + best_end;
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03002184 dev_dbg(musb->controller, "qh %p periodic slot %d\n", qh, best_end);
Felipe Balbi550a7372008-07-24 12:27:36 +03002185success:
Ajay Kumar Gupta23d15e02008-10-29 15:10:35 +02002186 if (head) {
2187 idle = list_empty(head);
2188 list_add_tail(&qh->ring, head);
2189 qh->mux = 1;
2190 }
Felipe Balbi550a7372008-07-24 12:27:36 +03002191 qh->hw_ep = hw_ep;
2192 qh->hep->hcpriv = qh;
2193 if (idle)
2194 musb_start_urb(musb, is_in, qh);
2195 return 0;
2196}
2197
2198static int musb_urb_enqueue(
2199 struct usb_hcd *hcd,
2200 struct urb *urb,
2201 gfp_t mem_flags)
2202{
2203 unsigned long flags;
2204 struct musb *musb = hcd_to_musb(hcd);
2205 struct usb_host_endpoint *hep = urb->ep;
David Brownell74bb3502009-03-26 17:36:57 -07002206 struct musb_qh *qh;
Felipe Balbi550a7372008-07-24 12:27:36 +03002207 struct usb_endpoint_descriptor *epd = &hep->desc;
2208 int ret;
2209 unsigned type_reg;
2210 unsigned interval;
2211
2212 /* host role must be active */
2213 if (!is_host_active(musb) || !musb->is_active)
2214 return -ENODEV;
2215
2216 spin_lock_irqsave(&musb->lock, flags);
2217 ret = usb_hcd_link_urb_to_ep(hcd, urb);
David Brownell74bb3502009-03-26 17:36:57 -07002218 qh = ret ? NULL : hep->hcpriv;
2219 if (qh)
2220 urb->hcpriv = qh;
Felipe Balbi550a7372008-07-24 12:27:36 +03002221 spin_unlock_irqrestore(&musb->lock, flags);
Felipe Balbi550a7372008-07-24 12:27:36 +03002222
2223 /* DMA mapping was already done, if needed, and this urb is on
David Brownell74bb3502009-03-26 17:36:57 -07002224 * hep->urb_list now ... so we're done, unless hep wasn't yet
2225 * scheduled onto a live qh.
Felipe Balbi550a7372008-07-24 12:27:36 +03002226 *
2227 * REVISIT best to keep hep->hcpriv valid until the endpoint gets
2228 * disabled, testing for empty qh->ring and avoiding qh setup costs
2229 * except for the first urb queued after a config change.
2230 */
David Brownell74bb3502009-03-26 17:36:57 -07002231 if (qh || ret)
2232 return ret;
Felipe Balbi550a7372008-07-24 12:27:36 +03002233
2234 /* Allocate and initialize qh, minimizing the work done each time
2235 * hw_ep gets reprogrammed, or with irqs blocked. Then schedule it.
2236 *
2237 * REVISIT consider a dedicated qh kmem_cache, so it's harder
2238 * for bugs in other kernel code to break this driver...
2239 */
2240 qh = kzalloc(sizeof *qh, mem_flags);
2241 if (!qh) {
Ajay Kumar Gupta2492e672008-09-11 11:53:21 +03002242 spin_lock_irqsave(&musb->lock, flags);
Felipe Balbi550a7372008-07-24 12:27:36 +03002243 usb_hcd_unlink_urb_from_ep(hcd, urb);
Ajay Kumar Gupta2492e672008-09-11 11:53:21 +03002244 spin_unlock_irqrestore(&musb->lock, flags);
Felipe Balbi550a7372008-07-24 12:27:36 +03002245 return -ENOMEM;
2246 }
2247
2248 qh->hep = hep;
2249 qh->dev = urb->dev;
2250 INIT_LIST_HEAD(&qh->ring);
2251 qh->is_ready = 1;
2252
Kuninori Morimoto29cc8892011-08-23 03:12:03 -07002253 qh->maxpacket = usb_endpoint_maxp(epd);
Ajay Kumar Guptaa483d702009-04-03 16:16:17 -07002254 qh->type = usb_endpoint_type(epd);
Felipe Balbi550a7372008-07-24 12:27:36 +03002255
Ajay Kumar Guptaa483d702009-04-03 16:16:17 -07002256 /* Bits 11 & 12 of wMaxPacketSize encode high bandwidth multiplier.
2257 * Some musb cores don't support high bandwidth ISO transfers; and
2258 * we don't (yet!) support high bandwidth interrupt transfers.
2259 */
2260 qh->hb_mult = 1 + ((qh->maxpacket >> 11) & 0x03);
2261 if (qh->hb_mult > 1) {
2262 int ok = (qh->type == USB_ENDPOINT_XFER_ISOC);
2263
2264 if (ok)
2265 ok = (usb_pipein(urb->pipe) && musb->hb_iso_rx)
2266 || (usb_pipeout(urb->pipe) && musb->hb_iso_tx);
2267 if (!ok) {
2268 ret = -EMSGSIZE;
2269 goto done;
2270 }
2271 qh->maxpacket &= 0x7ff;
Felipe Balbi550a7372008-07-24 12:27:36 +03002272 }
2273
Julia Lawall96bcd092009-01-24 17:57:24 -08002274 qh->epnum = usb_endpoint_num(epd);
Felipe Balbi550a7372008-07-24 12:27:36 +03002275
2276 /* NOTE: urb->dev->devnum is wrong during SET_ADDRESS */
2277 qh->addr_reg = (u8) usb_pipedevice(urb->pipe);
2278
2279 /* precompute rxtype/txtype/type0 register */
2280 type_reg = (qh->type << 4) | qh->epnum;
2281 switch (urb->dev->speed) {
2282 case USB_SPEED_LOW:
2283 type_reg |= 0xc0;
2284 break;
2285 case USB_SPEED_FULL:
2286 type_reg |= 0x80;
2287 break;
2288 default:
2289 type_reg |= 0x40;
2290 }
2291 qh->type_reg = type_reg;
2292
Sergei Shtylyov136733d2009-02-21 15:31:35 -08002293 /* Precompute RXINTERVAL/TXINTERVAL register */
Felipe Balbi550a7372008-07-24 12:27:36 +03002294 switch (qh->type) {
2295 case USB_ENDPOINT_XFER_INT:
Sergei Shtylyov136733d2009-02-21 15:31:35 -08002296 /*
2297 * Full/low speeds use the linear encoding,
2298 * high speed uses the logarithmic encoding.
2299 */
2300 if (urb->dev->speed <= USB_SPEED_FULL) {
2301 interval = max_t(u8, epd->bInterval, 1);
2302 break;
Felipe Balbi550a7372008-07-24 12:27:36 +03002303 }
2304 /* FALLTHROUGH */
2305 case USB_ENDPOINT_XFER_ISOC:
Sergei Shtylyov136733d2009-02-21 15:31:35 -08002306 /* ISO always uses logarithmic encoding */
2307 interval = min_t(u8, epd->bInterval, 16);
Felipe Balbi550a7372008-07-24 12:27:36 +03002308 break;
2309 default:
2310 /* REVISIT we actually want to use NAK limits, hinting to the
2311 * transfer scheduling logic to try some other qh, e.g. try
2312 * for 2 msec first:
2313 *
2314 * interval = (USB_SPEED_HIGH == urb->dev->speed) ? 16 : 2;
2315 *
2316 * The downside of disabling this is that transfer scheduling
2317 * gets VERY unfair for nonperiodic transfers; a misbehaving
Ajay Kumar Gupta1e0320f2009-02-24 15:26:13 -08002318 * peripheral could make that hurt. That's perfectly normal
2319 * for reads from network or serial adapters ... so we have
2320 * partial NAKlimit support for bulk RX.
Felipe Balbi550a7372008-07-24 12:27:36 +03002321 *
Ajay Kumar Gupta1e0320f2009-02-24 15:26:13 -08002322 * The upside of disabling it is simpler transfer scheduling.
Felipe Balbi550a7372008-07-24 12:27:36 +03002323 */
2324 interval = 0;
2325 }
2326 qh->intv_reg = interval;
2327
2328 /* precompute addressing for external hub/tt ports */
2329 if (musb->is_multipoint) {
2330 struct usb_device *parent = urb->dev->parent;
2331
2332 if (parent != hcd->self.root_hub) {
2333 qh->h_addr_reg = (u8) parent->devnum;
2334
2335 /* set up tt info if needed */
2336 if (urb->dev->tt) {
2337 qh->h_port_reg = (u8) urb->dev->ttport;
Ajay Kumar Guptaae5ad292008-09-11 11:53:20 +03002338 if (urb->dev->tt->hub)
2339 qh->h_addr_reg =
2340 (u8) urb->dev->tt->hub->devnum;
2341 if (urb->dev->tt->multi)
2342 qh->h_addr_reg |= 0x80;
Felipe Balbi550a7372008-07-24 12:27:36 +03002343 }
2344 }
2345 }
2346
2347 /* invariant: hep->hcpriv is null OR the qh that's already scheduled.
2348 * until we get real dma queues (with an entry for each urb/buffer),
2349 * we only have work to do in the former case.
2350 */
2351 spin_lock_irqsave(&musb->lock, flags);
yuzheng ma30677792012-08-15 16:11:40 +08002352 if (hep->hcpriv || !next_urb(qh)) {
Felipe Balbi550a7372008-07-24 12:27:36 +03002353 /* some concurrent activity submitted another urb to hep...
2354 * odd, rare, error prone, but legal.
2355 */
2356 kfree(qh);
Dan Carpenter714bc5e2010-03-25 13:14:27 +02002357 qh = NULL;
Felipe Balbi550a7372008-07-24 12:27:36 +03002358 ret = 0;
2359 } else
2360 ret = musb_schedule(musb, qh,
2361 epd->bEndpointAddress & USB_ENDPOINT_DIR_MASK);
2362
2363 if (ret == 0) {
2364 urb->hcpriv = qh;
2365 /* FIXME set urb->start_frame for iso/intr, it's tested in
2366 * musb_start_urb(), but otherwise only konicawc cares ...
2367 */
2368 }
2369 spin_unlock_irqrestore(&musb->lock, flags);
2370
2371done:
2372 if (ret != 0) {
Ajay Kumar Gupta2492e672008-09-11 11:53:21 +03002373 spin_lock_irqsave(&musb->lock, flags);
Felipe Balbi550a7372008-07-24 12:27:36 +03002374 usb_hcd_unlink_urb_from_ep(hcd, urb);
Ajay Kumar Gupta2492e672008-09-11 11:53:21 +03002375 spin_unlock_irqrestore(&musb->lock, flags);
Felipe Balbi550a7372008-07-24 12:27:36 +03002376 kfree(qh);
2377 }
2378 return ret;
2379}
2380
2381
2382/*
2383 * abort a transfer that's at the head of a hardware queue.
2384 * called with controller locked, irqs blocked
2385 * that hardware queue advances to the next transfer, unless prevented
2386 */
Sergei Shtylyov81ec4e42009-03-27 12:57:50 -07002387static int musb_cleanup_urb(struct urb *urb, struct musb_qh *qh)
Felipe Balbi550a7372008-07-24 12:27:36 +03002388{
2389 struct musb_hw_ep *ep = qh->hw_ep;
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03002390 struct musb *musb = ep->musb;
Felipe Balbi550a7372008-07-24 12:27:36 +03002391 void __iomem *epio = ep->regs;
2392 unsigned hw_end = ep->epnum;
2393 void __iomem *regs = ep->musb->mregs;
Sergei Shtylyov81ec4e42009-03-27 12:57:50 -07002394 int is_in = usb_pipein(urb->pipe);
Felipe Balbi550a7372008-07-24 12:27:36 +03002395 int status = 0;
Sergei Shtylyov81ec4e42009-03-27 12:57:50 -07002396 u16 csr;
Felipe Balbi550a7372008-07-24 12:27:36 +03002397
2398 musb_ep_select(regs, hw_end);
2399
2400 if (is_dma_capable()) {
2401 struct dma_channel *dma;
2402
2403 dma = is_in ? ep->rx_channel : ep->tx_channel;
2404 if (dma) {
2405 status = ep->musb->dma_controller->channel_abort(dma);
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03002406 dev_dbg(musb->controller,
Felipe Balbi550a7372008-07-24 12:27:36 +03002407 "abort %cX%d DMA for urb %p --> %d\n",
2408 is_in ? 'R' : 'T', ep->epnum,
2409 urb, status);
2410 urb->actual_length += dma->actual_len;
2411 }
2412 }
2413
2414 /* turn off DMA requests, discard state, stop polling ... */
Ajay Kumar Gupta692933b2012-03-14 17:33:35 +05302415 if (ep->epnum && is_in) {
Felipe Balbi550a7372008-07-24 12:27:36 +03002416 /* giveback saves bulk toggle */
2417 csr = musb_h_flush_rxfifo(ep, 0);
2418
2419 /* REVISIT we still get an irq; should likely clear the
2420 * endpoint's irq status here to avoid bogus irqs.
2421 * clearing that status is platform-specific...
2422 */
David Brownell78322c12009-03-26 17:38:30 -07002423 } else if (ep->epnum) {
Felipe Balbi550a7372008-07-24 12:27:36 +03002424 musb_h_tx_flush_fifo(ep);
2425 csr = musb_readw(epio, MUSB_TXCSR);
2426 csr &= ~(MUSB_TXCSR_AUTOSET
2427 | MUSB_TXCSR_DMAENAB
2428 | MUSB_TXCSR_H_RXSTALL
2429 | MUSB_TXCSR_H_NAKTIMEOUT
2430 | MUSB_TXCSR_H_ERROR
2431 | MUSB_TXCSR_TXPKTRDY);
2432 musb_writew(epio, MUSB_TXCSR, csr);
2433 /* REVISIT may need to clear FLUSHFIFO ... */
2434 musb_writew(epio, MUSB_TXCSR, csr);
2435 /* flush cpu writebuffer */
2436 csr = musb_readw(epio, MUSB_TXCSR);
David Brownell78322c12009-03-26 17:38:30 -07002437 } else {
2438 musb_h_ep0_flush_fifo(ep);
Felipe Balbi550a7372008-07-24 12:27:36 +03002439 }
2440 if (status == 0)
2441 musb_advance_schedule(ep->musb, urb, ep, is_in);
2442 return status;
2443}
2444
2445static int musb_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
2446{
2447 struct musb *musb = hcd_to_musb(hcd);
2448 struct musb_qh *qh;
Felipe Balbi550a7372008-07-24 12:27:36 +03002449 unsigned long flags;
Sergei Shtylyov22a0d6f2009-03-27 12:56:26 -07002450 int is_in = usb_pipein(urb->pipe);
Felipe Balbi550a7372008-07-24 12:27:36 +03002451 int ret;
2452
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03002453 dev_dbg(musb->controller, "urb=%p, dev%d ep%d%s\n", urb,
Felipe Balbi550a7372008-07-24 12:27:36 +03002454 usb_pipedevice(urb->pipe),
2455 usb_pipeendpoint(urb->pipe),
Sergei Shtylyov22a0d6f2009-03-27 12:56:26 -07002456 is_in ? "in" : "out");
Felipe Balbi550a7372008-07-24 12:27:36 +03002457
2458 spin_lock_irqsave(&musb->lock, flags);
2459 ret = usb_hcd_check_unlink_urb(hcd, urb, status);
2460 if (ret)
2461 goto done;
2462
2463 qh = urb->hcpriv;
2464 if (!qh)
2465 goto done;
2466
Sergei Shtylyov22a0d6f2009-03-27 12:56:26 -07002467 /*
2468 * Any URB not actively programmed into endpoint hardware can be
Sergei Shtylyova2fd8142009-02-21 15:30:45 -08002469 * immediately given back; that's any URB not at the head of an
Felipe Balbi550a7372008-07-24 12:27:36 +03002470 * endpoint queue, unless someday we get real DMA queues. And even
Sergei Shtylyova2fd8142009-02-21 15:30:45 -08002471 * if it's at the head, it might not be known to the hardware...
Felipe Balbi550a7372008-07-24 12:27:36 +03002472 *
Sergei Shtylyov22a0d6f2009-03-27 12:56:26 -07002473 * Otherwise abort current transfer, pending DMA, etc.; urb->status
Felipe Balbi550a7372008-07-24 12:27:36 +03002474 * has already been updated. This is a synchronous abort; it'd be
2475 * OK to hold off until after some IRQ, though.
Sergei Shtylyov22a0d6f2009-03-27 12:56:26 -07002476 *
2477 * NOTE: qh is invalid unless !list_empty(&hep->urb_list)
Felipe Balbi550a7372008-07-24 12:27:36 +03002478 */
Sergei Shtylyov22a0d6f2009-03-27 12:56:26 -07002479 if (!qh->is_ready
2480 || urb->urb_list.prev != &qh->hep->urb_list
2481 || musb_ep_get_qh(qh->hw_ep, is_in) != qh) {
Felipe Balbi550a7372008-07-24 12:27:36 +03002482 int ready = qh->is_ready;
2483
Felipe Balbi550a7372008-07-24 12:27:36 +03002484 qh->is_ready = 0;
Sergei Shtylyovc9cd06b2009-03-27 12:58:31 -07002485 musb_giveback(musb, urb, 0);
Felipe Balbi550a7372008-07-24 12:27:36 +03002486 qh->is_ready = ready;
Sergei Shtylyova2fd8142009-02-21 15:30:45 -08002487
2488 /* If nothing else (usually musb_giveback) is using it
2489 * and its URB list has emptied, recycle this qh.
2490 */
2491 if (ready && list_empty(&qh->hep->urb_list)) {
2492 qh->hep->hcpriv = NULL;
2493 list_del(&qh->ring);
2494 kfree(qh);
2495 }
Felipe Balbi550a7372008-07-24 12:27:36 +03002496 } else
Sergei Shtylyov81ec4e42009-03-27 12:57:50 -07002497 ret = musb_cleanup_urb(urb, qh);
Felipe Balbi550a7372008-07-24 12:27:36 +03002498done:
2499 spin_unlock_irqrestore(&musb->lock, flags);
2500 return ret;
2501}
2502
2503/* disable an endpoint */
2504static void
2505musb_h_disable(struct usb_hcd *hcd, struct usb_host_endpoint *hep)
2506{
Sergei Shtylyov22a0d6f2009-03-27 12:56:26 -07002507 u8 is_in = hep->desc.bEndpointAddress & USB_DIR_IN;
Felipe Balbi550a7372008-07-24 12:27:36 +03002508 unsigned long flags;
2509 struct musb *musb = hcd_to_musb(hcd);
Sergei Shtylyovdc61d232009-02-21 15:31:01 -08002510 struct musb_qh *qh;
2511 struct urb *urb;
Felipe Balbi550a7372008-07-24 12:27:36 +03002512
Felipe Balbi550a7372008-07-24 12:27:36 +03002513 spin_lock_irqsave(&musb->lock, flags);
2514
Sergei Shtylyovdc61d232009-02-21 15:31:01 -08002515 qh = hep->hcpriv;
2516 if (qh == NULL)
2517 goto exit;
2518
Sergei Shtylyov22a0d6f2009-03-27 12:56:26 -07002519 /* NOTE: qh is invalid unless !list_empty(&hep->urb_list) */
Felipe Balbi550a7372008-07-24 12:27:36 +03002520
Sergei Shtylyov22a0d6f2009-03-27 12:56:26 -07002521 /* Kick the first URB off the hardware, if needed */
Felipe Balbi550a7372008-07-24 12:27:36 +03002522 qh->is_ready = 0;
Sergei Shtylyov22a0d6f2009-03-27 12:56:26 -07002523 if (musb_ep_get_qh(qh->hw_ep, is_in) == qh) {
Felipe Balbi550a7372008-07-24 12:27:36 +03002524 urb = next_urb(qh);
2525
2526 /* make software (then hardware) stop ASAP */
2527 if (!urb->unlinked)
2528 urb->status = -ESHUTDOWN;
2529
2530 /* cleanup */
Sergei Shtylyov81ec4e42009-03-27 12:57:50 -07002531 musb_cleanup_urb(urb, qh);
Felipe Balbi550a7372008-07-24 12:27:36 +03002532
Sergei Shtylyovdc61d232009-02-21 15:31:01 -08002533 /* Then nuke all the others ... and advance the
2534 * queue on hw_ep (e.g. bulk ring) when we're done.
2535 */
2536 while (!list_empty(&hep->urb_list)) {
2537 urb = next_urb(qh);
2538 urb->status = -ESHUTDOWN;
2539 musb_advance_schedule(musb, urb, qh->hw_ep, is_in);
2540 }
2541 } else {
2542 /* Just empty the queue; the hardware is busy with
2543 * other transfers, and since !qh->is_ready nothing
2544 * will activate any of these as it advances.
2545 */
2546 while (!list_empty(&hep->urb_list))
Sergei Shtylyovc9cd06b2009-03-27 12:58:31 -07002547 musb_giveback(musb, next_urb(qh), -ESHUTDOWN);
Felipe Balbi550a7372008-07-24 12:27:36 +03002548
Sergei Shtylyovdc61d232009-02-21 15:31:01 -08002549 hep->hcpriv = NULL;
2550 list_del(&qh->ring);
2551 kfree(qh);
2552 }
2553exit:
Felipe Balbi550a7372008-07-24 12:27:36 +03002554 spin_unlock_irqrestore(&musb->lock, flags);
2555}
2556
2557static int musb_h_get_frame_number(struct usb_hcd *hcd)
2558{
2559 struct musb *musb = hcd_to_musb(hcd);
2560
2561 return musb_readw(musb->mregs, MUSB_FRAME);
2562}
2563
2564static int musb_h_start(struct usb_hcd *hcd)
2565{
2566 struct musb *musb = hcd_to_musb(hcd);
2567
2568 /* NOTE: musb_start() is called when the hub driver turns
2569 * on port power, or when (OTG) peripheral starts.
2570 */
2571 hcd->state = HC_STATE_RUNNING;
2572 musb->port1_status = 0;
2573 return 0;
2574}
2575
2576static void musb_h_stop(struct usb_hcd *hcd)
2577{
2578 musb_stop(hcd_to_musb(hcd));
2579 hcd->state = HC_STATE_HALT;
2580}
2581
2582static int musb_bus_suspend(struct usb_hcd *hcd)
2583{
2584 struct musb *musb = hcd_to_musb(hcd);
David Brownell89368d32009-07-01 03:36:16 -07002585 u8 devctl;
Felipe Balbi550a7372008-07-24 12:27:36 +03002586
Daniel Mack94f72132013-11-25 22:26:41 +01002587 musb_port_suspend(musb, true);
2588
David Brownell89368d32009-07-01 03:36:16 -07002589 if (!is_host_active(musb))
Felipe Balbi550a7372008-07-24 12:27:36 +03002590 return 0;
2591
Antoine Tenarte47d9252014-10-30 18:41:13 +01002592 switch (musb->xceiv->otg->state) {
David Brownell89368d32009-07-01 03:36:16 -07002593 case OTG_STATE_A_SUSPEND:
2594 return 0;
2595 case OTG_STATE_A_WAIT_VRISE:
2596 /* ID could be grounded even if there's no device
2597 * on the other end of the cable. NOTE that the
2598 * A_WAIT_VRISE timers are messy with MUSB...
2599 */
2600 devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
2601 if ((devctl & MUSB_DEVCTL_VBUS) == MUSB_DEVCTL_VBUS)
Antoine Tenarte47d9252014-10-30 18:41:13 +01002602 musb->xceiv->otg->state = OTG_STATE_A_WAIT_BCON;
David Brownell89368d32009-07-01 03:36:16 -07002603 break;
2604 default:
2605 break;
2606 }
2607
2608 if (musb->is_active) {
2609 WARNING("trying to suspend as %s while active\n",
Antoine Tenarte47d9252014-10-30 18:41:13 +01002610 usb_otg_state_string(musb->xceiv->otg->state));
Felipe Balbi550a7372008-07-24 12:27:36 +03002611 return -EBUSY;
2612 } else
2613 return 0;
2614}
2615
2616static int musb_bus_resume(struct usb_hcd *hcd)
2617{
Daniel Mack869c5972013-11-26 13:31:14 +01002618 struct musb *musb = hcd_to_musb(hcd);
2619
2620 if (musb->config &&
2621 musb->config->host_port_deassert_reset_at_resume)
2622 musb_port_reset(musb, false);
2623
Felipe Balbi550a7372008-07-24 12:27:36 +03002624 return 0;
2625}
2626
Ruslan Bilovol8408fd12013-03-29 19:15:21 +02002627#ifndef CONFIG_MUSB_PIO_ONLY
2628
2629#define MUSB_USB_DMA_ALIGN 4
2630
2631struct musb_temp_buffer {
2632 void *kmalloc_ptr;
2633 void *old_xfer_buffer;
2634 u8 data[0];
2635};
2636
2637static void musb_free_temp_buffer(struct urb *urb)
2638{
2639 enum dma_data_direction dir;
2640 struct musb_temp_buffer *temp;
Johan Hovoldd72348f2015-04-23 16:06:50 +02002641 size_t length;
Ruslan Bilovol8408fd12013-03-29 19:15:21 +02002642
2643 if (!(urb->transfer_flags & URB_ALIGNED_TEMP_BUFFER))
2644 return;
2645
2646 dir = usb_urb_dir_in(urb) ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
2647
2648 temp = container_of(urb->transfer_buffer, struct musb_temp_buffer,
2649 data);
2650
2651 if (dir == DMA_FROM_DEVICE) {
Johan Hovoldd72348f2015-04-23 16:06:50 +02002652 if (usb_pipeisoc(urb->pipe))
2653 length = urb->transfer_buffer_length;
2654 else
2655 length = urb->actual_length;
2656
2657 memcpy(temp->old_xfer_buffer, temp->data, length);
Ruslan Bilovol8408fd12013-03-29 19:15:21 +02002658 }
2659 urb->transfer_buffer = temp->old_xfer_buffer;
2660 kfree(temp->kmalloc_ptr);
2661
2662 urb->transfer_flags &= ~URB_ALIGNED_TEMP_BUFFER;
2663}
2664
2665static int musb_alloc_temp_buffer(struct urb *urb, gfp_t mem_flags)
2666{
2667 enum dma_data_direction dir;
2668 struct musb_temp_buffer *temp;
2669 void *kmalloc_ptr;
2670 size_t kmalloc_size;
2671
2672 if (urb->num_sgs || urb->sg ||
2673 urb->transfer_buffer_length == 0 ||
2674 !((uintptr_t)urb->transfer_buffer & (MUSB_USB_DMA_ALIGN - 1)))
2675 return 0;
2676
2677 dir = usb_urb_dir_in(urb) ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
2678
2679 /* Allocate a buffer with enough padding for alignment */
2680 kmalloc_size = urb->transfer_buffer_length +
2681 sizeof(struct musb_temp_buffer) + MUSB_USB_DMA_ALIGN - 1;
2682
2683 kmalloc_ptr = kmalloc(kmalloc_size, mem_flags);
2684 if (!kmalloc_ptr)
2685 return -ENOMEM;
2686
2687 /* Position our struct temp_buffer such that data is aligned */
2688 temp = PTR_ALIGN(kmalloc_ptr, MUSB_USB_DMA_ALIGN);
2689
2690
2691 temp->kmalloc_ptr = kmalloc_ptr;
2692 temp->old_xfer_buffer = urb->transfer_buffer;
2693 if (dir == DMA_TO_DEVICE)
2694 memcpy(temp->data, urb->transfer_buffer,
2695 urb->transfer_buffer_length);
2696 urb->transfer_buffer = temp->data;
2697
2698 urb->transfer_flags |= URB_ALIGNED_TEMP_BUFFER;
2699
2700 return 0;
2701}
2702
2703static int musb_map_urb_for_dma(struct usb_hcd *hcd, struct urb *urb,
2704 gfp_t mem_flags)
2705{
2706 struct musb *musb = hcd_to_musb(hcd);
2707 int ret;
2708
2709 /*
2710 * The DMA engine in RTL1.8 and above cannot handle
2711 * DMA addresses that are not aligned to a 4 byte boundary.
2712 * For such engine implemented (un)map_urb_for_dma hooks.
2713 * Do not use these hooks for RTL<1.8
2714 */
2715 if (musb->hwvers < MUSB_HWVERS_1800)
2716 return usb_hcd_map_urb_for_dma(hcd, urb, mem_flags);
2717
2718 ret = musb_alloc_temp_buffer(urb, mem_flags);
2719 if (ret)
2720 return ret;
2721
2722 ret = usb_hcd_map_urb_for_dma(hcd, urb, mem_flags);
2723 if (ret)
2724 musb_free_temp_buffer(urb);
2725
2726 return ret;
2727}
2728
2729static void musb_unmap_urb_for_dma(struct usb_hcd *hcd, struct urb *urb)
2730{
2731 struct musb *musb = hcd_to_musb(hcd);
2732
2733 usb_hcd_unmap_urb_for_dma(hcd, urb);
2734
2735 /* Do not use this hook for RTL<1.8 (see description above) */
2736 if (musb->hwvers < MUSB_HWVERS_1800)
2737 return;
2738
2739 musb_free_temp_buffer(urb);
2740}
2741#endif /* !CONFIG_MUSB_PIO_ONLY */
2742
Daniel Mack74c2e932013-04-10 21:55:45 +02002743static const struct hc_driver musb_hc_driver = {
Felipe Balbi550a7372008-07-24 12:27:36 +03002744 .description = "musb-hcd",
2745 .product_desc = "MUSB HDRC host driver",
Daniel Mack74c2e932013-04-10 21:55:45 +02002746 .hcd_priv_size = sizeof(struct musb *),
Bin Liuf551e132016-04-25 15:53:30 -05002747 .flags = HCD_USB2 | HCD_MEMORY,
Felipe Balbi550a7372008-07-24 12:27:36 +03002748
2749 /* not using irq handler or reset hooks from usbcore, since
2750 * those must be shared with peripheral code for OTG configs
2751 */
2752
2753 .start = musb_h_start,
2754 .stop = musb_h_stop,
2755
2756 .get_frame_number = musb_h_get_frame_number,
2757
2758 .urb_enqueue = musb_urb_enqueue,
2759 .urb_dequeue = musb_urb_dequeue,
2760 .endpoint_disable = musb_h_disable,
2761
Ruslan Bilovol8408fd12013-03-29 19:15:21 +02002762#ifndef CONFIG_MUSB_PIO_ONLY
2763 .map_urb_for_dma = musb_map_urb_for_dma,
2764 .unmap_urb_for_dma = musb_unmap_urb_for_dma,
2765#endif
2766
Felipe Balbi550a7372008-07-24 12:27:36 +03002767 .hub_status_data = musb_hub_status_data,
2768 .hub_control = musb_hub_control,
2769 .bus_suspend = musb_bus_suspend,
2770 .bus_resume = musb_bus_resume,
2771 /* .start_port_reset = NULL, */
2772 /* .hub_irq_enable = NULL, */
2773};
Daniel Mack0b3eba42013-04-10 21:55:42 +02002774
Daniel Mack74c2e932013-04-10 21:55:45 +02002775int musb_host_alloc(struct musb *musb)
2776{
2777 struct device *dev = musb->controller;
2778
2779 /* usbcore sets dev->driver_data to hcd, and sometimes uses that... */
2780 musb->hcd = usb_create_hcd(&musb_hc_driver, dev, dev_name(dev));
2781 if (!musb->hcd)
2782 return -EINVAL;
2783
2784 *musb->hcd->hcd_priv = (unsigned long) musb;
2785 musb->hcd->self.uses_pio_for_control = 1;
2786 musb->hcd->uses_new_polling = 1;
2787 musb->hcd->has_tt = 1;
2788
2789 return 0;
2790}
2791
2792void musb_host_cleanup(struct musb *musb)
2793{
Sebastian Andrzej Siewior90474282013-08-20 18:35:44 +02002794 if (musb->port_mode == MUSB_PORT_MODE_GADGET)
2795 return;
Daniel Mack74c2e932013-04-10 21:55:45 +02002796 usb_remove_hcd(musb->hcd);
Daniel Mack74c2e932013-04-10 21:55:45 +02002797}
2798
2799void musb_host_free(struct musb *musb)
2800{
2801 usb_put_hcd(musb->hcd);
2802}
2803
Daniel Mack2cc65fe2013-04-10 21:55:47 +02002804int musb_host_setup(struct musb *musb, int power_budget)
2805{
2806 int ret;
2807 struct usb_hcd *hcd = musb->hcd;
2808
2809 MUSB_HST_MODE(musb);
2810 musb->xceiv->otg->default_a = 1;
Antoine Tenarte47d9252014-10-30 18:41:13 +01002811 musb->xceiv->otg->state = OTG_STATE_A_IDLE;
Daniel Mack2cc65fe2013-04-10 21:55:47 +02002812
2813 otg_set_host(musb->xceiv->otg, &hcd->self);
2814 hcd->self.otg_port = 1;
2815 musb->xceiv->otg->host = &hcd->self;
2816 hcd->power_budget = 2 * (power_budget ? : 250);
2817
2818 ret = usb_add_hcd(hcd, 0, 0);
2819 if (ret < 0)
2820 return ret;
2821
Peter Chen3c9740a2013-11-05 10:46:02 +08002822 device_wakeup_enable(hcd->self.controller);
Daniel Mack2cc65fe2013-04-10 21:55:47 +02002823 return 0;
2824}
2825
Daniel Mack0b3eba42013-04-10 21:55:42 +02002826void musb_host_resume_root_hub(struct musb *musb)
2827{
Daniel Mack74c2e932013-04-10 21:55:45 +02002828 usb_hcd_resume_root_hub(musb->hcd);
Daniel Mack0b3eba42013-04-10 21:55:42 +02002829}
2830
2831void musb_host_poke_root_hub(struct musb *musb)
2832{
2833 MUSB_HST_MODE(musb);
Daniel Mack74c2e932013-04-10 21:55:45 +02002834 if (musb->hcd->status_urb)
2835 usb_hcd_poll_rh_status(musb->hcd);
Daniel Mack0b3eba42013-04-10 21:55:42 +02002836 else
Daniel Mack74c2e932013-04-10 21:55:45 +02002837 usb_hcd_resume_root_hub(musb->hcd);
Daniel Mack0b3eba42013-04-10 21:55:42 +02002838}