blob: 83eddedcd9be5895908672b329ebf881d26d8f2f [file] [log] [blame]
Felipe Balbi550a7372008-07-24 12:27:36 +03001/*
2 * MUSB OTG driver peripheral support
3 *
4 * Copyright 2005 Mentor Graphics Corporation
5 * Copyright (C) 2005-2006 by Texas Instruments
6 * Copyright (C) 2006-2007 Nokia Corporation
Sergei Shtylyovcea83242009-11-18 22:51:18 +03007 * Copyright (C) 2009 MontaVista Software, Inc. <source@mvista.com>
Felipe Balbi550a7372008-07-24 12:27:36 +03008 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * version 2 as published by the Free Software Foundation.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
21 * 02110-1301 USA
22 *
23 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
24 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
25 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
26 * NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
29 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
30 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 *
34 */
35
36#include <linux/kernel.h>
37#include <linux/list.h>
38#include <linux/timer.h>
39#include <linux/module.h>
40#include <linux/smp.h>
41#include <linux/spinlock.h>
42#include <linux/delay.h>
Felipe Balbi550a7372008-07-24 12:27:36 +030043#include <linux/dma-mapping.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090044#include <linux/slab.h>
Felipe Balbi550a7372008-07-24 12:27:36 +030045
46#include "musb_core.h"
47
48
49/* MUSB PERIPHERAL status 3-mar-2006:
50 *
51 * - EP0 seems solid. It passes both USBCV and usbtest control cases.
52 * Minor glitches:
53 *
54 * + remote wakeup to Linux hosts work, but saw USBCV failures;
55 * in one test run (operator error?)
56 * + endpoint halt tests -- in both usbtest and usbcv -- seem
57 * to break when dma is enabled ... is something wrongly
58 * clearing SENDSTALL?
59 *
60 * - Mass storage behaved ok when last tested. Network traffic patterns
61 * (with lots of short transfers etc) need retesting; they turn up the
62 * worst cases of the DMA, since short packets are typical but are not
63 * required.
64 *
65 * - TX/IN
66 * + both pio and dma behave in with network and g_zero tests
67 * + no cppi throughput issues other than no-hw-queueing
68 * + failed with FLAT_REG (DaVinci)
69 * + seems to behave with double buffering, PIO -and- CPPI
70 * + with gadgetfs + AIO, requests got lost?
71 *
72 * - RX/OUT
73 * + both pio and dma behave in with network and g_zero tests
74 * + dma is slow in typical case (short_not_ok is clear)
75 * + double buffering ok with PIO
76 * + double buffering *FAILS* with CPPI, wrong data bytes sometimes
77 * + request lossage observed with gadgetfs
78 *
79 * - ISO not tested ... might work, but only weakly isochronous
80 *
81 * - Gadget driver disabling of softconnect during bind() is ignored; so
82 * drivers can't hold off host requests until userspace is ready.
83 * (Workaround: they can turn it off later.)
84 *
85 * - PORTABILITY (assumes PIO works):
86 * + DaVinci, basically works with cppi dma
87 * + OMAP 2430, ditto with mentor dma
88 * + TUSB 6010, platform-specific dma in the works
89 */
90
91/* ----------------------------------------------------------------------- */
92
Mian Yousaf Kaukabc65bfa62011-01-04 12:47:02 +010093#define is_buffer_mapped(req) (is_dma_capable() && \
94 (req->map_state != UN_MAPPED))
95
Hema Kalliguddi92d27112010-11-15 04:24:01 -060096/* Maps the buffer to dma */
97
98static inline void map_dma_buffer(struct musb_request *request,
Mian Yousaf Kaukabc65bfa62011-01-04 12:47:02 +010099 struct musb *musb, struct musb_ep *musb_ep)
Hema Kalliguddi92d27112010-11-15 04:24:01 -0600100{
Mian Yousaf Kaukab5f5761c2011-01-04 12:47:03 +0100101 int compatible = true;
102 struct dma_controller *dma = musb->dma_controller;
103
Mian Yousaf Kaukabc65bfa62011-01-04 12:47:02 +0100104 request->map_state = UN_MAPPED;
105
106 if (!is_dma_capable() || !musb_ep->dma)
107 return;
108
Mian Yousaf Kaukab5f5761c2011-01-04 12:47:03 +0100109 /* Check if DMA engine can handle this request.
110 * DMA code must reject the USB request explicitly.
111 * Default behaviour is to map the request.
112 */
113 if (dma->is_compatible)
114 compatible = dma->is_compatible(musb_ep->dma,
115 musb_ep->packet_sz, request->request.buf,
116 request->request.length);
117 if (!compatible)
118 return;
119
Hema Kalliguddi92d27112010-11-15 04:24:01 -0600120 if (request->request.dma == DMA_ADDR_INVALID) {
121 request->request.dma = dma_map_single(
122 musb->controller,
123 request->request.buf,
124 request->request.length,
125 request->tx
126 ? DMA_TO_DEVICE
127 : DMA_FROM_DEVICE);
Mian Yousaf Kaukabc65bfa62011-01-04 12:47:02 +0100128 request->map_state = MUSB_MAPPED;
Hema Kalliguddi92d27112010-11-15 04:24:01 -0600129 } else {
130 dma_sync_single_for_device(musb->controller,
131 request->request.dma,
132 request->request.length,
133 request->tx
134 ? DMA_TO_DEVICE
135 : DMA_FROM_DEVICE);
Mian Yousaf Kaukabc65bfa62011-01-04 12:47:02 +0100136 request->map_state = PRE_MAPPED;
Hema Kalliguddi92d27112010-11-15 04:24:01 -0600137 }
138}
139
140/* Unmap the buffer from dma and maps it back to cpu */
141static inline void unmap_dma_buffer(struct musb_request *request,
142 struct musb *musb)
143{
Kishon Vijay Abraham I06d9db72013-03-15 18:58:50 +0530144 struct musb_ep *musb_ep = request->ep;
145
146 if (!is_buffer_mapped(request) || !musb_ep->dma)
Mian Yousaf Kaukabc65bfa62011-01-04 12:47:02 +0100147 return;
148
Hema Kalliguddi92d27112010-11-15 04:24:01 -0600149 if (request->request.dma == DMA_ADDR_INVALID) {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300150 dev_vdbg(musb->controller,
151 "not unmapping a never mapped buffer\n");
Hema Kalliguddi92d27112010-11-15 04:24:01 -0600152 return;
153 }
Mian Yousaf Kaukabc65bfa62011-01-04 12:47:02 +0100154 if (request->map_state == MUSB_MAPPED) {
Hema Kalliguddi92d27112010-11-15 04:24:01 -0600155 dma_unmap_single(musb->controller,
156 request->request.dma,
157 request->request.length,
158 request->tx
159 ? DMA_TO_DEVICE
160 : DMA_FROM_DEVICE);
161 request->request.dma = DMA_ADDR_INVALID;
Mian Yousaf Kaukabc65bfa62011-01-04 12:47:02 +0100162 } else { /* PRE_MAPPED */
Hema Kalliguddi92d27112010-11-15 04:24:01 -0600163 dma_sync_single_for_cpu(musb->controller,
164 request->request.dma,
165 request->request.length,
166 request->tx
167 ? DMA_TO_DEVICE
168 : DMA_FROM_DEVICE);
Hema Kalliguddi92d27112010-11-15 04:24:01 -0600169 }
Mian Yousaf Kaukabc65bfa62011-01-04 12:47:02 +0100170 request->map_state = UN_MAPPED;
Hema Kalliguddi92d27112010-11-15 04:24:01 -0600171}
172
Felipe Balbi550a7372008-07-24 12:27:36 +0300173/*
174 * Immediately complete a request.
175 *
176 * @param request the request to complete
177 * @param status the status to complete the request with
178 * Context: controller locked, IRQs blocked.
179 */
180void musb_g_giveback(
181 struct musb_ep *ep,
182 struct usb_request *request,
183 int status)
184__releases(ep->musb->lock)
185__acquires(ep->musb->lock)
186{
187 struct musb_request *req;
188 struct musb *musb;
189 int busy = ep->busy;
190
191 req = to_musb_request(request);
192
Felipe Balbiad1adb82011-02-16 12:40:05 +0200193 list_del(&req->list);
Felipe Balbi550a7372008-07-24 12:27:36 +0300194 if (req->request.status == -EINPROGRESS)
195 req->request.status = status;
196 musb = req->musb;
197
198 ep->busy = 1;
199 spin_unlock(&musb->lock);
Kishon Vijay Abraham I06d9db72013-03-15 18:58:50 +0530200
201 if (!dma_mapping_error(&musb->g.dev, request->dma))
202 unmap_dma_buffer(req, musb);
203
Felipe Balbi550a7372008-07-24 12:27:36 +0300204 if (request->status == 0)
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300205 dev_dbg(musb->controller, "%s done request %p, %d/%d\n",
Felipe Balbi550a7372008-07-24 12:27:36 +0300206 ep->end_point.name, request,
207 req->request.actual, req->request.length);
208 else
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300209 dev_dbg(musb->controller, "%s request %p, %d/%d fault %d\n",
Felipe Balbi550a7372008-07-24 12:27:36 +0300210 ep->end_point.name, request,
211 req->request.actual, req->request.length,
212 request->status);
213 req->request.complete(&req->ep->end_point, &req->request);
214 spin_lock(&musb->lock);
215 ep->busy = busy;
216}
217
218/* ----------------------------------------------------------------------- */
219
220/*
221 * Abort requests queued to an endpoint using the status. Synchronous.
222 * caller locked controller and blocked irqs, and selected this ep.
223 */
224static void nuke(struct musb_ep *ep, const int status)
225{
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300226 struct musb *musb = ep->musb;
Felipe Balbi550a7372008-07-24 12:27:36 +0300227 struct musb_request *req = NULL;
228 void __iomem *epio = ep->musb->endpoints[ep->current_epnum].regs;
229
230 ep->busy = 1;
231
232 if (is_dma_capable() && ep->dma) {
233 struct dma_controller *c = ep->musb->dma_controller;
234 int value;
Sergei Shtylyovb6e434a2009-03-26 18:27:47 -0700235
Felipe Balbi550a7372008-07-24 12:27:36 +0300236 if (ep->is_in) {
Sergei Shtylyovb6e434a2009-03-26 18:27:47 -0700237 /*
238 * The programming guide says that we must not clear
239 * the DMAMODE bit before DMAENAB, so we only
240 * clear it in the second write...
241 */
Felipe Balbi550a7372008-07-24 12:27:36 +0300242 musb_writew(epio, MUSB_TXCSR,
Sergei Shtylyovb6e434a2009-03-26 18:27:47 -0700243 MUSB_TXCSR_DMAMODE | MUSB_TXCSR_FLUSHFIFO);
Felipe Balbi550a7372008-07-24 12:27:36 +0300244 musb_writew(epio, MUSB_TXCSR,
245 0 | MUSB_TXCSR_FLUSHFIFO);
246 } else {
247 musb_writew(epio, MUSB_RXCSR,
248 0 | MUSB_RXCSR_FLUSHFIFO);
249 musb_writew(epio, MUSB_RXCSR,
250 0 | MUSB_RXCSR_FLUSHFIFO);
251 }
252
253 value = c->channel_abort(ep->dma);
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300254 dev_dbg(musb->controller, "%s: abort DMA --> %d\n",
255 ep->name, value);
Felipe Balbi550a7372008-07-24 12:27:36 +0300256 c->channel_release(ep->dma);
257 ep->dma = NULL;
258 }
259
Felipe Balbiad1adb82011-02-16 12:40:05 +0200260 while (!list_empty(&ep->req_list)) {
261 req = list_first_entry(&ep->req_list, struct musb_request, list);
Felipe Balbi550a7372008-07-24 12:27:36 +0300262 musb_g_giveback(ep, &req->request, status);
263 }
264}
265
266/* ----------------------------------------------------------------------- */
267
268/* Data transfers - pure PIO, pure DMA, or mixed mode */
269
270/*
271 * This assumes the separate CPPI engine is responding to DMA requests
272 * from the usb core ... sequenced a bit differently from mentor dma.
273 */
274
275static inline int max_ep_writesize(struct musb *musb, struct musb_ep *ep)
276{
277 if (can_bulk_split(musb, ep->type))
278 return ep->hw_ep->max_packet_sz_tx;
279 else
280 return ep->packet_sz;
281}
282
283
284#ifdef CONFIG_USB_INVENTRA_DMA
285
286/* Peripheral tx (IN) using Mentor DMA works as follows:
287 Only mode 0 is used for transfers <= wPktSize,
288 mode 1 is used for larger transfers,
289
290 One of the following happens:
291 - Host sends IN token which causes an endpoint interrupt
292 -> TxAvail
293 -> if DMA is currently busy, exit.
294 -> if queue is non-empty, txstate().
295
296 - Request is queued by the gadget driver.
297 -> if queue was previously empty, txstate()
298
299 txstate()
300 -> start
301 /\ -> setup DMA
302 | (data is transferred to the FIFO, then sent out when
303 | IN token(s) are recd from Host.
304 | -> DMA interrupt on completion
305 | calls TxAvail.
Sergei Shtylyovb6e434a2009-03-26 18:27:47 -0700306 | -> stop DMA, ~DMAENAB,
Felipe Balbi550a7372008-07-24 12:27:36 +0300307 | -> set TxPktRdy for last short pkt or zlp
308 | -> Complete Request
309 | -> Continue next request (call txstate)
310 |___________________________________|
311
312 * Non-Mentor DMA engines can of course work differently, such as by
313 * upleveling from irq-per-packet to irq-per-buffer.
314 */
315
316#endif
317
318/*
319 * An endpoint is transmitting data. This can be called either from
320 * the IRQ routine or from ep.queue() to kickstart a request on an
321 * endpoint.
322 *
323 * Context: controller locked, IRQs blocked, endpoint selected
324 */
325static void txstate(struct musb *musb, struct musb_request *req)
326{
327 u8 epnum = req->epnum;
328 struct musb_ep *musb_ep;
329 void __iomem *epio = musb->endpoints[epnum].regs;
330 struct usb_request *request;
331 u16 fifo_count = 0, csr;
332 int use_dma = 0;
333
334 musb_ep = req->ep;
335
Vikram Panditaabf710e2012-05-18 13:48:04 -0700336 /* Check if EP is disabled */
337 if (!musb_ep->desc) {
338 dev_dbg(musb->controller, "ep:%s disabled - ignore request\n",
339 musb_ep->end_point.name);
340 return;
341 }
342
Felipe Balbi550a7372008-07-24 12:27:36 +0300343 /* we shouldn't get here while DMA is active ... but we do ... */
344 if (dma_channel_status(musb_ep->dma) == MUSB_DMA_STATUS_BUSY) {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300345 dev_dbg(musb->controller, "dma pending...\n");
Felipe Balbi550a7372008-07-24 12:27:36 +0300346 return;
347 }
348
349 /* read TXCSR before */
350 csr = musb_readw(epio, MUSB_TXCSR);
351
352 request = &req->request;
353 fifo_count = min(max_ep_writesize(musb, musb_ep),
354 (int)(request->length - request->actual));
355
356 if (csr & MUSB_TXCSR_TXPKTRDY) {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300357 dev_dbg(musb->controller, "%s old packet still ready , txcsr %03x\n",
Felipe Balbi550a7372008-07-24 12:27:36 +0300358 musb_ep->end_point.name, csr);
359 return;
360 }
361
362 if (csr & MUSB_TXCSR_P_SENDSTALL) {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300363 dev_dbg(musb->controller, "%s stalling, txcsr %03x\n",
Felipe Balbi550a7372008-07-24 12:27:36 +0300364 musb_ep->end_point.name, csr);
365 return;
366 }
367
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300368 dev_dbg(musb->controller, "hw_ep%d, maxpacket %d, fifo count %d, txcsr %03x\n",
Felipe Balbi550a7372008-07-24 12:27:36 +0300369 epnum, musb_ep->packet_sz, fifo_count,
370 csr);
371
372#ifndef CONFIG_MUSB_PIO_ONLY
Mian Yousaf Kaukabc65bfa62011-01-04 12:47:02 +0100373 if (is_buffer_mapped(req)) {
Felipe Balbi550a7372008-07-24 12:27:36 +0300374 struct dma_controller *c = musb->dma_controller;
Ming Lei66af83d2010-09-20 10:32:06 +0300375 size_t request_size;
376
377 /* setup DMA, then program endpoint CSR */
378 request_size = min_t(size_t, request->length - request->actual,
379 musb_ep->dma->max_len);
Felipe Balbi550a7372008-07-24 12:27:36 +0300380
Ajay Kumar Guptad17d5352012-07-20 11:07:23 +0530381 use_dma = (request->dma != DMA_ADDR_INVALID && request_size);
Felipe Balbi550a7372008-07-24 12:27:36 +0300382
383 /* MUSB_TXCSR_P_ISO is still set correctly */
384
Mian Yousaf Kaukaba48ff902011-03-22 15:55:56 +0100385#if defined(CONFIG_USB_INVENTRA_DMA) || defined(CONFIG_USB_UX500_DMA)
Felipe Balbi550a7372008-07-24 12:27:36 +0300386 {
Anand Gadiyard1043a22009-04-02 12:07:08 -0700387 if (request_size < musb_ep->packet_sz)
Felipe Balbi550a7372008-07-24 12:27:36 +0300388 musb_ep->dma->desired_mode = 0;
389 else
390 musb_ep->dma->desired_mode = 1;
391
392 use_dma = use_dma && c->channel_program(
393 musb_ep->dma, musb_ep->packet_sz,
394 musb_ep->dma->desired_mode,
Cliff Cai796a83f2009-12-21 21:18:02 -0500395 request->dma + request->actual, request_size);
Felipe Balbi550a7372008-07-24 12:27:36 +0300396 if (use_dma) {
397 if (musb_ep->dma->desired_mode == 0) {
Sergei Shtylyovb6e434a2009-03-26 18:27:47 -0700398 /*
399 * We must not clear the DMAMODE bit
400 * before the DMAENAB bit -- and the
401 * latter doesn't always get cleared
402 * before we get here...
403 */
404 csr &= ~(MUSB_TXCSR_AUTOSET
405 | MUSB_TXCSR_DMAENAB);
406 musb_writew(epio, MUSB_TXCSR, csr
407 | MUSB_TXCSR_P_WZC_BITS);
408 csr &= ~MUSB_TXCSR_DMAMODE;
Felipe Balbi550a7372008-07-24 12:27:36 +0300409 csr |= (MUSB_TXCSR_DMAENAB |
410 MUSB_TXCSR_MODE);
411 /* against programming guide */
Ming Leif11d8932010-09-24 13:44:04 +0300412 } else {
413 csr |= (MUSB_TXCSR_DMAENAB
Felipe Balbi550a7372008-07-24 12:27:36 +0300414 | MUSB_TXCSR_DMAMODE
415 | MUSB_TXCSR_MODE);
supriya karanthbb3a2ef2012-12-06 11:12:48 +0530416 /*
417 * Enable Autoset according to table
418 * below
419 * bulk_split hb_mult Autoset_Enable
420 * 0 0 Yes(Normal)
421 * 0 >0 No(High BW ISO)
422 * 1 0 Yes(HS bulk)
423 * 1 >0 Yes(FS bulk)
424 */
425 if (!musb_ep->hb_mult ||
426 (musb_ep->hb_mult &&
427 can_bulk_split(musb,
428 musb_ep->type)))
Ming Leif11d8932010-09-24 13:44:04 +0300429 csr |= MUSB_TXCSR_AUTOSET;
430 }
Felipe Balbi550a7372008-07-24 12:27:36 +0300431 csr &= ~MUSB_TXCSR_P_UNDERRUN;
Ming Leif11d8932010-09-24 13:44:04 +0300432
Felipe Balbi550a7372008-07-24 12:27:36 +0300433 musb_writew(epio, MUSB_TXCSR, csr);
434 }
435 }
436
437#elif defined(CONFIG_USB_TI_CPPI_DMA)
438 /* program endpoint CSR first, then setup DMA */
Sergei Shtylyovb6e434a2009-03-26 18:27:47 -0700439 csr &= ~(MUSB_TXCSR_P_UNDERRUN | MUSB_TXCSR_TXPKTRDY);
Sergei Shtylyov37e3ee92009-03-27 12:53:32 -0700440 csr |= MUSB_TXCSR_DMAENAB | MUSB_TXCSR_DMAMODE |
441 MUSB_TXCSR_MODE;
Felipe Balbi550a7372008-07-24 12:27:36 +0300442 musb_writew(epio, MUSB_TXCSR,
443 (MUSB_TXCSR_P_WZC_BITS & ~MUSB_TXCSR_P_UNDERRUN)
444 | csr);
445
446 /* ensure writebuffer is empty */
447 csr = musb_readw(epio, MUSB_TXCSR);
448
449 /* NOTE host side sets DMAENAB later than this; both are
450 * OK since the transfer dma glue (between CPPI and Mentor
451 * fifos) just tells CPPI it could start. Data only moves
452 * to the USB TX fifo when both fifos are ready.
453 */
454
455 /* "mode" is irrelevant here; handle terminating ZLPs like
456 * PIO does, since the hardware RNDIS mode seems unreliable
457 * except for the last-packet-is-already-short case.
458 */
459 use_dma = use_dma && c->channel_program(
460 musb_ep->dma, musb_ep->packet_sz,
461 0,
Ming Lei66af83d2010-09-20 10:32:06 +0300462 request->dma + request->actual,
463 request_size);
Felipe Balbi550a7372008-07-24 12:27:36 +0300464 if (!use_dma) {
465 c->channel_release(musb_ep->dma);
466 musb_ep->dma = NULL;
Sergei Shtylyovb6e434a2009-03-26 18:27:47 -0700467 csr &= ~MUSB_TXCSR_DMAENAB;
468 musb_writew(epio, MUSB_TXCSR, csr);
Felipe Balbi550a7372008-07-24 12:27:36 +0300469 /* invariant: prequest->buf is non-null */
470 }
471#elif defined(CONFIG_USB_TUSB_OMAP_DMA)
472 use_dma = use_dma && c->channel_program(
473 musb_ep->dma, musb_ep->packet_sz,
474 request->zero,
Ming Lei66af83d2010-09-20 10:32:06 +0300475 request->dma + request->actual,
476 request_size);
Felipe Balbi550a7372008-07-24 12:27:36 +0300477#endif
478 }
479#endif
480
481 if (!use_dma) {
Hema Kalliguddi92d27112010-11-15 04:24:01 -0600482 /*
483 * Unmap the dma buffer back to cpu if dma channel
484 * programming fails
485 */
Mian Yousaf Kaukabc65bfa62011-01-04 12:47:02 +0100486 unmap_dma_buffer(req, musb);
Hema Kalliguddi92d27112010-11-15 04:24:01 -0600487
Felipe Balbi550a7372008-07-24 12:27:36 +0300488 musb_write_fifo(musb_ep->hw_ep, fifo_count,
489 (u8 *) (request->buf + request->actual));
490 request->actual += fifo_count;
491 csr |= MUSB_TXCSR_TXPKTRDY;
492 csr &= ~MUSB_TXCSR_P_UNDERRUN;
493 musb_writew(epio, MUSB_TXCSR, csr);
494 }
495
496 /* host may already have the data when this message shows... */
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300497 dev_dbg(musb->controller, "%s TX/IN %s len %d/%d, txcsr %04x, fifo %d/%d\n",
Felipe Balbi550a7372008-07-24 12:27:36 +0300498 musb_ep->end_point.name, use_dma ? "dma" : "pio",
499 request->actual, request->length,
500 musb_readw(epio, MUSB_TXCSR),
501 fifo_count,
502 musb_readw(epio, MUSB_TXMAXP));
503}
504
505/*
506 * FIFO state update (e.g. data ready).
507 * Called from IRQ, with controller locked.
508 */
509void musb_g_tx(struct musb *musb, u8 epnum)
510{
511 u16 csr;
Felipe Balbiad1adb82011-02-16 12:40:05 +0200512 struct musb_request *req;
Felipe Balbi550a7372008-07-24 12:27:36 +0300513 struct usb_request *request;
514 u8 __iomem *mbase = musb->mregs;
515 struct musb_ep *musb_ep = &musb->endpoints[epnum].ep_in;
516 void __iomem *epio = musb->endpoints[epnum].regs;
517 struct dma_channel *dma;
518
519 musb_ep_select(mbase, epnum);
Felipe Balbiad1adb82011-02-16 12:40:05 +0200520 req = next_request(musb_ep);
521 request = &req->request;
Felipe Balbi550a7372008-07-24 12:27:36 +0300522
523 csr = musb_readw(epio, MUSB_TXCSR);
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300524 dev_dbg(musb->controller, "<== %s, txcsr %04x\n", musb_ep->end_point.name, csr);
Felipe Balbi550a7372008-07-24 12:27:36 +0300525
526 dma = is_dma_capable() ? musb_ep->dma : NULL;
Sergei Shtylyov7723de72009-11-18 22:55:28 +0300527
528 /*
529 * REVISIT: for high bandwidth, MUSB_TXCSR_P_INCOMPTX
530 * probably rates reporting as a host error.
531 */
532 if (csr & MUSB_TXCSR_P_SENTSTALL) {
533 csr |= MUSB_TXCSR_P_WZC_BITS;
534 csr &= ~MUSB_TXCSR_P_SENTSTALL;
535 musb_writew(epio, MUSB_TXCSR, csr);
536 return;
537 }
538
539 if (csr & MUSB_TXCSR_P_UNDERRUN) {
540 /* We NAKed, no big deal... little reason to care. */
541 csr |= MUSB_TXCSR_P_WZC_BITS;
542 csr &= ~(MUSB_TXCSR_P_UNDERRUN | MUSB_TXCSR_TXPKTRDY);
543 musb_writew(epio, MUSB_TXCSR, csr);
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300544 dev_vdbg(musb->controller, "underrun on ep%d, req %p\n",
545 epnum, request);
Sergei Shtylyov7723de72009-11-18 22:55:28 +0300546 }
547
548 if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) {
549 /*
550 * SHOULD NOT HAPPEN... has with CPPI though, after
551 * changing SENDSTALL (and other cases); harmless?
Felipe Balbi550a7372008-07-24 12:27:36 +0300552 */
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300553 dev_dbg(musb->controller, "%s dma still busy?\n", musb_ep->end_point.name);
Sergei Shtylyov7723de72009-11-18 22:55:28 +0300554 return;
555 }
556
557 if (request) {
558 u8 is_dma = 0;
559
560 if (dma && (csr & MUSB_TXCSR_DMAENAB)) {
561 is_dma = 1;
Felipe Balbi550a7372008-07-24 12:27:36 +0300562 csr |= MUSB_TXCSR_P_WZC_BITS;
Sergei Shtylyov7723de72009-11-18 22:55:28 +0300563 csr &= ~(MUSB_TXCSR_DMAENAB | MUSB_TXCSR_P_UNDERRUN |
Mian Yousaf Kaukab100d4a92011-03-15 16:24:24 +0100564 MUSB_TXCSR_TXPKTRDY | MUSB_TXCSR_AUTOSET);
Felipe Balbi550a7372008-07-24 12:27:36 +0300565 musb_writew(epio, MUSB_TXCSR, csr);
Sergei Shtylyov7723de72009-11-18 22:55:28 +0300566 /* Ensure writebuffer is empty. */
567 csr = musb_readw(epio, MUSB_TXCSR);
568 request->actual += musb_ep->dma->actual_len;
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300569 dev_dbg(musb->controller, "TXCSR%d %04x, DMA off, len %zu, req %p\n",
Sergei Shtylyov7723de72009-11-18 22:55:28 +0300570 epnum, csr, musb_ep->dma->actual_len, request);
Felipe Balbi550a7372008-07-24 12:27:36 +0300571 }
572
Ming Leie7379aa2010-09-24 13:44:14 +0300573 /*
574 * First, maybe a terminating short packet. Some DMA
575 * engines might handle this by themselves.
576 */
577 if ((request->zero && request->length
578 && (request->length % musb_ep->packet_sz == 0)
579 && (request->actual == request->length))
Mian Yousaf Kaukaba48ff902011-03-22 15:55:56 +0100580#if defined(CONFIG_USB_INVENTRA_DMA) || defined(CONFIG_USB_UX500_DMA)
Ming Leie7379aa2010-09-24 13:44:14 +0300581 || (is_dma && (!dma->desired_mode ||
582 (request->actual &
583 (musb_ep->packet_sz - 1))))
Felipe Balbi550a7372008-07-24 12:27:36 +0300584#endif
Ming Leie7379aa2010-09-24 13:44:14 +0300585 ) {
586 /*
587 * On DMA completion, FIFO may not be
588 * available yet...
589 */
590 if (csr & MUSB_TXCSR_TXPKTRDY)
591 return;
Sergei Shtylyov7723de72009-11-18 22:55:28 +0300592
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300593 dev_dbg(musb->controller, "sending zero pkt\n");
Ming Leie7379aa2010-09-24 13:44:14 +0300594 musb_writew(epio, MUSB_TXCSR, MUSB_TXCSR_MODE
595 | MUSB_TXCSR_TXPKTRDY);
596 request->zero = 0;
597 }
Felipe Balbi550a7372008-07-24 12:27:36 +0300598
Ming Leie7379aa2010-09-24 13:44:14 +0300599 if (request->actual == request->length) {
600 musb_g_giveback(musb_ep, request, 0);
Supriya Karanth39287072012-02-17 14:54:52 +0530601 /*
602 * In the giveback function the MUSB lock is
603 * released and acquired after sometime. During
604 * this time period the INDEX register could get
605 * changed by the gadget_queue function especially
606 * on SMP systems. Reselect the INDEX to be sure
607 * we are reading/modifying the right registers
608 */
609 musb_ep_select(mbase, epnum);
Felipe Balbiad1adb82011-02-16 12:40:05 +0200610 req = musb_ep->desc ? next_request(musb_ep) : NULL;
611 if (!req) {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300612 dev_dbg(musb->controller, "%s idle now\n",
Ming Leie7379aa2010-09-24 13:44:14 +0300613 musb_ep->end_point.name);
614 return;
Sergei Shtylyov95962a72009-12-16 20:38:31 +0300615 }
Felipe Balbi550a7372008-07-24 12:27:36 +0300616 }
617
Felipe Balbiad1adb82011-02-16 12:40:05 +0200618 txstate(musb, req);
Sergei Shtylyov7723de72009-11-18 22:55:28 +0300619 }
Felipe Balbi550a7372008-07-24 12:27:36 +0300620}
621
622/* ------------------------------------------------------------ */
623
624#ifdef CONFIG_USB_INVENTRA_DMA
625
626/* Peripheral rx (OUT) using Mentor DMA works as follows:
627 - Only mode 0 is used.
628
629 - Request is queued by the gadget class driver.
630 -> if queue was previously empty, rxstate()
631
632 - Host sends OUT token which causes an endpoint interrupt
633 /\ -> RxReady
634 | -> if request queued, call rxstate
635 | /\ -> setup DMA
636 | | -> DMA interrupt on completion
637 | | -> RxReady
638 | | -> stop DMA
639 | | -> ack the read
640 | | -> if data recd = max expected
641 | | by the request, or host
642 | | sent a short packet,
643 | | complete the request,
644 | | and start the next one.
645 | |_____________________________________|
646 | else just wait for the host
647 | to send the next OUT token.
648 |__________________________________________________|
649
650 * Non-Mentor DMA engines can of course work differently.
651 */
652
653#endif
654
655/*
656 * Context: controller locked, IRQs blocked, endpoint selected
657 */
658static void rxstate(struct musb *musb, struct musb_request *req)
659{
Felipe Balbi550a7372008-07-24 12:27:36 +0300660 const u8 epnum = req->epnum;
661 struct usb_request *request = &req->request;
Ming Leibd2e74d2010-09-20 10:32:01 +0300662 struct musb_ep *musb_ep;
Felipe Balbi550a7372008-07-24 12:27:36 +0300663 void __iomem *epio = musb->endpoints[epnum].regs;
Sergei Shtylyovf0443af2012-07-16 23:25:04 +0400664 unsigned len = 0;
665 u16 fifo_count;
Sergei Shtylyovcea83242009-11-18 22:51:18 +0300666 u16 csr = musb_readw(epio, MUSB_RXCSR);
Ming Leibd2e74d2010-09-20 10:32:01 +0300667 struct musb_hw_ep *hw_ep = &musb->endpoints[epnum];
Anand Gadiyar0ae52d52011-07-19 22:11:58 -0700668 u8 use_mode_1;
Ming Leibd2e74d2010-09-20 10:32:01 +0300669
670 if (hw_ep->is_shared_fifo)
671 musb_ep = &hw_ep->ep_in;
672 else
673 musb_ep = &hw_ep->ep_out;
674
Sergei Shtylyovf0443af2012-07-16 23:25:04 +0400675 fifo_count = musb_ep->packet_sz;
Felipe Balbi550a7372008-07-24 12:27:36 +0300676
Vikram Panditaabf710e2012-05-18 13:48:04 -0700677 /* Check if EP is disabled */
678 if (!musb_ep->desc) {
679 dev_dbg(musb->controller, "ep:%s disabled - ignore request\n",
680 musb_ep->end_point.name);
681 return;
682 }
683
Sergei Shtylyovcea83242009-11-18 22:51:18 +0300684 /* We shouldn't get here while DMA is active, but we do... */
685 if (dma_channel_status(musb_ep->dma) == MUSB_DMA_STATUS_BUSY) {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300686 dev_dbg(musb->controller, "DMA pending...\n");
Sergei Shtylyovcea83242009-11-18 22:51:18 +0300687 return;
688 }
689
690 if (csr & MUSB_RXCSR_P_SENDSTALL) {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300691 dev_dbg(musb->controller, "%s stalling, RXCSR %04x\n",
Sergei Shtylyovcea83242009-11-18 22:51:18 +0300692 musb_ep->end_point.name, csr);
693 return;
694 }
Felipe Balbi550a7372008-07-24 12:27:36 +0300695
Mian Yousaf Kaukabc65bfa62011-01-04 12:47:02 +0100696 if (is_cppi_enabled() && is_buffer_mapped(req)) {
Felipe Balbi550a7372008-07-24 12:27:36 +0300697 struct dma_controller *c = musb->dma_controller;
698 struct dma_channel *channel = musb_ep->dma;
699
700 /* NOTE: CPPI won't actually stop advancing the DMA
701 * queue after short packet transfers, so this is almost
702 * always going to run as IRQ-per-packet DMA so that
703 * faults will be handled correctly.
704 */
705 if (c->channel_program(channel,
706 musb_ep->packet_sz,
707 !request->short_not_ok,
708 request->dma + request->actual,
709 request->length - request->actual)) {
710
711 /* make sure that if an rxpkt arrived after the irq,
712 * the cppi engine will be ready to take it as soon
713 * as DMA is enabled
714 */
715 csr &= ~(MUSB_RXCSR_AUTOCLEAR
716 | MUSB_RXCSR_DMAMODE);
717 csr |= MUSB_RXCSR_DMAENAB | MUSB_RXCSR_P_WZC_BITS;
718 musb_writew(epio, MUSB_RXCSR, csr);
719 return;
720 }
721 }
722
723 if (csr & MUSB_RXCSR_RXPKTRDY) {
Sergei Shtylyovf0443af2012-07-16 23:25:04 +0400724 fifo_count = musb_readw(epio, MUSB_RXCOUNT);
Anand Gadiyar0ae52d52011-07-19 22:11:58 -0700725
726 /*
Felipe Balbi00a89182012-10-26 09:55:31 +0300727 * Enable Mode 1 on RX transfers only when short_not_ok flag
728 * is set. Currently short_not_ok flag is set only from
729 * file_storage and f_mass_storage drivers
Anand Gadiyar0ae52d52011-07-19 22:11:58 -0700730 */
Felipe Balbi00a89182012-10-26 09:55:31 +0300731
732 if (request->short_not_ok && fifo_count == musb_ep->packet_sz)
Anand Gadiyar0ae52d52011-07-19 22:11:58 -0700733 use_mode_1 = 1;
734 else
735 use_mode_1 = 0;
736
Felipe Balbi550a7372008-07-24 12:27:36 +0300737 if (request->actual < request->length) {
738#ifdef CONFIG_USB_INVENTRA_DMA
Mian Yousaf Kaukabc65bfa62011-01-04 12:47:02 +0100739 if (is_buffer_mapped(req)) {
Felipe Balbi550a7372008-07-24 12:27:36 +0300740 struct dma_controller *c;
741 struct dma_channel *channel;
742 int use_dma = 0;
Roger Quadros660fa882012-08-07 16:26:32 +0300743 int transfer_size;
Felipe Balbi550a7372008-07-24 12:27:36 +0300744
745 c = musb->dma_controller;
746 channel = musb_ep->dma;
747
Felipe Balbi00a89182012-10-26 09:55:31 +0300748 /* We use DMA Req mode 0 in rx_csr, and DMA controller operates in
749 * mode 0 only. So we do not get endpoint interrupts due to DMA
750 * completion. We only get interrupts from DMA controller.
751 *
752 * We could operate in DMA mode 1 if we knew the size of the tranfer
753 * in advance. For mass storage class, request->length = what the host
754 * sends, so that'd work. But for pretty much everything else,
755 * request->length is routinely more than what the host sends. For
756 * most these gadgets, end of is signified either by a short packet,
757 * or filling the last byte of the buffer. (Sending extra data in
758 * that last pckate should trigger an overflow fault.) But in mode 1,
759 * we don't get DMA completion interrupt for short packets.
760 *
761 * Theoretically, we could enable DMAReq irq (MUSB_RXCSR_DMAMODE = 1),
762 * to get endpoint interrupt on every DMA req, but that didn't seem
763 * to work reliably.
764 *
765 * REVISIT an updated g_file_storage can set req->short_not_ok, which
766 * then becomes usable as a runtime "use mode 1" hint...
767 */
768
Anand Gadiyar0ae52d52011-07-19 22:11:58 -0700769 /* Experimental: Mode1 works with mass storage use cases */
770 if (use_mode_1) {
Ming Lei9001d802010-09-25 05:50:43 -0500771 csr |= MUSB_RXCSR_AUTOCLEAR;
Anand Gadiyar0ae52d52011-07-19 22:11:58 -0700772 musb_writew(epio, MUSB_RXCSR, csr);
773 csr |= MUSB_RXCSR_DMAENAB;
774 musb_writew(epio, MUSB_RXCSR, csr);
775
776 /*
777 * this special sequence (enabling and then
778 * disabling MUSB_RXCSR_DMAMODE) is required
779 * to get DMAReq to activate
780 */
781 musb_writew(epio, MUSB_RXCSR,
782 csr | MUSB_RXCSR_DMAMODE);
783 musb_writew(epio, MUSB_RXCSR, csr);
784
Roger Quadros660fa882012-08-07 16:26:32 +0300785 transfer_size = min(request->length - request->actual,
786 channel->max_len);
787 musb_ep->dma->desired_mode = 1;
788
Anand Gadiyar0ae52d52011-07-19 22:11:58 -0700789 } else {
790 if (!musb_ep->hb_mult &&
791 musb_ep->hw_ep->rx_double_buffered)
792 csr |= MUSB_RXCSR_AUTOCLEAR;
793 csr |= MUSB_RXCSR_DMAENAB;
794 musb_writew(epio, MUSB_RXCSR, csr);
Felipe Balbi550a7372008-07-24 12:27:36 +0300795
Roger Quadros660fa882012-08-07 16:26:32 +0300796 transfer_size = min(request->length - request->actual,
Sergei Shtylyovf0443af2012-07-16 23:25:04 +0400797 (unsigned)fifo_count);
Roger Quadros660fa882012-08-07 16:26:32 +0300798 musb_ep->dma->desired_mode = 0;
Felipe Balbi550a7372008-07-24 12:27:36 +0300799 }
800
Roger Quadros660fa882012-08-07 16:26:32 +0300801 use_dma = c->channel_program(
802 channel,
803 musb_ep->packet_sz,
804 channel->desired_mode,
805 request->dma
806 + request->actual,
807 transfer_size);
808
Felipe Balbi550a7372008-07-24 12:27:36 +0300809 if (use_dma)
810 return;
811 }
Mian Yousaf Kaukaba48ff902011-03-22 15:55:56 +0100812#elif defined(CONFIG_USB_UX500_DMA)
813 if ((is_buffer_mapped(req)) &&
814 (request->actual < request->length)) {
815
816 struct dma_controller *c;
817 struct dma_channel *channel;
818 int transfer_size = 0;
819
820 c = musb->dma_controller;
821 channel = musb_ep->dma;
822
823 /* In case first packet is short */
Sergei Shtylyovf0443af2012-07-16 23:25:04 +0400824 if (fifo_count < musb_ep->packet_sz)
825 transfer_size = fifo_count;
Mian Yousaf Kaukaba48ff902011-03-22 15:55:56 +0100826 else if (request->short_not_ok)
827 transfer_size = min(request->length -
828 request->actual,
829 channel->max_len);
830 else
831 transfer_size = min(request->length -
832 request->actual,
Sergei Shtylyovf0443af2012-07-16 23:25:04 +0400833 (unsigned)fifo_count);
Mian Yousaf Kaukaba48ff902011-03-22 15:55:56 +0100834
835 csr &= ~MUSB_RXCSR_DMAMODE;
836 csr |= (MUSB_RXCSR_DMAENAB |
837 MUSB_RXCSR_AUTOCLEAR);
838
839 musb_writew(epio, MUSB_RXCSR, csr);
840
841 if (transfer_size <= musb_ep->packet_sz) {
842 musb_ep->dma->desired_mode = 0;
843 } else {
844 musb_ep->dma->desired_mode = 1;
845 /* Mode must be set after DMAENAB */
846 csr |= MUSB_RXCSR_DMAMODE;
847 musb_writew(epio, MUSB_RXCSR, csr);
848 }
849
850 if (c->channel_program(channel,
851 musb_ep->packet_sz,
852 channel->desired_mode,
853 request->dma
854 + request->actual,
855 transfer_size))
856
857 return;
858 }
Felipe Balbi550a7372008-07-24 12:27:36 +0300859#endif /* Mentor's DMA */
860
Sergei Shtylyovf0443af2012-07-16 23:25:04 +0400861 len = request->length - request->actual;
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300862 dev_dbg(musb->controller, "%s OUT/RX pio fifo %d/%d, maxpacket %d\n",
Felipe Balbi550a7372008-07-24 12:27:36 +0300863 musb_ep->end_point.name,
Sergei Shtylyovf0443af2012-07-16 23:25:04 +0400864 fifo_count, len,
Felipe Balbi550a7372008-07-24 12:27:36 +0300865 musb_ep->packet_sz);
866
Felipe Balbic2c96322009-02-21 15:29:42 -0800867 fifo_count = min_t(unsigned, len, fifo_count);
Felipe Balbi550a7372008-07-24 12:27:36 +0300868
869#ifdef CONFIG_USB_TUSB_OMAP_DMA
Mian Yousaf Kaukabc65bfa62011-01-04 12:47:02 +0100870 if (tusb_dma_omap() && is_buffer_mapped(req)) {
Felipe Balbi550a7372008-07-24 12:27:36 +0300871 struct dma_controller *c = musb->dma_controller;
872 struct dma_channel *channel = musb_ep->dma;
873 u32 dma_addr = request->dma + request->actual;
874 int ret;
875
876 ret = c->channel_program(channel,
877 musb_ep->packet_sz,
878 channel->desired_mode,
879 dma_addr,
880 fifo_count);
881 if (ret)
882 return;
883 }
884#endif
Hema Kalliguddi92d27112010-11-15 04:24:01 -0600885 /*
886 * Unmap the dma buffer back to cpu if dma channel
887 * programming fails. This buffer is mapped if the
888 * channel allocation is successful
889 */
Mian Yousaf Kaukabc65bfa62011-01-04 12:47:02 +0100890 if (is_buffer_mapped(req)) {
Hema Kalliguddi92d27112010-11-15 04:24:01 -0600891 unmap_dma_buffer(req, musb);
892
Ming Leie75df372010-11-16 23:37:37 +0800893 /*
894 * Clear DMAENAB and AUTOCLEAR for the
Hema Kalliguddi92d27112010-11-15 04:24:01 -0600895 * PIO mode transfer
896 */
Ming Leie75df372010-11-16 23:37:37 +0800897 csr &= ~(MUSB_RXCSR_DMAENAB | MUSB_RXCSR_AUTOCLEAR);
Hema Kalliguddi92d27112010-11-15 04:24:01 -0600898 musb_writew(epio, MUSB_RXCSR, csr);
899 }
Felipe Balbi550a7372008-07-24 12:27:36 +0300900
901 musb_read_fifo(musb_ep->hw_ep, fifo_count, (u8 *)
902 (request->buf + request->actual));
903 request->actual += fifo_count;
904
905 /* REVISIT if we left anything in the fifo, flush
906 * it and report -EOVERFLOW
907 */
908
909 /* ack the read! */
910 csr |= MUSB_RXCSR_P_WZC_BITS;
911 csr &= ~MUSB_RXCSR_RXPKTRDY;
912 musb_writew(epio, MUSB_RXCSR, csr);
913 }
914 }
915
916 /* reach the end or short packet detected */
Sergei Shtylyovf0443af2012-07-16 23:25:04 +0400917 if (request->actual == request->length ||
918 fifo_count < musb_ep->packet_sz)
Felipe Balbi550a7372008-07-24 12:27:36 +0300919 musb_g_giveback(musb_ep, request, 0);
920}
921
922/*
923 * Data ready for a request; called from IRQ
924 */
925void musb_g_rx(struct musb *musb, u8 epnum)
926{
927 u16 csr;
Felipe Balbiad1adb82011-02-16 12:40:05 +0200928 struct musb_request *req;
Felipe Balbi550a7372008-07-24 12:27:36 +0300929 struct usb_request *request;
930 void __iomem *mbase = musb->mregs;
Ming Leibd2e74d2010-09-20 10:32:01 +0300931 struct musb_ep *musb_ep;
Felipe Balbi550a7372008-07-24 12:27:36 +0300932 void __iomem *epio = musb->endpoints[epnum].regs;
933 struct dma_channel *dma;
Ming Leibd2e74d2010-09-20 10:32:01 +0300934 struct musb_hw_ep *hw_ep = &musb->endpoints[epnum];
935
936 if (hw_ep->is_shared_fifo)
937 musb_ep = &hw_ep->ep_in;
938 else
939 musb_ep = &hw_ep->ep_out;
Felipe Balbi550a7372008-07-24 12:27:36 +0300940
941 musb_ep_select(mbase, epnum);
942
Felipe Balbiad1adb82011-02-16 12:40:05 +0200943 req = next_request(musb_ep);
944 if (!req)
Maulik Mankad0abdc362009-12-22 16:18:19 +0530945 return;
Felipe Balbi550a7372008-07-24 12:27:36 +0300946
Felipe Balbiad1adb82011-02-16 12:40:05 +0200947 request = &req->request;
948
Felipe Balbi550a7372008-07-24 12:27:36 +0300949 csr = musb_readw(epio, MUSB_RXCSR);
950 dma = is_dma_capable() ? musb_ep->dma : NULL;
951
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300952 dev_dbg(musb->controller, "<== %s, rxcsr %04x%s %p\n", musb_ep->end_point.name,
Felipe Balbi550a7372008-07-24 12:27:36 +0300953 csr, dma ? " (dma)" : "", request);
954
955 if (csr & MUSB_RXCSR_P_SENTSTALL) {
Felipe Balbi550a7372008-07-24 12:27:36 +0300956 csr |= MUSB_RXCSR_P_WZC_BITS;
957 csr &= ~MUSB_RXCSR_P_SENTSTALL;
958 musb_writew(epio, MUSB_RXCSR, csr);
Sergei Shtylyovcea83242009-11-18 22:51:18 +0300959 return;
Felipe Balbi550a7372008-07-24 12:27:36 +0300960 }
961
962 if (csr & MUSB_RXCSR_P_OVERRUN) {
963 /* csr |= MUSB_RXCSR_P_WZC_BITS; */
964 csr &= ~MUSB_RXCSR_P_OVERRUN;
965 musb_writew(epio, MUSB_RXCSR, csr);
966
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300967 dev_dbg(musb->controller, "%s iso overrun on %p\n", musb_ep->name, request);
Sergei Shtylyov43467862010-09-24 13:44:12 +0300968 if (request->status == -EINPROGRESS)
Felipe Balbi550a7372008-07-24 12:27:36 +0300969 request->status = -EOVERFLOW;
970 }
971 if (csr & MUSB_RXCSR_INCOMPRX) {
972 /* REVISIT not necessarily an error */
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300973 dev_dbg(musb->controller, "%s, incomprx\n", musb_ep->end_point.name);
Felipe Balbi550a7372008-07-24 12:27:36 +0300974 }
975
976 if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) {
977 /* "should not happen"; likely RXPKTRDY pending for DMA */
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300978 dev_dbg(musb->controller, "%s busy, csr %04x\n",
Felipe Balbi550a7372008-07-24 12:27:36 +0300979 musb_ep->end_point.name, csr);
Sergei Shtylyovcea83242009-11-18 22:51:18 +0300980 return;
Felipe Balbi550a7372008-07-24 12:27:36 +0300981 }
982
983 if (dma && (csr & MUSB_RXCSR_DMAENAB)) {
984 csr &= ~(MUSB_RXCSR_AUTOCLEAR
985 | MUSB_RXCSR_DMAENAB
986 | MUSB_RXCSR_DMAMODE);
987 musb_writew(epio, MUSB_RXCSR,
988 MUSB_RXCSR_P_WZC_BITS | csr);
989
990 request->actual += musb_ep->dma->actual_len;
991
Felipe Balbi5c8a86e2011-05-11 12:44:08 +0300992 dev_dbg(musb->controller, "RXCSR%d %04x, dma off, %04x, len %zu, req %p\n",
Felipe Balbi550a7372008-07-24 12:27:36 +0300993 epnum, csr,
994 musb_readw(epio, MUSB_RXCSR),
995 musb_ep->dma->actual_len, request);
996
Mian Yousaf Kaukaba48ff902011-03-22 15:55:56 +0100997#if defined(CONFIG_USB_INVENTRA_DMA) || defined(CONFIG_USB_TUSB_OMAP_DMA) || \
998 defined(CONFIG_USB_UX500_DMA)
Felipe Balbi550a7372008-07-24 12:27:36 +0300999 /* Autoclear doesn't clear RxPktRdy for short packets */
Ming Lei9001d802010-09-25 05:50:43 -05001000 if ((dma->desired_mode == 0 && !hw_ep->rx_double_buffered)
Felipe Balbi550a7372008-07-24 12:27:36 +03001001 || (dma->actual_len
1002 & (musb_ep->packet_sz - 1))) {
1003 /* ack the read! */
1004 csr &= ~MUSB_RXCSR_RXPKTRDY;
1005 musb_writew(epio, MUSB_RXCSR, csr);
1006 }
1007
1008 /* incomplete, and not short? wait for next IN packet */
1009 if ((request->actual < request->length)
1010 && (musb_ep->dma->actual_len
Ming Lei9001d802010-09-25 05:50:43 -05001011 == musb_ep->packet_sz)) {
1012 /* In double buffer case, continue to unload fifo if
1013 * there is Rx packet in FIFO.
1014 **/
1015 csr = musb_readw(epio, MUSB_RXCSR);
1016 if ((csr & MUSB_RXCSR_RXPKTRDY) &&
1017 hw_ep->rx_double_buffered)
1018 goto exit;
Sergei Shtylyovcea83242009-11-18 22:51:18 +03001019 return;
Ming Lei9001d802010-09-25 05:50:43 -05001020 }
Felipe Balbi550a7372008-07-24 12:27:36 +03001021#endif
1022 musb_g_giveback(musb_ep, request, 0);
Supriya Karanth39287072012-02-17 14:54:52 +05301023 /*
1024 * In the giveback function the MUSB lock is
1025 * released and acquired after sometime. During
1026 * this time period the INDEX register could get
1027 * changed by the gadget_queue function especially
1028 * on SMP systems. Reselect the INDEX to be sure
1029 * we are reading/modifying the right registers
1030 */
1031 musb_ep_select(mbase, epnum);
Felipe Balbi550a7372008-07-24 12:27:36 +03001032
Felipe Balbiad1adb82011-02-16 12:40:05 +02001033 req = next_request(musb_ep);
1034 if (!req)
Sergei Shtylyovcea83242009-11-18 22:51:18 +03001035 return;
Felipe Balbi550a7372008-07-24 12:27:36 +03001036 }
Mian Yousaf Kaukaba48ff902011-03-22 15:55:56 +01001037#if defined(CONFIG_USB_INVENTRA_DMA) || defined(CONFIG_USB_TUSB_OMAP_DMA) || \
1038 defined(CONFIG_USB_UX500_DMA)
Ming Lei9001d802010-09-25 05:50:43 -05001039exit:
Ajay Kumar Guptabb324b02010-11-22 14:22:41 +05301040#endif
Sergei Shtylyov43467862010-09-24 13:44:12 +03001041 /* Analyze request */
Felipe Balbiad1adb82011-02-16 12:40:05 +02001042 rxstate(musb, req);
Felipe Balbi550a7372008-07-24 12:27:36 +03001043}
1044
1045/* ------------------------------------------------------------ */
1046
1047static int musb_gadget_enable(struct usb_ep *ep,
1048 const struct usb_endpoint_descriptor *desc)
1049{
1050 unsigned long flags;
1051 struct musb_ep *musb_ep;
1052 struct musb_hw_ep *hw_ep;
1053 void __iomem *regs;
1054 struct musb *musb;
1055 void __iomem *mbase;
1056 u8 epnum;
1057 u16 csr;
1058 unsigned tmp;
1059 int status = -EINVAL;
1060
1061 if (!ep || !desc)
1062 return -EINVAL;
1063
1064 musb_ep = to_musb_ep(ep);
1065 hw_ep = musb_ep->hw_ep;
1066 regs = hw_ep->regs;
1067 musb = musb_ep->musb;
1068 mbase = musb->mregs;
1069 epnum = musb_ep->current_epnum;
1070
1071 spin_lock_irqsave(&musb->lock, flags);
1072
1073 if (musb_ep->desc) {
1074 status = -EBUSY;
1075 goto fail;
1076 }
Julia Lawall96bcd092009-01-24 17:57:24 -08001077 musb_ep->type = usb_endpoint_type(desc);
Felipe Balbi550a7372008-07-24 12:27:36 +03001078
1079 /* check direction and (later) maxpacket size against endpoint */
Julia Lawall96bcd092009-01-24 17:57:24 -08001080 if (usb_endpoint_num(desc) != epnum)
Felipe Balbi550a7372008-07-24 12:27:36 +03001081 goto fail;
1082
1083 /* REVISIT this rules out high bandwidth periodic transfers */
Kuninori Morimoto29cc8892011-08-23 03:12:03 -07001084 tmp = usb_endpoint_maxp(desc);
Ming Leif11d8932010-09-24 13:44:04 +03001085 if (tmp & ~0x07ff) {
1086 int ok;
1087
1088 if (usb_endpoint_dir_in(desc))
1089 ok = musb->hb_iso_tx;
1090 else
1091 ok = musb->hb_iso_rx;
1092
1093 if (!ok) {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001094 dev_dbg(musb->controller, "no support for high bandwidth ISO\n");
Ming Leif11d8932010-09-24 13:44:04 +03001095 goto fail;
1096 }
1097 musb_ep->hb_mult = (tmp >> 11) & 3;
1098 } else {
1099 musb_ep->hb_mult = 0;
1100 }
1101
1102 musb_ep->packet_sz = tmp & 0x7ff;
1103 tmp = musb_ep->packet_sz * (musb_ep->hb_mult + 1);
Felipe Balbi550a7372008-07-24 12:27:36 +03001104
1105 /* enable the interrupts for the endpoint, set the endpoint
1106 * packet size (or fail), set the mode, clear the fifo
1107 */
1108 musb_ep_select(mbase, epnum);
Julia Lawall96bcd092009-01-24 17:57:24 -08001109 if (usb_endpoint_dir_in(desc)) {
Felipe Balbi550a7372008-07-24 12:27:36 +03001110
1111 if (hw_ep->is_shared_fifo)
1112 musb_ep->is_in = 1;
1113 if (!musb_ep->is_in)
1114 goto fail;
Ming Leif11d8932010-09-24 13:44:04 +03001115
1116 if (tmp > hw_ep->max_packet_sz_tx) {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001117 dev_dbg(musb->controller, "packet size beyond hardware FIFO size\n");
Felipe Balbi550a7372008-07-24 12:27:36 +03001118 goto fail;
Ming Leif11d8932010-09-24 13:44:04 +03001119 }
Felipe Balbi550a7372008-07-24 12:27:36 +03001120
Sebastian Andrzej Siewiorb18d26f2012-10-30 19:52:26 +01001121 musb->intrtxe |= (1 << epnum);
1122 musb_writew(mbase, MUSB_INTRTXE, musb->intrtxe);
Felipe Balbi550a7372008-07-24 12:27:36 +03001123
1124 /* REVISIT if can_bulk_split(), use by updating "tmp";
1125 * likewise high bandwidth periodic tx
1126 */
Cliff Cai9f445cb2010-01-28 20:44:18 -05001127 /* Set TXMAXP with the FIFO size of the endpoint
Ming Lei31c99092010-10-19 19:08:25 -05001128 * to disable double buffering mode.
Cliff Cai9f445cb2010-01-28 20:44:18 -05001129 */
supriya karanthbb3a2ef2012-12-06 11:12:48 +05301130 if (musb->double_buffer_not_ok) {
Felipe Balbi06624812011-01-21 13:39:20 +08001131 musb_writew(regs, MUSB_TXMAXP, hw_ep->max_packet_sz_tx);
supriya karanthbb3a2ef2012-12-06 11:12:48 +05301132 } else {
1133 if (can_bulk_split(musb, musb_ep->type))
1134 musb_ep->hb_mult = (hw_ep->max_packet_sz_tx /
1135 musb_ep->packet_sz) - 1;
Felipe Balbi06624812011-01-21 13:39:20 +08001136 musb_writew(regs, MUSB_TXMAXP, musb_ep->packet_sz
1137 | (musb_ep->hb_mult << 11));
supriya karanthbb3a2ef2012-12-06 11:12:48 +05301138 }
Felipe Balbi550a7372008-07-24 12:27:36 +03001139
1140 csr = MUSB_TXCSR_MODE | MUSB_TXCSR_CLRDATATOG;
1141 if (musb_readw(regs, MUSB_TXCSR)
1142 & MUSB_TXCSR_FIFONOTEMPTY)
1143 csr |= MUSB_TXCSR_FLUSHFIFO;
1144 if (musb_ep->type == USB_ENDPOINT_XFER_ISOC)
1145 csr |= MUSB_TXCSR_P_ISO;
1146
1147 /* set twice in case of double buffering */
1148 musb_writew(regs, MUSB_TXCSR, csr);
1149 /* REVISIT may be inappropriate w/o FIFONOTEMPTY ... */
1150 musb_writew(regs, MUSB_TXCSR, csr);
1151
1152 } else {
Felipe Balbi550a7372008-07-24 12:27:36 +03001153
1154 if (hw_ep->is_shared_fifo)
1155 musb_ep->is_in = 0;
1156 if (musb_ep->is_in)
1157 goto fail;
Ming Leif11d8932010-09-24 13:44:04 +03001158
1159 if (tmp > hw_ep->max_packet_sz_rx) {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001160 dev_dbg(musb->controller, "packet size beyond hardware FIFO size\n");
Felipe Balbi550a7372008-07-24 12:27:36 +03001161 goto fail;
Ming Leif11d8932010-09-24 13:44:04 +03001162 }
Felipe Balbi550a7372008-07-24 12:27:36 +03001163
Sebastian Andrzej Siewioraf5ec142012-10-30 19:52:25 +01001164 musb->intrrxe |= (1 << epnum);
1165 musb_writew(mbase, MUSB_INTRRXE, musb->intrrxe);
Felipe Balbi550a7372008-07-24 12:27:36 +03001166
1167 /* REVISIT if can_bulk_combine() use by updating "tmp"
1168 * likewise high bandwidth periodic rx
1169 */
Cliff Cai9f445cb2010-01-28 20:44:18 -05001170 /* Set RXMAXP with the FIFO size of the endpoint
1171 * to disable double buffering mode.
1172 */
Felipe Balbi06624812011-01-21 13:39:20 +08001173 if (musb->double_buffer_not_ok)
1174 musb_writew(regs, MUSB_RXMAXP, hw_ep->max_packet_sz_tx);
1175 else
1176 musb_writew(regs, MUSB_RXMAXP, musb_ep->packet_sz
1177 | (musb_ep->hb_mult << 11));
Felipe Balbi550a7372008-07-24 12:27:36 +03001178
1179 /* force shared fifo to OUT-only mode */
1180 if (hw_ep->is_shared_fifo) {
1181 csr = musb_readw(regs, MUSB_TXCSR);
1182 csr &= ~(MUSB_TXCSR_MODE | MUSB_TXCSR_TXPKTRDY);
1183 musb_writew(regs, MUSB_TXCSR, csr);
1184 }
1185
1186 csr = MUSB_RXCSR_FLUSHFIFO | MUSB_RXCSR_CLRDATATOG;
1187 if (musb_ep->type == USB_ENDPOINT_XFER_ISOC)
1188 csr |= MUSB_RXCSR_P_ISO;
1189 else if (musb_ep->type == USB_ENDPOINT_XFER_INT)
1190 csr |= MUSB_RXCSR_DISNYET;
1191
1192 /* set twice in case of double buffering */
1193 musb_writew(regs, MUSB_RXCSR, csr);
1194 musb_writew(regs, MUSB_RXCSR, csr);
1195 }
1196
1197 /* NOTE: all the I/O code _should_ work fine without DMA, in case
1198 * for some reason you run out of channels here.
1199 */
1200 if (is_dma_capable() && musb->dma_controller) {
1201 struct dma_controller *c = musb->dma_controller;
1202
1203 musb_ep->dma = c->channel_alloc(c, hw_ep,
1204 (desc->bEndpointAddress & USB_DIR_IN));
1205 } else
1206 musb_ep->dma = NULL;
1207
1208 musb_ep->desc = desc;
1209 musb_ep->busy = 0;
Sergei Shtylyov47e97602009-11-18 22:51:51 +03001210 musb_ep->wedged = 0;
Felipe Balbi550a7372008-07-24 12:27:36 +03001211 status = 0;
1212
1213 pr_debug("%s periph: enabled %s for %s %s, %smaxpacket %d\n",
1214 musb_driver_name, musb_ep->end_point.name,
1215 ({ char *s; switch (musb_ep->type) {
1216 case USB_ENDPOINT_XFER_BULK: s = "bulk"; break;
1217 case USB_ENDPOINT_XFER_INT: s = "int"; break;
1218 default: s = "iso"; break;
1219 }; s; }),
1220 musb_ep->is_in ? "IN" : "OUT",
1221 musb_ep->dma ? "dma, " : "",
1222 musb_ep->packet_sz);
1223
1224 schedule_work(&musb->irq_work);
1225
1226fail:
1227 spin_unlock_irqrestore(&musb->lock, flags);
1228 return status;
1229}
1230
1231/*
1232 * Disable an endpoint flushing all requests queued.
1233 */
1234static int musb_gadget_disable(struct usb_ep *ep)
1235{
1236 unsigned long flags;
1237 struct musb *musb;
1238 u8 epnum;
1239 struct musb_ep *musb_ep;
1240 void __iomem *epio;
1241 int status = 0;
1242
1243 musb_ep = to_musb_ep(ep);
1244 musb = musb_ep->musb;
1245 epnum = musb_ep->current_epnum;
1246 epio = musb->endpoints[epnum].regs;
1247
1248 spin_lock_irqsave(&musb->lock, flags);
1249 musb_ep_select(musb->mregs, epnum);
1250
1251 /* zero the endpoint sizes */
1252 if (musb_ep->is_in) {
Sebastian Andrzej Siewiorb18d26f2012-10-30 19:52:26 +01001253 musb->intrtxe &= ~(1 << epnum);
1254 musb_writew(musb->mregs, MUSB_INTRTXE, musb->intrtxe);
Felipe Balbi550a7372008-07-24 12:27:36 +03001255 musb_writew(epio, MUSB_TXMAXP, 0);
1256 } else {
Sebastian Andrzej Siewioraf5ec142012-10-30 19:52:25 +01001257 musb->intrrxe &= ~(1 << epnum);
1258 musb_writew(musb->mregs, MUSB_INTRRXE, musb->intrrxe);
Felipe Balbi550a7372008-07-24 12:27:36 +03001259 musb_writew(epio, MUSB_RXMAXP, 0);
1260 }
1261
1262 musb_ep->desc = NULL;
Grazvydas Ignotas08f75bf2012-05-26 00:21:33 +03001263 musb_ep->end_point.desc = NULL;
Felipe Balbi550a7372008-07-24 12:27:36 +03001264
1265 /* abort all pending DMA and requests */
1266 nuke(musb_ep, -ESHUTDOWN);
1267
1268 schedule_work(&musb->irq_work);
1269
1270 spin_unlock_irqrestore(&(musb->lock), flags);
1271
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001272 dev_dbg(musb->controller, "%s\n", musb_ep->end_point.name);
Felipe Balbi550a7372008-07-24 12:27:36 +03001273
1274 return status;
1275}
1276
1277/*
1278 * Allocate a request for an endpoint.
1279 * Reused by ep0 code.
1280 */
1281struct usb_request *musb_alloc_request(struct usb_ep *ep, gfp_t gfp_flags)
1282{
1283 struct musb_ep *musb_ep = to_musb_ep(ep);
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001284 struct musb *musb = musb_ep->musb;
Felipe Balbi550a7372008-07-24 12:27:36 +03001285 struct musb_request *request = NULL;
1286
1287 request = kzalloc(sizeof *request, gfp_flags);
Felipe Balbi0607f862010-12-01 11:03:54 +02001288 if (!request) {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001289 dev_dbg(musb->controller, "not enough memory\n");
Felipe Balbi0607f862010-12-01 11:03:54 +02001290 return NULL;
Felipe Balbi550a7372008-07-24 12:27:36 +03001291 }
1292
Felipe Balbi0607f862010-12-01 11:03:54 +02001293 request->request.dma = DMA_ADDR_INVALID;
1294 request->epnum = musb_ep->current_epnum;
1295 request->ep = musb_ep;
1296
Felipe Balbi550a7372008-07-24 12:27:36 +03001297 return &request->request;
1298}
1299
1300/*
1301 * Free a request
1302 * Reused by ep0 code.
1303 */
1304void musb_free_request(struct usb_ep *ep, struct usb_request *req)
1305{
1306 kfree(to_musb_request(req));
1307}
1308
1309static LIST_HEAD(buffers);
1310
1311struct free_record {
1312 struct list_head list;
1313 struct device *dev;
1314 unsigned bytes;
1315 dma_addr_t dma;
1316};
1317
1318/*
1319 * Context: controller locked, IRQs blocked.
1320 */
Sergei Shtylyova666e3e2010-09-11 13:23:12 -05001321void musb_ep_restart(struct musb *musb, struct musb_request *req)
Felipe Balbi550a7372008-07-24 12:27:36 +03001322{
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001323 dev_dbg(musb->controller, "<== %s request %p len %u on hw_ep%d\n",
Felipe Balbi550a7372008-07-24 12:27:36 +03001324 req->tx ? "TX/IN" : "RX/OUT",
1325 &req->request, req->request.length, req->epnum);
1326
1327 musb_ep_select(musb->mregs, req->epnum);
1328 if (req->tx)
1329 txstate(musb, req);
1330 else
1331 rxstate(musb, req);
1332}
1333
1334static int musb_gadget_queue(struct usb_ep *ep, struct usb_request *req,
1335 gfp_t gfp_flags)
1336{
1337 struct musb_ep *musb_ep;
1338 struct musb_request *request;
1339 struct musb *musb;
1340 int status = 0;
1341 unsigned long lockflags;
1342
1343 if (!ep || !req)
1344 return -EINVAL;
1345 if (!req->buf)
1346 return -ENODATA;
1347
1348 musb_ep = to_musb_ep(ep);
1349 musb = musb_ep->musb;
1350
1351 request = to_musb_request(req);
1352 request->musb = musb;
1353
1354 if (request->ep != musb_ep)
1355 return -EINVAL;
1356
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001357 dev_dbg(musb->controller, "<== to %s request=%p\n", ep->name, req);
Felipe Balbi550a7372008-07-24 12:27:36 +03001358
1359 /* request is mine now... */
1360 request->request.actual = 0;
1361 request->request.status = -EINPROGRESS;
1362 request->epnum = musb_ep->current_epnum;
1363 request->tx = musb_ep->is_in;
1364
Mian Yousaf Kaukabc65bfa62011-01-04 12:47:02 +01001365 map_dma_buffer(request, musb, musb_ep);
Felipe Balbi550a7372008-07-24 12:27:36 +03001366
1367 spin_lock_irqsave(&musb->lock, lockflags);
1368
1369 /* don't queue if the ep is down */
1370 if (!musb_ep->desc) {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001371 dev_dbg(musb->controller, "req %p queued to %s while ep %s\n",
Felipe Balbi550a7372008-07-24 12:27:36 +03001372 req, ep->name, "disabled");
1373 status = -ESHUTDOWN;
1374 goto cleanup;
1375 }
1376
1377 /* add request to the list */
Felipe Balbiad1adb82011-02-16 12:40:05 +02001378 list_add_tail(&request->list, &musb_ep->req_list);
Felipe Balbi550a7372008-07-24 12:27:36 +03001379
1380 /* it this is the head of the queue, start i/o ... */
Felipe Balbiad1adb82011-02-16 12:40:05 +02001381 if (!musb_ep->busy && &request->list == musb_ep->req_list.next)
Felipe Balbi550a7372008-07-24 12:27:36 +03001382 musb_ep_restart(musb, request);
1383
1384cleanup:
1385 spin_unlock_irqrestore(&musb->lock, lockflags);
1386 return status;
1387}
1388
1389static int musb_gadget_dequeue(struct usb_ep *ep, struct usb_request *request)
1390{
1391 struct musb_ep *musb_ep = to_musb_ep(ep);
Felipe Balbi4cbbf082011-02-28 10:44:50 +02001392 struct musb_request *req = to_musb_request(request);
1393 struct musb_request *r;
Felipe Balbi550a7372008-07-24 12:27:36 +03001394 unsigned long flags;
1395 int status = 0;
1396 struct musb *musb = musb_ep->musb;
1397
1398 if (!ep || !request || to_musb_request(request)->ep != musb_ep)
1399 return -EINVAL;
1400
1401 spin_lock_irqsave(&musb->lock, flags);
1402
1403 list_for_each_entry(r, &musb_ep->req_list, list) {
Felipe Balbi4cbbf082011-02-28 10:44:50 +02001404 if (r == req)
Felipe Balbi550a7372008-07-24 12:27:36 +03001405 break;
1406 }
Felipe Balbi4cbbf082011-02-28 10:44:50 +02001407 if (r != req) {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001408 dev_dbg(musb->controller, "request %p not queued to %s\n", request, ep->name);
Felipe Balbi550a7372008-07-24 12:27:36 +03001409 status = -EINVAL;
1410 goto done;
1411 }
1412
1413 /* if the hardware doesn't have the request, easy ... */
Felipe Balbi3d5ad132011-03-22 11:38:49 +02001414 if (musb_ep->req_list.next != &req->list || musb_ep->busy)
Felipe Balbi550a7372008-07-24 12:27:36 +03001415 musb_g_giveback(musb_ep, request, -ECONNRESET);
1416
1417 /* ... else abort the dma transfer ... */
1418 else if (is_dma_capable() && musb_ep->dma) {
1419 struct dma_controller *c = musb->dma_controller;
1420
1421 musb_ep_select(musb->mregs, musb_ep->current_epnum);
1422 if (c->channel_abort)
1423 status = c->channel_abort(musb_ep->dma);
1424 else
1425 status = -EBUSY;
1426 if (status == 0)
1427 musb_g_giveback(musb_ep, request, -ECONNRESET);
1428 } else {
1429 /* NOTE: by sticking to easily tested hardware/driver states,
1430 * we leave counting of in-flight packets imprecise.
1431 */
1432 musb_g_giveback(musb_ep, request, -ECONNRESET);
1433 }
1434
1435done:
1436 spin_unlock_irqrestore(&musb->lock, flags);
1437 return status;
1438}
1439
1440/*
1441 * Set or clear the halt bit of an endpoint. A halted enpoint won't tx/rx any
1442 * data but will queue requests.
1443 *
1444 * exported to ep0 code
1445 */
Felipe Balbi1b6c3b02009-12-04 15:47:46 +02001446static int musb_gadget_set_halt(struct usb_ep *ep, int value)
Felipe Balbi550a7372008-07-24 12:27:36 +03001447{
1448 struct musb_ep *musb_ep = to_musb_ep(ep);
1449 u8 epnum = musb_ep->current_epnum;
1450 struct musb *musb = musb_ep->musb;
1451 void __iomem *epio = musb->endpoints[epnum].regs;
1452 void __iomem *mbase;
1453 unsigned long flags;
1454 u16 csr;
Sergei Shtylyovcea83242009-11-18 22:51:18 +03001455 struct musb_request *request;
Felipe Balbi550a7372008-07-24 12:27:36 +03001456 int status = 0;
1457
1458 if (!ep)
1459 return -EINVAL;
1460 mbase = musb->mregs;
1461
1462 spin_lock_irqsave(&musb->lock, flags);
1463
1464 if ((USB_ENDPOINT_XFER_ISOC == musb_ep->type)) {
1465 status = -EINVAL;
1466 goto done;
1467 }
1468
1469 musb_ep_select(mbase, epnum);
1470
Felipe Balbiad1adb82011-02-16 12:40:05 +02001471 request = next_request(musb_ep);
Sergei Shtylyovcea83242009-11-18 22:51:18 +03001472 if (value) {
1473 if (request) {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001474 dev_dbg(musb->controller, "request in progress, cannot halt %s\n",
Sergei Shtylyovcea83242009-11-18 22:51:18 +03001475 ep->name);
1476 status = -EAGAIN;
1477 goto done;
Felipe Balbi550a7372008-07-24 12:27:36 +03001478 }
Sergei Shtylyovcea83242009-11-18 22:51:18 +03001479 /* Cannot portably stall with non-empty FIFO */
1480 if (musb_ep->is_in) {
1481 csr = musb_readw(epio, MUSB_TXCSR);
1482 if (csr & MUSB_TXCSR_FIFONOTEMPTY) {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001483 dev_dbg(musb->controller, "FIFO busy, cannot halt %s\n", ep->name);
Sergei Shtylyovcea83242009-11-18 22:51:18 +03001484 status = -EAGAIN;
1485 goto done;
1486 }
1487 }
Sergei Shtylyov47e97602009-11-18 22:51:51 +03001488 } else
1489 musb_ep->wedged = 0;
Felipe Balbi550a7372008-07-24 12:27:36 +03001490
1491 /* set/clear the stall and toggle bits */
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001492 dev_dbg(musb->controller, "%s: %s stall\n", ep->name, value ? "set" : "clear");
Felipe Balbi550a7372008-07-24 12:27:36 +03001493 if (musb_ep->is_in) {
1494 csr = musb_readw(epio, MUSB_TXCSR);
Felipe Balbi550a7372008-07-24 12:27:36 +03001495 csr |= MUSB_TXCSR_P_WZC_BITS
1496 | MUSB_TXCSR_CLRDATATOG;
1497 if (value)
1498 csr |= MUSB_TXCSR_P_SENDSTALL;
1499 else
1500 csr &= ~(MUSB_TXCSR_P_SENDSTALL
1501 | MUSB_TXCSR_P_SENTSTALL);
1502 csr &= ~MUSB_TXCSR_TXPKTRDY;
1503 musb_writew(epio, MUSB_TXCSR, csr);
1504 } else {
1505 csr = musb_readw(epio, MUSB_RXCSR);
1506 csr |= MUSB_RXCSR_P_WZC_BITS
1507 | MUSB_RXCSR_FLUSHFIFO
1508 | MUSB_RXCSR_CLRDATATOG;
1509 if (value)
1510 csr |= MUSB_RXCSR_P_SENDSTALL;
1511 else
1512 csr &= ~(MUSB_RXCSR_P_SENDSTALL
1513 | MUSB_RXCSR_P_SENTSTALL);
1514 musb_writew(epio, MUSB_RXCSR, csr);
1515 }
1516
Felipe Balbi550a7372008-07-24 12:27:36 +03001517 /* maybe start the first request in the queue */
1518 if (!musb_ep->busy && !value && request) {
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001519 dev_dbg(musb->controller, "restarting the request\n");
Felipe Balbi550a7372008-07-24 12:27:36 +03001520 musb_ep_restart(musb, request);
1521 }
1522
Sergei Shtylyovcea83242009-11-18 22:51:18 +03001523done:
Felipe Balbi550a7372008-07-24 12:27:36 +03001524 spin_unlock_irqrestore(&musb->lock, flags);
1525 return status;
1526}
1527
Sergei Shtylyov47e97602009-11-18 22:51:51 +03001528/*
1529 * Sets the halt feature with the clear requests ignored
1530 */
Felipe Balbi1b6c3b02009-12-04 15:47:46 +02001531static int musb_gadget_set_wedge(struct usb_ep *ep)
Sergei Shtylyov47e97602009-11-18 22:51:51 +03001532{
1533 struct musb_ep *musb_ep = to_musb_ep(ep);
1534
1535 if (!ep)
1536 return -EINVAL;
1537
1538 musb_ep->wedged = 1;
1539
1540 return usb_ep_set_halt(ep);
1541}
1542
Felipe Balbi550a7372008-07-24 12:27:36 +03001543static int musb_gadget_fifo_status(struct usb_ep *ep)
1544{
1545 struct musb_ep *musb_ep = to_musb_ep(ep);
1546 void __iomem *epio = musb_ep->hw_ep->regs;
1547 int retval = -EINVAL;
1548
1549 if (musb_ep->desc && !musb_ep->is_in) {
1550 struct musb *musb = musb_ep->musb;
1551 int epnum = musb_ep->current_epnum;
1552 void __iomem *mbase = musb->mregs;
1553 unsigned long flags;
1554
1555 spin_lock_irqsave(&musb->lock, flags);
1556
1557 musb_ep_select(mbase, epnum);
1558 /* FIXME return zero unless RXPKTRDY is set */
1559 retval = musb_readw(epio, MUSB_RXCOUNT);
1560
1561 spin_unlock_irqrestore(&musb->lock, flags);
1562 }
1563 return retval;
1564}
1565
1566static void musb_gadget_fifo_flush(struct usb_ep *ep)
1567{
1568 struct musb_ep *musb_ep = to_musb_ep(ep);
1569 struct musb *musb = musb_ep->musb;
1570 u8 epnum = musb_ep->current_epnum;
1571 void __iomem *epio = musb->endpoints[epnum].regs;
1572 void __iomem *mbase;
1573 unsigned long flags;
Sebastian Andrzej Siewiorb18d26f2012-10-30 19:52:26 +01001574 u16 csr;
Felipe Balbi550a7372008-07-24 12:27:36 +03001575
1576 mbase = musb->mregs;
1577
1578 spin_lock_irqsave(&musb->lock, flags);
1579 musb_ep_select(mbase, (u8) epnum);
1580
1581 /* disable interrupts */
Sebastian Andrzej Siewiorb18d26f2012-10-30 19:52:26 +01001582 musb_writew(mbase, MUSB_INTRTXE, musb->intrtxe & ~(1 << epnum));
Felipe Balbi550a7372008-07-24 12:27:36 +03001583
1584 if (musb_ep->is_in) {
1585 csr = musb_readw(epio, MUSB_TXCSR);
1586 if (csr & MUSB_TXCSR_FIFONOTEMPTY) {
1587 csr |= MUSB_TXCSR_FLUSHFIFO | MUSB_TXCSR_P_WZC_BITS;
Yauheni Kaliuta4858f062011-06-08 17:12:02 +03001588 /*
1589 * Setting both TXPKTRDY and FLUSHFIFO makes controller
1590 * to interrupt current FIFO loading, but not flushing
1591 * the already loaded ones.
1592 */
1593 csr &= ~MUSB_TXCSR_TXPKTRDY;
Felipe Balbi550a7372008-07-24 12:27:36 +03001594 musb_writew(epio, MUSB_TXCSR, csr);
1595 /* REVISIT may be inappropriate w/o FIFONOTEMPTY ... */
1596 musb_writew(epio, MUSB_TXCSR, csr);
1597 }
1598 } else {
1599 csr = musb_readw(epio, MUSB_RXCSR);
1600 csr |= MUSB_RXCSR_FLUSHFIFO | MUSB_RXCSR_P_WZC_BITS;
1601 musb_writew(epio, MUSB_RXCSR, csr);
1602 musb_writew(epio, MUSB_RXCSR, csr);
1603 }
1604
1605 /* re-enable interrupt */
Sebastian Andrzej Siewiorb18d26f2012-10-30 19:52:26 +01001606 musb_writew(mbase, MUSB_INTRTXE, musb->intrtxe);
Felipe Balbi550a7372008-07-24 12:27:36 +03001607 spin_unlock_irqrestore(&musb->lock, flags);
1608}
1609
1610static const struct usb_ep_ops musb_ep_ops = {
1611 .enable = musb_gadget_enable,
1612 .disable = musb_gadget_disable,
1613 .alloc_request = musb_alloc_request,
1614 .free_request = musb_free_request,
1615 .queue = musb_gadget_queue,
1616 .dequeue = musb_gadget_dequeue,
1617 .set_halt = musb_gadget_set_halt,
Sergei Shtylyov47e97602009-11-18 22:51:51 +03001618 .set_wedge = musb_gadget_set_wedge,
Felipe Balbi550a7372008-07-24 12:27:36 +03001619 .fifo_status = musb_gadget_fifo_status,
1620 .fifo_flush = musb_gadget_fifo_flush
1621};
1622
1623/* ----------------------------------------------------------------------- */
1624
1625static int musb_gadget_get_frame(struct usb_gadget *gadget)
1626{
1627 struct musb *musb = gadget_to_musb(gadget);
1628
1629 return (int)musb_readw(musb->mregs, MUSB_FRAME);
1630}
1631
1632static int musb_gadget_wakeup(struct usb_gadget *gadget)
1633{
1634 struct musb *musb = gadget_to_musb(gadget);
1635 void __iomem *mregs = musb->mregs;
1636 unsigned long flags;
1637 int status = -EINVAL;
1638 u8 power, devctl;
1639 int retries;
1640
1641 spin_lock_irqsave(&musb->lock, flags);
1642
David Brownell84e250f2009-03-31 12:30:04 -07001643 switch (musb->xceiv->state) {
Felipe Balbi550a7372008-07-24 12:27:36 +03001644 case OTG_STATE_B_PERIPHERAL:
1645 /* NOTE: OTG state machine doesn't include B_SUSPENDED;
1646 * that's part of the standard usb 1.1 state machine, and
1647 * doesn't affect OTG transitions.
1648 */
1649 if (musb->may_wakeup && musb->is_suspended)
1650 break;
1651 goto done;
1652 case OTG_STATE_B_IDLE:
1653 /* Start SRP ... OTG not required. */
1654 devctl = musb_readb(mregs, MUSB_DEVCTL);
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001655 dev_dbg(musb->controller, "Sending SRP: devctl: %02x\n", devctl);
Felipe Balbi550a7372008-07-24 12:27:36 +03001656 devctl |= MUSB_DEVCTL_SESSION;
1657 musb_writeb(mregs, MUSB_DEVCTL, devctl);
1658 devctl = musb_readb(mregs, MUSB_DEVCTL);
1659 retries = 100;
1660 while (!(devctl & MUSB_DEVCTL_SESSION)) {
1661 devctl = musb_readb(mregs, MUSB_DEVCTL);
1662 if (retries-- < 1)
1663 break;
1664 }
1665 retries = 10000;
1666 while (devctl & MUSB_DEVCTL_SESSION) {
1667 devctl = musb_readb(mregs, MUSB_DEVCTL);
1668 if (retries-- < 1)
1669 break;
1670 }
1671
Hema HK86205432011-03-22 16:54:22 +05301672 spin_unlock_irqrestore(&musb->lock, flags);
Heikki Krogerus6e13c652012-02-13 13:24:20 +02001673 otg_start_srp(musb->xceiv->otg);
Hema HK86205432011-03-22 16:54:22 +05301674 spin_lock_irqsave(&musb->lock, flags);
1675
Felipe Balbi550a7372008-07-24 12:27:36 +03001676 /* Block idling for at least 1s */
1677 musb_platform_try_idle(musb,
1678 jiffies + msecs_to_jiffies(1 * HZ));
1679
1680 status = 0;
1681 goto done;
1682 default:
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001683 dev_dbg(musb->controller, "Unhandled wake: %s\n",
Anatolij Gustschin3df00452011-05-05 12:11:21 +02001684 otg_state_string(musb->xceiv->state));
Felipe Balbi550a7372008-07-24 12:27:36 +03001685 goto done;
1686 }
1687
1688 status = 0;
1689
1690 power = musb_readb(mregs, MUSB_POWER);
1691 power |= MUSB_POWER_RESUME;
1692 musb_writeb(mregs, MUSB_POWER, power);
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001693 dev_dbg(musb->controller, "issue wakeup\n");
Felipe Balbi550a7372008-07-24 12:27:36 +03001694
1695 /* FIXME do this next chunk in a timer callback, no udelay */
1696 mdelay(2);
1697
1698 power = musb_readb(mregs, MUSB_POWER);
1699 power &= ~MUSB_POWER_RESUME;
1700 musb_writeb(mregs, MUSB_POWER, power);
1701done:
1702 spin_unlock_irqrestore(&musb->lock, flags);
1703 return status;
1704}
1705
1706static int
1707musb_gadget_set_self_powered(struct usb_gadget *gadget, int is_selfpowered)
1708{
1709 struct musb *musb = gadget_to_musb(gadget);
1710
1711 musb->is_self_powered = !!is_selfpowered;
1712 return 0;
1713}
1714
1715static void musb_pullup(struct musb *musb, int is_on)
1716{
1717 u8 power;
1718
1719 power = musb_readb(musb->mregs, MUSB_POWER);
1720 if (is_on)
1721 power |= MUSB_POWER_SOFTCONN;
1722 else
1723 power &= ~MUSB_POWER_SOFTCONN;
1724
1725 /* FIXME if on, HdrcStart; if off, HdrcStop */
1726
Sebastian Andrzej Siewiore71eb392011-06-23 14:26:16 +02001727 dev_dbg(musb->controller, "gadget D+ pullup %s\n",
1728 is_on ? "on" : "off");
Felipe Balbi550a7372008-07-24 12:27:36 +03001729 musb_writeb(musb->mregs, MUSB_POWER, power);
1730}
1731
1732#if 0
1733static int musb_gadget_vbus_session(struct usb_gadget *gadget, int is_active)
1734{
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001735 dev_dbg(musb->controller, "<= %s =>\n", __func__);
Felipe Balbi550a7372008-07-24 12:27:36 +03001736
1737 /*
1738 * FIXME iff driver's softconnect flag is set (as it is during probe,
1739 * though that can clear it), just musb_pullup().
1740 */
1741
1742 return -EINVAL;
1743}
1744#endif
1745
1746static int musb_gadget_vbus_draw(struct usb_gadget *gadget, unsigned mA)
1747{
1748 struct musb *musb = gadget_to_musb(gadget);
1749
David Brownell84e250f2009-03-31 12:30:04 -07001750 if (!musb->xceiv->set_power)
Felipe Balbi550a7372008-07-24 12:27:36 +03001751 return -EOPNOTSUPP;
Heikki Krogerusb96d3b02012-02-13 13:24:18 +02001752 return usb_phy_set_power(musb->xceiv, mA);
Felipe Balbi550a7372008-07-24 12:27:36 +03001753}
1754
1755static int musb_gadget_pullup(struct usb_gadget *gadget, int is_on)
1756{
1757 struct musb *musb = gadget_to_musb(gadget);
1758 unsigned long flags;
1759
1760 is_on = !!is_on;
1761
John Stultz93e098a2011-07-20 17:09:34 -07001762 pm_runtime_get_sync(musb->controller);
1763
Felipe Balbi550a7372008-07-24 12:27:36 +03001764 /* NOTE: this assumes we are sensing vbus; we'd rather
1765 * not pullup unless the B-session is active.
1766 */
1767 spin_lock_irqsave(&musb->lock, flags);
1768 if (is_on != musb->softconnect) {
1769 musb->softconnect = is_on;
1770 musb_pullup(musb, is_on);
1771 }
1772 spin_unlock_irqrestore(&musb->lock, flags);
John Stultz93e098a2011-07-20 17:09:34 -07001773
1774 pm_runtime_put(musb->controller);
1775
Felipe Balbi550a7372008-07-24 12:27:36 +03001776 return 0;
1777}
1778
Sebastian Andrzej Siewiore71eb392011-06-23 14:26:16 +02001779static int musb_gadget_start(struct usb_gadget *g,
1780 struct usb_gadget_driver *driver);
1781static int musb_gadget_stop(struct usb_gadget *g,
1782 struct usb_gadget_driver *driver);
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001783
Felipe Balbi550a7372008-07-24 12:27:36 +03001784static const struct usb_gadget_ops musb_gadget_operations = {
1785 .get_frame = musb_gadget_get_frame,
1786 .wakeup = musb_gadget_wakeup,
1787 .set_selfpowered = musb_gadget_set_self_powered,
1788 /* .vbus_session = musb_gadget_vbus_session, */
1789 .vbus_draw = musb_gadget_vbus_draw,
1790 .pullup = musb_gadget_pullup,
Sebastian Andrzej Siewiore71eb392011-06-23 14:26:16 +02001791 .udc_start = musb_gadget_start,
1792 .udc_stop = musb_gadget_stop,
Felipe Balbi550a7372008-07-24 12:27:36 +03001793};
1794
1795/* ----------------------------------------------------------------------- */
1796
1797/* Registration */
1798
1799/* Only this registration code "knows" the rule (from USB standards)
1800 * about there being only one external upstream port. It assumes
1801 * all peripheral ports are external...
1802 */
Felipe Balbi550a7372008-07-24 12:27:36 +03001803
1804static void musb_gadget_release(struct device *dev)
1805{
1806 /* kref_put(WHAT) */
1807 dev_dbg(dev, "%s\n", __func__);
1808}
1809
1810
Bill Pemberton41ac7b32012-11-19 13:21:48 -05001811static void
Felipe Balbi550a7372008-07-24 12:27:36 +03001812init_peripheral_ep(struct musb *musb, struct musb_ep *ep, u8 epnum, int is_in)
1813{
1814 struct musb_hw_ep *hw_ep = musb->endpoints + epnum;
1815
1816 memset(ep, 0, sizeof *ep);
1817
1818 ep->current_epnum = epnum;
1819 ep->musb = musb;
1820 ep->hw_ep = hw_ep;
1821 ep->is_in = is_in;
1822
1823 INIT_LIST_HEAD(&ep->req_list);
1824
1825 sprintf(ep->name, "ep%d%s", epnum,
1826 (!epnum || hw_ep->is_shared_fifo) ? "" : (
1827 is_in ? "in" : "out"));
1828 ep->end_point.name = ep->name;
1829 INIT_LIST_HEAD(&ep->end_point.ep_list);
1830 if (!epnum) {
1831 ep->end_point.maxpacket = 64;
1832 ep->end_point.ops = &musb_g_ep0_ops;
1833 musb->g.ep0 = &ep->end_point;
1834 } else {
1835 if (is_in)
1836 ep->end_point.maxpacket = hw_ep->max_packet_sz_tx;
1837 else
1838 ep->end_point.maxpacket = hw_ep->max_packet_sz_rx;
1839 ep->end_point.ops = &musb_ep_ops;
1840 list_add_tail(&ep->end_point.ep_list, &musb->g.ep_list);
1841 }
1842}
1843
1844/*
1845 * Initialize the endpoints exposed to peripheral drivers, with backlinks
1846 * to the rest of the driver state.
1847 */
Bill Pemberton41ac7b32012-11-19 13:21:48 -05001848static inline void musb_g_init_endpoints(struct musb *musb)
Felipe Balbi550a7372008-07-24 12:27:36 +03001849{
1850 u8 epnum;
1851 struct musb_hw_ep *hw_ep;
1852 unsigned count = 0;
1853
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04001854 /* initialize endpoint list just once */
Felipe Balbi550a7372008-07-24 12:27:36 +03001855 INIT_LIST_HEAD(&(musb->g.ep_list));
1856
1857 for (epnum = 0, hw_ep = musb->endpoints;
1858 epnum < musb->nr_endpoints;
1859 epnum++, hw_ep++) {
1860 if (hw_ep->is_shared_fifo /* || !epnum */) {
1861 init_peripheral_ep(musb, &hw_ep->ep_in, epnum, 0);
1862 count++;
1863 } else {
1864 if (hw_ep->max_packet_sz_tx) {
1865 init_peripheral_ep(musb, &hw_ep->ep_in,
1866 epnum, 1);
1867 count++;
1868 }
1869 if (hw_ep->max_packet_sz_rx) {
1870 init_peripheral_ep(musb, &hw_ep->ep_out,
1871 epnum, 0);
1872 count++;
1873 }
1874 }
1875 }
1876}
1877
1878/* called once during driver setup to initialize and link into
1879 * the driver model; memory is zeroed.
1880 */
Bill Pemberton41ac7b32012-11-19 13:21:48 -05001881int musb_gadget_setup(struct musb *musb)
Felipe Balbi550a7372008-07-24 12:27:36 +03001882{
1883 int status;
1884
1885 /* REVISIT minor race: if (erroneously) setting up two
1886 * musb peripherals at the same time, only the bus lock
1887 * is probably held.
1888 */
Felipe Balbi550a7372008-07-24 12:27:36 +03001889
1890 musb->g.ops = &musb_gadget_operations;
Michal Nazarewiczd327ab52011-11-19 18:27:37 +01001891 musb->g.max_speed = USB_SPEED_HIGH;
Felipe Balbi550a7372008-07-24 12:27:36 +03001892 musb->g.speed = USB_SPEED_UNKNOWN;
1893
1894 /* this "gadget" abstracts/virtualizes the controller */
Kay Sievers427c4f32008-11-07 01:52:53 +01001895 dev_set_name(&musb->g.dev, "gadget");
Felipe Balbi550a7372008-07-24 12:27:36 +03001896 musb->g.dev.parent = musb->controller;
1897 musb->g.dev.dma_mask = musb->controller->dma_mask;
1898 musb->g.dev.release = musb_gadget_release;
1899 musb->g.name = musb_driver_name;
1900
Felipe Balbi032ec492011-11-24 15:46:26 +02001901 musb->g.is_otg = 1;
Felipe Balbi550a7372008-07-24 12:27:36 +03001902
1903 musb_g_init_endpoints(musb);
1904
1905 musb->is_active = 0;
1906 musb_platform_try_idle(musb, 0);
1907
1908 status = device_register(&musb->g.dev);
Rahul Ruikare2c34042010-10-02 01:35:48 -05001909 if (status != 0) {
1910 put_device(&musb->g.dev);
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001911 return status;
Rahul Ruikare2c34042010-10-02 01:35:48 -05001912 }
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001913 status = usb_add_gadget_udc(musb->controller, &musb->g);
1914 if (status)
1915 goto err;
1916
1917 return 0;
1918err:
Sebastian Andrzej Siewior6193d692011-08-10 11:01:57 +02001919 musb->g.dev.parent = NULL;
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001920 device_unregister(&musb->g.dev);
Felipe Balbi550a7372008-07-24 12:27:36 +03001921 return status;
1922}
1923
1924void musb_gadget_cleanup(struct musb *musb)
1925{
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03001926 usb_del_gadget_udc(&musb->g);
Sebastian Andrzej Siewior6193d692011-08-10 11:01:57 +02001927 if (musb->g.dev.parent)
1928 device_unregister(&musb->g.dev);
Felipe Balbi550a7372008-07-24 12:27:36 +03001929}
1930
1931/*
1932 * Register the gadget driver. Used by gadget drivers when
1933 * registering themselves with the controller.
1934 *
1935 * -EINVAL something went wrong (not driver)
1936 * -EBUSY another gadget is already using the controller
Uwe Kleine-Königb5950762010-11-01 15:38:34 -04001937 * -ENOMEM no memory to perform the operation
Felipe Balbi550a7372008-07-24 12:27:36 +03001938 *
1939 * @param driver the gadget driver
1940 * @return <0 if error, 0 if everything is fine
1941 */
Sebastian Andrzej Siewiore71eb392011-06-23 14:26:16 +02001942static int musb_gadget_start(struct usb_gadget *g,
1943 struct usb_gadget_driver *driver)
Felipe Balbi550a7372008-07-24 12:27:36 +03001944{
Sebastian Andrzej Siewiore71eb392011-06-23 14:26:16 +02001945 struct musb *musb = gadget_to_musb(g);
Heikki Krogerusd445b6d2012-02-13 13:24:15 +02001946 struct usb_otg *otg = musb->xceiv->otg;
Felipe Balbi032ec492011-11-24 15:46:26 +02001947 struct usb_hcd *hcd = musb_to_hcd(musb);
Felipe Balbi63eed2b2011-01-17 10:34:38 +02001948 unsigned long flags;
Felipe Balbi032ec492011-11-24 15:46:26 +02001949 int retval = 0;
Felipe Balbi550a7372008-07-24 12:27:36 +03001950
Felipe Balbi032ec492011-11-24 15:46:26 +02001951 if (driver->max_speed < USB_SPEED_HIGH) {
1952 retval = -EINVAL;
1953 goto err;
1954 }
Felipe Balbi550a7372008-07-24 12:27:36 +03001955
Hema HK7acc6192011-02-28 14:19:34 +05301956 pm_runtime_get_sync(musb->controller);
1957
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03001958 dev_dbg(musb->controller, "registering driver %s\n", driver->function);
Felipe Balbi550a7372008-07-24 12:27:36 +03001959
Sebastian Andrzej Siewiore71eb392011-06-23 14:26:16 +02001960 musb->softconnect = 0;
Felipe Balbi63eed2b2011-01-17 10:34:38 +02001961 musb->gadget_driver = driver;
Felipe Balbi63eed2b2011-01-17 10:34:38 +02001962
1963 spin_lock_irqsave(&musb->lock, flags);
Sebastian Andrzej Siewiore71eb392011-06-23 14:26:16 +02001964 musb->is_active = 1;
Felipe Balbi63eed2b2011-01-17 10:34:38 +02001965
Heikki Krogerus6e13c652012-02-13 13:24:20 +02001966 otg_set_peripheral(otg, &musb->g);
Felipe Balbi63eed2b2011-01-17 10:34:38 +02001967 musb->xceiv->state = OTG_STATE_B_IDLE;
Felipe Balbi550a7372008-07-24 12:27:36 +03001968 spin_unlock_irqrestore(&musb->lock, flags);
1969
Felipe Balbi032ec492011-11-24 15:46:26 +02001970 /* REVISIT: funcall to other code, which also
1971 * handles power budgeting ... this way also
1972 * ensures HdrcStart is indirectly called.
1973 */
1974 retval = usb_add_hcd(hcd, 0, 0);
1975 if (retval < 0) {
1976 dev_dbg(musb->controller, "add_hcd failed, %d\n", retval);
1977 goto err;
Felipe Balbi550a7372008-07-24 12:27:36 +03001978 }
Felipe Balbi032ec492011-11-24 15:46:26 +02001979
1980 if ((musb->xceiv->last_event == USB_EVENT_ID)
1981 && otg->set_vbus)
1982 otg_set_vbus(otg, 1);
1983
1984 hcd->self.uses_pio_for_control = 1;
1985
Jarkko Nikulacdefce12011-04-29 16:17:35 +03001986 if (musb->xceiv->last_event == USB_EVENT_NONE)
1987 pm_runtime_put(musb->controller);
Felipe Balbi550a7372008-07-24 12:27:36 +03001988
Felipe Balbi63eed2b2011-01-17 10:34:38 +02001989 return 0;
1990
Felipe Balbi032ec492011-11-24 15:46:26 +02001991err:
Felipe Balbi550a7372008-07-24 12:27:36 +03001992 return retval;
1993}
Felipe Balbi550a7372008-07-24 12:27:36 +03001994
1995static void stop_activity(struct musb *musb, struct usb_gadget_driver *driver)
1996{
1997 int i;
1998 struct musb_hw_ep *hw_ep;
1999
2000 /* don't disconnect if it's not connected */
2001 if (musb->g.speed == USB_SPEED_UNKNOWN)
2002 driver = NULL;
2003 else
2004 musb->g.speed = USB_SPEED_UNKNOWN;
2005
2006 /* deactivate the hardware */
2007 if (musb->softconnect) {
2008 musb->softconnect = 0;
2009 musb_pullup(musb, 0);
2010 }
2011 musb_stop(musb);
2012
2013 /* killing any outstanding requests will quiesce the driver;
2014 * then report disconnect
2015 */
2016 if (driver) {
2017 for (i = 0, hw_ep = musb->endpoints;
2018 i < musb->nr_endpoints;
2019 i++, hw_ep++) {
2020 musb_ep_select(musb->mregs, i);
2021 if (hw_ep->is_shared_fifo /* || !epnum */) {
2022 nuke(&hw_ep->ep_in, -ESHUTDOWN);
2023 } else {
2024 if (hw_ep->max_packet_sz_tx)
2025 nuke(&hw_ep->ep_in, -ESHUTDOWN);
2026 if (hw_ep->max_packet_sz_rx)
2027 nuke(&hw_ep->ep_out, -ESHUTDOWN);
2028 }
2029 }
Felipe Balbi550a7372008-07-24 12:27:36 +03002030 }
2031}
2032
2033/*
2034 * Unregister the gadget driver. Used by gadget drivers when
2035 * unregistering themselves from the controller.
2036 *
2037 * @param driver the gadget driver to unregister
2038 */
Sebastian Andrzej Siewiore71eb392011-06-23 14:26:16 +02002039static int musb_gadget_stop(struct usb_gadget *g,
2040 struct usb_gadget_driver *driver)
Felipe Balbi550a7372008-07-24 12:27:36 +03002041{
Sebastian Andrzej Siewiore71eb392011-06-23 14:26:16 +02002042 struct musb *musb = gadget_to_musb(g);
Felipe Balbi63eed2b2011-01-17 10:34:38 +02002043 unsigned long flags;
Felipe Balbi550a7372008-07-24 12:27:36 +03002044
Hema HK7acc6192011-02-28 14:19:34 +05302045 if (musb->xceiv->last_event == USB_EVENT_NONE)
2046 pm_runtime_get_sync(musb->controller);
2047
Felipe Balbi63eed2b2011-01-17 10:34:38 +02002048 /*
2049 * REVISIT always use otg_set_peripheral() here too;
Felipe Balbi550a7372008-07-24 12:27:36 +03002050 * this needs to shut down the OTG engine.
2051 */
2052
2053 spin_lock_irqsave(&musb->lock, flags);
2054
Felipe Balbi550a7372008-07-24 12:27:36 +03002055 musb_hnp_stop(musb);
Felipe Balbi550a7372008-07-24 12:27:36 +03002056
Felipe Balbi63eed2b2011-01-17 10:34:38 +02002057 (void) musb_gadget_vbus_draw(&musb->g, 0);
Felipe Balbi550a7372008-07-24 12:27:36 +03002058
Felipe Balbi63eed2b2011-01-17 10:34:38 +02002059 musb->xceiv->state = OTG_STATE_UNDEFINED;
2060 stop_activity(musb, driver);
Heikki Krogerus6e13c652012-02-13 13:24:20 +02002061 otg_set_peripheral(musb->xceiv->otg, NULL);
Felipe Balbi550a7372008-07-24 12:27:36 +03002062
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03002063 dev_dbg(musb->controller, "unregistering driver %s\n", driver->function);
Felipe Balbi550a7372008-07-24 12:27:36 +03002064
Felipe Balbi63eed2b2011-01-17 10:34:38 +02002065 musb->is_active = 0;
2066 musb_platform_try_idle(musb, 0);
Felipe Balbi550a7372008-07-24 12:27:36 +03002067 spin_unlock_irqrestore(&musb->lock, flags);
2068
Felipe Balbi032ec492011-11-24 15:46:26 +02002069 usb_remove_hcd(musb_to_hcd(musb));
2070 /*
2071 * FIXME we need to be able to register another
2072 * gadget driver here and have everything work;
2073 * that currently misbehaves.
2074 */
Felipe Balbi63eed2b2011-01-17 10:34:38 +02002075
Hema HK7acc6192011-02-28 14:19:34 +05302076 pm_runtime_put(musb->controller);
2077
Felipe Balbi63eed2b2011-01-17 10:34:38 +02002078 return 0;
Felipe Balbi550a7372008-07-24 12:27:36 +03002079}
Felipe Balbi550a7372008-07-24 12:27:36 +03002080
2081/* ----------------------------------------------------------------------- */
2082
2083/* lifecycle operations called through plat_uds.c */
2084
2085void musb_g_resume(struct musb *musb)
2086{
2087 musb->is_suspended = 0;
David Brownell84e250f2009-03-31 12:30:04 -07002088 switch (musb->xceiv->state) {
Felipe Balbi550a7372008-07-24 12:27:36 +03002089 case OTG_STATE_B_IDLE:
2090 break;
2091 case OTG_STATE_B_WAIT_ACON:
2092 case OTG_STATE_B_PERIPHERAL:
2093 musb->is_active = 1;
2094 if (musb->gadget_driver && musb->gadget_driver->resume) {
2095 spin_unlock(&musb->lock);
2096 musb->gadget_driver->resume(&musb->g);
2097 spin_lock(&musb->lock);
2098 }
2099 break;
2100 default:
2101 WARNING("unhandled RESUME transition (%s)\n",
Anatolij Gustschin3df00452011-05-05 12:11:21 +02002102 otg_state_string(musb->xceiv->state));
Felipe Balbi550a7372008-07-24 12:27:36 +03002103 }
2104}
2105
2106/* called when SOF packets stop for 3+ msec */
2107void musb_g_suspend(struct musb *musb)
2108{
2109 u8 devctl;
2110
2111 devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03002112 dev_dbg(musb->controller, "devctl %02x\n", devctl);
Felipe Balbi550a7372008-07-24 12:27:36 +03002113
David Brownell84e250f2009-03-31 12:30:04 -07002114 switch (musb->xceiv->state) {
Felipe Balbi550a7372008-07-24 12:27:36 +03002115 case OTG_STATE_B_IDLE:
2116 if ((devctl & MUSB_DEVCTL_VBUS) == MUSB_DEVCTL_VBUS)
David Brownell84e250f2009-03-31 12:30:04 -07002117 musb->xceiv->state = OTG_STATE_B_PERIPHERAL;
Felipe Balbi550a7372008-07-24 12:27:36 +03002118 break;
2119 case OTG_STATE_B_PERIPHERAL:
2120 musb->is_suspended = 1;
2121 if (musb->gadget_driver && musb->gadget_driver->suspend) {
2122 spin_unlock(&musb->lock);
2123 musb->gadget_driver->suspend(&musb->g);
2124 spin_lock(&musb->lock);
2125 }
2126 break;
2127 default:
2128 /* REVISIT if B_HOST, clear DEVCTL.HOSTREQ;
2129 * A_PERIPHERAL may need care too
2130 */
2131 WARNING("unhandled SUSPEND transition (%s)\n",
Anatolij Gustschin3df00452011-05-05 12:11:21 +02002132 otg_state_string(musb->xceiv->state));
Felipe Balbi550a7372008-07-24 12:27:36 +03002133 }
2134}
2135
2136/* Called during SRP */
2137void musb_g_wakeup(struct musb *musb)
2138{
2139 musb_gadget_wakeup(&musb->g);
2140}
2141
2142/* called when VBUS drops below session threshold, and in other cases */
2143void musb_g_disconnect(struct musb *musb)
2144{
2145 void __iomem *mregs = musb->mregs;
2146 u8 devctl = musb_readb(mregs, MUSB_DEVCTL);
2147
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03002148 dev_dbg(musb->controller, "devctl %02x\n", devctl);
Felipe Balbi550a7372008-07-24 12:27:36 +03002149
2150 /* clear HR */
2151 musb_writeb(mregs, MUSB_DEVCTL, devctl & MUSB_DEVCTL_SESSION);
2152
2153 /* don't draw vbus until new b-default session */
2154 (void) musb_gadget_vbus_draw(&musb->g, 0);
2155
2156 musb->g.speed = USB_SPEED_UNKNOWN;
2157 if (musb->gadget_driver && musb->gadget_driver->disconnect) {
2158 spin_unlock(&musb->lock);
2159 musb->gadget_driver->disconnect(&musb->g);
2160 spin_lock(&musb->lock);
2161 }
2162
David Brownell84e250f2009-03-31 12:30:04 -07002163 switch (musb->xceiv->state) {
Felipe Balbi550a7372008-07-24 12:27:36 +03002164 default:
Felipe Balbi5c8a86e2011-05-11 12:44:08 +03002165 dev_dbg(musb->controller, "Unhandled disconnect %s, setting a_idle\n",
Anatolij Gustschin3df00452011-05-05 12:11:21 +02002166 otg_state_string(musb->xceiv->state));
David Brownell84e250f2009-03-31 12:30:04 -07002167 musb->xceiv->state = OTG_STATE_A_IDLE;
David Brownellab983f2a2009-03-31 12:35:09 -07002168 MUSB_HST_MODE(musb);
Felipe Balbi550a7372008-07-24 12:27:36 +03002169 break;
2170 case OTG_STATE_A_PERIPHERAL:
David Brownell1de00da2009-04-02 10:16:11 -07002171 musb->xceiv->state = OTG_STATE_A_WAIT_BCON;
David Brownellab983f2a2009-03-31 12:35:09 -07002172 MUSB_HST_MODE(musb);
Felipe Balbi550a7372008-07-24 12:27:36 +03002173 break;
2174 case OTG_STATE_B_WAIT_ACON:
2175 case OTG_STATE_B_HOST:
Felipe Balbi550a7372008-07-24 12:27:36 +03002176 case OTG_STATE_B_PERIPHERAL:
2177 case OTG_STATE_B_IDLE:
David Brownell84e250f2009-03-31 12:30:04 -07002178 musb->xceiv->state = OTG_STATE_B_IDLE;
Felipe Balbi550a7372008-07-24 12:27:36 +03002179 break;
2180 case OTG_STATE_B_SRP_INIT:
2181 break;
2182 }
2183
2184 musb->is_active = 0;
2185}
2186
2187void musb_g_reset(struct musb *musb)
2188__releases(musb->lock)
2189__acquires(musb->lock)
2190{
2191 void __iomem *mbase = musb->mregs;
2192 u8 devctl = musb_readb(mbase, MUSB_DEVCTL);
2193 u8 power;
2194
Sebastian Andrzej Siewior515ba292012-10-30 19:52:24 +01002195 dev_dbg(musb->controller, "<== %s driver '%s'\n",
Felipe Balbi550a7372008-07-24 12:27:36 +03002196 (devctl & MUSB_DEVCTL_BDEVICE)
2197 ? "B-Device" : "A-Device",
Felipe Balbi550a7372008-07-24 12:27:36 +03002198 musb->gadget_driver
2199 ? musb->gadget_driver->driver.name
2200 : NULL
2201 );
2202
2203 /* report disconnect, if we didn't already (flushing EP state) */
2204 if (musb->g.speed != USB_SPEED_UNKNOWN)
2205 musb_g_disconnect(musb);
2206
2207 /* clear HR */
2208 else if (devctl & MUSB_DEVCTL_HR)
2209 musb_writeb(mbase, MUSB_DEVCTL, MUSB_DEVCTL_SESSION);
2210
2211
2212 /* what speed did we negotiate? */
2213 power = musb_readb(mbase, MUSB_POWER);
2214 musb->g.speed = (power & MUSB_POWER_HSMODE)
2215 ? USB_SPEED_HIGH : USB_SPEED_FULL;
2216
2217 /* start in USB_STATE_DEFAULT */
2218 musb->is_active = 1;
2219 musb->is_suspended = 0;
2220 MUSB_DEV_MODE(musb);
2221 musb->address = 0;
2222 musb->ep0_state = MUSB_EP0_STAGE_SETUP;
2223
2224 musb->may_wakeup = 0;
2225 musb->g.b_hnp_enable = 0;
2226 musb->g.a_alt_hnp_support = 0;
2227 musb->g.a_hnp_support = 0;
2228
2229 /* Normal reset, as B-Device;
2230 * or else after HNP, as A-Device
2231 */
2232 if (devctl & MUSB_DEVCTL_BDEVICE) {
David Brownell84e250f2009-03-31 12:30:04 -07002233 musb->xceiv->state = OTG_STATE_B_PERIPHERAL;
Felipe Balbi550a7372008-07-24 12:27:36 +03002234 musb->g.is_a_peripheral = 0;
Felipe Balbi032ec492011-11-24 15:46:26 +02002235 } else {
David Brownell84e250f2009-03-31 12:30:04 -07002236 musb->xceiv->state = OTG_STATE_A_PERIPHERAL;
Felipe Balbi550a7372008-07-24 12:27:36 +03002237 musb->g.is_a_peripheral = 1;
Felipe Balbi032ec492011-11-24 15:46:26 +02002238 }
Felipe Balbi550a7372008-07-24 12:27:36 +03002239
2240 /* start with default limits on VBUS power draw */
Felipe Balbi032ec492011-11-24 15:46:26 +02002241 (void) musb_gadget_vbus_draw(&musb->g, 8);
Felipe Balbi550a7372008-07-24 12:27:36 +03002242}