Lines Matching +full:lpm +full:- +full:nyet +full:- +full:threshold
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()
143 * dwc3_ep_inc_trb - increment a trb index.
153 if (*index == (DWC3_TRB_NUM - 1)) in dwc3_ep_inc_trb()
158 * dwc3_ep_inc_enq - increment endpoint's enqueue pointer
163 dwc3_ep_inc_trb(&dep->trb_enqueue); in dwc3_ep_inc_enq()
167 * dwc3_ep_inc_deq - increment endpoint's dequeue pointer
172 dwc3_ep_inc_trb(&dep->trb_dequeue); in dwc3_ep_inc_deq()
178 struct dwc3 *dwc = dep->dwc; in dwc3_gadget_del_and_unmap_request()
180 list_del(&req->list); in dwc3_gadget_del_and_unmap_request()
181 req->remaining = 0; in dwc3_gadget_del_and_unmap_request()
182 req->needs_extra_trb = false; in dwc3_gadget_del_and_unmap_request()
184 if (req->request.status == -EINPROGRESS) in dwc3_gadget_del_and_unmap_request()
185 req->request.status = status; in dwc3_gadget_del_and_unmap_request()
187 if (req->trb) in dwc3_gadget_del_and_unmap_request()
188 usb_gadget_unmap_request_by_dev(dwc->sysdev, in dwc3_gadget_del_and_unmap_request()
189 &req->request, req->direction); in dwc3_gadget_del_and_unmap_request()
191 req->trb = NULL; in dwc3_gadget_del_and_unmap_request()
194 if (dep->number > 1) in dwc3_gadget_del_and_unmap_request()
195 pm_runtime_put(dwc->dev); in dwc3_gadget_del_and_unmap_request()
199 * dwc3_gadget_giveback - call struct usb_request's ->complete callback
205 * function will unmap @req and call its ->complete() callback to notify upper
211 struct dwc3 *dwc = dep->dwc; in dwc3_gadget_giveback()
214 req->status = DWC3_REQUEST_STATUS_COMPLETED; in dwc3_gadget_giveback()
216 spin_unlock(&dwc->lock); in dwc3_gadget_giveback()
217 usb_gadget_giveback_request(&dep->endpoint, &req->request); in dwc3_gadget_giveback()
218 spin_lock(&dwc->lock); in dwc3_gadget_giveback()
222 * dwc3_send_gadget_generic_command - issue a generic command for the controller
238 dwc3_writel(dwc->regs, DWC3_DGCMDPAR, param); in dwc3_send_gadget_generic_command()
239 dwc3_writel(dwc->regs, DWC3_DGCMD, cmd | DWC3_DGCMD_CMDACT); in dwc3_send_gadget_generic_command()
242 reg = dwc3_readl(dwc->regs, DWC3_DGCMD); in dwc3_send_gadget_generic_command()
246 ret = -EINVAL; in dwc3_send_gadget_generic_command()
249 } while (--timeout); in dwc3_send_gadget_generic_command()
252 ret = -ETIMEDOUT; in dwc3_send_gadget_generic_command()
253 status = -ETIMEDOUT; in dwc3_send_gadget_generic_command()
264 * dwc3_send_gadget_ep_cmd - issue an endpoint command
275 const struct usb_endpoint_descriptor *desc = dep->endpoint.desc; in dwc3_send_gadget_ep_cmd()
276 struct dwc3 *dwc = dep->dwc; in dwc3_send_gadget_ep_cmd()
282 int ret = -EINVAL; in dwc3_send_gadget_ep_cmd()
294 if (dwc->gadget->speed <= USB_SPEED_HIGH || in dwc3_send_gadget_ep_cmd()
296 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0)); in dwc3_send_gadget_ep_cmd()
308 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg); in dwc3_send_gadget_ep_cmd()
323 if (dwc->gadget->speed >= USB_SPEED_SUPER) in dwc3_send_gadget_ep_cmd()
329 dev_WARN_ONCE(dwc->dev, ret, "wakeup failed --> %d\n", in dwc3_send_gadget_ep_cmd()
342 dwc3_writel(dep->regs, DWC3_DEPCMDPAR0, params->param0); in dwc3_send_gadget_ep_cmd()
343 dwc3_writel(dep->regs, DWC3_DEPCMDPAR1, params->param1); in dwc3_send_gadget_ep_cmd()
344 dwc3_writel(dep->regs, DWC3_DEPCMDPAR2, params->param2); in dwc3_send_gadget_ep_cmd()
368 dwc3_writel(dep->regs, DWC3_DEPCMD, cmd); in dwc3_send_gadget_ep_cmd()
378 reg = dwc3_readl(dep->regs, DWC3_DEPCMD); in dwc3_send_gadget_ep_cmd()
387 dev_WARN(dwc->dev, "No resource for %s\n", in dwc3_send_gadget_ep_cmd()
388 dep->name); in dwc3_send_gadget_ep_cmd()
389 ret = -EINVAL; in dwc3_send_gadget_ep_cmd()
399 * Instead of always returning -EINVAL, let's in dwc3_send_gadget_ep_cmd()
401 * the case by returning -EAGAIN. in dwc3_send_gadget_ep_cmd()
403 ret = -EAGAIN; in dwc3_send_gadget_ep_cmd()
406 dev_WARN(dwc->dev, "UNKNOWN cmd status\n"); in dwc3_send_gadget_ep_cmd()
411 } while (--timeout); in dwc3_send_gadget_ep_cmd()
414 ret = -ETIMEDOUT; in dwc3_send_gadget_ep_cmd()
415 cmd_status = -ETIMEDOUT; in dwc3_send_gadget_ep_cmd()
423 dep->flags |= DWC3_EP_TRANSFER_STARTED; in dwc3_send_gadget_ep_cmd()
425 if (ret != -ETIMEDOUT) in dwc3_send_gadget_ep_cmd()
430 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0)); in dwc3_send_gadget_ep_cmd()
432 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg); in dwc3_send_gadget_ep_cmd()
440 struct dwc3 *dwc = dep->dwc; in dwc3_send_clear_stall_ep_cmd()
448 * some (non-compliant) hosts may not send ACK TPs for pending in dwc3_send_clear_stall_ep_cmd()
452 if (dep->direction && in dwc3_send_clear_stall_ep_cmd()
454 (dwc->gadget->speed >= USB_SPEED_SUPER)) in dwc3_send_clear_stall_ep_cmd()
465 u32 offset = (char *) trb - (char *) dep->trb_pool; in dwc3_trb_dma_offset()
467 return dep->trb_pool_dma + offset; in dwc3_trb_dma_offset()
472 struct dwc3 *dwc = dep->dwc; in dwc3_alloc_trb_pool()
474 if (dep->trb_pool) in dwc3_alloc_trb_pool()
477 dep->trb_pool = dma_alloc_coherent(dwc->sysdev, in dwc3_alloc_trb_pool()
479 &dep->trb_pool_dma, GFP_KERNEL); in dwc3_alloc_trb_pool()
480 if (!dep->trb_pool) { in dwc3_alloc_trb_pool()
481 dev_err(dep->dwc->dev, "failed to allocate trb pool for %s\n", in dwc3_alloc_trb_pool()
482 dep->name); in dwc3_alloc_trb_pool()
483 return -ENOMEM; in dwc3_alloc_trb_pool()
491 struct dwc3 *dwc = dep->dwc; in dwc3_free_trb_pool()
493 dma_free_coherent(dwc->sysdev, sizeof(struct dwc3_trb) * DWC3_TRB_NUM, in dwc3_free_trb_pool()
494 dep->trb_pool, dep->trb_pool_dma); in dwc3_free_trb_pool()
496 dep->trb_pool = NULL; in dwc3_free_trb_pool()
497 dep->trb_pool_dma = 0; in dwc3_free_trb_pool()
513 * dwc3_gadget_start_config - configure ep resources
542 * triggered only when called for EP0-out, which always happens first, and which
553 if (dep->number) in dwc3_gadget_start_config()
558 dwc = dep->dwc; in dwc3_gadget_start_config()
565 struct dwc3_ep *dep = dwc->eps[i]; in dwc3_gadget_start_config()
583 struct dwc3 *dwc = dep->dwc; in dwc3_gadget_set_ep_config()
585 comp_desc = dep->endpoint.comp_desc; in dwc3_gadget_set_ep_config()
586 desc = dep->endpoint.desc; in dwc3_gadget_set_ep_config()
594 if (dwc->gadget->speed >= USB_SPEED_SUPER) { in dwc3_gadget_set_ep_config()
595 u32 burst = dep->endpoint.maxburst; in dwc3_gadget_set_ep_config()
597 params.param0 |= DWC3_DEPCFG_BURST_SIZE(burst - 1); in dwc3_gadget_set_ep_config()
602 params.param2 |= dep->saved_state; in dwc3_gadget_set_ep_config()
607 if (dep->number <= 1 || usb_endpoint_xfer_isoc(desc)) in dwc3_gadget_set_ep_config()
614 dep->stream_capable = true; in dwc3_gadget_set_ep_config()
626 params.param1 |= DWC3_DEPCFG_EP_NUMBER(dep->number); in dwc3_gadget_set_ep_config()
632 if (dep->direction) in dwc3_gadget_set_ep_config()
633 params.param0 |= DWC3_DEPCFG_FIFO_NUMBER(dep->number >> 1); in dwc3_gadget_set_ep_config()
635 if (desc->bInterval) { in dwc3_gadget_set_ep_config()
646 bInterval_m1 = min_t(u8, desc->bInterval - 1, 13); in dwc3_gadget_set_ep_config()
649 dwc->gadget->speed == USB_SPEED_FULL) in dwc3_gadget_set_ep_config()
650 dep->interval = desc->bInterval; in dwc3_gadget_set_ep_config()
652 dep->interval = 1 << (desc->bInterval - 1); in dwc3_gadget_set_ep_config()
661 * dwc3_gadget_calc_tx_fifo_size - calculates the txfifo size value
696 * dwc3_gadget_clear_tx_fifos - Clears txfifo allocation
709 if (!dwc->do_fifo_resize) in dwc3_gadget_clear_tx_fifos()
713 dep = dwc->eps[1]; in dwc3_gadget_clear_tx_fifos()
714 size = dwc3_readl(dwc->regs, DWC3_GTXFIFOSIZ(0)); in dwc3_gadget_clear_tx_fifos()
720 dwc->last_fifo_depth = fifo_depth; in dwc3_gadget_clear_tx_fifos()
722 for (num = 3; num < min_t(int, dwc->num_eps, DWC3_ENDPOINTS_NUM); in dwc3_gadget_clear_tx_fifos()
724 dep = dwc->eps[num]; in dwc3_gadget_clear_tx_fifos()
727 dwc3_readl(dwc->regs, DWC3_GTXFIFOSIZ(num >> 1)) & in dwc3_gadget_clear_tx_fifos()
730 dwc3_writel(dwc->regs, DWC3_GTXFIFOSIZ(num >> 1), size); in dwc3_gadget_clear_tx_fifos()
731 dep->flags &= ~DWC3_EP_TXFIFO_RESIZED; in dwc3_gadget_clear_tx_fifos()
733 dwc->num_ep_resized = 0; in dwc3_gadget_clear_tx_fifos()
737 * dwc3_gadget_resize_tx_fifos - reallocate fifo spaces for current use-case
745 * on the configured size for RAM1 - which contains TxFifo -,
759 struct dwc3 *dwc = dep->dwc; in dwc3_gadget_resize_tx_fifos()
770 if (!dwc->do_fifo_resize) in dwc3_gadget_resize_tx_fifos()
774 if (!usb_endpoint_dir_in(dep->endpoint.desc) || dep->number <= 1) in dwc3_gadget_resize_tx_fifos()
778 if (dep->flags & DWC3_EP_TXFIFO_RESIZED) in dwc3_gadget_resize_tx_fifos()
781 ram1_depth = DWC3_RAM1_DEPTH(dwc->hwparams.hwparams7); in dwc3_gadget_resize_tx_fifos()
783 if ((dep->endpoint.maxburst > 1 && 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)) in dwc3_gadget_resize_tx_fifos()
788 if (dep->endpoint.maxburst > 6 && in dwc3_gadget_resize_tx_fifos()
789 (usb_endpoint_xfer_bulk(dep->endpoint.desc) || in dwc3_gadget_resize_tx_fifos()
790 usb_endpoint_xfer_isoc(dep->endpoint.desc)) && DWC3_IP_IS(DWC31)) in dwc3_gadget_resize_tx_fifos()
791 num_fifos = dwc->tx_fifo_resize_max_num; in dwc3_gadget_resize_tx_fifos()
797 num_in_ep = dwc->max_cfg_eps; in dwc3_gadget_resize_tx_fifos()
798 num_in_ep -= dwc->num_ep_resized; in dwc3_gadget_resize_tx_fifos()
802 remaining = ram1_depth - min_depth - dwc->last_fifo_depth; in dwc3_gadget_resize_tx_fifos()
809 fifo_size = (num_fifos - 1) * fifo; in dwc3_gadget_resize_tx_fifos()
817 /* Check if TXFIFOs start at non-zero addr */ in dwc3_gadget_resize_tx_fifos()
818 tmp = dwc3_readl(dwc->regs, DWC3_GTXFIFOSIZ(0)); in dwc3_gadget_resize_tx_fifos()
821 fifo_size |= (fifo_0_start + (dwc->last_fifo_depth << 16)); in dwc3_gadget_resize_tx_fifos()
823 dwc->last_fifo_depth += DWC3_GTXFIFOSIZ_TXFDEP(fifo_size); in dwc3_gadget_resize_tx_fifos()
825 dwc->last_fifo_depth += DWC31_GTXFIFOSIZ_TXFDEP(fifo_size); in dwc3_gadget_resize_tx_fifos()
828 if (dwc->last_fifo_depth >= ram1_depth) { in dwc3_gadget_resize_tx_fifos()
829 dev_err(dwc->dev, "Fifosize(%d) > RAM size(%d) %s depth:%d\n", in dwc3_gadget_resize_tx_fifos()
830 dwc->last_fifo_depth, ram1_depth, in dwc3_gadget_resize_tx_fifos()
831 dep->endpoint.name, fifo_size); in dwc3_gadget_resize_tx_fifos()
837 dwc->last_fifo_depth -= fifo_size; in dwc3_gadget_resize_tx_fifos()
838 return -ENOMEM; in dwc3_gadget_resize_tx_fifos()
841 dwc3_writel(dwc->regs, DWC3_GTXFIFOSIZ(dep->number >> 1), fifo_size); in dwc3_gadget_resize_tx_fifos()
842 dep->flags |= DWC3_EP_TXFIFO_RESIZED; in dwc3_gadget_resize_tx_fifos()
843 dwc->num_ep_resized++; in dwc3_gadget_resize_tx_fifos()
849 * __dwc3_gadget_ep_enable - initializes a hw endpoint
858 const struct usb_endpoint_descriptor *desc = dep->endpoint.desc; in __dwc3_gadget_ep_enable()
859 struct dwc3 *dwc = dep->dwc; in __dwc3_gadget_ep_enable()
864 if (!(dep->flags & DWC3_EP_ENABLED)) { in __dwc3_gadget_ep_enable()
878 if (!(dep->flags & DWC3_EP_ENABLED)) { in __dwc3_gadget_ep_enable()
882 dep->type = usb_endpoint_type(desc); in __dwc3_gadget_ep_enable()
883 dep->flags |= DWC3_EP_ENABLED; in __dwc3_gadget_ep_enable()
885 reg = dwc3_readl(dwc->regs, DWC3_DALEPENA); in __dwc3_gadget_ep_enable()
886 reg |= DWC3_DALEPENA_EP(dep->number); in __dwc3_gadget_ep_enable()
887 dwc3_writel(dwc->regs, DWC3_DALEPENA, reg); in __dwc3_gadget_ep_enable()
889 dep->trb_dequeue = 0; in __dwc3_gadget_ep_enable()
890 dep->trb_enqueue = 0; in __dwc3_gadget_ep_enable()
896 memset(dep->trb_pool, 0, in __dwc3_gadget_ep_enable()
900 trb_st_hw = &dep->trb_pool[0]; in __dwc3_gadget_ep_enable()
902 trb_link = &dep->trb_pool[DWC3_TRB_NUM - 1]; in __dwc3_gadget_ep_enable()
903 trb_link->bpl = lower_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw)); in __dwc3_gadget_ep_enable()
904 trb_link->bph = upper_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw)); in __dwc3_gadget_ep_enable()
905 trb_link->ctrl |= DWC3_TRBCTL_LINK_TRB; in __dwc3_gadget_ep_enable()
906 trb_link->ctrl |= DWC3_TRB_CTRL_HWO; in __dwc3_gadget_ep_enable()
910 * Issue StartTransfer here with no-op TRB so we can always rely on No in __dwc3_gadget_ep_enable()
921 trb = &dep->trb_pool[0]; in __dwc3_gadget_ep_enable()
933 if (dep->stream_capable) { in __dwc3_gadget_ep_enable()
940 * no-op TRB as normal, but end it immediately. As a in __dwc3_gadget_ep_enable()
958 if (!dep->direction || in __dwc3_gadget_ep_enable()
959 !(dwc->hwparams.hwparams9 & in __dwc3_gadget_ep_enable()
961 dep->flags |= DWC3_EP_FORCE_RESTART_STREAM; in __dwc3_gadget_ep_enable()
978 if (dep->flags & DWC3_EP_DELAY_STOP) in dwc3_remove_requests()
981 /* - giveback all requests to gadget driver */ in dwc3_remove_requests()
982 while (!list_empty(&dep->started_list)) { in dwc3_remove_requests()
983 req = next_request(&dep->started_list); in dwc3_remove_requests()
988 while (!list_empty(&dep->pending_list)) { in dwc3_remove_requests()
989 req = next_request(&dep->pending_list); in dwc3_remove_requests()
994 while (!list_empty(&dep->cancelled_list)) { in dwc3_remove_requests()
995 req = next_request(&dep->cancelled_list); in dwc3_remove_requests()
1002 * __dwc3_gadget_ep_disable - disables a hw endpoint
1013 struct dwc3 *dwc = dep->dwc; in __dwc3_gadget_ep_disable()
1020 if (dep->flags & DWC3_EP_STALL) in __dwc3_gadget_ep_disable()
1023 reg = dwc3_readl(dwc->regs, DWC3_DALEPENA); in __dwc3_gadget_ep_disable()
1024 reg &= ~DWC3_DALEPENA_EP(dep->number); in __dwc3_gadget_ep_disable()
1025 dwc3_writel(dwc->regs, DWC3_DALEPENA, reg); in __dwc3_gadget_ep_disable()
1027 dwc3_remove_requests(dwc, dep, -ESHUTDOWN); in __dwc3_gadget_ep_disable()
1029 dep->stream_capable = false; in __dwc3_gadget_ep_disable()
1030 dep->type = 0; in __dwc3_gadget_ep_disable()
1037 if (dep->flags & DWC3_EP_DELAY_STOP) in __dwc3_gadget_ep_disable()
1039 dep->flags &= mask; in __dwc3_gadget_ep_disable()
1041 /* Clear out the ep descriptors for non-ep0 */ in __dwc3_gadget_ep_disable()
1042 if (dep->number > 1) { in __dwc3_gadget_ep_disable()
1043 dep->endpoint.comp_desc = NULL; in __dwc3_gadget_ep_disable()
1044 dep->endpoint.desc = NULL; in __dwc3_gadget_ep_disable()
1050 /* -------------------------------------------------------------------------- */
1055 return -EINVAL; in dwc3_gadget_ep0_enable()
1060 return -EINVAL; in dwc3_gadget_ep0_disable()
1063 /* -------------------------------------------------------------------------- */
1073 if (!ep || !desc || desc->bDescriptorType != USB_DT_ENDPOINT) { in dwc3_gadget_ep_enable()
1075 return -EINVAL; in dwc3_gadget_ep_enable()
1078 if (!desc->wMaxPacketSize) { in dwc3_gadget_ep_enable()
1080 return -EINVAL; in dwc3_gadget_ep_enable()
1084 dwc = dep->dwc; in dwc3_gadget_ep_enable()
1086 if (dev_WARN_ONCE(dwc->dev, dep->flags & DWC3_EP_ENABLED, in dwc3_gadget_ep_enable()
1088 dep->name)) in dwc3_gadget_ep_enable()
1091 spin_lock_irqsave(&dwc->lock, flags); in dwc3_gadget_ep_enable()
1093 spin_unlock_irqrestore(&dwc->lock, flags); in dwc3_gadget_ep_enable()
1107 return -EINVAL; in dwc3_gadget_ep_disable()
1111 dwc = dep->dwc; in dwc3_gadget_ep_disable()
1113 if (dev_WARN_ONCE(dwc->dev, !(dep->flags & DWC3_EP_ENABLED), in dwc3_gadget_ep_disable()
1115 dep->name)) in dwc3_gadget_ep_disable()
1118 spin_lock_irqsave(&dwc->lock, flags); in dwc3_gadget_ep_disable()
1120 spin_unlock_irqrestore(&dwc->lock, flags); in dwc3_gadget_ep_disable()
1135 req->direction = dep->direction; in dwc3_gadget_ep_alloc_request()
1136 req->epnum = dep->number; in dwc3_gadget_ep_alloc_request()
1137 req->dep = dep; in dwc3_gadget_ep_alloc_request()
1138 req->status = DWC3_REQUEST_STATUS_UNKNOWN; in dwc3_gadget_ep_alloc_request()
1142 return &req->request; in dwc3_gadget_ep_alloc_request()
1155 * dwc3_ep_prev_trb - returns the previous TRB in the ring
1168 tmp = DWC3_TRB_NUM - 1; in dwc3_ep_prev_trb()
1170 return &dep->trb_pool[tmp - 1]; in dwc3_ep_prev_trb()
1179 * or empty. It's considered full when there are DWC3_TRB_NUM-1 of TRBs in dwc3_calc_trbs_left()
1182 if (dep->trb_enqueue == dep->trb_dequeue) { in dwc3_calc_trbs_left()
1187 if (!list_empty(&dep->started_list)) 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()
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->request.num_mapped_sgs in dwc3_prepare_trbs_sg()
1421 - req->num_queued_sgs; 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, req->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()
1482 req->start_sg = sg_next(s); in dwc3_prepare_trbs_sg()
1484 req->num_queued_sgs++; in dwc3_prepare_trbs_sg()
1485 req->num_pending_sgs--; in dwc3_prepare_trbs_sg()
1493 req->num_pending_sgs = 0; in dwc3_prepare_trbs_sg()
1501 return req->num_trbs - num_trbs; in dwc3_prepare_trbs_sg()
1507 return dwc3_prepare_last_sg(dep, req, req->request.length, 0); in dwc3_prepare_trbs_linear()
1511 * dwc3_prepare_trbs - setup TRBs from requests
1537 list_for_each_entry(req, &dep->started_list, list) { in dwc3_prepare_trbs()
1538 if (req->num_pending_sgs > 0) { in dwc3_prepare_trbs()
1540 if (!ret || req->num_pending_sgs) in dwc3_prepare_trbs()
1552 if (dep->stream_capable && req->request.is_last && in dwc3_prepare_trbs()
1553 !DWC3_MST_CAPABLE(&dep->dwc->hwparams)) in dwc3_prepare_trbs()
1557 list_for_each_entry_safe(req, n, &dep->pending_list, list) { in dwc3_prepare_trbs()
1558 struct dwc3 *dwc = dep->dwc; in dwc3_prepare_trbs()
1560 ret = usb_gadget_map_request_by_dev(dwc->sysdev, &req->request, in dwc3_prepare_trbs()
1561 dep->direction); in dwc3_prepare_trbs()
1565 req->sg = req->request.sg; in dwc3_prepare_trbs()
1566 req->start_sg = req->sg; in dwc3_prepare_trbs()
1567 req->num_queued_sgs = 0; in dwc3_prepare_trbs()
1568 req->num_pending_sgs = req->request.num_mapped_sgs; in dwc3_prepare_trbs()
1570 if (req->num_pending_sgs > 0) { in dwc3_prepare_trbs()
1572 if (req->num_pending_sgs) in dwc3_prepare_trbs()
1586 if (dep->stream_capable && req->request.is_last && in dwc3_prepare_trbs()
1587 !DWC3_MST_CAPABLE(&dwc->hwparams)) in dwc3_prepare_trbs()
1613 starting = !(dep->flags & DWC3_EP_TRANSFER_STARTED); in __dwc3_gadget_kick_transfer()
1622 req = next_request(&dep->started_list); in __dwc3_gadget_kick_transfer()
1624 dep->flags |= DWC3_EP_PENDING_REQUEST; in __dwc3_gadget_kick_transfer()
1631 params.param0 = upper_32_bits(req->trb_dma); in __dwc3_gadget_kick_transfer()
1632 params.param1 = lower_32_bits(req->trb_dma); in __dwc3_gadget_kick_transfer()
1635 if (dep->stream_capable) in __dwc3_gadget_kick_transfer()
1636 cmd |= DWC3_DEPCMD_PARAM(req->request.stream_id); in __dwc3_gadget_kick_transfer()
1638 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) in __dwc3_gadget_kick_transfer()
1639 cmd |= DWC3_DEPCMD_PARAM(dep->frame_number); in __dwc3_gadget_kick_transfer()
1642 DWC3_DEPCMD_PARAM(dep->resource_index); in __dwc3_gadget_kick_transfer()
1649 if (ret == -EAGAIN) in __dwc3_gadget_kick_transfer()
1654 list_for_each_entry_safe(req, tmp, &dep->started_list, list) in __dwc3_gadget_kick_transfer()
1658 if (!(dep->flags & DWC3_EP_END_TRANSFER_PENDING)) in __dwc3_gadget_kick_transfer()
1664 if (dep->stream_capable && req->request.is_last && in __dwc3_gadget_kick_transfer()
1665 !DWC3_MST_CAPABLE(&dep->dwc->hwparams)) in __dwc3_gadget_kick_transfer()
1666 dep->flags |= DWC3_EP_WAIT_TRANSFER_COMPLETE; in __dwc3_gadget_kick_transfer()
1675 reg = dwc3_readl(dwc->regs, DWC3_DSTS); in __dwc3_gadget_get_frame()
1680 * __dwc3_stop_active_transfer - stop the current active transfer
1699 cmd |= DWC3_DEPCMD_PARAM(dep->resource_index); in __dwc3_stop_active_transfer()
1708 if (ret == -ETIMEDOUT && dep->dwc->ep0state != EP0_SETUP_PHASE) { in __dwc3_stop_active_transfer()
1709 dep->flags |= DWC3_EP_DELAY_STOP; in __dwc3_stop_active_transfer()
1713 dep->resource_index = 0; in __dwc3_stop_active_transfer()
1716 dep->flags &= ~DWC3_EP_TRANSFER_STARTED; in __dwc3_stop_active_transfer()
1718 dep->flags |= DWC3_EP_END_TRANSFER_PENDING; in __dwc3_stop_active_transfer()
1724 * dwc3_gadget_start_isoc_quirk - workaround invalid frame number
1727 * This function tests for the correct combination of BIT[15:14] from the 16-bit
1731 * In DWC_usb31 version 1.70a-ea06 and prior, for highspeed and fullspeed
1732 * isochronous IN, BIT[15:14] of the 16-bit microframe number reported by the
1736 * internal 16-bit microframe, the START TRANSFER command will pass and the
1739 * other conditions, the START TRANSFER command will fail with bus-expiry.
1744 * (or 2 seconds). 4 seconds into the future will result in a bus-expiry status.
1747 * command status will result in a 2-second delay start. The smaller BIT[15:14]
1771 while (dep->combo_num < 2) { in dwc3_gadget_start_isoc_quirk()
1778 * 4 uframes in the future with BIT[15:14] as dep->combo_num in dwc3_gadget_start_isoc_quirk()
1780 test_frame_number = dep->frame_number & DWC3_FRNUMBER_MASK; in dwc3_gadget_start_isoc_quirk()
1781 test_frame_number |= dep->combo_num << 14; in dwc3_gadget_start_isoc_quirk()
1782 test_frame_number += max_t(u32, 4, dep->interval); in dwc3_gadget_start_isoc_quirk()
1784 params.param0 = upper_32_bits(dep->dwc->bounce_addr); in dwc3_gadget_start_isoc_quirk()
1785 params.param1 = lower_32_bits(dep->dwc->bounce_addr); in dwc3_gadget_start_isoc_quirk()
1791 /* Redo if some other failure beside bus-expiry is received */ in dwc3_gadget_start_isoc_quirk()
1792 if (cmd_status && cmd_status != -EAGAIN) { in dwc3_gadget_start_isoc_quirk()
1793 dep->start_cmd_status = 0; in dwc3_gadget_start_isoc_quirk()
1794 dep->combo_num = 0; in dwc3_gadget_start_isoc_quirk()
1799 if (dep->combo_num == 0) in dwc3_gadget_start_isoc_quirk()
1800 dep->start_cmd_status = cmd_status; in dwc3_gadget_start_isoc_quirk()
1802 dep->combo_num++; in dwc3_gadget_start_isoc_quirk()
1815 test0 = (dep->start_cmd_status == 0); in dwc3_gadget_start_isoc_quirk()
1819 dep->combo_num = 1; in dwc3_gadget_start_isoc_quirk()
1821 dep->combo_num = 2; in dwc3_gadget_start_isoc_quirk()
1823 dep->combo_num = 3; in dwc3_gadget_start_isoc_quirk()
1825 dep->combo_num = 0; in dwc3_gadget_start_isoc_quirk()
1827 dep->frame_number &= DWC3_FRNUMBER_MASK; in dwc3_gadget_start_isoc_quirk()
1828 dep->frame_number |= dep->combo_num << 14; in dwc3_gadget_start_isoc_quirk()
1829 dep->frame_number += max_t(u32, 4, dep->interval); in dwc3_gadget_start_isoc_quirk()
1832 dep->start_cmd_status = 0; in dwc3_gadget_start_isoc_quirk()
1833 dep->combo_num = 0; in dwc3_gadget_start_isoc_quirk()
1840 const struct usb_endpoint_descriptor *desc = dep->endpoint.desc; in __dwc3_gadget_start_isoc()
1841 struct dwc3 *dwc = dep->dwc; in __dwc3_gadget_start_isoc()
1845 if (list_empty(&dep->pending_list) && in __dwc3_gadget_start_isoc()
1846 list_empty(&dep->started_list)) { in __dwc3_gadget_start_isoc()
1847 dep->flags |= DWC3_EP_PENDING_REQUEST; in __dwc3_gadget_start_isoc()
1848 return -EAGAIN; in __dwc3_gadget_start_isoc()
1851 if (!dwc->dis_start_transfer_quirk && in __dwc3_gadget_start_isoc()
1854 if (dwc->gadget->speed <= USB_SPEED_HIGH && dep->direction) in __dwc3_gadget_start_isoc()
1858 if (desc->bInterval <= 14 && in __dwc3_gadget_start_isoc()
1859 dwc->gadget->speed >= USB_SPEED_HIGH) { in __dwc3_gadget_start_isoc()
1862 (dep->frame_number & DWC3_FRNUMBER_MASK); in __dwc3_gadget_start_isoc()
1873 dep->frame_number = (dep->frame_number & ~DWC3_FRNUMBER_MASK) | in __dwc3_gadget_start_isoc()
1876 dep->frame_number += BIT(14); in __dwc3_gadget_start_isoc()
1883 if (desc->bInterval < 3) in __dwc3_gadget_start_isoc()
1884 future_interval += 3 - desc->bInterval; in __dwc3_gadget_start_isoc()
1886 dep->frame_number = DWC3_ALIGN_FRAME(dep, future_interval); in __dwc3_gadget_start_isoc()
1889 if (ret != -EAGAIN) in __dwc3_gadget_start_isoc()
1894 * After a number of unsuccessful start attempts due to bus-expiry in __dwc3_gadget_start_isoc()
1898 if (ret == -EAGAIN) in __dwc3_gadget_start_isoc()
1906 struct dwc3 *dwc = dep->dwc; in __dwc3_gadget_ep_queue()
1908 if (!dep->endpoint.desc || !dwc->pullups_connected || !dwc->connected) { in __dwc3_gadget_ep_queue()
1909 dev_dbg(dwc->dev, "%s: can't queue to disabled endpoint\n", in __dwc3_gadget_ep_queue()
1910 dep->name); in __dwc3_gadget_ep_queue()
1911 return -ESHUTDOWN; in __dwc3_gadget_ep_queue()
1914 if (WARN(req->dep != dep, "request %pK belongs to '%s'\n", in __dwc3_gadget_ep_queue()
1915 &req->request, req->dep->name)) in __dwc3_gadget_ep_queue()
1916 return -EINVAL; in __dwc3_gadget_ep_queue()
1918 if (WARN(req->status < DWC3_REQUEST_STATUS_COMPLETED, in __dwc3_gadget_ep_queue()
1920 dep->name, &req->request)) in __dwc3_gadget_ep_queue()
1921 return -EINVAL; in __dwc3_gadget_ep_queue()
1923 pm_runtime_get(dwc->dev); in __dwc3_gadget_ep_queue()
1925 req->request.actual = 0; in __dwc3_gadget_ep_queue()
1926 req->request.status = -EINPROGRESS; in __dwc3_gadget_ep_queue()
1930 list_add_tail(&req->list, &dep->pending_list); in __dwc3_gadget_ep_queue()
1931 req->status = DWC3_REQUEST_STATUS_QUEUED; in __dwc3_gadget_ep_queue()
1933 if (dep->flags & DWC3_EP_WAIT_TRANSFER_COMPLETE) in __dwc3_gadget_ep_queue()
1940 if ((dep->flags & DWC3_EP_END_TRANSFER_PENDING) || in __dwc3_gadget_ep_queue()
1941 (dep->flags & DWC3_EP_WEDGE) || in __dwc3_gadget_ep_queue()
1942 (dep->flags & DWC3_EP_DELAY_STOP) || in __dwc3_gadget_ep_queue()
1943 (dep->flags & DWC3_EP_STALL)) { in __dwc3_gadget_ep_queue()
1944 dep->flags |= DWC3_EP_DELAY_START; in __dwc3_gadget_ep_queue()
1951 * (micro-)frame number. in __dwc3_gadget_ep_queue()
1956 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) { in __dwc3_gadget_ep_queue()
1957 if (!(dep->flags & DWC3_EP_TRANSFER_STARTED)) { in __dwc3_gadget_ep_queue()
1958 if ((dep->flags & DWC3_EP_PENDING_REQUEST)) in __dwc3_gadget_ep_queue()
1975 struct dwc3 *dwc = dep->dwc; in dwc3_gadget_ep_queue()
1981 spin_lock_irqsave(&dwc->lock, flags); in dwc3_gadget_ep_queue()
1983 spin_unlock_irqrestore(&dwc->lock, flags); in dwc3_gadget_ep_queue()
1992 /* If req->trb is not set, then the request has not started */ in dwc3_gadget_ep_skip_trbs()
1993 if (!req->trb) in dwc3_gadget_ep_skip_trbs()
2006 for (i = 0; i < req->num_trbs; i++) { in dwc3_gadget_ep_skip_trbs()
2009 trb = &dep->trb_pool[dep->trb_dequeue]; in dwc3_gadget_ep_skip_trbs()
2010 trb->ctrl &= ~DWC3_TRB_CTRL_HWO; in dwc3_gadget_ep_skip_trbs()
2014 req->num_trbs = 0; in dwc3_gadget_ep_skip_trbs()
2020 struct dwc3 *dwc = dep->dwc; in dwc3_gadget_ep_cleanup_cancelled_requests()
2022 while (!list_empty(&dep->cancelled_list)) { in dwc3_gadget_ep_cleanup_cancelled_requests()
2023 req = next_request(&dep->cancelled_list); in dwc3_gadget_ep_cleanup_cancelled_requests()
2025 switch (req->status) { in dwc3_gadget_ep_cleanup_cancelled_requests()
2027 dwc3_gadget_giveback(dep, req, -ESHUTDOWN); in dwc3_gadget_ep_cleanup_cancelled_requests()
2030 dwc3_gadget_giveback(dep, req, -ECONNRESET); in dwc3_gadget_ep_cleanup_cancelled_requests()
2033 dwc3_gadget_giveback(dep, req, -EPIPE); in dwc3_gadget_ep_cleanup_cancelled_requests()
2036 dev_err(dwc->dev, "request cancelled with wrong reason:%d\n", req->status); in dwc3_gadget_ep_cleanup_cancelled_requests()
2037 dwc3_gadget_giveback(dep, req, -ECONNRESET); in dwc3_gadget_ep_cleanup_cancelled_requests()
2044 if (!dep->endpoint.desc) in dwc3_gadget_ep_cleanup_cancelled_requests()
2056 struct dwc3 *dwc = dep->dwc; in dwc3_gadget_ep_dequeue()
2063 spin_lock_irqsave(&dwc->lock, flags); in dwc3_gadget_ep_dequeue()
2065 list_for_each_entry(r, &dep->cancelled_list, list) { in dwc3_gadget_ep_dequeue()
2070 list_for_each_entry(r, &dep->pending_list, list) { in dwc3_gadget_ep_dequeue()
2072 dwc3_gadget_giveback(dep, req, -ECONNRESET); in dwc3_gadget_ep_dequeue()
2077 list_for_each_entry(r, &dep->started_list, list) { in dwc3_gadget_ep_dequeue()
2088 list_for_each_entry_safe(r, t, &dep->started_list, list) in dwc3_gadget_ep_dequeue()
2092 dep->flags &= ~DWC3_EP_WAIT_TRANSFER_COMPLETE; in dwc3_gadget_ep_dequeue()
2098 dev_err(dwc->dev, "request %pK was not queued to %s\n", in dwc3_gadget_ep_dequeue()
2099 request, ep->name); in dwc3_gadget_ep_dequeue()
2100 ret = -EINVAL; in dwc3_gadget_ep_dequeue()
2102 spin_unlock_irqrestore(&dwc->lock, flags); in dwc3_gadget_ep_dequeue()
2110 struct dwc3 *dwc = dep->dwc; in __dwc3_gadget_ep_set_halt()
2115 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) { in __dwc3_gadget_ep_set_halt()
2116 dev_err(dwc->dev, "%s is of Isochronous type\n", dep->name); in __dwc3_gadget_ep_set_halt()
2117 return -EINVAL; in __dwc3_gadget_ep_set_halt()
2128 if (dep->number > 1) in __dwc3_gadget_ep_set_halt()
2129 trb = dwc3_ep_prev_trb(dep, dep->trb_enqueue); in __dwc3_gadget_ep_set_halt()
2131 trb = &dwc->ep0_trb[dep->trb_enqueue]; in __dwc3_gadget_ep_set_halt()
2133 transfer_in_flight = trb->ctrl & DWC3_TRB_CTRL_HWO; in __dwc3_gadget_ep_set_halt()
2134 started = !list_empty(&dep->started_list); in __dwc3_gadget_ep_set_halt()
2136 if (!protocol && ((dep->direction && transfer_in_flight) || in __dwc3_gadget_ep_set_halt()
2137 (!dep->direction && started))) { in __dwc3_gadget_ep_set_halt()
2138 return -EAGAIN; in __dwc3_gadget_ep_set_halt()
2144 dev_err(dwc->dev, "failed to set STALL on %s\n", in __dwc3_gadget_ep_set_halt()
2145 dep->name); in __dwc3_gadget_ep_set_halt()
2147 dep->flags |= DWC3_EP_STALL; in __dwc3_gadget_ep_set_halt()
2154 if (dep->number <= 1) { in __dwc3_gadget_ep_set_halt()
2155 dep->flags &= ~(DWC3_EP_STALL | DWC3_EP_WEDGE); in __dwc3_gadget_ep_set_halt()
2161 list_for_each_entry_safe(req, tmp, &dep->started_list, list) in __dwc3_gadget_ep_set_halt()
2164 if (dep->flags & DWC3_EP_END_TRANSFER_PENDING || in __dwc3_gadget_ep_set_halt()
2165 (dep->flags & DWC3_EP_DELAY_STOP)) { in __dwc3_gadget_ep_set_halt()
2166 dep->flags |= DWC3_EP_PENDING_CLEAR_STALL; in __dwc3_gadget_ep_set_halt()
2168 dwc->clear_stall_protocol = dep->number; in __dwc3_gadget_ep_set_halt()
2177 dev_err(dwc->dev, "failed to clear STALL on %s\n", in __dwc3_gadget_ep_set_halt()
2178 dep->name); in __dwc3_gadget_ep_set_halt()
2182 dep->flags &= ~(DWC3_EP_STALL | DWC3_EP_WEDGE); in __dwc3_gadget_ep_set_halt()
2184 if ((dep->flags & DWC3_EP_DELAY_START) && in __dwc3_gadget_ep_set_halt()
2185 !usb_endpoint_xfer_isoc(dep->endpoint.desc)) in __dwc3_gadget_ep_set_halt()
2188 dep->flags &= ~DWC3_EP_DELAY_START; in __dwc3_gadget_ep_set_halt()
2197 struct dwc3 *dwc = dep->dwc; in dwc3_gadget_ep_set_halt()
2203 spin_lock_irqsave(&dwc->lock, flags); in dwc3_gadget_ep_set_halt()
2205 spin_unlock_irqrestore(&dwc->lock, flags); in dwc3_gadget_ep_set_halt()
2213 struct dwc3 *dwc = dep->dwc; in dwc3_gadget_ep_set_wedge()
2217 spin_lock_irqsave(&dwc->lock, flags); in dwc3_gadget_ep_set_wedge()
2218 dep->flags |= DWC3_EP_WEDGE; in dwc3_gadget_ep_set_wedge()
2220 if (dep->number == 0 || dep->number == 1) in dwc3_gadget_ep_set_wedge()
2224 spin_unlock_irqrestore(&dwc->lock, flags); in dwc3_gadget_ep_set_wedge()
2229 /* -------------------------------------------------------------------------- */
2259 /* -------------------------------------------------------------------------- */
2283 reg = dwc3_readl(dwc->regs, DWC3_DSTS); in __dwc3_gadget_wakeup()
2296 return -EINVAL; in __dwc3_gadget_wakeup()
2301 dev_err(dwc->dev, "failed to put link in Recovery\n"); in __dwc3_gadget_wakeup()
2308 reg = dwc3_readl(dwc->regs, DWC3_DCTL); in __dwc3_gadget_wakeup()
2310 dwc3_writel(dwc->regs, DWC3_DCTL, reg); in __dwc3_gadget_wakeup()
2316 while (retries--) { in __dwc3_gadget_wakeup()
2317 reg = dwc3_readl(dwc->regs, DWC3_DSTS); in __dwc3_gadget_wakeup()
2325 dev_err(dwc->dev, "failed to send remote wakeup\n"); in __dwc3_gadget_wakeup()
2326 return -EINVAL; in __dwc3_gadget_wakeup()
2338 spin_lock_irqsave(&dwc->lock, flags); in dwc3_gadget_wakeup()
2340 spin_unlock_irqrestore(&dwc->lock, flags); in dwc3_gadget_wakeup()
2351 spin_lock_irqsave(&dwc->lock, flags); in dwc3_gadget_set_selfpowered()
2352 g->is_selfpowered = !!is_selfpowered; in dwc3_gadget_set_selfpowered()
2353 spin_unlock_irqrestore(&dwc->lock, flags); in dwc3_gadget_set_selfpowered()
2362 for (epnum = 2; epnum < dwc->num_eps; epnum++) { in dwc3_stop_active_transfers()
2365 dep = dwc->eps[epnum]; in dwc3_stop_active_transfers()
2369 dwc3_remove_requests(dwc, dep, -ESHUTDOWN); in dwc3_stop_active_transfers()
2375 enum usb_ssp_rate ssp_rate = dwc->gadget_ssp_rate; in __dwc3_gadget_set_ssp_rate()
2379 ssp_rate = dwc->max_ssp_rate; in __dwc3_gadget_set_ssp_rate()
2381 reg = dwc3_readl(dwc->regs, DWC3_DCFG); in __dwc3_gadget_set_ssp_rate()
2387 else if (dwc->max_ssp_rate != USB_SSP_GEN_1x2) in __dwc3_gadget_set_ssp_rate()
2391 dwc->max_ssp_rate != USB_SSP_GEN_2x1) in __dwc3_gadget_set_ssp_rate()
2394 dwc3_writel(dwc->regs, DWC3_DCFG, reg); in __dwc3_gadget_set_ssp_rate()
2402 speed = dwc->gadget_max_speed; in __dwc3_gadget_set_speed()
2403 if (speed == USB_SPEED_UNKNOWN || speed > dwc->maximum_speed) in __dwc3_gadget_set_speed()
2404 speed = dwc->maximum_speed; in __dwc3_gadget_set_speed()
2412 reg = dwc3_readl(dwc->regs, DWC3_DCFG); in __dwc3_gadget_set_speed()
2418 * bit if we try to force the IP to USB2-only mode. in __dwc3_gadget_set_speed()
2429 !dwc->dis_metastability_quirk) { in __dwc3_gadget_set_speed()
2449 dev_err(dwc->dev, "invalid speed (%d)\n", speed); in __dwc3_gadget_set_speed()
2463 dwc3_writel(dwc->regs, DWC3_DCFG, reg); in __dwc3_gadget_set_speed()
2471 if (pm_runtime_suspended(dwc->dev)) in dwc3_gadget_run_stop()
2474 reg = dwc3_readl(dwc->regs, DWC3_DCTL); in dwc3_gadget_run_stop()
2485 if (dwc->has_hibernation) in dwc3_gadget_run_stop()
2489 dwc->pullups_connected = true; in dwc3_gadget_run_stop()
2493 if (dwc->has_hibernation && !suspend) in dwc3_gadget_run_stop()
2496 dwc->pullups_connected = false; in dwc3_gadget_run_stop()
2503 reg = dwc3_readl(dwc->regs, DWC3_DSTS); in dwc3_gadget_run_stop()
2505 } while (--timeout && !(!is_on ^ !reg)); in dwc3_gadget_run_stop()
2508 return -ETIMEDOUT; in dwc3_gadget_run_stop()
2521 spin_lock_irqsave(&dwc->lock, flags); in dwc3_gadget_soft_disconnect()
2522 dwc->connected = false; in dwc3_gadget_soft_disconnect()
2528 if (dwc->ep0state != EP0_SETUP_PHASE) { in dwc3_gadget_soft_disconnect()
2531 if (dwc->delayed_status) in dwc3_gadget_soft_disconnect()
2534 reinit_completion(&dwc->ep0_in_setup); in dwc3_gadget_soft_disconnect()
2536 spin_unlock_irqrestore(&dwc->lock, flags); in dwc3_gadget_soft_disconnect()
2537 ret = wait_for_completion_timeout(&dwc->ep0_in_setup, in dwc3_gadget_soft_disconnect()
2539 spin_lock_irqsave(&dwc->lock, flags); in dwc3_gadget_soft_disconnect()
2541 dev_warn(dwc->dev, "timed out waiting for SETUP phase\n"); in dwc3_gadget_soft_disconnect()
2546 * Section 4.1.8 Table 4-7, it states that for a device-initiated in dwc3_gadget_soft_disconnect()
2553 spin_unlock_irqrestore(&dwc->lock, flags); in dwc3_gadget_soft_disconnect()
2572 dwc->softconnect = is_on; in dwc3_gadget_pullup()
2580 pm_runtime_barrier(dwc->dev); in dwc3_gadget_pullup()
2581 if (pm_runtime_suspended(dwc->dev)) in dwc3_gadget_pullup()
2590 ret = pm_runtime_get_sync(dwc->dev); in dwc3_gadget_pullup()
2592 pm_runtime_put(dwc->dev); in dwc3_gadget_pullup()
2596 if (dwc->pullups_connected == is_on) { in dwc3_gadget_pullup()
2597 pm_runtime_put(dwc->dev); in dwc3_gadget_pullup()
2601 synchronize_irq(dwc->irq_gadget); in dwc3_gadget_pullup()
2609 * device-initiated disconnect requires a core soft reset in dwc3_gadget_pullup()
2619 pm_runtime_put(dwc->dev); in dwc3_gadget_pullup()
2640 /* On 2.30a and above this bit enables U3/L2-L1 Suspend Events */ in dwc3_gadget_enable_irq()
2644 dwc3_writel(dwc->regs, DWC3_DEVTEN, reg); in dwc3_gadget_enable_irq()
2650 dwc3_writel(dwc->regs, DWC3_DEVTEN, 0x00); in dwc3_gadget_disable_irq()
2657 * dwc3_gadget_setup_nump - calculate and initialize NUMP field of %DWC3_DCFG
2670 * RxFIFO Size = (RAM2_DEPTH * MDWIDTH / 8) - 24 - 16;
2684 ram2_depth = DWC3_GHWPARAMS7_RAM2_DEPTH(dwc->hwparams.hwparams7); in dwc3_gadget_setup_nump()
2687 nump = ((ram2_depth * mdwidth / 8) - 24 - 16) / 1024; in dwc3_gadget_setup_nump()
2691 reg = dwc3_readl(dwc->regs, DWC3_DCFG); in dwc3_gadget_setup_nump()
2694 dwc3_writel(dwc->regs, DWC3_DCFG, reg); in dwc3_gadget_setup_nump()
2704 * Use IMOD if enabled via dwc->imod_interval. Otherwise, if in __dwc3_gadget_start()
2707 if (dwc->imod_interval) { in __dwc3_gadget_start()
2708 dwc3_writel(dwc->regs, DWC3_DEV_IMOD(0), dwc->imod_interval); in __dwc3_gadget_start()
2709 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), DWC3_GEVNTCOUNT_EHB); in __dwc3_gadget_start()
2711 dwc3_writel(dwc->regs, DWC3_DEV_IMOD(0), 0); in __dwc3_gadget_start()
2721 reg = dwc3_readl(dwc->regs, DWC3_GRXTHRCFG); in __dwc3_gadget_start()
2727 dwc3_writel(dwc->regs, DWC3_GRXTHRCFG, reg); in __dwc3_gadget_start()
2738 reg = dwc3_readl(dwc->regs, DWC3_DCFG); in __dwc3_gadget_start()
2740 dwc3_writel(dwc->regs, DWC3_DCFG, reg); in __dwc3_gadget_start()
2743 if (DWC3_MST_CAPABLE(&dwc->hwparams)) { in __dwc3_gadget_start()
2744 reg = dwc3_readl(dwc->regs, DWC3_DCFG1); in __dwc3_gadget_start()
2746 dwc3_writel(dwc->regs, DWC3_DCFG1, reg); in __dwc3_gadget_start()
2752 dep = dwc->eps[0]; in __dwc3_gadget_start()
2753 dep->flags = 0; in __dwc3_gadget_start()
2756 dev_err(dwc->dev, "failed to enable %s\n", dep->name); in __dwc3_gadget_start()
2760 dep = dwc->eps[1]; in __dwc3_gadget_start()
2761 dep->flags = 0; in __dwc3_gadget_start()
2764 dev_err(dwc->dev, "failed to enable %s\n", dep->name); in __dwc3_gadget_start()
2769 dwc->ep0state = EP0_SETUP_PHASE; in __dwc3_gadget_start()
2770 dwc->ep0_bounced = false; in __dwc3_gadget_start()
2771 dwc->link_state = DWC3_LINK_STATE_SS_DIS; in __dwc3_gadget_start()
2772 dwc->delayed_status = false; in __dwc3_gadget_start()
2780 __dwc3_gadget_ep_disable(dwc->eps[0]); in __dwc3_gadget_start()
2794 irq = dwc->irq_gadget; in dwc3_gadget_start()
2796 IRQF_SHARED, "dwc3", dwc->ev_buf); in dwc3_gadget_start()
2798 dev_err(dwc->dev, "failed to request irq #%d --> %d\n", in dwc3_gadget_start()
2803 spin_lock_irqsave(&dwc->lock, flags); in dwc3_gadget_start()
2804 dwc->gadget_driver = driver; in dwc3_gadget_start()
2805 spin_unlock_irqrestore(&dwc->lock, flags); in dwc3_gadget_start()
2813 __dwc3_gadget_ep_disable(dwc->eps[0]); in __dwc3_gadget_stop()
2814 __dwc3_gadget_ep_disable(dwc->eps[1]); in __dwc3_gadget_stop()
2822 spin_lock_irqsave(&dwc->lock, flags); in dwc3_gadget_stop()
2823 dwc->gadget_driver = NULL; in dwc3_gadget_stop()
2824 dwc->max_cfg_eps = 0; in dwc3_gadget_stop()
2825 spin_unlock_irqrestore(&dwc->lock, flags); in dwc3_gadget_stop()
2827 free_irq(dwc->irq_gadget, dwc->ev_buf); in dwc3_gadget_stop()
2837 params->besl_baseline = USB_DEFAULT_BESL_UNSPECIFIED; in dwc3_gadget_config_params()
2838 params->besl_deep = USB_DEFAULT_BESL_UNSPECIFIED; in dwc3_gadget_config_params()
2841 if (!dwc->dis_enblslpm_quirk) { in dwc3_gadget_config_params()
2851 params->besl_baseline = 1; in dwc3_gadget_config_params()
2852 if (dwc->is_utmi_l1_suspend) in dwc3_gadget_config_params()
2853 params->besl_deep = in dwc3_gadget_config_params()
2854 clamp_t(u8, dwc->hird_threshold, 2, 15); in dwc3_gadget_config_params()
2858 if (dwc->dis_u1_entry_quirk) in dwc3_gadget_config_params()
2859 params->bU1devExitLat = 0; in dwc3_gadget_config_params()
2861 params->bU1devExitLat = DWC3_DEFAULT_U1_DEV_EXIT_LAT; in dwc3_gadget_config_params()
2864 if (dwc->dis_u2_entry_quirk) in dwc3_gadget_config_params()
2865 params->bU2DevExitLat = 0; in dwc3_gadget_config_params()
2867 params->bU2DevExitLat = in dwc3_gadget_config_params()
2877 spin_lock_irqsave(&dwc->lock, flags); in dwc3_gadget_set_speed()
2878 dwc->gadget_max_speed = speed; in dwc3_gadget_set_speed()
2879 spin_unlock_irqrestore(&dwc->lock, flags); in dwc3_gadget_set_speed()
2888 spin_lock_irqsave(&dwc->lock, flags); in dwc3_gadget_set_ssp_rate()
2889 dwc->gadget_max_speed = USB_SPEED_SUPER_PLUS; in dwc3_gadget_set_ssp_rate()
2890 dwc->gadget_ssp_rate = rate; in dwc3_gadget_set_ssp_rate()
2891 spin_unlock_irqrestore(&dwc->lock, flags); in dwc3_gadget_set_ssp_rate()
2900 if (dwc->usb2_phy) in dwc3_gadget_vbus_draw()
2901 return usb_phy_set_power(dwc->usb2_phy, mA); in dwc3_gadget_vbus_draw()
2903 if (!dwc->usb_psy) in dwc3_gadget_vbus_draw()
2904 return -EOPNOTSUPP; in dwc3_gadget_vbus_draw()
2907 ret = power_supply_set_property(dwc->usb_psy, POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT, &val); in dwc3_gadget_vbus_draw()
2913 * dwc3_gadget_check_config - ensure dwc3 can support the USB configuration
2930 if (!dwc->do_fifo_resize) in dwc3_gadget_check_config()
2933 list_for_each_entry(ep, &g->ep_list, ep_list) { in dwc3_gadget_check_config()
2935 if (ep->claimed && (ep->address & USB_DIR_IN)) in dwc3_gadget_check_config()
2939 if (ep_num <= dwc->max_cfg_eps) in dwc3_gadget_check_config()
2943 dwc->max_cfg_eps = ep_num; in dwc3_gadget_check_config()
2945 fifo_size = dwc3_gadget_calc_tx_fifo_size(dwc, dwc->max_cfg_eps); in dwc3_gadget_check_config()
2947 fifo_size += dwc->max_cfg_eps; in dwc3_gadget_check_config()
2950 ram1_depth = DWC3_RAM1_DEPTH(dwc->hwparams.hwparams7); in dwc3_gadget_check_config()
2952 return -ENOMEM; in dwc3_gadget_check_config()
2962 spin_lock_irqsave(&dwc->lock, flags); in dwc3_gadget_async_callbacks()
2963 dwc->async_callbacks = enable; in dwc3_gadget_async_callbacks()
2964 spin_unlock_irqrestore(&dwc->lock, flags); in dwc3_gadget_async_callbacks()
2982 /* -------------------------------------------------------------------------- */
2986 struct dwc3 *dwc = dep->dwc; in dwc3_gadget_init_control_endpoint()
2988 usb_ep_set_maxpacket_limit(&dep->endpoint, 512); in dwc3_gadget_init_control_endpoint()
2989 dep->endpoint.maxburst = 1; in dwc3_gadget_init_control_endpoint()
2990 dep->endpoint.ops = &dwc3_gadget_ep0_ops; in dwc3_gadget_init_control_endpoint()
2991 if (!dep->direction) in dwc3_gadget_init_control_endpoint()
2992 dwc->gadget->ep0 = &dep->endpoint; in dwc3_gadget_init_control_endpoint()
2994 dep->endpoint.caps.type_control = true; in dwc3_gadget_init_control_endpoint()
3001 struct dwc3 *dwc = dep->dwc; in dwc3_gadget_init_in_endpoint()
3011 size = dwc3_readl(dwc->regs, DWC3_GTXFIFOSIZ(dep->number >> 1)); in dwc3_gadget_init_in_endpoint()
3022 * maxpacket = mdwidth * (fifo_size - 1); in dwc3_gadget_init_in_endpoint()
3026 * maxpacket = mdwidth * ((fifo_size - 1) - 1) - mdwidth; in dwc3_gadget_init_in_endpoint()
3029 maxpacket = mdwidth * (size - 1); in dwc3_gadget_init_in_endpoint()
3031 maxpacket = mdwidth * ((size - 1) - 1) - mdwidth; in dwc3_gadget_init_in_endpoint()
3035 usb_ep_set_maxpacket_limit(&dep->endpoint, size); in dwc3_gadget_init_in_endpoint()
3037 dep->endpoint.max_streams = 16; in dwc3_gadget_init_in_endpoint()
3038 dep->endpoint.ops = &dwc3_gadget_ep_ops; in dwc3_gadget_init_in_endpoint()
3039 list_add_tail(&dep->endpoint.ep_list, in dwc3_gadget_init_in_endpoint()
3040 &dwc->gadget->ep_list); in dwc3_gadget_init_in_endpoint()
3041 dep->endpoint.caps.type_iso = true; in dwc3_gadget_init_in_endpoint()
3042 dep->endpoint.caps.type_bulk = true; in dwc3_gadget_init_in_endpoint()
3043 dep->endpoint.caps.type_int = true; in dwc3_gadget_init_in_endpoint()
3050 struct dwc3 *dwc = dep->dwc; in dwc3_gadget_init_out_endpoint()
3060 size = dwc3_readl(dwc->regs, DWC3_GRXFIFOSIZ(0)); in dwc3_gadget_init_out_endpoint()
3077 size -= (3 * 8) + 16; in dwc3_gadget_init_out_endpoint()
3083 usb_ep_set_maxpacket_limit(&dep->endpoint, size); in dwc3_gadget_init_out_endpoint()
3084 dep->endpoint.max_streams = 16; in dwc3_gadget_init_out_endpoint()
3085 dep->endpoint.ops = &dwc3_gadget_ep_ops; in dwc3_gadget_init_out_endpoint()
3086 list_add_tail(&dep->endpoint.ep_list, in dwc3_gadget_init_out_endpoint()
3087 &dwc->gadget->ep_list); in dwc3_gadget_init_out_endpoint()
3088 dep->endpoint.caps.type_iso = true; in dwc3_gadget_init_out_endpoint()
3089 dep->endpoint.caps.type_bulk = true; in dwc3_gadget_init_out_endpoint()
3090 dep->endpoint.caps.type_int = true; in dwc3_gadget_init_out_endpoint()
3104 return -ENOMEM; in dwc3_gadget_init_endpoint()
3106 dep->dwc = dwc; in dwc3_gadget_init_endpoint()
3107 dep->number = epnum; in dwc3_gadget_init_endpoint()
3108 dep->direction = direction; in dwc3_gadget_init_endpoint()
3109 dep->regs = dwc->regs + DWC3_DEP_BASE(epnum); in dwc3_gadget_init_endpoint()
3110 dwc->eps[epnum] = dep; in dwc3_gadget_init_endpoint()
3111 dep->combo_num = 0; in dwc3_gadget_init_endpoint()
3112 dep->start_cmd_status = 0; in dwc3_gadget_init_endpoint()
3114 snprintf(dep->name, sizeof(dep->name), "ep%u%s", num, in dwc3_gadget_init_endpoint()
3117 dep->endpoint.name = dep->name; in dwc3_gadget_init_endpoint()
3119 if (!(dep->number > 1)) { in dwc3_gadget_init_endpoint()
3120 dep->endpoint.desc = &dwc3_gadget_ep0_desc; in dwc3_gadget_init_endpoint()
3121 dep->endpoint.comp_desc = NULL; in dwc3_gadget_init_endpoint()
3134 dep->endpoint.caps.dir_in = direction; in dwc3_gadget_init_endpoint()
3135 dep->endpoint.caps.dir_out = !direction; in dwc3_gadget_init_endpoint()
3137 INIT_LIST_HEAD(&dep->pending_list); in dwc3_gadget_init_endpoint()
3138 INIT_LIST_HEAD(&dep->started_list); in dwc3_gadget_init_endpoint()
3139 INIT_LIST_HEAD(&dep->cancelled_list); in dwc3_gadget_init_endpoint()
3150 INIT_LIST_HEAD(&dwc->gadget->ep_list); in dwc3_gadget_init_endpoints()
3169 dep = dwc->eps[epnum]; in dwc3_gadget_free_endpoints()
3174 * bi-directional USB endpoint 0. in dwc3_gadget_free_endpoints()
3183 list_del(&dep->endpoint.ep_list); in dwc3_gadget_free_endpoints()
3186 debugfs_remove_recursive(debugfs_lookup(dep->name, in dwc3_gadget_free_endpoints()
3187 debugfs_lookup(dev_name(dep->dwc->dev), in dwc3_gadget_free_endpoints()
3193 /* -------------------------------------------------------------------------- */
3204 req->num_trbs--; in dwc3_gadget_ep_reclaim_completed_trb()
3216 if (chain && (trb->ctrl & DWC3_TRB_CTRL_HWO)) in dwc3_gadget_ep_reclaim_completed_trb()
3217 trb->ctrl &= ~DWC3_TRB_CTRL_HWO; in dwc3_gadget_ep_reclaim_completed_trb()
3221 * have the Isoc-First type. Track and report its interval frame number. in dwc3_gadget_ep_reclaim_completed_trb()
3223 if (usb_endpoint_xfer_isoc(dep->endpoint.desc) && in dwc3_gadget_ep_reclaim_completed_trb()
3224 (trb->ctrl & DWC3_TRBCTL_ISOCHRONOUS_FIRST)) { in dwc3_gadget_ep_reclaim_completed_trb()
3227 frame_number = DWC3_TRB_CTRL_GET_SID_SOFN(trb->ctrl); in dwc3_gadget_ep_reclaim_completed_trb()
3228 frame_number &= ~(dep->interval - 1); in dwc3_gadget_ep_reclaim_completed_trb()
3229 req->request.frame_number = frame_number; in dwc3_gadget_ep_reclaim_completed_trb()
3235 * TRB. Don't add it to req->remaining calculation. in dwc3_gadget_ep_reclaim_completed_trb()
3237 if (trb->bpl == lower_32_bits(dep->dwc->bounce_addr) && in dwc3_gadget_ep_reclaim_completed_trb()
3238 trb->bph == upper_32_bits(dep->dwc->bounce_addr)) { in dwc3_gadget_ep_reclaim_completed_trb()
3239 trb->ctrl &= ~DWC3_TRB_CTRL_HWO; in dwc3_gadget_ep_reclaim_completed_trb()
3243 count = trb->size & DWC3_TRB_SIZE_MASK; in dwc3_gadget_ep_reclaim_completed_trb()
3244 req->remaining += count; in dwc3_gadget_ep_reclaim_completed_trb()
3246 if ((trb->ctrl & DWC3_TRB_CTRL_HWO) && status != -ESHUTDOWN) in dwc3_gadget_ep_reclaim_completed_trb()
3249 if (event->status & DEPEVT_STATUS_SHORT && !chain) in dwc3_gadget_ep_reclaim_completed_trb()
3252 if ((trb->ctrl & DWC3_TRB_CTRL_ISP_IMI) && in dwc3_gadget_ep_reclaim_completed_trb()
3253 DWC3_TRB_SIZE_TRBSTS(trb->size) == DWC3_TRBSTS_MISSED_ISOC) in dwc3_gadget_ep_reclaim_completed_trb()
3256 if ((trb->ctrl & DWC3_TRB_CTRL_IOC) || in dwc3_gadget_ep_reclaim_completed_trb()
3257 (trb->ctrl & DWC3_TRB_CTRL_LST)) in dwc3_gadget_ep_reclaim_completed_trb()
3267 struct dwc3_trb *trb = &dep->trb_pool[dep->trb_dequeue]; in dwc3_gadget_ep_reclaim_trb_sg()
3268 struct scatterlist *sg = req->sg; in dwc3_gadget_ep_reclaim_trb_sg()
3270 unsigned int num_queued = req->num_queued_sgs; in dwc3_gadget_ep_reclaim_trb_sg()
3275 trb = &dep->trb_pool[dep->trb_dequeue]; in dwc3_gadget_ep_reclaim_trb_sg()
3277 req->sg = sg_next(s); in dwc3_gadget_ep_reclaim_trb_sg()
3278 req->num_queued_sgs--; in dwc3_gadget_ep_reclaim_trb_sg()
3293 struct dwc3_trb *trb = &dep->trb_pool[dep->trb_dequeue]; in dwc3_gadget_ep_reclaim_trb_linear()
3301 return req->num_pending_sgs == 0 && req->num_queued_sgs == 0; in dwc3_gadget_ep_request_completed()
3311 if (req->request.num_mapped_sgs) in dwc3_gadget_ep_cleanup_completed_request()
3318 req->request.actual = req->request.length - req->remaining; in dwc3_gadget_ep_cleanup_completed_request()
3323 if (req->needs_extra_trb) { in dwc3_gadget_ep_cleanup_completed_request()
3326 req->needs_extra_trb = false; in dwc3_gadget_ep_cleanup_completed_request()
3335 if (req->request.no_interrupt) { in dwc3_gadget_ep_cleanup_completed_request()
3338 trb = dwc3_ep_prev_trb(dep, dep->trb_dequeue); in dwc3_gadget_ep_cleanup_completed_request()
3339 switch (DWC3_TRB_SIZE_TRBSTS(trb->size)) { in dwc3_gadget_ep_cleanup_completed_request()
3342 request_status = -EXDEV; in dwc3_gadget_ep_cleanup_completed_request()
3368 while (!list_empty(&dep->started_list)) { in dwc3_gadget_ep_cleanup_completed_requests()
3371 req = next_request(&dep->started_list); in dwc3_gadget_ep_cleanup_completed_requests()
3380 if (!dep->endpoint.desc) in dwc3_gadget_ep_cleanup_completed_requests()
3388 struct dwc3 *dwc = dep->dwc; in dwc3_gadget_ep_should_continue()
3390 if (!dep->endpoint.desc || !dwc->pullups_connected || in dwc3_gadget_ep_should_continue()
3391 !dwc->connected) in dwc3_gadget_ep_should_continue()
3394 if (!list_empty(&dep->pending_list)) in dwc3_gadget_ep_should_continue()
3401 req = next_request(&dep->started_list); in dwc3_gadget_ep_should_continue()
3411 dep->frame_number = event->parameters; in dwc3_gadget_endpoint_frame_from_event()
3417 struct dwc3 *dwc = dep->dwc; in dwc3_gadget_endpoint_trbs_complete()
3422 if (dep->flags & DWC3_EP_END_TRANSFER_PENDING) in dwc3_gadget_endpoint_trbs_complete()
3425 if (!dep->endpoint.desc) in dwc3_gadget_endpoint_trbs_complete()
3428 if (usb_endpoint_xfer_isoc(dep->endpoint.desc) && in dwc3_gadget_endpoint_trbs_complete()
3429 list_empty(&dep->started_list) && in dwc3_gadget_endpoint_trbs_complete()
3430 (list_empty(&dep->pending_list) || status == -EXDEV)) in dwc3_gadget_endpoint_trbs_complete()
3438 * WORKAROUND: This is the 2nd half of U1/U2 -> U0 workaround. in dwc3_gadget_endpoint_trbs_complete()
3446 dep = dwc->eps[i]; in dwc3_gadget_endpoint_trbs_complete()
3448 if (!(dep->flags & DWC3_EP_ENABLED)) in dwc3_gadget_endpoint_trbs_complete()
3451 if (!list_empty(&dep->started_list)) in dwc3_gadget_endpoint_trbs_complete()
3455 reg = dwc3_readl(dwc->regs, DWC3_DCTL); in dwc3_gadget_endpoint_trbs_complete()
3456 reg |= dwc->u1u2; in dwc3_gadget_endpoint_trbs_complete()
3457 dwc3_writel(dwc->regs, DWC3_DCTL, reg); in dwc3_gadget_endpoint_trbs_complete()
3459 dwc->u1u2 = 0; in dwc3_gadget_endpoint_trbs_complete()
3470 if (!dep->endpoint.desc) in dwc3_gadget_endpoint_transfer_in_progress()
3473 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) in dwc3_gadget_endpoint_transfer_in_progress()
3476 if (event->status & DEPEVT_STATUS_BUSERR) in dwc3_gadget_endpoint_transfer_in_progress()
3477 status = -ECONNRESET; in dwc3_gadget_endpoint_transfer_in_progress()
3479 if (event->status & DEPEVT_STATUS_MISSED_ISOC) in dwc3_gadget_endpoint_transfer_in_progress()
3480 status = -EXDEV; in dwc3_gadget_endpoint_transfer_in_progress()
3490 dep->flags &= ~DWC3_EP_TRANSFER_STARTED; in dwc3_gadget_endpoint_transfer_complete()
3492 if (event->status & DEPEVT_STATUS_BUSERR) in dwc3_gadget_endpoint_transfer_complete()
3493 status = -ECONNRESET; in dwc3_gadget_endpoint_transfer_complete()
3496 dep->flags &= ~DWC3_EP_WAIT_TRANSFER_COMPLETE; in dwc3_gadget_endpoint_transfer_complete()
3512 if (dep->flags & DWC3_EP_END_TRANSFER_PENDING) in dwc3_gadget_endpoint_transfer_not_ready()
3521 u8 cmd = DEPEVT_PARAMETER_CMD(event->parameters); in dwc3_gadget_endpoint_command_complete()
3531 if (dep->stream_capable) in dwc3_gadget_endpoint_command_complete()
3532 dep->flags |= DWC3_EP_IGNORE_NEXT_NOSTREAM; in dwc3_gadget_endpoint_command_complete()
3534 dep->flags &= ~DWC3_EP_END_TRANSFER_PENDING; in dwc3_gadget_endpoint_command_complete()
3535 dep->flags &= ~DWC3_EP_TRANSFER_STARTED; in dwc3_gadget_endpoint_command_complete()
3538 if (dep->flags & DWC3_EP_PENDING_CLEAR_STALL) { in dwc3_gadget_endpoint_command_complete()
3539 struct dwc3 *dwc = dep->dwc; in dwc3_gadget_endpoint_command_complete()
3541 dep->flags &= ~DWC3_EP_PENDING_CLEAR_STALL; in dwc3_gadget_endpoint_command_complete()
3543 struct usb_ep *ep0 = &dwc->eps[0]->endpoint; in dwc3_gadget_endpoint_command_complete()
3545 dev_err(dwc->dev, "failed to clear STALL on %s\n", dep->name); in dwc3_gadget_endpoint_command_complete()
3546 if (dwc->delayed_status) in dwc3_gadget_endpoint_command_complete()
3551 dep->flags &= ~(DWC3_EP_STALL | DWC3_EP_WEDGE); in dwc3_gadget_endpoint_command_complete()
3552 if (dwc->clear_stall_protocol == dep->number) in dwc3_gadget_endpoint_command_complete()
3556 if ((dep->flags & DWC3_EP_DELAY_START) && in dwc3_gadget_endpoint_command_complete()
3557 !usb_endpoint_xfer_isoc(dep->endpoint.desc)) in dwc3_gadget_endpoint_command_complete()
3560 dep->flags &= ~DWC3_EP_DELAY_START; in dwc3_gadget_endpoint_command_complete()
3566 struct dwc3 *dwc = dep->dwc; in dwc3_gadget_endpoint_stream_event()
3568 if (event->status == DEPEVT_STREAMEVT_FOUND) { in dwc3_gadget_endpoint_stream_event()
3569 dep->flags |= DWC3_EP_FIRST_STREAM_PRIMED; in dwc3_gadget_endpoint_stream_event()
3574 switch (event->parameters) { in dwc3_gadget_endpoint_stream_event()
3583 if (dep->flags & DWC3_EP_FORCE_RESTART_STREAM) { in dwc3_gadget_endpoint_stream_event()
3584 if (dep->flags & DWC3_EP_FIRST_STREAM_PRIMED) in dwc3_gadget_endpoint_stream_event()
3585 dep->flags &= ~DWC3_EP_FORCE_RESTART_STREAM; in dwc3_gadget_endpoint_stream_event()
3587 dep->flags |= DWC3_EP_FIRST_STREAM_PRIMED; in dwc3_gadget_endpoint_stream_event()
3592 if ((dep->flags & DWC3_EP_IGNORE_NEXT_NOSTREAM) || in dwc3_gadget_endpoint_stream_event()
3593 !(dep->flags & DWC3_EP_FORCE_RESTART_STREAM) || in dwc3_gadget_endpoint_stream_event()
3594 (!DWC3_MST_CAPABLE(&dwc->hwparams) && in dwc3_gadget_endpoint_stream_event()
3595 !(dep->flags & DWC3_EP_WAIT_TRANSFER_COMPLETE))) in dwc3_gadget_endpoint_stream_event()
3615 dwc3_send_gadget_generic_command(dwc, cmd, dep->number); in dwc3_gadget_endpoint_stream_event()
3617 dep->flags |= DWC3_EP_DELAY_START; in dwc3_gadget_endpoint_stream_event()
3625 dep->flags &= ~DWC3_EP_IGNORE_NEXT_NOSTREAM; in dwc3_gadget_endpoint_stream_event()
3632 u8 epnum = event->endpoint_number; in dwc3_endpoint_interrupt()
3634 dep = dwc->eps[epnum]; in dwc3_endpoint_interrupt()
3636 if (!(dep->flags & DWC3_EP_ENABLED)) { in dwc3_endpoint_interrupt()
3637 if ((epnum > 1) && !(dep->flags & DWC3_EP_TRANSFER_STARTED)) in dwc3_endpoint_interrupt()
3641 if ((event->endpoint_event != DWC3_DEPEVT_EPCMDCMPLT) && in dwc3_endpoint_interrupt()
3642 !(epnum <= 1 && event->endpoint_event == DWC3_DEPEVT_XFERCOMPLETE)) in dwc3_endpoint_interrupt()
3651 switch (event->endpoint_event) { in dwc3_endpoint_interrupt()
3674 if (dwc->async_callbacks && dwc->gadget_driver->disconnect) { in dwc3_disconnect_gadget()
3675 spin_unlock(&dwc->lock); in dwc3_disconnect_gadget()
3676 dwc->gadget_driver->disconnect(dwc->gadget); in dwc3_disconnect_gadget()
3677 spin_lock(&dwc->lock); in dwc3_disconnect_gadget()
3683 if (dwc->async_callbacks && dwc->gadget_driver->suspend) { in dwc3_suspend_gadget()
3684 spin_unlock(&dwc->lock); in dwc3_suspend_gadget()
3685 dwc->gadget_driver->suspend(dwc->gadget); in dwc3_suspend_gadget()
3686 spin_lock(&dwc->lock); in dwc3_suspend_gadget()
3692 if (dwc->async_callbacks && dwc->gadget_driver->resume) { in dwc3_resume_gadget()
3693 spin_unlock(&dwc->lock); in dwc3_resume_gadget()
3694 dwc->gadget_driver->resume(dwc->gadget); in dwc3_resume_gadget()
3695 spin_lock(&dwc->lock); in dwc3_resume_gadget()
3701 if (!dwc->gadget_driver) in dwc3_reset_gadget()
3704 if (dwc->async_callbacks && dwc->gadget->speed != USB_SPEED_UNKNOWN) { in dwc3_reset_gadget()
3705 spin_unlock(&dwc->lock); in dwc3_reset_gadget()
3706 usb_gadget_udc_reset(dwc->gadget, dwc->gadget_driver); in dwc3_reset_gadget()
3707 spin_lock(&dwc->lock); in dwc3_reset_gadget()
3714 struct dwc3 *dwc = dep->dwc; in dwc3_stop_active_transfer()
3722 if (dep->number <= 1 && dwc->ep0state != EP0_DATA_PHASE) in dwc3_stop_active_transfer()
3725 if (!(dep->flags & DWC3_EP_TRANSFER_STARTED) || in dwc3_stop_active_transfer()
3726 (dep->flags & DWC3_EP_DELAY_STOP) || in dwc3_stop_active_transfer()
3727 (dep->flags & DWC3_EP_END_TRANSFER_PENDING)) in dwc3_stop_active_transfer()
3737 if (dwc->ep0state != EP0_SETUP_PHASE && !dwc->delayed_status) { in dwc3_stop_active_transfer()
3738 dep->flags |= DWC3_EP_DELAY_STOP; in dwc3_stop_active_transfer()
3780 dep = dwc->eps[epnum]; in dwc3_clear_stall_all_ep()
3784 if (!(dep->flags & DWC3_EP_STALL)) in dwc3_clear_stall_all_ep()
3787 dep->flags &= ~DWC3_EP_STALL; in dwc3_clear_stall_all_ep()
3800 reg = dwc3_readl(dwc->regs, DWC3_DCTL); in dwc3_gadget_disconnect_interrupt()
3805 dwc->connected = false; in dwc3_gadget_disconnect_interrupt()
3809 dwc->gadget->speed = USB_SPEED_UNKNOWN; in dwc3_gadget_disconnect_interrupt()
3810 dwc->setup_packet_pending = false; in dwc3_gadget_disconnect_interrupt()
3811 usb_gadget_set_state(dwc->gadget, USB_STATE_NOTATTACHED); in dwc3_gadget_disconnect_interrupt()
3813 if (dwc->ep0state != EP0_SETUP_PHASE) { in dwc3_gadget_disconnect_interrupt()
3816 dir = !!dwc->ep0_expect_in; in dwc3_gadget_disconnect_interrupt()
3817 if (dwc->ep0state == EP0_DATA_PHASE) in dwc3_gadget_disconnect_interrupt()
3818 dwc3_ep0_end_control_data(dwc, dwc->eps[dir]); in dwc3_gadget_disconnect_interrupt()
3820 dwc3_ep0_end_control_data(dwc, dwc->eps[!dir]); in dwc3_gadget_disconnect_interrupt()
3836 dwc->connected = false; in dwc3_gadget_reset_interrupt()
3865 if (dwc->setup_packet_pending) in dwc3_gadget_reset_interrupt()
3876 if (dwc->ep0state != EP0_SETUP_PHASE) { in dwc3_gadget_reset_interrupt()
3879 dir = !!dwc->ep0_expect_in; in dwc3_gadget_reset_interrupt()
3880 if (dwc->ep0state == EP0_DATA_PHASE) in dwc3_gadget_reset_interrupt()
3881 dwc3_ep0_end_control_data(dwc, dwc->eps[dir]); in dwc3_gadget_reset_interrupt()
3883 dwc3_ep0_end_control_data(dwc, dwc->eps[!dir]); in dwc3_gadget_reset_interrupt()
3885 dwc->eps[0]->trb_enqueue = 0; in dwc3_gadget_reset_interrupt()
3886 dwc->eps[1]->trb_enqueue = 0; in dwc3_gadget_reset_interrupt()
3893 * Section 4.1.2 Table 4-2, it states that during a USB reset, the SW in dwc3_gadget_reset_interrupt()
3898 dwc->connected = true; in dwc3_gadget_reset_interrupt()
3900 reg = dwc3_readl(dwc->regs, DWC3_DCTL); in dwc3_gadget_reset_interrupt()
3903 dwc->test_mode = false; in dwc3_gadget_reset_interrupt()
3907 reg = dwc3_readl(dwc->regs, DWC3_DCFG); in dwc3_gadget_reset_interrupt()
3909 dwc3_writel(dwc->regs, DWC3_DCFG, reg); in dwc3_gadget_reset_interrupt()
3920 if (!dwc->softconnect) in dwc3_gadget_conndone_interrupt()
3923 reg = dwc3_readl(dwc->regs, DWC3_DSTS); in dwc3_gadget_conndone_interrupt()
3925 dwc->speed = speed; in dwc3_gadget_conndone_interrupt()
3930 dwc->gadget->ssp_rate = USB_SSP_GEN_UNKNOWN; in dwc3_gadget_conndone_interrupt()
3944 dwc->gadget->ep0->maxpacket = 512; in dwc3_gadget_conndone_interrupt()
3945 dwc->gadget->speed = USB_SPEED_SUPER_PLUS; in dwc3_gadget_conndone_interrupt()
3948 dwc->gadget->ssp_rate = USB_SSP_GEN_2x2; in dwc3_gadget_conndone_interrupt()
3950 dwc->gadget->ssp_rate = USB_SSP_GEN_2x1; in dwc3_gadget_conndone_interrupt()
3970 dwc->gadget->ep0->maxpacket = 512; in dwc3_gadget_conndone_interrupt()
3971 dwc->gadget->speed = USB_SPEED_SUPER; in dwc3_gadget_conndone_interrupt()
3974 dwc->gadget->speed = USB_SPEED_SUPER_PLUS; in dwc3_gadget_conndone_interrupt()
3975 dwc->gadget->ssp_rate = USB_SSP_GEN_1x2; in dwc3_gadget_conndone_interrupt()
3980 dwc->gadget->ep0->maxpacket = 64; in dwc3_gadget_conndone_interrupt()
3981 dwc->gadget->speed = USB_SPEED_HIGH; in dwc3_gadget_conndone_interrupt()
3985 dwc->gadget->ep0->maxpacket = 64; in dwc3_gadget_conndone_interrupt()
3986 dwc->gadget->speed = USB_SPEED_FULL; in dwc3_gadget_conndone_interrupt()
3990 dwc->eps[1]->endpoint.maxpacket = dwc->gadget->ep0->maxpacket; in dwc3_gadget_conndone_interrupt()
3992 /* Enable USB2 LPM Capability */ in dwc3_gadget_conndone_interrupt()
3995 !dwc->usb2_gadget_lpm_disable && in dwc3_gadget_conndone_interrupt()
3998 reg = dwc3_readl(dwc->regs, DWC3_DCFG); in dwc3_gadget_conndone_interrupt()
4000 dwc3_writel(dwc->regs, DWC3_DCFG, reg); in dwc3_gadget_conndone_interrupt()
4002 reg = dwc3_readl(dwc->regs, DWC3_DCTL); in dwc3_gadget_conndone_interrupt()
4005 reg |= DWC3_DCTL_HIRD_THRES(dwc->hird_threshold | in dwc3_gadget_conndone_interrupt()
4006 (dwc->is_utmi_l1_suspend << 4)); in dwc3_gadget_conndone_interrupt()
4009 * When dwc3 revisions >= 2.40a, LPM Erratum is enabled and in dwc3_gadget_conndone_interrupt()
4011 * BESL value in the LPM token is less than or equal to LPM in dwc3_gadget_conndone_interrupt()
4012 * NYET threshold. in dwc3_gadget_conndone_interrupt()
4014 WARN_ONCE(DWC3_VER_IS_PRIOR(DWC3, 240A) && dwc->has_lpm_erratum, in dwc3_gadget_conndone_interrupt()
4015 "LPM Erratum not available on dwc3 revisions < 2.40a\n"); in dwc3_gadget_conndone_interrupt()
4017 if (dwc->has_lpm_erratum && !DWC3_VER_IS_PRIOR(DWC3, 240A)) in dwc3_gadget_conndone_interrupt()
4018 reg |= DWC3_DCTL_NYET_THRES(dwc->lpm_nyet_threshold); in dwc3_gadget_conndone_interrupt()
4022 if (dwc->usb2_gadget_lpm_disable) { in dwc3_gadget_conndone_interrupt()
4023 reg = dwc3_readl(dwc->regs, DWC3_DCFG); in dwc3_gadget_conndone_interrupt()
4025 dwc3_writel(dwc->regs, DWC3_DCFG, reg); in dwc3_gadget_conndone_interrupt()
4028 reg = dwc3_readl(dwc->regs, DWC3_DCTL); in dwc3_gadget_conndone_interrupt()
4033 dep = dwc->eps[0]; in dwc3_gadget_conndone_interrupt()
4036 dev_err(dwc->dev, "failed to enable %s\n", dep->name); in dwc3_gadget_conndone_interrupt()
4040 dep = dwc->eps[1]; in dwc3_gadget_conndone_interrupt()
4043 dev_err(dwc->dev, "failed to enable %s\n", dep->name); in dwc3_gadget_conndone_interrupt()
4063 if (dwc->async_callbacks && dwc->gadget_driver->resume) { in dwc3_gadget_wakeup_interrupt()
4064 spin_unlock(&dwc->lock); in dwc3_gadget_wakeup_interrupt()
4065 dwc->gadget_driver->resume(dwc->gadget); in dwc3_gadget_wakeup_interrupt()
4066 spin_lock(&dwc->lock); in dwc3_gadget_wakeup_interrupt()
4079 * host-initiated U3 exit. in dwc3_gadget_linksts_change_interrupt()
4090 * STAR#9000570034 RTL: SS Resume event generated in non-Hibernation in dwc3_gadget_linksts_change_interrupt()
4093 pwropt = DWC3_GHWPARAMS1_EN_PWROPT(dwc->hwparams.hwparams1); in dwc3_gadget_linksts_change_interrupt()
4096 if ((dwc->link_state == DWC3_LINK_STATE_U3) && in dwc3_gadget_linksts_change_interrupt()
4117 * STAR#9000446952: RTL: Device SS : if U1/U2 ->U0 takes >128us in dwc3_gadget_linksts_change_interrupt()
4125 switch (dwc->link_state) { in dwc3_gadget_linksts_change_interrupt()
4128 reg = dwc3_readl(dwc->regs, DWC3_DCTL); in dwc3_gadget_linksts_change_interrupt()
4134 if (!dwc->u1u2) in dwc3_gadget_linksts_change_interrupt()
4135 dwc->u1u2 = reg & u1u2; in dwc3_gadget_linksts_change_interrupt()
4150 if (dwc->speed == USB_SPEED_SUPER) in dwc3_gadget_linksts_change_interrupt()
4165 dwc->link_state = next; in dwc3_gadget_linksts_change_interrupt()
4173 if (dwc->link_state != next && next == DWC3_LINK_STATE_U3) in dwc3_gadget_suspend_interrupt()
4176 dwc->link_state = next; in dwc3_gadget_suspend_interrupt()
4197 if (is_ss ^ (dwc->speed == USB_SPEED_SUPER)) in dwc3_gadget_hibernation_interrupt()
4206 switch (event->type) { in dwc3_gadget_interrupt()
4220 if (dev_WARN_ONCE(dwc->dev, !dwc->has_hibernation, in dwc3_gadget_interrupt()
4224 dwc3_gadget_hibernation_interrupt(dwc, event->event_info); in dwc3_gadget_interrupt()
4227 dwc3_gadget_linksts_change_interrupt(dwc, event->event_info); in dwc3_gadget_interrupt()
4236 if (dwc->gadget->state >= USB_STATE_CONFIGURED) in dwc3_gadget_interrupt()
4238 event->event_info); in dwc3_gadget_interrupt()
4247 dev_WARN(dwc->dev, "UNKNOWN IRQ %d\n", event->type); in dwc3_gadget_interrupt()
4254 trace_dwc3_event(event->raw, dwc); in dwc3_process_event_entry()
4256 if (!event->type.is_devspec) in dwc3_process_event_entry()
4257 dwc3_endpoint_interrupt(dwc, &event->depevt); in dwc3_process_event_entry()
4258 else if (event->type.type == DWC3_EVENT_TYPE_DEV) in dwc3_process_event_entry()
4259 dwc3_gadget_interrupt(dwc, &event->devt); in dwc3_process_event_entry()
4261 dev_err(dwc->dev, "UNKNOWN IRQ type %d\n", event->raw); in dwc3_process_event_entry()
4266 struct dwc3 *dwc = evt->dwc; in dwc3_process_event_buf()
4270 left = evt->count; in dwc3_process_event_buf()
4272 if (!(evt->flags & DWC3_EVENT_PENDING)) in dwc3_process_event_buf()
4278 event.raw = *(u32 *) (evt->cache + evt->lpos); in dwc3_process_event_buf()
4291 evt->lpos = (evt->lpos + 4) % evt->length; in dwc3_process_event_buf()
4292 left -= 4; in dwc3_process_event_buf()
4295 evt->count = 0; in dwc3_process_event_buf()
4299 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0), in dwc3_process_event_buf()
4300 DWC3_GEVNTSIZ_SIZE(evt->length)); in dwc3_process_event_buf()
4302 if (dwc->imod_interval) { in dwc3_process_event_buf()
4303 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), DWC3_GEVNTCOUNT_EHB); in dwc3_process_event_buf()
4304 dwc3_writel(dwc->regs, DWC3_DEV_IMOD(0), dwc->imod_interval); in dwc3_process_event_buf()
4308 evt->flags &= ~DWC3_EVENT_PENDING; in dwc3_process_event_buf()
4316 struct dwc3 *dwc = evt->dwc; in dwc3_thread_interrupt()
4321 spin_lock_irqsave(&dwc->lock, flags); in dwc3_thread_interrupt()
4323 spin_unlock_irqrestore(&dwc->lock, flags); in dwc3_thread_interrupt()
4331 struct dwc3 *dwc = evt->dwc; in dwc3_check_event_buf()
4335 if (pm_runtime_suspended(dwc->dev)) { in dwc3_check_event_buf()
4336 pm_runtime_get(dwc->dev); in dwc3_check_event_buf()
4337 disable_irq_nosync(dwc->irq_gadget); in dwc3_check_event_buf()
4338 dwc->pending_events = true; in dwc3_check_event_buf()
4343 * With PCIe legacy interrupt, test shows that top-half irq handler can in dwc3_check_event_buf()
4344 * be called again after HW interrupt deassertion. Check if bottom-half in dwc3_check_event_buf()
4348 if (evt->flags & DWC3_EVENT_PENDING) in dwc3_check_event_buf()
4351 count = dwc3_readl(dwc->regs, DWC3_GEVNTCOUNT(0)); in dwc3_check_event_buf()
4356 evt->count = count; in dwc3_check_event_buf()
4357 evt->flags |= DWC3_EVENT_PENDING; in dwc3_check_event_buf()
4360 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0), in dwc3_check_event_buf()
4361 DWC3_GEVNTSIZ_INTMASK | DWC3_GEVNTSIZ_SIZE(evt->length)); in dwc3_check_event_buf()
4363 amount = min(count, evt->length - evt->lpos); in dwc3_check_event_buf()
4364 memcpy(evt->cache + evt->lpos, evt->buf + evt->lpos, amount); in dwc3_check_event_buf()
4367 memcpy(evt->cache, evt->buf, count - amount); in dwc3_check_event_buf()
4369 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), count); in dwc3_check_event_buf()
4383 struct platform_device *dwc3_pdev = to_platform_device(dwc->dev); in dwc3_gadget_get_irq()
4390 if (irq == -EPROBE_DEFER) in dwc3_gadget_get_irq()
4397 if (irq == -EPROBE_DEFER) in dwc3_gadget_get_irq()
4405 irq = -EINVAL; in dwc3_gadget_get_irq()
4419 * dwc3_gadget_init - initializes gadget related registers
4436 dwc->irq_gadget = irq; in dwc3_gadget_init()
4438 dwc->ep0_trb = dma_alloc_coherent(dwc->sysdev, in dwc3_gadget_init()
4439 sizeof(*dwc->ep0_trb) * 2, in dwc3_gadget_init()
4440 &dwc->ep0_trb_addr, GFP_KERNEL); in dwc3_gadget_init()
4441 if (!dwc->ep0_trb) { in dwc3_gadget_init()
4442 dev_err(dwc->dev, "failed to allocate ep0 trb\n"); in dwc3_gadget_init()
4443 ret = -ENOMEM; in dwc3_gadget_init()
4447 dwc->setup_buf = kzalloc(DWC3_EP0_SETUP_SIZE, GFP_KERNEL); in dwc3_gadget_init()
4448 if (!dwc->setup_buf) { in dwc3_gadget_init()
4449 ret = -ENOMEM; in dwc3_gadget_init()
4453 dwc->bounce = dma_alloc_coherent(dwc->sysdev, DWC3_BOUNCE_SIZE, in dwc3_gadget_init()
4454 &dwc->bounce_addr, GFP_KERNEL); in dwc3_gadget_init()
4455 if (!dwc->bounce) { in dwc3_gadget_init()
4456 ret = -ENOMEM; in dwc3_gadget_init()
4460 init_completion(&dwc->ep0_in_setup); in dwc3_gadget_init()
4461 dwc->gadget = kzalloc(sizeof(struct usb_gadget), GFP_KERNEL); in dwc3_gadget_init()
4462 if (!dwc->gadget) { in dwc3_gadget_init()
4463 ret = -ENOMEM; in dwc3_gadget_init()
4468 usb_initialize_gadget(dwc->dev, dwc->gadget, dwc_gadget_release); in dwc3_gadget_init()
4469 dev = &dwc->gadget->dev; in dwc3_gadget_init()
4470 dev->platform_data = dwc; in dwc3_gadget_init()
4471 dwc->gadget->ops = &dwc3_gadget_ops; in dwc3_gadget_init()
4472 dwc->gadget->speed = USB_SPEED_UNKNOWN; in dwc3_gadget_init()
4473 dwc->gadget->ssp_rate = USB_SSP_GEN_UNKNOWN; in dwc3_gadget_init()
4474 dwc->gadget->sg_supported = true; in dwc3_gadget_init()
4475 dwc->gadget->name = "dwc3-gadget"; in dwc3_gadget_init()
4476 dwc->gadget->lpm_capable = !dwc->usb2_gadget_lpm_disable; in dwc3_gadget_init()
4492 * composite.c that we are USB 2.0 + LPM ECN. in dwc3_gadget_init()
4495 !dwc->dis_metastability_quirk) in dwc3_gadget_init()
4496 dev_info(dwc->dev, "changing max_speed on rev %08x\n", in dwc3_gadget_init()
4497 dwc->revision); in dwc3_gadget_init()
4499 dwc->gadget->max_speed = dwc->maximum_speed; in dwc3_gadget_init()
4500 dwc->gadget->max_ssp_rate = dwc->max_ssp_rate; in dwc3_gadget_init()
4507 ret = dwc3_gadget_init_endpoints(dwc, dwc->num_eps); in dwc3_gadget_init()
4511 ret = usb_add_gadget(dwc->gadget); in dwc3_gadget_init()
4513 dev_err(dwc->dev, "failed to add gadget\n"); in dwc3_gadget_init()
4517 if (DWC3_IP_IS(DWC32) && dwc->maximum_speed == USB_SPEED_SUPER_PLUS) in dwc3_gadget_init()
4518 dwc3_gadget_set_ssp_rate(dwc->gadget, dwc->max_ssp_rate); in dwc3_gadget_init()
4520 dwc3_gadget_set_speed(dwc->gadget, dwc->maximum_speed); in dwc3_gadget_init()
4527 usb_put_gadget(dwc->gadget); in dwc3_gadget_init()
4528 dwc->gadget = NULL; in dwc3_gadget_init()
4530 dma_free_coherent(dwc->sysdev, DWC3_BOUNCE_SIZE, dwc->bounce, in dwc3_gadget_init()
4531 dwc->bounce_addr); in dwc3_gadget_init()
4534 kfree(dwc->setup_buf); in dwc3_gadget_init()
4537 dma_free_coherent(dwc->sysdev, sizeof(*dwc->ep0_trb) * 2, in dwc3_gadget_init()
4538 dwc->ep0_trb, dwc->ep0_trb_addr); in dwc3_gadget_init()
4544 /* -------------------------------------------------------------------------- */
4548 if (!dwc->gadget) in dwc3_gadget_exit()
4551 usb_del_gadget(dwc->gadget); in dwc3_gadget_exit()
4553 usb_put_gadget(dwc->gadget); in dwc3_gadget_exit()
4554 dma_free_coherent(dwc->sysdev, DWC3_BOUNCE_SIZE, dwc->bounce, in dwc3_gadget_exit()
4555 dwc->bounce_addr); in dwc3_gadget_exit()
4556 kfree(dwc->setup_buf); in dwc3_gadget_exit()
4557 dma_free_coherent(dwc->sysdev, sizeof(*dwc->ep0_trb) * 2, in dwc3_gadget_exit()
4558 dwc->ep0_trb, dwc->ep0_trb_addr); in dwc3_gadget_exit()
4565 if (!dwc->gadget_driver) in dwc3_gadget_suspend()
4570 spin_lock_irqsave(&dwc->lock, flags); in dwc3_gadget_suspend()
4573 spin_unlock_irqrestore(&dwc->lock, flags); in dwc3_gadget_suspend()
4582 if (!dwc->gadget_driver || !dwc->softconnect) in dwc3_gadget_resume()
4604 if (dwc->pending_events) { in dwc3_gadget_process_pending_events()
4605 dwc3_interrupt(dwc->irq_gadget, dwc->ev_buf); in dwc3_gadget_process_pending_events()
4606 dwc->pending_events = false; in dwc3_gadget_process_pending_events()
4607 enable_irq(dwc->irq_gadget); in dwc3_gadget_process_pending_events()