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