blob: 01ef25bd37e3882c3448d54a2b7aa10b36481767 [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 }
Bin Liu9f3ac2e2018-04-30 11:20:53 -05001026
1027 if (next_qh)
1028 musb_start_urb(musb, is_in, next_qh);
Ajay Kumar Guptaf2838622012-07-19 13:41:59 +05301029 }
1030}
Felipe Balbi550a7372008-07-24 12:27:36 +03001031
1032/*
1033 * Service the default endpoint (ep0) as host.
1034 * Return true until it's time to start the status stage.
1035 */
1036static bool musb_h_ep0_continue(struct musb *musb, u16 len, struct urb *urb)
1037{
1038 bool more = false;
1039 u8 *fifo_dest = NULL;
1040 u16 fifo_count = 0;
1041 struct musb_hw_ep *hw_ep = musb->control_ep;
1042 struct musb_qh *qh = hw_ep->in_qh;
1043 struct usb_ctrlrequest *request;
1044
1045 switch (musb->ep0_stage) {
1046 case MUSB_EP0_IN:
1047 fifo_dest = urb->transfer_buffer + urb->actual_length;
Sergei Shtylyov3ecdb9a2009-02-21 15:31:23 -08001048 fifo_count = min_t(size_t, len, urb->transfer_buffer_length -
1049 urb->actual_length);
Felipe Balbi550a7372008-07-24 12:27:36 +03001050 if (fifo_count < len)
1051 urb->status = -EOVERFLOW;
1052
1053 musb_read_fifo(hw_ep, fifo_count, fifo_dest);
1054
1055 urb->actual_length += fifo_count;
1056 if (len < qh->maxpacket) {
1057 /* always terminate on short read; it's
1058 * rarely reported as an error.
1059 */
1060 } else if (urb->actual_length <
1061 urb->transfer_buffer_length)
1062 more = true;
1063 break;
1064 case MUSB_EP0_START:
1065 request = (struct usb_ctrlrequest *) urb->setup_packet;
1066
1067 if (!request->wLength) {
Bin Liub99d3652016-06-30 12:12:22 -05001068 musb_dbg(musb, "start no-DATA");
Felipe Balbi550a7372008-07-24 12:27:36 +03001069 break;
1070 } else if (request->bRequestType & USB_DIR_IN) {
Bin Liub99d3652016-06-30 12:12:22 -05001071 musb_dbg(musb, "start IN-DATA");
Felipe Balbi550a7372008-07-24 12:27:36 +03001072 musb->ep0_stage = MUSB_EP0_IN;
1073 more = true;
1074 break;
1075 } else {
Bin Liub99d3652016-06-30 12:12:22 -05001076 musb_dbg(musb, "start OUT-DATA");
Felipe Balbi550a7372008-07-24 12:27:36 +03001077 musb->ep0_stage = MUSB_EP0_OUT;
1078 more = true;
1079 }
1080 /* FALLTHROUGH */
1081 case MUSB_EP0_OUT:
Sergei Shtylyov3ecdb9a2009-02-21 15:31:23 -08001082 fifo_count = min_t(size_t, qh->maxpacket,
1083 urb->transfer_buffer_length -
1084 urb->actual_length);
Felipe Balbi550a7372008-07-24 12:27:36 +03001085 if (fifo_count) {
1086 fifo_dest = (u8 *) (urb->transfer_buffer
1087 + urb->actual_length);
Bin Liub99d3652016-06-30 12:12:22 -05001088 musb_dbg(musb, "Sending %d byte%s to ep0 fifo %p",
David Brownellbb1c9ef2008-11-24 13:06:50 +02001089 fifo_count,
1090 (fifo_count == 1) ? "" : "s",
1091 fifo_dest);
Felipe Balbi550a7372008-07-24 12:27:36 +03001092 musb_write_fifo(hw_ep, fifo_count, fifo_dest);
1093
1094 urb->actual_length += fifo_count;
1095 more = true;
1096 }
1097 break;
1098 default:
1099 ERR("bogus ep0 stage %d\n", musb->ep0_stage);
1100 break;
1101 }
1102
1103 return more;
1104}
1105
1106/*
1107 * Handle default endpoint interrupt as host. Only called in IRQ time
David Brownellc767c1c2008-09-11 11:53:23 +03001108 * from musb_interrupt().
Felipe Balbi550a7372008-07-24 12:27:36 +03001109 *
1110 * called with controller irqlocked
1111 */
1112irqreturn_t musb_h_ep0_irq(struct musb *musb)
1113{
1114 struct urb *urb;
1115 u16 csr, len;
1116 int status = 0;
1117 void __iomem *mbase = musb->mregs;
1118 struct musb_hw_ep *hw_ep = musb->control_ep;
1119 void __iomem *epio = hw_ep->regs;
1120 struct musb_qh *qh = hw_ep->in_qh;
1121 bool complete = false;
1122 irqreturn_t retval = IRQ_NONE;
1123
1124 /* ep0 only has one queue, "in" */
1125 urb = next_urb(qh);
1126
1127 musb_ep_select(mbase, 0);
1128 csr = musb_readw(epio, MUSB_CSR0);
1129 len = (csr & MUSB_CSR0_RXPKTRDY)
1130 ? musb_readb(epio, MUSB_COUNT0)
1131 : 0;
1132
Bin Liub99d3652016-06-30 12:12:22 -05001133 musb_dbg(musb, "<== csr0 %04x, qh %p, count %d, urb %p, stage %d",
Felipe Balbi550a7372008-07-24 12:27:36 +03001134 csr, qh, len, urb, musb->ep0_stage);
1135
1136 /* if we just did status stage, we are done */
1137 if (MUSB_EP0_STATUS == musb->ep0_stage) {
1138 retval = IRQ_HANDLED;
1139 complete = true;
1140 }
1141
1142 /* prepare status */
1143 if (csr & MUSB_CSR0_H_RXSTALL) {
Bin Liub99d3652016-06-30 12:12:22 -05001144 musb_dbg(musb, "STALLING ENDPOINT");
Felipe Balbi550a7372008-07-24 12:27:36 +03001145 status = -EPIPE;
1146
1147 } else if (csr & MUSB_CSR0_H_ERROR) {
Bin Liub99d3652016-06-30 12:12:22 -05001148 musb_dbg(musb, "no response, csr0 %04x", csr);
Felipe Balbi550a7372008-07-24 12:27:36 +03001149 status = -EPROTO;
1150
1151 } else if (csr & MUSB_CSR0_H_NAKTIMEOUT) {
Bin Liub99d3652016-06-30 12:12:22 -05001152 musb_dbg(musb, "control NAK timeout");
Felipe Balbi550a7372008-07-24 12:27:36 +03001153
1154 /* NOTE: this code path would be a good place to PAUSE a
1155 * control transfer, if another one is queued, so that
Ajay Kumar Gupta1e0320f2009-02-24 15:26:13 -08001156 * ep0 is more likely to stay busy. That's already done
1157 * for bulk RX transfers.
Felipe Balbi550a7372008-07-24 12:27:36 +03001158 *
1159 * if (qh->ring.next != &musb->control), then
1160 * we have a candidate... NAKing is *NOT* an error
1161 */
1162 musb_writew(epio, MUSB_CSR0, 0);
1163 retval = IRQ_HANDLED;
1164 }
1165
1166 if (status) {
Bin Liub99d3652016-06-30 12:12:22 -05001167 musb_dbg(musb, "aborting");
Felipe Balbi550a7372008-07-24 12:27:36 +03001168 retval = IRQ_HANDLED;
1169 if (urb)
1170 urb->status = status;
1171 complete = true;
1172
1173 /* use the proper sequence to abort the transfer */
1174 if (csr & MUSB_CSR0_H_REQPKT) {
1175 csr &= ~MUSB_CSR0_H_REQPKT;
1176 musb_writew(epio, MUSB_CSR0, csr);
1177 csr &= ~MUSB_CSR0_H_NAKTIMEOUT;
1178 musb_writew(epio, MUSB_CSR0, csr);
1179 } else {
David Brownell78322c12009-03-26 17:38:30 -07001180 musb_h_ep0_flush_fifo(hw_ep);
Felipe Balbi550a7372008-07-24 12:27:36 +03001181 }
1182
1183 musb_writeb(epio, MUSB_NAKLIMIT0, 0);
1184
1185 /* clear it */
1186 musb_writew(epio, MUSB_CSR0, 0);
1187 }
1188
1189 if (unlikely(!urb)) {
1190 /* stop endpoint since we have no place for its data, this
1191 * SHOULD NEVER HAPPEN! */
1192 ERR("no URB for end 0\n");
1193
David Brownell78322c12009-03-26 17:38:30 -07001194 musb_h_ep0_flush_fifo(hw_ep);
Felipe Balbi550a7372008-07-24 12:27:36 +03001195 goto done;
1196 }
1197
1198 if (!complete) {
1199 /* call common logic and prepare response */
1200 if (musb_h_ep0_continue(musb, len, urb)) {
1201 /* more packets required */
1202 csr = (MUSB_EP0_IN == musb->ep0_stage)
1203 ? MUSB_CSR0_H_REQPKT : MUSB_CSR0_TXPKTRDY;
1204 } else {
1205 /* data transfer complete; perform status phase */
1206 if (usb_pipeout(urb->pipe)
1207 || !urb->transfer_buffer_length)
1208 csr = MUSB_CSR0_H_STATUSPKT
1209 | MUSB_CSR0_H_REQPKT;
1210 else
1211 csr = MUSB_CSR0_H_STATUSPKT
1212 | MUSB_CSR0_TXPKTRDY;
1213
Ajay Kumar Gupta3c4653c2014-02-04 15:28:06 +02001214 /* disable ping token in status phase */
1215 csr |= MUSB_CSR0_H_DIS_PING;
1216
Felipe Balbi550a7372008-07-24 12:27:36 +03001217 /* flag status stage */
1218 musb->ep0_stage = MUSB_EP0_STATUS;
1219
Bin Liub99d3652016-06-30 12:12:22 -05001220 musb_dbg(musb, "ep0 STATUS, csr %04x", csr);
Felipe Balbi550a7372008-07-24 12:27:36 +03001221
1222 }
1223 musb_writew(epio, MUSB_CSR0, csr);
1224 retval = IRQ_HANDLED;
1225 } else
1226 musb->ep0_stage = MUSB_EP0_IDLE;
1227
1228 /* call completion handler if done */
1229 if (complete)
1230 musb_advance_schedule(musb, urb, hw_ep, 1);
1231done:
1232 return retval;
1233}
1234
1235
1236#ifdef CONFIG_USB_INVENTRA_DMA
1237
1238/* Host side TX (OUT) using Mentor DMA works as follows:
1239 submit_urb ->
1240 - if queue was empty, Program Endpoint
1241 - ... which starts DMA to fifo in mode 1 or 0
1242
1243 DMA Isr (transfer complete) -> TxAvail()
1244 - Stop DMA (~DmaEnab) (<--- Alert ... currently happens
1245 only in musb_cleanup_urb)
1246 - TxPktRdy has to be set in mode 0 or for
1247 short packets in mode 1.
1248*/
1249
1250#endif
1251
1252/* Service a Tx-Available or dma completion irq for the endpoint */
1253void musb_host_tx(struct musb *musb, u8 epnum)
1254{
1255 int pipe;
1256 bool done = false;
1257 u16 tx_csr;
Sergei Shtylyov6b6e9712009-03-26 18:29:19 -07001258 size_t length = 0;
1259 size_t offset = 0;
Felipe Balbi550a7372008-07-24 12:27:36 +03001260 struct musb_hw_ep *hw_ep = musb->endpoints + epnum;
1261 void __iomem *epio = hw_ep->regs;
Sergei Shtylyov3e5c6dc2009-03-27 12:55:16 -07001262 struct musb_qh *qh = hw_ep->out_qh;
1263 struct urb *urb = next_urb(qh);
Felipe Balbi550a7372008-07-24 12:27:36 +03001264 u32 status = 0;
1265 void __iomem *mbase = musb->mregs;
1266 struct dma_channel *dma;
T. S., Anil Kumarf8afbf7f2010-09-24 13:44:09 +03001267 bool transfer_pending = false;
Felipe Balbi550a7372008-07-24 12:27:36 +03001268
Felipe Balbi550a7372008-07-24 12:27:36 +03001269 musb_ep_select(mbase, epnum);
1270 tx_csr = musb_readw(epio, MUSB_TXCSR);
1271
1272 /* with CPPI, DMA sometimes triggers "extra" irqs */
1273 if (!urb) {
Bin Liub99d3652016-06-30 12:12:22 -05001274 musb_dbg(musb, "extra TX%d ready, csr %04x", epnum, tx_csr);
Sergei Shtylyov6b6e9712009-03-26 18:29:19 -07001275 return;
Felipe Balbi550a7372008-07-24 12:27:36 +03001276 }
1277
1278 pipe = urb->pipe;
1279 dma = is_dma_capable() ? hw_ep->tx_channel : NULL;
Bin Liu19ca6822016-06-30 12:12:26 -05001280 trace_musb_urb_tx(musb, urb);
Bin Liub99d3652016-06-30 12:12:22 -05001281 musb_dbg(musb, "OUT/TX%d end, csr %04x%s", epnum, tx_csr,
Felipe Balbi550a7372008-07-24 12:27:36 +03001282 dma ? ", dma" : "");
1283
1284 /* check for errors */
1285 if (tx_csr & MUSB_TXCSR_H_RXSTALL) {
1286 /* dma was disabled, fifo flushed */
Bin Liub99d3652016-06-30 12:12:22 -05001287 musb_dbg(musb, "TX end %d stall", epnum);
Felipe Balbi550a7372008-07-24 12:27:36 +03001288
1289 /* stall; record URB status */
1290 status = -EPIPE;
1291
1292 } else if (tx_csr & MUSB_TXCSR_H_ERROR) {
1293 /* (NON-ISO) dma was disabled, fifo flushed */
Bin Liub99d3652016-06-30 12:12:22 -05001294 musb_dbg(musb, "TX 3strikes on ep=%d", epnum);
Felipe Balbi550a7372008-07-24 12:27:36 +03001295
1296 status = -ETIMEDOUT;
1297
1298 } else if (tx_csr & MUSB_TXCSR_H_NAKTIMEOUT) {
Ajay Kumar Guptaf2838622012-07-19 13:41:59 +05301299 if (USB_ENDPOINT_XFER_BULK == qh->type && qh->mux == 1
1300 && !list_is_singular(&musb->out_bulk)) {
Bin Liub99d3652016-06-30 12:12:22 -05001301 musb_dbg(musb, "NAK timeout on TX%d ep", epnum);
Ajay Kumar Guptaf2838622012-07-19 13:41:59 +05301302 musb_bulk_nak_timeout(musb, hw_ep, 0);
1303 } else {
Bin Liub99d3652016-06-30 12:12:22 -05001304 musb_dbg(musb, "TX ep%d device not responding", epnum);
Ajay Kumar Guptaf2838622012-07-19 13:41:59 +05301305 /* NOTE: this code path would be a good place to PAUSE a
1306 * transfer, if there's some other (nonperiodic) tx urb
1307 * that could use this fifo. (dma complicates it...)
1308 * That's already done for bulk RX transfers.
1309 *
1310 * if (bulk && qh->ring.next != &musb->out_bulk), then
1311 * we have a candidate... NAKing is *NOT* an error
1312 */
1313 musb_ep_select(mbase, epnum);
1314 musb_writew(epio, MUSB_TXCSR,
1315 MUSB_TXCSR_H_WZC_BITS
1316 | MUSB_TXCSR_TXPKTRDY);
1317 }
1318 return;
Felipe Balbi550a7372008-07-24 12:27:36 +03001319 }
1320
Virupax Sadashivpetimath8e8a5512012-08-07 14:46:20 +05301321done:
Felipe Balbi550a7372008-07-24 12:27:36 +03001322 if (status) {
1323 if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) {
1324 dma->status = MUSB_DMA_STATUS_CORE_ABORT;
Daniel Mack9c547692014-05-26 14:52:35 +02001325 musb->dma_controller->channel_abort(dma);
Felipe Balbi550a7372008-07-24 12:27:36 +03001326 }
1327
1328 /* do the proper sequence to abort the transfer in the
1329 * usb core; the dma engine should already be stopped.
1330 */
1331 musb_h_tx_flush_fifo(hw_ep);
1332 tx_csr &= ~(MUSB_TXCSR_AUTOSET
1333 | MUSB_TXCSR_DMAENAB
1334 | MUSB_TXCSR_H_ERROR
1335 | MUSB_TXCSR_H_RXSTALL
1336 | MUSB_TXCSR_H_NAKTIMEOUT
1337 );
1338
1339 musb_ep_select(mbase, epnum);
1340 musb_writew(epio, MUSB_TXCSR, tx_csr);
1341 /* REVISIT may need to clear FLUSHFIFO ... */
1342 musb_writew(epio, MUSB_TXCSR, tx_csr);
1343 musb_writeb(epio, MUSB_TXINTERVAL, 0);
1344
1345 done = true;
1346 }
1347
1348 /* second cppi case */
1349 if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) {
Bin Liub99d3652016-06-30 12:12:22 -05001350 musb_dbg(musb, "extra TX%d ready, csr %04x", epnum, tx_csr);
Sergei Shtylyov6b6e9712009-03-26 18:29:19 -07001351 return;
Felipe Balbi550a7372008-07-24 12:27:36 +03001352 }
1353
Sergei Shtylyovc7bbc052009-03-26 18:26:40 -07001354 if (is_dma_capable() && dma && !status) {
1355 /*
1356 * DMA has completed. But if we're using DMA mode 1 (multi
1357 * packet DMA), we need a terminal TXPKTRDY interrupt before
1358 * we can consider this transfer completed, lest we trash
1359 * its last packet when writing the next URB's data. So we
1360 * switch back to mode 0 to get that interrupt; we'll come
1361 * back here once it happens.
1362 */
1363 if (tx_csr & MUSB_TXCSR_DMAMODE) {
1364 /*
1365 * We shouldn't clear DMAMODE with DMAENAB set; so
1366 * clear them in a safe order. That should be OK
1367 * once TXPKTRDY has been set (and I've never seen
1368 * it being 0 at this moment -- DMA interrupt latency
1369 * is significant) but if it hasn't been then we have
1370 * no choice but to stop being polite and ignore the
1371 * programmer's guide... :-)
1372 *
1373 * Note that we must write TXCSR with TXPKTRDY cleared
1374 * in order not to re-trigger the packet send (this bit
1375 * can't be cleared by CPU), and there's another caveat:
1376 * TXPKTRDY may be set shortly and then cleared in the
1377 * double-buffered FIFO mode, so we do an extra TXCSR
1378 * read for debouncing...
1379 */
1380 tx_csr &= musb_readw(epio, MUSB_TXCSR);
1381 if (tx_csr & MUSB_TXCSR_TXPKTRDY) {
1382 tx_csr &= ~(MUSB_TXCSR_DMAENAB |
1383 MUSB_TXCSR_TXPKTRDY);
1384 musb_writew(epio, MUSB_TXCSR,
1385 tx_csr | MUSB_TXCSR_H_WZC_BITS);
1386 }
1387 tx_csr &= ~(MUSB_TXCSR_DMAMODE |
1388 MUSB_TXCSR_TXPKTRDY);
1389 musb_writew(epio, MUSB_TXCSR,
1390 tx_csr | MUSB_TXCSR_H_WZC_BITS);
1391
1392 /*
1393 * There is no guarantee that we'll get an interrupt
1394 * after clearing DMAMODE as we might have done this
1395 * too late (after TXPKTRDY was cleared by controller).
1396 * Re-read TXCSR as we have spoiled its previous value.
1397 */
1398 tx_csr = musb_readw(epio, MUSB_TXCSR);
1399 }
1400
1401 /*
1402 * We may get here from a DMA completion or TXPKTRDY interrupt.
1403 * In any case, we must check the FIFO status here and bail out
1404 * only if the FIFO still has data -- that should prevent the
1405 * "missed" TXPKTRDY interrupts and deal with double-buffered
1406 * FIFO mode too...
1407 */
1408 if (tx_csr & (MUSB_TXCSR_FIFONOTEMPTY | MUSB_TXCSR_TXPKTRDY)) {
Bin Liub99d3652016-06-30 12:12:22 -05001409 musb_dbg(musb,
1410 "DMA complete but FIFO not empty, CSR %04x",
1411 tx_csr);
Sergei Shtylyovc7bbc052009-03-26 18:26:40 -07001412 return;
1413 }
1414 }
1415
Felipe Balbi550a7372008-07-24 12:27:36 +03001416 if (!status || dma || usb_pipeisoc(pipe)) {
1417 if (dma)
Sergei Shtylyov6b6e9712009-03-26 18:29:19 -07001418 length = dma->actual_len;
Felipe Balbi550a7372008-07-24 12:27:36 +03001419 else
Sergei Shtylyov6b6e9712009-03-26 18:29:19 -07001420 length = qh->segsize;
1421 qh->offset += length;
Felipe Balbi550a7372008-07-24 12:27:36 +03001422
1423 if (usb_pipeisoc(pipe)) {
1424 struct usb_iso_packet_descriptor *d;
1425
1426 d = urb->iso_frame_desc + qh->iso_idx;
Sergei Shtylyov6b6e9712009-03-26 18:29:19 -07001427 d->actual_length = length;
1428 d->status = status;
Felipe Balbi550a7372008-07-24 12:27:36 +03001429 if (++qh->iso_idx >= urb->number_of_packets) {
1430 done = true;
1431 } else {
1432 d++;
Sergei Shtylyov6b6e9712009-03-26 18:29:19 -07001433 offset = d->offset;
1434 length = d->length;
Felipe Balbi550a7372008-07-24 12:27:36 +03001435 }
T. S., Anil Kumarf8afbf7f2010-09-24 13:44:09 +03001436 } else if (dma && urb->transfer_buffer_length == qh->offset) {
Felipe Balbi550a7372008-07-24 12:27:36 +03001437 done = true;
1438 } else {
1439 /* see if we need to send more data, or ZLP */
1440 if (qh->segsize < qh->maxpacket)
1441 done = true;
1442 else if (qh->offset == urb->transfer_buffer_length
1443 && !(urb->transfer_flags
1444 & URB_ZERO_PACKET))
1445 done = true;
1446 if (!done) {
Sergei Shtylyov6b6e9712009-03-26 18:29:19 -07001447 offset = qh->offset;
1448 length = urb->transfer_buffer_length - offset;
T. S., Anil Kumarf8afbf7f2010-09-24 13:44:09 +03001449 transfer_pending = true;
Felipe Balbi550a7372008-07-24 12:27:36 +03001450 }
1451 }
1452 }
1453
1454 /* urb->status != -EINPROGRESS means request has been faulted,
1455 * so we must abort this transfer after cleanup
1456 */
1457 if (urb->status != -EINPROGRESS) {
1458 done = true;
1459 if (status == 0)
1460 status = urb->status;
1461 }
1462
1463 if (done) {
1464 /* set status */
1465 urb->status = status;
1466 urb->actual_length = qh->offset;
1467 musb_advance_schedule(musb, urb, hw_ep, USB_DIR_OUT);
Sergei Shtylyov6b6e9712009-03-26 18:29:19 -07001468 return;
T. S., Anil Kumarf8afbf7f2010-09-24 13:44:09 +03001469 } else if ((usb_pipeisoc(pipe) || transfer_pending) && dma) {
Sergei Shtylyov6b6e9712009-03-26 18:29:19 -07001470 if (musb_tx_dma_program(musb->dma_controller, hw_ep, qh, urb,
Ajay Kumar Guptadfeffa52009-11-17 15:22:55 +05301471 offset, length)) {
Tony Lindgrenf8e9f34f2015-05-01 12:29:27 -07001472 if (is_cppi_enabled(musb) || tusb_dma_omap(musb))
Ajay Kumar Guptadfeffa52009-11-17 15:22:55 +05301473 musb_h_tx_dma_start(hw_ep);
Sergei Shtylyov6b6e9712009-03-26 18:29:19 -07001474 return;
Ajay Kumar Guptadfeffa52009-11-17 15:22:55 +05301475 }
Sergei Shtylyov6b6e9712009-03-26 18:29:19 -07001476 } else if (tx_csr & MUSB_TXCSR_DMAENAB) {
Bin Liub99d3652016-06-30 12:12:22 -05001477 musb_dbg(musb, "not complete, but DMA enabled?");
Sergei Shtylyov6b6e9712009-03-26 18:29:19 -07001478 return;
1479 }
Felipe Balbi550a7372008-07-24 12:27:36 +03001480
Sergei Shtylyov6b6e9712009-03-26 18:29:19 -07001481 /*
1482 * PIO: start next packet in this URB.
1483 *
1484 * REVISIT: some docs say that when hw_ep->tx_double_buffered,
1485 * (and presumably, FIFO is not half-full) we should write *two*
1486 * packets before updating TXCSR; other docs disagree...
1487 */
1488 if (length > qh->maxpacket)
1489 length = qh->maxpacket;
Maulik Mankad496dda72010-09-24 13:44:06 +03001490 /* Unmap the buffer so that CPU can use it */
Daniel Mack8b125df2013-04-10 21:55:50 +02001491 usb_hcd_unmap_urb_for_dma(musb->hcd, urb);
Virupax Sadashivpetimath8e8a5512012-08-07 14:46:20 +05301492
1493 /*
1494 * We need to map sg if the transfer_buffer is
1495 * NULL.
1496 */
Mans Rullgardedc87d92020-03-16 16:11:35 -05001497 if (!urb->transfer_buffer) {
Virupax Sadashivpetimath8e8a5512012-08-07 14:46:20 +05301498 /* sg_miter_start is already done in musb_ep_program */
1499 if (!sg_miter_next(&qh->sg_miter)) {
1500 dev_err(musb->controller, "error: sg list empty\n");
1501 sg_miter_stop(&qh->sg_miter);
1502 status = -EINVAL;
1503 goto done;
1504 }
Virupax Sadashivpetimath8e8a5512012-08-07 14:46:20 +05301505 length = min_t(u32, length, qh->sg_miter.length);
Mans Rullgardedc87d92020-03-16 16:11:35 -05001506 musb_write_fifo(hw_ep, length, qh->sg_miter.addr);
Virupax Sadashivpetimath8e8a5512012-08-07 14:46:20 +05301507 qh->sg_miter.consumed = length;
1508 sg_miter_stop(&qh->sg_miter);
1509 } else {
1510 musb_write_fifo(hw_ep, length, urb->transfer_buffer + offset);
1511 }
1512
Sergei Shtylyov6b6e9712009-03-26 18:29:19 -07001513 qh->segsize = length;
Felipe Balbi550a7372008-07-24 12:27:36 +03001514
Sergei Shtylyov6b6e9712009-03-26 18:29:19 -07001515 musb_ep_select(mbase, epnum);
1516 musb_writew(epio, MUSB_TXCSR,
1517 MUSB_TXCSR_H_WZC_BITS | MUSB_TXCSR_TXPKTRDY);
Felipe Balbi550a7372008-07-24 12:27:36 +03001518}
1519
Tony Lindgren069a3fd2015-05-01 12:29:33 -07001520#ifdef CONFIG_USB_TI_CPPI41_DMA
1521/* Seems to set up ISO for cppi41 and not advance len. See commit c57c41d */
1522static int musb_rx_dma_iso_cppi41(struct dma_controller *dma,
1523 struct musb_hw_ep *hw_ep,
1524 struct musb_qh *qh,
1525 struct urb *urb,
1526 size_t len)
1527{
Bin Liu04471eb2016-05-31 10:05:25 -05001528 struct dma_channel *channel = hw_ep->rx_channel;
Tony Lindgren069a3fd2015-05-01 12:29:33 -07001529 void __iomem *epio = hw_ep->regs;
1530 dma_addr_t *buf;
1531 u32 length, res;
1532 u16 val;
1533
1534 buf = (void *)urb->iso_frame_desc[qh->iso_idx].offset +
1535 (u32)urb->transfer_dma;
1536
1537 length = urb->iso_frame_desc[qh->iso_idx].length;
1538
1539 val = musb_readw(epio, MUSB_RXCSR);
1540 val |= MUSB_RXCSR_DMAENAB;
1541 musb_writew(hw_ep->regs, MUSB_RXCSR, val);
1542
1543 res = dma->channel_program(channel, qh->maxpacket, 0,
1544 (u32)buf, length);
1545
1546 return res;
1547}
1548#else
1549static inline int musb_rx_dma_iso_cppi41(struct dma_controller *dma,
1550 struct musb_hw_ep *hw_ep,
1551 struct musb_qh *qh,
1552 struct urb *urb,
1553 size_t len)
1554{
1555 return false;
1556}
1557#endif
Felipe Balbi550a7372008-07-24 12:27:36 +03001558
Tony Lindgrencff84bd2015-05-01 12:29:35 -07001559#if defined(CONFIG_USB_INVENTRA_DMA) || defined(CONFIG_USB_UX500_DMA) || \
1560 defined(CONFIG_USB_TI_CPPI41_DMA)
Felipe Balbi550a7372008-07-24 12:27:36 +03001561/* Host side RX (IN) using Mentor DMA works as follows:
1562 submit_urb ->
1563 - if queue was empty, ProgramEndpoint
1564 - first IN token is sent out (by setting ReqPkt)
1565 LinuxIsr -> RxReady()
1566 /\ => first packet is received
1567 | - Set in mode 0 (DmaEnab, ~ReqPkt)
1568 | -> DMA Isr (transfer complete) -> RxReady()
1569 | - Ack receive (~RxPktRdy), turn off DMA (~DmaEnab)
1570 | - if urb not complete, send next IN token (ReqPkt)
1571 | | else complete urb.
1572 | |
1573 ---------------------------
1574 *
1575 * Nuances of mode 1:
1576 * For short packets, no ack (+RxPktRdy) is sent automatically
1577 * (even if AutoClear is ON)
1578 * For full packets, ack (~RxPktRdy) and next IN token (+ReqPkt) is sent
1579 * automatically => major problem, as collecting the next packet becomes
1580 * difficult. Hence mode 1 is not used.
1581 *
1582 * REVISIT
1583 * All we care about at this driver level is that
1584 * (a) all URBs terminate with REQPKT cleared and fifo(s) empty;
1585 * (b) termination conditions are: short RX, or buffer full;
1586 * (c) fault modes include
1587 * - iff URB_SHORT_NOT_OK, short RX status is -EREMOTEIO.
1588 * (and that endpoint's dma queue stops immediately)
1589 * - overflow (full, PLUS more bytes in the terminal packet)
1590 *
1591 * So for example, usb-storage sets URB_SHORT_NOT_OK, and would
1592 * thus be a great candidate for using mode 1 ... for all but the
1593 * last packet of one URB's transfer.
1594 */
Tony Lindgrencff84bd2015-05-01 12:29:35 -07001595static int musb_rx_dma_inventra_cppi41(struct dma_controller *dma,
1596 struct musb_hw_ep *hw_ep,
1597 struct musb_qh *qh,
1598 struct urb *urb,
1599 size_t len)
1600{
1601 struct dma_channel *channel = hw_ep->rx_channel;
1602 void __iomem *epio = hw_ep->regs;
1603 u16 val;
1604 int pipe;
1605 bool done;
Felipe Balbi550a7372008-07-24 12:27:36 +03001606
Tony Lindgrencff84bd2015-05-01 12:29:35 -07001607 pipe = urb->pipe;
1608
1609 if (usb_pipeisoc(pipe)) {
1610 struct usb_iso_packet_descriptor *d;
1611
1612 d = urb->iso_frame_desc + qh->iso_idx;
1613 d->actual_length = len;
1614
1615 /* even if there was an error, we did the dma
1616 * for iso_frame_desc->length
1617 */
1618 if (d->status != -EILSEQ && d->status != -EOVERFLOW)
1619 d->status = 0;
1620
1621 if (++qh->iso_idx >= urb->number_of_packets) {
1622 done = true;
1623 } else {
1624 /* REVISIT: Why ignore return value here? */
1625 if (musb_dma_cppi41(hw_ep->musb))
1626 done = musb_rx_dma_iso_cppi41(dma, hw_ep, qh,
1627 urb, len);
1628 done = false;
1629 }
1630
1631 } else {
1632 /* done if urb buffer is full or short packet is recd */
1633 done = (urb->actual_length + len >=
1634 urb->transfer_buffer_length
1635 || channel->actual_len < qh->maxpacket
1636 || channel->rx_packet_done);
1637 }
1638
1639 /* send IN token for next packet, without AUTOREQ */
1640 if (!done) {
1641 val = musb_readw(epio, MUSB_RXCSR);
1642 val |= MUSB_RXCSR_H_REQPKT;
1643 musb_writew(epio, MUSB_RXCSR, MUSB_RXCSR_H_WZC_BITS | val);
1644 }
1645
1646 return done;
1647}
Tony Lindgrenac33cdb2015-05-01 12:29:37 -07001648
1649/* Disadvantage of using mode 1:
1650 * It's basically usable only for mass storage class; essentially all
1651 * other protocols also terminate transfers on short packets.
1652 *
1653 * Details:
1654 * An extra IN token is sent at the end of the transfer (due to AUTOREQ)
1655 * If you try to use mode 1 for (transfer_buffer_length - 512), and try
1656 * to use the extra IN token to grab the last packet using mode 0, then
1657 * the problem is that you cannot be sure when the device will send the
1658 * last packet and RxPktRdy set. Sometimes the packet is recd too soon
1659 * such that it gets lost when RxCSR is re-set at the end of the mode 1
1660 * transfer, while sometimes it is recd just a little late so that if you
1661 * try to configure for mode 0 soon after the mode 1 transfer is
1662 * completed, you will find rxcount 0. Okay, so you might think why not
1663 * wait for an interrupt when the pkt is recd. Well, you won't get any!
1664 */
1665static int musb_rx_dma_in_inventra_cppi41(struct dma_controller *dma,
1666 struct musb_hw_ep *hw_ep,
1667 struct musb_qh *qh,
1668 struct urb *urb,
1669 size_t len,
1670 u8 iso_err)
1671{
1672 struct musb *musb = hw_ep->musb;
1673 void __iomem *epio = hw_ep->regs;
1674 struct dma_channel *channel = hw_ep->rx_channel;
1675 u16 rx_count, val;
1676 int length, pipe, done;
1677 dma_addr_t buf;
1678
1679 rx_count = musb_readw(epio, MUSB_RXCOUNT);
1680 pipe = urb->pipe;
1681
1682 if (usb_pipeisoc(pipe)) {
1683 int d_status = 0;
1684 struct usb_iso_packet_descriptor *d;
1685
1686 d = urb->iso_frame_desc + qh->iso_idx;
1687
1688 if (iso_err) {
1689 d_status = -EILSEQ;
1690 urb->error_count++;
1691 }
1692 if (rx_count > d->length) {
1693 if (d_status == 0) {
1694 d_status = -EOVERFLOW;
1695 urb->error_count++;
1696 }
Bin Liub99d3652016-06-30 12:12:22 -05001697 musb_dbg(musb, "** OVERFLOW %d into %d",
Tony Lindgrenac33cdb2015-05-01 12:29:37 -07001698 rx_count, d->length);
1699
1700 length = d->length;
1701 } else
1702 length = rx_count;
1703 d->status = d_status;
1704 buf = urb->transfer_dma + d->offset;
1705 } else {
1706 length = rx_count;
1707 buf = urb->transfer_dma + urb->actual_length;
1708 }
1709
1710 channel->desired_mode = 0;
1711#ifdef USE_MODE1
1712 /* because of the issue below, mode 1 will
1713 * only rarely behave with correct semantics.
1714 */
1715 if ((urb->transfer_flags & URB_SHORT_NOT_OK)
1716 && (urb->transfer_buffer_length - urb->actual_length)
1717 > qh->maxpacket)
1718 channel->desired_mode = 1;
1719 if (rx_count < hw_ep->max_packet_sz_rx) {
1720 length = rx_count;
1721 channel->desired_mode = 0;
1722 } else {
1723 length = urb->transfer_buffer_length;
1724 }
1725#endif
1726
1727 /* See comments above on disadvantages of using mode 1 */
1728 val = musb_readw(epio, MUSB_RXCSR);
1729 val &= ~MUSB_RXCSR_H_REQPKT;
1730
1731 if (channel->desired_mode == 0)
1732 val &= ~MUSB_RXCSR_H_AUTOREQ;
1733 else
1734 val |= MUSB_RXCSR_H_AUTOREQ;
1735 val |= MUSB_RXCSR_DMAENAB;
1736
1737 /* autoclear shouldn't be set in high bandwidth */
1738 if (qh->hb_mult == 1)
1739 val |= MUSB_RXCSR_AUTOCLEAR;
1740
1741 musb_writew(epio, MUSB_RXCSR, MUSB_RXCSR_H_WZC_BITS | val);
1742
1743 /* REVISIT if when actual_length != 0,
1744 * transfer_buffer_length needs to be
1745 * adjusted first...
1746 */
1747 done = dma->channel_program(channel, qh->maxpacket,
1748 channel->desired_mode,
1749 buf, length);
1750
1751 if (!done) {
1752 dma->channel_release(channel);
1753 hw_ep->rx_channel = NULL;
1754 channel = NULL;
1755 val = musb_readw(epio, MUSB_RXCSR);
1756 val &= ~(MUSB_RXCSR_DMAENAB
1757 | MUSB_RXCSR_H_AUTOREQ
1758 | MUSB_RXCSR_AUTOCLEAR);
1759 musb_writew(epio, MUSB_RXCSR, val);
1760 }
1761
1762 return done;
1763}
Tony Lindgrencff84bd2015-05-01 12:29:35 -07001764#else
1765static inline int musb_rx_dma_inventra_cppi41(struct dma_controller *dma,
1766 struct musb_hw_ep *hw_ep,
1767 struct musb_qh *qh,
1768 struct urb *urb,
1769 size_t len)
1770{
1771 return false;
1772}
Tony Lindgrenac33cdb2015-05-01 12:29:37 -07001773
1774static inline int musb_rx_dma_in_inventra_cppi41(struct dma_controller *dma,
1775 struct musb_hw_ep *hw_ep,
1776 struct musb_qh *qh,
1777 struct urb *urb,
1778 size_t len,
1779 u8 iso_err)
1780{
1781 return false;
1782}
Felipe Balbi550a7372008-07-24 12:27:36 +03001783#endif
1784
1785/*
1786 * Service an RX interrupt for the given IN endpoint; docs cover bulk, iso,
1787 * and high-bandwidth IN transfer cases.
1788 */
1789void musb_host_rx(struct musb *musb, u8 epnum)
1790{
1791 struct urb *urb;
1792 struct musb_hw_ep *hw_ep = musb->endpoints + epnum;
Tony Lindgrencff84bd2015-05-01 12:29:35 -07001793 struct dma_controller *c = musb->dma_controller;
Felipe Balbi550a7372008-07-24 12:27:36 +03001794 void __iomem *epio = hw_ep->regs;
1795 struct musb_qh *qh = hw_ep->in_qh;
1796 size_t xfer_len;
1797 void __iomem *mbase = musb->mregs;
1798 int pipe;
1799 u16 rx_csr, val;
1800 bool iso_err = false;
1801 bool done = false;
1802 u32 status;
1803 struct dma_channel *dma;
Virupax Sadashivpetimath8e8a5512012-08-07 14:46:20 +05301804 unsigned int sg_flags = SG_MITER_ATOMIC | SG_MITER_TO_SG;
Felipe Balbi550a7372008-07-24 12:27:36 +03001805
1806 musb_ep_select(mbase, epnum);
1807
1808 urb = next_urb(qh);
1809 dma = is_dma_capable() ? hw_ep->rx_channel : NULL;
1810 status = 0;
1811 xfer_len = 0;
1812
1813 rx_csr = musb_readw(epio, MUSB_RXCSR);
1814 val = rx_csr;
1815
1816 if (unlikely(!urb)) {
1817 /* REVISIT -- THIS SHOULD NEVER HAPPEN ... but, at least
1818 * usbtest #11 (unlinks) triggers it regularly, sometimes
1819 * with fifo full. (Only with DMA??)
1820 */
Bin Liub99d3652016-06-30 12:12:22 -05001821 musb_dbg(musb, "BOGUS RX%d ready, csr %04x, count %d",
1822 epnum, val, musb_readw(epio, MUSB_RXCOUNT));
Felipe Balbi550a7372008-07-24 12:27:36 +03001823 musb_h_flush_rxfifo(hw_ep, MUSB_RXCSR_CLRDATATOG);
1824 return;
1825 }
1826
1827 pipe = urb->pipe;
1828
Bin Liu19ca6822016-06-30 12:12:26 -05001829 trace_musb_urb_rx(musb, urb);
Felipe Balbi550a7372008-07-24 12:27:36 +03001830
1831 /* check for errors, concurrent stall & unlink is not really
1832 * handled yet! */
1833 if (rx_csr & MUSB_RXCSR_H_RXSTALL) {
Bin Liub99d3652016-06-30 12:12:22 -05001834 musb_dbg(musb, "RX end %d STALL", epnum);
Felipe Balbi550a7372008-07-24 12:27:36 +03001835
1836 /* stall; record URB status */
1837 status = -EPIPE;
1838
1839 } else if (rx_csr & MUSB_RXCSR_H_ERROR) {
Bin Liub99d3652016-06-30 12:12:22 -05001840 musb_dbg(musb, "end %d RX proto error", epnum);
Felipe Balbi550a7372008-07-24 12:27:36 +03001841
1842 status = -EPROTO;
1843 musb_writeb(epio, MUSB_RXINTERVAL, 0);
1844
Bin Liub5801212016-05-31 10:05:03 -05001845 rx_csr &= ~MUSB_RXCSR_H_ERROR;
1846 musb_writew(epio, MUSB_RXCSR, rx_csr);
1847
Felipe Balbi550a7372008-07-24 12:27:36 +03001848 } else if (rx_csr & MUSB_RXCSR_DATAERROR) {
1849
1850 if (USB_ENDPOINT_XFER_ISOC != qh->type) {
Bin Liub99d3652016-06-30 12:12:22 -05001851 musb_dbg(musb, "RX end %d NAK timeout", epnum);
Ajay Kumar Gupta1e0320f2009-02-24 15:26:13 -08001852
1853 /* NOTE: NAKing is *NOT* an error, so we want to
1854 * continue. Except ... if there's a request for
1855 * another QH, use that instead of starving it.
1856 *
1857 * Devices like Ethernet and serial adapters keep
1858 * reads posted at all times, which will starve
1859 * other devices without this logic.
1860 */
1861 if (usb_pipebulk(urb->pipe)
1862 && qh->mux == 1
1863 && !list_is_singular(&musb->in_bulk)) {
Ajay Kumar Guptaf2838622012-07-19 13:41:59 +05301864 musb_bulk_nak_timeout(musb, hw_ep, 1);
Ajay Kumar Gupta1e0320f2009-02-24 15:26:13 -08001865 return;
1866 }
Felipe Balbi550a7372008-07-24 12:27:36 +03001867 musb_ep_select(mbase, epnum);
Ajay Kumar Gupta1e0320f2009-02-24 15:26:13 -08001868 rx_csr |= MUSB_RXCSR_H_WZC_BITS;
1869 rx_csr &= ~MUSB_RXCSR_DATAERROR;
1870 musb_writew(epio, MUSB_RXCSR, rx_csr);
Felipe Balbi550a7372008-07-24 12:27:36 +03001871
1872 goto finish;
1873 } else {
Bin Liub99d3652016-06-30 12:12:22 -05001874 musb_dbg(musb, "RX end %d ISO data error", epnum);
Felipe Balbi550a7372008-07-24 12:27:36 +03001875 /* packet error reported later */
1876 iso_err = true;
1877 }
Ajay Kumar Guptaa483d702009-04-03 16:16:17 -07001878 } else if (rx_csr & MUSB_RXCSR_INCOMPRX) {
Bin Liub99d3652016-06-30 12:12:22 -05001879 musb_dbg(musb, "end %d high bandwidth incomplete ISO packet RX",
Ajay Kumar Guptaa483d702009-04-03 16:16:17 -07001880 epnum);
1881 status = -EPROTO;
Felipe Balbi550a7372008-07-24 12:27:36 +03001882 }
1883
1884 /* faults abort the transfer */
1885 if (status) {
1886 /* clean up dma and collect transfer count */
1887 if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) {
1888 dma->status = MUSB_DMA_STATUS_CORE_ABORT;
Daniel Mack9c547692014-05-26 14:52:35 +02001889 musb->dma_controller->channel_abort(dma);
Felipe Balbi550a7372008-07-24 12:27:36 +03001890 xfer_len = dma->actual_len;
1891 }
1892 musb_h_flush_rxfifo(hw_ep, MUSB_RXCSR_CLRDATATOG);
1893 musb_writeb(epio, MUSB_RXINTERVAL, 0);
1894 done = true;
1895 goto finish;
1896 }
1897
1898 if (unlikely(dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY)) {
1899 /* SHOULD NEVER HAPPEN ... but at least DaVinci has done it */
1900 ERR("RX%d dma busy, csr %04x\n", epnum, rx_csr);
1901 goto finish;
1902 }
1903
1904 /* thorough shutdown for now ... given more precise fault handling
1905 * and better queueing support, we might keep a DMA pipeline going
1906 * while processing this irq for earlier completions.
1907 */
1908
1909 /* FIXME this is _way_ too much in-line logic for Mentor DMA */
Tony Lindgren557d5432015-05-01 12:29:34 -07001910 if (!musb_dma_inventra(musb) && !musb_dma_ux500(musb) &&
1911 (rx_csr & MUSB_RXCSR_H_REQPKT)) {
Felipe Balbi550a7372008-07-24 12:27:36 +03001912 /* REVISIT this happened for a while on some short reads...
1913 * the cleanup still needs investigation... looks bad...
1914 * and also duplicates dma cleanup code above ... plus,
1915 * shouldn't this be the "half full" double buffer case?
1916 */
1917 if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) {
1918 dma->status = MUSB_DMA_STATUS_CORE_ABORT;
Daniel Mack9c547692014-05-26 14:52:35 +02001919 musb->dma_controller->channel_abort(dma);
Felipe Balbi550a7372008-07-24 12:27:36 +03001920 xfer_len = dma->actual_len;
1921 done = true;
1922 }
1923
Bin Liub99d3652016-06-30 12:12:22 -05001924 musb_dbg(musb, "RXCSR%d %04x, reqpkt, len %zu%s", epnum, rx_csr,
Felipe Balbi550a7372008-07-24 12:27:36 +03001925 xfer_len, dma ? ", dma" : "");
1926 rx_csr &= ~MUSB_RXCSR_H_REQPKT;
1927
1928 musb_ep_select(mbase, epnum);
1929 musb_writew(epio, MUSB_RXCSR,
1930 MUSB_RXCSR_H_WZC_BITS | rx_csr);
1931 }
Tony Lindgren557d5432015-05-01 12:29:34 -07001932
Felipe Balbi550a7372008-07-24 12:27:36 +03001933 if (dma && (rx_csr & MUSB_RXCSR_DMAENAB)) {
1934 xfer_len = dma->actual_len;
1935
1936 val &= ~(MUSB_RXCSR_DMAENAB
1937 | MUSB_RXCSR_H_AUTOREQ
1938 | MUSB_RXCSR_AUTOCLEAR
1939 | MUSB_RXCSR_RXPKTRDY);
1940 musb_writew(hw_ep->regs, MUSB_RXCSR, val);
1941
Tony Lindgrencff84bd2015-05-01 12:29:35 -07001942 if (musb_dma_inventra(musb) || musb_dma_ux500(musb) ||
1943 musb_dma_cppi41(musb)) {
1944 done = musb_rx_dma_inventra_cppi41(c, hw_ep, qh, urb, xfer_len);
Bin Liub99d3652016-06-30 12:12:22 -05001945 musb_dbg(hw_ep->musb,
1946 "ep %d dma %s, rxcsr %04x, rxcount %d",
Tony Lindgrencff84bd2015-05-01 12:29:35 -07001947 epnum, done ? "off" : "reset",
1948 musb_readw(epio, MUSB_RXCSR),
1949 musb_readw(epio, MUSB_RXCOUNT));
1950 } else {
1951 done = true;
Ajay Kumar Guptaf82a6892008-10-29 15:10:31 +02001952 }
Felipe Balbi550a7372008-07-24 12:27:36 +03001953
Felipe Balbi550a7372008-07-24 12:27:36 +03001954 } else if (urb->status == -EINPROGRESS) {
1955 /* if no errors, be sure a packet is ready for unloading */
1956 if (unlikely(!(rx_csr & MUSB_RXCSR_RXPKTRDY))) {
1957 status = -EPROTO;
1958 ERR("Rx interrupt with no errors or packet!\n");
1959
1960 /* FIXME this is another "SHOULD NEVER HAPPEN" */
1961
1962/* SCRUB (RX) */
1963 /* do the proper sequence to abort the transfer */
1964 musb_ep_select(mbase, epnum);
1965 val &= ~MUSB_RXCSR_H_REQPKT;
1966 musb_writew(epio, MUSB_RXCSR, val);
1967 goto finish;
1968 }
1969
1970 /* we are expecting IN packets */
Tony Lindgrene530bb82015-05-01 12:29:36 -07001971 if ((musb_dma_inventra(musb) || musb_dma_ux500(musb) ||
1972 musb_dma_cppi41(musb)) && dma) {
Bin Liub99d3652016-06-30 12:12:22 -05001973 musb_dbg(hw_ep->musb,
1974 "RX%d count %d, buffer 0x%llx len %d/%d",
Tony Lindgrenac33cdb2015-05-01 12:29:37 -07001975 epnum, musb_readw(epio, MUSB_RXCOUNT),
1976 (unsigned long long) urb->transfer_dma
1977 + urb->actual_length,
1978 qh->offset,
1979 urb->transfer_buffer_length);
Felipe Balbi550a7372008-07-24 12:27:36 +03001980
Cristian Birsan4c2ba0c2016-02-19 10:11:56 +02001981 if (musb_rx_dma_in_inventra_cppi41(c, hw_ep, qh, urb,
1982 xfer_len, iso_err))
Tony Lindgrenac33cdb2015-05-01 12:29:37 -07001983 goto finish;
Felipe Balbi550a7372008-07-24 12:27:36 +03001984 else
Tony Lindgrenac33cdb2015-05-01 12:29:37 -07001985 dev_err(musb->controller, "error: rx_dma failed\n");
Felipe Balbi550a7372008-07-24 12:27:36 +03001986 }
Felipe Balbi550a7372008-07-24 12:27:36 +03001987
1988 if (!dma) {
Virupax Sadashivpetimath8e8a5512012-08-07 14:46:20 +05301989 unsigned int received_len;
1990
Maulik Mankad496dda72010-09-24 13:44:06 +03001991 /* Unmap the buffer so that CPU can use it */
Daniel Mack8b125df2013-04-10 21:55:50 +02001992 usb_hcd_unmap_urb_for_dma(musb->hcd, urb);
Virupax Sadashivpetimath8e8a5512012-08-07 14:46:20 +05301993
1994 /*
1995 * We need to map sg if the transfer_buffer is
1996 * NULL.
1997 */
1998 if (!urb->transfer_buffer) {
Virupax Sadashivpetimathed74df12013-04-24 08:38:48 +02001999 qh->use_sg = true;
Virupax Sadashivpetimath8e8a5512012-08-07 14:46:20 +05302000 sg_miter_start(&qh->sg_miter, urb->sg, 1,
2001 sg_flags);
2002 }
2003
Virupax Sadashivpetimathed74df12013-04-24 08:38:48 +02002004 if (qh->use_sg) {
Virupax Sadashivpetimath8e8a5512012-08-07 14:46:20 +05302005 if (!sg_miter_next(&qh->sg_miter)) {
2006 dev_err(musb->controller, "error: sg list empty\n");
2007 sg_miter_stop(&qh->sg_miter);
2008 status = -EINVAL;
2009 done = true;
2010 goto finish;
2011 }
2012 urb->transfer_buffer = qh->sg_miter.addr;
2013 received_len = urb->actual_length;
2014 qh->offset = 0x0;
2015 done = musb_host_packet_rx(musb, urb, epnum,
2016 iso_err);
2017 /* Calculate the number of bytes received */
2018 received_len = urb->actual_length -
2019 received_len;
2020 qh->sg_miter.consumed = received_len;
2021 sg_miter_stop(&qh->sg_miter);
2022 } else {
2023 done = musb_host_packet_rx(musb, urb,
2024 epnum, iso_err);
2025 }
Bin Liub99d3652016-06-30 12:12:22 -05002026 musb_dbg(musb, "read %spacket", done ? "last " : "");
Felipe Balbi550a7372008-07-24 12:27:36 +03002027 }
2028 }
2029
Felipe Balbi550a7372008-07-24 12:27:36 +03002030finish:
2031 urb->actual_length += xfer_len;
2032 qh->offset += xfer_len;
2033 if (done) {
Mans Rullgardedc87d92020-03-16 16:11:35 -05002034 if (qh->use_sg) {
Virupax Sadashivpetimathed74df12013-04-24 08:38:48 +02002035 qh->use_sg = false;
Mans Rullgardedc87d92020-03-16 16:11:35 -05002036 urb->transfer_buffer = NULL;
2037 }
Virupax Sadashivpetimath8e8a5512012-08-07 14:46:20 +05302038
Felipe Balbi550a7372008-07-24 12:27:36 +03002039 if (urb->status == -EINPROGRESS)
2040 urb->status = status;
2041 musb_advance_schedule(musb, urb, hw_ep, USB_DIR_IN);
2042 }
2043}
2044
2045/* schedule nodes correspond to peripheral endpoints, like an OHCI QH.
2046 * the software schedule associates multiple such nodes with a given
2047 * host side hardware endpoint + direction; scheduling may activate
2048 * that hardware endpoint.
2049 */
2050static int musb_schedule(
2051 struct musb *musb,
2052 struct musb_qh *qh,
2053 int is_in)
2054{
Rickard Strandqvisteac44dc2014-06-01 15:48:12 +02002055 int idle = 0;
Felipe Balbi550a7372008-07-24 12:27:36 +03002056 int best_diff;
2057 int best_end, epnum;
2058 struct musb_hw_ep *hw_ep = NULL;
2059 struct list_head *head = NULL;
Swaminathan S5274dab2009-12-28 13:40:37 +02002060 u8 toggle;
2061 u8 txtype;
2062 struct urb *urb = next_urb(qh);
Felipe Balbi550a7372008-07-24 12:27:36 +03002063
2064 /* use fixed hardware for control and bulk */
Ajay Kumar Gupta23d15e02008-10-29 15:10:35 +02002065 if (qh->type == USB_ENDPOINT_XFER_CONTROL) {
Felipe Balbi550a7372008-07-24 12:27:36 +03002066 head = &musb->control;
2067 hw_ep = musb->control_ep;
Felipe Balbi550a7372008-07-24 12:27:36 +03002068 goto success;
2069 }
2070
2071 /* else, periodic transfers get muxed to other endpoints */
2072
Sergei Shtylyov5d67a852009-02-24 15:23:34 -08002073 /*
2074 * We know this qh hasn't been scheduled, so all we need to do
Felipe Balbi550a7372008-07-24 12:27:36 +03002075 * is choose which hardware endpoint to put it on ...
2076 *
2077 * REVISIT what we really want here is a regular schedule tree
Sergei Shtylyov5d67a852009-02-24 15:23:34 -08002078 * like e.g. OHCI uses.
Felipe Balbi550a7372008-07-24 12:27:36 +03002079 */
2080 best_diff = 4096;
2081 best_end = -1;
2082
Sergei Shtylyov5d67a852009-02-24 15:23:34 -08002083 for (epnum = 1, hw_ep = musb->endpoints + 1;
2084 epnum < musb->nr_endpoints;
2085 epnum++, hw_ep++) {
Felipe Balbi550a7372008-07-24 12:27:36 +03002086 int diff;
2087
Sergei Shtylyov3e5c6dc2009-03-27 12:55:16 -07002088 if (musb_ep_get_qh(hw_ep, is_in) != NULL)
Felipe Balbi550a7372008-07-24 12:27:36 +03002089 continue;
Sergei Shtylyov5d67a852009-02-24 15:23:34 -08002090
Felipe Balbi550a7372008-07-24 12:27:36 +03002091 if (hw_ep == musb->bulk_ep)
2092 continue;
2093
2094 if (is_in)
Ajay Kumar Guptaa483d702009-04-03 16:16:17 -07002095 diff = hw_ep->max_packet_sz_rx;
Felipe Balbi550a7372008-07-24 12:27:36 +03002096 else
Ajay Kumar Guptaa483d702009-04-03 16:16:17 -07002097 diff = hw_ep->max_packet_sz_tx;
2098 diff -= (qh->maxpacket * qh->hb_mult);
Felipe Balbi550a7372008-07-24 12:27:36 +03002099
Ajay Kumar Gupta23d15e02008-10-29 15:10:35 +02002100 if (diff >= 0 && best_diff > diff) {
Swaminathan S5274dab2009-12-28 13:40:37 +02002101
2102 /*
2103 * Mentor controller has a bug in that if we schedule
2104 * a BULK Tx transfer on an endpoint that had earlier
2105 * handled ISOC then the BULK transfer has to start on
2106 * a zero toggle. If the BULK transfer starts on a 1
2107 * toggle then this transfer will fail as the mentor
2108 * controller starts the Bulk transfer on a 0 toggle
2109 * irrespective of the programming of the toggle bits
2110 * in the TXCSR register. Check for this condition
2111 * while allocating the EP for a Tx Bulk transfer. If
2112 * so skip this EP.
2113 */
2114 hw_ep = musb->endpoints + epnum;
2115 toggle = usb_gettoggle(urb->dev, qh->epnum, !is_in);
2116 txtype = (musb_readb(hw_ep->regs, MUSB_TXTYPE)
2117 >> 4) & 0x3;
2118 if (!is_in && (qh->type == USB_ENDPOINT_XFER_BULK) &&
2119 toggle && (txtype == USB_ENDPOINT_XFER_ISOC))
2120 continue;
2121
Felipe Balbi550a7372008-07-24 12:27:36 +03002122 best_diff = diff;
2123 best_end = epnum;
2124 }
2125 }
Ajay Kumar Gupta23d15e02008-10-29 15:10:35 +02002126 /* use bulk reserved ep1 if no other ep is free */
Felipe Balbiaa5cbbe2008-11-17 09:08:16 +02002127 if (best_end < 0 && qh->type == USB_ENDPOINT_XFER_BULK) {
Ajay Kumar Gupta23d15e02008-10-29 15:10:35 +02002128 hw_ep = musb->bulk_ep;
2129 if (is_in)
2130 head = &musb->in_bulk;
2131 else
2132 head = &musb->out_bulk;
Ajay Kumar Gupta1e0320f2009-02-24 15:26:13 -08002133
Ajay Kumar Guptaf2838622012-07-19 13:41:59 +05302134 /* Enable bulk RX/TX NAK timeout scheme when bulk requests are
Rahul Bedarkar5ae477b2014-01-02 19:27:47 +05302135 * multiplexed. This scheme does not work in high speed to full
Ajay Kumar Gupta1e0320f2009-02-24 15:26:13 -08002136 * speed scenario as NAK interrupts are not coming from a
2137 * full speed device connected to a high speed device.
2138 * NAK timeout interval is 8 (128 uframe or 16ms) for HS and
2139 * 4 (8 frame or 8ms) for FS device.
2140 */
Ajay Kumar Guptaf2838622012-07-19 13:41:59 +05302141 if (qh->dev)
Ajay Kumar Gupta1e0320f2009-02-24 15:26:13 -08002142 qh->intv_reg =
2143 (USB_SPEED_HIGH == qh->dev->speed) ? 8 : 4;
Ajay Kumar Gupta23d15e02008-10-29 15:10:35 +02002144 goto success;
2145 } else if (best_end < 0) {
Felipe Balbi550a7372008-07-24 12:27:36 +03002146 return -ENOSPC;
Ajay Kumar Gupta23d15e02008-10-29 15:10:35 +02002147 }
Felipe Balbi550a7372008-07-24 12:27:36 +03002148
2149 idle = 1;
Ajay Kumar Gupta23d15e02008-10-29 15:10:35 +02002150 qh->mux = 0;
Felipe Balbi550a7372008-07-24 12:27:36 +03002151 hw_ep = musb->endpoints + best_end;
Bin Liub99d3652016-06-30 12:12:22 -05002152 musb_dbg(musb, "qh %p periodic slot %d", qh, best_end);
Felipe Balbi550a7372008-07-24 12:27:36 +03002153success:
Ajay Kumar Gupta23d15e02008-10-29 15:10:35 +02002154 if (head) {
2155 idle = list_empty(head);
2156 list_add_tail(&qh->ring, head);
2157 qh->mux = 1;
2158 }
Felipe Balbi550a7372008-07-24 12:27:36 +03002159 qh->hw_ep = hw_ep;
2160 qh->hep->hcpriv = qh;
2161 if (idle)
2162 musb_start_urb(musb, is_in, qh);
2163 return 0;
2164}
2165
2166static int musb_urb_enqueue(
2167 struct usb_hcd *hcd,
2168 struct urb *urb,
2169 gfp_t mem_flags)
2170{
2171 unsigned long flags;
2172 struct musb *musb = hcd_to_musb(hcd);
2173 struct usb_host_endpoint *hep = urb->ep;
David Brownell74bb3502009-03-26 17:36:57 -07002174 struct musb_qh *qh;
Felipe Balbi550a7372008-07-24 12:27:36 +03002175 struct usb_endpoint_descriptor *epd = &hep->desc;
2176 int ret;
2177 unsigned type_reg;
2178 unsigned interval;
2179
2180 /* host role must be active */
2181 if (!is_host_active(musb) || !musb->is_active)
2182 return -ENODEV;
2183
Bin Liu19ca6822016-06-30 12:12:26 -05002184 trace_musb_urb_enq(musb, urb);
2185
Felipe Balbi550a7372008-07-24 12:27:36 +03002186 spin_lock_irqsave(&musb->lock, flags);
2187 ret = usb_hcd_link_urb_to_ep(hcd, urb);
David Brownell74bb3502009-03-26 17:36:57 -07002188 qh = ret ? NULL : hep->hcpriv;
2189 if (qh)
2190 urb->hcpriv = qh;
Felipe Balbi550a7372008-07-24 12:27:36 +03002191 spin_unlock_irqrestore(&musb->lock, flags);
Felipe Balbi550a7372008-07-24 12:27:36 +03002192
2193 /* DMA mapping was already done, if needed, and this urb is on
David Brownell74bb3502009-03-26 17:36:57 -07002194 * hep->urb_list now ... so we're done, unless hep wasn't yet
2195 * scheduled onto a live qh.
Felipe Balbi550a7372008-07-24 12:27:36 +03002196 *
2197 * REVISIT best to keep hep->hcpriv valid until the endpoint gets
2198 * disabled, testing for empty qh->ring and avoiding qh setup costs
2199 * except for the first urb queued after a config change.
2200 */
David Brownell74bb3502009-03-26 17:36:57 -07002201 if (qh || ret)
2202 return ret;
Felipe Balbi550a7372008-07-24 12:27:36 +03002203
2204 /* Allocate and initialize qh, minimizing the work done each time
2205 * hw_ep gets reprogrammed, or with irqs blocked. Then schedule it.
2206 *
2207 * REVISIT consider a dedicated qh kmem_cache, so it's harder
2208 * for bugs in other kernel code to break this driver...
2209 */
2210 qh = kzalloc(sizeof *qh, mem_flags);
2211 if (!qh) {
Ajay Kumar Gupta2492e672008-09-11 11:53:21 +03002212 spin_lock_irqsave(&musb->lock, flags);
Felipe Balbi550a7372008-07-24 12:27:36 +03002213 usb_hcd_unlink_urb_from_ep(hcd, urb);
Ajay Kumar Gupta2492e672008-09-11 11:53:21 +03002214 spin_unlock_irqrestore(&musb->lock, flags);
Felipe Balbi550a7372008-07-24 12:27:36 +03002215 return -ENOMEM;
2216 }
2217
2218 qh->hep = hep;
2219 qh->dev = urb->dev;
2220 INIT_LIST_HEAD(&qh->ring);
2221 qh->is_ready = 1;
2222
Kuninori Morimoto29cc8892011-08-23 03:12:03 -07002223 qh->maxpacket = usb_endpoint_maxp(epd);
Ajay Kumar Guptaa483d702009-04-03 16:16:17 -07002224 qh->type = usb_endpoint_type(epd);
Felipe Balbi550a7372008-07-24 12:27:36 +03002225
Ajay Kumar Guptaa483d702009-04-03 16:16:17 -07002226 /* Bits 11 & 12 of wMaxPacketSize encode high bandwidth multiplier.
2227 * Some musb cores don't support high bandwidth ISO transfers; and
2228 * we don't (yet!) support high bandwidth interrupt transfers.
2229 */
2230 qh->hb_mult = 1 + ((qh->maxpacket >> 11) & 0x03);
2231 if (qh->hb_mult > 1) {
2232 int ok = (qh->type == USB_ENDPOINT_XFER_ISOC);
2233
2234 if (ok)
2235 ok = (usb_pipein(urb->pipe) && musb->hb_iso_rx)
2236 || (usb_pipeout(urb->pipe) && musb->hb_iso_tx);
2237 if (!ok) {
2238 ret = -EMSGSIZE;
2239 goto done;
2240 }
2241 qh->maxpacket &= 0x7ff;
Felipe Balbi550a7372008-07-24 12:27:36 +03002242 }
2243
Julia Lawall96bcd092009-01-24 17:57:24 -08002244 qh->epnum = usb_endpoint_num(epd);
Felipe Balbi550a7372008-07-24 12:27:36 +03002245
2246 /* NOTE: urb->dev->devnum is wrong during SET_ADDRESS */
2247 qh->addr_reg = (u8) usb_pipedevice(urb->pipe);
2248
2249 /* precompute rxtype/txtype/type0 register */
2250 type_reg = (qh->type << 4) | qh->epnum;
2251 switch (urb->dev->speed) {
2252 case USB_SPEED_LOW:
2253 type_reg |= 0xc0;
2254 break;
2255 case USB_SPEED_FULL:
2256 type_reg |= 0x80;
2257 break;
2258 default:
2259 type_reg |= 0x40;
2260 }
2261 qh->type_reg = type_reg;
2262
Sergei Shtylyov136733d2009-02-21 15:31:35 -08002263 /* Precompute RXINTERVAL/TXINTERVAL register */
Felipe Balbi550a7372008-07-24 12:27:36 +03002264 switch (qh->type) {
2265 case USB_ENDPOINT_XFER_INT:
Sergei Shtylyov136733d2009-02-21 15:31:35 -08002266 /*
2267 * Full/low speeds use the linear encoding,
2268 * high speed uses the logarithmic encoding.
2269 */
2270 if (urb->dev->speed <= USB_SPEED_FULL) {
2271 interval = max_t(u8, epd->bInterval, 1);
2272 break;
Felipe Balbi550a7372008-07-24 12:27:36 +03002273 }
2274 /* FALLTHROUGH */
2275 case USB_ENDPOINT_XFER_ISOC:
Sergei Shtylyov136733d2009-02-21 15:31:35 -08002276 /* ISO always uses logarithmic encoding */
2277 interval = min_t(u8, epd->bInterval, 16);
Felipe Balbi550a7372008-07-24 12:27:36 +03002278 break;
2279 default:
2280 /* REVISIT we actually want to use NAK limits, hinting to the
2281 * transfer scheduling logic to try some other qh, e.g. try
2282 * for 2 msec first:
2283 *
2284 * interval = (USB_SPEED_HIGH == urb->dev->speed) ? 16 : 2;
2285 *
2286 * The downside of disabling this is that transfer scheduling
2287 * gets VERY unfair for nonperiodic transfers; a misbehaving
Ajay Kumar Gupta1e0320f2009-02-24 15:26:13 -08002288 * peripheral could make that hurt. That's perfectly normal
2289 * for reads from network or serial adapters ... so we have
2290 * partial NAKlimit support for bulk RX.
Felipe Balbi550a7372008-07-24 12:27:36 +03002291 *
Ajay Kumar Gupta1e0320f2009-02-24 15:26:13 -08002292 * The upside of disabling it is simpler transfer scheduling.
Felipe Balbi550a7372008-07-24 12:27:36 +03002293 */
2294 interval = 0;
2295 }
2296 qh->intv_reg = interval;
2297
2298 /* precompute addressing for external hub/tt ports */
2299 if (musb->is_multipoint) {
2300 struct usb_device *parent = urb->dev->parent;
2301
2302 if (parent != hcd->self.root_hub) {
2303 qh->h_addr_reg = (u8) parent->devnum;
2304
2305 /* set up tt info if needed */
2306 if (urb->dev->tt) {
2307 qh->h_port_reg = (u8) urb->dev->ttport;
Ajay Kumar Guptaae5ad292008-09-11 11:53:20 +03002308 if (urb->dev->tt->hub)
2309 qh->h_addr_reg =
2310 (u8) urb->dev->tt->hub->devnum;
2311 if (urb->dev->tt->multi)
2312 qh->h_addr_reg |= 0x80;
Felipe Balbi550a7372008-07-24 12:27:36 +03002313 }
2314 }
2315 }
2316
2317 /* invariant: hep->hcpriv is null OR the qh that's already scheduled.
2318 * until we get real dma queues (with an entry for each urb/buffer),
2319 * we only have work to do in the former case.
2320 */
2321 spin_lock_irqsave(&musb->lock, flags);
yuzheng ma30677792012-08-15 16:11:40 +08002322 if (hep->hcpriv || !next_urb(qh)) {
Felipe Balbi550a7372008-07-24 12:27:36 +03002323 /* some concurrent activity submitted another urb to hep...
2324 * odd, rare, error prone, but legal.
2325 */
2326 kfree(qh);
Dan Carpenter714bc5e2010-03-25 13:14:27 +02002327 qh = NULL;
Felipe Balbi550a7372008-07-24 12:27:36 +03002328 ret = 0;
2329 } else
2330 ret = musb_schedule(musb, qh,
2331 epd->bEndpointAddress & USB_ENDPOINT_DIR_MASK);
2332
2333 if (ret == 0) {
2334 urb->hcpriv = qh;
2335 /* FIXME set urb->start_frame for iso/intr, it's tested in
2336 * musb_start_urb(), but otherwise only konicawc cares ...
2337 */
2338 }
2339 spin_unlock_irqrestore(&musb->lock, flags);
2340
2341done:
2342 if (ret != 0) {
Ajay Kumar Gupta2492e672008-09-11 11:53:21 +03002343 spin_lock_irqsave(&musb->lock, flags);
Felipe Balbi550a7372008-07-24 12:27:36 +03002344 usb_hcd_unlink_urb_from_ep(hcd, urb);
Ajay Kumar Gupta2492e672008-09-11 11:53:21 +03002345 spin_unlock_irqrestore(&musb->lock, flags);
Felipe Balbi550a7372008-07-24 12:27:36 +03002346 kfree(qh);
2347 }
2348 return ret;
2349}
2350
2351
2352/*
2353 * abort a transfer that's at the head of a hardware queue.
2354 * called with controller locked, irqs blocked
2355 * that hardware queue advances to the next transfer, unless prevented
2356 */
Sergei Shtylyov81ec4e42009-03-27 12:57:50 -07002357static int musb_cleanup_urb(struct urb *urb, struct musb_qh *qh)
Felipe Balbi550a7372008-07-24 12:27:36 +03002358{
2359 struct musb_hw_ep *ep = qh->hw_ep;
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03002360 struct musb *musb = ep->musb;
Felipe Balbi550a7372008-07-24 12:27:36 +03002361 void __iomem *epio = ep->regs;
2362 unsigned hw_end = ep->epnum;
2363 void __iomem *regs = ep->musb->mregs;
Sergei Shtylyov81ec4e42009-03-27 12:57:50 -07002364 int is_in = usb_pipein(urb->pipe);
Felipe Balbi550a7372008-07-24 12:27:36 +03002365 int status = 0;
Sergei Shtylyov81ec4e42009-03-27 12:57:50 -07002366 u16 csr;
Bin Liu5de2dd72017-01-03 18:13:46 -06002367 struct dma_channel *dma = NULL;
Felipe Balbi550a7372008-07-24 12:27:36 +03002368
2369 musb_ep_select(regs, hw_end);
2370
2371 if (is_dma_capable()) {
Felipe Balbi550a7372008-07-24 12:27:36 +03002372 dma = is_in ? ep->rx_channel : ep->tx_channel;
2373 if (dma) {
2374 status = ep->musb->dma_controller->channel_abort(dma);
Bin Liub99d3652016-06-30 12:12:22 -05002375 musb_dbg(musb, "abort %cX%d DMA for urb %p --> %d",
Felipe Balbi550a7372008-07-24 12:27:36 +03002376 is_in ? 'R' : 'T', ep->epnum,
2377 urb, status);
2378 urb->actual_length += dma->actual_len;
2379 }
2380 }
2381
2382 /* turn off DMA requests, discard state, stop polling ... */
Ajay Kumar Gupta692933b2012-03-14 17:33:35 +05302383 if (ep->epnum && is_in) {
Felipe Balbi550a7372008-07-24 12:27:36 +03002384 /* giveback saves bulk toggle */
2385 csr = musb_h_flush_rxfifo(ep, 0);
2386
Bin Liu5de2dd72017-01-03 18:13:46 -06002387 /* clear the endpoint's irq status here to avoid bogus irqs */
2388 if (is_dma_capable() && dma)
2389 musb_platform_clear_ep_rxintr(musb, ep->epnum);
David Brownell78322c12009-03-26 17:38:30 -07002390 } else if (ep->epnum) {
Felipe Balbi550a7372008-07-24 12:27:36 +03002391 musb_h_tx_flush_fifo(ep);
2392 csr = musb_readw(epio, MUSB_TXCSR);
2393 csr &= ~(MUSB_TXCSR_AUTOSET
2394 | MUSB_TXCSR_DMAENAB
2395 | MUSB_TXCSR_H_RXSTALL
2396 | MUSB_TXCSR_H_NAKTIMEOUT
2397 | MUSB_TXCSR_H_ERROR
2398 | MUSB_TXCSR_TXPKTRDY);
2399 musb_writew(epio, MUSB_TXCSR, csr);
2400 /* REVISIT may need to clear FLUSHFIFO ... */
2401 musb_writew(epio, MUSB_TXCSR, csr);
2402 /* flush cpu writebuffer */
2403 csr = musb_readw(epio, MUSB_TXCSR);
David Brownell78322c12009-03-26 17:38:30 -07002404 } else {
2405 musb_h_ep0_flush_fifo(ep);
Felipe Balbi550a7372008-07-24 12:27:36 +03002406 }
2407 if (status == 0)
2408 musb_advance_schedule(ep->musb, urb, ep, is_in);
2409 return status;
2410}
2411
2412static int musb_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
2413{
2414 struct musb *musb = hcd_to_musb(hcd);
2415 struct musb_qh *qh;
Felipe Balbi550a7372008-07-24 12:27:36 +03002416 unsigned long flags;
Sergei Shtylyov22a0d6f2009-03-27 12:56:26 -07002417 int is_in = usb_pipein(urb->pipe);
Felipe Balbi550a7372008-07-24 12:27:36 +03002418 int ret;
2419
Bin Liu19ca6822016-06-30 12:12:26 -05002420 trace_musb_urb_deq(musb, urb);
Felipe Balbi550a7372008-07-24 12:27:36 +03002421
2422 spin_lock_irqsave(&musb->lock, flags);
2423 ret = usb_hcd_check_unlink_urb(hcd, urb, status);
2424 if (ret)
2425 goto done;
2426
2427 qh = urb->hcpriv;
2428 if (!qh)
2429 goto done;
2430
Sergei Shtylyov22a0d6f2009-03-27 12:56:26 -07002431 /*
2432 * Any URB not actively programmed into endpoint hardware can be
Sergei Shtylyova2fd8142009-02-21 15:30:45 -08002433 * immediately given back; that's any URB not at the head of an
Felipe Balbi550a7372008-07-24 12:27:36 +03002434 * endpoint queue, unless someday we get real DMA queues. And even
Sergei Shtylyova2fd8142009-02-21 15:30:45 -08002435 * if it's at the head, it might not be known to the hardware...
Felipe Balbi550a7372008-07-24 12:27:36 +03002436 *
Sergei Shtylyov22a0d6f2009-03-27 12:56:26 -07002437 * Otherwise abort current transfer, pending DMA, etc.; urb->status
Felipe Balbi550a7372008-07-24 12:27:36 +03002438 * has already been updated. This is a synchronous abort; it'd be
2439 * OK to hold off until after some IRQ, though.
Sergei Shtylyov22a0d6f2009-03-27 12:56:26 -07002440 *
2441 * NOTE: qh is invalid unless !list_empty(&hep->urb_list)
Felipe Balbi550a7372008-07-24 12:27:36 +03002442 */
Sergei Shtylyov22a0d6f2009-03-27 12:56:26 -07002443 if (!qh->is_ready
2444 || urb->urb_list.prev != &qh->hep->urb_list
2445 || musb_ep_get_qh(qh->hw_ep, is_in) != qh) {
Felipe Balbi550a7372008-07-24 12:27:36 +03002446 int ready = qh->is_ready;
2447
Felipe Balbi550a7372008-07-24 12:27:36 +03002448 qh->is_ready = 0;
Sergei Shtylyovc9cd06b2009-03-27 12:58:31 -07002449 musb_giveback(musb, urb, 0);
Felipe Balbi550a7372008-07-24 12:27:36 +03002450 qh->is_ready = ready;
Sergei Shtylyova2fd8142009-02-21 15:30:45 -08002451
2452 /* If nothing else (usually musb_giveback) is using it
2453 * and its URB list has emptied, recycle this qh.
2454 */
2455 if (ready && list_empty(&qh->hep->urb_list)) {
2456 qh->hep->hcpriv = NULL;
2457 list_del(&qh->ring);
2458 kfree(qh);
2459 }
Felipe Balbi550a7372008-07-24 12:27:36 +03002460 } else
Sergei Shtylyov81ec4e42009-03-27 12:57:50 -07002461 ret = musb_cleanup_urb(urb, qh);
Felipe Balbi550a7372008-07-24 12:27:36 +03002462done:
2463 spin_unlock_irqrestore(&musb->lock, flags);
2464 return ret;
2465}
2466
2467/* disable an endpoint */
2468static void
2469musb_h_disable(struct usb_hcd *hcd, struct usb_host_endpoint *hep)
2470{
Sergei Shtylyov22a0d6f2009-03-27 12:56:26 -07002471 u8 is_in = hep->desc.bEndpointAddress & USB_DIR_IN;
Felipe Balbi550a7372008-07-24 12:27:36 +03002472 unsigned long flags;
2473 struct musb *musb = hcd_to_musb(hcd);
Sergei Shtylyovdc61d232009-02-21 15:31:01 -08002474 struct musb_qh *qh;
2475 struct urb *urb;
Felipe Balbi550a7372008-07-24 12:27:36 +03002476
Felipe Balbi550a7372008-07-24 12:27:36 +03002477 spin_lock_irqsave(&musb->lock, flags);
2478
Sergei Shtylyovdc61d232009-02-21 15:31:01 -08002479 qh = hep->hcpriv;
2480 if (qh == NULL)
2481 goto exit;
2482
Sergei Shtylyov22a0d6f2009-03-27 12:56:26 -07002483 /* NOTE: qh is invalid unless !list_empty(&hep->urb_list) */
Felipe Balbi550a7372008-07-24 12:27:36 +03002484
Sergei Shtylyov22a0d6f2009-03-27 12:56:26 -07002485 /* Kick the first URB off the hardware, if needed */
Felipe Balbi550a7372008-07-24 12:27:36 +03002486 qh->is_ready = 0;
Sergei Shtylyov22a0d6f2009-03-27 12:56:26 -07002487 if (musb_ep_get_qh(qh->hw_ep, is_in) == qh) {
Felipe Balbi550a7372008-07-24 12:27:36 +03002488 urb = next_urb(qh);
2489
2490 /* make software (then hardware) stop ASAP */
2491 if (!urb->unlinked)
2492 urb->status = -ESHUTDOWN;
2493
2494 /* cleanup */
Sergei Shtylyov81ec4e42009-03-27 12:57:50 -07002495 musb_cleanup_urb(urb, qh);
Felipe Balbi550a7372008-07-24 12:27:36 +03002496
Sergei Shtylyovdc61d232009-02-21 15:31:01 -08002497 /* Then nuke all the others ... and advance the
2498 * queue on hw_ep (e.g. bulk ring) when we're done.
2499 */
2500 while (!list_empty(&hep->urb_list)) {
2501 urb = next_urb(qh);
2502 urb->status = -ESHUTDOWN;
2503 musb_advance_schedule(musb, urb, qh->hw_ep, is_in);
2504 }
2505 } else {
2506 /* Just empty the queue; the hardware is busy with
2507 * other transfers, and since !qh->is_ready nothing
2508 * will activate any of these as it advances.
2509 */
2510 while (!list_empty(&hep->urb_list))
Sergei Shtylyovc9cd06b2009-03-27 12:58:31 -07002511 musb_giveback(musb, next_urb(qh), -ESHUTDOWN);
Felipe Balbi550a7372008-07-24 12:27:36 +03002512
Sergei Shtylyovdc61d232009-02-21 15:31:01 -08002513 hep->hcpriv = NULL;
2514 list_del(&qh->ring);
2515 kfree(qh);
2516 }
2517exit:
Felipe Balbi550a7372008-07-24 12:27:36 +03002518 spin_unlock_irqrestore(&musb->lock, flags);
2519}
2520
2521static int musb_h_get_frame_number(struct usb_hcd *hcd)
2522{
2523 struct musb *musb = hcd_to_musb(hcd);
2524
2525 return musb_readw(musb->mregs, MUSB_FRAME);
2526}
2527
2528static int musb_h_start(struct usb_hcd *hcd)
2529{
2530 struct musb *musb = hcd_to_musb(hcd);
2531
2532 /* NOTE: musb_start() is called when the hub driver turns
2533 * on port power, or when (OTG) peripheral starts.
2534 */
2535 hcd->state = HC_STATE_RUNNING;
2536 musb->port1_status = 0;
2537 return 0;
2538}
2539
2540static void musb_h_stop(struct usb_hcd *hcd)
2541{
2542 musb_stop(hcd_to_musb(hcd));
2543 hcd->state = HC_STATE_HALT;
2544}
2545
2546static int musb_bus_suspend(struct usb_hcd *hcd)
2547{
2548 struct musb *musb = hcd_to_musb(hcd);
David Brownell89368d32009-07-01 03:36:16 -07002549 u8 devctl;
Daniel Glöcknerc4f24a02018-05-14 09:40:05 -05002550 int ret;
Felipe Balbi550a7372008-07-24 12:27:36 +03002551
Daniel Glöcknerc4f24a02018-05-14 09:40:05 -05002552 ret = musb_port_suspend(musb, true);
2553 if (ret)
2554 return ret;
Daniel Mack94f72132013-11-25 22:26:41 +01002555
David Brownell89368d32009-07-01 03:36:16 -07002556 if (!is_host_active(musb))
Felipe Balbi550a7372008-07-24 12:27:36 +03002557 return 0;
2558
Antoine Tenarte47d9252014-10-30 18:41:13 +01002559 switch (musb->xceiv->otg->state) {
David Brownell89368d32009-07-01 03:36:16 -07002560 case OTG_STATE_A_SUSPEND:
2561 return 0;
2562 case OTG_STATE_A_WAIT_VRISE:
2563 /* ID could be grounded even if there's no device
2564 * on the other end of the cable. NOTE that the
2565 * A_WAIT_VRISE timers are messy with MUSB...
2566 */
2567 devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
2568 if ((devctl & MUSB_DEVCTL_VBUS) == MUSB_DEVCTL_VBUS)
Antoine Tenarte47d9252014-10-30 18:41:13 +01002569 musb->xceiv->otg->state = OTG_STATE_A_WAIT_BCON;
David Brownell89368d32009-07-01 03:36:16 -07002570 break;
2571 default:
2572 break;
2573 }
2574
2575 if (musb->is_active) {
2576 WARNING("trying to suspend as %s while active\n",
Antoine Tenarte47d9252014-10-30 18:41:13 +01002577 usb_otg_state_string(musb->xceiv->otg->state));
Felipe Balbi550a7372008-07-24 12:27:36 +03002578 return -EBUSY;
2579 } else
2580 return 0;
2581}
2582
2583static int musb_bus_resume(struct usb_hcd *hcd)
2584{
Daniel Mack869c5972013-11-26 13:31:14 +01002585 struct musb *musb = hcd_to_musb(hcd);
2586
2587 if (musb->config &&
2588 musb->config->host_port_deassert_reset_at_resume)
2589 musb_port_reset(musb, false);
2590
Felipe Balbi550a7372008-07-24 12:27:36 +03002591 return 0;
2592}
2593
Ruslan Bilovol8408fd12013-03-29 19:15:21 +02002594#ifndef CONFIG_MUSB_PIO_ONLY
2595
2596#define MUSB_USB_DMA_ALIGN 4
2597
2598struct musb_temp_buffer {
2599 void *kmalloc_ptr;
2600 void *old_xfer_buffer;
2601 u8 data[0];
2602};
2603
2604static void musb_free_temp_buffer(struct urb *urb)
2605{
2606 enum dma_data_direction dir;
2607 struct musb_temp_buffer *temp;
Johan Hovoldd72348f2015-04-23 16:06:50 +02002608 size_t length;
Ruslan Bilovol8408fd12013-03-29 19:15:21 +02002609
2610 if (!(urb->transfer_flags & URB_ALIGNED_TEMP_BUFFER))
2611 return;
2612
2613 dir = usb_urb_dir_in(urb) ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
2614
2615 temp = container_of(urb->transfer_buffer, struct musb_temp_buffer,
2616 data);
2617
2618 if (dir == DMA_FROM_DEVICE) {
Johan Hovoldd72348f2015-04-23 16:06:50 +02002619 if (usb_pipeisoc(urb->pipe))
2620 length = urb->transfer_buffer_length;
2621 else
2622 length = urb->actual_length;
2623
2624 memcpy(temp->old_xfer_buffer, temp->data, length);
Ruslan Bilovol8408fd12013-03-29 19:15:21 +02002625 }
2626 urb->transfer_buffer = temp->old_xfer_buffer;
2627 kfree(temp->kmalloc_ptr);
2628
2629 urb->transfer_flags &= ~URB_ALIGNED_TEMP_BUFFER;
2630}
2631
2632static int musb_alloc_temp_buffer(struct urb *urb, gfp_t mem_flags)
2633{
2634 enum dma_data_direction dir;
2635 struct musb_temp_buffer *temp;
2636 void *kmalloc_ptr;
2637 size_t kmalloc_size;
2638
2639 if (urb->num_sgs || urb->sg ||
2640 urb->transfer_buffer_length == 0 ||
2641 !((uintptr_t)urb->transfer_buffer & (MUSB_USB_DMA_ALIGN - 1)))
2642 return 0;
2643
2644 dir = usb_urb_dir_in(urb) ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
2645
2646 /* Allocate a buffer with enough padding for alignment */
2647 kmalloc_size = urb->transfer_buffer_length +
2648 sizeof(struct musb_temp_buffer) + MUSB_USB_DMA_ALIGN - 1;
2649
2650 kmalloc_ptr = kmalloc(kmalloc_size, mem_flags);
2651 if (!kmalloc_ptr)
2652 return -ENOMEM;
2653
2654 /* Position our struct temp_buffer such that data is aligned */
2655 temp = PTR_ALIGN(kmalloc_ptr, MUSB_USB_DMA_ALIGN);
2656
2657
2658 temp->kmalloc_ptr = kmalloc_ptr;
2659 temp->old_xfer_buffer = urb->transfer_buffer;
2660 if (dir == DMA_TO_DEVICE)
2661 memcpy(temp->data, urb->transfer_buffer,
2662 urb->transfer_buffer_length);
2663 urb->transfer_buffer = temp->data;
2664
2665 urb->transfer_flags |= URB_ALIGNED_TEMP_BUFFER;
2666
2667 return 0;
2668}
2669
2670static int musb_map_urb_for_dma(struct usb_hcd *hcd, struct urb *urb,
2671 gfp_t mem_flags)
2672{
2673 struct musb *musb = hcd_to_musb(hcd);
2674 int ret;
2675
2676 /*
2677 * The DMA engine in RTL1.8 and above cannot handle
2678 * DMA addresses that are not aligned to a 4 byte boundary.
2679 * For such engine implemented (un)map_urb_for_dma hooks.
2680 * Do not use these hooks for RTL<1.8
2681 */
2682 if (musb->hwvers < MUSB_HWVERS_1800)
2683 return usb_hcd_map_urb_for_dma(hcd, urb, mem_flags);
2684
2685 ret = musb_alloc_temp_buffer(urb, mem_flags);
2686 if (ret)
2687 return ret;
2688
2689 ret = usb_hcd_map_urb_for_dma(hcd, urb, mem_flags);
2690 if (ret)
2691 musb_free_temp_buffer(urb);
2692
2693 return ret;
2694}
2695
2696static void musb_unmap_urb_for_dma(struct usb_hcd *hcd, struct urb *urb)
2697{
2698 struct musb *musb = hcd_to_musb(hcd);
2699
2700 usb_hcd_unmap_urb_for_dma(hcd, urb);
2701
2702 /* Do not use this hook for RTL<1.8 (see description above) */
2703 if (musb->hwvers < MUSB_HWVERS_1800)
2704 return;
2705
2706 musb_free_temp_buffer(urb);
2707}
2708#endif /* !CONFIG_MUSB_PIO_ONLY */
2709
Daniel Mack74c2e932013-04-10 21:55:45 +02002710static const struct hc_driver musb_hc_driver = {
Felipe Balbi550a7372008-07-24 12:27:36 +03002711 .description = "musb-hcd",
2712 .product_desc = "MUSB HDRC host driver",
Daniel Mack74c2e932013-04-10 21:55:45 +02002713 .hcd_priv_size = sizeof(struct musb *),
Bin Liuf551e132016-04-25 15:53:30 -05002714 .flags = HCD_USB2 | HCD_MEMORY,
Felipe Balbi550a7372008-07-24 12:27:36 +03002715
2716 /* not using irq handler or reset hooks from usbcore, since
2717 * those must be shared with peripheral code for OTG configs
2718 */
2719
2720 .start = musb_h_start,
2721 .stop = musb_h_stop,
2722
2723 .get_frame_number = musb_h_get_frame_number,
2724
2725 .urb_enqueue = musb_urb_enqueue,
2726 .urb_dequeue = musb_urb_dequeue,
2727 .endpoint_disable = musb_h_disable,
2728
Ruslan Bilovol8408fd12013-03-29 19:15:21 +02002729#ifndef CONFIG_MUSB_PIO_ONLY
2730 .map_urb_for_dma = musb_map_urb_for_dma,
2731 .unmap_urb_for_dma = musb_unmap_urb_for_dma,
2732#endif
2733
Felipe Balbi550a7372008-07-24 12:27:36 +03002734 .hub_status_data = musb_hub_status_data,
2735 .hub_control = musb_hub_control,
2736 .bus_suspend = musb_bus_suspend,
2737 .bus_resume = musb_bus_resume,
2738 /* .start_port_reset = NULL, */
2739 /* .hub_irq_enable = NULL, */
2740};
Daniel Mack0b3eba42013-04-10 21:55:42 +02002741
Daniel Mack74c2e932013-04-10 21:55:45 +02002742int musb_host_alloc(struct musb *musb)
2743{
2744 struct device *dev = musb->controller;
2745
2746 /* usbcore sets dev->driver_data to hcd, and sometimes uses that... */
2747 musb->hcd = usb_create_hcd(&musb_hc_driver, dev, dev_name(dev));
2748 if (!musb->hcd)
2749 return -EINVAL;
2750
2751 *musb->hcd->hcd_priv = (unsigned long) musb;
2752 musb->hcd->self.uses_pio_for_control = 1;
2753 musb->hcd->uses_new_polling = 1;
2754 musb->hcd->has_tt = 1;
2755
2756 return 0;
2757}
2758
2759void musb_host_cleanup(struct musb *musb)
2760{
Sebastian Andrzej Siewior90474282013-08-20 18:35:44 +02002761 if (musb->port_mode == MUSB_PORT_MODE_GADGET)
2762 return;
Daniel Mack74c2e932013-04-10 21:55:45 +02002763 usb_remove_hcd(musb->hcd);
Daniel Mack74c2e932013-04-10 21:55:45 +02002764}
2765
2766void musb_host_free(struct musb *musb)
2767{
2768 usb_put_hcd(musb->hcd);
2769}
2770
Daniel Mack2cc65fe2013-04-10 21:55:47 +02002771int musb_host_setup(struct musb *musb, int power_budget)
2772{
2773 int ret;
2774 struct usb_hcd *hcd = musb->hcd;
2775
Tony Lindgrendbb12732017-05-17 11:23:10 -05002776 if (musb->port_mode == MUSB_PORT_MODE_HOST) {
2777 MUSB_HST_MODE(musb);
2778 musb->xceiv->otg->default_a = 1;
2779 musb->xceiv->otg->state = OTG_STATE_A_IDLE;
2780 }
Daniel Mack2cc65fe2013-04-10 21:55:47 +02002781 otg_set_host(musb->xceiv->otg, &hcd->self);
2782 hcd->self.otg_port = 1;
2783 musb->xceiv->otg->host = &hcd->self;
2784 hcd->power_budget = 2 * (power_budget ? : 250);
2785
2786 ret = usb_add_hcd(hcd, 0, 0);
2787 if (ret < 0)
2788 return ret;
2789
Peter Chen3c9740a2013-11-05 10:46:02 +08002790 device_wakeup_enable(hcd->self.controller);
Daniel Mack2cc65fe2013-04-10 21:55:47 +02002791 return 0;
2792}
2793
Daniel Mack0b3eba42013-04-10 21:55:42 +02002794void musb_host_resume_root_hub(struct musb *musb)
2795{
Daniel Mack74c2e932013-04-10 21:55:45 +02002796 usb_hcd_resume_root_hub(musb->hcd);
Daniel Mack0b3eba42013-04-10 21:55:42 +02002797}
2798
2799void musb_host_poke_root_hub(struct musb *musb)
2800{
2801 MUSB_HST_MODE(musb);
Daniel Mack74c2e932013-04-10 21:55:45 +02002802 if (musb->hcd->status_urb)
2803 usb_hcd_poll_rh_status(musb->hcd);
Daniel Mack0b3eba42013-04-10 21:55:42 +02002804 else
Daniel Mack74c2e932013-04-10 21:55:45 +02002805 usb_hcd_resume_root_hub(musb->hcd);
Daniel Mack0b3eba42013-04-10 21:55:42 +02002806}