Greg Kroah-Hartman | 5fd54ac | 2017-11-03 11:28:30 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Felipe Balbi | bfad65e | 2017-04-19 14:59:27 +0300 | [diff] [blame] | 2 | /* |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3 | * gadget.c - DesignWare USB3 DRD Controller Gadget Framework Link |
| 4 | * |
Alexander A. Klimov | 10623b8 | 2020-07-11 15:58:04 +0200 | [diff] [blame] | 5 | * Copyright (C) 2010-2011 Texas Instruments Incorporated - https://www.ti.com |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 6 | * |
| 7 | * Authors: Felipe Balbi <balbi@ti.com>, |
| 8 | * Sebastian Andrzej Siewior <bigeasy@linutronix.de> |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 9 | */ |
| 10 | |
| 11 | #include <linux/kernel.h> |
| 12 | #include <linux/delay.h> |
| 13 | #include <linux/slab.h> |
| 14 | #include <linux/spinlock.h> |
| 15 | #include <linux/platform_device.h> |
| 16 | #include <linux/pm_runtime.h> |
| 17 | #include <linux/interrupt.h> |
| 18 | #include <linux/io.h> |
| 19 | #include <linux/list.h> |
| 20 | #include <linux/dma-mapping.h> |
| 21 | |
| 22 | #include <linux/usb/ch9.h> |
| 23 | #include <linux/usb/gadget.h> |
| 24 | |
Felipe Balbi | 80977dc | 2014-08-19 16:37:22 -0500 | [diff] [blame] | 25 | #include "debug.h" |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 26 | #include "core.h" |
| 27 | #include "gadget.h" |
| 28 | #include "io.h" |
| 29 | |
Felipe Balbi | d537010 | 2018-08-14 10:42:43 +0300 | [diff] [blame] | 30 | #define DWC3_ALIGN_FRAME(d, n) (((d)->frame_number + ((d)->interval * (n))) \ |
Felipe Balbi | f62afb4 | 2018-04-11 10:34:34 +0300 | [diff] [blame] | 31 | & ~((d)->interval - 1)) |
| 32 | |
Felipe Balbi | 04a9bfc | 2012-01-02 18:25:43 +0200 | [diff] [blame] | 33 | /** |
Felipe Balbi | bfad65e | 2017-04-19 14:59:27 +0300 | [diff] [blame] | 34 | * dwc3_gadget_set_test_mode - enables usb2 test modes |
Felipe Balbi | 04a9bfc | 2012-01-02 18:25:43 +0200 | [diff] [blame] | 35 | * @dwc: pointer to our context structure |
| 36 | * @mode: the mode to set (J, K SE0 NAK, Force Enable) |
| 37 | * |
Felipe Balbi | bfad65e | 2017-04-19 14:59:27 +0300 | [diff] [blame] | 38 | * Caller should take care of locking. This function will return 0 on |
| 39 | * success or -EINVAL if wrong Test Selector is passed. |
Felipe Balbi | 04a9bfc | 2012-01-02 18:25:43 +0200 | [diff] [blame] | 40 | */ |
| 41 | int dwc3_gadget_set_test_mode(struct dwc3 *dwc, int mode) |
| 42 | { |
| 43 | u32 reg; |
| 44 | |
| 45 | reg = dwc3_readl(dwc->regs, DWC3_DCTL); |
| 46 | reg &= ~DWC3_DCTL_TSTCTRL_MASK; |
| 47 | |
| 48 | switch (mode) { |
Greg Kroah-Hartman | 62fb45d | 2020-06-18 16:42:06 +0200 | [diff] [blame] | 49 | case USB_TEST_J: |
| 50 | case USB_TEST_K: |
| 51 | case USB_TEST_SE0_NAK: |
| 52 | case USB_TEST_PACKET: |
| 53 | case USB_TEST_FORCE_ENABLE: |
Felipe Balbi | 04a9bfc | 2012-01-02 18:25:43 +0200 | [diff] [blame] | 54 | reg |= mode << 1; |
| 55 | break; |
| 56 | default: |
| 57 | return -EINVAL; |
| 58 | } |
| 59 | |
Thinh Nguyen | 5b73821 | 2019-10-23 19:15:43 -0700 | [diff] [blame] | 60 | dwc3_gadget_dctl_write_safe(dwc, reg); |
Felipe Balbi | 04a9bfc | 2012-01-02 18:25:43 +0200 | [diff] [blame] | 61 | |
| 62 | return 0; |
| 63 | } |
| 64 | |
Felipe Balbi | 8598bde | 2012-01-02 18:55:57 +0200 | [diff] [blame] | 65 | /** |
Felipe Balbi | bfad65e | 2017-04-19 14:59:27 +0300 | [diff] [blame] | 66 | * dwc3_gadget_get_link_state - gets current state of usb link |
Paul Zimmerman | 911f1f8 | 2012-04-27 13:35:15 +0300 | [diff] [blame] | 67 | * @dwc: pointer to our context structure |
| 68 | * |
| 69 | * Caller should take care of locking. This function will |
| 70 | * return the link state on success (>= 0) or -ETIMEDOUT. |
| 71 | */ |
| 72 | int dwc3_gadget_get_link_state(struct dwc3 *dwc) |
| 73 | { |
| 74 | u32 reg; |
| 75 | |
| 76 | reg = dwc3_readl(dwc->regs, DWC3_DSTS); |
| 77 | |
| 78 | return DWC3_DSTS_USBLNKST(reg); |
| 79 | } |
| 80 | |
| 81 | /** |
Felipe Balbi | bfad65e | 2017-04-19 14:59:27 +0300 | [diff] [blame] | 82 | * dwc3_gadget_set_link_state - sets usb link to a particular state |
Felipe Balbi | 8598bde | 2012-01-02 18:55:57 +0200 | [diff] [blame] | 83 | * @dwc: pointer to our context structure |
| 84 | * @state: the state to put link into |
| 85 | * |
| 86 | * Caller should take care of locking. This function will |
Paul Zimmerman | aee63e3 | 2012-02-24 17:32:15 -0800 | [diff] [blame] | 87 | * return 0 on success or -ETIMEDOUT. |
Felipe Balbi | 8598bde | 2012-01-02 18:55:57 +0200 | [diff] [blame] | 88 | */ |
| 89 | int dwc3_gadget_set_link_state(struct dwc3 *dwc, enum dwc3_link_state state) |
| 90 | { |
Paul Zimmerman | aee63e3 | 2012-02-24 17:32:15 -0800 | [diff] [blame] | 91 | int retries = 10000; |
Felipe Balbi | 8598bde | 2012-01-02 18:55:57 +0200 | [diff] [blame] | 92 | u32 reg; |
| 93 | |
Paul Zimmerman | 802fde9 | 2012-04-27 13:10:52 +0300 | [diff] [blame] | 94 | /* |
| 95 | * Wait until device controller is ready. Only applies to 1.94a and |
| 96 | * later RTL. |
| 97 | */ |
Thinh Nguyen | 9af21dd | 2020-04-11 19:20:01 -0700 | [diff] [blame] | 98 | if (!DWC3_VER_IS_PRIOR(DWC3, 194A)) { |
Paul Zimmerman | 802fde9 | 2012-04-27 13:10:52 +0300 | [diff] [blame] | 99 | while (--retries) { |
| 100 | reg = dwc3_readl(dwc->regs, DWC3_DSTS); |
| 101 | if (reg & DWC3_DSTS_DCNRD) |
| 102 | udelay(5); |
| 103 | else |
| 104 | break; |
| 105 | } |
| 106 | |
| 107 | if (retries <= 0) |
| 108 | return -ETIMEDOUT; |
| 109 | } |
| 110 | |
Felipe Balbi | 8598bde | 2012-01-02 18:55:57 +0200 | [diff] [blame] | 111 | reg = dwc3_readl(dwc->regs, DWC3_DCTL); |
| 112 | reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK; |
| 113 | |
Thinh Nguyen | 2e708fa | 2019-10-23 19:15:55 -0700 | [diff] [blame] | 114 | /* set no action before sending new link state change */ |
| 115 | dwc3_writel(dwc->regs, DWC3_DCTL, reg); |
| 116 | |
Felipe Balbi | 8598bde | 2012-01-02 18:55:57 +0200 | [diff] [blame] | 117 | /* set requested state */ |
| 118 | reg |= DWC3_DCTL_ULSTCHNGREQ(state); |
| 119 | dwc3_writel(dwc->regs, DWC3_DCTL, reg); |
| 120 | |
Paul Zimmerman | 802fde9 | 2012-04-27 13:10:52 +0300 | [diff] [blame] | 121 | /* |
| 122 | * The following code is racy when called from dwc3_gadget_wakeup, |
| 123 | * and is not needed, at least on newer versions |
| 124 | */ |
Thinh Nguyen | 9af21dd | 2020-04-11 19:20:01 -0700 | [diff] [blame] | 125 | if (!DWC3_VER_IS_PRIOR(DWC3, 194A)) |
Paul Zimmerman | 802fde9 | 2012-04-27 13:10:52 +0300 | [diff] [blame] | 126 | return 0; |
| 127 | |
Felipe Balbi | 8598bde | 2012-01-02 18:55:57 +0200 | [diff] [blame] | 128 | /* wait for a change in DSTS */ |
Paul Zimmerman | aed430e | 2012-04-27 12:52:01 +0300 | [diff] [blame] | 129 | retries = 10000; |
Felipe Balbi | 8598bde | 2012-01-02 18:55:57 +0200 | [diff] [blame] | 130 | while (--retries) { |
| 131 | reg = dwc3_readl(dwc->regs, DWC3_DSTS); |
| 132 | |
Felipe Balbi | 8598bde | 2012-01-02 18:55:57 +0200 | [diff] [blame] | 133 | if (DWC3_DSTS_USBLNKST(reg) == state) |
| 134 | return 0; |
| 135 | |
Paul Zimmerman | aee63e3 | 2012-02-24 17:32:15 -0800 | [diff] [blame] | 136 | udelay(5); |
Felipe Balbi | 8598bde | 2012-01-02 18:55:57 +0200 | [diff] [blame] | 137 | } |
| 138 | |
Felipe Balbi | 8598bde | 2012-01-02 18:55:57 +0200 | [diff] [blame] | 139 | return -ETIMEDOUT; |
| 140 | } |
| 141 | |
John Youn | dca0119 | 2016-05-19 17:26:05 -0700 | [diff] [blame] | 142 | /** |
Felipe Balbi | bfad65e | 2017-04-19 14:59:27 +0300 | [diff] [blame] | 143 | * dwc3_ep_inc_trb - increment a trb index. |
| 144 | * @index: Pointer to the TRB index to increment. |
John Youn | dca0119 | 2016-05-19 17:26:05 -0700 | [diff] [blame] | 145 | * |
| 146 | * The index should never point to the link TRB. After incrementing, |
| 147 | * if it is point to the link TRB, wrap around to the beginning. The |
| 148 | * link TRB is always at the last TRB entry. |
| 149 | */ |
| 150 | static void dwc3_ep_inc_trb(u8 *index) |
| 151 | { |
| 152 | (*index)++; |
| 153 | if (*index == (DWC3_TRB_NUM - 1)) |
| 154 | *index = 0; |
| 155 | } |
| 156 | |
Felipe Balbi | bfad65e | 2017-04-19 14:59:27 +0300 | [diff] [blame] | 157 | /** |
| 158 | * dwc3_ep_inc_enq - increment endpoint's enqueue pointer |
| 159 | * @dep: The endpoint whose enqueue pointer we're incrementing |
| 160 | */ |
Felipe Balbi | ef966b9 | 2016-04-05 13:09:51 +0300 | [diff] [blame] | 161 | static void dwc3_ep_inc_enq(struct dwc3_ep *dep) |
Felipe Balbi | 457e84b | 2012-01-18 18:04:09 +0200 | [diff] [blame] | 162 | { |
John Youn | dca0119 | 2016-05-19 17:26:05 -0700 | [diff] [blame] | 163 | dwc3_ep_inc_trb(&dep->trb_enqueue); |
Felipe Balbi | ef966b9 | 2016-04-05 13:09:51 +0300 | [diff] [blame] | 164 | } |
Felipe Balbi | 457e84b | 2012-01-18 18:04:09 +0200 | [diff] [blame] | 165 | |
Felipe Balbi | bfad65e | 2017-04-19 14:59:27 +0300 | [diff] [blame] | 166 | /** |
| 167 | * dwc3_ep_inc_deq - increment endpoint's dequeue pointer |
| 168 | * @dep: The endpoint whose enqueue pointer we're incrementing |
| 169 | */ |
Felipe Balbi | ef966b9 | 2016-04-05 13:09:51 +0300 | [diff] [blame] | 170 | static void dwc3_ep_inc_deq(struct dwc3_ep *dep) |
| 171 | { |
John Youn | dca0119 | 2016-05-19 17:26:05 -0700 | [diff] [blame] | 172 | dwc3_ep_inc_trb(&dep->trb_dequeue); |
Felipe Balbi | 457e84b | 2012-01-18 18:04:09 +0200 | [diff] [blame] | 173 | } |
| 174 | |
Wei Yongjun | 6910251 | 2018-03-29 02:20:10 +0000 | [diff] [blame] | 175 | static void dwc3_gadget_del_and_unmap_request(struct dwc3_ep *dep, |
Felipe Balbi | c91815b | 2018-03-26 13:14:47 +0300 | [diff] [blame] | 176 | struct dwc3_request *req, int status) |
| 177 | { |
| 178 | struct dwc3 *dwc = dep->dwc; |
| 179 | |
Felipe Balbi | c91815b | 2018-03-26 13:14:47 +0300 | [diff] [blame] | 180 | list_del(&req->list); |
| 181 | req->remaining = 0; |
Jack Pham | bd674224 | 2019-01-10 12:39:55 -0800 | [diff] [blame] | 182 | req->needs_extra_trb = false; |
Felipe Balbi | c91815b | 2018-03-26 13:14:47 +0300 | [diff] [blame] | 183 | |
| 184 | if (req->request.status == -EINPROGRESS) |
| 185 | req->request.status = status; |
| 186 | |
| 187 | if (req->trb) |
| 188 | usb_gadget_unmap_request_by_dev(dwc->sysdev, |
| 189 | &req->request, req->direction); |
| 190 | |
| 191 | req->trb = NULL; |
| 192 | trace_dwc3_gadget_giveback(req); |
| 193 | |
| 194 | if (dep->number > 1) |
| 195 | pm_runtime_put(dwc->dev); |
| 196 | } |
| 197 | |
Felipe Balbi | bfad65e | 2017-04-19 14:59:27 +0300 | [diff] [blame] | 198 | /** |
| 199 | * dwc3_gadget_giveback - call struct usb_request's ->complete callback |
| 200 | * @dep: The endpoint to whom the request belongs to |
| 201 | * @req: The request we're giving back |
| 202 | * @status: completion code for the request |
| 203 | * |
| 204 | * Must be called with controller's lock held and interrupts disabled. This |
| 205 | * function will unmap @req and call its ->complete() callback to notify upper |
| 206 | * layers that it has completed. |
| 207 | */ |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 208 | void dwc3_gadget_giveback(struct dwc3_ep *dep, struct dwc3_request *req, |
| 209 | int status) |
| 210 | { |
| 211 | struct dwc3 *dwc = dep->dwc; |
| 212 | |
Felipe Balbi | c91815b | 2018-03-26 13:14:47 +0300 | [diff] [blame] | 213 | dwc3_gadget_del_and_unmap_request(dep, req, status); |
Felipe Balbi | a3af5e3 | 2019-01-11 12:57:09 +0200 | [diff] [blame] | 214 | req->status = DWC3_REQUEST_STATUS_COMPLETED; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 215 | |
| 216 | spin_unlock(&dwc->lock); |
Michal Sojka | 304f7e5 | 2014-09-24 22:43:19 +0200 | [diff] [blame] | 217 | usb_gadget_giveback_request(&dep->endpoint, &req->request); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 218 | spin_lock(&dwc->lock); |
| 219 | } |
| 220 | |
Felipe Balbi | bfad65e | 2017-04-19 14:59:27 +0300 | [diff] [blame] | 221 | /** |
| 222 | * dwc3_send_gadget_generic_command - issue a generic command for the controller |
| 223 | * @dwc: pointer to the controller context |
| 224 | * @cmd: the command to be issued |
| 225 | * @param: command parameter |
| 226 | * |
| 227 | * Caller should take care of locking. Issue @cmd with a given @param to @dwc |
| 228 | * and wait for its completion. |
| 229 | */ |
Felipe Balbi | e319bd6 | 2020-08-13 08:35:38 +0300 | [diff] [blame] | 230 | int dwc3_send_gadget_generic_command(struct dwc3 *dwc, unsigned int cmd, |
| 231 | u32 param) |
Felipe Balbi | b09bb64 | 2012-04-24 16:19:11 +0300 | [diff] [blame] | 232 | { |
| 233 | u32 timeout = 500; |
Felipe Balbi | 71f7e70 | 2016-05-23 14:16:19 +0300 | [diff] [blame] | 234 | int status = 0; |
Felipe Balbi | 0fe886c | 2016-05-23 14:06:07 +0300 | [diff] [blame] | 235 | int ret = 0; |
Felipe Balbi | b09bb64 | 2012-04-24 16:19:11 +0300 | [diff] [blame] | 236 | u32 reg; |
| 237 | |
| 238 | dwc3_writel(dwc->regs, DWC3_DGCMDPAR, param); |
| 239 | dwc3_writel(dwc->regs, DWC3_DGCMD, cmd | DWC3_DGCMD_CMDACT); |
| 240 | |
| 241 | do { |
| 242 | reg = dwc3_readl(dwc->regs, DWC3_DGCMD); |
| 243 | if (!(reg & DWC3_DGCMD_CMDACT)) { |
Felipe Balbi | 71f7e70 | 2016-05-23 14:16:19 +0300 | [diff] [blame] | 244 | status = DWC3_DGCMD_STATUS(reg); |
| 245 | if (status) |
Felipe Balbi | 0fe886c | 2016-05-23 14:06:07 +0300 | [diff] [blame] | 246 | ret = -EINVAL; |
| 247 | break; |
Felipe Balbi | b09bb64 | 2012-04-24 16:19:11 +0300 | [diff] [blame] | 248 | } |
Janusz Dziedzic | e3aee48 | 2016-11-09 11:01:33 +0100 | [diff] [blame] | 249 | } while (--timeout); |
Felipe Balbi | 0fe886c | 2016-05-23 14:06:07 +0300 | [diff] [blame] | 250 | |
| 251 | if (!timeout) { |
Felipe Balbi | 0fe886c | 2016-05-23 14:06:07 +0300 | [diff] [blame] | 252 | ret = -ETIMEDOUT; |
Felipe Balbi | 71f7e70 | 2016-05-23 14:16:19 +0300 | [diff] [blame] | 253 | status = -ETIMEDOUT; |
Felipe Balbi | 0fe886c | 2016-05-23 14:06:07 +0300 | [diff] [blame] | 254 | } |
| 255 | |
Felipe Balbi | 71f7e70 | 2016-05-23 14:16:19 +0300 | [diff] [blame] | 256 | trace_dwc3_gadget_generic_cmd(cmd, param, status); |
| 257 | |
Felipe Balbi | 0fe886c | 2016-05-23 14:06:07 +0300 | [diff] [blame] | 258 | return ret; |
Felipe Balbi | b09bb64 | 2012-04-24 16:19:11 +0300 | [diff] [blame] | 259 | } |
| 260 | |
Felipe Balbi | c36d8e9 | 2016-04-04 12:46:33 +0300 | [diff] [blame] | 261 | static int __dwc3_gadget_wakeup(struct dwc3 *dwc); |
| 262 | |
Felipe Balbi | bfad65e | 2017-04-19 14:59:27 +0300 | [diff] [blame] | 263 | /** |
| 264 | * dwc3_send_gadget_ep_cmd - issue an endpoint command |
| 265 | * @dep: the endpoint to which the command is going to be issued |
| 266 | * @cmd: the command to be issued |
| 267 | * @params: parameters to the command |
| 268 | * |
| 269 | * Caller should handle locking. This function will issue @cmd with given |
| 270 | * @params to @dep and wait for its completion. |
| 271 | */ |
Felipe Balbi | e319bd6 | 2020-08-13 08:35:38 +0300 | [diff] [blame] | 272 | int dwc3_send_gadget_ep_cmd(struct dwc3_ep *dep, unsigned int cmd, |
Felipe Balbi | 2cd4718 | 2016-04-12 16:42:43 +0300 | [diff] [blame] | 273 | struct dwc3_gadget_ep_cmd_params *params) |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 274 | { |
Felipe Balbi | 8897a76 | 2016-09-22 10:56:08 +0300 | [diff] [blame] | 275 | const struct usb_endpoint_descriptor *desc = dep->endpoint.desc; |
Felipe Balbi | 2cd4718 | 2016-04-12 16:42:43 +0300 | [diff] [blame] | 276 | struct dwc3 *dwc = dep->dwc; |
Yu Chen | 1c0e69a | 2020-05-21 16:46:43 +0800 | [diff] [blame] | 277 | u32 timeout = 5000; |
Thinh Nguyen | 87dd961 | 2018-09-11 12:42:05 -0700 | [diff] [blame] | 278 | u32 saved_config = 0; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 279 | u32 reg; |
| 280 | |
Felipe Balbi | 0933df1 | 2016-05-23 14:02:33 +0300 | [diff] [blame] | 281 | int cmd_status = 0; |
Felipe Balbi | c0ca324 | 2016-04-04 09:11:51 +0300 | [diff] [blame] | 282 | int ret = -EINVAL; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 283 | |
Felipe Balbi | 2b0f11d | 2016-04-04 09:19:17 +0300 | [diff] [blame] | 284 | /* |
Thinh Nguyen | 87dd961 | 2018-09-11 12:42:05 -0700 | [diff] [blame] | 285 | * When operating in USB 2.0 speeds (HS/FS), if GUSB2PHYCFG.ENBLSLPM or |
| 286 | * GUSB2PHYCFG.SUSPHY is set, it must be cleared before issuing an |
| 287 | * endpoint command. |
Felipe Balbi | 2b0f11d | 2016-04-04 09:19:17 +0300 | [diff] [blame] | 288 | * |
Thinh Nguyen | 87dd961 | 2018-09-11 12:42:05 -0700 | [diff] [blame] | 289 | * Save and clear both GUSB2PHYCFG.ENBLSLPM and GUSB2PHYCFG.SUSPHY |
| 290 | * settings. Restore them after the command is completed. |
| 291 | * |
| 292 | * DWC_usb3 3.30a and DWC_usb31 1.90a programming guide section 3.2.2 |
Felipe Balbi | 2b0f11d | 2016-04-04 09:19:17 +0300 | [diff] [blame] | 293 | */ |
Peter Chen | e81a701 | 2020-08-21 10:55:48 +0800 | [diff] [blame] | 294 | if (dwc->gadget->speed <= USB_SPEED_HIGH) { |
Felipe Balbi | ab2a92e | 2016-05-17 14:55:58 +0300 | [diff] [blame] | 295 | reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0)); |
| 296 | if (unlikely(reg & DWC3_GUSB2PHYCFG_SUSPHY)) { |
Thinh Nguyen | 87dd961 | 2018-09-11 12:42:05 -0700 | [diff] [blame] | 297 | saved_config |= DWC3_GUSB2PHYCFG_SUSPHY; |
Felipe Balbi | ab2a92e | 2016-05-17 14:55:58 +0300 | [diff] [blame] | 298 | reg &= ~DWC3_GUSB2PHYCFG_SUSPHY; |
Felipe Balbi | ab2a92e | 2016-05-17 14:55:58 +0300 | [diff] [blame] | 299 | } |
Thinh Nguyen | 87dd961 | 2018-09-11 12:42:05 -0700 | [diff] [blame] | 300 | |
| 301 | if (reg & DWC3_GUSB2PHYCFG_ENBLSLPM) { |
| 302 | saved_config |= DWC3_GUSB2PHYCFG_ENBLSLPM; |
| 303 | reg &= ~DWC3_GUSB2PHYCFG_ENBLSLPM; |
| 304 | } |
| 305 | |
| 306 | if (saved_config) |
| 307 | dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg); |
Felipe Balbi | 2b0f11d | 2016-04-04 09:19:17 +0300 | [diff] [blame] | 308 | } |
| 309 | |
Felipe Balbi | 5999914 | 2016-09-22 12:25:28 +0300 | [diff] [blame] | 310 | if (DWC3_DEPCMD_CMD(cmd) == DWC3_DEPCMD_STARTTRANSFER) { |
Thinh Nguyen | b624b32 | 2021-04-19 19:11:12 -0700 | [diff] [blame] | 311 | int link_state; |
Felipe Balbi | c36d8e9 | 2016-04-04 12:46:33 +0300 | [diff] [blame] | 312 | |
Thinh Nguyen | 140e2df | 2021-10-25 16:35:06 -0700 | [diff] [blame] | 313 | /* |
| 314 | * Initiate remote wakeup if the link state is in U3 when |
| 315 | * operating in SS/SSP or L1/L2 when operating in HS/FS. If the |
| 316 | * link state is in U1/U2, no remote wakeup is needed. The Start |
| 317 | * Transfer command will initiate the link recovery. |
| 318 | */ |
Thinh Nguyen | b624b32 | 2021-04-19 19:11:12 -0700 | [diff] [blame] | 319 | link_state = dwc3_gadget_get_link_state(dwc); |
Thinh Nguyen | 140e2df | 2021-10-25 16:35:06 -0700 | [diff] [blame] | 320 | switch (link_state) { |
| 321 | case DWC3_LINK_STATE_U2: |
| 322 | if (dwc->gadget->speed >= USB_SPEED_SUPER) |
| 323 | break; |
| 324 | |
| 325 | fallthrough; |
| 326 | case DWC3_LINK_STATE_U3: |
Felipe Balbi | c36d8e9 | 2016-04-04 12:46:33 +0300 | [diff] [blame] | 327 | ret = __dwc3_gadget_wakeup(dwc); |
| 328 | dev_WARN_ONCE(dwc->dev, ret, "wakeup failed --> %d\n", |
| 329 | ret); |
Thinh Nguyen | 140e2df | 2021-10-25 16:35:06 -0700 | [diff] [blame] | 330 | break; |
Felipe Balbi | c36d8e9 | 2016-04-04 12:46:33 +0300 | [diff] [blame] | 331 | } |
| 332 | } |
| 333 | |
Felipe Balbi | 2eb8801 | 2016-04-12 16:53:39 +0300 | [diff] [blame] | 334 | dwc3_writel(dep->regs, DWC3_DEPCMDPAR0, params->param0); |
| 335 | dwc3_writel(dep->regs, DWC3_DEPCMDPAR1, params->param1); |
| 336 | dwc3_writel(dep->regs, DWC3_DEPCMDPAR2, params->param2); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 337 | |
Felipe Balbi | 8897a76 | 2016-09-22 10:56:08 +0300 | [diff] [blame] | 338 | /* |
| 339 | * Synopsys Databook 2.60a states in section 6.3.2.5.6 of that if we're |
| 340 | * not relying on XferNotReady, we can make use of a special "No |
| 341 | * Response Update Transfer" command where we should clear both CmdAct |
| 342 | * and CmdIOC bits. |
| 343 | * |
| 344 | * With this, we don't need to wait for command completion and can |
| 345 | * straight away issue further commands to the endpoint. |
| 346 | * |
| 347 | * NOTICE: We're making an assumption that control endpoints will never |
| 348 | * make use of Update Transfer command. This is a safe assumption |
| 349 | * because we can never have more than one request at a time with |
| 350 | * Control Endpoints. If anybody changes that assumption, this chunk |
| 351 | * needs to be updated accordingly. |
| 352 | */ |
| 353 | if (DWC3_DEPCMD_CMD(cmd) == DWC3_DEPCMD_UPDATETRANSFER && |
| 354 | !usb_endpoint_xfer_isoc(desc)) |
| 355 | cmd &= ~(DWC3_DEPCMD_CMDIOC | DWC3_DEPCMD_CMDACT); |
| 356 | else |
| 357 | cmd |= DWC3_DEPCMD_CMDACT; |
| 358 | |
| 359 | dwc3_writel(dep->regs, DWC3_DEPCMD, cmd); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 360 | do { |
Felipe Balbi | 2eb8801 | 2016-04-12 16:53:39 +0300 | [diff] [blame] | 361 | reg = dwc3_readl(dep->regs, DWC3_DEPCMD); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 362 | if (!(reg & DWC3_DEPCMD_CMDACT)) { |
Felipe Balbi | 0933df1 | 2016-05-23 14:02:33 +0300 | [diff] [blame] | 363 | cmd_status = DWC3_DEPCMD_STATUS(reg); |
Konrad Leszczynski | 7b9cc7a | 2016-02-12 15:21:46 +0000 | [diff] [blame] | 364 | |
Konrad Leszczynski | 7b9cc7a | 2016-02-12 15:21:46 +0000 | [diff] [blame] | 365 | switch (cmd_status) { |
| 366 | case 0: |
| 367 | ret = 0; |
Felipe Balbi | c0ca324 | 2016-04-04 09:11:51 +0300 | [diff] [blame] | 368 | break; |
Konrad Leszczynski | 7b9cc7a | 2016-02-12 15:21:46 +0000 | [diff] [blame] | 369 | case DEPEVT_TRANSFER_NO_RESOURCE: |
Thinh Nguyen | f7ac582e | 2020-03-29 16:13:16 -0700 | [diff] [blame] | 370 | dev_WARN(dwc->dev, "No resource for %s\n", |
| 371 | dep->name); |
Konrad Leszczynski | 7b9cc7a | 2016-02-12 15:21:46 +0000 | [diff] [blame] | 372 | ret = -EINVAL; |
| 373 | break; |
| 374 | case DEPEVT_TRANSFER_BUS_EXPIRY: |
| 375 | /* |
| 376 | * SW issues START TRANSFER command to |
| 377 | * isochronous ep with future frame interval. If |
| 378 | * future interval time has already passed when |
| 379 | * core receives the command, it will respond |
| 380 | * with an error status of 'Bus Expiry'. |
| 381 | * |
| 382 | * Instead of always returning -EINVAL, let's |
| 383 | * give a hint to the gadget driver that this is |
| 384 | * the case by returning -EAGAIN. |
| 385 | */ |
Konrad Leszczynski | 7b9cc7a | 2016-02-12 15:21:46 +0000 | [diff] [blame] | 386 | ret = -EAGAIN; |
| 387 | break; |
| 388 | default: |
| 389 | dev_WARN(dwc->dev, "UNKNOWN cmd status\n"); |
| 390 | } |
| 391 | |
Felipe Balbi | c0ca324 | 2016-04-04 09:11:51 +0300 | [diff] [blame] | 392 | break; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 393 | } |
Felipe Balbi | f6bb225 | 2016-05-23 13:53:34 +0300 | [diff] [blame] | 394 | } while (--timeout); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 395 | |
Felipe Balbi | f6bb225 | 2016-05-23 13:53:34 +0300 | [diff] [blame] | 396 | if (timeout == 0) { |
Felipe Balbi | f6bb225 | 2016-05-23 13:53:34 +0300 | [diff] [blame] | 397 | ret = -ETIMEDOUT; |
Felipe Balbi | 0933df1 | 2016-05-23 14:02:33 +0300 | [diff] [blame] | 398 | cmd_status = -ETIMEDOUT; |
Felipe Balbi | f6bb225 | 2016-05-23 13:53:34 +0300 | [diff] [blame] | 399 | } |
Felipe Balbi | c0ca324 | 2016-04-04 09:11:51 +0300 | [diff] [blame] | 400 | |
Felipe Balbi | 0933df1 | 2016-05-23 14:02:33 +0300 | [diff] [blame] | 401 | trace_dwc3_gadget_ep_cmd(dep, cmd, params, cmd_status); |
| 402 | |
Thinh Nguyen | 9bc3395 | 2020-03-29 16:13:04 -0700 | [diff] [blame] | 403 | if (DWC3_DEPCMD_CMD(cmd) == DWC3_DEPCMD_STARTTRANSFER) { |
| 404 | if (ret == 0) |
| 405 | dep->flags |= DWC3_EP_TRANSFER_STARTED; |
| 406 | |
| 407 | if (ret != -ETIMEDOUT) |
| 408 | dwc3_gadget_ep_get_transfer_index(dep); |
Felipe Balbi | 6cb2e4e | 2016-10-21 13:07:09 +0300 | [diff] [blame] | 409 | } |
| 410 | |
Thinh Nguyen | 87dd961 | 2018-09-11 12:42:05 -0700 | [diff] [blame] | 411 | if (saved_config) { |
Felipe Balbi | 2b0f11d | 2016-04-04 09:19:17 +0300 | [diff] [blame] | 412 | reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0)); |
Thinh Nguyen | 87dd961 | 2018-09-11 12:42:05 -0700 | [diff] [blame] | 413 | reg |= saved_config; |
Felipe Balbi | 2b0f11d | 2016-04-04 09:19:17 +0300 | [diff] [blame] | 414 | dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg); |
| 415 | } |
| 416 | |
Felipe Balbi | c0ca324 | 2016-04-04 09:11:51 +0300 | [diff] [blame] | 417 | return ret; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 418 | } |
| 419 | |
John Youn | 50c763f | 2016-05-31 17:49:56 -0700 | [diff] [blame] | 420 | static int dwc3_send_clear_stall_ep_cmd(struct dwc3_ep *dep) |
| 421 | { |
| 422 | struct dwc3 *dwc = dep->dwc; |
| 423 | struct dwc3_gadget_ep_cmd_params params; |
| 424 | u32 cmd = DWC3_DEPCMD_CLEARSTALL; |
| 425 | |
| 426 | /* |
| 427 | * As of core revision 2.60a the recommended programming model |
| 428 | * is to set the ClearPendIN bit when issuing a Clear Stall EP |
| 429 | * command for IN endpoints. This is to prevent an issue where |
| 430 | * some (non-compliant) hosts may not send ACK TPs for pending |
| 431 | * IN transfers due to a mishandled error condition. Synopsys |
| 432 | * STAR 9000614252. |
| 433 | */ |
Thinh Nguyen | 9af21dd | 2020-04-11 19:20:01 -0700 | [diff] [blame] | 434 | if (dep->direction && |
| 435 | !DWC3_VER_IS_PRIOR(DWC3, 260A) && |
Peter Chen | e81a701 | 2020-08-21 10:55:48 +0800 | [diff] [blame] | 436 | (dwc->gadget->speed >= USB_SPEED_SUPER)) |
John Youn | 50c763f | 2016-05-31 17:49:56 -0700 | [diff] [blame] | 437 | cmd |= DWC3_DEPCMD_CLEARPENDIN; |
| 438 | |
| 439 | memset(¶ms, 0, sizeof(params)); |
| 440 | |
Felipe Balbi | 2cd4718 | 2016-04-12 16:42:43 +0300 | [diff] [blame] | 441 | return dwc3_send_gadget_ep_cmd(dep, cmd, ¶ms); |
John Youn | 50c763f | 2016-05-31 17:49:56 -0700 | [diff] [blame] | 442 | } |
| 443 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 444 | static dma_addr_t dwc3_trb_dma_offset(struct dwc3_ep *dep, |
Felipe Balbi | f6bafc6 | 2012-02-06 11:04:53 +0200 | [diff] [blame] | 445 | struct dwc3_trb *trb) |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 446 | { |
Paul Zimmerman | c439ef8 | 2011-09-30 10:58:45 +0300 | [diff] [blame] | 447 | u32 offset = (char *) trb - (char *) dep->trb_pool; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 448 | |
| 449 | return dep->trb_pool_dma + offset; |
| 450 | } |
| 451 | |
| 452 | static int dwc3_alloc_trb_pool(struct dwc3_ep *dep) |
| 453 | { |
| 454 | struct dwc3 *dwc = dep->dwc; |
| 455 | |
| 456 | if (dep->trb_pool) |
| 457 | return 0; |
| 458 | |
Arnd Bergmann | d64ff40 | 2016-11-17 17:13:47 +0530 | [diff] [blame] | 459 | dep->trb_pool = dma_alloc_coherent(dwc->sysdev, |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 460 | sizeof(struct dwc3_trb) * DWC3_TRB_NUM, |
| 461 | &dep->trb_pool_dma, GFP_KERNEL); |
| 462 | if (!dep->trb_pool) { |
| 463 | dev_err(dep->dwc->dev, "failed to allocate trb pool for %s\n", |
| 464 | dep->name); |
| 465 | return -ENOMEM; |
| 466 | } |
| 467 | |
| 468 | return 0; |
| 469 | } |
| 470 | |
| 471 | static void dwc3_free_trb_pool(struct dwc3_ep *dep) |
| 472 | { |
| 473 | struct dwc3 *dwc = dep->dwc; |
| 474 | |
Arnd Bergmann | d64ff40 | 2016-11-17 17:13:47 +0530 | [diff] [blame] | 475 | dma_free_coherent(dwc->sysdev, sizeof(struct dwc3_trb) * DWC3_TRB_NUM, |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 476 | dep->trb_pool, dep->trb_pool_dma); |
| 477 | |
| 478 | dep->trb_pool = NULL; |
| 479 | dep->trb_pool_dma = 0; |
| 480 | } |
| 481 | |
Felipe Balbi | 20d1d43 | 2018-04-09 12:49:02 +0300 | [diff] [blame] | 482 | static int dwc3_gadget_set_xfer_resource(struct dwc3_ep *dep) |
| 483 | { |
| 484 | struct dwc3_gadget_ep_cmd_params params; |
| 485 | |
| 486 | memset(¶ms, 0x00, sizeof(params)); |
| 487 | |
| 488 | params.param0 = DWC3_DEPXFERCFG_NUM_XFER_RES(1); |
| 489 | |
| 490 | return dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETTRANSFRESOURCE, |
| 491 | ¶ms); |
| 492 | } |
John Youn | c450960 | 2016-02-16 20:10:53 -0800 | [diff] [blame] | 493 | |
| 494 | /** |
Felipe Balbi | bfad65e | 2017-04-19 14:59:27 +0300 | [diff] [blame] | 495 | * dwc3_gadget_start_config - configure ep resources |
John Youn | c450960 | 2016-02-16 20:10:53 -0800 | [diff] [blame] | 496 | * @dep: endpoint that is being enabled |
| 497 | * |
Felipe Balbi | bfad65e | 2017-04-19 14:59:27 +0300 | [diff] [blame] | 498 | * Issue a %DWC3_DEPCMD_DEPSTARTCFG command to @dep. After the command's |
| 499 | * completion, it will set Transfer Resource for all available endpoints. |
John Youn | c450960 | 2016-02-16 20:10:53 -0800 | [diff] [blame] | 500 | * |
Felipe Balbi | bfad65e | 2017-04-19 14:59:27 +0300 | [diff] [blame] | 501 | * The assignment of transfer resources cannot perfectly follow the data book |
| 502 | * due to the fact that the controller driver does not have all knowledge of the |
| 503 | * configuration in advance. It is given this information piecemeal by the |
| 504 | * composite gadget framework after every SET_CONFIGURATION and |
| 505 | * SET_INTERFACE. Trying to follow the databook programming model in this |
| 506 | * scenario can cause errors. For two reasons: |
John Youn | c450960 | 2016-02-16 20:10:53 -0800 | [diff] [blame] | 507 | * |
Felipe Balbi | bfad65e | 2017-04-19 14:59:27 +0300 | [diff] [blame] | 508 | * 1) The databook says to do %DWC3_DEPCMD_DEPSTARTCFG for every |
| 509 | * %USB_REQ_SET_CONFIGURATION and %USB_REQ_SET_INTERFACE (8.1.5). This is |
| 510 | * incorrect in the scenario of multiple interfaces. |
| 511 | * |
| 512 | * 2) The databook does not mention doing more %DWC3_DEPCMD_DEPXFERCFG for new |
John Youn | c450960 | 2016-02-16 20:10:53 -0800 | [diff] [blame] | 513 | * endpoint on alt setting (8.1.6). |
| 514 | * |
| 515 | * The following simplified method is used instead: |
| 516 | * |
Felipe Balbi | bfad65e | 2017-04-19 14:59:27 +0300 | [diff] [blame] | 517 | * All hardware endpoints can be assigned a transfer resource and this setting |
| 518 | * will stay persistent until either a core reset or hibernation. So whenever we |
| 519 | * do a %DWC3_DEPCMD_DEPSTARTCFG(0) we can go ahead and do |
| 520 | * %DWC3_DEPCMD_DEPXFERCFG for every hardware endpoint as well. We are |
John Youn | c450960 | 2016-02-16 20:10:53 -0800 | [diff] [blame] | 521 | * guaranteed that there are as many transfer resources as endpoints. |
| 522 | * |
Felipe Balbi | bfad65e | 2017-04-19 14:59:27 +0300 | [diff] [blame] | 523 | * This function is called for each endpoint when it is being enabled but is |
| 524 | * triggered only when called for EP0-out, which always happens first, and which |
| 525 | * should only happen in one of the above conditions. |
John Youn | c450960 | 2016-02-16 20:10:53 -0800 | [diff] [blame] | 526 | */ |
Felipe Balbi | b07c2db | 2018-04-09 12:46:47 +0300 | [diff] [blame] | 527 | static int dwc3_gadget_start_config(struct dwc3_ep *dep) |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 528 | { |
| 529 | struct dwc3_gadget_ep_cmd_params params; |
Felipe Balbi | b07c2db | 2018-04-09 12:46:47 +0300 | [diff] [blame] | 530 | struct dwc3 *dwc; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 531 | u32 cmd; |
John Youn | c450960 | 2016-02-16 20:10:53 -0800 | [diff] [blame] | 532 | int i; |
| 533 | int ret; |
| 534 | |
| 535 | if (dep->number) |
| 536 | return 0; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 537 | |
| 538 | memset(¶ms, 0x00, sizeof(params)); |
John Youn | c450960 | 2016-02-16 20:10:53 -0800 | [diff] [blame] | 539 | cmd = DWC3_DEPCMD_DEPSTARTCFG; |
Felipe Balbi | b07c2db | 2018-04-09 12:46:47 +0300 | [diff] [blame] | 540 | dwc = dep->dwc; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 541 | |
Felipe Balbi | 2cd4718 | 2016-04-12 16:42:43 +0300 | [diff] [blame] | 542 | ret = dwc3_send_gadget_ep_cmd(dep, cmd, ¶ms); |
John Youn | c450960 | 2016-02-16 20:10:53 -0800 | [diff] [blame] | 543 | if (ret) |
| 544 | return ret; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 545 | |
John Youn | c450960 | 2016-02-16 20:10:53 -0800 | [diff] [blame] | 546 | for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) { |
| 547 | struct dwc3_ep *dep = dwc->eps[i]; |
| 548 | |
| 549 | if (!dep) |
| 550 | continue; |
| 551 | |
Felipe Balbi | b07c2db | 2018-04-09 12:46:47 +0300 | [diff] [blame] | 552 | ret = dwc3_gadget_set_xfer_resource(dep); |
John Youn | c450960 | 2016-02-16 20:10:53 -0800 | [diff] [blame] | 553 | if (ret) |
| 554 | return ret; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 555 | } |
| 556 | |
| 557 | return 0; |
| 558 | } |
| 559 | |
Felipe Balbi | b07c2db | 2018-04-09 12:46:47 +0300 | [diff] [blame] | 560 | static int dwc3_gadget_set_ep_config(struct dwc3_ep *dep, unsigned int action) |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 561 | { |
John Youn | 39ebb05 | 2016-11-09 16:36:28 -0800 | [diff] [blame] | 562 | const struct usb_ss_ep_comp_descriptor *comp_desc; |
| 563 | const struct usb_endpoint_descriptor *desc; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 564 | struct dwc3_gadget_ep_cmd_params params; |
Felipe Balbi | b07c2db | 2018-04-09 12:46:47 +0300 | [diff] [blame] | 565 | struct dwc3 *dwc = dep->dwc; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 566 | |
John Youn | 39ebb05 | 2016-11-09 16:36:28 -0800 | [diff] [blame] | 567 | comp_desc = dep->endpoint.comp_desc; |
| 568 | desc = dep->endpoint.desc; |
| 569 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 570 | memset(¶ms, 0x00, sizeof(params)); |
| 571 | |
Felipe Balbi | dc1c70a | 2011-09-30 10:58:51 +0300 | [diff] [blame] | 572 | params.param0 = DWC3_DEPCFG_EP_TYPE(usb_endpoint_type(desc)) |
Chanho Park | d2e9a13 | 2012-08-31 16:54:07 +0900 | [diff] [blame] | 573 | | DWC3_DEPCFG_MAX_PACKET_SIZE(usb_endpoint_maxp(desc)); |
| 574 | |
| 575 | /* Burst size is only needed in SuperSpeed mode */ |
Peter Chen | e81a701 | 2020-08-21 10:55:48 +0800 | [diff] [blame] | 576 | if (dwc->gadget->speed >= USB_SPEED_SUPER) { |
Felipe Balbi | 676e349 | 2016-04-26 10:49:07 +0300 | [diff] [blame] | 577 | u32 burst = dep->endpoint.maxburst; |
Felipe Balbi | e319bd6 | 2020-08-13 08:35:38 +0300 | [diff] [blame] | 578 | |
Felipe Balbi | 676e349 | 2016-04-26 10:49:07 +0300 | [diff] [blame] | 579 | params.param0 |= DWC3_DEPCFG_BURST_SIZE(burst - 1); |
Chanho Park | d2e9a13 | 2012-08-31 16:54:07 +0900 | [diff] [blame] | 580 | } |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 581 | |
Felipe Balbi | a2d23f0 | 2018-04-09 12:40:48 +0300 | [diff] [blame] | 582 | params.param0 |= action; |
| 583 | if (action == DWC3_DEPCFG_ACTION_RESTORE) |
Paul Zimmerman | 265b70a | 2013-12-19 12:38:49 -0600 | [diff] [blame] | 584 | params.param2 |= dep->saved_state; |
Paul Zimmerman | 265b70a | 2013-12-19 12:38:49 -0600 | [diff] [blame] | 585 | |
Felipe Balbi | 4bc48c9 | 2016-08-10 16:04:33 +0300 | [diff] [blame] | 586 | if (usb_endpoint_xfer_control(desc)) |
| 587 | params.param1 = DWC3_DEPCFG_XFER_COMPLETE_EN; |
Felipe Balbi | 13fa2e6 | 2016-05-30 13:40:00 +0300 | [diff] [blame] | 588 | |
| 589 | if (dep->number <= 1 || usb_endpoint_xfer_isoc(desc)) |
| 590 | params.param1 |= DWC3_DEPCFG_XFER_NOT_READY_EN; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 591 | |
Felipe Balbi | 18b7ede | 2012-01-02 13:35:41 +0200 | [diff] [blame] | 592 | if (usb_ss_max_streams(comp_desc) && usb_endpoint_xfer_bulk(desc)) { |
Felipe Balbi | dc1c70a | 2011-09-30 10:58:51 +0300 | [diff] [blame] | 593 | params.param1 |= DWC3_DEPCFG_STREAM_CAPABLE |
Thinh Nguyen | 548f8b3 | 2020-05-05 19:46:45 -0700 | [diff] [blame] | 594 | | DWC3_DEPCFG_XFER_COMPLETE_EN |
Felipe Balbi | dc1c70a | 2011-09-30 10:58:51 +0300 | [diff] [blame] | 595 | | DWC3_DEPCFG_STREAM_EVENT_EN; |
Felipe Balbi | 879631a | 2011-09-30 10:58:47 +0300 | [diff] [blame] | 596 | dep->stream_capable = true; |
| 597 | } |
| 598 | |
Felipe Balbi | 0b93a4c | 2014-09-04 10:28:10 -0500 | [diff] [blame] | 599 | if (!usb_endpoint_xfer_control(desc)) |
Felipe Balbi | dc1c70a | 2011-09-30 10:58:51 +0300 | [diff] [blame] | 600 | params.param1 |= DWC3_DEPCFG_XFER_IN_PROGRESS_EN; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 601 | |
| 602 | /* |
| 603 | * We are doing 1:1 mapping for endpoints, meaning |
| 604 | * Physical Endpoints 2 maps to Logical Endpoint 2 and |
| 605 | * so on. We consider the direction bit as part of the physical |
| 606 | * endpoint number. So USB endpoint 0x81 is 0x03. |
| 607 | */ |
Felipe Balbi | dc1c70a | 2011-09-30 10:58:51 +0300 | [diff] [blame] | 608 | params.param1 |= DWC3_DEPCFG_EP_NUMBER(dep->number); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 609 | |
| 610 | /* |
| 611 | * We must use the lower 16 TX FIFOs even though |
| 612 | * HW might have more |
| 613 | */ |
| 614 | if (dep->direction) |
Felipe Balbi | dc1c70a | 2011-09-30 10:58:51 +0300 | [diff] [blame] | 615 | params.param0 |= DWC3_DEPCFG_FIFO_NUMBER(dep->number >> 1); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 616 | |
| 617 | if (desc->bInterval) { |
Thinh Nguyen | 6b78b38 | 2021-02-08 13:53:10 -0800 | [diff] [blame] | 618 | u8 bInterval_m1; |
| 619 | |
| 620 | /* |
Thinh Nguyen | f9ddfaa | 2021-04-15 00:41:58 -0700 | [diff] [blame] | 621 | * Valid range for DEPCFG.bInterval_m1 is from 0 to 13. |
| 622 | * |
| 623 | * NOTE: The programming guide incorrectly stated bInterval_m1 |
| 624 | * must be set to 0 when operating in fullspeed. Internally the |
| 625 | * controller does not have this limitation. See DWC_usb3x |
| 626 | * programming guide section 3.2.2.1. |
Thinh Nguyen | 6b78b38 | 2021-02-08 13:53:10 -0800 | [diff] [blame] | 627 | */ |
| 628 | bInterval_m1 = min_t(u8, desc->bInterval - 1, 13); |
Thinh Nguyen | 6b78b38 | 2021-02-08 13:53:10 -0800 | [diff] [blame] | 629 | |
Thinh Nguyen | 5b4cd96 | 2021-02-08 13:53:16 -0800 | [diff] [blame] | 630 | if (usb_endpoint_type(desc) == USB_ENDPOINT_XFER_INT && |
| 631 | dwc->gadget->speed == USB_SPEED_FULL) |
| 632 | dep->interval = desc->bInterval; |
| 633 | else |
| 634 | dep->interval = 1 << (desc->bInterval - 1); |
| 635 | |
Thinh Nguyen | 6b78b38 | 2021-02-08 13:53:10 -0800 | [diff] [blame] | 636 | params.param1 |= DWC3_DEPCFG_BINTERVAL_M1(bInterval_m1); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 637 | } |
| 638 | |
Felipe Balbi | 2cd4718 | 2016-04-12 16:42:43 +0300 | [diff] [blame] | 639 | return dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETEPCONFIG, ¶ms); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 640 | } |
| 641 | |
Thinh Nguyen | 140ca4c | 2020-05-05 19:47:09 -0700 | [diff] [blame] | 642 | static void dwc3_stop_active_transfer(struct dwc3_ep *dep, bool force, |
| 643 | bool interrupt); |
| 644 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 645 | /** |
Felipe Balbi | bfad65e | 2017-04-19 14:59:27 +0300 | [diff] [blame] | 646 | * __dwc3_gadget_ep_enable - initializes a hw endpoint |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 647 | * @dep: endpoint to be initialized |
Felipe Balbi | a2d23f0 | 2018-04-09 12:40:48 +0300 | [diff] [blame] | 648 | * @action: one of INIT, MODIFY or RESTORE |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 649 | * |
Felipe Balbi | bfad65e | 2017-04-19 14:59:27 +0300 | [diff] [blame] | 650 | * Caller should take care of locking. Execute all necessary commands to |
| 651 | * initialize a HW endpoint so it can be used by a gadget driver. |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 652 | */ |
Felipe Balbi | a2d23f0 | 2018-04-09 12:40:48 +0300 | [diff] [blame] | 653 | static int __dwc3_gadget_ep_enable(struct dwc3_ep *dep, unsigned int action) |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 654 | { |
John Youn | 39ebb05 | 2016-11-09 16:36:28 -0800 | [diff] [blame] | 655 | const struct usb_endpoint_descriptor *desc = dep->endpoint.desc; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 656 | struct dwc3 *dwc = dep->dwc; |
John Youn | 39ebb05 | 2016-11-09 16:36:28 -0800 | [diff] [blame] | 657 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 658 | u32 reg; |
Andy Shevchenko | b09e99e | 2014-05-15 15:53:32 +0300 | [diff] [blame] | 659 | int ret; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 660 | |
| 661 | if (!(dep->flags & DWC3_EP_ENABLED)) { |
Felipe Balbi | b07c2db | 2018-04-09 12:46:47 +0300 | [diff] [blame] | 662 | ret = dwc3_gadget_start_config(dep); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 663 | if (ret) |
| 664 | return ret; |
| 665 | } |
| 666 | |
Felipe Balbi | b07c2db | 2018-04-09 12:46:47 +0300 | [diff] [blame] | 667 | ret = dwc3_gadget_set_ep_config(dep, action); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 668 | if (ret) |
| 669 | return ret; |
| 670 | |
| 671 | if (!(dep->flags & DWC3_EP_ENABLED)) { |
Felipe Balbi | f6bafc6 | 2012-02-06 11:04:53 +0200 | [diff] [blame] | 672 | struct dwc3_trb *trb_st_hw; |
| 673 | struct dwc3_trb *trb_link; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 674 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 675 | dep->type = usb_endpoint_type(desc); |
| 676 | dep->flags |= DWC3_EP_ENABLED; |
| 677 | |
| 678 | reg = dwc3_readl(dwc->regs, DWC3_DALEPENA); |
| 679 | reg |= DWC3_DALEPENA_EP(dep->number); |
| 680 | dwc3_writel(dwc->regs, DWC3_DALEPENA, reg); |
| 681 | |
Felipe Balbi | 36b68aa | 2016-04-05 13:24:36 +0300 | [diff] [blame] | 682 | if (usb_endpoint_xfer_control(desc)) |
Felipe Balbi | 2870e50 | 2016-11-03 13:53:29 +0200 | [diff] [blame] | 683 | goto out; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 684 | |
John Youn | 0d25744 | 2016-05-19 17:26:08 -0700 | [diff] [blame] | 685 | /* Initialize the TRB ring */ |
| 686 | dep->trb_dequeue = 0; |
| 687 | dep->trb_enqueue = 0; |
| 688 | memset(dep->trb_pool, 0, |
| 689 | sizeof(struct dwc3_trb) * DWC3_TRB_NUM); |
| 690 | |
Felipe Balbi | 36b68aa | 2016-04-05 13:24:36 +0300 | [diff] [blame] | 691 | /* Link TRB. The HWO bit is never reset */ |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 692 | trb_st_hw = &dep->trb_pool[0]; |
| 693 | |
Felipe Balbi | f6bafc6 | 2012-02-06 11:04:53 +0200 | [diff] [blame] | 694 | trb_link = &dep->trb_pool[DWC3_TRB_NUM - 1]; |
Felipe Balbi | f6bafc6 | 2012-02-06 11:04:53 +0200 | [diff] [blame] | 695 | trb_link->bpl = lower_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw)); |
| 696 | trb_link->bph = upper_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw)); |
| 697 | trb_link->ctrl |= DWC3_TRBCTL_LINK_TRB; |
| 698 | trb_link->ctrl |= DWC3_TRB_CTRL_HWO; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 699 | } |
| 700 | |
Felipe Balbi | a97ea99 | 2016-09-29 16:28:56 +0300 | [diff] [blame] | 701 | /* |
| 702 | * Issue StartTransfer here with no-op TRB so we can always rely on No |
| 703 | * Response Update Transfer command. |
| 704 | */ |
Thinh Nguyen | 140ca4c | 2020-05-05 19:47:09 -0700 | [diff] [blame] | 705 | if (usb_endpoint_xfer_bulk(desc) || |
Felipe Balbi | 52fcc0b | 2018-03-26 13:19:43 +0300 | [diff] [blame] | 706 | usb_endpoint_xfer_int(desc)) { |
Felipe Balbi | a97ea99 | 2016-09-29 16:28:56 +0300 | [diff] [blame] | 707 | struct dwc3_gadget_ep_cmd_params params; |
| 708 | struct dwc3_trb *trb; |
| 709 | dma_addr_t trb_dma; |
| 710 | u32 cmd; |
| 711 | |
| 712 | memset(¶ms, 0, sizeof(params)); |
| 713 | trb = &dep->trb_pool[0]; |
| 714 | trb_dma = dwc3_trb_dma_offset(dep, trb); |
| 715 | |
| 716 | params.param0 = upper_32_bits(trb_dma); |
| 717 | params.param1 = lower_32_bits(trb_dma); |
| 718 | |
| 719 | cmd = DWC3_DEPCMD_STARTTRANSFER; |
| 720 | |
| 721 | ret = dwc3_send_gadget_ep_cmd(dep, cmd, ¶ms); |
| 722 | if (ret < 0) |
| 723 | return ret; |
Thinh Nguyen | 140ca4c | 2020-05-05 19:47:09 -0700 | [diff] [blame] | 724 | |
| 725 | if (dep->stream_capable) { |
| 726 | /* |
| 727 | * For streams, at start, there maybe a race where the |
| 728 | * host primes the endpoint before the function driver |
| 729 | * queues a request to initiate a stream. In that case, |
| 730 | * the controller will not see the prime to generate the |
| 731 | * ERDY and start stream. To workaround this, issue a |
| 732 | * no-op TRB as normal, but end it immediately. As a |
| 733 | * result, when the function driver queues the request, |
| 734 | * the next START_TRANSFER command will cause the |
| 735 | * controller to generate an ERDY to initiate the |
| 736 | * stream. |
| 737 | */ |
| 738 | dwc3_stop_active_transfer(dep, true, true); |
| 739 | |
| 740 | /* |
| 741 | * All stream eps will reinitiate stream on NoStream |
| 742 | * rejection until we can determine that the host can |
| 743 | * prime after the first transfer. |
| 744 | */ |
| 745 | dep->flags |= DWC3_EP_FORCE_RESTART_STREAM; |
| 746 | } |
Felipe Balbi | a97ea99 | 2016-09-29 16:28:56 +0300 | [diff] [blame] | 747 | } |
| 748 | |
Felipe Balbi | 2870e50 | 2016-11-03 13:53:29 +0200 | [diff] [blame] | 749 | out: |
| 750 | trace_dwc3_gadget_ep_enable(dep); |
| 751 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 752 | return 0; |
| 753 | } |
| 754 | |
Sebastian Andrzej Siewior | 624407f | 2011-08-29 13:56:37 +0200 | [diff] [blame] | 755 | static void dwc3_remove_requests(struct dwc3 *dwc, struct dwc3_ep *dep) |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 756 | { |
| 757 | struct dwc3_request *req; |
| 758 | |
Felipe Balbi | c5353b2 | 2019-02-13 13:00:54 +0200 | [diff] [blame] | 759 | dwc3_stop_active_transfer(dep, true, false); |
Felipe Balbi | 69450c4 | 2016-05-30 13:37:02 +0300 | [diff] [blame] | 760 | |
Felipe Balbi | 0e14602 | 2016-06-21 10:32:02 +0300 | [diff] [blame] | 761 | /* - giveback all requests to gadget driver */ |
| 762 | while (!list_empty(&dep->started_list)) { |
| 763 | req = next_request(&dep->started_list); |
Sebastian Andrzej Siewior | 624407f | 2011-08-29 13:56:37 +0200 | [diff] [blame] | 764 | |
Felipe Balbi | 0e14602 | 2016-06-21 10:32:02 +0300 | [diff] [blame] | 765 | dwc3_gadget_giveback(dep, req, -ESHUTDOWN); |
Felipe Balbi | ea53b88 | 2012-02-17 12:10:04 +0200 | [diff] [blame] | 766 | } |
| 767 | |
Felipe Balbi | aa3342c | 2016-03-14 11:01:31 +0200 | [diff] [blame] | 768 | while (!list_empty(&dep->pending_list)) { |
| 769 | req = next_request(&dep->pending_list); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 770 | |
Sebastian Andrzej Siewior | 624407f | 2011-08-29 13:56:37 +0200 | [diff] [blame] | 771 | dwc3_gadget_giveback(dep, req, -ESHUTDOWN); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 772 | } |
Felipe Balbi | d8eca64 | 2019-10-31 11:07:13 +0200 | [diff] [blame] | 773 | |
| 774 | while (!list_empty(&dep->cancelled_list)) { |
| 775 | req = next_request(&dep->cancelled_list); |
| 776 | |
| 777 | dwc3_gadget_giveback(dep, req, -ESHUTDOWN); |
| 778 | } |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 779 | } |
| 780 | |
| 781 | /** |
Felipe Balbi | bfad65e | 2017-04-19 14:59:27 +0300 | [diff] [blame] | 782 | * __dwc3_gadget_ep_disable - disables a hw endpoint |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 783 | * @dep: the endpoint to disable |
| 784 | * |
Felipe Balbi | bfad65e | 2017-04-19 14:59:27 +0300 | [diff] [blame] | 785 | * This function undoes what __dwc3_gadget_ep_enable did and also removes |
| 786 | * requests which are currently being processed by the hardware and those which |
| 787 | * are not yet scheduled. |
| 788 | * |
Sebastian Andrzej Siewior | 624407f | 2011-08-29 13:56:37 +0200 | [diff] [blame] | 789 | * Caller should take care of locking. |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 790 | */ |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 791 | static int __dwc3_gadget_ep_disable(struct dwc3_ep *dep) |
| 792 | { |
| 793 | struct dwc3 *dwc = dep->dwc; |
| 794 | u32 reg; |
| 795 | |
Felipe Balbi | 2870e50 | 2016-11-03 13:53:29 +0200 | [diff] [blame] | 796 | trace_dwc3_gadget_ep_disable(dep); |
Felipe Balbi | 7eaeac5 | 2015-07-20 14:46:15 -0500 | [diff] [blame] | 797 | |
Felipe Balbi | 687ef98 | 2014-04-16 10:30:33 -0500 | [diff] [blame] | 798 | /* make sure HW endpoint isn't stalled */ |
| 799 | if (dep->flags & DWC3_EP_STALL) |
Felipe Balbi | 7a60855 | 2014-09-24 14:19:52 -0500 | [diff] [blame] | 800 | __dwc3_gadget_ep_set_halt(dep, 0, false); |
Felipe Balbi | 687ef98 | 2014-04-16 10:30:33 -0500 | [diff] [blame] | 801 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 802 | reg = dwc3_readl(dwc->regs, DWC3_DALEPENA); |
| 803 | reg &= ~DWC3_DALEPENA_EP(dep->number); |
| 804 | dwc3_writel(dwc->regs, DWC3_DALEPENA, reg); |
| 805 | |
John Youn | 39ebb05 | 2016-11-09 16:36:28 -0800 | [diff] [blame] | 806 | /* Clear out the ep descriptors for non-ep0 */ |
| 807 | if (dep->number > 1) { |
| 808 | dep->endpoint.comp_desc = NULL; |
| 809 | dep->endpoint.desc = NULL; |
| 810 | } |
| 811 | |
Wesley Cheng | c7bb96a | 2021-03-11 15:59:02 -0800 | [diff] [blame] | 812 | dwc3_remove_requests(dwc, dep); |
| 813 | |
Wesley Cheng | 996a578 | 2021-03-24 11:31:04 -0700 | [diff] [blame] | 814 | dep->stream_capable = false; |
| 815 | dep->type = 0; |
| 816 | dep->flags = 0; |
| 817 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 818 | return 0; |
| 819 | } |
| 820 | |
| 821 | /* -------------------------------------------------------------------------- */ |
| 822 | |
| 823 | static int dwc3_gadget_ep0_enable(struct usb_ep *ep, |
| 824 | const struct usb_endpoint_descriptor *desc) |
| 825 | { |
| 826 | return -EINVAL; |
| 827 | } |
| 828 | |
| 829 | static int dwc3_gadget_ep0_disable(struct usb_ep *ep) |
| 830 | { |
| 831 | return -EINVAL; |
| 832 | } |
| 833 | |
| 834 | /* -------------------------------------------------------------------------- */ |
| 835 | |
| 836 | static int dwc3_gadget_ep_enable(struct usb_ep *ep, |
| 837 | const struct usb_endpoint_descriptor *desc) |
| 838 | { |
| 839 | struct dwc3_ep *dep; |
| 840 | struct dwc3 *dwc; |
| 841 | unsigned long flags; |
| 842 | int ret; |
| 843 | |
| 844 | if (!ep || !desc || desc->bDescriptorType != USB_DT_ENDPOINT) { |
| 845 | pr_debug("dwc3: invalid parameters\n"); |
| 846 | return -EINVAL; |
| 847 | } |
| 848 | |
| 849 | if (!desc->wMaxPacketSize) { |
| 850 | pr_debug("dwc3: missing wMaxPacketSize\n"); |
| 851 | return -EINVAL; |
| 852 | } |
| 853 | |
| 854 | dep = to_dwc3_ep(ep); |
| 855 | dwc = dep->dwc; |
| 856 | |
Felipe Balbi | 95ca961 | 2015-12-10 13:08:20 -0600 | [diff] [blame] | 857 | if (dev_WARN_ONCE(dwc->dev, dep->flags & DWC3_EP_ENABLED, |
| 858 | "%s is already enabled\n", |
| 859 | dep->name)) |
Felipe Balbi | c6f83f3 | 2012-08-15 12:28:29 +0300 | [diff] [blame] | 860 | return 0; |
Felipe Balbi | c6f83f3 | 2012-08-15 12:28:29 +0300 | [diff] [blame] | 861 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 862 | spin_lock_irqsave(&dwc->lock, flags); |
Felipe Balbi | a2d23f0 | 2018-04-09 12:40:48 +0300 | [diff] [blame] | 863 | ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_INIT); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 864 | spin_unlock_irqrestore(&dwc->lock, flags); |
| 865 | |
| 866 | return ret; |
| 867 | } |
| 868 | |
| 869 | static int dwc3_gadget_ep_disable(struct usb_ep *ep) |
| 870 | { |
| 871 | struct dwc3_ep *dep; |
| 872 | struct dwc3 *dwc; |
| 873 | unsigned long flags; |
| 874 | int ret; |
| 875 | |
| 876 | if (!ep) { |
| 877 | pr_debug("dwc3: invalid parameters\n"); |
| 878 | return -EINVAL; |
| 879 | } |
| 880 | |
| 881 | dep = to_dwc3_ep(ep); |
| 882 | dwc = dep->dwc; |
| 883 | |
Felipe Balbi | 95ca961 | 2015-12-10 13:08:20 -0600 | [diff] [blame] | 884 | if (dev_WARN_ONCE(dwc->dev, !(dep->flags & DWC3_EP_ENABLED), |
| 885 | "%s is already disabled\n", |
| 886 | dep->name)) |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 887 | return 0; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 888 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 889 | spin_lock_irqsave(&dwc->lock, flags); |
| 890 | ret = __dwc3_gadget_ep_disable(dep); |
| 891 | spin_unlock_irqrestore(&dwc->lock, flags); |
| 892 | |
| 893 | return ret; |
| 894 | } |
| 895 | |
| 896 | static struct usb_request *dwc3_gadget_ep_alloc_request(struct usb_ep *ep, |
Felipe Balbi | 0bd0f6d | 2018-03-26 16:09:00 +0300 | [diff] [blame] | 897 | gfp_t gfp_flags) |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 898 | { |
| 899 | struct dwc3_request *req; |
| 900 | struct dwc3_ep *dep = to_dwc3_ep(ep); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 901 | |
| 902 | req = kzalloc(sizeof(*req), gfp_flags); |
Jingoo Han | 734d5a5 | 2014-07-17 12:45:11 +0900 | [diff] [blame] | 903 | if (!req) |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 904 | return NULL; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 905 | |
Felipe Balbi | 31a2f5a | 2018-05-07 15:19:31 +0300 | [diff] [blame] | 906 | req->direction = dep->direction; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 907 | req->epnum = dep->number; |
| 908 | req->dep = dep; |
Felipe Balbi | a3af5e3 | 2019-01-11 12:57:09 +0200 | [diff] [blame] | 909 | req->status = DWC3_REQUEST_STATUS_UNKNOWN; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 910 | |
Felipe Balbi | 2c4cbe6e5 | 2014-04-30 17:45:10 -0500 | [diff] [blame] | 911 | trace_dwc3_alloc_request(req); |
| 912 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 913 | return &req->request; |
| 914 | } |
| 915 | |
| 916 | static void dwc3_gadget_ep_free_request(struct usb_ep *ep, |
| 917 | struct usb_request *request) |
| 918 | { |
| 919 | struct dwc3_request *req = to_dwc3_request(request); |
| 920 | |
Felipe Balbi | 2c4cbe6e5 | 2014-04-30 17:45:10 -0500 | [diff] [blame] | 921 | trace_dwc3_free_request(req); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 922 | kfree(req); |
| 923 | } |
| 924 | |
Felipe Balbi | 4262691 | 2018-04-09 13:01:43 +0300 | [diff] [blame] | 925 | /** |
| 926 | * dwc3_ep_prev_trb - returns the previous TRB in the ring |
| 927 | * @dep: The endpoint with the TRB ring |
| 928 | * @index: The index of the current TRB in the ring |
| 929 | * |
| 930 | * Returns the TRB prior to the one pointed to by the index. If the |
| 931 | * index is 0, we will wrap backwards, skip the link TRB, and return |
| 932 | * the one just before that. |
| 933 | */ |
| 934 | static struct dwc3_trb *dwc3_ep_prev_trb(struct dwc3_ep *dep, u8 index) |
| 935 | { |
| 936 | u8 tmp = index; |
| 937 | |
| 938 | if (!tmp) |
| 939 | tmp = DWC3_TRB_NUM - 1; |
| 940 | |
| 941 | return &dep->trb_pool[tmp - 1]; |
| 942 | } |
| 943 | |
| 944 | static u32 dwc3_calc_trbs_left(struct dwc3_ep *dep) |
| 945 | { |
Felipe Balbi | 4262691 | 2018-04-09 13:01:43 +0300 | [diff] [blame] | 946 | u8 trbs_left; |
| 947 | |
| 948 | /* |
Thinh Nguyen | 87b2016 | 2021-08-19 03:17:03 +0200 | [diff] [blame] | 949 | * If the enqueue & dequeue are equal then the TRB ring is either full |
| 950 | * or empty. It's considered full when there are DWC3_TRB_NUM-1 of TRBs |
| 951 | * pending to be processed by the driver. |
Felipe Balbi | 4262691 | 2018-04-09 13:01:43 +0300 | [diff] [blame] | 952 | */ |
| 953 | if (dep->trb_enqueue == dep->trb_dequeue) { |
Thinh Nguyen | 87b2016 | 2021-08-19 03:17:03 +0200 | [diff] [blame] | 954 | /* |
| 955 | * If there is any request remained in the started_list at |
| 956 | * this point, that means there is no TRB available. |
| 957 | */ |
| 958 | if (!list_empty(&dep->started_list)) |
Felipe Balbi | 4262691 | 2018-04-09 13:01:43 +0300 | [diff] [blame] | 959 | return 0; |
| 960 | |
| 961 | return DWC3_TRB_NUM - 1; |
| 962 | } |
| 963 | |
| 964 | trbs_left = dep->trb_dequeue - dep->trb_enqueue; |
| 965 | trbs_left &= (DWC3_TRB_NUM - 1); |
| 966 | |
| 967 | if (dep->trb_dequeue < dep->trb_enqueue) |
| 968 | trbs_left--; |
| 969 | |
| 970 | return trbs_left; |
| 971 | } |
Felipe Balbi | 2c78c02 | 2016-08-12 13:13:10 +0300 | [diff] [blame] | 972 | |
Felipe Balbi | e49d3cf | 2017-01-05 14:40:53 +0200 | [diff] [blame] | 973 | static void __dwc3_prepare_one_trb(struct dwc3_ep *dep, struct dwc3_trb *trb, |
Felipe Balbi | e319bd6 | 2020-08-13 08:35:38 +0300 | [diff] [blame] | 974 | dma_addr_t dma, unsigned int length, unsigned int chain, |
| 975 | unsigned int node, unsigned int stream_id, |
| 976 | unsigned int short_not_ok, unsigned int no_interrupt, |
Thinh Nguyen | f9cc581 | 2020-09-30 17:44:19 -0700 | [diff] [blame] | 977 | unsigned int is_last, bool must_interrupt) |
Felipe Balbi | c71fc37 | 2011-11-22 11:37:34 +0200 | [diff] [blame] | 978 | { |
Felipe Balbi | 6b9018d | 2016-09-22 11:01:01 +0300 | [diff] [blame] | 979 | struct dwc3 *dwc = dep->dwc; |
Peter Chen | e81a701 | 2020-08-21 10:55:48 +0800 | [diff] [blame] | 980 | struct usb_gadget *gadget = dwc->gadget; |
Felipe Balbi | 6b9018d | 2016-09-22 11:01:01 +0300 | [diff] [blame] | 981 | enum usb_device_speed speed = gadget->speed; |
Felipe Balbi | c71fc37 | 2011-11-22 11:37:34 +0200 | [diff] [blame] | 982 | |
Felipe Balbi | f6bafc6 | 2012-02-06 11:04:53 +0200 | [diff] [blame] | 983 | trb->size = DWC3_TRB_SIZE_LENGTH(length); |
| 984 | trb->bpl = lower_32_bits(dma); |
| 985 | trb->bph = upper_32_bits(dma); |
Felipe Balbi | c71fc37 | 2011-11-22 11:37:34 +0200 | [diff] [blame] | 986 | |
Ido Shayevitz | 16e78db | 2012-03-12 20:25:24 +0200 | [diff] [blame] | 987 | switch (usb_endpoint_type(dep->endpoint.desc)) { |
Felipe Balbi | c71fc37 | 2011-11-22 11:37:34 +0200 | [diff] [blame] | 988 | case USB_ENDPOINT_XFER_CONTROL: |
Felipe Balbi | f6bafc6 | 2012-02-06 11:04:53 +0200 | [diff] [blame] | 989 | trb->ctrl = DWC3_TRBCTL_CONTROL_SETUP; |
Felipe Balbi | c71fc37 | 2011-11-22 11:37:34 +0200 | [diff] [blame] | 990 | break; |
| 991 | |
| 992 | case USB_ENDPOINT_XFER_ISOC: |
Felipe Balbi | 6b9018d | 2016-09-22 11:01:01 +0300 | [diff] [blame] | 993 | if (!node) { |
Pratyush Anand | e5ba5ec | 2013-01-14 15:59:37 +0530 | [diff] [blame] | 994 | trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS_FIRST; |
Felipe Balbi | 6b9018d | 2016-09-22 11:01:01 +0300 | [diff] [blame] | 995 | |
Manu Gautam | 40d829f | 2017-07-19 17:07:10 +0530 | [diff] [blame] | 996 | /* |
| 997 | * USB Specification 2.0 Section 5.9.2 states that: "If |
| 998 | * there is only a single transaction in the microframe, |
| 999 | * only a DATA0 data packet PID is used. If there are |
| 1000 | * two transactions per microframe, DATA1 is used for |
| 1001 | * the first transaction data packet and DATA0 is used |
| 1002 | * for the second transaction data packet. If there are |
| 1003 | * three transactions per microframe, DATA2 is used for |
| 1004 | * the first transaction data packet, DATA1 is used for |
| 1005 | * the second, and DATA0 is used for the third." |
| 1006 | * |
| 1007 | * IOW, we should satisfy the following cases: |
| 1008 | * |
| 1009 | * 1) length <= maxpacket |
| 1010 | * - DATA0 |
| 1011 | * |
| 1012 | * 2) maxpacket < length <= (2 * maxpacket) |
| 1013 | * - DATA1, DATA0 |
| 1014 | * |
| 1015 | * 3) (2 * maxpacket) < length <= (3 * maxpacket) |
| 1016 | * - DATA2, DATA1, DATA0 |
| 1017 | */ |
Felipe Balbi | 6b9018d | 2016-09-22 11:01:01 +0300 | [diff] [blame] | 1018 | if (speed == USB_SPEED_HIGH) { |
| 1019 | struct usb_ep *ep = &dep->endpoint; |
Manu Gautam | ec5bb87 | 2017-12-06 12:49:04 +0530 | [diff] [blame] | 1020 | unsigned int mult = 2; |
Manu Gautam | 40d829f | 2017-07-19 17:07:10 +0530 | [diff] [blame] | 1021 | unsigned int maxp = usb_endpoint_maxp(ep->desc); |
| 1022 | |
| 1023 | if (length <= (2 * maxp)) |
| 1024 | mult--; |
| 1025 | |
| 1026 | if (length <= maxp) |
| 1027 | mult--; |
| 1028 | |
| 1029 | trb->size |= DWC3_TRB_SIZE_PCM1(mult); |
Felipe Balbi | 6b9018d | 2016-09-22 11:01:01 +0300 | [diff] [blame] | 1030 | } |
| 1031 | } else { |
Pratyush Anand | e5ba5ec | 2013-01-14 15:59:37 +0530 | [diff] [blame] | 1032 | trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS; |
Felipe Balbi | 6b9018d | 2016-09-22 11:01:01 +0300 | [diff] [blame] | 1033 | } |
Felipe Balbi | ca4d44e | 2016-03-10 13:53:27 +0200 | [diff] [blame] | 1034 | |
| 1035 | /* always enable Interrupt on Missed ISOC */ |
| 1036 | trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI; |
Felipe Balbi | c71fc37 | 2011-11-22 11:37:34 +0200 | [diff] [blame] | 1037 | break; |
| 1038 | |
| 1039 | case USB_ENDPOINT_XFER_BULK: |
| 1040 | case USB_ENDPOINT_XFER_INT: |
Felipe Balbi | f6bafc6 | 2012-02-06 11:04:53 +0200 | [diff] [blame] | 1041 | trb->ctrl = DWC3_TRBCTL_NORMAL; |
Felipe Balbi | c71fc37 | 2011-11-22 11:37:34 +0200 | [diff] [blame] | 1042 | break; |
| 1043 | default: |
| 1044 | /* |
| 1045 | * This is only possible with faulty memory because we |
| 1046 | * checked it already :) |
| 1047 | */ |
Felipe Balbi | 0a695d4 | 2016-10-07 11:20:01 +0300 | [diff] [blame] | 1048 | dev_WARN(dwc->dev, "Unknown endpoint type %d\n", |
| 1049 | usb_endpoint_type(dep->endpoint.desc)); |
Felipe Balbi | c71fc37 | 2011-11-22 11:37:34 +0200 | [diff] [blame] | 1050 | } |
| 1051 | |
Tejas Joglekar | 244add8 | 2018-12-10 16:08:13 +0530 | [diff] [blame] | 1052 | /* |
| 1053 | * Enable Continue on Short Packet |
| 1054 | * when endpoint is not a stream capable |
| 1055 | */ |
Felipe Balbi | c9508c8 | 2016-10-05 14:26:23 +0300 | [diff] [blame] | 1056 | if (usb_endpoint_dir_out(dep->endpoint.desc)) { |
Tejas Joglekar | 244add8 | 2018-12-10 16:08:13 +0530 | [diff] [blame] | 1057 | if (!dep->stream_capable) |
| 1058 | trb->ctrl |= DWC3_TRB_CTRL_CSP; |
Felipe Balbi | f3af365 | 2013-12-13 14:19:33 -0600 | [diff] [blame] | 1059 | |
Felipe Balbi | e49d3cf | 2017-01-05 14:40:53 +0200 | [diff] [blame] | 1060 | if (short_not_ok) |
Felipe Balbi | c9508c8 | 2016-10-05 14:26:23 +0300 | [diff] [blame] | 1061 | trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI; |
| 1062 | } |
| 1063 | |
Thinh Nguyen | 8dbbe48 | 2020-09-30 17:44:25 -0700 | [diff] [blame] | 1064 | if ((!no_interrupt && !chain) || must_interrupt) |
Felipe Balbi | c9508c8 | 2016-10-05 14:26:23 +0300 | [diff] [blame] | 1065 | trb->ctrl |= DWC3_TRB_CTRL_IOC; |
Felipe Balbi | ca4d44e | 2016-03-10 13:53:27 +0200 | [diff] [blame] | 1066 | |
Pratyush Anand | e5ba5ec | 2013-01-14 15:59:37 +0530 | [diff] [blame] | 1067 | if (chain) |
| 1068 | trb->ctrl |= DWC3_TRB_CTRL_CHN; |
Thinh Nguyen | 3eaecd0 | 2020-05-05 19:46:51 -0700 | [diff] [blame] | 1069 | else if (dep->stream_capable && is_last) |
| 1070 | trb->ctrl |= DWC3_TRB_CTRL_LST; |
Pratyush Anand | e5ba5ec | 2013-01-14 15:59:37 +0530 | [diff] [blame] | 1071 | |
Ido Shayevitz | 16e78db | 2012-03-12 20:25:24 +0200 | [diff] [blame] | 1072 | if (usb_endpoint_xfer_bulk(dep->endpoint.desc) && dep->stream_capable) |
Felipe Balbi | e49d3cf | 2017-01-05 14:40:53 +0200 | [diff] [blame] | 1073 | trb->ctrl |= DWC3_TRB_CTRL_SID_SOFN(stream_id); |
Felipe Balbi | f6bafc6 | 2012-02-06 11:04:53 +0200 | [diff] [blame] | 1074 | |
Udipto Goswami | 3bfca38 | 2022-02-07 09:55:58 +0530 | [diff] [blame^] | 1075 | /* |
| 1076 | * As per data book 4.2.3.2TRB Control Bit Rules section |
| 1077 | * |
| 1078 | * The controller autonomously checks the HWO field of a TRB to determine if the |
| 1079 | * entire TRB is valid. Therefore, software must ensure that the rest of the TRB |
| 1080 | * is valid before setting the HWO field to '1'. In most systems, this means that |
| 1081 | * software must update the fourth DWORD of a TRB last. |
| 1082 | * |
| 1083 | * However there is a possibility of CPU re-ordering here which can cause |
| 1084 | * controller to observe the HWO bit set prematurely. |
| 1085 | * Add a write memory barrier to prevent CPU re-ordering. |
| 1086 | */ |
| 1087 | wmb(); |
Felipe Balbi | f6bafc6 | 2012-02-06 11:04:53 +0200 | [diff] [blame] | 1088 | trb->ctrl |= DWC3_TRB_CTRL_HWO; |
Felipe Balbi | 2c4cbe6e5 | 2014-04-30 17:45:10 -0500 | [diff] [blame] | 1089 | |
Anurag Kumar Vulisha | b7a4fbe | 2018-12-01 16:43:29 +0530 | [diff] [blame] | 1090 | dwc3_ep_inc_enq(dep); |
| 1091 | |
Felipe Balbi | 2c4cbe6e5 | 2014-04-30 17:45:10 -0500 | [diff] [blame] | 1092 | trace_dwc3_prepare_trb(dep, trb); |
Felipe Balbi | c71fc37 | 2011-11-22 11:37:34 +0200 | [diff] [blame] | 1093 | } |
| 1094 | |
John Youn | 361572b | 2016-05-19 17:26:17 -0700 | [diff] [blame] | 1095 | /** |
Felipe Balbi | e49d3cf | 2017-01-05 14:40:53 +0200 | [diff] [blame] | 1096 | * dwc3_prepare_one_trb - setup one TRB from one request |
| 1097 | * @dep: endpoint for which this request is prepared |
| 1098 | * @req: dwc3_request pointer |
Thinh Nguyen | 5d187c0 | 2020-08-06 19:46:23 -0700 | [diff] [blame] | 1099 | * @trb_length: buffer size of the TRB |
Felipe Balbi | e49d3cf | 2017-01-05 14:40:53 +0200 | [diff] [blame] | 1100 | * @chain: should this TRB be chained to the next? |
| 1101 | * @node: only for isochronous endpoints. First TRB needs different type. |
Thinh Nguyen | 2b80357 | 2020-09-24 01:21:30 -0700 | [diff] [blame] | 1102 | * @use_bounce_buffer: set to use bounce buffer |
Thinh Nguyen | f9cc581 | 2020-09-30 17:44:19 -0700 | [diff] [blame] | 1103 | * @must_interrupt: set to interrupt on TRB completion |
Felipe Balbi | e49d3cf | 2017-01-05 14:40:53 +0200 | [diff] [blame] | 1104 | */ |
| 1105 | static void dwc3_prepare_one_trb(struct dwc3_ep *dep, |
Thinh Nguyen | 5d187c0 | 2020-08-06 19:46:23 -0700 | [diff] [blame] | 1106 | struct dwc3_request *req, unsigned int trb_length, |
Thinh Nguyen | f9cc581 | 2020-09-30 17:44:19 -0700 | [diff] [blame] | 1107 | unsigned int chain, unsigned int node, bool use_bounce_buffer, |
| 1108 | bool must_interrupt) |
Felipe Balbi | e49d3cf | 2017-01-05 14:40:53 +0200 | [diff] [blame] | 1109 | { |
| 1110 | struct dwc3_trb *trb; |
Anurag Kumar Vulisha | a31e63b | 2018-03-27 16:35:20 +0530 | [diff] [blame] | 1111 | dma_addr_t dma; |
Felipe Balbi | e319bd6 | 2020-08-13 08:35:38 +0300 | [diff] [blame] | 1112 | unsigned int stream_id = req->request.stream_id; |
| 1113 | unsigned int short_not_ok = req->request.short_not_ok; |
| 1114 | unsigned int no_interrupt = req->request.no_interrupt; |
| 1115 | unsigned int is_last = req->request.is_last; |
Anurag Kumar Vulisha | a31e63b | 2018-03-27 16:35:20 +0530 | [diff] [blame] | 1116 | |
Thinh Nguyen | 2b80357 | 2020-09-24 01:21:30 -0700 | [diff] [blame] | 1117 | if (use_bounce_buffer) |
| 1118 | dma = dep->dwc->bounce_addr; |
| 1119 | else if (req->request.num_sgs > 0) |
Anurag Kumar Vulisha | a31e63b | 2018-03-27 16:35:20 +0530 | [diff] [blame] | 1120 | dma = sg_dma_address(req->start_sg); |
Thinh Nguyen | 5d187c0 | 2020-08-06 19:46:23 -0700 | [diff] [blame] | 1121 | else |
Anurag Kumar Vulisha | a31e63b | 2018-03-27 16:35:20 +0530 | [diff] [blame] | 1122 | dma = req->request.dma; |
Felipe Balbi | e49d3cf | 2017-01-05 14:40:53 +0200 | [diff] [blame] | 1123 | |
| 1124 | trb = &dep->trb_pool[dep->trb_enqueue]; |
| 1125 | |
| 1126 | if (!req->trb) { |
| 1127 | dwc3_gadget_move_started_request(req); |
| 1128 | req->trb = trb; |
| 1129 | req->trb_dma = dwc3_trb_dma_offset(dep, trb); |
Felipe Balbi | e49d3cf | 2017-01-05 14:40:53 +0200 | [diff] [blame] | 1130 | } |
| 1131 | |
Felipe Balbi | 09fe1f8 | 2018-08-01 13:32:07 +0300 | [diff] [blame] | 1132 | req->num_trbs++; |
| 1133 | |
Thinh Nguyen | 5d187c0 | 2020-08-06 19:46:23 -0700 | [diff] [blame] | 1134 | __dwc3_prepare_one_trb(dep, trb, dma, trb_length, chain, node, |
Thinh Nguyen | f9cc581 | 2020-09-30 17:44:19 -0700 | [diff] [blame] | 1135 | stream_id, short_not_ok, no_interrupt, is_last, |
| 1136 | must_interrupt); |
| 1137 | } |
| 1138 | |
| 1139 | static bool dwc3_needs_extra_trb(struct dwc3_ep *dep, struct dwc3_request *req) |
| 1140 | { |
| 1141 | unsigned int maxp = usb_endpoint_maxp(dep->endpoint.desc); |
| 1142 | unsigned int rem = req->request.length % maxp; |
| 1143 | |
| 1144 | if ((req->request.length && req->request.zero && !rem && |
| 1145 | !usb_endpoint_xfer_isoc(dep->endpoint.desc)) || |
| 1146 | (!req->direction && rem)) |
| 1147 | return true; |
| 1148 | |
| 1149 | return false; |
Felipe Balbi | e49d3cf | 2017-01-05 14:40:53 +0200 | [diff] [blame] | 1150 | } |
| 1151 | |
Thinh Nguyen | cb1b399 | 2020-09-24 01:22:07 -0700 | [diff] [blame] | 1152 | /** |
| 1153 | * dwc3_prepare_last_sg - prepare TRBs for the last SG entry |
| 1154 | * @dep: The endpoint that the request belongs to |
| 1155 | * @req: The request to prepare |
| 1156 | * @entry_length: The last SG entry size |
| 1157 | * @node: Indicates whether this is not the first entry (for isoc only) |
| 1158 | * |
| 1159 | * Return the number of TRBs prepared. |
| 1160 | */ |
| 1161 | static int dwc3_prepare_last_sg(struct dwc3_ep *dep, |
| 1162 | struct dwc3_request *req, unsigned int entry_length, |
| 1163 | unsigned int node) |
| 1164 | { |
| 1165 | unsigned int maxp = usb_endpoint_maxp(dep->endpoint.desc); |
| 1166 | unsigned int rem = req->request.length % maxp; |
| 1167 | unsigned int num_trbs = 1; |
| 1168 | |
Thinh Nguyen | f9cc581 | 2020-09-30 17:44:19 -0700 | [diff] [blame] | 1169 | if (dwc3_needs_extra_trb(dep, req)) |
Thinh Nguyen | cb1b399 | 2020-09-24 01:22:07 -0700 | [diff] [blame] | 1170 | num_trbs++; |
| 1171 | |
| 1172 | if (dwc3_calc_trbs_left(dep) < num_trbs) |
| 1173 | return 0; |
| 1174 | |
| 1175 | req->needs_extra_trb = num_trbs > 1; |
| 1176 | |
| 1177 | /* Prepare a normal TRB */ |
| 1178 | if (req->direction || req->request.length) |
| 1179 | dwc3_prepare_one_trb(dep, req, entry_length, |
Thinh Nguyen | f9cc581 | 2020-09-30 17:44:19 -0700 | [diff] [blame] | 1180 | req->needs_extra_trb, node, false, false); |
Thinh Nguyen | cb1b399 | 2020-09-24 01:22:07 -0700 | [diff] [blame] | 1181 | |
| 1182 | /* Prepare extra TRBs for ZLP and MPS OUT transfer alignment */ |
| 1183 | if ((!req->direction && !req->request.length) || req->needs_extra_trb) |
| 1184 | dwc3_prepare_one_trb(dep, req, |
| 1185 | req->direction ? 0 : maxp - rem, |
Thinh Nguyen | f9cc581 | 2020-09-30 17:44:19 -0700 | [diff] [blame] | 1186 | false, 1, true, false); |
Thinh Nguyen | cb1b399 | 2020-09-24 01:22:07 -0700 | [diff] [blame] | 1187 | |
| 1188 | return num_trbs; |
| 1189 | } |
| 1190 | |
Thinh Nguyen | 7f2958d | 2020-09-24 01:22:14 -0700 | [diff] [blame] | 1191 | static int dwc3_prepare_trbs_sg(struct dwc3_ep *dep, |
Felipe Balbi | 7ae7df4 | 2016-08-24 14:37:22 +0300 | [diff] [blame] | 1192 | struct dwc3_request *req) |
Felipe Balbi | 5ee85d8 | 2016-05-13 12:42:44 +0300 | [diff] [blame] | 1193 | { |
Anurag Kumar Vulisha | a31e63b | 2018-03-27 16:35:20 +0530 | [diff] [blame] | 1194 | struct scatterlist *sg = req->start_sg; |
Felipe Balbi | 5ee85d8 | 2016-05-13 12:42:44 +0300 | [diff] [blame] | 1195 | struct scatterlist *s; |
Felipe Balbi | 5ee85d8 | 2016-05-13 12:42:44 +0300 | [diff] [blame] | 1196 | int i; |
Thinh Nguyen | 5d187c0 | 2020-08-06 19:46:23 -0700 | [diff] [blame] | 1197 | unsigned int length = req->request.length; |
Anurag Kumar Vulisha | c96e672 | 2018-03-27 16:35:21 +0530 | [diff] [blame] | 1198 | unsigned int remaining = req->request.num_mapped_sgs |
| 1199 | - req->num_queued_sgs; |
Thinh Nguyen | 13111fc | 2020-09-24 01:21:49 -0700 | [diff] [blame] | 1200 | unsigned int num_trbs = req->num_trbs; |
Thinh Nguyen | f9cc581 | 2020-09-30 17:44:19 -0700 | [diff] [blame] | 1201 | bool needs_extra_trb = dwc3_needs_extra_trb(dep, req); |
Anurag Kumar Vulisha | c96e672 | 2018-03-27 16:35:21 +0530 | [diff] [blame] | 1202 | |
Thinh Nguyen | 5d187c0 | 2020-08-06 19:46:23 -0700 | [diff] [blame] | 1203 | /* |
| 1204 | * If we resume preparing the request, then get the remaining length of |
| 1205 | * the request and resume where we left off. |
| 1206 | */ |
| 1207 | for_each_sg(req->request.sg, s, req->num_queued_sgs, i) |
| 1208 | length -= sg_dma_len(s); |
| 1209 | |
Anurag Kumar Vulisha | c96e672 | 2018-03-27 16:35:21 +0530 | [diff] [blame] | 1210 | for_each_sg(sg, s, remaining, i) { |
Thinh Nguyen | 8dbbe48 | 2020-09-30 17:44:25 -0700 | [diff] [blame] | 1211 | unsigned int num_trbs_left = dwc3_calc_trbs_left(dep); |
Thinh Nguyen | 5d187c0 | 2020-08-06 19:46:23 -0700 | [diff] [blame] | 1212 | unsigned int trb_length; |
Thinh Nguyen | f9cc581 | 2020-09-30 17:44:19 -0700 | [diff] [blame] | 1213 | bool must_interrupt = false; |
Thinh Nguyen | cb1b399 | 2020-09-24 01:22:07 -0700 | [diff] [blame] | 1214 | bool last_sg = false; |
Felipe Balbi | 5ee85d8 | 2016-05-13 12:42:44 +0300 | [diff] [blame] | 1215 | |
Thinh Nguyen | 5d187c0 | 2020-08-06 19:46:23 -0700 | [diff] [blame] | 1216 | trb_length = min_t(unsigned int, length, sg_dma_len(s)); |
| 1217 | |
| 1218 | length -= trb_length; |
| 1219 | |
Pratham Pratap | dad2aff | 2020-03-02 21:44:43 +0000 | [diff] [blame] | 1220 | /* |
| 1221 | * IOMMU driver is coalescing the list of sgs which shares a |
| 1222 | * page boundary into one and giving it to USB driver. With |
| 1223 | * this the number of sgs mapped is not equal to the number of |
| 1224 | * sgs passed. So mark the chain bit to false if it isthe last |
| 1225 | * mapped sg. |
| 1226 | */ |
Thinh Nguyen | 5d187c0 | 2020-08-06 19:46:23 -0700 | [diff] [blame] | 1227 | if ((i == remaining - 1) || !length) |
Thinh Nguyen | cb1b399 | 2020-09-24 01:22:07 -0700 | [diff] [blame] | 1228 | last_sg = true; |
Felipe Balbi | 5ee85d8 | 2016-05-13 12:42:44 +0300 | [diff] [blame] | 1229 | |
Thinh Nguyen | 8dbbe48 | 2020-09-30 17:44:25 -0700 | [diff] [blame] | 1230 | if (!num_trbs_left) |
Thinh Nguyen | 13111fc | 2020-09-24 01:21:49 -0700 | [diff] [blame] | 1231 | break; |
| 1232 | |
Thinh Nguyen | cb1b399 | 2020-09-24 01:22:07 -0700 | [diff] [blame] | 1233 | if (last_sg) { |
| 1234 | if (!dwc3_prepare_last_sg(dep, req, trb_length, i)) |
Thinh Nguyen | f9cc581 | 2020-09-30 17:44:19 -0700 | [diff] [blame] | 1235 | break; |
Felipe Balbi | c6267a5 | 2017-01-05 14:58:46 +0200 | [diff] [blame] | 1236 | } else { |
Thinh Nguyen | f9cc581 | 2020-09-30 17:44:19 -0700 | [diff] [blame] | 1237 | /* |
| 1238 | * Look ahead to check if we have enough TRBs for the |
Thinh Nguyen | 8dbbe48 | 2020-09-30 17:44:25 -0700 | [diff] [blame] | 1239 | * next SG entry. If not, set interrupt on this TRB to |
| 1240 | * resume preparing the next SG entry when more TRBs are |
Thinh Nguyen | f9cc581 | 2020-09-30 17:44:19 -0700 | [diff] [blame] | 1241 | * free. |
| 1242 | */ |
Thinh Nguyen | 8dbbe48 | 2020-09-30 17:44:25 -0700 | [diff] [blame] | 1243 | if (num_trbs_left == 1 || (needs_extra_trb && |
| 1244 | num_trbs_left <= 2 && |
| 1245 | sg_dma_len(sg_next(s)) >= length)) |
Thinh Nguyen | f9cc581 | 2020-09-30 17:44:19 -0700 | [diff] [blame] | 1246 | must_interrupt = true; |
| 1247 | |
| 1248 | dwc3_prepare_one_trb(dep, req, trb_length, 1, i, false, |
| 1249 | must_interrupt); |
Felipe Balbi | c6267a5 | 2017-01-05 14:58:46 +0200 | [diff] [blame] | 1250 | } |
Felipe Balbi | 5ee85d8 | 2016-05-13 12:42:44 +0300 | [diff] [blame] | 1251 | |
Anurag Kumar Vulisha | a31e63b | 2018-03-27 16:35:20 +0530 | [diff] [blame] | 1252 | /* |
| 1253 | * There can be a situation where all sgs in sglist are not |
| 1254 | * queued because of insufficient trb number. To handle this |
| 1255 | * case, update start_sg to next sg to be queued, so that |
| 1256 | * we have free trbs we can continue queuing from where we |
| 1257 | * previously stopped |
| 1258 | */ |
Thinh Nguyen | cb1b399 | 2020-09-24 01:22:07 -0700 | [diff] [blame] | 1259 | if (!last_sg) |
Anurag Kumar Vulisha | a31e63b | 2018-03-27 16:35:20 +0530 | [diff] [blame] | 1260 | req->start_sg = sg_next(s); |
| 1261 | |
Anurag Kumar Vulisha | c96e672 | 2018-03-27 16:35:21 +0530 | [diff] [blame] | 1262 | req->num_queued_sgs++; |
Thinh Nguyen | adccf17 | 2021-05-12 20:17:09 -0700 | [diff] [blame] | 1263 | req->num_pending_sgs--; |
Anurag Kumar Vulisha | c96e672 | 2018-03-27 16:35:21 +0530 | [diff] [blame] | 1264 | |
Thinh Nguyen | 5d187c0 | 2020-08-06 19:46:23 -0700 | [diff] [blame] | 1265 | /* |
| 1266 | * The number of pending SG entries may not correspond to the |
| 1267 | * number of mapped SG entries. If all the data are queued, then |
| 1268 | * don't include unused SG entries. |
| 1269 | */ |
| 1270 | if (length == 0) { |
Thinh Nguyen | adccf17 | 2021-05-12 20:17:09 -0700 | [diff] [blame] | 1271 | req->num_pending_sgs = 0; |
Thinh Nguyen | 5d187c0 | 2020-08-06 19:46:23 -0700 | [diff] [blame] | 1272 | break; |
| 1273 | } |
| 1274 | |
Thinh Nguyen | 8dbbe48 | 2020-09-30 17:44:25 -0700 | [diff] [blame] | 1275 | if (must_interrupt) |
Felipe Balbi | 5ee85d8 | 2016-05-13 12:42:44 +0300 | [diff] [blame] | 1276 | break; |
| 1277 | } |
Thinh Nguyen | 13111fc | 2020-09-24 01:21:49 -0700 | [diff] [blame] | 1278 | |
Thinh Nguyen | 30892cb | 2020-09-24 01:22:01 -0700 | [diff] [blame] | 1279 | return req->num_trbs - num_trbs; |
Felipe Balbi | 5ee85d8 | 2016-05-13 12:42:44 +0300 | [diff] [blame] | 1280 | } |
| 1281 | |
Thinh Nguyen | 7f2958d | 2020-09-24 01:22:14 -0700 | [diff] [blame] | 1282 | static int dwc3_prepare_trbs_linear(struct dwc3_ep *dep, |
Felipe Balbi | 7ae7df4 | 2016-08-24 14:37:22 +0300 | [diff] [blame] | 1283 | struct dwc3_request *req) |
Felipe Balbi | 5ee85d8 | 2016-05-13 12:42:44 +0300 | [diff] [blame] | 1284 | { |
Thinh Nguyen | cb1b399 | 2020-09-24 01:22:07 -0700 | [diff] [blame] | 1285 | return dwc3_prepare_last_sg(dep, req, req->request.length, 0); |
Felipe Balbi | 5ee85d8 | 2016-05-13 12:42:44 +0300 | [diff] [blame] | 1286 | } |
| 1287 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1288 | /* |
| 1289 | * dwc3_prepare_trbs - setup TRBs from requests |
| 1290 | * @dep: endpoint for which requests are being prepared |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1291 | * |
Paul Zimmerman | 1d04679 | 2012-02-15 18:56:56 -0800 | [diff] [blame] | 1292 | * The function goes through the requests list and sets up TRBs for the |
| 1293 | * transfers. The function returns once there are no more TRBs available or |
| 1294 | * it runs out of requests. |
Thinh Nguyen | 490410b | 2020-09-24 01:21:55 -0700 | [diff] [blame] | 1295 | * |
| 1296 | * Returns the number of TRBs prepared or negative errno. |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1297 | */ |
Thinh Nguyen | 490410b | 2020-09-24 01:21:55 -0700 | [diff] [blame] | 1298 | static int dwc3_prepare_trbs(struct dwc3_ep *dep) |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1299 | { |
Felipe Balbi | 68e823e | 2011-11-28 12:25:01 +0200 | [diff] [blame] | 1300 | struct dwc3_request *req, *n; |
Thinh Nguyen | 490410b | 2020-09-24 01:21:55 -0700 | [diff] [blame] | 1301 | int ret = 0; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1302 | |
| 1303 | BUILD_BUG_ON_NOT_POWER_OF_2(DWC3_TRB_NUM); |
| 1304 | |
Felipe Balbi | d86c5a6 | 2016-10-25 13:48:52 +0300 | [diff] [blame] | 1305 | /* |
| 1306 | * We can get in a situation where there's a request in the started list |
| 1307 | * but there weren't enough TRBs to fully kick it in the first time |
| 1308 | * around, so it has been waiting for more TRBs to be freed up. |
| 1309 | * |
| 1310 | * In that case, we should check if we have a request with pending_sgs |
| 1311 | * in the started list and prepare TRBs for that request first, |
| 1312 | * otherwise we will prepare TRBs completely out of order and that will |
| 1313 | * break things. |
| 1314 | */ |
| 1315 | list_for_each_entry(req, &dep->started_list, list) { |
Thinh Nguyen | 490410b | 2020-09-24 01:21:55 -0700 | [diff] [blame] | 1316 | if (req->num_pending_sgs > 0) { |
Thinh Nguyen | 7f2958d | 2020-09-24 01:22:14 -0700 | [diff] [blame] | 1317 | ret = dwc3_prepare_trbs_sg(dep, req); |
Thinh Nguyen | 346a15c | 2020-09-30 17:44:32 -0700 | [diff] [blame] | 1318 | if (!ret || req->num_pending_sgs) |
Thinh Nguyen | 490410b | 2020-09-24 01:21:55 -0700 | [diff] [blame] | 1319 | return ret; |
| 1320 | } |
Felipe Balbi | d86c5a6 | 2016-10-25 13:48:52 +0300 | [diff] [blame] | 1321 | |
| 1322 | if (!dwc3_calc_trbs_left(dep)) |
Thinh Nguyen | 490410b | 2020-09-24 01:21:55 -0700 | [diff] [blame] | 1323 | return ret; |
Thinh Nguyen | 63c7bb2 | 2020-05-15 16:40:46 -0700 | [diff] [blame] | 1324 | |
| 1325 | /* |
| 1326 | * Don't prepare beyond a transfer. In DWC_usb32, its transfer |
| 1327 | * burst capability may try to read and use TRBs beyond the |
| 1328 | * active transfer instead of stopping. |
| 1329 | */ |
| 1330 | if (dep->stream_capable && req->request.is_last) |
Thinh Nguyen | 490410b | 2020-09-24 01:21:55 -0700 | [diff] [blame] | 1331 | return ret; |
Felipe Balbi | d86c5a6 | 2016-10-25 13:48:52 +0300 | [diff] [blame] | 1332 | } |
| 1333 | |
Felipe Balbi | aa3342c | 2016-03-14 11:01:31 +0200 | [diff] [blame] | 1334 | list_for_each_entry_safe(req, n, &dep->pending_list, list) { |
Felipe Balbi | cdb55b3 | 2017-05-17 13:21:14 +0300 | [diff] [blame] | 1335 | struct dwc3 *dwc = dep->dwc; |
Felipe Balbi | cdb55b3 | 2017-05-17 13:21:14 +0300 | [diff] [blame] | 1336 | |
| 1337 | ret = usb_gadget_map_request_by_dev(dwc->sysdev, &req->request, |
| 1338 | dep->direction); |
| 1339 | if (ret) |
Thinh Nguyen | 490410b | 2020-09-24 01:21:55 -0700 | [diff] [blame] | 1340 | return ret; |
Felipe Balbi | cdb55b3 | 2017-05-17 13:21:14 +0300 | [diff] [blame] | 1341 | |
| 1342 | req->sg = req->request.sg; |
Anurag Kumar Vulisha | a31e63b | 2018-03-27 16:35:20 +0530 | [diff] [blame] | 1343 | req->start_sg = req->sg; |
Anurag Kumar Vulisha | c96e672 | 2018-03-27 16:35:21 +0530 | [diff] [blame] | 1344 | req->num_queued_sgs = 0; |
Felipe Balbi | cdb55b3 | 2017-05-17 13:21:14 +0300 | [diff] [blame] | 1345 | req->num_pending_sgs = req->request.num_mapped_sgs; |
| 1346 | |
Thinh Nguyen | 346a15c | 2020-09-30 17:44:32 -0700 | [diff] [blame] | 1347 | if (req->num_pending_sgs > 0) { |
Thinh Nguyen | 7f2958d | 2020-09-24 01:22:14 -0700 | [diff] [blame] | 1348 | ret = dwc3_prepare_trbs_sg(dep, req); |
Thinh Nguyen | 346a15c | 2020-09-30 17:44:32 -0700 | [diff] [blame] | 1349 | if (req->num_pending_sgs) |
| 1350 | return ret; |
| 1351 | } else { |
Thinh Nguyen | 7f2958d | 2020-09-24 01:22:14 -0700 | [diff] [blame] | 1352 | ret = dwc3_prepare_trbs_linear(dep, req); |
Thinh Nguyen | 346a15c | 2020-09-30 17:44:32 -0700 | [diff] [blame] | 1353 | } |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1354 | |
Thinh Nguyen | 490410b | 2020-09-24 01:21:55 -0700 | [diff] [blame] | 1355 | if (!ret || !dwc3_calc_trbs_left(dep)) |
| 1356 | return ret; |
Thinh Nguyen | aefe3d2 | 2020-05-05 19:47:03 -0700 | [diff] [blame] | 1357 | |
| 1358 | /* |
| 1359 | * Don't prepare beyond a transfer. In DWC_usb32, its transfer |
| 1360 | * burst capability may try to read and use TRBs beyond the |
| 1361 | * active transfer instead of stopping. |
| 1362 | */ |
| 1363 | if (dep->stream_capable && req->request.is_last) |
Thinh Nguyen | 490410b | 2020-09-24 01:21:55 -0700 | [diff] [blame] | 1364 | return ret; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1365 | } |
Thinh Nguyen | 490410b | 2020-09-24 01:21:55 -0700 | [diff] [blame] | 1366 | |
| 1367 | return ret; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1368 | } |
| 1369 | |
Thinh Nguyen | 8d99087 | 2020-03-29 16:12:57 -0700 | [diff] [blame] | 1370 | static void dwc3_gadget_ep_cleanup_cancelled_requests(struct dwc3_ep *dep); |
| 1371 | |
Felipe Balbi | 7fdca76 | 2017-09-05 14:41:34 +0300 | [diff] [blame] | 1372 | static int __dwc3_gadget_kick_transfer(struct dwc3_ep *dep) |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1373 | { |
| 1374 | struct dwc3_gadget_ep_cmd_params params; |
| 1375 | struct dwc3_request *req; |
Felipe Balbi | 4fae2e3 | 2016-05-12 16:53:59 +0300 | [diff] [blame] | 1376 | int starting; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1377 | int ret; |
| 1378 | u32 cmd; |
| 1379 | |
Thinh Nguyen | d72ecc0 | 2020-09-29 00:18:48 -0700 | [diff] [blame] | 1380 | /* |
| 1381 | * Note that it's normal to have no new TRBs prepared (i.e. ret == 0). |
| 1382 | * This happens when we need to stop and restart a transfer such as in |
| 1383 | * the case of reinitiating a stream or retrying an isoc transfer. |
| 1384 | */ |
Thinh Nguyen | 490410b | 2020-09-24 01:21:55 -0700 | [diff] [blame] | 1385 | ret = dwc3_prepare_trbs(dep); |
Thinh Nguyen | d72ecc0 | 2020-09-29 00:18:48 -0700 | [diff] [blame] | 1386 | if (ret < 0) |
Thinh Nguyen | 490410b | 2020-09-24 01:21:55 -0700 | [diff] [blame] | 1387 | return ret; |
Felipe Balbi | ccb94eb | 2017-09-05 14:28:46 +0300 | [diff] [blame] | 1388 | |
Felipe Balbi | 1912cbc | 2018-03-29 11:08:46 +0300 | [diff] [blame] | 1389 | starting = !(dep->flags & DWC3_EP_TRANSFER_STARTED); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1390 | |
Thinh Nguyen | 2338484 | 2020-09-30 17:44:38 -0700 | [diff] [blame] | 1391 | /* |
| 1392 | * If there's no new TRB prepared and we don't need to restart a |
| 1393 | * transfer, there's no need to update the transfer. |
| 1394 | */ |
| 1395 | if (!ret && !starting) |
| 1396 | return ret; |
| 1397 | |
Felipe Balbi | 4fae2e3 | 2016-05-12 16:53:59 +0300 | [diff] [blame] | 1398 | req = next_request(&dep->started_list); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1399 | if (!req) { |
| 1400 | dep->flags |= DWC3_EP_PENDING_REQUEST; |
| 1401 | return 0; |
| 1402 | } |
| 1403 | |
| 1404 | memset(¶ms, 0, sizeof(params)); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1405 | |
Felipe Balbi | 4fae2e3 | 2016-05-12 16:53:59 +0300 | [diff] [blame] | 1406 | if (starting) { |
Pratyush Anand | 1877d6c | 2013-01-14 15:59:36 +0530 | [diff] [blame] | 1407 | params.param0 = upper_32_bits(req->trb_dma); |
| 1408 | params.param1 = lower_32_bits(req->trb_dma); |
Felipe Balbi | 7fdca76 | 2017-09-05 14:41:34 +0300 | [diff] [blame] | 1409 | cmd = DWC3_DEPCMD_STARTTRANSFER; |
| 1410 | |
Anurag Kumar Vulisha | a735180 | 2018-12-01 16:43:25 +0530 | [diff] [blame] | 1411 | if (dep->stream_capable) |
| 1412 | cmd |= DWC3_DEPCMD_PARAM(req->request.stream_id); |
| 1413 | |
Felipe Balbi | 7fdca76 | 2017-09-05 14:41:34 +0300 | [diff] [blame] | 1414 | if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) |
| 1415 | cmd |= DWC3_DEPCMD_PARAM(dep->frame_number); |
Pratyush Anand | 1877d6c | 2013-01-14 15:59:36 +0530 | [diff] [blame] | 1416 | } else { |
Felipe Balbi | b6b1c6d | 2016-05-30 13:29:35 +0300 | [diff] [blame] | 1417 | cmd = DWC3_DEPCMD_UPDATETRANSFER | |
| 1418 | DWC3_DEPCMD_PARAM(dep->resource_index); |
Pratyush Anand | 1877d6c | 2013-01-14 15:59:36 +0530 | [diff] [blame] | 1419 | } |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1420 | |
Felipe Balbi | 2cd4718 | 2016-04-12 16:42:43 +0300 | [diff] [blame] | 1421 | ret = dwc3_send_gadget_ep_cmd(dep, cmd, ¶ms); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1422 | if (ret < 0) { |
Thinh Nguyen | 8d99087 | 2020-03-29 16:12:57 -0700 | [diff] [blame] | 1423 | struct dwc3_request *tmp; |
| 1424 | |
| 1425 | if (ret == -EAGAIN) |
| 1426 | return ret; |
| 1427 | |
| 1428 | dwc3_stop_active_transfer(dep, true, true); |
| 1429 | |
| 1430 | list_for_each_entry_safe(req, tmp, &dep->started_list, list) |
| 1431 | dwc3_gadget_move_cancelled_request(req); |
| 1432 | |
| 1433 | /* If ep isn't started, then there's no end transfer pending */ |
| 1434 | if (!(dep->flags & DWC3_EP_END_TRANSFER_PENDING)) |
| 1435 | dwc3_gadget_ep_cleanup_cancelled_requests(dep); |
| 1436 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1437 | return ret; |
| 1438 | } |
| 1439 | |
Thinh Nguyen | e0d1956 | 2020-05-05 19:46:57 -0700 | [diff] [blame] | 1440 | if (dep->stream_capable && req->request.is_last) |
| 1441 | dep->flags |= DWC3_EP_WAIT_TRANSFER_COMPLETE; |
| 1442 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1443 | return 0; |
| 1444 | } |
| 1445 | |
Felipe Balbi | 6cb2e4e | 2016-10-21 13:07:09 +0300 | [diff] [blame] | 1446 | static int __dwc3_gadget_get_frame(struct dwc3 *dwc) |
| 1447 | { |
| 1448 | u32 reg; |
| 1449 | |
| 1450 | reg = dwc3_readl(dwc->regs, DWC3_DSTS); |
| 1451 | return DWC3_DSTS_SOFFN(reg); |
| 1452 | } |
| 1453 | |
Thinh Nguyen | d92021f | 2018-11-14 22:56:54 -0800 | [diff] [blame] | 1454 | /** |
| 1455 | * dwc3_gadget_start_isoc_quirk - workaround invalid frame number |
| 1456 | * @dep: isoc endpoint |
| 1457 | * |
| 1458 | * This function tests for the correct combination of BIT[15:14] from the 16-bit |
| 1459 | * microframe number reported by the XferNotReady event for the future frame |
| 1460 | * number to start the isoc transfer. |
| 1461 | * |
| 1462 | * In DWC_usb31 version 1.70a-ea06 and prior, for highspeed and fullspeed |
| 1463 | * isochronous IN, BIT[15:14] of the 16-bit microframe number reported by the |
| 1464 | * XferNotReady event are invalid. The driver uses this number to schedule the |
| 1465 | * isochronous transfer and passes it to the START TRANSFER command. Because |
| 1466 | * this number is invalid, the command may fail. If BIT[15:14] matches the |
| 1467 | * internal 16-bit microframe, the START TRANSFER command will pass and the |
| 1468 | * transfer will start at the scheduled time, if it is off by 1, the command |
| 1469 | * will still pass, but the transfer will start 2 seconds in the future. For all |
| 1470 | * other conditions, the START TRANSFER command will fail with bus-expiry. |
| 1471 | * |
| 1472 | * In order to workaround this issue, we can test for the correct combination of |
| 1473 | * BIT[15:14] by sending START TRANSFER commands with different values of |
| 1474 | * BIT[15:14]: 'b00, 'b01, 'b10, and 'b11. Each combination is 2^14 uframe apart |
| 1475 | * (or 2 seconds). 4 seconds into the future will result in a bus-expiry status. |
| 1476 | * As the result, within the 4 possible combinations for BIT[15:14], there will |
| 1477 | * be 2 successful and 2 failure START COMMAND status. One of the 2 successful |
| 1478 | * command status will result in a 2-second delay start. The smaller BIT[15:14] |
| 1479 | * value is the correct combination. |
| 1480 | * |
| 1481 | * Since there are only 4 outcomes and the results are ordered, we can simply |
| 1482 | * test 2 START TRANSFER commands with BIT[15:14] combinations 'b00 and 'b01 to |
| 1483 | * deduce the smaller successful combination. |
| 1484 | * |
| 1485 | * Let test0 = test status for combination 'b00 and test1 = test status for 'b01 |
| 1486 | * of BIT[15:14]. The correct combination is as follow: |
| 1487 | * |
| 1488 | * if test0 fails and test1 passes, BIT[15:14] is 'b01 |
| 1489 | * if test0 fails and test1 fails, BIT[15:14] is 'b10 |
| 1490 | * if test0 passes and test1 fails, BIT[15:14] is 'b11 |
| 1491 | * if test0 passes and test1 passes, BIT[15:14] is 'b00 |
| 1492 | * |
| 1493 | * Synopsys STAR 9001202023: Wrong microframe number for isochronous IN |
| 1494 | * endpoints. |
| 1495 | */ |
Felipe Balbi | 25abad6 | 2018-08-14 10:41:19 +0300 | [diff] [blame] | 1496 | static int dwc3_gadget_start_isoc_quirk(struct dwc3_ep *dep) |
Pratyush Anand | d6d6ec7 | 2012-05-25 18:54:56 +0530 | [diff] [blame] | 1497 | { |
Thinh Nguyen | d92021f | 2018-11-14 22:56:54 -0800 | [diff] [blame] | 1498 | int cmd_status = 0; |
| 1499 | bool test0; |
| 1500 | bool test1; |
| 1501 | |
| 1502 | while (dep->combo_num < 2) { |
| 1503 | struct dwc3_gadget_ep_cmd_params params; |
| 1504 | u32 test_frame_number; |
| 1505 | u32 cmd; |
| 1506 | |
| 1507 | /* |
| 1508 | * Check if we can start isoc transfer on the next interval or |
| 1509 | * 4 uframes in the future with BIT[15:14] as dep->combo_num |
| 1510 | */ |
Michael Grzeschik | ca14378 | 2020-07-01 20:24:51 +0200 | [diff] [blame] | 1511 | test_frame_number = dep->frame_number & DWC3_FRNUMBER_MASK; |
Thinh Nguyen | d92021f | 2018-11-14 22:56:54 -0800 | [diff] [blame] | 1512 | test_frame_number |= dep->combo_num << 14; |
| 1513 | test_frame_number += max_t(u32, 4, dep->interval); |
| 1514 | |
| 1515 | params.param0 = upper_32_bits(dep->dwc->bounce_addr); |
| 1516 | params.param1 = lower_32_bits(dep->dwc->bounce_addr); |
| 1517 | |
| 1518 | cmd = DWC3_DEPCMD_STARTTRANSFER; |
| 1519 | cmd |= DWC3_DEPCMD_PARAM(test_frame_number); |
| 1520 | cmd_status = dwc3_send_gadget_ep_cmd(dep, cmd, ¶ms); |
| 1521 | |
| 1522 | /* Redo if some other failure beside bus-expiry is received */ |
| 1523 | if (cmd_status && cmd_status != -EAGAIN) { |
| 1524 | dep->start_cmd_status = 0; |
| 1525 | dep->combo_num = 0; |
Felipe Balbi | 25abad6 | 2018-08-14 10:41:19 +0300 | [diff] [blame] | 1526 | return 0; |
Thinh Nguyen | d92021f | 2018-11-14 22:56:54 -0800 | [diff] [blame] | 1527 | } |
| 1528 | |
| 1529 | /* Store the first test status */ |
| 1530 | if (dep->combo_num == 0) |
| 1531 | dep->start_cmd_status = cmd_status; |
| 1532 | |
| 1533 | dep->combo_num++; |
| 1534 | |
| 1535 | /* |
| 1536 | * End the transfer if the START_TRANSFER command is successful |
| 1537 | * to wait for the next XferNotReady to test the command again |
| 1538 | */ |
| 1539 | if (cmd_status == 0) { |
Felipe Balbi | c5353b2 | 2019-02-13 13:00:54 +0200 | [diff] [blame] | 1540 | dwc3_stop_active_transfer(dep, true, true); |
Felipe Balbi | 25abad6 | 2018-08-14 10:41:19 +0300 | [diff] [blame] | 1541 | return 0; |
Thinh Nguyen | d92021f | 2018-11-14 22:56:54 -0800 | [diff] [blame] | 1542 | } |
Pratyush Anand | d6d6ec7 | 2012-05-25 18:54:56 +0530 | [diff] [blame] | 1543 | } |
| 1544 | |
Thinh Nguyen | d92021f | 2018-11-14 22:56:54 -0800 | [diff] [blame] | 1545 | /* test0 and test1 are both completed at this point */ |
| 1546 | test0 = (dep->start_cmd_status == 0); |
| 1547 | test1 = (cmd_status == 0); |
| 1548 | |
| 1549 | if (!test0 && test1) |
| 1550 | dep->combo_num = 1; |
| 1551 | else if (!test0 && !test1) |
| 1552 | dep->combo_num = 2; |
| 1553 | else if (test0 && !test1) |
| 1554 | dep->combo_num = 3; |
| 1555 | else if (test0 && test1) |
| 1556 | dep->combo_num = 0; |
| 1557 | |
Michael Grzeschik | ca14378 | 2020-07-01 20:24:51 +0200 | [diff] [blame] | 1558 | dep->frame_number &= DWC3_FRNUMBER_MASK; |
Thinh Nguyen | d92021f | 2018-11-14 22:56:54 -0800 | [diff] [blame] | 1559 | dep->frame_number |= dep->combo_num << 14; |
| 1560 | dep->frame_number += max_t(u32, 4, dep->interval); |
| 1561 | |
| 1562 | /* Reinitialize test variables */ |
| 1563 | dep->start_cmd_status = 0; |
| 1564 | dep->combo_num = 0; |
| 1565 | |
Felipe Balbi | 25abad6 | 2018-08-14 10:41:19 +0300 | [diff] [blame] | 1566 | return __dwc3_gadget_kick_transfer(dep); |
Thinh Nguyen | d92021f | 2018-11-14 22:56:54 -0800 | [diff] [blame] | 1567 | } |
| 1568 | |
Felipe Balbi | 25abad6 | 2018-08-14 10:41:19 +0300 | [diff] [blame] | 1569 | static int __dwc3_gadget_start_isoc(struct dwc3_ep *dep) |
Pratyush Anand | d6d6ec7 | 2012-05-25 18:54:56 +0530 | [diff] [blame] | 1570 | { |
Michael Olbrich | c5a7092 | 2020-07-01 20:24:52 +0200 | [diff] [blame] | 1571 | const struct usb_endpoint_descriptor *desc = dep->endpoint.desc; |
Thinh Nguyen | d92021f | 2018-11-14 22:56:54 -0800 | [diff] [blame] | 1572 | struct dwc3 *dwc = dep->dwc; |
Felipe Balbi | d537010 | 2018-08-14 10:42:43 +0300 | [diff] [blame] | 1573 | int ret; |
| 1574 | int i; |
Thinh Nguyen | d92021f | 2018-11-14 22:56:54 -0800 | [diff] [blame] | 1575 | |
Thinh Nguyen | 36f05d3 | 2020-03-29 16:13:10 -0700 | [diff] [blame] | 1576 | if (list_empty(&dep->pending_list) && |
| 1577 | list_empty(&dep->started_list)) { |
Pratyush Anand | d6d6ec7 | 2012-05-25 18:54:56 +0530 | [diff] [blame] | 1578 | dep->flags |= DWC3_EP_PENDING_REQUEST; |
Felipe Balbi | 25abad6 | 2018-08-14 10:41:19 +0300 | [diff] [blame] | 1579 | return -EAGAIN; |
Pratyush Anand | d6d6ec7 | 2012-05-25 18:54:56 +0530 | [diff] [blame] | 1580 | } |
| 1581 | |
Thinh Nguyen | 9af21dd | 2020-04-11 19:20:01 -0700 | [diff] [blame] | 1582 | if (!dwc->dis_start_transfer_quirk && |
| 1583 | (DWC3_VER_IS_PRIOR(DWC31, 170A) || |
| 1584 | DWC3_VER_TYPE_IS_WITHIN(DWC31, 170A, EA01, EA06))) { |
Peter Chen | e81a701 | 2020-08-21 10:55:48 +0800 | [diff] [blame] | 1585 | if (dwc->gadget->speed <= USB_SPEED_HIGH && dep->direction) |
Felipe Balbi | 25abad6 | 2018-08-14 10:41:19 +0300 | [diff] [blame] | 1586 | return dwc3_gadget_start_isoc_quirk(dep); |
Thinh Nguyen | d92021f | 2018-11-14 22:56:54 -0800 | [diff] [blame] | 1587 | } |
| 1588 | |
Michael Olbrich | c5a7092 | 2020-07-01 20:24:52 +0200 | [diff] [blame] | 1589 | if (desc->bInterval <= 14 && |
Peter Chen | e81a701 | 2020-08-21 10:55:48 +0800 | [diff] [blame] | 1590 | dwc->gadget->speed >= USB_SPEED_HIGH) { |
Michael Olbrich | c5a7092 | 2020-07-01 20:24:52 +0200 | [diff] [blame] | 1591 | u32 frame = __dwc3_gadget_get_frame(dwc); |
| 1592 | bool rollover = frame < |
| 1593 | (dep->frame_number & DWC3_FRNUMBER_MASK); |
| 1594 | |
| 1595 | /* |
| 1596 | * frame_number is set from XferNotReady and may be already |
| 1597 | * out of date. DSTS only provides the lower 14 bit of the |
| 1598 | * current frame number. So add the upper two bits of |
| 1599 | * frame_number and handle a possible rollover. |
| 1600 | * This will provide the correct frame_number unless more than |
| 1601 | * rollover has happened since XferNotReady. |
| 1602 | */ |
| 1603 | |
| 1604 | dep->frame_number = (dep->frame_number & ~DWC3_FRNUMBER_MASK) | |
| 1605 | frame; |
| 1606 | if (rollover) |
| 1607 | dep->frame_number += BIT(14); |
| 1608 | } |
| 1609 | |
Felipe Balbi | d537010 | 2018-08-14 10:42:43 +0300 | [diff] [blame] | 1610 | for (i = 0; i < DWC3_ISOC_MAX_RETRIES; i++) { |
| 1611 | dep->frame_number = DWC3_ALIGN_FRAME(dep, i + 1); |
| 1612 | |
| 1613 | ret = __dwc3_gadget_kick_transfer(dep); |
| 1614 | if (ret != -EAGAIN) |
| 1615 | break; |
| 1616 | } |
| 1617 | |
Thinh Nguyen | 36f05d3 | 2020-03-29 16:13:10 -0700 | [diff] [blame] | 1618 | /* |
| 1619 | * After a number of unsuccessful start attempts due to bus-expiry |
| 1620 | * status, issue END_TRANSFER command and retry on the next XferNotReady |
| 1621 | * event. |
| 1622 | */ |
| 1623 | if (ret == -EAGAIN) { |
| 1624 | struct dwc3_gadget_ep_cmd_params params; |
| 1625 | u32 cmd; |
| 1626 | |
| 1627 | cmd = DWC3_DEPCMD_ENDTRANSFER | |
| 1628 | DWC3_DEPCMD_CMDIOC | |
| 1629 | DWC3_DEPCMD_PARAM(dep->resource_index); |
| 1630 | |
| 1631 | dep->resource_index = 0; |
| 1632 | memset(¶ms, 0, sizeof(params)); |
| 1633 | |
| 1634 | ret = dwc3_send_gadget_ep_cmd(dep, cmd, ¶ms); |
| 1635 | if (!ret) |
| 1636 | dep->flags |= DWC3_EP_END_TRANSFER_PENDING; |
| 1637 | } |
| 1638 | |
Felipe Balbi | d537010 | 2018-08-14 10:42:43 +0300 | [diff] [blame] | 1639 | return ret; |
Pratyush Anand | d6d6ec7 | 2012-05-25 18:54:56 +0530 | [diff] [blame] | 1640 | } |
| 1641 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1642 | static int __dwc3_gadget_ep_queue(struct dwc3_ep *dep, struct dwc3_request *req) |
| 1643 | { |
Felipe Balbi | 0fc9a1b | 2011-12-19 11:32:34 +0200 | [diff] [blame] | 1644 | struct dwc3 *dwc = dep->dwc; |
Felipe Balbi | 0fc9a1b | 2011-12-19 11:32:34 +0200 | [diff] [blame] | 1645 | |
Wesley Cheng | c7bb96a | 2021-03-11 15:59:02 -0800 | [diff] [blame] | 1646 | if (!dep->endpoint.desc || !dwc->pullups_connected || !dwc->connected) { |
Felipe Balbi | 5eb30ce | 2016-11-03 14:07:51 +0200 | [diff] [blame] | 1647 | dev_err(dwc->dev, "%s: can't queue to disabled endpoint\n", |
| 1648 | dep->name); |
Felipe Balbi | bb42398 | 2015-11-16 15:31:21 -0600 | [diff] [blame] | 1649 | return -ESHUTDOWN; |
| 1650 | } |
| 1651 | |
Felipe Balbi | 04fb365 | 2017-05-17 15:57:45 +0300 | [diff] [blame] | 1652 | if (WARN(req->dep != dep, "request %pK belongs to '%s'\n", |
| 1653 | &req->request, req->dep->name)) |
Felipe Balbi | bb42398 | 2015-11-16 15:31:21 -0600 | [diff] [blame] | 1654 | return -EINVAL; |
Felipe Balbi | bb42398 | 2015-11-16 15:31:21 -0600 | [diff] [blame] | 1655 | |
Felipe Balbi | b2b6d60 | 2019-01-11 12:58:52 +0200 | [diff] [blame] | 1656 | if (WARN(req->status < DWC3_REQUEST_STATUS_COMPLETED, |
| 1657 | "%s: request %pK already in flight\n", |
| 1658 | dep->name, &req->request)) |
| 1659 | return -EINVAL; |
| 1660 | |
Felipe Balbi | fc8bb91 | 2016-05-16 13:14:48 +0300 | [diff] [blame] | 1661 | pm_runtime_get(dwc->dev); |
| 1662 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1663 | req->request.actual = 0; |
| 1664 | req->request.status = -EINPROGRESS; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1665 | |
Felipe Balbi | fe84f52 | 2015-09-01 09:01:38 -0500 | [diff] [blame] | 1666 | trace_dwc3_ep_queue(req); |
| 1667 | |
Felipe Balbi | aa3342c | 2016-03-14 11:01:31 +0200 | [diff] [blame] | 1668 | list_add_tail(&req->list, &dep->pending_list); |
Felipe Balbi | a3af5e3 | 2019-01-11 12:57:09 +0200 | [diff] [blame] | 1669 | req->status = DWC3_REQUEST_STATUS_QUEUED; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1670 | |
Thinh Nguyen | e0d1956 | 2020-05-05 19:46:57 -0700 | [diff] [blame] | 1671 | if (dep->flags & DWC3_EP_WAIT_TRANSFER_COMPLETE) |
| 1672 | return 0; |
| 1673 | |
Thinh Nguyen | c503672 | 2020-09-02 18:42:58 -0700 | [diff] [blame] | 1674 | /* |
| 1675 | * Start the transfer only after the END_TRANSFER is completed |
| 1676 | * and endpoint STALL is cleared. |
| 1677 | */ |
| 1678 | if ((dep->flags & DWC3_EP_END_TRANSFER_PENDING) || |
| 1679 | (dep->flags & DWC3_EP_WEDGE) || |
| 1680 | (dep->flags & DWC3_EP_STALL)) { |
Thinh Nguyen | da10bcd | 2019-12-18 18:14:50 -0800 | [diff] [blame] | 1681 | dep->flags |= DWC3_EP_DELAY_START; |
| 1682 | return 0; |
| 1683 | } |
| 1684 | |
Felipe Balbi | d889c23 | 2016-09-29 15:44:29 +0300 | [diff] [blame] | 1685 | /* |
| 1686 | * NOTICE: Isochronous endpoints should NEVER be prestarted. We must |
| 1687 | * wait for a XferNotReady event so we will know what's the current |
| 1688 | * (micro-)frame number. |
| 1689 | * |
| 1690 | * Without this trick, we are very, very likely gonna get Bus Expiry |
| 1691 | * errors which will force us issue EndTransfer command. |
| 1692 | */ |
| 1693 | if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) { |
Felipe Balbi | fe990ce | 2018-03-29 13:23:53 +0300 | [diff] [blame] | 1694 | if (!(dep->flags & DWC3_EP_PENDING_REQUEST) && |
| 1695 | !(dep->flags & DWC3_EP_TRANSFER_STARTED)) |
Roger Quadros | f1d6826 | 2017-04-21 15:58:08 +0300 | [diff] [blame] | 1696 | return 0; |
Felipe Balbi | fe990ce | 2018-03-29 13:23:53 +0300 | [diff] [blame] | 1697 | |
| 1698 | if ((dep->flags & DWC3_EP_PENDING_REQUEST)) { |
Felipe Balbi | e319bd6 | 2020-08-13 08:35:38 +0300 | [diff] [blame] | 1699 | if (!(dep->flags & DWC3_EP_TRANSFER_STARTED)) |
Felipe Balbi | 25abad6 | 2018-08-14 10:41:19 +0300 | [diff] [blame] | 1700 | return __dwc3_gadget_start_isoc(dep); |
Felipe Balbi | 08a36b5 | 2016-08-11 14:27:52 +0300 | [diff] [blame] | 1701 | } |
Felipe Balbi | b511e5e | 2012-06-06 12:00:50 +0300 | [diff] [blame] | 1702 | } |
| 1703 | |
Wesley Cheng | 9bd96a2 | 2021-05-07 10:55:19 -0700 | [diff] [blame] | 1704 | __dwc3_gadget_kick_transfer(dep); |
| 1705 | |
| 1706 | return 0; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1707 | } |
| 1708 | |
| 1709 | static int dwc3_gadget_ep_queue(struct usb_ep *ep, struct usb_request *request, |
| 1710 | gfp_t gfp_flags) |
| 1711 | { |
| 1712 | struct dwc3_request *req = to_dwc3_request(request); |
| 1713 | struct dwc3_ep *dep = to_dwc3_ep(ep); |
| 1714 | struct dwc3 *dwc = dep->dwc; |
| 1715 | |
| 1716 | unsigned long flags; |
| 1717 | |
| 1718 | int ret; |
| 1719 | |
Zhuang Jin Can | fdee4eb | 2014-09-03 14:26:34 +0800 | [diff] [blame] | 1720 | spin_lock_irqsave(&dwc->lock, flags); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1721 | ret = __dwc3_gadget_ep_queue(dep, req); |
| 1722 | spin_unlock_irqrestore(&dwc->lock, flags); |
| 1723 | |
| 1724 | return ret; |
| 1725 | } |
| 1726 | |
Felipe Balbi | 7746a8d | 2018-08-01 13:42:29 +0300 | [diff] [blame] | 1727 | static void dwc3_gadget_ep_skip_trbs(struct dwc3_ep *dep, struct dwc3_request *req) |
| 1728 | { |
| 1729 | int i; |
| 1730 | |
Thinh Nguyen | cb11ea5 | 2020-03-05 13:23:55 -0800 | [diff] [blame] | 1731 | /* If req->trb is not set, then the request has not started */ |
| 1732 | if (!req->trb) |
| 1733 | return; |
| 1734 | |
Felipe Balbi | 7746a8d | 2018-08-01 13:42:29 +0300 | [diff] [blame] | 1735 | /* |
| 1736 | * If request was already started, this means we had to |
| 1737 | * stop the transfer. With that we also need to ignore |
| 1738 | * all TRBs used by the request, however TRBs can only |
| 1739 | * be modified after completion of END_TRANSFER |
| 1740 | * command. So what we do here is that we wait for |
| 1741 | * END_TRANSFER completion and only after that, we jump |
| 1742 | * over TRBs by clearing HWO and incrementing dequeue |
| 1743 | * pointer. |
| 1744 | */ |
| 1745 | for (i = 0; i < req->num_trbs; i++) { |
| 1746 | struct dwc3_trb *trb; |
| 1747 | |
Thinh Nguyen | 2dedea0 | 2020-03-05 13:24:01 -0800 | [diff] [blame] | 1748 | trb = &dep->trb_pool[dep->trb_dequeue]; |
Felipe Balbi | 7746a8d | 2018-08-01 13:42:29 +0300 | [diff] [blame] | 1749 | trb->ctrl &= ~DWC3_TRB_CTRL_HWO; |
| 1750 | dwc3_ep_inc_deq(dep); |
| 1751 | } |
Thinh Nguyen | c715276 | 2019-02-12 19:39:27 -0800 | [diff] [blame] | 1752 | |
| 1753 | req->num_trbs = 0; |
Felipe Balbi | 7746a8d | 2018-08-01 13:42:29 +0300 | [diff] [blame] | 1754 | } |
| 1755 | |
Felipe Balbi | d4f1afe | 2018-08-01 13:54:25 +0300 | [diff] [blame] | 1756 | static void dwc3_gadget_ep_cleanup_cancelled_requests(struct dwc3_ep *dep) |
| 1757 | { |
| 1758 | struct dwc3_request *req; |
| 1759 | struct dwc3_request *tmp; |
| 1760 | |
| 1761 | list_for_each_entry_safe(req, tmp, &dep->cancelled_list, list) { |
| 1762 | dwc3_gadget_ep_skip_trbs(dep, req); |
| 1763 | dwc3_gadget_giveback(dep, req, -ECONNRESET); |
| 1764 | } |
| 1765 | } |
| 1766 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1767 | static int dwc3_gadget_ep_dequeue(struct usb_ep *ep, |
| 1768 | struct usb_request *request) |
| 1769 | { |
| 1770 | struct dwc3_request *req = to_dwc3_request(request); |
| 1771 | struct dwc3_request *r = NULL; |
| 1772 | |
| 1773 | struct dwc3_ep *dep = to_dwc3_ep(ep); |
| 1774 | struct dwc3 *dwc = dep->dwc; |
| 1775 | |
| 1776 | unsigned long flags; |
| 1777 | int ret = 0; |
| 1778 | |
Felipe Balbi | 2c4cbe6e5 | 2014-04-30 17:45:10 -0500 | [diff] [blame] | 1779 | trace_dwc3_ep_dequeue(req); |
| 1780 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1781 | spin_lock_irqsave(&dwc->lock, flags); |
| 1782 | |
Thinh Nguyen | a7027ca | 2020-03-05 13:24:08 -0800 | [diff] [blame] | 1783 | list_for_each_entry(r, &dep->cancelled_list, list) { |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1784 | if (r == req) |
Thinh Nguyen | fcd2def | 2020-03-05 13:24:20 -0800 | [diff] [blame] | 1785 | goto out; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1786 | } |
| 1787 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1788 | list_for_each_entry(r, &dep->pending_list, list) { |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1789 | if (r == req) { |
Thinh Nguyen | fcd2def | 2020-03-05 13:24:20 -0800 | [diff] [blame] | 1790 | dwc3_gadget_giveback(dep, req, -ECONNRESET); |
| 1791 | goto out; |
| 1792 | } |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1793 | } |
| 1794 | |
Thinh Nguyen | fcd2def | 2020-03-05 13:24:20 -0800 | [diff] [blame] | 1795 | list_for_each_entry(r, &dep->started_list, list) { |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1796 | if (r == req) { |
Thinh Nguyen | a7027ca | 2020-03-05 13:24:08 -0800 | [diff] [blame] | 1797 | struct dwc3_request *t; |
| 1798 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1799 | /* wait until it is processed */ |
Felipe Balbi | c5353b2 | 2019-02-13 13:00:54 +0200 | [diff] [blame] | 1800 | dwc3_stop_active_transfer(dep, true, true); |
Felipe Balbi | cf3113d | 2017-02-17 11:12:44 +0200 | [diff] [blame] | 1801 | |
Thinh Nguyen | a7027ca | 2020-03-05 13:24:08 -0800 | [diff] [blame] | 1802 | /* |
| 1803 | * Remove any started request if the transfer is |
| 1804 | * cancelled. |
| 1805 | */ |
| 1806 | list_for_each_entry_safe(r, t, &dep->started_list, list) |
| 1807 | dwc3_gadget_move_cancelled_request(r); |
Felipe Balbi | cf3113d | 2017-02-17 11:12:44 +0200 | [diff] [blame] | 1808 | |
Thinh Nguyen | 8907a10 | 2021-01-04 22:42:39 -0800 | [diff] [blame] | 1809 | dep->flags &= ~DWC3_EP_WAIT_TRANSFER_COMPLETE; |
| 1810 | |
Thinh Nguyen | fcd2def | 2020-03-05 13:24:20 -0800 | [diff] [blame] | 1811 | goto out; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1812 | } |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1813 | } |
| 1814 | |
Thinh Nguyen | fcd2def | 2020-03-05 13:24:20 -0800 | [diff] [blame] | 1815 | dev_err(dwc->dev, "request %pK was not queued to %s\n", |
| 1816 | request, ep->name); |
| 1817 | ret = -EINVAL; |
| 1818 | out: |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1819 | spin_unlock_irqrestore(&dwc->lock, flags); |
| 1820 | |
| 1821 | return ret; |
| 1822 | } |
| 1823 | |
Felipe Balbi | 7a60855 | 2014-09-24 14:19:52 -0500 | [diff] [blame] | 1824 | int __dwc3_gadget_ep_set_halt(struct dwc3_ep *dep, int value, int protocol) |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1825 | { |
| 1826 | struct dwc3_gadget_ep_cmd_params params; |
| 1827 | struct dwc3 *dwc = dep->dwc; |
Thinh Nguyen | cb11ea5 | 2020-03-05 13:23:55 -0800 | [diff] [blame] | 1828 | struct dwc3_request *req; |
| 1829 | struct dwc3_request *tmp; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1830 | int ret; |
| 1831 | |
Felipe Balbi | 5ad02fb | 2014-09-24 10:48:26 -0500 | [diff] [blame] | 1832 | if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) { |
| 1833 | dev_err(dwc->dev, "%s is of Isochronous type\n", dep->name); |
| 1834 | return -EINVAL; |
| 1835 | } |
| 1836 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1837 | memset(¶ms, 0x00, sizeof(params)); |
| 1838 | |
| 1839 | if (value) { |
Felipe Balbi | 69450c4 | 2016-05-30 13:37:02 +0300 | [diff] [blame] | 1840 | struct dwc3_trb *trb; |
| 1841 | |
Felipe Balbi | e319bd6 | 2020-08-13 08:35:38 +0300 | [diff] [blame] | 1842 | unsigned int transfer_in_flight; |
| 1843 | unsigned int started; |
Felipe Balbi | 69450c4 | 2016-05-30 13:37:02 +0300 | [diff] [blame] | 1844 | |
| 1845 | if (dep->number > 1) |
| 1846 | trb = dwc3_ep_prev_trb(dep, dep->trb_enqueue); |
| 1847 | else |
| 1848 | trb = &dwc->ep0_trb[dep->trb_enqueue]; |
| 1849 | |
| 1850 | transfer_in_flight = trb->ctrl & DWC3_TRB_CTRL_HWO; |
| 1851 | started = !list_empty(&dep->started_list); |
| 1852 | |
| 1853 | if (!protocol && ((dep->direction && transfer_in_flight) || |
| 1854 | (!dep->direction && started))) { |
Felipe Balbi | 7a60855 | 2014-09-24 14:19:52 -0500 | [diff] [blame] | 1855 | return -EAGAIN; |
| 1856 | } |
| 1857 | |
Felipe Balbi | 2cd4718 | 2016-04-12 16:42:43 +0300 | [diff] [blame] | 1858 | ret = dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETSTALL, |
| 1859 | ¶ms); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1860 | if (ret) |
Dan Carpenter | 3f89204 | 2014-03-07 14:20:22 +0300 | [diff] [blame] | 1861 | dev_err(dwc->dev, "failed to set STALL on %s\n", |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1862 | dep->name); |
| 1863 | else |
| 1864 | dep->flags |= DWC3_EP_STALL; |
| 1865 | } else { |
Thinh Nguyen | cb11ea5 | 2020-03-05 13:23:55 -0800 | [diff] [blame] | 1866 | /* |
| 1867 | * Don't issue CLEAR_STALL command to control endpoints. The |
| 1868 | * controller automatically clears the STALL when it receives |
| 1869 | * the SETUP token. |
| 1870 | */ |
| 1871 | if (dep->number <= 1) { |
| 1872 | dep->flags &= ~(DWC3_EP_STALL | DWC3_EP_WEDGE); |
| 1873 | return 0; |
| 1874 | } |
Felipe Balbi | 2cd4718 | 2016-04-12 16:42:43 +0300 | [diff] [blame] | 1875 | |
Thinh Nguyen | d97c78a | 2020-09-02 18:43:04 -0700 | [diff] [blame] | 1876 | dwc3_stop_active_transfer(dep, true, true); |
| 1877 | |
| 1878 | list_for_each_entry_safe(req, tmp, &dep->started_list, list) |
| 1879 | dwc3_gadget_move_cancelled_request(req); |
| 1880 | |
| 1881 | if (dep->flags & DWC3_EP_END_TRANSFER_PENDING) { |
| 1882 | dep->flags |= DWC3_EP_PENDING_CLEAR_STALL; |
| 1883 | return 0; |
| 1884 | } |
| 1885 | |
| 1886 | dwc3_gadget_ep_cleanup_cancelled_requests(dep); |
| 1887 | |
John Youn | 50c763f | 2016-05-31 17:49:56 -0700 | [diff] [blame] | 1888 | ret = dwc3_send_clear_stall_ep_cmd(dep); |
Thinh Nguyen | cb11ea5 | 2020-03-05 13:23:55 -0800 | [diff] [blame] | 1889 | if (ret) { |
Dan Carpenter | 3f89204 | 2014-03-07 14:20:22 +0300 | [diff] [blame] | 1890 | dev_err(dwc->dev, "failed to clear STALL on %s\n", |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1891 | dep->name); |
Thinh Nguyen | cb11ea5 | 2020-03-05 13:23:55 -0800 | [diff] [blame] | 1892 | return ret; |
| 1893 | } |
| 1894 | |
| 1895 | dep->flags &= ~(DWC3_EP_STALL | DWC3_EP_WEDGE); |
| 1896 | |
Thinh Nguyen | c503672 | 2020-09-02 18:42:58 -0700 | [diff] [blame] | 1897 | if ((dep->flags & DWC3_EP_DELAY_START) && |
| 1898 | !usb_endpoint_xfer_isoc(dep->endpoint.desc)) |
| 1899 | __dwc3_gadget_kick_transfer(dep); |
| 1900 | |
| 1901 | dep->flags &= ~DWC3_EP_DELAY_START; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1902 | } |
Paul Zimmerman | 5275455 | 2011-09-30 10:58:44 +0300 | [diff] [blame] | 1903 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1904 | return ret; |
| 1905 | } |
| 1906 | |
| 1907 | static int dwc3_gadget_ep_set_halt(struct usb_ep *ep, int value) |
| 1908 | { |
| 1909 | struct dwc3_ep *dep = to_dwc3_ep(ep); |
| 1910 | struct dwc3 *dwc = dep->dwc; |
| 1911 | |
| 1912 | unsigned long flags; |
| 1913 | |
| 1914 | int ret; |
| 1915 | |
| 1916 | spin_lock_irqsave(&dwc->lock, flags); |
Felipe Balbi | 7a60855 | 2014-09-24 14:19:52 -0500 | [diff] [blame] | 1917 | ret = __dwc3_gadget_ep_set_halt(dep, value, false); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1918 | spin_unlock_irqrestore(&dwc->lock, flags); |
| 1919 | |
| 1920 | return ret; |
| 1921 | } |
| 1922 | |
| 1923 | static int dwc3_gadget_ep_set_wedge(struct usb_ep *ep) |
| 1924 | { |
| 1925 | struct dwc3_ep *dep = to_dwc3_ep(ep); |
Paul Zimmerman | 249a456 | 2012-02-24 17:32:16 -0800 | [diff] [blame] | 1926 | struct dwc3 *dwc = dep->dwc; |
| 1927 | unsigned long flags; |
Felipe Balbi | 95aa4e8 | 2014-09-24 10:50:14 -0500 | [diff] [blame] | 1928 | int ret; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1929 | |
Paul Zimmerman | 249a456 | 2012-02-24 17:32:16 -0800 | [diff] [blame] | 1930 | spin_lock_irqsave(&dwc->lock, flags); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1931 | dep->flags |= DWC3_EP_WEDGE; |
| 1932 | |
Pratyush Anand | 08f0d96 | 2012-06-25 22:40:43 +0530 | [diff] [blame] | 1933 | if (dep->number == 0 || dep->number == 1) |
Felipe Balbi | 95aa4e8 | 2014-09-24 10:50:14 -0500 | [diff] [blame] | 1934 | ret = __dwc3_gadget_ep0_set_halt(ep, 1); |
Pratyush Anand | 08f0d96 | 2012-06-25 22:40:43 +0530 | [diff] [blame] | 1935 | else |
Felipe Balbi | 7a60855 | 2014-09-24 14:19:52 -0500 | [diff] [blame] | 1936 | ret = __dwc3_gadget_ep_set_halt(dep, 1, false); |
Felipe Balbi | 95aa4e8 | 2014-09-24 10:50:14 -0500 | [diff] [blame] | 1937 | spin_unlock_irqrestore(&dwc->lock, flags); |
| 1938 | |
| 1939 | return ret; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1940 | } |
| 1941 | |
| 1942 | /* -------------------------------------------------------------------------- */ |
| 1943 | |
| 1944 | static struct usb_endpoint_descriptor dwc3_gadget_ep0_desc = { |
| 1945 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 1946 | .bDescriptorType = USB_DT_ENDPOINT, |
| 1947 | .bmAttributes = USB_ENDPOINT_XFER_CONTROL, |
| 1948 | }; |
| 1949 | |
| 1950 | static const struct usb_ep_ops dwc3_gadget_ep0_ops = { |
| 1951 | .enable = dwc3_gadget_ep0_enable, |
| 1952 | .disable = dwc3_gadget_ep0_disable, |
| 1953 | .alloc_request = dwc3_gadget_ep_alloc_request, |
| 1954 | .free_request = dwc3_gadget_ep_free_request, |
| 1955 | .queue = dwc3_gadget_ep0_queue, |
| 1956 | .dequeue = dwc3_gadget_ep_dequeue, |
Pratyush Anand | 08f0d96 | 2012-06-25 22:40:43 +0530 | [diff] [blame] | 1957 | .set_halt = dwc3_gadget_ep0_set_halt, |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1958 | .set_wedge = dwc3_gadget_ep_set_wedge, |
| 1959 | }; |
| 1960 | |
| 1961 | static const struct usb_ep_ops dwc3_gadget_ep_ops = { |
| 1962 | .enable = dwc3_gadget_ep_enable, |
| 1963 | .disable = dwc3_gadget_ep_disable, |
| 1964 | .alloc_request = dwc3_gadget_ep_alloc_request, |
| 1965 | .free_request = dwc3_gadget_ep_free_request, |
| 1966 | .queue = dwc3_gadget_ep_queue, |
| 1967 | .dequeue = dwc3_gadget_ep_dequeue, |
| 1968 | .set_halt = dwc3_gadget_ep_set_halt, |
| 1969 | .set_wedge = dwc3_gadget_ep_set_wedge, |
| 1970 | }; |
| 1971 | |
| 1972 | /* -------------------------------------------------------------------------- */ |
| 1973 | |
| 1974 | static int dwc3_gadget_get_frame(struct usb_gadget *g) |
| 1975 | { |
| 1976 | struct dwc3 *dwc = gadget_to_dwc(g); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1977 | |
Felipe Balbi | 6cb2e4e | 2016-10-21 13:07:09 +0300 | [diff] [blame] | 1978 | return __dwc3_gadget_get_frame(dwc); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1979 | } |
| 1980 | |
Felipe Balbi | 218ef7b | 2016-04-04 11:24:04 +0300 | [diff] [blame] | 1981 | static int __dwc3_gadget_wakeup(struct dwc3 *dwc) |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1982 | { |
Nicolas Saenz Julienne | d6011f6 | 2016-08-16 10:22:38 +0100 | [diff] [blame] | 1983 | int retries; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1984 | |
Felipe Balbi | 218ef7b | 2016-04-04 11:24:04 +0300 | [diff] [blame] | 1985 | int ret; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1986 | u32 reg; |
| 1987 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1988 | u8 link_state; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1989 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1990 | /* |
| 1991 | * According to the Databook Remote wakeup request should |
| 1992 | * be issued only when the device is in early suspend state. |
| 1993 | * |
| 1994 | * We can check that via USB Link State bits in DSTS register. |
| 1995 | */ |
| 1996 | reg = dwc3_readl(dwc->regs, DWC3_DSTS); |
| 1997 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1998 | link_state = DWC3_DSTS_USBLNKST(reg); |
| 1999 | |
| 2000 | switch (link_state) { |
Thinh Nguyen | d0550cd | 2020-01-31 16:25:50 -0800 | [diff] [blame] | 2001 | case DWC3_LINK_STATE_RESET: |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2002 | case DWC3_LINK_STATE_RX_DET: /* in HS, means Early Suspend */ |
| 2003 | case DWC3_LINK_STATE_U3: /* in HS, means SUSPEND */ |
Thinh Nguyen | b624b32 | 2021-04-19 19:11:12 -0700 | [diff] [blame] | 2004 | case DWC3_LINK_STATE_U2: /* in HS, means Sleep (L1) */ |
| 2005 | case DWC3_LINK_STATE_U1: |
Thinh Nguyen | d0550cd | 2020-01-31 16:25:50 -0800 | [diff] [blame] | 2006 | case DWC3_LINK_STATE_RESUME: |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2007 | break; |
| 2008 | default: |
Felipe Balbi | 218ef7b | 2016-04-04 11:24:04 +0300 | [diff] [blame] | 2009 | return -EINVAL; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2010 | } |
| 2011 | |
Felipe Balbi | 8598bde | 2012-01-02 18:55:57 +0200 | [diff] [blame] | 2012 | ret = dwc3_gadget_set_link_state(dwc, DWC3_LINK_STATE_RECOV); |
| 2013 | if (ret < 0) { |
| 2014 | dev_err(dwc->dev, "failed to put link in Recovery\n"); |
Felipe Balbi | 218ef7b | 2016-04-04 11:24:04 +0300 | [diff] [blame] | 2015 | return ret; |
Felipe Balbi | 8598bde | 2012-01-02 18:55:57 +0200 | [diff] [blame] | 2016 | } |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2017 | |
Paul Zimmerman | 802fde9 | 2012-04-27 13:10:52 +0300 | [diff] [blame] | 2018 | /* Recent versions do this automatically */ |
Thinh Nguyen | 9af21dd | 2020-04-11 19:20:01 -0700 | [diff] [blame] | 2019 | if (DWC3_VER_IS_PRIOR(DWC3, 194A)) { |
Paul Zimmerman | 802fde9 | 2012-04-27 13:10:52 +0300 | [diff] [blame] | 2020 | /* write zeroes to Link Change Request */ |
Felipe Balbi | fcc023c | 2012-05-24 10:27:56 +0300 | [diff] [blame] | 2021 | reg = dwc3_readl(dwc->regs, DWC3_DCTL); |
Paul Zimmerman | 802fde9 | 2012-04-27 13:10:52 +0300 | [diff] [blame] | 2022 | reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK; |
| 2023 | dwc3_writel(dwc->regs, DWC3_DCTL, reg); |
| 2024 | } |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2025 | |
Paul Zimmerman | 1d04679 | 2012-02-15 18:56:56 -0800 | [diff] [blame] | 2026 | /* poll until Link State changes to ON */ |
Nicolas Saenz Julienne | d6011f6 | 2016-08-16 10:22:38 +0100 | [diff] [blame] | 2027 | retries = 20000; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2028 | |
Nicolas Saenz Julienne | d6011f6 | 2016-08-16 10:22:38 +0100 | [diff] [blame] | 2029 | while (retries--) { |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2030 | reg = dwc3_readl(dwc->regs, DWC3_DSTS); |
| 2031 | |
| 2032 | /* in HS, means ON */ |
| 2033 | if (DWC3_DSTS_USBLNKST(reg) == DWC3_LINK_STATE_U0) |
| 2034 | break; |
| 2035 | } |
| 2036 | |
| 2037 | if (DWC3_DSTS_USBLNKST(reg) != DWC3_LINK_STATE_U0) { |
| 2038 | dev_err(dwc->dev, "failed to send remote wakeup\n"); |
Felipe Balbi | 218ef7b | 2016-04-04 11:24:04 +0300 | [diff] [blame] | 2039 | return -EINVAL; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2040 | } |
| 2041 | |
Felipe Balbi | 218ef7b | 2016-04-04 11:24:04 +0300 | [diff] [blame] | 2042 | return 0; |
| 2043 | } |
| 2044 | |
| 2045 | static int dwc3_gadget_wakeup(struct usb_gadget *g) |
| 2046 | { |
| 2047 | struct dwc3 *dwc = gadget_to_dwc(g); |
| 2048 | unsigned long flags; |
| 2049 | int ret; |
| 2050 | |
| 2051 | spin_lock_irqsave(&dwc->lock, flags); |
| 2052 | ret = __dwc3_gadget_wakeup(dwc); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2053 | spin_unlock_irqrestore(&dwc->lock, flags); |
| 2054 | |
| 2055 | return ret; |
| 2056 | } |
| 2057 | |
| 2058 | static int dwc3_gadget_set_selfpowered(struct usb_gadget *g, |
| 2059 | int is_selfpowered) |
| 2060 | { |
| 2061 | struct dwc3 *dwc = gadget_to_dwc(g); |
Paul Zimmerman | 249a456 | 2012-02-24 17:32:16 -0800 | [diff] [blame] | 2062 | unsigned long flags; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2063 | |
Paul Zimmerman | 249a456 | 2012-02-24 17:32:16 -0800 | [diff] [blame] | 2064 | spin_lock_irqsave(&dwc->lock, flags); |
Peter Chen | bcdea50 | 2015-01-28 16:32:40 +0800 | [diff] [blame] | 2065 | g->is_selfpowered = !!is_selfpowered; |
Paul Zimmerman | 249a456 | 2012-02-24 17:32:16 -0800 | [diff] [blame] | 2066 | spin_unlock_irqrestore(&dwc->lock, flags); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2067 | |
| 2068 | return 0; |
| 2069 | } |
| 2070 | |
Wesley Cheng | ae7e861 | 2020-09-28 17:20:59 -0700 | [diff] [blame] | 2071 | static void dwc3_stop_active_transfers(struct dwc3 *dwc) |
| 2072 | { |
| 2073 | u32 epnum; |
| 2074 | |
| 2075 | for (epnum = 2; epnum < dwc->num_eps; epnum++) { |
| 2076 | struct dwc3_ep *dep; |
| 2077 | |
| 2078 | dep = dwc->eps[epnum]; |
| 2079 | if (!dep) |
| 2080 | continue; |
| 2081 | |
| 2082 | dwc3_remove_requests(dwc, dep); |
| 2083 | } |
| 2084 | } |
| 2085 | |
Felipe Balbi | 7b2a036 | 2013-12-19 13:43:19 -0600 | [diff] [blame] | 2086 | static int dwc3_gadget_run_stop(struct dwc3 *dwc, int is_on, int suspend) |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2087 | { |
| 2088 | u32 reg; |
Sebastian Andrzej Siewior | 61d5824 | 2011-08-29 16:46:38 +0200 | [diff] [blame] | 2089 | u32 timeout = 500; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2090 | |
Felipe Balbi | fc8bb91 | 2016-05-16 13:14:48 +0300 | [diff] [blame] | 2091 | if (pm_runtime_suspended(dwc->dev)) |
| 2092 | return 0; |
| 2093 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2094 | reg = dwc3_readl(dwc->regs, DWC3_DCTL); |
Felipe Balbi | 8db7ed1 | 2012-01-18 18:32:29 +0200 | [diff] [blame] | 2095 | if (is_on) { |
Thinh Nguyen | 9af21dd | 2020-04-11 19:20:01 -0700 | [diff] [blame] | 2096 | if (DWC3_VER_IS_WITHIN(DWC3, ANY, 187A)) { |
Paul Zimmerman | 802fde9 | 2012-04-27 13:10:52 +0300 | [diff] [blame] | 2097 | reg &= ~DWC3_DCTL_TRGTULST_MASK; |
| 2098 | reg |= DWC3_DCTL_TRGTULST_RX_DET; |
| 2099 | } |
| 2100 | |
Thinh Nguyen | 9af21dd | 2020-04-11 19:20:01 -0700 | [diff] [blame] | 2101 | if (!DWC3_VER_IS_PRIOR(DWC3, 194A)) |
Paul Zimmerman | 802fde9 | 2012-04-27 13:10:52 +0300 | [diff] [blame] | 2102 | reg &= ~DWC3_DCTL_KEEP_CONNECT; |
| 2103 | reg |= DWC3_DCTL_RUN_STOP; |
Felipe Balbi | 7b2a036 | 2013-12-19 13:43:19 -0600 | [diff] [blame] | 2104 | |
| 2105 | if (dwc->has_hibernation) |
| 2106 | reg |= DWC3_DCTL_KEEP_CONNECT; |
| 2107 | |
Felipe Balbi | 9fcb3bd | 2013-02-08 17:55:58 +0200 | [diff] [blame] | 2108 | dwc->pullups_connected = true; |
Felipe Balbi | 8db7ed1 | 2012-01-18 18:32:29 +0200 | [diff] [blame] | 2109 | } else { |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2110 | reg &= ~DWC3_DCTL_RUN_STOP; |
Felipe Balbi | 7b2a036 | 2013-12-19 13:43:19 -0600 | [diff] [blame] | 2111 | |
| 2112 | if (dwc->has_hibernation && !suspend) |
| 2113 | reg &= ~DWC3_DCTL_KEEP_CONNECT; |
| 2114 | |
Felipe Balbi | 9fcb3bd | 2013-02-08 17:55:58 +0200 | [diff] [blame] | 2115 | dwc->pullups_connected = false; |
Felipe Balbi | 8db7ed1 | 2012-01-18 18:32:29 +0200 | [diff] [blame] | 2116 | } |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2117 | |
Thinh Nguyen | 5b73821 | 2019-10-23 19:15:43 -0700 | [diff] [blame] | 2118 | dwc3_gadget_dctl_write_safe(dwc, reg); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2119 | |
| 2120 | do { |
| 2121 | reg = dwc3_readl(dwc->regs, DWC3_DSTS); |
Felipe Balbi | b6d4e16 | 2016-06-09 16:47:05 +0300 | [diff] [blame] | 2122 | reg &= DWC3_DSTS_DEVCTRLHLT; |
| 2123 | } while (--timeout && !(!is_on ^ !reg)); |
Felipe Balbi | f2df679 | 2016-06-09 16:31:34 +0300 | [diff] [blame] | 2124 | |
| 2125 | if (!timeout) |
| 2126 | return -ETIMEDOUT; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2127 | |
Pratyush Anand | 6f17f74 | 2012-07-02 10:21:55 +0530 | [diff] [blame] | 2128 | return 0; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2129 | } |
| 2130 | |
Wesley Cheng | ae7e861 | 2020-09-28 17:20:59 -0700 | [diff] [blame] | 2131 | static void dwc3_gadget_disable_irq(struct dwc3 *dwc); |
| 2132 | static void __dwc3_gadget_stop(struct dwc3 *dwc); |
Wesley Cheng | dd8363f | 2020-12-29 15:00:37 -0800 | [diff] [blame] | 2133 | static int __dwc3_gadget_start(struct dwc3 *dwc); |
Wesley Cheng | ae7e861 | 2020-09-28 17:20:59 -0700 | [diff] [blame] | 2134 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2135 | static int dwc3_gadget_pullup(struct usb_gadget *g, int is_on) |
| 2136 | { |
| 2137 | struct dwc3 *dwc = gadget_to_dwc(g); |
| 2138 | unsigned long flags; |
Pratyush Anand | 6f17f74 | 2012-07-02 10:21:55 +0530 | [diff] [blame] | 2139 | int ret; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2140 | |
| 2141 | is_on = !!is_on; |
| 2142 | |
Baolin Wang | bb01473 | 2016-10-14 17:11:33 +0800 | [diff] [blame] | 2143 | /* |
| 2144 | * Per databook, when we want to stop the gadget, if a control transfer |
| 2145 | * is still in process, complete it and get the core into setup phase. |
| 2146 | */ |
| 2147 | if (!is_on && dwc->ep0state != EP0_SETUP_PHASE) { |
| 2148 | reinit_completion(&dwc->ep0_in_setup); |
| 2149 | |
| 2150 | ret = wait_for_completion_timeout(&dwc->ep0_in_setup, |
| 2151 | msecs_to_jiffies(DWC3_PULL_UP_TIMEOUT)); |
Wesley Cheng | 01da7c1 | 2021-08-24 21:28:55 -0700 | [diff] [blame] | 2152 | if (ret == 0) |
| 2153 | dev_warn(dwc->dev, "timed out waiting for SETUP phase\n"); |
Baolin Wang | bb01473 | 2016-10-14 17:11:33 +0800 | [diff] [blame] | 2154 | } |
| 2155 | |
Wesley Cheng | ae7e861 | 2020-09-28 17:20:59 -0700 | [diff] [blame] | 2156 | /* |
Wesley Cheng | 98c83d7 | 2021-08-03 23:24:05 -0700 | [diff] [blame] | 2157 | * Avoid issuing a runtime resume if the device is already in the |
| 2158 | * suspended state during gadget disconnect. DWC3 gadget was already |
| 2159 | * halted/stopped during runtime suspend. |
| 2160 | */ |
| 2161 | if (!is_on) { |
| 2162 | pm_runtime_barrier(dwc->dev); |
| 2163 | if (pm_runtime_suspended(dwc->dev)) |
| 2164 | return 0; |
| 2165 | } |
| 2166 | |
| 2167 | /* |
Wesley Cheng | 395d273 | 2020-12-29 15:05:35 -0800 | [diff] [blame] | 2168 | * Check the return value for successful resume, or error. For a |
| 2169 | * successful resume, the DWC3 runtime PM resume routine will handle |
| 2170 | * the run stop sequence, so avoid duplicate operations here. |
| 2171 | */ |
| 2172 | ret = pm_runtime_get_sync(dwc->dev); |
| 2173 | if (!ret || ret < 0) { |
| 2174 | pm_runtime_put(dwc->dev); |
| 2175 | return 0; |
| 2176 | } |
| 2177 | |
| 2178 | /* |
Wesley Cheng | 9e0677c | 2021-05-20 21:23:57 -0700 | [diff] [blame] | 2179 | * Synchronize and disable any further event handling while controller |
| 2180 | * is being enabled/disabled. |
Wesley Cheng | ae7e861 | 2020-09-28 17:20:59 -0700 | [diff] [blame] | 2181 | */ |
Wesley Cheng | 9e0677c | 2021-05-20 21:23:57 -0700 | [diff] [blame] | 2182 | disable_irq(dwc->irq_gadget); |
Wesley Cheng | ae7e861 | 2020-09-28 17:20:59 -0700 | [diff] [blame] | 2183 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2184 | spin_lock_irqsave(&dwc->lock, flags); |
Wesley Cheng | ae7e861 | 2020-09-28 17:20:59 -0700 | [diff] [blame] | 2185 | |
| 2186 | if (!is_on) { |
| 2187 | u32 count; |
| 2188 | |
Wesley Cheng | c7bb96a | 2021-03-11 15:59:02 -0800 | [diff] [blame] | 2189 | dwc->connected = false; |
Wesley Cheng | ae7e861 | 2020-09-28 17:20:59 -0700 | [diff] [blame] | 2190 | /* |
| 2191 | * In the Synopsis DesignWare Cores USB3 Databook Rev. 3.30a |
| 2192 | * Section 4.1.8 Table 4-7, it states that for a device-initiated |
| 2193 | * disconnect, the SW needs to ensure that it sends "a DEPENDXFER |
| 2194 | * command for any active transfers" before clearing the RunStop |
| 2195 | * bit. |
| 2196 | */ |
| 2197 | dwc3_stop_active_transfers(dwc); |
| 2198 | __dwc3_gadget_stop(dwc); |
| 2199 | |
| 2200 | /* |
| 2201 | * In the Synopsis DesignWare Cores USB3 Databook Rev. 3.30a |
| 2202 | * Section 1.3.4, it mentions that for the DEVCTRLHLT bit, the |
| 2203 | * "software needs to acknowledge the events that are generated |
| 2204 | * (by writing to GEVNTCOUNTn) while it is waiting for this bit |
| 2205 | * to be set to '1'." |
| 2206 | */ |
| 2207 | count = dwc3_readl(dwc->regs, DWC3_GEVNTCOUNT(0)); |
| 2208 | count &= DWC3_GEVNTCOUNT_MASK; |
| 2209 | if (count > 0) { |
| 2210 | dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), count); |
| 2211 | dwc->ev_buf->lpos = (dwc->ev_buf->lpos + count) % |
| 2212 | dwc->ev_buf->length; |
| 2213 | } |
Wesley Cheng | dd8363f | 2020-12-29 15:00:37 -0800 | [diff] [blame] | 2214 | } else { |
| 2215 | __dwc3_gadget_start(dwc); |
Wesley Cheng | ae7e861 | 2020-09-28 17:20:59 -0700 | [diff] [blame] | 2216 | } |
| 2217 | |
Felipe Balbi | 7b2a036 | 2013-12-19 13:43:19 -0600 | [diff] [blame] | 2218 | ret = dwc3_gadget_run_stop(dwc, is_on, false); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2219 | spin_unlock_irqrestore(&dwc->lock, flags); |
Wesley Cheng | 9e0677c | 2021-05-20 21:23:57 -0700 | [diff] [blame] | 2220 | enable_irq(dwc->irq_gadget); |
| 2221 | |
Wesley Cheng | 395d273 | 2020-12-29 15:05:35 -0800 | [diff] [blame] | 2222 | pm_runtime_put(dwc->dev); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2223 | |
Pratyush Anand | 6f17f74 | 2012-07-02 10:21:55 +0530 | [diff] [blame] | 2224 | return ret; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2225 | } |
| 2226 | |
Felipe Balbi | 8698e2a | 2013-02-08 15:24:04 +0200 | [diff] [blame] | 2227 | static void dwc3_gadget_enable_irq(struct dwc3 *dwc) |
| 2228 | { |
| 2229 | u32 reg; |
| 2230 | |
| 2231 | /* Enable all but Start and End of Frame IRQs */ |
| 2232 | reg = (DWC3_DEVTEN_VNDRDEVTSTRCVEDEN | |
| 2233 | DWC3_DEVTEN_EVNTOVERFLOWEN | |
| 2234 | DWC3_DEVTEN_CMDCMPLTEN | |
| 2235 | DWC3_DEVTEN_ERRTICERREN | |
| 2236 | DWC3_DEVTEN_WKUPEVTEN | |
Felipe Balbi | 8698e2a | 2013-02-08 15:24:04 +0200 | [diff] [blame] | 2237 | DWC3_DEVTEN_CONNECTDONEEN | |
| 2238 | DWC3_DEVTEN_USBRSTEN | |
| 2239 | DWC3_DEVTEN_DISCONNEVTEN); |
| 2240 | |
Thinh Nguyen | 9af21dd | 2020-04-11 19:20:01 -0700 | [diff] [blame] | 2241 | if (DWC3_VER_IS_PRIOR(DWC3, 250A)) |
Felipe Balbi | 799e9dc | 2016-09-23 11:20:40 +0300 | [diff] [blame] | 2242 | reg |= DWC3_DEVTEN_ULSTCNGEN; |
| 2243 | |
Jack Pham | 45f37f5 | 2021-04-28 02:01:10 -0700 | [diff] [blame] | 2244 | /* On 2.30a and above this bit enables U3/L2-L1 Suspend Events */ |
| 2245 | if (!DWC3_VER_IS_PRIOR(DWC3, 230A)) |
| 2246 | reg |= DWC3_DEVTEN_EOPFEN; |
| 2247 | |
Felipe Balbi | 8698e2a | 2013-02-08 15:24:04 +0200 | [diff] [blame] | 2248 | dwc3_writel(dwc->regs, DWC3_DEVTEN, reg); |
| 2249 | } |
| 2250 | |
| 2251 | static void dwc3_gadget_disable_irq(struct dwc3 *dwc) |
| 2252 | { |
| 2253 | /* mask all interrupts */ |
| 2254 | dwc3_writel(dwc->regs, DWC3_DEVTEN, 0x00); |
| 2255 | } |
| 2256 | |
| 2257 | static irqreturn_t dwc3_interrupt(int irq, void *_dwc); |
Felipe Balbi | b15a762 | 2011-06-30 16:57:15 +0300 | [diff] [blame] | 2258 | static irqreturn_t dwc3_thread_interrupt(int irq, void *_dwc); |
Felipe Balbi | 8698e2a | 2013-02-08 15:24:04 +0200 | [diff] [blame] | 2259 | |
Felipe Balbi | 4e99472 | 2016-05-13 14:09:59 +0300 | [diff] [blame] | 2260 | /** |
Felipe Balbi | bfad65e | 2017-04-19 14:59:27 +0300 | [diff] [blame] | 2261 | * dwc3_gadget_setup_nump - calculate and initialize NUMP field of %DWC3_DCFG |
| 2262 | * @dwc: pointer to our context structure |
Felipe Balbi | 4e99472 | 2016-05-13 14:09:59 +0300 | [diff] [blame] | 2263 | * |
| 2264 | * The following looks like complex but it's actually very simple. In order to |
| 2265 | * calculate the number of packets we can burst at once on OUT transfers, we're |
| 2266 | * gonna use RxFIFO size. |
| 2267 | * |
| 2268 | * To calculate RxFIFO size we need two numbers: |
| 2269 | * MDWIDTH = size, in bits, of the internal memory bus |
| 2270 | * RAM2_DEPTH = depth, in MDWIDTH, of internal RAM2 (where RxFIFO sits) |
| 2271 | * |
| 2272 | * Given these two numbers, the formula is simple: |
| 2273 | * |
| 2274 | * RxFIFO Size = (RAM2_DEPTH * MDWIDTH / 8) - 24 - 16; |
| 2275 | * |
| 2276 | * 24 bytes is for 3x SETUP packets |
| 2277 | * 16 bytes is a clock domain crossing tolerance |
| 2278 | * |
| 2279 | * Given RxFIFO Size, NUMP = RxFIFOSize / 1024; |
| 2280 | */ |
| 2281 | static void dwc3_gadget_setup_nump(struct dwc3 *dwc) |
| 2282 | { |
| 2283 | u32 ram2_depth; |
| 2284 | u32 mdwidth; |
| 2285 | u32 nump; |
| 2286 | u32 reg; |
| 2287 | |
| 2288 | ram2_depth = DWC3_GHWPARAMS7_RAM2_DEPTH(dwc->hwparams.hwparams7); |
| 2289 | mdwidth = DWC3_GHWPARAMS0_MDWIDTH(dwc->hwparams.hwparams0); |
Thinh Nguyen | 4244ba0 | 2020-04-11 19:20:07 -0700 | [diff] [blame] | 2290 | if (DWC3_IP_IS(DWC32)) |
| 2291 | mdwidth += DWC3_GHWPARAMS6_MDWIDTH(dwc->hwparams.hwparams6); |
Felipe Balbi | 4e99472 | 2016-05-13 14:09:59 +0300 | [diff] [blame] | 2292 | |
| 2293 | nump = ((ram2_depth * mdwidth / 8) - 24 - 16) / 1024; |
| 2294 | nump = min_t(u32, nump, 16); |
| 2295 | |
| 2296 | /* update NumP */ |
| 2297 | reg = dwc3_readl(dwc->regs, DWC3_DCFG); |
| 2298 | reg &= ~DWC3_DCFG_NUMP_MASK; |
| 2299 | reg |= nump << DWC3_DCFG_NUMP_SHIFT; |
| 2300 | dwc3_writel(dwc->regs, DWC3_DCFG, reg); |
| 2301 | } |
| 2302 | |
Felipe Balbi | d7be295 | 2016-05-04 15:49:37 +0300 | [diff] [blame] | 2303 | static int __dwc3_gadget_start(struct dwc3 *dwc) |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2304 | { |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2305 | struct dwc3_ep *dep; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2306 | int ret = 0; |
| 2307 | u32 reg; |
| 2308 | |
John Youn | cf40b86 | 2016-11-14 12:32:43 -0800 | [diff] [blame] | 2309 | /* |
| 2310 | * Use IMOD if enabled via dwc->imod_interval. Otherwise, if |
| 2311 | * the core supports IMOD, disable it. |
| 2312 | */ |
| 2313 | if (dwc->imod_interval) { |
| 2314 | dwc3_writel(dwc->regs, DWC3_DEV_IMOD(0), dwc->imod_interval); |
| 2315 | dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), DWC3_GEVNTCOUNT_EHB); |
| 2316 | } else if (dwc3_has_imod(dwc)) { |
| 2317 | dwc3_writel(dwc->regs, DWC3_DEV_IMOD(0), 0); |
| 2318 | } |
| 2319 | |
Felipe Balbi | 2a58f9c | 2016-04-28 10:56:28 +0300 | [diff] [blame] | 2320 | /* |
| 2321 | * We are telling dwc3 that we want to use DCFG.NUMP as ACK TP's NUMP |
| 2322 | * field instead of letting dwc3 itself calculate that automatically. |
| 2323 | * |
| 2324 | * This way, we maximize the chances that we'll be able to get several |
| 2325 | * bursts of data without going through any sort of endpoint throttling. |
| 2326 | */ |
| 2327 | reg = dwc3_readl(dwc->regs, DWC3_GRXTHRCFG); |
Thinh Nguyen | 9af21dd | 2020-04-11 19:20:01 -0700 | [diff] [blame] | 2328 | if (DWC3_IP_IS(DWC3)) |
Thinh Nguyen | 01b0e2c | 2018-03-16 15:34:13 -0700 | [diff] [blame] | 2329 | reg &= ~DWC3_GRXTHRCFG_PKTCNTSEL; |
Thinh Nguyen | 9af21dd | 2020-04-11 19:20:01 -0700 | [diff] [blame] | 2330 | else |
| 2331 | reg &= ~DWC31_GRXTHRCFG_PKTCNTSEL; |
Thinh Nguyen | 01b0e2c | 2018-03-16 15:34:13 -0700 | [diff] [blame] | 2332 | |
Felipe Balbi | 2a58f9c | 2016-04-28 10:56:28 +0300 | [diff] [blame] | 2333 | dwc3_writel(dwc->regs, DWC3_GRXTHRCFG, reg); |
| 2334 | |
Felipe Balbi | 4e99472 | 2016-05-13 14:09:59 +0300 | [diff] [blame] | 2335 | dwc3_gadget_setup_nump(dwc); |
| 2336 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2337 | /* Start with SuperSpeed Default */ |
| 2338 | dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512); |
| 2339 | |
| 2340 | dep = dwc->eps[0]; |
Felipe Balbi | a2d23f0 | 2018-04-09 12:40:48 +0300 | [diff] [blame] | 2341 | ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_INIT); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2342 | if (ret) { |
| 2343 | dev_err(dwc->dev, "failed to enable %s\n", dep->name); |
Felipe Balbi | d7be295 | 2016-05-04 15:49:37 +0300 | [diff] [blame] | 2344 | goto err0; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2345 | } |
| 2346 | |
| 2347 | dep = dwc->eps[1]; |
Felipe Balbi | a2d23f0 | 2018-04-09 12:40:48 +0300 | [diff] [blame] | 2348 | ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_INIT); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2349 | if (ret) { |
| 2350 | dev_err(dwc->dev, "failed to enable %s\n", dep->name); |
Felipe Balbi | d7be295 | 2016-05-04 15:49:37 +0300 | [diff] [blame] | 2351 | goto err1; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2352 | } |
| 2353 | |
| 2354 | /* begin to receive SETUP packets */ |
Felipe Balbi | c7fcdeb | 2011-08-27 22:28:36 +0300 | [diff] [blame] | 2355 | dwc->ep0state = EP0_SETUP_PHASE; |
Zeng Tao | 88b1bb1 | 2018-12-26 19:22:00 +0800 | [diff] [blame] | 2356 | dwc->link_state = DWC3_LINK_STATE_SS_DIS; |
Wesley Cheng | 01da7c1 | 2021-08-24 21:28:55 -0700 | [diff] [blame] | 2357 | dwc->delayed_status = false; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2358 | dwc3_ep0_out_start(dwc); |
| 2359 | |
Felipe Balbi | 8698e2a | 2013-02-08 15:24:04 +0200 | [diff] [blame] | 2360 | dwc3_gadget_enable_irq(dwc); |
| 2361 | |
Felipe Balbi | d7be295 | 2016-05-04 15:49:37 +0300 | [diff] [blame] | 2362 | return 0; |
| 2363 | |
| 2364 | err1: |
| 2365 | __dwc3_gadget_ep_disable(dwc->eps[0]); |
| 2366 | |
| 2367 | err0: |
| 2368 | return ret; |
| 2369 | } |
| 2370 | |
| 2371 | static int dwc3_gadget_start(struct usb_gadget *g, |
| 2372 | struct usb_gadget_driver *driver) |
| 2373 | { |
| 2374 | struct dwc3 *dwc = gadget_to_dwc(g); |
| 2375 | unsigned long flags; |
| 2376 | int ret = 0; |
| 2377 | int irq; |
| 2378 | |
Roger Quadros | 9522def | 2016-06-10 14:48:38 +0300 | [diff] [blame] | 2379 | irq = dwc->irq_gadget; |
Felipe Balbi | d7be295 | 2016-05-04 15:49:37 +0300 | [diff] [blame] | 2380 | ret = request_threaded_irq(irq, dwc3_interrupt, dwc3_thread_interrupt, |
| 2381 | IRQF_SHARED, "dwc3", dwc->ev_buf); |
| 2382 | if (ret) { |
| 2383 | dev_err(dwc->dev, "failed to request irq #%d --> %d\n", |
| 2384 | irq, ret); |
| 2385 | goto err0; |
| 2386 | } |
| 2387 | |
| 2388 | spin_lock_irqsave(&dwc->lock, flags); |
| 2389 | if (dwc->gadget_driver) { |
| 2390 | dev_err(dwc->dev, "%s is already bound to %s\n", |
Peter Chen | e81a701 | 2020-08-21 10:55:48 +0800 | [diff] [blame] | 2391 | dwc->gadget->name, |
Felipe Balbi | d7be295 | 2016-05-04 15:49:37 +0300 | [diff] [blame] | 2392 | dwc->gadget_driver->driver.name); |
| 2393 | ret = -EBUSY; |
| 2394 | goto err1; |
| 2395 | } |
| 2396 | |
| 2397 | dwc->gadget_driver = driver; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2398 | spin_unlock_irqrestore(&dwc->lock, flags); |
| 2399 | |
| 2400 | return 0; |
| 2401 | |
Felipe Balbi | b0d7ffd | 2013-06-27 10:00:18 +0300 | [diff] [blame] | 2402 | err1: |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2403 | spin_unlock_irqrestore(&dwc->lock, flags); |
Felipe Balbi | d7be295 | 2016-05-04 15:49:37 +0300 | [diff] [blame] | 2404 | free_irq(irq, dwc); |
Felipe Balbi | b0d7ffd | 2013-06-27 10:00:18 +0300 | [diff] [blame] | 2405 | |
| 2406 | err0: |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2407 | return ret; |
| 2408 | } |
| 2409 | |
Felipe Balbi | d7be295 | 2016-05-04 15:49:37 +0300 | [diff] [blame] | 2410 | static void __dwc3_gadget_stop(struct dwc3 *dwc) |
| 2411 | { |
| 2412 | dwc3_gadget_disable_irq(dwc); |
| 2413 | __dwc3_gadget_ep_disable(dwc->eps[0]); |
| 2414 | __dwc3_gadget_ep_disable(dwc->eps[1]); |
| 2415 | } |
| 2416 | |
Felipe Balbi | 22835b8 | 2014-10-17 12:05:12 -0500 | [diff] [blame] | 2417 | static int dwc3_gadget_stop(struct usb_gadget *g) |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2418 | { |
| 2419 | struct dwc3 *dwc = gadget_to_dwc(g); |
| 2420 | unsigned long flags; |
| 2421 | |
| 2422 | spin_lock_irqsave(&dwc->lock, flags); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2423 | dwc->gadget_driver = NULL; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2424 | spin_unlock_irqrestore(&dwc->lock, flags); |
| 2425 | |
Felipe Balbi | 3f308d1 | 2016-05-16 14:17:06 +0300 | [diff] [blame] | 2426 | free_irq(dwc->irq_gadget, dwc->ev_buf); |
Felipe Balbi | b0d7ffd | 2013-06-27 10:00:18 +0300 | [diff] [blame] | 2427 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2428 | return 0; |
| 2429 | } |
Paul Zimmerman | 802fde9 | 2012-04-27 13:10:52 +0300 | [diff] [blame] | 2430 | |
Anurag Kumar Vulisha | 729dcff | 2019-05-10 12:37:28 +0530 | [diff] [blame] | 2431 | static void dwc3_gadget_config_params(struct usb_gadget *g, |
| 2432 | struct usb_dcd_config_params *params) |
| 2433 | { |
| 2434 | struct dwc3 *dwc = gadget_to_dwc(g); |
| 2435 | |
Thinh Nguyen | 54fb5ba | 2019-08-19 18:36:06 -0700 | [diff] [blame] | 2436 | params->besl_baseline = USB_DEFAULT_BESL_UNSPECIFIED; |
| 2437 | params->besl_deep = USB_DEFAULT_BESL_UNSPECIFIED; |
| 2438 | |
| 2439 | /* Recommended BESL */ |
| 2440 | if (!dwc->dis_enblslpm_quirk) { |
Thinh Nguyen | 17b6370 | 2019-08-29 18:00:16 -0700 | [diff] [blame] | 2441 | /* |
| 2442 | * If the recommended BESL baseline is 0 or if the BESL deep is |
| 2443 | * less than 2, Microsoft's Windows 10 host usb stack will issue |
| 2444 | * a usb reset immediately after it receives the extended BOS |
| 2445 | * descriptor and the enumeration will fail. To maintain |
| 2446 | * compatibility with the Windows' usb stack, let's set the |
| 2447 | * recommended BESL baseline to 1 and clamp the BESL deep to be |
| 2448 | * within 2 to 15. |
| 2449 | */ |
| 2450 | params->besl_baseline = 1; |
Thinh Nguyen | 54fb5ba | 2019-08-19 18:36:06 -0700 | [diff] [blame] | 2451 | if (dwc->is_utmi_l1_suspend) |
Thinh Nguyen | 17b6370 | 2019-08-29 18:00:16 -0700 | [diff] [blame] | 2452 | params->besl_deep = |
| 2453 | clamp_t(u8, dwc->hird_threshold, 2, 15); |
Thinh Nguyen | 54fb5ba | 2019-08-19 18:36:06 -0700 | [diff] [blame] | 2454 | } |
| 2455 | |
Anurag Kumar Vulisha | 729dcff | 2019-05-10 12:37:28 +0530 | [diff] [blame] | 2456 | /* U1 Device exit Latency */ |
| 2457 | if (dwc->dis_u1_entry_quirk) |
| 2458 | params->bU1devExitLat = 0; |
| 2459 | else |
| 2460 | params->bU1devExitLat = DWC3_DEFAULT_U1_DEV_EXIT_LAT; |
| 2461 | |
| 2462 | /* U2 Device exit Latency */ |
| 2463 | if (dwc->dis_u2_entry_quirk) |
| 2464 | params->bU2DevExitLat = 0; |
| 2465 | else |
| 2466 | params->bU2DevExitLat = |
| 2467 | cpu_to_le16(DWC3_DEFAULT_U2_DEV_EXIT_LAT); |
| 2468 | } |
| 2469 | |
Felipe Balbi | 7d8d063 | 2017-06-06 16:05:23 +0300 | [diff] [blame] | 2470 | static void dwc3_gadget_set_speed(struct usb_gadget *g, |
| 2471 | enum usb_device_speed speed) |
| 2472 | { |
| 2473 | struct dwc3 *dwc = gadget_to_dwc(g); |
| 2474 | unsigned long flags; |
| 2475 | u32 reg; |
| 2476 | |
| 2477 | spin_lock_irqsave(&dwc->lock, flags); |
| 2478 | reg = dwc3_readl(dwc->regs, DWC3_DCFG); |
| 2479 | reg &= ~(DWC3_DCFG_SPEED_MASK); |
| 2480 | |
| 2481 | /* |
| 2482 | * WORKAROUND: DWC3 revision < 2.20a have an issue |
| 2483 | * which would cause metastability state on Run/Stop |
| 2484 | * bit if we try to force the IP to USB2-only mode. |
| 2485 | * |
| 2486 | * Because of that, we cannot configure the IP to any |
| 2487 | * speed other than the SuperSpeed |
| 2488 | * |
| 2489 | * Refers to: |
| 2490 | * |
| 2491 | * STAR#9000525659: Clock Domain Crossing on DCTL in |
| 2492 | * USB 2.0 Mode |
| 2493 | */ |
Thinh Nguyen | 9af21dd | 2020-04-11 19:20:01 -0700 | [diff] [blame] | 2494 | if (DWC3_VER_IS_PRIOR(DWC3, 220A) && |
Roger Quadros | 42bf02e | 2017-10-31 15:11:55 +0200 | [diff] [blame] | 2495 | !dwc->dis_metastability_quirk) { |
Felipe Balbi | 7d8d063 | 2017-06-06 16:05:23 +0300 | [diff] [blame] | 2496 | reg |= DWC3_DCFG_SUPERSPEED; |
| 2497 | } else { |
| 2498 | switch (speed) { |
| 2499 | case USB_SPEED_LOW: |
| 2500 | reg |= DWC3_DCFG_LOWSPEED; |
| 2501 | break; |
| 2502 | case USB_SPEED_FULL: |
| 2503 | reg |= DWC3_DCFG_FULLSPEED; |
| 2504 | break; |
| 2505 | case USB_SPEED_HIGH: |
| 2506 | reg |= DWC3_DCFG_HIGHSPEED; |
| 2507 | break; |
| 2508 | case USB_SPEED_SUPER: |
| 2509 | reg |= DWC3_DCFG_SUPERSPEED; |
| 2510 | break; |
| 2511 | case USB_SPEED_SUPER_PLUS: |
Thinh Nguyen | 9af21dd | 2020-04-11 19:20:01 -0700 | [diff] [blame] | 2512 | if (DWC3_IP_IS(DWC3)) |
Thinh Nguyen | 2f3090c | 2018-03-16 15:35:57 -0700 | [diff] [blame] | 2513 | reg |= DWC3_DCFG_SUPERSPEED; |
Thinh Nguyen | 9af21dd | 2020-04-11 19:20:01 -0700 | [diff] [blame] | 2514 | else |
| 2515 | reg |= DWC3_DCFG_SUPERSPEED_PLUS; |
Felipe Balbi | 7d8d063 | 2017-06-06 16:05:23 +0300 | [diff] [blame] | 2516 | break; |
| 2517 | default: |
| 2518 | dev_err(dwc->dev, "invalid speed (%d)\n", speed); |
| 2519 | |
Thinh Nguyen | 9af21dd | 2020-04-11 19:20:01 -0700 | [diff] [blame] | 2520 | if (DWC3_IP_IS(DWC3)) |
Felipe Balbi | 7d8d063 | 2017-06-06 16:05:23 +0300 | [diff] [blame] | 2521 | reg |= DWC3_DCFG_SUPERSPEED; |
Thinh Nguyen | 9af21dd | 2020-04-11 19:20:01 -0700 | [diff] [blame] | 2522 | else |
| 2523 | reg |= DWC3_DCFG_SUPERSPEED_PLUS; |
Felipe Balbi | 7d8d063 | 2017-06-06 16:05:23 +0300 | [diff] [blame] | 2524 | } |
| 2525 | } |
| 2526 | dwc3_writel(dwc->regs, DWC3_DCFG, reg); |
| 2527 | |
| 2528 | spin_unlock_irqrestore(&dwc->lock, flags); |
| 2529 | } |
| 2530 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2531 | static const struct usb_gadget_ops dwc3_gadget_ops = { |
| 2532 | .get_frame = dwc3_gadget_get_frame, |
| 2533 | .wakeup = dwc3_gadget_wakeup, |
| 2534 | .set_selfpowered = dwc3_gadget_set_selfpowered, |
| 2535 | .pullup = dwc3_gadget_pullup, |
| 2536 | .udc_start = dwc3_gadget_start, |
| 2537 | .udc_stop = dwc3_gadget_stop, |
Felipe Balbi | 7d8d063 | 2017-06-06 16:05:23 +0300 | [diff] [blame] | 2538 | .udc_set_speed = dwc3_gadget_set_speed, |
Anurag Kumar Vulisha | 729dcff | 2019-05-10 12:37:28 +0530 | [diff] [blame] | 2539 | .get_config_params = dwc3_gadget_config_params, |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2540 | }; |
| 2541 | |
| 2542 | /* -------------------------------------------------------------------------- */ |
| 2543 | |
Felipe Balbi | 8f1c99c | 2018-04-09 11:06:09 +0300 | [diff] [blame] | 2544 | static int dwc3_gadget_init_control_endpoint(struct dwc3_ep *dep) |
| 2545 | { |
| 2546 | struct dwc3 *dwc = dep->dwc; |
| 2547 | |
| 2548 | usb_ep_set_maxpacket_limit(&dep->endpoint, 512); |
| 2549 | dep->endpoint.maxburst = 1; |
| 2550 | dep->endpoint.ops = &dwc3_gadget_ep0_ops; |
| 2551 | if (!dep->direction) |
Peter Chen | e81a701 | 2020-08-21 10:55:48 +0800 | [diff] [blame] | 2552 | dwc->gadget->ep0 = &dep->endpoint; |
Felipe Balbi | 8f1c99c | 2018-04-09 11:06:09 +0300 | [diff] [blame] | 2553 | |
| 2554 | dep->endpoint.caps.type_control = true; |
| 2555 | |
| 2556 | return 0; |
| 2557 | } |
| 2558 | |
| 2559 | static int dwc3_gadget_init_in_endpoint(struct dwc3_ep *dep) |
| 2560 | { |
| 2561 | struct dwc3 *dwc = dep->dwc; |
| 2562 | int mdwidth; |
Felipe Balbi | 8f1c99c | 2018-04-09 11:06:09 +0300 | [diff] [blame] | 2563 | int size; |
| 2564 | |
| 2565 | mdwidth = DWC3_MDWIDTH(dwc->hwparams.hwparams0); |
Thinh Nguyen | 4244ba0 | 2020-04-11 19:20:07 -0700 | [diff] [blame] | 2566 | if (DWC3_IP_IS(DWC32)) |
| 2567 | mdwidth += DWC3_GHWPARAMS6_MDWIDTH(dwc->hwparams.hwparams6); |
| 2568 | |
Felipe Balbi | 8f1c99c | 2018-04-09 11:06:09 +0300 | [diff] [blame] | 2569 | /* MDWIDTH is represented in bits, we need it in bytes */ |
| 2570 | mdwidth /= 8; |
| 2571 | |
| 2572 | size = dwc3_readl(dwc->regs, DWC3_GTXFIFOSIZ(dep->number >> 1)); |
Thinh Nguyen | 9af21dd | 2020-04-11 19:20:01 -0700 | [diff] [blame] | 2573 | if (DWC3_IP_IS(DWC3)) |
Thinh Nguyen | 586f433 | 2020-01-31 16:59:21 -0800 | [diff] [blame] | 2574 | size = DWC3_GTXFIFOSIZ_TXFDEP(size); |
Thinh Nguyen | 9af21dd | 2020-04-11 19:20:01 -0700 | [diff] [blame] | 2575 | else |
| 2576 | size = DWC31_GTXFIFOSIZ_TXFDEP(size); |
Felipe Balbi | 8f1c99c | 2018-04-09 11:06:09 +0300 | [diff] [blame] | 2577 | |
| 2578 | /* FIFO Depth is in MDWDITH bytes. Multiply */ |
| 2579 | size *= mdwidth; |
| 2580 | |
Felipe Balbi | 8f1c99c | 2018-04-09 11:06:09 +0300 | [diff] [blame] | 2581 | /* |
Thinh Nguyen | d94ea531 | 2020-01-31 16:59:27 -0800 | [diff] [blame] | 2582 | * To meet performance requirement, a minimum TxFIFO size of 3x |
| 2583 | * MaxPacketSize is recommended for endpoints that support burst and a |
| 2584 | * minimum TxFIFO size of 2x MaxPacketSize for endpoints that don't |
| 2585 | * support burst. Use those numbers and we can calculate the max packet |
| 2586 | * limit as below. |
Felipe Balbi | 8f1c99c | 2018-04-09 11:06:09 +0300 | [diff] [blame] | 2587 | */ |
Thinh Nguyen | d94ea531 | 2020-01-31 16:59:27 -0800 | [diff] [blame] | 2588 | if (dwc->maximum_speed >= USB_SPEED_SUPER) |
| 2589 | size /= 3; |
| 2590 | else |
| 2591 | size /= 2; |
Felipe Balbi | 8f1c99c | 2018-04-09 11:06:09 +0300 | [diff] [blame] | 2592 | |
| 2593 | usb_ep_set_maxpacket_limit(&dep->endpoint, size); |
| 2594 | |
Thinh Nguyen | e0a93d9 | 2020-09-29 15:26:29 -0700 | [diff] [blame] | 2595 | dep->endpoint.max_streams = 16; |
Felipe Balbi | 8f1c99c | 2018-04-09 11:06:09 +0300 | [diff] [blame] | 2596 | dep->endpoint.ops = &dwc3_gadget_ep_ops; |
| 2597 | list_add_tail(&dep->endpoint.ep_list, |
Peter Chen | e81a701 | 2020-08-21 10:55:48 +0800 | [diff] [blame] | 2598 | &dwc->gadget->ep_list); |
Felipe Balbi | 8f1c99c | 2018-04-09 11:06:09 +0300 | [diff] [blame] | 2599 | dep->endpoint.caps.type_iso = true; |
| 2600 | dep->endpoint.caps.type_bulk = true; |
| 2601 | dep->endpoint.caps.type_int = true; |
| 2602 | |
| 2603 | return dwc3_alloc_trb_pool(dep); |
| 2604 | } |
| 2605 | |
| 2606 | static int dwc3_gadget_init_out_endpoint(struct dwc3_ep *dep) |
| 2607 | { |
| 2608 | struct dwc3 *dwc = dep->dwc; |
Thinh Nguyen | d94ea531 | 2020-01-31 16:59:27 -0800 | [diff] [blame] | 2609 | int mdwidth; |
| 2610 | int size; |
Felipe Balbi | 8f1c99c | 2018-04-09 11:06:09 +0300 | [diff] [blame] | 2611 | |
Thinh Nguyen | d94ea531 | 2020-01-31 16:59:27 -0800 | [diff] [blame] | 2612 | mdwidth = DWC3_MDWIDTH(dwc->hwparams.hwparams0); |
Thinh Nguyen | 4244ba0 | 2020-04-11 19:20:07 -0700 | [diff] [blame] | 2613 | if (DWC3_IP_IS(DWC32)) |
| 2614 | mdwidth += DWC3_GHWPARAMS6_MDWIDTH(dwc->hwparams.hwparams6); |
Thinh Nguyen | d94ea531 | 2020-01-31 16:59:27 -0800 | [diff] [blame] | 2615 | |
| 2616 | /* MDWIDTH is represented in bits, convert to bytes */ |
| 2617 | mdwidth /= 8; |
| 2618 | |
| 2619 | /* All OUT endpoints share a single RxFIFO space */ |
| 2620 | size = dwc3_readl(dwc->regs, DWC3_GRXFIFOSIZ(0)); |
Thinh Nguyen | 9af21dd | 2020-04-11 19:20:01 -0700 | [diff] [blame] | 2621 | if (DWC3_IP_IS(DWC3)) |
Thinh Nguyen | d94ea531 | 2020-01-31 16:59:27 -0800 | [diff] [blame] | 2622 | size = DWC3_GRXFIFOSIZ_RXFDEP(size); |
Thinh Nguyen | 9af21dd | 2020-04-11 19:20:01 -0700 | [diff] [blame] | 2623 | else |
| 2624 | size = DWC31_GRXFIFOSIZ_RXFDEP(size); |
Thinh Nguyen | d94ea531 | 2020-01-31 16:59:27 -0800 | [diff] [blame] | 2625 | |
| 2626 | /* FIFO depth is in MDWDITH bytes */ |
| 2627 | size *= mdwidth; |
| 2628 | |
| 2629 | /* |
| 2630 | * To meet performance requirement, a minimum recommended RxFIFO size |
| 2631 | * is defined as follow: |
| 2632 | * RxFIFO size >= (3 x MaxPacketSize) + |
| 2633 | * (3 x 8 bytes setup packets size) + (16 bytes clock crossing margin) |
| 2634 | * |
| 2635 | * Then calculate the max packet limit as below. |
| 2636 | */ |
| 2637 | size -= (3 * 8) + 16; |
| 2638 | if (size < 0) |
| 2639 | size = 0; |
| 2640 | else |
| 2641 | size /= 3; |
| 2642 | |
| 2643 | usb_ep_set_maxpacket_limit(&dep->endpoint, size); |
Thinh Nguyen | e0a93d9 | 2020-09-29 15:26:29 -0700 | [diff] [blame] | 2644 | dep->endpoint.max_streams = 16; |
Felipe Balbi | 8f1c99c | 2018-04-09 11:06:09 +0300 | [diff] [blame] | 2645 | dep->endpoint.ops = &dwc3_gadget_ep_ops; |
| 2646 | list_add_tail(&dep->endpoint.ep_list, |
Peter Chen | e81a701 | 2020-08-21 10:55:48 +0800 | [diff] [blame] | 2647 | &dwc->gadget->ep_list); |
Felipe Balbi | 8f1c99c | 2018-04-09 11:06:09 +0300 | [diff] [blame] | 2648 | dep->endpoint.caps.type_iso = true; |
| 2649 | dep->endpoint.caps.type_bulk = true; |
| 2650 | dep->endpoint.caps.type_int = true; |
| 2651 | |
| 2652 | return dwc3_alloc_trb_pool(dep); |
| 2653 | } |
| 2654 | |
| 2655 | static int dwc3_gadget_init_endpoint(struct dwc3 *dwc, u8 epnum) |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2656 | { |
| 2657 | struct dwc3_ep *dep; |
Felipe Balbi | 8f1c99c | 2018-04-09 11:06:09 +0300 | [diff] [blame] | 2658 | bool direction = epnum & 1; |
| 2659 | int ret; |
| 2660 | u8 num = epnum >> 1; |
| 2661 | |
| 2662 | dep = kzalloc(sizeof(*dep), GFP_KERNEL); |
| 2663 | if (!dep) |
| 2664 | return -ENOMEM; |
| 2665 | |
| 2666 | dep->dwc = dwc; |
| 2667 | dep->number = epnum; |
| 2668 | dep->direction = direction; |
| 2669 | dep->regs = dwc->regs + DWC3_DEP_BASE(epnum); |
| 2670 | dwc->eps[epnum] = dep; |
Thinh Nguyen | d92021f | 2018-11-14 22:56:54 -0800 | [diff] [blame] | 2671 | dep->combo_num = 0; |
| 2672 | dep->start_cmd_status = 0; |
Felipe Balbi | 8f1c99c | 2018-04-09 11:06:09 +0300 | [diff] [blame] | 2673 | |
| 2674 | snprintf(dep->name, sizeof(dep->name), "ep%u%s", num, |
| 2675 | direction ? "in" : "out"); |
| 2676 | |
| 2677 | dep->endpoint.name = dep->name; |
| 2678 | |
| 2679 | if (!(dep->number > 1)) { |
| 2680 | dep->endpoint.desc = &dwc3_gadget_ep0_desc; |
| 2681 | dep->endpoint.comp_desc = NULL; |
| 2682 | } |
| 2683 | |
Felipe Balbi | 8f1c99c | 2018-04-09 11:06:09 +0300 | [diff] [blame] | 2684 | if (num == 0) |
| 2685 | ret = dwc3_gadget_init_control_endpoint(dep); |
| 2686 | else if (direction) |
| 2687 | ret = dwc3_gadget_init_in_endpoint(dep); |
| 2688 | else |
| 2689 | ret = dwc3_gadget_init_out_endpoint(dep); |
| 2690 | |
| 2691 | if (ret) |
| 2692 | return ret; |
| 2693 | |
| 2694 | dep->endpoint.caps.dir_in = direction; |
| 2695 | dep->endpoint.caps.dir_out = !direction; |
| 2696 | |
| 2697 | INIT_LIST_HEAD(&dep->pending_list); |
| 2698 | INIT_LIST_HEAD(&dep->started_list); |
Felipe Balbi | d5443bb | 2018-08-01 13:53:29 +0300 | [diff] [blame] | 2699 | INIT_LIST_HEAD(&dep->cancelled_list); |
Felipe Balbi | 8f1c99c | 2018-04-09 11:06:09 +0300 | [diff] [blame] | 2700 | |
Jack Pham | e52d43c | 2021-05-29 12:29:32 -0700 | [diff] [blame] | 2701 | dwc3_debugfs_create_endpoint_dir(dep); |
| 2702 | |
Felipe Balbi | 8f1c99c | 2018-04-09 11:06:09 +0300 | [diff] [blame] | 2703 | return 0; |
| 2704 | } |
| 2705 | |
| 2706 | static int dwc3_gadget_init_endpoints(struct dwc3 *dwc, u8 total) |
| 2707 | { |
Bryan O'Donoghue | 47d3946 | 2017-01-31 20:58:10 +0000 | [diff] [blame] | 2708 | u8 epnum; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2709 | |
Peter Chen | e81a701 | 2020-08-21 10:55:48 +0800 | [diff] [blame] | 2710 | INIT_LIST_HEAD(&dwc->gadget->ep_list); |
Bryan O'Donoghue | f3bcfc7 | 2017-01-31 20:58:11 +0000 | [diff] [blame] | 2711 | |
Andy Shevchenko | 46b780d | 2017-06-12 15:11:25 +0300 | [diff] [blame] | 2712 | for (epnum = 0; epnum < total; epnum++) { |
Felipe Balbi | 8f1c99c | 2018-04-09 11:06:09 +0300 | [diff] [blame] | 2713 | int ret; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2714 | |
Felipe Balbi | 8f1c99c | 2018-04-09 11:06:09 +0300 | [diff] [blame] | 2715 | ret = dwc3_gadget_init_endpoint(dwc, epnum); |
| 2716 | if (ret) |
| 2717 | return ret; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2718 | } |
| 2719 | |
| 2720 | return 0; |
| 2721 | } |
| 2722 | |
| 2723 | static void dwc3_gadget_free_endpoints(struct dwc3 *dwc) |
| 2724 | { |
| 2725 | struct dwc3_ep *dep; |
| 2726 | u8 epnum; |
| 2727 | |
| 2728 | for (epnum = 0; epnum < DWC3_ENDPOINTS_NUM; epnum++) { |
| 2729 | dep = dwc->eps[epnum]; |
Felipe Balbi | 6a1e3ef | 2011-05-05 16:21:59 +0300 | [diff] [blame] | 2730 | if (!dep) |
| 2731 | continue; |
George Cherian | 5bf8fae | 2013-05-27 14:35:49 +0530 | [diff] [blame] | 2732 | /* |
| 2733 | * Physical endpoints 0 and 1 are special; they form the |
| 2734 | * bi-directional USB endpoint 0. |
| 2735 | * |
| 2736 | * For those two physical endpoints, we don't allocate a TRB |
| 2737 | * pool nor do we add them the endpoints list. Due to that, we |
| 2738 | * shouldn't do these two operations otherwise we would end up |
| 2739 | * with all sorts of bugs when removing dwc3.ko. |
| 2740 | */ |
| 2741 | if (epnum != 0 && epnum != 1) { |
| 2742 | dwc3_free_trb_pool(dep); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2743 | list_del(&dep->endpoint.ep_list); |
George Cherian | 5bf8fae | 2013-05-27 14:35:49 +0530 | [diff] [blame] | 2744 | } |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2745 | |
Jack Pham | e52d43c | 2021-05-29 12:29:32 -0700 | [diff] [blame] | 2746 | debugfs_remove_recursive(debugfs_lookup(dep->name, dwc->root)); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2747 | kfree(dep); |
| 2748 | } |
| 2749 | } |
| 2750 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2751 | /* -------------------------------------------------------------------------- */ |
Felipe Balbi | e5caff6 | 2013-02-26 15:11:05 +0200 | [diff] [blame] | 2752 | |
Felipe Balbi | 8f608e8 | 2018-03-27 10:53:29 +0300 | [diff] [blame] | 2753 | static int dwc3_gadget_ep_reclaim_completed_trb(struct dwc3_ep *dep, |
| 2754 | struct dwc3_request *req, struct dwc3_trb *trb, |
| 2755 | const struct dwc3_event_depevt *event, int status, int chain) |
Pratyush Anand | e5ba5ec | 2013-01-14 15:59:37 +0530 | [diff] [blame] | 2756 | { |
| 2757 | unsigned int count; |
Pratyush Anand | e5ba5ec | 2013-01-14 15:59:37 +0530 | [diff] [blame] | 2758 | |
Felipe Balbi | dc55c67 | 2016-08-12 13:20:32 +0300 | [diff] [blame] | 2759 | dwc3_ep_inc_deq(dep); |
Felipe Balbi | a9c3ca5 | 2016-10-05 14:24:37 +0300 | [diff] [blame] | 2760 | |
Felipe Balbi | 2c4cbe6e5 | 2014-04-30 17:45:10 -0500 | [diff] [blame] | 2761 | trace_dwc3_complete_trb(dep, trb); |
Felipe Balbi | 09fe1f8 | 2018-08-01 13:32:07 +0300 | [diff] [blame] | 2762 | req->num_trbs--; |
Felipe Balbi | 2c4cbe6e5 | 2014-04-30 17:45:10 -0500 | [diff] [blame] | 2763 | |
Felipe Balbi | e5b36ae | 2016-08-10 11:13:26 +0300 | [diff] [blame] | 2764 | /* |
| 2765 | * If we're in the middle of series of chained TRBs and we |
| 2766 | * receive a short transfer along the way, DWC3 will skip |
| 2767 | * through all TRBs including the last TRB in the chain (the |
| 2768 | * where CHN bit is zero. DWC3 will also avoid clearing HWO |
| 2769 | * bit and SW has to do it manually. |
| 2770 | * |
| 2771 | * We're going to do that here to avoid problems of HW trying |
| 2772 | * to use bogus TRBs for transfers. |
| 2773 | */ |
| 2774 | if (chain && (trb->ctrl & DWC3_TRB_CTRL_HWO)) |
| 2775 | trb->ctrl &= ~DWC3_TRB_CTRL_HWO; |
| 2776 | |
Felipe Balbi | c6267a5 | 2017-01-05 14:58:46 +0200 | [diff] [blame] | 2777 | /* |
Thinh Nguyen | 6abfa0f | 2018-11-15 19:03:27 -0800 | [diff] [blame] | 2778 | * For isochronous transfers, the first TRB in a service interval must |
| 2779 | * have the Isoc-First type. Track and report its interval frame number. |
| 2780 | */ |
| 2781 | if (usb_endpoint_xfer_isoc(dep->endpoint.desc) && |
| 2782 | (trb->ctrl & DWC3_TRBCTL_ISOCHRONOUS_FIRST)) { |
| 2783 | unsigned int frame_number; |
| 2784 | |
| 2785 | frame_number = DWC3_TRB_CTRL_GET_SID_SOFN(trb->ctrl); |
| 2786 | frame_number &= ~(dep->interval - 1); |
| 2787 | req->request.frame_number = frame_number; |
| 2788 | } |
| 2789 | |
| 2790 | /* |
Thinh Nguyen | a2841f4 | 2020-09-24 01:21:36 -0700 | [diff] [blame] | 2791 | * We use bounce buffer for requests that needs extra TRB or OUT ZLP. If |
| 2792 | * this TRB points to the bounce buffer address, it's a MPS alignment |
| 2793 | * TRB. Don't add it to req->remaining calculation. |
Felipe Balbi | c6267a5 | 2017-01-05 14:58:46 +0200 | [diff] [blame] | 2794 | */ |
Thinh Nguyen | a2841f4 | 2020-09-24 01:21:36 -0700 | [diff] [blame] | 2795 | if (trb->bpl == lower_32_bits(dep->dwc->bounce_addr) && |
| 2796 | trb->bph == upper_32_bits(dep->dwc->bounce_addr)) { |
Felipe Balbi | c6267a5 | 2017-01-05 14:58:46 +0200 | [diff] [blame] | 2797 | trb->ctrl &= ~DWC3_TRB_CTRL_HWO; |
| 2798 | return 1; |
| 2799 | } |
| 2800 | |
Pratyush Anand | e5ba5ec | 2013-01-14 15:59:37 +0530 | [diff] [blame] | 2801 | count = trb->size & DWC3_TRB_SIZE_MASK; |
Felipe Balbi | e62c5bc5 | 2016-10-25 13:47:21 +0300 | [diff] [blame] | 2802 | req->remaining += count; |
Pratyush Anand | e5ba5ec | 2013-01-14 15:59:37 +0530 | [diff] [blame] | 2803 | |
Felipe Balbi | 35b2719 | 2017-03-08 13:56:37 +0200 | [diff] [blame] | 2804 | if ((trb->ctrl & DWC3_TRB_CTRL_HWO) && status != -ESHUTDOWN) |
| 2805 | return 1; |
| 2806 | |
Felipe Balbi | d80fe1b | 2018-04-06 11:04:21 +0300 | [diff] [blame] | 2807 | if (event->status & DEPEVT_STATUS_SHORT && !chain) |
Pratyush Anand | e5ba5ec | 2013-01-14 15:59:37 +0530 | [diff] [blame] | 2808 | return 1; |
Felipe Balbi | f99f53f | 2016-08-12 13:19:20 +0300 | [diff] [blame] | 2809 | |
Anurag Kumar Vulisha | 5ee8589 | 2020-01-27 19:30:46 +0000 | [diff] [blame] | 2810 | if ((trb->ctrl & DWC3_TRB_CTRL_IOC) || |
| 2811 | (trb->ctrl & DWC3_TRB_CTRL_LST)) |
Pratyush Anand | e5ba5ec | 2013-01-14 15:59:37 +0530 | [diff] [blame] | 2812 | return 1; |
Felipe Balbi | f99f53f | 2016-08-12 13:19:20 +0300 | [diff] [blame] | 2813 | |
Pratyush Anand | e5ba5ec | 2013-01-14 15:59:37 +0530 | [diff] [blame] | 2814 | return 0; |
| 2815 | } |
| 2816 | |
Felipe Balbi | d369295 | 2018-03-29 13:32:10 +0300 | [diff] [blame] | 2817 | static int dwc3_gadget_ep_reclaim_trb_sg(struct dwc3_ep *dep, |
| 2818 | struct dwc3_request *req, const struct dwc3_event_depevt *event, |
| 2819 | int status) |
| 2820 | { |
| 2821 | struct dwc3_trb *trb = &dep->trb_pool[dep->trb_dequeue]; |
| 2822 | struct scatterlist *sg = req->sg; |
| 2823 | struct scatterlist *s; |
Thinh Nguyen | adccf17 | 2021-05-12 20:17:09 -0700 | [diff] [blame] | 2824 | unsigned int num_queued = req->num_queued_sgs; |
Felipe Balbi | d369295 | 2018-03-29 13:32:10 +0300 | [diff] [blame] | 2825 | unsigned int i; |
| 2826 | int ret = 0; |
| 2827 | |
Thinh Nguyen | adccf17 | 2021-05-12 20:17:09 -0700 | [diff] [blame] | 2828 | for_each_sg(sg, s, num_queued, i) { |
Felipe Balbi | d369295 | 2018-03-29 13:32:10 +0300 | [diff] [blame] | 2829 | trb = &dep->trb_pool[dep->trb_dequeue]; |
| 2830 | |
Felipe Balbi | d369295 | 2018-03-29 13:32:10 +0300 | [diff] [blame] | 2831 | req->sg = sg_next(s); |
Thinh Nguyen | adccf17 | 2021-05-12 20:17:09 -0700 | [diff] [blame] | 2832 | req->num_queued_sgs--; |
Felipe Balbi | d369295 | 2018-03-29 13:32:10 +0300 | [diff] [blame] | 2833 | |
| 2834 | ret = dwc3_gadget_ep_reclaim_completed_trb(dep, req, |
| 2835 | trb, event, status, true); |
| 2836 | if (ret) |
| 2837 | break; |
| 2838 | } |
| 2839 | |
| 2840 | return ret; |
| 2841 | } |
| 2842 | |
| 2843 | static int dwc3_gadget_ep_reclaim_trb_linear(struct dwc3_ep *dep, |
| 2844 | struct dwc3_request *req, const struct dwc3_event_depevt *event, |
| 2845 | int status) |
| 2846 | { |
| 2847 | struct dwc3_trb *trb = &dep->trb_pool[dep->trb_dequeue]; |
| 2848 | |
| 2849 | return dwc3_gadget_ep_reclaim_completed_trb(dep, req, trb, |
| 2850 | event, status, false); |
| 2851 | } |
| 2852 | |
Felipe Balbi | e0c42ce | 2018-04-06 15:37:30 +0300 | [diff] [blame] | 2853 | static bool dwc3_gadget_ep_request_completed(struct dwc3_request *req) |
| 2854 | { |
Thinh Nguyen | adccf17 | 2021-05-12 20:17:09 -0700 | [diff] [blame] | 2855 | return req->num_pending_sgs == 0 && req->num_queued_sgs == 0; |
Felipe Balbi | e0c42ce | 2018-04-06 15:37:30 +0300 | [diff] [blame] | 2856 | } |
| 2857 | |
Felipe Balbi | f38e35d | 2018-04-06 15:56:35 +0300 | [diff] [blame] | 2858 | static int dwc3_gadget_ep_cleanup_completed_request(struct dwc3_ep *dep, |
| 2859 | const struct dwc3_event_depevt *event, |
| 2860 | struct dwc3_request *req, int status) |
| 2861 | { |
| 2862 | int ret; |
| 2863 | |
Thinh Nguyen | adccf17 | 2021-05-12 20:17:09 -0700 | [diff] [blame] | 2864 | if (req->request.num_mapped_sgs) |
Felipe Balbi | f38e35d | 2018-04-06 15:56:35 +0300 | [diff] [blame] | 2865 | ret = dwc3_gadget_ep_reclaim_trb_sg(dep, req, event, |
| 2866 | status); |
| 2867 | else |
| 2868 | ret = dwc3_gadget_ep_reclaim_trb_linear(dep, req, event, |
| 2869 | status); |
| 2870 | |
Thinh Nguyen | 690e5c2 | 2020-09-24 01:21:24 -0700 | [diff] [blame] | 2871 | req->request.actual = req->request.length - req->remaining; |
| 2872 | |
| 2873 | if (!dwc3_gadget_ep_request_completed(req)) |
| 2874 | goto out; |
| 2875 | |
Felipe Balbi | 1a22ec6 | 2018-08-01 13:15:05 +0300 | [diff] [blame] | 2876 | if (req->needs_extra_trb) { |
Felipe Balbi | f38e35d | 2018-04-06 15:56:35 +0300 | [diff] [blame] | 2877 | ret = dwc3_gadget_ep_reclaim_trb_linear(dep, req, event, |
| 2878 | status); |
Felipe Balbi | 1a22ec6 | 2018-08-01 13:15:05 +0300 | [diff] [blame] | 2879 | req->needs_extra_trb = false; |
Felipe Balbi | f38e35d | 2018-04-06 15:56:35 +0300 | [diff] [blame] | 2880 | } |
| 2881 | |
Felipe Balbi | f38e35d | 2018-04-06 15:56:35 +0300 | [diff] [blame] | 2882 | dwc3_gadget_giveback(dep, req, status); |
| 2883 | |
| 2884 | out: |
| 2885 | return ret; |
| 2886 | } |
| 2887 | |
Felipe Balbi | 12a3a4a | 2018-03-29 11:53:40 +0300 | [diff] [blame] | 2888 | static void dwc3_gadget_ep_cleanup_completed_requests(struct dwc3_ep *dep, |
Felipe Balbi | 8f608e8 | 2018-03-27 10:53:29 +0300 | [diff] [blame] | 2889 | const struct dwc3_event_depevt *event, int status) |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2890 | { |
Felipe Balbi | 6afbdb5 | 2018-04-06 15:49:49 +0300 | [diff] [blame] | 2891 | struct dwc3_request *req; |
| 2892 | struct dwc3_request *tmp; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2893 | |
Felipe Balbi | 6afbdb5 | 2018-04-06 15:49:49 +0300 | [diff] [blame] | 2894 | list_for_each_entry_safe(req, tmp, &dep->started_list, list) { |
Felipe Balbi | fee73e6 | 2018-04-06 15:50:29 +0300 | [diff] [blame] | 2895 | int ret; |
Felipe Balbi | e5b36ae | 2016-08-10 11:13:26 +0300 | [diff] [blame] | 2896 | |
Felipe Balbi | f38e35d | 2018-04-06 15:56:35 +0300 | [diff] [blame] | 2897 | ret = dwc3_gadget_ep_cleanup_completed_request(dep, event, |
| 2898 | req, status); |
Felipe Balbi | 58f0218 | 2018-03-29 12:10:31 +0300 | [diff] [blame] | 2899 | if (ret) |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2900 | break; |
Felipe Balbi | 31162af | 2016-08-11 14:38:37 +0300 | [diff] [blame] | 2901 | } |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2902 | } |
| 2903 | |
Thinh Nguyen | d9feef9 | 2020-03-31 01:40:42 -0700 | [diff] [blame] | 2904 | static bool dwc3_gadget_ep_should_continue(struct dwc3_ep *dep) |
| 2905 | { |
| 2906 | struct dwc3_request *req; |
| 2907 | |
| 2908 | if (!list_empty(&dep->pending_list)) |
| 2909 | return true; |
| 2910 | |
| 2911 | /* |
| 2912 | * We only need to check the first entry of the started list. We can |
| 2913 | * assume the completed requests are removed from the started list. |
| 2914 | */ |
| 2915 | req = next_request(&dep->started_list); |
| 2916 | if (!req) |
| 2917 | return false; |
| 2918 | |
| 2919 | return !dwc3_gadget_ep_request_completed(req); |
| 2920 | } |
| 2921 | |
Felipe Balbi | ee3638b | 2018-03-27 11:26:53 +0300 | [diff] [blame] | 2922 | static void dwc3_gadget_endpoint_frame_from_event(struct dwc3_ep *dep, |
| 2923 | const struct dwc3_event_depevt *event) |
| 2924 | { |
Felipe Balbi | f62afb4 | 2018-04-11 10:34:34 +0300 | [diff] [blame] | 2925 | dep->frame_number = event->parameters; |
Felipe Balbi | ee3638b | 2018-03-27 11:26:53 +0300 | [diff] [blame] | 2926 | } |
| 2927 | |
Thinh Nguyen | 2e6e9e4 | 2020-05-05 19:46:39 -0700 | [diff] [blame] | 2928 | static bool dwc3_gadget_endpoint_trbs_complete(struct dwc3_ep *dep, |
| 2929 | const struct dwc3_event_depevt *event, int status) |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2930 | { |
Felipe Balbi | 8f608e8 | 2018-03-27 10:53:29 +0300 | [diff] [blame] | 2931 | struct dwc3 *dwc = dep->dwc; |
Thinh Nguyen | 2e6e9e4 | 2020-05-05 19:46:39 -0700 | [diff] [blame] | 2932 | bool no_started_trb = true; |
Felipe Balbi | 6d8a019 | 2018-03-29 12:49:28 +0300 | [diff] [blame] | 2933 | |
Albert Wang | 0755f3f | 2021-11-09 17:26:42 +0800 | [diff] [blame] | 2934 | if (!dep->endpoint.desc) |
| 2935 | return no_started_trb; |
| 2936 | |
Felipe Balbi | 5f2e797 | 2018-03-29 11:10:45 +0300 | [diff] [blame] | 2937 | dwc3_gadget_ep_cleanup_completed_requests(dep, event, status); |
Felipe Balbi | fae2b90 | 2011-10-14 13:00:30 +0300 | [diff] [blame] | 2938 | |
Thinh Nguyen | b6842d4 | 2020-05-05 19:46:33 -0700 | [diff] [blame] | 2939 | if (dep->flags & DWC3_EP_END_TRANSFER_PENDING) |
| 2940 | goto out; |
Felipe Balbi | 6d8a019 | 2018-03-29 12:49:28 +0300 | [diff] [blame] | 2941 | |
Michael Grzeschik | f5e46aa | 2020-07-01 20:24:53 +0200 | [diff] [blame] | 2942 | if (usb_endpoint_xfer_isoc(dep->endpoint.desc) && |
| 2943 | list_empty(&dep->started_list) && |
| 2944 | (list_empty(&dep->pending_list) || status == -EXDEV)) |
Felipe Balbi | fae2b90 | 2011-10-14 13:00:30 +0300 | [diff] [blame] | 2945 | dwc3_stop_active_transfer(dep, true, true); |
Thinh Nguyen | d9feef9 | 2020-03-31 01:40:42 -0700 | [diff] [blame] | 2946 | else if (dwc3_gadget_ep_should_continue(dep)) |
Thinh Nguyen | 2e6e9e4 | 2020-05-05 19:46:39 -0700 | [diff] [blame] | 2947 | if (__dwc3_gadget_kick_transfer(dep) == 0) |
| 2948 | no_started_trb = false; |
Felipe Balbi | fae2b90 | 2011-10-14 13:00:30 +0300 | [diff] [blame] | 2949 | |
Thinh Nguyen | b6842d4 | 2020-05-05 19:46:33 -0700 | [diff] [blame] | 2950 | out: |
Felipe Balbi | fae2b90 | 2011-10-14 13:00:30 +0300 | [diff] [blame] | 2951 | /* |
| 2952 | * WORKAROUND: This is the 2nd half of U1/U2 -> U0 workaround. |
| 2953 | * See dwc3_gadget_linksts_change_interrupt() for 1st half. |
| 2954 | */ |
Thinh Nguyen | 9af21dd | 2020-04-11 19:20:01 -0700 | [diff] [blame] | 2955 | if (DWC3_VER_IS_PRIOR(DWC3, 183A)) { |
Felipe Balbi | fae2b90 | 2011-10-14 13:00:30 +0300 | [diff] [blame] | 2956 | u32 reg; |
| 2957 | int i; |
| 2958 | |
| 2959 | for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) { |
Moiz Sonasath | 348e026 | 2012-08-01 14:08:30 -0500 | [diff] [blame] | 2960 | dep = dwc->eps[i]; |
Felipe Balbi | fae2b90 | 2011-10-14 13:00:30 +0300 | [diff] [blame] | 2961 | |
| 2962 | if (!(dep->flags & DWC3_EP_ENABLED)) |
| 2963 | continue; |
| 2964 | |
Felipe Balbi | aa3342c | 2016-03-14 11:01:31 +0200 | [diff] [blame] | 2965 | if (!list_empty(&dep->started_list)) |
Thinh Nguyen | 2e6e9e4 | 2020-05-05 19:46:39 -0700 | [diff] [blame] | 2966 | return no_started_trb; |
Felipe Balbi | fae2b90 | 2011-10-14 13:00:30 +0300 | [diff] [blame] | 2967 | } |
| 2968 | |
| 2969 | reg = dwc3_readl(dwc->regs, DWC3_DCTL); |
| 2970 | reg |= dwc->u1u2; |
| 2971 | dwc3_writel(dwc->regs, DWC3_DCTL, reg); |
| 2972 | |
| 2973 | dwc->u1u2 = 0; |
| 2974 | } |
Thinh Nguyen | 2e6e9e4 | 2020-05-05 19:46:39 -0700 | [diff] [blame] | 2975 | |
| 2976 | return no_started_trb; |
| 2977 | } |
| 2978 | |
| 2979 | static void dwc3_gadget_endpoint_transfer_in_progress(struct dwc3_ep *dep, |
| 2980 | const struct dwc3_event_depevt *event) |
| 2981 | { |
| 2982 | int status = 0; |
| 2983 | |
Albert Wang | 0755f3f | 2021-11-09 17:26:42 +0800 | [diff] [blame] | 2984 | if (!dep->endpoint.desc) |
| 2985 | return; |
| 2986 | |
Thinh Nguyen | 2e6e9e4 | 2020-05-05 19:46:39 -0700 | [diff] [blame] | 2987 | if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) |
| 2988 | dwc3_gadget_endpoint_frame_from_event(dep, event); |
| 2989 | |
| 2990 | if (event->status & DEPEVT_STATUS_BUSERR) |
| 2991 | status = -ECONNRESET; |
| 2992 | |
| 2993 | if (event->status & DEPEVT_STATUS_MISSED_ISOC) |
| 2994 | status = -EXDEV; |
| 2995 | |
| 2996 | dwc3_gadget_endpoint_trbs_complete(dep, event, status); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2997 | } |
| 2998 | |
Thinh Nguyen | 3eaecd0 | 2020-05-05 19:46:51 -0700 | [diff] [blame] | 2999 | static void dwc3_gadget_endpoint_transfer_complete(struct dwc3_ep *dep, |
| 3000 | const struct dwc3_event_depevt *event) |
| 3001 | { |
| 3002 | int status = 0; |
| 3003 | |
| 3004 | dep->flags &= ~DWC3_EP_TRANSFER_STARTED; |
| 3005 | |
| 3006 | if (event->status & DEPEVT_STATUS_BUSERR) |
| 3007 | status = -ECONNRESET; |
| 3008 | |
Thinh Nguyen | e0d1956 | 2020-05-05 19:46:57 -0700 | [diff] [blame] | 3009 | if (dwc3_gadget_endpoint_trbs_complete(dep, event, status)) |
| 3010 | dep->flags &= ~DWC3_EP_WAIT_TRANSFER_COMPLETE; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3011 | } |
| 3012 | |
Felipe Balbi | 8f608e8 | 2018-03-27 10:53:29 +0300 | [diff] [blame] | 3013 | static void dwc3_gadget_endpoint_transfer_not_ready(struct dwc3_ep *dep, |
| 3014 | const struct dwc3_event_depevt *event) |
Felipe Balbi | 3203386 | 2018-03-27 10:47:48 +0300 | [diff] [blame] | 3015 | { |
Felipe Balbi | ee3638b | 2018-03-27 11:26:53 +0300 | [diff] [blame] | 3016 | dwc3_gadget_endpoint_frame_from_event(dep, event); |
Thinh Nguyen | 36f05d3 | 2020-03-29 16:13:10 -0700 | [diff] [blame] | 3017 | |
| 3018 | /* |
| 3019 | * The XferNotReady event is generated only once before the endpoint |
| 3020 | * starts. It will be generated again when END_TRANSFER command is |
| 3021 | * issued. For some controller versions, the XferNotReady event may be |
| 3022 | * generated while the END_TRANSFER command is still in process. Ignore |
| 3023 | * it and wait for the next XferNotReady event after the command is |
| 3024 | * completed. |
| 3025 | */ |
| 3026 | if (dep->flags & DWC3_EP_END_TRANSFER_PENDING) |
| 3027 | return; |
| 3028 | |
Felipe Balbi | 25abad6 | 2018-08-14 10:41:19 +0300 | [diff] [blame] | 3029 | (void) __dwc3_gadget_start_isoc(dep); |
Felipe Balbi | 3203386 | 2018-03-27 10:47:48 +0300 | [diff] [blame] | 3030 | } |
| 3031 | |
Thinh Nguyen | 8266b08 | 2020-07-30 16:29:03 -0700 | [diff] [blame] | 3032 | static void dwc3_gadget_endpoint_command_complete(struct dwc3_ep *dep, |
| 3033 | const struct dwc3_event_depevt *event) |
| 3034 | { |
| 3035 | u8 cmd = DEPEVT_PARAMETER_CMD(event->parameters); |
| 3036 | |
| 3037 | if (cmd != DWC3_DEPCMD_ENDTRANSFER) |
| 3038 | return; |
| 3039 | |
Thinh Nguyen | 3abf746 | 2021-10-25 16:21:10 -0700 | [diff] [blame] | 3040 | /* |
| 3041 | * The END_TRANSFER command will cause the controller to generate a |
| 3042 | * NoStream Event, and it's not due to the host DP NoStream rejection. |
| 3043 | * Ignore the next NoStream event. |
| 3044 | */ |
| 3045 | if (dep->stream_capable) |
| 3046 | dep->flags |= DWC3_EP_IGNORE_NEXT_NOSTREAM; |
| 3047 | |
Thinh Nguyen | 8266b08 | 2020-07-30 16:29:03 -0700 | [diff] [blame] | 3048 | dep->flags &= ~DWC3_EP_END_TRANSFER_PENDING; |
| 3049 | dep->flags &= ~DWC3_EP_TRANSFER_STARTED; |
| 3050 | dwc3_gadget_ep_cleanup_cancelled_requests(dep); |
| 3051 | |
| 3052 | if (dep->flags & DWC3_EP_PENDING_CLEAR_STALL) { |
| 3053 | struct dwc3 *dwc = dep->dwc; |
| 3054 | |
| 3055 | dep->flags &= ~DWC3_EP_PENDING_CLEAR_STALL; |
| 3056 | if (dwc3_send_clear_stall_ep_cmd(dep)) { |
| 3057 | struct usb_ep *ep0 = &dwc->eps[0]->endpoint; |
| 3058 | |
| 3059 | dev_err(dwc->dev, "failed to clear STALL on %s\n", dep->name); |
| 3060 | if (dwc->delayed_status) |
| 3061 | __dwc3_gadget_ep0_set_halt(ep0, 1); |
| 3062 | return; |
| 3063 | } |
| 3064 | |
| 3065 | dep->flags &= ~(DWC3_EP_STALL | DWC3_EP_WEDGE); |
| 3066 | if (dwc->delayed_status) |
| 3067 | dwc3_ep0_send_delayed_status(dwc); |
| 3068 | } |
| 3069 | |
| 3070 | if ((dep->flags & DWC3_EP_DELAY_START) && |
| 3071 | !usb_endpoint_xfer_isoc(dep->endpoint.desc)) |
| 3072 | __dwc3_gadget_kick_transfer(dep); |
| 3073 | |
| 3074 | dep->flags &= ~DWC3_EP_DELAY_START; |
| 3075 | } |
| 3076 | |
Thinh Nguyen | 140ca4c | 2020-05-05 19:47:09 -0700 | [diff] [blame] | 3077 | static void dwc3_gadget_endpoint_stream_event(struct dwc3_ep *dep, |
| 3078 | const struct dwc3_event_depevt *event) |
| 3079 | { |
| 3080 | struct dwc3 *dwc = dep->dwc; |
| 3081 | |
| 3082 | if (event->status == DEPEVT_STREAMEVT_FOUND) { |
| 3083 | dep->flags |= DWC3_EP_FIRST_STREAM_PRIMED; |
| 3084 | goto out; |
| 3085 | } |
| 3086 | |
| 3087 | /* Note: NoStream rejection event param value is 0 and not 0xFFFF */ |
| 3088 | switch (event->parameters) { |
| 3089 | case DEPEVT_STREAM_PRIME: |
| 3090 | /* |
| 3091 | * If the host can properly transition the endpoint state from |
| 3092 | * idle to prime after a NoStream rejection, there's no need to |
| 3093 | * force restarting the endpoint to reinitiate the stream. To |
| 3094 | * simplify the check, assume the host follows the USB spec if |
| 3095 | * it primed the endpoint more than once. |
| 3096 | */ |
| 3097 | if (dep->flags & DWC3_EP_FORCE_RESTART_STREAM) { |
| 3098 | if (dep->flags & DWC3_EP_FIRST_STREAM_PRIMED) |
| 3099 | dep->flags &= ~DWC3_EP_FORCE_RESTART_STREAM; |
| 3100 | else |
| 3101 | dep->flags |= DWC3_EP_FIRST_STREAM_PRIMED; |
| 3102 | } |
| 3103 | |
| 3104 | break; |
| 3105 | case DEPEVT_STREAM_NOSTREAM: |
| 3106 | if ((dep->flags & DWC3_EP_IGNORE_NEXT_NOSTREAM) || |
| 3107 | !(dep->flags & DWC3_EP_FORCE_RESTART_STREAM) || |
| 3108 | !(dep->flags & DWC3_EP_WAIT_TRANSFER_COMPLETE)) |
| 3109 | break; |
| 3110 | |
| 3111 | /* |
| 3112 | * If the host rejects a stream due to no active stream, by the |
| 3113 | * USB and xHCI spec, the endpoint will be put back to idle |
| 3114 | * state. When the host is ready (buffer added/updated), it will |
| 3115 | * prime the endpoint to inform the usb device controller. This |
| 3116 | * triggers the device controller to issue ERDY to restart the |
| 3117 | * stream. However, some hosts don't follow this and keep the |
| 3118 | * endpoint in the idle state. No prime will come despite host |
| 3119 | * streams are updated, and the device controller will not be |
| 3120 | * triggered to generate ERDY to move the next stream data. To |
| 3121 | * workaround this and maintain compatibility with various |
| 3122 | * hosts, force to reinitate the stream until the host is ready |
| 3123 | * instead of waiting for the host to prime the endpoint. |
| 3124 | */ |
Thinh Nguyen | b10e1c2 | 2020-05-05 19:47:15 -0700 | [diff] [blame] | 3125 | if (DWC3_VER_IS_WITHIN(DWC32, 100A, ANY)) { |
| 3126 | unsigned int cmd = DWC3_DGCMD_SET_ENDPOINT_PRIME; |
| 3127 | |
| 3128 | dwc3_send_gadget_generic_command(dwc, cmd, dep->number); |
| 3129 | } else { |
| 3130 | dep->flags |= DWC3_EP_DELAY_START; |
| 3131 | dwc3_stop_active_transfer(dep, true, true); |
| 3132 | return; |
| 3133 | } |
| 3134 | break; |
Thinh Nguyen | 140ca4c | 2020-05-05 19:47:09 -0700 | [diff] [blame] | 3135 | } |
| 3136 | |
| 3137 | out: |
| 3138 | dep->flags &= ~DWC3_EP_IGNORE_NEXT_NOSTREAM; |
| 3139 | } |
| 3140 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3141 | static void dwc3_endpoint_interrupt(struct dwc3 *dwc, |
| 3142 | const struct dwc3_event_depevt *event) |
| 3143 | { |
| 3144 | struct dwc3_ep *dep; |
| 3145 | u8 epnum = event->endpoint_number; |
| 3146 | |
| 3147 | dep = dwc->eps[epnum]; |
| 3148 | |
Janusz Dziedzic | d7fd41c | 2016-12-08 10:57:34 +0100 | [diff] [blame] | 3149 | if (!(dep->flags & DWC3_EP_ENABLED)) { |
Felipe Balbi | 3aec991 | 2019-01-21 13:08:44 +0200 | [diff] [blame] | 3150 | if (!(dep->flags & DWC3_EP_TRANSFER_STARTED)) |
Janusz Dziedzic | d7fd41c | 2016-12-08 10:57:34 +0100 | [diff] [blame] | 3151 | return; |
| 3152 | |
| 3153 | /* Handle only EPCMDCMPLT when EP disabled */ |
| 3154 | if (event->endpoint_event != DWC3_DEPEVT_EPCMDCMPLT) |
| 3155 | return; |
| 3156 | } |
Felipe Balbi | 3336abb | 2012-06-06 09:19:35 +0300 | [diff] [blame] | 3157 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3158 | if (epnum == 0 || epnum == 1) { |
| 3159 | dwc3_ep0_interrupt(dwc, event); |
| 3160 | return; |
| 3161 | } |
| 3162 | |
| 3163 | switch (event->endpoint_event) { |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3164 | case DWC3_DEPEVT_XFERINPROGRESS: |
Felipe Balbi | 8f608e8 | 2018-03-27 10:53:29 +0300 | [diff] [blame] | 3165 | dwc3_gadget_endpoint_transfer_in_progress(dep, event); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3166 | break; |
| 3167 | case DWC3_DEPEVT_XFERNOTREADY: |
Felipe Balbi | 8f608e8 | 2018-03-27 10:53:29 +0300 | [diff] [blame] | 3168 | dwc3_gadget_endpoint_transfer_not_ready(dep, event); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3169 | break; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3170 | case DWC3_DEPEVT_EPCMDCMPLT: |
Thinh Nguyen | 8266b08 | 2020-07-30 16:29:03 -0700 | [diff] [blame] | 3171 | dwc3_gadget_endpoint_command_complete(dep, event); |
Baolin Wang | 76a638f | 2016-10-31 19:38:36 +0800 | [diff] [blame] | 3172 | break; |
Felipe Balbi | 742a4ff | 2018-03-26 13:26:56 +0300 | [diff] [blame] | 3173 | case DWC3_DEPEVT_XFERCOMPLETE: |
Thinh Nguyen | 3eaecd0 | 2020-05-05 19:46:51 -0700 | [diff] [blame] | 3174 | dwc3_gadget_endpoint_transfer_complete(dep, event); |
| 3175 | break; |
| 3176 | case DWC3_DEPEVT_STREAMEVT: |
Thinh Nguyen | 140ca4c | 2020-05-05 19:47:09 -0700 | [diff] [blame] | 3177 | dwc3_gadget_endpoint_stream_event(dep, event); |
| 3178 | break; |
Baolin Wang | 76a638f | 2016-10-31 19:38:36 +0800 | [diff] [blame] | 3179 | case DWC3_DEPEVT_RXTXFIFOEVT: |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3180 | break; |
| 3181 | } |
| 3182 | } |
| 3183 | |
| 3184 | static void dwc3_disconnect_gadget(struct dwc3 *dwc) |
| 3185 | { |
| 3186 | if (dwc->gadget_driver && dwc->gadget_driver->disconnect) { |
| 3187 | spin_unlock(&dwc->lock); |
Peter Chen | e81a701 | 2020-08-21 10:55:48 +0800 | [diff] [blame] | 3188 | dwc->gadget_driver->disconnect(dwc->gadget); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3189 | spin_lock(&dwc->lock); |
| 3190 | } |
| 3191 | } |
| 3192 | |
Felipe Balbi | bc5ba2e | 2014-02-26 10:17:07 -0600 | [diff] [blame] | 3193 | static void dwc3_suspend_gadget(struct dwc3 *dwc) |
| 3194 | { |
Dan Carpenter | 73a30bf | 2014-03-07 14:19:57 +0300 | [diff] [blame] | 3195 | if (dwc->gadget_driver && dwc->gadget_driver->suspend) { |
Felipe Balbi | bc5ba2e | 2014-02-26 10:17:07 -0600 | [diff] [blame] | 3196 | spin_unlock(&dwc->lock); |
Peter Chen | e81a701 | 2020-08-21 10:55:48 +0800 | [diff] [blame] | 3197 | dwc->gadget_driver->suspend(dwc->gadget); |
Felipe Balbi | bc5ba2e | 2014-02-26 10:17:07 -0600 | [diff] [blame] | 3198 | spin_lock(&dwc->lock); |
| 3199 | } |
| 3200 | } |
| 3201 | |
| 3202 | static void dwc3_resume_gadget(struct dwc3 *dwc) |
| 3203 | { |
Dan Carpenter | 73a30bf | 2014-03-07 14:19:57 +0300 | [diff] [blame] | 3204 | if (dwc->gadget_driver && dwc->gadget_driver->resume) { |
Felipe Balbi | bc5ba2e | 2014-02-26 10:17:07 -0600 | [diff] [blame] | 3205 | spin_unlock(&dwc->lock); |
Peter Chen | e81a701 | 2020-08-21 10:55:48 +0800 | [diff] [blame] | 3206 | dwc->gadget_driver->resume(dwc->gadget); |
Felipe Balbi | 5c7b3b0 | 2015-01-29 10:29:18 -0600 | [diff] [blame] | 3207 | spin_lock(&dwc->lock); |
Felipe Balbi | 8e74475 | 2014-11-06 14:27:53 +0800 | [diff] [blame] | 3208 | } |
| 3209 | } |
| 3210 | |
| 3211 | static void dwc3_reset_gadget(struct dwc3 *dwc) |
| 3212 | { |
| 3213 | if (!dwc->gadget_driver) |
| 3214 | return; |
| 3215 | |
Peter Chen | e81a701 | 2020-08-21 10:55:48 +0800 | [diff] [blame] | 3216 | if (dwc->gadget->speed != USB_SPEED_UNKNOWN) { |
Felipe Balbi | 8e74475 | 2014-11-06 14:27:53 +0800 | [diff] [blame] | 3217 | spin_unlock(&dwc->lock); |
Peter Chen | e81a701 | 2020-08-21 10:55:48 +0800 | [diff] [blame] | 3218 | usb_gadget_udc_reset(dwc->gadget, dwc->gadget_driver); |
Felipe Balbi | bc5ba2e | 2014-02-26 10:17:07 -0600 | [diff] [blame] | 3219 | spin_lock(&dwc->lock); |
| 3220 | } |
| 3221 | } |
| 3222 | |
Felipe Balbi | c5353b2 | 2019-02-13 13:00:54 +0200 | [diff] [blame] | 3223 | static void dwc3_stop_active_transfer(struct dwc3_ep *dep, bool force, |
| 3224 | bool interrupt) |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3225 | { |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3226 | struct dwc3_gadget_ep_cmd_params params; |
| 3227 | u32 cmd; |
| 3228 | int ret; |
| 3229 | |
Thinh Nguyen | c58d8bf | 2019-12-18 18:14:44 -0800 | [diff] [blame] | 3230 | if (!(dep->flags & DWC3_EP_TRANSFER_STARTED) || |
| 3231 | (dep->flags & DWC3_EP_END_TRANSFER_PENDING)) |
Pratyush Anand | 3daf74d | 2012-06-23 02:23:08 +0530 | [diff] [blame] | 3232 | return; |
| 3233 | |
Pratyush Anand | 5791150 | 2012-07-06 15:19:10 +0530 | [diff] [blame] | 3234 | /* |
| 3235 | * NOTICE: We are violating what the Databook says about the |
| 3236 | * EndTransfer command. Ideally we would _always_ wait for the |
| 3237 | * EndTransfer Command Completion IRQ, but that's causing too |
| 3238 | * much trouble synchronizing between us and gadget driver. |
| 3239 | * |
| 3240 | * We have discussed this with the IP Provider and it was |
Thinh Nguyen | cf2f8b6 | 2019-12-18 18:14:56 -0800 | [diff] [blame] | 3241 | * suggested to giveback all requests here. |
Pratyush Anand | 5791150 | 2012-07-06 15:19:10 +0530 | [diff] [blame] | 3242 | * |
| 3243 | * Note also that a similar handling was tested by Synopsys |
| 3244 | * (thanks a lot Paul) and nothing bad has come out of it. |
Thinh Nguyen | cf2f8b6 | 2019-12-18 18:14:56 -0800 | [diff] [blame] | 3245 | * In short, what we're doing is issuing EndTransfer with |
| 3246 | * CMDIOC bit set and delay kicking transfer until the |
| 3247 | * EndTransfer command had completed. |
John Youn | 06281d4 | 2016-08-22 15:39:13 -0700 | [diff] [blame] | 3248 | * |
| 3249 | * As of IP version 3.10a of the DWC_usb3 IP, the controller |
| 3250 | * supports a mode to work around the above limitation. The |
| 3251 | * software can poll the CMDACT bit in the DEPCMD register |
| 3252 | * after issuing a EndTransfer command. This mode is enabled |
| 3253 | * by writing GUCTL2[14]. This polling is already done in the |
| 3254 | * dwc3_send_gadget_ep_cmd() function so if the mode is |
| 3255 | * enabled, the EndTransfer command will have completed upon |
Thinh Nguyen | cf2f8b6 | 2019-12-18 18:14:56 -0800 | [diff] [blame] | 3256 | * returning from this function. |
John Youn | 06281d4 | 2016-08-22 15:39:13 -0700 | [diff] [blame] | 3257 | * |
| 3258 | * This mode is NOT available on the DWC_usb31 IP. |
Pratyush Anand | 5791150 | 2012-07-06 15:19:10 +0530 | [diff] [blame] | 3259 | */ |
| 3260 | |
Pratyush Anand | 3daf74d | 2012-06-23 02:23:08 +0530 | [diff] [blame] | 3261 | cmd = DWC3_DEPCMD_ENDTRANSFER; |
Paul Zimmerman | b992e68 | 2012-04-27 14:17:35 +0300 | [diff] [blame] | 3262 | cmd |= force ? DWC3_DEPCMD_HIPRI_FORCERM : 0; |
Felipe Balbi | c5353b2 | 2019-02-13 13:00:54 +0200 | [diff] [blame] | 3263 | cmd |= interrupt ? DWC3_DEPCMD_CMDIOC : 0; |
Felipe Balbi | b4996a8 | 2012-06-06 12:04:13 +0300 | [diff] [blame] | 3264 | cmd |= DWC3_DEPCMD_PARAM(dep->resource_index); |
Pratyush Anand | 3daf74d | 2012-06-23 02:23:08 +0530 | [diff] [blame] | 3265 | memset(¶ms, 0, sizeof(params)); |
Felipe Balbi | 2cd4718 | 2016-04-12 16:42:43 +0300 | [diff] [blame] | 3266 | ret = dwc3_send_gadget_ep_cmd(dep, cmd, ¶ms); |
Pratyush Anand | 3daf74d | 2012-06-23 02:23:08 +0530 | [diff] [blame] | 3267 | WARN_ON_ONCE(ret); |
Felipe Balbi | b4996a8 | 2012-06-06 12:04:13 +0300 | [diff] [blame] | 3268 | dep->resource_index = 0; |
John Youn | 06281d4 | 2016-08-22 15:39:13 -0700 | [diff] [blame] | 3269 | |
Thinh Nguyen | d3abda5 | 2019-11-27 13:10:47 -0800 | [diff] [blame] | 3270 | if (!interrupt) |
| 3271 | dep->flags &= ~DWC3_EP_TRANSFER_STARTED; |
Thinh Nguyen | c58d8bf | 2019-12-18 18:14:44 -0800 | [diff] [blame] | 3272 | else |
| 3273 | dep->flags |= DWC3_EP_END_TRANSFER_PENDING; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3274 | } |
| 3275 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3276 | static void dwc3_clear_stall_all_ep(struct dwc3 *dwc) |
| 3277 | { |
| 3278 | u32 epnum; |
| 3279 | |
| 3280 | for (epnum = 1; epnum < DWC3_ENDPOINTS_NUM; epnum++) { |
| 3281 | struct dwc3_ep *dep; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3282 | int ret; |
| 3283 | |
| 3284 | dep = dwc->eps[epnum]; |
Felipe Balbi | 6a1e3ef | 2011-05-05 16:21:59 +0300 | [diff] [blame] | 3285 | if (!dep) |
| 3286 | continue; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3287 | |
| 3288 | if (!(dep->flags & DWC3_EP_STALL)) |
| 3289 | continue; |
| 3290 | |
| 3291 | dep->flags &= ~DWC3_EP_STALL; |
| 3292 | |
John Youn | 50c763f | 2016-05-31 17:49:56 -0700 | [diff] [blame] | 3293 | ret = dwc3_send_clear_stall_ep_cmd(dep); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3294 | WARN_ON_ONCE(ret); |
| 3295 | } |
| 3296 | } |
| 3297 | |
| 3298 | static void dwc3_gadget_disconnect_interrupt(struct dwc3 *dwc) |
| 3299 | { |
Felipe Balbi | c4430a2 | 2012-05-24 10:30:01 +0300 | [diff] [blame] | 3300 | int reg; |
| 3301 | |
Thinh Nguyen | 1b6009ea | 2019-10-23 19:15:49 -0700 | [diff] [blame] | 3302 | dwc3_gadget_set_link_state(dwc, DWC3_LINK_STATE_RX_DET); |
| 3303 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3304 | reg = dwc3_readl(dwc->regs, DWC3_DCTL); |
| 3305 | reg &= ~DWC3_DCTL_INITU1ENA; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3306 | reg &= ~DWC3_DCTL_INITU2ENA; |
Thinh Nguyen | 5b73821 | 2019-10-23 19:15:43 -0700 | [diff] [blame] | 3307 | dwc3_gadget_dctl_write_safe(dwc, reg); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3308 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3309 | dwc3_disconnect_gadget(dwc); |
| 3310 | |
Peter Chen | e81a701 | 2020-08-21 10:55:48 +0800 | [diff] [blame] | 3311 | dwc->gadget->speed = USB_SPEED_UNKNOWN; |
Felipe Balbi | df62df5 | 2011-10-14 15:11:49 +0300 | [diff] [blame] | 3312 | dwc->setup_packet_pending = false; |
Peter Chen | e81a701 | 2020-08-21 10:55:48 +0800 | [diff] [blame] | 3313 | usb_gadget_set_state(dwc->gadget, USB_STATE_NOTATTACHED); |
Felipe Balbi | fc8bb91 | 2016-05-16 13:14:48 +0300 | [diff] [blame] | 3314 | |
| 3315 | dwc->connected = false; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3316 | } |
| 3317 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3318 | static void dwc3_gadget_reset_interrupt(struct dwc3 *dwc) |
| 3319 | { |
| 3320 | u32 reg; |
| 3321 | |
Felipe Balbi | df62df5 | 2011-10-14 15:11:49 +0300 | [diff] [blame] | 3322 | /* |
Wesley Cheng | 45f879b | 2021-03-19 02:31:25 -0700 | [diff] [blame] | 3323 | * Ideally, dwc3_reset_gadget() would trigger the function |
| 3324 | * drivers to stop any active transfers through ep disable. |
| 3325 | * However, for functions which defer ep disable, such as mass |
| 3326 | * storage, we will need to rely on the call to stop active |
| 3327 | * transfers here, and avoid allowing of request queuing. |
| 3328 | */ |
| 3329 | dwc->connected = false; |
| 3330 | |
| 3331 | /* |
Felipe Balbi | df62df5 | 2011-10-14 15:11:49 +0300 | [diff] [blame] | 3332 | * WORKAROUND: DWC3 revisions <1.88a have an issue which |
| 3333 | * would cause a missing Disconnect Event if there's a |
| 3334 | * pending Setup Packet in the FIFO. |
| 3335 | * |
| 3336 | * There's no suggested workaround on the official Bug |
| 3337 | * report, which states that "unless the driver/application |
| 3338 | * is doing any special handling of a disconnect event, |
| 3339 | * there is no functional issue". |
| 3340 | * |
| 3341 | * Unfortunately, it turns out that we _do_ some special |
| 3342 | * handling of a disconnect event, namely complete all |
| 3343 | * pending transfers, notify gadget driver of the |
| 3344 | * disconnection, and so on. |
| 3345 | * |
| 3346 | * Our suggested workaround is to follow the Disconnect |
| 3347 | * Event steps here, instead, based on a setup_packet_pending |
Felipe Balbi | b5d335e | 2015-11-16 16:20:34 -0600 | [diff] [blame] | 3348 | * flag. Such flag gets set whenever we have a SETUP_PENDING |
| 3349 | * status for EP0 TRBs and gets cleared on XferComplete for the |
Felipe Balbi | df62df5 | 2011-10-14 15:11:49 +0300 | [diff] [blame] | 3350 | * same endpoint. |
| 3351 | * |
| 3352 | * Refers to: |
| 3353 | * |
| 3354 | * STAR#9000466709: RTL: Device : Disconnect event not |
| 3355 | * generated if setup packet pending in FIFO |
| 3356 | */ |
Thinh Nguyen | 9af21dd | 2020-04-11 19:20:01 -0700 | [diff] [blame] | 3357 | if (DWC3_VER_IS_PRIOR(DWC3, 188A)) { |
Felipe Balbi | df62df5 | 2011-10-14 15:11:49 +0300 | [diff] [blame] | 3358 | if (dwc->setup_packet_pending) |
| 3359 | dwc3_gadget_disconnect_interrupt(dwc); |
| 3360 | } |
| 3361 | |
Felipe Balbi | 8e74475 | 2014-11-06 14:27:53 +0800 | [diff] [blame] | 3362 | dwc3_reset_gadget(dwc); |
Wesley Cheng | ae7e861 | 2020-09-28 17:20:59 -0700 | [diff] [blame] | 3363 | /* |
| 3364 | * In the Synopsis DesignWare Cores USB3 Databook Rev. 3.30a |
| 3365 | * Section 4.1.2 Table 4-2, it states that during a USB reset, the SW |
| 3366 | * needs to ensure that it sends "a DEPENDXFER command for any active |
| 3367 | * transfers." |
| 3368 | */ |
| 3369 | dwc3_stop_active_transfers(dwc); |
Wesley Cheng | c7bb96a | 2021-03-11 15:59:02 -0800 | [diff] [blame] | 3370 | dwc->connected = true; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3371 | |
| 3372 | reg = dwc3_readl(dwc->regs, DWC3_DCTL); |
| 3373 | reg &= ~DWC3_DCTL_TSTCTRL_MASK; |
Thinh Nguyen | 5b73821 | 2019-10-23 19:15:43 -0700 | [diff] [blame] | 3374 | dwc3_gadget_dctl_write_safe(dwc, reg); |
Gerard Cauvy | 3b63736 | 2012-02-10 12:21:18 +0200 | [diff] [blame] | 3375 | dwc->test_mode = false; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3376 | dwc3_clear_stall_all_ep(dwc); |
| 3377 | |
| 3378 | /* Reset device address to zero */ |
| 3379 | reg = dwc3_readl(dwc->regs, DWC3_DCFG); |
| 3380 | reg &= ~(DWC3_DCFG_DEVADDR_MASK); |
| 3381 | dwc3_writel(dwc->regs, DWC3_DCFG, reg); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3382 | } |
| 3383 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3384 | static void dwc3_gadget_conndone_interrupt(struct dwc3 *dwc) |
| 3385 | { |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3386 | struct dwc3_ep *dep; |
| 3387 | int ret; |
| 3388 | u32 reg; |
| 3389 | u8 speed; |
| 3390 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3391 | reg = dwc3_readl(dwc->regs, DWC3_DSTS); |
| 3392 | speed = reg & DWC3_DSTS_CONNECTSPD; |
| 3393 | dwc->speed = speed; |
| 3394 | |
John Youn | 5fb6fda | 2016-11-10 17:23:25 -0800 | [diff] [blame] | 3395 | /* |
| 3396 | * RAMClkSel is reset to 0 after USB reset, so it must be reprogrammed |
| 3397 | * each time on Connect Done. |
| 3398 | * |
| 3399 | * Currently we always use the reset value. If any platform |
| 3400 | * wants to set this to a different value, we need to add a |
| 3401 | * setting and update GCTL.RAMCLKSEL here. |
| 3402 | */ |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3403 | |
| 3404 | switch (speed) { |
John Youn | 2da9ad7 | 2016-05-20 16:34:26 -0700 | [diff] [blame] | 3405 | case DWC3_DSTS_SUPERSPEED_PLUS: |
John Youn | 7580862 | 2016-02-05 17:09:13 -0800 | [diff] [blame] | 3406 | dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512); |
Peter Chen | e81a701 | 2020-08-21 10:55:48 +0800 | [diff] [blame] | 3407 | dwc->gadget->ep0->maxpacket = 512; |
| 3408 | dwc->gadget->speed = USB_SPEED_SUPER_PLUS; |
John Youn | 7580862 | 2016-02-05 17:09:13 -0800 | [diff] [blame] | 3409 | break; |
John Youn | 2da9ad7 | 2016-05-20 16:34:26 -0700 | [diff] [blame] | 3410 | case DWC3_DSTS_SUPERSPEED: |
Felipe Balbi | 05870c5 | 2011-10-14 14:51:38 +0300 | [diff] [blame] | 3411 | /* |
| 3412 | * WORKAROUND: DWC3 revisions <1.90a have an issue which |
| 3413 | * would cause a missing USB3 Reset event. |
| 3414 | * |
| 3415 | * In such situations, we should force a USB3 Reset |
| 3416 | * event by calling our dwc3_gadget_reset_interrupt() |
| 3417 | * routine. |
| 3418 | * |
| 3419 | * Refers to: |
| 3420 | * |
| 3421 | * STAR#9000483510: RTL: SS : USB3 reset event may |
| 3422 | * not be generated always when the link enters poll |
| 3423 | */ |
Thinh Nguyen | 9af21dd | 2020-04-11 19:20:01 -0700 | [diff] [blame] | 3424 | if (DWC3_VER_IS_PRIOR(DWC3, 190A)) |
Felipe Balbi | 05870c5 | 2011-10-14 14:51:38 +0300 | [diff] [blame] | 3425 | dwc3_gadget_reset_interrupt(dwc); |
| 3426 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3427 | dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512); |
Peter Chen | e81a701 | 2020-08-21 10:55:48 +0800 | [diff] [blame] | 3428 | dwc->gadget->ep0->maxpacket = 512; |
| 3429 | dwc->gadget->speed = USB_SPEED_SUPER; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3430 | break; |
John Youn | 2da9ad7 | 2016-05-20 16:34:26 -0700 | [diff] [blame] | 3431 | case DWC3_DSTS_HIGHSPEED: |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3432 | dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64); |
Peter Chen | e81a701 | 2020-08-21 10:55:48 +0800 | [diff] [blame] | 3433 | dwc->gadget->ep0->maxpacket = 64; |
| 3434 | dwc->gadget->speed = USB_SPEED_HIGH; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3435 | break; |
Roger Quadros | 9418ee1 | 2017-01-03 14:32:09 +0200 | [diff] [blame] | 3436 | case DWC3_DSTS_FULLSPEED: |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3437 | dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64); |
Peter Chen | e81a701 | 2020-08-21 10:55:48 +0800 | [diff] [blame] | 3438 | dwc->gadget->ep0->maxpacket = 64; |
| 3439 | dwc->gadget->speed = USB_SPEED_FULL; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3440 | break; |
John Youn | 2da9ad7 | 2016-05-20 16:34:26 -0700 | [diff] [blame] | 3441 | case DWC3_DSTS_LOWSPEED: |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3442 | dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(8); |
Peter Chen | e81a701 | 2020-08-21 10:55:48 +0800 | [diff] [blame] | 3443 | dwc->gadget->ep0->maxpacket = 8; |
| 3444 | dwc->gadget->speed = USB_SPEED_LOW; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3445 | break; |
| 3446 | } |
| 3447 | |
Peter Chen | e81a701 | 2020-08-21 10:55:48 +0800 | [diff] [blame] | 3448 | dwc->eps[1]->endpoint.maxpacket = dwc->gadget->ep0->maxpacket; |
Thinh Nguyen | 6180026 | 2018-01-12 18:18:05 -0800 | [diff] [blame] | 3449 | |
Pratyush Anand | 2b75835 | 2013-01-14 15:59:31 +0530 | [diff] [blame] | 3450 | /* Enable USB2 LPM Capability */ |
| 3451 | |
Thinh Nguyen | 9af21dd | 2020-04-11 19:20:01 -0700 | [diff] [blame] | 3452 | if (!DWC3_VER_IS_WITHIN(DWC3, ANY, 194A) && |
Thinh Nguyen | 8f7cdbb | 2021-04-13 19:13:18 -0700 | [diff] [blame] | 3453 | !dwc->usb2_gadget_lpm_disable && |
John Youn | 2da9ad7 | 2016-05-20 16:34:26 -0700 | [diff] [blame] | 3454 | (speed != DWC3_DSTS_SUPERSPEED) && |
| 3455 | (speed != DWC3_DSTS_SUPERSPEED_PLUS)) { |
Pratyush Anand | 2b75835 | 2013-01-14 15:59:31 +0530 | [diff] [blame] | 3456 | reg = dwc3_readl(dwc->regs, DWC3_DCFG); |
| 3457 | reg |= DWC3_DCFG_LPM_CAP; |
| 3458 | dwc3_writel(dwc->regs, DWC3_DCFG, reg); |
| 3459 | |
| 3460 | reg = dwc3_readl(dwc->regs, DWC3_DCTL); |
| 3461 | reg &= ~(DWC3_DCTL_HIRD_THRES_MASK | DWC3_DCTL_L1_HIBER_EN); |
| 3462 | |
Thinh Nguyen | 16fe4f3 | 2019-08-19 18:35:58 -0700 | [diff] [blame] | 3463 | reg |= DWC3_DCTL_HIRD_THRES(dwc->hird_threshold | |
| 3464 | (dwc->is_utmi_l1_suspend << 4)); |
Pratyush Anand | 2b75835 | 2013-01-14 15:59:31 +0530 | [diff] [blame] | 3465 | |
Huang Rui | 80caf7d | 2014-10-28 19:54:26 +0800 | [diff] [blame] | 3466 | /* |
| 3467 | * When dwc3 revisions >= 2.40a, LPM Erratum is enabled and |
| 3468 | * DCFG.LPMCap is set, core responses with an ACK and the |
| 3469 | * BESL value in the LPM token is less than or equal to LPM |
| 3470 | * NYET threshold. |
| 3471 | */ |
Thinh Nguyen | 9af21dd | 2020-04-11 19:20:01 -0700 | [diff] [blame] | 3472 | WARN_ONCE(DWC3_VER_IS_PRIOR(DWC3, 240A) && dwc->has_lpm_erratum, |
Masanari Iida | 9165dab | 2016-09-17 23:44:17 +0900 | [diff] [blame] | 3473 | "LPM Erratum not available on dwc3 revisions < 2.40a\n"); |
Huang Rui | 80caf7d | 2014-10-28 19:54:26 +0800 | [diff] [blame] | 3474 | |
Thinh Nguyen | 9af21dd | 2020-04-11 19:20:01 -0700 | [diff] [blame] | 3475 | if (dwc->has_lpm_erratum && !DWC3_VER_IS_PRIOR(DWC3, 240A)) |
Thinh Nguyen | 2e487d2 | 2019-04-25 13:55:30 -0700 | [diff] [blame] | 3476 | reg |= DWC3_DCTL_NYET_THRES(dwc->lpm_nyet_threshold); |
Huang Rui | 80caf7d | 2014-10-28 19:54:26 +0800 | [diff] [blame] | 3477 | |
Thinh Nguyen | 5b73821 | 2019-10-23 19:15:43 -0700 | [diff] [blame] | 3478 | dwc3_gadget_dctl_write_safe(dwc, reg); |
Felipe Balbi | 356363b | 2013-12-19 16:37:05 -0600 | [diff] [blame] | 3479 | } else { |
Thinh Nguyen | 8f7cdbb | 2021-04-13 19:13:18 -0700 | [diff] [blame] | 3480 | if (dwc->usb2_gadget_lpm_disable) { |
| 3481 | reg = dwc3_readl(dwc->regs, DWC3_DCFG); |
| 3482 | reg &= ~DWC3_DCFG_LPM_CAP; |
| 3483 | dwc3_writel(dwc->regs, DWC3_DCFG, reg); |
| 3484 | } |
| 3485 | |
Felipe Balbi | 356363b | 2013-12-19 16:37:05 -0600 | [diff] [blame] | 3486 | reg = dwc3_readl(dwc->regs, DWC3_DCTL); |
| 3487 | reg &= ~DWC3_DCTL_HIRD_THRES_MASK; |
Thinh Nguyen | 5b73821 | 2019-10-23 19:15:43 -0700 | [diff] [blame] | 3488 | dwc3_gadget_dctl_write_safe(dwc, reg); |
Pratyush Anand | 2b75835 | 2013-01-14 15:59:31 +0530 | [diff] [blame] | 3489 | } |
| 3490 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3491 | dep = dwc->eps[0]; |
Felipe Balbi | a2d23f0 | 2018-04-09 12:40:48 +0300 | [diff] [blame] | 3492 | ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_MODIFY); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3493 | if (ret) { |
| 3494 | dev_err(dwc->dev, "failed to enable %s\n", dep->name); |
| 3495 | return; |
| 3496 | } |
| 3497 | |
| 3498 | dep = dwc->eps[1]; |
Felipe Balbi | a2d23f0 | 2018-04-09 12:40:48 +0300 | [diff] [blame] | 3499 | ret = __dwc3_gadget_ep_enable(dep, DWC3_DEPCFG_ACTION_MODIFY); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3500 | if (ret) { |
| 3501 | dev_err(dwc->dev, "failed to enable %s\n", dep->name); |
| 3502 | return; |
| 3503 | } |
| 3504 | |
| 3505 | /* |
| 3506 | * Configure PHY via GUSB3PIPECTLn if required. |
| 3507 | * |
| 3508 | * Update GTXFIFOSIZn |
| 3509 | * |
| 3510 | * In both cases reset values should be sufficient. |
| 3511 | */ |
| 3512 | } |
| 3513 | |
| 3514 | static void dwc3_gadget_wakeup_interrupt(struct dwc3 *dwc) |
| 3515 | { |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3516 | /* |
| 3517 | * TODO take core out of low power mode when that's |
| 3518 | * implemented. |
| 3519 | */ |
| 3520 | |
Jiebing Li | ad14d4e | 2014-12-11 13:26:29 +0800 | [diff] [blame] | 3521 | if (dwc->gadget_driver && dwc->gadget_driver->resume) { |
| 3522 | spin_unlock(&dwc->lock); |
Peter Chen | e81a701 | 2020-08-21 10:55:48 +0800 | [diff] [blame] | 3523 | dwc->gadget_driver->resume(dwc->gadget); |
Jiebing Li | ad14d4e | 2014-12-11 13:26:29 +0800 | [diff] [blame] | 3524 | spin_lock(&dwc->lock); |
| 3525 | } |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3526 | } |
| 3527 | |
| 3528 | static void dwc3_gadget_linksts_change_interrupt(struct dwc3 *dwc, |
| 3529 | unsigned int evtinfo) |
| 3530 | { |
Felipe Balbi | fae2b90 | 2011-10-14 13:00:30 +0300 | [diff] [blame] | 3531 | enum dwc3_link_state next = evtinfo & DWC3_LINK_STATE_MASK; |
Felipe Balbi | 0b0cc1c | 2012-09-18 21:39:24 +0300 | [diff] [blame] | 3532 | unsigned int pwropt; |
| 3533 | |
| 3534 | /* |
| 3535 | * WORKAROUND: DWC3 < 2.50a have an issue when configured without |
| 3536 | * Hibernation mode enabled which would show up when device detects |
| 3537 | * host-initiated U3 exit. |
| 3538 | * |
| 3539 | * In that case, device will generate a Link State Change Interrupt |
| 3540 | * from U3 to RESUME which is only necessary if Hibernation is |
| 3541 | * configured in. |
| 3542 | * |
| 3543 | * There are no functional changes due to such spurious event and we |
| 3544 | * just need to ignore it. |
| 3545 | * |
| 3546 | * Refers to: |
| 3547 | * |
| 3548 | * STAR#9000570034 RTL: SS Resume event generated in non-Hibernation |
| 3549 | * operational mode |
| 3550 | */ |
| 3551 | pwropt = DWC3_GHWPARAMS1_EN_PWROPT(dwc->hwparams.hwparams1); |
Thinh Nguyen | 9af21dd | 2020-04-11 19:20:01 -0700 | [diff] [blame] | 3552 | if (DWC3_VER_IS_PRIOR(DWC3, 250A) && |
Felipe Balbi | 0b0cc1c | 2012-09-18 21:39:24 +0300 | [diff] [blame] | 3553 | (pwropt != DWC3_GHWPARAMS1_EN_PWROPT_HIB)) { |
| 3554 | if ((dwc->link_state == DWC3_LINK_STATE_U3) && |
| 3555 | (next == DWC3_LINK_STATE_RESUME)) { |
Felipe Balbi | 0b0cc1c | 2012-09-18 21:39:24 +0300 | [diff] [blame] | 3556 | return; |
| 3557 | } |
| 3558 | } |
Felipe Balbi | fae2b90 | 2011-10-14 13:00:30 +0300 | [diff] [blame] | 3559 | |
| 3560 | /* |
| 3561 | * WORKAROUND: DWC3 Revisions <1.83a have an issue which, depending |
| 3562 | * on the link partner, the USB session might do multiple entry/exit |
| 3563 | * of low power states before a transfer takes place. |
| 3564 | * |
| 3565 | * Due to this problem, we might experience lower throughput. The |
| 3566 | * suggested workaround is to disable DCTL[12:9] bits if we're |
| 3567 | * transitioning from U1/U2 to U0 and enable those bits again |
| 3568 | * after a transfer completes and there are no pending transfers |
| 3569 | * on any of the enabled endpoints. |
| 3570 | * |
| 3571 | * This is the first half of that workaround. |
| 3572 | * |
| 3573 | * Refers to: |
| 3574 | * |
| 3575 | * STAR#9000446952: RTL: Device SS : if U1/U2 ->U0 takes >128us |
| 3576 | * core send LGO_Ux entering U0 |
| 3577 | */ |
Thinh Nguyen | 9af21dd | 2020-04-11 19:20:01 -0700 | [diff] [blame] | 3578 | if (DWC3_VER_IS_PRIOR(DWC3, 183A)) { |
Felipe Balbi | fae2b90 | 2011-10-14 13:00:30 +0300 | [diff] [blame] | 3579 | if (next == DWC3_LINK_STATE_U0) { |
| 3580 | u32 u1u2; |
| 3581 | u32 reg; |
| 3582 | |
| 3583 | switch (dwc->link_state) { |
| 3584 | case DWC3_LINK_STATE_U1: |
| 3585 | case DWC3_LINK_STATE_U2: |
| 3586 | reg = dwc3_readl(dwc->regs, DWC3_DCTL); |
| 3587 | u1u2 = reg & (DWC3_DCTL_INITU2ENA |
| 3588 | | DWC3_DCTL_ACCEPTU2ENA |
| 3589 | | DWC3_DCTL_INITU1ENA |
| 3590 | | DWC3_DCTL_ACCEPTU1ENA); |
| 3591 | |
| 3592 | if (!dwc->u1u2) |
| 3593 | dwc->u1u2 = reg & u1u2; |
| 3594 | |
| 3595 | reg &= ~u1u2; |
| 3596 | |
Thinh Nguyen | 5b73821 | 2019-10-23 19:15:43 -0700 | [diff] [blame] | 3597 | dwc3_gadget_dctl_write_safe(dwc, reg); |
Felipe Balbi | fae2b90 | 2011-10-14 13:00:30 +0300 | [diff] [blame] | 3598 | break; |
| 3599 | default: |
| 3600 | /* do nothing */ |
| 3601 | break; |
| 3602 | } |
| 3603 | } |
| 3604 | } |
| 3605 | |
Felipe Balbi | bc5ba2e | 2014-02-26 10:17:07 -0600 | [diff] [blame] | 3606 | switch (next) { |
| 3607 | case DWC3_LINK_STATE_U1: |
| 3608 | if (dwc->speed == USB_SPEED_SUPER) |
| 3609 | dwc3_suspend_gadget(dwc); |
| 3610 | break; |
| 3611 | case DWC3_LINK_STATE_U2: |
| 3612 | case DWC3_LINK_STATE_U3: |
| 3613 | dwc3_suspend_gadget(dwc); |
| 3614 | break; |
| 3615 | case DWC3_LINK_STATE_RESUME: |
| 3616 | dwc3_resume_gadget(dwc); |
| 3617 | break; |
| 3618 | default: |
| 3619 | /* do nothing */ |
| 3620 | break; |
| 3621 | } |
| 3622 | |
Felipe Balbi | e57ebc1 | 2014-04-22 13:20:12 -0500 | [diff] [blame] | 3623 | dwc->link_state = next; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3624 | } |
| 3625 | |
Baolin Wang | 72704f8 | 2016-05-16 16:43:53 +0800 | [diff] [blame] | 3626 | static void dwc3_gadget_suspend_interrupt(struct dwc3 *dwc, |
| 3627 | unsigned int evtinfo) |
| 3628 | { |
| 3629 | enum dwc3_link_state next = evtinfo & DWC3_LINK_STATE_MASK; |
| 3630 | |
| 3631 | if (dwc->link_state != next && next == DWC3_LINK_STATE_U3) |
| 3632 | dwc3_suspend_gadget(dwc); |
| 3633 | |
| 3634 | dwc->link_state = next; |
| 3635 | } |
| 3636 | |
Felipe Balbi | e1dadd3 | 2014-02-25 14:47:54 -0600 | [diff] [blame] | 3637 | static void dwc3_gadget_hibernation_interrupt(struct dwc3 *dwc, |
| 3638 | unsigned int evtinfo) |
| 3639 | { |
| 3640 | unsigned int is_ss = evtinfo & BIT(4); |
| 3641 | |
Felipe Balbi | bfad65e | 2017-04-19 14:59:27 +0300 | [diff] [blame] | 3642 | /* |
Felipe Balbi | e1dadd3 | 2014-02-25 14:47:54 -0600 | [diff] [blame] | 3643 | * WORKAROUND: DWC3 revison 2.20a with hibernation support |
| 3644 | * have a known issue which can cause USB CV TD.9.23 to fail |
| 3645 | * randomly. |
| 3646 | * |
| 3647 | * Because of this issue, core could generate bogus hibernation |
| 3648 | * events which SW needs to ignore. |
| 3649 | * |
| 3650 | * Refers to: |
| 3651 | * |
| 3652 | * STAR#9000546576: Device Mode Hibernation: Issue in USB 2.0 |
| 3653 | * Device Fallback from SuperSpeed |
| 3654 | */ |
| 3655 | if (is_ss ^ (dwc->speed == USB_SPEED_SUPER)) |
| 3656 | return; |
| 3657 | |
| 3658 | /* enter hibernation here */ |
| 3659 | } |
| 3660 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3661 | static void dwc3_gadget_interrupt(struct dwc3 *dwc, |
| 3662 | const struct dwc3_event_devt *event) |
| 3663 | { |
| 3664 | switch (event->type) { |
| 3665 | case DWC3_DEVICE_EVENT_DISCONNECT: |
| 3666 | dwc3_gadget_disconnect_interrupt(dwc); |
| 3667 | break; |
| 3668 | case DWC3_DEVICE_EVENT_RESET: |
| 3669 | dwc3_gadget_reset_interrupt(dwc); |
| 3670 | break; |
| 3671 | case DWC3_DEVICE_EVENT_CONNECT_DONE: |
| 3672 | dwc3_gadget_conndone_interrupt(dwc); |
| 3673 | break; |
| 3674 | case DWC3_DEVICE_EVENT_WAKEUP: |
| 3675 | dwc3_gadget_wakeup_interrupt(dwc); |
| 3676 | break; |
Felipe Balbi | e1dadd3 | 2014-02-25 14:47:54 -0600 | [diff] [blame] | 3677 | case DWC3_DEVICE_EVENT_HIBER_REQ: |
| 3678 | if (dev_WARN_ONCE(dwc->dev, !dwc->has_hibernation, |
| 3679 | "unexpected hibernation event\n")) |
| 3680 | break; |
| 3681 | |
| 3682 | dwc3_gadget_hibernation_interrupt(dwc, event->event_info); |
| 3683 | break; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3684 | case DWC3_DEVICE_EVENT_LINK_STATUS_CHANGE: |
| 3685 | dwc3_gadget_linksts_change_interrupt(dwc, event->event_info); |
| 3686 | break; |
| 3687 | case DWC3_DEVICE_EVENT_EOPF: |
Baolin Wang | 72704f8 | 2016-05-16 16:43:53 +0800 | [diff] [blame] | 3688 | /* It changed to be suspend event for version 2.30a and above */ |
Thinh Nguyen | 9af21dd | 2020-04-11 19:20:01 -0700 | [diff] [blame] | 3689 | if (!DWC3_VER_IS_PRIOR(DWC3, 230A)) { |
Baolin Wang | 72704f8 | 2016-05-16 16:43:53 +0800 | [diff] [blame] | 3690 | /* |
| 3691 | * Ignore suspend event until the gadget enters into |
| 3692 | * USB_STATE_CONFIGURED state. |
| 3693 | */ |
Peter Chen | e81a701 | 2020-08-21 10:55:48 +0800 | [diff] [blame] | 3694 | if (dwc->gadget->state >= USB_STATE_CONFIGURED) |
Baolin Wang | 72704f8 | 2016-05-16 16:43:53 +0800 | [diff] [blame] | 3695 | dwc3_gadget_suspend_interrupt(dwc, |
| 3696 | event->event_info); |
| 3697 | } |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3698 | break; |
| 3699 | case DWC3_DEVICE_EVENT_SOF: |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3700 | case DWC3_DEVICE_EVENT_ERRATIC_ERROR: |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3701 | case DWC3_DEVICE_EVENT_CMD_CMPL: |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3702 | case DWC3_DEVICE_EVENT_OVERFLOW: |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3703 | break; |
| 3704 | default: |
Felipe Balbi | e9f2aa8 | 2015-01-27 13:49:28 -0600 | [diff] [blame] | 3705 | dev_WARN(dwc->dev, "UNKNOWN IRQ %d\n", event->type); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3706 | } |
| 3707 | } |
| 3708 | |
| 3709 | static void dwc3_process_event_entry(struct dwc3 *dwc, |
| 3710 | const union dwc3_event *event) |
| 3711 | { |
Felipe Balbi | 43c96be | 2016-09-26 13:23:34 +0300 | [diff] [blame] | 3712 | trace_dwc3_event(event->raw, dwc); |
Felipe Balbi | 2c4cbe6e5 | 2014-04-30 17:45:10 -0500 | [diff] [blame] | 3713 | |
Felipe Balbi | dfc5e80 | 2017-04-26 13:44:51 +0300 | [diff] [blame] | 3714 | if (!event->type.is_devspec) |
| 3715 | dwc3_endpoint_interrupt(dwc, &event->depevt); |
| 3716 | else if (event->type.type == DWC3_EVENT_TYPE_DEV) |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3717 | dwc3_gadget_interrupt(dwc, &event->devt); |
Felipe Balbi | dfc5e80 | 2017-04-26 13:44:51 +0300 | [diff] [blame] | 3718 | else |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3719 | dev_err(dwc->dev, "UNKNOWN IRQ type %d\n", event->raw); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3720 | } |
| 3721 | |
Felipe Balbi | dea520a | 2016-03-30 09:39:34 +0300 | [diff] [blame] | 3722 | static irqreturn_t dwc3_process_event_buf(struct dwc3_event_buffer *evt) |
Felipe Balbi | f42f244 | 2013-06-12 21:25:08 +0300 | [diff] [blame] | 3723 | { |
Felipe Balbi | dea520a | 2016-03-30 09:39:34 +0300 | [diff] [blame] | 3724 | struct dwc3 *dwc = evt->dwc; |
Felipe Balbi | f42f244 | 2013-06-12 21:25:08 +0300 | [diff] [blame] | 3725 | irqreturn_t ret = IRQ_NONE; |
| 3726 | int left; |
| 3727 | u32 reg; |
| 3728 | |
Felipe Balbi | f42f244 | 2013-06-12 21:25:08 +0300 | [diff] [blame] | 3729 | left = evt->count; |
| 3730 | |
| 3731 | if (!(evt->flags & DWC3_EVENT_PENDING)) |
| 3732 | return IRQ_NONE; |
| 3733 | |
| 3734 | while (left > 0) { |
| 3735 | union dwc3_event event; |
| 3736 | |
John Youn | ebbb2d5 | 2016-11-15 13:07:02 +0200 | [diff] [blame] | 3737 | event.raw = *(u32 *) (evt->cache + evt->lpos); |
Felipe Balbi | f42f244 | 2013-06-12 21:25:08 +0300 | [diff] [blame] | 3738 | |
| 3739 | dwc3_process_event_entry(dwc, &event); |
| 3740 | |
| 3741 | /* |
| 3742 | * FIXME we wrap around correctly to the next entry as |
| 3743 | * almost all entries are 4 bytes in size. There is one |
| 3744 | * entry which has 12 bytes which is a regular entry |
| 3745 | * followed by 8 bytes data. ATM I don't know how |
| 3746 | * things are organized if we get next to the a |
| 3747 | * boundary so I worry about that once we try to handle |
| 3748 | * that. |
| 3749 | */ |
Felipe Balbi | caefe6c | 2016-11-15 13:05:23 +0200 | [diff] [blame] | 3750 | evt->lpos = (evt->lpos + 4) % evt->length; |
Felipe Balbi | f42f244 | 2013-06-12 21:25:08 +0300 | [diff] [blame] | 3751 | left -= 4; |
Felipe Balbi | f42f244 | 2013-06-12 21:25:08 +0300 | [diff] [blame] | 3752 | } |
| 3753 | |
| 3754 | evt->count = 0; |
| 3755 | evt->flags &= ~DWC3_EVENT_PENDING; |
| 3756 | ret = IRQ_HANDLED; |
| 3757 | |
| 3758 | /* Unmask interrupt */ |
Felipe Balbi | 660e9bd | 2016-03-30 09:26:24 +0300 | [diff] [blame] | 3759 | reg = dwc3_readl(dwc->regs, DWC3_GEVNTSIZ(0)); |
Felipe Balbi | f42f244 | 2013-06-12 21:25:08 +0300 | [diff] [blame] | 3760 | reg &= ~DWC3_GEVNTSIZ_INTMASK; |
Felipe Balbi | 660e9bd | 2016-03-30 09:26:24 +0300 | [diff] [blame] | 3761 | dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0), reg); |
Felipe Balbi | f42f244 | 2013-06-12 21:25:08 +0300 | [diff] [blame] | 3762 | |
John Youn | cf40b86 | 2016-11-14 12:32:43 -0800 | [diff] [blame] | 3763 | if (dwc->imod_interval) { |
| 3764 | dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), DWC3_GEVNTCOUNT_EHB); |
| 3765 | dwc3_writel(dwc->regs, DWC3_DEV_IMOD(0), dwc->imod_interval); |
| 3766 | } |
| 3767 | |
Felipe Balbi | f42f244 | 2013-06-12 21:25:08 +0300 | [diff] [blame] | 3768 | return ret; |
| 3769 | } |
| 3770 | |
Felipe Balbi | dea520a | 2016-03-30 09:39:34 +0300 | [diff] [blame] | 3771 | static irqreturn_t dwc3_thread_interrupt(int irq, void *_evt) |
Felipe Balbi | b15a762 | 2011-06-30 16:57:15 +0300 | [diff] [blame] | 3772 | { |
Felipe Balbi | dea520a | 2016-03-30 09:39:34 +0300 | [diff] [blame] | 3773 | struct dwc3_event_buffer *evt = _evt; |
| 3774 | struct dwc3 *dwc = evt->dwc; |
Felipe Balbi | e5f68b4a | 2015-10-12 13:25:44 -0500 | [diff] [blame] | 3775 | unsigned long flags; |
Felipe Balbi | b15a762 | 2011-06-30 16:57:15 +0300 | [diff] [blame] | 3776 | irqreturn_t ret = IRQ_NONE; |
Felipe Balbi | b15a762 | 2011-06-30 16:57:15 +0300 | [diff] [blame] | 3777 | |
Felipe Balbi | e5f68b4a | 2015-10-12 13:25:44 -0500 | [diff] [blame] | 3778 | spin_lock_irqsave(&dwc->lock, flags); |
Felipe Balbi | dea520a | 2016-03-30 09:39:34 +0300 | [diff] [blame] | 3779 | ret = dwc3_process_event_buf(evt); |
Felipe Balbi | e5f68b4a | 2015-10-12 13:25:44 -0500 | [diff] [blame] | 3780 | spin_unlock_irqrestore(&dwc->lock, flags); |
Felipe Balbi | b15a762 | 2011-06-30 16:57:15 +0300 | [diff] [blame] | 3781 | |
| 3782 | return ret; |
| 3783 | } |
| 3784 | |
Felipe Balbi | dea520a | 2016-03-30 09:39:34 +0300 | [diff] [blame] | 3785 | static irqreturn_t dwc3_check_event_buf(struct dwc3_event_buffer *evt) |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3786 | { |
Felipe Balbi | dea520a | 2016-03-30 09:39:34 +0300 | [diff] [blame] | 3787 | struct dwc3 *dwc = evt->dwc; |
John Youn | ebbb2d5 | 2016-11-15 13:07:02 +0200 | [diff] [blame] | 3788 | u32 amount; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3789 | u32 count; |
Felipe Balbi | e8adfc3 | 2013-06-12 21:11:14 +0300 | [diff] [blame] | 3790 | u32 reg; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3791 | |
Felipe Balbi | fc8bb91 | 2016-05-16 13:14:48 +0300 | [diff] [blame] | 3792 | if (pm_runtime_suspended(dwc->dev)) { |
| 3793 | pm_runtime_get(dwc->dev); |
| 3794 | disable_irq_nosync(dwc->irq_gadget); |
| 3795 | dwc->pending_events = true; |
| 3796 | return IRQ_HANDLED; |
| 3797 | } |
| 3798 | |
Thinh Nguyen | d325a1d | 2017-05-11 17:26:47 -0700 | [diff] [blame] | 3799 | /* |
| 3800 | * With PCIe legacy interrupt, test shows that top-half irq handler can |
| 3801 | * be called again after HW interrupt deassertion. Check if bottom-half |
| 3802 | * irq event handler completes before caching new event to prevent |
| 3803 | * losing events. |
| 3804 | */ |
| 3805 | if (evt->flags & DWC3_EVENT_PENDING) |
| 3806 | return IRQ_HANDLED; |
| 3807 | |
Felipe Balbi | 660e9bd | 2016-03-30 09:26:24 +0300 | [diff] [blame] | 3808 | count = dwc3_readl(dwc->regs, DWC3_GEVNTCOUNT(0)); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3809 | count &= DWC3_GEVNTCOUNT_MASK; |
| 3810 | if (!count) |
| 3811 | return IRQ_NONE; |
| 3812 | |
Felipe Balbi | b15a762 | 2011-06-30 16:57:15 +0300 | [diff] [blame] | 3813 | evt->count = count; |
| 3814 | evt->flags |= DWC3_EVENT_PENDING; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3815 | |
Felipe Balbi | e8adfc3 | 2013-06-12 21:11:14 +0300 | [diff] [blame] | 3816 | /* Mask interrupt */ |
Felipe Balbi | 660e9bd | 2016-03-30 09:26:24 +0300 | [diff] [blame] | 3817 | reg = dwc3_readl(dwc->regs, DWC3_GEVNTSIZ(0)); |
Felipe Balbi | e8adfc3 | 2013-06-12 21:11:14 +0300 | [diff] [blame] | 3818 | reg |= DWC3_GEVNTSIZ_INTMASK; |
Felipe Balbi | 660e9bd | 2016-03-30 09:26:24 +0300 | [diff] [blame] | 3819 | dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0), reg); |
Felipe Balbi | e8adfc3 | 2013-06-12 21:11:14 +0300 | [diff] [blame] | 3820 | |
John Youn | ebbb2d5 | 2016-11-15 13:07:02 +0200 | [diff] [blame] | 3821 | amount = min(count, evt->length - evt->lpos); |
| 3822 | memcpy(evt->cache + evt->lpos, evt->buf + evt->lpos, amount); |
| 3823 | |
| 3824 | if (amount < count) |
| 3825 | memcpy(evt->cache, evt->buf, count - amount); |
| 3826 | |
John Youn | 65aca32 | 2016-11-15 13:08:59 +0200 | [diff] [blame] | 3827 | dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), count); |
| 3828 | |
Felipe Balbi | b15a762 | 2011-06-30 16:57:15 +0300 | [diff] [blame] | 3829 | return IRQ_WAKE_THREAD; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3830 | } |
| 3831 | |
Felipe Balbi | dea520a | 2016-03-30 09:39:34 +0300 | [diff] [blame] | 3832 | static irqreturn_t dwc3_interrupt(int irq, void *_evt) |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3833 | { |
Felipe Balbi | dea520a | 2016-03-30 09:39:34 +0300 | [diff] [blame] | 3834 | struct dwc3_event_buffer *evt = _evt; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3835 | |
Felipe Balbi | dea520a | 2016-03-30 09:39:34 +0300 | [diff] [blame] | 3836 | return dwc3_check_event_buf(evt); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3837 | } |
| 3838 | |
Felipe Balbi | 6db3812 | 2016-10-03 11:27:01 +0300 | [diff] [blame] | 3839 | static int dwc3_gadget_get_irq(struct dwc3 *dwc) |
| 3840 | { |
| 3841 | struct platform_device *dwc3_pdev = to_platform_device(dwc->dev); |
| 3842 | int irq; |
| 3843 | |
Hans de Goede | f146b40 | 2019-10-05 23:04:48 +0200 | [diff] [blame] | 3844 | irq = platform_get_irq_byname_optional(dwc3_pdev, "peripheral"); |
Felipe Balbi | 6db3812 | 2016-10-03 11:27:01 +0300 | [diff] [blame] | 3845 | if (irq > 0) |
| 3846 | goto out; |
| 3847 | |
| 3848 | if (irq == -EPROBE_DEFER) |
| 3849 | goto out; |
| 3850 | |
Hans de Goede | f146b40 | 2019-10-05 23:04:48 +0200 | [diff] [blame] | 3851 | irq = platform_get_irq_byname_optional(dwc3_pdev, "dwc_usb3"); |
Felipe Balbi | 6db3812 | 2016-10-03 11:27:01 +0300 | [diff] [blame] | 3852 | if (irq > 0) |
| 3853 | goto out; |
| 3854 | |
| 3855 | if (irq == -EPROBE_DEFER) |
| 3856 | goto out; |
| 3857 | |
| 3858 | irq = platform_get_irq(dwc3_pdev, 0); |
| 3859 | if (irq > 0) |
| 3860 | goto out; |
| 3861 | |
Felipe Balbi | 6db3812 | 2016-10-03 11:27:01 +0300 | [diff] [blame] | 3862 | if (!irq) |
| 3863 | irq = -EINVAL; |
| 3864 | |
| 3865 | out: |
| 3866 | return irq; |
| 3867 | } |
| 3868 | |
Peter Chen | e81a701 | 2020-08-21 10:55:48 +0800 | [diff] [blame] | 3869 | static void dwc_gadget_release(struct device *dev) |
| 3870 | { |
| 3871 | struct usb_gadget *gadget = container_of(dev, struct usb_gadget, dev); |
| 3872 | |
| 3873 | kfree(gadget); |
| 3874 | } |
| 3875 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3876 | /** |
Felipe Balbi | bfad65e | 2017-04-19 14:59:27 +0300 | [diff] [blame] | 3877 | * dwc3_gadget_init - initializes gadget related registers |
Paul Zimmerman | 1d04679 | 2012-02-15 18:56:56 -0800 | [diff] [blame] | 3878 | * @dwc: pointer to our controller context structure |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3879 | * |
| 3880 | * Returns 0 on success otherwise negative errno. |
| 3881 | */ |
Bill Pemberton | 41ac7b3 | 2012-11-19 13:21:48 -0500 | [diff] [blame] | 3882 | int dwc3_gadget_init(struct dwc3 *dwc) |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3883 | { |
Felipe Balbi | 6db3812 | 2016-10-03 11:27:01 +0300 | [diff] [blame] | 3884 | int ret; |
| 3885 | int irq; |
Peter Chen | e81a701 | 2020-08-21 10:55:48 +0800 | [diff] [blame] | 3886 | struct device *dev; |
Roger Quadros | 9522def | 2016-06-10 14:48:38 +0300 | [diff] [blame] | 3887 | |
Felipe Balbi | 6db3812 | 2016-10-03 11:27:01 +0300 | [diff] [blame] | 3888 | irq = dwc3_gadget_get_irq(dwc); |
| 3889 | if (irq < 0) { |
| 3890 | ret = irq; |
| 3891 | goto err0; |
Roger Quadros | 9522def | 2016-06-10 14:48:38 +0300 | [diff] [blame] | 3892 | } |
| 3893 | |
| 3894 | dwc->irq_gadget = irq; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3895 | |
Arnd Bergmann | d64ff40 | 2016-11-17 17:13:47 +0530 | [diff] [blame] | 3896 | dwc->ep0_trb = dma_alloc_coherent(dwc->sysdev, |
| 3897 | sizeof(*dwc->ep0_trb) * 2, |
| 3898 | &dwc->ep0_trb_addr, GFP_KERNEL); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3899 | if (!dwc->ep0_trb) { |
| 3900 | dev_err(dwc->dev, "failed to allocate ep0 trb\n"); |
| 3901 | ret = -ENOMEM; |
Felipe Balbi | 7d5e650 | 2017-04-07 13:34:21 +0300 | [diff] [blame] | 3902 | goto err0; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3903 | } |
| 3904 | |
Felipe Balbi | 4199c5f | 2017-04-07 14:09:13 +0300 | [diff] [blame] | 3905 | dwc->setup_buf = kzalloc(DWC3_EP0_SETUP_SIZE, GFP_KERNEL); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3906 | if (!dwc->setup_buf) { |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3907 | ret = -ENOMEM; |
Felipe Balbi | 7d5e650 | 2017-04-07 13:34:21 +0300 | [diff] [blame] | 3908 | goto err1; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3909 | } |
| 3910 | |
Felipe Balbi | 905dc04 | 2017-01-05 14:46:52 +0200 | [diff] [blame] | 3911 | dwc->bounce = dma_alloc_coherent(dwc->sysdev, DWC3_BOUNCE_SIZE, |
| 3912 | &dwc->bounce_addr, GFP_KERNEL); |
| 3913 | if (!dwc->bounce) { |
| 3914 | ret = -ENOMEM; |
Felipe Balbi | d6e5a54 | 2017-04-07 16:34:38 +0300 | [diff] [blame] | 3915 | goto err2; |
Felipe Balbi | 905dc04 | 2017-01-05 14:46:52 +0200 | [diff] [blame] | 3916 | } |
| 3917 | |
Baolin Wang | bb01473 | 2016-10-14 17:11:33 +0800 | [diff] [blame] | 3918 | init_completion(&dwc->ep0_in_setup); |
Peter Chen | e81a701 | 2020-08-21 10:55:48 +0800 | [diff] [blame] | 3919 | dwc->gadget = kzalloc(sizeof(struct usb_gadget), GFP_KERNEL); |
| 3920 | if (!dwc->gadget) { |
| 3921 | ret = -ENOMEM; |
| 3922 | goto err3; |
| 3923 | } |
Baolin Wang | bb01473 | 2016-10-14 17:11:33 +0800 | [diff] [blame] | 3924 | |
Peter Chen | e81a701 | 2020-08-21 10:55:48 +0800 | [diff] [blame] | 3925 | |
| 3926 | usb_initialize_gadget(dwc->dev, dwc->gadget, dwc_gadget_release); |
| 3927 | dev = &dwc->gadget->dev; |
| 3928 | dev->platform_data = dwc; |
| 3929 | dwc->gadget->ops = &dwc3_gadget_ops; |
| 3930 | dwc->gadget->speed = USB_SPEED_UNKNOWN; |
| 3931 | dwc->gadget->sg_supported = true; |
| 3932 | dwc->gadget->name = "dwc3-gadget"; |
Thinh Nguyen | 8f7cdbb | 2021-04-13 19:13:18 -0700 | [diff] [blame] | 3933 | dwc->gadget->lpm_capable = !dwc->usb2_gadget_lpm_disable; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3934 | |
| 3935 | /* |
Ben McCauley | b9e51b2 | 2015-11-16 10:47:24 -0600 | [diff] [blame] | 3936 | * FIXME We might be setting max_speed to <SUPER, however versions |
| 3937 | * <2.20a of dwc3 have an issue with metastability (documented |
| 3938 | * elsewhere in this driver) which tells us we can't set max speed to |
| 3939 | * anything lower than SUPER. |
| 3940 | * |
| 3941 | * Because gadget.max_speed is only used by composite.c and function |
| 3942 | * drivers (i.e. it won't go into dwc3's registers) we are allowing this |
| 3943 | * to happen so we avoid sending SuperSpeed Capability descriptor |
| 3944 | * together with our BOS descriptor as that could confuse host into |
| 3945 | * thinking we can handle super speed. |
| 3946 | * |
| 3947 | * Note that, in fact, we won't even support GetBOS requests when speed |
| 3948 | * is less than super speed because we don't have means, yet, to tell |
| 3949 | * composite.c that we are USB 2.0 + LPM ECN. |
| 3950 | */ |
Thinh Nguyen | 9af21dd | 2020-04-11 19:20:01 -0700 | [diff] [blame] | 3951 | if (DWC3_VER_IS_PRIOR(DWC3, 220A) && |
Roger Quadros | 42bf02e | 2017-10-31 15:11:55 +0200 | [diff] [blame] | 3952 | !dwc->dis_metastability_quirk) |
Felipe Balbi | 5eb30ce | 2016-11-03 14:07:51 +0200 | [diff] [blame] | 3953 | dev_info(dwc->dev, "changing max_speed on rev %08x\n", |
Ben McCauley | b9e51b2 | 2015-11-16 10:47:24 -0600 | [diff] [blame] | 3954 | dwc->revision); |
| 3955 | |
Peter Chen | e81a701 | 2020-08-21 10:55:48 +0800 | [diff] [blame] | 3956 | dwc->gadget->max_speed = dwc->maximum_speed; |
Ben McCauley | b9e51b2 | 2015-11-16 10:47:24 -0600 | [diff] [blame] | 3957 | |
| 3958 | /* |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3959 | * REVISIT: Here we should clear all pending IRQs to be |
| 3960 | * sure we're starting from a well known location. |
| 3961 | */ |
| 3962 | |
Bryan O'Donoghue | f3bcfc7 | 2017-01-31 20:58:11 +0000 | [diff] [blame] | 3963 | ret = dwc3_gadget_init_endpoints(dwc, dwc->num_eps); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3964 | if (ret) |
Felipe Balbi | d6e5a54 | 2017-04-07 16:34:38 +0300 | [diff] [blame] | 3965 | goto err4; |
Peter Chen | e81a701 | 2020-08-21 10:55:48 +0800 | [diff] [blame] | 3966 | |
| 3967 | ret = usb_add_gadget(dwc->gadget); |
| 3968 | if (ret) { |
| 3969 | dev_err(dwc->dev, "failed to add gadget\n"); |
| 3970 | goto err5; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3971 | } |
| 3972 | |
Peter Chen | e81a701 | 2020-08-21 10:55:48 +0800 | [diff] [blame] | 3973 | dwc3_gadget_set_speed(dwc->gadget, dwc->maximum_speed); |
Roger Quadros | 169e3b6 | 2019-01-10 17:04:28 +0200 | [diff] [blame] | 3974 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3975 | return 0; |
Felipe Balbi | 4199c5f | 2017-04-07 14:09:13 +0300 | [diff] [blame] | 3976 | |
Peter Chen | e81a701 | 2020-08-21 10:55:48 +0800 | [diff] [blame] | 3977 | err5: |
Felipe Balbi | d6e5a54 | 2017-04-07 16:34:38 +0300 | [diff] [blame] | 3978 | dwc3_gadget_free_endpoints(dwc); |
Peter Chen | e81a701 | 2020-08-21 10:55:48 +0800 | [diff] [blame] | 3979 | err4: |
| 3980 | usb_put_gadget(dwc->gadget); |
Jack Pham | 851dee5 | 2021-05-28 09:04:05 -0700 | [diff] [blame] | 3981 | dwc->gadget = NULL; |
Felipe Balbi | 7d5e650 | 2017-04-07 13:34:21 +0300 | [diff] [blame] | 3982 | err3: |
Felipe Balbi | d6e5a54 | 2017-04-07 16:34:38 +0300 | [diff] [blame] | 3983 | dma_free_coherent(dwc->sysdev, DWC3_BOUNCE_SIZE, dwc->bounce, |
| 3984 | dwc->bounce_addr); |
Felipe Balbi | 5812b1c | 2011-08-27 22:07:53 +0300 | [diff] [blame] | 3985 | |
Felipe Balbi | 7d5e650 | 2017-04-07 13:34:21 +0300 | [diff] [blame] | 3986 | err2: |
Felipe Balbi | 0fc9a1b | 2011-12-19 11:32:34 +0200 | [diff] [blame] | 3987 | kfree(dwc->setup_buf); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3988 | |
Felipe Balbi | 7d5e650 | 2017-04-07 13:34:21 +0300 | [diff] [blame] | 3989 | err1: |
Arnd Bergmann | d64ff40 | 2016-11-17 17:13:47 +0530 | [diff] [blame] | 3990 | dma_free_coherent(dwc->sysdev, sizeof(*dwc->ep0_trb) * 2, |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3991 | dwc->ep0_trb, dwc->ep0_trb_addr); |
| 3992 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3993 | err0: |
| 3994 | return ret; |
| 3995 | } |
| 3996 | |
Felipe Balbi | 7415f17 | 2012-04-30 14:56:33 +0300 | [diff] [blame] | 3997 | /* -------------------------------------------------------------------------- */ |
| 3998 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3999 | void dwc3_gadget_exit(struct dwc3 *dwc) |
| 4000 | { |
Jack Pham | 851dee5 | 2021-05-28 09:04:05 -0700 | [diff] [blame] | 4001 | if (!dwc->gadget) |
| 4002 | return; |
| 4003 | |
Jack Pham | 1ea7750 | 2021-05-01 02:35:58 -0700 | [diff] [blame] | 4004 | usb_del_gadget(dwc->gadget); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 4005 | dwc3_gadget_free_endpoints(dwc); |
Jack Pham | 1ea7750 | 2021-05-01 02:35:58 -0700 | [diff] [blame] | 4006 | usb_put_gadget(dwc->gadget); |
Felipe Balbi | 905dc04 | 2017-01-05 14:46:52 +0200 | [diff] [blame] | 4007 | dma_free_coherent(dwc->sysdev, DWC3_BOUNCE_SIZE, dwc->bounce, |
Felipe Balbi | d6e5a54 | 2017-04-07 16:34:38 +0300 | [diff] [blame] | 4008 | dwc->bounce_addr); |
Felipe Balbi | 0fc9a1b | 2011-12-19 11:32:34 +0200 | [diff] [blame] | 4009 | kfree(dwc->setup_buf); |
Arnd Bergmann | d64ff40 | 2016-11-17 17:13:47 +0530 | [diff] [blame] | 4010 | dma_free_coherent(dwc->sysdev, sizeof(*dwc->ep0_trb) * 2, |
Felipe Balbi | d6e5a54 | 2017-04-07 16:34:38 +0300 | [diff] [blame] | 4011 | dwc->ep0_trb, dwc->ep0_trb_addr); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 4012 | } |
Felipe Balbi | 7415f17 | 2012-04-30 14:56:33 +0300 | [diff] [blame] | 4013 | |
Felipe Balbi | 0b0231a | 2014-10-07 10:19:23 -0500 | [diff] [blame] | 4014 | int dwc3_gadget_suspend(struct dwc3 *dwc) |
Felipe Balbi | 7415f17 | 2012-04-30 14:56:33 +0300 | [diff] [blame] | 4015 | { |
Roger Quadros | 9772b47 | 2016-04-12 11:33:29 +0300 | [diff] [blame] | 4016 | if (!dwc->gadget_driver) |
| 4017 | return 0; |
| 4018 | |
Roger Quadros | 1551e35 | 2017-02-15 14:16:26 +0200 | [diff] [blame] | 4019 | dwc3_gadget_run_stop(dwc, false, false); |
Felipe Balbi | 9f8a67b | 2016-05-04 15:50:27 +0300 | [diff] [blame] | 4020 | dwc3_disconnect_gadget(dwc); |
| 4021 | __dwc3_gadget_stop(dwc); |
Felipe Balbi | 7415f17 | 2012-04-30 14:56:33 +0300 | [diff] [blame] | 4022 | |
| 4023 | return 0; |
| 4024 | } |
| 4025 | |
| 4026 | int dwc3_gadget_resume(struct dwc3 *dwc) |
| 4027 | { |
Felipe Balbi | 7415f17 | 2012-04-30 14:56:33 +0300 | [diff] [blame] | 4028 | int ret; |
| 4029 | |
Roger Quadros | 9772b47 | 2016-04-12 11:33:29 +0300 | [diff] [blame] | 4030 | if (!dwc->gadget_driver) |
| 4031 | return 0; |
| 4032 | |
Felipe Balbi | 9f8a67b | 2016-05-04 15:50:27 +0300 | [diff] [blame] | 4033 | ret = __dwc3_gadget_start(dwc); |
| 4034 | if (ret < 0) |
Felipe Balbi | 7415f17 | 2012-04-30 14:56:33 +0300 | [diff] [blame] | 4035 | goto err0; |
| 4036 | |
Felipe Balbi | 9f8a67b | 2016-05-04 15:50:27 +0300 | [diff] [blame] | 4037 | ret = dwc3_gadget_run_stop(dwc, true, false); |
| 4038 | if (ret < 0) |
Felipe Balbi | 7415f17 | 2012-04-30 14:56:33 +0300 | [diff] [blame] | 4039 | goto err1; |
| 4040 | |
Felipe Balbi | 7415f17 | 2012-04-30 14:56:33 +0300 | [diff] [blame] | 4041 | return 0; |
| 4042 | |
| 4043 | err1: |
Felipe Balbi | 9f8a67b | 2016-05-04 15:50:27 +0300 | [diff] [blame] | 4044 | __dwc3_gadget_stop(dwc); |
Felipe Balbi | 7415f17 | 2012-04-30 14:56:33 +0300 | [diff] [blame] | 4045 | |
| 4046 | err0: |
| 4047 | return ret; |
| 4048 | } |
Felipe Balbi | fc8bb91 | 2016-05-16 13:14:48 +0300 | [diff] [blame] | 4049 | |
| 4050 | void dwc3_gadget_process_pending_events(struct dwc3 *dwc) |
| 4051 | { |
| 4052 | if (dwc->pending_events) { |
| 4053 | dwc3_interrupt(dwc->irq_gadget, dwc->ev_buf); |
| 4054 | dwc->pending_events = false; |
| 4055 | enable_irq(dwc->irq_gadget); |
| 4056 | } |
| 4057 | } |