2 * gadget.c - DesignWare USB3 DRD Controller Gadget Framework Link
4 * Copyright (C) 2010-2011 Texas Instruments Incorporated - http://www.ti.com
6 * Authors: Felipe Balbi <balbi@ti.com>,
7 * Sebastian Andrzej Siewior <bigeasy@linutronix.de>
9 * This program is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 of
11 * the License as published by the Free Software Foundation.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
19 #include <linux/kernel.h>
20 #include <linux/delay.h>
21 #include <linux/slab.h>
22 #include <linux/spinlock.h>
23 #include <linux/platform_device.h>
24 #include <linux/pm_runtime.h>
25 #include <linux/interrupt.h>
27 #include <linux/list.h>
28 #include <linux/dma-mapping.h>
30 #include <linux/usb/ch9.h>
31 #include <linux/usb/gadget.h>
39 * dwc3_gadget_set_test_mode - Enables USB2 Test Modes
40 * @dwc: pointer to our context structure
41 * @mode: the mode to set (J, K SE0 NAK, Force Enable)
43 * Caller should take care of locking. This function will
44 * return 0 on success or -EINVAL if wrong Test Selector
47 int dwc3_gadget_set_test_mode(struct dwc3 *dwc, int mode)
51 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
52 reg &= ~DWC3_DCTL_TSTCTRL_MASK;
66 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
72 * dwc3_gadget_get_link_state - Gets current state of USB Link
73 * @dwc: pointer to our context structure
75 * Caller should take care of locking. This function will
76 * return the link state on success (>= 0) or -ETIMEDOUT.
78 int dwc3_gadget_get_link_state(struct dwc3 *dwc)
82 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
84 return DWC3_DSTS_USBLNKST(reg);
88 * dwc3_gadget_set_link_state - Sets USB Link to a particular State
89 * @dwc: pointer to our context structure
90 * @state: the state to put link into
92 * Caller should take care of locking. This function will
93 * return 0 on success or -ETIMEDOUT.
95 int dwc3_gadget_set_link_state(struct dwc3 *dwc, enum dwc3_link_state state)
101 * Wait until device controller is ready. Only applies to 1.94a and
104 if (dwc->revision >= DWC3_REVISION_194A) {
106 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
107 if (reg & DWC3_DSTS_DCNRD)
117 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
118 reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK;
120 /* set requested state */
121 reg |= DWC3_DCTL_ULSTCHNGREQ(state);
122 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
125 * The following code is racy when called from dwc3_gadget_wakeup,
126 * and is not needed, at least on newer versions
128 if (dwc->revision >= DWC3_REVISION_194A)
131 /* wait for a change in DSTS */
134 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
136 if (DWC3_DSTS_USBLNKST(reg) == state)
142 dwc3_trace(trace_dwc3_gadget,
143 "link state change request timed out");
149 * dwc3_gadget_resize_tx_fifos - reallocate fifo spaces for current use-case
150 * @dwc: pointer to our context structure
152 * This function will a best effort FIFO allocation in order
153 * to improve FIFO usage and throughput, while still allowing
154 * us to enable as many endpoints as possible.
156 * Keep in mind that this operation will be highly dependent
157 * on the configured size for RAM1 - which contains TxFifo -,
158 * the amount of endpoints enabled on coreConsultant tool, and
159 * the width of the Master Bus.
161 * In the ideal world, we would always be able to satisfy the
162 * following equation:
164 * ((512 + 2 * MDWIDTH-Bytes) + (Number of IN Endpoints - 1) * \
165 * (3 * (1024 + MDWIDTH-Bytes) + MDWIDTH-Bytes)) / MDWIDTH-Bytes
167 * Unfortunately, due to many variables that's not always the case.
169 int dwc3_gadget_resize_tx_fifos(struct dwc3 *dwc)
171 int last_fifo_depth = 0;
177 if (!dwc->needs_fifo_resize)
180 ram1_depth = DWC3_RAM1_DEPTH(dwc->hwparams.hwparams7);
181 mdwidth = DWC3_MDWIDTH(dwc->hwparams.hwparams0);
183 /* MDWIDTH is represented in bits, we need it in bytes */
187 * FIXME For now we will only allocate 1 wMaxPacketSize space
188 * for each enabled endpoint, later patches will come to
189 * improve this algorithm so that we better use the internal
192 for (num = 0; num < dwc->num_in_eps; num++) {
193 /* bit0 indicates direction; 1 means IN ep */
194 struct dwc3_ep *dep = dwc->eps[(num << 1) | 1];
198 if (!(dep->flags & DWC3_EP_ENABLED))
201 if (usb_endpoint_xfer_bulk(dep->endpoint.desc)
202 || usb_endpoint_xfer_isoc(dep->endpoint.desc))
206 * REVISIT: the following assumes we will always have enough
207 * space available on the FIFO RAM for all possible use cases.
208 * Make sure that's true somehow and change FIFO allocation
211 * If we have Bulk or Isochronous endpoints, we want
212 * them to be able to be very, very fast. So we're giving
213 * those endpoints a fifo_size which is enough for 3 full
216 tmp = mult * (dep->endpoint.maxpacket + mdwidth);
219 fifo_size = DIV_ROUND_UP(tmp, mdwidth);
221 fifo_size |= (last_fifo_depth << 16);
223 dwc3_trace(trace_dwc3_gadget, "%s: Fifo Addr %04x Size %d",
224 dep->name, last_fifo_depth, fifo_size & 0xffff);
226 dwc3_writel(dwc->regs, DWC3_GTXFIFOSIZ(num), fifo_size);
228 last_fifo_depth += (fifo_size & 0xffff);
234 void dwc3_gadget_giveback(struct dwc3_ep *dep, struct dwc3_request *req,
237 struct dwc3 *dwc = dep->dwc;
238 unsigned int unmap_after_complete = false;
246 * Skip LINK TRB. We can't use req->trb and check for
247 * DWC3_TRBCTL_LINK_TRB because it points the TRB we
248 * just completed (not the LINK TRB).
250 if (((dep->busy_slot & DWC3_TRB_MASK) ==
252 usb_endpoint_xfer_isoc(dep->endpoint.desc))
254 } while(++i < req->request.num_mapped_sgs);
257 list_del(&req->list);
260 if (req->request.status == -EINPROGRESS)
261 req->request.status = status;
264 * NOTICE we don't want to unmap before calling ->complete() if we're
265 * dealing with a bounced ep0 request. If we unmap it here, we would end
266 * up overwritting the contents of req->buf and this could confuse the
269 if (dwc->ep0_bounced && dep->number <= 1) {
270 dwc->ep0_bounced = false;
271 unmap_after_complete = true;
273 usb_gadget_unmap_request(&dwc->gadget,
274 &req->request, req->direction);
277 dev_dbg(dwc->dev, "request %p from %s completed %d/%d ===> %d\n",
278 req, dep->name, req->request.actual,
279 req->request.length, status);
280 trace_dwc3_gadget_giveback(req);
282 spin_unlock(&dwc->lock);
283 usb_gadget_giveback_request(&dep->endpoint, &req->request);
284 spin_lock(&dwc->lock);
286 if (unmap_after_complete)
287 usb_gadget_unmap_request(&dwc->gadget,
288 &req->request, req->direction);
291 int dwc3_send_gadget_generic_command(struct dwc3 *dwc, unsigned cmd, u32 param)
296 trace_dwc3_gadget_generic_cmd(cmd, param);
298 dwc3_writel(dwc->regs, DWC3_DGCMDPAR, param);
299 dwc3_writel(dwc->regs, DWC3_DGCMD, cmd | DWC3_DGCMD_CMDACT);
302 reg = dwc3_readl(dwc->regs, DWC3_DGCMD);
303 if (!(reg & DWC3_DGCMD_CMDACT)) {
304 dwc3_trace(trace_dwc3_gadget,
305 "Command Complete --> %d",
306 DWC3_DGCMD_STATUS(reg));
307 if (DWC3_DGCMD_STATUS(reg))
313 * We can't sleep here, because it's also called from
318 dwc3_trace(trace_dwc3_gadget,
319 "Command Timed Out");
326 int dwc3_send_gadget_ep_cmd(struct dwc3 *dwc, unsigned ep,
327 unsigned cmd, struct dwc3_gadget_ep_cmd_params *params)
329 struct dwc3_ep *dep = dwc->eps[ep];
333 trace_dwc3_gadget_ep_cmd(dep, cmd, params);
335 dwc3_writel(dwc->regs, DWC3_DEPCMDPAR0(ep), params->param0);
336 dwc3_writel(dwc->regs, DWC3_DEPCMDPAR1(ep), params->param1);
337 dwc3_writel(dwc->regs, DWC3_DEPCMDPAR2(ep), params->param2);
339 dwc3_writel(dwc->regs, DWC3_DEPCMD(ep), cmd | DWC3_DEPCMD_CMDACT);
341 reg = dwc3_readl(dwc->regs, DWC3_DEPCMD(ep));
342 if (!(reg & DWC3_DEPCMD_CMDACT)) {
343 dwc3_trace(trace_dwc3_gadget,
344 "Command Complete --> %d",
345 DWC3_DEPCMD_STATUS(reg));
346 if (DWC3_DEPCMD_STATUS(reg))
352 * We can't sleep here, because it is also called from
357 dwc3_trace(trace_dwc3_gadget,
358 "Command Timed Out");
366 static dma_addr_t dwc3_trb_dma_offset(struct dwc3_ep *dep,
367 struct dwc3_trb *trb)
369 u32 offset = (char *) trb - (char *) dep->trb_pool;
371 return dep->trb_pool_dma + offset;
374 static int dwc3_alloc_trb_pool(struct dwc3_ep *dep)
376 struct dwc3 *dwc = dep->dwc;
381 dep->trb_pool = dma_alloc_coherent(dwc->dev,
382 sizeof(struct dwc3_trb) * DWC3_TRB_NUM,
383 &dep->trb_pool_dma, GFP_KERNEL);
384 if (!dep->trb_pool) {
385 dev_err(dep->dwc->dev, "failed to allocate trb pool for %s\n",
393 static void dwc3_free_trb_pool(struct dwc3_ep *dep)
395 struct dwc3 *dwc = dep->dwc;
397 dma_free_coherent(dwc->dev, sizeof(struct dwc3_trb) * DWC3_TRB_NUM,
398 dep->trb_pool, dep->trb_pool_dma);
400 dep->trb_pool = NULL;
401 dep->trb_pool_dma = 0;
404 static int dwc3_gadget_set_xfer_resource(struct dwc3 *dwc, struct dwc3_ep *dep);
407 * dwc3_gadget_start_config - Configure EP resources
408 * @dwc: pointer to our controller context structure
409 * @dep: endpoint that is being enabled
411 * The assignment of transfer resources cannot perfectly follow the
412 * data book due to the fact that the controller driver does not have
413 * all knowledge of the configuration in advance. It is given this
414 * information piecemeal by the composite gadget framework after every
415 * SET_CONFIGURATION and SET_INTERFACE. Trying to follow the databook
416 * programming model in this scenario can cause errors. For two
419 * 1) The databook says to do DEPSTARTCFG for every SET_CONFIGURATION
420 * and SET_INTERFACE (8.1.5). This is incorrect in the scenario of
421 * multiple interfaces.
423 * 2) The databook does not mention doing more DEPXFERCFG for new
424 * endpoint on alt setting (8.1.6).
426 * The following simplified method is used instead:
428 * All hardware endpoints can be assigned a transfer resource and this
429 * setting will stay persistent until either a core reset or
430 * hibernation. So whenever we do a DEPSTARTCFG(0) we can go ahead and
431 * do DEPXFERCFG for every hardware endpoint as well. We are
432 * guaranteed that there are as many transfer resources as endpoints.
434 * This function is called for each endpoint when it is being enabled
435 * but is triggered only when called for EP0-out, which always happens
436 * first, and which should only happen in one of the above conditions.
438 static int dwc3_gadget_start_config(struct dwc3 *dwc, struct dwc3_ep *dep)
440 struct dwc3_gadget_ep_cmd_params params;
448 memset(¶ms, 0x00, sizeof(params));
449 cmd = DWC3_DEPCMD_DEPSTARTCFG;
451 ret = dwc3_send_gadget_ep_cmd(dwc, 0, cmd, ¶ms);
455 for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) {
456 struct dwc3_ep *dep = dwc->eps[i];
461 ret = dwc3_gadget_set_xfer_resource(dwc, dep);
469 static int dwc3_gadget_set_ep_config(struct dwc3 *dwc, struct dwc3_ep *dep,
470 const struct usb_endpoint_descriptor *desc,
471 const struct usb_ss_ep_comp_descriptor *comp_desc,
472 bool ignore, bool restore)
474 struct dwc3_gadget_ep_cmd_params params;
476 memset(¶ms, 0x00, sizeof(params));
478 params.param0 = DWC3_DEPCFG_EP_TYPE(usb_endpoint_type(desc))
479 | DWC3_DEPCFG_MAX_PACKET_SIZE(usb_endpoint_maxp(desc));
481 /* Burst size is only needed in SuperSpeed mode */
482 if (dwc->gadget.speed == USB_SPEED_SUPER) {
483 u32 burst = dep->endpoint.maxburst - 1;
485 params.param0 |= DWC3_DEPCFG_BURST_SIZE(burst);
489 params.param0 |= DWC3_DEPCFG_IGN_SEQ_NUM;
492 params.param0 |= DWC3_DEPCFG_ACTION_RESTORE;
493 params.param2 |= dep->saved_state;
496 params.param1 = DWC3_DEPCFG_XFER_COMPLETE_EN
497 | DWC3_DEPCFG_XFER_NOT_READY_EN;
499 if (usb_ss_max_streams(comp_desc) && usb_endpoint_xfer_bulk(desc)) {
500 params.param1 |= DWC3_DEPCFG_STREAM_CAPABLE
501 | DWC3_DEPCFG_STREAM_EVENT_EN;
502 dep->stream_capable = true;
505 if (!usb_endpoint_xfer_control(desc))
506 params.param1 |= DWC3_DEPCFG_XFER_IN_PROGRESS_EN;
509 * We are doing 1:1 mapping for endpoints, meaning
510 * Physical Endpoints 2 maps to Logical Endpoint 2 and
511 * so on. We consider the direction bit as part of the physical
512 * endpoint number. So USB endpoint 0x81 is 0x03.
514 params.param1 |= DWC3_DEPCFG_EP_NUMBER(dep->number);
517 * We must use the lower 16 TX FIFOs even though
521 params.param0 |= DWC3_DEPCFG_FIFO_NUMBER(dep->number >> 1);
523 if (desc->bInterval) {
527 * Valid range for DEPCFG.bInterval_m1 is from 0 to 13, and it
528 * must be set to 0 when the controller operates in full-speed.
530 bInterval_m1 = min_t(u8, desc->bInterval - 1, 13);
531 if (dwc->gadget.speed == USB_SPEED_FULL)
534 if (usb_endpoint_type(desc) == USB_ENDPOINT_XFER_INT &&
535 dwc->gadget.speed == USB_SPEED_FULL)
536 dep->interval = desc->bInterval;
538 dep->interval = 1 << (desc->bInterval - 1);
540 params.param1 |= DWC3_DEPCFG_BINTERVAL_M1(bInterval_m1);
543 return dwc3_send_gadget_ep_cmd(dwc, dep->number,
544 DWC3_DEPCMD_SETEPCONFIG, ¶ms);
547 static int dwc3_gadget_set_xfer_resource(struct dwc3 *dwc, struct dwc3_ep *dep)
549 struct dwc3_gadget_ep_cmd_params params;
551 memset(¶ms, 0x00, sizeof(params));
553 params.param0 = DWC3_DEPXFERCFG_NUM_XFER_RES(1);
555 return dwc3_send_gadget_ep_cmd(dwc, dep->number,
556 DWC3_DEPCMD_SETTRANSFRESOURCE, ¶ms);
560 * __dwc3_gadget_ep_enable - Initializes a HW endpoint
561 * @dep: endpoint to be initialized
562 * @desc: USB Endpoint Descriptor
564 * Caller should take care of locking
566 static int __dwc3_gadget_ep_enable(struct dwc3_ep *dep,
567 const struct usb_endpoint_descriptor *desc,
568 const struct usb_ss_ep_comp_descriptor *comp_desc,
569 bool ignore, bool restore)
571 struct dwc3 *dwc = dep->dwc;
575 dwc3_trace(trace_dwc3_gadget, "Enabling %s", dep->name);
577 if (!(dep->flags & DWC3_EP_ENABLED)) {
578 ret = dwc3_gadget_start_config(dwc, dep);
583 ret = dwc3_gadget_set_ep_config(dwc, dep, desc, comp_desc, ignore,
588 if (!(dep->flags & DWC3_EP_ENABLED)) {
589 struct dwc3_trb *trb_st_hw;
590 struct dwc3_trb *trb_link;
592 dep->endpoint.desc = desc;
593 dep->comp_desc = comp_desc;
594 dep->type = usb_endpoint_type(desc);
595 dep->flags |= DWC3_EP_ENABLED;
597 reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
598 reg |= DWC3_DALEPENA_EP(dep->number);
599 dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
601 if (!usb_endpoint_xfer_isoc(desc))
604 /* Link TRB for ISOC. The HWO bit is never reset */
605 trb_st_hw = &dep->trb_pool[0];
607 trb_link = &dep->trb_pool[DWC3_TRB_NUM - 1];
608 memset(trb_link, 0, sizeof(*trb_link));
610 trb_link->bpl = lower_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw));
611 trb_link->bph = upper_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw));
612 trb_link->ctrl |= DWC3_TRBCTL_LINK_TRB;
613 trb_link->ctrl |= DWC3_TRB_CTRL_HWO;
616 switch (usb_endpoint_type(desc)) {
617 case USB_ENDPOINT_XFER_CONTROL:
618 strlcat(dep->name, "-control", sizeof(dep->name));
620 case USB_ENDPOINT_XFER_ISOC:
621 strlcat(dep->name, "-isoc", sizeof(dep->name));
623 case USB_ENDPOINT_XFER_BULK:
624 strlcat(dep->name, "-bulk", sizeof(dep->name));
626 case USB_ENDPOINT_XFER_INT:
627 strlcat(dep->name, "-int", sizeof(dep->name));
630 dev_err(dwc->dev, "invalid endpoint transfer type\n");
636 static void dwc3_stop_active_transfer(struct dwc3 *dwc, u32 epnum, bool force);
637 static void dwc3_remove_requests(struct dwc3 *dwc, struct dwc3_ep *dep)
639 struct dwc3_request *req;
641 if (!list_empty(&dep->req_queued)) {
642 dwc3_stop_active_transfer(dwc, dep->number, true);
644 /* - giveback all requests to gadget driver */
645 while (!list_empty(&dep->req_queued)) {
646 req = next_request(&dep->req_queued);
648 dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
652 while (!list_empty(&dep->request_list)) {
653 req = next_request(&dep->request_list);
655 dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
660 * __dwc3_gadget_ep_disable - Disables a HW endpoint
661 * @dep: the endpoint to disable
663 * This function also removes requests which are currently processed ny the
664 * hardware and those which are not yet scheduled.
665 * Caller should take care of locking.
667 static int __dwc3_gadget_ep_disable(struct dwc3_ep *dep)
669 struct dwc3 *dwc = dep->dwc;
672 dwc3_trace(trace_dwc3_gadget, "Disabling %s", dep->name);
674 dwc3_remove_requests(dwc, dep);
676 /* make sure HW endpoint isn't stalled */
677 if (dep->flags & DWC3_EP_STALL)
678 __dwc3_gadget_ep_set_halt(dep, 0, false);
680 reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
681 reg &= ~DWC3_DALEPENA_EP(dep->number);
682 dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
684 dep->stream_capable = false;
685 dep->endpoint.desc = NULL;
686 dep->comp_desc = NULL;
690 snprintf(dep->name, sizeof(dep->name), "ep%d%s",
692 (dep->number & 1) ? "in" : "out");
697 /* -------------------------------------------------------------------------- */
699 static int dwc3_gadget_ep0_enable(struct usb_ep *ep,
700 const struct usb_endpoint_descriptor *desc)
705 static int dwc3_gadget_ep0_disable(struct usb_ep *ep)
710 /* -------------------------------------------------------------------------- */
712 static int dwc3_gadget_ep_enable(struct usb_ep *ep,
713 const struct usb_endpoint_descriptor *desc)
720 if (!ep || !desc || desc->bDescriptorType != USB_DT_ENDPOINT) {
721 pr_debug("dwc3: invalid parameters\n");
725 if (!desc->wMaxPacketSize) {
726 pr_debug("dwc3: missing wMaxPacketSize\n");
730 dep = to_dwc3_ep(ep);
733 if (dep->flags & DWC3_EP_ENABLED) {
734 dev_WARN_ONCE(dwc->dev, true, "%s is already enabled\n",
739 spin_lock_irqsave(&dwc->lock, flags);
740 ret = __dwc3_gadget_ep_enable(dep, desc, ep->comp_desc, false, false);
741 spin_unlock_irqrestore(&dwc->lock, flags);
746 static int dwc3_gadget_ep_disable(struct usb_ep *ep)
754 pr_debug("dwc3: invalid parameters\n");
758 dep = to_dwc3_ep(ep);
761 if (!(dep->flags & DWC3_EP_ENABLED)) {
762 dev_WARN_ONCE(dwc->dev, true, "%s is already disabled\n",
767 spin_lock_irqsave(&dwc->lock, flags);
768 ret = __dwc3_gadget_ep_disable(dep);
769 spin_unlock_irqrestore(&dwc->lock, flags);
774 static struct usb_request *dwc3_gadget_ep_alloc_request(struct usb_ep *ep,
777 struct dwc3_request *req;
778 struct dwc3_ep *dep = to_dwc3_ep(ep);
780 req = kzalloc(sizeof(*req), gfp_flags);
784 req->epnum = dep->number;
787 trace_dwc3_alloc_request(req);
789 return &req->request;
792 static void dwc3_gadget_ep_free_request(struct usb_ep *ep,
793 struct usb_request *request)
795 struct dwc3_request *req = to_dwc3_request(request);
797 trace_dwc3_free_request(req);
802 * dwc3_prepare_one_trb - setup one TRB from one request
803 * @dep: endpoint for which this request is prepared
804 * @req: dwc3_request pointer
806 static void dwc3_prepare_one_trb(struct dwc3_ep *dep,
807 struct dwc3_request *req, dma_addr_t dma,
808 unsigned length, unsigned last, unsigned chain, unsigned node)
810 struct dwc3_trb *trb;
812 dwc3_trace(trace_dwc3_gadget, "%s: req %p dma %08llx length %d%s%s",
813 dep->name, req, (unsigned long long) dma,
814 length, last ? " last" : "",
815 chain ? " chain" : "");
818 trb = &dep->trb_pool[dep->free_slot & DWC3_TRB_MASK];
821 dwc3_gadget_move_request_queued(req);
823 req->trb_dma = dwc3_trb_dma_offset(dep, trb);
824 req->start_slot = dep->free_slot & DWC3_TRB_MASK;
828 /* Skip the LINK-TRB on ISOC */
829 if (((dep->free_slot & DWC3_TRB_MASK) == DWC3_TRB_NUM - 1) &&
830 usb_endpoint_xfer_isoc(dep->endpoint.desc))
833 trb->size = DWC3_TRB_SIZE_LENGTH(length);
834 trb->bpl = lower_32_bits(dma);
835 trb->bph = upper_32_bits(dma);
837 switch (usb_endpoint_type(dep->endpoint.desc)) {
838 case USB_ENDPOINT_XFER_CONTROL:
839 trb->ctrl = DWC3_TRBCTL_CONTROL_SETUP;
842 case USB_ENDPOINT_XFER_ISOC:
844 trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS_FIRST;
846 trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS;
849 case USB_ENDPOINT_XFER_BULK:
850 case USB_ENDPOINT_XFER_INT:
851 trb->ctrl = DWC3_TRBCTL_NORMAL;
855 * This is only possible with faulty memory because we
856 * checked it already :)
861 if (!req->request.no_interrupt && !chain)
862 trb->ctrl |= DWC3_TRB_CTRL_IOC;
864 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
865 trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI;
866 trb->ctrl |= DWC3_TRB_CTRL_CSP;
868 trb->ctrl |= DWC3_TRB_CTRL_LST;
872 trb->ctrl |= DWC3_TRB_CTRL_CHN;
874 if (usb_endpoint_xfer_bulk(dep->endpoint.desc) && dep->stream_capable)
875 trb->ctrl |= DWC3_TRB_CTRL_SID_SOFN(req->request.stream_id);
877 trb->ctrl |= DWC3_TRB_CTRL_HWO;
879 trace_dwc3_prepare_trb(dep, trb);
883 * dwc3_prepare_trbs - setup TRBs from requests
884 * @dep: endpoint for which requests are being prepared
885 * @starting: true if the endpoint is idle and no requests are queued.
887 * The function goes through the requests list and sets up TRBs for the
888 * transfers. The function returns once there are no more TRBs available or
889 * it runs out of requests.
891 static void dwc3_prepare_trbs(struct dwc3_ep *dep, bool starting)
893 struct dwc3_request *req, *n;
896 unsigned int last_one = 0;
898 BUILD_BUG_ON_NOT_POWER_OF_2(DWC3_TRB_NUM);
900 /* the first request must not be queued */
901 trbs_left = (dep->busy_slot - dep->free_slot) & DWC3_TRB_MASK;
903 /* Can't wrap around on a non-isoc EP since there's no link TRB */
904 if (!usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
905 max = DWC3_TRB_NUM - (dep->free_slot & DWC3_TRB_MASK);
911 * If busy & slot are equal than it is either full or empty. If we are
912 * starting to process requests then we are empty. Otherwise we are
913 * full and don't do anything
918 trbs_left = DWC3_TRB_NUM;
920 * In case we start from scratch, we queue the ISOC requests
921 * starting from slot 1. This is done because we use ring
922 * buffer and have no LST bit to stop us. Instead, we place
923 * IOC bit every TRB_NUM/4. We try to avoid having an interrupt
924 * after the first request so we start at slot 1 and have
925 * 7 requests proceed before we hit the first IOC.
926 * Other transfer types don't use the ring buffer and are
927 * processed from the first TRB until the last one. Since we
928 * don't wrap around we have to start at the beginning.
930 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
939 /* The last TRB is a link TRB, not used for xfer */
940 if ((trbs_left <= 1) && usb_endpoint_xfer_isoc(dep->endpoint.desc))
943 list_for_each_entry_safe(req, n, &dep->request_list, list) {
948 if (req->request.num_mapped_sgs > 0) {
949 struct usb_request *request = &req->request;
950 struct scatterlist *sg = request->sg;
951 struct scatterlist *s;
954 for_each_sg(sg, s, request->num_mapped_sgs, i) {
955 unsigned chain = true;
957 length = sg_dma_len(s);
958 dma = sg_dma_address(s);
960 if (i == (request->num_mapped_sgs - 1) ||
962 if (list_empty(&dep->request_list))
974 dwc3_prepare_one_trb(dep, req, dma, length,
984 dma = req->request.dma;
985 length = req->request.length;
991 /* Is this the last request? */
992 if (list_is_last(&req->list, &dep->request_list))
995 dwc3_prepare_one_trb(dep, req, dma, length,
1004 static int __dwc3_gadget_kick_transfer(struct dwc3_ep *dep, u16 cmd_param,
1007 struct dwc3_gadget_ep_cmd_params params;
1008 struct dwc3_request *req;
1009 struct dwc3 *dwc = dep->dwc;
1013 if (start_new && (dep->flags & DWC3_EP_BUSY)) {
1014 dwc3_trace(trace_dwc3_gadget, "%s: endpoint busy", dep->name);
1019 * If we are getting here after a short-out-packet we don't enqueue any
1020 * new requests as we try to set the IOC bit only on the last request.
1023 if (list_empty(&dep->req_queued))
1024 dwc3_prepare_trbs(dep, start_new);
1026 /* req points to the first request which will be sent */
1027 req = next_request(&dep->req_queued);
1029 dwc3_prepare_trbs(dep, start_new);
1032 * req points to the first request where HWO changed from 0 to 1
1034 req = next_request(&dep->req_queued);
1037 dep->flags |= DWC3_EP_PENDING_REQUEST;
1041 memset(¶ms, 0, sizeof(params));
1044 params.param0 = upper_32_bits(req->trb_dma);
1045 params.param1 = lower_32_bits(req->trb_dma);
1046 cmd = DWC3_DEPCMD_STARTTRANSFER;
1048 cmd = DWC3_DEPCMD_UPDATETRANSFER;
1051 cmd |= DWC3_DEPCMD_PARAM(cmd_param);
1052 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number, cmd, ¶ms);
1054 dev_dbg(dwc->dev, "failed to send STARTTRANSFER command\n");
1057 * FIXME we need to iterate over the list of requests
1058 * here and stop, unmap, free and del each of the linked
1059 * requests instead of what we do now.
1061 usb_gadget_unmap_request(&dwc->gadget, &req->request,
1063 list_del(&req->list);
1067 dep->flags |= DWC3_EP_BUSY;
1070 dep->resource_index = dwc3_gadget_ep_get_transfer_index(dwc,
1072 WARN_ON_ONCE(!dep->resource_index);
1078 static void __dwc3_gadget_start_isoc(struct dwc3 *dwc,
1079 struct dwc3_ep *dep, u32 cur_uf)
1083 if (list_empty(&dep->request_list)) {
1084 dwc3_trace(trace_dwc3_gadget,
1085 "ISOC ep %s run out for requests",
1087 dep->flags |= DWC3_EP_PENDING_REQUEST;
1091 /* 4 micro frames in the future */
1092 uf = cur_uf + dep->interval * 4;
1094 __dwc3_gadget_kick_transfer(dep, uf, 1);
1097 static void dwc3_gadget_start_isoc(struct dwc3 *dwc,
1098 struct dwc3_ep *dep, const struct dwc3_event_depevt *event)
1102 mask = ~(dep->interval - 1);
1103 cur_uf = event->parameters & mask;
1105 __dwc3_gadget_start_isoc(dwc, dep, cur_uf);
1108 static int __dwc3_gadget_ep_queue(struct dwc3_ep *dep, struct dwc3_request *req)
1110 struct dwc3 *dwc = dep->dwc;
1113 req->request.actual = 0;
1114 req->request.status = -EINPROGRESS;
1115 req->direction = dep->direction;
1116 req->epnum = dep->number;
1118 trace_dwc3_ep_queue(req);
1121 * We only add to our list of requests now and
1122 * start consuming the list once we get XferNotReady
1125 * That way, we avoid doing anything that we don't need
1126 * to do now and defer it until the point we receive a
1127 * particular token from the Host side.
1129 * This will also avoid Host cancelling URBs due to too
1132 ret = usb_gadget_map_request(&dwc->gadget, &req->request,
1137 list_add_tail(&req->list, &dep->request_list);
1140 * If there are no pending requests and the endpoint isn't already
1141 * busy, we will just start the request straight away.
1143 * This will save one IRQ (XFER_NOT_READY) and possibly make it a
1144 * little bit faster.
1146 if (!usb_endpoint_xfer_isoc(dep->endpoint.desc) &&
1147 !usb_endpoint_xfer_int(dep->endpoint.desc) &&
1148 !(dep->flags & DWC3_EP_BUSY)) {
1149 ret = __dwc3_gadget_kick_transfer(dep, 0, true);
1154 * There are a few special cases:
1156 * 1. XferNotReady with empty list of requests. We need to kick the
1157 * transfer here in that situation, otherwise we will be NAKing
1158 * forever. If we get XferNotReady before gadget driver has a
1159 * chance to queue a request, we will ACK the IRQ but won't be
1160 * able to receive the data until the next request is queued.
1161 * The following code is handling exactly that.
1164 if (dep->flags & DWC3_EP_PENDING_REQUEST) {
1166 * If xfernotready is already elapsed and it is a case
1167 * of isoc transfer, then issue END TRANSFER, so that
1168 * you can receive xfernotready again and can have
1169 * notion of current microframe.
1171 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
1172 if (list_empty(&dep->req_queued)) {
1173 dwc3_stop_active_transfer(dwc, dep->number, true);
1174 dep->flags = DWC3_EP_ENABLED;
1179 ret = __dwc3_gadget_kick_transfer(dep, 0, true);
1181 dep->flags &= ~DWC3_EP_PENDING_REQUEST;
1187 * 2. XferInProgress on Isoc EP with an active transfer. We need to
1188 * kick the transfer here after queuing a request, otherwise the
1189 * core may not see the modified TRB(s).
1191 if (usb_endpoint_xfer_isoc(dep->endpoint.desc) &&
1192 (dep->flags & DWC3_EP_BUSY) &&
1193 !(dep->flags & DWC3_EP_MISSED_ISOC)) {
1194 WARN_ON_ONCE(!dep->resource_index);
1195 ret = __dwc3_gadget_kick_transfer(dep, dep->resource_index,
1201 * 4. Stream Capable Bulk Endpoints. We need to start the transfer
1202 * right away, otherwise host will not know we have streams to be
1205 if (dep->stream_capable)
1206 ret = __dwc3_gadget_kick_transfer(dep, 0, true);
1209 if (ret && ret != -EBUSY)
1210 dev_dbg(dwc->dev, "%s: failed to kick transfers\n",
1218 static int dwc3_gadget_ep_queue(struct usb_ep *ep, struct usb_request *request,
1221 struct dwc3_request *req = to_dwc3_request(request);
1222 struct dwc3_ep *dep = to_dwc3_ep(ep);
1223 struct dwc3 *dwc = dep->dwc;
1225 unsigned long flags;
1229 spin_lock_irqsave(&dwc->lock, flags);
1230 if (!dep->endpoint.desc) {
1231 dev_dbg(dwc->dev, "trying to queue request %p to disabled %s\n",
1237 if (WARN(req->dep != dep, "request %pK belongs to '%s'\n",
1238 request, req->dep->name)) {
1243 ret = __dwc3_gadget_ep_queue(dep, req);
1246 spin_unlock_irqrestore(&dwc->lock, flags);
1251 static int dwc3_gadget_ep_dequeue(struct usb_ep *ep,
1252 struct usb_request *request)
1254 struct dwc3_request *req = to_dwc3_request(request);
1255 struct dwc3_request *r = NULL;
1257 struct dwc3_ep *dep = to_dwc3_ep(ep);
1258 struct dwc3 *dwc = dep->dwc;
1260 unsigned long flags;
1263 trace_dwc3_ep_dequeue(req);
1265 spin_lock_irqsave(&dwc->lock, flags);
1267 list_for_each_entry(r, &dep->request_list, list) {
1273 list_for_each_entry(r, &dep->req_queued, list) {
1278 /* wait until it is processed */
1279 dwc3_stop_active_transfer(dwc, dep->number, true);
1282 dev_err(dwc->dev, "request %pK was not queued to %s\n",
1289 /* giveback the request */
1290 dwc3_gadget_giveback(dep, req, -ECONNRESET);
1293 spin_unlock_irqrestore(&dwc->lock, flags);
1298 int __dwc3_gadget_ep_set_halt(struct dwc3_ep *dep, int value, int protocol)
1300 struct dwc3_gadget_ep_cmd_params params;
1301 struct dwc3 *dwc = dep->dwc;
1304 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
1305 dev_err(dwc->dev, "%s is of Isochronous type\n", dep->name);
1309 memset(¶ms, 0x00, sizeof(params));
1312 if (!protocol && ((dep->direction && dep->flags & DWC3_EP_BUSY) ||
1313 (!list_empty(&dep->req_queued) ||
1314 !list_empty(&dep->request_list)))) {
1315 dev_dbg(dwc->dev, "%s: pending request, cannot halt\n",
1320 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number,
1321 DWC3_DEPCMD_SETSTALL, ¶ms);
1323 dev_err(dwc->dev, "failed to set STALL on %s\n",
1326 dep->flags |= DWC3_EP_STALL;
1328 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number,
1329 DWC3_DEPCMD_CLEARSTALL, ¶ms);
1331 dev_err(dwc->dev, "failed to clear STALL on %s\n",
1334 dep->flags &= ~(DWC3_EP_STALL | DWC3_EP_WEDGE);
1340 static int dwc3_gadget_ep_set_halt(struct usb_ep *ep, int value)
1342 struct dwc3_ep *dep = to_dwc3_ep(ep);
1343 struct dwc3 *dwc = dep->dwc;
1345 unsigned long flags;
1349 spin_lock_irqsave(&dwc->lock, flags);
1350 ret = __dwc3_gadget_ep_set_halt(dep, value, false);
1351 spin_unlock_irqrestore(&dwc->lock, flags);
1356 static int dwc3_gadget_ep_set_wedge(struct usb_ep *ep)
1358 struct dwc3_ep *dep = to_dwc3_ep(ep);
1359 struct dwc3 *dwc = dep->dwc;
1360 unsigned long flags;
1363 spin_lock_irqsave(&dwc->lock, flags);
1364 dep->flags |= DWC3_EP_WEDGE;
1366 if (dep->number == 0 || dep->number == 1)
1367 ret = __dwc3_gadget_ep0_set_halt(ep, 1);
1369 ret = __dwc3_gadget_ep_set_halt(dep, 1, false);
1370 spin_unlock_irqrestore(&dwc->lock, flags);
1375 /* -------------------------------------------------------------------------- */
1377 static struct usb_endpoint_descriptor dwc3_gadget_ep0_desc = {
1378 .bLength = USB_DT_ENDPOINT_SIZE,
1379 .bDescriptorType = USB_DT_ENDPOINT,
1380 .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
1383 static const struct usb_ep_ops dwc3_gadget_ep0_ops = {
1384 .enable = dwc3_gadget_ep0_enable,
1385 .disable = dwc3_gadget_ep0_disable,
1386 .alloc_request = dwc3_gadget_ep_alloc_request,
1387 .free_request = dwc3_gadget_ep_free_request,
1388 .queue = dwc3_gadget_ep0_queue,
1389 .dequeue = dwc3_gadget_ep_dequeue,
1390 .set_halt = dwc3_gadget_ep0_set_halt,
1391 .set_wedge = dwc3_gadget_ep_set_wedge,
1394 static const struct usb_ep_ops dwc3_gadget_ep_ops = {
1395 .enable = dwc3_gadget_ep_enable,
1396 .disable = dwc3_gadget_ep_disable,
1397 .alloc_request = dwc3_gadget_ep_alloc_request,
1398 .free_request = dwc3_gadget_ep_free_request,
1399 .queue = dwc3_gadget_ep_queue,
1400 .dequeue = dwc3_gadget_ep_dequeue,
1401 .set_halt = dwc3_gadget_ep_set_halt,
1402 .set_wedge = dwc3_gadget_ep_set_wedge,
1405 /* -------------------------------------------------------------------------- */
1407 static int dwc3_gadget_get_frame(struct usb_gadget *g)
1409 struct dwc3 *dwc = gadget_to_dwc(g);
1412 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1413 return DWC3_DSTS_SOFFN(reg);
1416 static int dwc3_gadget_wakeup(struct usb_gadget *g)
1418 struct dwc3 *dwc = gadget_to_dwc(g);
1420 unsigned long timeout;
1421 unsigned long flags;
1430 spin_lock_irqsave(&dwc->lock, flags);
1433 * According to the Databook Remote wakeup request should
1434 * be issued only when the device is in early suspend state.
1436 * We can check that via USB Link State bits in DSTS register.
1438 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1440 speed = reg & DWC3_DSTS_CONNECTSPD;
1441 if (speed == DWC3_DSTS_SUPERSPEED) {
1442 dev_dbg(dwc->dev, "no wakeup on SuperSpeed\n");
1447 link_state = DWC3_DSTS_USBLNKST(reg);
1449 switch (link_state) {
1450 case DWC3_LINK_STATE_RX_DET: /* in HS, means Early Suspend */
1451 case DWC3_LINK_STATE_U3: /* in HS, means SUSPEND */
1454 dev_dbg(dwc->dev, "can't wakeup from link state %d\n",
1460 ret = dwc3_gadget_set_link_state(dwc, DWC3_LINK_STATE_RECOV);
1462 dev_err(dwc->dev, "failed to put link in Recovery\n");
1466 /* Recent versions do this automatically */
1467 if (dwc->revision < DWC3_REVISION_194A) {
1468 /* write zeroes to Link Change Request */
1469 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
1470 reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK;
1471 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1474 /* poll until Link State changes to ON */
1475 timeout = jiffies + msecs_to_jiffies(100);
1477 while (!time_after(jiffies, timeout)) {
1478 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1480 /* in HS, means ON */
1481 if (DWC3_DSTS_USBLNKST(reg) == DWC3_LINK_STATE_U0)
1485 if (DWC3_DSTS_USBLNKST(reg) != DWC3_LINK_STATE_U0) {
1486 dev_err(dwc->dev, "failed to send remote wakeup\n");
1491 spin_unlock_irqrestore(&dwc->lock, flags);
1496 static int dwc3_gadget_set_selfpowered(struct usb_gadget *g,
1499 struct dwc3 *dwc = gadget_to_dwc(g);
1500 unsigned long flags;
1502 spin_lock_irqsave(&dwc->lock, flags);
1503 g->is_selfpowered = !!is_selfpowered;
1504 spin_unlock_irqrestore(&dwc->lock, flags);
1509 static int dwc3_gadget_run_stop(struct dwc3 *dwc, int is_on, int suspend)
1514 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
1516 if (dwc->revision <= DWC3_REVISION_187A) {
1517 reg &= ~DWC3_DCTL_TRGTULST_MASK;
1518 reg |= DWC3_DCTL_TRGTULST_RX_DET;
1521 if (dwc->revision >= DWC3_REVISION_194A)
1522 reg &= ~DWC3_DCTL_KEEP_CONNECT;
1523 reg |= DWC3_DCTL_RUN_STOP;
1525 if (dwc->has_hibernation)
1526 reg |= DWC3_DCTL_KEEP_CONNECT;
1528 dwc->pullups_connected = true;
1530 reg &= ~DWC3_DCTL_RUN_STOP;
1532 if (dwc->has_hibernation && !suspend)
1533 reg &= ~DWC3_DCTL_KEEP_CONNECT;
1535 dwc->pullups_connected = false;
1538 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1541 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1543 if (!(reg & DWC3_DSTS_DEVCTRLHLT))
1546 if (reg & DWC3_DSTS_DEVCTRLHLT)
1555 dwc3_trace(trace_dwc3_gadget, "gadget %s data soft-%s",
1557 ? dwc->gadget_driver->function : "no-function",
1558 is_on ? "connect" : "disconnect");
1563 static int dwc3_gadget_pullup(struct usb_gadget *g, int is_on)
1565 struct dwc3 *dwc = gadget_to_dwc(g);
1566 unsigned long flags;
1571 spin_lock_irqsave(&dwc->lock, flags);
1572 ret = dwc3_gadget_run_stop(dwc, is_on, false);
1573 spin_unlock_irqrestore(&dwc->lock, flags);
1578 static void dwc3_gadget_enable_irq(struct dwc3 *dwc)
1582 /* Enable all but Start and End of Frame IRQs */
1583 reg = (DWC3_DEVTEN_VNDRDEVTSTRCVEDEN |
1584 DWC3_DEVTEN_EVNTOVERFLOWEN |
1585 DWC3_DEVTEN_CMDCMPLTEN |
1586 DWC3_DEVTEN_ERRTICERREN |
1587 DWC3_DEVTEN_WKUPEVTEN |
1588 DWC3_DEVTEN_ULSTCNGEN |
1589 DWC3_DEVTEN_CONNECTDONEEN |
1590 DWC3_DEVTEN_USBRSTEN |
1591 DWC3_DEVTEN_DISCONNEVTEN);
1593 dwc3_writel(dwc->regs, DWC3_DEVTEN, reg);
1596 static void dwc3_gadget_disable_irq(struct dwc3 *dwc)
1598 /* mask all interrupts */
1599 dwc3_writel(dwc->regs, DWC3_DEVTEN, 0x00);
1602 static irqreturn_t dwc3_interrupt(int irq, void *_dwc);
1603 static irqreturn_t dwc3_thread_interrupt(int irq, void *_dwc);
1605 static int dwc3_gadget_start(struct usb_gadget *g,
1606 struct usb_gadget_driver *driver)
1608 struct dwc3 *dwc = gadget_to_dwc(g);
1609 struct dwc3_ep *dep;
1610 unsigned long flags;
1615 irq = platform_get_irq(to_platform_device(dwc->dev), 0);
1616 ret = request_threaded_irq(irq, dwc3_interrupt, dwc3_thread_interrupt,
1617 IRQF_SHARED, "dwc3", dwc);
1619 dev_err(dwc->dev, "failed to request irq #%d --> %d\n",
1624 spin_lock_irqsave(&dwc->lock, flags);
1626 if (dwc->gadget_driver) {
1627 dev_err(dwc->dev, "%s is already bound to %s\n",
1629 dwc->gadget_driver->driver.name);
1634 dwc->gadget_driver = driver;
1636 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
1637 reg &= ~(DWC3_DCFG_SPEED_MASK);
1640 * WORKAROUND: DWC3 revision < 2.20a have an issue
1641 * which would cause metastability state on Run/Stop
1642 * bit if we try to force the IP to USB2-only mode.
1644 * Because of that, we cannot configure the IP to any
1645 * speed other than the SuperSpeed
1649 * STAR#9000525659: Clock Domain Crossing on DCTL in
1652 if (dwc->revision < DWC3_REVISION_220A) {
1653 reg |= DWC3_DCFG_SUPERSPEED;
1655 switch (dwc->maximum_speed) {
1657 reg |= DWC3_DSTS_LOWSPEED;
1659 case USB_SPEED_FULL:
1660 reg |= DWC3_DSTS_FULLSPEED1;
1662 case USB_SPEED_HIGH:
1663 reg |= DWC3_DSTS_HIGHSPEED;
1665 case USB_SPEED_SUPER: /* FALLTHROUGH */
1666 case USB_SPEED_UNKNOWN: /* FALTHROUGH */
1668 reg |= DWC3_DSTS_SUPERSPEED;
1671 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
1673 /* Start with SuperSpeed Default */
1674 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
1677 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, false,
1680 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
1685 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, false,
1688 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
1692 /* begin to receive SETUP packets */
1693 dwc->ep0state = EP0_SETUP_PHASE;
1694 dwc->link_state = DWC3_LINK_STATE_SS_DIS;
1695 dwc3_ep0_out_start(dwc);
1697 dwc3_gadget_enable_irq(dwc);
1699 spin_unlock_irqrestore(&dwc->lock, flags);
1704 __dwc3_gadget_ep_disable(dwc->eps[0]);
1707 dwc->gadget_driver = NULL;
1710 spin_unlock_irqrestore(&dwc->lock, flags);
1718 static int dwc3_gadget_stop(struct usb_gadget *g)
1720 struct dwc3 *dwc = gadget_to_dwc(g);
1721 unsigned long flags;
1724 spin_lock_irqsave(&dwc->lock, flags);
1726 dwc3_gadget_disable_irq(dwc);
1727 __dwc3_gadget_ep_disable(dwc->eps[0]);
1728 __dwc3_gadget_ep_disable(dwc->eps[1]);
1730 dwc->gadget_driver = NULL;
1732 spin_unlock_irqrestore(&dwc->lock, flags);
1734 irq = platform_get_irq(to_platform_device(dwc->dev), 0);
1740 static const struct usb_gadget_ops dwc3_gadget_ops = {
1741 .get_frame = dwc3_gadget_get_frame,
1742 .wakeup = dwc3_gadget_wakeup,
1743 .set_selfpowered = dwc3_gadget_set_selfpowered,
1744 .pullup = dwc3_gadget_pullup,
1745 .udc_start = dwc3_gadget_start,
1746 .udc_stop = dwc3_gadget_stop,
1749 /* -------------------------------------------------------------------------- */
1751 static int dwc3_gadget_init_hw_endpoints(struct dwc3 *dwc,
1752 u8 num, u32 direction)
1754 struct dwc3_ep *dep;
1757 for (i = 0; i < num; i++) {
1758 u8 epnum = (i << 1) | (!!direction);
1760 dep = kzalloc(sizeof(*dep), GFP_KERNEL);
1765 dep->number = epnum;
1766 dep->direction = !!direction;
1767 dwc->eps[epnum] = dep;
1769 snprintf(dep->name, sizeof(dep->name), "ep%d%s", epnum >> 1,
1770 (epnum & 1) ? "in" : "out");
1772 dep->endpoint.name = dep->name;
1774 dwc3_trace(trace_dwc3_gadget, "initializing %s", dep->name);
1776 if (epnum == 0 || epnum == 1) {
1777 usb_ep_set_maxpacket_limit(&dep->endpoint, 512);
1778 dep->endpoint.maxburst = 1;
1779 dep->endpoint.ops = &dwc3_gadget_ep0_ops;
1781 dwc->gadget.ep0 = &dep->endpoint;
1785 usb_ep_set_maxpacket_limit(&dep->endpoint, 1024);
1786 dep->endpoint.max_streams = 15;
1787 dep->endpoint.ops = &dwc3_gadget_ep_ops;
1788 list_add_tail(&dep->endpoint.ep_list,
1789 &dwc->gadget.ep_list);
1791 ret = dwc3_alloc_trb_pool(dep);
1796 if (epnum == 0 || epnum == 1) {
1797 dep->endpoint.caps.type_control = true;
1799 dep->endpoint.caps.type_iso = true;
1800 dep->endpoint.caps.type_bulk = true;
1801 dep->endpoint.caps.type_int = true;
1804 dep->endpoint.caps.dir_in = !!direction;
1805 dep->endpoint.caps.dir_out = !direction;
1807 INIT_LIST_HEAD(&dep->request_list);
1808 INIT_LIST_HEAD(&dep->req_queued);
1814 static int dwc3_gadget_init_endpoints(struct dwc3 *dwc)
1818 INIT_LIST_HEAD(&dwc->gadget.ep_list);
1820 ret = dwc3_gadget_init_hw_endpoints(dwc, dwc->num_out_eps, 0);
1822 dwc3_trace(trace_dwc3_gadget,
1823 "failed to allocate OUT endpoints");
1827 ret = dwc3_gadget_init_hw_endpoints(dwc, dwc->num_in_eps, 1);
1829 dwc3_trace(trace_dwc3_gadget,
1830 "failed to allocate IN endpoints");
1837 static void dwc3_gadget_free_endpoints(struct dwc3 *dwc)
1839 struct dwc3_ep *dep;
1842 for (epnum = 0; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
1843 dep = dwc->eps[epnum];
1847 * Physical endpoints 0 and 1 are special; they form the
1848 * bi-directional USB endpoint 0.
1850 * For those two physical endpoints, we don't allocate a TRB
1851 * pool nor do we add them the endpoints list. Due to that, we
1852 * shouldn't do these two operations otherwise we would end up
1853 * with all sorts of bugs when removing dwc3.ko.
1855 if (epnum != 0 && epnum != 1) {
1856 dwc3_free_trb_pool(dep);
1857 list_del(&dep->endpoint.ep_list);
1864 /* -------------------------------------------------------------------------- */
1866 static int __dwc3_cleanup_done_trbs(struct dwc3 *dwc, struct dwc3_ep *dep,
1867 struct dwc3_request *req, struct dwc3_trb *trb,
1868 const struct dwc3_event_depevt *event, int status)
1871 unsigned int s_pkt = 0;
1872 unsigned int trb_status;
1874 trace_dwc3_complete_trb(dep, trb);
1876 if ((trb->ctrl & DWC3_TRB_CTRL_HWO) && status != -ESHUTDOWN)
1878 * We continue despite the error. There is not much we
1879 * can do. If we don't clean it up we loop forever. If
1880 * we skip the TRB then it gets overwritten after a
1881 * while since we use them in a ring buffer. A BUG()
1882 * would help. Lets hope that if this occurs, someone
1883 * fixes the root cause instead of looking away :)
1885 dev_err(dwc->dev, "%s's TRB (%pK) still owned by HW\n",
1887 count = trb->size & DWC3_TRB_SIZE_MASK;
1889 if (dep->direction) {
1891 trb_status = DWC3_TRB_SIZE_TRBSTS(trb->size);
1892 if (trb_status == DWC3_TRBSTS_MISSED_ISOC) {
1893 dev_dbg(dwc->dev, "incomplete IN transfer %s\n",
1896 * If missed isoc occurred and there is
1897 * no request queued then issue END
1898 * TRANSFER, so that core generates
1899 * next xfernotready and we will issue
1900 * a fresh START TRANSFER.
1901 * If there are still queued request
1902 * then wait, do not issue either END
1903 * or UPDATE TRANSFER, just attach next
1904 * request in request_list during
1905 * giveback.If any future queued request
1906 * is successfully transferred then we
1907 * will issue UPDATE TRANSFER for all
1908 * request in the request_list.
1910 dep->flags |= DWC3_EP_MISSED_ISOC;
1912 dev_err(dwc->dev, "incomplete IN transfer %s\n",
1914 status = -ECONNRESET;
1917 dep->flags &= ~DWC3_EP_MISSED_ISOC;
1920 if (count && (event->status & DEPEVT_STATUS_SHORT))
1926 if ((event->status & DEPEVT_STATUS_LST) &&
1927 (trb->ctrl & (DWC3_TRB_CTRL_LST |
1928 DWC3_TRB_CTRL_HWO)))
1930 if ((event->status & DEPEVT_STATUS_IOC) &&
1931 (trb->ctrl & DWC3_TRB_CTRL_IOC))
1936 static int dwc3_cleanup_done_reqs(struct dwc3 *dwc, struct dwc3_ep *dep,
1937 const struct dwc3_event_depevt *event, int status)
1939 struct dwc3_request *req;
1940 struct dwc3_trb *trb;
1947 req = next_request(&dep->req_queued);
1954 slot = req->start_slot + i;
1955 if ((slot == DWC3_TRB_NUM - 1) &&
1956 usb_endpoint_xfer_isoc(dep->endpoint.desc))
1958 slot %= DWC3_TRB_NUM;
1959 trb = &dep->trb_pool[slot];
1960 count += trb->size & DWC3_TRB_SIZE_MASK;
1963 ret = __dwc3_cleanup_done_trbs(dwc, dep, req, trb,
1967 } while (++i < req->request.num_mapped_sgs);
1970 * We assume here we will always receive the entire data block
1971 * which we should receive. Meaning, if we program RX to
1972 * receive 4K but we receive only 2K, we assume that's all we
1973 * should receive and we simply bounce the request back to the
1974 * gadget driver for further processing.
1976 req->request.actual += req->request.length - count;
1977 dwc3_gadget_giveback(dep, req, status);
1983 if (usb_endpoint_xfer_isoc(dep->endpoint.desc) &&
1984 list_empty(&dep->req_queued)) {
1985 if (list_empty(&dep->request_list)) {
1987 * If there is no entry in request list then do
1988 * not issue END TRANSFER now. Just set PENDING
1989 * flag, so that END TRANSFER is issued when an
1990 * entry is added into request list.
1992 dep->flags = DWC3_EP_PENDING_REQUEST;
1994 dwc3_stop_active_transfer(dwc, dep->number, true);
1995 dep->flags = DWC3_EP_ENABLED;
2000 if (usb_endpoint_xfer_isoc(dep->endpoint.desc))
2001 if ((event->status & DEPEVT_STATUS_IOC) &&
2002 (trb->ctrl & DWC3_TRB_CTRL_IOC))
2007 static void dwc3_endpoint_transfer_complete(struct dwc3 *dwc,
2008 struct dwc3_ep *dep, const struct dwc3_event_depevt *event)
2010 unsigned status = 0;
2012 u32 is_xfer_complete;
2014 is_xfer_complete = (event->endpoint_event == DWC3_DEPEVT_XFERCOMPLETE);
2016 if (event->status & DEPEVT_STATUS_BUSERR)
2017 status = -ECONNRESET;
2019 clean_busy = dwc3_cleanup_done_reqs(dwc, dep, event, status);
2020 if (clean_busy && (is_xfer_complete ||
2021 usb_endpoint_xfer_isoc(dep->endpoint.desc)))
2022 dep->flags &= ~DWC3_EP_BUSY;
2025 * WORKAROUND: This is the 2nd half of U1/U2 -> U0 workaround.
2026 * See dwc3_gadget_linksts_change_interrupt() for 1st half.
2028 if (dwc->revision < DWC3_REVISION_183A) {
2032 for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) {
2035 if (!(dep->flags & DWC3_EP_ENABLED))
2038 if (!list_empty(&dep->req_queued))
2042 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2044 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2049 if (!usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
2052 ret = __dwc3_gadget_kick_transfer(dep, 0, is_xfer_complete);
2053 if (!ret || ret == -EBUSY)
2058 static void dwc3_endpoint_interrupt(struct dwc3 *dwc,
2059 const struct dwc3_event_depevt *event)
2061 struct dwc3_ep *dep;
2062 u8 epnum = event->endpoint_number;
2064 dep = dwc->eps[epnum];
2066 if (!(dep->flags & DWC3_EP_ENABLED))
2069 if (epnum == 0 || epnum == 1) {
2070 dwc3_ep0_interrupt(dwc, event);
2074 switch (event->endpoint_event) {
2075 case DWC3_DEPEVT_XFERCOMPLETE:
2076 dep->resource_index = 0;
2078 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
2079 dev_dbg(dwc->dev, "%s is an Isochronous endpoint\n",
2084 dwc3_endpoint_transfer_complete(dwc, dep, event);
2086 case DWC3_DEPEVT_XFERINPROGRESS:
2087 dwc3_endpoint_transfer_complete(dwc, dep, event);
2089 case DWC3_DEPEVT_XFERNOTREADY:
2090 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
2091 dwc3_gadget_start_isoc(dwc, dep, event);
2096 active = event->status & DEPEVT_STATUS_TRANSFER_ACTIVE;
2098 dwc3_trace(trace_dwc3_gadget, "%s: reason %s",
2099 dep->name, active ? "Transfer Active"
2100 : "Transfer Not Active");
2102 ret = __dwc3_gadget_kick_transfer(dep, 0, !active);
2103 if (!ret || ret == -EBUSY)
2106 dev_dbg(dwc->dev, "%s: failed to kick transfers\n",
2111 case DWC3_DEPEVT_STREAMEVT:
2112 if (!usb_endpoint_xfer_bulk(dep->endpoint.desc)) {
2113 dev_err(dwc->dev, "Stream event for non-Bulk %s\n",
2118 switch (event->status) {
2119 case DEPEVT_STREAMEVT_FOUND:
2120 dwc3_trace(trace_dwc3_gadget,
2121 "Stream %d found and started",
2125 case DEPEVT_STREAMEVT_NOTFOUND:
2128 dev_dbg(dwc->dev, "Couldn't find suitable stream\n");
2131 case DWC3_DEPEVT_RXTXFIFOEVT:
2132 dev_dbg(dwc->dev, "%s FIFO Overrun\n", dep->name);
2134 case DWC3_DEPEVT_EPCMDCMPLT:
2135 dwc3_trace(trace_dwc3_gadget, "Endpoint Command Complete");
2140 static void dwc3_disconnect_gadget(struct dwc3 *dwc)
2142 if (dwc->gadget_driver && dwc->gadget_driver->disconnect) {
2143 spin_unlock(&dwc->lock);
2144 dwc->gadget_driver->disconnect(&dwc->gadget);
2145 spin_lock(&dwc->lock);
2149 static void dwc3_suspend_gadget(struct dwc3 *dwc)
2151 if (dwc->gadget_driver && dwc->gadget_driver->suspend) {
2152 spin_unlock(&dwc->lock);
2153 dwc->gadget_driver->suspend(&dwc->gadget);
2154 spin_lock(&dwc->lock);
2158 static void dwc3_resume_gadget(struct dwc3 *dwc)
2160 if (dwc->gadget_driver && dwc->gadget_driver->resume) {
2161 spin_unlock(&dwc->lock);
2162 dwc->gadget_driver->resume(&dwc->gadget);
2163 spin_lock(&dwc->lock);
2167 static void dwc3_reset_gadget(struct dwc3 *dwc)
2169 if (!dwc->gadget_driver)
2172 if (dwc->gadget.speed != USB_SPEED_UNKNOWN) {
2173 spin_unlock(&dwc->lock);
2174 usb_gadget_udc_reset(&dwc->gadget, dwc->gadget_driver);
2175 spin_lock(&dwc->lock);
2179 static void dwc3_stop_active_transfer(struct dwc3 *dwc, u32 epnum, bool force)
2181 struct dwc3_ep *dep;
2182 struct dwc3_gadget_ep_cmd_params params;
2186 dep = dwc->eps[epnum];
2188 if (!dep->resource_index)
2192 * NOTICE: We are violating what the Databook says about the
2193 * EndTransfer command. Ideally we would _always_ wait for the
2194 * EndTransfer Command Completion IRQ, but that's causing too
2195 * much trouble synchronizing between us and gadget driver.
2197 * We have discussed this with the IP Provider and it was
2198 * suggested to giveback all requests here, but give HW some
2199 * extra time to synchronize with the interconnect. We're using
2200 * an arbitrary 100us delay for that.
2202 * Note also that a similar handling was tested by Synopsys
2203 * (thanks a lot Paul) and nothing bad has come out of it.
2204 * In short, what we're doing is:
2206 * - Issue EndTransfer WITH CMDIOC bit set
2210 cmd = DWC3_DEPCMD_ENDTRANSFER;
2211 cmd |= force ? DWC3_DEPCMD_HIPRI_FORCERM : 0;
2212 cmd |= DWC3_DEPCMD_CMDIOC;
2213 cmd |= DWC3_DEPCMD_PARAM(dep->resource_index);
2214 memset(¶ms, 0, sizeof(params));
2215 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number, cmd, ¶ms);
2217 dep->resource_index = 0;
2218 dep->flags &= ~DWC3_EP_BUSY;
2222 static void dwc3_stop_active_transfers(struct dwc3 *dwc)
2226 for (epnum = 2; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
2227 struct dwc3_ep *dep;
2229 dep = dwc->eps[epnum];
2233 if (!(dep->flags & DWC3_EP_ENABLED))
2236 dwc3_remove_requests(dwc, dep);
2240 static void dwc3_clear_stall_all_ep(struct dwc3 *dwc)
2244 for (epnum = 1; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
2245 struct dwc3_ep *dep;
2246 struct dwc3_gadget_ep_cmd_params params;
2249 dep = dwc->eps[epnum];
2253 if (!(dep->flags & DWC3_EP_STALL))
2256 dep->flags &= ~DWC3_EP_STALL;
2258 memset(¶ms, 0, sizeof(params));
2259 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number,
2260 DWC3_DEPCMD_CLEARSTALL, ¶ms);
2265 static void dwc3_gadget_disconnect_interrupt(struct dwc3 *dwc)
2269 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2270 reg &= ~DWC3_DCTL_INITU1ENA;
2271 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2273 reg &= ~DWC3_DCTL_INITU2ENA;
2274 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2276 dwc3_disconnect_gadget(dwc);
2278 dwc->gadget.speed = USB_SPEED_UNKNOWN;
2279 dwc->setup_packet_pending = false;
2280 usb_gadget_set_state(&dwc->gadget, USB_STATE_NOTATTACHED);
2283 static void dwc3_gadget_reset_interrupt(struct dwc3 *dwc)
2288 * WORKAROUND: DWC3 revisions <1.88a have an issue which
2289 * would cause a missing Disconnect Event if there's a
2290 * pending Setup Packet in the FIFO.
2292 * There's no suggested workaround on the official Bug
2293 * report, which states that "unless the driver/application
2294 * is doing any special handling of a disconnect event,
2295 * there is no functional issue".
2297 * Unfortunately, it turns out that we _do_ some special
2298 * handling of a disconnect event, namely complete all
2299 * pending transfers, notify gadget driver of the
2300 * disconnection, and so on.
2302 * Our suggested workaround is to follow the Disconnect
2303 * Event steps here, instead, based on a setup_packet_pending
2304 * flag. Such flag gets set whenever we have a XferNotReady
2305 * event on EP0 and gets cleared on XferComplete for the
2310 * STAR#9000466709: RTL: Device : Disconnect event not
2311 * generated if setup packet pending in FIFO
2313 if (dwc->revision < DWC3_REVISION_188A) {
2314 if (dwc->setup_packet_pending)
2315 dwc3_gadget_disconnect_interrupt(dwc);
2318 dwc3_reset_gadget(dwc);
2320 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2321 reg &= ~DWC3_DCTL_TSTCTRL_MASK;
2322 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2323 dwc->test_mode = false;
2325 dwc3_stop_active_transfers(dwc);
2326 dwc3_clear_stall_all_ep(dwc);
2328 /* Reset device address to zero */
2329 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2330 reg &= ~(DWC3_DCFG_DEVADDR_MASK);
2331 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
2334 static void dwc3_update_ram_clk_sel(struct dwc3 *dwc, u32 speed)
2337 u32 usb30_clock = DWC3_GCTL_CLK_BUS;
2340 * We change the clock only at SS but I dunno why I would want to do
2341 * this. Maybe it becomes part of the power saving plan.
2344 if (speed != DWC3_DSTS_SUPERSPEED)
2348 * RAMClkSel is reset to 0 after USB reset, so it must be reprogrammed
2349 * each time on Connect Done.
2354 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
2355 reg |= DWC3_GCTL_RAMCLKSEL(usb30_clock);
2356 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
2359 static void dwc3_gadget_conndone_interrupt(struct dwc3 *dwc)
2361 struct dwc3_ep *dep;
2366 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
2367 speed = reg & DWC3_DSTS_CONNECTSPD;
2370 dwc3_update_ram_clk_sel(dwc, speed);
2373 case DWC3_DCFG_SUPERSPEED:
2375 * WORKAROUND: DWC3 revisions <1.90a have an issue which
2376 * would cause a missing USB3 Reset event.
2378 * In such situations, we should force a USB3 Reset
2379 * event by calling our dwc3_gadget_reset_interrupt()
2384 * STAR#9000483510: RTL: SS : USB3 reset event may
2385 * not be generated always when the link enters poll
2387 if (dwc->revision < DWC3_REVISION_190A)
2388 dwc3_gadget_reset_interrupt(dwc);
2390 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
2391 dwc->gadget.ep0->maxpacket = 512;
2392 dwc->gadget.speed = USB_SPEED_SUPER;
2394 case DWC3_DCFG_HIGHSPEED:
2395 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
2396 dwc->gadget.ep0->maxpacket = 64;
2397 dwc->gadget.speed = USB_SPEED_HIGH;
2399 case DWC3_DCFG_FULLSPEED2:
2400 case DWC3_DCFG_FULLSPEED1:
2401 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
2402 dwc->gadget.ep0->maxpacket = 64;
2403 dwc->gadget.speed = USB_SPEED_FULL;
2405 case DWC3_DCFG_LOWSPEED:
2406 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(8);
2407 dwc->gadget.ep0->maxpacket = 8;
2408 dwc->gadget.speed = USB_SPEED_LOW;
2412 dwc->eps[1]->endpoint.maxpacket = dwc->gadget.ep0->maxpacket;
2414 /* Enable USB2 LPM Capability */
2416 if ((dwc->revision > DWC3_REVISION_194A)
2417 && (speed != DWC3_DCFG_SUPERSPEED)) {
2418 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2419 reg |= DWC3_DCFG_LPM_CAP;
2420 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
2422 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2423 reg &= ~(DWC3_DCTL_HIRD_THRES_MASK | DWC3_DCTL_L1_HIBER_EN);
2425 reg |= DWC3_DCTL_HIRD_THRES(dwc->hird_threshold);
2428 * When dwc3 revisions >= 2.40a, LPM Erratum is enabled and
2429 * DCFG.LPMCap is set, core responses with an ACK and the
2430 * BESL value in the LPM token is less than or equal to LPM
2433 WARN_ONCE(dwc->revision < DWC3_REVISION_240A
2434 && dwc->has_lpm_erratum,
2435 "LPM Erratum not available on dwc3 revisisions < 2.40a\n");
2437 if (dwc->has_lpm_erratum && dwc->revision >= DWC3_REVISION_240A)
2438 reg |= DWC3_DCTL_LPM_ERRATA(dwc->lpm_nyet_threshold);
2440 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2442 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2443 reg &= ~DWC3_DCTL_HIRD_THRES_MASK;
2444 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2448 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, true,
2451 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
2456 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, true,
2459 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
2464 * Configure PHY via GUSB3PIPECTLn if required.
2466 * Update GTXFIFOSIZn
2468 * In both cases reset values should be sufficient.
2472 static void dwc3_gadget_wakeup_interrupt(struct dwc3 *dwc)
2475 * TODO take core out of low power mode when that's
2479 dwc->gadget_driver->resume(&dwc->gadget);
2482 static void dwc3_gadget_linksts_change_interrupt(struct dwc3 *dwc,
2483 unsigned int evtinfo)
2485 enum dwc3_link_state next = evtinfo & DWC3_LINK_STATE_MASK;
2486 unsigned int pwropt;
2489 * WORKAROUND: DWC3 < 2.50a have an issue when configured without
2490 * Hibernation mode enabled which would show up when device detects
2491 * host-initiated U3 exit.
2493 * In that case, device will generate a Link State Change Interrupt
2494 * from U3 to RESUME which is only necessary if Hibernation is
2497 * There are no functional changes due to such spurious event and we
2498 * just need to ignore it.
2502 * STAR#9000570034 RTL: SS Resume event generated in non-Hibernation
2505 pwropt = DWC3_GHWPARAMS1_EN_PWROPT(dwc->hwparams.hwparams1);
2506 if ((dwc->revision < DWC3_REVISION_250A) &&
2507 (pwropt != DWC3_GHWPARAMS1_EN_PWROPT_HIB)) {
2508 if ((dwc->link_state == DWC3_LINK_STATE_U3) &&
2509 (next == DWC3_LINK_STATE_RESUME)) {
2510 dwc3_trace(trace_dwc3_gadget,
2511 "ignoring transition U3 -> Resume");
2517 * WORKAROUND: DWC3 Revisions <1.83a have an issue which, depending
2518 * on the link partner, the USB session might do multiple entry/exit
2519 * of low power states before a transfer takes place.
2521 * Due to this problem, we might experience lower throughput. The
2522 * suggested workaround is to disable DCTL[12:9] bits if we're
2523 * transitioning from U1/U2 to U0 and enable those bits again
2524 * after a transfer completes and there are no pending transfers
2525 * on any of the enabled endpoints.
2527 * This is the first half of that workaround.
2531 * STAR#9000446952: RTL: Device SS : if U1/U2 ->U0 takes >128us
2532 * core send LGO_Ux entering U0
2534 if (dwc->revision < DWC3_REVISION_183A) {
2535 if (next == DWC3_LINK_STATE_U0) {
2539 switch (dwc->link_state) {
2540 case DWC3_LINK_STATE_U1:
2541 case DWC3_LINK_STATE_U2:
2542 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2543 u1u2 = reg & (DWC3_DCTL_INITU2ENA
2544 | DWC3_DCTL_ACCEPTU2ENA
2545 | DWC3_DCTL_INITU1ENA
2546 | DWC3_DCTL_ACCEPTU1ENA);
2549 dwc->u1u2 = reg & u1u2;
2553 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2563 case DWC3_LINK_STATE_U1:
2564 if (dwc->speed == USB_SPEED_SUPER)
2565 dwc3_suspend_gadget(dwc);
2567 case DWC3_LINK_STATE_U2:
2568 case DWC3_LINK_STATE_U3:
2569 dwc3_suspend_gadget(dwc);
2571 case DWC3_LINK_STATE_RESUME:
2572 dwc3_resume_gadget(dwc);
2579 dwc->link_state = next;
2582 static void dwc3_gadget_hibernation_interrupt(struct dwc3 *dwc,
2583 unsigned int evtinfo)
2585 unsigned int is_ss = evtinfo & BIT(4);
2588 * WORKAROUND: DWC3 revison 2.20a with hibernation support
2589 * have a known issue which can cause USB CV TD.9.23 to fail
2592 * Because of this issue, core could generate bogus hibernation
2593 * events which SW needs to ignore.
2597 * STAR#9000546576: Device Mode Hibernation: Issue in USB 2.0
2598 * Device Fallback from SuperSpeed
2600 if (is_ss ^ (dwc->speed == USB_SPEED_SUPER))
2603 /* enter hibernation here */
2606 static void dwc3_gadget_interrupt(struct dwc3 *dwc,
2607 const struct dwc3_event_devt *event)
2609 switch (event->type) {
2610 case DWC3_DEVICE_EVENT_DISCONNECT:
2611 dwc3_gadget_disconnect_interrupt(dwc);
2613 case DWC3_DEVICE_EVENT_RESET:
2614 dwc3_gadget_reset_interrupt(dwc);
2616 case DWC3_DEVICE_EVENT_CONNECT_DONE:
2617 dwc3_gadget_conndone_interrupt(dwc);
2619 case DWC3_DEVICE_EVENT_WAKEUP:
2620 dwc3_gadget_wakeup_interrupt(dwc);
2622 case DWC3_DEVICE_EVENT_HIBER_REQ:
2623 if (dev_WARN_ONCE(dwc->dev, !dwc->has_hibernation,
2624 "unexpected hibernation event\n"))
2627 dwc3_gadget_hibernation_interrupt(dwc, event->event_info);
2629 case DWC3_DEVICE_EVENT_LINK_STATUS_CHANGE:
2630 dwc3_gadget_linksts_change_interrupt(dwc, event->event_info);
2632 case DWC3_DEVICE_EVENT_EOPF:
2633 dwc3_trace(trace_dwc3_gadget, "End of Periodic Frame");
2635 case DWC3_DEVICE_EVENT_SOF:
2636 dwc3_trace(trace_dwc3_gadget, "Start of Periodic Frame");
2638 case DWC3_DEVICE_EVENT_ERRATIC_ERROR:
2639 dwc3_trace(trace_dwc3_gadget, "Erratic Error");
2641 case DWC3_DEVICE_EVENT_CMD_CMPL:
2642 dwc3_trace(trace_dwc3_gadget, "Command Complete");
2644 case DWC3_DEVICE_EVENT_OVERFLOW:
2645 dwc3_trace(trace_dwc3_gadget, "Overflow");
2648 dev_WARN(dwc->dev, "UNKNOWN IRQ %d\n", event->type);
2652 static void dwc3_process_event_entry(struct dwc3 *dwc,
2653 const union dwc3_event *event)
2655 trace_dwc3_event(event->raw);
2657 /* Endpoint IRQ, handle it and return early */
2658 if (event->type.is_devspec == 0) {
2660 return dwc3_endpoint_interrupt(dwc, &event->depevt);
2663 switch (event->type.type) {
2664 case DWC3_EVENT_TYPE_DEV:
2665 dwc3_gadget_interrupt(dwc, &event->devt);
2667 /* REVISIT what to do with Carkit and I2C events ? */
2669 dev_err(dwc->dev, "UNKNOWN IRQ type %d\n", event->raw);
2673 static irqreturn_t dwc3_process_event_buf(struct dwc3 *dwc, u32 buf)
2675 struct dwc3_event_buffer *evt;
2676 irqreturn_t ret = IRQ_NONE;
2680 evt = dwc->ev_buffs[buf];
2683 if (!(evt->flags & DWC3_EVENT_PENDING))
2687 union dwc3_event event;
2689 event.raw = *(u32 *) (evt->buf + evt->lpos);
2691 dwc3_process_event_entry(dwc, &event);
2694 * FIXME we wrap around correctly to the next entry as
2695 * almost all entries are 4 bytes in size. There is one
2696 * entry which has 12 bytes which is a regular entry
2697 * followed by 8 bytes data. ATM I don't know how
2698 * things are organized if we get next to the a
2699 * boundary so I worry about that once we try to handle
2702 evt->lpos = (evt->lpos + 4) % DWC3_EVENT_BUFFERS_SIZE;
2705 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(buf), 4);
2709 evt->flags &= ~DWC3_EVENT_PENDING;
2712 /* Unmask interrupt */
2713 reg = dwc3_readl(dwc->regs, DWC3_GEVNTSIZ(buf));
2714 reg &= ~DWC3_GEVNTSIZ_INTMASK;
2715 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(buf), reg);
2720 static irqreturn_t dwc3_thread_interrupt(int irq, void *_dwc)
2722 struct dwc3 *dwc = _dwc;
2723 unsigned long flags;
2724 irqreturn_t ret = IRQ_NONE;
2727 spin_lock_irqsave(&dwc->lock, flags);
2729 for (i = 0; i < dwc->num_event_buffers; i++)
2730 ret |= dwc3_process_event_buf(dwc, i);
2732 spin_unlock_irqrestore(&dwc->lock, flags);
2737 static irqreturn_t dwc3_check_event_buf(struct dwc3 *dwc, u32 buf)
2739 struct dwc3_event_buffer *evt;
2743 evt = dwc->ev_buffs[buf];
2745 count = dwc3_readl(dwc->regs, DWC3_GEVNTCOUNT(buf));
2746 count &= DWC3_GEVNTCOUNT_MASK;
2751 evt->flags |= DWC3_EVENT_PENDING;
2753 /* Mask interrupt */
2754 reg = dwc3_readl(dwc->regs, DWC3_GEVNTSIZ(buf));
2755 reg |= DWC3_GEVNTSIZ_INTMASK;
2756 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(buf), reg);
2758 return IRQ_WAKE_THREAD;
2761 static irqreturn_t dwc3_interrupt(int irq, void *_dwc)
2763 struct dwc3 *dwc = _dwc;
2765 irqreturn_t ret = IRQ_NONE;
2767 for (i = 0; i < dwc->num_event_buffers; i++) {
2770 status = dwc3_check_event_buf(dwc, i);
2771 if (status == IRQ_WAKE_THREAD)
2779 * dwc3_gadget_init - Initializes gadget related registers
2780 * @dwc: pointer to our controller context structure
2782 * Returns 0 on success otherwise negative errno.
2784 int dwc3_gadget_init(struct dwc3 *dwc)
2788 dwc->ctrl_req = dma_alloc_coherent(dwc->dev, sizeof(*dwc->ctrl_req),
2789 &dwc->ctrl_req_addr, GFP_KERNEL);
2790 if (!dwc->ctrl_req) {
2791 dev_err(dwc->dev, "failed to allocate ctrl request\n");
2796 dwc->ep0_trb = dma_alloc_coherent(dwc->dev, sizeof(*dwc->ep0_trb) * 2,
2797 &dwc->ep0_trb_addr, GFP_KERNEL);
2798 if (!dwc->ep0_trb) {
2799 dev_err(dwc->dev, "failed to allocate ep0 trb\n");
2804 dwc->setup_buf = kzalloc(DWC3_EP0_BOUNCE_SIZE, GFP_KERNEL);
2805 if (!dwc->setup_buf) {
2810 dwc->ep0_bounce = dma_alloc_coherent(dwc->dev,
2811 DWC3_EP0_BOUNCE_SIZE, &dwc->ep0_bounce_addr,
2813 if (!dwc->ep0_bounce) {
2814 dev_err(dwc->dev, "failed to allocate ep0 bounce buffer\n");
2819 dwc->gadget.ops = &dwc3_gadget_ops;
2820 dwc->gadget.speed = USB_SPEED_UNKNOWN;
2821 dwc->gadget.sg_supported = true;
2822 dwc->gadget.name = "dwc3-gadget";
2825 * FIXME We might be setting max_speed to <SUPER, however versions
2826 * <2.20a of dwc3 have an issue with metastability (documented
2827 * elsewhere in this driver) which tells us we can't set max speed to
2828 * anything lower than SUPER.
2830 * Because gadget.max_speed is only used by composite.c and function
2831 * drivers (i.e. it won't go into dwc3's registers) we are allowing this
2832 * to happen so we avoid sending SuperSpeed Capability descriptor
2833 * together with our BOS descriptor as that could confuse host into
2834 * thinking we can handle super speed.
2836 * Note that, in fact, we won't even support GetBOS requests when speed
2837 * is less than super speed because we don't have means, yet, to tell
2838 * composite.c that we are USB 2.0 + LPM ECN.
2840 if (dwc->revision < DWC3_REVISION_220A)
2841 dwc3_trace(trace_dwc3_gadget,
2842 "Changing max_speed on rev %08x\n",
2845 dwc->gadget.max_speed = dwc->maximum_speed;
2848 * Per databook, DWC3 needs buffer size to be aligned to MaxPacketSize
2851 dwc->gadget.quirk_ep_out_aligned_size = true;
2854 * REVISIT: Here we should clear all pending IRQs to be
2855 * sure we're starting from a well known location.
2858 ret = dwc3_gadget_init_endpoints(dwc);
2862 ret = usb_add_gadget_udc(dwc->dev, &dwc->gadget);
2864 dev_err(dwc->dev, "failed to register udc\n");
2871 dwc3_gadget_free_endpoints(dwc);
2872 dma_free_coherent(dwc->dev, DWC3_EP0_BOUNCE_SIZE,
2873 dwc->ep0_bounce, dwc->ep0_bounce_addr);
2876 kfree(dwc->setup_buf);
2879 dma_free_coherent(dwc->dev, sizeof(*dwc->ep0_trb) * 2,
2880 dwc->ep0_trb, dwc->ep0_trb_addr);
2883 dma_free_coherent(dwc->dev, sizeof(*dwc->ctrl_req),
2884 dwc->ctrl_req, dwc->ctrl_req_addr);
2890 /* -------------------------------------------------------------------------- */
2892 void dwc3_gadget_exit(struct dwc3 *dwc)
2894 usb_del_gadget_udc(&dwc->gadget);
2896 dwc3_gadget_free_endpoints(dwc);
2898 dma_free_coherent(dwc->dev, DWC3_EP0_BOUNCE_SIZE,
2899 dwc->ep0_bounce, dwc->ep0_bounce_addr);
2901 kfree(dwc->setup_buf);
2903 dma_free_coherent(dwc->dev, sizeof(*dwc->ep0_trb) * 2,
2904 dwc->ep0_trb, dwc->ep0_trb_addr);
2906 dma_free_coherent(dwc->dev, sizeof(*dwc->ctrl_req),
2907 dwc->ctrl_req, dwc->ctrl_req_addr);
2910 int dwc3_gadget_suspend(struct dwc3 *dwc)
2912 if (!dwc->gadget_driver)
2915 if (dwc->pullups_connected) {
2916 dwc3_gadget_disable_irq(dwc);
2917 dwc3_gadget_run_stop(dwc, true, true);
2920 __dwc3_gadget_ep_disable(dwc->eps[0]);
2921 __dwc3_gadget_ep_disable(dwc->eps[1]);
2923 dwc->dcfg = dwc3_readl(dwc->regs, DWC3_DCFG);
2928 int dwc3_gadget_resume(struct dwc3 *dwc)
2930 struct dwc3_ep *dep;
2933 if (!dwc->gadget_driver)
2936 /* Start with SuperSpeed Default */
2937 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
2940 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, false,
2946 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, false,
2951 /* begin to receive SETUP packets */
2952 dwc->ep0state = EP0_SETUP_PHASE;
2953 dwc3_ep0_out_start(dwc);
2955 dwc3_writel(dwc->regs, DWC3_DCFG, dwc->dcfg);
2957 if (dwc->pullups_connected) {
2958 dwc3_gadget_enable_irq(dwc);
2959 dwc3_gadget_run_stop(dwc, true, false);
2965 __dwc3_gadget_ep_disable(dwc->eps[0]);