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