xref: /linux/drivers/usb/musb/musb_host.c (revision 0ea5c948cb64bab5bc7a5516774eb8536f05aa0d)
15fd54aceSGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
2550a7375SFelipe Balbi /*
3550a7375SFelipe Balbi  * MUSB OTG driver host support
4550a7375SFelipe Balbi  *
5550a7375SFelipe Balbi  * Copyright 2005 Mentor Graphics Corporation
6550a7375SFelipe Balbi  * Copyright (C) 2005-2006 by Texas Instruments
7550a7375SFelipe Balbi  * Copyright (C) 2006-2007 Nokia Corporation
8c7bbc056SSergei Shtylyov  * Copyright (C) 2008-2009 MontaVista Software, Inc. <source@mvista.com>
9550a7375SFelipe Balbi  */
10550a7375SFelipe Balbi 
11550a7375SFelipe Balbi #include <linux/module.h>
12550a7375SFelipe Balbi #include <linux/kernel.h>
13550a7375SFelipe Balbi #include <linux/delay.h>
14550a7375SFelipe Balbi #include <linux/sched.h>
15550a7375SFelipe Balbi #include <linux/slab.h>
16550a7375SFelipe Balbi #include <linux/errno.h>
17550a7375SFelipe Balbi #include <linux/list.h>
18496dda70SMaulik Mankad #include <linux/dma-mapping.h>
19550a7375SFelipe Balbi 
20550a7375SFelipe Balbi #include "musb_core.h"
21550a7375SFelipe Balbi #include "musb_host.h"
2219ca682eSBin Liu #include "musb_trace.h"
23550a7375SFelipe Balbi 
24550a7375SFelipe Balbi /* MUSB HOST status 22-mar-2006
25550a7375SFelipe Balbi  *
26550a7375SFelipe Balbi  * - There's still lots of partial code duplication for fault paths, so
27550a7375SFelipe Balbi  *   they aren't handled as consistently as they need to be.
28550a7375SFelipe Balbi  *
29550a7375SFelipe Balbi  * - PIO mostly behaved when last tested.
30550a7375SFelipe Balbi  *     + including ep0, with all usbtest cases 9, 10
31550a7375SFelipe Balbi  *     + usbtest 14 (ep0out) doesn't seem to run at all
32550a7375SFelipe Balbi  *     + double buffered OUT/TX endpoints saw stalls(!) with certain usbtest
33550a7375SFelipe Balbi  *       configurations, but otherwise double buffering passes basic tests.
34550a7375SFelipe Balbi  *     + for 2.6.N, for N > ~10, needs API changes for hcd framework.
35550a7375SFelipe Balbi  *
36550a7375SFelipe Balbi  * - DMA (CPPI) ... partially behaves, not currently recommended
37550a7375SFelipe Balbi  *     + about 1/15 the speed of typical EHCI implementations (PCI)
38550a7375SFelipe Balbi  *     + RX, all too often reqpkt seems to misbehave after tx
39550a7375SFelipe Balbi  *     + TX, no known issues (other than evident silicon issue)
40550a7375SFelipe Balbi  *
41550a7375SFelipe Balbi  * - DMA (Mentor/OMAP) ...has at least toggle update problems
42550a7375SFelipe Balbi  *
431e0320f0SAjay Kumar Gupta  * - [23-feb-2009] minimal traffic scheduling to avoid bulk RX packet
441e0320f0SAjay Kumar Gupta  *   starvation ... nothing yet for TX, interrupt, or bulk.
45550a7375SFelipe Balbi  *
46550a7375SFelipe Balbi  * - Not tested with HNP, but some SRP paths seem to behave.
47550a7375SFelipe Balbi  *
48550a7375SFelipe Balbi  * NOTE 24-August-2006:
49550a7375SFelipe Balbi  *
50550a7375SFelipe Balbi  * - Bulk traffic finally uses both sides of hardware ep1, freeing up an
51550a7375SFelipe Balbi  *   extra endpoint for periodic use enabling hub + keybd + mouse.  That
52550a7375SFelipe Balbi  *   mostly works, except that with "usbnet" it's easy to trigger cases
53550a7375SFelipe Balbi  *   with "ping" where RX loses.  (a) ping to davinci, even "ping -f",
54550a7375SFelipe Balbi  *   fine; but (b) ping _from_ davinci, even "ping -c 1", ICMP RX loses
55550a7375SFelipe Balbi  *   although ARP RX wins.  (That test was done with a full speed link.)
56550a7375SFelipe Balbi  */
57550a7375SFelipe Balbi 
58550a7375SFelipe Balbi 
59550a7375SFelipe Balbi /*
60550a7375SFelipe Balbi  * NOTE on endpoint usage:
61550a7375SFelipe Balbi  *
62550a7375SFelipe Balbi  * CONTROL transfers all go through ep0.  BULK ones go through dedicated IN
63550a7375SFelipe Balbi  * and OUT endpoints ... hardware is dedicated for those "async" queue(s).
64550a7375SFelipe Balbi  * (Yes, bulk _could_ use more of the endpoints than that, and would even
651e0320f0SAjay Kumar Gupta  * benefit from it.)
66550a7375SFelipe Balbi  *
67550a7375SFelipe Balbi  * INTERUPPT and ISOCHRONOUS transfers are scheduled to the other endpoints.
68550a7375SFelipe Balbi  * So far that scheduling is both dumb and optimistic:  the endpoint will be
69550a7375SFelipe Balbi  * "claimed" until its software queue is no longer refilled.  No multiplexing
70550a7375SFelipe Balbi  * of transfers between endpoints, or anything clever.
71550a7375SFelipe Balbi  */
72550a7375SFelipe Balbi 
hcd_to_musb(struct usb_hcd * hcd)7374c2e936SDaniel Mack struct musb *hcd_to_musb(struct usb_hcd *hcd)
7474c2e936SDaniel Mack {
7574c2e936SDaniel Mack 	return *(struct musb **) hcd->hcd_priv;
7674c2e936SDaniel Mack }
7774c2e936SDaniel Mack 
78550a7375SFelipe Balbi 
79550a7375SFelipe Balbi static void musb_ep_program(struct musb *musb, u8 epnum,
806b6e9710SSergei Shtylyov 			struct urb *urb, int is_out,
816b6e9710SSergei Shtylyov 			u8 *buf, u32 offset, u32 len);
82550a7375SFelipe Balbi 
83550a7375SFelipe Balbi /*
84550a7375SFelipe Balbi  * Clear TX fifo. Needed to avoid BABBLE errors.
85550a7375SFelipe Balbi  */
musb_h_tx_flush_fifo(struct musb_hw_ep * ep)86c767c1c6SDavid Brownell static void musb_h_tx_flush_fifo(struct musb_hw_ep *ep)
87550a7375SFelipe Balbi {
885c8a86e1SFelipe Balbi 	struct musb	*musb = ep->musb;
89550a7375SFelipe Balbi 	void __iomem	*epio = ep->regs;
90550a7375SFelipe Balbi 	u16		csr;
91550a7375SFelipe Balbi 	int		retries = 1000;
92550a7375SFelipe Balbi 
93550a7375SFelipe Balbi 	csr = musb_readw(epio, MUSB_TXCSR);
94550a7375SFelipe Balbi 	while (csr & MUSB_TXCSR_FIFONOTEMPTY) {
952ccc6d30SDaniel Mack 		csr |= MUSB_TXCSR_FLUSHFIFO | MUSB_TXCSR_TXPKTRDY;
96550a7375SFelipe Balbi 		musb_writew(epio, MUSB_TXCSR, csr);
97550a7375SFelipe Balbi 		csr = musb_readw(epio, MUSB_TXCSR);
9868fe05e2SBin Liu 
9968fe05e2SBin Liu 		/*
10068fe05e2SBin Liu 		 * FIXME: sometimes the tx fifo flush failed, it has been
10168fe05e2SBin Liu 		 * observed during device disconnect on AM335x.
10268fe05e2SBin Liu 		 *
10368fe05e2SBin Liu 		 * To reproduce the issue, ensure tx urb(s) are queued when
10468fe05e2SBin Liu 		 * unplug the usb device which is connected to AM335x usb
10568fe05e2SBin Liu 		 * host port.
10668fe05e2SBin Liu 		 *
10768fe05e2SBin Liu 		 * I found using a usb-ethernet device and running iperf
10868fe05e2SBin Liu 		 * (client on AM335x) has very high chance to trigger it.
10968fe05e2SBin Liu 		 *
110b99d3659SBin Liu 		 * Better to turn on musb_dbg() in musb_cleanup_urb() with
11168fe05e2SBin Liu 		 * CPPI enabled to see the issue when aborting the tx channel.
11268fe05e2SBin Liu 		 */
11368fe05e2SBin Liu 		if (dev_WARN_ONCE(musb->controller, retries-- < 1,
114bb1c9ef1SDavid Brownell 				"Could not flush host TX%d fifo: csr: %04x\n",
115bb1c9ef1SDavid Brownell 				ep->epnum, csr))
116550a7375SFelipe Balbi 			return;
11745d73860SBin Liu 		mdelay(1);
118550a7375SFelipe Balbi 	}
119550a7375SFelipe Balbi }
120550a7375SFelipe Balbi 
musb_h_ep0_flush_fifo(struct musb_hw_ep * ep)12178322c1aSDavid Brownell static void musb_h_ep0_flush_fifo(struct musb_hw_ep *ep)
12278322c1aSDavid Brownell {
12378322c1aSDavid Brownell 	void __iomem	*epio = ep->regs;
12478322c1aSDavid Brownell 	u16		csr;
12578322c1aSDavid Brownell 	int		retries = 5;
12678322c1aSDavid Brownell 
12778322c1aSDavid Brownell 	/* scrub any data left in the fifo */
12878322c1aSDavid Brownell 	do {
12978322c1aSDavid Brownell 		csr = musb_readw(epio, MUSB_TXCSR);
13078322c1aSDavid Brownell 		if (!(csr & (MUSB_CSR0_TXPKTRDY | MUSB_CSR0_RXPKTRDY)))
13178322c1aSDavid Brownell 			break;
13278322c1aSDavid Brownell 		musb_writew(epio, MUSB_TXCSR, MUSB_CSR0_FLUSHFIFO);
13378322c1aSDavid Brownell 		csr = musb_readw(epio, MUSB_TXCSR);
13478322c1aSDavid Brownell 		udelay(10);
13578322c1aSDavid Brownell 	} while (--retries);
13678322c1aSDavid Brownell 
13778322c1aSDavid Brownell 	WARN(!retries, "Could not flush host TX%d fifo: csr: %04x\n",
13878322c1aSDavid Brownell 			ep->epnum, csr);
13978322c1aSDavid Brownell 
14078322c1aSDavid Brownell 	/* and reset for the next transfer */
14178322c1aSDavid Brownell 	musb_writew(epio, MUSB_TXCSR, 0);
14278322c1aSDavid Brownell }
14378322c1aSDavid Brownell 
144550a7375SFelipe Balbi /*
145550a7375SFelipe Balbi  * Start transmit. Caller is responsible for locking shared resources.
146550a7375SFelipe Balbi  * musb must be locked.
147550a7375SFelipe Balbi  */
musb_h_tx_start(struct musb_hw_ep * ep)148550a7375SFelipe Balbi static inline void musb_h_tx_start(struct musb_hw_ep *ep)
149550a7375SFelipe Balbi {
150550a7375SFelipe Balbi 	u16	txcsr;
151550a7375SFelipe Balbi 
152550a7375SFelipe Balbi 	/* NOTE: no locks here; caller should lock and select EP */
153550a7375SFelipe Balbi 	if (ep->epnum) {
154550a7375SFelipe Balbi 		txcsr = musb_readw(ep->regs, MUSB_TXCSR);
155550a7375SFelipe Balbi 		txcsr |= MUSB_TXCSR_TXPKTRDY | MUSB_TXCSR_H_WZC_BITS;
156550a7375SFelipe Balbi 		musb_writew(ep->regs, MUSB_TXCSR, txcsr);
157550a7375SFelipe Balbi 	} else {
158550a7375SFelipe Balbi 		txcsr = MUSB_CSR0_H_SETUPPKT | MUSB_CSR0_TXPKTRDY;
159550a7375SFelipe Balbi 		musb_writew(ep->regs, MUSB_CSR0, txcsr);
160550a7375SFelipe Balbi 	}
161550a7375SFelipe Balbi 
162550a7375SFelipe Balbi }
163550a7375SFelipe Balbi 
musb_h_tx_dma_start(struct musb_hw_ep * ep)164c7bbc056SSergei Shtylyov static inline void musb_h_tx_dma_start(struct musb_hw_ep *ep)
165550a7375SFelipe Balbi {
166550a7375SFelipe Balbi 	u16	txcsr;
167550a7375SFelipe Balbi 
168550a7375SFelipe Balbi 	/* NOTE: no locks here; caller should lock and select EP */
169550a7375SFelipe Balbi 	txcsr = musb_readw(ep->regs, MUSB_TXCSR);
170550a7375SFelipe Balbi 	txcsr |= MUSB_TXCSR_DMAENAB | MUSB_TXCSR_H_WZC_BITS;
171f8e9f34fSTony Lindgren 	if (is_cppi_enabled(ep->musb))
172c7bbc056SSergei Shtylyov 		txcsr |= MUSB_TXCSR_DMAMODE;
173550a7375SFelipe Balbi 	musb_writew(ep->regs, MUSB_TXCSR, txcsr);
174550a7375SFelipe Balbi }
175550a7375SFelipe Balbi 
musb_ep_set_qh(struct musb_hw_ep * ep,int is_in,struct musb_qh * qh)1763e5c6dc7SSergei Shtylyov static void musb_ep_set_qh(struct musb_hw_ep *ep, int is_in, struct musb_qh *qh)
1773e5c6dc7SSergei Shtylyov {
1783e5c6dc7SSergei Shtylyov 	if (is_in != 0 || ep->is_shared_fifo)
1793e5c6dc7SSergei Shtylyov 		ep->in_qh  = qh;
1803e5c6dc7SSergei Shtylyov 	if (is_in == 0 || ep->is_shared_fifo)
1813e5c6dc7SSergei Shtylyov 		ep->out_qh = qh;
1823e5c6dc7SSergei Shtylyov }
1833e5c6dc7SSergei Shtylyov 
musb_ep_get_qh(struct musb_hw_ep * ep,int is_in)1843e5c6dc7SSergei Shtylyov static struct musb_qh *musb_ep_get_qh(struct musb_hw_ep *ep, int is_in)
1853e5c6dc7SSergei Shtylyov {
1863e5c6dc7SSergei Shtylyov 	return is_in ? ep->in_qh : ep->out_qh;
1873e5c6dc7SSergei Shtylyov }
1883e5c6dc7SSergei Shtylyov 
189550a7375SFelipe Balbi /*
190550a7375SFelipe Balbi  * Start the URB at the front of an endpoint's queue
191550a7375SFelipe Balbi  * end must be claimed from the caller.
192550a7375SFelipe Balbi  *
193550a7375SFelipe Balbi  * Context: controller locked, irqs blocked
194550a7375SFelipe Balbi  */
195550a7375SFelipe Balbi static void
musb_start_urb(struct musb * musb,int is_in,struct musb_qh * qh)196550a7375SFelipe Balbi musb_start_urb(struct musb *musb, int is_in, struct musb_qh *qh)
197550a7375SFelipe Balbi {
198550a7375SFelipe Balbi 	u32			len;
199550a7375SFelipe Balbi 	void __iomem		*mbase =  musb->mregs;
200550a7375SFelipe Balbi 	struct urb		*urb = next_urb(qh);
2016b6e9710SSergei Shtylyov 	void			*buf = urb->transfer_buffer;
2026b6e9710SSergei Shtylyov 	u32			offset = 0;
203550a7375SFelipe Balbi 	struct musb_hw_ep	*hw_ep = qh->hw_ep;
204550a7375SFelipe Balbi 	int			epnum = hw_ep->epnum;
205550a7375SFelipe Balbi 
206550a7375SFelipe Balbi 	/* initialize software qh state */
207550a7375SFelipe Balbi 	qh->offset = 0;
208550a7375SFelipe Balbi 	qh->segsize = 0;
209550a7375SFelipe Balbi 
210550a7375SFelipe Balbi 	/* gather right source of data */
211550a7375SFelipe Balbi 	switch (qh->type) {
212550a7375SFelipe Balbi 	case USB_ENDPOINT_XFER_CONTROL:
213550a7375SFelipe Balbi 		/* control transfers always start with SETUP */
214550a7375SFelipe Balbi 		is_in = 0;
215550a7375SFelipe Balbi 		musb->ep0_stage = MUSB_EP0_START;
216550a7375SFelipe Balbi 		buf = urb->setup_packet;
217550a7375SFelipe Balbi 		len = 8;
218550a7375SFelipe Balbi 		break;
219550a7375SFelipe Balbi 	case USB_ENDPOINT_XFER_ISOC:
220550a7375SFelipe Balbi 		qh->iso_idx = 0;
221550a7375SFelipe Balbi 		qh->frame = 0;
2226b6e9710SSergei Shtylyov 		offset = urb->iso_frame_desc[0].offset;
223550a7375SFelipe Balbi 		len = urb->iso_frame_desc[0].length;
224550a7375SFelipe Balbi 		break;
225550a7375SFelipe Balbi 	default:		/* bulk, interrupt */
2261e0320f0SAjay Kumar Gupta 		/* actual_length may be nonzero on retry paths */
2271e0320f0SAjay Kumar Gupta 		buf = urb->transfer_buffer + urb->actual_length;
2281e0320f0SAjay Kumar Gupta 		len = urb->transfer_buffer_length - urb->actual_length;
229550a7375SFelipe Balbi 	}
230550a7375SFelipe Balbi 
23119ca682eSBin Liu 	trace_musb_urb_start(musb, urb);
232550a7375SFelipe Balbi 
233550a7375SFelipe Balbi 	/* Configure endpoint */
2343e5c6dc7SSergei Shtylyov 	musb_ep_set_qh(hw_ep, is_in, qh);
2356b6e9710SSergei Shtylyov 	musb_ep_program(musb, epnum, urb, !is_in, buf, offset, len);
236550a7375SFelipe Balbi 
237550a7375SFelipe Balbi 	/* transmit may have more work: start it when it is time */
238550a7375SFelipe Balbi 	if (is_in)
239550a7375SFelipe Balbi 		return;
240550a7375SFelipe Balbi 
241550a7375SFelipe Balbi 	/* determine if the time is right for a periodic transfer */
242550a7375SFelipe Balbi 	switch (qh->type) {
243550a7375SFelipe Balbi 	case USB_ENDPOINT_XFER_ISOC:
244550a7375SFelipe Balbi 	case USB_ENDPOINT_XFER_INT:
245b99d3659SBin Liu 		musb_dbg(musb, "check whether there's still time for periodic Tx");
246550a7375SFelipe Balbi 		/* FIXME this doesn't implement that scheduling policy ...
247550a7375SFelipe Balbi 		 * or handle framecounter wrapping
248550a7375SFelipe Balbi 		 */
2498a1ea51fSAlan Stern 		if (1) {	/* Always assume URB_ISO_ASAP */
250550a7375SFelipe Balbi 			/* REVISIT the SOF irq handler shouldn't duplicate
251550a7375SFelipe Balbi 			 * this code; and we don't init urb->start_frame...
252550a7375SFelipe Balbi 			 */
253550a7375SFelipe Balbi 			qh->frame = 0;
254550a7375SFelipe Balbi 			goto start;
255550a7375SFelipe Balbi 		} else {
256550a7375SFelipe Balbi 			qh->frame = urb->start_frame;
257550a7375SFelipe Balbi 			/* enable SOF interrupt so we can count down */
258b99d3659SBin Liu 			musb_dbg(musb, "SOF for %d", epnum);
259550a7375SFelipe Balbi #if 1 /* ifndef	CONFIG_ARCH_DAVINCI */
260550a7375SFelipe Balbi 			musb_writeb(mbase, MUSB_INTRUSBE, 0xff);
261550a7375SFelipe Balbi #endif
262550a7375SFelipe Balbi 		}
263550a7375SFelipe Balbi 		break;
264550a7375SFelipe Balbi 	default:
265550a7375SFelipe Balbi start:
266b99d3659SBin Liu 		musb_dbg(musb, "Start TX%d %s", epnum,
267550a7375SFelipe Balbi 			hw_ep->tx_channel ? "dma" : "pio");
268550a7375SFelipe Balbi 
269550a7375SFelipe Balbi 		if (!hw_ep->tx_channel)
270550a7375SFelipe Balbi 			musb_h_tx_start(hw_ep);
271f8e9f34fSTony Lindgren 		else if (is_cppi_enabled(musb) || tusb_dma_omap(musb))
272c7bbc056SSergei Shtylyov 			musb_h_tx_dma_start(hw_ep);
273550a7375SFelipe Balbi 	}
274550a7375SFelipe Balbi }
275550a7375SFelipe Balbi 
276c9cd06b3SSergei Shtylyov /* Context: caller owns controller lock, IRQs are blocked */
musb_giveback(struct musb * musb,struct urb * urb,int status)277c9cd06b3SSergei Shtylyov static void musb_giveback(struct musb *musb, struct urb *urb, int status)
278550a7375SFelipe Balbi __releases(musb->lock)
279550a7375SFelipe Balbi __acquires(musb->lock)
280550a7375SFelipe Balbi {
28119ca682eSBin Liu 	trace_musb_urb_gb(musb, urb);
282550a7375SFelipe Balbi 
2838b125df5SDaniel Mack 	usb_hcd_unlink_urb_from_ep(musb->hcd, urb);
284550a7375SFelipe Balbi 	spin_unlock(&musb->lock);
2858b125df5SDaniel Mack 	usb_hcd_giveback_urb(musb->hcd, urb, status);
286550a7375SFelipe Balbi 	spin_lock(&musb->lock);
287550a7375SFelipe Balbi }
288550a7375SFelipe Balbi 
289c9cd06b3SSergei Shtylyov /*
290c9cd06b3SSergei Shtylyov  * Advance this hardware endpoint's queue, completing the specified URB and
291c9cd06b3SSergei Shtylyov  * advancing to either the next URB queued to that qh, or else invalidating
292c9cd06b3SSergei Shtylyov  * that qh and advancing to the next qh scheduled after the current one.
293c9cd06b3SSergei Shtylyov  *
294c9cd06b3SSergei Shtylyov  * Context: caller owns controller lock, IRQs are blocked
295c9cd06b3SSergei Shtylyov  */
musb_advance_schedule(struct musb * musb,struct urb * urb,struct musb_hw_ep * hw_ep,int is_in)296c9cd06b3SSergei Shtylyov static void musb_advance_schedule(struct musb *musb, struct urb *urb,
297c9cd06b3SSergei Shtylyov 				  struct musb_hw_ep *hw_ep, int is_in)
298550a7375SFelipe Balbi {
299c9cd06b3SSergei Shtylyov 	struct musb_qh		*qh = musb_ep_get_qh(hw_ep, is_in);
300550a7375SFelipe Balbi 	struct musb_hw_ep	*ep = qh->hw_ep;
301550a7375SFelipe Balbi 	int			ready = qh->is_ready;
302c9cd06b3SSergei Shtylyov 	int			status;
303fe3bbd6bSMin Guo 	u16			toggle;
304c9cd06b3SSergei Shtylyov 
305c9cd06b3SSergei Shtylyov 	status = (urb->status == -EINPROGRESS) ? 0 : urb->status;
306550a7375SFelipe Balbi 
307550a7375SFelipe Balbi 	/* save toggle eagerly, for paranoia */
308550a7375SFelipe Balbi 	switch (qh->type) {
309550a7375SFelipe Balbi 	case USB_ENDPOINT_XFER_BULK:
310550a7375SFelipe Balbi 	case USB_ENDPOINT_XFER_INT:
311fe3bbd6bSMin Guo 		toggle = musb->io.get_toggle(qh, !is_in);
312fe3bbd6bSMin Guo 		usb_settoggle(urb->dev, qh->epnum, !is_in, toggle ? 1 : 0);
313550a7375SFelipe Balbi 		break;
314550a7375SFelipe Balbi 	case USB_ENDPOINT_XFER_ISOC:
3151fe975f9SSergei Shtylyov 		if (status == 0 && urb->error_count)
316550a7375SFelipe Balbi 			status = -EXDEV;
317550a7375SFelipe Balbi 		break;
318550a7375SFelipe Balbi 	}
319550a7375SFelipe Balbi 
320550a7375SFelipe Balbi 	qh->is_ready = 0;
321c9cd06b3SSergei Shtylyov 	musb_giveback(musb, urb, status);
322550a7375SFelipe Balbi 	qh->is_ready = ready;
323550a7375SFelipe Balbi 
324*33d7e372SXingxing Luo 	/*
325*33d7e372SXingxing Luo 	 * musb->lock had been unlocked in musb_giveback, so qh may
326*33d7e372SXingxing Luo 	 * be freed, need to get it again
327*33d7e372SXingxing Luo 	 */
328*33d7e372SXingxing Luo 	qh = musb_ep_get_qh(hw_ep, is_in);
329*33d7e372SXingxing Luo 
330550a7375SFelipe Balbi 	/* reclaim resources (and bandwidth) ASAP; deschedule it, and
331550a7375SFelipe Balbi 	 * invalidate qh as soon as list_empty(&hep->urb_list)
332550a7375SFelipe Balbi 	 */
333*33d7e372SXingxing Luo 	if (qh && list_empty(&qh->hep->urb_list)) {
334550a7375SFelipe Balbi 		struct list_head	*head;
3358c778db9SAjay Kumar Gupta 		struct dma_controller	*dma = musb->dma_controller;
336550a7375SFelipe Balbi 
3378c778db9SAjay Kumar Gupta 		if (is_in) {
338550a7375SFelipe Balbi 			ep->rx_reinit = 1;
3398c778db9SAjay Kumar Gupta 			if (ep->rx_channel) {
3408c778db9SAjay Kumar Gupta 				dma->channel_release(ep->rx_channel);
3418c778db9SAjay Kumar Gupta 				ep->rx_channel = NULL;
3428c778db9SAjay Kumar Gupta 			}
3438c778db9SAjay Kumar Gupta 		} else {
344550a7375SFelipe Balbi 			ep->tx_reinit = 1;
3458c778db9SAjay Kumar Gupta 			if (ep->tx_channel) {
3468c778db9SAjay Kumar Gupta 				dma->channel_release(ep->tx_channel);
3478c778db9SAjay Kumar Gupta 				ep->tx_channel = NULL;
3488c778db9SAjay Kumar Gupta 			}
3498c778db9SAjay Kumar Gupta 		}
350550a7375SFelipe Balbi 
3513e5c6dc7SSergei Shtylyov 		/* Clobber old pointers to this qh */
3523e5c6dc7SSergei Shtylyov 		musb_ep_set_qh(ep, is_in, NULL);
353550a7375SFelipe Balbi 		qh->hep->hcpriv = NULL;
354550a7375SFelipe Balbi 
355550a7375SFelipe Balbi 		switch (qh->type) {
356550a7375SFelipe Balbi 
35723d15e07SAjay Kumar Gupta 		case USB_ENDPOINT_XFER_CONTROL:
35823d15e07SAjay Kumar Gupta 		case USB_ENDPOINT_XFER_BULK:
35923d15e07SAjay Kumar Gupta 			/* fifo policy for these lists, except that NAKing
36023d15e07SAjay Kumar Gupta 			 * should rotate a qh to the end (for fairness).
36123d15e07SAjay Kumar Gupta 			 */
36223d15e07SAjay Kumar Gupta 			if (qh->mux == 1) {
36323d15e07SAjay Kumar Gupta 				head = qh->ring.prev;
36423d15e07SAjay Kumar Gupta 				list_del(&qh->ring);
36523d15e07SAjay Kumar Gupta 				kfree(qh);
36623d15e07SAjay Kumar Gupta 				qh = first_qh(head);
36723d15e07SAjay Kumar Gupta 				break;
36823d15e07SAjay Kumar Gupta 			}
369df561f66SGustavo A. R. Silva 			fallthrough;
37023d15e07SAjay Kumar Gupta 
371550a7375SFelipe Balbi 		case USB_ENDPOINT_XFER_ISOC:
372550a7375SFelipe Balbi 		case USB_ENDPOINT_XFER_INT:
373550a7375SFelipe Balbi 			/* this is where periodic bandwidth should be
374550a7375SFelipe Balbi 			 * de-allocated if it's tracked and allocated;
375550a7375SFelipe Balbi 			 * and where we'd update the schedule tree...
376550a7375SFelipe Balbi 			 */
377550a7375SFelipe Balbi 			kfree(qh);
378550a7375SFelipe Balbi 			qh = NULL;
379550a7375SFelipe Balbi 			break;
380550a7375SFelipe Balbi 		}
381550a7375SFelipe Balbi 	}
382550a7375SFelipe Balbi 
38344eb5e12SBin Liu 	if (qh != NULL && qh->is_ready) {
384b99d3659SBin Liu 		musb_dbg(musb, "... next ep%d %cX urb %p",
385c9cd06b3SSergei Shtylyov 		    hw_ep->epnum, is_in ? 'R' : 'T', next_urb(qh));
386550a7375SFelipe Balbi 		musb_start_urb(musb, is_in, qh);
387550a7375SFelipe Balbi 	}
388550a7375SFelipe Balbi }
389550a7375SFelipe Balbi 
musb_h_flush_rxfifo(struct musb_hw_ep * hw_ep,u16 csr)390c767c1c6SDavid Brownell static u16 musb_h_flush_rxfifo(struct musb_hw_ep *hw_ep, u16 csr)
391550a7375SFelipe Balbi {
392550a7375SFelipe Balbi 	/* we don't want fifo to fill itself again;
393550a7375SFelipe Balbi 	 * ignore dma (various models),
394550a7375SFelipe Balbi 	 * leave toggle alone (may not have been saved yet)
395550a7375SFelipe Balbi 	 */
396550a7375SFelipe Balbi 	csr |= MUSB_RXCSR_FLUSHFIFO | MUSB_RXCSR_RXPKTRDY;
397550a7375SFelipe Balbi 	csr &= ~(MUSB_RXCSR_H_REQPKT
398550a7375SFelipe Balbi 		| MUSB_RXCSR_H_AUTOREQ
399550a7375SFelipe Balbi 		| MUSB_RXCSR_AUTOCLEAR);
400550a7375SFelipe Balbi 
401550a7375SFelipe Balbi 	/* write 2x to allow double buffering */
402550a7375SFelipe Balbi 	musb_writew(hw_ep->regs, MUSB_RXCSR, csr);
403550a7375SFelipe Balbi 	musb_writew(hw_ep->regs, MUSB_RXCSR, csr);
404550a7375SFelipe Balbi 
405550a7375SFelipe Balbi 	/* flush writebuffer */
406550a7375SFelipe Balbi 	return musb_readw(hw_ep->regs, MUSB_RXCSR);
407550a7375SFelipe Balbi }
408550a7375SFelipe Balbi 
409550a7375SFelipe Balbi /*
410550a7375SFelipe Balbi  * PIO RX for a packet (or part of it).
411550a7375SFelipe Balbi  */
412550a7375SFelipe Balbi static bool
musb_host_packet_rx(struct musb * musb,struct urb * urb,u8 epnum,u8 iso_err)413550a7375SFelipe Balbi musb_host_packet_rx(struct musb *musb, struct urb *urb, u8 epnum, u8 iso_err)
414550a7375SFelipe Balbi {
415550a7375SFelipe Balbi 	u16			rx_count;
416550a7375SFelipe Balbi 	u8			*buf;
417550a7375SFelipe Balbi 	u16			csr;
418550a7375SFelipe Balbi 	bool			done = false;
419550a7375SFelipe Balbi 	u32			length;
420550a7375SFelipe Balbi 	int			do_flush = 0;
421550a7375SFelipe Balbi 	struct musb_hw_ep	*hw_ep = musb->endpoints + epnum;
422550a7375SFelipe Balbi 	void __iomem		*epio = hw_ep->regs;
423550a7375SFelipe Balbi 	struct musb_qh		*qh = hw_ep->in_qh;
424550a7375SFelipe Balbi 	int			pipe = urb->pipe;
425550a7375SFelipe Balbi 	void			*buffer = urb->transfer_buffer;
426550a7375SFelipe Balbi 
427550a7375SFelipe Balbi 	/* musb_ep_select(mbase, epnum); */
428550a7375SFelipe Balbi 	rx_count = musb_readw(epio, MUSB_RXCOUNT);
429b99d3659SBin Liu 	musb_dbg(musb, "RX%d count %d, buffer %p len %d/%d", epnum, rx_count,
430550a7375SFelipe Balbi 			urb->transfer_buffer, qh->offset,
431550a7375SFelipe Balbi 			urb->transfer_buffer_length);
432550a7375SFelipe Balbi 
433550a7375SFelipe Balbi 	/* unload FIFO */
434550a7375SFelipe Balbi 	if (usb_pipeisoc(pipe)) {
435550a7375SFelipe Balbi 		int					status = 0;
436550a7375SFelipe Balbi 		struct usb_iso_packet_descriptor	*d;
437550a7375SFelipe Balbi 
438550a7375SFelipe Balbi 		if (iso_err) {
439550a7375SFelipe Balbi 			status = -EILSEQ;
440550a7375SFelipe Balbi 			urb->error_count++;
441550a7375SFelipe Balbi 		}
442550a7375SFelipe Balbi 
443550a7375SFelipe Balbi 		d = urb->iso_frame_desc + qh->iso_idx;
444550a7375SFelipe Balbi 		buf = buffer + d->offset;
445550a7375SFelipe Balbi 		length = d->length;
446550a7375SFelipe Balbi 		if (rx_count > length) {
447550a7375SFelipe Balbi 			if (status == 0) {
448550a7375SFelipe Balbi 				status = -EOVERFLOW;
449550a7375SFelipe Balbi 				urb->error_count++;
450550a7375SFelipe Balbi 			}
451b99d3659SBin Liu 			musb_dbg(musb, "OVERFLOW %d into %d", rx_count, length);
452550a7375SFelipe Balbi 			do_flush = 1;
453550a7375SFelipe Balbi 		} else
454550a7375SFelipe Balbi 			length = rx_count;
455550a7375SFelipe Balbi 		urb->actual_length += length;
456550a7375SFelipe Balbi 		d->actual_length = length;
457550a7375SFelipe Balbi 
458550a7375SFelipe Balbi 		d->status = status;
459550a7375SFelipe Balbi 
460550a7375SFelipe Balbi 		/* see if we are done */
461550a7375SFelipe Balbi 		done = (++qh->iso_idx >= urb->number_of_packets);
462550a7375SFelipe Balbi 	} else {
463550a7375SFelipe Balbi 		/* non-isoch */
464550a7375SFelipe Balbi 		buf = buffer + qh->offset;
465550a7375SFelipe Balbi 		length = urb->transfer_buffer_length - qh->offset;
466550a7375SFelipe Balbi 		if (rx_count > length) {
467550a7375SFelipe Balbi 			if (urb->status == -EINPROGRESS)
468550a7375SFelipe Balbi 				urb->status = -EOVERFLOW;
469b99d3659SBin Liu 			musb_dbg(musb, "OVERFLOW %d into %d", rx_count, length);
470550a7375SFelipe Balbi 			do_flush = 1;
471550a7375SFelipe Balbi 		} else
472550a7375SFelipe Balbi 			length = rx_count;
473550a7375SFelipe Balbi 		urb->actual_length += length;
474550a7375SFelipe Balbi 		qh->offset += length;
475550a7375SFelipe Balbi 
476550a7375SFelipe Balbi 		/* see if we are done */
477550a7375SFelipe Balbi 		done = (urb->actual_length == urb->transfer_buffer_length)
478550a7375SFelipe Balbi 			|| (rx_count < qh->maxpacket)
479550a7375SFelipe Balbi 			|| (urb->status != -EINPROGRESS);
480550a7375SFelipe Balbi 		if (done
481550a7375SFelipe Balbi 				&& (urb->status == -EINPROGRESS)
482550a7375SFelipe Balbi 				&& (urb->transfer_flags & URB_SHORT_NOT_OK)
483550a7375SFelipe Balbi 				&& (urb->actual_length
484550a7375SFelipe Balbi 					< urb->transfer_buffer_length))
485550a7375SFelipe Balbi 			urb->status = -EREMOTEIO;
486550a7375SFelipe Balbi 	}
487550a7375SFelipe Balbi 
488550a7375SFelipe Balbi 	musb_read_fifo(hw_ep, length, buf);
489550a7375SFelipe Balbi 
490550a7375SFelipe Balbi 	csr = musb_readw(epio, MUSB_RXCSR);
491550a7375SFelipe Balbi 	csr |= MUSB_RXCSR_H_WZC_BITS;
492550a7375SFelipe Balbi 	if (unlikely(do_flush))
493550a7375SFelipe Balbi 		musb_h_flush_rxfifo(hw_ep, csr);
494550a7375SFelipe Balbi 	else {
495550a7375SFelipe Balbi 		/* REVISIT this assumes AUTOCLEAR is never set */
496550a7375SFelipe Balbi 		csr &= ~(MUSB_RXCSR_RXPKTRDY | MUSB_RXCSR_H_REQPKT);
497550a7375SFelipe Balbi 		if (!done)
498550a7375SFelipe Balbi 			csr |= MUSB_RXCSR_H_REQPKT;
499550a7375SFelipe Balbi 		musb_writew(epio, MUSB_RXCSR, csr);
500550a7375SFelipe Balbi 	}
501550a7375SFelipe Balbi 
502550a7375SFelipe Balbi 	return done;
503550a7375SFelipe Balbi }
504550a7375SFelipe Balbi 
505550a7375SFelipe Balbi /* we don't always need to reinit a given side of an endpoint...
506550a7375SFelipe Balbi  * when we do, use tx/rx reinit routine and then construct a new CSR
507550a7375SFelipe Balbi  * to address data toggle, NYET, and DMA or PIO.
508550a7375SFelipe Balbi  *
509550a7375SFelipe Balbi  * it's possible that driver bugs (especially for DMA) or aborting a
510550a7375SFelipe Balbi  * transfer might have left the endpoint busier than it should be.
511550a7375SFelipe Balbi  * the busy/not-empty tests are basically paranoia.
512550a7375SFelipe Balbi  */
513550a7375SFelipe Balbi static void
musb_rx_reinit(struct musb * musb,struct musb_qh * qh,u8 epnum)5140cb74b3dSHans de Goede musb_rx_reinit(struct musb *musb, struct musb_qh *qh, u8 epnum)
515550a7375SFelipe Balbi {
5160cb74b3dSHans de Goede 	struct musb_hw_ep *ep = musb->endpoints + epnum;
517550a7375SFelipe Balbi 	u16	csr;
518550a7375SFelipe Balbi 
519550a7375SFelipe Balbi 	/* NOTE:  we know the "rx" fifo reinit never triggers for ep0.
520550a7375SFelipe Balbi 	 * That always uses tx_reinit since ep0 repurposes TX register
521550a7375SFelipe Balbi 	 * offsets; the initial SETUP packet is also a kind of OUT.
522550a7375SFelipe Balbi 	 */
523550a7375SFelipe Balbi 
524550a7375SFelipe Balbi 	/* if programmed for Tx, put it in RX mode */
525550a7375SFelipe Balbi 	if (ep->is_shared_fifo) {
526550a7375SFelipe Balbi 		csr = musb_readw(ep->regs, MUSB_TXCSR);
527550a7375SFelipe Balbi 		if (csr & MUSB_TXCSR_MODE) {
528550a7375SFelipe Balbi 			musb_h_tx_flush_fifo(ep);
529b6e434a5SSergei Shtylyov 			csr = musb_readw(ep->regs, MUSB_TXCSR);
530550a7375SFelipe Balbi 			musb_writew(ep->regs, MUSB_TXCSR,
531b6e434a5SSergei Shtylyov 				    csr | MUSB_TXCSR_FRCDATATOG);
532550a7375SFelipe Balbi 		}
533b6e434a5SSergei Shtylyov 
534b6e434a5SSergei Shtylyov 		/*
535b6e434a5SSergei Shtylyov 		 * Clear the MODE bit (and everything else) to enable Rx.
536b6e434a5SSergei Shtylyov 		 * NOTE: we mustn't clear the DMAMODE bit before DMAENAB.
537b6e434a5SSergei Shtylyov 		 */
538b6e434a5SSergei Shtylyov 		if (csr & MUSB_TXCSR_DMAMODE)
539b6e434a5SSergei Shtylyov 			musb_writew(ep->regs, MUSB_TXCSR, MUSB_TXCSR_DMAMODE);
540550a7375SFelipe Balbi 		musb_writew(ep->regs, MUSB_TXCSR, 0);
541550a7375SFelipe Balbi 
542550a7375SFelipe Balbi 	/* scrub all previous state, clearing toggle */
543f3eec0cfSAndrew Goodbody 	}
544550a7375SFelipe Balbi 	csr = musb_readw(ep->regs, MUSB_RXCSR);
545550a7375SFelipe Balbi 	if (csr & MUSB_RXCSR_RXPKTRDY)
546550a7375SFelipe Balbi 		WARNING("rx%d, packet/%d ready?\n", ep->epnum,
547550a7375SFelipe Balbi 			musb_readw(ep->regs, MUSB_RXCOUNT));
548550a7375SFelipe Balbi 
549550a7375SFelipe Balbi 	musb_h_flush_rxfifo(ep, MUSB_RXCSR_CLRDATATOG);
550550a7375SFelipe Balbi 
551550a7375SFelipe Balbi 	/* target addr and (for multipoint) hub addr/port */
552550a7375SFelipe Balbi 	if (musb->is_multipoint) {
5536cc2af6dSHans de Goede 		musb_write_rxfunaddr(musb, epnum, qh->addr_reg);
5546cc2af6dSHans de Goede 		musb_write_rxhubaddr(musb, epnum, qh->h_addr_reg);
5556cc2af6dSHans de Goede 		musb_write_rxhubport(musb, epnum, qh->h_port_reg);
556550a7375SFelipe Balbi 	} else
557550a7375SFelipe Balbi 		musb_writeb(musb->mregs, MUSB_FADDR, qh->addr_reg);
558550a7375SFelipe Balbi 
559550a7375SFelipe Balbi 	/* protocol/endpoint, interval/NAKlimit, i/o size */
560550a7375SFelipe Balbi 	musb_writeb(ep->regs, MUSB_RXTYPE, qh->type_reg);
561550a7375SFelipe Balbi 	musb_writeb(ep->regs, MUSB_RXINTERVAL, qh->intv_reg);
562550a7375SFelipe Balbi 	/* NOTE: bulk combining rewrites high bits of maxpacket */
5639f445cb2SCliff Cai 	/* Set RXMAXP with the FIFO size of the endpoint
5649f445cb2SCliff Cai 	 * to disable double buffer mode.
5659f445cb2SCliff Cai 	 */
566a483d706SAjay Kumar Gupta 	musb_writew(ep->regs, MUSB_RXMAXP,
567a483d706SAjay Kumar Gupta 			qh->maxpacket | ((qh->hb_mult - 1) << 11));
568550a7375SFelipe Balbi 
569550a7375SFelipe Balbi 	ep->rx_reinit = 0;
570550a7375SFelipe Balbi }
571550a7375SFelipe Balbi 
musb_tx_dma_set_mode_mentor(struct musb_hw_ep * hw_ep,struct musb_qh * qh,u32 * length,u8 * mode)5726cfe9036SSaurav Girepunje static void musb_tx_dma_set_mode_mentor(struct musb_hw_ep *hw_ep,
5736cfe9036SSaurav Girepunje 					struct musb_qh *qh,
574754fe4a9STony Lindgren 					u32 *length, u8 *mode)
5756b6e9710SSergei Shtylyov {
5766b6e9710SSergei Shtylyov 	struct dma_channel	*channel = hw_ep->tx_channel;
5776b6e9710SSergei Shtylyov 	void __iomem		*epio = hw_ep->regs;
5786b6e9710SSergei Shtylyov 	u16			pkt_size = qh->maxpacket;
5796b6e9710SSergei Shtylyov 	u16			csr;
5806b6e9710SSergei Shtylyov 
581754fe4a9STony Lindgren 	if (*length > channel->max_len)
582754fe4a9STony Lindgren 		*length = channel->max_len;
5836b6e9710SSergei Shtylyov 
5846b6e9710SSergei Shtylyov 	csr = musb_readw(epio, MUSB_TXCSR);
585754fe4a9STony Lindgren 	if (*length > pkt_size) {
586754fe4a9STony Lindgren 		*mode = 1;
587a483d706SAjay Kumar Gupta 		csr |= MUSB_TXCSR_DMAMODE | MUSB_TXCSR_DMAENAB;
588a483d706SAjay Kumar Gupta 		/* autoset shouldn't be set in high bandwidth */
589f2786281Ssupriya karanth 		/*
590f2786281Ssupriya karanth 		 * Enable Autoset according to table
591f2786281Ssupriya karanth 		 * below
592f2786281Ssupriya karanth 		 * bulk_split hb_mult	Autoset_Enable
593f2786281Ssupriya karanth 		 *	0	1	Yes(Normal)
594f2786281Ssupriya karanth 		 *	0	>1	No(High BW ISO)
595f2786281Ssupriya karanth 		 *	1	1	Yes(HS bulk)
596f2786281Ssupriya karanth 		 *	1	>1	Yes(FS bulk)
597f2786281Ssupriya karanth 		 */
598f2786281Ssupriya karanth 		if (qh->hb_mult == 1 || (qh->hb_mult > 1 &&
599f2786281Ssupriya karanth 					can_bulk_split(hw_ep->musb, qh->type)))
600a483d706SAjay Kumar Gupta 			csr |= MUSB_TXCSR_AUTOSET;
6016b6e9710SSergei Shtylyov 	} else {
602754fe4a9STony Lindgren 		*mode = 0;
6036b6e9710SSergei Shtylyov 		csr &= ~(MUSB_TXCSR_AUTOSET | MUSB_TXCSR_DMAMODE);
6046b6e9710SSergei Shtylyov 		csr |= MUSB_TXCSR_DMAENAB; /* against programmer's guide */
6056b6e9710SSergei Shtylyov 	}
606bba40e69SCristian Birsan 	channel->desired_mode = *mode;
6076b6e9710SSergei Shtylyov 	musb_writew(epio, MUSB_TXCSR, csr);
608754fe4a9STony Lindgren }
609754fe4a9STony Lindgren 
musb_tx_dma_set_mode_cppi_tusb(struct musb_hw_ep * hw_ep,struct urb * urb,u8 * mode)6103c5e0d0eSSaurav Girepunje static void musb_tx_dma_set_mode_cppi_tusb(struct musb_hw_ep *hw_ep,
611754fe4a9STony Lindgren 					   struct urb *urb,
612754fe4a9STony Lindgren 					   u8 *mode)
613754fe4a9STony Lindgren {
614754fe4a9STony Lindgren 	struct dma_channel *channel = hw_ep->tx_channel;
615754fe4a9STony Lindgren 
6166b6e9710SSergei Shtylyov 	channel->actual_len = 0;
6176b6e9710SSergei Shtylyov 
6186b6e9710SSergei Shtylyov 	/*
6196b6e9710SSergei Shtylyov 	 * TX uses "RNDIS" mode automatically but needs help
6206b6e9710SSergei Shtylyov 	 * to identify the zero-length-final-packet case.
6216b6e9710SSergei Shtylyov 	 */
622754fe4a9STony Lindgren 	*mode = (urb->transfer_flags & URB_ZERO_PACKET) ? 1 : 0;
623754fe4a9STony Lindgren }
624754fe4a9STony Lindgren 
musb_tx_dma_program(struct dma_controller * dma,struct musb_hw_ep * hw_ep,struct musb_qh * qh,struct urb * urb,u32 offset,u32 length)625754fe4a9STony Lindgren static bool musb_tx_dma_program(struct dma_controller *dma,
626754fe4a9STony Lindgren 		struct musb_hw_ep *hw_ep, struct musb_qh *qh,
627754fe4a9STony Lindgren 		struct urb *urb, u32 offset, u32 length)
628754fe4a9STony Lindgren {
629754fe4a9STony Lindgren 	struct dma_channel	*channel = hw_ep->tx_channel;
630754fe4a9STony Lindgren 	u16			pkt_size = qh->maxpacket;
631754fe4a9STony Lindgren 	u8			mode;
632754fe4a9STony Lindgren 
633754fe4a9STony Lindgren 	if (musb_dma_inventra(hw_ep->musb) || musb_dma_ux500(hw_ep->musb))
6346cfe9036SSaurav Girepunje 		musb_tx_dma_set_mode_mentor(hw_ep, qh,
635b6a6631dSSergei Shtylyov 					    &length, &mode);
636858b9be7SSergei Shtylyov 	else if (is_cppi_enabled(hw_ep->musb) || tusb_dma_omap(hw_ep->musb))
6373c5e0d0eSSaurav Girepunje 		musb_tx_dma_set_mode_cppi_tusb(hw_ep, urb, &mode);
638858b9be7SSergei Shtylyov 	else
639858b9be7SSergei Shtylyov 		return false;
6406b6e9710SSergei Shtylyov 
6416b6e9710SSergei Shtylyov 	qh->segsize = length;
6426b6e9710SSergei Shtylyov 
6434c647338SSantosh Shilimkar 	/*
6444c647338SSantosh Shilimkar 	 * Ensure the data reaches to main memory before starting
6454c647338SSantosh Shilimkar 	 * DMA transfer
6464c647338SSantosh Shilimkar 	 */
6474c647338SSantosh Shilimkar 	wmb();
6484c647338SSantosh Shilimkar 
6496b6e9710SSergei Shtylyov 	if (!dma->channel_program(channel, pkt_size, mode,
6506b6e9710SSergei Shtylyov 			urb->transfer_dma + offset, length)) {
651754fe4a9STony Lindgren 		void __iomem *epio = hw_ep->regs;
652754fe4a9STony Lindgren 		u16 csr;
653754fe4a9STony Lindgren 
6546b6e9710SSergei Shtylyov 		dma->channel_release(channel);
6556b6e9710SSergei Shtylyov 		hw_ep->tx_channel = NULL;
6566b6e9710SSergei Shtylyov 
6576b6e9710SSergei Shtylyov 		csr = musb_readw(epio, MUSB_TXCSR);
6586b6e9710SSergei Shtylyov 		csr &= ~(MUSB_TXCSR_AUTOSET | MUSB_TXCSR_DMAENAB);
6596b6e9710SSergei Shtylyov 		musb_writew(epio, MUSB_TXCSR, csr | MUSB_TXCSR_H_WZC_BITS);
6606b6e9710SSergei Shtylyov 		return false;
6616b6e9710SSergei Shtylyov 	}
6626b6e9710SSergei Shtylyov 	return true;
6636b6e9710SSergei Shtylyov }
664550a7375SFelipe Balbi 
665550a7375SFelipe Balbi /*
666550a7375SFelipe Balbi  * Program an HDRC endpoint as per the given URB
667550a7375SFelipe Balbi  * Context: irqs blocked, controller lock held
668550a7375SFelipe Balbi  */
musb_ep_program(struct musb * musb,u8 epnum,struct urb * urb,int is_out,u8 * buf,u32 offset,u32 len)669550a7375SFelipe Balbi static void musb_ep_program(struct musb *musb, u8 epnum,
6706b6e9710SSergei Shtylyov 			struct urb *urb, int is_out,
6716b6e9710SSergei Shtylyov 			u8 *buf, u32 offset, u32 len)
672550a7375SFelipe Balbi {
673550a7375SFelipe Balbi 	struct dma_controller	*dma_controller;
674550a7375SFelipe Balbi 	struct dma_channel	*dma_channel;
675550a7375SFelipe Balbi 	u8			dma_ok;
676550a7375SFelipe Balbi 	void __iomem		*mbase = musb->mregs;
677550a7375SFelipe Balbi 	struct musb_hw_ep	*hw_ep = musb->endpoints + epnum;
678550a7375SFelipe Balbi 	void __iomem		*epio = hw_ep->regs;
6793e5c6dc7SSergei Shtylyov 	struct musb_qh		*qh = musb_ep_get_qh(hw_ep, !is_out);
6803e5c6dc7SSergei Shtylyov 	u16			packet_sz = qh->maxpacket;
6813132122cSAjay Kumar Gupta 	u8			use_dma = 1;
6823132122cSAjay Kumar Gupta 	u16			csr;
683550a7375SFelipe Balbi 
684b99d3659SBin Liu 	musb_dbg(musb, "%s hw%d urb %p spd%d dev%d ep%d%s "
685b99d3659SBin Liu 				"h_addr%02x h_port%02x bytes %d",
686550a7375SFelipe Balbi 			is_out ? "-->" : "<--",
687550a7375SFelipe Balbi 			epnum, urb, urb->dev->speed,
688550a7375SFelipe Balbi 			qh->addr_reg, qh->epnum, is_out ? "out" : "in",
689550a7375SFelipe Balbi 			qh->h_addr_reg, qh->h_port_reg,
690550a7375SFelipe Balbi 			len);
691550a7375SFelipe Balbi 
692550a7375SFelipe Balbi 	musb_ep_select(mbase, epnum);
693550a7375SFelipe Balbi 
6943132122cSAjay Kumar Gupta 	if (is_out && !len) {
6953132122cSAjay Kumar Gupta 		use_dma = 0;
6963132122cSAjay Kumar Gupta 		csr = musb_readw(epio, MUSB_TXCSR);
6973132122cSAjay Kumar Gupta 		csr &= ~MUSB_TXCSR_DMAENAB;
6983132122cSAjay Kumar Gupta 		musb_writew(epio, MUSB_TXCSR, csr);
6993132122cSAjay Kumar Gupta 		hw_ep->tx_channel = NULL;
7003132122cSAjay Kumar Gupta 	}
7013132122cSAjay Kumar Gupta 
702550a7375SFelipe Balbi 	/* candidate for DMA? */
703550a7375SFelipe Balbi 	dma_controller = musb->dma_controller;
7043132122cSAjay Kumar Gupta 	if (use_dma && is_dma_capable() && epnum && dma_controller) {
705550a7375SFelipe Balbi 		dma_channel = is_out ? hw_ep->tx_channel : hw_ep->rx_channel;
706550a7375SFelipe Balbi 		if (!dma_channel) {
707550a7375SFelipe Balbi 			dma_channel = dma_controller->channel_alloc(
708550a7375SFelipe Balbi 					dma_controller, hw_ep, is_out);
709550a7375SFelipe Balbi 			if (is_out)
710550a7375SFelipe Balbi 				hw_ep->tx_channel = dma_channel;
711550a7375SFelipe Balbi 			else
712550a7375SFelipe Balbi 				hw_ep->rx_channel = dma_channel;
713550a7375SFelipe Balbi 		}
714550a7375SFelipe Balbi 	} else
715550a7375SFelipe Balbi 		dma_channel = NULL;
716550a7375SFelipe Balbi 
717550a7375SFelipe Balbi 	/* make sure we clear DMAEnab, autoSet bits from previous run */
718550a7375SFelipe Balbi 
719550a7375SFelipe Balbi 	/* OUT/transmit/EP0 or IN/receive? */
720550a7375SFelipe Balbi 	if (is_out) {
721550a7375SFelipe Balbi 		u16	csr;
722550a7375SFelipe Balbi 		u16	int_txe;
723550a7375SFelipe Balbi 		u16	load_count;
724550a7375SFelipe Balbi 
725550a7375SFelipe Balbi 		csr = musb_readw(epio, MUSB_TXCSR);
726550a7375SFelipe Balbi 
727550a7375SFelipe Balbi 		/* disable interrupt in case we flush */
728b18d26f6SSebastian Andrzej Siewior 		int_txe = musb->intrtxe;
729550a7375SFelipe Balbi 		musb_writew(mbase, MUSB_INTRTXE, int_txe & ~(1 << epnum));
730550a7375SFelipe Balbi 
731550a7375SFelipe Balbi 		/* general endpoint setup */
732550a7375SFelipe Balbi 		if (epnum) {
733550a7375SFelipe Balbi 			/* flush all old state, set default */
734a70b8442Ssupriya karanth 			/*
735a70b8442Ssupriya karanth 			 * We could be flushing valid
736a70b8442Ssupriya karanth 			 * packets in double buffering
737a70b8442Ssupriya karanth 			 * case
738a70b8442Ssupriya karanth 			 */
739a70b8442Ssupriya karanth 			if (!hw_ep->tx_double_buffered)
740550a7375SFelipe Balbi 				musb_h_tx_flush_fifo(hw_ep);
741b6e434a5SSergei Shtylyov 
742b6e434a5SSergei Shtylyov 			/*
743b6e434a5SSergei Shtylyov 			 * We must not clear the DMAMODE bit before or in
744b6e434a5SSergei Shtylyov 			 * the same cycle with the DMAENAB bit, so we clear
745b6e434a5SSergei Shtylyov 			 * the latter first...
746b6e434a5SSergei Shtylyov 			 */
747550a7375SFelipe Balbi 			csr &= ~(MUSB_TXCSR_H_NAKTIMEOUT
748b6e434a5SSergei Shtylyov 					| MUSB_TXCSR_AUTOSET
749b6e434a5SSergei Shtylyov 					| MUSB_TXCSR_DMAENAB
750550a7375SFelipe Balbi 					| MUSB_TXCSR_FRCDATATOG
751550a7375SFelipe Balbi 					| MUSB_TXCSR_H_RXSTALL
752550a7375SFelipe Balbi 					| MUSB_TXCSR_H_ERROR
753550a7375SFelipe Balbi 					| MUSB_TXCSR_TXPKTRDY
754550a7375SFelipe Balbi 					);
755550a7375SFelipe Balbi 			csr |= MUSB_TXCSR_MODE;
756550a7375SFelipe Balbi 
757fe3bbd6bSMin Guo 			if (!hw_ep->tx_double_buffered)
758fe3bbd6bSMin Guo 				csr |= musb->io.set_toggle(qh, is_out, urb);
759550a7375SFelipe Balbi 
760550a7375SFelipe Balbi 			musb_writew(epio, MUSB_TXCSR, csr);
761550a7375SFelipe Balbi 			/* REVISIT may need to clear FLUSHFIFO ... */
762b6e434a5SSergei Shtylyov 			csr &= ~MUSB_TXCSR_DMAMODE;
763550a7375SFelipe Balbi 			musb_writew(epio, MUSB_TXCSR, csr);
764550a7375SFelipe Balbi 			csr = musb_readw(epio, MUSB_TXCSR);
765550a7375SFelipe Balbi 		} else {
766550a7375SFelipe Balbi 			/* endpoint 0: just flush */
76778322c1aSDavid Brownell 			musb_h_ep0_flush_fifo(hw_ep);
768550a7375SFelipe Balbi 		}
769550a7375SFelipe Balbi 
770550a7375SFelipe Balbi 		/* target addr and (for multipoint) hub addr/port */
771550a7375SFelipe Balbi 		if (musb->is_multipoint) {
7726cc2af6dSHans de Goede 			musb_write_txfunaddr(musb, epnum, qh->addr_reg);
7736cc2af6dSHans de Goede 			musb_write_txhubaddr(musb, epnum, qh->h_addr_reg);
7746cc2af6dSHans de Goede 			musb_write_txhubport(musb, epnum, qh->h_port_reg);
775550a7375SFelipe Balbi /* FIXME if !epnum, do the same for RX ... */
776550a7375SFelipe Balbi 		} else
777550a7375SFelipe Balbi 			musb_writeb(mbase, MUSB_FADDR, qh->addr_reg);
778550a7375SFelipe Balbi 
779550a7375SFelipe Balbi 		/* protocol/endpoint/interval/NAKlimit */
780550a7375SFelipe Balbi 		if (epnum) {
781550a7375SFelipe Balbi 			musb_writeb(epio, MUSB_TXTYPE, qh->type_reg);
782a9762b70SArnd Bergmann 			if (can_bulk_split(musb, qh->type)) {
783f2786281Ssupriya karanth 				qh->hb_mult = hw_ep->max_packet_sz_tx
784f2786281Ssupriya karanth 						/ packet_sz;
785ccc080c7SAjay Kumar Gupta 				musb_writew(epio, MUSB_TXMAXP, packet_sz
786f2786281Ssupriya karanth 					| ((qh->hb_mult) - 1) << 11);
787f2786281Ssupriya karanth 			} else {
788550a7375SFelipe Balbi 				musb_writew(epio, MUSB_TXMAXP,
78906624818SFelipe Balbi 						qh->maxpacket |
79006624818SFelipe Balbi 						((qh->hb_mult - 1) << 11));
791f2786281Ssupriya karanth 			}
792550a7375SFelipe Balbi 			musb_writeb(epio, MUSB_TXINTERVAL, qh->intv_reg);
793550a7375SFelipe Balbi 		} else {
794550a7375SFelipe Balbi 			musb_writeb(epio, MUSB_NAKLIMIT0, qh->intv_reg);
795550a7375SFelipe Balbi 			if (musb->is_multipoint)
796550a7375SFelipe Balbi 				musb_writeb(epio, MUSB_TYPE0,
797550a7375SFelipe Balbi 						qh->type_reg);
798550a7375SFelipe Balbi 		}
799550a7375SFelipe Balbi 
800550a7375SFelipe Balbi 		if (can_bulk_split(musb, qh->type))
801550a7375SFelipe Balbi 			load_count = min((u32) hw_ep->max_packet_sz_tx,
802550a7375SFelipe Balbi 						len);
803550a7375SFelipe Balbi 		else
804550a7375SFelipe Balbi 			load_count = min((u32) packet_sz, len);
805550a7375SFelipe Balbi 
8066b6e9710SSergei Shtylyov 		if (dma_channel && musb_tx_dma_program(dma_controller,
8076b6e9710SSergei Shtylyov 					hw_ep, qh, urb, offset, len))
808550a7375SFelipe Balbi 			load_count = 0;
809550a7375SFelipe Balbi 
810550a7375SFelipe Balbi 		if (load_count) {
811550a7375SFelipe Balbi 			/* PIO to load FIFO */
812550a7375SFelipe Balbi 			qh->segsize = load_count;
8138e8a5516SVirupax Sadashivpetimath 			if (!buf) {
8148e8a5516SVirupax Sadashivpetimath 				sg_miter_start(&qh->sg_miter, urb->sg, 1,
8158e8a5516SVirupax Sadashivpetimath 						SG_MITER_ATOMIC
8168e8a5516SVirupax Sadashivpetimath 						| SG_MITER_FROM_SG);
8178e8a5516SVirupax Sadashivpetimath 				if (!sg_miter_next(&qh->sg_miter)) {
8188e8a5516SVirupax Sadashivpetimath 					dev_err(musb->controller,
8198e8a5516SVirupax Sadashivpetimath 							"error: sg"
8208e8a5516SVirupax Sadashivpetimath 							"list empty\n");
8218e8a5516SVirupax Sadashivpetimath 					sg_miter_stop(&qh->sg_miter);
8228e8a5516SVirupax Sadashivpetimath 					goto finish;
8238e8a5516SVirupax Sadashivpetimath 				}
8248e8a5516SVirupax Sadashivpetimath 				buf = qh->sg_miter.addr + urb->sg->offset +
8258e8a5516SVirupax Sadashivpetimath 					urb->actual_length;
8268e8a5516SVirupax Sadashivpetimath 				load_count = min_t(u32, load_count,
8278e8a5516SVirupax Sadashivpetimath 						qh->sg_miter.length);
8288e8a5516SVirupax Sadashivpetimath 				musb_write_fifo(hw_ep, load_count, buf);
8298e8a5516SVirupax Sadashivpetimath 				qh->sg_miter.consumed = load_count;
8308e8a5516SVirupax Sadashivpetimath 				sg_miter_stop(&qh->sg_miter);
8318e8a5516SVirupax Sadashivpetimath 			} else
832550a7375SFelipe Balbi 				musb_write_fifo(hw_ep, load_count, buf);
833550a7375SFelipe Balbi 		}
8348e8a5516SVirupax Sadashivpetimath finish:
835550a7375SFelipe Balbi 		/* re-enable interrupt */
836550a7375SFelipe Balbi 		musb_writew(mbase, MUSB_INTRTXE, int_txe);
837550a7375SFelipe Balbi 
838550a7375SFelipe Balbi 	/* IN/receive */
839550a7375SFelipe Balbi 	} else {
840fe3bbd6bSMin Guo 		u16 csr = 0;
841550a7375SFelipe Balbi 
842550a7375SFelipe Balbi 		if (hw_ep->rx_reinit) {
8430cb74b3dSHans de Goede 			musb_rx_reinit(musb, qh, epnum);
844fe3bbd6bSMin Guo 			csr |= musb->io.set_toggle(qh, is_out, urb);
845550a7375SFelipe Balbi 
846550a7375SFelipe Balbi 			if (qh->type == USB_ENDPOINT_XFER_INT)
847550a7375SFelipe Balbi 				csr |= MUSB_RXCSR_DISNYET;
848550a7375SFelipe Balbi 
849550a7375SFelipe Balbi 		} else {
850550a7375SFelipe Balbi 			csr = musb_readw(hw_ep->regs, MUSB_RXCSR);
851550a7375SFelipe Balbi 
852550a7375SFelipe Balbi 			if (csr & (MUSB_RXCSR_RXPKTRDY
853550a7375SFelipe Balbi 					| MUSB_RXCSR_DMAENAB
854550a7375SFelipe Balbi 					| MUSB_RXCSR_H_REQPKT))
855550a7375SFelipe Balbi 				ERR("broken !rx_reinit, ep%d csr %04x\n",
856550a7375SFelipe Balbi 						hw_ep->epnum, csr);
857550a7375SFelipe Balbi 
858550a7375SFelipe Balbi 			/* scrub any stale state, leaving toggle alone */
859550a7375SFelipe Balbi 			csr &= MUSB_RXCSR_DISNYET;
860550a7375SFelipe Balbi 		}
861550a7375SFelipe Balbi 
862550a7375SFelipe Balbi 		/* kick things off */
863550a7375SFelipe Balbi 
864f8e9f34fSTony Lindgren 		if ((is_cppi_enabled(musb) || tusb_dma_omap(musb)) && dma_channel) {
865c51e36dcSSergei Shtylyov 			/* Candidate for DMA */
866550a7375SFelipe Balbi 			dma_channel->actual_len = 0L;
867550a7375SFelipe Balbi 			qh->segsize = len;
868550a7375SFelipe Balbi 
869550a7375SFelipe Balbi 			/* AUTOREQ is in a DMA register */
870550a7375SFelipe Balbi 			musb_writew(hw_ep->regs, MUSB_RXCSR, csr);
871c51e36dcSSergei Shtylyov 			csr = musb_readw(hw_ep->regs, MUSB_RXCSR);
872550a7375SFelipe Balbi 
873c51e36dcSSergei Shtylyov 			/*
874c51e36dcSSergei Shtylyov 			 * Unless caller treats short RX transfers as
875550a7375SFelipe Balbi 			 * errors, we dare not queue multiple transfers.
876550a7375SFelipe Balbi 			 */
877c51e36dcSSergei Shtylyov 			dma_ok = dma_controller->channel_program(dma_channel,
878c51e36dcSSergei Shtylyov 					packet_sz, !(urb->transfer_flags &
879c51e36dcSSergei Shtylyov 						     URB_SHORT_NOT_OK),
8806b6e9710SSergei Shtylyov 					urb->transfer_dma + offset,
881550a7375SFelipe Balbi 					qh->segsize);
882550a7375SFelipe Balbi 			if (!dma_ok) {
883c51e36dcSSergei Shtylyov 				dma_controller->channel_release(dma_channel);
884c51e36dcSSergei Shtylyov 				hw_ep->rx_channel = dma_channel = NULL;
885550a7375SFelipe Balbi 			} else
886550a7375SFelipe Balbi 				csr |= MUSB_RXCSR_DMAENAB;
887550a7375SFelipe Balbi 		}
888550a7375SFelipe Balbi 
889550a7375SFelipe Balbi 		csr |= MUSB_RXCSR_H_REQPKT;
890b99d3659SBin Liu 		musb_dbg(musb, "RXCSR%d := %04x", epnum, csr);
891550a7375SFelipe Balbi 		musb_writew(hw_ep->regs, MUSB_RXCSR, csr);
892550a7375SFelipe Balbi 		csr = musb_readw(hw_ep->regs, MUSB_RXCSR);
893550a7375SFelipe Balbi 	}
894550a7375SFelipe Balbi }
895550a7375SFelipe Balbi 
896f283862fSAjay Kumar Gupta /* Schedule next QH from musb->in_bulk/out_bulk and move the current qh to
897f283862fSAjay Kumar Gupta  * the end; avoids starvation for other endpoints.
898f283862fSAjay Kumar Gupta  */
musb_bulk_nak_timeout(struct musb * musb,struct musb_hw_ep * ep,int is_in)899f283862fSAjay Kumar Gupta static void musb_bulk_nak_timeout(struct musb *musb, struct musb_hw_ep *ep,
900f283862fSAjay Kumar Gupta 	int is_in)
901f283862fSAjay Kumar Gupta {
902f283862fSAjay Kumar Gupta 	struct dma_channel	*dma;
903f283862fSAjay Kumar Gupta 	struct urb		*urb;
904f283862fSAjay Kumar Gupta 	void __iomem		*mbase = musb->mregs;
905f283862fSAjay Kumar Gupta 	void __iomem		*epio = ep->regs;
906f283862fSAjay Kumar Gupta 	struct musb_qh		*cur_qh, *next_qh;
907f283862fSAjay Kumar Gupta 	u16			rx_csr, tx_csr;
908fe3bbd6bSMin Guo 	u16			toggle;
909f283862fSAjay Kumar Gupta 
910f283862fSAjay Kumar Gupta 	musb_ep_select(mbase, ep->epnum);
911f283862fSAjay Kumar Gupta 	if (is_in) {
912f283862fSAjay Kumar Gupta 		dma = is_dma_capable() ? ep->rx_channel : NULL;
913f283862fSAjay Kumar Gupta 
9147b2c17f8SAndrew Goodbody 		/*
9157b2c17f8SAndrew Goodbody 		 * Need to stop the transaction by clearing REQPKT first
9167b2c17f8SAndrew Goodbody 		 * then the NAK Timeout bit ref MUSBMHDRC USB 2.0 HIGH-SPEED
9177b2c17f8SAndrew Goodbody 		 * DUAL-ROLE CONTROLLER Programmer's Guide, section 9.2.2
9187b2c17f8SAndrew Goodbody 		 */
919f283862fSAjay Kumar Gupta 		rx_csr = musb_readw(epio, MUSB_RXCSR);
920f283862fSAjay Kumar Gupta 		rx_csr |= MUSB_RXCSR_H_WZC_BITS;
9217b2c17f8SAndrew Goodbody 		rx_csr &= ~MUSB_RXCSR_H_REQPKT;
9227b2c17f8SAndrew Goodbody 		musb_writew(epio, MUSB_RXCSR, rx_csr);
923f283862fSAjay Kumar Gupta 		rx_csr &= ~MUSB_RXCSR_DATAERROR;
924f283862fSAjay Kumar Gupta 		musb_writew(epio, MUSB_RXCSR, rx_csr);
925f283862fSAjay Kumar Gupta 
926f283862fSAjay Kumar Gupta 		cur_qh = first_qh(&musb->in_bulk);
927f283862fSAjay Kumar Gupta 	} else {
928f283862fSAjay Kumar Gupta 		dma = is_dma_capable() ? ep->tx_channel : NULL;
929f283862fSAjay Kumar Gupta 
930f283862fSAjay Kumar Gupta 		/* clear nak timeout bit */
931f283862fSAjay Kumar Gupta 		tx_csr = musb_readw(epio, MUSB_TXCSR);
932f283862fSAjay Kumar Gupta 		tx_csr |= MUSB_TXCSR_H_WZC_BITS;
933f283862fSAjay Kumar Gupta 		tx_csr &= ~MUSB_TXCSR_H_NAKTIMEOUT;
934f283862fSAjay Kumar Gupta 		musb_writew(epio, MUSB_TXCSR, tx_csr);
935f283862fSAjay Kumar Gupta 
936f283862fSAjay Kumar Gupta 		cur_qh = first_qh(&musb->out_bulk);
937f283862fSAjay Kumar Gupta 	}
938f283862fSAjay Kumar Gupta 	if (cur_qh) {
939f283862fSAjay Kumar Gupta 		urb = next_urb(cur_qh);
940f283862fSAjay Kumar Gupta 		if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) {
941f283862fSAjay Kumar Gupta 			dma->status = MUSB_DMA_STATUS_CORE_ABORT;
942f283862fSAjay Kumar Gupta 			musb->dma_controller->channel_abort(dma);
943f283862fSAjay Kumar Gupta 			urb->actual_length += dma->actual_len;
944f283862fSAjay Kumar Gupta 			dma->actual_len = 0L;
945f283862fSAjay Kumar Gupta 		}
946fe3bbd6bSMin Guo 		toggle = musb->io.get_toggle(cur_qh, !is_in);
947fe3bbd6bSMin Guo 		usb_settoggle(urb->dev, cur_qh->epnum, !is_in, toggle ? 1 : 0);
948f283862fSAjay Kumar Gupta 
949f283862fSAjay Kumar Gupta 		if (is_in) {
950f283862fSAjay Kumar Gupta 			/* move cur_qh to end of queue */
951f283862fSAjay Kumar Gupta 			list_move_tail(&cur_qh->ring, &musb->in_bulk);
952f283862fSAjay Kumar Gupta 
953f283862fSAjay Kumar Gupta 			/* get the next qh from musb->in_bulk */
954f283862fSAjay Kumar Gupta 			next_qh = first_qh(&musb->in_bulk);
955f283862fSAjay Kumar Gupta 
956f283862fSAjay Kumar Gupta 			/* set rx_reinit and schedule the next qh */
957f283862fSAjay Kumar Gupta 			ep->rx_reinit = 1;
958f283862fSAjay Kumar Gupta 		} else {
959f283862fSAjay Kumar Gupta 			/* move cur_qh to end of queue */
960f283862fSAjay Kumar Gupta 			list_move_tail(&cur_qh->ring, &musb->out_bulk);
961f283862fSAjay Kumar Gupta 
962f283862fSAjay Kumar Gupta 			/* get the next qh from musb->out_bulk */
963f283862fSAjay Kumar Gupta 			next_qh = first_qh(&musb->out_bulk);
964f283862fSAjay Kumar Gupta 
965f283862fSAjay Kumar Gupta 			/* set tx_reinit and schedule the next qh */
966f283862fSAjay Kumar Gupta 			ep->tx_reinit = 1;
967f283862fSAjay Kumar Gupta 		}
9682b63f132SBin Liu 
9692b63f132SBin Liu 		if (next_qh)
970f283862fSAjay Kumar Gupta 			musb_start_urb(musb, is_in, next_qh);
971f283862fSAjay Kumar Gupta 	}
972f283862fSAjay Kumar Gupta }
973550a7375SFelipe Balbi 
974550a7375SFelipe Balbi /*
975550a7375SFelipe Balbi  * Service the default endpoint (ep0) as host.
976550a7375SFelipe Balbi  * Return true until it's time to start the status stage.
977550a7375SFelipe Balbi  */
musb_h_ep0_continue(struct musb * musb,u16 len,struct urb * urb)978550a7375SFelipe Balbi static bool musb_h_ep0_continue(struct musb *musb, u16 len, struct urb *urb)
979550a7375SFelipe Balbi {
980550a7375SFelipe Balbi 	bool			 more = false;
981550a7375SFelipe Balbi 	u8			*fifo_dest = NULL;
982550a7375SFelipe Balbi 	u16			fifo_count = 0;
983550a7375SFelipe Balbi 	struct musb_hw_ep	*hw_ep = musb->control_ep;
984550a7375SFelipe Balbi 	struct musb_qh		*qh = hw_ep->in_qh;
985550a7375SFelipe Balbi 	struct usb_ctrlrequest	*request;
986550a7375SFelipe Balbi 
987550a7375SFelipe Balbi 	switch (musb->ep0_stage) {
988550a7375SFelipe Balbi 	case MUSB_EP0_IN:
989550a7375SFelipe Balbi 		fifo_dest = urb->transfer_buffer + urb->actual_length;
9903ecdb9acSSergei Shtylyov 		fifo_count = min_t(size_t, len, urb->transfer_buffer_length -
9913ecdb9acSSergei Shtylyov 				   urb->actual_length);
992550a7375SFelipe Balbi 		if (fifo_count < len)
993550a7375SFelipe Balbi 			urb->status = -EOVERFLOW;
994550a7375SFelipe Balbi 
995550a7375SFelipe Balbi 		musb_read_fifo(hw_ep, fifo_count, fifo_dest);
996550a7375SFelipe Balbi 
997550a7375SFelipe Balbi 		urb->actual_length += fifo_count;
998550a7375SFelipe Balbi 		if (len < qh->maxpacket) {
999550a7375SFelipe Balbi 			/* always terminate on short read; it's
1000550a7375SFelipe Balbi 			 * rarely reported as an error.
1001550a7375SFelipe Balbi 			 */
1002550a7375SFelipe Balbi 		} else if (urb->actual_length <
1003550a7375SFelipe Balbi 				urb->transfer_buffer_length)
1004550a7375SFelipe Balbi 			more = true;
1005550a7375SFelipe Balbi 		break;
1006550a7375SFelipe Balbi 	case MUSB_EP0_START:
1007550a7375SFelipe Balbi 		request = (struct usb_ctrlrequest *) urb->setup_packet;
1008550a7375SFelipe Balbi 
1009550a7375SFelipe Balbi 		if (!request->wLength) {
1010b99d3659SBin Liu 			musb_dbg(musb, "start no-DATA");
1011550a7375SFelipe Balbi 			break;
1012550a7375SFelipe Balbi 		} else if (request->bRequestType & USB_DIR_IN) {
1013b99d3659SBin Liu 			musb_dbg(musb, "start IN-DATA");
1014550a7375SFelipe Balbi 			musb->ep0_stage = MUSB_EP0_IN;
1015550a7375SFelipe Balbi 			more = true;
1016550a7375SFelipe Balbi 			break;
1017550a7375SFelipe Balbi 		} else {
1018b99d3659SBin Liu 			musb_dbg(musb, "start OUT-DATA");
1019550a7375SFelipe Balbi 			musb->ep0_stage = MUSB_EP0_OUT;
1020550a7375SFelipe Balbi 			more = true;
1021550a7375SFelipe Balbi 		}
1022df561f66SGustavo A. R. Silva 		fallthrough;
1023550a7375SFelipe Balbi 	case MUSB_EP0_OUT:
10243ecdb9acSSergei Shtylyov 		fifo_count = min_t(size_t, qh->maxpacket,
10253ecdb9acSSergei Shtylyov 				   urb->transfer_buffer_length -
10263ecdb9acSSergei Shtylyov 				   urb->actual_length);
1027550a7375SFelipe Balbi 		if (fifo_count) {
1028550a7375SFelipe Balbi 			fifo_dest = (u8 *) (urb->transfer_buffer
1029550a7375SFelipe Balbi 					+ urb->actual_length);
1030b99d3659SBin Liu 			musb_dbg(musb, "Sending %d byte%s to ep0 fifo %p",
1031bb1c9ef1SDavid Brownell 					fifo_count,
1032bb1c9ef1SDavid Brownell 					(fifo_count == 1) ? "" : "s",
1033bb1c9ef1SDavid Brownell 					fifo_dest);
1034550a7375SFelipe Balbi 			musb_write_fifo(hw_ep, fifo_count, fifo_dest);
1035550a7375SFelipe Balbi 
1036550a7375SFelipe Balbi 			urb->actual_length += fifo_count;
1037550a7375SFelipe Balbi 			more = true;
1038550a7375SFelipe Balbi 		}
1039550a7375SFelipe Balbi 		break;
1040550a7375SFelipe Balbi 	default:
1041550a7375SFelipe Balbi 		ERR("bogus ep0 stage %d\n", musb->ep0_stage);
1042550a7375SFelipe Balbi 		break;
1043550a7375SFelipe Balbi 	}
1044550a7375SFelipe Balbi 
1045550a7375SFelipe Balbi 	return more;
1046550a7375SFelipe Balbi }
1047550a7375SFelipe Balbi 
1048550a7375SFelipe Balbi /*
1049550a7375SFelipe Balbi  * Handle default endpoint interrupt as host. Only called in IRQ time
1050c767c1c6SDavid Brownell  * from musb_interrupt().
1051550a7375SFelipe Balbi  *
1052550a7375SFelipe Balbi  * called with controller irqlocked
1053550a7375SFelipe Balbi  */
musb_h_ep0_irq(struct musb * musb)1054550a7375SFelipe Balbi irqreturn_t musb_h_ep0_irq(struct musb *musb)
1055550a7375SFelipe Balbi {
1056550a7375SFelipe Balbi 	struct urb		*urb;
1057550a7375SFelipe Balbi 	u16			csr, len;
1058550a7375SFelipe Balbi 	int			status = 0;
1059550a7375SFelipe Balbi 	void __iomem		*mbase = musb->mregs;
1060550a7375SFelipe Balbi 	struct musb_hw_ep	*hw_ep = musb->control_ep;
1061550a7375SFelipe Balbi 	void __iomem		*epio = hw_ep->regs;
1062550a7375SFelipe Balbi 	struct musb_qh		*qh = hw_ep->in_qh;
1063550a7375SFelipe Balbi 	bool			complete = false;
1064550a7375SFelipe Balbi 	irqreturn_t		retval = IRQ_NONE;
1065550a7375SFelipe Balbi 
1066550a7375SFelipe Balbi 	/* ep0 only has one queue, "in" */
1067550a7375SFelipe Balbi 	urb = next_urb(qh);
1068550a7375SFelipe Balbi 
1069550a7375SFelipe Balbi 	musb_ep_select(mbase, 0);
1070550a7375SFelipe Balbi 	csr = musb_readw(epio, MUSB_CSR0);
1071550a7375SFelipe Balbi 	len = (csr & MUSB_CSR0_RXPKTRDY)
1072550a7375SFelipe Balbi 			? musb_readb(epio, MUSB_COUNT0)
1073550a7375SFelipe Balbi 			: 0;
1074550a7375SFelipe Balbi 
1075b99d3659SBin Liu 	musb_dbg(musb, "<== csr0 %04x, qh %p, count %d, urb %p, stage %d",
1076550a7375SFelipe Balbi 		csr, qh, len, urb, musb->ep0_stage);
1077550a7375SFelipe Balbi 
1078550a7375SFelipe Balbi 	/* if we just did status stage, we are done */
1079550a7375SFelipe Balbi 	if (MUSB_EP0_STATUS == musb->ep0_stage) {
1080550a7375SFelipe Balbi 		retval = IRQ_HANDLED;
1081550a7375SFelipe Balbi 		complete = true;
1082550a7375SFelipe Balbi 	}
1083550a7375SFelipe Balbi 
1084550a7375SFelipe Balbi 	/* prepare status */
1085550a7375SFelipe Balbi 	if (csr & MUSB_CSR0_H_RXSTALL) {
1086b99d3659SBin Liu 		musb_dbg(musb, "STALLING ENDPOINT");
1087550a7375SFelipe Balbi 		status = -EPIPE;
1088550a7375SFelipe Balbi 
1089550a7375SFelipe Balbi 	} else if (csr & MUSB_CSR0_H_ERROR) {
1090b99d3659SBin Liu 		musb_dbg(musb, "no response, csr0 %04x", csr);
1091550a7375SFelipe Balbi 		status = -EPROTO;
1092550a7375SFelipe Balbi 
1093550a7375SFelipe Balbi 	} else if (csr & MUSB_CSR0_H_NAKTIMEOUT) {
1094b99d3659SBin Liu 		musb_dbg(musb, "control NAK timeout");
1095550a7375SFelipe Balbi 
1096550a7375SFelipe Balbi 		/* NOTE:  this code path would be a good place to PAUSE a
1097550a7375SFelipe Balbi 		 * control transfer, if another one is queued, so that
10981e0320f0SAjay Kumar Gupta 		 * ep0 is more likely to stay busy.  That's already done
10991e0320f0SAjay Kumar Gupta 		 * for bulk RX transfers.
1100550a7375SFelipe Balbi 		 *
1101550a7375SFelipe Balbi 		 * if (qh->ring.next != &musb->control), then
1102550a7375SFelipe Balbi 		 * we have a candidate... NAKing is *NOT* an error
1103550a7375SFelipe Balbi 		 */
1104550a7375SFelipe Balbi 		musb_writew(epio, MUSB_CSR0, 0);
1105550a7375SFelipe Balbi 		retval = IRQ_HANDLED;
1106550a7375SFelipe Balbi 	}
1107550a7375SFelipe Balbi 
1108550a7375SFelipe Balbi 	if (status) {
1109b99d3659SBin Liu 		musb_dbg(musb, "aborting");
1110550a7375SFelipe Balbi 		retval = IRQ_HANDLED;
1111550a7375SFelipe Balbi 		if (urb)
1112550a7375SFelipe Balbi 			urb->status = status;
1113550a7375SFelipe Balbi 		complete = true;
1114550a7375SFelipe Balbi 
1115550a7375SFelipe Balbi 		/* use the proper sequence to abort the transfer */
1116550a7375SFelipe Balbi 		if (csr & MUSB_CSR0_H_REQPKT) {
1117550a7375SFelipe Balbi 			csr &= ~MUSB_CSR0_H_REQPKT;
1118550a7375SFelipe Balbi 			musb_writew(epio, MUSB_CSR0, csr);
1119550a7375SFelipe Balbi 			csr &= ~MUSB_CSR0_H_NAKTIMEOUT;
1120550a7375SFelipe Balbi 			musb_writew(epio, MUSB_CSR0, csr);
1121550a7375SFelipe Balbi 		} else {
112278322c1aSDavid Brownell 			musb_h_ep0_flush_fifo(hw_ep);
1123550a7375SFelipe Balbi 		}
1124550a7375SFelipe Balbi 
1125550a7375SFelipe Balbi 		musb_writeb(epio, MUSB_NAKLIMIT0, 0);
1126550a7375SFelipe Balbi 
1127550a7375SFelipe Balbi 		/* clear it */
1128550a7375SFelipe Balbi 		musb_writew(epio, MUSB_CSR0, 0);
1129550a7375SFelipe Balbi 	}
1130550a7375SFelipe Balbi 
1131550a7375SFelipe Balbi 	if (unlikely(!urb)) {
1132550a7375SFelipe Balbi 		/* stop endpoint since we have no place for its data, this
1133550a7375SFelipe Balbi 		 * SHOULD NEVER HAPPEN! */
1134550a7375SFelipe Balbi 		ERR("no URB for end 0\n");
1135550a7375SFelipe Balbi 
113678322c1aSDavid Brownell 		musb_h_ep0_flush_fifo(hw_ep);
1137550a7375SFelipe Balbi 		goto done;
1138550a7375SFelipe Balbi 	}
1139550a7375SFelipe Balbi 
1140550a7375SFelipe Balbi 	if (!complete) {
1141550a7375SFelipe Balbi 		/* call common logic and prepare response */
1142550a7375SFelipe Balbi 		if (musb_h_ep0_continue(musb, len, urb)) {
1143550a7375SFelipe Balbi 			/* more packets required */
1144550a7375SFelipe Balbi 			csr = (MUSB_EP0_IN == musb->ep0_stage)
1145550a7375SFelipe Balbi 				?  MUSB_CSR0_H_REQPKT : MUSB_CSR0_TXPKTRDY;
1146550a7375SFelipe Balbi 		} else {
1147550a7375SFelipe Balbi 			/* data transfer complete; perform status phase */
1148550a7375SFelipe Balbi 			if (usb_pipeout(urb->pipe)
1149550a7375SFelipe Balbi 					|| !urb->transfer_buffer_length)
1150550a7375SFelipe Balbi 				csr = MUSB_CSR0_H_STATUSPKT
1151550a7375SFelipe Balbi 					| MUSB_CSR0_H_REQPKT;
1152550a7375SFelipe Balbi 			else
1153550a7375SFelipe Balbi 				csr = MUSB_CSR0_H_STATUSPKT
1154550a7375SFelipe Balbi 					| MUSB_CSR0_TXPKTRDY;
1155550a7375SFelipe Balbi 
11563c4653c1SAjay Kumar Gupta 			/* disable ping token in status phase */
11573c4653c1SAjay Kumar Gupta 			csr |= MUSB_CSR0_H_DIS_PING;
11583c4653c1SAjay Kumar Gupta 
1159550a7375SFelipe Balbi 			/* flag status stage */
1160550a7375SFelipe Balbi 			musb->ep0_stage = MUSB_EP0_STATUS;
1161550a7375SFelipe Balbi 
1162b99d3659SBin Liu 			musb_dbg(musb, "ep0 STATUS, csr %04x", csr);
1163550a7375SFelipe Balbi 
1164550a7375SFelipe Balbi 		}
1165550a7375SFelipe Balbi 		musb_writew(epio, MUSB_CSR0, csr);
1166550a7375SFelipe Balbi 		retval = IRQ_HANDLED;
1167550a7375SFelipe Balbi 	} else
1168550a7375SFelipe Balbi 		musb->ep0_stage = MUSB_EP0_IDLE;
1169550a7375SFelipe Balbi 
1170550a7375SFelipe Balbi 	/* call completion handler if done */
1171550a7375SFelipe Balbi 	if (complete)
1172550a7375SFelipe Balbi 		musb_advance_schedule(musb, urb, hw_ep, 1);
1173550a7375SFelipe Balbi done:
1174550a7375SFelipe Balbi 	return retval;
1175550a7375SFelipe Balbi }
1176550a7375SFelipe Balbi 
1177550a7375SFelipe Balbi 
1178550a7375SFelipe Balbi #ifdef CONFIG_USB_INVENTRA_DMA
1179550a7375SFelipe Balbi 
1180550a7375SFelipe Balbi /* Host side TX (OUT) using Mentor DMA works as follows:
1181550a7375SFelipe Balbi 	submit_urb ->
1182550a7375SFelipe Balbi 		- if queue was empty, Program Endpoint
1183550a7375SFelipe Balbi 		- ... which starts DMA to fifo in mode 1 or 0
1184550a7375SFelipe Balbi 
1185550a7375SFelipe Balbi 	DMA Isr (transfer complete) -> TxAvail()
1186550a7375SFelipe Balbi 		- Stop DMA (~DmaEnab)	(<--- Alert ... currently happens
1187550a7375SFelipe Balbi 					only in musb_cleanup_urb)
1188550a7375SFelipe Balbi 		- TxPktRdy has to be set in mode 0 or for
1189550a7375SFelipe Balbi 			short packets in mode 1.
1190550a7375SFelipe Balbi */
1191550a7375SFelipe Balbi 
1192550a7375SFelipe Balbi #endif
1193550a7375SFelipe Balbi 
1194550a7375SFelipe Balbi /* Service a Tx-Available or dma completion irq for the endpoint */
musb_host_tx(struct musb * musb,u8 epnum)1195550a7375SFelipe Balbi void musb_host_tx(struct musb *musb, u8 epnum)
1196550a7375SFelipe Balbi {
1197550a7375SFelipe Balbi 	int			pipe;
1198550a7375SFelipe Balbi 	bool			done = false;
1199550a7375SFelipe Balbi 	u16			tx_csr;
12006b6e9710SSergei Shtylyov 	size_t			length = 0;
12016b6e9710SSergei Shtylyov 	size_t			offset = 0;
1202550a7375SFelipe Balbi 	struct musb_hw_ep	*hw_ep = musb->endpoints + epnum;
1203550a7375SFelipe Balbi 	void __iomem		*epio = hw_ep->regs;
12043e5c6dc7SSergei Shtylyov 	struct musb_qh		*qh = hw_ep->out_qh;
12053e5c6dc7SSergei Shtylyov 	struct urb		*urb = next_urb(qh);
1206550a7375SFelipe Balbi 	u32			status = 0;
1207550a7375SFelipe Balbi 	void __iomem		*mbase = musb->mregs;
1208550a7375SFelipe Balbi 	struct dma_channel	*dma;
1209f8afbf7fST. S., Anil Kumar 	bool			transfer_pending = false;
1210550a7375SFelipe Balbi 
1211550a7375SFelipe Balbi 	musb_ep_select(mbase, epnum);
1212550a7375SFelipe Balbi 	tx_csr = musb_readw(epio, MUSB_TXCSR);
1213550a7375SFelipe Balbi 
1214550a7375SFelipe Balbi 	/* with CPPI, DMA sometimes triggers "extra" irqs */
1215550a7375SFelipe Balbi 	if (!urb) {
1216b99d3659SBin Liu 		musb_dbg(musb, "extra TX%d ready, csr %04x", epnum, tx_csr);
12176b6e9710SSergei Shtylyov 		return;
1218550a7375SFelipe Balbi 	}
1219550a7375SFelipe Balbi 
1220550a7375SFelipe Balbi 	pipe = urb->pipe;
1221550a7375SFelipe Balbi 	dma = is_dma_capable() ? hw_ep->tx_channel : NULL;
122219ca682eSBin Liu 	trace_musb_urb_tx(musb, urb);
1223b99d3659SBin Liu 	musb_dbg(musb, "OUT/TX%d end, csr %04x%s", epnum, tx_csr,
1224550a7375SFelipe Balbi 			dma ? ", dma" : "");
1225550a7375SFelipe Balbi 
1226550a7375SFelipe Balbi 	/* check for errors */
1227550a7375SFelipe Balbi 	if (tx_csr & MUSB_TXCSR_H_RXSTALL) {
1228550a7375SFelipe Balbi 		/* dma was disabled, fifo flushed */
1229b99d3659SBin Liu 		musb_dbg(musb, "TX end %d stall", epnum);
1230550a7375SFelipe Balbi 
1231550a7375SFelipe Balbi 		/* stall; record URB status */
1232550a7375SFelipe Balbi 		status = -EPIPE;
1233550a7375SFelipe Balbi 
1234550a7375SFelipe Balbi 	} else if (tx_csr & MUSB_TXCSR_H_ERROR) {
1235550a7375SFelipe Balbi 		/* (NON-ISO) dma was disabled, fifo flushed */
1236b99d3659SBin Liu 		musb_dbg(musb, "TX 3strikes on ep=%d", epnum);
1237550a7375SFelipe Balbi 
1238550a7375SFelipe Balbi 		status = -ETIMEDOUT;
1239550a7375SFelipe Balbi 
1240550a7375SFelipe Balbi 	} else if (tx_csr & MUSB_TXCSR_H_NAKTIMEOUT) {
1241f283862fSAjay Kumar Gupta 		if (USB_ENDPOINT_XFER_BULK == qh->type && qh->mux == 1
1242f283862fSAjay Kumar Gupta 				&& !list_is_singular(&musb->out_bulk)) {
1243b99d3659SBin Liu 			musb_dbg(musb, "NAK timeout on TX%d ep", epnum);
1244f283862fSAjay Kumar Gupta 			musb_bulk_nak_timeout(musb, hw_ep, 0);
1245f283862fSAjay Kumar Gupta 		} else {
1246b99d3659SBin Liu 			musb_dbg(musb, "TX ep%d device not responding", epnum);
1247550a7375SFelipe Balbi 			/* NOTE:  this code path would be a good place to PAUSE a
1248550a7375SFelipe Balbi 			 * transfer, if there's some other (nonperiodic) tx urb
1249550a7375SFelipe Balbi 			 * that could use this fifo.  (dma complicates it...)
12501e0320f0SAjay Kumar Gupta 			 * That's already done for bulk RX transfers.
1251550a7375SFelipe Balbi 			 *
1252550a7375SFelipe Balbi 			 * if (bulk && qh->ring.next != &musb->out_bulk), then
1253550a7375SFelipe Balbi 			 * we have a candidate... NAKing is *NOT* an error
1254550a7375SFelipe Balbi 			 */
1255550a7375SFelipe Balbi 			musb_ep_select(mbase, epnum);
1256550a7375SFelipe Balbi 			musb_writew(epio, MUSB_TXCSR,
1257550a7375SFelipe Balbi 					MUSB_TXCSR_H_WZC_BITS
1258550a7375SFelipe Balbi 					| MUSB_TXCSR_TXPKTRDY);
1259f283862fSAjay Kumar Gupta 		}
12606b6e9710SSergei Shtylyov 		return;
1261550a7375SFelipe Balbi 	}
1262550a7375SFelipe Balbi 
12638e8a5516SVirupax Sadashivpetimath done:
1264550a7375SFelipe Balbi 	if (status) {
1265550a7375SFelipe Balbi 		if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) {
1266550a7375SFelipe Balbi 			dma->status = MUSB_DMA_STATUS_CORE_ABORT;
12679c547699SDaniel Mack 			musb->dma_controller->channel_abort(dma);
1268550a7375SFelipe Balbi 		}
1269550a7375SFelipe Balbi 
1270550a7375SFelipe Balbi 		/* do the proper sequence to abort the transfer in the
1271550a7375SFelipe Balbi 		 * usb core; the dma engine should already be stopped.
1272550a7375SFelipe Balbi 		 */
1273550a7375SFelipe Balbi 		musb_h_tx_flush_fifo(hw_ep);
1274550a7375SFelipe Balbi 		tx_csr &= ~(MUSB_TXCSR_AUTOSET
1275550a7375SFelipe Balbi 				| MUSB_TXCSR_DMAENAB
1276550a7375SFelipe Balbi 				| MUSB_TXCSR_H_ERROR
1277550a7375SFelipe Balbi 				| MUSB_TXCSR_H_RXSTALL
1278550a7375SFelipe Balbi 				| MUSB_TXCSR_H_NAKTIMEOUT
1279550a7375SFelipe Balbi 				);
1280550a7375SFelipe Balbi 
1281550a7375SFelipe Balbi 		musb_ep_select(mbase, epnum);
1282550a7375SFelipe Balbi 		musb_writew(epio, MUSB_TXCSR, tx_csr);
1283550a7375SFelipe Balbi 		/* REVISIT may need to clear FLUSHFIFO ... */
1284550a7375SFelipe Balbi 		musb_writew(epio, MUSB_TXCSR, tx_csr);
1285550a7375SFelipe Balbi 		musb_writeb(epio, MUSB_TXINTERVAL, 0);
1286550a7375SFelipe Balbi 
1287550a7375SFelipe Balbi 		done = true;
1288550a7375SFelipe Balbi 	}
1289550a7375SFelipe Balbi 
1290550a7375SFelipe Balbi 	/* second cppi case */
1291550a7375SFelipe Balbi 	if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) {
1292b99d3659SBin Liu 		musb_dbg(musb, "extra TX%d ready, csr %04x", epnum, tx_csr);
12936b6e9710SSergei Shtylyov 		return;
1294550a7375SFelipe Balbi 	}
1295550a7375SFelipe Balbi 
1296c7bbc056SSergei Shtylyov 	if (is_dma_capable() && dma && !status) {
1297c7bbc056SSergei Shtylyov 		/*
1298c7bbc056SSergei Shtylyov 		 * DMA has completed.  But if we're using DMA mode 1 (multi
1299c7bbc056SSergei Shtylyov 		 * packet DMA), we need a terminal TXPKTRDY interrupt before
1300c7bbc056SSergei Shtylyov 		 * we can consider this transfer completed, lest we trash
1301c7bbc056SSergei Shtylyov 		 * its last packet when writing the next URB's data.  So we
1302c7bbc056SSergei Shtylyov 		 * switch back to mode 0 to get that interrupt; we'll come
1303c7bbc056SSergei Shtylyov 		 * back here once it happens.
1304c7bbc056SSergei Shtylyov 		 */
1305c7bbc056SSergei Shtylyov 		if (tx_csr & MUSB_TXCSR_DMAMODE) {
1306c7bbc056SSergei Shtylyov 			/*
1307c7bbc056SSergei Shtylyov 			 * We shouldn't clear DMAMODE with DMAENAB set; so
1308c7bbc056SSergei Shtylyov 			 * clear them in a safe order.  That should be OK
1309c7bbc056SSergei Shtylyov 			 * once TXPKTRDY has been set (and I've never seen
1310c7bbc056SSergei Shtylyov 			 * it being 0 at this moment -- DMA interrupt latency
1311c7bbc056SSergei Shtylyov 			 * is significant) but if it hasn't been then we have
1312c7bbc056SSergei Shtylyov 			 * no choice but to stop being polite and ignore the
1313c7bbc056SSergei Shtylyov 			 * programmer's guide... :-)
1314c7bbc056SSergei Shtylyov 			 *
1315c7bbc056SSergei Shtylyov 			 * Note that we must write TXCSR with TXPKTRDY cleared
1316c7bbc056SSergei Shtylyov 			 * in order not to re-trigger the packet send (this bit
1317c7bbc056SSergei Shtylyov 			 * can't be cleared by CPU), and there's another caveat:
1318c7bbc056SSergei Shtylyov 			 * TXPKTRDY may be set shortly and then cleared in the
1319c7bbc056SSergei Shtylyov 			 * double-buffered FIFO mode, so we do an extra TXCSR
1320c7bbc056SSergei Shtylyov 			 * read for debouncing...
1321c7bbc056SSergei Shtylyov 			 */
1322c7bbc056SSergei Shtylyov 			tx_csr &= musb_readw(epio, MUSB_TXCSR);
1323c7bbc056SSergei Shtylyov 			if (tx_csr & MUSB_TXCSR_TXPKTRDY) {
1324c7bbc056SSergei Shtylyov 				tx_csr &= ~(MUSB_TXCSR_DMAENAB |
1325c7bbc056SSergei Shtylyov 					    MUSB_TXCSR_TXPKTRDY);
1326c7bbc056SSergei Shtylyov 				musb_writew(epio, MUSB_TXCSR,
1327c7bbc056SSergei Shtylyov 					    tx_csr | MUSB_TXCSR_H_WZC_BITS);
1328c7bbc056SSergei Shtylyov 			}
1329c7bbc056SSergei Shtylyov 			tx_csr &= ~(MUSB_TXCSR_DMAMODE |
1330c7bbc056SSergei Shtylyov 				    MUSB_TXCSR_TXPKTRDY);
1331c7bbc056SSergei Shtylyov 			musb_writew(epio, MUSB_TXCSR,
1332c7bbc056SSergei Shtylyov 				    tx_csr | MUSB_TXCSR_H_WZC_BITS);
1333c7bbc056SSergei Shtylyov 
1334c7bbc056SSergei Shtylyov 			/*
1335c7bbc056SSergei Shtylyov 			 * There is no guarantee that we'll get an interrupt
1336c7bbc056SSergei Shtylyov 			 * after clearing DMAMODE as we might have done this
1337c7bbc056SSergei Shtylyov 			 * too late (after TXPKTRDY was cleared by controller).
1338c7bbc056SSergei Shtylyov 			 * Re-read TXCSR as we have spoiled its previous value.
1339c7bbc056SSergei Shtylyov 			 */
1340c7bbc056SSergei Shtylyov 			tx_csr = musb_readw(epio, MUSB_TXCSR);
1341c7bbc056SSergei Shtylyov 		}
1342c7bbc056SSergei Shtylyov 
1343c7bbc056SSergei Shtylyov 		/*
1344c7bbc056SSergei Shtylyov 		 * We may get here from a DMA completion or TXPKTRDY interrupt.
1345c7bbc056SSergei Shtylyov 		 * In any case, we must check the FIFO status here and bail out
1346c7bbc056SSergei Shtylyov 		 * only if the FIFO still has data -- that should prevent the
1347c7bbc056SSergei Shtylyov 		 * "missed" TXPKTRDY interrupts and deal with double-buffered
1348c7bbc056SSergei Shtylyov 		 * FIFO mode too...
1349c7bbc056SSergei Shtylyov 		 */
1350c7bbc056SSergei Shtylyov 		if (tx_csr & (MUSB_TXCSR_FIFONOTEMPTY | MUSB_TXCSR_TXPKTRDY)) {
1351b99d3659SBin Liu 			musb_dbg(musb,
1352b99d3659SBin Liu 				"DMA complete but FIFO not empty, CSR %04x",
1353b99d3659SBin Liu 				tx_csr);
1354c7bbc056SSergei Shtylyov 			return;
1355c7bbc056SSergei Shtylyov 		}
1356c7bbc056SSergei Shtylyov 	}
1357c7bbc056SSergei Shtylyov 
1358550a7375SFelipe Balbi 	if (!status || dma || usb_pipeisoc(pipe)) {
1359550a7375SFelipe Balbi 		if (dma)
13606b6e9710SSergei Shtylyov 			length = dma->actual_len;
1361550a7375SFelipe Balbi 		else
13626b6e9710SSergei Shtylyov 			length = qh->segsize;
13636b6e9710SSergei Shtylyov 		qh->offset += length;
1364550a7375SFelipe Balbi 
1365550a7375SFelipe Balbi 		if (usb_pipeisoc(pipe)) {
1366550a7375SFelipe Balbi 			struct usb_iso_packet_descriptor	*d;
1367550a7375SFelipe Balbi 
1368550a7375SFelipe Balbi 			d = urb->iso_frame_desc + qh->iso_idx;
13696b6e9710SSergei Shtylyov 			d->actual_length = length;
13706b6e9710SSergei Shtylyov 			d->status = status;
1371550a7375SFelipe Balbi 			if (++qh->iso_idx >= urb->number_of_packets) {
1372550a7375SFelipe Balbi 				done = true;
1373550a7375SFelipe Balbi 			} else {
1374550a7375SFelipe Balbi 				d++;
13756b6e9710SSergei Shtylyov 				offset = d->offset;
13766b6e9710SSergei Shtylyov 				length = d->length;
1377550a7375SFelipe Balbi 			}
1378f8afbf7fST. S., Anil Kumar 		} else if (dma && urb->transfer_buffer_length == qh->offset) {
1379550a7375SFelipe Balbi 			done = true;
1380550a7375SFelipe Balbi 		} else {
1381550a7375SFelipe Balbi 			/* see if we need to send more data, or ZLP */
1382550a7375SFelipe Balbi 			if (qh->segsize < qh->maxpacket)
1383550a7375SFelipe Balbi 				done = true;
1384550a7375SFelipe Balbi 			else if (qh->offset == urb->transfer_buffer_length
1385550a7375SFelipe Balbi 					&& !(urb->transfer_flags
1386550a7375SFelipe Balbi 						& URB_ZERO_PACKET))
1387550a7375SFelipe Balbi 				done = true;
1388550a7375SFelipe Balbi 			if (!done) {
13896b6e9710SSergei Shtylyov 				offset = qh->offset;
13906b6e9710SSergei Shtylyov 				length = urb->transfer_buffer_length - offset;
1391f8afbf7fST. S., Anil Kumar 				transfer_pending = true;
1392550a7375SFelipe Balbi 			}
1393550a7375SFelipe Balbi 		}
1394550a7375SFelipe Balbi 	}
1395550a7375SFelipe Balbi 
1396550a7375SFelipe Balbi 	/* urb->status != -EINPROGRESS means request has been faulted,
1397550a7375SFelipe Balbi 	 * so we must abort this transfer after cleanup
1398550a7375SFelipe Balbi 	 */
1399550a7375SFelipe Balbi 	if (urb->status != -EINPROGRESS) {
1400550a7375SFelipe Balbi 		done = true;
1401550a7375SFelipe Balbi 		if (status == 0)
1402550a7375SFelipe Balbi 			status = urb->status;
1403550a7375SFelipe Balbi 	}
1404550a7375SFelipe Balbi 
1405550a7375SFelipe Balbi 	if (done) {
1406550a7375SFelipe Balbi 		/* set status */
1407550a7375SFelipe Balbi 		urb->status = status;
1408550a7375SFelipe Balbi 		urb->actual_length = qh->offset;
1409550a7375SFelipe Balbi 		musb_advance_schedule(musb, urb, hw_ep, USB_DIR_OUT);
14106b6e9710SSergei Shtylyov 		return;
1411f8afbf7fST. S., Anil Kumar 	} else if ((usb_pipeisoc(pipe) || transfer_pending) && dma) {
14126b6e9710SSergei Shtylyov 		if (musb_tx_dma_program(musb->dma_controller, hw_ep, qh, urb,
1413dfeffa53SAjay Kumar Gupta 				offset, length)) {
1414f8e9f34fSTony Lindgren 			if (is_cppi_enabled(musb) || tusb_dma_omap(musb))
1415dfeffa53SAjay Kumar Gupta 				musb_h_tx_dma_start(hw_ep);
14166b6e9710SSergei Shtylyov 			return;
1417dfeffa53SAjay Kumar Gupta 		}
14186b6e9710SSergei Shtylyov 	} else	if (tx_csr & MUSB_TXCSR_DMAENAB) {
1419b99d3659SBin Liu 		musb_dbg(musb, "not complete, but DMA enabled?");
14206b6e9710SSergei Shtylyov 		return;
14216b6e9710SSergei Shtylyov 	}
1422550a7375SFelipe Balbi 
14236b6e9710SSergei Shtylyov 	/*
14246b6e9710SSergei Shtylyov 	 * PIO: start next packet in this URB.
14256b6e9710SSergei Shtylyov 	 *
14266b6e9710SSergei Shtylyov 	 * REVISIT: some docs say that when hw_ep->tx_double_buffered,
14276b6e9710SSergei Shtylyov 	 * (and presumably, FIFO is not half-full) we should write *two*
14286b6e9710SSergei Shtylyov 	 * packets before updating TXCSR; other docs disagree...
1429550a7375SFelipe Balbi 	 */
14306b6e9710SSergei Shtylyov 	if (length > qh->maxpacket)
14316b6e9710SSergei Shtylyov 		length = qh->maxpacket;
1432496dda70SMaulik Mankad 	/* Unmap the buffer so that CPU can use it */
14338b125df5SDaniel Mack 	usb_hcd_unmap_urb_for_dma(musb->hcd, urb);
14348e8a5516SVirupax Sadashivpetimath 
14358e8a5516SVirupax Sadashivpetimath 	/*
14368e8a5516SVirupax Sadashivpetimath 	 * We need to map sg if the transfer_buffer is
14378e8a5516SVirupax Sadashivpetimath 	 * NULL.
14388e8a5516SVirupax Sadashivpetimath 	 */
143952974d94SMans Rullgard 	if (!urb->transfer_buffer) {
14408e8a5516SVirupax Sadashivpetimath 		/* sg_miter_start is already done in musb_ep_program */
14418e8a5516SVirupax Sadashivpetimath 		if (!sg_miter_next(&qh->sg_miter)) {
14428e8a5516SVirupax Sadashivpetimath 			dev_err(musb->controller, "error: sg list empty\n");
14438e8a5516SVirupax Sadashivpetimath 			sg_miter_stop(&qh->sg_miter);
14448e8a5516SVirupax Sadashivpetimath 			status = -EINVAL;
14458e8a5516SVirupax Sadashivpetimath 			goto done;
14468e8a5516SVirupax Sadashivpetimath 		}
14478e8a5516SVirupax Sadashivpetimath 		length = min_t(u32, length, qh->sg_miter.length);
144852974d94SMans Rullgard 		musb_write_fifo(hw_ep, length, qh->sg_miter.addr);
14498e8a5516SVirupax Sadashivpetimath 		qh->sg_miter.consumed = length;
14508e8a5516SVirupax Sadashivpetimath 		sg_miter_stop(&qh->sg_miter);
14518e8a5516SVirupax Sadashivpetimath 	} else {
14526b6e9710SSergei Shtylyov 		musb_write_fifo(hw_ep, length, urb->transfer_buffer + offset);
14538e8a5516SVirupax Sadashivpetimath 	}
14548e8a5516SVirupax Sadashivpetimath 
14556b6e9710SSergei Shtylyov 	qh->segsize = length;
1456550a7375SFelipe Balbi 
1457550a7375SFelipe Balbi 	musb_ep_select(mbase, epnum);
1458550a7375SFelipe Balbi 	musb_writew(epio, MUSB_TXCSR,
1459550a7375SFelipe Balbi 			MUSB_TXCSR_H_WZC_BITS | MUSB_TXCSR_TXPKTRDY);
1460550a7375SFelipe Balbi }
1461550a7375SFelipe Balbi 
1462069a3fd1STony Lindgren #ifdef CONFIG_USB_TI_CPPI41_DMA
1463069a3fd1STony Lindgren /* Seems to set up ISO for cppi41 and not advance len. See commit c57c41d */
musb_rx_dma_iso_cppi41(struct dma_controller * dma,struct musb_hw_ep * hw_ep,struct musb_qh * qh,struct urb * urb,size_t len)1464069a3fd1STony Lindgren static int musb_rx_dma_iso_cppi41(struct dma_controller *dma,
1465069a3fd1STony Lindgren 				  struct musb_hw_ep *hw_ep,
1466069a3fd1STony Lindgren 				  struct musb_qh *qh,
1467069a3fd1STony Lindgren 				  struct urb *urb,
1468069a3fd1STony Lindgren 				  size_t len)
1469069a3fd1STony Lindgren {
147004471eb8SBin Liu 	struct dma_channel *channel = hw_ep->rx_channel;
1471069a3fd1STony Lindgren 	void __iomem *epio = hw_ep->regs;
1472069a3fd1STony Lindgren 	dma_addr_t *buf;
1473c68bb0efSGustavo A. R. Silva 	u32 length;
1474069a3fd1STony Lindgren 	u16 val;
1475069a3fd1STony Lindgren 
1476069a3fd1STony Lindgren 	buf = (void *)urb->iso_frame_desc[qh->iso_idx].offset +
1477069a3fd1STony Lindgren 		(u32)urb->transfer_dma;
1478069a3fd1STony Lindgren 
1479069a3fd1STony Lindgren 	length = urb->iso_frame_desc[qh->iso_idx].length;
1480069a3fd1STony Lindgren 
1481069a3fd1STony Lindgren 	val = musb_readw(epio, MUSB_RXCSR);
1482069a3fd1STony Lindgren 	val |= MUSB_RXCSR_DMAENAB;
1483069a3fd1STony Lindgren 	musb_writew(hw_ep->regs, MUSB_RXCSR, val);
1484069a3fd1STony Lindgren 
1485c68bb0efSGustavo A. R. Silva 	return dma->channel_program(channel, qh->maxpacket, 0,
1486069a3fd1STony Lindgren 				   (u32)buf, length);
1487069a3fd1STony Lindgren }
1488069a3fd1STony Lindgren #else
musb_rx_dma_iso_cppi41(struct dma_controller * dma,struct musb_hw_ep * hw_ep,struct musb_qh * qh,struct urb * urb,size_t len)1489069a3fd1STony Lindgren static inline int musb_rx_dma_iso_cppi41(struct dma_controller *dma,
1490069a3fd1STony Lindgren 					 struct musb_hw_ep *hw_ep,
1491069a3fd1STony Lindgren 					 struct musb_qh *qh,
1492069a3fd1STony Lindgren 					 struct urb *urb,
1493069a3fd1STony Lindgren 					 size_t len)
1494069a3fd1STony Lindgren {
1495069a3fd1STony Lindgren 	return false;
1496069a3fd1STony Lindgren }
1497069a3fd1STony Lindgren #endif
1498550a7375SFelipe Balbi 
1499cff84bdbSTony Lindgren #if defined(CONFIG_USB_INVENTRA_DMA) || defined(CONFIG_USB_UX500_DMA) || \
1500cff84bdbSTony Lindgren 	defined(CONFIG_USB_TI_CPPI41_DMA)
1501550a7375SFelipe Balbi /* Host side RX (IN) using Mentor DMA works as follows:
1502550a7375SFelipe Balbi 	submit_urb ->
1503550a7375SFelipe Balbi 		- if queue was empty, ProgramEndpoint
1504550a7375SFelipe Balbi 		- first IN token is sent out (by setting ReqPkt)
1505550a7375SFelipe Balbi 	LinuxIsr -> RxReady()
1506550a7375SFelipe Balbi 	/\	=> first packet is received
1507550a7375SFelipe Balbi 	|	- Set in mode 0 (DmaEnab, ~ReqPkt)
1508550a7375SFelipe Balbi 	|		-> DMA Isr (transfer complete) -> RxReady()
1509550a7375SFelipe Balbi 	|		    - Ack receive (~RxPktRdy), turn off DMA (~DmaEnab)
1510550a7375SFelipe Balbi 	|		    - if urb not complete, send next IN token (ReqPkt)
1511550a7375SFelipe Balbi 	|			   |		else complete urb.
1512550a7375SFelipe Balbi 	|			   |
1513550a7375SFelipe Balbi 	---------------------------
1514550a7375SFelipe Balbi  *
1515550a7375SFelipe Balbi  * Nuances of mode 1:
1516550a7375SFelipe Balbi  *	For short packets, no ack (+RxPktRdy) is sent automatically
1517550a7375SFelipe Balbi  *	(even if AutoClear is ON)
1518550a7375SFelipe Balbi  *	For full packets, ack (~RxPktRdy) and next IN token (+ReqPkt) is sent
1519550a7375SFelipe Balbi  *	automatically => major problem, as collecting the next packet becomes
1520550a7375SFelipe Balbi  *	difficult. Hence mode 1 is not used.
1521550a7375SFelipe Balbi  *
1522550a7375SFelipe Balbi  * REVISIT
1523550a7375SFelipe Balbi  *	All we care about at this driver level is that
1524550a7375SFelipe Balbi  *       (a) all URBs terminate with REQPKT cleared and fifo(s) empty;
1525550a7375SFelipe Balbi  *       (b) termination conditions are: short RX, or buffer full;
1526550a7375SFelipe Balbi  *       (c) fault modes include
1527550a7375SFelipe Balbi  *           - iff URB_SHORT_NOT_OK, short RX status is -EREMOTEIO.
1528550a7375SFelipe Balbi  *             (and that endpoint's dma queue stops immediately)
1529550a7375SFelipe Balbi  *           - overflow (full, PLUS more bytes in the terminal packet)
1530550a7375SFelipe Balbi  *
1531550a7375SFelipe Balbi  *	So for example, usb-storage sets URB_SHORT_NOT_OK, and would
1532550a7375SFelipe Balbi  *	thus be a great candidate for using mode 1 ... for all but the
1533550a7375SFelipe Balbi  *	last packet of one URB's transfer.
1534550a7375SFelipe Balbi  */
musb_rx_dma_inventra_cppi41(struct dma_controller * dma,struct musb_hw_ep * hw_ep,struct musb_qh * qh,struct urb * urb,size_t len)1535cff84bdbSTony Lindgren static int musb_rx_dma_inventra_cppi41(struct dma_controller *dma,
1536cff84bdbSTony Lindgren 				       struct musb_hw_ep *hw_ep,
1537cff84bdbSTony Lindgren 				       struct musb_qh *qh,
1538cff84bdbSTony Lindgren 				       struct urb *urb,
1539cff84bdbSTony Lindgren 				       size_t len)
1540cff84bdbSTony Lindgren {
1541cff84bdbSTony Lindgren 	struct dma_channel *channel = hw_ep->rx_channel;
1542cff84bdbSTony Lindgren 	void __iomem *epio = hw_ep->regs;
1543cff84bdbSTony Lindgren 	u16 val;
1544cff84bdbSTony Lindgren 	int pipe;
1545cff84bdbSTony Lindgren 	bool done;
1546550a7375SFelipe Balbi 
1547cff84bdbSTony Lindgren 	pipe = urb->pipe;
1548cff84bdbSTony Lindgren 
1549cff84bdbSTony Lindgren 	if (usb_pipeisoc(pipe)) {
1550cff84bdbSTony Lindgren 		struct usb_iso_packet_descriptor *d;
1551cff84bdbSTony Lindgren 
1552cff84bdbSTony Lindgren 		d = urb->iso_frame_desc + qh->iso_idx;
1553cff84bdbSTony Lindgren 		d->actual_length = len;
1554cff84bdbSTony Lindgren 
1555cff84bdbSTony Lindgren 		/* even if there was an error, we did the dma
1556cff84bdbSTony Lindgren 		 * for iso_frame_desc->length
1557cff84bdbSTony Lindgren 		 */
1558cff84bdbSTony Lindgren 		if (d->status != -EILSEQ && d->status != -EOVERFLOW)
1559cff84bdbSTony Lindgren 			d->status = 0;
1560cff84bdbSTony Lindgren 
1561cff84bdbSTony Lindgren 		if (++qh->iso_idx >= urb->number_of_packets) {
1562cff84bdbSTony Lindgren 			done = true;
1563cff84bdbSTony Lindgren 		} else {
1564cff84bdbSTony Lindgren 			/* REVISIT: Why ignore return value here? */
1565cff84bdbSTony Lindgren 			if (musb_dma_cppi41(hw_ep->musb))
1566cff84bdbSTony Lindgren 				done = musb_rx_dma_iso_cppi41(dma, hw_ep, qh,
1567cff84bdbSTony Lindgren 							      urb, len);
1568cff84bdbSTony Lindgren 			done = false;
1569cff84bdbSTony Lindgren 		}
1570cff84bdbSTony Lindgren 
1571cff84bdbSTony Lindgren 	} else  {
1572cff84bdbSTony Lindgren 		/* done if urb buffer is full or short packet is recd */
1573cff84bdbSTony Lindgren 		done = (urb->actual_length + len >=
1574cff84bdbSTony Lindgren 			urb->transfer_buffer_length
1575cff84bdbSTony Lindgren 			|| channel->actual_len < qh->maxpacket
1576cff84bdbSTony Lindgren 			|| channel->rx_packet_done);
1577cff84bdbSTony Lindgren 	}
1578cff84bdbSTony Lindgren 
1579cff84bdbSTony Lindgren 	/* send IN token for next packet, without AUTOREQ */
1580cff84bdbSTony Lindgren 	if (!done) {
1581cff84bdbSTony Lindgren 		val = musb_readw(epio, MUSB_RXCSR);
1582cff84bdbSTony Lindgren 		val |= MUSB_RXCSR_H_REQPKT;
1583cff84bdbSTony Lindgren 		musb_writew(epio, MUSB_RXCSR, MUSB_RXCSR_H_WZC_BITS | val);
1584cff84bdbSTony Lindgren 	}
1585cff84bdbSTony Lindgren 
1586cff84bdbSTony Lindgren 	return done;
1587cff84bdbSTony Lindgren }
1588ac33cdb1STony Lindgren 
1589ac33cdb1STony Lindgren /* Disadvantage of using mode 1:
1590ac33cdb1STony Lindgren  *	It's basically usable only for mass storage class; essentially all
1591ac33cdb1STony Lindgren  *	other protocols also terminate transfers on short packets.
1592ac33cdb1STony Lindgren  *
1593ac33cdb1STony Lindgren  * Details:
1594ac33cdb1STony Lindgren  *	An extra IN token is sent at the end of the transfer (due to AUTOREQ)
1595ac33cdb1STony Lindgren  *	If you try to use mode 1 for (transfer_buffer_length - 512), and try
1596ac33cdb1STony Lindgren  *	to use the extra IN token to grab the last packet using mode 0, then
1597ac33cdb1STony Lindgren  *	the problem is that you cannot be sure when the device will send the
1598ac33cdb1STony Lindgren  *	last packet and RxPktRdy set. Sometimes the packet is recd too soon
1599ac33cdb1STony Lindgren  *	such that it gets lost when RxCSR is re-set at the end of the mode 1
1600ac33cdb1STony Lindgren  *	transfer, while sometimes it is recd just a little late so that if you
1601ac33cdb1STony Lindgren  *	try to configure for mode 0 soon after the mode 1 transfer is
1602ac33cdb1STony Lindgren  *	completed, you will find rxcount 0. Okay, so you might think why not
1603ac33cdb1STony Lindgren  *	wait for an interrupt when the pkt is recd. Well, you won't get any!
1604ac33cdb1STony Lindgren  */
musb_rx_dma_in_inventra_cppi41(struct dma_controller * dma,struct musb_hw_ep * hw_ep,struct musb_qh * qh,struct urb * urb,size_t len,u8 iso_err)1605ac33cdb1STony Lindgren static int musb_rx_dma_in_inventra_cppi41(struct dma_controller *dma,
1606ac33cdb1STony Lindgren 					  struct musb_hw_ep *hw_ep,
1607ac33cdb1STony Lindgren 					  struct musb_qh *qh,
1608ac33cdb1STony Lindgren 					  struct urb *urb,
1609ac33cdb1STony Lindgren 					  size_t len,
1610ac33cdb1STony Lindgren 					  u8 iso_err)
1611ac33cdb1STony Lindgren {
1612ac33cdb1STony Lindgren 	struct musb *musb = hw_ep->musb;
1613ac33cdb1STony Lindgren 	void __iomem *epio = hw_ep->regs;
1614ac33cdb1STony Lindgren 	struct dma_channel *channel = hw_ep->rx_channel;
1615ac33cdb1STony Lindgren 	u16 rx_count, val;
1616ac33cdb1STony Lindgren 	int length, pipe, done;
1617ac33cdb1STony Lindgren 	dma_addr_t buf;
1618ac33cdb1STony Lindgren 
1619ac33cdb1STony Lindgren 	rx_count = musb_readw(epio, MUSB_RXCOUNT);
1620ac33cdb1STony Lindgren 	pipe = urb->pipe;
1621ac33cdb1STony Lindgren 
1622ac33cdb1STony Lindgren 	if (usb_pipeisoc(pipe)) {
1623ac33cdb1STony Lindgren 		int d_status = 0;
1624ac33cdb1STony Lindgren 		struct usb_iso_packet_descriptor *d;
1625ac33cdb1STony Lindgren 
1626ac33cdb1STony Lindgren 		d = urb->iso_frame_desc + qh->iso_idx;
1627ac33cdb1STony Lindgren 
1628ac33cdb1STony Lindgren 		if (iso_err) {
1629ac33cdb1STony Lindgren 			d_status = -EILSEQ;
1630ac33cdb1STony Lindgren 			urb->error_count++;
1631ac33cdb1STony Lindgren 		}
1632ac33cdb1STony Lindgren 		if (rx_count > d->length) {
1633ac33cdb1STony Lindgren 			if (d_status == 0) {
1634ac33cdb1STony Lindgren 				d_status = -EOVERFLOW;
1635ac33cdb1STony Lindgren 				urb->error_count++;
1636ac33cdb1STony Lindgren 			}
1637b99d3659SBin Liu 			musb_dbg(musb, "** OVERFLOW %d into %d",
1638ac33cdb1STony Lindgren 				rx_count, d->length);
1639ac33cdb1STony Lindgren 
1640ac33cdb1STony Lindgren 			length = d->length;
1641ac33cdb1STony Lindgren 		} else
1642ac33cdb1STony Lindgren 			length = rx_count;
1643ac33cdb1STony Lindgren 		d->status = d_status;
1644ac33cdb1STony Lindgren 		buf = urb->transfer_dma + d->offset;
1645ac33cdb1STony Lindgren 	} else {
1646ac33cdb1STony Lindgren 		length = rx_count;
1647ac33cdb1STony Lindgren 		buf = urb->transfer_dma + urb->actual_length;
1648ac33cdb1STony Lindgren 	}
1649ac33cdb1STony Lindgren 
1650ac33cdb1STony Lindgren 	channel->desired_mode = 0;
1651ac33cdb1STony Lindgren #ifdef USE_MODE1
1652ac33cdb1STony Lindgren 	/* because of the issue below, mode 1 will
1653ac33cdb1STony Lindgren 	 * only rarely behave with correct semantics.
1654ac33cdb1STony Lindgren 	 */
1655ac33cdb1STony Lindgren 	if ((urb->transfer_flags & URB_SHORT_NOT_OK)
1656ac33cdb1STony Lindgren 	    && (urb->transfer_buffer_length - urb->actual_length)
1657ac33cdb1STony Lindgren 	    > qh->maxpacket)
1658ac33cdb1STony Lindgren 		channel->desired_mode = 1;
1659ac33cdb1STony Lindgren 	if (rx_count < hw_ep->max_packet_sz_rx) {
1660ac33cdb1STony Lindgren 		length = rx_count;
1661ac33cdb1STony Lindgren 		channel->desired_mode = 0;
1662ac33cdb1STony Lindgren 	} else {
1663ac33cdb1STony Lindgren 		length = urb->transfer_buffer_length;
1664ac33cdb1STony Lindgren 	}
1665ac33cdb1STony Lindgren #endif
1666ac33cdb1STony Lindgren 
1667ac33cdb1STony Lindgren 	/* See comments above on disadvantages of using mode 1 */
1668ac33cdb1STony Lindgren 	val = musb_readw(epio, MUSB_RXCSR);
1669ac33cdb1STony Lindgren 	val &= ~MUSB_RXCSR_H_REQPKT;
1670ac33cdb1STony Lindgren 
1671ac33cdb1STony Lindgren 	if (channel->desired_mode == 0)
1672ac33cdb1STony Lindgren 		val &= ~MUSB_RXCSR_H_AUTOREQ;
1673ac33cdb1STony Lindgren 	else
1674ac33cdb1STony Lindgren 		val |= MUSB_RXCSR_H_AUTOREQ;
1675ac33cdb1STony Lindgren 	val |= MUSB_RXCSR_DMAENAB;
1676ac33cdb1STony Lindgren 
1677ac33cdb1STony Lindgren 	/* autoclear shouldn't be set in high bandwidth */
1678ac33cdb1STony Lindgren 	if (qh->hb_mult == 1)
1679ac33cdb1STony Lindgren 		val |= MUSB_RXCSR_AUTOCLEAR;
1680ac33cdb1STony Lindgren 
1681ac33cdb1STony Lindgren 	musb_writew(epio, MUSB_RXCSR, MUSB_RXCSR_H_WZC_BITS | val);
1682ac33cdb1STony Lindgren 
1683ac33cdb1STony Lindgren 	/* REVISIT if when actual_length != 0,
1684ac33cdb1STony Lindgren 	 * transfer_buffer_length needs to be
1685ac33cdb1STony Lindgren 	 * adjusted first...
1686ac33cdb1STony Lindgren 	 */
1687ac33cdb1STony Lindgren 	done = dma->channel_program(channel, qh->maxpacket,
1688ac33cdb1STony Lindgren 				   channel->desired_mode,
1689ac33cdb1STony Lindgren 				   buf, length);
1690ac33cdb1STony Lindgren 
1691ac33cdb1STony Lindgren 	if (!done) {
1692ac33cdb1STony Lindgren 		dma->channel_release(channel);
1693ac33cdb1STony Lindgren 		hw_ep->rx_channel = NULL;
1694ac33cdb1STony Lindgren 		channel = NULL;
1695ac33cdb1STony Lindgren 		val = musb_readw(epio, MUSB_RXCSR);
1696ac33cdb1STony Lindgren 		val &= ~(MUSB_RXCSR_DMAENAB
1697ac33cdb1STony Lindgren 			 | MUSB_RXCSR_H_AUTOREQ
1698ac33cdb1STony Lindgren 			 | MUSB_RXCSR_AUTOCLEAR);
1699ac33cdb1STony Lindgren 		musb_writew(epio, MUSB_RXCSR, val);
1700ac33cdb1STony Lindgren 	}
1701ac33cdb1STony Lindgren 
1702ac33cdb1STony Lindgren 	return done;
1703ac33cdb1STony Lindgren }
1704cff84bdbSTony Lindgren #else
musb_rx_dma_inventra_cppi41(struct dma_controller * dma,struct musb_hw_ep * hw_ep,struct musb_qh * qh,struct urb * urb,size_t len)1705cff84bdbSTony Lindgren static inline int musb_rx_dma_inventra_cppi41(struct dma_controller *dma,
1706cff84bdbSTony Lindgren 					      struct musb_hw_ep *hw_ep,
1707cff84bdbSTony Lindgren 					      struct musb_qh *qh,
1708cff84bdbSTony Lindgren 					      struct urb *urb,
1709cff84bdbSTony Lindgren 					      size_t len)
1710cff84bdbSTony Lindgren {
1711cff84bdbSTony Lindgren 	return false;
1712cff84bdbSTony Lindgren }
1713ac33cdb1STony Lindgren 
musb_rx_dma_in_inventra_cppi41(struct dma_controller * dma,struct musb_hw_ep * hw_ep,struct musb_qh * qh,struct urb * urb,size_t len,u8 iso_err)1714ac33cdb1STony Lindgren static inline int musb_rx_dma_in_inventra_cppi41(struct dma_controller *dma,
1715ac33cdb1STony Lindgren 						 struct musb_hw_ep *hw_ep,
1716ac33cdb1STony Lindgren 						 struct musb_qh *qh,
1717ac33cdb1STony Lindgren 						 struct urb *urb,
1718ac33cdb1STony Lindgren 						 size_t len,
1719ac33cdb1STony Lindgren 						 u8 iso_err)
1720ac33cdb1STony Lindgren {
1721ac33cdb1STony Lindgren 	return false;
1722ac33cdb1STony Lindgren }
1723550a7375SFelipe Balbi #endif
1724550a7375SFelipe Balbi 
1725550a7375SFelipe Balbi /*
1726550a7375SFelipe Balbi  * Service an RX interrupt for the given IN endpoint; docs cover bulk, iso,
1727550a7375SFelipe Balbi  * and high-bandwidth IN transfer cases.
1728550a7375SFelipe Balbi  */
musb_host_rx(struct musb * musb,u8 epnum)1729550a7375SFelipe Balbi void musb_host_rx(struct musb *musb, u8 epnum)
1730550a7375SFelipe Balbi {
1731550a7375SFelipe Balbi 	struct urb		*urb;
1732550a7375SFelipe Balbi 	struct musb_hw_ep	*hw_ep = musb->endpoints + epnum;
1733cff84bdbSTony Lindgren 	struct dma_controller	*c = musb->dma_controller;
1734550a7375SFelipe Balbi 	void __iomem		*epio = hw_ep->regs;
1735550a7375SFelipe Balbi 	struct musb_qh		*qh = hw_ep->in_qh;
1736550a7375SFelipe Balbi 	size_t			xfer_len;
1737550a7375SFelipe Balbi 	void __iomem		*mbase = musb->mregs;
1738550a7375SFelipe Balbi 	u16			rx_csr, val;
1739550a7375SFelipe Balbi 	bool			iso_err = false;
1740550a7375SFelipe Balbi 	bool			done = false;
1741550a7375SFelipe Balbi 	u32			status;
1742550a7375SFelipe Balbi 	struct dma_channel	*dma;
17438e8a5516SVirupax Sadashivpetimath 	unsigned int sg_flags = SG_MITER_ATOMIC | SG_MITER_TO_SG;
1744550a7375SFelipe Balbi 
1745550a7375SFelipe Balbi 	musb_ep_select(mbase, epnum);
1746550a7375SFelipe Balbi 
1747550a7375SFelipe Balbi 	urb = next_urb(qh);
1748550a7375SFelipe Balbi 	dma = is_dma_capable() ? hw_ep->rx_channel : NULL;
1749550a7375SFelipe Balbi 	status = 0;
1750550a7375SFelipe Balbi 	xfer_len = 0;
1751550a7375SFelipe Balbi 
1752550a7375SFelipe Balbi 	rx_csr = musb_readw(epio, MUSB_RXCSR);
1753550a7375SFelipe Balbi 	val = rx_csr;
1754550a7375SFelipe Balbi 
1755550a7375SFelipe Balbi 	if (unlikely(!urb)) {
1756550a7375SFelipe Balbi 		/* REVISIT -- THIS SHOULD NEVER HAPPEN ... but, at least
1757550a7375SFelipe Balbi 		 * usbtest #11 (unlinks) triggers it regularly, sometimes
1758550a7375SFelipe Balbi 		 * with fifo full.  (Only with DMA??)
1759550a7375SFelipe Balbi 		 */
1760b99d3659SBin Liu 		musb_dbg(musb, "BOGUS RX%d ready, csr %04x, count %d",
1761b99d3659SBin Liu 			epnum, val, musb_readw(epio, MUSB_RXCOUNT));
1762550a7375SFelipe Balbi 		musb_h_flush_rxfifo(hw_ep, MUSB_RXCSR_CLRDATATOG);
1763550a7375SFelipe Balbi 		return;
1764550a7375SFelipe Balbi 	}
1765550a7375SFelipe Balbi 
176619ca682eSBin Liu 	trace_musb_urb_rx(musb, urb);
1767550a7375SFelipe Balbi 
1768550a7375SFelipe Balbi 	/* check for errors, concurrent stall & unlink is not really
1769550a7375SFelipe Balbi 	 * handled yet! */
1770550a7375SFelipe Balbi 	if (rx_csr & MUSB_RXCSR_H_RXSTALL) {
1771b99d3659SBin Liu 		musb_dbg(musb, "RX end %d STALL", epnum);
1772550a7375SFelipe Balbi 
1773550a7375SFelipe Balbi 		/* stall; record URB status */
1774550a7375SFelipe Balbi 		status = -EPIPE;
1775550a7375SFelipe Balbi 
1776550a7375SFelipe Balbi 	} else if (rx_csr & MUSB_RXCSR_H_ERROR) {
17771b967691SBin Liu 		dev_err(musb->controller, "ep%d RX three-strikes error", epnum);
1778550a7375SFelipe Balbi 
17791b967691SBin Liu 		/*
17801b967691SBin Liu 		 * The three-strikes error could only happen when the USB
17811b967691SBin Liu 		 * device is not accessible, for example detached or powered
17821b967691SBin Liu 		 * off. So return the fatal error -ESHUTDOWN so hopefully the
17831b967691SBin Liu 		 * USB device drivers won't immediately resubmit the same URB.
17841b967691SBin Liu 		 */
17851b967691SBin Liu 		status = -ESHUTDOWN;
1786550a7375SFelipe Balbi 		musb_writeb(epio, MUSB_RXINTERVAL, 0);
1787550a7375SFelipe Balbi 
1788b5801212SBin Liu 		rx_csr &= ~MUSB_RXCSR_H_ERROR;
1789b5801212SBin Liu 		musb_writew(epio, MUSB_RXCSR, rx_csr);
1790b5801212SBin Liu 
1791550a7375SFelipe Balbi 	} else if (rx_csr & MUSB_RXCSR_DATAERROR) {
1792550a7375SFelipe Balbi 
1793550a7375SFelipe Balbi 		if (USB_ENDPOINT_XFER_ISOC != qh->type) {
1794b99d3659SBin Liu 			musb_dbg(musb, "RX end %d NAK timeout", epnum);
17951e0320f0SAjay Kumar Gupta 
17961e0320f0SAjay Kumar Gupta 			/* NOTE: NAKing is *NOT* an error, so we want to
17971e0320f0SAjay Kumar Gupta 			 * continue.  Except ... if there's a request for
17981e0320f0SAjay Kumar Gupta 			 * another QH, use that instead of starving it.
17991e0320f0SAjay Kumar Gupta 			 *
18001e0320f0SAjay Kumar Gupta 			 * Devices like Ethernet and serial adapters keep
18011e0320f0SAjay Kumar Gupta 			 * reads posted at all times, which will starve
18021e0320f0SAjay Kumar Gupta 			 * other devices without this logic.
18031e0320f0SAjay Kumar Gupta 			 */
18041e0320f0SAjay Kumar Gupta 			if (usb_pipebulk(urb->pipe)
18051e0320f0SAjay Kumar Gupta 					&& qh->mux == 1
18061e0320f0SAjay Kumar Gupta 					&& !list_is_singular(&musb->in_bulk)) {
1807f283862fSAjay Kumar Gupta 				musb_bulk_nak_timeout(musb, hw_ep, 1);
18081e0320f0SAjay Kumar Gupta 				return;
18091e0320f0SAjay Kumar Gupta 			}
1810550a7375SFelipe Balbi 			musb_ep_select(mbase, epnum);
18111e0320f0SAjay Kumar Gupta 			rx_csr |= MUSB_RXCSR_H_WZC_BITS;
18121e0320f0SAjay Kumar Gupta 			rx_csr &= ~MUSB_RXCSR_DATAERROR;
18131e0320f0SAjay Kumar Gupta 			musb_writew(epio, MUSB_RXCSR, rx_csr);
1814550a7375SFelipe Balbi 
1815550a7375SFelipe Balbi 			goto finish;
1816550a7375SFelipe Balbi 		} else {
1817b99d3659SBin Liu 			musb_dbg(musb, "RX end %d ISO data error", epnum);
1818550a7375SFelipe Balbi 			/* packet error reported later */
1819550a7375SFelipe Balbi 			iso_err = true;
1820550a7375SFelipe Balbi 		}
1821a483d706SAjay Kumar Gupta 	} else if (rx_csr & MUSB_RXCSR_INCOMPRX) {
1822b99d3659SBin Liu 		musb_dbg(musb, "end %d high bandwidth incomplete ISO packet RX",
1823a483d706SAjay Kumar Gupta 				epnum);
1824a483d706SAjay Kumar Gupta 		status = -EPROTO;
1825550a7375SFelipe Balbi 	}
1826550a7375SFelipe Balbi 
1827550a7375SFelipe Balbi 	/* faults abort the transfer */
1828550a7375SFelipe Balbi 	if (status) {
1829550a7375SFelipe Balbi 		/* clean up dma and collect transfer count */
1830550a7375SFelipe Balbi 		if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) {
1831550a7375SFelipe Balbi 			dma->status = MUSB_DMA_STATUS_CORE_ABORT;
18329c547699SDaniel Mack 			musb->dma_controller->channel_abort(dma);
1833550a7375SFelipe Balbi 			xfer_len = dma->actual_len;
1834550a7375SFelipe Balbi 		}
1835550a7375SFelipe Balbi 		musb_h_flush_rxfifo(hw_ep, MUSB_RXCSR_CLRDATATOG);
1836550a7375SFelipe Balbi 		musb_writeb(epio, MUSB_RXINTERVAL, 0);
1837550a7375SFelipe Balbi 		done = true;
1838550a7375SFelipe Balbi 		goto finish;
1839550a7375SFelipe Balbi 	}
1840550a7375SFelipe Balbi 
1841550a7375SFelipe Balbi 	if (unlikely(dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY)) {
1842550a7375SFelipe Balbi 		/* SHOULD NEVER HAPPEN ... but at least DaVinci has done it */
1843550a7375SFelipe Balbi 		ERR("RX%d dma busy, csr %04x\n", epnum, rx_csr);
1844550a7375SFelipe Balbi 		goto finish;
1845550a7375SFelipe Balbi 	}
1846550a7375SFelipe Balbi 
1847550a7375SFelipe Balbi 	/* thorough shutdown for now ... given more precise fault handling
1848550a7375SFelipe Balbi 	 * and better queueing support, we might keep a DMA pipeline going
1849550a7375SFelipe Balbi 	 * while processing this irq for earlier completions.
1850550a7375SFelipe Balbi 	 */
1851550a7375SFelipe Balbi 
1852550a7375SFelipe Balbi 	/* FIXME this is _way_ too much in-line logic for Mentor DMA */
1853557d543eSTony Lindgren 	if (!musb_dma_inventra(musb) && !musb_dma_ux500(musb) &&
1854557d543eSTony Lindgren 	    (rx_csr & MUSB_RXCSR_H_REQPKT)) {
1855550a7375SFelipe Balbi 		/* REVISIT this happened for a while on some short reads...
1856550a7375SFelipe Balbi 		 * the cleanup still needs investigation... looks bad...
1857550a7375SFelipe Balbi 		 * and also duplicates dma cleanup code above ... plus,
1858550a7375SFelipe Balbi 		 * shouldn't this be the "half full" double buffer case?
1859550a7375SFelipe Balbi 		 */
1860550a7375SFelipe Balbi 		if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) {
1861550a7375SFelipe Balbi 			dma->status = MUSB_DMA_STATUS_CORE_ABORT;
18629c547699SDaniel Mack 			musb->dma_controller->channel_abort(dma);
1863550a7375SFelipe Balbi 			xfer_len = dma->actual_len;
1864550a7375SFelipe Balbi 			done = true;
1865550a7375SFelipe Balbi 		}
1866550a7375SFelipe Balbi 
1867b99d3659SBin Liu 		musb_dbg(musb, "RXCSR%d %04x, reqpkt, len %zu%s", epnum, rx_csr,
1868550a7375SFelipe Balbi 				xfer_len, dma ? ", dma" : "");
1869550a7375SFelipe Balbi 		rx_csr &= ~MUSB_RXCSR_H_REQPKT;
1870550a7375SFelipe Balbi 
1871550a7375SFelipe Balbi 		musb_ep_select(mbase, epnum);
1872550a7375SFelipe Balbi 		musb_writew(epio, MUSB_RXCSR,
1873550a7375SFelipe Balbi 				MUSB_RXCSR_H_WZC_BITS | rx_csr);
1874550a7375SFelipe Balbi 	}
1875557d543eSTony Lindgren 
1876550a7375SFelipe Balbi 	if (dma && (rx_csr & MUSB_RXCSR_DMAENAB)) {
1877550a7375SFelipe Balbi 		xfer_len = dma->actual_len;
1878550a7375SFelipe Balbi 
1879550a7375SFelipe Balbi 		val &= ~(MUSB_RXCSR_DMAENAB
1880550a7375SFelipe Balbi 			| MUSB_RXCSR_H_AUTOREQ
1881550a7375SFelipe Balbi 			| MUSB_RXCSR_AUTOCLEAR
1882550a7375SFelipe Balbi 			| MUSB_RXCSR_RXPKTRDY);
1883550a7375SFelipe Balbi 		musb_writew(hw_ep->regs, MUSB_RXCSR, val);
1884550a7375SFelipe Balbi 
1885cff84bdbSTony Lindgren 		if (musb_dma_inventra(musb) || musb_dma_ux500(musb) ||
1886cff84bdbSTony Lindgren 		    musb_dma_cppi41(musb)) {
1887cff84bdbSTony Lindgren 			    done = musb_rx_dma_inventra_cppi41(c, hw_ep, qh, urb, xfer_len);
1888b99d3659SBin Liu 			    musb_dbg(hw_ep->musb,
1889b99d3659SBin Liu 				    "ep %d dma %s, rxcsr %04x, rxcount %d",
1890cff84bdbSTony Lindgren 				    epnum, done ? "off" : "reset",
1891550a7375SFelipe Balbi 				    musb_readw(epio, MUSB_RXCSR),
1892550a7375SFelipe Balbi 				    musb_readw(epio, MUSB_RXCOUNT));
1893cff84bdbSTony Lindgren 		} else {
1894550a7375SFelipe Balbi 			done = true;
1895cff84bdbSTony Lindgren 		}
1896cff84bdbSTony Lindgren 
1897550a7375SFelipe Balbi 	} else if (urb->status == -EINPROGRESS) {
1898550a7375SFelipe Balbi 		/* if no errors, be sure a packet is ready for unloading */
1899550a7375SFelipe Balbi 		if (unlikely(!(rx_csr & MUSB_RXCSR_RXPKTRDY))) {
1900550a7375SFelipe Balbi 			status = -EPROTO;
1901550a7375SFelipe Balbi 			ERR("Rx interrupt with no errors or packet!\n");
1902550a7375SFelipe Balbi 
1903550a7375SFelipe Balbi 			/* FIXME this is another "SHOULD NEVER HAPPEN" */
1904550a7375SFelipe Balbi 
1905550a7375SFelipe Balbi /* SCRUB (RX) */
1906550a7375SFelipe Balbi 			/* do the proper sequence to abort the transfer */
1907550a7375SFelipe Balbi 			musb_ep_select(mbase, epnum);
1908550a7375SFelipe Balbi 			val &= ~MUSB_RXCSR_H_REQPKT;
1909550a7375SFelipe Balbi 			musb_writew(epio, MUSB_RXCSR, val);
1910550a7375SFelipe Balbi 			goto finish;
1911550a7375SFelipe Balbi 		}
1912550a7375SFelipe Balbi 
1913550a7375SFelipe Balbi 		/* we are expecting IN packets */
1914e530bb8fSTony Lindgren 		if ((musb_dma_inventra(musb) || musb_dma_ux500(musb) ||
1915e530bb8fSTony Lindgren 		    musb_dma_cppi41(musb)) && dma) {
1916b99d3659SBin Liu 			musb_dbg(hw_ep->musb,
1917b99d3659SBin Liu 				"RX%d count %d, buffer 0x%llx len %d/%d",
1918ac33cdb1STony Lindgren 				epnum, musb_readw(epio, MUSB_RXCOUNT),
191991e3af64SFelipe Balbi 				(unsigned long long) urb->transfer_dma
1920550a7375SFelipe Balbi 				+ urb->actual_length,
1921550a7375SFelipe Balbi 				qh->offset,
1922550a7375SFelipe Balbi 				urb->transfer_buffer_length);
1923550a7375SFelipe Balbi 
19244c2ba0c6SCristian Birsan 			if (musb_rx_dma_in_inventra_cppi41(c, hw_ep, qh, urb,
19254c2ba0c6SCristian Birsan 							   xfer_len, iso_err))
1926ac33cdb1STony Lindgren 				goto finish;
1927550a7375SFelipe Balbi 			else
1928ac33cdb1STony Lindgren 				dev_err(musb->controller, "error: rx_dma failed\n");
1929550a7375SFelipe Balbi 		}
1930550a7375SFelipe Balbi 
1931550a7375SFelipe Balbi 		if (!dma) {
19328e8a5516SVirupax Sadashivpetimath 			unsigned int received_len;
19338e8a5516SVirupax Sadashivpetimath 
1934496dda70SMaulik Mankad 			/* Unmap the buffer so that CPU can use it */
19358b125df5SDaniel Mack 			usb_hcd_unmap_urb_for_dma(musb->hcd, urb);
19368e8a5516SVirupax Sadashivpetimath 
19378e8a5516SVirupax Sadashivpetimath 			/*
19388e8a5516SVirupax Sadashivpetimath 			 * We need to map sg if the transfer_buffer is
19398e8a5516SVirupax Sadashivpetimath 			 * NULL.
19408e8a5516SVirupax Sadashivpetimath 			 */
19418e8a5516SVirupax Sadashivpetimath 			if (!urb->transfer_buffer) {
1942ed74df12SVirupax Sadashivpetimath 				qh->use_sg = true;
19438e8a5516SVirupax Sadashivpetimath 				sg_miter_start(&qh->sg_miter, urb->sg, 1,
19448e8a5516SVirupax Sadashivpetimath 						sg_flags);
19458e8a5516SVirupax Sadashivpetimath 			}
19468e8a5516SVirupax Sadashivpetimath 
1947ed74df12SVirupax Sadashivpetimath 			if (qh->use_sg) {
19488e8a5516SVirupax Sadashivpetimath 				if (!sg_miter_next(&qh->sg_miter)) {
19498e8a5516SVirupax Sadashivpetimath 					dev_err(musb->controller, "error: sg list empty\n");
19508e8a5516SVirupax Sadashivpetimath 					sg_miter_stop(&qh->sg_miter);
19518e8a5516SVirupax Sadashivpetimath 					status = -EINVAL;
19528e8a5516SVirupax Sadashivpetimath 					done = true;
19538e8a5516SVirupax Sadashivpetimath 					goto finish;
19548e8a5516SVirupax Sadashivpetimath 				}
19558e8a5516SVirupax Sadashivpetimath 				urb->transfer_buffer = qh->sg_miter.addr;
19568e8a5516SVirupax Sadashivpetimath 				received_len = urb->actual_length;
19578e8a5516SVirupax Sadashivpetimath 				qh->offset = 0x0;
19588e8a5516SVirupax Sadashivpetimath 				done = musb_host_packet_rx(musb, urb, epnum,
19598e8a5516SVirupax Sadashivpetimath 						iso_err);
19608e8a5516SVirupax Sadashivpetimath 				/* Calculate the number of bytes received */
19618e8a5516SVirupax Sadashivpetimath 				received_len = urb->actual_length -
19628e8a5516SVirupax Sadashivpetimath 					received_len;
19638e8a5516SVirupax Sadashivpetimath 				qh->sg_miter.consumed = received_len;
19648e8a5516SVirupax Sadashivpetimath 				sg_miter_stop(&qh->sg_miter);
19658e8a5516SVirupax Sadashivpetimath 			} else {
1966550a7375SFelipe Balbi 				done = musb_host_packet_rx(musb, urb,
1967550a7375SFelipe Balbi 						epnum, iso_err);
19688e8a5516SVirupax Sadashivpetimath 			}
1969b99d3659SBin Liu 			musb_dbg(musb, "read %spacket", done ? "last " : "");
1970550a7375SFelipe Balbi 		}
1971550a7375SFelipe Balbi 	}
1972550a7375SFelipe Balbi 
1973550a7375SFelipe Balbi finish:
1974550a7375SFelipe Balbi 	urb->actual_length += xfer_len;
1975550a7375SFelipe Balbi 	qh->offset += xfer_len;
1976550a7375SFelipe Balbi 	if (done) {
197752974d94SMans Rullgard 		if (qh->use_sg) {
1978ed74df12SVirupax Sadashivpetimath 			qh->use_sg = false;
197952974d94SMans Rullgard 			urb->transfer_buffer = NULL;
198052974d94SMans Rullgard 		}
19818e8a5516SVirupax Sadashivpetimath 
1982550a7375SFelipe Balbi 		if (urb->status == -EINPROGRESS)
1983550a7375SFelipe Balbi 			urb->status = status;
1984550a7375SFelipe Balbi 		musb_advance_schedule(musb, urb, hw_ep, USB_DIR_IN);
1985550a7375SFelipe Balbi 	}
1986550a7375SFelipe Balbi }
1987550a7375SFelipe Balbi 
1988550a7375SFelipe Balbi /* schedule nodes correspond to peripheral endpoints, like an OHCI QH.
1989550a7375SFelipe Balbi  * the software schedule associates multiple such nodes with a given
1990550a7375SFelipe Balbi  * host side hardware endpoint + direction; scheduling may activate
1991550a7375SFelipe Balbi  * that hardware endpoint.
1992550a7375SFelipe Balbi  */
musb_schedule(struct musb * musb,struct musb_qh * qh,int is_in)1993550a7375SFelipe Balbi static int musb_schedule(
1994550a7375SFelipe Balbi 	struct musb		*musb,
1995550a7375SFelipe Balbi 	struct musb_qh		*qh,
1996550a7375SFelipe Balbi 	int			is_in)
1997550a7375SFelipe Balbi {
1998eac44dc4SRickard Strandqvist 	int			idle = 0;
1999550a7375SFelipe Balbi 	int			best_diff;
2000550a7375SFelipe Balbi 	int			best_end, epnum;
2001550a7375SFelipe Balbi 	struct musb_hw_ep	*hw_ep = NULL;
2002550a7375SFelipe Balbi 	struct list_head	*head = NULL;
20035274dab6SSwaminathan S 	u8			toggle;
20045274dab6SSwaminathan S 	u8			txtype;
20055274dab6SSwaminathan S 	struct urb		*urb = next_urb(qh);
2006550a7375SFelipe Balbi 
2007550a7375SFelipe Balbi 	/* use fixed hardware for control and bulk */
200823d15e07SAjay Kumar Gupta 	if (qh->type == USB_ENDPOINT_XFER_CONTROL) {
2009550a7375SFelipe Balbi 		head = &musb->control;
2010550a7375SFelipe Balbi 		hw_ep = musb->control_ep;
2011550a7375SFelipe Balbi 		goto success;
2012550a7375SFelipe Balbi 	}
2013550a7375SFelipe Balbi 
2014550a7375SFelipe Balbi 	/* else, periodic transfers get muxed to other endpoints */
2015550a7375SFelipe Balbi 
20165d67a851SSergei Shtylyov 	/*
20175d67a851SSergei Shtylyov 	 * We know this qh hasn't been scheduled, so all we need to do
2018550a7375SFelipe Balbi 	 * is choose which hardware endpoint to put it on ...
2019550a7375SFelipe Balbi 	 *
2020550a7375SFelipe Balbi 	 * REVISIT what we really want here is a regular schedule tree
20215d67a851SSergei Shtylyov 	 * like e.g. OHCI uses.
2022550a7375SFelipe Balbi 	 */
2023550a7375SFelipe Balbi 	best_diff = 4096;
2024550a7375SFelipe Balbi 	best_end = -1;
2025550a7375SFelipe Balbi 
20265d67a851SSergei Shtylyov 	for (epnum = 1, hw_ep = musb->endpoints + 1;
20275d67a851SSergei Shtylyov 			epnum < musb->nr_endpoints;
20285d67a851SSergei Shtylyov 			epnum++, hw_ep++) {
2029550a7375SFelipe Balbi 		int	diff;
2030550a7375SFelipe Balbi 
20313e5c6dc7SSergei Shtylyov 		if (musb_ep_get_qh(hw_ep, is_in) != NULL)
20325d67a851SSergei Shtylyov 			continue;
20335d67a851SSergei Shtylyov 
2034550a7375SFelipe Balbi 		if (hw_ep == musb->bulk_ep)
2035550a7375SFelipe Balbi 			continue;
2036550a7375SFelipe Balbi 
2037550a7375SFelipe Balbi 		if (is_in)
2038a483d706SAjay Kumar Gupta 			diff = hw_ep->max_packet_sz_rx;
2039550a7375SFelipe Balbi 		else
2040a483d706SAjay Kumar Gupta 			diff = hw_ep->max_packet_sz_tx;
2041a483d706SAjay Kumar Gupta 		diff -= (qh->maxpacket * qh->hb_mult);
2042550a7375SFelipe Balbi 
204323d15e07SAjay Kumar Gupta 		if (diff >= 0 && best_diff > diff) {
20445274dab6SSwaminathan S 
20455274dab6SSwaminathan S 			/*
20465274dab6SSwaminathan S 			 * Mentor controller has a bug in that if we schedule
20475274dab6SSwaminathan S 			 * a BULK Tx transfer on an endpoint that had earlier
20485274dab6SSwaminathan S 			 * handled ISOC then the BULK transfer has to start on
20495274dab6SSwaminathan S 			 * a zero toggle.  If the BULK transfer starts on a 1
20505274dab6SSwaminathan S 			 * toggle then this transfer will fail as the mentor
20515274dab6SSwaminathan S 			 * controller starts the Bulk transfer on a 0 toggle
20525274dab6SSwaminathan S 			 * irrespective of the programming of the toggle bits
20535274dab6SSwaminathan S 			 * in the TXCSR register.  Check for this condition
20545274dab6SSwaminathan S 			 * while allocating the EP for a Tx Bulk transfer.  If
20555274dab6SSwaminathan S 			 * so skip this EP.
20565274dab6SSwaminathan S 			 */
20575274dab6SSwaminathan S 			hw_ep = musb->endpoints + epnum;
20585274dab6SSwaminathan S 			toggle = usb_gettoggle(urb->dev, qh->epnum, !is_in);
20595274dab6SSwaminathan S 			txtype = (musb_readb(hw_ep->regs, MUSB_TXTYPE)
20605274dab6SSwaminathan S 					>> 4) & 0x3;
20615274dab6SSwaminathan S 			if (!is_in && (qh->type == USB_ENDPOINT_XFER_BULK) &&
20625274dab6SSwaminathan S 				toggle && (txtype == USB_ENDPOINT_XFER_ISOC))
20635274dab6SSwaminathan S 				continue;
20645274dab6SSwaminathan S 
2065550a7375SFelipe Balbi 			best_diff = diff;
2066550a7375SFelipe Balbi 			best_end = epnum;
2067550a7375SFelipe Balbi 		}
2068550a7375SFelipe Balbi 	}
206923d15e07SAjay Kumar Gupta 	/* use bulk reserved ep1 if no other ep is free */
2070aa5cbbecSFelipe Balbi 	if (best_end < 0 && qh->type == USB_ENDPOINT_XFER_BULK) {
207123d15e07SAjay Kumar Gupta 		hw_ep = musb->bulk_ep;
207223d15e07SAjay Kumar Gupta 		if (is_in)
207323d15e07SAjay Kumar Gupta 			head = &musb->in_bulk;
207423d15e07SAjay Kumar Gupta 		else
207523d15e07SAjay Kumar Gupta 			head = &musb->out_bulk;
20761e0320f0SAjay Kumar Gupta 
2077f283862fSAjay Kumar Gupta 		/* Enable bulk RX/TX NAK timeout scheme when bulk requests are
20785ae477b0SRahul Bedarkar 		 * multiplexed. This scheme does not work in high speed to full
20791e0320f0SAjay Kumar Gupta 		 * speed scenario as NAK interrupts are not coming from a
20801e0320f0SAjay Kumar Gupta 		 * full speed device connected to a high speed device.
20811e0320f0SAjay Kumar Gupta 		 * NAK timeout interval is 8 (128 uframe or 16ms) for HS and
20821e0320f0SAjay Kumar Gupta 		 * 4 (8 frame or 8ms) for FS device.
20831e0320f0SAjay Kumar Gupta 		 */
2084f283862fSAjay Kumar Gupta 		if (qh->dev)
20851e0320f0SAjay Kumar Gupta 			qh->intv_reg =
20861e0320f0SAjay Kumar Gupta 				(USB_SPEED_HIGH == qh->dev->speed) ? 8 : 4;
208723d15e07SAjay Kumar Gupta 		goto success;
208823d15e07SAjay Kumar Gupta 	} else if (best_end < 0) {
2089a2f65606SBin Liu 		dev_err(musb->controller,
2090a2f65606SBin Liu 				"%s hwep alloc failed for %dx%d\n",
2091a2f65606SBin Liu 				musb_ep_xfertype_string(qh->type),
2092a2f65606SBin Liu 				qh->hb_mult, qh->maxpacket);
2093550a7375SFelipe Balbi 		return -ENOSPC;
209423d15e07SAjay Kumar Gupta 	}
2095550a7375SFelipe Balbi 
2096550a7375SFelipe Balbi 	idle = 1;
209723d15e07SAjay Kumar Gupta 	qh->mux = 0;
2098550a7375SFelipe Balbi 	hw_ep = musb->endpoints + best_end;
2099b99d3659SBin Liu 	musb_dbg(musb, "qh %p periodic slot %d", qh, best_end);
2100550a7375SFelipe Balbi success:
210123d15e07SAjay Kumar Gupta 	if (head) {
210223d15e07SAjay Kumar Gupta 		idle = list_empty(head);
210323d15e07SAjay Kumar Gupta 		list_add_tail(&qh->ring, head);
210423d15e07SAjay Kumar Gupta 		qh->mux = 1;
210523d15e07SAjay Kumar Gupta 	}
2106550a7375SFelipe Balbi 	qh->hw_ep = hw_ep;
2107550a7375SFelipe Balbi 	qh->hep->hcpriv = qh;
2108550a7375SFelipe Balbi 	if (idle)
2109550a7375SFelipe Balbi 		musb_start_urb(musb, is_in, qh);
2110550a7375SFelipe Balbi 	return 0;
2111550a7375SFelipe Balbi }
2112550a7375SFelipe Balbi 
musb_urb_enqueue(struct usb_hcd * hcd,struct urb * urb,gfp_t mem_flags)2113550a7375SFelipe Balbi static int musb_urb_enqueue(
2114550a7375SFelipe Balbi 	struct usb_hcd			*hcd,
2115550a7375SFelipe Balbi 	struct urb			*urb,
2116550a7375SFelipe Balbi 	gfp_t				mem_flags)
2117550a7375SFelipe Balbi {
2118550a7375SFelipe Balbi 	unsigned long			flags;
2119550a7375SFelipe Balbi 	struct musb			*musb = hcd_to_musb(hcd);
2120550a7375SFelipe Balbi 	struct usb_host_endpoint	*hep = urb->ep;
212174bb3508SDavid Brownell 	struct musb_qh			*qh;
2122550a7375SFelipe Balbi 	struct usb_endpoint_descriptor	*epd = &hep->desc;
2123550a7375SFelipe Balbi 	int				ret;
2124550a7375SFelipe Balbi 	unsigned			type_reg;
2125550a7375SFelipe Balbi 	unsigned			interval;
2126550a7375SFelipe Balbi 
2127550a7375SFelipe Balbi 	/* host role must be active */
2128550a7375SFelipe Balbi 	if (!is_host_active(musb) || !musb->is_active)
2129550a7375SFelipe Balbi 		return -ENODEV;
2130550a7375SFelipe Balbi 
213119ca682eSBin Liu 	trace_musb_urb_enq(musb, urb);
213219ca682eSBin Liu 
2133550a7375SFelipe Balbi 	spin_lock_irqsave(&musb->lock, flags);
2134550a7375SFelipe Balbi 	ret = usb_hcd_link_urb_to_ep(hcd, urb);
213574bb3508SDavid Brownell 	qh = ret ? NULL : hep->hcpriv;
213674bb3508SDavid Brownell 	if (qh)
213774bb3508SDavid Brownell 		urb->hcpriv = qh;
2138550a7375SFelipe Balbi 	spin_unlock_irqrestore(&musb->lock, flags);
2139550a7375SFelipe Balbi 
2140550a7375SFelipe Balbi 	/* DMA mapping was already done, if needed, and this urb is on
214174bb3508SDavid Brownell 	 * hep->urb_list now ... so we're done, unless hep wasn't yet
214274bb3508SDavid Brownell 	 * scheduled onto a live qh.
2143550a7375SFelipe Balbi 	 *
2144550a7375SFelipe Balbi 	 * REVISIT best to keep hep->hcpriv valid until the endpoint gets
2145550a7375SFelipe Balbi 	 * disabled, testing for empty qh->ring and avoiding qh setup costs
2146550a7375SFelipe Balbi 	 * except for the first urb queued after a config change.
2147550a7375SFelipe Balbi 	 */
214874bb3508SDavid Brownell 	if (qh || ret)
214974bb3508SDavid Brownell 		return ret;
2150550a7375SFelipe Balbi 
2151550a7375SFelipe Balbi 	/* Allocate and initialize qh, minimizing the work done each time
2152550a7375SFelipe Balbi 	 * hw_ep gets reprogrammed, or with irqs blocked.  Then schedule it.
2153550a7375SFelipe Balbi 	 *
2154550a7375SFelipe Balbi 	 * REVISIT consider a dedicated qh kmem_cache, so it's harder
2155550a7375SFelipe Balbi 	 * for bugs in other kernel code to break this driver...
2156550a7375SFelipe Balbi 	 */
2157550a7375SFelipe Balbi 	qh = kzalloc(sizeof *qh, mem_flags);
2158550a7375SFelipe Balbi 	if (!qh) {
21592492e674SAjay Kumar Gupta 		spin_lock_irqsave(&musb->lock, flags);
2160550a7375SFelipe Balbi 		usb_hcd_unlink_urb_from_ep(hcd, urb);
21612492e674SAjay Kumar Gupta 		spin_unlock_irqrestore(&musb->lock, flags);
2162550a7375SFelipe Balbi 		return -ENOMEM;
2163550a7375SFelipe Balbi 	}
2164550a7375SFelipe Balbi 
2165550a7375SFelipe Balbi 	qh->hep = hep;
2166550a7375SFelipe Balbi 	qh->dev = urb->dev;
2167550a7375SFelipe Balbi 	INIT_LIST_HEAD(&qh->ring);
2168550a7375SFelipe Balbi 	qh->is_ready = 1;
2169550a7375SFelipe Balbi 
217029cc8897SKuninori Morimoto 	qh->maxpacket = usb_endpoint_maxp(epd);
2171a483d706SAjay Kumar Gupta 	qh->type = usb_endpoint_type(epd);
2172550a7375SFelipe Balbi 
2173a483d706SAjay Kumar Gupta 	/* Bits 11 & 12 of wMaxPacketSize encode high bandwidth multiplier.
2174a483d706SAjay Kumar Gupta 	 * Some musb cores don't support high bandwidth ISO transfers; and
2175a483d706SAjay Kumar Gupta 	 * we don't (yet!) support high bandwidth interrupt transfers.
2176a483d706SAjay Kumar Gupta 	 */
21776ddcabc2SFelipe Balbi 	qh->hb_mult = usb_endpoint_maxp_mult(epd);
2178a483d706SAjay Kumar Gupta 	if (qh->hb_mult > 1) {
2179a483d706SAjay Kumar Gupta 		int ok = (qh->type == USB_ENDPOINT_XFER_ISOC);
2180a483d706SAjay Kumar Gupta 
2181a483d706SAjay Kumar Gupta 		if (ok)
2182a483d706SAjay Kumar Gupta 			ok = (usb_pipein(urb->pipe) && musb->hb_iso_rx)
2183a483d706SAjay Kumar Gupta 				|| (usb_pipeout(urb->pipe) && musb->hb_iso_tx);
2184a483d706SAjay Kumar Gupta 		if (!ok) {
21851bff25eaSBin Liu 			dev_err(musb->controller,
21861bff25eaSBin Liu 				"high bandwidth %s (%dx%d) not supported\n",
21871bff25eaSBin Liu 				musb_ep_xfertype_string(qh->type),
21881bff25eaSBin Liu 				qh->hb_mult, qh->maxpacket & 0x7ff);
2189550a7375SFelipe Balbi 			ret = -EMSGSIZE;
2190550a7375SFelipe Balbi 			goto done;
2191550a7375SFelipe Balbi 		}
2192a483d706SAjay Kumar Gupta 		qh->maxpacket &= 0x7ff;
2193a483d706SAjay Kumar Gupta 	}
2194550a7375SFelipe Balbi 
219596bcd090SJulia Lawall 	qh->epnum = usb_endpoint_num(epd);
2196550a7375SFelipe Balbi 
2197550a7375SFelipe Balbi 	/* NOTE: urb->dev->devnum is wrong during SET_ADDRESS */
2198550a7375SFelipe Balbi 	qh->addr_reg = (u8) usb_pipedevice(urb->pipe);
2199550a7375SFelipe Balbi 
2200550a7375SFelipe Balbi 	/* precompute rxtype/txtype/type0 register */
2201550a7375SFelipe Balbi 	type_reg = (qh->type << 4) | qh->epnum;
2202550a7375SFelipe Balbi 	switch (urb->dev->speed) {
2203550a7375SFelipe Balbi 	case USB_SPEED_LOW:
2204550a7375SFelipe Balbi 		type_reg |= 0xc0;
2205550a7375SFelipe Balbi 		break;
2206550a7375SFelipe Balbi 	case USB_SPEED_FULL:
2207550a7375SFelipe Balbi 		type_reg |= 0x80;
2208550a7375SFelipe Balbi 		break;
2209550a7375SFelipe Balbi 	default:
2210550a7375SFelipe Balbi 		type_reg |= 0x40;
2211550a7375SFelipe Balbi 	}
2212550a7375SFelipe Balbi 	qh->type_reg = type_reg;
2213550a7375SFelipe Balbi 
2214136733d6SSergei Shtylyov 	/* Precompute RXINTERVAL/TXINTERVAL register */
2215550a7375SFelipe Balbi 	switch (qh->type) {
2216550a7375SFelipe Balbi 	case USB_ENDPOINT_XFER_INT:
2217136733d6SSergei Shtylyov 		/*
2218136733d6SSergei Shtylyov 		 * Full/low speeds use the  linear encoding,
2219136733d6SSergei Shtylyov 		 * high speed uses the logarithmic encoding.
2220136733d6SSergei Shtylyov 		 */
2221136733d6SSergei Shtylyov 		if (urb->dev->speed <= USB_SPEED_FULL) {
2222136733d6SSergei Shtylyov 			interval = max_t(u8, epd->bInterval, 1);
2223136733d6SSergei Shtylyov 			break;
2224550a7375SFelipe Balbi 		}
2225df561f66SGustavo A. R. Silva 		fallthrough;
2226550a7375SFelipe Balbi 	case USB_ENDPOINT_XFER_ISOC:
2227136733d6SSergei Shtylyov 		/* ISO always uses logarithmic encoding */
2228136733d6SSergei Shtylyov 		interval = min_t(u8, epd->bInterval, 16);
2229550a7375SFelipe Balbi 		break;
2230550a7375SFelipe Balbi 	default:
2231550a7375SFelipe Balbi 		/* REVISIT we actually want to use NAK limits, hinting to the
2232550a7375SFelipe Balbi 		 * transfer scheduling logic to try some other qh, e.g. try
2233550a7375SFelipe Balbi 		 * for 2 msec first:
2234550a7375SFelipe Balbi 		 *
2235550a7375SFelipe Balbi 		 * interval = (USB_SPEED_HIGH == urb->dev->speed) ? 16 : 2;
2236550a7375SFelipe Balbi 		 *
2237550a7375SFelipe Balbi 		 * The downside of disabling this is that transfer scheduling
2238550a7375SFelipe Balbi 		 * gets VERY unfair for nonperiodic transfers; a misbehaving
22391e0320f0SAjay Kumar Gupta 		 * peripheral could make that hurt.  That's perfectly normal
22401e0320f0SAjay Kumar Gupta 		 * for reads from network or serial adapters ... so we have
22411e0320f0SAjay Kumar Gupta 		 * partial NAKlimit support for bulk RX.
2242550a7375SFelipe Balbi 		 *
22431e0320f0SAjay Kumar Gupta 		 * The upside of disabling it is simpler transfer scheduling.
2244550a7375SFelipe Balbi 		 */
2245550a7375SFelipe Balbi 		interval = 0;
2246550a7375SFelipe Balbi 	}
2247550a7375SFelipe Balbi 	qh->intv_reg = interval;
2248550a7375SFelipe Balbi 
2249550a7375SFelipe Balbi 	/* precompute addressing for external hub/tt ports */
2250550a7375SFelipe Balbi 	if (musb->is_multipoint) {
2251550a7375SFelipe Balbi 		struct usb_device	*parent = urb->dev->parent;
2252550a7375SFelipe Balbi 
2253550a7375SFelipe Balbi 		if (parent != hcd->self.root_hub) {
2254550a7375SFelipe Balbi 			qh->h_addr_reg = (u8) parent->devnum;
2255550a7375SFelipe Balbi 
2256550a7375SFelipe Balbi 			/* set up tt info if needed */
2257550a7375SFelipe Balbi 			if (urb->dev->tt) {
2258550a7375SFelipe Balbi 				qh->h_port_reg = (u8) urb->dev->ttport;
2259ae5ad296SAjay Kumar Gupta 				if (urb->dev->tt->hub)
2260ae5ad296SAjay Kumar Gupta 					qh->h_addr_reg =
2261ae5ad296SAjay Kumar Gupta 						(u8) urb->dev->tt->hub->devnum;
2262ae5ad296SAjay Kumar Gupta 				if (urb->dev->tt->multi)
2263550a7375SFelipe Balbi 					qh->h_addr_reg |= 0x80;
2264550a7375SFelipe Balbi 			}
2265550a7375SFelipe Balbi 		}
2266550a7375SFelipe Balbi 	}
2267550a7375SFelipe Balbi 
2268550a7375SFelipe Balbi 	/* invariant: hep->hcpriv is null OR the qh that's already scheduled.
2269550a7375SFelipe Balbi 	 * until we get real dma queues (with an entry for each urb/buffer),
2270550a7375SFelipe Balbi 	 * we only have work to do in the former case.
2271550a7375SFelipe Balbi 	 */
2272550a7375SFelipe Balbi 	spin_lock_irqsave(&musb->lock, flags);
22733067779bSyuzheng ma 	if (hep->hcpriv || !next_urb(qh)) {
2274550a7375SFelipe Balbi 		/* some concurrent activity submitted another urb to hep...
2275550a7375SFelipe Balbi 		 * odd, rare, error prone, but legal.
2276550a7375SFelipe Balbi 		 */
2277550a7375SFelipe Balbi 		kfree(qh);
2278714bc5efSDan Carpenter 		qh = NULL;
2279550a7375SFelipe Balbi 		ret = 0;
2280550a7375SFelipe Balbi 	} else
2281550a7375SFelipe Balbi 		ret = musb_schedule(musb, qh,
2282550a7375SFelipe Balbi 				epd->bEndpointAddress & USB_ENDPOINT_DIR_MASK);
2283550a7375SFelipe Balbi 
2284550a7375SFelipe Balbi 	if (ret == 0) {
2285550a7375SFelipe Balbi 		urb->hcpriv = qh;
2286550a7375SFelipe Balbi 		/* FIXME set urb->start_frame for iso/intr, it's tested in
2287550a7375SFelipe Balbi 		 * musb_start_urb(), but otherwise only konicawc cares ...
2288550a7375SFelipe Balbi 		 */
2289550a7375SFelipe Balbi 	}
2290550a7375SFelipe Balbi 	spin_unlock_irqrestore(&musb->lock, flags);
2291550a7375SFelipe Balbi 
2292550a7375SFelipe Balbi done:
2293550a7375SFelipe Balbi 	if (ret != 0) {
22942492e674SAjay Kumar Gupta 		spin_lock_irqsave(&musb->lock, flags);
2295550a7375SFelipe Balbi 		usb_hcd_unlink_urb_from_ep(hcd, urb);
22962492e674SAjay Kumar Gupta 		spin_unlock_irqrestore(&musb->lock, flags);
2297550a7375SFelipe Balbi 		kfree(qh);
2298550a7375SFelipe Balbi 	}
2299550a7375SFelipe Balbi 	return ret;
2300550a7375SFelipe Balbi }
2301550a7375SFelipe Balbi 
2302550a7375SFelipe Balbi 
2303550a7375SFelipe Balbi /*
2304550a7375SFelipe Balbi  * abort a transfer that's at the head of a hardware queue.
2305550a7375SFelipe Balbi  * called with controller locked, irqs blocked
2306550a7375SFelipe Balbi  * that hardware queue advances to the next transfer, unless prevented
2307550a7375SFelipe Balbi  */
musb_cleanup_urb(struct urb * urb,struct musb_qh * qh)230881ec4e4aSSergei Shtylyov static int musb_cleanup_urb(struct urb *urb, struct musb_qh *qh)
2309550a7375SFelipe Balbi {
2310550a7375SFelipe Balbi 	struct musb_hw_ep	*ep = qh->hw_ep;
23115c8a86e1SFelipe Balbi 	struct musb		*musb = ep->musb;
2312550a7375SFelipe Balbi 	void __iomem		*epio = ep->regs;
2313550a7375SFelipe Balbi 	unsigned		hw_end = ep->epnum;
2314550a7375SFelipe Balbi 	void __iomem		*regs = ep->musb->mregs;
231581ec4e4aSSergei Shtylyov 	int			is_in = usb_pipein(urb->pipe);
2316550a7375SFelipe Balbi 	int			status = 0;
231781ec4e4aSSergei Shtylyov 	u16			csr;
23186def85a3SBin Liu 	struct dma_channel	*dma = NULL;
2319550a7375SFelipe Balbi 
2320550a7375SFelipe Balbi 	musb_ep_select(regs, hw_end);
2321550a7375SFelipe Balbi 
2322550a7375SFelipe Balbi 	if (is_dma_capable()) {
2323550a7375SFelipe Balbi 		dma = is_in ? ep->rx_channel : ep->tx_channel;
2324550a7375SFelipe Balbi 		if (dma) {
2325550a7375SFelipe Balbi 			status = ep->musb->dma_controller->channel_abort(dma);
2326b99d3659SBin Liu 			musb_dbg(musb, "abort %cX%d DMA for urb %p --> %d",
2327550a7375SFelipe Balbi 				is_in ? 'R' : 'T', ep->epnum,
2328550a7375SFelipe Balbi 				urb, status);
2329550a7375SFelipe Balbi 			urb->actual_length += dma->actual_len;
2330550a7375SFelipe Balbi 		}
2331550a7375SFelipe Balbi 	}
2332550a7375SFelipe Balbi 
2333550a7375SFelipe Balbi 	/* turn off DMA requests, discard state, stop polling ... */
2334692933b2SAjay Kumar Gupta 	if (ep->epnum && is_in) {
2335550a7375SFelipe Balbi 		/* giveback saves bulk toggle */
2336550a7375SFelipe Balbi 		csr = musb_h_flush_rxfifo(ep, 0);
2337550a7375SFelipe Balbi 
23386def85a3SBin Liu 		/* clear the endpoint's irq status here to avoid bogus irqs */
23396def85a3SBin Liu 		if (is_dma_capable() && dma)
23406def85a3SBin Liu 			musb_platform_clear_ep_rxintr(musb, ep->epnum);
234178322c1aSDavid Brownell 	} else if (ep->epnum) {
2342550a7375SFelipe Balbi 		musb_h_tx_flush_fifo(ep);
2343550a7375SFelipe Balbi 		csr = musb_readw(epio, MUSB_TXCSR);
2344550a7375SFelipe Balbi 		csr &= ~(MUSB_TXCSR_AUTOSET
2345550a7375SFelipe Balbi 			| MUSB_TXCSR_DMAENAB
2346550a7375SFelipe Balbi 			| MUSB_TXCSR_H_RXSTALL
2347550a7375SFelipe Balbi 			| MUSB_TXCSR_H_NAKTIMEOUT
2348550a7375SFelipe Balbi 			| MUSB_TXCSR_H_ERROR
2349550a7375SFelipe Balbi 			| MUSB_TXCSR_TXPKTRDY);
2350550a7375SFelipe Balbi 		musb_writew(epio, MUSB_TXCSR, csr);
2351550a7375SFelipe Balbi 		/* REVISIT may need to clear FLUSHFIFO ... */
2352550a7375SFelipe Balbi 		musb_writew(epio, MUSB_TXCSR, csr);
2353550a7375SFelipe Balbi 		/* flush cpu writebuffer */
2354550a7375SFelipe Balbi 		csr = musb_readw(epio, MUSB_TXCSR);
235578322c1aSDavid Brownell 	} else  {
235678322c1aSDavid Brownell 		musb_h_ep0_flush_fifo(ep);
2357550a7375SFelipe Balbi 	}
2358550a7375SFelipe Balbi 	if (status == 0)
2359550a7375SFelipe Balbi 		musb_advance_schedule(ep->musb, urb, ep, is_in);
2360550a7375SFelipe Balbi 	return status;
2361550a7375SFelipe Balbi }
2362550a7375SFelipe Balbi 
musb_urb_dequeue(struct usb_hcd * hcd,struct urb * urb,int status)2363550a7375SFelipe Balbi static int musb_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
2364550a7375SFelipe Balbi {
2365550a7375SFelipe Balbi 	struct musb		*musb = hcd_to_musb(hcd);
2366550a7375SFelipe Balbi 	struct musb_qh		*qh;
2367550a7375SFelipe Balbi 	unsigned long		flags;
236822a0d6f1SSergei Shtylyov 	int			is_in  = usb_pipein(urb->pipe);
2369550a7375SFelipe Balbi 	int			ret;
2370550a7375SFelipe Balbi 
237119ca682eSBin Liu 	trace_musb_urb_deq(musb, urb);
2372550a7375SFelipe Balbi 
2373550a7375SFelipe Balbi 	spin_lock_irqsave(&musb->lock, flags);
2374550a7375SFelipe Balbi 	ret = usb_hcd_check_unlink_urb(hcd, urb, status);
2375550a7375SFelipe Balbi 	if (ret)
2376550a7375SFelipe Balbi 		goto done;
2377550a7375SFelipe Balbi 
2378550a7375SFelipe Balbi 	qh = urb->hcpriv;
2379550a7375SFelipe Balbi 	if (!qh)
2380550a7375SFelipe Balbi 		goto done;
2381550a7375SFelipe Balbi 
238222a0d6f1SSergei Shtylyov 	/*
238322a0d6f1SSergei Shtylyov 	 * Any URB not actively programmed into endpoint hardware can be
2384a2fd814eSSergei Shtylyov 	 * immediately given back; that's any URB not at the head of an
2385550a7375SFelipe Balbi 	 * endpoint queue, unless someday we get real DMA queues.  And even
2386a2fd814eSSergei Shtylyov 	 * if it's at the head, it might not be known to the hardware...
2387550a7375SFelipe Balbi 	 *
238822a0d6f1SSergei Shtylyov 	 * Otherwise abort current transfer, pending DMA, etc.; urb->status
2389550a7375SFelipe Balbi 	 * has already been updated.  This is a synchronous abort; it'd be
2390550a7375SFelipe Balbi 	 * OK to hold off until after some IRQ, though.
239122a0d6f1SSergei Shtylyov 	 *
239222a0d6f1SSergei Shtylyov 	 * NOTE: qh is invalid unless !list_empty(&hep->urb_list)
2393550a7375SFelipe Balbi 	 */
239422a0d6f1SSergei Shtylyov 	if (!qh->is_ready
239522a0d6f1SSergei Shtylyov 			|| urb->urb_list.prev != &qh->hep->urb_list
239622a0d6f1SSergei Shtylyov 			|| musb_ep_get_qh(qh->hw_ep, is_in) != qh) {
2397550a7375SFelipe Balbi 		int	ready = qh->is_ready;
2398550a7375SFelipe Balbi 
2399550a7375SFelipe Balbi 		qh->is_ready = 0;
2400c9cd06b3SSergei Shtylyov 		musb_giveback(musb, urb, 0);
2401550a7375SFelipe Balbi 		qh->is_ready = ready;
2402a2fd814eSSergei Shtylyov 
2403a2fd814eSSergei Shtylyov 		/* If nothing else (usually musb_giveback) is using it
2404a2fd814eSSergei Shtylyov 		 * and its URB list has emptied, recycle this qh.
2405a2fd814eSSergei Shtylyov 		 */
2406a2fd814eSSergei Shtylyov 		if (ready && list_empty(&qh->hep->urb_list)) {
2407*33d7e372SXingxing Luo 			musb_ep_set_qh(qh->hw_ep, is_in, NULL);
2408a2fd814eSSergei Shtylyov 			qh->hep->hcpriv = NULL;
2409a2fd814eSSergei Shtylyov 			list_del(&qh->ring);
2410a2fd814eSSergei Shtylyov 			kfree(qh);
2411a2fd814eSSergei Shtylyov 		}
2412550a7375SFelipe Balbi 	} else
241381ec4e4aSSergei Shtylyov 		ret = musb_cleanup_urb(urb, qh);
2414550a7375SFelipe Balbi done:
2415550a7375SFelipe Balbi 	spin_unlock_irqrestore(&musb->lock, flags);
2416550a7375SFelipe Balbi 	return ret;
2417550a7375SFelipe Balbi }
2418550a7375SFelipe Balbi 
2419550a7375SFelipe Balbi /* disable an endpoint */
2420550a7375SFelipe Balbi static void
musb_h_disable(struct usb_hcd * hcd,struct usb_host_endpoint * hep)2421550a7375SFelipe Balbi musb_h_disable(struct usb_hcd *hcd, struct usb_host_endpoint *hep)
2422550a7375SFelipe Balbi {
242322a0d6f1SSergei Shtylyov 	u8			is_in = hep->desc.bEndpointAddress & USB_DIR_IN;
2424550a7375SFelipe Balbi 	unsigned long		flags;
2425550a7375SFelipe Balbi 	struct musb		*musb = hcd_to_musb(hcd);
2426dc61d238SSergei Shtylyov 	struct musb_qh		*qh;
2427dc61d238SSergei Shtylyov 	struct urb		*urb;
2428550a7375SFelipe Balbi 
2429550a7375SFelipe Balbi 	spin_lock_irqsave(&musb->lock, flags);
2430550a7375SFelipe Balbi 
2431dc61d238SSergei Shtylyov 	qh = hep->hcpriv;
2432dc61d238SSergei Shtylyov 	if (qh == NULL)
2433dc61d238SSergei Shtylyov 		goto exit;
2434dc61d238SSergei Shtylyov 
2435550a7375SFelipe Balbi 	/* NOTE: qh is invalid unless !list_empty(&hep->urb_list) */
2436550a7375SFelipe Balbi 
243722a0d6f1SSergei Shtylyov 	/* Kick the first URB off the hardware, if needed */
2438550a7375SFelipe Balbi 	qh->is_ready = 0;
243922a0d6f1SSergei Shtylyov 	if (musb_ep_get_qh(qh->hw_ep, is_in) == qh) {
2440550a7375SFelipe Balbi 		urb = next_urb(qh);
2441550a7375SFelipe Balbi 
2442550a7375SFelipe Balbi 		/* make software (then hardware) stop ASAP */
2443550a7375SFelipe Balbi 		if (!urb->unlinked)
2444550a7375SFelipe Balbi 			urb->status = -ESHUTDOWN;
2445550a7375SFelipe Balbi 
2446550a7375SFelipe Balbi 		/* cleanup */
244781ec4e4aSSergei Shtylyov 		musb_cleanup_urb(urb, qh);
2448550a7375SFelipe Balbi 
2449dc61d238SSergei Shtylyov 		/* Then nuke all the others ... and advance the
2450dc61d238SSergei Shtylyov 		 * queue on hw_ep (e.g. bulk ring) when we're done.
2451dc61d238SSergei Shtylyov 		 */
2452dc61d238SSergei Shtylyov 		while (!list_empty(&hep->urb_list)) {
2453dc61d238SSergei Shtylyov 			urb = next_urb(qh);
2454dc61d238SSergei Shtylyov 			urb->status = -ESHUTDOWN;
2455dc61d238SSergei Shtylyov 			musb_advance_schedule(musb, urb, qh->hw_ep, is_in);
2456dc61d238SSergei Shtylyov 		}
2457dc61d238SSergei Shtylyov 	} else {
2458dc61d238SSergei Shtylyov 		/* Just empty the queue; the hardware is busy with
2459dc61d238SSergei Shtylyov 		 * other transfers, and since !qh->is_ready nothing
2460dc61d238SSergei Shtylyov 		 * will activate any of these as it advances.
2461dc61d238SSergei Shtylyov 		 */
2462dc61d238SSergei Shtylyov 		while (!list_empty(&hep->urb_list))
2463c9cd06b3SSergei Shtylyov 			musb_giveback(musb, next_urb(qh), -ESHUTDOWN);
2464550a7375SFelipe Balbi 
2465dc61d238SSergei Shtylyov 		hep->hcpriv = NULL;
2466dc61d238SSergei Shtylyov 		list_del(&qh->ring);
2467dc61d238SSergei Shtylyov 		kfree(qh);
2468dc61d238SSergei Shtylyov 	}
2469dc61d238SSergei Shtylyov exit:
2470550a7375SFelipe Balbi 	spin_unlock_irqrestore(&musb->lock, flags);
2471550a7375SFelipe Balbi }
2472550a7375SFelipe Balbi 
musb_h_get_frame_number(struct usb_hcd * hcd)2473550a7375SFelipe Balbi static int musb_h_get_frame_number(struct usb_hcd *hcd)
2474550a7375SFelipe Balbi {
2475550a7375SFelipe Balbi 	struct musb	*musb = hcd_to_musb(hcd);
2476550a7375SFelipe Balbi 
2477550a7375SFelipe Balbi 	return musb_readw(musb->mregs, MUSB_FRAME);
2478550a7375SFelipe Balbi }
2479550a7375SFelipe Balbi 
musb_h_start(struct usb_hcd * hcd)2480550a7375SFelipe Balbi static int musb_h_start(struct usb_hcd *hcd)
2481550a7375SFelipe Balbi {
2482550a7375SFelipe Balbi 	struct musb	*musb = hcd_to_musb(hcd);
2483550a7375SFelipe Balbi 
2484550a7375SFelipe Balbi 	/* NOTE: musb_start() is called when the hub driver turns
2485550a7375SFelipe Balbi 	 * on port power, or when (OTG) peripheral starts.
2486550a7375SFelipe Balbi 	 */
2487550a7375SFelipe Balbi 	hcd->state = HC_STATE_RUNNING;
2488550a7375SFelipe Balbi 	musb->port1_status = 0;
2489550a7375SFelipe Balbi 	return 0;
2490550a7375SFelipe Balbi }
2491550a7375SFelipe Balbi 
musb_h_stop(struct usb_hcd * hcd)2492550a7375SFelipe Balbi static void musb_h_stop(struct usb_hcd *hcd)
2493550a7375SFelipe Balbi {
2494550a7375SFelipe Balbi 	musb_stop(hcd_to_musb(hcd));
2495550a7375SFelipe Balbi 	hcd->state = HC_STATE_HALT;
2496550a7375SFelipe Balbi }
2497550a7375SFelipe Balbi 
musb_bus_suspend(struct usb_hcd * hcd)2498550a7375SFelipe Balbi static int musb_bus_suspend(struct usb_hcd *hcd)
2499550a7375SFelipe Balbi {
2500550a7375SFelipe Balbi 	struct musb	*musb = hcd_to_musb(hcd);
250189368d3dSDavid Brownell 	u8		devctl;
2502ebc3dd68SDaniel Glöckner 	int		ret;
2503550a7375SFelipe Balbi 
2504ebc3dd68SDaniel Glöckner 	ret = musb_port_suspend(musb, true);
2505ebc3dd68SDaniel Glöckner 	if (ret)
2506ebc3dd68SDaniel Glöckner 		return ret;
250794f72136SDaniel Mack 
250889368d3dSDavid Brownell 	if (!is_host_active(musb))
2509550a7375SFelipe Balbi 		return 0;
2510550a7375SFelipe Balbi 
251121acc656SPaul Cercueil 	switch (musb_get_state(musb)) {
251289368d3dSDavid Brownell 	case OTG_STATE_A_SUSPEND:
251389368d3dSDavid Brownell 		return 0;
251489368d3dSDavid Brownell 	case OTG_STATE_A_WAIT_VRISE:
251589368d3dSDavid Brownell 		/* ID could be grounded even if there's no device
251689368d3dSDavid Brownell 		 * on the other end of the cable.  NOTE that the
251789368d3dSDavid Brownell 		 * A_WAIT_VRISE timers are messy with MUSB...
251889368d3dSDavid Brownell 		 */
251989368d3dSDavid Brownell 		devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
252089368d3dSDavid Brownell 		if ((devctl & MUSB_DEVCTL_VBUS) == MUSB_DEVCTL_VBUS)
252121acc656SPaul Cercueil 			musb_set_state(musb, OTG_STATE_A_WAIT_BCON);
252289368d3dSDavid Brownell 		break;
252389368d3dSDavid Brownell 	default:
252489368d3dSDavid Brownell 		break;
252589368d3dSDavid Brownell 	}
252689368d3dSDavid Brownell 
252789368d3dSDavid Brownell 	if (musb->is_active) {
252889368d3dSDavid Brownell 		WARNING("trying to suspend as %s while active\n",
2529285f28bfSPaul Cercueil 			musb_otg_state_string(musb));
2530550a7375SFelipe Balbi 		return -EBUSY;
2531550a7375SFelipe Balbi 	} else
2532550a7375SFelipe Balbi 		return 0;
2533550a7375SFelipe Balbi }
2534550a7375SFelipe Balbi 
musb_bus_resume(struct usb_hcd * hcd)2535550a7375SFelipe Balbi static int musb_bus_resume(struct usb_hcd *hcd)
2536550a7375SFelipe Balbi {
2537869c5978SDaniel Mack 	struct musb *musb = hcd_to_musb(hcd);
2538869c5978SDaniel Mack 
2539869c5978SDaniel Mack 	if (musb->config &&
2540869c5978SDaniel Mack 	    musb->config->host_port_deassert_reset_at_resume)
2541869c5978SDaniel Mack 		musb_port_reset(musb, false);
2542869c5978SDaniel Mack 
2543550a7375SFelipe Balbi 	return 0;
2544550a7375SFelipe Balbi }
2545550a7375SFelipe Balbi 
25468408fd1dSRuslan Bilovol #ifndef CONFIG_MUSB_PIO_ONLY
25478408fd1dSRuslan Bilovol 
25488408fd1dSRuslan Bilovol #define MUSB_USB_DMA_ALIGN 4
25498408fd1dSRuslan Bilovol 
25508408fd1dSRuslan Bilovol struct musb_temp_buffer {
25518408fd1dSRuslan Bilovol 	void *kmalloc_ptr;
25528408fd1dSRuslan Bilovol 	void *old_xfer_buffer;
255321a37aedSGustavo A. R. Silva 	u8 data[];
25548408fd1dSRuslan Bilovol };
25558408fd1dSRuslan Bilovol 
musb_free_temp_buffer(struct urb * urb)25568408fd1dSRuslan Bilovol static void musb_free_temp_buffer(struct urb *urb)
25578408fd1dSRuslan Bilovol {
25588408fd1dSRuslan Bilovol 	enum dma_data_direction dir;
25598408fd1dSRuslan Bilovol 	struct musb_temp_buffer *temp;
2560d72348fbSJohan Hovold 	size_t length;
25618408fd1dSRuslan Bilovol 
25628408fd1dSRuslan Bilovol 	if (!(urb->transfer_flags & URB_ALIGNED_TEMP_BUFFER))
25638408fd1dSRuslan Bilovol 		return;
25648408fd1dSRuslan Bilovol 
25658408fd1dSRuslan Bilovol 	dir = usb_urb_dir_in(urb) ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
25668408fd1dSRuslan Bilovol 
25678408fd1dSRuslan Bilovol 	temp = container_of(urb->transfer_buffer, struct musb_temp_buffer,
25688408fd1dSRuslan Bilovol 			    data);
25698408fd1dSRuslan Bilovol 
25708408fd1dSRuslan Bilovol 	if (dir == DMA_FROM_DEVICE) {
2571d72348fbSJohan Hovold 		if (usb_pipeisoc(urb->pipe))
2572d72348fbSJohan Hovold 			length = urb->transfer_buffer_length;
2573d72348fbSJohan Hovold 		else
2574d72348fbSJohan Hovold 			length = urb->actual_length;
2575d72348fbSJohan Hovold 
2576d72348fbSJohan Hovold 		memcpy(temp->old_xfer_buffer, temp->data, length);
25778408fd1dSRuslan Bilovol 	}
25788408fd1dSRuslan Bilovol 	urb->transfer_buffer = temp->old_xfer_buffer;
25798408fd1dSRuslan Bilovol 	kfree(temp->kmalloc_ptr);
25808408fd1dSRuslan Bilovol 
25818408fd1dSRuslan Bilovol 	urb->transfer_flags &= ~URB_ALIGNED_TEMP_BUFFER;
25828408fd1dSRuslan Bilovol }
25838408fd1dSRuslan Bilovol 
musb_alloc_temp_buffer(struct urb * urb,gfp_t mem_flags)25848408fd1dSRuslan Bilovol static int musb_alloc_temp_buffer(struct urb *urb, gfp_t mem_flags)
25858408fd1dSRuslan Bilovol {
25868408fd1dSRuslan Bilovol 	enum dma_data_direction dir;
25878408fd1dSRuslan Bilovol 	struct musb_temp_buffer *temp;
25888408fd1dSRuslan Bilovol 	void *kmalloc_ptr;
25898408fd1dSRuslan Bilovol 	size_t kmalloc_size;
25908408fd1dSRuslan Bilovol 
25918408fd1dSRuslan Bilovol 	if (urb->num_sgs || urb->sg ||
25928408fd1dSRuslan Bilovol 	    urb->transfer_buffer_length == 0 ||
25938408fd1dSRuslan Bilovol 	    !((uintptr_t)urb->transfer_buffer & (MUSB_USB_DMA_ALIGN - 1)))
25948408fd1dSRuslan Bilovol 		return 0;
25958408fd1dSRuslan Bilovol 
25968408fd1dSRuslan Bilovol 	dir = usb_urb_dir_in(urb) ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
25978408fd1dSRuslan Bilovol 
25988408fd1dSRuslan Bilovol 	/* Allocate a buffer with enough padding for alignment */
25998408fd1dSRuslan Bilovol 	kmalloc_size = urb->transfer_buffer_length +
26008408fd1dSRuslan Bilovol 		sizeof(struct musb_temp_buffer) + MUSB_USB_DMA_ALIGN - 1;
26018408fd1dSRuslan Bilovol 
26028408fd1dSRuslan Bilovol 	kmalloc_ptr = kmalloc(kmalloc_size, mem_flags);
26038408fd1dSRuslan Bilovol 	if (!kmalloc_ptr)
26048408fd1dSRuslan Bilovol 		return -ENOMEM;
26058408fd1dSRuslan Bilovol 
26068408fd1dSRuslan Bilovol 	/* Position our struct temp_buffer such that data is aligned */
26078408fd1dSRuslan Bilovol 	temp = PTR_ALIGN(kmalloc_ptr, MUSB_USB_DMA_ALIGN);
26088408fd1dSRuslan Bilovol 
26098408fd1dSRuslan Bilovol 
26108408fd1dSRuslan Bilovol 	temp->kmalloc_ptr = kmalloc_ptr;
26118408fd1dSRuslan Bilovol 	temp->old_xfer_buffer = urb->transfer_buffer;
26128408fd1dSRuslan Bilovol 	if (dir == DMA_TO_DEVICE)
26138408fd1dSRuslan Bilovol 		memcpy(temp->data, urb->transfer_buffer,
26148408fd1dSRuslan Bilovol 		       urb->transfer_buffer_length);
26158408fd1dSRuslan Bilovol 	urb->transfer_buffer = temp->data;
26168408fd1dSRuslan Bilovol 
26178408fd1dSRuslan Bilovol 	urb->transfer_flags |= URB_ALIGNED_TEMP_BUFFER;
26188408fd1dSRuslan Bilovol 
26198408fd1dSRuslan Bilovol 	return 0;
26208408fd1dSRuslan Bilovol }
26218408fd1dSRuslan Bilovol 
musb_map_urb_for_dma(struct usb_hcd * hcd,struct urb * urb,gfp_t mem_flags)26228408fd1dSRuslan Bilovol static int musb_map_urb_for_dma(struct usb_hcd *hcd, struct urb *urb,
26238408fd1dSRuslan Bilovol 				      gfp_t mem_flags)
26248408fd1dSRuslan Bilovol {
26258408fd1dSRuslan Bilovol 	struct musb	*musb = hcd_to_musb(hcd);
26268408fd1dSRuslan Bilovol 	int ret;
26278408fd1dSRuslan Bilovol 
26288408fd1dSRuslan Bilovol 	/*
26298408fd1dSRuslan Bilovol 	 * The DMA engine in RTL1.8 and above cannot handle
26308408fd1dSRuslan Bilovol 	 * DMA addresses that are not aligned to a 4 byte boundary.
26318408fd1dSRuslan Bilovol 	 * For such engine implemented (un)map_urb_for_dma hooks.
26328408fd1dSRuslan Bilovol 	 * Do not use these hooks for RTL<1.8
26338408fd1dSRuslan Bilovol 	 */
26348408fd1dSRuslan Bilovol 	if (musb->hwvers < MUSB_HWVERS_1800)
26358408fd1dSRuslan Bilovol 		return usb_hcd_map_urb_for_dma(hcd, urb, mem_flags);
26368408fd1dSRuslan Bilovol 
26378408fd1dSRuslan Bilovol 	ret = musb_alloc_temp_buffer(urb, mem_flags);
26388408fd1dSRuslan Bilovol 	if (ret)
26398408fd1dSRuslan Bilovol 		return ret;
26408408fd1dSRuslan Bilovol 
26418408fd1dSRuslan Bilovol 	ret = usb_hcd_map_urb_for_dma(hcd, urb, mem_flags);
26428408fd1dSRuslan Bilovol 	if (ret)
26438408fd1dSRuslan Bilovol 		musb_free_temp_buffer(urb);
26448408fd1dSRuslan Bilovol 
26458408fd1dSRuslan Bilovol 	return ret;
26468408fd1dSRuslan Bilovol }
26478408fd1dSRuslan Bilovol 
musb_unmap_urb_for_dma(struct usb_hcd * hcd,struct urb * urb)26488408fd1dSRuslan Bilovol static void musb_unmap_urb_for_dma(struct usb_hcd *hcd, struct urb *urb)
26498408fd1dSRuslan Bilovol {
26508408fd1dSRuslan Bilovol 	struct musb	*musb = hcd_to_musb(hcd);
26518408fd1dSRuslan Bilovol 
26528408fd1dSRuslan Bilovol 	usb_hcd_unmap_urb_for_dma(hcd, urb);
26538408fd1dSRuslan Bilovol 
26548408fd1dSRuslan Bilovol 	/* Do not use this hook for RTL<1.8 (see description above) */
26558408fd1dSRuslan Bilovol 	if (musb->hwvers < MUSB_HWVERS_1800)
26568408fd1dSRuslan Bilovol 		return;
26578408fd1dSRuslan Bilovol 
26588408fd1dSRuslan Bilovol 	musb_free_temp_buffer(urb);
26598408fd1dSRuslan Bilovol }
26608408fd1dSRuslan Bilovol #endif /* !CONFIG_MUSB_PIO_ONLY */
26618408fd1dSRuslan Bilovol 
266274c2e936SDaniel Mack static const struct hc_driver musb_hc_driver = {
2663550a7375SFelipe Balbi 	.description		= "musb-hcd",
2664550a7375SFelipe Balbi 	.product_desc		= "MUSB HDRC host driver",
266574c2e936SDaniel Mack 	.hcd_priv_size		= sizeof(struct musb *),
26667b81cb6bSChristoph Hellwig 	.flags			= HCD_USB2 | HCD_DMA | HCD_MEMORY,
2667550a7375SFelipe Balbi 
2668550a7375SFelipe Balbi 	/* not using irq handler or reset hooks from usbcore, since
2669550a7375SFelipe Balbi 	 * those must be shared with peripheral code for OTG configs
2670550a7375SFelipe Balbi 	 */
2671550a7375SFelipe Balbi 
2672550a7375SFelipe Balbi 	.start			= musb_h_start,
2673550a7375SFelipe Balbi 	.stop			= musb_h_stop,
2674550a7375SFelipe Balbi 
2675550a7375SFelipe Balbi 	.get_frame_number	= musb_h_get_frame_number,
2676550a7375SFelipe Balbi 
2677550a7375SFelipe Balbi 	.urb_enqueue		= musb_urb_enqueue,
2678550a7375SFelipe Balbi 	.urb_dequeue		= musb_urb_dequeue,
2679550a7375SFelipe Balbi 	.endpoint_disable	= musb_h_disable,
2680550a7375SFelipe Balbi 
26818408fd1dSRuslan Bilovol #ifndef CONFIG_MUSB_PIO_ONLY
26828408fd1dSRuslan Bilovol 	.map_urb_for_dma	= musb_map_urb_for_dma,
26838408fd1dSRuslan Bilovol 	.unmap_urb_for_dma	= musb_unmap_urb_for_dma,
26848408fd1dSRuslan Bilovol #endif
26858408fd1dSRuslan Bilovol 
2686550a7375SFelipe Balbi 	.hub_status_data	= musb_hub_status_data,
2687550a7375SFelipe Balbi 	.hub_control		= musb_hub_control,
2688550a7375SFelipe Balbi 	.bus_suspend		= musb_bus_suspend,
2689550a7375SFelipe Balbi 	.bus_resume		= musb_bus_resume,
2690550a7375SFelipe Balbi 	/* .start_port_reset	= NULL, */
2691550a7375SFelipe Balbi 	/* .hub_irq_enable	= NULL, */
2692550a7375SFelipe Balbi };
26930b3eba44SDaniel Mack 
musb_host_alloc(struct musb * musb)269474c2e936SDaniel Mack int musb_host_alloc(struct musb *musb)
269574c2e936SDaniel Mack {
269674c2e936SDaniel Mack 	struct device	*dev = musb->controller;
269774c2e936SDaniel Mack 
269874c2e936SDaniel Mack 	/* usbcore sets dev->driver_data to hcd, and sometimes uses that... */
269974c2e936SDaniel Mack 	musb->hcd = usb_create_hcd(&musb_hc_driver, dev, dev_name(dev));
270074c2e936SDaniel Mack 	if (!musb->hcd)
270174c2e936SDaniel Mack 		return -EINVAL;
270274c2e936SDaniel Mack 
270374c2e936SDaniel Mack 	*musb->hcd->hcd_priv = (unsigned long) musb;
270474c2e936SDaniel Mack 	musb->hcd->self.uses_pio_for_control = 1;
270574c2e936SDaniel Mack 	musb->hcd->uses_new_polling = 1;
270674c2e936SDaniel Mack 	musb->hcd->has_tt = 1;
270774c2e936SDaniel Mack 
270874c2e936SDaniel Mack 	return 0;
270974c2e936SDaniel Mack }
271074c2e936SDaniel Mack 
musb_host_cleanup(struct musb * musb)271174c2e936SDaniel Mack void musb_host_cleanup(struct musb *musb)
271274c2e936SDaniel Mack {
27137ad76955SBin Liu 	if (musb->port_mode == MUSB_PERIPHERAL)
271490474288SSebastian Andrzej Siewior 		return;
271574c2e936SDaniel Mack 	usb_remove_hcd(musb->hcd);
271674c2e936SDaniel Mack }
271774c2e936SDaniel Mack 
musb_host_free(struct musb * musb)271874c2e936SDaniel Mack void musb_host_free(struct musb *musb)
271974c2e936SDaniel Mack {
272074c2e936SDaniel Mack 	usb_put_hcd(musb->hcd);
272174c2e936SDaniel Mack }
272274c2e936SDaniel Mack 
musb_host_setup(struct musb * musb,int power_budget)27232cc65feaSDaniel Mack int musb_host_setup(struct musb *musb, int power_budget)
27242cc65feaSDaniel Mack {
27252cc65feaSDaniel Mack 	int ret;
27262cc65feaSDaniel Mack 	struct usb_hcd *hcd = musb->hcd;
27272cc65feaSDaniel Mack 
27287ad76955SBin Liu 	if (musb->port_mode == MUSB_HOST) {
27292cc65feaSDaniel Mack 		MUSB_HST_MODE(musb);
273021acc656SPaul Cercueil 		musb_set_state(musb, OTG_STATE_A_IDLE);
27313c50ffefSTony Lindgren 	}
2732a6d45ea0SPaul Cercueil 
2733a6d45ea0SPaul Cercueil 	if (musb->xceiv) {
27342cc65feaSDaniel Mack 		otg_set_host(musb->xceiv->otg, &hcd->self);
2735a6d45ea0SPaul Cercueil 		musb->xceiv->otg->host = &hcd->self;
27360afddf1eSPaul Cercueil 	} else {
27370afddf1eSPaul Cercueil 		phy_set_mode(musb->phy, PHY_MODE_USB_HOST);
2738a6d45ea0SPaul Cercueil 	}
2739a6d45ea0SPaul Cercueil 
27400a9134bdSBin Liu 	/* don't support otg protocols */
27410a9134bdSBin Liu 	hcd->self.otg_port = 0;
27422cc65feaSDaniel Mack 	hcd->power_budget = 2 * (power_budget ? : 250);
27431f81f118SJohan Hovold 	hcd->skip_phy_initialization = 1;
27442cc65feaSDaniel Mack 
27452cc65feaSDaniel Mack 	ret = usb_add_hcd(hcd, 0, 0);
27462cc65feaSDaniel Mack 	if (ret < 0)
27472cc65feaSDaniel Mack 		return ret;
27482cc65feaSDaniel Mack 
27493c9740a1SPeter Chen 	device_wakeup_enable(hcd->self.controller);
27502cc65feaSDaniel Mack 	return 0;
27512cc65feaSDaniel Mack }
27522cc65feaSDaniel Mack 
musb_host_resume_root_hub(struct musb * musb)27530b3eba44SDaniel Mack void musb_host_resume_root_hub(struct musb *musb)
27540b3eba44SDaniel Mack {
275574c2e936SDaniel Mack 	usb_hcd_resume_root_hub(musb->hcd);
27560b3eba44SDaniel Mack }
27570b3eba44SDaniel Mack 
musb_host_poke_root_hub(struct musb * musb)27580b3eba44SDaniel Mack void musb_host_poke_root_hub(struct musb *musb)
27590b3eba44SDaniel Mack {
27600b3eba44SDaniel Mack 	MUSB_HST_MODE(musb);
276174c2e936SDaniel Mack 	if (musb->hcd->status_urb)
276274c2e936SDaniel Mack 		usb_hcd_poll_rh_status(musb->hcd);
27630b3eba44SDaniel Mack 	else
276474c2e936SDaniel Mack 		usb_hcd_resume_root_hub(musb->hcd);
27650b3eba44SDaniel Mack }
2766