2 * MUSB OTG driver peripheral support
4 * Copyright 2005 Mentor Graphics Corporation
5 * Copyright (C) 2005-2006 by Texas Instruments
6 * Copyright (C) 2006-2007 Nokia Corporation
7 * Copyright (C) 2009 MontaVista Software, Inc. <source@mvista.com>
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.
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.
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
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.
38 #include <linux/kernel.h>
39 #include <linux/list.h>
40 #include <linux/timer.h>
41 #include <linux/module.h>
42 #include <linux/smp.h>
43 #include <linux/spinlock.h>
44 #include <linux/delay.h>
45 #include <linux/dma-mapping.h>
46 #include <linux/slab.h>
49 #include <linux/usb/ch9.h>
50 #include "linux-compat.h"
53 #include "musb_core.h"
56 /* MUSB PERIPHERAL status 3-mar-2006:
58 * - EP0 seems solid. It passes both USBCV and usbtest control cases.
61 * + remote wakeup to Linux hosts work, but saw USBCV failures;
62 * in one test run (operator error?)
63 * + endpoint halt tests -- in both usbtest and usbcv -- seem
64 * to break when dma is enabled ... is something wrongly
67 * - Mass storage behaved ok when last tested. Network traffic patterns
68 * (with lots of short transfers etc) need retesting; they turn up the
69 * worst cases of the DMA, since short packets are typical but are not
73 * + both pio and dma behave in with network and g_zero tests
74 * + no cppi throughput issues other than no-hw-queueing
75 * + failed with FLAT_REG (DaVinci)
76 * + seems to behave with double buffering, PIO -and- CPPI
77 * + with gadgetfs + AIO, requests got lost?
80 * + both pio and dma behave in with network and g_zero tests
81 * + dma is slow in typical case (short_not_ok is clear)
82 * + double buffering ok with PIO
83 * + double buffering *FAILS* with CPPI, wrong data bytes sometimes
84 * + request lossage observed with gadgetfs
86 * - ISO not tested ... might work, but only weakly isochronous
88 * - Gadget driver disabling of softconnect during bind() is ignored; so
89 * drivers can't hold off host requests until userspace is ready.
90 * (Workaround: they can turn it off later.)
92 * - PORTABILITY (assumes PIO works):
93 * + DaVinci, basically works with cppi dma
94 * + OMAP 2430, ditto with mentor dma
95 * + TUSB 6010, platform-specific dma in the works
98 /* ----------------------------------------------------------------------- */
100 #define is_buffer_mapped(req) (is_dma_capable() && \
101 (req->map_state != UN_MAPPED))
103 #ifndef CONFIG_MUSB_PIO_ONLY
104 /* Maps the buffer to dma */
106 static inline void map_dma_buffer(struct musb_request *request,
107 struct musb *musb, struct musb_ep *musb_ep)
109 int compatible = true;
110 struct dma_controller *dma = musb->dma_controller;
112 request->map_state = UN_MAPPED;
114 if (!is_dma_capable() || !musb_ep->dma)
117 /* Check if DMA engine can handle this request.
118 * DMA code must reject the USB request explicitly.
119 * Default behaviour is to map the request.
121 if (dma->is_compatible)
122 compatible = dma->is_compatible(musb_ep->dma,
123 musb_ep->packet_sz, request->request.buf,
124 request->request.length);
128 if (request->request.dma == DMA_ADDR_INVALID) {
129 request->request.dma = dma_map_single(
131 request->request.buf,
132 request->request.length,
136 request->map_state = MUSB_MAPPED;
138 dma_sync_single_for_device(musb->controller,
139 request->request.dma,
140 request->request.length,
144 request->map_state = PRE_MAPPED;
148 /* Unmap the buffer from dma and maps it back to cpu */
149 static inline void unmap_dma_buffer(struct musb_request *request,
152 if (!is_buffer_mapped(request))
155 if (request->request.dma == DMA_ADDR_INVALID) {
156 dev_vdbg(musb->controller,
157 "not unmapping a never mapped buffer\n");
160 if (request->map_state == MUSB_MAPPED) {
161 dma_unmap_single(musb->controller,
162 request->request.dma,
163 request->request.length,
167 request->request.dma = DMA_ADDR_INVALID;
168 } else { /* PRE_MAPPED */
169 dma_sync_single_for_cpu(musb->controller,
170 request->request.dma,
171 request->request.length,
176 request->map_state = UN_MAPPED;
179 static inline void map_dma_buffer(struct musb_request *request,
180 struct musb *musb, struct musb_ep *musb_ep)
184 static inline void unmap_dma_buffer(struct musb_request *request,
191 * Immediately complete a request.
193 * @param request the request to complete
194 * @param status the status to complete the request with
195 * Context: controller locked, IRQs blocked.
197 void musb_g_giveback(
199 struct usb_request *request,
201 __releases(ep->musb->lock)
202 __acquires(ep->musb->lock)
204 struct musb_request *req;
208 req = to_musb_request(request);
210 list_del(&req->list);
211 if (req->request.status == -EINPROGRESS)
212 req->request.status = status;
216 spin_unlock(&musb->lock);
217 unmap_dma_buffer(req, musb);
218 if (request->status == 0)
219 dev_dbg(musb->controller, "%s done request %p, %d/%d\n",
220 ep->end_point.name, request,
221 req->request.actual, req->request.length);
223 dev_dbg(musb->controller, "%s request %p, %d/%d fault %d\n",
224 ep->end_point.name, request,
225 req->request.actual, req->request.length,
227 req->request.complete(&req->ep->end_point, &req->request);
228 spin_lock(&musb->lock);
232 /* ----------------------------------------------------------------------- */
235 * Abort requests queued to an endpoint using the status. Synchronous.
236 * caller locked controller and blocked irqs, and selected this ep.
238 static void nuke(struct musb_ep *ep, const int status)
240 struct musb *musb = ep->musb;
241 struct musb_request *req = NULL;
242 void __iomem *epio = ep->musb->endpoints[ep->current_epnum].regs;
246 if (is_dma_capable() && ep->dma) {
247 struct dma_controller *c = ep->musb->dma_controller;
252 * The programming guide says that we must not clear
253 * the DMAMODE bit before DMAENAB, so we only
254 * clear it in the second write...
256 musb_writew(epio, MUSB_TXCSR,
257 MUSB_TXCSR_DMAMODE | MUSB_TXCSR_FLUSHFIFO);
258 musb_writew(epio, MUSB_TXCSR,
259 0 | MUSB_TXCSR_FLUSHFIFO);
261 musb_writew(epio, MUSB_RXCSR,
262 0 | MUSB_RXCSR_FLUSHFIFO);
263 musb_writew(epio, MUSB_RXCSR,
264 0 | MUSB_RXCSR_FLUSHFIFO);
267 value = c->channel_abort(ep->dma);
268 dev_dbg(musb->controller, "%s: abort DMA --> %d\n",
270 c->channel_release(ep->dma);
274 while (!list_empty(&ep->req_list)) {
275 req = list_first_entry(&ep->req_list, struct musb_request, list);
276 musb_g_giveback(ep, &req->request, status);
280 /* ----------------------------------------------------------------------- */
282 /* Data transfers - pure PIO, pure DMA, or mixed mode */
285 * This assumes the separate CPPI engine is responding to DMA requests
286 * from the usb core ... sequenced a bit differently from mentor dma.
289 static inline int max_ep_writesize(struct musb *musb, struct musb_ep *ep)
291 if (can_bulk_split(musb, ep->type))
292 return ep->hw_ep->max_packet_sz_tx;
294 return ep->packet_sz;
298 #ifdef CONFIG_USB_INVENTRA_DMA
300 /* Peripheral tx (IN) using Mentor DMA works as follows:
301 Only mode 0 is used for transfers <= wPktSize,
302 mode 1 is used for larger transfers,
304 One of the following happens:
305 - Host sends IN token which causes an endpoint interrupt
307 -> if DMA is currently busy, exit.
308 -> if queue is non-empty, txstate().
310 - Request is queued by the gadget driver.
311 -> if queue was previously empty, txstate()
316 | (data is transferred to the FIFO, then sent out when
317 | IN token(s) are recd from Host.
318 | -> DMA interrupt on completion
320 | -> stop DMA, ~DMAENAB,
321 | -> set TxPktRdy for last short pkt or zlp
322 | -> Complete Request
323 | -> Continue next request (call txstate)
324 |___________________________________|
326 * Non-Mentor DMA engines can of course work differently, such as by
327 * upleveling from irq-per-packet to irq-per-buffer.
333 * An endpoint is transmitting data. This can be called either from
334 * the IRQ routine or from ep.queue() to kickstart a request on an
337 * Context: controller locked, IRQs blocked, endpoint selected
339 static void txstate(struct musb *musb, struct musb_request *req)
341 u8 epnum = req->epnum;
342 struct musb_ep *musb_ep;
343 void __iomem *epio = musb->endpoints[epnum].regs;
344 struct usb_request *request;
345 u16 fifo_count = 0, csr;
350 /* Check if EP is disabled */
351 if (!musb_ep->desc) {
352 dev_dbg(musb->controller, "ep:%s disabled - ignore request\n",
353 musb_ep->end_point.name);
357 /* we shouldn't get here while DMA is active ... but we do ... */
358 if (dma_channel_status(musb_ep->dma) == MUSB_DMA_STATUS_BUSY) {
359 dev_dbg(musb->controller, "dma pending...\n");
363 /* read TXCSR before */
364 csr = musb_readw(epio, MUSB_TXCSR);
366 request = &req->request;
367 fifo_count = min(max_ep_writesize(musb, musb_ep),
368 (int)(request->length - request->actual));
370 if (csr & MUSB_TXCSR_TXPKTRDY) {
371 dev_dbg(musb->controller, "%s old packet still ready , txcsr %03x\n",
372 musb_ep->end_point.name, csr);
376 if (csr & MUSB_TXCSR_P_SENDSTALL) {
377 dev_dbg(musb->controller, "%s stalling, txcsr %03x\n",
378 musb_ep->end_point.name, csr);
382 dev_dbg(musb->controller, "hw_ep%d, maxpacket %d, fifo count %d, txcsr %03x\n",
383 epnum, musb_ep->packet_sz, fifo_count,
386 #ifndef CONFIG_MUSB_PIO_ONLY
387 if (is_buffer_mapped(req)) {
388 struct dma_controller *c = musb->dma_controller;
391 /* setup DMA, then program endpoint CSR */
392 request_size = min_t(size_t, request->length - request->actual,
393 musb_ep->dma->max_len);
395 use_dma = (request->dma != DMA_ADDR_INVALID);
397 /* MUSB_TXCSR_P_ISO is still set correctly */
399 #if defined(CONFIG_USB_INVENTRA_DMA) || defined(CONFIG_USB_UX500_DMA)
401 if (request_size < musb_ep->packet_sz)
402 musb_ep->dma->desired_mode = 0;
404 musb_ep->dma->desired_mode = 1;
406 use_dma = use_dma && c->channel_program(
407 musb_ep->dma, musb_ep->packet_sz,
408 musb_ep->dma->desired_mode,
409 request->dma + request->actual, request_size);
411 if (musb_ep->dma->desired_mode == 0) {
413 * We must not clear the DMAMODE bit
414 * before the DMAENAB bit -- and the
415 * latter doesn't always get cleared
416 * before we get here...
418 csr &= ~(MUSB_TXCSR_AUTOSET
419 | MUSB_TXCSR_DMAENAB);
420 musb_writew(epio, MUSB_TXCSR, csr
421 | MUSB_TXCSR_P_WZC_BITS);
422 csr &= ~MUSB_TXCSR_DMAMODE;
423 csr |= (MUSB_TXCSR_DMAENAB |
425 /* against programming guide */
427 csr |= (MUSB_TXCSR_DMAENAB
430 if (!musb_ep->hb_mult)
431 csr |= MUSB_TXCSR_AUTOSET;
433 csr &= ~MUSB_TXCSR_P_UNDERRUN;
435 musb_writew(epio, MUSB_TXCSR, csr);
439 #elif defined(CONFIG_USB_TI_CPPI_DMA)
440 /* program endpoint CSR first, then setup DMA */
441 csr &= ~(MUSB_TXCSR_P_UNDERRUN | MUSB_TXCSR_TXPKTRDY);
442 csr |= MUSB_TXCSR_DMAENAB | MUSB_TXCSR_DMAMODE |
444 musb_writew(epio, MUSB_TXCSR,
445 (MUSB_TXCSR_P_WZC_BITS & ~MUSB_TXCSR_P_UNDERRUN)
448 /* ensure writebuffer is empty */
449 csr = musb_readw(epio, MUSB_TXCSR);
451 /* NOTE host side sets DMAENAB later than this; both are
452 * OK since the transfer dma glue (between CPPI and Mentor
453 * fifos) just tells CPPI it could start. Data only moves
454 * to the USB TX fifo when both fifos are ready.
457 /* "mode" is irrelevant here; handle terminating ZLPs like
458 * PIO does, since the hardware RNDIS mode seems unreliable
459 * except for the last-packet-is-already-short case.
461 use_dma = use_dma && c->channel_program(
462 musb_ep->dma, musb_ep->packet_sz,
464 request->dma + request->actual,
467 c->channel_release(musb_ep->dma);
469 csr &= ~MUSB_TXCSR_DMAENAB;
470 musb_writew(epio, MUSB_TXCSR, csr);
471 /* invariant: prequest->buf is non-null */
473 #elif defined(CONFIG_USB_TUSB_OMAP_DMA)
474 use_dma = use_dma && c->channel_program(
475 musb_ep->dma, musb_ep->packet_sz,
477 request->dma + request->actual,
485 * Unmap the dma buffer back to cpu if dma channel
488 unmap_dma_buffer(req, musb);
490 musb_write_fifo(musb_ep->hw_ep, fifo_count,
491 (u8 *) (request->buf + request->actual));
492 request->actual += fifo_count;
493 csr |= MUSB_TXCSR_TXPKTRDY;
494 csr &= ~MUSB_TXCSR_P_UNDERRUN;
495 musb_writew(epio, MUSB_TXCSR, csr);
498 /* host may already have the data when this message shows... */
499 dev_dbg(musb->controller, "%s TX/IN %s len %d/%d, txcsr %04x, fifo %d/%d\n",
500 musb_ep->end_point.name, use_dma ? "dma" : "pio",
501 request->actual, request->length,
502 musb_readw(epio, MUSB_TXCSR),
504 musb_readw(epio, MUSB_TXMAXP));
508 * FIFO state update (e.g. data ready).
509 * Called from IRQ, with controller locked.
511 void musb_g_tx(struct musb *musb, u8 epnum)
514 struct musb_request *req;
515 struct usb_request *request;
516 u8 __iomem *mbase = musb->mregs;
517 struct musb_ep *musb_ep = &musb->endpoints[epnum].ep_in;
518 void __iomem *epio = musb->endpoints[epnum].regs;
519 struct dma_channel *dma;
521 musb_ep_select(mbase, epnum);
522 req = next_request(musb_ep);
523 request = &req->request;
525 csr = musb_readw(epio, MUSB_TXCSR);
526 dev_dbg(musb->controller, "<== %s, txcsr %04x\n", musb_ep->end_point.name, csr);
528 dma = is_dma_capable() ? musb_ep->dma : NULL;
531 * REVISIT: for high bandwidth, MUSB_TXCSR_P_INCOMPTX
532 * probably rates reporting as a host error.
534 if (csr & MUSB_TXCSR_P_SENTSTALL) {
535 csr |= MUSB_TXCSR_P_WZC_BITS;
536 csr &= ~MUSB_TXCSR_P_SENTSTALL;
537 musb_writew(epio, MUSB_TXCSR, csr);
541 if (csr & MUSB_TXCSR_P_UNDERRUN) {
542 /* We NAKed, no big deal... little reason to care. */
543 csr |= MUSB_TXCSR_P_WZC_BITS;
544 csr &= ~(MUSB_TXCSR_P_UNDERRUN | MUSB_TXCSR_TXPKTRDY);
545 musb_writew(epio, MUSB_TXCSR, csr);
546 dev_vdbg(musb->controller, "underrun on ep%d, req %p\n",
550 if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) {
552 * SHOULD NOT HAPPEN... has with CPPI though, after
553 * changing SENDSTALL (and other cases); harmless?
555 dev_dbg(musb->controller, "%s dma still busy?\n", musb_ep->end_point.name);
562 if (dma && (csr & MUSB_TXCSR_DMAENAB)) {
564 csr |= MUSB_TXCSR_P_WZC_BITS;
565 csr &= ~(MUSB_TXCSR_DMAENAB | MUSB_TXCSR_P_UNDERRUN |
566 MUSB_TXCSR_TXPKTRDY | MUSB_TXCSR_AUTOSET);
567 musb_writew(epio, MUSB_TXCSR, csr);
568 /* Ensure writebuffer is empty. */
569 csr = musb_readw(epio, MUSB_TXCSR);
570 request->actual += musb_ep->dma->actual_len;
571 dev_dbg(musb->controller, "TXCSR%d %04x, DMA off, len %zu, req %p\n",
572 epnum, csr, musb_ep->dma->actual_len, request);
576 * First, maybe a terminating short packet. Some DMA
577 * engines might handle this by themselves.
579 if ((request->zero && request->length
580 && (request->length % musb_ep->packet_sz == 0)
581 && (request->actual == request->length))
582 #if defined(CONFIG_USB_INVENTRA_DMA) || defined(CONFIG_USB_UX500_DMA)
583 || (is_dma && (!dma->desired_mode ||
585 (musb_ep->packet_sz - 1))))
589 * On DMA completion, FIFO may not be
592 if (csr & MUSB_TXCSR_TXPKTRDY)
595 dev_dbg(musb->controller, "sending zero pkt\n");
596 musb_writew(epio, MUSB_TXCSR, MUSB_TXCSR_MODE
597 | MUSB_TXCSR_TXPKTRDY);
601 if (request->actual == request->length) {
602 musb_g_giveback(musb_ep, request, 0);
604 * In the giveback function the MUSB lock is
605 * released and acquired after sometime. During
606 * this time period the INDEX register could get
607 * changed by the gadget_queue function especially
608 * on SMP systems. Reselect the INDEX to be sure
609 * we are reading/modifying the right registers
611 musb_ep_select(mbase, epnum);
612 req = musb_ep->desc ? next_request(musb_ep) : NULL;
614 dev_dbg(musb->controller, "%s idle now\n",
615 musb_ep->end_point.name);
624 /* ------------------------------------------------------------ */
626 #ifdef CONFIG_USB_INVENTRA_DMA
628 /* Peripheral rx (OUT) using Mentor DMA works as follows:
629 - Only mode 0 is used.
631 - Request is queued by the gadget class driver.
632 -> if queue was previously empty, rxstate()
634 - Host sends OUT token which causes an endpoint interrupt
636 | -> if request queued, call rxstate
638 | | -> DMA interrupt on completion
642 | | -> if data recd = max expected
643 | | by the request, or host
644 | | sent a short packet,
645 | | complete the request,
646 | | and start the next one.
647 | |_____________________________________|
648 | else just wait for the host
649 | to send the next OUT token.
650 |__________________________________________________|
652 * Non-Mentor DMA engines can of course work differently.
658 * Context: controller locked, IRQs blocked, endpoint selected
660 static void rxstate(struct musb *musb, struct musb_request *req)
662 const u8 epnum = req->epnum;
663 struct usb_request *request = &req->request;
664 struct musb_ep *musb_ep;
665 void __iomem *epio = musb->endpoints[epnum].regs;
666 unsigned fifo_count = 0;
668 u16 csr = musb_readw(epio, MUSB_RXCSR);
669 struct musb_hw_ep *hw_ep = &musb->endpoints[epnum];
672 if (hw_ep->is_shared_fifo)
673 musb_ep = &hw_ep->ep_in;
675 musb_ep = &hw_ep->ep_out;
677 len = musb_ep->packet_sz;
679 /* Check if EP is disabled */
680 if (!musb_ep->desc) {
681 dev_dbg(musb->controller, "ep:%s disabled - ignore request\n",
682 musb_ep->end_point.name);
686 /* We shouldn't get here while DMA is active, but we do... */
687 if (dma_channel_status(musb_ep->dma) == MUSB_DMA_STATUS_BUSY) {
688 dev_dbg(musb->controller, "DMA pending...\n");
692 if (csr & MUSB_RXCSR_P_SENDSTALL) {
693 dev_dbg(musb->controller, "%s stalling, RXCSR %04x\n",
694 musb_ep->end_point.name, csr);
698 if (is_cppi_enabled() && is_buffer_mapped(req)) {
699 struct dma_controller *c = musb->dma_controller;
700 struct dma_channel *channel = musb_ep->dma;
702 /* NOTE: CPPI won't actually stop advancing the DMA
703 * queue after short packet transfers, so this is almost
704 * always going to run as IRQ-per-packet DMA so that
705 * faults will be handled correctly.
707 if (c->channel_program(channel,
709 !request->short_not_ok,
710 request->dma + request->actual,
711 request->length - request->actual)) {
713 /* make sure that if an rxpkt arrived after the irq,
714 * the cppi engine will be ready to take it as soon
717 csr &= ~(MUSB_RXCSR_AUTOCLEAR
718 | MUSB_RXCSR_DMAMODE);
719 csr |= MUSB_RXCSR_DMAENAB | MUSB_RXCSR_P_WZC_BITS;
720 musb_writew(epio, MUSB_RXCSR, csr);
725 if (csr & MUSB_RXCSR_RXPKTRDY) {
726 len = musb_readw(epio, MUSB_RXCOUNT);
729 * Enable Mode 1 on RX transfers only when short_not_ok flag
730 * is set. Currently short_not_ok flag is set only from
731 * file_storage and f_mass_storage drivers
734 if (request->short_not_ok && len == musb_ep->packet_sz)
739 if (request->actual < request->length) {
740 #ifdef CONFIG_USB_INVENTRA_DMA
741 if (is_buffer_mapped(req)) {
742 struct dma_controller *c;
743 struct dma_channel *channel;
746 c = musb->dma_controller;
747 channel = musb_ep->dma;
749 /* We use DMA Req mode 0 in rx_csr, and DMA controller operates in
750 * mode 0 only. So we do not get endpoint interrupts due to DMA
751 * completion. We only get interrupts from DMA controller.
753 * We could operate in DMA mode 1 if we knew the size of the tranfer
754 * in advance. For mass storage class, request->length = what the host
755 * sends, so that'd work. But for pretty much everything else,
756 * request->length is routinely more than what the host sends. For
757 * most these gadgets, end of is signified either by a short packet,
758 * or filling the last byte of the buffer. (Sending extra data in
759 * that last pckate should trigger an overflow fault.) But in mode 1,
760 * we don't get DMA completion interrupt for short packets.
762 * Theoretically, we could enable DMAReq irq (MUSB_RXCSR_DMAMODE = 1),
763 * to get endpoint interrupt on every DMA req, but that didn't seem
766 * REVISIT an updated g_file_storage can set req->short_not_ok, which
767 * then becomes usable as a runtime "use mode 1" hint...
770 /* Experimental: Mode1 works with mass storage use cases */
772 csr |= MUSB_RXCSR_AUTOCLEAR;
773 musb_writew(epio, MUSB_RXCSR, csr);
774 csr |= MUSB_RXCSR_DMAENAB;
775 musb_writew(epio, MUSB_RXCSR, csr);
778 * this special sequence (enabling and then
779 * disabling MUSB_RXCSR_DMAMODE) is required
780 * to get DMAReq to activate
782 musb_writew(epio, MUSB_RXCSR,
783 csr | MUSB_RXCSR_DMAMODE);
784 musb_writew(epio, MUSB_RXCSR, csr);
787 if (!musb_ep->hb_mult &&
788 musb_ep->hw_ep->rx_double_buffered)
789 csr |= MUSB_RXCSR_AUTOCLEAR;
790 csr |= MUSB_RXCSR_DMAENAB;
791 musb_writew(epio, MUSB_RXCSR, csr);
794 if (request->actual < request->length) {
795 int transfer_size = 0;
797 transfer_size = min(request->length - request->actual,
799 musb_ep->dma->desired_mode = 1;
801 transfer_size = min(request->length - request->actual,
803 musb_ep->dma->desired_mode = 0;
806 use_dma = c->channel_program(
809 channel->desired_mode,
818 #elif defined(CONFIG_USB_UX500_DMA)
819 if ((is_buffer_mapped(req)) &&
820 (request->actual < request->length)) {
822 struct dma_controller *c;
823 struct dma_channel *channel;
824 int transfer_size = 0;
826 c = musb->dma_controller;
827 channel = musb_ep->dma;
829 /* In case first packet is short */
830 if (len < musb_ep->packet_sz)
832 else if (request->short_not_ok)
833 transfer_size = min(request->length -
837 transfer_size = min(request->length -
841 csr &= ~MUSB_RXCSR_DMAMODE;
842 csr |= (MUSB_RXCSR_DMAENAB |
843 MUSB_RXCSR_AUTOCLEAR);
845 musb_writew(epio, MUSB_RXCSR, csr);
847 if (transfer_size <= musb_ep->packet_sz) {
848 musb_ep->dma->desired_mode = 0;
850 musb_ep->dma->desired_mode = 1;
851 /* Mode must be set after DMAENAB */
852 csr |= MUSB_RXCSR_DMAMODE;
853 musb_writew(epio, MUSB_RXCSR, csr);
856 if (c->channel_program(channel,
858 channel->desired_mode,
865 #endif /* Mentor's DMA */
867 fifo_count = request->length - request->actual;
868 dev_dbg(musb->controller, "%s OUT/RX pio fifo %d/%d, maxpacket %d\n",
869 musb_ep->end_point.name,
873 fifo_count = min_t(unsigned, len, fifo_count);
875 #ifdef CONFIG_USB_TUSB_OMAP_DMA
876 if (tusb_dma_omap() && is_buffer_mapped(req)) {
877 struct dma_controller *c = musb->dma_controller;
878 struct dma_channel *channel = musb_ep->dma;
879 u32 dma_addr = request->dma + request->actual;
882 ret = c->channel_program(channel,
884 channel->desired_mode,
892 * Unmap the dma buffer back to cpu if dma channel
893 * programming fails. This buffer is mapped if the
894 * channel allocation is successful
896 if (is_buffer_mapped(req)) {
897 unmap_dma_buffer(req, musb);
900 * Clear DMAENAB and AUTOCLEAR for the
903 csr &= ~(MUSB_RXCSR_DMAENAB | MUSB_RXCSR_AUTOCLEAR);
904 musb_writew(epio, MUSB_RXCSR, csr);
907 musb_read_fifo(musb_ep->hw_ep, fifo_count, (u8 *)
908 (request->buf + request->actual));
909 request->actual += fifo_count;
911 /* REVISIT if we left anything in the fifo, flush
912 * it and report -EOVERFLOW
916 csr |= MUSB_RXCSR_P_WZC_BITS;
917 csr &= ~MUSB_RXCSR_RXPKTRDY;
918 musb_writew(epio, MUSB_RXCSR, csr);
922 /* reach the end or short packet detected */
923 if (request->actual == request->length || len < musb_ep->packet_sz)
924 musb_g_giveback(musb_ep, request, 0);
928 * Data ready for a request; called from IRQ
930 void musb_g_rx(struct musb *musb, u8 epnum)
933 struct musb_request *req;
934 struct usb_request *request;
935 void __iomem *mbase = musb->mregs;
936 struct musb_ep *musb_ep;
937 void __iomem *epio = musb->endpoints[epnum].regs;
938 struct dma_channel *dma;
939 struct musb_hw_ep *hw_ep = &musb->endpoints[epnum];
941 if (hw_ep->is_shared_fifo)
942 musb_ep = &hw_ep->ep_in;
944 musb_ep = &hw_ep->ep_out;
946 musb_ep_select(mbase, epnum);
948 req = next_request(musb_ep);
952 request = &req->request;
954 csr = musb_readw(epio, MUSB_RXCSR);
955 dma = is_dma_capable() ? musb_ep->dma : NULL;
957 dev_dbg(musb->controller, "<== %s, rxcsr %04x%s %p\n", musb_ep->end_point.name,
958 csr, dma ? " (dma)" : "", request);
960 if (csr & MUSB_RXCSR_P_SENTSTALL) {
961 csr |= MUSB_RXCSR_P_WZC_BITS;
962 csr &= ~MUSB_RXCSR_P_SENTSTALL;
963 musb_writew(epio, MUSB_RXCSR, csr);
967 if (csr & MUSB_RXCSR_P_OVERRUN) {
968 /* csr |= MUSB_RXCSR_P_WZC_BITS; */
969 csr &= ~MUSB_RXCSR_P_OVERRUN;
970 musb_writew(epio, MUSB_RXCSR, csr);
972 dev_dbg(musb->controller, "%s iso overrun on %p\n", musb_ep->name, request);
973 if (request->status == -EINPROGRESS)
974 request->status = -EOVERFLOW;
976 if (csr & MUSB_RXCSR_INCOMPRX) {
977 /* REVISIT not necessarily an error */
978 dev_dbg(musb->controller, "%s, incomprx\n", musb_ep->end_point.name);
981 if (dma_channel_status(dma) == MUSB_DMA_STATUS_BUSY) {
982 /* "should not happen"; likely RXPKTRDY pending for DMA */
983 dev_dbg(musb->controller, "%s busy, csr %04x\n",
984 musb_ep->end_point.name, csr);
988 if (dma && (csr & MUSB_RXCSR_DMAENAB)) {
989 csr &= ~(MUSB_RXCSR_AUTOCLEAR
991 | MUSB_RXCSR_DMAMODE);
992 musb_writew(epio, MUSB_RXCSR,
993 MUSB_RXCSR_P_WZC_BITS | csr);
995 request->actual += musb_ep->dma->actual_len;
997 dev_dbg(musb->controller, "RXCSR%d %04x, dma off, %04x, len %zu, req %p\n",
999 musb_readw(epio, MUSB_RXCSR),
1000 musb_ep->dma->actual_len, request);
1002 #if defined(CONFIG_USB_INVENTRA_DMA) || defined(CONFIG_USB_TUSB_OMAP_DMA) || \
1003 defined(CONFIG_USB_UX500_DMA)
1004 /* Autoclear doesn't clear RxPktRdy for short packets */
1005 if ((dma->desired_mode == 0 && !hw_ep->rx_double_buffered)
1007 & (musb_ep->packet_sz - 1))) {
1009 csr &= ~MUSB_RXCSR_RXPKTRDY;
1010 musb_writew(epio, MUSB_RXCSR, csr);
1013 /* incomplete, and not short? wait for next IN packet */
1014 if ((request->actual < request->length)
1015 && (musb_ep->dma->actual_len
1016 == musb_ep->packet_sz)) {
1017 /* In double buffer case, continue to unload fifo if
1018 * there is Rx packet in FIFO.
1020 csr = musb_readw(epio, MUSB_RXCSR);
1021 if ((csr & MUSB_RXCSR_RXPKTRDY) &&
1022 hw_ep->rx_double_buffered)
1027 musb_g_giveback(musb_ep, request, 0);
1029 * In the giveback function the MUSB lock is
1030 * released and acquired after sometime. During
1031 * this time period the INDEX register could get
1032 * changed by the gadget_queue function especially
1033 * on SMP systems. Reselect the INDEX to be sure
1034 * we are reading/modifying the right registers
1036 musb_ep_select(mbase, epnum);
1038 req = next_request(musb_ep);
1042 #if defined(CONFIG_USB_INVENTRA_DMA) || defined(CONFIG_USB_TUSB_OMAP_DMA) || \
1043 defined(CONFIG_USB_UX500_DMA)
1046 /* Analyze request */
1050 /* ------------------------------------------------------------ */
1052 static int musb_gadget_enable(struct usb_ep *ep,
1053 const struct usb_endpoint_descriptor *desc)
1055 unsigned long flags;
1056 struct musb_ep *musb_ep;
1057 struct musb_hw_ep *hw_ep;
1060 void __iomem *mbase;
1064 int status = -EINVAL;
1069 musb_ep = to_musb_ep(ep);
1070 hw_ep = musb_ep->hw_ep;
1072 musb = musb_ep->musb;
1073 mbase = musb->mregs;
1074 epnum = musb_ep->current_epnum;
1076 spin_lock_irqsave(&musb->lock, flags);
1078 if (musb_ep->desc) {
1082 musb_ep->type = usb_endpoint_type(desc);
1084 /* check direction and (later) maxpacket size against endpoint */
1085 if (usb_endpoint_num(desc) != epnum)
1088 /* REVISIT this rules out high bandwidth periodic transfers */
1089 tmp = usb_endpoint_maxp(desc);
1090 if (tmp & ~0x07ff) {
1093 if (usb_endpoint_dir_in(desc))
1094 ok = musb->hb_iso_tx;
1096 ok = musb->hb_iso_rx;
1099 dev_dbg(musb->controller, "no support for high bandwidth ISO\n");
1102 musb_ep->hb_mult = (tmp >> 11) & 3;
1104 musb_ep->hb_mult = 0;
1107 musb_ep->packet_sz = tmp & 0x7ff;
1108 tmp = musb_ep->packet_sz * (musb_ep->hb_mult + 1);
1110 /* enable the interrupts for the endpoint, set the endpoint
1111 * packet size (or fail), set the mode, clear the fifo
1113 musb_ep_select(mbase, epnum);
1114 if (usb_endpoint_dir_in(desc)) {
1115 u16 int_txe = musb_readw(mbase, MUSB_INTRTXE);
1117 if (hw_ep->is_shared_fifo)
1119 if (!musb_ep->is_in)
1122 if (tmp > hw_ep->max_packet_sz_tx) {
1123 dev_dbg(musb->controller, "packet size beyond hardware FIFO size\n");
1127 int_txe |= (1 << epnum);
1128 musb_writew(mbase, MUSB_INTRTXE, int_txe);
1130 /* REVISIT if can_bulk_split(), use by updating "tmp";
1131 * likewise high bandwidth periodic tx
1133 /* Set TXMAXP with the FIFO size of the endpoint
1134 * to disable double buffering mode.
1136 if (musb->double_buffer_not_ok)
1137 musb_writew(regs, MUSB_TXMAXP, hw_ep->max_packet_sz_tx);
1139 musb_writew(regs, MUSB_TXMAXP, musb_ep->packet_sz
1140 | (musb_ep->hb_mult << 11));
1142 csr = MUSB_TXCSR_MODE | MUSB_TXCSR_CLRDATATOG;
1143 if (musb_readw(regs, MUSB_TXCSR)
1144 & MUSB_TXCSR_FIFONOTEMPTY)
1145 csr |= MUSB_TXCSR_FLUSHFIFO;
1146 if (musb_ep->type == USB_ENDPOINT_XFER_ISOC)
1147 csr |= MUSB_TXCSR_P_ISO;
1149 /* set twice in case of double buffering */
1150 musb_writew(regs, MUSB_TXCSR, csr);
1151 /* REVISIT may be inappropriate w/o FIFONOTEMPTY ... */
1152 musb_writew(regs, MUSB_TXCSR, csr);
1155 u16 int_rxe = musb_readw(mbase, MUSB_INTRRXE);
1157 if (hw_ep->is_shared_fifo)
1162 if (tmp > hw_ep->max_packet_sz_rx) {
1163 dev_dbg(musb->controller, "packet size beyond hardware FIFO size\n");
1167 int_rxe |= (1 << epnum);
1168 musb_writew(mbase, MUSB_INTRRXE, int_rxe);
1170 /* REVISIT if can_bulk_combine() use by updating "tmp"
1171 * likewise high bandwidth periodic rx
1173 /* Set RXMAXP with the FIFO size of the endpoint
1174 * to disable double buffering mode.
1176 if (musb->double_buffer_not_ok)
1177 musb_writew(regs, MUSB_RXMAXP, hw_ep->max_packet_sz_tx);
1179 musb_writew(regs, MUSB_RXMAXP, musb_ep->packet_sz
1180 | (musb_ep->hb_mult << 11));
1182 /* force shared fifo to OUT-only mode */
1183 if (hw_ep->is_shared_fifo) {
1184 csr = musb_readw(regs, MUSB_TXCSR);
1185 csr &= ~(MUSB_TXCSR_MODE | MUSB_TXCSR_TXPKTRDY);
1186 musb_writew(regs, MUSB_TXCSR, csr);
1189 csr = MUSB_RXCSR_FLUSHFIFO | MUSB_RXCSR_CLRDATATOG;
1190 if (musb_ep->type == USB_ENDPOINT_XFER_ISOC)
1191 csr |= MUSB_RXCSR_P_ISO;
1192 else if (musb_ep->type == USB_ENDPOINT_XFER_INT)
1193 csr |= MUSB_RXCSR_DISNYET;
1195 /* set twice in case of double buffering */
1196 musb_writew(regs, MUSB_RXCSR, csr);
1197 musb_writew(regs, MUSB_RXCSR, csr);
1200 /* NOTE: all the I/O code _should_ work fine without DMA, in case
1201 * for some reason you run out of channels here.
1203 if (is_dma_capable() && musb->dma_controller) {
1204 struct dma_controller *c = musb->dma_controller;
1206 musb_ep->dma = c->channel_alloc(c, hw_ep,
1207 (desc->bEndpointAddress & USB_DIR_IN));
1209 musb_ep->dma = NULL;
1211 musb_ep->desc = desc;
1213 musb_ep->wedged = 0;
1216 pr_debug("%s periph: enabled %s for %s %s, %smaxpacket %d\n",
1217 musb_driver_name, musb_ep->end_point.name,
1218 ({ char *s; switch (musb_ep->type) {
1219 case USB_ENDPOINT_XFER_BULK: s = "bulk"; break;
1220 case USB_ENDPOINT_XFER_INT: s = "int"; break;
1221 default: s = "iso"; break;
1223 musb_ep->is_in ? "IN" : "OUT",
1224 musb_ep->dma ? "dma, " : "",
1225 musb_ep->packet_sz);
1227 schedule_work(&musb->irq_work);
1230 spin_unlock_irqrestore(&musb->lock, flags);
1235 * Disable an endpoint flushing all requests queued.
1237 static int musb_gadget_disable(struct usb_ep *ep)
1239 unsigned long flags;
1242 struct musb_ep *musb_ep;
1246 musb_ep = to_musb_ep(ep);
1247 musb = musb_ep->musb;
1248 epnum = musb_ep->current_epnum;
1249 epio = musb->endpoints[epnum].regs;
1251 spin_lock_irqsave(&musb->lock, flags);
1252 musb_ep_select(musb->mregs, epnum);
1254 /* zero the endpoint sizes */
1255 if (musb_ep->is_in) {
1256 u16 int_txe = musb_readw(musb->mregs, MUSB_INTRTXE);
1257 int_txe &= ~(1 << epnum);
1258 musb_writew(musb->mregs, MUSB_INTRTXE, int_txe);
1259 musb_writew(epio, MUSB_TXMAXP, 0);
1261 u16 int_rxe = musb_readw(musb->mregs, MUSB_INTRRXE);
1262 int_rxe &= ~(1 << epnum);
1263 musb_writew(musb->mregs, MUSB_INTRRXE, int_rxe);
1264 musb_writew(epio, MUSB_RXMAXP, 0);
1267 musb_ep->desc = NULL;
1269 musb_ep->end_point.desc = NULL;
1272 /* abort all pending DMA and requests */
1273 nuke(musb_ep, -ESHUTDOWN);
1275 schedule_work(&musb->irq_work);
1277 spin_unlock_irqrestore(&(musb->lock), flags);
1279 dev_dbg(musb->controller, "%s\n", musb_ep->end_point.name);
1285 * Allocate a request for an endpoint.
1286 * Reused by ep0 code.
1288 struct usb_request *musb_alloc_request(struct usb_ep *ep, gfp_t gfp_flags)
1290 struct musb_ep *musb_ep = to_musb_ep(ep);
1291 struct musb *musb = musb_ep->musb;
1292 struct musb_request *request = NULL;
1294 request = kzalloc(sizeof *request, gfp_flags);
1296 dev_dbg(musb->controller, "not enough memory\n");
1300 request->request.dma = DMA_ADDR_INVALID;
1301 request->epnum = musb_ep->current_epnum;
1302 request->ep = musb_ep;
1304 return &request->request;
1309 * Reused by ep0 code.
1311 void musb_free_request(struct usb_ep *ep, struct usb_request *req)
1313 kfree(to_musb_request(req));
1316 static LIST_HEAD(buffers);
1318 struct free_record {
1319 struct list_head list;
1326 * Context: controller locked, IRQs blocked.
1328 void musb_ep_restart(struct musb *musb, struct musb_request *req)
1330 dev_dbg(musb->controller, "<== %s request %p len %u on hw_ep%d\n",
1331 req->tx ? "TX/IN" : "RX/OUT",
1332 &req->request, req->request.length, req->epnum);
1334 musb_ep_select(musb->mregs, req->epnum);
1341 static int musb_gadget_queue(struct usb_ep *ep, struct usb_request *req,
1344 struct musb_ep *musb_ep;
1345 struct musb_request *request;
1348 unsigned long lockflags;
1355 musb_ep = to_musb_ep(ep);
1356 musb = musb_ep->musb;
1358 request = to_musb_request(req);
1359 request->musb = musb;
1361 if (request->ep != musb_ep)
1364 dev_dbg(musb->controller, "<== to %s request=%p\n", ep->name, req);
1366 /* request is mine now... */
1367 request->request.actual = 0;
1368 request->request.status = -EINPROGRESS;
1369 request->epnum = musb_ep->current_epnum;
1370 request->tx = musb_ep->is_in;
1372 map_dma_buffer(request, musb, musb_ep);
1374 spin_lock_irqsave(&musb->lock, lockflags);
1376 /* don't queue if the ep is down */
1377 if (!musb_ep->desc) {
1378 dev_dbg(musb->controller, "req %p queued to %s while ep %s\n",
1379 req, ep->name, "disabled");
1380 status = -ESHUTDOWN;
1384 /* add request to the list */
1385 list_add_tail(&request->list, &musb_ep->req_list);
1387 /* it this is the head of the queue, start i/o ... */
1388 if (!musb_ep->busy && &request->list == musb_ep->req_list.next)
1389 musb_ep_restart(musb, request);
1392 spin_unlock_irqrestore(&musb->lock, lockflags);
1396 static int musb_gadget_dequeue(struct usb_ep *ep, struct usb_request *request)
1398 struct musb_ep *musb_ep = to_musb_ep(ep);
1399 struct musb_request *req = to_musb_request(request);
1400 struct musb_request *r;
1401 unsigned long flags;
1403 struct musb *musb = musb_ep->musb;
1405 if (!ep || !request || to_musb_request(request)->ep != musb_ep)
1408 spin_lock_irqsave(&musb->lock, flags);
1410 list_for_each_entry(r, &musb_ep->req_list, list) {
1415 dev_dbg(musb->controller, "request %p not queued to %s\n", request, ep->name);
1420 /* if the hardware doesn't have the request, easy ... */
1421 if (musb_ep->req_list.next != &req->list || musb_ep->busy)
1422 musb_g_giveback(musb_ep, request, -ECONNRESET);
1424 /* ... else abort the dma transfer ... */
1425 else if (is_dma_capable() && musb_ep->dma) {
1426 struct dma_controller *c = musb->dma_controller;
1428 musb_ep_select(musb->mregs, musb_ep->current_epnum);
1429 if (c->channel_abort)
1430 status = c->channel_abort(musb_ep->dma);
1434 musb_g_giveback(musb_ep, request, -ECONNRESET);
1436 /* NOTE: by sticking to easily tested hardware/driver states,
1437 * we leave counting of in-flight packets imprecise.
1439 musb_g_giveback(musb_ep, request, -ECONNRESET);
1443 spin_unlock_irqrestore(&musb->lock, flags);
1448 * Set or clear the halt bit of an endpoint. A halted enpoint won't tx/rx any
1449 * data but will queue requests.
1451 * exported to ep0 code
1453 static int musb_gadget_set_halt(struct usb_ep *ep, int value)
1455 struct musb_ep *musb_ep = to_musb_ep(ep);
1456 u8 epnum = musb_ep->current_epnum;
1457 struct musb *musb = musb_ep->musb;
1458 void __iomem *epio = musb->endpoints[epnum].regs;
1459 void __iomem *mbase;
1460 unsigned long flags;
1462 struct musb_request *request;
1467 mbase = musb->mregs;
1469 spin_lock_irqsave(&musb->lock, flags);
1471 if ((USB_ENDPOINT_XFER_ISOC == musb_ep->type)) {
1476 musb_ep_select(mbase, epnum);
1478 request = next_request(musb_ep);
1481 dev_dbg(musb->controller, "request in progress, cannot halt %s\n",
1486 /* Cannot portably stall with non-empty FIFO */
1487 if (musb_ep->is_in) {
1488 csr = musb_readw(epio, MUSB_TXCSR);
1489 if (csr & MUSB_TXCSR_FIFONOTEMPTY) {
1490 dev_dbg(musb->controller, "FIFO busy, cannot halt %s\n", ep->name);
1496 musb_ep->wedged = 0;
1498 /* set/clear the stall and toggle bits */
1499 dev_dbg(musb->controller, "%s: %s stall\n", ep->name, value ? "set" : "clear");
1500 if (musb_ep->is_in) {
1501 csr = musb_readw(epio, MUSB_TXCSR);
1502 csr |= MUSB_TXCSR_P_WZC_BITS
1503 | MUSB_TXCSR_CLRDATATOG;
1505 csr |= MUSB_TXCSR_P_SENDSTALL;
1507 csr &= ~(MUSB_TXCSR_P_SENDSTALL
1508 | MUSB_TXCSR_P_SENTSTALL);
1509 csr &= ~MUSB_TXCSR_TXPKTRDY;
1510 musb_writew(epio, MUSB_TXCSR, csr);
1512 csr = musb_readw(epio, MUSB_RXCSR);
1513 csr |= MUSB_RXCSR_P_WZC_BITS
1514 | MUSB_RXCSR_FLUSHFIFO
1515 | MUSB_RXCSR_CLRDATATOG;
1517 csr |= MUSB_RXCSR_P_SENDSTALL;
1519 csr &= ~(MUSB_RXCSR_P_SENDSTALL
1520 | MUSB_RXCSR_P_SENTSTALL);
1521 musb_writew(epio, MUSB_RXCSR, csr);
1524 /* maybe start the first request in the queue */
1525 if (!musb_ep->busy && !value && request) {
1526 dev_dbg(musb->controller, "restarting the request\n");
1527 musb_ep_restart(musb, request);
1531 spin_unlock_irqrestore(&musb->lock, flags);
1537 * Sets the halt feature with the clear requests ignored
1539 static int musb_gadget_set_wedge(struct usb_ep *ep)
1541 struct musb_ep *musb_ep = to_musb_ep(ep);
1546 musb_ep->wedged = 1;
1548 return usb_ep_set_halt(ep);
1552 static int musb_gadget_fifo_status(struct usb_ep *ep)
1554 struct musb_ep *musb_ep = to_musb_ep(ep);
1555 void __iomem *epio = musb_ep->hw_ep->regs;
1556 int retval = -EINVAL;
1558 if (musb_ep->desc && !musb_ep->is_in) {
1559 struct musb *musb = musb_ep->musb;
1560 int epnum = musb_ep->current_epnum;
1561 void __iomem *mbase = musb->mregs;
1562 unsigned long flags;
1564 spin_lock_irqsave(&musb->lock, flags);
1566 musb_ep_select(mbase, epnum);
1567 /* FIXME return zero unless RXPKTRDY is set */
1568 retval = musb_readw(epio, MUSB_RXCOUNT);
1570 spin_unlock_irqrestore(&musb->lock, flags);
1575 static void musb_gadget_fifo_flush(struct usb_ep *ep)
1577 struct musb_ep *musb_ep = to_musb_ep(ep);
1578 struct musb *musb = musb_ep->musb;
1579 u8 epnum = musb_ep->current_epnum;
1580 void __iomem *epio = musb->endpoints[epnum].regs;
1581 void __iomem *mbase;
1582 unsigned long flags;
1585 mbase = musb->mregs;
1587 spin_lock_irqsave(&musb->lock, flags);
1588 musb_ep_select(mbase, (u8) epnum);
1590 /* disable interrupts */
1591 int_txe = musb_readw(mbase, MUSB_INTRTXE);
1592 musb_writew(mbase, MUSB_INTRTXE, int_txe & ~(1 << epnum));
1594 if (musb_ep->is_in) {
1595 csr = musb_readw(epio, MUSB_TXCSR);
1596 if (csr & MUSB_TXCSR_FIFONOTEMPTY) {
1597 csr |= MUSB_TXCSR_FLUSHFIFO | MUSB_TXCSR_P_WZC_BITS;
1599 * Setting both TXPKTRDY and FLUSHFIFO makes controller
1600 * to interrupt current FIFO loading, but not flushing
1601 * the already loaded ones.
1603 csr &= ~MUSB_TXCSR_TXPKTRDY;
1604 musb_writew(epio, MUSB_TXCSR, csr);
1605 /* REVISIT may be inappropriate w/o FIFONOTEMPTY ... */
1606 musb_writew(epio, MUSB_TXCSR, csr);
1609 csr = musb_readw(epio, MUSB_RXCSR);
1610 csr |= MUSB_RXCSR_FLUSHFIFO | MUSB_RXCSR_P_WZC_BITS;
1611 musb_writew(epio, MUSB_RXCSR, csr);
1612 musb_writew(epio, MUSB_RXCSR, csr);
1615 /* re-enable interrupt */
1616 musb_writew(mbase, MUSB_INTRTXE, int_txe);
1617 spin_unlock_irqrestore(&musb->lock, flags);
1620 static const struct usb_ep_ops musb_ep_ops = {
1621 .enable = musb_gadget_enable,
1622 .disable = musb_gadget_disable,
1623 .alloc_request = musb_alloc_request,
1624 .free_request = musb_free_request,
1625 .queue = musb_gadget_queue,
1626 .dequeue = musb_gadget_dequeue,
1627 .set_halt = musb_gadget_set_halt,
1629 .set_wedge = musb_gadget_set_wedge,
1631 .fifo_status = musb_gadget_fifo_status,
1632 .fifo_flush = musb_gadget_fifo_flush
1635 /* ----------------------------------------------------------------------- */
1637 static int musb_gadget_get_frame(struct usb_gadget *gadget)
1639 struct musb *musb = gadget_to_musb(gadget);
1641 return (int)musb_readw(musb->mregs, MUSB_FRAME);
1644 static int musb_gadget_wakeup(struct usb_gadget *gadget)
1647 struct musb *musb = gadget_to_musb(gadget);
1648 void __iomem *mregs = musb->mregs;
1649 unsigned long flags;
1650 int status = -EINVAL;
1654 spin_lock_irqsave(&musb->lock, flags);
1656 switch (musb->xceiv->state) {
1657 case OTG_STATE_B_PERIPHERAL:
1658 /* NOTE: OTG state machine doesn't include B_SUSPENDED;
1659 * that's part of the standard usb 1.1 state machine, and
1660 * doesn't affect OTG transitions.
1662 if (musb->may_wakeup && musb->is_suspended)
1665 case OTG_STATE_B_IDLE:
1666 /* Start SRP ... OTG not required. */
1667 devctl = musb_readb(mregs, MUSB_DEVCTL);
1668 dev_dbg(musb->controller, "Sending SRP: devctl: %02x\n", devctl);
1669 devctl |= MUSB_DEVCTL_SESSION;
1670 musb_writeb(mregs, MUSB_DEVCTL, devctl);
1671 devctl = musb_readb(mregs, MUSB_DEVCTL);
1673 while (!(devctl & MUSB_DEVCTL_SESSION)) {
1674 devctl = musb_readb(mregs, MUSB_DEVCTL);
1679 while (devctl & MUSB_DEVCTL_SESSION) {
1680 devctl = musb_readb(mregs, MUSB_DEVCTL);
1685 spin_unlock_irqrestore(&musb->lock, flags);
1686 otg_start_srp(musb->xceiv->otg);
1687 spin_lock_irqsave(&musb->lock, flags);
1689 /* Block idling for at least 1s */
1690 musb_platform_try_idle(musb,
1691 jiffies + msecs_to_jiffies(1 * HZ));
1696 dev_dbg(musb->controller, "Unhandled wake: %s\n",
1697 otg_state_string(musb->xceiv->state));
1703 power = musb_readb(mregs, MUSB_POWER);
1704 power |= MUSB_POWER_RESUME;
1705 musb_writeb(mregs, MUSB_POWER, power);
1706 dev_dbg(musb->controller, "issue wakeup\n");
1708 /* FIXME do this next chunk in a timer callback, no udelay */
1711 power = musb_readb(mregs, MUSB_POWER);
1712 power &= ~MUSB_POWER_RESUME;
1713 musb_writeb(mregs, MUSB_POWER, power);
1715 spin_unlock_irqrestore(&musb->lock, flags);
1723 musb_gadget_set_self_powered(struct usb_gadget *gadget, int is_selfpowered)
1725 struct musb *musb = gadget_to_musb(gadget);
1727 musb->is_self_powered = !!is_selfpowered;
1731 static void musb_pullup(struct musb *musb, int is_on)
1735 power = musb_readb(musb->mregs, MUSB_POWER);
1737 power |= MUSB_POWER_SOFTCONN;
1739 power &= ~MUSB_POWER_SOFTCONN;
1741 /* FIXME if on, HdrcStart; if off, HdrcStop */
1743 dev_dbg(musb->controller, "gadget D+ pullup %s\n",
1744 is_on ? "on" : "off");
1745 musb_writeb(musb->mregs, MUSB_POWER, power);
1749 static int musb_gadget_vbus_session(struct usb_gadget *gadget, int is_active)
1751 dev_dbg(musb->controller, "<= %s =>\n", __func__);
1754 * FIXME iff driver's softconnect flag is set (as it is during probe,
1755 * though that can clear it), just musb_pullup().
1762 static int musb_gadget_vbus_draw(struct usb_gadget *gadget, unsigned mA)
1765 struct musb *musb = gadget_to_musb(gadget);
1767 if (!musb->xceiv->set_power)
1769 return usb_phy_set_power(musb->xceiv, mA);
1775 static int musb_gadget_pullup(struct usb_gadget *gadget, int is_on)
1777 struct musb *musb = gadget_to_musb(gadget);
1778 unsigned long flags;
1782 pm_runtime_get_sync(musb->controller);
1784 /* NOTE: this assumes we are sensing vbus; we'd rather
1785 * not pullup unless the B-session is active.
1787 spin_lock_irqsave(&musb->lock, flags);
1788 if (is_on != musb->softconnect) {
1789 musb->softconnect = is_on;
1790 musb_pullup(musb, is_on);
1792 spin_unlock_irqrestore(&musb->lock, flags);
1794 pm_runtime_put(musb->controller);
1800 static int musb_gadget_start(struct usb_gadget *g,
1801 struct usb_gadget_driver *driver);
1802 static int musb_gadget_stop(struct usb_gadget *g,
1803 struct usb_gadget_driver *driver);
1806 static const struct usb_gadget_ops musb_gadget_operations = {
1807 .get_frame = musb_gadget_get_frame,
1808 .wakeup = musb_gadget_wakeup,
1809 .set_selfpowered = musb_gadget_set_self_powered,
1810 /* .vbus_session = musb_gadget_vbus_session, */
1811 .vbus_draw = musb_gadget_vbus_draw,
1812 .pullup = musb_gadget_pullup,
1814 .udc_start = musb_gadget_start,
1815 .udc_stop = musb_gadget_stop,
1819 /* ----------------------------------------------------------------------- */
1823 /* Only this registration code "knows" the rule (from USB standards)
1824 * about there being only one external upstream port. It assumes
1825 * all peripheral ports are external...
1829 static void musb_gadget_release(struct device *dev)
1831 /* kref_put(WHAT) */
1832 dev_dbg(dev, "%s\n", __func__);
1837 static void __devinit
1838 init_peripheral_ep(struct musb *musb, struct musb_ep *ep, u8 epnum, int is_in)
1840 struct musb_hw_ep *hw_ep = musb->endpoints + epnum;
1842 memset(ep, 0, sizeof *ep);
1844 ep->current_epnum = epnum;
1849 INIT_LIST_HEAD(&ep->req_list);
1851 sprintf(ep->name, "ep%d%s", epnum,
1852 (!epnum || hw_ep->is_shared_fifo) ? "" : (
1853 is_in ? "in" : "out"));
1854 ep->end_point.name = ep->name;
1855 INIT_LIST_HEAD(&ep->end_point.ep_list);
1857 ep->end_point.maxpacket = 64;
1858 ep->end_point.ops = &musb_g_ep0_ops;
1859 musb->g.ep0 = &ep->end_point;
1862 ep->end_point.maxpacket = hw_ep->max_packet_sz_tx;
1864 ep->end_point.maxpacket = hw_ep->max_packet_sz_rx;
1865 ep->end_point.ops = &musb_ep_ops;
1866 list_add_tail(&ep->end_point.ep_list, &musb->g.ep_list);
1871 * Initialize the endpoints exposed to peripheral drivers, with backlinks
1872 * to the rest of the driver state.
1874 static inline void __devinit musb_g_init_endpoints(struct musb *musb)
1877 struct musb_hw_ep *hw_ep;
1880 /* initialize endpoint list just once */
1881 INIT_LIST_HEAD(&(musb->g.ep_list));
1883 for (epnum = 0, hw_ep = musb->endpoints;
1884 epnum < musb->nr_endpoints;
1886 if (hw_ep->is_shared_fifo /* || !epnum */) {
1887 init_peripheral_ep(musb, &hw_ep->ep_in, epnum, 0);
1890 if (hw_ep->max_packet_sz_tx) {
1891 init_peripheral_ep(musb, &hw_ep->ep_in,
1895 if (hw_ep->max_packet_sz_rx) {
1896 init_peripheral_ep(musb, &hw_ep->ep_out,
1904 /* called once during driver setup to initialize and link into
1905 * the driver model; memory is zeroed.
1907 int __devinit musb_gadget_setup(struct musb *musb)
1911 /* REVISIT minor race: if (erroneously) setting up two
1912 * musb peripherals at the same time, only the bus lock
1916 musb->g.ops = &musb_gadget_operations;
1918 musb->g.max_speed = USB_SPEED_HIGH;
1920 musb->g.speed = USB_SPEED_UNKNOWN;
1923 /* this "gadget" abstracts/virtualizes the controller */
1924 dev_set_name(&musb->g.dev, "gadget");
1925 musb->g.dev.parent = musb->controller;
1926 musb->g.dev.dma_mask = musb->controller->dma_mask;
1927 musb->g.dev.release = musb_gadget_release;
1929 musb->g.name = musb_driver_name;
1932 if (is_otg_enabled(musb))
1936 musb_g_init_endpoints(musb);
1938 musb->is_active = 0;
1939 musb_platform_try_idle(musb, 0);
1942 status = device_register(&musb->g.dev);
1944 put_device(&musb->g.dev);
1947 status = usb_add_gadget_udc(musb->controller, &musb->g);
1955 musb->g.dev.parent = NULL;
1956 device_unregister(&musb->g.dev);
1961 void musb_gadget_cleanup(struct musb *musb)
1964 usb_del_gadget_udc(&musb->g);
1965 if (musb->g.dev.parent)
1966 device_unregister(&musb->g.dev);
1971 * Register the gadget driver. Used by gadget drivers when
1972 * registering themselves with the controller.
1974 * -EINVAL something went wrong (not driver)
1975 * -EBUSY another gadget is already using the controller
1976 * -ENOMEM no memory to perform the operation
1978 * @param driver the gadget driver
1979 * @return <0 if error, 0 if everything is fine
1982 static int musb_gadget_start(struct usb_gadget *g,
1983 struct usb_gadget_driver *driver)
1985 int musb_gadget_start(struct usb_gadget *g,
1986 struct usb_gadget_driver *driver)
1989 struct musb *musb = gadget_to_musb(g);
1991 struct usb_otg *otg = musb->xceiv->otg;
1993 unsigned long flags;
1994 int retval = -EINVAL;
1997 if (driver->max_speed < USB_SPEED_HIGH)
2001 pm_runtime_get_sync(musb->controller);
2004 dev_dbg(musb->controller, "registering driver %s\n", driver->function);
2007 musb->softconnect = 0;
2008 musb->gadget_driver = driver;
2010 spin_lock_irqsave(&musb->lock, flags);
2011 musb->is_active = 1;
2014 otg_set_peripheral(otg, &musb->g);
2015 musb->xceiv->state = OTG_STATE_B_IDLE;
2018 * FIXME this ignores the softconnect flag. Drivers are
2019 * allowed hold the peripheral inactive until for example
2020 * userspace hooks up printer hardware or DSP codecs, so
2021 * hosts only see fully functional devices.
2024 if (!is_otg_enabled(musb))
2028 spin_unlock_irqrestore(&musb->lock, flags);
2031 if (is_otg_enabled(musb)) {
2032 struct usb_hcd *hcd = musb_to_hcd(musb);
2034 dev_dbg(musb->controller, "OTG startup...\n");
2036 /* REVISIT: funcall to other code, which also
2037 * handles power budgeting ... this way also
2038 * ensures HdrcStart is indirectly called.
2040 retval = usb_add_hcd(musb_to_hcd(musb), 0, 0);
2042 dev_dbg(musb->controller, "add_hcd failed, %d\n", retval);
2046 if ((musb->xceiv->last_event == USB_EVENT_ID)
2048 otg_set_vbus(otg, 1);
2050 hcd->self.uses_pio_for_control = 1;
2052 if (musb->xceiv->last_event == USB_EVENT_NONE)
2053 pm_runtime_put(musb->controller);
2060 if (!is_otg_enabled(musb))
2068 static void stop_activity(struct musb *musb, struct usb_gadget_driver *driver)
2071 struct musb_hw_ep *hw_ep;
2073 /* don't disconnect if it's not connected */
2074 if (musb->g.speed == USB_SPEED_UNKNOWN)
2077 musb->g.speed = USB_SPEED_UNKNOWN;
2079 /* deactivate the hardware */
2080 if (musb->softconnect) {
2081 musb->softconnect = 0;
2082 musb_pullup(musb, 0);
2086 /* killing any outstanding requests will quiesce the driver;
2087 * then report disconnect
2090 for (i = 0, hw_ep = musb->endpoints;
2091 i < musb->nr_endpoints;
2093 musb_ep_select(musb->mregs, i);
2094 if (hw_ep->is_shared_fifo /* || !epnum */) {
2095 nuke(&hw_ep->ep_in, -ESHUTDOWN);
2097 if (hw_ep->max_packet_sz_tx)
2098 nuke(&hw_ep->ep_in, -ESHUTDOWN);
2099 if (hw_ep->max_packet_sz_rx)
2100 nuke(&hw_ep->ep_out, -ESHUTDOWN);
2107 * Unregister the gadget driver. Used by gadget drivers when
2108 * unregistering themselves from the controller.
2110 * @param driver the gadget driver to unregister
2112 static int musb_gadget_stop(struct usb_gadget *g,
2113 struct usb_gadget_driver *driver)
2115 struct musb *musb = gadget_to_musb(g);
2116 unsigned long flags;
2118 if (musb->xceiv->last_event == USB_EVENT_NONE)
2119 pm_runtime_get_sync(musb->controller);
2122 * REVISIT always use otg_set_peripheral() here too;
2123 * this needs to shut down the OTG engine.
2126 spin_lock_irqsave(&musb->lock, flags);
2128 musb_hnp_stop(musb);
2130 (void) musb_gadget_vbus_draw(&musb->g, 0);
2132 musb->xceiv->state = OTG_STATE_UNDEFINED;
2133 stop_activity(musb, driver);
2134 otg_set_peripheral(musb->xceiv->otg, NULL);
2136 dev_dbg(musb->controller, "unregistering driver %s\n", driver->function);
2138 musb->is_active = 0;
2139 musb_platform_try_idle(musb, 0);
2140 spin_unlock_irqrestore(&musb->lock, flags);
2142 if (is_otg_enabled(musb)) {
2143 usb_remove_hcd(musb_to_hcd(musb));
2144 /* FIXME we need to be able to register another
2145 * gadget driver here and have everything work;
2146 * that currently misbehaves.
2150 if (!is_otg_enabled(musb))
2153 pm_runtime_put(musb->controller);
2159 /* ----------------------------------------------------------------------- */
2161 /* lifecycle operations called through plat_uds.c */
2163 void musb_g_resume(struct musb *musb)
2166 musb->is_suspended = 0;
2167 switch (musb->xceiv->state) {
2168 case OTG_STATE_B_IDLE:
2170 case OTG_STATE_B_WAIT_ACON:
2171 case OTG_STATE_B_PERIPHERAL:
2172 musb->is_active = 1;
2173 if (musb->gadget_driver && musb->gadget_driver->resume) {
2174 spin_unlock(&musb->lock);
2175 musb->gadget_driver->resume(&musb->g);
2176 spin_lock(&musb->lock);
2180 WARNING("unhandled RESUME transition (%s)\n",
2181 otg_state_string(musb->xceiv->state));
2186 /* called when SOF packets stop for 3+ msec */
2187 void musb_g_suspend(struct musb *musb)
2192 devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
2193 dev_dbg(musb->controller, "devctl %02x\n", devctl);
2195 switch (musb->xceiv->state) {
2196 case OTG_STATE_B_IDLE:
2197 if ((devctl & MUSB_DEVCTL_VBUS) == MUSB_DEVCTL_VBUS)
2198 musb->xceiv->state = OTG_STATE_B_PERIPHERAL;
2200 case OTG_STATE_B_PERIPHERAL:
2201 musb->is_suspended = 1;
2202 if (musb->gadget_driver && musb->gadget_driver->suspend) {
2203 spin_unlock(&musb->lock);
2204 musb->gadget_driver->suspend(&musb->g);
2205 spin_lock(&musb->lock);
2209 /* REVISIT if B_HOST, clear DEVCTL.HOSTREQ;
2210 * A_PERIPHERAL may need care too
2212 WARNING("unhandled SUSPEND transition (%s)\n",
2213 otg_state_string(musb->xceiv->state));
2218 /* Called during SRP */
2219 void musb_g_wakeup(struct musb *musb)
2221 musb_gadget_wakeup(&musb->g);
2224 /* called when VBUS drops below session threshold, and in other cases */
2225 void musb_g_disconnect(struct musb *musb)
2227 void __iomem *mregs = musb->mregs;
2228 u8 devctl = musb_readb(mregs, MUSB_DEVCTL);
2230 dev_dbg(musb->controller, "devctl %02x\n", devctl);
2233 musb_writeb(mregs, MUSB_DEVCTL, devctl & MUSB_DEVCTL_SESSION);
2235 /* don't draw vbus until new b-default session */
2236 (void) musb_gadget_vbus_draw(&musb->g, 0);
2238 musb->g.speed = USB_SPEED_UNKNOWN;
2239 if (musb->gadget_driver && musb->gadget_driver->disconnect) {
2240 spin_unlock(&musb->lock);
2241 musb->gadget_driver->disconnect(&musb->g);
2242 spin_lock(&musb->lock);
2246 switch (musb->xceiv->state) {
2248 dev_dbg(musb->controller, "Unhandled disconnect %s, setting a_idle\n",
2249 otg_state_string(musb->xceiv->state));
2250 musb->xceiv->state = OTG_STATE_A_IDLE;
2251 MUSB_HST_MODE(musb);
2253 case OTG_STATE_A_PERIPHERAL:
2254 musb->xceiv->state = OTG_STATE_A_WAIT_BCON;
2255 MUSB_HST_MODE(musb);
2257 case OTG_STATE_B_WAIT_ACON:
2258 case OTG_STATE_B_HOST:
2259 case OTG_STATE_B_PERIPHERAL:
2260 case OTG_STATE_B_IDLE:
2261 musb->xceiv->state = OTG_STATE_B_IDLE;
2263 case OTG_STATE_B_SRP_INIT:
2268 musb->is_active = 0;
2271 void musb_g_reset(struct musb *musb)
2272 __releases(musb->lock)
2273 __acquires(musb->lock)
2275 void __iomem *mbase = musb->mregs;
2276 u8 devctl = musb_readb(mbase, MUSB_DEVCTL);
2280 dev_dbg(musb->controller, "<== %s addr=%x driver '%s'\n",
2281 (devctl & MUSB_DEVCTL_BDEVICE)
2282 ? "B-Device" : "A-Device",
2283 musb_readb(mbase, MUSB_FADDR),
2285 ? musb->gadget_driver->driver.name
2290 /* report disconnect, if we didn't already (flushing EP state) */
2291 if (musb->g.speed != USB_SPEED_UNKNOWN)
2292 musb_g_disconnect(musb);
2295 else if (devctl & MUSB_DEVCTL_HR)
2296 musb_writeb(mbase, MUSB_DEVCTL, MUSB_DEVCTL_SESSION);
2299 /* what speed did we negotiate? */
2300 power = musb_readb(mbase, MUSB_POWER);
2301 musb->g.speed = (power & MUSB_POWER_HSMODE)
2302 ? USB_SPEED_HIGH : USB_SPEED_FULL;
2304 /* start in USB_STATE_DEFAULT */
2305 musb->is_active = 1;
2306 musb->is_suspended = 0;
2307 MUSB_DEV_MODE(musb);
2309 musb->ep0_state = MUSB_EP0_STAGE_SETUP;
2311 musb->may_wakeup = 0;
2312 musb->g.b_hnp_enable = 0;
2313 musb->g.a_alt_hnp_support = 0;
2314 musb->g.a_hnp_support = 0;
2317 /* Normal reset, as B-Device;
2318 * or else after HNP, as A-Device
2320 if (devctl & MUSB_DEVCTL_BDEVICE) {
2321 musb->xceiv->state = OTG_STATE_B_PERIPHERAL;
2322 musb->g.is_a_peripheral = 0;
2323 } else if (is_otg_enabled(musb)) {
2324 musb->xceiv->state = OTG_STATE_A_PERIPHERAL;
2325 musb->g.is_a_peripheral = 1;
2329 /* start with default limits on VBUS power draw */
2330 (void) musb_gadget_vbus_draw(&musb->g,
2331 is_otg_enabled(musb) ? 8 : 100);