xref: /openbmc/linux/drivers/usb/musb/musb_gadget.c (revision ecc23d0a422a3118fcf6e4f0a46e17a6c2047b02)
15fd54aceSGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
2550a7375SFelipe Balbi /*
3550a7375SFelipe Balbi  * MUSB OTG driver peripheral support
4550a7375SFelipe Balbi  *
5550a7375SFelipe Balbi  * Copyright 2005 Mentor Graphics Corporation
6550a7375SFelipe Balbi  * Copyright (C) 2005-2006 by Texas Instruments
7550a7375SFelipe Balbi  * Copyright (C) 2006-2007 Nokia Corporation
8cea83241SSergei Shtylyov  * Copyright (C) 2009 MontaVista Software, Inc. <source@mvista.com>
9550a7375SFelipe Balbi  */
10550a7375SFelipe Balbi 
11550a7375SFelipe Balbi #include <linux/kernel.h>
12550a7375SFelipe Balbi #include <linux/list.h>
13550a7375SFelipe Balbi #include <linux/timer.h>
14550a7375SFelipe Balbi #include <linux/module.h>
15550a7375SFelipe Balbi #include <linux/smp.h>
16550a7375SFelipe Balbi #include <linux/spinlock.h>
17550a7375SFelipe Balbi #include <linux/delay.h>
18550a7375SFelipe Balbi #include <linux/dma-mapping.h>
195a0e3ad6STejun Heo #include <linux/slab.h>
20550a7375SFelipe Balbi 
21550a7375SFelipe Balbi #include "musb_core.h"
22fc78003eSBin Liu #include "musb_trace.h"
23550a7375SFelipe Balbi 
24550a7375SFelipe Balbi 
25550a7375SFelipe Balbi /* ----------------------------------------------------------------------- */
26550a7375SFelipe Balbi 
27c65bfa62SMian Yousaf Kaukab #define is_buffer_mapped(req) (is_dma_capable() && \
28c65bfa62SMian Yousaf Kaukab 					(req->map_state != UN_MAPPED))
29c65bfa62SMian Yousaf Kaukab 
3092d2711fSHema Kalliguddi /* Maps the buffer to dma  */
3192d2711fSHema Kalliguddi 
map_dma_buffer(struct musb_request * request,struct musb * musb,struct musb_ep * musb_ep)3292d2711fSHema Kalliguddi static inline void map_dma_buffer(struct musb_request *request,
33c65bfa62SMian Yousaf Kaukab 			struct musb *musb, struct musb_ep *musb_ep)
3492d2711fSHema Kalliguddi {
355f5761cbSMian Yousaf Kaukab 	int compatible = true;
365f5761cbSMian Yousaf Kaukab 	struct dma_controller *dma = musb->dma_controller;
375f5761cbSMian Yousaf Kaukab 
38c65bfa62SMian Yousaf Kaukab 	request->map_state = UN_MAPPED;
39c65bfa62SMian Yousaf Kaukab 
40c65bfa62SMian Yousaf Kaukab 	if (!is_dma_capable() || !musb_ep->dma)
41c65bfa62SMian Yousaf Kaukab 		return;
42c65bfa62SMian Yousaf Kaukab 
435f5761cbSMian Yousaf Kaukab 	/* Check if DMA engine can handle this request.
445f5761cbSMian Yousaf Kaukab 	 * DMA code must reject the USB request explicitly.
455f5761cbSMian Yousaf Kaukab 	 * Default behaviour is to map the request.
465f5761cbSMian Yousaf Kaukab 	 */
475f5761cbSMian Yousaf Kaukab 	if (dma->is_compatible)
485f5761cbSMian Yousaf Kaukab 		compatible = dma->is_compatible(musb_ep->dma,
495f5761cbSMian Yousaf Kaukab 				musb_ep->packet_sz, request->request.buf,
505f5761cbSMian Yousaf Kaukab 				request->request.length);
515f5761cbSMian Yousaf Kaukab 	if (!compatible)
525f5761cbSMian Yousaf Kaukab 		return;
535f5761cbSMian Yousaf Kaukab 
5492d2711fSHema Kalliguddi 	if (request->request.dma == DMA_ADDR_INVALID) {
557b360f42SSebastian Andrzej Siewior 		dma_addr_t dma_addr;
567b360f42SSebastian Andrzej Siewior 		int ret;
577b360f42SSebastian Andrzej Siewior 
587b360f42SSebastian Andrzej Siewior 		dma_addr = dma_map_single(
5992d2711fSHema Kalliguddi 				musb->controller,
6092d2711fSHema Kalliguddi 				request->request.buf,
6192d2711fSHema Kalliguddi 				request->request.length,
6292d2711fSHema Kalliguddi 				request->tx
6392d2711fSHema Kalliguddi 					? DMA_TO_DEVICE
6492d2711fSHema Kalliguddi 					: DMA_FROM_DEVICE);
657b360f42SSebastian Andrzej Siewior 		ret = dma_mapping_error(musb->controller, dma_addr);
667b360f42SSebastian Andrzej Siewior 		if (ret)
677b360f42SSebastian Andrzej Siewior 			return;
687b360f42SSebastian Andrzej Siewior 
697b360f42SSebastian Andrzej Siewior 		request->request.dma = dma_addr;
70c65bfa62SMian Yousaf Kaukab 		request->map_state = MUSB_MAPPED;
7192d2711fSHema Kalliguddi 	} else {
7292d2711fSHema Kalliguddi 		dma_sync_single_for_device(musb->controller,
7392d2711fSHema Kalliguddi 			request->request.dma,
7492d2711fSHema Kalliguddi 			request->request.length,
7592d2711fSHema Kalliguddi 			request->tx
7692d2711fSHema Kalliguddi 				? DMA_TO_DEVICE
7792d2711fSHema Kalliguddi 				: DMA_FROM_DEVICE);
78c65bfa62SMian Yousaf Kaukab 		request->map_state = PRE_MAPPED;
7992d2711fSHema Kalliguddi 	}
8092d2711fSHema Kalliguddi }
8192d2711fSHema Kalliguddi 
8292d2711fSHema Kalliguddi /* Unmap the buffer from dma and maps it back to cpu */
unmap_dma_buffer(struct musb_request * request,struct musb * musb)8392d2711fSHema Kalliguddi static inline void unmap_dma_buffer(struct musb_request *request,
8492d2711fSHema Kalliguddi 				struct musb *musb)
8592d2711fSHema Kalliguddi {
8606d9db72SKishon Vijay Abraham I 	struct musb_ep *musb_ep = request->ep;
8706d9db72SKishon Vijay Abraham I 
8806d9db72SKishon Vijay Abraham I 	if (!is_buffer_mapped(request) || !musb_ep->dma)
89c65bfa62SMian Yousaf Kaukab 		return;
90c65bfa62SMian Yousaf Kaukab 
9192d2711fSHema Kalliguddi 	if (request->request.dma == DMA_ADDR_INVALID) {
925c8a86e1SFelipe Balbi 		dev_vdbg(musb->controller,
935c8a86e1SFelipe Balbi 				"not unmapping a never mapped buffer\n");
9492d2711fSHema Kalliguddi 		return;
9592d2711fSHema Kalliguddi 	}
96c65bfa62SMian Yousaf Kaukab 	if (request->map_state == MUSB_MAPPED) {
9792d2711fSHema Kalliguddi 		dma_unmap_single(musb->controller,
9892d2711fSHema Kalliguddi 			request->request.dma,
9992d2711fSHema Kalliguddi 			request->request.length,
10092d2711fSHema Kalliguddi 			request->tx
10192d2711fSHema Kalliguddi 				? DMA_TO_DEVICE
10292d2711fSHema Kalliguddi 				: DMA_FROM_DEVICE);
10392d2711fSHema Kalliguddi 		request->request.dma = DMA_ADDR_INVALID;
104c65bfa62SMian Yousaf Kaukab 	} else { /* PRE_MAPPED */
10592d2711fSHema Kalliguddi 		dma_sync_single_for_cpu(musb->controller,
10692d2711fSHema Kalliguddi 			request->request.dma,
10792d2711fSHema Kalliguddi 			request->request.length,
10892d2711fSHema Kalliguddi 			request->tx
10992d2711fSHema Kalliguddi 				? DMA_TO_DEVICE
11092d2711fSHema Kalliguddi 				: DMA_FROM_DEVICE);
11192d2711fSHema Kalliguddi 	}
112c65bfa62SMian Yousaf Kaukab 	request->map_state = UN_MAPPED;
11392d2711fSHema Kalliguddi }
11492d2711fSHema Kalliguddi 
115550a7375SFelipe Balbi /*
116550a7375SFelipe Balbi  * Immediately complete a request.
117550a7375SFelipe Balbi  *
118550a7375SFelipe Balbi  * @param request the request to complete
119550a7375SFelipe Balbi  * @param status the status to complete the request with
120550a7375SFelipe Balbi  * Context: controller locked, IRQs blocked.
121550a7375SFelipe Balbi  */
musb_g_giveback(struct musb_ep * ep,struct usb_request * request,int status)122550a7375SFelipe Balbi void musb_g_giveback(
123550a7375SFelipe Balbi 	struct musb_ep		*ep,
124550a7375SFelipe Balbi 	struct usb_request	*request,
125550a7375SFelipe Balbi 	int			status)
126550a7375SFelipe Balbi __releases(ep->musb->lock)
127550a7375SFelipe Balbi __acquires(ep->musb->lock)
128550a7375SFelipe Balbi {
129550a7375SFelipe Balbi 	struct musb_request	*req;
130550a7375SFelipe Balbi 	struct musb		*musb;
131550a7375SFelipe Balbi 	int			busy = ep->busy;
132550a7375SFelipe Balbi 
133550a7375SFelipe Balbi 	req = to_musb_request(request);
134550a7375SFelipe Balbi 
135ad1adb89SFelipe Balbi 	list_del(&req->list);
136550a7375SFelipe Balbi 	if (req->request.status == -EINPROGRESS)
137550a7375SFelipe Balbi 		req->request.status = status;
138550a7375SFelipe Balbi 	musb = req->musb;
139550a7375SFelipe Balbi 
140550a7375SFelipe Balbi 	ep->busy = 1;
141550a7375SFelipe Balbi 	spin_unlock(&musb->lock);
14206d9db72SKishon Vijay Abraham I 
14306d9db72SKishon Vijay Abraham I 	if (!dma_mapping_error(&musb->g.dev, request->dma))
14492d2711fSHema Kalliguddi 		unmap_dma_buffer(req, musb);
14506d9db72SKishon Vijay Abraham I 
146fc78003eSBin Liu 	trace_musb_req_gb(req);
147304f7e5eSMichal Sojka 	usb_gadget_giveback_request(&req->ep->end_point, &req->request);
148550a7375SFelipe Balbi 	spin_lock(&musb->lock);
149550a7375SFelipe Balbi 	ep->busy = busy;
150550a7375SFelipe Balbi }
151550a7375SFelipe Balbi 
152550a7375SFelipe Balbi /* ----------------------------------------------------------------------- */
153550a7375SFelipe Balbi 
154550a7375SFelipe Balbi /*
155550a7375SFelipe Balbi  * Abort requests queued to an endpoint using the status. Synchronous.
156550a7375SFelipe Balbi  * caller locked controller and blocked irqs, and selected this ep.
157550a7375SFelipe Balbi  */
nuke(struct musb_ep * ep,const int status)158550a7375SFelipe Balbi static void nuke(struct musb_ep *ep, const int status)
159550a7375SFelipe Balbi {
1605c8a86e1SFelipe Balbi 	struct musb		*musb = ep->musb;
161550a7375SFelipe Balbi 	struct musb_request	*req = NULL;
162550a7375SFelipe Balbi 	void __iomem *epio = ep->musb->endpoints[ep->current_epnum].regs;
163550a7375SFelipe Balbi 
164550a7375SFelipe Balbi 	ep->busy = 1;
165550a7375SFelipe Balbi 
166550a7375SFelipe Balbi 	if (is_dma_capable() && ep->dma) {
167550a7375SFelipe Balbi 		struct dma_controller	*c = ep->musb->dma_controller;
168550a7375SFelipe Balbi 		int value;
169b6e434a5SSergei Shtylyov 
170550a7375SFelipe Balbi 		if (ep->is_in) {
171b6e434a5SSergei Shtylyov 			/*
172b6e434a5SSergei Shtylyov 			 * The programming guide says that we must not clear
173b6e434a5SSergei Shtylyov 			 * the DMAMODE bit before DMAENAB, so we only
174b6e434a5SSergei Shtylyov 			 * clear it in the second write...
175b6e434a5SSergei Shtylyov 			 */
176550a7375SFelipe Balbi 			musb_writew(epio, MUSB_TXCSR,
177b6e434a5SSergei Shtylyov 				    MUSB_TXCSR_DMAMODE | MUSB_TXCSR_FLUSHFIFO);
178550a7375SFelipe Balbi 			musb_writew(epio, MUSB_TXCSR,
179550a7375SFelipe Balbi 					0 | MUSB_TXCSR_FLUSHFIFO);
180550a7375SFelipe Balbi 		} else {
181550a7375SFelipe Balbi 			musb_writew(epio, MUSB_RXCSR,
182550a7375SFelipe Balbi 					0 | MUSB_RXCSR_FLUSHFIFO);
183550a7375SFelipe Balbi 			musb_writew(epio, MUSB_RXCSR,
184550a7375SFelipe Balbi 					0 | MUSB_RXCSR_FLUSHFIFO);
185550a7375SFelipe Balbi 		}
186550a7375SFelipe Balbi 
187550a7375SFelipe Balbi 		value = c->channel_abort(ep->dma);
188b99d3659SBin Liu 		musb_dbg(musb, "%s: abort DMA --> %d", ep->name, value);
189550a7375SFelipe Balbi 		c->channel_release(ep->dma);
190550a7375SFelipe Balbi 		ep->dma = NULL;
191550a7375SFelipe Balbi 	}
192550a7375SFelipe Balbi 
193ad1adb89SFelipe Balbi 	while (!list_empty(&ep->req_list)) {
194ad1adb89SFelipe Balbi 		req = list_first_entry(&ep->req_list, struct musb_request, list);
195550a7375SFelipe Balbi 		musb_g_giveback(ep, &req->request, status);
196550a7375SFelipe Balbi 	}
197550a7375SFelipe Balbi }
198550a7375SFelipe Balbi 
199550a7375SFelipe Balbi /* ----------------------------------------------------------------------- */
200550a7375SFelipe Balbi 
201550a7375SFelipe Balbi /* Data transfers - pure PIO, pure DMA, or mixed mode */
202550a7375SFelipe Balbi 
203550a7375SFelipe Balbi /*
204550a7375SFelipe Balbi  * This assumes the separate CPPI engine is responding to DMA requests
205550a7375SFelipe Balbi  * from the usb core ... sequenced a bit differently from mentor dma.
206550a7375SFelipe Balbi  */
207550a7375SFelipe Balbi 
max_ep_writesize(struct musb * musb,struct musb_ep * ep)208550a7375SFelipe Balbi static inline int max_ep_writesize(struct musb *musb, struct musb_ep *ep)
209550a7375SFelipe Balbi {
210550a7375SFelipe Balbi 	if (can_bulk_split(musb, ep->type))
211550a7375SFelipe Balbi 		return ep->hw_ep->max_packet_sz_tx;
212550a7375SFelipe Balbi 	else
213550a7375SFelipe Balbi 		return ep->packet_sz;
214550a7375SFelipe Balbi }
215550a7375SFelipe Balbi 
216550a7375SFelipe Balbi /*
217550a7375SFelipe Balbi  * An endpoint is transmitting data. This can be called either from
218550a7375SFelipe Balbi  * the IRQ routine or from ep.queue() to kickstart a request on an
219550a7375SFelipe Balbi  * endpoint.
220550a7375SFelipe Balbi  *
221550a7375SFelipe Balbi  * Context: controller locked, IRQs blocked, endpoint selected
222550a7375SFelipe Balbi  */
txstate(struct musb * musb,struct musb_request * req)223550a7375SFelipe Balbi static void txstate(struct musb *musb, struct musb_request *req)
224550a7375SFelipe Balbi {
225550a7375SFelipe Balbi 	u8			epnum = req->epnum;
226550a7375SFelipe Balbi 	struct musb_ep		*musb_ep;
227550a7375SFelipe Balbi 	void __iomem		*epio = musb->endpoints[epnum].regs;
228550a7375SFelipe Balbi 	struct usb_request	*request;
229550a7375SFelipe Balbi 	u16			fifo_count = 0, csr;
230550a7375SFelipe Balbi 	int			use_dma = 0;
231550a7375SFelipe Balbi 
232550a7375SFelipe Balbi 	musb_ep = req->ep;
233550a7375SFelipe Balbi 
234abf710e6SVikram Pandita 	/* Check if EP is disabled */
235abf710e6SVikram Pandita 	if (!musb_ep->desc) {
236b99d3659SBin Liu 		musb_dbg(musb, "ep:%s disabled - ignore request",
237abf710e6SVikram Pandita 						musb_ep->end_point.name);
238abf710e6SVikram Pandita 		return;
239abf710e6SVikram Pandita 	}
240abf710e6SVikram Pandita 
241550a7375SFelipe Balbi 	/* we shouldn't get here while DMA is active ... but we do ... */
242550a7375SFelipe Balbi 	if (dma_channel_status(musb_ep->dma) == MUSB_DMA_STATUS_BUSY) {
243b99d3659SBin Liu 		musb_dbg(musb, "dma pending...");
244550a7375SFelipe Balbi 		return;
245550a7375SFelipe Balbi 	}
246550a7375SFelipe Balbi 
247550a7375SFelipe Balbi 	/* read TXCSR before */
248550a7375SFelipe Balbi 	csr = musb_readw(epio, MUSB_TXCSR);
249550a7375SFelipe Balbi 
250550a7375SFelipe Balbi 	request = &req->request;
251550a7375SFelipe Balbi 	fifo_count = min(max_ep_writesize(musb, musb_ep),
252550a7375SFelipe Balbi 			(int)(request->length - request->actual));
253550a7375SFelipe Balbi 
254550a7375SFelipe Balbi 	if (csr & MUSB_TXCSR_TXPKTRDY) {
255b99d3659SBin Liu 		musb_dbg(musb, "%s old packet still ready , txcsr %03x",
256550a7375SFelipe Balbi 				musb_ep->end_point.name, csr);
257550a7375SFelipe Balbi 		return;
258550a7375SFelipe Balbi 	}
259550a7375SFelipe Balbi 
260550a7375SFelipe Balbi 	if (csr & MUSB_TXCSR_P_SENDSTALL) {
261b99d3659SBin Liu 		musb_dbg(musb, "%s stalling, txcsr %03x",
262550a7375SFelipe Balbi 				musb_ep->end_point.name, csr);
263550a7375SFelipe Balbi 		return;
264550a7375SFelipe Balbi 	}
265550a7375SFelipe Balbi 
266b99d3659SBin Liu 	musb_dbg(musb, "hw_ep%d, maxpacket %d, fifo count %d, txcsr %03x",
267550a7375SFelipe Balbi 			epnum, musb_ep->packet_sz, fifo_count,
268550a7375SFelipe Balbi 			csr);
269550a7375SFelipe Balbi 
270550a7375SFelipe Balbi #ifndef	CONFIG_MUSB_PIO_ONLY
271c65bfa62SMian Yousaf Kaukab 	if (is_buffer_mapped(req)) {
272550a7375SFelipe Balbi 		struct dma_controller	*c = musb->dma_controller;
27366af83ddSMing Lei 		size_t request_size;
27466af83ddSMing Lei 
27566af83ddSMing Lei 		/* setup DMA, then program endpoint CSR */
27666af83ddSMing Lei 		request_size = min_t(size_t, request->length - request->actual,
27766af83ddSMing Lei 					musb_ep->dma->max_len);
278550a7375SFelipe Balbi 
279d17d535fSAjay Kumar Gupta 		use_dma = (request->dma != DMA_ADDR_INVALID && request_size);
280550a7375SFelipe Balbi 
281550a7375SFelipe Balbi 		/* MUSB_TXCSR_P_ISO is still set correctly */
282550a7375SFelipe Balbi 
28303840fadSFelipe Balbi 		if (musb_dma_inventra(musb) || musb_dma_ux500(musb)) {
284d1043a26SAnand Gadiyar 			if (request_size < musb_ep->packet_sz)
285550a7375SFelipe Balbi 				musb_ep->dma->desired_mode = 0;
286550a7375SFelipe Balbi 			else
287550a7375SFelipe Balbi 				musb_ep->dma->desired_mode = 1;
288550a7375SFelipe Balbi 
289550a7375SFelipe Balbi 			use_dma = use_dma && c->channel_program(
290550a7375SFelipe Balbi 					musb_ep->dma, musb_ep->packet_sz,
291550a7375SFelipe Balbi 					musb_ep->dma->desired_mode,
292796a83faSCliff Cai 					request->dma + request->actual, request_size);
293550a7375SFelipe Balbi 			if (use_dma) {
294550a7375SFelipe Balbi 				if (musb_ep->dma->desired_mode == 0) {
295b6e434a5SSergei Shtylyov 					/*
296b6e434a5SSergei Shtylyov 					 * We must not clear the DMAMODE bit
297b6e434a5SSergei Shtylyov 					 * before the DMAENAB bit -- and the
298b6e434a5SSergei Shtylyov 					 * latter doesn't always get cleared
299b6e434a5SSergei Shtylyov 					 * before we get here...
300b6e434a5SSergei Shtylyov 					 */
301b6e434a5SSergei Shtylyov 					csr &= ~(MUSB_TXCSR_AUTOSET
302b6e434a5SSergei Shtylyov 						| MUSB_TXCSR_DMAENAB);
303b6e434a5SSergei Shtylyov 					musb_writew(epio, MUSB_TXCSR, csr
304b6e434a5SSergei Shtylyov 						| MUSB_TXCSR_P_WZC_BITS);
305b6e434a5SSergei Shtylyov 					csr &= ~MUSB_TXCSR_DMAMODE;
306550a7375SFelipe Balbi 					csr |= (MUSB_TXCSR_DMAENAB |
307550a7375SFelipe Balbi 							MUSB_TXCSR_MODE);
308550a7375SFelipe Balbi 					/* against programming guide */
309f11d893dSMing Lei 				} else {
310f11d893dSMing Lei 					csr |= (MUSB_TXCSR_DMAENAB
311550a7375SFelipe Balbi 							| MUSB_TXCSR_DMAMODE
312550a7375SFelipe Balbi 							| MUSB_TXCSR_MODE);
313bb3a2ef2Ssupriya karanth 					/*
314bb3a2ef2Ssupriya karanth 					 * Enable Autoset according to table
315bb3a2ef2Ssupriya karanth 					 * below
316bb3a2ef2Ssupriya karanth 					 * bulk_split hb_mult	Autoset_Enable
317bb3a2ef2Ssupriya karanth 					 *	0	0	Yes(Normal)
318bb3a2ef2Ssupriya karanth 					 *	0	>0	No(High BW ISO)
319bb3a2ef2Ssupriya karanth 					 *	1	0	Yes(HS bulk)
320bb3a2ef2Ssupriya karanth 					 *	1	>0	Yes(FS bulk)
321bb3a2ef2Ssupriya karanth 					 */
322bb3a2ef2Ssupriya karanth 					if (!musb_ep->hb_mult ||
323bb3a2ef2Ssupriya karanth 					    can_bulk_split(musb,
3241a171626SGeyslan G. Bem 							   musb_ep->type))
325f11d893dSMing Lei 						csr |= MUSB_TXCSR_AUTOSET;
326f11d893dSMing Lei 				}
327550a7375SFelipe Balbi 				csr &= ~MUSB_TXCSR_P_UNDERRUN;
328f11d893dSMing Lei 
329550a7375SFelipe Balbi 				musb_writew(epio, MUSB_TXCSR, csr);
330550a7375SFelipe Balbi 			}
331550a7375SFelipe Balbi 		}
332550a7375SFelipe Balbi 
333f8e9f34fSTony Lindgren 		if (is_cppi_enabled(musb)) {
334550a7375SFelipe Balbi 			/* program endpoint CSR first, then setup DMA */
335b6e434a5SSergei Shtylyov 			csr &= ~(MUSB_TXCSR_P_UNDERRUN | MUSB_TXCSR_TXPKTRDY);
33637e3ee99SSergei Shtylyov 			csr |= MUSB_TXCSR_DMAENAB | MUSB_TXCSR_DMAMODE |
33737e3ee99SSergei Shtylyov 				MUSB_TXCSR_MODE;
338fc525751SSebastian Andrzej Siewior 			musb_writew(epio, MUSB_TXCSR, (MUSB_TXCSR_P_WZC_BITS &
339fc525751SSebastian Andrzej Siewior 						~MUSB_TXCSR_P_UNDERRUN) | csr);
340550a7375SFelipe Balbi 
341550a7375SFelipe Balbi 			/* ensure writebuffer is empty */
342550a7375SFelipe Balbi 			csr = musb_readw(epio, MUSB_TXCSR);
343550a7375SFelipe Balbi 
344fc525751SSebastian Andrzej Siewior 			/*
345fc525751SSebastian Andrzej Siewior 			 * NOTE host side sets DMAENAB later than this; both are
346fc525751SSebastian Andrzej Siewior 			 * OK since the transfer dma glue (between CPPI and
347fc525751SSebastian Andrzej Siewior 			 * Mentor fifos) just tells CPPI it could start. Data
348fc525751SSebastian Andrzej Siewior 			 * only moves to the USB TX fifo when both fifos are
349fc525751SSebastian Andrzej Siewior 			 * ready.
350550a7375SFelipe Balbi 			 */
351fc525751SSebastian Andrzej Siewior 			/*
352fc525751SSebastian Andrzej Siewior 			 * "mode" is irrelevant here; handle terminating ZLPs
353fc525751SSebastian Andrzej Siewior 			 * like PIO does, since the hardware RNDIS mode seems
354fc525751SSebastian Andrzej Siewior 			 * unreliable except for the
355fc525751SSebastian Andrzej Siewior 			 * last-packet-is-already-short case.
356550a7375SFelipe Balbi 			 */
357550a7375SFelipe Balbi 			use_dma = use_dma && c->channel_program(
358550a7375SFelipe Balbi 					musb_ep->dma, musb_ep->packet_sz,
359550a7375SFelipe Balbi 					0,
36066af83ddSMing Lei 					request->dma + request->actual,
36166af83ddSMing Lei 					request_size);
362550a7375SFelipe Balbi 			if (!use_dma) {
363550a7375SFelipe Balbi 				c->channel_release(musb_ep->dma);
364550a7375SFelipe Balbi 				musb_ep->dma = NULL;
365b6e434a5SSergei Shtylyov 				csr &= ~MUSB_TXCSR_DMAENAB;
366b6e434a5SSergei Shtylyov 				musb_writew(epio, MUSB_TXCSR, csr);
367550a7375SFelipe Balbi 				/* invariant: prequest->buf is non-null */
368550a7375SFelipe Balbi 			}
369f8e9f34fSTony Lindgren 		} else if (tusb_dma_omap(musb))
370550a7375SFelipe Balbi 			use_dma = use_dma && c->channel_program(
371550a7375SFelipe Balbi 					musb_ep->dma, musb_ep->packet_sz,
372550a7375SFelipe Balbi 					request->zero,
37366af83ddSMing Lei 					request->dma + request->actual,
37466af83ddSMing Lei 					request_size);
375550a7375SFelipe Balbi 	}
376550a7375SFelipe Balbi #endif
377550a7375SFelipe Balbi 
378550a7375SFelipe Balbi 	if (!use_dma) {
37992d2711fSHema Kalliguddi 		/*
38092d2711fSHema Kalliguddi 		 * Unmap the dma buffer back to cpu if dma channel
38192d2711fSHema Kalliguddi 		 * programming fails
38292d2711fSHema Kalliguddi 		 */
38392d2711fSHema Kalliguddi 		unmap_dma_buffer(req, musb);
38492d2711fSHema Kalliguddi 
385550a7375SFelipe Balbi 		musb_write_fifo(musb_ep->hw_ep, fifo_count,
386550a7375SFelipe Balbi 				(u8 *) (request->buf + request->actual));
387550a7375SFelipe Balbi 		request->actual += fifo_count;
388550a7375SFelipe Balbi 		csr |= MUSB_TXCSR_TXPKTRDY;
389550a7375SFelipe Balbi 		csr &= ~MUSB_TXCSR_P_UNDERRUN;
390550a7375SFelipe Balbi 		musb_writew(epio, MUSB_TXCSR, csr);
391550a7375SFelipe Balbi 	}
392550a7375SFelipe Balbi 
393550a7375SFelipe Balbi 	/* host may already have the data when this message shows... */
394b99d3659SBin Liu 	musb_dbg(musb, "%s TX/IN %s len %d/%d, txcsr %04x, fifo %d/%d",
395550a7375SFelipe Balbi 			musb_ep->end_point.name, use_dma ? "dma" : "pio",
396550a7375SFelipe Balbi 			request->actual, request->length,
397550a7375SFelipe Balbi 			musb_readw(epio, MUSB_TXCSR),
398550a7375SFelipe Balbi 			fifo_count,
399550a7375SFelipe Balbi 			musb_readw(epio, MUSB_TXMAXP));
400550a7375SFelipe Balbi }
401550a7375SFelipe Balbi 
402550a7375SFelipe Balbi /*
403550a7375SFelipe Balbi  * FIFO state update (e.g. data ready).
404550a7375SFelipe Balbi  * Called from IRQ,  with controller locked.
405550a7375SFelipe Balbi  */
musb_g_tx(struct musb * musb,u8 epnum)406550a7375SFelipe Balbi void musb_g_tx(struct musb *musb, u8 epnum)
407550a7375SFelipe Balbi {
408550a7375SFelipe Balbi 	u16			csr;
409ad1adb89SFelipe Balbi 	struct musb_request	*req;
410550a7375SFelipe Balbi 	struct usb_request	*request;
411550a7375SFelipe Balbi 	u8 __iomem		*mbase = musb->mregs;
412550a7375SFelipe Balbi 	struct musb_ep		*musb_ep = &musb->endpoints[epnum].ep_in;
413550a7375SFelipe Balbi 	void __iomem		*epio = musb->endpoints[epnum].regs;
414550a7375SFelipe Balbi 	struct dma_channel	*dma;
415550a7375SFelipe Balbi 
416550a7375SFelipe Balbi 	musb_ep_select(mbase, epnum);
417ad1adb89SFelipe Balbi 	req = next_request(musb_ep);
418ad1adb89SFelipe Balbi 	request = &req->request;
419550a7375SFelipe Balbi 
420550a7375SFelipe Balbi 	csr = musb_readw(epio, MUSB_TXCSR);
421b99d3659SBin Liu 	musb_dbg(musb, "<== %s, txcsr %04x", musb_ep->end_point.name, csr);
422550a7375SFelipe Balbi 
423550a7375SFelipe Balbi 	dma = is_dma_capable() ? musb_ep->dma : NULL;
4247723de7eSSergei Shtylyov 
4257723de7eSSergei Shtylyov 	/*
4267723de7eSSergei Shtylyov 	 * REVISIT: for high bandwidth, MUSB_TXCSR_P_INCOMPTX
4277723de7eSSergei Shtylyov 	 * probably rates reporting as a host error.
428550a7375SFelipe Balbi 	 */
429550a7375SFelipe Balbi 	if (csr & MUSB_TXCSR_P_SENTSTALL) {
430550a7375SFelipe Balbi 		csr |=	MUSB_TXCSR_P_WZC_BITS;
431550a7375SFelipe Balbi 		csr &= ~MUSB_TXCSR_P_SENTSTALL;
432550a7375SFelipe Balbi 		musb_writew(epio, MUSB_TXCSR, csr);
4337723de7eSSergei Shtylyov 		return;
434550a7375SFelipe Balbi 	}
435550a7375SFelipe Balbi 
436550a7375SFelipe Balbi 	if (csr & MUSB_TXCSR_P_UNDERRUN) {
4377723de7eSSergei Shtylyov 		/* We NAKed, no big deal... little reason to care. */
438550a7375SFelipe Balbi 		csr |=	 MUSB_TXCSR_P_WZC_BITS;
4397723de7eSSergei Shtylyov 		csr &= ~(MUSB_TXCSR_P_UNDERRUN | MUSB_TXCSR_TXPKTRDY);
440550a7375SFelipe Balbi 		musb_writew(epio, MUSB_TXCSR, csr);
4415c8a86e1SFelipe Balbi 		dev_vdbg(musb->controller, "underrun on ep%d, req %p\n",
4425c8a86e1SFelipe Balbi 				epnum, request);
443550a7375SFelipe Balbi 	}
444550a7375SFelipe Balbi 
445550a7375SFelipe Balbi 	if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) {
4467723de7eSSergei Shtylyov 		/*
4477723de7eSSergei Shtylyov 		 * SHOULD NOT HAPPEN... has with CPPI though, after
448550a7375SFelipe Balbi 		 * changing SENDSTALL (and other cases); harmless?
449550a7375SFelipe Balbi 		 */
450b99d3659SBin Liu 		musb_dbg(musb, "%s dma still busy?", musb_ep->end_point.name);
4517723de7eSSergei Shtylyov 		return;
452550a7375SFelipe Balbi 	}
453550a7375SFelipe Balbi 
454ae2938c3SPaul Cercueil 	if (req) {
455550a7375SFelipe Balbi 
4569aea9b6cSBin Liu 		trace_musb_req_tx(req);
4579aea9b6cSBin Liu 
458550a7375SFelipe Balbi 		if (dma && (csr & MUSB_TXCSR_DMAENAB)) {
459550a7375SFelipe Balbi 			csr |= MUSB_TXCSR_P_WZC_BITS;
4607723de7eSSergei Shtylyov 			csr &= ~(MUSB_TXCSR_DMAENAB | MUSB_TXCSR_P_UNDERRUN |
461100d4a9dSMian Yousaf Kaukab 				 MUSB_TXCSR_TXPKTRDY | MUSB_TXCSR_AUTOSET);
462550a7375SFelipe Balbi 			musb_writew(epio, MUSB_TXCSR, csr);
4637723de7eSSergei Shtylyov 			/* Ensure writebuffer is empty. */
464550a7375SFelipe Balbi 			csr = musb_readw(epio, MUSB_TXCSR);
465550a7375SFelipe Balbi 			request->actual += musb_ep->dma->actual_len;
466b99d3659SBin Liu 			musb_dbg(musb, "TXCSR%d %04x, DMA off, len %zu, req %p",
4677723de7eSSergei Shtylyov 				epnum, csr, musb_ep->dma->actual_len, request);
468550a7375SFelipe Balbi 		}
469550a7375SFelipe Balbi 
4707723de7eSSergei Shtylyov 		/*
4717723de7eSSergei Shtylyov 		 * First, maybe a terminating short packet. Some DMA
4727723de7eSSergei Shtylyov 		 * engines might handle this by themselves.
473550a7375SFelipe Balbi 		 */
474fb91cddcSTony Lindgren 		if ((request->zero && request->length)
475e7379aaaSMing Lei 			&& (request->length % musb_ep->packet_sz == 0)
476c418fd6cSPaul Elder 			&& (request->actual == request->length)) {
477fb91cddcSTony Lindgren 
4787723de7eSSergei Shtylyov 			/*
4797723de7eSSergei Shtylyov 			 * On DMA completion, FIFO may not be
4807723de7eSSergei Shtylyov 			 * available yet...
481550a7375SFelipe Balbi 			 */
482550a7375SFelipe Balbi 			if (csr & MUSB_TXCSR_TXPKTRDY)
4837723de7eSSergei Shtylyov 				return;
484550a7375SFelipe Balbi 
4857723de7eSSergei Shtylyov 			musb_writew(epio, MUSB_TXCSR, MUSB_TXCSR_MODE
486550a7375SFelipe Balbi 					| MUSB_TXCSR_TXPKTRDY);
487550a7375SFelipe Balbi 			request->zero = 0;
488550a7375SFelipe Balbi 		}
489550a7375SFelipe Balbi 
490bb27bc2cSMing Lei 		if (request->actual == request->length) {
491550a7375SFelipe Balbi 			musb_g_giveback(musb_ep, request, 0);
49239287076SSupriya Karanth 			/*
49339287076SSupriya Karanth 			 * In the giveback function the MUSB lock is
49439287076SSupriya Karanth 			 * released and acquired after sometime. During
49539287076SSupriya Karanth 			 * this time period the INDEX register could get
49639287076SSupriya Karanth 			 * changed by the gadget_queue function especially
49739287076SSupriya Karanth 			 * on SMP systems. Reselect the INDEX to be sure
49839287076SSupriya Karanth 			 * we are reading/modifying the right registers
49939287076SSupriya Karanth 			 */
50039287076SSupriya Karanth 			musb_ep_select(mbase, epnum);
501ad1adb89SFelipe Balbi 			req = musb_ep->desc ? next_request(musb_ep) : NULL;
502ad1adb89SFelipe Balbi 			if (!req) {
503b99d3659SBin Liu 				musb_dbg(musb, "%s idle now",
504550a7375SFelipe Balbi 					musb_ep->end_point.name);
5057723de7eSSergei Shtylyov 				return;
50695962a77SSergei Shtylyov 			}
507550a7375SFelipe Balbi 		}
508550a7375SFelipe Balbi 
509ad1adb89SFelipe Balbi 		txstate(musb, req);
510550a7375SFelipe Balbi 	}
511550a7375SFelipe Balbi }
512550a7375SFelipe Balbi 
513550a7375SFelipe Balbi /* ------------------------------------------------------------ */
514550a7375SFelipe Balbi 
515550a7375SFelipe Balbi /*
516550a7375SFelipe Balbi  * Context: controller locked, IRQs blocked, endpoint selected
517550a7375SFelipe Balbi  */
rxstate(struct musb * musb,struct musb_request * req)518550a7375SFelipe Balbi static void rxstate(struct musb *musb, struct musb_request *req)
519550a7375SFelipe Balbi {
520550a7375SFelipe Balbi 	const u8		epnum = req->epnum;
521550a7375SFelipe Balbi 	struct usb_request	*request = &req->request;
522bd2e74d6SMing Lei 	struct musb_ep		*musb_ep;
523550a7375SFelipe Balbi 	void __iomem		*epio = musb->endpoints[epnum].regs;
524f0443afdSSergei Shtylyov 	unsigned		len = 0;
525f0443afdSSergei Shtylyov 	u16			fifo_count;
526cea83241SSergei Shtylyov 	u16			csr = musb_readw(epio, MUSB_RXCSR);
527bd2e74d6SMing Lei 	struct musb_hw_ep	*hw_ep = &musb->endpoints[epnum];
5280ae52d54SAnand Gadiyar 	u8			use_mode_1;
529bd2e74d6SMing Lei 
530bd2e74d6SMing Lei 	if (hw_ep->is_shared_fifo)
531bd2e74d6SMing Lei 		musb_ep = &hw_ep->ep_in;
532bd2e74d6SMing Lei 	else
533bd2e74d6SMing Lei 		musb_ep = &hw_ep->ep_out;
534bd2e74d6SMing Lei 
535f0443afdSSergei Shtylyov 	fifo_count = musb_ep->packet_sz;
536550a7375SFelipe Balbi 
537abf710e6SVikram Pandita 	/* Check if EP is disabled */
538abf710e6SVikram Pandita 	if (!musb_ep->desc) {
539b99d3659SBin Liu 		musb_dbg(musb, "ep:%s disabled - ignore request",
540abf710e6SVikram Pandita 						musb_ep->end_point.name);
541abf710e6SVikram Pandita 		return;
542abf710e6SVikram Pandita 	}
543abf710e6SVikram Pandita 
544cea83241SSergei Shtylyov 	/* We shouldn't get here while DMA is active, but we do... */
545cea83241SSergei Shtylyov 	if (dma_channel_status(musb_ep->dma) == MUSB_DMA_STATUS_BUSY) {
546b99d3659SBin Liu 		musb_dbg(musb, "DMA pending...");
547cea83241SSergei Shtylyov 		return;
548cea83241SSergei Shtylyov 	}
549cea83241SSergei Shtylyov 
550cea83241SSergei Shtylyov 	if (csr & MUSB_RXCSR_P_SENDSTALL) {
551b99d3659SBin Liu 		musb_dbg(musb, "%s stalling, RXCSR %04x",
552cea83241SSergei Shtylyov 		    musb_ep->end_point.name, csr);
553cea83241SSergei Shtylyov 		return;
554cea83241SSergei Shtylyov 	}
555550a7375SFelipe Balbi 
556f8e9f34fSTony Lindgren 	if (is_cppi_enabled(musb) && is_buffer_mapped(req)) {
557550a7375SFelipe Balbi 		struct dma_controller	*c = musb->dma_controller;
558550a7375SFelipe Balbi 		struct dma_channel	*channel = musb_ep->dma;
559550a7375SFelipe Balbi 
560550a7375SFelipe Balbi 		/* NOTE:  CPPI won't actually stop advancing the DMA
561550a7375SFelipe Balbi 		 * queue after short packet transfers, so this is almost
562550a7375SFelipe Balbi 		 * always going to run as IRQ-per-packet DMA so that
563550a7375SFelipe Balbi 		 * faults will be handled correctly.
564550a7375SFelipe Balbi 		 */
565550a7375SFelipe Balbi 		if (c->channel_program(channel,
566550a7375SFelipe Balbi 				musb_ep->packet_sz,
567550a7375SFelipe Balbi 				!request->short_not_ok,
568550a7375SFelipe Balbi 				request->dma + request->actual,
569550a7375SFelipe Balbi 				request->length - request->actual)) {
570550a7375SFelipe Balbi 
571550a7375SFelipe Balbi 			/* make sure that if an rxpkt arrived after the irq,
572550a7375SFelipe Balbi 			 * the cppi engine will be ready to take it as soon
573550a7375SFelipe Balbi 			 * as DMA is enabled
574550a7375SFelipe Balbi 			 */
575550a7375SFelipe Balbi 			csr &= ~(MUSB_RXCSR_AUTOCLEAR
576550a7375SFelipe Balbi 					| MUSB_RXCSR_DMAMODE);
577550a7375SFelipe Balbi 			csr |= MUSB_RXCSR_DMAENAB | MUSB_RXCSR_P_WZC_BITS;
578550a7375SFelipe Balbi 			musb_writew(epio, MUSB_RXCSR, csr);
579550a7375SFelipe Balbi 			return;
580550a7375SFelipe Balbi 		}
581550a7375SFelipe Balbi 	}
582550a7375SFelipe Balbi 
583550a7375SFelipe Balbi 	if (csr & MUSB_RXCSR_RXPKTRDY) {
584f0443afdSSergei Shtylyov 		fifo_count = musb_readw(epio, MUSB_RXCOUNT);
5850ae52d54SAnand Gadiyar 
5860ae52d54SAnand Gadiyar 		/*
58700a89180SFelipe Balbi 		 * Enable Mode 1 on RX transfers only when short_not_ok flag
58800a89180SFelipe Balbi 		 * is set. Currently short_not_ok flag is set only from
58900a89180SFelipe Balbi 		 * file_storage and f_mass_storage drivers
5900ae52d54SAnand Gadiyar 		 */
59100a89180SFelipe Balbi 
59200a89180SFelipe Balbi 		if (request->short_not_ok && fifo_count == musb_ep->packet_sz)
5930ae52d54SAnand Gadiyar 			use_mode_1 = 1;
5940ae52d54SAnand Gadiyar 		else
5950ae52d54SAnand Gadiyar 			use_mode_1 = 0;
5960ae52d54SAnand Gadiyar 
597550a7375SFelipe Balbi 		if (request->actual < request->length) {
59803840fadSFelipe Balbi 			if (!is_buffer_mapped(req))
59903840fadSFelipe Balbi 				goto buffer_aint_mapped;
60003840fadSFelipe Balbi 
60103840fadSFelipe Balbi 			if (musb_dma_inventra(musb)) {
602550a7375SFelipe Balbi 				struct dma_controller	*c;
603550a7375SFelipe Balbi 				struct dma_channel	*channel;
604550a7375SFelipe Balbi 				int			use_dma = 0;
60537730eccSFelipe Balbi 				unsigned int transfer_size;
606550a7375SFelipe Balbi 
607550a7375SFelipe Balbi 				c = musb->dma_controller;
608550a7375SFelipe Balbi 				channel = musb_ep->dma;
609550a7375SFelipe Balbi 
61000a89180SFelipe Balbi 	/* We use DMA Req mode 0 in rx_csr, and DMA controller operates in
61100a89180SFelipe Balbi 	 * mode 0 only. So we do not get endpoint interrupts due to DMA
61200a89180SFelipe Balbi 	 * completion. We only get interrupts from DMA controller.
61300a89180SFelipe Balbi 	 *
6140826dae3SSaurav Girepunje 	 * We could operate in DMA mode 1 if we knew the size of the transfer
61500a89180SFelipe Balbi 	 * in advance. For mass storage class, request->length = what the host
61600a89180SFelipe Balbi 	 * sends, so that'd work.  But for pretty much everything else,
61700a89180SFelipe Balbi 	 * request->length is routinely more than what the host sends. For
61800a89180SFelipe Balbi 	 * most these gadgets, end of is signified either by a short packet,
61900a89180SFelipe Balbi 	 * or filling the last byte of the buffer.  (Sending extra data in
62000a89180SFelipe Balbi 	 * that last pckate should trigger an overflow fault.)  But in mode 1,
62100a89180SFelipe Balbi 	 * we don't get DMA completion interrupt for short packets.
62200a89180SFelipe Balbi 	 *
62300a89180SFelipe Balbi 	 * Theoretically, we could enable DMAReq irq (MUSB_RXCSR_DMAMODE = 1),
62400a89180SFelipe Balbi 	 * to get endpoint interrupt on every DMA req, but that didn't seem
62500a89180SFelipe Balbi 	 * to work reliably.
62600a89180SFelipe Balbi 	 *
62700a89180SFelipe Balbi 	 * REVISIT an updated g_file_storage can set req->short_not_ok, which
62800a89180SFelipe Balbi 	 * then becomes usable as a runtime "use mode 1" hint...
62900a89180SFelipe Balbi 	 */
63000a89180SFelipe Balbi 
6310ae52d54SAnand Gadiyar 				/* Experimental: Mode1 works with mass storage use cases */
6320ae52d54SAnand Gadiyar 				if (use_mode_1) {
6339001d80dSMing Lei 					csr |= MUSB_RXCSR_AUTOCLEAR;
6340ae52d54SAnand Gadiyar 					musb_writew(epio, MUSB_RXCSR, csr);
6350ae52d54SAnand Gadiyar 					csr |= MUSB_RXCSR_DMAENAB;
6360ae52d54SAnand Gadiyar 					musb_writew(epio, MUSB_RXCSR, csr);
637550a7375SFelipe Balbi 
6380ae52d54SAnand Gadiyar 					/*
6390ae52d54SAnand Gadiyar 					 * this special sequence (enabling and then
640550a7375SFelipe Balbi 					 * disabling MUSB_RXCSR_DMAMODE) is required
641550a7375SFelipe Balbi 					 * to get DMAReq to activate
642550a7375SFelipe Balbi 					 */
643550a7375SFelipe Balbi 					musb_writew(epio, MUSB_RXCSR,
644550a7375SFelipe Balbi 						csr | MUSB_RXCSR_DMAMODE);
6450ae52d54SAnand Gadiyar 					musb_writew(epio, MUSB_RXCSR, csr);
6460ae52d54SAnand Gadiyar 
64737730eccSFelipe Balbi 					transfer_size = min_t(unsigned int,
64837730eccSFelipe Balbi 							request->length -
64937730eccSFelipe Balbi 							request->actual,
650660fa886SRoger Quadros 							channel->max_len);
651660fa886SRoger Quadros 					musb_ep->dma->desired_mode = 1;
6520ae52d54SAnand Gadiyar 				} else {
6539001d80dSMing Lei 					if (!musb_ep->hb_mult &&
6549001d80dSMing Lei 						musb_ep->hw_ep->rx_double_buffered)
6559001d80dSMing Lei 						csr |= MUSB_RXCSR_AUTOCLEAR;
6560ae52d54SAnand Gadiyar 					csr |= MUSB_RXCSR_DMAENAB;
657550a7375SFelipe Balbi 					musb_writew(epio, MUSB_RXCSR, csr);
658550a7375SFelipe Balbi 
6591018b4e4SMing Lei 					transfer_size = min(request->length - request->actual,
660f0443afdSSergei Shtylyov 							(unsigned)fifo_count);
661550a7375SFelipe Balbi 					musb_ep->dma->desired_mode = 0;
6620ae52d54SAnand Gadiyar 				}
663550a7375SFelipe Balbi 
664550a7375SFelipe Balbi 				use_dma = c->channel_program(
665550a7375SFelipe Balbi 						channel,
666550a7375SFelipe Balbi 						musb_ep->packet_sz,
667550a7375SFelipe Balbi 						channel->desired_mode,
668550a7375SFelipe Balbi 						request->dma
669550a7375SFelipe Balbi 						+ request->actual,
670550a7375SFelipe Balbi 						transfer_size);
671550a7375SFelipe Balbi 
672550a7375SFelipe Balbi 				if (use_dma)
673550a7375SFelipe Balbi 					return;
674550a7375SFelipe Balbi 			}
67503840fadSFelipe Balbi 
67603840fadSFelipe Balbi 			if ((musb_dma_ux500(musb)) &&
677a48ff906SMian Yousaf Kaukab 				(request->actual < request->length)) {
678a48ff906SMian Yousaf Kaukab 
679a48ff906SMian Yousaf Kaukab 				struct dma_controller *c;
680a48ff906SMian Yousaf Kaukab 				struct dma_channel *channel;
68137730eccSFelipe Balbi 				unsigned int transfer_size = 0;
682a48ff906SMian Yousaf Kaukab 
683a48ff906SMian Yousaf Kaukab 				c = musb->dma_controller;
684a48ff906SMian Yousaf Kaukab 				channel = musb_ep->dma;
685a48ff906SMian Yousaf Kaukab 
686a48ff906SMian Yousaf Kaukab 				/* In case first packet is short */
687f0443afdSSergei Shtylyov 				if (fifo_count < musb_ep->packet_sz)
688f0443afdSSergei Shtylyov 					transfer_size = fifo_count;
689a48ff906SMian Yousaf Kaukab 				else if (request->short_not_ok)
69037730eccSFelipe Balbi 					transfer_size =	min_t(unsigned int,
69137730eccSFelipe Balbi 							request->length -
692a48ff906SMian Yousaf Kaukab 							request->actual,
693a48ff906SMian Yousaf Kaukab 							channel->max_len);
694a48ff906SMian Yousaf Kaukab 				else
69537730eccSFelipe Balbi 					transfer_size = min_t(unsigned int,
69637730eccSFelipe Balbi 							request->length -
697a48ff906SMian Yousaf Kaukab 							request->actual,
698f0443afdSSergei Shtylyov 							(unsigned)fifo_count);
699a48ff906SMian Yousaf Kaukab 
700a48ff906SMian Yousaf Kaukab 				csr &= ~MUSB_RXCSR_DMAMODE;
701a48ff906SMian Yousaf Kaukab 				csr |= (MUSB_RXCSR_DMAENAB |
702a48ff906SMian Yousaf Kaukab 					MUSB_RXCSR_AUTOCLEAR);
703a48ff906SMian Yousaf Kaukab 
704a48ff906SMian Yousaf Kaukab 				musb_writew(epio, MUSB_RXCSR, csr);
705a48ff906SMian Yousaf Kaukab 
706a48ff906SMian Yousaf Kaukab 				if (transfer_size <= musb_ep->packet_sz) {
707a48ff906SMian Yousaf Kaukab 					musb_ep->dma->desired_mode = 0;
708a48ff906SMian Yousaf Kaukab 				} else {
709a48ff906SMian Yousaf Kaukab 					musb_ep->dma->desired_mode = 1;
710a48ff906SMian Yousaf Kaukab 					/* Mode must be set after DMAENAB */
711a48ff906SMian Yousaf Kaukab 					csr |= MUSB_RXCSR_DMAMODE;
712a48ff906SMian Yousaf Kaukab 					musb_writew(epio, MUSB_RXCSR, csr);
713a48ff906SMian Yousaf Kaukab 				}
714a48ff906SMian Yousaf Kaukab 
715a48ff906SMian Yousaf Kaukab 				if (c->channel_program(channel,
716a48ff906SMian Yousaf Kaukab 							musb_ep->packet_sz,
717a48ff906SMian Yousaf Kaukab 							channel->desired_mode,
718a48ff906SMian Yousaf Kaukab 							request->dma
719a48ff906SMian Yousaf Kaukab 							+ request->actual,
720a48ff906SMian Yousaf Kaukab 							transfer_size))
721a48ff906SMian Yousaf Kaukab 
722a48ff906SMian Yousaf Kaukab 					return;
723a48ff906SMian Yousaf Kaukab 			}
724550a7375SFelipe Balbi 
725f0443afdSSergei Shtylyov 			len = request->length - request->actual;
726b99d3659SBin Liu 			musb_dbg(musb, "%s OUT/RX pio fifo %d/%d, maxpacket %d",
727550a7375SFelipe Balbi 					musb_ep->end_point.name,
728f0443afdSSergei Shtylyov 					fifo_count, len,
729550a7375SFelipe Balbi 					musb_ep->packet_sz);
730550a7375SFelipe Balbi 
731c2c96321SFelipe Balbi 			fifo_count = min_t(unsigned, len, fifo_count);
732550a7375SFelipe Balbi 
73303840fadSFelipe Balbi 			if (tusb_dma_omap(musb)) {
734550a7375SFelipe Balbi 				struct dma_controller *c = musb->dma_controller;
735550a7375SFelipe Balbi 				struct dma_channel *channel = musb_ep->dma;
736550a7375SFelipe Balbi 				u32 dma_addr = request->dma + request->actual;
737550a7375SFelipe Balbi 				int ret;
738550a7375SFelipe Balbi 
739550a7375SFelipe Balbi 				ret = c->channel_program(channel,
740550a7375SFelipe Balbi 						musb_ep->packet_sz,
741550a7375SFelipe Balbi 						channel->desired_mode,
742550a7375SFelipe Balbi 						dma_addr,
743550a7375SFelipe Balbi 						fifo_count);
744550a7375SFelipe Balbi 				if (ret)
745550a7375SFelipe Balbi 					return;
746550a7375SFelipe Balbi 			}
74703840fadSFelipe Balbi 
74892d2711fSHema Kalliguddi 			/*
74992d2711fSHema Kalliguddi 			 * Unmap the dma buffer back to cpu if dma channel
75092d2711fSHema Kalliguddi 			 * programming fails. This buffer is mapped if the
75192d2711fSHema Kalliguddi 			 * channel allocation is successful
75292d2711fSHema Kalliguddi 			 */
75392d2711fSHema Kalliguddi 			unmap_dma_buffer(req, musb);
75492d2711fSHema Kalliguddi 
755e75df371SMing Lei 			/*
756e75df371SMing Lei 			 * Clear DMAENAB and AUTOCLEAR for the
75792d2711fSHema Kalliguddi 			 * PIO mode transfer
75892d2711fSHema Kalliguddi 			 */
759e75df371SMing Lei 			csr &= ~(MUSB_RXCSR_DMAENAB | MUSB_RXCSR_AUTOCLEAR);
76092d2711fSHema Kalliguddi 			musb_writew(epio, MUSB_RXCSR, csr);
761550a7375SFelipe Balbi 
76203840fadSFelipe Balbi buffer_aint_mapped:
763eea4c860SRobin Guo 			fifo_count = min_t(unsigned int,
764eea4c860SRobin Guo 					request->length - request->actual,
765eea4c860SRobin Guo 					(unsigned int)fifo_count);
766550a7375SFelipe Balbi 			musb_read_fifo(musb_ep->hw_ep, fifo_count, (u8 *)
767550a7375SFelipe Balbi 					(request->buf + request->actual));
768550a7375SFelipe Balbi 			request->actual += fifo_count;
769550a7375SFelipe Balbi 
770550a7375SFelipe Balbi 			/* REVISIT if we left anything in the fifo, flush
771550a7375SFelipe Balbi 			 * it and report -EOVERFLOW
772550a7375SFelipe Balbi 			 */
773550a7375SFelipe Balbi 
774550a7375SFelipe Balbi 			/* ack the read! */
775550a7375SFelipe Balbi 			csr |= MUSB_RXCSR_P_WZC_BITS;
776550a7375SFelipe Balbi 			csr &= ~MUSB_RXCSR_RXPKTRDY;
777550a7375SFelipe Balbi 			musb_writew(epio, MUSB_RXCSR, csr);
778550a7375SFelipe Balbi 		}
779550a7375SFelipe Balbi 	}
780550a7375SFelipe Balbi 
781550a7375SFelipe Balbi 	/* reach the end or short packet detected */
782f0443afdSSergei Shtylyov 	if (request->actual == request->length ||
783f0443afdSSergei Shtylyov 	    fifo_count < musb_ep->packet_sz)
784550a7375SFelipe Balbi 		musb_g_giveback(musb_ep, request, 0);
785550a7375SFelipe Balbi }
786550a7375SFelipe Balbi 
787550a7375SFelipe Balbi /*
788550a7375SFelipe Balbi  * Data ready for a request; called from IRQ
789550a7375SFelipe Balbi  */
musb_g_rx(struct musb * musb,u8 epnum)790550a7375SFelipe Balbi void musb_g_rx(struct musb *musb, u8 epnum)
791550a7375SFelipe Balbi {
792550a7375SFelipe Balbi 	u16			csr;
793ad1adb89SFelipe Balbi 	struct musb_request	*req;
794550a7375SFelipe Balbi 	struct usb_request	*request;
795550a7375SFelipe Balbi 	void __iomem		*mbase = musb->mregs;
796bd2e74d6SMing Lei 	struct musb_ep		*musb_ep;
797550a7375SFelipe Balbi 	void __iomem		*epio = musb->endpoints[epnum].regs;
798550a7375SFelipe Balbi 	struct dma_channel	*dma;
799bd2e74d6SMing Lei 	struct musb_hw_ep	*hw_ep = &musb->endpoints[epnum];
800bd2e74d6SMing Lei 
801bd2e74d6SMing Lei 	if (hw_ep->is_shared_fifo)
802bd2e74d6SMing Lei 		musb_ep = &hw_ep->ep_in;
803bd2e74d6SMing Lei 	else
804bd2e74d6SMing Lei 		musb_ep = &hw_ep->ep_out;
805550a7375SFelipe Balbi 
806550a7375SFelipe Balbi 	musb_ep_select(mbase, epnum);
807550a7375SFelipe Balbi 
808ad1adb89SFelipe Balbi 	req = next_request(musb_ep);
809ad1adb89SFelipe Balbi 	if (!req)
8100abdc36fSMaulik Mankad 		return;
811550a7375SFelipe Balbi 
812fc78003eSBin Liu 	trace_musb_req_rx(req);
813ad1adb89SFelipe Balbi 	request = &req->request;
814ad1adb89SFelipe Balbi 
815550a7375SFelipe Balbi 	csr = musb_readw(epio, MUSB_RXCSR);
816550a7375SFelipe Balbi 	dma = is_dma_capable() ? musb_ep->dma : NULL;
817550a7375SFelipe Balbi 
818b99d3659SBin Liu 	musb_dbg(musb, "<== %s, rxcsr %04x%s %p", musb_ep->end_point.name,
819550a7375SFelipe Balbi 			csr, dma ? " (dma)" : "", request);
820550a7375SFelipe Balbi 
821550a7375SFelipe Balbi 	if (csr & MUSB_RXCSR_P_SENTSTALL) {
822550a7375SFelipe Balbi 		csr |= MUSB_RXCSR_P_WZC_BITS;
823550a7375SFelipe Balbi 		csr &= ~MUSB_RXCSR_P_SENTSTALL;
824550a7375SFelipe Balbi 		musb_writew(epio, MUSB_RXCSR, csr);
825cea83241SSergei Shtylyov 		return;
826550a7375SFelipe Balbi 	}
827550a7375SFelipe Balbi 
828550a7375SFelipe Balbi 	if (csr & MUSB_RXCSR_P_OVERRUN) {
829550a7375SFelipe Balbi 		/* csr |= MUSB_RXCSR_P_WZC_BITS; */
830550a7375SFelipe Balbi 		csr &= ~MUSB_RXCSR_P_OVERRUN;
831550a7375SFelipe Balbi 		musb_writew(epio, MUSB_RXCSR, csr);
832550a7375SFelipe Balbi 
833b99d3659SBin Liu 		musb_dbg(musb, "%s iso overrun on %p", musb_ep->name, request);
83443467868SSergei Shtylyov 		if (request->status == -EINPROGRESS)
835550a7375SFelipe Balbi 			request->status = -EOVERFLOW;
836550a7375SFelipe Balbi 	}
837550a7375SFelipe Balbi 	if (csr & MUSB_RXCSR_INCOMPRX) {
838550a7375SFelipe Balbi 		/* REVISIT not necessarily an error */
839b99d3659SBin Liu 		musb_dbg(musb, "%s, incomprx", musb_ep->end_point.name);
840550a7375SFelipe Balbi 	}
841550a7375SFelipe Balbi 
842550a7375SFelipe Balbi 	if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) {
843550a7375SFelipe Balbi 		/* "should not happen"; likely RXPKTRDY pending for DMA */
844b99d3659SBin Liu 		musb_dbg(musb, "%s busy, csr %04x",
845550a7375SFelipe Balbi 			musb_ep->end_point.name, csr);
846cea83241SSergei Shtylyov 		return;
847550a7375SFelipe Balbi 	}
848550a7375SFelipe Balbi 
849550a7375SFelipe Balbi 	if (dma && (csr & MUSB_RXCSR_DMAENAB)) {
850550a7375SFelipe Balbi 		csr &= ~(MUSB_RXCSR_AUTOCLEAR
851550a7375SFelipe Balbi 				| MUSB_RXCSR_DMAENAB
852550a7375SFelipe Balbi 				| MUSB_RXCSR_DMAMODE);
853550a7375SFelipe Balbi 		musb_writew(epio, MUSB_RXCSR,
854550a7375SFelipe Balbi 			MUSB_RXCSR_P_WZC_BITS | csr);
855550a7375SFelipe Balbi 
856550a7375SFelipe Balbi 		request->actual += musb_ep->dma->actual_len;
857550a7375SFelipe Balbi 
858a48ff906SMian Yousaf Kaukab #if defined(CONFIG_USB_INVENTRA_DMA) || defined(CONFIG_USB_TUSB_OMAP_DMA) || \
859a48ff906SMian Yousaf Kaukab 	defined(CONFIG_USB_UX500_DMA)
860550a7375SFelipe Balbi 		/* Autoclear doesn't clear RxPktRdy for short packets */
8619001d80dSMing Lei 		if ((dma->desired_mode == 0 && !hw_ep->rx_double_buffered)
862550a7375SFelipe Balbi 				|| (dma->actual_len
863550a7375SFelipe Balbi 					& (musb_ep->packet_sz - 1))) {
864550a7375SFelipe Balbi 			/* ack the read! */
865550a7375SFelipe Balbi 			csr &= ~MUSB_RXCSR_RXPKTRDY;
866550a7375SFelipe Balbi 			musb_writew(epio, MUSB_RXCSR, csr);
867550a7375SFelipe Balbi 		}
868550a7375SFelipe Balbi 
869550a7375SFelipe Balbi 		/* incomplete, and not short? wait for next IN packet */
870550a7375SFelipe Balbi 		if ((request->actual < request->length)
871550a7375SFelipe Balbi 				&& (musb_ep->dma->actual_len
8729001d80dSMing Lei 					== musb_ep->packet_sz)) {
8739001d80dSMing Lei 			/* In double buffer case, continue to unload fifo if
8749001d80dSMing Lei  			 * there is Rx packet in FIFO.
8759001d80dSMing Lei  			 **/
8769001d80dSMing Lei 			csr = musb_readw(epio, MUSB_RXCSR);
8779001d80dSMing Lei 			if ((csr & MUSB_RXCSR_RXPKTRDY) &&
8789001d80dSMing Lei 				hw_ep->rx_double_buffered)
8799001d80dSMing Lei 				goto exit;
880cea83241SSergei Shtylyov 			return;
8819001d80dSMing Lei 		}
882550a7375SFelipe Balbi #endif
883550a7375SFelipe Balbi 		musb_g_giveback(musb_ep, request, 0);
88439287076SSupriya Karanth 		/*
88539287076SSupriya Karanth 		 * In the giveback function the MUSB lock is
88639287076SSupriya Karanth 		 * released and acquired after sometime. During
88739287076SSupriya Karanth 		 * this time period the INDEX register could get
88839287076SSupriya Karanth 		 * changed by the gadget_queue function especially
88939287076SSupriya Karanth 		 * on SMP systems. Reselect the INDEX to be sure
89039287076SSupriya Karanth 		 * we are reading/modifying the right registers
89139287076SSupriya Karanth 		 */
89239287076SSupriya Karanth 		musb_ep_select(mbase, epnum);
893550a7375SFelipe Balbi 
894ad1adb89SFelipe Balbi 		req = next_request(musb_ep);
895ad1adb89SFelipe Balbi 		if (!req)
896cea83241SSergei Shtylyov 			return;
897550a7375SFelipe Balbi 	}
898a48ff906SMian Yousaf Kaukab #if defined(CONFIG_USB_INVENTRA_DMA) || defined(CONFIG_USB_TUSB_OMAP_DMA) || \
899a48ff906SMian Yousaf Kaukab 	defined(CONFIG_USB_UX500_DMA)
9009001d80dSMing Lei exit:
901bb324b08SAjay Kumar Gupta #endif
90243467868SSergei Shtylyov 	/* Analyze request */
903ad1adb89SFelipe Balbi 	rxstate(musb, req);
904550a7375SFelipe Balbi }
905550a7375SFelipe Balbi 
906550a7375SFelipe Balbi /* ------------------------------------------------------------ */
907550a7375SFelipe Balbi 
musb_gadget_enable(struct usb_ep * ep,const struct usb_endpoint_descriptor * desc)908550a7375SFelipe Balbi static int musb_gadget_enable(struct usb_ep *ep,
909550a7375SFelipe Balbi 			const struct usb_endpoint_descriptor *desc)
910550a7375SFelipe Balbi {
911550a7375SFelipe Balbi 	unsigned long		flags;
912550a7375SFelipe Balbi 	struct musb_ep		*musb_ep;
913550a7375SFelipe Balbi 	struct musb_hw_ep	*hw_ep;
914550a7375SFelipe Balbi 	void __iomem		*regs;
915550a7375SFelipe Balbi 	struct musb		*musb;
916550a7375SFelipe Balbi 	void __iomem	*mbase;
917550a7375SFelipe Balbi 	u8		epnum;
918550a7375SFelipe Balbi 	u16		csr;
919550a7375SFelipe Balbi 	unsigned	tmp;
920550a7375SFelipe Balbi 	int		status = -EINVAL;
921550a7375SFelipe Balbi 
922550a7375SFelipe Balbi 	if (!ep || !desc)
923550a7375SFelipe Balbi 		return -EINVAL;
924550a7375SFelipe Balbi 
925550a7375SFelipe Balbi 	musb_ep = to_musb_ep(ep);
926550a7375SFelipe Balbi 	hw_ep = musb_ep->hw_ep;
927550a7375SFelipe Balbi 	regs = hw_ep->regs;
928550a7375SFelipe Balbi 	musb = musb_ep->musb;
929550a7375SFelipe Balbi 	mbase = musb->mregs;
930550a7375SFelipe Balbi 	epnum = musb_ep->current_epnum;
931550a7375SFelipe Balbi 
932550a7375SFelipe Balbi 	spin_lock_irqsave(&musb->lock, flags);
933550a7375SFelipe Balbi 
934550a7375SFelipe Balbi 	if (musb_ep->desc) {
935550a7375SFelipe Balbi 		status = -EBUSY;
936550a7375SFelipe Balbi 		goto fail;
937550a7375SFelipe Balbi 	}
93896bcd090SJulia Lawall 	musb_ep->type = usb_endpoint_type(desc);
939550a7375SFelipe Balbi 
940550a7375SFelipe Balbi 	/* check direction and (later) maxpacket size against endpoint */
94196bcd090SJulia Lawall 	if (usb_endpoint_num(desc) != epnum)
942550a7375SFelipe Balbi 		goto fail;
943550a7375SFelipe Balbi 
944550a7375SFelipe Balbi 	/* REVISIT this rules out high bandwidth periodic transfers */
9456ddcabc2SFelipe Balbi 	tmp = usb_endpoint_maxp_mult(desc) - 1;
9466ddcabc2SFelipe Balbi 	if (tmp) {
947f11d893dSMing Lei 		int ok;
948f11d893dSMing Lei 
949f11d893dSMing Lei 		if (usb_endpoint_dir_in(desc))
950f11d893dSMing Lei 			ok = musb->hb_iso_tx;
951f11d893dSMing Lei 		else
952f11d893dSMing Lei 			ok = musb->hb_iso_rx;
953f11d893dSMing Lei 
954f11d893dSMing Lei 		if (!ok) {
955b99d3659SBin Liu 			musb_dbg(musb, "no support for high bandwidth ISO");
956550a7375SFelipe Balbi 			goto fail;
957f11d893dSMing Lei 		}
9586ddcabc2SFelipe Balbi 		musb_ep->hb_mult = tmp;
959f11d893dSMing Lei 	} else {
960f11d893dSMing Lei 		musb_ep->hb_mult = 0;
961f11d893dSMing Lei 	}
962f11d893dSMing Lei 
9636ddcabc2SFelipe Balbi 	musb_ep->packet_sz = usb_endpoint_maxp(desc);
964f11d893dSMing Lei 	tmp = musb_ep->packet_sz * (musb_ep->hb_mult + 1);
965550a7375SFelipe Balbi 
966550a7375SFelipe Balbi 	/* enable the interrupts for the endpoint, set the endpoint
967550a7375SFelipe Balbi 	 * packet size (or fail), set the mode, clear the fifo
968550a7375SFelipe Balbi 	 */
969550a7375SFelipe Balbi 	musb_ep_select(mbase, epnum);
97096bcd090SJulia Lawall 	if (usb_endpoint_dir_in(desc)) {
971550a7375SFelipe Balbi 
972550a7375SFelipe Balbi 		if (hw_ep->is_shared_fifo)
973550a7375SFelipe Balbi 			musb_ep->is_in = 1;
974550a7375SFelipe Balbi 		if (!musb_ep->is_in)
975550a7375SFelipe Balbi 			goto fail;
976f11d893dSMing Lei 
977f11d893dSMing Lei 		if (tmp > hw_ep->max_packet_sz_tx) {
978b99d3659SBin Liu 			musb_dbg(musb, "packet size beyond hardware FIFO size");
979550a7375SFelipe Balbi 			goto fail;
980f11d893dSMing Lei 		}
981550a7375SFelipe Balbi 
982b18d26f6SSebastian Andrzej Siewior 		musb->intrtxe |= (1 << epnum);
983b18d26f6SSebastian Andrzej Siewior 		musb_writew(mbase, MUSB_INTRTXE, musb->intrtxe);
984550a7375SFelipe Balbi 
985550a7375SFelipe Balbi 		/* REVISIT if can_bulk_split(), use by updating "tmp";
986550a7375SFelipe Balbi 		 * likewise high bandwidth periodic tx
987550a7375SFelipe Balbi 		 */
9889f445cb2SCliff Cai 		/* Set TXMAXP with the FIFO size of the endpoint
98931c9909bSMing Lei 		 * to disable double buffering mode.
9909f445cb2SCliff Cai 		 */
991bb3a2ef2Ssupriya karanth 		if (can_bulk_split(musb, musb_ep->type))
992bb3a2ef2Ssupriya karanth 			musb_ep->hb_mult = (hw_ep->max_packet_sz_tx /
993bb3a2ef2Ssupriya karanth 						musb_ep->packet_sz) - 1;
99406624818SFelipe Balbi 		musb_writew(regs, MUSB_TXMAXP, musb_ep->packet_sz
99506624818SFelipe Balbi 				| (musb_ep->hb_mult << 11));
996550a7375SFelipe Balbi 
997550a7375SFelipe Balbi 		csr = MUSB_TXCSR_MODE | MUSB_TXCSR_CLRDATATOG;
998550a7375SFelipe Balbi 		if (musb_readw(regs, MUSB_TXCSR)
999550a7375SFelipe Balbi 				& MUSB_TXCSR_FIFONOTEMPTY)
1000550a7375SFelipe Balbi 			csr |= MUSB_TXCSR_FLUSHFIFO;
1001550a7375SFelipe Balbi 		if (musb_ep->type == USB_ENDPOINT_XFER_ISOC)
1002550a7375SFelipe Balbi 			csr |= MUSB_TXCSR_P_ISO;
1003550a7375SFelipe Balbi 
1004550a7375SFelipe Balbi 		/* set twice in case of double buffering */
1005550a7375SFelipe Balbi 		musb_writew(regs, MUSB_TXCSR, csr);
1006550a7375SFelipe Balbi 		/* REVISIT may be inappropriate w/o FIFONOTEMPTY ... */
1007550a7375SFelipe Balbi 		musb_writew(regs, MUSB_TXCSR, csr);
1008550a7375SFelipe Balbi 
1009550a7375SFelipe Balbi 	} else {
1010550a7375SFelipe Balbi 
1011550a7375SFelipe Balbi 		if (hw_ep->is_shared_fifo)
1012550a7375SFelipe Balbi 			musb_ep->is_in = 0;
1013550a7375SFelipe Balbi 		if (musb_ep->is_in)
1014550a7375SFelipe Balbi 			goto fail;
1015f11d893dSMing Lei 
1016f11d893dSMing Lei 		if (tmp > hw_ep->max_packet_sz_rx) {
1017b99d3659SBin Liu 			musb_dbg(musb, "packet size beyond hardware FIFO size");
1018550a7375SFelipe Balbi 			goto fail;
1019f11d893dSMing Lei 		}
1020550a7375SFelipe Balbi 
1021af5ec14dSSebastian Andrzej Siewior 		musb->intrrxe |= (1 << epnum);
1022af5ec14dSSebastian Andrzej Siewior 		musb_writew(mbase, MUSB_INTRRXE, musb->intrrxe);
1023550a7375SFelipe Balbi 
1024550a7375SFelipe Balbi 		/* REVISIT if can_bulk_combine() use by updating "tmp"
1025550a7375SFelipe Balbi 		 * likewise high bandwidth periodic rx
1026550a7375SFelipe Balbi 		 */
10279f445cb2SCliff Cai 		/* Set RXMAXP with the FIFO size of the endpoint
10289f445cb2SCliff Cai 		 * to disable double buffering mode.
10299f445cb2SCliff Cai 		 */
103006624818SFelipe Balbi 		musb_writew(regs, MUSB_RXMAXP, musb_ep->packet_sz
103106624818SFelipe Balbi 				| (musb_ep->hb_mult << 11));
1032550a7375SFelipe Balbi 
1033550a7375SFelipe Balbi 		/* force shared fifo to OUT-only mode */
1034550a7375SFelipe Balbi 		if (hw_ep->is_shared_fifo) {
1035550a7375SFelipe Balbi 			csr = musb_readw(regs, MUSB_TXCSR);
1036550a7375SFelipe Balbi 			csr &= ~(MUSB_TXCSR_MODE | MUSB_TXCSR_TXPKTRDY);
1037550a7375SFelipe Balbi 			musb_writew(regs, MUSB_TXCSR, csr);
1038550a7375SFelipe Balbi 		}
1039550a7375SFelipe Balbi 
1040550a7375SFelipe Balbi 		csr = MUSB_RXCSR_FLUSHFIFO | MUSB_RXCSR_CLRDATATOG;
1041550a7375SFelipe Balbi 		if (musb_ep->type == USB_ENDPOINT_XFER_ISOC)
1042550a7375SFelipe Balbi 			csr |= MUSB_RXCSR_P_ISO;
1043550a7375SFelipe Balbi 		else if (musb_ep->type == USB_ENDPOINT_XFER_INT)
1044550a7375SFelipe Balbi 			csr |= MUSB_RXCSR_DISNYET;
1045550a7375SFelipe Balbi 
1046550a7375SFelipe Balbi 		/* set twice in case of double buffering */
1047550a7375SFelipe Balbi 		musb_writew(regs, MUSB_RXCSR, csr);
1048550a7375SFelipe Balbi 		musb_writew(regs, MUSB_RXCSR, csr);
1049550a7375SFelipe Balbi 	}
1050550a7375SFelipe Balbi 
1051550a7375SFelipe Balbi 	/* NOTE:  all the I/O code _should_ work fine without DMA, in case
1052550a7375SFelipe Balbi 	 * for some reason you run out of channels here.
1053550a7375SFelipe Balbi 	 */
1054550a7375SFelipe Balbi 	if (is_dma_capable() && musb->dma_controller) {
1055550a7375SFelipe Balbi 		struct dma_controller	*c = musb->dma_controller;
1056550a7375SFelipe Balbi 
1057550a7375SFelipe Balbi 		musb_ep->dma = c->channel_alloc(c, hw_ep,
1058550a7375SFelipe Balbi 				(desc->bEndpointAddress & USB_DIR_IN));
1059550a7375SFelipe Balbi 	} else
1060550a7375SFelipe Balbi 		musb_ep->dma = NULL;
1061550a7375SFelipe Balbi 
1062550a7375SFelipe Balbi 	musb_ep->desc = desc;
1063550a7375SFelipe Balbi 	musb_ep->busy = 0;
106447e97605SSergei Shtylyov 	musb_ep->wedged = 0;
1065550a7375SFelipe Balbi 	status = 0;
1066550a7375SFelipe Balbi 
1067550a7375SFelipe Balbi 	pr_debug("%s periph: enabled %s for %s %s, %smaxpacket %d\n",
1068550a7375SFelipe Balbi 			musb_driver_name, musb_ep->end_point.name,
10690ccbadafSBin Liu 			musb_ep_xfertype_string(musb_ep->type),
1070550a7375SFelipe Balbi 			musb_ep->is_in ? "IN" : "OUT",
1071550a7375SFelipe Balbi 			musb_ep->dma ? "dma, " : "",
1072550a7375SFelipe Balbi 			musb_ep->packet_sz);
1073550a7375SFelipe Balbi 
10742bff3916STony Lindgren 	schedule_delayed_work(&musb->irq_work, 0);
1075550a7375SFelipe Balbi 
1076550a7375SFelipe Balbi fail:
1077550a7375SFelipe Balbi 	spin_unlock_irqrestore(&musb->lock, flags);
1078550a7375SFelipe Balbi 	return status;
1079550a7375SFelipe Balbi }
1080550a7375SFelipe Balbi 
1081550a7375SFelipe Balbi /*
1082550a7375SFelipe Balbi  * Disable an endpoint flushing all requests queued.
1083550a7375SFelipe Balbi  */
musb_gadget_disable(struct usb_ep * ep)1084550a7375SFelipe Balbi static int musb_gadget_disable(struct usb_ep *ep)
1085550a7375SFelipe Balbi {
1086550a7375SFelipe Balbi 	unsigned long	flags;
1087550a7375SFelipe Balbi 	struct musb	*musb;
1088550a7375SFelipe Balbi 	u8		epnum;
1089550a7375SFelipe Balbi 	struct musb_ep	*musb_ep;
1090550a7375SFelipe Balbi 	void __iomem	*epio;
1091550a7375SFelipe Balbi 
1092550a7375SFelipe Balbi 	musb_ep = to_musb_ep(ep);
1093550a7375SFelipe Balbi 	musb = musb_ep->musb;
1094550a7375SFelipe Balbi 	epnum = musb_ep->current_epnum;
1095550a7375SFelipe Balbi 	epio = musb->endpoints[epnum].regs;
1096550a7375SFelipe Balbi 
1097550a7375SFelipe Balbi 	spin_lock_irqsave(&musb->lock, flags);
1098550a7375SFelipe Balbi 	musb_ep_select(musb->mregs, epnum);
1099550a7375SFelipe Balbi 
1100550a7375SFelipe Balbi 	/* zero the endpoint sizes */
1101550a7375SFelipe Balbi 	if (musb_ep->is_in) {
1102b18d26f6SSebastian Andrzej Siewior 		musb->intrtxe &= ~(1 << epnum);
1103b18d26f6SSebastian Andrzej Siewior 		musb_writew(musb->mregs, MUSB_INTRTXE, musb->intrtxe);
1104550a7375SFelipe Balbi 		musb_writew(epio, MUSB_TXMAXP, 0);
1105550a7375SFelipe Balbi 	} else {
1106af5ec14dSSebastian Andrzej Siewior 		musb->intrrxe &= ~(1 << epnum);
1107af5ec14dSSebastian Andrzej Siewior 		musb_writew(musb->mregs, MUSB_INTRRXE, musb->intrrxe);
1108550a7375SFelipe Balbi 		musb_writew(epio, MUSB_RXMAXP, 0);
1109550a7375SFelipe Balbi 	}
1110550a7375SFelipe Balbi 
1111550a7375SFelipe Balbi 	/* abort all pending DMA and requests */
1112550a7375SFelipe Balbi 	nuke(musb_ep, -ESHUTDOWN);
1113550a7375SFelipe Balbi 
1114607fb0f4STal Shorer 	musb_ep->desc = NULL;
1115607fb0f4STal Shorer 	musb_ep->end_point.desc = NULL;
1116607fb0f4STal Shorer 
11172bff3916STony Lindgren 	schedule_delayed_work(&musb->irq_work, 0);
1118550a7375SFelipe Balbi 
1119550a7375SFelipe Balbi 	spin_unlock_irqrestore(&(musb->lock), flags);
1120550a7375SFelipe Balbi 
1121b99d3659SBin Liu 	musb_dbg(musb, "%s", musb_ep->end_point.name);
1122550a7375SFelipe Balbi 
112329e56c0cSSaurav Girepunje 	return 0;
1124550a7375SFelipe Balbi }
1125550a7375SFelipe Balbi 
1126550a7375SFelipe Balbi /*
1127550a7375SFelipe Balbi  * Allocate a request for an endpoint.
1128550a7375SFelipe Balbi  * Reused by ep0 code.
1129550a7375SFelipe Balbi  */
musb_alloc_request(struct usb_ep * ep,gfp_t gfp_flags)1130550a7375SFelipe Balbi struct usb_request *musb_alloc_request(struct usb_ep *ep, gfp_t gfp_flags)
1131550a7375SFelipe Balbi {
1132550a7375SFelipe Balbi 	struct musb_ep		*musb_ep = to_musb_ep(ep);
11339de17578SRuan Jinjie 	struct musb_request	*request;
1134550a7375SFelipe Balbi 
1135550a7375SFelipe Balbi 	request = kzalloc(sizeof *request, gfp_flags);
1136b99d3659SBin Liu 	if (!request)
11370607f862SFelipe Balbi 		return NULL;
11380607f862SFelipe Balbi 
1139550a7375SFelipe Balbi 	request->request.dma = DMA_ADDR_INVALID;
1140550a7375SFelipe Balbi 	request->epnum = musb_ep->current_epnum;
1141550a7375SFelipe Balbi 	request->ep = musb_ep;
1142550a7375SFelipe Balbi 
1143fc78003eSBin Liu 	trace_musb_req_alloc(request);
1144550a7375SFelipe Balbi 	return &request->request;
1145550a7375SFelipe Balbi }
1146550a7375SFelipe Balbi 
1147550a7375SFelipe Balbi /*
1148550a7375SFelipe Balbi  * Free a request
1149550a7375SFelipe Balbi  * Reused by ep0 code.
1150550a7375SFelipe Balbi  */
musb_free_request(struct usb_ep * ep,struct usb_request * req)1151550a7375SFelipe Balbi void musb_free_request(struct usb_ep *ep, struct usb_request *req)
1152550a7375SFelipe Balbi {
1153fc78003eSBin Liu 	struct musb_request *request = to_musb_request(req);
1154fc78003eSBin Liu 
1155fc78003eSBin Liu 	trace_musb_req_free(request);
1156fc78003eSBin Liu 	kfree(request);
1157550a7375SFelipe Balbi }
1158550a7375SFelipe Balbi 
1159550a7375SFelipe Balbi static LIST_HEAD(buffers);
1160550a7375SFelipe Balbi 
1161550a7375SFelipe Balbi struct free_record {
1162550a7375SFelipe Balbi 	struct list_head	list;
1163550a7375SFelipe Balbi 	struct device		*dev;
1164550a7375SFelipe Balbi 	unsigned		bytes;
1165550a7375SFelipe Balbi 	dma_addr_t		dma;
1166550a7375SFelipe Balbi };
1167550a7375SFelipe Balbi 
1168550a7375SFelipe Balbi /*
1169550a7375SFelipe Balbi  * Context: controller locked, IRQs blocked.
1170550a7375SFelipe Balbi  */
musb_ep_restart(struct musb * musb,struct musb_request * req)1171a666e3e6SSergei Shtylyov void musb_ep_restart(struct musb *musb, struct musb_request *req)
1172550a7375SFelipe Balbi {
1173*5906ee36SHubert Wiśniewski 	u16 csr;
1174*5906ee36SHubert Wiśniewski 	void __iomem *epio = req->ep->hw_ep->regs;
1175*5906ee36SHubert Wiśniewski 
1176fc78003eSBin Liu 	trace_musb_req_start(req);
1177550a7375SFelipe Balbi 	musb_ep_select(musb->mregs, req->epnum);
1178*5906ee36SHubert Wiśniewski 	if (req->tx) {
1179550a7375SFelipe Balbi 		txstate(musb, req);
1180*5906ee36SHubert Wiśniewski 	} else {
1181*5906ee36SHubert Wiśniewski 		csr = musb_readw(epio, MUSB_RXCSR);
1182*5906ee36SHubert Wiśniewski 		csr |= MUSB_RXCSR_FLUSHFIFO | MUSB_RXCSR_P_WZC_BITS;
1183*5906ee36SHubert Wiśniewski 		musb_writew(epio, MUSB_RXCSR, csr);
1184*5906ee36SHubert Wiśniewski 		musb_writew(epio, MUSB_RXCSR, csr);
1185*5906ee36SHubert Wiśniewski 	}
1186550a7375SFelipe Balbi }
1187550a7375SFelipe Balbi 
musb_ep_restart_resume_work(struct musb * musb,void * data)1188ea2f35c0STony Lindgren static int musb_ep_restart_resume_work(struct musb *musb, void *data)
1189ea2f35c0STony Lindgren {
1190ea2f35c0STony Lindgren 	struct musb_request *req = data;
1191ea2f35c0STony Lindgren 
1192ea2f35c0STony Lindgren 	musb_ep_restart(musb, req);
1193ea2f35c0STony Lindgren 
1194ea2f35c0STony Lindgren 	return 0;
1195ea2f35c0STony Lindgren }
1196ea2f35c0STony Lindgren 
musb_gadget_queue(struct usb_ep * ep,struct usb_request * req,gfp_t gfp_flags)1197550a7375SFelipe Balbi static int musb_gadget_queue(struct usb_ep *ep, struct usb_request *req,
1198550a7375SFelipe Balbi 			gfp_t gfp_flags)
1199550a7375SFelipe Balbi {
1200550a7375SFelipe Balbi 	struct musb_ep		*musb_ep;
1201550a7375SFelipe Balbi 	struct musb_request	*request;
1202550a7375SFelipe Balbi 	struct musb		*musb;
1203ea2f35c0STony Lindgren 	int			status;
1204550a7375SFelipe Balbi 	unsigned long		lockflags;
1205550a7375SFelipe Balbi 
1206550a7375SFelipe Balbi 	if (!ep || !req)
1207550a7375SFelipe Balbi 		return -EINVAL;
1208550a7375SFelipe Balbi 	if (!req->buf)
1209550a7375SFelipe Balbi 		return -ENODATA;
1210550a7375SFelipe Balbi 
1211550a7375SFelipe Balbi 	musb_ep = to_musb_ep(ep);
1212550a7375SFelipe Balbi 	musb = musb_ep->musb;
1213550a7375SFelipe Balbi 
1214550a7375SFelipe Balbi 	request = to_musb_request(req);
1215550a7375SFelipe Balbi 	request->musb = musb;
1216550a7375SFelipe Balbi 
1217550a7375SFelipe Balbi 	if (request->ep != musb_ep)
1218550a7375SFelipe Balbi 		return -EINVAL;
1219550a7375SFelipe Balbi 
1220ea2f35c0STony Lindgren 	status = pm_runtime_get(musb->controller);
1221ea2f35c0STony Lindgren 	if ((status != -EINPROGRESS) && status < 0) {
1222ea2f35c0STony Lindgren 		dev_err(musb->controller,
1223ea2f35c0STony Lindgren 			"pm runtime get failed in %s\n",
1224ea2f35c0STony Lindgren 			__func__);
1225ea2f35c0STony Lindgren 		pm_runtime_put_noidle(musb->controller);
1226ea2f35c0STony Lindgren 
1227ea2f35c0STony Lindgren 		return status;
1228ea2f35c0STony Lindgren 	}
1229ea2f35c0STony Lindgren 	status = 0;
1230ea2f35c0STony Lindgren 
1231fc78003eSBin Liu 	trace_musb_req_enq(request);
1232550a7375SFelipe Balbi 
1233550a7375SFelipe Balbi 	/* request is mine now... */
1234550a7375SFelipe Balbi 	request->request.actual = 0;
1235550a7375SFelipe Balbi 	request->request.status = -EINPROGRESS;
1236550a7375SFelipe Balbi 	request->epnum = musb_ep->current_epnum;
1237550a7375SFelipe Balbi 	request->tx = musb_ep->is_in;
1238550a7375SFelipe Balbi 
1239c65bfa62SMian Yousaf Kaukab 	map_dma_buffer(request, musb, musb_ep);
1240550a7375SFelipe Balbi 
1241550a7375SFelipe Balbi 	spin_lock_irqsave(&musb->lock, lockflags);
1242550a7375SFelipe Balbi 
1243550a7375SFelipe Balbi 	/* don't queue if the ep is down */
1244550a7375SFelipe Balbi 	if (!musb_ep->desc) {
1245b99d3659SBin Liu 		musb_dbg(musb, "req %p queued to %s while ep %s",
1246550a7375SFelipe Balbi 				req, ep->name, "disabled");
1247550a7375SFelipe Balbi 		status = -ESHUTDOWN;
124823a53d90SSebastian Andrzej Siewior 		unmap_dma_buffer(request, musb);
124923a53d90SSebastian Andrzej Siewior 		goto unlock;
1250550a7375SFelipe Balbi 	}
1251550a7375SFelipe Balbi 
1252550a7375SFelipe Balbi 	/* add request to the list */
1253ad1adb89SFelipe Balbi 	list_add_tail(&request->list, &musb_ep->req_list);
1254550a7375SFelipe Balbi 
1255550a7375SFelipe Balbi 	/* it this is the head of the queue, start i/o ... */
1256ea2f35c0STony Lindgren 	if (!musb_ep->busy && &request->list == musb_ep->req_list.next) {
1257ea2f35c0STony Lindgren 		status = musb_queue_resume_work(musb,
1258ea2f35c0STony Lindgren 						musb_ep_restart_resume_work,
1259ea2f35c0STony Lindgren 						request);
126021b5fcdcSViraj Shah 		if (status < 0) {
1261ea2f35c0STony Lindgren 			dev_err(musb->controller, "%s resume work: %i\n",
1262ea2f35c0STony Lindgren 				__func__, status);
126321b5fcdcSViraj Shah 			list_del(&request->list);
126421b5fcdcSViraj Shah 		}
1265ea2f35c0STony Lindgren 	}
1266550a7375SFelipe Balbi 
126723a53d90SSebastian Andrzej Siewior unlock:
1268550a7375SFelipe Balbi 	spin_unlock_irqrestore(&musb->lock, lockflags);
1269cacaaf80STony Lindgren 	pm_runtime_mark_last_busy(musb->controller);
1270cacaaf80STony Lindgren 	pm_runtime_put_autosuspend(musb->controller);
1271cacaaf80STony Lindgren 
1272550a7375SFelipe Balbi 	return status;
1273550a7375SFelipe Balbi }
1274550a7375SFelipe Balbi 
musb_gadget_dequeue(struct usb_ep * ep,struct usb_request * request)1275550a7375SFelipe Balbi static int musb_gadget_dequeue(struct usb_ep *ep, struct usb_request *request)
1276550a7375SFelipe Balbi {
1277550a7375SFelipe Balbi 	struct musb_ep		*musb_ep = to_musb_ep(ep);
12784cbbf084SFelipe Balbi 	struct musb_request	*req = to_musb_request(request);
12794cbbf084SFelipe Balbi 	struct musb_request	*r;
1280550a7375SFelipe Balbi 	unsigned long		flags;
1281550a7375SFelipe Balbi 	int			status = 0;
1282550a7375SFelipe Balbi 	struct musb		*musb = musb_ep->musb;
1283550a7375SFelipe Balbi 
1284fc78003eSBin Liu 	if (!ep || !request || req->ep != musb_ep)
1285550a7375SFelipe Balbi 		return -EINVAL;
1286550a7375SFelipe Balbi 
1287fc78003eSBin Liu 	trace_musb_req_deq(req);
1288fc78003eSBin Liu 
1289550a7375SFelipe Balbi 	spin_lock_irqsave(&musb->lock, flags);
1290550a7375SFelipe Balbi 
1291550a7375SFelipe Balbi 	list_for_each_entry(r, &musb_ep->req_list, list) {
12924cbbf084SFelipe Balbi 		if (r == req)
1293550a7375SFelipe Balbi 			break;
1294550a7375SFelipe Balbi 	}
12954cbbf084SFelipe Balbi 	if (r != req) {
1296b99d3659SBin Liu 		dev_err(musb->controller, "request %p not queued to %s\n",
1297b99d3659SBin Liu 				request, ep->name);
1298550a7375SFelipe Balbi 		status = -EINVAL;
1299550a7375SFelipe Balbi 		goto done;
1300550a7375SFelipe Balbi 	}
1301550a7375SFelipe Balbi 
1302550a7375SFelipe Balbi 	/* if the hardware doesn't have the request, easy ... */
13033d5ad13eSFelipe Balbi 	if (musb_ep->req_list.next != &req->list || musb_ep->busy)
1304550a7375SFelipe Balbi 		musb_g_giveback(musb_ep, request, -ECONNRESET);
1305550a7375SFelipe Balbi 
1306550a7375SFelipe Balbi 	/* ... else abort the dma transfer ... */
1307550a7375SFelipe Balbi 	else if (is_dma_capable() && musb_ep->dma) {
1308550a7375SFelipe Balbi 		struct dma_controller	*c = musb->dma_controller;
1309550a7375SFelipe Balbi 
1310550a7375SFelipe Balbi 		musb_ep_select(musb->mregs, musb_ep->current_epnum);
1311550a7375SFelipe Balbi 		if (c->channel_abort)
1312550a7375SFelipe Balbi 			status = c->channel_abort(musb_ep->dma);
1313550a7375SFelipe Balbi 		else
1314550a7375SFelipe Balbi 			status = -EBUSY;
1315550a7375SFelipe Balbi 		if (status == 0)
1316550a7375SFelipe Balbi 			musb_g_giveback(musb_ep, request, -ECONNRESET);
1317550a7375SFelipe Balbi 	} else {
1318550a7375SFelipe Balbi 		/* NOTE: by sticking to easily tested hardware/driver states,
1319550a7375SFelipe Balbi 		 * we leave counting of in-flight packets imprecise.
1320550a7375SFelipe Balbi 		 */
1321550a7375SFelipe Balbi 		musb_g_giveback(musb_ep, request, -ECONNRESET);
1322550a7375SFelipe Balbi 	}
1323550a7375SFelipe Balbi 
1324550a7375SFelipe Balbi done:
1325550a7375SFelipe Balbi 	spin_unlock_irqrestore(&musb->lock, flags);
1326550a7375SFelipe Balbi 	return status;
1327550a7375SFelipe Balbi }
1328550a7375SFelipe Balbi 
1329550a7375SFelipe Balbi /*
1330c1aa81daSGeert Uytterhoeven  * Set or clear the halt bit of an endpoint. A halted endpoint won't tx/rx any
1331550a7375SFelipe Balbi  * data but will queue requests.
1332550a7375SFelipe Balbi  *
1333550a7375SFelipe Balbi  * exported to ep0 code
1334550a7375SFelipe Balbi  */
musb_gadget_set_halt(struct usb_ep * ep,int value)13351b6c3b0fSFelipe Balbi static int musb_gadget_set_halt(struct usb_ep *ep, int value)
1336550a7375SFelipe Balbi {
1337550a7375SFelipe Balbi 	struct musb_ep		*musb_ep = to_musb_ep(ep);
1338550a7375SFelipe Balbi 	u8			epnum = musb_ep->current_epnum;
1339550a7375SFelipe Balbi 	struct musb		*musb = musb_ep->musb;
1340550a7375SFelipe Balbi 	void __iomem		*epio = musb->endpoints[epnum].regs;
1341550a7375SFelipe Balbi 	void __iomem		*mbase;
1342550a7375SFelipe Balbi 	unsigned long		flags;
1343550a7375SFelipe Balbi 	u16			csr;
1344cea83241SSergei Shtylyov 	struct musb_request	*request;
1345550a7375SFelipe Balbi 	int			status = 0;
1346550a7375SFelipe Balbi 
1347550a7375SFelipe Balbi 	if (!ep)
1348550a7375SFelipe Balbi 		return -EINVAL;
1349550a7375SFelipe Balbi 	mbase = musb->mregs;
1350550a7375SFelipe Balbi 
1351550a7375SFelipe Balbi 	spin_lock_irqsave(&musb->lock, flags);
1352550a7375SFelipe Balbi 
1353550a7375SFelipe Balbi 	if ((USB_ENDPOINT_XFER_ISOC == musb_ep->type)) {
1354550a7375SFelipe Balbi 		status = -EINVAL;
1355550a7375SFelipe Balbi 		goto done;
1356550a7375SFelipe Balbi 	}
1357550a7375SFelipe Balbi 
1358550a7375SFelipe Balbi 	musb_ep_select(mbase, epnum);
1359550a7375SFelipe Balbi 
1360ad1adb89SFelipe Balbi 	request = next_request(musb_ep);
1361cea83241SSergei Shtylyov 	if (value) {
1362cea83241SSergei Shtylyov 		if (request) {
1363b99d3659SBin Liu 			musb_dbg(musb, "request in progress, cannot halt %s",
1364cea83241SSergei Shtylyov 			    ep->name);
1365cea83241SSergei Shtylyov 			status = -EAGAIN;
1366cea83241SSergei Shtylyov 			goto done;
1367cea83241SSergei Shtylyov 		}
1368cea83241SSergei Shtylyov 		/* Cannot portably stall with non-empty FIFO */
1369cea83241SSergei Shtylyov 		if (musb_ep->is_in) {
1370550a7375SFelipe Balbi 			csr = musb_readw(epio, MUSB_TXCSR);
1371550a7375SFelipe Balbi 			if (csr & MUSB_TXCSR_FIFONOTEMPTY) {
1372b99d3659SBin Liu 				musb_dbg(musb, "FIFO busy, cannot halt %s",
1373b99d3659SBin Liu 						ep->name);
1374cea83241SSergei Shtylyov 				status = -EAGAIN;
1375cea83241SSergei Shtylyov 				goto done;
1376550a7375SFelipe Balbi 			}
1377cea83241SSergei Shtylyov 		}
137847e97605SSergei Shtylyov 	} else
137947e97605SSergei Shtylyov 		musb_ep->wedged = 0;
1380550a7375SFelipe Balbi 
1381550a7375SFelipe Balbi 	/* set/clear the stall and toggle bits */
1382b99d3659SBin Liu 	musb_dbg(musb, "%s: %s stall", ep->name, value ? "set" : "clear");
1383550a7375SFelipe Balbi 	if (musb_ep->is_in) {
1384550a7375SFelipe Balbi 		csr = musb_readw(epio, MUSB_TXCSR);
1385550a7375SFelipe Balbi 		csr |= MUSB_TXCSR_P_WZC_BITS
1386550a7375SFelipe Balbi 			| MUSB_TXCSR_CLRDATATOG;
1387550a7375SFelipe Balbi 		if (value)
1388550a7375SFelipe Balbi 			csr |= MUSB_TXCSR_P_SENDSTALL;
1389550a7375SFelipe Balbi 		else
1390550a7375SFelipe Balbi 			csr &= ~(MUSB_TXCSR_P_SENDSTALL
1391550a7375SFelipe Balbi 				| MUSB_TXCSR_P_SENTSTALL);
1392550a7375SFelipe Balbi 		csr &= ~MUSB_TXCSR_TXPKTRDY;
1393550a7375SFelipe Balbi 		musb_writew(epio, MUSB_TXCSR, csr);
1394550a7375SFelipe Balbi 	} else {
1395550a7375SFelipe Balbi 		csr = musb_readw(epio, MUSB_RXCSR);
1396550a7375SFelipe Balbi 		csr |= MUSB_RXCSR_P_WZC_BITS
1397550a7375SFelipe Balbi 			| MUSB_RXCSR_FLUSHFIFO
1398550a7375SFelipe Balbi 			| MUSB_RXCSR_CLRDATATOG;
1399550a7375SFelipe Balbi 		if (value)
1400550a7375SFelipe Balbi 			csr |= MUSB_RXCSR_P_SENDSTALL;
1401550a7375SFelipe Balbi 		else
1402550a7375SFelipe Balbi 			csr &= ~(MUSB_RXCSR_P_SENDSTALL
1403550a7375SFelipe Balbi 				| MUSB_RXCSR_P_SENTSTALL);
1404550a7375SFelipe Balbi 		musb_writew(epio, MUSB_RXCSR, csr);
1405550a7375SFelipe Balbi 	}
1406550a7375SFelipe Balbi 
1407550a7375SFelipe Balbi 	/* maybe start the first request in the queue */
1408550a7375SFelipe Balbi 	if (!musb_ep->busy && !value && request) {
1409b99d3659SBin Liu 		musb_dbg(musb, "restarting the request");
1410550a7375SFelipe Balbi 		musb_ep_restart(musb, request);
1411550a7375SFelipe Balbi 	}
1412550a7375SFelipe Balbi 
1413cea83241SSergei Shtylyov done:
1414550a7375SFelipe Balbi 	spin_unlock_irqrestore(&musb->lock, flags);
1415550a7375SFelipe Balbi 	return status;
1416550a7375SFelipe Balbi }
1417550a7375SFelipe Balbi 
141847e97605SSergei Shtylyov /*
141947e97605SSergei Shtylyov  * Sets the halt feature with the clear requests ignored
142047e97605SSergei Shtylyov  */
musb_gadget_set_wedge(struct usb_ep * ep)14211b6c3b0fSFelipe Balbi static int musb_gadget_set_wedge(struct usb_ep *ep)
142247e97605SSergei Shtylyov {
142347e97605SSergei Shtylyov 	struct musb_ep		*musb_ep = to_musb_ep(ep);
142447e97605SSergei Shtylyov 
142547e97605SSergei Shtylyov 	if (!ep)
142647e97605SSergei Shtylyov 		return -EINVAL;
142747e97605SSergei Shtylyov 
142847e97605SSergei Shtylyov 	musb_ep->wedged = 1;
142947e97605SSergei Shtylyov 
143047e97605SSergei Shtylyov 	return usb_ep_set_halt(ep);
143147e97605SSergei Shtylyov }
143247e97605SSergei Shtylyov 
musb_gadget_fifo_status(struct usb_ep * ep)1433550a7375SFelipe Balbi static int musb_gadget_fifo_status(struct usb_ep *ep)
1434550a7375SFelipe Balbi {
1435550a7375SFelipe Balbi 	struct musb_ep		*musb_ep = to_musb_ep(ep);
1436550a7375SFelipe Balbi 	void __iomem		*epio = musb_ep->hw_ep->regs;
1437550a7375SFelipe Balbi 	int			retval = -EINVAL;
1438550a7375SFelipe Balbi 
1439550a7375SFelipe Balbi 	if (musb_ep->desc && !musb_ep->is_in) {
1440550a7375SFelipe Balbi 		struct musb		*musb = musb_ep->musb;
1441550a7375SFelipe Balbi 		int			epnum = musb_ep->current_epnum;
1442550a7375SFelipe Balbi 		void __iomem		*mbase = musb->mregs;
1443550a7375SFelipe Balbi 		unsigned long		flags;
1444550a7375SFelipe Balbi 
1445550a7375SFelipe Balbi 		spin_lock_irqsave(&musb->lock, flags);
1446550a7375SFelipe Balbi 
1447550a7375SFelipe Balbi 		musb_ep_select(mbase, epnum);
1448550a7375SFelipe Balbi 		/* FIXME return zero unless RXPKTRDY is set */
1449550a7375SFelipe Balbi 		retval = musb_readw(epio, MUSB_RXCOUNT);
1450550a7375SFelipe Balbi 
1451550a7375SFelipe Balbi 		spin_unlock_irqrestore(&musb->lock, flags);
1452550a7375SFelipe Balbi 	}
1453550a7375SFelipe Balbi 	return retval;
1454550a7375SFelipe Balbi }
1455550a7375SFelipe Balbi 
musb_gadget_fifo_flush(struct usb_ep * ep)1456550a7375SFelipe Balbi static void musb_gadget_fifo_flush(struct usb_ep *ep)
1457550a7375SFelipe Balbi {
1458550a7375SFelipe Balbi 	struct musb_ep	*musb_ep = to_musb_ep(ep);
1459550a7375SFelipe Balbi 	struct musb	*musb = musb_ep->musb;
1460550a7375SFelipe Balbi 	u8		epnum = musb_ep->current_epnum;
1461550a7375SFelipe Balbi 	void __iomem	*epio = musb->endpoints[epnum].regs;
1462550a7375SFelipe Balbi 	void __iomem	*mbase;
1463550a7375SFelipe Balbi 	unsigned long	flags;
1464b18d26f6SSebastian Andrzej Siewior 	u16		csr;
1465550a7375SFelipe Balbi 
1466550a7375SFelipe Balbi 	mbase = musb->mregs;
1467550a7375SFelipe Balbi 
1468550a7375SFelipe Balbi 	spin_lock_irqsave(&musb->lock, flags);
1469550a7375SFelipe Balbi 	musb_ep_select(mbase, (u8) epnum);
1470550a7375SFelipe Balbi 
1471550a7375SFelipe Balbi 	/* disable interrupts */
1472b18d26f6SSebastian Andrzej Siewior 	musb_writew(mbase, MUSB_INTRTXE, musb->intrtxe & ~(1 << epnum));
1473550a7375SFelipe Balbi 
1474550a7375SFelipe Balbi 	if (musb_ep->is_in) {
1475550a7375SFelipe Balbi 		csr = musb_readw(epio, MUSB_TXCSR);
1476550a7375SFelipe Balbi 		if (csr & MUSB_TXCSR_FIFONOTEMPTY) {
1477550a7375SFelipe Balbi 			csr |= MUSB_TXCSR_FLUSHFIFO | MUSB_TXCSR_P_WZC_BITS;
14784858f06eSYauheni Kaliuta 			/*
14794858f06eSYauheni Kaliuta 			 * Setting both TXPKTRDY and FLUSHFIFO makes controller
14804858f06eSYauheni Kaliuta 			 * to interrupt current FIFO loading, but not flushing
14814858f06eSYauheni Kaliuta 			 * the already loaded ones.
14824858f06eSYauheni Kaliuta 			 */
14834858f06eSYauheni Kaliuta 			csr &= ~MUSB_TXCSR_TXPKTRDY;
1484550a7375SFelipe Balbi 			musb_writew(epio, MUSB_TXCSR, csr);
1485550a7375SFelipe Balbi 			/* REVISIT may be inappropriate w/o FIFONOTEMPTY ... */
1486550a7375SFelipe Balbi 			musb_writew(epio, MUSB_TXCSR, csr);
1487550a7375SFelipe Balbi 		}
1488550a7375SFelipe Balbi 	} else {
1489550a7375SFelipe Balbi 		csr = musb_readw(epio, MUSB_RXCSR);
1490550a7375SFelipe Balbi 		csr |= MUSB_RXCSR_FLUSHFIFO | MUSB_RXCSR_P_WZC_BITS;
1491550a7375SFelipe Balbi 		musb_writew(epio, MUSB_RXCSR, csr);
1492550a7375SFelipe Balbi 		musb_writew(epio, MUSB_RXCSR, csr);
1493550a7375SFelipe Balbi 	}
1494550a7375SFelipe Balbi 
1495550a7375SFelipe Balbi 	/* re-enable interrupt */
1496b18d26f6SSebastian Andrzej Siewior 	musb_writew(mbase, MUSB_INTRTXE, musb->intrtxe);
1497550a7375SFelipe Balbi 	spin_unlock_irqrestore(&musb->lock, flags);
1498550a7375SFelipe Balbi }
1499550a7375SFelipe Balbi 
1500550a7375SFelipe Balbi static const struct usb_ep_ops musb_ep_ops = {
1501550a7375SFelipe Balbi 	.enable		= musb_gadget_enable,
1502550a7375SFelipe Balbi 	.disable	= musb_gadget_disable,
1503550a7375SFelipe Balbi 	.alloc_request	= musb_alloc_request,
1504550a7375SFelipe Balbi 	.free_request	= musb_free_request,
1505550a7375SFelipe Balbi 	.queue		= musb_gadget_queue,
1506550a7375SFelipe Balbi 	.dequeue	= musb_gadget_dequeue,
1507550a7375SFelipe Balbi 	.set_halt	= musb_gadget_set_halt,
150847e97605SSergei Shtylyov 	.set_wedge	= musb_gadget_set_wedge,
1509550a7375SFelipe Balbi 	.fifo_status	= musb_gadget_fifo_status,
1510550a7375SFelipe Balbi 	.fifo_flush	= musb_gadget_fifo_flush
1511550a7375SFelipe Balbi };
1512550a7375SFelipe Balbi 
1513550a7375SFelipe Balbi /* ----------------------------------------------------------------------- */
1514550a7375SFelipe Balbi 
musb_gadget_get_frame(struct usb_gadget * gadget)1515550a7375SFelipe Balbi static int musb_gadget_get_frame(struct usb_gadget *gadget)
1516550a7375SFelipe Balbi {
1517550a7375SFelipe Balbi 	struct musb	*musb = gadget_to_musb(gadget);
1518550a7375SFelipe Balbi 
1519550a7375SFelipe Balbi 	return (int)musb_readw(musb->mregs, MUSB_FRAME);
1520550a7375SFelipe Balbi }
1521550a7375SFelipe Balbi 
musb_gadget_wakeup(struct usb_gadget * gadget)1522550a7375SFelipe Balbi static int musb_gadget_wakeup(struct usb_gadget *gadget)
1523550a7375SFelipe Balbi {
1524550a7375SFelipe Balbi 	struct musb	*musb = gadget_to_musb(gadget);
1525550a7375SFelipe Balbi 	void __iomem	*mregs = musb->mregs;
1526550a7375SFelipe Balbi 	unsigned long	flags;
1527550a7375SFelipe Balbi 	int		status = -EINVAL;
1528550a7375SFelipe Balbi 	u8		power, devctl;
1529550a7375SFelipe Balbi 	int		retries;
1530550a7375SFelipe Balbi 
1531550a7375SFelipe Balbi 	spin_lock_irqsave(&musb->lock, flags);
1532550a7375SFelipe Balbi 
153321acc656SPaul Cercueil 	switch (musb_get_state(musb)) {
1534550a7375SFelipe Balbi 	case OTG_STATE_B_PERIPHERAL:
1535550a7375SFelipe Balbi 		/* NOTE:  OTG state machine doesn't include B_SUSPENDED;
1536550a7375SFelipe Balbi 		 * that's part of the standard usb 1.1 state machine, and
1537550a7375SFelipe Balbi 		 * doesn't affect OTG transitions.
1538550a7375SFelipe Balbi 		 */
1539550a7375SFelipe Balbi 		if (musb->may_wakeup && musb->is_suspended)
1540550a7375SFelipe Balbi 			break;
1541550a7375SFelipe Balbi 		goto done;
1542550a7375SFelipe Balbi 	case OTG_STATE_B_IDLE:
1543550a7375SFelipe Balbi 		/* Start SRP ... OTG not required. */
1544550a7375SFelipe Balbi 		devctl = musb_readb(mregs, MUSB_DEVCTL);
1545b99d3659SBin Liu 		musb_dbg(musb, "Sending SRP: devctl: %02x", devctl);
1546550a7375SFelipe Balbi 		devctl |= MUSB_DEVCTL_SESSION;
1547550a7375SFelipe Balbi 		musb_writeb(mregs, MUSB_DEVCTL, devctl);
1548550a7375SFelipe Balbi 		devctl = musb_readb(mregs, MUSB_DEVCTL);
1549550a7375SFelipe Balbi 		retries = 100;
1550550a7375SFelipe Balbi 		while (!(devctl & MUSB_DEVCTL_SESSION)) {
1551550a7375SFelipe Balbi 			devctl = musb_readb(mregs, MUSB_DEVCTL);
1552550a7375SFelipe Balbi 			if (retries-- < 1)
1553550a7375SFelipe Balbi 				break;
1554550a7375SFelipe Balbi 		}
1555550a7375SFelipe Balbi 		retries = 10000;
1556550a7375SFelipe Balbi 		while (devctl & MUSB_DEVCTL_SESSION) {
1557550a7375SFelipe Balbi 			devctl = musb_readb(mregs, MUSB_DEVCTL);
1558550a7375SFelipe Balbi 			if (retries-- < 1)
1559550a7375SFelipe Balbi 				break;
1560550a7375SFelipe Balbi 		}
1561550a7375SFelipe Balbi 
1562a6d45ea0SPaul Cercueil 		if (musb->xceiv) {
15638620543eSHema HK 			spin_unlock_irqrestore(&musb->lock, flags);
15646e13c650SHeikki Krogerus 			otg_start_srp(musb->xceiv->otg);
15658620543eSHema HK 			spin_lock_irqsave(&musb->lock, flags);
1566a6d45ea0SPaul Cercueil 		}
15678620543eSHema HK 
1568550a7375SFelipe Balbi 		/* Block idling for at least 1s */
1569550a7375SFelipe Balbi 		musb_platform_try_idle(musb,
1570550a7375SFelipe Balbi 			jiffies + msecs_to_jiffies(1 * HZ));
1571550a7375SFelipe Balbi 
1572550a7375SFelipe Balbi 		status = 0;
1573550a7375SFelipe Balbi 		goto done;
1574550a7375SFelipe Balbi 	default:
1575b99d3659SBin Liu 		musb_dbg(musb, "Unhandled wake: %s",
1576285f28bfSPaul Cercueil 			 musb_otg_state_string(musb));
1577550a7375SFelipe Balbi 		goto done;
1578550a7375SFelipe Balbi 	}
1579550a7375SFelipe Balbi 
1580550a7375SFelipe Balbi 	status = 0;
1581550a7375SFelipe Balbi 
1582550a7375SFelipe Balbi 	power = musb_readb(mregs, MUSB_POWER);
1583550a7375SFelipe Balbi 	power |= MUSB_POWER_RESUME;
1584550a7375SFelipe Balbi 	musb_writeb(mregs, MUSB_POWER, power);
1585b99d3659SBin Liu 	musb_dbg(musb, "issue wakeup");
1586550a7375SFelipe Balbi 
1587550a7375SFelipe Balbi 	/* FIXME do this next chunk in a timer callback, no udelay */
1588550a7375SFelipe Balbi 	mdelay(2);
1589550a7375SFelipe Balbi 
1590550a7375SFelipe Balbi 	power = musb_readb(mregs, MUSB_POWER);
1591550a7375SFelipe Balbi 	power &= ~MUSB_POWER_RESUME;
1592550a7375SFelipe Balbi 	musb_writeb(mregs, MUSB_POWER, power);
1593550a7375SFelipe Balbi done:
1594550a7375SFelipe Balbi 	spin_unlock_irqrestore(&musb->lock, flags);
1595550a7375SFelipe Balbi 	return status;
1596550a7375SFelipe Balbi }
1597550a7375SFelipe Balbi 
1598550a7375SFelipe Balbi static int
musb_gadget_set_self_powered(struct usb_gadget * gadget,int is_selfpowered)1599550a7375SFelipe Balbi musb_gadget_set_self_powered(struct usb_gadget *gadget, int is_selfpowered)
1600550a7375SFelipe Balbi {
1601dadac986SPeter Chen 	gadget->is_selfpowered = !!is_selfpowered;
1602550a7375SFelipe Balbi 	return 0;
1603550a7375SFelipe Balbi }
1604550a7375SFelipe Balbi 
musb_pullup(struct musb * musb,int is_on)1605550a7375SFelipe Balbi static void musb_pullup(struct musb *musb, int is_on)
1606550a7375SFelipe Balbi {
1607550a7375SFelipe Balbi 	u8 power;
1608550a7375SFelipe Balbi 
1609550a7375SFelipe Balbi 	power = musb_readb(musb->mregs, MUSB_POWER);
1610550a7375SFelipe Balbi 	if (is_on)
1611550a7375SFelipe Balbi 		power |= MUSB_POWER_SOFTCONN;
1612550a7375SFelipe Balbi 	else
1613550a7375SFelipe Balbi 		power &= ~MUSB_POWER_SOFTCONN;
1614550a7375SFelipe Balbi 
1615550a7375SFelipe Balbi 	/* FIXME if on, HdrcStart; if off, HdrcStop */
1616550a7375SFelipe Balbi 
1617b99d3659SBin Liu 	musb_dbg(musb, "gadget D+ pullup %s",
1618e71eb392SSebastian Andrzej Siewior 		is_on ? "on" : "off");
1619550a7375SFelipe Balbi 	musb_writeb(musb->mregs, MUSB_POWER, power);
1620550a7375SFelipe Balbi }
1621550a7375SFelipe Balbi 
1622550a7375SFelipe Balbi #if 0
1623550a7375SFelipe Balbi static int musb_gadget_vbus_session(struct usb_gadget *gadget, int is_active)
1624550a7375SFelipe Balbi {
1625b99d3659SBin Liu 	musb_dbg(musb, "<= %s =>\n", __func__);
1626550a7375SFelipe Balbi 
1627550a7375SFelipe Balbi 	/*
1628550a7375SFelipe Balbi 	 * FIXME iff driver's softconnect flag is set (as it is during probe,
1629550a7375SFelipe Balbi 	 * though that can clear it), just musb_pullup().
1630550a7375SFelipe Balbi 	 */
1631550a7375SFelipe Balbi 
1632550a7375SFelipe Balbi 	return -EINVAL;
1633550a7375SFelipe Balbi }
1634550a7375SFelipe Balbi #endif
1635550a7375SFelipe Balbi 
musb_gadget_vbus_draw(struct usb_gadget * gadget,unsigned mA)1636550a7375SFelipe Balbi static int musb_gadget_vbus_draw(struct usb_gadget *gadget, unsigned mA)
1637550a7375SFelipe Balbi {
1638550a7375SFelipe Balbi 	struct musb	*musb = gadget_to_musb(gadget);
1639550a7375SFelipe Balbi 
1640b96d3b08SHeikki Krogerus 	return usb_phy_set_power(musb->xceiv, mA);
1641550a7375SFelipe Balbi }
1642550a7375SFelipe Balbi 
musb_gadget_work(struct work_struct * work)1643517bafffSTony Lindgren static void musb_gadget_work(struct work_struct *work)
1644517bafffSTony Lindgren {
1645517bafffSTony Lindgren 	struct musb *musb;
1646517bafffSTony Lindgren 	unsigned long flags;
1647517bafffSTony Lindgren 
1648517bafffSTony Lindgren 	musb = container_of(work, struct musb, gadget_work.work);
1649517bafffSTony Lindgren 	pm_runtime_get_sync(musb->controller);
1650517bafffSTony Lindgren 	spin_lock_irqsave(&musb->lock, flags);
1651517bafffSTony Lindgren 	musb_pullup(musb, musb->softconnect);
1652517bafffSTony Lindgren 	spin_unlock_irqrestore(&musb->lock, flags);
1653517bafffSTony Lindgren 	pm_runtime_mark_last_busy(musb->controller);
1654517bafffSTony Lindgren 	pm_runtime_put_autosuspend(musb->controller);
1655517bafffSTony Lindgren }
1656517bafffSTony Lindgren 
musb_gadget_pullup(struct usb_gadget * gadget,int is_on)1657550a7375SFelipe Balbi static int musb_gadget_pullup(struct usb_gadget *gadget, int is_on)
1658550a7375SFelipe Balbi {
1659550a7375SFelipe Balbi 	struct musb	*musb = gadget_to_musb(gadget);
1660550a7375SFelipe Balbi 	unsigned long	flags;
1661550a7375SFelipe Balbi 
1662550a7375SFelipe Balbi 	is_on = !!is_on;
1663550a7375SFelipe Balbi 
1664550a7375SFelipe Balbi 	/* NOTE: this assumes we are sensing vbus; we'd rather
1665550a7375SFelipe Balbi 	 * not pullup unless the B-session is active.
1666550a7375SFelipe Balbi 	 */
1667550a7375SFelipe Balbi 	spin_lock_irqsave(&musb->lock, flags);
1668550a7375SFelipe Balbi 	if (is_on != musb->softconnect) {
1669550a7375SFelipe Balbi 		musb->softconnect = is_on;
1670517bafffSTony Lindgren 		schedule_delayed_work(&musb->gadget_work, 0);
1671550a7375SFelipe Balbi 	}
1672550a7375SFelipe Balbi 	spin_unlock_irqrestore(&musb->lock, flags);
167393e098a8SJohn Stultz 
1674550a7375SFelipe Balbi 	return 0;
1675550a7375SFelipe Balbi }
1676550a7375SFelipe Balbi 
1677e71eb392SSebastian Andrzej Siewior static int musb_gadget_start(struct usb_gadget *g,
1678e71eb392SSebastian Andrzej Siewior 		struct usb_gadget_driver *driver);
167922835b80SFelipe Balbi static int musb_gadget_stop(struct usb_gadget *g);
16800f91349bSSebastian Andrzej Siewior 
1681550a7375SFelipe Balbi static const struct usb_gadget_ops musb_gadget_operations = {
1682550a7375SFelipe Balbi 	.get_frame		= musb_gadget_get_frame,
1683550a7375SFelipe Balbi 	.wakeup			= musb_gadget_wakeup,
1684550a7375SFelipe Balbi 	.set_selfpowered	= musb_gadget_set_self_powered,
1685550a7375SFelipe Balbi 	/* .vbus_session		= musb_gadget_vbus_session, */
1686550a7375SFelipe Balbi 	.vbus_draw		= musb_gadget_vbus_draw,
1687550a7375SFelipe Balbi 	.pullup			= musb_gadget_pullup,
1688e71eb392SSebastian Andrzej Siewior 	.udc_start		= musb_gadget_start,
1689e71eb392SSebastian Andrzej Siewior 	.udc_stop		= musb_gadget_stop,
1690550a7375SFelipe Balbi };
1691550a7375SFelipe Balbi 
1692550a7375SFelipe Balbi /* ----------------------------------------------------------------------- */
1693550a7375SFelipe Balbi 
1694550a7375SFelipe Balbi /* Registration */
1695550a7375SFelipe Balbi 
1696550a7375SFelipe Balbi /* Only this registration code "knows" the rule (from USB standards)
1697550a7375SFelipe Balbi  * about there being only one external upstream port.  It assumes
1698550a7375SFelipe Balbi  * all peripheral ports are external...
1699550a7375SFelipe Balbi  */
1700550a7375SFelipe Balbi 
170141ac7b3aSBill Pemberton static void
init_peripheral_ep(struct musb * musb,struct musb_ep * ep,u8 epnum,int is_in)1702550a7375SFelipe Balbi init_peripheral_ep(struct musb *musb, struct musb_ep *ep, u8 epnum, int is_in)
1703550a7375SFelipe Balbi {
1704550a7375SFelipe Balbi 	struct musb_hw_ep	*hw_ep = musb->endpoints + epnum;
1705550a7375SFelipe Balbi 
1706550a7375SFelipe Balbi 	memset(ep, 0, sizeof *ep);
1707550a7375SFelipe Balbi 
1708550a7375SFelipe Balbi 	ep->current_epnum = epnum;
1709550a7375SFelipe Balbi 	ep->musb = musb;
1710550a7375SFelipe Balbi 	ep->hw_ep = hw_ep;
1711550a7375SFelipe Balbi 	ep->is_in = is_in;
1712550a7375SFelipe Balbi 
1713550a7375SFelipe Balbi 	INIT_LIST_HEAD(&ep->req_list);
1714550a7375SFelipe Balbi 
1715550a7375SFelipe Balbi 	sprintf(ep->name, "ep%d%s", epnum,
1716550a7375SFelipe Balbi 			(!epnum || hw_ep->is_shared_fifo) ? "" : (
1717550a7375SFelipe Balbi 				is_in ? "in" : "out"));
1718550a7375SFelipe Balbi 	ep->end_point.name = ep->name;
1719550a7375SFelipe Balbi 	INIT_LIST_HEAD(&ep->end_point.ep_list);
1720550a7375SFelipe Balbi 	if (!epnum) {
1721e117e742SRobert Baldyga 		usb_ep_set_maxpacket_limit(&ep->end_point, 64);
17228501955eSRobert Baldyga 		ep->end_point.caps.type_control = true;
1723550a7375SFelipe Balbi 		ep->end_point.ops = &musb_g_ep0_ops;
1724550a7375SFelipe Balbi 		musb->g.ep0 = &ep->end_point;
1725550a7375SFelipe Balbi 	} else {
1726550a7375SFelipe Balbi 		if (is_in)
1727e117e742SRobert Baldyga 			usb_ep_set_maxpacket_limit(&ep->end_point, hw_ep->max_packet_sz_tx);
1728550a7375SFelipe Balbi 		else
1729e117e742SRobert Baldyga 			usb_ep_set_maxpacket_limit(&ep->end_point, hw_ep->max_packet_sz_rx);
17308501955eSRobert Baldyga 		ep->end_point.caps.type_iso = true;
17318501955eSRobert Baldyga 		ep->end_point.caps.type_bulk = true;
17328501955eSRobert Baldyga 		ep->end_point.caps.type_int = true;
1733550a7375SFelipe Balbi 		ep->end_point.ops = &musb_ep_ops;
1734550a7375SFelipe Balbi 		list_add_tail(&ep->end_point.ep_list, &musb->g.ep_list);
1735550a7375SFelipe Balbi 	}
17368501955eSRobert Baldyga 
17378501955eSRobert Baldyga 	if (!epnum || hw_ep->is_shared_fifo) {
17388501955eSRobert Baldyga 		ep->end_point.caps.dir_in = true;
17398501955eSRobert Baldyga 		ep->end_point.caps.dir_out = true;
17408501955eSRobert Baldyga 	} else if (is_in)
17418501955eSRobert Baldyga 		ep->end_point.caps.dir_in = true;
17428501955eSRobert Baldyga 	else
17438501955eSRobert Baldyga 		ep->end_point.caps.dir_out = true;
1744550a7375SFelipe Balbi }
1745550a7375SFelipe Balbi 
1746550a7375SFelipe Balbi /*
1747550a7375SFelipe Balbi  * Initialize the endpoints exposed to peripheral drivers, with backlinks
1748550a7375SFelipe Balbi  * to the rest of the driver state.
1749550a7375SFelipe Balbi  */
musb_g_init_endpoints(struct musb * musb)175041ac7b3aSBill Pemberton static inline void musb_g_init_endpoints(struct musb *musb)
1751550a7375SFelipe Balbi {
1752550a7375SFelipe Balbi 	u8			epnum;
1753550a7375SFelipe Balbi 	struct musb_hw_ep	*hw_ep;
1754550a7375SFelipe Balbi 	unsigned		count = 0;
1755550a7375SFelipe Balbi 
1756b595076aSUwe Kleine-König 	/* initialize endpoint list just once */
1757550a7375SFelipe Balbi 	INIT_LIST_HEAD(&(musb->g.ep_list));
1758550a7375SFelipe Balbi 
1759550a7375SFelipe Balbi 	for (epnum = 0, hw_ep = musb->endpoints;
1760550a7375SFelipe Balbi 			epnum < musb->nr_endpoints;
1761550a7375SFelipe Balbi 			epnum++, hw_ep++) {
1762550a7375SFelipe Balbi 		if (hw_ep->is_shared_fifo /* || !epnum */) {
1763550a7375SFelipe Balbi 			init_peripheral_ep(musb, &hw_ep->ep_in, epnum, 0);
1764550a7375SFelipe Balbi 			count++;
1765550a7375SFelipe Balbi 		} else {
1766550a7375SFelipe Balbi 			if (hw_ep->max_packet_sz_tx) {
1767550a7375SFelipe Balbi 				init_peripheral_ep(musb, &hw_ep->ep_in,
1768550a7375SFelipe Balbi 							epnum, 1);
1769550a7375SFelipe Balbi 				count++;
1770550a7375SFelipe Balbi 			}
1771550a7375SFelipe Balbi 			if (hw_ep->max_packet_sz_rx) {
1772550a7375SFelipe Balbi 				init_peripheral_ep(musb, &hw_ep->ep_out,
1773550a7375SFelipe Balbi 							epnum, 0);
1774550a7375SFelipe Balbi 				count++;
1775550a7375SFelipe Balbi 			}
1776550a7375SFelipe Balbi 		}
1777550a7375SFelipe Balbi 	}
1778550a7375SFelipe Balbi }
1779550a7375SFelipe Balbi 
1780550a7375SFelipe Balbi /* called once during driver setup to initialize and link into
1781550a7375SFelipe Balbi  * the driver model; memory is zeroed.
1782550a7375SFelipe Balbi  */
musb_gadget_setup(struct musb * musb)178341ac7b3aSBill Pemberton int musb_gadget_setup(struct musb *musb)
1784550a7375SFelipe Balbi {
1785550a7375SFelipe Balbi 	int status;
1786550a7375SFelipe Balbi 
1787550a7375SFelipe Balbi 	/* REVISIT minor race:  if (erroneously) setting up two
1788550a7375SFelipe Balbi 	 * musb peripherals at the same time, only the bus lock
1789550a7375SFelipe Balbi 	 * is probably held.
1790550a7375SFelipe Balbi 	 */
1791550a7375SFelipe Balbi 
1792550a7375SFelipe Balbi 	musb->g.ops = &musb_gadget_operations;
1793d327ab5bSMichal Nazarewicz 	musb->g.max_speed = USB_SPEED_HIGH;
1794550a7375SFelipe Balbi 	musb->g.speed = USB_SPEED_UNKNOWN;
1795550a7375SFelipe Balbi 
17961374a430SBin Liu 	MUSB_DEV_MODE(musb);
179721acc656SPaul Cercueil 	musb_set_state(musb, OTG_STATE_B_IDLE);
17981374a430SBin Liu 
1799550a7375SFelipe Balbi 	/* this "gadget" abstracts/virtualizes the controller */
1800550a7375SFelipe Balbi 	musb->g.name = musb_driver_name;
18010a9134bdSBin Liu 	/* don't support otg protocols */
1802fd3923a9SApelete Seketeli 	musb->g.is_otg = 0;
1803517bafffSTony Lindgren 	INIT_DELAYED_WORK(&musb->gadget_work, musb_gadget_work);
1804550a7375SFelipe Balbi 	musb_g_init_endpoints(musb);
1805550a7375SFelipe Balbi 
1806550a7375SFelipe Balbi 	musb->is_active = 0;
1807550a7375SFelipe Balbi 	musb_platform_try_idle(musb, 0);
1808550a7375SFelipe Balbi 
18090f91349bSSebastian Andrzej Siewior 	status = usb_add_gadget_udc(musb->controller, &musb->g);
18100f91349bSSebastian Andrzej Siewior 	if (status)
18110f91349bSSebastian Andrzej Siewior 		goto err;
18120f91349bSSebastian Andrzej Siewior 
18130f91349bSSebastian Andrzej Siewior 	return 0;
18140f91349bSSebastian Andrzej Siewior err:
18156193d699SSebastian Andrzej Siewior 	musb->g.dev.parent = NULL;
18160f91349bSSebastian Andrzej Siewior 	device_unregister(&musb->g.dev);
1817550a7375SFelipe Balbi 	return status;
1818550a7375SFelipe Balbi }
1819550a7375SFelipe Balbi 
musb_gadget_cleanup(struct musb * musb)1820550a7375SFelipe Balbi void musb_gadget_cleanup(struct musb *musb)
1821550a7375SFelipe Balbi {
18227ad76955SBin Liu 	if (musb->port_mode == MUSB_HOST)
182390474288SSebastian Andrzej Siewior 		return;
1824517bafffSTony Lindgren 
1825517bafffSTony Lindgren 	cancel_delayed_work_sync(&musb->gadget_work);
18260f91349bSSebastian Andrzej Siewior 	usb_del_gadget_udc(&musb->g);
1827550a7375SFelipe Balbi }
1828550a7375SFelipe Balbi 
1829550a7375SFelipe Balbi /*
1830550a7375SFelipe Balbi  * Register the gadget driver. Used by gadget drivers when
1831550a7375SFelipe Balbi  * registering themselves with the controller.
1832550a7375SFelipe Balbi  *
1833550a7375SFelipe Balbi  * -EINVAL something went wrong (not driver)
1834550a7375SFelipe Balbi  * -EBUSY another gadget is already using the controller
1835b595076aSUwe Kleine-König  * -ENOMEM no memory to perform the operation
1836550a7375SFelipe Balbi  *
1837550a7375SFelipe Balbi  * @param driver the gadget driver
1838550a7375SFelipe Balbi  * @return <0 if error, 0 if everything is fine
1839550a7375SFelipe Balbi  */
musb_gadget_start(struct usb_gadget * g,struct usb_gadget_driver * driver)1840e71eb392SSebastian Andrzej Siewior static int musb_gadget_start(struct usb_gadget *g,
1841e71eb392SSebastian Andrzej Siewior 		struct usb_gadget_driver *driver)
1842550a7375SFelipe Balbi {
1843e71eb392SSebastian Andrzej Siewior 	struct musb		*musb = gadget_to_musb(g);
184463eed2b5SFelipe Balbi 	unsigned long		flags;
1845032ec49fSFelipe Balbi 	int			retval = 0;
1846550a7375SFelipe Balbi 
1847032ec49fSFelipe Balbi 	if (driver->max_speed < USB_SPEED_HIGH) {
1848032ec49fSFelipe Balbi 		retval = -EINVAL;
1849032ec49fSFelipe Balbi 		goto err;
1850032ec49fSFelipe Balbi 	}
1851550a7375SFelipe Balbi 
18527acc6197SHema HK 	pm_runtime_get_sync(musb->controller);
18537acc6197SHema HK 
1854e71eb392SSebastian Andrzej Siewior 	musb->softconnect = 0;
1855550a7375SFelipe Balbi 	musb->gadget_driver = driver;
1856550a7375SFelipe Balbi 
1857550a7375SFelipe Balbi 	spin_lock_irqsave(&musb->lock, flags);
185843e699ceSGreg Kroah-Hartman 	musb->is_active = 1;
1859550a7375SFelipe Balbi 
1860a6d45ea0SPaul Cercueil 	if (musb->xceiv)
1861a6d45ea0SPaul Cercueil 		otg_set_peripheral(musb->xceiv->otg, &musb->g);
18620afddf1eSPaul Cercueil 	else
18630afddf1eSPaul Cercueil 		phy_set_mode(musb->phy, PHY_MODE_USB_DEVICE);
1864a6d45ea0SPaul Cercueil 
186521acc656SPaul Cercueil 	musb_set_state(musb, OTG_STATE_B_IDLE);
1866550a7375SFelipe Balbi 	spin_unlock_irqrestore(&musb->lock, flags);
1867550a7375SFelipe Balbi 
1868001dd84aSSebastian Andrzej Siewior 	musb_start(musb);
1869001dd84aSSebastian Andrzej Siewior 
1870550a7375SFelipe Balbi 	/* REVISIT:  funcall to other code, which also
1871550a7375SFelipe Balbi 	 * handles power budgeting ... this way also
1872550a7375SFelipe Balbi 	 * ensures HdrcStart is indirectly called.
1873550a7375SFelipe Balbi 	 */
1874a6d45ea0SPaul Cercueil 	if (musb->xceiv && musb->xceiv->last_event == USB_EVENT_ID)
1875b65ae0f1SGrazvydas Ignotas 		musb_platform_set_vbus(musb, 1);
1876550a7375SFelipe Balbi 
187730647217STony Lindgren 	pm_runtime_mark_last_busy(musb->controller);
187830647217STony Lindgren 	pm_runtime_put_autosuspend(musb->controller);
18797acc6197SHema HK 
188063eed2b5SFelipe Balbi 	return 0;
188163eed2b5SFelipe Balbi 
1882032ec49fSFelipe Balbi err:
1883550a7375SFelipe Balbi 	return retval;
1884550a7375SFelipe Balbi }
1885550a7375SFelipe Balbi 
1886550a7375SFelipe Balbi /*
1887550a7375SFelipe Balbi  * Unregister the gadget driver. Used by gadget drivers when
1888550a7375SFelipe Balbi  * unregistering themselves from the controller.
1889550a7375SFelipe Balbi  *
1890550a7375SFelipe Balbi  * @param driver the gadget driver to unregister
1891550a7375SFelipe Balbi  */
musb_gadget_stop(struct usb_gadget * g)189222835b80SFelipe Balbi static int musb_gadget_stop(struct usb_gadget *g)
1893550a7375SFelipe Balbi {
1894e71eb392SSebastian Andrzej Siewior 	struct musb	*musb = gadget_to_musb(g);
189563eed2b5SFelipe Balbi 	unsigned long	flags;
1896550a7375SFelipe Balbi 
18977acc6197SHema HK 	pm_runtime_get_sync(musb->controller);
18987acc6197SHema HK 
189963eed2b5SFelipe Balbi 	/*
190063eed2b5SFelipe Balbi 	 * REVISIT always use otg_set_peripheral() here too;
1901550a7375SFelipe Balbi 	 * this needs to shut down the OTG engine.
1902550a7375SFelipe Balbi 	 */
1903550a7375SFelipe Balbi 
1904550a7375SFelipe Balbi 	spin_lock_irqsave(&musb->lock, flags);
1905550a7375SFelipe Balbi 
1906550a7375SFelipe Balbi 	musb_hnp_stop(musb);
1907550a7375SFelipe Balbi 
1908550a7375SFelipe Balbi 	(void) musb_gadget_vbus_draw(&musb->g, 0);
1909550a7375SFelipe Balbi 
191021acc656SPaul Cercueil 	musb_set_state(musb, OTG_STATE_UNDEFINED);
1911d5638fcfSFelipe Balbi 	musb_stop(musb);
1912a6d45ea0SPaul Cercueil 
1913a6d45ea0SPaul Cercueil 	if (musb->xceiv)
19146e13c650SHeikki Krogerus 		otg_set_peripheral(musb->xceiv->otg, NULL);
19150afddf1eSPaul Cercueil 	else
19160afddf1eSPaul Cercueil 		phy_set_mode(musb->phy, PHY_MODE_INVALID);
1917550a7375SFelipe Balbi 
1918550a7375SFelipe Balbi 	musb->is_active = 0;
1919e21de10cSGrazvydas Ignotas 	musb->gadget_driver = NULL;
1920550a7375SFelipe Balbi 	musb_platform_try_idle(musb, 0);
1921550a7375SFelipe Balbi 	spin_unlock_irqrestore(&musb->lock, flags);
1922550a7375SFelipe Balbi 
1923032ec49fSFelipe Balbi 	/*
1924032ec49fSFelipe Balbi 	 * FIXME we need to be able to register another
1925550a7375SFelipe Balbi 	 * gadget driver here and have everything work;
1926550a7375SFelipe Balbi 	 * that currently misbehaves.
1927550a7375SFelipe Balbi 	 */
192863eed2b5SFelipe Balbi 
19294e719183STony Lindgren 	/* Force check of devctl register for PM runtime */
19307099dbc5STony Lindgren 	pm_runtime_mark_last_busy(musb->controller);
19317099dbc5STony Lindgren 	pm_runtime_put_autosuspend(musb->controller);
19327acc6197SHema HK 
193363eed2b5SFelipe Balbi 	return 0;
1934550a7375SFelipe Balbi }
1935550a7375SFelipe Balbi 
1936550a7375SFelipe Balbi /* ----------------------------------------------------------------------- */
1937550a7375SFelipe Balbi 
1938550a7375SFelipe Balbi /* lifecycle operations called through plat_uds.c */
1939550a7375SFelipe Balbi 
musb_g_resume(struct musb * musb)1940550a7375SFelipe Balbi void musb_g_resume(struct musb *musb)
1941550a7375SFelipe Balbi {
1942550a7375SFelipe Balbi 	musb->is_suspended = 0;
194321acc656SPaul Cercueil 	switch (musb_get_state(musb)) {
1944550a7375SFelipe Balbi 	case OTG_STATE_B_IDLE:
1945550a7375SFelipe Balbi 		break;
1946550a7375SFelipe Balbi 	case OTG_STATE_B_WAIT_ACON:
1947550a7375SFelipe Balbi 	case OTG_STATE_B_PERIPHERAL:
1948550a7375SFelipe Balbi 		musb->is_active = 1;
1949550a7375SFelipe Balbi 		if (musb->gadget_driver && musb->gadget_driver->resume) {
1950550a7375SFelipe Balbi 			spin_unlock(&musb->lock);
1951550a7375SFelipe Balbi 			musb->gadget_driver->resume(&musb->g);
1952550a7375SFelipe Balbi 			spin_lock(&musb->lock);
1953550a7375SFelipe Balbi 		}
1954550a7375SFelipe Balbi 		break;
1955550a7375SFelipe Balbi 	default:
1956550a7375SFelipe Balbi 		WARNING("unhandled RESUME transition (%s)\n",
1957285f28bfSPaul Cercueil 			musb_otg_state_string(musb));
1958550a7375SFelipe Balbi 	}
1959550a7375SFelipe Balbi }
1960550a7375SFelipe Balbi 
1961550a7375SFelipe Balbi /* called when SOF packets stop for 3+ msec */
musb_g_suspend(struct musb * musb)1962550a7375SFelipe Balbi void musb_g_suspend(struct musb *musb)
1963550a7375SFelipe Balbi {
1964550a7375SFelipe Balbi 	u8	devctl;
1965550a7375SFelipe Balbi 
1966550a7375SFelipe Balbi 	devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
1967b99d3659SBin Liu 	musb_dbg(musb, "musb_g_suspend: devctl %02x", devctl);
1968550a7375SFelipe Balbi 
196921acc656SPaul Cercueil 	switch (musb_get_state(musb)) {
1970550a7375SFelipe Balbi 	case OTG_STATE_B_IDLE:
1971550a7375SFelipe Balbi 		if ((devctl & MUSB_DEVCTL_VBUS) == MUSB_DEVCTL_VBUS)
197221acc656SPaul Cercueil 			musb_set_state(musb, OTG_STATE_B_PERIPHERAL);
1973550a7375SFelipe Balbi 		break;
1974550a7375SFelipe Balbi 	case OTG_STATE_B_PERIPHERAL:
1975550a7375SFelipe Balbi 		musb->is_suspended = 1;
1976550a7375SFelipe Balbi 		if (musb->gadget_driver && musb->gadget_driver->suspend) {
1977550a7375SFelipe Balbi 			spin_unlock(&musb->lock);
1978550a7375SFelipe Balbi 			musb->gadget_driver->suspend(&musb->g);
1979550a7375SFelipe Balbi 			spin_lock(&musb->lock);
1980550a7375SFelipe Balbi 		}
1981550a7375SFelipe Balbi 		break;
1982550a7375SFelipe Balbi 	default:
1983550a7375SFelipe Balbi 		/* REVISIT if B_HOST, clear DEVCTL.HOSTREQ;
1984550a7375SFelipe Balbi 		 * A_PERIPHERAL may need care too
1985550a7375SFelipe Balbi 		 */
1986b99d3659SBin Liu 		WARNING("unhandled SUSPEND transition (%s)",
1987285f28bfSPaul Cercueil 			musb_otg_state_string(musb));
1988550a7375SFelipe Balbi 	}
1989550a7375SFelipe Balbi }
1990550a7375SFelipe Balbi 
1991550a7375SFelipe Balbi /* Called during SRP */
musb_g_wakeup(struct musb * musb)1992550a7375SFelipe Balbi void musb_g_wakeup(struct musb *musb)
1993550a7375SFelipe Balbi {
1994550a7375SFelipe Balbi 	musb_gadget_wakeup(&musb->g);
1995550a7375SFelipe Balbi }
1996550a7375SFelipe Balbi 
1997550a7375SFelipe Balbi /* called when VBUS drops below session threshold, and in other cases */
musb_g_disconnect(struct musb * musb)1998550a7375SFelipe Balbi void musb_g_disconnect(struct musb *musb)
1999550a7375SFelipe Balbi {
2000550a7375SFelipe Balbi 	void __iomem	*mregs = musb->mregs;
2001550a7375SFelipe Balbi 	u8	devctl = musb_readb(mregs, MUSB_DEVCTL);
2002550a7375SFelipe Balbi 
2003b99d3659SBin Liu 	musb_dbg(musb, "musb_g_disconnect: devctl %02x", devctl);
2004550a7375SFelipe Balbi 
2005550a7375SFelipe Balbi 	/* clear HR */
2006550a7375SFelipe Balbi 	musb_writeb(mregs, MUSB_DEVCTL, devctl & MUSB_DEVCTL_SESSION);
2007550a7375SFelipe Balbi 
2008550a7375SFelipe Balbi 	/* don't draw vbus until new b-default session */
2009550a7375SFelipe Balbi 	(void) musb_gadget_vbus_draw(&musb->g, 0);
2010550a7375SFelipe Balbi 
2011550a7375SFelipe Balbi 	musb->g.speed = USB_SPEED_UNKNOWN;
2012550a7375SFelipe Balbi 	if (musb->gadget_driver && musb->gadget_driver->disconnect) {
2013550a7375SFelipe Balbi 		spin_unlock(&musb->lock);
2014550a7375SFelipe Balbi 		musb->gadget_driver->disconnect(&musb->g);
2015550a7375SFelipe Balbi 		spin_lock(&musb->lock);
2016550a7375SFelipe Balbi 	}
2017550a7375SFelipe Balbi 
201821acc656SPaul Cercueil 	switch (musb_get_state(musb)) {
2019550a7375SFelipe Balbi 	default:
2020b99d3659SBin Liu 		musb_dbg(musb, "Unhandled disconnect %s, setting a_idle",
2021285f28bfSPaul Cercueil 			 musb_otg_state_string(musb));
202221acc656SPaul Cercueil 		musb_set_state(musb, OTG_STATE_A_IDLE);
2023ab983f2aSDavid Brownell 		MUSB_HST_MODE(musb);
2024550a7375SFelipe Balbi 		break;
2025550a7375SFelipe Balbi 	case OTG_STATE_A_PERIPHERAL:
202621acc656SPaul Cercueil 		musb_set_state(musb, OTG_STATE_A_WAIT_BCON);
2027ab983f2aSDavid Brownell 		MUSB_HST_MODE(musb);
2028550a7375SFelipe Balbi 		break;
2029550a7375SFelipe Balbi 	case OTG_STATE_B_WAIT_ACON:
2030550a7375SFelipe Balbi 	case OTG_STATE_B_HOST:
2031550a7375SFelipe Balbi 	case OTG_STATE_B_PERIPHERAL:
2032550a7375SFelipe Balbi 	case OTG_STATE_B_IDLE:
203321acc656SPaul Cercueil 		musb_set_state(musb, OTG_STATE_B_IDLE);
2034550a7375SFelipe Balbi 		break;
2035550a7375SFelipe Balbi 	case OTG_STATE_B_SRP_INIT:
2036550a7375SFelipe Balbi 		break;
2037550a7375SFelipe Balbi 	}
2038550a7375SFelipe Balbi 
2039550a7375SFelipe Balbi 	musb->is_active = 0;
2040550a7375SFelipe Balbi }
2041550a7375SFelipe Balbi 
musb_g_reset(struct musb * musb)2042550a7375SFelipe Balbi void musb_g_reset(struct musb *musb)
2043550a7375SFelipe Balbi __releases(musb->lock)
2044550a7375SFelipe Balbi __acquires(musb->lock)
2045550a7375SFelipe Balbi {
2046550a7375SFelipe Balbi 	void __iomem	*mbase = musb->mregs;
2047550a7375SFelipe Balbi 	u8		devctl = musb_readb(mbase, MUSB_DEVCTL);
2048550a7375SFelipe Balbi 	u8		power;
2049550a7375SFelipe Balbi 
2050b99d3659SBin Liu 	musb_dbg(musb, "<== %s driver '%s'",
2051550a7375SFelipe Balbi 			(devctl & MUSB_DEVCTL_BDEVICE)
2052550a7375SFelipe Balbi 				? "B-Device" : "A-Device",
2053550a7375SFelipe Balbi 			musb->gadget_driver
2054550a7375SFelipe Balbi 				? musb->gadget_driver->driver.name
2055550a7375SFelipe Balbi 				: NULL
2056550a7375SFelipe Balbi 			);
2057550a7375SFelipe Balbi 
20581189f7f6SFelipe Balbi 	/* report reset, if we didn't already (flushing EP state) */
20591189f7f6SFelipe Balbi 	if (musb->gadget_driver && musb->g.speed != USB_SPEED_UNKNOWN) {
20601189f7f6SFelipe Balbi 		spin_unlock(&musb->lock);
20611189f7f6SFelipe Balbi 		usb_gadget_udc_reset(&musb->g, musb->gadget_driver);
20621189f7f6SFelipe Balbi 		spin_lock(&musb->lock);
20631189f7f6SFelipe Balbi 	}
2064550a7375SFelipe Balbi 
2065550a7375SFelipe Balbi 	/* clear HR */
2066550a7375SFelipe Balbi 	else if (devctl & MUSB_DEVCTL_HR)
2067550a7375SFelipe Balbi 		musb_writeb(mbase, MUSB_DEVCTL, MUSB_DEVCTL_SESSION);
2068550a7375SFelipe Balbi 
2069550a7375SFelipe Balbi 
2070550a7375SFelipe Balbi 	/* what speed did we negotiate? */
2071550a7375SFelipe Balbi 	power = musb_readb(mbase, MUSB_POWER);
2072550a7375SFelipe Balbi 	musb->g.speed = (power & MUSB_POWER_HSMODE)
2073550a7375SFelipe Balbi 			? USB_SPEED_HIGH : USB_SPEED_FULL;
2074550a7375SFelipe Balbi 
2075550a7375SFelipe Balbi 	/* start in USB_STATE_DEFAULT */
2076550a7375SFelipe Balbi 	musb->is_active = 1;
2077550a7375SFelipe Balbi 	musb->is_suspended = 0;
2078550a7375SFelipe Balbi 	MUSB_DEV_MODE(musb);
2079550a7375SFelipe Balbi 	musb->address = 0;
2080550a7375SFelipe Balbi 	musb->ep0_state = MUSB_EP0_STAGE_SETUP;
2081550a7375SFelipe Balbi 
2082550a7375SFelipe Balbi 	musb->may_wakeup = 0;
2083550a7375SFelipe Balbi 	musb->g.b_hnp_enable = 0;
2084550a7375SFelipe Balbi 	musb->g.a_alt_hnp_support = 0;
2085550a7375SFelipe Balbi 	musb->g.a_hnp_support = 0;
2086ca1023c8SRobert Baldyga 	musb->g.quirk_zlp_not_supp = 1;
2087550a7375SFelipe Balbi 
2088550a7375SFelipe Balbi 	/* Normal reset, as B-Device;
2089550a7375SFelipe Balbi 	 * or else after HNP, as A-Device
2090550a7375SFelipe Balbi 	 */
209123db9fd2SApelete Seketeli 	if (!musb->g.is_otg) {
209223db9fd2SApelete Seketeli 		/* USB device controllers that are not OTG compatible
209323db9fd2SApelete Seketeli 		 * may not have DEVCTL register in silicon.
209423db9fd2SApelete Seketeli 		 * In that case, do not rely on devctl for setting
209523db9fd2SApelete Seketeli 		 * peripheral mode.
209623db9fd2SApelete Seketeli 		 */
209721acc656SPaul Cercueil 		musb_set_state(musb, OTG_STATE_B_PERIPHERAL);
209823db9fd2SApelete Seketeli 		musb->g.is_a_peripheral = 0;
209923db9fd2SApelete Seketeli 	} else if (devctl & MUSB_DEVCTL_BDEVICE) {
210021acc656SPaul Cercueil 		musb_set_state(musb, OTG_STATE_B_PERIPHERAL);
2101550a7375SFelipe Balbi 		musb->g.is_a_peripheral = 0;
2102032ec49fSFelipe Balbi 	} else {
210321acc656SPaul Cercueil 		musb_set_state(musb, OTG_STATE_A_PERIPHERAL);
2104550a7375SFelipe Balbi 		musb->g.is_a_peripheral = 1;
2105032ec49fSFelipe Balbi 	}
2106550a7375SFelipe Balbi 
2107550a7375SFelipe Balbi 	/* start with default limits on VBUS power draw */
2108032ec49fSFelipe Balbi 	(void) musb_gadget_vbus_draw(&musb->g, 8);
2109550a7375SFelipe Balbi }
2110