blob: 35d2123dddeeb78a04fea121d46d3820ee8f2085 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0
Ilya Yanok06bb9202012-11-06 13:48:21 +00002/*
3 * MUSB OTG driver peripheral support
4 *
5 * Copyright 2005 Mentor Graphics Corporation
6 * Copyright (C) 2005-2006 by Texas Instruments
7 * Copyright (C) 2006-2007 Nokia Corporation
8 * Copyright (C) 2009 MontaVista Software, Inc. <source@mvista.com>
Ilya Yanok06bb9202012-11-06 13:48:21 +00009 */
10
Ilya Yanok06bb9202012-11-06 13:48:21 +000011#ifndef __UBOOT__
Simon Glass9bc15642020-02-03 07:36:16 -070012#include <dm/device_compat.h>
Simon Glassd66c5f72020-02-03 07:36:15 -070013#include <dm/devres.h>
Ilya Yanok06bb9202012-11-06 13:48:21 +000014#include <linux/kernel.h>
15#include <linux/list.h>
16#include <linux/timer.h>
17#include <linux/module.h>
18#include <linux/smp.h>
19#include <linux/spinlock.h>
20#include <linux/delay.h>
21#include <linux/dma-mapping.h>
22#include <linux/slab.h>
23#else
24#include <common.h>
25#include <linux/usb/ch9.h>
26#include "linux-compat.h"
27#endif
28
29#include "musb_core.h"
30
31
32/* MUSB PERIPHERAL status 3-mar-2006:
33 *
34 * - EP0 seems solid. It passes both USBCV and usbtest control cases.
35 * Minor glitches:
36 *
37 * + remote wakeup to Linux hosts work, but saw USBCV failures;
38 * in one test run (operator error?)
39 * + endpoint halt tests -- in both usbtest and usbcv -- seem
40 * to break when dma is enabled ... is something wrongly
41 * clearing SENDSTALL?
42 *
43 * - Mass storage behaved ok when last tested. Network traffic patterns
44 * (with lots of short transfers etc) need retesting; they turn up the
45 * worst cases of the DMA, since short packets are typical but are not
46 * required.
47 *
48 * - TX/IN
49 * + both pio and dma behave in with network and g_zero tests
50 * + no cppi throughput issues other than no-hw-queueing
51 * + failed with FLAT_REG (DaVinci)
52 * + seems to behave with double buffering, PIO -and- CPPI
53 * + with gadgetfs + AIO, requests got lost?
54 *
55 * - RX/OUT
56 * + both pio and dma behave in with network and g_zero tests
57 * + dma is slow in typical case (short_not_ok is clear)
58 * + double buffering ok with PIO
59 * + double buffering *FAILS* with CPPI, wrong data bytes sometimes
60 * + request lossage observed with gadgetfs
61 *
62 * - ISO not tested ... might work, but only weakly isochronous
63 *
64 * - Gadget driver disabling of softconnect during bind() is ignored; so
65 * drivers can't hold off host requests until userspace is ready.
66 * (Workaround: they can turn it off later.)
67 *
68 * - PORTABILITY (assumes PIO works):
69 * + DaVinci, basically works with cppi dma
70 * + OMAP 2430, ditto with mentor dma
71 * + TUSB 6010, platform-specific dma in the works
72 */
73
74/* ----------------------------------------------------------------------- */
75
76#define is_buffer_mapped(req) (is_dma_capable() && \
77 (req->map_state != UN_MAPPED))
78
Paul Kocialkowskif34dfcb2015-08-04 17:04:06 +020079#ifndef CONFIG_USB_MUSB_PIO_ONLY
Ilya Yanok06bb9202012-11-06 13:48:21 +000080/* Maps the buffer to dma */
81
82static inline void map_dma_buffer(struct musb_request *request,
83 struct musb *musb, struct musb_ep *musb_ep)
84{
85 int compatible = true;
86 struct dma_controller *dma = musb->dma_controller;
87
88 request->map_state = UN_MAPPED;
89
90 if (!is_dma_capable() || !musb_ep->dma)
91 return;
92
93 /* Check if DMA engine can handle this request.
94 * DMA code must reject the USB request explicitly.
95 * Default behaviour is to map the request.
96 */
97 if (dma->is_compatible)
98 compatible = dma->is_compatible(musb_ep->dma,
99 musb_ep->packet_sz, request->request.buf,
100 request->request.length);
101 if (!compatible)
102 return;
103
104 if (request->request.dma == DMA_ADDR_INVALID) {
105 request->request.dma = dma_map_single(
106 musb->controller,
107 request->request.buf,
108 request->request.length,
109 request->tx
110 ? DMA_TO_DEVICE
111 : DMA_FROM_DEVICE);
112 request->map_state = MUSB_MAPPED;
113 } else {
114 dma_sync_single_for_device(musb->controller,
115 request->request.dma,
116 request->request.length,
117 request->tx
118 ? DMA_TO_DEVICE
119 : DMA_FROM_DEVICE);
120 request->map_state = PRE_MAPPED;
121 }
122}
123
124/* Unmap the buffer from dma and maps it back to cpu */
125static inline void unmap_dma_buffer(struct musb_request *request,
126 struct musb *musb)
127{
128 if (!is_buffer_mapped(request))
129 return;
130
131 if (request->request.dma == DMA_ADDR_INVALID) {
132 dev_vdbg(musb->controller,
133 "not unmapping a never mapped buffer\n");
134 return;
135 }
136 if (request->map_state == MUSB_MAPPED) {
137 dma_unmap_single(musb->controller,
138 request->request.dma,
139 request->request.length,
140 request->tx
141 ? DMA_TO_DEVICE
142 : DMA_FROM_DEVICE);
143 request->request.dma = DMA_ADDR_INVALID;
144 } else { /* PRE_MAPPED */
145 dma_sync_single_for_cpu(musb->controller,
146 request->request.dma,
147 request->request.length,
148 request->tx
149 ? DMA_TO_DEVICE
150 : DMA_FROM_DEVICE);
151 }
152 request->map_state = UN_MAPPED;
153}
154#else
155static inline void map_dma_buffer(struct musb_request *request,
156 struct musb *musb, struct musb_ep *musb_ep)
157{
158}
159
160static inline void unmap_dma_buffer(struct musb_request *request,
161 struct musb *musb)
162{
163}
164#endif
165
166/*
167 * Immediately complete a request.
168 *
169 * @param request the request to complete
170 * @param status the status to complete the request with
171 * Context: controller locked, IRQs blocked.
172 */
173void musb_g_giveback(
174 struct musb_ep *ep,
175 struct usb_request *request,
176 int status)
177__releases(ep->musb->lock)
178__acquires(ep->musb->lock)
179{
180 struct musb_request *req;
181 struct musb *musb;
182 int busy = ep->busy;
183
184 req = to_musb_request(request);
185
186 list_del(&req->list);
187 if (req->request.status == -EINPROGRESS)
188 req->request.status = status;
189 musb = req->musb;
190
191 ep->busy = 1;
192 spin_unlock(&musb->lock);
193 unmap_dma_buffer(req, musb);
194 if (request->status == 0)
195 dev_dbg(musb->controller, "%s done request %p, %d/%d\n",
196 ep->end_point.name, request,
197 req->request.actual, req->request.length);
198 else
199 dev_dbg(musb->controller, "%s request %p, %d/%d fault %d\n",
200 ep->end_point.name, request,
201 req->request.actual, req->request.length,
202 request->status);
203 req->request.complete(&req->ep->end_point, &req->request);
204 spin_lock(&musb->lock);
205 ep->busy = busy;
206}
207
208/* ----------------------------------------------------------------------- */
209
210/*
211 * Abort requests queued to an endpoint using the status. Synchronous.
212 * caller locked controller and blocked irqs, and selected this ep.
213 */
214static void nuke(struct musb_ep *ep, const int status)
215{
216 struct musb *musb = ep->musb;
217 struct musb_request *req = NULL;
218 void __iomem *epio = ep->musb->endpoints[ep->current_epnum].regs;
219
220 ep->busy = 1;
221
222 if (is_dma_capable() && ep->dma) {
223 struct dma_controller *c = ep->musb->dma_controller;
224 int value;
225
226 if (ep->is_in) {
227 /*
228 * The programming guide says that we must not clear
229 * the DMAMODE bit before DMAENAB, so we only
230 * clear it in the second write...
231 */
232 musb_writew(epio, MUSB_TXCSR,
233 MUSB_TXCSR_DMAMODE | MUSB_TXCSR_FLUSHFIFO);
234 musb_writew(epio, MUSB_TXCSR,
235 0 | MUSB_TXCSR_FLUSHFIFO);
236 } else {
237 musb_writew(epio, MUSB_RXCSR,
238 0 | MUSB_RXCSR_FLUSHFIFO);
239 musb_writew(epio, MUSB_RXCSR,
240 0 | MUSB_RXCSR_FLUSHFIFO);
241 }
242
243 value = c->channel_abort(ep->dma);
244 dev_dbg(musb->controller, "%s: abort DMA --> %d\n",
245 ep->name, value);
246 c->channel_release(ep->dma);
247 ep->dma = NULL;
248 }
249
250 while (!list_empty(&ep->req_list)) {
251 req = list_first_entry(&ep->req_list, struct musb_request, list);
252 musb_g_giveback(ep, &req->request, status);
253 }
254}
255
256/* ----------------------------------------------------------------------- */
257
258/* Data transfers - pure PIO, pure DMA, or mixed mode */
259
260/*
261 * This assumes the separate CPPI engine is responding to DMA requests
262 * from the usb core ... sequenced a bit differently from mentor dma.
263 */
264
265static inline int max_ep_writesize(struct musb *musb, struct musb_ep *ep)
266{
267 if (can_bulk_split(musb, ep->type))
268 return ep->hw_ep->max_packet_sz_tx;
269 else
270 return ep->packet_sz;
271}
272
273
274#ifdef CONFIG_USB_INVENTRA_DMA
275
276/* Peripheral tx (IN) using Mentor DMA works as follows:
277 Only mode 0 is used for transfers <= wPktSize,
278 mode 1 is used for larger transfers,
279
280 One of the following happens:
281 - Host sends IN token which causes an endpoint interrupt
282 -> TxAvail
283 -> if DMA is currently busy, exit.
284 -> if queue is non-empty, txstate().
285
286 - Request is queued by the gadget driver.
287 -> if queue was previously empty, txstate()
288
289 txstate()
290 -> start
291 /\ -> setup DMA
292 | (data is transferred to the FIFO, then sent out when
293 | IN token(s) are recd from Host.
294 | -> DMA interrupt on completion
295 | calls TxAvail.
296 | -> stop DMA, ~DMAENAB,
297 | -> set TxPktRdy for last short pkt or zlp
298 | -> Complete Request
299 | -> Continue next request (call txstate)
300 |___________________________________|
301
302 * Non-Mentor DMA engines can of course work differently, such as by
303 * upleveling from irq-per-packet to irq-per-buffer.
304 */
305
306#endif
307
308/*
309 * An endpoint is transmitting data. This can be called either from
310 * the IRQ routine or from ep.queue() to kickstart a request on an
311 * endpoint.
312 *
313 * Context: controller locked, IRQs blocked, endpoint selected
314 */
315static void txstate(struct musb *musb, struct musb_request *req)
316{
317 u8 epnum = req->epnum;
318 struct musb_ep *musb_ep;
319 void __iomem *epio = musb->endpoints[epnum].regs;
320 struct usb_request *request;
321 u16 fifo_count = 0, csr;
322 int use_dma = 0;
323
324 musb_ep = req->ep;
325
326 /* Check if EP is disabled */
327 if (!musb_ep->desc) {
328 dev_dbg(musb->controller, "ep:%s disabled - ignore request\n",
329 musb_ep->end_point.name);
330 return;
331 }
332
333 /* we shouldn't get here while DMA is active ... but we do ... */
334 if (dma_channel_status(musb_ep->dma) == MUSB_DMA_STATUS_BUSY) {
335 dev_dbg(musb->controller, "dma pending...\n");
336 return;
337 }
338
339 /* read TXCSR before */
340 csr = musb_readw(epio, MUSB_TXCSR);
341
342 request = &req->request;
343 fifo_count = min(max_ep_writesize(musb, musb_ep),
344 (int)(request->length - request->actual));
345
346 if (csr & MUSB_TXCSR_TXPKTRDY) {
347 dev_dbg(musb->controller, "%s old packet still ready , txcsr %03x\n",
348 musb_ep->end_point.name, csr);
349 return;
350 }
351
352 if (csr & MUSB_TXCSR_P_SENDSTALL) {
353 dev_dbg(musb->controller, "%s stalling, txcsr %03x\n",
354 musb_ep->end_point.name, csr);
355 return;
356 }
357
358 dev_dbg(musb->controller, "hw_ep%d, maxpacket %d, fifo count %d, txcsr %03x\n",
359 epnum, musb_ep->packet_sz, fifo_count,
360 csr);
361
Paul Kocialkowskif34dfcb2015-08-04 17:04:06 +0200362#ifndef CONFIG_USB_MUSB_PIO_ONLY
Ilya Yanok06bb9202012-11-06 13:48:21 +0000363 if (is_buffer_mapped(req)) {
364 struct dma_controller *c = musb->dma_controller;
365 size_t request_size;
366
367 /* setup DMA, then program endpoint CSR */
368 request_size = min_t(size_t, request->length - request->actual,
369 musb_ep->dma->max_len);
370
371 use_dma = (request->dma != DMA_ADDR_INVALID);
372
373 /* MUSB_TXCSR_P_ISO is still set correctly */
374
375#if defined(CONFIG_USB_INVENTRA_DMA) || defined(CONFIG_USB_UX500_DMA)
376 {
377 if (request_size < musb_ep->packet_sz)
378 musb_ep->dma->desired_mode = 0;
379 else
380 musb_ep->dma->desired_mode = 1;
381
382 use_dma = use_dma && c->channel_program(
383 musb_ep->dma, musb_ep->packet_sz,
384 musb_ep->dma->desired_mode,
385 request->dma + request->actual, request_size);
386 if (use_dma) {
387 if (musb_ep->dma->desired_mode == 0) {
388 /*
389 * We must not clear the DMAMODE bit
390 * before the DMAENAB bit -- and the
391 * latter doesn't always get cleared
392 * before we get here...
393 */
394 csr &= ~(MUSB_TXCSR_AUTOSET
395 | MUSB_TXCSR_DMAENAB);
396 musb_writew(epio, MUSB_TXCSR, csr
397 | MUSB_TXCSR_P_WZC_BITS);
398 csr &= ~MUSB_TXCSR_DMAMODE;
399 csr |= (MUSB_TXCSR_DMAENAB |
400 MUSB_TXCSR_MODE);
401 /* against programming guide */
402 } else {
403 csr |= (MUSB_TXCSR_DMAENAB
404 | MUSB_TXCSR_DMAMODE
405 | MUSB_TXCSR_MODE);
406 if (!musb_ep->hb_mult)
407 csr |= MUSB_TXCSR_AUTOSET;
408 }
409 csr &= ~MUSB_TXCSR_P_UNDERRUN;
410
411 musb_writew(epio, MUSB_TXCSR, csr);
412 }
413 }
414
415#elif defined(CONFIG_USB_TI_CPPI_DMA)
416 /* program endpoint CSR first, then setup DMA */
417 csr &= ~(MUSB_TXCSR_P_UNDERRUN | MUSB_TXCSR_TXPKTRDY);
418 csr |= MUSB_TXCSR_DMAENAB | MUSB_TXCSR_DMAMODE |
419 MUSB_TXCSR_MODE;
420 musb_writew(epio, MUSB_TXCSR,
421 (MUSB_TXCSR_P_WZC_BITS & ~MUSB_TXCSR_P_UNDERRUN)
422 | csr);
423
424 /* ensure writebuffer is empty */
425 csr = musb_readw(epio, MUSB_TXCSR);
426
427 /* NOTE host side sets DMAENAB later than this; both are
428 * OK since the transfer dma glue (between CPPI and Mentor
429 * fifos) just tells CPPI it could start. Data only moves
430 * to the USB TX fifo when both fifos are ready.
431 */
432
433 /* "mode" is irrelevant here; handle terminating ZLPs like
434 * PIO does, since the hardware RNDIS mode seems unreliable
435 * except for the last-packet-is-already-short case.
436 */
437 use_dma = use_dma && c->channel_program(
438 musb_ep->dma, musb_ep->packet_sz,
439 0,
440 request->dma + request->actual,
441 request_size);
442 if (!use_dma) {
443 c->channel_release(musb_ep->dma);
444 musb_ep->dma = NULL;
445 csr &= ~MUSB_TXCSR_DMAENAB;
446 musb_writew(epio, MUSB_TXCSR, csr);
447 /* invariant: prequest->buf is non-null */
448 }
449#elif defined(CONFIG_USB_TUSB_OMAP_DMA)
450 use_dma = use_dma && c->channel_program(
451 musb_ep->dma, musb_ep->packet_sz,
452 request->zero,
453 request->dma + request->actual,
454 request_size);
455#endif
456 }
457#endif
458
459 if (!use_dma) {
460 /*
461 * Unmap the dma buffer back to cpu if dma channel
462 * programming fails
463 */
464 unmap_dma_buffer(req, musb);
465
466 musb_write_fifo(musb_ep->hw_ep, fifo_count,
467 (u8 *) (request->buf + request->actual));
468 request->actual += fifo_count;
469 csr |= MUSB_TXCSR_TXPKTRDY;
470 csr &= ~MUSB_TXCSR_P_UNDERRUN;
471 musb_writew(epio, MUSB_TXCSR, csr);
472 }
473
474 /* host may already have the data when this message shows... */
475 dev_dbg(musb->controller, "%s TX/IN %s len %d/%d, txcsr %04x, fifo %d/%d\n",
476 musb_ep->end_point.name, use_dma ? "dma" : "pio",
477 request->actual, request->length,
478 musb_readw(epio, MUSB_TXCSR),
479 fifo_count,
480 musb_readw(epio, MUSB_TXMAXP));
481}
482
483/*
484 * FIFO state update (e.g. data ready).
485 * Called from IRQ, with controller locked.
486 */
487void musb_g_tx(struct musb *musb, u8 epnum)
488{
489 u16 csr;
490 struct musb_request *req;
491 struct usb_request *request;
492 u8 __iomem *mbase = musb->mregs;
493 struct musb_ep *musb_ep = &musb->endpoints[epnum].ep_in;
494 void __iomem *epio = musb->endpoints[epnum].regs;
495 struct dma_channel *dma;
496
497 musb_ep_select(mbase, epnum);
498 req = next_request(musb_ep);
499 request = &req->request;
500
501 csr = musb_readw(epio, MUSB_TXCSR);
502 dev_dbg(musb->controller, "<== %s, txcsr %04x\n", musb_ep->end_point.name, csr);
503
504 dma = is_dma_capable() ? musb_ep->dma : NULL;
505
506 /*
507 * REVISIT: for high bandwidth, MUSB_TXCSR_P_INCOMPTX
508 * probably rates reporting as a host error.
509 */
510 if (csr & MUSB_TXCSR_P_SENTSTALL) {
511 csr |= MUSB_TXCSR_P_WZC_BITS;
512 csr &= ~MUSB_TXCSR_P_SENTSTALL;
513 musb_writew(epio, MUSB_TXCSR, csr);
514 return;
515 }
516
517 if (csr & MUSB_TXCSR_P_UNDERRUN) {
518 /* We NAKed, no big deal... little reason to care. */
519 csr |= MUSB_TXCSR_P_WZC_BITS;
520 csr &= ~(MUSB_TXCSR_P_UNDERRUN | MUSB_TXCSR_TXPKTRDY);
521 musb_writew(epio, MUSB_TXCSR, csr);
522 dev_vdbg(musb->controller, "underrun on ep%d, req %p\n",
523 epnum, request);
524 }
525
526 if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) {
527 /*
528 * SHOULD NOT HAPPEN... has with CPPI though, after
529 * changing SENDSTALL (and other cases); harmless?
530 */
531 dev_dbg(musb->controller, "%s dma still busy?\n", musb_ep->end_point.name);
532 return;
533 }
534
535 if (request) {
536 u8 is_dma = 0;
537
538 if (dma && (csr & MUSB_TXCSR_DMAENAB)) {
539 is_dma = 1;
540 csr |= MUSB_TXCSR_P_WZC_BITS;
541 csr &= ~(MUSB_TXCSR_DMAENAB | MUSB_TXCSR_P_UNDERRUN |
542 MUSB_TXCSR_TXPKTRDY | MUSB_TXCSR_AUTOSET);
543 musb_writew(epio, MUSB_TXCSR, csr);
544 /* Ensure writebuffer is empty. */
545 csr = musb_readw(epio, MUSB_TXCSR);
546 request->actual += musb_ep->dma->actual_len;
547 dev_dbg(musb->controller, "TXCSR%d %04x, DMA off, len %zu, req %p\n",
548 epnum, csr, musb_ep->dma->actual_len, request);
549 }
550
551 /*
552 * First, maybe a terminating short packet. Some DMA
553 * engines might handle this by themselves.
554 */
555 if ((request->zero && request->length
556 && (request->length % musb_ep->packet_sz == 0)
557 && (request->actual == request->length))
558#if defined(CONFIG_USB_INVENTRA_DMA) || defined(CONFIG_USB_UX500_DMA)
559 || (is_dma && (!dma->desired_mode ||
560 (request->actual &
561 (musb_ep->packet_sz - 1))))
562#endif
563 ) {
564 /*
565 * On DMA completion, FIFO may not be
566 * available yet...
567 */
568 if (csr & MUSB_TXCSR_TXPKTRDY)
569 return;
570
571 dev_dbg(musb->controller, "sending zero pkt\n");
572 musb_writew(epio, MUSB_TXCSR, MUSB_TXCSR_MODE
573 | MUSB_TXCSR_TXPKTRDY);
574 request->zero = 0;
575 }
576
577 if (request->actual == request->length) {
578 musb_g_giveback(musb_ep, request, 0);
579 /*
580 * In the giveback function the MUSB lock is
581 * released and acquired after sometime. During
582 * this time period the INDEX register could get
583 * changed by the gadget_queue function especially
584 * on SMP systems. Reselect the INDEX to be sure
585 * we are reading/modifying the right registers
586 */
587 musb_ep_select(mbase, epnum);
588 req = musb_ep->desc ? next_request(musb_ep) : NULL;
589 if (!req) {
590 dev_dbg(musb->controller, "%s idle now\n",
591 musb_ep->end_point.name);
592 return;
593 }
594 }
595
596 txstate(musb, req);
597 }
598}
599
600/* ------------------------------------------------------------ */
601
602#ifdef CONFIG_USB_INVENTRA_DMA
603
604/* Peripheral rx (OUT) using Mentor DMA works as follows:
605 - Only mode 0 is used.
606
607 - Request is queued by the gadget class driver.
608 -> if queue was previously empty, rxstate()
609
610 - Host sends OUT token which causes an endpoint interrupt
611 /\ -> RxReady
612 | -> if request queued, call rxstate
613 | /\ -> setup DMA
614 | | -> DMA interrupt on completion
615 | | -> RxReady
616 | | -> stop DMA
617 | | -> ack the read
618 | | -> if data recd = max expected
619 | | by the request, or host
620 | | sent a short packet,
621 | | complete the request,
622 | | and start the next one.
623 | |_____________________________________|
624 | else just wait for the host
625 | to send the next OUT token.
626 |__________________________________________________|
627
628 * Non-Mentor DMA engines can of course work differently.
629 */
630
631#endif
632
633/*
634 * Context: controller locked, IRQs blocked, endpoint selected
635 */
636static void rxstate(struct musb *musb, struct musb_request *req)
637{
638 const u8 epnum = req->epnum;
639 struct usb_request *request = &req->request;
640 struct musb_ep *musb_ep;
641 void __iomem *epio = musb->endpoints[epnum].regs;
642 unsigned fifo_count = 0;
643 u16 len;
644 u16 csr = musb_readw(epio, MUSB_RXCSR);
645 struct musb_hw_ep *hw_ep = &musb->endpoints[epnum];
646 u8 use_mode_1;
647
648 if (hw_ep->is_shared_fifo)
649 musb_ep = &hw_ep->ep_in;
650 else
651 musb_ep = &hw_ep->ep_out;
652
653 len = musb_ep->packet_sz;
654
655 /* Check if EP is disabled */
656 if (!musb_ep->desc) {
657 dev_dbg(musb->controller, "ep:%s disabled - ignore request\n",
658 musb_ep->end_point.name);
659 return;
660 }
661
662 /* We shouldn't get here while DMA is active, but we do... */
663 if (dma_channel_status(musb_ep->dma) == MUSB_DMA_STATUS_BUSY) {
664 dev_dbg(musb->controller, "DMA pending...\n");
665 return;
666 }
667
668 if (csr & MUSB_RXCSR_P_SENDSTALL) {
669 dev_dbg(musb->controller, "%s stalling, RXCSR %04x\n",
670 musb_ep->end_point.name, csr);
671 return;
672 }
673
674 if (is_cppi_enabled() && is_buffer_mapped(req)) {
675 struct dma_controller *c = musb->dma_controller;
676 struct dma_channel *channel = musb_ep->dma;
677
678 /* NOTE: CPPI won't actually stop advancing the DMA
679 * queue after short packet transfers, so this is almost
680 * always going to run as IRQ-per-packet DMA so that
681 * faults will be handled correctly.
682 */
683 if (c->channel_program(channel,
684 musb_ep->packet_sz,
685 !request->short_not_ok,
686 request->dma + request->actual,
687 request->length - request->actual)) {
688
689 /* make sure that if an rxpkt arrived after the irq,
690 * the cppi engine will be ready to take it as soon
691 * as DMA is enabled
692 */
693 csr &= ~(MUSB_RXCSR_AUTOCLEAR
694 | MUSB_RXCSR_DMAMODE);
695 csr |= MUSB_RXCSR_DMAENAB | MUSB_RXCSR_P_WZC_BITS;
696 musb_writew(epio, MUSB_RXCSR, csr);
697 return;
698 }
699 }
700
701 if (csr & MUSB_RXCSR_RXPKTRDY) {
702 len = musb_readw(epio, MUSB_RXCOUNT);
703
704 /*
705 * Enable Mode 1 on RX transfers only when short_not_ok flag
706 * is set. Currently short_not_ok flag is set only from
707 * file_storage and f_mass_storage drivers
708 */
709
710 if (request->short_not_ok && len == musb_ep->packet_sz)
711 use_mode_1 = 1;
712 else
713 use_mode_1 = 0;
714
715 if (request->actual < request->length) {
716#ifdef CONFIG_USB_INVENTRA_DMA
717 if (is_buffer_mapped(req)) {
718 struct dma_controller *c;
719 struct dma_channel *channel;
720 int use_dma = 0;
721
722 c = musb->dma_controller;
723 channel = musb_ep->dma;
724
725 /* We use DMA Req mode 0 in rx_csr, and DMA controller operates in
726 * mode 0 only. So we do not get endpoint interrupts due to DMA
727 * completion. We only get interrupts from DMA controller.
728 *
729 * We could operate in DMA mode 1 if we knew the size of the tranfer
730 * in advance. For mass storage class, request->length = what the host
731 * sends, so that'd work. But for pretty much everything else,
732 * request->length is routinely more than what the host sends. For
733 * most these gadgets, end of is signified either by a short packet,
734 * or filling the last byte of the buffer. (Sending extra data in
735 * that last pckate should trigger an overflow fault.) But in mode 1,
736 * we don't get DMA completion interrupt for short packets.
737 *
738 * Theoretically, we could enable DMAReq irq (MUSB_RXCSR_DMAMODE = 1),
739 * to get endpoint interrupt on every DMA req, but that didn't seem
740 * to work reliably.
741 *
742 * REVISIT an updated g_file_storage can set req->short_not_ok, which
743 * then becomes usable as a runtime "use mode 1" hint...
744 */
745
746 /* Experimental: Mode1 works with mass storage use cases */
747 if (use_mode_1) {
748 csr |= MUSB_RXCSR_AUTOCLEAR;
749 musb_writew(epio, MUSB_RXCSR, csr);
750 csr |= MUSB_RXCSR_DMAENAB;
751 musb_writew(epio, MUSB_RXCSR, csr);
752
753 /*
754 * this special sequence (enabling and then
755 * disabling MUSB_RXCSR_DMAMODE) is required
756 * to get DMAReq to activate
757 */
758 musb_writew(epio, MUSB_RXCSR,
759 csr | MUSB_RXCSR_DMAMODE);
760 musb_writew(epio, MUSB_RXCSR, csr);
761
762 } else {
763 if (!musb_ep->hb_mult &&
764 musb_ep->hw_ep->rx_double_buffered)
765 csr |= MUSB_RXCSR_AUTOCLEAR;
766 csr |= MUSB_RXCSR_DMAENAB;
767 musb_writew(epio, MUSB_RXCSR, csr);
768 }
769
770 if (request->actual < request->length) {
771 int transfer_size = 0;
772 if (use_mode_1) {
773 transfer_size = min(request->length - request->actual,
774 channel->max_len);
775 musb_ep->dma->desired_mode = 1;
776 } else {
777 transfer_size = min(request->length - request->actual,
778 (unsigned)len);
779 musb_ep->dma->desired_mode = 0;
780 }
781
782 use_dma = c->channel_program(
783 channel,
784 musb_ep->packet_sz,
785 channel->desired_mode,
786 request->dma
787 + request->actual,
788 transfer_size);
789 }
790
791 if (use_dma)
792 return;
793 }
794#elif defined(CONFIG_USB_UX500_DMA)
795 if ((is_buffer_mapped(req)) &&
796 (request->actual < request->length)) {
797
798 struct dma_controller *c;
799 struct dma_channel *channel;
800 int transfer_size = 0;
801
802 c = musb->dma_controller;
803 channel = musb_ep->dma;
804
805 /* In case first packet is short */
806 if (len < musb_ep->packet_sz)
807 transfer_size = len;
808 else if (request->short_not_ok)
809 transfer_size = min(request->length -
810 request->actual,
811 channel->max_len);
812 else
813 transfer_size = min(request->length -
814 request->actual,
815 (unsigned)len);
816
817 csr &= ~MUSB_RXCSR_DMAMODE;
818 csr |= (MUSB_RXCSR_DMAENAB |
819 MUSB_RXCSR_AUTOCLEAR);
820
821 musb_writew(epio, MUSB_RXCSR, csr);
822
823 if (transfer_size <= musb_ep->packet_sz) {
824 musb_ep->dma->desired_mode = 0;
825 } else {
826 musb_ep->dma->desired_mode = 1;
827 /* Mode must be set after DMAENAB */
828 csr |= MUSB_RXCSR_DMAMODE;
829 musb_writew(epio, MUSB_RXCSR, csr);
830 }
831
832 if (c->channel_program(channel,
833 musb_ep->packet_sz,
834 channel->desired_mode,
835 request->dma
836 + request->actual,
837 transfer_size))
838
839 return;
840 }
841#endif /* Mentor's DMA */
842
843 fifo_count = request->length - request->actual;
844 dev_dbg(musb->controller, "%s OUT/RX pio fifo %d/%d, maxpacket %d\n",
845 musb_ep->end_point.name,
846 len, fifo_count,
847 musb_ep->packet_sz);
848
849 fifo_count = min_t(unsigned, len, fifo_count);
850
851#ifdef CONFIG_USB_TUSB_OMAP_DMA
852 if (tusb_dma_omap() && is_buffer_mapped(req)) {
853 struct dma_controller *c = musb->dma_controller;
854 struct dma_channel *channel = musb_ep->dma;
855 u32 dma_addr = request->dma + request->actual;
856 int ret;
857
858 ret = c->channel_program(channel,
859 musb_ep->packet_sz,
860 channel->desired_mode,
861 dma_addr,
862 fifo_count);
863 if (ret)
864 return;
865 }
866#endif
867 /*
868 * Unmap the dma buffer back to cpu if dma channel
869 * programming fails. This buffer is mapped if the
870 * channel allocation is successful
871 */
872 if (is_buffer_mapped(req)) {
873 unmap_dma_buffer(req, musb);
874
875 /*
876 * Clear DMAENAB and AUTOCLEAR for the
877 * PIO mode transfer
878 */
879 csr &= ~(MUSB_RXCSR_DMAENAB | MUSB_RXCSR_AUTOCLEAR);
880 musb_writew(epio, MUSB_RXCSR, csr);
881 }
882
883 musb_read_fifo(musb_ep->hw_ep, fifo_count, (u8 *)
884 (request->buf + request->actual));
885 request->actual += fifo_count;
886
887 /* REVISIT if we left anything in the fifo, flush
888 * it and report -EOVERFLOW
889 */
890
891 /* ack the read! */
892 csr |= MUSB_RXCSR_P_WZC_BITS;
893 csr &= ~MUSB_RXCSR_RXPKTRDY;
894 musb_writew(epio, MUSB_RXCSR, csr);
895 }
896 }
897
898 /* reach the end or short packet detected */
899 if (request->actual == request->length || len < musb_ep->packet_sz)
900 musb_g_giveback(musb_ep, request, 0);
901}
902
903/*
904 * Data ready for a request; called from IRQ
905 */
906void musb_g_rx(struct musb *musb, u8 epnum)
907{
908 u16 csr;
909 struct musb_request *req;
910 struct usb_request *request;
911 void __iomem *mbase = musb->mregs;
912 struct musb_ep *musb_ep;
913 void __iomem *epio = musb->endpoints[epnum].regs;
914 struct dma_channel *dma;
915 struct musb_hw_ep *hw_ep = &musb->endpoints[epnum];
916
917 if (hw_ep->is_shared_fifo)
918 musb_ep = &hw_ep->ep_in;
919 else
920 musb_ep = &hw_ep->ep_out;
921
922 musb_ep_select(mbase, epnum);
923
924 req = next_request(musb_ep);
925 if (!req)
926 return;
927
928 request = &req->request;
929
930 csr = musb_readw(epio, MUSB_RXCSR);
931 dma = is_dma_capable() ? musb_ep->dma : NULL;
932
933 dev_dbg(musb->controller, "<== %s, rxcsr %04x%s %p\n", musb_ep->end_point.name,
934 csr, dma ? " (dma)" : "", request);
935
936 if (csr & MUSB_RXCSR_P_SENTSTALL) {
937 csr |= MUSB_RXCSR_P_WZC_BITS;
938 csr &= ~MUSB_RXCSR_P_SENTSTALL;
939 musb_writew(epio, MUSB_RXCSR, csr);
940 return;
941 }
942
943 if (csr & MUSB_RXCSR_P_OVERRUN) {
944 /* csr |= MUSB_RXCSR_P_WZC_BITS; */
945 csr &= ~MUSB_RXCSR_P_OVERRUN;
946 musb_writew(epio, MUSB_RXCSR, csr);
947
948 dev_dbg(musb->controller, "%s iso overrun on %p\n", musb_ep->name, request);
949 if (request->status == -EINPROGRESS)
950 request->status = -EOVERFLOW;
951 }
952 if (csr & MUSB_RXCSR_INCOMPRX) {
953 /* REVISIT not necessarily an error */
954 dev_dbg(musb->controller, "%s, incomprx\n", musb_ep->end_point.name);
955 }
956
957 if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) {
958 /* "should not happen"; likely RXPKTRDY pending for DMA */
959 dev_dbg(musb->controller, "%s busy, csr %04x\n",
960 musb_ep->end_point.name, csr);
961 return;
962 }
963
964 if (dma && (csr & MUSB_RXCSR_DMAENAB)) {
965 csr &= ~(MUSB_RXCSR_AUTOCLEAR
966 | MUSB_RXCSR_DMAENAB
967 | MUSB_RXCSR_DMAMODE);
968 musb_writew(epio, MUSB_RXCSR,
969 MUSB_RXCSR_P_WZC_BITS | csr);
970
971 request->actual += musb_ep->dma->actual_len;
972
973 dev_dbg(musb->controller, "RXCSR%d %04x, dma off, %04x, len %zu, req %p\n",
974 epnum, csr,
975 musb_readw(epio, MUSB_RXCSR),
976 musb_ep->dma->actual_len, request);
977
978#if defined(CONFIG_USB_INVENTRA_DMA) || defined(CONFIG_USB_TUSB_OMAP_DMA) || \
979 defined(CONFIG_USB_UX500_DMA)
980 /* Autoclear doesn't clear RxPktRdy for short packets */
981 if ((dma->desired_mode == 0 && !hw_ep->rx_double_buffered)
982 || (dma->actual_len
983 & (musb_ep->packet_sz - 1))) {
984 /* ack the read! */
985 csr &= ~MUSB_RXCSR_RXPKTRDY;
986 musb_writew(epio, MUSB_RXCSR, csr);
987 }
988
989 /* incomplete, and not short? wait for next IN packet */
990 if ((request->actual < request->length)
991 && (musb_ep->dma->actual_len
992 == musb_ep->packet_sz)) {
993 /* In double buffer case, continue to unload fifo if
994 * there is Rx packet in FIFO.
995 **/
996 csr = musb_readw(epio, MUSB_RXCSR);
997 if ((csr & MUSB_RXCSR_RXPKTRDY) &&
998 hw_ep->rx_double_buffered)
999 goto exit;
1000 return;
1001 }
1002#endif
1003 musb_g_giveback(musb_ep, request, 0);
1004 /*
1005 * In the giveback function the MUSB lock is
1006 * released and acquired after sometime. During
1007 * this time period the INDEX register could get
1008 * changed by the gadget_queue function especially
1009 * on SMP systems. Reselect the INDEX to be sure
1010 * we are reading/modifying the right registers
1011 */
1012 musb_ep_select(mbase, epnum);
1013
1014 req = next_request(musb_ep);
1015 if (!req)
1016 return;
1017 }
1018#if defined(CONFIG_USB_INVENTRA_DMA) || defined(CONFIG_USB_TUSB_OMAP_DMA) || \
1019 defined(CONFIG_USB_UX500_DMA)
1020exit:
1021#endif
1022 /* Analyze request */
1023 rxstate(musb, req);
1024}
1025
1026/* ------------------------------------------------------------ */
1027
1028static int musb_gadget_enable(struct usb_ep *ep,
1029 const struct usb_endpoint_descriptor *desc)
1030{
1031 unsigned long flags;
1032 struct musb_ep *musb_ep;
1033 struct musb_hw_ep *hw_ep;
1034 void __iomem *regs;
1035 struct musb *musb;
1036 void __iomem *mbase;
1037 u8 epnum;
1038 u16 csr;
1039 unsigned tmp;
1040 int status = -EINVAL;
1041
1042 if (!ep || !desc)
1043 return -EINVAL;
1044
1045 musb_ep = to_musb_ep(ep);
1046 hw_ep = musb_ep->hw_ep;
1047 regs = hw_ep->regs;
1048 musb = musb_ep->musb;
1049 mbase = musb->mregs;
1050 epnum = musb_ep->current_epnum;
1051
1052 spin_lock_irqsave(&musb->lock, flags);
1053
1054 if (musb_ep->desc) {
1055 status = -EBUSY;
1056 goto fail;
1057 }
1058 musb_ep->type = usb_endpoint_type(desc);
1059
1060 /* check direction and (later) maxpacket size against endpoint */
1061 if (usb_endpoint_num(desc) != epnum)
1062 goto fail;
1063
1064 /* REVISIT this rules out high bandwidth periodic transfers */
1065 tmp = usb_endpoint_maxp(desc);
1066 if (tmp & ~0x07ff) {
1067 int ok;
1068
1069 if (usb_endpoint_dir_in(desc))
1070 ok = musb->hb_iso_tx;
1071 else
1072 ok = musb->hb_iso_rx;
1073
1074 if (!ok) {
1075 dev_dbg(musb->controller, "no support for high bandwidth ISO\n");
1076 goto fail;
1077 }
1078 musb_ep->hb_mult = (tmp >> 11) & 3;
1079 } else {
1080 musb_ep->hb_mult = 0;
1081 }
1082
1083 musb_ep->packet_sz = tmp & 0x7ff;
1084 tmp = musb_ep->packet_sz * (musb_ep->hb_mult + 1);
1085
1086 /* enable the interrupts for the endpoint, set the endpoint
1087 * packet size (or fail), set the mode, clear the fifo
1088 */
1089 musb_ep_select(mbase, epnum);
1090 if (usb_endpoint_dir_in(desc)) {
1091 u16 int_txe = musb_readw(mbase, MUSB_INTRTXE);
1092
1093 if (hw_ep->is_shared_fifo)
1094 musb_ep->is_in = 1;
1095 if (!musb_ep->is_in)
1096 goto fail;
1097
1098 if (tmp > hw_ep->max_packet_sz_tx) {
1099 dev_dbg(musb->controller, "packet size beyond hardware FIFO size\n");
1100 goto fail;
1101 }
1102
1103 int_txe |= (1 << epnum);
1104 musb_writew(mbase, MUSB_INTRTXE, int_txe);
1105
1106 /* REVISIT if can_bulk_split(), use by updating "tmp";
1107 * likewise high bandwidth periodic tx
1108 */
1109 /* Set TXMAXP with the FIFO size of the endpoint
1110 * to disable double buffering mode.
1111 */
1112 if (musb->double_buffer_not_ok)
1113 musb_writew(regs, MUSB_TXMAXP, hw_ep->max_packet_sz_tx);
1114 else
1115 musb_writew(regs, MUSB_TXMAXP, musb_ep->packet_sz
1116 | (musb_ep->hb_mult << 11));
1117
1118 csr = MUSB_TXCSR_MODE | MUSB_TXCSR_CLRDATATOG;
1119 if (musb_readw(regs, MUSB_TXCSR)
1120 & MUSB_TXCSR_FIFONOTEMPTY)
1121 csr |= MUSB_TXCSR_FLUSHFIFO;
1122 if (musb_ep->type == USB_ENDPOINT_XFER_ISOC)
1123 csr |= MUSB_TXCSR_P_ISO;
1124
1125 /* set twice in case of double buffering */
1126 musb_writew(regs, MUSB_TXCSR, csr);
1127 /* REVISIT may be inappropriate w/o FIFONOTEMPTY ... */
1128 musb_writew(regs, MUSB_TXCSR, csr);
1129
1130 } else {
1131 u16 int_rxe = musb_readw(mbase, MUSB_INTRRXE);
1132
1133 if (hw_ep->is_shared_fifo)
1134 musb_ep->is_in = 0;
1135 if (musb_ep->is_in)
1136 goto fail;
1137
1138 if (tmp > hw_ep->max_packet_sz_rx) {
1139 dev_dbg(musb->controller, "packet size beyond hardware FIFO size\n");
1140 goto fail;
1141 }
1142
1143 int_rxe |= (1 << epnum);
1144 musb_writew(mbase, MUSB_INTRRXE, int_rxe);
1145
1146 /* REVISIT if can_bulk_combine() use by updating "tmp"
1147 * likewise high bandwidth periodic rx
1148 */
1149 /* Set RXMAXP with the FIFO size of the endpoint
1150 * to disable double buffering mode.
1151 */
1152 if (musb->double_buffer_not_ok)
1153 musb_writew(regs, MUSB_RXMAXP, hw_ep->max_packet_sz_tx);
1154 else
1155 musb_writew(regs, MUSB_RXMAXP, musb_ep->packet_sz
1156 | (musb_ep->hb_mult << 11));
1157
1158 /* force shared fifo to OUT-only mode */
1159 if (hw_ep->is_shared_fifo) {
1160 csr = musb_readw(regs, MUSB_TXCSR);
1161 csr &= ~(MUSB_TXCSR_MODE | MUSB_TXCSR_TXPKTRDY);
1162 musb_writew(regs, MUSB_TXCSR, csr);
1163 }
1164
1165 csr = MUSB_RXCSR_FLUSHFIFO | MUSB_RXCSR_CLRDATATOG;
1166 if (musb_ep->type == USB_ENDPOINT_XFER_ISOC)
1167 csr |= MUSB_RXCSR_P_ISO;
1168 else if (musb_ep->type == USB_ENDPOINT_XFER_INT)
1169 csr |= MUSB_RXCSR_DISNYET;
1170
1171 /* set twice in case of double buffering */
1172 musb_writew(regs, MUSB_RXCSR, csr);
1173 musb_writew(regs, MUSB_RXCSR, csr);
1174 }
1175
1176 /* NOTE: all the I/O code _should_ work fine without DMA, in case
1177 * for some reason you run out of channels here.
1178 */
1179 if (is_dma_capable() && musb->dma_controller) {
1180 struct dma_controller *c = musb->dma_controller;
1181
1182 musb_ep->dma = c->channel_alloc(c, hw_ep,
1183 (desc->bEndpointAddress & USB_DIR_IN));
1184 } else
1185 musb_ep->dma = NULL;
1186
1187 musb_ep->desc = desc;
1188 musb_ep->busy = 0;
1189 musb_ep->wedged = 0;
1190 status = 0;
1191
1192 pr_debug("%s periph: enabled %s for %s %s, %smaxpacket %d\n",
1193 musb_driver_name, musb_ep->end_point.name,
1194 ({ char *s; switch (musb_ep->type) {
1195 case USB_ENDPOINT_XFER_BULK: s = "bulk"; break;
1196 case USB_ENDPOINT_XFER_INT: s = "int"; break;
1197 default: s = "iso"; break;
1198 }; s; }),
1199 musb_ep->is_in ? "IN" : "OUT",
1200 musb_ep->dma ? "dma, " : "",
1201 musb_ep->packet_sz);
1202
1203 schedule_work(&musb->irq_work);
1204
1205fail:
1206 spin_unlock_irqrestore(&musb->lock, flags);
1207 return status;
1208}
1209
1210/*
1211 * Disable an endpoint flushing all requests queued.
1212 */
1213static int musb_gadget_disable(struct usb_ep *ep)
1214{
1215 unsigned long flags;
1216 struct musb *musb;
1217 u8 epnum;
1218 struct musb_ep *musb_ep;
1219 void __iomem *epio;
1220 int status = 0;
1221
1222 musb_ep = to_musb_ep(ep);
1223 musb = musb_ep->musb;
1224 epnum = musb_ep->current_epnum;
1225 epio = musb->endpoints[epnum].regs;
1226
1227 spin_lock_irqsave(&musb->lock, flags);
1228 musb_ep_select(musb->mregs, epnum);
1229
1230 /* zero the endpoint sizes */
1231 if (musb_ep->is_in) {
1232 u16 int_txe = musb_readw(musb->mregs, MUSB_INTRTXE);
1233 int_txe &= ~(1 << epnum);
1234 musb_writew(musb->mregs, MUSB_INTRTXE, int_txe);
1235 musb_writew(epio, MUSB_TXMAXP, 0);
1236 } else {
1237 u16 int_rxe = musb_readw(musb->mregs, MUSB_INTRRXE);
1238 int_rxe &= ~(1 << epnum);
1239 musb_writew(musb->mregs, MUSB_INTRRXE, int_rxe);
1240 musb_writew(epio, MUSB_RXMAXP, 0);
1241 }
1242
1243 musb_ep->desc = NULL;
1244#ifndef __UBOOT__
1245 musb_ep->end_point.desc = NULL;
1246#endif
1247
1248 /* abort all pending DMA and requests */
1249 nuke(musb_ep, -ESHUTDOWN);
1250
1251 schedule_work(&musb->irq_work);
1252
1253 spin_unlock_irqrestore(&(musb->lock), flags);
1254
1255 dev_dbg(musb->controller, "%s\n", musb_ep->end_point.name);
1256
1257 return status;
1258}
1259
1260/*
1261 * Allocate a request for an endpoint.
1262 * Reused by ep0 code.
1263 */
1264struct usb_request *musb_alloc_request(struct usb_ep *ep, gfp_t gfp_flags)
1265{
1266 struct musb_ep *musb_ep = to_musb_ep(ep);
1267 struct musb *musb = musb_ep->musb;
1268 struct musb_request *request = NULL;
1269
1270 request = kzalloc(sizeof *request, gfp_flags);
1271 if (!request) {
1272 dev_dbg(musb->controller, "not enough memory\n");
1273 return NULL;
1274 }
1275
1276 request->request.dma = DMA_ADDR_INVALID;
1277 request->epnum = musb_ep->current_epnum;
1278 request->ep = musb_ep;
1279
1280 return &request->request;
1281}
1282
1283/*
1284 * Free a request
1285 * Reused by ep0 code.
1286 */
1287void musb_free_request(struct usb_ep *ep, struct usb_request *req)
1288{
1289 kfree(to_musb_request(req));
1290}
1291
1292static LIST_HEAD(buffers);
1293
1294struct free_record {
1295 struct list_head list;
1296 struct device *dev;
1297 unsigned bytes;
1298 dma_addr_t dma;
1299};
1300
1301/*
1302 * Context: controller locked, IRQs blocked.
1303 */
1304void musb_ep_restart(struct musb *musb, struct musb_request *req)
1305{
1306 dev_dbg(musb->controller, "<== %s request %p len %u on hw_ep%d\n",
1307 req->tx ? "TX/IN" : "RX/OUT",
1308 &req->request, req->request.length, req->epnum);
1309
1310 musb_ep_select(musb->mregs, req->epnum);
1311 if (req->tx)
1312 txstate(musb, req);
1313 else
1314 rxstate(musb, req);
1315}
1316
1317static int musb_gadget_queue(struct usb_ep *ep, struct usb_request *req,
1318 gfp_t gfp_flags)
1319{
1320 struct musb_ep *musb_ep;
1321 struct musb_request *request;
1322 struct musb *musb;
1323 int status = 0;
1324 unsigned long lockflags;
1325
1326 if (!ep || !req)
1327 return -EINVAL;
1328 if (!req->buf)
1329 return -ENODATA;
1330
1331 musb_ep = to_musb_ep(ep);
1332 musb = musb_ep->musb;
1333
1334 request = to_musb_request(req);
1335 request->musb = musb;
1336
1337 if (request->ep != musb_ep)
1338 return -EINVAL;
1339
1340 dev_dbg(musb->controller, "<== to %s request=%p\n", ep->name, req);
1341
1342 /* request is mine now... */
1343 request->request.actual = 0;
1344 request->request.status = -EINPROGRESS;
1345 request->epnum = musb_ep->current_epnum;
1346 request->tx = musb_ep->is_in;
1347
1348 map_dma_buffer(request, musb, musb_ep);
1349
1350 spin_lock_irqsave(&musb->lock, lockflags);
1351
1352 /* don't queue if the ep is down */
1353 if (!musb_ep->desc) {
1354 dev_dbg(musb->controller, "req %p queued to %s while ep %s\n",
1355 req, ep->name, "disabled");
1356 status = -ESHUTDOWN;
1357 goto cleanup;
1358 }
1359
1360 /* add request to the list */
1361 list_add_tail(&request->list, &musb_ep->req_list);
1362
1363 /* it this is the head of the queue, start i/o ... */
1364 if (!musb_ep->busy && &request->list == musb_ep->req_list.next)
1365 musb_ep_restart(musb, request);
1366
1367cleanup:
1368 spin_unlock_irqrestore(&musb->lock, lockflags);
1369 return status;
1370}
1371
1372static int musb_gadget_dequeue(struct usb_ep *ep, struct usb_request *request)
1373{
1374 struct musb_ep *musb_ep = to_musb_ep(ep);
1375 struct musb_request *req = to_musb_request(request);
1376 struct musb_request *r;
1377 unsigned long flags;
1378 int status = 0;
1379 struct musb *musb = musb_ep->musb;
1380
1381 if (!ep || !request || to_musb_request(request)->ep != musb_ep)
1382 return -EINVAL;
1383
1384 spin_lock_irqsave(&musb->lock, flags);
1385
1386 list_for_each_entry(r, &musb_ep->req_list, list) {
1387 if (r == req)
1388 break;
1389 }
1390 if (r != req) {
1391 dev_dbg(musb->controller, "request %p not queued to %s\n", request, ep->name);
1392 status = -EINVAL;
1393 goto done;
1394 }
1395
1396 /* if the hardware doesn't have the request, easy ... */
1397 if (musb_ep->req_list.next != &req->list || musb_ep->busy)
1398 musb_g_giveback(musb_ep, request, -ECONNRESET);
1399
1400 /* ... else abort the dma transfer ... */
1401 else if (is_dma_capable() && musb_ep->dma) {
1402 struct dma_controller *c = musb->dma_controller;
1403
1404 musb_ep_select(musb->mregs, musb_ep->current_epnum);
1405 if (c->channel_abort)
1406 status = c->channel_abort(musb_ep->dma);
1407 else
1408 status = -EBUSY;
1409 if (status == 0)
1410 musb_g_giveback(musb_ep, request, -ECONNRESET);
1411 } else {
1412 /* NOTE: by sticking to easily tested hardware/driver states,
1413 * we leave counting of in-flight packets imprecise.
1414 */
1415 musb_g_giveback(musb_ep, request, -ECONNRESET);
1416 }
1417
1418done:
1419 spin_unlock_irqrestore(&musb->lock, flags);
1420 return status;
1421}
1422
1423/*
1424 * Set or clear the halt bit of an endpoint. A halted enpoint won't tx/rx any
1425 * data but will queue requests.
1426 *
1427 * exported to ep0 code
1428 */
1429static int musb_gadget_set_halt(struct usb_ep *ep, int value)
1430{
1431 struct musb_ep *musb_ep = to_musb_ep(ep);
1432 u8 epnum = musb_ep->current_epnum;
1433 struct musb *musb = musb_ep->musb;
1434 void __iomem *epio = musb->endpoints[epnum].regs;
1435 void __iomem *mbase;
1436 unsigned long flags;
1437 u16 csr;
1438 struct musb_request *request;
1439 int status = 0;
1440
1441 if (!ep)
1442 return -EINVAL;
1443 mbase = musb->mregs;
1444
1445 spin_lock_irqsave(&musb->lock, flags);
1446
1447 if ((USB_ENDPOINT_XFER_ISOC == musb_ep->type)) {
1448 status = -EINVAL;
1449 goto done;
1450 }
1451
1452 musb_ep_select(mbase, epnum);
1453
1454 request = next_request(musb_ep);
1455 if (value) {
1456 if (request) {
1457 dev_dbg(musb->controller, "request in progress, cannot halt %s\n",
1458 ep->name);
1459 status = -EAGAIN;
1460 goto done;
1461 }
1462 /* Cannot portably stall with non-empty FIFO */
1463 if (musb_ep->is_in) {
1464 csr = musb_readw(epio, MUSB_TXCSR);
1465 if (csr & MUSB_TXCSR_FIFONOTEMPTY) {
1466 dev_dbg(musb->controller, "FIFO busy, cannot halt %s\n", ep->name);
1467 status = -EAGAIN;
1468 goto done;
1469 }
1470 }
1471 } else
1472 musb_ep->wedged = 0;
1473
1474 /* set/clear the stall and toggle bits */
1475 dev_dbg(musb->controller, "%s: %s stall\n", ep->name, value ? "set" : "clear");
1476 if (musb_ep->is_in) {
1477 csr = musb_readw(epio, MUSB_TXCSR);
1478 csr |= MUSB_TXCSR_P_WZC_BITS
1479 | MUSB_TXCSR_CLRDATATOG;
1480 if (value)
1481 csr |= MUSB_TXCSR_P_SENDSTALL;
1482 else
1483 csr &= ~(MUSB_TXCSR_P_SENDSTALL
1484 | MUSB_TXCSR_P_SENTSTALL);
1485 csr &= ~MUSB_TXCSR_TXPKTRDY;
1486 musb_writew(epio, MUSB_TXCSR, csr);
1487 } else {
1488 csr = musb_readw(epio, MUSB_RXCSR);
1489 csr |= MUSB_RXCSR_P_WZC_BITS
1490 | MUSB_RXCSR_FLUSHFIFO
1491 | MUSB_RXCSR_CLRDATATOG;
1492 if (value)
1493 csr |= MUSB_RXCSR_P_SENDSTALL;
1494 else
1495 csr &= ~(MUSB_RXCSR_P_SENDSTALL
1496 | MUSB_RXCSR_P_SENTSTALL);
1497 musb_writew(epio, MUSB_RXCSR, csr);
1498 }
1499
1500 /* maybe start the first request in the queue */
1501 if (!musb_ep->busy && !value && request) {
1502 dev_dbg(musb->controller, "restarting the request\n");
1503 musb_ep_restart(musb, request);
1504 }
1505
1506done:
1507 spin_unlock_irqrestore(&musb->lock, flags);
1508 return status;
1509}
1510
1511#ifndef __UBOOT__
1512/*
1513 * Sets the halt feature with the clear requests ignored
1514 */
1515static int musb_gadget_set_wedge(struct usb_ep *ep)
1516{
1517 struct musb_ep *musb_ep = to_musb_ep(ep);
1518
1519 if (!ep)
1520 return -EINVAL;
1521
1522 musb_ep->wedged = 1;
1523
1524 return usb_ep_set_halt(ep);
1525}
1526#endif
1527
1528static int musb_gadget_fifo_status(struct usb_ep *ep)
1529{
1530 struct musb_ep *musb_ep = to_musb_ep(ep);
1531 void __iomem *epio = musb_ep->hw_ep->regs;
1532 int retval = -EINVAL;
1533
1534 if (musb_ep->desc && !musb_ep->is_in) {
1535 struct musb *musb = musb_ep->musb;
1536 int epnum = musb_ep->current_epnum;
1537 void __iomem *mbase = musb->mregs;
1538 unsigned long flags;
1539
1540 spin_lock_irqsave(&musb->lock, flags);
1541
1542 musb_ep_select(mbase, epnum);
1543 /* FIXME return zero unless RXPKTRDY is set */
1544 retval = musb_readw(epio, MUSB_RXCOUNT);
1545
1546 spin_unlock_irqrestore(&musb->lock, flags);
1547 }
1548 return retval;
1549}
1550
1551static void musb_gadget_fifo_flush(struct usb_ep *ep)
1552{
1553 struct musb_ep *musb_ep = to_musb_ep(ep);
1554 struct musb *musb = musb_ep->musb;
1555 u8 epnum = musb_ep->current_epnum;
1556 void __iomem *epio = musb->endpoints[epnum].regs;
1557 void __iomem *mbase;
1558 unsigned long flags;
1559 u16 csr, int_txe;
1560
1561 mbase = musb->mregs;
1562
1563 spin_lock_irqsave(&musb->lock, flags);
1564 musb_ep_select(mbase, (u8) epnum);
1565
1566 /* disable interrupts */
1567 int_txe = musb_readw(mbase, MUSB_INTRTXE);
1568 musb_writew(mbase, MUSB_INTRTXE, int_txe & ~(1 << epnum));
1569
1570 if (musb_ep->is_in) {
1571 csr = musb_readw(epio, MUSB_TXCSR);
1572 if (csr & MUSB_TXCSR_FIFONOTEMPTY) {
1573 csr |= MUSB_TXCSR_FLUSHFIFO | MUSB_TXCSR_P_WZC_BITS;
1574 /*
1575 * Setting both TXPKTRDY and FLUSHFIFO makes controller
1576 * to interrupt current FIFO loading, but not flushing
1577 * the already loaded ones.
1578 */
1579 csr &= ~MUSB_TXCSR_TXPKTRDY;
1580 musb_writew(epio, MUSB_TXCSR, csr);
1581 /* REVISIT may be inappropriate w/o FIFONOTEMPTY ... */
1582 musb_writew(epio, MUSB_TXCSR, csr);
1583 }
1584 } else {
1585 csr = musb_readw(epio, MUSB_RXCSR);
1586 csr |= MUSB_RXCSR_FLUSHFIFO | MUSB_RXCSR_P_WZC_BITS;
1587 musb_writew(epio, MUSB_RXCSR, csr);
1588 musb_writew(epio, MUSB_RXCSR, csr);
1589 }
1590
1591 /* re-enable interrupt */
1592 musb_writew(mbase, MUSB_INTRTXE, int_txe);
1593 spin_unlock_irqrestore(&musb->lock, flags);
1594}
1595
1596static const struct usb_ep_ops musb_ep_ops = {
1597 .enable = musb_gadget_enable,
1598 .disable = musb_gadget_disable,
1599 .alloc_request = musb_alloc_request,
1600 .free_request = musb_free_request,
1601 .queue = musb_gadget_queue,
1602 .dequeue = musb_gadget_dequeue,
1603 .set_halt = musb_gadget_set_halt,
1604#ifndef __UBOOT__
1605 .set_wedge = musb_gadget_set_wedge,
1606#endif
1607 .fifo_status = musb_gadget_fifo_status,
1608 .fifo_flush = musb_gadget_fifo_flush
1609};
1610
1611/* ----------------------------------------------------------------------- */
1612
1613static int musb_gadget_get_frame(struct usb_gadget *gadget)
1614{
1615 struct musb *musb = gadget_to_musb(gadget);
1616
1617 return (int)musb_readw(musb->mregs, MUSB_FRAME);
1618}
1619
1620static int musb_gadget_wakeup(struct usb_gadget *gadget)
1621{
1622#ifndef __UBOOT__
1623 struct musb *musb = gadget_to_musb(gadget);
1624 void __iomem *mregs = musb->mregs;
1625 unsigned long flags;
1626 int status = -EINVAL;
1627 u8 power, devctl;
1628 int retries;
1629
1630 spin_lock_irqsave(&musb->lock, flags);
1631
1632 switch (musb->xceiv->state) {
1633 case OTG_STATE_B_PERIPHERAL:
1634 /* NOTE: OTG state machine doesn't include B_SUSPENDED;
1635 * that's part of the standard usb 1.1 state machine, and
1636 * doesn't affect OTG transitions.
1637 */
1638 if (musb->may_wakeup && musb->is_suspended)
1639 break;
1640 goto done;
1641 case OTG_STATE_B_IDLE:
1642 /* Start SRP ... OTG not required. */
1643 devctl = musb_readb(mregs, MUSB_DEVCTL);
1644 dev_dbg(musb->controller, "Sending SRP: devctl: %02x\n", devctl);
1645 devctl |= MUSB_DEVCTL_SESSION;
1646 musb_writeb(mregs, MUSB_DEVCTL, devctl);
1647 devctl = musb_readb(mregs, MUSB_DEVCTL);
1648 retries = 100;
1649 while (!(devctl & MUSB_DEVCTL_SESSION)) {
1650 devctl = musb_readb(mregs, MUSB_DEVCTL);
1651 if (retries-- < 1)
1652 break;
1653 }
1654 retries = 10000;
1655 while (devctl & MUSB_DEVCTL_SESSION) {
1656 devctl = musb_readb(mregs, MUSB_DEVCTL);
1657 if (retries-- < 1)
1658 break;
1659 }
1660
1661 spin_unlock_irqrestore(&musb->lock, flags);
1662 otg_start_srp(musb->xceiv->otg);
1663 spin_lock_irqsave(&musb->lock, flags);
1664
1665 /* Block idling for at least 1s */
1666 musb_platform_try_idle(musb,
1667 jiffies + msecs_to_jiffies(1 * HZ));
1668
1669 status = 0;
1670 goto done;
1671 default:
1672 dev_dbg(musb->controller, "Unhandled wake: %s\n",
1673 otg_state_string(musb->xceiv->state));
1674 goto done;
1675 }
1676
1677 status = 0;
1678
1679 power = musb_readb(mregs, MUSB_POWER);
1680 power |= MUSB_POWER_RESUME;
1681 musb_writeb(mregs, MUSB_POWER, power);
1682 dev_dbg(musb->controller, "issue wakeup\n");
1683
1684 /* FIXME do this next chunk in a timer callback, no udelay */
1685 mdelay(2);
1686
1687 power = musb_readb(mregs, MUSB_POWER);
1688 power &= ~MUSB_POWER_RESUME;
1689 musb_writeb(mregs, MUSB_POWER, power);
1690done:
1691 spin_unlock_irqrestore(&musb->lock, flags);
1692 return status;
1693#else
1694 return 0;
1695#endif
1696}
1697
1698static int
1699musb_gadget_set_self_powered(struct usb_gadget *gadget, int is_selfpowered)
1700{
1701 struct musb *musb = gadget_to_musb(gadget);
1702
1703 musb->is_self_powered = !!is_selfpowered;
1704 return 0;
1705}
1706
1707static void musb_pullup(struct musb *musb, int is_on)
1708{
1709 u8 power;
1710
1711 power = musb_readb(musb->mregs, MUSB_POWER);
1712 if (is_on)
1713 power |= MUSB_POWER_SOFTCONN;
1714 else
1715 power &= ~MUSB_POWER_SOFTCONN;
1716
1717 /* FIXME if on, HdrcStart; if off, HdrcStop */
1718
1719 dev_dbg(musb->controller, "gadget D+ pullup %s\n",
1720 is_on ? "on" : "off");
1721 musb_writeb(musb->mregs, MUSB_POWER, power);
1722}
1723
1724#if 0
1725static int musb_gadget_vbus_session(struct usb_gadget *gadget, int is_active)
1726{
1727 dev_dbg(musb->controller, "<= %s =>\n", __func__);
1728
1729 /*
1730 * FIXME iff driver's softconnect flag is set (as it is during probe,
1731 * though that can clear it), just musb_pullup().
1732 */
1733
1734 return -EINVAL;
1735}
1736#endif
1737
1738static int musb_gadget_vbus_draw(struct usb_gadget *gadget, unsigned mA)
1739{
1740#ifndef __UBOOT__
1741 struct musb *musb = gadget_to_musb(gadget);
1742
1743 if (!musb->xceiv->set_power)
1744 return -EOPNOTSUPP;
1745 return usb_phy_set_power(musb->xceiv, mA);
1746#else
1747 return 0;
1748#endif
1749}
1750
1751static int musb_gadget_pullup(struct usb_gadget *gadget, int is_on)
1752{
1753 struct musb *musb = gadget_to_musb(gadget);
1754 unsigned long flags;
1755
1756 is_on = !!is_on;
1757
1758 pm_runtime_get_sync(musb->controller);
1759
1760 /* NOTE: this assumes we are sensing vbus; we'd rather
1761 * not pullup unless the B-session is active.
1762 */
1763 spin_lock_irqsave(&musb->lock, flags);
1764 if (is_on != musb->softconnect) {
1765 musb->softconnect = is_on;
1766 musb_pullup(musb, is_on);
1767 }
1768 spin_unlock_irqrestore(&musb->lock, flags);
1769
1770 pm_runtime_put(musb->controller);
1771
1772 return 0;
1773}
1774
1775#ifndef __UBOOT__
1776static int musb_gadget_start(struct usb_gadget *g,
1777 struct usb_gadget_driver *driver);
1778static int musb_gadget_stop(struct usb_gadget *g,
1779 struct usb_gadget_driver *driver);
Jean-Jacques Hiblot57118f62018-12-04 11:30:57 +01001780#else
1781static int musb_gadget_stop(struct usb_gadget *g)
1782{
1783 struct musb *musb = gadget_to_musb(g);
1784
1785 musb_stop(musb);
1786 return 0;
1787}
Ilya Yanok06bb9202012-11-06 13:48:21 +00001788#endif
1789
1790static const struct usb_gadget_ops musb_gadget_operations = {
1791 .get_frame = musb_gadget_get_frame,
1792 .wakeup = musb_gadget_wakeup,
1793 .set_selfpowered = musb_gadget_set_self_powered,
1794 /* .vbus_session = musb_gadget_vbus_session, */
1795 .vbus_draw = musb_gadget_vbus_draw,
1796 .pullup = musb_gadget_pullup,
1797#ifndef __UBOOT__
1798 .udc_start = musb_gadget_start,
1799 .udc_stop = musb_gadget_stop,
Jean-Jacques Hiblot57118f62018-12-04 11:30:57 +01001800#else
1801 .udc_start = musb_gadget_start,
1802 .udc_stop = musb_gadget_stop,
Ilya Yanok06bb9202012-11-06 13:48:21 +00001803#endif
1804};
1805
1806/* ----------------------------------------------------------------------- */
1807
1808/* Registration */
1809
1810/* Only this registration code "knows" the rule (from USB standards)
1811 * about there being only one external upstream port. It assumes
1812 * all peripheral ports are external...
1813 */
1814
1815#ifndef __UBOOT__
1816static void musb_gadget_release(struct device *dev)
1817{
1818 /* kref_put(WHAT) */
1819 dev_dbg(dev, "%s\n", __func__);
1820}
1821#endif
1822
1823
1824static void __devinit
1825init_peripheral_ep(struct musb *musb, struct musb_ep *ep, u8 epnum, int is_in)
1826{
1827 struct musb_hw_ep *hw_ep = musb->endpoints + epnum;
1828
1829 memset(ep, 0, sizeof *ep);
1830
1831 ep->current_epnum = epnum;
1832 ep->musb = musb;
1833 ep->hw_ep = hw_ep;
1834 ep->is_in = is_in;
1835
1836 INIT_LIST_HEAD(&ep->req_list);
1837
1838 sprintf(ep->name, "ep%d%s", epnum,
1839 (!epnum || hw_ep->is_shared_fifo) ? "" : (
1840 is_in ? "in" : "out"));
1841 ep->end_point.name = ep->name;
1842 INIT_LIST_HEAD(&ep->end_point.ep_list);
1843 if (!epnum) {
1844 ep->end_point.maxpacket = 64;
1845 ep->end_point.ops = &musb_g_ep0_ops;
1846 musb->g.ep0 = &ep->end_point;
1847 } else {
1848 if (is_in)
1849 ep->end_point.maxpacket = hw_ep->max_packet_sz_tx;
1850 else
1851 ep->end_point.maxpacket = hw_ep->max_packet_sz_rx;
1852 ep->end_point.ops = &musb_ep_ops;
1853 list_add_tail(&ep->end_point.ep_list, &musb->g.ep_list);
1854 }
1855}
1856
1857/*
1858 * Initialize the endpoints exposed to peripheral drivers, with backlinks
1859 * to the rest of the driver state.
1860 */
1861static inline void __devinit musb_g_init_endpoints(struct musb *musb)
1862{
1863 u8 epnum;
1864 struct musb_hw_ep *hw_ep;
1865 unsigned count = 0;
1866
1867 /* initialize endpoint list just once */
1868 INIT_LIST_HEAD(&(musb->g.ep_list));
1869
1870 for (epnum = 0, hw_ep = musb->endpoints;
1871 epnum < musb->nr_endpoints;
1872 epnum++, hw_ep++) {
1873 if (hw_ep->is_shared_fifo /* || !epnum */) {
1874 init_peripheral_ep(musb, &hw_ep->ep_in, epnum, 0);
1875 count++;
1876 } else {
1877 if (hw_ep->max_packet_sz_tx) {
1878 init_peripheral_ep(musb, &hw_ep->ep_in,
1879 epnum, 1);
1880 count++;
1881 }
1882 if (hw_ep->max_packet_sz_rx) {
1883 init_peripheral_ep(musb, &hw_ep->ep_out,
1884 epnum, 0);
1885 count++;
1886 }
1887 }
1888 }
1889}
1890
1891/* called once during driver setup to initialize and link into
1892 * the driver model; memory is zeroed.
1893 */
1894int __devinit musb_gadget_setup(struct musb *musb)
1895{
1896 int status;
1897
1898 /* REVISIT minor race: if (erroneously) setting up two
1899 * musb peripherals at the same time, only the bus lock
1900 * is probably held.
1901 */
1902
1903 musb->g.ops = &musb_gadget_operations;
1904#ifndef __UBOOT__
1905 musb->g.max_speed = USB_SPEED_HIGH;
1906#endif
1907 musb->g.speed = USB_SPEED_UNKNOWN;
1908
1909#ifndef __UBOOT__
1910 /* this "gadget" abstracts/virtualizes the controller */
1911 dev_set_name(&musb->g.dev, "gadget");
1912 musb->g.dev.parent = musb->controller;
1913 musb->g.dev.dma_mask = musb->controller->dma_mask;
1914 musb->g.dev.release = musb_gadget_release;
1915#endif
1916 musb->g.name = musb_driver_name;
1917
1918#ifndef __UBOOT__
1919 if (is_otg_enabled(musb))
1920 musb->g.is_otg = 1;
1921#endif
1922
1923 musb_g_init_endpoints(musb);
1924
1925 musb->is_active = 0;
1926 musb_platform_try_idle(musb, 0);
1927
1928#ifndef __UBOOT__
1929 status = device_register(&musb->g.dev);
1930 if (status != 0) {
1931 put_device(&musb->g.dev);
1932 return status;
1933 }
1934 status = usb_add_gadget_udc(musb->controller, &musb->g);
1935 if (status)
1936 goto err;
1937#endif
1938
1939 return 0;
1940#ifndef __UBOOT__
1941err:
1942 musb->g.dev.parent = NULL;
1943 device_unregister(&musb->g.dev);
1944 return status;
1945#endif
1946}
1947
1948void musb_gadget_cleanup(struct musb *musb)
1949{
1950#ifndef __UBOOT__
1951 usb_del_gadget_udc(&musb->g);
1952 if (musb->g.dev.parent)
1953 device_unregister(&musb->g.dev);
1954#endif
1955}
1956
1957/*
1958 * Register the gadget driver. Used by gadget drivers when
1959 * registering themselves with the controller.
1960 *
1961 * -EINVAL something went wrong (not driver)
1962 * -EBUSY another gadget is already using the controller
1963 * -ENOMEM no memory to perform the operation
1964 *
1965 * @param driver the gadget driver
1966 * @return <0 if error, 0 if everything is fine
1967 */
1968#ifndef __UBOOT__
1969static int musb_gadget_start(struct usb_gadget *g,
1970 struct usb_gadget_driver *driver)
1971#else
1972int musb_gadget_start(struct usb_gadget *g,
1973 struct usb_gadget_driver *driver)
1974#endif
1975{
1976 struct musb *musb = gadget_to_musb(g);
1977#ifndef __UBOOT__
1978 struct usb_otg *otg = musb->xceiv->otg;
1979#endif
1980 unsigned long flags;
1981 int retval = -EINVAL;
1982
1983#ifndef __UBOOT__
1984 if (driver->max_speed < USB_SPEED_HIGH)
1985 goto err0;
1986#endif
1987
1988 pm_runtime_get_sync(musb->controller);
1989
1990#ifndef __UBOOT__
1991 dev_dbg(musb->controller, "registering driver %s\n", driver->function);
1992#endif
1993
1994 musb->softconnect = 0;
1995 musb->gadget_driver = driver;
1996
1997 spin_lock_irqsave(&musb->lock, flags);
1998 musb->is_active = 1;
1999
2000#ifndef __UBOOT__
2001 otg_set_peripheral(otg, &musb->g);
2002 musb->xceiv->state = OTG_STATE_B_IDLE;
2003
2004 /*
2005 * FIXME this ignores the softconnect flag. Drivers are
2006 * allowed hold the peripheral inactive until for example
2007 * userspace hooks up printer hardware or DSP codecs, so
2008 * hosts only see fully functional devices.
2009 */
2010
2011 if (!is_otg_enabled(musb))
2012#endif
2013 musb_start(musb);
2014
2015 spin_unlock_irqrestore(&musb->lock, flags);
2016
2017#ifndef __UBOOT__
2018 if (is_otg_enabled(musb)) {
2019 struct usb_hcd *hcd = musb_to_hcd(musb);
2020
2021 dev_dbg(musb->controller, "OTG startup...\n");
2022
2023 /* REVISIT: funcall to other code, which also
2024 * handles power budgeting ... this way also
2025 * ensures HdrcStart is indirectly called.
2026 */
2027 retval = usb_add_hcd(musb_to_hcd(musb), 0, 0);
2028 if (retval < 0) {
2029 dev_dbg(musb->controller, "add_hcd failed, %d\n", retval);
2030 goto err2;
2031 }
2032
2033 if ((musb->xceiv->last_event == USB_EVENT_ID)
2034 && otg->set_vbus)
2035 otg_set_vbus(otg, 1);
2036
2037 hcd->self.uses_pio_for_control = 1;
2038 }
2039 if (musb->xceiv->last_event == USB_EVENT_NONE)
2040 pm_runtime_put(musb->controller);
2041#endif
2042
2043 return 0;
2044
2045#ifndef __UBOOT__
2046err2:
2047 if (!is_otg_enabled(musb))
2048 musb_stop(musb);
2049err0:
2050 return retval;
2051#endif
2052}
2053
2054#ifndef __UBOOT__
2055static void stop_activity(struct musb *musb, struct usb_gadget_driver *driver)
2056{
2057 int i;
2058 struct musb_hw_ep *hw_ep;
2059
2060 /* don't disconnect if it's not connected */
2061 if (musb->g.speed == USB_SPEED_UNKNOWN)
2062 driver = NULL;
2063 else
2064 musb->g.speed = USB_SPEED_UNKNOWN;
2065
2066 /* deactivate the hardware */
2067 if (musb->softconnect) {
2068 musb->softconnect = 0;
2069 musb_pullup(musb, 0);
2070 }
2071 musb_stop(musb);
2072
2073 /* killing any outstanding requests will quiesce the driver;
2074 * then report disconnect
2075 */
2076 if (driver) {
2077 for (i = 0, hw_ep = musb->endpoints;
2078 i < musb->nr_endpoints;
2079 i++, hw_ep++) {
2080 musb_ep_select(musb->mregs, i);
2081 if (hw_ep->is_shared_fifo /* || !epnum */) {
2082 nuke(&hw_ep->ep_in, -ESHUTDOWN);
2083 } else {
2084 if (hw_ep->max_packet_sz_tx)
2085 nuke(&hw_ep->ep_in, -ESHUTDOWN);
2086 if (hw_ep->max_packet_sz_rx)
2087 nuke(&hw_ep->ep_out, -ESHUTDOWN);
2088 }
2089 }
2090 }
2091}
2092
2093/*
2094 * Unregister the gadget driver. Used by gadget drivers when
2095 * unregistering themselves from the controller.
2096 *
2097 * @param driver the gadget driver to unregister
2098 */
2099static int musb_gadget_stop(struct usb_gadget *g,
2100 struct usb_gadget_driver *driver)
2101{
2102 struct musb *musb = gadget_to_musb(g);
2103 unsigned long flags;
2104
2105 if (musb->xceiv->last_event == USB_EVENT_NONE)
2106 pm_runtime_get_sync(musb->controller);
2107
2108 /*
2109 * REVISIT always use otg_set_peripheral() here too;
2110 * this needs to shut down the OTG engine.
2111 */
2112
2113 spin_lock_irqsave(&musb->lock, flags);
2114
2115 musb_hnp_stop(musb);
2116
2117 (void) musb_gadget_vbus_draw(&musb->g, 0);
2118
2119 musb->xceiv->state = OTG_STATE_UNDEFINED;
2120 stop_activity(musb, driver);
2121 otg_set_peripheral(musb->xceiv->otg, NULL);
2122
2123 dev_dbg(musb->controller, "unregistering driver %s\n", driver->function);
2124
2125 musb->is_active = 0;
2126 musb_platform_try_idle(musb, 0);
2127 spin_unlock_irqrestore(&musb->lock, flags);
2128
2129 if (is_otg_enabled(musb)) {
2130 usb_remove_hcd(musb_to_hcd(musb));
2131 /* FIXME we need to be able to register another
2132 * gadget driver here and have everything work;
2133 * that currently misbehaves.
2134 */
2135 }
2136
2137 if (!is_otg_enabled(musb))
2138 musb_stop(musb);
2139
2140 pm_runtime_put(musb->controller);
2141
2142 return 0;
2143}
2144#endif
2145
2146/* ----------------------------------------------------------------------- */
2147
2148/* lifecycle operations called through plat_uds.c */
2149
2150void musb_g_resume(struct musb *musb)
2151{
2152#ifndef __UBOOT__
2153 musb->is_suspended = 0;
2154 switch (musb->xceiv->state) {
2155 case OTG_STATE_B_IDLE:
2156 break;
2157 case OTG_STATE_B_WAIT_ACON:
2158 case OTG_STATE_B_PERIPHERAL:
2159 musb->is_active = 1;
2160 if (musb->gadget_driver && musb->gadget_driver->resume) {
2161 spin_unlock(&musb->lock);
2162 musb->gadget_driver->resume(&musb->g);
2163 spin_lock(&musb->lock);
2164 }
2165 break;
2166 default:
2167 WARNING("unhandled RESUME transition (%s)\n",
2168 otg_state_string(musb->xceiv->state));
2169 }
2170#endif
2171}
2172
2173/* called when SOF packets stop for 3+ msec */
2174void musb_g_suspend(struct musb *musb)
2175{
2176#ifndef __UBOOT__
2177 u8 devctl;
2178
2179 devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
2180 dev_dbg(musb->controller, "devctl %02x\n", devctl);
2181
2182 switch (musb->xceiv->state) {
2183 case OTG_STATE_B_IDLE:
2184 if ((devctl & MUSB_DEVCTL_VBUS) == MUSB_DEVCTL_VBUS)
2185 musb->xceiv->state = OTG_STATE_B_PERIPHERAL;
2186 break;
2187 case OTG_STATE_B_PERIPHERAL:
2188 musb->is_suspended = 1;
2189 if (musb->gadget_driver && musb->gadget_driver->suspend) {
2190 spin_unlock(&musb->lock);
2191 musb->gadget_driver->suspend(&musb->g);
2192 spin_lock(&musb->lock);
2193 }
2194 break;
2195 default:
2196 /* REVISIT if B_HOST, clear DEVCTL.HOSTREQ;
2197 * A_PERIPHERAL may need care too
2198 */
2199 WARNING("unhandled SUSPEND transition (%s)\n",
2200 otg_state_string(musb->xceiv->state));
2201 }
2202#endif
2203}
2204
2205/* Called during SRP */
2206void musb_g_wakeup(struct musb *musb)
2207{
2208 musb_gadget_wakeup(&musb->g);
2209}
2210
2211/* called when VBUS drops below session threshold, and in other cases */
2212void musb_g_disconnect(struct musb *musb)
2213{
2214 void __iomem *mregs = musb->mregs;
2215 u8 devctl = musb_readb(mregs, MUSB_DEVCTL);
2216
2217 dev_dbg(musb->controller, "devctl %02x\n", devctl);
2218
2219 /* clear HR */
2220 musb_writeb(mregs, MUSB_DEVCTL, devctl & MUSB_DEVCTL_SESSION);
2221
2222 /* don't draw vbus until new b-default session */
2223 (void) musb_gadget_vbus_draw(&musb->g, 0);
2224
2225 musb->g.speed = USB_SPEED_UNKNOWN;
2226 if (musb->gadget_driver && musb->gadget_driver->disconnect) {
2227 spin_unlock(&musb->lock);
2228 musb->gadget_driver->disconnect(&musb->g);
2229 spin_lock(&musb->lock);
2230 }
2231
2232#ifndef __UBOOT__
2233 switch (musb->xceiv->state) {
2234 default:
2235 dev_dbg(musb->controller, "Unhandled disconnect %s, setting a_idle\n",
2236 otg_state_string(musb->xceiv->state));
2237 musb->xceiv->state = OTG_STATE_A_IDLE;
2238 MUSB_HST_MODE(musb);
2239 break;
2240 case OTG_STATE_A_PERIPHERAL:
2241 musb->xceiv->state = OTG_STATE_A_WAIT_BCON;
2242 MUSB_HST_MODE(musb);
2243 break;
2244 case OTG_STATE_B_WAIT_ACON:
2245 case OTG_STATE_B_HOST:
2246 case OTG_STATE_B_PERIPHERAL:
2247 case OTG_STATE_B_IDLE:
2248 musb->xceiv->state = OTG_STATE_B_IDLE;
2249 break;
2250 case OTG_STATE_B_SRP_INIT:
2251 break;
2252 }
2253#endif
2254
2255 musb->is_active = 0;
2256}
2257
2258void musb_g_reset(struct musb *musb)
2259__releases(musb->lock)
2260__acquires(musb->lock)
2261{
2262 void __iomem *mbase = musb->mregs;
2263 u8 devctl = musb_readb(mbase, MUSB_DEVCTL);
2264 u8 power;
2265
2266#ifndef __UBOOT__
2267 dev_dbg(musb->controller, "<== %s addr=%x driver '%s'\n",
2268 (devctl & MUSB_DEVCTL_BDEVICE)
2269 ? "B-Device" : "A-Device",
2270 musb_readb(mbase, MUSB_FADDR),
2271 musb->gadget_driver
2272 ? musb->gadget_driver->driver.name
2273 : NULL
2274 );
2275#endif
2276
2277 /* report disconnect, if we didn't already (flushing EP state) */
2278 if (musb->g.speed != USB_SPEED_UNKNOWN)
2279 musb_g_disconnect(musb);
2280
2281 /* clear HR */
2282 else if (devctl & MUSB_DEVCTL_HR)
2283 musb_writeb(mbase, MUSB_DEVCTL, MUSB_DEVCTL_SESSION);
2284
2285
2286 /* what speed did we negotiate? */
2287 power = musb_readb(mbase, MUSB_POWER);
2288 musb->g.speed = (power & MUSB_POWER_HSMODE)
2289 ? USB_SPEED_HIGH : USB_SPEED_FULL;
2290
2291 /* start in USB_STATE_DEFAULT */
2292 musb->is_active = 1;
2293 musb->is_suspended = 0;
2294 MUSB_DEV_MODE(musb);
2295 musb->address = 0;
2296 musb->ep0_state = MUSB_EP0_STAGE_SETUP;
2297
2298 musb->may_wakeup = 0;
2299 musb->g.b_hnp_enable = 0;
2300 musb->g.a_alt_hnp_support = 0;
2301 musb->g.a_hnp_support = 0;
2302
2303#ifndef __UBOOT__
2304 /* Normal reset, as B-Device;
2305 * or else after HNP, as A-Device
2306 */
2307 if (devctl & MUSB_DEVCTL_BDEVICE) {
2308 musb->xceiv->state = OTG_STATE_B_PERIPHERAL;
2309 musb->g.is_a_peripheral = 0;
2310 } else if (is_otg_enabled(musb)) {
2311 musb->xceiv->state = OTG_STATE_A_PERIPHERAL;
2312 musb->g.is_a_peripheral = 1;
2313 } else
2314 WARN_ON(1);
2315
2316 /* start with default limits on VBUS power draw */
2317 (void) musb_gadget_vbus_draw(&musb->g,
2318 is_otg_enabled(musb) ? 8 : 100);
2319#endif
2320}