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);
86 req->request.dma = DMA_ADDR_INVALID;
88 dma_sync_single_for_cpu(dwc->dev, req->request.dma,
89 req->request.length, req->direction
90 ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
94 void dwc3_gadget_giveback(struct dwc3_ep *dep, struct dwc3_request *req,
97 struct dwc3 *dwc = dep->dwc;
102 * Skip LINK TRB. We can't use req->trb and check for
103 * DWC3_TRBCTL_LINK_TRB because it points the TRB we just
104 * completed (not the LINK TRB).
106 if (((dep->busy_slot & DWC3_TRB_MASK) == DWC3_TRB_NUM - 1) &&
107 usb_endpoint_xfer_isoc(dep->desc))
110 list_del(&req->list);
112 if (req->request.status == -EINPROGRESS)
113 req->request.status = status;
115 dwc3_unmap_buffer_from_dma(req);
117 dev_dbg(dwc->dev, "request %p from %s completed %d/%d ===> %d\n",
118 req, dep->name, req->request.actual,
119 req->request.length, status);
121 spin_unlock(&dwc->lock);
122 req->request.complete(&req->dep->endpoint, &req->request);
123 spin_lock(&dwc->lock);
126 static const char *dwc3_gadget_ep_cmd_string(u8 cmd)
129 case DWC3_DEPCMD_DEPSTARTCFG:
130 return "Start New Configuration";
131 case DWC3_DEPCMD_ENDTRANSFER:
132 return "End Transfer";
133 case DWC3_DEPCMD_UPDATETRANSFER:
134 return "Update Transfer";
135 case DWC3_DEPCMD_STARTTRANSFER:
136 return "Start Transfer";
137 case DWC3_DEPCMD_CLEARSTALL:
138 return "Clear Stall";
139 case DWC3_DEPCMD_SETSTALL:
141 case DWC3_DEPCMD_GETSEQNUMBER:
142 return "Get Data Sequence Number";
143 case DWC3_DEPCMD_SETTRANSFRESOURCE:
144 return "Set Endpoint Transfer Resource";
145 case DWC3_DEPCMD_SETEPCONFIG:
146 return "Set Endpoint Configuration";
148 return "UNKNOWN command";
152 int dwc3_send_gadget_ep_cmd(struct dwc3 *dwc, unsigned ep,
153 unsigned cmd, struct dwc3_gadget_ep_cmd_params *params)
155 struct dwc3_ep *dep = dwc->eps[ep];
156 unsigned long timeout = 500;
159 dev_vdbg(dwc->dev, "%s: cmd '%s' params %08x %08x %08x\n",
161 dwc3_gadget_ep_cmd_string(cmd), params->param0.raw,
162 params->param1.raw, params->param2.raw);
164 dwc3_writel(dwc->regs, DWC3_DEPCMDPAR0(ep), params->param0.raw);
165 dwc3_writel(dwc->regs, DWC3_DEPCMDPAR1(ep), params->param1.raw);
166 dwc3_writel(dwc->regs, DWC3_DEPCMDPAR2(ep), params->param2.raw);
168 dwc3_writel(dwc->regs, DWC3_DEPCMD(ep), cmd | DWC3_DEPCMD_CMDACT);
170 reg = dwc3_readl(dwc->regs, DWC3_DEPCMD(ep));
171 if (!(reg & DWC3_DEPCMD_CMDACT)) {
172 dev_vdbg(dwc->dev, "Command Complete --> %d\n",
173 DWC3_DEPCMD_STATUS(reg));
178 * XXX Figure out a sane timeout here. 500ms is way too much.
179 * We can't sleep here, because it is also called from
190 static dma_addr_t dwc3_trb_dma_offset(struct dwc3_ep *dep,
191 struct dwc3_trb_hw *trb)
193 u32 offset = trb - dep->trb_pool;
195 return dep->trb_pool_dma + offset;
198 static int dwc3_alloc_trb_pool(struct dwc3_ep *dep)
200 struct dwc3 *dwc = dep->dwc;
205 if (dep->number == 0 || dep->number == 1)
208 dep->trb_pool = dma_alloc_coherent(dwc->dev,
209 sizeof(struct dwc3_trb) * DWC3_TRB_NUM,
210 &dep->trb_pool_dma, GFP_KERNEL);
211 if (!dep->trb_pool) {
212 dev_err(dep->dwc->dev, "failed to allocate trb pool for %s\n",
220 static void dwc3_free_trb_pool(struct dwc3_ep *dep)
222 struct dwc3 *dwc = dep->dwc;
224 dma_free_coherent(dwc->dev, sizeof(struct dwc3_trb) * DWC3_TRB_NUM,
225 dep->trb_pool, dep->trb_pool_dma);
227 dep->trb_pool = NULL;
228 dep->trb_pool_dma = 0;
231 static int dwc3_gadget_start_config(struct dwc3 *dwc, struct dwc3_ep *dep)
233 struct dwc3_gadget_ep_cmd_params params;
236 memset(¶ms, 0x00, sizeof(params));
238 if (dep->number != 1) {
239 cmd = DWC3_DEPCMD_DEPSTARTCFG;
240 /* XferRscIdx == 0 for ep0 and 2 for the remaining */
242 cmd |= DWC3_DEPCMD_PARAM(2);
244 return dwc3_send_gadget_ep_cmd(dwc, 0, cmd, ¶ms);
250 static int dwc3_gadget_set_ep_config(struct dwc3 *dwc, struct dwc3_ep *dep,
251 const struct usb_endpoint_descriptor *desc)
253 struct dwc3_gadget_ep_cmd_params params;
255 memset(¶ms, 0x00, sizeof(params));
257 params.param0.depcfg.ep_type = usb_endpoint_type(desc);
258 params.param0.depcfg.max_packet_size = usb_endpoint_maxp(desc);
260 params.param1.depcfg.xfer_complete_enable = true;
261 params.param1.depcfg.xfer_not_ready_enable = true;
263 if (usb_endpoint_xfer_isoc(desc))
264 params.param1.depcfg.xfer_in_progress_enable = true;
267 * We are doing 1:1 mapping for endpoints, meaning
268 * Physical Endpoints 2 maps to Logical Endpoint 2 and
269 * so on. We consider the direction bit as part of the physical
270 * endpoint number. So USB endpoint 0x81 is 0x03.
272 params.param1.depcfg.ep_number = dep->number;
275 * We must use the lower 16 TX FIFOs even though
279 params.param0.depcfg.fifo_number = dep->number >> 1;
281 if (desc->bInterval) {
282 params.param1.depcfg.binterval_m1 = desc->bInterval - 1;
283 dep->interval = 1 << (desc->bInterval - 1);
286 return dwc3_send_gadget_ep_cmd(dwc, dep->number,
287 DWC3_DEPCMD_SETEPCONFIG, ¶ms);
290 static int dwc3_gadget_set_xfer_resource(struct dwc3 *dwc, struct dwc3_ep *dep)
292 struct dwc3_gadget_ep_cmd_params params;
294 memset(¶ms, 0x00, sizeof(params));
296 params.param0.depxfercfg.number_xfer_resources = 1;
298 return dwc3_send_gadget_ep_cmd(dwc, dep->number,
299 DWC3_DEPCMD_SETTRANSFRESOURCE, ¶ms);
303 * __dwc3_gadget_ep_enable - Initializes a HW endpoint
304 * @dep: endpoint to be initialized
305 * @desc: USB Endpoint Descriptor
307 * Caller should take care of locking
309 static int __dwc3_gadget_ep_enable(struct dwc3_ep *dep,
310 const struct usb_endpoint_descriptor *desc)
312 struct dwc3 *dwc = dep->dwc;
316 if (!(dep->flags & DWC3_EP_ENABLED)) {
317 ret = dwc3_gadget_start_config(dwc, dep);
322 ret = dwc3_gadget_set_ep_config(dwc, dep, desc);
326 if (!(dep->flags & DWC3_EP_ENABLED)) {
327 struct dwc3_trb_hw *trb_st_hw;
328 struct dwc3_trb_hw *trb_link_hw;
329 struct dwc3_trb trb_link;
331 ret = dwc3_gadget_set_xfer_resource(dwc, dep);
336 dep->type = usb_endpoint_type(desc);
337 dep->flags |= DWC3_EP_ENABLED;
339 reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
340 reg |= DWC3_DALEPENA_EP(dep->number);
341 dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
343 if (!usb_endpoint_xfer_isoc(desc))
346 memset(&trb_link, 0, sizeof(trb_link));
348 /* Link TRB for ISOC. The HWO but is never reset */
349 trb_st_hw = &dep->trb_pool[0];
351 trb_link.bplh = dwc3_trb_dma_offset(dep, trb_st_hw);
352 trb_link.trbctl = DWC3_TRBCTL_LINK_TRB;
355 trb_link_hw = &dep->trb_pool[DWC3_TRB_NUM - 1];
356 dwc3_trb_to_hw(&trb_link, trb_link_hw);
362 static void dwc3_stop_active_transfer(struct dwc3 *dwc, u32 epnum);
363 static void dwc3_remove_requests(struct dwc3 *dwc, struct dwc3_ep *dep)
365 struct dwc3_request *req;
367 if (!list_empty(&dep->req_queued))
368 dwc3_stop_active_transfer(dwc, dep->number);
370 while (!list_empty(&dep->request_list)) {
371 req = next_request(&dep->request_list);
373 dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
378 * __dwc3_gadget_ep_disable - Disables a HW endpoint
379 * @dep: the endpoint to disable
381 * This function also removes requests which are currently processed ny the
382 * hardware and those which are not yet scheduled.
383 * Caller should take care of locking.
385 static int __dwc3_gadget_ep_disable(struct dwc3_ep *dep)
387 struct dwc3 *dwc = dep->dwc;
390 dep->flags &= ~DWC3_EP_ENABLED;
391 dwc3_remove_requests(dwc, dep);
393 reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
394 reg &= ~DWC3_DALEPENA_EP(dep->number);
395 dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
403 /* -------------------------------------------------------------------------- */
405 static int dwc3_gadget_ep0_enable(struct usb_ep *ep,
406 const struct usb_endpoint_descriptor *desc)
411 static int dwc3_gadget_ep0_disable(struct usb_ep *ep)
416 /* -------------------------------------------------------------------------- */
418 static int dwc3_gadget_ep_enable(struct usb_ep *ep,
419 const struct usb_endpoint_descriptor *desc)
426 if (!ep || !desc || desc->bDescriptorType != USB_DT_ENDPOINT) {
427 pr_debug("dwc3: invalid parameters\n");
431 if (!desc->wMaxPacketSize) {
432 pr_debug("dwc3: missing wMaxPacketSize\n");
436 dep = to_dwc3_ep(ep);
439 switch (usb_endpoint_type(desc)) {
440 case USB_ENDPOINT_XFER_CONTROL:
441 strncat(dep->name, "-control", sizeof(dep->name));
443 case USB_ENDPOINT_XFER_ISOC:
444 strncat(dep->name, "-isoc", sizeof(dep->name));
446 case USB_ENDPOINT_XFER_BULK:
447 strncat(dep->name, "-bulk", sizeof(dep->name));
449 case USB_ENDPOINT_XFER_INT:
450 strncat(dep->name, "-int", sizeof(dep->name));
453 dev_err(dwc->dev, "invalid endpoint transfer type\n");
456 if (dep->flags & DWC3_EP_ENABLED) {
457 dev_WARN_ONCE(dwc->dev, true, "%s is already enabled\n",
462 dev_vdbg(dwc->dev, "Enabling %s\n", dep->name);
464 spin_lock_irqsave(&dwc->lock, flags);
465 ret = __dwc3_gadget_ep_enable(dep, desc);
466 spin_unlock_irqrestore(&dwc->lock, flags);
471 static int dwc3_gadget_ep_disable(struct usb_ep *ep)
479 pr_debug("dwc3: invalid parameters\n");
483 dep = to_dwc3_ep(ep);
486 if (!(dep->flags & DWC3_EP_ENABLED)) {
487 dev_WARN_ONCE(dwc->dev, true, "%s is already disabled\n",
492 snprintf(dep->name, sizeof(dep->name), "ep%d%s",
494 (dep->number & 1) ? "in" : "out");
496 spin_lock_irqsave(&dwc->lock, flags);
497 ret = __dwc3_gadget_ep_disable(dep);
498 spin_unlock_irqrestore(&dwc->lock, flags);
503 static struct usb_request *dwc3_gadget_ep_alloc_request(struct usb_ep *ep,
506 struct dwc3_request *req;
507 struct dwc3_ep *dep = to_dwc3_ep(ep);
508 struct dwc3 *dwc = dep->dwc;
510 req = kzalloc(sizeof(*req), gfp_flags);
512 dev_err(dwc->dev, "not enough memory\n");
516 req->epnum = dep->number;
518 req->request.dma = DMA_ADDR_INVALID;
520 return &req->request;
523 static void dwc3_gadget_ep_free_request(struct usb_ep *ep,
524 struct usb_request *request)
526 struct dwc3_request *req = to_dwc3_request(request);
532 * dwc3_prepare_trbs - setup TRBs from requests
533 * @dep: endpoint for which requests are being prepared
534 * @starting: true if the endpoint is idle and no requests are queued.
536 * The functions goes through the requests list and setups TRBs for the
537 * transfers. The functions returns once there are not more TRBs available or
538 * it run out of requests.
540 static struct dwc3_request *dwc3_prepare_trbs(struct dwc3_ep *dep,
543 struct dwc3_request *req, *n, *ret = NULL;
544 struct dwc3_trb_hw *trb_hw;
548 BUILD_BUG_ON_NOT_POWER_OF_2(DWC3_TRB_NUM);
550 /* the first request must not be queued */
551 trbs_left = (dep->busy_slot - dep->free_slot) & DWC3_TRB_MASK;
553 * if busy & slot are equal than it is either full or empty. If we are
554 * starting to proceed requests then we are empty. Otherwise we ar
555 * full and don't do anything
560 trbs_left = DWC3_TRB_NUM;
562 * In case we start from scratch, we queue the ISOC requests
563 * starting from slot 1. This is done because we use ring
564 * buffer and have no LST bit to stop us. Instead, we place
565 * IOC bit TRB_NUM/4. We try to avoid to having an interrupt
566 * after the first request so we start at slot 1 and have
567 * 7 requests proceed before we hit the first IOC.
568 * Other transfer types don't use the ring buffer and are
569 * processed from the first TRB until the last one. Since we
570 * don't wrap around we have to start at the beginning.
572 if (usb_endpoint_xfer_isoc(dep->desc)) {
581 /* The last TRB is a link TRB, not used for xfer */
582 if ((trbs_left <= 1) && usb_endpoint_xfer_isoc(dep->desc))
585 list_for_each_entry_safe(req, n, &dep->request_list, list) {
586 unsigned int last_one = 0;
587 unsigned int cur_slot;
589 trb_hw = &dep->trb_pool[dep->free_slot & DWC3_TRB_MASK];
590 cur_slot = dep->free_slot;
593 /* Skip the LINK-TRB on ISOC */
594 if (((cur_slot & DWC3_TRB_MASK) == DWC3_TRB_NUM - 1) &&
595 usb_endpoint_xfer_isoc(dep->desc))
598 dwc3_gadget_move_request_queued(req);
599 memset(&trb, 0, sizeof(trb));
602 /* Is our TRB pool empty? */
605 /* Is this the last request? */
606 if (list_empty(&dep->request_list))
610 * FIXME we shouldn't need to set LST bit always but we are
611 * facing some weird problem with the Hardware where it doesn't
612 * complete even though it has been previously started.
614 * While we're debugging the problem, as a workaround to
615 * multiple TRBs handling, use only one TRB at a time.
623 trb.bplh = req->request.dma;
625 if (usb_endpoint_xfer_isoc(dep->desc)) {
632 switch (usb_endpoint_type(dep->desc)) {
633 case USB_ENDPOINT_XFER_CONTROL:
634 trb.trbctl = DWC3_TRBCTL_CONTROL_SETUP;
637 case USB_ENDPOINT_XFER_ISOC:
638 trb.trbctl = DWC3_TRBCTL_ISOCHRONOUS_FIRST;
640 /* IOC every DWC3_TRB_NUM / 4 so we can refill */
641 if (!(cur_slot % (DWC3_TRB_NUM / 4)))
645 case USB_ENDPOINT_XFER_BULK:
646 case USB_ENDPOINT_XFER_INT:
647 trb.trbctl = DWC3_TRBCTL_NORMAL;
651 * This is only possible with faulty memory because we
652 * checked it already :)
657 trb.length = req->request.length;
660 dwc3_trb_to_hw(&trb, trb_hw);
661 req->trb_dma = dwc3_trb_dma_offset(dep, trb_hw);
670 static int __dwc3_gadget_kick_transfer(struct dwc3_ep *dep, u16 cmd_param,
673 struct dwc3_gadget_ep_cmd_params params;
674 struct dwc3_request *req;
675 struct dwc3 *dwc = dep->dwc;
679 if (start_new && (dep->flags & DWC3_EP_BUSY)) {
680 dev_vdbg(dwc->dev, "%s: endpoint busy\n", dep->name);
683 dep->flags &= ~DWC3_EP_PENDING_REQUEST;
686 * If we are getting here after a short-out-packet we don't enqueue any
687 * new requests as we try to set the IOC bit only on the last request.
690 if (list_empty(&dep->req_queued))
691 dwc3_prepare_trbs(dep, start_new);
693 /* req points to the first request which will be sent */
694 req = next_request(&dep->req_queued);
697 * req points to the first request where HWO changed
700 req = dwc3_prepare_trbs(dep, start_new);
703 dep->flags |= DWC3_EP_PENDING_REQUEST;
707 memset(¶ms, 0, sizeof(params));
708 params.param0.depstrtxfer.transfer_desc_addr_high =
709 upper_32_bits(req->trb_dma);
710 params.param1.depstrtxfer.transfer_desc_addr_low =
711 lower_32_bits(req->trb_dma);
714 cmd = DWC3_DEPCMD_STARTTRANSFER;
716 cmd = DWC3_DEPCMD_UPDATETRANSFER;
718 cmd |= DWC3_DEPCMD_PARAM(cmd_param);
719 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number, cmd, ¶ms);
721 dev_dbg(dwc->dev, "failed to send STARTTRANSFER command\n");
724 * FIXME we need to iterate over the list of requests
725 * here and stop, unmap, free and del each of the linked
726 * requests instead of we do now.
728 dwc3_unmap_buffer_from_dma(req);
729 list_del(&req->list);
733 dep->flags |= DWC3_EP_BUSY;
734 dep->res_trans_idx = dwc3_gadget_ep_get_transfer_index(dwc,
736 if (!dep->res_trans_idx)
737 printk_once(KERN_ERR "%s() res_trans_idx is invalid\n", __func__);
741 static int __dwc3_gadget_ep_queue(struct dwc3_ep *dep, struct dwc3_request *req)
743 req->request.actual = 0;
744 req->request.status = -EINPROGRESS;
745 req->direction = dep->direction;
746 req->epnum = dep->number;
749 * We only add to our list of requests now and
750 * start consuming the list once we get XferNotReady
753 * That way, we avoid doing anything that we don't need
754 * to do now and defer it until the point we receive a
755 * particular token from the Host side.
757 * This will also avoid Host cancelling URBs due to too
760 dwc3_map_buffer_to_dma(req);
761 list_add_tail(&req->list, &dep->request_list);
764 * There is one special case: XferNotReady with
765 * empty list of requests. We need to kick the
766 * transfer here in that situation, otherwise
767 * we will be NAKing forever.
769 * If we get XferNotReady before gadget driver
770 * has a chance to queue a request, we will ACK
771 * the IRQ but won't be able to receive the data
772 * until the next request is queued. The following
773 * code is handling exactly that.
775 if (dep->flags & DWC3_EP_PENDING_REQUEST) {
780 if (usb_endpoint_xfer_isoc(dep->endpoint.desc) &&
781 dep->flags & DWC3_EP_BUSY)
784 ret = __dwc3_gadget_kick_transfer(dep, 0, start_trans);
785 if (ret && ret != -EBUSY) {
786 struct dwc3 *dwc = dep->dwc;
788 dev_dbg(dwc->dev, "%s: failed to kick transfers\n",
796 static int dwc3_gadget_ep_queue(struct usb_ep *ep, struct usb_request *request,
799 struct dwc3_request *req = to_dwc3_request(request);
800 struct dwc3_ep *dep = to_dwc3_ep(ep);
801 struct dwc3 *dwc = dep->dwc;
808 dev_dbg(dwc->dev, "trying to queue request %p to disabled %s\n",
813 dev_vdbg(dwc->dev, "queing request %p to %s length %d\n",
814 request, ep->name, request->length);
816 spin_lock_irqsave(&dwc->lock, flags);
817 ret = __dwc3_gadget_ep_queue(dep, req);
818 spin_unlock_irqrestore(&dwc->lock, flags);
823 static int dwc3_gadget_ep_dequeue(struct usb_ep *ep,
824 struct usb_request *request)
826 struct dwc3_request *req = to_dwc3_request(request);
827 struct dwc3_request *r = NULL;
829 struct dwc3_ep *dep = to_dwc3_ep(ep);
830 struct dwc3 *dwc = dep->dwc;
835 spin_lock_irqsave(&dwc->lock, flags);
837 list_for_each_entry(r, &dep->request_list, list) {
843 list_for_each_entry(r, &dep->req_queued, list) {
848 /* wait until it is processed */
849 dwc3_stop_active_transfer(dwc, dep->number);
852 dev_err(dwc->dev, "request %p was not queued to %s\n",
858 /* giveback the request */
859 dwc3_gadget_giveback(dep, req, -ECONNRESET);
862 spin_unlock_irqrestore(&dwc->lock, flags);
867 int __dwc3_gadget_ep_set_halt(struct dwc3_ep *dep, int value)
869 struct dwc3_gadget_ep_cmd_params params;
870 struct dwc3 *dwc = dep->dwc;
873 memset(¶ms, 0x00, sizeof(params));
876 if (dep->number == 0 || dep->number == 1)
877 dwc->ep0state = EP0_STALL;
879 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number,
880 DWC3_DEPCMD_SETSTALL, ¶ms);
882 dev_err(dwc->dev, "failed to %s STALL on %s\n",
883 value ? "set" : "clear",
886 dep->flags |= DWC3_EP_STALL;
888 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number,
889 DWC3_DEPCMD_CLEARSTALL, ¶ms);
891 dev_err(dwc->dev, "failed to %s STALL on %s\n",
892 value ? "set" : "clear",
895 dep->flags &= ~DWC3_EP_STALL;
900 static int dwc3_gadget_ep_set_halt(struct usb_ep *ep, int value)
902 struct dwc3_ep *dep = to_dwc3_ep(ep);
903 struct dwc3 *dwc = dep->dwc;
909 spin_lock_irqsave(&dwc->lock, flags);
911 if (usb_endpoint_xfer_isoc(dep->desc)) {
912 dev_err(dwc->dev, "%s is of Isochronous type\n", dep->name);
917 ret = __dwc3_gadget_ep_set_halt(dep, value);
919 spin_unlock_irqrestore(&dwc->lock, flags);
924 static int dwc3_gadget_ep_set_wedge(struct usb_ep *ep)
926 struct dwc3_ep *dep = to_dwc3_ep(ep);
928 dep->flags |= DWC3_EP_WEDGE;
930 return usb_ep_set_halt(ep);
933 /* -------------------------------------------------------------------------- */
935 static struct usb_endpoint_descriptor dwc3_gadget_ep0_desc = {
936 .bLength = USB_DT_ENDPOINT_SIZE,
937 .bDescriptorType = USB_DT_ENDPOINT,
938 .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
941 static const struct usb_ep_ops dwc3_gadget_ep0_ops = {
942 .enable = dwc3_gadget_ep0_enable,
943 .disable = dwc3_gadget_ep0_disable,
944 .alloc_request = dwc3_gadget_ep_alloc_request,
945 .free_request = dwc3_gadget_ep_free_request,
946 .queue = dwc3_gadget_ep0_queue,
947 .dequeue = dwc3_gadget_ep_dequeue,
948 .set_halt = dwc3_gadget_ep_set_halt,
949 .set_wedge = dwc3_gadget_ep_set_wedge,
952 static const struct usb_ep_ops dwc3_gadget_ep_ops = {
953 .enable = dwc3_gadget_ep_enable,
954 .disable = dwc3_gadget_ep_disable,
955 .alloc_request = dwc3_gadget_ep_alloc_request,
956 .free_request = dwc3_gadget_ep_free_request,
957 .queue = dwc3_gadget_ep_queue,
958 .dequeue = dwc3_gadget_ep_dequeue,
959 .set_halt = dwc3_gadget_ep_set_halt,
960 .set_wedge = dwc3_gadget_ep_set_wedge,
963 /* -------------------------------------------------------------------------- */
965 static int dwc3_gadget_get_frame(struct usb_gadget *g)
967 struct dwc3 *dwc = gadget_to_dwc(g);
970 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
971 return DWC3_DSTS_SOFFN(reg);
974 static int dwc3_gadget_wakeup(struct usb_gadget *g)
976 struct dwc3 *dwc = gadget_to_dwc(g);
978 unsigned long timeout;
988 spin_lock_irqsave(&dwc->lock, flags);
991 * According to the Databook Remote wakeup request should
992 * be issued only when the device is in early suspend state.
994 * We can check that via USB Link State bits in DSTS register.
996 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
998 speed = reg & DWC3_DSTS_CONNECTSPD;
999 if (speed == DWC3_DSTS_SUPERSPEED) {
1000 dev_dbg(dwc->dev, "no wakeup on SuperSpeed\n");
1005 link_state = DWC3_DSTS_USBLNKST(reg);
1007 switch (link_state) {
1008 case DWC3_LINK_STATE_RX_DET: /* in HS, means Early Suspend */
1009 case DWC3_LINK_STATE_U3: /* in HS, means SUSPEND */
1012 dev_dbg(dwc->dev, "can't wakeup from link state %d\n",
1018 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
1021 * Switch link state to Recovery. In HS/FS/LS this means
1022 * RemoteWakeup Request
1024 reg |= DWC3_DCTL_ULSTCHNG_RECOVERY;
1025 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1027 /* wait for at least 2000us */
1028 usleep_range(2000, 2500);
1030 /* write zeroes to Link Change Request */
1031 reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK;
1032 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1034 /* pool until Link State change to ON */
1035 timeout = jiffies + msecs_to_jiffies(100);
1037 while (!(time_after(jiffies, timeout))) {
1038 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1040 /* in HS, means ON */
1041 if (DWC3_DSTS_USBLNKST(reg) == DWC3_LINK_STATE_U0)
1045 if (DWC3_DSTS_USBLNKST(reg) != DWC3_LINK_STATE_U0) {
1046 dev_err(dwc->dev, "failed to send remote wakeup\n");
1051 spin_unlock_irqrestore(&dwc->lock, flags);
1056 static int dwc3_gadget_set_selfpowered(struct usb_gadget *g,
1059 struct dwc3 *dwc = gadget_to_dwc(g);
1061 dwc->is_selfpowered = !!is_selfpowered;
1066 static void dwc3_gadget_run_stop(struct dwc3 *dwc, int is_on)
1069 unsigned long timeout = 500;
1071 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
1073 reg |= DWC3_DCTL_RUN_STOP;
1075 reg &= ~DWC3_DCTL_RUN_STOP;
1077 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1080 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1082 if (!(reg & DWC3_DSTS_DEVCTRLHLT))
1085 if (reg & DWC3_DSTS_DEVCTRLHLT)
1089 * XXX reduce the 500ms delay
1097 dev_vdbg(dwc->dev, "gadget %s data soft-%s\n",
1099 ? dwc->gadget_driver->function : "no-function",
1100 is_on ? "connect" : "disconnect");
1103 static int dwc3_gadget_pullup(struct usb_gadget *g, int is_on)
1105 struct dwc3 *dwc = gadget_to_dwc(g);
1106 unsigned long flags;
1110 spin_lock_irqsave(&dwc->lock, flags);
1111 dwc3_gadget_run_stop(dwc, is_on);
1112 spin_unlock_irqrestore(&dwc->lock, flags);
1117 static int dwc3_gadget_start(struct usb_gadget *g,
1118 struct usb_gadget_driver *driver)
1120 struct dwc3 *dwc = gadget_to_dwc(g);
1121 struct dwc3_ep *dep;
1122 unsigned long flags;
1126 spin_lock_irqsave(&dwc->lock, flags);
1128 if (dwc->gadget_driver) {
1129 dev_err(dwc->dev, "%s is already bound to %s\n",
1131 dwc->gadget_driver->driver.name);
1136 dwc->gadget_driver = driver;
1137 dwc->gadget.dev.driver = &driver->driver;
1139 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
1142 * REVISIT: power down scale might be different
1143 * depending on PHY used, need to pass that via platform_data
1145 reg |= DWC3_GCTL_PWRDNSCALE(0x61a)
1146 | DWC3_GCTL_PRTCAPDIR(DWC3_GCTL_PRTCAP_DEVICE);
1147 reg &= ~DWC3_GCTL_DISSCRAMBLE;
1150 * WORKAROUND: DWC3 revisions <1.90a have a bug
1151 * when The device fails to connect at SuperSpeed
1152 * and falls back to high-speed mode which causes
1153 * the device to enter in a Connect/Disconnect loop
1155 if (dwc->revision < DWC3_REVISION_190A)
1156 reg |= DWC3_GCTL_U2RSTECN;
1158 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
1160 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
1161 reg &= ~(DWC3_DCFG_SPEED_MASK);
1162 reg |= DWC3_DCFG_SUPERSPEED;
1163 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
1165 /* Start with SuperSpeed Default */
1166 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
1169 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc);
1171 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
1176 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc);
1178 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
1182 /* begin to receive SETUP packets */
1183 dwc->ep0state = EP0_SETUP_PHASE;
1184 dwc3_ep0_out_start(dwc);
1186 spin_unlock_irqrestore(&dwc->lock, flags);
1191 __dwc3_gadget_ep_disable(dwc->eps[0]);
1194 spin_unlock_irqrestore(&dwc->lock, flags);
1199 static int dwc3_gadget_stop(struct usb_gadget *g,
1200 struct usb_gadget_driver *driver)
1202 struct dwc3 *dwc = gadget_to_dwc(g);
1203 unsigned long flags;
1205 spin_lock_irqsave(&dwc->lock, flags);
1207 __dwc3_gadget_ep_disable(dwc->eps[0]);
1208 __dwc3_gadget_ep_disable(dwc->eps[1]);
1210 dwc->gadget_driver = NULL;
1211 dwc->gadget.dev.driver = NULL;
1213 spin_unlock_irqrestore(&dwc->lock, flags);
1217 static const struct usb_gadget_ops dwc3_gadget_ops = {
1218 .get_frame = dwc3_gadget_get_frame,
1219 .wakeup = dwc3_gadget_wakeup,
1220 .set_selfpowered = dwc3_gadget_set_selfpowered,
1221 .pullup = dwc3_gadget_pullup,
1222 .udc_start = dwc3_gadget_start,
1223 .udc_stop = dwc3_gadget_stop,
1226 /* -------------------------------------------------------------------------- */
1228 static int __devinit dwc3_gadget_init_endpoints(struct dwc3 *dwc)
1230 struct dwc3_ep *dep;
1233 INIT_LIST_HEAD(&dwc->gadget.ep_list);
1235 for (epnum = 0; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
1236 dep = kzalloc(sizeof(*dep), GFP_KERNEL);
1238 dev_err(dwc->dev, "can't allocate endpoint %d\n",
1244 dep->number = epnum;
1245 dwc->eps[epnum] = dep;
1247 snprintf(dep->name, sizeof(dep->name), "ep%d%s", epnum >> 1,
1248 (epnum & 1) ? "in" : "out");
1249 dep->endpoint.name = dep->name;
1250 dep->direction = (epnum & 1);
1252 if (epnum == 0 || epnum == 1) {
1253 dep->endpoint.maxpacket = 512;
1254 dep->endpoint.ops = &dwc3_gadget_ep0_ops;
1256 dwc->gadget.ep0 = &dep->endpoint;
1260 dep->endpoint.maxpacket = 1024;
1261 dep->endpoint.ops = &dwc3_gadget_ep_ops;
1262 list_add_tail(&dep->endpoint.ep_list,
1263 &dwc->gadget.ep_list);
1265 ret = dwc3_alloc_trb_pool(dep);
1267 dev_err(dwc->dev, "%s: failed to allocate TRB pool\n", dep->name);
1271 INIT_LIST_HEAD(&dep->request_list);
1272 INIT_LIST_HEAD(&dep->req_queued);
1278 static void dwc3_gadget_free_endpoints(struct dwc3 *dwc)
1280 struct dwc3_ep *dep;
1283 for (epnum = 0; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
1284 dep = dwc->eps[epnum];
1285 dwc3_free_trb_pool(dep);
1287 if (epnum != 0 && epnum != 1)
1288 list_del(&dep->endpoint.ep_list);
1294 static void dwc3_gadget_release(struct device *dev)
1296 dev_dbg(dev, "%s\n", __func__);
1299 /* -------------------------------------------------------------------------- */
1300 static int dwc3_cleanup_done_reqs(struct dwc3 *dwc, struct dwc3_ep *dep,
1301 const struct dwc3_event_depevt *event, int status)
1303 struct dwc3_request *req;
1304 struct dwc3_trb trb;
1306 unsigned int s_pkt = 0;
1309 req = next_request(&dep->req_queued);
1313 dwc3_trb_to_nat(req->trb, &trb);
1315 if (trb.hwo && status != -ESHUTDOWN)
1317 * We continue despite the error. There is not much we
1318 * can do. If we don't clean in up we loop for ever. If
1319 * we skip the TRB than it gets overwritten reused after
1320 * a while since we use them in a ring buffer. a BUG()
1321 * would help. Lets hope that if this occures, someone
1322 * fixes the root cause instead of looking away :)
1324 dev_err(dwc->dev, "%s's TRB (%p) still owned by HW\n",
1325 dep->name, req->trb);
1328 if (dep->direction) {
1330 dev_err(dwc->dev, "incomplete IN transfer %s\n",
1332 status = -ECONNRESET;
1335 if (count && (event->status & DEPEVT_STATUS_SHORT))
1340 * We assume here we will always receive the entire data block
1341 * which we should receive. Meaning, if we program RX to
1342 * receive 4K but we receive only 2K, we assume that's all we
1343 * should receive and we simply bounce the request back to the
1344 * gadget driver for further processing.
1346 req->request.actual += req->request.length - count;
1347 dwc3_gadget_giveback(dep, req, status);
1350 if ((event->status & DEPEVT_STATUS_LST) && trb.lst)
1352 if ((event->status & DEPEVT_STATUS_IOC) && trb.ioc)
1356 if ((event->status & DEPEVT_STATUS_IOC) && trb.ioc)
1361 static void dwc3_endpoint_transfer_complete(struct dwc3 *dwc,
1362 struct dwc3_ep *dep, const struct dwc3_event_depevt *event,
1365 unsigned status = 0;
1368 if (event->status & DEPEVT_STATUS_BUSERR)
1369 status = -ECONNRESET;
1371 clean_busy = dwc3_cleanup_done_reqs(dwc, dep, event, status);
1373 dep->flags &= ~DWC3_EP_BUSY;
1374 dep->res_trans_idx = 0;
1378 static void dwc3_gadget_start_isoc(struct dwc3 *dwc,
1379 struct dwc3_ep *dep, const struct dwc3_event_depevt *event)
1383 if (list_empty(&dep->request_list)) {
1384 dev_vdbg(dwc->dev, "ISOC ep %s run out for requests.\n",
1389 if (event->parameters) {
1392 mask = ~(dep->interval - 1);
1393 uf = event->parameters & mask;
1394 /* 4 micro frames in the future */
1395 uf += dep->interval * 4;
1400 __dwc3_gadget_kick_transfer(dep, uf, 1);
1403 static void dwc3_process_ep_cmd_complete(struct dwc3_ep *dep,
1404 const struct dwc3_event_depevt *event)
1406 struct dwc3 *dwc = dep->dwc;
1407 struct dwc3_event_depevt mod_ev = *event;
1410 * We were asked to remove one requests. It is possible that this
1411 * request and a few other were started together and have the same
1412 * transfer index. Since we stopped the complete endpoint we don't
1413 * know how many requests were already completed (and not yet)
1414 * reported and how could be done (later). We purge them all until
1415 * the end of the list.
1417 mod_ev.status = DEPEVT_STATUS_LST;
1418 dwc3_cleanup_done_reqs(dwc, dep, &mod_ev, -ESHUTDOWN);
1419 dep->flags &= ~DWC3_EP_BUSY;
1420 /* pending requets are ignored and are queued on XferNotReady */
1423 static void dwc3_ep_cmd_compl(struct dwc3_ep *dep,
1424 const struct dwc3_event_depevt *event)
1426 u32 param = event->parameters;
1427 u32 cmd_type = (param >> 8) & ((1 << 5) - 1);
1430 case DWC3_DEPCMD_ENDTRANSFER:
1431 dwc3_process_ep_cmd_complete(dep, event);
1433 case DWC3_DEPCMD_STARTTRANSFER:
1434 dep->res_trans_idx = param & 0x7f;
1437 printk(KERN_ERR "%s() unknown /unexpected type: %d\n",
1438 __func__, cmd_type);
1443 static void dwc3_endpoint_interrupt(struct dwc3 *dwc,
1444 const struct dwc3_event_depevt *event)
1446 struct dwc3_ep *dep;
1447 u8 epnum = event->endpoint_number;
1449 dep = dwc->eps[epnum];
1451 dev_vdbg(dwc->dev, "%s: %s\n", dep->name,
1452 dwc3_ep_event_string(event->endpoint_event));
1454 if (epnum == 0 || epnum == 1) {
1455 dwc3_ep0_interrupt(dwc, event);
1459 switch (event->endpoint_event) {
1460 case DWC3_DEPEVT_XFERCOMPLETE:
1461 if (usb_endpoint_xfer_isoc(dep->desc)) {
1462 dev_dbg(dwc->dev, "%s is an Isochronous endpoint\n",
1467 dwc3_endpoint_transfer_complete(dwc, dep, event, 1);
1469 case DWC3_DEPEVT_XFERINPROGRESS:
1470 if (!usb_endpoint_xfer_isoc(dep->desc)) {
1471 dev_dbg(dwc->dev, "%s is not an Isochronous endpoint\n",
1476 dwc3_endpoint_transfer_complete(dwc, dep, event, 0);
1478 case DWC3_DEPEVT_XFERNOTREADY:
1479 if (usb_endpoint_xfer_isoc(dep->desc)) {
1480 dwc3_gadget_start_isoc(dwc, dep, event);
1484 dev_vdbg(dwc->dev, "%s: reason %s\n",
1485 dep->name, event->status
1487 : "Transfer Not Active");
1489 ret = __dwc3_gadget_kick_transfer(dep, 0, 1);
1490 if (!ret || ret == -EBUSY)
1493 dev_dbg(dwc->dev, "%s: failed to kick transfers\n",
1498 case DWC3_DEPEVT_RXTXFIFOEVT:
1499 dev_dbg(dwc->dev, "%s FIFO Overrun\n", dep->name);
1501 case DWC3_DEPEVT_STREAMEVT:
1502 dev_dbg(dwc->dev, "%s Stream Event\n", dep->name);
1504 case DWC3_DEPEVT_EPCMDCMPLT:
1505 dwc3_ep_cmd_compl(dep, event);
1510 static void dwc3_disconnect_gadget(struct dwc3 *dwc)
1512 if (dwc->gadget_driver && dwc->gadget_driver->disconnect) {
1513 spin_unlock(&dwc->lock);
1514 dwc->gadget_driver->disconnect(&dwc->gadget);
1515 spin_lock(&dwc->lock);
1519 static void dwc3_stop_active_transfer(struct dwc3 *dwc, u32 epnum)
1521 struct dwc3_ep *dep;
1522 struct dwc3_gadget_ep_cmd_params params;
1526 dep = dwc->eps[epnum];
1528 WARN_ON(!dep->res_trans_idx);
1529 if (dep->res_trans_idx) {
1530 cmd = DWC3_DEPCMD_ENDTRANSFER;
1531 cmd |= DWC3_DEPCMD_HIPRI_FORCERM | DWC3_DEPCMD_CMDIOC;
1532 cmd |= DWC3_DEPCMD_PARAM(dep->res_trans_idx);
1533 memset(¶ms, 0, sizeof(params));
1534 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number, cmd, ¶ms);
1536 dep->res_trans_idx = 0;
1540 static void dwc3_stop_active_transfers(struct dwc3 *dwc)
1544 for (epnum = 2; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
1545 struct dwc3_ep *dep;
1547 dep = dwc->eps[epnum];
1548 if (!(dep->flags & DWC3_EP_ENABLED))
1551 dwc3_remove_requests(dwc, dep);
1555 static void dwc3_clear_stall_all_ep(struct dwc3 *dwc)
1559 for (epnum = 1; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
1560 struct dwc3_ep *dep;
1561 struct dwc3_gadget_ep_cmd_params params;
1564 dep = dwc->eps[epnum];
1566 if (!(dep->flags & DWC3_EP_STALL))
1569 dep->flags &= ~DWC3_EP_STALL;
1571 memset(¶ms, 0, sizeof(params));
1572 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number,
1573 DWC3_DEPCMD_CLEARSTALL, ¶ms);
1578 static void dwc3_gadget_disconnect_interrupt(struct dwc3 *dwc)
1580 dev_vdbg(dwc->dev, "%s\n", __func__);
1583 U1/U2 is powersave optimization. Skip it for now. Anyway we need to
1584 enable it before we can disable it.
1586 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
1587 reg &= ~DWC3_DCTL_INITU1ENA;
1588 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1590 reg &= ~DWC3_DCTL_INITU2ENA;
1591 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1594 dwc3_stop_active_transfers(dwc);
1595 dwc3_disconnect_gadget(dwc);
1597 dwc->gadget.speed = USB_SPEED_UNKNOWN;
1600 static void dwc3_gadget_usb3_phy_power(struct dwc3 *dwc, int on)
1604 reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0));
1607 reg &= ~DWC3_GUSB3PIPECTL_SUSPHY;
1609 reg |= DWC3_GUSB3PIPECTL_SUSPHY;
1611 dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg);
1614 static void dwc3_gadget_usb2_phy_power(struct dwc3 *dwc, int on)
1618 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
1621 reg &= ~DWC3_GUSB2PHYCFG_SUSPHY;
1623 reg |= DWC3_GUSB2PHYCFG_SUSPHY;
1625 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
1628 static void dwc3_gadget_reset_interrupt(struct dwc3 *dwc)
1632 dev_vdbg(dwc->dev, "%s\n", __func__);
1635 dwc3_gadget_usb2_phy_power(dwc, true);
1636 dwc3_gadget_usb3_phy_power(dwc, true);
1638 if (dwc->gadget.speed != USB_SPEED_UNKNOWN)
1639 dwc3_disconnect_gadget(dwc);
1641 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
1642 reg &= ~DWC3_DCTL_TSTCTRL_MASK;
1643 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1645 dwc3_stop_active_transfers(dwc);
1646 dwc3_clear_stall_all_ep(dwc);
1648 /* Reset device address to zero */
1649 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
1650 reg &= ~(DWC3_DCFG_DEVADDR_MASK);
1651 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
1654 * Wait for RxFifo to drain
1656 * REVISIT probably shouldn't wait forever.
1657 * In case Hardware ends up in a screwed up
1658 * case, we error out, notify the user and,
1659 * maybe, WARN() or BUG() but leave the rest
1660 * of the kernel working fine.
1662 * REVISIT the below is rather CPU intensive,
1663 * maybe we should read and if it doesn't work
1664 * sleep (not busy wait) for a few useconds.
1666 * REVISIT why wait until the RXFIFO is empty anyway?
1668 while (!(dwc3_readl(dwc->regs, DWC3_DSTS)
1669 & DWC3_DSTS_RXFIFOEMPTY))
1673 static void dwc3_update_ram_clk_sel(struct dwc3 *dwc, u32 speed)
1676 u32 usb30_clock = DWC3_GCTL_CLK_BUS;
1679 * We change the clock only at SS but I dunno why I would want to do
1680 * this. Maybe it becomes part of the power saving plan.
1683 if (speed != DWC3_DSTS_SUPERSPEED)
1687 * RAMClkSel is reset to 0 after USB reset, so it must be reprogrammed
1688 * each time on Connect Done.
1693 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
1694 reg |= DWC3_GCTL_RAMCLKSEL(usb30_clock);
1695 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
1698 static void dwc3_gadget_disable_phy(struct dwc3 *dwc, u8 speed)
1701 case USB_SPEED_SUPER:
1702 dwc3_gadget_usb2_phy_power(dwc, false);
1704 case USB_SPEED_HIGH:
1705 case USB_SPEED_FULL:
1707 dwc3_gadget_usb3_phy_power(dwc, false);
1712 static void dwc3_gadget_conndone_interrupt(struct dwc3 *dwc)
1714 struct dwc3_gadget_ep_cmd_params params;
1715 struct dwc3_ep *dep;
1720 dev_vdbg(dwc->dev, "%s\n", __func__);
1722 memset(¶ms, 0x00, sizeof(params));
1724 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1725 speed = reg & DWC3_DSTS_CONNECTSPD;
1728 dwc3_update_ram_clk_sel(dwc, speed);
1731 case DWC3_DCFG_SUPERSPEED:
1732 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
1733 dwc->gadget.ep0->maxpacket = 512;
1734 dwc->gadget.speed = USB_SPEED_SUPER;
1736 case DWC3_DCFG_HIGHSPEED:
1737 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
1738 dwc->gadget.ep0->maxpacket = 64;
1739 dwc->gadget.speed = USB_SPEED_HIGH;
1741 case DWC3_DCFG_FULLSPEED2:
1742 case DWC3_DCFG_FULLSPEED1:
1743 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
1744 dwc->gadget.ep0->maxpacket = 64;
1745 dwc->gadget.speed = USB_SPEED_FULL;
1747 case DWC3_DCFG_LOWSPEED:
1748 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(8);
1749 dwc->gadget.ep0->maxpacket = 8;
1750 dwc->gadget.speed = USB_SPEED_LOW;
1754 /* Disable unneded PHY */
1755 dwc3_gadget_disable_phy(dwc, dwc->gadget.speed);
1758 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc);
1760 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
1765 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc);
1767 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
1772 * Configure PHY via GUSB3PIPECTLn if required.
1774 * Update GTXFIFOSIZn
1776 * In both cases reset values should be sufficient.
1780 static void dwc3_gadget_wakeup_interrupt(struct dwc3 *dwc)
1782 dev_vdbg(dwc->dev, "%s\n", __func__);
1785 * TODO take core out of low power mode when that's
1789 dwc->gadget_driver->resume(&dwc->gadget);
1792 static void dwc3_gadget_linksts_change_interrupt(struct dwc3 *dwc,
1793 unsigned int evtinfo)
1795 dev_vdbg(dwc->dev, "%s\n", __func__);
1797 /* The fith bit says SuperSpeed yes or no. */
1798 dwc->link_state = evtinfo & DWC3_LINK_STATE_MASK;
1801 static void dwc3_gadget_interrupt(struct dwc3 *dwc,
1802 const struct dwc3_event_devt *event)
1804 switch (event->type) {
1805 case DWC3_DEVICE_EVENT_DISCONNECT:
1806 dwc3_gadget_disconnect_interrupt(dwc);
1808 case DWC3_DEVICE_EVENT_RESET:
1809 dwc3_gadget_reset_interrupt(dwc);
1811 case DWC3_DEVICE_EVENT_CONNECT_DONE:
1812 dwc3_gadget_conndone_interrupt(dwc);
1814 case DWC3_DEVICE_EVENT_WAKEUP:
1815 dwc3_gadget_wakeup_interrupt(dwc);
1817 case DWC3_DEVICE_EVENT_LINK_STATUS_CHANGE:
1818 dwc3_gadget_linksts_change_interrupt(dwc, event->event_info);
1820 case DWC3_DEVICE_EVENT_EOPF:
1821 dev_vdbg(dwc->dev, "End of Periodic Frame\n");
1823 case DWC3_DEVICE_EVENT_SOF:
1824 dev_vdbg(dwc->dev, "Start of Periodic Frame\n");
1826 case DWC3_DEVICE_EVENT_ERRATIC_ERROR:
1827 dev_vdbg(dwc->dev, "Erratic Error\n");
1829 case DWC3_DEVICE_EVENT_CMD_CMPL:
1830 dev_vdbg(dwc->dev, "Command Complete\n");
1832 case DWC3_DEVICE_EVENT_OVERFLOW:
1833 dev_vdbg(dwc->dev, "Overflow\n");
1836 dev_dbg(dwc->dev, "UNKNOWN IRQ %d\n", event->type);
1840 static void dwc3_process_event_entry(struct dwc3 *dwc,
1841 const union dwc3_event *event)
1843 /* Endpoint IRQ, handle it and return early */
1844 if (event->type.is_devspec == 0) {
1846 return dwc3_endpoint_interrupt(dwc, &event->depevt);
1849 switch (event->type.type) {
1850 case DWC3_EVENT_TYPE_DEV:
1851 dwc3_gadget_interrupt(dwc, &event->devt);
1853 /* REVISIT what to do with Carkit and I2C events ? */
1855 dev_err(dwc->dev, "UNKNOWN IRQ type %d\n", event->raw);
1859 static irqreturn_t dwc3_process_event_buf(struct dwc3 *dwc, u32 buf)
1861 struct dwc3_event_buffer *evt;
1865 count = dwc3_readl(dwc->regs, DWC3_GEVNTCOUNT(buf));
1866 count &= DWC3_GEVNTCOUNT_MASK;
1870 evt = dwc->ev_buffs[buf];
1874 union dwc3_event event;
1876 memcpy(&event.raw, (evt->buf + evt->lpos), sizeof(event.raw));
1877 dwc3_process_event_entry(dwc, &event);
1879 * XXX we wrap around correctly to the next entry as almost all
1880 * entries are 4 bytes in size. There is one entry which has 12
1881 * bytes which is a regular entry followed by 8 bytes data. ATM
1882 * I don't know how things are organized if were get next to the
1883 * a boundary so I worry about that once we try to handle that.
1885 evt->lpos = (evt->lpos + 4) % DWC3_EVENT_BUFFERS_SIZE;
1888 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(buf), 4);
1894 static irqreturn_t dwc3_interrupt(int irq, void *_dwc)
1896 struct dwc3 *dwc = _dwc;
1898 irqreturn_t ret = IRQ_NONE;
1900 spin_lock(&dwc->lock);
1902 for (i = 0; i < DWC3_EVENT_BUFFERS_NUM; i++) {
1905 status = dwc3_process_event_buf(dwc, i);
1906 if (status == IRQ_HANDLED)
1910 spin_unlock(&dwc->lock);
1916 * dwc3_gadget_init - Initializes gadget related registers
1917 * @dwc: Pointer to out controller context structure
1919 * Returns 0 on success otherwise negative errno.
1921 int __devinit dwc3_gadget_init(struct dwc3 *dwc)
1927 dwc->ctrl_req = dma_alloc_coherent(dwc->dev, sizeof(*dwc->ctrl_req),
1928 &dwc->ctrl_req_addr, GFP_KERNEL);
1929 if (!dwc->ctrl_req) {
1930 dev_err(dwc->dev, "failed to allocate ctrl request\n");
1935 dwc->ep0_trb = dma_alloc_coherent(dwc->dev, sizeof(*dwc->ep0_trb),
1936 &dwc->ep0_trb_addr, GFP_KERNEL);
1937 if (!dwc->ep0_trb) {
1938 dev_err(dwc->dev, "failed to allocate ep0 trb\n");
1943 dwc->setup_buf = dma_alloc_coherent(dwc->dev,
1944 sizeof(*dwc->setup_buf) * 2,
1945 &dwc->setup_buf_addr, GFP_KERNEL);
1946 if (!dwc->setup_buf) {
1947 dev_err(dwc->dev, "failed to allocate setup buffer\n");
1952 dwc->ep0_bounce = dma_alloc_coherent(dwc->dev,
1953 512, &dwc->ep0_bounce_addr, GFP_KERNEL);
1954 if (!dwc->ep0_bounce) {
1955 dev_err(dwc->dev, "failed to allocate ep0 bounce buffer\n");
1960 dev_set_name(&dwc->gadget.dev, "gadget");
1962 dwc->gadget.ops = &dwc3_gadget_ops;
1963 dwc->gadget.is_dualspeed = true;
1964 dwc->gadget.speed = USB_SPEED_UNKNOWN;
1965 dwc->gadget.dev.parent = dwc->dev;
1967 dma_set_coherent_mask(&dwc->gadget.dev, dwc->dev->coherent_dma_mask);
1969 dwc->gadget.dev.dma_parms = dwc->dev->dma_parms;
1970 dwc->gadget.dev.dma_mask = dwc->dev->dma_mask;
1971 dwc->gadget.dev.release = dwc3_gadget_release;
1972 dwc->gadget.name = "dwc3-gadget";
1975 * REVISIT: Here we should clear all pending IRQs to be
1976 * sure we're starting from a well known location.
1979 ret = dwc3_gadget_init_endpoints(dwc);
1983 irq = platform_get_irq(to_platform_device(dwc->dev), 0);
1985 ret = request_irq(irq, dwc3_interrupt, IRQF_SHARED,
1988 dev_err(dwc->dev, "failed to request irq #%d --> %d\n",
1993 /* Enable all but Start and End of Frame IRQs */
1994 reg = (DWC3_DEVTEN_VNDRDEVTSTRCVEDEN |
1995 DWC3_DEVTEN_EVNTOVERFLOWEN |
1996 DWC3_DEVTEN_CMDCMPLTEN |
1997 DWC3_DEVTEN_ERRTICERREN |
1998 DWC3_DEVTEN_WKUPEVTEN |
1999 DWC3_DEVTEN_ULSTCNGEN |
2000 DWC3_DEVTEN_CONNECTDONEEN |
2001 DWC3_DEVTEN_USBRSTEN |
2002 DWC3_DEVTEN_DISCONNEVTEN);
2003 dwc3_writel(dwc->regs, DWC3_DEVTEN, reg);
2005 ret = device_register(&dwc->gadget.dev);
2007 dev_err(dwc->dev, "failed to register gadget device\n");
2008 put_device(&dwc->gadget.dev);
2012 ret = usb_add_gadget_udc(dwc->dev, &dwc->gadget);
2014 dev_err(dwc->dev, "failed to register udc\n");
2021 device_unregister(&dwc->gadget.dev);
2024 dwc3_writel(dwc->regs, DWC3_DEVTEN, 0x00);
2028 dwc3_gadget_free_endpoints(dwc);
2031 dma_free_coherent(dwc->dev, 512, dwc->ep0_bounce,
2032 dwc->ep0_bounce_addr);
2035 dma_free_coherent(dwc->dev, sizeof(*dwc->setup_buf) * 2,
2036 dwc->setup_buf, dwc->setup_buf_addr);
2039 dma_free_coherent(dwc->dev, sizeof(*dwc->ep0_trb),
2040 dwc->ep0_trb, dwc->ep0_trb_addr);
2043 dma_free_coherent(dwc->dev, sizeof(*dwc->ctrl_req),
2044 dwc->ctrl_req, dwc->ctrl_req_addr);
2050 void dwc3_gadget_exit(struct dwc3 *dwc)
2055 usb_del_gadget_udc(&dwc->gadget);
2056 irq = platform_get_irq(to_platform_device(dwc->dev), 0);
2058 dwc3_writel(dwc->regs, DWC3_DEVTEN, 0x00);
2061 for (i = 0; i < ARRAY_SIZE(dwc->eps); i++)
2062 __dwc3_gadget_ep_disable(dwc->eps[i]);
2064 dwc3_gadget_free_endpoints(dwc);
2066 dma_free_coherent(dwc->dev, 512, dwc->ep0_bounce,
2067 dwc->ep0_bounce_addr);
2069 dma_free_coherent(dwc->dev, sizeof(*dwc->setup_buf) * 2,
2070 dwc->setup_buf, dwc->setup_buf_addr);
2072 dma_free_coherent(dwc->dev, sizeof(*dwc->ep0_trb),
2073 dwc->ep0_trb, dwc->ep0_trb_addr);
2075 dma_free_coherent(dwc->dev, sizeof(*dwc->ctrl_req),
2076 dwc->ctrl_req, dwc->ctrl_req_addr);
2078 device_unregister(&dwc->gadget.dev);