xref: /openbmc/linux/drivers/usb/musb/musb_gadget.c (revision b99d3659b309b358e5b789e644b046d6721c9da4)
1550a7375SFelipe Balbi /*
2550a7375SFelipe Balbi  * MUSB OTG driver peripheral support
3550a7375SFelipe Balbi  *
4550a7375SFelipe Balbi  * Copyright 2005 Mentor Graphics Corporation
5550a7375SFelipe Balbi  * Copyright (C) 2005-2006 by Texas Instruments
6550a7375SFelipe Balbi  * Copyright (C) 2006-2007 Nokia Corporation
7cea83241SSergei Shtylyov  * Copyright (C) 2009 MontaVista Software, Inc. <source@mvista.com>
8550a7375SFelipe Balbi  *
9550a7375SFelipe Balbi  * This program is free software; you can redistribute it and/or
10550a7375SFelipe Balbi  * modify it under the terms of the GNU General Public License
11550a7375SFelipe Balbi  * version 2 as published by the Free Software Foundation.
12550a7375SFelipe Balbi  *
13550a7375SFelipe Balbi  * This program is distributed in the hope that it will be useful, but
14550a7375SFelipe Balbi  * WITHOUT ANY WARRANTY; without even the implied warranty of
15550a7375SFelipe Balbi  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16550a7375SFelipe Balbi  * General Public License for more details.
17550a7375SFelipe Balbi  *
18550a7375SFelipe Balbi  * You should have received a copy of the GNU General Public License
19550a7375SFelipe Balbi  * along with this program; if not, write to the Free Software
20550a7375SFelipe Balbi  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
21550a7375SFelipe Balbi  * 02110-1301 USA
22550a7375SFelipe Balbi  *
23550a7375SFelipe Balbi  * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
24550a7375SFelipe Balbi  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
25550a7375SFelipe Balbi  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN
26550a7375SFelipe Balbi  * NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27550a7375SFelipe Balbi  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28550a7375SFelipe Balbi  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
29550a7375SFelipe Balbi  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
30550a7375SFelipe Balbi  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31550a7375SFelipe Balbi  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32550a7375SFelipe Balbi  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33550a7375SFelipe Balbi  *
34550a7375SFelipe Balbi  */
35550a7375SFelipe Balbi 
36550a7375SFelipe Balbi #include <linux/kernel.h>
37550a7375SFelipe Balbi #include <linux/list.h>
38550a7375SFelipe Balbi #include <linux/timer.h>
39550a7375SFelipe Balbi #include <linux/module.h>
40550a7375SFelipe Balbi #include <linux/smp.h>
41550a7375SFelipe Balbi #include <linux/spinlock.h>
42550a7375SFelipe Balbi #include <linux/delay.h>
43550a7375SFelipe Balbi #include <linux/dma-mapping.h>
445a0e3ad6STejun Heo #include <linux/slab.h>
45550a7375SFelipe Balbi 
46550a7375SFelipe Balbi #include "musb_core.h"
47550a7375SFelipe Balbi 
48550a7375SFelipe Balbi 
49550a7375SFelipe Balbi /* ----------------------------------------------------------------------- */
50550a7375SFelipe Balbi 
51c65bfa62SMian Yousaf Kaukab #define is_buffer_mapped(req) (is_dma_capable() && \
52c65bfa62SMian Yousaf Kaukab 					(req->map_state != UN_MAPPED))
53c65bfa62SMian Yousaf Kaukab 
5492d2711fSHema Kalliguddi /* Maps the buffer to dma  */
5592d2711fSHema Kalliguddi 
5692d2711fSHema Kalliguddi static inline void map_dma_buffer(struct musb_request *request,
57c65bfa62SMian Yousaf Kaukab 			struct musb *musb, struct musb_ep *musb_ep)
5892d2711fSHema Kalliguddi {
595f5761cbSMian Yousaf Kaukab 	int compatible = true;
605f5761cbSMian Yousaf Kaukab 	struct dma_controller *dma = musb->dma_controller;
615f5761cbSMian Yousaf Kaukab 
62c65bfa62SMian Yousaf Kaukab 	request->map_state = UN_MAPPED;
63c65bfa62SMian Yousaf Kaukab 
64c65bfa62SMian Yousaf Kaukab 	if (!is_dma_capable() || !musb_ep->dma)
65c65bfa62SMian Yousaf Kaukab 		return;
66c65bfa62SMian Yousaf Kaukab 
675f5761cbSMian Yousaf Kaukab 	/* Check if DMA engine can handle this request.
685f5761cbSMian Yousaf Kaukab 	 * DMA code must reject the USB request explicitly.
695f5761cbSMian Yousaf Kaukab 	 * Default behaviour is to map the request.
705f5761cbSMian Yousaf Kaukab 	 */
715f5761cbSMian Yousaf Kaukab 	if (dma->is_compatible)
725f5761cbSMian Yousaf Kaukab 		compatible = dma->is_compatible(musb_ep->dma,
735f5761cbSMian Yousaf Kaukab 				musb_ep->packet_sz, request->request.buf,
745f5761cbSMian Yousaf Kaukab 				request->request.length);
755f5761cbSMian Yousaf Kaukab 	if (!compatible)
765f5761cbSMian Yousaf Kaukab 		return;
775f5761cbSMian Yousaf Kaukab 
7892d2711fSHema Kalliguddi 	if (request->request.dma == DMA_ADDR_INVALID) {
797b360f42SSebastian Andrzej Siewior 		dma_addr_t dma_addr;
807b360f42SSebastian Andrzej Siewior 		int ret;
817b360f42SSebastian Andrzej Siewior 
827b360f42SSebastian Andrzej Siewior 		dma_addr = dma_map_single(
8392d2711fSHema Kalliguddi 				musb->controller,
8492d2711fSHema Kalliguddi 				request->request.buf,
8592d2711fSHema Kalliguddi 				request->request.length,
8692d2711fSHema Kalliguddi 				request->tx
8792d2711fSHema Kalliguddi 					? DMA_TO_DEVICE
8892d2711fSHema Kalliguddi 					: DMA_FROM_DEVICE);
897b360f42SSebastian Andrzej Siewior 		ret = dma_mapping_error(musb->controller, dma_addr);
907b360f42SSebastian Andrzej Siewior 		if (ret)
917b360f42SSebastian Andrzej Siewior 			return;
927b360f42SSebastian Andrzej Siewior 
937b360f42SSebastian Andrzej Siewior 		request->request.dma = dma_addr;
94c65bfa62SMian Yousaf Kaukab 		request->map_state = MUSB_MAPPED;
9592d2711fSHema Kalliguddi 	} else {
9692d2711fSHema Kalliguddi 		dma_sync_single_for_device(musb->controller,
9792d2711fSHema Kalliguddi 			request->request.dma,
9892d2711fSHema Kalliguddi 			request->request.length,
9992d2711fSHema Kalliguddi 			request->tx
10092d2711fSHema Kalliguddi 				? DMA_TO_DEVICE
10192d2711fSHema Kalliguddi 				: DMA_FROM_DEVICE);
102c65bfa62SMian Yousaf Kaukab 		request->map_state = PRE_MAPPED;
10392d2711fSHema Kalliguddi 	}
10492d2711fSHema Kalliguddi }
10592d2711fSHema Kalliguddi 
10692d2711fSHema Kalliguddi /* Unmap the buffer from dma and maps it back to cpu */
10792d2711fSHema Kalliguddi static inline void unmap_dma_buffer(struct musb_request *request,
10892d2711fSHema Kalliguddi 				struct musb *musb)
10992d2711fSHema Kalliguddi {
11006d9db72SKishon Vijay Abraham I 	struct musb_ep *musb_ep = request->ep;
11106d9db72SKishon Vijay Abraham I 
11206d9db72SKishon Vijay Abraham I 	if (!is_buffer_mapped(request) || !musb_ep->dma)
113c65bfa62SMian Yousaf Kaukab 		return;
114c65bfa62SMian Yousaf Kaukab 
11592d2711fSHema Kalliguddi 	if (request->request.dma == DMA_ADDR_INVALID) {
1165c8a86e1SFelipe Balbi 		dev_vdbg(musb->controller,
1175c8a86e1SFelipe Balbi 				"not unmapping a never mapped buffer\n");
11892d2711fSHema Kalliguddi 		return;
11992d2711fSHema Kalliguddi 	}
120c65bfa62SMian Yousaf Kaukab 	if (request->map_state == MUSB_MAPPED) {
12192d2711fSHema Kalliguddi 		dma_unmap_single(musb->controller,
12292d2711fSHema Kalliguddi 			request->request.dma,
12392d2711fSHema Kalliguddi 			request->request.length,
12492d2711fSHema Kalliguddi 			request->tx
12592d2711fSHema Kalliguddi 				? DMA_TO_DEVICE
12692d2711fSHema Kalliguddi 				: DMA_FROM_DEVICE);
12792d2711fSHema Kalliguddi 		request->request.dma = DMA_ADDR_INVALID;
128c65bfa62SMian Yousaf Kaukab 	} else { /* PRE_MAPPED */
12992d2711fSHema Kalliguddi 		dma_sync_single_for_cpu(musb->controller,
13092d2711fSHema Kalliguddi 			request->request.dma,
13192d2711fSHema Kalliguddi 			request->request.length,
13292d2711fSHema Kalliguddi 			request->tx
13392d2711fSHema Kalliguddi 				? DMA_TO_DEVICE
13492d2711fSHema Kalliguddi 				: DMA_FROM_DEVICE);
13592d2711fSHema Kalliguddi 	}
136c65bfa62SMian Yousaf Kaukab 	request->map_state = UN_MAPPED;
13792d2711fSHema Kalliguddi }
13892d2711fSHema Kalliguddi 
139550a7375SFelipe Balbi /*
140550a7375SFelipe Balbi  * Immediately complete a request.
141550a7375SFelipe Balbi  *
142550a7375SFelipe Balbi  * @param request the request to complete
143550a7375SFelipe Balbi  * @param status the status to complete the request with
144550a7375SFelipe Balbi  * Context: controller locked, IRQs blocked.
145550a7375SFelipe Balbi  */
146550a7375SFelipe Balbi void musb_g_giveback(
147550a7375SFelipe Balbi 	struct musb_ep		*ep,
148550a7375SFelipe Balbi 	struct usb_request	*request,
149550a7375SFelipe Balbi 	int			status)
150550a7375SFelipe Balbi __releases(ep->musb->lock)
151550a7375SFelipe Balbi __acquires(ep->musb->lock)
152550a7375SFelipe Balbi {
153550a7375SFelipe Balbi 	struct musb_request	*req;
154550a7375SFelipe Balbi 	struct musb		*musb;
155550a7375SFelipe Balbi 	int			busy = ep->busy;
156550a7375SFelipe Balbi 
157550a7375SFelipe Balbi 	req = to_musb_request(request);
158550a7375SFelipe Balbi 
159ad1adb89SFelipe Balbi 	list_del(&req->list);
160550a7375SFelipe Balbi 	if (req->request.status == -EINPROGRESS)
161550a7375SFelipe Balbi 		req->request.status = status;
162550a7375SFelipe Balbi 	musb = req->musb;
163550a7375SFelipe Balbi 
164550a7375SFelipe Balbi 	ep->busy = 1;
165550a7375SFelipe Balbi 	spin_unlock(&musb->lock);
16606d9db72SKishon Vijay Abraham I 
16706d9db72SKishon Vijay Abraham I 	if (!dma_mapping_error(&musb->g.dev, request->dma))
16892d2711fSHema Kalliguddi 		unmap_dma_buffer(req, musb);
16906d9db72SKishon Vijay Abraham I 
170550a7375SFelipe Balbi 	if (request->status == 0)
171*b99d3659SBin Liu 		musb_dbg(musb, "%s done request %p,  %d/",
172550a7375SFelipe Balbi 				ep->end_point.name, request,
173550a7375SFelipe Balbi 				req->request.actual, req->request.length);
174550a7375SFelipe Balbi 	else
175*b99d3659SBin Liu 		musb_dbg(musb, "%s request %p, %d/%d fault %d",
176550a7375SFelipe Balbi 				ep->end_point.name, request,
177550a7375SFelipe Balbi 				req->request.actual, req->request.length,
178550a7375SFelipe Balbi 				request->status);
179304f7e5eSMichal Sojka 	usb_gadget_giveback_request(&req->ep->end_point, &req->request);
180550a7375SFelipe Balbi 	spin_lock(&musb->lock);
181550a7375SFelipe Balbi 	ep->busy = busy;
182550a7375SFelipe Balbi }
183550a7375SFelipe Balbi 
184550a7375SFelipe Balbi /* ----------------------------------------------------------------------- */
185550a7375SFelipe Balbi 
186550a7375SFelipe Balbi /*
187550a7375SFelipe Balbi  * Abort requests queued to an endpoint using the status. Synchronous.
188550a7375SFelipe Balbi  * caller locked controller and blocked irqs, and selected this ep.
189550a7375SFelipe Balbi  */
190550a7375SFelipe Balbi static void nuke(struct musb_ep *ep, const int status)
191550a7375SFelipe Balbi {
1925c8a86e1SFelipe Balbi 	struct musb		*musb = ep->musb;
193550a7375SFelipe Balbi 	struct musb_request	*req = NULL;
194550a7375SFelipe Balbi 	void __iomem *epio = ep->musb->endpoints[ep->current_epnum].regs;
195550a7375SFelipe Balbi 
196550a7375SFelipe Balbi 	ep->busy = 1;
197550a7375SFelipe Balbi 
198550a7375SFelipe Balbi 	if (is_dma_capable() && ep->dma) {
199550a7375SFelipe Balbi 		struct dma_controller	*c = ep->musb->dma_controller;
200550a7375SFelipe Balbi 		int value;
201b6e434a5SSergei Shtylyov 
202550a7375SFelipe Balbi 		if (ep->is_in) {
203b6e434a5SSergei Shtylyov 			/*
204b6e434a5SSergei Shtylyov 			 * The programming guide says that we must not clear
205b6e434a5SSergei Shtylyov 			 * the DMAMODE bit before DMAENAB, so we only
206b6e434a5SSergei Shtylyov 			 * clear it in the second write...
207b6e434a5SSergei Shtylyov 			 */
208550a7375SFelipe Balbi 			musb_writew(epio, MUSB_TXCSR,
209b6e434a5SSergei Shtylyov 				    MUSB_TXCSR_DMAMODE | MUSB_TXCSR_FLUSHFIFO);
210550a7375SFelipe Balbi 			musb_writew(epio, MUSB_TXCSR,
211550a7375SFelipe Balbi 					0 | MUSB_TXCSR_FLUSHFIFO);
212550a7375SFelipe Balbi 		} else {
213550a7375SFelipe Balbi 			musb_writew(epio, MUSB_RXCSR,
214550a7375SFelipe Balbi 					0 | MUSB_RXCSR_FLUSHFIFO);
215550a7375SFelipe Balbi 			musb_writew(epio, MUSB_RXCSR,
216550a7375SFelipe Balbi 					0 | MUSB_RXCSR_FLUSHFIFO);
217550a7375SFelipe Balbi 		}
218550a7375SFelipe Balbi 
219550a7375SFelipe Balbi 		value = c->channel_abort(ep->dma);
220*b99d3659SBin Liu 		musb_dbg(musb, "%s: abort DMA --> %d", ep->name, value);
221550a7375SFelipe Balbi 		c->channel_release(ep->dma);
222550a7375SFelipe Balbi 		ep->dma = NULL;
223550a7375SFelipe Balbi 	}
224550a7375SFelipe Balbi 
225ad1adb89SFelipe Balbi 	while (!list_empty(&ep->req_list)) {
226ad1adb89SFelipe Balbi 		req = list_first_entry(&ep->req_list, struct musb_request, list);
227550a7375SFelipe Balbi 		musb_g_giveback(ep, &req->request, status);
228550a7375SFelipe Balbi 	}
229550a7375SFelipe Balbi }
230550a7375SFelipe Balbi 
231550a7375SFelipe Balbi /* ----------------------------------------------------------------------- */
232550a7375SFelipe Balbi 
233550a7375SFelipe Balbi /* Data transfers - pure PIO, pure DMA, or mixed mode */
234550a7375SFelipe Balbi 
235550a7375SFelipe Balbi /*
236550a7375SFelipe Balbi  * This assumes the separate CPPI engine is responding to DMA requests
237550a7375SFelipe Balbi  * from the usb core ... sequenced a bit differently from mentor dma.
238550a7375SFelipe Balbi  */
239550a7375SFelipe Balbi 
240550a7375SFelipe Balbi static inline int max_ep_writesize(struct musb *musb, struct musb_ep *ep)
241550a7375SFelipe Balbi {
242550a7375SFelipe Balbi 	if (can_bulk_split(musb, ep->type))
243550a7375SFelipe Balbi 		return ep->hw_ep->max_packet_sz_tx;
244550a7375SFelipe Balbi 	else
245550a7375SFelipe Balbi 		return ep->packet_sz;
246550a7375SFelipe Balbi }
247550a7375SFelipe Balbi 
248550a7375SFelipe Balbi /*
249550a7375SFelipe Balbi  * An endpoint is transmitting data. This can be called either from
250550a7375SFelipe Balbi  * the IRQ routine or from ep.queue() to kickstart a request on an
251550a7375SFelipe Balbi  * endpoint.
252550a7375SFelipe Balbi  *
253550a7375SFelipe Balbi  * Context: controller locked, IRQs blocked, endpoint selected
254550a7375SFelipe Balbi  */
255550a7375SFelipe Balbi static void txstate(struct musb *musb, struct musb_request *req)
256550a7375SFelipe Balbi {
257550a7375SFelipe Balbi 	u8			epnum = req->epnum;
258550a7375SFelipe Balbi 	struct musb_ep		*musb_ep;
259550a7375SFelipe Balbi 	void __iomem		*epio = musb->endpoints[epnum].regs;
260550a7375SFelipe Balbi 	struct usb_request	*request;
261550a7375SFelipe Balbi 	u16			fifo_count = 0, csr;
262550a7375SFelipe Balbi 	int			use_dma = 0;
263550a7375SFelipe Balbi 
264550a7375SFelipe Balbi 	musb_ep = req->ep;
265550a7375SFelipe Balbi 
266abf710e6SVikram Pandita 	/* Check if EP is disabled */
267abf710e6SVikram Pandita 	if (!musb_ep->desc) {
268*b99d3659SBin Liu 		musb_dbg(musb, "ep:%s disabled - ignore request",
269abf710e6SVikram Pandita 						musb_ep->end_point.name);
270abf710e6SVikram Pandita 		return;
271abf710e6SVikram Pandita 	}
272abf710e6SVikram Pandita 
273550a7375SFelipe Balbi 	/* we shouldn't get here while DMA is active ... but we do ... */
274550a7375SFelipe Balbi 	if (dma_channel_status(musb_ep->dma) == MUSB_DMA_STATUS_BUSY) {
275*b99d3659SBin Liu 		musb_dbg(musb, "dma pending...");
276550a7375SFelipe Balbi 		return;
277550a7375SFelipe Balbi 	}
278550a7375SFelipe Balbi 
279550a7375SFelipe Balbi 	/* read TXCSR before */
280550a7375SFelipe Balbi 	csr = musb_readw(epio, MUSB_TXCSR);
281550a7375SFelipe Balbi 
282550a7375SFelipe Balbi 	request = &req->request;
283550a7375SFelipe Balbi 	fifo_count = min(max_ep_writesize(musb, musb_ep),
284550a7375SFelipe Balbi 			(int)(request->length - request->actual));
285550a7375SFelipe Balbi 
286550a7375SFelipe Balbi 	if (csr & MUSB_TXCSR_TXPKTRDY) {
287*b99d3659SBin Liu 		musb_dbg(musb, "%s old packet still ready , txcsr %03x",
288550a7375SFelipe Balbi 				musb_ep->end_point.name, csr);
289550a7375SFelipe Balbi 		return;
290550a7375SFelipe Balbi 	}
291550a7375SFelipe Balbi 
292550a7375SFelipe Balbi 	if (csr & MUSB_TXCSR_P_SENDSTALL) {
293*b99d3659SBin Liu 		musb_dbg(musb, "%s stalling, txcsr %03x",
294550a7375SFelipe Balbi 				musb_ep->end_point.name, csr);
295550a7375SFelipe Balbi 		return;
296550a7375SFelipe Balbi 	}
297550a7375SFelipe Balbi 
298*b99d3659SBin Liu 	musb_dbg(musb, "hw_ep%d, maxpacket %d, fifo count %d, txcsr %03x",
299550a7375SFelipe Balbi 			epnum, musb_ep->packet_sz, fifo_count,
300550a7375SFelipe Balbi 			csr);
301550a7375SFelipe Balbi 
302550a7375SFelipe Balbi #ifndef	CONFIG_MUSB_PIO_ONLY
303c65bfa62SMian Yousaf Kaukab 	if (is_buffer_mapped(req)) {
304550a7375SFelipe Balbi 		struct dma_controller	*c = musb->dma_controller;
30566af83ddSMing Lei 		size_t request_size;
30666af83ddSMing Lei 
30766af83ddSMing Lei 		/* setup DMA, then program endpoint CSR */
30866af83ddSMing Lei 		request_size = min_t(size_t, request->length - request->actual,
30966af83ddSMing Lei 					musb_ep->dma->max_len);
310550a7375SFelipe Balbi 
311d17d535fSAjay Kumar Gupta 		use_dma = (request->dma != DMA_ADDR_INVALID && request_size);
312550a7375SFelipe Balbi 
313550a7375SFelipe Balbi 		/* MUSB_TXCSR_P_ISO is still set correctly */
314550a7375SFelipe Balbi 
31503840fadSFelipe Balbi 		if (musb_dma_inventra(musb) || musb_dma_ux500(musb)) {
316d1043a26SAnand Gadiyar 			if (request_size < musb_ep->packet_sz)
317550a7375SFelipe Balbi 				musb_ep->dma->desired_mode = 0;
318550a7375SFelipe Balbi 			else
319550a7375SFelipe Balbi 				musb_ep->dma->desired_mode = 1;
320550a7375SFelipe Balbi 
321550a7375SFelipe Balbi 			use_dma = use_dma && c->channel_program(
322550a7375SFelipe Balbi 					musb_ep->dma, musb_ep->packet_sz,
323550a7375SFelipe Balbi 					musb_ep->dma->desired_mode,
324796a83faSCliff Cai 					request->dma + request->actual, request_size);
325550a7375SFelipe Balbi 			if (use_dma) {
326550a7375SFelipe Balbi 				if (musb_ep->dma->desired_mode == 0) {
327b6e434a5SSergei Shtylyov 					/*
328b6e434a5SSergei Shtylyov 					 * We must not clear the DMAMODE bit
329b6e434a5SSergei Shtylyov 					 * before the DMAENAB bit -- and the
330b6e434a5SSergei Shtylyov 					 * latter doesn't always get cleared
331b6e434a5SSergei Shtylyov 					 * before we get here...
332b6e434a5SSergei Shtylyov 					 */
333b6e434a5SSergei Shtylyov 					csr &= ~(MUSB_TXCSR_AUTOSET
334b6e434a5SSergei Shtylyov 						| MUSB_TXCSR_DMAENAB);
335b6e434a5SSergei Shtylyov 					musb_writew(epio, MUSB_TXCSR, csr
336b6e434a5SSergei Shtylyov 						| MUSB_TXCSR_P_WZC_BITS);
337b6e434a5SSergei Shtylyov 					csr &= ~MUSB_TXCSR_DMAMODE;
338550a7375SFelipe Balbi 					csr |= (MUSB_TXCSR_DMAENAB |
339550a7375SFelipe Balbi 							MUSB_TXCSR_MODE);
340550a7375SFelipe Balbi 					/* against programming guide */
341f11d893dSMing Lei 				} else {
342f11d893dSMing Lei 					csr |= (MUSB_TXCSR_DMAENAB
343550a7375SFelipe Balbi 							| MUSB_TXCSR_DMAMODE
344550a7375SFelipe Balbi 							| MUSB_TXCSR_MODE);
345bb3a2ef2Ssupriya karanth 					/*
346bb3a2ef2Ssupriya karanth 					 * Enable Autoset according to table
347bb3a2ef2Ssupriya karanth 					 * below
348bb3a2ef2Ssupriya karanth 					 * bulk_split hb_mult	Autoset_Enable
349bb3a2ef2Ssupriya karanth 					 *	0	0	Yes(Normal)
350bb3a2ef2Ssupriya karanth 					 *	0	>0	No(High BW ISO)
351bb3a2ef2Ssupriya karanth 					 *	1	0	Yes(HS bulk)
352bb3a2ef2Ssupriya karanth 					 *	1	>0	Yes(FS bulk)
353bb3a2ef2Ssupriya karanth 					 */
354bb3a2ef2Ssupriya karanth 					if (!musb_ep->hb_mult ||
355bb3a2ef2Ssupriya karanth 					    can_bulk_split(musb,
3561a171626SGeyslan G. Bem 							   musb_ep->type))
357f11d893dSMing Lei 						csr |= MUSB_TXCSR_AUTOSET;
358f11d893dSMing Lei 				}
359550a7375SFelipe Balbi 				csr &= ~MUSB_TXCSR_P_UNDERRUN;
360f11d893dSMing Lei 
361550a7375SFelipe Balbi 				musb_writew(epio, MUSB_TXCSR, csr);
362550a7375SFelipe Balbi 			}
363550a7375SFelipe Balbi 		}
364550a7375SFelipe Balbi 
365f8e9f34fSTony Lindgren 		if (is_cppi_enabled(musb)) {
366550a7375SFelipe Balbi 			/* program endpoint CSR first, then setup DMA */
367b6e434a5SSergei Shtylyov 			csr &= ~(MUSB_TXCSR_P_UNDERRUN | MUSB_TXCSR_TXPKTRDY);
36837e3ee99SSergei Shtylyov 			csr |= MUSB_TXCSR_DMAENAB | MUSB_TXCSR_DMAMODE |
36937e3ee99SSergei Shtylyov 				MUSB_TXCSR_MODE;
370fc525751SSebastian Andrzej Siewior 			musb_writew(epio, MUSB_TXCSR, (MUSB_TXCSR_P_WZC_BITS &
371fc525751SSebastian Andrzej Siewior 						~MUSB_TXCSR_P_UNDERRUN) | csr);
372550a7375SFelipe Balbi 
373550a7375SFelipe Balbi 			/* ensure writebuffer is empty */
374550a7375SFelipe Balbi 			csr = musb_readw(epio, MUSB_TXCSR);
375550a7375SFelipe Balbi 
376fc525751SSebastian Andrzej Siewior 			/*
377fc525751SSebastian Andrzej Siewior 			 * NOTE host side sets DMAENAB later than this; both are
378fc525751SSebastian Andrzej Siewior 			 * OK since the transfer dma glue (between CPPI and
379fc525751SSebastian Andrzej Siewior 			 * Mentor fifos) just tells CPPI it could start. Data
380fc525751SSebastian Andrzej Siewior 			 * only moves to the USB TX fifo when both fifos are
381fc525751SSebastian Andrzej Siewior 			 * ready.
382550a7375SFelipe Balbi 			 */
383fc525751SSebastian Andrzej Siewior 			/*
384fc525751SSebastian Andrzej Siewior 			 * "mode" is irrelevant here; handle terminating ZLPs
385fc525751SSebastian Andrzej Siewior 			 * like PIO does, since the hardware RNDIS mode seems
386fc525751SSebastian Andrzej Siewior 			 * unreliable except for the
387fc525751SSebastian Andrzej Siewior 			 * last-packet-is-already-short case.
388550a7375SFelipe Balbi 			 */
389550a7375SFelipe Balbi 			use_dma = use_dma && c->channel_program(
390550a7375SFelipe Balbi 					musb_ep->dma, musb_ep->packet_sz,
391550a7375SFelipe Balbi 					0,
39266af83ddSMing Lei 					request->dma + request->actual,
39366af83ddSMing Lei 					request_size);
394550a7375SFelipe Balbi 			if (!use_dma) {
395550a7375SFelipe Balbi 				c->channel_release(musb_ep->dma);
396550a7375SFelipe Balbi 				musb_ep->dma = NULL;
397b6e434a5SSergei Shtylyov 				csr &= ~MUSB_TXCSR_DMAENAB;
398b6e434a5SSergei Shtylyov 				musb_writew(epio, MUSB_TXCSR, csr);
399550a7375SFelipe Balbi 				/* invariant: prequest->buf is non-null */
400550a7375SFelipe Balbi 			}
401f8e9f34fSTony Lindgren 		} else if (tusb_dma_omap(musb))
402550a7375SFelipe Balbi 			use_dma = use_dma && c->channel_program(
403550a7375SFelipe Balbi 					musb_ep->dma, musb_ep->packet_sz,
404550a7375SFelipe Balbi 					request->zero,
40566af83ddSMing Lei 					request->dma + request->actual,
40666af83ddSMing Lei 					request_size);
407550a7375SFelipe Balbi 	}
408550a7375SFelipe Balbi #endif
409550a7375SFelipe Balbi 
410550a7375SFelipe Balbi 	if (!use_dma) {
41192d2711fSHema Kalliguddi 		/*
41292d2711fSHema Kalliguddi 		 * Unmap the dma buffer back to cpu if dma channel
41392d2711fSHema Kalliguddi 		 * programming fails
41492d2711fSHema Kalliguddi 		 */
41592d2711fSHema Kalliguddi 		unmap_dma_buffer(req, musb);
41692d2711fSHema Kalliguddi 
417550a7375SFelipe Balbi 		musb_write_fifo(musb_ep->hw_ep, fifo_count,
418550a7375SFelipe Balbi 				(u8 *) (request->buf + request->actual));
419550a7375SFelipe Balbi 		request->actual += fifo_count;
420550a7375SFelipe Balbi 		csr |= MUSB_TXCSR_TXPKTRDY;
421550a7375SFelipe Balbi 		csr &= ~MUSB_TXCSR_P_UNDERRUN;
422550a7375SFelipe Balbi 		musb_writew(epio, MUSB_TXCSR, csr);
423550a7375SFelipe Balbi 	}
424550a7375SFelipe Balbi 
425550a7375SFelipe Balbi 	/* host may already have the data when this message shows... */
426*b99d3659SBin Liu 	musb_dbg(musb, "%s TX/IN %s len %d/%d, txcsr %04x, fifo %d/%d",
427550a7375SFelipe Balbi 			musb_ep->end_point.name, use_dma ? "dma" : "pio",
428550a7375SFelipe Balbi 			request->actual, request->length,
429550a7375SFelipe Balbi 			musb_readw(epio, MUSB_TXCSR),
430550a7375SFelipe Balbi 			fifo_count,
431550a7375SFelipe Balbi 			musb_readw(epio, MUSB_TXMAXP));
432550a7375SFelipe Balbi }
433550a7375SFelipe Balbi 
434550a7375SFelipe Balbi /*
435550a7375SFelipe Balbi  * FIFO state update (e.g. data ready).
436550a7375SFelipe Balbi  * Called from IRQ,  with controller locked.
437550a7375SFelipe Balbi  */
438550a7375SFelipe Balbi void musb_g_tx(struct musb *musb, u8 epnum)
439550a7375SFelipe Balbi {
440550a7375SFelipe Balbi 	u16			csr;
441ad1adb89SFelipe Balbi 	struct musb_request	*req;
442550a7375SFelipe Balbi 	struct usb_request	*request;
443550a7375SFelipe Balbi 	u8 __iomem		*mbase = musb->mregs;
444550a7375SFelipe Balbi 	struct musb_ep		*musb_ep = &musb->endpoints[epnum].ep_in;
445550a7375SFelipe Balbi 	void __iomem		*epio = musb->endpoints[epnum].regs;
446550a7375SFelipe Balbi 	struct dma_channel	*dma;
447550a7375SFelipe Balbi 
448550a7375SFelipe Balbi 	musb_ep_select(mbase, epnum);
449ad1adb89SFelipe Balbi 	req = next_request(musb_ep);
450ad1adb89SFelipe Balbi 	request = &req->request;
451550a7375SFelipe Balbi 
452550a7375SFelipe Balbi 	csr = musb_readw(epio, MUSB_TXCSR);
453*b99d3659SBin Liu 	musb_dbg(musb, "<== %s, txcsr %04x", musb_ep->end_point.name, csr);
454550a7375SFelipe Balbi 
455550a7375SFelipe Balbi 	dma = is_dma_capable() ? musb_ep->dma : NULL;
4567723de7eSSergei Shtylyov 
4577723de7eSSergei Shtylyov 	/*
4587723de7eSSergei Shtylyov 	 * REVISIT: for high bandwidth, MUSB_TXCSR_P_INCOMPTX
4597723de7eSSergei Shtylyov 	 * probably rates reporting as a host error.
460550a7375SFelipe Balbi 	 */
461550a7375SFelipe Balbi 	if (csr & MUSB_TXCSR_P_SENTSTALL) {
462550a7375SFelipe Balbi 		csr |=	MUSB_TXCSR_P_WZC_BITS;
463550a7375SFelipe Balbi 		csr &= ~MUSB_TXCSR_P_SENTSTALL;
464550a7375SFelipe Balbi 		musb_writew(epio, MUSB_TXCSR, csr);
4657723de7eSSergei Shtylyov 		return;
466550a7375SFelipe Balbi 	}
467550a7375SFelipe Balbi 
468550a7375SFelipe Balbi 	if (csr & MUSB_TXCSR_P_UNDERRUN) {
4697723de7eSSergei Shtylyov 		/* We NAKed, no big deal... little reason to care. */
470550a7375SFelipe Balbi 		csr |=	 MUSB_TXCSR_P_WZC_BITS;
4717723de7eSSergei Shtylyov 		csr &= ~(MUSB_TXCSR_P_UNDERRUN | MUSB_TXCSR_TXPKTRDY);
472550a7375SFelipe Balbi 		musb_writew(epio, MUSB_TXCSR, csr);
4735c8a86e1SFelipe Balbi 		dev_vdbg(musb->controller, "underrun on ep%d, req %p\n",
4745c8a86e1SFelipe Balbi 				epnum, request);
475550a7375SFelipe Balbi 	}
476550a7375SFelipe Balbi 
477550a7375SFelipe Balbi 	if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) {
4787723de7eSSergei Shtylyov 		/*
4797723de7eSSergei Shtylyov 		 * SHOULD NOT HAPPEN... has with CPPI though, after
480550a7375SFelipe Balbi 		 * changing SENDSTALL (and other cases); harmless?
481550a7375SFelipe Balbi 		 */
482*b99d3659SBin Liu 		musb_dbg(musb, "%s dma still busy?", musb_ep->end_point.name);
4837723de7eSSergei Shtylyov 		return;
484550a7375SFelipe Balbi 	}
485550a7375SFelipe Balbi 
486550a7375SFelipe Balbi 	if (request) {
487550a7375SFelipe Balbi 		u8	is_dma = 0;
488fb91cddcSTony Lindgren 		bool	short_packet = false;
489550a7375SFelipe Balbi 
490550a7375SFelipe Balbi 		if (dma && (csr & MUSB_TXCSR_DMAENAB)) {
491550a7375SFelipe Balbi 			is_dma = 1;
492550a7375SFelipe Balbi 			csr |= MUSB_TXCSR_P_WZC_BITS;
4937723de7eSSergei Shtylyov 			csr &= ~(MUSB_TXCSR_DMAENAB | MUSB_TXCSR_P_UNDERRUN |
494100d4a9dSMian Yousaf Kaukab 				 MUSB_TXCSR_TXPKTRDY | MUSB_TXCSR_AUTOSET);
495550a7375SFelipe Balbi 			musb_writew(epio, MUSB_TXCSR, csr);
4967723de7eSSergei Shtylyov 			/* Ensure writebuffer is empty. */
497550a7375SFelipe Balbi 			csr = musb_readw(epio, MUSB_TXCSR);
498550a7375SFelipe Balbi 			request->actual += musb_ep->dma->actual_len;
499*b99d3659SBin Liu 			musb_dbg(musb, "TXCSR%d %04x, DMA off, len %zu, req %p",
5007723de7eSSergei Shtylyov 				epnum, csr, musb_ep->dma->actual_len, request);
501550a7375SFelipe Balbi 		}
502550a7375SFelipe Balbi 
5037723de7eSSergei Shtylyov 		/*
5047723de7eSSergei Shtylyov 		 * First, maybe a terminating short packet. Some DMA
5057723de7eSSergei Shtylyov 		 * engines might handle this by themselves.
506550a7375SFelipe Balbi 		 */
507fb91cddcSTony Lindgren 		if ((request->zero && request->length)
508e7379aaaSMing Lei 			&& (request->length % musb_ep->packet_sz == 0)
509e7379aaaSMing Lei 			&& (request->actual == request->length))
510fb91cddcSTony Lindgren 				short_packet = true;
511fb91cddcSTony Lindgren 
512fb91cddcSTony Lindgren 		if ((musb_dma_inventra(musb) || musb_dma_ux500(musb)) &&
513fb91cddcSTony Lindgren 			(is_dma && (!dma->desired_mode ||
514550a7375SFelipe Balbi 				(request->actual &
515fb91cddcSTony Lindgren 					(musb_ep->packet_sz - 1)))))
516fb91cddcSTony Lindgren 				short_packet = true;
517fb91cddcSTony Lindgren 
518fb91cddcSTony Lindgren 		if (short_packet) {
5197723de7eSSergei Shtylyov 			/*
5207723de7eSSergei Shtylyov 			 * On DMA completion, FIFO may not be
5217723de7eSSergei Shtylyov 			 * available yet...
522550a7375SFelipe Balbi 			 */
523550a7375SFelipe Balbi 			if (csr & MUSB_TXCSR_TXPKTRDY)
5247723de7eSSergei Shtylyov 				return;
525550a7375SFelipe Balbi 
5267723de7eSSergei Shtylyov 			musb_writew(epio, MUSB_TXCSR, MUSB_TXCSR_MODE
527550a7375SFelipe Balbi 					| MUSB_TXCSR_TXPKTRDY);
528550a7375SFelipe Balbi 			request->zero = 0;
529550a7375SFelipe Balbi 		}
530550a7375SFelipe Balbi 
531bb27bc2cSMing Lei 		if (request->actual == request->length) {
532550a7375SFelipe Balbi 			musb_g_giveback(musb_ep, request, 0);
53339287076SSupriya Karanth 			/*
53439287076SSupriya Karanth 			 * In the giveback function the MUSB lock is
53539287076SSupriya Karanth 			 * released and acquired after sometime. During
53639287076SSupriya Karanth 			 * this time period the INDEX register could get
53739287076SSupriya Karanth 			 * changed by the gadget_queue function especially
53839287076SSupriya Karanth 			 * on SMP systems. Reselect the INDEX to be sure
53939287076SSupriya Karanth 			 * we are reading/modifying the right registers
54039287076SSupriya Karanth 			 */
54139287076SSupriya Karanth 			musb_ep_select(mbase, epnum);
542ad1adb89SFelipe Balbi 			req = musb_ep->desc ? next_request(musb_ep) : NULL;
543ad1adb89SFelipe Balbi 			if (!req) {
544*b99d3659SBin Liu 				musb_dbg(musb, "%s idle now",
545550a7375SFelipe Balbi 					musb_ep->end_point.name);
5467723de7eSSergei Shtylyov 				return;
54795962a77SSergei Shtylyov 			}
548550a7375SFelipe Balbi 		}
549550a7375SFelipe Balbi 
550ad1adb89SFelipe Balbi 		txstate(musb, req);
551550a7375SFelipe Balbi 	}
552550a7375SFelipe Balbi }
553550a7375SFelipe Balbi 
554550a7375SFelipe Balbi /* ------------------------------------------------------------ */
555550a7375SFelipe Balbi 
556550a7375SFelipe Balbi /*
557550a7375SFelipe Balbi  * Context: controller locked, IRQs blocked, endpoint selected
558550a7375SFelipe Balbi  */
559550a7375SFelipe Balbi static void rxstate(struct musb *musb, struct musb_request *req)
560550a7375SFelipe Balbi {
561550a7375SFelipe Balbi 	const u8		epnum = req->epnum;
562550a7375SFelipe Balbi 	struct usb_request	*request = &req->request;
563bd2e74d6SMing Lei 	struct musb_ep		*musb_ep;
564550a7375SFelipe Balbi 	void __iomem		*epio = musb->endpoints[epnum].regs;
565f0443afdSSergei Shtylyov 	unsigned		len = 0;
566f0443afdSSergei Shtylyov 	u16			fifo_count;
567cea83241SSergei Shtylyov 	u16			csr = musb_readw(epio, MUSB_RXCSR);
568bd2e74d6SMing Lei 	struct musb_hw_ep	*hw_ep = &musb->endpoints[epnum];
5690ae52d54SAnand Gadiyar 	u8			use_mode_1;
570bd2e74d6SMing Lei 
571bd2e74d6SMing Lei 	if (hw_ep->is_shared_fifo)
572bd2e74d6SMing Lei 		musb_ep = &hw_ep->ep_in;
573bd2e74d6SMing Lei 	else
574bd2e74d6SMing Lei 		musb_ep = &hw_ep->ep_out;
575bd2e74d6SMing Lei 
576f0443afdSSergei Shtylyov 	fifo_count = musb_ep->packet_sz;
577550a7375SFelipe Balbi 
578abf710e6SVikram Pandita 	/* Check if EP is disabled */
579abf710e6SVikram Pandita 	if (!musb_ep->desc) {
580*b99d3659SBin Liu 		musb_dbg(musb, "ep:%s disabled - ignore request",
581abf710e6SVikram Pandita 						musb_ep->end_point.name);
582abf710e6SVikram Pandita 		return;
583abf710e6SVikram Pandita 	}
584abf710e6SVikram Pandita 
585cea83241SSergei Shtylyov 	/* We shouldn't get here while DMA is active, but we do... */
586cea83241SSergei Shtylyov 	if (dma_channel_status(musb_ep->dma) == MUSB_DMA_STATUS_BUSY) {
587*b99d3659SBin Liu 		musb_dbg(musb, "DMA pending...");
588cea83241SSergei Shtylyov 		return;
589cea83241SSergei Shtylyov 	}
590cea83241SSergei Shtylyov 
591cea83241SSergei Shtylyov 	if (csr & MUSB_RXCSR_P_SENDSTALL) {
592*b99d3659SBin Liu 		musb_dbg(musb, "%s stalling, RXCSR %04x",
593cea83241SSergei Shtylyov 		    musb_ep->end_point.name, csr);
594cea83241SSergei Shtylyov 		return;
595cea83241SSergei Shtylyov 	}
596550a7375SFelipe Balbi 
597f8e9f34fSTony Lindgren 	if (is_cppi_enabled(musb) && is_buffer_mapped(req)) {
598550a7375SFelipe Balbi 		struct dma_controller	*c = musb->dma_controller;
599550a7375SFelipe Balbi 		struct dma_channel	*channel = musb_ep->dma;
600550a7375SFelipe Balbi 
601550a7375SFelipe Balbi 		/* NOTE:  CPPI won't actually stop advancing the DMA
602550a7375SFelipe Balbi 		 * queue after short packet transfers, so this is almost
603550a7375SFelipe Balbi 		 * always going to run as IRQ-per-packet DMA so that
604550a7375SFelipe Balbi 		 * faults will be handled correctly.
605550a7375SFelipe Balbi 		 */
606550a7375SFelipe Balbi 		if (c->channel_program(channel,
607550a7375SFelipe Balbi 				musb_ep->packet_sz,
608550a7375SFelipe Balbi 				!request->short_not_ok,
609550a7375SFelipe Balbi 				request->dma + request->actual,
610550a7375SFelipe Balbi 				request->length - request->actual)) {
611550a7375SFelipe Balbi 
612550a7375SFelipe Balbi 			/* make sure that if an rxpkt arrived after the irq,
613550a7375SFelipe Balbi 			 * the cppi engine will be ready to take it as soon
614550a7375SFelipe Balbi 			 * as DMA is enabled
615550a7375SFelipe Balbi 			 */
616550a7375SFelipe Balbi 			csr &= ~(MUSB_RXCSR_AUTOCLEAR
617550a7375SFelipe Balbi 					| MUSB_RXCSR_DMAMODE);
618550a7375SFelipe Balbi 			csr |= MUSB_RXCSR_DMAENAB | MUSB_RXCSR_P_WZC_BITS;
619550a7375SFelipe Balbi 			musb_writew(epio, MUSB_RXCSR, csr);
620550a7375SFelipe Balbi 			return;
621550a7375SFelipe Balbi 		}
622550a7375SFelipe Balbi 	}
623550a7375SFelipe Balbi 
624550a7375SFelipe Balbi 	if (csr & MUSB_RXCSR_RXPKTRDY) {
625f0443afdSSergei Shtylyov 		fifo_count = musb_readw(epio, MUSB_RXCOUNT);
6260ae52d54SAnand Gadiyar 
6270ae52d54SAnand Gadiyar 		/*
62800a89180SFelipe Balbi 		 * Enable Mode 1 on RX transfers only when short_not_ok flag
62900a89180SFelipe Balbi 		 * is set. Currently short_not_ok flag is set only from
63000a89180SFelipe Balbi 		 * file_storage and f_mass_storage drivers
6310ae52d54SAnand Gadiyar 		 */
63200a89180SFelipe Balbi 
63300a89180SFelipe Balbi 		if (request->short_not_ok && fifo_count == musb_ep->packet_sz)
6340ae52d54SAnand Gadiyar 			use_mode_1 = 1;
6350ae52d54SAnand Gadiyar 		else
6360ae52d54SAnand Gadiyar 			use_mode_1 = 0;
6370ae52d54SAnand Gadiyar 
638550a7375SFelipe Balbi 		if (request->actual < request->length) {
63903840fadSFelipe Balbi 			if (!is_buffer_mapped(req))
64003840fadSFelipe Balbi 				goto buffer_aint_mapped;
64103840fadSFelipe Balbi 
64203840fadSFelipe Balbi 			if (musb_dma_inventra(musb)) {
643550a7375SFelipe Balbi 				struct dma_controller	*c;
644550a7375SFelipe Balbi 				struct dma_channel	*channel;
645550a7375SFelipe Balbi 				int			use_dma = 0;
64637730eccSFelipe Balbi 				unsigned int transfer_size;
647550a7375SFelipe Balbi 
648550a7375SFelipe Balbi 				c = musb->dma_controller;
649550a7375SFelipe Balbi 				channel = musb_ep->dma;
650550a7375SFelipe Balbi 
65100a89180SFelipe Balbi 	/* We use DMA Req mode 0 in rx_csr, and DMA controller operates in
65200a89180SFelipe Balbi 	 * mode 0 only. So we do not get endpoint interrupts due to DMA
65300a89180SFelipe Balbi 	 * completion. We only get interrupts from DMA controller.
65400a89180SFelipe Balbi 	 *
65500a89180SFelipe Balbi 	 * We could operate in DMA mode 1 if we knew the size of the tranfer
65600a89180SFelipe Balbi 	 * in advance. For mass storage class, request->length = what the host
65700a89180SFelipe Balbi 	 * sends, so that'd work.  But for pretty much everything else,
65800a89180SFelipe Balbi 	 * request->length is routinely more than what the host sends. For
65900a89180SFelipe Balbi 	 * most these gadgets, end of is signified either by a short packet,
66000a89180SFelipe Balbi 	 * or filling the last byte of the buffer.  (Sending extra data in
66100a89180SFelipe Balbi 	 * that last pckate should trigger an overflow fault.)  But in mode 1,
66200a89180SFelipe Balbi 	 * we don't get DMA completion interrupt for short packets.
66300a89180SFelipe Balbi 	 *
66400a89180SFelipe Balbi 	 * Theoretically, we could enable DMAReq irq (MUSB_RXCSR_DMAMODE = 1),
66500a89180SFelipe Balbi 	 * to get endpoint interrupt on every DMA req, but that didn't seem
66600a89180SFelipe Balbi 	 * to work reliably.
66700a89180SFelipe Balbi 	 *
66800a89180SFelipe Balbi 	 * REVISIT an updated g_file_storage can set req->short_not_ok, which
66900a89180SFelipe Balbi 	 * then becomes usable as a runtime "use mode 1" hint...
67000a89180SFelipe Balbi 	 */
67100a89180SFelipe Balbi 
6720ae52d54SAnand Gadiyar 				/* Experimental: Mode1 works with mass storage use cases */
6730ae52d54SAnand Gadiyar 				if (use_mode_1) {
6749001d80dSMing Lei 					csr |= MUSB_RXCSR_AUTOCLEAR;
6750ae52d54SAnand Gadiyar 					musb_writew(epio, MUSB_RXCSR, csr);
6760ae52d54SAnand Gadiyar 					csr |= MUSB_RXCSR_DMAENAB;
6770ae52d54SAnand Gadiyar 					musb_writew(epio, MUSB_RXCSR, csr);
678550a7375SFelipe Balbi 
6790ae52d54SAnand Gadiyar 					/*
6800ae52d54SAnand Gadiyar 					 * this special sequence (enabling and then
681550a7375SFelipe Balbi 					 * disabling MUSB_RXCSR_DMAMODE) is required
682550a7375SFelipe Balbi 					 * to get DMAReq to activate
683550a7375SFelipe Balbi 					 */
684550a7375SFelipe Balbi 					musb_writew(epio, MUSB_RXCSR,
685550a7375SFelipe Balbi 						csr | MUSB_RXCSR_DMAMODE);
6860ae52d54SAnand Gadiyar 					musb_writew(epio, MUSB_RXCSR, csr);
6870ae52d54SAnand Gadiyar 
68837730eccSFelipe Balbi 					transfer_size = min_t(unsigned int,
68937730eccSFelipe Balbi 							request->length -
69037730eccSFelipe Balbi 							request->actual,
691660fa886SRoger Quadros 							channel->max_len);
692660fa886SRoger Quadros 					musb_ep->dma->desired_mode = 1;
6930ae52d54SAnand Gadiyar 				} else {
6949001d80dSMing Lei 					if (!musb_ep->hb_mult &&
6959001d80dSMing Lei 						musb_ep->hw_ep->rx_double_buffered)
6969001d80dSMing Lei 						csr |= MUSB_RXCSR_AUTOCLEAR;
6970ae52d54SAnand Gadiyar 					csr |= MUSB_RXCSR_DMAENAB;
698550a7375SFelipe Balbi 					musb_writew(epio, MUSB_RXCSR, csr);
699550a7375SFelipe Balbi 
7001018b4e4SMing Lei 					transfer_size = min(request->length - request->actual,
701f0443afdSSergei Shtylyov 							(unsigned)fifo_count);
702550a7375SFelipe Balbi 					musb_ep->dma->desired_mode = 0;
7030ae52d54SAnand Gadiyar 				}
704550a7375SFelipe Balbi 
705550a7375SFelipe Balbi 				use_dma = c->channel_program(
706550a7375SFelipe Balbi 						channel,
707550a7375SFelipe Balbi 						musb_ep->packet_sz,
708550a7375SFelipe Balbi 						channel->desired_mode,
709550a7375SFelipe Balbi 						request->dma
710550a7375SFelipe Balbi 						+ request->actual,
711550a7375SFelipe Balbi 						transfer_size);
712550a7375SFelipe Balbi 
713550a7375SFelipe Balbi 				if (use_dma)
714550a7375SFelipe Balbi 					return;
715550a7375SFelipe Balbi 			}
71603840fadSFelipe Balbi 
71703840fadSFelipe Balbi 			if ((musb_dma_ux500(musb)) &&
718a48ff906SMian Yousaf Kaukab 				(request->actual < request->length)) {
719a48ff906SMian Yousaf Kaukab 
720a48ff906SMian Yousaf Kaukab 				struct dma_controller *c;
721a48ff906SMian Yousaf Kaukab 				struct dma_channel *channel;
72237730eccSFelipe Balbi 				unsigned int transfer_size = 0;
723a48ff906SMian Yousaf Kaukab 
724a48ff906SMian Yousaf Kaukab 				c = musb->dma_controller;
725a48ff906SMian Yousaf Kaukab 				channel = musb_ep->dma;
726a48ff906SMian Yousaf Kaukab 
727a48ff906SMian Yousaf Kaukab 				/* In case first packet is short */
728f0443afdSSergei Shtylyov 				if (fifo_count < musb_ep->packet_sz)
729f0443afdSSergei Shtylyov 					transfer_size = fifo_count;
730a48ff906SMian Yousaf Kaukab 				else if (request->short_not_ok)
73137730eccSFelipe Balbi 					transfer_size =	min_t(unsigned int,
73237730eccSFelipe Balbi 							request->length -
733a48ff906SMian Yousaf Kaukab 							request->actual,
734a48ff906SMian Yousaf Kaukab 							channel->max_len);
735a48ff906SMian Yousaf Kaukab 				else
73637730eccSFelipe Balbi 					transfer_size = min_t(unsigned int,
73737730eccSFelipe Balbi 							request->length -
738a48ff906SMian Yousaf Kaukab 							request->actual,
739f0443afdSSergei Shtylyov 							(unsigned)fifo_count);
740a48ff906SMian Yousaf Kaukab 
741a48ff906SMian Yousaf Kaukab 				csr &= ~MUSB_RXCSR_DMAMODE;
742a48ff906SMian Yousaf Kaukab 				csr |= (MUSB_RXCSR_DMAENAB |
743a48ff906SMian Yousaf Kaukab 					MUSB_RXCSR_AUTOCLEAR);
744a48ff906SMian Yousaf Kaukab 
745a48ff906SMian Yousaf Kaukab 				musb_writew(epio, MUSB_RXCSR, csr);
746a48ff906SMian Yousaf Kaukab 
747a48ff906SMian Yousaf Kaukab 				if (transfer_size <= musb_ep->packet_sz) {
748a48ff906SMian Yousaf Kaukab 					musb_ep->dma->desired_mode = 0;
749a48ff906SMian Yousaf Kaukab 				} else {
750a48ff906SMian Yousaf Kaukab 					musb_ep->dma->desired_mode = 1;
751a48ff906SMian Yousaf Kaukab 					/* Mode must be set after DMAENAB */
752a48ff906SMian Yousaf Kaukab 					csr |= MUSB_RXCSR_DMAMODE;
753a48ff906SMian Yousaf Kaukab 					musb_writew(epio, MUSB_RXCSR, csr);
754a48ff906SMian Yousaf Kaukab 				}
755a48ff906SMian Yousaf Kaukab 
756a48ff906SMian Yousaf Kaukab 				if (c->channel_program(channel,
757a48ff906SMian Yousaf Kaukab 							musb_ep->packet_sz,
758a48ff906SMian Yousaf Kaukab 							channel->desired_mode,
759a48ff906SMian Yousaf Kaukab 							request->dma
760a48ff906SMian Yousaf Kaukab 							+ request->actual,
761a48ff906SMian Yousaf Kaukab 							transfer_size))
762a48ff906SMian Yousaf Kaukab 
763a48ff906SMian Yousaf Kaukab 					return;
764a48ff906SMian Yousaf Kaukab 			}
765550a7375SFelipe Balbi 
766f0443afdSSergei Shtylyov 			len = request->length - request->actual;
767*b99d3659SBin Liu 			musb_dbg(musb, "%s OUT/RX pio fifo %d/%d, maxpacket %d",
768550a7375SFelipe Balbi 					musb_ep->end_point.name,
769f0443afdSSergei Shtylyov 					fifo_count, len,
770550a7375SFelipe Balbi 					musb_ep->packet_sz);
771550a7375SFelipe Balbi 
772c2c96321SFelipe Balbi 			fifo_count = min_t(unsigned, len, fifo_count);
773550a7375SFelipe Balbi 
77403840fadSFelipe Balbi 			if (tusb_dma_omap(musb)) {
775550a7375SFelipe Balbi 				struct dma_controller *c = musb->dma_controller;
776550a7375SFelipe Balbi 				struct dma_channel *channel = musb_ep->dma;
777550a7375SFelipe Balbi 				u32 dma_addr = request->dma + request->actual;
778550a7375SFelipe Balbi 				int ret;
779550a7375SFelipe Balbi 
780550a7375SFelipe Balbi 				ret = c->channel_program(channel,
781550a7375SFelipe Balbi 						musb_ep->packet_sz,
782550a7375SFelipe Balbi 						channel->desired_mode,
783550a7375SFelipe Balbi 						dma_addr,
784550a7375SFelipe Balbi 						fifo_count);
785550a7375SFelipe Balbi 				if (ret)
786550a7375SFelipe Balbi 					return;
787550a7375SFelipe Balbi 			}
78803840fadSFelipe Balbi 
78992d2711fSHema Kalliguddi 			/*
79092d2711fSHema Kalliguddi 			 * Unmap the dma buffer back to cpu if dma channel
79192d2711fSHema Kalliguddi 			 * programming fails. This buffer is mapped if the
79292d2711fSHema Kalliguddi 			 * channel allocation is successful
79392d2711fSHema Kalliguddi 			 */
79492d2711fSHema Kalliguddi 			unmap_dma_buffer(req, musb);
79592d2711fSHema Kalliguddi 
796e75df371SMing Lei 			/*
797e75df371SMing Lei 			 * Clear DMAENAB and AUTOCLEAR for the
79892d2711fSHema Kalliguddi 			 * PIO mode transfer
79992d2711fSHema Kalliguddi 			 */
800e75df371SMing Lei 			csr &= ~(MUSB_RXCSR_DMAENAB | MUSB_RXCSR_AUTOCLEAR);
80192d2711fSHema Kalliguddi 			musb_writew(epio, MUSB_RXCSR, csr);
802550a7375SFelipe Balbi 
80303840fadSFelipe Balbi buffer_aint_mapped:
804550a7375SFelipe Balbi 			musb_read_fifo(musb_ep->hw_ep, fifo_count, (u8 *)
805550a7375SFelipe Balbi 					(request->buf + request->actual));
806550a7375SFelipe Balbi 			request->actual += fifo_count;
807550a7375SFelipe Balbi 
808550a7375SFelipe Balbi 			/* REVISIT if we left anything in the fifo, flush
809550a7375SFelipe Balbi 			 * it and report -EOVERFLOW
810550a7375SFelipe Balbi 			 */
811550a7375SFelipe Balbi 
812550a7375SFelipe Balbi 			/* ack the read! */
813550a7375SFelipe Balbi 			csr |= MUSB_RXCSR_P_WZC_BITS;
814550a7375SFelipe Balbi 			csr &= ~MUSB_RXCSR_RXPKTRDY;
815550a7375SFelipe Balbi 			musb_writew(epio, MUSB_RXCSR, csr);
816550a7375SFelipe Balbi 		}
817550a7375SFelipe Balbi 	}
818550a7375SFelipe Balbi 
819550a7375SFelipe Balbi 	/* reach the end or short packet detected */
820f0443afdSSergei Shtylyov 	if (request->actual == request->length ||
821f0443afdSSergei Shtylyov 	    fifo_count < musb_ep->packet_sz)
822550a7375SFelipe Balbi 		musb_g_giveback(musb_ep, request, 0);
823550a7375SFelipe Balbi }
824550a7375SFelipe Balbi 
825550a7375SFelipe Balbi /*
826550a7375SFelipe Balbi  * Data ready for a request; called from IRQ
827550a7375SFelipe Balbi  */
828550a7375SFelipe Balbi void musb_g_rx(struct musb *musb, u8 epnum)
829550a7375SFelipe Balbi {
830550a7375SFelipe Balbi 	u16			csr;
831ad1adb89SFelipe Balbi 	struct musb_request	*req;
832550a7375SFelipe Balbi 	struct usb_request	*request;
833550a7375SFelipe Balbi 	void __iomem		*mbase = musb->mregs;
834bd2e74d6SMing Lei 	struct musb_ep		*musb_ep;
835550a7375SFelipe Balbi 	void __iomem		*epio = musb->endpoints[epnum].regs;
836550a7375SFelipe Balbi 	struct dma_channel	*dma;
837bd2e74d6SMing Lei 	struct musb_hw_ep	*hw_ep = &musb->endpoints[epnum];
838bd2e74d6SMing Lei 
839bd2e74d6SMing Lei 	if (hw_ep->is_shared_fifo)
840bd2e74d6SMing Lei 		musb_ep = &hw_ep->ep_in;
841bd2e74d6SMing Lei 	else
842bd2e74d6SMing Lei 		musb_ep = &hw_ep->ep_out;
843550a7375SFelipe Balbi 
844550a7375SFelipe Balbi 	musb_ep_select(mbase, epnum);
845550a7375SFelipe Balbi 
846ad1adb89SFelipe Balbi 	req = next_request(musb_ep);
847ad1adb89SFelipe Balbi 	if (!req)
8480abdc36fSMaulik Mankad 		return;
849550a7375SFelipe Balbi 
850ad1adb89SFelipe Balbi 	request = &req->request;
851ad1adb89SFelipe Balbi 
852550a7375SFelipe Balbi 	csr = musb_readw(epio, MUSB_RXCSR);
853550a7375SFelipe Balbi 	dma = is_dma_capable() ? musb_ep->dma : NULL;
854550a7375SFelipe Balbi 
855*b99d3659SBin Liu 	musb_dbg(musb, "<== %s, rxcsr %04x%s %p", musb_ep->end_point.name,
856550a7375SFelipe Balbi 			csr, dma ? " (dma)" : "", request);
857550a7375SFelipe Balbi 
858550a7375SFelipe Balbi 	if (csr & MUSB_RXCSR_P_SENTSTALL) {
859550a7375SFelipe Balbi 		csr |= MUSB_RXCSR_P_WZC_BITS;
860550a7375SFelipe Balbi 		csr &= ~MUSB_RXCSR_P_SENTSTALL;
861550a7375SFelipe Balbi 		musb_writew(epio, MUSB_RXCSR, csr);
862cea83241SSergei Shtylyov 		return;
863550a7375SFelipe Balbi 	}
864550a7375SFelipe Balbi 
865550a7375SFelipe Balbi 	if (csr & MUSB_RXCSR_P_OVERRUN) {
866550a7375SFelipe Balbi 		/* csr |= MUSB_RXCSR_P_WZC_BITS; */
867550a7375SFelipe Balbi 		csr &= ~MUSB_RXCSR_P_OVERRUN;
868550a7375SFelipe Balbi 		musb_writew(epio, MUSB_RXCSR, csr);
869550a7375SFelipe Balbi 
870*b99d3659SBin Liu 		musb_dbg(musb, "%s iso overrun on %p", musb_ep->name, request);
87143467868SSergei Shtylyov 		if (request->status == -EINPROGRESS)
872550a7375SFelipe Balbi 			request->status = -EOVERFLOW;
873550a7375SFelipe Balbi 	}
874550a7375SFelipe Balbi 	if (csr & MUSB_RXCSR_INCOMPRX) {
875550a7375SFelipe Balbi 		/* REVISIT not necessarily an error */
876*b99d3659SBin Liu 		musb_dbg(musb, "%s, incomprx", musb_ep->end_point.name);
877550a7375SFelipe Balbi 	}
878550a7375SFelipe Balbi 
879550a7375SFelipe Balbi 	if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) {
880550a7375SFelipe Balbi 		/* "should not happen"; likely RXPKTRDY pending for DMA */
881*b99d3659SBin Liu 		musb_dbg(musb, "%s busy, csr %04x",
882550a7375SFelipe Balbi 			musb_ep->end_point.name, csr);
883cea83241SSergei Shtylyov 		return;
884550a7375SFelipe Balbi 	}
885550a7375SFelipe Balbi 
886550a7375SFelipe Balbi 	if (dma && (csr & MUSB_RXCSR_DMAENAB)) {
887550a7375SFelipe Balbi 		csr &= ~(MUSB_RXCSR_AUTOCLEAR
888550a7375SFelipe Balbi 				| MUSB_RXCSR_DMAENAB
889550a7375SFelipe Balbi 				| MUSB_RXCSR_DMAMODE);
890550a7375SFelipe Balbi 		musb_writew(epio, MUSB_RXCSR,
891550a7375SFelipe Balbi 			MUSB_RXCSR_P_WZC_BITS | csr);
892550a7375SFelipe Balbi 
893550a7375SFelipe Balbi 		request->actual += musb_ep->dma->actual_len;
894550a7375SFelipe Balbi 
895*b99d3659SBin Liu 		musb_dbg(musb, "RXCSR%d %04x, dma off, %04x, len %zu, req %p",
896550a7375SFelipe Balbi 			epnum, csr,
897550a7375SFelipe Balbi 			musb_readw(epio, MUSB_RXCSR),
898550a7375SFelipe Balbi 			musb_ep->dma->actual_len, request);
899550a7375SFelipe Balbi 
900a48ff906SMian Yousaf Kaukab #if defined(CONFIG_USB_INVENTRA_DMA) || defined(CONFIG_USB_TUSB_OMAP_DMA) || \
901a48ff906SMian Yousaf Kaukab 	defined(CONFIG_USB_UX500_DMA)
902550a7375SFelipe Balbi 		/* Autoclear doesn't clear RxPktRdy for short packets */
9039001d80dSMing Lei 		if ((dma->desired_mode == 0 && !hw_ep->rx_double_buffered)
904550a7375SFelipe Balbi 				|| (dma->actual_len
905550a7375SFelipe Balbi 					& (musb_ep->packet_sz - 1))) {
906550a7375SFelipe Balbi 			/* ack the read! */
907550a7375SFelipe Balbi 			csr &= ~MUSB_RXCSR_RXPKTRDY;
908550a7375SFelipe Balbi 			musb_writew(epio, MUSB_RXCSR, csr);
909550a7375SFelipe Balbi 		}
910550a7375SFelipe Balbi 
911550a7375SFelipe Balbi 		/* incomplete, and not short? wait for next IN packet */
912550a7375SFelipe Balbi 		if ((request->actual < request->length)
913550a7375SFelipe Balbi 				&& (musb_ep->dma->actual_len
9149001d80dSMing Lei 					== musb_ep->packet_sz)) {
9159001d80dSMing Lei 			/* In double buffer case, continue to unload fifo if
9169001d80dSMing Lei  			 * there is Rx packet in FIFO.
9179001d80dSMing Lei  			 **/
9189001d80dSMing Lei 			csr = musb_readw(epio, MUSB_RXCSR);
9199001d80dSMing Lei 			if ((csr & MUSB_RXCSR_RXPKTRDY) &&
9209001d80dSMing Lei 				hw_ep->rx_double_buffered)
9219001d80dSMing Lei 				goto exit;
922cea83241SSergei Shtylyov 			return;
9239001d80dSMing Lei 		}
924550a7375SFelipe Balbi #endif
925550a7375SFelipe Balbi 		musb_g_giveback(musb_ep, request, 0);
92639287076SSupriya Karanth 		/*
92739287076SSupriya Karanth 		 * In the giveback function the MUSB lock is
92839287076SSupriya Karanth 		 * released and acquired after sometime. During
92939287076SSupriya Karanth 		 * this time period the INDEX register could get
93039287076SSupriya Karanth 		 * changed by the gadget_queue function especially
93139287076SSupriya Karanth 		 * on SMP systems. Reselect the INDEX to be sure
93239287076SSupriya Karanth 		 * we are reading/modifying the right registers
93339287076SSupriya Karanth 		 */
93439287076SSupriya Karanth 		musb_ep_select(mbase, epnum);
935550a7375SFelipe Balbi 
936ad1adb89SFelipe Balbi 		req = next_request(musb_ep);
937ad1adb89SFelipe Balbi 		if (!req)
938cea83241SSergei Shtylyov 			return;
939550a7375SFelipe Balbi 	}
940a48ff906SMian Yousaf Kaukab #if defined(CONFIG_USB_INVENTRA_DMA) || defined(CONFIG_USB_TUSB_OMAP_DMA) || \
941a48ff906SMian Yousaf Kaukab 	defined(CONFIG_USB_UX500_DMA)
9429001d80dSMing Lei exit:
943bb324b08SAjay Kumar Gupta #endif
94443467868SSergei Shtylyov 	/* Analyze request */
945ad1adb89SFelipe Balbi 	rxstate(musb, req);
946550a7375SFelipe Balbi }
947550a7375SFelipe Balbi 
948550a7375SFelipe Balbi /* ------------------------------------------------------------ */
949550a7375SFelipe Balbi 
950550a7375SFelipe Balbi static int musb_gadget_enable(struct usb_ep *ep,
951550a7375SFelipe Balbi 			const struct usb_endpoint_descriptor *desc)
952550a7375SFelipe Balbi {
953550a7375SFelipe Balbi 	unsigned long		flags;
954550a7375SFelipe Balbi 	struct musb_ep		*musb_ep;
955550a7375SFelipe Balbi 	struct musb_hw_ep	*hw_ep;
956550a7375SFelipe Balbi 	void __iomem		*regs;
957550a7375SFelipe Balbi 	struct musb		*musb;
958550a7375SFelipe Balbi 	void __iomem	*mbase;
959550a7375SFelipe Balbi 	u8		epnum;
960550a7375SFelipe Balbi 	u16		csr;
961550a7375SFelipe Balbi 	unsigned	tmp;
962550a7375SFelipe Balbi 	int		status = -EINVAL;
963550a7375SFelipe Balbi 
964550a7375SFelipe Balbi 	if (!ep || !desc)
965550a7375SFelipe Balbi 		return -EINVAL;
966550a7375SFelipe Balbi 
967550a7375SFelipe Balbi 	musb_ep = to_musb_ep(ep);
968550a7375SFelipe Balbi 	hw_ep = musb_ep->hw_ep;
969550a7375SFelipe Balbi 	regs = hw_ep->regs;
970550a7375SFelipe Balbi 	musb = musb_ep->musb;
971550a7375SFelipe Balbi 	mbase = musb->mregs;
972550a7375SFelipe Balbi 	epnum = musb_ep->current_epnum;
973550a7375SFelipe Balbi 
974550a7375SFelipe Balbi 	spin_lock_irqsave(&musb->lock, flags);
975550a7375SFelipe Balbi 
976550a7375SFelipe Balbi 	if (musb_ep->desc) {
977550a7375SFelipe Balbi 		status = -EBUSY;
978550a7375SFelipe Balbi 		goto fail;
979550a7375SFelipe Balbi 	}
98096bcd090SJulia Lawall 	musb_ep->type = usb_endpoint_type(desc);
981550a7375SFelipe Balbi 
982550a7375SFelipe Balbi 	/* check direction and (later) maxpacket size against endpoint */
98396bcd090SJulia Lawall 	if (usb_endpoint_num(desc) != epnum)
984550a7375SFelipe Balbi 		goto fail;
985550a7375SFelipe Balbi 
986550a7375SFelipe Balbi 	/* REVISIT this rules out high bandwidth periodic transfers */
98729cc8897SKuninori Morimoto 	tmp = usb_endpoint_maxp(desc);
988f11d893dSMing Lei 	if (tmp & ~0x07ff) {
989f11d893dSMing Lei 		int ok;
990f11d893dSMing Lei 
991f11d893dSMing Lei 		if (usb_endpoint_dir_in(desc))
992f11d893dSMing Lei 			ok = musb->hb_iso_tx;
993f11d893dSMing Lei 		else
994f11d893dSMing Lei 			ok = musb->hb_iso_rx;
995f11d893dSMing Lei 
996f11d893dSMing Lei 		if (!ok) {
997*b99d3659SBin Liu 			musb_dbg(musb, "no support for high bandwidth ISO");
998550a7375SFelipe Balbi 			goto fail;
999f11d893dSMing Lei 		}
1000f11d893dSMing Lei 		musb_ep->hb_mult = (tmp >> 11) & 3;
1001f11d893dSMing Lei 	} else {
1002f11d893dSMing Lei 		musb_ep->hb_mult = 0;
1003f11d893dSMing Lei 	}
1004f11d893dSMing Lei 
1005f11d893dSMing Lei 	musb_ep->packet_sz = tmp & 0x7ff;
1006f11d893dSMing Lei 	tmp = musb_ep->packet_sz * (musb_ep->hb_mult + 1);
1007550a7375SFelipe Balbi 
1008550a7375SFelipe Balbi 	/* enable the interrupts for the endpoint, set the endpoint
1009550a7375SFelipe Balbi 	 * packet size (or fail), set the mode, clear the fifo
1010550a7375SFelipe Balbi 	 */
1011550a7375SFelipe Balbi 	musb_ep_select(mbase, epnum);
101296bcd090SJulia Lawall 	if (usb_endpoint_dir_in(desc)) {
1013550a7375SFelipe Balbi 
1014550a7375SFelipe Balbi 		if (hw_ep->is_shared_fifo)
1015550a7375SFelipe Balbi 			musb_ep->is_in = 1;
1016550a7375SFelipe Balbi 		if (!musb_ep->is_in)
1017550a7375SFelipe Balbi 			goto fail;
1018f11d893dSMing Lei 
1019f11d893dSMing Lei 		if (tmp > hw_ep->max_packet_sz_tx) {
1020*b99d3659SBin Liu 			musb_dbg(musb, "packet size beyond hardware FIFO size");
1021550a7375SFelipe Balbi 			goto fail;
1022f11d893dSMing Lei 		}
1023550a7375SFelipe Balbi 
1024b18d26f6SSebastian Andrzej Siewior 		musb->intrtxe |= (1 << epnum);
1025b18d26f6SSebastian Andrzej Siewior 		musb_writew(mbase, MUSB_INTRTXE, musb->intrtxe);
1026550a7375SFelipe Balbi 
1027550a7375SFelipe Balbi 		/* REVISIT if can_bulk_split(), use by updating "tmp";
1028550a7375SFelipe Balbi 		 * likewise high bandwidth periodic tx
1029550a7375SFelipe Balbi 		 */
10309f445cb2SCliff Cai 		/* Set TXMAXP with the FIFO size of the endpoint
103131c9909bSMing Lei 		 * to disable double buffering mode.
10329f445cb2SCliff Cai 		 */
1033bb3a2ef2Ssupriya karanth 		if (musb->double_buffer_not_ok) {
103406624818SFelipe Balbi 			musb_writew(regs, MUSB_TXMAXP, hw_ep->max_packet_sz_tx);
1035bb3a2ef2Ssupriya karanth 		} else {
1036bb3a2ef2Ssupriya karanth 			if (can_bulk_split(musb, musb_ep->type))
1037bb3a2ef2Ssupriya karanth 				musb_ep->hb_mult = (hw_ep->max_packet_sz_tx /
1038bb3a2ef2Ssupriya karanth 							musb_ep->packet_sz) - 1;
103906624818SFelipe Balbi 			musb_writew(regs, MUSB_TXMAXP, musb_ep->packet_sz
104006624818SFelipe Balbi 					| (musb_ep->hb_mult << 11));
1041bb3a2ef2Ssupriya karanth 		}
1042550a7375SFelipe Balbi 
1043550a7375SFelipe Balbi 		csr = MUSB_TXCSR_MODE | MUSB_TXCSR_CLRDATATOG;
1044550a7375SFelipe Balbi 		if (musb_readw(regs, MUSB_TXCSR)
1045550a7375SFelipe Balbi 				& MUSB_TXCSR_FIFONOTEMPTY)
1046550a7375SFelipe Balbi 			csr |= MUSB_TXCSR_FLUSHFIFO;
1047550a7375SFelipe Balbi 		if (musb_ep->type == USB_ENDPOINT_XFER_ISOC)
1048550a7375SFelipe Balbi 			csr |= MUSB_TXCSR_P_ISO;
1049550a7375SFelipe Balbi 
1050550a7375SFelipe Balbi 		/* set twice in case of double buffering */
1051550a7375SFelipe Balbi 		musb_writew(regs, MUSB_TXCSR, csr);
1052550a7375SFelipe Balbi 		/* REVISIT may be inappropriate w/o FIFONOTEMPTY ... */
1053550a7375SFelipe Balbi 		musb_writew(regs, MUSB_TXCSR, csr);
1054550a7375SFelipe Balbi 
1055550a7375SFelipe Balbi 	} else {
1056550a7375SFelipe Balbi 
1057550a7375SFelipe Balbi 		if (hw_ep->is_shared_fifo)
1058550a7375SFelipe Balbi 			musb_ep->is_in = 0;
1059550a7375SFelipe Balbi 		if (musb_ep->is_in)
1060550a7375SFelipe Balbi 			goto fail;
1061f11d893dSMing Lei 
1062f11d893dSMing Lei 		if (tmp > hw_ep->max_packet_sz_rx) {
1063*b99d3659SBin Liu 			musb_dbg(musb, "packet size beyond hardware FIFO size");
1064550a7375SFelipe Balbi 			goto fail;
1065f11d893dSMing Lei 		}
1066550a7375SFelipe Balbi 
1067af5ec14dSSebastian Andrzej Siewior 		musb->intrrxe |= (1 << epnum);
1068af5ec14dSSebastian Andrzej Siewior 		musb_writew(mbase, MUSB_INTRRXE, musb->intrrxe);
1069550a7375SFelipe Balbi 
1070550a7375SFelipe Balbi 		/* REVISIT if can_bulk_combine() use by updating "tmp"
1071550a7375SFelipe Balbi 		 * likewise high bandwidth periodic rx
1072550a7375SFelipe Balbi 		 */
10739f445cb2SCliff Cai 		/* Set RXMAXP with the FIFO size of the endpoint
10749f445cb2SCliff Cai 		 * to disable double buffering mode.
10759f445cb2SCliff Cai 		 */
107606624818SFelipe Balbi 		if (musb->double_buffer_not_ok)
107706624818SFelipe Balbi 			musb_writew(regs, MUSB_RXMAXP, hw_ep->max_packet_sz_tx);
107806624818SFelipe Balbi 		else
107906624818SFelipe Balbi 			musb_writew(regs, MUSB_RXMAXP, musb_ep->packet_sz
108006624818SFelipe Balbi 					| (musb_ep->hb_mult << 11));
1081550a7375SFelipe Balbi 
1082550a7375SFelipe Balbi 		/* force shared fifo to OUT-only mode */
1083550a7375SFelipe Balbi 		if (hw_ep->is_shared_fifo) {
1084550a7375SFelipe Balbi 			csr = musb_readw(regs, MUSB_TXCSR);
1085550a7375SFelipe Balbi 			csr &= ~(MUSB_TXCSR_MODE | MUSB_TXCSR_TXPKTRDY);
1086550a7375SFelipe Balbi 			musb_writew(regs, MUSB_TXCSR, csr);
1087550a7375SFelipe Balbi 		}
1088550a7375SFelipe Balbi 
1089550a7375SFelipe Balbi 		csr = MUSB_RXCSR_FLUSHFIFO | MUSB_RXCSR_CLRDATATOG;
1090550a7375SFelipe Balbi 		if (musb_ep->type == USB_ENDPOINT_XFER_ISOC)
1091550a7375SFelipe Balbi 			csr |= MUSB_RXCSR_P_ISO;
1092550a7375SFelipe Balbi 		else if (musb_ep->type == USB_ENDPOINT_XFER_INT)
1093550a7375SFelipe Balbi 			csr |= MUSB_RXCSR_DISNYET;
1094550a7375SFelipe Balbi 
1095550a7375SFelipe Balbi 		/* set twice in case of double buffering */
1096550a7375SFelipe Balbi 		musb_writew(regs, MUSB_RXCSR, csr);
1097550a7375SFelipe Balbi 		musb_writew(regs, MUSB_RXCSR, csr);
1098550a7375SFelipe Balbi 	}
1099550a7375SFelipe Balbi 
1100550a7375SFelipe Balbi 	/* NOTE:  all the I/O code _should_ work fine without DMA, in case
1101550a7375SFelipe Balbi 	 * for some reason you run out of channels here.
1102550a7375SFelipe Balbi 	 */
1103550a7375SFelipe Balbi 	if (is_dma_capable() && musb->dma_controller) {
1104550a7375SFelipe Balbi 		struct dma_controller	*c = musb->dma_controller;
1105550a7375SFelipe Balbi 
1106550a7375SFelipe Balbi 		musb_ep->dma = c->channel_alloc(c, hw_ep,
1107550a7375SFelipe Balbi 				(desc->bEndpointAddress & USB_DIR_IN));
1108550a7375SFelipe Balbi 	} else
1109550a7375SFelipe Balbi 		musb_ep->dma = NULL;
1110550a7375SFelipe Balbi 
1111550a7375SFelipe Balbi 	musb_ep->desc = desc;
1112550a7375SFelipe Balbi 	musb_ep->busy = 0;
111347e97605SSergei Shtylyov 	musb_ep->wedged = 0;
1114550a7375SFelipe Balbi 	status = 0;
1115550a7375SFelipe Balbi 
1116550a7375SFelipe Balbi 	pr_debug("%s periph: enabled %s for %s %s, %smaxpacket %d\n",
1117550a7375SFelipe Balbi 			musb_driver_name, musb_ep->end_point.name,
1118550a7375SFelipe Balbi 			({ char *s; switch (musb_ep->type) {
1119550a7375SFelipe Balbi 			case USB_ENDPOINT_XFER_BULK:	s = "bulk"; break;
1120550a7375SFelipe Balbi 			case USB_ENDPOINT_XFER_INT:	s = "int"; break;
1121550a7375SFelipe Balbi 			default:			s = "iso"; break;
11222b84f92bSJoe Perches 			} s; }),
1123550a7375SFelipe Balbi 			musb_ep->is_in ? "IN" : "OUT",
1124550a7375SFelipe Balbi 			musb_ep->dma ? "dma, " : "",
1125550a7375SFelipe Balbi 			musb_ep->packet_sz);
1126550a7375SFelipe Balbi 
1127550a7375SFelipe Balbi 	schedule_work(&musb->irq_work);
1128550a7375SFelipe Balbi 
1129550a7375SFelipe Balbi fail:
1130550a7375SFelipe Balbi 	spin_unlock_irqrestore(&musb->lock, flags);
1131550a7375SFelipe Balbi 	return status;
1132550a7375SFelipe Balbi }
1133550a7375SFelipe Balbi 
1134550a7375SFelipe Balbi /*
1135550a7375SFelipe Balbi  * Disable an endpoint flushing all requests queued.
1136550a7375SFelipe Balbi  */
1137550a7375SFelipe Balbi static int musb_gadget_disable(struct usb_ep *ep)
1138550a7375SFelipe Balbi {
1139550a7375SFelipe Balbi 	unsigned long	flags;
1140550a7375SFelipe Balbi 	struct musb	*musb;
1141550a7375SFelipe Balbi 	u8		epnum;
1142550a7375SFelipe Balbi 	struct musb_ep	*musb_ep;
1143550a7375SFelipe Balbi 	void __iomem	*epio;
1144550a7375SFelipe Balbi 	int		status = 0;
1145550a7375SFelipe Balbi 
1146550a7375SFelipe Balbi 	musb_ep = to_musb_ep(ep);
1147550a7375SFelipe Balbi 	musb = musb_ep->musb;
1148550a7375SFelipe Balbi 	epnum = musb_ep->current_epnum;
1149550a7375SFelipe Balbi 	epio = musb->endpoints[epnum].regs;
1150550a7375SFelipe Balbi 
1151550a7375SFelipe Balbi 	spin_lock_irqsave(&musb->lock, flags);
1152550a7375SFelipe Balbi 	musb_ep_select(musb->mregs, epnum);
1153550a7375SFelipe Balbi 
1154550a7375SFelipe Balbi 	/* zero the endpoint sizes */
1155550a7375SFelipe Balbi 	if (musb_ep->is_in) {
1156b18d26f6SSebastian Andrzej Siewior 		musb->intrtxe &= ~(1 << epnum);
1157b18d26f6SSebastian Andrzej Siewior 		musb_writew(musb->mregs, MUSB_INTRTXE, musb->intrtxe);
1158550a7375SFelipe Balbi 		musb_writew(epio, MUSB_TXMAXP, 0);
1159550a7375SFelipe Balbi 	} else {
1160af5ec14dSSebastian Andrzej Siewior 		musb->intrrxe &= ~(1 << epnum);
1161af5ec14dSSebastian Andrzej Siewior 		musb_writew(musb->mregs, MUSB_INTRRXE, musb->intrrxe);
1162550a7375SFelipe Balbi 		musb_writew(epio, MUSB_RXMAXP, 0);
1163550a7375SFelipe Balbi 	}
1164550a7375SFelipe Balbi 
1165550a7375SFelipe Balbi 	/* abort all pending DMA and requests */
1166550a7375SFelipe Balbi 	nuke(musb_ep, -ESHUTDOWN);
1167550a7375SFelipe Balbi 
1168607fb0f4STal Shorer 	musb_ep->desc = NULL;
1169607fb0f4STal Shorer 	musb_ep->end_point.desc = NULL;
1170607fb0f4STal Shorer 
1171550a7375SFelipe Balbi 	schedule_work(&musb->irq_work);
1172550a7375SFelipe Balbi 
1173550a7375SFelipe Balbi 	spin_unlock_irqrestore(&(musb->lock), flags);
1174550a7375SFelipe Balbi 
1175*b99d3659SBin Liu 	musb_dbg(musb, "%s", musb_ep->end_point.name);
1176550a7375SFelipe Balbi 
1177550a7375SFelipe Balbi 	return status;
1178550a7375SFelipe Balbi }
1179550a7375SFelipe Balbi 
1180550a7375SFelipe Balbi /*
1181550a7375SFelipe Balbi  * Allocate a request for an endpoint.
1182550a7375SFelipe Balbi  * Reused by ep0 code.
1183550a7375SFelipe Balbi  */
1184550a7375SFelipe Balbi struct usb_request *musb_alloc_request(struct usb_ep *ep, gfp_t gfp_flags)
1185550a7375SFelipe Balbi {
1186550a7375SFelipe Balbi 	struct musb_ep		*musb_ep = to_musb_ep(ep);
1187550a7375SFelipe Balbi 	struct musb_request	*request = NULL;
1188550a7375SFelipe Balbi 
1189550a7375SFelipe Balbi 	request = kzalloc(sizeof *request, gfp_flags);
1190*b99d3659SBin Liu 	if (!request)
11910607f862SFelipe Balbi 		return NULL;
11920607f862SFelipe Balbi 
1193550a7375SFelipe Balbi 	request->request.dma = DMA_ADDR_INVALID;
1194550a7375SFelipe Balbi 	request->epnum = musb_ep->current_epnum;
1195550a7375SFelipe Balbi 	request->ep = musb_ep;
1196550a7375SFelipe Balbi 
1197550a7375SFelipe Balbi 	return &request->request;
1198550a7375SFelipe Balbi }
1199550a7375SFelipe Balbi 
1200550a7375SFelipe Balbi /*
1201550a7375SFelipe Balbi  * Free a request
1202550a7375SFelipe Balbi  * Reused by ep0 code.
1203550a7375SFelipe Balbi  */
1204550a7375SFelipe Balbi void musb_free_request(struct usb_ep *ep, struct usb_request *req)
1205550a7375SFelipe Balbi {
1206550a7375SFelipe Balbi 	kfree(to_musb_request(req));
1207550a7375SFelipe Balbi }
1208550a7375SFelipe Balbi 
1209550a7375SFelipe Balbi static LIST_HEAD(buffers);
1210550a7375SFelipe Balbi 
1211550a7375SFelipe Balbi struct free_record {
1212550a7375SFelipe Balbi 	struct list_head	list;
1213550a7375SFelipe Balbi 	struct device		*dev;
1214550a7375SFelipe Balbi 	unsigned		bytes;
1215550a7375SFelipe Balbi 	dma_addr_t		dma;
1216550a7375SFelipe Balbi };
1217550a7375SFelipe Balbi 
1218550a7375SFelipe Balbi /*
1219550a7375SFelipe Balbi  * Context: controller locked, IRQs blocked.
1220550a7375SFelipe Balbi  */
1221a666e3e6SSergei Shtylyov void musb_ep_restart(struct musb *musb, struct musb_request *req)
1222550a7375SFelipe Balbi {
1223*b99d3659SBin Liu 	musb_dbg(musb, "<== %s request %p len %u on hw_ep%d",
1224550a7375SFelipe Balbi 		req->tx ? "TX/IN" : "RX/OUT",
1225550a7375SFelipe Balbi 		&req->request, req->request.length, req->epnum);
1226550a7375SFelipe Balbi 
1227550a7375SFelipe Balbi 	musb_ep_select(musb->mregs, req->epnum);
1228550a7375SFelipe Balbi 	if (req->tx)
1229550a7375SFelipe Balbi 		txstate(musb, req);
1230550a7375SFelipe Balbi 	else
1231550a7375SFelipe Balbi 		rxstate(musb, req);
1232550a7375SFelipe Balbi }
1233550a7375SFelipe Balbi 
1234550a7375SFelipe Balbi static int musb_gadget_queue(struct usb_ep *ep, struct usb_request *req,
1235550a7375SFelipe Balbi 			gfp_t gfp_flags)
1236550a7375SFelipe Balbi {
1237550a7375SFelipe Balbi 	struct musb_ep		*musb_ep;
1238550a7375SFelipe Balbi 	struct musb_request	*request;
1239550a7375SFelipe Balbi 	struct musb		*musb;
1240550a7375SFelipe Balbi 	int			status = 0;
1241550a7375SFelipe Balbi 	unsigned long		lockflags;
1242550a7375SFelipe Balbi 
1243550a7375SFelipe Balbi 	if (!ep || !req)
1244550a7375SFelipe Balbi 		return -EINVAL;
1245550a7375SFelipe Balbi 	if (!req->buf)
1246550a7375SFelipe Balbi 		return -ENODATA;
1247550a7375SFelipe Balbi 
1248550a7375SFelipe Balbi 	musb_ep = to_musb_ep(ep);
1249550a7375SFelipe Balbi 	musb = musb_ep->musb;
1250550a7375SFelipe Balbi 
1251550a7375SFelipe Balbi 	request = to_musb_request(req);
1252550a7375SFelipe Balbi 	request->musb = musb;
1253550a7375SFelipe Balbi 
1254550a7375SFelipe Balbi 	if (request->ep != musb_ep)
1255550a7375SFelipe Balbi 		return -EINVAL;
1256550a7375SFelipe Balbi 
1257*b99d3659SBin Liu 	musb_dbg(musb, "<== to %s request=%p", ep->name, req);
1258550a7375SFelipe Balbi 
1259550a7375SFelipe Balbi 	/* request is mine now... */
1260550a7375SFelipe Balbi 	request->request.actual = 0;
1261550a7375SFelipe Balbi 	request->request.status = -EINPROGRESS;
1262550a7375SFelipe Balbi 	request->epnum = musb_ep->current_epnum;
1263550a7375SFelipe Balbi 	request->tx = musb_ep->is_in;
1264550a7375SFelipe Balbi 
1265c65bfa62SMian Yousaf Kaukab 	map_dma_buffer(request, musb, musb_ep);
1266550a7375SFelipe Balbi 
1267550a7375SFelipe Balbi 	spin_lock_irqsave(&musb->lock, lockflags);
1268550a7375SFelipe Balbi 
1269550a7375SFelipe Balbi 	/* don't queue if the ep is down */
1270550a7375SFelipe Balbi 	if (!musb_ep->desc) {
1271*b99d3659SBin Liu 		musb_dbg(musb, "req %p queued to %s while ep %s",
1272550a7375SFelipe Balbi 				req, ep->name, "disabled");
1273550a7375SFelipe Balbi 		status = -ESHUTDOWN;
127423a53d90SSebastian Andrzej Siewior 		unmap_dma_buffer(request, musb);
127523a53d90SSebastian Andrzej Siewior 		goto unlock;
1276550a7375SFelipe Balbi 	}
1277550a7375SFelipe Balbi 
1278550a7375SFelipe Balbi 	/* add request to the list */
1279ad1adb89SFelipe Balbi 	list_add_tail(&request->list, &musb_ep->req_list);
1280550a7375SFelipe Balbi 
1281550a7375SFelipe Balbi 	/* it this is the head of the queue, start i/o ... */
1282ad1adb89SFelipe Balbi 	if (!musb_ep->busy && &request->list == musb_ep->req_list.next)
1283550a7375SFelipe Balbi 		musb_ep_restart(musb, request);
1284550a7375SFelipe Balbi 
128523a53d90SSebastian Andrzej Siewior unlock:
1286550a7375SFelipe Balbi 	spin_unlock_irqrestore(&musb->lock, lockflags);
1287550a7375SFelipe Balbi 	return status;
1288550a7375SFelipe Balbi }
1289550a7375SFelipe Balbi 
1290550a7375SFelipe Balbi static int musb_gadget_dequeue(struct usb_ep *ep, struct usb_request *request)
1291550a7375SFelipe Balbi {
1292550a7375SFelipe Balbi 	struct musb_ep		*musb_ep = to_musb_ep(ep);
12934cbbf084SFelipe Balbi 	struct musb_request	*req = to_musb_request(request);
12944cbbf084SFelipe Balbi 	struct musb_request	*r;
1295550a7375SFelipe Balbi 	unsigned long		flags;
1296550a7375SFelipe Balbi 	int			status = 0;
1297550a7375SFelipe Balbi 	struct musb		*musb = musb_ep->musb;
1298550a7375SFelipe Balbi 
1299550a7375SFelipe Balbi 	if (!ep || !request || to_musb_request(request)->ep != musb_ep)
1300550a7375SFelipe Balbi 		return -EINVAL;
1301550a7375SFelipe Balbi 
1302550a7375SFelipe Balbi 	spin_lock_irqsave(&musb->lock, flags);
1303550a7375SFelipe Balbi 
1304550a7375SFelipe Balbi 	list_for_each_entry(r, &musb_ep->req_list, list) {
13054cbbf084SFelipe Balbi 		if (r == req)
1306550a7375SFelipe Balbi 			break;
1307550a7375SFelipe Balbi 	}
13084cbbf084SFelipe Balbi 	if (r != req) {
1309*b99d3659SBin Liu 		dev_err(musb->controller, "request %p not queued to %s\n",
1310*b99d3659SBin Liu 				request, ep->name);
1311550a7375SFelipe Balbi 		status = -EINVAL;
1312550a7375SFelipe Balbi 		goto done;
1313550a7375SFelipe Balbi 	}
1314550a7375SFelipe Balbi 
1315550a7375SFelipe Balbi 	/* if the hardware doesn't have the request, easy ... */
13163d5ad13eSFelipe Balbi 	if (musb_ep->req_list.next != &req->list || musb_ep->busy)
1317550a7375SFelipe Balbi 		musb_g_giveback(musb_ep, request, -ECONNRESET);
1318550a7375SFelipe Balbi 
1319550a7375SFelipe Balbi 	/* ... else abort the dma transfer ... */
1320550a7375SFelipe Balbi 	else if (is_dma_capable() && musb_ep->dma) {
1321550a7375SFelipe Balbi 		struct dma_controller	*c = musb->dma_controller;
1322550a7375SFelipe Balbi 
1323550a7375SFelipe Balbi 		musb_ep_select(musb->mregs, musb_ep->current_epnum);
1324550a7375SFelipe Balbi 		if (c->channel_abort)
1325550a7375SFelipe Balbi 			status = c->channel_abort(musb_ep->dma);
1326550a7375SFelipe Balbi 		else
1327550a7375SFelipe Balbi 			status = -EBUSY;
1328550a7375SFelipe Balbi 		if (status == 0)
1329550a7375SFelipe Balbi 			musb_g_giveback(musb_ep, request, -ECONNRESET);
1330550a7375SFelipe Balbi 	} else {
1331550a7375SFelipe Balbi 		/* NOTE: by sticking to easily tested hardware/driver states,
1332550a7375SFelipe Balbi 		 * we leave counting of in-flight packets imprecise.
1333550a7375SFelipe Balbi 		 */
1334550a7375SFelipe Balbi 		musb_g_giveback(musb_ep, request, -ECONNRESET);
1335550a7375SFelipe Balbi 	}
1336550a7375SFelipe Balbi 
1337550a7375SFelipe Balbi done:
1338550a7375SFelipe Balbi 	spin_unlock_irqrestore(&musb->lock, flags);
1339550a7375SFelipe Balbi 	return status;
1340550a7375SFelipe Balbi }
1341550a7375SFelipe Balbi 
1342550a7375SFelipe Balbi /*
1343550a7375SFelipe Balbi  * Set or clear the halt bit of an endpoint. A halted enpoint won't tx/rx any
1344550a7375SFelipe Balbi  * data but will queue requests.
1345550a7375SFelipe Balbi  *
1346550a7375SFelipe Balbi  * exported to ep0 code
1347550a7375SFelipe Balbi  */
13481b6c3b0fSFelipe Balbi static int musb_gadget_set_halt(struct usb_ep *ep, int value)
1349550a7375SFelipe Balbi {
1350550a7375SFelipe Balbi 	struct musb_ep		*musb_ep = to_musb_ep(ep);
1351550a7375SFelipe Balbi 	u8			epnum = musb_ep->current_epnum;
1352550a7375SFelipe Balbi 	struct musb		*musb = musb_ep->musb;
1353550a7375SFelipe Balbi 	void __iomem		*epio = musb->endpoints[epnum].regs;
1354550a7375SFelipe Balbi 	void __iomem		*mbase;
1355550a7375SFelipe Balbi 	unsigned long		flags;
1356550a7375SFelipe Balbi 	u16			csr;
1357cea83241SSergei Shtylyov 	struct musb_request	*request;
1358550a7375SFelipe Balbi 	int			status = 0;
1359550a7375SFelipe Balbi 
1360550a7375SFelipe Balbi 	if (!ep)
1361550a7375SFelipe Balbi 		return -EINVAL;
1362550a7375SFelipe Balbi 	mbase = musb->mregs;
1363550a7375SFelipe Balbi 
1364550a7375SFelipe Balbi 	spin_lock_irqsave(&musb->lock, flags);
1365550a7375SFelipe Balbi 
1366550a7375SFelipe Balbi 	if ((USB_ENDPOINT_XFER_ISOC == musb_ep->type)) {
1367550a7375SFelipe Balbi 		status = -EINVAL;
1368550a7375SFelipe Balbi 		goto done;
1369550a7375SFelipe Balbi 	}
1370550a7375SFelipe Balbi 
1371550a7375SFelipe Balbi 	musb_ep_select(mbase, epnum);
1372550a7375SFelipe Balbi 
1373ad1adb89SFelipe Balbi 	request = next_request(musb_ep);
1374cea83241SSergei Shtylyov 	if (value) {
1375cea83241SSergei Shtylyov 		if (request) {
1376*b99d3659SBin Liu 			musb_dbg(musb, "request in progress, cannot halt %s",
1377cea83241SSergei Shtylyov 			    ep->name);
1378cea83241SSergei Shtylyov 			status = -EAGAIN;
1379cea83241SSergei Shtylyov 			goto done;
1380cea83241SSergei Shtylyov 		}
1381cea83241SSergei Shtylyov 		/* Cannot portably stall with non-empty FIFO */
1382cea83241SSergei Shtylyov 		if (musb_ep->is_in) {
1383550a7375SFelipe Balbi 			csr = musb_readw(epio, MUSB_TXCSR);
1384550a7375SFelipe Balbi 			if (csr & MUSB_TXCSR_FIFONOTEMPTY) {
1385*b99d3659SBin Liu 				musb_dbg(musb, "FIFO busy, cannot halt %s",
1386*b99d3659SBin Liu 						ep->name);
1387cea83241SSergei Shtylyov 				status = -EAGAIN;
1388cea83241SSergei Shtylyov 				goto done;
1389550a7375SFelipe Balbi 			}
1390cea83241SSergei Shtylyov 		}
139147e97605SSergei Shtylyov 	} else
139247e97605SSergei Shtylyov 		musb_ep->wedged = 0;
1393550a7375SFelipe Balbi 
1394550a7375SFelipe Balbi 	/* set/clear the stall and toggle bits */
1395*b99d3659SBin Liu 	musb_dbg(musb, "%s: %s stall", ep->name, value ? "set" : "clear");
1396550a7375SFelipe Balbi 	if (musb_ep->is_in) {
1397550a7375SFelipe Balbi 		csr = musb_readw(epio, MUSB_TXCSR);
1398550a7375SFelipe Balbi 		csr |= MUSB_TXCSR_P_WZC_BITS
1399550a7375SFelipe Balbi 			| MUSB_TXCSR_CLRDATATOG;
1400550a7375SFelipe Balbi 		if (value)
1401550a7375SFelipe Balbi 			csr |= MUSB_TXCSR_P_SENDSTALL;
1402550a7375SFelipe Balbi 		else
1403550a7375SFelipe Balbi 			csr &= ~(MUSB_TXCSR_P_SENDSTALL
1404550a7375SFelipe Balbi 				| MUSB_TXCSR_P_SENTSTALL);
1405550a7375SFelipe Balbi 		csr &= ~MUSB_TXCSR_TXPKTRDY;
1406550a7375SFelipe Balbi 		musb_writew(epio, MUSB_TXCSR, csr);
1407550a7375SFelipe Balbi 	} else {
1408550a7375SFelipe Balbi 		csr = musb_readw(epio, MUSB_RXCSR);
1409550a7375SFelipe Balbi 		csr |= MUSB_RXCSR_P_WZC_BITS
1410550a7375SFelipe Balbi 			| MUSB_RXCSR_FLUSHFIFO
1411550a7375SFelipe Balbi 			| MUSB_RXCSR_CLRDATATOG;
1412550a7375SFelipe Balbi 		if (value)
1413550a7375SFelipe Balbi 			csr |= MUSB_RXCSR_P_SENDSTALL;
1414550a7375SFelipe Balbi 		else
1415550a7375SFelipe Balbi 			csr &= ~(MUSB_RXCSR_P_SENDSTALL
1416550a7375SFelipe Balbi 				| MUSB_RXCSR_P_SENTSTALL);
1417550a7375SFelipe Balbi 		musb_writew(epio, MUSB_RXCSR, csr);
1418550a7375SFelipe Balbi 	}
1419550a7375SFelipe Balbi 
1420550a7375SFelipe Balbi 	/* maybe start the first request in the queue */
1421550a7375SFelipe Balbi 	if (!musb_ep->busy && !value && request) {
1422*b99d3659SBin Liu 		musb_dbg(musb, "restarting the request");
1423550a7375SFelipe Balbi 		musb_ep_restart(musb, request);
1424550a7375SFelipe Balbi 	}
1425550a7375SFelipe Balbi 
1426cea83241SSergei Shtylyov done:
1427550a7375SFelipe Balbi 	spin_unlock_irqrestore(&musb->lock, flags);
1428550a7375SFelipe Balbi 	return status;
1429550a7375SFelipe Balbi }
1430550a7375SFelipe Balbi 
143147e97605SSergei Shtylyov /*
143247e97605SSergei Shtylyov  * Sets the halt feature with the clear requests ignored
143347e97605SSergei Shtylyov  */
14341b6c3b0fSFelipe Balbi static int musb_gadget_set_wedge(struct usb_ep *ep)
143547e97605SSergei Shtylyov {
143647e97605SSergei Shtylyov 	struct musb_ep		*musb_ep = to_musb_ep(ep);
143747e97605SSergei Shtylyov 
143847e97605SSergei Shtylyov 	if (!ep)
143947e97605SSergei Shtylyov 		return -EINVAL;
144047e97605SSergei Shtylyov 
144147e97605SSergei Shtylyov 	musb_ep->wedged = 1;
144247e97605SSergei Shtylyov 
144347e97605SSergei Shtylyov 	return usb_ep_set_halt(ep);
144447e97605SSergei Shtylyov }
144547e97605SSergei Shtylyov 
1446550a7375SFelipe Balbi static int musb_gadget_fifo_status(struct usb_ep *ep)
1447550a7375SFelipe Balbi {
1448550a7375SFelipe Balbi 	struct musb_ep		*musb_ep = to_musb_ep(ep);
1449550a7375SFelipe Balbi 	void __iomem		*epio = musb_ep->hw_ep->regs;
1450550a7375SFelipe Balbi 	int			retval = -EINVAL;
1451550a7375SFelipe Balbi 
1452550a7375SFelipe Balbi 	if (musb_ep->desc && !musb_ep->is_in) {
1453550a7375SFelipe Balbi 		struct musb		*musb = musb_ep->musb;
1454550a7375SFelipe Balbi 		int			epnum = musb_ep->current_epnum;
1455550a7375SFelipe Balbi 		void __iomem		*mbase = musb->mregs;
1456550a7375SFelipe Balbi 		unsigned long		flags;
1457550a7375SFelipe Balbi 
1458550a7375SFelipe Balbi 		spin_lock_irqsave(&musb->lock, flags);
1459550a7375SFelipe Balbi 
1460550a7375SFelipe Balbi 		musb_ep_select(mbase, epnum);
1461550a7375SFelipe Balbi 		/* FIXME return zero unless RXPKTRDY is set */
1462550a7375SFelipe Balbi 		retval = musb_readw(epio, MUSB_RXCOUNT);
1463550a7375SFelipe Balbi 
1464550a7375SFelipe Balbi 		spin_unlock_irqrestore(&musb->lock, flags);
1465550a7375SFelipe Balbi 	}
1466550a7375SFelipe Balbi 	return retval;
1467550a7375SFelipe Balbi }
1468550a7375SFelipe Balbi 
1469550a7375SFelipe Balbi static void musb_gadget_fifo_flush(struct usb_ep *ep)
1470550a7375SFelipe Balbi {
1471550a7375SFelipe Balbi 	struct musb_ep	*musb_ep = to_musb_ep(ep);
1472550a7375SFelipe Balbi 	struct musb	*musb = musb_ep->musb;
1473550a7375SFelipe Balbi 	u8		epnum = musb_ep->current_epnum;
1474550a7375SFelipe Balbi 	void __iomem	*epio = musb->endpoints[epnum].regs;
1475550a7375SFelipe Balbi 	void __iomem	*mbase;
1476550a7375SFelipe Balbi 	unsigned long	flags;
1477b18d26f6SSebastian Andrzej Siewior 	u16		csr;
1478550a7375SFelipe Balbi 
1479550a7375SFelipe Balbi 	mbase = musb->mregs;
1480550a7375SFelipe Balbi 
1481550a7375SFelipe Balbi 	spin_lock_irqsave(&musb->lock, flags);
1482550a7375SFelipe Balbi 	musb_ep_select(mbase, (u8) epnum);
1483550a7375SFelipe Balbi 
1484550a7375SFelipe Balbi 	/* disable interrupts */
1485b18d26f6SSebastian Andrzej Siewior 	musb_writew(mbase, MUSB_INTRTXE, musb->intrtxe & ~(1 << epnum));
1486550a7375SFelipe Balbi 
1487550a7375SFelipe Balbi 	if (musb_ep->is_in) {
1488550a7375SFelipe Balbi 		csr = musb_readw(epio, MUSB_TXCSR);
1489550a7375SFelipe Balbi 		if (csr & MUSB_TXCSR_FIFONOTEMPTY) {
1490550a7375SFelipe Balbi 			csr |= MUSB_TXCSR_FLUSHFIFO | MUSB_TXCSR_P_WZC_BITS;
14914858f06eSYauheni Kaliuta 			/*
14924858f06eSYauheni Kaliuta 			 * Setting both TXPKTRDY and FLUSHFIFO makes controller
14934858f06eSYauheni Kaliuta 			 * to interrupt current FIFO loading, but not flushing
14944858f06eSYauheni Kaliuta 			 * the already loaded ones.
14954858f06eSYauheni Kaliuta 			 */
14964858f06eSYauheni Kaliuta 			csr &= ~MUSB_TXCSR_TXPKTRDY;
1497550a7375SFelipe Balbi 			musb_writew(epio, MUSB_TXCSR, csr);
1498550a7375SFelipe Balbi 			/* REVISIT may be inappropriate w/o FIFONOTEMPTY ... */
1499550a7375SFelipe Balbi 			musb_writew(epio, MUSB_TXCSR, csr);
1500550a7375SFelipe Balbi 		}
1501550a7375SFelipe Balbi 	} else {
1502550a7375SFelipe Balbi 		csr = musb_readw(epio, MUSB_RXCSR);
1503550a7375SFelipe Balbi 		csr |= MUSB_RXCSR_FLUSHFIFO | MUSB_RXCSR_P_WZC_BITS;
1504550a7375SFelipe Balbi 		musb_writew(epio, MUSB_RXCSR, csr);
1505550a7375SFelipe Balbi 		musb_writew(epio, MUSB_RXCSR, csr);
1506550a7375SFelipe Balbi 	}
1507550a7375SFelipe Balbi 
1508550a7375SFelipe Balbi 	/* re-enable interrupt */
1509b18d26f6SSebastian Andrzej Siewior 	musb_writew(mbase, MUSB_INTRTXE, musb->intrtxe);
1510550a7375SFelipe Balbi 	spin_unlock_irqrestore(&musb->lock, flags);
1511550a7375SFelipe Balbi }
1512550a7375SFelipe Balbi 
1513550a7375SFelipe Balbi static const struct usb_ep_ops musb_ep_ops = {
1514550a7375SFelipe Balbi 	.enable		= musb_gadget_enable,
1515550a7375SFelipe Balbi 	.disable	= musb_gadget_disable,
1516550a7375SFelipe Balbi 	.alloc_request	= musb_alloc_request,
1517550a7375SFelipe Balbi 	.free_request	= musb_free_request,
1518550a7375SFelipe Balbi 	.queue		= musb_gadget_queue,
1519550a7375SFelipe Balbi 	.dequeue	= musb_gadget_dequeue,
1520550a7375SFelipe Balbi 	.set_halt	= musb_gadget_set_halt,
152147e97605SSergei Shtylyov 	.set_wedge	= musb_gadget_set_wedge,
1522550a7375SFelipe Balbi 	.fifo_status	= musb_gadget_fifo_status,
1523550a7375SFelipe Balbi 	.fifo_flush	= musb_gadget_fifo_flush
1524550a7375SFelipe Balbi };
1525550a7375SFelipe Balbi 
1526550a7375SFelipe Balbi /* ----------------------------------------------------------------------- */
1527550a7375SFelipe Balbi 
1528550a7375SFelipe Balbi static int musb_gadget_get_frame(struct usb_gadget *gadget)
1529550a7375SFelipe Balbi {
1530550a7375SFelipe Balbi 	struct musb	*musb = gadget_to_musb(gadget);
1531550a7375SFelipe Balbi 
1532550a7375SFelipe Balbi 	return (int)musb_readw(musb->mregs, MUSB_FRAME);
1533550a7375SFelipe Balbi }
1534550a7375SFelipe Balbi 
1535550a7375SFelipe Balbi static int musb_gadget_wakeup(struct usb_gadget *gadget)
1536550a7375SFelipe Balbi {
1537550a7375SFelipe Balbi 	struct musb	*musb = gadget_to_musb(gadget);
1538550a7375SFelipe Balbi 	void __iomem	*mregs = musb->mregs;
1539550a7375SFelipe Balbi 	unsigned long	flags;
1540550a7375SFelipe Balbi 	int		status = -EINVAL;
1541550a7375SFelipe Balbi 	u8		power, devctl;
1542550a7375SFelipe Balbi 	int		retries;
1543550a7375SFelipe Balbi 
1544550a7375SFelipe Balbi 	spin_lock_irqsave(&musb->lock, flags);
1545550a7375SFelipe Balbi 
1546e47d9254SAntoine Tenart 	switch (musb->xceiv->otg->state) {
1547550a7375SFelipe Balbi 	case OTG_STATE_B_PERIPHERAL:
1548550a7375SFelipe Balbi 		/* NOTE:  OTG state machine doesn't include B_SUSPENDED;
1549550a7375SFelipe Balbi 		 * that's part of the standard usb 1.1 state machine, and
1550550a7375SFelipe Balbi 		 * doesn't affect OTG transitions.
1551550a7375SFelipe Balbi 		 */
1552550a7375SFelipe Balbi 		if (musb->may_wakeup && musb->is_suspended)
1553550a7375SFelipe Balbi 			break;
1554550a7375SFelipe Balbi 		goto done;
1555550a7375SFelipe Balbi 	case OTG_STATE_B_IDLE:
1556550a7375SFelipe Balbi 		/* Start SRP ... OTG not required. */
1557550a7375SFelipe Balbi 		devctl = musb_readb(mregs, MUSB_DEVCTL);
1558*b99d3659SBin Liu 		musb_dbg(musb, "Sending SRP: devctl: %02x", devctl);
1559550a7375SFelipe Balbi 		devctl |= MUSB_DEVCTL_SESSION;
1560550a7375SFelipe Balbi 		musb_writeb(mregs, MUSB_DEVCTL, devctl);
1561550a7375SFelipe Balbi 		devctl = musb_readb(mregs, MUSB_DEVCTL);
1562550a7375SFelipe Balbi 		retries = 100;
1563550a7375SFelipe Balbi 		while (!(devctl & MUSB_DEVCTL_SESSION)) {
1564550a7375SFelipe Balbi 			devctl = musb_readb(mregs, MUSB_DEVCTL);
1565550a7375SFelipe Balbi 			if (retries-- < 1)
1566550a7375SFelipe Balbi 				break;
1567550a7375SFelipe Balbi 		}
1568550a7375SFelipe Balbi 		retries = 10000;
1569550a7375SFelipe Balbi 		while (devctl & MUSB_DEVCTL_SESSION) {
1570550a7375SFelipe Balbi 			devctl = musb_readb(mregs, MUSB_DEVCTL);
1571550a7375SFelipe Balbi 			if (retries-- < 1)
1572550a7375SFelipe Balbi 				break;
1573550a7375SFelipe Balbi 		}
1574550a7375SFelipe Balbi 
15758620543eSHema HK 		spin_unlock_irqrestore(&musb->lock, flags);
15766e13c650SHeikki Krogerus 		otg_start_srp(musb->xceiv->otg);
15778620543eSHema HK 		spin_lock_irqsave(&musb->lock, flags);
15788620543eSHema HK 
1579550a7375SFelipe Balbi 		/* Block idling for at least 1s */
1580550a7375SFelipe Balbi 		musb_platform_try_idle(musb,
1581550a7375SFelipe Balbi 			jiffies + msecs_to_jiffies(1 * HZ));
1582550a7375SFelipe Balbi 
1583550a7375SFelipe Balbi 		status = 0;
1584550a7375SFelipe Balbi 		goto done;
1585550a7375SFelipe Balbi 	default:
1586*b99d3659SBin Liu 		musb_dbg(musb, "Unhandled wake: %s",
1587e47d9254SAntoine Tenart 			usb_otg_state_string(musb->xceiv->otg->state));
1588550a7375SFelipe Balbi 		goto done;
1589550a7375SFelipe Balbi 	}
1590550a7375SFelipe Balbi 
1591550a7375SFelipe Balbi 	status = 0;
1592550a7375SFelipe Balbi 
1593550a7375SFelipe Balbi 	power = musb_readb(mregs, MUSB_POWER);
1594550a7375SFelipe Balbi 	power |= MUSB_POWER_RESUME;
1595550a7375SFelipe Balbi 	musb_writeb(mregs, MUSB_POWER, power);
1596*b99d3659SBin Liu 	musb_dbg(musb, "issue wakeup");
1597550a7375SFelipe Balbi 
1598550a7375SFelipe Balbi 	/* FIXME do this next chunk in a timer callback, no udelay */
1599550a7375SFelipe Balbi 	mdelay(2);
1600550a7375SFelipe Balbi 
1601550a7375SFelipe Balbi 	power = musb_readb(mregs, MUSB_POWER);
1602550a7375SFelipe Balbi 	power &= ~MUSB_POWER_RESUME;
1603550a7375SFelipe Balbi 	musb_writeb(mregs, MUSB_POWER, power);
1604550a7375SFelipe Balbi done:
1605550a7375SFelipe Balbi 	spin_unlock_irqrestore(&musb->lock, flags);
1606550a7375SFelipe Balbi 	return status;
1607550a7375SFelipe Balbi }
1608550a7375SFelipe Balbi 
1609550a7375SFelipe Balbi static int
1610550a7375SFelipe Balbi musb_gadget_set_self_powered(struct usb_gadget *gadget, int is_selfpowered)
1611550a7375SFelipe Balbi {
1612dadac986SPeter Chen 	gadget->is_selfpowered = !!is_selfpowered;
1613550a7375SFelipe Balbi 	return 0;
1614550a7375SFelipe Balbi }
1615550a7375SFelipe Balbi 
1616550a7375SFelipe Balbi static void musb_pullup(struct musb *musb, int is_on)
1617550a7375SFelipe Balbi {
1618550a7375SFelipe Balbi 	u8 power;
1619550a7375SFelipe Balbi 
1620550a7375SFelipe Balbi 	power = musb_readb(musb->mregs, MUSB_POWER);
1621550a7375SFelipe Balbi 	if (is_on)
1622550a7375SFelipe Balbi 		power |= MUSB_POWER_SOFTCONN;
1623550a7375SFelipe Balbi 	else
1624550a7375SFelipe Balbi 		power &= ~MUSB_POWER_SOFTCONN;
1625550a7375SFelipe Balbi 
1626550a7375SFelipe Balbi 	/* FIXME if on, HdrcStart; if off, HdrcStop */
1627550a7375SFelipe Balbi 
1628*b99d3659SBin Liu 	musb_dbg(musb, "gadget D+ pullup %s",
1629e71eb392SSebastian Andrzej Siewior 		is_on ? "on" : "off");
1630550a7375SFelipe Balbi 	musb_writeb(musb->mregs, MUSB_POWER, power);
1631550a7375SFelipe Balbi }
1632550a7375SFelipe Balbi 
1633550a7375SFelipe Balbi #if 0
1634550a7375SFelipe Balbi static int musb_gadget_vbus_session(struct usb_gadget *gadget, int is_active)
1635550a7375SFelipe Balbi {
1636*b99d3659SBin Liu 	musb_dbg(musb, "<= %s =>\n", __func__);
1637550a7375SFelipe Balbi 
1638550a7375SFelipe Balbi 	/*
1639550a7375SFelipe Balbi 	 * FIXME iff driver's softconnect flag is set (as it is during probe,
1640550a7375SFelipe Balbi 	 * though that can clear it), just musb_pullup().
1641550a7375SFelipe Balbi 	 */
1642550a7375SFelipe Balbi 
1643550a7375SFelipe Balbi 	return -EINVAL;
1644550a7375SFelipe Balbi }
1645550a7375SFelipe Balbi #endif
1646550a7375SFelipe Balbi 
1647550a7375SFelipe Balbi static int musb_gadget_vbus_draw(struct usb_gadget *gadget, unsigned mA)
1648550a7375SFelipe Balbi {
1649550a7375SFelipe Balbi 	struct musb	*musb = gadget_to_musb(gadget);
1650550a7375SFelipe Balbi 
165184e250ffSDavid Brownell 	if (!musb->xceiv->set_power)
1652550a7375SFelipe Balbi 		return -EOPNOTSUPP;
1653b96d3b08SHeikki Krogerus 	return usb_phy_set_power(musb->xceiv, mA);
1654550a7375SFelipe Balbi }
1655550a7375SFelipe Balbi 
1656517bafffSTony Lindgren static void musb_gadget_work(struct work_struct *work)
1657517bafffSTony Lindgren {
1658517bafffSTony Lindgren 	struct musb *musb;
1659517bafffSTony Lindgren 	unsigned long flags;
1660517bafffSTony Lindgren 
1661517bafffSTony Lindgren 	musb = container_of(work, struct musb, gadget_work.work);
1662517bafffSTony Lindgren 	pm_runtime_get_sync(musb->controller);
1663517bafffSTony Lindgren 	spin_lock_irqsave(&musb->lock, flags);
1664517bafffSTony Lindgren 	musb_pullup(musb, musb->softconnect);
1665517bafffSTony Lindgren 	spin_unlock_irqrestore(&musb->lock, flags);
1666517bafffSTony Lindgren 	pm_runtime_mark_last_busy(musb->controller);
1667517bafffSTony Lindgren 	pm_runtime_put_autosuspend(musb->controller);
1668517bafffSTony Lindgren }
1669517bafffSTony Lindgren 
1670550a7375SFelipe Balbi static int musb_gadget_pullup(struct usb_gadget *gadget, int is_on)
1671550a7375SFelipe Balbi {
1672550a7375SFelipe Balbi 	struct musb	*musb = gadget_to_musb(gadget);
1673550a7375SFelipe Balbi 	unsigned long	flags;
1674550a7375SFelipe Balbi 
1675550a7375SFelipe Balbi 	is_on = !!is_on;
1676550a7375SFelipe Balbi 
1677550a7375SFelipe Balbi 	/* NOTE: this assumes we are sensing vbus; we'd rather
1678550a7375SFelipe Balbi 	 * not pullup unless the B-session is active.
1679550a7375SFelipe Balbi 	 */
1680550a7375SFelipe Balbi 	spin_lock_irqsave(&musb->lock, flags);
1681550a7375SFelipe Balbi 	if (is_on != musb->softconnect) {
1682550a7375SFelipe Balbi 		musb->softconnect = is_on;
1683517bafffSTony Lindgren 		schedule_delayed_work(&musb->gadget_work, 0);
1684550a7375SFelipe Balbi 	}
1685550a7375SFelipe Balbi 	spin_unlock_irqrestore(&musb->lock, flags);
168693e098a8SJohn Stultz 
1687550a7375SFelipe Balbi 	return 0;
1688550a7375SFelipe Balbi }
1689550a7375SFelipe Balbi 
169026b8aa45SRobert Baldyga #ifdef CONFIG_BLACKFIN
169126b8aa45SRobert Baldyga static struct usb_ep *musb_match_ep(struct usb_gadget *g,
169226b8aa45SRobert Baldyga 		struct usb_endpoint_descriptor *desc,
169326b8aa45SRobert Baldyga 		struct usb_ss_ep_comp_descriptor *ep_comp)
169426b8aa45SRobert Baldyga {
169526b8aa45SRobert Baldyga 	struct usb_ep *ep = NULL;
169626b8aa45SRobert Baldyga 
169726b8aa45SRobert Baldyga 	switch (usb_endpoint_type(desc)) {
169826b8aa45SRobert Baldyga 	case USB_ENDPOINT_XFER_ISOC:
169926b8aa45SRobert Baldyga 	case USB_ENDPOINT_XFER_BULK:
170026b8aa45SRobert Baldyga 		if (usb_endpoint_dir_in(desc))
170126b8aa45SRobert Baldyga 			ep = gadget_find_ep_by_name(g, "ep5in");
170226b8aa45SRobert Baldyga 		else
170326b8aa45SRobert Baldyga 			ep = gadget_find_ep_by_name(g, "ep6out");
170426b8aa45SRobert Baldyga 		break;
170526b8aa45SRobert Baldyga 	case USB_ENDPOINT_XFER_INT:
170626b8aa45SRobert Baldyga 		if (usb_endpoint_dir_in(desc))
170726b8aa45SRobert Baldyga 			ep = gadget_find_ep_by_name(g, "ep1in");
170826b8aa45SRobert Baldyga 		else
170926b8aa45SRobert Baldyga 			ep = gadget_find_ep_by_name(g, "ep2out");
171026b8aa45SRobert Baldyga 		break;
171126b8aa45SRobert Baldyga 	default:
17122f3cc24fSRobert Baldyga 		break;
171326b8aa45SRobert Baldyga 	}
171426b8aa45SRobert Baldyga 
171526b8aa45SRobert Baldyga 	if (ep && usb_gadget_ep_match_desc(g, ep, desc, ep_comp))
171626b8aa45SRobert Baldyga 		return ep;
171726b8aa45SRobert Baldyga 
171826b8aa45SRobert Baldyga 	return NULL;
171926b8aa45SRobert Baldyga }
172026b8aa45SRobert Baldyga #else
172126b8aa45SRobert Baldyga #define musb_match_ep NULL
172226b8aa45SRobert Baldyga #endif
172326b8aa45SRobert Baldyga 
1724e71eb392SSebastian Andrzej Siewior static int musb_gadget_start(struct usb_gadget *g,
1725e71eb392SSebastian Andrzej Siewior 		struct usb_gadget_driver *driver);
172622835b80SFelipe Balbi static int musb_gadget_stop(struct usb_gadget *g);
17270f91349bSSebastian Andrzej Siewior 
1728550a7375SFelipe Balbi static const struct usb_gadget_ops musb_gadget_operations = {
1729550a7375SFelipe Balbi 	.get_frame		= musb_gadget_get_frame,
1730550a7375SFelipe Balbi 	.wakeup			= musb_gadget_wakeup,
1731550a7375SFelipe Balbi 	.set_selfpowered	= musb_gadget_set_self_powered,
1732550a7375SFelipe Balbi 	/* .vbus_session		= musb_gadget_vbus_session, */
1733550a7375SFelipe Balbi 	.vbus_draw		= musb_gadget_vbus_draw,
1734550a7375SFelipe Balbi 	.pullup			= musb_gadget_pullup,
1735e71eb392SSebastian Andrzej Siewior 	.udc_start		= musb_gadget_start,
1736e71eb392SSebastian Andrzej Siewior 	.udc_stop		= musb_gadget_stop,
173726b8aa45SRobert Baldyga 	.match_ep		= musb_match_ep,
1738550a7375SFelipe Balbi };
1739550a7375SFelipe Balbi 
1740550a7375SFelipe Balbi /* ----------------------------------------------------------------------- */
1741550a7375SFelipe Balbi 
1742550a7375SFelipe Balbi /* Registration */
1743550a7375SFelipe Balbi 
1744550a7375SFelipe Balbi /* Only this registration code "knows" the rule (from USB standards)
1745550a7375SFelipe Balbi  * about there being only one external upstream port.  It assumes
1746550a7375SFelipe Balbi  * all peripheral ports are external...
1747550a7375SFelipe Balbi  */
1748550a7375SFelipe Balbi 
174941ac7b3aSBill Pemberton static void
1750550a7375SFelipe Balbi init_peripheral_ep(struct musb *musb, struct musb_ep *ep, u8 epnum, int is_in)
1751550a7375SFelipe Balbi {
1752550a7375SFelipe Balbi 	struct musb_hw_ep	*hw_ep = musb->endpoints + epnum;
1753550a7375SFelipe Balbi 
1754550a7375SFelipe Balbi 	memset(ep, 0, sizeof *ep);
1755550a7375SFelipe Balbi 
1756550a7375SFelipe Balbi 	ep->current_epnum = epnum;
1757550a7375SFelipe Balbi 	ep->musb = musb;
1758550a7375SFelipe Balbi 	ep->hw_ep = hw_ep;
1759550a7375SFelipe Balbi 	ep->is_in = is_in;
1760550a7375SFelipe Balbi 
1761550a7375SFelipe Balbi 	INIT_LIST_HEAD(&ep->req_list);
1762550a7375SFelipe Balbi 
1763550a7375SFelipe Balbi 	sprintf(ep->name, "ep%d%s", epnum,
1764550a7375SFelipe Balbi 			(!epnum || hw_ep->is_shared_fifo) ? "" : (
1765550a7375SFelipe Balbi 				is_in ? "in" : "out"));
1766550a7375SFelipe Balbi 	ep->end_point.name = ep->name;
1767550a7375SFelipe Balbi 	INIT_LIST_HEAD(&ep->end_point.ep_list);
1768550a7375SFelipe Balbi 	if (!epnum) {
1769e117e742SRobert Baldyga 		usb_ep_set_maxpacket_limit(&ep->end_point, 64);
17708501955eSRobert Baldyga 		ep->end_point.caps.type_control = true;
1771550a7375SFelipe Balbi 		ep->end_point.ops = &musb_g_ep0_ops;
1772550a7375SFelipe Balbi 		musb->g.ep0 = &ep->end_point;
1773550a7375SFelipe Balbi 	} else {
1774550a7375SFelipe Balbi 		if (is_in)
1775e117e742SRobert Baldyga 			usb_ep_set_maxpacket_limit(&ep->end_point, hw_ep->max_packet_sz_tx);
1776550a7375SFelipe Balbi 		else
1777e117e742SRobert Baldyga 			usb_ep_set_maxpacket_limit(&ep->end_point, hw_ep->max_packet_sz_rx);
17788501955eSRobert Baldyga 		ep->end_point.caps.type_iso = true;
17798501955eSRobert Baldyga 		ep->end_point.caps.type_bulk = true;
17808501955eSRobert Baldyga 		ep->end_point.caps.type_int = true;
1781550a7375SFelipe Balbi 		ep->end_point.ops = &musb_ep_ops;
1782550a7375SFelipe Balbi 		list_add_tail(&ep->end_point.ep_list, &musb->g.ep_list);
1783550a7375SFelipe Balbi 	}
17848501955eSRobert Baldyga 
17858501955eSRobert Baldyga 	if (!epnum || hw_ep->is_shared_fifo) {
17868501955eSRobert Baldyga 		ep->end_point.caps.dir_in = true;
17878501955eSRobert Baldyga 		ep->end_point.caps.dir_out = true;
17888501955eSRobert Baldyga 	} else if (is_in)
17898501955eSRobert Baldyga 		ep->end_point.caps.dir_in = true;
17908501955eSRobert Baldyga 	else
17918501955eSRobert Baldyga 		ep->end_point.caps.dir_out = true;
1792550a7375SFelipe Balbi }
1793550a7375SFelipe Balbi 
1794550a7375SFelipe Balbi /*
1795550a7375SFelipe Balbi  * Initialize the endpoints exposed to peripheral drivers, with backlinks
1796550a7375SFelipe Balbi  * to the rest of the driver state.
1797550a7375SFelipe Balbi  */
179841ac7b3aSBill Pemberton static inline void musb_g_init_endpoints(struct musb *musb)
1799550a7375SFelipe Balbi {
1800550a7375SFelipe Balbi 	u8			epnum;
1801550a7375SFelipe Balbi 	struct musb_hw_ep	*hw_ep;
1802550a7375SFelipe Balbi 	unsigned		count = 0;
1803550a7375SFelipe Balbi 
1804b595076aSUwe Kleine-König 	/* initialize endpoint list just once */
1805550a7375SFelipe Balbi 	INIT_LIST_HEAD(&(musb->g.ep_list));
1806550a7375SFelipe Balbi 
1807550a7375SFelipe Balbi 	for (epnum = 0, hw_ep = musb->endpoints;
1808550a7375SFelipe Balbi 			epnum < musb->nr_endpoints;
1809550a7375SFelipe Balbi 			epnum++, hw_ep++) {
1810550a7375SFelipe Balbi 		if (hw_ep->is_shared_fifo /* || !epnum */) {
1811550a7375SFelipe Balbi 			init_peripheral_ep(musb, &hw_ep->ep_in, epnum, 0);
1812550a7375SFelipe Balbi 			count++;
1813550a7375SFelipe Balbi 		} else {
1814550a7375SFelipe Balbi 			if (hw_ep->max_packet_sz_tx) {
1815550a7375SFelipe Balbi 				init_peripheral_ep(musb, &hw_ep->ep_in,
1816550a7375SFelipe Balbi 							epnum, 1);
1817550a7375SFelipe Balbi 				count++;
1818550a7375SFelipe Balbi 			}
1819550a7375SFelipe Balbi 			if (hw_ep->max_packet_sz_rx) {
1820550a7375SFelipe Balbi 				init_peripheral_ep(musb, &hw_ep->ep_out,
1821550a7375SFelipe Balbi 							epnum, 0);
1822550a7375SFelipe Balbi 				count++;
1823550a7375SFelipe Balbi 			}
1824550a7375SFelipe Balbi 		}
1825550a7375SFelipe Balbi 	}
1826550a7375SFelipe Balbi }
1827550a7375SFelipe Balbi 
1828550a7375SFelipe Balbi /* called once during driver setup to initialize and link into
1829550a7375SFelipe Balbi  * the driver model; memory is zeroed.
1830550a7375SFelipe Balbi  */
183141ac7b3aSBill Pemberton int musb_gadget_setup(struct musb *musb)
1832550a7375SFelipe Balbi {
1833550a7375SFelipe Balbi 	int status;
1834550a7375SFelipe Balbi 
1835550a7375SFelipe Balbi 	/* REVISIT minor race:  if (erroneously) setting up two
1836550a7375SFelipe Balbi 	 * musb peripherals at the same time, only the bus lock
1837550a7375SFelipe Balbi 	 * is probably held.
1838550a7375SFelipe Balbi 	 */
1839550a7375SFelipe Balbi 
1840550a7375SFelipe Balbi 	musb->g.ops = &musb_gadget_operations;
1841d327ab5bSMichal Nazarewicz 	musb->g.max_speed = USB_SPEED_HIGH;
1842550a7375SFelipe Balbi 	musb->g.speed = USB_SPEED_UNKNOWN;
1843550a7375SFelipe Balbi 
18441374a430SBin Liu 	MUSB_DEV_MODE(musb);
18451374a430SBin Liu 	musb->xceiv->otg->default_a = 0;
1846e47d9254SAntoine Tenart 	musb->xceiv->otg->state = OTG_STATE_B_IDLE;
18471374a430SBin Liu 
1848550a7375SFelipe Balbi 	/* this "gadget" abstracts/virtualizes the controller */
1849550a7375SFelipe Balbi 	musb->g.name = musb_driver_name;
1850fd3923a9SApelete Seketeli #if IS_ENABLED(CONFIG_USB_MUSB_DUAL_ROLE)
1851550a7375SFelipe Balbi 	musb->g.is_otg = 1;
1852fd3923a9SApelete Seketeli #elif IS_ENABLED(CONFIG_USB_MUSB_GADGET)
1853fd3923a9SApelete Seketeli 	musb->g.is_otg = 0;
1854fd3923a9SApelete Seketeli #endif
1855517bafffSTony Lindgren 	INIT_DELAYED_WORK(&musb->gadget_work, musb_gadget_work);
1856550a7375SFelipe Balbi 	musb_g_init_endpoints(musb);
1857550a7375SFelipe Balbi 
1858550a7375SFelipe Balbi 	musb->is_active = 0;
1859550a7375SFelipe Balbi 	musb_platform_try_idle(musb, 0);
1860550a7375SFelipe Balbi 
18610f91349bSSebastian Andrzej Siewior 	status = usb_add_gadget_udc(musb->controller, &musb->g);
18620f91349bSSebastian Andrzej Siewior 	if (status)
18630f91349bSSebastian Andrzej Siewior 		goto err;
18640f91349bSSebastian Andrzej Siewior 
18650f91349bSSebastian Andrzej Siewior 	return 0;
18660f91349bSSebastian Andrzej Siewior err:
18676193d699SSebastian Andrzej Siewior 	musb->g.dev.parent = NULL;
18680f91349bSSebastian Andrzej Siewior 	device_unregister(&musb->g.dev);
1869550a7375SFelipe Balbi 	return status;
1870550a7375SFelipe Balbi }
1871550a7375SFelipe Balbi 
1872550a7375SFelipe Balbi void musb_gadget_cleanup(struct musb *musb)
1873550a7375SFelipe Balbi {
187490474288SSebastian Andrzej Siewior 	if (musb->port_mode == MUSB_PORT_MODE_HOST)
187590474288SSebastian Andrzej Siewior 		return;
1876517bafffSTony Lindgren 
1877517bafffSTony Lindgren 	cancel_delayed_work_sync(&musb->gadget_work);
18780f91349bSSebastian Andrzej Siewior 	usb_del_gadget_udc(&musb->g);
1879550a7375SFelipe Balbi }
1880550a7375SFelipe Balbi 
1881550a7375SFelipe Balbi /*
1882550a7375SFelipe Balbi  * Register the gadget driver. Used by gadget drivers when
1883550a7375SFelipe Balbi  * registering themselves with the controller.
1884550a7375SFelipe Balbi  *
1885550a7375SFelipe Balbi  * -EINVAL something went wrong (not driver)
1886550a7375SFelipe Balbi  * -EBUSY another gadget is already using the controller
1887b595076aSUwe Kleine-König  * -ENOMEM no memory to perform the operation
1888550a7375SFelipe Balbi  *
1889550a7375SFelipe Balbi  * @param driver the gadget driver
1890550a7375SFelipe Balbi  * @return <0 if error, 0 if everything is fine
1891550a7375SFelipe Balbi  */
1892e71eb392SSebastian Andrzej Siewior static int musb_gadget_start(struct usb_gadget *g,
1893e71eb392SSebastian Andrzej Siewior 		struct usb_gadget_driver *driver)
1894550a7375SFelipe Balbi {
1895e71eb392SSebastian Andrzej Siewior 	struct musb		*musb = gadget_to_musb(g);
1896d445b6daSHeikki Krogerus 	struct usb_otg		*otg = musb->xceiv->otg;
189763eed2b5SFelipe Balbi 	unsigned long		flags;
1898032ec49fSFelipe Balbi 	int			retval = 0;
1899550a7375SFelipe Balbi 
1900032ec49fSFelipe Balbi 	if (driver->max_speed < USB_SPEED_HIGH) {
1901032ec49fSFelipe Balbi 		retval = -EINVAL;
1902032ec49fSFelipe Balbi 		goto err;
1903032ec49fSFelipe Balbi 	}
1904550a7375SFelipe Balbi 
19057acc6197SHema HK 	pm_runtime_get_sync(musb->controller);
19067acc6197SHema HK 
1907e71eb392SSebastian Andrzej Siewior 	musb->softconnect = 0;
1908550a7375SFelipe Balbi 	musb->gadget_driver = driver;
1909550a7375SFelipe Balbi 
1910550a7375SFelipe Balbi 	spin_lock_irqsave(&musb->lock, flags);
191143e699ceSGreg Kroah-Hartman 	musb->is_active = 1;
1912550a7375SFelipe Balbi 
19136e13c650SHeikki Krogerus 	otg_set_peripheral(otg, &musb->g);
1914e47d9254SAntoine Tenart 	musb->xceiv->otg->state = OTG_STATE_B_IDLE;
1915550a7375SFelipe Balbi 	spin_unlock_irqrestore(&musb->lock, flags);
1916550a7375SFelipe Balbi 
1917001dd84aSSebastian Andrzej Siewior 	musb_start(musb);
1918001dd84aSSebastian Andrzej Siewior 
1919550a7375SFelipe Balbi 	/* REVISIT:  funcall to other code, which also
1920550a7375SFelipe Balbi 	 * handles power budgeting ... this way also
1921550a7375SFelipe Balbi 	 * ensures HdrcStart is indirectly called.
1922550a7375SFelipe Balbi 	 */
1923b65ae0f1SGrazvydas Ignotas 	if (musb->xceiv->last_event == USB_EVENT_ID)
1924b65ae0f1SGrazvydas Ignotas 		musb_platform_set_vbus(musb, 1);
1925550a7375SFelipe Balbi 
192630647217STony Lindgren 	pm_runtime_mark_last_busy(musb->controller);
192730647217STony Lindgren 	pm_runtime_put_autosuspend(musb->controller);
19287acc6197SHema HK 
192963eed2b5SFelipe Balbi 	return 0;
193063eed2b5SFelipe Balbi 
1931032ec49fSFelipe Balbi err:
1932550a7375SFelipe Balbi 	return retval;
1933550a7375SFelipe Balbi }
1934550a7375SFelipe Balbi 
1935550a7375SFelipe Balbi /*
1936550a7375SFelipe Balbi  * Unregister the gadget driver. Used by gadget drivers when
1937550a7375SFelipe Balbi  * unregistering themselves from the controller.
1938550a7375SFelipe Balbi  *
1939550a7375SFelipe Balbi  * @param driver the gadget driver to unregister
1940550a7375SFelipe Balbi  */
194122835b80SFelipe Balbi static int musb_gadget_stop(struct usb_gadget *g)
1942550a7375SFelipe Balbi {
1943e71eb392SSebastian Andrzej Siewior 	struct musb	*musb = gadget_to_musb(g);
194463eed2b5SFelipe Balbi 	unsigned long	flags;
1945550a7375SFelipe Balbi 
19467acc6197SHema HK 	pm_runtime_get_sync(musb->controller);
19477acc6197SHema HK 
194863eed2b5SFelipe Balbi 	/*
194963eed2b5SFelipe Balbi 	 * REVISIT always use otg_set_peripheral() here too;
1950550a7375SFelipe Balbi 	 * this needs to shut down the OTG engine.
1951550a7375SFelipe Balbi 	 */
1952550a7375SFelipe Balbi 
1953550a7375SFelipe Balbi 	spin_lock_irqsave(&musb->lock, flags);
1954550a7375SFelipe Balbi 
1955550a7375SFelipe Balbi 	musb_hnp_stop(musb);
1956550a7375SFelipe Balbi 
1957550a7375SFelipe Balbi 	(void) musb_gadget_vbus_draw(&musb->g, 0);
1958550a7375SFelipe Balbi 
1959e47d9254SAntoine Tenart 	musb->xceiv->otg->state = OTG_STATE_UNDEFINED;
1960d5638fcfSFelipe Balbi 	musb_stop(musb);
19616e13c650SHeikki Krogerus 	otg_set_peripheral(musb->xceiv->otg, NULL);
1962550a7375SFelipe Balbi 
1963550a7375SFelipe Balbi 	musb->is_active = 0;
1964e21de10cSGrazvydas Ignotas 	musb->gadget_driver = NULL;
1965550a7375SFelipe Balbi 	musb_platform_try_idle(musb, 0);
1966550a7375SFelipe Balbi 	spin_unlock_irqrestore(&musb->lock, flags);
1967550a7375SFelipe Balbi 
1968032ec49fSFelipe Balbi 	/*
1969032ec49fSFelipe Balbi 	 * FIXME we need to be able to register another
1970550a7375SFelipe Balbi 	 * gadget driver here and have everything work;
1971550a7375SFelipe Balbi 	 * that currently misbehaves.
1972550a7375SFelipe Balbi 	 */
197363eed2b5SFelipe Balbi 
19747099dbc5STony Lindgren 	pm_runtime_mark_last_busy(musb->controller);
19757099dbc5STony Lindgren 	pm_runtime_put_autosuspend(musb->controller);
19767acc6197SHema HK 
197763eed2b5SFelipe Balbi 	return 0;
1978550a7375SFelipe Balbi }
1979550a7375SFelipe Balbi 
1980550a7375SFelipe Balbi /* ----------------------------------------------------------------------- */
1981550a7375SFelipe Balbi 
1982550a7375SFelipe Balbi /* lifecycle operations called through plat_uds.c */
1983550a7375SFelipe Balbi 
1984550a7375SFelipe Balbi void musb_g_resume(struct musb *musb)
1985550a7375SFelipe Balbi {
1986550a7375SFelipe Balbi 	musb->is_suspended = 0;
1987e47d9254SAntoine Tenart 	switch (musb->xceiv->otg->state) {
1988550a7375SFelipe Balbi 	case OTG_STATE_B_IDLE:
1989550a7375SFelipe Balbi 		break;
1990550a7375SFelipe Balbi 	case OTG_STATE_B_WAIT_ACON:
1991550a7375SFelipe Balbi 	case OTG_STATE_B_PERIPHERAL:
1992550a7375SFelipe Balbi 		musb->is_active = 1;
1993550a7375SFelipe Balbi 		if (musb->gadget_driver && musb->gadget_driver->resume) {
1994550a7375SFelipe Balbi 			spin_unlock(&musb->lock);
1995550a7375SFelipe Balbi 			musb->gadget_driver->resume(&musb->g);
1996550a7375SFelipe Balbi 			spin_lock(&musb->lock);
1997550a7375SFelipe Balbi 		}
1998550a7375SFelipe Balbi 		break;
1999550a7375SFelipe Balbi 	default:
2000550a7375SFelipe Balbi 		WARNING("unhandled RESUME transition (%s)\n",
2001e47d9254SAntoine Tenart 				usb_otg_state_string(musb->xceiv->otg->state));
2002550a7375SFelipe Balbi 	}
2003550a7375SFelipe Balbi }
2004550a7375SFelipe Balbi 
2005550a7375SFelipe Balbi /* called when SOF packets stop for 3+ msec */
2006550a7375SFelipe Balbi void musb_g_suspend(struct musb *musb)
2007550a7375SFelipe Balbi {
2008550a7375SFelipe Balbi 	u8	devctl;
2009550a7375SFelipe Balbi 
2010550a7375SFelipe Balbi 	devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
2011*b99d3659SBin Liu 	musb_dbg(musb, "musb_g_suspend: devctl %02x", devctl);
2012550a7375SFelipe Balbi 
2013e47d9254SAntoine Tenart 	switch (musb->xceiv->otg->state) {
2014550a7375SFelipe Balbi 	case OTG_STATE_B_IDLE:
2015550a7375SFelipe Balbi 		if ((devctl & MUSB_DEVCTL_VBUS) == MUSB_DEVCTL_VBUS)
2016e47d9254SAntoine Tenart 			musb->xceiv->otg->state = OTG_STATE_B_PERIPHERAL;
2017550a7375SFelipe Balbi 		break;
2018550a7375SFelipe Balbi 	case OTG_STATE_B_PERIPHERAL:
2019550a7375SFelipe Balbi 		musb->is_suspended = 1;
2020550a7375SFelipe Balbi 		if (musb->gadget_driver && musb->gadget_driver->suspend) {
2021550a7375SFelipe Balbi 			spin_unlock(&musb->lock);
2022550a7375SFelipe Balbi 			musb->gadget_driver->suspend(&musb->g);
2023550a7375SFelipe Balbi 			spin_lock(&musb->lock);
2024550a7375SFelipe Balbi 		}
2025550a7375SFelipe Balbi 		break;
2026550a7375SFelipe Balbi 	default:
2027550a7375SFelipe Balbi 		/* REVISIT if B_HOST, clear DEVCTL.HOSTREQ;
2028550a7375SFelipe Balbi 		 * A_PERIPHERAL may need care too
2029550a7375SFelipe Balbi 		 */
2030*b99d3659SBin Liu 		WARNING("unhandled SUSPEND transition (%s)",
2031e47d9254SAntoine Tenart 				usb_otg_state_string(musb->xceiv->otg->state));
2032550a7375SFelipe Balbi 	}
2033550a7375SFelipe Balbi }
2034550a7375SFelipe Balbi 
2035550a7375SFelipe Balbi /* Called during SRP */
2036550a7375SFelipe Balbi void musb_g_wakeup(struct musb *musb)
2037550a7375SFelipe Balbi {
2038550a7375SFelipe Balbi 	musb_gadget_wakeup(&musb->g);
2039550a7375SFelipe Balbi }
2040550a7375SFelipe Balbi 
2041550a7375SFelipe Balbi /* called when VBUS drops below session threshold, and in other cases */
2042550a7375SFelipe Balbi void musb_g_disconnect(struct musb *musb)
2043550a7375SFelipe Balbi {
2044550a7375SFelipe Balbi 	void __iomem	*mregs = musb->mregs;
2045550a7375SFelipe Balbi 	u8	devctl = musb_readb(mregs, MUSB_DEVCTL);
2046550a7375SFelipe Balbi 
2047*b99d3659SBin Liu 	musb_dbg(musb, "musb_g_disconnect: devctl %02x", devctl);
2048550a7375SFelipe Balbi 
2049550a7375SFelipe Balbi 	/* clear HR */
2050550a7375SFelipe Balbi 	musb_writeb(mregs, MUSB_DEVCTL, devctl & MUSB_DEVCTL_SESSION);
2051550a7375SFelipe Balbi 
2052550a7375SFelipe Balbi 	/* don't draw vbus until new b-default session */
2053550a7375SFelipe Balbi 	(void) musb_gadget_vbus_draw(&musb->g, 0);
2054550a7375SFelipe Balbi 
2055550a7375SFelipe Balbi 	musb->g.speed = USB_SPEED_UNKNOWN;
2056550a7375SFelipe Balbi 	if (musb->gadget_driver && musb->gadget_driver->disconnect) {
2057550a7375SFelipe Balbi 		spin_unlock(&musb->lock);
2058550a7375SFelipe Balbi 		musb->gadget_driver->disconnect(&musb->g);
2059550a7375SFelipe Balbi 		spin_lock(&musb->lock);
2060550a7375SFelipe Balbi 	}
2061550a7375SFelipe Balbi 
2062e47d9254SAntoine Tenart 	switch (musb->xceiv->otg->state) {
2063550a7375SFelipe Balbi 	default:
2064*b99d3659SBin Liu 		musb_dbg(musb, "Unhandled disconnect %s, setting a_idle",
2065e47d9254SAntoine Tenart 			usb_otg_state_string(musb->xceiv->otg->state));
2066e47d9254SAntoine Tenart 		musb->xceiv->otg->state = OTG_STATE_A_IDLE;
2067ab983f2aSDavid Brownell 		MUSB_HST_MODE(musb);
2068550a7375SFelipe Balbi 		break;
2069550a7375SFelipe Balbi 	case OTG_STATE_A_PERIPHERAL:
2070e47d9254SAntoine Tenart 		musb->xceiv->otg->state = OTG_STATE_A_WAIT_BCON;
2071ab983f2aSDavid Brownell 		MUSB_HST_MODE(musb);
2072550a7375SFelipe Balbi 		break;
2073550a7375SFelipe Balbi 	case OTG_STATE_B_WAIT_ACON:
2074550a7375SFelipe Balbi 	case OTG_STATE_B_HOST:
2075550a7375SFelipe Balbi 	case OTG_STATE_B_PERIPHERAL:
2076550a7375SFelipe Balbi 	case OTG_STATE_B_IDLE:
2077e47d9254SAntoine Tenart 		musb->xceiv->otg->state = OTG_STATE_B_IDLE;
2078550a7375SFelipe Balbi 		break;
2079550a7375SFelipe Balbi 	case OTG_STATE_B_SRP_INIT:
2080550a7375SFelipe Balbi 		break;
2081550a7375SFelipe Balbi 	}
2082550a7375SFelipe Balbi 
2083550a7375SFelipe Balbi 	musb->is_active = 0;
2084550a7375SFelipe Balbi }
2085550a7375SFelipe Balbi 
2086550a7375SFelipe Balbi void musb_g_reset(struct musb *musb)
2087550a7375SFelipe Balbi __releases(musb->lock)
2088550a7375SFelipe Balbi __acquires(musb->lock)
2089550a7375SFelipe Balbi {
2090550a7375SFelipe Balbi 	void __iomem	*mbase = musb->mregs;
2091550a7375SFelipe Balbi 	u8		devctl = musb_readb(mbase, MUSB_DEVCTL);
2092550a7375SFelipe Balbi 	u8		power;
2093550a7375SFelipe Balbi 
2094*b99d3659SBin Liu 	musb_dbg(musb, "<== %s driver '%s'",
2095550a7375SFelipe Balbi 			(devctl & MUSB_DEVCTL_BDEVICE)
2096550a7375SFelipe Balbi 				? "B-Device" : "A-Device",
2097550a7375SFelipe Balbi 			musb->gadget_driver
2098550a7375SFelipe Balbi 				? musb->gadget_driver->driver.name
2099550a7375SFelipe Balbi 				: NULL
2100550a7375SFelipe Balbi 			);
2101550a7375SFelipe Balbi 
21021189f7f6SFelipe Balbi 	/* report reset, if we didn't already (flushing EP state) */
21031189f7f6SFelipe Balbi 	if (musb->gadget_driver && musb->g.speed != USB_SPEED_UNKNOWN) {
21041189f7f6SFelipe Balbi 		spin_unlock(&musb->lock);
21051189f7f6SFelipe Balbi 		usb_gadget_udc_reset(&musb->g, musb->gadget_driver);
21061189f7f6SFelipe Balbi 		spin_lock(&musb->lock);
21071189f7f6SFelipe Balbi 	}
2108550a7375SFelipe Balbi 
2109550a7375SFelipe Balbi 	/* clear HR */
2110550a7375SFelipe Balbi 	else if (devctl & MUSB_DEVCTL_HR)
2111550a7375SFelipe Balbi 		musb_writeb(mbase, MUSB_DEVCTL, MUSB_DEVCTL_SESSION);
2112550a7375SFelipe Balbi 
2113550a7375SFelipe Balbi 
2114550a7375SFelipe Balbi 	/* what speed did we negotiate? */
2115550a7375SFelipe Balbi 	power = musb_readb(mbase, MUSB_POWER);
2116550a7375SFelipe Balbi 	musb->g.speed = (power & MUSB_POWER_HSMODE)
2117550a7375SFelipe Balbi 			? USB_SPEED_HIGH : USB_SPEED_FULL;
2118550a7375SFelipe Balbi 
2119550a7375SFelipe Balbi 	/* start in USB_STATE_DEFAULT */
2120550a7375SFelipe Balbi 	musb->is_active = 1;
2121550a7375SFelipe Balbi 	musb->is_suspended = 0;
2122550a7375SFelipe Balbi 	MUSB_DEV_MODE(musb);
2123550a7375SFelipe Balbi 	musb->address = 0;
2124550a7375SFelipe Balbi 	musb->ep0_state = MUSB_EP0_STAGE_SETUP;
2125550a7375SFelipe Balbi 
2126550a7375SFelipe Balbi 	musb->may_wakeup = 0;
2127550a7375SFelipe Balbi 	musb->g.b_hnp_enable = 0;
2128550a7375SFelipe Balbi 	musb->g.a_alt_hnp_support = 0;
2129550a7375SFelipe Balbi 	musb->g.a_hnp_support = 0;
2130ca1023c8SRobert Baldyga 	musb->g.quirk_zlp_not_supp = 1;
2131550a7375SFelipe Balbi 
2132550a7375SFelipe Balbi 	/* Normal reset, as B-Device;
2133550a7375SFelipe Balbi 	 * or else after HNP, as A-Device
2134550a7375SFelipe Balbi 	 */
213523db9fd2SApelete Seketeli 	if (!musb->g.is_otg) {
213623db9fd2SApelete Seketeli 		/* USB device controllers that are not OTG compatible
213723db9fd2SApelete Seketeli 		 * may not have DEVCTL register in silicon.
213823db9fd2SApelete Seketeli 		 * In that case, do not rely on devctl for setting
213923db9fd2SApelete Seketeli 		 * peripheral mode.
214023db9fd2SApelete Seketeli 		 */
2141e47d9254SAntoine Tenart 		musb->xceiv->otg->state = OTG_STATE_B_PERIPHERAL;
214223db9fd2SApelete Seketeli 		musb->g.is_a_peripheral = 0;
214323db9fd2SApelete Seketeli 	} else if (devctl & MUSB_DEVCTL_BDEVICE) {
2144e47d9254SAntoine Tenart 		musb->xceiv->otg->state = OTG_STATE_B_PERIPHERAL;
2145550a7375SFelipe Balbi 		musb->g.is_a_peripheral = 0;
2146032ec49fSFelipe Balbi 	} else {
2147e47d9254SAntoine Tenart 		musb->xceiv->otg->state = OTG_STATE_A_PERIPHERAL;
2148550a7375SFelipe Balbi 		musb->g.is_a_peripheral = 1;
2149032ec49fSFelipe Balbi 	}
2150550a7375SFelipe Balbi 
2151550a7375SFelipe Balbi 	/* start with default limits on VBUS power draw */
2152032ec49fSFelipe Balbi 	(void) musb_gadget_vbus_draw(&musb->g, 8);
2153550a7375SFelipe Balbi }
2154