Lines Matching +full:usb3 +full:- +full:lpm +full:- +full:capable
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 - https://www.ti.com
20 #include <linux/dma-mapping.h>
30 #define DWC3_ALIGN_FRAME(d, n) (((d)->frame_number + ((d)->interval * (n))) \
31 & ~((d)->interval - 1))
34 * dwc3_gadget_set_test_mode - enables usb2 test modes
39 * success or -EINVAL if wrong Test Selector is passed.
45 reg = dwc3_readl(dwc->regs, DWC3_DCTL); in dwc3_gadget_set_test_mode()
57 return -EINVAL; in dwc3_gadget_set_test_mode()
66 * dwc3_gadget_get_link_state - gets current state of usb link
70 * return the link state on success (>= 0) or -ETIMEDOUT.
76 reg = dwc3_readl(dwc->regs, DWC3_DSTS); in dwc3_gadget_get_link_state()
82 * dwc3_gadget_set_link_state - sets usb link to a particular state
87 * return 0 on success or -ETIMEDOUT.
99 while (--retries) { in dwc3_gadget_set_link_state()
100 reg = dwc3_readl(dwc->regs, DWC3_DSTS); in dwc3_gadget_set_link_state()
108 return -ETIMEDOUT; in dwc3_gadget_set_link_state()
111 reg = dwc3_readl(dwc->regs, DWC3_DCTL); in dwc3_gadget_set_link_state()
115 dwc3_writel(dwc->regs, DWC3_DCTL, reg); in dwc3_gadget_set_link_state()
119 dwc3_writel(dwc->regs, DWC3_DCTL, reg); in dwc3_gadget_set_link_state()
130 while (--retries) { in dwc3_gadget_set_link_state()
131 reg = dwc3_readl(dwc->regs, DWC3_DSTS); in dwc3_gadget_set_link_state()
139 return -ETIMEDOUT; in dwc3_gadget_set_link_state()
146 if (dwc->ep0state != EP0_SETUP_PHASE) { in dwc3_ep0_reset_state()
147 dir = !!dwc->ep0_expect_in; in dwc3_ep0_reset_state()
148 if (dwc->ep0state == EP0_DATA_PHASE) in dwc3_ep0_reset_state()
149 dwc3_ep0_end_control_data(dwc, dwc->eps[dir]); in dwc3_ep0_reset_state()
151 dwc3_ep0_end_control_data(dwc, dwc->eps[!dir]); in dwc3_ep0_reset_state()
153 dwc->eps[0]->trb_enqueue = 0; in dwc3_ep0_reset_state()
154 dwc->eps[1]->trb_enqueue = 0; in dwc3_ep0_reset_state()
161 * dwc3_ep_inc_trb - increment a trb index.
171 if (*index == (DWC3_TRB_NUM - 1)) in dwc3_ep_inc_trb()
176 * dwc3_ep_inc_enq - increment endpoint's enqueue pointer
181 dwc3_ep_inc_trb(&dep->trb_enqueue); in dwc3_ep_inc_enq()
185 * dwc3_ep_inc_deq - increment endpoint's dequeue pointer
190 dwc3_ep_inc_trb(&dep->trb_dequeue); in dwc3_ep_inc_deq()
196 struct dwc3 *dwc = dep->dwc; in dwc3_gadget_del_and_unmap_request()
198 list_del(&req->list); in dwc3_gadget_del_and_unmap_request()
199 req->remaining = 0; in dwc3_gadget_del_and_unmap_request()
200 req->needs_extra_trb = false; in dwc3_gadget_del_and_unmap_request()
201 req->num_trbs = 0; in dwc3_gadget_del_and_unmap_request()
203 if (req->request.status == -EINPROGRESS) in dwc3_gadget_del_and_unmap_request()
204 req->request.status = status; in dwc3_gadget_del_and_unmap_request()
206 if (req->trb) in dwc3_gadget_del_and_unmap_request()
207 usb_gadget_unmap_request_by_dev(dwc->sysdev, in dwc3_gadget_del_and_unmap_request()
208 &req->request, req->direction); in dwc3_gadget_del_and_unmap_request()
210 req->trb = NULL; in dwc3_gadget_del_and_unmap_request()
213 if (dep->number > 1) in dwc3_gadget_del_and_unmap_request()
214 pm_runtime_put(dwc->dev); in dwc3_gadget_del_and_unmap_request()
218 * dwc3_gadget_giveback - call struct usb_request's ->complete callback
224 * function will unmap @req and call its ->complete() callback to notify upper
230 struct dwc3 *dwc = dep->dwc; in dwc3_gadget_giveback()
233 req->status = DWC3_REQUEST_STATUS_COMPLETED; in dwc3_gadget_giveback()
235 spin_unlock(&dwc->lock); in dwc3_gadget_giveback()
236 usb_gadget_giveback_request(&dep->endpoint, &req->request); in dwc3_gadget_giveback()
237 spin_lock(&dwc->lock); in dwc3_gadget_giveback()
241 * dwc3_send_gadget_generic_command - issue a generic command for the controller
257 dwc3_writel(dwc->regs, DWC3_DGCMDPAR, param); in dwc3_send_gadget_generic_command()
258 dwc3_writel(dwc->regs, DWC3_DGCMD, cmd | DWC3_DGCMD_CMDACT); in dwc3_send_gadget_generic_command()
261 reg = dwc3_readl(dwc->regs, DWC3_DGCMD); in dwc3_send_gadget_generic_command()
265 ret = -EINVAL; in dwc3_send_gadget_generic_command()
268 } while (--timeout); in dwc3_send_gadget_generic_command()
271 ret = -ETIMEDOUT; in dwc3_send_gadget_generic_command()
272 status = -ETIMEDOUT; in dwc3_send_gadget_generic_command()
283 * dwc3_send_gadget_ep_cmd - issue an endpoint command
311 const struct usb_endpoint_descriptor *desc = dep->endpoint.desc; in dwc3_send_gadget_ep_cmd()
312 struct dwc3 *dwc = dep->dwc; in dwc3_send_gadget_ep_cmd()
318 int ret = -EINVAL; in dwc3_send_gadget_ep_cmd()
330 if (dwc->gadget->speed <= USB_SPEED_HIGH || in dwc3_send_gadget_ep_cmd()
332 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0)); in dwc3_send_gadget_ep_cmd()
344 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg); in dwc3_send_gadget_ep_cmd()
354 dwc3_writel(dep->regs, DWC3_DEPCMDPAR0, params->param0); in dwc3_send_gadget_ep_cmd()
355 dwc3_writel(dep->regs, DWC3_DEPCMDPAR1, params->param1); in dwc3_send_gadget_ep_cmd()
356 dwc3_writel(dep->regs, DWC3_DEPCMDPAR2, params->param2); in dwc3_send_gadget_ep_cmd()
380 dwc3_writel(dep->regs, DWC3_DEPCMD, cmd); in dwc3_send_gadget_ep_cmd()
390 reg = dwc3_readl(dep->regs, DWC3_DEPCMD); in dwc3_send_gadget_ep_cmd()
399 dev_WARN(dwc->dev, "No resource for %s\n", in dwc3_send_gadget_ep_cmd()
400 dep->name); in dwc3_send_gadget_ep_cmd()
401 ret = -EINVAL; in dwc3_send_gadget_ep_cmd()
411 * Instead of always returning -EINVAL, let's in dwc3_send_gadget_ep_cmd()
413 * the case by returning -EAGAIN. in dwc3_send_gadget_ep_cmd()
415 ret = -EAGAIN; in dwc3_send_gadget_ep_cmd()
418 dev_WARN(dwc->dev, "UNKNOWN cmd status\n"); in dwc3_send_gadget_ep_cmd()
423 } while (--timeout); in dwc3_send_gadget_ep_cmd()
426 ret = -ETIMEDOUT; in dwc3_send_gadget_ep_cmd()
427 cmd_status = -ETIMEDOUT; in dwc3_send_gadget_ep_cmd()
435 dep->flags |= DWC3_EP_TRANSFER_STARTED; in dwc3_send_gadget_ep_cmd()
437 if (ret != -ETIMEDOUT) in dwc3_send_gadget_ep_cmd()
446 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0)); in dwc3_send_gadget_ep_cmd()
448 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg); in dwc3_send_gadget_ep_cmd()
456 struct dwc3 *dwc = dep->dwc; in dwc3_send_clear_stall_ep_cmd()
464 * some (non-compliant) hosts may not send ACK TPs for pending in dwc3_send_clear_stall_ep_cmd()
468 if (dep->direction && in dwc3_send_clear_stall_ep_cmd()
470 (dwc->gadget->speed >= USB_SPEED_SUPER)) in dwc3_send_clear_stall_ep_cmd()
481 u32 offset = (char *) trb - (char *) dep->trb_pool; in dwc3_trb_dma_offset()
483 return dep->trb_pool_dma + offset; in dwc3_trb_dma_offset()
488 struct dwc3 *dwc = dep->dwc; in dwc3_alloc_trb_pool()
490 if (dep->trb_pool) in dwc3_alloc_trb_pool()
493 dep->trb_pool = dma_alloc_coherent(dwc->sysdev, in dwc3_alloc_trb_pool()
495 &dep->trb_pool_dma, GFP_KERNEL); in dwc3_alloc_trb_pool()
496 if (!dep->trb_pool) { in dwc3_alloc_trb_pool()
497 dev_err(dep->dwc->dev, "failed to allocate trb pool for %s\n", in dwc3_alloc_trb_pool()
498 dep->name); in dwc3_alloc_trb_pool()
499 return -ENOMEM; in dwc3_alloc_trb_pool()
507 struct dwc3 *dwc = dep->dwc; in dwc3_free_trb_pool()
509 dma_free_coherent(dwc->sysdev, sizeof(struct dwc3_trb) * DWC3_TRB_NUM, in dwc3_free_trb_pool()
510 dep->trb_pool, dep->trb_pool_dma); in dwc3_free_trb_pool()
512 dep->trb_pool = NULL; in dwc3_free_trb_pool()
513 dep->trb_pool_dma = 0; in dwc3_free_trb_pool()
521 if (dep->flags & DWC3_EP_RESOURCE_ALLOCATED) in dwc3_gadget_set_xfer_resource()
533 dep->flags |= DWC3_EP_RESOURCE_ALLOCATED; in dwc3_gadget_set_xfer_resource()
538 * dwc3_gadget_start_config - reset endpoint resources
543 * part of the power-on/soft-reset initialization.
545 * Set resource_index=2 to reset only non-control endpoints' resources. Do this
556 return -EINVAL; in dwc3_gadget_start_config()
562 ret = dwc3_send_gadget_ep_cmd(dwc->eps[0], cmd, ¶ms); in dwc3_gadget_start_config()
567 for (i = resource_index; i < dwc->num_eps && dwc->eps[i]; i++) in dwc3_gadget_start_config()
568 dwc->eps[i]->flags &= ~DWC3_EP_RESOURCE_ALLOCATED; in dwc3_gadget_start_config()
578 struct dwc3 *dwc = dep->dwc; in dwc3_gadget_set_ep_config()
580 comp_desc = dep->endpoint.comp_desc; in dwc3_gadget_set_ep_config()
581 desc = dep->endpoint.desc; in dwc3_gadget_set_ep_config()
589 if (dwc->gadget->speed >= USB_SPEED_SUPER) { in dwc3_gadget_set_ep_config()
590 u32 burst = dep->endpoint.maxburst; in dwc3_gadget_set_ep_config()
592 params.param0 |= DWC3_DEPCFG_BURST_SIZE(burst - 1); in dwc3_gadget_set_ep_config()
597 params.param2 |= dep->saved_state; in dwc3_gadget_set_ep_config()
602 if (dep->number <= 1 || usb_endpoint_xfer_isoc(desc)) in dwc3_gadget_set_ep_config()
609 dep->stream_capable = true; in dwc3_gadget_set_ep_config()
621 params.param1 |= DWC3_DEPCFG_EP_NUMBER(dep->number); in dwc3_gadget_set_ep_config()
627 if (dep->direction) in dwc3_gadget_set_ep_config()
628 params.param0 |= DWC3_DEPCFG_FIFO_NUMBER(dep->number >> 1); in dwc3_gadget_set_ep_config()
630 if (desc->bInterval) { in dwc3_gadget_set_ep_config()
641 bInterval_m1 = min_t(u8, desc->bInterval - 1, 13); in dwc3_gadget_set_ep_config()
644 dwc->gadget->speed == USB_SPEED_FULL) in dwc3_gadget_set_ep_config()
645 dep->interval = desc->bInterval; in dwc3_gadget_set_ep_config()
647 dep->interval = 1 << (desc->bInterval - 1); in dwc3_gadget_set_ep_config()
656 * dwc3_gadget_calc_tx_fifo_size - calculates the txfifo size value
691 * dwc3_gadget_clear_tx_fifos - Clears txfifo allocation
704 if (!dwc->do_fifo_resize) in dwc3_gadget_clear_tx_fifos()
708 dep = dwc->eps[1]; in dwc3_gadget_clear_tx_fifos()
709 size = dwc3_readl(dwc->regs, DWC3_GTXFIFOSIZ(0)); in dwc3_gadget_clear_tx_fifos()
715 dwc->last_fifo_depth = fifo_depth; in dwc3_gadget_clear_tx_fifos()
717 for (num = 3; num < min_t(int, dwc->num_eps, DWC3_ENDPOINTS_NUM); in dwc3_gadget_clear_tx_fifos()
719 dep = dwc->eps[num]; in dwc3_gadget_clear_tx_fifos()
722 dwc3_readl(dwc->regs, DWC3_GTXFIFOSIZ(num >> 1)) & in dwc3_gadget_clear_tx_fifos()
725 dwc3_writel(dwc->regs, DWC3_GTXFIFOSIZ(num >> 1), size); in dwc3_gadget_clear_tx_fifos()
726 dep->flags &= ~DWC3_EP_TXFIFO_RESIZED; in dwc3_gadget_clear_tx_fifos()
728 dwc->num_ep_resized = 0; in dwc3_gadget_clear_tx_fifos()
732 * dwc3_gadget_resize_tx_fifos - reallocate fifo spaces for current use-case
740 * on the configured size for RAM1 - which contains TxFifo -,
754 struct dwc3 *dwc = dep->dwc; in dwc3_gadget_resize_tx_fifos()
765 if (!dwc->do_fifo_resize) in dwc3_gadget_resize_tx_fifos()
769 if (!usb_endpoint_dir_in(dep->endpoint.desc) || dep->number <= 1) in dwc3_gadget_resize_tx_fifos()
773 if (dep->flags & DWC3_EP_TXFIFO_RESIZED) in dwc3_gadget_resize_tx_fifos()
776 ram1_depth = DWC3_RAM1_DEPTH(dwc->hwparams.hwparams7); in dwc3_gadget_resize_tx_fifos()
778 if ((dep->endpoint.maxburst > 1 && in dwc3_gadget_resize_tx_fifos()
779 usb_endpoint_xfer_bulk(dep->endpoint.desc)) || in dwc3_gadget_resize_tx_fifos()
780 usb_endpoint_xfer_isoc(dep->endpoint.desc)) in dwc3_gadget_resize_tx_fifos()
783 if (dep->endpoint.maxburst > 6 && in dwc3_gadget_resize_tx_fifos()
784 (usb_endpoint_xfer_bulk(dep->endpoint.desc) || in dwc3_gadget_resize_tx_fifos()
785 usb_endpoint_xfer_isoc(dep->endpoint.desc)) && DWC3_IP_IS(DWC31)) in dwc3_gadget_resize_tx_fifos()
786 num_fifos = dwc->tx_fifo_resize_max_num; in dwc3_gadget_resize_tx_fifos()
792 num_in_ep = dwc->max_cfg_eps; in dwc3_gadget_resize_tx_fifos()
793 num_in_ep -= dwc->num_ep_resized; in dwc3_gadget_resize_tx_fifos()
797 remaining = ram1_depth - min_depth - dwc->last_fifo_depth; in dwc3_gadget_resize_tx_fifos()
804 fifo_size = (num_fifos - 1) * fifo; in dwc3_gadget_resize_tx_fifos()
812 /* Check if TXFIFOs start at non-zero addr */ in dwc3_gadget_resize_tx_fifos()
813 tmp = dwc3_readl(dwc->regs, DWC3_GTXFIFOSIZ(0)); in dwc3_gadget_resize_tx_fifos()
816 fifo_size |= (fifo_0_start + (dwc->last_fifo_depth << 16)); in dwc3_gadget_resize_tx_fifos()
818 dwc->last_fifo_depth += DWC3_GTXFIFOSIZ_TXFDEP(fifo_size); in dwc3_gadget_resize_tx_fifos()
820 dwc->last_fifo_depth += DWC31_GTXFIFOSIZ_TXFDEP(fifo_size); in dwc3_gadget_resize_tx_fifos()
823 if (dwc->last_fifo_depth >= ram1_depth) { in dwc3_gadget_resize_tx_fifos()
824 dev_err(dwc->dev, "Fifosize(%d) > RAM size(%d) %s depth:%d\n", in dwc3_gadget_resize_tx_fifos()
825 dwc->last_fifo_depth, ram1_depth, in dwc3_gadget_resize_tx_fifos()
826 dep->endpoint.name, fifo_size); in dwc3_gadget_resize_tx_fifos()
832 dwc->last_fifo_depth -= fifo_size; in dwc3_gadget_resize_tx_fifos()
833 return -ENOMEM; in dwc3_gadget_resize_tx_fifos()
836 dwc3_writel(dwc->regs, DWC3_GTXFIFOSIZ(dep->number >> 1), fifo_size); in dwc3_gadget_resize_tx_fifos()
837 dep->flags |= DWC3_EP_TXFIFO_RESIZED; in dwc3_gadget_resize_tx_fifos()
838 dwc->num_ep_resized++; in dwc3_gadget_resize_tx_fifos()
844 * __dwc3_gadget_ep_enable - initializes a hw endpoint
853 const struct usb_endpoint_descriptor *desc = dep->endpoint.desc; in __dwc3_gadget_ep_enable()
854 struct dwc3 *dwc = dep->dwc; in __dwc3_gadget_ep_enable()
859 if (!(dep->flags & DWC3_EP_ENABLED)) { in __dwc3_gadget_ep_enable()
869 if (!(dep->flags & DWC3_EP_RESOURCE_ALLOCATED)) { in __dwc3_gadget_ep_enable()
875 if (!(dep->flags & DWC3_EP_ENABLED)) { in __dwc3_gadget_ep_enable()
879 dep->type = usb_endpoint_type(desc); in __dwc3_gadget_ep_enable()
880 dep->flags |= DWC3_EP_ENABLED; in __dwc3_gadget_ep_enable()
882 reg = dwc3_readl(dwc->regs, DWC3_DALEPENA); in __dwc3_gadget_ep_enable()
883 reg |= DWC3_DALEPENA_EP(dep->number); in __dwc3_gadget_ep_enable()
884 dwc3_writel(dwc->regs, DWC3_DALEPENA, reg); in __dwc3_gadget_ep_enable()
886 dep->trb_dequeue = 0; in __dwc3_gadget_ep_enable()
887 dep->trb_enqueue = 0; in __dwc3_gadget_ep_enable()
893 memset(dep->trb_pool, 0, in __dwc3_gadget_ep_enable()
897 trb_st_hw = &dep->trb_pool[0]; in __dwc3_gadget_ep_enable()
899 trb_link = &dep->trb_pool[DWC3_TRB_NUM - 1]; in __dwc3_gadget_ep_enable()
900 trb_link->bpl = lower_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw)); in __dwc3_gadget_ep_enable()
901 trb_link->bph = upper_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw)); in __dwc3_gadget_ep_enable()
902 trb_link->ctrl |= DWC3_TRBCTL_LINK_TRB; in __dwc3_gadget_ep_enable()
903 trb_link->ctrl |= DWC3_TRB_CTRL_HWO; in __dwc3_gadget_ep_enable()
907 * Issue StartTransfer here with no-op TRB so we can always rely on No in __dwc3_gadget_ep_enable()
918 trb = &dep->trb_pool[0]; in __dwc3_gadget_ep_enable()
930 if (dep->stream_capable) { in __dwc3_gadget_ep_enable()
937 * no-op TRB as normal, but end it immediately. As a in __dwc3_gadget_ep_enable()
950 * However, if the controller is capable of in __dwc3_gadget_ep_enable()
955 if (!dep->direction || in __dwc3_gadget_ep_enable()
956 !(dwc->hwparams.hwparams9 & in __dwc3_gadget_ep_enable()
958 dep->flags |= DWC3_EP_FORCE_RESTART_STREAM; in __dwc3_gadget_ep_enable()
975 if (dep->flags & DWC3_EP_DELAY_STOP) in dwc3_remove_requests()
978 /* - giveback all requests to gadget driver */ in dwc3_remove_requests()
979 while (!list_empty(&dep->started_list)) { in dwc3_remove_requests()
980 req = next_request(&dep->started_list); in dwc3_remove_requests()
985 while (!list_empty(&dep->pending_list)) { in dwc3_remove_requests()
986 req = next_request(&dep->pending_list); in dwc3_remove_requests()
991 while (!list_empty(&dep->cancelled_list)) { in dwc3_remove_requests()
992 req = next_request(&dep->cancelled_list); in dwc3_remove_requests()
999 * __dwc3_gadget_ep_disable - disables a hw endpoint
1010 struct dwc3 *dwc = dep->dwc; in __dwc3_gadget_ep_disable()
1017 if (dep->flags & DWC3_EP_STALL) in __dwc3_gadget_ep_disable()
1020 reg = dwc3_readl(dwc->regs, DWC3_DALEPENA); in __dwc3_gadget_ep_disable()
1021 reg &= ~DWC3_DALEPENA_EP(dep->number); in __dwc3_gadget_ep_disable()
1022 dwc3_writel(dwc->regs, DWC3_DALEPENA, reg); in __dwc3_gadget_ep_disable()
1024 dwc3_remove_requests(dwc, dep, -ESHUTDOWN); in __dwc3_gadget_ep_disable()
1026 dep->stream_capable = false; in __dwc3_gadget_ep_disable()
1027 dep->type = 0; in __dwc3_gadget_ep_disable()
1034 if (dep->flags & DWC3_EP_DELAY_STOP) in __dwc3_gadget_ep_disable()
1036 dep->flags &= mask; in __dwc3_gadget_ep_disable()
1038 /* Clear out the ep descriptors for non-ep0 */ in __dwc3_gadget_ep_disable()
1039 if (dep->number > 1) { in __dwc3_gadget_ep_disable()
1040 dep->endpoint.comp_desc = NULL; in __dwc3_gadget_ep_disable()
1041 dep->endpoint.desc = NULL; in __dwc3_gadget_ep_disable()
1047 /* -------------------------------------------------------------------------- */
1052 return -EINVAL; in dwc3_gadget_ep0_enable()
1057 return -EINVAL; in dwc3_gadget_ep0_disable()
1060 /* -------------------------------------------------------------------------- */
1070 if (!ep || !desc || desc->bDescriptorType != USB_DT_ENDPOINT) { in dwc3_gadget_ep_enable()
1072 return -EINVAL; in dwc3_gadget_ep_enable()
1075 if (!desc->wMaxPacketSize) { in dwc3_gadget_ep_enable()
1077 return -EINVAL; in dwc3_gadget_ep_enable()
1081 dwc = dep->dwc; in dwc3_gadget_ep_enable()
1083 if (dev_WARN_ONCE(dwc->dev, dep->flags & DWC3_EP_ENABLED, in dwc3_gadget_ep_enable()
1085 dep->name)) in dwc3_gadget_ep_enable()
1088 spin_lock_irqsave(&dwc->lock, flags); in dwc3_gadget_ep_enable()
1090 spin_unlock_irqrestore(&dwc->lock, flags); in dwc3_gadget_ep_enable()
1104 return -EINVAL; in dwc3_gadget_ep_disable()
1108 dwc = dep->dwc; in dwc3_gadget_ep_disable()
1110 if (dev_WARN_ONCE(dwc->dev, !(dep->flags & DWC3_EP_ENABLED), in dwc3_gadget_ep_disable()
1112 dep->name)) in dwc3_gadget_ep_disable()
1115 spin_lock_irqsave(&dwc->lock, flags); in dwc3_gadget_ep_disable()
1117 spin_unlock_irqrestore(&dwc->lock, flags); in dwc3_gadget_ep_disable()
1132 req->direction = dep->direction; in dwc3_gadget_ep_alloc_request()
1133 req->epnum = dep->number; in dwc3_gadget_ep_alloc_request()
1134 req->dep = dep; in dwc3_gadget_ep_alloc_request()
1135 req->status = DWC3_REQUEST_STATUS_UNKNOWN; in dwc3_gadget_ep_alloc_request()
1139 return &req->request; in dwc3_gadget_ep_alloc_request()
1152 * dwc3_ep_prev_trb - returns the previous TRB in the ring
1165 tmp = DWC3_TRB_NUM - 1; in dwc3_ep_prev_trb()
1167 return &dep->trb_pool[tmp - 1]; in dwc3_ep_prev_trb()
1176 * or empty. It's considered full when there are DWC3_TRB_NUM-1 of TRBs in dwc3_calc_trbs_left()
1179 if (dep->trb_enqueue == dep->trb_dequeue) { in dwc3_calc_trbs_left()
1186 req = next_request(&dep->started_list); in dwc3_calc_trbs_left()
1187 if (req && req->num_trbs) in dwc3_calc_trbs_left()
1190 return DWC3_TRB_NUM - 1; in dwc3_calc_trbs_left()
1193 trbs_left = dep->trb_dequeue - dep->trb_enqueue; in dwc3_calc_trbs_left()
1194 trbs_left &= (DWC3_TRB_NUM - 1); in dwc3_calc_trbs_left()
1196 if (dep->trb_dequeue < dep->trb_enqueue) in dwc3_calc_trbs_left()
1197 trbs_left--; in dwc3_calc_trbs_left()
1203 * dwc3_prepare_one_trb - setup one TRB from one request
1219 unsigned int stream_id = req->request.stream_id; in dwc3_prepare_one_trb()
1220 unsigned int short_not_ok = req->request.short_not_ok; in dwc3_prepare_one_trb()
1221 unsigned int no_interrupt = req->request.no_interrupt; in dwc3_prepare_one_trb()
1222 unsigned int is_last = req->request.is_last; in dwc3_prepare_one_trb()
1223 struct dwc3 *dwc = dep->dwc; in dwc3_prepare_one_trb()
1224 struct usb_gadget *gadget = dwc->gadget; in dwc3_prepare_one_trb()
1225 enum usb_device_speed speed = gadget->speed; in dwc3_prepare_one_trb()
1228 dma = dep->dwc->bounce_addr; in dwc3_prepare_one_trb()
1229 else if (req->request.num_sgs > 0) in dwc3_prepare_one_trb()
1230 dma = sg_dma_address(req->start_sg); in dwc3_prepare_one_trb()
1232 dma = req->request.dma; in dwc3_prepare_one_trb()
1234 trb = &dep->trb_pool[dep->trb_enqueue]; in dwc3_prepare_one_trb()
1236 if (!req->trb) { in dwc3_prepare_one_trb()
1238 req->trb = trb; in dwc3_prepare_one_trb()
1239 req->trb_dma = dwc3_trb_dma_offset(dep, trb); in dwc3_prepare_one_trb()
1242 req->num_trbs++; in dwc3_prepare_one_trb()
1244 trb->size = DWC3_TRB_SIZE_LENGTH(trb_length); in dwc3_prepare_one_trb()
1245 trb->bpl = lower_32_bits(dma); in dwc3_prepare_one_trb()
1246 trb->bph = upper_32_bits(dma); in dwc3_prepare_one_trb()
1248 switch (usb_endpoint_type(dep->endpoint.desc)) { in dwc3_prepare_one_trb()
1250 trb->ctrl = DWC3_TRBCTL_CONTROL_SETUP; in dwc3_prepare_one_trb()
1255 trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS_FIRST; in dwc3_prepare_one_trb()
1271 * - DATA0 in dwc3_prepare_one_trb()
1274 * - DATA1, DATA0 in dwc3_prepare_one_trb()
1277 * - DATA2, DATA1, DATA0 in dwc3_prepare_one_trb()
1280 struct usb_ep *ep = &dep->endpoint; in dwc3_prepare_one_trb()
1282 unsigned int maxp = usb_endpoint_maxp(ep->desc); in dwc3_prepare_one_trb()
1284 if (req->request.length <= (2 * maxp)) in dwc3_prepare_one_trb()
1285 mult--; in dwc3_prepare_one_trb()
1287 if (req->request.length <= maxp) in dwc3_prepare_one_trb()
1288 mult--; in dwc3_prepare_one_trb()
1290 trb->size |= DWC3_TRB_SIZE_PCM1(mult); in dwc3_prepare_one_trb()
1293 trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS; in dwc3_prepare_one_trb()
1297 trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI; in dwc3_prepare_one_trb()
1302 trb->ctrl = DWC3_TRBCTL_NORMAL; in dwc3_prepare_one_trb()
1309 dev_WARN(dwc->dev, "Unknown endpoint type %d\n", in dwc3_prepare_one_trb()
1310 usb_endpoint_type(dep->endpoint.desc)); in dwc3_prepare_one_trb()
1315 * when endpoint is not a stream capable in dwc3_prepare_one_trb()
1317 if (usb_endpoint_dir_out(dep->endpoint.desc)) { in dwc3_prepare_one_trb()
1318 if (!dep->stream_capable) in dwc3_prepare_one_trb()
1319 trb->ctrl |= DWC3_TRB_CTRL_CSP; in dwc3_prepare_one_trb()
1322 trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI; in dwc3_prepare_one_trb()
1326 if (dep->stream_capable && DWC3_MST_CAPABLE(&dwc->hwparams)) in dwc3_prepare_one_trb()
1327 trb->ctrl |= DWC3_TRB_CTRL_CSP; in dwc3_prepare_one_trb()
1330 trb->ctrl |= DWC3_TRB_CTRL_IOC; in dwc3_prepare_one_trb()
1333 trb->ctrl |= DWC3_TRB_CTRL_CHN; in dwc3_prepare_one_trb()
1334 else if (dep->stream_capable && is_last && in dwc3_prepare_one_trb()
1335 !DWC3_MST_CAPABLE(&dwc->hwparams)) in dwc3_prepare_one_trb()
1336 trb->ctrl |= DWC3_TRB_CTRL_LST; in dwc3_prepare_one_trb()
1338 if (usb_endpoint_xfer_bulk(dep->endpoint.desc) && dep->stream_capable) in dwc3_prepare_one_trb()
1339 trb->ctrl |= DWC3_TRB_CTRL_SID_SOFN(stream_id); in dwc3_prepare_one_trb()
1349 * However there is a possibility of CPU re-ordering here which can cause in dwc3_prepare_one_trb()
1351 * Add a write memory barrier to prevent CPU re-ordering. in dwc3_prepare_one_trb()
1354 trb->ctrl |= DWC3_TRB_CTRL_HWO; in dwc3_prepare_one_trb()
1363 unsigned int maxp = usb_endpoint_maxp(dep->endpoint.desc); in dwc3_needs_extra_trb()
1364 unsigned int rem = req->request.length % maxp; in dwc3_needs_extra_trb()
1366 if ((req->request.length && req->request.zero && !rem && in dwc3_needs_extra_trb()
1367 !usb_endpoint_xfer_isoc(dep->endpoint.desc)) || in dwc3_needs_extra_trb()
1368 (!req->direction && rem)) in dwc3_needs_extra_trb()
1375 * dwc3_prepare_last_sg - prepare TRBs for the last SG entry
1387 unsigned int maxp = usb_endpoint_maxp(dep->endpoint.desc); in dwc3_prepare_last_sg()
1388 unsigned int rem = req->request.length % maxp; in dwc3_prepare_last_sg()
1397 req->needs_extra_trb = num_trbs > 1; in dwc3_prepare_last_sg()
1400 if (req->direction || req->request.length) in dwc3_prepare_last_sg()
1402 req->needs_extra_trb, node, false, false); in dwc3_prepare_last_sg()
1405 if ((!req->direction && !req->request.length) || req->needs_extra_trb) in dwc3_prepare_last_sg()
1407 req->direction ? 0 : maxp - rem, in dwc3_prepare_last_sg()
1416 struct scatterlist *sg = req->start_sg; in dwc3_prepare_trbs_sg()
1419 unsigned int length = req->request.length; in dwc3_prepare_trbs_sg()
1420 unsigned int remaining = req->num_pending_sgs; in dwc3_prepare_trbs_sg()
1421 unsigned int num_queued_sgs = req->request.num_mapped_sgs - remaining; in dwc3_prepare_trbs_sg()
1422 unsigned int num_trbs = req->num_trbs; in dwc3_prepare_trbs_sg()
1429 for_each_sg(req->request.sg, s, num_queued_sgs, i) in dwc3_prepare_trbs_sg()
1430 length -= sg_dma_len(s); in dwc3_prepare_trbs_sg()
1440 length -= trb_length; in dwc3_prepare_trbs_sg()
1449 if ((i == remaining - 1) || !length) in dwc3_prepare_trbs_sg()
1471 list_for_each_entry(r, &dep->started_list, list) { in dwc3_prepare_trbs_sg()
1472 if (r != req && !r->request.no_interrupt) in dwc3_prepare_trbs_sg()
1492 req->start_sg = sg_next(s); in dwc3_prepare_trbs_sg()
1494 req->num_queued_sgs++; in dwc3_prepare_trbs_sg()
1495 req->num_pending_sgs--; in dwc3_prepare_trbs_sg()
1503 req->num_pending_sgs = 0; in dwc3_prepare_trbs_sg()
1511 return req->num_trbs - num_trbs; in dwc3_prepare_trbs_sg()
1517 return dwc3_prepare_last_sg(dep, req, req->request.length, 0); in dwc3_prepare_trbs_linear()
1521 * dwc3_prepare_trbs - setup TRBs from requests
1547 list_for_each_entry(req, &dep->started_list, list) { in dwc3_prepare_trbs()
1548 if (req->num_pending_sgs > 0) { in dwc3_prepare_trbs()
1550 if (!ret || req->num_pending_sgs) in dwc3_prepare_trbs()
1562 if (dep->stream_capable && req->request.is_last && in dwc3_prepare_trbs()
1563 !DWC3_MST_CAPABLE(&dep->dwc->hwparams)) in dwc3_prepare_trbs()
1567 list_for_each_entry_safe(req, n, &dep->pending_list, list) { in dwc3_prepare_trbs()
1568 struct dwc3 *dwc = dep->dwc; in dwc3_prepare_trbs()
1570 ret = usb_gadget_map_request_by_dev(dwc->sysdev, &req->request, in dwc3_prepare_trbs()
1571 dep->direction); in dwc3_prepare_trbs()
1575 req->sg = req->request.sg; in dwc3_prepare_trbs()
1576 req->start_sg = req->sg; in dwc3_prepare_trbs()
1577 req->num_queued_sgs = 0; in dwc3_prepare_trbs()
1578 req->num_pending_sgs = req->request.num_mapped_sgs; in dwc3_prepare_trbs()
1580 if (req->num_pending_sgs > 0) { in dwc3_prepare_trbs()
1582 if (req->num_pending_sgs) in dwc3_prepare_trbs()
1596 if (dep->stream_capable && req->request.is_last && in dwc3_prepare_trbs()
1597 !DWC3_MST_CAPABLE(&dwc->hwparams)) in dwc3_prepare_trbs()
1623 starting = !(dep->flags & DWC3_EP_TRANSFER_STARTED); in __dwc3_gadget_kick_transfer()
1632 req = next_request(&dep->started_list); in __dwc3_gadget_kick_transfer()
1634 dep->flags |= DWC3_EP_PENDING_REQUEST; in __dwc3_gadget_kick_transfer()
1641 params.param0 = upper_32_bits(req->trb_dma); in __dwc3_gadget_kick_transfer()
1642 params.param1 = lower_32_bits(req->trb_dma); in __dwc3_gadget_kick_transfer()
1645 if (dep->stream_capable) in __dwc3_gadget_kick_transfer()
1646 cmd |= DWC3_DEPCMD_PARAM(req->request.stream_id); in __dwc3_gadget_kick_transfer()
1648 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) in __dwc3_gadget_kick_transfer()
1649 cmd |= DWC3_DEPCMD_PARAM(dep->frame_number); in __dwc3_gadget_kick_transfer()
1652 DWC3_DEPCMD_PARAM(dep->resource_index); in __dwc3_gadget_kick_transfer()
1659 if (ret == -EAGAIN) in __dwc3_gadget_kick_transfer()
1664 list_for_each_entry_safe(req, tmp, &dep->started_list, list) in __dwc3_gadget_kick_transfer()
1668 if (!(dep->flags & DWC3_EP_END_TRANSFER_PENDING)) in __dwc3_gadget_kick_transfer()
1674 if (dep->stream_capable && req->request.is_last && in __dwc3_gadget_kick_transfer()
1675 !DWC3_MST_CAPABLE(&dep->dwc->hwparams)) in __dwc3_gadget_kick_transfer()
1676 dep->flags |= DWC3_EP_WAIT_TRANSFER_COMPLETE; in __dwc3_gadget_kick_transfer()
1685 reg = dwc3_readl(dwc->regs, DWC3_DSTS); in __dwc3_gadget_get_frame()
1690 * __dwc3_stop_active_transfer - stop the current active transfer
1709 cmd |= DWC3_DEPCMD_PARAM(dep->resource_index); in __dwc3_stop_active_transfer()
1718 if (ret == -ETIMEDOUT && dep->dwc->ep0state != EP0_SETUP_PHASE) { in __dwc3_stop_active_transfer()
1719 dep->flags |= DWC3_EP_DELAY_STOP; in __dwc3_stop_active_transfer()
1723 dep->resource_index = 0; in __dwc3_stop_active_transfer()
1726 dep->flags &= ~DWC3_EP_TRANSFER_STARTED; in __dwc3_stop_active_transfer()
1728 dep->flags |= DWC3_EP_END_TRANSFER_PENDING; in __dwc3_stop_active_transfer()
1730 dep->flags &= ~DWC3_EP_DELAY_STOP; in __dwc3_stop_active_transfer()
1735 * dwc3_gadget_start_isoc_quirk - workaround invalid frame number
1738 * This function tests for the correct combination of BIT[15:14] from the 16-bit
1742 * In DWC_usb31 version 1.70a-ea06 and prior, for highspeed and fullspeed
1743 * isochronous IN, BIT[15:14] of the 16-bit microframe number reported by the
1747 * internal 16-bit microframe, the START TRANSFER command will pass and the
1750 * other conditions, the START TRANSFER command will fail with bus-expiry.
1755 * (or 2 seconds). 4 seconds into the future will result in a bus-expiry status.
1758 * command status will result in a 2-second delay start. The smaller BIT[15:14]
1782 while (dep->combo_num < 2) { in dwc3_gadget_start_isoc_quirk()
1789 * 4 uframes in the future with BIT[15:14] as dep->combo_num in dwc3_gadget_start_isoc_quirk()
1791 test_frame_number = dep->frame_number & DWC3_FRNUMBER_MASK; in dwc3_gadget_start_isoc_quirk()
1792 test_frame_number |= dep->combo_num << 14; in dwc3_gadget_start_isoc_quirk()
1793 test_frame_number += max_t(u32, 4, dep->interval); in dwc3_gadget_start_isoc_quirk()
1795 params.param0 = upper_32_bits(dep->dwc->bounce_addr); in dwc3_gadget_start_isoc_quirk()
1796 params.param1 = lower_32_bits(dep->dwc->bounce_addr); in dwc3_gadget_start_isoc_quirk()
1802 /* Redo if some other failure beside bus-expiry is received */ in dwc3_gadget_start_isoc_quirk()
1803 if (cmd_status && cmd_status != -EAGAIN) { in dwc3_gadget_start_isoc_quirk()
1804 dep->start_cmd_status = 0; in dwc3_gadget_start_isoc_quirk()
1805 dep->combo_num = 0; in dwc3_gadget_start_isoc_quirk()
1810 if (dep->combo_num == 0) in dwc3_gadget_start_isoc_quirk()
1811 dep->start_cmd_status = cmd_status; in dwc3_gadget_start_isoc_quirk()
1813 dep->combo_num++; in dwc3_gadget_start_isoc_quirk()
1826 test0 = (dep->start_cmd_status == 0); in dwc3_gadget_start_isoc_quirk()
1830 dep->combo_num = 1; in dwc3_gadget_start_isoc_quirk()
1832 dep->combo_num = 2; in dwc3_gadget_start_isoc_quirk()
1834 dep->combo_num = 3; in dwc3_gadget_start_isoc_quirk()
1836 dep->combo_num = 0; in dwc3_gadget_start_isoc_quirk()
1838 dep->frame_number &= DWC3_FRNUMBER_MASK; in dwc3_gadget_start_isoc_quirk()
1839 dep->frame_number |= dep->combo_num << 14; in dwc3_gadget_start_isoc_quirk()
1840 dep->frame_number += max_t(u32, 4, dep->interval); in dwc3_gadget_start_isoc_quirk()
1843 dep->start_cmd_status = 0; in dwc3_gadget_start_isoc_quirk()
1844 dep->combo_num = 0; in dwc3_gadget_start_isoc_quirk()
1851 const struct usb_endpoint_descriptor *desc = dep->endpoint.desc; in __dwc3_gadget_start_isoc()
1852 struct dwc3 *dwc = dep->dwc; in __dwc3_gadget_start_isoc()
1856 if (list_empty(&dep->pending_list) && in __dwc3_gadget_start_isoc()
1857 list_empty(&dep->started_list)) { in __dwc3_gadget_start_isoc()
1858 dep->flags |= DWC3_EP_PENDING_REQUEST; in __dwc3_gadget_start_isoc()
1859 return -EAGAIN; in __dwc3_gadget_start_isoc()
1862 if (!dwc->dis_start_transfer_quirk && in __dwc3_gadget_start_isoc()
1865 if (dwc->gadget->speed <= USB_SPEED_HIGH && dep->direction) in __dwc3_gadget_start_isoc()
1869 if (desc->bInterval <= 14 && in __dwc3_gadget_start_isoc()
1870 dwc->gadget->speed >= USB_SPEED_HIGH) { in __dwc3_gadget_start_isoc()
1873 (dep->frame_number & DWC3_FRNUMBER_MASK); in __dwc3_gadget_start_isoc()
1884 dep->frame_number = (dep->frame_number & ~DWC3_FRNUMBER_MASK) | in __dwc3_gadget_start_isoc()
1887 dep->frame_number += BIT(14); in __dwc3_gadget_start_isoc()
1894 if (desc->bInterval < 3) in __dwc3_gadget_start_isoc()
1895 future_interval += 3 - desc->bInterval; in __dwc3_gadget_start_isoc()
1897 dep->frame_number = DWC3_ALIGN_FRAME(dep, future_interval); in __dwc3_gadget_start_isoc()
1900 if (ret != -EAGAIN) in __dwc3_gadget_start_isoc()
1905 * After a number of unsuccessful start attempts due to bus-expiry in __dwc3_gadget_start_isoc()
1909 if (ret == -EAGAIN) in __dwc3_gadget_start_isoc()
1917 struct dwc3 *dwc = dep->dwc; in __dwc3_gadget_ep_queue()
1919 if (!dep->endpoint.desc || !dwc->pullups_connected || !dwc->connected) { in __dwc3_gadget_ep_queue()
1920 dev_dbg(dwc->dev, "%s: can't queue to disabled endpoint\n", in __dwc3_gadget_ep_queue()
1921 dep->name); in __dwc3_gadget_ep_queue()
1922 return -ESHUTDOWN; in __dwc3_gadget_ep_queue()
1925 if (WARN(req->dep != dep, "request %pK belongs to '%s'\n", in __dwc3_gadget_ep_queue()
1926 &req->request, req->dep->name)) in __dwc3_gadget_ep_queue()
1927 return -EINVAL; in __dwc3_gadget_ep_queue()
1929 if (WARN(req->status < DWC3_REQUEST_STATUS_COMPLETED, in __dwc3_gadget_ep_queue()
1931 dep->name, &req->request)) in __dwc3_gadget_ep_queue()
1932 return -EINVAL; in __dwc3_gadget_ep_queue()
1934 pm_runtime_get(dwc->dev); in __dwc3_gadget_ep_queue()
1936 req->request.actual = 0; in __dwc3_gadget_ep_queue()
1937 req->request.status = -EINPROGRESS; in __dwc3_gadget_ep_queue()
1941 list_add_tail(&req->list, &dep->pending_list); in __dwc3_gadget_ep_queue()
1942 req->status = DWC3_REQUEST_STATUS_QUEUED; in __dwc3_gadget_ep_queue()
1944 if (dep->flags & DWC3_EP_WAIT_TRANSFER_COMPLETE) in __dwc3_gadget_ep_queue()
1951 if ((dep->flags & DWC3_EP_END_TRANSFER_PENDING) || in __dwc3_gadget_ep_queue()
1952 (dep->flags & DWC3_EP_WEDGE) || in __dwc3_gadget_ep_queue()
1953 (dep->flags & DWC3_EP_DELAY_STOP) || in __dwc3_gadget_ep_queue()
1954 (dep->flags & DWC3_EP_STALL)) { in __dwc3_gadget_ep_queue()
1955 dep->flags |= DWC3_EP_DELAY_START; in __dwc3_gadget_ep_queue()
1962 * (micro-)frame number. in __dwc3_gadget_ep_queue()
1967 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) { in __dwc3_gadget_ep_queue()
1968 if (!(dep->flags & DWC3_EP_TRANSFER_STARTED)) { in __dwc3_gadget_ep_queue()
1969 if ((dep->flags & DWC3_EP_PENDING_REQUEST)) in __dwc3_gadget_ep_queue()
1986 struct dwc3 *dwc = dep->dwc; in dwc3_gadget_ep_queue()
1992 spin_lock_irqsave(&dwc->lock, flags); in dwc3_gadget_ep_queue()
1994 spin_unlock_irqrestore(&dwc->lock, flags); in dwc3_gadget_ep_queue()
2003 /* If req->trb is not set, then the request has not started */ in dwc3_gadget_ep_skip_trbs()
2004 if (!req->trb) in dwc3_gadget_ep_skip_trbs()
2017 for (i = 0; i < req->num_trbs; i++) { in dwc3_gadget_ep_skip_trbs()
2020 trb = &dep->trb_pool[dep->trb_dequeue]; in dwc3_gadget_ep_skip_trbs()
2021 trb->ctrl &= ~DWC3_TRB_CTRL_HWO; in dwc3_gadget_ep_skip_trbs()
2025 req->num_trbs = 0; in dwc3_gadget_ep_skip_trbs()
2031 struct dwc3 *dwc = dep->dwc; in dwc3_gadget_ep_cleanup_cancelled_requests()
2033 while (!list_empty(&dep->cancelled_list)) { in dwc3_gadget_ep_cleanup_cancelled_requests()
2034 req = next_request(&dep->cancelled_list); in dwc3_gadget_ep_cleanup_cancelled_requests()
2036 switch (req->status) { in dwc3_gadget_ep_cleanup_cancelled_requests()
2038 dwc3_gadget_giveback(dep, req, -ESHUTDOWN); in dwc3_gadget_ep_cleanup_cancelled_requests()
2041 dwc3_gadget_giveback(dep, req, -ECONNRESET); in dwc3_gadget_ep_cleanup_cancelled_requests()
2044 dwc3_gadget_giveback(dep, req, -EPIPE); in dwc3_gadget_ep_cleanup_cancelled_requests()
2047 dev_err(dwc->dev, "request cancelled with wrong reason:%d\n", req->status); in dwc3_gadget_ep_cleanup_cancelled_requests()
2048 dwc3_gadget_giveback(dep, req, -ECONNRESET); in dwc3_gadget_ep_cleanup_cancelled_requests()
2055 if (!dep->endpoint.desc) in dwc3_gadget_ep_cleanup_cancelled_requests()
2067 struct dwc3 *dwc = dep->dwc; in dwc3_gadget_ep_dequeue()
2074 spin_lock_irqsave(&dwc->lock, flags); in dwc3_gadget_ep_dequeue()
2076 list_for_each_entry(r, &dep->cancelled_list, list) { in dwc3_gadget_ep_dequeue()
2081 list_for_each_entry(r, &dep->pending_list, list) { in dwc3_gadget_ep_dequeue()
2090 if (dep->number > 1) in dwc3_gadget_ep_dequeue()
2091 dwc3_gadget_giveback(dep, req, -ECONNRESET); in dwc3_gadget_ep_dequeue()
2098 list_for_each_entry(r, &dep->started_list, list) { in dwc3_gadget_ep_dequeue()
2109 list_for_each_entry_safe(r, t, &dep->started_list, list) in dwc3_gadget_ep_dequeue()
2113 dep->flags &= ~DWC3_EP_WAIT_TRANSFER_COMPLETE; in dwc3_gadget_ep_dequeue()
2119 dev_err(dwc->dev, "request %pK was not queued to %s\n", in dwc3_gadget_ep_dequeue()
2120 request, ep->name); in dwc3_gadget_ep_dequeue()
2121 ret = -EINVAL; in dwc3_gadget_ep_dequeue()
2123 spin_unlock_irqrestore(&dwc->lock, flags); in dwc3_gadget_ep_dequeue()
2131 struct dwc3 *dwc = dep->dwc; in __dwc3_gadget_ep_set_halt()
2136 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) { in __dwc3_gadget_ep_set_halt()
2137 dev_err(dwc->dev, "%s is of Isochronous type\n", dep->name); in __dwc3_gadget_ep_set_halt()
2138 return -EINVAL; in __dwc3_gadget_ep_set_halt()
2149 if (dep->number > 1) in __dwc3_gadget_ep_set_halt()
2150 trb = dwc3_ep_prev_trb(dep, dep->trb_enqueue); in __dwc3_gadget_ep_set_halt()
2152 trb = &dwc->ep0_trb[dep->trb_enqueue]; in __dwc3_gadget_ep_set_halt()
2154 transfer_in_flight = trb->ctrl & DWC3_TRB_CTRL_HWO; in __dwc3_gadget_ep_set_halt()
2155 started = !list_empty(&dep->started_list); in __dwc3_gadget_ep_set_halt()
2157 if (!protocol && ((dep->direction && transfer_in_flight) || in __dwc3_gadget_ep_set_halt()
2158 (!dep->direction && started))) { in __dwc3_gadget_ep_set_halt()
2159 return -EAGAIN; in __dwc3_gadget_ep_set_halt()
2165 dev_err(dwc->dev, "failed to set STALL on %s\n", in __dwc3_gadget_ep_set_halt()
2166 dep->name); in __dwc3_gadget_ep_set_halt()
2168 dep->flags |= DWC3_EP_STALL; in __dwc3_gadget_ep_set_halt()
2175 if (dep->number <= 1) { in __dwc3_gadget_ep_set_halt()
2176 dep->flags &= ~(DWC3_EP_STALL | DWC3_EP_WEDGE); in __dwc3_gadget_ep_set_halt()
2182 list_for_each_entry_safe(req, tmp, &dep->started_list, list) in __dwc3_gadget_ep_set_halt()
2185 if (dep->flags & DWC3_EP_END_TRANSFER_PENDING || in __dwc3_gadget_ep_set_halt()
2186 (dep->flags & DWC3_EP_DELAY_STOP)) { in __dwc3_gadget_ep_set_halt()
2187 dep->flags |= DWC3_EP_PENDING_CLEAR_STALL; in __dwc3_gadget_ep_set_halt()
2189 dwc->clear_stall_protocol = dep->number; in __dwc3_gadget_ep_set_halt()
2198 dev_err(dwc->dev, "failed to clear STALL on %s\n", in __dwc3_gadget_ep_set_halt()
2199 dep->name); in __dwc3_gadget_ep_set_halt()
2203 dep->flags &= ~(DWC3_EP_STALL | DWC3_EP_WEDGE); in __dwc3_gadget_ep_set_halt()
2205 if ((dep->flags & DWC3_EP_DELAY_START) && in __dwc3_gadget_ep_set_halt()
2206 !usb_endpoint_xfer_isoc(dep->endpoint.desc)) in __dwc3_gadget_ep_set_halt()
2209 dep->flags &= ~DWC3_EP_DELAY_START; in __dwc3_gadget_ep_set_halt()
2218 struct dwc3 *dwc = dep->dwc; in dwc3_gadget_ep_set_halt()
2224 spin_lock_irqsave(&dwc->lock, flags); in dwc3_gadget_ep_set_halt()
2226 spin_unlock_irqrestore(&dwc->lock, flags); in dwc3_gadget_ep_set_halt()
2234 struct dwc3 *dwc = dep->dwc; in dwc3_gadget_ep_set_wedge()
2238 spin_lock_irqsave(&dwc->lock, flags); in dwc3_gadget_ep_set_wedge()
2239 dep->flags |= DWC3_EP_WEDGE; in dwc3_gadget_ep_set_wedge()
2241 if (dep->number == 0 || dep->number == 1) in dwc3_gadget_ep_set_wedge()
2245 spin_unlock_irqrestore(&dwc->lock, flags); in dwc3_gadget_ep_set_wedge()
2250 /* -------------------------------------------------------------------------- */
2280 /* -------------------------------------------------------------------------- */
2289 reg = dwc3_readl(dwc->regs, DWC3_DEVTEN); in dwc3_gadget_enable_linksts_evts()
2295 dwc3_writel(dwc->regs, DWC3_DEVTEN, reg); in dwc3_gadget_enable_linksts_evts()
2320 reg = dwc3_readl(dwc->regs, DWC3_DSTS); in __dwc3_gadget_wakeup()
2333 return -EINVAL; in __dwc3_gadget_wakeup()
2341 dev_err(dwc->dev, "failed to put link in Recovery\n"); in __dwc3_gadget_wakeup()
2349 reg = dwc3_readl(dwc->regs, DWC3_DCTL); in __dwc3_gadget_wakeup()
2351 dwc3_writel(dwc->regs, DWC3_DCTL, reg); in __dwc3_gadget_wakeup()
2364 while (retries--) { in __dwc3_gadget_wakeup()
2365 reg = dwc3_readl(dwc->regs, DWC3_DSTS); in __dwc3_gadget_wakeup()
2373 dev_err(dwc->dev, "failed to send remote wakeup\n"); in __dwc3_gadget_wakeup()
2374 return -EINVAL; in __dwc3_gadget_wakeup()
2386 if (!dwc->wakeup_configured) { in dwc3_gadget_wakeup()
2387 dev_err(dwc->dev, "remote wakeup not configured\n"); in dwc3_gadget_wakeup()
2388 return -EINVAL; in dwc3_gadget_wakeup()
2391 spin_lock_irqsave(&dwc->lock, flags); in dwc3_gadget_wakeup()
2392 if (!dwc->gadget->wakeup_armed) { in dwc3_gadget_wakeup()
2393 dev_err(dwc->dev, "not armed for remote wakeup\n"); in dwc3_gadget_wakeup()
2394 spin_unlock_irqrestore(&dwc->lock, flags); in dwc3_gadget_wakeup()
2395 return -EINVAL; in dwc3_gadget_wakeup()
2399 spin_unlock_irqrestore(&dwc->lock, flags); in dwc3_gadget_wakeup()
2413 if (!dwc->wakeup_configured) { in dwc3_gadget_func_wakeup()
2414 dev_err(dwc->dev, "remote wakeup not configured\n"); in dwc3_gadget_func_wakeup()
2415 return -EINVAL; in dwc3_gadget_func_wakeup()
2418 spin_lock_irqsave(&dwc->lock, flags); in dwc3_gadget_func_wakeup()
2427 spin_unlock_irqrestore(&dwc->lock, flags); in dwc3_gadget_func_wakeup()
2428 return -EINVAL; in dwc3_gadget_func_wakeup()
2431 dwc->suspended = false; in dwc3_gadget_func_wakeup()
2432 dwc->link_state = DWC3_LINK_STATE_U0; in dwc3_gadget_func_wakeup()
2439 dev_err(dwc->dev, "function remote wakeup failed, ret:%d\n", ret); in dwc3_gadget_func_wakeup()
2441 spin_unlock_irqrestore(&dwc->lock, flags); in dwc3_gadget_func_wakeup()
2451 spin_lock_irqsave(&dwc->lock, flags); in dwc3_gadget_set_remote_wakeup()
2452 dwc->wakeup_configured = !!set; in dwc3_gadget_set_remote_wakeup()
2453 spin_unlock_irqrestore(&dwc->lock, flags); in dwc3_gadget_set_remote_wakeup()
2464 spin_lock_irqsave(&dwc->lock, flags); in dwc3_gadget_set_selfpowered()
2465 g->is_selfpowered = !!is_selfpowered; in dwc3_gadget_set_selfpowered()
2466 spin_unlock_irqrestore(&dwc->lock, flags); in dwc3_gadget_set_selfpowered()
2475 for (epnum = 2; epnum < dwc->num_eps; epnum++) { in dwc3_stop_active_transfers()
2478 dep = dwc->eps[epnum]; in dwc3_stop_active_transfers()
2482 dwc3_remove_requests(dwc, dep, -ESHUTDOWN); in dwc3_stop_active_transfers()
2488 enum usb_ssp_rate ssp_rate = dwc->gadget_ssp_rate; in __dwc3_gadget_set_ssp_rate()
2492 ssp_rate = dwc->max_ssp_rate; in __dwc3_gadget_set_ssp_rate()
2494 reg = dwc3_readl(dwc->regs, DWC3_DCFG); in __dwc3_gadget_set_ssp_rate()
2500 else if (dwc->max_ssp_rate != USB_SSP_GEN_1x2) in __dwc3_gadget_set_ssp_rate()
2504 dwc->max_ssp_rate != USB_SSP_GEN_2x1) in __dwc3_gadget_set_ssp_rate()
2507 dwc3_writel(dwc->regs, DWC3_DCFG, reg); in __dwc3_gadget_set_ssp_rate()
2515 speed = dwc->gadget_max_speed; in __dwc3_gadget_set_speed()
2516 if (speed == USB_SPEED_UNKNOWN || speed > dwc->maximum_speed) in __dwc3_gadget_set_speed()
2517 speed = dwc->maximum_speed; in __dwc3_gadget_set_speed()
2525 reg = dwc3_readl(dwc->regs, DWC3_DCFG); in __dwc3_gadget_set_speed()
2531 * bit if we try to force the IP to USB2-only mode. in __dwc3_gadget_set_speed()
2542 !dwc->dis_metastability_quirk) { in __dwc3_gadget_set_speed()
2562 dev_err(dwc->dev, "invalid speed (%d)\n", speed); in __dwc3_gadget_set_speed()
2576 dwc3_writel(dwc->regs, DWC3_DCFG, reg); in __dwc3_gadget_set_speed()
2584 if (pm_runtime_suspended(dwc->dev)) in dwc3_gadget_run_stop()
2587 reg = dwc3_readl(dwc->regs, DWC3_DCTL); in dwc3_gadget_run_stop()
2599 dwc->pullups_connected = true; in dwc3_gadget_run_stop()
2603 dwc->pullups_connected = false; in dwc3_gadget_run_stop()
2610 reg = dwc3_readl(dwc->regs, DWC3_DSTS); in dwc3_gadget_run_stop()
2612 } while (--timeout && !(!is_on ^ !reg)); in dwc3_gadget_run_stop()
2615 return -ETIMEDOUT; in dwc3_gadget_run_stop()
2629 spin_lock_irqsave(&dwc->lock, flags); in dwc3_gadget_soft_disconnect()
2630 if (!dwc->pullups_connected) { in dwc3_gadget_soft_disconnect()
2631 spin_unlock_irqrestore(&dwc->lock, flags); in dwc3_gadget_soft_disconnect()
2635 dwc->connected = false; in dwc3_gadget_soft_disconnect()
2641 if (dwc->delayed_status) in dwc3_gadget_soft_disconnect()
2645 * In the Synopsys DesignWare Cores USB3 Databook Rev. 3.30a in dwc3_gadget_soft_disconnect()
2646 * Section 4.1.8 Table 4-7, it states that for a device-initiated in dwc3_gadget_soft_disconnect()
2652 spin_unlock_irqrestore(&dwc->lock, flags); in dwc3_gadget_soft_disconnect()
2661 if (dwc->ep0state != EP0_SETUP_PHASE) { in dwc3_gadget_soft_disconnect()
2662 reinit_completion(&dwc->ep0_in_setup); in dwc3_gadget_soft_disconnect()
2664 ret = wait_for_completion_timeout(&dwc->ep0_in_setup, in dwc3_gadget_soft_disconnect()
2667 dev_warn(dwc->dev, "wait for SETUP phase timed out\n"); in dwc3_gadget_soft_disconnect()
2668 spin_lock_irqsave(&dwc->lock, flags); in dwc3_gadget_soft_disconnect()
2670 spin_unlock_irqrestore(&dwc->lock, flags); in dwc3_gadget_soft_disconnect()
2689 spin_lock_irqsave(&dwc->lock, flags); in dwc3_gadget_soft_disconnect()
2691 spin_unlock_irqrestore(&dwc->lock, flags); in dwc3_gadget_soft_disconnect()
2703 * device-initiated disconnect requires a core soft reset in dwc3_gadget_soft_connect()
2722 dwc->softconnect = is_on; in dwc3_gadget_pullup()
2730 pm_runtime_barrier(dwc->dev); in dwc3_gadget_pullup()
2731 if (pm_runtime_suspended(dwc->dev)) in dwc3_gadget_pullup()
2740 ret = pm_runtime_get_sync(dwc->dev); in dwc3_gadget_pullup()
2742 pm_runtime_put(dwc->dev); in dwc3_gadget_pullup()
2744 pm_runtime_set_suspended(dwc->dev); in dwc3_gadget_pullup()
2748 if (dwc->pullups_connected == is_on) { in dwc3_gadget_pullup()
2749 pm_runtime_put(dwc->dev); in dwc3_gadget_pullup()
2753 synchronize_irq(dwc->irq_gadget); in dwc3_gadget_pullup()
2760 pm_runtime_put(dwc->dev); in dwc3_gadget_pullup()
2781 /* On 2.30a and above this bit enables U3/L2-L1 Suspend Events */ in dwc3_gadget_enable_irq()
2785 dwc3_writel(dwc->regs, DWC3_DEVTEN, reg); in dwc3_gadget_enable_irq()
2791 dwc3_writel(dwc->regs, DWC3_DEVTEN, 0x00); in dwc3_gadget_disable_irq()
2798 * dwc3_gadget_setup_nump - calculate and initialize NUMP field of %DWC3_DCFG
2811 * RxFIFO Size = (RAM2_DEPTH * MDWIDTH / 8) - 24 - 16;
2825 ram2_depth = DWC3_GHWPARAMS7_RAM2_DEPTH(dwc->hwparams.hwparams7); in dwc3_gadget_setup_nump()
2828 nump = ((ram2_depth * mdwidth / 8) - 24 - 16) / 1024; in dwc3_gadget_setup_nump()
2832 reg = dwc3_readl(dwc->regs, DWC3_DCFG); in dwc3_gadget_setup_nump()
2835 dwc3_writel(dwc->regs, DWC3_DCFG, reg); in dwc3_gadget_setup_nump()
2845 * Use IMOD if enabled via dwc->imod_interval. Otherwise, if in __dwc3_gadget_start()
2848 if (dwc->imod_interval) { in __dwc3_gadget_start()
2849 dwc3_writel(dwc->regs, DWC3_DEV_IMOD(0), dwc->imod_interval); in __dwc3_gadget_start()
2850 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), DWC3_GEVNTCOUNT_EHB); in __dwc3_gadget_start()
2852 dwc3_writel(dwc->regs, DWC3_DEV_IMOD(0), 0); in __dwc3_gadget_start()
2862 reg = dwc3_readl(dwc->regs, DWC3_GRXTHRCFG); in __dwc3_gadget_start()
2868 dwc3_writel(dwc->regs, DWC3_GRXTHRCFG, reg); in __dwc3_gadget_start()
2879 reg = dwc3_readl(dwc->regs, DWC3_DCFG); in __dwc3_gadget_start()
2881 dwc3_writel(dwc->regs, DWC3_DCFG, reg); in __dwc3_gadget_start()
2883 /* Enable MST by default if the device is capable of MST */ in __dwc3_gadget_start()
2884 if (DWC3_MST_CAPABLE(&dwc->hwparams)) { in __dwc3_gadget_start()
2885 reg = dwc3_readl(dwc->regs, DWC3_DCFG1); in __dwc3_gadget_start()
2887 dwc3_writel(dwc->regs, DWC3_DCFG1, reg); in __dwc3_gadget_start()
2895 dev_err(dwc->dev, "failed to config endpoints\n"); in __dwc3_gadget_start()
2899 dep = dwc->eps[0]; in __dwc3_gadget_start()
2900 dep->flags = 0; in __dwc3_gadget_start()
2903 dev_err(dwc->dev, "failed to enable %s\n", dep->name); in __dwc3_gadget_start()
2907 dep = dwc->eps[1]; in __dwc3_gadget_start()
2908 dep->flags = 0; in __dwc3_gadget_start()
2911 dev_err(dwc->dev, "failed to enable %s\n", dep->name); in __dwc3_gadget_start()
2916 dwc->ep0state = EP0_SETUP_PHASE; in __dwc3_gadget_start()
2917 dwc->ep0_bounced = false; in __dwc3_gadget_start()
2918 dwc->link_state = DWC3_LINK_STATE_SS_DIS; in __dwc3_gadget_start()
2919 dwc->delayed_status = false; in __dwc3_gadget_start()
2928 __dwc3_gadget_ep_disable(dwc->eps[0]); in __dwc3_gadget_start()
2942 irq = dwc->irq_gadget; in dwc3_gadget_start()
2944 IRQF_SHARED, "dwc3", dwc->ev_buf); in dwc3_gadget_start()
2946 dev_err(dwc->dev, "failed to request irq #%d --> %d\n", in dwc3_gadget_start()
2951 spin_lock_irqsave(&dwc->lock, flags); in dwc3_gadget_start()
2952 dwc->gadget_driver = driver; in dwc3_gadget_start()
2953 spin_unlock_irqrestore(&dwc->lock, flags); in dwc3_gadget_start()
2955 if (dwc->sys_wakeup) in dwc3_gadget_start()
2956 device_wakeup_enable(dwc->sysdev); in dwc3_gadget_start()
2964 __dwc3_gadget_ep_disable(dwc->eps[0]); in __dwc3_gadget_stop()
2965 __dwc3_gadget_ep_disable(dwc->eps[1]); in __dwc3_gadget_stop()
2973 if (dwc->sys_wakeup) in dwc3_gadget_stop()
2974 device_wakeup_disable(dwc->sysdev); in dwc3_gadget_stop()
2976 spin_lock_irqsave(&dwc->lock, flags); in dwc3_gadget_stop()
2977 dwc->gadget_driver = NULL; in dwc3_gadget_stop()
2978 dwc->max_cfg_eps = 0; in dwc3_gadget_stop()
2979 spin_unlock_irqrestore(&dwc->lock, flags); in dwc3_gadget_stop()
2981 free_irq(dwc->irq_gadget, dwc->ev_buf); in dwc3_gadget_stop()
2991 params->besl_baseline = USB_DEFAULT_BESL_UNSPECIFIED; in dwc3_gadget_config_params()
2992 params->besl_deep = USB_DEFAULT_BESL_UNSPECIFIED; in dwc3_gadget_config_params()
2995 if (!dwc->dis_enblslpm_quirk) { in dwc3_gadget_config_params()
3005 params->besl_baseline = 1; in dwc3_gadget_config_params()
3006 if (dwc->is_utmi_l1_suspend) in dwc3_gadget_config_params()
3007 params->besl_deep = in dwc3_gadget_config_params()
3008 clamp_t(u8, dwc->hird_threshold, 2, 15); in dwc3_gadget_config_params()
3012 if (dwc->dis_u1_entry_quirk) in dwc3_gadget_config_params()
3013 params->bU1devExitLat = 0; in dwc3_gadget_config_params()
3015 params->bU1devExitLat = DWC3_DEFAULT_U1_DEV_EXIT_LAT; in dwc3_gadget_config_params()
3018 if (dwc->dis_u2_entry_quirk) in dwc3_gadget_config_params()
3019 params->bU2DevExitLat = 0; in dwc3_gadget_config_params()
3021 params->bU2DevExitLat = in dwc3_gadget_config_params()
3031 spin_lock_irqsave(&dwc->lock, flags); in dwc3_gadget_set_speed()
3032 dwc->gadget_max_speed = speed; in dwc3_gadget_set_speed()
3033 spin_unlock_irqrestore(&dwc->lock, flags); in dwc3_gadget_set_speed()
3042 spin_lock_irqsave(&dwc->lock, flags); in dwc3_gadget_set_ssp_rate()
3043 dwc->gadget_max_speed = USB_SPEED_SUPER_PLUS; in dwc3_gadget_set_ssp_rate()
3044 dwc->gadget_ssp_rate = rate; in dwc3_gadget_set_ssp_rate()
3045 spin_unlock_irqrestore(&dwc->lock, flags); in dwc3_gadget_set_ssp_rate()
3054 if (dwc->usb2_phy) in dwc3_gadget_vbus_draw()
3055 return usb_phy_set_power(dwc->usb2_phy, mA); in dwc3_gadget_vbus_draw()
3057 if (!dwc->usb_psy) in dwc3_gadget_vbus_draw()
3058 return -EOPNOTSUPP; in dwc3_gadget_vbus_draw()
3061 ret = power_supply_set_property(dwc->usb_psy, POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT, &val); in dwc3_gadget_vbus_draw()
3067 * dwc3_gadget_check_config - ensure dwc3 can support the USB configuration
3084 if (!dwc->do_fifo_resize) in dwc3_gadget_check_config()
3087 list_for_each_entry(ep, &g->ep_list, ep_list) { in dwc3_gadget_check_config()
3089 if (ep->claimed && (ep->address & USB_DIR_IN)) in dwc3_gadget_check_config()
3093 if (ep_num <= dwc->max_cfg_eps) in dwc3_gadget_check_config()
3097 dwc->max_cfg_eps = ep_num; in dwc3_gadget_check_config()
3099 fifo_size = dwc3_gadget_calc_tx_fifo_size(dwc, dwc->max_cfg_eps); in dwc3_gadget_check_config()
3101 fifo_size += dwc->max_cfg_eps; in dwc3_gadget_check_config()
3104 ram1_depth = DWC3_RAM1_DEPTH(dwc->hwparams.hwparams7); in dwc3_gadget_check_config()
3106 return -ENOMEM; in dwc3_gadget_check_config()
3116 spin_lock_irqsave(&dwc->lock, flags); in dwc3_gadget_async_callbacks()
3117 dwc->async_callbacks = enable; in dwc3_gadget_async_callbacks()
3118 spin_unlock_irqrestore(&dwc->lock, flags); in dwc3_gadget_async_callbacks()
3138 /* -------------------------------------------------------------------------- */
3142 struct dwc3 *dwc = dep->dwc; in dwc3_gadget_init_control_endpoint()
3144 usb_ep_set_maxpacket_limit(&dep->endpoint, 512); in dwc3_gadget_init_control_endpoint()
3145 dep->endpoint.maxburst = 1; in dwc3_gadget_init_control_endpoint()
3146 dep->endpoint.ops = &dwc3_gadget_ep0_ops; in dwc3_gadget_init_control_endpoint()
3147 if (!dep->direction) in dwc3_gadget_init_control_endpoint()
3148 dwc->gadget->ep0 = &dep->endpoint; in dwc3_gadget_init_control_endpoint()
3150 dep->endpoint.caps.type_control = true; in dwc3_gadget_init_control_endpoint()
3157 struct dwc3 *dwc = dep->dwc; in dwc3_gadget_init_in_endpoint()
3167 size = dwc3_readl(dwc->regs, DWC3_GTXFIFOSIZ(dep->number >> 1)); in dwc3_gadget_init_in_endpoint()
3178 * maxpacket = mdwidth * (fifo_size - 1); in dwc3_gadget_init_in_endpoint()
3182 * maxpacket = mdwidth * ((fifo_size - 1) - 1) - mdwidth; in dwc3_gadget_init_in_endpoint()
3185 maxpacket = mdwidth * (size - 1); in dwc3_gadget_init_in_endpoint()
3187 maxpacket = mdwidth * ((size - 1) - 1) - mdwidth; in dwc3_gadget_init_in_endpoint()
3191 usb_ep_set_maxpacket_limit(&dep->endpoint, size); in dwc3_gadget_init_in_endpoint()
3193 dep->endpoint.max_streams = 16; in dwc3_gadget_init_in_endpoint()
3194 dep->endpoint.ops = &dwc3_gadget_ep_ops; in dwc3_gadget_init_in_endpoint()
3195 list_add_tail(&dep->endpoint.ep_list, in dwc3_gadget_init_in_endpoint()
3196 &dwc->gadget->ep_list); in dwc3_gadget_init_in_endpoint()
3197 dep->endpoint.caps.type_iso = true; in dwc3_gadget_init_in_endpoint()
3198 dep->endpoint.caps.type_bulk = true; in dwc3_gadget_init_in_endpoint()
3199 dep->endpoint.caps.type_int = true; in dwc3_gadget_init_in_endpoint()
3206 struct dwc3 *dwc = dep->dwc; in dwc3_gadget_init_out_endpoint()
3216 size = dwc3_readl(dwc->regs, DWC3_GRXFIFOSIZ(0)); in dwc3_gadget_init_out_endpoint()
3233 size -= (3 * 8) + 16; in dwc3_gadget_init_out_endpoint()
3239 usb_ep_set_maxpacket_limit(&dep->endpoint, size); in dwc3_gadget_init_out_endpoint()
3240 dep->endpoint.max_streams = 16; in dwc3_gadget_init_out_endpoint()
3241 dep->endpoint.ops = &dwc3_gadget_ep_ops; in dwc3_gadget_init_out_endpoint()
3242 list_add_tail(&dep->endpoint.ep_list, in dwc3_gadget_init_out_endpoint()
3243 &dwc->gadget->ep_list); in dwc3_gadget_init_out_endpoint()
3244 dep->endpoint.caps.type_iso = true; in dwc3_gadget_init_out_endpoint()
3245 dep->endpoint.caps.type_bulk = true; in dwc3_gadget_init_out_endpoint()
3246 dep->endpoint.caps.type_int = true; in dwc3_gadget_init_out_endpoint()
3260 return -ENOMEM; in dwc3_gadget_init_endpoint()
3262 dep->dwc = dwc; in dwc3_gadget_init_endpoint()
3263 dep->number = epnum; in dwc3_gadget_init_endpoint()
3264 dep->direction = direction; in dwc3_gadget_init_endpoint()
3265 dep->regs = dwc->regs + DWC3_DEP_BASE(epnum); in dwc3_gadget_init_endpoint()
3266 dwc->eps[epnum] = dep; in dwc3_gadget_init_endpoint()
3267 dep->combo_num = 0; in dwc3_gadget_init_endpoint()
3268 dep->start_cmd_status = 0; in dwc3_gadget_init_endpoint()
3270 snprintf(dep->name, sizeof(dep->name), "ep%u%s", num, in dwc3_gadget_init_endpoint()
3273 dep->endpoint.name = dep->name; in dwc3_gadget_init_endpoint()
3275 if (!(dep->number > 1)) { in dwc3_gadget_init_endpoint()
3276 dep->endpoint.desc = &dwc3_gadget_ep0_desc; in dwc3_gadget_init_endpoint()
3277 dep->endpoint.comp_desc = NULL; in dwc3_gadget_init_endpoint()
3290 dep->endpoint.caps.dir_in = direction; in dwc3_gadget_init_endpoint()
3291 dep->endpoint.caps.dir_out = !direction; in dwc3_gadget_init_endpoint()
3293 INIT_LIST_HEAD(&dep->pending_list); in dwc3_gadget_init_endpoint()
3294 INIT_LIST_HEAD(&dep->started_list); in dwc3_gadget_init_endpoint()
3295 INIT_LIST_HEAD(&dep->cancelled_list); in dwc3_gadget_init_endpoint()
3306 INIT_LIST_HEAD(&dwc->gadget->ep_list); in dwc3_gadget_init_endpoints()
3325 dep = dwc->eps[epnum]; in dwc3_gadget_free_endpoints()
3330 * bi-directional USB endpoint 0. in dwc3_gadget_free_endpoints()
3339 list_del(&dep->endpoint.ep_list); in dwc3_gadget_free_endpoints()
3347 /* -------------------------------------------------------------------------- */
3358 req->num_trbs--; in dwc3_gadget_ep_reclaim_completed_trb()
3370 if (chain && (trb->ctrl & DWC3_TRB_CTRL_HWO)) in dwc3_gadget_ep_reclaim_completed_trb()
3371 trb->ctrl &= ~DWC3_TRB_CTRL_HWO; in dwc3_gadget_ep_reclaim_completed_trb()
3375 * have the Isoc-First type. Track and report its interval frame number. in dwc3_gadget_ep_reclaim_completed_trb()
3377 if (usb_endpoint_xfer_isoc(dep->endpoint.desc) && in dwc3_gadget_ep_reclaim_completed_trb()
3378 (trb->ctrl & DWC3_TRBCTL_ISOCHRONOUS_FIRST)) { in dwc3_gadget_ep_reclaim_completed_trb()
3381 frame_number = DWC3_TRB_CTRL_GET_SID_SOFN(trb->ctrl); in dwc3_gadget_ep_reclaim_completed_trb()
3382 frame_number &= ~(dep->interval - 1); in dwc3_gadget_ep_reclaim_completed_trb()
3383 req->request.frame_number = frame_number; in dwc3_gadget_ep_reclaim_completed_trb()
3389 * TRB. Don't add it to req->remaining calculation. in dwc3_gadget_ep_reclaim_completed_trb()
3391 if (trb->bpl == lower_32_bits(dep->dwc->bounce_addr) && in dwc3_gadget_ep_reclaim_completed_trb()
3392 trb->bph == upper_32_bits(dep->dwc->bounce_addr)) { in dwc3_gadget_ep_reclaim_completed_trb()
3393 trb->ctrl &= ~DWC3_TRB_CTRL_HWO; in dwc3_gadget_ep_reclaim_completed_trb()
3397 count = trb->size & DWC3_TRB_SIZE_MASK; in dwc3_gadget_ep_reclaim_completed_trb()
3398 req->remaining += count; in dwc3_gadget_ep_reclaim_completed_trb()
3400 if ((trb->ctrl & DWC3_TRB_CTRL_HWO) && status != -ESHUTDOWN) in dwc3_gadget_ep_reclaim_completed_trb()
3403 if (event->status & DEPEVT_STATUS_SHORT && !chain) in dwc3_gadget_ep_reclaim_completed_trb()
3406 if ((trb->ctrl & DWC3_TRB_CTRL_ISP_IMI) && in dwc3_gadget_ep_reclaim_completed_trb()
3407 DWC3_TRB_SIZE_TRBSTS(trb->size) == DWC3_TRBSTS_MISSED_ISOC) in dwc3_gadget_ep_reclaim_completed_trb()
3410 if ((trb->ctrl & DWC3_TRB_CTRL_IOC) || in dwc3_gadget_ep_reclaim_completed_trb()
3411 (trb->ctrl & DWC3_TRB_CTRL_LST)) in dwc3_gadget_ep_reclaim_completed_trb()
3421 struct dwc3_trb *trb = &dep->trb_pool[dep->trb_dequeue]; in dwc3_gadget_ep_reclaim_trb_sg()
3422 struct scatterlist *sg = req->sg; in dwc3_gadget_ep_reclaim_trb_sg()
3424 unsigned int num_queued = req->num_queued_sgs; in dwc3_gadget_ep_reclaim_trb_sg()
3429 trb = &dep->trb_pool[dep->trb_dequeue]; in dwc3_gadget_ep_reclaim_trb_sg()
3431 req->sg = sg_next(s); in dwc3_gadget_ep_reclaim_trb_sg()
3432 req->num_queued_sgs--; in dwc3_gadget_ep_reclaim_trb_sg()
3447 struct dwc3_trb *trb = &dep->trb_pool[dep->trb_dequeue]; in dwc3_gadget_ep_reclaim_trb_linear()
3455 return req->num_pending_sgs == 0 && req->num_queued_sgs == 0; in dwc3_gadget_ep_request_completed()
3465 if (req->request.num_mapped_sgs) in dwc3_gadget_ep_cleanup_completed_request()
3472 req->request.actual = req->request.length - req->remaining; in dwc3_gadget_ep_cleanup_completed_request()
3477 if (req->needs_extra_trb) { in dwc3_gadget_ep_cleanup_completed_request()
3480 req->needs_extra_trb = false; in dwc3_gadget_ep_cleanup_completed_request()
3489 if (req->request.no_interrupt) { in dwc3_gadget_ep_cleanup_completed_request()
3492 trb = dwc3_ep_prev_trb(dep, dep->trb_dequeue); in dwc3_gadget_ep_cleanup_completed_request()
3493 switch (DWC3_TRB_SIZE_TRBSTS(trb->size)) { in dwc3_gadget_ep_cleanup_completed_request()
3496 request_status = -EXDEV; in dwc3_gadget_ep_cleanup_completed_request()
3522 while (!list_empty(&dep->started_list)) { in dwc3_gadget_ep_cleanup_completed_requests()
3525 req = next_request(&dep->started_list); in dwc3_gadget_ep_cleanup_completed_requests()
3534 if (!dep->endpoint.desc) in dwc3_gadget_ep_cleanup_completed_requests()
3542 struct dwc3 *dwc = dep->dwc; in dwc3_gadget_ep_should_continue()
3544 if (!dep->endpoint.desc || !dwc->pullups_connected || in dwc3_gadget_ep_should_continue()
3545 !dwc->connected) in dwc3_gadget_ep_should_continue()
3548 if (!list_empty(&dep->pending_list)) in dwc3_gadget_ep_should_continue()
3555 req = next_request(&dep->started_list); in dwc3_gadget_ep_should_continue()
3565 dep->frame_number = event->parameters; in dwc3_gadget_endpoint_frame_from_event()
3571 struct dwc3 *dwc = dep->dwc; in dwc3_gadget_endpoint_trbs_complete()
3576 if (dep->flags & DWC3_EP_END_TRANSFER_PENDING) in dwc3_gadget_endpoint_trbs_complete()
3579 if (!dep->endpoint.desc) in dwc3_gadget_endpoint_trbs_complete()
3582 if (usb_endpoint_xfer_isoc(dep->endpoint.desc) && in dwc3_gadget_endpoint_trbs_complete()
3583 list_empty(&dep->started_list) && in dwc3_gadget_endpoint_trbs_complete()
3584 (list_empty(&dep->pending_list) || status == -EXDEV)) in dwc3_gadget_endpoint_trbs_complete()
3592 * WORKAROUND: This is the 2nd half of U1/U2 -> U0 workaround. in dwc3_gadget_endpoint_trbs_complete()
3600 dep = dwc->eps[i]; in dwc3_gadget_endpoint_trbs_complete()
3602 if (!(dep->flags & DWC3_EP_ENABLED)) in dwc3_gadget_endpoint_trbs_complete()
3605 if (!list_empty(&dep->started_list)) in dwc3_gadget_endpoint_trbs_complete()
3609 reg = dwc3_readl(dwc->regs, DWC3_DCTL); in dwc3_gadget_endpoint_trbs_complete()
3610 reg |= dwc->u1u2; in dwc3_gadget_endpoint_trbs_complete()
3611 dwc3_writel(dwc->regs, DWC3_DCTL, reg); in dwc3_gadget_endpoint_trbs_complete()
3613 dwc->u1u2 = 0; in dwc3_gadget_endpoint_trbs_complete()
3624 if (!dep->endpoint.desc) in dwc3_gadget_endpoint_transfer_in_progress()
3627 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) in dwc3_gadget_endpoint_transfer_in_progress()
3630 if (event->status & DEPEVT_STATUS_BUSERR) in dwc3_gadget_endpoint_transfer_in_progress()
3631 status = -ECONNRESET; in dwc3_gadget_endpoint_transfer_in_progress()
3633 if (event->status & DEPEVT_STATUS_MISSED_ISOC) in dwc3_gadget_endpoint_transfer_in_progress()
3634 status = -EXDEV; in dwc3_gadget_endpoint_transfer_in_progress()
3644 dep->flags &= ~DWC3_EP_TRANSFER_STARTED; in dwc3_gadget_endpoint_transfer_complete()
3646 if (event->status & DEPEVT_STATUS_BUSERR) in dwc3_gadget_endpoint_transfer_complete()
3647 status = -ECONNRESET; in dwc3_gadget_endpoint_transfer_complete()
3650 dep->flags &= ~DWC3_EP_WAIT_TRANSFER_COMPLETE; in dwc3_gadget_endpoint_transfer_complete()
3666 if (dep->flags & DWC3_EP_END_TRANSFER_PENDING) in dwc3_gadget_endpoint_transfer_not_ready()
3675 u8 cmd = DEPEVT_PARAMETER_CMD(event->parameters); in dwc3_gadget_endpoint_command_complete()
3685 if (dep->stream_capable) in dwc3_gadget_endpoint_command_complete()
3686 dep->flags |= DWC3_EP_IGNORE_NEXT_NOSTREAM; in dwc3_gadget_endpoint_command_complete()
3688 dep->flags &= ~DWC3_EP_END_TRANSFER_PENDING; in dwc3_gadget_endpoint_command_complete()
3689 dep->flags &= ~DWC3_EP_TRANSFER_STARTED; in dwc3_gadget_endpoint_command_complete()
3692 if (dep->flags & DWC3_EP_PENDING_CLEAR_STALL) { in dwc3_gadget_endpoint_command_complete()
3693 struct dwc3 *dwc = dep->dwc; in dwc3_gadget_endpoint_command_complete()
3695 dep->flags &= ~DWC3_EP_PENDING_CLEAR_STALL; in dwc3_gadget_endpoint_command_complete()
3697 struct usb_ep *ep0 = &dwc->eps[0]->endpoint; in dwc3_gadget_endpoint_command_complete()
3699 dev_err(dwc->dev, "failed to clear STALL on %s\n", dep->name); in dwc3_gadget_endpoint_command_complete()
3700 if (dwc->delayed_status) in dwc3_gadget_endpoint_command_complete()
3705 dep->flags &= ~(DWC3_EP_STALL | DWC3_EP_WEDGE); in dwc3_gadget_endpoint_command_complete()
3706 if (dwc->clear_stall_protocol == dep->number) in dwc3_gadget_endpoint_command_complete()
3710 if ((dep->flags & DWC3_EP_DELAY_START) && in dwc3_gadget_endpoint_command_complete()
3711 !usb_endpoint_xfer_isoc(dep->endpoint.desc)) in dwc3_gadget_endpoint_command_complete()
3714 dep->flags &= ~DWC3_EP_DELAY_START; in dwc3_gadget_endpoint_command_complete()
3720 struct dwc3 *dwc = dep->dwc; in dwc3_gadget_endpoint_stream_event()
3722 if (event->status == DEPEVT_STREAMEVT_FOUND) { in dwc3_gadget_endpoint_stream_event()
3723 dep->flags |= DWC3_EP_FIRST_STREAM_PRIMED; in dwc3_gadget_endpoint_stream_event()
3728 switch (event->parameters) { in dwc3_gadget_endpoint_stream_event()
3737 if (dep->flags & DWC3_EP_FORCE_RESTART_STREAM) { in dwc3_gadget_endpoint_stream_event()
3738 if (dep->flags & DWC3_EP_FIRST_STREAM_PRIMED) in dwc3_gadget_endpoint_stream_event()
3739 dep->flags &= ~DWC3_EP_FORCE_RESTART_STREAM; in dwc3_gadget_endpoint_stream_event()
3741 dep->flags |= DWC3_EP_FIRST_STREAM_PRIMED; in dwc3_gadget_endpoint_stream_event()
3746 if ((dep->flags & DWC3_EP_IGNORE_NEXT_NOSTREAM) || in dwc3_gadget_endpoint_stream_event()
3747 !(dep->flags & DWC3_EP_FORCE_RESTART_STREAM) || in dwc3_gadget_endpoint_stream_event()
3748 (!DWC3_MST_CAPABLE(&dwc->hwparams) && in dwc3_gadget_endpoint_stream_event()
3749 !(dep->flags & DWC3_EP_WAIT_TRANSFER_COMPLETE))) in dwc3_gadget_endpoint_stream_event()
3769 dwc3_send_gadget_generic_command(dwc, cmd, dep->number); in dwc3_gadget_endpoint_stream_event()
3771 dep->flags |= DWC3_EP_DELAY_START; in dwc3_gadget_endpoint_stream_event()
3779 dep->flags &= ~DWC3_EP_IGNORE_NEXT_NOSTREAM; in dwc3_gadget_endpoint_stream_event()
3786 u8 epnum = event->endpoint_number; in dwc3_endpoint_interrupt()
3788 dep = dwc->eps[epnum]; in dwc3_endpoint_interrupt()
3790 if (!(dep->flags & DWC3_EP_ENABLED)) { in dwc3_endpoint_interrupt()
3791 if ((epnum > 1) && !(dep->flags & DWC3_EP_TRANSFER_STARTED)) in dwc3_endpoint_interrupt()
3795 if ((event->endpoint_event != DWC3_DEPEVT_EPCMDCMPLT) && in dwc3_endpoint_interrupt()
3796 !(epnum <= 1 && event->endpoint_event == DWC3_DEPEVT_XFERCOMPLETE)) in dwc3_endpoint_interrupt()
3805 switch (event->endpoint_event) { in dwc3_endpoint_interrupt()
3824 dev_err(dwc->dev, "unknown endpoint event %d\n", event->endpoint_event); in dwc3_endpoint_interrupt()
3831 if (dwc->async_callbacks && dwc->gadget_driver->disconnect) { in dwc3_disconnect_gadget()
3832 spin_unlock(&dwc->lock); in dwc3_disconnect_gadget()
3833 dwc->gadget_driver->disconnect(dwc->gadget); in dwc3_disconnect_gadget()
3834 spin_lock(&dwc->lock); in dwc3_disconnect_gadget()
3840 if (dwc->async_callbacks && dwc->gadget_driver->suspend) { in dwc3_suspend_gadget()
3841 spin_unlock(&dwc->lock); in dwc3_suspend_gadget()
3842 dwc->gadget_driver->suspend(dwc->gadget); in dwc3_suspend_gadget()
3843 spin_lock(&dwc->lock); in dwc3_suspend_gadget()
3849 if (dwc->async_callbacks && dwc->gadget_driver->resume) { in dwc3_resume_gadget()
3850 spin_unlock(&dwc->lock); in dwc3_resume_gadget()
3851 dwc->gadget_driver->resume(dwc->gadget); in dwc3_resume_gadget()
3852 spin_lock(&dwc->lock); in dwc3_resume_gadget()
3858 if (!dwc->gadget_driver) in dwc3_reset_gadget()
3861 if (dwc->async_callbacks && dwc->gadget->speed != USB_SPEED_UNKNOWN) { in dwc3_reset_gadget()
3862 spin_unlock(&dwc->lock); in dwc3_reset_gadget()
3863 usb_gadget_udc_reset(dwc->gadget, dwc->gadget_driver); in dwc3_reset_gadget()
3864 spin_lock(&dwc->lock); in dwc3_reset_gadget()
3871 struct dwc3 *dwc = dep->dwc; in dwc3_stop_active_transfer()
3879 if (dep->number <= 1 && dwc->ep0state != EP0_DATA_PHASE) in dwc3_stop_active_transfer()
3882 if (interrupt && (dep->flags & DWC3_EP_DELAY_STOP)) in dwc3_stop_active_transfer()
3885 if (!(dep->flags & DWC3_EP_TRANSFER_STARTED) || in dwc3_stop_active_transfer()
3886 (dep->flags & DWC3_EP_END_TRANSFER_PENDING)) in dwc3_stop_active_transfer()
3896 if (dwc->ep0state != EP0_SETUP_PHASE && !dwc->delayed_status) { in dwc3_stop_active_transfer()
3897 dep->flags |= DWC3_EP_DELAY_STOP; in dwc3_stop_active_transfer()
3943 dep = dwc->eps[epnum]; in dwc3_clear_stall_all_ep()
3947 if (!(dep->flags & DWC3_EP_STALL)) in dwc3_clear_stall_all_ep()
3950 dep->flags &= ~DWC3_EP_STALL; in dwc3_clear_stall_all_ep()
3961 dwc->suspended = false; in dwc3_gadget_disconnect_interrupt()
3965 reg = dwc3_readl(dwc->regs, DWC3_DCTL); in dwc3_gadget_disconnect_interrupt()
3970 dwc->connected = false; in dwc3_gadget_disconnect_interrupt()
3974 dwc->gadget->speed = USB_SPEED_UNKNOWN; in dwc3_gadget_disconnect_interrupt()
3975 dwc->setup_packet_pending = false; in dwc3_gadget_disconnect_interrupt()
3976 dwc->gadget->wakeup_armed = false; in dwc3_gadget_disconnect_interrupt()
3978 usb_gadget_set_state(dwc->gadget, USB_STATE_NOTATTACHED); in dwc3_gadget_disconnect_interrupt()
3985 * interrupt to set dwc->connected to FALSE. in dwc3_gadget_disconnect_interrupt()
3987 pm_request_idle(dwc->dev); in dwc3_gadget_disconnect_interrupt()
3994 dwc->suspended = false; in dwc3_gadget_reset_interrupt()
4003 dwc->connected = false; in dwc3_gadget_reset_interrupt()
4032 if (dwc->setup_packet_pending) in dwc3_gadget_reset_interrupt()
4046 * In the Synopsis DesignWare Cores USB3 Databook Rev. 3.30a in dwc3_gadget_reset_interrupt()
4047 * Section 4.1.2 Table 4-2, it states that during a USB reset, the SW in dwc3_gadget_reset_interrupt()
4052 dwc->connected = true; in dwc3_gadget_reset_interrupt()
4054 reg = dwc3_readl(dwc->regs, DWC3_DCTL); in dwc3_gadget_reset_interrupt()
4057 dwc->test_mode = false; in dwc3_gadget_reset_interrupt()
4058 dwc->gadget->wakeup_armed = false; in dwc3_gadget_reset_interrupt()
4063 reg = dwc3_readl(dwc->regs, DWC3_DCFG); in dwc3_gadget_reset_interrupt()
4065 dwc3_writel(dwc->regs, DWC3_DCFG, reg); in dwc3_gadget_reset_interrupt()
4076 if (!dwc->softconnect) in dwc3_gadget_conndone_interrupt()
4079 reg = dwc3_readl(dwc->regs, DWC3_DSTS); in dwc3_gadget_conndone_interrupt()
4081 dwc->speed = speed; in dwc3_gadget_conndone_interrupt()
4086 dwc->gadget->ssp_rate = USB_SSP_GEN_UNKNOWN; in dwc3_gadget_conndone_interrupt()
4100 dwc->gadget->ep0->maxpacket = 512; in dwc3_gadget_conndone_interrupt()
4101 dwc->gadget->speed = USB_SPEED_SUPER_PLUS; in dwc3_gadget_conndone_interrupt()
4104 dwc->gadget->ssp_rate = USB_SSP_GEN_2x2; in dwc3_gadget_conndone_interrupt()
4106 dwc->gadget->ssp_rate = USB_SSP_GEN_2x1; in dwc3_gadget_conndone_interrupt()
4111 * would cause a missing USB3 Reset event. in dwc3_gadget_conndone_interrupt()
4113 * In such situations, we should force a USB3 Reset in dwc3_gadget_conndone_interrupt()
4119 * STAR#9000483510: RTL: SS : USB3 reset event may in dwc3_gadget_conndone_interrupt()
4126 dwc->gadget->ep0->maxpacket = 512; in dwc3_gadget_conndone_interrupt()
4127 dwc->gadget->speed = USB_SPEED_SUPER; in dwc3_gadget_conndone_interrupt()
4130 dwc->gadget->speed = USB_SPEED_SUPER_PLUS; in dwc3_gadget_conndone_interrupt()
4131 dwc->gadget->ssp_rate = USB_SSP_GEN_1x2; in dwc3_gadget_conndone_interrupt()
4136 dwc->gadget->ep0->maxpacket = 64; in dwc3_gadget_conndone_interrupt()
4137 dwc->gadget->speed = USB_SPEED_HIGH; in dwc3_gadget_conndone_interrupt()
4141 dwc->gadget->ep0->maxpacket = 64; in dwc3_gadget_conndone_interrupt()
4142 dwc->gadget->speed = USB_SPEED_FULL; in dwc3_gadget_conndone_interrupt()
4146 dwc->eps[1]->endpoint.maxpacket = dwc->gadget->ep0->maxpacket; in dwc3_gadget_conndone_interrupt()
4148 /* Enable USB2 LPM Capability */ in dwc3_gadget_conndone_interrupt()
4151 !dwc->usb2_gadget_lpm_disable && in dwc3_gadget_conndone_interrupt()
4154 reg = dwc3_readl(dwc->regs, DWC3_DCFG); in dwc3_gadget_conndone_interrupt()
4156 dwc3_writel(dwc->regs, DWC3_DCFG, reg); in dwc3_gadget_conndone_interrupt()
4158 reg = dwc3_readl(dwc->regs, DWC3_DCTL); in dwc3_gadget_conndone_interrupt()
4161 reg |= DWC3_DCTL_HIRD_THRES(dwc->hird_threshold | in dwc3_gadget_conndone_interrupt()
4162 (dwc->is_utmi_l1_suspend << 4)); in dwc3_gadget_conndone_interrupt()
4165 * When dwc3 revisions >= 2.40a, LPM Erratum is enabled and in dwc3_gadget_conndone_interrupt()
4167 * BESL value in the LPM token is less than or equal to LPM in dwc3_gadget_conndone_interrupt()
4170 WARN_ONCE(DWC3_VER_IS_PRIOR(DWC3, 240A) && dwc->has_lpm_erratum, in dwc3_gadget_conndone_interrupt()
4171 "LPM Erratum not available on dwc3 revisions < 2.40a\n"); in dwc3_gadget_conndone_interrupt()
4173 if (dwc->has_lpm_erratum && !DWC3_VER_IS_PRIOR(DWC3, 240A)) in dwc3_gadget_conndone_interrupt()
4174 reg |= DWC3_DCTL_NYET_THRES(dwc->lpm_nyet_threshold); in dwc3_gadget_conndone_interrupt()
4178 if (dwc->usb2_gadget_lpm_disable) { in dwc3_gadget_conndone_interrupt()
4179 reg = dwc3_readl(dwc->regs, DWC3_DCFG); in dwc3_gadget_conndone_interrupt()
4181 dwc3_writel(dwc->regs, DWC3_DCFG, reg); in dwc3_gadget_conndone_interrupt()
4184 reg = dwc3_readl(dwc->regs, DWC3_DCTL); in dwc3_gadget_conndone_interrupt()
4189 dep = dwc->eps[0]; in dwc3_gadget_conndone_interrupt()
4192 dev_err(dwc->dev, "failed to enable %s\n", dep->name); in dwc3_gadget_conndone_interrupt()
4196 dep = dwc->eps[1]; in dwc3_gadget_conndone_interrupt()
4199 dev_err(dwc->dev, "failed to enable %s\n", dep->name); in dwc3_gadget_conndone_interrupt()
4214 dwc->suspended = false; in dwc3_gadget_wakeup_interrupt()
4221 if (dwc->async_callbacks && dwc->gadget_driver->resume) { in dwc3_gadget_wakeup_interrupt()
4222 spin_unlock(&dwc->lock); in dwc3_gadget_wakeup_interrupt()
4223 dwc->gadget_driver->resume(dwc->gadget); in dwc3_gadget_wakeup_interrupt()
4224 spin_lock(&dwc->lock); in dwc3_gadget_wakeup_interrupt()
4227 dwc->link_state = evtinfo & DWC3_LINK_STATE_MASK; in dwc3_gadget_wakeup_interrupt()
4239 * host-initiated U3 exit. in dwc3_gadget_linksts_change_interrupt()
4250 * STAR#9000570034 RTL: SS Resume event generated in non-Hibernation in dwc3_gadget_linksts_change_interrupt()
4253 pwropt = DWC3_GHWPARAMS1_EN_PWROPT(dwc->hwparams.hwparams1); in dwc3_gadget_linksts_change_interrupt()
4256 if ((dwc->link_state == DWC3_LINK_STATE_U3) && in dwc3_gadget_linksts_change_interrupt()
4277 * STAR#9000446952: RTL: Device SS : if U1/U2 ->U0 takes >128us in dwc3_gadget_linksts_change_interrupt()
4285 switch (dwc->link_state) { in dwc3_gadget_linksts_change_interrupt()
4288 reg = dwc3_readl(dwc->regs, DWC3_DCTL); in dwc3_gadget_linksts_change_interrupt()
4294 if (!dwc->u1u2) in dwc3_gadget_linksts_change_interrupt()
4295 dwc->u1u2 = reg & u1u2; in dwc3_gadget_linksts_change_interrupt()
4310 if (dwc->gadget->wakeup_armed) { in dwc3_gadget_linksts_change_interrupt()
4313 dwc->suspended = false; in dwc3_gadget_linksts_change_interrupt()
4317 if (dwc->speed == USB_SPEED_SUPER) in dwc3_gadget_linksts_change_interrupt()
4332 dwc->link_state = next; in dwc3_gadget_linksts_change_interrupt()
4340 if (!dwc->suspended && next == DWC3_LINK_STATE_U3) { in dwc3_gadget_suspend_interrupt()
4341 dwc->suspended = true; in dwc3_gadget_suspend_interrupt()
4345 dwc->link_state = next; in dwc3_gadget_suspend_interrupt()
4351 switch (event->type) { in dwc3_gadget_interrupt()
4362 dwc3_gadget_wakeup_interrupt(dwc, event->event_info); in dwc3_gadget_interrupt()
4365 dev_WARN_ONCE(dwc->dev, true, "unexpected hibernation event\n"); in dwc3_gadget_interrupt()
4368 dwc3_gadget_linksts_change_interrupt(dwc, event->event_info); in dwc3_gadget_interrupt()
4373 dwc3_gadget_suspend_interrupt(dwc, event->event_info); in dwc3_gadget_interrupt()
4381 dev_WARN(dwc->dev, "UNKNOWN IRQ %d\n", event->type); in dwc3_gadget_interrupt()
4388 trace_dwc3_event(event->raw, dwc); in dwc3_process_event_entry()
4390 if (!event->type.is_devspec) in dwc3_process_event_entry()
4391 dwc3_endpoint_interrupt(dwc, &event->depevt); in dwc3_process_event_entry()
4392 else if (event->type.type == DWC3_EVENT_TYPE_DEV) in dwc3_process_event_entry()
4393 dwc3_gadget_interrupt(dwc, &event->devt); in dwc3_process_event_entry()
4395 dev_err(dwc->dev, "UNKNOWN IRQ type %d\n", event->raw); in dwc3_process_event_entry()
4400 struct dwc3 *dwc = evt->dwc; in dwc3_process_event_buf()
4404 left = evt->count; in dwc3_process_event_buf()
4406 if (!(evt->flags & DWC3_EVENT_PENDING)) in dwc3_process_event_buf()
4412 event.raw = *(u32 *) (evt->cache + evt->lpos); in dwc3_process_event_buf()
4425 evt->lpos = (evt->lpos + 4) % evt->length; in dwc3_process_event_buf()
4426 left -= 4; in dwc3_process_event_buf()
4429 evt->count = 0; in dwc3_process_event_buf()
4433 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0), in dwc3_process_event_buf()
4434 DWC3_GEVNTSIZ_SIZE(evt->length)); in dwc3_process_event_buf()
4436 if (dwc->imod_interval) { in dwc3_process_event_buf()
4437 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), DWC3_GEVNTCOUNT_EHB); in dwc3_process_event_buf()
4438 dwc3_writel(dwc->regs, DWC3_DEV_IMOD(0), dwc->imod_interval); in dwc3_process_event_buf()
4442 evt->flags &= ~DWC3_EVENT_PENDING; in dwc3_process_event_buf()
4450 struct dwc3 *dwc = evt->dwc; in dwc3_thread_interrupt()
4455 spin_lock_irqsave(&dwc->lock, flags); in dwc3_thread_interrupt()
4457 spin_unlock_irqrestore(&dwc->lock, flags); in dwc3_thread_interrupt()
4465 struct dwc3 *dwc = evt->dwc; in dwc3_check_event_buf()
4469 if (pm_runtime_suspended(dwc->dev)) { in dwc3_check_event_buf()
4470 dwc->pending_events = true; in dwc3_check_event_buf()
4476 pm_runtime_get(dwc->dev); in dwc3_check_event_buf()
4477 disable_irq_nosync(dwc->irq_gadget); in dwc3_check_event_buf()
4482 * With PCIe legacy interrupt, test shows that top-half irq handler can in dwc3_check_event_buf()
4483 * be called again after HW interrupt deassertion. Check if bottom-half in dwc3_check_event_buf()
4487 if (evt->flags & DWC3_EVENT_PENDING) in dwc3_check_event_buf()
4490 count = dwc3_readl(dwc->regs, DWC3_GEVNTCOUNT(0)); in dwc3_check_event_buf()
4495 evt->count = count; in dwc3_check_event_buf()
4496 evt->flags |= DWC3_EVENT_PENDING; in dwc3_check_event_buf()
4499 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0), in dwc3_check_event_buf()
4500 DWC3_GEVNTSIZ_INTMASK | DWC3_GEVNTSIZ_SIZE(evt->length)); in dwc3_check_event_buf()
4502 amount = min(count, evt->length - evt->lpos); in dwc3_check_event_buf()
4503 memcpy(evt->cache + evt->lpos, evt->buf + evt->lpos, amount); in dwc3_check_event_buf()
4506 memcpy(evt->cache, evt->buf, count - amount); in dwc3_check_event_buf()
4508 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), count); in dwc3_check_event_buf()
4522 struct platform_device *dwc3_pdev = to_platform_device(dwc->dev); in dwc3_gadget_get_irq()
4529 if (irq == -EPROBE_DEFER) in dwc3_gadget_get_irq()
4536 if (irq == -EPROBE_DEFER) in dwc3_gadget_get_irq()
4553 * dwc3_gadget_init - initializes gadget related registers
4570 dwc->irq_gadget = irq; in dwc3_gadget_init()
4572 dwc->ep0_trb = dma_alloc_coherent(dwc->sysdev, in dwc3_gadget_init()
4573 sizeof(*dwc->ep0_trb) * 2, in dwc3_gadget_init()
4574 &dwc->ep0_trb_addr, GFP_KERNEL); in dwc3_gadget_init()
4575 if (!dwc->ep0_trb) { in dwc3_gadget_init()
4576 dev_err(dwc->dev, "failed to allocate ep0 trb\n"); in dwc3_gadget_init()
4577 ret = -ENOMEM; in dwc3_gadget_init()
4581 dwc->setup_buf = kzalloc(DWC3_EP0_SETUP_SIZE, GFP_KERNEL); in dwc3_gadget_init()
4582 if (!dwc->setup_buf) { in dwc3_gadget_init()
4583 ret = -ENOMEM; in dwc3_gadget_init()
4587 dwc->bounce = dma_alloc_coherent(dwc->sysdev, DWC3_BOUNCE_SIZE, in dwc3_gadget_init()
4588 &dwc->bounce_addr, GFP_KERNEL); in dwc3_gadget_init()
4589 if (!dwc->bounce) { in dwc3_gadget_init()
4590 ret = -ENOMEM; in dwc3_gadget_init()
4594 init_completion(&dwc->ep0_in_setup); in dwc3_gadget_init()
4595 dwc->gadget = kzalloc(sizeof(struct usb_gadget), GFP_KERNEL); in dwc3_gadget_init()
4596 if (!dwc->gadget) { in dwc3_gadget_init()
4597 ret = -ENOMEM; in dwc3_gadget_init()
4602 usb_initialize_gadget(dwc->dev, dwc->gadget, dwc_gadget_release); in dwc3_gadget_init()
4603 dev = &dwc->gadget->dev; in dwc3_gadget_init()
4604 dev->platform_data = dwc; in dwc3_gadget_init()
4605 dwc->gadget->ops = &dwc3_gadget_ops; in dwc3_gadget_init()
4606 dwc->gadget->speed = USB_SPEED_UNKNOWN; in dwc3_gadget_init()
4607 dwc->gadget->ssp_rate = USB_SSP_GEN_UNKNOWN; in dwc3_gadget_init()
4608 dwc->gadget->sg_supported = true; in dwc3_gadget_init()
4609 dwc->gadget->name = "dwc3-gadget"; in dwc3_gadget_init()
4610 dwc->gadget->lpm_capable = !dwc->usb2_gadget_lpm_disable; in dwc3_gadget_init()
4611 dwc->gadget->wakeup_capable = true; in dwc3_gadget_init()
4627 * composite.c that we are USB 2.0 + LPM ECN. in dwc3_gadget_init()
4630 !dwc->dis_metastability_quirk) in dwc3_gadget_init()
4631 dev_info(dwc->dev, "changing max_speed on rev %08x\n", in dwc3_gadget_init()
4632 dwc->revision); in dwc3_gadget_init()
4634 dwc->gadget->max_speed = dwc->maximum_speed; in dwc3_gadget_init()
4635 dwc->gadget->max_ssp_rate = dwc->max_ssp_rate; in dwc3_gadget_init()
4642 ret = dwc3_gadget_init_endpoints(dwc, dwc->num_eps); in dwc3_gadget_init()
4646 ret = usb_add_gadget(dwc->gadget); in dwc3_gadget_init()
4648 dev_err(dwc->dev, "failed to add gadget\n"); in dwc3_gadget_init()
4652 if (DWC3_IP_IS(DWC32) && dwc->maximum_speed == USB_SPEED_SUPER_PLUS) in dwc3_gadget_init()
4653 dwc3_gadget_set_ssp_rate(dwc->gadget, dwc->max_ssp_rate); in dwc3_gadget_init()
4655 dwc3_gadget_set_speed(dwc->gadget, dwc->maximum_speed); in dwc3_gadget_init()
4658 if (dwc->sys_wakeup) in dwc3_gadget_init()
4659 device_wakeup_disable(dwc->sysdev); in dwc3_gadget_init()
4666 usb_put_gadget(dwc->gadget); in dwc3_gadget_init()
4667 dwc->gadget = NULL; in dwc3_gadget_init()
4669 dma_free_coherent(dwc->sysdev, DWC3_BOUNCE_SIZE, dwc->bounce, in dwc3_gadget_init()
4670 dwc->bounce_addr); in dwc3_gadget_init()
4673 kfree(dwc->setup_buf); in dwc3_gadget_init()
4676 dma_free_coherent(dwc->sysdev, sizeof(*dwc->ep0_trb) * 2, in dwc3_gadget_init()
4677 dwc->ep0_trb, dwc->ep0_trb_addr); in dwc3_gadget_init()
4683 /* -------------------------------------------------------------------------- */
4687 if (!dwc->gadget) in dwc3_gadget_exit()
4691 usb_del_gadget(dwc->gadget); in dwc3_gadget_exit()
4693 usb_put_gadget(dwc->gadget); in dwc3_gadget_exit()
4694 dma_free_coherent(dwc->sysdev, DWC3_BOUNCE_SIZE, dwc->bounce, in dwc3_gadget_exit()
4695 dwc->bounce_addr); in dwc3_gadget_exit()
4696 kfree(dwc->setup_buf); in dwc3_gadget_exit()
4697 dma_free_coherent(dwc->sysdev, sizeof(*dwc->ep0_trb) * 2, in dwc3_gadget_exit()
4698 dwc->ep0_trb, dwc->ep0_trb_addr); in dwc3_gadget_exit()
4710 spin_lock_irqsave(&dwc->lock, flags); in dwc3_gadget_suspend()
4711 if (dwc->gadget_driver) in dwc3_gadget_suspend()
4713 spin_unlock_irqrestore(&dwc->lock, flags); in dwc3_gadget_suspend()
4723 if (dwc->softconnect) in dwc3_gadget_suspend()
4731 if (!dwc->gadget_driver || !dwc->softconnect) in dwc3_gadget_resume()