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>
30 #define DWC3_ALIGN_FRAME(d) (((d)->frame_number + (d)->interval) \
31 & ~((d)->interval - 1))
34 * dwc3_gadget_set_test_mode - enables usb2 test modes
35 * @dwc: pointer to our context structure
36 * @mode: the mode to set (J, K SE0 NAK, Force Enable)
38 * Caller should take care of locking. This function will return 0 on
39 * success or -EINVAL if wrong Test Selector is passed.
41 int dwc3_gadget_set_test_mode(struct dwc3 *dwc, int mode)
45 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
46 reg &= ~DWC3_DCTL_TSTCTRL_MASK;
60 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
66 * dwc3_gadget_get_link_state - gets current state of usb link
67 * @dwc: pointer to our context structure
69 * Caller should take care of locking. This function will
70 * return the link state on success (>= 0) or -ETIMEDOUT.
72 int dwc3_gadget_get_link_state(struct dwc3 *dwc)
76 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
78 return DWC3_DSTS_USBLNKST(reg);
82 * dwc3_gadget_set_link_state - sets usb link to a particular state
83 * @dwc: pointer to our context structure
84 * @state: the state to put link into
86 * Caller should take care of locking. This function will
87 * return 0 on success or -ETIMEDOUT.
89 int dwc3_gadget_set_link_state(struct dwc3 *dwc, enum dwc3_link_state state)
95 * Wait until device controller is ready. Only applies to 1.94a and
98 if (dwc->revision >= DWC3_REVISION_194A) {
100 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
101 if (reg & DWC3_DSTS_DCNRD)
111 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
112 reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK;
114 /* set requested state */
115 reg |= DWC3_DCTL_ULSTCHNGREQ(state);
116 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
119 * The following code is racy when called from dwc3_gadget_wakeup,
120 * and is not needed, at least on newer versions
122 if (dwc->revision >= DWC3_REVISION_194A)
125 /* wait for a change in DSTS */
128 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
130 if (DWC3_DSTS_USBLNKST(reg) == state)
140 * dwc3_ep_inc_trb - increment a trb index.
141 * @index: Pointer to the TRB index to increment.
143 * The index should never point to the link TRB. After incrementing,
144 * if it is point to the link TRB, wrap around to the beginning. The
145 * link TRB is always at the last TRB entry.
147 static void dwc3_ep_inc_trb(u8 *index)
150 if (*index == (DWC3_TRB_NUM - 1))
155 * dwc3_ep_inc_enq - increment endpoint's enqueue pointer
156 * @dep: The endpoint whose enqueue pointer we're incrementing
158 static void dwc3_ep_inc_enq(struct dwc3_ep *dep)
160 dwc3_ep_inc_trb(&dep->trb_enqueue);
164 * dwc3_ep_inc_deq - increment endpoint's dequeue pointer
165 * @dep: The endpoint whose enqueue pointer we're incrementing
167 static void dwc3_ep_inc_deq(struct dwc3_ep *dep)
169 dwc3_ep_inc_trb(&dep->trb_dequeue);
172 static void dwc3_gadget_del_and_unmap_request(struct dwc3_ep *dep,
173 struct dwc3_request *req, int status)
175 struct dwc3 *dwc = dep->dwc;
177 req->started = false;
178 list_del(&req->list);
181 if (req->request.status == -EINPROGRESS)
182 req->request.status = status;
185 usb_gadget_unmap_request_by_dev(dwc->sysdev,
186 &req->request, req->direction);
189 trace_dwc3_gadget_giveback(req);
192 pm_runtime_put(dwc->dev);
196 * dwc3_gadget_giveback - call struct usb_request's ->complete callback
197 * @dep: The endpoint to whom the request belongs to
198 * @req: The request we're giving back
199 * @status: completion code for the request
201 * Must be called with controller's lock held and interrupts disabled. This
202 * function will unmap @req and call its ->complete() callback to notify upper
203 * layers that it has completed.
205 void dwc3_gadget_giveback(struct dwc3_ep *dep, struct dwc3_request *req,
208 struct dwc3 *dwc = dep->dwc;
210 if (dep->stream_capable && timer_pending(&req->stream_timeout_timer))
211 del_timer(&req->stream_timeout_timer);
213 dwc3_gadget_del_and_unmap_request(dep, req, status);
215 spin_unlock(&dwc->lock);
216 usb_gadget_giveback_request(&dep->endpoint, &req->request);
217 spin_lock(&dwc->lock);
221 * dwc3_send_gadget_generic_command - issue a generic command for the controller
222 * @dwc: pointer to the controller context
223 * @cmd: the command to be issued
224 * @param: command parameter
226 * Caller should take care of locking. Issue @cmd with a given @param to @dwc
227 * and wait for its completion.
229 int dwc3_send_gadget_generic_command(struct dwc3 *dwc, unsigned cmd, u32 param)
236 dwc3_writel(dwc->regs, DWC3_DGCMDPAR, param);
237 dwc3_writel(dwc->regs, DWC3_DGCMD, cmd | DWC3_DGCMD_CMDACT);
240 reg = dwc3_readl(dwc->regs, DWC3_DGCMD);
241 if (!(reg & DWC3_DGCMD_CMDACT)) {
242 status = DWC3_DGCMD_STATUS(reg);
254 trace_dwc3_gadget_generic_cmd(cmd, param, status);
259 static int __dwc3_gadget_wakeup(struct dwc3 *dwc);
262 * dwc3_send_gadget_ep_cmd - issue an endpoint command
263 * @dep: the endpoint to which the command is going to be issued
264 * @cmd: the command to be issued
265 * @params: parameters to the command
267 * Caller should handle locking. This function will issue @cmd with given
268 * @params to @dep and wait for its completion.
270 int dwc3_send_gadget_ep_cmd(struct dwc3_ep *dep, unsigned cmd,
271 struct dwc3_gadget_ep_cmd_params *params)
273 const struct usb_endpoint_descriptor *desc = dep->endpoint.desc;
274 struct dwc3 *dwc = dep->dwc;
283 * Synopsys Databook 2.60a states, on section 6.3.2.5.[1-8], that if
284 * we're issuing an endpoint command, we must check if
285 * GUSB2PHYCFG.SUSPHY bit is set. If it is, then we need to clear it.
287 * We will also set SUSPHY bit to what it was before returning as stated
288 * by the same section on Synopsys databook.
290 if (dwc->gadget.speed <= USB_SPEED_HIGH) {
291 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
292 if (unlikely(reg & DWC3_GUSB2PHYCFG_SUSPHY)) {
294 reg &= ~DWC3_GUSB2PHYCFG_SUSPHY;
295 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
299 if (DWC3_DEPCMD_CMD(cmd) == DWC3_DEPCMD_STARTTRANSFER) {
302 needs_wakeup = (dwc->link_state == DWC3_LINK_STATE_U1 ||
303 dwc->link_state == DWC3_LINK_STATE_U2 ||
304 dwc->link_state == DWC3_LINK_STATE_U3);
306 if (unlikely(needs_wakeup)) {
307 ret = __dwc3_gadget_wakeup(dwc);
308 dev_WARN_ONCE(dwc->dev, ret, "wakeup failed --> %d\n",
313 dwc3_writel(dep->regs, DWC3_DEPCMDPAR0, params->param0);
314 dwc3_writel(dep->regs, DWC3_DEPCMDPAR1, params->param1);
315 dwc3_writel(dep->regs, DWC3_DEPCMDPAR2, params->param2);
318 * Synopsys Databook 2.60a states in section 6.3.2.5.6 of that if we're
319 * not relying on XferNotReady, we can make use of a special "No
320 * Response Update Transfer" command where we should clear both CmdAct
323 * With this, we don't need to wait for command completion and can
324 * straight away issue further commands to the endpoint.
326 * NOTICE: We're making an assumption that control endpoints will never
327 * make use of Update Transfer command. This is a safe assumption
328 * because we can never have more than one request at a time with
329 * Control Endpoints. If anybody changes that assumption, this chunk
330 * needs to be updated accordingly.
332 if (DWC3_DEPCMD_CMD(cmd) == DWC3_DEPCMD_UPDATETRANSFER &&
333 !usb_endpoint_xfer_isoc(desc))
334 cmd &= ~(DWC3_DEPCMD_CMDIOC | DWC3_DEPCMD_CMDACT);
336 cmd |= DWC3_DEPCMD_CMDACT;
338 dwc3_writel(dep->regs, DWC3_DEPCMD, cmd);
340 reg = dwc3_readl(dep->regs, DWC3_DEPCMD);
341 if (!(reg & DWC3_DEPCMD_CMDACT)) {
342 cmd_status = DWC3_DEPCMD_STATUS(reg);
344 switch (cmd_status) {
348 case DEPEVT_TRANSFER_NO_RESOURCE:
351 case DEPEVT_TRANSFER_BUS_EXPIRY:
353 * SW issues START TRANSFER command to
354 * isochronous ep with future frame interval. If
355 * future interval time has already passed when
356 * core receives the command, it will respond
357 * with an error status of 'Bus Expiry'.
359 * Instead of always returning -EINVAL, let's
360 * give a hint to the gadget driver that this is
361 * the case by returning -EAGAIN.
366 dev_WARN(dwc->dev, "UNKNOWN cmd status\n");
375 cmd_status = -ETIMEDOUT;
378 trace_dwc3_gadget_ep_cmd(dep, cmd, params, cmd_status);
381 switch (DWC3_DEPCMD_CMD(cmd)) {
382 case DWC3_DEPCMD_STARTTRANSFER:
383 dep->flags |= DWC3_EP_TRANSFER_STARTED;
384 dwc3_gadget_ep_get_transfer_index(dep);
386 case DWC3_DEPCMD_ENDTRANSFER:
387 dep->flags &= ~DWC3_EP_TRANSFER_STARTED;
395 if (unlikely(susphy)) {
396 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
397 reg |= DWC3_GUSB2PHYCFG_SUSPHY;
398 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
404 static int dwc3_send_clear_stall_ep_cmd(struct dwc3_ep *dep)
406 struct dwc3 *dwc = dep->dwc;
407 struct dwc3_gadget_ep_cmd_params params;
408 u32 cmd = DWC3_DEPCMD_CLEARSTALL;
411 * As of core revision 2.60a the recommended programming model
412 * is to set the ClearPendIN bit when issuing a Clear Stall EP
413 * command for IN endpoints. This is to prevent an issue where
414 * some (non-compliant) hosts may not send ACK TPs for pending
415 * IN transfers due to a mishandled error condition. Synopsys
418 if (dep->direction && (dwc->revision >= DWC3_REVISION_260A) &&
419 (dwc->gadget.speed >= USB_SPEED_SUPER))
420 cmd |= DWC3_DEPCMD_CLEARPENDIN;
422 memset(¶ms, 0, sizeof(params));
424 return dwc3_send_gadget_ep_cmd(dep, cmd, ¶ms);
427 dma_addr_t dwc3_trb_dma_offset(struct dwc3_ep *dep, struct dwc3_trb *trb)
429 u32 offset = (char *) trb - (char *) dep->trb_pool;
431 return dep->trb_pool_dma + offset;
434 static int dwc3_alloc_trb_pool(struct dwc3_ep *dep)
436 struct dwc3 *dwc = dep->dwc;
441 dep->trb_pool = dma_alloc_coherent(dwc->sysdev,
442 sizeof(struct dwc3_trb) * DWC3_TRB_NUM,
443 &dep->trb_pool_dma, GFP_KERNEL);
444 if (!dep->trb_pool) {
445 dev_err(dep->dwc->dev, "failed to allocate trb pool for %s\n",
453 static void dwc3_free_trb_pool(struct dwc3_ep *dep)
455 struct dwc3 *dwc = dep->dwc;
457 dma_free_coherent(dwc->sysdev, sizeof(struct dwc3_trb) * DWC3_TRB_NUM,
458 dep->trb_pool, dep->trb_pool_dma);
460 dep->trb_pool = NULL;
461 dep->trb_pool_dma = 0;
464 static int dwc3_gadget_set_xfer_resource(struct dwc3_ep *dep)
466 struct dwc3_gadget_ep_cmd_params params;
468 memset(¶ms, 0x00, sizeof(params));
470 params.param0 = DWC3_DEPXFERCFG_NUM_XFER_RES(1);
472 return dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETTRANSFRESOURCE,
477 * dwc3_gadget_start_config - configure ep resources
478 * @dep: endpoint that is being enabled
480 * Issue a %DWC3_DEPCMD_DEPSTARTCFG command to @dep. After the command's
481 * completion, it will set Transfer Resource for all available endpoints.
483 * The assignment of transfer resources cannot perfectly follow the data book
484 * due to the fact that the controller driver does not have all knowledge of the
485 * configuration in advance. It is given this information piecemeal by the
486 * composite gadget framework after every SET_CONFIGURATION and
487 * SET_INTERFACE. Trying to follow the databook programming model in this
488 * scenario can cause errors. For two reasons:
490 * 1) The databook says to do %DWC3_DEPCMD_DEPSTARTCFG for every
491 * %USB_REQ_SET_CONFIGURATION and %USB_REQ_SET_INTERFACE (8.1.5). This is
492 * incorrect in the scenario of multiple interfaces.
494 * 2) The databook does not mention doing more %DWC3_DEPCMD_DEPXFERCFG for new
495 * endpoint on alt setting (8.1.6).
497 * The following simplified method is used instead:
499 * All hardware endpoints can be assigned a transfer resource and this setting
500 * will stay persistent until either a core reset or hibernation. So whenever we
501 * do a %DWC3_DEPCMD_DEPSTARTCFG(0) we can go ahead and do
502 * %DWC3_DEPCMD_DEPXFERCFG for every hardware endpoint as well. We are
503 * guaranteed that there are as many transfer resources as endpoints.
505 * This function is called for each endpoint when it is being enabled but is
506 * triggered only when called for EP0-out, which always happens first, and which
507 * should only happen in one of the above conditions.
509 static int dwc3_gadget_start_config(struct dwc3_ep *dep)
511 struct dwc3_gadget_ep_cmd_params params;
520 memset(¶ms, 0x00, sizeof(params));
521 cmd = DWC3_DEPCMD_DEPSTARTCFG;
524 ret = dwc3_send_gadget_ep_cmd(dep, cmd, ¶ms);
528 for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) {
529 struct dwc3_ep *dep = dwc->eps[i];
534 ret = dwc3_gadget_set_xfer_resource(dep);
542 static void stream_timeout_function(struct timer_list *arg)
544 struct dwc3_request *req = from_timer(req, arg, stream_timeout_timer);
545 struct dwc3_ep *dep = req->dep;
546 struct dwc3 *dwc = dep->dwc;
549 spin_lock_irqsave(&dwc->lock, flags);
550 dwc3_stop_active_transfer(dep, true);
551 __dwc3_gadget_kick_transfer(dep);
552 spin_unlock_irqrestore(&dwc->lock, flags);
555 static int dwc3_gadget_set_ep_config(struct dwc3_ep *dep, unsigned int action)
557 const struct usb_ss_ep_comp_descriptor *comp_desc;
558 const struct usb_endpoint_descriptor *desc;
559 struct dwc3_gadget_ep_cmd_params params;
560 struct dwc3 *dwc = dep->dwc;
562 comp_desc = dep->endpoint.comp_desc;
563 desc = dep->endpoint.desc;
565 memset(¶ms, 0x00, sizeof(params));
567 params.param0 = DWC3_DEPCFG_EP_TYPE(usb_endpoint_type(desc))
568 | DWC3_DEPCFG_MAX_PACKET_SIZE(usb_endpoint_maxp(desc));
570 /* Burst size is only needed in SuperSpeed mode */
571 if (dwc->gadget.speed >= USB_SPEED_SUPER) {
572 u32 burst = dep->endpoint.maxburst;
573 params.param0 |= DWC3_DEPCFG_BURST_SIZE(burst - 1);
576 params.param0 |= action;
577 if (action == DWC3_DEPCFG_ACTION_RESTORE)
578 params.param2 |= dep->saved_state;
580 if (usb_endpoint_xfer_control(desc))
581 params.param1 = DWC3_DEPCFG_XFER_COMPLETE_EN;
583 if (dep->number <= 1 || usb_endpoint_xfer_isoc(desc))
584 params.param1 |= DWC3_DEPCFG_XFER_NOT_READY_EN;
586 if (usb_ss_max_streams(comp_desc) && usb_endpoint_xfer_bulk(desc)) {
587 params.param1 |= DWC3_DEPCFG_STREAM_CAPABLE
588 | DWC3_DEPCFG_STREAM_EVENT_EN
589 | DWC3_DEPCFG_XFER_COMPLETE_EN;
590 dep->stream_capable = true;
593 if (!usb_endpoint_xfer_control(desc))
594 params.param1 |= DWC3_DEPCFG_XFER_IN_PROGRESS_EN;
597 * We are doing 1:1 mapping for endpoints, meaning
598 * Physical Endpoints 2 maps to Logical Endpoint 2 and
599 * so on. We consider the direction bit as part of the physical
600 * endpoint number. So USB endpoint 0x81 is 0x03.
602 params.param1 |= DWC3_DEPCFG_EP_NUMBER(dep->number);
605 * We must use the lower 16 TX FIFOs even though
609 params.param0 |= DWC3_DEPCFG_FIFO_NUMBER(dep->number >> 1);
611 if (desc->bInterval) {
612 params.param1 |= DWC3_DEPCFG_BINTERVAL_M1(desc->bInterval - 1);
613 dep->interval = 1 << (desc->bInterval - 1);
616 return dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETEPCONFIG, ¶ms);
620 * __dwc3_gadget_ep_enable - initializes a hw endpoint
621 * @dep: endpoint to be initialized
622 * @action: one of INIT, MODIFY or RESTORE
624 * Caller should take care of locking. Execute all necessary commands to
625 * initialize a HW endpoint so it can be used by a gadget driver.
627 int __dwc3_gadget_ep_enable(struct dwc3_ep *dep, unsigned int action)
629 const struct usb_endpoint_descriptor *desc = dep->endpoint.desc;
630 struct dwc3 *dwc = dep->dwc;
635 if (!(dep->flags & DWC3_EP_ENABLED) || dwc->is_hibernated) {
636 ret = dwc3_gadget_start_config(dep);
641 ret = dwc3_gadget_set_ep_config(dep, action);
645 if (!(dep->flags & DWC3_EP_ENABLED) || dwc->is_hibernated) {
646 struct dwc3_trb *trb_st_hw;
647 struct dwc3_trb *trb_link;
649 dep->type = usb_endpoint_type(desc);
650 dep->flags |= DWC3_EP_ENABLED;
651 dep->flags &= ~DWC3_EP_END_TRANSFER_PENDING;
653 reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
654 reg |= DWC3_DALEPENA_EP(dep->number);
655 dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
657 init_waitqueue_head(&dep->wait_end_transfer);
659 if (usb_endpoint_xfer_control(desc))
662 if (!dwc->is_hibernated) {
663 /* Initialize the TRB ring */
664 dep->trb_dequeue = 0;
665 dep->trb_enqueue = 0;
666 memset(dep->trb_pool, 0,
667 sizeof(struct dwc3_trb) * DWC3_TRB_NUM);
670 /* Link TRB. The HWO bit is never reset */
671 trb_st_hw = &dep->trb_pool[0];
673 trb_link = &dep->trb_pool[DWC3_TRB_NUM - 1];
674 trb_link->bpl = lower_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw));
675 trb_link->bph = upper_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw));
676 trb_link->ctrl |= DWC3_TRBCTL_LINK_TRB;
677 trb_link->ctrl |= DWC3_TRB_CTRL_HWO;
681 * Issue StartTransfer here with no-op TRB so we can always rely on No
682 * Response Update Transfer command.
684 if (((usb_endpoint_xfer_bulk(desc) && !dep->stream_capable) ||
685 usb_endpoint_xfer_int(desc)) && !dwc->is_hibernated) {
686 struct dwc3_gadget_ep_cmd_params params;
687 struct dwc3_trb *trb;
691 memset(¶ms, 0, sizeof(params));
692 trb = &dep->trb_pool[0];
693 trb_dma = dwc3_trb_dma_offset(dep, trb);
695 params.param0 = upper_32_bits(trb_dma);
696 params.param1 = lower_32_bits(trb_dma);
698 cmd = DWC3_DEPCMD_STARTTRANSFER;
700 ret = dwc3_send_gadget_ep_cmd(dep, cmd, ¶ms);
706 trace_dwc3_gadget_ep_enable(dep);
711 static void dwc3_remove_requests(struct dwc3 *dwc, struct dwc3_ep *dep)
713 struct dwc3_request *req;
715 dwc3_stop_active_transfer(dep, true);
717 /* - giveback all requests to gadget driver */
718 while (!list_empty(&dep->started_list)) {
719 req = next_request(&dep->started_list);
721 dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
724 while (!list_empty(&dep->pending_list)) {
725 req = next_request(&dep->pending_list);
727 dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
732 * __dwc3_gadget_ep_disable - disables a hw endpoint
733 * @dep: the endpoint to disable
735 * This function undoes what __dwc3_gadget_ep_enable did and also removes
736 * requests which are currently being processed by the hardware and those which
737 * are not yet scheduled.
739 * Caller should take care of locking.
741 int __dwc3_gadget_ep_disable(struct dwc3_ep *dep)
743 struct dwc3 *dwc = dep->dwc;
746 trace_dwc3_gadget_ep_disable(dep);
748 dwc3_remove_requests(dwc, dep);
750 /* make sure HW endpoint isn't stalled */
751 if (dep->flags & DWC3_EP_STALL)
752 __dwc3_gadget_ep_set_halt(dep, 0, false);
754 reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
755 reg &= ~DWC3_DALEPENA_EP(dep->number);
756 dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
758 dep->stream_capable = false;
760 dep->flags &= DWC3_EP_END_TRANSFER_PENDING;
762 /* Clear out the ep descriptors for non-ep0 */
763 if (dep->number > 1) {
764 dep->endpoint.comp_desc = NULL;
765 dep->endpoint.desc = NULL;
771 /* -------------------------------------------------------------------------- */
773 static int dwc3_gadget_ep0_enable(struct usb_ep *ep,
774 const struct usb_endpoint_descriptor *desc)
779 static int dwc3_gadget_ep0_disable(struct usb_ep *ep)
784 /* -------------------------------------------------------------------------- */
786 static int dwc3_gadget_ep_enable(struct usb_ep *ep,
787 const struct usb_endpoint_descriptor *desc)
794 if (!ep || !desc || desc->bDescriptorType != USB_DT_ENDPOINT) {
795 pr_debug("dwc3: invalid parameters\n");
799 if (!desc->wMaxPacketSize) {
800 pr_debug("dwc3: missing wMaxPacketSize\n");
804 dep = to_dwc3_ep(ep);
807 if (dev_WARN_ONCE(dwc->dev, dep->flags & DWC3_EP_ENABLED,
808 "%s is already enabled\n",
812 spin_lock_irqsave(&dwc->lock, flags);
813 ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_INIT);
814 spin_unlock_irqrestore(&dwc->lock, flags);
819 static int dwc3_gadget_ep_disable(struct usb_ep *ep)
827 pr_debug("dwc3: invalid parameters\n");
831 dep = to_dwc3_ep(ep);
834 if (dev_WARN_ONCE(dwc->dev, !(dep->flags & DWC3_EP_ENABLED),
835 "%s is already disabled\n",
839 spin_lock_irqsave(&dwc->lock, flags);
840 ret = __dwc3_gadget_ep_disable(dep);
841 spin_unlock_irqrestore(&dwc->lock, flags);
846 static struct usb_request *dwc3_gadget_ep_alloc_request(struct usb_ep *ep,
849 struct dwc3_request *req;
850 struct dwc3_ep *dep = to_dwc3_ep(ep);
852 req = kzalloc(sizeof(*req), gfp_flags);
856 req->direction = dep->direction;
857 req->epnum = dep->number;
860 trace_dwc3_alloc_request(req);
862 return &req->request;
865 static void dwc3_gadget_ep_free_request(struct usb_ep *ep,
866 struct usb_request *request)
868 struct dwc3_request *req = to_dwc3_request(request);
870 trace_dwc3_free_request(req);
875 * dwc3_ep_prev_trb - returns the previous TRB in the ring
876 * @dep: The endpoint with the TRB ring
877 * @index: The index of the current TRB in the ring
879 * Returns the TRB prior to the one pointed to by the index. If the
880 * index is 0, we will wrap backwards, skip the link TRB, and return
881 * the one just before that.
883 static struct dwc3_trb *dwc3_ep_prev_trb(struct dwc3_ep *dep, u8 index)
888 tmp = DWC3_TRB_NUM - 1;
890 return &dep->trb_pool[tmp - 1];
893 static u32 dwc3_calc_trbs_left(struct dwc3_ep *dep)
895 struct dwc3_trb *tmp;
899 * If enqueue & dequeue are equal than it is either full or empty.
901 * One way to know for sure is if the TRB right before us has HWO bit
902 * set or not. If it has, then we're definitely full and can't fit any
903 * more transfers in our ring.
905 if (dep->trb_enqueue == dep->trb_dequeue) {
906 tmp = dwc3_ep_prev_trb(dep, dep->trb_enqueue);
907 if (tmp->ctrl & DWC3_TRB_CTRL_HWO)
910 return DWC3_TRB_NUM - 1;
913 trbs_left = dep->trb_dequeue - dep->trb_enqueue;
914 trbs_left &= (DWC3_TRB_NUM - 1);
916 if (dep->trb_dequeue < dep->trb_enqueue)
922 static void __dwc3_prepare_one_trb(struct dwc3_ep *dep, struct dwc3_trb *trb,
923 dma_addr_t dma, unsigned length, unsigned chain, unsigned node,
924 unsigned stream_id, unsigned short_not_ok, unsigned no_interrupt)
926 struct dwc3 *dwc = dep->dwc;
927 struct usb_gadget *gadget = &dwc->gadget;
928 enum usb_device_speed speed = gadget->speed;
930 trb->size = DWC3_TRB_SIZE_LENGTH(length);
931 trb->bpl = lower_32_bits(dma);
932 trb->bph = upper_32_bits(dma);
934 switch (usb_endpoint_type(dep->endpoint.desc)) {
935 case USB_ENDPOINT_XFER_CONTROL:
936 trb->ctrl = DWC3_TRBCTL_CONTROL_SETUP;
939 case USB_ENDPOINT_XFER_ISOC:
941 trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS_FIRST;
944 * USB Specification 2.0 Section 5.9.2 states that: "If
945 * there is only a single transaction in the microframe,
946 * only a DATA0 data packet PID is used. If there are
947 * two transactions per microframe, DATA1 is used for
948 * the first transaction data packet and DATA0 is used
949 * for the second transaction data packet. If there are
950 * three transactions per microframe, DATA2 is used for
951 * the first transaction data packet, DATA1 is used for
952 * the second, and DATA0 is used for the third."
954 * IOW, we should satisfy the following cases:
956 * 1) length <= maxpacket
959 * 2) maxpacket < length <= (2 * maxpacket)
962 * 3) (2 * maxpacket) < length <= (3 * maxpacket)
963 * - DATA2, DATA1, DATA0
965 if (speed == USB_SPEED_HIGH) {
966 struct usb_ep *ep = &dep->endpoint;
967 unsigned int mult = 2;
968 unsigned int maxp = usb_endpoint_maxp(ep->desc);
970 if (length <= (2 * maxp))
976 trb->size |= DWC3_TRB_SIZE_PCM1(mult);
979 trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS;
982 /* always enable Interrupt on Missed ISOC */
983 trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI;
986 case USB_ENDPOINT_XFER_BULK:
987 case USB_ENDPOINT_XFER_INT:
988 trb->ctrl = DWC3_TRBCTL_NORMAL;
992 * This is only possible with faulty memory because we
993 * checked it already :)
995 dev_WARN(dwc->dev, "Unknown endpoint type %d\n",
996 usb_endpoint_type(dep->endpoint.desc));
999 /* always enable Continue on Short Packet */
1000 if (usb_endpoint_dir_out(dep->endpoint.desc)) {
1001 trb->ctrl |= DWC3_TRB_CTRL_CSP;
1004 trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI;
1007 if ((!no_interrupt && !chain) ||
1008 (dwc3_calc_trbs_left(dep) == 1))
1009 trb->ctrl |= DWC3_TRB_CTRL_IOC;
1012 trb->ctrl |= DWC3_TRB_CTRL_CHN;
1014 * To start transfer on another stream number endpoint need to relase
1015 * previously acquired transfer resource for doing that there is two
1016 * ways 1. end transfer 2. set lst bit of control trb
1018 * by using lst bit in ctrl trb we will be able to save the time of
1019 * ending transfer hence improved performance
1021 else if (dep->stream_capable)
1022 trb->ctrl |= DWC3_TRB_CTRL_LST;
1024 if (usb_endpoint_xfer_bulk(dep->endpoint.desc) && dep->stream_capable)
1025 trb->ctrl |= DWC3_TRB_CTRL_SID_SOFN(stream_id);
1027 trb->ctrl |= DWC3_TRB_CTRL_HWO;
1029 dwc3_ep_inc_enq(dep);
1031 trace_dwc3_prepare_trb(dep, trb);
1035 * dwc3_prepare_one_trb - setup one TRB from one request
1036 * @dep: endpoint for which this request is prepared
1037 * @req: dwc3_request pointer
1038 * @chain: should this TRB be chained to the next?
1039 * @node: only for isochronous endpoints. First TRB needs different type.
1041 static void dwc3_prepare_one_trb(struct dwc3_ep *dep,
1042 struct dwc3_request *req, unsigned chain, unsigned node)
1044 struct dwc3_trb *trb;
1045 unsigned int length;
1047 unsigned stream_id = req->request.stream_id;
1048 unsigned short_not_ok = req->request.short_not_ok;
1049 unsigned no_interrupt = req->request.no_interrupt;
1051 if (req->request.num_sgs > 0) {
1052 length = sg_dma_len(req->start_sg);
1053 dma = sg_dma_address(req->start_sg);
1055 length = req->request.length;
1056 dma = req->request.dma;
1059 trb = &dep->trb_pool[dep->trb_enqueue];
1062 dwc3_gadget_move_started_request(req);
1064 req->trb_dma = dwc3_trb_dma_offset(dep, trb);
1067 __dwc3_prepare_one_trb(dep, trb, dma, length, chain, node,
1068 stream_id, short_not_ok, no_interrupt);
1071 static void dwc3_prepare_one_trb_sg(struct dwc3_ep *dep,
1072 struct dwc3_request *req)
1074 struct scatterlist *sg = req->start_sg;
1075 struct scatterlist *s;
1078 unsigned int remaining = req->request.num_mapped_sgs
1079 - req->num_queued_sgs;
1081 for_each_sg(sg, s, remaining, i) {
1082 unsigned int length = req->request.length;
1083 unsigned int maxp = usb_endpoint_maxp(dep->endpoint.desc);
1084 unsigned int rem = length % maxp;
1085 unsigned chain = true;
1090 if (rem && usb_endpoint_dir_out(dep->endpoint.desc) && !chain) {
1091 struct dwc3 *dwc = dep->dwc;
1092 struct dwc3_trb *trb;
1094 req->unaligned = true;
1096 /* prepare normal TRB */
1097 dwc3_prepare_one_trb(dep, req, true, i);
1099 /* Now prepare one extra TRB to align transfer size */
1100 trb = &dep->trb_pool[dep->trb_enqueue];
1101 __dwc3_prepare_one_trb(dep, trb, dwc->bounce_addr,
1102 maxp - rem, false, 0,
1103 req->request.stream_id,
1104 req->request.short_not_ok,
1105 req->request.no_interrupt);
1107 dwc3_prepare_one_trb(dep, req, chain, i);
1111 * There can be a situation where all sgs in sglist are not
1112 * queued because of insufficient trb number. To handle this
1113 * case, update start_sg to next sg to be queued, so that
1114 * we have free trbs we can continue queuing from where we
1115 * previously stopped
1118 req->start_sg = sg_next(s);
1120 req->num_queued_sgs++;
1122 if (!dwc3_calc_trbs_left(dep))
1127 static void dwc3_prepare_one_trb_linear(struct dwc3_ep *dep,
1128 struct dwc3_request *req)
1130 unsigned int length = req->request.length;
1131 unsigned int maxp = usb_endpoint_maxp(dep->endpoint.desc);
1132 unsigned int rem = length % maxp;
1134 if (rem && usb_endpoint_dir_out(dep->endpoint.desc)) {
1135 struct dwc3 *dwc = dep->dwc;
1136 struct dwc3_trb *trb;
1138 req->unaligned = true;
1140 /* prepare normal TRB */
1141 dwc3_prepare_one_trb(dep, req, true, 0);
1143 /* Now prepare one extra TRB to align transfer size */
1144 trb = &dep->trb_pool[dep->trb_enqueue];
1145 __dwc3_prepare_one_trb(dep, trb, dwc->bounce_addr, maxp - rem,
1146 false, 0, req->request.stream_id,
1147 req->request.short_not_ok,
1148 req->request.no_interrupt);
1149 } else if (req->request.zero && req->request.length &&
1150 (IS_ALIGNED(req->request.length, maxp))) {
1151 struct dwc3 *dwc = dep->dwc;
1152 struct dwc3_trb *trb;
1156 /* prepare normal TRB */
1157 dwc3_prepare_one_trb(dep, req, true, 0);
1159 /* Now prepare one extra TRB to handle ZLP */
1160 trb = &dep->trb_pool[dep->trb_enqueue];
1161 __dwc3_prepare_one_trb(dep, trb, dwc->bounce_addr, 0,
1162 false, 0, req->request.stream_id,
1163 req->request.short_not_ok,
1164 req->request.no_interrupt);
1166 dwc3_prepare_one_trb(dep, req, false, 0);
1171 * dwc3_prepare_trbs - setup TRBs from requests
1172 * @dep: endpoint for which requests are being prepared
1174 * The function goes through the requests list and sets up TRBs for the
1175 * transfers. The function returns once there are no more TRBs available or
1176 * it runs out of requests.
1178 static void dwc3_prepare_trbs(struct dwc3_ep *dep)
1180 struct dwc3_request *req, *n;
1182 BUILD_BUG_ON_NOT_POWER_OF_2(DWC3_TRB_NUM);
1185 * We can get in a situation where there's a request in the started list
1186 * but there weren't enough TRBs to fully kick it in the first time
1187 * around, so it has been waiting for more TRBs to be freed up.
1189 * In that case, we should check if we have a request with pending_sgs
1190 * in the started list and prepare TRBs for that request first,
1191 * otherwise we will prepare TRBs completely out of order and that will
1194 list_for_each_entry(req, &dep->started_list, list) {
1195 if (req->num_pending_sgs > 0)
1196 dwc3_prepare_one_trb_sg(dep, req);
1198 if (!dwc3_calc_trbs_left(dep))
1202 list_for_each_entry_safe(req, n, &dep->pending_list, list) {
1203 struct dwc3 *dwc = dep->dwc;
1206 ret = usb_gadget_map_request_by_dev(dwc->sysdev, &req->request,
1211 req->sg = req->request.sg;
1212 req->start_sg = req->sg;
1213 req->num_queued_sgs = 0;
1214 req->num_pending_sgs = req->request.num_mapped_sgs;
1216 if (req->num_pending_sgs > 0)
1217 dwc3_prepare_one_trb_sg(dep, req);
1219 dwc3_prepare_one_trb_linear(dep, req);
1221 if (!dwc3_calc_trbs_left(dep))
1226 int __dwc3_gadget_kick_transfer(struct dwc3_ep *dep)
1228 struct dwc3_gadget_ep_cmd_params params;
1229 struct dwc3_request *req;
1234 if (!dwc3_calc_trbs_left(dep))
1237 starting = !(dep->flags & DWC3_EP_TRANSFER_STARTED);
1239 dwc3_prepare_trbs(dep);
1240 req = next_request(&dep->started_list);
1242 dep->flags |= DWC3_EP_PENDING_REQUEST;
1246 memset(¶ms, 0, sizeof(params));
1249 params.param0 = upper_32_bits(req->trb_dma);
1250 params.param1 = lower_32_bits(req->trb_dma);
1251 cmd = DWC3_DEPCMD_STARTTRANSFER;
1253 if (dep->stream_capable)
1254 cmd = cmd | DWC3_DEPCMD_PARAM(req->request.stream_id);
1256 if (usb_endpoint_xfer_isoc(dep->endpoint.desc))
1257 cmd |= DWC3_DEPCMD_PARAM(dep->frame_number);
1260 cmd = DWC3_DEPCMD_UPDATETRANSFER |
1261 DWC3_DEPCMD_PARAM(dep->resource_index);
1264 ret = dwc3_send_gadget_ep_cmd(dep, cmd, ¶ms);
1267 * FIXME we need to iterate over the list of requests
1268 * here and stop, unmap, free and del each of the linked
1269 * requests instead of what we do now.
1272 memset(req->trb, 0, sizeof(struct dwc3_trb));
1273 dwc3_gadget_del_and_unmap_request(dep, req, ret);
1277 if (starting && dep->stream_capable) {
1278 req->stream_timeout_timer.expires = jiffies +
1279 msecs_to_jiffies(STREAM_TIMEOUT_MS);
1280 mod_timer(&req->stream_timeout_timer,
1281 req->stream_timeout_timer.expires);
1287 static int __dwc3_gadget_get_frame(struct dwc3 *dwc)
1291 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1292 return DWC3_DSTS_SOFFN(reg);
1295 static void __dwc3_gadget_start_isoc(struct dwc3_ep *dep)
1297 if (list_empty(&dep->pending_list)) {
1298 dev_info(dep->dwc->dev, "%s: ran out of requests\n",
1300 dep->flags |= DWC3_EP_PENDING_REQUEST;
1304 dep->frame_number = DWC3_ALIGN_FRAME(dep);
1305 __dwc3_gadget_kick_transfer(dep);
1308 static void dwc3_gadget_wakeup_interrupt(struct dwc3 *dwc);
1309 static int __dwc3_gadget_ep_queue(struct dwc3_ep *dep, struct dwc3_request *req)
1311 struct dwc3 *dwc = dep->dwc;
1313 if (!dep->endpoint.desc) {
1314 dev_err(dwc->dev, "%s: can't queue to disabled endpoint\n",
1319 if (WARN(req->dep != dep, "request %pK belongs to '%s'\n",
1320 &req->request, req->dep->name))
1323 pm_runtime_get(dwc->dev);
1325 req->request.actual = 0;
1326 req->request.status = -EINPROGRESS;
1328 if (dep->stream_capable)
1329 timer_setup(&req->stream_timeout_timer,
1330 stream_timeout_function, 0);
1332 trace_dwc3_ep_queue(req);
1334 list_add_tail(&req->list, &dep->pending_list);
1336 /* If core is hibernated, need to wakeup (remote wakeup) */
1337 if (dwc->is_hibernated) {
1338 dwc->force_hiber_wake = true;
1339 gadget_wakeup_interrupt(dwc);
1340 dwc->force_hiber_wake = false;
1344 * NOTICE: Isochronous endpoints should NEVER be prestarted. We must
1345 * wait for a XferNotReady event so we will know what's the current
1346 * (micro-)frame number.
1348 * Without this trick, we are very, very likely gonna get Bus Expiry
1349 * errors which will force us issue EndTransfer command.
1351 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
1352 if ((dep->flags & DWC3_EP_PENDING_REQUEST)) {
1353 if (dep->flags & DWC3_EP_TRANSFER_STARTED) {
1354 dwc3_stop_active_transfer(dep, true);
1355 dep->flags = DWC3_EP_ENABLED;
1359 cur_uf = __dwc3_gadget_get_frame(dwc);
1360 __dwc3_gadget_start_isoc(dep);
1361 dep->flags &= ~DWC3_EP_PENDING_REQUEST;
1366 if ((dep->flags & DWC3_EP_PENDING_REQUEST)) {
1367 if (!(dep->flags & DWC3_EP_TRANSFER_STARTED)) {
1368 __dwc3_gadget_start_isoc(dep);
1374 return __dwc3_gadget_kick_transfer(dep);
1377 static int dwc3_gadget_ep_queue(struct usb_ep *ep, struct usb_request *request,
1380 struct dwc3_request *req = to_dwc3_request(request);
1381 struct dwc3_ep *dep = to_dwc3_ep(ep);
1382 struct dwc3 *dwc = dep->dwc;
1384 unsigned long flags;
1388 spin_lock_irqsave(&dwc->lock, flags);
1389 ret = __dwc3_gadget_ep_queue(dep, req);
1390 spin_unlock_irqrestore(&dwc->lock, flags);
1395 static int dwc3_gadget_ep_dequeue(struct usb_ep *ep,
1396 struct usb_request *request)
1398 struct dwc3_request *req = to_dwc3_request(request);
1399 struct dwc3_request *r = NULL;
1401 struct dwc3_ep *dep = to_dwc3_ep(ep);
1402 struct dwc3 *dwc = dep->dwc;
1404 unsigned long flags;
1407 trace_dwc3_ep_dequeue(req);
1409 spin_lock_irqsave(&dwc->lock, flags);
1411 if (dep->stream_capable && timer_pending(&req->stream_timeout_timer))
1412 del_timer(&req->stream_timeout_timer);
1414 list_for_each_entry(r, &dep->pending_list, list) {
1420 list_for_each_entry(r, &dep->started_list, list) {
1425 /* wait until it is processed */
1426 dwc3_stop_active_transfer(dep, true);
1429 * If request was already started, this means we had to
1430 * stop the transfer. With that we also need to ignore
1431 * all TRBs used by the request, however TRBs can only
1432 * be modified after completion of END_TRANSFER
1433 * command. So what we do here is that we wait for
1434 * END_TRANSFER completion and only after that, we jump
1435 * over TRBs by clearing HWO and incrementing dequeue
1438 * Note that we have 2 possible types of transfers here:
1440 * i) Linear buffer request
1441 * ii) SG-list based request
1443 * SG-list based requests will have r->num_pending_sgs
1444 * set to a valid number (> 0). Linear requests,
1445 * normally use a single TRB.
1447 * For each of these two cases, if r->unaligned flag is
1448 * set, one extra TRB has been used to align transfer
1449 * size to wMaxPacketSize.
1451 * All of these cases need to be taken into
1452 * consideration so we don't mess up our TRB ring
1455 wait_event_lock_irq(dep->wait_end_transfer,
1456 !(dep->flags & DWC3_EP_END_TRANSFER_PENDING),
1462 if (r->num_pending_sgs) {
1463 struct dwc3_trb *trb;
1466 for (i = 0; i < r->num_pending_sgs; i++) {
1468 trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
1469 dwc3_ep_inc_deq(dep);
1472 if (r->unaligned || r->zero) {
1473 trb = r->trb + r->num_pending_sgs + 1;
1474 trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
1475 dwc3_ep_inc_deq(dep);
1478 struct dwc3_trb *trb = r->trb;
1480 trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
1481 dwc3_ep_inc_deq(dep);
1483 if (r->unaligned || r->zero) {
1485 trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
1486 dwc3_ep_inc_deq(dep);
1491 dev_err(dwc->dev, "request %pK was not queued to %s\n",
1498 /* giveback the request */
1500 dwc3_gadget_giveback(dep, req, -ECONNRESET);
1503 spin_unlock_irqrestore(&dwc->lock, flags);
1508 int __dwc3_gadget_ep_set_halt(struct dwc3_ep *dep, int value, int protocol)
1510 struct dwc3_gadget_ep_cmd_params params;
1511 struct dwc3 *dwc = dep->dwc;
1514 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
1515 dev_err(dwc->dev, "%s is of Isochronous type\n", dep->name);
1519 memset(¶ms, 0x00, sizeof(params));
1522 struct dwc3_trb *trb;
1524 unsigned transfer_in_flight;
1527 if (dep->flags & DWC3_EP_STALL)
1530 if (dep->number > 1)
1531 trb = dwc3_ep_prev_trb(dep, dep->trb_enqueue);
1533 trb = &dwc->ep0_trb[dep->trb_enqueue];
1535 transfer_in_flight = trb->ctrl & DWC3_TRB_CTRL_HWO;
1536 started = !list_empty(&dep->started_list);
1538 if (!protocol && ((dep->direction && transfer_in_flight) ||
1539 (!dep->direction && started))) {
1543 ret = dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETSTALL,
1546 dev_err(dwc->dev, "failed to set STALL on %s\n",
1549 dep->flags |= DWC3_EP_STALL;
1551 if (!(dep->flags & DWC3_EP_STALL))
1554 ret = dwc3_send_clear_stall_ep_cmd(dep);
1556 dev_err(dwc->dev, "failed to clear STALL on %s\n",
1559 dep->flags &= ~(DWC3_EP_STALL | DWC3_EP_WEDGE);
1565 static int dwc3_gadget_ep_set_halt(struct usb_ep *ep, int value)
1567 struct dwc3_ep *dep = to_dwc3_ep(ep);
1568 struct dwc3 *dwc = dep->dwc;
1570 unsigned long flags;
1574 spin_lock_irqsave(&dwc->lock, flags);
1575 ret = __dwc3_gadget_ep_set_halt(dep, value, false);
1576 spin_unlock_irqrestore(&dwc->lock, flags);
1581 static int dwc3_gadget_ep_set_wedge(struct usb_ep *ep)
1583 struct dwc3_ep *dep = to_dwc3_ep(ep);
1584 struct dwc3 *dwc = dep->dwc;
1585 unsigned long flags;
1588 spin_lock_irqsave(&dwc->lock, flags);
1589 dep->flags |= DWC3_EP_WEDGE;
1591 if (dep->number == 0 || dep->number == 1)
1592 ret = __dwc3_gadget_ep0_set_halt(ep, 1);
1594 ret = __dwc3_gadget_ep_set_halt(dep, 1, false);
1595 spin_unlock_irqrestore(&dwc->lock, flags);
1600 /* -------------------------------------------------------------------------- */
1602 static struct usb_endpoint_descriptor dwc3_gadget_ep0_desc = {
1603 .bLength = USB_DT_ENDPOINT_SIZE,
1604 .bDescriptorType = USB_DT_ENDPOINT,
1605 .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
1608 static const struct usb_ep_ops dwc3_gadget_ep0_ops = {
1609 .enable = dwc3_gadget_ep0_enable,
1610 .disable = dwc3_gadget_ep0_disable,
1611 .alloc_request = dwc3_gadget_ep_alloc_request,
1612 .free_request = dwc3_gadget_ep_free_request,
1613 .queue = dwc3_gadget_ep0_queue,
1614 .dequeue = dwc3_gadget_ep_dequeue,
1615 .set_halt = dwc3_gadget_ep0_set_halt,
1616 .set_wedge = dwc3_gadget_ep_set_wedge,
1619 static const struct usb_ep_ops dwc3_gadget_ep_ops = {
1620 .enable = dwc3_gadget_ep_enable,
1621 .disable = dwc3_gadget_ep_disable,
1622 .alloc_request = dwc3_gadget_ep_alloc_request,
1623 .free_request = dwc3_gadget_ep_free_request,
1624 .queue = dwc3_gadget_ep_queue,
1625 .dequeue = dwc3_gadget_ep_dequeue,
1626 .set_halt = dwc3_gadget_ep_set_halt,
1627 .set_wedge = dwc3_gadget_ep_set_wedge,
1630 /* -------------------------------------------------------------------------- */
1632 static int dwc3_gadget_get_frame(struct usb_gadget *g)
1634 struct dwc3 *dwc = gadget_to_dwc(g);
1636 return __dwc3_gadget_get_frame(dwc);
1639 static int __dwc3_gadget_wakeup(struct dwc3 *dwc)
1650 * According to the Databook Remote wakeup request should
1651 * be issued only when the device is in early suspend state.
1653 * We can check that via USB Link State bits in DSTS register.
1655 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1657 speed = reg & DWC3_DSTS_CONNECTSPD;
1658 if ((speed == DWC3_DSTS_SUPERSPEED) ||
1659 (speed == DWC3_DSTS_SUPERSPEED_PLUS))
1662 link_state = DWC3_DSTS_USBLNKST(reg);
1664 switch (link_state) {
1665 case DWC3_LINK_STATE_RX_DET: /* in HS, means Early Suspend */
1666 case DWC3_LINK_STATE_U3: /* in HS, means SUSPEND */
1672 ret = dwc3_gadget_set_link_state(dwc, DWC3_LINK_STATE_RECOV);
1674 dev_err(dwc->dev, "failed to put link in Recovery\n");
1678 /* Recent versions do this automatically */
1679 if (dwc->revision < DWC3_REVISION_194A) {
1680 /* write zeroes to Link Change Request */
1681 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
1682 reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK;
1683 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1686 /* poll until Link State changes to ON */
1690 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1692 /* in HS, means ON */
1693 if (DWC3_DSTS_USBLNKST(reg) == DWC3_LINK_STATE_U0)
1697 if (DWC3_DSTS_USBLNKST(reg) != DWC3_LINK_STATE_U0) {
1698 dev_err(dwc->dev, "failed to send remote wakeup\n");
1705 static int dwc3_gadget_wakeup(struct usb_gadget *g)
1707 struct dwc3 *dwc = gadget_to_dwc(g);
1708 unsigned long flags;
1711 spin_lock_irqsave(&dwc->lock, flags);
1712 ret = __dwc3_gadget_wakeup(dwc);
1713 spin_unlock_irqrestore(&dwc->lock, flags);
1718 static int dwc3_gadget_set_selfpowered(struct usb_gadget *g,
1721 struct dwc3 *dwc = gadget_to_dwc(g);
1722 unsigned long flags;
1724 spin_lock_irqsave(&dwc->lock, flags);
1725 g->is_selfpowered = !!is_selfpowered;
1726 spin_unlock_irqrestore(&dwc->lock, flags);
1731 int dwc3_gadget_run_stop(struct dwc3 *dwc, int is_on, int suspend)
1736 if (pm_runtime_suspended(dwc->dev))
1739 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
1741 if (dwc->revision <= DWC3_REVISION_187A) {
1742 reg &= ~DWC3_DCTL_TRGTULST_MASK;
1743 reg |= DWC3_DCTL_TRGTULST_RX_DET;
1746 if (dwc->revision >= DWC3_REVISION_194A)
1747 reg &= ~DWC3_DCTL_KEEP_CONNECT;
1748 reg |= DWC3_DCTL_RUN_STOP;
1750 if (dwc->has_hibernation)
1751 reg |= DWC3_DCTL_KEEP_CONNECT;
1753 dwc->pullups_connected = true;
1755 reg &= ~DWC3_DCTL_RUN_STOP;
1757 if (dwc->has_hibernation && !suspend)
1758 reg &= ~DWC3_DCTL_KEEP_CONNECT;
1760 dwc->pullups_connected = false;
1763 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1766 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1767 reg &= DWC3_DSTS_DEVCTRLHLT;
1768 } while (--timeout && !(!is_on ^ !reg));
1776 static int dwc3_gadget_pullup(struct usb_gadget *g, int is_on)
1778 struct dwc3 *dwc = gadget_to_dwc(g);
1779 unsigned long flags;
1785 * Per databook, when we want to stop the gadget, if a control transfer
1786 * is still in process, complete it and get the core into setup phase.
1788 if (!is_on && dwc->ep0state != EP0_SETUP_PHASE) {
1789 reinit_completion(&dwc->ep0_in_setup);
1791 ret = wait_for_completion_timeout(&dwc->ep0_in_setup,
1792 msecs_to_jiffies(DWC3_PULL_UP_TIMEOUT));
1794 dev_err(dwc->dev, "timed out waiting for SETUP phase\n");
1799 spin_lock_irqsave(&dwc->lock, flags);
1800 ret = dwc3_gadget_run_stop(dwc, is_on, false);
1801 spin_unlock_irqrestore(&dwc->lock, flags);
1806 void dwc3_gadget_enable_irq(struct dwc3 *dwc)
1810 /* Enable all but Start and End of Frame IRQs */
1811 reg = (DWC3_DEVTEN_VNDRDEVTSTRCVEDEN |
1812 DWC3_DEVTEN_EVNTOVERFLOWEN |
1813 DWC3_DEVTEN_CMDCMPLTEN |
1814 DWC3_DEVTEN_ERRTICERREN |
1815 DWC3_DEVTEN_WKUPEVTEN |
1816 DWC3_DEVTEN_CONNECTDONEEN |
1817 DWC3_DEVTEN_USBRSTEN |
1818 DWC3_DEVTEN_DISCONNEVTEN);
1820 /* Enable hibernation IRQ */
1821 if (dwc->has_hibernation)
1822 reg |= DWC3_DEVTEN_HIBERNATIONREQEVTEN;
1824 if (dwc->revision < DWC3_REVISION_250A)
1825 reg |= DWC3_DEVTEN_ULSTCNGEN;
1827 dwc3_writel(dwc->regs, DWC3_DEVTEN, reg);
1830 void dwc3_gadget_disable_irq(struct dwc3 *dwc)
1832 /* mask all interrupts */
1833 dwc3_writel(dwc->regs, DWC3_DEVTEN, 0x00);
1836 static irqreturn_t dwc3_interrupt(int irq, void *_dwc);
1837 static irqreturn_t dwc3_thread_interrupt(int irq, void *_dwc);
1840 * dwc3_gadget_setup_nump - calculate and initialize NUMP field of %DWC3_DCFG
1841 * @dwc: pointer to our context structure
1843 * The following looks like complex but it's actually very simple. In order to
1844 * calculate the number of packets we can burst at once on OUT transfers, we're
1845 * gonna use RxFIFO size.
1847 * To calculate RxFIFO size we need two numbers:
1848 * MDWIDTH = size, in bits, of the internal memory bus
1849 * RAM2_DEPTH = depth, in MDWIDTH, of internal RAM2 (where RxFIFO sits)
1851 * Given these two numbers, the formula is simple:
1853 * RxFIFO Size = (RAM2_DEPTH * MDWIDTH / 8) - 24 - 16;
1855 * 24 bytes is for 3x SETUP packets
1856 * 16 bytes is a clock domain crossing tolerance
1858 * Given RxFIFO Size, NUMP = RxFIFOSize / 1024;
1860 static void dwc3_gadget_setup_nump(struct dwc3 *dwc)
1867 ram2_depth = DWC3_GHWPARAMS7_RAM2_DEPTH(dwc->hwparams.hwparams7);
1868 mdwidth = DWC3_GHWPARAMS0_MDWIDTH(dwc->hwparams.hwparams0);
1870 nump = ((ram2_depth * mdwidth / 8) - 24 - 16) / 1024;
1871 nump = min_t(u32, nump, 16);
1874 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
1875 reg &= ~DWC3_DCFG_NUMP_MASK;
1876 reg |= nump << DWC3_DCFG_NUMP_SHIFT;
1877 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
1880 static int __dwc3_gadget_start(struct dwc3 *dwc)
1882 struct dwc3_ep *dep;
1887 * Use IMOD if enabled via dwc->imod_interval. Otherwise, if
1888 * the core supports IMOD, disable it.
1890 if (dwc->imod_interval) {
1891 dwc3_writel(dwc->regs, DWC3_DEV_IMOD(0), dwc->imod_interval);
1892 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), DWC3_GEVNTCOUNT_EHB);
1893 } else if (dwc3_has_imod(dwc)) {
1894 dwc3_writel(dwc->regs, DWC3_DEV_IMOD(0), 0);
1898 * We are telling dwc3 that we want to use DCFG.NUMP as ACK TP's NUMP
1899 * field instead of letting dwc3 itself calculate that automatically.
1901 * This way, we maximize the chances that we'll be able to get several
1902 * bursts of data without going through any sort of endpoint throttling.
1904 reg = dwc3_readl(dwc->regs, DWC3_GRXTHRCFG);
1905 if (dwc3_is_usb31(dwc))
1906 reg &= ~DWC31_GRXTHRCFG_PKTCNTSEL;
1908 reg &= ~DWC3_GRXTHRCFG_PKTCNTSEL;
1910 dwc3_writel(dwc->regs, DWC3_GRXTHRCFG, reg);
1912 dwc3_gadget_setup_nump(dwc);
1914 /* For OTG mode, check if the core is currently in Host mode.
1915 * This is not an error condition as there are times when the core is
1916 * working as host and kernel is told to initiate bind operation with
1917 * gadget class driver module.
1918 * The below remaining operations are handled in OTG driver whenever
1921 if (dwc3_readl(dwc->regs, DWC3_GSTS) & DWC3_GSTS_CUR_MODE)
1924 /* Start with SuperSpeed Default */
1925 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
1928 ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_INIT);
1930 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
1935 ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_INIT);
1937 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
1941 /* begin to receive SETUP packets */
1942 dwc->ep0state = EP0_SETUP_PHASE;
1943 dwc3_ep0_out_start(dwc);
1945 dwc3_gadget_enable_irq(dwc);
1950 __dwc3_gadget_ep_disable(dwc->eps[0]);
1956 static irqreturn_t wakeup_interrupt(int irq, void *_dwc);
1957 static int dwc3_gadget_start(struct usb_gadget *g,
1958 struct usb_gadget_driver *driver)
1960 struct dwc3 *dwc = gadget_to_dwc(g);
1961 unsigned long flags;
1965 irq = dwc->irq_gadget;
1966 ret = request_threaded_irq(irq, dwc3_interrupt, dwc3_thread_interrupt,
1967 IRQF_SHARED, "dwc3", dwc->ev_buf);
1969 dev_err(dwc->dev, "failed to request irq #%d --> %d\n",
1974 /* look for wakeup interrupt if hibernation is supported */
1975 if (dwc->has_hibernation) {
1976 irq = dwc->irq_wakeup;
1977 ret = devm_request_irq(dwc->dev, irq, wakeup_interrupt,
1978 IRQF_SHARED, "usb-wakeup", dwc);
1980 dev_err(dwc->dev, "failed to request wakeup irq #%d --> %d\n",
1986 spin_lock_irqsave(&dwc->lock, flags);
1987 if (dwc->gadget_driver) {
1988 dev_err(dwc->dev, "%s is already bound to %s\n",
1990 dwc->gadget_driver->driver.name);
1995 dwc->gadget_driver = driver;
1997 if (pm_runtime_active(dwc->dev))
1998 __dwc3_gadget_start(dwc);
2000 spin_unlock_irqrestore(&dwc->lock, flags);
2005 spin_unlock_irqrestore(&dwc->lock, flags);
2006 if (dwc->irq_gadget)
2007 free_irq(dwc->irq_gadget, dwc->ev_buf);
2008 if (dwc->irq_wakeup)
2009 free_irq(dwc->irq_wakeup, dwc);
2015 static void __dwc3_gadget_stop(struct dwc3 *dwc)
2017 dwc3_gadget_disable_irq(dwc);
2018 __dwc3_gadget_ep_disable(dwc->eps[0]);
2019 __dwc3_gadget_ep_disable(dwc->eps[1]);
2022 static int dwc3_gadget_stop(struct usb_gadget *g)
2024 struct dwc3 *dwc = gadget_to_dwc(g);
2025 unsigned long flags;
2029 spin_lock_irqsave(&dwc->lock, flags);
2031 if (pm_runtime_suspended(dwc->dev))
2034 __dwc3_gadget_stop(dwc);
2036 for (epnum = 2; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
2037 struct dwc3_ep *dep = dwc->eps[epnum];
2043 if (!(dep->flags & DWC3_EP_END_TRANSFER_PENDING))
2046 ret = wait_event_interruptible_lock_irq_timeout(dep->wait_end_transfer,
2047 !(dep->flags & DWC3_EP_END_TRANSFER_PENDING),
2048 dwc->lock, msecs_to_jiffies(5));
2051 /* Timed out or interrupted! There's nothing much
2052 * we can do so we just log here and print which
2053 * endpoints timed out at the end.
2055 tmo_eps |= 1 << epnum;
2056 dep->flags &= DWC3_EP_END_TRANSFER_PENDING;
2062 "end transfer timed out on endpoints 0x%x [bitmap]\n",
2067 dwc->gadget_driver = NULL;
2068 spin_unlock_irqrestore(&dwc->lock, flags);
2070 free_irq(dwc->irq_gadget, dwc->ev_buf);
2071 free_irq(dwc->irq_wakeup, dwc);
2076 static void dwc3_gadget_set_speed(struct usb_gadget *g,
2077 enum usb_device_speed speed)
2079 struct dwc3 *dwc = gadget_to_dwc(g);
2080 unsigned long flags;
2083 spin_lock_irqsave(&dwc->lock, flags);
2084 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2085 reg &= ~(DWC3_DCFG_SPEED_MASK);
2088 * WORKAROUND: DWC3 revision < 2.20a have an issue
2089 * which would cause metastability state on Run/Stop
2090 * bit if we try to force the IP to USB2-only mode.
2092 * Because of that, we cannot configure the IP to any
2093 * speed other than the SuperSpeed
2097 * STAR#9000525659: Clock Domain Crossing on DCTL in
2100 if (dwc->revision < DWC3_REVISION_220A &&
2101 !dwc->dis_metastability_quirk) {
2102 reg |= DWC3_DCFG_SUPERSPEED;
2106 reg |= DWC3_DCFG_LOWSPEED;
2108 case USB_SPEED_FULL:
2109 reg |= DWC3_DCFG_FULLSPEED;
2111 case USB_SPEED_HIGH:
2112 reg |= DWC3_DCFG_HIGHSPEED;
2114 case USB_SPEED_SUPER:
2115 reg |= DWC3_DCFG_SUPERSPEED;
2117 case USB_SPEED_SUPER_PLUS:
2118 if (dwc3_is_usb31(dwc))
2119 reg |= DWC3_DCFG_SUPERSPEED_PLUS;
2121 reg |= DWC3_DCFG_SUPERSPEED;
2124 dev_err(dwc->dev, "invalid speed (%d)\n", speed);
2126 if (dwc->revision & DWC3_REVISION_IS_DWC31)
2127 reg |= DWC3_DCFG_SUPERSPEED_PLUS;
2129 reg |= DWC3_DCFG_SUPERSPEED;
2132 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
2134 spin_unlock_irqrestore(&dwc->lock, flags);
2137 static const struct usb_gadget_ops dwc3_gadget_ops = {
2138 .get_frame = dwc3_gadget_get_frame,
2139 .wakeup = dwc3_gadget_wakeup,
2140 .set_selfpowered = dwc3_gadget_set_selfpowered,
2141 .pullup = dwc3_gadget_pullup,
2142 .udc_start = dwc3_gadget_start,
2143 .udc_stop = dwc3_gadget_stop,
2144 .udc_set_speed = dwc3_gadget_set_speed,
2147 /* -------------------------------------------------------------------------- */
2149 static int dwc3_gadget_init_control_endpoint(struct dwc3_ep *dep)
2151 struct dwc3 *dwc = dep->dwc;
2153 usb_ep_set_maxpacket_limit(&dep->endpoint, 512);
2154 dep->endpoint.maxburst = 1;
2155 dep->endpoint.ops = &dwc3_gadget_ep0_ops;
2156 if (!dep->direction)
2157 dwc->gadget.ep0 = &dep->endpoint;
2159 dep->endpoint.caps.type_control = true;
2164 static int dwc3_gadget_init_in_endpoint(struct dwc3_ep *dep)
2166 struct dwc3 *dwc = dep->dwc;
2171 mdwidth = DWC3_MDWIDTH(dwc->hwparams.hwparams0);
2172 /* MDWIDTH is represented in bits, we need it in bytes */
2175 size = dwc3_readl(dwc->regs, DWC3_GTXFIFOSIZ(dep->number >> 1));
2176 if (dwc3_is_usb31(dwc))
2177 size = DWC31_GTXFIFOSIZ_TXFDEF(size);
2179 size = DWC3_GTXFIFOSIZ_TXFDEF(size);
2181 /* FIFO Depth is in MDWDITH bytes. Multiply */
2184 kbytes = size / 1024;
2189 * FIFO sizes account an extra MDWIDTH * (kbytes + 1) bytes for
2190 * internal overhead. We don't really know how these are used,
2191 * but documentation say it exists.
2193 size -= mdwidth * (kbytes + 1);
2196 usb_ep_set_maxpacket_limit(&dep->endpoint, size);
2198 dep->endpoint.max_streams = 15;
2199 dep->endpoint.ops = &dwc3_gadget_ep_ops;
2200 list_add_tail(&dep->endpoint.ep_list,
2201 &dwc->gadget.ep_list);
2202 dep->endpoint.caps.type_iso = true;
2203 dep->endpoint.caps.type_bulk = true;
2204 dep->endpoint.caps.type_int = true;
2206 return dwc3_alloc_trb_pool(dep);
2209 static int dwc3_gadget_init_out_endpoint(struct dwc3_ep *dep)
2211 struct dwc3 *dwc = dep->dwc;
2213 usb_ep_set_maxpacket_limit(&dep->endpoint, 1024);
2214 dep->endpoint.max_streams = 15;
2215 dep->endpoint.ops = &dwc3_gadget_ep_ops;
2216 list_add_tail(&dep->endpoint.ep_list,
2217 &dwc->gadget.ep_list);
2218 dep->endpoint.caps.type_iso = true;
2219 dep->endpoint.caps.type_bulk = true;
2220 dep->endpoint.caps.type_int = true;
2222 return dwc3_alloc_trb_pool(dep);
2225 static int dwc3_gadget_init_endpoint(struct dwc3 *dwc, u8 epnum)
2227 struct dwc3_ep *dep;
2228 bool direction = epnum & 1;
2230 u8 num = epnum >> 1;
2232 dep = kzalloc(sizeof(*dep), GFP_KERNEL);
2237 dep->number = epnum;
2238 dep->direction = direction;
2239 dep->regs = dwc->regs + DWC3_DEP_BASE(epnum);
2240 dwc->eps[epnum] = dep;
2242 snprintf(dep->name, sizeof(dep->name), "ep%u%s", num,
2243 direction ? "in" : "out");
2245 dep->endpoint.name = dep->name;
2247 if (!(dep->number > 1)) {
2248 dep->endpoint.desc = &dwc3_gadget_ep0_desc;
2249 dep->endpoint.comp_desc = NULL;
2252 spin_lock_init(&dep->lock);
2255 ret = dwc3_gadget_init_control_endpoint(dep);
2257 ret = dwc3_gadget_init_in_endpoint(dep);
2259 ret = dwc3_gadget_init_out_endpoint(dep);
2264 dep->endpoint.caps.dir_in = direction;
2265 dep->endpoint.caps.dir_out = !direction;
2267 INIT_LIST_HEAD(&dep->pending_list);
2268 INIT_LIST_HEAD(&dep->started_list);
2273 static int dwc3_gadget_init_endpoints(struct dwc3 *dwc, u8 total)
2277 INIT_LIST_HEAD(&dwc->gadget.ep_list);
2279 for (epnum = 0; epnum < total; epnum++) {
2282 ret = dwc3_gadget_init_endpoint(dwc, epnum);
2290 static void dwc3_gadget_free_endpoints(struct dwc3 *dwc)
2292 struct dwc3_ep *dep;
2295 for (epnum = 0; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
2296 dep = dwc->eps[epnum];
2300 * Physical endpoints 0 and 1 are special; they form the
2301 * bi-directional USB endpoint 0.
2303 * For those two physical endpoints, we don't allocate a TRB
2304 * pool nor do we add them the endpoints list. Due to that, we
2305 * shouldn't do these two operations otherwise we would end up
2306 * with all sorts of bugs when removing dwc3.ko.
2308 if (epnum != 0 && epnum != 1) {
2309 dwc3_free_trb_pool(dep);
2310 list_del(&dep->endpoint.ep_list);
2317 /* -------------------------------------------------------------------------- */
2319 static int dwc3_gadget_ep_reclaim_completed_trb(struct dwc3_ep *dep,
2320 struct dwc3_request *req, struct dwc3_trb *trb,
2321 const struct dwc3_event_depevt *event, int status, int chain)
2325 dwc3_ep_inc_deq(dep);
2327 trace_dwc3_complete_trb(dep, trb);
2330 * If we're in the middle of series of chained TRBs and we
2331 * receive a short transfer along the way, DWC3 will skip
2332 * through all TRBs including the last TRB in the chain (the
2333 * where CHN bit is zero. DWC3 will also avoid clearing HWO
2334 * bit and SW has to do it manually.
2336 * We're going to do that here to avoid problems of HW trying
2337 * to use bogus TRBs for transfers.
2339 if (chain && (trb->ctrl & DWC3_TRB_CTRL_HWO))
2340 trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
2343 * If we're dealing with unaligned size OUT transfer, we will be left
2344 * with one TRB pending in the ring. We need to manually clear HWO bit
2347 if ((req->zero || req->unaligned) && (trb->ctrl & DWC3_TRB_CTRL_HWO)) {
2348 trb->ctrl &= ~DWC3_TRB_CTRL_HWO;
2352 count = trb->size & DWC3_TRB_SIZE_MASK;
2353 req->remaining += count;
2355 if ((trb->ctrl & DWC3_TRB_CTRL_HWO) && status != -ESHUTDOWN)
2358 if (event->status & DEPEVT_STATUS_SHORT && !chain)
2361 if ((event->status & DEPEVT_STATUS_IOC) &&
2362 (trb->ctrl & DWC3_TRB_CTRL_IOC))
2365 if ((event->status & DEPEVT_STATUS_LST) &&
2366 (trb->ctrl & DWC3_TRB_CTRL_LST))
2372 static int dwc3_gadget_ep_reclaim_trb_sg(struct dwc3_ep *dep,
2373 struct dwc3_request *req, const struct dwc3_event_depevt *event,
2376 struct dwc3_trb *trb = &dep->trb_pool[dep->trb_dequeue];
2377 struct scatterlist *sg = req->sg;
2378 struct scatterlist *s;
2379 unsigned int pending = req->num_pending_sgs;
2383 for_each_sg(sg, s, pending, i) {
2384 trb = &dep->trb_pool[dep->trb_dequeue];
2386 if (trb->ctrl & DWC3_TRB_CTRL_HWO)
2389 req->sg = sg_next(s);
2390 req->num_pending_sgs--;
2392 ret = dwc3_gadget_ep_reclaim_completed_trb(dep, req,
2393 trb, event, status, true);
2401 static int dwc3_gadget_ep_reclaim_trb_linear(struct dwc3_ep *dep,
2402 struct dwc3_request *req, const struct dwc3_event_depevt *event,
2405 struct dwc3_trb *trb = &dep->trb_pool[dep->trb_dequeue];
2407 return dwc3_gadget_ep_reclaim_completed_trb(dep, req, trb,
2408 event, status, false);
2411 static bool dwc3_gadget_ep_request_completed(struct dwc3_request *req)
2413 return req->request.actual == req->request.length;
2416 static int dwc3_gadget_ep_cleanup_completed_request(struct dwc3_ep *dep,
2417 const struct dwc3_event_depevt *event,
2418 struct dwc3_request *req, int status)
2422 if (req->num_pending_sgs)
2423 ret = dwc3_gadget_ep_reclaim_trb_sg(dep, req, event,
2426 ret = dwc3_gadget_ep_reclaim_trb_linear(dep, req, event,
2429 if (req->unaligned || req->zero) {
2430 ret = dwc3_gadget_ep_reclaim_trb_linear(dep, req, event,
2432 req->unaligned = false;
2436 req->request.actual = req->request.length - req->remaining;
2438 if (!dwc3_gadget_ep_request_completed(req) &&
2439 req->num_pending_sgs) {
2440 __dwc3_gadget_kick_transfer(dep);
2444 dwc3_gadget_giveback(dep, req, status);
2450 static void dwc3_gadget_ep_cleanup_completed_requests(struct dwc3_ep *dep,
2451 const struct dwc3_event_depevt *event, int status)
2453 struct dwc3_request *req;
2454 struct dwc3_request *tmp;
2456 list_for_each_entry_safe(req, tmp, &dep->started_list, list) {
2459 ret = dwc3_gadget_ep_cleanup_completed_request(dep, event,
2466 static void dwc3_gadget_endpoint_frame_from_event(struct dwc3_ep *dep,
2467 const struct dwc3_event_depevt *event)
2469 dep->frame_number = event->parameters;
2472 static void dwc3_gadget_endpoint_transfer_in_progress(struct dwc3_ep *dep,
2473 const struct dwc3_event_depevt *event)
2475 struct dwc3 *dwc = dep->dwc;
2476 unsigned status = 0;
2479 dwc3_gadget_endpoint_frame_from_event(dep, event);
2481 if (event->status & DEPEVT_STATUS_BUSERR)
2482 status = -ECONNRESET;
2484 if ((event->status & DEPEVT_STATUS_MISSED_ISOC) &&
2485 usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
2488 if (list_empty(&dep->started_list))
2492 dwc3_gadget_ep_cleanup_completed_requests(dep, event, status);
2494 if (dep->stream_capable && !list_empty(&dep->started_list))
2495 __dwc3_gadget_kick_transfer(dep);
2498 dwc3_stop_active_transfer(dep, true);
2499 dep->flags = DWC3_EP_ENABLED;
2503 * WORKAROUND: This is the 2nd half of U1/U2 -> U0 workaround.
2504 * See dwc3_gadget_linksts_change_interrupt() for 1st half.
2506 if (dwc->revision < DWC3_REVISION_183A) {
2510 for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) {
2513 if (!(dep->flags & DWC3_EP_ENABLED))
2516 if (!list_empty(&dep->started_list))
2520 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2522 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2528 static void dwc3_gadget_endpoint_transfer_not_ready(struct dwc3_ep *dep,
2529 const struct dwc3_event_depevt *event)
2531 dwc3_gadget_endpoint_frame_from_event(dep, event);
2532 __dwc3_gadget_start_isoc(dep);
2535 static void dwc3_endpoint_stream_event(struct dwc3 *dwc,
2536 const struct dwc3_event_depevt *event)
2538 struct dwc3_ep *dep;
2539 struct dwc3_request *req;
2540 u8 epnum = event->endpoint_number;
2543 dep = dwc->eps[epnum];
2545 stream_id = event->parameters;
2547 /* Check for request matching the streamid and delete the timer */
2548 list_for_each_entry(req, &dep->started_list, list) {
2549 if (req->request.stream_id == stream_id) {
2550 if (timer_pending(&req->stream_timeout_timer))
2551 del_timer(&req->stream_timeout_timer);
2557 static void dwc3_endpoint_interrupt(struct dwc3 *dwc,
2558 const struct dwc3_event_depevt *event)
2560 struct dwc3_ep *dep;
2561 u8 epnum = event->endpoint_number;
2564 dep = dwc->eps[epnum];
2566 if (!(dep->flags & DWC3_EP_ENABLED)) {
2567 if (!(dep->flags & DWC3_EP_END_TRANSFER_PENDING))
2570 /* Handle only EPCMDCMPLT when EP disabled */
2571 if (event->endpoint_event != DWC3_DEPEVT_EPCMDCMPLT)
2575 if (epnum == 0 || epnum == 1) {
2576 dwc3_ep0_interrupt(dwc, event);
2580 switch (event->endpoint_event) {
2581 case DWC3_DEPEVT_XFERCOMPLETE:
2582 if (!dep->stream_capable)
2584 dep->flags &= ~DWC3_EP_TRANSFER_STARTED;
2586 case DWC3_DEPEVT_XFERINPROGRESS:
2587 dwc3_gadget_endpoint_transfer_in_progress(dep, event);
2589 case DWC3_DEPEVT_XFERNOTREADY:
2590 dwc3_gadget_endpoint_transfer_not_ready(dep, event);
2592 case DWC3_DEPEVT_STREAMEVT:
2593 if (event->status == DEPEVT_STREAMEVT_FOUND)
2594 dwc3_endpoint_stream_event(dwc, event);
2596 case DWC3_DEPEVT_EPCMDCMPLT:
2597 cmd = DEPEVT_PARAMETER_CMD(event->parameters);
2599 if (cmd == DWC3_DEPCMD_ENDTRANSFER) {
2600 dep->flags &= ~DWC3_EP_END_TRANSFER_PENDING;
2601 wake_up(&dep->wait_end_transfer);
2604 case DWC3_DEPEVT_RXTXFIFOEVT:
2609 static void dwc3_disconnect_gadget(struct dwc3 *dwc)
2611 if (dwc->gadget_driver && dwc->gadget_driver->disconnect) {
2612 spin_unlock(&dwc->lock);
2613 dwc->gadget_driver->disconnect(&dwc->gadget);
2614 spin_lock(&dwc->lock);
2618 static void dwc3_suspend_gadget(struct dwc3 *dwc)
2620 if (dwc->gadget_driver && dwc->gadget_driver->suspend) {
2621 spin_unlock(&dwc->lock);
2622 dwc->gadget_driver->suspend(&dwc->gadget);
2623 spin_lock(&dwc->lock);
2627 static void dwc3_resume_gadget(struct dwc3 *dwc)
2629 if (dwc->gadget_driver && dwc->gadget_driver->resume) {
2630 spin_unlock(&dwc->lock);
2631 dwc->gadget_driver->resume(&dwc->gadget);
2632 spin_lock(&dwc->lock);
2636 static void dwc3_reset_gadget(struct dwc3 *dwc)
2638 if (!dwc->gadget_driver)
2641 if (dwc->gadget.speed != USB_SPEED_UNKNOWN) {
2642 spin_unlock(&dwc->lock);
2643 usb_gadget_udc_reset(&dwc->gadget, dwc->gadget_driver);
2644 spin_lock(&dwc->lock);
2648 void dwc3_stop_active_transfer(struct dwc3_ep *dep, bool force)
2650 struct dwc3 *dwc = dep->dwc;
2651 struct dwc3_gadget_ep_cmd_params params;
2655 if ((dep->flags & DWC3_EP_END_TRANSFER_PENDING) ||
2656 !dep->resource_index)
2660 * NOTICE: We are violating what the Databook says about the
2661 * EndTransfer command. Ideally we would _always_ wait for the
2662 * EndTransfer Command Completion IRQ, but that's causing too
2663 * much trouble synchronizing between us and gadget driver.
2665 * We have discussed this with the IP Provider and it was
2666 * suggested to giveback all requests here, but give HW some
2667 * extra time to synchronize with the interconnect. We're using
2668 * an arbitrary 100us delay for that.
2670 * Note also that a similar handling was tested by Synopsys
2671 * (thanks a lot Paul) and nothing bad has come out of it.
2672 * In short, what we're doing is:
2674 * - Issue EndTransfer WITH CMDIOC bit set
2677 * As of IP version 3.10a of the DWC_usb3 IP, the controller
2678 * supports a mode to work around the above limitation. The
2679 * software can poll the CMDACT bit in the DEPCMD register
2680 * after issuing a EndTransfer command. This mode is enabled
2681 * by writing GUCTL2[14]. This polling is already done in the
2682 * dwc3_send_gadget_ep_cmd() function so if the mode is
2683 * enabled, the EndTransfer command will have completed upon
2684 * returning from this function and we don't need to delay for
2687 * This mode is NOT available on the DWC_usb31 IP.
2690 cmd = DWC3_DEPCMD_ENDTRANSFER;
2691 cmd |= force ? DWC3_DEPCMD_HIPRI_FORCERM : 0;
2692 cmd |= DWC3_DEPCMD_CMDIOC;
2693 cmd |= DWC3_DEPCMD_PARAM(dep->resource_index);
2694 memset(¶ms, 0, sizeof(params));
2695 ret = dwc3_send_gadget_ep_cmd(dep, cmd, ¶ms);
2697 dep->resource_index = 0;
2700 * when transfer is stopped with force rm bit false, it can be
2701 * restarted by passing resource_index in params; don't loose it
2704 dep->resource_index = 0;
2706 if (dwc3_is_usb31(dwc) || dwc->revision < DWC3_REVISION_310A) {
2708 * CMD COMPLETE interrupt is not getting generated for isoc
2709 * endpoints, so don't set DWC3_EP_END_TRANSFER_PENDING flag
2711 if (!usb_endpoint_xfer_isoc(dep->endpoint.desc))
2712 dep->flags |= DWC3_EP_END_TRANSFER_PENDING;
2718 static void dwc3_clear_stall_all_ep(struct dwc3 *dwc)
2722 for (epnum = 1; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
2723 struct dwc3_ep *dep;
2726 dep = dwc->eps[epnum];
2730 if (!(dep->flags & DWC3_EP_STALL))
2733 dep->flags &= ~DWC3_EP_STALL;
2735 ret = dwc3_send_clear_stall_ep_cmd(dep);
2740 static void dwc3_gadget_disconnect_interrupt(struct dwc3 *dwc)
2744 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2745 reg &= ~DWC3_DCTL_INITU1ENA;
2746 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2748 reg &= ~DWC3_DCTL_INITU2ENA;
2749 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2751 dwc3_disconnect_gadget(dwc);
2753 /* In USB 2.0, to avoid hibernation interrupt at the time of connection
2754 * clear DWC3_DCTL_KEEP_CONNECT bit.
2756 if (dwc->has_hibernation) {
2757 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2758 reg &= ~DWC3_DCTL_KEEP_CONNECT;
2759 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2762 dwc->gadget.speed = USB_SPEED_UNKNOWN;
2763 dwc->setup_packet_pending = false;
2764 usb_gadget_set_state(&dwc->gadget, USB_STATE_NOTATTACHED);
2766 dwc->connected = false;
2769 static void dwc3_gadget_reset_interrupt(struct dwc3 *dwc)
2773 dwc->connected = true;
2776 * WORKAROUND: DWC3 revisions <1.88a have an issue which
2777 * would cause a missing Disconnect Event if there's a
2778 * pending Setup Packet in the FIFO.
2780 * There's no suggested workaround on the official Bug
2781 * report, which states that "unless the driver/application
2782 * is doing any special handling of a disconnect event,
2783 * there is no functional issue".
2785 * Unfortunately, it turns out that we _do_ some special
2786 * handling of a disconnect event, namely complete all
2787 * pending transfers, notify gadget driver of the
2788 * disconnection, and so on.
2790 * Our suggested workaround is to follow the Disconnect
2791 * Event steps here, instead, based on a setup_packet_pending
2792 * flag. Such flag gets set whenever we have a SETUP_PENDING
2793 * status for EP0 TRBs and gets cleared on XferComplete for the
2798 * STAR#9000466709: RTL: Device : Disconnect event not
2799 * generated if setup packet pending in FIFO
2801 if (dwc->revision < DWC3_REVISION_188A) {
2802 if (dwc->setup_packet_pending)
2803 dwc3_gadget_disconnect_interrupt(dwc);
2806 dwc3_reset_gadget(dwc);
2808 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2809 reg &= ~DWC3_DCTL_TSTCTRL_MASK;
2810 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2811 dwc->test_mode = false;
2812 dwc3_clear_stall_all_ep(dwc);
2814 /* Reset device address to zero */
2815 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2816 reg &= ~(DWC3_DCFG_DEVADDR_MASK);
2817 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
2820 static void dwc3_gadget_conndone_interrupt(struct dwc3 *dwc)
2822 struct dwc3_ep *dep;
2827 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
2828 speed = reg & DWC3_DSTS_CONNECTSPD;
2832 * RAMClkSel is reset to 0 after USB reset, so it must be reprogrammed
2833 * each time on Connect Done.
2835 * Currently we always use the reset value. If any platform
2836 * wants to set this to a different value, we need to add a
2837 * setting and update GCTL.RAMCLKSEL here.
2841 case DWC3_DSTS_SUPERSPEED_PLUS:
2842 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
2843 dwc->gadget.ep0->maxpacket = 512;
2844 dwc->gadget.speed = USB_SPEED_SUPER_PLUS;
2846 case DWC3_DSTS_SUPERSPEED:
2848 * WORKAROUND: DWC3 revisions <1.90a have an issue which
2849 * would cause a missing USB3 Reset event.
2851 * In such situations, we should force a USB3 Reset
2852 * event by calling our dwc3_gadget_reset_interrupt()
2857 * STAR#9000483510: RTL: SS : USB3 reset event may
2858 * not be generated always when the link enters poll
2860 if (dwc->revision < DWC3_REVISION_190A)
2861 dwc3_gadget_reset_interrupt(dwc);
2863 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
2864 dwc->gadget.ep0->maxpacket = 512;
2865 dwc->gadget.speed = USB_SPEED_SUPER;
2867 case DWC3_DSTS_HIGHSPEED:
2868 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
2869 dwc->gadget.ep0->maxpacket = 64;
2870 dwc->gadget.speed = USB_SPEED_HIGH;
2872 case DWC3_DSTS_FULLSPEED:
2873 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
2874 dwc->gadget.ep0->maxpacket = 64;
2875 dwc->gadget.speed = USB_SPEED_FULL;
2877 case DWC3_DSTS_LOWSPEED:
2878 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(8);
2879 dwc->gadget.ep0->maxpacket = 8;
2880 dwc->gadget.speed = USB_SPEED_LOW;
2884 dwc->eps[1]->endpoint.maxpacket = dwc->gadget.ep0->maxpacket;
2886 /* Enable USB2 LPM Capability */
2888 if ((dwc->revision > DWC3_REVISION_194A) &&
2889 (speed != DWC3_DSTS_SUPERSPEED) &&
2890 (speed != DWC3_DSTS_SUPERSPEED_PLUS)) {
2891 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2892 reg |= DWC3_DCFG_LPM_CAP;
2893 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
2895 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2896 reg &= ~(DWC3_DCTL_HIRD_THRES_MASK | DWC3_DCTL_L1_HIBER_EN);
2898 reg |= DWC3_DCTL_HIRD_THRES(dwc->hird_threshold);
2901 * When dwc3 revisions >= 2.40a, LPM Erratum is enabled and
2902 * DCFG.LPMCap is set, core responses with an ACK and the
2903 * BESL value in the LPM token is less than or equal to LPM
2906 WARN_ONCE(dwc->revision < DWC3_REVISION_240A
2907 && dwc->has_lpm_erratum,
2908 "LPM Erratum not available on dwc3 revisions < 2.40a\n");
2910 if (dwc->has_lpm_erratum && dwc->revision >= DWC3_REVISION_240A)
2911 reg |= DWC3_DCTL_LPM_ERRATA(dwc->lpm_nyet_threshold);
2913 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2915 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2916 reg &= ~DWC3_DCTL_HIRD_THRES_MASK;
2917 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2921 ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_MODIFY);
2923 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
2928 ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_MODIFY);
2930 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
2935 * In USB 2.0, to avoid hibernation interrupt at the time of connection
2936 * set DWC3_DCTL_KEEP_CONNECT bit here
2938 if (dwc->has_hibernation) {
2939 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2940 reg |= DWC3_DCTL_KEEP_CONNECT;
2941 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2945 * Configure PHY via GUSB3PIPECTLn if required.
2947 * Update GTXFIFOSIZn
2949 * In both cases reset values should be sufficient.
2953 static void dwc3_gadget_wakeup_interrupt(struct dwc3 *dwc)
2956 * TODO take core out of low power mode when that's
2960 if (dwc->gadget_driver && dwc->gadget_driver->resume) {
2961 spin_unlock(&dwc->lock);
2962 dwc->gadget_driver->resume(&dwc->gadget);
2963 spin_lock(&dwc->lock);
2967 static irqreturn_t wakeup_interrupt(int irq, void *_dwc)
2969 struct dwc3 *dwc = (struct dwc3 *)_dwc;
2971 spin_lock(&dwc->lock);
2972 gadget_wakeup_interrupt(dwc);
2973 spin_unlock(&dwc->lock);
2978 static void dwc3_gadget_linksts_change_interrupt(struct dwc3 *dwc,
2979 unsigned int evtinfo)
2981 enum dwc3_link_state next = evtinfo & DWC3_LINK_STATE_MASK;
2982 unsigned int pwropt;
2985 * WORKAROUND: DWC3 < 2.50a have an issue when configured without
2986 * Hibernation mode enabled which would show up when device detects
2987 * host-initiated U3 exit.
2989 * In that case, device will generate a Link State Change Interrupt
2990 * from U3 to RESUME which is only necessary if Hibernation is
2993 * There are no functional changes due to such spurious event and we
2994 * just need to ignore it.
2998 * STAR#9000570034 RTL: SS Resume event generated in non-Hibernation
3001 pwropt = DWC3_GHWPARAMS1_EN_PWROPT(dwc->hwparams.hwparams1);
3002 if ((dwc->revision < DWC3_REVISION_250A) &&
3003 (pwropt != DWC3_GHWPARAMS1_EN_PWROPT_HIB)) {
3004 if ((dwc->link_state == DWC3_LINK_STATE_U3) &&
3005 (next == DWC3_LINK_STATE_RESUME)) {
3011 * WORKAROUND: DWC3 Revisions <1.83a have an issue which, depending
3012 * on the link partner, the USB session might do multiple entry/exit
3013 * of low power states before a transfer takes place.
3015 * Due to this problem, we might experience lower throughput. The
3016 * suggested workaround is to disable DCTL[12:9] bits if we're
3017 * transitioning from U1/U2 to U0 and enable those bits again
3018 * after a transfer completes and there are no pending transfers
3019 * on any of the enabled endpoints.
3021 * This is the first half of that workaround.
3025 * STAR#9000446952: RTL: Device SS : if U1/U2 ->U0 takes >128us
3026 * core send LGO_Ux entering U0
3028 if (dwc->revision < DWC3_REVISION_183A) {
3029 if (next == DWC3_LINK_STATE_U0) {
3033 switch (dwc->link_state) {
3034 case DWC3_LINK_STATE_U1:
3035 case DWC3_LINK_STATE_U2:
3036 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
3037 u1u2 = reg & (DWC3_DCTL_INITU2ENA
3038 | DWC3_DCTL_ACCEPTU2ENA
3039 | DWC3_DCTL_INITU1ENA
3040 | DWC3_DCTL_ACCEPTU1ENA);
3043 dwc->u1u2 = reg & u1u2;
3047 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
3057 case DWC3_LINK_STATE_U1:
3058 if (dwc->speed == USB_SPEED_SUPER)
3059 dwc3_suspend_gadget(dwc);
3061 case DWC3_LINK_STATE_U2:
3062 case DWC3_LINK_STATE_U3:
3063 dwc3_suspend_gadget(dwc);
3065 case DWC3_LINK_STATE_RESUME:
3066 dwc3_resume_gadget(dwc);
3073 dwc->link_state = next;
3076 static void dwc3_gadget_suspend_interrupt(struct dwc3 *dwc,
3077 unsigned int evtinfo)
3079 enum dwc3_link_state next = evtinfo & DWC3_LINK_STATE_MASK;
3081 if (dwc->link_state != next && next == DWC3_LINK_STATE_U3)
3082 dwc3_suspend_gadget(dwc);
3084 dwc->link_state = next;
3087 static void dwc3_gadget_hibernation_interrupt(struct dwc3 *dwc,
3088 unsigned int evtinfo)
3090 unsigned int is_ss = evtinfo & BIT(4);
3093 * WORKAROUND: DWC3 revison 2.20a with hibernation support
3094 * have a known issue which can cause USB CV TD.9.23 to fail
3097 * Because of this issue, core could generate bogus hibernation
3098 * events which SW needs to ignore.
3102 * STAR#9000546576: Device Mode Hibernation: Issue in USB 2.0
3103 * Device Fallback from SuperSpeed
3105 if ((!!is_ss ^ (dwc->speed >= DWC3_DSTS_SUPERSPEED)) &&
3106 (!(dwc->has_hibernation)))
3109 /* enter hibernation here */
3110 gadget_hibernation_interrupt(dwc);
3113 static void dwc3_gadget_interrupt(struct dwc3 *dwc,
3114 const struct dwc3_event_devt *event)
3116 switch (event->type) {
3117 case DWC3_DEVICE_EVENT_DISCONNECT:
3118 dwc3_gadget_disconnect_interrupt(dwc);
3120 case DWC3_DEVICE_EVENT_RESET:
3121 dwc3_gadget_reset_interrupt(dwc);
3123 case DWC3_DEVICE_EVENT_CONNECT_DONE:
3124 dwc3_gadget_conndone_interrupt(dwc);
3126 case DWC3_DEVICE_EVENT_WAKEUP:
3127 dwc3_gadget_wakeup_interrupt(dwc);
3129 case DWC3_DEVICE_EVENT_HIBER_REQ:
3130 if (dev_WARN_ONCE(dwc->dev, !dwc->has_hibernation,
3131 "unexpected hibernation event\n"))
3134 dwc3_gadget_hibernation_interrupt(dwc, event->event_info);
3136 case DWC3_DEVICE_EVENT_LINK_STATUS_CHANGE:
3137 dwc3_gadget_linksts_change_interrupt(dwc, event->event_info);
3139 case DWC3_DEVICE_EVENT_EOPF:
3140 /* It changed to be suspend event for version 2.30a and above */
3141 if (dwc->revision >= DWC3_REVISION_230A) {
3143 * Ignore suspend event until the gadget enters into
3144 * USB_STATE_CONFIGURED state.
3146 if (dwc->gadget.state >= USB_STATE_CONFIGURED)
3147 dwc3_gadget_suspend_interrupt(dwc,
3151 case DWC3_DEVICE_EVENT_SOF:
3152 case DWC3_DEVICE_EVENT_ERRATIC_ERROR:
3153 case DWC3_DEVICE_EVENT_CMD_CMPL:
3154 case DWC3_DEVICE_EVENT_OVERFLOW:
3157 dev_WARN(dwc->dev, "UNKNOWN IRQ %d\n", event->type);
3161 static void dwc3_process_event_entry(struct dwc3 *dwc,
3162 const union dwc3_event *event)
3164 trace_dwc3_event(event->raw, dwc);
3166 if (!event->type.is_devspec)
3167 dwc3_endpoint_interrupt(dwc, &event->depevt);
3168 else if (event->type.type == DWC3_EVENT_TYPE_DEV)
3169 dwc3_gadget_interrupt(dwc, &event->devt);
3171 dev_err(dwc->dev, "UNKNOWN IRQ type %d\n", event->raw);
3174 static irqreturn_t dwc3_process_event_buf(struct dwc3_event_buffer *evt)
3176 struct dwc3 *dwc = evt->dwc;
3177 irqreturn_t ret = IRQ_NONE;
3183 if (!(evt->flags & DWC3_EVENT_PENDING))
3187 union dwc3_event event;
3189 event.raw = *(u32 *) (evt->cache + evt->lpos);
3191 dwc3_process_event_entry(dwc, &event);
3194 * FIXME we wrap around correctly to the next entry as
3195 * almost all entries are 4 bytes in size. There is one
3196 * entry which has 12 bytes which is a regular entry
3197 * followed by 8 bytes data. ATM I don't know how
3198 * things are organized if we get next to the a
3199 * boundary so I worry about that once we try to handle
3202 evt->lpos = (evt->lpos + 4) % evt->length;
3205 if (dwc->is_hibernated)
3210 evt->flags &= ~DWC3_EVENT_PENDING;
3213 if (dwc->is_hibernated)
3216 /* Unmask interrupt */
3217 reg = dwc3_readl(dwc->regs, DWC3_GEVNTSIZ(0));
3218 reg &= ~DWC3_GEVNTSIZ_INTMASK;
3219 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0), reg);
3221 if (dwc->imod_interval) {
3222 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), DWC3_GEVNTCOUNT_EHB);
3223 dwc3_writel(dwc->regs, DWC3_DEV_IMOD(0), dwc->imod_interval);
3229 static irqreturn_t dwc3_thread_interrupt(int irq, void *_evt)
3231 struct dwc3_event_buffer *evt = _evt;
3232 struct dwc3 *dwc = evt->dwc;
3233 unsigned long flags;
3234 irqreturn_t ret = IRQ_NONE;
3236 spin_lock_irqsave(&dwc->lock, flags);
3237 ret = dwc3_process_event_buf(evt);
3238 spin_unlock_irqrestore(&dwc->lock, flags);
3243 static irqreturn_t dwc3_check_event_buf(struct dwc3_event_buffer *evt)
3245 struct dwc3 *dwc = evt->dwc;
3250 if (pm_runtime_suspended(dwc->dev)) {
3251 pm_runtime_get(dwc->dev);
3252 disable_irq_nosync(dwc->irq_gadget);
3253 dwc->pending_events = true;
3257 if (dwc->is_hibernated)
3261 * With PCIe legacy interrupt, test shows that top-half irq handler can
3262 * be called again after HW interrupt deassertion. Check if bottom-half
3263 * irq event handler completes before caching new event to prevent
3266 if (evt->flags & DWC3_EVENT_PENDING)
3269 count = dwc3_readl(dwc->regs, DWC3_GEVNTCOUNT(0));
3270 count &= DWC3_GEVNTCOUNT_MASK;
3275 evt->flags |= DWC3_EVENT_PENDING;
3277 /* Mask interrupt */
3278 reg = dwc3_readl(dwc->regs, DWC3_GEVNTSIZ(0));
3279 reg |= DWC3_GEVNTSIZ_INTMASK;
3280 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0), reg);
3282 amount = min(count, evt->length - evt->lpos);
3283 memcpy(evt->cache + evt->lpos, evt->buf + evt->lpos, amount);
3286 memcpy(evt->cache, evt->buf, count - amount);
3288 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), count);
3290 return IRQ_WAKE_THREAD;
3293 static irqreturn_t dwc3_interrupt(int irq, void *_evt)
3295 struct dwc3_event_buffer *evt = _evt;
3297 return dwc3_check_event_buf(evt);
3300 static int dwc3_gadget_get_irq(struct dwc3 *dwc)
3302 struct platform_device *dwc3_pdev = to_platform_device(dwc->dev);
3305 irq = platform_get_irq_byname(dwc3_pdev, "peripheral");
3309 if (irq == -EPROBE_DEFER)
3312 irq = platform_get_irq_byname(dwc3_pdev, "dwc_usb3");
3316 if (irq == -EPROBE_DEFER)
3319 irq = platform_get_irq(dwc3_pdev, 0);
3321 dwc->irq_gadget = irq;
3323 if (irq == -EPROBE_DEFER)
3326 /* look for wakeup interrupt if hibernation is supported */
3327 if (dwc->has_hibernation) {
3328 irq = platform_get_irq(dwc3_pdev, 2);
3330 dwc->irq_wakeup = irq;
3332 if (irq == -EPROBE_DEFER)
3337 if (irq != -EPROBE_DEFER)
3338 dev_err(dwc->dev, "missing peripheral IRQ\n");
3348 * dwc3_gadget_init - initializes gadget related registers
3349 * @dwc: pointer to our controller context structure
3351 * Returns 0 on success otherwise negative errno.
3353 int dwc3_gadget_init(struct dwc3 *dwc)
3358 irq = dwc3_gadget_get_irq(dwc);
3364 if (dwc->dr_mode == USB_DR_MODE_OTG) {
3365 struct usb_phy *phy;
3367 /* Switch otg to peripheral mode */
3368 phy = usb_get_phy(USB_PHY_TYPE_USB3);
3370 if (phy && phy->otg)
3371 otg_set_peripheral(phy->otg,
3372 (struct usb_gadget *)(long)1);
3377 dwc->ep0_trb = dma_alloc_coherent(dwc->sysdev,
3378 sizeof(*dwc->ep0_trb) * 2,
3379 &dwc->ep0_trb_addr, GFP_KERNEL);
3380 if (!dwc->ep0_trb) {
3381 dev_err(dwc->dev, "failed to allocate ep0 trb\n");
3386 dwc->setup_buf = kzalloc(DWC3_EP0_SETUP_SIZE, GFP_KERNEL);
3387 if (!dwc->setup_buf) {
3392 dwc->bounce = dma_alloc_coherent(dwc->sysdev, DWC3_BOUNCE_SIZE,
3393 &dwc->bounce_addr, GFP_KERNEL);
3399 init_completion(&dwc->ep0_in_setup);
3401 dwc->gadget.ops = &dwc3_gadget_ops;
3402 dwc->gadget.speed = USB_SPEED_UNKNOWN;
3403 dwc->gadget.sg_supported = true;
3404 dwc->gadget.name = "dwc3-gadget";
3405 dwc->gadget.is_otg = dwc->dr_mode == USB_DR_MODE_OTG;
3408 * FIXME We might be setting max_speed to <SUPER, however versions
3409 * <2.20a of dwc3 have an issue with metastability (documented
3410 * elsewhere in this driver) which tells us we can't set max speed to
3411 * anything lower than SUPER.
3413 * Because gadget.max_speed is only used by composite.c and function
3414 * drivers (i.e. it won't go into dwc3's registers) we are allowing this
3415 * to happen so we avoid sending SuperSpeed Capability descriptor
3416 * together with our BOS descriptor as that could confuse host into
3417 * thinking we can handle super speed.
3419 * Note that, in fact, we won't even support GetBOS requests when speed
3420 * is less than super speed because we don't have means, yet, to tell
3421 * composite.c that we are USB 2.0 + LPM ECN.
3423 if (dwc->revision < DWC3_REVISION_220A &&
3424 !dwc->dis_metastability_quirk)
3425 dev_info(dwc->dev, "changing max_speed on rev %08x\n",
3428 dwc->gadget.max_speed = dwc->maximum_speed;
3431 * REVISIT: Here we should clear all pending IRQs to be
3432 * sure we're starting from a well known location.
3435 ret = dwc3_gadget_init_endpoints(dwc, dwc->num_eps);
3439 ret = usb_add_gadget_udc(dwc->dev, &dwc->gadget);
3441 dev_err(dwc->dev, "failed to register udc\n");
3445 if (dwc->dr_mode == USB_DR_MODE_OTG) {
3446 struct usb_phy *phy;
3448 phy = usb_get_phy(USB_PHY_TYPE_USB3);
3450 if (phy && phy->otg) {
3451 ret = otg_set_peripheral(phy->otg,
3455 "%s otg_set_peripheral failed\n",
3471 dwc3_gadget_free_endpoints(dwc);
3474 dma_free_coherent(dwc->sysdev, DWC3_BOUNCE_SIZE, dwc->bounce,
3478 kfree(dwc->setup_buf);
3481 dma_free_coherent(dwc->sysdev, sizeof(*dwc->ep0_trb) * 2,
3482 dwc->ep0_trb, dwc->ep0_trb_addr);
3488 /* -------------------------------------------------------------------------- */
3490 void dwc3_gadget_exit(struct dwc3 *dwc)
3492 usb_del_gadget_udc(&dwc->gadget);
3493 dwc3_gadget_free_endpoints(dwc);
3494 dma_free_coherent(dwc->sysdev, DWC3_BOUNCE_SIZE, dwc->bounce,
3496 kfree(dwc->setup_buf);
3497 dma_free_coherent(dwc->sysdev, sizeof(*dwc->ep0_trb) * 2,
3498 dwc->ep0_trb, dwc->ep0_trb_addr);
3501 int dwc3_gadget_suspend(struct dwc3 *dwc)
3503 if (!dwc->gadget_driver)
3506 if (dwc->is_hibernated) {
3508 * As we are about to suspend, wake the controller from
3509 * D3 & hibernation states
3511 dwc->force_hiber_wake = true;
3512 gadget_wakeup_interrupt(dwc);
3513 dwc->force_hiber_wake = false;
3516 dwc3_gadget_run_stop(dwc, false, false);
3517 dwc3_disconnect_gadget(dwc);
3518 __dwc3_gadget_stop(dwc);
3523 int dwc3_gadget_resume(struct dwc3 *dwc)
3528 if (!dwc->gadget_driver)
3531 ret = __dwc3_gadget_start(dwc);
3535 ret = dwc3_gadget_run_stop(dwc, true, false);
3539 /* In USB 2.0, to avoid hibernation interrupt at the time of connection
3540 * set DWC3_DCTL_KEEP_CONNECT bit.
3542 if (dwc->has_hibernation) {
3543 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
3544 reg |= DWC3_DCTL_KEEP_CONNECT;
3545 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
3551 __dwc3_gadget_stop(dwc);
3557 void dwc3_gadget_process_pending_events(struct dwc3 *dwc)
3559 if (dwc->pending_events) {
3560 dwc3_interrupt(dwc->irq_gadget, dwc->ev_buf);
3561 dwc->pending_events = false;
3562 enable_irq(dwc->irq_gadget);