1 // SPDX-License-Identifier: GPL-2.0
3 * gadget.c - DesignWare USB3 DRD Controller Gadget Framework Link
5 * Copyright (C) 2015 Texas Instruments Incorporated - http://www.ti.com
7 * Authors: Felipe Balbi <balbi@ti.com>,
8 * Sebastian Andrzej Siewior <bigeasy@linutronix.de>
10 * Taken from Linux Kernel v3.19-rc1 (drivers/usb/dwc3/gadget.c) and ported
13 * commit 8e74475b0e : usb: dwc3: gadget: use udc-core's reset notifier
20 #include <dm/device_compat.h>
21 #include <dm/devres.h>
22 #include <linux/bug.h>
23 #include <linux/dma-mapping.h>
24 #include <linux/list.h>
26 #include <linux/usb/ch9.h>
27 #include <linux/usb/gadget.h>
33 #include "linux-compat.h"
36 * dwc3_gadget_set_test_mode - Enables USB2 Test Modes
37 * @dwc: pointer to our context structure
38 * @mode: the mode to set (J, K SE0 NAK, Force Enable)
40 * Caller should take care of locking. This function will
41 * return 0 on success or -EINVAL if wrong Test Selector
44 int dwc3_gadget_set_test_mode(struct dwc3 *dwc, int mode)
48 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
49 reg &= ~DWC3_DCTL_TSTCTRL_MASK;
63 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
69 * dwc3_gadget_get_link_state - Gets current state of USB Link
70 * @dwc: pointer to our context structure
72 * Caller should take care of locking. This function will
73 * return the link state on success (>= 0) or -ETIMEDOUT.
75 int dwc3_gadget_get_link_state(struct dwc3 *dwc)
79 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
81 return DWC3_DSTS_USBLNKST(reg);
85 * dwc3_gadget_set_link_state - Sets USB Link to a particular State
86 * @dwc: pointer to our context structure
87 * @state: the state to put link into
89 * Caller should take care of locking. This function will
90 * return 0 on success or -ETIMEDOUT.
92 int dwc3_gadget_set_link_state(struct dwc3 *dwc, enum dwc3_link_state state)
98 * Wait until device controller is ready. Only applies to 1.94a and
101 if (dwc->revision >= DWC3_REVISION_194A) {
103 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
104 if (reg & DWC3_DSTS_DCNRD)
114 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
115 reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK;
117 /* set requested state */
118 reg |= DWC3_DCTL_ULSTCHNGREQ(state);
119 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
122 * The following code is racy when called from dwc3_gadget_wakeup,
123 * and is not needed, at least on newer versions
125 if (dwc->revision >= DWC3_REVISION_194A)
128 /* wait for a change in DSTS */
131 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
133 if (DWC3_DSTS_USBLNKST(reg) == state)
139 dev_vdbg(dwc->dev, "link state change request timed out\n");
145 * dwc3_gadget_resize_tx_fifos - reallocate fifo spaces for current use-case
146 * @dwc: pointer to our context structure
148 * This function will a best effort FIFO allocation in order
149 * to improve FIFO usage and throughput, while still allowing
150 * us to enable as many endpoints as possible.
152 * Keep in mind that this operation will be highly dependent
153 * on the configured size for RAM1 - which contains TxFifo -,
154 * the amount of endpoints enabled on coreConsultant tool, and
155 * the width of the Master Bus.
157 * In the ideal world, we would always be able to satisfy the
158 * following equation:
160 * ((512 + 2 * MDWIDTH-Bytes) + (Number of IN Endpoints - 1) * \
161 * (3 * (1024 + MDWIDTH-Bytes) + MDWIDTH-Bytes)) / MDWIDTH-Bytes
163 * Unfortunately, due to many variables that's not always the case.
165 int dwc3_gadget_resize_tx_fifos(struct dwc3 *dwc)
167 int last_fifo_depth = 0;
172 if (!dwc->needs_fifo_resize)
175 mdwidth = DWC3_MDWIDTH(dwc->hwparams.hwparams0);
177 /* MDWIDTH is represented in bits, we need it in bytes */
181 * FIXME For now we will only allocate 1 wMaxPacketSize space
182 * for each enabled endpoint, later patches will come to
183 * improve this algorithm so that we better use the internal
186 for (num = 0; num < dwc->num_in_eps; num++) {
187 /* bit0 indicates direction; 1 means IN ep */
188 struct dwc3_ep *dep = dwc->eps[(num << 1) | 1];
192 if (!(dep->flags & DWC3_EP_ENABLED))
195 if (usb_endpoint_xfer_bulk(dep->endpoint.desc)
196 || usb_endpoint_xfer_isoc(dep->endpoint.desc))
200 * REVISIT: the following assumes we will always have enough
201 * space available on the FIFO RAM for all possible use cases.
202 * Make sure that's true somehow and change FIFO allocation
205 * If we have Bulk or Isochronous endpoints, we want
206 * them to be able to be very, very fast. So we're giving
207 * those endpoints a fifo_size which is enough for 3 full
210 tmp = mult * (dep->endpoint.maxpacket + mdwidth);
213 fifo_size = DIV_ROUND_UP(tmp, mdwidth);
215 fifo_size |= (last_fifo_depth << 16);
217 dev_vdbg(dwc->dev, "%s: Fifo Addr %04x Size %d\n",
218 dep->name, last_fifo_depth, fifo_size & 0xffff);
220 dwc3_writel(dwc->regs, DWC3_GTXFIFOSIZ(num), fifo_size);
222 last_fifo_depth += (fifo_size & 0xffff);
228 void dwc3_gadget_giveback(struct dwc3_ep *dep, struct dwc3_request *req,
231 struct dwc3 *dwc = dep->dwc;
236 * Skip LINK TRB. We can't use req->trb and check for
237 * DWC3_TRBCTL_LINK_TRB because it points the TRB we
238 * just completed (not the LINK TRB).
240 if (((dep->busy_slot & DWC3_TRB_MASK) ==
242 usb_endpoint_xfer_isoc(dep->endpoint.desc))
247 list_del(&req->list);
249 if (req->request.length)
250 dwc3_flush_cache((uintptr_t)req->request.dma, req->request.length);
252 if (req->request.status == -EINPROGRESS)
253 req->request.status = status;
255 if (dwc->ep0_bounced && dep->number == 0)
256 dwc->ep0_bounced = false;
258 usb_gadget_unmap_request(&dwc->gadget, &req->request,
261 dev_dbg(dwc->dev, "request %p from %s completed %d/%d ===> %d\n",
262 req, dep->name, req->request.actual,
263 req->request.length, status);
265 spin_unlock(&dwc->lock);
266 usb_gadget_giveback_request(&dep->endpoint, &req->request);
267 spin_lock(&dwc->lock);
270 int dwc3_send_gadget_generic_command(struct dwc3 *dwc, unsigned cmd, u32 param)
275 dwc3_writel(dwc->regs, DWC3_DGCMDPAR, param);
276 dwc3_writel(dwc->regs, DWC3_DGCMD, cmd | DWC3_DGCMD_CMDACT);
279 reg = dwc3_readl(dwc->regs, DWC3_DGCMD);
280 if (!(reg & DWC3_DGCMD_CMDACT)) {
281 dev_vdbg(dwc->dev, "Command Complete --> %d\n",
282 DWC3_DGCMD_STATUS(reg));
287 * We can't sleep here, because it's also called from
297 int dwc3_send_gadget_ep_cmd(struct dwc3 *dwc, unsigned ep,
298 unsigned cmd, struct dwc3_gadget_ep_cmd_params *params)
303 dwc3_writel(dwc->regs, DWC3_DEPCMDPAR0(ep), params->param0);
304 dwc3_writel(dwc->regs, DWC3_DEPCMDPAR1(ep), params->param1);
305 dwc3_writel(dwc->regs, DWC3_DEPCMDPAR2(ep), params->param2);
307 dwc3_writel(dwc->regs, DWC3_DEPCMD(ep), cmd | DWC3_DEPCMD_CMDACT);
309 reg = dwc3_readl(dwc->regs, DWC3_DEPCMD(ep));
310 if (!(reg & DWC3_DEPCMD_CMDACT)) {
311 dev_vdbg(dwc->dev, "Command Complete --> %d\n",
312 DWC3_DEPCMD_STATUS(reg));
317 * We can't sleep here, because it is also called from
328 static dma_addr_t dwc3_trb_dma_offset(struct dwc3_ep *dep,
329 struct dwc3_trb *trb)
331 u32 offset = (char *) trb - (char *) dep->trb_pool;
333 return dep->trb_pool_dma + offset;
336 static int dwc3_alloc_trb_pool(struct dwc3_ep *dep)
341 if (dep->number == 0 || dep->number == 1)
344 dep->trb_pool = dma_alloc_coherent(sizeof(struct dwc3_trb) *
346 (unsigned long *)&dep->trb_pool_dma);
347 if (!dep->trb_pool) {
348 dev_err(dep->dwc->dev, "failed to allocate trb pool for %s\n",
356 static void dwc3_free_trb_pool(struct dwc3_ep *dep)
358 dma_free_coherent(dep->trb_pool);
360 dep->trb_pool = NULL;
361 dep->trb_pool_dma = 0;
364 static int dwc3_gadget_start_config(struct dwc3 *dwc, struct dwc3_ep *dep)
366 struct dwc3_gadget_ep_cmd_params params;
369 memset(¶ms, 0x00, sizeof(params));
371 if (dep->number != 1) {
372 cmd = DWC3_DEPCMD_DEPSTARTCFG;
373 /* XferRscIdx == 0 for ep0 and 2 for the remaining */
374 if (dep->number > 1) {
375 if (dwc->start_config_issued)
377 dwc->start_config_issued = true;
378 cmd |= DWC3_DEPCMD_PARAM(2);
381 return dwc3_send_gadget_ep_cmd(dwc, 0, cmd, ¶ms);
387 static int dwc3_gadget_set_ep_config(struct dwc3 *dwc, struct dwc3_ep *dep,
388 const struct usb_endpoint_descriptor *desc,
389 const struct usb_ss_ep_comp_descriptor *comp_desc,
390 bool ignore, bool restore)
392 struct dwc3_gadget_ep_cmd_params params;
394 memset(¶ms, 0x00, sizeof(params));
396 params.param0 = DWC3_DEPCFG_EP_TYPE(usb_endpoint_type(desc))
397 | DWC3_DEPCFG_MAX_PACKET_SIZE(usb_endpoint_maxp(desc));
399 /* Burst size is only needed in SuperSpeed mode */
400 if (dwc->gadget.speed == USB_SPEED_SUPER) {
401 u32 burst = dep->endpoint.maxburst - 1;
403 params.param0 |= DWC3_DEPCFG_BURST_SIZE(burst);
407 params.param0 |= DWC3_DEPCFG_IGN_SEQ_NUM;
410 params.param0 |= DWC3_DEPCFG_ACTION_RESTORE;
411 params.param2 |= dep->saved_state;
414 params.param1 = DWC3_DEPCFG_XFER_COMPLETE_EN
415 | DWC3_DEPCFG_XFER_NOT_READY_EN;
417 if (usb_ss_max_streams(comp_desc) && usb_endpoint_xfer_bulk(desc)) {
418 params.param1 |= DWC3_DEPCFG_STREAM_CAPABLE
419 | DWC3_DEPCFG_STREAM_EVENT_EN;
420 dep->stream_capable = true;
423 if (!usb_endpoint_xfer_control(desc))
424 params.param1 |= DWC3_DEPCFG_XFER_IN_PROGRESS_EN;
427 * We are doing 1:1 mapping for endpoints, meaning
428 * Physical Endpoints 2 maps to Logical Endpoint 2 and
429 * so on. We consider the direction bit as part of the physical
430 * endpoint number. So USB endpoint 0x81 is 0x03.
432 params.param1 |= DWC3_DEPCFG_EP_NUMBER(dep->number);
435 * We must use the lower 16 TX FIFOs even though
439 params.param0 |= DWC3_DEPCFG_FIFO_NUMBER(dep->number >> 1);
441 if (desc->bInterval) {
442 params.param1 |= DWC3_DEPCFG_BINTERVAL_M1(desc->bInterval - 1);
443 dep->interval = 1 << (desc->bInterval - 1);
446 return dwc3_send_gadget_ep_cmd(dwc, dep->number,
447 DWC3_DEPCMD_SETEPCONFIG, ¶ms);
450 static int dwc3_gadget_set_xfer_resource(struct dwc3 *dwc, struct dwc3_ep *dep)
452 struct dwc3_gadget_ep_cmd_params params;
454 memset(¶ms, 0x00, sizeof(params));
456 params.param0 = DWC3_DEPXFERCFG_NUM_XFER_RES(1);
458 return dwc3_send_gadget_ep_cmd(dwc, dep->number,
459 DWC3_DEPCMD_SETTRANSFRESOURCE, ¶ms);
463 * __dwc3_gadget_ep_enable - Initializes a HW endpoint
464 * @dep: endpoint to be initialized
465 * @desc: USB Endpoint Descriptor
467 * Caller should take care of locking
469 static int __dwc3_gadget_ep_enable(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 *dwc = dep->dwc;
478 dev_vdbg(dwc->dev, "Enabling %s\n", dep->name);
480 if (!(dep->flags & DWC3_EP_ENABLED)) {
481 ret = dwc3_gadget_start_config(dwc, dep);
486 ret = dwc3_gadget_set_ep_config(dwc, dep, desc, comp_desc, ignore,
491 if (!(dep->flags & DWC3_EP_ENABLED)) {
492 struct dwc3_trb *trb_st_hw;
493 struct dwc3_trb *trb_link;
495 ret = dwc3_gadget_set_xfer_resource(dwc, dep);
499 dep->endpoint.desc = desc;
500 dep->comp_desc = comp_desc;
501 dep->type = usb_endpoint_type(desc);
502 dep->flags |= DWC3_EP_ENABLED;
504 reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
505 reg |= DWC3_DALEPENA_EP(dep->number);
506 dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
508 if (!usb_endpoint_xfer_isoc(desc))
511 /* Link TRB for ISOC. The HWO bit is never reset */
512 trb_st_hw = &dep->trb_pool[0];
514 trb_link = &dep->trb_pool[DWC3_TRB_NUM - 1];
515 memset(trb_link, 0, sizeof(*trb_link));
517 trb_link->bpl = lower_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw));
518 trb_link->bph = upper_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw));
519 trb_link->ctrl |= DWC3_TRBCTL_LINK_TRB;
520 trb_link->ctrl |= DWC3_TRB_CTRL_HWO;
526 static void dwc3_stop_active_transfer(struct dwc3 *dwc, u32 epnum, bool force);
527 static void dwc3_remove_requests(struct dwc3 *dwc, struct dwc3_ep *dep)
529 struct dwc3_request *req;
531 if (!list_empty(&dep->req_queued)) {
532 dwc3_stop_active_transfer(dwc, dep->number, true);
534 /* - giveback all requests to gadget driver */
535 while (!list_empty(&dep->req_queued)) {
536 req = next_request(&dep->req_queued);
538 dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
542 while (!list_empty(&dep->request_list)) {
543 req = next_request(&dep->request_list);
545 dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
550 * __dwc3_gadget_ep_disable - Disables a HW endpoint
551 * @dep: the endpoint to disable
553 * This function also removes requests which are currently processed ny the
554 * hardware and those which are not yet scheduled.
555 * Caller should take care of locking.
557 static int __dwc3_gadget_ep_disable(struct dwc3_ep *dep)
559 struct dwc3 *dwc = dep->dwc;
562 dwc3_remove_requests(dwc, dep);
564 /* make sure HW endpoint isn't stalled */
565 if (dep->flags & DWC3_EP_STALL)
566 __dwc3_gadget_ep_set_halt(dep, 0, false);
568 reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
569 reg &= ~DWC3_DALEPENA_EP(dep->number);
570 dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
572 dep->stream_capable = false;
573 dep->endpoint.desc = NULL;
574 dep->comp_desc = NULL;
581 /* -------------------------------------------------------------------------- */
583 static int dwc3_gadget_ep0_enable(struct usb_ep *ep,
584 const struct usb_endpoint_descriptor *desc)
589 static int dwc3_gadget_ep0_disable(struct usb_ep *ep)
594 /* -------------------------------------------------------------------------- */
596 static int dwc3_gadget_ep_enable(struct usb_ep *ep,
597 const struct usb_endpoint_descriptor *desc)
603 if (!ep || !desc || desc->bDescriptorType != USB_DT_ENDPOINT) {
604 pr_debug("dwc3: invalid parameters\n");
608 if (!desc->wMaxPacketSize) {
609 pr_debug("dwc3: missing wMaxPacketSize\n");
613 dep = to_dwc3_ep(ep);
615 if (dep->flags & DWC3_EP_ENABLED) {
616 WARN(true, "%s is already enabled\n",
621 switch (usb_endpoint_type(desc)) {
622 case USB_ENDPOINT_XFER_CONTROL:
623 strlcat(dep->name, "-control", sizeof(dep->name));
625 case USB_ENDPOINT_XFER_ISOC:
626 strlcat(dep->name, "-isoc", sizeof(dep->name));
628 case USB_ENDPOINT_XFER_BULK:
629 strlcat(dep->name, "-bulk", sizeof(dep->name));
631 case USB_ENDPOINT_XFER_INT:
632 strlcat(dep->name, "-int", sizeof(dep->name));
635 dev_err(dwc->dev, "invalid endpoint transfer type\n");
638 spin_lock_irqsave(&dwc->lock, flags);
639 ret = __dwc3_gadget_ep_enable(dep, desc, ep->comp_desc, false, false);
640 spin_unlock_irqrestore(&dwc->lock, flags);
645 static int dwc3_gadget_ep_disable(struct usb_ep *ep)
652 pr_debug("dwc3: invalid parameters\n");
656 dep = to_dwc3_ep(ep);
658 if (!(dep->flags & DWC3_EP_ENABLED)) {
659 WARN(true, "%s is already disabled\n",
664 snprintf(dep->name, sizeof(dep->name), "ep%d%s",
666 (dep->number & 1) ? "in" : "out");
668 spin_lock_irqsave(&dwc->lock, flags);
669 ret = __dwc3_gadget_ep_disable(dep);
670 spin_unlock_irqrestore(&dwc->lock, flags);
675 static struct usb_request *dwc3_gadget_ep_alloc_request(struct usb_ep *ep,
678 struct dwc3_request *req;
679 struct dwc3_ep *dep = to_dwc3_ep(ep);
681 req = kzalloc(sizeof(*req), gfp_flags);
685 req->epnum = dep->number;
688 return &req->request;
691 static void dwc3_gadget_ep_free_request(struct usb_ep *ep,
692 struct usb_request *request)
694 struct dwc3_request *req = to_dwc3_request(request);
700 * dwc3_prepare_one_trb - setup one TRB from one request
701 * @dep: endpoint for which this request is prepared
702 * @req: dwc3_request pointer
704 static void dwc3_prepare_one_trb(struct dwc3_ep *dep,
705 struct dwc3_request *req, dma_addr_t dma,
706 unsigned length, unsigned last, unsigned chain, unsigned node)
708 struct dwc3_trb *trb;
710 dev_vdbg(dwc->dev, "%s: req %p dma %08llx length %d%s%s\n",
711 dep->name, req, (unsigned long long) dma,
712 length, last ? " last" : "",
713 chain ? " chain" : "");
716 trb = &dep->trb_pool[dep->free_slot & DWC3_TRB_MASK];
719 dwc3_gadget_move_request_queued(req);
721 req->trb_dma = dwc3_trb_dma_offset(dep, trb);
722 req->start_slot = dep->free_slot & DWC3_TRB_MASK;
726 /* Skip the LINK-TRB on ISOC */
727 if (((dep->free_slot & DWC3_TRB_MASK) == DWC3_TRB_NUM - 1) &&
728 usb_endpoint_xfer_isoc(dep->endpoint.desc))
731 trb->size = DWC3_TRB_SIZE_LENGTH(length);
732 trb->bpl = lower_32_bits(dma);
733 trb->bph = upper_32_bits(dma);
735 switch (usb_endpoint_type(dep->endpoint.desc)) {
736 case USB_ENDPOINT_XFER_CONTROL:
737 trb->ctrl = DWC3_TRBCTL_CONTROL_SETUP;
740 case USB_ENDPOINT_XFER_ISOC:
742 trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS_FIRST;
744 trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS;
747 case USB_ENDPOINT_XFER_BULK:
748 case USB_ENDPOINT_XFER_INT:
749 trb->ctrl = DWC3_TRBCTL_NORMAL;
753 * This is only possible with faulty memory because we
754 * checked it already :)
759 if (!req->request.no_interrupt && !chain)
760 trb->ctrl |= DWC3_TRB_CTRL_IOC;
762 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
763 trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI;
764 trb->ctrl |= DWC3_TRB_CTRL_CSP;
766 trb->ctrl |= DWC3_TRB_CTRL_LST;
770 trb->ctrl |= DWC3_TRB_CTRL_CHN;
772 if (usb_endpoint_xfer_bulk(dep->endpoint.desc) && dep->stream_capable)
773 trb->ctrl |= DWC3_TRB_CTRL_SID_SOFN(req->request.stream_id);
775 trb->ctrl |= DWC3_TRB_CTRL_HWO;
777 dwc3_flush_cache((uintptr_t)dma, length);
778 dwc3_flush_cache((uintptr_t)trb, sizeof(*trb));
782 * dwc3_prepare_trbs - setup TRBs from requests
783 * @dep: endpoint for which requests are being prepared
784 * @starting: true if the endpoint is idle and no requests are queued.
786 * The function goes through the requests list and sets up TRBs for the
787 * transfers. The function returns once there are no more TRBs available or
788 * it runs out of requests.
790 static void dwc3_prepare_trbs(struct dwc3_ep *dep, bool starting)
792 struct dwc3_request *req, *n;
796 BUILD_BUG_ON_NOT_POWER_OF_2(DWC3_TRB_NUM);
798 /* the first request must not be queued */
799 trbs_left = (dep->busy_slot - dep->free_slot) & DWC3_TRB_MASK;
801 /* Can't wrap around on a non-isoc EP since there's no link TRB */
802 if (!usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
803 max = DWC3_TRB_NUM - (dep->free_slot & DWC3_TRB_MASK);
809 * If busy & slot are equal than it is either full or empty. If we are
810 * starting to process requests then we are empty. Otherwise we are
811 * full and don't do anything
816 trbs_left = DWC3_TRB_NUM;
818 * In case we start from scratch, we queue the ISOC requests
819 * starting from slot 1. This is done because we use ring
820 * buffer and have no LST bit to stop us. Instead, we place
821 * IOC bit every TRB_NUM/4. We try to avoid having an interrupt
822 * after the first request so we start at slot 1 and have
823 * 7 requests proceed before we hit the first IOC.
824 * Other transfer types don't use the ring buffer and are
825 * processed from the first TRB until the last one. Since we
826 * don't wrap around we have to start at the beginning.
828 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
837 /* The last TRB is a link TRB, not used for xfer */
838 if ((trbs_left <= 1) && usb_endpoint_xfer_isoc(dep->endpoint.desc))
841 list_for_each_entry_safe(req, n, &dep->request_list, list) {
845 dma = req->request.dma;
846 length = req->request.length;
848 dwc3_prepare_one_trb(dep, req, dma, length,
855 static int __dwc3_gadget_kick_transfer(struct dwc3_ep *dep, u16 cmd_param,
858 struct dwc3_gadget_ep_cmd_params params;
859 struct dwc3_request *req;
860 struct dwc3 *dwc = dep->dwc;
864 if (start_new && (dep->flags & DWC3_EP_BUSY)) {
865 dev_vdbg(dwc->dev, "%s: endpoint busy\n", dep->name);
868 dep->flags &= ~DWC3_EP_PENDING_REQUEST;
871 * If we are getting here after a short-out-packet we don't enqueue any
872 * new requests as we try to set the IOC bit only on the last request.
875 if (list_empty(&dep->req_queued))
876 dwc3_prepare_trbs(dep, start_new);
878 /* req points to the first request which will be sent */
879 req = next_request(&dep->req_queued);
881 dwc3_prepare_trbs(dep, start_new);
884 * req points to the first request where HWO changed from 0 to 1
886 req = next_request(&dep->req_queued);
889 dep->flags |= DWC3_EP_PENDING_REQUEST;
893 memset(¶ms, 0, sizeof(params));
896 params.param0 = upper_32_bits(req->trb_dma);
897 params.param1 = lower_32_bits(req->trb_dma);
898 cmd = DWC3_DEPCMD_STARTTRANSFER;
900 cmd = DWC3_DEPCMD_UPDATETRANSFER;
903 cmd |= DWC3_DEPCMD_PARAM(cmd_param);
904 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number, cmd, ¶ms);
906 dev_dbg(dwc->dev, "failed to send STARTTRANSFER command\n");
909 * FIXME we need to iterate over the list of requests
910 * here and stop, unmap, free and del each of the linked
911 * requests instead of what we do now.
913 usb_gadget_unmap_request(&dwc->gadget, &req->request,
915 list_del(&req->list);
919 dep->flags |= DWC3_EP_BUSY;
922 dep->resource_index = dwc3_gadget_ep_get_transfer_index(dwc,
924 WARN_ON_ONCE(!dep->resource_index);
930 static void __dwc3_gadget_start_isoc(struct dwc3 *dwc,
931 struct dwc3_ep *dep, u32 cur_uf)
935 if (list_empty(&dep->request_list)) {
936 dev_vdbg(dwc->dev, "ISOC ep %s run out for requests.\n",
938 dep->flags |= DWC3_EP_PENDING_REQUEST;
942 /* 4 micro frames in the future */
943 uf = cur_uf + dep->interval * 4;
945 __dwc3_gadget_kick_transfer(dep, uf, 1);
948 static void dwc3_gadget_start_isoc(struct dwc3 *dwc,
949 struct dwc3_ep *dep, const struct dwc3_event_depevt *event)
953 mask = ~(dep->interval - 1);
954 cur_uf = event->parameters & mask;
956 __dwc3_gadget_start_isoc(dwc, dep, cur_uf);
959 static int __dwc3_gadget_ep_queue(struct dwc3_ep *dep, struct dwc3_request *req)
961 struct dwc3 *dwc = dep->dwc;
964 req->request.actual = 0;
965 req->request.status = -EINPROGRESS;
966 req->direction = dep->direction;
967 req->epnum = dep->number;
970 * DWC3 hangs on OUT requests smaller than maxpacket size,
971 * so HACK the request length
973 if (dep->direction == 0 &&
974 req->request.length < dep->endpoint.maxpacket)
975 req->request.length = dep->endpoint.maxpacket;
978 * We only add to our list of requests now and
979 * start consuming the list once we get XferNotReady
982 * That way, we avoid doing anything that we don't need
983 * to do now and defer it until the point we receive a
984 * particular token from the Host side.
986 * This will also avoid Host cancelling URBs due to too
989 ret = usb_gadget_map_request(&dwc->gadget, &req->request,
994 list_add_tail(&req->list, &dep->request_list);
997 * There are a few special cases:
999 * 1. XferNotReady with empty list of requests. We need to kick the
1000 * transfer here in that situation, otherwise we will be NAKing
1001 * forever. If we get XferNotReady before gadget driver has a
1002 * chance to queue a request, we will ACK the IRQ but won't be
1003 * able to receive the data until the next request is queued.
1004 * The following code is handling exactly that.
1007 if (dep->flags & DWC3_EP_PENDING_REQUEST) {
1009 * If xfernotready is already elapsed and it is a case
1010 * of isoc transfer, then issue END TRANSFER, so that
1011 * you can receive xfernotready again and can have
1012 * notion of current microframe.
1014 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
1015 if (list_empty(&dep->req_queued)) {
1016 dwc3_stop_active_transfer(dwc, dep->number, true);
1017 dep->flags = DWC3_EP_ENABLED;
1022 ret = __dwc3_gadget_kick_transfer(dep, 0, true);
1023 if (ret && ret != -EBUSY)
1024 dev_dbg(dwc->dev, "%s: failed to kick transfers\n",
1030 * 2. XferInProgress on Isoc EP with an active transfer. We need to
1031 * kick the transfer here after queuing a request, otherwise the
1032 * core may not see the modified TRB(s).
1034 if (usb_endpoint_xfer_isoc(dep->endpoint.desc) &&
1035 (dep->flags & DWC3_EP_BUSY) &&
1036 !(dep->flags & DWC3_EP_MISSED_ISOC)) {
1037 WARN_ON_ONCE(!dep->resource_index);
1038 ret = __dwc3_gadget_kick_transfer(dep, dep->resource_index,
1040 if (ret && ret != -EBUSY)
1041 dev_dbg(dwc->dev, "%s: failed to kick transfers\n",
1047 * 4. Stream Capable Bulk Endpoints. We need to start the transfer
1048 * right away, otherwise host will not know we have streams to be
1051 if (dep->stream_capable) {
1054 ret = __dwc3_gadget_kick_transfer(dep, 0, true);
1055 if (ret && ret != -EBUSY) {
1056 dev_dbg(dwc->dev, "%s: failed to kick transfers\n",
1064 static int dwc3_gadget_ep_queue(struct usb_ep *ep, struct usb_request *request,
1067 struct dwc3_request *req = to_dwc3_request(request);
1068 struct dwc3_ep *dep = to_dwc3_ep(ep);
1070 unsigned long flags;
1074 spin_lock_irqsave(&dwc->lock, flags);
1075 if (!dep->endpoint.desc) {
1076 dev_dbg(dwc->dev, "trying to queue request %p to disabled %s\n",
1082 if (req->dep != dep) {
1083 WARN(true, "request %p belongs to '%s'\n",
1084 request, req->dep->name);
1089 dev_vdbg(dwc->dev, "queing request %p to %s length %d\n",
1090 request, ep->name, request->length);
1092 ret = __dwc3_gadget_ep_queue(dep, req);
1095 spin_unlock_irqrestore(&dwc->lock, flags);
1100 static int dwc3_gadget_ep_dequeue(struct usb_ep *ep,
1101 struct usb_request *request)
1103 struct dwc3_request *req = to_dwc3_request(request);
1104 struct dwc3_request *r = NULL;
1106 struct dwc3_ep *dep = to_dwc3_ep(ep);
1107 struct dwc3 *dwc = dep->dwc;
1109 unsigned long flags;
1112 spin_lock_irqsave(&dwc->lock, flags);
1114 list_for_each_entry(r, &dep->request_list, list) {
1120 list_for_each_entry(r, &dep->req_queued, list) {
1125 /* wait until it is processed */
1126 dwc3_stop_active_transfer(dwc, dep->number, true);
1129 dev_err(dwc->dev, "request %p was not queued to %s\n",
1136 /* giveback the request */
1137 dwc3_gadget_giveback(dep, req, -ECONNRESET);
1140 spin_unlock_irqrestore(&dwc->lock, flags);
1145 int __dwc3_gadget_ep_set_halt(struct dwc3_ep *dep, int value, int protocol)
1147 struct dwc3_gadget_ep_cmd_params params;
1148 struct dwc3 *dwc = dep->dwc;
1151 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
1152 dev_err(dwc->dev, "%s is of Isochronous type\n", dep->name);
1156 memset(¶ms, 0x00, sizeof(params));
1159 if (!protocol && ((dep->direction && dep->flags & DWC3_EP_BUSY) ||
1160 (!list_empty(&dep->req_queued) ||
1161 !list_empty(&dep->request_list)))) {
1162 dev_dbg(dwc->dev, "%s: pending request, cannot halt\n",
1167 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number,
1168 DWC3_DEPCMD_SETSTALL, ¶ms);
1170 dev_err(dwc->dev, "failed to set STALL on %s\n",
1173 dep->flags |= DWC3_EP_STALL;
1175 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number,
1176 DWC3_DEPCMD_CLEARSTALL, ¶ms);
1178 dev_err(dwc->dev, "failed to clear STALL on %s\n",
1181 dep->flags &= ~(DWC3_EP_STALL | DWC3_EP_WEDGE);
1187 static int dwc3_gadget_ep_set_halt(struct usb_ep *ep, int value)
1189 struct dwc3_ep *dep = to_dwc3_ep(ep);
1191 unsigned long flags;
1195 spin_lock_irqsave(&dwc->lock, flags);
1196 ret = __dwc3_gadget_ep_set_halt(dep, value, false);
1197 spin_unlock_irqrestore(&dwc->lock, flags);
1202 static int dwc3_gadget_ep_set_wedge(struct usb_ep *ep)
1204 struct dwc3_ep *dep = to_dwc3_ep(ep);
1205 unsigned long flags;
1208 spin_lock_irqsave(&dwc->lock, flags);
1209 dep->flags |= DWC3_EP_WEDGE;
1211 if (dep->number == 0 || dep->number == 1)
1212 ret = __dwc3_gadget_ep0_set_halt(ep, 1);
1214 ret = __dwc3_gadget_ep_set_halt(dep, 1, false);
1215 spin_unlock_irqrestore(&dwc->lock, flags);
1220 /* -------------------------------------------------------------------------- */
1222 static struct usb_endpoint_descriptor dwc3_gadget_ep0_desc = {
1223 .bLength = USB_DT_ENDPOINT_SIZE,
1224 .bDescriptorType = USB_DT_ENDPOINT,
1225 .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
1228 static const struct usb_ep_ops dwc3_gadget_ep0_ops = {
1229 .enable = dwc3_gadget_ep0_enable,
1230 .disable = dwc3_gadget_ep0_disable,
1231 .alloc_request = dwc3_gadget_ep_alloc_request,
1232 .free_request = dwc3_gadget_ep_free_request,
1233 .queue = dwc3_gadget_ep0_queue,
1234 .dequeue = dwc3_gadget_ep_dequeue,
1235 .set_halt = dwc3_gadget_ep0_set_halt,
1236 .set_wedge = dwc3_gadget_ep_set_wedge,
1239 static const struct usb_ep_ops dwc3_gadget_ep_ops = {
1240 .enable = dwc3_gadget_ep_enable,
1241 .disable = dwc3_gadget_ep_disable,
1242 .alloc_request = dwc3_gadget_ep_alloc_request,
1243 .free_request = dwc3_gadget_ep_free_request,
1244 .queue = dwc3_gadget_ep_queue,
1245 .dequeue = dwc3_gadget_ep_dequeue,
1246 .set_halt = dwc3_gadget_ep_set_halt,
1247 .set_wedge = dwc3_gadget_ep_set_wedge,
1250 /* -------------------------------------------------------------------------- */
1252 static int dwc3_gadget_get_frame(struct usb_gadget *g)
1254 struct dwc3 *dwc = gadget_to_dwc(g);
1257 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1258 return DWC3_DSTS_SOFFN(reg);
1261 static int dwc3_gadget_wakeup(struct usb_gadget *g)
1263 struct dwc3 *dwc = gadget_to_dwc(g);
1265 unsigned long timeout;
1266 unsigned long flags;
1275 spin_lock_irqsave(&dwc->lock, flags);
1278 * According to the Databook Remote wakeup request should
1279 * be issued only when the device is in early suspend state.
1281 * We can check that via USB Link State bits in DSTS register.
1283 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1285 speed = reg & DWC3_DSTS_CONNECTSPD;
1286 if (speed == DWC3_DSTS_SUPERSPEED) {
1287 dev_dbg(dwc->dev, "no wakeup on SuperSpeed\n");
1292 link_state = DWC3_DSTS_USBLNKST(reg);
1294 switch (link_state) {
1295 case DWC3_LINK_STATE_RX_DET: /* in HS, means Early Suspend */
1296 case DWC3_LINK_STATE_U3: /* in HS, means SUSPEND */
1299 dev_dbg(dwc->dev, "can't wakeup from link state %d\n",
1305 ret = dwc3_gadget_set_link_state(dwc, DWC3_LINK_STATE_RECOV);
1307 dev_err(dwc->dev, "failed to put link in Recovery\n");
1311 /* Recent versions do this automatically */
1312 if (dwc->revision < DWC3_REVISION_194A) {
1313 /* write zeroes to Link Change Request */
1314 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
1315 reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK;
1316 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1319 /* poll until Link State changes to ON */
1323 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1325 /* in HS, means ON */
1326 if (DWC3_DSTS_USBLNKST(reg) == DWC3_LINK_STATE_U0)
1330 if (DWC3_DSTS_USBLNKST(reg) != DWC3_LINK_STATE_U0) {
1331 dev_err(dwc->dev, "failed to send remote wakeup\n");
1336 spin_unlock_irqrestore(&dwc->lock, flags);
1341 static int dwc3_gadget_set_selfpowered(struct usb_gadget *g,
1344 struct dwc3 *dwc = gadget_to_dwc(g);
1345 unsigned long flags;
1347 spin_lock_irqsave(&dwc->lock, flags);
1348 dwc->is_selfpowered = !!is_selfpowered;
1349 spin_unlock_irqrestore(&dwc->lock, flags);
1354 static int dwc3_gadget_run_stop(struct dwc3 *dwc, int is_on, int suspend)
1359 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
1361 if (dwc->revision <= DWC3_REVISION_187A) {
1362 reg &= ~DWC3_DCTL_TRGTULST_MASK;
1363 reg |= DWC3_DCTL_TRGTULST_RX_DET;
1366 if (dwc->revision >= DWC3_REVISION_194A)
1367 reg &= ~DWC3_DCTL_KEEP_CONNECT;
1368 reg |= DWC3_DCTL_RUN_STOP;
1370 if (dwc->has_hibernation)
1371 reg |= DWC3_DCTL_KEEP_CONNECT;
1373 dwc->pullups_connected = true;
1375 reg &= ~DWC3_DCTL_RUN_STOP;
1377 if (dwc->has_hibernation && !suspend)
1378 reg &= ~DWC3_DCTL_KEEP_CONNECT;
1380 dwc->pullups_connected = false;
1383 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1386 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1388 if (!(reg & DWC3_DSTS_DEVCTRLHLT))
1391 if (reg & DWC3_DSTS_DEVCTRLHLT)
1400 dev_vdbg(dwc->dev, "gadget %s data soft-%s\n",
1402 ? dwc->gadget_driver->function : "no-function",
1403 is_on ? "connect" : "disconnect");
1408 static int dwc3_gadget_pullup(struct usb_gadget *g, int is_on)
1410 struct dwc3 *dwc = gadget_to_dwc(g);
1411 unsigned long flags;
1416 spin_lock_irqsave(&dwc->lock, flags);
1417 ret = dwc3_gadget_run_stop(dwc, is_on, false);
1418 spin_unlock_irqrestore(&dwc->lock, flags);
1423 static void dwc3_gadget_enable_irq(struct dwc3 *dwc)
1427 /* Enable all but Start and End of Frame IRQs */
1428 reg = (DWC3_DEVTEN_VNDRDEVTSTRCVEDEN |
1429 DWC3_DEVTEN_EVNTOVERFLOWEN |
1430 DWC3_DEVTEN_CMDCMPLTEN |
1431 DWC3_DEVTEN_ERRTICERREN |
1432 DWC3_DEVTEN_WKUPEVTEN |
1433 DWC3_DEVTEN_ULSTCNGEN |
1434 DWC3_DEVTEN_CONNECTDONEEN |
1435 DWC3_DEVTEN_USBRSTEN |
1436 DWC3_DEVTEN_DISCONNEVTEN);
1438 dwc3_writel(dwc->regs, DWC3_DEVTEN, reg);
1441 static void dwc3_gadget_disable_irq(struct dwc3 *dwc)
1443 /* mask all interrupts */
1444 dwc3_writel(dwc->regs, DWC3_DEVTEN, 0x00);
1447 static int dwc3_gadget_start(struct usb_gadget *g,
1448 struct usb_gadget_driver *driver)
1450 struct dwc3 *dwc = gadget_to_dwc(g);
1451 struct dwc3_ep *dep;
1452 unsigned long flags;
1456 spin_lock_irqsave(&dwc->lock, flags);
1458 if (dwc->gadget_driver) {
1459 dev_err(dwc->dev, "%s is already bound to %s\n",
1461 dwc->gadget_driver->function);
1466 dwc->gadget_driver = driver;
1468 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
1469 reg &= ~(DWC3_DCFG_SPEED_MASK);
1472 * WORKAROUND: DWC3 revision < 2.20a have an issue
1473 * which would cause metastability state on Run/Stop
1474 * bit if we try to force the IP to USB2-only mode.
1476 * Because of that, we cannot configure the IP to any
1477 * speed other than the SuperSpeed
1481 * STAR#9000525659: Clock Domain Crossing on DCTL in
1484 if (dwc->revision < DWC3_REVISION_220A) {
1485 reg |= DWC3_DCFG_SUPERSPEED;
1487 switch (dwc->maximum_speed) {
1489 reg |= DWC3_DSTS_LOWSPEED;
1491 case USB_SPEED_FULL:
1492 reg |= DWC3_DSTS_FULLSPEED1;
1494 case USB_SPEED_HIGH:
1495 reg |= DWC3_DSTS_HIGHSPEED;
1497 case USB_SPEED_SUPER: /* FALLTHROUGH */
1498 case USB_SPEED_UNKNOWN: /* FALTHROUGH */
1500 reg |= DWC3_DSTS_SUPERSPEED;
1503 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
1505 dwc->start_config_issued = false;
1507 /* Start with SuperSpeed Default */
1508 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
1511 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, false,
1514 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
1519 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, false,
1522 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
1526 /* begin to receive SETUP packets */
1527 dwc->ep0state = EP0_SETUP_PHASE;
1528 dwc3_ep0_out_start(dwc);
1530 dwc3_gadget_enable_irq(dwc);
1532 spin_unlock_irqrestore(&dwc->lock, flags);
1537 __dwc3_gadget_ep_disable(dwc->eps[0]);
1540 dwc->gadget_driver = NULL;
1543 spin_unlock_irqrestore(&dwc->lock, flags);
1548 static int dwc3_gadget_stop(struct usb_gadget *g)
1550 struct dwc3 *dwc = gadget_to_dwc(g);
1551 unsigned long flags;
1553 spin_lock_irqsave(&dwc->lock, flags);
1555 dwc3_gadget_disable_irq(dwc);
1556 __dwc3_gadget_ep_disable(dwc->eps[0]);
1557 __dwc3_gadget_ep_disable(dwc->eps[1]);
1559 dwc->gadget_driver = NULL;
1561 spin_unlock_irqrestore(&dwc->lock, flags);
1566 static const struct usb_gadget_ops dwc3_gadget_ops = {
1567 .get_frame = dwc3_gadget_get_frame,
1568 .wakeup = dwc3_gadget_wakeup,
1569 .set_selfpowered = dwc3_gadget_set_selfpowered,
1570 .pullup = dwc3_gadget_pullup,
1571 .udc_start = dwc3_gadget_start,
1572 .udc_stop = dwc3_gadget_stop,
1575 /* -------------------------------------------------------------------------- */
1577 static int dwc3_gadget_init_hw_endpoints(struct dwc3 *dwc,
1578 u8 num, u32 direction)
1580 struct dwc3_ep *dep;
1583 for (i = 0; i < num; i++) {
1584 u8 epnum = (i << 1) | (!!direction);
1586 dep = kzalloc(sizeof(*dep), GFP_KERNEL);
1591 dep->number = epnum;
1592 dep->direction = !!direction;
1593 dwc->eps[epnum] = dep;
1595 snprintf(dep->name, sizeof(dep->name), "ep%d%s", epnum >> 1,
1596 (epnum & 1) ? "in" : "out");
1598 dep->endpoint.name = dep->name;
1600 dev_vdbg(dwc->dev, "initializing %s\n", dep->name);
1602 if (epnum == 0 || epnum == 1) {
1603 usb_ep_set_maxpacket_limit(&dep->endpoint, 512);
1604 dep->endpoint.maxburst = 1;
1605 dep->endpoint.ops = &dwc3_gadget_ep0_ops;
1607 dwc->gadget.ep0 = &dep->endpoint;
1611 usb_ep_set_maxpacket_limit(&dep->endpoint, 512);
1612 dep->endpoint.max_streams = 15;
1613 dep->endpoint.ops = &dwc3_gadget_ep_ops;
1614 list_add_tail(&dep->endpoint.ep_list,
1615 &dwc->gadget.ep_list);
1617 ret = dwc3_alloc_trb_pool(dep);
1622 INIT_LIST_HEAD(&dep->request_list);
1623 INIT_LIST_HEAD(&dep->req_queued);
1629 static int dwc3_gadget_init_endpoints(struct dwc3 *dwc)
1633 INIT_LIST_HEAD(&dwc->gadget.ep_list);
1635 ret = dwc3_gadget_init_hw_endpoints(dwc, dwc->num_out_eps, 0);
1637 dev_vdbg(dwc->dev, "failed to allocate OUT endpoints\n");
1641 ret = dwc3_gadget_init_hw_endpoints(dwc, dwc->num_in_eps, 1);
1643 dev_vdbg(dwc->dev, "failed to allocate IN endpoints\n");
1650 static void dwc3_gadget_free_endpoints(struct dwc3 *dwc)
1652 struct dwc3_ep *dep;
1655 for (epnum = 0; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
1656 dep = dwc->eps[epnum];
1660 * Physical endpoints 0 and 1 are special; they form the
1661 * bi-directional USB endpoint 0.
1663 * For those two physical endpoints, we don't allocate a TRB
1664 * pool nor do we add them the endpoints list. Due to that, we
1665 * shouldn't do these two operations otherwise we would end up
1666 * with all sorts of bugs when removing dwc3.ko.
1668 if (epnum != 0 && epnum != 1) {
1669 dwc3_free_trb_pool(dep);
1670 list_del(&dep->endpoint.ep_list);
1677 /* -------------------------------------------------------------------------- */
1679 static int __dwc3_cleanup_done_trbs(struct dwc3 *dwc, struct dwc3_ep *dep,
1680 struct dwc3_request *req, struct dwc3_trb *trb,
1681 const struct dwc3_event_depevt *event, int status)
1684 unsigned int s_pkt = 0;
1685 unsigned int trb_status;
1687 if ((trb->ctrl & DWC3_TRB_CTRL_HWO) && status != -ESHUTDOWN)
1689 * We continue despite the error. There is not much we
1690 * can do. If we don't clean it up we loop forever. If
1691 * we skip the TRB then it gets overwritten after a
1692 * while since we use them in a ring buffer. A BUG()
1693 * would help. Lets hope that if this occurs, someone
1694 * fixes the root cause instead of looking away :)
1696 dev_err(dwc->dev, "%s's TRB (%p) still owned by HW\n",
1698 count = trb->size & DWC3_TRB_SIZE_MASK;
1700 if (dep->direction) {
1702 trb_status = DWC3_TRB_SIZE_TRBSTS(trb->size);
1703 if (trb_status == DWC3_TRBSTS_MISSED_ISOC) {
1704 dev_dbg(dwc->dev, "incomplete IN transfer %s\n",
1707 * If missed isoc occurred and there is
1708 * no request queued then issue END
1709 * TRANSFER, so that core generates
1710 * next xfernotready and we will issue
1711 * a fresh START TRANSFER.
1712 * If there are still queued request
1713 * then wait, do not issue either END
1714 * or UPDATE TRANSFER, just attach next
1715 * request in request_list during
1716 * giveback.If any future queued request
1717 * is successfully transferred then we
1718 * will issue UPDATE TRANSFER for all
1719 * request in the request_list.
1721 dep->flags |= DWC3_EP_MISSED_ISOC;
1723 dev_err(dwc->dev, "incomplete IN transfer %s\n",
1725 status = -ECONNRESET;
1728 dep->flags &= ~DWC3_EP_MISSED_ISOC;
1731 if (count && (event->status & DEPEVT_STATUS_SHORT))
1736 * We assume here we will always receive the entire data block
1737 * which we should receive. Meaning, if we program RX to
1738 * receive 4K but we receive only 2K, we assume that's all we
1739 * should receive and we simply bounce the request back to the
1740 * gadget driver for further processing.
1742 req->request.actual += req->request.length - count;
1745 if ((event->status & DEPEVT_STATUS_LST) &&
1746 (trb->ctrl & (DWC3_TRB_CTRL_LST |
1747 DWC3_TRB_CTRL_HWO)))
1749 if ((event->status & DEPEVT_STATUS_IOC) &&
1750 (trb->ctrl & DWC3_TRB_CTRL_IOC))
1755 static int dwc3_cleanup_done_reqs(struct dwc3 *dwc, struct dwc3_ep *dep,
1756 const struct dwc3_event_depevt *event, int status)
1758 struct dwc3_request *req;
1759 struct dwc3_trb *trb;
1762 req = next_request(&dep->req_queued);
1768 slot = req->start_slot;
1769 if ((slot == DWC3_TRB_NUM - 1) &&
1770 usb_endpoint_xfer_isoc(dep->endpoint.desc))
1772 slot %= DWC3_TRB_NUM;
1773 trb = &dep->trb_pool[slot];
1775 dwc3_flush_cache((uintptr_t)trb, sizeof(*trb));
1776 __dwc3_cleanup_done_trbs(dwc, dep, req, trb, event, status);
1777 dwc3_gadget_giveback(dep, req, status);
1779 if (usb_endpoint_xfer_isoc(dep->endpoint.desc) &&
1780 list_empty(&dep->req_queued)) {
1781 if (list_empty(&dep->request_list)) {
1783 * If there is no entry in request list then do
1784 * not issue END TRANSFER now. Just set PENDING
1785 * flag, so that END TRANSFER is issued when an
1786 * entry is added into request list.
1788 dep->flags = DWC3_EP_PENDING_REQUEST;
1790 dwc3_stop_active_transfer(dwc, dep->number, true);
1791 dep->flags = DWC3_EP_ENABLED;
1799 static void dwc3_endpoint_transfer_complete(struct dwc3 *dwc,
1800 struct dwc3_ep *dep, const struct dwc3_event_depevt *event)
1802 unsigned status = 0;
1805 if (event->status & DEPEVT_STATUS_BUSERR)
1806 status = -ECONNRESET;
1808 clean_busy = dwc3_cleanup_done_reqs(dwc, dep, event, status);
1810 dep->flags &= ~DWC3_EP_BUSY;
1813 * WORKAROUND: This is the 2nd half of U1/U2 -> U0 workaround.
1814 * See dwc3_gadget_linksts_change_interrupt() for 1st half.
1816 if (dwc->revision < DWC3_REVISION_183A) {
1820 for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) {
1823 if (!(dep->flags & DWC3_EP_ENABLED))
1826 if (!list_empty(&dep->req_queued))
1830 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
1832 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1838 static void dwc3_endpoint_interrupt(struct dwc3 *dwc,
1839 const struct dwc3_event_depevt *event)
1841 struct dwc3_ep *dep;
1842 u8 epnum = event->endpoint_number;
1844 dep = dwc->eps[epnum];
1846 if (!(dep->flags & DWC3_EP_ENABLED))
1849 if (epnum == 0 || epnum == 1) {
1850 dwc3_ep0_interrupt(dwc, event);
1854 switch (event->endpoint_event) {
1855 case DWC3_DEPEVT_XFERCOMPLETE:
1856 dep->resource_index = 0;
1858 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
1859 dev_dbg(dwc->dev, "%s is an Isochronous endpoint\n",
1864 dwc3_endpoint_transfer_complete(dwc, dep, event);
1866 case DWC3_DEPEVT_XFERINPROGRESS:
1867 dwc3_endpoint_transfer_complete(dwc, dep, event);
1869 case DWC3_DEPEVT_XFERNOTREADY:
1870 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
1871 dwc3_gadget_start_isoc(dwc, dep, event);
1875 dev_vdbg(dwc->dev, "%s: reason %s\n",
1876 dep->name, event->status &
1877 DEPEVT_STATUS_TRANSFER_ACTIVE
1879 : "Transfer Not Active");
1881 ret = __dwc3_gadget_kick_transfer(dep, 0, 1);
1882 if (!ret || ret == -EBUSY)
1885 dev_dbg(dwc->dev, "%s: failed to kick transfers\n",
1890 case DWC3_DEPEVT_STREAMEVT:
1891 if (!usb_endpoint_xfer_bulk(dep->endpoint.desc)) {
1892 dev_err(dwc->dev, "Stream event for non-Bulk %s\n",
1897 switch (event->status) {
1898 case DEPEVT_STREAMEVT_FOUND:
1899 dev_vdbg(dwc->dev, "Stream %d found and started\n",
1903 case DEPEVT_STREAMEVT_NOTFOUND:
1906 dev_dbg(dwc->dev, "Couldn't find suitable stream\n");
1909 case DWC3_DEPEVT_RXTXFIFOEVT:
1910 dev_dbg(dwc->dev, "%s FIFO Overrun\n", dep->name);
1912 case DWC3_DEPEVT_EPCMDCMPLT:
1913 dev_vdbg(dwc->dev, "Endpoint Command Complete\n");
1918 static void dwc3_disconnect_gadget(struct dwc3 *dwc)
1920 if (dwc->gadget_driver && dwc->gadget_driver->disconnect) {
1921 spin_unlock(&dwc->lock);
1922 dwc->gadget_driver->disconnect(&dwc->gadget);
1923 spin_lock(&dwc->lock);
1927 static void dwc3_suspend_gadget(struct dwc3 *dwc)
1929 if (dwc->gadget_driver && dwc->gadget_driver->suspend) {
1930 spin_unlock(&dwc->lock);
1931 dwc->gadget_driver->suspend(&dwc->gadget);
1932 spin_lock(&dwc->lock);
1936 static void dwc3_resume_gadget(struct dwc3 *dwc)
1938 if (dwc->gadget_driver && dwc->gadget_driver->resume) {
1939 spin_unlock(&dwc->lock);
1940 dwc->gadget_driver->resume(&dwc->gadget);
1944 static void dwc3_reset_gadget(struct dwc3 *dwc)
1946 if (!dwc->gadget_driver)
1949 if (dwc->gadget.speed != USB_SPEED_UNKNOWN) {
1950 spin_unlock(&dwc->lock);
1951 usb_gadget_udc_reset(&dwc->gadget, dwc->gadget_driver);
1952 spin_lock(&dwc->lock);
1956 static void dwc3_stop_active_transfer(struct dwc3 *dwc, u32 epnum, bool force)
1958 struct dwc3_ep *dep;
1959 struct dwc3_gadget_ep_cmd_params params;
1963 dep = dwc->eps[epnum];
1965 if (!dep->resource_index)
1969 * NOTICE: We are violating what the Databook says about the
1970 * EndTransfer command. Ideally we would _always_ wait for the
1971 * EndTransfer Command Completion IRQ, but that's causing too
1972 * much trouble synchronizing between us and gadget driver.
1974 * We have discussed this with the IP Provider and it was
1975 * suggested to giveback all requests here, but give HW some
1976 * extra time to synchronize with the interconnect. We're using
1977 * an arbitraty 100us delay for that.
1979 * Note also that a similar handling was tested by Synopsys
1980 * (thanks a lot Paul) and nothing bad has come out of it.
1981 * In short, what we're doing is:
1983 * - Issue EndTransfer WITH CMDIOC bit set
1987 cmd = DWC3_DEPCMD_ENDTRANSFER;
1988 cmd |= force ? DWC3_DEPCMD_HIPRI_FORCERM : 0;
1989 cmd |= DWC3_DEPCMD_CMDIOC;
1990 cmd |= DWC3_DEPCMD_PARAM(dep->resource_index);
1991 memset(¶ms, 0, sizeof(params));
1992 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number, cmd, ¶ms);
1994 dep->resource_index = 0;
1995 dep->flags &= ~DWC3_EP_BUSY;
1999 static void dwc3_stop_active_transfers(struct dwc3 *dwc)
2003 for (epnum = 2; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
2004 struct dwc3_ep *dep;
2006 dep = dwc->eps[epnum];
2010 if (!(dep->flags & DWC3_EP_ENABLED))
2013 dwc3_remove_requests(dwc, dep);
2017 static void dwc3_clear_stall_all_ep(struct dwc3 *dwc)
2021 for (epnum = 1; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
2022 struct dwc3_ep *dep;
2023 struct dwc3_gadget_ep_cmd_params params;
2026 dep = dwc->eps[epnum];
2030 if (!(dep->flags & DWC3_EP_STALL))
2033 dep->flags &= ~DWC3_EP_STALL;
2035 memset(¶ms, 0, sizeof(params));
2036 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number,
2037 DWC3_DEPCMD_CLEARSTALL, ¶ms);
2042 static void dwc3_gadget_disconnect_interrupt(struct dwc3 *dwc)
2046 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2047 reg &= ~DWC3_DCTL_INITU1ENA;
2048 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2050 reg &= ~DWC3_DCTL_INITU2ENA;
2051 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2053 dwc3_disconnect_gadget(dwc);
2054 dwc->start_config_issued = false;
2056 dwc->gadget.speed = USB_SPEED_UNKNOWN;
2057 dwc->setup_packet_pending = false;
2058 usb_gadget_set_state(&dwc->gadget, USB_STATE_NOTATTACHED);
2061 static void dwc3_gadget_reset_interrupt(struct dwc3 *dwc)
2066 * WORKAROUND: DWC3 revisions <1.88a have an issue which
2067 * would cause a missing Disconnect Event if there's a
2068 * pending Setup Packet in the FIFO.
2070 * There's no suggested workaround on the official Bug
2071 * report, which states that "unless the driver/application
2072 * is doing any special handling of a disconnect event,
2073 * there is no functional issue".
2075 * Unfortunately, it turns out that we _do_ some special
2076 * handling of a disconnect event, namely complete all
2077 * pending transfers, notify gadget driver of the
2078 * disconnection, and so on.
2080 * Our suggested workaround is to follow the Disconnect
2081 * Event steps here, instead, based on a setup_packet_pending
2082 * flag. Such flag gets set whenever we have a XferNotReady
2083 * event on EP0 and gets cleared on XferComplete for the
2088 * STAR#9000466709: RTL: Device : Disconnect event not
2089 * generated if setup packet pending in FIFO
2091 if (dwc->revision < DWC3_REVISION_188A) {
2092 if (dwc->setup_packet_pending)
2093 dwc3_gadget_disconnect_interrupt(dwc);
2096 dwc3_reset_gadget(dwc);
2098 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2099 reg &= ~DWC3_DCTL_TSTCTRL_MASK;
2100 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2101 dwc->test_mode = false;
2103 dwc3_stop_active_transfers(dwc);
2104 dwc3_clear_stall_all_ep(dwc);
2105 dwc->start_config_issued = false;
2107 /* Reset device address to zero */
2108 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2109 reg &= ~(DWC3_DCFG_DEVADDR_MASK);
2110 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
2113 static void dwc3_update_ram_clk_sel(struct dwc3 *dwc, u32 speed)
2116 u32 usb30_clock = DWC3_GCTL_CLK_BUS;
2119 * We change the clock only at SS but I dunno why I would want to do
2120 * this. Maybe it becomes part of the power saving plan.
2123 if (speed != DWC3_DSTS_SUPERSPEED)
2127 * RAMClkSel is reset to 0 after USB reset, so it must be reprogrammed
2128 * each time on Connect Done.
2133 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
2134 reg |= DWC3_GCTL_RAMCLKSEL(usb30_clock);
2135 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
2138 static void dwc3_gadget_conndone_interrupt(struct dwc3 *dwc)
2140 struct dwc3_ep *dep;
2145 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
2146 speed = reg & DWC3_DSTS_CONNECTSPD;
2149 dwc3_update_ram_clk_sel(dwc, speed);
2152 case DWC3_DCFG_SUPERSPEED:
2154 * WORKAROUND: DWC3 revisions <1.90a have an issue which
2155 * would cause a missing USB3 Reset event.
2157 * In such situations, we should force a USB3 Reset
2158 * event by calling our dwc3_gadget_reset_interrupt()
2163 * STAR#9000483510: RTL: SS : USB3 reset event may
2164 * not be generated always when the link enters poll
2166 if (dwc->revision < DWC3_REVISION_190A)
2167 dwc3_gadget_reset_interrupt(dwc);
2169 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
2170 dwc->gadget.ep0->maxpacket = 512;
2171 dwc->gadget.speed = USB_SPEED_SUPER;
2173 case DWC3_DCFG_HIGHSPEED:
2174 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
2175 dwc->gadget.ep0->maxpacket = 64;
2176 dwc->gadget.speed = USB_SPEED_HIGH;
2178 case DWC3_DCFG_FULLSPEED2:
2179 case DWC3_DCFG_FULLSPEED1:
2180 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
2181 dwc->gadget.ep0->maxpacket = 64;
2182 dwc->gadget.speed = USB_SPEED_FULL;
2184 case DWC3_DCFG_LOWSPEED:
2185 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(8);
2186 dwc->gadget.ep0->maxpacket = 8;
2187 dwc->gadget.speed = USB_SPEED_LOW;
2191 /* Enable USB2 LPM Capability */
2193 if ((dwc->revision > DWC3_REVISION_194A)
2194 && (speed != DWC3_DCFG_SUPERSPEED)) {
2195 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2196 reg |= DWC3_DCFG_LPM_CAP;
2197 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
2199 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2200 reg &= ~(DWC3_DCTL_HIRD_THRES_MASK | DWC3_DCTL_L1_HIBER_EN);
2202 reg |= DWC3_DCTL_HIRD_THRES(dwc->hird_threshold);
2205 * When dwc3 revisions >= 2.40a, LPM Erratum is enabled and
2206 * DCFG.LPMCap is set, core responses with an ACK and the
2207 * BESL value in the LPM token is less than or equal to LPM
2210 if (dwc->revision < DWC3_REVISION_240A && dwc->has_lpm_erratum)
2211 WARN(true, "LPM Erratum not available on dwc3 revisisions < 2.40a\n");
2213 if (dwc->has_lpm_erratum && dwc->revision >= DWC3_REVISION_240A)
2214 reg |= DWC3_DCTL_LPM_ERRATA(dwc->lpm_nyet_threshold);
2216 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2218 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2219 reg &= ~DWC3_DCTL_HIRD_THRES_MASK;
2220 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2224 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, true,
2227 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
2232 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, true,
2235 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
2240 * Configure PHY via GUSB3PIPECTLn if required.
2242 * Update GTXFIFOSIZn
2244 * In both cases reset values should be sufficient.
2248 static void dwc3_gadget_wakeup_interrupt(struct dwc3 *dwc)
2251 * TODO take core out of low power mode when that's
2255 dwc->gadget_driver->resume(&dwc->gadget);
2258 static void dwc3_gadget_linksts_change_interrupt(struct dwc3 *dwc,
2259 unsigned int evtinfo)
2261 enum dwc3_link_state next = evtinfo & DWC3_LINK_STATE_MASK;
2262 unsigned int pwropt;
2265 * WORKAROUND: DWC3 < 2.50a have an issue when configured without
2266 * Hibernation mode enabled which would show up when device detects
2267 * host-initiated U3 exit.
2269 * In that case, device will generate a Link State Change Interrupt
2270 * from U3 to RESUME which is only necessary if Hibernation is
2273 * There are no functional changes due to such spurious event and we
2274 * just need to ignore it.
2278 * STAR#9000570034 RTL: SS Resume event generated in non-Hibernation
2281 pwropt = DWC3_GHWPARAMS1_EN_PWROPT(dwc->hwparams.hwparams1);
2282 if ((dwc->revision < DWC3_REVISION_250A) &&
2283 (pwropt != DWC3_GHWPARAMS1_EN_PWROPT_HIB)) {
2284 if ((dwc->link_state == DWC3_LINK_STATE_U3) &&
2285 (next == DWC3_LINK_STATE_RESUME)) {
2286 dev_vdbg(dwc->dev, "ignoring transition U3 -> Resume\n");
2292 * WORKAROUND: DWC3 Revisions <1.83a have an issue which, depending
2293 * on the link partner, the USB session might do multiple entry/exit
2294 * of low power states before a transfer takes place.
2296 * Due to this problem, we might experience lower throughput. The
2297 * suggested workaround is to disable DCTL[12:9] bits if we're
2298 * transitioning from U1/U2 to U0 and enable those bits again
2299 * after a transfer completes and there are no pending transfers
2300 * on any of the enabled endpoints.
2302 * This is the first half of that workaround.
2306 * STAR#9000446952: RTL: Device SS : if U1/U2 ->U0 takes >128us
2307 * core send LGO_Ux entering U0
2309 if (dwc->revision < DWC3_REVISION_183A) {
2310 if (next == DWC3_LINK_STATE_U0) {
2314 switch (dwc->link_state) {
2315 case DWC3_LINK_STATE_U1:
2316 case DWC3_LINK_STATE_U2:
2317 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2318 u1u2 = reg & (DWC3_DCTL_INITU2ENA
2319 | DWC3_DCTL_ACCEPTU2ENA
2320 | DWC3_DCTL_INITU1ENA
2321 | DWC3_DCTL_ACCEPTU1ENA);
2324 dwc->u1u2 = reg & u1u2;
2328 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2338 case DWC3_LINK_STATE_U1:
2339 if (dwc->speed == USB_SPEED_SUPER)
2340 dwc3_suspend_gadget(dwc);
2342 case DWC3_LINK_STATE_U2:
2343 case DWC3_LINK_STATE_U3:
2344 dwc3_suspend_gadget(dwc);
2346 case DWC3_LINK_STATE_RESUME:
2347 dwc3_resume_gadget(dwc);
2354 dwc->link_state = next;
2357 static void dwc3_gadget_hibernation_interrupt(struct dwc3 *dwc,
2358 unsigned int evtinfo)
2360 unsigned int is_ss = evtinfo & (1UL << 4);
2363 * WORKAROUND: DWC3 revison 2.20a with hibernation support
2364 * have a known issue which can cause USB CV TD.9.23 to fail
2367 * Because of this issue, core could generate bogus hibernation
2368 * events which SW needs to ignore.
2372 * STAR#9000546576: Device Mode Hibernation: Issue in USB 2.0
2373 * Device Fallback from SuperSpeed
2375 if (is_ss ^ (dwc->speed == USB_SPEED_SUPER))
2378 /* enter hibernation here */
2381 static void dwc3_gadget_interrupt(struct dwc3 *dwc,
2382 const struct dwc3_event_devt *event)
2384 switch (event->type) {
2385 case DWC3_DEVICE_EVENT_DISCONNECT:
2386 dwc3_gadget_disconnect_interrupt(dwc);
2388 case DWC3_DEVICE_EVENT_RESET:
2389 dwc3_gadget_reset_interrupt(dwc);
2391 case DWC3_DEVICE_EVENT_CONNECT_DONE:
2392 dwc3_gadget_conndone_interrupt(dwc);
2394 case DWC3_DEVICE_EVENT_WAKEUP:
2395 dwc3_gadget_wakeup_interrupt(dwc);
2397 case DWC3_DEVICE_EVENT_HIBER_REQ:
2398 if (!dwc->has_hibernation) {
2399 WARN(1 ,"unexpected hibernation event\n");
2402 dwc3_gadget_hibernation_interrupt(dwc, event->event_info);
2404 case DWC3_DEVICE_EVENT_LINK_STATUS_CHANGE:
2405 dwc3_gadget_linksts_change_interrupt(dwc, event->event_info);
2407 case DWC3_DEVICE_EVENT_EOPF:
2408 dev_vdbg(dwc->dev, "End of Periodic Frame\n");
2410 case DWC3_DEVICE_EVENT_SOF:
2411 dev_vdbg(dwc->dev, "Start of Periodic Frame\n");
2413 case DWC3_DEVICE_EVENT_ERRATIC_ERROR:
2414 dev_vdbg(dwc->dev, "Erratic Error\n");
2416 case DWC3_DEVICE_EVENT_CMD_CMPL:
2417 dev_vdbg(dwc->dev, "Command Complete\n");
2419 case DWC3_DEVICE_EVENT_OVERFLOW:
2420 dev_vdbg(dwc->dev, "Overflow\n");
2423 dev_dbg(dwc->dev, "UNKNOWN IRQ %d\n", event->type);
2427 static void dwc3_process_event_entry(struct dwc3 *dwc,
2428 const union dwc3_event *event)
2430 /* Endpoint IRQ, handle it and return early */
2431 if (event->type.is_devspec == 0) {
2433 return dwc3_endpoint_interrupt(dwc, &event->depevt);
2436 switch (event->type.type) {
2437 case DWC3_EVENT_TYPE_DEV:
2438 dwc3_gadget_interrupt(dwc, &event->devt);
2440 /* REVISIT what to do with Carkit and I2C events ? */
2442 dev_err(dwc->dev, "UNKNOWN IRQ type %d\n", event->raw);
2446 static irqreturn_t dwc3_process_event_buf(struct dwc3 *dwc, u32 buf)
2448 struct dwc3_event_buffer *evt;
2449 irqreturn_t ret = IRQ_NONE;
2453 evt = dwc->ev_buffs[buf];
2456 if (!(evt->flags & DWC3_EVENT_PENDING))
2460 union dwc3_event event;
2462 event.raw = *(u32 *) (evt->buf + evt->lpos);
2464 dwc3_process_event_entry(dwc, &event);
2467 * FIXME we wrap around correctly to the next entry as
2468 * almost all entries are 4 bytes in size. There is one
2469 * entry which has 12 bytes which is a regular entry
2470 * followed by 8 bytes data. ATM I don't know how
2471 * things are organized if we get next to the a
2472 * boundary so I worry about that once we try to handle
2475 evt->lpos = (evt->lpos + 4) % DWC3_EVENT_BUFFERS_SIZE;
2478 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(buf), 4);
2482 evt->flags &= ~DWC3_EVENT_PENDING;
2485 /* Unmask interrupt */
2486 reg = dwc3_readl(dwc->regs, DWC3_GEVNTSIZ(buf));
2487 reg &= ~DWC3_GEVNTSIZ_INTMASK;
2488 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(buf), reg);
2493 static irqreturn_t dwc3_thread_interrupt(int irq, void *_dwc)
2495 struct dwc3 *dwc = _dwc;
2496 unsigned long flags;
2497 irqreturn_t ret = IRQ_NONE;
2500 spin_lock_irqsave(&dwc->lock, flags);
2502 for (i = 0; i < dwc->num_event_buffers; i++)
2503 ret |= dwc3_process_event_buf(dwc, i);
2505 spin_unlock_irqrestore(&dwc->lock, flags);
2510 static irqreturn_t dwc3_check_event_buf(struct dwc3 *dwc, u32 buf)
2512 struct dwc3_event_buffer *evt;
2516 evt = dwc->ev_buffs[buf];
2518 count = dwc3_readl(dwc->regs, DWC3_GEVNTCOUNT(buf));
2519 count &= DWC3_GEVNTCOUNT_MASK;
2524 evt->flags |= DWC3_EVENT_PENDING;
2526 /* Mask interrupt */
2527 reg = dwc3_readl(dwc->regs, DWC3_GEVNTSIZ(buf));
2528 reg |= DWC3_GEVNTSIZ_INTMASK;
2529 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(buf), reg);
2531 return IRQ_WAKE_THREAD;
2534 static irqreturn_t dwc3_interrupt(int irq, void *_dwc)
2536 struct dwc3 *dwc = _dwc;
2538 irqreturn_t ret = IRQ_NONE;
2540 spin_lock(&dwc->lock);
2542 for (i = 0; i < dwc->num_event_buffers; i++) {
2545 status = dwc3_check_event_buf(dwc, i);
2546 if (status == IRQ_WAKE_THREAD)
2550 spin_unlock(&dwc->lock);
2556 * dwc3_gadget_init - Initializes gadget related registers
2557 * @dwc: pointer to our controller context structure
2559 * Returns 0 on success otherwise negative errno.
2561 int dwc3_gadget_init(struct dwc3 *dwc)
2565 dwc->ctrl_req = dma_alloc_coherent(sizeof(*dwc->ctrl_req),
2566 (unsigned long *)&dwc->ctrl_req_addr);
2567 if (!dwc->ctrl_req) {
2568 dev_err(dwc->dev, "failed to allocate ctrl request\n");
2573 dwc->ep0_trb = dma_alloc_coherent(sizeof(*dwc->ep0_trb) * 2,
2574 (unsigned long *)&dwc->ep0_trb_addr);
2575 if (!dwc->ep0_trb) {
2576 dev_err(dwc->dev, "failed to allocate ep0 trb\n");
2581 dwc->setup_buf = memalign(CONFIG_SYS_CACHELINE_SIZE,
2582 DWC3_EP0_BOUNCE_SIZE);
2583 if (!dwc->setup_buf) {
2588 dwc->ep0_bounce = dma_alloc_coherent(DWC3_EP0_BOUNCE_SIZE,
2589 (unsigned long *)&dwc->ep0_bounce_addr);
2590 if (!dwc->ep0_bounce) {
2591 dev_err(dwc->dev, "failed to allocate ep0 bounce buffer\n");
2596 dwc->gadget.ops = &dwc3_gadget_ops;
2597 dwc->gadget.max_speed = USB_SPEED_SUPER;
2598 dwc->gadget.speed = USB_SPEED_UNKNOWN;
2599 dwc->gadget.name = "dwc3-gadget";
2602 * Per databook, DWC3 needs buffer size to be aligned to MaxPacketSize
2605 dwc->gadget.quirk_ep_out_aligned_size = true;
2608 * REVISIT: Here we should clear all pending IRQs to be
2609 * sure we're starting from a well known location.
2612 ret = dwc3_gadget_init_endpoints(dwc);
2616 ret = usb_add_gadget_udc((struct device *)dwc->dev, &dwc->gadget);
2618 dev_err(dwc->dev, "failed to register udc\n");
2625 dwc3_gadget_free_endpoints(dwc);
2626 dma_free_coherent(dwc->ep0_bounce);
2629 kfree(dwc->setup_buf);
2632 dma_free_coherent(dwc->ep0_trb);
2635 dma_free_coherent(dwc->ctrl_req);
2641 /* -------------------------------------------------------------------------- */
2643 void dwc3_gadget_exit(struct dwc3 *dwc)
2645 usb_del_gadget_udc(&dwc->gadget);
2647 dwc3_gadget_free_endpoints(dwc);
2649 dma_free_coherent(dwc->ep0_bounce);
2651 kfree(dwc->setup_buf);
2653 dma_free_coherent(dwc->ep0_trb);
2655 dma_free_coherent(dwc->ctrl_req);
2659 * dwc3_gadget_uboot_handle_interrupt - handle dwc3 gadget interrupt
2660 * @dwc: struct dwce *
2662 * Handles ep0 and gadget interrupt
2664 * Should be called from dwc3 core.
2666 void dwc3_gadget_uboot_handle_interrupt(struct dwc3 *dwc)
2668 int ret = dwc3_interrupt(0, dwc);
2670 if (ret == IRQ_WAKE_THREAD) {
2672 struct dwc3_event_buffer *evt;
2674 dwc3_thread_interrupt(0, dwc);
2676 /* Clean + Invalidate the buffers after touching them */
2677 for (i = 0; i < dwc->num_event_buffers; i++) {
2678 evt = dwc->ev_buffs[i];
2679 dwc3_flush_cache((uintptr_t)evt->buf, evt->length);