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