1 // SPDX-License-Identifier: GPL-2.0
3 * gadget.c - DesignWare USB3 DRD Controller Gadget Framework Link
5 * Copyright (C) 2010-2011 Texas Instruments Incorporated - http://www.ti.com
7 * Authors: Felipe Balbi <balbi@ti.com>,
8 * Sebastian Andrzej Siewior <bigeasy@linutronix.de>
11 #include <linux/kernel.h>
12 #include <linux/delay.h>
13 #include <linux/slab.h>
14 #include <linux/spinlock.h>
15 #include <linux/platform_device.h>
16 #include <linux/pm_runtime.h>
17 #include <linux/interrupt.h>
19 #include <linux/list.h>
20 #include <linux/dma-mapping.h>
22 #include <linux/usb/ch9.h>
23 #include <linux/usb/gadget.h>
31 * dwc3_gadget_set_test_mode - enables usb2 test modes
32 * @dwc: pointer to our context structure
33 * @mode: the mode to set (J, K SE0 NAK, Force Enable)
35 * Caller should take care of locking. This function will return 0 on
36 * success or -EINVAL if wrong Test Selector is passed.
38 int dwc3_gadget_set_test_mode(struct dwc3 *dwc, int mode)
42 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
43 reg &= ~DWC3_DCTL_TSTCTRL_MASK;
57 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
63 * dwc3_gadget_get_link_state - gets current state of usb link
64 * @dwc: pointer to our context structure
66 * Caller should take care of locking. This function will
67 * return the link state on success (>= 0) or -ETIMEDOUT.
69 int dwc3_gadget_get_link_state(struct dwc3 *dwc)
73 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
75 return DWC3_DSTS_USBLNKST(reg);
79 * dwc3_gadget_set_link_state - sets usb link to a particular state
80 * @dwc: pointer to our context structure
81 * @state: the state to put link into
83 * Caller should take care of locking. This function will
84 * return 0 on success or -ETIMEDOUT.
86 int dwc3_gadget_set_link_state(struct dwc3 *dwc, enum dwc3_link_state state)
92 * Wait until device controller is ready. Only applies to 1.94a and
95 if (dwc->revision >= DWC3_REVISION_194A) {
97 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
98 if (reg & DWC3_DSTS_DCNRD)
108 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
109 reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK;
111 /* set requested state */
112 reg |= DWC3_DCTL_ULSTCHNGREQ(state);
113 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
116 * The following code is racy when called from dwc3_gadget_wakeup,
117 * and is not needed, at least on newer versions
119 if (dwc->revision >= DWC3_REVISION_194A)
122 /* wait for a change in DSTS */
125 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
127 if (DWC3_DSTS_USBLNKST(reg) == state)
137 * dwc3_ep_inc_trb - increment a trb index.
138 * @index: Pointer to the TRB index to increment.
140 * The index should never point to the link TRB. After incrementing,
141 * if it is point to the link TRB, wrap around to the beginning. The
142 * link TRB is always at the last TRB entry.
144 static void dwc3_ep_inc_trb(u8 *index)
147 if (*index == (DWC3_TRB_NUM - 1))
152 * dwc3_ep_inc_enq - increment endpoint's enqueue pointer
153 * @dep: The endpoint whose enqueue pointer we're incrementing
155 static void dwc3_ep_inc_enq(struct dwc3_ep *dep)
157 dwc3_ep_inc_trb(&dep->trb_enqueue);
161 * dwc3_ep_inc_deq - increment endpoint's dequeue pointer
162 * @dep: The endpoint whose enqueue pointer we're incrementing
164 static void dwc3_ep_inc_deq(struct dwc3_ep *dep)
166 dwc3_ep_inc_trb(&dep->trb_dequeue);
169 void dwc3_gadget_del_and_unmap_request(struct dwc3_ep *dep,
170 struct dwc3_request *req, int status)
172 struct dwc3 *dwc = dep->dwc;
174 req->started = false;
175 list_del(&req->list);
178 if (req->request.status == -EINPROGRESS)
179 req->request.status = status;
182 usb_gadget_unmap_request_by_dev(dwc->sysdev,
183 &req->request, req->direction);
186 trace_dwc3_gadget_giveback(req);
189 pm_runtime_put(dwc->dev);
193 * dwc3_gadget_giveback - call struct usb_request's ->complete callback
194 * @dep: The endpoint to whom the request belongs to
195 * @req: The request we're giving back
196 * @status: completion code for the request
198 * Must be called with controller's lock held and interrupts disabled. This
199 * function will unmap @req and call its ->complete() callback to notify upper
200 * layers that it has completed.
202 void dwc3_gadget_giveback(struct dwc3_ep *dep, struct dwc3_request *req,
205 struct dwc3 *dwc = dep->dwc;
207 dwc3_gadget_del_and_unmap_request(dep, req, status);
209 spin_unlock(&dwc->lock);
210 usb_gadget_giveback_request(&dep->endpoint, &req->request);
211 spin_lock(&dwc->lock);
215 * dwc3_send_gadget_generic_command - issue a generic command for the controller
216 * @dwc: pointer to the controller context
217 * @cmd: the command to be issued
218 * @param: command parameter
220 * Caller should take care of locking. Issue @cmd with a given @param to @dwc
221 * and wait for its completion.
223 int dwc3_send_gadget_generic_command(struct dwc3 *dwc, unsigned cmd, u32 param)
230 dwc3_writel(dwc->regs, DWC3_DGCMDPAR, param);
231 dwc3_writel(dwc->regs, DWC3_DGCMD, cmd | DWC3_DGCMD_CMDACT);
234 reg = dwc3_readl(dwc->regs, DWC3_DGCMD);
235 if (!(reg & DWC3_DGCMD_CMDACT)) {
236 status = DWC3_DGCMD_STATUS(reg);
248 trace_dwc3_gadget_generic_cmd(cmd, param, status);
253 static int __dwc3_gadget_wakeup(struct dwc3 *dwc);
256 * dwc3_send_gadget_ep_cmd - issue an endpoint command
257 * @dep: the endpoint to which the command is going to be issued
258 * @cmd: the command to be issued
259 * @params: parameters to the command
261 * Caller should handle locking. This function will issue @cmd with given
262 * @params to @dep and wait for its completion.
264 int dwc3_send_gadget_ep_cmd(struct dwc3_ep *dep, unsigned cmd,
265 struct dwc3_gadget_ep_cmd_params *params)
267 const struct usb_endpoint_descriptor *desc = dep->endpoint.desc;
268 struct dwc3 *dwc = dep->dwc;
277 * Synopsys Databook 2.60a states, on section 6.3.2.5.[1-8], that if
278 * we're issuing an endpoint command, we must check if
279 * GUSB2PHYCFG.SUSPHY bit is set. If it is, then we need to clear it.
281 * We will also set SUSPHY bit to what it was before returning as stated
282 * by the same section on Synopsys databook.
284 if (dwc->gadget.speed <= USB_SPEED_HIGH) {
285 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
286 if (unlikely(reg & DWC3_GUSB2PHYCFG_SUSPHY)) {
288 reg &= ~DWC3_GUSB2PHYCFG_SUSPHY;
289 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
293 if (DWC3_DEPCMD_CMD(cmd) == DWC3_DEPCMD_STARTTRANSFER) {
296 needs_wakeup = (dwc->link_state == DWC3_LINK_STATE_U1 ||
297 dwc->link_state == DWC3_LINK_STATE_U2 ||
298 dwc->link_state == DWC3_LINK_STATE_U3);
300 if (unlikely(needs_wakeup)) {
301 ret = __dwc3_gadget_wakeup(dwc);
302 dev_WARN_ONCE(dwc->dev, ret, "wakeup failed --> %d\n",
307 dwc3_writel(dep->regs, DWC3_DEPCMDPAR0, params->param0);
308 dwc3_writel(dep->regs, DWC3_DEPCMDPAR1, params->param1);
309 dwc3_writel(dep->regs, DWC3_DEPCMDPAR2, params->param2);
312 * Synopsys Databook 2.60a states in section 6.3.2.5.6 of that if we're
313 * not relying on XferNotReady, we can make use of a special "No
314 * Response Update Transfer" command where we should clear both CmdAct
317 * With this, we don't need to wait for command completion and can
318 * straight away issue further commands to the endpoint.
320 * NOTICE: We're making an assumption that control endpoints will never
321 * make use of Update Transfer command. This is a safe assumption
322 * because we can never have more than one request at a time with
323 * Control Endpoints. If anybody changes that assumption, this chunk
324 * needs to be updated accordingly.
326 if (DWC3_DEPCMD_CMD(cmd) == DWC3_DEPCMD_UPDATETRANSFER &&
327 !usb_endpoint_xfer_isoc(desc))
328 cmd &= ~(DWC3_DEPCMD_CMDIOC | DWC3_DEPCMD_CMDACT);
330 cmd |= DWC3_DEPCMD_CMDACT;
332 dwc3_writel(dep->regs, DWC3_DEPCMD, cmd);
334 reg = dwc3_readl(dep->regs, DWC3_DEPCMD);
335 if (!(reg & DWC3_DEPCMD_CMDACT)) {
336 cmd_status = DWC3_DEPCMD_STATUS(reg);
338 switch (cmd_status) {
342 case DEPEVT_TRANSFER_NO_RESOURCE:
345 case DEPEVT_TRANSFER_BUS_EXPIRY:
347 * SW issues START TRANSFER command to
348 * isochronous ep with future frame interval. If
349 * future interval time has already passed when
350 * core receives the command, it will respond
351 * with an error status of 'Bus Expiry'.
353 * Instead of always returning -EINVAL, let's
354 * give a hint to the gadget driver that this is
355 * the case by returning -EAGAIN.
360 dev_WARN(dwc->dev, "UNKNOWN cmd status\n");
369 cmd_status = -ETIMEDOUT;
372 trace_dwc3_gadget_ep_cmd(dep, cmd, params, cmd_status);
375 switch (DWC3_DEPCMD_CMD(cmd)) {
376 case DWC3_DEPCMD_STARTTRANSFER:
377 dep->flags |= DWC3_EP_TRANSFER_STARTED;
379 case DWC3_DEPCMD_ENDTRANSFER:
380 dep->flags &= ~DWC3_EP_TRANSFER_STARTED;
388 if (unlikely(susphy)) {
389 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
390 reg |= DWC3_GUSB2PHYCFG_SUSPHY;
391 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
397 static int dwc3_send_clear_stall_ep_cmd(struct dwc3_ep *dep)
399 struct dwc3 *dwc = dep->dwc;
400 struct dwc3_gadget_ep_cmd_params params;
401 u32 cmd = DWC3_DEPCMD_CLEARSTALL;
404 * As of core revision 2.60a the recommended programming model
405 * is to set the ClearPendIN bit when issuing a Clear Stall EP
406 * command for IN endpoints. This is to prevent an issue where
407 * some (non-compliant) hosts may not send ACK TPs for pending
408 * IN transfers due to a mishandled error condition. Synopsys
411 if (dep->direction && (dwc->revision >= DWC3_REVISION_260A) &&
412 (dwc->gadget.speed >= USB_SPEED_SUPER))
413 cmd |= DWC3_DEPCMD_CLEARPENDIN;
415 memset(¶ms, 0, sizeof(params));
417 return dwc3_send_gadget_ep_cmd(dep, cmd, ¶ms);
420 static dma_addr_t dwc3_trb_dma_offset(struct dwc3_ep *dep,
421 struct dwc3_trb *trb)
423 u32 offset = (char *) trb - (char *) dep->trb_pool;
425 return dep->trb_pool_dma + offset;
428 static int dwc3_alloc_trb_pool(struct dwc3_ep *dep)
430 struct dwc3 *dwc = dep->dwc;
435 dep->trb_pool = dma_alloc_coherent(dwc->sysdev,
436 sizeof(struct dwc3_trb) * DWC3_TRB_NUM,
437 &dep->trb_pool_dma, GFP_KERNEL);
438 if (!dep->trb_pool) {
439 dev_err(dep->dwc->dev, "failed to allocate trb pool for %s\n",
447 static void dwc3_free_trb_pool(struct dwc3_ep *dep)
449 struct dwc3 *dwc = dep->dwc;
451 dma_free_coherent(dwc->sysdev, sizeof(struct dwc3_trb) * DWC3_TRB_NUM,
452 dep->trb_pool, dep->trb_pool_dma);
454 dep->trb_pool = NULL;
455 dep->trb_pool_dma = 0;
458 static int dwc3_gadget_set_xfer_resource(struct dwc3 *dwc, struct dwc3_ep *dep);
461 * dwc3_gadget_start_config - configure ep resources
462 * @dwc: pointer to our controller context structure
463 * @dep: endpoint that is being enabled
465 * Issue a %DWC3_DEPCMD_DEPSTARTCFG command to @dep. After the command's
466 * completion, it will set Transfer Resource for all available endpoints.
468 * The assignment of transfer resources cannot perfectly follow the data book
469 * due to the fact that the controller driver does not have all knowledge of the
470 * configuration in advance. It is given this information piecemeal by the
471 * composite gadget framework after every SET_CONFIGURATION and
472 * SET_INTERFACE. Trying to follow the databook programming model in this
473 * scenario can cause errors. For two reasons:
475 * 1) The databook says to do %DWC3_DEPCMD_DEPSTARTCFG for every
476 * %USB_REQ_SET_CONFIGURATION and %USB_REQ_SET_INTERFACE (8.1.5). This is
477 * incorrect in the scenario of multiple interfaces.
479 * 2) The databook does not mention doing more %DWC3_DEPCMD_DEPXFERCFG for new
480 * endpoint on alt setting (8.1.6).
482 * The following simplified method is used instead:
484 * All hardware endpoints can be assigned a transfer resource and this setting
485 * will stay persistent until either a core reset or hibernation. So whenever we
486 * do a %DWC3_DEPCMD_DEPSTARTCFG(0) we can go ahead and do
487 * %DWC3_DEPCMD_DEPXFERCFG for every hardware endpoint as well. We are
488 * guaranteed that there are as many transfer resources as endpoints.
490 * This function is called for each endpoint when it is being enabled but is
491 * triggered only when called for EP0-out, which always happens first, and which
492 * should only happen in one of the above conditions.
494 static int dwc3_gadget_start_config(struct dwc3 *dwc, struct dwc3_ep *dep)
496 struct dwc3_gadget_ep_cmd_params params;
504 memset(¶ms, 0x00, sizeof(params));
505 cmd = DWC3_DEPCMD_DEPSTARTCFG;
507 ret = dwc3_send_gadget_ep_cmd(dep, cmd, ¶ms);
511 for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) {
512 struct dwc3_ep *dep = dwc->eps[i];
517 ret = dwc3_gadget_set_xfer_resource(dwc, dep);
525 static int dwc3_gadget_set_ep_config(struct dwc3 *dwc, struct dwc3_ep *dep,
526 bool modify, bool restore)
528 const struct usb_ss_ep_comp_descriptor *comp_desc;
529 const struct usb_endpoint_descriptor *desc;
530 struct dwc3_gadget_ep_cmd_params params;
532 if (dev_WARN_ONCE(dwc->dev, modify && restore,
533 "Can't modify and restore\n"))
536 comp_desc = dep->endpoint.comp_desc;
537 desc = dep->endpoint.desc;
539 memset(¶ms, 0x00, sizeof(params));
541 params.param0 = DWC3_DEPCFG_EP_TYPE(usb_endpoint_type(desc))
542 | DWC3_DEPCFG_MAX_PACKET_SIZE(usb_endpoint_maxp(desc));
544 /* Burst size is only needed in SuperSpeed mode */
545 if (dwc->gadget.speed >= USB_SPEED_SUPER) {
546 u32 burst = dep->endpoint.maxburst;
547 params.param0 |= DWC3_DEPCFG_BURST_SIZE(burst - 1);
551 params.param0 |= DWC3_DEPCFG_ACTION_MODIFY;
552 } else if (restore) {
553 params.param0 |= DWC3_DEPCFG_ACTION_RESTORE;
554 params.param2 |= dep->saved_state;
556 params.param0 |= DWC3_DEPCFG_ACTION_INIT;
559 if (usb_endpoint_xfer_control(desc))
560 params.param1 = DWC3_DEPCFG_XFER_COMPLETE_EN;
562 if (dep->number <= 1 || usb_endpoint_xfer_isoc(desc))
563 params.param1 |= DWC3_DEPCFG_XFER_NOT_READY_EN;
565 if (usb_ss_max_streams(comp_desc) && usb_endpoint_xfer_bulk(desc)) {
566 params.param1 |= DWC3_DEPCFG_STREAM_CAPABLE
567 | DWC3_DEPCFG_STREAM_EVENT_EN;
568 dep->stream_capable = true;
571 if (!usb_endpoint_xfer_control(desc))
572 params.param1 |= DWC3_DEPCFG_XFER_IN_PROGRESS_EN;
575 * We are doing 1:1 mapping for endpoints, meaning
576 * Physical Endpoints 2 maps to Logical Endpoint 2 and
577 * so on. We consider the direction bit as part of the physical
578 * endpoint number. So USB endpoint 0x81 is 0x03.
580 params.param1 |= DWC3_DEPCFG_EP_NUMBER(dep->number);
583 * We must use the lower 16 TX FIFOs even though
587 params.param0 |= DWC3_DEPCFG_FIFO_NUMBER(dep->number >> 1);
589 if (desc->bInterval) {
590 params.param1 |= DWC3_DEPCFG_BINTERVAL_M1(desc->bInterval - 1);
591 dep->interval = 1 << (desc->bInterval - 1);
594 return dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETEPCONFIG, ¶ms);
597 static int dwc3_gadget_set_xfer_resource(struct dwc3 *dwc, struct dwc3_ep *dep)
599 struct dwc3_gadget_ep_cmd_params params;
601 memset(¶ms, 0x00, sizeof(params));
603 params.param0 = DWC3_DEPXFERCFG_NUM_XFER_RES(1);
605 return dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETTRANSFRESOURCE,
610 * __dwc3_gadget_ep_enable - initializes a hw endpoint
611 * @dep: endpoint to be initialized
612 * @modify: if true, modify existing endpoint configuration
613 * @restore: if true, restore endpoint configuration from scratch buffer
615 * Caller should take care of locking. Execute all necessary commands to
616 * initialize a HW endpoint so it can be used by a gadget driver.
618 static int __dwc3_gadget_ep_enable(struct dwc3_ep *dep,
619 bool modify, bool restore)
621 const struct usb_endpoint_descriptor *desc = dep->endpoint.desc;
622 struct dwc3 *dwc = dep->dwc;
627 if (!(dep->flags & DWC3_EP_ENABLED)) {
628 ret = dwc3_gadget_start_config(dwc, dep);
633 ret = dwc3_gadget_set_ep_config(dwc, dep, modify, restore);
637 if (!(dep->flags & DWC3_EP_ENABLED)) {
638 struct dwc3_trb *trb_st_hw;
639 struct dwc3_trb *trb_link;
641 dep->type = usb_endpoint_type(desc);
642 dep->flags |= DWC3_EP_ENABLED;
643 dep->flags &= ~DWC3_EP_END_TRANSFER_PENDING;
645 reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
646 reg |= DWC3_DALEPENA_EP(dep->number);
647 dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
649 init_waitqueue_head(&dep->wait_end_transfer);
651 if (usb_endpoint_xfer_control(desc))
654 /* Initialize the TRB ring */
655 dep->trb_dequeue = 0;
656 dep->trb_enqueue = 0;
657 memset(dep->trb_pool, 0,
658 sizeof(struct dwc3_trb) * DWC3_TRB_NUM);
660 /* Link TRB. The HWO bit is never reset */
661 trb_st_hw = &dep->trb_pool[0];
663 trb_link = &dep->trb_pool[DWC3_TRB_NUM - 1];
664 trb_link->bpl = lower_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw));
665 trb_link->bph = upper_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw));
666 trb_link->ctrl |= DWC3_TRBCTL_LINK_TRB;
667 trb_link->ctrl |= DWC3_TRB_CTRL_HWO;
671 * Issue StartTransfer here with no-op TRB so we can always rely on No
672 * Response Update Transfer command.
674 if (usb_endpoint_xfer_bulk(desc) ||
675 usb_endpoint_xfer_int(desc)) {
676 struct dwc3_gadget_ep_cmd_params params;
677 struct dwc3_trb *trb;
681 memset(¶ms, 0, sizeof(params));
682 trb = &dep->trb_pool[0];
683 trb_dma = dwc3_trb_dma_offset(dep, trb);
685 params.param0 = upper_32_bits(trb_dma);
686 params.param1 = lower_32_bits(trb_dma);
688 cmd = DWC3_DEPCMD_STARTTRANSFER;
690 ret = dwc3_send_gadget_ep_cmd(dep, cmd, ¶ms);
694 dep->flags |= DWC3_EP_BUSY;
696 dep->resource_index = dwc3_gadget_ep_get_transfer_index(dep);
697 WARN_ON_ONCE(!dep->resource_index);
701 trace_dwc3_gadget_ep_enable(dep);
706 static void dwc3_stop_active_transfer(struct dwc3 *dwc, u32 epnum, bool force);
707 static void dwc3_remove_requests(struct dwc3 *dwc, struct dwc3_ep *dep)
709 struct dwc3_request *req;
711 dwc3_stop_active_transfer(dwc, dep->number, true);
713 /* - giveback all requests to gadget driver */
714 while (!list_empty(&dep->started_list)) {
715 req = next_request(&dep->started_list);
717 dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
720 while (!list_empty(&dep->pending_list)) {
721 req = next_request(&dep->pending_list);
723 dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
728 * __dwc3_gadget_ep_disable - disables a hw endpoint
729 * @dep: the endpoint to disable
731 * This function undoes what __dwc3_gadget_ep_enable did and also removes
732 * requests which are currently being processed by the hardware and those which
733 * are not yet scheduled.
735 * Caller should take care of locking.
737 static int __dwc3_gadget_ep_disable(struct dwc3_ep *dep)
739 struct dwc3 *dwc = dep->dwc;
742 trace_dwc3_gadget_ep_disable(dep);
744 dwc3_remove_requests(dwc, dep);
746 /* make sure HW endpoint isn't stalled */
747 if (dep->flags & DWC3_EP_STALL)
748 __dwc3_gadget_ep_set_halt(dep, 0, false);
750 reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
751 reg &= ~DWC3_DALEPENA_EP(dep->number);
752 dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
754 dep->stream_capable = false;
756 dep->flags &= DWC3_EP_END_TRANSFER_PENDING;
758 /* Clear out the ep descriptors for non-ep0 */
759 if (dep->number > 1) {
760 dep->endpoint.comp_desc = NULL;
761 dep->endpoint.desc = NULL;
767 /* -------------------------------------------------------------------------- */
769 static int dwc3_gadget_ep0_enable(struct usb_ep *ep,
770 const struct usb_endpoint_descriptor *desc)
775 static int dwc3_gadget_ep0_disable(struct usb_ep *ep)
780 /* -------------------------------------------------------------------------- */
782 static int dwc3_gadget_ep_enable(struct usb_ep *ep,
783 const struct usb_endpoint_descriptor *desc)
790 if (!ep || !desc || desc->bDescriptorType != USB_DT_ENDPOINT) {
791 pr_debug("dwc3: invalid parameters\n");
795 if (!desc->wMaxPacketSize) {
796 pr_debug("dwc3: missing wMaxPacketSize\n");
800 dep = to_dwc3_ep(ep);
803 if (dev_WARN_ONCE(dwc->dev, dep->flags & DWC3_EP_ENABLED,
804 "%s is already enabled\n",
808 spin_lock_irqsave(&dwc->lock, flags);
809 ret = __dwc3_gadget_ep_enable(dep, false, false);
810 spin_unlock_irqrestore(&dwc->lock, flags);
815 static int dwc3_gadget_ep_disable(struct usb_ep *ep)
823 pr_debug("dwc3: invalid parameters\n");
827 dep = to_dwc3_ep(ep);
830 if (dev_WARN_ONCE(dwc->dev, !(dep->flags & DWC3_EP_ENABLED),
831 "%s is already disabled\n",
835 spin_lock_irqsave(&dwc->lock, flags);
836 ret = __dwc3_gadget_ep_disable(dep);
837 spin_unlock_irqrestore(&dwc->lock, flags);
842 static struct usb_request *dwc3_gadget_ep_alloc_request(struct usb_ep *ep,
845 struct dwc3_request *req;
846 struct dwc3_ep *dep = to_dwc3_ep(ep);
848 req = kzalloc(sizeof(*req), gfp_flags);
852 req->epnum = dep->number;
855 trace_dwc3_alloc_request(req);
857 return &req->request;
860 static void dwc3_gadget_ep_free_request(struct usb_ep *ep,
861 struct usb_request *request)
863 struct dwc3_request *req = to_dwc3_request(request);
865 trace_dwc3_free_request(req);
869 static u32 dwc3_calc_trbs_left(struct dwc3_ep *dep);
871 static void __dwc3_prepare_one_trb(struct dwc3_ep *dep, struct dwc3_trb *trb,
872 dma_addr_t dma, unsigned length, unsigned chain, unsigned node,
873 unsigned stream_id, unsigned short_not_ok, unsigned no_interrupt)
875 struct dwc3 *dwc = dep->dwc;
876 struct usb_gadget *gadget = &dwc->gadget;
877 enum usb_device_speed speed = gadget->speed;
879 dwc3_ep_inc_enq(dep);
881 trb->size = DWC3_TRB_SIZE_LENGTH(length);
882 trb->bpl = lower_32_bits(dma);
883 trb->bph = upper_32_bits(dma);
885 switch (usb_endpoint_type(dep->endpoint.desc)) {
886 case USB_ENDPOINT_XFER_CONTROL:
887 trb->ctrl = DWC3_TRBCTL_CONTROL_SETUP;
890 case USB_ENDPOINT_XFER_ISOC:
892 trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS_FIRST;
895 * USB Specification 2.0 Section 5.9.2 states that: "If
896 * there is only a single transaction in the microframe,
897 * only a DATA0 data packet PID is used. If there are
898 * two transactions per microframe, DATA1 is used for
899 * the first transaction data packet and DATA0 is used
900 * for the second transaction data packet. If there are
901 * three transactions per microframe, DATA2 is used for
902 * the first transaction data packet, DATA1 is used for
903 * the second, and DATA0 is used for the third."
905 * IOW, we should satisfy the following cases:
907 * 1) length <= maxpacket
910 * 2) maxpacket < length <= (2 * maxpacket)
913 * 3) (2 * maxpacket) < length <= (3 * maxpacket)
914 * - DATA2, DATA1, DATA0
916 if (speed == USB_SPEED_HIGH) {
917 struct usb_ep *ep = &dep->endpoint;
918 unsigned int mult = 2;
919 unsigned int maxp = usb_endpoint_maxp(ep->desc);
921 if (length <= (2 * maxp))
927 trb->size |= DWC3_TRB_SIZE_PCM1(mult);
930 trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS;
933 /* always enable Interrupt on Missed ISOC */
934 trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI;
937 case USB_ENDPOINT_XFER_BULK:
938 case USB_ENDPOINT_XFER_INT:
939 trb->ctrl = DWC3_TRBCTL_NORMAL;
943 * This is only possible with faulty memory because we
944 * checked it already :)
946 dev_WARN(dwc->dev, "Unknown endpoint type %d\n",
947 usb_endpoint_type(dep->endpoint.desc));
950 /* always enable Continue on Short Packet */
951 if (usb_endpoint_dir_out(dep->endpoint.desc)) {
952 trb->ctrl |= DWC3_TRB_CTRL_CSP;
955 trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI;
958 if ((!no_interrupt && !chain) ||
959 (dwc3_calc_trbs_left(dep) == 0))
960 trb->ctrl |= DWC3_TRB_CTRL_IOC;
963 trb->ctrl |= DWC3_TRB_CTRL_CHN;
965 if (usb_endpoint_xfer_bulk(dep->endpoint.desc) && dep->stream_capable)
966 trb->ctrl |= DWC3_TRB_CTRL_SID_SOFN(stream_id);
968 trb->ctrl |= DWC3_TRB_CTRL_HWO;
970 trace_dwc3_prepare_trb(dep, trb);
974 * dwc3_prepare_one_trb - setup one TRB from one request
975 * @dep: endpoint for which this request is prepared
976 * @req: dwc3_request pointer
977 * @chain: should this TRB be chained to the next?
978 * @node: only for isochronous endpoints. First TRB needs different type.
980 static void dwc3_prepare_one_trb(struct dwc3_ep *dep,
981 struct dwc3_request *req, unsigned chain, unsigned node)
983 struct dwc3_trb *trb;
986 unsigned stream_id = req->request.stream_id;
987 unsigned short_not_ok = req->request.short_not_ok;
988 unsigned no_interrupt = req->request.no_interrupt;
990 if (req->request.num_sgs > 0) {
991 length = sg_dma_len(req->start_sg);
992 dma = sg_dma_address(req->start_sg);
994 length = req->request.length;
995 dma = req->request.dma;
998 trb = &dep->trb_pool[dep->trb_enqueue];
1001 dwc3_gadget_move_started_request(req);
1003 req->trb_dma = dwc3_trb_dma_offset(dep, trb);
1006 __dwc3_prepare_one_trb(dep, trb, dma, length, chain, node,
1007 stream_id, short_not_ok, no_interrupt);
1011 * dwc3_ep_prev_trb - returns the previous TRB in the ring
1012 * @dep: The endpoint with the TRB ring
1013 * @index: The index of the current TRB in the ring
1015 * Returns the TRB prior to the one pointed to by the index. If the
1016 * index is 0, we will wrap backwards, skip the link TRB, and return
1017 * the one just before that.
1019 static struct dwc3_trb *dwc3_ep_prev_trb(struct dwc3_ep *dep, u8 index)
1024 tmp = DWC3_TRB_NUM - 1;
1026 return &dep->trb_pool[tmp - 1];
1029 static u32 dwc3_calc_trbs_left(struct dwc3_ep *dep)
1031 struct dwc3_trb *tmp;
1035 * If enqueue & dequeue are equal than it is either full or empty.
1037 * One way to know for sure is if the TRB right before us has HWO bit
1038 * set or not. If it has, then we're definitely full and can't fit any
1039 * more transfers in our ring.
1041 if (dep->trb_enqueue == dep->trb_dequeue) {
1042 tmp = dwc3_ep_prev_trb(dep, dep->trb_enqueue);
1043 if (tmp->ctrl & DWC3_TRB_CTRL_HWO)
1046 return DWC3_TRB_NUM - 1;
1049 trbs_left = dep->trb_dequeue - dep->trb_enqueue;
1050 trbs_left &= (DWC3_TRB_NUM - 1);
1052 if (dep->trb_dequeue < dep->trb_enqueue)
1058 static void dwc3_prepare_one_trb_sg(struct dwc3_ep *dep,
1059 struct dwc3_request *req)
1061 struct scatterlist *sg = req->start_sg;
1062 struct scatterlist *s;
1065 unsigned int remaining = req->request.num_mapped_sgs
1066 - req->num_queued_sgs;
1068 for_each_sg(sg, s, remaining, i) {
1069 unsigned int length = req->request.length;
1070 unsigned int maxp = usb_endpoint_maxp(dep->endpoint.desc);
1071 unsigned int rem = length % maxp;
1072 unsigned chain = true;
1077 if (rem && usb_endpoint_dir_out(dep->endpoint.desc) && !chain) {
1078 struct dwc3 *dwc = dep->dwc;
1079 struct dwc3_trb *trb;
1081 req->unaligned = true;
1083 /* prepare normal TRB */
1084 dwc3_prepare_one_trb(dep, req, true, i);
1086 /* Now prepare one extra TRB to align transfer size */
1087 trb = &dep->trb_pool[dep->trb_enqueue];
1088 __dwc3_prepare_one_trb(dep, trb, dwc->bounce_addr,
1089 maxp - rem, false, 0,
1090 req->request.stream_id,
1091 req->request.short_not_ok,
1092 req->request.no_interrupt);
1094 dwc3_prepare_one_trb(dep, req, chain, i);
1098 * There can be a situation where all sgs in sglist are not
1099 * queued because of insufficient trb number. To handle this
1100 * case, update start_sg to next sg to be queued, so that
1101 * we have free trbs we can continue queuing from where we
1102 * previously stopped
1105 req->start_sg = sg_next(s);
1107 req->num_queued_sgs++;
1109 if (!dwc3_calc_trbs_left(dep))
1114 static void dwc3_prepare_one_trb_linear(struct dwc3_ep *dep,
1115 struct dwc3_request *req)
1117 unsigned int length = req->request.length;
1118 unsigned int maxp = usb_endpoint_maxp(dep->endpoint.desc);
1119 unsigned int rem = length % maxp;
1121 if (rem && usb_endpoint_dir_out(dep->endpoint.desc)) {
1122 struct dwc3 *dwc = dep->dwc;
1123 struct dwc3_trb *trb;
1125 req->unaligned = true;
1127 /* prepare normal TRB */
1128 dwc3_prepare_one_trb(dep, req, true, 0);
1130 /* Now prepare one extra TRB to align transfer size */
1131 trb = &dep->trb_pool[dep->trb_enqueue];
1132 __dwc3_prepare_one_trb(dep, trb, dwc->bounce_addr, maxp - rem,
1133 false, 0, req->request.stream_id,
1134 req->request.short_not_ok,
1135 req->request.no_interrupt);
1136 } else if (req->request.zero && req->request.length &&
1137 (IS_ALIGNED(req->request.length,dep->endpoint.maxpacket))) {
1138 struct dwc3 *dwc = dep->dwc;
1139 struct dwc3_trb *trb;
1143 /* prepare normal TRB */
1144 dwc3_prepare_one_trb(dep, req, true, 0);
1146 /* Now prepare one extra TRB to handle ZLP */
1147 trb = &dep->trb_pool[dep->trb_enqueue];
1148 __dwc3_prepare_one_trb(dep, trb, dwc->bounce_addr, 0,
1149 false, 0, req->request.stream_id,
1150 req->request.short_not_ok,
1151 req->request.no_interrupt);
1153 dwc3_prepare_one_trb(dep, req, false, 0);
1158 * dwc3_prepare_trbs - setup TRBs from requests
1159 * @dep: endpoint for which requests are being prepared
1161 * The function goes through the requests list and sets up TRBs for the
1162 * transfers. The function returns once there are no more TRBs available or
1163 * it runs out of requests.
1165 static void dwc3_prepare_trbs(struct dwc3_ep *dep)
1167 struct dwc3_request *req, *n;
1169 BUILD_BUG_ON_NOT_POWER_OF_2(DWC3_TRB_NUM);
1172 * We can get in a situation where there's a request in the started list
1173 * but there weren't enough TRBs to fully kick it in the first time
1174 * around, so it has been waiting for more TRBs to be freed up.
1176 * In that case, we should check if we have a request with pending_sgs
1177 * in the started list and prepare TRBs for that request first,
1178 * otherwise we will prepare TRBs completely out of order and that will
1181 list_for_each_entry(req, &dep->started_list, list) {
1182 if (req->num_pending_sgs > 0)
1183 dwc3_prepare_one_trb_sg(dep, req);
1185 if (!dwc3_calc_trbs_left(dep))
1189 list_for_each_entry_safe(req, n, &dep->pending_list, list) {
1190 struct dwc3 *dwc = dep->dwc;
1193 ret = usb_gadget_map_request_by_dev(dwc->sysdev, &req->request,
1198 req->sg = req->request.sg;
1199 req->start_sg = req->sg;
1200 req->num_queued_sgs = 0;
1201 req->num_pending_sgs = req->request.num_mapped_sgs;
1203 if (req->num_pending_sgs > 0)
1204 dwc3_prepare_one_trb_sg(dep, req);
1206 dwc3_prepare_one_trb_linear(dep, req);
1208 if (!dwc3_calc_trbs_left(dep))
1213 static int __dwc3_gadget_kick_transfer(struct dwc3_ep *dep)
1215 struct dwc3_gadget_ep_cmd_params params;
1216 struct dwc3_request *req;
1221 if (!dwc3_calc_trbs_left(dep))
1224 starting = !(dep->flags & DWC3_EP_BUSY);
1226 dwc3_prepare_trbs(dep);
1227 req = next_request(&dep->started_list);
1229 dep->flags |= DWC3_EP_PENDING_REQUEST;
1233 memset(¶ms, 0, sizeof(params));
1236 params.param0 = upper_32_bits(req->trb_dma);
1237 params.param1 = lower_32_bits(req->trb_dma);
1238 cmd = DWC3_DEPCMD_STARTTRANSFER;
1240 if (usb_endpoint_xfer_isoc(dep->endpoint.desc))
1241 cmd |= DWC3_DEPCMD_PARAM(dep->frame_number);
1243 cmd = DWC3_DEPCMD_UPDATETRANSFER |
1244 DWC3_DEPCMD_PARAM(dep->resource_index);
1247 ret = dwc3_send_gadget_ep_cmd(dep, cmd, ¶ms);
1250 * FIXME we need to iterate over the list of requests
1251 * here and stop, unmap, free and del each of the linked
1252 * requests instead of what we do now.
1255 memset(req->trb, 0, sizeof(struct dwc3_trb));
1256 dwc3_gadget_del_and_unmap_request(dep, req, ret);
1260 dep->flags |= DWC3_EP_BUSY;
1263 dep->resource_index = dwc3_gadget_ep_get_transfer_index(dep);
1264 WARN_ON_ONCE(!dep->resource_index);
1270 static int __dwc3_gadget_get_frame(struct dwc3 *dwc)
1274 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1275 return DWC3_DSTS_SOFFN(reg);
1278 static void __dwc3_gadget_start_isoc(struct dwc3 *dwc,
1279 struct dwc3_ep *dep, u32 cur_uf)
1281 if (list_empty(&dep->pending_list)) {
1282 dev_info(dwc->dev, "%s: ran out of requests\n",
1284 dep->flags |= DWC3_EP_PENDING_REQUEST;
1289 * Schedule the first trb for one interval in the future or at
1290 * least 4 microframes.
1292 dep->frame_number = cur_uf + max_t(u32, 4, dep->interval);
1293 __dwc3_gadget_kick_transfer(dep);
1296 static void dwc3_gadget_start_isoc(struct dwc3 *dwc,
1297 struct dwc3_ep *dep, const struct dwc3_event_depevt *event)
1301 mask = ~(dep->interval - 1);
1302 cur_uf = event->parameters & mask;
1304 __dwc3_gadget_start_isoc(dwc, dep, cur_uf);
1307 static int __dwc3_gadget_ep_queue(struct dwc3_ep *dep, struct dwc3_request *req)
1309 struct dwc3 *dwc = dep->dwc;
1311 if (!dep->endpoint.desc) {
1312 dev_err(dwc->dev, "%s: can't queue to disabled endpoint\n",
1317 if (WARN(req->dep != dep, "request %pK belongs to '%s'\n",
1318 &req->request, req->dep->name))
1321 pm_runtime_get(dwc->dev);
1323 req->request.actual = 0;
1324 req->request.status = -EINPROGRESS;
1325 req->direction = dep->direction;
1326 req->epnum = dep->number;
1328 trace_dwc3_ep_queue(req);
1330 list_add_tail(&req->list, &dep->pending_list);
1333 * NOTICE: Isochronous endpoints should NEVER be prestarted. We must
1334 * wait for a XferNotReady event so we will know what's the current
1335 * (micro-)frame number.
1337 * Without this trick, we are very, very likely gonna get Bus Expiry
1338 * errors which will force us issue EndTransfer command.
1340 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
1341 if ((dep->flags & DWC3_EP_PENDING_REQUEST)) {
1342 if (dep->flags & DWC3_EP_TRANSFER_STARTED) {
1343 dwc3_stop_active_transfer(dwc, dep->number, true);
1344 dep->flags = DWC3_EP_ENABLED;
1348 cur_uf = __dwc3_gadget_get_frame(dwc);
1349 __dwc3_gadget_start_isoc(dwc, dep, cur_uf);
1350 dep->flags &= ~DWC3_EP_PENDING_REQUEST;
1355 if ((dep->flags & DWC3_EP_BUSY) &&
1356 !(dep->flags & DWC3_EP_MISSED_ISOC))
1363 return __dwc3_gadget_kick_transfer(dep);
1366 static int dwc3_gadget_ep_queue(struct usb_ep *ep, struct usb_request *request,
1369 struct dwc3_request *req = to_dwc3_request(request);
1370 struct dwc3_ep *dep = to_dwc3_ep(ep);
1371 struct dwc3 *dwc = dep->dwc;
1373 unsigned long flags;
1377 spin_lock_irqsave(&dwc->lock, flags);
1378 ret = __dwc3_gadget_ep_queue(dep, req);
1379 spin_unlock_irqrestore(&dwc->lock, flags);
1384 static int dwc3_gadget_ep_dequeue(struct usb_ep *ep,
1385 struct usb_request *request)
1387 struct dwc3_request *req = to_dwc3_request(request);
1388 struct dwc3_request *r = NULL;
1390 struct dwc3_ep *dep = to_dwc3_ep(ep);
1391 struct dwc3 *dwc = dep->dwc;
1393 unsigned long flags;
1396 trace_dwc3_ep_dequeue(req);
1398 spin_lock_irqsave(&dwc->lock, flags);
1400 list_for_each_entry(r, &dep->pending_list, list) {
1406 list_for_each_entry(r, &dep->started_list, list) {
1411 /* wait until it is processed */
1412 dwc3_stop_active_transfer(dwc, dep->number, true);
1415 * If request was already started, this means we had to
1416 * stop the transfer. With that we also need to ignore
1417 * all TRBs used by the request, however TRBs can only
1418 * be modified after completion of END_TRANSFER
1419 * command. So what we do here is that we wait for
1420 * END_TRANSFER completion and only after that, we jump
1421 * over TRBs by clearing HWO and incrementing dequeue
1424 * Note that we have 2 possible types of transfers here:
1426 * i) Linear buffer request
1427 * ii) SG-list based request
1429 * SG-list based requests will have r->num_pending_sgs
1430 * set to a valid number (> 0). Linear requests,
1431 * normally use a single TRB.
1433 * For each of these two cases, if r->unaligned flag is
1434 * set, one extra TRB has been used to align transfer
1435 * size to wMaxPacketSize.
1437 * All of these cases need to be taken into
1438 * consideration so we don't mess up our TRB ring
1441 wait_event_lock_irq(dep->wait_end_transfer,
1442 !(dep->flags & DWC3_EP_END_TRANSFER_PENDING),
1448 if (r->num_pending_sgs) {
1449 struct dwc3_trb *trb;
1452 for (i = 0; i < r->num_pending_sgs; i++) {
1454 trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
1455 dwc3_ep_inc_deq(dep);
1458 if (r->unaligned || r->zero) {
1459 trb = r->trb + r->num_pending_sgs + 1;
1460 trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
1461 dwc3_ep_inc_deq(dep);
1464 struct dwc3_trb *trb = r->trb;
1466 trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
1467 dwc3_ep_inc_deq(dep);
1469 if (r->unaligned || r->zero) {
1471 trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
1472 dwc3_ep_inc_deq(dep);
1477 dev_err(dwc->dev, "request %pK was not queued to %s\n",
1484 /* giveback the request */
1486 dwc3_gadget_giveback(dep, req, -ECONNRESET);
1489 spin_unlock_irqrestore(&dwc->lock, flags);
1494 int __dwc3_gadget_ep_set_halt(struct dwc3_ep *dep, int value, int protocol)
1496 struct dwc3_gadget_ep_cmd_params params;
1497 struct dwc3 *dwc = dep->dwc;
1500 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
1501 dev_err(dwc->dev, "%s is of Isochronous type\n", dep->name);
1505 memset(¶ms, 0x00, sizeof(params));
1508 struct dwc3_trb *trb;
1510 unsigned transfer_in_flight;
1513 if (dep->flags & DWC3_EP_STALL)
1516 if (dep->number > 1)
1517 trb = dwc3_ep_prev_trb(dep, dep->trb_enqueue);
1519 trb = &dwc->ep0_trb[dep->trb_enqueue];
1521 transfer_in_flight = trb->ctrl & DWC3_TRB_CTRL_HWO;
1522 started = !list_empty(&dep->started_list);
1524 if (!protocol && ((dep->direction && transfer_in_flight) ||
1525 (!dep->direction && started))) {
1529 ret = dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETSTALL,
1532 dev_err(dwc->dev, "failed to set STALL on %s\n",
1535 dep->flags |= DWC3_EP_STALL;
1537 if (!(dep->flags & DWC3_EP_STALL))
1540 ret = dwc3_send_clear_stall_ep_cmd(dep);
1542 dev_err(dwc->dev, "failed to clear STALL on %s\n",
1545 dep->flags &= ~(DWC3_EP_STALL | DWC3_EP_WEDGE);
1551 static int dwc3_gadget_ep_set_halt(struct usb_ep *ep, int value)
1553 struct dwc3_ep *dep = to_dwc3_ep(ep);
1554 struct dwc3 *dwc = dep->dwc;
1556 unsigned long flags;
1560 spin_lock_irqsave(&dwc->lock, flags);
1561 ret = __dwc3_gadget_ep_set_halt(dep, value, false);
1562 spin_unlock_irqrestore(&dwc->lock, flags);
1567 static int dwc3_gadget_ep_set_wedge(struct usb_ep *ep)
1569 struct dwc3_ep *dep = to_dwc3_ep(ep);
1570 struct dwc3 *dwc = dep->dwc;
1571 unsigned long flags;
1574 spin_lock_irqsave(&dwc->lock, flags);
1575 dep->flags |= DWC3_EP_WEDGE;
1577 if (dep->number == 0 || dep->number == 1)
1578 ret = __dwc3_gadget_ep0_set_halt(ep, 1);
1580 ret = __dwc3_gadget_ep_set_halt(dep, 1, false);
1581 spin_unlock_irqrestore(&dwc->lock, flags);
1586 /* -------------------------------------------------------------------------- */
1588 static struct usb_endpoint_descriptor dwc3_gadget_ep0_desc = {
1589 .bLength = USB_DT_ENDPOINT_SIZE,
1590 .bDescriptorType = USB_DT_ENDPOINT,
1591 .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
1594 static const struct usb_ep_ops dwc3_gadget_ep0_ops = {
1595 .enable = dwc3_gadget_ep0_enable,
1596 .disable = dwc3_gadget_ep0_disable,
1597 .alloc_request = dwc3_gadget_ep_alloc_request,
1598 .free_request = dwc3_gadget_ep_free_request,
1599 .queue = dwc3_gadget_ep0_queue,
1600 .dequeue = dwc3_gadget_ep_dequeue,
1601 .set_halt = dwc3_gadget_ep0_set_halt,
1602 .set_wedge = dwc3_gadget_ep_set_wedge,
1605 static const struct usb_ep_ops dwc3_gadget_ep_ops = {
1606 .enable = dwc3_gadget_ep_enable,
1607 .disable = dwc3_gadget_ep_disable,
1608 .alloc_request = dwc3_gadget_ep_alloc_request,
1609 .free_request = dwc3_gadget_ep_free_request,
1610 .queue = dwc3_gadget_ep_queue,
1611 .dequeue = dwc3_gadget_ep_dequeue,
1612 .set_halt = dwc3_gadget_ep_set_halt,
1613 .set_wedge = dwc3_gadget_ep_set_wedge,
1616 /* -------------------------------------------------------------------------- */
1618 static int dwc3_gadget_get_frame(struct usb_gadget *g)
1620 struct dwc3 *dwc = gadget_to_dwc(g);
1622 return __dwc3_gadget_get_frame(dwc);
1625 static int __dwc3_gadget_wakeup(struct dwc3 *dwc)
1636 * According to the Databook Remote wakeup request should
1637 * be issued only when the device is in early suspend state.
1639 * We can check that via USB Link State bits in DSTS register.
1641 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1643 speed = reg & DWC3_DSTS_CONNECTSPD;
1644 if ((speed == DWC3_DSTS_SUPERSPEED) ||
1645 (speed == DWC3_DSTS_SUPERSPEED_PLUS))
1648 link_state = DWC3_DSTS_USBLNKST(reg);
1650 switch (link_state) {
1651 case DWC3_LINK_STATE_RX_DET: /* in HS, means Early Suspend */
1652 case DWC3_LINK_STATE_U3: /* in HS, means SUSPEND */
1658 ret = dwc3_gadget_set_link_state(dwc, DWC3_LINK_STATE_RECOV);
1660 dev_err(dwc->dev, "failed to put link in Recovery\n");
1664 /* Recent versions do this automatically */
1665 if (dwc->revision < DWC3_REVISION_194A) {
1666 /* write zeroes to Link Change Request */
1667 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
1668 reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK;
1669 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1672 /* poll until Link State changes to ON */
1676 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1678 /* in HS, means ON */
1679 if (DWC3_DSTS_USBLNKST(reg) == DWC3_LINK_STATE_U0)
1683 if (DWC3_DSTS_USBLNKST(reg) != DWC3_LINK_STATE_U0) {
1684 dev_err(dwc->dev, "failed to send remote wakeup\n");
1691 static int dwc3_gadget_wakeup(struct usb_gadget *g)
1693 struct dwc3 *dwc = gadget_to_dwc(g);
1694 unsigned long flags;
1697 spin_lock_irqsave(&dwc->lock, flags);
1698 ret = __dwc3_gadget_wakeup(dwc);
1699 spin_unlock_irqrestore(&dwc->lock, flags);
1704 static int dwc3_gadget_set_selfpowered(struct usb_gadget *g,
1707 struct dwc3 *dwc = gadget_to_dwc(g);
1708 unsigned long flags;
1710 spin_lock_irqsave(&dwc->lock, flags);
1711 g->is_selfpowered = !!is_selfpowered;
1712 spin_unlock_irqrestore(&dwc->lock, flags);
1717 static int dwc3_gadget_run_stop(struct dwc3 *dwc, int is_on, int suspend)
1722 if (pm_runtime_suspended(dwc->dev))
1725 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
1727 if (dwc->revision <= DWC3_REVISION_187A) {
1728 reg &= ~DWC3_DCTL_TRGTULST_MASK;
1729 reg |= DWC3_DCTL_TRGTULST_RX_DET;
1732 if (dwc->revision >= DWC3_REVISION_194A)
1733 reg &= ~DWC3_DCTL_KEEP_CONNECT;
1734 reg |= DWC3_DCTL_RUN_STOP;
1736 if (dwc->has_hibernation)
1737 reg |= DWC3_DCTL_KEEP_CONNECT;
1739 dwc->pullups_connected = true;
1741 reg &= ~DWC3_DCTL_RUN_STOP;
1743 if (dwc->has_hibernation && !suspend)
1744 reg &= ~DWC3_DCTL_KEEP_CONNECT;
1746 dwc->pullups_connected = false;
1749 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1752 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1753 reg &= DWC3_DSTS_DEVCTRLHLT;
1754 } while (--timeout && !(!is_on ^ !reg));
1762 static int dwc3_gadget_pullup(struct usb_gadget *g, int is_on)
1764 struct dwc3 *dwc = gadget_to_dwc(g);
1765 unsigned long flags;
1771 * Per databook, when we want to stop the gadget, if a control transfer
1772 * is still in process, complete it and get the core into setup phase.
1774 if (!is_on && dwc->ep0state != EP0_SETUP_PHASE) {
1775 reinit_completion(&dwc->ep0_in_setup);
1777 ret = wait_for_completion_timeout(&dwc->ep0_in_setup,
1778 msecs_to_jiffies(DWC3_PULL_UP_TIMEOUT));
1780 dev_err(dwc->dev, "timed out waiting for SETUP phase\n");
1785 spin_lock_irqsave(&dwc->lock, flags);
1786 ret = dwc3_gadget_run_stop(dwc, is_on, false);
1787 spin_unlock_irqrestore(&dwc->lock, flags);
1792 static void dwc3_gadget_enable_irq(struct dwc3 *dwc)
1796 /* Enable all but Start and End of Frame IRQs */
1797 reg = (DWC3_DEVTEN_VNDRDEVTSTRCVEDEN |
1798 DWC3_DEVTEN_EVNTOVERFLOWEN |
1799 DWC3_DEVTEN_CMDCMPLTEN |
1800 DWC3_DEVTEN_ERRTICERREN |
1801 DWC3_DEVTEN_WKUPEVTEN |
1802 DWC3_DEVTEN_CONNECTDONEEN |
1803 DWC3_DEVTEN_USBRSTEN |
1804 DWC3_DEVTEN_DISCONNEVTEN);
1806 if (dwc->revision < DWC3_REVISION_250A)
1807 reg |= DWC3_DEVTEN_ULSTCNGEN;
1809 dwc3_writel(dwc->regs, DWC3_DEVTEN, reg);
1812 static void dwc3_gadget_disable_irq(struct dwc3 *dwc)
1814 /* mask all interrupts */
1815 dwc3_writel(dwc->regs, DWC3_DEVTEN, 0x00);
1818 static irqreturn_t dwc3_interrupt(int irq, void *_dwc);
1819 static irqreturn_t dwc3_thread_interrupt(int irq, void *_dwc);
1822 * dwc3_gadget_setup_nump - calculate and initialize NUMP field of %DWC3_DCFG
1823 * @dwc: pointer to our context structure
1825 * The following looks like complex but it's actually very simple. In order to
1826 * calculate the number of packets we can burst at once on OUT transfers, we're
1827 * gonna use RxFIFO size.
1829 * To calculate RxFIFO size we need two numbers:
1830 * MDWIDTH = size, in bits, of the internal memory bus
1831 * RAM2_DEPTH = depth, in MDWIDTH, of internal RAM2 (where RxFIFO sits)
1833 * Given these two numbers, the formula is simple:
1835 * RxFIFO Size = (RAM2_DEPTH * MDWIDTH / 8) - 24 - 16;
1837 * 24 bytes is for 3x SETUP packets
1838 * 16 bytes is a clock domain crossing tolerance
1840 * Given RxFIFO Size, NUMP = RxFIFOSize / 1024;
1842 static void dwc3_gadget_setup_nump(struct dwc3 *dwc)
1849 ram2_depth = DWC3_GHWPARAMS7_RAM2_DEPTH(dwc->hwparams.hwparams7);
1850 mdwidth = DWC3_GHWPARAMS0_MDWIDTH(dwc->hwparams.hwparams0);
1852 nump = ((ram2_depth * mdwidth / 8) - 24 - 16) / 1024;
1853 nump = min_t(u32, nump, 16);
1856 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
1857 reg &= ~DWC3_DCFG_NUMP_MASK;
1858 reg |= nump << DWC3_DCFG_NUMP_SHIFT;
1859 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
1862 static int __dwc3_gadget_start(struct dwc3 *dwc)
1864 struct dwc3_ep *dep;
1869 * Use IMOD if enabled via dwc->imod_interval. Otherwise, if
1870 * the core supports IMOD, disable it.
1872 if (dwc->imod_interval) {
1873 dwc3_writel(dwc->regs, DWC3_DEV_IMOD(0), dwc->imod_interval);
1874 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), DWC3_GEVNTCOUNT_EHB);
1875 } else if (dwc3_has_imod(dwc)) {
1876 dwc3_writel(dwc->regs, DWC3_DEV_IMOD(0), 0);
1880 * We are telling dwc3 that we want to use DCFG.NUMP as ACK TP's NUMP
1881 * field instead of letting dwc3 itself calculate that automatically.
1883 * This way, we maximize the chances that we'll be able to get several
1884 * bursts of data without going through any sort of endpoint throttling.
1886 reg = dwc3_readl(dwc->regs, DWC3_GRXTHRCFG);
1887 if (dwc3_is_usb31(dwc))
1888 reg &= ~DWC31_GRXTHRCFG_PKTCNTSEL;
1890 reg &= ~DWC3_GRXTHRCFG_PKTCNTSEL;
1892 dwc3_writel(dwc->regs, DWC3_GRXTHRCFG, reg);
1894 dwc3_gadget_setup_nump(dwc);
1896 /* Start with SuperSpeed Default */
1897 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
1900 ret = __dwc3_gadget_ep_enable(dep, false, false);
1902 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
1907 ret = __dwc3_gadget_ep_enable(dep, false, false);
1909 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
1913 /* begin to receive SETUP packets */
1914 dwc->ep0state = EP0_SETUP_PHASE;
1915 dwc3_ep0_out_start(dwc);
1917 dwc3_gadget_enable_irq(dwc);
1922 __dwc3_gadget_ep_disable(dwc->eps[0]);
1928 static int dwc3_gadget_start(struct usb_gadget *g,
1929 struct usb_gadget_driver *driver)
1931 struct dwc3 *dwc = gadget_to_dwc(g);
1932 unsigned long flags;
1936 irq = dwc->irq_gadget;
1937 ret = request_threaded_irq(irq, dwc3_interrupt, dwc3_thread_interrupt,
1938 IRQF_SHARED, "dwc3", dwc->ev_buf);
1940 dev_err(dwc->dev, "failed to request irq #%d --> %d\n",
1945 spin_lock_irqsave(&dwc->lock, flags);
1946 if (dwc->gadget_driver) {
1947 dev_err(dwc->dev, "%s is already bound to %s\n",
1949 dwc->gadget_driver->driver.name);
1954 dwc->gadget_driver = driver;
1956 if (pm_runtime_active(dwc->dev))
1957 __dwc3_gadget_start(dwc);
1959 spin_unlock_irqrestore(&dwc->lock, flags);
1964 spin_unlock_irqrestore(&dwc->lock, flags);
1971 static void __dwc3_gadget_stop(struct dwc3 *dwc)
1973 dwc3_gadget_disable_irq(dwc);
1974 __dwc3_gadget_ep_disable(dwc->eps[0]);
1975 __dwc3_gadget_ep_disable(dwc->eps[1]);
1978 static int dwc3_gadget_stop(struct usb_gadget *g)
1980 struct dwc3 *dwc = gadget_to_dwc(g);
1981 unsigned long flags;
1985 spin_lock_irqsave(&dwc->lock, flags);
1987 if (pm_runtime_suspended(dwc->dev))
1990 __dwc3_gadget_stop(dwc);
1992 for (epnum = 2; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
1993 struct dwc3_ep *dep = dwc->eps[epnum];
1999 if (!(dep->flags & DWC3_EP_END_TRANSFER_PENDING))
2002 ret = wait_event_interruptible_lock_irq_timeout(dep->wait_end_transfer,
2003 !(dep->flags & DWC3_EP_END_TRANSFER_PENDING),
2004 dwc->lock, msecs_to_jiffies(5));
2007 /* Timed out or interrupted! There's nothing much
2008 * we can do so we just log here and print which
2009 * endpoints timed out at the end.
2011 tmo_eps |= 1 << epnum;
2012 dep->flags &= DWC3_EP_END_TRANSFER_PENDING;
2018 "end transfer timed out on endpoints 0x%x [bitmap]\n",
2023 dwc->gadget_driver = NULL;
2024 spin_unlock_irqrestore(&dwc->lock, flags);
2026 free_irq(dwc->irq_gadget, dwc->ev_buf);
2031 static void dwc3_gadget_set_speed(struct usb_gadget *g,
2032 enum usb_device_speed speed)
2034 struct dwc3 *dwc = gadget_to_dwc(g);
2035 unsigned long flags;
2038 spin_lock_irqsave(&dwc->lock, flags);
2039 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2040 reg &= ~(DWC3_DCFG_SPEED_MASK);
2043 * WORKAROUND: DWC3 revision < 2.20a have an issue
2044 * which would cause metastability state on Run/Stop
2045 * bit if we try to force the IP to USB2-only mode.
2047 * Because of that, we cannot configure the IP to any
2048 * speed other than the SuperSpeed
2052 * STAR#9000525659: Clock Domain Crossing on DCTL in
2055 if (dwc->revision < DWC3_REVISION_220A &&
2056 !dwc->dis_metastability_quirk) {
2057 reg |= DWC3_DCFG_SUPERSPEED;
2061 reg |= DWC3_DCFG_LOWSPEED;
2063 case USB_SPEED_FULL:
2064 reg |= DWC3_DCFG_FULLSPEED;
2066 case USB_SPEED_HIGH:
2067 reg |= DWC3_DCFG_HIGHSPEED;
2069 case USB_SPEED_SUPER:
2070 reg |= DWC3_DCFG_SUPERSPEED;
2072 case USB_SPEED_SUPER_PLUS:
2073 if (dwc3_is_usb31(dwc))
2074 reg |= DWC3_DCFG_SUPERSPEED_PLUS;
2076 reg |= DWC3_DCFG_SUPERSPEED;
2079 dev_err(dwc->dev, "invalid speed (%d)\n", speed);
2081 if (dwc->revision & DWC3_REVISION_IS_DWC31)
2082 reg |= DWC3_DCFG_SUPERSPEED_PLUS;
2084 reg |= DWC3_DCFG_SUPERSPEED;
2087 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
2089 spin_unlock_irqrestore(&dwc->lock, flags);
2092 static const struct usb_gadget_ops dwc3_gadget_ops = {
2093 .get_frame = dwc3_gadget_get_frame,
2094 .wakeup = dwc3_gadget_wakeup,
2095 .set_selfpowered = dwc3_gadget_set_selfpowered,
2096 .pullup = dwc3_gadget_pullup,
2097 .udc_start = dwc3_gadget_start,
2098 .udc_stop = dwc3_gadget_stop,
2099 .udc_set_speed = dwc3_gadget_set_speed,
2102 /* -------------------------------------------------------------------------- */
2104 static int dwc3_gadget_init_endpoints(struct dwc3 *dwc, u8 total)
2106 struct dwc3_ep *dep;
2109 INIT_LIST_HEAD(&dwc->gadget.ep_list);
2111 for (epnum = 0; epnum < total; epnum++) {
2112 bool direction = epnum & 1;
2113 u8 num = epnum >> 1;
2115 dep = kzalloc(sizeof(*dep), GFP_KERNEL);
2120 dep->number = epnum;
2121 dep->direction = direction;
2122 dep->regs = dwc->regs + DWC3_DEP_BASE(epnum);
2123 dwc->eps[epnum] = dep;
2125 snprintf(dep->name, sizeof(dep->name), "ep%u%s", num,
2126 direction ? "in" : "out");
2128 dep->endpoint.name = dep->name;
2130 if (!(dep->number > 1)) {
2131 dep->endpoint.desc = &dwc3_gadget_ep0_desc;
2132 dep->endpoint.comp_desc = NULL;
2135 spin_lock_init(&dep->lock);
2138 usb_ep_set_maxpacket_limit(&dep->endpoint, 512);
2139 dep->endpoint.maxburst = 1;
2140 dep->endpoint.ops = &dwc3_gadget_ep0_ops;
2142 dwc->gadget.ep0 = &dep->endpoint;
2143 } else if (direction) {
2149 mdwidth = DWC3_MDWIDTH(dwc->hwparams.hwparams0);
2150 /* MDWIDTH is represented in bits, we need it in bytes */
2153 size = dwc3_readl(dwc->regs, DWC3_GTXFIFOSIZ(num));
2154 if (dwc3_is_usb31(dwc))
2155 size = DWC31_GTXFIFOSIZ_TXFDEF(size);
2157 size = DWC3_GTXFIFOSIZ_TXFDEF(size);
2159 /* FIFO Depth is in MDWDITH bytes. Multiply */
2162 kbytes = size / 1024;
2167 * FIFO sizes account an extra MDWIDTH * (kbytes + 1) bytes for
2168 * internal overhead. We don't really know how these are used,
2169 * but documentation say it exists.
2171 size -= mdwidth * (kbytes + 1);
2174 usb_ep_set_maxpacket_limit(&dep->endpoint, size);
2176 dep->endpoint.max_streams = 15;
2177 dep->endpoint.ops = &dwc3_gadget_ep_ops;
2178 list_add_tail(&dep->endpoint.ep_list,
2179 &dwc->gadget.ep_list);
2181 ret = dwc3_alloc_trb_pool(dep);
2187 usb_ep_set_maxpacket_limit(&dep->endpoint, 1024);
2188 dep->endpoint.max_streams = 15;
2189 dep->endpoint.ops = &dwc3_gadget_ep_ops;
2190 list_add_tail(&dep->endpoint.ep_list,
2191 &dwc->gadget.ep_list);
2193 ret = dwc3_alloc_trb_pool(dep);
2199 dep->endpoint.caps.type_control = true;
2201 dep->endpoint.caps.type_iso = true;
2202 dep->endpoint.caps.type_bulk = true;
2203 dep->endpoint.caps.type_int = true;
2206 dep->endpoint.caps.dir_in = direction;
2207 dep->endpoint.caps.dir_out = !direction;
2209 INIT_LIST_HEAD(&dep->pending_list);
2210 INIT_LIST_HEAD(&dep->started_list);
2216 static void dwc3_gadget_free_endpoints(struct dwc3 *dwc)
2218 struct dwc3_ep *dep;
2221 for (epnum = 0; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
2222 dep = dwc->eps[epnum];
2226 * Physical endpoints 0 and 1 are special; they form the
2227 * bi-directional USB endpoint 0.
2229 * For those two physical endpoints, we don't allocate a TRB
2230 * pool nor do we add them the endpoints list. Due to that, we
2231 * shouldn't do these two operations otherwise we would end up
2232 * with all sorts of bugs when removing dwc3.ko.
2234 if (epnum != 0 && epnum != 1) {
2235 dwc3_free_trb_pool(dep);
2236 list_del(&dep->endpoint.ep_list);
2243 /* -------------------------------------------------------------------------- */
2245 static int dwc3_gadget_ep_reclaim_completed_trb(struct dwc3 *dwc,
2246 struct dwc3_ep *dep, struct dwc3_request *req,
2247 struct dwc3_trb *trb, const struct dwc3_event_depevt *event,
2248 int status, int chain)
2251 unsigned int s_pkt = 0;
2252 unsigned int trb_status;
2254 dwc3_ep_inc_deq(dep);
2256 trace_dwc3_complete_trb(dep, trb);
2259 * If we're in the middle of series of chained TRBs and we
2260 * receive a short transfer along the way, DWC3 will skip
2261 * through all TRBs including the last TRB in the chain (the
2262 * where CHN bit is zero. DWC3 will also avoid clearing HWO
2263 * bit and SW has to do it manually.
2265 * We're going to do that here to avoid problems of HW trying
2266 * to use bogus TRBs for transfers.
2268 if (chain && (trb->ctrl & DWC3_TRB_CTRL_HWO))
2269 trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
2272 * If we're dealing with unaligned size OUT transfer, we will be left
2273 * with one TRB pending in the ring. We need to manually clear HWO bit
2276 if ((req->zero || req->unaligned) && (trb->ctrl & DWC3_TRB_CTRL_HWO)) {
2277 trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
2281 count = trb->size & DWC3_TRB_SIZE_MASK;
2282 req->remaining += count;
2284 if ((trb->ctrl & DWC3_TRB_CTRL_HWO) && status != -ESHUTDOWN)
2287 if (dep->direction) {
2289 trb_status = DWC3_TRB_SIZE_TRBSTS(trb->size);
2290 if (trb_status == DWC3_TRBSTS_MISSED_ISOC) {
2292 * If missed isoc occurred and there is
2293 * no request queued then issue END
2294 * TRANSFER, so that core generates
2295 * next xfernotready and we will issue
2296 * a fresh START TRANSFER.
2297 * If there are still queued request
2298 * then wait, do not issue either END
2299 * or UPDATE TRANSFER, just attach next
2300 * request in pending_list during
2301 * giveback.If any future queued request
2302 * is successfully transferred then we
2303 * will issue UPDATE TRANSFER for all
2304 * request in the pending_list.
2306 dep->flags |= DWC3_EP_MISSED_ISOC;
2308 dev_err(dwc->dev, "incomplete IN transfer %s\n",
2310 status = -ECONNRESET;
2313 dep->flags &= ~DWC3_EP_MISSED_ISOC;
2316 if (count && (event->status & DEPEVT_STATUS_SHORT))
2320 if (s_pkt && !chain)
2323 if ((event->status & DEPEVT_STATUS_IOC) &&
2324 (trb->ctrl & DWC3_TRB_CTRL_IOC))
2330 static int dwc3_gadget_ep_cleanup_completed_requests(struct dwc3 *dwc,
2331 struct dwc3_ep *dep, const struct dwc3_event_depevt *event,
2334 struct dwc3_request *req, *n;
2335 struct dwc3_trb *trb;
2339 list_for_each_entry_safe(req, n, &dep->started_list, list) {
2343 length = req->request.length;
2344 chain = req->num_pending_sgs > 0;
2346 struct scatterlist *sg = req->sg;
2347 struct scatterlist *s;
2348 unsigned int pending = req->num_pending_sgs;
2351 for_each_sg(sg, s, pending, i) {
2352 trb = &dep->trb_pool[dep->trb_dequeue];
2354 if (trb->ctrl & DWC3_TRB_CTRL_HWO)
2357 req->sg = sg_next(s);
2358 req->num_pending_sgs--;
2360 ret = dwc3_gadget_ep_reclaim_completed_trb(dwc,
2361 dep, req, trb, event, status,
2367 trb = &dep->trb_pool[dep->trb_dequeue];
2368 ret = dwc3_gadget_ep_reclaim_completed_trb(dwc, dep,
2369 req, trb, event, status, chain);
2372 if (req->unaligned || req->zero) {
2373 trb = &dep->trb_pool[dep->trb_dequeue];
2374 ret = dwc3_gadget_ep_reclaim_completed_trb(dwc, dep,
2375 req, trb, event, status, false);
2376 req->unaligned = false;
2380 req->request.actual = length - req->remaining;
2382 if (req->request.actual < length || req->num_pending_sgs) {
2384 * There could be a scenario where the whole req can't
2385 * be mapped into available TRB's. In that case, we need
2386 * to kick transfer again if (req->num_pending_sgs > 0)
2388 if (req->num_pending_sgs) {
2389 dev_WARN_ONCE(dwc->dev,
2390 (req->request.actual == length),
2391 "There are some pending sg's that needs to be queued again\n");
2392 return __dwc3_gadget_kick_transfer(dep);
2396 dwc3_gadget_giveback(dep, req, status);
2399 if ((event->status & DEPEVT_STATUS_IOC) &&
2400 (trb->ctrl & DWC3_TRB_CTRL_IOC))
2407 * Our endpoint might get disabled by another thread during
2408 * dwc3_gadget_giveback(). If that happens, we're just gonna return 1
2409 * early on so DWC3_EP_BUSY flag gets cleared
2411 if (!dep->endpoint.desc)
2414 if (usb_endpoint_xfer_isoc(dep->endpoint.desc) &&
2415 list_empty(&dep->started_list)) {
2416 if (list_empty(&dep->pending_list)) {
2418 * If there is no entry in request list then do
2419 * not issue END TRANSFER now. Just set PENDING
2420 * flag, so that END TRANSFER is issued when an
2421 * entry is added into request list.
2423 dep->flags = DWC3_EP_PENDING_REQUEST;
2425 dwc3_stop_active_transfer(dwc, dep->number, true);
2426 dep->flags = DWC3_EP_ENABLED;
2431 if (usb_endpoint_xfer_isoc(dep->endpoint.desc) && ioc)
2437 static void dwc3_gadget_endpoint_transfer_in_progress(struct dwc3 *dwc,
2438 struct dwc3_ep *dep, const struct dwc3_event_depevt *event)
2440 unsigned status = 0;
2443 if (event->status & DEPEVT_STATUS_BUSERR)
2444 status = -ECONNRESET;
2446 clean_busy = dwc3_gadget_ep_cleanup_completed_requests(dwc, dep, event,
2448 if (clean_busy && (!dep->endpoint.desc ||
2449 usb_endpoint_xfer_isoc(dep->endpoint.desc)))
2450 dep->flags &= ~DWC3_EP_BUSY;
2453 * WORKAROUND: This is the 2nd half of U1/U2 -> U0 workaround.
2454 * See dwc3_gadget_linksts_change_interrupt() for 1st half.
2456 if (dwc->revision < DWC3_REVISION_183A) {
2460 for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) {
2463 if (!(dep->flags & DWC3_EP_ENABLED))
2466 if (!list_empty(&dep->started_list))
2470 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2472 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2478 static void dwc3_endpoint_interrupt(struct dwc3 *dwc,
2479 const struct dwc3_event_depevt *event)
2481 struct dwc3_ep *dep;
2482 u8 epnum = event->endpoint_number;
2485 dep = dwc->eps[epnum];
2487 if (!(dep->flags & DWC3_EP_ENABLED)) {
2488 if (!(dep->flags & DWC3_EP_END_TRANSFER_PENDING))
2491 /* Handle only EPCMDCMPLT when EP disabled */
2492 if (event->endpoint_event != DWC3_DEPEVT_EPCMDCMPLT)
2496 if (epnum == 0 || epnum == 1) {
2497 dwc3_ep0_interrupt(dwc, event);
2501 switch (event->endpoint_event) {
2502 case DWC3_DEPEVT_XFERINPROGRESS:
2503 dwc3_gadget_endpoint_transfer_in_progress(dwc, dep, event);
2505 case DWC3_DEPEVT_XFERNOTREADY:
2506 if (!usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
2507 dev_err(dwc->dev, "XferNotReady for non-Isoc %s\n",
2512 dwc3_gadget_start_isoc(dwc, dep, event);
2514 case DWC3_DEPEVT_STREAMEVT:
2515 if (!usb_endpoint_xfer_bulk(dep->endpoint.desc)) {
2516 dev_err(dwc->dev, "Stream event for non-Bulk %s\n",
2521 case DWC3_DEPEVT_EPCMDCMPLT:
2522 cmd = DEPEVT_PARAMETER_CMD(event->parameters);
2524 if (cmd == DWC3_DEPCMD_ENDTRANSFER) {
2525 dep->flags &= ~DWC3_EP_END_TRANSFER_PENDING;
2526 wake_up(&dep->wait_end_transfer);
2529 case DWC3_DEPEVT_XFERCOMPLETE:
2530 case DWC3_DEPEVT_RXTXFIFOEVT:
2535 static void dwc3_disconnect_gadget(struct dwc3 *dwc)
2537 if (dwc->gadget_driver && dwc->gadget_driver->disconnect) {
2538 spin_unlock(&dwc->lock);
2539 dwc->gadget_driver->disconnect(&dwc->gadget);
2540 spin_lock(&dwc->lock);
2544 static void dwc3_suspend_gadget(struct dwc3 *dwc)
2546 if (dwc->gadget_driver && dwc->gadget_driver->suspend) {
2547 spin_unlock(&dwc->lock);
2548 dwc->gadget_driver->suspend(&dwc->gadget);
2549 spin_lock(&dwc->lock);
2553 static void dwc3_resume_gadget(struct dwc3 *dwc)
2555 if (dwc->gadget_driver && dwc->gadget_driver->resume) {
2556 spin_unlock(&dwc->lock);
2557 dwc->gadget_driver->resume(&dwc->gadget);
2558 spin_lock(&dwc->lock);
2562 static void dwc3_reset_gadget(struct dwc3 *dwc)
2564 if (!dwc->gadget_driver)
2567 if (dwc->gadget.speed != USB_SPEED_UNKNOWN) {
2568 spin_unlock(&dwc->lock);
2569 usb_gadget_udc_reset(&dwc->gadget, dwc->gadget_driver);
2570 spin_lock(&dwc->lock);
2574 static void dwc3_stop_active_transfer(struct dwc3 *dwc, u32 epnum, bool force)
2576 struct dwc3_ep *dep;
2577 struct dwc3_gadget_ep_cmd_params params;
2581 dep = dwc->eps[epnum];
2583 if ((dep->flags & DWC3_EP_END_TRANSFER_PENDING) ||
2584 !dep->resource_index)
2588 * NOTICE: We are violating what the Databook says about the
2589 * EndTransfer command. Ideally we would _always_ wait for the
2590 * EndTransfer Command Completion IRQ, but that's causing too
2591 * much trouble synchronizing between us and gadget driver.
2593 * We have discussed this with the IP Provider and it was
2594 * suggested to giveback all requests here, but give HW some
2595 * extra time to synchronize with the interconnect. We're using
2596 * an arbitrary 100us delay for that.
2598 * Note also that a similar handling was tested by Synopsys
2599 * (thanks a lot Paul) and nothing bad has come out of it.
2600 * In short, what we're doing is:
2602 * - Issue EndTransfer WITH CMDIOC bit set
2605 * As of IP version 3.10a of the DWC_usb3 IP, the controller
2606 * supports a mode to work around the above limitation. The
2607 * software can poll the CMDACT bit in the DEPCMD register
2608 * after issuing a EndTransfer command. This mode is enabled
2609 * by writing GUCTL2[14]. This polling is already done in the
2610 * dwc3_send_gadget_ep_cmd() function so if the mode is
2611 * enabled, the EndTransfer command will have completed upon
2612 * returning from this function and we don't need to delay for
2615 * This mode is NOT available on the DWC_usb31 IP.
2618 cmd = DWC3_DEPCMD_ENDTRANSFER;
2619 cmd |= force ? DWC3_DEPCMD_HIPRI_FORCERM : 0;
2620 cmd |= DWC3_DEPCMD_CMDIOC;
2621 cmd |= DWC3_DEPCMD_PARAM(dep->resource_index);
2622 memset(¶ms, 0, sizeof(params));
2623 ret = dwc3_send_gadget_ep_cmd(dep, cmd, ¶ms);
2625 dep->resource_index = 0;
2626 dep->flags &= ~DWC3_EP_BUSY;
2628 if (dwc3_is_usb31(dwc) || dwc->revision < DWC3_REVISION_310A) {
2629 dep->flags |= DWC3_EP_END_TRANSFER_PENDING;
2634 static void dwc3_clear_stall_all_ep(struct dwc3 *dwc)
2638 for (epnum = 1; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
2639 struct dwc3_ep *dep;
2642 dep = dwc->eps[epnum];
2646 if (!(dep->flags & DWC3_EP_STALL))
2649 dep->flags &= ~DWC3_EP_STALL;
2651 ret = dwc3_send_clear_stall_ep_cmd(dep);
2656 static void dwc3_gadget_disconnect_interrupt(struct dwc3 *dwc)
2660 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2661 reg &= ~DWC3_DCTL_INITU1ENA;
2662 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2664 reg &= ~DWC3_DCTL_INITU2ENA;
2665 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2667 dwc3_disconnect_gadget(dwc);
2669 dwc->gadget.speed = USB_SPEED_UNKNOWN;
2670 dwc->setup_packet_pending = false;
2671 usb_gadget_set_state(&dwc->gadget, USB_STATE_NOTATTACHED);
2673 dwc->connected = false;
2676 static void dwc3_gadget_reset_interrupt(struct dwc3 *dwc)
2680 dwc->connected = true;
2683 * WORKAROUND: DWC3 revisions <1.88a have an issue which
2684 * would cause a missing Disconnect Event if there's a
2685 * pending Setup Packet in the FIFO.
2687 * There's no suggested workaround on the official Bug
2688 * report, which states that "unless the driver/application
2689 * is doing any special handling of a disconnect event,
2690 * there is no functional issue".
2692 * Unfortunately, it turns out that we _do_ some special
2693 * handling of a disconnect event, namely complete all
2694 * pending transfers, notify gadget driver of the
2695 * disconnection, and so on.
2697 * Our suggested workaround is to follow the Disconnect
2698 * Event steps here, instead, based on a setup_packet_pending
2699 * flag. Such flag gets set whenever we have a SETUP_PENDING
2700 * status for EP0 TRBs and gets cleared on XferComplete for the
2705 * STAR#9000466709: RTL: Device : Disconnect event not
2706 * generated if setup packet pending in FIFO
2708 if (dwc->revision < DWC3_REVISION_188A) {
2709 if (dwc->setup_packet_pending)
2710 dwc3_gadget_disconnect_interrupt(dwc);
2713 dwc3_reset_gadget(dwc);
2715 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2716 reg &= ~DWC3_DCTL_TSTCTRL_MASK;
2717 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2718 dwc->test_mode = false;
2719 dwc3_clear_stall_all_ep(dwc);
2721 /* Reset device address to zero */
2722 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2723 reg &= ~(DWC3_DCFG_DEVADDR_MASK);
2724 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
2727 static void dwc3_gadget_conndone_interrupt(struct dwc3 *dwc)
2729 struct dwc3_ep *dep;
2734 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
2735 speed = reg & DWC3_DSTS_CONNECTSPD;
2739 * RAMClkSel is reset to 0 after USB reset, so it must be reprogrammed
2740 * each time on Connect Done.
2742 * Currently we always use the reset value. If any platform
2743 * wants to set this to a different value, we need to add a
2744 * setting and update GCTL.RAMCLKSEL here.
2748 case DWC3_DSTS_SUPERSPEED_PLUS:
2749 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
2750 dwc->gadget.ep0->maxpacket = 512;
2751 dwc->gadget.speed = USB_SPEED_SUPER_PLUS;
2753 case DWC3_DSTS_SUPERSPEED:
2755 * WORKAROUND: DWC3 revisions <1.90a have an issue which
2756 * would cause a missing USB3 Reset event.
2758 * In such situations, we should force a USB3 Reset
2759 * event by calling our dwc3_gadget_reset_interrupt()
2764 * STAR#9000483510: RTL: SS : USB3 reset event may
2765 * not be generated always when the link enters poll
2767 if (dwc->revision < DWC3_REVISION_190A)
2768 dwc3_gadget_reset_interrupt(dwc);
2770 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
2771 dwc->gadget.ep0->maxpacket = 512;
2772 dwc->gadget.speed = USB_SPEED_SUPER;
2774 case DWC3_DSTS_HIGHSPEED:
2775 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
2776 dwc->gadget.ep0->maxpacket = 64;
2777 dwc->gadget.speed = USB_SPEED_HIGH;
2779 case DWC3_DSTS_FULLSPEED:
2780 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
2781 dwc->gadget.ep0->maxpacket = 64;
2782 dwc->gadget.speed = USB_SPEED_FULL;
2784 case DWC3_DSTS_LOWSPEED:
2785 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(8);
2786 dwc->gadget.ep0->maxpacket = 8;
2787 dwc->gadget.speed = USB_SPEED_LOW;
2791 dwc->eps[1]->endpoint.maxpacket = dwc->gadget.ep0->maxpacket;
2793 /* Enable USB2 LPM Capability */
2795 if ((dwc->revision > DWC3_REVISION_194A) &&
2796 (speed != DWC3_DSTS_SUPERSPEED) &&
2797 (speed != DWC3_DSTS_SUPERSPEED_PLUS)) {
2798 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2799 reg |= DWC3_DCFG_LPM_CAP;
2800 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
2802 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2803 reg &= ~(DWC3_DCTL_HIRD_THRES_MASK | DWC3_DCTL_L1_HIBER_EN);
2805 reg |= DWC3_DCTL_HIRD_THRES(dwc->hird_threshold);
2808 * When dwc3 revisions >= 2.40a, LPM Erratum is enabled and
2809 * DCFG.LPMCap is set, core responses with an ACK and the
2810 * BESL value in the LPM token is less than or equal to LPM
2813 WARN_ONCE(dwc->revision < DWC3_REVISION_240A
2814 && dwc->has_lpm_erratum,
2815 "LPM Erratum not available on dwc3 revisions < 2.40a\n");
2817 if (dwc->has_lpm_erratum && dwc->revision >= DWC3_REVISION_240A)
2818 reg |= DWC3_DCTL_LPM_ERRATA(dwc->lpm_nyet_threshold);
2820 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2822 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2823 reg &= ~DWC3_DCTL_HIRD_THRES_MASK;
2824 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2828 ret = __dwc3_gadget_ep_enable(dep, true, false);
2830 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
2835 ret = __dwc3_gadget_ep_enable(dep, true, false);
2837 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
2842 * Configure PHY via GUSB3PIPECTLn if required.
2844 * Update GTXFIFOSIZn
2846 * In both cases reset values should be sufficient.
2850 static void dwc3_gadget_wakeup_interrupt(struct dwc3 *dwc)
2853 * TODO take core out of low power mode when that's
2857 if (dwc->gadget_driver && dwc->gadget_driver->resume) {
2858 spin_unlock(&dwc->lock);
2859 dwc->gadget_driver->resume(&dwc->gadget);
2860 spin_lock(&dwc->lock);
2864 static void dwc3_gadget_linksts_change_interrupt(struct dwc3 *dwc,
2865 unsigned int evtinfo)
2867 enum dwc3_link_state next = evtinfo & DWC3_LINK_STATE_MASK;
2868 unsigned int pwropt;
2871 * WORKAROUND: DWC3 < 2.50a have an issue when configured without
2872 * Hibernation mode enabled which would show up when device detects
2873 * host-initiated U3 exit.
2875 * In that case, device will generate a Link State Change Interrupt
2876 * from U3 to RESUME which is only necessary if Hibernation is
2879 * There are no functional changes due to such spurious event and we
2880 * just need to ignore it.
2884 * STAR#9000570034 RTL: SS Resume event generated in non-Hibernation
2887 pwropt = DWC3_GHWPARAMS1_EN_PWROPT(dwc->hwparams.hwparams1);
2888 if ((dwc->revision < DWC3_REVISION_250A) &&
2889 (pwropt != DWC3_GHWPARAMS1_EN_PWROPT_HIB)) {
2890 if ((dwc->link_state == DWC3_LINK_STATE_U3) &&
2891 (next == DWC3_LINK_STATE_RESUME)) {
2897 * WORKAROUND: DWC3 Revisions <1.83a have an issue which, depending
2898 * on the link partner, the USB session might do multiple entry/exit
2899 * of low power states before a transfer takes place.
2901 * Due to this problem, we might experience lower throughput. The
2902 * suggested workaround is to disable DCTL[12:9] bits if we're
2903 * transitioning from U1/U2 to U0 and enable those bits again
2904 * after a transfer completes and there are no pending transfers
2905 * on any of the enabled endpoints.
2907 * This is the first half of that workaround.
2911 * STAR#9000446952: RTL: Device SS : if U1/U2 ->U0 takes >128us
2912 * core send LGO_Ux entering U0
2914 if (dwc->revision < DWC3_REVISION_183A) {
2915 if (next == DWC3_LINK_STATE_U0) {
2919 switch (dwc->link_state) {
2920 case DWC3_LINK_STATE_U1:
2921 case DWC3_LINK_STATE_U2:
2922 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2923 u1u2 = reg & (DWC3_DCTL_INITU2ENA
2924 | DWC3_DCTL_ACCEPTU2ENA
2925 | DWC3_DCTL_INITU1ENA
2926 | DWC3_DCTL_ACCEPTU1ENA);
2929 dwc->u1u2 = reg & u1u2;
2933 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2943 case DWC3_LINK_STATE_U1:
2944 if (dwc->speed == USB_SPEED_SUPER)
2945 dwc3_suspend_gadget(dwc);
2947 case DWC3_LINK_STATE_U2:
2948 case DWC3_LINK_STATE_U3:
2949 dwc3_suspend_gadget(dwc);
2951 case DWC3_LINK_STATE_RESUME:
2952 dwc3_resume_gadget(dwc);
2959 dwc->link_state = next;
2962 static void dwc3_gadget_suspend_interrupt(struct dwc3 *dwc,
2963 unsigned int evtinfo)
2965 enum dwc3_link_state next = evtinfo & DWC3_LINK_STATE_MASK;
2967 if (dwc->link_state != next && next == DWC3_LINK_STATE_U3)
2968 dwc3_suspend_gadget(dwc);
2970 dwc->link_state = next;
2973 static void dwc3_gadget_hibernation_interrupt(struct dwc3 *dwc,
2974 unsigned int evtinfo)
2976 unsigned int is_ss = evtinfo & BIT(4);
2979 * WORKAROUND: DWC3 revison 2.20a with hibernation support
2980 * have a known issue which can cause USB CV TD.9.23 to fail
2983 * Because of this issue, core could generate bogus hibernation
2984 * events which SW needs to ignore.
2988 * STAR#9000546576: Device Mode Hibernation: Issue in USB 2.0
2989 * Device Fallback from SuperSpeed
2991 if (is_ss ^ (dwc->speed == USB_SPEED_SUPER))
2994 /* enter hibernation here */
2997 static void dwc3_gadget_interrupt(struct dwc3 *dwc,
2998 const struct dwc3_event_devt *event)
3000 switch (event->type) {
3001 case DWC3_DEVICE_EVENT_DISCONNECT:
3002 dwc3_gadget_disconnect_interrupt(dwc);
3004 case DWC3_DEVICE_EVENT_RESET:
3005 dwc3_gadget_reset_interrupt(dwc);
3007 case DWC3_DEVICE_EVENT_CONNECT_DONE:
3008 dwc3_gadget_conndone_interrupt(dwc);
3010 case DWC3_DEVICE_EVENT_WAKEUP:
3011 dwc3_gadget_wakeup_interrupt(dwc);
3013 case DWC3_DEVICE_EVENT_HIBER_REQ:
3014 if (dev_WARN_ONCE(dwc->dev, !dwc->has_hibernation,
3015 "unexpected hibernation event\n"))
3018 dwc3_gadget_hibernation_interrupt(dwc, event->event_info);
3020 case DWC3_DEVICE_EVENT_LINK_STATUS_CHANGE:
3021 dwc3_gadget_linksts_change_interrupt(dwc, event->event_info);
3023 case DWC3_DEVICE_EVENT_EOPF:
3024 /* It changed to be suspend event for version 2.30a and above */
3025 if (dwc->revision >= DWC3_REVISION_230A) {
3027 * Ignore suspend event until the gadget enters into
3028 * USB_STATE_CONFIGURED state.
3030 if (dwc->gadget.state >= USB_STATE_CONFIGURED)
3031 dwc3_gadget_suspend_interrupt(dwc,
3035 case DWC3_DEVICE_EVENT_SOF:
3036 case DWC3_DEVICE_EVENT_ERRATIC_ERROR:
3037 case DWC3_DEVICE_EVENT_CMD_CMPL:
3038 case DWC3_DEVICE_EVENT_OVERFLOW:
3041 dev_WARN(dwc->dev, "UNKNOWN IRQ %d\n", event->type);
3045 static void dwc3_process_event_entry(struct dwc3 *dwc,
3046 const union dwc3_event *event)
3048 trace_dwc3_event(event->raw, dwc);
3050 if (!event->type.is_devspec)
3051 dwc3_endpoint_interrupt(dwc, &event->depevt);
3052 else if (event->type.type == DWC3_EVENT_TYPE_DEV)
3053 dwc3_gadget_interrupt(dwc, &event->devt);
3055 dev_err(dwc->dev, "UNKNOWN IRQ type %d\n", event->raw);
3058 static irqreturn_t dwc3_process_event_buf(struct dwc3_event_buffer *evt)
3060 struct dwc3 *dwc = evt->dwc;
3061 irqreturn_t ret = IRQ_NONE;
3067 if (!(evt->flags & DWC3_EVENT_PENDING))
3071 union dwc3_event event;
3073 event.raw = *(u32 *) (evt->cache + evt->lpos);
3075 dwc3_process_event_entry(dwc, &event);
3078 * FIXME we wrap around correctly to the next entry as
3079 * almost all entries are 4 bytes in size. There is one
3080 * entry which has 12 bytes which is a regular entry
3081 * followed by 8 bytes data. ATM I don't know how
3082 * things are organized if we get next to the a
3083 * boundary so I worry about that once we try to handle
3086 evt->lpos = (evt->lpos + 4) % evt->length;
3091 evt->flags &= ~DWC3_EVENT_PENDING;
3094 /* Unmask interrupt */
3095 reg = dwc3_readl(dwc->regs, DWC3_GEVNTSIZ(0));
3096 reg &= ~DWC3_GEVNTSIZ_INTMASK;
3097 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0), reg);
3099 if (dwc->imod_interval) {
3100 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), DWC3_GEVNTCOUNT_EHB);
3101 dwc3_writel(dwc->regs, DWC3_DEV_IMOD(0), dwc->imod_interval);
3107 static irqreturn_t dwc3_thread_interrupt(int irq, void *_evt)
3109 struct dwc3_event_buffer *evt = _evt;
3110 struct dwc3 *dwc = evt->dwc;
3111 unsigned long flags;
3112 irqreturn_t ret = IRQ_NONE;
3114 spin_lock_irqsave(&dwc->lock, flags);
3115 ret = dwc3_process_event_buf(evt);
3116 spin_unlock_irqrestore(&dwc->lock, flags);
3121 static irqreturn_t dwc3_check_event_buf(struct dwc3_event_buffer *evt)
3123 struct dwc3 *dwc = evt->dwc;
3128 if (pm_runtime_suspended(dwc->dev)) {
3129 pm_runtime_get(dwc->dev);
3130 disable_irq_nosync(dwc->irq_gadget);
3131 dwc->pending_events = true;
3136 * With PCIe legacy interrupt, test shows that top-half irq handler can
3137 * be called again after HW interrupt deassertion. Check if bottom-half
3138 * irq event handler completes before caching new event to prevent
3141 if (evt->flags & DWC3_EVENT_PENDING)
3144 count = dwc3_readl(dwc->regs, DWC3_GEVNTCOUNT(0));
3145 count &= DWC3_GEVNTCOUNT_MASK;
3150 evt->flags |= DWC3_EVENT_PENDING;
3152 /* Mask interrupt */
3153 reg = dwc3_readl(dwc->regs, DWC3_GEVNTSIZ(0));
3154 reg |= DWC3_GEVNTSIZ_INTMASK;
3155 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0), reg);
3157 amount = min(count, evt->length - evt->lpos);
3158 memcpy(evt->cache + evt->lpos, evt->buf + evt->lpos, amount);
3161 memcpy(evt->cache, evt->buf, count - amount);
3163 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), count);
3165 return IRQ_WAKE_THREAD;
3168 static irqreturn_t dwc3_interrupt(int irq, void *_evt)
3170 struct dwc3_event_buffer *evt = _evt;
3172 return dwc3_check_event_buf(evt);
3175 static int dwc3_gadget_get_irq(struct dwc3 *dwc)
3177 struct platform_device *dwc3_pdev = to_platform_device(dwc->dev);
3180 irq = platform_get_irq_byname(dwc3_pdev, "peripheral");
3184 if (irq == -EPROBE_DEFER)
3187 irq = platform_get_irq_byname(dwc3_pdev, "dwc_usb3");
3191 if (irq == -EPROBE_DEFER)
3194 irq = platform_get_irq(dwc3_pdev, 0);
3198 if (irq != -EPROBE_DEFER)
3199 dev_err(dwc->dev, "missing peripheral IRQ\n");
3209 * dwc3_gadget_init - initializes gadget related registers
3210 * @dwc: pointer to our controller context structure
3212 * Returns 0 on success otherwise negative errno.
3214 int dwc3_gadget_init(struct dwc3 *dwc)
3219 irq = dwc3_gadget_get_irq(dwc);
3225 dwc->irq_gadget = irq;
3227 dwc->ep0_trb = dma_alloc_coherent(dwc->sysdev,
3228 sizeof(*dwc->ep0_trb) * 2,
3229 &dwc->ep0_trb_addr, GFP_KERNEL);
3230 if (!dwc->ep0_trb) {
3231 dev_err(dwc->dev, "failed to allocate ep0 trb\n");
3236 dwc->setup_buf = kzalloc(DWC3_EP0_SETUP_SIZE, GFP_KERNEL);
3237 if (!dwc->setup_buf) {
3242 dwc->bounce = dma_alloc_coherent(dwc->sysdev, DWC3_BOUNCE_SIZE,
3243 &dwc->bounce_addr, GFP_KERNEL);
3249 init_completion(&dwc->ep0_in_setup);
3251 dwc->gadget.ops = &dwc3_gadget_ops;
3252 dwc->gadget.speed = USB_SPEED_UNKNOWN;
3253 dwc->gadget.sg_supported = true;
3254 dwc->gadget.name = "dwc3-gadget";
3255 dwc->gadget.is_otg = dwc->dr_mode == USB_DR_MODE_OTG;
3258 * FIXME We might be setting max_speed to <SUPER, however versions
3259 * <2.20a of dwc3 have an issue with metastability (documented
3260 * elsewhere in this driver) which tells us we can't set max speed to
3261 * anything lower than SUPER.
3263 * Because gadget.max_speed is only used by composite.c and function
3264 * drivers (i.e. it won't go into dwc3's registers) we are allowing this
3265 * to happen so we avoid sending SuperSpeed Capability descriptor
3266 * together with our BOS descriptor as that could confuse host into
3267 * thinking we can handle super speed.
3269 * Note that, in fact, we won't even support GetBOS requests when speed
3270 * is less than super speed because we don't have means, yet, to tell
3271 * composite.c that we are USB 2.0 + LPM ECN.
3273 if (dwc->revision < DWC3_REVISION_220A &&
3274 !dwc->dis_metastability_quirk)
3275 dev_info(dwc->dev, "changing max_speed on rev %08x\n",
3278 dwc->gadget.max_speed = dwc->maximum_speed;
3281 * REVISIT: Here we should clear all pending IRQs to be
3282 * sure we're starting from a well known location.
3285 ret = dwc3_gadget_init_endpoints(dwc, dwc->num_eps);
3289 ret = usb_add_gadget_udc(dwc->dev, &dwc->gadget);
3291 dev_err(dwc->dev, "failed to register udc\n");
3298 dwc3_gadget_free_endpoints(dwc);
3301 dma_free_coherent(dwc->sysdev, DWC3_BOUNCE_SIZE, dwc->bounce,
3305 kfree(dwc->setup_buf);
3308 dma_free_coherent(dwc->sysdev, sizeof(*dwc->ep0_trb) * 2,
3309 dwc->ep0_trb, dwc->ep0_trb_addr);
3315 /* -------------------------------------------------------------------------- */
3317 void dwc3_gadget_exit(struct dwc3 *dwc)
3319 usb_del_gadget_udc(&dwc->gadget);
3320 dwc3_gadget_free_endpoints(dwc);
3321 dma_free_coherent(dwc->sysdev, DWC3_BOUNCE_SIZE, dwc->bounce,
3323 kfree(dwc->setup_buf);
3324 dma_free_coherent(dwc->sysdev, sizeof(*dwc->ep0_trb) * 2,
3325 dwc->ep0_trb, dwc->ep0_trb_addr);
3328 int dwc3_gadget_suspend(struct dwc3 *dwc)
3330 if (!dwc->gadget_driver)
3333 dwc3_gadget_run_stop(dwc, false, false);
3334 dwc3_disconnect_gadget(dwc);
3335 __dwc3_gadget_stop(dwc);
3340 int dwc3_gadget_resume(struct dwc3 *dwc)
3344 if (!dwc->gadget_driver)
3347 ret = __dwc3_gadget_start(dwc);
3351 ret = dwc3_gadget_run_stop(dwc, true, false);
3358 __dwc3_gadget_stop(dwc);
3364 void dwc3_gadget_process_pending_events(struct dwc3 *dwc)
3366 if (dwc->pending_events) {
3367 dwc3_interrupt(dwc->irq_gadget, dwc->ev_buf);
3368 dwc->pending_events = false;
3369 enable_irq(dwc->irq_gadget);