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