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.
36 #include <linux/kernel.h>
37 #include <linux/list.h>
38 #include <linux/timer.h>
39 #include <linux/module.h>
40 #include <linux/smp.h>
41 #include <linux/spinlock.h>
42 #include <linux/delay.h>
43 #include <linux/dma-mapping.h>
44 #include <linux/slab.h>
46 #include "musb_core.h"
49 /* ----------------------------------------------------------------------- */
51 #define is_buffer_mapped(req) (is_dma_capable() && \
52 (req->map_state != UN_MAPPED))
54 /* Maps the buffer to dma */
56 static inline void map_dma_buffer(struct musb_request
*request
,
57 struct musb
*musb
, struct musb_ep
*musb_ep
)
59 int compatible
= true;
60 struct dma_controller
*dma
= musb
->dma_controller
;
62 request
->map_state
= UN_MAPPED
;
64 if (!is_dma_capable() || !musb_ep
->dma
)
67 /* Check if DMA engine can handle this request.
68 * DMA code must reject the USB request explicitly.
69 * Default behaviour is to map the request.
71 if (dma
->is_compatible
)
72 compatible
= dma
->is_compatible(musb_ep
->dma
,
73 musb_ep
->packet_sz
, request
->request
.buf
,
74 request
->request
.length
);
78 if (request
->request
.dma
== DMA_ADDR_INVALID
) {
82 dma_addr
= dma_map_single(
85 request
->request
.length
,
89 ret
= dma_mapping_error(musb
->controller
, dma_addr
);
93 request
->request
.dma
= dma_addr
;
94 request
->map_state
= MUSB_MAPPED
;
96 dma_sync_single_for_device(musb
->controller
,
98 request
->request
.length
,
102 request
->map_state
= PRE_MAPPED
;
106 /* Unmap the buffer from dma and maps it back to cpu */
107 static inline void unmap_dma_buffer(struct musb_request
*request
,
110 struct musb_ep
*musb_ep
= request
->ep
;
112 if (!is_buffer_mapped(request
) || !musb_ep
->dma
)
115 if (request
->request
.dma
== DMA_ADDR_INVALID
) {
116 dev_vdbg(musb
->controller
,
117 "not unmapping a never mapped buffer\n");
120 if (request
->map_state
== MUSB_MAPPED
) {
121 dma_unmap_single(musb
->controller
,
122 request
->request
.dma
,
123 request
->request
.length
,
127 request
->request
.dma
= DMA_ADDR_INVALID
;
128 } else { /* PRE_MAPPED */
129 dma_sync_single_for_cpu(musb
->controller
,
130 request
->request
.dma
,
131 request
->request
.length
,
136 request
->map_state
= UN_MAPPED
;
140 * Immediately complete a request.
142 * @param request the request to complete
143 * @param status the status to complete the request with
144 * Context: controller locked, IRQs blocked.
146 void musb_g_giveback(
148 struct usb_request
*request
,
150 __releases(ep
->musb
->lock
)
151 __acquires(ep
->musb
->lock
)
153 struct musb_request
*req
;
157 req
= to_musb_request(request
);
159 list_del(&req
->list
);
160 if (req
->request
.status
== -EINPROGRESS
)
161 req
->request
.status
= status
;
165 spin_unlock(&musb
->lock
);
167 if (!dma_mapping_error(&musb
->g
.dev
, request
->dma
))
168 unmap_dma_buffer(req
, musb
);
170 if (request
->status
== 0)
171 dev_dbg(musb
->controller
, "%s done request %p, %d/%d\n",
172 ep
->end_point
.name
, request
,
173 req
->request
.actual
, req
->request
.length
);
175 dev_dbg(musb
->controller
, "%s request %p, %d/%d fault %d\n",
176 ep
->end_point
.name
, request
,
177 req
->request
.actual
, req
->request
.length
,
179 usb_gadget_giveback_request(&req
->ep
->end_point
, &req
->request
);
180 spin_lock(&musb
->lock
);
184 /* ----------------------------------------------------------------------- */
187 * Abort requests queued to an endpoint using the status. Synchronous.
188 * caller locked controller and blocked irqs, and selected this ep.
190 static void nuke(struct musb_ep
*ep
, const int status
)
192 struct musb
*musb
= ep
->musb
;
193 struct musb_request
*req
= NULL
;
194 void __iomem
*epio
= ep
->musb
->endpoints
[ep
->current_epnum
].regs
;
198 if (is_dma_capable() && ep
->dma
) {
199 struct dma_controller
*c
= ep
->musb
->dma_controller
;
204 * The programming guide says that we must not clear
205 * the DMAMODE bit before DMAENAB, so we only
206 * clear it in the second write...
208 musb_writew(epio
, MUSB_TXCSR
,
209 MUSB_TXCSR_DMAMODE
| MUSB_TXCSR_FLUSHFIFO
);
210 musb_writew(epio
, MUSB_TXCSR
,
211 0 | MUSB_TXCSR_FLUSHFIFO
);
213 musb_writew(epio
, MUSB_RXCSR
,
214 0 | MUSB_RXCSR_FLUSHFIFO
);
215 musb_writew(epio
, MUSB_RXCSR
,
216 0 | MUSB_RXCSR_FLUSHFIFO
);
219 value
= c
->channel_abort(ep
->dma
);
220 dev_dbg(musb
->controller
, "%s: abort DMA --> %d\n",
222 c
->channel_release(ep
->dma
);
226 while (!list_empty(&ep
->req_list
)) {
227 req
= list_first_entry(&ep
->req_list
, struct musb_request
, list
);
228 musb_g_giveback(ep
, &req
->request
, status
);
232 /* ----------------------------------------------------------------------- */
234 /* Data transfers - pure PIO, pure DMA, or mixed mode */
237 * This assumes the separate CPPI engine is responding to DMA requests
238 * from the usb core ... sequenced a bit differently from mentor dma.
241 static inline int max_ep_writesize(struct musb
*musb
, struct musb_ep
*ep
)
243 if (can_bulk_split(musb
, ep
->type
))
244 return ep
->hw_ep
->max_packet_sz_tx
;
246 return ep
->packet_sz
;
250 * An endpoint is transmitting data. This can be called either from
251 * the IRQ routine or from ep.queue() to kickstart a request on an
254 * Context: controller locked, IRQs blocked, endpoint selected
256 static void txstate(struct musb
*musb
, struct musb_request
*req
)
258 u8 epnum
= req
->epnum
;
259 struct musb_ep
*musb_ep
;
260 void __iomem
*epio
= musb
->endpoints
[epnum
].regs
;
261 struct usb_request
*request
;
262 u16 fifo_count
= 0, csr
;
267 /* Check if EP is disabled */
268 if (!musb_ep
->desc
) {
269 dev_dbg(musb
->controller
, "ep:%s disabled - ignore request\n",
270 musb_ep
->end_point
.name
);
274 /* we shouldn't get here while DMA is active ... but we do ... */
275 if (dma_channel_status(musb_ep
->dma
) == MUSB_DMA_STATUS_BUSY
) {
276 dev_dbg(musb
->controller
, "dma pending...\n");
280 /* read TXCSR before */
281 csr
= musb_readw(epio
, MUSB_TXCSR
);
283 request
= &req
->request
;
284 fifo_count
= min(max_ep_writesize(musb
, musb_ep
),
285 (int)(request
->length
- request
->actual
));
287 if (csr
& MUSB_TXCSR_TXPKTRDY
) {
288 dev_dbg(musb
->controller
, "%s old packet still ready , txcsr %03x\n",
289 musb_ep
->end_point
.name
, csr
);
293 if (csr
& MUSB_TXCSR_P_SENDSTALL
) {
294 dev_dbg(musb
->controller
, "%s stalling, txcsr %03x\n",
295 musb_ep
->end_point
.name
, csr
);
299 dev_dbg(musb
->controller
, "hw_ep%d, maxpacket %d, fifo count %d, txcsr %03x\n",
300 epnum
, musb_ep
->packet_sz
, fifo_count
,
303 #ifndef CONFIG_MUSB_PIO_ONLY
304 if (is_buffer_mapped(req
)) {
305 struct dma_controller
*c
= musb
->dma_controller
;
308 /* setup DMA, then program endpoint CSR */
309 request_size
= min_t(size_t, request
->length
- request
->actual
,
310 musb_ep
->dma
->max_len
);
312 use_dma
= (request
->dma
!= DMA_ADDR_INVALID
&& request_size
);
314 /* MUSB_TXCSR_P_ISO is still set correctly */
316 #if defined(CONFIG_USB_INVENTRA_DMA) || defined(CONFIG_USB_UX500_DMA)
318 if (request_size
< musb_ep
->packet_sz
)
319 musb_ep
->dma
->desired_mode
= 0;
321 musb_ep
->dma
->desired_mode
= 1;
323 use_dma
= use_dma
&& c
->channel_program(
324 musb_ep
->dma
, musb_ep
->packet_sz
,
325 musb_ep
->dma
->desired_mode
,
326 request
->dma
+ request
->actual
, request_size
);
328 if (musb_ep
->dma
->desired_mode
== 0) {
330 * We must not clear the DMAMODE bit
331 * before the DMAENAB bit -- and the
332 * latter doesn't always get cleared
333 * before we get here...
335 csr
&= ~(MUSB_TXCSR_AUTOSET
336 | MUSB_TXCSR_DMAENAB
);
337 musb_writew(epio
, MUSB_TXCSR
, csr
338 | MUSB_TXCSR_P_WZC_BITS
);
339 csr
&= ~MUSB_TXCSR_DMAMODE
;
340 csr
|= (MUSB_TXCSR_DMAENAB
|
342 /* against programming guide */
344 csr
|= (MUSB_TXCSR_DMAENAB
348 * Enable Autoset according to table
350 * bulk_split hb_mult Autoset_Enable
352 * 0 >0 No(High BW ISO)
356 if (!musb_ep
->hb_mult
||
360 csr
|= MUSB_TXCSR_AUTOSET
;
362 csr
&= ~MUSB_TXCSR_P_UNDERRUN
;
364 musb_writew(epio
, MUSB_TXCSR
, csr
);
369 if (is_cppi_enabled(musb
)) {
370 /* program endpoint CSR first, then setup DMA */
371 csr
&= ~(MUSB_TXCSR_P_UNDERRUN
| MUSB_TXCSR_TXPKTRDY
);
372 csr
|= MUSB_TXCSR_DMAENAB
| MUSB_TXCSR_DMAMODE
|
374 musb_writew(epio
, MUSB_TXCSR
, (MUSB_TXCSR_P_WZC_BITS
&
375 ~MUSB_TXCSR_P_UNDERRUN
) | csr
);
377 /* ensure writebuffer is empty */
378 csr
= musb_readw(epio
, MUSB_TXCSR
);
381 * NOTE host side sets DMAENAB later than this; both are
382 * OK since the transfer dma glue (between CPPI and
383 * Mentor fifos) just tells CPPI it could start. Data
384 * only moves to the USB TX fifo when both fifos are
388 * "mode" is irrelevant here; handle terminating ZLPs
389 * like PIO does, since the hardware RNDIS mode seems
390 * unreliable except for the
391 * last-packet-is-already-short case.
393 use_dma
= use_dma
&& c
->channel_program(
394 musb_ep
->dma
, musb_ep
->packet_sz
,
396 request
->dma
+ request
->actual
,
399 c
->channel_release(musb_ep
->dma
);
401 csr
&= ~MUSB_TXCSR_DMAENAB
;
402 musb_writew(epio
, MUSB_TXCSR
, csr
);
403 /* invariant: prequest->buf is non-null */
405 } else if (tusb_dma_omap(musb
))
406 use_dma
= use_dma
&& c
->channel_program(
407 musb_ep
->dma
, musb_ep
->packet_sz
,
409 request
->dma
+ request
->actual
,
416 * Unmap the dma buffer back to cpu if dma channel
419 unmap_dma_buffer(req
, musb
);
421 musb_write_fifo(musb_ep
->hw_ep
, fifo_count
,
422 (u8
*) (request
->buf
+ request
->actual
));
423 request
->actual
+= fifo_count
;
424 csr
|= MUSB_TXCSR_TXPKTRDY
;
425 csr
&= ~MUSB_TXCSR_P_UNDERRUN
;
426 musb_writew(epio
, MUSB_TXCSR
, csr
);
429 /* host may already have the data when this message shows... */
430 dev_dbg(musb
->controller
, "%s TX/IN %s len %d/%d, txcsr %04x, fifo %d/%d\n",
431 musb_ep
->end_point
.name
, use_dma
? "dma" : "pio",
432 request
->actual
, request
->length
,
433 musb_readw(epio
, MUSB_TXCSR
),
435 musb_readw(epio
, MUSB_TXMAXP
));
439 * FIFO state update (e.g. data ready).
440 * Called from IRQ, with controller locked.
442 void musb_g_tx(struct musb
*musb
, u8 epnum
)
445 struct musb_request
*req
;
446 struct usb_request
*request
;
447 u8 __iomem
*mbase
= musb
->mregs
;
448 struct musb_ep
*musb_ep
= &musb
->endpoints
[epnum
].ep_in
;
449 void __iomem
*epio
= musb
->endpoints
[epnum
].regs
;
450 struct dma_channel
*dma
;
452 musb_ep_select(mbase
, epnum
);
453 req
= next_request(musb_ep
);
454 request
= &req
->request
;
456 csr
= musb_readw(epio
, MUSB_TXCSR
);
457 dev_dbg(musb
->controller
, "<== %s, txcsr %04x\n", musb_ep
->end_point
.name
, csr
);
459 dma
= is_dma_capable() ? musb_ep
->dma
: NULL
;
462 * REVISIT: for high bandwidth, MUSB_TXCSR_P_INCOMPTX
463 * probably rates reporting as a host error.
465 if (csr
& MUSB_TXCSR_P_SENTSTALL
) {
466 csr
|= MUSB_TXCSR_P_WZC_BITS
;
467 csr
&= ~MUSB_TXCSR_P_SENTSTALL
;
468 musb_writew(epio
, MUSB_TXCSR
, csr
);
472 if (csr
& MUSB_TXCSR_P_UNDERRUN
) {
473 /* We NAKed, no big deal... little reason to care. */
474 csr
|= MUSB_TXCSR_P_WZC_BITS
;
475 csr
&= ~(MUSB_TXCSR_P_UNDERRUN
| MUSB_TXCSR_TXPKTRDY
);
476 musb_writew(epio
, MUSB_TXCSR
, csr
);
477 dev_vdbg(musb
->controller
, "underrun on ep%d, req %p\n",
481 if (dma_channel_status(dma
) == MUSB_DMA_STATUS_BUSY
) {
483 * SHOULD NOT HAPPEN... has with CPPI though, after
484 * changing SENDSTALL (and other cases); harmless?
486 dev_dbg(musb
->controller
, "%s dma still busy?\n", musb_ep
->end_point
.name
);
492 bool short_packet
= false;
494 if (dma
&& (csr
& MUSB_TXCSR_DMAENAB
)) {
496 csr
|= MUSB_TXCSR_P_WZC_BITS
;
497 csr
&= ~(MUSB_TXCSR_DMAENAB
| MUSB_TXCSR_P_UNDERRUN
|
498 MUSB_TXCSR_TXPKTRDY
| MUSB_TXCSR_AUTOSET
);
499 musb_writew(epio
, MUSB_TXCSR
, csr
);
500 /* Ensure writebuffer is empty. */
501 csr
= musb_readw(epio
, MUSB_TXCSR
);
502 request
->actual
+= musb_ep
->dma
->actual_len
;
503 dev_dbg(musb
->controller
, "TXCSR%d %04x, DMA off, len %zu, req %p\n",
504 epnum
, csr
, musb_ep
->dma
->actual_len
, request
);
508 * First, maybe a terminating short packet. Some DMA
509 * engines might handle this by themselves.
511 if ((request
->zero
&& request
->length
)
512 && (request
->length
% musb_ep
->packet_sz
== 0)
513 && (request
->actual
== request
->length
))
516 if ((musb_dma_inventra(musb
) || musb_dma_ux500(musb
)) &&
517 (is_dma
&& (!dma
->desired_mode
||
519 (musb_ep
->packet_sz
- 1)))))
524 * On DMA completion, FIFO may not be
527 if (csr
& MUSB_TXCSR_TXPKTRDY
)
530 dev_dbg(musb
->controller
, "sending zero pkt\n");
531 musb_writew(epio
, MUSB_TXCSR
, MUSB_TXCSR_MODE
532 | MUSB_TXCSR_TXPKTRDY
);
536 if (request
->actual
== request
->length
) {
537 musb_g_giveback(musb_ep
, request
, 0);
539 * In the giveback function the MUSB lock is
540 * released and acquired after sometime. During
541 * this time period the INDEX register could get
542 * changed by the gadget_queue function especially
543 * on SMP systems. Reselect the INDEX to be sure
544 * we are reading/modifying the right registers
546 musb_ep_select(mbase
, epnum
);
547 req
= musb_ep
->desc
? next_request(musb_ep
) : NULL
;
549 dev_dbg(musb
->controller
, "%s idle now\n",
550 musb_ep
->end_point
.name
);
559 /* ------------------------------------------------------------ */
562 * Context: controller locked, IRQs blocked, endpoint selected
564 static void rxstate(struct musb
*musb
, struct musb_request
*req
)
566 const u8 epnum
= req
->epnum
;
567 struct usb_request
*request
= &req
->request
;
568 struct musb_ep
*musb_ep
;
569 void __iomem
*epio
= musb
->endpoints
[epnum
].regs
;
572 u16 csr
= musb_readw(epio
, MUSB_RXCSR
);
573 struct musb_hw_ep
*hw_ep
= &musb
->endpoints
[epnum
];
576 if (hw_ep
->is_shared_fifo
)
577 musb_ep
= &hw_ep
->ep_in
;
579 musb_ep
= &hw_ep
->ep_out
;
581 fifo_count
= musb_ep
->packet_sz
;
583 /* Check if EP is disabled */
584 if (!musb_ep
->desc
) {
585 dev_dbg(musb
->controller
, "ep:%s disabled - ignore request\n",
586 musb_ep
->end_point
.name
);
590 /* We shouldn't get here while DMA is active, but we do... */
591 if (dma_channel_status(musb_ep
->dma
) == MUSB_DMA_STATUS_BUSY
) {
592 dev_dbg(musb
->controller
, "DMA pending...\n");
596 if (csr
& MUSB_RXCSR_P_SENDSTALL
) {
597 dev_dbg(musb
->controller
, "%s stalling, RXCSR %04x\n",
598 musb_ep
->end_point
.name
, csr
);
602 if (is_cppi_enabled(musb
) && is_buffer_mapped(req
)) {
603 struct dma_controller
*c
= musb
->dma_controller
;
604 struct dma_channel
*channel
= musb_ep
->dma
;
606 /* NOTE: CPPI won't actually stop advancing the DMA
607 * queue after short packet transfers, so this is almost
608 * always going to run as IRQ-per-packet DMA so that
609 * faults will be handled correctly.
611 if (c
->channel_program(channel
,
613 !request
->short_not_ok
,
614 request
->dma
+ request
->actual
,
615 request
->length
- request
->actual
)) {
617 /* make sure that if an rxpkt arrived after the irq,
618 * the cppi engine will be ready to take it as soon
621 csr
&= ~(MUSB_RXCSR_AUTOCLEAR
622 | MUSB_RXCSR_DMAMODE
);
623 csr
|= MUSB_RXCSR_DMAENAB
| MUSB_RXCSR_P_WZC_BITS
;
624 musb_writew(epio
, MUSB_RXCSR
, csr
);
629 if (csr
& MUSB_RXCSR_RXPKTRDY
) {
630 fifo_count
= musb_readw(epio
, MUSB_RXCOUNT
);
633 * Enable Mode 1 on RX transfers only when short_not_ok flag
634 * is set. Currently short_not_ok flag is set only from
635 * file_storage and f_mass_storage drivers
638 if (request
->short_not_ok
&& fifo_count
== musb_ep
->packet_sz
)
643 if (request
->actual
< request
->length
) {
644 #ifdef CONFIG_USB_INVENTRA_DMA
645 if (is_buffer_mapped(req
)) {
646 struct dma_controller
*c
;
647 struct dma_channel
*channel
;
649 unsigned int transfer_size
;
651 c
= musb
->dma_controller
;
652 channel
= musb_ep
->dma
;
654 /* We use DMA Req mode 0 in rx_csr, and DMA controller operates in
655 * mode 0 only. So we do not get endpoint interrupts due to DMA
656 * completion. We only get interrupts from DMA controller.
658 * We could operate in DMA mode 1 if we knew the size of the tranfer
659 * in advance. For mass storage class, request->length = what the host
660 * sends, so that'd work. But for pretty much everything else,
661 * request->length is routinely more than what the host sends. For
662 * most these gadgets, end of is signified either by a short packet,
663 * or filling the last byte of the buffer. (Sending extra data in
664 * that last pckate should trigger an overflow fault.) But in mode 1,
665 * we don't get DMA completion interrupt for short packets.
667 * Theoretically, we could enable DMAReq irq (MUSB_RXCSR_DMAMODE = 1),
668 * to get endpoint interrupt on every DMA req, but that didn't seem
671 * REVISIT an updated g_file_storage can set req->short_not_ok, which
672 * then becomes usable as a runtime "use mode 1" hint...
675 /* Experimental: Mode1 works with mass storage use cases */
677 csr
|= MUSB_RXCSR_AUTOCLEAR
;
678 musb_writew(epio
, MUSB_RXCSR
, csr
);
679 csr
|= MUSB_RXCSR_DMAENAB
;
680 musb_writew(epio
, MUSB_RXCSR
, csr
);
683 * this special sequence (enabling and then
684 * disabling MUSB_RXCSR_DMAMODE) is required
685 * to get DMAReq to activate
687 musb_writew(epio
, MUSB_RXCSR
,
688 csr
| MUSB_RXCSR_DMAMODE
);
689 musb_writew(epio
, MUSB_RXCSR
, csr
);
691 transfer_size
= min_t(unsigned int,
695 musb_ep
->dma
->desired_mode
= 1;
697 if (!musb_ep
->hb_mult
&&
698 musb_ep
->hw_ep
->rx_double_buffered
)
699 csr
|= MUSB_RXCSR_AUTOCLEAR
;
700 csr
|= MUSB_RXCSR_DMAENAB
;
701 musb_writew(epio
, MUSB_RXCSR
, csr
);
703 transfer_size
= min(request
->length
- request
->actual
,
704 (unsigned)fifo_count
);
705 musb_ep
->dma
->desired_mode
= 0;
708 use_dma
= c
->channel_program(
711 channel
->desired_mode
,
719 #elif defined(CONFIG_USB_UX500_DMA)
720 if ((is_buffer_mapped(req
)) &&
721 (request
->actual
< request
->length
)) {
723 struct dma_controller
*c
;
724 struct dma_channel
*channel
;
725 unsigned int transfer_size
= 0;
727 c
= musb
->dma_controller
;
728 channel
= musb_ep
->dma
;
730 /* In case first packet is short */
731 if (fifo_count
< musb_ep
->packet_sz
)
732 transfer_size
= fifo_count
;
733 else if (request
->short_not_ok
)
734 transfer_size
= min_t(unsigned int,
739 transfer_size
= min_t(unsigned int,
742 (unsigned)fifo_count
);
744 csr
&= ~MUSB_RXCSR_DMAMODE
;
745 csr
|= (MUSB_RXCSR_DMAENAB
|
746 MUSB_RXCSR_AUTOCLEAR
);
748 musb_writew(epio
, MUSB_RXCSR
, csr
);
750 if (transfer_size
<= musb_ep
->packet_sz
) {
751 musb_ep
->dma
->desired_mode
= 0;
753 musb_ep
->dma
->desired_mode
= 1;
754 /* Mode must be set after DMAENAB */
755 csr
|= MUSB_RXCSR_DMAMODE
;
756 musb_writew(epio
, MUSB_RXCSR
, csr
);
759 if (c
->channel_program(channel
,
761 channel
->desired_mode
,
768 #endif /* Mentor's DMA */
770 len
= request
->length
- request
->actual
;
771 dev_dbg(musb
->controller
, "%s OUT/RX pio fifo %d/%d, maxpacket %d\n",
772 musb_ep
->end_point
.name
,
776 fifo_count
= min_t(unsigned, len
, fifo_count
);
778 #ifdef CONFIG_USB_TUSB_OMAP_DMA
779 if (tusb_dma_omap(musb
) && is_buffer_mapped(req
)) {
780 struct dma_controller
*c
= musb
->dma_controller
;
781 struct dma_channel
*channel
= musb_ep
->dma
;
782 u32 dma_addr
= request
->dma
+ request
->actual
;
785 ret
= c
->channel_program(channel
,
787 channel
->desired_mode
,
795 * Unmap the dma buffer back to cpu if dma channel
796 * programming fails. This buffer is mapped if the
797 * channel allocation is successful
799 if (is_buffer_mapped(req
)) {
800 unmap_dma_buffer(req
, musb
);
803 * Clear DMAENAB and AUTOCLEAR for the
806 csr
&= ~(MUSB_RXCSR_DMAENAB
| MUSB_RXCSR_AUTOCLEAR
);
807 musb_writew(epio
, MUSB_RXCSR
, csr
);
810 musb_read_fifo(musb_ep
->hw_ep
, fifo_count
, (u8
*)
811 (request
->buf
+ request
->actual
));
812 request
->actual
+= fifo_count
;
814 /* REVISIT if we left anything in the fifo, flush
815 * it and report -EOVERFLOW
819 csr
|= MUSB_RXCSR_P_WZC_BITS
;
820 csr
&= ~MUSB_RXCSR_RXPKTRDY
;
821 musb_writew(epio
, MUSB_RXCSR
, csr
);
825 /* reach the end or short packet detected */
826 if (request
->actual
== request
->length
||
827 fifo_count
< musb_ep
->packet_sz
)
828 musb_g_giveback(musb_ep
, request
, 0);
832 * Data ready for a request; called from IRQ
834 void musb_g_rx(struct musb
*musb
, u8 epnum
)
837 struct musb_request
*req
;
838 struct usb_request
*request
;
839 void __iomem
*mbase
= musb
->mregs
;
840 struct musb_ep
*musb_ep
;
841 void __iomem
*epio
= musb
->endpoints
[epnum
].regs
;
842 struct dma_channel
*dma
;
843 struct musb_hw_ep
*hw_ep
= &musb
->endpoints
[epnum
];
845 if (hw_ep
->is_shared_fifo
)
846 musb_ep
= &hw_ep
->ep_in
;
848 musb_ep
= &hw_ep
->ep_out
;
850 musb_ep_select(mbase
, epnum
);
852 req
= next_request(musb_ep
);
856 request
= &req
->request
;
858 csr
= musb_readw(epio
, MUSB_RXCSR
);
859 dma
= is_dma_capable() ? musb_ep
->dma
: NULL
;
861 dev_dbg(musb
->controller
, "<== %s, rxcsr %04x%s %p\n", musb_ep
->end_point
.name
,
862 csr
, dma
? " (dma)" : "", request
);
864 if (csr
& MUSB_RXCSR_P_SENTSTALL
) {
865 csr
|= MUSB_RXCSR_P_WZC_BITS
;
866 csr
&= ~MUSB_RXCSR_P_SENTSTALL
;
867 musb_writew(epio
, MUSB_RXCSR
, csr
);
871 if (csr
& MUSB_RXCSR_P_OVERRUN
) {
872 /* csr |= MUSB_RXCSR_P_WZC_BITS; */
873 csr
&= ~MUSB_RXCSR_P_OVERRUN
;
874 musb_writew(epio
, MUSB_RXCSR
, csr
);
876 dev_dbg(musb
->controller
, "%s iso overrun on %p\n", musb_ep
->name
, request
);
877 if (request
->status
== -EINPROGRESS
)
878 request
->status
= -EOVERFLOW
;
880 if (csr
& MUSB_RXCSR_INCOMPRX
) {
881 /* REVISIT not necessarily an error */
882 dev_dbg(musb
->controller
, "%s, incomprx\n", musb_ep
->end_point
.name
);
885 if (dma_channel_status(dma
) == MUSB_DMA_STATUS_BUSY
) {
886 /* "should not happen"; likely RXPKTRDY pending for DMA */
887 dev_dbg(musb
->controller
, "%s busy, csr %04x\n",
888 musb_ep
->end_point
.name
, csr
);
892 if (dma
&& (csr
& MUSB_RXCSR_DMAENAB
)) {
893 csr
&= ~(MUSB_RXCSR_AUTOCLEAR
895 | MUSB_RXCSR_DMAMODE
);
896 musb_writew(epio
, MUSB_RXCSR
,
897 MUSB_RXCSR_P_WZC_BITS
| csr
);
899 request
->actual
+= musb_ep
->dma
->actual_len
;
901 dev_dbg(musb
->controller
, "RXCSR%d %04x, dma off, %04x, len %zu, req %p\n",
903 musb_readw(epio
, MUSB_RXCSR
),
904 musb_ep
->dma
->actual_len
, request
);
906 #if defined(CONFIG_USB_INVENTRA_DMA) || defined(CONFIG_USB_TUSB_OMAP_DMA) || \
907 defined(CONFIG_USB_UX500_DMA)
908 /* Autoclear doesn't clear RxPktRdy for short packets */
909 if ((dma
->desired_mode
== 0 && !hw_ep
->rx_double_buffered
)
911 & (musb_ep
->packet_sz
- 1))) {
913 csr
&= ~MUSB_RXCSR_RXPKTRDY
;
914 musb_writew(epio
, MUSB_RXCSR
, csr
);
917 /* incomplete, and not short? wait for next IN packet */
918 if ((request
->actual
< request
->length
)
919 && (musb_ep
->dma
->actual_len
920 == musb_ep
->packet_sz
)) {
921 /* In double buffer case, continue to unload fifo if
922 * there is Rx packet in FIFO.
924 csr
= musb_readw(epio
, MUSB_RXCSR
);
925 if ((csr
& MUSB_RXCSR_RXPKTRDY
) &&
926 hw_ep
->rx_double_buffered
)
931 musb_g_giveback(musb_ep
, request
, 0);
933 * In the giveback function the MUSB lock is
934 * released and acquired after sometime. During
935 * this time period the INDEX register could get
936 * changed by the gadget_queue function especially
937 * on SMP systems. Reselect the INDEX to be sure
938 * we are reading/modifying the right registers
940 musb_ep_select(mbase
, epnum
);
942 req
= next_request(musb_ep
);
946 #if defined(CONFIG_USB_INVENTRA_DMA) || defined(CONFIG_USB_TUSB_OMAP_DMA) || \
947 defined(CONFIG_USB_UX500_DMA)
950 /* Analyze request */
954 /* ------------------------------------------------------------ */
956 static int musb_gadget_enable(struct usb_ep
*ep
,
957 const struct usb_endpoint_descriptor
*desc
)
960 struct musb_ep
*musb_ep
;
961 struct musb_hw_ep
*hw_ep
;
968 int status
= -EINVAL
;
973 musb_ep
= to_musb_ep(ep
);
974 hw_ep
= musb_ep
->hw_ep
;
976 musb
= musb_ep
->musb
;
978 epnum
= musb_ep
->current_epnum
;
980 spin_lock_irqsave(&musb
->lock
, flags
);
986 musb_ep
->type
= usb_endpoint_type(desc
);
988 /* check direction and (later) maxpacket size against endpoint */
989 if (usb_endpoint_num(desc
) != epnum
)
992 /* REVISIT this rules out high bandwidth periodic transfers */
993 tmp
= usb_endpoint_maxp(desc
);
997 if (usb_endpoint_dir_in(desc
))
998 ok
= musb
->hb_iso_tx
;
1000 ok
= musb
->hb_iso_rx
;
1003 dev_dbg(musb
->controller
, "no support for high bandwidth ISO\n");
1006 musb_ep
->hb_mult
= (tmp
>> 11) & 3;
1008 musb_ep
->hb_mult
= 0;
1011 musb_ep
->packet_sz
= tmp
& 0x7ff;
1012 tmp
= musb_ep
->packet_sz
* (musb_ep
->hb_mult
+ 1);
1014 /* enable the interrupts for the endpoint, set the endpoint
1015 * packet size (or fail), set the mode, clear the fifo
1017 musb_ep_select(mbase
, epnum
);
1018 if (usb_endpoint_dir_in(desc
)) {
1020 if (hw_ep
->is_shared_fifo
)
1022 if (!musb_ep
->is_in
)
1025 if (tmp
> hw_ep
->max_packet_sz_tx
) {
1026 dev_dbg(musb
->controller
, "packet size beyond hardware FIFO size\n");
1030 musb
->intrtxe
|= (1 << epnum
);
1031 musb_writew(mbase
, MUSB_INTRTXE
, musb
->intrtxe
);
1033 /* REVISIT if can_bulk_split(), use by updating "tmp";
1034 * likewise high bandwidth periodic tx
1036 /* Set TXMAXP with the FIFO size of the endpoint
1037 * to disable double buffering mode.
1039 if (musb
->double_buffer_not_ok
) {
1040 musb_writew(regs
, MUSB_TXMAXP
, hw_ep
->max_packet_sz_tx
);
1042 if (can_bulk_split(musb
, musb_ep
->type
))
1043 musb_ep
->hb_mult
= (hw_ep
->max_packet_sz_tx
/
1044 musb_ep
->packet_sz
) - 1;
1045 musb_writew(regs
, MUSB_TXMAXP
, musb_ep
->packet_sz
1046 | (musb_ep
->hb_mult
<< 11));
1049 csr
= MUSB_TXCSR_MODE
| MUSB_TXCSR_CLRDATATOG
;
1050 if (musb_readw(regs
, MUSB_TXCSR
)
1051 & MUSB_TXCSR_FIFONOTEMPTY
)
1052 csr
|= MUSB_TXCSR_FLUSHFIFO
;
1053 if (musb_ep
->type
== USB_ENDPOINT_XFER_ISOC
)
1054 csr
|= MUSB_TXCSR_P_ISO
;
1056 /* set twice in case of double buffering */
1057 musb_writew(regs
, MUSB_TXCSR
, csr
);
1058 /* REVISIT may be inappropriate w/o FIFONOTEMPTY ... */
1059 musb_writew(regs
, MUSB_TXCSR
, csr
);
1063 if (hw_ep
->is_shared_fifo
)
1068 if (tmp
> hw_ep
->max_packet_sz_rx
) {
1069 dev_dbg(musb
->controller
, "packet size beyond hardware FIFO size\n");
1073 musb
->intrrxe
|= (1 << epnum
);
1074 musb_writew(mbase
, MUSB_INTRRXE
, musb
->intrrxe
);
1076 /* REVISIT if can_bulk_combine() use by updating "tmp"
1077 * likewise high bandwidth periodic rx
1079 /* Set RXMAXP with the FIFO size of the endpoint
1080 * to disable double buffering mode.
1082 if (musb
->double_buffer_not_ok
)
1083 musb_writew(regs
, MUSB_RXMAXP
, hw_ep
->max_packet_sz_tx
);
1085 musb_writew(regs
, MUSB_RXMAXP
, musb_ep
->packet_sz
1086 | (musb_ep
->hb_mult
<< 11));
1088 /* force shared fifo to OUT-only mode */
1089 if (hw_ep
->is_shared_fifo
) {
1090 csr
= musb_readw(regs
, MUSB_TXCSR
);
1091 csr
&= ~(MUSB_TXCSR_MODE
| MUSB_TXCSR_TXPKTRDY
);
1092 musb_writew(regs
, MUSB_TXCSR
, csr
);
1095 csr
= MUSB_RXCSR_FLUSHFIFO
| MUSB_RXCSR_CLRDATATOG
;
1096 if (musb_ep
->type
== USB_ENDPOINT_XFER_ISOC
)
1097 csr
|= MUSB_RXCSR_P_ISO
;
1098 else if (musb_ep
->type
== USB_ENDPOINT_XFER_INT
)
1099 csr
|= MUSB_RXCSR_DISNYET
;
1101 /* set twice in case of double buffering */
1102 musb_writew(regs
, MUSB_RXCSR
, csr
);
1103 musb_writew(regs
, MUSB_RXCSR
, csr
);
1106 /* NOTE: all the I/O code _should_ work fine without DMA, in case
1107 * for some reason you run out of channels here.
1109 if (is_dma_capable() && musb
->dma_controller
) {
1110 struct dma_controller
*c
= musb
->dma_controller
;
1112 musb_ep
->dma
= c
->channel_alloc(c
, hw_ep
,
1113 (desc
->bEndpointAddress
& USB_DIR_IN
));
1115 musb_ep
->dma
= NULL
;
1117 musb_ep
->desc
= desc
;
1119 musb_ep
->wedged
= 0;
1122 pr_debug("%s periph: enabled %s for %s %s, %smaxpacket %d\n",
1123 musb_driver_name
, musb_ep
->end_point
.name
,
1124 ({ char *s
; switch (musb_ep
->type
) {
1125 case USB_ENDPOINT_XFER_BULK
: s
= "bulk"; break;
1126 case USB_ENDPOINT_XFER_INT
: s
= "int"; break;
1127 default: s
= "iso"; break;
1129 musb_ep
->is_in
? "IN" : "OUT",
1130 musb_ep
->dma
? "dma, " : "",
1131 musb_ep
->packet_sz
);
1133 schedule_work(&musb
->irq_work
);
1136 spin_unlock_irqrestore(&musb
->lock
, flags
);
1141 * Disable an endpoint flushing all requests queued.
1143 static int musb_gadget_disable(struct usb_ep
*ep
)
1145 unsigned long flags
;
1148 struct musb_ep
*musb_ep
;
1152 musb_ep
= to_musb_ep(ep
);
1153 musb
= musb_ep
->musb
;
1154 epnum
= musb_ep
->current_epnum
;
1155 epio
= musb
->endpoints
[epnum
].regs
;
1157 spin_lock_irqsave(&musb
->lock
, flags
);
1158 musb_ep_select(musb
->mregs
, epnum
);
1160 /* zero the endpoint sizes */
1161 if (musb_ep
->is_in
) {
1162 musb
->intrtxe
&= ~(1 << epnum
);
1163 musb_writew(musb
->mregs
, MUSB_INTRTXE
, musb
->intrtxe
);
1164 musb_writew(epio
, MUSB_TXMAXP
, 0);
1166 musb
->intrrxe
&= ~(1 << epnum
);
1167 musb_writew(musb
->mregs
, MUSB_INTRRXE
, musb
->intrrxe
);
1168 musb_writew(epio
, MUSB_RXMAXP
, 0);
1171 musb_ep
->desc
= NULL
;
1172 musb_ep
->end_point
.desc
= NULL
;
1174 /* abort all pending DMA and requests */
1175 nuke(musb_ep
, -ESHUTDOWN
);
1177 schedule_work(&musb
->irq_work
);
1179 spin_unlock_irqrestore(&(musb
->lock
), flags
);
1181 dev_dbg(musb
->controller
, "%s\n", musb_ep
->end_point
.name
);
1187 * Allocate a request for an endpoint.
1188 * Reused by ep0 code.
1190 struct usb_request
*musb_alloc_request(struct usb_ep
*ep
, gfp_t gfp_flags
)
1192 struct musb_ep
*musb_ep
= to_musb_ep(ep
);
1193 struct musb
*musb
= musb_ep
->musb
;
1194 struct musb_request
*request
= NULL
;
1196 request
= kzalloc(sizeof *request
, gfp_flags
);
1198 dev_dbg(musb
->controller
, "not enough memory\n");
1202 request
->request
.dma
= DMA_ADDR_INVALID
;
1203 request
->epnum
= musb_ep
->current_epnum
;
1204 request
->ep
= musb_ep
;
1206 return &request
->request
;
1211 * Reused by ep0 code.
1213 void musb_free_request(struct usb_ep
*ep
, struct usb_request
*req
)
1215 kfree(to_musb_request(req
));
1218 static LIST_HEAD(buffers
);
1220 struct free_record
{
1221 struct list_head list
;
1228 * Context: controller locked, IRQs blocked.
1230 void musb_ep_restart(struct musb
*musb
, struct musb_request
*req
)
1232 dev_dbg(musb
->controller
, "<== %s request %p len %u on hw_ep%d\n",
1233 req
->tx
? "TX/IN" : "RX/OUT",
1234 &req
->request
, req
->request
.length
, req
->epnum
);
1236 musb_ep_select(musb
->mregs
, req
->epnum
);
1243 static int musb_gadget_queue(struct usb_ep
*ep
, struct usb_request
*req
,
1246 struct musb_ep
*musb_ep
;
1247 struct musb_request
*request
;
1250 unsigned long lockflags
;
1257 musb_ep
= to_musb_ep(ep
);
1258 musb
= musb_ep
->musb
;
1260 request
= to_musb_request(req
);
1261 request
->musb
= musb
;
1263 if (request
->ep
!= musb_ep
)
1266 dev_dbg(musb
->controller
, "<== to %s request=%p\n", ep
->name
, req
);
1268 /* request is mine now... */
1269 request
->request
.actual
= 0;
1270 request
->request
.status
= -EINPROGRESS
;
1271 request
->epnum
= musb_ep
->current_epnum
;
1272 request
->tx
= musb_ep
->is_in
;
1274 map_dma_buffer(request
, musb
, musb_ep
);
1276 spin_lock_irqsave(&musb
->lock
, lockflags
);
1278 /* don't queue if the ep is down */
1279 if (!musb_ep
->desc
) {
1280 dev_dbg(musb
->controller
, "req %p queued to %s while ep %s\n",
1281 req
, ep
->name
, "disabled");
1282 status
= -ESHUTDOWN
;
1283 unmap_dma_buffer(request
, musb
);
1287 /* add request to the list */
1288 list_add_tail(&request
->list
, &musb_ep
->req_list
);
1290 /* it this is the head of the queue, start i/o ... */
1291 if (!musb_ep
->busy
&& &request
->list
== musb_ep
->req_list
.next
)
1292 musb_ep_restart(musb
, request
);
1295 spin_unlock_irqrestore(&musb
->lock
, lockflags
);
1299 static int musb_gadget_dequeue(struct usb_ep
*ep
, struct usb_request
*request
)
1301 struct musb_ep
*musb_ep
= to_musb_ep(ep
);
1302 struct musb_request
*req
= to_musb_request(request
);
1303 struct musb_request
*r
;
1304 unsigned long flags
;
1306 struct musb
*musb
= musb_ep
->musb
;
1308 if (!ep
|| !request
|| to_musb_request(request
)->ep
!= musb_ep
)
1311 spin_lock_irqsave(&musb
->lock
, flags
);
1313 list_for_each_entry(r
, &musb_ep
->req_list
, list
) {
1318 dev_dbg(musb
->controller
, "request %p not queued to %s\n", request
, ep
->name
);
1323 /* if the hardware doesn't have the request, easy ... */
1324 if (musb_ep
->req_list
.next
!= &req
->list
|| musb_ep
->busy
)
1325 musb_g_giveback(musb_ep
, request
, -ECONNRESET
);
1327 /* ... else abort the dma transfer ... */
1328 else if (is_dma_capable() && musb_ep
->dma
) {
1329 struct dma_controller
*c
= musb
->dma_controller
;
1331 musb_ep_select(musb
->mregs
, musb_ep
->current_epnum
);
1332 if (c
->channel_abort
)
1333 status
= c
->channel_abort(musb_ep
->dma
);
1337 musb_g_giveback(musb_ep
, request
, -ECONNRESET
);
1339 /* NOTE: by sticking to easily tested hardware/driver states,
1340 * we leave counting of in-flight packets imprecise.
1342 musb_g_giveback(musb_ep
, request
, -ECONNRESET
);
1346 spin_unlock_irqrestore(&musb
->lock
, flags
);
1351 * Set or clear the halt bit of an endpoint. A halted enpoint won't tx/rx any
1352 * data but will queue requests.
1354 * exported to ep0 code
1356 static int musb_gadget_set_halt(struct usb_ep
*ep
, int value
)
1358 struct musb_ep
*musb_ep
= to_musb_ep(ep
);
1359 u8 epnum
= musb_ep
->current_epnum
;
1360 struct musb
*musb
= musb_ep
->musb
;
1361 void __iomem
*epio
= musb
->endpoints
[epnum
].regs
;
1362 void __iomem
*mbase
;
1363 unsigned long flags
;
1365 struct musb_request
*request
;
1370 mbase
= musb
->mregs
;
1372 spin_lock_irqsave(&musb
->lock
, flags
);
1374 if ((USB_ENDPOINT_XFER_ISOC
== musb_ep
->type
)) {
1379 musb_ep_select(mbase
, epnum
);
1381 request
= next_request(musb_ep
);
1384 dev_dbg(musb
->controller
, "request in progress, cannot halt %s\n",
1389 /* Cannot portably stall with non-empty FIFO */
1390 if (musb_ep
->is_in
) {
1391 csr
= musb_readw(epio
, MUSB_TXCSR
);
1392 if (csr
& MUSB_TXCSR_FIFONOTEMPTY
) {
1393 dev_dbg(musb
->controller
, "FIFO busy, cannot halt %s\n", ep
->name
);
1399 musb_ep
->wedged
= 0;
1401 /* set/clear the stall and toggle bits */
1402 dev_dbg(musb
->controller
, "%s: %s stall\n", ep
->name
, value
? "set" : "clear");
1403 if (musb_ep
->is_in
) {
1404 csr
= musb_readw(epio
, MUSB_TXCSR
);
1405 csr
|= MUSB_TXCSR_P_WZC_BITS
1406 | MUSB_TXCSR_CLRDATATOG
;
1408 csr
|= MUSB_TXCSR_P_SENDSTALL
;
1410 csr
&= ~(MUSB_TXCSR_P_SENDSTALL
1411 | MUSB_TXCSR_P_SENTSTALL
);
1412 csr
&= ~MUSB_TXCSR_TXPKTRDY
;
1413 musb_writew(epio
, MUSB_TXCSR
, csr
);
1415 csr
= musb_readw(epio
, MUSB_RXCSR
);
1416 csr
|= MUSB_RXCSR_P_WZC_BITS
1417 | MUSB_RXCSR_FLUSHFIFO
1418 | MUSB_RXCSR_CLRDATATOG
;
1420 csr
|= MUSB_RXCSR_P_SENDSTALL
;
1422 csr
&= ~(MUSB_RXCSR_P_SENDSTALL
1423 | MUSB_RXCSR_P_SENTSTALL
);
1424 musb_writew(epio
, MUSB_RXCSR
, csr
);
1427 /* maybe start the first request in the queue */
1428 if (!musb_ep
->busy
&& !value
&& request
) {
1429 dev_dbg(musb
->controller
, "restarting the request\n");
1430 musb_ep_restart(musb
, request
);
1434 spin_unlock_irqrestore(&musb
->lock
, flags
);
1439 * Sets the halt feature with the clear requests ignored
1441 static int musb_gadget_set_wedge(struct usb_ep
*ep
)
1443 struct musb_ep
*musb_ep
= to_musb_ep(ep
);
1448 musb_ep
->wedged
= 1;
1450 return usb_ep_set_halt(ep
);
1453 static int musb_gadget_fifo_status(struct usb_ep
*ep
)
1455 struct musb_ep
*musb_ep
= to_musb_ep(ep
);
1456 void __iomem
*epio
= musb_ep
->hw_ep
->regs
;
1457 int retval
= -EINVAL
;
1459 if (musb_ep
->desc
&& !musb_ep
->is_in
) {
1460 struct musb
*musb
= musb_ep
->musb
;
1461 int epnum
= musb_ep
->current_epnum
;
1462 void __iomem
*mbase
= musb
->mregs
;
1463 unsigned long flags
;
1465 spin_lock_irqsave(&musb
->lock
, flags
);
1467 musb_ep_select(mbase
, epnum
);
1468 /* FIXME return zero unless RXPKTRDY is set */
1469 retval
= musb_readw(epio
, MUSB_RXCOUNT
);
1471 spin_unlock_irqrestore(&musb
->lock
, flags
);
1476 static void musb_gadget_fifo_flush(struct usb_ep
*ep
)
1478 struct musb_ep
*musb_ep
= to_musb_ep(ep
);
1479 struct musb
*musb
= musb_ep
->musb
;
1480 u8 epnum
= musb_ep
->current_epnum
;
1481 void __iomem
*epio
= musb
->endpoints
[epnum
].regs
;
1482 void __iomem
*mbase
;
1483 unsigned long flags
;
1486 mbase
= musb
->mregs
;
1488 spin_lock_irqsave(&musb
->lock
, flags
);
1489 musb_ep_select(mbase
, (u8
) epnum
);
1491 /* disable interrupts */
1492 musb_writew(mbase
, MUSB_INTRTXE
, musb
->intrtxe
& ~(1 << epnum
));
1494 if (musb_ep
->is_in
) {
1495 csr
= musb_readw(epio
, MUSB_TXCSR
);
1496 if (csr
& MUSB_TXCSR_FIFONOTEMPTY
) {
1497 csr
|= MUSB_TXCSR_FLUSHFIFO
| MUSB_TXCSR_P_WZC_BITS
;
1499 * Setting both TXPKTRDY and FLUSHFIFO makes controller
1500 * to interrupt current FIFO loading, but not flushing
1501 * the already loaded ones.
1503 csr
&= ~MUSB_TXCSR_TXPKTRDY
;
1504 musb_writew(epio
, MUSB_TXCSR
, csr
);
1505 /* REVISIT may be inappropriate w/o FIFONOTEMPTY ... */
1506 musb_writew(epio
, MUSB_TXCSR
, csr
);
1509 csr
= musb_readw(epio
, MUSB_RXCSR
);
1510 csr
|= MUSB_RXCSR_FLUSHFIFO
| MUSB_RXCSR_P_WZC_BITS
;
1511 musb_writew(epio
, MUSB_RXCSR
, csr
);
1512 musb_writew(epio
, MUSB_RXCSR
, csr
);
1515 /* re-enable interrupt */
1516 musb_writew(mbase
, MUSB_INTRTXE
, musb
->intrtxe
);
1517 spin_unlock_irqrestore(&musb
->lock
, flags
);
1520 static const struct usb_ep_ops musb_ep_ops
= {
1521 .enable
= musb_gadget_enable
,
1522 .disable
= musb_gadget_disable
,
1523 .alloc_request
= musb_alloc_request
,
1524 .free_request
= musb_free_request
,
1525 .queue
= musb_gadget_queue
,
1526 .dequeue
= musb_gadget_dequeue
,
1527 .set_halt
= musb_gadget_set_halt
,
1528 .set_wedge
= musb_gadget_set_wedge
,
1529 .fifo_status
= musb_gadget_fifo_status
,
1530 .fifo_flush
= musb_gadget_fifo_flush
1533 /* ----------------------------------------------------------------------- */
1535 static int musb_gadget_get_frame(struct usb_gadget
*gadget
)
1537 struct musb
*musb
= gadget_to_musb(gadget
);
1539 return (int)musb_readw(musb
->mregs
, MUSB_FRAME
);
1542 static int musb_gadget_wakeup(struct usb_gadget
*gadget
)
1544 struct musb
*musb
= gadget_to_musb(gadget
);
1545 void __iomem
*mregs
= musb
->mregs
;
1546 unsigned long flags
;
1547 int status
= -EINVAL
;
1551 spin_lock_irqsave(&musb
->lock
, flags
);
1553 switch (musb
->xceiv
->otg
->state
) {
1554 case OTG_STATE_B_PERIPHERAL
:
1555 /* NOTE: OTG state machine doesn't include B_SUSPENDED;
1556 * that's part of the standard usb 1.1 state machine, and
1557 * doesn't affect OTG transitions.
1559 if (musb
->may_wakeup
&& musb
->is_suspended
)
1562 case OTG_STATE_B_IDLE
:
1563 /* Start SRP ... OTG not required. */
1564 devctl
= musb_readb(mregs
, MUSB_DEVCTL
);
1565 dev_dbg(musb
->controller
, "Sending SRP: devctl: %02x\n", devctl
);
1566 devctl
|= MUSB_DEVCTL_SESSION
;
1567 musb_writeb(mregs
, MUSB_DEVCTL
, devctl
);
1568 devctl
= musb_readb(mregs
, MUSB_DEVCTL
);
1570 while (!(devctl
& MUSB_DEVCTL_SESSION
)) {
1571 devctl
= musb_readb(mregs
, MUSB_DEVCTL
);
1576 while (devctl
& MUSB_DEVCTL_SESSION
) {
1577 devctl
= musb_readb(mregs
, MUSB_DEVCTL
);
1582 spin_unlock_irqrestore(&musb
->lock
, flags
);
1583 otg_start_srp(musb
->xceiv
->otg
);
1584 spin_lock_irqsave(&musb
->lock
, flags
);
1586 /* Block idling for at least 1s */
1587 musb_platform_try_idle(musb
,
1588 jiffies
+ msecs_to_jiffies(1 * HZ
));
1593 dev_dbg(musb
->controller
, "Unhandled wake: %s\n",
1594 usb_otg_state_string(musb
->xceiv
->otg
->state
));
1600 power
= musb_readb(mregs
, MUSB_POWER
);
1601 power
|= MUSB_POWER_RESUME
;
1602 musb_writeb(mregs
, MUSB_POWER
, power
);
1603 dev_dbg(musb
->controller
, "issue wakeup\n");
1605 /* FIXME do this next chunk in a timer callback, no udelay */
1608 power
= musb_readb(mregs
, MUSB_POWER
);
1609 power
&= ~MUSB_POWER_RESUME
;
1610 musb_writeb(mregs
, MUSB_POWER
, power
);
1612 spin_unlock_irqrestore(&musb
->lock
, flags
);
1617 musb_gadget_set_self_powered(struct usb_gadget
*gadget
, int is_selfpowered
)
1619 gadget
->is_selfpowered
= !!is_selfpowered
;
1623 static void musb_pullup(struct musb
*musb
, int is_on
)
1627 power
= musb_readb(musb
->mregs
, MUSB_POWER
);
1629 power
|= MUSB_POWER_SOFTCONN
;
1631 power
&= ~MUSB_POWER_SOFTCONN
;
1633 /* FIXME if on, HdrcStart; if off, HdrcStop */
1635 dev_dbg(musb
->controller
, "gadget D+ pullup %s\n",
1636 is_on
? "on" : "off");
1637 musb_writeb(musb
->mregs
, MUSB_POWER
, power
);
1641 static int musb_gadget_vbus_session(struct usb_gadget
*gadget
, int is_active
)
1643 dev_dbg(musb
->controller
, "<= %s =>\n", __func__
);
1646 * FIXME iff driver's softconnect flag is set (as it is during probe,
1647 * though that can clear it), just musb_pullup().
1654 static int musb_gadget_vbus_draw(struct usb_gadget
*gadget
, unsigned mA
)
1656 struct musb
*musb
= gadget_to_musb(gadget
);
1658 if (!musb
->xceiv
->set_power
)
1660 return usb_phy_set_power(musb
->xceiv
, mA
);
1663 static int musb_gadget_pullup(struct usb_gadget
*gadget
, int is_on
)
1665 struct musb
*musb
= gadget_to_musb(gadget
);
1666 unsigned long flags
;
1670 pm_runtime_get_sync(musb
->controller
);
1672 /* NOTE: this assumes we are sensing vbus; we'd rather
1673 * not pullup unless the B-session is active.
1675 spin_lock_irqsave(&musb
->lock
, flags
);
1676 if (is_on
!= musb
->softconnect
) {
1677 musb
->softconnect
= is_on
;
1678 musb_pullup(musb
, is_on
);
1680 spin_unlock_irqrestore(&musb
->lock
, flags
);
1682 pm_runtime_put(musb
->controller
);
1687 static int musb_gadget_start(struct usb_gadget
*g
,
1688 struct usb_gadget_driver
*driver
);
1689 static int musb_gadget_stop(struct usb_gadget
*g
);
1691 static const struct usb_gadget_ops musb_gadget_operations
= {
1692 .get_frame
= musb_gadget_get_frame
,
1693 .wakeup
= musb_gadget_wakeup
,
1694 .set_selfpowered
= musb_gadget_set_self_powered
,
1695 /* .vbus_session = musb_gadget_vbus_session, */
1696 .vbus_draw
= musb_gadget_vbus_draw
,
1697 .pullup
= musb_gadget_pullup
,
1698 .udc_start
= musb_gadget_start
,
1699 .udc_stop
= musb_gadget_stop
,
1702 /* ----------------------------------------------------------------------- */
1706 /* Only this registration code "knows" the rule (from USB standards)
1707 * about there being only one external upstream port. It assumes
1708 * all peripheral ports are external...
1712 init_peripheral_ep(struct musb
*musb
, struct musb_ep
*ep
, u8 epnum
, int is_in
)
1714 struct musb_hw_ep
*hw_ep
= musb
->endpoints
+ epnum
;
1716 memset(ep
, 0, sizeof *ep
);
1718 ep
->current_epnum
= epnum
;
1723 INIT_LIST_HEAD(&ep
->req_list
);
1725 sprintf(ep
->name
, "ep%d%s", epnum
,
1726 (!epnum
|| hw_ep
->is_shared_fifo
) ? "" : (
1727 is_in
? "in" : "out"));
1728 ep
->end_point
.name
= ep
->name
;
1729 INIT_LIST_HEAD(&ep
->end_point
.ep_list
);
1731 usb_ep_set_maxpacket_limit(&ep
->end_point
, 64);
1732 ep
->end_point
.ops
= &musb_g_ep0_ops
;
1733 musb
->g
.ep0
= &ep
->end_point
;
1736 usb_ep_set_maxpacket_limit(&ep
->end_point
, hw_ep
->max_packet_sz_tx
);
1738 usb_ep_set_maxpacket_limit(&ep
->end_point
, hw_ep
->max_packet_sz_rx
);
1739 ep
->end_point
.ops
= &musb_ep_ops
;
1740 list_add_tail(&ep
->end_point
.ep_list
, &musb
->g
.ep_list
);
1745 * Initialize the endpoints exposed to peripheral drivers, with backlinks
1746 * to the rest of the driver state.
1748 static inline void musb_g_init_endpoints(struct musb
*musb
)
1751 struct musb_hw_ep
*hw_ep
;
1754 /* initialize endpoint list just once */
1755 INIT_LIST_HEAD(&(musb
->g
.ep_list
));
1757 for (epnum
= 0, hw_ep
= musb
->endpoints
;
1758 epnum
< musb
->nr_endpoints
;
1760 if (hw_ep
->is_shared_fifo
/* || !epnum */) {
1761 init_peripheral_ep(musb
, &hw_ep
->ep_in
, epnum
, 0);
1764 if (hw_ep
->max_packet_sz_tx
) {
1765 init_peripheral_ep(musb
, &hw_ep
->ep_in
,
1769 if (hw_ep
->max_packet_sz_rx
) {
1770 init_peripheral_ep(musb
, &hw_ep
->ep_out
,
1778 /* called once during driver setup to initialize and link into
1779 * the driver model; memory is zeroed.
1781 int musb_gadget_setup(struct musb
*musb
)
1785 /* REVISIT minor race: if (erroneously) setting up two
1786 * musb peripherals at the same time, only the bus lock
1790 musb
->g
.ops
= &musb_gadget_operations
;
1791 musb
->g
.max_speed
= USB_SPEED_HIGH
;
1792 musb
->g
.speed
= USB_SPEED_UNKNOWN
;
1794 MUSB_DEV_MODE(musb
);
1795 musb
->xceiv
->otg
->default_a
= 0;
1796 musb
->xceiv
->otg
->state
= OTG_STATE_B_IDLE
;
1798 /* this "gadget" abstracts/virtualizes the controller */
1799 musb
->g
.name
= musb_driver_name
;
1800 #if IS_ENABLED(CONFIG_USB_MUSB_DUAL_ROLE)
1802 #elif IS_ENABLED(CONFIG_USB_MUSB_GADGET)
1806 musb_g_init_endpoints(musb
);
1808 musb
->is_active
= 0;
1809 musb_platform_try_idle(musb
, 0);
1811 status
= usb_add_gadget_udc(musb
->controller
, &musb
->g
);
1817 musb
->g
.dev
.parent
= NULL
;
1818 device_unregister(&musb
->g
.dev
);
1822 void musb_gadget_cleanup(struct musb
*musb
)
1824 if (musb
->port_mode
== MUSB_PORT_MODE_HOST
)
1826 usb_del_gadget_udc(&musb
->g
);
1830 * Register the gadget driver. Used by gadget drivers when
1831 * registering themselves with the controller.
1833 * -EINVAL something went wrong (not driver)
1834 * -EBUSY another gadget is already using the controller
1835 * -ENOMEM no memory to perform the operation
1837 * @param driver the gadget driver
1838 * @return <0 if error, 0 if everything is fine
1840 static int musb_gadget_start(struct usb_gadget
*g
,
1841 struct usb_gadget_driver
*driver
)
1843 struct musb
*musb
= gadget_to_musb(g
);
1844 struct usb_otg
*otg
= musb
->xceiv
->otg
;
1845 unsigned long flags
;
1848 if (driver
->max_speed
< USB_SPEED_HIGH
) {
1853 pm_runtime_get_sync(musb
->controller
);
1855 musb
->softconnect
= 0;
1856 musb
->gadget_driver
= driver
;
1858 spin_lock_irqsave(&musb
->lock
, flags
);
1859 musb
->is_active
= 1;
1861 otg_set_peripheral(otg
, &musb
->g
);
1862 musb
->xceiv
->otg
->state
= OTG_STATE_B_IDLE
;
1863 spin_unlock_irqrestore(&musb
->lock
, flags
);
1867 /* REVISIT: funcall to other code, which also
1868 * handles power budgeting ... this way also
1869 * ensures HdrcStart is indirectly called.
1871 if (musb
->xceiv
->last_event
== USB_EVENT_ID
)
1872 musb_platform_set_vbus(musb
, 1);
1874 if (musb
->xceiv
->last_event
== USB_EVENT_NONE
)
1875 pm_runtime_put(musb
->controller
);
1884 * Unregister the gadget driver. Used by gadget drivers when
1885 * unregistering themselves from the controller.
1887 * @param driver the gadget driver to unregister
1889 static int musb_gadget_stop(struct usb_gadget
*g
)
1891 struct musb
*musb
= gadget_to_musb(g
);
1892 unsigned long flags
;
1894 if (musb
->xceiv
->last_event
== USB_EVENT_NONE
)
1895 pm_runtime_get_sync(musb
->controller
);
1898 * REVISIT always use otg_set_peripheral() here too;
1899 * this needs to shut down the OTG engine.
1902 spin_lock_irqsave(&musb
->lock
, flags
);
1904 musb_hnp_stop(musb
);
1906 (void) musb_gadget_vbus_draw(&musb
->g
, 0);
1908 musb
->xceiv
->otg
->state
= OTG_STATE_UNDEFINED
;
1910 otg_set_peripheral(musb
->xceiv
->otg
, NULL
);
1912 musb
->is_active
= 0;
1913 musb
->gadget_driver
= NULL
;
1914 musb_platform_try_idle(musb
, 0);
1915 spin_unlock_irqrestore(&musb
->lock
, flags
);
1918 * FIXME we need to be able to register another
1919 * gadget driver here and have everything work;
1920 * that currently misbehaves.
1923 pm_runtime_put(musb
->controller
);
1928 /* ----------------------------------------------------------------------- */
1930 /* lifecycle operations called through plat_uds.c */
1932 void musb_g_resume(struct musb
*musb
)
1934 musb
->is_suspended
= 0;
1935 switch (musb
->xceiv
->otg
->state
) {
1936 case OTG_STATE_B_IDLE
:
1938 case OTG_STATE_B_WAIT_ACON
:
1939 case OTG_STATE_B_PERIPHERAL
:
1940 musb
->is_active
= 1;
1941 if (musb
->gadget_driver
&& musb
->gadget_driver
->resume
) {
1942 spin_unlock(&musb
->lock
);
1943 musb
->gadget_driver
->resume(&musb
->g
);
1944 spin_lock(&musb
->lock
);
1948 WARNING("unhandled RESUME transition (%s)\n",
1949 usb_otg_state_string(musb
->xceiv
->otg
->state
));
1953 /* called when SOF packets stop for 3+ msec */
1954 void musb_g_suspend(struct musb
*musb
)
1958 devctl
= musb_readb(musb
->mregs
, MUSB_DEVCTL
);
1959 dev_dbg(musb
->controller
, "devctl %02x\n", devctl
);
1961 switch (musb
->xceiv
->otg
->state
) {
1962 case OTG_STATE_B_IDLE
:
1963 if ((devctl
& MUSB_DEVCTL_VBUS
) == MUSB_DEVCTL_VBUS
)
1964 musb
->xceiv
->otg
->state
= OTG_STATE_B_PERIPHERAL
;
1966 case OTG_STATE_B_PERIPHERAL
:
1967 musb
->is_suspended
= 1;
1968 if (musb
->gadget_driver
&& musb
->gadget_driver
->suspend
) {
1969 spin_unlock(&musb
->lock
);
1970 musb
->gadget_driver
->suspend(&musb
->g
);
1971 spin_lock(&musb
->lock
);
1975 /* REVISIT if B_HOST, clear DEVCTL.HOSTREQ;
1976 * A_PERIPHERAL may need care too
1978 WARNING("unhandled SUSPEND transition (%s)\n",
1979 usb_otg_state_string(musb
->xceiv
->otg
->state
));
1983 /* Called during SRP */
1984 void musb_g_wakeup(struct musb
*musb
)
1986 musb_gadget_wakeup(&musb
->g
);
1989 /* called when VBUS drops below session threshold, and in other cases */
1990 void musb_g_disconnect(struct musb
*musb
)
1992 void __iomem
*mregs
= musb
->mregs
;
1993 u8 devctl
= musb_readb(mregs
, MUSB_DEVCTL
);
1995 dev_dbg(musb
->controller
, "devctl %02x\n", devctl
);
1998 musb_writeb(mregs
, MUSB_DEVCTL
, devctl
& MUSB_DEVCTL_SESSION
);
2000 /* don't draw vbus until new b-default session */
2001 (void) musb_gadget_vbus_draw(&musb
->g
, 0);
2003 musb
->g
.speed
= USB_SPEED_UNKNOWN
;
2004 if (musb
->gadget_driver
&& musb
->gadget_driver
->disconnect
) {
2005 spin_unlock(&musb
->lock
);
2006 musb
->gadget_driver
->disconnect(&musb
->g
);
2007 spin_lock(&musb
->lock
);
2010 switch (musb
->xceiv
->otg
->state
) {
2012 dev_dbg(musb
->controller
, "Unhandled disconnect %s, setting a_idle\n",
2013 usb_otg_state_string(musb
->xceiv
->otg
->state
));
2014 musb
->xceiv
->otg
->state
= OTG_STATE_A_IDLE
;
2015 MUSB_HST_MODE(musb
);
2017 case OTG_STATE_A_PERIPHERAL
:
2018 musb
->xceiv
->otg
->state
= OTG_STATE_A_WAIT_BCON
;
2019 MUSB_HST_MODE(musb
);
2021 case OTG_STATE_B_WAIT_ACON
:
2022 case OTG_STATE_B_HOST
:
2023 case OTG_STATE_B_PERIPHERAL
:
2024 case OTG_STATE_B_IDLE
:
2025 musb
->xceiv
->otg
->state
= OTG_STATE_B_IDLE
;
2027 case OTG_STATE_B_SRP_INIT
:
2031 musb
->is_active
= 0;
2034 void musb_g_reset(struct musb
*musb
)
2035 __releases(musb
->lock
)
2036 __acquires(musb
->lock
)
2038 void __iomem
*mbase
= musb
->mregs
;
2039 u8 devctl
= musb_readb(mbase
, MUSB_DEVCTL
);
2042 dev_dbg(musb
->controller
, "<== %s driver '%s'\n",
2043 (devctl
& MUSB_DEVCTL_BDEVICE
)
2044 ? "B-Device" : "A-Device",
2046 ? musb
->gadget_driver
->driver
.name
2050 /* report reset, if we didn't already (flushing EP state) */
2051 if (musb
->gadget_driver
&& musb
->g
.speed
!= USB_SPEED_UNKNOWN
) {
2052 spin_unlock(&musb
->lock
);
2053 usb_gadget_udc_reset(&musb
->g
, musb
->gadget_driver
);
2054 spin_lock(&musb
->lock
);
2058 else if (devctl
& MUSB_DEVCTL_HR
)
2059 musb_writeb(mbase
, MUSB_DEVCTL
, MUSB_DEVCTL_SESSION
);
2062 /* what speed did we negotiate? */
2063 power
= musb_readb(mbase
, MUSB_POWER
);
2064 musb
->g
.speed
= (power
& MUSB_POWER_HSMODE
)
2065 ? USB_SPEED_HIGH
: USB_SPEED_FULL
;
2067 /* start in USB_STATE_DEFAULT */
2068 musb
->is_active
= 1;
2069 musb
->is_suspended
= 0;
2070 MUSB_DEV_MODE(musb
);
2072 musb
->ep0_state
= MUSB_EP0_STAGE_SETUP
;
2074 musb
->may_wakeup
= 0;
2075 musb
->g
.b_hnp_enable
= 0;
2076 musb
->g
.a_alt_hnp_support
= 0;
2077 musb
->g
.a_hnp_support
= 0;
2079 /* Normal reset, as B-Device;
2080 * or else after HNP, as A-Device
2082 if (!musb
->g
.is_otg
) {
2083 /* USB device controllers that are not OTG compatible
2084 * may not have DEVCTL register in silicon.
2085 * In that case, do not rely on devctl for setting
2088 musb
->xceiv
->otg
->state
= OTG_STATE_B_PERIPHERAL
;
2089 musb
->g
.is_a_peripheral
= 0;
2090 } else if (devctl
& MUSB_DEVCTL_BDEVICE
) {
2091 musb
->xceiv
->otg
->state
= OTG_STATE_B_PERIPHERAL
;
2092 musb
->g
.is_a_peripheral
= 0;
2094 musb
->xceiv
->otg
->state
= OTG_STATE_A_PERIPHERAL
;
2095 musb
->g
.is_a_peripheral
= 1;
2098 /* start with default limits on VBUS power draw */
2099 (void) musb_gadget_vbus_draw(&musb
->g
, 8);