xref: /linux/drivers/usb/musb/musb_host.c (revision ae5ad2963939d24eb77b8fa725d0703dc0f97a47)
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
7550a7375SFelipe Balbi  *
8550a7375SFelipe Balbi  * This program is free software; you can redistribute it and/or
9550a7375SFelipe Balbi  * modify it under the terms of the GNU General Public License
10550a7375SFelipe Balbi  * version 2 as published by the Free Software Foundation.
11550a7375SFelipe Balbi  *
12550a7375SFelipe Balbi  * This program is distributed in the hope that it will be useful, but
13550a7375SFelipe Balbi  * WITHOUT ANY WARRANTY; without even the implied warranty of
14550a7375SFelipe Balbi  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15550a7375SFelipe Balbi  * General Public License for more details.
16550a7375SFelipe Balbi  *
17550a7375SFelipe Balbi  * You should have received a copy of the GNU General Public License
18550a7375SFelipe Balbi  * along with this program; if not, write to the Free Software
19550a7375SFelipe Balbi  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20550a7375SFelipe Balbi  * 02110-1301 USA
21550a7375SFelipe Balbi  *
22550a7375SFelipe Balbi  * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
23550a7375SFelipe Balbi  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
24550a7375SFelipe Balbi  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN
25550a7375SFelipe Balbi  * NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26550a7375SFelipe Balbi  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27550a7375SFelipe Balbi  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
28550a7375SFelipe Balbi  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
29550a7375SFelipe Balbi  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30550a7375SFelipe Balbi  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31550a7375SFelipe Balbi  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32550a7375SFelipe Balbi  *
33550a7375SFelipe Balbi  */
34550a7375SFelipe Balbi 
35550a7375SFelipe Balbi #include <linux/module.h>
36550a7375SFelipe Balbi #include <linux/kernel.h>
37550a7375SFelipe Balbi #include <linux/delay.h>
38550a7375SFelipe Balbi #include <linux/sched.h>
39550a7375SFelipe Balbi #include <linux/slab.h>
40550a7375SFelipe Balbi #include <linux/errno.h>
41550a7375SFelipe Balbi #include <linux/init.h>
42550a7375SFelipe Balbi #include <linux/list.h>
43550a7375SFelipe Balbi 
44550a7375SFelipe Balbi #include "musb_core.h"
45550a7375SFelipe Balbi #include "musb_host.h"
46550a7375SFelipe Balbi 
47550a7375SFelipe Balbi 
48550a7375SFelipe Balbi /* MUSB HOST status 22-mar-2006
49550a7375SFelipe Balbi  *
50550a7375SFelipe Balbi  * - There's still lots of partial code duplication for fault paths, so
51550a7375SFelipe Balbi  *   they aren't handled as consistently as they need to be.
52550a7375SFelipe Balbi  *
53550a7375SFelipe Balbi  * - PIO mostly behaved when last tested.
54550a7375SFelipe Balbi  *     + including ep0, with all usbtest cases 9, 10
55550a7375SFelipe Balbi  *     + usbtest 14 (ep0out) doesn't seem to run at all
56550a7375SFelipe Balbi  *     + double buffered OUT/TX endpoints saw stalls(!) with certain usbtest
57550a7375SFelipe Balbi  *       configurations, but otherwise double buffering passes basic tests.
58550a7375SFelipe Balbi  *     + for 2.6.N, for N > ~10, needs API changes for hcd framework.
59550a7375SFelipe Balbi  *
60550a7375SFelipe Balbi  * - DMA (CPPI) ... partially behaves, not currently recommended
61550a7375SFelipe Balbi  *     + about 1/15 the speed of typical EHCI implementations (PCI)
62550a7375SFelipe Balbi  *     + RX, all too often reqpkt seems to misbehave after tx
63550a7375SFelipe Balbi  *     + TX, no known issues (other than evident silicon issue)
64550a7375SFelipe Balbi  *
65550a7375SFelipe Balbi  * - DMA (Mentor/OMAP) ...has at least toggle update problems
66550a7375SFelipe Balbi  *
67550a7375SFelipe Balbi  * - Still no traffic scheduling code to make NAKing for bulk or control
68550a7375SFelipe Balbi  *   transfers unable to starve other requests; or to make efficient use
69550a7375SFelipe Balbi  *   of hardware with periodic transfers.  (Note that network drivers
70550a7375SFelipe Balbi  *   commonly post bulk reads that stay pending for a long time; these
71550a7375SFelipe Balbi  *   would make very visible trouble.)
72550a7375SFelipe Balbi  *
73550a7375SFelipe Balbi  * - Not tested with HNP, but some SRP paths seem to behave.
74550a7375SFelipe Balbi  *
75550a7375SFelipe Balbi  * NOTE 24-August-2006:
76550a7375SFelipe Balbi  *
77550a7375SFelipe Balbi  * - Bulk traffic finally uses both sides of hardware ep1, freeing up an
78550a7375SFelipe Balbi  *   extra endpoint for periodic use enabling hub + keybd + mouse.  That
79550a7375SFelipe Balbi  *   mostly works, except that with "usbnet" it's easy to trigger cases
80550a7375SFelipe Balbi  *   with "ping" where RX loses.  (a) ping to davinci, even "ping -f",
81550a7375SFelipe Balbi  *   fine; but (b) ping _from_ davinci, even "ping -c 1", ICMP RX loses
82550a7375SFelipe Balbi  *   although ARP RX wins.  (That test was done with a full speed link.)
83550a7375SFelipe Balbi  */
84550a7375SFelipe Balbi 
85550a7375SFelipe Balbi 
86550a7375SFelipe Balbi /*
87550a7375SFelipe Balbi  * NOTE on endpoint usage:
88550a7375SFelipe Balbi  *
89550a7375SFelipe Balbi  * CONTROL transfers all go through ep0.  BULK ones go through dedicated IN
90550a7375SFelipe Balbi  * and OUT endpoints ... hardware is dedicated for those "async" queue(s).
91550a7375SFelipe Balbi  *
92550a7375SFelipe Balbi  * (Yes, bulk _could_ use more of the endpoints than that, and would even
93550a7375SFelipe Balbi  * benefit from it ... one remote device may easily be NAKing while others
94550a7375SFelipe Balbi  * need to perform transfers in that same direction.  The same thing could
95550a7375SFelipe Balbi  * be done in software though, assuming dma cooperates.)
96550a7375SFelipe Balbi  *
97550a7375SFelipe Balbi  * INTERUPPT and ISOCHRONOUS transfers are scheduled to the other endpoints.
98550a7375SFelipe Balbi  * So far that scheduling is both dumb and optimistic:  the endpoint will be
99550a7375SFelipe Balbi  * "claimed" until its software queue is no longer refilled.  No multiplexing
100550a7375SFelipe Balbi  * of transfers between endpoints, or anything clever.
101550a7375SFelipe Balbi  */
102550a7375SFelipe Balbi 
103550a7375SFelipe Balbi 
104550a7375SFelipe Balbi static void musb_ep_program(struct musb *musb, u8 epnum,
105550a7375SFelipe Balbi 			struct urb *urb, unsigned int nOut,
106550a7375SFelipe Balbi 			u8 *buf, u32 len);
107550a7375SFelipe Balbi 
108550a7375SFelipe Balbi /*
109550a7375SFelipe Balbi  * Clear TX fifo. Needed to avoid BABBLE errors.
110550a7375SFelipe Balbi  */
111550a7375SFelipe Balbi static inline void musb_h_tx_flush_fifo(struct musb_hw_ep *ep)
112550a7375SFelipe Balbi {
113550a7375SFelipe Balbi 	void __iomem	*epio = ep->regs;
114550a7375SFelipe Balbi 	u16		csr;
115550a7375SFelipe Balbi 	int		retries = 1000;
116550a7375SFelipe Balbi 
117550a7375SFelipe Balbi 	csr = musb_readw(epio, MUSB_TXCSR);
118550a7375SFelipe Balbi 	while (csr & MUSB_TXCSR_FIFONOTEMPTY) {
119550a7375SFelipe Balbi 		DBG(5, "Host TX FIFONOTEMPTY csr: %02x\n", csr);
120550a7375SFelipe Balbi 		csr |= MUSB_TXCSR_FLUSHFIFO;
121550a7375SFelipe Balbi 		musb_writew(epio, MUSB_TXCSR, csr);
122550a7375SFelipe Balbi 		csr = musb_readw(epio, MUSB_TXCSR);
123550a7375SFelipe Balbi 		if (retries-- < 1) {
124550a7375SFelipe Balbi 			ERR("Could not flush host TX fifo: csr: %04x\n", csr);
125550a7375SFelipe Balbi 			return;
126550a7375SFelipe Balbi 		}
127550a7375SFelipe Balbi 		mdelay(1);
128550a7375SFelipe Balbi 	}
129550a7375SFelipe Balbi }
130550a7375SFelipe Balbi 
131550a7375SFelipe Balbi /*
132550a7375SFelipe Balbi  * Start transmit. Caller is responsible for locking shared resources.
133550a7375SFelipe Balbi  * musb must be locked.
134550a7375SFelipe Balbi  */
135550a7375SFelipe Balbi static inline void musb_h_tx_start(struct musb_hw_ep *ep)
136550a7375SFelipe Balbi {
137550a7375SFelipe Balbi 	u16	txcsr;
138550a7375SFelipe Balbi 
139550a7375SFelipe Balbi 	/* NOTE: no locks here; caller should lock and select EP */
140550a7375SFelipe Balbi 	if (ep->epnum) {
141550a7375SFelipe Balbi 		txcsr = musb_readw(ep->regs, MUSB_TXCSR);
142550a7375SFelipe Balbi 		txcsr |= MUSB_TXCSR_TXPKTRDY | MUSB_TXCSR_H_WZC_BITS;
143550a7375SFelipe Balbi 		musb_writew(ep->regs, MUSB_TXCSR, txcsr);
144550a7375SFelipe Balbi 	} else {
145550a7375SFelipe Balbi 		txcsr = MUSB_CSR0_H_SETUPPKT | MUSB_CSR0_TXPKTRDY;
146550a7375SFelipe Balbi 		musb_writew(ep->regs, MUSB_CSR0, txcsr);
147550a7375SFelipe Balbi 	}
148550a7375SFelipe Balbi 
149550a7375SFelipe Balbi }
150550a7375SFelipe Balbi 
151550a7375SFelipe Balbi static inline void cppi_host_txdma_start(struct musb_hw_ep *ep)
152550a7375SFelipe Balbi {
153550a7375SFelipe Balbi 	u16	txcsr;
154550a7375SFelipe Balbi 
155550a7375SFelipe Balbi 	/* NOTE: no locks here; caller should lock and select EP */
156550a7375SFelipe Balbi 	txcsr = musb_readw(ep->regs, MUSB_TXCSR);
157550a7375SFelipe Balbi 	txcsr |= MUSB_TXCSR_DMAENAB | MUSB_TXCSR_H_WZC_BITS;
158550a7375SFelipe Balbi 	musb_writew(ep->regs, MUSB_TXCSR, txcsr);
159550a7375SFelipe Balbi }
160550a7375SFelipe Balbi 
161550a7375SFelipe Balbi /*
162550a7375SFelipe Balbi  * Start the URB at the front of an endpoint's queue
163550a7375SFelipe Balbi  * end must be claimed from the caller.
164550a7375SFelipe Balbi  *
165550a7375SFelipe Balbi  * Context: controller locked, irqs blocked
166550a7375SFelipe Balbi  */
167550a7375SFelipe Balbi static void
168550a7375SFelipe Balbi musb_start_urb(struct musb *musb, int is_in, struct musb_qh *qh)
169550a7375SFelipe Balbi {
170550a7375SFelipe Balbi 	u16			frame;
171550a7375SFelipe Balbi 	u32			len;
172550a7375SFelipe Balbi 	void			*buf;
173550a7375SFelipe Balbi 	void __iomem		*mbase =  musb->mregs;
174550a7375SFelipe Balbi 	struct urb		*urb = next_urb(qh);
175550a7375SFelipe Balbi 	struct musb_hw_ep	*hw_ep = qh->hw_ep;
176550a7375SFelipe Balbi 	unsigned		pipe = urb->pipe;
177550a7375SFelipe Balbi 	u8			address = usb_pipedevice(pipe);
178550a7375SFelipe Balbi 	int			epnum = hw_ep->epnum;
179550a7375SFelipe Balbi 
180550a7375SFelipe Balbi 	/* initialize software qh state */
181550a7375SFelipe Balbi 	qh->offset = 0;
182550a7375SFelipe Balbi 	qh->segsize = 0;
183550a7375SFelipe Balbi 
184550a7375SFelipe Balbi 	/* gather right source of data */
185550a7375SFelipe Balbi 	switch (qh->type) {
186550a7375SFelipe Balbi 	case USB_ENDPOINT_XFER_CONTROL:
187550a7375SFelipe Balbi 		/* control transfers always start with SETUP */
188550a7375SFelipe Balbi 		is_in = 0;
189550a7375SFelipe Balbi 		hw_ep->out_qh = qh;
190550a7375SFelipe Balbi 		musb->ep0_stage = MUSB_EP0_START;
191550a7375SFelipe Balbi 		buf = urb->setup_packet;
192550a7375SFelipe Balbi 		len = 8;
193550a7375SFelipe Balbi 		break;
194550a7375SFelipe Balbi 	case USB_ENDPOINT_XFER_ISOC:
195550a7375SFelipe Balbi 		qh->iso_idx = 0;
196550a7375SFelipe Balbi 		qh->frame = 0;
197550a7375SFelipe Balbi 		buf = urb->transfer_buffer + urb->iso_frame_desc[0].offset;
198550a7375SFelipe Balbi 		len = urb->iso_frame_desc[0].length;
199550a7375SFelipe Balbi 		break;
200550a7375SFelipe Balbi 	default:		/* bulk, interrupt */
201550a7375SFelipe Balbi 		buf = urb->transfer_buffer;
202550a7375SFelipe Balbi 		len = urb->transfer_buffer_length;
203550a7375SFelipe Balbi 	}
204550a7375SFelipe Balbi 
205550a7375SFelipe Balbi 	DBG(4, "qh %p urb %p dev%d ep%d%s%s, hw_ep %d, %p/%d\n",
206550a7375SFelipe Balbi 			qh, urb, address, qh->epnum,
207550a7375SFelipe Balbi 			is_in ? "in" : "out",
208550a7375SFelipe Balbi 			({char *s; switch (qh->type) {
209550a7375SFelipe Balbi 			case USB_ENDPOINT_XFER_CONTROL:	s = ""; break;
210550a7375SFelipe Balbi 			case USB_ENDPOINT_XFER_BULK:	s = "-bulk"; break;
211550a7375SFelipe Balbi 			case USB_ENDPOINT_XFER_ISOC:	s = "-iso"; break;
212550a7375SFelipe Balbi 			default:			s = "-intr"; break;
213550a7375SFelipe Balbi 			}; s; }),
214550a7375SFelipe Balbi 			epnum, buf, len);
215550a7375SFelipe Balbi 
216550a7375SFelipe Balbi 	/* Configure endpoint */
217550a7375SFelipe Balbi 	if (is_in || hw_ep->is_shared_fifo)
218550a7375SFelipe Balbi 		hw_ep->in_qh = qh;
219550a7375SFelipe Balbi 	else
220550a7375SFelipe Balbi 		hw_ep->out_qh = qh;
221550a7375SFelipe Balbi 	musb_ep_program(musb, epnum, urb, !is_in, buf, len);
222550a7375SFelipe Balbi 
223550a7375SFelipe Balbi 	/* transmit may have more work: start it when it is time */
224550a7375SFelipe Balbi 	if (is_in)
225550a7375SFelipe Balbi 		return;
226550a7375SFelipe Balbi 
227550a7375SFelipe Balbi 	/* determine if the time is right for a periodic transfer */
228550a7375SFelipe Balbi 	switch (qh->type) {
229550a7375SFelipe Balbi 	case USB_ENDPOINT_XFER_ISOC:
230550a7375SFelipe Balbi 	case USB_ENDPOINT_XFER_INT:
231550a7375SFelipe Balbi 		DBG(3, "check whether there's still time for periodic Tx\n");
232550a7375SFelipe Balbi 		qh->iso_idx = 0;
233550a7375SFelipe Balbi 		frame = musb_readw(mbase, MUSB_FRAME);
234550a7375SFelipe Balbi 		/* FIXME this doesn't implement that scheduling policy ...
235550a7375SFelipe Balbi 		 * or handle framecounter wrapping
236550a7375SFelipe Balbi 		 */
237550a7375SFelipe Balbi 		if ((urb->transfer_flags & URB_ISO_ASAP)
238550a7375SFelipe Balbi 				|| (frame >= urb->start_frame)) {
239550a7375SFelipe Balbi 			/* REVISIT the SOF irq handler shouldn't duplicate
240550a7375SFelipe Balbi 			 * this code; and we don't init urb->start_frame...
241550a7375SFelipe Balbi 			 */
242550a7375SFelipe Balbi 			qh->frame = 0;
243550a7375SFelipe Balbi 			goto start;
244550a7375SFelipe Balbi 		} else {
245550a7375SFelipe Balbi 			qh->frame = urb->start_frame;
246550a7375SFelipe Balbi 			/* enable SOF interrupt so we can count down */
247550a7375SFelipe Balbi 			DBG(1, "SOF for %d\n", epnum);
248550a7375SFelipe Balbi #if 1 /* ifndef	CONFIG_ARCH_DAVINCI */
249550a7375SFelipe Balbi 			musb_writeb(mbase, MUSB_INTRUSBE, 0xff);
250550a7375SFelipe Balbi #endif
251550a7375SFelipe Balbi 		}
252550a7375SFelipe Balbi 		break;
253550a7375SFelipe Balbi 	default:
254550a7375SFelipe Balbi start:
255550a7375SFelipe Balbi 		DBG(4, "Start TX%d %s\n", epnum,
256550a7375SFelipe Balbi 			hw_ep->tx_channel ? "dma" : "pio");
257550a7375SFelipe Balbi 
258550a7375SFelipe Balbi 		if (!hw_ep->tx_channel)
259550a7375SFelipe Balbi 			musb_h_tx_start(hw_ep);
260550a7375SFelipe Balbi 		else if (is_cppi_enabled() || tusb_dma_omap())
261550a7375SFelipe Balbi 			cppi_host_txdma_start(hw_ep);
262550a7375SFelipe Balbi 	}
263550a7375SFelipe Balbi }
264550a7375SFelipe Balbi 
265550a7375SFelipe Balbi /* caller owns controller lock, irqs are blocked */
266550a7375SFelipe Balbi static void
267550a7375SFelipe Balbi __musb_giveback(struct musb *musb, struct urb *urb, int status)
268550a7375SFelipe Balbi __releases(musb->lock)
269550a7375SFelipe Balbi __acquires(musb->lock)
270550a7375SFelipe Balbi {
271550a7375SFelipe Balbi 	DBG(({ int level; switch (urb->status) {
272550a7375SFelipe Balbi 				case 0:
273550a7375SFelipe Balbi 					level = 4;
274550a7375SFelipe Balbi 					break;
275550a7375SFelipe Balbi 				/* common/boring faults */
276550a7375SFelipe Balbi 				case -EREMOTEIO:
277550a7375SFelipe Balbi 				case -ESHUTDOWN:
278550a7375SFelipe Balbi 				case -ECONNRESET:
279550a7375SFelipe Balbi 				case -EPIPE:
280550a7375SFelipe Balbi 					level = 3;
281550a7375SFelipe Balbi 					break;
282550a7375SFelipe Balbi 				default:
283550a7375SFelipe Balbi 					level = 2;
284550a7375SFelipe Balbi 					break;
285550a7375SFelipe Balbi 				}; level; }),
286550a7375SFelipe Balbi 			"complete %p (%d), dev%d ep%d%s, %d/%d\n",
287550a7375SFelipe Balbi 			urb, urb->status,
288550a7375SFelipe Balbi 			usb_pipedevice(urb->pipe),
289550a7375SFelipe Balbi 			usb_pipeendpoint(urb->pipe),
290550a7375SFelipe Balbi 			usb_pipein(urb->pipe) ? "in" : "out",
291550a7375SFelipe Balbi 			urb->actual_length, urb->transfer_buffer_length
292550a7375SFelipe Balbi 			);
293550a7375SFelipe Balbi 
294550a7375SFelipe Balbi 	spin_unlock(&musb->lock);
295550a7375SFelipe Balbi 	usb_hcd_giveback_urb(musb_to_hcd(musb), urb, status);
296550a7375SFelipe Balbi 	spin_lock(&musb->lock);
297550a7375SFelipe Balbi }
298550a7375SFelipe Balbi 
299550a7375SFelipe Balbi /* for bulk/interrupt endpoints only */
300550a7375SFelipe Balbi static inline void
301550a7375SFelipe Balbi musb_save_toggle(struct musb_hw_ep *ep, int is_in, struct urb *urb)
302550a7375SFelipe Balbi {
303550a7375SFelipe Balbi 	struct usb_device	*udev = urb->dev;
304550a7375SFelipe Balbi 	u16			csr;
305550a7375SFelipe Balbi 	void __iomem		*epio = ep->regs;
306550a7375SFelipe Balbi 	struct musb_qh		*qh;
307550a7375SFelipe Balbi 
308550a7375SFelipe Balbi 	/* FIXME:  the current Mentor DMA code seems to have
309550a7375SFelipe Balbi 	 * problems getting toggle correct.
310550a7375SFelipe Balbi 	 */
311550a7375SFelipe Balbi 
312550a7375SFelipe Balbi 	if (is_in || ep->is_shared_fifo)
313550a7375SFelipe Balbi 		qh = ep->in_qh;
314550a7375SFelipe Balbi 	else
315550a7375SFelipe Balbi 		qh = ep->out_qh;
316550a7375SFelipe Balbi 
317550a7375SFelipe Balbi 	if (!is_in) {
318550a7375SFelipe Balbi 		csr = musb_readw(epio, MUSB_TXCSR);
319550a7375SFelipe Balbi 		usb_settoggle(udev, qh->epnum, 1,
320550a7375SFelipe Balbi 			(csr & MUSB_TXCSR_H_DATATOGGLE)
321550a7375SFelipe Balbi 				? 1 : 0);
322550a7375SFelipe Balbi 	} else {
323550a7375SFelipe Balbi 		csr = musb_readw(epio, MUSB_RXCSR);
324550a7375SFelipe Balbi 		usb_settoggle(udev, qh->epnum, 0,
325550a7375SFelipe Balbi 			(csr & MUSB_RXCSR_H_DATATOGGLE)
326550a7375SFelipe Balbi 				? 1 : 0);
327550a7375SFelipe Balbi 	}
328550a7375SFelipe Balbi }
329550a7375SFelipe Balbi 
330550a7375SFelipe Balbi /* caller owns controller lock, irqs are blocked */
331550a7375SFelipe Balbi static struct musb_qh *
332550a7375SFelipe Balbi musb_giveback(struct musb_qh *qh, struct urb *urb, int status)
333550a7375SFelipe Balbi {
334550a7375SFelipe Balbi 	int			is_in;
335550a7375SFelipe Balbi 	struct musb_hw_ep	*ep = qh->hw_ep;
336550a7375SFelipe Balbi 	struct musb		*musb = ep->musb;
337550a7375SFelipe Balbi 	int			ready = qh->is_ready;
338550a7375SFelipe Balbi 
339550a7375SFelipe Balbi 	if (ep->is_shared_fifo)
340550a7375SFelipe Balbi 		is_in = 1;
341550a7375SFelipe Balbi 	else
342550a7375SFelipe Balbi 		is_in = usb_pipein(urb->pipe);
343550a7375SFelipe Balbi 
344550a7375SFelipe Balbi 	/* save toggle eagerly, for paranoia */
345550a7375SFelipe Balbi 	switch (qh->type) {
346550a7375SFelipe Balbi 	case USB_ENDPOINT_XFER_BULK:
347550a7375SFelipe Balbi 	case USB_ENDPOINT_XFER_INT:
348550a7375SFelipe Balbi 		musb_save_toggle(ep, is_in, urb);
349550a7375SFelipe Balbi 		break;
350550a7375SFelipe Balbi 	case USB_ENDPOINT_XFER_ISOC:
351550a7375SFelipe Balbi 		if (status == 0 && urb->error_count)
352550a7375SFelipe Balbi 			status = -EXDEV;
353550a7375SFelipe Balbi 		break;
354550a7375SFelipe Balbi 	}
355550a7375SFelipe Balbi 
356550a7375SFelipe Balbi 	usb_hcd_unlink_urb_from_ep(musb_to_hcd(musb), urb);
357550a7375SFelipe Balbi 
358550a7375SFelipe Balbi 	qh->is_ready = 0;
359550a7375SFelipe Balbi 	__musb_giveback(musb, urb, status);
360550a7375SFelipe Balbi 	qh->is_ready = ready;
361550a7375SFelipe Balbi 
362550a7375SFelipe Balbi 	/* reclaim resources (and bandwidth) ASAP; deschedule it, and
363550a7375SFelipe Balbi 	 * invalidate qh as soon as list_empty(&hep->urb_list)
364550a7375SFelipe Balbi 	 */
365550a7375SFelipe Balbi 	if (list_empty(&qh->hep->urb_list)) {
366550a7375SFelipe Balbi 		struct list_head	*head;
367550a7375SFelipe Balbi 
368550a7375SFelipe Balbi 		if (is_in)
369550a7375SFelipe Balbi 			ep->rx_reinit = 1;
370550a7375SFelipe Balbi 		else
371550a7375SFelipe Balbi 			ep->tx_reinit = 1;
372550a7375SFelipe Balbi 
373550a7375SFelipe Balbi 		/* clobber old pointers to this qh */
374550a7375SFelipe Balbi 		if (is_in || ep->is_shared_fifo)
375550a7375SFelipe Balbi 			ep->in_qh = NULL;
376550a7375SFelipe Balbi 		else
377550a7375SFelipe Balbi 			ep->out_qh = NULL;
378550a7375SFelipe Balbi 		qh->hep->hcpriv = NULL;
379550a7375SFelipe Balbi 
380550a7375SFelipe Balbi 		switch (qh->type) {
381550a7375SFelipe Balbi 
382550a7375SFelipe Balbi 		case USB_ENDPOINT_XFER_ISOC:
383550a7375SFelipe Balbi 		case USB_ENDPOINT_XFER_INT:
384550a7375SFelipe Balbi 			/* this is where periodic bandwidth should be
385550a7375SFelipe Balbi 			 * de-allocated if it's tracked and allocated;
386550a7375SFelipe Balbi 			 * and where we'd update the schedule tree...
387550a7375SFelipe Balbi 			 */
388550a7375SFelipe Balbi 			musb->periodic[ep->epnum] = NULL;
389550a7375SFelipe Balbi 			kfree(qh);
390550a7375SFelipe Balbi 			qh = NULL;
391550a7375SFelipe Balbi 			break;
392550a7375SFelipe Balbi 
393550a7375SFelipe Balbi 		case USB_ENDPOINT_XFER_CONTROL:
394550a7375SFelipe Balbi 		case USB_ENDPOINT_XFER_BULK:
395550a7375SFelipe Balbi 			/* fifo policy for these lists, except that NAKing
396550a7375SFelipe Balbi 			 * should rotate a qh to the end (for fairness).
397550a7375SFelipe Balbi 			 */
398550a7375SFelipe Balbi 			head = qh->ring.prev;
399550a7375SFelipe Balbi 			list_del(&qh->ring);
400550a7375SFelipe Balbi 			kfree(qh);
401550a7375SFelipe Balbi 			qh = first_qh(head);
402550a7375SFelipe Balbi 			break;
403550a7375SFelipe Balbi 		}
404550a7375SFelipe Balbi 	}
405550a7375SFelipe Balbi 	return qh;
406550a7375SFelipe Balbi }
407550a7375SFelipe Balbi 
408550a7375SFelipe Balbi /*
409550a7375SFelipe Balbi  * Advance this hardware endpoint's queue, completing the specified urb and
410550a7375SFelipe Balbi  * advancing to either the next urb queued to that qh, or else invalidating
411550a7375SFelipe Balbi  * that qh and advancing to the next qh scheduled after the current one.
412550a7375SFelipe Balbi  *
413550a7375SFelipe Balbi  * Context: caller owns controller lock, irqs are blocked
414550a7375SFelipe Balbi  */
415550a7375SFelipe Balbi static void
416550a7375SFelipe Balbi musb_advance_schedule(struct musb *musb, struct urb *urb,
417550a7375SFelipe Balbi 		struct musb_hw_ep *hw_ep, int is_in)
418550a7375SFelipe Balbi {
419550a7375SFelipe Balbi 	struct musb_qh	*qh;
420550a7375SFelipe Balbi 
421550a7375SFelipe Balbi 	if (is_in || hw_ep->is_shared_fifo)
422550a7375SFelipe Balbi 		qh = hw_ep->in_qh;
423550a7375SFelipe Balbi 	else
424550a7375SFelipe Balbi 		qh = hw_ep->out_qh;
425550a7375SFelipe Balbi 
426550a7375SFelipe Balbi 	if (urb->status == -EINPROGRESS)
427550a7375SFelipe Balbi 		qh = musb_giveback(qh, urb, 0);
428550a7375SFelipe Balbi 	else
429550a7375SFelipe Balbi 		qh = musb_giveback(qh, urb, urb->status);
430550a7375SFelipe Balbi 
431550a7375SFelipe Balbi 	if (qh && qh->is_ready && !list_empty(&qh->hep->urb_list)) {
432550a7375SFelipe Balbi 		DBG(4, "... next ep%d %cX urb %p\n",
433550a7375SFelipe Balbi 				hw_ep->epnum, is_in ? 'R' : 'T',
434550a7375SFelipe Balbi 				next_urb(qh));
435550a7375SFelipe Balbi 		musb_start_urb(musb, is_in, qh);
436550a7375SFelipe Balbi 	}
437550a7375SFelipe Balbi }
438550a7375SFelipe Balbi 
439550a7375SFelipe Balbi static inline u16 musb_h_flush_rxfifo(struct musb_hw_ep *hw_ep, u16 csr)
440550a7375SFelipe Balbi {
441550a7375SFelipe Balbi 	/* we don't want fifo to fill itself again;
442550a7375SFelipe Balbi 	 * ignore dma (various models),
443550a7375SFelipe Balbi 	 * leave toggle alone (may not have been saved yet)
444550a7375SFelipe Balbi 	 */
445550a7375SFelipe Balbi 	csr |= MUSB_RXCSR_FLUSHFIFO | MUSB_RXCSR_RXPKTRDY;
446550a7375SFelipe Balbi 	csr &= ~(MUSB_RXCSR_H_REQPKT
447550a7375SFelipe Balbi 		| MUSB_RXCSR_H_AUTOREQ
448550a7375SFelipe Balbi 		| MUSB_RXCSR_AUTOCLEAR);
449550a7375SFelipe Balbi 
450550a7375SFelipe Balbi 	/* write 2x to allow double buffering */
451550a7375SFelipe Balbi 	musb_writew(hw_ep->regs, MUSB_RXCSR, csr);
452550a7375SFelipe Balbi 	musb_writew(hw_ep->regs, MUSB_RXCSR, csr);
453550a7375SFelipe Balbi 
454550a7375SFelipe Balbi 	/* flush writebuffer */
455550a7375SFelipe Balbi 	return musb_readw(hw_ep->regs, MUSB_RXCSR);
456550a7375SFelipe Balbi }
457550a7375SFelipe Balbi 
458550a7375SFelipe Balbi /*
459550a7375SFelipe Balbi  * PIO RX for a packet (or part of it).
460550a7375SFelipe Balbi  */
461550a7375SFelipe Balbi static bool
462550a7375SFelipe Balbi musb_host_packet_rx(struct musb *musb, struct urb *urb, u8 epnum, u8 iso_err)
463550a7375SFelipe Balbi {
464550a7375SFelipe Balbi 	u16			rx_count;
465550a7375SFelipe Balbi 	u8			*buf;
466550a7375SFelipe Balbi 	u16			csr;
467550a7375SFelipe Balbi 	bool			done = false;
468550a7375SFelipe Balbi 	u32			length;
469550a7375SFelipe Balbi 	int			do_flush = 0;
470550a7375SFelipe Balbi 	struct musb_hw_ep	*hw_ep = musb->endpoints + epnum;
471550a7375SFelipe Balbi 	void __iomem		*epio = hw_ep->regs;
472550a7375SFelipe Balbi 	struct musb_qh		*qh = hw_ep->in_qh;
473550a7375SFelipe Balbi 	int			pipe = urb->pipe;
474550a7375SFelipe Balbi 	void			*buffer = urb->transfer_buffer;
475550a7375SFelipe Balbi 
476550a7375SFelipe Balbi 	/* musb_ep_select(mbase, epnum); */
477550a7375SFelipe Balbi 	rx_count = musb_readw(epio, MUSB_RXCOUNT);
478550a7375SFelipe Balbi 	DBG(3, "RX%d count %d, buffer %p len %d/%d\n", epnum, rx_count,
479550a7375SFelipe Balbi 			urb->transfer_buffer, qh->offset,
480550a7375SFelipe Balbi 			urb->transfer_buffer_length);
481550a7375SFelipe Balbi 
482550a7375SFelipe Balbi 	/* unload FIFO */
483550a7375SFelipe Balbi 	if (usb_pipeisoc(pipe)) {
484550a7375SFelipe Balbi 		int					status = 0;
485550a7375SFelipe Balbi 		struct usb_iso_packet_descriptor	*d;
486550a7375SFelipe Balbi 
487550a7375SFelipe Balbi 		if (iso_err) {
488550a7375SFelipe Balbi 			status = -EILSEQ;
489550a7375SFelipe Balbi 			urb->error_count++;
490550a7375SFelipe Balbi 		}
491550a7375SFelipe Balbi 
492550a7375SFelipe Balbi 		d = urb->iso_frame_desc + qh->iso_idx;
493550a7375SFelipe Balbi 		buf = buffer + d->offset;
494550a7375SFelipe Balbi 		length = d->length;
495550a7375SFelipe Balbi 		if (rx_count > length) {
496550a7375SFelipe Balbi 			if (status == 0) {
497550a7375SFelipe Balbi 				status = -EOVERFLOW;
498550a7375SFelipe Balbi 				urb->error_count++;
499550a7375SFelipe Balbi 			}
500550a7375SFelipe Balbi 			DBG(2, "** OVERFLOW %d into %d\n", rx_count, length);
501550a7375SFelipe Balbi 			do_flush = 1;
502550a7375SFelipe Balbi 		} else
503550a7375SFelipe Balbi 			length = rx_count;
504550a7375SFelipe Balbi 		urb->actual_length += length;
505550a7375SFelipe Balbi 		d->actual_length = length;
506550a7375SFelipe Balbi 
507550a7375SFelipe Balbi 		d->status = status;
508550a7375SFelipe Balbi 
509550a7375SFelipe Balbi 		/* see if we are done */
510550a7375SFelipe Balbi 		done = (++qh->iso_idx >= urb->number_of_packets);
511550a7375SFelipe Balbi 	} else {
512550a7375SFelipe Balbi 		/* non-isoch */
513550a7375SFelipe Balbi 		buf = buffer + qh->offset;
514550a7375SFelipe Balbi 		length = urb->transfer_buffer_length - qh->offset;
515550a7375SFelipe Balbi 		if (rx_count > length) {
516550a7375SFelipe Balbi 			if (urb->status == -EINPROGRESS)
517550a7375SFelipe Balbi 				urb->status = -EOVERFLOW;
518550a7375SFelipe Balbi 			DBG(2, "** OVERFLOW %d into %d\n", rx_count, length);
519550a7375SFelipe Balbi 			do_flush = 1;
520550a7375SFelipe Balbi 		} else
521550a7375SFelipe Balbi 			length = rx_count;
522550a7375SFelipe Balbi 		urb->actual_length += length;
523550a7375SFelipe Balbi 		qh->offset += length;
524550a7375SFelipe Balbi 
525550a7375SFelipe Balbi 		/* see if we are done */
526550a7375SFelipe Balbi 		done = (urb->actual_length == urb->transfer_buffer_length)
527550a7375SFelipe Balbi 			|| (rx_count < qh->maxpacket)
528550a7375SFelipe Balbi 			|| (urb->status != -EINPROGRESS);
529550a7375SFelipe Balbi 		if (done
530550a7375SFelipe Balbi 				&& (urb->status == -EINPROGRESS)
531550a7375SFelipe Balbi 				&& (urb->transfer_flags & URB_SHORT_NOT_OK)
532550a7375SFelipe Balbi 				&& (urb->actual_length
533550a7375SFelipe Balbi 					< urb->transfer_buffer_length))
534550a7375SFelipe Balbi 			urb->status = -EREMOTEIO;
535550a7375SFelipe Balbi 	}
536550a7375SFelipe Balbi 
537550a7375SFelipe Balbi 	musb_read_fifo(hw_ep, length, buf);
538550a7375SFelipe Balbi 
539550a7375SFelipe Balbi 	csr = musb_readw(epio, MUSB_RXCSR);
540550a7375SFelipe Balbi 	csr |= MUSB_RXCSR_H_WZC_BITS;
541550a7375SFelipe Balbi 	if (unlikely(do_flush))
542550a7375SFelipe Balbi 		musb_h_flush_rxfifo(hw_ep, csr);
543550a7375SFelipe Balbi 	else {
544550a7375SFelipe Balbi 		/* REVISIT this assumes AUTOCLEAR is never set */
545550a7375SFelipe Balbi 		csr &= ~(MUSB_RXCSR_RXPKTRDY | MUSB_RXCSR_H_REQPKT);
546550a7375SFelipe Balbi 		if (!done)
547550a7375SFelipe Balbi 			csr |= MUSB_RXCSR_H_REQPKT;
548550a7375SFelipe Balbi 		musb_writew(epio, MUSB_RXCSR, csr);
549550a7375SFelipe Balbi 	}
550550a7375SFelipe Balbi 
551550a7375SFelipe Balbi 	return done;
552550a7375SFelipe Balbi }
553550a7375SFelipe Balbi 
554550a7375SFelipe Balbi /* we don't always need to reinit a given side of an endpoint...
555550a7375SFelipe Balbi  * when we do, use tx/rx reinit routine and then construct a new CSR
556550a7375SFelipe Balbi  * to address data toggle, NYET, and DMA or PIO.
557550a7375SFelipe Balbi  *
558550a7375SFelipe Balbi  * it's possible that driver bugs (especially for DMA) or aborting a
559550a7375SFelipe Balbi  * transfer might have left the endpoint busier than it should be.
560550a7375SFelipe Balbi  * the busy/not-empty tests are basically paranoia.
561550a7375SFelipe Balbi  */
562550a7375SFelipe Balbi static void
563550a7375SFelipe Balbi musb_rx_reinit(struct musb *musb, struct musb_qh *qh, struct musb_hw_ep *ep)
564550a7375SFelipe Balbi {
565550a7375SFelipe Balbi 	u16	csr;
566550a7375SFelipe Balbi 
567550a7375SFelipe Balbi 	/* NOTE:  we know the "rx" fifo reinit never triggers for ep0.
568550a7375SFelipe Balbi 	 * That always uses tx_reinit since ep0 repurposes TX register
569550a7375SFelipe Balbi 	 * offsets; the initial SETUP packet is also a kind of OUT.
570550a7375SFelipe Balbi 	 */
571550a7375SFelipe Balbi 
572550a7375SFelipe Balbi 	/* if programmed for Tx, put it in RX mode */
573550a7375SFelipe Balbi 	if (ep->is_shared_fifo) {
574550a7375SFelipe Balbi 		csr = musb_readw(ep->regs, MUSB_TXCSR);
575550a7375SFelipe Balbi 		if (csr & MUSB_TXCSR_MODE) {
576550a7375SFelipe Balbi 			musb_h_tx_flush_fifo(ep);
577550a7375SFelipe Balbi 			musb_writew(ep->regs, MUSB_TXCSR,
578550a7375SFelipe Balbi 					MUSB_TXCSR_FRCDATATOG);
579550a7375SFelipe Balbi 		}
580550a7375SFelipe Balbi 		/* clear mode (and everything else) to enable Rx */
581550a7375SFelipe Balbi 		musb_writew(ep->regs, MUSB_TXCSR, 0);
582550a7375SFelipe Balbi 
583550a7375SFelipe Balbi 	/* scrub all previous state, clearing toggle */
584550a7375SFelipe Balbi 	} else {
585550a7375SFelipe Balbi 		csr = musb_readw(ep->regs, MUSB_RXCSR);
586550a7375SFelipe Balbi 		if (csr & MUSB_RXCSR_RXPKTRDY)
587550a7375SFelipe Balbi 			WARNING("rx%d, packet/%d ready?\n", ep->epnum,
588550a7375SFelipe Balbi 				musb_readw(ep->regs, MUSB_RXCOUNT));
589550a7375SFelipe Balbi 
590550a7375SFelipe Balbi 		musb_h_flush_rxfifo(ep, MUSB_RXCSR_CLRDATATOG);
591550a7375SFelipe Balbi 	}
592550a7375SFelipe Balbi 
593550a7375SFelipe Balbi 	/* target addr and (for multipoint) hub addr/port */
594550a7375SFelipe Balbi 	if (musb->is_multipoint) {
595550a7375SFelipe Balbi 		musb_writeb(ep->target_regs, MUSB_RXFUNCADDR,
596550a7375SFelipe Balbi 			qh->addr_reg);
597550a7375SFelipe Balbi 		musb_writeb(ep->target_regs, MUSB_RXHUBADDR,
598550a7375SFelipe Balbi 			qh->h_addr_reg);
599550a7375SFelipe Balbi 		musb_writeb(ep->target_regs, MUSB_RXHUBPORT,
600550a7375SFelipe Balbi 			qh->h_port_reg);
601550a7375SFelipe Balbi 	} else
602550a7375SFelipe Balbi 		musb_writeb(musb->mregs, MUSB_FADDR, qh->addr_reg);
603550a7375SFelipe Balbi 
604550a7375SFelipe Balbi 	/* protocol/endpoint, interval/NAKlimit, i/o size */
605550a7375SFelipe Balbi 	musb_writeb(ep->regs, MUSB_RXTYPE, qh->type_reg);
606550a7375SFelipe Balbi 	musb_writeb(ep->regs, MUSB_RXINTERVAL, qh->intv_reg);
607550a7375SFelipe Balbi 	/* NOTE: bulk combining rewrites high bits of maxpacket */
608550a7375SFelipe Balbi 	musb_writew(ep->regs, MUSB_RXMAXP, qh->maxpacket);
609550a7375SFelipe Balbi 
610550a7375SFelipe Balbi 	ep->rx_reinit = 0;
611550a7375SFelipe Balbi }
612550a7375SFelipe Balbi 
613550a7375SFelipe Balbi 
614550a7375SFelipe Balbi /*
615550a7375SFelipe Balbi  * Program an HDRC endpoint as per the given URB
616550a7375SFelipe Balbi  * Context: irqs blocked, controller lock held
617550a7375SFelipe Balbi  */
618550a7375SFelipe Balbi static void musb_ep_program(struct musb *musb, u8 epnum,
619550a7375SFelipe Balbi 			struct urb *urb, unsigned int is_out,
620550a7375SFelipe Balbi 			u8 *buf, u32 len)
621550a7375SFelipe Balbi {
622550a7375SFelipe Balbi 	struct dma_controller	*dma_controller;
623550a7375SFelipe Balbi 	struct dma_channel	*dma_channel;
624550a7375SFelipe Balbi 	u8			dma_ok;
625550a7375SFelipe Balbi 	void __iomem		*mbase = musb->mregs;
626550a7375SFelipe Balbi 	struct musb_hw_ep	*hw_ep = musb->endpoints + epnum;
627550a7375SFelipe Balbi 	void __iomem		*epio = hw_ep->regs;
628550a7375SFelipe Balbi 	struct musb_qh		*qh;
629550a7375SFelipe Balbi 	u16			packet_sz;
630550a7375SFelipe Balbi 
631550a7375SFelipe Balbi 	if (!is_out || hw_ep->is_shared_fifo)
632550a7375SFelipe Balbi 		qh = hw_ep->in_qh;
633550a7375SFelipe Balbi 	else
634550a7375SFelipe Balbi 		qh = hw_ep->out_qh;
635550a7375SFelipe Balbi 
636550a7375SFelipe Balbi 	packet_sz = qh->maxpacket;
637550a7375SFelipe Balbi 
638550a7375SFelipe Balbi 	DBG(3, "%s hw%d urb %p spd%d dev%d ep%d%s "
639550a7375SFelipe Balbi 				"h_addr%02x h_port%02x bytes %d\n",
640550a7375SFelipe Balbi 			is_out ? "-->" : "<--",
641550a7375SFelipe Balbi 			epnum, urb, urb->dev->speed,
642550a7375SFelipe Balbi 			qh->addr_reg, qh->epnum, is_out ? "out" : "in",
643550a7375SFelipe Balbi 			qh->h_addr_reg, qh->h_port_reg,
644550a7375SFelipe Balbi 			len);
645550a7375SFelipe Balbi 
646550a7375SFelipe Balbi 	musb_ep_select(mbase, epnum);
647550a7375SFelipe Balbi 
648550a7375SFelipe Balbi 	/* candidate for DMA? */
649550a7375SFelipe Balbi 	dma_controller = musb->dma_controller;
650550a7375SFelipe Balbi 	if (is_dma_capable() && epnum && dma_controller) {
651550a7375SFelipe Balbi 		dma_channel = is_out ? hw_ep->tx_channel : hw_ep->rx_channel;
652550a7375SFelipe Balbi 		if (!dma_channel) {
653550a7375SFelipe Balbi 			dma_channel = dma_controller->channel_alloc(
654550a7375SFelipe Balbi 					dma_controller, hw_ep, is_out);
655550a7375SFelipe Balbi 			if (is_out)
656550a7375SFelipe Balbi 				hw_ep->tx_channel = dma_channel;
657550a7375SFelipe Balbi 			else
658550a7375SFelipe Balbi 				hw_ep->rx_channel = dma_channel;
659550a7375SFelipe Balbi 		}
660550a7375SFelipe Balbi 	} else
661550a7375SFelipe Balbi 		dma_channel = NULL;
662550a7375SFelipe Balbi 
663550a7375SFelipe Balbi 	/* make sure we clear DMAEnab, autoSet bits from previous run */
664550a7375SFelipe Balbi 
665550a7375SFelipe Balbi 	/* OUT/transmit/EP0 or IN/receive? */
666550a7375SFelipe Balbi 	if (is_out) {
667550a7375SFelipe Balbi 		u16	csr;
668550a7375SFelipe Balbi 		u16	int_txe;
669550a7375SFelipe Balbi 		u16	load_count;
670550a7375SFelipe Balbi 
671550a7375SFelipe Balbi 		csr = musb_readw(epio, MUSB_TXCSR);
672550a7375SFelipe Balbi 
673550a7375SFelipe Balbi 		/* disable interrupt in case we flush */
674550a7375SFelipe Balbi 		int_txe = musb_readw(mbase, MUSB_INTRTXE);
675550a7375SFelipe Balbi 		musb_writew(mbase, MUSB_INTRTXE, int_txe & ~(1 << epnum));
676550a7375SFelipe Balbi 
677550a7375SFelipe Balbi 		/* general endpoint setup */
678550a7375SFelipe Balbi 		if (epnum) {
679550a7375SFelipe Balbi 			/* ASSERT:  TXCSR_DMAENAB was already cleared */
680550a7375SFelipe Balbi 
681550a7375SFelipe Balbi 			/* flush all old state, set default */
682550a7375SFelipe Balbi 			musb_h_tx_flush_fifo(hw_ep);
683550a7375SFelipe Balbi 			csr &= ~(MUSB_TXCSR_H_NAKTIMEOUT
684550a7375SFelipe Balbi 					| MUSB_TXCSR_DMAMODE
685550a7375SFelipe Balbi 					| MUSB_TXCSR_FRCDATATOG
686550a7375SFelipe Balbi 					| MUSB_TXCSR_H_RXSTALL
687550a7375SFelipe Balbi 					| MUSB_TXCSR_H_ERROR
688550a7375SFelipe Balbi 					| MUSB_TXCSR_TXPKTRDY
689550a7375SFelipe Balbi 					);
690550a7375SFelipe Balbi 			csr |= MUSB_TXCSR_MODE;
691550a7375SFelipe Balbi 
692550a7375SFelipe Balbi 			if (usb_gettoggle(urb->dev,
693550a7375SFelipe Balbi 					qh->epnum, 1))
694550a7375SFelipe Balbi 				csr |= MUSB_TXCSR_H_WR_DATATOGGLE
695550a7375SFelipe Balbi 					| MUSB_TXCSR_H_DATATOGGLE;
696550a7375SFelipe Balbi 			else
697550a7375SFelipe Balbi 				csr |= MUSB_TXCSR_CLRDATATOG;
698550a7375SFelipe Balbi 
699550a7375SFelipe Balbi 			/* twice in case of double packet buffering */
700550a7375SFelipe Balbi 			musb_writew(epio, MUSB_TXCSR, csr);
701550a7375SFelipe Balbi 			/* REVISIT may need to clear FLUSHFIFO ... */
702550a7375SFelipe Balbi 			musb_writew(epio, MUSB_TXCSR, csr);
703550a7375SFelipe Balbi 			csr = musb_readw(epio, MUSB_TXCSR);
704550a7375SFelipe Balbi 		} else {
705550a7375SFelipe Balbi 			/* endpoint 0: just flush */
706550a7375SFelipe Balbi 			musb_writew(epio, MUSB_CSR0,
707550a7375SFelipe Balbi 				csr | MUSB_CSR0_FLUSHFIFO);
708550a7375SFelipe Balbi 			musb_writew(epio, MUSB_CSR0,
709550a7375SFelipe Balbi 				csr | MUSB_CSR0_FLUSHFIFO);
710550a7375SFelipe Balbi 		}
711550a7375SFelipe Balbi 
712550a7375SFelipe Balbi 		/* target addr and (for multipoint) hub addr/port */
713550a7375SFelipe Balbi 		if (musb->is_multipoint) {
714550a7375SFelipe Balbi 			musb_writeb(mbase,
715550a7375SFelipe Balbi 				MUSB_BUSCTL_OFFSET(epnum, MUSB_TXFUNCADDR),
716550a7375SFelipe Balbi 				qh->addr_reg);
717550a7375SFelipe Balbi 			musb_writeb(mbase,
718550a7375SFelipe Balbi 				MUSB_BUSCTL_OFFSET(epnum, MUSB_TXHUBADDR),
719550a7375SFelipe Balbi 				qh->h_addr_reg);
720550a7375SFelipe Balbi 			musb_writeb(mbase,
721550a7375SFelipe Balbi 				MUSB_BUSCTL_OFFSET(epnum, MUSB_TXHUBPORT),
722550a7375SFelipe Balbi 				qh->h_port_reg);
723550a7375SFelipe Balbi /* FIXME if !epnum, do the same for RX ... */
724550a7375SFelipe Balbi 		} else
725550a7375SFelipe Balbi 			musb_writeb(mbase, MUSB_FADDR, qh->addr_reg);
726550a7375SFelipe Balbi 
727550a7375SFelipe Balbi 		/* protocol/endpoint/interval/NAKlimit */
728550a7375SFelipe Balbi 		if (epnum) {
729550a7375SFelipe Balbi 			musb_writeb(epio, MUSB_TXTYPE, qh->type_reg);
730550a7375SFelipe Balbi 			if (can_bulk_split(musb, qh->type))
731550a7375SFelipe Balbi 				musb_writew(epio, MUSB_TXMAXP,
732550a7375SFelipe Balbi 					packet_sz
733550a7375SFelipe Balbi 					| ((hw_ep->max_packet_sz_tx /
734550a7375SFelipe Balbi 						packet_sz) - 1) << 11);
735550a7375SFelipe Balbi 			else
736550a7375SFelipe Balbi 				musb_writew(epio, MUSB_TXMAXP,
737550a7375SFelipe Balbi 					packet_sz);
738550a7375SFelipe Balbi 			musb_writeb(epio, MUSB_TXINTERVAL, qh->intv_reg);
739550a7375SFelipe Balbi 		} else {
740550a7375SFelipe Balbi 			musb_writeb(epio, MUSB_NAKLIMIT0, qh->intv_reg);
741550a7375SFelipe Balbi 			if (musb->is_multipoint)
742550a7375SFelipe Balbi 				musb_writeb(epio, MUSB_TYPE0,
743550a7375SFelipe Balbi 						qh->type_reg);
744550a7375SFelipe Balbi 		}
745550a7375SFelipe Balbi 
746550a7375SFelipe Balbi 		if (can_bulk_split(musb, qh->type))
747550a7375SFelipe Balbi 			load_count = min((u32) hw_ep->max_packet_sz_tx,
748550a7375SFelipe Balbi 						len);
749550a7375SFelipe Balbi 		else
750550a7375SFelipe Balbi 			load_count = min((u32) packet_sz, len);
751550a7375SFelipe Balbi 
752550a7375SFelipe Balbi #ifdef CONFIG_USB_INVENTRA_DMA
753550a7375SFelipe Balbi 		if (dma_channel) {
754550a7375SFelipe Balbi 
755550a7375SFelipe Balbi 			/* clear previous state */
756550a7375SFelipe Balbi 			csr = musb_readw(epio, MUSB_TXCSR);
757550a7375SFelipe Balbi 			csr &= ~(MUSB_TXCSR_AUTOSET
758550a7375SFelipe Balbi 				| MUSB_TXCSR_DMAMODE
759550a7375SFelipe Balbi 				| MUSB_TXCSR_DMAENAB);
760550a7375SFelipe Balbi 			csr |= MUSB_TXCSR_MODE;
761550a7375SFelipe Balbi 			musb_writew(epio, MUSB_TXCSR,
762550a7375SFelipe Balbi 				csr | MUSB_TXCSR_MODE);
763550a7375SFelipe Balbi 
764550a7375SFelipe Balbi 			qh->segsize = min(len, dma_channel->max_len);
765550a7375SFelipe Balbi 
766550a7375SFelipe Balbi 			if (qh->segsize <= packet_sz)
767550a7375SFelipe Balbi 				dma_channel->desired_mode = 0;
768550a7375SFelipe Balbi 			else
769550a7375SFelipe Balbi 				dma_channel->desired_mode = 1;
770550a7375SFelipe Balbi 
771550a7375SFelipe Balbi 
772550a7375SFelipe Balbi 			if (dma_channel->desired_mode == 0) {
773550a7375SFelipe Balbi 				csr &= ~(MUSB_TXCSR_AUTOSET
774550a7375SFelipe Balbi 					| MUSB_TXCSR_DMAMODE);
775550a7375SFelipe Balbi 				csr |= (MUSB_TXCSR_DMAENAB);
776550a7375SFelipe Balbi 					/* against programming guide */
777550a7375SFelipe Balbi 			} else
778550a7375SFelipe Balbi 				csr |= (MUSB_TXCSR_AUTOSET
779550a7375SFelipe Balbi 					| MUSB_TXCSR_DMAENAB
780550a7375SFelipe Balbi 					| MUSB_TXCSR_DMAMODE);
781550a7375SFelipe Balbi 
782550a7375SFelipe Balbi 			musb_writew(epio, MUSB_TXCSR, csr);
783550a7375SFelipe Balbi 
784550a7375SFelipe Balbi 			dma_ok = dma_controller->channel_program(
785550a7375SFelipe Balbi 					dma_channel, packet_sz,
786550a7375SFelipe Balbi 					dma_channel->desired_mode,
787550a7375SFelipe Balbi 					urb->transfer_dma,
788550a7375SFelipe Balbi 					qh->segsize);
789550a7375SFelipe Balbi 			if (dma_ok) {
790550a7375SFelipe Balbi 				load_count = 0;
791550a7375SFelipe Balbi 			} else {
792550a7375SFelipe Balbi 				dma_controller->channel_release(dma_channel);
793550a7375SFelipe Balbi 				if (is_out)
794550a7375SFelipe Balbi 					hw_ep->tx_channel = NULL;
795550a7375SFelipe Balbi 				else
796550a7375SFelipe Balbi 					hw_ep->rx_channel = NULL;
797550a7375SFelipe Balbi 				dma_channel = NULL;
798550a7375SFelipe Balbi 			}
799550a7375SFelipe Balbi 		}
800550a7375SFelipe Balbi #endif
801550a7375SFelipe Balbi 
802550a7375SFelipe Balbi 		/* candidate for DMA */
803550a7375SFelipe Balbi 		if ((is_cppi_enabled() || tusb_dma_omap()) && dma_channel) {
804550a7375SFelipe Balbi 
805550a7375SFelipe Balbi 			/* program endpoint CSRs first, then setup DMA.
806550a7375SFelipe Balbi 			 * assume CPPI setup succeeds.
807550a7375SFelipe Balbi 			 * defer enabling dma.
808550a7375SFelipe Balbi 			 */
809550a7375SFelipe Balbi 			csr = musb_readw(epio, MUSB_TXCSR);
810550a7375SFelipe Balbi 			csr &= ~(MUSB_TXCSR_AUTOSET
811550a7375SFelipe Balbi 					| MUSB_TXCSR_DMAMODE
812550a7375SFelipe Balbi 					| MUSB_TXCSR_DMAENAB);
813550a7375SFelipe Balbi 			csr |= MUSB_TXCSR_MODE;
814550a7375SFelipe Balbi 			musb_writew(epio, MUSB_TXCSR,
815550a7375SFelipe Balbi 				csr | MUSB_TXCSR_MODE);
816550a7375SFelipe Balbi 
817550a7375SFelipe Balbi 			dma_channel->actual_len = 0L;
818550a7375SFelipe Balbi 			qh->segsize = len;
819550a7375SFelipe Balbi 
820550a7375SFelipe Balbi 			/* TX uses "rndis" mode automatically, but needs help
821550a7375SFelipe Balbi 			 * to identify the zero-length-final-packet case.
822550a7375SFelipe Balbi 			 */
823550a7375SFelipe Balbi 			dma_ok = dma_controller->channel_program(
824550a7375SFelipe Balbi 					dma_channel, packet_sz,
825550a7375SFelipe Balbi 					(urb->transfer_flags
826550a7375SFelipe Balbi 							& URB_ZERO_PACKET)
827550a7375SFelipe Balbi 						== URB_ZERO_PACKET,
828550a7375SFelipe Balbi 					urb->transfer_dma,
829550a7375SFelipe Balbi 					qh->segsize);
830550a7375SFelipe Balbi 			if (dma_ok) {
831550a7375SFelipe Balbi 				load_count = 0;
832550a7375SFelipe Balbi 			} else {
833550a7375SFelipe Balbi 				dma_controller->channel_release(dma_channel);
834550a7375SFelipe Balbi 				hw_ep->tx_channel = NULL;
835550a7375SFelipe Balbi 				dma_channel = NULL;
836550a7375SFelipe Balbi 
837550a7375SFelipe Balbi 				/* REVISIT there's an error path here that
838550a7375SFelipe Balbi 				 * needs handling:  can't do dma, but
839550a7375SFelipe Balbi 				 * there's no pio buffer address...
840550a7375SFelipe Balbi 				 */
841550a7375SFelipe Balbi 			}
842550a7375SFelipe Balbi 		}
843550a7375SFelipe Balbi 
844550a7375SFelipe Balbi 		if (load_count) {
845550a7375SFelipe Balbi 			/* ASSERT:  TXCSR_DMAENAB was already cleared */
846550a7375SFelipe Balbi 
847550a7375SFelipe Balbi 			/* PIO to load FIFO */
848550a7375SFelipe Balbi 			qh->segsize = load_count;
849550a7375SFelipe Balbi 			musb_write_fifo(hw_ep, load_count, buf);
850550a7375SFelipe Balbi 			csr = musb_readw(epio, MUSB_TXCSR);
851550a7375SFelipe Balbi 			csr &= ~(MUSB_TXCSR_DMAENAB
852550a7375SFelipe Balbi 				| MUSB_TXCSR_DMAMODE
853550a7375SFelipe Balbi 				| MUSB_TXCSR_AUTOSET);
854550a7375SFelipe Balbi 			/* write CSR */
855550a7375SFelipe Balbi 			csr |= MUSB_TXCSR_MODE;
856550a7375SFelipe Balbi 
857550a7375SFelipe Balbi 			if (epnum)
858550a7375SFelipe Balbi 				musb_writew(epio, MUSB_TXCSR, csr);
859550a7375SFelipe Balbi 		}
860550a7375SFelipe Balbi 
861550a7375SFelipe Balbi 		/* re-enable interrupt */
862550a7375SFelipe Balbi 		musb_writew(mbase, MUSB_INTRTXE, int_txe);
863550a7375SFelipe Balbi 
864550a7375SFelipe Balbi 	/* IN/receive */
865550a7375SFelipe Balbi 	} else {
866550a7375SFelipe Balbi 		u16	csr;
867550a7375SFelipe Balbi 
868550a7375SFelipe Balbi 		if (hw_ep->rx_reinit) {
869550a7375SFelipe Balbi 			musb_rx_reinit(musb, qh, hw_ep);
870550a7375SFelipe Balbi 
871550a7375SFelipe Balbi 			/* init new state: toggle and NYET, maybe DMA later */
872550a7375SFelipe Balbi 			if (usb_gettoggle(urb->dev, qh->epnum, 0))
873550a7375SFelipe Balbi 				csr = MUSB_RXCSR_H_WR_DATATOGGLE
874550a7375SFelipe Balbi 					| MUSB_RXCSR_H_DATATOGGLE;
875550a7375SFelipe Balbi 			else
876550a7375SFelipe Balbi 				csr = 0;
877550a7375SFelipe Balbi 			if (qh->type == USB_ENDPOINT_XFER_INT)
878550a7375SFelipe Balbi 				csr |= MUSB_RXCSR_DISNYET;
879550a7375SFelipe Balbi 
880550a7375SFelipe Balbi 		} else {
881550a7375SFelipe Balbi 			csr = musb_readw(hw_ep->regs, MUSB_RXCSR);
882550a7375SFelipe Balbi 
883550a7375SFelipe Balbi 			if (csr & (MUSB_RXCSR_RXPKTRDY
884550a7375SFelipe Balbi 					| MUSB_RXCSR_DMAENAB
885550a7375SFelipe Balbi 					| MUSB_RXCSR_H_REQPKT))
886550a7375SFelipe Balbi 				ERR("broken !rx_reinit, ep%d csr %04x\n",
887550a7375SFelipe Balbi 						hw_ep->epnum, csr);
888550a7375SFelipe Balbi 
889550a7375SFelipe Balbi 			/* scrub any stale state, leaving toggle alone */
890550a7375SFelipe Balbi 			csr &= MUSB_RXCSR_DISNYET;
891550a7375SFelipe Balbi 		}
892550a7375SFelipe Balbi 
893550a7375SFelipe Balbi 		/* kick things off */
894550a7375SFelipe Balbi 
895550a7375SFelipe Balbi 		if ((is_cppi_enabled() || tusb_dma_omap()) && dma_channel) {
896550a7375SFelipe Balbi 			/* candidate for DMA */
897550a7375SFelipe Balbi 			if (dma_channel) {
898550a7375SFelipe Balbi 				dma_channel->actual_len = 0L;
899550a7375SFelipe Balbi 				qh->segsize = len;
900550a7375SFelipe Balbi 
901550a7375SFelipe Balbi 				/* AUTOREQ is in a DMA register */
902550a7375SFelipe Balbi 				musb_writew(hw_ep->regs, MUSB_RXCSR, csr);
903550a7375SFelipe Balbi 				csr = musb_readw(hw_ep->regs,
904550a7375SFelipe Balbi 						MUSB_RXCSR);
905550a7375SFelipe Balbi 
906550a7375SFelipe Balbi 				/* unless caller treats short rx transfers as
907550a7375SFelipe Balbi 				 * errors, we dare not queue multiple transfers.
908550a7375SFelipe Balbi 				 */
909550a7375SFelipe Balbi 				dma_ok = dma_controller->channel_program(
910550a7375SFelipe Balbi 						dma_channel, packet_sz,
911550a7375SFelipe Balbi 						!(urb->transfer_flags
912550a7375SFelipe Balbi 							& URB_SHORT_NOT_OK),
913550a7375SFelipe Balbi 						urb->transfer_dma,
914550a7375SFelipe Balbi 						qh->segsize);
915550a7375SFelipe Balbi 				if (!dma_ok) {
916550a7375SFelipe Balbi 					dma_controller->channel_release(
917550a7375SFelipe Balbi 							dma_channel);
918550a7375SFelipe Balbi 					hw_ep->rx_channel = NULL;
919550a7375SFelipe Balbi 					dma_channel = NULL;
920550a7375SFelipe Balbi 				} else
921550a7375SFelipe Balbi 					csr |= MUSB_RXCSR_DMAENAB;
922550a7375SFelipe Balbi 			}
923550a7375SFelipe Balbi 		}
924550a7375SFelipe Balbi 
925550a7375SFelipe Balbi 		csr |= MUSB_RXCSR_H_REQPKT;
926550a7375SFelipe Balbi 		DBG(7, "RXCSR%d := %04x\n", epnum, csr);
927550a7375SFelipe Balbi 		musb_writew(hw_ep->regs, MUSB_RXCSR, csr);
928550a7375SFelipe Balbi 		csr = musb_readw(hw_ep->regs, MUSB_RXCSR);
929550a7375SFelipe Balbi 	}
930550a7375SFelipe Balbi }
931550a7375SFelipe Balbi 
932550a7375SFelipe Balbi 
933550a7375SFelipe Balbi /*
934550a7375SFelipe Balbi  * Service the default endpoint (ep0) as host.
935550a7375SFelipe Balbi  * Return true until it's time to start the status stage.
936550a7375SFelipe Balbi  */
937550a7375SFelipe Balbi static bool musb_h_ep0_continue(struct musb *musb, u16 len, struct urb *urb)
938550a7375SFelipe Balbi {
939550a7375SFelipe Balbi 	bool			 more = false;
940550a7375SFelipe Balbi 	u8			*fifo_dest = NULL;
941550a7375SFelipe Balbi 	u16			fifo_count = 0;
942550a7375SFelipe Balbi 	struct musb_hw_ep	*hw_ep = musb->control_ep;
943550a7375SFelipe Balbi 	struct musb_qh		*qh = hw_ep->in_qh;
944550a7375SFelipe Balbi 	struct usb_ctrlrequest	*request;
945550a7375SFelipe Balbi 
946550a7375SFelipe Balbi 	switch (musb->ep0_stage) {
947550a7375SFelipe Balbi 	case MUSB_EP0_IN:
948550a7375SFelipe Balbi 		fifo_dest = urb->transfer_buffer + urb->actual_length;
949550a7375SFelipe Balbi 		fifo_count = min(len, ((u16) (urb->transfer_buffer_length
950550a7375SFelipe Balbi 					- urb->actual_length)));
951550a7375SFelipe Balbi 		if (fifo_count < len)
952550a7375SFelipe Balbi 			urb->status = -EOVERFLOW;
953550a7375SFelipe Balbi 
954550a7375SFelipe Balbi 		musb_read_fifo(hw_ep, fifo_count, fifo_dest);
955550a7375SFelipe Balbi 
956550a7375SFelipe Balbi 		urb->actual_length += fifo_count;
957550a7375SFelipe Balbi 		if (len < qh->maxpacket) {
958550a7375SFelipe Balbi 			/* always terminate on short read; it's
959550a7375SFelipe Balbi 			 * rarely reported as an error.
960550a7375SFelipe Balbi 			 */
961550a7375SFelipe Balbi 		} else if (urb->actual_length <
962550a7375SFelipe Balbi 				urb->transfer_buffer_length)
963550a7375SFelipe Balbi 			more = true;
964550a7375SFelipe Balbi 		break;
965550a7375SFelipe Balbi 	case MUSB_EP0_START:
966550a7375SFelipe Balbi 		request = (struct usb_ctrlrequest *) urb->setup_packet;
967550a7375SFelipe Balbi 
968550a7375SFelipe Balbi 		if (!request->wLength) {
969550a7375SFelipe Balbi 			DBG(4, "start no-DATA\n");
970550a7375SFelipe Balbi 			break;
971550a7375SFelipe Balbi 		} else if (request->bRequestType & USB_DIR_IN) {
972550a7375SFelipe Balbi 			DBG(4, "start IN-DATA\n");
973550a7375SFelipe Balbi 			musb->ep0_stage = MUSB_EP0_IN;
974550a7375SFelipe Balbi 			more = true;
975550a7375SFelipe Balbi 			break;
976550a7375SFelipe Balbi 		} else {
977550a7375SFelipe Balbi 			DBG(4, "start OUT-DATA\n");
978550a7375SFelipe Balbi 			musb->ep0_stage = MUSB_EP0_OUT;
979550a7375SFelipe Balbi 			more = true;
980550a7375SFelipe Balbi 		}
981550a7375SFelipe Balbi 		/* FALLTHROUGH */
982550a7375SFelipe Balbi 	case MUSB_EP0_OUT:
983550a7375SFelipe Balbi 		fifo_count = min(qh->maxpacket, ((u16)
984550a7375SFelipe Balbi 				(urb->transfer_buffer_length
985550a7375SFelipe Balbi 				- urb->actual_length)));
986550a7375SFelipe Balbi 
987550a7375SFelipe Balbi 		if (fifo_count) {
988550a7375SFelipe Balbi 			fifo_dest = (u8 *) (urb->transfer_buffer
989550a7375SFelipe Balbi 					+ urb->actual_length);
990550a7375SFelipe Balbi 			DBG(3, "Sending %d bytes to %p\n",
991550a7375SFelipe Balbi 					fifo_count, fifo_dest);
992550a7375SFelipe Balbi 			musb_write_fifo(hw_ep, fifo_count, fifo_dest);
993550a7375SFelipe Balbi 
994550a7375SFelipe Balbi 			urb->actual_length += fifo_count;
995550a7375SFelipe Balbi 			more = true;
996550a7375SFelipe Balbi 		}
997550a7375SFelipe Balbi 		break;
998550a7375SFelipe Balbi 	default:
999550a7375SFelipe Balbi 		ERR("bogus ep0 stage %d\n", musb->ep0_stage);
1000550a7375SFelipe Balbi 		break;
1001550a7375SFelipe Balbi 	}
1002550a7375SFelipe Balbi 
1003550a7375SFelipe Balbi 	return more;
1004550a7375SFelipe Balbi }
1005550a7375SFelipe Balbi 
1006550a7375SFelipe Balbi /*
1007550a7375SFelipe Balbi  * Handle default endpoint interrupt as host. Only called in IRQ time
1008550a7375SFelipe Balbi  * from the LinuxIsr() interrupt service routine.
1009550a7375SFelipe Balbi  *
1010550a7375SFelipe Balbi  * called with controller irqlocked
1011550a7375SFelipe Balbi  */
1012550a7375SFelipe Balbi irqreturn_t musb_h_ep0_irq(struct musb *musb)
1013550a7375SFelipe Balbi {
1014550a7375SFelipe Balbi 	struct urb		*urb;
1015550a7375SFelipe Balbi 	u16			csr, len;
1016550a7375SFelipe Balbi 	int			status = 0;
1017550a7375SFelipe Balbi 	void __iomem		*mbase = musb->mregs;
1018550a7375SFelipe Balbi 	struct musb_hw_ep	*hw_ep = musb->control_ep;
1019550a7375SFelipe Balbi 	void __iomem		*epio = hw_ep->regs;
1020550a7375SFelipe Balbi 	struct musb_qh		*qh = hw_ep->in_qh;
1021550a7375SFelipe Balbi 	bool			complete = false;
1022550a7375SFelipe Balbi 	irqreturn_t		retval = IRQ_NONE;
1023550a7375SFelipe Balbi 
1024550a7375SFelipe Balbi 	/* ep0 only has one queue, "in" */
1025550a7375SFelipe Balbi 	urb = next_urb(qh);
1026550a7375SFelipe Balbi 
1027550a7375SFelipe Balbi 	musb_ep_select(mbase, 0);
1028550a7375SFelipe Balbi 	csr = musb_readw(epio, MUSB_CSR0);
1029550a7375SFelipe Balbi 	len = (csr & MUSB_CSR0_RXPKTRDY)
1030550a7375SFelipe Balbi 			? musb_readb(epio, MUSB_COUNT0)
1031550a7375SFelipe Balbi 			: 0;
1032550a7375SFelipe Balbi 
1033550a7375SFelipe Balbi 	DBG(4, "<== csr0 %04x, qh %p, count %d, urb %p, stage %d\n",
1034550a7375SFelipe Balbi 		csr, qh, len, urb, musb->ep0_stage);
1035550a7375SFelipe Balbi 
1036550a7375SFelipe Balbi 	/* if we just did status stage, we are done */
1037550a7375SFelipe Balbi 	if (MUSB_EP0_STATUS == musb->ep0_stage) {
1038550a7375SFelipe Balbi 		retval = IRQ_HANDLED;
1039550a7375SFelipe Balbi 		complete = true;
1040550a7375SFelipe Balbi 	}
1041550a7375SFelipe Balbi 
1042550a7375SFelipe Balbi 	/* prepare status */
1043550a7375SFelipe Balbi 	if (csr & MUSB_CSR0_H_RXSTALL) {
1044550a7375SFelipe Balbi 		DBG(6, "STALLING ENDPOINT\n");
1045550a7375SFelipe Balbi 		status = -EPIPE;
1046550a7375SFelipe Balbi 
1047550a7375SFelipe Balbi 	} else if (csr & MUSB_CSR0_H_ERROR) {
1048550a7375SFelipe Balbi 		DBG(2, "no response, csr0 %04x\n", csr);
1049550a7375SFelipe Balbi 		status = -EPROTO;
1050550a7375SFelipe Balbi 
1051550a7375SFelipe Balbi 	} else if (csr & MUSB_CSR0_H_NAKTIMEOUT) {
1052550a7375SFelipe Balbi 		DBG(2, "control NAK timeout\n");
1053550a7375SFelipe Balbi 
1054550a7375SFelipe Balbi 		/* NOTE:  this code path would be a good place to PAUSE a
1055550a7375SFelipe Balbi 		 * control transfer, if another one is queued, so that
1056550a7375SFelipe Balbi 		 * ep0 is more likely to stay busy.
1057550a7375SFelipe Balbi 		 *
1058550a7375SFelipe Balbi 		 * if (qh->ring.next != &musb->control), then
1059550a7375SFelipe Balbi 		 * we have a candidate... NAKing is *NOT* an error
1060550a7375SFelipe Balbi 		 */
1061550a7375SFelipe Balbi 		musb_writew(epio, MUSB_CSR0, 0);
1062550a7375SFelipe Balbi 		retval = IRQ_HANDLED;
1063550a7375SFelipe Balbi 	}
1064550a7375SFelipe Balbi 
1065550a7375SFelipe Balbi 	if (status) {
1066550a7375SFelipe Balbi 		DBG(6, "aborting\n");
1067550a7375SFelipe Balbi 		retval = IRQ_HANDLED;
1068550a7375SFelipe Balbi 		if (urb)
1069550a7375SFelipe Balbi 			urb->status = status;
1070550a7375SFelipe Balbi 		complete = true;
1071550a7375SFelipe Balbi 
1072550a7375SFelipe Balbi 		/* use the proper sequence to abort the transfer */
1073550a7375SFelipe Balbi 		if (csr & MUSB_CSR0_H_REQPKT) {
1074550a7375SFelipe Balbi 			csr &= ~MUSB_CSR0_H_REQPKT;
1075550a7375SFelipe Balbi 			musb_writew(epio, MUSB_CSR0, csr);
1076550a7375SFelipe Balbi 			csr &= ~MUSB_CSR0_H_NAKTIMEOUT;
1077550a7375SFelipe Balbi 			musb_writew(epio, MUSB_CSR0, csr);
1078550a7375SFelipe Balbi 		} else {
1079550a7375SFelipe Balbi 			csr |= MUSB_CSR0_FLUSHFIFO;
1080550a7375SFelipe Balbi 			musb_writew(epio, MUSB_CSR0, csr);
1081550a7375SFelipe Balbi 			musb_writew(epio, MUSB_CSR0, csr);
1082550a7375SFelipe Balbi 			csr &= ~MUSB_CSR0_H_NAKTIMEOUT;
1083550a7375SFelipe Balbi 			musb_writew(epio, MUSB_CSR0, csr);
1084550a7375SFelipe Balbi 		}
1085550a7375SFelipe Balbi 
1086550a7375SFelipe Balbi 		musb_writeb(epio, MUSB_NAKLIMIT0, 0);
1087550a7375SFelipe Balbi 
1088550a7375SFelipe Balbi 		/* clear it */
1089550a7375SFelipe Balbi 		musb_writew(epio, MUSB_CSR0, 0);
1090550a7375SFelipe Balbi 	}
1091550a7375SFelipe Balbi 
1092550a7375SFelipe Balbi 	if (unlikely(!urb)) {
1093550a7375SFelipe Balbi 		/* stop endpoint since we have no place for its data, this
1094550a7375SFelipe Balbi 		 * SHOULD NEVER HAPPEN! */
1095550a7375SFelipe Balbi 		ERR("no URB for end 0\n");
1096550a7375SFelipe Balbi 
1097550a7375SFelipe Balbi 		musb_writew(epio, MUSB_CSR0, MUSB_CSR0_FLUSHFIFO);
1098550a7375SFelipe Balbi 		musb_writew(epio, MUSB_CSR0, MUSB_CSR0_FLUSHFIFO);
1099550a7375SFelipe Balbi 		musb_writew(epio, MUSB_CSR0, 0);
1100550a7375SFelipe Balbi 
1101550a7375SFelipe Balbi 		goto done;
1102550a7375SFelipe Balbi 	}
1103550a7375SFelipe Balbi 
1104550a7375SFelipe Balbi 	if (!complete) {
1105550a7375SFelipe Balbi 		/* call common logic and prepare response */
1106550a7375SFelipe Balbi 		if (musb_h_ep0_continue(musb, len, urb)) {
1107550a7375SFelipe Balbi 			/* more packets required */
1108550a7375SFelipe Balbi 			csr = (MUSB_EP0_IN == musb->ep0_stage)
1109550a7375SFelipe Balbi 				?  MUSB_CSR0_H_REQPKT : MUSB_CSR0_TXPKTRDY;
1110550a7375SFelipe Balbi 		} else {
1111550a7375SFelipe Balbi 			/* data transfer complete; perform status phase */
1112550a7375SFelipe Balbi 			if (usb_pipeout(urb->pipe)
1113550a7375SFelipe Balbi 					|| !urb->transfer_buffer_length)
1114550a7375SFelipe Balbi 				csr = MUSB_CSR0_H_STATUSPKT
1115550a7375SFelipe Balbi 					| MUSB_CSR0_H_REQPKT;
1116550a7375SFelipe Balbi 			else
1117550a7375SFelipe Balbi 				csr = MUSB_CSR0_H_STATUSPKT
1118550a7375SFelipe Balbi 					| MUSB_CSR0_TXPKTRDY;
1119550a7375SFelipe Balbi 
1120550a7375SFelipe Balbi 			/* flag status stage */
1121550a7375SFelipe Balbi 			musb->ep0_stage = MUSB_EP0_STATUS;
1122550a7375SFelipe Balbi 
1123550a7375SFelipe Balbi 			DBG(5, "ep0 STATUS, csr %04x\n", csr);
1124550a7375SFelipe Balbi 
1125550a7375SFelipe Balbi 		}
1126550a7375SFelipe Balbi 		musb_writew(epio, MUSB_CSR0, csr);
1127550a7375SFelipe Balbi 		retval = IRQ_HANDLED;
1128550a7375SFelipe Balbi 	} else
1129550a7375SFelipe Balbi 		musb->ep0_stage = MUSB_EP0_IDLE;
1130550a7375SFelipe Balbi 
1131550a7375SFelipe Balbi 	/* call completion handler if done */
1132550a7375SFelipe Balbi 	if (complete)
1133550a7375SFelipe Balbi 		musb_advance_schedule(musb, urb, hw_ep, 1);
1134550a7375SFelipe Balbi done:
1135550a7375SFelipe Balbi 	return retval;
1136550a7375SFelipe Balbi }
1137550a7375SFelipe Balbi 
1138550a7375SFelipe Balbi 
1139550a7375SFelipe Balbi #ifdef CONFIG_USB_INVENTRA_DMA
1140550a7375SFelipe Balbi 
1141550a7375SFelipe Balbi /* Host side TX (OUT) using Mentor DMA works as follows:
1142550a7375SFelipe Balbi 	submit_urb ->
1143550a7375SFelipe Balbi 		- if queue was empty, Program Endpoint
1144550a7375SFelipe Balbi 		- ... which starts DMA to fifo in mode 1 or 0
1145550a7375SFelipe Balbi 
1146550a7375SFelipe Balbi 	DMA Isr (transfer complete) -> TxAvail()
1147550a7375SFelipe Balbi 		- Stop DMA (~DmaEnab)	(<--- Alert ... currently happens
1148550a7375SFelipe Balbi 					only in musb_cleanup_urb)
1149550a7375SFelipe Balbi 		- TxPktRdy has to be set in mode 0 or for
1150550a7375SFelipe Balbi 			short packets in mode 1.
1151550a7375SFelipe Balbi */
1152550a7375SFelipe Balbi 
1153550a7375SFelipe Balbi #endif
1154550a7375SFelipe Balbi 
1155550a7375SFelipe Balbi /* Service a Tx-Available or dma completion irq for the endpoint */
1156550a7375SFelipe Balbi void musb_host_tx(struct musb *musb, u8 epnum)
1157550a7375SFelipe Balbi {
1158550a7375SFelipe Balbi 	int			pipe;
1159550a7375SFelipe Balbi 	bool			done = false;
1160550a7375SFelipe Balbi 	u16			tx_csr;
1161550a7375SFelipe Balbi 	size_t			wLength = 0;
1162550a7375SFelipe Balbi 	u8			*buf = NULL;
1163550a7375SFelipe Balbi 	struct urb		*urb;
1164550a7375SFelipe Balbi 	struct musb_hw_ep	*hw_ep = musb->endpoints + epnum;
1165550a7375SFelipe Balbi 	void __iomem		*epio = hw_ep->regs;
1166550a7375SFelipe Balbi 	struct musb_qh		*qh = hw_ep->out_qh;
1167550a7375SFelipe Balbi 	u32			status = 0;
1168550a7375SFelipe Balbi 	void __iomem		*mbase = musb->mregs;
1169550a7375SFelipe Balbi 	struct dma_channel	*dma;
1170550a7375SFelipe Balbi 
1171550a7375SFelipe Balbi 	urb = next_urb(qh);
1172550a7375SFelipe Balbi 
1173550a7375SFelipe Balbi 	musb_ep_select(mbase, epnum);
1174550a7375SFelipe Balbi 	tx_csr = musb_readw(epio, MUSB_TXCSR);
1175550a7375SFelipe Balbi 
1176550a7375SFelipe Balbi 	/* with CPPI, DMA sometimes triggers "extra" irqs */
1177550a7375SFelipe Balbi 	if (!urb) {
1178550a7375SFelipe Balbi 		DBG(4, "extra TX%d ready, csr %04x\n", epnum, tx_csr);
1179550a7375SFelipe Balbi 		goto finish;
1180550a7375SFelipe Balbi 	}
1181550a7375SFelipe Balbi 
1182550a7375SFelipe Balbi 	pipe = urb->pipe;
1183550a7375SFelipe Balbi 	dma = is_dma_capable() ? hw_ep->tx_channel : NULL;
1184550a7375SFelipe Balbi 	DBG(4, "OUT/TX%d end, csr %04x%s\n", epnum, tx_csr,
1185550a7375SFelipe Balbi 			dma ? ", dma" : "");
1186550a7375SFelipe Balbi 
1187550a7375SFelipe Balbi 	/* check for errors */
1188550a7375SFelipe Balbi 	if (tx_csr & MUSB_TXCSR_H_RXSTALL) {
1189550a7375SFelipe Balbi 		/* dma was disabled, fifo flushed */
1190550a7375SFelipe Balbi 		DBG(3, "TX end %d stall\n", epnum);
1191550a7375SFelipe Balbi 
1192550a7375SFelipe Balbi 		/* stall; record URB status */
1193550a7375SFelipe Balbi 		status = -EPIPE;
1194550a7375SFelipe Balbi 
1195550a7375SFelipe Balbi 	} else if (tx_csr & MUSB_TXCSR_H_ERROR) {
1196550a7375SFelipe Balbi 		/* (NON-ISO) dma was disabled, fifo flushed */
1197550a7375SFelipe Balbi 		DBG(3, "TX 3strikes on ep=%d\n", epnum);
1198550a7375SFelipe Balbi 
1199550a7375SFelipe Balbi 		status = -ETIMEDOUT;
1200550a7375SFelipe Balbi 
1201550a7375SFelipe Balbi 	} else if (tx_csr & MUSB_TXCSR_H_NAKTIMEOUT) {
1202550a7375SFelipe Balbi 		DBG(6, "TX end=%d device not responding\n", epnum);
1203550a7375SFelipe Balbi 
1204550a7375SFelipe Balbi 		/* NOTE:  this code path would be a good place to PAUSE a
1205550a7375SFelipe Balbi 		 * transfer, if there's some other (nonperiodic) tx urb
1206550a7375SFelipe Balbi 		 * that could use this fifo.  (dma complicates it...)
1207550a7375SFelipe Balbi 		 *
1208550a7375SFelipe Balbi 		 * if (bulk && qh->ring.next != &musb->out_bulk), then
1209550a7375SFelipe Balbi 		 * we have a candidate... NAKing is *NOT* an error
1210550a7375SFelipe Balbi 		 */
1211550a7375SFelipe Balbi 		musb_ep_select(mbase, epnum);
1212550a7375SFelipe Balbi 		musb_writew(epio, MUSB_TXCSR,
1213550a7375SFelipe Balbi 				MUSB_TXCSR_H_WZC_BITS
1214550a7375SFelipe Balbi 				| MUSB_TXCSR_TXPKTRDY);
1215550a7375SFelipe Balbi 		goto finish;
1216550a7375SFelipe Balbi 	}
1217550a7375SFelipe Balbi 
1218550a7375SFelipe Balbi 	if (status) {
1219550a7375SFelipe Balbi 		if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) {
1220550a7375SFelipe Balbi 			dma->status = MUSB_DMA_STATUS_CORE_ABORT;
1221550a7375SFelipe Balbi 			(void) musb->dma_controller->channel_abort(dma);
1222550a7375SFelipe Balbi 		}
1223550a7375SFelipe Balbi 
1224550a7375SFelipe Balbi 		/* do the proper sequence to abort the transfer in the
1225550a7375SFelipe Balbi 		 * usb core; the dma engine should already be stopped.
1226550a7375SFelipe Balbi 		 */
1227550a7375SFelipe Balbi 		musb_h_tx_flush_fifo(hw_ep);
1228550a7375SFelipe Balbi 		tx_csr &= ~(MUSB_TXCSR_AUTOSET
1229550a7375SFelipe Balbi 				| MUSB_TXCSR_DMAENAB
1230550a7375SFelipe Balbi 				| MUSB_TXCSR_H_ERROR
1231550a7375SFelipe Balbi 				| MUSB_TXCSR_H_RXSTALL
1232550a7375SFelipe Balbi 				| MUSB_TXCSR_H_NAKTIMEOUT
1233550a7375SFelipe Balbi 				);
1234550a7375SFelipe Balbi 
1235550a7375SFelipe Balbi 		musb_ep_select(mbase, epnum);
1236550a7375SFelipe Balbi 		musb_writew(epio, MUSB_TXCSR, tx_csr);
1237550a7375SFelipe Balbi 		/* REVISIT may need to clear FLUSHFIFO ... */
1238550a7375SFelipe Balbi 		musb_writew(epio, MUSB_TXCSR, tx_csr);
1239550a7375SFelipe Balbi 		musb_writeb(epio, MUSB_TXINTERVAL, 0);
1240550a7375SFelipe Balbi 
1241550a7375SFelipe Balbi 		done = true;
1242550a7375SFelipe Balbi 	}
1243550a7375SFelipe Balbi 
1244550a7375SFelipe Balbi 	/* second cppi case */
1245550a7375SFelipe Balbi 	if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) {
1246550a7375SFelipe Balbi 		DBG(4, "extra TX%d ready, csr %04x\n", epnum, tx_csr);
1247550a7375SFelipe Balbi 		goto finish;
1248550a7375SFelipe Balbi 
1249550a7375SFelipe Balbi 	}
1250550a7375SFelipe Balbi 
1251550a7375SFelipe Balbi 	/* REVISIT this looks wrong... */
1252550a7375SFelipe Balbi 	if (!status || dma || usb_pipeisoc(pipe)) {
1253550a7375SFelipe Balbi 		if (dma)
1254550a7375SFelipe Balbi 			wLength = dma->actual_len;
1255550a7375SFelipe Balbi 		else
1256550a7375SFelipe Balbi 			wLength = qh->segsize;
1257550a7375SFelipe Balbi 		qh->offset += wLength;
1258550a7375SFelipe Balbi 
1259550a7375SFelipe Balbi 		if (usb_pipeisoc(pipe)) {
1260550a7375SFelipe Balbi 			struct usb_iso_packet_descriptor	*d;
1261550a7375SFelipe Balbi 
1262550a7375SFelipe Balbi 			d = urb->iso_frame_desc + qh->iso_idx;
1263550a7375SFelipe Balbi 			d->actual_length = qh->segsize;
1264550a7375SFelipe Balbi 			if (++qh->iso_idx >= urb->number_of_packets) {
1265550a7375SFelipe Balbi 				done = true;
1266550a7375SFelipe Balbi 			} else {
1267550a7375SFelipe Balbi 				d++;
1268550a7375SFelipe Balbi 				buf = urb->transfer_buffer + d->offset;
1269550a7375SFelipe Balbi 				wLength = d->length;
1270550a7375SFelipe Balbi 			}
1271550a7375SFelipe Balbi 		} else if (dma) {
1272550a7375SFelipe Balbi 			done = true;
1273550a7375SFelipe Balbi 		} else {
1274550a7375SFelipe Balbi 			/* see if we need to send more data, or ZLP */
1275550a7375SFelipe Balbi 			if (qh->segsize < qh->maxpacket)
1276550a7375SFelipe Balbi 				done = true;
1277550a7375SFelipe Balbi 			else if (qh->offset == urb->transfer_buffer_length
1278550a7375SFelipe Balbi 					&& !(urb->transfer_flags
1279550a7375SFelipe Balbi 						& URB_ZERO_PACKET))
1280550a7375SFelipe Balbi 				done = true;
1281550a7375SFelipe Balbi 			if (!done) {
1282550a7375SFelipe Balbi 				buf = urb->transfer_buffer
1283550a7375SFelipe Balbi 						+ qh->offset;
1284550a7375SFelipe Balbi 				wLength = urb->transfer_buffer_length
1285550a7375SFelipe Balbi 						- qh->offset;
1286550a7375SFelipe Balbi 			}
1287550a7375SFelipe Balbi 		}
1288550a7375SFelipe Balbi 	}
1289550a7375SFelipe Balbi 
1290550a7375SFelipe Balbi 	/* urb->status != -EINPROGRESS means request has been faulted,
1291550a7375SFelipe Balbi 	 * so we must abort this transfer after cleanup
1292550a7375SFelipe Balbi 	 */
1293550a7375SFelipe Balbi 	if (urb->status != -EINPROGRESS) {
1294550a7375SFelipe Balbi 		done = true;
1295550a7375SFelipe Balbi 		if (status == 0)
1296550a7375SFelipe Balbi 			status = urb->status;
1297550a7375SFelipe Balbi 	}
1298550a7375SFelipe Balbi 
1299550a7375SFelipe Balbi 	if (done) {
1300550a7375SFelipe Balbi 		/* set status */
1301550a7375SFelipe Balbi 		urb->status = status;
1302550a7375SFelipe Balbi 		urb->actual_length = qh->offset;
1303550a7375SFelipe Balbi 		musb_advance_schedule(musb, urb, hw_ep, USB_DIR_OUT);
1304550a7375SFelipe Balbi 
1305550a7375SFelipe Balbi 	} else if (!(tx_csr & MUSB_TXCSR_DMAENAB)) {
1306550a7375SFelipe Balbi 		/* WARN_ON(!buf); */
1307550a7375SFelipe Balbi 
1308550a7375SFelipe Balbi 		/* REVISIT:  some docs say that when hw_ep->tx_double_buffered,
1309550a7375SFelipe Balbi 		 * (and presumably, fifo is not half-full) we should write TWO
1310550a7375SFelipe Balbi 		 * packets before updating TXCSR ... other docs disagree ...
1311550a7375SFelipe Balbi 		 */
1312550a7375SFelipe Balbi 		/* PIO:  start next packet in this URB */
1313550a7375SFelipe Balbi 		wLength = min(qh->maxpacket, (u16) wLength);
1314550a7375SFelipe Balbi 		musb_write_fifo(hw_ep, wLength, buf);
1315550a7375SFelipe Balbi 		qh->segsize = wLength;
1316550a7375SFelipe Balbi 
1317550a7375SFelipe Balbi 		musb_ep_select(mbase, epnum);
1318550a7375SFelipe Balbi 		musb_writew(epio, MUSB_TXCSR,
1319550a7375SFelipe Balbi 				MUSB_TXCSR_H_WZC_BITS | MUSB_TXCSR_TXPKTRDY);
1320550a7375SFelipe Balbi 	} else
1321550a7375SFelipe Balbi 		DBG(1, "not complete, but dma enabled?\n");
1322550a7375SFelipe Balbi 
1323550a7375SFelipe Balbi finish:
1324550a7375SFelipe Balbi 	return;
1325550a7375SFelipe Balbi }
1326550a7375SFelipe Balbi 
1327550a7375SFelipe Balbi 
1328550a7375SFelipe Balbi #ifdef CONFIG_USB_INVENTRA_DMA
1329550a7375SFelipe Balbi 
1330550a7375SFelipe Balbi /* Host side RX (IN) using Mentor DMA works as follows:
1331550a7375SFelipe Balbi 	submit_urb ->
1332550a7375SFelipe Balbi 		- if queue was empty, ProgramEndpoint
1333550a7375SFelipe Balbi 		- first IN token is sent out (by setting ReqPkt)
1334550a7375SFelipe Balbi 	LinuxIsr -> RxReady()
1335550a7375SFelipe Balbi 	/\	=> first packet is received
1336550a7375SFelipe Balbi 	|	- Set in mode 0 (DmaEnab, ~ReqPkt)
1337550a7375SFelipe Balbi 	|		-> DMA Isr (transfer complete) -> RxReady()
1338550a7375SFelipe Balbi 	|		    - Ack receive (~RxPktRdy), turn off DMA (~DmaEnab)
1339550a7375SFelipe Balbi 	|		    - if urb not complete, send next IN token (ReqPkt)
1340550a7375SFelipe Balbi 	|			   |		else complete urb.
1341550a7375SFelipe Balbi 	|			   |
1342550a7375SFelipe Balbi 	---------------------------
1343550a7375SFelipe Balbi  *
1344550a7375SFelipe Balbi  * Nuances of mode 1:
1345550a7375SFelipe Balbi  *	For short packets, no ack (+RxPktRdy) is sent automatically
1346550a7375SFelipe Balbi  *	(even if AutoClear is ON)
1347550a7375SFelipe Balbi  *	For full packets, ack (~RxPktRdy) and next IN token (+ReqPkt) is sent
1348550a7375SFelipe Balbi  *	automatically => major problem, as collecting the next packet becomes
1349550a7375SFelipe Balbi  *	difficult. Hence mode 1 is not used.
1350550a7375SFelipe Balbi  *
1351550a7375SFelipe Balbi  * REVISIT
1352550a7375SFelipe Balbi  *	All we care about at this driver level is that
1353550a7375SFelipe Balbi  *       (a) all URBs terminate with REQPKT cleared and fifo(s) empty;
1354550a7375SFelipe Balbi  *       (b) termination conditions are: short RX, or buffer full;
1355550a7375SFelipe Balbi  *       (c) fault modes include
1356550a7375SFelipe Balbi  *           - iff URB_SHORT_NOT_OK, short RX status is -EREMOTEIO.
1357550a7375SFelipe Balbi  *             (and that endpoint's dma queue stops immediately)
1358550a7375SFelipe Balbi  *           - overflow (full, PLUS more bytes in the terminal packet)
1359550a7375SFelipe Balbi  *
1360550a7375SFelipe Balbi  *	So for example, usb-storage sets URB_SHORT_NOT_OK, and would
1361550a7375SFelipe Balbi  *	thus be a great candidate for using mode 1 ... for all but the
1362550a7375SFelipe Balbi  *	last packet of one URB's transfer.
1363550a7375SFelipe Balbi  */
1364550a7375SFelipe Balbi 
1365550a7375SFelipe Balbi #endif
1366550a7375SFelipe Balbi 
1367550a7375SFelipe Balbi /*
1368550a7375SFelipe Balbi  * Service an RX interrupt for the given IN endpoint; docs cover bulk, iso,
1369550a7375SFelipe Balbi  * and high-bandwidth IN transfer cases.
1370550a7375SFelipe Balbi  */
1371550a7375SFelipe Balbi void musb_host_rx(struct musb *musb, u8 epnum)
1372550a7375SFelipe Balbi {
1373550a7375SFelipe Balbi 	struct urb		*urb;
1374550a7375SFelipe Balbi 	struct musb_hw_ep	*hw_ep = musb->endpoints + epnum;
1375550a7375SFelipe Balbi 	void __iomem		*epio = hw_ep->regs;
1376550a7375SFelipe Balbi 	struct musb_qh		*qh = hw_ep->in_qh;
1377550a7375SFelipe Balbi 	size_t			xfer_len;
1378550a7375SFelipe Balbi 	void __iomem		*mbase = musb->mregs;
1379550a7375SFelipe Balbi 	int			pipe;
1380550a7375SFelipe Balbi 	u16			rx_csr, val;
1381550a7375SFelipe Balbi 	bool			iso_err = false;
1382550a7375SFelipe Balbi 	bool			done = false;
1383550a7375SFelipe Balbi 	u32			status;
1384550a7375SFelipe Balbi 	struct dma_channel	*dma;
1385550a7375SFelipe Balbi 
1386550a7375SFelipe Balbi 	musb_ep_select(mbase, epnum);
1387550a7375SFelipe Balbi 
1388550a7375SFelipe Balbi 	urb = next_urb(qh);
1389550a7375SFelipe Balbi 	dma = is_dma_capable() ? hw_ep->rx_channel : NULL;
1390550a7375SFelipe Balbi 	status = 0;
1391550a7375SFelipe Balbi 	xfer_len = 0;
1392550a7375SFelipe Balbi 
1393550a7375SFelipe Balbi 	rx_csr = musb_readw(epio, MUSB_RXCSR);
1394550a7375SFelipe Balbi 	val = rx_csr;
1395550a7375SFelipe Balbi 
1396550a7375SFelipe Balbi 	if (unlikely(!urb)) {
1397550a7375SFelipe Balbi 		/* REVISIT -- THIS SHOULD NEVER HAPPEN ... but, at least
1398550a7375SFelipe Balbi 		 * usbtest #11 (unlinks) triggers it regularly, sometimes
1399550a7375SFelipe Balbi 		 * with fifo full.  (Only with DMA??)
1400550a7375SFelipe Balbi 		 */
1401550a7375SFelipe Balbi 		DBG(3, "BOGUS RX%d ready, csr %04x, count %d\n", epnum, val,
1402550a7375SFelipe Balbi 			musb_readw(epio, MUSB_RXCOUNT));
1403550a7375SFelipe Balbi 		musb_h_flush_rxfifo(hw_ep, MUSB_RXCSR_CLRDATATOG);
1404550a7375SFelipe Balbi 		return;
1405550a7375SFelipe Balbi 	}
1406550a7375SFelipe Balbi 
1407550a7375SFelipe Balbi 	pipe = urb->pipe;
1408550a7375SFelipe Balbi 
1409550a7375SFelipe Balbi 	DBG(5, "<== hw %d rxcsr %04x, urb actual %d (+dma %zu)\n",
1410550a7375SFelipe Balbi 		epnum, rx_csr, urb->actual_length,
1411550a7375SFelipe Balbi 		dma ? dma->actual_len : 0);
1412550a7375SFelipe Balbi 
1413550a7375SFelipe Balbi 	/* check for errors, concurrent stall & unlink is not really
1414550a7375SFelipe Balbi 	 * handled yet! */
1415550a7375SFelipe Balbi 	if (rx_csr & MUSB_RXCSR_H_RXSTALL) {
1416550a7375SFelipe Balbi 		DBG(3, "RX end %d STALL\n", epnum);
1417550a7375SFelipe Balbi 
1418550a7375SFelipe Balbi 		/* stall; record URB status */
1419550a7375SFelipe Balbi 		status = -EPIPE;
1420550a7375SFelipe Balbi 
1421550a7375SFelipe Balbi 	} else if (rx_csr & MUSB_RXCSR_H_ERROR) {
1422550a7375SFelipe Balbi 		DBG(3, "end %d RX proto error\n", epnum);
1423550a7375SFelipe Balbi 
1424550a7375SFelipe Balbi 		status = -EPROTO;
1425550a7375SFelipe Balbi 		musb_writeb(epio, MUSB_RXINTERVAL, 0);
1426550a7375SFelipe Balbi 
1427550a7375SFelipe Balbi 	} else if (rx_csr & MUSB_RXCSR_DATAERROR) {
1428550a7375SFelipe Balbi 
1429550a7375SFelipe Balbi 		if (USB_ENDPOINT_XFER_ISOC != qh->type) {
1430550a7375SFelipe Balbi 			/* NOTE this code path would be a good place to PAUSE a
1431550a7375SFelipe Balbi 			 * transfer, if there's some other (nonperiodic) rx urb
1432550a7375SFelipe Balbi 			 * that could use this fifo.  (dma complicates it...)
1433550a7375SFelipe Balbi 			 *
1434550a7375SFelipe Balbi 			 * if (bulk && qh->ring.next != &musb->in_bulk), then
1435550a7375SFelipe Balbi 			 * we have a candidate... NAKing is *NOT* an error
1436550a7375SFelipe Balbi 			 */
1437550a7375SFelipe Balbi 			DBG(6, "RX end %d NAK timeout\n", epnum);
1438550a7375SFelipe Balbi 			musb_ep_select(mbase, epnum);
1439550a7375SFelipe Balbi 			musb_writew(epio, MUSB_RXCSR,
1440550a7375SFelipe Balbi 					MUSB_RXCSR_H_WZC_BITS
1441550a7375SFelipe Balbi 					| MUSB_RXCSR_H_REQPKT);
1442550a7375SFelipe Balbi 
1443550a7375SFelipe Balbi 			goto finish;
1444550a7375SFelipe Balbi 		} else {
1445550a7375SFelipe Balbi 			DBG(4, "RX end %d ISO data error\n", epnum);
1446550a7375SFelipe Balbi 			/* packet error reported later */
1447550a7375SFelipe Balbi 			iso_err = true;
1448550a7375SFelipe Balbi 		}
1449550a7375SFelipe Balbi 	}
1450550a7375SFelipe Balbi 
1451550a7375SFelipe Balbi 	/* faults abort the transfer */
1452550a7375SFelipe Balbi 	if (status) {
1453550a7375SFelipe Balbi 		/* clean up dma and collect transfer count */
1454550a7375SFelipe Balbi 		if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) {
1455550a7375SFelipe Balbi 			dma->status = MUSB_DMA_STATUS_CORE_ABORT;
1456550a7375SFelipe Balbi 			(void) musb->dma_controller->channel_abort(dma);
1457550a7375SFelipe Balbi 			xfer_len = dma->actual_len;
1458550a7375SFelipe Balbi 		}
1459550a7375SFelipe Balbi 		musb_h_flush_rxfifo(hw_ep, MUSB_RXCSR_CLRDATATOG);
1460550a7375SFelipe Balbi 		musb_writeb(epio, MUSB_RXINTERVAL, 0);
1461550a7375SFelipe Balbi 		done = true;
1462550a7375SFelipe Balbi 		goto finish;
1463550a7375SFelipe Balbi 	}
1464550a7375SFelipe Balbi 
1465550a7375SFelipe Balbi 	if (unlikely(dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY)) {
1466550a7375SFelipe Balbi 		/* SHOULD NEVER HAPPEN ... but at least DaVinci has done it */
1467550a7375SFelipe Balbi 		ERR("RX%d dma busy, csr %04x\n", epnum, rx_csr);
1468550a7375SFelipe Balbi 		goto finish;
1469550a7375SFelipe Balbi 	}
1470550a7375SFelipe Balbi 
1471550a7375SFelipe Balbi 	/* thorough shutdown for now ... given more precise fault handling
1472550a7375SFelipe Balbi 	 * and better queueing support, we might keep a DMA pipeline going
1473550a7375SFelipe Balbi 	 * while processing this irq for earlier completions.
1474550a7375SFelipe Balbi 	 */
1475550a7375SFelipe Balbi 
1476550a7375SFelipe Balbi 	/* FIXME this is _way_ too much in-line logic for Mentor DMA */
1477550a7375SFelipe Balbi 
1478550a7375SFelipe Balbi #ifndef CONFIG_USB_INVENTRA_DMA
1479550a7375SFelipe Balbi 	if (rx_csr & MUSB_RXCSR_H_REQPKT)  {
1480550a7375SFelipe Balbi 		/* REVISIT this happened for a while on some short reads...
1481550a7375SFelipe Balbi 		 * the cleanup still needs investigation... looks bad...
1482550a7375SFelipe Balbi 		 * and also duplicates dma cleanup code above ... plus,
1483550a7375SFelipe Balbi 		 * shouldn't this be the "half full" double buffer case?
1484550a7375SFelipe Balbi 		 */
1485550a7375SFelipe Balbi 		if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) {
1486550a7375SFelipe Balbi 			dma->status = MUSB_DMA_STATUS_CORE_ABORT;
1487550a7375SFelipe Balbi 			(void) musb->dma_controller->channel_abort(dma);
1488550a7375SFelipe Balbi 			xfer_len = dma->actual_len;
1489550a7375SFelipe Balbi 			done = true;
1490550a7375SFelipe Balbi 		}
1491550a7375SFelipe Balbi 
1492550a7375SFelipe Balbi 		DBG(2, "RXCSR%d %04x, reqpkt, len %zu%s\n", epnum, rx_csr,
1493550a7375SFelipe Balbi 				xfer_len, dma ? ", dma" : "");
1494550a7375SFelipe Balbi 		rx_csr &= ~MUSB_RXCSR_H_REQPKT;
1495550a7375SFelipe Balbi 
1496550a7375SFelipe Balbi 		musb_ep_select(mbase, epnum);
1497550a7375SFelipe Balbi 		musb_writew(epio, MUSB_RXCSR,
1498550a7375SFelipe Balbi 				MUSB_RXCSR_H_WZC_BITS | rx_csr);
1499550a7375SFelipe Balbi 	}
1500550a7375SFelipe Balbi #endif
1501550a7375SFelipe Balbi 	if (dma && (rx_csr & MUSB_RXCSR_DMAENAB)) {
1502550a7375SFelipe Balbi 		xfer_len = dma->actual_len;
1503550a7375SFelipe Balbi 
1504550a7375SFelipe Balbi 		val &= ~(MUSB_RXCSR_DMAENAB
1505550a7375SFelipe Balbi 			| MUSB_RXCSR_H_AUTOREQ
1506550a7375SFelipe Balbi 			| MUSB_RXCSR_AUTOCLEAR
1507550a7375SFelipe Balbi 			| MUSB_RXCSR_RXPKTRDY);
1508550a7375SFelipe Balbi 		musb_writew(hw_ep->regs, MUSB_RXCSR, val);
1509550a7375SFelipe Balbi 
1510550a7375SFelipe Balbi #ifdef CONFIG_USB_INVENTRA_DMA
1511550a7375SFelipe Balbi 		/* done if urb buffer is full or short packet is recd */
1512550a7375SFelipe Balbi 		done = (urb->actual_length + xfer_len >=
1513550a7375SFelipe Balbi 				urb->transfer_buffer_length
1514550a7375SFelipe Balbi 			|| dma->actual_len < qh->maxpacket);
1515550a7375SFelipe Balbi 
1516550a7375SFelipe Balbi 		/* send IN token for next packet, without AUTOREQ */
1517550a7375SFelipe Balbi 		if (!done) {
1518550a7375SFelipe Balbi 			val |= MUSB_RXCSR_H_REQPKT;
1519550a7375SFelipe Balbi 			musb_writew(epio, MUSB_RXCSR,
1520550a7375SFelipe Balbi 				MUSB_RXCSR_H_WZC_BITS | val);
1521550a7375SFelipe Balbi 		}
1522550a7375SFelipe Balbi 
1523550a7375SFelipe Balbi 		DBG(4, "ep %d dma %s, rxcsr %04x, rxcount %d\n", epnum,
1524550a7375SFelipe Balbi 			done ? "off" : "reset",
1525550a7375SFelipe Balbi 			musb_readw(epio, MUSB_RXCSR),
1526550a7375SFelipe Balbi 			musb_readw(epio, MUSB_RXCOUNT));
1527550a7375SFelipe Balbi #else
1528550a7375SFelipe Balbi 		done = true;
1529550a7375SFelipe Balbi #endif
1530550a7375SFelipe Balbi 	} else if (urb->status == -EINPROGRESS) {
1531550a7375SFelipe Balbi 		/* if no errors, be sure a packet is ready for unloading */
1532550a7375SFelipe Balbi 		if (unlikely(!(rx_csr & MUSB_RXCSR_RXPKTRDY))) {
1533550a7375SFelipe Balbi 			status = -EPROTO;
1534550a7375SFelipe Balbi 			ERR("Rx interrupt with no errors or packet!\n");
1535550a7375SFelipe Balbi 
1536550a7375SFelipe Balbi 			/* FIXME this is another "SHOULD NEVER HAPPEN" */
1537550a7375SFelipe Balbi 
1538550a7375SFelipe Balbi /* SCRUB (RX) */
1539550a7375SFelipe Balbi 			/* do the proper sequence to abort the transfer */
1540550a7375SFelipe Balbi 			musb_ep_select(mbase, epnum);
1541550a7375SFelipe Balbi 			val &= ~MUSB_RXCSR_H_REQPKT;
1542550a7375SFelipe Balbi 			musb_writew(epio, MUSB_RXCSR, val);
1543550a7375SFelipe Balbi 			goto finish;
1544550a7375SFelipe Balbi 		}
1545550a7375SFelipe Balbi 
1546550a7375SFelipe Balbi 		/* we are expecting IN packets */
1547550a7375SFelipe Balbi #ifdef CONFIG_USB_INVENTRA_DMA
1548550a7375SFelipe Balbi 		if (dma) {
1549550a7375SFelipe Balbi 			struct dma_controller	*c;
1550550a7375SFelipe Balbi 			u16			rx_count;
1551550a7375SFelipe Balbi 			int			ret;
1552550a7375SFelipe Balbi 
1553550a7375SFelipe Balbi 			rx_count = musb_readw(epio, MUSB_RXCOUNT);
1554550a7375SFelipe Balbi 
1555550a7375SFelipe Balbi 			DBG(2, "RX%d count %d, buffer 0x%x len %d/%d\n",
1556550a7375SFelipe Balbi 					epnum, rx_count,
1557550a7375SFelipe Balbi 					urb->transfer_dma
1558550a7375SFelipe Balbi 						+ urb->actual_length,
1559550a7375SFelipe Balbi 					qh->offset,
1560550a7375SFelipe Balbi 					urb->transfer_buffer_length);
1561550a7375SFelipe Balbi 
1562550a7375SFelipe Balbi 			c = musb->dma_controller;
1563550a7375SFelipe Balbi 
1564550a7375SFelipe Balbi 			dma->desired_mode = 0;
1565550a7375SFelipe Balbi #ifdef USE_MODE1
1566550a7375SFelipe Balbi 			/* because of the issue below, mode 1 will
1567550a7375SFelipe Balbi 			 * only rarely behave with correct semantics.
1568550a7375SFelipe Balbi 			 */
1569550a7375SFelipe Balbi 			if ((urb->transfer_flags &
1570550a7375SFelipe Balbi 						URB_SHORT_NOT_OK)
1571550a7375SFelipe Balbi 				&& (urb->transfer_buffer_length -
1572550a7375SFelipe Balbi 						urb->actual_length)
1573550a7375SFelipe Balbi 					> qh->maxpacket)
1574550a7375SFelipe Balbi 				dma->desired_mode = 1;
1575550a7375SFelipe Balbi #endif
1576550a7375SFelipe Balbi 
1577550a7375SFelipe Balbi /* Disadvantage of using mode 1:
1578550a7375SFelipe Balbi  *	It's basically usable only for mass storage class; essentially all
1579550a7375SFelipe Balbi  *	other protocols also terminate transfers on short packets.
1580550a7375SFelipe Balbi  *
1581550a7375SFelipe Balbi  * Details:
1582550a7375SFelipe Balbi  *	An extra IN token is sent at the end of the transfer (due to AUTOREQ)
1583550a7375SFelipe Balbi  *	If you try to use mode 1 for (transfer_buffer_length - 512), and try
1584550a7375SFelipe Balbi  *	to use the extra IN token to grab the last packet using mode 0, then
1585550a7375SFelipe Balbi  *	the problem is that you cannot be sure when the device will send the
1586550a7375SFelipe Balbi  *	last packet and RxPktRdy set. Sometimes the packet is recd too soon
1587550a7375SFelipe Balbi  *	such that it gets lost when RxCSR is re-set at the end of the mode 1
1588550a7375SFelipe Balbi  *	transfer, while sometimes it is recd just a little late so that if you
1589550a7375SFelipe Balbi  *	try to configure for mode 0 soon after the mode 1 transfer is
1590550a7375SFelipe Balbi  *	completed, you will find rxcount 0. Okay, so you might think why not
1591550a7375SFelipe Balbi  *	wait for an interrupt when the pkt is recd. Well, you won't get any!
1592550a7375SFelipe Balbi  */
1593550a7375SFelipe Balbi 
1594550a7375SFelipe Balbi 			val = musb_readw(epio, MUSB_RXCSR);
1595550a7375SFelipe Balbi 			val &= ~MUSB_RXCSR_H_REQPKT;
1596550a7375SFelipe Balbi 
1597550a7375SFelipe Balbi 			if (dma->desired_mode == 0)
1598550a7375SFelipe Balbi 				val &= ~MUSB_RXCSR_H_AUTOREQ;
1599550a7375SFelipe Balbi 			else
1600550a7375SFelipe Balbi 				val |= MUSB_RXCSR_H_AUTOREQ;
1601550a7375SFelipe Balbi 			val |= MUSB_RXCSR_AUTOCLEAR | MUSB_RXCSR_DMAENAB;
1602550a7375SFelipe Balbi 
1603550a7375SFelipe Balbi 			musb_writew(epio, MUSB_RXCSR,
1604550a7375SFelipe Balbi 				MUSB_RXCSR_H_WZC_BITS | val);
1605550a7375SFelipe Balbi 
1606550a7375SFelipe Balbi 			/* REVISIT if when actual_length != 0,
1607550a7375SFelipe Balbi 			 * transfer_buffer_length needs to be
1608550a7375SFelipe Balbi 			 * adjusted first...
1609550a7375SFelipe Balbi 			 */
1610550a7375SFelipe Balbi 			ret = c->channel_program(
1611550a7375SFelipe Balbi 				dma, qh->maxpacket,
1612550a7375SFelipe Balbi 				dma->desired_mode,
1613550a7375SFelipe Balbi 				urb->transfer_dma
1614550a7375SFelipe Balbi 					+ urb->actual_length,
1615550a7375SFelipe Balbi 				(dma->desired_mode == 0)
1616550a7375SFelipe Balbi 					? rx_count
1617550a7375SFelipe Balbi 					: urb->transfer_buffer_length);
1618550a7375SFelipe Balbi 
1619550a7375SFelipe Balbi 			if (!ret) {
1620550a7375SFelipe Balbi 				c->channel_release(dma);
1621550a7375SFelipe Balbi 				hw_ep->rx_channel = NULL;
1622550a7375SFelipe Balbi 				dma = NULL;
1623550a7375SFelipe Balbi 				/* REVISIT reset CSR */
1624550a7375SFelipe Balbi 			}
1625550a7375SFelipe Balbi 		}
1626550a7375SFelipe Balbi #endif	/* Mentor DMA */
1627550a7375SFelipe Balbi 
1628550a7375SFelipe Balbi 		if (!dma) {
1629550a7375SFelipe Balbi 			done = musb_host_packet_rx(musb, urb,
1630550a7375SFelipe Balbi 					epnum, iso_err);
1631550a7375SFelipe Balbi 			DBG(6, "read %spacket\n", done ? "last " : "");
1632550a7375SFelipe Balbi 		}
1633550a7375SFelipe Balbi 	}
1634550a7375SFelipe Balbi 
1635550a7375SFelipe Balbi 	if (dma && usb_pipeisoc(pipe)) {
1636550a7375SFelipe Balbi 		struct usb_iso_packet_descriptor	*d;
1637550a7375SFelipe Balbi 		int					iso_stat = status;
1638550a7375SFelipe Balbi 
1639550a7375SFelipe Balbi 		d = urb->iso_frame_desc + qh->iso_idx;
1640550a7375SFelipe Balbi 		d->actual_length += xfer_len;
1641550a7375SFelipe Balbi 		if (iso_err) {
1642550a7375SFelipe Balbi 			iso_stat = -EILSEQ;
1643550a7375SFelipe Balbi 			urb->error_count++;
1644550a7375SFelipe Balbi 		}
1645550a7375SFelipe Balbi 		d->status = iso_stat;
1646550a7375SFelipe Balbi 	}
1647550a7375SFelipe Balbi 
1648550a7375SFelipe Balbi finish:
1649550a7375SFelipe Balbi 	urb->actual_length += xfer_len;
1650550a7375SFelipe Balbi 	qh->offset += xfer_len;
1651550a7375SFelipe Balbi 	if (done) {
1652550a7375SFelipe Balbi 		if (urb->status == -EINPROGRESS)
1653550a7375SFelipe Balbi 			urb->status = status;
1654550a7375SFelipe Balbi 		musb_advance_schedule(musb, urb, hw_ep, USB_DIR_IN);
1655550a7375SFelipe Balbi 	}
1656550a7375SFelipe Balbi }
1657550a7375SFelipe Balbi 
1658550a7375SFelipe Balbi /* schedule nodes correspond to peripheral endpoints, like an OHCI QH.
1659550a7375SFelipe Balbi  * the software schedule associates multiple such nodes with a given
1660550a7375SFelipe Balbi  * host side hardware endpoint + direction; scheduling may activate
1661550a7375SFelipe Balbi  * that hardware endpoint.
1662550a7375SFelipe Balbi  */
1663550a7375SFelipe Balbi static int musb_schedule(
1664550a7375SFelipe Balbi 	struct musb		*musb,
1665550a7375SFelipe Balbi 	struct musb_qh		*qh,
1666550a7375SFelipe Balbi 	int			is_in)
1667550a7375SFelipe Balbi {
1668550a7375SFelipe Balbi 	int			idle;
1669550a7375SFelipe Balbi 	int			best_diff;
1670550a7375SFelipe Balbi 	int			best_end, epnum;
1671550a7375SFelipe Balbi 	struct musb_hw_ep	*hw_ep = NULL;
1672550a7375SFelipe Balbi 	struct list_head	*head = NULL;
1673550a7375SFelipe Balbi 
1674550a7375SFelipe Balbi 	/* use fixed hardware for control and bulk */
1675550a7375SFelipe Balbi 	switch (qh->type) {
1676550a7375SFelipe Balbi 	case USB_ENDPOINT_XFER_CONTROL:
1677550a7375SFelipe Balbi 		head = &musb->control;
1678550a7375SFelipe Balbi 		hw_ep = musb->control_ep;
1679550a7375SFelipe Balbi 		break;
1680550a7375SFelipe Balbi 	case USB_ENDPOINT_XFER_BULK:
1681550a7375SFelipe Balbi 		hw_ep = musb->bulk_ep;
1682550a7375SFelipe Balbi 		if (is_in)
1683550a7375SFelipe Balbi 			head = &musb->in_bulk;
1684550a7375SFelipe Balbi 		else
1685550a7375SFelipe Balbi 			head = &musb->out_bulk;
1686550a7375SFelipe Balbi 		break;
1687550a7375SFelipe Balbi 	}
1688550a7375SFelipe Balbi 	if (head) {
1689550a7375SFelipe Balbi 		idle = list_empty(head);
1690550a7375SFelipe Balbi 		list_add_tail(&qh->ring, head);
1691550a7375SFelipe Balbi 		goto success;
1692550a7375SFelipe Balbi 	}
1693550a7375SFelipe Balbi 
1694550a7375SFelipe Balbi 	/* else, periodic transfers get muxed to other endpoints */
1695550a7375SFelipe Balbi 
1696550a7375SFelipe Balbi 	/* FIXME this doesn't consider direction, so it can only
1697550a7375SFelipe Balbi 	 * work for one half of the endpoint hardware, and assumes
1698550a7375SFelipe Balbi 	 * the previous cases handled all non-shared endpoints...
1699550a7375SFelipe Balbi 	 */
1700550a7375SFelipe Balbi 
1701550a7375SFelipe Balbi 	/* we know this qh hasn't been scheduled, so all we need to do
1702550a7375SFelipe Balbi 	 * is choose which hardware endpoint to put it on ...
1703550a7375SFelipe Balbi 	 *
1704550a7375SFelipe Balbi 	 * REVISIT what we really want here is a regular schedule tree
1705550a7375SFelipe Balbi 	 * like e.g. OHCI uses, but for now musb->periodic is just an
1706550a7375SFelipe Balbi 	 * array of the _single_ logical endpoint associated with a
1707550a7375SFelipe Balbi 	 * given physical one (identity mapping logical->physical).
1708550a7375SFelipe Balbi 	 *
1709550a7375SFelipe Balbi 	 * that simplistic approach makes TT scheduling a lot simpler;
1710550a7375SFelipe Balbi 	 * there is none, and thus none of its complexity...
1711550a7375SFelipe Balbi 	 */
1712550a7375SFelipe Balbi 	best_diff = 4096;
1713550a7375SFelipe Balbi 	best_end = -1;
1714550a7375SFelipe Balbi 
1715550a7375SFelipe Balbi 	for (epnum = 1; epnum < musb->nr_endpoints; epnum++) {
1716550a7375SFelipe Balbi 		int	diff;
1717550a7375SFelipe Balbi 
1718550a7375SFelipe Balbi 		if (musb->periodic[epnum])
1719550a7375SFelipe Balbi 			continue;
1720550a7375SFelipe Balbi 		hw_ep = &musb->endpoints[epnum];
1721550a7375SFelipe Balbi 		if (hw_ep == musb->bulk_ep)
1722550a7375SFelipe Balbi 			continue;
1723550a7375SFelipe Balbi 
1724550a7375SFelipe Balbi 		if (is_in)
1725550a7375SFelipe Balbi 			diff = hw_ep->max_packet_sz_rx - qh->maxpacket;
1726550a7375SFelipe Balbi 		else
1727550a7375SFelipe Balbi 			diff = hw_ep->max_packet_sz_tx - qh->maxpacket;
1728550a7375SFelipe Balbi 
1729550a7375SFelipe Balbi 		if (diff > 0 && best_diff > diff) {
1730550a7375SFelipe Balbi 			best_diff = diff;
1731550a7375SFelipe Balbi 			best_end = epnum;
1732550a7375SFelipe Balbi 		}
1733550a7375SFelipe Balbi 	}
1734550a7375SFelipe Balbi 	if (best_end < 0)
1735550a7375SFelipe Balbi 		return -ENOSPC;
1736550a7375SFelipe Balbi 
1737550a7375SFelipe Balbi 	idle = 1;
1738550a7375SFelipe Balbi 	hw_ep = musb->endpoints + best_end;
1739550a7375SFelipe Balbi 	musb->periodic[best_end] = qh;
1740550a7375SFelipe Balbi 	DBG(4, "qh %p periodic slot %d\n", qh, best_end);
1741550a7375SFelipe Balbi success:
1742550a7375SFelipe Balbi 	qh->hw_ep = hw_ep;
1743550a7375SFelipe Balbi 	qh->hep->hcpriv = qh;
1744550a7375SFelipe Balbi 	if (idle)
1745550a7375SFelipe Balbi 		musb_start_urb(musb, is_in, qh);
1746550a7375SFelipe Balbi 	return 0;
1747550a7375SFelipe Balbi }
1748550a7375SFelipe Balbi 
1749550a7375SFelipe Balbi static int musb_urb_enqueue(
1750550a7375SFelipe Balbi 	struct usb_hcd			*hcd,
1751550a7375SFelipe Balbi 	struct urb			*urb,
1752550a7375SFelipe Balbi 	gfp_t				mem_flags)
1753550a7375SFelipe Balbi {
1754550a7375SFelipe Balbi 	unsigned long			flags;
1755550a7375SFelipe Balbi 	struct musb			*musb = hcd_to_musb(hcd);
1756550a7375SFelipe Balbi 	struct usb_host_endpoint	*hep = urb->ep;
1757550a7375SFelipe Balbi 	struct musb_qh			*qh = hep->hcpriv;
1758550a7375SFelipe Balbi 	struct usb_endpoint_descriptor	*epd = &hep->desc;
1759550a7375SFelipe Balbi 	int				ret;
1760550a7375SFelipe Balbi 	unsigned			type_reg;
1761550a7375SFelipe Balbi 	unsigned			interval;
1762550a7375SFelipe Balbi 
1763550a7375SFelipe Balbi 	/* host role must be active */
1764550a7375SFelipe Balbi 	if (!is_host_active(musb) || !musb->is_active)
1765550a7375SFelipe Balbi 		return -ENODEV;
1766550a7375SFelipe Balbi 
1767550a7375SFelipe Balbi 	spin_lock_irqsave(&musb->lock, flags);
1768550a7375SFelipe Balbi 	ret = usb_hcd_link_urb_to_ep(hcd, urb);
1769550a7375SFelipe Balbi 	spin_unlock_irqrestore(&musb->lock, flags);
1770550a7375SFelipe Balbi 	if (ret)
1771550a7375SFelipe Balbi 		return ret;
1772550a7375SFelipe Balbi 
1773550a7375SFelipe Balbi 	/* DMA mapping was already done, if needed, and this urb is on
1774550a7375SFelipe Balbi 	 * hep->urb_list ... so there's little to do unless hep wasn't
1775550a7375SFelipe Balbi 	 * yet scheduled onto a live qh.
1776550a7375SFelipe Balbi 	 *
1777550a7375SFelipe Balbi 	 * REVISIT best to keep hep->hcpriv valid until the endpoint gets
1778550a7375SFelipe Balbi 	 * disabled, testing for empty qh->ring and avoiding qh setup costs
1779550a7375SFelipe Balbi 	 * except for the first urb queued after a config change.
1780550a7375SFelipe Balbi 	 */
1781550a7375SFelipe Balbi 	if (qh) {
1782550a7375SFelipe Balbi 		urb->hcpriv = qh;
1783550a7375SFelipe Balbi 		return 0;
1784550a7375SFelipe Balbi 	}
1785550a7375SFelipe Balbi 
1786550a7375SFelipe Balbi 	/* Allocate and initialize qh, minimizing the work done each time
1787550a7375SFelipe Balbi 	 * hw_ep gets reprogrammed, or with irqs blocked.  Then schedule it.
1788550a7375SFelipe Balbi 	 *
1789550a7375SFelipe Balbi 	 * REVISIT consider a dedicated qh kmem_cache, so it's harder
1790550a7375SFelipe Balbi 	 * for bugs in other kernel code to break this driver...
1791550a7375SFelipe Balbi 	 */
1792550a7375SFelipe Balbi 	qh = kzalloc(sizeof *qh, mem_flags);
1793550a7375SFelipe Balbi 	if (!qh) {
1794550a7375SFelipe Balbi 		usb_hcd_unlink_urb_from_ep(hcd, urb);
1795550a7375SFelipe Balbi 		return -ENOMEM;
1796550a7375SFelipe Balbi 	}
1797550a7375SFelipe Balbi 
1798550a7375SFelipe Balbi 	qh->hep = hep;
1799550a7375SFelipe Balbi 	qh->dev = urb->dev;
1800550a7375SFelipe Balbi 	INIT_LIST_HEAD(&qh->ring);
1801550a7375SFelipe Balbi 	qh->is_ready = 1;
1802550a7375SFelipe Balbi 
1803550a7375SFelipe Balbi 	qh->maxpacket = le16_to_cpu(epd->wMaxPacketSize);
1804550a7375SFelipe Balbi 
1805550a7375SFelipe Balbi 	/* no high bandwidth support yet */
1806550a7375SFelipe Balbi 	if (qh->maxpacket & ~0x7ff) {
1807550a7375SFelipe Balbi 		ret = -EMSGSIZE;
1808550a7375SFelipe Balbi 		goto done;
1809550a7375SFelipe Balbi 	}
1810550a7375SFelipe Balbi 
1811550a7375SFelipe Balbi 	qh->epnum = epd->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
1812550a7375SFelipe Balbi 	qh->type = epd->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
1813550a7375SFelipe Balbi 
1814550a7375SFelipe Balbi 	/* NOTE: urb->dev->devnum is wrong during SET_ADDRESS */
1815550a7375SFelipe Balbi 	qh->addr_reg = (u8) usb_pipedevice(urb->pipe);
1816550a7375SFelipe Balbi 
1817550a7375SFelipe Balbi 	/* precompute rxtype/txtype/type0 register */
1818550a7375SFelipe Balbi 	type_reg = (qh->type << 4) | qh->epnum;
1819550a7375SFelipe Balbi 	switch (urb->dev->speed) {
1820550a7375SFelipe Balbi 	case USB_SPEED_LOW:
1821550a7375SFelipe Balbi 		type_reg |= 0xc0;
1822550a7375SFelipe Balbi 		break;
1823550a7375SFelipe Balbi 	case USB_SPEED_FULL:
1824550a7375SFelipe Balbi 		type_reg |= 0x80;
1825550a7375SFelipe Balbi 		break;
1826550a7375SFelipe Balbi 	default:
1827550a7375SFelipe Balbi 		type_reg |= 0x40;
1828550a7375SFelipe Balbi 	}
1829550a7375SFelipe Balbi 	qh->type_reg = type_reg;
1830550a7375SFelipe Balbi 
1831550a7375SFelipe Balbi 	/* precompute rxinterval/txinterval register */
1832550a7375SFelipe Balbi 	interval = min((u8)16, epd->bInterval);	/* log encoding */
1833550a7375SFelipe Balbi 	switch (qh->type) {
1834550a7375SFelipe Balbi 	case USB_ENDPOINT_XFER_INT:
1835550a7375SFelipe Balbi 		/* fullspeed uses linear encoding */
1836550a7375SFelipe Balbi 		if (USB_SPEED_FULL == urb->dev->speed) {
1837550a7375SFelipe Balbi 			interval = epd->bInterval;
1838550a7375SFelipe Balbi 			if (!interval)
1839550a7375SFelipe Balbi 				interval = 1;
1840550a7375SFelipe Balbi 		}
1841550a7375SFelipe Balbi 		/* FALLTHROUGH */
1842550a7375SFelipe Balbi 	case USB_ENDPOINT_XFER_ISOC:
1843550a7375SFelipe Balbi 		/* iso always uses log encoding */
1844550a7375SFelipe Balbi 		break;
1845550a7375SFelipe Balbi 	default:
1846550a7375SFelipe Balbi 		/* REVISIT we actually want to use NAK limits, hinting to the
1847550a7375SFelipe Balbi 		 * transfer scheduling logic to try some other qh, e.g. try
1848550a7375SFelipe Balbi 		 * for 2 msec first:
1849550a7375SFelipe Balbi 		 *
1850550a7375SFelipe Balbi 		 * interval = (USB_SPEED_HIGH == urb->dev->speed) ? 16 : 2;
1851550a7375SFelipe Balbi 		 *
1852550a7375SFelipe Balbi 		 * The downside of disabling this is that transfer scheduling
1853550a7375SFelipe Balbi 		 * gets VERY unfair for nonperiodic transfers; a misbehaving
1854550a7375SFelipe Balbi 		 * peripheral could make that hurt.  Or for reads, one that's
1855550a7375SFelipe Balbi 		 * perfectly normal:  network and other drivers keep reads
1856550a7375SFelipe Balbi 		 * posted at all times, having one pending for a week should
1857550a7375SFelipe Balbi 		 * be perfectly safe.
1858550a7375SFelipe Balbi 		 *
1859550a7375SFelipe Balbi 		 * The upside of disabling it is avoidng transfer scheduling
1860550a7375SFelipe Balbi 		 * code to put this aside for while.
1861550a7375SFelipe Balbi 		 */
1862550a7375SFelipe Balbi 		interval = 0;
1863550a7375SFelipe Balbi 	}
1864550a7375SFelipe Balbi 	qh->intv_reg = interval;
1865550a7375SFelipe Balbi 
1866550a7375SFelipe Balbi 	/* precompute addressing for external hub/tt ports */
1867550a7375SFelipe Balbi 	if (musb->is_multipoint) {
1868550a7375SFelipe Balbi 		struct usb_device	*parent = urb->dev->parent;
1869550a7375SFelipe Balbi 
1870550a7375SFelipe Balbi 		if (parent != hcd->self.root_hub) {
1871550a7375SFelipe Balbi 			qh->h_addr_reg = (u8) parent->devnum;
1872550a7375SFelipe Balbi 
1873550a7375SFelipe Balbi 			/* set up tt info if needed */
1874550a7375SFelipe Balbi 			if (urb->dev->tt) {
1875550a7375SFelipe Balbi 				qh->h_port_reg = (u8) urb->dev->ttport;
1876*ae5ad296SAjay Kumar Gupta 				if (urb->dev->tt->hub)
1877*ae5ad296SAjay Kumar Gupta 					qh->h_addr_reg =
1878*ae5ad296SAjay Kumar Gupta 						(u8) urb->dev->tt->hub->devnum;
1879*ae5ad296SAjay Kumar Gupta 				if (urb->dev->tt->multi)
1880550a7375SFelipe Balbi 					qh->h_addr_reg |= 0x80;
1881550a7375SFelipe Balbi 			}
1882550a7375SFelipe Balbi 		}
1883550a7375SFelipe Balbi 	}
1884550a7375SFelipe Balbi 
1885550a7375SFelipe Balbi 	/* invariant: hep->hcpriv is null OR the qh that's already scheduled.
1886550a7375SFelipe Balbi 	 * until we get real dma queues (with an entry for each urb/buffer),
1887550a7375SFelipe Balbi 	 * we only have work to do in the former case.
1888550a7375SFelipe Balbi 	 */
1889550a7375SFelipe Balbi 	spin_lock_irqsave(&musb->lock, flags);
1890550a7375SFelipe Balbi 	if (hep->hcpriv) {
1891550a7375SFelipe Balbi 		/* some concurrent activity submitted another urb to hep...
1892550a7375SFelipe Balbi 		 * odd, rare, error prone, but legal.
1893550a7375SFelipe Balbi 		 */
1894550a7375SFelipe Balbi 		kfree(qh);
1895550a7375SFelipe Balbi 		ret = 0;
1896550a7375SFelipe Balbi 	} else
1897550a7375SFelipe Balbi 		ret = musb_schedule(musb, qh,
1898550a7375SFelipe Balbi 				epd->bEndpointAddress & USB_ENDPOINT_DIR_MASK);
1899550a7375SFelipe Balbi 
1900550a7375SFelipe Balbi 	if (ret == 0) {
1901550a7375SFelipe Balbi 		urb->hcpriv = qh;
1902550a7375SFelipe Balbi 		/* FIXME set urb->start_frame for iso/intr, it's tested in
1903550a7375SFelipe Balbi 		 * musb_start_urb(), but otherwise only konicawc cares ...
1904550a7375SFelipe Balbi 		 */
1905550a7375SFelipe Balbi 	}
1906550a7375SFelipe Balbi 	spin_unlock_irqrestore(&musb->lock, flags);
1907550a7375SFelipe Balbi 
1908550a7375SFelipe Balbi done:
1909550a7375SFelipe Balbi 	if (ret != 0) {
1910550a7375SFelipe Balbi 		usb_hcd_unlink_urb_from_ep(hcd, urb);
1911550a7375SFelipe Balbi 		kfree(qh);
1912550a7375SFelipe Balbi 	}
1913550a7375SFelipe Balbi 	return ret;
1914550a7375SFelipe Balbi }
1915550a7375SFelipe Balbi 
1916550a7375SFelipe Balbi 
1917550a7375SFelipe Balbi /*
1918550a7375SFelipe Balbi  * abort a transfer that's at the head of a hardware queue.
1919550a7375SFelipe Balbi  * called with controller locked, irqs blocked
1920550a7375SFelipe Balbi  * that hardware queue advances to the next transfer, unless prevented
1921550a7375SFelipe Balbi  */
1922550a7375SFelipe Balbi static int musb_cleanup_urb(struct urb *urb, struct musb_qh *qh, int is_in)
1923550a7375SFelipe Balbi {
1924550a7375SFelipe Balbi 	struct musb_hw_ep	*ep = qh->hw_ep;
1925550a7375SFelipe Balbi 	void __iomem		*epio = ep->regs;
1926550a7375SFelipe Balbi 	unsigned		hw_end = ep->epnum;
1927550a7375SFelipe Balbi 	void __iomem		*regs = ep->musb->mregs;
1928550a7375SFelipe Balbi 	u16			csr;
1929550a7375SFelipe Balbi 	int			status = 0;
1930550a7375SFelipe Balbi 
1931550a7375SFelipe Balbi 	musb_ep_select(regs, hw_end);
1932550a7375SFelipe Balbi 
1933550a7375SFelipe Balbi 	if (is_dma_capable()) {
1934550a7375SFelipe Balbi 		struct dma_channel	*dma;
1935550a7375SFelipe Balbi 
1936550a7375SFelipe Balbi 		dma = is_in ? ep->rx_channel : ep->tx_channel;
1937550a7375SFelipe Balbi 		if (dma) {
1938550a7375SFelipe Balbi 			status = ep->musb->dma_controller->channel_abort(dma);
1939550a7375SFelipe Balbi 			DBG(status ? 1 : 3,
1940550a7375SFelipe Balbi 				"abort %cX%d DMA for urb %p --> %d\n",
1941550a7375SFelipe Balbi 				is_in ? 'R' : 'T', ep->epnum,
1942550a7375SFelipe Balbi 				urb, status);
1943550a7375SFelipe Balbi 			urb->actual_length += dma->actual_len;
1944550a7375SFelipe Balbi 		}
1945550a7375SFelipe Balbi 	}
1946550a7375SFelipe Balbi 
1947550a7375SFelipe Balbi 	/* turn off DMA requests, discard state, stop polling ... */
1948550a7375SFelipe Balbi 	if (is_in) {
1949550a7375SFelipe Balbi 		/* giveback saves bulk toggle */
1950550a7375SFelipe Balbi 		csr = musb_h_flush_rxfifo(ep, 0);
1951550a7375SFelipe Balbi 
1952550a7375SFelipe Balbi 		/* REVISIT we still get an irq; should likely clear the
1953550a7375SFelipe Balbi 		 * endpoint's irq status here to avoid bogus irqs.
1954550a7375SFelipe Balbi 		 * clearing that status is platform-specific...
1955550a7375SFelipe Balbi 		 */
1956550a7375SFelipe Balbi 	} else {
1957550a7375SFelipe Balbi 		musb_h_tx_flush_fifo(ep);
1958550a7375SFelipe Balbi 		csr = musb_readw(epio, MUSB_TXCSR);
1959550a7375SFelipe Balbi 		csr &= ~(MUSB_TXCSR_AUTOSET
1960550a7375SFelipe Balbi 			| MUSB_TXCSR_DMAENAB
1961550a7375SFelipe Balbi 			| MUSB_TXCSR_H_RXSTALL
1962550a7375SFelipe Balbi 			| MUSB_TXCSR_H_NAKTIMEOUT
1963550a7375SFelipe Balbi 			| MUSB_TXCSR_H_ERROR
1964550a7375SFelipe Balbi 			| MUSB_TXCSR_TXPKTRDY);
1965550a7375SFelipe Balbi 		musb_writew(epio, MUSB_TXCSR, csr);
1966550a7375SFelipe Balbi 		/* REVISIT may need to clear FLUSHFIFO ... */
1967550a7375SFelipe Balbi 		musb_writew(epio, MUSB_TXCSR, csr);
1968550a7375SFelipe Balbi 		/* flush cpu writebuffer */
1969550a7375SFelipe Balbi 		csr = musb_readw(epio, MUSB_TXCSR);
1970550a7375SFelipe Balbi 	}
1971550a7375SFelipe Balbi 	if (status == 0)
1972550a7375SFelipe Balbi 		musb_advance_schedule(ep->musb, urb, ep, is_in);
1973550a7375SFelipe Balbi 	return status;
1974550a7375SFelipe Balbi }
1975550a7375SFelipe Balbi 
1976550a7375SFelipe Balbi static int musb_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
1977550a7375SFelipe Balbi {
1978550a7375SFelipe Balbi 	struct musb		*musb = hcd_to_musb(hcd);
1979550a7375SFelipe Balbi 	struct musb_qh		*qh;
1980550a7375SFelipe Balbi 	struct list_head	*sched;
1981550a7375SFelipe Balbi 	unsigned long		flags;
1982550a7375SFelipe Balbi 	int			ret;
1983550a7375SFelipe Balbi 
1984550a7375SFelipe Balbi 	DBG(4, "urb=%p, dev%d ep%d%s\n", urb,
1985550a7375SFelipe Balbi 			usb_pipedevice(urb->pipe),
1986550a7375SFelipe Balbi 			usb_pipeendpoint(urb->pipe),
1987550a7375SFelipe Balbi 			usb_pipein(urb->pipe) ? "in" : "out");
1988550a7375SFelipe Balbi 
1989550a7375SFelipe Balbi 	spin_lock_irqsave(&musb->lock, flags);
1990550a7375SFelipe Balbi 	ret = usb_hcd_check_unlink_urb(hcd, urb, status);
1991550a7375SFelipe Balbi 	if (ret)
1992550a7375SFelipe Balbi 		goto done;
1993550a7375SFelipe Balbi 
1994550a7375SFelipe Balbi 	qh = urb->hcpriv;
1995550a7375SFelipe Balbi 	if (!qh)
1996550a7375SFelipe Balbi 		goto done;
1997550a7375SFelipe Balbi 
1998550a7375SFelipe Balbi 	/* Any URB not actively programmed into endpoint hardware can be
1999550a7375SFelipe Balbi 	 * immediately given back.  Such an URB must be at the head of its
2000550a7375SFelipe Balbi 	 * endpoint queue, unless someday we get real DMA queues.  And even
2001550a7375SFelipe Balbi 	 * then, it might not be known to the hardware...
2002550a7375SFelipe Balbi 	 *
2003550a7375SFelipe Balbi 	 * Otherwise abort current transfer, pending dma, etc.; urb->status
2004550a7375SFelipe Balbi 	 * has already been updated.  This is a synchronous abort; it'd be
2005550a7375SFelipe Balbi 	 * OK to hold off until after some IRQ, though.
2006550a7375SFelipe Balbi 	 */
2007550a7375SFelipe Balbi 	if (!qh->is_ready || urb->urb_list.prev != &qh->hep->urb_list)
2008550a7375SFelipe Balbi 		ret = -EINPROGRESS;
2009550a7375SFelipe Balbi 	else {
2010550a7375SFelipe Balbi 		switch (qh->type) {
2011550a7375SFelipe Balbi 		case USB_ENDPOINT_XFER_CONTROL:
2012550a7375SFelipe Balbi 			sched = &musb->control;
2013550a7375SFelipe Balbi 			break;
2014550a7375SFelipe Balbi 		case USB_ENDPOINT_XFER_BULK:
2015550a7375SFelipe Balbi 			if (usb_pipein(urb->pipe))
2016550a7375SFelipe Balbi 				sched = &musb->in_bulk;
2017550a7375SFelipe Balbi 			else
2018550a7375SFelipe Balbi 				sched = &musb->out_bulk;
2019550a7375SFelipe Balbi 			break;
2020550a7375SFelipe Balbi 		default:
2021550a7375SFelipe Balbi 			/* REVISIT when we get a schedule tree, periodic
2022550a7375SFelipe Balbi 			 * transfers won't always be at the head of a
2023550a7375SFelipe Balbi 			 * singleton queue...
2024550a7375SFelipe Balbi 			 */
2025550a7375SFelipe Balbi 			sched = NULL;
2026550a7375SFelipe Balbi 			break;
2027550a7375SFelipe Balbi 		}
2028550a7375SFelipe Balbi 	}
2029550a7375SFelipe Balbi 
2030550a7375SFelipe Balbi 	/* NOTE:  qh is invalid unless !list_empty(&hep->urb_list) */
2031550a7375SFelipe Balbi 	if (ret < 0 || (sched && qh != first_qh(sched))) {
2032550a7375SFelipe Balbi 		int	ready = qh->is_ready;
2033550a7375SFelipe Balbi 
2034550a7375SFelipe Balbi 		ret = 0;
2035550a7375SFelipe Balbi 		qh->is_ready = 0;
2036550a7375SFelipe Balbi 		__musb_giveback(musb, urb, 0);
2037550a7375SFelipe Balbi 		qh->is_ready = ready;
2038550a7375SFelipe Balbi 	} else
2039550a7375SFelipe Balbi 		ret = musb_cleanup_urb(urb, qh, urb->pipe & USB_DIR_IN);
2040550a7375SFelipe Balbi done:
2041550a7375SFelipe Balbi 	spin_unlock_irqrestore(&musb->lock, flags);
2042550a7375SFelipe Balbi 	return ret;
2043550a7375SFelipe Balbi }
2044550a7375SFelipe Balbi 
2045550a7375SFelipe Balbi /* disable an endpoint */
2046550a7375SFelipe Balbi static void
2047550a7375SFelipe Balbi musb_h_disable(struct usb_hcd *hcd, struct usb_host_endpoint *hep)
2048550a7375SFelipe Balbi {
2049550a7375SFelipe Balbi 	u8			epnum = hep->desc.bEndpointAddress;
2050550a7375SFelipe Balbi 	unsigned long		flags;
2051550a7375SFelipe Balbi 	struct musb		*musb = hcd_to_musb(hcd);
2052550a7375SFelipe Balbi 	u8			is_in = epnum & USB_DIR_IN;
2053550a7375SFelipe Balbi 	struct musb_qh		*qh = hep->hcpriv;
2054550a7375SFelipe Balbi 	struct urb		*urb, *tmp;
2055550a7375SFelipe Balbi 	struct list_head	*sched;
2056550a7375SFelipe Balbi 
2057550a7375SFelipe Balbi 	if (!qh)
2058550a7375SFelipe Balbi 		return;
2059550a7375SFelipe Balbi 
2060550a7375SFelipe Balbi 	spin_lock_irqsave(&musb->lock, flags);
2061550a7375SFelipe Balbi 
2062550a7375SFelipe Balbi 	switch (qh->type) {
2063550a7375SFelipe Balbi 	case USB_ENDPOINT_XFER_CONTROL:
2064550a7375SFelipe Balbi 		sched = &musb->control;
2065550a7375SFelipe Balbi 		break;
2066550a7375SFelipe Balbi 	case USB_ENDPOINT_XFER_BULK:
2067550a7375SFelipe Balbi 		if (is_in)
2068550a7375SFelipe Balbi 			sched = &musb->in_bulk;
2069550a7375SFelipe Balbi 		else
2070550a7375SFelipe Balbi 			sched = &musb->out_bulk;
2071550a7375SFelipe Balbi 		break;
2072550a7375SFelipe Balbi 	default:
2073550a7375SFelipe Balbi 		/* REVISIT when we get a schedule tree, periodic transfers
2074550a7375SFelipe Balbi 		 * won't always be at the head of a singleton queue...
2075550a7375SFelipe Balbi 		 */
2076550a7375SFelipe Balbi 		sched = NULL;
2077550a7375SFelipe Balbi 		break;
2078550a7375SFelipe Balbi 	}
2079550a7375SFelipe Balbi 
2080550a7375SFelipe Balbi 	/* NOTE:  qh is invalid unless !list_empty(&hep->urb_list) */
2081550a7375SFelipe Balbi 
2082550a7375SFelipe Balbi 	/* kick first urb off the hardware, if needed */
2083550a7375SFelipe Balbi 	qh->is_ready = 0;
2084550a7375SFelipe Balbi 	if (!sched || qh == first_qh(sched)) {
2085550a7375SFelipe Balbi 		urb = next_urb(qh);
2086550a7375SFelipe Balbi 
2087550a7375SFelipe Balbi 		/* make software (then hardware) stop ASAP */
2088550a7375SFelipe Balbi 		if (!urb->unlinked)
2089550a7375SFelipe Balbi 			urb->status = -ESHUTDOWN;
2090550a7375SFelipe Balbi 
2091550a7375SFelipe Balbi 		/* cleanup */
2092550a7375SFelipe Balbi 		musb_cleanup_urb(urb, qh, urb->pipe & USB_DIR_IN);
2093550a7375SFelipe Balbi 	} else
2094550a7375SFelipe Balbi 		urb = NULL;
2095550a7375SFelipe Balbi 
2096550a7375SFelipe Balbi 	/* then just nuke all the others */
2097550a7375SFelipe Balbi 	list_for_each_entry_safe_from(urb, tmp, &hep->urb_list, urb_list)
2098550a7375SFelipe Balbi 		musb_giveback(qh, urb, -ESHUTDOWN);
2099550a7375SFelipe Balbi 
2100550a7375SFelipe Balbi 	spin_unlock_irqrestore(&musb->lock, flags);
2101550a7375SFelipe Balbi }
2102550a7375SFelipe Balbi 
2103550a7375SFelipe Balbi static int musb_h_get_frame_number(struct usb_hcd *hcd)
2104550a7375SFelipe Balbi {
2105550a7375SFelipe Balbi 	struct musb	*musb = hcd_to_musb(hcd);
2106550a7375SFelipe Balbi 
2107550a7375SFelipe Balbi 	return musb_readw(musb->mregs, MUSB_FRAME);
2108550a7375SFelipe Balbi }
2109550a7375SFelipe Balbi 
2110550a7375SFelipe Balbi static int musb_h_start(struct usb_hcd *hcd)
2111550a7375SFelipe Balbi {
2112550a7375SFelipe Balbi 	struct musb	*musb = hcd_to_musb(hcd);
2113550a7375SFelipe Balbi 
2114550a7375SFelipe Balbi 	/* NOTE: musb_start() is called when the hub driver turns
2115550a7375SFelipe Balbi 	 * on port power, or when (OTG) peripheral starts.
2116550a7375SFelipe Balbi 	 */
2117550a7375SFelipe Balbi 	hcd->state = HC_STATE_RUNNING;
2118550a7375SFelipe Balbi 	musb->port1_status = 0;
2119550a7375SFelipe Balbi 	return 0;
2120550a7375SFelipe Balbi }
2121550a7375SFelipe Balbi 
2122550a7375SFelipe Balbi static void musb_h_stop(struct usb_hcd *hcd)
2123550a7375SFelipe Balbi {
2124550a7375SFelipe Balbi 	musb_stop(hcd_to_musb(hcd));
2125550a7375SFelipe Balbi 	hcd->state = HC_STATE_HALT;
2126550a7375SFelipe Balbi }
2127550a7375SFelipe Balbi 
2128550a7375SFelipe Balbi static int musb_bus_suspend(struct usb_hcd *hcd)
2129550a7375SFelipe Balbi {
2130550a7375SFelipe Balbi 	struct musb	*musb = hcd_to_musb(hcd);
2131550a7375SFelipe Balbi 
2132550a7375SFelipe Balbi 	if (musb->xceiv.state == OTG_STATE_A_SUSPEND)
2133550a7375SFelipe Balbi 		return 0;
2134550a7375SFelipe Balbi 
2135550a7375SFelipe Balbi 	if (is_host_active(musb) && musb->is_active) {
2136550a7375SFelipe Balbi 		WARNING("trying to suspend as %s is_active=%i\n",
2137550a7375SFelipe Balbi 			otg_state_string(musb), musb->is_active);
2138550a7375SFelipe Balbi 		return -EBUSY;
2139550a7375SFelipe Balbi 	} else
2140550a7375SFelipe Balbi 		return 0;
2141550a7375SFelipe Balbi }
2142550a7375SFelipe Balbi 
2143550a7375SFelipe Balbi static int musb_bus_resume(struct usb_hcd *hcd)
2144550a7375SFelipe Balbi {
2145550a7375SFelipe Balbi 	/* resuming child port does the work */
2146550a7375SFelipe Balbi 	return 0;
2147550a7375SFelipe Balbi }
2148550a7375SFelipe Balbi 
2149550a7375SFelipe Balbi const struct hc_driver musb_hc_driver = {
2150550a7375SFelipe Balbi 	.description		= "musb-hcd",
2151550a7375SFelipe Balbi 	.product_desc		= "MUSB HDRC host driver",
2152550a7375SFelipe Balbi 	.hcd_priv_size		= sizeof(struct musb),
2153550a7375SFelipe Balbi 	.flags			= HCD_USB2 | HCD_MEMORY,
2154550a7375SFelipe Balbi 
2155550a7375SFelipe Balbi 	/* not using irq handler or reset hooks from usbcore, since
2156550a7375SFelipe Balbi 	 * those must be shared with peripheral code for OTG configs
2157550a7375SFelipe Balbi 	 */
2158550a7375SFelipe Balbi 
2159550a7375SFelipe Balbi 	.start			= musb_h_start,
2160550a7375SFelipe Balbi 	.stop			= musb_h_stop,
2161550a7375SFelipe Balbi 
2162550a7375SFelipe Balbi 	.get_frame_number	= musb_h_get_frame_number,
2163550a7375SFelipe Balbi 
2164550a7375SFelipe Balbi 	.urb_enqueue		= musb_urb_enqueue,
2165550a7375SFelipe Balbi 	.urb_dequeue		= musb_urb_dequeue,
2166550a7375SFelipe Balbi 	.endpoint_disable	= musb_h_disable,
2167550a7375SFelipe Balbi 
2168550a7375SFelipe Balbi 	.hub_status_data	= musb_hub_status_data,
2169550a7375SFelipe Balbi 	.hub_control		= musb_hub_control,
2170550a7375SFelipe Balbi 	.bus_suspend		= musb_bus_suspend,
2171550a7375SFelipe Balbi 	.bus_resume		= musb_bus_resume,
2172550a7375SFelipe Balbi 	/* .start_port_reset	= NULL, */
2173550a7375SFelipe Balbi 	/* .hub_irq_enable	= NULL, */
2174550a7375SFelipe Balbi };
2175