blob: 43033895e8f632cf1d737270067e6f571570b65a [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"
Bin Liu19ca6822016-06-30 12:12:26 -050047#include "musb_trace.h"
Felipe Balbi550a7372008-07-24 12:27:36 +030048
Felipe Balbi550a7372008-07-24 12:27:36 +030049/* MUSB HOST status 22-mar-2006
50 *
51 * - There's still lots of partial code duplication for fault paths, so
52 * they aren't handled as consistently as they need to be.
53 *
54 * - PIO mostly behaved when last tested.
55 * + including ep0, with all usbtest cases 9, 10
56 * + usbtest 14 (ep0out) doesn't seem to run at all
57 * + double buffered OUT/TX endpoints saw stalls(!) with certain usbtest
58 * configurations, but otherwise double buffering passes basic tests.
59 * + for 2.6.N, for N > ~10, needs API changes for hcd framework.
60 *
61 * - DMA (CPPI) ... partially behaves, not currently recommended
62 * + about 1/15 the speed of typical EHCI implementations (PCI)
63 * + RX, all too often reqpkt seems to misbehave after tx
64 * + TX, no known issues (other than evident silicon issue)
65 *
66 * - DMA (Mentor/OMAP) ...has at least toggle update problems
67 *
Ajay Kumar Gupta1e0320f2009-02-24 15:26:13 -080068 * - [23-feb-2009] minimal traffic scheduling to avoid bulk RX packet
69 * starvation ... nothing yet for TX, interrupt, or bulk.
Felipe Balbi550a7372008-07-24 12:27:36 +030070 *
71 * - Not tested with HNP, but some SRP paths seem to behave.
72 *
73 * NOTE 24-August-2006:
74 *
75 * - Bulk traffic finally uses both sides of hardware ep1, freeing up an
76 * extra endpoint for periodic use enabling hub + keybd + mouse. That
77 * mostly works, except that with "usbnet" it's easy to trigger cases
78 * with "ping" where RX loses. (a) ping to davinci, even "ping -f",
79 * fine; but (b) ping _from_ davinci, even "ping -c 1", ICMP RX loses
80 * although ARP RX wins. (That test was done with a full speed link.)
81 */
82
83
84/*
85 * NOTE on endpoint usage:
86 *
87 * CONTROL transfers all go through ep0. BULK ones go through dedicated IN
88 * and OUT endpoints ... hardware is dedicated for those "async" queue(s).
Felipe Balbi550a7372008-07-24 12:27:36 +030089 * (Yes, bulk _could_ use more of the endpoints than that, and would even
Ajay Kumar Gupta1e0320f2009-02-24 15:26:13 -080090 * benefit from it.)
Felipe Balbi550a7372008-07-24 12:27:36 +030091 *
92 * INTERUPPT and ISOCHRONOUS transfers are scheduled to the other endpoints.
93 * So far that scheduling is both dumb and optimistic: the endpoint will be
94 * "claimed" until its software queue is no longer refilled. No multiplexing
95 * of transfers between endpoints, or anything clever.
96 */
97
Daniel Mack74c2e932013-04-10 21:55:45 +020098struct musb *hcd_to_musb(struct usb_hcd *hcd)
99{
100 return *(struct musb **) hcd->hcd_priv;
101}
102
Felipe Balbi550a7372008-07-24 12:27:36 +0300103
104static void musb_ep_program(struct musb *musb, u8 epnum,
Sergei Shtylyov6b6e9712009-03-26 18:29:19 -0700105 struct urb *urb, int is_out,
106 u8 *buf, u32 offset, u32 len);
Felipe Balbi550a7372008-07-24 12:27:36 +0300107
108/*
109 * Clear TX fifo. Needed to avoid BABBLE errors.
110 */
David Brownellc767c1c2008-09-11 11:53:23 +0300111static void musb_h_tx_flush_fifo(struct musb_hw_ep *ep)
Felipe Balbi550a7372008-07-24 12:27:36 +0300112{
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300113 struct musb *musb = ep->musb;
Felipe Balbi550a7372008-07-24 12:27:36 +0300114 void __iomem *epio = ep->regs;
115 u16 csr;
116 int retries = 1000;
117
118 csr = musb_readw(epio, MUSB_TXCSR);
119 while (csr & MUSB_TXCSR_FIFONOTEMPTY) {
Daniel Mack2ccc6d32014-05-26 14:52:37 +0200120 csr |= MUSB_TXCSR_FLUSHFIFO | MUSB_TXCSR_TXPKTRDY;
Felipe Balbi550a7372008-07-24 12:27:36 +0300121 musb_writew(epio, MUSB_TXCSR, csr);
122 csr = musb_readw(epio, MUSB_TXCSR);
Bin Liu68fe05e2015-11-06 12:08:56 -0600123
124 /*
125 * FIXME: sometimes the tx fifo flush failed, it has been
126 * observed during device disconnect on AM335x.
127 *
128 * To reproduce the issue, ensure tx urb(s) are queued when
129 * unplug the usb device which is connected to AM335x usb
130 * host port.
131 *
132 * I found using a usb-ethernet device and running iperf
133 * (client on AM335x) has very high chance to trigger it.
134 *
Bin Liub99d3652016-06-30 12:12:22 -0500135 * Better to turn on musb_dbg() in musb_cleanup_urb() with
Bin Liu68fe05e2015-11-06 12:08:56 -0600136 * CPPI enabled to see the issue when aborting the tx channel.
137 */
138 if (dev_WARN_ONCE(musb->controller, retries-- < 1,
David Brownellbb1c9ef2008-11-24 13:06:50 +0200139 "Could not flush host TX%d fifo: csr: %04x\n",
140 ep->epnum, csr))
Felipe Balbi550a7372008-07-24 12:27:36 +0300141 return;
Bin Liu821ccbe2017-07-25 09:31:34 -0500142 mdelay(1);
Felipe Balbi550a7372008-07-24 12:27:36 +0300143 }
144}
145
David Brownell78322c12009-03-26 17:38:30 -0700146static void musb_h_ep0_flush_fifo(struct musb_hw_ep *ep)
147{
148 void __iomem *epio = ep->regs;
149 u16 csr;
150 int retries = 5;
151
152 /* scrub any data left in the fifo */
153 do {
154 csr = musb_readw(epio, MUSB_TXCSR);
155 if (!(csr & (MUSB_CSR0_TXPKTRDY | MUSB_CSR0_RXPKTRDY)))
156 break;
157 musb_writew(epio, MUSB_TXCSR, MUSB_CSR0_FLUSHFIFO);
158 csr = musb_readw(epio, MUSB_TXCSR);
159 udelay(10);
160 } while (--retries);
161
162 WARN(!retries, "Could not flush host TX%d fifo: csr: %04x\n",
163 ep->epnum, csr);
164
165 /* and reset for the next transfer */
166 musb_writew(epio, MUSB_TXCSR, 0);
167}
168
Felipe Balbi550a7372008-07-24 12:27:36 +0300169/*
170 * Start transmit. Caller is responsible for locking shared resources.
171 * musb must be locked.
172 */
173static inline void musb_h_tx_start(struct musb_hw_ep *ep)
174{
175 u16 txcsr;
176
177 /* NOTE: no locks here; caller should lock and select EP */
178 if (ep->epnum) {
179 txcsr = musb_readw(ep->regs, MUSB_TXCSR);
180 txcsr |= MUSB_TXCSR_TXPKTRDY | MUSB_TXCSR_H_WZC_BITS;
181 musb_writew(ep->regs, MUSB_TXCSR, txcsr);
182 } else {
183 txcsr = MUSB_CSR0_H_SETUPPKT | MUSB_CSR0_TXPKTRDY;
184 musb_writew(ep->regs, MUSB_CSR0, txcsr);
185 }
186
187}
188
Sergei Shtylyovc7bbc052009-03-26 18:26:40 -0700189static inline void musb_h_tx_dma_start(struct musb_hw_ep *ep)
Felipe Balbi550a7372008-07-24 12:27:36 +0300190{
191 u16 txcsr;
192
193 /* NOTE: no locks here; caller should lock and select EP */
194 txcsr = musb_readw(ep->regs, MUSB_TXCSR);
195 txcsr |= MUSB_TXCSR_DMAENAB | MUSB_TXCSR_H_WZC_BITS;
Tony Lindgrenf8e9f34f2015-05-01 12:29:27 -0700196 if (is_cppi_enabled(ep->musb))
Sergei Shtylyovc7bbc052009-03-26 18:26:40 -0700197 txcsr |= MUSB_TXCSR_DMAMODE;
Felipe Balbi550a7372008-07-24 12:27:36 +0300198 musb_writew(ep->regs, MUSB_TXCSR, txcsr);
199}
200
Sergei Shtylyov3e5c6dc2009-03-27 12:55:16 -0700201static void musb_ep_set_qh(struct musb_hw_ep *ep, int is_in, struct musb_qh *qh)
202{
203 if (is_in != 0 || ep->is_shared_fifo)
204 ep->in_qh = qh;
205 if (is_in == 0 || ep->is_shared_fifo)
206 ep->out_qh = qh;
207}
208
209static struct musb_qh *musb_ep_get_qh(struct musb_hw_ep *ep, int is_in)
210{
211 return is_in ? ep->in_qh : ep->out_qh;
212}
213
Felipe Balbi550a7372008-07-24 12:27:36 +0300214/*
215 * Start the URB at the front of an endpoint's queue
216 * end must be claimed from the caller.
217 *
218 * Context: controller locked, irqs blocked
219 */
220static void
221musb_start_urb(struct musb *musb, int is_in, struct musb_qh *qh)
222{
223 u16 frame;
224 u32 len;
Felipe Balbi550a7372008-07-24 12:27:36 +0300225 void __iomem *mbase = musb->mregs;
226 struct urb *urb = next_urb(qh);
Sergei Shtylyov6b6e9712009-03-26 18:29:19 -0700227 void *buf = urb->transfer_buffer;
228 u32 offset = 0;
Felipe Balbi550a7372008-07-24 12:27:36 +0300229 struct musb_hw_ep *hw_ep = qh->hw_ep;
Felipe Balbi550a7372008-07-24 12:27:36 +0300230 int epnum = hw_ep->epnum;
231
232 /* initialize software qh state */
233 qh->offset = 0;
234 qh->segsize = 0;
235
236 /* gather right source of data */
237 switch (qh->type) {
238 case USB_ENDPOINT_XFER_CONTROL:
239 /* control transfers always start with SETUP */
240 is_in = 0;
Felipe Balbi550a7372008-07-24 12:27:36 +0300241 musb->ep0_stage = MUSB_EP0_START;
242 buf = urb->setup_packet;
243 len = 8;
244 break;
245 case USB_ENDPOINT_XFER_ISOC:
246 qh->iso_idx = 0;
247 qh->frame = 0;
Sergei Shtylyov6b6e9712009-03-26 18:29:19 -0700248 offset = urb->iso_frame_desc[0].offset;
Felipe Balbi550a7372008-07-24 12:27:36 +0300249 len = urb->iso_frame_desc[0].length;
250 break;
251 default: /* bulk, interrupt */
Ajay Kumar Gupta1e0320f2009-02-24 15:26:13 -0800252 /* actual_length may be nonzero on retry paths */
253 buf = urb->transfer_buffer + urb->actual_length;
254 len = urb->transfer_buffer_length - urb->actual_length;
Felipe Balbi550a7372008-07-24 12:27:36 +0300255 }
256
Bin Liu19ca6822016-06-30 12:12:26 -0500257 trace_musb_urb_start(musb, urb);
Felipe Balbi550a7372008-07-24 12:27:36 +0300258
259 /* Configure endpoint */
Sergei Shtylyov3e5c6dc2009-03-27 12:55:16 -0700260 musb_ep_set_qh(hw_ep, is_in, qh);
Sergei Shtylyov6b6e9712009-03-26 18:29:19 -0700261 musb_ep_program(musb, epnum, urb, !is_in, buf, offset, len);
Felipe Balbi550a7372008-07-24 12:27:36 +0300262
263 /* transmit may have more work: start it when it is time */
264 if (is_in)
265 return;
266
267 /* determine if the time is right for a periodic transfer */
268 switch (qh->type) {
269 case USB_ENDPOINT_XFER_ISOC:
270 case USB_ENDPOINT_XFER_INT:
Bin Liub99d3652016-06-30 12:12:22 -0500271 musb_dbg(musb, "check whether there's still time for periodic Tx");
Felipe Balbi550a7372008-07-24 12:27:36 +0300272 frame = musb_readw(mbase, MUSB_FRAME);
273 /* FIXME this doesn't implement that scheduling policy ...
274 * or handle framecounter wrapping
275 */
Alan Stern8a1ea512013-05-29 13:21:01 -0400276 if (1) { /* Always assume URB_ISO_ASAP */
Felipe Balbi550a7372008-07-24 12:27:36 +0300277 /* REVISIT the SOF irq handler shouldn't duplicate
278 * this code; and we don't init urb->start_frame...
279 */
280 qh->frame = 0;
281 goto start;
282 } else {
283 qh->frame = urb->start_frame;
284 /* enable SOF interrupt so we can count down */
Bin Liub99d3652016-06-30 12:12:22 -0500285 musb_dbg(musb, "SOF for %d", epnum);
Felipe Balbi550a7372008-07-24 12:27:36 +0300286#if 1 /* ifndef CONFIG_ARCH_DAVINCI */
287 musb_writeb(mbase, MUSB_INTRUSBE, 0xff);
288#endif
289 }
290 break;
291 default:
292start:
Bin Liub99d3652016-06-30 12:12:22 -0500293 musb_dbg(musb, "Start TX%d %s", epnum,
Felipe Balbi550a7372008-07-24 12:27:36 +0300294 hw_ep->tx_channel ? "dma" : "pio");
295
296 if (!hw_ep->tx_channel)
297 musb_h_tx_start(hw_ep);
Tony Lindgrenf8e9f34f2015-05-01 12:29:27 -0700298 else if (is_cppi_enabled(musb) || tusb_dma_omap(musb))
Sergei Shtylyovc7bbc052009-03-26 18:26:40 -0700299 musb_h_tx_dma_start(hw_ep);
Felipe Balbi550a7372008-07-24 12:27:36 +0300300 }
301}
302
Sergei Shtylyovc9cd06b2009-03-27 12:58:31 -0700303/* Context: caller owns controller lock, IRQs are blocked */
304static void musb_giveback(struct musb *musb, struct urb *urb, int status)
Felipe Balbi550a7372008-07-24 12:27:36 +0300305__releases(musb->lock)
306__acquires(musb->lock)
307{
Bin Liu19ca6822016-06-30 12:12:26 -0500308 trace_musb_urb_gb(musb, urb);
Felipe Balbi550a7372008-07-24 12:27:36 +0300309
Daniel Mack8b125df2013-04-10 21:55:50 +0200310 usb_hcd_unlink_urb_from_ep(musb->hcd, urb);
Felipe Balbi550a7372008-07-24 12:27:36 +0300311 spin_unlock(&musb->lock);
Daniel Mack8b125df2013-04-10 21:55:50 +0200312 usb_hcd_giveback_urb(musb->hcd, urb, status);
Felipe Balbi550a7372008-07-24 12:27:36 +0300313 spin_lock(&musb->lock);
314}
315
Sergei Shtylyov846099a2009-03-27 12:54:21 -0700316/* For bulk/interrupt endpoints only */
317static inline void musb_save_toggle(struct musb_qh *qh, int is_in,
318 struct urb *urb)
Felipe Balbi550a7372008-07-24 12:27:36 +0300319{
Sergei Shtylyov846099a2009-03-27 12:54:21 -0700320 void __iomem *epio = qh->hw_ep->regs;
Felipe Balbi550a7372008-07-24 12:27:36 +0300321 u16 csr;
Felipe Balbi550a7372008-07-24 12:27:36 +0300322
Sergei Shtylyov846099a2009-03-27 12:54:21 -0700323 /*
324 * FIXME: the current Mentor DMA code seems to have
Felipe Balbi550a7372008-07-24 12:27:36 +0300325 * problems getting toggle correct.
326 */
327
Sergei Shtylyov846099a2009-03-27 12:54:21 -0700328 if (is_in)
329 csr = musb_readw(epio, MUSB_RXCSR) & MUSB_RXCSR_H_DATATOGGLE;
Felipe Balbi550a7372008-07-24 12:27:36 +0300330 else
Sergei Shtylyov846099a2009-03-27 12:54:21 -0700331 csr = musb_readw(epio, MUSB_TXCSR) & MUSB_TXCSR_H_DATATOGGLE;
Felipe Balbi550a7372008-07-24 12:27:36 +0300332
Sergei Shtylyov846099a2009-03-27 12:54:21 -0700333 usb_settoggle(urb->dev, qh->epnum, !is_in, csr ? 1 : 0);
Felipe Balbi550a7372008-07-24 12:27:36 +0300334}
335
Sergei Shtylyovc9cd06b2009-03-27 12:58:31 -0700336/*
337 * Advance this hardware endpoint's queue, completing the specified URB and
338 * advancing to either the next URB queued to that qh, or else invalidating
339 * that qh and advancing to the next qh scheduled after the current one.
340 *
341 * Context: caller owns controller lock, IRQs are blocked
342 */
343static void musb_advance_schedule(struct musb *musb, struct urb *urb,
344 struct musb_hw_ep *hw_ep, int is_in)
Felipe Balbi550a7372008-07-24 12:27:36 +0300345{
Sergei Shtylyovc9cd06b2009-03-27 12:58:31 -0700346 struct musb_qh *qh = musb_ep_get_qh(hw_ep, is_in);
Felipe Balbi550a7372008-07-24 12:27:36 +0300347 struct musb_hw_ep *ep = qh->hw_ep;
Felipe Balbi550a7372008-07-24 12:27:36 +0300348 int ready = qh->is_ready;
Sergei Shtylyovc9cd06b2009-03-27 12:58:31 -0700349 int status;
350
351 status = (urb->status == -EINPROGRESS) ? 0 : urb->status;
Felipe Balbi550a7372008-07-24 12:27:36 +0300352
Felipe Balbi550a7372008-07-24 12:27:36 +0300353 /* save toggle eagerly, for paranoia */
354 switch (qh->type) {
355 case USB_ENDPOINT_XFER_BULK:
356 case USB_ENDPOINT_XFER_INT:
Sergei Shtylyov846099a2009-03-27 12:54:21 -0700357 musb_save_toggle(qh, is_in, urb);
Felipe Balbi550a7372008-07-24 12:27:36 +0300358 break;
359 case USB_ENDPOINT_XFER_ISOC:
Sergei Shtylyov1fe975f2009-07-10 20:02:44 +0300360 if (status == 0 && urb->error_count)
Felipe Balbi550a7372008-07-24 12:27:36 +0300361 status = -EXDEV;
362 break;
363 }
364
Felipe Balbi550a7372008-07-24 12:27:36 +0300365 qh->is_ready = 0;
Sergei Shtylyovc9cd06b2009-03-27 12:58:31 -0700366 musb_giveback(musb, urb, status);
Felipe Balbi550a7372008-07-24 12:27:36 +0300367 qh->is_ready = ready;
368
369 /* reclaim resources (and bandwidth) ASAP; deschedule it, and
370 * invalidate qh as soon as list_empty(&hep->urb_list)
371 */
372 if (list_empty(&qh->hep->urb_list)) {
373 struct list_head *head;
Ajay Kumar Gupta8c778db2012-06-21 17:18:12 +0530374 struct dma_controller *dma = musb->dma_controller;
Felipe Balbi550a7372008-07-24 12:27:36 +0300375
Ajay Kumar Gupta8c778db2012-06-21 17:18:12 +0530376 if (is_in) {
Felipe Balbi550a7372008-07-24 12:27:36 +0300377 ep->rx_reinit = 1;
Ajay Kumar Gupta8c778db2012-06-21 17:18:12 +0530378 if (ep->rx_channel) {
379 dma->channel_release(ep->rx_channel);
380 ep->rx_channel = NULL;
381 }
382 } else {
Felipe Balbi550a7372008-07-24 12:27:36 +0300383 ep->tx_reinit = 1;
Ajay Kumar Gupta8c778db2012-06-21 17:18:12 +0530384 if (ep->tx_channel) {
385 dma->channel_release(ep->tx_channel);
386 ep->tx_channel = NULL;
387 }
388 }
Felipe Balbi550a7372008-07-24 12:27:36 +0300389
Sergei Shtylyov3e5c6dc2009-03-27 12:55:16 -0700390 /* Clobber old pointers to this qh */
391 musb_ep_set_qh(ep, is_in, NULL);
Felipe Balbi550a7372008-07-24 12:27:36 +0300392 qh->hep->hcpriv = NULL;
393
394 switch (qh->type) {
395
Ajay Kumar Gupta23d15e02008-10-29 15:10:35 +0200396 case USB_ENDPOINT_XFER_CONTROL:
397 case USB_ENDPOINT_XFER_BULK:
398 /* fifo policy for these lists, except that NAKing
399 * should rotate a qh to the end (for fairness).
400 */
401 if (qh->mux == 1) {
402 head = qh->ring.prev;
403 list_del(&qh->ring);
404 kfree(qh);
405 qh = first_qh(head);
406 break;
407 }
408
Felipe Balbi550a7372008-07-24 12:27:36 +0300409 case USB_ENDPOINT_XFER_ISOC:
410 case USB_ENDPOINT_XFER_INT:
411 /* this is where periodic bandwidth should be
412 * de-allocated if it's tracked and allocated;
413 * and where we'd update the schedule tree...
414 */
Felipe Balbi550a7372008-07-24 12:27:36 +0300415 kfree(qh);
416 qh = NULL;
417 break;
Felipe Balbi550a7372008-07-24 12:27:36 +0300418 }
419 }
Felipe Balbi550a7372008-07-24 12:27:36 +0300420
Bin Liufe80d732018-02-20 07:31:35 -0600421 if (qh != NULL && qh->is_ready) {
Bin Liub99d3652016-06-30 12:12:22 -0500422 musb_dbg(musb, "... next ep%d %cX urb %p",
Sergei Shtylyovc9cd06b2009-03-27 12:58:31 -0700423 hw_ep->epnum, is_in ? 'R' : 'T', next_urb(qh));
Felipe Balbi550a7372008-07-24 12:27:36 +0300424 musb_start_urb(musb, is_in, qh);
425 }
426}
427
David Brownellc767c1c2008-09-11 11:53:23 +0300428static u16 musb_h_flush_rxfifo(struct musb_hw_ep *hw_ep, u16 csr)
Felipe Balbi550a7372008-07-24 12:27:36 +0300429{
430 /* we don't want fifo to fill itself again;
431 * ignore dma (various models),
432 * leave toggle alone (may not have been saved yet)
433 */
434 csr |= MUSB_RXCSR_FLUSHFIFO | MUSB_RXCSR_RXPKTRDY;
435 csr &= ~(MUSB_RXCSR_H_REQPKT
436 | MUSB_RXCSR_H_AUTOREQ
437 | MUSB_RXCSR_AUTOCLEAR);
438
439 /* write 2x to allow double buffering */
440 musb_writew(hw_ep->regs, MUSB_RXCSR, csr);
441 musb_writew(hw_ep->regs, MUSB_RXCSR, csr);
442
443 /* flush writebuffer */
444 return musb_readw(hw_ep->regs, MUSB_RXCSR);
445}
446
447/*
448 * PIO RX for a packet (or part of it).
449 */
450static bool
451musb_host_packet_rx(struct musb *musb, struct urb *urb, u8 epnum, u8 iso_err)
452{
453 u16 rx_count;
454 u8 *buf;
455 u16 csr;
456 bool done = false;
457 u32 length;
458 int do_flush = 0;
459 struct musb_hw_ep *hw_ep = musb->endpoints + epnum;
460 void __iomem *epio = hw_ep->regs;
461 struct musb_qh *qh = hw_ep->in_qh;
462 int pipe = urb->pipe;
463 void *buffer = urb->transfer_buffer;
464
465 /* musb_ep_select(mbase, epnum); */
466 rx_count = musb_readw(epio, MUSB_RXCOUNT);
Bin Liub99d3652016-06-30 12:12:22 -0500467 musb_dbg(musb, "RX%d count %d, buffer %p len %d/%d", epnum, rx_count,
Felipe Balbi550a7372008-07-24 12:27:36 +0300468 urb->transfer_buffer, qh->offset,
469 urb->transfer_buffer_length);
470
471 /* unload FIFO */
472 if (usb_pipeisoc(pipe)) {
473 int status = 0;
474 struct usb_iso_packet_descriptor *d;
475
476 if (iso_err) {
477 status = -EILSEQ;
478 urb->error_count++;
479 }
480
481 d = urb->iso_frame_desc + qh->iso_idx;
482 buf = buffer + d->offset;
483 length = d->length;
484 if (rx_count > length) {
485 if (status == 0) {
486 status = -EOVERFLOW;
487 urb->error_count++;
488 }
Bin Liub99d3652016-06-30 12:12:22 -0500489 musb_dbg(musb, "OVERFLOW %d into %d", rx_count, length);
Felipe Balbi550a7372008-07-24 12:27:36 +0300490 do_flush = 1;
491 } else
492 length = rx_count;
493 urb->actual_length += length;
494 d->actual_length = length;
495
496 d->status = status;
497
498 /* see if we are done */
499 done = (++qh->iso_idx >= urb->number_of_packets);
500 } else {
501 /* non-isoch */
502 buf = buffer + qh->offset;
503 length = urb->transfer_buffer_length - qh->offset;
504 if (rx_count > length) {
505 if (urb->status == -EINPROGRESS)
506 urb->status = -EOVERFLOW;
Bin Liub99d3652016-06-30 12:12:22 -0500507 musb_dbg(musb, "OVERFLOW %d into %d", rx_count, length);
Felipe Balbi550a7372008-07-24 12:27:36 +0300508 do_flush = 1;
509 } else
510 length = rx_count;
511 urb->actual_length += length;
512 qh->offset += length;
513
514 /* see if we are done */
515 done = (urb->actual_length == urb->transfer_buffer_length)
516 || (rx_count < qh->maxpacket)
517 || (urb->status != -EINPROGRESS);
518 if (done
519 && (urb->status == -EINPROGRESS)
520 && (urb->transfer_flags & URB_SHORT_NOT_OK)
521 && (urb->actual_length
522 < urb->transfer_buffer_length))
523 urb->status = -EREMOTEIO;
524 }
525
526 musb_read_fifo(hw_ep, length, buf);
527
528 csr = musb_readw(epio, MUSB_RXCSR);
529 csr |= MUSB_RXCSR_H_WZC_BITS;
530 if (unlikely(do_flush))
531 musb_h_flush_rxfifo(hw_ep, csr);
532 else {
533 /* REVISIT this assumes AUTOCLEAR is never set */
534 csr &= ~(MUSB_RXCSR_RXPKTRDY | MUSB_RXCSR_H_REQPKT);
535 if (!done)
536 csr |= MUSB_RXCSR_H_REQPKT;
537 musb_writew(epio, MUSB_RXCSR, csr);
538 }
539
540 return done;
541}
542
543/* we don't always need to reinit a given side of an endpoint...
544 * when we do, use tx/rx reinit routine and then construct a new CSR
545 * to address data toggle, NYET, and DMA or PIO.
546 *
547 * it's possible that driver bugs (especially for DMA) or aborting a
548 * transfer might have left the endpoint busier than it should be.
549 * the busy/not-empty tests are basically paranoia.
550 */
551static void
Hans de Goede0cb74b32015-03-20 20:11:11 +0100552musb_rx_reinit(struct musb *musb, struct musb_qh *qh, u8 epnum)
Felipe Balbi550a7372008-07-24 12:27:36 +0300553{
Hans de Goede0cb74b32015-03-20 20:11:11 +0100554 struct musb_hw_ep *ep = musb->endpoints + epnum;
Felipe Balbi550a7372008-07-24 12:27:36 +0300555 u16 csr;
556
557 /* NOTE: we know the "rx" fifo reinit never triggers for ep0.
558 * That always uses tx_reinit since ep0 repurposes TX register
559 * offsets; the initial SETUP packet is also a kind of OUT.
560 */
561
562 /* if programmed for Tx, put it in RX mode */
563 if (ep->is_shared_fifo) {
564 csr = musb_readw(ep->regs, MUSB_TXCSR);
565 if (csr & MUSB_TXCSR_MODE) {
566 musb_h_tx_flush_fifo(ep);
Sergei Shtylyovb6e434a2009-03-26 18:27:47 -0700567 csr = musb_readw(ep->regs, MUSB_TXCSR);
Felipe Balbi550a7372008-07-24 12:27:36 +0300568 musb_writew(ep->regs, MUSB_TXCSR,
Sergei Shtylyovb6e434a2009-03-26 18:27:47 -0700569 csr | MUSB_TXCSR_FRCDATATOG);
Felipe Balbi550a7372008-07-24 12:27:36 +0300570 }
Sergei Shtylyovb6e434a2009-03-26 18:27:47 -0700571
572 /*
573 * Clear the MODE bit (and everything else) to enable Rx.
574 * NOTE: we mustn't clear the DMAMODE bit before DMAENAB.
575 */
576 if (csr & MUSB_TXCSR_DMAMODE)
577 musb_writew(ep->regs, MUSB_TXCSR, MUSB_TXCSR_DMAMODE);
Felipe Balbi550a7372008-07-24 12:27:36 +0300578 musb_writew(ep->regs, MUSB_TXCSR, 0);
579
580 /* scrub all previous state, clearing toggle */
Felipe Balbi550a7372008-07-24 12:27:36 +0300581 }
Andrew Goodbodyf3eec0cf2016-05-31 10:05:26 -0500582 csr = musb_readw(ep->regs, MUSB_RXCSR);
583 if (csr & MUSB_RXCSR_RXPKTRDY)
584 WARNING("rx%d, packet/%d ready?\n", ep->epnum,
585 musb_readw(ep->regs, MUSB_RXCOUNT));
586
587 musb_h_flush_rxfifo(ep, MUSB_RXCSR_CLRDATATOG);
Felipe Balbi550a7372008-07-24 12:27:36 +0300588
589 /* target addr and (for multipoint) hub addr/port */
590 if (musb->is_multipoint) {
Hans de Goede6cc2af62015-03-20 20:11:12 +0100591 musb_write_rxfunaddr(musb, epnum, qh->addr_reg);
592 musb_write_rxhubaddr(musb, epnum, qh->h_addr_reg);
593 musb_write_rxhubport(musb, epnum, qh->h_port_reg);
Felipe Balbi550a7372008-07-24 12:27:36 +0300594 } else
595 musb_writeb(musb->mregs, MUSB_FADDR, qh->addr_reg);
596
597 /* protocol/endpoint, interval/NAKlimit, i/o size */
598 musb_writeb(ep->regs, MUSB_RXTYPE, qh->type_reg);
599 musb_writeb(ep->regs, MUSB_RXINTERVAL, qh->intv_reg);
600 /* NOTE: bulk combining rewrites high bits of maxpacket */
Cliff Cai9f445cb2010-01-28 20:44:18 -0500601 /* Set RXMAXP with the FIFO size of the endpoint
602 * to disable double buffer mode.
603 */
Felipe Balbi06624812011-01-21 13:39:20 +0800604 if (musb->double_buffer_not_ok)
Cliff Cai9f445cb2010-01-28 20:44:18 -0500605 musb_writew(ep->regs, MUSB_RXMAXP, ep->max_packet_sz_rx);
606 else
607 musb_writew(ep->regs, MUSB_RXMAXP,
608 qh->maxpacket | ((qh->hb_mult - 1) << 11));
Felipe Balbi550a7372008-07-24 12:27:36 +0300609
610 ep->rx_reinit = 0;
611}
612
Sergei Shtylyovb6a66312016-05-31 10:05:06 -0500613static void musb_tx_dma_set_mode_mentor(struct dma_controller *dma,
Sergei Shtylyov6b6e9712009-03-26 18:29:19 -0700614 struct musb_hw_ep *hw_ep, struct musb_qh *qh,
Tony Lindgren754fe4a2015-05-01 12:29:32 -0700615 struct urb *urb, u32 offset,
616 u32 *length, u8 *mode)
Sergei Shtylyov6b6e9712009-03-26 18:29:19 -0700617{
618 struct dma_channel *channel = hw_ep->tx_channel;
619 void __iomem *epio = hw_ep->regs;
620 u16 pkt_size = qh->maxpacket;
621 u16 csr;
Sergei Shtylyov6b6e9712009-03-26 18:29:19 -0700622
Tony Lindgren754fe4a2015-05-01 12:29:32 -0700623 if (*length > channel->max_len)
624 *length = channel->max_len;
Sergei Shtylyov6b6e9712009-03-26 18:29:19 -0700625
626 csr = musb_readw(epio, MUSB_TXCSR);
Tony Lindgren754fe4a2015-05-01 12:29:32 -0700627 if (*length > pkt_size) {
628 *mode = 1;
Ajay Kumar Guptaa483d702009-04-03 16:16:17 -0700629 csr |= MUSB_TXCSR_DMAMODE | MUSB_TXCSR_DMAENAB;
630 /* autoset shouldn't be set in high bandwidth */
supriya karanthf2786282012-12-06 11:16:23 +0530631 /*
632 * Enable Autoset according to table
633 * below
634 * bulk_split hb_mult Autoset_Enable
635 * 0 1 Yes(Normal)
636 * 0 >1 No(High BW ISO)
637 * 1 1 Yes(HS bulk)
638 * 1 >1 Yes(FS bulk)
639 */
640 if (qh->hb_mult == 1 || (qh->hb_mult > 1 &&
641 can_bulk_split(hw_ep->musb, qh->type)))
Ajay Kumar Guptaa483d702009-04-03 16:16:17 -0700642 csr |= MUSB_TXCSR_AUTOSET;
Sergei Shtylyov6b6e9712009-03-26 18:29:19 -0700643 } else {
Tony Lindgren754fe4a2015-05-01 12:29:32 -0700644 *mode = 0;
Sergei Shtylyov6b6e9712009-03-26 18:29:19 -0700645 csr &= ~(MUSB_TXCSR_AUTOSET | MUSB_TXCSR_DMAMODE);
646 csr |= MUSB_TXCSR_DMAENAB; /* against programmer's guide */
647 }
Cristian Birsanbba40e62016-02-11 08:58:17 -0700648 channel->desired_mode = *mode;
Sergei Shtylyov6b6e9712009-03-26 18:29:19 -0700649 musb_writew(epio, MUSB_TXCSR, csr);
Tony Lindgren754fe4a2015-05-01 12:29:32 -0700650}
651
Sergei Shtylyovb6a66312016-05-31 10:05:06 -0500652static void musb_tx_dma_set_mode_cppi_tusb(struct dma_controller *dma,
653 struct musb_hw_ep *hw_ep,
654 struct musb_qh *qh,
655 struct urb *urb,
656 u32 offset,
657 u32 *length,
658 u8 *mode)
Tony Lindgren754fe4a2015-05-01 12:29:32 -0700659{
660 struct dma_channel *channel = hw_ep->tx_channel;
661
Sergei Shtylyov6b6e9712009-03-26 18:29:19 -0700662 channel->actual_len = 0;
663
664 /*
665 * TX uses "RNDIS" mode automatically but needs help
666 * to identify the zero-length-final-packet case.
667 */
Tony Lindgren754fe4a2015-05-01 12:29:32 -0700668 *mode = (urb->transfer_flags & URB_ZERO_PACKET) ? 1 : 0;
Tony Lindgren754fe4a2015-05-01 12:29:32 -0700669}
670
671static bool musb_tx_dma_program(struct dma_controller *dma,
672 struct musb_hw_ep *hw_ep, struct musb_qh *qh,
673 struct urb *urb, u32 offset, u32 length)
674{
675 struct dma_channel *channel = hw_ep->tx_channel;
676 u16 pkt_size = qh->maxpacket;
677 u8 mode;
Tony Lindgren754fe4a2015-05-01 12:29:32 -0700678
679 if (musb_dma_inventra(hw_ep->musb) || musb_dma_ux500(hw_ep->musb))
Sergei Shtylyovb6a66312016-05-31 10:05:06 -0500680 musb_tx_dma_set_mode_mentor(dma, hw_ep, qh, urb, offset,
681 &length, &mode);
Sergei Shtylyov858b9be2016-05-31 10:05:05 -0500682 else if (is_cppi_enabled(hw_ep->musb) || tusb_dma_omap(hw_ep->musb))
Sergei Shtylyovb6a66312016-05-31 10:05:06 -0500683 musb_tx_dma_set_mode_cppi_tusb(dma, hw_ep, qh, urb, offset,
684 &length, &mode);
Sergei Shtylyov858b9be2016-05-31 10:05:05 -0500685 else
686 return false;
Sergei Shtylyov6b6e9712009-03-26 18:29:19 -0700687
688 qh->segsize = length;
689
Santosh Shilimkar4c647332010-09-20 10:32:07 +0300690 /*
691 * Ensure the data reaches to main memory before starting
692 * DMA transfer
693 */
694 wmb();
695
Sergei Shtylyov6b6e9712009-03-26 18:29:19 -0700696 if (!dma->channel_program(channel, pkt_size, mode,
697 urb->transfer_dma + offset, length)) {
Tony Lindgren754fe4a2015-05-01 12:29:32 -0700698 void __iomem *epio = hw_ep->regs;
699 u16 csr;
700
Sergei Shtylyov6b6e9712009-03-26 18:29:19 -0700701 dma->channel_release(channel);
702 hw_ep->tx_channel = NULL;
703
704 csr = musb_readw(epio, MUSB_TXCSR);
705 csr &= ~(MUSB_TXCSR_AUTOSET | MUSB_TXCSR_DMAENAB);
706 musb_writew(epio, MUSB_TXCSR, csr | MUSB_TXCSR_H_WZC_BITS);
707 return false;
708 }
709 return true;
710}
Felipe Balbi550a7372008-07-24 12:27:36 +0300711
712/*
713 * Program an HDRC endpoint as per the given URB
714 * Context: irqs blocked, controller lock held
715 */
716static void musb_ep_program(struct musb *musb, u8 epnum,
Sergei Shtylyov6b6e9712009-03-26 18:29:19 -0700717 struct urb *urb, int is_out,
718 u8 *buf, u32 offset, u32 len)
Felipe Balbi550a7372008-07-24 12:27:36 +0300719{
720 struct dma_controller *dma_controller;
721 struct dma_channel *dma_channel;
722 u8 dma_ok;
723 void __iomem *mbase = musb->mregs;
724 struct musb_hw_ep *hw_ep = musb->endpoints + epnum;
725 void __iomem *epio = hw_ep->regs;
Sergei Shtylyov3e5c6dc2009-03-27 12:55:16 -0700726 struct musb_qh *qh = musb_ep_get_qh(hw_ep, !is_out);
727 u16 packet_sz = qh->maxpacket;
Ajay Kumar Gupta31321222012-07-20 11:07:22 +0530728 u8 use_dma = 1;
729 u16 csr;
Felipe Balbi550a7372008-07-24 12:27:36 +0300730
Bin Liub99d3652016-06-30 12:12:22 -0500731 musb_dbg(musb, "%s hw%d urb %p spd%d dev%d ep%d%s "
732 "h_addr%02x h_port%02x bytes %d",
Felipe Balbi550a7372008-07-24 12:27:36 +0300733 is_out ? "-->" : "<--",
734 epnum, urb, urb->dev->speed,
735 qh->addr_reg, qh->epnum, is_out ? "out" : "in",
736 qh->h_addr_reg, qh->h_port_reg,
737 len);
738
739 musb_ep_select(mbase, epnum);
740
Ajay Kumar Gupta31321222012-07-20 11:07:22 +0530741 if (is_out && !len) {
742 use_dma = 0;
743 csr = musb_readw(epio, MUSB_TXCSR);
744 csr &= ~MUSB_TXCSR_DMAENAB;
745 musb_writew(epio, MUSB_TXCSR, csr);
746 hw_ep->tx_channel = NULL;
747 }
748
Felipe Balbi550a7372008-07-24 12:27:36 +0300749 /* candidate for DMA? */
750 dma_controller = musb->dma_controller;
Ajay Kumar Gupta31321222012-07-20 11:07:22 +0530751 if (use_dma && is_dma_capable() && epnum && dma_controller) {
Felipe Balbi550a7372008-07-24 12:27:36 +0300752 dma_channel = is_out ? hw_ep->tx_channel : hw_ep->rx_channel;
753 if (!dma_channel) {
754 dma_channel = dma_controller->channel_alloc(
755 dma_controller, hw_ep, is_out);
756 if (is_out)
757 hw_ep->tx_channel = dma_channel;
758 else
759 hw_ep->rx_channel = dma_channel;
760 }
761 } else
762 dma_channel = NULL;
763
764 /* make sure we clear DMAEnab, autoSet bits from previous run */
765
766 /* OUT/transmit/EP0 or IN/receive? */
767 if (is_out) {
768 u16 csr;
769 u16 int_txe;
770 u16 load_count;
771
772 csr = musb_readw(epio, MUSB_TXCSR);
773
774 /* disable interrupt in case we flush */
Sebastian Andrzej Siewiorb18d26f2012-10-30 19:52:26 +0100775 int_txe = musb->intrtxe;
Felipe Balbi550a7372008-07-24 12:27:36 +0300776 musb_writew(mbase, MUSB_INTRTXE, int_txe & ~(1 << epnum));
777
778 /* general endpoint setup */
779 if (epnum) {
Felipe Balbi550a7372008-07-24 12:27:36 +0300780 /* flush all old state, set default */
supriya karantha70b8442013-01-04 17:10:33 +0530781 /*
782 * We could be flushing valid
783 * packets in double buffering
784 * case
785 */
786 if (!hw_ep->tx_double_buffered)
787 musb_h_tx_flush_fifo(hw_ep);
Sergei Shtylyovb6e434a2009-03-26 18:27:47 -0700788
789 /*
790 * We must not clear the DMAMODE bit before or in
791 * the same cycle with the DMAENAB bit, so we clear
792 * the latter first...
793 */
Felipe Balbi550a7372008-07-24 12:27:36 +0300794 csr &= ~(MUSB_TXCSR_H_NAKTIMEOUT
Sergei Shtylyovb6e434a2009-03-26 18:27:47 -0700795 | MUSB_TXCSR_AUTOSET
796 | MUSB_TXCSR_DMAENAB
Felipe Balbi550a7372008-07-24 12:27:36 +0300797 | MUSB_TXCSR_FRCDATATOG
798 | MUSB_TXCSR_H_RXSTALL
799 | MUSB_TXCSR_H_ERROR
800 | MUSB_TXCSR_TXPKTRDY
801 );
802 csr |= MUSB_TXCSR_MODE;
803
supriya karantha70b8442013-01-04 17:10:33 +0530804 if (!hw_ep->tx_double_buffered) {
805 if (usb_gettoggle(urb->dev, qh->epnum, 1))
806 csr |= MUSB_TXCSR_H_WR_DATATOGGLE
807 | MUSB_TXCSR_H_DATATOGGLE;
808 else
809 csr |= MUSB_TXCSR_CLRDATATOG;
810 }
Felipe Balbi550a7372008-07-24 12:27:36 +0300811
Felipe Balbi550a7372008-07-24 12:27:36 +0300812 musb_writew(epio, MUSB_TXCSR, csr);
813 /* REVISIT may need to clear FLUSHFIFO ... */
Sergei Shtylyovb6e434a2009-03-26 18:27:47 -0700814 csr &= ~MUSB_TXCSR_DMAMODE;
Felipe Balbi550a7372008-07-24 12:27:36 +0300815 musb_writew(epio, MUSB_TXCSR, csr);
816 csr = musb_readw(epio, MUSB_TXCSR);
817 } else {
818 /* endpoint 0: just flush */
David Brownell78322c12009-03-26 17:38:30 -0700819 musb_h_ep0_flush_fifo(hw_ep);
Felipe Balbi550a7372008-07-24 12:27:36 +0300820 }
821
822 /* target addr and (for multipoint) hub addr/port */
823 if (musb->is_multipoint) {
Hans de Goede6cc2af62015-03-20 20:11:12 +0100824 musb_write_txfunaddr(musb, epnum, qh->addr_reg);
825 musb_write_txhubaddr(musb, epnum, qh->h_addr_reg);
826 musb_write_txhubport(musb, epnum, qh->h_port_reg);
Felipe Balbi550a7372008-07-24 12:27:36 +0300827/* FIXME if !epnum, do the same for RX ... */
828 } else
829 musb_writeb(mbase, MUSB_FADDR, qh->addr_reg);
830
831 /* protocol/endpoint/interval/NAKlimit */
832 if (epnum) {
833 musb_writeb(epio, MUSB_TXTYPE, qh->type_reg);
supriya karanthf2786282012-12-06 11:16:23 +0530834 if (musb->double_buffer_not_ok) {
Felipe Balbi550a7372008-07-24 12:27:36 +0300835 musb_writew(epio, MUSB_TXMAXP,
Felipe Balbi06624812011-01-21 13:39:20 +0800836 hw_ep->max_packet_sz_tx);
supriya karanthf2786282012-12-06 11:16:23 +0530837 } else if (can_bulk_split(musb, qh->type)) {
838 qh->hb_mult = hw_ep->max_packet_sz_tx
839 / packet_sz;
Ajay Kumar Guptaccc080c2011-12-13 10:32:42 +0530840 musb_writew(epio, MUSB_TXMAXP, packet_sz
supriya karanthf2786282012-12-06 11:16:23 +0530841 | ((qh->hb_mult) - 1) << 11);
842 } else {
Felipe Balbi550a7372008-07-24 12:27:36 +0300843 musb_writew(epio, MUSB_TXMAXP,
Felipe Balbi06624812011-01-21 13:39:20 +0800844 qh->maxpacket |
845 ((qh->hb_mult - 1) << 11));
supriya karanthf2786282012-12-06 11:16:23 +0530846 }
Felipe Balbi550a7372008-07-24 12:27:36 +0300847 musb_writeb(epio, MUSB_TXINTERVAL, qh->intv_reg);
848 } else {
849 musb_writeb(epio, MUSB_NAKLIMIT0, qh->intv_reg);
850 if (musb->is_multipoint)
851 musb_writeb(epio, MUSB_TYPE0,
852 qh->type_reg);
853 }
854
855 if (can_bulk_split(musb, qh->type))
856 load_count = min((u32) hw_ep->max_packet_sz_tx,
857 len);
858 else
859 load_count = min((u32) packet_sz, len);
860
Sergei Shtylyov6b6e9712009-03-26 18:29:19 -0700861 if (dma_channel && musb_tx_dma_program(dma_controller,
862 hw_ep, qh, urb, offset, len))
863 load_count = 0;
Felipe Balbi550a7372008-07-24 12:27:36 +0300864
865 if (load_count) {
Felipe Balbi550a7372008-07-24 12:27:36 +0300866 /* PIO to load FIFO */
867 qh->segsize = load_count;
Virupax Sadashivpetimath8e8a5512012-08-07 14:46:20 +0530868 if (!buf) {
869 sg_miter_start(&qh->sg_miter, urb->sg, 1,
870 SG_MITER_ATOMIC
871 | SG_MITER_FROM_SG);
872 if (!sg_miter_next(&qh->sg_miter)) {
873 dev_err(musb->controller,
874 "error: sg"
875 "list empty\n");
876 sg_miter_stop(&qh->sg_miter);
877 goto finish;
878 }
879 buf = qh->sg_miter.addr + urb->sg->offset +
880 urb->actual_length;
881 load_count = min_t(u32, load_count,
882 qh->sg_miter.length);
883 musb_write_fifo(hw_ep, load_count, buf);
884 qh->sg_miter.consumed = load_count;
885 sg_miter_stop(&qh->sg_miter);
886 } else
887 musb_write_fifo(hw_ep, load_count, buf);
Felipe Balbi550a7372008-07-24 12:27:36 +0300888 }
Virupax Sadashivpetimath8e8a5512012-08-07 14:46:20 +0530889finish:
Felipe Balbi550a7372008-07-24 12:27:36 +0300890 /* re-enable interrupt */
891 musb_writew(mbase, MUSB_INTRTXE, int_txe);
892
893 /* IN/receive */
894 } else {
895 u16 csr;
896
897 if (hw_ep->rx_reinit) {
Hans de Goede0cb74b32015-03-20 20:11:11 +0100898 musb_rx_reinit(musb, qh, epnum);
Felipe Balbi550a7372008-07-24 12:27:36 +0300899
900 /* init new state: toggle and NYET, maybe DMA later */
901 if (usb_gettoggle(urb->dev, qh->epnum, 0))
902 csr = MUSB_RXCSR_H_WR_DATATOGGLE
903 | MUSB_RXCSR_H_DATATOGGLE;
904 else
905 csr = 0;
906 if (qh->type == USB_ENDPOINT_XFER_INT)
907 csr |= MUSB_RXCSR_DISNYET;
908
909 } else {
910 csr = musb_readw(hw_ep->regs, MUSB_RXCSR);
911
912 if (csr & (MUSB_RXCSR_RXPKTRDY
913 | MUSB_RXCSR_DMAENAB
914 | MUSB_RXCSR_H_REQPKT))
915 ERR("broken !rx_reinit, ep%d csr %04x\n",
916 hw_ep->epnum, csr);
917
918 /* scrub any stale state, leaving toggle alone */
919 csr &= MUSB_RXCSR_DISNYET;
920 }
921
922 /* kick things off */
923
Tony Lindgrenf8e9f34f2015-05-01 12:29:27 -0700924 if ((is_cppi_enabled(musb) || tusb_dma_omap(musb)) && dma_channel) {
Sergei Shtylyovc51e36d2011-05-07 19:44:13 +0400925 /* Candidate for DMA */
926 dma_channel->actual_len = 0L;
927 qh->segsize = len;
Felipe Balbi550a7372008-07-24 12:27:36 +0300928
Sergei Shtylyovc51e36d2011-05-07 19:44:13 +0400929 /* AUTOREQ is in a DMA register */
930 musb_writew(hw_ep->regs, MUSB_RXCSR, csr);
931 csr = musb_readw(hw_ep->regs, MUSB_RXCSR);
Felipe Balbi550a7372008-07-24 12:27:36 +0300932
Sergei Shtylyovc51e36d2011-05-07 19:44:13 +0400933 /*
934 * Unless caller treats short RX transfers as
935 * errors, we dare not queue multiple transfers.
936 */
937 dma_ok = dma_controller->channel_program(dma_channel,
938 packet_sz, !(urb->transfer_flags &
939 URB_SHORT_NOT_OK),
940 urb->transfer_dma + offset,
941 qh->segsize);
942 if (!dma_ok) {
943 dma_controller->channel_release(dma_channel);
944 hw_ep->rx_channel = dma_channel = NULL;
945 } else
946 csr |= MUSB_RXCSR_DMAENAB;
Felipe Balbi550a7372008-07-24 12:27:36 +0300947 }
948
949 csr |= MUSB_RXCSR_H_REQPKT;
Bin Liub99d3652016-06-30 12:12:22 -0500950 musb_dbg(musb, "RXCSR%d := %04x", epnum, csr);
Felipe Balbi550a7372008-07-24 12:27:36 +0300951 musb_writew(hw_ep->regs, MUSB_RXCSR, csr);
952 csr = musb_readw(hw_ep->regs, MUSB_RXCSR);
953 }
954}
955
Ajay Kumar Guptaf2838622012-07-19 13:41:59 +0530956/* Schedule next QH from musb->in_bulk/out_bulk and move the current qh to
957 * the end; avoids starvation for other endpoints.
958 */
959static void musb_bulk_nak_timeout(struct musb *musb, struct musb_hw_ep *ep,
960 int is_in)
961{
962 struct dma_channel *dma;
963 struct urb *urb;
964 void __iomem *mbase = musb->mregs;
965 void __iomem *epio = ep->regs;
966 struct musb_qh *cur_qh, *next_qh;
967 u16 rx_csr, tx_csr;
968
969 musb_ep_select(mbase, ep->epnum);
970 if (is_in) {
971 dma = is_dma_capable() ? ep->rx_channel : NULL;
972
Andrew Goodbody7b2c17f2016-05-31 10:05:27 -0500973 /*
974 * Need to stop the transaction by clearing REQPKT first
975 * then the NAK Timeout bit ref MUSBMHDRC USB 2.0 HIGH-SPEED
976 * DUAL-ROLE CONTROLLER Programmer's Guide, section 9.2.2
977 */
Ajay Kumar Guptaf2838622012-07-19 13:41:59 +0530978 rx_csr = musb_readw(epio, MUSB_RXCSR);
979 rx_csr |= MUSB_RXCSR_H_WZC_BITS;
Andrew Goodbody7b2c17f2016-05-31 10:05:27 -0500980 rx_csr &= ~MUSB_RXCSR_H_REQPKT;
981 musb_writew(epio, MUSB_RXCSR, rx_csr);
Ajay Kumar Guptaf2838622012-07-19 13:41:59 +0530982 rx_csr &= ~MUSB_RXCSR_DATAERROR;
983 musb_writew(epio, MUSB_RXCSR, rx_csr);
984
985 cur_qh = first_qh(&musb->in_bulk);
986 } else {
987 dma = is_dma_capable() ? ep->tx_channel : NULL;
988
989 /* clear nak timeout bit */
990 tx_csr = musb_readw(epio, MUSB_TXCSR);
991 tx_csr |= MUSB_TXCSR_H_WZC_BITS;
992 tx_csr &= ~MUSB_TXCSR_H_NAKTIMEOUT;
993 musb_writew(epio, MUSB_TXCSR, tx_csr);
994
995 cur_qh = first_qh(&musb->out_bulk);
996 }
997 if (cur_qh) {
998 urb = next_urb(cur_qh);
999 if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) {
1000 dma->status = MUSB_DMA_STATUS_CORE_ABORT;
1001 musb->dma_controller->channel_abort(dma);
1002 urb->actual_length += dma->actual_len;
1003 dma->actual_len = 0L;
1004 }
1005 musb_save_toggle(cur_qh, is_in, urb);
1006
1007 if (is_in) {
1008 /* move cur_qh to end of queue */
1009 list_move_tail(&cur_qh->ring, &musb->in_bulk);
1010
1011 /* get the next qh from musb->in_bulk */
1012 next_qh = first_qh(&musb->in_bulk);
1013
1014 /* set rx_reinit and schedule the next qh */
1015 ep->rx_reinit = 1;
1016 } else {
1017 /* move cur_qh to end of queue */
1018 list_move_tail(&cur_qh->ring, &musb->out_bulk);
1019
1020 /* get the next qh from musb->out_bulk */
1021 next_qh = first_qh(&musb->out_bulk);
1022
1023 /* set tx_reinit and schedule the next qh */
1024 ep->tx_reinit = 1;
1025 }
1026 musb_start_urb(musb, is_in, next_qh);
1027 }
1028}
Felipe Balbi550a7372008-07-24 12:27:36 +03001029
1030/*
1031 * Service the default endpoint (ep0) as host.
1032 * Return true until it's time to start the status stage.
1033 */
1034static bool musb_h_ep0_continue(struct musb *musb, u16 len, struct urb *urb)
1035{
1036 bool more = false;
1037 u8 *fifo_dest = NULL;
1038 u16 fifo_count = 0;
1039 struct musb_hw_ep *hw_ep = musb->control_ep;
1040 struct musb_qh *qh = hw_ep->in_qh;
1041 struct usb_ctrlrequest *request;
1042
1043 switch (musb->ep0_stage) {
1044 case MUSB_EP0_IN:
1045 fifo_dest = urb->transfer_buffer + urb->actual_length;
Sergei Shtylyov3ecdb9a2009-02-21 15:31:23 -08001046 fifo_count = min_t(size_t, len, urb->transfer_buffer_length -
1047 urb->actual_length);
Felipe Balbi550a7372008-07-24 12:27:36 +03001048 if (fifo_count < len)
1049 urb->status = -EOVERFLOW;
1050
1051 musb_read_fifo(hw_ep, fifo_count, fifo_dest);
1052
1053 urb->actual_length += fifo_count;
1054 if (len < qh->maxpacket) {
1055 /* always terminate on short read; it's
1056 * rarely reported as an error.
1057 */
1058 } else if (urb->actual_length <
1059 urb->transfer_buffer_length)
1060 more = true;
1061 break;
1062 case MUSB_EP0_START:
1063 request = (struct usb_ctrlrequest *) urb->setup_packet;
1064
1065 if (!request->wLength) {
Bin Liub99d3652016-06-30 12:12:22 -05001066 musb_dbg(musb, "start no-DATA");
Felipe Balbi550a7372008-07-24 12:27:36 +03001067 break;
1068 } else if (request->bRequestType & USB_DIR_IN) {
Bin Liub99d3652016-06-30 12:12:22 -05001069 musb_dbg(musb, "start IN-DATA");
Felipe Balbi550a7372008-07-24 12:27:36 +03001070 musb->ep0_stage = MUSB_EP0_IN;
1071 more = true;
1072 break;
1073 } else {
Bin Liub99d3652016-06-30 12:12:22 -05001074 musb_dbg(musb, "start OUT-DATA");
Felipe Balbi550a7372008-07-24 12:27:36 +03001075 musb->ep0_stage = MUSB_EP0_OUT;
1076 more = true;
1077 }
1078 /* FALLTHROUGH */
1079 case MUSB_EP0_OUT:
Sergei Shtylyov3ecdb9a2009-02-21 15:31:23 -08001080 fifo_count = min_t(size_t, qh->maxpacket,
1081 urb->transfer_buffer_length -
1082 urb->actual_length);
Felipe Balbi550a7372008-07-24 12:27:36 +03001083 if (fifo_count) {
1084 fifo_dest = (u8 *) (urb->transfer_buffer
1085 + urb->actual_length);
Bin Liub99d3652016-06-30 12:12:22 -05001086 musb_dbg(musb, "Sending %d byte%s to ep0 fifo %p",
David Brownellbb1c9ef2008-11-24 13:06:50 +02001087 fifo_count,
1088 (fifo_count == 1) ? "" : "s",
1089 fifo_dest);
Felipe Balbi550a7372008-07-24 12:27:36 +03001090 musb_write_fifo(hw_ep, fifo_count, fifo_dest);
1091
1092 urb->actual_length += fifo_count;
1093 more = true;
1094 }
1095 break;
1096 default:
1097 ERR("bogus ep0 stage %d\n", musb->ep0_stage);
1098 break;
1099 }
1100
1101 return more;
1102}
1103
1104/*
1105 * Handle default endpoint interrupt as host. Only called in IRQ time
David Brownellc767c1c2008-09-11 11:53:23 +03001106 * from musb_interrupt().
Felipe Balbi550a7372008-07-24 12:27:36 +03001107 *
1108 * called with controller irqlocked
1109 */
1110irqreturn_t musb_h_ep0_irq(struct musb *musb)
1111{
1112 struct urb *urb;
1113 u16 csr, len;
1114 int status = 0;
1115 void __iomem *mbase = musb->mregs;
1116 struct musb_hw_ep *hw_ep = musb->control_ep;
1117 void __iomem *epio = hw_ep->regs;
1118 struct musb_qh *qh = hw_ep->in_qh;
1119 bool complete = false;
1120 irqreturn_t retval = IRQ_NONE;
1121
1122 /* ep0 only has one queue, "in" */
1123 urb = next_urb(qh);
1124
1125 musb_ep_select(mbase, 0);
1126 csr = musb_readw(epio, MUSB_CSR0);
1127 len = (csr & MUSB_CSR0_RXPKTRDY)
1128 ? musb_readb(epio, MUSB_COUNT0)
1129 : 0;
1130
Bin Liub99d3652016-06-30 12:12:22 -05001131 musb_dbg(musb, "<== csr0 %04x, qh %p, count %d, urb %p, stage %d",
Felipe Balbi550a7372008-07-24 12:27:36 +03001132 csr, qh, len, urb, musb->ep0_stage);
1133
1134 /* if we just did status stage, we are done */
1135 if (MUSB_EP0_STATUS == musb->ep0_stage) {
1136 retval = IRQ_HANDLED;
1137 complete = true;
1138 }
1139
1140 /* prepare status */
1141 if (csr & MUSB_CSR0_H_RXSTALL) {
Bin Liub99d3652016-06-30 12:12:22 -05001142 musb_dbg(musb, "STALLING ENDPOINT");
Felipe Balbi550a7372008-07-24 12:27:36 +03001143 status = -EPIPE;
1144
1145 } else if (csr & MUSB_CSR0_H_ERROR) {
Bin Liub99d3652016-06-30 12:12:22 -05001146 musb_dbg(musb, "no response, csr0 %04x", csr);
Felipe Balbi550a7372008-07-24 12:27:36 +03001147 status = -EPROTO;
1148
1149 } else if (csr & MUSB_CSR0_H_NAKTIMEOUT) {
Bin Liub99d3652016-06-30 12:12:22 -05001150 musb_dbg(musb, "control NAK timeout");
Felipe Balbi550a7372008-07-24 12:27:36 +03001151
1152 /* NOTE: this code path would be a good place to PAUSE a
1153 * control transfer, if another one is queued, so that
Ajay Kumar Gupta1e0320f2009-02-24 15:26:13 -08001154 * ep0 is more likely to stay busy. That's already done
1155 * for bulk RX transfers.
Felipe Balbi550a7372008-07-24 12:27:36 +03001156 *
1157 * if (qh->ring.next != &musb->control), then
1158 * we have a candidate... NAKing is *NOT* an error
1159 */
1160 musb_writew(epio, MUSB_CSR0, 0);
1161 retval = IRQ_HANDLED;
1162 }
1163
1164 if (status) {
Bin Liub99d3652016-06-30 12:12:22 -05001165 musb_dbg(musb, "aborting");
Felipe Balbi550a7372008-07-24 12:27:36 +03001166 retval = IRQ_HANDLED;
1167 if (urb)
1168 urb->status = status;
1169 complete = true;
1170
1171 /* use the proper sequence to abort the transfer */
1172 if (csr & MUSB_CSR0_H_REQPKT) {
1173 csr &= ~MUSB_CSR0_H_REQPKT;
1174 musb_writew(epio, MUSB_CSR0, csr);
1175 csr &= ~MUSB_CSR0_H_NAKTIMEOUT;
1176 musb_writew(epio, MUSB_CSR0, csr);
1177 } else {
David Brownell78322c12009-03-26 17:38:30 -07001178 musb_h_ep0_flush_fifo(hw_ep);
Felipe Balbi550a7372008-07-24 12:27:36 +03001179 }
1180
1181 musb_writeb(epio, MUSB_NAKLIMIT0, 0);
1182
1183 /* clear it */
1184 musb_writew(epio, MUSB_CSR0, 0);
1185 }
1186
1187 if (unlikely(!urb)) {
1188 /* stop endpoint since we have no place for its data, this
1189 * SHOULD NEVER HAPPEN! */
1190 ERR("no URB for end 0\n");
1191
David Brownell78322c12009-03-26 17:38:30 -07001192 musb_h_ep0_flush_fifo(hw_ep);
Felipe Balbi550a7372008-07-24 12:27:36 +03001193 goto done;
1194 }
1195
1196 if (!complete) {
1197 /* call common logic and prepare response */
1198 if (musb_h_ep0_continue(musb, len, urb)) {
1199 /* more packets required */
1200 csr = (MUSB_EP0_IN == musb->ep0_stage)
1201 ? MUSB_CSR0_H_REQPKT : MUSB_CSR0_TXPKTRDY;
1202 } else {
1203 /* data transfer complete; perform status phase */
1204 if (usb_pipeout(urb->pipe)
1205 || !urb->transfer_buffer_length)
1206 csr = MUSB_CSR0_H_STATUSPKT
1207 | MUSB_CSR0_H_REQPKT;
1208 else
1209 csr = MUSB_CSR0_H_STATUSPKT
1210 | MUSB_CSR0_TXPKTRDY;
1211
Ajay Kumar Gupta3c4653c2014-02-04 15:28:06 +02001212 /* disable ping token in status phase */
1213 csr |= MUSB_CSR0_H_DIS_PING;
1214
Felipe Balbi550a7372008-07-24 12:27:36 +03001215 /* flag status stage */
1216 musb->ep0_stage = MUSB_EP0_STATUS;
1217
Bin Liub99d3652016-06-30 12:12:22 -05001218 musb_dbg(musb, "ep0 STATUS, csr %04x", csr);
Felipe Balbi550a7372008-07-24 12:27:36 +03001219
1220 }
1221 musb_writew(epio, MUSB_CSR0, csr);
1222 retval = IRQ_HANDLED;
1223 } else
1224 musb->ep0_stage = MUSB_EP0_IDLE;
1225
1226 /* call completion handler if done */
1227 if (complete)
1228 musb_advance_schedule(musb, urb, hw_ep, 1);
1229done:
1230 return retval;
1231}
1232
1233
1234#ifdef CONFIG_USB_INVENTRA_DMA
1235
1236/* Host side TX (OUT) using Mentor DMA works as follows:
1237 submit_urb ->
1238 - if queue was empty, Program Endpoint
1239 - ... which starts DMA to fifo in mode 1 or 0
1240
1241 DMA Isr (transfer complete) -> TxAvail()
1242 - Stop DMA (~DmaEnab) (<--- Alert ... currently happens
1243 only in musb_cleanup_urb)
1244 - TxPktRdy has to be set in mode 0 or for
1245 short packets in mode 1.
1246*/
1247
1248#endif
1249
1250/* Service a Tx-Available or dma completion irq for the endpoint */
1251void musb_host_tx(struct musb *musb, u8 epnum)
1252{
1253 int pipe;
1254 bool done = false;
1255 u16 tx_csr;
Sergei Shtylyov6b6e9712009-03-26 18:29:19 -07001256 size_t length = 0;
1257 size_t offset = 0;
Felipe Balbi550a7372008-07-24 12:27:36 +03001258 struct musb_hw_ep *hw_ep = musb->endpoints + epnum;
1259 void __iomem *epio = hw_ep->regs;
Sergei Shtylyov3e5c6dc2009-03-27 12:55:16 -07001260 struct musb_qh *qh = hw_ep->out_qh;
1261 struct urb *urb = next_urb(qh);
Felipe Balbi550a7372008-07-24 12:27:36 +03001262 u32 status = 0;
1263 void __iomem *mbase = musb->mregs;
1264 struct dma_channel *dma;
T. S., Anil Kumarf8afbf7f2010-09-24 13:44:09 +03001265 bool transfer_pending = false;
Felipe Balbi550a7372008-07-24 12:27:36 +03001266
Felipe Balbi550a7372008-07-24 12:27:36 +03001267 musb_ep_select(mbase, epnum);
1268 tx_csr = musb_readw(epio, MUSB_TXCSR);
1269
1270 /* with CPPI, DMA sometimes triggers "extra" irqs */
1271 if (!urb) {
Bin Liub99d3652016-06-30 12:12:22 -05001272 musb_dbg(musb, "extra TX%d ready, csr %04x", epnum, tx_csr);
Sergei Shtylyov6b6e9712009-03-26 18:29:19 -07001273 return;
Felipe Balbi550a7372008-07-24 12:27:36 +03001274 }
1275
1276 pipe = urb->pipe;
1277 dma = is_dma_capable() ? hw_ep->tx_channel : NULL;
Bin Liu19ca6822016-06-30 12:12:26 -05001278 trace_musb_urb_tx(musb, urb);
Bin Liub99d3652016-06-30 12:12:22 -05001279 musb_dbg(musb, "OUT/TX%d end, csr %04x%s", epnum, tx_csr,
Felipe Balbi550a7372008-07-24 12:27:36 +03001280 dma ? ", dma" : "");
1281
1282 /* check for errors */
1283 if (tx_csr & MUSB_TXCSR_H_RXSTALL) {
1284 /* dma was disabled, fifo flushed */
Bin Liub99d3652016-06-30 12:12:22 -05001285 musb_dbg(musb, "TX end %d stall", epnum);
Felipe Balbi550a7372008-07-24 12:27:36 +03001286
1287 /* stall; record URB status */
1288 status = -EPIPE;
1289
1290 } else if (tx_csr & MUSB_TXCSR_H_ERROR) {
1291 /* (NON-ISO) dma was disabled, fifo flushed */
Bin Liub99d3652016-06-30 12:12:22 -05001292 musb_dbg(musb, "TX 3strikes on ep=%d", epnum);
Felipe Balbi550a7372008-07-24 12:27:36 +03001293
1294 status = -ETIMEDOUT;
1295
1296 } else if (tx_csr & MUSB_TXCSR_H_NAKTIMEOUT) {
Ajay Kumar Guptaf2838622012-07-19 13:41:59 +05301297 if (USB_ENDPOINT_XFER_BULK == qh->type && qh->mux == 1
1298 && !list_is_singular(&musb->out_bulk)) {
Bin Liub99d3652016-06-30 12:12:22 -05001299 musb_dbg(musb, "NAK timeout on TX%d ep", epnum);
Ajay Kumar Guptaf2838622012-07-19 13:41:59 +05301300 musb_bulk_nak_timeout(musb, hw_ep, 0);
1301 } else {
Bin Liub99d3652016-06-30 12:12:22 -05001302 musb_dbg(musb, "TX ep%d device not responding", epnum);
Ajay Kumar Guptaf2838622012-07-19 13:41:59 +05301303 /* NOTE: this code path would be a good place to PAUSE a
1304 * transfer, if there's some other (nonperiodic) tx urb
1305 * that could use this fifo. (dma complicates it...)
1306 * That's already done for bulk RX transfers.
1307 *
1308 * if (bulk && qh->ring.next != &musb->out_bulk), then
1309 * we have a candidate... NAKing is *NOT* an error
1310 */
1311 musb_ep_select(mbase, epnum);
1312 musb_writew(epio, MUSB_TXCSR,
1313 MUSB_TXCSR_H_WZC_BITS
1314 | MUSB_TXCSR_TXPKTRDY);
1315 }
1316 return;
Felipe Balbi550a7372008-07-24 12:27:36 +03001317 }
1318
Virupax Sadashivpetimath8e8a5512012-08-07 14:46:20 +05301319done:
Felipe Balbi550a7372008-07-24 12:27:36 +03001320 if (status) {
1321 if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) {
1322 dma->status = MUSB_DMA_STATUS_CORE_ABORT;
Daniel Mack9c547692014-05-26 14:52:35 +02001323 musb->dma_controller->channel_abort(dma);
Felipe Balbi550a7372008-07-24 12:27:36 +03001324 }
1325
1326 /* do the proper sequence to abort the transfer in the
1327 * usb core; the dma engine should already be stopped.
1328 */
1329 musb_h_tx_flush_fifo(hw_ep);
1330 tx_csr &= ~(MUSB_TXCSR_AUTOSET
1331 | MUSB_TXCSR_DMAENAB
1332 | MUSB_TXCSR_H_ERROR
1333 | MUSB_TXCSR_H_RXSTALL
1334 | MUSB_TXCSR_H_NAKTIMEOUT
1335 );
1336
1337 musb_ep_select(mbase, epnum);
1338 musb_writew(epio, MUSB_TXCSR, tx_csr);
1339 /* REVISIT may need to clear FLUSHFIFO ... */
1340 musb_writew(epio, MUSB_TXCSR, tx_csr);
1341 musb_writeb(epio, MUSB_TXINTERVAL, 0);
1342
1343 done = true;
1344 }
1345
1346 /* second cppi case */
1347 if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) {
Bin Liub99d3652016-06-30 12:12:22 -05001348 musb_dbg(musb, "extra TX%d ready, csr %04x", epnum, tx_csr);
Sergei Shtylyov6b6e9712009-03-26 18:29:19 -07001349 return;
Felipe Balbi550a7372008-07-24 12:27:36 +03001350 }
1351
Sergei Shtylyovc7bbc052009-03-26 18:26:40 -07001352 if (is_dma_capable() && dma && !status) {
1353 /*
1354 * DMA has completed. But if we're using DMA mode 1 (multi
1355 * packet DMA), we need a terminal TXPKTRDY interrupt before
1356 * we can consider this transfer completed, lest we trash
1357 * its last packet when writing the next URB's data. So we
1358 * switch back to mode 0 to get that interrupt; we'll come
1359 * back here once it happens.
1360 */
1361 if (tx_csr & MUSB_TXCSR_DMAMODE) {
1362 /*
1363 * We shouldn't clear DMAMODE with DMAENAB set; so
1364 * clear them in a safe order. That should be OK
1365 * once TXPKTRDY has been set (and I've never seen
1366 * it being 0 at this moment -- DMA interrupt latency
1367 * is significant) but if it hasn't been then we have
1368 * no choice but to stop being polite and ignore the
1369 * programmer's guide... :-)
1370 *
1371 * Note that we must write TXCSR with TXPKTRDY cleared
1372 * in order not to re-trigger the packet send (this bit
1373 * can't be cleared by CPU), and there's another caveat:
1374 * TXPKTRDY may be set shortly and then cleared in the
1375 * double-buffered FIFO mode, so we do an extra TXCSR
1376 * read for debouncing...
1377 */
1378 tx_csr &= musb_readw(epio, MUSB_TXCSR);
1379 if (tx_csr & MUSB_TXCSR_TXPKTRDY) {
1380 tx_csr &= ~(MUSB_TXCSR_DMAENAB |
1381 MUSB_TXCSR_TXPKTRDY);
1382 musb_writew(epio, MUSB_TXCSR,
1383 tx_csr | MUSB_TXCSR_H_WZC_BITS);
1384 }
1385 tx_csr &= ~(MUSB_TXCSR_DMAMODE |
1386 MUSB_TXCSR_TXPKTRDY);
1387 musb_writew(epio, MUSB_TXCSR,
1388 tx_csr | MUSB_TXCSR_H_WZC_BITS);
1389
1390 /*
1391 * There is no guarantee that we'll get an interrupt
1392 * after clearing DMAMODE as we might have done this
1393 * too late (after TXPKTRDY was cleared by controller).
1394 * Re-read TXCSR as we have spoiled its previous value.
1395 */
1396 tx_csr = musb_readw(epio, MUSB_TXCSR);
1397 }
1398
1399 /*
1400 * We may get here from a DMA completion or TXPKTRDY interrupt.
1401 * In any case, we must check the FIFO status here and bail out
1402 * only if the FIFO still has data -- that should prevent the
1403 * "missed" TXPKTRDY interrupts and deal with double-buffered
1404 * FIFO mode too...
1405 */
1406 if (tx_csr & (MUSB_TXCSR_FIFONOTEMPTY | MUSB_TXCSR_TXPKTRDY)) {
Bin Liub99d3652016-06-30 12:12:22 -05001407 musb_dbg(musb,
1408 "DMA complete but FIFO not empty, CSR %04x",
1409 tx_csr);
Sergei Shtylyovc7bbc052009-03-26 18:26:40 -07001410 return;
1411 }
1412 }
1413
Felipe Balbi550a7372008-07-24 12:27:36 +03001414 if (!status || dma || usb_pipeisoc(pipe)) {
1415 if (dma)
Sergei Shtylyov6b6e9712009-03-26 18:29:19 -07001416 length = dma->actual_len;
Felipe Balbi550a7372008-07-24 12:27:36 +03001417 else
Sergei Shtylyov6b6e9712009-03-26 18:29:19 -07001418 length = qh->segsize;
1419 qh->offset += length;
Felipe Balbi550a7372008-07-24 12:27:36 +03001420
1421 if (usb_pipeisoc(pipe)) {
1422 struct usb_iso_packet_descriptor *d;
1423
1424 d = urb->iso_frame_desc + qh->iso_idx;
Sergei Shtylyov6b6e9712009-03-26 18:29:19 -07001425 d->actual_length = length;
1426 d->status = status;
Felipe Balbi550a7372008-07-24 12:27:36 +03001427 if (++qh->iso_idx >= urb->number_of_packets) {
1428 done = true;
1429 } else {
1430 d++;
Sergei Shtylyov6b6e9712009-03-26 18:29:19 -07001431 offset = d->offset;
1432 length = d->length;
Felipe Balbi550a7372008-07-24 12:27:36 +03001433 }
T. S., Anil Kumarf8afbf7f2010-09-24 13:44:09 +03001434 } else if (dma && urb->transfer_buffer_length == qh->offset) {
Felipe Balbi550a7372008-07-24 12:27:36 +03001435 done = true;
1436 } else {
1437 /* see if we need to send more data, or ZLP */
1438 if (qh->segsize < qh->maxpacket)
1439 done = true;
1440 else if (qh->offset == urb->transfer_buffer_length
1441 && !(urb->transfer_flags
1442 & URB_ZERO_PACKET))
1443 done = true;
1444 if (!done) {
Sergei Shtylyov6b6e9712009-03-26 18:29:19 -07001445 offset = qh->offset;
1446 length = urb->transfer_buffer_length - offset;
T. S., Anil Kumarf8afbf7f2010-09-24 13:44:09 +03001447 transfer_pending = true;
Felipe Balbi550a7372008-07-24 12:27:36 +03001448 }
1449 }
1450 }
1451
1452 /* urb->status != -EINPROGRESS means request has been faulted,
1453 * so we must abort this transfer after cleanup
1454 */
1455 if (urb->status != -EINPROGRESS) {
1456 done = true;
1457 if (status == 0)
1458 status = urb->status;
1459 }
1460
1461 if (done) {
1462 /* set status */
1463 urb->status = status;
1464 urb->actual_length = qh->offset;
1465 musb_advance_schedule(musb, urb, hw_ep, USB_DIR_OUT);
Sergei Shtylyov6b6e9712009-03-26 18:29:19 -07001466 return;
T. S., Anil Kumarf8afbf7f2010-09-24 13:44:09 +03001467 } else if ((usb_pipeisoc(pipe) || transfer_pending) && dma) {
Sergei Shtylyov6b6e9712009-03-26 18:29:19 -07001468 if (musb_tx_dma_program(musb->dma_controller, hw_ep, qh, urb,
Ajay Kumar Guptadfeffa52009-11-17 15:22:55 +05301469 offset, length)) {
Tony Lindgrenf8e9f34f2015-05-01 12:29:27 -07001470 if (is_cppi_enabled(musb) || tusb_dma_omap(musb))
Ajay Kumar Guptadfeffa52009-11-17 15:22:55 +05301471 musb_h_tx_dma_start(hw_ep);
Sergei Shtylyov6b6e9712009-03-26 18:29:19 -07001472 return;
Ajay Kumar Guptadfeffa52009-11-17 15:22:55 +05301473 }
Sergei Shtylyov6b6e9712009-03-26 18:29:19 -07001474 } else if (tx_csr & MUSB_TXCSR_DMAENAB) {
Bin Liub99d3652016-06-30 12:12:22 -05001475 musb_dbg(musb, "not complete, but DMA enabled?");
Sergei Shtylyov6b6e9712009-03-26 18:29:19 -07001476 return;
1477 }
Felipe Balbi550a7372008-07-24 12:27:36 +03001478
Sergei Shtylyov6b6e9712009-03-26 18:29:19 -07001479 /*
1480 * PIO: start next packet in this URB.
1481 *
1482 * REVISIT: some docs say that when hw_ep->tx_double_buffered,
1483 * (and presumably, FIFO is not half-full) we should write *two*
1484 * packets before updating TXCSR; other docs disagree...
1485 */
1486 if (length > qh->maxpacket)
1487 length = qh->maxpacket;
Maulik Mankad496dda72010-09-24 13:44:06 +03001488 /* Unmap the buffer so that CPU can use it */
Daniel Mack8b125df2013-04-10 21:55:50 +02001489 usb_hcd_unmap_urb_for_dma(musb->hcd, urb);
Virupax Sadashivpetimath8e8a5512012-08-07 14:46:20 +05301490
1491 /*
1492 * We need to map sg if the transfer_buffer is
1493 * NULL.
1494 */
1495 if (!urb->transfer_buffer)
Virupax Sadashivpetimathed74df12013-04-24 08:38:48 +02001496 qh->use_sg = true;
Virupax Sadashivpetimath8e8a5512012-08-07 14:46:20 +05301497
Virupax Sadashivpetimathed74df12013-04-24 08:38:48 +02001498 if (qh->use_sg) {
Virupax Sadashivpetimath8e8a5512012-08-07 14:46:20 +05301499 /* sg_miter_start is already done in musb_ep_program */
1500 if (!sg_miter_next(&qh->sg_miter)) {
1501 dev_err(musb->controller, "error: sg list empty\n");
1502 sg_miter_stop(&qh->sg_miter);
1503 status = -EINVAL;
1504 goto done;
1505 }
1506 urb->transfer_buffer = qh->sg_miter.addr;
1507 length = min_t(u32, length, qh->sg_miter.length);
1508 musb_write_fifo(hw_ep, length, urb->transfer_buffer);
1509 qh->sg_miter.consumed = length;
1510 sg_miter_stop(&qh->sg_miter);
1511 } else {
1512 musb_write_fifo(hw_ep, length, urb->transfer_buffer + offset);
1513 }
1514
Sergei Shtylyov6b6e9712009-03-26 18:29:19 -07001515 qh->segsize = length;
Felipe Balbi550a7372008-07-24 12:27:36 +03001516
Virupax Sadashivpetimathed74df12013-04-24 08:38:48 +02001517 if (qh->use_sg) {
Virupax Sadashivpetimath8e8a5512012-08-07 14:46:20 +05301518 if (offset + length >= urb->transfer_buffer_length)
Virupax Sadashivpetimathed74df12013-04-24 08:38:48 +02001519 qh->use_sg = false;
Virupax Sadashivpetimath8e8a5512012-08-07 14:46:20 +05301520 }
1521
Sergei Shtylyov6b6e9712009-03-26 18:29:19 -07001522 musb_ep_select(mbase, epnum);
1523 musb_writew(epio, MUSB_TXCSR,
1524 MUSB_TXCSR_H_WZC_BITS | MUSB_TXCSR_TXPKTRDY);
Felipe Balbi550a7372008-07-24 12:27:36 +03001525}
1526
Tony Lindgren069a3fd2015-05-01 12:29:33 -07001527#ifdef CONFIG_USB_TI_CPPI41_DMA
1528/* Seems to set up ISO for cppi41 and not advance len. See commit c57c41d */
1529static int musb_rx_dma_iso_cppi41(struct dma_controller *dma,
1530 struct musb_hw_ep *hw_ep,
1531 struct musb_qh *qh,
1532 struct urb *urb,
1533 size_t len)
1534{
Bin Liu04471eb2016-05-31 10:05:25 -05001535 struct dma_channel *channel = hw_ep->rx_channel;
Tony Lindgren069a3fd2015-05-01 12:29:33 -07001536 void __iomem *epio = hw_ep->regs;
1537 dma_addr_t *buf;
1538 u32 length, res;
1539 u16 val;
1540
1541 buf = (void *)urb->iso_frame_desc[qh->iso_idx].offset +
1542 (u32)urb->transfer_dma;
1543
1544 length = urb->iso_frame_desc[qh->iso_idx].length;
1545
1546 val = musb_readw(epio, MUSB_RXCSR);
1547 val |= MUSB_RXCSR_DMAENAB;
1548 musb_writew(hw_ep->regs, MUSB_RXCSR, val);
1549
1550 res = dma->channel_program(channel, qh->maxpacket, 0,
1551 (u32)buf, length);
1552
1553 return res;
1554}
1555#else
1556static inline int musb_rx_dma_iso_cppi41(struct dma_controller *dma,
1557 struct musb_hw_ep *hw_ep,
1558 struct musb_qh *qh,
1559 struct urb *urb,
1560 size_t len)
1561{
1562 return false;
1563}
1564#endif
Felipe Balbi550a7372008-07-24 12:27:36 +03001565
Tony Lindgrencff84bd2015-05-01 12:29:35 -07001566#if defined(CONFIG_USB_INVENTRA_DMA) || defined(CONFIG_USB_UX500_DMA) || \
1567 defined(CONFIG_USB_TI_CPPI41_DMA)
Felipe Balbi550a7372008-07-24 12:27:36 +03001568/* Host side RX (IN) using Mentor DMA works as follows:
1569 submit_urb ->
1570 - if queue was empty, ProgramEndpoint
1571 - first IN token is sent out (by setting ReqPkt)
1572 LinuxIsr -> RxReady()
1573 /\ => first packet is received
1574 | - Set in mode 0 (DmaEnab, ~ReqPkt)
1575 | -> DMA Isr (transfer complete) -> RxReady()
1576 | - Ack receive (~RxPktRdy), turn off DMA (~DmaEnab)
1577 | - if urb not complete, send next IN token (ReqPkt)
1578 | | else complete urb.
1579 | |
1580 ---------------------------
1581 *
1582 * Nuances of mode 1:
1583 * For short packets, no ack (+RxPktRdy) is sent automatically
1584 * (even if AutoClear is ON)
1585 * For full packets, ack (~RxPktRdy) and next IN token (+ReqPkt) is sent
1586 * automatically => major problem, as collecting the next packet becomes
1587 * difficult. Hence mode 1 is not used.
1588 *
1589 * REVISIT
1590 * All we care about at this driver level is that
1591 * (a) all URBs terminate with REQPKT cleared and fifo(s) empty;
1592 * (b) termination conditions are: short RX, or buffer full;
1593 * (c) fault modes include
1594 * - iff URB_SHORT_NOT_OK, short RX status is -EREMOTEIO.
1595 * (and that endpoint's dma queue stops immediately)
1596 * - overflow (full, PLUS more bytes in the terminal packet)
1597 *
1598 * So for example, usb-storage sets URB_SHORT_NOT_OK, and would
1599 * thus be a great candidate for using mode 1 ... for all but the
1600 * last packet of one URB's transfer.
1601 */
Tony Lindgrencff84bd2015-05-01 12:29:35 -07001602static int musb_rx_dma_inventra_cppi41(struct dma_controller *dma,
1603 struct musb_hw_ep *hw_ep,
1604 struct musb_qh *qh,
1605 struct urb *urb,
1606 size_t len)
1607{
1608 struct dma_channel *channel = hw_ep->rx_channel;
1609 void __iomem *epio = hw_ep->regs;
1610 u16 val;
1611 int pipe;
1612 bool done;
Felipe Balbi550a7372008-07-24 12:27:36 +03001613
Tony Lindgrencff84bd2015-05-01 12:29:35 -07001614 pipe = urb->pipe;
1615
1616 if (usb_pipeisoc(pipe)) {
1617 struct usb_iso_packet_descriptor *d;
1618
1619 d = urb->iso_frame_desc + qh->iso_idx;
1620 d->actual_length = len;
1621
1622 /* even if there was an error, we did the dma
1623 * for iso_frame_desc->length
1624 */
1625 if (d->status != -EILSEQ && d->status != -EOVERFLOW)
1626 d->status = 0;
1627
1628 if (++qh->iso_idx >= urb->number_of_packets) {
1629 done = true;
1630 } else {
1631 /* REVISIT: Why ignore return value here? */
1632 if (musb_dma_cppi41(hw_ep->musb))
1633 done = musb_rx_dma_iso_cppi41(dma, hw_ep, qh,
1634 urb, len);
1635 done = false;
1636 }
1637
1638 } else {
1639 /* done if urb buffer is full or short packet is recd */
1640 done = (urb->actual_length + len >=
1641 urb->transfer_buffer_length
1642 || channel->actual_len < qh->maxpacket
1643 || channel->rx_packet_done);
1644 }
1645
1646 /* send IN token for next packet, without AUTOREQ */
1647 if (!done) {
1648 val = musb_readw(epio, MUSB_RXCSR);
1649 val |= MUSB_RXCSR_H_REQPKT;
1650 musb_writew(epio, MUSB_RXCSR, MUSB_RXCSR_H_WZC_BITS | val);
1651 }
1652
1653 return done;
1654}
Tony Lindgrenac33cdb2015-05-01 12:29:37 -07001655
1656/* Disadvantage of using mode 1:
1657 * It's basically usable only for mass storage class; essentially all
1658 * other protocols also terminate transfers on short packets.
1659 *
1660 * Details:
1661 * An extra IN token is sent at the end of the transfer (due to AUTOREQ)
1662 * If you try to use mode 1 for (transfer_buffer_length - 512), and try
1663 * to use the extra IN token to grab the last packet using mode 0, then
1664 * the problem is that you cannot be sure when the device will send the
1665 * last packet and RxPktRdy set. Sometimes the packet is recd too soon
1666 * such that it gets lost when RxCSR is re-set at the end of the mode 1
1667 * transfer, while sometimes it is recd just a little late so that if you
1668 * try to configure for mode 0 soon after the mode 1 transfer is
1669 * completed, you will find rxcount 0. Okay, so you might think why not
1670 * wait for an interrupt when the pkt is recd. Well, you won't get any!
1671 */
1672static int musb_rx_dma_in_inventra_cppi41(struct dma_controller *dma,
1673 struct musb_hw_ep *hw_ep,
1674 struct musb_qh *qh,
1675 struct urb *urb,
1676 size_t len,
1677 u8 iso_err)
1678{
1679 struct musb *musb = hw_ep->musb;
1680 void __iomem *epio = hw_ep->regs;
1681 struct dma_channel *channel = hw_ep->rx_channel;
1682 u16 rx_count, val;
1683 int length, pipe, done;
1684 dma_addr_t buf;
1685
1686 rx_count = musb_readw(epio, MUSB_RXCOUNT);
1687 pipe = urb->pipe;
1688
1689 if (usb_pipeisoc(pipe)) {
1690 int d_status = 0;
1691 struct usb_iso_packet_descriptor *d;
1692
1693 d = urb->iso_frame_desc + qh->iso_idx;
1694
1695 if (iso_err) {
1696 d_status = -EILSEQ;
1697 urb->error_count++;
1698 }
1699 if (rx_count > d->length) {
1700 if (d_status == 0) {
1701 d_status = -EOVERFLOW;
1702 urb->error_count++;
1703 }
Bin Liub99d3652016-06-30 12:12:22 -05001704 musb_dbg(musb, "** OVERFLOW %d into %d",
Tony Lindgrenac33cdb2015-05-01 12:29:37 -07001705 rx_count, d->length);
1706
1707 length = d->length;
1708 } else
1709 length = rx_count;
1710 d->status = d_status;
1711 buf = urb->transfer_dma + d->offset;
1712 } else {
1713 length = rx_count;
1714 buf = urb->transfer_dma + urb->actual_length;
1715 }
1716
1717 channel->desired_mode = 0;
1718#ifdef USE_MODE1
1719 /* because of the issue below, mode 1 will
1720 * only rarely behave with correct semantics.
1721 */
1722 if ((urb->transfer_flags & URB_SHORT_NOT_OK)
1723 && (urb->transfer_buffer_length - urb->actual_length)
1724 > qh->maxpacket)
1725 channel->desired_mode = 1;
1726 if (rx_count < hw_ep->max_packet_sz_rx) {
1727 length = rx_count;
1728 channel->desired_mode = 0;
1729 } else {
1730 length = urb->transfer_buffer_length;
1731 }
1732#endif
1733
1734 /* See comments above on disadvantages of using mode 1 */
1735 val = musb_readw(epio, MUSB_RXCSR);
1736 val &= ~MUSB_RXCSR_H_REQPKT;
1737
1738 if (channel->desired_mode == 0)
1739 val &= ~MUSB_RXCSR_H_AUTOREQ;
1740 else
1741 val |= MUSB_RXCSR_H_AUTOREQ;
1742 val |= MUSB_RXCSR_DMAENAB;
1743
1744 /* autoclear shouldn't be set in high bandwidth */
1745 if (qh->hb_mult == 1)
1746 val |= MUSB_RXCSR_AUTOCLEAR;
1747
1748 musb_writew(epio, MUSB_RXCSR, MUSB_RXCSR_H_WZC_BITS | val);
1749
1750 /* REVISIT if when actual_length != 0,
1751 * transfer_buffer_length needs to be
1752 * adjusted first...
1753 */
1754 done = dma->channel_program(channel, qh->maxpacket,
1755 channel->desired_mode,
1756 buf, length);
1757
1758 if (!done) {
1759 dma->channel_release(channel);
1760 hw_ep->rx_channel = NULL;
1761 channel = NULL;
1762 val = musb_readw(epio, MUSB_RXCSR);
1763 val &= ~(MUSB_RXCSR_DMAENAB
1764 | MUSB_RXCSR_H_AUTOREQ
1765 | MUSB_RXCSR_AUTOCLEAR);
1766 musb_writew(epio, MUSB_RXCSR, val);
1767 }
1768
1769 return done;
1770}
Tony Lindgrencff84bd2015-05-01 12:29:35 -07001771#else
1772static inline int musb_rx_dma_inventra_cppi41(struct dma_controller *dma,
1773 struct musb_hw_ep *hw_ep,
1774 struct musb_qh *qh,
1775 struct urb *urb,
1776 size_t len)
1777{
1778 return false;
1779}
Tony Lindgrenac33cdb2015-05-01 12:29:37 -07001780
1781static inline int musb_rx_dma_in_inventra_cppi41(struct dma_controller *dma,
1782 struct musb_hw_ep *hw_ep,
1783 struct musb_qh *qh,
1784 struct urb *urb,
1785 size_t len,
1786 u8 iso_err)
1787{
1788 return false;
1789}
Felipe Balbi550a7372008-07-24 12:27:36 +03001790#endif
1791
1792/*
1793 * Service an RX interrupt for the given IN endpoint; docs cover bulk, iso,
1794 * and high-bandwidth IN transfer cases.
1795 */
1796void musb_host_rx(struct musb *musb, u8 epnum)
1797{
1798 struct urb *urb;
1799 struct musb_hw_ep *hw_ep = musb->endpoints + epnum;
Tony Lindgrencff84bd2015-05-01 12:29:35 -07001800 struct dma_controller *c = musb->dma_controller;
Felipe Balbi550a7372008-07-24 12:27:36 +03001801 void __iomem *epio = hw_ep->regs;
1802 struct musb_qh *qh = hw_ep->in_qh;
1803 size_t xfer_len;
1804 void __iomem *mbase = musb->mregs;
1805 int pipe;
1806 u16 rx_csr, val;
1807 bool iso_err = false;
1808 bool done = false;
1809 u32 status;
1810 struct dma_channel *dma;
Virupax Sadashivpetimath8e8a5512012-08-07 14:46:20 +05301811 unsigned int sg_flags = SG_MITER_ATOMIC | SG_MITER_TO_SG;
Felipe Balbi550a7372008-07-24 12:27:36 +03001812
1813 musb_ep_select(mbase, epnum);
1814
1815 urb = next_urb(qh);
1816 dma = is_dma_capable() ? hw_ep->rx_channel : NULL;
1817 status = 0;
1818 xfer_len = 0;
1819
1820 rx_csr = musb_readw(epio, MUSB_RXCSR);
1821 val = rx_csr;
1822
1823 if (unlikely(!urb)) {
1824 /* REVISIT -- THIS SHOULD NEVER HAPPEN ... but, at least
1825 * usbtest #11 (unlinks) triggers it regularly, sometimes
1826 * with fifo full. (Only with DMA??)
1827 */
Bin Liub99d3652016-06-30 12:12:22 -05001828 musb_dbg(musb, "BOGUS RX%d ready, csr %04x, count %d",
1829 epnum, val, musb_readw(epio, MUSB_RXCOUNT));
Felipe Balbi550a7372008-07-24 12:27:36 +03001830 musb_h_flush_rxfifo(hw_ep, MUSB_RXCSR_CLRDATATOG);
1831 return;
1832 }
1833
1834 pipe = urb->pipe;
1835
Bin Liu19ca6822016-06-30 12:12:26 -05001836 trace_musb_urb_rx(musb, urb);
Felipe Balbi550a7372008-07-24 12:27:36 +03001837
1838 /* check for errors, concurrent stall & unlink is not really
1839 * handled yet! */
1840 if (rx_csr & MUSB_RXCSR_H_RXSTALL) {
Bin Liub99d3652016-06-30 12:12:22 -05001841 musb_dbg(musb, "RX end %d STALL", epnum);
Felipe Balbi550a7372008-07-24 12:27:36 +03001842
1843 /* stall; record URB status */
1844 status = -EPIPE;
1845
1846 } else if (rx_csr & MUSB_RXCSR_H_ERROR) {
Bin Liub99d3652016-06-30 12:12:22 -05001847 musb_dbg(musb, "end %d RX proto error", epnum);
Felipe Balbi550a7372008-07-24 12:27:36 +03001848
1849 status = -EPROTO;
1850 musb_writeb(epio, MUSB_RXINTERVAL, 0);
1851
Bin Liub5801212016-05-31 10:05:03 -05001852 rx_csr &= ~MUSB_RXCSR_H_ERROR;
1853 musb_writew(epio, MUSB_RXCSR, rx_csr);
1854
Felipe Balbi550a7372008-07-24 12:27:36 +03001855 } else if (rx_csr & MUSB_RXCSR_DATAERROR) {
1856
1857 if (USB_ENDPOINT_XFER_ISOC != qh->type) {
Bin Liub99d3652016-06-30 12:12:22 -05001858 musb_dbg(musb, "RX end %d NAK timeout", epnum);
Ajay Kumar Gupta1e0320f2009-02-24 15:26:13 -08001859
1860 /* NOTE: NAKing is *NOT* an error, so we want to
1861 * continue. Except ... if there's a request for
1862 * another QH, use that instead of starving it.
1863 *
1864 * Devices like Ethernet and serial adapters keep
1865 * reads posted at all times, which will starve
1866 * other devices without this logic.
1867 */
1868 if (usb_pipebulk(urb->pipe)
1869 && qh->mux == 1
1870 && !list_is_singular(&musb->in_bulk)) {
Ajay Kumar Guptaf2838622012-07-19 13:41:59 +05301871 musb_bulk_nak_timeout(musb, hw_ep, 1);
Ajay Kumar Gupta1e0320f2009-02-24 15:26:13 -08001872 return;
1873 }
Felipe Balbi550a7372008-07-24 12:27:36 +03001874 musb_ep_select(mbase, epnum);
Ajay Kumar Gupta1e0320f2009-02-24 15:26:13 -08001875 rx_csr |= MUSB_RXCSR_H_WZC_BITS;
1876 rx_csr &= ~MUSB_RXCSR_DATAERROR;
1877 musb_writew(epio, MUSB_RXCSR, rx_csr);
Felipe Balbi550a7372008-07-24 12:27:36 +03001878
1879 goto finish;
1880 } else {
Bin Liub99d3652016-06-30 12:12:22 -05001881 musb_dbg(musb, "RX end %d ISO data error", epnum);
Felipe Balbi550a7372008-07-24 12:27:36 +03001882 /* packet error reported later */
1883 iso_err = true;
1884 }
Ajay Kumar Guptaa483d702009-04-03 16:16:17 -07001885 } else if (rx_csr & MUSB_RXCSR_INCOMPRX) {
Bin Liub99d3652016-06-30 12:12:22 -05001886 musb_dbg(musb, "end %d high bandwidth incomplete ISO packet RX",
Ajay Kumar Guptaa483d702009-04-03 16:16:17 -07001887 epnum);
1888 status = -EPROTO;
Felipe Balbi550a7372008-07-24 12:27:36 +03001889 }
1890
1891 /* faults abort the transfer */
1892 if (status) {
1893 /* clean up dma and collect transfer count */
1894 if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) {
1895 dma->status = MUSB_DMA_STATUS_CORE_ABORT;
Daniel Mack9c547692014-05-26 14:52:35 +02001896 musb->dma_controller->channel_abort(dma);
Felipe Balbi550a7372008-07-24 12:27:36 +03001897 xfer_len = dma->actual_len;
1898 }
1899 musb_h_flush_rxfifo(hw_ep, MUSB_RXCSR_CLRDATATOG);
1900 musb_writeb(epio, MUSB_RXINTERVAL, 0);
1901 done = true;
1902 goto finish;
1903 }
1904
1905 if (unlikely(dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY)) {
1906 /* SHOULD NEVER HAPPEN ... but at least DaVinci has done it */
1907 ERR("RX%d dma busy, csr %04x\n", epnum, rx_csr);
1908 goto finish;
1909 }
1910
1911 /* thorough shutdown for now ... given more precise fault handling
1912 * and better queueing support, we might keep a DMA pipeline going
1913 * while processing this irq for earlier completions.
1914 */
1915
1916 /* FIXME this is _way_ too much in-line logic for Mentor DMA */
Tony Lindgren557d5432015-05-01 12:29:34 -07001917 if (!musb_dma_inventra(musb) && !musb_dma_ux500(musb) &&
1918 (rx_csr & MUSB_RXCSR_H_REQPKT)) {
Felipe Balbi550a7372008-07-24 12:27:36 +03001919 /* REVISIT this happened for a while on some short reads...
1920 * the cleanup still needs investigation... looks bad...
1921 * and also duplicates dma cleanup code above ... plus,
1922 * shouldn't this be the "half full" double buffer case?
1923 */
1924 if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) {
1925 dma->status = MUSB_DMA_STATUS_CORE_ABORT;
Daniel Mack9c547692014-05-26 14:52:35 +02001926 musb->dma_controller->channel_abort(dma);
Felipe Balbi550a7372008-07-24 12:27:36 +03001927 xfer_len = dma->actual_len;
1928 done = true;
1929 }
1930
Bin Liub99d3652016-06-30 12:12:22 -05001931 musb_dbg(musb, "RXCSR%d %04x, reqpkt, len %zu%s", epnum, rx_csr,
Felipe Balbi550a7372008-07-24 12:27:36 +03001932 xfer_len, dma ? ", dma" : "");
1933 rx_csr &= ~MUSB_RXCSR_H_REQPKT;
1934
1935 musb_ep_select(mbase, epnum);
1936 musb_writew(epio, MUSB_RXCSR,
1937 MUSB_RXCSR_H_WZC_BITS | rx_csr);
1938 }
Tony Lindgren557d5432015-05-01 12:29:34 -07001939
Felipe Balbi550a7372008-07-24 12:27:36 +03001940 if (dma && (rx_csr & MUSB_RXCSR_DMAENAB)) {
1941 xfer_len = dma->actual_len;
1942
1943 val &= ~(MUSB_RXCSR_DMAENAB
1944 | MUSB_RXCSR_H_AUTOREQ
1945 | MUSB_RXCSR_AUTOCLEAR
1946 | MUSB_RXCSR_RXPKTRDY);
1947 musb_writew(hw_ep->regs, MUSB_RXCSR, val);
1948
Tony Lindgrencff84bd2015-05-01 12:29:35 -07001949 if (musb_dma_inventra(musb) || musb_dma_ux500(musb) ||
1950 musb_dma_cppi41(musb)) {
1951 done = musb_rx_dma_inventra_cppi41(c, hw_ep, qh, urb, xfer_len);
Bin Liub99d3652016-06-30 12:12:22 -05001952 musb_dbg(hw_ep->musb,
1953 "ep %d dma %s, rxcsr %04x, rxcount %d",
Tony Lindgrencff84bd2015-05-01 12:29:35 -07001954 epnum, done ? "off" : "reset",
1955 musb_readw(epio, MUSB_RXCSR),
1956 musb_readw(epio, MUSB_RXCOUNT));
1957 } else {
1958 done = true;
Ajay Kumar Guptaf82a6892008-10-29 15:10:31 +02001959 }
Felipe Balbi550a7372008-07-24 12:27:36 +03001960
Felipe Balbi550a7372008-07-24 12:27:36 +03001961 } else if (urb->status == -EINPROGRESS) {
1962 /* if no errors, be sure a packet is ready for unloading */
1963 if (unlikely(!(rx_csr & MUSB_RXCSR_RXPKTRDY))) {
1964 status = -EPROTO;
1965 ERR("Rx interrupt with no errors or packet!\n");
1966
1967 /* FIXME this is another "SHOULD NEVER HAPPEN" */
1968
1969/* SCRUB (RX) */
1970 /* do the proper sequence to abort the transfer */
1971 musb_ep_select(mbase, epnum);
1972 val &= ~MUSB_RXCSR_H_REQPKT;
1973 musb_writew(epio, MUSB_RXCSR, val);
1974 goto finish;
1975 }
1976
1977 /* we are expecting IN packets */
Tony Lindgrene530bb82015-05-01 12:29:36 -07001978 if ((musb_dma_inventra(musb) || musb_dma_ux500(musb) ||
1979 musb_dma_cppi41(musb)) && dma) {
Bin Liub99d3652016-06-30 12:12:22 -05001980 musb_dbg(hw_ep->musb,
1981 "RX%d count %d, buffer 0x%llx len %d/%d",
Tony Lindgrenac33cdb2015-05-01 12:29:37 -07001982 epnum, musb_readw(epio, MUSB_RXCOUNT),
1983 (unsigned long long) urb->transfer_dma
1984 + urb->actual_length,
1985 qh->offset,
1986 urb->transfer_buffer_length);
Felipe Balbi550a7372008-07-24 12:27:36 +03001987
Cristian Birsan4c2ba0c2016-02-19 10:11:56 +02001988 if (musb_rx_dma_in_inventra_cppi41(c, hw_ep, qh, urb,
1989 xfer_len, iso_err))
Tony Lindgrenac33cdb2015-05-01 12:29:37 -07001990 goto finish;
Felipe Balbi550a7372008-07-24 12:27:36 +03001991 else
Tony Lindgrenac33cdb2015-05-01 12:29:37 -07001992 dev_err(musb->controller, "error: rx_dma failed\n");
Felipe Balbi550a7372008-07-24 12:27:36 +03001993 }
Felipe Balbi550a7372008-07-24 12:27:36 +03001994
1995 if (!dma) {
Virupax Sadashivpetimath8e8a5512012-08-07 14:46:20 +05301996 unsigned int received_len;
1997
Maulik Mankad496dda72010-09-24 13:44:06 +03001998 /* Unmap the buffer so that CPU can use it */
Daniel Mack8b125df2013-04-10 21:55:50 +02001999 usb_hcd_unmap_urb_for_dma(musb->hcd, urb);
Virupax Sadashivpetimath8e8a5512012-08-07 14:46:20 +05302000
2001 /*
2002 * We need to map sg if the transfer_buffer is
2003 * NULL.
2004 */
2005 if (!urb->transfer_buffer) {
Virupax Sadashivpetimathed74df12013-04-24 08:38:48 +02002006 qh->use_sg = true;
Virupax Sadashivpetimath8e8a5512012-08-07 14:46:20 +05302007 sg_miter_start(&qh->sg_miter, urb->sg, 1,
2008 sg_flags);
2009 }
2010
Virupax Sadashivpetimathed74df12013-04-24 08:38:48 +02002011 if (qh->use_sg) {
Virupax Sadashivpetimath8e8a5512012-08-07 14:46:20 +05302012 if (!sg_miter_next(&qh->sg_miter)) {
2013 dev_err(musb->controller, "error: sg list empty\n");
2014 sg_miter_stop(&qh->sg_miter);
2015 status = -EINVAL;
2016 done = true;
2017 goto finish;
2018 }
2019 urb->transfer_buffer = qh->sg_miter.addr;
2020 received_len = urb->actual_length;
2021 qh->offset = 0x0;
2022 done = musb_host_packet_rx(musb, urb, epnum,
2023 iso_err);
2024 /* Calculate the number of bytes received */
2025 received_len = urb->actual_length -
2026 received_len;
2027 qh->sg_miter.consumed = received_len;
2028 sg_miter_stop(&qh->sg_miter);
2029 } else {
2030 done = musb_host_packet_rx(musb, urb,
2031 epnum, iso_err);
2032 }
Bin Liub99d3652016-06-30 12:12:22 -05002033 musb_dbg(musb, "read %spacket", done ? "last " : "");
Felipe Balbi550a7372008-07-24 12:27:36 +03002034 }
2035 }
2036
Felipe Balbi550a7372008-07-24 12:27:36 +03002037finish:
2038 urb->actual_length += xfer_len;
2039 qh->offset += xfer_len;
2040 if (done) {
Virupax Sadashivpetimathed74df12013-04-24 08:38:48 +02002041 if (qh->use_sg)
2042 qh->use_sg = false;
Virupax Sadashivpetimath8e8a5512012-08-07 14:46:20 +05302043
Felipe Balbi550a7372008-07-24 12:27:36 +03002044 if (urb->status == -EINPROGRESS)
2045 urb->status = status;
2046 musb_advance_schedule(musb, urb, hw_ep, USB_DIR_IN);
2047 }
2048}
2049
2050/* schedule nodes correspond to peripheral endpoints, like an OHCI QH.
2051 * the software schedule associates multiple such nodes with a given
2052 * host side hardware endpoint + direction; scheduling may activate
2053 * that hardware endpoint.
2054 */
2055static int musb_schedule(
2056 struct musb *musb,
2057 struct musb_qh *qh,
2058 int is_in)
2059{
Rickard Strandqvisteac44dc2014-06-01 15:48:12 +02002060 int idle = 0;
Felipe Balbi550a7372008-07-24 12:27:36 +03002061 int best_diff;
2062 int best_end, epnum;
2063 struct musb_hw_ep *hw_ep = NULL;
2064 struct list_head *head = NULL;
Swaminathan S5274dab2009-12-28 13:40:37 +02002065 u8 toggle;
2066 u8 txtype;
2067 struct urb *urb = next_urb(qh);
Felipe Balbi550a7372008-07-24 12:27:36 +03002068
2069 /* use fixed hardware for control and bulk */
Ajay Kumar Gupta23d15e02008-10-29 15:10:35 +02002070 if (qh->type == USB_ENDPOINT_XFER_CONTROL) {
Felipe Balbi550a7372008-07-24 12:27:36 +03002071 head = &musb->control;
2072 hw_ep = musb->control_ep;
Felipe Balbi550a7372008-07-24 12:27:36 +03002073 goto success;
2074 }
2075
2076 /* else, periodic transfers get muxed to other endpoints */
2077
Sergei Shtylyov5d67a852009-02-24 15:23:34 -08002078 /*
2079 * We know this qh hasn't been scheduled, so all we need to do
Felipe Balbi550a7372008-07-24 12:27:36 +03002080 * is choose which hardware endpoint to put it on ...
2081 *
2082 * REVISIT what we really want here is a regular schedule tree
Sergei Shtylyov5d67a852009-02-24 15:23:34 -08002083 * like e.g. OHCI uses.
Felipe Balbi550a7372008-07-24 12:27:36 +03002084 */
2085 best_diff = 4096;
2086 best_end = -1;
2087
Sergei Shtylyov5d67a852009-02-24 15:23:34 -08002088 for (epnum = 1, hw_ep = musb->endpoints + 1;
2089 epnum < musb->nr_endpoints;
2090 epnum++, hw_ep++) {
Felipe Balbi550a7372008-07-24 12:27:36 +03002091 int diff;
2092
Sergei Shtylyov3e5c6dc2009-03-27 12:55:16 -07002093 if (musb_ep_get_qh(hw_ep, is_in) != NULL)
Felipe Balbi550a7372008-07-24 12:27:36 +03002094 continue;
Sergei Shtylyov5d67a852009-02-24 15:23:34 -08002095
Felipe Balbi550a7372008-07-24 12:27:36 +03002096 if (hw_ep == musb->bulk_ep)
2097 continue;
2098
2099 if (is_in)
Ajay Kumar Guptaa483d702009-04-03 16:16:17 -07002100 diff = hw_ep->max_packet_sz_rx;
Felipe Balbi550a7372008-07-24 12:27:36 +03002101 else
Ajay Kumar Guptaa483d702009-04-03 16:16:17 -07002102 diff = hw_ep->max_packet_sz_tx;
2103 diff -= (qh->maxpacket * qh->hb_mult);
Felipe Balbi550a7372008-07-24 12:27:36 +03002104
Ajay Kumar Gupta23d15e02008-10-29 15:10:35 +02002105 if (diff >= 0 && best_diff > diff) {
Swaminathan S5274dab2009-12-28 13:40:37 +02002106
2107 /*
2108 * Mentor controller has a bug in that if we schedule
2109 * a BULK Tx transfer on an endpoint that had earlier
2110 * handled ISOC then the BULK transfer has to start on
2111 * a zero toggle. If the BULK transfer starts on a 1
2112 * toggle then this transfer will fail as the mentor
2113 * controller starts the Bulk transfer on a 0 toggle
2114 * irrespective of the programming of the toggle bits
2115 * in the TXCSR register. Check for this condition
2116 * while allocating the EP for a Tx Bulk transfer. If
2117 * so skip this EP.
2118 */
2119 hw_ep = musb->endpoints + epnum;
2120 toggle = usb_gettoggle(urb->dev, qh->epnum, !is_in);
2121 txtype = (musb_readb(hw_ep->regs, MUSB_TXTYPE)
2122 >> 4) & 0x3;
2123 if (!is_in && (qh->type == USB_ENDPOINT_XFER_BULK) &&
2124 toggle && (txtype == USB_ENDPOINT_XFER_ISOC))
2125 continue;
2126
Felipe Balbi550a7372008-07-24 12:27:36 +03002127 best_diff = diff;
2128 best_end = epnum;
2129 }
2130 }
Ajay Kumar Gupta23d15e02008-10-29 15:10:35 +02002131 /* use bulk reserved ep1 if no other ep is free */
Felipe Balbiaa5cbbe2008-11-17 09:08:16 +02002132 if (best_end < 0 && qh->type == USB_ENDPOINT_XFER_BULK) {
Ajay Kumar Gupta23d15e02008-10-29 15:10:35 +02002133 hw_ep = musb->bulk_ep;
2134 if (is_in)
2135 head = &musb->in_bulk;
2136 else
2137 head = &musb->out_bulk;
Ajay Kumar Gupta1e0320f2009-02-24 15:26:13 -08002138
Ajay Kumar Guptaf2838622012-07-19 13:41:59 +05302139 /* Enable bulk RX/TX NAK timeout scheme when bulk requests are
Rahul Bedarkar5ae477b2014-01-02 19:27:47 +05302140 * multiplexed. This scheme does not work in high speed to full
Ajay Kumar Gupta1e0320f2009-02-24 15:26:13 -08002141 * speed scenario as NAK interrupts are not coming from a
2142 * full speed device connected to a high speed device.
2143 * NAK timeout interval is 8 (128 uframe or 16ms) for HS and
2144 * 4 (8 frame or 8ms) for FS device.
2145 */
Ajay Kumar Guptaf2838622012-07-19 13:41:59 +05302146 if (qh->dev)
Ajay Kumar Gupta1e0320f2009-02-24 15:26:13 -08002147 qh->intv_reg =
2148 (USB_SPEED_HIGH == qh->dev->speed) ? 8 : 4;
Ajay Kumar Gupta23d15e02008-10-29 15:10:35 +02002149 goto success;
2150 } else if (best_end < 0) {
Felipe Balbi550a7372008-07-24 12:27:36 +03002151 return -ENOSPC;
Ajay Kumar Gupta23d15e02008-10-29 15:10:35 +02002152 }
Felipe Balbi550a7372008-07-24 12:27:36 +03002153
2154 idle = 1;
Ajay Kumar Gupta23d15e02008-10-29 15:10:35 +02002155 qh->mux = 0;
Felipe Balbi550a7372008-07-24 12:27:36 +03002156 hw_ep = musb->endpoints + best_end;
Bin Liub99d3652016-06-30 12:12:22 -05002157 musb_dbg(musb, "qh %p periodic slot %d", qh, best_end);
Felipe Balbi550a7372008-07-24 12:27:36 +03002158success:
Ajay Kumar Gupta23d15e02008-10-29 15:10:35 +02002159 if (head) {
2160 idle = list_empty(head);
2161 list_add_tail(&qh->ring, head);
2162 qh->mux = 1;
2163 }
Felipe Balbi550a7372008-07-24 12:27:36 +03002164 qh->hw_ep = hw_ep;
2165 qh->hep->hcpriv = qh;
2166 if (idle)
2167 musb_start_urb(musb, is_in, qh);
2168 return 0;
2169}
2170
2171static int musb_urb_enqueue(
2172 struct usb_hcd *hcd,
2173 struct urb *urb,
2174 gfp_t mem_flags)
2175{
2176 unsigned long flags;
2177 struct musb *musb = hcd_to_musb(hcd);
2178 struct usb_host_endpoint *hep = urb->ep;
David Brownell74bb3502009-03-26 17:36:57 -07002179 struct musb_qh *qh;
Felipe Balbi550a7372008-07-24 12:27:36 +03002180 struct usb_endpoint_descriptor *epd = &hep->desc;
2181 int ret;
2182 unsigned type_reg;
2183 unsigned interval;
2184
2185 /* host role must be active */
2186 if (!is_host_active(musb) || !musb->is_active)
2187 return -ENODEV;
2188
Bin Liu19ca6822016-06-30 12:12:26 -05002189 trace_musb_urb_enq(musb, urb);
2190
Felipe Balbi550a7372008-07-24 12:27:36 +03002191 spin_lock_irqsave(&musb->lock, flags);
2192 ret = usb_hcd_link_urb_to_ep(hcd, urb);
David Brownell74bb3502009-03-26 17:36:57 -07002193 qh = ret ? NULL : hep->hcpriv;
2194 if (qh)
2195 urb->hcpriv = qh;
Felipe Balbi550a7372008-07-24 12:27:36 +03002196 spin_unlock_irqrestore(&musb->lock, flags);
Felipe Balbi550a7372008-07-24 12:27:36 +03002197
2198 /* DMA mapping was already done, if needed, and this urb is on
David Brownell74bb3502009-03-26 17:36:57 -07002199 * hep->urb_list now ... so we're done, unless hep wasn't yet
2200 * scheduled onto a live qh.
Felipe Balbi550a7372008-07-24 12:27:36 +03002201 *
2202 * REVISIT best to keep hep->hcpriv valid until the endpoint gets
2203 * disabled, testing for empty qh->ring and avoiding qh setup costs
2204 * except for the first urb queued after a config change.
2205 */
David Brownell74bb3502009-03-26 17:36:57 -07002206 if (qh || ret)
2207 return ret;
Felipe Balbi550a7372008-07-24 12:27:36 +03002208
2209 /* Allocate and initialize qh, minimizing the work done each time
2210 * hw_ep gets reprogrammed, or with irqs blocked. Then schedule it.
2211 *
2212 * REVISIT consider a dedicated qh kmem_cache, so it's harder
2213 * for bugs in other kernel code to break this driver...
2214 */
2215 qh = kzalloc(sizeof *qh, mem_flags);
2216 if (!qh) {
Ajay Kumar Gupta2492e672008-09-11 11:53:21 +03002217 spin_lock_irqsave(&musb->lock, flags);
Felipe Balbi550a7372008-07-24 12:27:36 +03002218 usb_hcd_unlink_urb_from_ep(hcd, urb);
Ajay Kumar Gupta2492e672008-09-11 11:53:21 +03002219 spin_unlock_irqrestore(&musb->lock, flags);
Felipe Balbi550a7372008-07-24 12:27:36 +03002220 return -ENOMEM;
2221 }
2222
2223 qh->hep = hep;
2224 qh->dev = urb->dev;
2225 INIT_LIST_HEAD(&qh->ring);
2226 qh->is_ready = 1;
2227
Kuninori Morimoto29cc8892011-08-23 03:12:03 -07002228 qh->maxpacket = usb_endpoint_maxp(epd);
Ajay Kumar Guptaa483d702009-04-03 16:16:17 -07002229 qh->type = usb_endpoint_type(epd);
Felipe Balbi550a7372008-07-24 12:27:36 +03002230
Ajay Kumar Guptaa483d702009-04-03 16:16:17 -07002231 /* Bits 11 & 12 of wMaxPacketSize encode high bandwidth multiplier.
2232 * Some musb cores don't support high bandwidth ISO transfers; and
2233 * we don't (yet!) support high bandwidth interrupt transfers.
2234 */
2235 qh->hb_mult = 1 + ((qh->maxpacket >> 11) & 0x03);
2236 if (qh->hb_mult > 1) {
2237 int ok = (qh->type == USB_ENDPOINT_XFER_ISOC);
2238
2239 if (ok)
2240 ok = (usb_pipein(urb->pipe) && musb->hb_iso_rx)
2241 || (usb_pipeout(urb->pipe) && musb->hb_iso_tx);
2242 if (!ok) {
2243 ret = -EMSGSIZE;
2244 goto done;
2245 }
2246 qh->maxpacket &= 0x7ff;
Felipe Balbi550a7372008-07-24 12:27:36 +03002247 }
2248
Julia Lawall96bcd092009-01-24 17:57:24 -08002249 qh->epnum = usb_endpoint_num(epd);
Felipe Balbi550a7372008-07-24 12:27:36 +03002250
2251 /* NOTE: urb->dev->devnum is wrong during SET_ADDRESS */
2252 qh->addr_reg = (u8) usb_pipedevice(urb->pipe);
2253
2254 /* precompute rxtype/txtype/type0 register */
2255 type_reg = (qh->type << 4) | qh->epnum;
2256 switch (urb->dev->speed) {
2257 case USB_SPEED_LOW:
2258 type_reg |= 0xc0;
2259 break;
2260 case USB_SPEED_FULL:
2261 type_reg |= 0x80;
2262 break;
2263 default:
2264 type_reg |= 0x40;
2265 }
2266 qh->type_reg = type_reg;
2267
Sergei Shtylyov136733d2009-02-21 15:31:35 -08002268 /* Precompute RXINTERVAL/TXINTERVAL register */
Felipe Balbi550a7372008-07-24 12:27:36 +03002269 switch (qh->type) {
2270 case USB_ENDPOINT_XFER_INT:
Sergei Shtylyov136733d2009-02-21 15:31:35 -08002271 /*
2272 * Full/low speeds use the linear encoding,
2273 * high speed uses the logarithmic encoding.
2274 */
2275 if (urb->dev->speed <= USB_SPEED_FULL) {
2276 interval = max_t(u8, epd->bInterval, 1);
2277 break;
Felipe Balbi550a7372008-07-24 12:27:36 +03002278 }
2279 /* FALLTHROUGH */
2280 case USB_ENDPOINT_XFER_ISOC:
Sergei Shtylyov136733d2009-02-21 15:31:35 -08002281 /* ISO always uses logarithmic encoding */
2282 interval = min_t(u8, epd->bInterval, 16);
Felipe Balbi550a7372008-07-24 12:27:36 +03002283 break;
2284 default:
2285 /* REVISIT we actually want to use NAK limits, hinting to the
2286 * transfer scheduling logic to try some other qh, e.g. try
2287 * for 2 msec first:
2288 *
2289 * interval = (USB_SPEED_HIGH == urb->dev->speed) ? 16 : 2;
2290 *
2291 * The downside of disabling this is that transfer scheduling
2292 * gets VERY unfair for nonperiodic transfers; a misbehaving
Ajay Kumar Gupta1e0320f2009-02-24 15:26:13 -08002293 * peripheral could make that hurt. That's perfectly normal
2294 * for reads from network or serial adapters ... so we have
2295 * partial NAKlimit support for bulk RX.
Felipe Balbi550a7372008-07-24 12:27:36 +03002296 *
Ajay Kumar Gupta1e0320f2009-02-24 15:26:13 -08002297 * The upside of disabling it is simpler transfer scheduling.
Felipe Balbi550a7372008-07-24 12:27:36 +03002298 */
2299 interval = 0;
2300 }
2301 qh->intv_reg = interval;
2302
2303 /* precompute addressing for external hub/tt ports */
2304 if (musb->is_multipoint) {
2305 struct usb_device *parent = urb->dev->parent;
2306
2307 if (parent != hcd->self.root_hub) {
2308 qh->h_addr_reg = (u8) parent->devnum;
2309
2310 /* set up tt info if needed */
2311 if (urb->dev->tt) {
2312 qh->h_port_reg = (u8) urb->dev->ttport;
Ajay Kumar Guptaae5ad292008-09-11 11:53:20 +03002313 if (urb->dev->tt->hub)
2314 qh->h_addr_reg =
2315 (u8) urb->dev->tt->hub->devnum;
2316 if (urb->dev->tt->multi)
2317 qh->h_addr_reg |= 0x80;
Felipe Balbi550a7372008-07-24 12:27:36 +03002318 }
2319 }
2320 }
2321
2322 /* invariant: hep->hcpriv is null OR the qh that's already scheduled.
2323 * until we get real dma queues (with an entry for each urb/buffer),
2324 * we only have work to do in the former case.
2325 */
2326 spin_lock_irqsave(&musb->lock, flags);
yuzheng ma30677792012-08-15 16:11:40 +08002327 if (hep->hcpriv || !next_urb(qh)) {
Felipe Balbi550a7372008-07-24 12:27:36 +03002328 /* some concurrent activity submitted another urb to hep...
2329 * odd, rare, error prone, but legal.
2330 */
2331 kfree(qh);
Dan Carpenter714bc5e2010-03-25 13:14:27 +02002332 qh = NULL;
Felipe Balbi550a7372008-07-24 12:27:36 +03002333 ret = 0;
2334 } else
2335 ret = musb_schedule(musb, qh,
2336 epd->bEndpointAddress & USB_ENDPOINT_DIR_MASK);
2337
2338 if (ret == 0) {
2339 urb->hcpriv = qh;
2340 /* FIXME set urb->start_frame for iso/intr, it's tested in
2341 * musb_start_urb(), but otherwise only konicawc cares ...
2342 */
2343 }
2344 spin_unlock_irqrestore(&musb->lock, flags);
2345
2346done:
2347 if (ret != 0) {
Ajay Kumar Gupta2492e672008-09-11 11:53:21 +03002348 spin_lock_irqsave(&musb->lock, flags);
Felipe Balbi550a7372008-07-24 12:27:36 +03002349 usb_hcd_unlink_urb_from_ep(hcd, urb);
Ajay Kumar Gupta2492e672008-09-11 11:53:21 +03002350 spin_unlock_irqrestore(&musb->lock, flags);
Felipe Balbi550a7372008-07-24 12:27:36 +03002351 kfree(qh);
2352 }
2353 return ret;
2354}
2355
2356
2357/*
2358 * abort a transfer that's at the head of a hardware queue.
2359 * called with controller locked, irqs blocked
2360 * that hardware queue advances to the next transfer, unless prevented
2361 */
Sergei Shtylyov81ec4e42009-03-27 12:57:50 -07002362static int musb_cleanup_urb(struct urb *urb, struct musb_qh *qh)
Felipe Balbi550a7372008-07-24 12:27:36 +03002363{
2364 struct musb_hw_ep *ep = qh->hw_ep;
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03002365 struct musb *musb = ep->musb;
Felipe Balbi550a7372008-07-24 12:27:36 +03002366 void __iomem *epio = ep->regs;
2367 unsigned hw_end = ep->epnum;
2368 void __iomem *regs = ep->musb->mregs;
Sergei Shtylyov81ec4e42009-03-27 12:57:50 -07002369 int is_in = usb_pipein(urb->pipe);
Felipe Balbi550a7372008-07-24 12:27:36 +03002370 int status = 0;
Sergei Shtylyov81ec4e42009-03-27 12:57:50 -07002371 u16 csr;
Bin Liu5de2dd72017-01-03 18:13:46 -06002372 struct dma_channel *dma = NULL;
Felipe Balbi550a7372008-07-24 12:27:36 +03002373
2374 musb_ep_select(regs, hw_end);
2375
2376 if (is_dma_capable()) {
Felipe Balbi550a7372008-07-24 12:27:36 +03002377 dma = is_in ? ep->rx_channel : ep->tx_channel;
2378 if (dma) {
2379 status = ep->musb->dma_controller->channel_abort(dma);
Bin Liub99d3652016-06-30 12:12:22 -05002380 musb_dbg(musb, "abort %cX%d DMA for urb %p --> %d",
Felipe Balbi550a7372008-07-24 12:27:36 +03002381 is_in ? 'R' : 'T', ep->epnum,
2382 urb, status);
2383 urb->actual_length += dma->actual_len;
2384 }
2385 }
2386
2387 /* turn off DMA requests, discard state, stop polling ... */
Ajay Kumar Gupta692933b2012-03-14 17:33:35 +05302388 if (ep->epnum && is_in) {
Felipe Balbi550a7372008-07-24 12:27:36 +03002389 /* giveback saves bulk toggle */
2390 csr = musb_h_flush_rxfifo(ep, 0);
2391
Bin Liu5de2dd72017-01-03 18:13:46 -06002392 /* clear the endpoint's irq status here to avoid bogus irqs */
2393 if (is_dma_capable() && dma)
2394 musb_platform_clear_ep_rxintr(musb, ep->epnum);
David Brownell78322c12009-03-26 17:38:30 -07002395 } else if (ep->epnum) {
Felipe Balbi550a7372008-07-24 12:27:36 +03002396 musb_h_tx_flush_fifo(ep);
2397 csr = musb_readw(epio, MUSB_TXCSR);
2398 csr &= ~(MUSB_TXCSR_AUTOSET
2399 | MUSB_TXCSR_DMAENAB
2400 | MUSB_TXCSR_H_RXSTALL
2401 | MUSB_TXCSR_H_NAKTIMEOUT
2402 | MUSB_TXCSR_H_ERROR
2403 | MUSB_TXCSR_TXPKTRDY);
2404 musb_writew(epio, MUSB_TXCSR, csr);
2405 /* REVISIT may need to clear FLUSHFIFO ... */
2406 musb_writew(epio, MUSB_TXCSR, csr);
2407 /* flush cpu writebuffer */
2408 csr = musb_readw(epio, MUSB_TXCSR);
David Brownell78322c12009-03-26 17:38:30 -07002409 } else {
2410 musb_h_ep0_flush_fifo(ep);
Felipe Balbi550a7372008-07-24 12:27:36 +03002411 }
2412 if (status == 0)
2413 musb_advance_schedule(ep->musb, urb, ep, is_in);
2414 return status;
2415}
2416
2417static int musb_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
2418{
2419 struct musb *musb = hcd_to_musb(hcd);
2420 struct musb_qh *qh;
Felipe Balbi550a7372008-07-24 12:27:36 +03002421 unsigned long flags;
Sergei Shtylyov22a0d6f2009-03-27 12:56:26 -07002422 int is_in = usb_pipein(urb->pipe);
Felipe Balbi550a7372008-07-24 12:27:36 +03002423 int ret;
2424
Bin Liu19ca6822016-06-30 12:12:26 -05002425 trace_musb_urb_deq(musb, urb);
Felipe Balbi550a7372008-07-24 12:27:36 +03002426
2427 spin_lock_irqsave(&musb->lock, flags);
2428 ret = usb_hcd_check_unlink_urb(hcd, urb, status);
2429 if (ret)
2430 goto done;
2431
2432 qh = urb->hcpriv;
2433 if (!qh)
2434 goto done;
2435
Sergei Shtylyov22a0d6f2009-03-27 12:56:26 -07002436 /*
2437 * Any URB not actively programmed into endpoint hardware can be
Sergei Shtylyova2fd8142009-02-21 15:30:45 -08002438 * immediately given back; that's any URB not at the head of an
Felipe Balbi550a7372008-07-24 12:27:36 +03002439 * endpoint queue, unless someday we get real DMA queues. And even
Sergei Shtylyova2fd8142009-02-21 15:30:45 -08002440 * if it's at the head, it might not be known to the hardware...
Felipe Balbi550a7372008-07-24 12:27:36 +03002441 *
Sergei Shtylyov22a0d6f2009-03-27 12:56:26 -07002442 * Otherwise abort current transfer, pending DMA, etc.; urb->status
Felipe Balbi550a7372008-07-24 12:27:36 +03002443 * has already been updated. This is a synchronous abort; it'd be
2444 * OK to hold off until after some IRQ, though.
Sergei Shtylyov22a0d6f2009-03-27 12:56:26 -07002445 *
2446 * NOTE: qh is invalid unless !list_empty(&hep->urb_list)
Felipe Balbi550a7372008-07-24 12:27:36 +03002447 */
Sergei Shtylyov22a0d6f2009-03-27 12:56:26 -07002448 if (!qh->is_ready
2449 || urb->urb_list.prev != &qh->hep->urb_list
2450 || musb_ep_get_qh(qh->hw_ep, is_in) != qh) {
Felipe Balbi550a7372008-07-24 12:27:36 +03002451 int ready = qh->is_ready;
2452
Felipe Balbi550a7372008-07-24 12:27:36 +03002453 qh->is_ready = 0;
Sergei Shtylyovc9cd06b2009-03-27 12:58:31 -07002454 musb_giveback(musb, urb, 0);
Felipe Balbi550a7372008-07-24 12:27:36 +03002455 qh->is_ready = ready;
Sergei Shtylyova2fd8142009-02-21 15:30:45 -08002456
2457 /* If nothing else (usually musb_giveback) is using it
2458 * and its URB list has emptied, recycle this qh.
2459 */
2460 if (ready && list_empty(&qh->hep->urb_list)) {
2461 qh->hep->hcpriv = NULL;
2462 list_del(&qh->ring);
2463 kfree(qh);
2464 }
Felipe Balbi550a7372008-07-24 12:27:36 +03002465 } else
Sergei Shtylyov81ec4e42009-03-27 12:57:50 -07002466 ret = musb_cleanup_urb(urb, qh);
Felipe Balbi550a7372008-07-24 12:27:36 +03002467done:
2468 spin_unlock_irqrestore(&musb->lock, flags);
2469 return ret;
2470}
2471
2472/* disable an endpoint */
2473static void
2474musb_h_disable(struct usb_hcd *hcd, struct usb_host_endpoint *hep)
2475{
Sergei Shtylyov22a0d6f2009-03-27 12:56:26 -07002476 u8 is_in = hep->desc.bEndpointAddress & USB_DIR_IN;
Felipe Balbi550a7372008-07-24 12:27:36 +03002477 unsigned long flags;
2478 struct musb *musb = hcd_to_musb(hcd);
Sergei Shtylyovdc61d232009-02-21 15:31:01 -08002479 struct musb_qh *qh;
2480 struct urb *urb;
Felipe Balbi550a7372008-07-24 12:27:36 +03002481
Felipe Balbi550a7372008-07-24 12:27:36 +03002482 spin_lock_irqsave(&musb->lock, flags);
2483
Sergei Shtylyovdc61d232009-02-21 15:31:01 -08002484 qh = hep->hcpriv;
2485 if (qh == NULL)
2486 goto exit;
2487
Sergei Shtylyov22a0d6f2009-03-27 12:56:26 -07002488 /* NOTE: qh is invalid unless !list_empty(&hep->urb_list) */
Felipe Balbi550a7372008-07-24 12:27:36 +03002489
Sergei Shtylyov22a0d6f2009-03-27 12:56:26 -07002490 /* Kick the first URB off the hardware, if needed */
Felipe Balbi550a7372008-07-24 12:27:36 +03002491 qh->is_ready = 0;
Sergei Shtylyov22a0d6f2009-03-27 12:56:26 -07002492 if (musb_ep_get_qh(qh->hw_ep, is_in) == qh) {
Felipe Balbi550a7372008-07-24 12:27:36 +03002493 urb = next_urb(qh);
2494
2495 /* make software (then hardware) stop ASAP */
2496 if (!urb->unlinked)
2497 urb->status = -ESHUTDOWN;
2498
2499 /* cleanup */
Sergei Shtylyov81ec4e42009-03-27 12:57:50 -07002500 musb_cleanup_urb(urb, qh);
Felipe Balbi550a7372008-07-24 12:27:36 +03002501
Sergei Shtylyovdc61d232009-02-21 15:31:01 -08002502 /* Then nuke all the others ... and advance the
2503 * queue on hw_ep (e.g. bulk ring) when we're done.
2504 */
2505 while (!list_empty(&hep->urb_list)) {
2506 urb = next_urb(qh);
2507 urb->status = -ESHUTDOWN;
2508 musb_advance_schedule(musb, urb, qh->hw_ep, is_in);
2509 }
2510 } else {
2511 /* Just empty the queue; the hardware is busy with
2512 * other transfers, and since !qh->is_ready nothing
2513 * will activate any of these as it advances.
2514 */
2515 while (!list_empty(&hep->urb_list))
Sergei Shtylyovc9cd06b2009-03-27 12:58:31 -07002516 musb_giveback(musb, next_urb(qh), -ESHUTDOWN);
Felipe Balbi550a7372008-07-24 12:27:36 +03002517
Sergei Shtylyovdc61d232009-02-21 15:31:01 -08002518 hep->hcpriv = NULL;
2519 list_del(&qh->ring);
2520 kfree(qh);
2521 }
2522exit:
Felipe Balbi550a7372008-07-24 12:27:36 +03002523 spin_unlock_irqrestore(&musb->lock, flags);
2524}
2525
2526static int musb_h_get_frame_number(struct usb_hcd *hcd)
2527{
2528 struct musb *musb = hcd_to_musb(hcd);
2529
2530 return musb_readw(musb->mregs, MUSB_FRAME);
2531}
2532
2533static int musb_h_start(struct usb_hcd *hcd)
2534{
2535 struct musb *musb = hcd_to_musb(hcd);
2536
2537 /* NOTE: musb_start() is called when the hub driver turns
2538 * on port power, or when (OTG) peripheral starts.
2539 */
2540 hcd->state = HC_STATE_RUNNING;
2541 musb->port1_status = 0;
2542 return 0;
2543}
2544
2545static void musb_h_stop(struct usb_hcd *hcd)
2546{
2547 musb_stop(hcd_to_musb(hcd));
2548 hcd->state = HC_STATE_HALT;
2549}
2550
2551static int musb_bus_suspend(struct usb_hcd *hcd)
2552{
2553 struct musb *musb = hcd_to_musb(hcd);
David Brownell89368d32009-07-01 03:36:16 -07002554 u8 devctl;
Felipe Balbi550a7372008-07-24 12:27:36 +03002555
Daniel Mack94f72132013-11-25 22:26:41 +01002556 musb_port_suspend(musb, true);
2557
David Brownell89368d32009-07-01 03:36:16 -07002558 if (!is_host_active(musb))
Felipe Balbi550a7372008-07-24 12:27:36 +03002559 return 0;
2560
Antoine Tenarte47d9252014-10-30 18:41:13 +01002561 switch (musb->xceiv->otg->state) {
David Brownell89368d32009-07-01 03:36:16 -07002562 case OTG_STATE_A_SUSPEND:
2563 return 0;
2564 case OTG_STATE_A_WAIT_VRISE:
2565 /* ID could be grounded even if there's no device
2566 * on the other end of the cable. NOTE that the
2567 * A_WAIT_VRISE timers are messy with MUSB...
2568 */
2569 devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
2570 if ((devctl & MUSB_DEVCTL_VBUS) == MUSB_DEVCTL_VBUS)
Antoine Tenarte47d9252014-10-30 18:41:13 +01002571 musb->xceiv->otg->state = OTG_STATE_A_WAIT_BCON;
David Brownell89368d32009-07-01 03:36:16 -07002572 break;
2573 default:
2574 break;
2575 }
2576
2577 if (musb->is_active) {
2578 WARNING("trying to suspend as %s while active\n",
Antoine Tenarte47d9252014-10-30 18:41:13 +01002579 usb_otg_state_string(musb->xceiv->otg->state));
Felipe Balbi550a7372008-07-24 12:27:36 +03002580 return -EBUSY;
2581 } else
2582 return 0;
2583}
2584
2585static int musb_bus_resume(struct usb_hcd *hcd)
2586{
Daniel Mack869c5972013-11-26 13:31:14 +01002587 struct musb *musb = hcd_to_musb(hcd);
2588
2589 if (musb->config &&
2590 musb->config->host_port_deassert_reset_at_resume)
2591 musb_port_reset(musb, false);
2592
Felipe Balbi550a7372008-07-24 12:27:36 +03002593 return 0;
2594}
2595
Ruslan Bilovol8408fd12013-03-29 19:15:21 +02002596#ifndef CONFIG_MUSB_PIO_ONLY
2597
2598#define MUSB_USB_DMA_ALIGN 4
2599
2600struct musb_temp_buffer {
2601 void *kmalloc_ptr;
2602 void *old_xfer_buffer;
2603 u8 data[0];
2604};
2605
2606static void musb_free_temp_buffer(struct urb *urb)
2607{
2608 enum dma_data_direction dir;
2609 struct musb_temp_buffer *temp;
Johan Hovoldd72348f2015-04-23 16:06:50 +02002610 size_t length;
Ruslan Bilovol8408fd12013-03-29 19:15:21 +02002611
2612 if (!(urb->transfer_flags & URB_ALIGNED_TEMP_BUFFER))
2613 return;
2614
2615 dir = usb_urb_dir_in(urb) ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
2616
2617 temp = container_of(urb->transfer_buffer, struct musb_temp_buffer,
2618 data);
2619
2620 if (dir == DMA_FROM_DEVICE) {
Johan Hovoldd72348f2015-04-23 16:06:50 +02002621 if (usb_pipeisoc(urb->pipe))
2622 length = urb->transfer_buffer_length;
2623 else
2624 length = urb->actual_length;
2625
2626 memcpy(temp->old_xfer_buffer, temp->data, length);
Ruslan Bilovol8408fd12013-03-29 19:15:21 +02002627 }
2628 urb->transfer_buffer = temp->old_xfer_buffer;
2629 kfree(temp->kmalloc_ptr);
2630
2631 urb->transfer_flags &= ~URB_ALIGNED_TEMP_BUFFER;
2632}
2633
2634static int musb_alloc_temp_buffer(struct urb *urb, gfp_t mem_flags)
2635{
2636 enum dma_data_direction dir;
2637 struct musb_temp_buffer *temp;
2638 void *kmalloc_ptr;
2639 size_t kmalloc_size;
2640
2641 if (urb->num_sgs || urb->sg ||
2642 urb->transfer_buffer_length == 0 ||
2643 !((uintptr_t)urb->transfer_buffer & (MUSB_USB_DMA_ALIGN - 1)))
2644 return 0;
2645
2646 dir = usb_urb_dir_in(urb) ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
2647
2648 /* Allocate a buffer with enough padding for alignment */
2649 kmalloc_size = urb->transfer_buffer_length +
2650 sizeof(struct musb_temp_buffer) + MUSB_USB_DMA_ALIGN - 1;
2651
2652 kmalloc_ptr = kmalloc(kmalloc_size, mem_flags);
2653 if (!kmalloc_ptr)
2654 return -ENOMEM;
2655
2656 /* Position our struct temp_buffer such that data is aligned */
2657 temp = PTR_ALIGN(kmalloc_ptr, MUSB_USB_DMA_ALIGN);
2658
2659
2660 temp->kmalloc_ptr = kmalloc_ptr;
2661 temp->old_xfer_buffer = urb->transfer_buffer;
2662 if (dir == DMA_TO_DEVICE)
2663 memcpy(temp->data, urb->transfer_buffer,
2664 urb->transfer_buffer_length);
2665 urb->transfer_buffer = temp->data;
2666
2667 urb->transfer_flags |= URB_ALIGNED_TEMP_BUFFER;
2668
2669 return 0;
2670}
2671
2672static int musb_map_urb_for_dma(struct usb_hcd *hcd, struct urb *urb,
2673 gfp_t mem_flags)
2674{
2675 struct musb *musb = hcd_to_musb(hcd);
2676 int ret;
2677
2678 /*
2679 * The DMA engine in RTL1.8 and above cannot handle
2680 * DMA addresses that are not aligned to a 4 byte boundary.
2681 * For such engine implemented (un)map_urb_for_dma hooks.
2682 * Do not use these hooks for RTL<1.8
2683 */
2684 if (musb->hwvers < MUSB_HWVERS_1800)
2685 return usb_hcd_map_urb_for_dma(hcd, urb, mem_flags);
2686
2687 ret = musb_alloc_temp_buffer(urb, mem_flags);
2688 if (ret)
2689 return ret;
2690
2691 ret = usb_hcd_map_urb_for_dma(hcd, urb, mem_flags);
2692 if (ret)
2693 musb_free_temp_buffer(urb);
2694
2695 return ret;
2696}
2697
2698static void musb_unmap_urb_for_dma(struct usb_hcd *hcd, struct urb *urb)
2699{
2700 struct musb *musb = hcd_to_musb(hcd);
2701
2702 usb_hcd_unmap_urb_for_dma(hcd, urb);
2703
2704 /* Do not use this hook for RTL<1.8 (see description above) */
2705 if (musb->hwvers < MUSB_HWVERS_1800)
2706 return;
2707
2708 musb_free_temp_buffer(urb);
2709}
2710#endif /* !CONFIG_MUSB_PIO_ONLY */
2711
Daniel Mack74c2e932013-04-10 21:55:45 +02002712static const struct hc_driver musb_hc_driver = {
Felipe Balbi550a7372008-07-24 12:27:36 +03002713 .description = "musb-hcd",
2714 .product_desc = "MUSB HDRC host driver",
Daniel Mack74c2e932013-04-10 21:55:45 +02002715 .hcd_priv_size = sizeof(struct musb *),
Bin Liuf551e132016-04-25 15:53:30 -05002716 .flags = HCD_USB2 | HCD_MEMORY,
Felipe Balbi550a7372008-07-24 12:27:36 +03002717
2718 /* not using irq handler or reset hooks from usbcore, since
2719 * those must be shared with peripheral code for OTG configs
2720 */
2721
2722 .start = musb_h_start,
2723 .stop = musb_h_stop,
2724
2725 .get_frame_number = musb_h_get_frame_number,
2726
2727 .urb_enqueue = musb_urb_enqueue,
2728 .urb_dequeue = musb_urb_dequeue,
2729 .endpoint_disable = musb_h_disable,
2730
Ruslan Bilovol8408fd12013-03-29 19:15:21 +02002731#ifndef CONFIG_MUSB_PIO_ONLY
2732 .map_urb_for_dma = musb_map_urb_for_dma,
2733 .unmap_urb_for_dma = musb_unmap_urb_for_dma,
2734#endif
2735
Felipe Balbi550a7372008-07-24 12:27:36 +03002736 .hub_status_data = musb_hub_status_data,
2737 .hub_control = musb_hub_control,
2738 .bus_suspend = musb_bus_suspend,
2739 .bus_resume = musb_bus_resume,
2740 /* .start_port_reset = NULL, */
2741 /* .hub_irq_enable = NULL, */
2742};
Daniel Mack0b3eba42013-04-10 21:55:42 +02002743
Daniel Mack74c2e932013-04-10 21:55:45 +02002744int musb_host_alloc(struct musb *musb)
2745{
2746 struct device *dev = musb->controller;
2747
2748 /* usbcore sets dev->driver_data to hcd, and sometimes uses that... */
2749 musb->hcd = usb_create_hcd(&musb_hc_driver, dev, dev_name(dev));
2750 if (!musb->hcd)
2751 return -EINVAL;
2752
2753 *musb->hcd->hcd_priv = (unsigned long) musb;
2754 musb->hcd->self.uses_pio_for_control = 1;
2755 musb->hcd->uses_new_polling = 1;
2756 musb->hcd->has_tt = 1;
2757
2758 return 0;
2759}
2760
2761void musb_host_cleanup(struct musb *musb)
2762{
Sebastian Andrzej Siewior90474282013-08-20 18:35:44 +02002763 if (musb->port_mode == MUSB_PORT_MODE_GADGET)
2764 return;
Daniel Mack74c2e932013-04-10 21:55:45 +02002765 usb_remove_hcd(musb->hcd);
Daniel Mack74c2e932013-04-10 21:55:45 +02002766}
2767
2768void musb_host_free(struct musb *musb)
2769{
2770 usb_put_hcd(musb->hcd);
2771}
2772
Daniel Mack2cc65fe2013-04-10 21:55:47 +02002773int musb_host_setup(struct musb *musb, int power_budget)
2774{
2775 int ret;
2776 struct usb_hcd *hcd = musb->hcd;
2777
Tony Lindgrendbb12732017-05-17 11:23:10 -05002778 if (musb->port_mode == MUSB_PORT_MODE_HOST) {
2779 MUSB_HST_MODE(musb);
2780 musb->xceiv->otg->default_a = 1;
2781 musb->xceiv->otg->state = OTG_STATE_A_IDLE;
2782 }
Daniel Mack2cc65fe2013-04-10 21:55:47 +02002783 otg_set_host(musb->xceiv->otg, &hcd->self);
2784 hcd->self.otg_port = 1;
2785 musb->xceiv->otg->host = &hcd->self;
2786 hcd->power_budget = 2 * (power_budget ? : 250);
2787
2788 ret = usb_add_hcd(hcd, 0, 0);
2789 if (ret < 0)
2790 return ret;
2791
Peter Chen3c9740a2013-11-05 10:46:02 +08002792 device_wakeup_enable(hcd->self.controller);
Daniel Mack2cc65fe2013-04-10 21:55:47 +02002793 return 0;
2794}
2795
Daniel Mack0b3eba42013-04-10 21:55:42 +02002796void musb_host_resume_root_hub(struct musb *musb)
2797{
Daniel Mack74c2e932013-04-10 21:55:45 +02002798 usb_hcd_resume_root_hub(musb->hcd);
Daniel Mack0b3eba42013-04-10 21:55:42 +02002799}
2800
2801void musb_host_poke_root_hub(struct musb *musb)
2802{
2803 MUSB_HST_MODE(musb);
Daniel Mack74c2e932013-04-10 21:55:45 +02002804 if (musb->hcd->status_urb)
2805 usb_hcd_poll_rh_status(musb->hcd);
Daniel Mack0b3eba42013-04-10 21:55:42 +02002806 else
Daniel Mack74c2e932013-04-10 21:55:45 +02002807 usb_hcd_resume_root_hub(musb->hcd);
Daniel Mack0b3eba42013-04-10 21:55:42 +02002808}