xref: /openbmc/linux/drivers/usb/musb/musb_gadget.c (revision 41ac7b3ab7fe1d6175839947a877fdf95cbd2211)
1550a7375SFelipe Balbi /*
2550a7375SFelipe Balbi  * MUSB OTG driver peripheral 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
7cea83241SSergei Shtylyov  * Copyright (C) 2009 MontaVista Software, Inc. <source@mvista.com>
8550a7375SFelipe Balbi  *
9550a7375SFelipe Balbi  * This program is free software; you can redistribute it and/or
10550a7375SFelipe Balbi  * modify it under the terms of the GNU General Public License
11550a7375SFelipe Balbi  * version 2 as published by the Free Software Foundation.
12550a7375SFelipe Balbi  *
13550a7375SFelipe Balbi  * This program is distributed in the hope that it will be useful, but
14550a7375SFelipe Balbi  * WITHOUT ANY WARRANTY; without even the implied warranty of
15550a7375SFelipe Balbi  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16550a7375SFelipe Balbi  * General Public License for more details.
17550a7375SFelipe Balbi  *
18550a7375SFelipe Balbi  * You should have received a copy of the GNU General Public License
19550a7375SFelipe Balbi  * along with this program; if not, write to the Free Software
20550a7375SFelipe Balbi  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
21550a7375SFelipe Balbi  * 02110-1301 USA
22550a7375SFelipe Balbi  *
23550a7375SFelipe Balbi  * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
24550a7375SFelipe Balbi  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
25550a7375SFelipe Balbi  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN
26550a7375SFelipe Balbi  * NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27550a7375SFelipe Balbi  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28550a7375SFelipe Balbi  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
29550a7375SFelipe Balbi  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
30550a7375SFelipe Balbi  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31550a7375SFelipe Balbi  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32550a7375SFelipe Balbi  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33550a7375SFelipe Balbi  *
34550a7375SFelipe Balbi  */
35550a7375SFelipe Balbi 
36550a7375SFelipe Balbi #include <linux/kernel.h>
37550a7375SFelipe Balbi #include <linux/list.h>
38550a7375SFelipe Balbi #include <linux/timer.h>
39550a7375SFelipe Balbi #include <linux/module.h>
40550a7375SFelipe Balbi #include <linux/smp.h>
41550a7375SFelipe Balbi #include <linux/spinlock.h>
42550a7375SFelipe Balbi #include <linux/delay.h>
43550a7375SFelipe Balbi #include <linux/dma-mapping.h>
445a0e3ad6STejun Heo #include <linux/slab.h>
45550a7375SFelipe Balbi 
46550a7375SFelipe Balbi #include "musb_core.h"
47550a7375SFelipe Balbi 
48550a7375SFelipe Balbi 
49550a7375SFelipe Balbi /* MUSB PERIPHERAL status 3-mar-2006:
50550a7375SFelipe Balbi  *
51550a7375SFelipe Balbi  * - EP0 seems solid.  It passes both USBCV and usbtest control cases.
52550a7375SFelipe Balbi  *   Minor glitches:
53550a7375SFelipe Balbi  *
54550a7375SFelipe Balbi  *     + remote wakeup to Linux hosts work, but saw USBCV failures;
55550a7375SFelipe Balbi  *       in one test run (operator error?)
56550a7375SFelipe Balbi  *     + endpoint halt tests -- in both usbtest and usbcv -- seem
57550a7375SFelipe Balbi  *       to break when dma is enabled ... is something wrongly
58550a7375SFelipe Balbi  *       clearing SENDSTALL?
59550a7375SFelipe Balbi  *
60550a7375SFelipe Balbi  * - Mass storage behaved ok when last tested.  Network traffic patterns
61550a7375SFelipe Balbi  *   (with lots of short transfers etc) need retesting; they turn up the
62550a7375SFelipe Balbi  *   worst cases of the DMA, since short packets are typical but are not
63550a7375SFelipe Balbi  *   required.
64550a7375SFelipe Balbi  *
65550a7375SFelipe Balbi  * - TX/IN
66550a7375SFelipe Balbi  *     + both pio and dma behave in with network and g_zero tests
67550a7375SFelipe Balbi  *     + no cppi throughput issues other than no-hw-queueing
68550a7375SFelipe Balbi  *     + failed with FLAT_REG (DaVinci)
69550a7375SFelipe Balbi  *     + seems to behave with double buffering, PIO -and- CPPI
70550a7375SFelipe Balbi  *     + with gadgetfs + AIO, requests got lost?
71550a7375SFelipe Balbi  *
72550a7375SFelipe Balbi  * - RX/OUT
73550a7375SFelipe Balbi  *     + both pio and dma behave in with network and g_zero tests
74550a7375SFelipe Balbi  *     + dma is slow in typical case (short_not_ok is clear)
75550a7375SFelipe Balbi  *     + double buffering ok with PIO
76550a7375SFelipe Balbi  *     + double buffering *FAILS* with CPPI, wrong data bytes sometimes
77550a7375SFelipe Balbi  *     + request lossage observed with gadgetfs
78550a7375SFelipe Balbi  *
79550a7375SFelipe Balbi  * - ISO not tested ... might work, but only weakly isochronous
80550a7375SFelipe Balbi  *
81550a7375SFelipe Balbi  * - Gadget driver disabling of softconnect during bind() is ignored; so
82550a7375SFelipe Balbi  *   drivers can't hold off host requests until userspace is ready.
83550a7375SFelipe Balbi  *   (Workaround:  they can turn it off later.)
84550a7375SFelipe Balbi  *
85550a7375SFelipe Balbi  * - PORTABILITY (assumes PIO works):
86550a7375SFelipe Balbi  *     + DaVinci, basically works with cppi dma
87550a7375SFelipe Balbi  *     + OMAP 2430, ditto with mentor dma
88550a7375SFelipe Balbi  *     + TUSB 6010, platform-specific dma in the works
89550a7375SFelipe Balbi  */
90550a7375SFelipe Balbi 
91550a7375SFelipe Balbi /* ----------------------------------------------------------------------- */
92550a7375SFelipe Balbi 
93c65bfa62SMian Yousaf Kaukab #define is_buffer_mapped(req) (is_dma_capable() && \
94c65bfa62SMian Yousaf Kaukab 					(req->map_state != UN_MAPPED))
95c65bfa62SMian Yousaf Kaukab 
9692d2711fSHema Kalliguddi /* Maps the buffer to dma  */
9792d2711fSHema Kalliguddi 
9892d2711fSHema Kalliguddi static inline void map_dma_buffer(struct musb_request *request,
99c65bfa62SMian Yousaf Kaukab 			struct musb *musb, struct musb_ep *musb_ep)
10092d2711fSHema Kalliguddi {
1015f5761cbSMian Yousaf Kaukab 	int compatible = true;
1025f5761cbSMian Yousaf Kaukab 	struct dma_controller *dma = musb->dma_controller;
1035f5761cbSMian Yousaf Kaukab 
104c65bfa62SMian Yousaf Kaukab 	request->map_state = UN_MAPPED;
105c65bfa62SMian Yousaf Kaukab 
106c65bfa62SMian Yousaf Kaukab 	if (!is_dma_capable() || !musb_ep->dma)
107c65bfa62SMian Yousaf Kaukab 		return;
108c65bfa62SMian Yousaf Kaukab 
1095f5761cbSMian Yousaf Kaukab 	/* Check if DMA engine can handle this request.
1105f5761cbSMian Yousaf Kaukab 	 * DMA code must reject the USB request explicitly.
1115f5761cbSMian Yousaf Kaukab 	 * Default behaviour is to map the request.
1125f5761cbSMian Yousaf Kaukab 	 */
1135f5761cbSMian Yousaf Kaukab 	if (dma->is_compatible)
1145f5761cbSMian Yousaf Kaukab 		compatible = dma->is_compatible(musb_ep->dma,
1155f5761cbSMian Yousaf Kaukab 				musb_ep->packet_sz, request->request.buf,
1165f5761cbSMian Yousaf Kaukab 				request->request.length);
1175f5761cbSMian Yousaf Kaukab 	if (!compatible)
1185f5761cbSMian Yousaf Kaukab 		return;
1195f5761cbSMian Yousaf Kaukab 
12092d2711fSHema Kalliguddi 	if (request->request.dma == DMA_ADDR_INVALID) {
12192d2711fSHema Kalliguddi 		request->request.dma = dma_map_single(
12292d2711fSHema Kalliguddi 				musb->controller,
12392d2711fSHema Kalliguddi 				request->request.buf,
12492d2711fSHema Kalliguddi 				request->request.length,
12592d2711fSHema Kalliguddi 				request->tx
12692d2711fSHema Kalliguddi 					? DMA_TO_DEVICE
12792d2711fSHema Kalliguddi 					: DMA_FROM_DEVICE);
128c65bfa62SMian Yousaf Kaukab 		request->map_state = MUSB_MAPPED;
12992d2711fSHema Kalliguddi 	} else {
13092d2711fSHema Kalliguddi 		dma_sync_single_for_device(musb->controller,
13192d2711fSHema Kalliguddi 			request->request.dma,
13292d2711fSHema Kalliguddi 			request->request.length,
13392d2711fSHema Kalliguddi 			request->tx
13492d2711fSHema Kalliguddi 				? DMA_TO_DEVICE
13592d2711fSHema Kalliguddi 				: DMA_FROM_DEVICE);
136c65bfa62SMian Yousaf Kaukab 		request->map_state = PRE_MAPPED;
13792d2711fSHema Kalliguddi 	}
13892d2711fSHema Kalliguddi }
13992d2711fSHema Kalliguddi 
14092d2711fSHema Kalliguddi /* Unmap the buffer from dma and maps it back to cpu */
14192d2711fSHema Kalliguddi static inline void unmap_dma_buffer(struct musb_request *request,
14292d2711fSHema Kalliguddi 				struct musb *musb)
14392d2711fSHema Kalliguddi {
144c65bfa62SMian Yousaf Kaukab 	if (!is_buffer_mapped(request))
145c65bfa62SMian Yousaf Kaukab 		return;
146c65bfa62SMian Yousaf Kaukab 
14792d2711fSHema Kalliguddi 	if (request->request.dma == DMA_ADDR_INVALID) {
1485c8a86e1SFelipe Balbi 		dev_vdbg(musb->controller,
1495c8a86e1SFelipe Balbi 				"not unmapping a never mapped buffer\n");
15092d2711fSHema Kalliguddi 		return;
15192d2711fSHema Kalliguddi 	}
152c65bfa62SMian Yousaf Kaukab 	if (request->map_state == MUSB_MAPPED) {
15392d2711fSHema Kalliguddi 		dma_unmap_single(musb->controller,
15492d2711fSHema Kalliguddi 			request->request.dma,
15592d2711fSHema Kalliguddi 			request->request.length,
15692d2711fSHema Kalliguddi 			request->tx
15792d2711fSHema Kalliguddi 				? DMA_TO_DEVICE
15892d2711fSHema Kalliguddi 				: DMA_FROM_DEVICE);
15992d2711fSHema Kalliguddi 		request->request.dma = DMA_ADDR_INVALID;
160c65bfa62SMian Yousaf Kaukab 	} else { /* PRE_MAPPED */
16192d2711fSHema Kalliguddi 		dma_sync_single_for_cpu(musb->controller,
16292d2711fSHema Kalliguddi 			request->request.dma,
16392d2711fSHema Kalliguddi 			request->request.length,
16492d2711fSHema Kalliguddi 			request->tx
16592d2711fSHema Kalliguddi 				? DMA_TO_DEVICE
16692d2711fSHema Kalliguddi 				: DMA_FROM_DEVICE);
16792d2711fSHema Kalliguddi 	}
168c65bfa62SMian Yousaf Kaukab 	request->map_state = UN_MAPPED;
16992d2711fSHema Kalliguddi }
17092d2711fSHema Kalliguddi 
171550a7375SFelipe Balbi /*
172550a7375SFelipe Balbi  * Immediately complete a request.
173550a7375SFelipe Balbi  *
174550a7375SFelipe Balbi  * @param request the request to complete
175550a7375SFelipe Balbi  * @param status the status to complete the request with
176550a7375SFelipe Balbi  * Context: controller locked, IRQs blocked.
177550a7375SFelipe Balbi  */
178550a7375SFelipe Balbi void musb_g_giveback(
179550a7375SFelipe Balbi 	struct musb_ep		*ep,
180550a7375SFelipe Balbi 	struct usb_request	*request,
181550a7375SFelipe Balbi 	int			status)
182550a7375SFelipe Balbi __releases(ep->musb->lock)
183550a7375SFelipe Balbi __acquires(ep->musb->lock)
184550a7375SFelipe Balbi {
185550a7375SFelipe Balbi 	struct musb_request	*req;
186550a7375SFelipe Balbi 	struct musb		*musb;
187550a7375SFelipe Balbi 	int			busy = ep->busy;
188550a7375SFelipe Balbi 
189550a7375SFelipe Balbi 	req = to_musb_request(request);
190550a7375SFelipe Balbi 
191ad1adb89SFelipe Balbi 	list_del(&req->list);
192550a7375SFelipe Balbi 	if (req->request.status == -EINPROGRESS)
193550a7375SFelipe Balbi 		req->request.status = status;
194550a7375SFelipe Balbi 	musb = req->musb;
195550a7375SFelipe Balbi 
196550a7375SFelipe Balbi 	ep->busy = 1;
197550a7375SFelipe Balbi 	spin_unlock(&musb->lock);
19892d2711fSHema Kalliguddi 	unmap_dma_buffer(req, musb);
199550a7375SFelipe Balbi 	if (request->status == 0)
2005c8a86e1SFelipe Balbi 		dev_dbg(musb->controller, "%s done request %p,  %d/%d\n",
201550a7375SFelipe Balbi 				ep->end_point.name, request,
202550a7375SFelipe Balbi 				req->request.actual, req->request.length);
203550a7375SFelipe Balbi 	else
2045c8a86e1SFelipe Balbi 		dev_dbg(musb->controller, "%s request %p, %d/%d fault %d\n",
205550a7375SFelipe Balbi 				ep->end_point.name, request,
206550a7375SFelipe Balbi 				req->request.actual, req->request.length,
207550a7375SFelipe Balbi 				request->status);
208550a7375SFelipe Balbi 	req->request.complete(&req->ep->end_point, &req->request);
209550a7375SFelipe Balbi 	spin_lock(&musb->lock);
210550a7375SFelipe Balbi 	ep->busy = busy;
211550a7375SFelipe Balbi }
212550a7375SFelipe Balbi 
213550a7375SFelipe Balbi /* ----------------------------------------------------------------------- */
214550a7375SFelipe Balbi 
215550a7375SFelipe Balbi /*
216550a7375SFelipe Balbi  * Abort requests queued to an endpoint using the status. Synchronous.
217550a7375SFelipe Balbi  * caller locked controller and blocked irqs, and selected this ep.
218550a7375SFelipe Balbi  */
219550a7375SFelipe Balbi static void nuke(struct musb_ep *ep, const int status)
220550a7375SFelipe Balbi {
2215c8a86e1SFelipe Balbi 	struct musb		*musb = ep->musb;
222550a7375SFelipe Balbi 	struct musb_request	*req = NULL;
223550a7375SFelipe Balbi 	void __iomem *epio = ep->musb->endpoints[ep->current_epnum].regs;
224550a7375SFelipe Balbi 
225550a7375SFelipe Balbi 	ep->busy = 1;
226550a7375SFelipe Balbi 
227550a7375SFelipe Balbi 	if (is_dma_capable() && ep->dma) {
228550a7375SFelipe Balbi 		struct dma_controller	*c = ep->musb->dma_controller;
229550a7375SFelipe Balbi 		int value;
230b6e434a5SSergei Shtylyov 
231550a7375SFelipe Balbi 		if (ep->is_in) {
232b6e434a5SSergei Shtylyov 			/*
233b6e434a5SSergei Shtylyov 			 * The programming guide says that we must not clear
234b6e434a5SSergei Shtylyov 			 * the DMAMODE bit before DMAENAB, so we only
235b6e434a5SSergei Shtylyov 			 * clear it in the second write...
236b6e434a5SSergei Shtylyov 			 */
237550a7375SFelipe Balbi 			musb_writew(epio, MUSB_TXCSR,
238b6e434a5SSergei Shtylyov 				    MUSB_TXCSR_DMAMODE | MUSB_TXCSR_FLUSHFIFO);
239550a7375SFelipe Balbi 			musb_writew(epio, MUSB_TXCSR,
240550a7375SFelipe Balbi 					0 | MUSB_TXCSR_FLUSHFIFO);
241550a7375SFelipe Balbi 		} else {
242550a7375SFelipe Balbi 			musb_writew(epio, MUSB_RXCSR,
243550a7375SFelipe Balbi 					0 | MUSB_RXCSR_FLUSHFIFO);
244550a7375SFelipe Balbi 			musb_writew(epio, MUSB_RXCSR,
245550a7375SFelipe Balbi 					0 | MUSB_RXCSR_FLUSHFIFO);
246550a7375SFelipe Balbi 		}
247550a7375SFelipe Balbi 
248550a7375SFelipe Balbi 		value = c->channel_abort(ep->dma);
2495c8a86e1SFelipe Balbi 		dev_dbg(musb->controller, "%s: abort DMA --> %d\n",
2505c8a86e1SFelipe Balbi 				ep->name, value);
251550a7375SFelipe Balbi 		c->channel_release(ep->dma);
252550a7375SFelipe Balbi 		ep->dma = NULL;
253550a7375SFelipe Balbi 	}
254550a7375SFelipe Balbi 
255ad1adb89SFelipe Balbi 	while (!list_empty(&ep->req_list)) {
256ad1adb89SFelipe Balbi 		req = list_first_entry(&ep->req_list, struct musb_request, list);
257550a7375SFelipe Balbi 		musb_g_giveback(ep, &req->request, status);
258550a7375SFelipe Balbi 	}
259550a7375SFelipe Balbi }
260550a7375SFelipe Balbi 
261550a7375SFelipe Balbi /* ----------------------------------------------------------------------- */
262550a7375SFelipe Balbi 
263550a7375SFelipe Balbi /* Data transfers - pure PIO, pure DMA, or mixed mode */
264550a7375SFelipe Balbi 
265550a7375SFelipe Balbi /*
266550a7375SFelipe Balbi  * This assumes the separate CPPI engine is responding to DMA requests
267550a7375SFelipe Balbi  * from the usb core ... sequenced a bit differently from mentor dma.
268550a7375SFelipe Balbi  */
269550a7375SFelipe Balbi 
270550a7375SFelipe Balbi static inline int max_ep_writesize(struct musb *musb, struct musb_ep *ep)
271550a7375SFelipe Balbi {
272550a7375SFelipe Balbi 	if (can_bulk_split(musb, ep->type))
273550a7375SFelipe Balbi 		return ep->hw_ep->max_packet_sz_tx;
274550a7375SFelipe Balbi 	else
275550a7375SFelipe Balbi 		return ep->packet_sz;
276550a7375SFelipe Balbi }
277550a7375SFelipe Balbi 
278550a7375SFelipe Balbi 
279550a7375SFelipe Balbi #ifdef CONFIG_USB_INVENTRA_DMA
280550a7375SFelipe Balbi 
281550a7375SFelipe Balbi /* Peripheral tx (IN) using Mentor DMA works as follows:
282550a7375SFelipe Balbi 	Only mode 0 is used for transfers <= wPktSize,
283550a7375SFelipe Balbi 	mode 1 is used for larger transfers,
284550a7375SFelipe Balbi 
285550a7375SFelipe Balbi 	One of the following happens:
286550a7375SFelipe Balbi 	- Host sends IN token which causes an endpoint interrupt
287550a7375SFelipe Balbi 		-> TxAvail
288550a7375SFelipe Balbi 			-> if DMA is currently busy, exit.
289550a7375SFelipe Balbi 			-> if queue is non-empty, txstate().
290550a7375SFelipe Balbi 
291550a7375SFelipe Balbi 	- Request is queued by the gadget driver.
292550a7375SFelipe Balbi 		-> if queue was previously empty, txstate()
293550a7375SFelipe Balbi 
294550a7375SFelipe Balbi 	txstate()
295550a7375SFelipe Balbi 		-> start
296550a7375SFelipe Balbi 		  /\	-> setup DMA
297550a7375SFelipe Balbi 		  |     (data is transferred to the FIFO, then sent out when
298550a7375SFelipe Balbi 		  |	IN token(s) are recd from Host.
299550a7375SFelipe Balbi 		  |		-> DMA interrupt on completion
300550a7375SFelipe Balbi 		  |		   calls TxAvail.
301b6e434a5SSergei Shtylyov 		  |		      -> stop DMA, ~DMAENAB,
302550a7375SFelipe Balbi 		  |		      -> set TxPktRdy for last short pkt or zlp
303550a7375SFelipe Balbi 		  |		      -> Complete Request
304550a7375SFelipe Balbi 		  |		      -> Continue next request (call txstate)
305550a7375SFelipe Balbi 		  |___________________________________|
306550a7375SFelipe Balbi 
307550a7375SFelipe Balbi  * Non-Mentor DMA engines can of course work differently, such as by
308550a7375SFelipe Balbi  * upleveling from irq-per-packet to irq-per-buffer.
309550a7375SFelipe Balbi  */
310550a7375SFelipe Balbi 
311550a7375SFelipe Balbi #endif
312550a7375SFelipe Balbi 
313550a7375SFelipe Balbi /*
314550a7375SFelipe Balbi  * An endpoint is transmitting data. This can be called either from
315550a7375SFelipe Balbi  * the IRQ routine or from ep.queue() to kickstart a request on an
316550a7375SFelipe Balbi  * endpoint.
317550a7375SFelipe Balbi  *
318550a7375SFelipe Balbi  * Context: controller locked, IRQs blocked, endpoint selected
319550a7375SFelipe Balbi  */
320550a7375SFelipe Balbi static void txstate(struct musb *musb, struct musb_request *req)
321550a7375SFelipe Balbi {
322550a7375SFelipe Balbi 	u8			epnum = req->epnum;
323550a7375SFelipe Balbi 	struct musb_ep		*musb_ep;
324550a7375SFelipe Balbi 	void __iomem		*epio = musb->endpoints[epnum].regs;
325550a7375SFelipe Balbi 	struct usb_request	*request;
326550a7375SFelipe Balbi 	u16			fifo_count = 0, csr;
327550a7375SFelipe Balbi 	int			use_dma = 0;
328550a7375SFelipe Balbi 
329550a7375SFelipe Balbi 	musb_ep = req->ep;
330550a7375SFelipe Balbi 
331abf710e6SVikram Pandita 	/* Check if EP is disabled */
332abf710e6SVikram Pandita 	if (!musb_ep->desc) {
333abf710e6SVikram Pandita 		dev_dbg(musb->controller, "ep:%s disabled - ignore request\n",
334abf710e6SVikram Pandita 						musb_ep->end_point.name);
335abf710e6SVikram Pandita 		return;
336abf710e6SVikram Pandita 	}
337abf710e6SVikram Pandita 
338550a7375SFelipe Balbi 	/* we shouldn't get here while DMA is active ... but we do ... */
339550a7375SFelipe Balbi 	if (dma_channel_status(musb_ep->dma) == MUSB_DMA_STATUS_BUSY) {
3405c8a86e1SFelipe Balbi 		dev_dbg(musb->controller, "dma pending...\n");
341550a7375SFelipe Balbi 		return;
342550a7375SFelipe Balbi 	}
343550a7375SFelipe Balbi 
344550a7375SFelipe Balbi 	/* read TXCSR before */
345550a7375SFelipe Balbi 	csr = musb_readw(epio, MUSB_TXCSR);
346550a7375SFelipe Balbi 
347550a7375SFelipe Balbi 	request = &req->request;
348550a7375SFelipe Balbi 	fifo_count = min(max_ep_writesize(musb, musb_ep),
349550a7375SFelipe Balbi 			(int)(request->length - request->actual));
350550a7375SFelipe Balbi 
351550a7375SFelipe Balbi 	if (csr & MUSB_TXCSR_TXPKTRDY) {
3525c8a86e1SFelipe Balbi 		dev_dbg(musb->controller, "%s old packet still ready , txcsr %03x\n",
353550a7375SFelipe Balbi 				musb_ep->end_point.name, csr);
354550a7375SFelipe Balbi 		return;
355550a7375SFelipe Balbi 	}
356550a7375SFelipe Balbi 
357550a7375SFelipe Balbi 	if (csr & MUSB_TXCSR_P_SENDSTALL) {
3585c8a86e1SFelipe Balbi 		dev_dbg(musb->controller, "%s stalling, txcsr %03x\n",
359550a7375SFelipe Balbi 				musb_ep->end_point.name, csr);
360550a7375SFelipe Balbi 		return;
361550a7375SFelipe Balbi 	}
362550a7375SFelipe Balbi 
3635c8a86e1SFelipe Balbi 	dev_dbg(musb->controller, "hw_ep%d, maxpacket %d, fifo count %d, txcsr %03x\n",
364550a7375SFelipe Balbi 			epnum, musb_ep->packet_sz, fifo_count,
365550a7375SFelipe Balbi 			csr);
366550a7375SFelipe Balbi 
367550a7375SFelipe Balbi #ifndef	CONFIG_MUSB_PIO_ONLY
368c65bfa62SMian Yousaf Kaukab 	if (is_buffer_mapped(req)) {
369550a7375SFelipe Balbi 		struct dma_controller	*c = musb->dma_controller;
37066af83ddSMing Lei 		size_t request_size;
37166af83ddSMing Lei 
37266af83ddSMing Lei 		/* setup DMA, then program endpoint CSR */
37366af83ddSMing Lei 		request_size = min_t(size_t, request->length - request->actual,
37466af83ddSMing Lei 					musb_ep->dma->max_len);
375550a7375SFelipe Balbi 
376d17d535fSAjay Kumar Gupta 		use_dma = (request->dma != DMA_ADDR_INVALID && request_size);
377550a7375SFelipe Balbi 
378550a7375SFelipe Balbi 		/* MUSB_TXCSR_P_ISO is still set correctly */
379550a7375SFelipe Balbi 
380a48ff906SMian Yousaf Kaukab #if defined(CONFIG_USB_INVENTRA_DMA) || defined(CONFIG_USB_UX500_DMA)
381550a7375SFelipe Balbi 		{
382d1043a26SAnand Gadiyar 			if (request_size < musb_ep->packet_sz)
383550a7375SFelipe Balbi 				musb_ep->dma->desired_mode = 0;
384550a7375SFelipe Balbi 			else
385550a7375SFelipe Balbi 				musb_ep->dma->desired_mode = 1;
386550a7375SFelipe Balbi 
387550a7375SFelipe Balbi 			use_dma = use_dma && c->channel_program(
388550a7375SFelipe Balbi 					musb_ep->dma, musb_ep->packet_sz,
389550a7375SFelipe Balbi 					musb_ep->dma->desired_mode,
390796a83faSCliff Cai 					request->dma + request->actual, request_size);
391550a7375SFelipe Balbi 			if (use_dma) {
392550a7375SFelipe Balbi 				if (musb_ep->dma->desired_mode == 0) {
393b6e434a5SSergei Shtylyov 					/*
394b6e434a5SSergei Shtylyov 					 * We must not clear the DMAMODE bit
395b6e434a5SSergei Shtylyov 					 * before the DMAENAB bit -- and the
396b6e434a5SSergei Shtylyov 					 * latter doesn't always get cleared
397b6e434a5SSergei Shtylyov 					 * before we get here...
398b6e434a5SSergei Shtylyov 					 */
399b6e434a5SSergei Shtylyov 					csr &= ~(MUSB_TXCSR_AUTOSET
400b6e434a5SSergei Shtylyov 						| MUSB_TXCSR_DMAENAB);
401b6e434a5SSergei Shtylyov 					musb_writew(epio, MUSB_TXCSR, csr
402b6e434a5SSergei Shtylyov 						| MUSB_TXCSR_P_WZC_BITS);
403b6e434a5SSergei Shtylyov 					csr &= ~MUSB_TXCSR_DMAMODE;
404550a7375SFelipe Balbi 					csr |= (MUSB_TXCSR_DMAENAB |
405550a7375SFelipe Balbi 							MUSB_TXCSR_MODE);
406550a7375SFelipe Balbi 					/* against programming guide */
407f11d893dSMing Lei 				} else {
408f11d893dSMing Lei 					csr |= (MUSB_TXCSR_DMAENAB
409550a7375SFelipe Balbi 							| MUSB_TXCSR_DMAMODE
410550a7375SFelipe Balbi 							| MUSB_TXCSR_MODE);
411f11d893dSMing Lei 					if (!musb_ep->hb_mult)
412f11d893dSMing Lei 						csr |= MUSB_TXCSR_AUTOSET;
413f11d893dSMing Lei 				}
414550a7375SFelipe Balbi 				csr &= ~MUSB_TXCSR_P_UNDERRUN;
415f11d893dSMing Lei 
416550a7375SFelipe Balbi 				musb_writew(epio, MUSB_TXCSR, csr);
417550a7375SFelipe Balbi 			}
418550a7375SFelipe Balbi 		}
419550a7375SFelipe Balbi 
420550a7375SFelipe Balbi #elif defined(CONFIG_USB_TI_CPPI_DMA)
421550a7375SFelipe Balbi 		/* program endpoint CSR first, then setup DMA */
422b6e434a5SSergei Shtylyov 		csr &= ~(MUSB_TXCSR_P_UNDERRUN | MUSB_TXCSR_TXPKTRDY);
42337e3ee99SSergei Shtylyov 		csr |= MUSB_TXCSR_DMAENAB | MUSB_TXCSR_DMAMODE |
42437e3ee99SSergei Shtylyov 		       MUSB_TXCSR_MODE;
425550a7375SFelipe Balbi 		musb_writew(epio, MUSB_TXCSR,
426550a7375SFelipe Balbi 			(MUSB_TXCSR_P_WZC_BITS & ~MUSB_TXCSR_P_UNDERRUN)
427550a7375SFelipe Balbi 				| csr);
428550a7375SFelipe Balbi 
429550a7375SFelipe Balbi 		/* ensure writebuffer is empty */
430550a7375SFelipe Balbi 		csr = musb_readw(epio, MUSB_TXCSR);
431550a7375SFelipe Balbi 
432550a7375SFelipe Balbi 		/* NOTE host side sets DMAENAB later than this; both are
433550a7375SFelipe Balbi 		 * OK since the transfer dma glue (between CPPI and Mentor
434550a7375SFelipe Balbi 		 * fifos) just tells CPPI it could start.  Data only moves
435550a7375SFelipe Balbi 		 * to the USB TX fifo when both fifos are ready.
436550a7375SFelipe Balbi 		 */
437550a7375SFelipe Balbi 
438550a7375SFelipe Balbi 		/* "mode" is irrelevant here; handle terminating ZLPs like
439550a7375SFelipe Balbi 		 * PIO does, since the hardware RNDIS mode seems unreliable
440550a7375SFelipe Balbi 		 * except for the last-packet-is-already-short case.
441550a7375SFelipe Balbi 		 */
442550a7375SFelipe Balbi 		use_dma = use_dma && c->channel_program(
443550a7375SFelipe Balbi 				musb_ep->dma, musb_ep->packet_sz,
444550a7375SFelipe Balbi 				0,
44566af83ddSMing Lei 				request->dma + request->actual,
44666af83ddSMing Lei 				request_size);
447550a7375SFelipe Balbi 		if (!use_dma) {
448550a7375SFelipe Balbi 			c->channel_release(musb_ep->dma);
449550a7375SFelipe Balbi 			musb_ep->dma = NULL;
450b6e434a5SSergei Shtylyov 			csr &= ~MUSB_TXCSR_DMAENAB;
451b6e434a5SSergei Shtylyov 			musb_writew(epio, MUSB_TXCSR, csr);
452550a7375SFelipe Balbi 			/* invariant: prequest->buf is non-null */
453550a7375SFelipe Balbi 		}
454550a7375SFelipe Balbi #elif defined(CONFIG_USB_TUSB_OMAP_DMA)
455550a7375SFelipe Balbi 		use_dma = use_dma && c->channel_program(
456550a7375SFelipe Balbi 				musb_ep->dma, musb_ep->packet_sz,
457550a7375SFelipe Balbi 				request->zero,
45866af83ddSMing Lei 				request->dma + request->actual,
45966af83ddSMing Lei 				request_size);
460550a7375SFelipe Balbi #endif
461550a7375SFelipe Balbi 	}
462550a7375SFelipe Balbi #endif
463550a7375SFelipe Balbi 
464550a7375SFelipe Balbi 	if (!use_dma) {
46592d2711fSHema Kalliguddi 		/*
46692d2711fSHema Kalliguddi 		 * Unmap the dma buffer back to cpu if dma channel
46792d2711fSHema Kalliguddi 		 * programming fails
46892d2711fSHema Kalliguddi 		 */
46992d2711fSHema Kalliguddi 		unmap_dma_buffer(req, musb);
47092d2711fSHema Kalliguddi 
471550a7375SFelipe Balbi 		musb_write_fifo(musb_ep->hw_ep, fifo_count,
472550a7375SFelipe Balbi 				(u8 *) (request->buf + request->actual));
473550a7375SFelipe Balbi 		request->actual += fifo_count;
474550a7375SFelipe Balbi 		csr |= MUSB_TXCSR_TXPKTRDY;
475550a7375SFelipe Balbi 		csr &= ~MUSB_TXCSR_P_UNDERRUN;
476550a7375SFelipe Balbi 		musb_writew(epio, MUSB_TXCSR, csr);
477550a7375SFelipe Balbi 	}
478550a7375SFelipe Balbi 
479550a7375SFelipe Balbi 	/* host may already have the data when this message shows... */
4805c8a86e1SFelipe Balbi 	dev_dbg(musb->controller, "%s TX/IN %s len %d/%d, txcsr %04x, fifo %d/%d\n",
481550a7375SFelipe Balbi 			musb_ep->end_point.name, use_dma ? "dma" : "pio",
482550a7375SFelipe Balbi 			request->actual, request->length,
483550a7375SFelipe Balbi 			musb_readw(epio, MUSB_TXCSR),
484550a7375SFelipe Balbi 			fifo_count,
485550a7375SFelipe Balbi 			musb_readw(epio, MUSB_TXMAXP));
486550a7375SFelipe Balbi }
487550a7375SFelipe Balbi 
488550a7375SFelipe Balbi /*
489550a7375SFelipe Balbi  * FIFO state update (e.g. data ready).
490550a7375SFelipe Balbi  * Called from IRQ,  with controller locked.
491550a7375SFelipe Balbi  */
492550a7375SFelipe Balbi void musb_g_tx(struct musb *musb, u8 epnum)
493550a7375SFelipe Balbi {
494550a7375SFelipe Balbi 	u16			csr;
495ad1adb89SFelipe Balbi 	struct musb_request	*req;
496550a7375SFelipe Balbi 	struct usb_request	*request;
497550a7375SFelipe Balbi 	u8 __iomem		*mbase = musb->mregs;
498550a7375SFelipe Balbi 	struct musb_ep		*musb_ep = &musb->endpoints[epnum].ep_in;
499550a7375SFelipe Balbi 	void __iomem		*epio = musb->endpoints[epnum].regs;
500550a7375SFelipe Balbi 	struct dma_channel	*dma;
501550a7375SFelipe Balbi 
502550a7375SFelipe Balbi 	musb_ep_select(mbase, epnum);
503ad1adb89SFelipe Balbi 	req = next_request(musb_ep);
504ad1adb89SFelipe Balbi 	request = &req->request;
505550a7375SFelipe Balbi 
506550a7375SFelipe Balbi 	csr = musb_readw(epio, MUSB_TXCSR);
5075c8a86e1SFelipe Balbi 	dev_dbg(musb->controller, "<== %s, txcsr %04x\n", musb_ep->end_point.name, csr);
508550a7375SFelipe Balbi 
509550a7375SFelipe Balbi 	dma = is_dma_capable() ? musb_ep->dma : NULL;
5107723de7eSSergei Shtylyov 
5117723de7eSSergei Shtylyov 	/*
5127723de7eSSergei Shtylyov 	 * REVISIT: for high bandwidth, MUSB_TXCSR_P_INCOMPTX
5137723de7eSSergei Shtylyov 	 * probably rates reporting as a host error.
514550a7375SFelipe Balbi 	 */
515550a7375SFelipe Balbi 	if (csr & MUSB_TXCSR_P_SENTSTALL) {
516550a7375SFelipe Balbi 		csr |=	MUSB_TXCSR_P_WZC_BITS;
517550a7375SFelipe Balbi 		csr &= ~MUSB_TXCSR_P_SENTSTALL;
518550a7375SFelipe Balbi 		musb_writew(epio, MUSB_TXCSR, csr);
5197723de7eSSergei Shtylyov 		return;
520550a7375SFelipe Balbi 	}
521550a7375SFelipe Balbi 
522550a7375SFelipe Balbi 	if (csr & MUSB_TXCSR_P_UNDERRUN) {
5237723de7eSSergei Shtylyov 		/* We NAKed, no big deal... little reason to care. */
524550a7375SFelipe Balbi 		csr |=	 MUSB_TXCSR_P_WZC_BITS;
5257723de7eSSergei Shtylyov 		csr &= ~(MUSB_TXCSR_P_UNDERRUN | MUSB_TXCSR_TXPKTRDY);
526550a7375SFelipe Balbi 		musb_writew(epio, MUSB_TXCSR, csr);
5275c8a86e1SFelipe Balbi 		dev_vdbg(musb->controller, "underrun on ep%d, req %p\n",
5285c8a86e1SFelipe Balbi 				epnum, request);
529550a7375SFelipe Balbi 	}
530550a7375SFelipe Balbi 
531550a7375SFelipe Balbi 	if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) {
5327723de7eSSergei Shtylyov 		/*
5337723de7eSSergei Shtylyov 		 * SHOULD NOT HAPPEN... has with CPPI though, after
534550a7375SFelipe Balbi 		 * changing SENDSTALL (and other cases); harmless?
535550a7375SFelipe Balbi 		 */
5365c8a86e1SFelipe Balbi 		dev_dbg(musb->controller, "%s dma still busy?\n", musb_ep->end_point.name);
5377723de7eSSergei Shtylyov 		return;
538550a7375SFelipe Balbi 	}
539550a7375SFelipe Balbi 
540550a7375SFelipe Balbi 	if (request) {
541550a7375SFelipe Balbi 		u8	is_dma = 0;
542550a7375SFelipe Balbi 
543550a7375SFelipe Balbi 		if (dma && (csr & MUSB_TXCSR_DMAENAB)) {
544550a7375SFelipe Balbi 			is_dma = 1;
545550a7375SFelipe Balbi 			csr |= MUSB_TXCSR_P_WZC_BITS;
5467723de7eSSergei Shtylyov 			csr &= ~(MUSB_TXCSR_DMAENAB | MUSB_TXCSR_P_UNDERRUN |
547100d4a9dSMian Yousaf Kaukab 				 MUSB_TXCSR_TXPKTRDY | MUSB_TXCSR_AUTOSET);
548550a7375SFelipe Balbi 			musb_writew(epio, MUSB_TXCSR, csr);
5497723de7eSSergei Shtylyov 			/* Ensure writebuffer is empty. */
550550a7375SFelipe Balbi 			csr = musb_readw(epio, MUSB_TXCSR);
551550a7375SFelipe Balbi 			request->actual += musb_ep->dma->actual_len;
5525c8a86e1SFelipe Balbi 			dev_dbg(musb->controller, "TXCSR%d %04x, DMA off, len %zu, req %p\n",
5537723de7eSSergei Shtylyov 				epnum, csr, musb_ep->dma->actual_len, request);
554550a7375SFelipe Balbi 		}
555550a7375SFelipe Balbi 
5567723de7eSSergei Shtylyov 		/*
5577723de7eSSergei Shtylyov 		 * First, maybe a terminating short packet. Some DMA
5587723de7eSSergei Shtylyov 		 * engines might handle this by themselves.
559550a7375SFelipe Balbi 		 */
5607723de7eSSergei Shtylyov 		if ((request->zero && request->length
561e7379aaaSMing Lei 			&& (request->length % musb_ep->packet_sz == 0)
562e7379aaaSMing Lei 			&& (request->actual == request->length))
563a48ff906SMian Yousaf Kaukab #if defined(CONFIG_USB_INVENTRA_DMA) || defined(CONFIG_USB_UX500_DMA)
5647723de7eSSergei Shtylyov 			|| (is_dma && (!dma->desired_mode ||
565550a7375SFelipe Balbi 				(request->actual &
566550a7375SFelipe Balbi 					(musb_ep->packet_sz - 1))))
567550a7375SFelipe Balbi #endif
568550a7375SFelipe Balbi 		) {
5697723de7eSSergei Shtylyov 			/*
5707723de7eSSergei Shtylyov 			 * On DMA completion, FIFO may not be
5717723de7eSSergei Shtylyov 			 * available yet...
572550a7375SFelipe Balbi 			 */
573550a7375SFelipe Balbi 			if (csr & MUSB_TXCSR_TXPKTRDY)
5747723de7eSSergei Shtylyov 				return;
575550a7375SFelipe Balbi 
5765c8a86e1SFelipe Balbi 			dev_dbg(musb->controller, "sending zero pkt\n");
5777723de7eSSergei Shtylyov 			musb_writew(epio, MUSB_TXCSR, MUSB_TXCSR_MODE
578550a7375SFelipe Balbi 					| MUSB_TXCSR_TXPKTRDY);
579550a7375SFelipe Balbi 			request->zero = 0;
580550a7375SFelipe Balbi 		}
581550a7375SFelipe Balbi 
582bb27bc2cSMing Lei 		if (request->actual == request->length) {
583550a7375SFelipe Balbi 			musb_g_giveback(musb_ep, request, 0);
58439287076SSupriya Karanth 			/*
58539287076SSupriya Karanth 			 * In the giveback function the MUSB lock is
58639287076SSupriya Karanth 			 * released and acquired after sometime. During
58739287076SSupriya Karanth 			 * this time period the INDEX register could get
58839287076SSupriya Karanth 			 * changed by the gadget_queue function especially
58939287076SSupriya Karanth 			 * on SMP systems. Reselect the INDEX to be sure
59039287076SSupriya Karanth 			 * we are reading/modifying the right registers
59139287076SSupriya Karanth 			 */
59239287076SSupriya Karanth 			musb_ep_select(mbase, epnum);
593ad1adb89SFelipe Balbi 			req = musb_ep->desc ? next_request(musb_ep) : NULL;
594ad1adb89SFelipe Balbi 			if (!req) {
5955c8a86e1SFelipe Balbi 				dev_dbg(musb->controller, "%s idle now\n",
596550a7375SFelipe Balbi 					musb_ep->end_point.name);
5977723de7eSSergei Shtylyov 				return;
59895962a77SSergei Shtylyov 			}
599550a7375SFelipe Balbi 		}
600550a7375SFelipe Balbi 
601ad1adb89SFelipe Balbi 		txstate(musb, req);
602550a7375SFelipe Balbi 	}
603550a7375SFelipe Balbi }
604550a7375SFelipe Balbi 
605550a7375SFelipe Balbi /* ------------------------------------------------------------ */
606550a7375SFelipe Balbi 
607550a7375SFelipe Balbi #ifdef CONFIG_USB_INVENTRA_DMA
608550a7375SFelipe Balbi 
609550a7375SFelipe Balbi /* Peripheral rx (OUT) using Mentor DMA works as follows:
610550a7375SFelipe Balbi 	- Only mode 0 is used.
611550a7375SFelipe Balbi 
612550a7375SFelipe Balbi 	- Request is queued by the gadget class driver.
613550a7375SFelipe Balbi 		-> if queue was previously empty, rxstate()
614550a7375SFelipe Balbi 
615550a7375SFelipe Balbi 	- Host sends OUT token which causes an endpoint interrupt
616550a7375SFelipe Balbi 	  /\      -> RxReady
617550a7375SFelipe Balbi 	  |	      -> if request queued, call rxstate
618550a7375SFelipe Balbi 	  |		/\	-> setup DMA
619550a7375SFelipe Balbi 	  |		|	     -> DMA interrupt on completion
620550a7375SFelipe Balbi 	  |		|		-> RxReady
621550a7375SFelipe Balbi 	  |		|		      -> stop DMA
622550a7375SFelipe Balbi 	  |		|		      -> ack the read
623550a7375SFelipe Balbi 	  |		|		      -> if data recd = max expected
624550a7375SFelipe Balbi 	  |		|				by the request, or host
625550a7375SFelipe Balbi 	  |		|				sent a short packet,
626550a7375SFelipe Balbi 	  |		|				complete the request,
627550a7375SFelipe Balbi 	  |		|				and start the next one.
628550a7375SFelipe Balbi 	  |		|_____________________________________|
629550a7375SFelipe Balbi 	  |					 else just wait for the host
630550a7375SFelipe Balbi 	  |					    to send the next OUT token.
631550a7375SFelipe Balbi 	  |__________________________________________________|
632550a7375SFelipe Balbi 
633550a7375SFelipe Balbi  * Non-Mentor DMA engines can of course work differently.
634550a7375SFelipe Balbi  */
635550a7375SFelipe Balbi 
636550a7375SFelipe Balbi #endif
637550a7375SFelipe Balbi 
638550a7375SFelipe Balbi /*
639550a7375SFelipe Balbi  * Context: controller locked, IRQs blocked, endpoint selected
640550a7375SFelipe Balbi  */
641550a7375SFelipe Balbi static void rxstate(struct musb *musb, struct musb_request *req)
642550a7375SFelipe Balbi {
643550a7375SFelipe Balbi 	const u8		epnum = req->epnum;
644550a7375SFelipe Balbi 	struct usb_request	*request = &req->request;
645bd2e74d6SMing Lei 	struct musb_ep		*musb_ep;
646550a7375SFelipe Balbi 	void __iomem		*epio = musb->endpoints[epnum].regs;
647f0443afdSSergei Shtylyov 	unsigned		len = 0;
648f0443afdSSergei Shtylyov 	u16			fifo_count;
649cea83241SSergei Shtylyov 	u16			csr = musb_readw(epio, MUSB_RXCSR);
650bd2e74d6SMing Lei 	struct musb_hw_ep	*hw_ep = &musb->endpoints[epnum];
6510ae52d54SAnand Gadiyar 	u8			use_mode_1;
652bd2e74d6SMing Lei 
653bd2e74d6SMing Lei 	if (hw_ep->is_shared_fifo)
654bd2e74d6SMing Lei 		musb_ep = &hw_ep->ep_in;
655bd2e74d6SMing Lei 	else
656bd2e74d6SMing Lei 		musb_ep = &hw_ep->ep_out;
657bd2e74d6SMing Lei 
658f0443afdSSergei Shtylyov 	fifo_count = musb_ep->packet_sz;
659550a7375SFelipe Balbi 
660abf710e6SVikram Pandita 	/* Check if EP is disabled */
661abf710e6SVikram Pandita 	if (!musb_ep->desc) {
662abf710e6SVikram Pandita 		dev_dbg(musb->controller, "ep:%s disabled - ignore request\n",
663abf710e6SVikram Pandita 						musb_ep->end_point.name);
664abf710e6SVikram Pandita 		return;
665abf710e6SVikram Pandita 	}
666abf710e6SVikram Pandita 
667cea83241SSergei Shtylyov 	/* We shouldn't get here while DMA is active, but we do... */
668cea83241SSergei Shtylyov 	if (dma_channel_status(musb_ep->dma) == MUSB_DMA_STATUS_BUSY) {
6695c8a86e1SFelipe Balbi 		dev_dbg(musb->controller, "DMA pending...\n");
670cea83241SSergei Shtylyov 		return;
671cea83241SSergei Shtylyov 	}
672cea83241SSergei Shtylyov 
673cea83241SSergei Shtylyov 	if (csr & MUSB_RXCSR_P_SENDSTALL) {
6745c8a86e1SFelipe Balbi 		dev_dbg(musb->controller, "%s stalling, RXCSR %04x\n",
675cea83241SSergei Shtylyov 		    musb_ep->end_point.name, csr);
676cea83241SSergei Shtylyov 		return;
677cea83241SSergei Shtylyov 	}
678550a7375SFelipe Balbi 
679c65bfa62SMian Yousaf Kaukab 	if (is_cppi_enabled() && is_buffer_mapped(req)) {
680550a7375SFelipe Balbi 		struct dma_controller	*c = musb->dma_controller;
681550a7375SFelipe Balbi 		struct dma_channel	*channel = musb_ep->dma;
682550a7375SFelipe Balbi 
683550a7375SFelipe Balbi 		/* NOTE:  CPPI won't actually stop advancing the DMA
684550a7375SFelipe Balbi 		 * queue after short packet transfers, so this is almost
685550a7375SFelipe Balbi 		 * always going to run as IRQ-per-packet DMA so that
686550a7375SFelipe Balbi 		 * faults will be handled correctly.
687550a7375SFelipe Balbi 		 */
688550a7375SFelipe Balbi 		if (c->channel_program(channel,
689550a7375SFelipe Balbi 				musb_ep->packet_sz,
690550a7375SFelipe Balbi 				!request->short_not_ok,
691550a7375SFelipe Balbi 				request->dma + request->actual,
692550a7375SFelipe Balbi 				request->length - request->actual)) {
693550a7375SFelipe Balbi 
694550a7375SFelipe Balbi 			/* make sure that if an rxpkt arrived after the irq,
695550a7375SFelipe Balbi 			 * the cppi engine will be ready to take it as soon
696550a7375SFelipe Balbi 			 * as DMA is enabled
697550a7375SFelipe Balbi 			 */
698550a7375SFelipe Balbi 			csr &= ~(MUSB_RXCSR_AUTOCLEAR
699550a7375SFelipe Balbi 					| MUSB_RXCSR_DMAMODE);
700550a7375SFelipe Balbi 			csr |= MUSB_RXCSR_DMAENAB | MUSB_RXCSR_P_WZC_BITS;
701550a7375SFelipe Balbi 			musb_writew(epio, MUSB_RXCSR, csr);
702550a7375SFelipe Balbi 			return;
703550a7375SFelipe Balbi 		}
704550a7375SFelipe Balbi 	}
705550a7375SFelipe Balbi 
706550a7375SFelipe Balbi 	if (csr & MUSB_RXCSR_RXPKTRDY) {
707f0443afdSSergei Shtylyov 		fifo_count = musb_readw(epio, MUSB_RXCOUNT);
7080ae52d54SAnand Gadiyar 
7090ae52d54SAnand Gadiyar 		/*
71000a89180SFelipe Balbi 		 * Enable Mode 1 on RX transfers only when short_not_ok flag
71100a89180SFelipe Balbi 		 * is set. Currently short_not_ok flag is set only from
71200a89180SFelipe Balbi 		 * file_storage and f_mass_storage drivers
7130ae52d54SAnand Gadiyar 		 */
71400a89180SFelipe Balbi 
71500a89180SFelipe Balbi 		if (request->short_not_ok && fifo_count == musb_ep->packet_sz)
7160ae52d54SAnand Gadiyar 			use_mode_1 = 1;
7170ae52d54SAnand Gadiyar 		else
7180ae52d54SAnand Gadiyar 			use_mode_1 = 0;
7190ae52d54SAnand Gadiyar 
720550a7375SFelipe Balbi 		if (request->actual < request->length) {
721550a7375SFelipe Balbi #ifdef CONFIG_USB_INVENTRA_DMA
722c65bfa62SMian Yousaf Kaukab 			if (is_buffer_mapped(req)) {
723550a7375SFelipe Balbi 				struct dma_controller	*c;
724550a7375SFelipe Balbi 				struct dma_channel	*channel;
725550a7375SFelipe Balbi 				int			use_dma = 0;
726660fa886SRoger Quadros 				int transfer_size;
727550a7375SFelipe Balbi 
728550a7375SFelipe Balbi 				c = musb->dma_controller;
729550a7375SFelipe Balbi 				channel = musb_ep->dma;
730550a7375SFelipe Balbi 
73100a89180SFelipe Balbi 	/* We use DMA Req mode 0 in rx_csr, and DMA controller operates in
73200a89180SFelipe Balbi 	 * mode 0 only. So we do not get endpoint interrupts due to DMA
73300a89180SFelipe Balbi 	 * completion. We only get interrupts from DMA controller.
73400a89180SFelipe Balbi 	 *
73500a89180SFelipe Balbi 	 * We could operate in DMA mode 1 if we knew the size of the tranfer
73600a89180SFelipe Balbi 	 * in advance. For mass storage class, request->length = what the host
73700a89180SFelipe Balbi 	 * sends, so that'd work.  But for pretty much everything else,
73800a89180SFelipe Balbi 	 * request->length is routinely more than what the host sends. For
73900a89180SFelipe Balbi 	 * most these gadgets, end of is signified either by a short packet,
74000a89180SFelipe Balbi 	 * or filling the last byte of the buffer.  (Sending extra data in
74100a89180SFelipe Balbi 	 * that last pckate should trigger an overflow fault.)  But in mode 1,
74200a89180SFelipe Balbi 	 * we don't get DMA completion interrupt for short packets.
74300a89180SFelipe Balbi 	 *
74400a89180SFelipe Balbi 	 * Theoretically, we could enable DMAReq irq (MUSB_RXCSR_DMAMODE = 1),
74500a89180SFelipe Balbi 	 * to get endpoint interrupt on every DMA req, but that didn't seem
74600a89180SFelipe Balbi 	 * to work reliably.
74700a89180SFelipe Balbi 	 *
74800a89180SFelipe Balbi 	 * REVISIT an updated g_file_storage can set req->short_not_ok, which
74900a89180SFelipe Balbi 	 * then becomes usable as a runtime "use mode 1" hint...
75000a89180SFelipe Balbi 	 */
75100a89180SFelipe Balbi 
7520ae52d54SAnand Gadiyar 				/* Experimental: Mode1 works with mass storage use cases */
7530ae52d54SAnand Gadiyar 				if (use_mode_1) {
7549001d80dSMing Lei 					csr |= MUSB_RXCSR_AUTOCLEAR;
7550ae52d54SAnand Gadiyar 					musb_writew(epio, MUSB_RXCSR, csr);
7560ae52d54SAnand Gadiyar 					csr |= MUSB_RXCSR_DMAENAB;
7570ae52d54SAnand Gadiyar 					musb_writew(epio, MUSB_RXCSR, csr);
758550a7375SFelipe Balbi 
7590ae52d54SAnand Gadiyar 					/*
7600ae52d54SAnand Gadiyar 					 * this special sequence (enabling and then
761550a7375SFelipe Balbi 					 * disabling MUSB_RXCSR_DMAMODE) is required
762550a7375SFelipe Balbi 					 * to get DMAReq to activate
763550a7375SFelipe Balbi 					 */
764550a7375SFelipe Balbi 					musb_writew(epio, MUSB_RXCSR,
765550a7375SFelipe Balbi 						csr | MUSB_RXCSR_DMAMODE);
7660ae52d54SAnand Gadiyar 					musb_writew(epio, MUSB_RXCSR, csr);
7670ae52d54SAnand Gadiyar 
768660fa886SRoger Quadros 					transfer_size = min(request->length - request->actual,
769660fa886SRoger Quadros 							channel->max_len);
770660fa886SRoger Quadros 					musb_ep->dma->desired_mode = 1;
771660fa886SRoger Quadros 
7720ae52d54SAnand Gadiyar 				} else {
7739001d80dSMing Lei 					if (!musb_ep->hb_mult &&
7749001d80dSMing Lei 						musb_ep->hw_ep->rx_double_buffered)
7759001d80dSMing Lei 						csr |= MUSB_RXCSR_AUTOCLEAR;
7760ae52d54SAnand Gadiyar 					csr |= MUSB_RXCSR_DMAENAB;
777550a7375SFelipe Balbi 					musb_writew(epio, MUSB_RXCSR, csr);
778550a7375SFelipe Balbi 
7791018b4e4SMing Lei 					transfer_size = min(request->length - request->actual,
780f0443afdSSergei Shtylyov 							(unsigned)fifo_count);
781550a7375SFelipe Balbi 					musb_ep->dma->desired_mode = 0;
7820ae52d54SAnand Gadiyar 				}
783550a7375SFelipe Balbi 
784550a7375SFelipe Balbi 				use_dma = c->channel_program(
785550a7375SFelipe Balbi 						channel,
786550a7375SFelipe Balbi 						musb_ep->packet_sz,
787550a7375SFelipe Balbi 						channel->desired_mode,
788550a7375SFelipe Balbi 						request->dma
789550a7375SFelipe Balbi 						+ request->actual,
790550a7375SFelipe Balbi 						transfer_size);
791550a7375SFelipe Balbi 
792550a7375SFelipe Balbi 				if (use_dma)
793550a7375SFelipe Balbi 					return;
794550a7375SFelipe Balbi 			}
795a48ff906SMian Yousaf Kaukab #elif defined(CONFIG_USB_UX500_DMA)
796a48ff906SMian Yousaf Kaukab 			if ((is_buffer_mapped(req)) &&
797a48ff906SMian Yousaf Kaukab 				(request->actual < request->length)) {
798a48ff906SMian Yousaf Kaukab 
799a48ff906SMian Yousaf Kaukab 				struct dma_controller *c;
800a48ff906SMian Yousaf Kaukab 				struct dma_channel *channel;
801a48ff906SMian Yousaf Kaukab 				int transfer_size = 0;
802a48ff906SMian Yousaf Kaukab 
803a48ff906SMian Yousaf Kaukab 				c = musb->dma_controller;
804a48ff906SMian Yousaf Kaukab 				channel = musb_ep->dma;
805a48ff906SMian Yousaf Kaukab 
806a48ff906SMian Yousaf Kaukab 				/* In case first packet is short */
807f0443afdSSergei Shtylyov 				if (fifo_count < musb_ep->packet_sz)
808f0443afdSSergei Shtylyov 					transfer_size = fifo_count;
809a48ff906SMian Yousaf Kaukab 				else if (request->short_not_ok)
810a48ff906SMian Yousaf Kaukab 					transfer_size =	min(request->length -
811a48ff906SMian Yousaf Kaukab 							request->actual,
812a48ff906SMian Yousaf Kaukab 							channel->max_len);
813a48ff906SMian Yousaf Kaukab 				else
814a48ff906SMian Yousaf Kaukab 					transfer_size = min(request->length -
815a48ff906SMian Yousaf Kaukab 							request->actual,
816f0443afdSSergei Shtylyov 							(unsigned)fifo_count);
817a48ff906SMian Yousaf Kaukab 
818a48ff906SMian Yousaf Kaukab 				csr &= ~MUSB_RXCSR_DMAMODE;
819a48ff906SMian Yousaf Kaukab 				csr |= (MUSB_RXCSR_DMAENAB |
820a48ff906SMian Yousaf Kaukab 					MUSB_RXCSR_AUTOCLEAR);
821a48ff906SMian Yousaf Kaukab 
822a48ff906SMian Yousaf Kaukab 				musb_writew(epio, MUSB_RXCSR, csr);
823a48ff906SMian Yousaf Kaukab 
824a48ff906SMian Yousaf Kaukab 				if (transfer_size <= musb_ep->packet_sz) {
825a48ff906SMian Yousaf Kaukab 					musb_ep->dma->desired_mode = 0;
826a48ff906SMian Yousaf Kaukab 				} else {
827a48ff906SMian Yousaf Kaukab 					musb_ep->dma->desired_mode = 1;
828a48ff906SMian Yousaf Kaukab 					/* Mode must be set after DMAENAB */
829a48ff906SMian Yousaf Kaukab 					csr |= MUSB_RXCSR_DMAMODE;
830a48ff906SMian Yousaf Kaukab 					musb_writew(epio, MUSB_RXCSR, csr);
831a48ff906SMian Yousaf Kaukab 				}
832a48ff906SMian Yousaf Kaukab 
833a48ff906SMian Yousaf Kaukab 				if (c->channel_program(channel,
834a48ff906SMian Yousaf Kaukab 							musb_ep->packet_sz,
835a48ff906SMian Yousaf Kaukab 							channel->desired_mode,
836a48ff906SMian Yousaf Kaukab 							request->dma
837a48ff906SMian Yousaf Kaukab 							+ request->actual,
838a48ff906SMian Yousaf Kaukab 							transfer_size))
839a48ff906SMian Yousaf Kaukab 
840a48ff906SMian Yousaf Kaukab 					return;
841a48ff906SMian Yousaf Kaukab 			}
842550a7375SFelipe Balbi #endif	/* Mentor's DMA */
843550a7375SFelipe Balbi 
844f0443afdSSergei Shtylyov 			len = request->length - request->actual;
8455c8a86e1SFelipe Balbi 			dev_dbg(musb->controller, "%s OUT/RX pio fifo %d/%d, maxpacket %d\n",
846550a7375SFelipe Balbi 					musb_ep->end_point.name,
847f0443afdSSergei Shtylyov 					fifo_count, len,
848550a7375SFelipe Balbi 					musb_ep->packet_sz);
849550a7375SFelipe Balbi 
850c2c96321SFelipe Balbi 			fifo_count = min_t(unsigned, len, fifo_count);
851550a7375SFelipe Balbi 
852550a7375SFelipe Balbi #ifdef	CONFIG_USB_TUSB_OMAP_DMA
853c65bfa62SMian Yousaf Kaukab 			if (tusb_dma_omap() && is_buffer_mapped(req)) {
854550a7375SFelipe Balbi 				struct dma_controller *c = musb->dma_controller;
855550a7375SFelipe Balbi 				struct dma_channel *channel = musb_ep->dma;
856550a7375SFelipe Balbi 				u32 dma_addr = request->dma + request->actual;
857550a7375SFelipe Balbi 				int ret;
858550a7375SFelipe Balbi 
859550a7375SFelipe Balbi 				ret = c->channel_program(channel,
860550a7375SFelipe Balbi 						musb_ep->packet_sz,
861550a7375SFelipe Balbi 						channel->desired_mode,
862550a7375SFelipe Balbi 						dma_addr,
863550a7375SFelipe Balbi 						fifo_count);
864550a7375SFelipe Balbi 				if (ret)
865550a7375SFelipe Balbi 					return;
866550a7375SFelipe Balbi 			}
867550a7375SFelipe Balbi #endif
86892d2711fSHema Kalliguddi 			/*
86992d2711fSHema Kalliguddi 			 * Unmap the dma buffer back to cpu if dma channel
87092d2711fSHema Kalliguddi 			 * programming fails. This buffer is mapped if the
87192d2711fSHema Kalliguddi 			 * channel allocation is successful
87292d2711fSHema Kalliguddi 			 */
873c65bfa62SMian Yousaf Kaukab 			 if (is_buffer_mapped(req)) {
87492d2711fSHema Kalliguddi 				unmap_dma_buffer(req, musb);
87592d2711fSHema Kalliguddi 
876e75df371SMing Lei 				/*
877e75df371SMing Lei 				 * Clear DMAENAB and AUTOCLEAR for the
87892d2711fSHema Kalliguddi 				 * PIO mode transfer
87992d2711fSHema Kalliguddi 				 */
880e75df371SMing Lei 				csr &= ~(MUSB_RXCSR_DMAENAB | MUSB_RXCSR_AUTOCLEAR);
88192d2711fSHema Kalliguddi 				musb_writew(epio, MUSB_RXCSR, csr);
88292d2711fSHema Kalliguddi 			}
883550a7375SFelipe Balbi 
884550a7375SFelipe Balbi 			musb_read_fifo(musb_ep->hw_ep, fifo_count, (u8 *)
885550a7375SFelipe Balbi 					(request->buf + request->actual));
886550a7375SFelipe Balbi 			request->actual += fifo_count;
887550a7375SFelipe Balbi 
888550a7375SFelipe Balbi 			/* REVISIT if we left anything in the fifo, flush
889550a7375SFelipe Balbi 			 * it and report -EOVERFLOW
890550a7375SFelipe Balbi 			 */
891550a7375SFelipe Balbi 
892550a7375SFelipe Balbi 			/* ack the read! */
893550a7375SFelipe Balbi 			csr |= MUSB_RXCSR_P_WZC_BITS;
894550a7375SFelipe Balbi 			csr &= ~MUSB_RXCSR_RXPKTRDY;
895550a7375SFelipe Balbi 			musb_writew(epio, MUSB_RXCSR, csr);
896550a7375SFelipe Balbi 		}
897550a7375SFelipe Balbi 	}
898550a7375SFelipe Balbi 
899550a7375SFelipe Balbi 	/* reach the end or short packet detected */
900f0443afdSSergei Shtylyov 	if (request->actual == request->length ||
901f0443afdSSergei Shtylyov 	    fifo_count < musb_ep->packet_sz)
902550a7375SFelipe Balbi 		musb_g_giveback(musb_ep, request, 0);
903550a7375SFelipe Balbi }
904550a7375SFelipe Balbi 
905550a7375SFelipe Balbi /*
906550a7375SFelipe Balbi  * Data ready for a request; called from IRQ
907550a7375SFelipe Balbi  */
908550a7375SFelipe Balbi void musb_g_rx(struct musb *musb, u8 epnum)
909550a7375SFelipe Balbi {
910550a7375SFelipe Balbi 	u16			csr;
911ad1adb89SFelipe Balbi 	struct musb_request	*req;
912550a7375SFelipe Balbi 	struct usb_request	*request;
913550a7375SFelipe Balbi 	void __iomem		*mbase = musb->mregs;
914bd2e74d6SMing Lei 	struct musb_ep		*musb_ep;
915550a7375SFelipe Balbi 	void __iomem		*epio = musb->endpoints[epnum].regs;
916550a7375SFelipe Balbi 	struct dma_channel	*dma;
917bd2e74d6SMing Lei 	struct musb_hw_ep	*hw_ep = &musb->endpoints[epnum];
918bd2e74d6SMing Lei 
919bd2e74d6SMing Lei 	if (hw_ep->is_shared_fifo)
920bd2e74d6SMing Lei 		musb_ep = &hw_ep->ep_in;
921bd2e74d6SMing Lei 	else
922bd2e74d6SMing Lei 		musb_ep = &hw_ep->ep_out;
923550a7375SFelipe Balbi 
924550a7375SFelipe Balbi 	musb_ep_select(mbase, epnum);
925550a7375SFelipe Balbi 
926ad1adb89SFelipe Balbi 	req = next_request(musb_ep);
927ad1adb89SFelipe Balbi 	if (!req)
9280abdc36fSMaulik Mankad 		return;
929550a7375SFelipe Balbi 
930ad1adb89SFelipe Balbi 	request = &req->request;
931ad1adb89SFelipe Balbi 
932550a7375SFelipe Balbi 	csr = musb_readw(epio, MUSB_RXCSR);
933550a7375SFelipe Balbi 	dma = is_dma_capable() ? musb_ep->dma : NULL;
934550a7375SFelipe Balbi 
9355c8a86e1SFelipe Balbi 	dev_dbg(musb->controller, "<== %s, rxcsr %04x%s %p\n", musb_ep->end_point.name,
936550a7375SFelipe Balbi 			csr, dma ? " (dma)" : "", request);
937550a7375SFelipe Balbi 
938550a7375SFelipe Balbi 	if (csr & MUSB_RXCSR_P_SENTSTALL) {
939550a7375SFelipe Balbi 		csr |= MUSB_RXCSR_P_WZC_BITS;
940550a7375SFelipe Balbi 		csr &= ~MUSB_RXCSR_P_SENTSTALL;
941550a7375SFelipe Balbi 		musb_writew(epio, MUSB_RXCSR, csr);
942cea83241SSergei Shtylyov 		return;
943550a7375SFelipe Balbi 	}
944550a7375SFelipe Balbi 
945550a7375SFelipe Balbi 	if (csr & MUSB_RXCSR_P_OVERRUN) {
946550a7375SFelipe Balbi 		/* csr |= MUSB_RXCSR_P_WZC_BITS; */
947550a7375SFelipe Balbi 		csr &= ~MUSB_RXCSR_P_OVERRUN;
948550a7375SFelipe Balbi 		musb_writew(epio, MUSB_RXCSR, csr);
949550a7375SFelipe Balbi 
9505c8a86e1SFelipe Balbi 		dev_dbg(musb->controller, "%s iso overrun on %p\n", musb_ep->name, request);
95143467868SSergei Shtylyov 		if (request->status == -EINPROGRESS)
952550a7375SFelipe Balbi 			request->status = -EOVERFLOW;
953550a7375SFelipe Balbi 	}
954550a7375SFelipe Balbi 	if (csr & MUSB_RXCSR_INCOMPRX) {
955550a7375SFelipe Balbi 		/* REVISIT not necessarily an error */
9565c8a86e1SFelipe Balbi 		dev_dbg(musb->controller, "%s, incomprx\n", musb_ep->end_point.name);
957550a7375SFelipe Balbi 	}
958550a7375SFelipe Balbi 
959550a7375SFelipe Balbi 	if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) {
960550a7375SFelipe Balbi 		/* "should not happen"; likely RXPKTRDY pending for DMA */
9615c8a86e1SFelipe Balbi 		dev_dbg(musb->controller, "%s busy, csr %04x\n",
962550a7375SFelipe Balbi 			musb_ep->end_point.name, csr);
963cea83241SSergei Shtylyov 		return;
964550a7375SFelipe Balbi 	}
965550a7375SFelipe Balbi 
966550a7375SFelipe Balbi 	if (dma && (csr & MUSB_RXCSR_DMAENAB)) {
967550a7375SFelipe Balbi 		csr &= ~(MUSB_RXCSR_AUTOCLEAR
968550a7375SFelipe Balbi 				| MUSB_RXCSR_DMAENAB
969550a7375SFelipe Balbi 				| MUSB_RXCSR_DMAMODE);
970550a7375SFelipe Balbi 		musb_writew(epio, MUSB_RXCSR,
971550a7375SFelipe Balbi 			MUSB_RXCSR_P_WZC_BITS | csr);
972550a7375SFelipe Balbi 
973550a7375SFelipe Balbi 		request->actual += musb_ep->dma->actual_len;
974550a7375SFelipe Balbi 
9755c8a86e1SFelipe Balbi 		dev_dbg(musb->controller, "RXCSR%d %04x, dma off, %04x, len %zu, req %p\n",
976550a7375SFelipe Balbi 			epnum, csr,
977550a7375SFelipe Balbi 			musb_readw(epio, MUSB_RXCSR),
978550a7375SFelipe Balbi 			musb_ep->dma->actual_len, request);
979550a7375SFelipe Balbi 
980a48ff906SMian Yousaf Kaukab #if defined(CONFIG_USB_INVENTRA_DMA) || defined(CONFIG_USB_TUSB_OMAP_DMA) || \
981a48ff906SMian Yousaf Kaukab 	defined(CONFIG_USB_UX500_DMA)
982550a7375SFelipe Balbi 		/* Autoclear doesn't clear RxPktRdy for short packets */
9839001d80dSMing Lei 		if ((dma->desired_mode == 0 && !hw_ep->rx_double_buffered)
984550a7375SFelipe Balbi 				|| (dma->actual_len
985550a7375SFelipe Balbi 					& (musb_ep->packet_sz - 1))) {
986550a7375SFelipe Balbi 			/* ack the read! */
987550a7375SFelipe Balbi 			csr &= ~MUSB_RXCSR_RXPKTRDY;
988550a7375SFelipe Balbi 			musb_writew(epio, MUSB_RXCSR, csr);
989550a7375SFelipe Balbi 		}
990550a7375SFelipe Balbi 
991550a7375SFelipe Balbi 		/* incomplete, and not short? wait for next IN packet */
992550a7375SFelipe Balbi 		if ((request->actual < request->length)
993550a7375SFelipe Balbi 				&& (musb_ep->dma->actual_len
9949001d80dSMing Lei 					== musb_ep->packet_sz)) {
9959001d80dSMing Lei 			/* In double buffer case, continue to unload fifo if
9969001d80dSMing Lei  			 * there is Rx packet in FIFO.
9979001d80dSMing Lei  			 **/
9989001d80dSMing Lei 			csr = musb_readw(epio, MUSB_RXCSR);
9999001d80dSMing Lei 			if ((csr & MUSB_RXCSR_RXPKTRDY) &&
10009001d80dSMing Lei 				hw_ep->rx_double_buffered)
10019001d80dSMing Lei 				goto exit;
1002cea83241SSergei Shtylyov 			return;
10039001d80dSMing Lei 		}
1004550a7375SFelipe Balbi #endif
1005550a7375SFelipe Balbi 		musb_g_giveback(musb_ep, request, 0);
100639287076SSupriya Karanth 		/*
100739287076SSupriya Karanth 		 * In the giveback function the MUSB lock is
100839287076SSupriya Karanth 		 * released and acquired after sometime. During
100939287076SSupriya Karanth 		 * this time period the INDEX register could get
101039287076SSupriya Karanth 		 * changed by the gadget_queue function especially
101139287076SSupriya Karanth 		 * on SMP systems. Reselect the INDEX to be sure
101239287076SSupriya Karanth 		 * we are reading/modifying the right registers
101339287076SSupriya Karanth 		 */
101439287076SSupriya Karanth 		musb_ep_select(mbase, epnum);
1015550a7375SFelipe Balbi 
1016ad1adb89SFelipe Balbi 		req = next_request(musb_ep);
1017ad1adb89SFelipe Balbi 		if (!req)
1018cea83241SSergei Shtylyov 			return;
1019550a7375SFelipe Balbi 	}
1020a48ff906SMian Yousaf Kaukab #if defined(CONFIG_USB_INVENTRA_DMA) || defined(CONFIG_USB_TUSB_OMAP_DMA) || \
1021a48ff906SMian Yousaf Kaukab 	defined(CONFIG_USB_UX500_DMA)
10229001d80dSMing Lei exit:
1023bb324b08SAjay Kumar Gupta #endif
102443467868SSergei Shtylyov 	/* Analyze request */
1025ad1adb89SFelipe Balbi 	rxstate(musb, req);
1026550a7375SFelipe Balbi }
1027550a7375SFelipe Balbi 
1028550a7375SFelipe Balbi /* ------------------------------------------------------------ */
1029550a7375SFelipe Balbi 
1030550a7375SFelipe Balbi static int musb_gadget_enable(struct usb_ep *ep,
1031550a7375SFelipe Balbi 			const struct usb_endpoint_descriptor *desc)
1032550a7375SFelipe Balbi {
1033550a7375SFelipe Balbi 	unsigned long		flags;
1034550a7375SFelipe Balbi 	struct musb_ep		*musb_ep;
1035550a7375SFelipe Balbi 	struct musb_hw_ep	*hw_ep;
1036550a7375SFelipe Balbi 	void __iomem		*regs;
1037550a7375SFelipe Balbi 	struct musb		*musb;
1038550a7375SFelipe Balbi 	void __iomem	*mbase;
1039550a7375SFelipe Balbi 	u8		epnum;
1040550a7375SFelipe Balbi 	u16		csr;
1041550a7375SFelipe Balbi 	unsigned	tmp;
1042550a7375SFelipe Balbi 	int		status = -EINVAL;
1043550a7375SFelipe Balbi 
1044550a7375SFelipe Balbi 	if (!ep || !desc)
1045550a7375SFelipe Balbi 		return -EINVAL;
1046550a7375SFelipe Balbi 
1047550a7375SFelipe Balbi 	musb_ep = to_musb_ep(ep);
1048550a7375SFelipe Balbi 	hw_ep = musb_ep->hw_ep;
1049550a7375SFelipe Balbi 	regs = hw_ep->regs;
1050550a7375SFelipe Balbi 	musb = musb_ep->musb;
1051550a7375SFelipe Balbi 	mbase = musb->mregs;
1052550a7375SFelipe Balbi 	epnum = musb_ep->current_epnum;
1053550a7375SFelipe Balbi 
1054550a7375SFelipe Balbi 	spin_lock_irqsave(&musb->lock, flags);
1055550a7375SFelipe Balbi 
1056550a7375SFelipe Balbi 	if (musb_ep->desc) {
1057550a7375SFelipe Balbi 		status = -EBUSY;
1058550a7375SFelipe Balbi 		goto fail;
1059550a7375SFelipe Balbi 	}
106096bcd090SJulia Lawall 	musb_ep->type = usb_endpoint_type(desc);
1061550a7375SFelipe Balbi 
1062550a7375SFelipe Balbi 	/* check direction and (later) maxpacket size against endpoint */
106396bcd090SJulia Lawall 	if (usb_endpoint_num(desc) != epnum)
1064550a7375SFelipe Balbi 		goto fail;
1065550a7375SFelipe Balbi 
1066550a7375SFelipe Balbi 	/* REVISIT this rules out high bandwidth periodic transfers */
106729cc8897SKuninori Morimoto 	tmp = usb_endpoint_maxp(desc);
1068f11d893dSMing Lei 	if (tmp & ~0x07ff) {
1069f11d893dSMing Lei 		int ok;
1070f11d893dSMing Lei 
1071f11d893dSMing Lei 		if (usb_endpoint_dir_in(desc))
1072f11d893dSMing Lei 			ok = musb->hb_iso_tx;
1073f11d893dSMing Lei 		else
1074f11d893dSMing Lei 			ok = musb->hb_iso_rx;
1075f11d893dSMing Lei 
1076f11d893dSMing Lei 		if (!ok) {
10775c8a86e1SFelipe Balbi 			dev_dbg(musb->controller, "no support for high bandwidth ISO\n");
1078550a7375SFelipe Balbi 			goto fail;
1079f11d893dSMing Lei 		}
1080f11d893dSMing Lei 		musb_ep->hb_mult = (tmp >> 11) & 3;
1081f11d893dSMing Lei 	} else {
1082f11d893dSMing Lei 		musb_ep->hb_mult = 0;
1083f11d893dSMing Lei 	}
1084f11d893dSMing Lei 
1085f11d893dSMing Lei 	musb_ep->packet_sz = tmp & 0x7ff;
1086f11d893dSMing Lei 	tmp = musb_ep->packet_sz * (musb_ep->hb_mult + 1);
1087550a7375SFelipe Balbi 
1088550a7375SFelipe Balbi 	/* enable the interrupts for the endpoint, set the endpoint
1089550a7375SFelipe Balbi 	 * packet size (or fail), set the mode, clear the fifo
1090550a7375SFelipe Balbi 	 */
1091550a7375SFelipe Balbi 	musb_ep_select(mbase, epnum);
109296bcd090SJulia Lawall 	if (usb_endpoint_dir_in(desc)) {
1093550a7375SFelipe Balbi 
1094550a7375SFelipe Balbi 		if (hw_ep->is_shared_fifo)
1095550a7375SFelipe Balbi 			musb_ep->is_in = 1;
1096550a7375SFelipe Balbi 		if (!musb_ep->is_in)
1097550a7375SFelipe Balbi 			goto fail;
1098f11d893dSMing Lei 
1099f11d893dSMing Lei 		if (tmp > hw_ep->max_packet_sz_tx) {
11005c8a86e1SFelipe Balbi 			dev_dbg(musb->controller, "packet size beyond hardware FIFO size\n");
1101550a7375SFelipe Balbi 			goto fail;
1102f11d893dSMing Lei 		}
1103550a7375SFelipe Balbi 
1104b18d26f6SSebastian Andrzej Siewior 		musb->intrtxe |= (1 << epnum);
1105b18d26f6SSebastian Andrzej Siewior 		musb_writew(mbase, MUSB_INTRTXE, musb->intrtxe);
1106550a7375SFelipe Balbi 
1107550a7375SFelipe Balbi 		/* REVISIT if can_bulk_split(), use by updating "tmp";
1108550a7375SFelipe Balbi 		 * likewise high bandwidth periodic tx
1109550a7375SFelipe Balbi 		 */
11109f445cb2SCliff Cai 		/* Set TXMAXP with the FIFO size of the endpoint
111131c9909bSMing Lei 		 * to disable double buffering mode.
11129f445cb2SCliff Cai 		 */
111306624818SFelipe Balbi 		if (musb->double_buffer_not_ok)
111406624818SFelipe Balbi 			musb_writew(regs, MUSB_TXMAXP, hw_ep->max_packet_sz_tx);
111506624818SFelipe Balbi 		else
111606624818SFelipe Balbi 			musb_writew(regs, MUSB_TXMAXP, musb_ep->packet_sz
111706624818SFelipe Balbi 					| (musb_ep->hb_mult << 11));
1118550a7375SFelipe Balbi 
1119550a7375SFelipe Balbi 		csr = MUSB_TXCSR_MODE | MUSB_TXCSR_CLRDATATOG;
1120550a7375SFelipe Balbi 		if (musb_readw(regs, MUSB_TXCSR)
1121550a7375SFelipe Balbi 				& MUSB_TXCSR_FIFONOTEMPTY)
1122550a7375SFelipe Balbi 			csr |= MUSB_TXCSR_FLUSHFIFO;
1123550a7375SFelipe Balbi 		if (musb_ep->type == USB_ENDPOINT_XFER_ISOC)
1124550a7375SFelipe Balbi 			csr |= MUSB_TXCSR_P_ISO;
1125550a7375SFelipe Balbi 
1126550a7375SFelipe Balbi 		/* set twice in case of double buffering */
1127550a7375SFelipe Balbi 		musb_writew(regs, MUSB_TXCSR, csr);
1128550a7375SFelipe Balbi 		/* REVISIT may be inappropriate w/o FIFONOTEMPTY ... */
1129550a7375SFelipe Balbi 		musb_writew(regs, MUSB_TXCSR, csr);
1130550a7375SFelipe Balbi 
1131550a7375SFelipe Balbi 	} else {
1132550a7375SFelipe Balbi 
1133550a7375SFelipe Balbi 		if (hw_ep->is_shared_fifo)
1134550a7375SFelipe Balbi 			musb_ep->is_in = 0;
1135550a7375SFelipe Balbi 		if (musb_ep->is_in)
1136550a7375SFelipe Balbi 			goto fail;
1137f11d893dSMing Lei 
1138f11d893dSMing Lei 		if (tmp > hw_ep->max_packet_sz_rx) {
11395c8a86e1SFelipe Balbi 			dev_dbg(musb->controller, "packet size beyond hardware FIFO size\n");
1140550a7375SFelipe Balbi 			goto fail;
1141f11d893dSMing Lei 		}
1142550a7375SFelipe Balbi 
1143af5ec14dSSebastian Andrzej Siewior 		musb->intrrxe |= (1 << epnum);
1144af5ec14dSSebastian Andrzej Siewior 		musb_writew(mbase, MUSB_INTRRXE, musb->intrrxe);
1145550a7375SFelipe Balbi 
1146550a7375SFelipe Balbi 		/* REVISIT if can_bulk_combine() use by updating "tmp"
1147550a7375SFelipe Balbi 		 * likewise high bandwidth periodic rx
1148550a7375SFelipe Balbi 		 */
11499f445cb2SCliff Cai 		/* Set RXMAXP with the FIFO size of the endpoint
11509f445cb2SCliff Cai 		 * to disable double buffering mode.
11519f445cb2SCliff Cai 		 */
115206624818SFelipe Balbi 		if (musb->double_buffer_not_ok)
115306624818SFelipe Balbi 			musb_writew(regs, MUSB_RXMAXP, hw_ep->max_packet_sz_tx);
115406624818SFelipe Balbi 		else
115506624818SFelipe Balbi 			musb_writew(regs, MUSB_RXMAXP, musb_ep->packet_sz
115606624818SFelipe Balbi 					| (musb_ep->hb_mult << 11));
1157550a7375SFelipe Balbi 
1158550a7375SFelipe Balbi 		/* force shared fifo to OUT-only mode */
1159550a7375SFelipe Balbi 		if (hw_ep->is_shared_fifo) {
1160550a7375SFelipe Balbi 			csr = musb_readw(regs, MUSB_TXCSR);
1161550a7375SFelipe Balbi 			csr &= ~(MUSB_TXCSR_MODE | MUSB_TXCSR_TXPKTRDY);
1162550a7375SFelipe Balbi 			musb_writew(regs, MUSB_TXCSR, csr);
1163550a7375SFelipe Balbi 		}
1164550a7375SFelipe Balbi 
1165550a7375SFelipe Balbi 		csr = MUSB_RXCSR_FLUSHFIFO | MUSB_RXCSR_CLRDATATOG;
1166550a7375SFelipe Balbi 		if (musb_ep->type == USB_ENDPOINT_XFER_ISOC)
1167550a7375SFelipe Balbi 			csr |= MUSB_RXCSR_P_ISO;
1168550a7375SFelipe Balbi 		else if (musb_ep->type == USB_ENDPOINT_XFER_INT)
1169550a7375SFelipe Balbi 			csr |= MUSB_RXCSR_DISNYET;
1170550a7375SFelipe Balbi 
1171550a7375SFelipe Balbi 		/* set twice in case of double buffering */
1172550a7375SFelipe Balbi 		musb_writew(regs, MUSB_RXCSR, csr);
1173550a7375SFelipe Balbi 		musb_writew(regs, MUSB_RXCSR, csr);
1174550a7375SFelipe Balbi 	}
1175550a7375SFelipe Balbi 
1176550a7375SFelipe Balbi 	/* NOTE:  all the I/O code _should_ work fine without DMA, in case
1177550a7375SFelipe Balbi 	 * for some reason you run out of channels here.
1178550a7375SFelipe Balbi 	 */
1179550a7375SFelipe Balbi 	if (is_dma_capable() && musb->dma_controller) {
1180550a7375SFelipe Balbi 		struct dma_controller	*c = musb->dma_controller;
1181550a7375SFelipe Balbi 
1182550a7375SFelipe Balbi 		musb_ep->dma = c->channel_alloc(c, hw_ep,
1183550a7375SFelipe Balbi 				(desc->bEndpointAddress & USB_DIR_IN));
1184550a7375SFelipe Balbi 	} else
1185550a7375SFelipe Balbi 		musb_ep->dma = NULL;
1186550a7375SFelipe Balbi 
1187550a7375SFelipe Balbi 	musb_ep->desc = desc;
1188550a7375SFelipe Balbi 	musb_ep->busy = 0;
118947e97605SSergei Shtylyov 	musb_ep->wedged = 0;
1190550a7375SFelipe Balbi 	status = 0;
1191550a7375SFelipe Balbi 
1192550a7375SFelipe Balbi 	pr_debug("%s periph: enabled %s for %s %s, %smaxpacket %d\n",
1193550a7375SFelipe Balbi 			musb_driver_name, musb_ep->end_point.name,
1194550a7375SFelipe Balbi 			({ char *s; switch (musb_ep->type) {
1195550a7375SFelipe Balbi 			case USB_ENDPOINT_XFER_BULK:	s = "bulk"; break;
1196550a7375SFelipe Balbi 			case USB_ENDPOINT_XFER_INT:	s = "int"; break;
1197550a7375SFelipe Balbi 			default:			s = "iso"; break;
1198550a7375SFelipe Balbi 			}; s; }),
1199550a7375SFelipe Balbi 			musb_ep->is_in ? "IN" : "OUT",
1200550a7375SFelipe Balbi 			musb_ep->dma ? "dma, " : "",
1201550a7375SFelipe Balbi 			musb_ep->packet_sz);
1202550a7375SFelipe Balbi 
1203550a7375SFelipe Balbi 	schedule_work(&musb->irq_work);
1204550a7375SFelipe Balbi 
1205550a7375SFelipe Balbi fail:
1206550a7375SFelipe Balbi 	spin_unlock_irqrestore(&musb->lock, flags);
1207550a7375SFelipe Balbi 	return status;
1208550a7375SFelipe Balbi }
1209550a7375SFelipe Balbi 
1210550a7375SFelipe Balbi /*
1211550a7375SFelipe Balbi  * Disable an endpoint flushing all requests queued.
1212550a7375SFelipe Balbi  */
1213550a7375SFelipe Balbi static int musb_gadget_disable(struct usb_ep *ep)
1214550a7375SFelipe Balbi {
1215550a7375SFelipe Balbi 	unsigned long	flags;
1216550a7375SFelipe Balbi 	struct musb	*musb;
1217550a7375SFelipe Balbi 	u8		epnum;
1218550a7375SFelipe Balbi 	struct musb_ep	*musb_ep;
1219550a7375SFelipe Balbi 	void __iomem	*epio;
1220550a7375SFelipe Balbi 	int		status = 0;
1221550a7375SFelipe Balbi 
1222550a7375SFelipe Balbi 	musb_ep = to_musb_ep(ep);
1223550a7375SFelipe Balbi 	musb = musb_ep->musb;
1224550a7375SFelipe Balbi 	epnum = musb_ep->current_epnum;
1225550a7375SFelipe Balbi 	epio = musb->endpoints[epnum].regs;
1226550a7375SFelipe Balbi 
1227550a7375SFelipe Balbi 	spin_lock_irqsave(&musb->lock, flags);
1228550a7375SFelipe Balbi 	musb_ep_select(musb->mregs, epnum);
1229550a7375SFelipe Balbi 
1230550a7375SFelipe Balbi 	/* zero the endpoint sizes */
1231550a7375SFelipe Balbi 	if (musb_ep->is_in) {
1232b18d26f6SSebastian Andrzej Siewior 		musb->intrtxe &= ~(1 << epnum);
1233b18d26f6SSebastian Andrzej Siewior 		musb_writew(musb->mregs, MUSB_INTRTXE, musb->intrtxe);
1234550a7375SFelipe Balbi 		musb_writew(epio, MUSB_TXMAXP, 0);
1235550a7375SFelipe Balbi 	} else {
1236af5ec14dSSebastian Andrzej Siewior 		musb->intrrxe &= ~(1 << epnum);
1237af5ec14dSSebastian Andrzej Siewior 		musb_writew(musb->mregs, MUSB_INTRRXE, musb->intrrxe);
1238550a7375SFelipe Balbi 		musb_writew(epio, MUSB_RXMAXP, 0);
1239550a7375SFelipe Balbi 	}
1240550a7375SFelipe Balbi 
1241550a7375SFelipe Balbi 	musb_ep->desc = NULL;
124208f75bf1SGrazvydas Ignotas 	musb_ep->end_point.desc = NULL;
1243550a7375SFelipe Balbi 
1244550a7375SFelipe Balbi 	/* abort all pending DMA and requests */
1245550a7375SFelipe Balbi 	nuke(musb_ep, -ESHUTDOWN);
1246550a7375SFelipe Balbi 
1247550a7375SFelipe Balbi 	schedule_work(&musb->irq_work);
1248550a7375SFelipe Balbi 
1249550a7375SFelipe Balbi 	spin_unlock_irqrestore(&(musb->lock), flags);
1250550a7375SFelipe Balbi 
12515c8a86e1SFelipe Balbi 	dev_dbg(musb->controller, "%s\n", musb_ep->end_point.name);
1252550a7375SFelipe Balbi 
1253550a7375SFelipe Balbi 	return status;
1254550a7375SFelipe Balbi }
1255550a7375SFelipe Balbi 
1256550a7375SFelipe Balbi /*
1257550a7375SFelipe Balbi  * Allocate a request for an endpoint.
1258550a7375SFelipe Balbi  * Reused by ep0 code.
1259550a7375SFelipe Balbi  */
1260550a7375SFelipe Balbi struct usb_request *musb_alloc_request(struct usb_ep *ep, gfp_t gfp_flags)
1261550a7375SFelipe Balbi {
1262550a7375SFelipe Balbi 	struct musb_ep		*musb_ep = to_musb_ep(ep);
12635c8a86e1SFelipe Balbi 	struct musb		*musb = musb_ep->musb;
1264550a7375SFelipe Balbi 	struct musb_request	*request = NULL;
1265550a7375SFelipe Balbi 
1266550a7375SFelipe Balbi 	request = kzalloc(sizeof *request, gfp_flags);
12670607f862SFelipe Balbi 	if (!request) {
12685c8a86e1SFelipe Balbi 		dev_dbg(musb->controller, "not enough memory\n");
12690607f862SFelipe Balbi 		return NULL;
12700607f862SFelipe Balbi 	}
12710607f862SFelipe Balbi 
1272550a7375SFelipe Balbi 	request->request.dma = DMA_ADDR_INVALID;
1273550a7375SFelipe Balbi 	request->epnum = musb_ep->current_epnum;
1274550a7375SFelipe Balbi 	request->ep = musb_ep;
1275550a7375SFelipe Balbi 
1276550a7375SFelipe Balbi 	return &request->request;
1277550a7375SFelipe Balbi }
1278550a7375SFelipe Balbi 
1279550a7375SFelipe Balbi /*
1280550a7375SFelipe Balbi  * Free a request
1281550a7375SFelipe Balbi  * Reused by ep0 code.
1282550a7375SFelipe Balbi  */
1283550a7375SFelipe Balbi void musb_free_request(struct usb_ep *ep, struct usb_request *req)
1284550a7375SFelipe Balbi {
1285550a7375SFelipe Balbi 	kfree(to_musb_request(req));
1286550a7375SFelipe Balbi }
1287550a7375SFelipe Balbi 
1288550a7375SFelipe Balbi static LIST_HEAD(buffers);
1289550a7375SFelipe Balbi 
1290550a7375SFelipe Balbi struct free_record {
1291550a7375SFelipe Balbi 	struct list_head	list;
1292550a7375SFelipe Balbi 	struct device		*dev;
1293550a7375SFelipe Balbi 	unsigned		bytes;
1294550a7375SFelipe Balbi 	dma_addr_t		dma;
1295550a7375SFelipe Balbi };
1296550a7375SFelipe Balbi 
1297550a7375SFelipe Balbi /*
1298550a7375SFelipe Balbi  * Context: controller locked, IRQs blocked.
1299550a7375SFelipe Balbi  */
1300a666e3e6SSergei Shtylyov void musb_ep_restart(struct musb *musb, struct musb_request *req)
1301550a7375SFelipe Balbi {
13025c8a86e1SFelipe Balbi 	dev_dbg(musb->controller, "<== %s request %p len %u on hw_ep%d\n",
1303550a7375SFelipe Balbi 		req->tx ? "TX/IN" : "RX/OUT",
1304550a7375SFelipe Balbi 		&req->request, req->request.length, req->epnum);
1305550a7375SFelipe Balbi 
1306550a7375SFelipe Balbi 	musb_ep_select(musb->mregs, req->epnum);
1307550a7375SFelipe Balbi 	if (req->tx)
1308550a7375SFelipe Balbi 		txstate(musb, req);
1309550a7375SFelipe Balbi 	else
1310550a7375SFelipe Balbi 		rxstate(musb, req);
1311550a7375SFelipe Balbi }
1312550a7375SFelipe Balbi 
1313550a7375SFelipe Balbi static int musb_gadget_queue(struct usb_ep *ep, struct usb_request *req,
1314550a7375SFelipe Balbi 			gfp_t gfp_flags)
1315550a7375SFelipe Balbi {
1316550a7375SFelipe Balbi 	struct musb_ep		*musb_ep;
1317550a7375SFelipe Balbi 	struct musb_request	*request;
1318550a7375SFelipe Balbi 	struct musb		*musb;
1319550a7375SFelipe Balbi 	int			status = 0;
1320550a7375SFelipe Balbi 	unsigned long		lockflags;
1321550a7375SFelipe Balbi 
1322550a7375SFelipe Balbi 	if (!ep || !req)
1323550a7375SFelipe Balbi 		return -EINVAL;
1324550a7375SFelipe Balbi 	if (!req->buf)
1325550a7375SFelipe Balbi 		return -ENODATA;
1326550a7375SFelipe Balbi 
1327550a7375SFelipe Balbi 	musb_ep = to_musb_ep(ep);
1328550a7375SFelipe Balbi 	musb = musb_ep->musb;
1329550a7375SFelipe Balbi 
1330550a7375SFelipe Balbi 	request = to_musb_request(req);
1331550a7375SFelipe Balbi 	request->musb = musb;
1332550a7375SFelipe Balbi 
1333550a7375SFelipe Balbi 	if (request->ep != musb_ep)
1334550a7375SFelipe Balbi 		return -EINVAL;
1335550a7375SFelipe Balbi 
13365c8a86e1SFelipe Balbi 	dev_dbg(musb->controller, "<== to %s request=%p\n", ep->name, req);
1337550a7375SFelipe Balbi 
1338550a7375SFelipe Balbi 	/* request is mine now... */
1339550a7375SFelipe Balbi 	request->request.actual = 0;
1340550a7375SFelipe Balbi 	request->request.status = -EINPROGRESS;
1341550a7375SFelipe Balbi 	request->epnum = musb_ep->current_epnum;
1342550a7375SFelipe Balbi 	request->tx = musb_ep->is_in;
1343550a7375SFelipe Balbi 
1344c65bfa62SMian Yousaf Kaukab 	map_dma_buffer(request, musb, musb_ep);
1345550a7375SFelipe Balbi 
1346550a7375SFelipe Balbi 	spin_lock_irqsave(&musb->lock, lockflags);
1347550a7375SFelipe Balbi 
1348550a7375SFelipe Balbi 	/* don't queue if the ep is down */
1349550a7375SFelipe Balbi 	if (!musb_ep->desc) {
13505c8a86e1SFelipe Balbi 		dev_dbg(musb->controller, "req %p queued to %s while ep %s\n",
1351550a7375SFelipe Balbi 				req, ep->name, "disabled");
1352550a7375SFelipe Balbi 		status = -ESHUTDOWN;
1353550a7375SFelipe Balbi 		goto cleanup;
1354550a7375SFelipe Balbi 	}
1355550a7375SFelipe Balbi 
1356550a7375SFelipe Balbi 	/* add request to the list */
1357ad1adb89SFelipe Balbi 	list_add_tail(&request->list, &musb_ep->req_list);
1358550a7375SFelipe Balbi 
1359550a7375SFelipe Balbi 	/* it this is the head of the queue, start i/o ... */
1360ad1adb89SFelipe Balbi 	if (!musb_ep->busy && &request->list == musb_ep->req_list.next)
1361550a7375SFelipe Balbi 		musb_ep_restart(musb, request);
1362550a7375SFelipe Balbi 
1363550a7375SFelipe Balbi cleanup:
1364550a7375SFelipe Balbi 	spin_unlock_irqrestore(&musb->lock, lockflags);
1365550a7375SFelipe Balbi 	return status;
1366550a7375SFelipe Balbi }
1367550a7375SFelipe Balbi 
1368550a7375SFelipe Balbi static int musb_gadget_dequeue(struct usb_ep *ep, struct usb_request *request)
1369550a7375SFelipe Balbi {
1370550a7375SFelipe Balbi 	struct musb_ep		*musb_ep = to_musb_ep(ep);
13714cbbf084SFelipe Balbi 	struct musb_request	*req = to_musb_request(request);
13724cbbf084SFelipe Balbi 	struct musb_request	*r;
1373550a7375SFelipe Balbi 	unsigned long		flags;
1374550a7375SFelipe Balbi 	int			status = 0;
1375550a7375SFelipe Balbi 	struct musb		*musb = musb_ep->musb;
1376550a7375SFelipe Balbi 
1377550a7375SFelipe Balbi 	if (!ep || !request || to_musb_request(request)->ep != musb_ep)
1378550a7375SFelipe Balbi 		return -EINVAL;
1379550a7375SFelipe Balbi 
1380550a7375SFelipe Balbi 	spin_lock_irqsave(&musb->lock, flags);
1381550a7375SFelipe Balbi 
1382550a7375SFelipe Balbi 	list_for_each_entry(r, &musb_ep->req_list, list) {
13834cbbf084SFelipe Balbi 		if (r == req)
1384550a7375SFelipe Balbi 			break;
1385550a7375SFelipe Balbi 	}
13864cbbf084SFelipe Balbi 	if (r != req) {
13875c8a86e1SFelipe Balbi 		dev_dbg(musb->controller, "request %p not queued to %s\n", request, ep->name);
1388550a7375SFelipe Balbi 		status = -EINVAL;
1389550a7375SFelipe Balbi 		goto done;
1390550a7375SFelipe Balbi 	}
1391550a7375SFelipe Balbi 
1392550a7375SFelipe Balbi 	/* if the hardware doesn't have the request, easy ... */
13933d5ad13eSFelipe Balbi 	if (musb_ep->req_list.next != &req->list || musb_ep->busy)
1394550a7375SFelipe Balbi 		musb_g_giveback(musb_ep, request, -ECONNRESET);
1395550a7375SFelipe Balbi 
1396550a7375SFelipe Balbi 	/* ... else abort the dma transfer ... */
1397550a7375SFelipe Balbi 	else if (is_dma_capable() && musb_ep->dma) {
1398550a7375SFelipe Balbi 		struct dma_controller	*c = musb->dma_controller;
1399550a7375SFelipe Balbi 
1400550a7375SFelipe Balbi 		musb_ep_select(musb->mregs, musb_ep->current_epnum);
1401550a7375SFelipe Balbi 		if (c->channel_abort)
1402550a7375SFelipe Balbi 			status = c->channel_abort(musb_ep->dma);
1403550a7375SFelipe Balbi 		else
1404550a7375SFelipe Balbi 			status = -EBUSY;
1405550a7375SFelipe Balbi 		if (status == 0)
1406550a7375SFelipe Balbi 			musb_g_giveback(musb_ep, request, -ECONNRESET);
1407550a7375SFelipe Balbi 	} else {
1408550a7375SFelipe Balbi 		/* NOTE: by sticking to easily tested hardware/driver states,
1409550a7375SFelipe Balbi 		 * we leave counting of in-flight packets imprecise.
1410550a7375SFelipe Balbi 		 */
1411550a7375SFelipe Balbi 		musb_g_giveback(musb_ep, request, -ECONNRESET);
1412550a7375SFelipe Balbi 	}
1413550a7375SFelipe Balbi 
1414550a7375SFelipe Balbi done:
1415550a7375SFelipe Balbi 	spin_unlock_irqrestore(&musb->lock, flags);
1416550a7375SFelipe Balbi 	return status;
1417550a7375SFelipe Balbi }
1418550a7375SFelipe Balbi 
1419550a7375SFelipe Balbi /*
1420550a7375SFelipe Balbi  * Set or clear the halt bit of an endpoint. A halted enpoint won't tx/rx any
1421550a7375SFelipe Balbi  * data but will queue requests.
1422550a7375SFelipe Balbi  *
1423550a7375SFelipe Balbi  * exported to ep0 code
1424550a7375SFelipe Balbi  */
14251b6c3b0fSFelipe Balbi static int musb_gadget_set_halt(struct usb_ep *ep, int value)
1426550a7375SFelipe Balbi {
1427550a7375SFelipe Balbi 	struct musb_ep		*musb_ep = to_musb_ep(ep);
1428550a7375SFelipe Balbi 	u8			epnum = musb_ep->current_epnum;
1429550a7375SFelipe Balbi 	struct musb		*musb = musb_ep->musb;
1430550a7375SFelipe Balbi 	void __iomem		*epio = musb->endpoints[epnum].regs;
1431550a7375SFelipe Balbi 	void __iomem		*mbase;
1432550a7375SFelipe Balbi 	unsigned long		flags;
1433550a7375SFelipe Balbi 	u16			csr;
1434cea83241SSergei Shtylyov 	struct musb_request	*request;
1435550a7375SFelipe Balbi 	int			status = 0;
1436550a7375SFelipe Balbi 
1437550a7375SFelipe Balbi 	if (!ep)
1438550a7375SFelipe Balbi 		return -EINVAL;
1439550a7375SFelipe Balbi 	mbase = musb->mregs;
1440550a7375SFelipe Balbi 
1441550a7375SFelipe Balbi 	spin_lock_irqsave(&musb->lock, flags);
1442550a7375SFelipe Balbi 
1443550a7375SFelipe Balbi 	if ((USB_ENDPOINT_XFER_ISOC == musb_ep->type)) {
1444550a7375SFelipe Balbi 		status = -EINVAL;
1445550a7375SFelipe Balbi 		goto done;
1446550a7375SFelipe Balbi 	}
1447550a7375SFelipe Balbi 
1448550a7375SFelipe Balbi 	musb_ep_select(mbase, epnum);
1449550a7375SFelipe Balbi 
1450ad1adb89SFelipe Balbi 	request = next_request(musb_ep);
1451cea83241SSergei Shtylyov 	if (value) {
1452cea83241SSergei Shtylyov 		if (request) {
14535c8a86e1SFelipe Balbi 			dev_dbg(musb->controller, "request in progress, cannot halt %s\n",
1454cea83241SSergei Shtylyov 			    ep->name);
1455cea83241SSergei Shtylyov 			status = -EAGAIN;
1456cea83241SSergei Shtylyov 			goto done;
1457cea83241SSergei Shtylyov 		}
1458cea83241SSergei Shtylyov 		/* Cannot portably stall with non-empty FIFO */
1459cea83241SSergei Shtylyov 		if (musb_ep->is_in) {
1460550a7375SFelipe Balbi 			csr = musb_readw(epio, MUSB_TXCSR);
1461550a7375SFelipe Balbi 			if (csr & MUSB_TXCSR_FIFONOTEMPTY) {
14625c8a86e1SFelipe Balbi 				dev_dbg(musb->controller, "FIFO busy, cannot halt %s\n", ep->name);
1463cea83241SSergei Shtylyov 				status = -EAGAIN;
1464cea83241SSergei Shtylyov 				goto done;
1465550a7375SFelipe Balbi 			}
1466cea83241SSergei Shtylyov 		}
146747e97605SSergei Shtylyov 	} else
146847e97605SSergei Shtylyov 		musb_ep->wedged = 0;
1469550a7375SFelipe Balbi 
1470550a7375SFelipe Balbi 	/* set/clear the stall and toggle bits */
14715c8a86e1SFelipe Balbi 	dev_dbg(musb->controller, "%s: %s stall\n", ep->name, value ? "set" : "clear");
1472550a7375SFelipe Balbi 	if (musb_ep->is_in) {
1473550a7375SFelipe Balbi 		csr = musb_readw(epio, MUSB_TXCSR);
1474550a7375SFelipe Balbi 		csr |= MUSB_TXCSR_P_WZC_BITS
1475550a7375SFelipe Balbi 			| MUSB_TXCSR_CLRDATATOG;
1476550a7375SFelipe Balbi 		if (value)
1477550a7375SFelipe Balbi 			csr |= MUSB_TXCSR_P_SENDSTALL;
1478550a7375SFelipe Balbi 		else
1479550a7375SFelipe Balbi 			csr &= ~(MUSB_TXCSR_P_SENDSTALL
1480550a7375SFelipe Balbi 				| MUSB_TXCSR_P_SENTSTALL);
1481550a7375SFelipe Balbi 		csr &= ~MUSB_TXCSR_TXPKTRDY;
1482550a7375SFelipe Balbi 		musb_writew(epio, MUSB_TXCSR, csr);
1483550a7375SFelipe Balbi 	} else {
1484550a7375SFelipe Balbi 		csr = musb_readw(epio, MUSB_RXCSR);
1485550a7375SFelipe Balbi 		csr |= MUSB_RXCSR_P_WZC_BITS
1486550a7375SFelipe Balbi 			| MUSB_RXCSR_FLUSHFIFO
1487550a7375SFelipe Balbi 			| MUSB_RXCSR_CLRDATATOG;
1488550a7375SFelipe Balbi 		if (value)
1489550a7375SFelipe Balbi 			csr |= MUSB_RXCSR_P_SENDSTALL;
1490550a7375SFelipe Balbi 		else
1491550a7375SFelipe Balbi 			csr &= ~(MUSB_RXCSR_P_SENDSTALL
1492550a7375SFelipe Balbi 				| MUSB_RXCSR_P_SENTSTALL);
1493550a7375SFelipe Balbi 		musb_writew(epio, MUSB_RXCSR, csr);
1494550a7375SFelipe Balbi 	}
1495550a7375SFelipe Balbi 
1496550a7375SFelipe Balbi 	/* maybe start the first request in the queue */
1497550a7375SFelipe Balbi 	if (!musb_ep->busy && !value && request) {
14985c8a86e1SFelipe Balbi 		dev_dbg(musb->controller, "restarting the request\n");
1499550a7375SFelipe Balbi 		musb_ep_restart(musb, request);
1500550a7375SFelipe Balbi 	}
1501550a7375SFelipe Balbi 
1502cea83241SSergei Shtylyov done:
1503550a7375SFelipe Balbi 	spin_unlock_irqrestore(&musb->lock, flags);
1504550a7375SFelipe Balbi 	return status;
1505550a7375SFelipe Balbi }
1506550a7375SFelipe Balbi 
150747e97605SSergei Shtylyov /*
150847e97605SSergei Shtylyov  * Sets the halt feature with the clear requests ignored
150947e97605SSergei Shtylyov  */
15101b6c3b0fSFelipe Balbi static int musb_gadget_set_wedge(struct usb_ep *ep)
151147e97605SSergei Shtylyov {
151247e97605SSergei Shtylyov 	struct musb_ep		*musb_ep = to_musb_ep(ep);
151347e97605SSergei Shtylyov 
151447e97605SSergei Shtylyov 	if (!ep)
151547e97605SSergei Shtylyov 		return -EINVAL;
151647e97605SSergei Shtylyov 
151747e97605SSergei Shtylyov 	musb_ep->wedged = 1;
151847e97605SSergei Shtylyov 
151947e97605SSergei Shtylyov 	return usb_ep_set_halt(ep);
152047e97605SSergei Shtylyov }
152147e97605SSergei Shtylyov 
1522550a7375SFelipe Balbi static int musb_gadget_fifo_status(struct usb_ep *ep)
1523550a7375SFelipe Balbi {
1524550a7375SFelipe Balbi 	struct musb_ep		*musb_ep = to_musb_ep(ep);
1525550a7375SFelipe Balbi 	void __iomem		*epio = musb_ep->hw_ep->regs;
1526550a7375SFelipe Balbi 	int			retval = -EINVAL;
1527550a7375SFelipe Balbi 
1528550a7375SFelipe Balbi 	if (musb_ep->desc && !musb_ep->is_in) {
1529550a7375SFelipe Balbi 		struct musb		*musb = musb_ep->musb;
1530550a7375SFelipe Balbi 		int			epnum = musb_ep->current_epnum;
1531550a7375SFelipe Balbi 		void __iomem		*mbase = musb->mregs;
1532550a7375SFelipe Balbi 		unsigned long		flags;
1533550a7375SFelipe Balbi 
1534550a7375SFelipe Balbi 		spin_lock_irqsave(&musb->lock, flags);
1535550a7375SFelipe Balbi 
1536550a7375SFelipe Balbi 		musb_ep_select(mbase, epnum);
1537550a7375SFelipe Balbi 		/* FIXME return zero unless RXPKTRDY is set */
1538550a7375SFelipe Balbi 		retval = musb_readw(epio, MUSB_RXCOUNT);
1539550a7375SFelipe Balbi 
1540550a7375SFelipe Balbi 		spin_unlock_irqrestore(&musb->lock, flags);
1541550a7375SFelipe Balbi 	}
1542550a7375SFelipe Balbi 	return retval;
1543550a7375SFelipe Balbi }
1544550a7375SFelipe Balbi 
1545550a7375SFelipe Balbi static void musb_gadget_fifo_flush(struct usb_ep *ep)
1546550a7375SFelipe Balbi {
1547550a7375SFelipe Balbi 	struct musb_ep	*musb_ep = to_musb_ep(ep);
1548550a7375SFelipe Balbi 	struct musb	*musb = musb_ep->musb;
1549550a7375SFelipe Balbi 	u8		epnum = musb_ep->current_epnum;
1550550a7375SFelipe Balbi 	void __iomem	*epio = musb->endpoints[epnum].regs;
1551550a7375SFelipe Balbi 	void __iomem	*mbase;
1552550a7375SFelipe Balbi 	unsigned long	flags;
1553b18d26f6SSebastian Andrzej Siewior 	u16		csr;
1554550a7375SFelipe Balbi 
1555550a7375SFelipe Balbi 	mbase = musb->mregs;
1556550a7375SFelipe Balbi 
1557550a7375SFelipe Balbi 	spin_lock_irqsave(&musb->lock, flags);
1558550a7375SFelipe Balbi 	musb_ep_select(mbase, (u8) epnum);
1559550a7375SFelipe Balbi 
1560550a7375SFelipe Balbi 	/* disable interrupts */
1561b18d26f6SSebastian Andrzej Siewior 	musb_writew(mbase, MUSB_INTRTXE, musb->intrtxe & ~(1 << epnum));
1562550a7375SFelipe Balbi 
1563550a7375SFelipe Balbi 	if (musb_ep->is_in) {
1564550a7375SFelipe Balbi 		csr = musb_readw(epio, MUSB_TXCSR);
1565550a7375SFelipe Balbi 		if (csr & MUSB_TXCSR_FIFONOTEMPTY) {
1566550a7375SFelipe Balbi 			csr |= MUSB_TXCSR_FLUSHFIFO | MUSB_TXCSR_P_WZC_BITS;
15674858f06eSYauheni Kaliuta 			/*
15684858f06eSYauheni Kaliuta 			 * Setting both TXPKTRDY and FLUSHFIFO makes controller
15694858f06eSYauheni Kaliuta 			 * to interrupt current FIFO loading, but not flushing
15704858f06eSYauheni Kaliuta 			 * the already loaded ones.
15714858f06eSYauheni Kaliuta 			 */
15724858f06eSYauheni Kaliuta 			csr &= ~MUSB_TXCSR_TXPKTRDY;
1573550a7375SFelipe Balbi 			musb_writew(epio, MUSB_TXCSR, csr);
1574550a7375SFelipe Balbi 			/* REVISIT may be inappropriate w/o FIFONOTEMPTY ... */
1575550a7375SFelipe Balbi 			musb_writew(epio, MUSB_TXCSR, csr);
1576550a7375SFelipe Balbi 		}
1577550a7375SFelipe Balbi 	} else {
1578550a7375SFelipe Balbi 		csr = musb_readw(epio, MUSB_RXCSR);
1579550a7375SFelipe Balbi 		csr |= MUSB_RXCSR_FLUSHFIFO | MUSB_RXCSR_P_WZC_BITS;
1580550a7375SFelipe Balbi 		musb_writew(epio, MUSB_RXCSR, csr);
1581550a7375SFelipe Balbi 		musb_writew(epio, MUSB_RXCSR, csr);
1582550a7375SFelipe Balbi 	}
1583550a7375SFelipe Balbi 
1584550a7375SFelipe Balbi 	/* re-enable interrupt */
1585b18d26f6SSebastian Andrzej Siewior 	musb_writew(mbase, MUSB_INTRTXE, musb->intrtxe);
1586550a7375SFelipe Balbi 	spin_unlock_irqrestore(&musb->lock, flags);
1587550a7375SFelipe Balbi }
1588550a7375SFelipe Balbi 
1589550a7375SFelipe Balbi static const struct usb_ep_ops musb_ep_ops = {
1590550a7375SFelipe Balbi 	.enable		= musb_gadget_enable,
1591550a7375SFelipe Balbi 	.disable	= musb_gadget_disable,
1592550a7375SFelipe Balbi 	.alloc_request	= musb_alloc_request,
1593550a7375SFelipe Balbi 	.free_request	= musb_free_request,
1594550a7375SFelipe Balbi 	.queue		= musb_gadget_queue,
1595550a7375SFelipe Balbi 	.dequeue	= musb_gadget_dequeue,
1596550a7375SFelipe Balbi 	.set_halt	= musb_gadget_set_halt,
159747e97605SSergei Shtylyov 	.set_wedge	= musb_gadget_set_wedge,
1598550a7375SFelipe Balbi 	.fifo_status	= musb_gadget_fifo_status,
1599550a7375SFelipe Balbi 	.fifo_flush	= musb_gadget_fifo_flush
1600550a7375SFelipe Balbi };
1601550a7375SFelipe Balbi 
1602550a7375SFelipe Balbi /* ----------------------------------------------------------------------- */
1603550a7375SFelipe Balbi 
1604550a7375SFelipe Balbi static int musb_gadget_get_frame(struct usb_gadget *gadget)
1605550a7375SFelipe Balbi {
1606550a7375SFelipe Balbi 	struct musb	*musb = gadget_to_musb(gadget);
1607550a7375SFelipe Balbi 
1608550a7375SFelipe Balbi 	return (int)musb_readw(musb->mregs, MUSB_FRAME);
1609550a7375SFelipe Balbi }
1610550a7375SFelipe Balbi 
1611550a7375SFelipe Balbi static int musb_gadget_wakeup(struct usb_gadget *gadget)
1612550a7375SFelipe Balbi {
1613550a7375SFelipe Balbi 	struct musb	*musb = gadget_to_musb(gadget);
1614550a7375SFelipe Balbi 	void __iomem	*mregs = musb->mregs;
1615550a7375SFelipe Balbi 	unsigned long	flags;
1616550a7375SFelipe Balbi 	int		status = -EINVAL;
1617550a7375SFelipe Balbi 	u8		power, devctl;
1618550a7375SFelipe Balbi 	int		retries;
1619550a7375SFelipe Balbi 
1620550a7375SFelipe Balbi 	spin_lock_irqsave(&musb->lock, flags);
1621550a7375SFelipe Balbi 
162284e250ffSDavid Brownell 	switch (musb->xceiv->state) {
1623550a7375SFelipe Balbi 	case OTG_STATE_B_PERIPHERAL:
1624550a7375SFelipe Balbi 		/* NOTE:  OTG state machine doesn't include B_SUSPENDED;
1625550a7375SFelipe Balbi 		 * that's part of the standard usb 1.1 state machine, and
1626550a7375SFelipe Balbi 		 * doesn't affect OTG transitions.
1627550a7375SFelipe Balbi 		 */
1628550a7375SFelipe Balbi 		if (musb->may_wakeup && musb->is_suspended)
1629550a7375SFelipe Balbi 			break;
1630550a7375SFelipe Balbi 		goto done;
1631550a7375SFelipe Balbi 	case OTG_STATE_B_IDLE:
1632550a7375SFelipe Balbi 		/* Start SRP ... OTG not required. */
1633550a7375SFelipe Balbi 		devctl = musb_readb(mregs, MUSB_DEVCTL);
16345c8a86e1SFelipe Balbi 		dev_dbg(musb->controller, "Sending SRP: devctl: %02x\n", devctl);
1635550a7375SFelipe Balbi 		devctl |= MUSB_DEVCTL_SESSION;
1636550a7375SFelipe Balbi 		musb_writeb(mregs, MUSB_DEVCTL, devctl);
1637550a7375SFelipe Balbi 		devctl = musb_readb(mregs, MUSB_DEVCTL);
1638550a7375SFelipe Balbi 		retries = 100;
1639550a7375SFelipe Balbi 		while (!(devctl & MUSB_DEVCTL_SESSION)) {
1640550a7375SFelipe Balbi 			devctl = musb_readb(mregs, MUSB_DEVCTL);
1641550a7375SFelipe Balbi 			if (retries-- < 1)
1642550a7375SFelipe Balbi 				break;
1643550a7375SFelipe Balbi 		}
1644550a7375SFelipe Balbi 		retries = 10000;
1645550a7375SFelipe Balbi 		while (devctl & MUSB_DEVCTL_SESSION) {
1646550a7375SFelipe Balbi 			devctl = musb_readb(mregs, MUSB_DEVCTL);
1647550a7375SFelipe Balbi 			if (retries-- < 1)
1648550a7375SFelipe Balbi 				break;
1649550a7375SFelipe Balbi 		}
1650550a7375SFelipe Balbi 
16518620543eSHema HK 		spin_unlock_irqrestore(&musb->lock, flags);
16526e13c650SHeikki Krogerus 		otg_start_srp(musb->xceiv->otg);
16538620543eSHema HK 		spin_lock_irqsave(&musb->lock, flags);
16548620543eSHema HK 
1655550a7375SFelipe Balbi 		/* Block idling for at least 1s */
1656550a7375SFelipe Balbi 		musb_platform_try_idle(musb,
1657550a7375SFelipe Balbi 			jiffies + msecs_to_jiffies(1 * HZ));
1658550a7375SFelipe Balbi 
1659550a7375SFelipe Balbi 		status = 0;
1660550a7375SFelipe Balbi 		goto done;
1661550a7375SFelipe Balbi 	default:
16625c8a86e1SFelipe Balbi 		dev_dbg(musb->controller, "Unhandled wake: %s\n",
16633df00453SAnatolij Gustschin 			otg_state_string(musb->xceiv->state));
1664550a7375SFelipe Balbi 		goto done;
1665550a7375SFelipe Balbi 	}
1666550a7375SFelipe Balbi 
1667550a7375SFelipe Balbi 	status = 0;
1668550a7375SFelipe Balbi 
1669550a7375SFelipe Balbi 	power = musb_readb(mregs, MUSB_POWER);
1670550a7375SFelipe Balbi 	power |= MUSB_POWER_RESUME;
1671550a7375SFelipe Balbi 	musb_writeb(mregs, MUSB_POWER, power);
16725c8a86e1SFelipe Balbi 	dev_dbg(musb->controller, "issue wakeup\n");
1673550a7375SFelipe Balbi 
1674550a7375SFelipe Balbi 	/* FIXME do this next chunk in a timer callback, no udelay */
1675550a7375SFelipe Balbi 	mdelay(2);
1676550a7375SFelipe Balbi 
1677550a7375SFelipe Balbi 	power = musb_readb(mregs, MUSB_POWER);
1678550a7375SFelipe Balbi 	power &= ~MUSB_POWER_RESUME;
1679550a7375SFelipe Balbi 	musb_writeb(mregs, MUSB_POWER, power);
1680550a7375SFelipe Balbi done:
1681550a7375SFelipe Balbi 	spin_unlock_irqrestore(&musb->lock, flags);
1682550a7375SFelipe Balbi 	return status;
1683550a7375SFelipe Balbi }
1684550a7375SFelipe Balbi 
1685550a7375SFelipe Balbi static int
1686550a7375SFelipe Balbi musb_gadget_set_self_powered(struct usb_gadget *gadget, int is_selfpowered)
1687550a7375SFelipe Balbi {
1688550a7375SFelipe Balbi 	struct musb	*musb = gadget_to_musb(gadget);
1689550a7375SFelipe Balbi 
1690550a7375SFelipe Balbi 	musb->is_self_powered = !!is_selfpowered;
1691550a7375SFelipe Balbi 	return 0;
1692550a7375SFelipe Balbi }
1693550a7375SFelipe Balbi 
1694550a7375SFelipe Balbi static void musb_pullup(struct musb *musb, int is_on)
1695550a7375SFelipe Balbi {
1696550a7375SFelipe Balbi 	u8 power;
1697550a7375SFelipe Balbi 
1698550a7375SFelipe Balbi 	power = musb_readb(musb->mregs, MUSB_POWER);
1699550a7375SFelipe Balbi 	if (is_on)
1700550a7375SFelipe Balbi 		power |= MUSB_POWER_SOFTCONN;
1701550a7375SFelipe Balbi 	else
1702550a7375SFelipe Balbi 		power &= ~MUSB_POWER_SOFTCONN;
1703550a7375SFelipe Balbi 
1704550a7375SFelipe Balbi 	/* FIXME if on, HdrcStart; if off, HdrcStop */
1705550a7375SFelipe Balbi 
1706e71eb392SSebastian Andrzej Siewior 	dev_dbg(musb->controller, "gadget D+ pullup %s\n",
1707e71eb392SSebastian Andrzej Siewior 		is_on ? "on" : "off");
1708550a7375SFelipe Balbi 	musb_writeb(musb->mregs, MUSB_POWER, power);
1709550a7375SFelipe Balbi }
1710550a7375SFelipe Balbi 
1711550a7375SFelipe Balbi #if 0
1712550a7375SFelipe Balbi static int musb_gadget_vbus_session(struct usb_gadget *gadget, int is_active)
1713550a7375SFelipe Balbi {
17145c8a86e1SFelipe Balbi 	dev_dbg(musb->controller, "<= %s =>\n", __func__);
1715550a7375SFelipe Balbi 
1716550a7375SFelipe Balbi 	/*
1717550a7375SFelipe Balbi 	 * FIXME iff driver's softconnect flag is set (as it is during probe,
1718550a7375SFelipe Balbi 	 * though that can clear it), just musb_pullup().
1719550a7375SFelipe Balbi 	 */
1720550a7375SFelipe Balbi 
1721550a7375SFelipe Balbi 	return -EINVAL;
1722550a7375SFelipe Balbi }
1723550a7375SFelipe Balbi #endif
1724550a7375SFelipe Balbi 
1725550a7375SFelipe Balbi static int musb_gadget_vbus_draw(struct usb_gadget *gadget, unsigned mA)
1726550a7375SFelipe Balbi {
1727550a7375SFelipe Balbi 	struct musb	*musb = gadget_to_musb(gadget);
1728550a7375SFelipe Balbi 
172984e250ffSDavid Brownell 	if (!musb->xceiv->set_power)
1730550a7375SFelipe Balbi 		return -EOPNOTSUPP;
1731b96d3b08SHeikki Krogerus 	return usb_phy_set_power(musb->xceiv, mA);
1732550a7375SFelipe Balbi }
1733550a7375SFelipe Balbi 
1734550a7375SFelipe Balbi static int musb_gadget_pullup(struct usb_gadget *gadget, int is_on)
1735550a7375SFelipe Balbi {
1736550a7375SFelipe Balbi 	struct musb	*musb = gadget_to_musb(gadget);
1737550a7375SFelipe Balbi 	unsigned long	flags;
1738550a7375SFelipe Balbi 
1739550a7375SFelipe Balbi 	is_on = !!is_on;
1740550a7375SFelipe Balbi 
174193e098a8SJohn Stultz 	pm_runtime_get_sync(musb->controller);
174293e098a8SJohn Stultz 
1743550a7375SFelipe Balbi 	/* NOTE: this assumes we are sensing vbus; we'd rather
1744550a7375SFelipe Balbi 	 * not pullup unless the B-session is active.
1745550a7375SFelipe Balbi 	 */
1746550a7375SFelipe Balbi 	spin_lock_irqsave(&musb->lock, flags);
1747550a7375SFelipe Balbi 	if (is_on != musb->softconnect) {
1748550a7375SFelipe Balbi 		musb->softconnect = is_on;
1749550a7375SFelipe Balbi 		musb_pullup(musb, is_on);
1750550a7375SFelipe Balbi 	}
1751550a7375SFelipe Balbi 	spin_unlock_irqrestore(&musb->lock, flags);
175293e098a8SJohn Stultz 
175393e098a8SJohn Stultz 	pm_runtime_put(musb->controller);
175493e098a8SJohn Stultz 
1755550a7375SFelipe Balbi 	return 0;
1756550a7375SFelipe Balbi }
1757550a7375SFelipe Balbi 
1758e71eb392SSebastian Andrzej Siewior static int musb_gadget_start(struct usb_gadget *g,
1759e71eb392SSebastian Andrzej Siewior 		struct usb_gadget_driver *driver);
1760e71eb392SSebastian Andrzej Siewior static int musb_gadget_stop(struct usb_gadget *g,
1761e71eb392SSebastian Andrzej Siewior 		struct usb_gadget_driver *driver);
17620f91349bSSebastian Andrzej Siewior 
1763550a7375SFelipe Balbi static const struct usb_gadget_ops musb_gadget_operations = {
1764550a7375SFelipe Balbi 	.get_frame		= musb_gadget_get_frame,
1765550a7375SFelipe Balbi 	.wakeup			= musb_gadget_wakeup,
1766550a7375SFelipe Balbi 	.set_selfpowered	= musb_gadget_set_self_powered,
1767550a7375SFelipe Balbi 	/* .vbus_session		= musb_gadget_vbus_session, */
1768550a7375SFelipe Balbi 	.vbus_draw		= musb_gadget_vbus_draw,
1769550a7375SFelipe Balbi 	.pullup			= musb_gadget_pullup,
1770e71eb392SSebastian Andrzej Siewior 	.udc_start		= musb_gadget_start,
1771e71eb392SSebastian Andrzej Siewior 	.udc_stop		= musb_gadget_stop,
1772550a7375SFelipe Balbi };
1773550a7375SFelipe Balbi 
1774550a7375SFelipe Balbi /* ----------------------------------------------------------------------- */
1775550a7375SFelipe Balbi 
1776550a7375SFelipe Balbi /* Registration */
1777550a7375SFelipe Balbi 
1778550a7375SFelipe Balbi /* Only this registration code "knows" the rule (from USB standards)
1779550a7375SFelipe Balbi  * about there being only one external upstream port.  It assumes
1780550a7375SFelipe Balbi  * all peripheral ports are external...
1781550a7375SFelipe Balbi  */
1782550a7375SFelipe Balbi 
1783550a7375SFelipe Balbi static void musb_gadget_release(struct device *dev)
1784550a7375SFelipe Balbi {
1785550a7375SFelipe Balbi 	/* kref_put(WHAT) */
1786550a7375SFelipe Balbi 	dev_dbg(dev, "%s\n", __func__);
1787550a7375SFelipe Balbi }
1788550a7375SFelipe Balbi 
1789550a7375SFelipe Balbi 
1790*41ac7b3aSBill Pemberton static void
1791550a7375SFelipe Balbi init_peripheral_ep(struct musb *musb, struct musb_ep *ep, u8 epnum, int is_in)
1792550a7375SFelipe Balbi {
1793550a7375SFelipe Balbi 	struct musb_hw_ep	*hw_ep = musb->endpoints + epnum;
1794550a7375SFelipe Balbi 
1795550a7375SFelipe Balbi 	memset(ep, 0, sizeof *ep);
1796550a7375SFelipe Balbi 
1797550a7375SFelipe Balbi 	ep->current_epnum = epnum;
1798550a7375SFelipe Balbi 	ep->musb = musb;
1799550a7375SFelipe Balbi 	ep->hw_ep = hw_ep;
1800550a7375SFelipe Balbi 	ep->is_in = is_in;
1801550a7375SFelipe Balbi 
1802550a7375SFelipe Balbi 	INIT_LIST_HEAD(&ep->req_list);
1803550a7375SFelipe Balbi 
1804550a7375SFelipe Balbi 	sprintf(ep->name, "ep%d%s", epnum,
1805550a7375SFelipe Balbi 			(!epnum || hw_ep->is_shared_fifo) ? "" : (
1806550a7375SFelipe Balbi 				is_in ? "in" : "out"));
1807550a7375SFelipe Balbi 	ep->end_point.name = ep->name;
1808550a7375SFelipe Balbi 	INIT_LIST_HEAD(&ep->end_point.ep_list);
1809550a7375SFelipe Balbi 	if (!epnum) {
1810550a7375SFelipe Balbi 		ep->end_point.maxpacket = 64;
1811550a7375SFelipe Balbi 		ep->end_point.ops = &musb_g_ep0_ops;
1812550a7375SFelipe Balbi 		musb->g.ep0 = &ep->end_point;
1813550a7375SFelipe Balbi 	} else {
1814550a7375SFelipe Balbi 		if (is_in)
1815550a7375SFelipe Balbi 			ep->end_point.maxpacket = hw_ep->max_packet_sz_tx;
1816550a7375SFelipe Balbi 		else
1817550a7375SFelipe Balbi 			ep->end_point.maxpacket = hw_ep->max_packet_sz_rx;
1818550a7375SFelipe Balbi 		ep->end_point.ops = &musb_ep_ops;
1819550a7375SFelipe Balbi 		list_add_tail(&ep->end_point.ep_list, &musb->g.ep_list);
1820550a7375SFelipe Balbi 	}
1821550a7375SFelipe Balbi }
1822550a7375SFelipe Balbi 
1823550a7375SFelipe Balbi /*
1824550a7375SFelipe Balbi  * Initialize the endpoints exposed to peripheral drivers, with backlinks
1825550a7375SFelipe Balbi  * to the rest of the driver state.
1826550a7375SFelipe Balbi  */
1827*41ac7b3aSBill Pemberton static inline void musb_g_init_endpoints(struct musb *musb)
1828550a7375SFelipe Balbi {
1829550a7375SFelipe Balbi 	u8			epnum;
1830550a7375SFelipe Balbi 	struct musb_hw_ep	*hw_ep;
1831550a7375SFelipe Balbi 	unsigned		count = 0;
1832550a7375SFelipe Balbi 
1833b595076aSUwe Kleine-König 	/* initialize endpoint list just once */
1834550a7375SFelipe Balbi 	INIT_LIST_HEAD(&(musb->g.ep_list));
1835550a7375SFelipe Balbi 
1836550a7375SFelipe Balbi 	for (epnum = 0, hw_ep = musb->endpoints;
1837550a7375SFelipe Balbi 			epnum < musb->nr_endpoints;
1838550a7375SFelipe Balbi 			epnum++, hw_ep++) {
1839550a7375SFelipe Balbi 		if (hw_ep->is_shared_fifo /* || !epnum */) {
1840550a7375SFelipe Balbi 			init_peripheral_ep(musb, &hw_ep->ep_in, epnum, 0);
1841550a7375SFelipe Balbi 			count++;
1842550a7375SFelipe Balbi 		} else {
1843550a7375SFelipe Balbi 			if (hw_ep->max_packet_sz_tx) {
1844550a7375SFelipe Balbi 				init_peripheral_ep(musb, &hw_ep->ep_in,
1845550a7375SFelipe Balbi 							epnum, 1);
1846550a7375SFelipe Balbi 				count++;
1847550a7375SFelipe Balbi 			}
1848550a7375SFelipe Balbi 			if (hw_ep->max_packet_sz_rx) {
1849550a7375SFelipe Balbi 				init_peripheral_ep(musb, &hw_ep->ep_out,
1850550a7375SFelipe Balbi 							epnum, 0);
1851550a7375SFelipe Balbi 				count++;
1852550a7375SFelipe Balbi 			}
1853550a7375SFelipe Balbi 		}
1854550a7375SFelipe Balbi 	}
1855550a7375SFelipe Balbi }
1856550a7375SFelipe Balbi 
1857550a7375SFelipe Balbi /* called once during driver setup to initialize and link into
1858550a7375SFelipe Balbi  * the driver model; memory is zeroed.
1859550a7375SFelipe Balbi  */
1860*41ac7b3aSBill Pemberton int musb_gadget_setup(struct musb *musb)
1861550a7375SFelipe Balbi {
1862550a7375SFelipe Balbi 	int status;
1863550a7375SFelipe Balbi 
1864550a7375SFelipe Balbi 	/* REVISIT minor race:  if (erroneously) setting up two
1865550a7375SFelipe Balbi 	 * musb peripherals at the same time, only the bus lock
1866550a7375SFelipe Balbi 	 * is probably held.
1867550a7375SFelipe Balbi 	 */
1868550a7375SFelipe Balbi 
1869550a7375SFelipe Balbi 	musb->g.ops = &musb_gadget_operations;
1870d327ab5bSMichal Nazarewicz 	musb->g.max_speed = USB_SPEED_HIGH;
1871550a7375SFelipe Balbi 	musb->g.speed = USB_SPEED_UNKNOWN;
1872550a7375SFelipe Balbi 
1873550a7375SFelipe Balbi 	/* this "gadget" abstracts/virtualizes the controller */
1874427c4f33SKay Sievers 	dev_set_name(&musb->g.dev, "gadget");
1875550a7375SFelipe Balbi 	musb->g.dev.parent = musb->controller;
1876550a7375SFelipe Balbi 	musb->g.dev.dma_mask = musb->controller->dma_mask;
1877550a7375SFelipe Balbi 	musb->g.dev.release = musb_gadget_release;
1878550a7375SFelipe Balbi 	musb->g.name = musb_driver_name;
1879550a7375SFelipe Balbi 
1880550a7375SFelipe Balbi 	musb->g.is_otg = 1;
1881550a7375SFelipe Balbi 
1882550a7375SFelipe Balbi 	musb_g_init_endpoints(musb);
1883550a7375SFelipe Balbi 
1884550a7375SFelipe Balbi 	musb->is_active = 0;
1885550a7375SFelipe Balbi 	musb_platform_try_idle(musb, 0);
1886550a7375SFelipe Balbi 
1887550a7375SFelipe Balbi 	status = device_register(&musb->g.dev);
1888e2c34045SRahul Ruikar 	if (status != 0) {
1889e2c34045SRahul Ruikar 		put_device(&musb->g.dev);
18900f91349bSSebastian Andrzej Siewior 		return status;
1891e2c34045SRahul Ruikar 	}
18920f91349bSSebastian Andrzej Siewior 	status = usb_add_gadget_udc(musb->controller, &musb->g);
18930f91349bSSebastian Andrzej Siewior 	if (status)
18940f91349bSSebastian Andrzej Siewior 		goto err;
18950f91349bSSebastian Andrzej Siewior 
18960f91349bSSebastian Andrzej Siewior 	return 0;
18970f91349bSSebastian Andrzej Siewior err:
18986193d699SSebastian Andrzej Siewior 	musb->g.dev.parent = NULL;
18990f91349bSSebastian Andrzej Siewior 	device_unregister(&musb->g.dev);
1900550a7375SFelipe Balbi 	return status;
1901550a7375SFelipe Balbi }
1902550a7375SFelipe Balbi 
1903550a7375SFelipe Balbi void musb_gadget_cleanup(struct musb *musb)
1904550a7375SFelipe Balbi {
19050f91349bSSebastian Andrzej Siewior 	usb_del_gadget_udc(&musb->g);
19066193d699SSebastian Andrzej Siewior 	if (musb->g.dev.parent)
1907550a7375SFelipe Balbi 		device_unregister(&musb->g.dev);
1908550a7375SFelipe Balbi }
1909550a7375SFelipe Balbi 
1910550a7375SFelipe Balbi /*
1911550a7375SFelipe Balbi  * Register the gadget driver. Used by gadget drivers when
1912550a7375SFelipe Balbi  * registering themselves with the controller.
1913550a7375SFelipe Balbi  *
1914550a7375SFelipe Balbi  * -EINVAL something went wrong (not driver)
1915550a7375SFelipe Balbi  * -EBUSY another gadget is already using the controller
1916b595076aSUwe Kleine-König  * -ENOMEM no memory to perform the operation
1917550a7375SFelipe Balbi  *
1918550a7375SFelipe Balbi  * @param driver the gadget driver
1919550a7375SFelipe Balbi  * @return <0 if error, 0 if everything is fine
1920550a7375SFelipe Balbi  */
1921e71eb392SSebastian Andrzej Siewior static int musb_gadget_start(struct usb_gadget *g,
1922e71eb392SSebastian Andrzej Siewior 		struct usb_gadget_driver *driver)
1923550a7375SFelipe Balbi {
1924e71eb392SSebastian Andrzej Siewior 	struct musb		*musb = gadget_to_musb(g);
1925d445b6daSHeikki Krogerus 	struct usb_otg		*otg = musb->xceiv->otg;
1926032ec49fSFelipe Balbi 	struct usb_hcd		*hcd = musb_to_hcd(musb);
192763eed2b5SFelipe Balbi 	unsigned long		flags;
1928032ec49fSFelipe Balbi 	int			retval = 0;
1929550a7375SFelipe Balbi 
1930032ec49fSFelipe Balbi 	if (driver->max_speed < USB_SPEED_HIGH) {
1931032ec49fSFelipe Balbi 		retval = -EINVAL;
1932032ec49fSFelipe Balbi 		goto err;
1933032ec49fSFelipe Balbi 	}
1934550a7375SFelipe Balbi 
19357acc6197SHema HK 	pm_runtime_get_sync(musb->controller);
19367acc6197SHema HK 
19375c8a86e1SFelipe Balbi 	dev_dbg(musb->controller, "registering driver %s\n", driver->function);
1938550a7375SFelipe Balbi 
1939e71eb392SSebastian Andrzej Siewior 	musb->softconnect = 0;
1940550a7375SFelipe Balbi 	musb->gadget_driver = driver;
1941550a7375SFelipe Balbi 
1942550a7375SFelipe Balbi 	spin_lock_irqsave(&musb->lock, flags);
1943e71eb392SSebastian Andrzej Siewior 	musb->is_active = 1;
1944550a7375SFelipe Balbi 
19456e13c650SHeikki Krogerus 	otg_set_peripheral(otg, &musb->g);
1946d4c433feSArnaud Mandy 	musb->xceiv->state = OTG_STATE_B_IDLE;
1947550a7375SFelipe Balbi 	spin_unlock_irqrestore(&musb->lock, flags);
1948550a7375SFelipe Balbi 
1949550a7375SFelipe Balbi 	/* REVISIT:  funcall to other code, which also
1950550a7375SFelipe Balbi 	 * handles power budgeting ... this way also
1951550a7375SFelipe Balbi 	 * ensures HdrcStart is indirectly called.
1952550a7375SFelipe Balbi 	 */
1953032ec49fSFelipe Balbi 	retval = usb_add_hcd(hcd, 0, 0);
1954550a7375SFelipe Balbi 	if (retval < 0) {
19555c8a86e1SFelipe Balbi 		dev_dbg(musb->controller, "add_hcd failed, %d\n", retval);
1956032ec49fSFelipe Balbi 		goto err;
19575f1e8ce7SHema HK 	}
1958002eda13SHema HK 
1959002eda13SHema HK 	if ((musb->xceiv->last_event == USB_EVENT_ID)
1960d445b6daSHeikki Krogerus 				&& otg->set_vbus)
19616e13c650SHeikki Krogerus 		otg_set_vbus(otg, 1);
1962550a7375SFelipe Balbi 
196363eed2b5SFelipe Balbi 	hcd->self.uses_pio_for_control = 1;
1964032ec49fSFelipe Balbi 
19657acc6197SHema HK 	if (musb->xceiv->last_event == USB_EVENT_NONE)
19667acc6197SHema HK 		pm_runtime_put(musb->controller);
19677acc6197SHema HK 
196863eed2b5SFelipe Balbi 	return 0;
196963eed2b5SFelipe Balbi 
1970032ec49fSFelipe Balbi err:
1971550a7375SFelipe Balbi 	return retval;
1972550a7375SFelipe Balbi }
1973550a7375SFelipe Balbi 
1974550a7375SFelipe Balbi static void stop_activity(struct musb *musb, struct usb_gadget_driver *driver)
1975550a7375SFelipe Balbi {
1976550a7375SFelipe Balbi 	int			i;
1977550a7375SFelipe Balbi 	struct musb_hw_ep	*hw_ep;
1978550a7375SFelipe Balbi 
1979550a7375SFelipe Balbi 	/* don't disconnect if it's not connected */
1980550a7375SFelipe Balbi 	if (musb->g.speed == USB_SPEED_UNKNOWN)
1981550a7375SFelipe Balbi 		driver = NULL;
1982550a7375SFelipe Balbi 	else
1983550a7375SFelipe Balbi 		musb->g.speed = USB_SPEED_UNKNOWN;
1984550a7375SFelipe Balbi 
1985550a7375SFelipe Balbi 	/* deactivate the hardware */
1986550a7375SFelipe Balbi 	if (musb->softconnect) {
1987550a7375SFelipe Balbi 		musb->softconnect = 0;
1988550a7375SFelipe Balbi 		musb_pullup(musb, 0);
1989550a7375SFelipe Balbi 	}
1990550a7375SFelipe Balbi 	musb_stop(musb);
1991550a7375SFelipe Balbi 
1992550a7375SFelipe Balbi 	/* killing any outstanding requests will quiesce the driver;
1993550a7375SFelipe Balbi 	 * then report disconnect
1994550a7375SFelipe Balbi 	 */
1995550a7375SFelipe Balbi 	if (driver) {
1996550a7375SFelipe Balbi 		for (i = 0, hw_ep = musb->endpoints;
1997550a7375SFelipe Balbi 				i < musb->nr_endpoints;
1998550a7375SFelipe Balbi 				i++, hw_ep++) {
1999550a7375SFelipe Balbi 			musb_ep_select(musb->mregs, i);
2000550a7375SFelipe Balbi 			if (hw_ep->is_shared_fifo /* || !epnum */) {
2001550a7375SFelipe Balbi 				nuke(&hw_ep->ep_in, -ESHUTDOWN);
2002550a7375SFelipe Balbi 			} else {
2003550a7375SFelipe Balbi 				if (hw_ep->max_packet_sz_tx)
2004550a7375SFelipe Balbi 					nuke(&hw_ep->ep_in, -ESHUTDOWN);
2005550a7375SFelipe Balbi 				if (hw_ep->max_packet_sz_rx)
2006550a7375SFelipe Balbi 					nuke(&hw_ep->ep_out, -ESHUTDOWN);
2007550a7375SFelipe Balbi 			}
2008550a7375SFelipe Balbi 		}
2009550a7375SFelipe Balbi 	}
2010550a7375SFelipe Balbi }
2011550a7375SFelipe Balbi 
2012550a7375SFelipe Balbi /*
2013550a7375SFelipe Balbi  * Unregister the gadget driver. Used by gadget drivers when
2014550a7375SFelipe Balbi  * unregistering themselves from the controller.
2015550a7375SFelipe Balbi  *
2016550a7375SFelipe Balbi  * @param driver the gadget driver to unregister
2017550a7375SFelipe Balbi  */
2018e71eb392SSebastian Andrzej Siewior static int musb_gadget_stop(struct usb_gadget *g,
2019e71eb392SSebastian Andrzej Siewior 		struct usb_gadget_driver *driver)
2020550a7375SFelipe Balbi {
2021e71eb392SSebastian Andrzej Siewior 	struct musb	*musb = gadget_to_musb(g);
202263eed2b5SFelipe Balbi 	unsigned long	flags;
2023550a7375SFelipe Balbi 
20247acc6197SHema HK 	if (musb->xceiv->last_event == USB_EVENT_NONE)
20257acc6197SHema HK 		pm_runtime_get_sync(musb->controller);
20267acc6197SHema HK 
202763eed2b5SFelipe Balbi 	/*
202863eed2b5SFelipe Balbi 	 * REVISIT always use otg_set_peripheral() here too;
2029550a7375SFelipe Balbi 	 * this needs to shut down the OTG engine.
2030550a7375SFelipe Balbi 	 */
2031550a7375SFelipe Balbi 
2032550a7375SFelipe Balbi 	spin_lock_irqsave(&musb->lock, flags);
2033550a7375SFelipe Balbi 
2034550a7375SFelipe Balbi 	musb_hnp_stop(musb);
2035550a7375SFelipe Balbi 
2036550a7375SFelipe Balbi 	(void) musb_gadget_vbus_draw(&musb->g, 0);
2037550a7375SFelipe Balbi 
203884e250ffSDavid Brownell 	musb->xceiv->state = OTG_STATE_UNDEFINED;
2039550a7375SFelipe Balbi 	stop_activity(musb, driver);
20406e13c650SHeikki Krogerus 	otg_set_peripheral(musb->xceiv->otg, NULL);
2041550a7375SFelipe Balbi 
20425c8a86e1SFelipe Balbi 	dev_dbg(musb->controller, "unregistering driver %s\n", driver->function);
204363eed2b5SFelipe Balbi 
2044550a7375SFelipe Balbi 	musb->is_active = 0;
2045550a7375SFelipe Balbi 	musb_platform_try_idle(musb, 0);
2046550a7375SFelipe Balbi 	spin_unlock_irqrestore(&musb->lock, flags);
2047550a7375SFelipe Balbi 
2048550a7375SFelipe Balbi 	usb_remove_hcd(musb_to_hcd(musb));
2049032ec49fSFelipe Balbi 	/*
2050032ec49fSFelipe Balbi 	 * FIXME we need to be able to register another
2051550a7375SFelipe Balbi 	 * gadget driver here and have everything work;
2052550a7375SFelipe Balbi 	 * that currently misbehaves.
2053550a7375SFelipe Balbi 	 */
205463eed2b5SFelipe Balbi 
20557acc6197SHema HK 	pm_runtime_put(musb->controller);
20567acc6197SHema HK 
205763eed2b5SFelipe Balbi 	return 0;
2058550a7375SFelipe Balbi }
2059550a7375SFelipe Balbi 
2060550a7375SFelipe Balbi /* ----------------------------------------------------------------------- */
2061550a7375SFelipe Balbi 
2062550a7375SFelipe Balbi /* lifecycle operations called through plat_uds.c */
2063550a7375SFelipe Balbi 
2064550a7375SFelipe Balbi void musb_g_resume(struct musb *musb)
2065550a7375SFelipe Balbi {
2066550a7375SFelipe Balbi 	musb->is_suspended = 0;
206784e250ffSDavid Brownell 	switch (musb->xceiv->state) {
2068550a7375SFelipe Balbi 	case OTG_STATE_B_IDLE:
2069550a7375SFelipe Balbi 		break;
2070550a7375SFelipe Balbi 	case OTG_STATE_B_WAIT_ACON:
2071550a7375SFelipe Balbi 	case OTG_STATE_B_PERIPHERAL:
2072550a7375SFelipe Balbi 		musb->is_active = 1;
2073550a7375SFelipe Balbi 		if (musb->gadget_driver && musb->gadget_driver->resume) {
2074550a7375SFelipe Balbi 			spin_unlock(&musb->lock);
2075550a7375SFelipe Balbi 			musb->gadget_driver->resume(&musb->g);
2076550a7375SFelipe Balbi 			spin_lock(&musb->lock);
2077550a7375SFelipe Balbi 		}
2078550a7375SFelipe Balbi 		break;
2079550a7375SFelipe Balbi 	default:
2080550a7375SFelipe Balbi 		WARNING("unhandled RESUME transition (%s)\n",
20813df00453SAnatolij Gustschin 				otg_state_string(musb->xceiv->state));
2082550a7375SFelipe Balbi 	}
2083550a7375SFelipe Balbi }
2084550a7375SFelipe Balbi 
2085550a7375SFelipe Balbi /* called when SOF packets stop for 3+ msec */
2086550a7375SFelipe Balbi void musb_g_suspend(struct musb *musb)
2087550a7375SFelipe Balbi {
2088550a7375SFelipe Balbi 	u8	devctl;
2089550a7375SFelipe Balbi 
2090550a7375SFelipe Balbi 	devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
20915c8a86e1SFelipe Balbi 	dev_dbg(musb->controller, "devctl %02x\n", devctl);
2092550a7375SFelipe Balbi 
209384e250ffSDavid Brownell 	switch (musb->xceiv->state) {
2094550a7375SFelipe Balbi 	case OTG_STATE_B_IDLE:
2095550a7375SFelipe Balbi 		if ((devctl & MUSB_DEVCTL_VBUS) == MUSB_DEVCTL_VBUS)
209684e250ffSDavid Brownell 			musb->xceiv->state = OTG_STATE_B_PERIPHERAL;
2097550a7375SFelipe Balbi 		break;
2098550a7375SFelipe Balbi 	case OTG_STATE_B_PERIPHERAL:
2099550a7375SFelipe Balbi 		musb->is_suspended = 1;
2100550a7375SFelipe Balbi 		if (musb->gadget_driver && musb->gadget_driver->suspend) {
2101550a7375SFelipe Balbi 			spin_unlock(&musb->lock);
2102550a7375SFelipe Balbi 			musb->gadget_driver->suspend(&musb->g);
2103550a7375SFelipe Balbi 			spin_lock(&musb->lock);
2104550a7375SFelipe Balbi 		}
2105550a7375SFelipe Balbi 		break;
2106550a7375SFelipe Balbi 	default:
2107550a7375SFelipe Balbi 		/* REVISIT if B_HOST, clear DEVCTL.HOSTREQ;
2108550a7375SFelipe Balbi 		 * A_PERIPHERAL may need care too
2109550a7375SFelipe Balbi 		 */
2110550a7375SFelipe Balbi 		WARNING("unhandled SUSPEND transition (%s)\n",
21113df00453SAnatolij Gustschin 				otg_state_string(musb->xceiv->state));
2112550a7375SFelipe Balbi 	}
2113550a7375SFelipe Balbi }
2114550a7375SFelipe Balbi 
2115550a7375SFelipe Balbi /* Called during SRP */
2116550a7375SFelipe Balbi void musb_g_wakeup(struct musb *musb)
2117550a7375SFelipe Balbi {
2118550a7375SFelipe Balbi 	musb_gadget_wakeup(&musb->g);
2119550a7375SFelipe Balbi }
2120550a7375SFelipe Balbi 
2121550a7375SFelipe Balbi /* called when VBUS drops below session threshold, and in other cases */
2122550a7375SFelipe Balbi void musb_g_disconnect(struct musb *musb)
2123550a7375SFelipe Balbi {
2124550a7375SFelipe Balbi 	void __iomem	*mregs = musb->mregs;
2125550a7375SFelipe Balbi 	u8	devctl = musb_readb(mregs, MUSB_DEVCTL);
2126550a7375SFelipe Balbi 
21275c8a86e1SFelipe Balbi 	dev_dbg(musb->controller, "devctl %02x\n", devctl);
2128550a7375SFelipe Balbi 
2129550a7375SFelipe Balbi 	/* clear HR */
2130550a7375SFelipe Balbi 	musb_writeb(mregs, MUSB_DEVCTL, devctl & MUSB_DEVCTL_SESSION);
2131550a7375SFelipe Balbi 
2132550a7375SFelipe Balbi 	/* don't draw vbus until new b-default session */
2133550a7375SFelipe Balbi 	(void) musb_gadget_vbus_draw(&musb->g, 0);
2134550a7375SFelipe Balbi 
2135550a7375SFelipe Balbi 	musb->g.speed = USB_SPEED_UNKNOWN;
2136550a7375SFelipe Balbi 	if (musb->gadget_driver && musb->gadget_driver->disconnect) {
2137550a7375SFelipe Balbi 		spin_unlock(&musb->lock);
2138550a7375SFelipe Balbi 		musb->gadget_driver->disconnect(&musb->g);
2139550a7375SFelipe Balbi 		spin_lock(&musb->lock);
2140550a7375SFelipe Balbi 	}
2141550a7375SFelipe Balbi 
214284e250ffSDavid Brownell 	switch (musb->xceiv->state) {
2143550a7375SFelipe Balbi 	default:
21445c8a86e1SFelipe Balbi 		dev_dbg(musb->controller, "Unhandled disconnect %s, setting a_idle\n",
21453df00453SAnatolij Gustschin 			otg_state_string(musb->xceiv->state));
214684e250ffSDavid Brownell 		musb->xceiv->state = OTG_STATE_A_IDLE;
2147ab983f2aSDavid Brownell 		MUSB_HST_MODE(musb);
2148550a7375SFelipe Balbi 		break;
2149550a7375SFelipe Balbi 	case OTG_STATE_A_PERIPHERAL:
21501de00daeSDavid Brownell 		musb->xceiv->state = OTG_STATE_A_WAIT_BCON;
2151ab983f2aSDavid Brownell 		MUSB_HST_MODE(musb);
2152550a7375SFelipe Balbi 		break;
2153550a7375SFelipe Balbi 	case OTG_STATE_B_WAIT_ACON:
2154550a7375SFelipe Balbi 	case OTG_STATE_B_HOST:
2155550a7375SFelipe Balbi 	case OTG_STATE_B_PERIPHERAL:
2156550a7375SFelipe Balbi 	case OTG_STATE_B_IDLE:
215784e250ffSDavid Brownell 		musb->xceiv->state = OTG_STATE_B_IDLE;
2158550a7375SFelipe Balbi 		break;
2159550a7375SFelipe Balbi 	case OTG_STATE_B_SRP_INIT:
2160550a7375SFelipe Balbi 		break;
2161550a7375SFelipe Balbi 	}
2162550a7375SFelipe Balbi 
2163550a7375SFelipe Balbi 	musb->is_active = 0;
2164550a7375SFelipe Balbi }
2165550a7375SFelipe Balbi 
2166550a7375SFelipe Balbi void musb_g_reset(struct musb *musb)
2167550a7375SFelipe Balbi __releases(musb->lock)
2168550a7375SFelipe Balbi __acquires(musb->lock)
2169550a7375SFelipe Balbi {
2170550a7375SFelipe Balbi 	void __iomem	*mbase = musb->mregs;
2171550a7375SFelipe Balbi 	u8		devctl = musb_readb(mbase, MUSB_DEVCTL);
2172550a7375SFelipe Balbi 	u8		power;
2173550a7375SFelipe Balbi 
2174515ba29cSSebastian Andrzej Siewior 	dev_dbg(musb->controller, "<== %s driver '%s'\n",
2175550a7375SFelipe Balbi 			(devctl & MUSB_DEVCTL_BDEVICE)
2176550a7375SFelipe Balbi 				? "B-Device" : "A-Device",
2177550a7375SFelipe Balbi 			musb->gadget_driver
2178550a7375SFelipe Balbi 				? musb->gadget_driver->driver.name
2179550a7375SFelipe Balbi 				: NULL
2180550a7375SFelipe Balbi 			);
2181550a7375SFelipe Balbi 
2182550a7375SFelipe Balbi 	/* report disconnect, if we didn't already (flushing EP state) */
2183550a7375SFelipe Balbi 	if (musb->g.speed != USB_SPEED_UNKNOWN)
2184550a7375SFelipe Balbi 		musb_g_disconnect(musb);
2185550a7375SFelipe Balbi 
2186550a7375SFelipe Balbi 	/* clear HR */
2187550a7375SFelipe Balbi 	else if (devctl & MUSB_DEVCTL_HR)
2188550a7375SFelipe Balbi 		musb_writeb(mbase, MUSB_DEVCTL, MUSB_DEVCTL_SESSION);
2189550a7375SFelipe Balbi 
2190550a7375SFelipe Balbi 
2191550a7375SFelipe Balbi 	/* what speed did we negotiate? */
2192550a7375SFelipe Balbi 	power = musb_readb(mbase, MUSB_POWER);
2193550a7375SFelipe Balbi 	musb->g.speed = (power & MUSB_POWER_HSMODE)
2194550a7375SFelipe Balbi 			? USB_SPEED_HIGH : USB_SPEED_FULL;
2195550a7375SFelipe Balbi 
2196550a7375SFelipe Balbi 	/* start in USB_STATE_DEFAULT */
2197550a7375SFelipe Balbi 	musb->is_active = 1;
2198550a7375SFelipe Balbi 	musb->is_suspended = 0;
2199550a7375SFelipe Balbi 	MUSB_DEV_MODE(musb);
2200550a7375SFelipe Balbi 	musb->address = 0;
2201550a7375SFelipe Balbi 	musb->ep0_state = MUSB_EP0_STAGE_SETUP;
2202550a7375SFelipe Balbi 
2203550a7375SFelipe Balbi 	musb->may_wakeup = 0;
2204550a7375SFelipe Balbi 	musb->g.b_hnp_enable = 0;
2205550a7375SFelipe Balbi 	musb->g.a_alt_hnp_support = 0;
2206550a7375SFelipe Balbi 	musb->g.a_hnp_support = 0;
2207550a7375SFelipe Balbi 
2208550a7375SFelipe Balbi 	/* Normal reset, as B-Device;
2209550a7375SFelipe Balbi 	 * or else after HNP, as A-Device
2210550a7375SFelipe Balbi 	 */
2211550a7375SFelipe Balbi 	if (devctl & MUSB_DEVCTL_BDEVICE) {
221284e250ffSDavid Brownell 		musb->xceiv->state = OTG_STATE_B_PERIPHERAL;
2213550a7375SFelipe Balbi 		musb->g.is_a_peripheral = 0;
2214032ec49fSFelipe Balbi 	} else {
221584e250ffSDavid Brownell 		musb->xceiv->state = OTG_STATE_A_PERIPHERAL;
2216550a7375SFelipe Balbi 		musb->g.is_a_peripheral = 1;
2217032ec49fSFelipe Balbi 	}
2218550a7375SFelipe Balbi 
2219550a7375SFelipe Balbi 	/* start with default limits on VBUS power draw */
2220032ec49fSFelipe Balbi 	(void) musb_gadget_vbus_draw(&musb->g, 8);
2221550a7375SFelipe Balbi }
2222