xref: /linux/drivers/usb/musb/musb_host.c (revision 2ed9127cff9a255b7671b8d3d938109f68a87961)
1550a7375SFelipe Balbi /*
2550a7375SFelipe Balbi  * MUSB OTG driver host support
3550a7375SFelipe Balbi  *
4550a7375SFelipe Balbi  * Copyright 2005 Mentor Graphics Corporation
5550a7375SFelipe Balbi  * Copyright (C) 2005-2006 by Texas Instruments
6550a7375SFelipe Balbi  * Copyright (C) 2006-2007 Nokia Corporation
7c7bbc056SSergei Shtylyov  * Copyright (C) 2008-2009 MontaVista Software, Inc. <source@mvista.com>
8550a7375SFelipe Balbi  *
9550a7375SFelipe Balbi  * This program is free software; you can redistribute it and/or
10550a7375SFelipe Balbi  * modify it under the terms of the GNU General Public License
11550a7375SFelipe Balbi  * version 2 as published by the Free Software Foundation.
12550a7375SFelipe Balbi  *
13550a7375SFelipe Balbi  * This program is distributed in the hope that it will be useful, but
14550a7375SFelipe Balbi  * WITHOUT ANY WARRANTY; without even the implied warranty of
15550a7375SFelipe Balbi  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16550a7375SFelipe Balbi  * General Public License for more details.
17550a7375SFelipe Balbi  *
18550a7375SFelipe Balbi  * You should have received a copy of the GNU General Public License
19550a7375SFelipe Balbi  * along with this program; if not, write to the Free Software
20550a7375SFelipe Balbi  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
21550a7375SFelipe Balbi  * 02110-1301 USA
22550a7375SFelipe Balbi  *
23550a7375SFelipe Balbi  * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
24550a7375SFelipe Balbi  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
25550a7375SFelipe Balbi  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN
26550a7375SFelipe Balbi  * NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27550a7375SFelipe Balbi  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28550a7375SFelipe Balbi  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
29550a7375SFelipe Balbi  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
30550a7375SFelipe Balbi  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31550a7375SFelipe Balbi  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32550a7375SFelipe Balbi  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33550a7375SFelipe Balbi  *
34550a7375SFelipe Balbi  */
35550a7375SFelipe Balbi 
36550a7375SFelipe Balbi #include <linux/module.h>
37550a7375SFelipe Balbi #include <linux/kernel.h>
38550a7375SFelipe Balbi #include <linux/delay.h>
39550a7375SFelipe Balbi #include <linux/sched.h>
40550a7375SFelipe Balbi #include <linux/slab.h>
41550a7375SFelipe Balbi #include <linux/errno.h>
42550a7375SFelipe Balbi #include <linux/init.h>
43550a7375SFelipe Balbi #include <linux/list.h>
44496dda70SMaulik Mankad #include <linux/dma-mapping.h>
45550a7375SFelipe Balbi 
46550a7375SFelipe Balbi #include "musb_core.h"
47550a7375SFelipe Balbi #include "musb_host.h"
48550a7375SFelipe Balbi 
49550a7375SFelipe Balbi 
50550a7375SFelipe Balbi /* MUSB HOST status 22-mar-2006
51550a7375SFelipe Balbi  *
52550a7375SFelipe Balbi  * - There's still lots of partial code duplication for fault paths, so
53550a7375SFelipe Balbi  *   they aren't handled as consistently as they need to be.
54550a7375SFelipe Balbi  *
55550a7375SFelipe Balbi  * - PIO mostly behaved when last tested.
56550a7375SFelipe Balbi  *     + including ep0, with all usbtest cases 9, 10
57550a7375SFelipe Balbi  *     + usbtest 14 (ep0out) doesn't seem to run at all
58550a7375SFelipe Balbi  *     + double buffered OUT/TX endpoints saw stalls(!) with certain usbtest
59550a7375SFelipe Balbi  *       configurations, but otherwise double buffering passes basic tests.
60550a7375SFelipe Balbi  *     + for 2.6.N, for N > ~10, needs API changes for hcd framework.
61550a7375SFelipe Balbi  *
62550a7375SFelipe Balbi  * - DMA (CPPI) ... partially behaves, not currently recommended
63550a7375SFelipe Balbi  *     + about 1/15 the speed of typical EHCI implementations (PCI)
64550a7375SFelipe Balbi  *     + RX, all too often reqpkt seems to misbehave after tx
65550a7375SFelipe Balbi  *     + TX, no known issues (other than evident silicon issue)
66550a7375SFelipe Balbi  *
67550a7375SFelipe Balbi  * - DMA (Mentor/OMAP) ...has at least toggle update problems
68550a7375SFelipe Balbi  *
691e0320f0SAjay Kumar Gupta  * - [23-feb-2009] minimal traffic scheduling to avoid bulk RX packet
701e0320f0SAjay Kumar Gupta  *   starvation ... nothing yet for TX, interrupt, or bulk.
71550a7375SFelipe Balbi  *
72550a7375SFelipe Balbi  * - Not tested with HNP, but some SRP paths seem to behave.
73550a7375SFelipe Balbi  *
74550a7375SFelipe Balbi  * NOTE 24-August-2006:
75550a7375SFelipe Balbi  *
76550a7375SFelipe Balbi  * - Bulk traffic finally uses both sides of hardware ep1, freeing up an
77550a7375SFelipe Balbi  *   extra endpoint for periodic use enabling hub + keybd + mouse.  That
78550a7375SFelipe Balbi  *   mostly works, except that with "usbnet" it's easy to trigger cases
79550a7375SFelipe Balbi  *   with "ping" where RX loses.  (a) ping to davinci, even "ping -f",
80550a7375SFelipe Balbi  *   fine; but (b) ping _from_ davinci, even "ping -c 1", ICMP RX loses
81550a7375SFelipe Balbi  *   although ARP RX wins.  (That test was done with a full speed link.)
82550a7375SFelipe Balbi  */
83550a7375SFelipe Balbi 
84550a7375SFelipe Balbi 
85550a7375SFelipe Balbi /*
86550a7375SFelipe Balbi  * NOTE on endpoint usage:
87550a7375SFelipe Balbi  *
88550a7375SFelipe Balbi  * CONTROL transfers all go through ep0.  BULK ones go through dedicated IN
89550a7375SFelipe Balbi  * and OUT endpoints ... hardware is dedicated for those "async" queue(s).
90550a7375SFelipe Balbi  * (Yes, bulk _could_ use more of the endpoints than that, and would even
911e0320f0SAjay Kumar Gupta  * benefit from it.)
92550a7375SFelipe Balbi  *
93550a7375SFelipe Balbi  * INTERUPPT and ISOCHRONOUS transfers are scheduled to the other endpoints.
94550a7375SFelipe Balbi  * So far that scheduling is both dumb and optimistic:  the endpoint will be
95550a7375SFelipe Balbi  * "claimed" until its software queue is no longer refilled.  No multiplexing
96550a7375SFelipe Balbi  * of transfers between endpoints, or anything clever.
97550a7375SFelipe Balbi  */
98550a7375SFelipe Balbi 
99550a7375SFelipe Balbi 
100550a7375SFelipe Balbi static void musb_ep_program(struct musb *musb, u8 epnum,
1016b6e9710SSergei Shtylyov 			struct urb *urb, int is_out,
1026b6e9710SSergei Shtylyov 			u8 *buf, u32 offset, u32 len);
103550a7375SFelipe Balbi 
104550a7375SFelipe Balbi /*
105550a7375SFelipe Balbi  * Clear TX fifo. Needed to avoid BABBLE errors.
106550a7375SFelipe Balbi  */
107c767c1c6SDavid Brownell static void musb_h_tx_flush_fifo(struct musb_hw_ep *ep)
108550a7375SFelipe Balbi {
1095c8a86e1SFelipe Balbi 	struct musb	*musb = ep->musb;
110550a7375SFelipe Balbi 	void __iomem	*epio = ep->regs;
111550a7375SFelipe Balbi 	u16		csr;
112bb1c9ef1SDavid Brownell 	u16		lastcsr = 0;
113550a7375SFelipe Balbi 	int		retries = 1000;
114550a7375SFelipe Balbi 
115550a7375SFelipe Balbi 	csr = musb_readw(epio, MUSB_TXCSR);
116550a7375SFelipe Balbi 	while (csr & MUSB_TXCSR_FIFONOTEMPTY) {
117bb1c9ef1SDavid Brownell 		if (csr != lastcsr)
1185c8a86e1SFelipe Balbi 			dev_dbg(musb->controller, "Host TX FIFONOTEMPTY csr: %02x\n", csr);
119bb1c9ef1SDavid Brownell 		lastcsr = csr;
120550a7375SFelipe Balbi 		csr |= MUSB_TXCSR_FLUSHFIFO;
121550a7375SFelipe Balbi 		musb_writew(epio, MUSB_TXCSR, csr);
122550a7375SFelipe Balbi 		csr = musb_readw(epio, MUSB_TXCSR);
123bb1c9ef1SDavid Brownell 		if (WARN(retries-- < 1,
124bb1c9ef1SDavid Brownell 				"Could not flush host TX%d fifo: csr: %04x\n",
125bb1c9ef1SDavid Brownell 				ep->epnum, csr))
126550a7375SFelipe Balbi 			return;
127550a7375SFelipe Balbi 		mdelay(1);
128550a7375SFelipe Balbi 	}
129550a7375SFelipe Balbi }
130550a7375SFelipe Balbi 
13178322c1aSDavid Brownell static void musb_h_ep0_flush_fifo(struct musb_hw_ep *ep)
13278322c1aSDavid Brownell {
13378322c1aSDavid Brownell 	void __iomem	*epio = ep->regs;
13478322c1aSDavid Brownell 	u16		csr;
13578322c1aSDavid Brownell 	int		retries = 5;
13678322c1aSDavid Brownell 
13778322c1aSDavid Brownell 	/* scrub any data left in the fifo */
13878322c1aSDavid Brownell 	do {
13978322c1aSDavid Brownell 		csr = musb_readw(epio, MUSB_TXCSR);
14078322c1aSDavid Brownell 		if (!(csr & (MUSB_CSR0_TXPKTRDY | MUSB_CSR0_RXPKTRDY)))
14178322c1aSDavid Brownell 			break;
14278322c1aSDavid Brownell 		musb_writew(epio, MUSB_TXCSR, MUSB_CSR0_FLUSHFIFO);
14378322c1aSDavid Brownell 		csr = musb_readw(epio, MUSB_TXCSR);
14478322c1aSDavid Brownell 		udelay(10);
14578322c1aSDavid Brownell 	} while (--retries);
14678322c1aSDavid Brownell 
14778322c1aSDavid Brownell 	WARN(!retries, "Could not flush host TX%d fifo: csr: %04x\n",
14878322c1aSDavid Brownell 			ep->epnum, csr);
14978322c1aSDavid Brownell 
15078322c1aSDavid Brownell 	/* and reset for the next transfer */
15178322c1aSDavid Brownell 	musb_writew(epio, MUSB_TXCSR, 0);
15278322c1aSDavid Brownell }
15378322c1aSDavid Brownell 
154550a7375SFelipe Balbi /*
155550a7375SFelipe Balbi  * Start transmit. Caller is responsible for locking shared resources.
156550a7375SFelipe Balbi  * musb must be locked.
157550a7375SFelipe Balbi  */
158550a7375SFelipe Balbi static inline void musb_h_tx_start(struct musb_hw_ep *ep)
159550a7375SFelipe Balbi {
160550a7375SFelipe Balbi 	u16	txcsr;
161550a7375SFelipe Balbi 
162550a7375SFelipe Balbi 	/* NOTE: no locks here; caller should lock and select EP */
163550a7375SFelipe Balbi 	if (ep->epnum) {
164550a7375SFelipe Balbi 		txcsr = musb_readw(ep->regs, MUSB_TXCSR);
165550a7375SFelipe Balbi 		txcsr |= MUSB_TXCSR_TXPKTRDY | MUSB_TXCSR_H_WZC_BITS;
166550a7375SFelipe Balbi 		musb_writew(ep->regs, MUSB_TXCSR, txcsr);
167550a7375SFelipe Balbi 	} else {
168550a7375SFelipe Balbi 		txcsr = MUSB_CSR0_H_SETUPPKT | MUSB_CSR0_TXPKTRDY;
169550a7375SFelipe Balbi 		musb_writew(ep->regs, MUSB_CSR0, txcsr);
170550a7375SFelipe Balbi 	}
171550a7375SFelipe Balbi 
172550a7375SFelipe Balbi }
173550a7375SFelipe Balbi 
174c7bbc056SSergei Shtylyov static inline void musb_h_tx_dma_start(struct musb_hw_ep *ep)
175550a7375SFelipe Balbi {
176550a7375SFelipe Balbi 	u16	txcsr;
177550a7375SFelipe Balbi 
178550a7375SFelipe Balbi 	/* NOTE: no locks here; caller should lock and select EP */
179550a7375SFelipe Balbi 	txcsr = musb_readw(ep->regs, MUSB_TXCSR);
180550a7375SFelipe Balbi 	txcsr |= MUSB_TXCSR_DMAENAB | MUSB_TXCSR_H_WZC_BITS;
181c7bbc056SSergei Shtylyov 	if (is_cppi_enabled())
182c7bbc056SSergei Shtylyov 		txcsr |= MUSB_TXCSR_DMAMODE;
183550a7375SFelipe Balbi 	musb_writew(ep->regs, MUSB_TXCSR, txcsr);
184550a7375SFelipe Balbi }
185550a7375SFelipe Balbi 
1863e5c6dc7SSergei Shtylyov static void musb_ep_set_qh(struct musb_hw_ep *ep, int is_in, struct musb_qh *qh)
1873e5c6dc7SSergei Shtylyov {
1883e5c6dc7SSergei Shtylyov 	if (is_in != 0 || ep->is_shared_fifo)
1893e5c6dc7SSergei Shtylyov 		ep->in_qh  = qh;
1903e5c6dc7SSergei Shtylyov 	if (is_in == 0 || ep->is_shared_fifo)
1913e5c6dc7SSergei Shtylyov 		ep->out_qh = qh;
1923e5c6dc7SSergei Shtylyov }
1933e5c6dc7SSergei Shtylyov 
1943e5c6dc7SSergei Shtylyov static struct musb_qh *musb_ep_get_qh(struct musb_hw_ep *ep, int is_in)
1953e5c6dc7SSergei Shtylyov {
1963e5c6dc7SSergei Shtylyov 	return is_in ? ep->in_qh : ep->out_qh;
1973e5c6dc7SSergei Shtylyov }
1983e5c6dc7SSergei Shtylyov 
199550a7375SFelipe Balbi /*
200550a7375SFelipe Balbi  * Start the URB at the front of an endpoint's queue
201550a7375SFelipe Balbi  * end must be claimed from the caller.
202550a7375SFelipe Balbi  *
203550a7375SFelipe Balbi  * Context: controller locked, irqs blocked
204550a7375SFelipe Balbi  */
205550a7375SFelipe Balbi static void
206550a7375SFelipe Balbi musb_start_urb(struct musb *musb, int is_in, struct musb_qh *qh)
207550a7375SFelipe Balbi {
208550a7375SFelipe Balbi 	u16			frame;
209550a7375SFelipe Balbi 	u32			len;
210550a7375SFelipe Balbi 	void __iomem		*mbase =  musb->mregs;
211550a7375SFelipe Balbi 	struct urb		*urb = next_urb(qh);
2126b6e9710SSergei Shtylyov 	void			*buf = urb->transfer_buffer;
2136b6e9710SSergei Shtylyov 	u32			offset = 0;
214550a7375SFelipe Balbi 	struct musb_hw_ep	*hw_ep = qh->hw_ep;
215550a7375SFelipe Balbi 	unsigned		pipe = urb->pipe;
216550a7375SFelipe Balbi 	u8			address = usb_pipedevice(pipe);
217550a7375SFelipe Balbi 	int			epnum = hw_ep->epnum;
218550a7375SFelipe Balbi 
219550a7375SFelipe Balbi 	/* initialize software qh state */
220550a7375SFelipe Balbi 	qh->offset = 0;
221550a7375SFelipe Balbi 	qh->segsize = 0;
222550a7375SFelipe Balbi 
223550a7375SFelipe Balbi 	/* gather right source of data */
224550a7375SFelipe Balbi 	switch (qh->type) {
225550a7375SFelipe Balbi 	case USB_ENDPOINT_XFER_CONTROL:
226550a7375SFelipe Balbi 		/* control transfers always start with SETUP */
227550a7375SFelipe Balbi 		is_in = 0;
228550a7375SFelipe Balbi 		musb->ep0_stage = MUSB_EP0_START;
229550a7375SFelipe Balbi 		buf = urb->setup_packet;
230550a7375SFelipe Balbi 		len = 8;
231550a7375SFelipe Balbi 		break;
232550a7375SFelipe Balbi 	case USB_ENDPOINT_XFER_ISOC:
233550a7375SFelipe Balbi 		qh->iso_idx = 0;
234550a7375SFelipe Balbi 		qh->frame = 0;
2356b6e9710SSergei Shtylyov 		offset = urb->iso_frame_desc[0].offset;
236550a7375SFelipe Balbi 		len = urb->iso_frame_desc[0].length;
237550a7375SFelipe Balbi 		break;
238550a7375SFelipe Balbi 	default:		/* bulk, interrupt */
2391e0320f0SAjay Kumar Gupta 		/* actual_length may be nonzero on retry paths */
2401e0320f0SAjay Kumar Gupta 		buf = urb->transfer_buffer + urb->actual_length;
2411e0320f0SAjay Kumar Gupta 		len = urb->transfer_buffer_length - urb->actual_length;
242550a7375SFelipe Balbi 	}
243550a7375SFelipe Balbi 
2445c8a86e1SFelipe Balbi 	dev_dbg(musb->controller, "qh %p urb %p dev%d ep%d%s%s, hw_ep %d, %p/%d\n",
245550a7375SFelipe Balbi 			qh, urb, address, qh->epnum,
246550a7375SFelipe Balbi 			is_in ? "in" : "out",
247550a7375SFelipe Balbi 			({char *s; switch (qh->type) {
248550a7375SFelipe Balbi 			case USB_ENDPOINT_XFER_CONTROL:	s = ""; break;
249550a7375SFelipe Balbi 			case USB_ENDPOINT_XFER_BULK:	s = "-bulk"; break;
250550a7375SFelipe Balbi 			case USB_ENDPOINT_XFER_ISOC:	s = "-iso"; break;
251550a7375SFelipe Balbi 			default:			s = "-intr"; break;
252550a7375SFelipe Balbi 			}; s; }),
2536b6e9710SSergei Shtylyov 			epnum, buf + offset, len);
254550a7375SFelipe Balbi 
255550a7375SFelipe Balbi 	/* Configure endpoint */
2563e5c6dc7SSergei Shtylyov 	musb_ep_set_qh(hw_ep, is_in, qh);
2576b6e9710SSergei Shtylyov 	musb_ep_program(musb, epnum, urb, !is_in, buf, offset, len);
258550a7375SFelipe Balbi 
259550a7375SFelipe Balbi 	/* transmit may have more work: start it when it is time */
260550a7375SFelipe Balbi 	if (is_in)
261550a7375SFelipe Balbi 		return;
262550a7375SFelipe Balbi 
263550a7375SFelipe Balbi 	/* determine if the time is right for a periodic transfer */
264550a7375SFelipe Balbi 	switch (qh->type) {
265550a7375SFelipe Balbi 	case USB_ENDPOINT_XFER_ISOC:
266550a7375SFelipe Balbi 	case USB_ENDPOINT_XFER_INT:
2675c8a86e1SFelipe Balbi 		dev_dbg(musb->controller, "check whether there's still time for periodic Tx\n");
268550a7375SFelipe Balbi 		frame = musb_readw(mbase, MUSB_FRAME);
269550a7375SFelipe Balbi 		/* FIXME this doesn't implement that scheduling policy ...
270550a7375SFelipe Balbi 		 * or handle framecounter wrapping
271550a7375SFelipe Balbi 		 */
272550a7375SFelipe Balbi 		if ((urb->transfer_flags & URB_ISO_ASAP)
273550a7375SFelipe Balbi 				|| (frame >= urb->start_frame)) {
274550a7375SFelipe Balbi 			/* REVISIT the SOF irq handler shouldn't duplicate
275550a7375SFelipe Balbi 			 * this code; and we don't init urb->start_frame...
276550a7375SFelipe Balbi 			 */
277550a7375SFelipe Balbi 			qh->frame = 0;
278550a7375SFelipe Balbi 			goto start;
279550a7375SFelipe Balbi 		} else {
280550a7375SFelipe Balbi 			qh->frame = urb->start_frame;
281550a7375SFelipe Balbi 			/* enable SOF interrupt so we can count down */
2825c8a86e1SFelipe Balbi 			dev_dbg(musb->controller, "SOF for %d\n", epnum);
283550a7375SFelipe Balbi #if 1 /* ifndef	CONFIG_ARCH_DAVINCI */
284550a7375SFelipe Balbi 			musb_writeb(mbase, MUSB_INTRUSBE, 0xff);
285550a7375SFelipe Balbi #endif
286550a7375SFelipe Balbi 		}
287550a7375SFelipe Balbi 		break;
288550a7375SFelipe Balbi 	default:
289550a7375SFelipe Balbi start:
2905c8a86e1SFelipe Balbi 		dev_dbg(musb->controller, "Start TX%d %s\n", epnum,
291550a7375SFelipe Balbi 			hw_ep->tx_channel ? "dma" : "pio");
292550a7375SFelipe Balbi 
293550a7375SFelipe Balbi 		if (!hw_ep->tx_channel)
294550a7375SFelipe Balbi 			musb_h_tx_start(hw_ep);
295550a7375SFelipe Balbi 		else if (is_cppi_enabled() || tusb_dma_omap())
296c7bbc056SSergei Shtylyov 			musb_h_tx_dma_start(hw_ep);
297550a7375SFelipe Balbi 	}
298550a7375SFelipe Balbi }
299550a7375SFelipe Balbi 
300c9cd06b3SSergei Shtylyov /* Context: caller owns controller lock, IRQs are blocked */
301c9cd06b3SSergei Shtylyov static void musb_giveback(struct musb *musb, struct urb *urb, int status)
302550a7375SFelipe Balbi __releases(musb->lock)
303550a7375SFelipe Balbi __acquires(musb->lock)
304550a7375SFelipe Balbi {
3055c8a86e1SFelipe Balbi 	dev_dbg(musb->controller,
306bb1c9ef1SDavid Brownell 			"complete %p %pF (%d), dev%d ep%d%s, %d/%d\n",
307bb1c9ef1SDavid Brownell 			urb, urb->complete, status,
308550a7375SFelipe Balbi 			usb_pipedevice(urb->pipe),
309550a7375SFelipe Balbi 			usb_pipeendpoint(urb->pipe),
310550a7375SFelipe Balbi 			usb_pipein(urb->pipe) ? "in" : "out",
311550a7375SFelipe Balbi 			urb->actual_length, urb->transfer_buffer_length
312550a7375SFelipe Balbi 			);
313550a7375SFelipe Balbi 
3142492e674SAjay Kumar Gupta 	usb_hcd_unlink_urb_from_ep(musb_to_hcd(musb), urb);
315550a7375SFelipe Balbi 	spin_unlock(&musb->lock);
316550a7375SFelipe Balbi 	usb_hcd_giveback_urb(musb_to_hcd(musb), urb, status);
317550a7375SFelipe Balbi 	spin_lock(&musb->lock);
318550a7375SFelipe Balbi }
319550a7375SFelipe Balbi 
320846099a6SSergei Shtylyov /* For bulk/interrupt endpoints only */
321846099a6SSergei Shtylyov static inline void musb_save_toggle(struct musb_qh *qh, int is_in,
322846099a6SSergei Shtylyov 				    struct urb *urb)
323550a7375SFelipe Balbi {
324846099a6SSergei Shtylyov 	void __iomem		*epio = qh->hw_ep->regs;
325550a7375SFelipe Balbi 	u16			csr;
326550a7375SFelipe Balbi 
327846099a6SSergei Shtylyov 	/*
328846099a6SSergei Shtylyov 	 * FIXME: the current Mentor DMA code seems to have
329550a7375SFelipe Balbi 	 * problems getting toggle correct.
330550a7375SFelipe Balbi 	 */
331550a7375SFelipe Balbi 
332846099a6SSergei Shtylyov 	if (is_in)
333846099a6SSergei Shtylyov 		csr = musb_readw(epio, MUSB_RXCSR) & MUSB_RXCSR_H_DATATOGGLE;
334550a7375SFelipe Balbi 	else
335846099a6SSergei Shtylyov 		csr = musb_readw(epio, MUSB_TXCSR) & MUSB_TXCSR_H_DATATOGGLE;
336550a7375SFelipe Balbi 
337846099a6SSergei Shtylyov 	usb_settoggle(urb->dev, qh->epnum, !is_in, csr ? 1 : 0);
338550a7375SFelipe Balbi }
339550a7375SFelipe Balbi 
340c9cd06b3SSergei Shtylyov /*
341c9cd06b3SSergei Shtylyov  * Advance this hardware endpoint's queue, completing the specified URB and
342c9cd06b3SSergei Shtylyov  * advancing to either the next URB queued to that qh, or else invalidating
343c9cd06b3SSergei Shtylyov  * that qh and advancing to the next qh scheduled after the current one.
344c9cd06b3SSergei Shtylyov  *
345c9cd06b3SSergei Shtylyov  * Context: caller owns controller lock, IRQs are blocked
346c9cd06b3SSergei Shtylyov  */
347c9cd06b3SSergei Shtylyov static void musb_advance_schedule(struct musb *musb, struct urb *urb,
348c9cd06b3SSergei Shtylyov 				  struct musb_hw_ep *hw_ep, int is_in)
349550a7375SFelipe Balbi {
350c9cd06b3SSergei Shtylyov 	struct musb_qh		*qh = musb_ep_get_qh(hw_ep, is_in);
351550a7375SFelipe Balbi 	struct musb_hw_ep	*ep = qh->hw_ep;
352550a7375SFelipe Balbi 	int			ready = qh->is_ready;
353c9cd06b3SSergei Shtylyov 	int			status;
354c9cd06b3SSergei Shtylyov 
355c9cd06b3SSergei Shtylyov 	status = (urb->status == -EINPROGRESS) ? 0 : urb->status;
356550a7375SFelipe Balbi 
357550a7375SFelipe Balbi 	/* save toggle eagerly, for paranoia */
358550a7375SFelipe Balbi 	switch (qh->type) {
359550a7375SFelipe Balbi 	case USB_ENDPOINT_XFER_BULK:
360550a7375SFelipe Balbi 	case USB_ENDPOINT_XFER_INT:
361846099a6SSergei Shtylyov 		musb_save_toggle(qh, is_in, urb);
362550a7375SFelipe Balbi 		break;
363550a7375SFelipe Balbi 	case USB_ENDPOINT_XFER_ISOC:
3641fe975f9SSergei Shtylyov 		if (status == 0 && urb->error_count)
365550a7375SFelipe Balbi 			status = -EXDEV;
366550a7375SFelipe Balbi 		break;
367550a7375SFelipe Balbi 	}
368550a7375SFelipe Balbi 
369550a7375SFelipe Balbi 	qh->is_ready = 0;
370c9cd06b3SSergei Shtylyov 	musb_giveback(musb, urb, status);
371550a7375SFelipe Balbi 	qh->is_ready = ready;
372550a7375SFelipe Balbi 
373550a7375SFelipe Balbi 	/* reclaim resources (and bandwidth) ASAP; deschedule it, and
374550a7375SFelipe Balbi 	 * invalidate qh as soon as list_empty(&hep->urb_list)
375550a7375SFelipe Balbi 	 */
376550a7375SFelipe Balbi 	if (list_empty(&qh->hep->urb_list)) {
377550a7375SFelipe Balbi 		struct list_head	*head;
378550a7375SFelipe Balbi 
379550a7375SFelipe Balbi 		if (is_in)
380550a7375SFelipe Balbi 			ep->rx_reinit = 1;
381550a7375SFelipe Balbi 		else
382550a7375SFelipe Balbi 			ep->tx_reinit = 1;
383550a7375SFelipe Balbi 
3843e5c6dc7SSergei Shtylyov 		/* Clobber old pointers to this qh */
3853e5c6dc7SSergei Shtylyov 		musb_ep_set_qh(ep, is_in, NULL);
386550a7375SFelipe Balbi 		qh->hep->hcpriv = NULL;
387550a7375SFelipe Balbi 
388550a7375SFelipe Balbi 		switch (qh->type) {
389550a7375SFelipe Balbi 
39023d15e07SAjay Kumar Gupta 		case USB_ENDPOINT_XFER_CONTROL:
39123d15e07SAjay Kumar Gupta 		case USB_ENDPOINT_XFER_BULK:
39223d15e07SAjay Kumar Gupta 			/* fifo policy for these lists, except that NAKing
39323d15e07SAjay Kumar Gupta 			 * should rotate a qh to the end (for fairness).
39423d15e07SAjay Kumar Gupta 			 */
39523d15e07SAjay Kumar Gupta 			if (qh->mux == 1) {
39623d15e07SAjay Kumar Gupta 				head = qh->ring.prev;
39723d15e07SAjay Kumar Gupta 				list_del(&qh->ring);
39823d15e07SAjay Kumar Gupta 				kfree(qh);
39923d15e07SAjay Kumar Gupta 				qh = first_qh(head);
40023d15e07SAjay Kumar Gupta 				break;
40123d15e07SAjay Kumar Gupta 			}
40223d15e07SAjay Kumar Gupta 
403550a7375SFelipe Balbi 		case USB_ENDPOINT_XFER_ISOC:
404550a7375SFelipe Balbi 		case USB_ENDPOINT_XFER_INT:
405550a7375SFelipe Balbi 			/* this is where periodic bandwidth should be
406550a7375SFelipe Balbi 			 * de-allocated if it's tracked and allocated;
407550a7375SFelipe Balbi 			 * and where we'd update the schedule tree...
408550a7375SFelipe Balbi 			 */
409550a7375SFelipe Balbi 			kfree(qh);
410550a7375SFelipe Balbi 			qh = NULL;
411550a7375SFelipe Balbi 			break;
412550a7375SFelipe Balbi 		}
413550a7375SFelipe Balbi 	}
414550a7375SFelipe Balbi 
415a2fd814eSSergei Shtylyov 	if (qh != NULL && qh->is_ready) {
4165c8a86e1SFelipe Balbi 		dev_dbg(musb->controller, "... next ep%d %cX urb %p\n",
417c9cd06b3SSergei Shtylyov 		    hw_ep->epnum, is_in ? 'R' : 'T', next_urb(qh));
418550a7375SFelipe Balbi 		musb_start_urb(musb, is_in, qh);
419550a7375SFelipe Balbi 	}
420550a7375SFelipe Balbi }
421550a7375SFelipe Balbi 
422c767c1c6SDavid Brownell static u16 musb_h_flush_rxfifo(struct musb_hw_ep *hw_ep, u16 csr)
423550a7375SFelipe Balbi {
424550a7375SFelipe Balbi 	/* we don't want fifo to fill itself again;
425550a7375SFelipe Balbi 	 * ignore dma (various models),
426550a7375SFelipe Balbi 	 * leave toggle alone (may not have been saved yet)
427550a7375SFelipe Balbi 	 */
428550a7375SFelipe Balbi 	csr |= MUSB_RXCSR_FLUSHFIFO | MUSB_RXCSR_RXPKTRDY;
429550a7375SFelipe Balbi 	csr &= ~(MUSB_RXCSR_H_REQPKT
430550a7375SFelipe Balbi 		| MUSB_RXCSR_H_AUTOREQ
431550a7375SFelipe Balbi 		| MUSB_RXCSR_AUTOCLEAR);
432550a7375SFelipe Balbi 
433550a7375SFelipe Balbi 	/* write 2x to allow double buffering */
434550a7375SFelipe Balbi 	musb_writew(hw_ep->regs, MUSB_RXCSR, csr);
435550a7375SFelipe Balbi 	musb_writew(hw_ep->regs, MUSB_RXCSR, csr);
436550a7375SFelipe Balbi 
437550a7375SFelipe Balbi 	/* flush writebuffer */
438550a7375SFelipe Balbi 	return musb_readw(hw_ep->regs, MUSB_RXCSR);
439550a7375SFelipe Balbi }
440550a7375SFelipe Balbi 
441550a7375SFelipe Balbi /*
442550a7375SFelipe Balbi  * PIO RX for a packet (or part of it).
443550a7375SFelipe Balbi  */
444550a7375SFelipe Balbi static bool
445550a7375SFelipe Balbi musb_host_packet_rx(struct musb *musb, struct urb *urb, u8 epnum, u8 iso_err)
446550a7375SFelipe Balbi {
447550a7375SFelipe Balbi 	u16			rx_count;
448550a7375SFelipe Balbi 	u8			*buf;
449550a7375SFelipe Balbi 	u16			csr;
450550a7375SFelipe Balbi 	bool			done = false;
451550a7375SFelipe Balbi 	u32			length;
452550a7375SFelipe Balbi 	int			do_flush = 0;
453550a7375SFelipe Balbi 	struct musb_hw_ep	*hw_ep = musb->endpoints + epnum;
454550a7375SFelipe Balbi 	void __iomem		*epio = hw_ep->regs;
455550a7375SFelipe Balbi 	struct musb_qh		*qh = hw_ep->in_qh;
456550a7375SFelipe Balbi 	int			pipe = urb->pipe;
457550a7375SFelipe Balbi 	void			*buffer = urb->transfer_buffer;
458550a7375SFelipe Balbi 
459550a7375SFelipe Balbi 	/* musb_ep_select(mbase, epnum); */
460550a7375SFelipe Balbi 	rx_count = musb_readw(epio, MUSB_RXCOUNT);
4615c8a86e1SFelipe Balbi 	dev_dbg(musb->controller, "RX%d count %d, buffer %p len %d/%d\n", epnum, rx_count,
462550a7375SFelipe Balbi 			urb->transfer_buffer, qh->offset,
463550a7375SFelipe Balbi 			urb->transfer_buffer_length);
464550a7375SFelipe Balbi 
465550a7375SFelipe Balbi 	/* unload FIFO */
466550a7375SFelipe Balbi 	if (usb_pipeisoc(pipe)) {
467550a7375SFelipe Balbi 		int					status = 0;
468550a7375SFelipe Balbi 		struct usb_iso_packet_descriptor	*d;
469550a7375SFelipe Balbi 
470550a7375SFelipe Balbi 		if (iso_err) {
471550a7375SFelipe Balbi 			status = -EILSEQ;
472550a7375SFelipe Balbi 			urb->error_count++;
473550a7375SFelipe Balbi 		}
474550a7375SFelipe Balbi 
475550a7375SFelipe Balbi 		d = urb->iso_frame_desc + qh->iso_idx;
476550a7375SFelipe Balbi 		buf = buffer + d->offset;
477550a7375SFelipe Balbi 		length = d->length;
478550a7375SFelipe Balbi 		if (rx_count > length) {
479550a7375SFelipe Balbi 			if (status == 0) {
480550a7375SFelipe Balbi 				status = -EOVERFLOW;
481550a7375SFelipe Balbi 				urb->error_count++;
482550a7375SFelipe Balbi 			}
4835c8a86e1SFelipe Balbi 			dev_dbg(musb->controller, "** OVERFLOW %d into %d\n", rx_count, length);
484550a7375SFelipe Balbi 			do_flush = 1;
485550a7375SFelipe Balbi 		} else
486550a7375SFelipe Balbi 			length = rx_count;
487550a7375SFelipe Balbi 		urb->actual_length += length;
488550a7375SFelipe Balbi 		d->actual_length = length;
489550a7375SFelipe Balbi 
490550a7375SFelipe Balbi 		d->status = status;
491550a7375SFelipe Balbi 
492550a7375SFelipe Balbi 		/* see if we are done */
493550a7375SFelipe Balbi 		done = (++qh->iso_idx >= urb->number_of_packets);
494550a7375SFelipe Balbi 	} else {
495550a7375SFelipe Balbi 		/* non-isoch */
496550a7375SFelipe Balbi 		buf = buffer + qh->offset;
497550a7375SFelipe Balbi 		length = urb->transfer_buffer_length - qh->offset;
498550a7375SFelipe Balbi 		if (rx_count > length) {
499550a7375SFelipe Balbi 			if (urb->status == -EINPROGRESS)
500550a7375SFelipe Balbi 				urb->status = -EOVERFLOW;
5015c8a86e1SFelipe Balbi 			dev_dbg(musb->controller, "** OVERFLOW %d into %d\n", rx_count, length);
502550a7375SFelipe Balbi 			do_flush = 1;
503550a7375SFelipe Balbi 		} else
504550a7375SFelipe Balbi 			length = rx_count;
505550a7375SFelipe Balbi 		urb->actual_length += length;
506550a7375SFelipe Balbi 		qh->offset += length;
507550a7375SFelipe Balbi 
508550a7375SFelipe Balbi 		/* see if we are done */
509550a7375SFelipe Balbi 		done = (urb->actual_length == urb->transfer_buffer_length)
510550a7375SFelipe Balbi 			|| (rx_count < qh->maxpacket)
511550a7375SFelipe Balbi 			|| (urb->status != -EINPROGRESS);
512550a7375SFelipe Balbi 		if (done
513550a7375SFelipe Balbi 				&& (urb->status == -EINPROGRESS)
514550a7375SFelipe Balbi 				&& (urb->transfer_flags & URB_SHORT_NOT_OK)
515550a7375SFelipe Balbi 				&& (urb->actual_length
516550a7375SFelipe Balbi 					< urb->transfer_buffer_length))
517550a7375SFelipe Balbi 			urb->status = -EREMOTEIO;
518550a7375SFelipe Balbi 	}
519550a7375SFelipe Balbi 
520550a7375SFelipe Balbi 	musb_read_fifo(hw_ep, length, buf);
521550a7375SFelipe Balbi 
522550a7375SFelipe Balbi 	csr = musb_readw(epio, MUSB_RXCSR);
523550a7375SFelipe Balbi 	csr |= MUSB_RXCSR_H_WZC_BITS;
524550a7375SFelipe Balbi 	if (unlikely(do_flush))
525550a7375SFelipe Balbi 		musb_h_flush_rxfifo(hw_ep, csr);
526550a7375SFelipe Balbi 	else {
527550a7375SFelipe Balbi 		/* REVISIT this assumes AUTOCLEAR is never set */
528550a7375SFelipe Balbi 		csr &= ~(MUSB_RXCSR_RXPKTRDY | MUSB_RXCSR_H_REQPKT);
529550a7375SFelipe Balbi 		if (!done)
530550a7375SFelipe Balbi 			csr |= MUSB_RXCSR_H_REQPKT;
531550a7375SFelipe Balbi 		musb_writew(epio, MUSB_RXCSR, csr);
532550a7375SFelipe Balbi 	}
533550a7375SFelipe Balbi 
534550a7375SFelipe Balbi 	return done;
535550a7375SFelipe Balbi }
536550a7375SFelipe Balbi 
537550a7375SFelipe Balbi /* we don't always need to reinit a given side of an endpoint...
538550a7375SFelipe Balbi  * when we do, use tx/rx reinit routine and then construct a new CSR
539550a7375SFelipe Balbi  * to address data toggle, NYET, and DMA or PIO.
540550a7375SFelipe Balbi  *
541550a7375SFelipe Balbi  * it's possible that driver bugs (especially for DMA) or aborting a
542550a7375SFelipe Balbi  * transfer might have left the endpoint busier than it should be.
543550a7375SFelipe Balbi  * the busy/not-empty tests are basically paranoia.
544550a7375SFelipe Balbi  */
545550a7375SFelipe Balbi static void
546550a7375SFelipe Balbi musb_rx_reinit(struct musb *musb, struct musb_qh *qh, struct musb_hw_ep *ep)
547550a7375SFelipe Balbi {
548550a7375SFelipe Balbi 	u16	csr;
549550a7375SFelipe Balbi 
550550a7375SFelipe Balbi 	/* NOTE:  we know the "rx" fifo reinit never triggers for ep0.
551550a7375SFelipe Balbi 	 * That always uses tx_reinit since ep0 repurposes TX register
552550a7375SFelipe Balbi 	 * offsets; the initial SETUP packet is also a kind of OUT.
553550a7375SFelipe Balbi 	 */
554550a7375SFelipe Balbi 
555550a7375SFelipe Balbi 	/* if programmed for Tx, put it in RX mode */
556550a7375SFelipe Balbi 	if (ep->is_shared_fifo) {
557550a7375SFelipe Balbi 		csr = musb_readw(ep->regs, MUSB_TXCSR);
558550a7375SFelipe Balbi 		if (csr & MUSB_TXCSR_MODE) {
559550a7375SFelipe Balbi 			musb_h_tx_flush_fifo(ep);
560b6e434a5SSergei Shtylyov 			csr = musb_readw(ep->regs, MUSB_TXCSR);
561550a7375SFelipe Balbi 			musb_writew(ep->regs, MUSB_TXCSR,
562b6e434a5SSergei Shtylyov 				    csr | MUSB_TXCSR_FRCDATATOG);
563550a7375SFelipe Balbi 		}
564b6e434a5SSergei Shtylyov 
565b6e434a5SSergei Shtylyov 		/*
566b6e434a5SSergei Shtylyov 		 * Clear the MODE bit (and everything else) to enable Rx.
567b6e434a5SSergei Shtylyov 		 * NOTE: we mustn't clear the DMAMODE bit before DMAENAB.
568b6e434a5SSergei Shtylyov 		 */
569b6e434a5SSergei Shtylyov 		if (csr & MUSB_TXCSR_DMAMODE)
570b6e434a5SSergei Shtylyov 			musb_writew(ep->regs, MUSB_TXCSR, MUSB_TXCSR_DMAMODE);
571550a7375SFelipe Balbi 		musb_writew(ep->regs, MUSB_TXCSR, 0);
572550a7375SFelipe Balbi 
573550a7375SFelipe Balbi 	/* scrub all previous state, clearing toggle */
574550a7375SFelipe Balbi 	} else {
575550a7375SFelipe Balbi 		csr = musb_readw(ep->regs, MUSB_RXCSR);
576550a7375SFelipe Balbi 		if (csr & MUSB_RXCSR_RXPKTRDY)
577550a7375SFelipe Balbi 			WARNING("rx%d, packet/%d ready?\n", ep->epnum,
578550a7375SFelipe Balbi 				musb_readw(ep->regs, MUSB_RXCOUNT));
579550a7375SFelipe Balbi 
580550a7375SFelipe Balbi 		musb_h_flush_rxfifo(ep, MUSB_RXCSR_CLRDATATOG);
581550a7375SFelipe Balbi 	}
582550a7375SFelipe Balbi 
583550a7375SFelipe Balbi 	/* target addr and (for multipoint) hub addr/port */
584550a7375SFelipe Balbi 	if (musb->is_multipoint) {
585c6cf8b00SBryan Wu 		musb_write_rxfunaddr(ep->target_regs, qh->addr_reg);
586c6cf8b00SBryan Wu 		musb_write_rxhubaddr(ep->target_regs, qh->h_addr_reg);
587c6cf8b00SBryan Wu 		musb_write_rxhubport(ep->target_regs, qh->h_port_reg);
588c6cf8b00SBryan Wu 
589550a7375SFelipe Balbi 	} else
590550a7375SFelipe Balbi 		musb_writeb(musb->mregs, MUSB_FADDR, qh->addr_reg);
591550a7375SFelipe Balbi 
592550a7375SFelipe Balbi 	/* protocol/endpoint, interval/NAKlimit, i/o size */
593550a7375SFelipe Balbi 	musb_writeb(ep->regs, MUSB_RXTYPE, qh->type_reg);
594550a7375SFelipe Balbi 	musb_writeb(ep->regs, MUSB_RXINTERVAL, qh->intv_reg);
595550a7375SFelipe Balbi 	/* NOTE: bulk combining rewrites high bits of maxpacket */
5969f445cb2SCliff Cai 	/* Set RXMAXP with the FIFO size of the endpoint
5979f445cb2SCliff Cai 	 * to disable double buffer mode.
5989f445cb2SCliff Cai 	 */
59906624818SFelipe Balbi 	if (musb->double_buffer_not_ok)
6009f445cb2SCliff Cai 		musb_writew(ep->regs, MUSB_RXMAXP, ep->max_packet_sz_rx);
6019f445cb2SCliff Cai 	else
602a483d706SAjay Kumar Gupta 		musb_writew(ep->regs, MUSB_RXMAXP,
603a483d706SAjay Kumar Gupta 				qh->maxpacket | ((qh->hb_mult - 1) << 11));
604550a7375SFelipe Balbi 
605550a7375SFelipe Balbi 	ep->rx_reinit = 0;
606550a7375SFelipe Balbi }
607550a7375SFelipe Balbi 
6086b6e9710SSergei Shtylyov static bool musb_tx_dma_program(struct dma_controller *dma,
6096b6e9710SSergei Shtylyov 		struct musb_hw_ep *hw_ep, struct musb_qh *qh,
6106b6e9710SSergei Shtylyov 		struct urb *urb, u32 offset, u32 length)
6116b6e9710SSergei Shtylyov {
6126b6e9710SSergei Shtylyov 	struct dma_channel	*channel = hw_ep->tx_channel;
6136b6e9710SSergei Shtylyov 	void __iomem		*epio = hw_ep->regs;
6146b6e9710SSergei Shtylyov 	u16			pkt_size = qh->maxpacket;
6156b6e9710SSergei Shtylyov 	u16			csr;
6166b6e9710SSergei Shtylyov 	u8			mode;
6176b6e9710SSergei Shtylyov 
6186b6e9710SSergei Shtylyov #ifdef	CONFIG_USB_INVENTRA_DMA
6196b6e9710SSergei Shtylyov 	if (length > channel->max_len)
6206b6e9710SSergei Shtylyov 		length = channel->max_len;
6216b6e9710SSergei Shtylyov 
6226b6e9710SSergei Shtylyov 	csr = musb_readw(epio, MUSB_TXCSR);
6236b6e9710SSergei Shtylyov 	if (length > pkt_size) {
6246b6e9710SSergei Shtylyov 		mode = 1;
625a483d706SAjay Kumar Gupta 		csr |= MUSB_TXCSR_DMAMODE | MUSB_TXCSR_DMAENAB;
626a483d706SAjay Kumar Gupta 		/* autoset shouldn't be set in high bandwidth */
627a483d706SAjay Kumar Gupta 		if (qh->hb_mult == 1)
628a483d706SAjay Kumar Gupta 			csr |= MUSB_TXCSR_AUTOSET;
6296b6e9710SSergei Shtylyov 	} else {
6306b6e9710SSergei Shtylyov 		mode = 0;
6316b6e9710SSergei Shtylyov 		csr &= ~(MUSB_TXCSR_AUTOSET | MUSB_TXCSR_DMAMODE);
6326b6e9710SSergei Shtylyov 		csr |= MUSB_TXCSR_DMAENAB; /* against programmer's guide */
6336b6e9710SSergei Shtylyov 	}
6346b6e9710SSergei Shtylyov 	channel->desired_mode = mode;
6356b6e9710SSergei Shtylyov 	musb_writew(epio, MUSB_TXCSR, csr);
6366b6e9710SSergei Shtylyov #else
6376b6e9710SSergei Shtylyov 	if (!is_cppi_enabled() && !tusb_dma_omap())
6386b6e9710SSergei Shtylyov 		return false;
6396b6e9710SSergei Shtylyov 
6406b6e9710SSergei Shtylyov 	channel->actual_len = 0;
6416b6e9710SSergei Shtylyov 
6426b6e9710SSergei Shtylyov 	/*
6436b6e9710SSergei Shtylyov 	 * TX uses "RNDIS" mode automatically but needs help
6446b6e9710SSergei Shtylyov 	 * to identify the zero-length-final-packet case.
6456b6e9710SSergei Shtylyov 	 */
6466b6e9710SSergei Shtylyov 	mode = (urb->transfer_flags & URB_ZERO_PACKET) ? 1 : 0;
6476b6e9710SSergei Shtylyov #endif
6486b6e9710SSergei Shtylyov 
6496b6e9710SSergei Shtylyov 	qh->segsize = length;
6506b6e9710SSergei Shtylyov 
6514c647338SSantosh Shilimkar 	/*
6524c647338SSantosh Shilimkar 	 * Ensure the data reaches to main memory before starting
6534c647338SSantosh Shilimkar 	 * DMA transfer
6544c647338SSantosh Shilimkar 	 */
6554c647338SSantosh Shilimkar 	wmb();
6564c647338SSantosh Shilimkar 
6576b6e9710SSergei Shtylyov 	if (!dma->channel_program(channel, pkt_size, mode,
6586b6e9710SSergei Shtylyov 			urb->transfer_dma + offset, length)) {
6596b6e9710SSergei Shtylyov 		dma->channel_release(channel);
6606b6e9710SSergei Shtylyov 		hw_ep->tx_channel = NULL;
6616b6e9710SSergei Shtylyov 
6626b6e9710SSergei Shtylyov 		csr = musb_readw(epio, MUSB_TXCSR);
6636b6e9710SSergei Shtylyov 		csr &= ~(MUSB_TXCSR_AUTOSET | MUSB_TXCSR_DMAENAB);
6646b6e9710SSergei Shtylyov 		musb_writew(epio, MUSB_TXCSR, csr | MUSB_TXCSR_H_WZC_BITS);
6656b6e9710SSergei Shtylyov 		return false;
6666b6e9710SSergei Shtylyov 	}
6676b6e9710SSergei Shtylyov 	return true;
6686b6e9710SSergei Shtylyov }
669550a7375SFelipe Balbi 
670550a7375SFelipe Balbi /*
671550a7375SFelipe Balbi  * Program an HDRC endpoint as per the given URB
672550a7375SFelipe Balbi  * Context: irqs blocked, controller lock held
673550a7375SFelipe Balbi  */
674550a7375SFelipe Balbi static void musb_ep_program(struct musb *musb, u8 epnum,
6756b6e9710SSergei Shtylyov 			struct urb *urb, int is_out,
6766b6e9710SSergei Shtylyov 			u8 *buf, u32 offset, u32 len)
677550a7375SFelipe Balbi {
678550a7375SFelipe Balbi 	struct dma_controller	*dma_controller;
679550a7375SFelipe Balbi 	struct dma_channel	*dma_channel;
680550a7375SFelipe Balbi 	u8			dma_ok;
681550a7375SFelipe Balbi 	void __iomem		*mbase = musb->mregs;
682550a7375SFelipe Balbi 	struct musb_hw_ep	*hw_ep = musb->endpoints + epnum;
683550a7375SFelipe Balbi 	void __iomem		*epio = hw_ep->regs;
6843e5c6dc7SSergei Shtylyov 	struct musb_qh		*qh = musb_ep_get_qh(hw_ep, !is_out);
6853e5c6dc7SSergei Shtylyov 	u16			packet_sz = qh->maxpacket;
686550a7375SFelipe Balbi 
6875c8a86e1SFelipe Balbi 	dev_dbg(musb->controller, "%s hw%d urb %p spd%d dev%d ep%d%s "
688550a7375SFelipe Balbi 				"h_addr%02x h_port%02x bytes %d\n",
689550a7375SFelipe Balbi 			is_out ? "-->" : "<--",
690550a7375SFelipe Balbi 			epnum, urb, urb->dev->speed,
691550a7375SFelipe Balbi 			qh->addr_reg, qh->epnum, is_out ? "out" : "in",
692550a7375SFelipe Balbi 			qh->h_addr_reg, qh->h_port_reg,
693550a7375SFelipe Balbi 			len);
694550a7375SFelipe Balbi 
695550a7375SFelipe Balbi 	musb_ep_select(mbase, epnum);
696550a7375SFelipe Balbi 
697550a7375SFelipe Balbi 	/* candidate for DMA? */
698550a7375SFelipe Balbi 	dma_controller = musb->dma_controller;
699550a7375SFelipe Balbi 	if (is_dma_capable() && epnum && dma_controller) {
700550a7375SFelipe Balbi 		dma_channel = is_out ? hw_ep->tx_channel : hw_ep->rx_channel;
701550a7375SFelipe Balbi 		if (!dma_channel) {
702550a7375SFelipe Balbi 			dma_channel = dma_controller->channel_alloc(
703550a7375SFelipe Balbi 					dma_controller, hw_ep, is_out);
704550a7375SFelipe Balbi 			if (is_out)
705550a7375SFelipe Balbi 				hw_ep->tx_channel = dma_channel;
706550a7375SFelipe Balbi 			else
707550a7375SFelipe Balbi 				hw_ep->rx_channel = dma_channel;
708550a7375SFelipe Balbi 		}
709550a7375SFelipe Balbi 	} else
710550a7375SFelipe Balbi 		dma_channel = NULL;
711550a7375SFelipe Balbi 
712550a7375SFelipe Balbi 	/* make sure we clear DMAEnab, autoSet bits from previous run */
713550a7375SFelipe Balbi 
714550a7375SFelipe Balbi 	/* OUT/transmit/EP0 or IN/receive? */
715550a7375SFelipe Balbi 	if (is_out) {
716550a7375SFelipe Balbi 		u16	csr;
717550a7375SFelipe Balbi 		u16	int_txe;
718550a7375SFelipe Balbi 		u16	load_count;
719550a7375SFelipe Balbi 
720550a7375SFelipe Balbi 		csr = musb_readw(epio, MUSB_TXCSR);
721550a7375SFelipe Balbi 
722550a7375SFelipe Balbi 		/* disable interrupt in case we flush */
723550a7375SFelipe Balbi 		int_txe = musb_readw(mbase, MUSB_INTRTXE);
724550a7375SFelipe Balbi 		musb_writew(mbase, MUSB_INTRTXE, int_txe & ~(1 << epnum));
725550a7375SFelipe Balbi 
726550a7375SFelipe Balbi 		/* general endpoint setup */
727550a7375SFelipe Balbi 		if (epnum) {
728550a7375SFelipe Balbi 			/* flush all old state, set default */
729550a7375SFelipe Balbi 			musb_h_tx_flush_fifo(hw_ep);
730b6e434a5SSergei Shtylyov 
731b6e434a5SSergei Shtylyov 			/*
732b6e434a5SSergei Shtylyov 			 * We must not clear the DMAMODE bit before or in
733b6e434a5SSergei Shtylyov 			 * the same cycle with the DMAENAB bit, so we clear
734b6e434a5SSergei Shtylyov 			 * the latter first...
735b6e434a5SSergei Shtylyov 			 */
736550a7375SFelipe Balbi 			csr &= ~(MUSB_TXCSR_H_NAKTIMEOUT
737b6e434a5SSergei Shtylyov 					| MUSB_TXCSR_AUTOSET
738b6e434a5SSergei Shtylyov 					| MUSB_TXCSR_DMAENAB
739550a7375SFelipe Balbi 					| MUSB_TXCSR_FRCDATATOG
740550a7375SFelipe Balbi 					| MUSB_TXCSR_H_RXSTALL
741550a7375SFelipe Balbi 					| MUSB_TXCSR_H_ERROR
742550a7375SFelipe Balbi 					| MUSB_TXCSR_TXPKTRDY
743550a7375SFelipe Balbi 					);
744550a7375SFelipe Balbi 			csr |= MUSB_TXCSR_MODE;
745550a7375SFelipe Balbi 
746b6e434a5SSergei Shtylyov 			if (usb_gettoggle(urb->dev, qh->epnum, 1))
747550a7375SFelipe Balbi 				csr |= MUSB_TXCSR_H_WR_DATATOGGLE
748550a7375SFelipe Balbi 					| MUSB_TXCSR_H_DATATOGGLE;
749550a7375SFelipe Balbi 			else
750550a7375SFelipe Balbi 				csr |= MUSB_TXCSR_CLRDATATOG;
751550a7375SFelipe Balbi 
752550a7375SFelipe Balbi 			musb_writew(epio, MUSB_TXCSR, csr);
753550a7375SFelipe Balbi 			/* REVISIT may need to clear FLUSHFIFO ... */
754b6e434a5SSergei Shtylyov 			csr &= ~MUSB_TXCSR_DMAMODE;
755550a7375SFelipe Balbi 			musb_writew(epio, MUSB_TXCSR, csr);
756550a7375SFelipe Balbi 			csr = musb_readw(epio, MUSB_TXCSR);
757550a7375SFelipe Balbi 		} else {
758550a7375SFelipe Balbi 			/* endpoint 0: just flush */
75978322c1aSDavid Brownell 			musb_h_ep0_flush_fifo(hw_ep);
760550a7375SFelipe Balbi 		}
761550a7375SFelipe Balbi 
762550a7375SFelipe Balbi 		/* target addr and (for multipoint) hub addr/port */
763550a7375SFelipe Balbi 		if (musb->is_multipoint) {
764c6cf8b00SBryan Wu 			musb_write_txfunaddr(mbase, epnum, qh->addr_reg);
765c6cf8b00SBryan Wu 			musb_write_txhubaddr(mbase, epnum, qh->h_addr_reg);
766c6cf8b00SBryan Wu 			musb_write_txhubport(mbase, epnum, qh->h_port_reg);
767550a7375SFelipe Balbi /* FIXME if !epnum, do the same for RX ... */
768550a7375SFelipe Balbi 		} else
769550a7375SFelipe Balbi 			musb_writeb(mbase, MUSB_FADDR, qh->addr_reg);
770550a7375SFelipe Balbi 
771550a7375SFelipe Balbi 		/* protocol/endpoint/interval/NAKlimit */
772550a7375SFelipe Balbi 		if (epnum) {
773550a7375SFelipe Balbi 			musb_writeb(epio, MUSB_TXTYPE, qh->type_reg);
77406624818SFelipe Balbi 			if (musb->double_buffer_not_ok)
775550a7375SFelipe Balbi 				musb_writew(epio, MUSB_TXMAXP,
77606624818SFelipe Balbi 						hw_ep->max_packet_sz_tx);
777ccc080c7SAjay Kumar Gupta 			else if (can_bulk_split(musb, qh->type))
778ccc080c7SAjay Kumar Gupta 				musb_writew(epio, MUSB_TXMAXP, packet_sz
779ccc080c7SAjay Kumar Gupta 					| ((hw_ep->max_packet_sz_tx /
780ccc080c7SAjay Kumar Gupta 						packet_sz) - 1) << 11);
781550a7375SFelipe Balbi 			else
782550a7375SFelipe Balbi 				musb_writew(epio, MUSB_TXMAXP,
78306624818SFelipe Balbi 						qh->maxpacket |
78406624818SFelipe Balbi 						((qh->hb_mult - 1) << 11));
785550a7375SFelipe Balbi 			musb_writeb(epio, MUSB_TXINTERVAL, qh->intv_reg);
786550a7375SFelipe Balbi 		} else {
787550a7375SFelipe Balbi 			musb_writeb(epio, MUSB_NAKLIMIT0, qh->intv_reg);
788550a7375SFelipe Balbi 			if (musb->is_multipoint)
789550a7375SFelipe Balbi 				musb_writeb(epio, MUSB_TYPE0,
790550a7375SFelipe Balbi 						qh->type_reg);
791550a7375SFelipe Balbi 		}
792550a7375SFelipe Balbi 
793550a7375SFelipe Balbi 		if (can_bulk_split(musb, qh->type))
794550a7375SFelipe Balbi 			load_count = min((u32) hw_ep->max_packet_sz_tx,
795550a7375SFelipe Balbi 						len);
796550a7375SFelipe Balbi 		else
797550a7375SFelipe Balbi 			load_count = min((u32) packet_sz, len);
798550a7375SFelipe Balbi 
7996b6e9710SSergei Shtylyov 		if (dma_channel && musb_tx_dma_program(dma_controller,
8006b6e9710SSergei Shtylyov 					hw_ep, qh, urb, offset, len))
801550a7375SFelipe Balbi 			load_count = 0;
802550a7375SFelipe Balbi 
803550a7375SFelipe Balbi 		if (load_count) {
804550a7375SFelipe Balbi 			/* PIO to load FIFO */
805550a7375SFelipe Balbi 			qh->segsize = load_count;
806550a7375SFelipe Balbi 			musb_write_fifo(hw_ep, load_count, buf);
807550a7375SFelipe Balbi 		}
808550a7375SFelipe Balbi 
809550a7375SFelipe Balbi 		/* re-enable interrupt */
810550a7375SFelipe Balbi 		musb_writew(mbase, MUSB_INTRTXE, int_txe);
811550a7375SFelipe Balbi 
812550a7375SFelipe Balbi 	/* IN/receive */
813550a7375SFelipe Balbi 	} else {
814550a7375SFelipe Balbi 		u16	csr;
815550a7375SFelipe Balbi 
816550a7375SFelipe Balbi 		if (hw_ep->rx_reinit) {
817550a7375SFelipe Balbi 			musb_rx_reinit(musb, qh, hw_ep);
818550a7375SFelipe Balbi 
819550a7375SFelipe Balbi 			/* init new state: toggle and NYET, maybe DMA later */
820550a7375SFelipe Balbi 			if (usb_gettoggle(urb->dev, qh->epnum, 0))
821550a7375SFelipe Balbi 				csr = MUSB_RXCSR_H_WR_DATATOGGLE
822550a7375SFelipe Balbi 					| MUSB_RXCSR_H_DATATOGGLE;
823550a7375SFelipe Balbi 			else
824550a7375SFelipe Balbi 				csr = 0;
825550a7375SFelipe Balbi 			if (qh->type == USB_ENDPOINT_XFER_INT)
826550a7375SFelipe Balbi 				csr |= MUSB_RXCSR_DISNYET;
827550a7375SFelipe Balbi 
828550a7375SFelipe Balbi 		} else {
829550a7375SFelipe Balbi 			csr = musb_readw(hw_ep->regs, MUSB_RXCSR);
830550a7375SFelipe Balbi 
831550a7375SFelipe Balbi 			if (csr & (MUSB_RXCSR_RXPKTRDY
832550a7375SFelipe Balbi 					| MUSB_RXCSR_DMAENAB
833550a7375SFelipe Balbi 					| MUSB_RXCSR_H_REQPKT))
834550a7375SFelipe Balbi 				ERR("broken !rx_reinit, ep%d csr %04x\n",
835550a7375SFelipe Balbi 						hw_ep->epnum, csr);
836550a7375SFelipe Balbi 
837550a7375SFelipe Balbi 			/* scrub any stale state, leaving toggle alone */
838550a7375SFelipe Balbi 			csr &= MUSB_RXCSR_DISNYET;
839550a7375SFelipe Balbi 		}
840550a7375SFelipe Balbi 
841550a7375SFelipe Balbi 		/* kick things off */
842550a7375SFelipe Balbi 
843550a7375SFelipe Balbi 		if ((is_cppi_enabled() || tusb_dma_omap()) && dma_channel) {
844c51e36dcSSergei Shtylyov 			/* Candidate for DMA */
845550a7375SFelipe Balbi 			dma_channel->actual_len = 0L;
846550a7375SFelipe Balbi 			qh->segsize = len;
847550a7375SFelipe Balbi 
848550a7375SFelipe Balbi 			/* AUTOREQ is in a DMA register */
849550a7375SFelipe Balbi 			musb_writew(hw_ep->regs, MUSB_RXCSR, csr);
850c51e36dcSSergei Shtylyov 			csr = musb_readw(hw_ep->regs, MUSB_RXCSR);
851550a7375SFelipe Balbi 
852c51e36dcSSergei Shtylyov 			/*
853c51e36dcSSergei Shtylyov 			 * Unless caller treats short RX transfers as
854550a7375SFelipe Balbi 			 * errors, we dare not queue multiple transfers.
855550a7375SFelipe Balbi 			 */
856c51e36dcSSergei Shtylyov 			dma_ok = dma_controller->channel_program(dma_channel,
857c51e36dcSSergei Shtylyov 					packet_sz, !(urb->transfer_flags &
858c51e36dcSSergei Shtylyov 						     URB_SHORT_NOT_OK),
8596b6e9710SSergei Shtylyov 					urb->transfer_dma + offset,
860550a7375SFelipe Balbi 					qh->segsize);
861550a7375SFelipe Balbi 			if (!dma_ok) {
862c51e36dcSSergei Shtylyov 				dma_controller->channel_release(dma_channel);
863c51e36dcSSergei Shtylyov 				hw_ep->rx_channel = dma_channel = NULL;
864550a7375SFelipe Balbi 			} else
865550a7375SFelipe Balbi 				csr |= MUSB_RXCSR_DMAENAB;
866550a7375SFelipe Balbi 		}
867550a7375SFelipe Balbi 
868550a7375SFelipe Balbi 		csr |= MUSB_RXCSR_H_REQPKT;
8695c8a86e1SFelipe Balbi 		dev_dbg(musb->controller, "RXCSR%d := %04x\n", epnum, csr);
870550a7375SFelipe Balbi 		musb_writew(hw_ep->regs, MUSB_RXCSR, csr);
871550a7375SFelipe Balbi 		csr = musb_readw(hw_ep->regs, MUSB_RXCSR);
872550a7375SFelipe Balbi 	}
873550a7375SFelipe Balbi }
874550a7375SFelipe Balbi 
875550a7375SFelipe Balbi 
876550a7375SFelipe Balbi /*
877550a7375SFelipe Balbi  * Service the default endpoint (ep0) as host.
878550a7375SFelipe Balbi  * Return true until it's time to start the status stage.
879550a7375SFelipe Balbi  */
880550a7375SFelipe Balbi static bool musb_h_ep0_continue(struct musb *musb, u16 len, struct urb *urb)
881550a7375SFelipe Balbi {
882550a7375SFelipe Balbi 	bool			 more = false;
883550a7375SFelipe Balbi 	u8			*fifo_dest = NULL;
884550a7375SFelipe Balbi 	u16			fifo_count = 0;
885550a7375SFelipe Balbi 	struct musb_hw_ep	*hw_ep = musb->control_ep;
886550a7375SFelipe Balbi 	struct musb_qh		*qh = hw_ep->in_qh;
887550a7375SFelipe Balbi 	struct usb_ctrlrequest	*request;
888550a7375SFelipe Balbi 
889550a7375SFelipe Balbi 	switch (musb->ep0_stage) {
890550a7375SFelipe Balbi 	case MUSB_EP0_IN:
891550a7375SFelipe Balbi 		fifo_dest = urb->transfer_buffer + urb->actual_length;
8923ecdb9acSSergei Shtylyov 		fifo_count = min_t(size_t, len, urb->transfer_buffer_length -
8933ecdb9acSSergei Shtylyov 				   urb->actual_length);
894550a7375SFelipe Balbi 		if (fifo_count < len)
895550a7375SFelipe Balbi 			urb->status = -EOVERFLOW;
896550a7375SFelipe Balbi 
897550a7375SFelipe Balbi 		musb_read_fifo(hw_ep, fifo_count, fifo_dest);
898550a7375SFelipe Balbi 
899550a7375SFelipe Balbi 		urb->actual_length += fifo_count;
900550a7375SFelipe Balbi 		if (len < qh->maxpacket) {
901550a7375SFelipe Balbi 			/* always terminate on short read; it's
902550a7375SFelipe Balbi 			 * rarely reported as an error.
903550a7375SFelipe Balbi 			 */
904550a7375SFelipe Balbi 		} else if (urb->actual_length <
905550a7375SFelipe Balbi 				urb->transfer_buffer_length)
906550a7375SFelipe Balbi 			more = true;
907550a7375SFelipe Balbi 		break;
908550a7375SFelipe Balbi 	case MUSB_EP0_START:
909550a7375SFelipe Balbi 		request = (struct usb_ctrlrequest *) urb->setup_packet;
910550a7375SFelipe Balbi 
911550a7375SFelipe Balbi 		if (!request->wLength) {
9125c8a86e1SFelipe Balbi 			dev_dbg(musb->controller, "start no-DATA\n");
913550a7375SFelipe Balbi 			break;
914550a7375SFelipe Balbi 		} else if (request->bRequestType & USB_DIR_IN) {
9155c8a86e1SFelipe Balbi 			dev_dbg(musb->controller, "start IN-DATA\n");
916550a7375SFelipe Balbi 			musb->ep0_stage = MUSB_EP0_IN;
917550a7375SFelipe Balbi 			more = true;
918550a7375SFelipe Balbi 			break;
919550a7375SFelipe Balbi 		} else {
9205c8a86e1SFelipe Balbi 			dev_dbg(musb->controller, "start OUT-DATA\n");
921550a7375SFelipe Balbi 			musb->ep0_stage = MUSB_EP0_OUT;
922550a7375SFelipe Balbi 			more = true;
923550a7375SFelipe Balbi 		}
924550a7375SFelipe Balbi 		/* FALLTHROUGH */
925550a7375SFelipe Balbi 	case MUSB_EP0_OUT:
9263ecdb9acSSergei Shtylyov 		fifo_count = min_t(size_t, qh->maxpacket,
9273ecdb9acSSergei Shtylyov 				   urb->transfer_buffer_length -
9283ecdb9acSSergei Shtylyov 				   urb->actual_length);
929550a7375SFelipe Balbi 		if (fifo_count) {
930550a7375SFelipe Balbi 			fifo_dest = (u8 *) (urb->transfer_buffer
931550a7375SFelipe Balbi 					+ urb->actual_length);
9325c8a86e1SFelipe Balbi 			dev_dbg(musb->controller, "Sending %d byte%s to ep0 fifo %p\n",
933bb1c9ef1SDavid Brownell 					fifo_count,
934bb1c9ef1SDavid Brownell 					(fifo_count == 1) ? "" : "s",
935bb1c9ef1SDavid Brownell 					fifo_dest);
936550a7375SFelipe Balbi 			musb_write_fifo(hw_ep, fifo_count, fifo_dest);
937550a7375SFelipe Balbi 
938550a7375SFelipe Balbi 			urb->actual_length += fifo_count;
939550a7375SFelipe Balbi 			more = true;
940550a7375SFelipe Balbi 		}
941550a7375SFelipe Balbi 		break;
942550a7375SFelipe Balbi 	default:
943550a7375SFelipe Balbi 		ERR("bogus ep0 stage %d\n", musb->ep0_stage);
944550a7375SFelipe Balbi 		break;
945550a7375SFelipe Balbi 	}
946550a7375SFelipe Balbi 
947550a7375SFelipe Balbi 	return more;
948550a7375SFelipe Balbi }
949550a7375SFelipe Balbi 
950550a7375SFelipe Balbi /*
951550a7375SFelipe Balbi  * Handle default endpoint interrupt as host. Only called in IRQ time
952c767c1c6SDavid Brownell  * from musb_interrupt().
953550a7375SFelipe Balbi  *
954550a7375SFelipe Balbi  * called with controller irqlocked
955550a7375SFelipe Balbi  */
956550a7375SFelipe Balbi irqreturn_t musb_h_ep0_irq(struct musb *musb)
957550a7375SFelipe Balbi {
958550a7375SFelipe Balbi 	struct urb		*urb;
959550a7375SFelipe Balbi 	u16			csr, len;
960550a7375SFelipe Balbi 	int			status = 0;
961550a7375SFelipe Balbi 	void __iomem		*mbase = musb->mregs;
962550a7375SFelipe Balbi 	struct musb_hw_ep	*hw_ep = musb->control_ep;
963550a7375SFelipe Balbi 	void __iomem		*epio = hw_ep->regs;
964550a7375SFelipe Balbi 	struct musb_qh		*qh = hw_ep->in_qh;
965550a7375SFelipe Balbi 	bool			complete = false;
966550a7375SFelipe Balbi 	irqreturn_t		retval = IRQ_NONE;
967550a7375SFelipe Balbi 
968550a7375SFelipe Balbi 	/* ep0 only has one queue, "in" */
969550a7375SFelipe Balbi 	urb = next_urb(qh);
970550a7375SFelipe Balbi 
971550a7375SFelipe Balbi 	musb_ep_select(mbase, 0);
972550a7375SFelipe Balbi 	csr = musb_readw(epio, MUSB_CSR0);
973550a7375SFelipe Balbi 	len = (csr & MUSB_CSR0_RXPKTRDY)
974550a7375SFelipe Balbi 			? musb_readb(epio, MUSB_COUNT0)
975550a7375SFelipe Balbi 			: 0;
976550a7375SFelipe Balbi 
9775c8a86e1SFelipe Balbi 	dev_dbg(musb->controller, "<== csr0 %04x, qh %p, count %d, urb %p, stage %d\n",
978550a7375SFelipe Balbi 		csr, qh, len, urb, musb->ep0_stage);
979550a7375SFelipe Balbi 
980550a7375SFelipe Balbi 	/* if we just did status stage, we are done */
981550a7375SFelipe Balbi 	if (MUSB_EP0_STATUS == musb->ep0_stage) {
982550a7375SFelipe Balbi 		retval = IRQ_HANDLED;
983550a7375SFelipe Balbi 		complete = true;
984550a7375SFelipe Balbi 	}
985550a7375SFelipe Balbi 
986550a7375SFelipe Balbi 	/* prepare status */
987550a7375SFelipe Balbi 	if (csr & MUSB_CSR0_H_RXSTALL) {
9885c8a86e1SFelipe Balbi 		dev_dbg(musb->controller, "STALLING ENDPOINT\n");
989550a7375SFelipe Balbi 		status = -EPIPE;
990550a7375SFelipe Balbi 
991550a7375SFelipe Balbi 	} else if (csr & MUSB_CSR0_H_ERROR) {
9925c8a86e1SFelipe Balbi 		dev_dbg(musb->controller, "no response, csr0 %04x\n", csr);
993550a7375SFelipe Balbi 		status = -EPROTO;
994550a7375SFelipe Balbi 
995550a7375SFelipe Balbi 	} else if (csr & MUSB_CSR0_H_NAKTIMEOUT) {
9965c8a86e1SFelipe Balbi 		dev_dbg(musb->controller, "control NAK timeout\n");
997550a7375SFelipe Balbi 
998550a7375SFelipe Balbi 		/* NOTE:  this code path would be a good place to PAUSE a
999550a7375SFelipe Balbi 		 * control transfer, if another one is queued, so that
10001e0320f0SAjay Kumar Gupta 		 * ep0 is more likely to stay busy.  That's already done
10011e0320f0SAjay Kumar Gupta 		 * for bulk RX transfers.
1002550a7375SFelipe Balbi 		 *
1003550a7375SFelipe Balbi 		 * if (qh->ring.next != &musb->control), then
1004550a7375SFelipe Balbi 		 * we have a candidate... NAKing is *NOT* an error
1005550a7375SFelipe Balbi 		 */
1006550a7375SFelipe Balbi 		musb_writew(epio, MUSB_CSR0, 0);
1007550a7375SFelipe Balbi 		retval = IRQ_HANDLED;
1008550a7375SFelipe Balbi 	}
1009550a7375SFelipe Balbi 
1010550a7375SFelipe Balbi 	if (status) {
10115c8a86e1SFelipe Balbi 		dev_dbg(musb->controller, "aborting\n");
1012550a7375SFelipe Balbi 		retval = IRQ_HANDLED;
1013550a7375SFelipe Balbi 		if (urb)
1014550a7375SFelipe Balbi 			urb->status = status;
1015550a7375SFelipe Balbi 		complete = true;
1016550a7375SFelipe Balbi 
1017550a7375SFelipe Balbi 		/* use the proper sequence to abort the transfer */
1018550a7375SFelipe Balbi 		if (csr & MUSB_CSR0_H_REQPKT) {
1019550a7375SFelipe Balbi 			csr &= ~MUSB_CSR0_H_REQPKT;
1020550a7375SFelipe Balbi 			musb_writew(epio, MUSB_CSR0, csr);
1021550a7375SFelipe Balbi 			csr &= ~MUSB_CSR0_H_NAKTIMEOUT;
1022550a7375SFelipe Balbi 			musb_writew(epio, MUSB_CSR0, csr);
1023550a7375SFelipe Balbi 		} else {
102478322c1aSDavid Brownell 			musb_h_ep0_flush_fifo(hw_ep);
1025550a7375SFelipe Balbi 		}
1026550a7375SFelipe Balbi 
1027550a7375SFelipe Balbi 		musb_writeb(epio, MUSB_NAKLIMIT0, 0);
1028550a7375SFelipe Balbi 
1029550a7375SFelipe Balbi 		/* clear it */
1030550a7375SFelipe Balbi 		musb_writew(epio, MUSB_CSR0, 0);
1031550a7375SFelipe Balbi 	}
1032550a7375SFelipe Balbi 
1033550a7375SFelipe Balbi 	if (unlikely(!urb)) {
1034550a7375SFelipe Balbi 		/* stop endpoint since we have no place for its data, this
1035550a7375SFelipe Balbi 		 * SHOULD NEVER HAPPEN! */
1036550a7375SFelipe Balbi 		ERR("no URB for end 0\n");
1037550a7375SFelipe Balbi 
103878322c1aSDavid Brownell 		musb_h_ep0_flush_fifo(hw_ep);
1039550a7375SFelipe Balbi 		goto done;
1040550a7375SFelipe Balbi 	}
1041550a7375SFelipe Balbi 
1042550a7375SFelipe Balbi 	if (!complete) {
1043550a7375SFelipe Balbi 		/* call common logic and prepare response */
1044550a7375SFelipe Balbi 		if (musb_h_ep0_continue(musb, len, urb)) {
1045550a7375SFelipe Balbi 			/* more packets required */
1046550a7375SFelipe Balbi 			csr = (MUSB_EP0_IN == musb->ep0_stage)
1047550a7375SFelipe Balbi 				?  MUSB_CSR0_H_REQPKT : MUSB_CSR0_TXPKTRDY;
1048550a7375SFelipe Balbi 		} else {
1049550a7375SFelipe Balbi 			/* data transfer complete; perform status phase */
1050550a7375SFelipe Balbi 			if (usb_pipeout(urb->pipe)
1051550a7375SFelipe Balbi 					|| !urb->transfer_buffer_length)
1052550a7375SFelipe Balbi 				csr = MUSB_CSR0_H_STATUSPKT
1053550a7375SFelipe Balbi 					| MUSB_CSR0_H_REQPKT;
1054550a7375SFelipe Balbi 			else
1055550a7375SFelipe Balbi 				csr = MUSB_CSR0_H_STATUSPKT
1056550a7375SFelipe Balbi 					| MUSB_CSR0_TXPKTRDY;
1057550a7375SFelipe Balbi 
1058550a7375SFelipe Balbi 			/* flag status stage */
1059550a7375SFelipe Balbi 			musb->ep0_stage = MUSB_EP0_STATUS;
1060550a7375SFelipe Balbi 
10615c8a86e1SFelipe Balbi 			dev_dbg(musb->controller, "ep0 STATUS, csr %04x\n", csr);
1062550a7375SFelipe Balbi 
1063550a7375SFelipe Balbi 		}
1064550a7375SFelipe Balbi 		musb_writew(epio, MUSB_CSR0, csr);
1065550a7375SFelipe Balbi 		retval = IRQ_HANDLED;
1066550a7375SFelipe Balbi 	} else
1067550a7375SFelipe Balbi 		musb->ep0_stage = MUSB_EP0_IDLE;
1068550a7375SFelipe Balbi 
1069550a7375SFelipe Balbi 	/* call completion handler if done */
1070550a7375SFelipe Balbi 	if (complete)
1071550a7375SFelipe Balbi 		musb_advance_schedule(musb, urb, hw_ep, 1);
1072550a7375SFelipe Balbi done:
1073550a7375SFelipe Balbi 	return retval;
1074550a7375SFelipe Balbi }
1075550a7375SFelipe Balbi 
1076550a7375SFelipe Balbi 
1077550a7375SFelipe Balbi #ifdef CONFIG_USB_INVENTRA_DMA
1078550a7375SFelipe Balbi 
1079550a7375SFelipe Balbi /* Host side TX (OUT) using Mentor DMA works as follows:
1080550a7375SFelipe Balbi 	submit_urb ->
1081550a7375SFelipe Balbi 		- if queue was empty, Program Endpoint
1082550a7375SFelipe Balbi 		- ... which starts DMA to fifo in mode 1 or 0
1083550a7375SFelipe Balbi 
1084550a7375SFelipe Balbi 	DMA Isr (transfer complete) -> TxAvail()
1085550a7375SFelipe Balbi 		- Stop DMA (~DmaEnab)	(<--- Alert ... currently happens
1086550a7375SFelipe Balbi 					only in musb_cleanup_urb)
1087550a7375SFelipe Balbi 		- TxPktRdy has to be set in mode 0 or for
1088550a7375SFelipe Balbi 			short packets in mode 1.
1089550a7375SFelipe Balbi */
1090550a7375SFelipe Balbi 
1091550a7375SFelipe Balbi #endif
1092550a7375SFelipe Balbi 
1093550a7375SFelipe Balbi /* Service a Tx-Available or dma completion irq for the endpoint */
1094550a7375SFelipe Balbi void musb_host_tx(struct musb *musb, u8 epnum)
1095550a7375SFelipe Balbi {
1096550a7375SFelipe Balbi 	int			pipe;
1097550a7375SFelipe Balbi 	bool			done = false;
1098550a7375SFelipe Balbi 	u16			tx_csr;
10996b6e9710SSergei Shtylyov 	size_t			length = 0;
11006b6e9710SSergei Shtylyov 	size_t			offset = 0;
1101550a7375SFelipe Balbi 	struct musb_hw_ep	*hw_ep = musb->endpoints + epnum;
1102550a7375SFelipe Balbi 	void __iomem		*epio = hw_ep->regs;
11033e5c6dc7SSergei Shtylyov 	struct musb_qh		*qh = hw_ep->out_qh;
11043e5c6dc7SSergei Shtylyov 	struct urb		*urb = next_urb(qh);
1105550a7375SFelipe Balbi 	u32			status = 0;
1106550a7375SFelipe Balbi 	void __iomem		*mbase = musb->mregs;
1107550a7375SFelipe Balbi 	struct dma_channel	*dma;
1108f8afbf7fST. S., Anil Kumar 	bool			transfer_pending = false;
1109550a7375SFelipe Balbi 
1110550a7375SFelipe Balbi 	musb_ep_select(mbase, epnum);
1111550a7375SFelipe Balbi 	tx_csr = musb_readw(epio, MUSB_TXCSR);
1112550a7375SFelipe Balbi 
1113550a7375SFelipe Balbi 	/* with CPPI, DMA sometimes triggers "extra" irqs */
1114550a7375SFelipe Balbi 	if (!urb) {
11155c8a86e1SFelipe Balbi 		dev_dbg(musb->controller, "extra TX%d ready, csr %04x\n", epnum, tx_csr);
11166b6e9710SSergei Shtylyov 		return;
1117550a7375SFelipe Balbi 	}
1118550a7375SFelipe Balbi 
1119550a7375SFelipe Balbi 	pipe = urb->pipe;
1120550a7375SFelipe Balbi 	dma = is_dma_capable() ? hw_ep->tx_channel : NULL;
11215c8a86e1SFelipe Balbi 	dev_dbg(musb->controller, "OUT/TX%d end, csr %04x%s\n", epnum, tx_csr,
1122550a7375SFelipe Balbi 			dma ? ", dma" : "");
1123550a7375SFelipe Balbi 
1124550a7375SFelipe Balbi 	/* check for errors */
1125550a7375SFelipe Balbi 	if (tx_csr & MUSB_TXCSR_H_RXSTALL) {
1126550a7375SFelipe Balbi 		/* dma was disabled, fifo flushed */
11275c8a86e1SFelipe Balbi 		dev_dbg(musb->controller, "TX end %d stall\n", epnum);
1128550a7375SFelipe Balbi 
1129550a7375SFelipe Balbi 		/* stall; record URB status */
1130550a7375SFelipe Balbi 		status = -EPIPE;
1131550a7375SFelipe Balbi 
1132550a7375SFelipe Balbi 	} else if (tx_csr & MUSB_TXCSR_H_ERROR) {
1133550a7375SFelipe Balbi 		/* (NON-ISO) dma was disabled, fifo flushed */
11345c8a86e1SFelipe Balbi 		dev_dbg(musb->controller, "TX 3strikes on ep=%d\n", epnum);
1135550a7375SFelipe Balbi 
1136550a7375SFelipe Balbi 		status = -ETIMEDOUT;
1137550a7375SFelipe Balbi 
1138550a7375SFelipe Balbi 	} else if (tx_csr & MUSB_TXCSR_H_NAKTIMEOUT) {
11395c8a86e1SFelipe Balbi 		dev_dbg(musb->controller, "TX end=%d device not responding\n", epnum);
1140550a7375SFelipe Balbi 
1141550a7375SFelipe Balbi 		/* NOTE:  this code path would be a good place to PAUSE a
1142550a7375SFelipe Balbi 		 * transfer, if there's some other (nonperiodic) tx urb
1143550a7375SFelipe Balbi 		 * that could use this fifo.  (dma complicates it...)
11441e0320f0SAjay Kumar Gupta 		 * That's already done for bulk RX transfers.
1145550a7375SFelipe Balbi 		 *
1146550a7375SFelipe Balbi 		 * if (bulk && qh->ring.next != &musb->out_bulk), then
1147550a7375SFelipe Balbi 		 * we have a candidate... NAKing is *NOT* an error
1148550a7375SFelipe Balbi 		 */
1149550a7375SFelipe Balbi 		musb_ep_select(mbase, epnum);
1150550a7375SFelipe Balbi 		musb_writew(epio, MUSB_TXCSR,
1151550a7375SFelipe Balbi 				MUSB_TXCSR_H_WZC_BITS
1152550a7375SFelipe Balbi 				| MUSB_TXCSR_TXPKTRDY);
11536b6e9710SSergei Shtylyov 		return;
1154550a7375SFelipe Balbi 	}
1155550a7375SFelipe Balbi 
1156550a7375SFelipe Balbi 	if (status) {
1157550a7375SFelipe Balbi 		if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) {
1158550a7375SFelipe Balbi 			dma->status = MUSB_DMA_STATUS_CORE_ABORT;
1159550a7375SFelipe Balbi 			(void) musb->dma_controller->channel_abort(dma);
1160550a7375SFelipe Balbi 		}
1161550a7375SFelipe Balbi 
1162550a7375SFelipe Balbi 		/* do the proper sequence to abort the transfer in the
1163550a7375SFelipe Balbi 		 * usb core; the dma engine should already be stopped.
1164550a7375SFelipe Balbi 		 */
1165550a7375SFelipe Balbi 		musb_h_tx_flush_fifo(hw_ep);
1166550a7375SFelipe Balbi 		tx_csr &= ~(MUSB_TXCSR_AUTOSET
1167550a7375SFelipe Balbi 				| MUSB_TXCSR_DMAENAB
1168550a7375SFelipe Balbi 				| MUSB_TXCSR_H_ERROR
1169550a7375SFelipe Balbi 				| MUSB_TXCSR_H_RXSTALL
1170550a7375SFelipe Balbi 				| MUSB_TXCSR_H_NAKTIMEOUT
1171550a7375SFelipe Balbi 				);
1172550a7375SFelipe Balbi 
1173550a7375SFelipe Balbi 		musb_ep_select(mbase, epnum);
1174550a7375SFelipe Balbi 		musb_writew(epio, MUSB_TXCSR, tx_csr);
1175550a7375SFelipe Balbi 		/* REVISIT may need to clear FLUSHFIFO ... */
1176550a7375SFelipe Balbi 		musb_writew(epio, MUSB_TXCSR, tx_csr);
1177550a7375SFelipe Balbi 		musb_writeb(epio, MUSB_TXINTERVAL, 0);
1178550a7375SFelipe Balbi 
1179550a7375SFelipe Balbi 		done = true;
1180550a7375SFelipe Balbi 	}
1181550a7375SFelipe Balbi 
1182550a7375SFelipe Balbi 	/* second cppi case */
1183550a7375SFelipe Balbi 	if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) {
11845c8a86e1SFelipe Balbi 		dev_dbg(musb->controller, "extra TX%d ready, csr %04x\n", epnum, tx_csr);
11856b6e9710SSergei Shtylyov 		return;
1186550a7375SFelipe Balbi 	}
1187550a7375SFelipe Balbi 
1188c7bbc056SSergei Shtylyov 	if (is_dma_capable() && dma && !status) {
1189c7bbc056SSergei Shtylyov 		/*
1190c7bbc056SSergei Shtylyov 		 * DMA has completed.  But if we're using DMA mode 1 (multi
1191c7bbc056SSergei Shtylyov 		 * packet DMA), we need a terminal TXPKTRDY interrupt before
1192c7bbc056SSergei Shtylyov 		 * we can consider this transfer completed, lest we trash
1193c7bbc056SSergei Shtylyov 		 * its last packet when writing the next URB's data.  So we
1194c7bbc056SSergei Shtylyov 		 * switch back to mode 0 to get that interrupt; we'll come
1195c7bbc056SSergei Shtylyov 		 * back here once it happens.
1196c7bbc056SSergei Shtylyov 		 */
1197c7bbc056SSergei Shtylyov 		if (tx_csr & MUSB_TXCSR_DMAMODE) {
1198c7bbc056SSergei Shtylyov 			/*
1199c7bbc056SSergei Shtylyov 			 * We shouldn't clear DMAMODE with DMAENAB set; so
1200c7bbc056SSergei Shtylyov 			 * clear them in a safe order.  That should be OK
1201c7bbc056SSergei Shtylyov 			 * once TXPKTRDY has been set (and I've never seen
1202c7bbc056SSergei Shtylyov 			 * it being 0 at this moment -- DMA interrupt latency
1203c7bbc056SSergei Shtylyov 			 * is significant) but if it hasn't been then we have
1204c7bbc056SSergei Shtylyov 			 * no choice but to stop being polite and ignore the
1205c7bbc056SSergei Shtylyov 			 * programmer's guide... :-)
1206c7bbc056SSergei Shtylyov 			 *
1207c7bbc056SSergei Shtylyov 			 * Note that we must write TXCSR with TXPKTRDY cleared
1208c7bbc056SSergei Shtylyov 			 * in order not to re-trigger the packet send (this bit
1209c7bbc056SSergei Shtylyov 			 * can't be cleared by CPU), and there's another caveat:
1210c7bbc056SSergei Shtylyov 			 * TXPKTRDY may be set shortly and then cleared in the
1211c7bbc056SSergei Shtylyov 			 * double-buffered FIFO mode, so we do an extra TXCSR
1212c7bbc056SSergei Shtylyov 			 * read for debouncing...
1213c7bbc056SSergei Shtylyov 			 */
1214c7bbc056SSergei Shtylyov 			tx_csr &= musb_readw(epio, MUSB_TXCSR);
1215c7bbc056SSergei Shtylyov 			if (tx_csr & MUSB_TXCSR_TXPKTRDY) {
1216c7bbc056SSergei Shtylyov 				tx_csr &= ~(MUSB_TXCSR_DMAENAB |
1217c7bbc056SSergei Shtylyov 					    MUSB_TXCSR_TXPKTRDY);
1218c7bbc056SSergei Shtylyov 				musb_writew(epio, MUSB_TXCSR,
1219c7bbc056SSergei Shtylyov 					    tx_csr | MUSB_TXCSR_H_WZC_BITS);
1220c7bbc056SSergei Shtylyov 			}
1221c7bbc056SSergei Shtylyov 			tx_csr &= ~(MUSB_TXCSR_DMAMODE |
1222c7bbc056SSergei Shtylyov 				    MUSB_TXCSR_TXPKTRDY);
1223c7bbc056SSergei Shtylyov 			musb_writew(epio, MUSB_TXCSR,
1224c7bbc056SSergei Shtylyov 				    tx_csr | MUSB_TXCSR_H_WZC_BITS);
1225c7bbc056SSergei Shtylyov 
1226c7bbc056SSergei Shtylyov 			/*
1227c7bbc056SSergei Shtylyov 			 * There is no guarantee that we'll get an interrupt
1228c7bbc056SSergei Shtylyov 			 * after clearing DMAMODE as we might have done this
1229c7bbc056SSergei Shtylyov 			 * too late (after TXPKTRDY was cleared by controller).
1230c7bbc056SSergei Shtylyov 			 * Re-read TXCSR as we have spoiled its previous value.
1231c7bbc056SSergei Shtylyov 			 */
1232c7bbc056SSergei Shtylyov 			tx_csr = musb_readw(epio, MUSB_TXCSR);
1233c7bbc056SSergei Shtylyov 		}
1234c7bbc056SSergei Shtylyov 
1235c7bbc056SSergei Shtylyov 		/*
1236c7bbc056SSergei Shtylyov 		 * We may get here from a DMA completion or TXPKTRDY interrupt.
1237c7bbc056SSergei Shtylyov 		 * In any case, we must check the FIFO status here and bail out
1238c7bbc056SSergei Shtylyov 		 * only if the FIFO still has data -- that should prevent the
1239c7bbc056SSergei Shtylyov 		 * "missed" TXPKTRDY interrupts and deal with double-buffered
1240c7bbc056SSergei Shtylyov 		 * FIFO mode too...
1241c7bbc056SSergei Shtylyov 		 */
1242c7bbc056SSergei Shtylyov 		if (tx_csr & (MUSB_TXCSR_FIFONOTEMPTY | MUSB_TXCSR_TXPKTRDY)) {
12435c8a86e1SFelipe Balbi 			dev_dbg(musb->controller, "DMA complete but packet still in FIFO, "
1244c7bbc056SSergei Shtylyov 			    "CSR %04x\n", tx_csr);
1245c7bbc056SSergei Shtylyov 			return;
1246c7bbc056SSergei Shtylyov 		}
1247c7bbc056SSergei Shtylyov 	}
1248c7bbc056SSergei Shtylyov 
1249550a7375SFelipe Balbi 	if (!status || dma || usb_pipeisoc(pipe)) {
1250550a7375SFelipe Balbi 		if (dma)
12516b6e9710SSergei Shtylyov 			length = dma->actual_len;
1252550a7375SFelipe Balbi 		else
12536b6e9710SSergei Shtylyov 			length = qh->segsize;
12546b6e9710SSergei Shtylyov 		qh->offset += length;
1255550a7375SFelipe Balbi 
1256550a7375SFelipe Balbi 		if (usb_pipeisoc(pipe)) {
1257550a7375SFelipe Balbi 			struct usb_iso_packet_descriptor	*d;
1258550a7375SFelipe Balbi 
1259550a7375SFelipe Balbi 			d = urb->iso_frame_desc + qh->iso_idx;
12606b6e9710SSergei Shtylyov 			d->actual_length = length;
12616b6e9710SSergei Shtylyov 			d->status = status;
1262550a7375SFelipe Balbi 			if (++qh->iso_idx >= urb->number_of_packets) {
1263550a7375SFelipe Balbi 				done = true;
1264550a7375SFelipe Balbi 			} else {
1265550a7375SFelipe Balbi 				d++;
12666b6e9710SSergei Shtylyov 				offset = d->offset;
12676b6e9710SSergei Shtylyov 				length = d->length;
1268550a7375SFelipe Balbi 			}
1269f8afbf7fST. S., Anil Kumar 		} else if (dma && urb->transfer_buffer_length == qh->offset) {
1270550a7375SFelipe Balbi 			done = true;
1271550a7375SFelipe Balbi 		} else {
1272550a7375SFelipe Balbi 			/* see if we need to send more data, or ZLP */
1273550a7375SFelipe Balbi 			if (qh->segsize < qh->maxpacket)
1274550a7375SFelipe Balbi 				done = true;
1275550a7375SFelipe Balbi 			else if (qh->offset == urb->transfer_buffer_length
1276550a7375SFelipe Balbi 					&& !(urb->transfer_flags
1277550a7375SFelipe Balbi 						& URB_ZERO_PACKET))
1278550a7375SFelipe Balbi 				done = true;
1279550a7375SFelipe Balbi 			if (!done) {
12806b6e9710SSergei Shtylyov 				offset = qh->offset;
12816b6e9710SSergei Shtylyov 				length = urb->transfer_buffer_length - offset;
1282f8afbf7fST. S., Anil Kumar 				transfer_pending = true;
1283550a7375SFelipe Balbi 			}
1284550a7375SFelipe Balbi 		}
1285550a7375SFelipe Balbi 	}
1286550a7375SFelipe Balbi 
1287550a7375SFelipe Balbi 	/* urb->status != -EINPROGRESS means request has been faulted,
1288550a7375SFelipe Balbi 	 * so we must abort this transfer after cleanup
1289550a7375SFelipe Balbi 	 */
1290550a7375SFelipe Balbi 	if (urb->status != -EINPROGRESS) {
1291550a7375SFelipe Balbi 		done = true;
1292550a7375SFelipe Balbi 		if (status == 0)
1293550a7375SFelipe Balbi 			status = urb->status;
1294550a7375SFelipe Balbi 	}
1295550a7375SFelipe Balbi 
1296550a7375SFelipe Balbi 	if (done) {
1297550a7375SFelipe Balbi 		/* set status */
1298550a7375SFelipe Balbi 		urb->status = status;
1299550a7375SFelipe Balbi 		urb->actual_length = qh->offset;
1300550a7375SFelipe Balbi 		musb_advance_schedule(musb, urb, hw_ep, USB_DIR_OUT);
13016b6e9710SSergei Shtylyov 		return;
1302f8afbf7fST. S., Anil Kumar 	} else if ((usb_pipeisoc(pipe) || transfer_pending) && dma) {
13036b6e9710SSergei Shtylyov 		if (musb_tx_dma_program(musb->dma_controller, hw_ep, qh, urb,
1304dfeffa53SAjay Kumar Gupta 				offset, length)) {
1305dfeffa53SAjay Kumar Gupta 			if (is_cppi_enabled() || tusb_dma_omap())
1306dfeffa53SAjay Kumar Gupta 				musb_h_tx_dma_start(hw_ep);
13076b6e9710SSergei Shtylyov 			return;
1308dfeffa53SAjay Kumar Gupta 		}
13096b6e9710SSergei Shtylyov 	} else	if (tx_csr & MUSB_TXCSR_DMAENAB) {
13105c8a86e1SFelipe Balbi 		dev_dbg(musb->controller, "not complete, but DMA enabled?\n");
13116b6e9710SSergei Shtylyov 		return;
13126b6e9710SSergei Shtylyov 	}
1313550a7375SFelipe Balbi 
13146b6e9710SSergei Shtylyov 	/*
13156b6e9710SSergei Shtylyov 	 * PIO: start next packet in this URB.
13166b6e9710SSergei Shtylyov 	 *
13176b6e9710SSergei Shtylyov 	 * REVISIT: some docs say that when hw_ep->tx_double_buffered,
13186b6e9710SSergei Shtylyov 	 * (and presumably, FIFO is not half-full) we should write *two*
13196b6e9710SSergei Shtylyov 	 * packets before updating TXCSR; other docs disagree...
1320550a7375SFelipe Balbi 	 */
13216b6e9710SSergei Shtylyov 	if (length > qh->maxpacket)
13226b6e9710SSergei Shtylyov 		length = qh->maxpacket;
1323496dda70SMaulik Mankad 	/* Unmap the buffer so that CPU can use it */
1324c8cf203aSRobert Morell 	usb_hcd_unmap_urb_for_dma(musb_to_hcd(musb), urb);
13256b6e9710SSergei Shtylyov 	musb_write_fifo(hw_ep, length, urb->transfer_buffer + offset);
13266b6e9710SSergei Shtylyov 	qh->segsize = length;
1327550a7375SFelipe Balbi 
1328550a7375SFelipe Balbi 	musb_ep_select(mbase, epnum);
1329550a7375SFelipe Balbi 	musb_writew(epio, MUSB_TXCSR,
1330550a7375SFelipe Balbi 			MUSB_TXCSR_H_WZC_BITS | MUSB_TXCSR_TXPKTRDY);
1331550a7375SFelipe Balbi }
1332550a7375SFelipe Balbi 
1333550a7375SFelipe Balbi 
1334550a7375SFelipe Balbi #ifdef CONFIG_USB_INVENTRA_DMA
1335550a7375SFelipe Balbi 
1336550a7375SFelipe Balbi /* Host side RX (IN) using Mentor DMA works as follows:
1337550a7375SFelipe Balbi 	submit_urb ->
1338550a7375SFelipe Balbi 		- if queue was empty, ProgramEndpoint
1339550a7375SFelipe Balbi 		- first IN token is sent out (by setting ReqPkt)
1340550a7375SFelipe Balbi 	LinuxIsr -> RxReady()
1341550a7375SFelipe Balbi 	/\	=> first packet is received
1342550a7375SFelipe Balbi 	|	- Set in mode 0 (DmaEnab, ~ReqPkt)
1343550a7375SFelipe Balbi 	|		-> DMA Isr (transfer complete) -> RxReady()
1344550a7375SFelipe Balbi 	|		    - Ack receive (~RxPktRdy), turn off DMA (~DmaEnab)
1345550a7375SFelipe Balbi 	|		    - if urb not complete, send next IN token (ReqPkt)
1346550a7375SFelipe Balbi 	|			   |		else complete urb.
1347550a7375SFelipe Balbi 	|			   |
1348550a7375SFelipe Balbi 	---------------------------
1349550a7375SFelipe Balbi  *
1350550a7375SFelipe Balbi  * Nuances of mode 1:
1351550a7375SFelipe Balbi  *	For short packets, no ack (+RxPktRdy) is sent automatically
1352550a7375SFelipe Balbi  *	(even if AutoClear is ON)
1353550a7375SFelipe Balbi  *	For full packets, ack (~RxPktRdy) and next IN token (+ReqPkt) is sent
1354550a7375SFelipe Balbi  *	automatically => major problem, as collecting the next packet becomes
1355550a7375SFelipe Balbi  *	difficult. Hence mode 1 is not used.
1356550a7375SFelipe Balbi  *
1357550a7375SFelipe Balbi  * REVISIT
1358550a7375SFelipe Balbi  *	All we care about at this driver level is that
1359550a7375SFelipe Balbi  *       (a) all URBs terminate with REQPKT cleared and fifo(s) empty;
1360550a7375SFelipe Balbi  *       (b) termination conditions are: short RX, or buffer full;
1361550a7375SFelipe Balbi  *       (c) fault modes include
1362550a7375SFelipe Balbi  *           - iff URB_SHORT_NOT_OK, short RX status is -EREMOTEIO.
1363550a7375SFelipe Balbi  *             (and that endpoint's dma queue stops immediately)
1364550a7375SFelipe Balbi  *           - overflow (full, PLUS more bytes in the terminal packet)
1365550a7375SFelipe Balbi  *
1366550a7375SFelipe Balbi  *	So for example, usb-storage sets URB_SHORT_NOT_OK, and would
1367550a7375SFelipe Balbi  *	thus be a great candidate for using mode 1 ... for all but the
1368550a7375SFelipe Balbi  *	last packet of one URB's transfer.
1369550a7375SFelipe Balbi  */
1370550a7375SFelipe Balbi 
1371550a7375SFelipe Balbi #endif
1372550a7375SFelipe Balbi 
13731e0320f0SAjay Kumar Gupta /* Schedule next QH from musb->in_bulk and move the current qh to
13741e0320f0SAjay Kumar Gupta  * the end; avoids starvation for other endpoints.
13751e0320f0SAjay Kumar Gupta  */
13761e0320f0SAjay Kumar Gupta static void musb_bulk_rx_nak_timeout(struct musb *musb, struct musb_hw_ep *ep)
13771e0320f0SAjay Kumar Gupta {
13781e0320f0SAjay Kumar Gupta 	struct dma_channel	*dma;
13791e0320f0SAjay Kumar Gupta 	struct urb		*urb;
13801e0320f0SAjay Kumar Gupta 	void __iomem		*mbase = musb->mregs;
13811e0320f0SAjay Kumar Gupta 	void __iomem		*epio = ep->regs;
13821e0320f0SAjay Kumar Gupta 	struct musb_qh		*cur_qh, *next_qh;
13831e0320f0SAjay Kumar Gupta 	u16			rx_csr;
13841e0320f0SAjay Kumar Gupta 
13851e0320f0SAjay Kumar Gupta 	musb_ep_select(mbase, ep->epnum);
13861e0320f0SAjay Kumar Gupta 	dma = is_dma_capable() ? ep->rx_channel : NULL;
13871e0320f0SAjay Kumar Gupta 
13881e0320f0SAjay Kumar Gupta 	/* clear nak timeout bit */
13891e0320f0SAjay Kumar Gupta 	rx_csr = musb_readw(epio, MUSB_RXCSR);
13901e0320f0SAjay Kumar Gupta 	rx_csr |= MUSB_RXCSR_H_WZC_BITS;
13911e0320f0SAjay Kumar Gupta 	rx_csr &= ~MUSB_RXCSR_DATAERROR;
13921e0320f0SAjay Kumar Gupta 	musb_writew(epio, MUSB_RXCSR, rx_csr);
13931e0320f0SAjay Kumar Gupta 
13941e0320f0SAjay Kumar Gupta 	cur_qh = first_qh(&musb->in_bulk);
13951e0320f0SAjay Kumar Gupta 	if (cur_qh) {
13961e0320f0SAjay Kumar Gupta 		urb = next_urb(cur_qh);
13971e0320f0SAjay Kumar Gupta 		if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) {
13981e0320f0SAjay Kumar Gupta 			dma->status = MUSB_DMA_STATUS_CORE_ABORT;
13991e0320f0SAjay Kumar Gupta 			musb->dma_controller->channel_abort(dma);
14001e0320f0SAjay Kumar Gupta 			urb->actual_length += dma->actual_len;
14011e0320f0SAjay Kumar Gupta 			dma->actual_len = 0L;
14021e0320f0SAjay Kumar Gupta 		}
1403846099a6SSergei Shtylyov 		musb_save_toggle(cur_qh, 1, urb);
14041e0320f0SAjay Kumar Gupta 
14051e0320f0SAjay Kumar Gupta 		/* move cur_qh to end of queue */
14061e0320f0SAjay Kumar Gupta 		list_move_tail(&cur_qh->ring, &musb->in_bulk);
14071e0320f0SAjay Kumar Gupta 
14081e0320f0SAjay Kumar Gupta 		/* get the next qh from musb->in_bulk */
14091e0320f0SAjay Kumar Gupta 		next_qh = first_qh(&musb->in_bulk);
14101e0320f0SAjay Kumar Gupta 
14111e0320f0SAjay Kumar Gupta 		/* set rx_reinit and schedule the next qh */
14121e0320f0SAjay Kumar Gupta 		ep->rx_reinit = 1;
14131e0320f0SAjay Kumar Gupta 		musb_start_urb(musb, 1, next_qh);
14141e0320f0SAjay Kumar Gupta 	}
14151e0320f0SAjay Kumar Gupta }
14161e0320f0SAjay Kumar Gupta 
1417550a7375SFelipe Balbi /*
1418550a7375SFelipe Balbi  * Service an RX interrupt for the given IN endpoint; docs cover bulk, iso,
1419550a7375SFelipe Balbi  * and high-bandwidth IN transfer cases.
1420550a7375SFelipe Balbi  */
1421550a7375SFelipe Balbi void musb_host_rx(struct musb *musb, u8 epnum)
1422550a7375SFelipe Balbi {
1423550a7375SFelipe Balbi 	struct urb		*urb;
1424550a7375SFelipe Balbi 	struct musb_hw_ep	*hw_ep = musb->endpoints + epnum;
1425550a7375SFelipe Balbi 	void __iomem		*epio = hw_ep->regs;
1426550a7375SFelipe Balbi 	struct musb_qh		*qh = hw_ep->in_qh;
1427550a7375SFelipe Balbi 	size_t			xfer_len;
1428550a7375SFelipe Balbi 	void __iomem		*mbase = musb->mregs;
1429550a7375SFelipe Balbi 	int			pipe;
1430550a7375SFelipe Balbi 	u16			rx_csr, val;
1431550a7375SFelipe Balbi 	bool			iso_err = false;
1432550a7375SFelipe Balbi 	bool			done = false;
1433550a7375SFelipe Balbi 	u32			status;
1434550a7375SFelipe Balbi 	struct dma_channel	*dma;
1435550a7375SFelipe Balbi 
1436550a7375SFelipe Balbi 	musb_ep_select(mbase, epnum);
1437550a7375SFelipe Balbi 
1438550a7375SFelipe Balbi 	urb = next_urb(qh);
1439550a7375SFelipe Balbi 	dma = is_dma_capable() ? hw_ep->rx_channel : NULL;
1440550a7375SFelipe Balbi 	status = 0;
1441550a7375SFelipe Balbi 	xfer_len = 0;
1442550a7375SFelipe Balbi 
1443550a7375SFelipe Balbi 	rx_csr = musb_readw(epio, MUSB_RXCSR);
1444550a7375SFelipe Balbi 	val = rx_csr;
1445550a7375SFelipe Balbi 
1446550a7375SFelipe Balbi 	if (unlikely(!urb)) {
1447550a7375SFelipe Balbi 		/* REVISIT -- THIS SHOULD NEVER HAPPEN ... but, at least
1448550a7375SFelipe Balbi 		 * usbtest #11 (unlinks) triggers it regularly, sometimes
1449550a7375SFelipe Balbi 		 * with fifo full.  (Only with DMA??)
1450550a7375SFelipe Balbi 		 */
14515c8a86e1SFelipe Balbi 		dev_dbg(musb->controller, "BOGUS RX%d ready, csr %04x, count %d\n", epnum, val,
1452550a7375SFelipe Balbi 			musb_readw(epio, MUSB_RXCOUNT));
1453550a7375SFelipe Balbi 		musb_h_flush_rxfifo(hw_ep, MUSB_RXCSR_CLRDATATOG);
1454550a7375SFelipe Balbi 		return;
1455550a7375SFelipe Balbi 	}
1456550a7375SFelipe Balbi 
1457550a7375SFelipe Balbi 	pipe = urb->pipe;
1458550a7375SFelipe Balbi 
14595c8a86e1SFelipe Balbi 	dev_dbg(musb->controller, "<== hw %d rxcsr %04x, urb actual %d (+dma %zu)\n",
1460550a7375SFelipe Balbi 		epnum, rx_csr, urb->actual_length,
1461550a7375SFelipe Balbi 		dma ? dma->actual_len : 0);
1462550a7375SFelipe Balbi 
1463550a7375SFelipe Balbi 	/* check for errors, concurrent stall & unlink is not really
1464550a7375SFelipe Balbi 	 * handled yet! */
1465550a7375SFelipe Balbi 	if (rx_csr & MUSB_RXCSR_H_RXSTALL) {
14665c8a86e1SFelipe Balbi 		dev_dbg(musb->controller, "RX end %d STALL\n", epnum);
1467550a7375SFelipe Balbi 
1468550a7375SFelipe Balbi 		/* stall; record URB status */
1469550a7375SFelipe Balbi 		status = -EPIPE;
1470550a7375SFelipe Balbi 
1471550a7375SFelipe Balbi 	} else if (rx_csr & MUSB_RXCSR_H_ERROR) {
14725c8a86e1SFelipe Balbi 		dev_dbg(musb->controller, "end %d RX proto error\n", epnum);
1473550a7375SFelipe Balbi 
1474550a7375SFelipe Balbi 		status = -EPROTO;
1475550a7375SFelipe Balbi 		musb_writeb(epio, MUSB_RXINTERVAL, 0);
1476550a7375SFelipe Balbi 
1477550a7375SFelipe Balbi 	} else if (rx_csr & MUSB_RXCSR_DATAERROR) {
1478550a7375SFelipe Balbi 
1479550a7375SFelipe Balbi 		if (USB_ENDPOINT_XFER_ISOC != qh->type) {
14805c8a86e1SFelipe Balbi 			dev_dbg(musb->controller, "RX end %d NAK timeout\n", epnum);
14811e0320f0SAjay Kumar Gupta 
14821e0320f0SAjay Kumar Gupta 			/* NOTE: NAKing is *NOT* an error, so we want to
14831e0320f0SAjay Kumar Gupta 			 * continue.  Except ... if there's a request for
14841e0320f0SAjay Kumar Gupta 			 * another QH, use that instead of starving it.
14851e0320f0SAjay Kumar Gupta 			 *
14861e0320f0SAjay Kumar Gupta 			 * Devices like Ethernet and serial adapters keep
14871e0320f0SAjay Kumar Gupta 			 * reads posted at all times, which will starve
14881e0320f0SAjay Kumar Gupta 			 * other devices without this logic.
14891e0320f0SAjay Kumar Gupta 			 */
14901e0320f0SAjay Kumar Gupta 			if (usb_pipebulk(urb->pipe)
14911e0320f0SAjay Kumar Gupta 					&& qh->mux == 1
14921e0320f0SAjay Kumar Gupta 					&& !list_is_singular(&musb->in_bulk)) {
14931e0320f0SAjay Kumar Gupta 				musb_bulk_rx_nak_timeout(musb, hw_ep);
14941e0320f0SAjay Kumar Gupta 				return;
14951e0320f0SAjay Kumar Gupta 			}
1496550a7375SFelipe Balbi 			musb_ep_select(mbase, epnum);
14971e0320f0SAjay Kumar Gupta 			rx_csr |= MUSB_RXCSR_H_WZC_BITS;
14981e0320f0SAjay Kumar Gupta 			rx_csr &= ~MUSB_RXCSR_DATAERROR;
14991e0320f0SAjay Kumar Gupta 			musb_writew(epio, MUSB_RXCSR, rx_csr);
1500550a7375SFelipe Balbi 
1501550a7375SFelipe Balbi 			goto finish;
1502550a7375SFelipe Balbi 		} else {
15035c8a86e1SFelipe Balbi 			dev_dbg(musb->controller, "RX end %d ISO data error\n", epnum);
1504550a7375SFelipe Balbi 			/* packet error reported later */
1505550a7375SFelipe Balbi 			iso_err = true;
1506550a7375SFelipe Balbi 		}
1507a483d706SAjay Kumar Gupta 	} else if (rx_csr & MUSB_RXCSR_INCOMPRX) {
15085c8a86e1SFelipe Balbi 		dev_dbg(musb->controller, "end %d high bandwidth incomplete ISO packet RX\n",
1509a483d706SAjay Kumar Gupta 				epnum);
1510a483d706SAjay Kumar Gupta 		status = -EPROTO;
1511550a7375SFelipe Balbi 	}
1512550a7375SFelipe Balbi 
1513550a7375SFelipe Balbi 	/* faults abort the transfer */
1514550a7375SFelipe Balbi 	if (status) {
1515550a7375SFelipe Balbi 		/* clean up dma and collect transfer count */
1516550a7375SFelipe Balbi 		if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) {
1517550a7375SFelipe Balbi 			dma->status = MUSB_DMA_STATUS_CORE_ABORT;
1518550a7375SFelipe Balbi 			(void) musb->dma_controller->channel_abort(dma);
1519550a7375SFelipe Balbi 			xfer_len = dma->actual_len;
1520550a7375SFelipe Balbi 		}
1521550a7375SFelipe Balbi 		musb_h_flush_rxfifo(hw_ep, MUSB_RXCSR_CLRDATATOG);
1522550a7375SFelipe Balbi 		musb_writeb(epio, MUSB_RXINTERVAL, 0);
1523550a7375SFelipe Balbi 		done = true;
1524550a7375SFelipe Balbi 		goto finish;
1525550a7375SFelipe Balbi 	}
1526550a7375SFelipe Balbi 
1527550a7375SFelipe Balbi 	if (unlikely(dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY)) {
1528550a7375SFelipe Balbi 		/* SHOULD NEVER HAPPEN ... but at least DaVinci has done it */
1529550a7375SFelipe Balbi 		ERR("RX%d dma busy, csr %04x\n", epnum, rx_csr);
1530550a7375SFelipe Balbi 		goto finish;
1531550a7375SFelipe Balbi 	}
1532550a7375SFelipe Balbi 
1533550a7375SFelipe Balbi 	/* thorough shutdown for now ... given more precise fault handling
1534550a7375SFelipe Balbi 	 * and better queueing support, we might keep a DMA pipeline going
1535550a7375SFelipe Balbi 	 * while processing this irq for earlier completions.
1536550a7375SFelipe Balbi 	 */
1537550a7375SFelipe Balbi 
1538550a7375SFelipe Balbi 	/* FIXME this is _way_ too much in-line logic for Mentor DMA */
1539550a7375SFelipe Balbi 
1540550a7375SFelipe Balbi #ifndef CONFIG_USB_INVENTRA_DMA
1541550a7375SFelipe Balbi 	if (rx_csr & MUSB_RXCSR_H_REQPKT)  {
1542550a7375SFelipe Balbi 		/* REVISIT this happened for a while on some short reads...
1543550a7375SFelipe Balbi 		 * the cleanup still needs investigation... looks bad...
1544550a7375SFelipe Balbi 		 * and also duplicates dma cleanup code above ... plus,
1545550a7375SFelipe Balbi 		 * shouldn't this be the "half full" double buffer case?
1546550a7375SFelipe Balbi 		 */
1547550a7375SFelipe Balbi 		if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) {
1548550a7375SFelipe Balbi 			dma->status = MUSB_DMA_STATUS_CORE_ABORT;
1549550a7375SFelipe Balbi 			(void) musb->dma_controller->channel_abort(dma);
1550550a7375SFelipe Balbi 			xfer_len = dma->actual_len;
1551550a7375SFelipe Balbi 			done = true;
1552550a7375SFelipe Balbi 		}
1553550a7375SFelipe Balbi 
15545c8a86e1SFelipe Balbi 		dev_dbg(musb->controller, "RXCSR%d %04x, reqpkt, len %zu%s\n", epnum, rx_csr,
1555550a7375SFelipe Balbi 				xfer_len, dma ? ", dma" : "");
1556550a7375SFelipe Balbi 		rx_csr &= ~MUSB_RXCSR_H_REQPKT;
1557550a7375SFelipe Balbi 
1558550a7375SFelipe Balbi 		musb_ep_select(mbase, epnum);
1559550a7375SFelipe Balbi 		musb_writew(epio, MUSB_RXCSR,
1560550a7375SFelipe Balbi 				MUSB_RXCSR_H_WZC_BITS | rx_csr);
1561550a7375SFelipe Balbi 	}
1562550a7375SFelipe Balbi #endif
1563550a7375SFelipe Balbi 	if (dma && (rx_csr & MUSB_RXCSR_DMAENAB)) {
1564550a7375SFelipe Balbi 		xfer_len = dma->actual_len;
1565550a7375SFelipe Balbi 
1566550a7375SFelipe Balbi 		val &= ~(MUSB_RXCSR_DMAENAB
1567550a7375SFelipe Balbi 			| MUSB_RXCSR_H_AUTOREQ
1568550a7375SFelipe Balbi 			| MUSB_RXCSR_AUTOCLEAR
1569550a7375SFelipe Balbi 			| MUSB_RXCSR_RXPKTRDY);
1570550a7375SFelipe Balbi 		musb_writew(hw_ep->regs, MUSB_RXCSR, val);
1571550a7375SFelipe Balbi 
1572550a7375SFelipe Balbi #ifdef CONFIG_USB_INVENTRA_DMA
1573f82a689fSAjay Kumar Gupta 		if (usb_pipeisoc(pipe)) {
1574f82a689fSAjay Kumar Gupta 			struct usb_iso_packet_descriptor *d;
1575f82a689fSAjay Kumar Gupta 
1576f82a689fSAjay Kumar Gupta 			d = urb->iso_frame_desc + qh->iso_idx;
1577f82a689fSAjay Kumar Gupta 			d->actual_length = xfer_len;
1578f82a689fSAjay Kumar Gupta 
1579f82a689fSAjay Kumar Gupta 			/* even if there was an error, we did the dma
1580f82a689fSAjay Kumar Gupta 			 * for iso_frame_desc->length
1581f82a689fSAjay Kumar Gupta 			 */
158272887c86SMárton Németh 			if (d->status != -EILSEQ && d->status != -EOVERFLOW)
1583f82a689fSAjay Kumar Gupta 				d->status = 0;
1584f82a689fSAjay Kumar Gupta 
1585f82a689fSAjay Kumar Gupta 			if (++qh->iso_idx >= urb->number_of_packets)
1586f82a689fSAjay Kumar Gupta 				done = true;
1587f82a689fSAjay Kumar Gupta 			else
1588f82a689fSAjay Kumar Gupta 				done = false;
1589f82a689fSAjay Kumar Gupta 
1590f82a689fSAjay Kumar Gupta 		} else  {
1591550a7375SFelipe Balbi 		/* done if urb buffer is full or short packet is recd */
1592550a7375SFelipe Balbi 		done = (urb->actual_length + xfer_len >=
1593550a7375SFelipe Balbi 				urb->transfer_buffer_length
1594550a7375SFelipe Balbi 			|| dma->actual_len < qh->maxpacket);
1595f82a689fSAjay Kumar Gupta 		}
1596550a7375SFelipe Balbi 
1597550a7375SFelipe Balbi 		/* send IN token for next packet, without AUTOREQ */
1598550a7375SFelipe Balbi 		if (!done) {
1599550a7375SFelipe Balbi 			val |= MUSB_RXCSR_H_REQPKT;
1600550a7375SFelipe Balbi 			musb_writew(epio, MUSB_RXCSR,
1601550a7375SFelipe Balbi 				MUSB_RXCSR_H_WZC_BITS | val);
1602550a7375SFelipe Balbi 		}
1603550a7375SFelipe Balbi 
16045c8a86e1SFelipe Balbi 		dev_dbg(musb->controller, "ep %d dma %s, rxcsr %04x, rxcount %d\n", epnum,
1605550a7375SFelipe Balbi 			done ? "off" : "reset",
1606550a7375SFelipe Balbi 			musb_readw(epio, MUSB_RXCSR),
1607550a7375SFelipe Balbi 			musb_readw(epio, MUSB_RXCOUNT));
1608550a7375SFelipe Balbi #else
1609550a7375SFelipe Balbi 		done = true;
1610550a7375SFelipe Balbi #endif
1611550a7375SFelipe Balbi 	} else if (urb->status == -EINPROGRESS) {
1612550a7375SFelipe Balbi 		/* if no errors, be sure a packet is ready for unloading */
1613550a7375SFelipe Balbi 		if (unlikely(!(rx_csr & MUSB_RXCSR_RXPKTRDY))) {
1614550a7375SFelipe Balbi 			status = -EPROTO;
1615550a7375SFelipe Balbi 			ERR("Rx interrupt with no errors or packet!\n");
1616550a7375SFelipe Balbi 
1617550a7375SFelipe Balbi 			/* FIXME this is another "SHOULD NEVER HAPPEN" */
1618550a7375SFelipe Balbi 
1619550a7375SFelipe Balbi /* SCRUB (RX) */
1620550a7375SFelipe Balbi 			/* do the proper sequence to abort the transfer */
1621550a7375SFelipe Balbi 			musb_ep_select(mbase, epnum);
1622550a7375SFelipe Balbi 			val &= ~MUSB_RXCSR_H_REQPKT;
1623550a7375SFelipe Balbi 			musb_writew(epio, MUSB_RXCSR, val);
1624550a7375SFelipe Balbi 			goto finish;
1625550a7375SFelipe Balbi 		}
1626550a7375SFelipe Balbi 
1627550a7375SFelipe Balbi 		/* we are expecting IN packets */
1628550a7375SFelipe Balbi #ifdef CONFIG_USB_INVENTRA_DMA
1629550a7375SFelipe Balbi 		if (dma) {
1630550a7375SFelipe Balbi 			struct dma_controller	*c;
1631550a7375SFelipe Balbi 			u16			rx_count;
1632f82a689fSAjay Kumar Gupta 			int			ret, length;
1633f82a689fSAjay Kumar Gupta 			dma_addr_t		buf;
1634550a7375SFelipe Balbi 
1635550a7375SFelipe Balbi 			rx_count = musb_readw(epio, MUSB_RXCOUNT);
1636550a7375SFelipe Balbi 
16375c8a86e1SFelipe Balbi 			dev_dbg(musb->controller, "RX%d count %d, buffer 0x%x len %d/%d\n",
1638550a7375SFelipe Balbi 					epnum, rx_count,
1639550a7375SFelipe Balbi 					urb->transfer_dma
1640550a7375SFelipe Balbi 						+ urb->actual_length,
1641550a7375SFelipe Balbi 					qh->offset,
1642550a7375SFelipe Balbi 					urb->transfer_buffer_length);
1643550a7375SFelipe Balbi 
1644550a7375SFelipe Balbi 			c = musb->dma_controller;
1645550a7375SFelipe Balbi 
1646f82a689fSAjay Kumar Gupta 			if (usb_pipeisoc(pipe)) {
16478b4959d6SFelipe Balbi 				int d_status = 0;
1648f82a689fSAjay Kumar Gupta 				struct usb_iso_packet_descriptor *d;
1649f82a689fSAjay Kumar Gupta 
1650f82a689fSAjay Kumar Gupta 				d = urb->iso_frame_desc + qh->iso_idx;
1651f82a689fSAjay Kumar Gupta 
1652f82a689fSAjay Kumar Gupta 				if (iso_err) {
16538b4959d6SFelipe Balbi 					d_status = -EILSEQ;
1654f82a689fSAjay Kumar Gupta 					urb->error_count++;
1655f82a689fSAjay Kumar Gupta 				}
1656f82a689fSAjay Kumar Gupta 				if (rx_count > d->length) {
16578b4959d6SFelipe Balbi 					if (d_status == 0) {
16588b4959d6SFelipe Balbi 						d_status = -EOVERFLOW;
1659f82a689fSAjay Kumar Gupta 						urb->error_count++;
1660f82a689fSAjay Kumar Gupta 					}
16615c8a86e1SFelipe Balbi 					dev_dbg(musb->controller, "** OVERFLOW %d into %d\n",\
1662f82a689fSAjay Kumar Gupta 					    rx_count, d->length);
1663f82a689fSAjay Kumar Gupta 
1664f82a689fSAjay Kumar Gupta 					length = d->length;
1665f82a689fSAjay Kumar Gupta 				} else
1666f82a689fSAjay Kumar Gupta 					length = rx_count;
16678b4959d6SFelipe Balbi 				d->status = d_status;
1668f82a689fSAjay Kumar Gupta 				buf = urb->transfer_dma + d->offset;
1669f82a689fSAjay Kumar Gupta 			} else {
1670f82a689fSAjay Kumar Gupta 				length = rx_count;
1671f82a689fSAjay Kumar Gupta 				buf = urb->transfer_dma +
1672f82a689fSAjay Kumar Gupta 						urb->actual_length;
1673f82a689fSAjay Kumar Gupta 			}
1674f82a689fSAjay Kumar Gupta 
1675550a7375SFelipe Balbi 			dma->desired_mode = 0;
1676550a7375SFelipe Balbi #ifdef USE_MODE1
1677550a7375SFelipe Balbi 			/* because of the issue below, mode 1 will
1678550a7375SFelipe Balbi 			 * only rarely behave with correct semantics.
1679550a7375SFelipe Balbi 			 */
1680550a7375SFelipe Balbi 			if ((urb->transfer_flags &
1681550a7375SFelipe Balbi 						URB_SHORT_NOT_OK)
1682550a7375SFelipe Balbi 				&& (urb->transfer_buffer_length -
1683550a7375SFelipe Balbi 						urb->actual_length)
1684550a7375SFelipe Balbi 					> qh->maxpacket)
1685550a7375SFelipe Balbi 				dma->desired_mode = 1;
1686f82a689fSAjay Kumar Gupta 			if (rx_count < hw_ep->max_packet_sz_rx) {
1687f82a689fSAjay Kumar Gupta 				length = rx_count;
1688ae926976SSonic Zhang 				dma->desired_mode = 0;
1689f82a689fSAjay Kumar Gupta 			} else {
1690f82a689fSAjay Kumar Gupta 				length = urb->transfer_buffer_length;
1691f82a689fSAjay Kumar Gupta 			}
1692550a7375SFelipe Balbi #endif
1693550a7375SFelipe Balbi 
1694550a7375SFelipe Balbi /* Disadvantage of using mode 1:
1695550a7375SFelipe Balbi  *	It's basically usable only for mass storage class; essentially all
1696550a7375SFelipe Balbi  *	other protocols also terminate transfers on short packets.
1697550a7375SFelipe Balbi  *
1698550a7375SFelipe Balbi  * Details:
1699550a7375SFelipe Balbi  *	An extra IN token is sent at the end of the transfer (due to AUTOREQ)
1700550a7375SFelipe Balbi  *	If you try to use mode 1 for (transfer_buffer_length - 512), and try
1701550a7375SFelipe Balbi  *	to use the extra IN token to grab the last packet using mode 0, then
1702550a7375SFelipe Balbi  *	the problem is that you cannot be sure when the device will send the
1703550a7375SFelipe Balbi  *	last packet and RxPktRdy set. Sometimes the packet is recd too soon
1704550a7375SFelipe Balbi  *	such that it gets lost when RxCSR is re-set at the end of the mode 1
1705550a7375SFelipe Balbi  *	transfer, while sometimes it is recd just a little late so that if you
1706550a7375SFelipe Balbi  *	try to configure for mode 0 soon after the mode 1 transfer is
1707550a7375SFelipe Balbi  *	completed, you will find rxcount 0. Okay, so you might think why not
1708550a7375SFelipe Balbi  *	wait for an interrupt when the pkt is recd. Well, you won't get any!
1709550a7375SFelipe Balbi  */
1710550a7375SFelipe Balbi 
1711550a7375SFelipe Balbi 			val = musb_readw(epio, MUSB_RXCSR);
1712550a7375SFelipe Balbi 			val &= ~MUSB_RXCSR_H_REQPKT;
1713550a7375SFelipe Balbi 
1714550a7375SFelipe Balbi 			if (dma->desired_mode == 0)
1715550a7375SFelipe Balbi 				val &= ~MUSB_RXCSR_H_AUTOREQ;
1716550a7375SFelipe Balbi 			else
1717550a7375SFelipe Balbi 				val |= MUSB_RXCSR_H_AUTOREQ;
1718a483d706SAjay Kumar Gupta 			val |= MUSB_RXCSR_DMAENAB;
1719a483d706SAjay Kumar Gupta 
1720a483d706SAjay Kumar Gupta 			/* autoclear shouldn't be set in high bandwidth */
1721a483d706SAjay Kumar Gupta 			if (qh->hb_mult == 1)
1722a483d706SAjay Kumar Gupta 				val |= MUSB_RXCSR_AUTOCLEAR;
1723550a7375SFelipe Balbi 
1724550a7375SFelipe Balbi 			musb_writew(epio, MUSB_RXCSR,
1725550a7375SFelipe Balbi 				MUSB_RXCSR_H_WZC_BITS | val);
1726550a7375SFelipe Balbi 
1727550a7375SFelipe Balbi 			/* REVISIT if when actual_length != 0,
1728550a7375SFelipe Balbi 			 * transfer_buffer_length needs to be
1729550a7375SFelipe Balbi 			 * adjusted first...
1730550a7375SFelipe Balbi 			 */
1731550a7375SFelipe Balbi 			ret = c->channel_program(
1732550a7375SFelipe Balbi 				dma, qh->maxpacket,
1733f82a689fSAjay Kumar Gupta 				dma->desired_mode, buf, length);
1734550a7375SFelipe Balbi 
1735550a7375SFelipe Balbi 			if (!ret) {
1736550a7375SFelipe Balbi 				c->channel_release(dma);
1737550a7375SFelipe Balbi 				hw_ep->rx_channel = NULL;
1738550a7375SFelipe Balbi 				dma = NULL;
1739*2ed9127cSMantesh Sarasetti 				val = musb_readw(epio, MUSB_RXCSR);
1740*2ed9127cSMantesh Sarasetti 				val &= ~(MUSB_RXCSR_DMAENAB
1741*2ed9127cSMantesh Sarasetti 					| MUSB_RXCSR_H_AUTOREQ
1742*2ed9127cSMantesh Sarasetti 					| MUSB_RXCSR_AUTOCLEAR);
1743*2ed9127cSMantesh Sarasetti 				musb_writew(epio, MUSB_RXCSR, val);
1744550a7375SFelipe Balbi 			}
1745550a7375SFelipe Balbi 		}
1746550a7375SFelipe Balbi #endif	/* Mentor DMA */
1747550a7375SFelipe Balbi 
1748550a7375SFelipe Balbi 		if (!dma) {
1749496dda70SMaulik Mankad 			/* Unmap the buffer so that CPU can use it */
1750c8cf203aSRobert Morell 			usb_hcd_unmap_urb_for_dma(musb_to_hcd(musb), urb);
1751550a7375SFelipe Balbi 			done = musb_host_packet_rx(musb, urb,
1752550a7375SFelipe Balbi 					epnum, iso_err);
17535c8a86e1SFelipe Balbi 			dev_dbg(musb->controller, "read %spacket\n", done ? "last " : "");
1754550a7375SFelipe Balbi 		}
1755550a7375SFelipe Balbi 	}
1756550a7375SFelipe Balbi 
1757550a7375SFelipe Balbi finish:
1758550a7375SFelipe Balbi 	urb->actual_length += xfer_len;
1759550a7375SFelipe Balbi 	qh->offset += xfer_len;
1760550a7375SFelipe Balbi 	if (done) {
1761550a7375SFelipe Balbi 		if (urb->status == -EINPROGRESS)
1762550a7375SFelipe Balbi 			urb->status = status;
1763550a7375SFelipe Balbi 		musb_advance_schedule(musb, urb, hw_ep, USB_DIR_IN);
1764550a7375SFelipe Balbi 	}
1765550a7375SFelipe Balbi }
1766550a7375SFelipe Balbi 
1767550a7375SFelipe Balbi /* schedule nodes correspond to peripheral endpoints, like an OHCI QH.
1768550a7375SFelipe Balbi  * the software schedule associates multiple such nodes with a given
1769550a7375SFelipe Balbi  * host side hardware endpoint + direction; scheduling may activate
1770550a7375SFelipe Balbi  * that hardware endpoint.
1771550a7375SFelipe Balbi  */
1772550a7375SFelipe Balbi static int musb_schedule(
1773550a7375SFelipe Balbi 	struct musb		*musb,
1774550a7375SFelipe Balbi 	struct musb_qh		*qh,
1775550a7375SFelipe Balbi 	int			is_in)
1776550a7375SFelipe Balbi {
1777550a7375SFelipe Balbi 	int			idle;
1778550a7375SFelipe Balbi 	int			best_diff;
1779550a7375SFelipe Balbi 	int			best_end, epnum;
1780550a7375SFelipe Balbi 	struct musb_hw_ep	*hw_ep = NULL;
1781550a7375SFelipe Balbi 	struct list_head	*head = NULL;
17825274dab6SSwaminathan S 	u8			toggle;
17835274dab6SSwaminathan S 	u8			txtype;
17845274dab6SSwaminathan S 	struct urb		*urb = next_urb(qh);
1785550a7375SFelipe Balbi 
1786550a7375SFelipe Balbi 	/* use fixed hardware for control and bulk */
178723d15e07SAjay Kumar Gupta 	if (qh->type == USB_ENDPOINT_XFER_CONTROL) {
1788550a7375SFelipe Balbi 		head = &musb->control;
1789550a7375SFelipe Balbi 		hw_ep = musb->control_ep;
1790550a7375SFelipe Balbi 		goto success;
1791550a7375SFelipe Balbi 	}
1792550a7375SFelipe Balbi 
1793550a7375SFelipe Balbi 	/* else, periodic transfers get muxed to other endpoints */
1794550a7375SFelipe Balbi 
17955d67a851SSergei Shtylyov 	/*
17965d67a851SSergei Shtylyov 	 * We know this qh hasn't been scheduled, so all we need to do
1797550a7375SFelipe Balbi 	 * is choose which hardware endpoint to put it on ...
1798550a7375SFelipe Balbi 	 *
1799550a7375SFelipe Balbi 	 * REVISIT what we really want here is a regular schedule tree
18005d67a851SSergei Shtylyov 	 * like e.g. OHCI uses.
1801550a7375SFelipe Balbi 	 */
1802550a7375SFelipe Balbi 	best_diff = 4096;
1803550a7375SFelipe Balbi 	best_end = -1;
1804550a7375SFelipe Balbi 
18055d67a851SSergei Shtylyov 	for (epnum = 1, hw_ep = musb->endpoints + 1;
18065d67a851SSergei Shtylyov 			epnum < musb->nr_endpoints;
18075d67a851SSergei Shtylyov 			epnum++, hw_ep++) {
1808550a7375SFelipe Balbi 		int	diff;
1809550a7375SFelipe Balbi 
18103e5c6dc7SSergei Shtylyov 		if (musb_ep_get_qh(hw_ep, is_in) != NULL)
18115d67a851SSergei Shtylyov 			continue;
18125d67a851SSergei Shtylyov 
1813550a7375SFelipe Balbi 		if (hw_ep == musb->bulk_ep)
1814550a7375SFelipe Balbi 			continue;
1815550a7375SFelipe Balbi 
1816550a7375SFelipe Balbi 		if (is_in)
1817a483d706SAjay Kumar Gupta 			diff = hw_ep->max_packet_sz_rx;
1818550a7375SFelipe Balbi 		else
1819a483d706SAjay Kumar Gupta 			diff = hw_ep->max_packet_sz_tx;
1820a483d706SAjay Kumar Gupta 		diff -= (qh->maxpacket * qh->hb_mult);
1821550a7375SFelipe Balbi 
182223d15e07SAjay Kumar Gupta 		if (diff >= 0 && best_diff > diff) {
18235274dab6SSwaminathan S 
18245274dab6SSwaminathan S 			/*
18255274dab6SSwaminathan S 			 * Mentor controller has a bug in that if we schedule
18265274dab6SSwaminathan S 			 * a BULK Tx transfer on an endpoint that had earlier
18275274dab6SSwaminathan S 			 * handled ISOC then the BULK transfer has to start on
18285274dab6SSwaminathan S 			 * a zero toggle.  If the BULK transfer starts on a 1
18295274dab6SSwaminathan S 			 * toggle then this transfer will fail as the mentor
18305274dab6SSwaminathan S 			 * controller starts the Bulk transfer on a 0 toggle
18315274dab6SSwaminathan S 			 * irrespective of the programming of the toggle bits
18325274dab6SSwaminathan S 			 * in the TXCSR register.  Check for this condition
18335274dab6SSwaminathan S 			 * while allocating the EP for a Tx Bulk transfer.  If
18345274dab6SSwaminathan S 			 * so skip this EP.
18355274dab6SSwaminathan S 			 */
18365274dab6SSwaminathan S 			hw_ep = musb->endpoints + epnum;
18375274dab6SSwaminathan S 			toggle = usb_gettoggle(urb->dev, qh->epnum, !is_in);
18385274dab6SSwaminathan S 			txtype = (musb_readb(hw_ep->regs, MUSB_TXTYPE)
18395274dab6SSwaminathan S 					>> 4) & 0x3;
18405274dab6SSwaminathan S 			if (!is_in && (qh->type == USB_ENDPOINT_XFER_BULK) &&
18415274dab6SSwaminathan S 				toggle && (txtype == USB_ENDPOINT_XFER_ISOC))
18425274dab6SSwaminathan S 				continue;
18435274dab6SSwaminathan S 
1844550a7375SFelipe Balbi 			best_diff = diff;
1845550a7375SFelipe Balbi 			best_end = epnum;
1846550a7375SFelipe Balbi 		}
1847550a7375SFelipe Balbi 	}
184823d15e07SAjay Kumar Gupta 	/* use bulk reserved ep1 if no other ep is free */
1849aa5cbbecSFelipe Balbi 	if (best_end < 0 && qh->type == USB_ENDPOINT_XFER_BULK) {
185023d15e07SAjay Kumar Gupta 		hw_ep = musb->bulk_ep;
185123d15e07SAjay Kumar Gupta 		if (is_in)
185223d15e07SAjay Kumar Gupta 			head = &musb->in_bulk;
185323d15e07SAjay Kumar Gupta 		else
185423d15e07SAjay Kumar Gupta 			head = &musb->out_bulk;
18551e0320f0SAjay Kumar Gupta 
18561e0320f0SAjay Kumar Gupta 		/* Enable bulk RX NAK timeout scheme when bulk requests are
18571e0320f0SAjay Kumar Gupta 		 * multiplexed.  This scheme doen't work in high speed to full
18581e0320f0SAjay Kumar Gupta 		 * speed scenario as NAK interrupts are not coming from a
18591e0320f0SAjay Kumar Gupta 		 * full speed device connected to a high speed device.
18601e0320f0SAjay Kumar Gupta 		 * NAK timeout interval is 8 (128 uframe or 16ms) for HS and
18611e0320f0SAjay Kumar Gupta 		 * 4 (8 frame or 8ms) for FS device.
18621e0320f0SAjay Kumar Gupta 		 */
18631e0320f0SAjay Kumar Gupta 		if (is_in && qh->dev)
18641e0320f0SAjay Kumar Gupta 			qh->intv_reg =
18651e0320f0SAjay Kumar Gupta 				(USB_SPEED_HIGH == qh->dev->speed) ? 8 : 4;
186623d15e07SAjay Kumar Gupta 		goto success;
186723d15e07SAjay Kumar Gupta 	} else if (best_end < 0) {
1868550a7375SFelipe Balbi 		return -ENOSPC;
186923d15e07SAjay Kumar Gupta 	}
1870550a7375SFelipe Balbi 
1871550a7375SFelipe Balbi 	idle = 1;
187223d15e07SAjay Kumar Gupta 	qh->mux = 0;
1873550a7375SFelipe Balbi 	hw_ep = musb->endpoints + best_end;
18745c8a86e1SFelipe Balbi 	dev_dbg(musb->controller, "qh %p periodic slot %d\n", qh, best_end);
1875550a7375SFelipe Balbi success:
187623d15e07SAjay Kumar Gupta 	if (head) {
187723d15e07SAjay Kumar Gupta 		idle = list_empty(head);
187823d15e07SAjay Kumar Gupta 		list_add_tail(&qh->ring, head);
187923d15e07SAjay Kumar Gupta 		qh->mux = 1;
188023d15e07SAjay Kumar Gupta 	}
1881550a7375SFelipe Balbi 	qh->hw_ep = hw_ep;
1882550a7375SFelipe Balbi 	qh->hep->hcpriv = qh;
1883550a7375SFelipe Balbi 	if (idle)
1884550a7375SFelipe Balbi 		musb_start_urb(musb, is_in, qh);
1885550a7375SFelipe Balbi 	return 0;
1886550a7375SFelipe Balbi }
1887550a7375SFelipe Balbi 
1888550a7375SFelipe Balbi static int musb_urb_enqueue(
1889550a7375SFelipe Balbi 	struct usb_hcd			*hcd,
1890550a7375SFelipe Balbi 	struct urb			*urb,
1891550a7375SFelipe Balbi 	gfp_t				mem_flags)
1892550a7375SFelipe Balbi {
1893550a7375SFelipe Balbi 	unsigned long			flags;
1894550a7375SFelipe Balbi 	struct musb			*musb = hcd_to_musb(hcd);
1895550a7375SFelipe Balbi 	struct usb_host_endpoint	*hep = urb->ep;
189674bb3508SDavid Brownell 	struct musb_qh			*qh;
1897550a7375SFelipe Balbi 	struct usb_endpoint_descriptor	*epd = &hep->desc;
1898550a7375SFelipe Balbi 	int				ret;
1899550a7375SFelipe Balbi 	unsigned			type_reg;
1900550a7375SFelipe Balbi 	unsigned			interval;
1901550a7375SFelipe Balbi 
1902550a7375SFelipe Balbi 	/* host role must be active */
1903550a7375SFelipe Balbi 	if (!is_host_active(musb) || !musb->is_active)
1904550a7375SFelipe Balbi 		return -ENODEV;
1905550a7375SFelipe Balbi 
1906550a7375SFelipe Balbi 	spin_lock_irqsave(&musb->lock, flags);
1907550a7375SFelipe Balbi 	ret = usb_hcd_link_urb_to_ep(hcd, urb);
190874bb3508SDavid Brownell 	qh = ret ? NULL : hep->hcpriv;
190974bb3508SDavid Brownell 	if (qh)
191074bb3508SDavid Brownell 		urb->hcpriv = qh;
1911550a7375SFelipe Balbi 	spin_unlock_irqrestore(&musb->lock, flags);
1912550a7375SFelipe Balbi 
1913550a7375SFelipe Balbi 	/* DMA mapping was already done, if needed, and this urb is on
191474bb3508SDavid Brownell 	 * hep->urb_list now ... so we're done, unless hep wasn't yet
191574bb3508SDavid Brownell 	 * scheduled onto a live qh.
1916550a7375SFelipe Balbi 	 *
1917550a7375SFelipe Balbi 	 * REVISIT best to keep hep->hcpriv valid until the endpoint gets
1918550a7375SFelipe Balbi 	 * disabled, testing for empty qh->ring and avoiding qh setup costs
1919550a7375SFelipe Balbi 	 * except for the first urb queued after a config change.
1920550a7375SFelipe Balbi 	 */
192174bb3508SDavid Brownell 	if (qh || ret)
192274bb3508SDavid Brownell 		return ret;
1923550a7375SFelipe Balbi 
1924550a7375SFelipe Balbi 	/* Allocate and initialize qh, minimizing the work done each time
1925550a7375SFelipe Balbi 	 * hw_ep gets reprogrammed, or with irqs blocked.  Then schedule it.
1926550a7375SFelipe Balbi 	 *
1927550a7375SFelipe Balbi 	 * REVISIT consider a dedicated qh kmem_cache, so it's harder
1928550a7375SFelipe Balbi 	 * for bugs in other kernel code to break this driver...
1929550a7375SFelipe Balbi 	 */
1930550a7375SFelipe Balbi 	qh = kzalloc(sizeof *qh, mem_flags);
1931550a7375SFelipe Balbi 	if (!qh) {
19322492e674SAjay Kumar Gupta 		spin_lock_irqsave(&musb->lock, flags);
1933550a7375SFelipe Balbi 		usb_hcd_unlink_urb_from_ep(hcd, urb);
19342492e674SAjay Kumar Gupta 		spin_unlock_irqrestore(&musb->lock, flags);
1935550a7375SFelipe Balbi 		return -ENOMEM;
1936550a7375SFelipe Balbi 	}
1937550a7375SFelipe Balbi 
1938550a7375SFelipe Balbi 	qh->hep = hep;
1939550a7375SFelipe Balbi 	qh->dev = urb->dev;
1940550a7375SFelipe Balbi 	INIT_LIST_HEAD(&qh->ring);
1941550a7375SFelipe Balbi 	qh->is_ready = 1;
1942550a7375SFelipe Balbi 
194329cc8897SKuninori Morimoto 	qh->maxpacket = usb_endpoint_maxp(epd);
1944a483d706SAjay Kumar Gupta 	qh->type = usb_endpoint_type(epd);
1945550a7375SFelipe Balbi 
1946a483d706SAjay Kumar Gupta 	/* Bits 11 & 12 of wMaxPacketSize encode high bandwidth multiplier.
1947a483d706SAjay Kumar Gupta 	 * Some musb cores don't support high bandwidth ISO transfers; and
1948a483d706SAjay Kumar Gupta 	 * we don't (yet!) support high bandwidth interrupt transfers.
1949a483d706SAjay Kumar Gupta 	 */
1950a483d706SAjay Kumar Gupta 	qh->hb_mult = 1 + ((qh->maxpacket >> 11) & 0x03);
1951a483d706SAjay Kumar Gupta 	if (qh->hb_mult > 1) {
1952a483d706SAjay Kumar Gupta 		int ok = (qh->type == USB_ENDPOINT_XFER_ISOC);
1953a483d706SAjay Kumar Gupta 
1954a483d706SAjay Kumar Gupta 		if (ok)
1955a483d706SAjay Kumar Gupta 			ok = (usb_pipein(urb->pipe) && musb->hb_iso_rx)
1956a483d706SAjay Kumar Gupta 				|| (usb_pipeout(urb->pipe) && musb->hb_iso_tx);
1957a483d706SAjay Kumar Gupta 		if (!ok) {
1958550a7375SFelipe Balbi 			ret = -EMSGSIZE;
1959550a7375SFelipe Balbi 			goto done;
1960550a7375SFelipe Balbi 		}
1961a483d706SAjay Kumar Gupta 		qh->maxpacket &= 0x7ff;
1962a483d706SAjay Kumar Gupta 	}
1963550a7375SFelipe Balbi 
196496bcd090SJulia Lawall 	qh->epnum = usb_endpoint_num(epd);
1965550a7375SFelipe Balbi 
1966550a7375SFelipe Balbi 	/* NOTE: urb->dev->devnum is wrong during SET_ADDRESS */
1967550a7375SFelipe Balbi 	qh->addr_reg = (u8) usb_pipedevice(urb->pipe);
1968550a7375SFelipe Balbi 
1969550a7375SFelipe Balbi 	/* precompute rxtype/txtype/type0 register */
1970550a7375SFelipe Balbi 	type_reg = (qh->type << 4) | qh->epnum;
1971550a7375SFelipe Balbi 	switch (urb->dev->speed) {
1972550a7375SFelipe Balbi 	case USB_SPEED_LOW:
1973550a7375SFelipe Balbi 		type_reg |= 0xc0;
1974550a7375SFelipe Balbi 		break;
1975550a7375SFelipe Balbi 	case USB_SPEED_FULL:
1976550a7375SFelipe Balbi 		type_reg |= 0x80;
1977550a7375SFelipe Balbi 		break;
1978550a7375SFelipe Balbi 	default:
1979550a7375SFelipe Balbi 		type_reg |= 0x40;
1980550a7375SFelipe Balbi 	}
1981550a7375SFelipe Balbi 	qh->type_reg = type_reg;
1982550a7375SFelipe Balbi 
1983136733d6SSergei Shtylyov 	/* Precompute RXINTERVAL/TXINTERVAL register */
1984550a7375SFelipe Balbi 	switch (qh->type) {
1985550a7375SFelipe Balbi 	case USB_ENDPOINT_XFER_INT:
1986136733d6SSergei Shtylyov 		/*
1987136733d6SSergei Shtylyov 		 * Full/low speeds use the  linear encoding,
1988136733d6SSergei Shtylyov 		 * high speed uses the logarithmic encoding.
1989136733d6SSergei Shtylyov 		 */
1990136733d6SSergei Shtylyov 		if (urb->dev->speed <= USB_SPEED_FULL) {
1991136733d6SSergei Shtylyov 			interval = max_t(u8, epd->bInterval, 1);
1992136733d6SSergei Shtylyov 			break;
1993550a7375SFelipe Balbi 		}
1994550a7375SFelipe Balbi 		/* FALLTHROUGH */
1995550a7375SFelipe Balbi 	case USB_ENDPOINT_XFER_ISOC:
1996136733d6SSergei Shtylyov 		/* ISO always uses logarithmic encoding */
1997136733d6SSergei Shtylyov 		interval = min_t(u8, epd->bInterval, 16);
1998550a7375SFelipe Balbi 		break;
1999550a7375SFelipe Balbi 	default:
2000550a7375SFelipe Balbi 		/* REVISIT we actually want to use NAK limits, hinting to the
2001550a7375SFelipe Balbi 		 * transfer scheduling logic to try some other qh, e.g. try
2002550a7375SFelipe Balbi 		 * for 2 msec first:
2003550a7375SFelipe Balbi 		 *
2004550a7375SFelipe Balbi 		 * interval = (USB_SPEED_HIGH == urb->dev->speed) ? 16 : 2;
2005550a7375SFelipe Balbi 		 *
2006550a7375SFelipe Balbi 		 * The downside of disabling this is that transfer scheduling
2007550a7375SFelipe Balbi 		 * gets VERY unfair for nonperiodic transfers; a misbehaving
20081e0320f0SAjay Kumar Gupta 		 * peripheral could make that hurt.  That's perfectly normal
20091e0320f0SAjay Kumar Gupta 		 * for reads from network or serial adapters ... so we have
20101e0320f0SAjay Kumar Gupta 		 * partial NAKlimit support for bulk RX.
2011550a7375SFelipe Balbi 		 *
20121e0320f0SAjay Kumar Gupta 		 * The upside of disabling it is simpler transfer scheduling.
2013550a7375SFelipe Balbi 		 */
2014550a7375SFelipe Balbi 		interval = 0;
2015550a7375SFelipe Balbi 	}
2016550a7375SFelipe Balbi 	qh->intv_reg = interval;
2017550a7375SFelipe Balbi 
2018550a7375SFelipe Balbi 	/* precompute addressing for external hub/tt ports */
2019550a7375SFelipe Balbi 	if (musb->is_multipoint) {
2020550a7375SFelipe Balbi 		struct usb_device	*parent = urb->dev->parent;
2021550a7375SFelipe Balbi 
2022550a7375SFelipe Balbi 		if (parent != hcd->self.root_hub) {
2023550a7375SFelipe Balbi 			qh->h_addr_reg = (u8) parent->devnum;
2024550a7375SFelipe Balbi 
2025550a7375SFelipe Balbi 			/* set up tt info if needed */
2026550a7375SFelipe Balbi 			if (urb->dev->tt) {
2027550a7375SFelipe Balbi 				qh->h_port_reg = (u8) urb->dev->ttport;
2028ae5ad296SAjay Kumar Gupta 				if (urb->dev->tt->hub)
2029ae5ad296SAjay Kumar Gupta 					qh->h_addr_reg =
2030ae5ad296SAjay Kumar Gupta 						(u8) urb->dev->tt->hub->devnum;
2031ae5ad296SAjay Kumar Gupta 				if (urb->dev->tt->multi)
2032550a7375SFelipe Balbi 					qh->h_addr_reg |= 0x80;
2033550a7375SFelipe Balbi 			}
2034550a7375SFelipe Balbi 		}
2035550a7375SFelipe Balbi 	}
2036550a7375SFelipe Balbi 
2037550a7375SFelipe Balbi 	/* invariant: hep->hcpriv is null OR the qh that's already scheduled.
2038550a7375SFelipe Balbi 	 * until we get real dma queues (with an entry for each urb/buffer),
2039550a7375SFelipe Balbi 	 * we only have work to do in the former case.
2040550a7375SFelipe Balbi 	 */
2041550a7375SFelipe Balbi 	spin_lock_irqsave(&musb->lock, flags);
2042550a7375SFelipe Balbi 	if (hep->hcpriv) {
2043550a7375SFelipe Balbi 		/* some concurrent activity submitted another urb to hep...
2044550a7375SFelipe Balbi 		 * odd, rare, error prone, but legal.
2045550a7375SFelipe Balbi 		 */
2046550a7375SFelipe Balbi 		kfree(qh);
2047714bc5efSDan Carpenter 		qh = NULL;
2048550a7375SFelipe Balbi 		ret = 0;
2049550a7375SFelipe Balbi 	} else
2050550a7375SFelipe Balbi 		ret = musb_schedule(musb, qh,
2051550a7375SFelipe Balbi 				epd->bEndpointAddress & USB_ENDPOINT_DIR_MASK);
2052550a7375SFelipe Balbi 
2053550a7375SFelipe Balbi 	if (ret == 0) {
2054550a7375SFelipe Balbi 		urb->hcpriv = qh;
2055550a7375SFelipe Balbi 		/* FIXME set urb->start_frame for iso/intr, it's tested in
2056550a7375SFelipe Balbi 		 * musb_start_urb(), but otherwise only konicawc cares ...
2057550a7375SFelipe Balbi 		 */
2058550a7375SFelipe Balbi 	}
2059550a7375SFelipe Balbi 	spin_unlock_irqrestore(&musb->lock, flags);
2060550a7375SFelipe Balbi 
2061550a7375SFelipe Balbi done:
2062550a7375SFelipe Balbi 	if (ret != 0) {
20632492e674SAjay Kumar Gupta 		spin_lock_irqsave(&musb->lock, flags);
2064550a7375SFelipe Balbi 		usb_hcd_unlink_urb_from_ep(hcd, urb);
20652492e674SAjay Kumar Gupta 		spin_unlock_irqrestore(&musb->lock, flags);
2066550a7375SFelipe Balbi 		kfree(qh);
2067550a7375SFelipe Balbi 	}
2068550a7375SFelipe Balbi 	return ret;
2069550a7375SFelipe Balbi }
2070550a7375SFelipe Balbi 
2071550a7375SFelipe Balbi 
2072550a7375SFelipe Balbi /*
2073550a7375SFelipe Balbi  * abort a transfer that's at the head of a hardware queue.
2074550a7375SFelipe Balbi  * called with controller locked, irqs blocked
2075550a7375SFelipe Balbi  * that hardware queue advances to the next transfer, unless prevented
2076550a7375SFelipe Balbi  */
207781ec4e4aSSergei Shtylyov static int musb_cleanup_urb(struct urb *urb, struct musb_qh *qh)
2078550a7375SFelipe Balbi {
2079550a7375SFelipe Balbi 	struct musb_hw_ep	*ep = qh->hw_ep;
20805c8a86e1SFelipe Balbi 	struct musb		*musb = ep->musb;
2081550a7375SFelipe Balbi 	void __iomem		*epio = ep->regs;
2082550a7375SFelipe Balbi 	unsigned		hw_end = ep->epnum;
2083550a7375SFelipe Balbi 	void __iomem		*regs = ep->musb->mregs;
208481ec4e4aSSergei Shtylyov 	int			is_in = usb_pipein(urb->pipe);
2085550a7375SFelipe Balbi 	int			status = 0;
208681ec4e4aSSergei Shtylyov 	u16			csr;
2087550a7375SFelipe Balbi 
2088550a7375SFelipe Balbi 	musb_ep_select(regs, hw_end);
2089550a7375SFelipe Balbi 
2090550a7375SFelipe Balbi 	if (is_dma_capable()) {
2091550a7375SFelipe Balbi 		struct dma_channel	*dma;
2092550a7375SFelipe Balbi 
2093550a7375SFelipe Balbi 		dma = is_in ? ep->rx_channel : ep->tx_channel;
2094550a7375SFelipe Balbi 		if (dma) {
2095550a7375SFelipe Balbi 			status = ep->musb->dma_controller->channel_abort(dma);
20965c8a86e1SFelipe Balbi 			dev_dbg(musb->controller,
2097550a7375SFelipe Balbi 				"abort %cX%d DMA for urb %p --> %d\n",
2098550a7375SFelipe Balbi 				is_in ? 'R' : 'T', ep->epnum,
2099550a7375SFelipe Balbi 				urb, status);
2100550a7375SFelipe Balbi 			urb->actual_length += dma->actual_len;
2101550a7375SFelipe Balbi 		}
2102550a7375SFelipe Balbi 	}
2103550a7375SFelipe Balbi 
2104550a7375SFelipe Balbi 	/* turn off DMA requests, discard state, stop polling ... */
2105692933b2SAjay Kumar Gupta 	if (ep->epnum && is_in) {
2106550a7375SFelipe Balbi 		/* giveback saves bulk toggle */
2107550a7375SFelipe Balbi 		csr = musb_h_flush_rxfifo(ep, 0);
2108550a7375SFelipe Balbi 
2109550a7375SFelipe Balbi 		/* REVISIT we still get an irq; should likely clear the
2110550a7375SFelipe Balbi 		 * endpoint's irq status here to avoid bogus irqs.
2111550a7375SFelipe Balbi 		 * clearing that status is platform-specific...
2112550a7375SFelipe Balbi 		 */
211378322c1aSDavid Brownell 	} else if (ep->epnum) {
2114550a7375SFelipe Balbi 		musb_h_tx_flush_fifo(ep);
2115550a7375SFelipe Balbi 		csr = musb_readw(epio, MUSB_TXCSR);
2116550a7375SFelipe Balbi 		csr &= ~(MUSB_TXCSR_AUTOSET
2117550a7375SFelipe Balbi 			| MUSB_TXCSR_DMAENAB
2118550a7375SFelipe Balbi 			| MUSB_TXCSR_H_RXSTALL
2119550a7375SFelipe Balbi 			| MUSB_TXCSR_H_NAKTIMEOUT
2120550a7375SFelipe Balbi 			| MUSB_TXCSR_H_ERROR
2121550a7375SFelipe Balbi 			| MUSB_TXCSR_TXPKTRDY);
2122550a7375SFelipe Balbi 		musb_writew(epio, MUSB_TXCSR, csr);
2123550a7375SFelipe Balbi 		/* REVISIT may need to clear FLUSHFIFO ... */
2124550a7375SFelipe Balbi 		musb_writew(epio, MUSB_TXCSR, csr);
2125550a7375SFelipe Balbi 		/* flush cpu writebuffer */
2126550a7375SFelipe Balbi 		csr = musb_readw(epio, MUSB_TXCSR);
212778322c1aSDavid Brownell 	} else  {
212878322c1aSDavid Brownell 		musb_h_ep0_flush_fifo(ep);
2129550a7375SFelipe Balbi 	}
2130550a7375SFelipe Balbi 	if (status == 0)
2131550a7375SFelipe Balbi 		musb_advance_schedule(ep->musb, urb, ep, is_in);
2132550a7375SFelipe Balbi 	return status;
2133550a7375SFelipe Balbi }
2134550a7375SFelipe Balbi 
2135550a7375SFelipe Balbi static int musb_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
2136550a7375SFelipe Balbi {
2137550a7375SFelipe Balbi 	struct musb		*musb = hcd_to_musb(hcd);
2138550a7375SFelipe Balbi 	struct musb_qh		*qh;
2139550a7375SFelipe Balbi 	unsigned long		flags;
214022a0d6f1SSergei Shtylyov 	int			is_in  = usb_pipein(urb->pipe);
2141550a7375SFelipe Balbi 	int			ret;
2142550a7375SFelipe Balbi 
21435c8a86e1SFelipe Balbi 	dev_dbg(musb->controller, "urb=%p, dev%d ep%d%s\n", urb,
2144550a7375SFelipe Balbi 			usb_pipedevice(urb->pipe),
2145550a7375SFelipe Balbi 			usb_pipeendpoint(urb->pipe),
214622a0d6f1SSergei Shtylyov 			is_in ? "in" : "out");
2147550a7375SFelipe Balbi 
2148550a7375SFelipe Balbi 	spin_lock_irqsave(&musb->lock, flags);
2149550a7375SFelipe Balbi 	ret = usb_hcd_check_unlink_urb(hcd, urb, status);
2150550a7375SFelipe Balbi 	if (ret)
2151550a7375SFelipe Balbi 		goto done;
2152550a7375SFelipe Balbi 
2153550a7375SFelipe Balbi 	qh = urb->hcpriv;
2154550a7375SFelipe Balbi 	if (!qh)
2155550a7375SFelipe Balbi 		goto done;
2156550a7375SFelipe Balbi 
215722a0d6f1SSergei Shtylyov 	/*
215822a0d6f1SSergei Shtylyov 	 * Any URB not actively programmed into endpoint hardware can be
2159a2fd814eSSergei Shtylyov 	 * immediately given back; that's any URB not at the head of an
2160550a7375SFelipe Balbi 	 * endpoint queue, unless someday we get real DMA queues.  And even
2161a2fd814eSSergei Shtylyov 	 * if it's at the head, it might not be known to the hardware...
2162550a7375SFelipe Balbi 	 *
216322a0d6f1SSergei Shtylyov 	 * Otherwise abort current transfer, pending DMA, etc.; urb->status
2164550a7375SFelipe Balbi 	 * has already been updated.  This is a synchronous abort; it'd be
2165550a7375SFelipe Balbi 	 * OK to hold off until after some IRQ, though.
216622a0d6f1SSergei Shtylyov 	 *
216722a0d6f1SSergei Shtylyov 	 * NOTE: qh is invalid unless !list_empty(&hep->urb_list)
2168550a7375SFelipe Balbi 	 */
216922a0d6f1SSergei Shtylyov 	if (!qh->is_ready
217022a0d6f1SSergei Shtylyov 			|| urb->urb_list.prev != &qh->hep->urb_list
217122a0d6f1SSergei Shtylyov 			|| musb_ep_get_qh(qh->hw_ep, is_in) != qh) {
2172550a7375SFelipe Balbi 		int	ready = qh->is_ready;
2173550a7375SFelipe Balbi 
2174550a7375SFelipe Balbi 		qh->is_ready = 0;
2175c9cd06b3SSergei Shtylyov 		musb_giveback(musb, urb, 0);
2176550a7375SFelipe Balbi 		qh->is_ready = ready;
2177a2fd814eSSergei Shtylyov 
2178a2fd814eSSergei Shtylyov 		/* If nothing else (usually musb_giveback) is using it
2179a2fd814eSSergei Shtylyov 		 * and its URB list has emptied, recycle this qh.
2180a2fd814eSSergei Shtylyov 		 */
2181a2fd814eSSergei Shtylyov 		if (ready && list_empty(&qh->hep->urb_list)) {
2182a2fd814eSSergei Shtylyov 			qh->hep->hcpriv = NULL;
2183a2fd814eSSergei Shtylyov 			list_del(&qh->ring);
2184a2fd814eSSergei Shtylyov 			kfree(qh);
2185a2fd814eSSergei Shtylyov 		}
2186550a7375SFelipe Balbi 	} else
218781ec4e4aSSergei Shtylyov 		ret = musb_cleanup_urb(urb, qh);
2188550a7375SFelipe Balbi done:
2189550a7375SFelipe Balbi 	spin_unlock_irqrestore(&musb->lock, flags);
2190550a7375SFelipe Balbi 	return ret;
2191550a7375SFelipe Balbi }
2192550a7375SFelipe Balbi 
2193550a7375SFelipe Balbi /* disable an endpoint */
2194550a7375SFelipe Balbi static void
2195550a7375SFelipe Balbi musb_h_disable(struct usb_hcd *hcd, struct usb_host_endpoint *hep)
2196550a7375SFelipe Balbi {
219722a0d6f1SSergei Shtylyov 	u8			is_in = hep->desc.bEndpointAddress & USB_DIR_IN;
2198550a7375SFelipe Balbi 	unsigned long		flags;
2199550a7375SFelipe Balbi 	struct musb		*musb = hcd_to_musb(hcd);
2200dc61d238SSergei Shtylyov 	struct musb_qh		*qh;
2201dc61d238SSergei Shtylyov 	struct urb		*urb;
2202550a7375SFelipe Balbi 
2203550a7375SFelipe Balbi 	spin_lock_irqsave(&musb->lock, flags);
2204550a7375SFelipe Balbi 
2205dc61d238SSergei Shtylyov 	qh = hep->hcpriv;
2206dc61d238SSergei Shtylyov 	if (qh == NULL)
2207dc61d238SSergei Shtylyov 		goto exit;
2208dc61d238SSergei Shtylyov 
2209550a7375SFelipe Balbi 	/* NOTE: qh is invalid unless !list_empty(&hep->urb_list) */
2210550a7375SFelipe Balbi 
221122a0d6f1SSergei Shtylyov 	/* Kick the first URB off the hardware, if needed */
2212550a7375SFelipe Balbi 	qh->is_ready = 0;
221322a0d6f1SSergei Shtylyov 	if (musb_ep_get_qh(qh->hw_ep, is_in) == qh) {
2214550a7375SFelipe Balbi 		urb = next_urb(qh);
2215550a7375SFelipe Balbi 
2216550a7375SFelipe Balbi 		/* make software (then hardware) stop ASAP */
2217550a7375SFelipe Balbi 		if (!urb->unlinked)
2218550a7375SFelipe Balbi 			urb->status = -ESHUTDOWN;
2219550a7375SFelipe Balbi 
2220550a7375SFelipe Balbi 		/* cleanup */
222181ec4e4aSSergei Shtylyov 		musb_cleanup_urb(urb, qh);
2222550a7375SFelipe Balbi 
2223dc61d238SSergei Shtylyov 		/* Then nuke all the others ... and advance the
2224dc61d238SSergei Shtylyov 		 * queue on hw_ep (e.g. bulk ring) when we're done.
2225dc61d238SSergei Shtylyov 		 */
2226dc61d238SSergei Shtylyov 		while (!list_empty(&hep->urb_list)) {
2227dc61d238SSergei Shtylyov 			urb = next_urb(qh);
2228dc61d238SSergei Shtylyov 			urb->status = -ESHUTDOWN;
2229dc61d238SSergei Shtylyov 			musb_advance_schedule(musb, urb, qh->hw_ep, is_in);
2230dc61d238SSergei Shtylyov 		}
2231dc61d238SSergei Shtylyov 	} else {
2232dc61d238SSergei Shtylyov 		/* Just empty the queue; the hardware is busy with
2233dc61d238SSergei Shtylyov 		 * other transfers, and since !qh->is_ready nothing
2234dc61d238SSergei Shtylyov 		 * will activate any of these as it advances.
2235dc61d238SSergei Shtylyov 		 */
2236dc61d238SSergei Shtylyov 		while (!list_empty(&hep->urb_list))
2237c9cd06b3SSergei Shtylyov 			musb_giveback(musb, next_urb(qh), -ESHUTDOWN);
2238550a7375SFelipe Balbi 
2239dc61d238SSergei Shtylyov 		hep->hcpriv = NULL;
2240dc61d238SSergei Shtylyov 		list_del(&qh->ring);
2241dc61d238SSergei Shtylyov 		kfree(qh);
2242dc61d238SSergei Shtylyov 	}
2243dc61d238SSergei Shtylyov exit:
2244550a7375SFelipe Balbi 	spin_unlock_irqrestore(&musb->lock, flags);
2245550a7375SFelipe Balbi }
2246550a7375SFelipe Balbi 
2247550a7375SFelipe Balbi static int musb_h_get_frame_number(struct usb_hcd *hcd)
2248550a7375SFelipe Balbi {
2249550a7375SFelipe Balbi 	struct musb	*musb = hcd_to_musb(hcd);
2250550a7375SFelipe Balbi 
2251550a7375SFelipe Balbi 	return musb_readw(musb->mregs, MUSB_FRAME);
2252550a7375SFelipe Balbi }
2253550a7375SFelipe Balbi 
2254550a7375SFelipe Balbi static int musb_h_start(struct usb_hcd *hcd)
2255550a7375SFelipe Balbi {
2256550a7375SFelipe Balbi 	struct musb	*musb = hcd_to_musb(hcd);
2257550a7375SFelipe Balbi 
2258550a7375SFelipe Balbi 	/* NOTE: musb_start() is called when the hub driver turns
2259550a7375SFelipe Balbi 	 * on port power, or when (OTG) peripheral starts.
2260550a7375SFelipe Balbi 	 */
2261550a7375SFelipe Balbi 	hcd->state = HC_STATE_RUNNING;
2262550a7375SFelipe Balbi 	musb->port1_status = 0;
2263550a7375SFelipe Balbi 	return 0;
2264550a7375SFelipe Balbi }
2265550a7375SFelipe Balbi 
2266550a7375SFelipe Balbi static void musb_h_stop(struct usb_hcd *hcd)
2267550a7375SFelipe Balbi {
2268550a7375SFelipe Balbi 	musb_stop(hcd_to_musb(hcd));
2269550a7375SFelipe Balbi 	hcd->state = HC_STATE_HALT;
2270550a7375SFelipe Balbi }
2271550a7375SFelipe Balbi 
2272550a7375SFelipe Balbi static int musb_bus_suspend(struct usb_hcd *hcd)
2273550a7375SFelipe Balbi {
2274550a7375SFelipe Balbi 	struct musb	*musb = hcd_to_musb(hcd);
227589368d3dSDavid Brownell 	u8		devctl;
2276550a7375SFelipe Balbi 
227789368d3dSDavid Brownell 	if (!is_host_active(musb))
2278550a7375SFelipe Balbi 		return 0;
2279550a7375SFelipe Balbi 
228089368d3dSDavid Brownell 	switch (musb->xceiv->state) {
228189368d3dSDavid Brownell 	case OTG_STATE_A_SUSPEND:
228289368d3dSDavid Brownell 		return 0;
228389368d3dSDavid Brownell 	case OTG_STATE_A_WAIT_VRISE:
228489368d3dSDavid Brownell 		/* ID could be grounded even if there's no device
228589368d3dSDavid Brownell 		 * on the other end of the cable.  NOTE that the
228689368d3dSDavid Brownell 		 * A_WAIT_VRISE timers are messy with MUSB...
228789368d3dSDavid Brownell 		 */
228889368d3dSDavid Brownell 		devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
228989368d3dSDavid Brownell 		if ((devctl & MUSB_DEVCTL_VBUS) == MUSB_DEVCTL_VBUS)
229089368d3dSDavid Brownell 			musb->xceiv->state = OTG_STATE_A_WAIT_BCON;
229189368d3dSDavid Brownell 		break;
229289368d3dSDavid Brownell 	default:
229389368d3dSDavid Brownell 		break;
229489368d3dSDavid Brownell 	}
229589368d3dSDavid Brownell 
229689368d3dSDavid Brownell 	if (musb->is_active) {
229789368d3dSDavid Brownell 		WARNING("trying to suspend as %s while active\n",
22983df00453SAnatolij Gustschin 				otg_state_string(musb->xceiv->state));
2299550a7375SFelipe Balbi 		return -EBUSY;
2300550a7375SFelipe Balbi 	} else
2301550a7375SFelipe Balbi 		return 0;
2302550a7375SFelipe Balbi }
2303550a7375SFelipe Balbi 
2304550a7375SFelipe Balbi static int musb_bus_resume(struct usb_hcd *hcd)
2305550a7375SFelipe Balbi {
2306550a7375SFelipe Balbi 	/* resuming child port does the work */
2307550a7375SFelipe Balbi 	return 0;
2308550a7375SFelipe Balbi }
2309550a7375SFelipe Balbi 
2310550a7375SFelipe Balbi const struct hc_driver musb_hc_driver = {
2311550a7375SFelipe Balbi 	.description		= "musb-hcd",
2312550a7375SFelipe Balbi 	.product_desc		= "MUSB HDRC host driver",
2313550a7375SFelipe Balbi 	.hcd_priv_size		= sizeof(struct musb),
2314550a7375SFelipe Balbi 	.flags			= HCD_USB2 | HCD_MEMORY,
2315550a7375SFelipe Balbi 
2316550a7375SFelipe Balbi 	/* not using irq handler or reset hooks from usbcore, since
2317550a7375SFelipe Balbi 	 * those must be shared with peripheral code for OTG configs
2318550a7375SFelipe Balbi 	 */
2319550a7375SFelipe Balbi 
2320550a7375SFelipe Balbi 	.start			= musb_h_start,
2321550a7375SFelipe Balbi 	.stop			= musb_h_stop,
2322550a7375SFelipe Balbi 
2323550a7375SFelipe Balbi 	.get_frame_number	= musb_h_get_frame_number,
2324550a7375SFelipe Balbi 
2325550a7375SFelipe Balbi 	.urb_enqueue		= musb_urb_enqueue,
2326550a7375SFelipe Balbi 	.urb_dequeue		= musb_urb_dequeue,
2327550a7375SFelipe Balbi 	.endpoint_disable	= musb_h_disable,
2328550a7375SFelipe Balbi 
2329550a7375SFelipe Balbi 	.hub_status_data	= musb_hub_status_data,
2330550a7375SFelipe Balbi 	.hub_control		= musb_hub_control,
2331550a7375SFelipe Balbi 	.bus_suspend		= musb_bus_suspend,
2332550a7375SFelipe Balbi 	.bus_resume		= musb_bus_resume,
2333550a7375SFelipe Balbi 	/* .start_port_reset	= NULL, */
2334550a7375SFelipe Balbi 	/* .hub_irq_enable	= NULL, */
2335550a7375SFelipe Balbi };
2336