blob: 45a489e3aeaf3105e95b2ee17a6710894fd675e8 [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
Sergei Shtylyov6b6e9712009-03-26 18:29:19 -0700687 channel->actual_len = 0;
688
689 /*
690 * TX uses "RNDIS" mode automatically but needs help
691 * to identify the zero-length-final-packet case.
692 */
Tony Lindgren754fe4a2015-05-01 12:29:32 -0700693 *mode = (urb->transfer_flags & URB_ZERO_PACKET) ? 1 : 0;
694
695 return 0;
696}
697
698static bool musb_tx_dma_program(struct dma_controller *dma,
699 struct musb_hw_ep *hw_ep, struct musb_qh *qh,
700 struct urb *urb, u32 offset, u32 length)
701{
702 struct dma_channel *channel = hw_ep->tx_channel;
703 u16 pkt_size = qh->maxpacket;
704 u8 mode;
705 int res;
706
707 if (musb_dma_inventra(hw_ep->musb) || musb_dma_ux500(hw_ep->musb))
708 res = musb_tx_dma_set_mode_mentor(dma, hw_ep, qh, urb,
709 offset, &length, &mode);
Sergei Shtylyov858b9be2016-05-31 10:05:05 -0500710 else if (is_cppi_enabled(hw_ep->musb) || tusb_dma_omap(hw_ep->musb))
Tony Lindgren754fe4a2015-05-01 12:29:32 -0700711 res = musb_tx_dma_set_mode_cppi_tusb(dma, hw_ep, qh, urb,
712 offset, &length, &mode);
Sergei Shtylyov858b9be2016-05-31 10:05:05 -0500713 else
714 return false;
Tony Lindgren754fe4a2015-05-01 12:29:32 -0700715 if (res)
716 return false;
Sergei Shtylyov6b6e9712009-03-26 18:29:19 -0700717
718 qh->segsize = length;
719
Santosh Shilimkar4c647332010-09-20 10:32:07 +0300720 /*
721 * Ensure the data reaches to main memory before starting
722 * DMA transfer
723 */
724 wmb();
725
Sergei Shtylyov6b6e9712009-03-26 18:29:19 -0700726 if (!dma->channel_program(channel, pkt_size, mode,
727 urb->transfer_dma + offset, length)) {
Tony Lindgren754fe4a2015-05-01 12:29:32 -0700728 void __iomem *epio = hw_ep->regs;
729 u16 csr;
730
Sergei Shtylyov6b6e9712009-03-26 18:29:19 -0700731 dma->channel_release(channel);
732 hw_ep->tx_channel = NULL;
733
734 csr = musb_readw(epio, MUSB_TXCSR);
735 csr &= ~(MUSB_TXCSR_AUTOSET | MUSB_TXCSR_DMAENAB);
736 musb_writew(epio, MUSB_TXCSR, csr | MUSB_TXCSR_H_WZC_BITS);
737 return false;
738 }
739 return true;
740}
Felipe Balbi550a7372008-07-24 12:27:36 +0300741
742/*
743 * Program an HDRC endpoint as per the given URB
744 * Context: irqs blocked, controller lock held
745 */
746static void musb_ep_program(struct musb *musb, u8 epnum,
Sergei Shtylyov6b6e9712009-03-26 18:29:19 -0700747 struct urb *urb, int is_out,
748 u8 *buf, u32 offset, u32 len)
Felipe Balbi550a7372008-07-24 12:27:36 +0300749{
750 struct dma_controller *dma_controller;
751 struct dma_channel *dma_channel;
752 u8 dma_ok;
753 void __iomem *mbase = musb->mregs;
754 struct musb_hw_ep *hw_ep = musb->endpoints + epnum;
755 void __iomem *epio = hw_ep->regs;
Sergei Shtylyov3e5c6dc2009-03-27 12:55:16 -0700756 struct musb_qh *qh = musb_ep_get_qh(hw_ep, !is_out);
757 u16 packet_sz = qh->maxpacket;
Ajay Kumar Gupta31321222012-07-20 11:07:22 +0530758 u8 use_dma = 1;
759 u16 csr;
Felipe Balbi550a7372008-07-24 12:27:36 +0300760
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300761 dev_dbg(musb->controller, "%s hw%d urb %p spd%d dev%d ep%d%s "
Felipe Balbi550a7372008-07-24 12:27:36 +0300762 "h_addr%02x h_port%02x bytes %d\n",
763 is_out ? "-->" : "<--",
764 epnum, urb, urb->dev->speed,
765 qh->addr_reg, qh->epnum, is_out ? "out" : "in",
766 qh->h_addr_reg, qh->h_port_reg,
767 len);
768
769 musb_ep_select(mbase, epnum);
770
Ajay Kumar Gupta31321222012-07-20 11:07:22 +0530771 if (is_out && !len) {
772 use_dma = 0;
773 csr = musb_readw(epio, MUSB_TXCSR);
774 csr &= ~MUSB_TXCSR_DMAENAB;
775 musb_writew(epio, MUSB_TXCSR, csr);
776 hw_ep->tx_channel = NULL;
777 }
778
Felipe Balbi550a7372008-07-24 12:27:36 +0300779 /* candidate for DMA? */
780 dma_controller = musb->dma_controller;
Ajay Kumar Gupta31321222012-07-20 11:07:22 +0530781 if (use_dma && is_dma_capable() && epnum && dma_controller) {
Felipe Balbi550a7372008-07-24 12:27:36 +0300782 dma_channel = is_out ? hw_ep->tx_channel : hw_ep->rx_channel;
783 if (!dma_channel) {
784 dma_channel = dma_controller->channel_alloc(
785 dma_controller, hw_ep, is_out);
786 if (is_out)
787 hw_ep->tx_channel = dma_channel;
788 else
789 hw_ep->rx_channel = dma_channel;
790 }
791 } else
792 dma_channel = NULL;
793
794 /* make sure we clear DMAEnab, autoSet bits from previous run */
795
796 /* OUT/transmit/EP0 or IN/receive? */
797 if (is_out) {
798 u16 csr;
799 u16 int_txe;
800 u16 load_count;
801
802 csr = musb_readw(epio, MUSB_TXCSR);
803
804 /* disable interrupt in case we flush */
Sebastian Andrzej Siewiorb18d26f2012-10-30 19:52:26 +0100805 int_txe = musb->intrtxe;
Felipe Balbi550a7372008-07-24 12:27:36 +0300806 musb_writew(mbase, MUSB_INTRTXE, int_txe & ~(1 << epnum));
807
808 /* general endpoint setup */
809 if (epnum) {
Felipe Balbi550a7372008-07-24 12:27:36 +0300810 /* flush all old state, set default */
supriya karantha70b8442013-01-04 17:10:33 +0530811 /*
812 * We could be flushing valid
813 * packets in double buffering
814 * case
815 */
816 if (!hw_ep->tx_double_buffered)
817 musb_h_tx_flush_fifo(hw_ep);
Sergei Shtylyovb6e434a2009-03-26 18:27:47 -0700818
819 /*
820 * We must not clear the DMAMODE bit before or in
821 * the same cycle with the DMAENAB bit, so we clear
822 * the latter first...
823 */
Felipe Balbi550a7372008-07-24 12:27:36 +0300824 csr &= ~(MUSB_TXCSR_H_NAKTIMEOUT
Sergei Shtylyovb6e434a2009-03-26 18:27:47 -0700825 | MUSB_TXCSR_AUTOSET
826 | MUSB_TXCSR_DMAENAB
Felipe Balbi550a7372008-07-24 12:27:36 +0300827 | MUSB_TXCSR_FRCDATATOG
828 | MUSB_TXCSR_H_RXSTALL
829 | MUSB_TXCSR_H_ERROR
830 | MUSB_TXCSR_TXPKTRDY
831 );
832 csr |= MUSB_TXCSR_MODE;
833
supriya karantha70b8442013-01-04 17:10:33 +0530834 if (!hw_ep->tx_double_buffered) {
835 if (usb_gettoggle(urb->dev, qh->epnum, 1))
836 csr |= MUSB_TXCSR_H_WR_DATATOGGLE
837 | MUSB_TXCSR_H_DATATOGGLE;
838 else
839 csr |= MUSB_TXCSR_CLRDATATOG;
840 }
Felipe Balbi550a7372008-07-24 12:27:36 +0300841
Felipe Balbi550a7372008-07-24 12:27:36 +0300842 musb_writew(epio, MUSB_TXCSR, csr);
843 /* REVISIT may need to clear FLUSHFIFO ... */
Sergei Shtylyovb6e434a2009-03-26 18:27:47 -0700844 csr &= ~MUSB_TXCSR_DMAMODE;
Felipe Balbi550a7372008-07-24 12:27:36 +0300845 musb_writew(epio, MUSB_TXCSR, csr);
846 csr = musb_readw(epio, MUSB_TXCSR);
847 } else {
848 /* endpoint 0: just flush */
David Brownell78322c12009-03-26 17:38:30 -0700849 musb_h_ep0_flush_fifo(hw_ep);
Felipe Balbi550a7372008-07-24 12:27:36 +0300850 }
851
852 /* target addr and (for multipoint) hub addr/port */
853 if (musb->is_multipoint) {
Hans de Goede6cc2af62015-03-20 20:11:12 +0100854 musb_write_txfunaddr(musb, epnum, qh->addr_reg);
855 musb_write_txhubaddr(musb, epnum, qh->h_addr_reg);
856 musb_write_txhubport(musb, epnum, qh->h_port_reg);
Felipe Balbi550a7372008-07-24 12:27:36 +0300857/* FIXME if !epnum, do the same for RX ... */
858 } else
859 musb_writeb(mbase, MUSB_FADDR, qh->addr_reg);
860
861 /* protocol/endpoint/interval/NAKlimit */
862 if (epnum) {
863 musb_writeb(epio, MUSB_TXTYPE, qh->type_reg);
supriya karanthf2786282012-12-06 11:16:23 +0530864 if (musb->double_buffer_not_ok) {
Felipe Balbi550a7372008-07-24 12:27:36 +0300865 musb_writew(epio, MUSB_TXMAXP,
Felipe Balbi06624812011-01-21 13:39:20 +0800866 hw_ep->max_packet_sz_tx);
supriya karanthf2786282012-12-06 11:16:23 +0530867 } else if (can_bulk_split(musb, qh->type)) {
868 qh->hb_mult = hw_ep->max_packet_sz_tx
869 / packet_sz;
Ajay Kumar Guptaccc080c2011-12-13 10:32:42 +0530870 musb_writew(epio, MUSB_TXMAXP, packet_sz
supriya karanthf2786282012-12-06 11:16:23 +0530871 | ((qh->hb_mult) - 1) << 11);
872 } else {
Felipe Balbi550a7372008-07-24 12:27:36 +0300873 musb_writew(epio, MUSB_TXMAXP,
Felipe Balbi06624812011-01-21 13:39:20 +0800874 qh->maxpacket |
875 ((qh->hb_mult - 1) << 11));
supriya karanthf2786282012-12-06 11:16:23 +0530876 }
Felipe Balbi550a7372008-07-24 12:27:36 +0300877 musb_writeb(epio, MUSB_TXINTERVAL, qh->intv_reg);
878 } else {
879 musb_writeb(epio, MUSB_NAKLIMIT0, qh->intv_reg);
880 if (musb->is_multipoint)
881 musb_writeb(epio, MUSB_TYPE0,
882 qh->type_reg);
883 }
884
885 if (can_bulk_split(musb, qh->type))
886 load_count = min((u32) hw_ep->max_packet_sz_tx,
887 len);
888 else
889 load_count = min((u32) packet_sz, len);
890
Sergei Shtylyov6b6e9712009-03-26 18:29:19 -0700891 if (dma_channel && musb_tx_dma_program(dma_controller,
892 hw_ep, qh, urb, offset, len))
893 load_count = 0;
Felipe Balbi550a7372008-07-24 12:27:36 +0300894
895 if (load_count) {
Felipe Balbi550a7372008-07-24 12:27:36 +0300896 /* PIO to load FIFO */
897 qh->segsize = load_count;
Virupax Sadashivpetimath8e8a5512012-08-07 14:46:20 +0530898 if (!buf) {
899 sg_miter_start(&qh->sg_miter, urb->sg, 1,
900 SG_MITER_ATOMIC
901 | SG_MITER_FROM_SG);
902 if (!sg_miter_next(&qh->sg_miter)) {
903 dev_err(musb->controller,
904 "error: sg"
905 "list empty\n");
906 sg_miter_stop(&qh->sg_miter);
907 goto finish;
908 }
909 buf = qh->sg_miter.addr + urb->sg->offset +
910 urb->actual_length;
911 load_count = min_t(u32, load_count,
912 qh->sg_miter.length);
913 musb_write_fifo(hw_ep, load_count, buf);
914 qh->sg_miter.consumed = load_count;
915 sg_miter_stop(&qh->sg_miter);
916 } else
917 musb_write_fifo(hw_ep, load_count, buf);
Felipe Balbi550a7372008-07-24 12:27:36 +0300918 }
Virupax Sadashivpetimath8e8a5512012-08-07 14:46:20 +0530919finish:
Felipe Balbi550a7372008-07-24 12:27:36 +0300920 /* re-enable interrupt */
921 musb_writew(mbase, MUSB_INTRTXE, int_txe);
922
923 /* IN/receive */
924 } else {
925 u16 csr;
926
927 if (hw_ep->rx_reinit) {
Hans de Goede0cb74b32015-03-20 20:11:11 +0100928 musb_rx_reinit(musb, qh, epnum);
Felipe Balbi550a7372008-07-24 12:27:36 +0300929
930 /* init new state: toggle and NYET, maybe DMA later */
931 if (usb_gettoggle(urb->dev, qh->epnum, 0))
932 csr = MUSB_RXCSR_H_WR_DATATOGGLE
933 | MUSB_RXCSR_H_DATATOGGLE;
934 else
935 csr = 0;
936 if (qh->type == USB_ENDPOINT_XFER_INT)
937 csr |= MUSB_RXCSR_DISNYET;
938
939 } else {
940 csr = musb_readw(hw_ep->regs, MUSB_RXCSR);
941
942 if (csr & (MUSB_RXCSR_RXPKTRDY
943 | MUSB_RXCSR_DMAENAB
944 | MUSB_RXCSR_H_REQPKT))
945 ERR("broken !rx_reinit, ep%d csr %04x\n",
946 hw_ep->epnum, csr);
947
948 /* scrub any stale state, leaving toggle alone */
949 csr &= MUSB_RXCSR_DISNYET;
950 }
951
952 /* kick things off */
953
Tony Lindgrenf8e9f34f2015-05-01 12:29:27 -0700954 if ((is_cppi_enabled(musb) || tusb_dma_omap(musb)) && dma_channel) {
Sergei Shtylyovc51e36d2011-05-07 19:44:13 +0400955 /* Candidate for DMA */
956 dma_channel->actual_len = 0L;
957 qh->segsize = len;
Felipe Balbi550a7372008-07-24 12:27:36 +0300958
Sergei Shtylyovc51e36d2011-05-07 19:44:13 +0400959 /* AUTOREQ is in a DMA register */
960 musb_writew(hw_ep->regs, MUSB_RXCSR, csr);
961 csr = musb_readw(hw_ep->regs, MUSB_RXCSR);
Felipe Balbi550a7372008-07-24 12:27:36 +0300962
Sergei Shtylyovc51e36d2011-05-07 19:44:13 +0400963 /*
964 * Unless caller treats short RX transfers as
965 * errors, we dare not queue multiple transfers.
966 */
967 dma_ok = dma_controller->channel_program(dma_channel,
968 packet_sz, !(urb->transfer_flags &
969 URB_SHORT_NOT_OK),
970 urb->transfer_dma + offset,
971 qh->segsize);
972 if (!dma_ok) {
973 dma_controller->channel_release(dma_channel);
974 hw_ep->rx_channel = dma_channel = NULL;
975 } else
976 csr |= MUSB_RXCSR_DMAENAB;
Felipe Balbi550a7372008-07-24 12:27:36 +0300977 }
978
979 csr |= MUSB_RXCSR_H_REQPKT;
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300980 dev_dbg(musb->controller, "RXCSR%d := %04x\n", epnum, csr);
Felipe Balbi550a7372008-07-24 12:27:36 +0300981 musb_writew(hw_ep->regs, MUSB_RXCSR, csr);
982 csr = musb_readw(hw_ep->regs, MUSB_RXCSR);
983 }
984}
985
Ajay Kumar Guptaf2838622012-07-19 13:41:59 +0530986/* Schedule next QH from musb->in_bulk/out_bulk and move the current qh to
987 * the end; avoids starvation for other endpoints.
988 */
989static void musb_bulk_nak_timeout(struct musb *musb, struct musb_hw_ep *ep,
990 int is_in)
991{
992 struct dma_channel *dma;
993 struct urb *urb;
994 void __iomem *mbase = musb->mregs;
995 void __iomem *epio = ep->regs;
996 struct musb_qh *cur_qh, *next_qh;
997 u16 rx_csr, tx_csr;
998
999 musb_ep_select(mbase, ep->epnum);
1000 if (is_in) {
1001 dma = is_dma_capable() ? ep->rx_channel : NULL;
1002
1003 /* clear nak timeout bit */
1004 rx_csr = musb_readw(epio, MUSB_RXCSR);
1005 rx_csr |= MUSB_RXCSR_H_WZC_BITS;
1006 rx_csr &= ~MUSB_RXCSR_DATAERROR;
1007 musb_writew(epio, MUSB_RXCSR, rx_csr);
1008
1009 cur_qh = first_qh(&musb->in_bulk);
1010 } else {
1011 dma = is_dma_capable() ? ep->tx_channel : NULL;
1012
1013 /* clear nak timeout bit */
1014 tx_csr = musb_readw(epio, MUSB_TXCSR);
1015 tx_csr |= MUSB_TXCSR_H_WZC_BITS;
1016 tx_csr &= ~MUSB_TXCSR_H_NAKTIMEOUT;
1017 musb_writew(epio, MUSB_TXCSR, tx_csr);
1018
1019 cur_qh = first_qh(&musb->out_bulk);
1020 }
1021 if (cur_qh) {
1022 urb = next_urb(cur_qh);
1023 if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) {
1024 dma->status = MUSB_DMA_STATUS_CORE_ABORT;
1025 musb->dma_controller->channel_abort(dma);
1026 urb->actual_length += dma->actual_len;
1027 dma->actual_len = 0L;
1028 }
1029 musb_save_toggle(cur_qh, is_in, urb);
1030
1031 if (is_in) {
1032 /* move cur_qh to end of queue */
1033 list_move_tail(&cur_qh->ring, &musb->in_bulk);
1034
1035 /* get the next qh from musb->in_bulk */
1036 next_qh = first_qh(&musb->in_bulk);
1037
1038 /* set rx_reinit and schedule the next qh */
1039 ep->rx_reinit = 1;
1040 } else {
1041 /* move cur_qh to end of queue */
1042 list_move_tail(&cur_qh->ring, &musb->out_bulk);
1043
1044 /* get the next qh from musb->out_bulk */
1045 next_qh = first_qh(&musb->out_bulk);
1046
1047 /* set tx_reinit and schedule the next qh */
1048 ep->tx_reinit = 1;
1049 }
1050 musb_start_urb(musb, is_in, next_qh);
1051 }
1052}
Felipe Balbi550a7372008-07-24 12:27:36 +03001053
1054/*
1055 * Service the default endpoint (ep0) as host.
1056 * Return true until it's time to start the status stage.
1057 */
1058static bool musb_h_ep0_continue(struct musb *musb, u16 len, struct urb *urb)
1059{
1060 bool more = false;
1061 u8 *fifo_dest = NULL;
1062 u16 fifo_count = 0;
1063 struct musb_hw_ep *hw_ep = musb->control_ep;
1064 struct musb_qh *qh = hw_ep->in_qh;
1065 struct usb_ctrlrequest *request;
1066
1067 switch (musb->ep0_stage) {
1068 case MUSB_EP0_IN:
1069 fifo_dest = urb->transfer_buffer + urb->actual_length;
Sergei Shtylyov3ecdb9a2009-02-21 15:31:23 -08001070 fifo_count = min_t(size_t, len, urb->transfer_buffer_length -
1071 urb->actual_length);
Felipe Balbi550a7372008-07-24 12:27:36 +03001072 if (fifo_count < len)
1073 urb->status = -EOVERFLOW;
1074
1075 musb_read_fifo(hw_ep, fifo_count, fifo_dest);
1076
1077 urb->actual_length += fifo_count;
1078 if (len < qh->maxpacket) {
1079 /* always terminate on short read; it's
1080 * rarely reported as an error.
1081 */
1082 } else if (urb->actual_length <
1083 urb->transfer_buffer_length)
1084 more = true;
1085 break;
1086 case MUSB_EP0_START:
1087 request = (struct usb_ctrlrequest *) urb->setup_packet;
1088
1089 if (!request->wLength) {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001090 dev_dbg(musb->controller, "start no-DATA\n");
Felipe Balbi550a7372008-07-24 12:27:36 +03001091 break;
1092 } else if (request->bRequestType & USB_DIR_IN) {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001093 dev_dbg(musb->controller, "start IN-DATA\n");
Felipe Balbi550a7372008-07-24 12:27:36 +03001094 musb->ep0_stage = MUSB_EP0_IN;
1095 more = true;
1096 break;
1097 } else {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001098 dev_dbg(musb->controller, "start OUT-DATA\n");
Felipe Balbi550a7372008-07-24 12:27:36 +03001099 musb->ep0_stage = MUSB_EP0_OUT;
1100 more = true;
1101 }
1102 /* FALLTHROUGH */
1103 case MUSB_EP0_OUT:
Sergei Shtylyov3ecdb9a2009-02-21 15:31:23 -08001104 fifo_count = min_t(size_t, qh->maxpacket,
1105 urb->transfer_buffer_length -
1106 urb->actual_length);
Felipe Balbi550a7372008-07-24 12:27:36 +03001107 if (fifo_count) {
1108 fifo_dest = (u8 *) (urb->transfer_buffer
1109 + urb->actual_length);
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001110 dev_dbg(musb->controller, "Sending %d byte%s to ep0 fifo %p\n",
David Brownellbb1c9ef2008-11-24 13:06:50 +02001111 fifo_count,
1112 (fifo_count == 1) ? "" : "s",
1113 fifo_dest);
Felipe Balbi550a7372008-07-24 12:27:36 +03001114 musb_write_fifo(hw_ep, fifo_count, fifo_dest);
1115
1116 urb->actual_length += fifo_count;
1117 more = true;
1118 }
1119 break;
1120 default:
1121 ERR("bogus ep0 stage %d\n", musb->ep0_stage);
1122 break;
1123 }
1124
1125 return more;
1126}
1127
1128/*
1129 * Handle default endpoint interrupt as host. Only called in IRQ time
David Brownellc767c1c2008-09-11 11:53:23 +03001130 * from musb_interrupt().
Felipe Balbi550a7372008-07-24 12:27:36 +03001131 *
1132 * called with controller irqlocked
1133 */
1134irqreturn_t musb_h_ep0_irq(struct musb *musb)
1135{
1136 struct urb *urb;
1137 u16 csr, len;
1138 int status = 0;
1139 void __iomem *mbase = musb->mregs;
1140 struct musb_hw_ep *hw_ep = musb->control_ep;
1141 void __iomem *epio = hw_ep->regs;
1142 struct musb_qh *qh = hw_ep->in_qh;
1143 bool complete = false;
1144 irqreturn_t retval = IRQ_NONE;
1145
1146 /* ep0 only has one queue, "in" */
1147 urb = next_urb(qh);
1148
1149 musb_ep_select(mbase, 0);
1150 csr = musb_readw(epio, MUSB_CSR0);
1151 len = (csr & MUSB_CSR0_RXPKTRDY)
1152 ? musb_readb(epio, MUSB_COUNT0)
1153 : 0;
1154
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001155 dev_dbg(musb->controller, "<== csr0 %04x, qh %p, count %d, urb %p, stage %d\n",
Felipe Balbi550a7372008-07-24 12:27:36 +03001156 csr, qh, len, urb, musb->ep0_stage);
1157
1158 /* if we just did status stage, we are done */
1159 if (MUSB_EP0_STATUS == musb->ep0_stage) {
1160 retval = IRQ_HANDLED;
1161 complete = true;
1162 }
1163
1164 /* prepare status */
1165 if (csr & MUSB_CSR0_H_RXSTALL) {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001166 dev_dbg(musb->controller, "STALLING ENDPOINT\n");
Felipe Balbi550a7372008-07-24 12:27:36 +03001167 status = -EPIPE;
1168
1169 } else if (csr & MUSB_CSR0_H_ERROR) {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001170 dev_dbg(musb->controller, "no response, csr0 %04x\n", csr);
Felipe Balbi550a7372008-07-24 12:27:36 +03001171 status = -EPROTO;
1172
1173 } else if (csr & MUSB_CSR0_H_NAKTIMEOUT) {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001174 dev_dbg(musb->controller, "control NAK timeout\n");
Felipe Balbi550a7372008-07-24 12:27:36 +03001175
1176 /* NOTE: this code path would be a good place to PAUSE a
1177 * control transfer, if another one is queued, so that
Ajay Kumar Gupta1e0320f2009-02-24 15:26:13 -08001178 * ep0 is more likely to stay busy. That's already done
1179 * for bulk RX transfers.
Felipe Balbi550a7372008-07-24 12:27:36 +03001180 *
1181 * if (qh->ring.next != &musb->control), then
1182 * we have a candidate... NAKing is *NOT* an error
1183 */
1184 musb_writew(epio, MUSB_CSR0, 0);
1185 retval = IRQ_HANDLED;
1186 }
1187
1188 if (status) {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001189 dev_dbg(musb->controller, "aborting\n");
Felipe Balbi550a7372008-07-24 12:27:36 +03001190 retval = IRQ_HANDLED;
1191 if (urb)
1192 urb->status = status;
1193 complete = true;
1194
1195 /* use the proper sequence to abort the transfer */
1196 if (csr & MUSB_CSR0_H_REQPKT) {
1197 csr &= ~MUSB_CSR0_H_REQPKT;
1198 musb_writew(epio, MUSB_CSR0, csr);
1199 csr &= ~MUSB_CSR0_H_NAKTIMEOUT;
1200 musb_writew(epio, MUSB_CSR0, csr);
1201 } else {
David Brownell78322c12009-03-26 17:38:30 -07001202 musb_h_ep0_flush_fifo(hw_ep);
Felipe Balbi550a7372008-07-24 12:27:36 +03001203 }
1204
1205 musb_writeb(epio, MUSB_NAKLIMIT0, 0);
1206
1207 /* clear it */
1208 musb_writew(epio, MUSB_CSR0, 0);
1209 }
1210
1211 if (unlikely(!urb)) {
1212 /* stop endpoint since we have no place for its data, this
1213 * SHOULD NEVER HAPPEN! */
1214 ERR("no URB for end 0\n");
1215
David Brownell78322c12009-03-26 17:38:30 -07001216 musb_h_ep0_flush_fifo(hw_ep);
Felipe Balbi550a7372008-07-24 12:27:36 +03001217 goto done;
1218 }
1219
1220 if (!complete) {
1221 /* call common logic and prepare response */
1222 if (musb_h_ep0_continue(musb, len, urb)) {
1223 /* more packets required */
1224 csr = (MUSB_EP0_IN == musb->ep0_stage)
1225 ? MUSB_CSR0_H_REQPKT : MUSB_CSR0_TXPKTRDY;
1226 } else {
1227 /* data transfer complete; perform status phase */
1228 if (usb_pipeout(urb->pipe)
1229 || !urb->transfer_buffer_length)
1230 csr = MUSB_CSR0_H_STATUSPKT
1231 | MUSB_CSR0_H_REQPKT;
1232 else
1233 csr = MUSB_CSR0_H_STATUSPKT
1234 | MUSB_CSR0_TXPKTRDY;
1235
Ajay Kumar Gupta3c4653c2014-02-04 15:28:06 +02001236 /* disable ping token in status phase */
1237 csr |= MUSB_CSR0_H_DIS_PING;
1238
Felipe Balbi550a7372008-07-24 12:27:36 +03001239 /* flag status stage */
1240 musb->ep0_stage = MUSB_EP0_STATUS;
1241
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001242 dev_dbg(musb->controller, "ep0 STATUS, csr %04x\n", csr);
Felipe Balbi550a7372008-07-24 12:27:36 +03001243
1244 }
1245 musb_writew(epio, MUSB_CSR0, csr);
1246 retval = IRQ_HANDLED;
1247 } else
1248 musb->ep0_stage = MUSB_EP0_IDLE;
1249
1250 /* call completion handler if done */
1251 if (complete)
1252 musb_advance_schedule(musb, urb, hw_ep, 1);
1253done:
1254 return retval;
1255}
1256
1257
1258#ifdef CONFIG_USB_INVENTRA_DMA
1259
1260/* Host side TX (OUT) using Mentor DMA works as follows:
1261 submit_urb ->
1262 - if queue was empty, Program Endpoint
1263 - ... which starts DMA to fifo in mode 1 or 0
1264
1265 DMA Isr (transfer complete) -> TxAvail()
1266 - Stop DMA (~DmaEnab) (<--- Alert ... currently happens
1267 only in musb_cleanup_urb)
1268 - TxPktRdy has to be set in mode 0 or for
1269 short packets in mode 1.
1270*/
1271
1272#endif
1273
1274/* Service a Tx-Available or dma completion irq for the endpoint */
1275void musb_host_tx(struct musb *musb, u8 epnum)
1276{
1277 int pipe;
1278 bool done = false;
1279 u16 tx_csr;
Sergei Shtylyov6b6e9712009-03-26 18:29:19 -07001280 size_t length = 0;
1281 size_t offset = 0;
Felipe Balbi550a7372008-07-24 12:27:36 +03001282 struct musb_hw_ep *hw_ep = musb->endpoints + epnum;
1283 void __iomem *epio = hw_ep->regs;
Sergei Shtylyov3e5c6dc2009-03-27 12:55:16 -07001284 struct musb_qh *qh = hw_ep->out_qh;
1285 struct urb *urb = next_urb(qh);
Felipe Balbi550a7372008-07-24 12:27:36 +03001286 u32 status = 0;
1287 void __iomem *mbase = musb->mregs;
1288 struct dma_channel *dma;
T. S., Anil Kumarf8afbf7f2010-09-24 13:44:09 +03001289 bool transfer_pending = false;
Felipe Balbi550a7372008-07-24 12:27:36 +03001290
Felipe Balbi550a7372008-07-24 12:27:36 +03001291 musb_ep_select(mbase, epnum);
1292 tx_csr = musb_readw(epio, MUSB_TXCSR);
1293
1294 /* with CPPI, DMA sometimes triggers "extra" irqs */
1295 if (!urb) {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001296 dev_dbg(musb->controller, "extra TX%d ready, csr %04x\n", epnum, tx_csr);
Sergei Shtylyov6b6e9712009-03-26 18:29:19 -07001297 return;
Felipe Balbi550a7372008-07-24 12:27:36 +03001298 }
1299
1300 pipe = urb->pipe;
1301 dma = is_dma_capable() ? hw_ep->tx_channel : NULL;
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001302 dev_dbg(musb->controller, "OUT/TX%d end, csr %04x%s\n", epnum, tx_csr,
Felipe Balbi550a7372008-07-24 12:27:36 +03001303 dma ? ", dma" : "");
1304
1305 /* check for errors */
1306 if (tx_csr & MUSB_TXCSR_H_RXSTALL) {
1307 /* dma was disabled, fifo flushed */
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001308 dev_dbg(musb->controller, "TX end %d stall\n", epnum);
Felipe Balbi550a7372008-07-24 12:27:36 +03001309
1310 /* stall; record URB status */
1311 status = -EPIPE;
1312
1313 } else if (tx_csr & MUSB_TXCSR_H_ERROR) {
1314 /* (NON-ISO) dma was disabled, fifo flushed */
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001315 dev_dbg(musb->controller, "TX 3strikes on ep=%d\n", epnum);
Felipe Balbi550a7372008-07-24 12:27:36 +03001316
1317 status = -ETIMEDOUT;
1318
1319 } else if (tx_csr & MUSB_TXCSR_H_NAKTIMEOUT) {
Ajay Kumar Guptaf2838622012-07-19 13:41:59 +05301320 if (USB_ENDPOINT_XFER_BULK == qh->type && qh->mux == 1
1321 && !list_is_singular(&musb->out_bulk)) {
1322 dev_dbg(musb->controller,
1323 "NAK timeout on TX%d ep\n", epnum);
1324 musb_bulk_nak_timeout(musb, hw_ep, 0);
1325 } else {
1326 dev_dbg(musb->controller,
1327 "TX end=%d device not responding\n", epnum);
1328 /* NOTE: this code path would be a good place to PAUSE a
1329 * transfer, if there's some other (nonperiodic) tx urb
1330 * that could use this fifo. (dma complicates it...)
1331 * That's already done for bulk RX transfers.
1332 *
1333 * if (bulk && qh->ring.next != &musb->out_bulk), then
1334 * we have a candidate... NAKing is *NOT* an error
1335 */
1336 musb_ep_select(mbase, epnum);
1337 musb_writew(epio, MUSB_TXCSR,
1338 MUSB_TXCSR_H_WZC_BITS
1339 | MUSB_TXCSR_TXPKTRDY);
1340 }
1341 return;
Felipe Balbi550a7372008-07-24 12:27:36 +03001342 }
1343
Virupax Sadashivpetimath8e8a5512012-08-07 14:46:20 +05301344done:
Felipe Balbi550a7372008-07-24 12:27:36 +03001345 if (status) {
1346 if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) {
1347 dma->status = MUSB_DMA_STATUS_CORE_ABORT;
Daniel Mack9c547692014-05-26 14:52:35 +02001348 musb->dma_controller->channel_abort(dma);
Felipe Balbi550a7372008-07-24 12:27:36 +03001349 }
1350
1351 /* do the proper sequence to abort the transfer in the
1352 * usb core; the dma engine should already be stopped.
1353 */
1354 musb_h_tx_flush_fifo(hw_ep);
1355 tx_csr &= ~(MUSB_TXCSR_AUTOSET
1356 | MUSB_TXCSR_DMAENAB
1357 | MUSB_TXCSR_H_ERROR
1358 | MUSB_TXCSR_H_RXSTALL
1359 | MUSB_TXCSR_H_NAKTIMEOUT
1360 );
1361
1362 musb_ep_select(mbase, epnum);
1363 musb_writew(epio, MUSB_TXCSR, tx_csr);
1364 /* REVISIT may need to clear FLUSHFIFO ... */
1365 musb_writew(epio, MUSB_TXCSR, tx_csr);
1366 musb_writeb(epio, MUSB_TXINTERVAL, 0);
1367
1368 done = true;
1369 }
1370
1371 /* second cppi case */
1372 if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001373 dev_dbg(musb->controller, "extra TX%d ready, csr %04x\n", epnum, tx_csr);
Sergei Shtylyov6b6e9712009-03-26 18:29:19 -07001374 return;
Felipe Balbi550a7372008-07-24 12:27:36 +03001375 }
1376
Sergei Shtylyovc7bbc052009-03-26 18:26:40 -07001377 if (is_dma_capable() && dma && !status) {
1378 /*
1379 * DMA has completed. But if we're using DMA mode 1 (multi
1380 * packet DMA), we need a terminal TXPKTRDY interrupt before
1381 * we can consider this transfer completed, lest we trash
1382 * its last packet when writing the next URB's data. So we
1383 * switch back to mode 0 to get that interrupt; we'll come
1384 * back here once it happens.
1385 */
1386 if (tx_csr & MUSB_TXCSR_DMAMODE) {
1387 /*
1388 * We shouldn't clear DMAMODE with DMAENAB set; so
1389 * clear them in a safe order. That should be OK
1390 * once TXPKTRDY has been set (and I've never seen
1391 * it being 0 at this moment -- DMA interrupt latency
1392 * is significant) but if it hasn't been then we have
1393 * no choice but to stop being polite and ignore the
1394 * programmer's guide... :-)
1395 *
1396 * Note that we must write TXCSR with TXPKTRDY cleared
1397 * in order not to re-trigger the packet send (this bit
1398 * can't be cleared by CPU), and there's another caveat:
1399 * TXPKTRDY may be set shortly and then cleared in the
1400 * double-buffered FIFO mode, so we do an extra TXCSR
1401 * read for debouncing...
1402 */
1403 tx_csr &= musb_readw(epio, MUSB_TXCSR);
1404 if (tx_csr & MUSB_TXCSR_TXPKTRDY) {
1405 tx_csr &= ~(MUSB_TXCSR_DMAENAB |
1406 MUSB_TXCSR_TXPKTRDY);
1407 musb_writew(epio, MUSB_TXCSR,
1408 tx_csr | MUSB_TXCSR_H_WZC_BITS);
1409 }
1410 tx_csr &= ~(MUSB_TXCSR_DMAMODE |
1411 MUSB_TXCSR_TXPKTRDY);
1412 musb_writew(epio, MUSB_TXCSR,
1413 tx_csr | MUSB_TXCSR_H_WZC_BITS);
1414
1415 /*
1416 * There is no guarantee that we'll get an interrupt
1417 * after clearing DMAMODE as we might have done this
1418 * too late (after TXPKTRDY was cleared by controller).
1419 * Re-read TXCSR as we have spoiled its previous value.
1420 */
1421 tx_csr = musb_readw(epio, MUSB_TXCSR);
1422 }
1423
1424 /*
1425 * We may get here from a DMA completion or TXPKTRDY interrupt.
1426 * In any case, we must check the FIFO status here and bail out
1427 * only if the FIFO still has data -- that should prevent the
1428 * "missed" TXPKTRDY interrupts and deal with double-buffered
1429 * FIFO mode too...
1430 */
1431 if (tx_csr & (MUSB_TXCSR_FIFONOTEMPTY | MUSB_TXCSR_TXPKTRDY)) {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001432 dev_dbg(musb->controller, "DMA complete but packet still in FIFO, "
Sergei Shtylyovc7bbc052009-03-26 18:26:40 -07001433 "CSR %04x\n", tx_csr);
1434 return;
1435 }
1436 }
1437
Felipe Balbi550a7372008-07-24 12:27:36 +03001438 if (!status || dma || usb_pipeisoc(pipe)) {
1439 if (dma)
Sergei Shtylyov6b6e9712009-03-26 18:29:19 -07001440 length = dma->actual_len;
Felipe Balbi550a7372008-07-24 12:27:36 +03001441 else
Sergei Shtylyov6b6e9712009-03-26 18:29:19 -07001442 length = qh->segsize;
1443 qh->offset += length;
Felipe Balbi550a7372008-07-24 12:27:36 +03001444
1445 if (usb_pipeisoc(pipe)) {
1446 struct usb_iso_packet_descriptor *d;
1447
1448 d = urb->iso_frame_desc + qh->iso_idx;
Sergei Shtylyov6b6e9712009-03-26 18:29:19 -07001449 d->actual_length = length;
1450 d->status = status;
Felipe Balbi550a7372008-07-24 12:27:36 +03001451 if (++qh->iso_idx >= urb->number_of_packets) {
1452 done = true;
1453 } else {
1454 d++;
Sergei Shtylyov6b6e9712009-03-26 18:29:19 -07001455 offset = d->offset;
1456 length = d->length;
Felipe Balbi550a7372008-07-24 12:27:36 +03001457 }
T. S., Anil Kumarf8afbf7f2010-09-24 13:44:09 +03001458 } else if (dma && urb->transfer_buffer_length == qh->offset) {
Felipe Balbi550a7372008-07-24 12:27:36 +03001459 done = true;
1460 } else {
1461 /* see if we need to send more data, or ZLP */
1462 if (qh->segsize < qh->maxpacket)
1463 done = true;
1464 else if (qh->offset == urb->transfer_buffer_length
1465 && !(urb->transfer_flags
1466 & URB_ZERO_PACKET))
1467 done = true;
1468 if (!done) {
Sergei Shtylyov6b6e9712009-03-26 18:29:19 -07001469 offset = qh->offset;
1470 length = urb->transfer_buffer_length - offset;
T. S., Anil Kumarf8afbf7f2010-09-24 13:44:09 +03001471 transfer_pending = true;
Felipe Balbi550a7372008-07-24 12:27:36 +03001472 }
1473 }
1474 }
1475
1476 /* urb->status != -EINPROGRESS means request has been faulted,
1477 * so we must abort this transfer after cleanup
1478 */
1479 if (urb->status != -EINPROGRESS) {
1480 done = true;
1481 if (status == 0)
1482 status = urb->status;
1483 }
1484
1485 if (done) {
1486 /* set status */
1487 urb->status = status;
1488 urb->actual_length = qh->offset;
1489 musb_advance_schedule(musb, urb, hw_ep, USB_DIR_OUT);
Sergei Shtylyov6b6e9712009-03-26 18:29:19 -07001490 return;
T. S., Anil Kumarf8afbf7f2010-09-24 13:44:09 +03001491 } else if ((usb_pipeisoc(pipe) || transfer_pending) && dma) {
Sergei Shtylyov6b6e9712009-03-26 18:29:19 -07001492 if (musb_tx_dma_program(musb->dma_controller, hw_ep, qh, urb,
Ajay Kumar Guptadfeffa52009-11-17 15:22:55 +05301493 offset, length)) {
Tony Lindgrenf8e9f34f2015-05-01 12:29:27 -07001494 if (is_cppi_enabled(musb) || tusb_dma_omap(musb))
Ajay Kumar Guptadfeffa52009-11-17 15:22:55 +05301495 musb_h_tx_dma_start(hw_ep);
Sergei Shtylyov6b6e9712009-03-26 18:29:19 -07001496 return;
Ajay Kumar Guptadfeffa52009-11-17 15:22:55 +05301497 }
Sergei Shtylyov6b6e9712009-03-26 18:29:19 -07001498 } else if (tx_csr & MUSB_TXCSR_DMAENAB) {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001499 dev_dbg(musb->controller, "not complete, but DMA enabled?\n");
Sergei Shtylyov6b6e9712009-03-26 18:29:19 -07001500 return;
1501 }
Felipe Balbi550a7372008-07-24 12:27:36 +03001502
Sergei Shtylyov6b6e9712009-03-26 18:29:19 -07001503 /*
1504 * PIO: start next packet in this URB.
1505 *
1506 * REVISIT: some docs say that when hw_ep->tx_double_buffered,
1507 * (and presumably, FIFO is not half-full) we should write *two*
1508 * packets before updating TXCSR; other docs disagree...
1509 */
1510 if (length > qh->maxpacket)
1511 length = qh->maxpacket;
Maulik Mankad496dda72010-09-24 13:44:06 +03001512 /* Unmap the buffer so that CPU can use it */
Daniel Mack8b125df2013-04-10 21:55:50 +02001513 usb_hcd_unmap_urb_for_dma(musb->hcd, urb);
Virupax Sadashivpetimath8e8a5512012-08-07 14:46:20 +05301514
1515 /*
1516 * We need to map sg if the transfer_buffer is
1517 * NULL.
1518 */
1519 if (!urb->transfer_buffer)
Virupax Sadashivpetimathed74df12013-04-24 08:38:48 +02001520 qh->use_sg = true;
Virupax Sadashivpetimath8e8a5512012-08-07 14:46:20 +05301521
Virupax Sadashivpetimathed74df12013-04-24 08:38:48 +02001522 if (qh->use_sg) {
Virupax Sadashivpetimath8e8a5512012-08-07 14:46:20 +05301523 /* sg_miter_start is already done in musb_ep_program */
1524 if (!sg_miter_next(&qh->sg_miter)) {
1525 dev_err(musb->controller, "error: sg list empty\n");
1526 sg_miter_stop(&qh->sg_miter);
1527 status = -EINVAL;
1528 goto done;
1529 }
1530 urb->transfer_buffer = qh->sg_miter.addr;
1531 length = min_t(u32, length, qh->sg_miter.length);
1532 musb_write_fifo(hw_ep, length, urb->transfer_buffer);
1533 qh->sg_miter.consumed = length;
1534 sg_miter_stop(&qh->sg_miter);
1535 } else {
1536 musb_write_fifo(hw_ep, length, urb->transfer_buffer + offset);
1537 }
1538
Sergei Shtylyov6b6e9712009-03-26 18:29:19 -07001539 qh->segsize = length;
Felipe Balbi550a7372008-07-24 12:27:36 +03001540
Virupax Sadashivpetimathed74df12013-04-24 08:38:48 +02001541 if (qh->use_sg) {
Virupax Sadashivpetimath8e8a5512012-08-07 14:46:20 +05301542 if (offset + length >= urb->transfer_buffer_length)
Virupax Sadashivpetimathed74df12013-04-24 08:38:48 +02001543 qh->use_sg = false;
Virupax Sadashivpetimath8e8a5512012-08-07 14:46:20 +05301544 }
1545
Sergei Shtylyov6b6e9712009-03-26 18:29:19 -07001546 musb_ep_select(mbase, epnum);
1547 musb_writew(epio, MUSB_TXCSR,
1548 MUSB_TXCSR_H_WZC_BITS | MUSB_TXCSR_TXPKTRDY);
Felipe Balbi550a7372008-07-24 12:27:36 +03001549}
1550
Tony Lindgren069a3fd2015-05-01 12:29:33 -07001551#ifdef CONFIG_USB_TI_CPPI41_DMA
1552/* Seems to set up ISO for cppi41 and not advance len. See commit c57c41d */
1553static int musb_rx_dma_iso_cppi41(struct dma_controller *dma,
1554 struct musb_hw_ep *hw_ep,
1555 struct musb_qh *qh,
1556 struct urb *urb,
1557 size_t len)
1558{
1559 struct dma_channel *channel = hw_ep->tx_channel;
1560 void __iomem *epio = hw_ep->regs;
1561 dma_addr_t *buf;
1562 u32 length, res;
1563 u16 val;
1564
1565 buf = (void *)urb->iso_frame_desc[qh->iso_idx].offset +
1566 (u32)urb->transfer_dma;
1567
1568 length = urb->iso_frame_desc[qh->iso_idx].length;
1569
1570 val = musb_readw(epio, MUSB_RXCSR);
1571 val |= MUSB_RXCSR_DMAENAB;
1572 musb_writew(hw_ep->regs, MUSB_RXCSR, val);
1573
1574 res = dma->channel_program(channel, qh->maxpacket, 0,
1575 (u32)buf, length);
1576
1577 return res;
1578}
1579#else
1580static inline int musb_rx_dma_iso_cppi41(struct dma_controller *dma,
1581 struct musb_hw_ep *hw_ep,
1582 struct musb_qh *qh,
1583 struct urb *urb,
1584 size_t len)
1585{
1586 return false;
1587}
1588#endif
Felipe Balbi550a7372008-07-24 12:27:36 +03001589
Tony Lindgrencff84bd2015-05-01 12:29:35 -07001590#if defined(CONFIG_USB_INVENTRA_DMA) || defined(CONFIG_USB_UX500_DMA) || \
1591 defined(CONFIG_USB_TI_CPPI41_DMA)
Felipe Balbi550a7372008-07-24 12:27:36 +03001592/* Host side RX (IN) using Mentor DMA works as follows:
1593 submit_urb ->
1594 - if queue was empty, ProgramEndpoint
1595 - first IN token is sent out (by setting ReqPkt)
1596 LinuxIsr -> RxReady()
1597 /\ => first packet is received
1598 | - Set in mode 0 (DmaEnab, ~ReqPkt)
1599 | -> DMA Isr (transfer complete) -> RxReady()
1600 | - Ack receive (~RxPktRdy), turn off DMA (~DmaEnab)
1601 | - if urb not complete, send next IN token (ReqPkt)
1602 | | else complete urb.
1603 | |
1604 ---------------------------
1605 *
1606 * Nuances of mode 1:
1607 * For short packets, no ack (+RxPktRdy) is sent automatically
1608 * (even if AutoClear is ON)
1609 * For full packets, ack (~RxPktRdy) and next IN token (+ReqPkt) is sent
1610 * automatically => major problem, as collecting the next packet becomes
1611 * difficult. Hence mode 1 is not used.
1612 *
1613 * REVISIT
1614 * All we care about at this driver level is that
1615 * (a) all URBs terminate with REQPKT cleared and fifo(s) empty;
1616 * (b) termination conditions are: short RX, or buffer full;
1617 * (c) fault modes include
1618 * - iff URB_SHORT_NOT_OK, short RX status is -EREMOTEIO.
1619 * (and that endpoint's dma queue stops immediately)
1620 * - overflow (full, PLUS more bytes in the terminal packet)
1621 *
1622 * So for example, usb-storage sets URB_SHORT_NOT_OK, and would
1623 * thus be a great candidate for using mode 1 ... for all but the
1624 * last packet of one URB's transfer.
1625 */
Tony Lindgrencff84bd2015-05-01 12:29:35 -07001626static int musb_rx_dma_inventra_cppi41(struct dma_controller *dma,
1627 struct musb_hw_ep *hw_ep,
1628 struct musb_qh *qh,
1629 struct urb *urb,
1630 size_t len)
1631{
1632 struct dma_channel *channel = hw_ep->rx_channel;
1633 void __iomem *epio = hw_ep->regs;
1634 u16 val;
1635 int pipe;
1636 bool done;
Felipe Balbi550a7372008-07-24 12:27:36 +03001637
Tony Lindgrencff84bd2015-05-01 12:29:35 -07001638 pipe = urb->pipe;
1639
1640 if (usb_pipeisoc(pipe)) {
1641 struct usb_iso_packet_descriptor *d;
1642
1643 d = urb->iso_frame_desc + qh->iso_idx;
1644 d->actual_length = len;
1645
1646 /* even if there was an error, we did the dma
1647 * for iso_frame_desc->length
1648 */
1649 if (d->status != -EILSEQ && d->status != -EOVERFLOW)
1650 d->status = 0;
1651
1652 if (++qh->iso_idx >= urb->number_of_packets) {
1653 done = true;
1654 } else {
1655 /* REVISIT: Why ignore return value here? */
1656 if (musb_dma_cppi41(hw_ep->musb))
1657 done = musb_rx_dma_iso_cppi41(dma, hw_ep, qh,
1658 urb, len);
1659 done = false;
1660 }
1661
1662 } else {
1663 /* done if urb buffer is full or short packet is recd */
1664 done = (urb->actual_length + len >=
1665 urb->transfer_buffer_length
1666 || channel->actual_len < qh->maxpacket
1667 || channel->rx_packet_done);
1668 }
1669
1670 /* send IN token for next packet, without AUTOREQ */
1671 if (!done) {
1672 val = musb_readw(epio, MUSB_RXCSR);
1673 val |= MUSB_RXCSR_H_REQPKT;
1674 musb_writew(epio, MUSB_RXCSR, MUSB_RXCSR_H_WZC_BITS | val);
1675 }
1676
1677 return done;
1678}
Tony Lindgrenac33cdb2015-05-01 12:29:37 -07001679
1680/* Disadvantage of using mode 1:
1681 * It's basically usable only for mass storage class; essentially all
1682 * other protocols also terminate transfers on short packets.
1683 *
1684 * Details:
1685 * An extra IN token is sent at the end of the transfer (due to AUTOREQ)
1686 * If you try to use mode 1 for (transfer_buffer_length - 512), and try
1687 * to use the extra IN token to grab the last packet using mode 0, then
1688 * the problem is that you cannot be sure when the device will send the
1689 * last packet and RxPktRdy set. Sometimes the packet is recd too soon
1690 * such that it gets lost when RxCSR is re-set at the end of the mode 1
1691 * transfer, while sometimes it is recd just a little late so that if you
1692 * try to configure for mode 0 soon after the mode 1 transfer is
1693 * completed, you will find rxcount 0. Okay, so you might think why not
1694 * wait for an interrupt when the pkt is recd. Well, you won't get any!
1695 */
1696static int musb_rx_dma_in_inventra_cppi41(struct dma_controller *dma,
1697 struct musb_hw_ep *hw_ep,
1698 struct musb_qh *qh,
1699 struct urb *urb,
1700 size_t len,
1701 u8 iso_err)
1702{
1703 struct musb *musb = hw_ep->musb;
1704 void __iomem *epio = hw_ep->regs;
1705 struct dma_channel *channel = hw_ep->rx_channel;
1706 u16 rx_count, val;
1707 int length, pipe, done;
1708 dma_addr_t buf;
1709
1710 rx_count = musb_readw(epio, MUSB_RXCOUNT);
1711 pipe = urb->pipe;
1712
1713 if (usb_pipeisoc(pipe)) {
1714 int d_status = 0;
1715 struct usb_iso_packet_descriptor *d;
1716
1717 d = urb->iso_frame_desc + qh->iso_idx;
1718
1719 if (iso_err) {
1720 d_status = -EILSEQ;
1721 urb->error_count++;
1722 }
1723 if (rx_count > d->length) {
1724 if (d_status == 0) {
1725 d_status = -EOVERFLOW;
1726 urb->error_count++;
1727 }
1728 dev_dbg(musb->controller, "** OVERFLOW %d into %d\n",
1729 rx_count, d->length);
1730
1731 length = d->length;
1732 } else
1733 length = rx_count;
1734 d->status = d_status;
1735 buf = urb->transfer_dma + d->offset;
1736 } else {
1737 length = rx_count;
1738 buf = urb->transfer_dma + urb->actual_length;
1739 }
1740
1741 channel->desired_mode = 0;
1742#ifdef USE_MODE1
1743 /* because of the issue below, mode 1 will
1744 * only rarely behave with correct semantics.
1745 */
1746 if ((urb->transfer_flags & URB_SHORT_NOT_OK)
1747 && (urb->transfer_buffer_length - urb->actual_length)
1748 > qh->maxpacket)
1749 channel->desired_mode = 1;
1750 if (rx_count < hw_ep->max_packet_sz_rx) {
1751 length = rx_count;
1752 channel->desired_mode = 0;
1753 } else {
1754 length = urb->transfer_buffer_length;
1755 }
1756#endif
1757
1758 /* See comments above on disadvantages of using mode 1 */
1759 val = musb_readw(epio, MUSB_RXCSR);
1760 val &= ~MUSB_RXCSR_H_REQPKT;
1761
1762 if (channel->desired_mode == 0)
1763 val &= ~MUSB_RXCSR_H_AUTOREQ;
1764 else
1765 val |= MUSB_RXCSR_H_AUTOREQ;
1766 val |= MUSB_RXCSR_DMAENAB;
1767
1768 /* autoclear shouldn't be set in high bandwidth */
1769 if (qh->hb_mult == 1)
1770 val |= MUSB_RXCSR_AUTOCLEAR;
1771
1772 musb_writew(epio, MUSB_RXCSR, MUSB_RXCSR_H_WZC_BITS | val);
1773
1774 /* REVISIT if when actual_length != 0,
1775 * transfer_buffer_length needs to be
1776 * adjusted first...
1777 */
1778 done = dma->channel_program(channel, qh->maxpacket,
1779 channel->desired_mode,
1780 buf, length);
1781
1782 if (!done) {
1783 dma->channel_release(channel);
1784 hw_ep->rx_channel = NULL;
1785 channel = NULL;
1786 val = musb_readw(epio, MUSB_RXCSR);
1787 val &= ~(MUSB_RXCSR_DMAENAB
1788 | MUSB_RXCSR_H_AUTOREQ
1789 | MUSB_RXCSR_AUTOCLEAR);
1790 musb_writew(epio, MUSB_RXCSR, val);
1791 }
1792
1793 return done;
1794}
Tony Lindgrencff84bd2015-05-01 12:29:35 -07001795#else
1796static inline int musb_rx_dma_inventra_cppi41(struct dma_controller *dma,
1797 struct musb_hw_ep *hw_ep,
1798 struct musb_qh *qh,
1799 struct urb *urb,
1800 size_t len)
1801{
1802 return false;
1803}
Tony Lindgrenac33cdb2015-05-01 12:29:37 -07001804
1805static inline int musb_rx_dma_in_inventra_cppi41(struct dma_controller *dma,
1806 struct musb_hw_ep *hw_ep,
1807 struct musb_qh *qh,
1808 struct urb *urb,
1809 size_t len,
1810 u8 iso_err)
1811{
1812 return false;
1813}
Felipe Balbi550a7372008-07-24 12:27:36 +03001814#endif
1815
1816/*
1817 * Service an RX interrupt for the given IN endpoint; docs cover bulk, iso,
1818 * and high-bandwidth IN transfer cases.
1819 */
1820void musb_host_rx(struct musb *musb, u8 epnum)
1821{
1822 struct urb *urb;
1823 struct musb_hw_ep *hw_ep = musb->endpoints + epnum;
Tony Lindgrencff84bd2015-05-01 12:29:35 -07001824 struct dma_controller *c = musb->dma_controller;
Felipe Balbi550a7372008-07-24 12:27:36 +03001825 void __iomem *epio = hw_ep->regs;
1826 struct musb_qh *qh = hw_ep->in_qh;
1827 size_t xfer_len;
1828 void __iomem *mbase = musb->mregs;
1829 int pipe;
1830 u16 rx_csr, val;
1831 bool iso_err = false;
1832 bool done = false;
1833 u32 status;
1834 struct dma_channel *dma;
Virupax Sadashivpetimath8e8a5512012-08-07 14:46:20 +05301835 unsigned int sg_flags = SG_MITER_ATOMIC | SG_MITER_TO_SG;
Felipe Balbi550a7372008-07-24 12:27:36 +03001836
1837 musb_ep_select(mbase, epnum);
1838
1839 urb = next_urb(qh);
1840 dma = is_dma_capable() ? hw_ep->rx_channel : NULL;
1841 status = 0;
1842 xfer_len = 0;
1843
1844 rx_csr = musb_readw(epio, MUSB_RXCSR);
1845 val = rx_csr;
1846
1847 if (unlikely(!urb)) {
1848 /* REVISIT -- THIS SHOULD NEVER HAPPEN ... but, at least
1849 * usbtest #11 (unlinks) triggers it regularly, sometimes
1850 * with fifo full. (Only with DMA??)
1851 */
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001852 dev_dbg(musb->controller, "BOGUS RX%d ready, csr %04x, count %d\n", epnum, val,
Felipe Balbi550a7372008-07-24 12:27:36 +03001853 musb_readw(epio, MUSB_RXCOUNT));
1854 musb_h_flush_rxfifo(hw_ep, MUSB_RXCSR_CLRDATATOG);
1855 return;
1856 }
1857
1858 pipe = urb->pipe;
1859
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001860 dev_dbg(musb->controller, "<== hw %d rxcsr %04x, urb actual %d (+dma %zu)\n",
Felipe Balbi550a7372008-07-24 12:27:36 +03001861 epnum, rx_csr, urb->actual_length,
1862 dma ? dma->actual_len : 0);
1863
1864 /* check for errors, concurrent stall & unlink is not really
1865 * handled yet! */
1866 if (rx_csr & MUSB_RXCSR_H_RXSTALL) {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001867 dev_dbg(musb->controller, "RX end %d STALL\n", epnum);
Felipe Balbi550a7372008-07-24 12:27:36 +03001868
1869 /* stall; record URB status */
1870 status = -EPIPE;
1871
1872 } else if (rx_csr & MUSB_RXCSR_H_ERROR) {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001873 dev_dbg(musb->controller, "end %d RX proto error\n", epnum);
Felipe Balbi550a7372008-07-24 12:27:36 +03001874
1875 status = -EPROTO;
1876 musb_writeb(epio, MUSB_RXINTERVAL, 0);
1877
Bin Liub5801212016-05-31 10:05:03 -05001878 rx_csr &= ~MUSB_RXCSR_H_ERROR;
1879 musb_writew(epio, MUSB_RXCSR, rx_csr);
1880
Felipe Balbi550a7372008-07-24 12:27:36 +03001881 } else if (rx_csr & MUSB_RXCSR_DATAERROR) {
1882
1883 if (USB_ENDPOINT_XFER_ISOC != qh->type) {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001884 dev_dbg(musb->controller, "RX end %d NAK timeout\n", epnum);
Ajay Kumar Gupta1e0320f2009-02-24 15:26:13 -08001885
1886 /* NOTE: NAKing is *NOT* an error, so we want to
1887 * continue. Except ... if there's a request for
1888 * another QH, use that instead of starving it.
1889 *
1890 * Devices like Ethernet and serial adapters keep
1891 * reads posted at all times, which will starve
1892 * other devices without this logic.
1893 */
1894 if (usb_pipebulk(urb->pipe)
1895 && qh->mux == 1
1896 && !list_is_singular(&musb->in_bulk)) {
Ajay Kumar Guptaf2838622012-07-19 13:41:59 +05301897 musb_bulk_nak_timeout(musb, hw_ep, 1);
Ajay Kumar Gupta1e0320f2009-02-24 15:26:13 -08001898 return;
1899 }
Felipe Balbi550a7372008-07-24 12:27:36 +03001900 musb_ep_select(mbase, epnum);
Ajay Kumar Gupta1e0320f2009-02-24 15:26:13 -08001901 rx_csr |= MUSB_RXCSR_H_WZC_BITS;
1902 rx_csr &= ~MUSB_RXCSR_DATAERROR;
1903 musb_writew(epio, MUSB_RXCSR, rx_csr);
Felipe Balbi550a7372008-07-24 12:27:36 +03001904
1905 goto finish;
1906 } else {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001907 dev_dbg(musb->controller, "RX end %d ISO data error\n", epnum);
Felipe Balbi550a7372008-07-24 12:27:36 +03001908 /* packet error reported later */
1909 iso_err = true;
1910 }
Ajay Kumar Guptaa483d702009-04-03 16:16:17 -07001911 } else if (rx_csr & MUSB_RXCSR_INCOMPRX) {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001912 dev_dbg(musb->controller, "end %d high bandwidth incomplete ISO packet RX\n",
Ajay Kumar Guptaa483d702009-04-03 16:16:17 -07001913 epnum);
1914 status = -EPROTO;
Felipe Balbi550a7372008-07-24 12:27:36 +03001915 }
1916
1917 /* faults abort the transfer */
1918 if (status) {
1919 /* clean up dma and collect transfer count */
1920 if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) {
1921 dma->status = MUSB_DMA_STATUS_CORE_ABORT;
Daniel Mack9c547692014-05-26 14:52:35 +02001922 musb->dma_controller->channel_abort(dma);
Felipe Balbi550a7372008-07-24 12:27:36 +03001923 xfer_len = dma->actual_len;
1924 }
1925 musb_h_flush_rxfifo(hw_ep, MUSB_RXCSR_CLRDATATOG);
1926 musb_writeb(epio, MUSB_RXINTERVAL, 0);
1927 done = true;
1928 goto finish;
1929 }
1930
1931 if (unlikely(dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY)) {
1932 /* SHOULD NEVER HAPPEN ... but at least DaVinci has done it */
1933 ERR("RX%d dma busy, csr %04x\n", epnum, rx_csr);
1934 goto finish;
1935 }
1936
1937 /* thorough shutdown for now ... given more precise fault handling
1938 * and better queueing support, we might keep a DMA pipeline going
1939 * while processing this irq for earlier completions.
1940 */
1941
1942 /* FIXME this is _way_ too much in-line logic for Mentor DMA */
Tony Lindgren557d5432015-05-01 12:29:34 -07001943 if (!musb_dma_inventra(musb) && !musb_dma_ux500(musb) &&
1944 (rx_csr & MUSB_RXCSR_H_REQPKT)) {
Felipe Balbi550a7372008-07-24 12:27:36 +03001945 /* REVISIT this happened for a while on some short reads...
1946 * the cleanup still needs investigation... looks bad...
1947 * and also duplicates dma cleanup code above ... plus,
1948 * shouldn't this be the "half full" double buffer case?
1949 */
1950 if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) {
1951 dma->status = MUSB_DMA_STATUS_CORE_ABORT;
Daniel Mack9c547692014-05-26 14:52:35 +02001952 musb->dma_controller->channel_abort(dma);
Felipe Balbi550a7372008-07-24 12:27:36 +03001953 xfer_len = dma->actual_len;
1954 done = true;
1955 }
1956
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001957 dev_dbg(musb->controller, "RXCSR%d %04x, reqpkt, len %zu%s\n", epnum, rx_csr,
Felipe Balbi550a7372008-07-24 12:27:36 +03001958 xfer_len, dma ? ", dma" : "");
1959 rx_csr &= ~MUSB_RXCSR_H_REQPKT;
1960
1961 musb_ep_select(mbase, epnum);
1962 musb_writew(epio, MUSB_RXCSR,
1963 MUSB_RXCSR_H_WZC_BITS | rx_csr);
1964 }
Tony Lindgren557d5432015-05-01 12:29:34 -07001965
Felipe Balbi550a7372008-07-24 12:27:36 +03001966 if (dma && (rx_csr & MUSB_RXCSR_DMAENAB)) {
1967 xfer_len = dma->actual_len;
1968
1969 val &= ~(MUSB_RXCSR_DMAENAB
1970 | MUSB_RXCSR_H_AUTOREQ
1971 | MUSB_RXCSR_AUTOCLEAR
1972 | MUSB_RXCSR_RXPKTRDY);
1973 musb_writew(hw_ep->regs, MUSB_RXCSR, val);
1974
Tony Lindgrencff84bd2015-05-01 12:29:35 -07001975 if (musb_dma_inventra(musb) || musb_dma_ux500(musb) ||
1976 musb_dma_cppi41(musb)) {
1977 done = musb_rx_dma_inventra_cppi41(c, hw_ep, qh, urb, xfer_len);
1978 dev_dbg(hw_ep->musb->controller,
1979 "ep %d dma %s, rxcsr %04x, rxcount %d\n",
1980 epnum, done ? "off" : "reset",
1981 musb_readw(epio, MUSB_RXCSR),
1982 musb_readw(epio, MUSB_RXCOUNT));
1983 } else {
1984 done = true;
Ajay Kumar Guptaf82a6892008-10-29 15:10:31 +02001985 }
Felipe Balbi550a7372008-07-24 12:27:36 +03001986
Felipe Balbi550a7372008-07-24 12:27:36 +03001987 } else if (urb->status == -EINPROGRESS) {
1988 /* if no errors, be sure a packet is ready for unloading */
1989 if (unlikely(!(rx_csr & MUSB_RXCSR_RXPKTRDY))) {
1990 status = -EPROTO;
1991 ERR("Rx interrupt with no errors or packet!\n");
1992
1993 /* FIXME this is another "SHOULD NEVER HAPPEN" */
1994
1995/* SCRUB (RX) */
1996 /* do the proper sequence to abort the transfer */
1997 musb_ep_select(mbase, epnum);
1998 val &= ~MUSB_RXCSR_H_REQPKT;
1999 musb_writew(epio, MUSB_RXCSR, val);
2000 goto finish;
2001 }
2002
2003 /* we are expecting IN packets */
Tony Lindgrene530bb82015-05-01 12:29:36 -07002004 if ((musb_dma_inventra(musb) || musb_dma_ux500(musb) ||
2005 musb_dma_cppi41(musb)) && dma) {
Tony Lindgrenac33cdb2015-05-01 12:29:37 -07002006 dev_dbg(hw_ep->musb->controller,
2007 "RX%d count %d, buffer 0x%llx len %d/%d\n",
2008 epnum, musb_readw(epio, MUSB_RXCOUNT),
2009 (unsigned long long) urb->transfer_dma
2010 + urb->actual_length,
2011 qh->offset,
2012 urb->transfer_buffer_length);
Felipe Balbi550a7372008-07-24 12:27:36 +03002013
Cristian Birsan4c2ba0c2016-02-19 10:11:56 +02002014 if (musb_rx_dma_in_inventra_cppi41(c, hw_ep, qh, urb,
2015 xfer_len, iso_err))
Tony Lindgrenac33cdb2015-05-01 12:29:37 -07002016 goto finish;
Felipe Balbi550a7372008-07-24 12:27:36 +03002017 else
Tony Lindgrenac33cdb2015-05-01 12:29:37 -07002018 dev_err(musb->controller, "error: rx_dma failed\n");
Felipe Balbi550a7372008-07-24 12:27:36 +03002019 }
Felipe Balbi550a7372008-07-24 12:27:36 +03002020
2021 if (!dma) {
Virupax Sadashivpetimath8e8a5512012-08-07 14:46:20 +05302022 unsigned int received_len;
2023
Maulik Mankad496dda72010-09-24 13:44:06 +03002024 /* Unmap the buffer so that CPU can use it */
Daniel Mack8b125df2013-04-10 21:55:50 +02002025 usb_hcd_unmap_urb_for_dma(musb->hcd, urb);
Virupax Sadashivpetimath8e8a5512012-08-07 14:46:20 +05302026
2027 /*
2028 * We need to map sg if the transfer_buffer is
2029 * NULL.
2030 */
2031 if (!urb->transfer_buffer) {
Virupax Sadashivpetimathed74df12013-04-24 08:38:48 +02002032 qh->use_sg = true;
Virupax Sadashivpetimath8e8a5512012-08-07 14:46:20 +05302033 sg_miter_start(&qh->sg_miter, urb->sg, 1,
2034 sg_flags);
2035 }
2036
Virupax Sadashivpetimathed74df12013-04-24 08:38:48 +02002037 if (qh->use_sg) {
Virupax Sadashivpetimath8e8a5512012-08-07 14:46:20 +05302038 if (!sg_miter_next(&qh->sg_miter)) {
2039 dev_err(musb->controller, "error: sg list empty\n");
2040 sg_miter_stop(&qh->sg_miter);
2041 status = -EINVAL;
2042 done = true;
2043 goto finish;
2044 }
2045 urb->transfer_buffer = qh->sg_miter.addr;
2046 received_len = urb->actual_length;
2047 qh->offset = 0x0;
2048 done = musb_host_packet_rx(musb, urb, epnum,
2049 iso_err);
2050 /* Calculate the number of bytes received */
2051 received_len = urb->actual_length -
2052 received_len;
2053 qh->sg_miter.consumed = received_len;
2054 sg_miter_stop(&qh->sg_miter);
2055 } else {
2056 done = musb_host_packet_rx(musb, urb,
2057 epnum, iso_err);
2058 }
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03002059 dev_dbg(musb->controller, "read %spacket\n", done ? "last " : "");
Felipe Balbi550a7372008-07-24 12:27:36 +03002060 }
2061 }
2062
Felipe Balbi550a7372008-07-24 12:27:36 +03002063finish:
2064 urb->actual_length += xfer_len;
2065 qh->offset += xfer_len;
2066 if (done) {
Virupax Sadashivpetimathed74df12013-04-24 08:38:48 +02002067 if (qh->use_sg)
2068 qh->use_sg = false;
Virupax Sadashivpetimath8e8a5512012-08-07 14:46:20 +05302069
Felipe Balbi550a7372008-07-24 12:27:36 +03002070 if (urb->status == -EINPROGRESS)
2071 urb->status = status;
2072 musb_advance_schedule(musb, urb, hw_ep, USB_DIR_IN);
2073 }
2074}
2075
2076/* schedule nodes correspond to peripheral endpoints, like an OHCI QH.
2077 * the software schedule associates multiple such nodes with a given
2078 * host side hardware endpoint + direction; scheduling may activate
2079 * that hardware endpoint.
2080 */
2081static int musb_schedule(
2082 struct musb *musb,
2083 struct musb_qh *qh,
2084 int is_in)
2085{
Rickard Strandqvisteac44dc2014-06-01 15:48:12 +02002086 int idle = 0;
Felipe Balbi550a7372008-07-24 12:27:36 +03002087 int best_diff;
2088 int best_end, epnum;
2089 struct musb_hw_ep *hw_ep = NULL;
2090 struct list_head *head = NULL;
Swaminathan S5274dab2009-12-28 13:40:37 +02002091 u8 toggle;
2092 u8 txtype;
2093 struct urb *urb = next_urb(qh);
Felipe Balbi550a7372008-07-24 12:27:36 +03002094
2095 /* use fixed hardware for control and bulk */
Ajay Kumar Gupta23d15e02008-10-29 15:10:35 +02002096 if (qh->type == USB_ENDPOINT_XFER_CONTROL) {
Felipe Balbi550a7372008-07-24 12:27:36 +03002097 head = &musb->control;
2098 hw_ep = musb->control_ep;
Felipe Balbi550a7372008-07-24 12:27:36 +03002099 goto success;
2100 }
2101
2102 /* else, periodic transfers get muxed to other endpoints */
2103
Sergei Shtylyov5d67a852009-02-24 15:23:34 -08002104 /*
2105 * We know this qh hasn't been scheduled, so all we need to do
Felipe Balbi550a7372008-07-24 12:27:36 +03002106 * is choose which hardware endpoint to put it on ...
2107 *
2108 * REVISIT what we really want here is a regular schedule tree
Sergei Shtylyov5d67a852009-02-24 15:23:34 -08002109 * like e.g. OHCI uses.
Felipe Balbi550a7372008-07-24 12:27:36 +03002110 */
2111 best_diff = 4096;
2112 best_end = -1;
2113
Sergei Shtylyov5d67a852009-02-24 15:23:34 -08002114 for (epnum = 1, hw_ep = musb->endpoints + 1;
2115 epnum < musb->nr_endpoints;
2116 epnum++, hw_ep++) {
Felipe Balbi550a7372008-07-24 12:27:36 +03002117 int diff;
2118
Sergei Shtylyov3e5c6dc2009-03-27 12:55:16 -07002119 if (musb_ep_get_qh(hw_ep, is_in) != NULL)
Felipe Balbi550a7372008-07-24 12:27:36 +03002120 continue;
Sergei Shtylyov5d67a852009-02-24 15:23:34 -08002121
Felipe Balbi550a7372008-07-24 12:27:36 +03002122 if (hw_ep == musb->bulk_ep)
2123 continue;
2124
2125 if (is_in)
Ajay Kumar Guptaa483d702009-04-03 16:16:17 -07002126 diff = hw_ep->max_packet_sz_rx;
Felipe Balbi550a7372008-07-24 12:27:36 +03002127 else
Ajay Kumar Guptaa483d702009-04-03 16:16:17 -07002128 diff = hw_ep->max_packet_sz_tx;
2129 diff -= (qh->maxpacket * qh->hb_mult);
Felipe Balbi550a7372008-07-24 12:27:36 +03002130
Ajay Kumar Gupta23d15e02008-10-29 15:10:35 +02002131 if (diff >= 0 && best_diff > diff) {
Swaminathan S5274dab2009-12-28 13:40:37 +02002132
2133 /*
2134 * Mentor controller has a bug in that if we schedule
2135 * a BULK Tx transfer on an endpoint that had earlier
2136 * handled ISOC then the BULK transfer has to start on
2137 * a zero toggle. If the BULK transfer starts on a 1
2138 * toggle then this transfer will fail as the mentor
2139 * controller starts the Bulk transfer on a 0 toggle
2140 * irrespective of the programming of the toggle bits
2141 * in the TXCSR register. Check for this condition
2142 * while allocating the EP for a Tx Bulk transfer. If
2143 * so skip this EP.
2144 */
2145 hw_ep = musb->endpoints + epnum;
2146 toggle = usb_gettoggle(urb->dev, qh->epnum, !is_in);
2147 txtype = (musb_readb(hw_ep->regs, MUSB_TXTYPE)
2148 >> 4) & 0x3;
2149 if (!is_in && (qh->type == USB_ENDPOINT_XFER_BULK) &&
2150 toggle && (txtype == USB_ENDPOINT_XFER_ISOC))
2151 continue;
2152
Felipe Balbi550a7372008-07-24 12:27:36 +03002153 best_diff = diff;
2154 best_end = epnum;
2155 }
2156 }
Ajay Kumar Gupta23d15e02008-10-29 15:10:35 +02002157 /* use bulk reserved ep1 if no other ep is free */
Felipe Balbiaa5cbbe2008-11-17 09:08:16 +02002158 if (best_end < 0 && qh->type == USB_ENDPOINT_XFER_BULK) {
Ajay Kumar Gupta23d15e02008-10-29 15:10:35 +02002159 hw_ep = musb->bulk_ep;
2160 if (is_in)
2161 head = &musb->in_bulk;
2162 else
2163 head = &musb->out_bulk;
Ajay Kumar Gupta1e0320f2009-02-24 15:26:13 -08002164
Ajay Kumar Guptaf2838622012-07-19 13:41:59 +05302165 /* Enable bulk RX/TX NAK timeout scheme when bulk requests are
Rahul Bedarkar5ae477b2014-01-02 19:27:47 +05302166 * multiplexed. This scheme does not work in high speed to full
Ajay Kumar Gupta1e0320f2009-02-24 15:26:13 -08002167 * speed scenario as NAK interrupts are not coming from a
2168 * full speed device connected to a high speed device.
2169 * NAK timeout interval is 8 (128 uframe or 16ms) for HS and
2170 * 4 (8 frame or 8ms) for FS device.
2171 */
Ajay Kumar Guptaf2838622012-07-19 13:41:59 +05302172 if (qh->dev)
Ajay Kumar Gupta1e0320f2009-02-24 15:26:13 -08002173 qh->intv_reg =
2174 (USB_SPEED_HIGH == qh->dev->speed) ? 8 : 4;
Ajay Kumar Gupta23d15e02008-10-29 15:10:35 +02002175 goto success;
2176 } else if (best_end < 0) {
Felipe Balbi550a7372008-07-24 12:27:36 +03002177 return -ENOSPC;
Ajay Kumar Gupta23d15e02008-10-29 15:10:35 +02002178 }
Felipe Balbi550a7372008-07-24 12:27:36 +03002179
2180 idle = 1;
Ajay Kumar Gupta23d15e02008-10-29 15:10:35 +02002181 qh->mux = 0;
Felipe Balbi550a7372008-07-24 12:27:36 +03002182 hw_ep = musb->endpoints + best_end;
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03002183 dev_dbg(musb->controller, "qh %p periodic slot %d\n", qh, best_end);
Felipe Balbi550a7372008-07-24 12:27:36 +03002184success:
Ajay Kumar Gupta23d15e02008-10-29 15:10:35 +02002185 if (head) {
2186 idle = list_empty(head);
2187 list_add_tail(&qh->ring, head);
2188 qh->mux = 1;
2189 }
Felipe Balbi550a7372008-07-24 12:27:36 +03002190 qh->hw_ep = hw_ep;
2191 qh->hep->hcpriv = qh;
2192 if (idle)
2193 musb_start_urb(musb, is_in, qh);
2194 return 0;
2195}
2196
2197static int musb_urb_enqueue(
2198 struct usb_hcd *hcd,
2199 struct urb *urb,
2200 gfp_t mem_flags)
2201{
2202 unsigned long flags;
2203 struct musb *musb = hcd_to_musb(hcd);
2204 struct usb_host_endpoint *hep = urb->ep;
David Brownell74bb3502009-03-26 17:36:57 -07002205 struct musb_qh *qh;
Felipe Balbi550a7372008-07-24 12:27:36 +03002206 struct usb_endpoint_descriptor *epd = &hep->desc;
2207 int ret;
2208 unsigned type_reg;
2209 unsigned interval;
2210
2211 /* host role must be active */
2212 if (!is_host_active(musb) || !musb->is_active)
2213 return -ENODEV;
2214
2215 spin_lock_irqsave(&musb->lock, flags);
2216 ret = usb_hcd_link_urb_to_ep(hcd, urb);
David Brownell74bb3502009-03-26 17:36:57 -07002217 qh = ret ? NULL : hep->hcpriv;
2218 if (qh)
2219 urb->hcpriv = qh;
Felipe Balbi550a7372008-07-24 12:27:36 +03002220 spin_unlock_irqrestore(&musb->lock, flags);
Felipe Balbi550a7372008-07-24 12:27:36 +03002221
2222 /* DMA mapping was already done, if needed, and this urb is on
David Brownell74bb3502009-03-26 17:36:57 -07002223 * hep->urb_list now ... so we're done, unless hep wasn't yet
2224 * scheduled onto a live qh.
Felipe Balbi550a7372008-07-24 12:27:36 +03002225 *
2226 * REVISIT best to keep hep->hcpriv valid until the endpoint gets
2227 * disabled, testing for empty qh->ring and avoiding qh setup costs
2228 * except for the first urb queued after a config change.
2229 */
David Brownell74bb3502009-03-26 17:36:57 -07002230 if (qh || ret)
2231 return ret;
Felipe Balbi550a7372008-07-24 12:27:36 +03002232
2233 /* Allocate and initialize qh, minimizing the work done each time
2234 * hw_ep gets reprogrammed, or with irqs blocked. Then schedule it.
2235 *
2236 * REVISIT consider a dedicated qh kmem_cache, so it's harder
2237 * for bugs in other kernel code to break this driver...
2238 */
2239 qh = kzalloc(sizeof *qh, mem_flags);
2240 if (!qh) {
Ajay Kumar Gupta2492e672008-09-11 11:53:21 +03002241 spin_lock_irqsave(&musb->lock, flags);
Felipe Balbi550a7372008-07-24 12:27:36 +03002242 usb_hcd_unlink_urb_from_ep(hcd, urb);
Ajay Kumar Gupta2492e672008-09-11 11:53:21 +03002243 spin_unlock_irqrestore(&musb->lock, flags);
Felipe Balbi550a7372008-07-24 12:27:36 +03002244 return -ENOMEM;
2245 }
2246
2247 qh->hep = hep;
2248 qh->dev = urb->dev;
2249 INIT_LIST_HEAD(&qh->ring);
2250 qh->is_ready = 1;
2251
Kuninori Morimoto29cc8892011-08-23 03:12:03 -07002252 qh->maxpacket = usb_endpoint_maxp(epd);
Ajay Kumar Guptaa483d702009-04-03 16:16:17 -07002253 qh->type = usb_endpoint_type(epd);
Felipe Balbi550a7372008-07-24 12:27:36 +03002254
Ajay Kumar Guptaa483d702009-04-03 16:16:17 -07002255 /* Bits 11 & 12 of wMaxPacketSize encode high bandwidth multiplier.
2256 * Some musb cores don't support high bandwidth ISO transfers; and
2257 * we don't (yet!) support high bandwidth interrupt transfers.
2258 */
2259 qh->hb_mult = 1 + ((qh->maxpacket >> 11) & 0x03);
2260 if (qh->hb_mult > 1) {
2261 int ok = (qh->type == USB_ENDPOINT_XFER_ISOC);
2262
2263 if (ok)
2264 ok = (usb_pipein(urb->pipe) && musb->hb_iso_rx)
2265 || (usb_pipeout(urb->pipe) && musb->hb_iso_tx);
2266 if (!ok) {
2267 ret = -EMSGSIZE;
2268 goto done;
2269 }
2270 qh->maxpacket &= 0x7ff;
Felipe Balbi550a7372008-07-24 12:27:36 +03002271 }
2272
Julia Lawall96bcd092009-01-24 17:57:24 -08002273 qh->epnum = usb_endpoint_num(epd);
Felipe Balbi550a7372008-07-24 12:27:36 +03002274
2275 /* NOTE: urb->dev->devnum is wrong during SET_ADDRESS */
2276 qh->addr_reg = (u8) usb_pipedevice(urb->pipe);
2277
2278 /* precompute rxtype/txtype/type0 register */
2279 type_reg = (qh->type << 4) | qh->epnum;
2280 switch (urb->dev->speed) {
2281 case USB_SPEED_LOW:
2282 type_reg |= 0xc0;
2283 break;
2284 case USB_SPEED_FULL:
2285 type_reg |= 0x80;
2286 break;
2287 default:
2288 type_reg |= 0x40;
2289 }
2290 qh->type_reg = type_reg;
2291
Sergei Shtylyov136733d2009-02-21 15:31:35 -08002292 /* Precompute RXINTERVAL/TXINTERVAL register */
Felipe Balbi550a7372008-07-24 12:27:36 +03002293 switch (qh->type) {
2294 case USB_ENDPOINT_XFER_INT:
Sergei Shtylyov136733d2009-02-21 15:31:35 -08002295 /*
2296 * Full/low speeds use the linear encoding,
2297 * high speed uses the logarithmic encoding.
2298 */
2299 if (urb->dev->speed <= USB_SPEED_FULL) {
2300 interval = max_t(u8, epd->bInterval, 1);
2301 break;
Felipe Balbi550a7372008-07-24 12:27:36 +03002302 }
2303 /* FALLTHROUGH */
2304 case USB_ENDPOINT_XFER_ISOC:
Sergei Shtylyov136733d2009-02-21 15:31:35 -08002305 /* ISO always uses logarithmic encoding */
2306 interval = min_t(u8, epd->bInterval, 16);
Felipe Balbi550a7372008-07-24 12:27:36 +03002307 break;
2308 default:
2309 /* REVISIT we actually want to use NAK limits, hinting to the
2310 * transfer scheduling logic to try some other qh, e.g. try
2311 * for 2 msec first:
2312 *
2313 * interval = (USB_SPEED_HIGH == urb->dev->speed) ? 16 : 2;
2314 *
2315 * The downside of disabling this is that transfer scheduling
2316 * gets VERY unfair for nonperiodic transfers; a misbehaving
Ajay Kumar Gupta1e0320f2009-02-24 15:26:13 -08002317 * peripheral could make that hurt. That's perfectly normal
2318 * for reads from network or serial adapters ... so we have
2319 * partial NAKlimit support for bulk RX.
Felipe Balbi550a7372008-07-24 12:27:36 +03002320 *
Ajay Kumar Gupta1e0320f2009-02-24 15:26:13 -08002321 * The upside of disabling it is simpler transfer scheduling.
Felipe Balbi550a7372008-07-24 12:27:36 +03002322 */
2323 interval = 0;
2324 }
2325 qh->intv_reg = interval;
2326
2327 /* precompute addressing for external hub/tt ports */
2328 if (musb->is_multipoint) {
2329 struct usb_device *parent = urb->dev->parent;
2330
2331 if (parent != hcd->self.root_hub) {
2332 qh->h_addr_reg = (u8) parent->devnum;
2333
2334 /* set up tt info if needed */
2335 if (urb->dev->tt) {
2336 qh->h_port_reg = (u8) urb->dev->ttport;
Ajay Kumar Guptaae5ad292008-09-11 11:53:20 +03002337 if (urb->dev->tt->hub)
2338 qh->h_addr_reg =
2339 (u8) urb->dev->tt->hub->devnum;
2340 if (urb->dev->tt->multi)
2341 qh->h_addr_reg |= 0x80;
Felipe Balbi550a7372008-07-24 12:27:36 +03002342 }
2343 }
2344 }
2345
2346 /* invariant: hep->hcpriv is null OR the qh that's already scheduled.
2347 * until we get real dma queues (with an entry for each urb/buffer),
2348 * we only have work to do in the former case.
2349 */
2350 spin_lock_irqsave(&musb->lock, flags);
yuzheng ma30677792012-08-15 16:11:40 +08002351 if (hep->hcpriv || !next_urb(qh)) {
Felipe Balbi550a7372008-07-24 12:27:36 +03002352 /* some concurrent activity submitted another urb to hep...
2353 * odd, rare, error prone, but legal.
2354 */
2355 kfree(qh);
Dan Carpenter714bc5e2010-03-25 13:14:27 +02002356 qh = NULL;
Felipe Balbi550a7372008-07-24 12:27:36 +03002357 ret = 0;
2358 } else
2359 ret = musb_schedule(musb, qh,
2360 epd->bEndpointAddress & USB_ENDPOINT_DIR_MASK);
2361
2362 if (ret == 0) {
2363 urb->hcpriv = qh;
2364 /* FIXME set urb->start_frame for iso/intr, it's tested in
2365 * musb_start_urb(), but otherwise only konicawc cares ...
2366 */
2367 }
2368 spin_unlock_irqrestore(&musb->lock, flags);
2369
2370done:
2371 if (ret != 0) {
Ajay Kumar Gupta2492e672008-09-11 11:53:21 +03002372 spin_lock_irqsave(&musb->lock, flags);
Felipe Balbi550a7372008-07-24 12:27:36 +03002373 usb_hcd_unlink_urb_from_ep(hcd, urb);
Ajay Kumar Gupta2492e672008-09-11 11:53:21 +03002374 spin_unlock_irqrestore(&musb->lock, flags);
Felipe Balbi550a7372008-07-24 12:27:36 +03002375 kfree(qh);
2376 }
2377 return ret;
2378}
2379
2380
2381/*
2382 * abort a transfer that's at the head of a hardware queue.
2383 * called with controller locked, irqs blocked
2384 * that hardware queue advances to the next transfer, unless prevented
2385 */
Sergei Shtylyov81ec4e42009-03-27 12:57:50 -07002386static int musb_cleanup_urb(struct urb *urb, struct musb_qh *qh)
Felipe Balbi550a7372008-07-24 12:27:36 +03002387{
2388 struct musb_hw_ep *ep = qh->hw_ep;
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03002389 struct musb *musb = ep->musb;
Felipe Balbi550a7372008-07-24 12:27:36 +03002390 void __iomem *epio = ep->regs;
2391 unsigned hw_end = ep->epnum;
2392 void __iomem *regs = ep->musb->mregs;
Sergei Shtylyov81ec4e42009-03-27 12:57:50 -07002393 int is_in = usb_pipein(urb->pipe);
Felipe Balbi550a7372008-07-24 12:27:36 +03002394 int status = 0;
Sergei Shtylyov81ec4e42009-03-27 12:57:50 -07002395 u16 csr;
Felipe Balbi550a7372008-07-24 12:27:36 +03002396
2397 musb_ep_select(regs, hw_end);
2398
2399 if (is_dma_capable()) {
2400 struct dma_channel *dma;
2401
2402 dma = is_in ? ep->rx_channel : ep->tx_channel;
2403 if (dma) {
2404 status = ep->musb->dma_controller->channel_abort(dma);
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03002405 dev_dbg(musb->controller,
Felipe Balbi550a7372008-07-24 12:27:36 +03002406 "abort %cX%d DMA for urb %p --> %d\n",
2407 is_in ? 'R' : 'T', ep->epnum,
2408 urb, status);
2409 urb->actual_length += dma->actual_len;
2410 }
2411 }
2412
2413 /* turn off DMA requests, discard state, stop polling ... */
Ajay Kumar Gupta692933b2012-03-14 17:33:35 +05302414 if (ep->epnum && is_in) {
Felipe Balbi550a7372008-07-24 12:27:36 +03002415 /* giveback saves bulk toggle */
2416 csr = musb_h_flush_rxfifo(ep, 0);
2417
2418 /* REVISIT we still get an irq; should likely clear the
2419 * endpoint's irq status here to avoid bogus irqs.
2420 * clearing that status is platform-specific...
2421 */
David Brownell78322c12009-03-26 17:38:30 -07002422 } else if (ep->epnum) {
Felipe Balbi550a7372008-07-24 12:27:36 +03002423 musb_h_tx_flush_fifo(ep);
2424 csr = musb_readw(epio, MUSB_TXCSR);
2425 csr &= ~(MUSB_TXCSR_AUTOSET
2426 | MUSB_TXCSR_DMAENAB
2427 | MUSB_TXCSR_H_RXSTALL
2428 | MUSB_TXCSR_H_NAKTIMEOUT
2429 | MUSB_TXCSR_H_ERROR
2430 | MUSB_TXCSR_TXPKTRDY);
2431 musb_writew(epio, MUSB_TXCSR, csr);
2432 /* REVISIT may need to clear FLUSHFIFO ... */
2433 musb_writew(epio, MUSB_TXCSR, csr);
2434 /* flush cpu writebuffer */
2435 csr = musb_readw(epio, MUSB_TXCSR);
David Brownell78322c12009-03-26 17:38:30 -07002436 } else {
2437 musb_h_ep0_flush_fifo(ep);
Felipe Balbi550a7372008-07-24 12:27:36 +03002438 }
2439 if (status == 0)
2440 musb_advance_schedule(ep->musb, urb, ep, is_in);
2441 return status;
2442}
2443
2444static int musb_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
2445{
2446 struct musb *musb = hcd_to_musb(hcd);
2447 struct musb_qh *qh;
Felipe Balbi550a7372008-07-24 12:27:36 +03002448 unsigned long flags;
Sergei Shtylyov22a0d6f2009-03-27 12:56:26 -07002449 int is_in = usb_pipein(urb->pipe);
Felipe Balbi550a7372008-07-24 12:27:36 +03002450 int ret;
2451
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03002452 dev_dbg(musb->controller, "urb=%p, dev%d ep%d%s\n", urb,
Felipe Balbi550a7372008-07-24 12:27:36 +03002453 usb_pipedevice(urb->pipe),
2454 usb_pipeendpoint(urb->pipe),
Sergei Shtylyov22a0d6f2009-03-27 12:56:26 -07002455 is_in ? "in" : "out");
Felipe Balbi550a7372008-07-24 12:27:36 +03002456
2457 spin_lock_irqsave(&musb->lock, flags);
2458 ret = usb_hcd_check_unlink_urb(hcd, urb, status);
2459 if (ret)
2460 goto done;
2461
2462 qh = urb->hcpriv;
2463 if (!qh)
2464 goto done;
2465
Sergei Shtylyov22a0d6f2009-03-27 12:56:26 -07002466 /*
2467 * Any URB not actively programmed into endpoint hardware can be
Sergei Shtylyova2fd8142009-02-21 15:30:45 -08002468 * immediately given back; that's any URB not at the head of an
Felipe Balbi550a7372008-07-24 12:27:36 +03002469 * endpoint queue, unless someday we get real DMA queues. And even
Sergei Shtylyova2fd8142009-02-21 15:30:45 -08002470 * if it's at the head, it might not be known to the hardware...
Felipe Balbi550a7372008-07-24 12:27:36 +03002471 *
Sergei Shtylyov22a0d6f2009-03-27 12:56:26 -07002472 * Otherwise abort current transfer, pending DMA, etc.; urb->status
Felipe Balbi550a7372008-07-24 12:27:36 +03002473 * has already been updated. This is a synchronous abort; it'd be
2474 * OK to hold off until after some IRQ, though.
Sergei Shtylyov22a0d6f2009-03-27 12:56:26 -07002475 *
2476 * NOTE: qh is invalid unless !list_empty(&hep->urb_list)
Felipe Balbi550a7372008-07-24 12:27:36 +03002477 */
Sergei Shtylyov22a0d6f2009-03-27 12:56:26 -07002478 if (!qh->is_ready
2479 || urb->urb_list.prev != &qh->hep->urb_list
2480 || musb_ep_get_qh(qh->hw_ep, is_in) != qh) {
Felipe Balbi550a7372008-07-24 12:27:36 +03002481 int ready = qh->is_ready;
2482
Felipe Balbi550a7372008-07-24 12:27:36 +03002483 qh->is_ready = 0;
Sergei Shtylyovc9cd06b2009-03-27 12:58:31 -07002484 musb_giveback(musb, urb, 0);
Felipe Balbi550a7372008-07-24 12:27:36 +03002485 qh->is_ready = ready;
Sergei Shtylyova2fd8142009-02-21 15:30:45 -08002486
2487 /* If nothing else (usually musb_giveback) is using it
2488 * and its URB list has emptied, recycle this qh.
2489 */
2490 if (ready && list_empty(&qh->hep->urb_list)) {
2491 qh->hep->hcpriv = NULL;
2492 list_del(&qh->ring);
2493 kfree(qh);
2494 }
Felipe Balbi550a7372008-07-24 12:27:36 +03002495 } else
Sergei Shtylyov81ec4e42009-03-27 12:57:50 -07002496 ret = musb_cleanup_urb(urb, qh);
Felipe Balbi550a7372008-07-24 12:27:36 +03002497done:
2498 spin_unlock_irqrestore(&musb->lock, flags);
2499 return ret;
2500}
2501
2502/* disable an endpoint */
2503static void
2504musb_h_disable(struct usb_hcd *hcd, struct usb_host_endpoint *hep)
2505{
Sergei Shtylyov22a0d6f2009-03-27 12:56:26 -07002506 u8 is_in = hep->desc.bEndpointAddress & USB_DIR_IN;
Felipe Balbi550a7372008-07-24 12:27:36 +03002507 unsigned long flags;
2508 struct musb *musb = hcd_to_musb(hcd);
Sergei Shtylyovdc61d232009-02-21 15:31:01 -08002509 struct musb_qh *qh;
2510 struct urb *urb;
Felipe Balbi550a7372008-07-24 12:27:36 +03002511
Felipe Balbi550a7372008-07-24 12:27:36 +03002512 spin_lock_irqsave(&musb->lock, flags);
2513
Sergei Shtylyovdc61d232009-02-21 15:31:01 -08002514 qh = hep->hcpriv;
2515 if (qh == NULL)
2516 goto exit;
2517
Sergei Shtylyov22a0d6f2009-03-27 12:56:26 -07002518 /* NOTE: qh is invalid unless !list_empty(&hep->urb_list) */
Felipe Balbi550a7372008-07-24 12:27:36 +03002519
Sergei Shtylyov22a0d6f2009-03-27 12:56:26 -07002520 /* Kick the first URB off the hardware, if needed */
Felipe Balbi550a7372008-07-24 12:27:36 +03002521 qh->is_ready = 0;
Sergei Shtylyov22a0d6f2009-03-27 12:56:26 -07002522 if (musb_ep_get_qh(qh->hw_ep, is_in) == qh) {
Felipe Balbi550a7372008-07-24 12:27:36 +03002523 urb = next_urb(qh);
2524
2525 /* make software (then hardware) stop ASAP */
2526 if (!urb->unlinked)
2527 urb->status = -ESHUTDOWN;
2528
2529 /* cleanup */
Sergei Shtylyov81ec4e42009-03-27 12:57:50 -07002530 musb_cleanup_urb(urb, qh);
Felipe Balbi550a7372008-07-24 12:27:36 +03002531
Sergei Shtylyovdc61d232009-02-21 15:31:01 -08002532 /* Then nuke all the others ... and advance the
2533 * queue on hw_ep (e.g. bulk ring) when we're done.
2534 */
2535 while (!list_empty(&hep->urb_list)) {
2536 urb = next_urb(qh);
2537 urb->status = -ESHUTDOWN;
2538 musb_advance_schedule(musb, urb, qh->hw_ep, is_in);
2539 }
2540 } else {
2541 /* Just empty the queue; the hardware is busy with
2542 * other transfers, and since !qh->is_ready nothing
2543 * will activate any of these as it advances.
2544 */
2545 while (!list_empty(&hep->urb_list))
Sergei Shtylyovc9cd06b2009-03-27 12:58:31 -07002546 musb_giveback(musb, next_urb(qh), -ESHUTDOWN);
Felipe Balbi550a7372008-07-24 12:27:36 +03002547
Sergei Shtylyovdc61d232009-02-21 15:31:01 -08002548 hep->hcpriv = NULL;
2549 list_del(&qh->ring);
2550 kfree(qh);
2551 }
2552exit:
Felipe Balbi550a7372008-07-24 12:27:36 +03002553 spin_unlock_irqrestore(&musb->lock, flags);
2554}
2555
2556static int musb_h_get_frame_number(struct usb_hcd *hcd)
2557{
2558 struct musb *musb = hcd_to_musb(hcd);
2559
2560 return musb_readw(musb->mregs, MUSB_FRAME);
2561}
2562
2563static int musb_h_start(struct usb_hcd *hcd)
2564{
2565 struct musb *musb = hcd_to_musb(hcd);
2566
2567 /* NOTE: musb_start() is called when the hub driver turns
2568 * on port power, or when (OTG) peripheral starts.
2569 */
2570 hcd->state = HC_STATE_RUNNING;
2571 musb->port1_status = 0;
2572 return 0;
2573}
2574
2575static void musb_h_stop(struct usb_hcd *hcd)
2576{
2577 musb_stop(hcd_to_musb(hcd));
2578 hcd->state = HC_STATE_HALT;
2579}
2580
2581static int musb_bus_suspend(struct usb_hcd *hcd)
2582{
2583 struct musb *musb = hcd_to_musb(hcd);
David Brownell89368d32009-07-01 03:36:16 -07002584 u8 devctl;
Felipe Balbi550a7372008-07-24 12:27:36 +03002585
Daniel Mack94f72132013-11-25 22:26:41 +01002586 musb_port_suspend(musb, true);
2587
David Brownell89368d32009-07-01 03:36:16 -07002588 if (!is_host_active(musb))
Felipe Balbi550a7372008-07-24 12:27:36 +03002589 return 0;
2590
Antoine Tenarte47d9252014-10-30 18:41:13 +01002591 switch (musb->xceiv->otg->state) {
David Brownell89368d32009-07-01 03:36:16 -07002592 case OTG_STATE_A_SUSPEND:
2593 return 0;
2594 case OTG_STATE_A_WAIT_VRISE:
2595 /* ID could be grounded even if there's no device
2596 * on the other end of the cable. NOTE that the
2597 * A_WAIT_VRISE timers are messy with MUSB...
2598 */
2599 devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
2600 if ((devctl & MUSB_DEVCTL_VBUS) == MUSB_DEVCTL_VBUS)
Antoine Tenarte47d9252014-10-30 18:41:13 +01002601 musb->xceiv->otg->state = OTG_STATE_A_WAIT_BCON;
David Brownell89368d32009-07-01 03:36:16 -07002602 break;
2603 default:
2604 break;
2605 }
2606
2607 if (musb->is_active) {
2608 WARNING("trying to suspend as %s while active\n",
Antoine Tenarte47d9252014-10-30 18:41:13 +01002609 usb_otg_state_string(musb->xceiv->otg->state));
Felipe Balbi550a7372008-07-24 12:27:36 +03002610 return -EBUSY;
2611 } else
2612 return 0;
2613}
2614
2615static int musb_bus_resume(struct usb_hcd *hcd)
2616{
Daniel Mack869c5972013-11-26 13:31:14 +01002617 struct musb *musb = hcd_to_musb(hcd);
2618
2619 if (musb->config &&
2620 musb->config->host_port_deassert_reset_at_resume)
2621 musb_port_reset(musb, false);
2622
Felipe Balbi550a7372008-07-24 12:27:36 +03002623 return 0;
2624}
2625
Ruslan Bilovol8408fd12013-03-29 19:15:21 +02002626#ifndef CONFIG_MUSB_PIO_ONLY
2627
2628#define MUSB_USB_DMA_ALIGN 4
2629
2630struct musb_temp_buffer {
2631 void *kmalloc_ptr;
2632 void *old_xfer_buffer;
2633 u8 data[0];
2634};
2635
2636static void musb_free_temp_buffer(struct urb *urb)
2637{
2638 enum dma_data_direction dir;
2639 struct musb_temp_buffer *temp;
Johan Hovoldd72348f2015-04-23 16:06:50 +02002640 size_t length;
Ruslan Bilovol8408fd12013-03-29 19:15:21 +02002641
2642 if (!(urb->transfer_flags & URB_ALIGNED_TEMP_BUFFER))
2643 return;
2644
2645 dir = usb_urb_dir_in(urb) ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
2646
2647 temp = container_of(urb->transfer_buffer, struct musb_temp_buffer,
2648 data);
2649
2650 if (dir == DMA_FROM_DEVICE) {
Johan Hovoldd72348f2015-04-23 16:06:50 +02002651 if (usb_pipeisoc(urb->pipe))
2652 length = urb->transfer_buffer_length;
2653 else
2654 length = urb->actual_length;
2655
2656 memcpy(temp->old_xfer_buffer, temp->data, length);
Ruslan Bilovol8408fd12013-03-29 19:15:21 +02002657 }
2658 urb->transfer_buffer = temp->old_xfer_buffer;
2659 kfree(temp->kmalloc_ptr);
2660
2661 urb->transfer_flags &= ~URB_ALIGNED_TEMP_BUFFER;
2662}
2663
2664static int musb_alloc_temp_buffer(struct urb *urb, gfp_t mem_flags)
2665{
2666 enum dma_data_direction dir;
2667 struct musb_temp_buffer *temp;
2668 void *kmalloc_ptr;
2669 size_t kmalloc_size;
2670
2671 if (urb->num_sgs || urb->sg ||
2672 urb->transfer_buffer_length == 0 ||
2673 !((uintptr_t)urb->transfer_buffer & (MUSB_USB_DMA_ALIGN - 1)))
2674 return 0;
2675
2676 dir = usb_urb_dir_in(urb) ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
2677
2678 /* Allocate a buffer with enough padding for alignment */
2679 kmalloc_size = urb->transfer_buffer_length +
2680 sizeof(struct musb_temp_buffer) + MUSB_USB_DMA_ALIGN - 1;
2681
2682 kmalloc_ptr = kmalloc(kmalloc_size, mem_flags);
2683 if (!kmalloc_ptr)
2684 return -ENOMEM;
2685
2686 /* Position our struct temp_buffer such that data is aligned */
2687 temp = PTR_ALIGN(kmalloc_ptr, MUSB_USB_DMA_ALIGN);
2688
2689
2690 temp->kmalloc_ptr = kmalloc_ptr;
2691 temp->old_xfer_buffer = urb->transfer_buffer;
2692 if (dir == DMA_TO_DEVICE)
2693 memcpy(temp->data, urb->transfer_buffer,
2694 urb->transfer_buffer_length);
2695 urb->transfer_buffer = temp->data;
2696
2697 urb->transfer_flags |= URB_ALIGNED_TEMP_BUFFER;
2698
2699 return 0;
2700}
2701
2702static int musb_map_urb_for_dma(struct usb_hcd *hcd, struct urb *urb,
2703 gfp_t mem_flags)
2704{
2705 struct musb *musb = hcd_to_musb(hcd);
2706 int ret;
2707
2708 /*
2709 * The DMA engine in RTL1.8 and above cannot handle
2710 * DMA addresses that are not aligned to a 4 byte boundary.
2711 * For such engine implemented (un)map_urb_for_dma hooks.
2712 * Do not use these hooks for RTL<1.8
2713 */
2714 if (musb->hwvers < MUSB_HWVERS_1800)
2715 return usb_hcd_map_urb_for_dma(hcd, urb, mem_flags);
2716
2717 ret = musb_alloc_temp_buffer(urb, mem_flags);
2718 if (ret)
2719 return ret;
2720
2721 ret = usb_hcd_map_urb_for_dma(hcd, urb, mem_flags);
2722 if (ret)
2723 musb_free_temp_buffer(urb);
2724
2725 return ret;
2726}
2727
2728static void musb_unmap_urb_for_dma(struct usb_hcd *hcd, struct urb *urb)
2729{
2730 struct musb *musb = hcd_to_musb(hcd);
2731
2732 usb_hcd_unmap_urb_for_dma(hcd, urb);
2733
2734 /* Do not use this hook for RTL<1.8 (see description above) */
2735 if (musb->hwvers < MUSB_HWVERS_1800)
2736 return;
2737
2738 musb_free_temp_buffer(urb);
2739}
2740#endif /* !CONFIG_MUSB_PIO_ONLY */
2741
Daniel Mack74c2e932013-04-10 21:55:45 +02002742static const struct hc_driver musb_hc_driver = {
Felipe Balbi550a7372008-07-24 12:27:36 +03002743 .description = "musb-hcd",
2744 .product_desc = "MUSB HDRC host driver",
Daniel Mack74c2e932013-04-10 21:55:45 +02002745 .hcd_priv_size = sizeof(struct musb *),
Bin Liuf551e132016-04-25 15:53:30 -05002746 .flags = HCD_USB2 | HCD_MEMORY,
Felipe Balbi550a7372008-07-24 12:27:36 +03002747
2748 /* not using irq handler or reset hooks from usbcore, since
2749 * those must be shared with peripheral code for OTG configs
2750 */
2751
2752 .start = musb_h_start,
2753 .stop = musb_h_stop,
2754
2755 .get_frame_number = musb_h_get_frame_number,
2756
2757 .urb_enqueue = musb_urb_enqueue,
2758 .urb_dequeue = musb_urb_dequeue,
2759 .endpoint_disable = musb_h_disable,
2760
Ruslan Bilovol8408fd12013-03-29 19:15:21 +02002761#ifndef CONFIG_MUSB_PIO_ONLY
2762 .map_urb_for_dma = musb_map_urb_for_dma,
2763 .unmap_urb_for_dma = musb_unmap_urb_for_dma,
2764#endif
2765
Felipe Balbi550a7372008-07-24 12:27:36 +03002766 .hub_status_data = musb_hub_status_data,
2767 .hub_control = musb_hub_control,
2768 .bus_suspend = musb_bus_suspend,
2769 .bus_resume = musb_bus_resume,
2770 /* .start_port_reset = NULL, */
2771 /* .hub_irq_enable = NULL, */
2772};
Daniel Mack0b3eba42013-04-10 21:55:42 +02002773
Daniel Mack74c2e932013-04-10 21:55:45 +02002774int musb_host_alloc(struct musb *musb)
2775{
2776 struct device *dev = musb->controller;
2777
2778 /* usbcore sets dev->driver_data to hcd, and sometimes uses that... */
2779 musb->hcd = usb_create_hcd(&musb_hc_driver, dev, dev_name(dev));
2780 if (!musb->hcd)
2781 return -EINVAL;
2782
2783 *musb->hcd->hcd_priv = (unsigned long) musb;
2784 musb->hcd->self.uses_pio_for_control = 1;
2785 musb->hcd->uses_new_polling = 1;
2786 musb->hcd->has_tt = 1;
2787
2788 return 0;
2789}
2790
2791void musb_host_cleanup(struct musb *musb)
2792{
Sebastian Andrzej Siewior90474282013-08-20 18:35:44 +02002793 if (musb->port_mode == MUSB_PORT_MODE_GADGET)
2794 return;
Daniel Mack74c2e932013-04-10 21:55:45 +02002795 usb_remove_hcd(musb->hcd);
Daniel Mack74c2e932013-04-10 21:55:45 +02002796}
2797
2798void musb_host_free(struct musb *musb)
2799{
2800 usb_put_hcd(musb->hcd);
2801}
2802
Daniel Mack2cc65fe2013-04-10 21:55:47 +02002803int musb_host_setup(struct musb *musb, int power_budget)
2804{
2805 int ret;
2806 struct usb_hcd *hcd = musb->hcd;
2807
2808 MUSB_HST_MODE(musb);
2809 musb->xceiv->otg->default_a = 1;
Antoine Tenarte47d9252014-10-30 18:41:13 +01002810 musb->xceiv->otg->state = OTG_STATE_A_IDLE;
Daniel Mack2cc65fe2013-04-10 21:55:47 +02002811
2812 otg_set_host(musb->xceiv->otg, &hcd->self);
2813 hcd->self.otg_port = 1;
2814 musb->xceiv->otg->host = &hcd->self;
2815 hcd->power_budget = 2 * (power_budget ? : 250);
2816
2817 ret = usb_add_hcd(hcd, 0, 0);
2818 if (ret < 0)
2819 return ret;
2820
Peter Chen3c9740a2013-11-05 10:46:02 +08002821 device_wakeup_enable(hcd->self.controller);
Daniel Mack2cc65fe2013-04-10 21:55:47 +02002822 return 0;
2823}
2824
Daniel Mack0b3eba42013-04-10 21:55:42 +02002825void musb_host_resume_root_hub(struct musb *musb)
2826{
Daniel Mack74c2e932013-04-10 21:55:45 +02002827 usb_hcd_resume_root_hub(musb->hcd);
Daniel Mack0b3eba42013-04-10 21:55:42 +02002828}
2829
2830void musb_host_poke_root_hub(struct musb *musb)
2831{
2832 MUSB_HST_MODE(musb);
Daniel Mack74c2e932013-04-10 21:55:45 +02002833 if (musb->hcd->status_urb)
2834 usb_hcd_poll_rh_status(musb->hcd);
Daniel Mack0b3eba42013-04-10 21:55:42 +02002835 else
Daniel Mack74c2e932013-04-10 21:55:45 +02002836 usb_hcd_resume_root_hub(musb->hcd);
Daniel Mack0b3eba42013-04-10 21:55:42 +02002837}