2 * gadget.c - DesignWare USB3 DRD Controller Gadget Framework Link
4 * Copyright (C) 2010-2011 Texas Instruments Incorporated - http://www.ti.com
7 * Authors: Felipe Balbi <balbi@ti.com>,
8 * Sebastian Andrzej Siewior <bigeasy@linutronix.de>
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions, and the following disclaimer,
15 * without modification.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. The names of the above-listed copyright holders may not be used
20 * to endorse or promote products derived from this software without
21 * specific prior written permission.
23 * ALTERNATIVELY, this software may be distributed under the terms of the
24 * GNU General Public License ("GPL") version 2, as published by the Free
25 * Software Foundation.
27 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
28 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
29 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
31 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
32 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
33 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
34 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
35 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
36 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
37 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40 #include <linux/kernel.h>
41 #include <linux/delay.h>
42 #include <linux/slab.h>
43 #include <linux/spinlock.h>
44 #include <linux/platform_device.h>
45 #include <linux/pm_runtime.h>
46 #include <linux/interrupt.h>
48 #include <linux/list.h>
49 #include <linux/dma-mapping.h>
51 #include <linux/usb/ch9.h>
52 #include <linux/usb/gadget.h>
58 #define DMA_ADDR_INVALID (~(dma_addr_t)0)
60 void dwc3_map_buffer_to_dma(struct dwc3_request
*req
)
62 struct dwc3
*dwc
= req
->dep
->dwc
;
64 if (req
->request
.dma
== DMA_ADDR_INVALID
) {
65 req
->request
.dma
= dma_map_single(dwc
->dev
, req
->request
.buf
,
66 req
->request
.length
, req
->direction
67 ? DMA_TO_DEVICE
: DMA_FROM_DEVICE
);
70 dma_sync_single_for_device(dwc
->dev
, req
->request
.dma
,
71 req
->request
.length
, req
->direction
72 ? DMA_TO_DEVICE
: DMA_FROM_DEVICE
);
77 void dwc3_unmap_buffer_from_dma(struct dwc3_request
*req
)
79 struct dwc3
*dwc
= req
->dep
->dwc
;
82 dma_unmap_single(dwc
->dev
, req
->request
.dma
,
83 req
->request
.length
, req
->direction
84 ? DMA_TO_DEVICE
: DMA_FROM_DEVICE
);
87 dma_sync_single_for_cpu(dwc
->dev
, req
->request
.dma
,
88 req
->request
.length
, req
->direction
89 ? DMA_TO_DEVICE
: DMA_FROM_DEVICE
);
93 void dwc3_gadget_giveback(struct dwc3_ep
*dep
, struct dwc3_request
*req
,
96 struct dwc3
*dwc
= dep
->dwc
;
101 * Skip LINK TRB. We can't use req->trb and check for
102 * DWC3_TRBCTL_LINK_TRB because it points the TRB we just
103 * completed (not the LINK TRB).
105 if (((dep
->busy_slot
& DWC3_TRB_MASK
) == DWC3_TRB_NUM
- 1) &&
106 usb_endpoint_xfer_isoc(dep
->desc
))
109 list_del(&req
->list
);
111 if (req
->request
.status
== -EINPROGRESS
)
112 req
->request
.status
= status
;
114 dwc3_unmap_buffer_from_dma(req
);
116 dev_dbg(dwc
->dev
, "request %p from %s completed %d/%d ===> %d\n",
117 req
, dep
->name
, req
->request
.actual
,
118 req
->request
.length
, status
);
120 spin_unlock(&dwc
->lock
);
121 req
->request
.complete(&req
->dep
->endpoint
, &req
->request
);
122 spin_lock(&dwc
->lock
);
125 static const char *dwc3_gadget_ep_cmd_string(u8 cmd
)
128 case DWC3_DEPCMD_DEPSTARTCFG
:
129 return "Start New Configuration";
130 case DWC3_DEPCMD_ENDTRANSFER
:
131 return "End Transfer";
132 case DWC3_DEPCMD_UPDATETRANSFER
:
133 return "Update Transfer";
134 case DWC3_DEPCMD_STARTTRANSFER
:
135 return "Start Transfer";
136 case DWC3_DEPCMD_CLEARSTALL
:
137 return "Clear Stall";
138 case DWC3_DEPCMD_SETSTALL
:
140 case DWC3_DEPCMD_GETSEQNUMBER
:
141 return "Get Data Sequence Number";
142 case DWC3_DEPCMD_SETTRANSFRESOURCE
:
143 return "Set Endpoint Transfer Resource";
144 case DWC3_DEPCMD_SETEPCONFIG
:
145 return "Set Endpoint Configuration";
147 return "UNKNOWN command";
151 int dwc3_send_gadget_ep_cmd(struct dwc3
*dwc
, unsigned ep
,
152 unsigned cmd
, struct dwc3_gadget_ep_cmd_params
*params
)
154 struct dwc3_ep
*dep
= dwc
->eps
[ep
];
155 unsigned long timeout
= 500;
158 dev_vdbg(dwc
->dev
, "%s: cmd '%s' params %08x %08x %08x\n",
160 dwc3_gadget_ep_cmd_string(cmd
), params
->param0
.raw
,
161 params
->param1
.raw
, params
->param2
.raw
);
163 dwc3_writel(dwc
->regs
, DWC3_DEPCMDPAR0(ep
), params
->param0
.raw
);
164 dwc3_writel(dwc
->regs
, DWC3_DEPCMDPAR1(ep
), params
->param1
.raw
);
165 dwc3_writel(dwc
->regs
, DWC3_DEPCMDPAR2(ep
), params
->param2
.raw
);
167 dwc3_writel(dwc
->regs
, DWC3_DEPCMD(ep
), cmd
| DWC3_DEPCMD_CMDACT
);
169 reg
= dwc3_readl(dwc
->regs
, DWC3_DEPCMD(ep
));
170 if (!(reg
& DWC3_DEPCMD_CMDACT
)) {
171 dev_vdbg(dwc
->dev
, "CMD Compl Status %d DEPCMD %04x\n",
172 ((reg
& 0xf000) >> 12), reg
);
177 * XXX Figure out a sane timeout here. 500ms is way too much.
178 * We can't sleep here, because it is also called from
189 static dma_addr_t
dwc3_trb_dma_offset(struct dwc3_ep
*dep
,
190 struct dwc3_trb_hw
*trb
)
192 u32 offset
= trb
- dep
->trb_pool
;
194 return dep
->trb_pool_dma
+ offset
;
197 static int dwc3_alloc_trb_pool(struct dwc3_ep
*dep
)
199 struct dwc3
*dwc
= dep
->dwc
;
204 if (dep
->number
== 0 || dep
->number
== 1)
207 dep
->trb_pool
= dma_alloc_coherent(dwc
->dev
,
208 sizeof(struct dwc3_trb
) * DWC3_TRB_NUM
,
209 &dep
->trb_pool_dma
, GFP_KERNEL
);
210 if (!dep
->trb_pool
) {
211 dev_err(dep
->dwc
->dev
, "failed to allocate trb pool for %s\n",
219 static void dwc3_free_trb_pool(struct dwc3_ep
*dep
)
221 struct dwc3
*dwc
= dep
->dwc
;
223 dma_free_coherent(dwc
->dev
, sizeof(struct dwc3_trb
) * DWC3_TRB_NUM
,
224 dep
->trb_pool
, dep
->trb_pool_dma
);
226 dep
->trb_pool
= NULL
;
227 dep
->trb_pool_dma
= 0;
230 static int dwc3_gadget_start_config(struct dwc3
*dwc
, struct dwc3_ep
*dep
)
232 struct dwc3_gadget_ep_cmd_params params
;
235 memset(¶ms
, 0x00, sizeof(params
));
237 if (dep
->number
!= 1) {
238 cmd
= DWC3_DEPCMD_DEPSTARTCFG
;
239 /* XferRscIdx == 0 for ep0 and 2 for the remaining */
241 cmd
|= DWC3_DEPCMD_PARAM(2);
243 return dwc3_send_gadget_ep_cmd(dwc
, 0, cmd
, ¶ms
);
249 static int dwc3_gadget_set_ep_config(struct dwc3
*dwc
, struct dwc3_ep
*dep
,
250 const struct usb_endpoint_descriptor
*desc
)
252 struct dwc3_gadget_ep_cmd_params params
;
254 memset(¶ms
, 0x00, sizeof(params
));
256 params
.param0
.depcfg
.ep_type
= usb_endpoint_type(desc
);
257 params
.param0
.depcfg
.max_packet_size
= usb_endpoint_maxp(desc
);
259 params
.param1
.depcfg
.xfer_complete_enable
= true;
260 params
.param1
.depcfg
.xfer_not_ready_enable
= true;
262 if (usb_endpoint_xfer_isoc(desc
))
263 params
.param1
.depcfg
.xfer_in_progress_enable
= true;
266 * We are doing 1:1 mapping for endpoints, meaning
267 * Physical Endpoints 2 maps to Logical Endpoint 2 and
268 * so on. We consider the direction bit as part of the physical
269 * endpoint number. So USB endpoint 0x81 is 0x03.
271 params
.param1
.depcfg
.ep_number
= dep
->number
;
274 * We must use the lower 16 TX FIFOs even though
278 params
.param0
.depcfg
.fifo_number
= dep
->number
>> 1;
280 if (desc
->bInterval
) {
281 params
.param1
.depcfg
.binterval_m1
= desc
->bInterval
- 1;
282 dep
->interval
= 1 << (desc
->bInterval
- 1);
285 return dwc3_send_gadget_ep_cmd(dwc
, dep
->number
,
286 DWC3_DEPCMD_SETEPCONFIG
, ¶ms
);
289 static int dwc3_gadget_set_xfer_resource(struct dwc3
*dwc
, struct dwc3_ep
*dep
)
291 struct dwc3_gadget_ep_cmd_params params
;
293 memset(¶ms
, 0x00, sizeof(params
));
295 params
.param0
.depxfercfg
.number_xfer_resources
= 1;
297 return dwc3_send_gadget_ep_cmd(dwc
, dep
->number
,
298 DWC3_DEPCMD_SETTRANSFRESOURCE
, ¶ms
);
302 * __dwc3_gadget_ep_enable - Initializes a HW endpoint
303 * @dep: endpoint to be initialized
304 * @desc: USB Endpoint Descriptor
306 * Caller should take care of locking
308 static int __dwc3_gadget_ep_enable(struct dwc3_ep
*dep
,
309 const struct usb_endpoint_descriptor
*desc
)
311 struct dwc3
*dwc
= dep
->dwc
;
315 if (!(dep
->flags
& DWC3_EP_ENABLED
)) {
316 ret
= dwc3_gadget_start_config(dwc
, dep
);
321 ret
= dwc3_gadget_set_ep_config(dwc
, dep
, desc
);
325 if (!(dep
->flags
& DWC3_EP_ENABLED
)) {
326 struct dwc3_trb_hw
*trb_st_hw
;
327 struct dwc3_trb_hw
*trb_link_hw
;
328 struct dwc3_trb trb_link
;
330 ret
= dwc3_gadget_set_xfer_resource(dwc
, dep
);
335 dep
->type
= usb_endpoint_type(desc
);
336 dep
->flags
|= DWC3_EP_ENABLED
;
338 reg
= dwc3_readl(dwc
->regs
, DWC3_DALEPENA
);
339 reg
|= DWC3_DALEPENA_EP(dep
->number
);
340 dwc3_writel(dwc
->regs
, DWC3_DALEPENA
, reg
);
342 if (!usb_endpoint_xfer_isoc(desc
))
345 memset(&trb_link
, 0, sizeof(trb_link
));
347 /* Link TRB for ISOC. The HWO but is never reset */
348 trb_st_hw
= &dep
->trb_pool
[0];
350 trb_link
.bplh
= dwc3_trb_dma_offset(dep
, trb_st_hw
);
351 trb_link
.trbctl
= DWC3_TRBCTL_LINK_TRB
;
354 trb_link_hw
= &dep
->trb_pool
[DWC3_TRB_NUM
- 1];
355 dwc3_trb_to_hw(&trb_link
, trb_link_hw
);
361 static void dwc3_gadget_nuke_reqs(struct dwc3_ep
*dep
, const int status
)
363 struct dwc3_request
*req
;
365 while (!list_empty(&dep
->request_list
)) {
366 req
= next_request(&dep
->request_list
);
368 dwc3_gadget_giveback(dep
, req
, status
);
370 /* nuke queued TRBs as well on command complete */
371 dep
->flags
|= DWC3_EP_WILL_SHUTDOWN
;
375 * __dwc3_gadget_ep_disable - Disables a HW endpoint
376 * @dep: the endpoint to disable
378 * Caller should take care of locking
380 static void dwc3_stop_active_transfer(struct dwc3
*dwc
, u32 epnum
);
381 static int __dwc3_gadget_ep_disable(struct dwc3_ep
*dep
)
383 struct dwc3
*dwc
= dep
->dwc
;
386 dep
->flags
&= ~DWC3_EP_ENABLED
;
387 dwc3_stop_active_transfer(dwc
, dep
->number
);
388 dwc3_gadget_nuke_reqs(dep
, -ESHUTDOWN
);
390 reg
= dwc3_readl(dwc
->regs
, DWC3_DALEPENA
);
391 reg
&= ~DWC3_DALEPENA_EP(dep
->number
);
392 dwc3_writel(dwc
->regs
, DWC3_DALEPENA
, reg
);
400 /* -------------------------------------------------------------------------- */
402 static int dwc3_gadget_ep0_enable(struct usb_ep
*ep
,
403 const struct usb_endpoint_descriptor
*desc
)
408 static int dwc3_gadget_ep0_disable(struct usb_ep
*ep
)
413 /* -------------------------------------------------------------------------- */
415 static int dwc3_gadget_ep_enable(struct usb_ep
*ep
,
416 const struct usb_endpoint_descriptor
*desc
)
423 if (!ep
|| !desc
|| desc
->bDescriptorType
!= USB_DT_ENDPOINT
) {
424 pr_debug("dwc3: invalid parameters\n");
428 if (!desc
->wMaxPacketSize
) {
429 pr_debug("dwc3: missing wMaxPacketSize\n");
433 dep
= to_dwc3_ep(ep
);
436 switch (usb_endpoint_type(desc
)) {
437 case USB_ENDPOINT_XFER_CONTROL
:
438 strncat(dep
->name
, "-control", sizeof(dep
->name
));
440 case USB_ENDPOINT_XFER_ISOC
:
441 strncat(dep
->name
, "-isoc", sizeof(dep
->name
));
443 case USB_ENDPOINT_XFER_BULK
:
444 strncat(dep
->name
, "-bulk", sizeof(dep
->name
));
446 case USB_ENDPOINT_XFER_INT
:
447 strncat(dep
->name
, "-int", sizeof(dep
->name
));
450 dev_err(dwc
->dev
, "invalid endpoint transfer type\n");
453 if (dep
->flags
& DWC3_EP_ENABLED
) {
454 dev_WARN_ONCE(dwc
->dev
, true, "%s is already enabled\n",
459 dev_vdbg(dwc
->dev
, "Enabling %s\n", dep
->name
);
461 spin_lock_irqsave(&dwc
->lock
, flags
);
462 ret
= __dwc3_gadget_ep_enable(dep
, desc
);
463 spin_unlock_irqrestore(&dwc
->lock
, flags
);
468 static int dwc3_gadget_ep_disable(struct usb_ep
*ep
)
476 pr_debug("dwc3: invalid parameters\n");
480 dep
= to_dwc3_ep(ep
);
483 if (!(dep
->flags
& DWC3_EP_ENABLED
)) {
484 dev_WARN_ONCE(dwc
->dev
, true, "%s is already disabled\n",
489 snprintf(dep
->name
, sizeof(dep
->name
), "ep%d%s",
491 (dep
->number
& 1) ? "in" : "out");
493 spin_lock_irqsave(&dwc
->lock
, flags
);
494 ret
= __dwc3_gadget_ep_disable(dep
);
495 spin_unlock_irqrestore(&dwc
->lock
, flags
);
500 static struct usb_request
*dwc3_gadget_ep_alloc_request(struct usb_ep
*ep
,
503 struct dwc3_request
*req
;
504 struct dwc3_ep
*dep
= to_dwc3_ep(ep
);
505 struct dwc3
*dwc
= dep
->dwc
;
507 req
= kzalloc(sizeof(*req
), gfp_flags
);
509 dev_err(dwc
->dev
, "not enough memory\n");
513 req
->epnum
= dep
->number
;
515 req
->request
.dma
= DMA_ADDR_INVALID
;
517 return &req
->request
;
520 static void dwc3_gadget_ep_free_request(struct usb_ep
*ep
,
521 struct usb_request
*request
)
523 struct dwc3_request
*req
= to_dwc3_request(request
);
529 * dwc3_prepare_trbs - setup TRBs from requests
530 * @dep: endpoint for which requests are being prepared
531 * @starting: true if the endpoint is idle and no requests are queued.
533 * The functions goes through the requests list and setups TRBs for the
534 * transfers. The functions returns once there are not more TRBs available or
535 * it run out of requests.
537 static struct dwc3_request
*dwc3_prepare_trbs(struct dwc3_ep
*dep
,
540 struct dwc3_request
*req
, *n
, *ret
= NULL
;
541 struct dwc3_trb_hw
*trb_hw
;
545 BUILD_BUG_ON_NOT_POWER_OF_2(DWC3_TRB_NUM
);
547 /* the first request must not be queued */
548 trbs_left
= (dep
->busy_slot
- dep
->free_slot
) & DWC3_TRB_MASK
;
550 * if busy & slot are equal than it is either full or empty. If we are
551 * starting to proceed requests then we are empty. Otherwise we ar
552 * full and don't do anything
557 trbs_left
= DWC3_TRB_NUM
;
559 * In case we start from scratch, we queue the ISOC requests
560 * starting from slot 1. This is done because we use ring
561 * buffer and have no LST bit to stop us. Instead, we place
562 * IOC bit TRB_NUM/4. We try to avoid to having an interrupt
563 * after the first request so we start at slot 1 and have
564 * 7 requests proceed before we hit the first IOC.
565 * Other transfer types don't use the ring buffer and are
566 * processed from the first TRB until the last one. Since we
567 * don't wrap around we have to start at the beginning.
569 if (usb_endpoint_xfer_isoc(dep
->desc
)) {
578 /* The last TRB is a link TRB, not used for xfer */
579 if ((trbs_left
<= 1) && usb_endpoint_xfer_isoc(dep
->desc
))
582 list_for_each_entry_safe(req
, n
, &dep
->request_list
, list
) {
583 unsigned int last_one
= 0;
584 unsigned int cur_slot
;
586 trb_hw
= &dep
->trb_pool
[dep
->free_slot
& DWC3_TRB_MASK
];
587 cur_slot
= dep
->free_slot
;
590 /* Skip the LINK-TRB on ISOC */
591 if (((cur_slot
& DWC3_TRB_MASK
) == DWC3_TRB_NUM
- 1) &&
592 usb_endpoint_xfer_isoc(dep
->desc
))
595 dwc3_gadget_move_request_queued(req
);
596 memset(&trb
, 0, sizeof(trb
));
599 /* Is our TRB pool empty? */
602 /* Is this the last request? */
603 if (list_empty(&dep
->request_list
))
607 * FIXME we shouldn't need to set LST bit always but we are
608 * facing some weird problem with the Hardware where it doesn't
609 * complete even though it has been previously started.
611 * While we're debugging the problem, as a workaround to
612 * multiple TRBs handling, use only one TRB at a time.
620 trb
.bplh
= req
->request
.dma
;
622 if (usb_endpoint_xfer_isoc(dep
->desc
)) {
629 switch (usb_endpoint_type(dep
->desc
)) {
630 case USB_ENDPOINT_XFER_CONTROL
:
631 trb
.trbctl
= DWC3_TRBCTL_CONTROL_SETUP
;
634 case USB_ENDPOINT_XFER_ISOC
:
635 trb
.trbctl
= DWC3_TRBCTL_ISOCHRONOUS
;
637 /* IOC every DWC3_TRB_NUM / 4 so we can refill */
638 if (!(cur_slot
% (DWC3_TRB_NUM
/ 4)))
642 case USB_ENDPOINT_XFER_BULK
:
643 case USB_ENDPOINT_XFER_INT
:
644 trb
.trbctl
= DWC3_TRBCTL_NORMAL
;
648 * This is only possible with faulty memory because we
649 * checked it already :)
654 trb
.length
= req
->request
.length
;
657 dwc3_trb_to_hw(&trb
, trb_hw
);
658 req
->trb_dma
= dwc3_trb_dma_offset(dep
, trb_hw
);
667 static int __dwc3_gadget_kick_transfer(struct dwc3_ep
*dep
, u16 cmd_param
,
670 struct dwc3_gadget_ep_cmd_params params
;
671 struct dwc3_request
*req
;
672 struct dwc3
*dwc
= dep
->dwc
;
676 if (start_new
&& (dep
->flags
& DWC3_EP_BUSY
)) {
677 dev_vdbg(dwc
->dev
, "%s: endpoint busy\n", dep
->name
);
680 dep
->flags
&= ~DWC3_EP_PENDING_REQUEST
;
683 * If we are getting here after a short-out-packet we don't enqueue any
684 * new requests as we try to set the IOC bit only on the last request.
687 if (list_empty(&dep
->req_queued
))
688 dwc3_prepare_trbs(dep
, start_new
);
690 /* req points to the first request which will be sent */
691 req
= next_request(&dep
->req_queued
);
694 * req points to the first request where HWO changed
697 req
= dwc3_prepare_trbs(dep
, start_new
);
700 dep
->flags
|= DWC3_EP_PENDING_REQUEST
;
704 memset(¶ms
, 0, sizeof(params
));
705 params
.param0
.depstrtxfer
.transfer_desc_addr_high
=
706 upper_32_bits(req
->trb_dma
);
707 params
.param1
.depstrtxfer
.transfer_desc_addr_low
=
708 lower_32_bits(req
->trb_dma
);
711 cmd
= DWC3_DEPCMD_STARTTRANSFER
;
713 cmd
= DWC3_DEPCMD_UPDATETRANSFER
;
715 cmd
|= DWC3_DEPCMD_PARAM(cmd_param
);
716 ret
= dwc3_send_gadget_ep_cmd(dwc
, dep
->number
, cmd
, ¶ms
);
718 dev_dbg(dwc
->dev
, "failed to send STARTTRANSFER command\n");
721 * FIXME we need to iterate over the list of requests
722 * here and stop, unmap, free and del each of the linked
723 * requests instead of we do now.
725 dwc3_unmap_buffer_from_dma(req
);
726 list_del(&req
->list
);
730 dep
->flags
|= DWC3_EP_BUSY
;
731 dep
->res_trans_idx
= dwc3_gadget_ep_get_transfer_index(dwc
,
733 if (!dep
->res_trans_idx
)
734 printk_once(KERN_ERR
"%s() res_trans_idx is invalid\n", __func__
);
738 static int __dwc3_gadget_ep_queue(struct dwc3_ep
*dep
, struct dwc3_request
*req
)
740 req
->request
.actual
= 0;
741 req
->request
.status
= -EINPROGRESS
;
742 req
->direction
= dep
->direction
;
743 req
->epnum
= dep
->number
;
746 * We only add to our list of requests now and
747 * start consuming the list once we get XferNotReady
750 * That way, we avoid doing anything that we don't need
751 * to do now and defer it until the point we receive a
752 * particular token from the Host side.
754 * This will also avoid Host cancelling URBs due to too
757 dwc3_map_buffer_to_dma(req
);
758 list_add_tail(&req
->list
, &dep
->request_list
);
761 * There is one special case: XferNotReady with
762 * empty list of requests. We need to kick the
763 * transfer here in that situation, otherwise
764 * we will be NAKing forever.
766 * If we get XferNotReady before gadget driver
767 * has a chance to queue a request, we will ACK
768 * the IRQ but won't be able to receive the data
769 * until the next request is queued. The following
770 * code is handling exactly that.
772 if (dep
->flags
& DWC3_EP_PENDING_REQUEST
) {
777 if (usb_endpoint_xfer_isoc(dep
->endpoint
.desc
) &&
778 dep
->flags
& DWC3_EP_BUSY
)
781 ret
= __dwc3_gadget_kick_transfer(dep
, 0, start_trans
);
782 if (ret
&& ret
!= -EBUSY
) {
783 struct dwc3
*dwc
= dep
->dwc
;
785 dev_dbg(dwc
->dev
, "%s: failed to kick transfers\n",
793 static int dwc3_gadget_ep_queue(struct usb_ep
*ep
, struct usb_request
*request
,
796 struct dwc3_request
*req
= to_dwc3_request(request
);
797 struct dwc3_ep
*dep
= to_dwc3_ep(ep
);
798 struct dwc3
*dwc
= dep
->dwc
;
805 dev_dbg(dwc
->dev
, "trying to queue request %p to disabled %s\n",
810 dev_vdbg(dwc
->dev
, "queing request %p to %s length %d\n",
811 request
, ep
->name
, request
->length
);
813 spin_lock_irqsave(&dwc
->lock
, flags
);
814 ret
= __dwc3_gadget_ep_queue(dep
, req
);
815 spin_unlock_irqrestore(&dwc
->lock
, flags
);
820 static int dwc3_gadget_ep_dequeue(struct usb_ep
*ep
,
821 struct usb_request
*request
)
823 struct dwc3_request
*req
= to_dwc3_request(request
);
824 struct dwc3_request
*r
= NULL
;
826 struct dwc3_ep
*dep
= to_dwc3_ep(ep
);
827 struct dwc3
*dwc
= dep
->dwc
;
832 spin_lock_irqsave(&dwc
->lock
, flags
);
834 list_for_each_entry(r
, &dep
->request_list
, list
) {
840 list_for_each_entry(r
, &dep
->req_queued
, list
) {
845 /* wait until it is processed */
846 dwc3_stop_active_transfer(dwc
, dep
->number
);
849 dev_err(dwc
->dev
, "request %p was not queued to %s\n",
855 /* giveback the request */
856 dwc3_gadget_giveback(dep
, req
, -ECONNRESET
);
859 spin_unlock_irqrestore(&dwc
->lock
, flags
);
864 int __dwc3_gadget_ep_set_halt(struct dwc3_ep
*dep
, int value
)
866 struct dwc3_gadget_ep_cmd_params params
;
867 struct dwc3
*dwc
= dep
->dwc
;
870 memset(¶ms
, 0x00, sizeof(params
));
873 if (dep
->number
== 0 || dep
->number
== 1)
874 dwc
->ep0state
= EP0_STALL
;
876 ret
= dwc3_send_gadget_ep_cmd(dwc
, dep
->number
,
877 DWC3_DEPCMD_SETSTALL
, ¶ms
);
879 dev_err(dwc
->dev
, "failed to %s STALL on %s\n",
880 value
? "set" : "clear",
883 dep
->flags
|= DWC3_EP_STALL
;
885 ret
= dwc3_send_gadget_ep_cmd(dwc
, dep
->number
,
886 DWC3_DEPCMD_CLEARSTALL
, ¶ms
);
888 dev_err(dwc
->dev
, "failed to %s STALL on %s\n",
889 value
? "set" : "clear",
892 dep
->flags
&= ~DWC3_EP_STALL
;
897 static int dwc3_gadget_ep_set_halt(struct usb_ep
*ep
, int value
)
899 struct dwc3_ep
*dep
= to_dwc3_ep(ep
);
900 struct dwc3
*dwc
= dep
->dwc
;
906 spin_lock_irqsave(&dwc
->lock
, flags
);
908 if (usb_endpoint_xfer_isoc(dep
->desc
)) {
909 dev_err(dwc
->dev
, "%s is of Isochronous type\n", dep
->name
);
914 ret
= __dwc3_gadget_ep_set_halt(dep
, value
);
916 spin_unlock_irqrestore(&dwc
->lock
, flags
);
921 static int dwc3_gadget_ep_set_wedge(struct usb_ep
*ep
)
923 struct dwc3_ep
*dep
= to_dwc3_ep(ep
);
925 dep
->flags
|= DWC3_EP_WEDGE
;
927 return usb_ep_set_halt(ep
);
930 /* -------------------------------------------------------------------------- */
932 static struct usb_endpoint_descriptor dwc3_gadget_ep0_desc
= {
933 .bLength
= USB_DT_ENDPOINT_SIZE
,
934 .bDescriptorType
= USB_DT_ENDPOINT
,
935 .bmAttributes
= USB_ENDPOINT_XFER_CONTROL
,
938 static const struct usb_ep_ops dwc3_gadget_ep0_ops
= {
939 .enable
= dwc3_gadget_ep0_enable
,
940 .disable
= dwc3_gadget_ep0_disable
,
941 .alloc_request
= dwc3_gadget_ep_alloc_request
,
942 .free_request
= dwc3_gadget_ep_free_request
,
943 .queue
= dwc3_gadget_ep0_queue
,
944 .dequeue
= dwc3_gadget_ep_dequeue
,
945 .set_halt
= dwc3_gadget_ep_set_halt
,
946 .set_wedge
= dwc3_gadget_ep_set_wedge
,
949 static const struct usb_ep_ops dwc3_gadget_ep_ops
= {
950 .enable
= dwc3_gadget_ep_enable
,
951 .disable
= dwc3_gadget_ep_disable
,
952 .alloc_request
= dwc3_gadget_ep_alloc_request
,
953 .free_request
= dwc3_gadget_ep_free_request
,
954 .queue
= dwc3_gadget_ep_queue
,
955 .dequeue
= dwc3_gadget_ep_dequeue
,
956 .set_halt
= dwc3_gadget_ep_set_halt
,
957 .set_wedge
= dwc3_gadget_ep_set_wedge
,
960 /* -------------------------------------------------------------------------- */
962 static int dwc3_gadget_get_frame(struct usb_gadget
*g
)
964 struct dwc3
*dwc
= gadget_to_dwc(g
);
967 reg
= dwc3_readl(dwc
->regs
, DWC3_DSTS
);
968 return DWC3_DSTS_SOFFN(reg
);
971 static int dwc3_gadget_wakeup(struct usb_gadget
*g
)
973 struct dwc3
*dwc
= gadget_to_dwc(g
);
975 unsigned long timeout
;
985 spin_lock_irqsave(&dwc
->lock
, flags
);
988 * According to the Databook Remote wakeup request should
989 * be issued only when the device is in early suspend state.
991 * We can check that via USB Link State bits in DSTS register.
993 reg
= dwc3_readl(dwc
->regs
, DWC3_DSTS
);
995 speed
= reg
& DWC3_DSTS_CONNECTSPD
;
996 if (speed
== DWC3_DSTS_SUPERSPEED
) {
997 dev_dbg(dwc
->dev
, "no wakeup on SuperSpeed\n");
1002 link_state
= DWC3_DSTS_USBLNKST(reg
);
1004 switch (link_state
) {
1005 case DWC3_LINK_STATE_RX_DET
: /* in HS, means Early Suspend */
1006 case DWC3_LINK_STATE_U3
: /* in HS, means SUSPEND */
1009 dev_dbg(dwc
->dev
, "can't wakeup from link state %d\n",
1015 reg
= dwc3_readl(dwc
->regs
, DWC3_DCTL
);
1018 * Switch link state to Recovery. In HS/FS/LS this means
1019 * RemoteWakeup Request
1021 reg
|= DWC3_DCTL_ULSTCHNG_RECOVERY
;
1022 dwc3_writel(dwc
->regs
, DWC3_DCTL
, reg
);
1024 /* wait for at least 2000us */
1025 usleep_range(2000, 2500);
1027 /* write zeroes to Link Change Request */
1028 reg
&= ~DWC3_DCTL_ULSTCHNGREQ_MASK
;
1029 dwc3_writel(dwc
->regs
, DWC3_DCTL
, reg
);
1031 /* pool until Link State change to ON */
1032 timeout
= jiffies
+ msecs_to_jiffies(100);
1034 while (!(time_after(jiffies
, timeout
))) {
1035 reg
= dwc3_readl(dwc
->regs
, DWC3_DSTS
);
1037 /* in HS, means ON */
1038 if (DWC3_DSTS_USBLNKST(reg
) == DWC3_LINK_STATE_U0
)
1042 if (DWC3_DSTS_USBLNKST(reg
) != DWC3_LINK_STATE_U0
) {
1043 dev_err(dwc
->dev
, "failed to send remote wakeup\n");
1048 spin_unlock_irqrestore(&dwc
->lock
, flags
);
1053 static int dwc3_gadget_set_selfpowered(struct usb_gadget
*g
,
1056 struct dwc3
*dwc
= gadget_to_dwc(g
);
1058 dwc
->is_selfpowered
= !!is_selfpowered
;
1063 static void dwc3_gadget_run_stop(struct dwc3
*dwc
, int is_on
)
1066 unsigned long timeout
= 500;
1068 reg
= dwc3_readl(dwc
->regs
, DWC3_DCTL
);
1070 reg
|= DWC3_DCTL_RUN_STOP
;
1072 reg
&= ~DWC3_DCTL_RUN_STOP
;
1074 dwc3_writel(dwc
->regs
, DWC3_DCTL
, reg
);
1077 reg
= dwc3_readl(dwc
->regs
, DWC3_DSTS
);
1079 if (!(reg
& DWC3_DSTS_DEVCTRLHLT
))
1082 if (reg
& DWC3_DSTS_DEVCTRLHLT
)
1086 * XXX reduce the 500ms delay
1094 dev_vdbg(dwc
->dev
, "gadget %s data soft-%s\n",
1096 ? dwc
->gadget_driver
->function
: "no-function",
1097 is_on
? "connect" : "disconnect");
1100 static int dwc3_gadget_pullup(struct usb_gadget
*g
, int is_on
)
1102 struct dwc3
*dwc
= gadget_to_dwc(g
);
1103 unsigned long flags
;
1107 spin_lock_irqsave(&dwc
->lock
, flags
);
1108 dwc3_gadget_run_stop(dwc
, is_on
);
1109 spin_unlock_irqrestore(&dwc
->lock
, flags
);
1114 static int dwc3_gadget_start(struct usb_gadget
*g
,
1115 struct usb_gadget_driver
*driver
)
1117 struct dwc3
*dwc
= gadget_to_dwc(g
);
1118 struct dwc3_ep
*dep
;
1119 unsigned long flags
;
1123 spin_lock_irqsave(&dwc
->lock
, flags
);
1125 if (dwc
->gadget_driver
) {
1126 dev_err(dwc
->dev
, "%s is already bound to %s\n",
1128 dwc
->gadget_driver
->driver
.name
);
1133 dwc
->gadget_driver
= driver
;
1134 dwc
->gadget
.dev
.driver
= &driver
->driver
;
1136 reg
= dwc3_readl(dwc
->regs
, DWC3_GCTL
);
1139 * REVISIT: power down scale might be different
1140 * depending on PHY used, need to pass that via platform_data
1142 reg
|= DWC3_GCTL_PWRDNSCALE(0x61a)
1143 | DWC3_GCTL_PRTCAPDIR(DWC3_GCTL_PRTCAP_DEVICE
);
1144 reg
&= ~DWC3_GCTL_DISSCRAMBLE
;
1147 * WORKAROUND: DWC3 revisions <1.90a have a bug
1148 * when The device fails to connect at SuperSpeed
1149 * and falls back to high-speed mode which causes
1150 * the device to enter in a Connect/Disconnect loop
1152 if (dwc
->revision
< DWC3_REVISION_190A
)
1153 reg
|= DWC3_GCTL_U2RSTECN
;
1155 dwc3_writel(dwc
->regs
, DWC3_GCTL
, reg
);
1157 reg
= dwc3_readl(dwc
->regs
, DWC3_DCFG
);
1158 reg
&= ~(DWC3_DCFG_SPEED_MASK
);
1159 reg
|= DWC3_DCFG_SUPERSPEED
;
1160 dwc3_writel(dwc
->regs
, DWC3_DCFG
, reg
);
1162 /* Start with SuperSpeed Default */
1163 dwc3_gadget_ep0_desc
.wMaxPacketSize
= cpu_to_le16(512);
1166 ret
= __dwc3_gadget_ep_enable(dep
, &dwc3_gadget_ep0_desc
);
1168 dev_err(dwc
->dev
, "failed to enable %s\n", dep
->name
);
1173 ret
= __dwc3_gadget_ep_enable(dep
, &dwc3_gadget_ep0_desc
);
1175 dev_err(dwc
->dev
, "failed to enable %s\n", dep
->name
);
1179 /* begin to receive SETUP packets */
1180 dwc
->ep0state
= EP0_IDLE
;
1181 dwc3_ep0_out_start(dwc
);
1183 spin_unlock_irqrestore(&dwc
->lock
, flags
);
1188 __dwc3_gadget_ep_disable(dwc
->eps
[0]);
1191 spin_unlock_irqrestore(&dwc
->lock
, flags
);
1196 static int dwc3_gadget_stop(struct usb_gadget
*g
,
1197 struct usb_gadget_driver
*driver
)
1199 struct dwc3
*dwc
= gadget_to_dwc(g
);
1200 unsigned long flags
;
1202 spin_lock_irqsave(&dwc
->lock
, flags
);
1204 __dwc3_gadget_ep_disable(dwc
->eps
[0]);
1205 __dwc3_gadget_ep_disable(dwc
->eps
[1]);
1207 dwc
->gadget_driver
= NULL
;
1208 dwc
->gadget
.dev
.driver
= NULL
;
1210 spin_unlock_irqrestore(&dwc
->lock
, flags
);
1214 static const struct usb_gadget_ops dwc3_gadget_ops
= {
1215 .get_frame
= dwc3_gadget_get_frame
,
1216 .wakeup
= dwc3_gadget_wakeup
,
1217 .set_selfpowered
= dwc3_gadget_set_selfpowered
,
1218 .pullup
= dwc3_gadget_pullup
,
1219 .udc_start
= dwc3_gadget_start
,
1220 .udc_stop
= dwc3_gadget_stop
,
1223 /* -------------------------------------------------------------------------- */
1225 static int __devinit
dwc3_gadget_init_endpoints(struct dwc3
*dwc
)
1227 struct dwc3_ep
*dep
;
1230 INIT_LIST_HEAD(&dwc
->gadget
.ep_list
);
1232 for (epnum
= 0; epnum
< DWC3_ENDPOINTS_NUM
; epnum
++) {
1233 dep
= kzalloc(sizeof(*dep
), GFP_KERNEL
);
1235 dev_err(dwc
->dev
, "can't allocate endpoint %d\n",
1241 dep
->number
= epnum
;
1242 dwc
->eps
[epnum
] = dep
;
1244 snprintf(dep
->name
, sizeof(dep
->name
), "ep%d%s", epnum
>> 1,
1245 (epnum
& 1) ? "in" : "out");
1246 dep
->endpoint
.name
= dep
->name
;
1247 dep
->direction
= (epnum
& 1);
1249 if (epnum
== 0 || epnum
== 1) {
1250 dep
->endpoint
.maxpacket
= 512;
1251 dep
->endpoint
.ops
= &dwc3_gadget_ep0_ops
;
1253 dwc
->gadget
.ep0
= &dep
->endpoint
;
1257 dep
->endpoint
.maxpacket
= 1024;
1258 dep
->endpoint
.ops
= &dwc3_gadget_ep_ops
;
1259 list_add_tail(&dep
->endpoint
.ep_list
,
1260 &dwc
->gadget
.ep_list
);
1262 ret
= dwc3_alloc_trb_pool(dep
);
1264 dev_err(dwc
->dev
, "%s: failed to allocate TRB pool\n", dep
->name
);
1268 INIT_LIST_HEAD(&dep
->request_list
);
1269 INIT_LIST_HEAD(&dep
->req_queued
);
1275 static void dwc3_gadget_free_endpoints(struct dwc3
*dwc
)
1277 struct dwc3_ep
*dep
;
1280 for (epnum
= 0; epnum
< DWC3_ENDPOINTS_NUM
; epnum
++) {
1281 dep
= dwc
->eps
[epnum
];
1282 dwc3_free_trb_pool(dep
);
1284 if (epnum
!= 0 && epnum
!= 1)
1285 list_del(&dep
->endpoint
.ep_list
);
1291 static void dwc3_gadget_release(struct device
*dev
)
1293 dev_dbg(dev
, "%s\n", __func__
);
1296 /* -------------------------------------------------------------------------- */
1297 static int dwc3_cleanup_done_reqs(struct dwc3
*dwc
, struct dwc3_ep
*dep
,
1298 const struct dwc3_event_depevt
*event
, int status
)
1300 struct dwc3_request
*req
;
1301 struct dwc3_trb trb
;
1303 unsigned int s_pkt
= 0;
1306 req
= next_request(&dep
->req_queued
);
1310 dwc3_trb_to_nat(req
->trb
, &trb
);
1313 dev_err(dwc
->dev
, "%s's TRB (%p) still owned by HW\n",
1314 dep
->name
, req
->trb
);
1319 if (dep
->direction
) {
1321 dev_err(dwc
->dev
, "incomplete IN transfer %s\n",
1323 status
= -ECONNRESET
;
1326 if (count
&& (event
->status
& DEPEVT_STATUS_SHORT
))
1331 * We assume here we will always receive the entire data block
1332 * which we should receive. Meaning, if we program RX to
1333 * receive 4K but we receive only 2K, we assume that's all we
1334 * should receive and we simply bounce the request back to the
1335 * gadget driver for further processing.
1337 req
->request
.actual
+= req
->request
.length
- count
;
1338 dwc3_gadget_giveback(dep
, req
, status
);
1341 if ((event
->status
& DEPEVT_STATUS_LST
) && trb
.lst
)
1343 if ((event
->status
& DEPEVT_STATUS_IOC
) && trb
.ioc
)
1347 if ((event
->status
& DEPEVT_STATUS_IOC
) && trb
.ioc
)
1352 static void dwc3_endpoint_transfer_complete(struct dwc3
*dwc
,
1353 struct dwc3_ep
*dep
, const struct dwc3_event_depevt
*event
,
1356 unsigned status
= 0;
1359 if (event
->status
& DEPEVT_STATUS_BUSERR
)
1360 status
= -ECONNRESET
;
1362 clean_busy
= dwc3_cleanup_done_reqs(dwc
, dep
, event
, status
);
1364 dep
->flags
&= ~DWC3_EP_BUSY
;
1367 static void dwc3_gadget_start_isoc(struct dwc3
*dwc
,
1368 struct dwc3_ep
*dep
, const struct dwc3_event_depevt
*event
)
1372 if (list_empty(&dep
->request_list
)) {
1373 dev_vdbg(dwc
->dev
, "ISOC ep %s run out for requests.\n",
1378 if (event
->parameters
) {
1381 mask
= ~(dep
->interval
- 1);
1382 uf
= event
->parameters
& mask
;
1383 /* 4 micro frames in the future */
1384 uf
+= dep
->interval
* 4;
1389 __dwc3_gadget_kick_transfer(dep
, uf
, 1);
1392 static void dwc3_process_ep_cmd_complete(struct dwc3_ep
*dep
,
1393 const struct dwc3_event_depevt
*event
)
1395 struct dwc3
*dwc
= dep
->dwc
;
1396 struct dwc3_event_depevt mod_ev
= *event
;
1399 * We were asked to remove one requests. It is possible that this
1400 * request and a few other were started together and have the same
1401 * transfer index. Since we stopped the complete endpoint we don't
1402 * know how many requests were already completed (and not yet)
1403 * reported and how could be done (later). We purge them all until
1404 * the end of the list.
1406 mod_ev
.status
= DEPEVT_STATUS_LST
;
1407 dwc3_cleanup_done_reqs(dwc
, dep
, &mod_ev
, -ESHUTDOWN
);
1408 dep
->flags
&= ~DWC3_EP_BUSY
;
1409 /* pending requets are ignored and are queued on XferNotReady */
1411 if (dep
->flags
& DWC3_EP_WILL_SHUTDOWN
) {
1412 while (!list_empty(&dep
->req_queued
)) {
1413 struct dwc3_request
*req
;
1415 req
= next_request(&dep
->req_queued
);
1416 dwc3_gadget_giveback(dep
, req
, -ESHUTDOWN
);
1418 dep
->flags
&= DWC3_EP_WILL_SHUTDOWN
;
1422 static void dwc3_ep_cmd_compl(struct dwc3_ep
*dep
,
1423 const struct dwc3_event_depevt
*event
)
1425 u32 param
= event
->parameters
;
1426 u32 cmd_type
= (param
>> 8) & ((1 << 5) - 1);
1429 case DWC3_DEPCMD_ENDTRANSFER
:
1430 dwc3_process_ep_cmd_complete(dep
, event
);
1432 case DWC3_DEPCMD_STARTTRANSFER
:
1433 dep
->res_trans_idx
= param
& 0x7f;
1436 printk(KERN_ERR
"%s() unknown /unexpected type: %d\n",
1437 __func__
, cmd_type
);
1442 static void dwc3_endpoint_interrupt(struct dwc3
*dwc
,
1443 const struct dwc3_event_depevt
*event
)
1445 struct dwc3_ep
*dep
;
1446 u8 epnum
= event
->endpoint_number
;
1448 dep
= dwc
->eps
[epnum
];
1450 dev_vdbg(dwc
->dev
, "%s: %s\n", dep
->name
,
1451 dwc3_ep_event_string(event
->endpoint_event
));
1453 if (epnum
== 0 || epnum
== 1) {
1454 dwc3_ep0_interrupt(dwc
, event
);
1458 switch (event
->endpoint_event
) {
1459 case DWC3_DEPEVT_XFERCOMPLETE
:
1460 if (usb_endpoint_xfer_isoc(dep
->desc
)) {
1461 dev_dbg(dwc
->dev
, "%s is an Isochronous endpoint\n",
1466 dwc3_endpoint_transfer_complete(dwc
, dep
, event
, 1);
1468 case DWC3_DEPEVT_XFERINPROGRESS
:
1469 if (!usb_endpoint_xfer_isoc(dep
->desc
)) {
1470 dev_dbg(dwc
->dev
, "%s is not an Isochronous endpoint\n",
1475 dwc3_endpoint_transfer_complete(dwc
, dep
, event
, 0);
1477 case DWC3_DEPEVT_XFERNOTREADY
:
1478 if (usb_endpoint_xfer_isoc(dep
->desc
)) {
1479 dwc3_gadget_start_isoc(dwc
, dep
, event
);
1483 dev_vdbg(dwc
->dev
, "%s: reason %s\n",
1484 dep
->name
, event
->status
1486 : "Transfer Not Active");
1488 ret
= __dwc3_gadget_kick_transfer(dep
, 0, 1);
1489 if (!ret
|| ret
== -EBUSY
)
1492 dev_dbg(dwc
->dev
, "%s: failed to kick transfers\n",
1497 case DWC3_DEPEVT_RXTXFIFOEVT
:
1498 dev_dbg(dwc
->dev
, "%s FIFO Overrun\n", dep
->name
);
1500 case DWC3_DEPEVT_STREAMEVT
:
1501 dev_dbg(dwc
->dev
, "%s Stream Event\n", dep
->name
);
1503 case DWC3_DEPEVT_EPCMDCMPLT
:
1504 dwc3_ep_cmd_compl(dep
, event
);
1509 static void dwc3_disconnect_gadget(struct dwc3
*dwc
)
1511 if (dwc
->gadget_driver
&& dwc
->gadget_driver
->disconnect
) {
1512 spin_unlock(&dwc
->lock
);
1513 dwc
->gadget_driver
->disconnect(&dwc
->gadget
);
1514 spin_lock(&dwc
->lock
);
1518 static void dwc3_stop_active_transfer(struct dwc3
*dwc
, u32 epnum
)
1520 struct dwc3_ep
*dep
;
1521 struct dwc3_gadget_ep_cmd_params params
;
1525 dep
= dwc
->eps
[epnum
];
1527 if (dep
->res_trans_idx
) {
1528 cmd
= DWC3_DEPCMD_ENDTRANSFER
;
1529 cmd
|= DWC3_DEPCMD_HIPRI_FORCERM
| DWC3_DEPCMD_CMDIOC
;
1530 cmd
|= DWC3_DEPCMD_PARAM(dep
->res_trans_idx
);
1531 memset(¶ms
, 0, sizeof(params
));
1532 ret
= dwc3_send_gadget_ep_cmd(dwc
, dep
->number
, cmd
, ¶ms
);
1537 static void dwc3_stop_active_transfers(struct dwc3
*dwc
)
1541 for (epnum
= 2; epnum
< DWC3_ENDPOINTS_NUM
; epnum
++) {
1542 struct dwc3_ep
*dep
;
1544 dep
= dwc
->eps
[epnum
];
1545 if (!(dep
->flags
& DWC3_EP_ENABLED
))
1548 __dwc3_gadget_ep_disable(dep
);
1552 static void dwc3_clear_stall_all_ep(struct dwc3
*dwc
)
1556 for (epnum
= 1; epnum
< DWC3_ENDPOINTS_NUM
; epnum
++) {
1557 struct dwc3_ep
*dep
;
1558 struct dwc3_gadget_ep_cmd_params params
;
1561 dep
= dwc
->eps
[epnum
];
1563 if (!(dep
->flags
& DWC3_EP_STALL
))
1566 dep
->flags
&= ~DWC3_EP_STALL
;
1568 memset(¶ms
, 0, sizeof(params
));
1569 ret
= dwc3_send_gadget_ep_cmd(dwc
, dep
->number
,
1570 DWC3_DEPCMD_CLEARSTALL
, ¶ms
);
1575 static void dwc3_gadget_disconnect_interrupt(struct dwc3
*dwc
)
1577 dev_vdbg(dwc
->dev
, "%s\n", __func__
);
1580 U1
/U2 is powersave optimization
. Skip it
for now
. Anyway we need to
1581 enable it before we can disable it
.
1583 reg
= dwc3_readl(dwc
->regs
, DWC3_DCTL
);
1584 reg
&= ~DWC3_DCTL_INITU1ENA
;
1585 dwc3_writel(dwc
->regs
, DWC3_DCTL
, reg
);
1587 reg
&= ~DWC3_DCTL_INITU2ENA
;
1588 dwc3_writel(dwc
->regs
, DWC3_DCTL
, reg
);
1591 dwc3_stop_active_transfers(dwc
);
1592 dwc3_disconnect_gadget(dwc
);
1594 dwc
->gadget
.speed
= USB_SPEED_UNKNOWN
;
1597 static void dwc3_gadget_usb3_phy_power(struct dwc3
*dwc
, int on
)
1601 reg
= dwc3_readl(dwc
->regs
, DWC3_GUSB3PIPECTL(0));
1604 reg
&= ~DWC3_GUSB3PIPECTL_SUSPHY
;
1606 reg
|= DWC3_GUSB3PIPECTL_SUSPHY
;
1608 dwc3_writel(dwc
->regs
, DWC3_GUSB3PIPECTL(0), reg
);
1611 static void dwc3_gadget_usb2_phy_power(struct dwc3
*dwc
, int on
)
1615 reg
= dwc3_readl(dwc
->regs
, DWC3_GUSB2PHYCFG(0));
1618 reg
&= ~DWC3_GUSB2PHYCFG_SUSPHY
;
1620 reg
|= DWC3_GUSB2PHYCFG_SUSPHY
;
1622 dwc3_writel(dwc
->regs
, DWC3_GUSB2PHYCFG(0), reg
);
1625 static void dwc3_gadget_reset_interrupt(struct dwc3
*dwc
)
1629 dev_vdbg(dwc
->dev
, "%s\n", __func__
);
1632 dwc3_gadget_usb2_phy_power(dwc
, true);
1633 dwc3_gadget_usb3_phy_power(dwc
, true);
1635 if (dwc
->gadget
.speed
!= USB_SPEED_UNKNOWN
)
1636 dwc3_disconnect_gadget(dwc
);
1638 reg
= dwc3_readl(dwc
->regs
, DWC3_DCTL
);
1639 reg
&= ~DWC3_DCTL_TSTCTRL_MASK
;
1640 dwc3_writel(dwc
->regs
, DWC3_DCTL
, reg
);
1642 dwc3_stop_active_transfers(dwc
);
1643 dwc3_clear_stall_all_ep(dwc
);
1645 /* Reset device address to zero */
1646 reg
= dwc3_readl(dwc
->regs
, DWC3_DCFG
);
1647 reg
&= ~(DWC3_DCFG_DEVADDR_MASK
);
1648 dwc3_writel(dwc
->regs
, DWC3_DCFG
, reg
);
1651 * Wait for RxFifo to drain
1653 * REVISIT probably shouldn't wait forever.
1654 * In case Hardware ends up in a screwed up
1655 * case, we error out, notify the user and,
1656 * maybe, WARN() or BUG() but leave the rest
1657 * of the kernel working fine.
1659 * REVISIT the below is rather CPU intensive,
1660 * maybe we should read and if it doesn't work
1661 * sleep (not busy wait) for a few useconds.
1663 * REVISIT why wait until the RXFIFO is empty anyway?
1665 while (!(dwc3_readl(dwc
->regs
, DWC3_DSTS
)
1666 & DWC3_DSTS_RXFIFOEMPTY
))
1670 static void dwc3_update_ram_clk_sel(struct dwc3
*dwc
, u32 speed
)
1673 u32 usb30_clock
= DWC3_GCTL_CLK_BUS
;
1676 * We change the clock only at SS but I dunno why I would want to do
1677 * this. Maybe it becomes part of the power saving plan.
1680 if (speed
!= DWC3_DSTS_SUPERSPEED
)
1684 * RAMClkSel is reset to 0 after USB reset, so it must be reprogrammed
1685 * each time on Connect Done.
1690 reg
= dwc3_readl(dwc
->regs
, DWC3_GCTL
);
1691 reg
|= DWC3_GCTL_RAMCLKSEL(usb30_clock
);
1692 dwc3_writel(dwc
->regs
, DWC3_GCTL
, reg
);
1695 static void dwc3_gadget_disable_phy(struct dwc3
*dwc
, u8 speed
)
1698 case USB_SPEED_SUPER
:
1699 dwc3_gadget_usb2_phy_power(dwc
, false);
1701 case USB_SPEED_HIGH
:
1702 case USB_SPEED_FULL
:
1704 dwc3_gadget_usb3_phy_power(dwc
, false);
1709 static void dwc3_gadget_conndone_interrupt(struct dwc3
*dwc
)
1711 struct dwc3_gadget_ep_cmd_params params
;
1712 struct dwc3_ep
*dep
;
1717 dev_vdbg(dwc
->dev
, "%s\n", __func__
);
1719 memset(¶ms
, 0x00, sizeof(params
));
1721 dwc
->ep0state
= EP0_IDLE
;
1722 reg
= dwc3_readl(dwc
->regs
, DWC3_DSTS
);
1723 speed
= reg
& DWC3_DSTS_CONNECTSPD
;
1726 dwc3_update_ram_clk_sel(dwc
, speed
);
1729 case DWC3_DCFG_SUPERSPEED
:
1730 dwc3_gadget_ep0_desc
.wMaxPacketSize
= cpu_to_le16(512);
1731 dwc
->gadget
.ep0
->maxpacket
= 512;
1732 dwc
->gadget
.speed
= USB_SPEED_SUPER
;
1734 case DWC3_DCFG_HIGHSPEED
:
1735 dwc3_gadget_ep0_desc
.wMaxPacketSize
= cpu_to_le16(64);
1736 dwc
->gadget
.ep0
->maxpacket
= 64;
1737 dwc
->gadget
.speed
= USB_SPEED_HIGH
;
1739 case DWC3_DCFG_FULLSPEED2
:
1740 case DWC3_DCFG_FULLSPEED1
:
1741 dwc3_gadget_ep0_desc
.wMaxPacketSize
= cpu_to_le16(64);
1742 dwc
->gadget
.ep0
->maxpacket
= 64;
1743 dwc
->gadget
.speed
= USB_SPEED_FULL
;
1745 case DWC3_DCFG_LOWSPEED
:
1746 dwc3_gadget_ep0_desc
.wMaxPacketSize
= cpu_to_le16(8);
1747 dwc
->gadget
.ep0
->maxpacket
= 8;
1748 dwc
->gadget
.speed
= USB_SPEED_LOW
;
1752 /* Disable unneded PHY */
1753 dwc3_gadget_disable_phy(dwc
, dwc
->gadget
.speed
);
1756 ret
= __dwc3_gadget_ep_enable(dep
, &dwc3_gadget_ep0_desc
);
1758 dev_err(dwc
->dev
, "failed to enable %s\n", dep
->name
);
1763 ret
= __dwc3_gadget_ep_enable(dep
, &dwc3_gadget_ep0_desc
);
1765 dev_err(dwc
->dev
, "failed to enable %s\n", dep
->name
);
1770 * Configure PHY via GUSB3PIPECTLn if required.
1772 * Update GTXFIFOSIZn
1774 * In both cases reset values should be sufficient.
1778 static void dwc3_gadget_wakeup_interrupt(struct dwc3
*dwc
)
1780 dev_vdbg(dwc
->dev
, "%s\n", __func__
);
1783 * TODO take core out of low power mode when that's
1787 dwc
->gadget_driver
->resume(&dwc
->gadget
);
1790 static void dwc3_gadget_linksts_change_interrupt(struct dwc3
*dwc
,
1791 unsigned int evtinfo
)
1793 dev_vdbg(dwc
->dev
, "%s\n", __func__
);
1795 /* The fith bit says SuperSpeed yes or no. */
1796 dwc
->link_state
= evtinfo
& DWC3_LINK_STATE_MASK
;
1799 static void dwc3_gadget_interrupt(struct dwc3
*dwc
,
1800 const struct dwc3_event_devt
*event
)
1802 switch (event
->type
) {
1803 case DWC3_DEVICE_EVENT_DISCONNECT
:
1804 dwc3_gadget_disconnect_interrupt(dwc
);
1806 case DWC3_DEVICE_EVENT_RESET
:
1807 dwc3_gadget_reset_interrupt(dwc
);
1809 case DWC3_DEVICE_EVENT_CONNECT_DONE
:
1810 dwc3_gadget_conndone_interrupt(dwc
);
1812 case DWC3_DEVICE_EVENT_WAKEUP
:
1813 dwc3_gadget_wakeup_interrupt(dwc
);
1815 case DWC3_DEVICE_EVENT_LINK_STATUS_CHANGE
:
1816 dwc3_gadget_linksts_change_interrupt(dwc
, event
->event_info
);
1818 case DWC3_DEVICE_EVENT_EOPF
:
1819 dev_vdbg(dwc
->dev
, "End of Periodic Frame\n");
1821 case DWC3_DEVICE_EVENT_SOF
:
1822 dev_vdbg(dwc
->dev
, "Start of Periodic Frame\n");
1824 case DWC3_DEVICE_EVENT_ERRATIC_ERROR
:
1825 dev_vdbg(dwc
->dev
, "Erratic Error\n");
1827 case DWC3_DEVICE_EVENT_CMD_CMPL
:
1828 dev_vdbg(dwc
->dev
, "Command Complete\n");
1830 case DWC3_DEVICE_EVENT_OVERFLOW
:
1831 dev_vdbg(dwc
->dev
, "Overflow\n");
1834 dev_dbg(dwc
->dev
, "UNKNOWN IRQ %d\n", event
->type
);
1838 static void dwc3_process_event_entry(struct dwc3
*dwc
,
1839 const union dwc3_event
*event
)
1841 /* Endpoint IRQ, handle it and return early */
1842 if (event
->type
.is_devspec
== 0) {
1844 return dwc3_endpoint_interrupt(dwc
, &event
->depevt
);
1847 switch (event
->type
.type
) {
1848 case DWC3_EVENT_TYPE_DEV
:
1849 dwc3_gadget_interrupt(dwc
, &event
->devt
);
1851 /* REVISIT what to do with Carkit and I2C events ? */
1853 dev_err(dwc
->dev
, "UNKNOWN IRQ type %d\n", event
->raw
);
1857 static irqreturn_t
dwc3_process_event_buf(struct dwc3
*dwc
, u32 buf
)
1859 struct dwc3_event_buffer
*evt
;
1863 count
= dwc3_readl(dwc
->regs
, DWC3_GEVNTCOUNT(buf
));
1864 count
&= DWC3_GEVNTCOUNT_MASK
;
1868 evt
= dwc
->ev_buffs
[buf
];
1872 union dwc3_event event
;
1874 memcpy(&event
.raw
, (evt
->buf
+ evt
->lpos
), sizeof(event
.raw
));
1875 dwc3_process_event_entry(dwc
, &event
);
1877 * XXX we wrap around correctly to the next entry as almost all
1878 * entries are 4 bytes in size. There is one entry which has 12
1879 * bytes which is a regular entry followed by 8 bytes data. ATM
1880 * I don't know how things are organized if were get next to the
1881 * a boundary so I worry about that once we try to handle that.
1883 evt
->lpos
= (evt
->lpos
+ 4) % DWC3_EVENT_BUFFERS_SIZE
;
1886 dwc3_writel(dwc
->regs
, DWC3_GEVNTCOUNT(buf
), 4);
1892 static irqreturn_t
dwc3_interrupt(int irq
, void *_dwc
)
1894 struct dwc3
*dwc
= _dwc
;
1896 irqreturn_t ret
= IRQ_NONE
;
1898 spin_lock(&dwc
->lock
);
1900 for (i
= 0; i
< DWC3_EVENT_BUFFERS_NUM
; i
++) {
1903 status
= dwc3_process_event_buf(dwc
, i
);
1904 if (status
== IRQ_HANDLED
)
1908 spin_unlock(&dwc
->lock
);
1914 * dwc3_gadget_init - Initializes gadget related registers
1915 * @dwc: Pointer to out controller context structure
1917 * Returns 0 on success otherwise negative errno.
1919 int __devinit
dwc3_gadget_init(struct dwc3
*dwc
)
1925 dwc
->ctrl_req
= dma_alloc_coherent(dwc
->dev
, sizeof(*dwc
->ctrl_req
),
1926 &dwc
->ctrl_req_addr
, GFP_KERNEL
);
1927 if (!dwc
->ctrl_req
) {
1928 dev_err(dwc
->dev
, "failed to allocate ctrl request\n");
1933 dwc
->ep0_trb
= dma_alloc_coherent(dwc
->dev
, sizeof(*dwc
->ep0_trb
),
1934 &dwc
->ep0_trb_addr
, GFP_KERNEL
);
1935 if (!dwc
->ep0_trb
) {
1936 dev_err(dwc
->dev
, "failed to allocate ep0 trb\n");
1941 dwc
->setup_buf
= dma_alloc_coherent(dwc
->dev
,
1942 sizeof(*dwc
->setup_buf
) * 2,
1943 &dwc
->setup_buf_addr
, GFP_KERNEL
);
1944 if (!dwc
->setup_buf
) {
1945 dev_err(dwc
->dev
, "failed to allocate setup buffer\n");
1950 dev_set_name(&dwc
->gadget
.dev
, "gadget");
1952 dwc
->gadget
.ops
= &dwc3_gadget_ops
;
1953 dwc
->gadget
.is_dualspeed
= true;
1954 dwc
->gadget
.speed
= USB_SPEED_UNKNOWN
;
1955 dwc
->gadget
.dev
.parent
= dwc
->dev
;
1957 dma_set_coherent_mask(&dwc
->gadget
.dev
, dwc
->dev
->coherent_dma_mask
);
1959 dwc
->gadget
.dev
.dma_parms
= dwc
->dev
->dma_parms
;
1960 dwc
->gadget
.dev
.dma_mask
= dwc
->dev
->dma_mask
;
1961 dwc
->gadget
.dev
.release
= dwc3_gadget_release
;
1962 dwc
->gadget
.name
= "dwc3-gadget";
1965 * REVISIT: Here we should clear all pending IRQs to be
1966 * sure we're starting from a well known location.
1969 ret
= dwc3_gadget_init_endpoints(dwc
);
1973 irq
= platform_get_irq(to_platform_device(dwc
->dev
), 0);
1975 ret
= request_irq(irq
, dwc3_interrupt
, IRQF_SHARED
,
1978 dev_err(dwc
->dev
, "failed to request irq #%d --> %d\n",
1983 /* Enable all but Start and End of Frame IRQs */
1984 reg
= (DWC3_DEVTEN_VNDRDEVTSTRCVEDEN
|
1985 DWC3_DEVTEN_EVNTOVERFLOWEN
|
1986 DWC3_DEVTEN_CMDCMPLTEN
|
1987 DWC3_DEVTEN_ERRTICERREN
|
1988 DWC3_DEVTEN_WKUPEVTEN
|
1989 DWC3_DEVTEN_ULSTCNGEN
|
1990 DWC3_DEVTEN_CONNECTDONEEN
|
1991 DWC3_DEVTEN_USBRSTEN
|
1992 DWC3_DEVTEN_DISCONNEVTEN
);
1993 dwc3_writel(dwc
->regs
, DWC3_DEVTEN
, reg
);
1995 ret
= device_register(&dwc
->gadget
.dev
);
1997 dev_err(dwc
->dev
, "failed to register gadget device\n");
1998 put_device(&dwc
->gadget
.dev
);
2002 ret
= usb_add_gadget_udc(dwc
->dev
, &dwc
->gadget
);
2004 dev_err(dwc
->dev
, "failed to register udc\n");
2011 device_unregister(&dwc
->gadget
.dev
);
2014 dwc3_writel(dwc
->regs
, DWC3_DEVTEN
, 0x00);
2018 dwc3_gadget_free_endpoints(dwc
);
2021 dma_free_coherent(dwc
->dev
, sizeof(*dwc
->setup_buf
) * 2,
2022 dwc
->setup_buf
, dwc
->setup_buf_addr
);
2025 dma_free_coherent(dwc
->dev
, sizeof(*dwc
->ep0_trb
),
2026 dwc
->ep0_trb
, dwc
->ep0_trb_addr
);
2029 dma_free_coherent(dwc
->dev
, sizeof(*dwc
->ctrl_req
),
2030 dwc
->ctrl_req
, dwc
->ctrl_req_addr
);
2036 void dwc3_gadget_exit(struct dwc3
*dwc
)
2041 usb_del_gadget_udc(&dwc
->gadget
);
2042 irq
= platform_get_irq(to_platform_device(dwc
->dev
), 0);
2044 dwc3_writel(dwc
->regs
, DWC3_DEVTEN
, 0x00);
2047 for (i
= 0; i
< ARRAY_SIZE(dwc
->eps
); i
++)
2048 __dwc3_gadget_ep_disable(dwc
->eps
[i
]);
2050 dwc3_gadget_free_endpoints(dwc
);
2052 dma_free_coherent(dwc
->dev
, sizeof(*dwc
->setup_buf
) * 2,
2053 dwc
->setup_buf
, dwc
->setup_buf_addr
);
2055 dma_free_coherent(dwc
->dev
, sizeof(*dwc
->ep0_trb
),
2056 dwc
->ep0_trb
, dwc
->ep0_trb_addr
);
2058 dma_free_coherent(dwc
->dev
, sizeof(*dwc
->ctrl_req
),
2059 dwc
->ctrl_req
, dwc
->ctrl_req_addr
);
2061 device_unregister(&dwc
->gadget
.dev
);