Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1 | /** |
| 2 | * gadget.c - DesignWare USB3 DRD Controller Gadget Framework Link |
| 3 | * |
| 4 | * Copyright (C) 2010-2011 Texas Instruments Incorporated - http://www.ti.com |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 5 | * |
| 6 | * Authors: Felipe Balbi <balbi@ti.com>, |
| 7 | * Sebastian Andrzej Siewior <bigeasy@linutronix.de> |
| 8 | * |
Felipe Balbi | 5945f78 | 2013-06-30 14:15:11 +0300 | [diff] [blame] | 9 | * This program is free software: you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License version 2 of |
| 11 | * the License as published by the Free Software Foundation. |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 12 | * |
Felipe Balbi | 5945f78 | 2013-06-30 14:15:11 +0300 | [diff] [blame] | 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 17 | */ |
| 18 | |
| 19 | #include <linux/kernel.h> |
| 20 | #include <linux/delay.h> |
| 21 | #include <linux/slab.h> |
| 22 | #include <linux/spinlock.h> |
| 23 | #include <linux/platform_device.h> |
| 24 | #include <linux/pm_runtime.h> |
Mayank Rana | a99689a | 2016-08-10 17:39:47 -0700 | [diff] [blame] | 25 | #include <linux/ratelimit.h> |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 26 | #include <linux/interrupt.h> |
| 27 | #include <linux/io.h> |
| 28 | #include <linux/list.h> |
| 29 | #include <linux/dma-mapping.h> |
| 30 | |
| 31 | #include <linux/usb/ch9.h> |
Mayank Rana | a99689a | 2016-08-10 17:39:47 -0700 | [diff] [blame] | 32 | #include <linux/usb/composite.h> |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 33 | #include <linux/usb/gadget.h> |
| 34 | |
Felipe Balbi | 80977dc | 2014-08-19 16:37:22 -0500 | [diff] [blame] | 35 | #include "debug.h" |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 36 | #include "core.h" |
| 37 | #include "gadget.h" |
| 38 | #include "io.h" |
| 39 | |
Mayank Rana | a99689a | 2016-08-10 17:39:47 -0700 | [diff] [blame] | 40 | static void dwc3_gadget_wakeup_interrupt(struct dwc3 *dwc, bool remote_wakeup); |
| 41 | static int dwc3_gadget_wakeup_int(struct dwc3 *dwc); |
Felipe Balbi | 04a9bfc | 2012-01-02 18:25:43 +0200 | [diff] [blame] | 42 | /** |
| 43 | * dwc3_gadget_set_test_mode - Enables USB2 Test Modes |
| 44 | * @dwc: pointer to our context structure |
| 45 | * @mode: the mode to set (J, K SE0 NAK, Force Enable) |
| 46 | * |
| 47 | * Caller should take care of locking. This function will |
| 48 | * return 0 on success or -EINVAL if wrong Test Selector |
| 49 | * is passed |
| 50 | */ |
| 51 | int dwc3_gadget_set_test_mode(struct dwc3 *dwc, int mode) |
| 52 | { |
| 53 | u32 reg; |
| 54 | |
| 55 | reg = dwc3_readl(dwc->regs, DWC3_DCTL); |
| 56 | reg &= ~DWC3_DCTL_TSTCTRL_MASK; |
| 57 | |
| 58 | switch (mode) { |
| 59 | case TEST_J: |
| 60 | case TEST_K: |
| 61 | case TEST_SE0_NAK: |
| 62 | case TEST_PACKET: |
| 63 | case TEST_FORCE_EN: |
| 64 | reg |= mode << 1; |
| 65 | break; |
| 66 | default: |
| 67 | return -EINVAL; |
| 68 | } |
| 69 | |
| 70 | dwc3_writel(dwc->regs, DWC3_DCTL, reg); |
| 71 | |
| 72 | return 0; |
| 73 | } |
| 74 | |
Felipe Balbi | 8598bde | 2012-01-02 18:55:57 +0200 | [diff] [blame] | 75 | /** |
Paul Zimmerman | 911f1f8 | 2012-04-27 13:35:15 +0300 | [diff] [blame] | 76 | * dwc3_gadget_get_link_state - Gets current state of USB Link |
| 77 | * @dwc: pointer to our context structure |
| 78 | * |
| 79 | * Caller should take care of locking. This function will |
| 80 | * return the link state on success (>= 0) or -ETIMEDOUT. |
| 81 | */ |
| 82 | int dwc3_gadget_get_link_state(struct dwc3 *dwc) |
| 83 | { |
| 84 | u32 reg; |
| 85 | |
| 86 | reg = dwc3_readl(dwc->regs, DWC3_DSTS); |
| 87 | |
| 88 | return DWC3_DSTS_USBLNKST(reg); |
| 89 | } |
| 90 | |
| 91 | /** |
Felipe Balbi | 8598bde | 2012-01-02 18:55:57 +0200 | [diff] [blame] | 92 | * dwc3_gadget_set_link_state - Sets USB Link to a particular State |
| 93 | * @dwc: pointer to our context structure |
| 94 | * @state: the state to put link into |
| 95 | * |
| 96 | * Caller should take care of locking. This function will |
Paul Zimmerman | aee63e3 | 2012-02-24 17:32:15 -0800 | [diff] [blame] | 97 | * return 0 on success or -ETIMEDOUT. |
Felipe Balbi | 8598bde | 2012-01-02 18:55:57 +0200 | [diff] [blame] | 98 | */ |
| 99 | int dwc3_gadget_set_link_state(struct dwc3 *dwc, enum dwc3_link_state state) |
| 100 | { |
Paul Zimmerman | aee63e3 | 2012-02-24 17:32:15 -0800 | [diff] [blame] | 101 | int retries = 10000; |
Felipe Balbi | 8598bde | 2012-01-02 18:55:57 +0200 | [diff] [blame] | 102 | u32 reg; |
| 103 | |
Paul Zimmerman | 802fde9 | 2012-04-27 13:10:52 +0300 | [diff] [blame] | 104 | /* |
| 105 | * Wait until device controller is ready. Only applies to 1.94a and |
| 106 | * later RTL. |
| 107 | */ |
| 108 | if (dwc->revision >= DWC3_REVISION_194A) { |
| 109 | while (--retries) { |
| 110 | reg = dwc3_readl(dwc->regs, DWC3_DSTS); |
| 111 | if (reg & DWC3_DSTS_DCNRD) |
| 112 | udelay(5); |
| 113 | else |
| 114 | break; |
| 115 | } |
| 116 | |
| 117 | if (retries <= 0) |
| 118 | return -ETIMEDOUT; |
| 119 | } |
| 120 | |
Felipe Balbi | 8598bde | 2012-01-02 18:55:57 +0200 | [diff] [blame] | 121 | reg = dwc3_readl(dwc->regs, DWC3_DCTL); |
| 122 | reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK; |
| 123 | |
| 124 | /* set requested state */ |
| 125 | reg |= DWC3_DCTL_ULSTCHNGREQ(state); |
| 126 | dwc3_writel(dwc->regs, DWC3_DCTL, reg); |
| 127 | |
Paul Zimmerman | 802fde9 | 2012-04-27 13:10:52 +0300 | [diff] [blame] | 128 | /* |
| 129 | * The following code is racy when called from dwc3_gadget_wakeup, |
| 130 | * and is not needed, at least on newer versions |
| 131 | */ |
| 132 | if (dwc->revision >= DWC3_REVISION_194A) |
| 133 | return 0; |
| 134 | |
Felipe Balbi | 8598bde | 2012-01-02 18:55:57 +0200 | [diff] [blame] | 135 | /* wait for a change in DSTS */ |
Paul Zimmerman | aed430e | 2012-04-27 12:52:01 +0300 | [diff] [blame] | 136 | retries = 10000; |
Felipe Balbi | 8598bde | 2012-01-02 18:55:57 +0200 | [diff] [blame] | 137 | while (--retries) { |
| 138 | reg = dwc3_readl(dwc->regs, DWC3_DSTS); |
| 139 | |
Felipe Balbi | 8598bde | 2012-01-02 18:55:57 +0200 | [diff] [blame] | 140 | if (DWC3_DSTS_USBLNKST(reg) == state) |
| 141 | return 0; |
| 142 | |
Paul Zimmerman | aee63e3 | 2012-02-24 17:32:15 -0800 | [diff] [blame] | 143 | udelay(5); |
Felipe Balbi | 8598bde | 2012-01-02 18:55:57 +0200 | [diff] [blame] | 144 | } |
| 145 | |
Felipe Balbi | 7381528 | 2015-01-27 13:48:14 -0600 | [diff] [blame] | 146 | dwc3_trace(trace_dwc3_gadget, |
| 147 | "link state change request timed out"); |
Felipe Balbi | 8598bde | 2012-01-02 18:55:57 +0200 | [diff] [blame] | 148 | |
| 149 | return -ETIMEDOUT; |
| 150 | } |
| 151 | |
John Youn | dca0119 | 2016-05-19 17:26:05 -0700 | [diff] [blame] | 152 | /** |
| 153 | * dwc3_ep_inc_trb() - Increment a TRB index. |
| 154 | * @index - Pointer to the TRB index to increment. |
| 155 | * |
| 156 | * The index should never point to the link TRB. After incrementing, |
| 157 | * if it is point to the link TRB, wrap around to the beginning. The |
| 158 | * link TRB is always at the last TRB entry. |
| 159 | */ |
| 160 | static void dwc3_ep_inc_trb(u8 *index) |
| 161 | { |
| 162 | (*index)++; |
| 163 | if (*index == (DWC3_TRB_NUM - 1)) |
| 164 | *index = 0; |
| 165 | } |
| 166 | |
Felipe Balbi | ef966b9 | 2016-04-05 13:09:51 +0300 | [diff] [blame] | 167 | static void dwc3_ep_inc_enq(struct dwc3_ep *dep) |
Felipe Balbi | 457e84b | 2012-01-18 18:04:09 +0200 | [diff] [blame] | 168 | { |
John Youn | dca0119 | 2016-05-19 17:26:05 -0700 | [diff] [blame] | 169 | dwc3_ep_inc_trb(&dep->trb_enqueue); |
Felipe Balbi | ef966b9 | 2016-04-05 13:09:51 +0300 | [diff] [blame] | 170 | } |
Felipe Balbi | 457e84b | 2012-01-18 18:04:09 +0200 | [diff] [blame] | 171 | |
Felipe Balbi | ef966b9 | 2016-04-05 13:09:51 +0300 | [diff] [blame] | 172 | static void dwc3_ep_inc_deq(struct dwc3_ep *dep) |
| 173 | { |
John Youn | dca0119 | 2016-05-19 17:26:05 -0700 | [diff] [blame] | 174 | dwc3_ep_inc_trb(&dep->trb_dequeue); |
Felipe Balbi | 457e84b | 2012-01-18 18:04:09 +0200 | [diff] [blame] | 175 | } |
| 176 | |
Mayank Rana | a8e4de6 | 2016-12-13 17:11:15 -0800 | [diff] [blame^] | 177 | /* |
| 178 | * dwc3_gadget_resize_tx_fifos - reallocate fifo spaces for current use-case |
| 179 | * @dwc: pointer to our context structure |
| 180 | * |
| 181 | * This function will a best effort FIFO allocation in order |
| 182 | * to improve FIFO usage and throughput, while still allowing |
| 183 | * us to enable as many endpoints as possible. |
| 184 | * |
| 185 | * Keep in mind that this operation will be highly dependent |
| 186 | * on the configured size for RAM1 - which contains TxFifo -, |
| 187 | * the amount of endpoints enabled on coreConsultant tool, and |
| 188 | * the width of the Master Bus. |
| 189 | * |
| 190 | * In the ideal world, we would always be able to satisfy the |
| 191 | * following equation: |
| 192 | * |
| 193 | * ((512 + 2 * MDWIDTH-Bytes) + (Number of IN Endpoints - 1) * \ |
| 194 | * (3 * (1024 + MDWIDTH-Bytes) + MDWIDTH-Bytes)) / MDWIDTH-Bytes |
| 195 | * |
| 196 | * Unfortunately, due to many variables that's not always the case. |
| 197 | */ |
| 198 | int dwc3_gadget_resize_tx_fifos(struct dwc3 *dwc) |
| 199 | { |
| 200 | int last_fifo_depth = 0; |
| 201 | int ram1_depth; |
| 202 | int fifo_size; |
| 203 | int mdwidth; |
| 204 | int num; |
| 205 | |
| 206 | if (!dwc->needs_fifo_resize) |
| 207 | return 0; |
| 208 | |
| 209 | ram1_depth = DWC3_RAM1_DEPTH(dwc->hwparams.hwparams7); |
| 210 | mdwidth = DWC3_MDWIDTH(dwc->hwparams.hwparams0); |
| 211 | |
| 212 | /* MDWIDTH is represented in bits, we need it in bytes */ |
| 213 | mdwidth >>= 3; |
| 214 | |
| 215 | /* |
| 216 | * FIXME For now we will only allocate 1 wMaxPacketSize space |
| 217 | * for each enabled endpoint, later patches will come to |
| 218 | * improve this algorithm so that we better use the internal |
| 219 | * FIFO space |
| 220 | */ |
| 221 | for (num = 0; num < dwc->num_in_eps; num++) { |
| 222 | /* bit0 indicates direction; 1 means IN ep */ |
| 223 | struct dwc3_ep *dep = dwc->eps[(num << 1) | 1]; |
| 224 | int mult = 1; |
| 225 | int tmp; |
| 226 | |
| 227 | if (!(dep->flags & DWC3_EP_ENABLED)) |
| 228 | continue; |
| 229 | |
| 230 | if (usb_endpoint_xfer_bulk(dep->endpoint.desc) |
| 231 | || usb_endpoint_xfer_isoc(dep->endpoint.desc)) |
| 232 | mult = 3; |
| 233 | |
| 234 | /* |
| 235 | * REVISIT: the following assumes we will always have enough |
| 236 | * space available on the FIFO RAM for all possible use cases. |
| 237 | * Make sure that's true somehow and change FIFO allocation |
| 238 | * accordingly. |
| 239 | * |
| 240 | * If we have Bulk or Isochronous endpoints, we want |
| 241 | * them to be able to be very, very fast. So we're giving |
| 242 | * those endpoints a fifo_size which is enough for 3 full |
| 243 | * packets |
| 244 | */ |
| 245 | tmp = mult * (dep->endpoint.maxpacket + mdwidth); |
| 246 | tmp += mdwidth; |
| 247 | |
| 248 | fifo_size = DIV_ROUND_UP(tmp, mdwidth); |
| 249 | |
| 250 | fifo_size |= (last_fifo_depth << 16); |
| 251 | |
| 252 | dwc3_trace(trace_dwc3_gadget, "%s: Fifo Addr %04x Size %d", |
| 253 | dep->name, last_fifo_depth, fifo_size & 0xffff); |
| 254 | |
| 255 | dwc3_writel(dwc->regs, DWC3_GTXFIFOSIZ(num), fifo_size); |
| 256 | |
| 257 | last_fifo_depth += (fifo_size & 0xffff); |
| 258 | } |
| 259 | |
| 260 | return 0; |
| 261 | } |
| 262 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 263 | void dwc3_gadget_giveback(struct dwc3_ep *dep, struct dwc3_request *req, |
| 264 | int status) |
| 265 | { |
| 266 | struct dwc3 *dwc = dep->dwc; |
| 267 | |
Felipe Balbi | 737f1ae | 2016-08-11 12:24:27 +0300 | [diff] [blame] | 268 | req->started = false; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 269 | list_del(&req->list); |
Felipe Balbi | eeb720f | 2011-11-28 12:46:59 +0200 | [diff] [blame] | 270 | req->trb = NULL; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 271 | |
| 272 | if (req->request.status == -EINPROGRESS) |
| 273 | req->request.status = status; |
| 274 | |
Pratyush Anand | 0416e49 | 2012-08-10 13:42:16 +0530 | [diff] [blame] | 275 | if (dwc->ep0_bounced && dep->number == 0) |
| 276 | dwc->ep0_bounced = false; |
| 277 | else |
| 278 | usb_gadget_unmap_request(&dwc->gadget, &req->request, |
| 279 | req->direction); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 280 | |
Felipe Balbi | 2c4cbe6e5 | 2014-04-30 17:45:10 -0500 | [diff] [blame] | 281 | trace_dwc3_gadget_giveback(req); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 282 | |
| 283 | spin_unlock(&dwc->lock); |
Michal Sojka | 304f7e5 | 2014-09-24 22:43:19 +0200 | [diff] [blame] | 284 | usb_gadget_giveback_request(&dep->endpoint, &req->request); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 285 | spin_lock(&dwc->lock); |
Felipe Balbi | fc8bb91 | 2016-05-16 13:14:48 +0300 | [diff] [blame] | 286 | |
| 287 | if (dep->number > 1) |
| 288 | pm_runtime_put(dwc->dev); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 289 | } |
| 290 | |
Felipe Balbi | 3ece0ec | 2014-09-05 09:47:44 -0500 | [diff] [blame] | 291 | 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] | 292 | { |
| 293 | u32 timeout = 500; |
Felipe Balbi | 71f7e70 | 2016-05-23 14:16:19 +0300 | [diff] [blame] | 294 | int status = 0; |
Felipe Balbi | 0fe886c | 2016-05-23 14:06:07 +0300 | [diff] [blame] | 295 | int ret = 0; |
Felipe Balbi | b09bb64 | 2012-04-24 16:19:11 +0300 | [diff] [blame] | 296 | u32 reg; |
| 297 | |
| 298 | dwc3_writel(dwc->regs, DWC3_DGCMDPAR, param); |
| 299 | dwc3_writel(dwc->regs, DWC3_DGCMD, cmd | DWC3_DGCMD_CMDACT); |
| 300 | |
| 301 | do { |
| 302 | reg = dwc3_readl(dwc->regs, DWC3_DGCMD); |
| 303 | if (!(reg & DWC3_DGCMD_CMDACT)) { |
Felipe Balbi | 71f7e70 | 2016-05-23 14:16:19 +0300 | [diff] [blame] | 304 | status = DWC3_DGCMD_STATUS(reg); |
| 305 | if (status) |
Felipe Balbi | 0fe886c | 2016-05-23 14:06:07 +0300 | [diff] [blame] | 306 | ret = -EINVAL; |
| 307 | break; |
Felipe Balbi | b09bb64 | 2012-04-24 16:19:11 +0300 | [diff] [blame] | 308 | } |
Felipe Balbi | 0fe886c | 2016-05-23 14:06:07 +0300 | [diff] [blame] | 309 | } while (timeout--); |
| 310 | |
| 311 | if (!timeout) { |
Felipe Balbi | 0fe886c | 2016-05-23 14:06:07 +0300 | [diff] [blame] | 312 | ret = -ETIMEDOUT; |
Felipe Balbi | 71f7e70 | 2016-05-23 14:16:19 +0300 | [diff] [blame] | 313 | status = -ETIMEDOUT; |
Felipe Balbi | 0fe886c | 2016-05-23 14:06:07 +0300 | [diff] [blame] | 314 | } |
| 315 | |
Felipe Balbi | 71f7e70 | 2016-05-23 14:16:19 +0300 | [diff] [blame] | 316 | trace_dwc3_gadget_generic_cmd(cmd, param, status); |
| 317 | |
Felipe Balbi | 0fe886c | 2016-05-23 14:06:07 +0300 | [diff] [blame] | 318 | return ret; |
Felipe Balbi | b09bb64 | 2012-04-24 16:19:11 +0300 | [diff] [blame] | 319 | } |
| 320 | |
Felipe Balbi | c36d8e9 | 2016-04-04 12:46:33 +0300 | [diff] [blame] | 321 | static int __dwc3_gadget_wakeup(struct dwc3 *dwc); |
| 322 | |
Felipe Balbi | 2cd4718 | 2016-04-12 16:42:43 +0300 | [diff] [blame] | 323 | int dwc3_send_gadget_ep_cmd(struct dwc3_ep *dep, unsigned cmd, |
| 324 | struct dwc3_gadget_ep_cmd_params *params) |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 325 | { |
Felipe Balbi | 2cd4718 | 2016-04-12 16:42:43 +0300 | [diff] [blame] | 326 | struct dwc3 *dwc = dep->dwc; |
Hemant Kumar | 4387417 | 2016-08-25 16:17:48 -0700 | [diff] [blame] | 327 | u32 timeout = 3000; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 328 | u32 reg; |
| 329 | |
Felipe Balbi | 0933df1 | 2016-05-23 14:02:33 +0300 | [diff] [blame] | 330 | int cmd_status = 0; |
Felipe Balbi | 2b0f11d | 2016-04-04 09:19:17 +0300 | [diff] [blame] | 331 | int susphy = false; |
Felipe Balbi | c0ca324 | 2016-04-04 09:11:51 +0300 | [diff] [blame] | 332 | int ret = -EINVAL; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 333 | |
Felipe Balbi | 2b0f11d | 2016-04-04 09:19:17 +0300 | [diff] [blame] | 334 | /* |
| 335 | * Synopsys Databook 2.60a states, on section 6.3.2.5.[1-8], that if |
| 336 | * we're issuing an endpoint command, we must check if |
| 337 | * GUSB2PHYCFG.SUSPHY bit is set. If it is, then we need to clear it. |
| 338 | * |
| 339 | * We will also set SUSPHY bit to what it was before returning as stated |
| 340 | * by the same section on Synopsys databook. |
| 341 | */ |
Felipe Balbi | ab2a92e | 2016-05-17 14:55:58 +0300 | [diff] [blame] | 342 | if (dwc->gadget.speed <= USB_SPEED_HIGH) { |
| 343 | reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0)); |
| 344 | if (unlikely(reg & DWC3_GUSB2PHYCFG_SUSPHY)) { |
| 345 | susphy = true; |
| 346 | reg &= ~DWC3_GUSB2PHYCFG_SUSPHY; |
| 347 | dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg); |
| 348 | } |
Felipe Balbi | 2b0f11d | 2016-04-04 09:19:17 +0300 | [diff] [blame] | 349 | } |
| 350 | |
Felipe Balbi | c36d8e9 | 2016-04-04 12:46:33 +0300 | [diff] [blame] | 351 | if (cmd == DWC3_DEPCMD_STARTTRANSFER) { |
| 352 | int needs_wakeup; |
| 353 | |
| 354 | needs_wakeup = (dwc->link_state == DWC3_LINK_STATE_U1 || |
| 355 | dwc->link_state == DWC3_LINK_STATE_U2 || |
| 356 | dwc->link_state == DWC3_LINK_STATE_U3); |
| 357 | |
| 358 | if (unlikely(needs_wakeup)) { |
| 359 | ret = __dwc3_gadget_wakeup(dwc); |
| 360 | dev_WARN_ONCE(dwc->dev, ret, "wakeup failed --> %d\n", |
| 361 | ret); |
| 362 | } |
| 363 | } |
| 364 | |
Felipe Balbi | 2eb8801 | 2016-04-12 16:53:39 +0300 | [diff] [blame] | 365 | dwc3_writel(dep->regs, DWC3_DEPCMDPAR0, params->param0); |
| 366 | dwc3_writel(dep->regs, DWC3_DEPCMDPAR1, params->param1); |
| 367 | dwc3_writel(dep->regs, DWC3_DEPCMDPAR2, params->param2); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 368 | |
Felipe Balbi | 2eb8801 | 2016-04-12 16:53:39 +0300 | [diff] [blame] | 369 | dwc3_writel(dep->regs, DWC3_DEPCMD, cmd | DWC3_DEPCMD_CMDACT); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 370 | do { |
Felipe Balbi | 2eb8801 | 2016-04-12 16:53:39 +0300 | [diff] [blame] | 371 | reg = dwc3_readl(dep->regs, DWC3_DEPCMD); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 372 | if (!(reg & DWC3_DEPCMD_CMDACT)) { |
Felipe Balbi | 0933df1 | 2016-05-23 14:02:33 +0300 | [diff] [blame] | 373 | cmd_status = DWC3_DEPCMD_STATUS(reg); |
Konrad Leszczynski | 7b9cc7a | 2016-02-12 15:21:46 +0000 | [diff] [blame] | 374 | |
Konrad Leszczynski | 7b9cc7a | 2016-02-12 15:21:46 +0000 | [diff] [blame] | 375 | switch (cmd_status) { |
| 376 | case 0: |
| 377 | ret = 0; |
Felipe Balbi | c0ca324 | 2016-04-04 09:11:51 +0300 | [diff] [blame] | 378 | break; |
Konrad Leszczynski | 7b9cc7a | 2016-02-12 15:21:46 +0000 | [diff] [blame] | 379 | case DEPEVT_TRANSFER_NO_RESOURCE: |
Konrad Leszczynski | 7b9cc7a | 2016-02-12 15:21:46 +0000 | [diff] [blame] | 380 | ret = -EINVAL; |
| 381 | break; |
| 382 | case DEPEVT_TRANSFER_BUS_EXPIRY: |
| 383 | /* |
| 384 | * SW issues START TRANSFER command to |
| 385 | * isochronous ep with future frame interval. If |
| 386 | * future interval time has already passed when |
| 387 | * core receives the command, it will respond |
| 388 | * with an error status of 'Bus Expiry'. |
| 389 | * |
| 390 | * Instead of always returning -EINVAL, let's |
| 391 | * give a hint to the gadget driver that this is |
| 392 | * the case by returning -EAGAIN. |
| 393 | */ |
Konrad Leszczynski | 7b9cc7a | 2016-02-12 15:21:46 +0000 | [diff] [blame] | 394 | ret = -EAGAIN; |
| 395 | break; |
| 396 | default: |
| 397 | dev_WARN(dwc->dev, "UNKNOWN cmd status\n"); |
| 398 | } |
| 399 | |
Felipe Balbi | c0ca324 | 2016-04-04 09:11:51 +0300 | [diff] [blame] | 400 | break; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 401 | } |
Felipe Balbi | f6bb225 | 2016-05-23 13:53:34 +0300 | [diff] [blame] | 402 | } while (--timeout); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 403 | |
Felipe Balbi | f6bb225 | 2016-05-23 13:53:34 +0300 | [diff] [blame] | 404 | if (timeout == 0) { |
Felipe Balbi | f6bb225 | 2016-05-23 13:53:34 +0300 | [diff] [blame] | 405 | ret = -ETIMEDOUT; |
Mayank Rana | a99689a | 2016-08-10 17:39:47 -0700 | [diff] [blame] | 406 | dwc3_trace(trace_dwc3_gadget, "Command Timed Out"); |
| 407 | dev_err(dwc->dev, "%s command timeout for %s\n", |
| 408 | dwc3_gadget_ep_cmd_string(cmd), dep->name); |
Hemant Kumar | 4387417 | 2016-08-25 16:17:48 -0700 | [diff] [blame] | 409 | if (!(cmd & DWC3_DEPCMD_ENDTRANSFER)) { |
| 410 | dwc->ep_cmd_timeout_cnt++; |
| 411 | dwc3_notify_event(dwc, |
| 412 | DWC3_CONTROLLER_RESTART_USB_SESSION); |
| 413 | } |
Felipe Balbi | 0933df1 | 2016-05-23 14:02:33 +0300 | [diff] [blame] | 414 | cmd_status = -ETIMEDOUT; |
Felipe Balbi | f6bb225 | 2016-05-23 13:53:34 +0300 | [diff] [blame] | 415 | } |
Felipe Balbi | c0ca324 | 2016-04-04 09:11:51 +0300 | [diff] [blame] | 416 | |
Felipe Balbi | 0933df1 | 2016-05-23 14:02:33 +0300 | [diff] [blame] | 417 | trace_dwc3_gadget_ep_cmd(dep, cmd, params, cmd_status); |
| 418 | |
Felipe Balbi | 2b0f11d | 2016-04-04 09:19:17 +0300 | [diff] [blame] | 419 | if (unlikely(susphy)) { |
| 420 | reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0)); |
| 421 | reg |= DWC3_GUSB2PHYCFG_SUSPHY; |
| 422 | dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg); |
| 423 | } |
| 424 | |
Felipe Balbi | c0ca324 | 2016-04-04 09:11:51 +0300 | [diff] [blame] | 425 | return ret; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 426 | } |
| 427 | |
John Youn | 50c763f | 2016-05-31 17:49:56 -0700 | [diff] [blame] | 428 | static int dwc3_send_clear_stall_ep_cmd(struct dwc3_ep *dep) |
| 429 | { |
| 430 | struct dwc3 *dwc = dep->dwc; |
| 431 | struct dwc3_gadget_ep_cmd_params params; |
| 432 | u32 cmd = DWC3_DEPCMD_CLEARSTALL; |
| 433 | |
| 434 | /* |
| 435 | * As of core revision 2.60a the recommended programming model |
| 436 | * is to set the ClearPendIN bit when issuing a Clear Stall EP |
| 437 | * command for IN endpoints. This is to prevent an issue where |
| 438 | * some (non-compliant) hosts may not send ACK TPs for pending |
| 439 | * IN transfers due to a mishandled error condition. Synopsys |
| 440 | * STAR 9000614252. |
| 441 | */ |
Lu Baolu | 5e6c88d | 2016-09-09 12:51:27 +0800 | [diff] [blame] | 442 | if (dep->direction && (dwc->revision >= DWC3_REVISION_260A) && |
| 443 | (dwc->gadget.speed >= USB_SPEED_SUPER)) |
John Youn | 50c763f | 2016-05-31 17:49:56 -0700 | [diff] [blame] | 444 | cmd |= DWC3_DEPCMD_CLEARPENDIN; |
| 445 | |
| 446 | memset(¶ms, 0, sizeof(params)); |
| 447 | |
Felipe Balbi | 2cd4718 | 2016-04-12 16:42:43 +0300 | [diff] [blame] | 448 | return dwc3_send_gadget_ep_cmd(dep, cmd, ¶ms); |
John Youn | 50c763f | 2016-05-31 17:49:56 -0700 | [diff] [blame] | 449 | } |
| 450 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 451 | static int dwc3_alloc_trb_pool(struct dwc3_ep *dep) |
| 452 | { |
| 453 | struct dwc3 *dwc = dep->dwc; |
Mayank Rana | a99689a | 2016-08-10 17:39:47 -0700 | [diff] [blame] | 454 | u32 num_trbs = DWC3_TRB_NUM; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 455 | |
| 456 | if (dep->trb_pool) |
| 457 | return 0; |
| 458 | |
Mayank Rana | a99689a | 2016-08-10 17:39:47 -0700 | [diff] [blame] | 459 | dep->trb_pool = dma_zalloc_coherent(dwc->dev, |
| 460 | sizeof(struct dwc3_trb) * num_trbs, |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 461 | &dep->trb_pool_dma, GFP_KERNEL); |
| 462 | if (!dep->trb_pool) { |
| 463 | dev_err(dep->dwc->dev, "failed to allocate trb pool for %s\n", |
| 464 | dep->name); |
| 465 | return -ENOMEM; |
| 466 | } |
Mayank Rana | a99689a | 2016-08-10 17:39:47 -0700 | [diff] [blame] | 467 | dep->num_trbs = num_trbs; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 468 | |
| 469 | return 0; |
| 470 | } |
| 471 | |
| 472 | static void dwc3_free_trb_pool(struct dwc3_ep *dep) |
| 473 | { |
| 474 | struct dwc3 *dwc = dep->dwc; |
| 475 | |
Mayank Rana | a99689a | 2016-08-10 17:39:47 -0700 | [diff] [blame] | 476 | /* Freeing of GSI EP TRBs are handled by GSI EP ops. */ |
| 477 | if (dep->endpoint.ep_type == EP_TYPE_GSI) |
| 478 | return; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 479 | |
Mayank Rana | 4dd882c | 2016-10-05 09:43:05 -0700 | [diff] [blame] | 480 | /* |
| 481 | * Clean up ep ring to avoid getting xferInProgress due to stale trbs |
| 482 | * with HWO bit set from previous composition when update transfer cmd |
| 483 | * is issued. |
| 484 | */ |
| 485 | if (dep->number > 1 && dep->trb_pool && dep->trb_pool_dma) { |
| 486 | memset(&dep->trb_pool[0], 0, |
| 487 | sizeof(struct dwc3_trb) * dep->num_trbs); |
| 488 | dev_dbg(dwc->dev, "Clr_TRB ring of %s\n", dep->name); |
| 489 | |
Mayank Rana | a99689a | 2016-08-10 17:39:47 -0700 | [diff] [blame] | 490 | dma_free_coherent(dwc->dev, |
| 491 | sizeof(struct dwc3_trb) * DWC3_TRB_NUM, |
| 492 | dep->trb_pool, dep->trb_pool_dma); |
| 493 | dep->trb_pool = NULL; |
| 494 | dep->trb_pool_dma = 0; |
| 495 | } |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 496 | } |
| 497 | |
John Youn | c450960 | 2016-02-16 20:10:53 -0800 | [diff] [blame] | 498 | static int dwc3_gadget_set_xfer_resource(struct dwc3 *dwc, struct dwc3_ep *dep); |
| 499 | |
| 500 | /** |
| 501 | * dwc3_gadget_start_config - Configure EP resources |
| 502 | * @dwc: pointer to our controller context structure |
| 503 | * @dep: endpoint that is being enabled |
| 504 | * |
| 505 | * The assignment of transfer resources cannot perfectly follow the |
| 506 | * data book due to the fact that the controller driver does not have |
| 507 | * all knowledge of the configuration in advance. It is given this |
| 508 | * information piecemeal by the composite gadget framework after every |
| 509 | * SET_CONFIGURATION and SET_INTERFACE. Trying to follow the databook |
| 510 | * programming model in this scenario can cause errors. For two |
| 511 | * reasons: |
| 512 | * |
| 513 | * 1) The databook says to do DEPSTARTCFG for every SET_CONFIGURATION |
| 514 | * and SET_INTERFACE (8.1.5). This is incorrect in the scenario of |
| 515 | * multiple interfaces. |
| 516 | * |
| 517 | * 2) The databook does not mention doing more DEPXFERCFG for new |
| 518 | * endpoint on alt setting (8.1.6). |
| 519 | * |
| 520 | * The following simplified method is used instead: |
| 521 | * |
| 522 | * All hardware endpoints can be assigned a transfer resource and this |
| 523 | * setting will stay persistent until either a core reset or |
| 524 | * hibernation. So whenever we do a DEPSTARTCFG(0) we can go ahead and |
| 525 | * do DEPXFERCFG for every hardware endpoint as well. We are |
| 526 | * guaranteed that there are as many transfer resources as endpoints. |
| 527 | * |
| 528 | * This function is called for each endpoint when it is being enabled |
| 529 | * but is triggered only when called for EP0-out, which always happens |
| 530 | * first, and which should only happen in one of the above conditions. |
| 531 | */ |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 532 | static int dwc3_gadget_start_config(struct dwc3 *dwc, struct dwc3_ep *dep) |
| 533 | { |
| 534 | struct dwc3_gadget_ep_cmd_params params; |
| 535 | u32 cmd; |
John Youn | c450960 | 2016-02-16 20:10:53 -0800 | [diff] [blame] | 536 | int i; |
| 537 | int ret; |
| 538 | |
| 539 | if (dep->number) |
| 540 | return 0; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 541 | |
| 542 | memset(¶ms, 0x00, sizeof(params)); |
John Youn | c450960 | 2016-02-16 20:10:53 -0800 | [diff] [blame] | 543 | cmd = DWC3_DEPCMD_DEPSTARTCFG; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 544 | |
Felipe Balbi | 2cd4718 | 2016-04-12 16:42:43 +0300 | [diff] [blame] | 545 | ret = dwc3_send_gadget_ep_cmd(dep, cmd, ¶ms); |
John Youn | c450960 | 2016-02-16 20:10:53 -0800 | [diff] [blame] | 546 | if (ret) |
| 547 | return ret; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 548 | |
John Youn | c450960 | 2016-02-16 20:10:53 -0800 | [diff] [blame] | 549 | for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) { |
| 550 | struct dwc3_ep *dep = dwc->eps[i]; |
| 551 | |
| 552 | if (!dep) |
| 553 | continue; |
| 554 | |
| 555 | ret = dwc3_gadget_set_xfer_resource(dwc, dep); |
| 556 | if (ret) |
| 557 | return ret; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 558 | } |
| 559 | |
| 560 | return 0; |
| 561 | } |
| 562 | |
| 563 | static int dwc3_gadget_set_ep_config(struct dwc3 *dwc, struct dwc3_ep *dep, |
Felipe Balbi | c90bfae | 2011-11-29 13:11:21 +0200 | [diff] [blame] | 564 | const struct usb_endpoint_descriptor *desc, |
Felipe Balbi | 4b345c9 | 2012-07-16 14:08:16 +0300 | [diff] [blame] | 565 | const struct usb_ss_ep_comp_descriptor *comp_desc, |
Felipe Balbi | 21e64bf | 2016-06-02 12:37:31 +0300 | [diff] [blame] | 566 | bool modify, bool restore) |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 567 | { |
| 568 | struct dwc3_gadget_ep_cmd_params params; |
| 569 | |
Felipe Balbi | 21e64bf | 2016-06-02 12:37:31 +0300 | [diff] [blame] | 570 | if (dev_WARN_ONCE(dwc->dev, modify && restore, |
| 571 | "Can't modify and restore\n")) |
| 572 | return -EINVAL; |
| 573 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 574 | memset(¶ms, 0x00, sizeof(params)); |
| 575 | |
Felipe Balbi | dc1c70a | 2011-09-30 10:58:51 +0300 | [diff] [blame] | 576 | params.param0 = DWC3_DEPCFG_EP_TYPE(usb_endpoint_type(desc)) |
Chanho Park | d2e9a13 | 2012-08-31 16:54:07 +0900 | [diff] [blame] | 577 | | DWC3_DEPCFG_MAX_PACKET_SIZE(usb_endpoint_maxp(desc)); |
| 578 | |
| 579 | /* Burst size is only needed in SuperSpeed mode */ |
John Youn | ee5cd41 | 2016-02-05 17:08:45 -0800 | [diff] [blame] | 580 | if (dwc->gadget.speed >= USB_SPEED_SUPER) { |
Felipe Balbi | 676e349 | 2016-04-26 10:49:07 +0300 | [diff] [blame] | 581 | u32 burst = dep->endpoint.maxburst; |
Felipe Balbi | 676e349 | 2016-04-26 10:49:07 +0300 | [diff] [blame] | 582 | params.param0 |= DWC3_DEPCFG_BURST_SIZE(burst - 1); |
Chanho Park | d2e9a13 | 2012-08-31 16:54:07 +0900 | [diff] [blame] | 583 | } |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 584 | |
Felipe Balbi | 21e64bf | 2016-06-02 12:37:31 +0300 | [diff] [blame] | 585 | if (modify) { |
| 586 | params.param0 |= DWC3_DEPCFG_ACTION_MODIFY; |
| 587 | } else if (restore) { |
Paul Zimmerman | 265b70a | 2013-12-19 12:38:49 -0600 | [diff] [blame] | 588 | params.param0 |= DWC3_DEPCFG_ACTION_RESTORE; |
| 589 | params.param2 |= dep->saved_state; |
Felipe Balbi | 21e64bf | 2016-06-02 12:37:31 +0300 | [diff] [blame] | 590 | } else { |
| 591 | params.param0 |= DWC3_DEPCFG_ACTION_INIT; |
Paul Zimmerman | 265b70a | 2013-12-19 12:38:49 -0600 | [diff] [blame] | 592 | } |
| 593 | |
Felipe Balbi | 4bc48c9 | 2016-08-10 16:04:33 +0300 | [diff] [blame] | 594 | if (usb_endpoint_xfer_control(desc)) |
| 595 | params.param1 = DWC3_DEPCFG_XFER_COMPLETE_EN; |
Felipe Balbi | 13fa2e6 | 2016-05-30 13:40:00 +0300 | [diff] [blame] | 596 | |
| 597 | if (dep->number <= 1 || usb_endpoint_xfer_isoc(desc)) |
| 598 | params.param1 |= DWC3_DEPCFG_XFER_NOT_READY_EN; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 599 | |
Felipe Balbi | 18b7ede | 2012-01-02 13:35:41 +0200 | [diff] [blame] | 600 | if (usb_ss_max_streams(comp_desc) && usb_endpoint_xfer_bulk(desc)) { |
Felipe Balbi | dc1c70a | 2011-09-30 10:58:51 +0300 | [diff] [blame] | 601 | params.param1 |= DWC3_DEPCFG_STREAM_CAPABLE |
| 602 | | DWC3_DEPCFG_STREAM_EVENT_EN; |
Felipe Balbi | 879631a | 2011-09-30 10:58:47 +0300 | [diff] [blame] | 603 | dep->stream_capable = true; |
| 604 | } |
| 605 | |
Felipe Balbi | 0b93a4c | 2014-09-04 10:28:10 -0500 | [diff] [blame] | 606 | if (!usb_endpoint_xfer_control(desc)) |
Felipe Balbi | dc1c70a | 2011-09-30 10:58:51 +0300 | [diff] [blame] | 607 | params.param1 |= DWC3_DEPCFG_XFER_IN_PROGRESS_EN; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 608 | |
| 609 | /* |
| 610 | * We are doing 1:1 mapping for endpoints, meaning |
| 611 | * Physical Endpoints 2 maps to Logical Endpoint 2 and |
| 612 | * so on. We consider the direction bit as part of the physical |
| 613 | * endpoint number. So USB endpoint 0x81 is 0x03. |
| 614 | */ |
Felipe Balbi | dc1c70a | 2011-09-30 10:58:51 +0300 | [diff] [blame] | 615 | params.param1 |= DWC3_DEPCFG_EP_NUMBER(dep->number); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 616 | |
| 617 | /* |
| 618 | * We must use the lower 16 TX FIFOs even though |
| 619 | * HW might have more |
| 620 | */ |
| 621 | if (dep->direction) |
Felipe Balbi | dc1c70a | 2011-09-30 10:58:51 +0300 | [diff] [blame] | 622 | params.param0 |= DWC3_DEPCFG_FIFO_NUMBER(dep->number >> 1); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 623 | |
| 624 | if (desc->bInterval) { |
Felipe Balbi | dc1c70a | 2011-09-30 10:58:51 +0300 | [diff] [blame] | 625 | params.param1 |= DWC3_DEPCFG_BINTERVAL_M1(desc->bInterval - 1); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 626 | dep->interval = 1 << (desc->bInterval - 1); |
| 627 | } |
| 628 | |
Felipe Balbi | 2cd4718 | 2016-04-12 16:42:43 +0300 | [diff] [blame] | 629 | return dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETEPCONFIG, ¶ms); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 630 | } |
| 631 | |
| 632 | static int dwc3_gadget_set_xfer_resource(struct dwc3 *dwc, struct dwc3_ep *dep) |
| 633 | { |
| 634 | struct dwc3_gadget_ep_cmd_params params; |
| 635 | |
| 636 | memset(¶ms, 0x00, sizeof(params)); |
| 637 | |
Felipe Balbi | dc1c70a | 2011-09-30 10:58:51 +0300 | [diff] [blame] | 638 | params.param0 = DWC3_DEPXFERCFG_NUM_XFER_RES(1); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 639 | |
Felipe Balbi | 2cd4718 | 2016-04-12 16:42:43 +0300 | [diff] [blame] | 640 | return dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETTRANSFRESOURCE, |
| 641 | ¶ms); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 642 | } |
| 643 | |
| 644 | /** |
| 645 | * __dwc3_gadget_ep_enable - Initializes a HW endpoint |
| 646 | * @dep: endpoint to be initialized |
| 647 | * @desc: USB Endpoint Descriptor |
| 648 | * |
| 649 | * Caller should take care of locking |
| 650 | */ |
| 651 | static int __dwc3_gadget_ep_enable(struct dwc3_ep *dep, |
Felipe Balbi | c90bfae | 2011-11-29 13:11:21 +0200 | [diff] [blame] | 652 | const struct usb_endpoint_descriptor *desc, |
Felipe Balbi | 4b345c9 | 2012-07-16 14:08:16 +0300 | [diff] [blame] | 653 | const struct usb_ss_ep_comp_descriptor *comp_desc, |
Felipe Balbi | 21e64bf | 2016-06-02 12:37:31 +0300 | [diff] [blame] | 654 | bool modify, bool restore) |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 655 | { |
| 656 | struct dwc3 *dwc = dep->dwc; |
| 657 | u32 reg; |
Andy Shevchenko | b09e99e | 2014-05-15 15:53:32 +0300 | [diff] [blame] | 658 | int ret; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 659 | |
Felipe Balbi | 7381528 | 2015-01-27 13:48:14 -0600 | [diff] [blame] | 660 | dwc3_trace(trace_dwc3_gadget, "Enabling %s", dep->name); |
Felipe Balbi | ff62d6b | 2013-07-12 19:09:39 +0300 | [diff] [blame] | 661 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 662 | if (!(dep->flags & DWC3_EP_ENABLED)) { |
| 663 | ret = dwc3_gadget_start_config(dwc, dep); |
Mayank Rana | a99689a | 2016-08-10 17:39:47 -0700 | [diff] [blame] | 664 | if (ret) { |
| 665 | dev_err(dwc->dev, "start_config() failed for %s\n", |
| 666 | dep->name); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 667 | return ret; |
Mayank Rana | a99689a | 2016-08-10 17:39:47 -0700 | [diff] [blame] | 668 | } |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 669 | } |
| 670 | |
Felipe Balbi | 21e64bf | 2016-06-02 12:37:31 +0300 | [diff] [blame] | 671 | ret = dwc3_gadget_set_ep_config(dwc, dep, desc, comp_desc, modify, |
Paul Zimmerman | 265b70a | 2013-12-19 12:38:49 -0600 | [diff] [blame] | 672 | restore); |
Mayank Rana | a99689a | 2016-08-10 17:39:47 -0700 | [diff] [blame] | 673 | if (ret) { |
| 674 | dev_err(dwc->dev, "set_ep_config() failed for %s\n", dep->name); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 675 | return ret; |
Mayank Rana | a99689a | 2016-08-10 17:39:47 -0700 | [diff] [blame] | 676 | } |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 677 | |
| 678 | if (!(dep->flags & DWC3_EP_ENABLED)) { |
Felipe Balbi | f6bafc6 | 2012-02-06 11:04:53 +0200 | [diff] [blame] | 679 | struct dwc3_trb *trb_st_hw; |
| 680 | struct dwc3_trb *trb_link; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 681 | |
Ido Shayevitz | 16e78db | 2012-03-12 20:25:24 +0200 | [diff] [blame] | 682 | dep->endpoint.desc = desc; |
Felipe Balbi | c90bfae | 2011-11-29 13:11:21 +0200 | [diff] [blame] | 683 | dep->comp_desc = comp_desc; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 684 | dep->type = usb_endpoint_type(desc); |
| 685 | dep->flags |= DWC3_EP_ENABLED; |
| 686 | |
| 687 | reg = dwc3_readl(dwc->regs, DWC3_DALEPENA); |
| 688 | reg |= DWC3_DALEPENA_EP(dep->number); |
| 689 | dwc3_writel(dwc->regs, DWC3_DALEPENA, reg); |
| 690 | |
Felipe Balbi | 36b68aa | 2016-04-05 13:24:36 +0300 | [diff] [blame] | 691 | if (usb_endpoint_xfer_control(desc)) |
Felipe Balbi | 7ab373a | 2016-05-23 11:27:26 +0300 | [diff] [blame] | 692 | return 0; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 693 | |
John Youn | 0d25744 | 2016-05-19 17:26:08 -0700 | [diff] [blame] | 694 | /* Initialize the TRB ring */ |
| 695 | dep->trb_dequeue = 0; |
| 696 | dep->trb_enqueue = 0; |
| 697 | memset(dep->trb_pool, 0, |
| 698 | sizeof(struct dwc3_trb) * DWC3_TRB_NUM); |
| 699 | |
Felipe Balbi | 36b68aa | 2016-04-05 13:24:36 +0300 | [diff] [blame] | 700 | /* Link TRB. The HWO bit is never reset */ |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 701 | trb_st_hw = &dep->trb_pool[0]; |
| 702 | |
Felipe Balbi | f6bafc6 | 2012-02-06 11:04:53 +0200 | [diff] [blame] | 703 | trb_link = &dep->trb_pool[DWC3_TRB_NUM - 1]; |
Felipe Balbi | f6bafc6 | 2012-02-06 11:04:53 +0200 | [diff] [blame] | 704 | trb_link->bpl = lower_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw)); |
| 705 | trb_link->bph = upper_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw)); |
| 706 | trb_link->ctrl |= DWC3_TRBCTL_LINK_TRB; |
| 707 | trb_link->ctrl |= DWC3_TRB_CTRL_HWO; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 708 | } |
| 709 | |
| 710 | return 0; |
| 711 | } |
| 712 | |
Sebastian Andrzej Siewior | 624407f | 2011-08-29 13:56:37 +0200 | [diff] [blame] | 713 | static void dwc3_remove_requests(struct dwc3 *dwc, struct dwc3_ep *dep) |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 714 | { |
| 715 | struct dwc3_request *req; |
| 716 | |
Felipe Balbi | 0e14602 | 2016-06-21 10:32:02 +0300 | [diff] [blame] | 717 | dwc3_stop_active_transfer(dwc, dep->number, true); |
Felipe Balbi | 69450c4 | 2016-05-30 13:37:02 +0300 | [diff] [blame] | 718 | |
Felipe Balbi | 0e14602 | 2016-06-21 10:32:02 +0300 | [diff] [blame] | 719 | /* - giveback all requests to gadget driver */ |
| 720 | while (!list_empty(&dep->started_list)) { |
| 721 | req = next_request(&dep->started_list); |
Sebastian Andrzej Siewior | 624407f | 2011-08-29 13:56:37 +0200 | [diff] [blame] | 722 | |
Felipe Balbi | 0e14602 | 2016-06-21 10:32:02 +0300 | [diff] [blame] | 723 | dwc3_gadget_giveback(dep, req, -ESHUTDOWN); |
Felipe Balbi | ea53b88 | 2012-02-17 12:10:04 +0200 | [diff] [blame] | 724 | } |
| 725 | |
Felipe Balbi | aa3342c | 2016-03-14 11:01:31 +0200 | [diff] [blame] | 726 | while (!list_empty(&dep->pending_list)) { |
| 727 | req = next_request(&dep->pending_list); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 728 | |
Sebastian Andrzej Siewior | 624407f | 2011-08-29 13:56:37 +0200 | [diff] [blame] | 729 | dwc3_gadget_giveback(dep, req, -ESHUTDOWN); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 730 | } |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 731 | } |
| 732 | |
| 733 | /** |
| 734 | * __dwc3_gadget_ep_disable - Disables a HW endpoint |
| 735 | * @dep: the endpoint to disable |
| 736 | * |
Sebastian Andrzej Siewior | 624407f | 2011-08-29 13:56:37 +0200 | [diff] [blame] | 737 | * This function also removes requests which are currently processed ny the |
| 738 | * hardware and those which are not yet scheduled. |
| 739 | * Caller should take care of locking. |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 740 | */ |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 741 | static int __dwc3_gadget_ep_disable(struct dwc3_ep *dep) |
| 742 | { |
| 743 | struct dwc3 *dwc = dep->dwc; |
| 744 | u32 reg; |
| 745 | |
Felipe Balbi | 7eaeac5 | 2015-07-20 14:46:15 -0500 | [diff] [blame] | 746 | dwc3_trace(trace_dwc3_gadget, "Disabling %s", dep->name); |
| 747 | |
Mayank Rana | a99689a | 2016-08-10 17:39:47 -0700 | [diff] [blame] | 748 | if (dep->endpoint.ep_type == EP_TYPE_NORMAL) |
| 749 | dwc3_remove_requests(dwc, dep); |
| 750 | else if (dep->endpoint.ep_type == EP_TYPE_GSI) |
| 751 | dwc3_stop_active_transfer(dwc, dep->number, true); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 752 | |
Felipe Balbi | 687ef98 | 2014-04-16 10:30:33 -0500 | [diff] [blame] | 753 | /* make sure HW endpoint isn't stalled */ |
| 754 | if (dep->flags & DWC3_EP_STALL) |
Felipe Balbi | 7a60855 | 2014-09-24 14:19:52 -0500 | [diff] [blame] | 755 | __dwc3_gadget_ep_set_halt(dep, 0, false); |
Felipe Balbi | 687ef98 | 2014-04-16 10:30:33 -0500 | [diff] [blame] | 756 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 757 | reg = dwc3_readl(dwc->regs, DWC3_DALEPENA); |
| 758 | reg &= ~DWC3_DALEPENA_EP(dep->number); |
| 759 | dwc3_writel(dwc->regs, DWC3_DALEPENA, reg); |
| 760 | |
Felipe Balbi | 879631a | 2011-09-30 10:58:47 +0300 | [diff] [blame] | 761 | dep->stream_capable = false; |
Ido Shayevitz | f9c56cd | 2012-02-08 13:56:48 +0200 | [diff] [blame] | 762 | dep->endpoint.desc = NULL; |
Felipe Balbi | c90bfae | 2011-11-29 13:11:21 +0200 | [diff] [blame] | 763 | dep->comp_desc = NULL; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 764 | dep->type = 0; |
Felipe Balbi | 879631a | 2011-09-30 10:58:47 +0300 | [diff] [blame] | 765 | dep->flags = 0; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 766 | |
Mayank Rana | a99689a | 2016-08-10 17:39:47 -0700 | [diff] [blame] | 767 | /* Keep GSI ep names with "-gsi" suffix */ |
| 768 | if (!strnstr(dep->name, "gsi", 10)) { |
| 769 | snprintf(dep->name, sizeof(dep->name), "ep%d%s", |
| 770 | dep->number >> 1, |
| 771 | (dep->number & 1) ? "in" : "out"); |
| 772 | } |
| 773 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 774 | return 0; |
| 775 | } |
| 776 | |
| 777 | /* -------------------------------------------------------------------------- */ |
| 778 | |
| 779 | static int dwc3_gadget_ep0_enable(struct usb_ep *ep, |
| 780 | const struct usb_endpoint_descriptor *desc) |
| 781 | { |
| 782 | return -EINVAL; |
| 783 | } |
| 784 | |
| 785 | static int dwc3_gadget_ep0_disable(struct usb_ep *ep) |
| 786 | { |
| 787 | return -EINVAL; |
| 788 | } |
| 789 | |
| 790 | /* -------------------------------------------------------------------------- */ |
| 791 | |
| 792 | static int dwc3_gadget_ep_enable(struct usb_ep *ep, |
| 793 | const struct usb_endpoint_descriptor *desc) |
| 794 | { |
| 795 | struct dwc3_ep *dep; |
| 796 | struct dwc3 *dwc; |
| 797 | unsigned long flags; |
| 798 | int ret; |
| 799 | |
| 800 | if (!ep || !desc || desc->bDescriptorType != USB_DT_ENDPOINT) { |
| 801 | pr_debug("dwc3: invalid parameters\n"); |
| 802 | return -EINVAL; |
| 803 | } |
| 804 | |
| 805 | if (!desc->wMaxPacketSize) { |
| 806 | pr_debug("dwc3: missing wMaxPacketSize\n"); |
| 807 | return -EINVAL; |
| 808 | } |
| 809 | |
| 810 | dep = to_dwc3_ep(ep); |
| 811 | dwc = dep->dwc; |
| 812 | |
Felipe Balbi | 95ca961 | 2015-12-10 13:08:20 -0600 | [diff] [blame] | 813 | if (dev_WARN_ONCE(dwc->dev, dep->flags & DWC3_EP_ENABLED, |
| 814 | "%s is already enabled\n", |
| 815 | dep->name)) |
Felipe Balbi | c6f83f3 | 2012-08-15 12:28:29 +0300 | [diff] [blame] | 816 | return 0; |
Felipe Balbi | c6f83f3 | 2012-08-15 12:28:29 +0300 | [diff] [blame] | 817 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 818 | spin_lock_irqsave(&dwc->lock, flags); |
Paul Zimmerman | 265b70a | 2013-12-19 12:38:49 -0600 | [diff] [blame] | 819 | ret = __dwc3_gadget_ep_enable(dep, desc, ep->comp_desc, false, false); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 820 | spin_unlock_irqrestore(&dwc->lock, flags); |
| 821 | |
| 822 | return ret; |
| 823 | } |
| 824 | |
| 825 | static int dwc3_gadget_ep_disable(struct usb_ep *ep) |
| 826 | { |
| 827 | struct dwc3_ep *dep; |
| 828 | struct dwc3 *dwc; |
| 829 | unsigned long flags; |
| 830 | int ret; |
| 831 | |
| 832 | if (!ep) { |
| 833 | pr_debug("dwc3: invalid parameters\n"); |
| 834 | return -EINVAL; |
| 835 | } |
| 836 | |
| 837 | dep = to_dwc3_ep(ep); |
| 838 | dwc = dep->dwc; |
| 839 | |
Felipe Balbi | 95ca961 | 2015-12-10 13:08:20 -0600 | [diff] [blame] | 840 | if (dev_WARN_ONCE(dwc->dev, !(dep->flags & DWC3_EP_ENABLED), |
| 841 | "%s is already disabled\n", |
| 842 | dep->name)) |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 843 | return 0; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 844 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 845 | spin_lock_irqsave(&dwc->lock, flags); |
| 846 | ret = __dwc3_gadget_ep_disable(dep); |
| 847 | spin_unlock_irqrestore(&dwc->lock, flags); |
| 848 | |
| 849 | return ret; |
| 850 | } |
| 851 | |
| 852 | static struct usb_request *dwc3_gadget_ep_alloc_request(struct usb_ep *ep, |
| 853 | gfp_t gfp_flags) |
| 854 | { |
| 855 | struct dwc3_request *req; |
| 856 | struct dwc3_ep *dep = to_dwc3_ep(ep); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 857 | |
| 858 | req = kzalloc(sizeof(*req), gfp_flags); |
Jingoo Han | 734d5a5 | 2014-07-17 12:45:11 +0900 | [diff] [blame] | 859 | if (!req) |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 860 | return NULL; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 861 | |
| 862 | req->epnum = dep->number; |
| 863 | req->dep = dep; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 864 | |
Felipe Balbi | 68d34c8 | 2016-05-30 13:34:58 +0300 | [diff] [blame] | 865 | dep->allocated_requests++; |
| 866 | |
Felipe Balbi | 2c4cbe6e5 | 2014-04-30 17:45:10 -0500 | [diff] [blame] | 867 | trace_dwc3_alloc_request(req); |
| 868 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 869 | return &req->request; |
| 870 | } |
| 871 | |
| 872 | static void dwc3_gadget_ep_free_request(struct usb_ep *ep, |
| 873 | struct usb_request *request) |
| 874 | { |
| 875 | struct dwc3_request *req = to_dwc3_request(request); |
Felipe Balbi | 68d34c8 | 2016-05-30 13:34:58 +0300 | [diff] [blame] | 876 | struct dwc3_ep *dep = to_dwc3_ep(ep); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 877 | |
Felipe Balbi | 68d34c8 | 2016-05-30 13:34:58 +0300 | [diff] [blame] | 878 | dep->allocated_requests--; |
Felipe Balbi | 2c4cbe6e5 | 2014-04-30 17:45:10 -0500 | [diff] [blame] | 879 | trace_dwc3_free_request(req); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 880 | kfree(req); |
| 881 | } |
| 882 | |
Felipe Balbi | 2c78c02 | 2016-08-12 13:13:10 +0300 | [diff] [blame] | 883 | static u32 dwc3_calc_trbs_left(struct dwc3_ep *dep); |
| 884 | |
Felipe Balbi | c71fc37 | 2011-11-22 11:37:34 +0200 | [diff] [blame] | 885 | /** |
| 886 | * dwc3_prepare_one_trb - setup one TRB from one request |
| 887 | * @dep: endpoint for which this request is prepared |
| 888 | * @req: dwc3_request pointer |
| 889 | */ |
Felipe Balbi | 68e823e | 2011-11-28 12:25:01 +0200 | [diff] [blame] | 890 | static void dwc3_prepare_one_trb(struct dwc3_ep *dep, |
Felipe Balbi | eeb720f | 2011-11-28 12:46:59 +0200 | [diff] [blame] | 891 | struct dwc3_request *req, dma_addr_t dma, |
Felipe Balbi | 4bc48c9 | 2016-08-10 16:04:33 +0300 | [diff] [blame] | 892 | unsigned length, unsigned chain, unsigned node) |
Felipe Balbi | c71fc37 | 2011-11-22 11:37:34 +0200 | [diff] [blame] | 893 | { |
Felipe Balbi | f6bafc6 | 2012-02-06 11:04:53 +0200 | [diff] [blame] | 894 | struct dwc3_trb *trb; |
Felipe Balbi | c71fc37 | 2011-11-22 11:37:34 +0200 | [diff] [blame] | 895 | |
Felipe Balbi | 4bc48c9 | 2016-08-10 16:04:33 +0300 | [diff] [blame] | 896 | dwc3_trace(trace_dwc3_gadget, "%s: req %p dma %08llx length %d%s", |
Felipe Balbi | eeb720f | 2011-11-28 12:46:59 +0200 | [diff] [blame] | 897 | dep->name, req, (unsigned long long) dma, |
Felipe Balbi | 4bc48c9 | 2016-08-10 16:04:33 +0300 | [diff] [blame] | 898 | length, chain ? " chain" : ""); |
Pratyush Anand | 915e202 | 2013-01-14 15:59:35 +0530 | [diff] [blame] | 899 | |
Felipe Balbi | 4faf755 | 2016-04-05 13:14:31 +0300 | [diff] [blame] | 900 | trb = &dep->trb_pool[dep->trb_enqueue]; |
Felipe Balbi | c71fc37 | 2011-11-22 11:37:34 +0200 | [diff] [blame] | 901 | |
Felipe Balbi | eeb720f | 2011-11-28 12:46:59 +0200 | [diff] [blame] | 902 | if (!req->trb) { |
Felipe Balbi | aa3342c | 2016-03-14 11:01:31 +0200 | [diff] [blame] | 903 | dwc3_gadget_move_started_request(req); |
Felipe Balbi | f6bafc6 | 2012-02-06 11:04:53 +0200 | [diff] [blame] | 904 | req->trb = trb; |
| 905 | req->trb_dma = dwc3_trb_dma_offset(dep, trb); |
Felipe Balbi | 4faf755 | 2016-04-05 13:14:31 +0300 | [diff] [blame] | 906 | req->first_trb_index = dep->trb_enqueue; |
Felipe Balbi | a9c3ca5 | 2016-10-05 14:24:37 +0300 | [diff] [blame] | 907 | dep->queued_requests++; |
Felipe Balbi | eeb720f | 2011-11-28 12:46:59 +0200 | [diff] [blame] | 908 | } |
Felipe Balbi | c71fc37 | 2011-11-22 11:37:34 +0200 | [diff] [blame] | 909 | |
Felipe Balbi | ef966b9 | 2016-04-05 13:09:51 +0300 | [diff] [blame] | 910 | dwc3_ep_inc_enq(dep); |
Pratyush Anand | e5ba5ec | 2013-01-14 15:59:37 +0530 | [diff] [blame] | 911 | |
Felipe Balbi | f6bafc6 | 2012-02-06 11:04:53 +0200 | [diff] [blame] | 912 | trb->size = DWC3_TRB_SIZE_LENGTH(length); |
| 913 | trb->bpl = lower_32_bits(dma); |
| 914 | trb->bph = upper_32_bits(dma); |
Felipe Balbi | c71fc37 | 2011-11-22 11:37:34 +0200 | [diff] [blame] | 915 | |
Ido Shayevitz | 16e78db | 2012-03-12 20:25:24 +0200 | [diff] [blame] | 916 | switch (usb_endpoint_type(dep->endpoint.desc)) { |
Felipe Balbi | c71fc37 | 2011-11-22 11:37:34 +0200 | [diff] [blame] | 917 | case USB_ENDPOINT_XFER_CONTROL: |
Felipe Balbi | f6bafc6 | 2012-02-06 11:04:53 +0200 | [diff] [blame] | 918 | trb->ctrl = DWC3_TRBCTL_CONTROL_SETUP; |
Felipe Balbi | c71fc37 | 2011-11-22 11:37:34 +0200 | [diff] [blame] | 919 | break; |
| 920 | |
| 921 | case USB_ENDPOINT_XFER_ISOC: |
Pratyush Anand | e5ba5ec | 2013-01-14 15:59:37 +0530 | [diff] [blame] | 922 | if (!node) |
| 923 | trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS_FIRST; |
| 924 | else |
| 925 | trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS; |
Felipe Balbi | ca4d44e | 2016-03-10 13:53:27 +0200 | [diff] [blame] | 926 | |
| 927 | /* always enable Interrupt on Missed ISOC */ |
| 928 | trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI; |
Felipe Balbi | c71fc37 | 2011-11-22 11:37:34 +0200 | [diff] [blame] | 929 | break; |
| 930 | |
| 931 | case USB_ENDPOINT_XFER_BULK: |
| 932 | case USB_ENDPOINT_XFER_INT: |
Felipe Balbi | f6bafc6 | 2012-02-06 11:04:53 +0200 | [diff] [blame] | 933 | trb->ctrl = DWC3_TRBCTL_NORMAL; |
Felipe Balbi | c71fc37 | 2011-11-22 11:37:34 +0200 | [diff] [blame] | 934 | break; |
| 935 | default: |
| 936 | /* |
| 937 | * This is only possible with faulty memory because we |
| 938 | * checked it already :) |
| 939 | */ |
| 940 | BUG(); |
| 941 | } |
| 942 | |
Felipe Balbi | ca4d44e | 2016-03-10 13:53:27 +0200 | [diff] [blame] | 943 | /* always enable Continue on Short Packet */ |
| 944 | trb->ctrl |= DWC3_TRB_CTRL_CSP; |
Felipe Balbi | f3af365 | 2013-12-13 14:19:33 -0600 | [diff] [blame] | 945 | |
Felipe Balbi | 2c78c02 | 2016-08-12 13:13:10 +0300 | [diff] [blame] | 946 | if ((!req->request.no_interrupt && !chain) || |
| 947 | (dwc3_calc_trbs_left(dep) == 0)) |
Felipe Balbi | ca4d44e | 2016-03-10 13:53:27 +0200 | [diff] [blame] | 948 | trb->ctrl |= DWC3_TRB_CTRL_IOC | DWC3_TRB_CTRL_ISP_IMI; |
| 949 | |
Pratyush Anand | e5ba5ec | 2013-01-14 15:59:37 +0530 | [diff] [blame] | 950 | if (chain) |
| 951 | trb->ctrl |= DWC3_TRB_CTRL_CHN; |
| 952 | |
Ido Shayevitz | 16e78db | 2012-03-12 20:25:24 +0200 | [diff] [blame] | 953 | if (usb_endpoint_xfer_bulk(dep->endpoint.desc) && dep->stream_capable) |
Felipe Balbi | f6bafc6 | 2012-02-06 11:04:53 +0200 | [diff] [blame] | 954 | trb->ctrl |= DWC3_TRB_CTRL_SID_SOFN(req->request.stream_id); |
| 955 | |
| 956 | trb->ctrl |= DWC3_TRB_CTRL_HWO; |
Felipe Balbi | 2c4cbe6e5 | 2014-04-30 17:45:10 -0500 | [diff] [blame] | 957 | |
| 958 | trace_dwc3_prepare_trb(dep, trb); |
Felipe Balbi | c71fc37 | 2011-11-22 11:37:34 +0200 | [diff] [blame] | 959 | } |
| 960 | |
John Youn | 361572b | 2016-05-19 17:26:17 -0700 | [diff] [blame] | 961 | /** |
| 962 | * dwc3_ep_prev_trb() - Returns the previous TRB in the ring |
| 963 | * @dep: The endpoint with the TRB ring |
| 964 | * @index: The index of the current TRB in the ring |
| 965 | * |
| 966 | * Returns the TRB prior to the one pointed to by the index. If the |
| 967 | * index is 0, we will wrap backwards, skip the link TRB, and return |
| 968 | * the one just before that. |
| 969 | */ |
| 970 | static struct dwc3_trb *dwc3_ep_prev_trb(struct dwc3_ep *dep, u8 index) |
| 971 | { |
Felipe Balbi | 45438a0 | 2016-08-11 12:26:59 +0300 | [diff] [blame] | 972 | u8 tmp = index; |
John Youn | 361572b | 2016-05-19 17:26:17 -0700 | [diff] [blame] | 973 | |
Felipe Balbi | 45438a0 | 2016-08-11 12:26:59 +0300 | [diff] [blame] | 974 | if (!tmp) |
| 975 | tmp = DWC3_TRB_NUM - 1; |
| 976 | |
| 977 | return &dep->trb_pool[tmp - 1]; |
John Youn | 361572b | 2016-05-19 17:26:17 -0700 | [diff] [blame] | 978 | } |
| 979 | |
Felipe Balbi | c423357 | 2016-05-12 14:08:34 +0300 | [diff] [blame] | 980 | static u32 dwc3_calc_trbs_left(struct dwc3_ep *dep) |
| 981 | { |
| 982 | struct dwc3_trb *tmp; |
John Youn | 32db3d9 | 2016-05-19 17:26:12 -0700 | [diff] [blame] | 983 | u8 trbs_left; |
Felipe Balbi | c423357 | 2016-05-12 14:08:34 +0300 | [diff] [blame] | 984 | |
| 985 | /* |
| 986 | * If enqueue & dequeue are equal than it is either full or empty. |
| 987 | * |
| 988 | * One way to know for sure is if the TRB right before us has HWO bit |
| 989 | * set or not. If it has, then we're definitely full and can't fit any |
| 990 | * more transfers in our ring. |
| 991 | */ |
| 992 | if (dep->trb_enqueue == dep->trb_dequeue) { |
John Youn | 361572b | 2016-05-19 17:26:17 -0700 | [diff] [blame] | 993 | tmp = dwc3_ep_prev_trb(dep, dep->trb_enqueue); |
| 994 | if (tmp->ctrl & DWC3_TRB_CTRL_HWO) |
| 995 | return 0; |
Felipe Balbi | c423357 | 2016-05-12 14:08:34 +0300 | [diff] [blame] | 996 | |
| 997 | return DWC3_TRB_NUM - 1; |
| 998 | } |
| 999 | |
John Youn | 9d7aba7 | 2016-08-26 18:43:01 -0700 | [diff] [blame] | 1000 | trbs_left = dep->trb_dequeue - dep->trb_enqueue; |
John Youn | 3de2685f | 2016-05-23 11:32:45 -0700 | [diff] [blame] | 1001 | trbs_left &= (DWC3_TRB_NUM - 1); |
John Youn | 32db3d9 | 2016-05-19 17:26:12 -0700 | [diff] [blame] | 1002 | |
John Youn | 9d7aba7 | 2016-08-26 18:43:01 -0700 | [diff] [blame] | 1003 | if (dep->trb_dequeue < dep->trb_enqueue) |
| 1004 | trbs_left--; |
| 1005 | |
John Youn | 32db3d9 | 2016-05-19 17:26:12 -0700 | [diff] [blame] | 1006 | return trbs_left; |
Felipe Balbi | c423357 | 2016-05-12 14:08:34 +0300 | [diff] [blame] | 1007 | } |
| 1008 | |
Felipe Balbi | 5ee85d8 | 2016-05-13 12:42:44 +0300 | [diff] [blame] | 1009 | static void dwc3_prepare_one_trb_sg(struct dwc3_ep *dep, |
Felipe Balbi | 7ae7df4 | 2016-08-24 14:37:22 +0300 | [diff] [blame] | 1010 | struct dwc3_request *req) |
Felipe Balbi | 5ee85d8 | 2016-05-13 12:42:44 +0300 | [diff] [blame] | 1011 | { |
Felipe Balbi | 1f51211 | 2016-08-12 13:17:27 +0300 | [diff] [blame] | 1012 | struct scatterlist *sg = req->sg; |
Felipe Balbi | 5ee85d8 | 2016-05-13 12:42:44 +0300 | [diff] [blame] | 1013 | struct scatterlist *s; |
Felipe Balbi | 5ee85d8 | 2016-05-13 12:42:44 +0300 | [diff] [blame] | 1014 | unsigned int length; |
| 1015 | dma_addr_t dma; |
| 1016 | int i; |
| 1017 | |
Felipe Balbi | 1f51211 | 2016-08-12 13:17:27 +0300 | [diff] [blame] | 1018 | for_each_sg(sg, s, req->num_pending_sgs, i) { |
Felipe Balbi | 5ee85d8 | 2016-05-13 12:42:44 +0300 | [diff] [blame] | 1019 | unsigned chain = true; |
| 1020 | |
| 1021 | length = sg_dma_len(s); |
| 1022 | dma = sg_dma_address(s); |
| 1023 | |
Felipe Balbi | 4bc48c9 | 2016-08-10 16:04:33 +0300 | [diff] [blame] | 1024 | if (sg_is_last(s)) |
Felipe Balbi | 5ee85d8 | 2016-05-13 12:42:44 +0300 | [diff] [blame] | 1025 | chain = false; |
| 1026 | |
| 1027 | dwc3_prepare_one_trb(dep, req, dma, length, |
Felipe Balbi | 4bc48c9 | 2016-08-10 16:04:33 +0300 | [diff] [blame] | 1028 | chain, i); |
Felipe Balbi | 5ee85d8 | 2016-05-13 12:42:44 +0300 | [diff] [blame] | 1029 | |
Felipe Balbi | 7ae7df4 | 2016-08-24 14:37:22 +0300 | [diff] [blame] | 1030 | if (!dwc3_calc_trbs_left(dep)) |
Felipe Balbi | 5ee85d8 | 2016-05-13 12:42:44 +0300 | [diff] [blame] | 1031 | break; |
| 1032 | } |
| 1033 | } |
| 1034 | |
| 1035 | static void dwc3_prepare_one_trb_linear(struct dwc3_ep *dep, |
Felipe Balbi | 7ae7df4 | 2016-08-24 14:37:22 +0300 | [diff] [blame] | 1036 | struct dwc3_request *req) |
Felipe Balbi | 5ee85d8 | 2016-05-13 12:42:44 +0300 | [diff] [blame] | 1037 | { |
Felipe Balbi | 5ee85d8 | 2016-05-13 12:42:44 +0300 | [diff] [blame] | 1038 | unsigned int length; |
| 1039 | dma_addr_t dma; |
| 1040 | |
| 1041 | dma = req->request.dma; |
| 1042 | length = req->request.length; |
| 1043 | |
Felipe Balbi | 5ee85d8 | 2016-05-13 12:42:44 +0300 | [diff] [blame] | 1044 | dwc3_prepare_one_trb(dep, req, dma, length, |
Felipe Balbi | 4bc48c9 | 2016-08-10 16:04:33 +0300 | [diff] [blame] | 1045 | false, 0); |
Felipe Balbi | 5ee85d8 | 2016-05-13 12:42:44 +0300 | [diff] [blame] | 1046 | } |
| 1047 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1048 | /* |
| 1049 | * dwc3_prepare_trbs - setup TRBs from requests |
| 1050 | * @dep: endpoint for which requests are being prepared |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1051 | * |
Paul Zimmerman | 1d04679 | 2012-02-15 18:56:56 -0800 | [diff] [blame] | 1052 | * The function goes through the requests list and sets up TRBs for the |
| 1053 | * transfers. The function returns once there are no more TRBs available or |
| 1054 | * it runs out of requests. |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1055 | */ |
Felipe Balbi | c423357 | 2016-05-12 14:08:34 +0300 | [diff] [blame] | 1056 | static void dwc3_prepare_trbs(struct dwc3_ep *dep) |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1057 | { |
Felipe Balbi | 68e823e | 2011-11-28 12:25:01 +0200 | [diff] [blame] | 1058 | struct dwc3_request *req, *n; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1059 | |
| 1060 | BUILD_BUG_ON_NOT_POWER_OF_2(DWC3_TRB_NUM); |
| 1061 | |
Felipe Balbi | 7ae7df4 | 2016-08-24 14:37:22 +0300 | [diff] [blame] | 1062 | if (!dwc3_calc_trbs_left(dep)) |
John Youn | 89bc856 | 2016-05-19 17:26:10 -0700 | [diff] [blame] | 1063 | return; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1064 | |
Felipe Balbi | aa3342c | 2016-03-14 11:01:31 +0200 | [diff] [blame] | 1065 | list_for_each_entry_safe(req, n, &dep->pending_list, list) { |
Felipe Balbi | 1f51211 | 2016-08-12 13:17:27 +0300 | [diff] [blame] | 1066 | if (req->num_pending_sgs > 0) |
Felipe Balbi | 7ae7df4 | 2016-08-24 14:37:22 +0300 | [diff] [blame] | 1067 | dwc3_prepare_one_trb_sg(dep, req); |
Felipe Balbi | 5ee85d8 | 2016-05-13 12:42:44 +0300 | [diff] [blame] | 1068 | else |
Felipe Balbi | 7ae7df4 | 2016-08-24 14:37:22 +0300 | [diff] [blame] | 1069 | dwc3_prepare_one_trb_linear(dep, req); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1070 | |
Felipe Balbi | 7ae7df4 | 2016-08-24 14:37:22 +0300 | [diff] [blame] | 1071 | if (!dwc3_calc_trbs_left(dep)) |
Felipe Balbi | 5ee85d8 | 2016-05-13 12:42:44 +0300 | [diff] [blame] | 1072 | return; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1073 | } |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1074 | } |
| 1075 | |
Felipe Balbi | 4fae2e3 | 2016-05-12 16:53:59 +0300 | [diff] [blame] | 1076 | static int __dwc3_gadget_kick_transfer(struct dwc3_ep *dep, u16 cmd_param) |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1077 | { |
| 1078 | struct dwc3_gadget_ep_cmd_params params; |
| 1079 | struct dwc3_request *req; |
| 1080 | struct dwc3 *dwc = dep->dwc; |
Felipe Balbi | 4fae2e3 | 2016-05-12 16:53:59 +0300 | [diff] [blame] | 1081 | int starting; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1082 | int ret; |
| 1083 | u32 cmd; |
| 1084 | |
Felipe Balbi | 4fae2e3 | 2016-05-12 16:53:59 +0300 | [diff] [blame] | 1085 | starting = !(dep->flags & DWC3_EP_BUSY); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1086 | |
Felipe Balbi | 4fae2e3 | 2016-05-12 16:53:59 +0300 | [diff] [blame] | 1087 | dwc3_prepare_trbs(dep); |
| 1088 | req = next_request(&dep->started_list); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1089 | if (!req) { |
| 1090 | dep->flags |= DWC3_EP_PENDING_REQUEST; |
| 1091 | return 0; |
| 1092 | } |
| 1093 | |
| 1094 | memset(¶ms, 0, sizeof(params)); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1095 | |
Felipe Balbi | 4fae2e3 | 2016-05-12 16:53:59 +0300 | [diff] [blame] | 1096 | if (starting) { |
Pratyush Anand | 1877d6c | 2013-01-14 15:59:36 +0530 | [diff] [blame] | 1097 | params.param0 = upper_32_bits(req->trb_dma); |
| 1098 | params.param1 = lower_32_bits(req->trb_dma); |
Felipe Balbi | b6b1c6d | 2016-05-30 13:29:35 +0300 | [diff] [blame] | 1099 | cmd = DWC3_DEPCMD_STARTTRANSFER | |
| 1100 | DWC3_DEPCMD_PARAM(cmd_param); |
Pratyush Anand | 1877d6c | 2013-01-14 15:59:36 +0530 | [diff] [blame] | 1101 | } else { |
Felipe Balbi | b6b1c6d | 2016-05-30 13:29:35 +0300 | [diff] [blame] | 1102 | cmd = DWC3_DEPCMD_UPDATETRANSFER | |
| 1103 | DWC3_DEPCMD_PARAM(dep->resource_index); |
Pratyush Anand | 1877d6c | 2013-01-14 15:59:36 +0530 | [diff] [blame] | 1104 | } |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1105 | |
Felipe Balbi | 2cd4718 | 2016-04-12 16:42:43 +0300 | [diff] [blame] | 1106 | ret = dwc3_send_gadget_ep_cmd(dep, cmd, ¶ms); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1107 | if (ret < 0) { |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1108 | /* |
| 1109 | * FIXME we need to iterate over the list of requests |
| 1110 | * here and stop, unmap, free and del each of the linked |
Paul Zimmerman | 1d04679 | 2012-02-15 18:56:56 -0800 | [diff] [blame] | 1111 | * requests instead of what we do now. |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1112 | */ |
Felipe Balbi | 0fc9a1b | 2011-12-19 11:32:34 +0200 | [diff] [blame] | 1113 | usb_gadget_unmap_request(&dwc->gadget, &req->request, |
| 1114 | req->direction); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1115 | list_del(&req->list); |
| 1116 | return ret; |
| 1117 | } |
| 1118 | |
| 1119 | dep->flags |= DWC3_EP_BUSY; |
Felipe Balbi | 25b8ff6 | 2011-11-04 12:32:47 +0200 | [diff] [blame] | 1120 | |
Felipe Balbi | 4fae2e3 | 2016-05-12 16:53:59 +0300 | [diff] [blame] | 1121 | if (starting) { |
Felipe Balbi | 2eb8801 | 2016-04-12 16:53:39 +0300 | [diff] [blame] | 1122 | dep->resource_index = dwc3_gadget_ep_get_transfer_index(dep); |
Felipe Balbi | b4996a8 | 2012-06-06 12:04:13 +0300 | [diff] [blame] | 1123 | WARN_ON_ONCE(!dep->resource_index); |
Paul Zimmerman | f898ae0 | 2012-03-29 18:16:54 +0000 | [diff] [blame] | 1124 | } |
Felipe Balbi | 25b8ff6 | 2011-11-04 12:32:47 +0200 | [diff] [blame] | 1125 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1126 | return 0; |
| 1127 | } |
| 1128 | |
Pratyush Anand | d6d6ec7 | 2012-05-25 18:54:56 +0530 | [diff] [blame] | 1129 | static void __dwc3_gadget_start_isoc(struct dwc3 *dwc, |
| 1130 | struct dwc3_ep *dep, u32 cur_uf) |
| 1131 | { |
| 1132 | u32 uf; |
| 1133 | |
Felipe Balbi | aa3342c | 2016-03-14 11:01:31 +0200 | [diff] [blame] | 1134 | if (list_empty(&dep->pending_list)) { |
Felipe Balbi | 7381528 | 2015-01-27 13:48:14 -0600 | [diff] [blame] | 1135 | dwc3_trace(trace_dwc3_gadget, |
| 1136 | "ISOC ep %s run out for requests", |
| 1137 | dep->name); |
Pratyush Anand | f4a53c5 | 2012-08-30 12:21:43 +0530 | [diff] [blame] | 1138 | dep->flags |= DWC3_EP_PENDING_REQUEST; |
Pratyush Anand | d6d6ec7 | 2012-05-25 18:54:56 +0530 | [diff] [blame] | 1139 | return; |
| 1140 | } |
| 1141 | |
| 1142 | /* 4 micro frames in the future */ |
| 1143 | uf = cur_uf + dep->interval * 4; |
| 1144 | |
Felipe Balbi | 4fae2e3 | 2016-05-12 16:53:59 +0300 | [diff] [blame] | 1145 | __dwc3_gadget_kick_transfer(dep, uf); |
Pratyush Anand | d6d6ec7 | 2012-05-25 18:54:56 +0530 | [diff] [blame] | 1146 | } |
| 1147 | |
| 1148 | static void dwc3_gadget_start_isoc(struct dwc3 *dwc, |
| 1149 | struct dwc3_ep *dep, const struct dwc3_event_depevt *event) |
| 1150 | { |
| 1151 | u32 cur_uf, mask; |
| 1152 | |
| 1153 | mask = ~(dep->interval - 1); |
| 1154 | cur_uf = event->parameters & mask; |
| 1155 | |
| 1156 | __dwc3_gadget_start_isoc(dwc, dep, cur_uf); |
| 1157 | } |
| 1158 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1159 | static int __dwc3_gadget_ep_queue(struct dwc3_ep *dep, struct dwc3_request *req) |
| 1160 | { |
Felipe Balbi | 0fc9a1b | 2011-12-19 11:32:34 +0200 | [diff] [blame] | 1161 | struct dwc3 *dwc = dep->dwc; |
| 1162 | int ret; |
| 1163 | |
Felipe Balbi | bb42398 | 2015-11-16 15:31:21 -0600 | [diff] [blame] | 1164 | if (!dep->endpoint.desc) { |
Felipe Balbi | ec5e795 | 2015-11-16 16:04:13 -0600 | [diff] [blame] | 1165 | dwc3_trace(trace_dwc3_gadget, |
Felipe Balbi | 60cfb37 | 2016-05-24 13:45:17 +0300 | [diff] [blame] | 1166 | "trying to queue request %p to disabled %s", |
Felipe Balbi | bb42398 | 2015-11-16 15:31:21 -0600 | [diff] [blame] | 1167 | &req->request, dep->endpoint.name); |
| 1168 | return -ESHUTDOWN; |
| 1169 | } |
| 1170 | |
| 1171 | if (WARN(req->dep != dep, "request %p belongs to '%s'\n", |
| 1172 | &req->request, req->dep->name)) { |
Felipe Balbi | 60cfb37 | 2016-05-24 13:45:17 +0300 | [diff] [blame] | 1173 | dwc3_trace(trace_dwc3_gadget, "request %p belongs to '%s'", |
Felipe Balbi | ec5e795 | 2015-11-16 16:04:13 -0600 | [diff] [blame] | 1174 | &req->request, req->dep->name); |
Felipe Balbi | bb42398 | 2015-11-16 15:31:21 -0600 | [diff] [blame] | 1175 | return -EINVAL; |
| 1176 | } |
| 1177 | |
Felipe Balbi | fc8bb91 | 2016-05-16 13:14:48 +0300 | [diff] [blame] | 1178 | pm_runtime_get(dwc->dev); |
| 1179 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1180 | req->request.actual = 0; |
| 1181 | req->request.status = -EINPROGRESS; |
| 1182 | req->direction = dep->direction; |
| 1183 | req->epnum = dep->number; |
| 1184 | |
Felipe Balbi | fe84f52 | 2015-09-01 09:01:38 -0500 | [diff] [blame] | 1185 | trace_dwc3_ep_queue(req); |
| 1186 | |
Felipe Balbi | 0fc9a1b | 2011-12-19 11:32:34 +0200 | [diff] [blame] | 1187 | ret = usb_gadget_map_request(&dwc->gadget, &req->request, |
| 1188 | dep->direction); |
| 1189 | if (ret) |
| 1190 | return ret; |
| 1191 | |
Felipe Balbi | 1f51211 | 2016-08-12 13:17:27 +0300 | [diff] [blame] | 1192 | req->sg = req->request.sg; |
| 1193 | req->num_pending_sgs = req->request.num_mapped_sgs; |
| 1194 | |
Felipe Balbi | aa3342c | 2016-03-14 11:01:31 +0200 | [diff] [blame] | 1195 | list_add_tail(&req->list, &dep->pending_list); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1196 | |
Felipe Balbi | d889c23 | 2016-09-29 15:44:29 +0300 | [diff] [blame] | 1197 | /* |
| 1198 | * NOTICE: Isochronous endpoints should NEVER be prestarted. We must |
| 1199 | * wait for a XferNotReady event so we will know what's the current |
| 1200 | * (micro-)frame number. |
| 1201 | * |
| 1202 | * Without this trick, we are very, very likely gonna get Bus Expiry |
| 1203 | * errors which will force us issue EndTransfer command. |
| 1204 | */ |
| 1205 | if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) { |
| 1206 | if ((dep->flags & DWC3_EP_PENDING_REQUEST) && |
| 1207 | list_empty(&dep->started_list)) { |
Felipe Balbi | 08a36b5 | 2016-08-11 14:27:52 +0300 | [diff] [blame] | 1208 | dwc3_stop_active_transfer(dwc, dep->number, true); |
| 1209 | dep->flags = DWC3_EP_ENABLED; |
| 1210 | } |
| 1211 | return 0; |
Felipe Balbi | b511e5e | 2012-06-06 12:00:50 +0300 | [diff] [blame] | 1212 | } |
| 1213 | |
Felipe Balbi | 594e121 | 2016-08-24 14:38:10 +0300 | [diff] [blame] | 1214 | if (!dwc3_calc_trbs_left(dep)) |
| 1215 | return 0; |
Felipe Balbi | b997ada | 2012-07-26 13:26:50 +0300 | [diff] [blame] | 1216 | |
Felipe Balbi | 08a36b5 | 2016-08-11 14:27:52 +0300 | [diff] [blame] | 1217 | ret = __dwc3_gadget_kick_transfer(dep, 0); |
Felipe Balbi | a8f3281 | 2015-09-16 10:40:07 -0500 | [diff] [blame] | 1218 | if (ret && ret != -EBUSY) |
Felipe Balbi | ec5e795 | 2015-11-16 16:04:13 -0600 | [diff] [blame] | 1219 | dwc3_trace(trace_dwc3_gadget, |
Felipe Balbi | 60cfb37 | 2016-05-24 13:45:17 +0300 | [diff] [blame] | 1220 | "%s: failed to kick transfers", |
Felipe Balbi | a8f3281 | 2015-09-16 10:40:07 -0500 | [diff] [blame] | 1221 | dep->name); |
| 1222 | if (ret == -EBUSY) |
| 1223 | ret = 0; |
| 1224 | |
| 1225 | return ret; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1226 | } |
| 1227 | |
Mayank Rana | a99689a | 2016-08-10 17:39:47 -0700 | [diff] [blame] | 1228 | static int dwc3_gadget_wakeup(struct usb_gadget *g) |
| 1229 | { |
| 1230 | struct dwc3 *dwc = gadget_to_dwc(g); |
| 1231 | |
| 1232 | schedule_work(&dwc->wakeup_work); |
| 1233 | return 0; |
| 1234 | } |
| 1235 | |
| 1236 | static inline enum dwc3_link_state dwc3_get_link_state(struct dwc3 *dwc) |
| 1237 | { |
| 1238 | u32 reg; |
| 1239 | |
| 1240 | reg = dwc3_readl(dwc->regs, DWC3_DSTS); |
| 1241 | return DWC3_DSTS_USBLNKST(reg); |
| 1242 | } |
| 1243 | |
| 1244 | static bool dwc3_gadget_is_suspended(struct dwc3 *dwc) |
| 1245 | { |
| 1246 | if (atomic_read(&dwc->in_lpm) || |
| 1247 | dwc->link_state == DWC3_LINK_STATE_U3) |
| 1248 | return true; |
| 1249 | return false; |
| 1250 | } |
| 1251 | |
Felipe Balbi | 04c03d1 | 2015-12-02 10:06:45 -0600 | [diff] [blame] | 1252 | static void __dwc3_gadget_ep_zlp_complete(struct usb_ep *ep, |
| 1253 | struct usb_request *request) |
| 1254 | { |
| 1255 | dwc3_gadget_ep_free_request(ep, request); |
| 1256 | } |
| 1257 | |
| 1258 | static int __dwc3_gadget_ep_queue_zlp(struct dwc3 *dwc, struct dwc3_ep *dep) |
| 1259 | { |
| 1260 | struct dwc3_request *req; |
| 1261 | struct usb_request *request; |
| 1262 | struct usb_ep *ep = &dep->endpoint; |
| 1263 | |
Felipe Balbi | 60cfb37 | 2016-05-24 13:45:17 +0300 | [diff] [blame] | 1264 | dwc3_trace(trace_dwc3_gadget, "queueing ZLP"); |
Felipe Balbi | 04c03d1 | 2015-12-02 10:06:45 -0600 | [diff] [blame] | 1265 | request = dwc3_gadget_ep_alloc_request(ep, GFP_ATOMIC); |
| 1266 | if (!request) |
| 1267 | return -ENOMEM; |
| 1268 | |
| 1269 | request->length = 0; |
| 1270 | request->buf = dwc->zlp_buf; |
| 1271 | request->complete = __dwc3_gadget_ep_zlp_complete; |
| 1272 | |
| 1273 | req = to_dwc3_request(request); |
| 1274 | |
| 1275 | return __dwc3_gadget_ep_queue(dep, req); |
| 1276 | } |
| 1277 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1278 | static int dwc3_gadget_ep_queue(struct usb_ep *ep, struct usb_request *request, |
| 1279 | gfp_t gfp_flags) |
| 1280 | { |
| 1281 | struct dwc3_request *req = to_dwc3_request(request); |
| 1282 | struct dwc3_ep *dep = to_dwc3_ep(ep); |
| 1283 | struct dwc3 *dwc = dep->dwc; |
| 1284 | |
| 1285 | unsigned long flags; |
| 1286 | |
| 1287 | int ret; |
| 1288 | |
Zhuang Jin Can | fdee4eb | 2014-09-03 14:26:34 +0800 | [diff] [blame] | 1289 | spin_lock_irqsave(&dwc->lock, flags); |
Mayank Rana | a99689a | 2016-08-10 17:39:47 -0700 | [diff] [blame] | 1290 | if (!dep->endpoint.desc) { |
| 1291 | dev_dbg(dwc->dev, "trying to queue request %p to disabled %s\n", |
| 1292 | request, ep->name); |
| 1293 | ret = -ESHUTDOWN; |
| 1294 | goto out; |
| 1295 | } |
| 1296 | |
| 1297 | if (WARN(req->dep != dep, "request %p belongs to '%s'\n", |
| 1298 | request, req->dep->name)) { |
| 1299 | ret = -EINVAL; |
| 1300 | goto out; |
| 1301 | } |
| 1302 | |
| 1303 | if (dwc3_gadget_is_suspended(dwc)) { |
| 1304 | if (dwc->gadget.remote_wakeup) |
| 1305 | dwc3_gadget_wakeup(&dwc->gadget); |
| 1306 | ret = dwc->gadget.remote_wakeup ? -EAGAIN : -ENOTSUPP; |
| 1307 | goto out; |
| 1308 | } |
| 1309 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1310 | ret = __dwc3_gadget_ep_queue(dep, req); |
Felipe Balbi | 04c03d1 | 2015-12-02 10:06:45 -0600 | [diff] [blame] | 1311 | |
| 1312 | /* |
| 1313 | * Okay, here's the thing, if gadget driver has requested for a ZLP by |
| 1314 | * setting request->zero, instead of doing magic, we will just queue an |
| 1315 | * extra usb_request ourselves so that it gets handled the same way as |
| 1316 | * any other request. |
| 1317 | */ |
John Youn | d9261898 | 2015-12-22 12:23:20 -0800 | [diff] [blame] | 1318 | if (ret == 0 && request->zero && request->length && |
| 1319 | (request->length % ep->maxpacket == 0)) |
Felipe Balbi | 04c03d1 | 2015-12-02 10:06:45 -0600 | [diff] [blame] | 1320 | ret = __dwc3_gadget_ep_queue_zlp(dwc, dep); |
| 1321 | |
Mayank Rana | a99689a | 2016-08-10 17:39:47 -0700 | [diff] [blame] | 1322 | out: |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1323 | spin_unlock_irqrestore(&dwc->lock, flags); |
| 1324 | |
| 1325 | return ret; |
| 1326 | } |
| 1327 | |
| 1328 | static int dwc3_gadget_ep_dequeue(struct usb_ep *ep, |
| 1329 | struct usb_request *request) |
| 1330 | { |
| 1331 | struct dwc3_request *req = to_dwc3_request(request); |
| 1332 | struct dwc3_request *r = NULL; |
| 1333 | |
| 1334 | struct dwc3_ep *dep = to_dwc3_ep(ep); |
| 1335 | struct dwc3 *dwc = dep->dwc; |
| 1336 | |
| 1337 | unsigned long flags; |
| 1338 | int ret = 0; |
| 1339 | |
Mayank Rana | a99689a | 2016-08-10 17:39:47 -0700 | [diff] [blame] | 1340 | if (atomic_read(&dwc->in_lpm)) { |
| 1341 | dev_err(dwc->dev, "Unable to dequeue while in LPM\n"); |
| 1342 | return -EAGAIN; |
| 1343 | } |
| 1344 | |
Felipe Balbi | 2c4cbe6e5 | 2014-04-30 17:45:10 -0500 | [diff] [blame] | 1345 | trace_dwc3_ep_dequeue(req); |
| 1346 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1347 | spin_lock_irqsave(&dwc->lock, flags); |
| 1348 | |
Felipe Balbi | aa3342c | 2016-03-14 11:01:31 +0200 | [diff] [blame] | 1349 | list_for_each_entry(r, &dep->pending_list, list) { |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1350 | if (r == req) |
| 1351 | break; |
| 1352 | } |
| 1353 | |
| 1354 | if (r != req) { |
Felipe Balbi | aa3342c | 2016-03-14 11:01:31 +0200 | [diff] [blame] | 1355 | list_for_each_entry(r, &dep->started_list, list) { |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1356 | if (r == req) |
| 1357 | break; |
| 1358 | } |
| 1359 | if (r == req) { |
| 1360 | /* wait until it is processed */ |
Paul Zimmerman | b992e68 | 2012-04-27 14:17:35 +0300 | [diff] [blame] | 1361 | dwc3_stop_active_transfer(dwc, dep->number, true); |
Pratyush Anand | e8d4e8b | 2012-06-15 11:54:00 +0530 | [diff] [blame] | 1362 | goto out1; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1363 | } |
| 1364 | dev_err(dwc->dev, "request %p was not queued to %s\n", |
| 1365 | request, ep->name); |
| 1366 | ret = -EINVAL; |
| 1367 | goto out0; |
| 1368 | } |
| 1369 | |
Pratyush Anand | e8d4e8b | 2012-06-15 11:54:00 +0530 | [diff] [blame] | 1370 | out1: |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1371 | /* giveback the request */ |
| 1372 | dwc3_gadget_giveback(dep, req, -ECONNRESET); |
| 1373 | |
| 1374 | out0: |
| 1375 | spin_unlock_irqrestore(&dwc->lock, flags); |
| 1376 | |
| 1377 | return ret; |
| 1378 | } |
| 1379 | |
Felipe Balbi | 7a60855 | 2014-09-24 14:19:52 -0500 | [diff] [blame] | 1380 | 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] | 1381 | { |
| 1382 | struct dwc3_gadget_ep_cmd_params params; |
| 1383 | struct dwc3 *dwc = dep->dwc; |
| 1384 | int ret; |
| 1385 | |
Felipe Balbi | 5ad02fb | 2014-09-24 10:48:26 -0500 | [diff] [blame] | 1386 | if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) { |
| 1387 | dev_err(dwc->dev, "%s is of Isochronous type\n", dep->name); |
| 1388 | return -EINVAL; |
| 1389 | } |
| 1390 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1391 | memset(¶ms, 0x00, sizeof(params)); |
| 1392 | |
| 1393 | if (value) { |
Felipe Balbi | 69450c4 | 2016-05-30 13:37:02 +0300 | [diff] [blame] | 1394 | struct dwc3_trb *trb; |
| 1395 | |
| 1396 | unsigned transfer_in_flight; |
| 1397 | unsigned started; |
| 1398 | |
| 1399 | if (dep->number > 1) |
| 1400 | trb = dwc3_ep_prev_trb(dep, dep->trb_enqueue); |
| 1401 | else |
| 1402 | trb = &dwc->ep0_trb[dep->trb_enqueue]; |
| 1403 | |
| 1404 | transfer_in_flight = trb->ctrl & DWC3_TRB_CTRL_HWO; |
| 1405 | started = !list_empty(&dep->started_list); |
| 1406 | |
| 1407 | if (!protocol && ((dep->direction && transfer_in_flight) || |
| 1408 | (!dep->direction && started))) { |
Felipe Balbi | ec5e795 | 2015-11-16 16:04:13 -0600 | [diff] [blame] | 1409 | dwc3_trace(trace_dwc3_gadget, |
Felipe Balbi | 052ba52ef | 2016-04-05 15:05:05 +0300 | [diff] [blame] | 1410 | "%s: pending request, cannot halt", |
Felipe Balbi | 7a60855 | 2014-09-24 14:19:52 -0500 | [diff] [blame] | 1411 | dep->name); |
| 1412 | return -EAGAIN; |
| 1413 | } |
| 1414 | |
Felipe Balbi | 2cd4718 | 2016-04-12 16:42:43 +0300 | [diff] [blame] | 1415 | ret = dwc3_send_gadget_ep_cmd(dep, DWC3_DEPCMD_SETSTALL, |
| 1416 | ¶ms); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1417 | if (ret) |
Dan Carpenter | 3f89204 | 2014-03-07 14:20:22 +0300 | [diff] [blame] | 1418 | dev_err(dwc->dev, "failed to set STALL on %s\n", |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1419 | dep->name); |
| 1420 | else |
| 1421 | dep->flags |= DWC3_EP_STALL; |
| 1422 | } else { |
Felipe Balbi | 2cd4718 | 2016-04-12 16:42:43 +0300 | [diff] [blame] | 1423 | |
John Youn | 50c763f | 2016-05-31 17:49:56 -0700 | [diff] [blame] | 1424 | ret = dwc3_send_clear_stall_ep_cmd(dep); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1425 | if (ret) |
Dan Carpenter | 3f89204 | 2014-03-07 14:20:22 +0300 | [diff] [blame] | 1426 | dev_err(dwc->dev, "failed to clear STALL on %s\n", |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1427 | dep->name); |
| 1428 | else |
Alan Stern | a535d81 | 2013-11-01 12:05:12 -0400 | [diff] [blame] | 1429 | dep->flags &= ~(DWC3_EP_STALL | DWC3_EP_WEDGE); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1430 | } |
Paul Zimmerman | 5275455 | 2011-09-30 10:58:44 +0300 | [diff] [blame] | 1431 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1432 | return ret; |
| 1433 | } |
| 1434 | |
| 1435 | static int dwc3_gadget_ep_set_halt(struct usb_ep *ep, int value) |
| 1436 | { |
| 1437 | struct dwc3_ep *dep = to_dwc3_ep(ep); |
| 1438 | struct dwc3 *dwc = dep->dwc; |
| 1439 | |
| 1440 | unsigned long flags; |
| 1441 | |
| 1442 | int ret; |
| 1443 | |
Mayank Rana | a99689a | 2016-08-10 17:39:47 -0700 | [diff] [blame] | 1444 | if (!ep->desc) { |
| 1445 | dev_err(dwc->dev, "(%s)'s desc is NULL.\n", dep->name); |
| 1446 | return -EINVAL; |
| 1447 | } |
| 1448 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1449 | spin_lock_irqsave(&dwc->lock, flags); |
Felipe Balbi | 7a60855 | 2014-09-24 14:19:52 -0500 | [diff] [blame] | 1450 | ret = __dwc3_gadget_ep_set_halt(dep, value, false); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1451 | spin_unlock_irqrestore(&dwc->lock, flags); |
| 1452 | |
| 1453 | return ret; |
| 1454 | } |
| 1455 | |
| 1456 | static int dwc3_gadget_ep_set_wedge(struct usb_ep *ep) |
| 1457 | { |
| 1458 | struct dwc3_ep *dep = to_dwc3_ep(ep); |
Paul Zimmerman | 249a456 | 2012-02-24 17:32:16 -0800 | [diff] [blame] | 1459 | struct dwc3 *dwc = dep->dwc; |
| 1460 | unsigned long flags; |
Felipe Balbi | 95aa4e8 | 2014-09-24 10:50:14 -0500 | [diff] [blame] | 1461 | int ret; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1462 | |
Paul Zimmerman | 249a456 | 2012-02-24 17:32:16 -0800 | [diff] [blame] | 1463 | spin_lock_irqsave(&dwc->lock, flags); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1464 | dep->flags |= DWC3_EP_WEDGE; |
| 1465 | |
Pratyush Anand | 08f0d96 | 2012-06-25 22:40:43 +0530 | [diff] [blame] | 1466 | if (dep->number == 0 || dep->number == 1) |
Felipe Balbi | 95aa4e8 | 2014-09-24 10:50:14 -0500 | [diff] [blame] | 1467 | ret = __dwc3_gadget_ep0_set_halt(ep, 1); |
Pratyush Anand | 08f0d96 | 2012-06-25 22:40:43 +0530 | [diff] [blame] | 1468 | else |
Felipe Balbi | 7a60855 | 2014-09-24 14:19:52 -0500 | [diff] [blame] | 1469 | ret = __dwc3_gadget_ep_set_halt(dep, 1, false); |
Felipe Balbi | 95aa4e8 | 2014-09-24 10:50:14 -0500 | [diff] [blame] | 1470 | spin_unlock_irqrestore(&dwc->lock, flags); |
| 1471 | |
| 1472 | return ret; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1473 | } |
| 1474 | |
| 1475 | /* -------------------------------------------------------------------------- */ |
| 1476 | |
| 1477 | static struct usb_endpoint_descriptor dwc3_gadget_ep0_desc = { |
| 1478 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 1479 | .bDescriptorType = USB_DT_ENDPOINT, |
| 1480 | .bmAttributes = USB_ENDPOINT_XFER_CONTROL, |
| 1481 | }; |
| 1482 | |
| 1483 | static const struct usb_ep_ops dwc3_gadget_ep0_ops = { |
| 1484 | .enable = dwc3_gadget_ep0_enable, |
| 1485 | .disable = dwc3_gadget_ep0_disable, |
| 1486 | .alloc_request = dwc3_gadget_ep_alloc_request, |
| 1487 | .free_request = dwc3_gadget_ep_free_request, |
| 1488 | .queue = dwc3_gadget_ep0_queue, |
| 1489 | .dequeue = dwc3_gadget_ep_dequeue, |
Pratyush Anand | 08f0d96 | 2012-06-25 22:40:43 +0530 | [diff] [blame] | 1490 | .set_halt = dwc3_gadget_ep0_set_halt, |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1491 | .set_wedge = dwc3_gadget_ep_set_wedge, |
| 1492 | }; |
| 1493 | |
| 1494 | static const struct usb_ep_ops dwc3_gadget_ep_ops = { |
| 1495 | .enable = dwc3_gadget_ep_enable, |
| 1496 | .disable = dwc3_gadget_ep_disable, |
| 1497 | .alloc_request = dwc3_gadget_ep_alloc_request, |
| 1498 | .free_request = dwc3_gadget_ep_free_request, |
| 1499 | .queue = dwc3_gadget_ep_queue, |
| 1500 | .dequeue = dwc3_gadget_ep_dequeue, |
| 1501 | .set_halt = dwc3_gadget_ep_set_halt, |
| 1502 | .set_wedge = dwc3_gadget_ep_set_wedge, |
| 1503 | }; |
| 1504 | |
| 1505 | /* -------------------------------------------------------------------------- */ |
| 1506 | |
| 1507 | static int dwc3_gadget_get_frame(struct usb_gadget *g) |
| 1508 | { |
| 1509 | struct dwc3 *dwc = gadget_to_dwc(g); |
| 1510 | u32 reg; |
| 1511 | |
| 1512 | reg = dwc3_readl(dwc->regs, DWC3_DSTS); |
| 1513 | return DWC3_DSTS_SOFFN(reg); |
| 1514 | } |
| 1515 | |
Felipe Balbi | 218ef7b | 2016-04-04 11:24:04 +0300 | [diff] [blame] | 1516 | static int __dwc3_gadget_wakeup(struct dwc3 *dwc) |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1517 | { |
Nicolas Saenz Julienne | d6011f6 | 2016-08-16 10:22:38 +0100 | [diff] [blame] | 1518 | int retries; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1519 | |
Felipe Balbi | 218ef7b | 2016-04-04 11:24:04 +0300 | [diff] [blame] | 1520 | int ret; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1521 | u32 reg; |
| 1522 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1523 | u8 link_state; |
| 1524 | u8 speed; |
| 1525 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1526 | /* |
| 1527 | * According to the Databook Remote wakeup request should |
| 1528 | * be issued only when the device is in early suspend state. |
| 1529 | * |
| 1530 | * We can check that via USB Link State bits in DSTS register. |
| 1531 | */ |
| 1532 | reg = dwc3_readl(dwc->regs, DWC3_DSTS); |
| 1533 | |
| 1534 | speed = reg & DWC3_DSTS_CONNECTSPD; |
John Youn | ee5cd41 | 2016-02-05 17:08:45 -0800 | [diff] [blame] | 1535 | if ((speed == DWC3_DSTS_SUPERSPEED) || |
| 1536 | (speed == DWC3_DSTS_SUPERSPEED_PLUS)) { |
Felipe Balbi | 60cfb37 | 2016-05-24 13:45:17 +0300 | [diff] [blame] | 1537 | dwc3_trace(trace_dwc3_gadget, "no wakeup on SuperSpeed"); |
Felipe Balbi | 6b74289 | 2016-05-13 10:19:42 +0300 | [diff] [blame] | 1538 | return 0; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1539 | } |
| 1540 | |
| 1541 | link_state = DWC3_DSTS_USBLNKST(reg); |
| 1542 | |
| 1543 | switch (link_state) { |
| 1544 | case DWC3_LINK_STATE_RX_DET: /* in HS, means Early Suspend */ |
| 1545 | case DWC3_LINK_STATE_U3: /* in HS, means SUSPEND */ |
| 1546 | break; |
| 1547 | default: |
Felipe Balbi | ec5e795 | 2015-11-16 16:04:13 -0600 | [diff] [blame] | 1548 | dwc3_trace(trace_dwc3_gadget, |
Felipe Balbi | 60cfb37 | 2016-05-24 13:45:17 +0300 | [diff] [blame] | 1549 | "can't wakeup from '%s'", |
Felipe Balbi | ec5e795 | 2015-11-16 16:04:13 -0600 | [diff] [blame] | 1550 | dwc3_gadget_link_string(link_state)); |
Felipe Balbi | 218ef7b | 2016-04-04 11:24:04 +0300 | [diff] [blame] | 1551 | return -EINVAL; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1552 | } |
| 1553 | |
Felipe Balbi | 8598bde | 2012-01-02 18:55:57 +0200 | [diff] [blame] | 1554 | ret = dwc3_gadget_set_link_state(dwc, DWC3_LINK_STATE_RECOV); |
| 1555 | if (ret < 0) { |
| 1556 | dev_err(dwc->dev, "failed to put link in Recovery\n"); |
Felipe Balbi | 218ef7b | 2016-04-04 11:24:04 +0300 | [diff] [blame] | 1557 | return ret; |
Felipe Balbi | 8598bde | 2012-01-02 18:55:57 +0200 | [diff] [blame] | 1558 | } |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1559 | |
Paul Zimmerman | 802fde9 | 2012-04-27 13:10:52 +0300 | [diff] [blame] | 1560 | /* Recent versions do this automatically */ |
| 1561 | if (dwc->revision < DWC3_REVISION_194A) { |
| 1562 | /* write zeroes to Link Change Request */ |
Felipe Balbi | fcc023c | 2012-05-24 10:27:56 +0300 | [diff] [blame] | 1563 | reg = dwc3_readl(dwc->regs, DWC3_DCTL); |
Paul Zimmerman | 802fde9 | 2012-04-27 13:10:52 +0300 | [diff] [blame] | 1564 | reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK; |
| 1565 | dwc3_writel(dwc->regs, DWC3_DCTL, reg); |
| 1566 | } |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1567 | |
Paul Zimmerman | 1d04679 | 2012-02-15 18:56:56 -0800 | [diff] [blame] | 1568 | /* poll until Link State changes to ON */ |
Nicolas Saenz Julienne | d6011f6 | 2016-08-16 10:22:38 +0100 | [diff] [blame] | 1569 | retries = 20000; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1570 | |
Nicolas Saenz Julienne | d6011f6 | 2016-08-16 10:22:38 +0100 | [diff] [blame] | 1571 | while (retries--) { |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1572 | reg = dwc3_readl(dwc->regs, DWC3_DSTS); |
| 1573 | |
| 1574 | /* in HS, means ON */ |
| 1575 | if (DWC3_DSTS_USBLNKST(reg) == DWC3_LINK_STATE_U0) |
| 1576 | break; |
| 1577 | } |
| 1578 | |
| 1579 | if (DWC3_DSTS_USBLNKST(reg) != DWC3_LINK_STATE_U0) { |
| 1580 | dev_err(dwc->dev, "failed to send remote wakeup\n"); |
Felipe Balbi | 218ef7b | 2016-04-04 11:24:04 +0300 | [diff] [blame] | 1581 | return -EINVAL; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1582 | } |
| 1583 | |
Felipe Balbi | 218ef7b | 2016-04-04 11:24:04 +0300 | [diff] [blame] | 1584 | return 0; |
| 1585 | } |
| 1586 | |
Mayank Rana | a99689a | 2016-08-10 17:39:47 -0700 | [diff] [blame] | 1587 | #define DWC3_PM_RESUME_RETRIES 20 /* Max Number of retries */ |
| 1588 | #define DWC3_PM_RESUME_DELAY 100 /* 100 msec */ |
| 1589 | |
| 1590 | static void dwc3_gadget_wakeup_work(struct work_struct *w) |
Felipe Balbi | 218ef7b | 2016-04-04 11:24:04 +0300 | [diff] [blame] | 1591 | { |
Mayank Rana | a99689a | 2016-08-10 17:39:47 -0700 | [diff] [blame] | 1592 | struct dwc3 *dwc; |
Felipe Balbi | 218ef7b | 2016-04-04 11:24:04 +0300 | [diff] [blame] | 1593 | int ret; |
Mayank Rana | a99689a | 2016-08-10 17:39:47 -0700 | [diff] [blame] | 1594 | static int retry_count; |
| 1595 | |
| 1596 | dwc = container_of(w, struct dwc3, wakeup_work); |
| 1597 | |
| 1598 | ret = pm_runtime_get_sync(dwc->dev); |
| 1599 | if (ret) { |
| 1600 | /* pm_runtime_get_sync returns -EACCES error between |
| 1601 | * late_suspend and early_resume, wait for system resume to |
| 1602 | * finish and queue work again |
| 1603 | */ |
| 1604 | pr_debug("PM runtime get sync failed, ret %d\n", ret); |
| 1605 | if (ret == -EACCES) { |
| 1606 | pm_runtime_put_noidle(dwc->dev); |
| 1607 | if (retry_count == DWC3_PM_RESUME_RETRIES) { |
| 1608 | retry_count = 0; |
| 1609 | pr_err("pm_runtime_get_sync timed out\n"); |
| 1610 | return; |
| 1611 | } |
| 1612 | msleep(DWC3_PM_RESUME_DELAY); |
| 1613 | retry_count++; |
| 1614 | schedule_work(&dwc->wakeup_work); |
| 1615 | return; |
| 1616 | } |
| 1617 | } |
| 1618 | retry_count = 0; |
| 1619 | |
| 1620 | ret = dwc3_gadget_wakeup_int(dwc); |
| 1621 | |
| 1622 | if (ret) |
| 1623 | pr_err("Remote wakeup failed. ret = %d.\n", ret); |
| 1624 | else |
| 1625 | pr_debug("Remote wakeup succeeded.\n"); |
| 1626 | |
| 1627 | pm_runtime_put_noidle(dwc->dev); |
| 1628 | } |
| 1629 | |
| 1630 | static int dwc3_gadget_wakeup_int(struct dwc3 *dwc) |
| 1631 | { |
| 1632 | bool link_recover_only = false; |
| 1633 | |
| 1634 | u32 reg; |
| 1635 | int ret = 0; |
| 1636 | u8 link_state; |
| 1637 | unsigned long flags; |
| 1638 | |
| 1639 | pr_debug("%s(): Entry\n", __func__); |
| 1640 | disable_irq(dwc->irq); |
| 1641 | spin_lock_irqsave(&dwc->lock, flags); |
| 1642 | /* |
| 1643 | * According to the Databook Remote wakeup request should |
| 1644 | * be issued only when the device is in early suspend state. |
| 1645 | * |
| 1646 | * We can check that via USB Link State bits in DSTS register. |
| 1647 | */ |
| 1648 | link_state = dwc3_get_link_state(dwc); |
| 1649 | |
| 1650 | switch (link_state) { |
| 1651 | case DWC3_LINK_STATE_RX_DET: /* in HS, means Early Suspend */ |
| 1652 | case DWC3_LINK_STATE_U3: /* in HS, means SUSPEND */ |
| 1653 | break; |
| 1654 | case DWC3_LINK_STATE_U1: |
| 1655 | if (dwc->gadget.speed != USB_SPEED_SUPER) { |
| 1656 | link_recover_only = true; |
| 1657 | break; |
| 1658 | } |
| 1659 | /* Intentional fallthrough */ |
| 1660 | default: |
| 1661 | dev_dbg(dwc->dev, "can't wakeup from link state %d\n", |
| 1662 | link_state); |
| 1663 | ret = -EINVAL; |
| 1664 | goto out; |
| 1665 | } |
| 1666 | |
| 1667 | /* Enable LINK STATUS change event */ |
| 1668 | reg = dwc3_readl(dwc->regs, DWC3_DEVTEN); |
| 1669 | reg |= DWC3_DEVTEN_ULSTCNGEN; |
| 1670 | dwc3_writel(dwc->regs, DWC3_DEVTEN, reg); |
| 1671 | /* |
| 1672 | * memory barrier is required to make sure that required events |
| 1673 | * with core is enabled before performing RECOVERY mechnism. |
| 1674 | */ |
| 1675 | mb(); |
| 1676 | |
| 1677 | ret = dwc3_gadget_set_link_state(dwc, DWC3_LINK_STATE_RECOV); |
| 1678 | if (ret < 0) { |
| 1679 | dev_err(dwc->dev, "failed to put link in Recovery\n"); |
| 1680 | /* Disable LINK STATUS change */ |
| 1681 | reg = dwc3_readl(dwc->regs, DWC3_DEVTEN); |
| 1682 | reg &= ~DWC3_DEVTEN_ULSTCNGEN; |
| 1683 | dwc3_writel(dwc->regs, DWC3_DEVTEN, reg); |
| 1684 | /* Required to complete this operation before returning */ |
| 1685 | mb(); |
| 1686 | goto out; |
| 1687 | } |
| 1688 | |
| 1689 | /* Recent versions do this automatically */ |
| 1690 | if (dwc->revision < DWC3_REVISION_194A) { |
| 1691 | /* write zeroes to Link Change Request */ |
| 1692 | reg = dwc3_readl(dwc->regs, DWC3_DCTL); |
| 1693 | reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK; |
| 1694 | dwc3_writel(dwc->regs, DWC3_DCTL, reg); |
| 1695 | } |
| 1696 | |
| 1697 | spin_unlock_irqrestore(&dwc->lock, flags); |
| 1698 | enable_irq(dwc->irq); |
| 1699 | |
| 1700 | /* |
| 1701 | * Have bigger value (16 sec) for timeout since some host PCs driving |
| 1702 | * resume for very long time (e.g. 8 sec) |
| 1703 | */ |
| 1704 | ret = wait_event_interruptible_timeout(dwc->wait_linkstate, |
| 1705 | (dwc->link_state < DWC3_LINK_STATE_U3) || |
| 1706 | (dwc->link_state == DWC3_LINK_STATE_SS_DIS), |
| 1707 | msecs_to_jiffies(16000)); |
Felipe Balbi | 218ef7b | 2016-04-04 11:24:04 +0300 | [diff] [blame] | 1708 | |
| 1709 | spin_lock_irqsave(&dwc->lock, flags); |
Mayank Rana | a99689a | 2016-08-10 17:39:47 -0700 | [diff] [blame] | 1710 | /* Disable link status change event */ |
| 1711 | reg = dwc3_readl(dwc->regs, DWC3_DEVTEN); |
| 1712 | reg &= ~DWC3_DEVTEN_ULSTCNGEN; |
| 1713 | dwc3_writel(dwc->regs, DWC3_DEVTEN, reg); |
| 1714 | /* |
| 1715 | * Complete this write before we go ahead and perform resume |
| 1716 | * as we don't need link status change notificaiton anymore. |
| 1717 | */ |
| 1718 | mb(); |
| 1719 | |
| 1720 | if (!ret) { |
| 1721 | dev_dbg(dwc->dev, "Timeout moving into state(%d)\n", |
| 1722 | dwc->link_state); |
| 1723 | ret = -EINVAL; |
| 1724 | spin_unlock_irqrestore(&dwc->lock, flags); |
| 1725 | goto out1; |
| 1726 | } else { |
| 1727 | ret = 0; |
| 1728 | /* |
| 1729 | * If USB is disconnected OR received RESET from host, |
| 1730 | * don't perform resume |
| 1731 | */ |
| 1732 | if (dwc->link_state == DWC3_LINK_STATE_SS_DIS || |
| 1733 | dwc->gadget.state == USB_STATE_DEFAULT) |
| 1734 | link_recover_only = true; |
| 1735 | } |
| 1736 | |
| 1737 | /* |
| 1738 | * According to DWC3 databook, the controller does not |
| 1739 | * trigger a wakeup event when remote-wakeup is used. |
| 1740 | * Hence, after remote-wakeup sequence is complete, and |
| 1741 | * the device is back at U0 state, it is required that |
| 1742 | * the resume sequence is initiated by SW. |
| 1743 | */ |
| 1744 | if (!link_recover_only) |
| 1745 | dwc3_gadget_wakeup_interrupt(dwc, true); |
| 1746 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1747 | spin_unlock_irqrestore(&dwc->lock, flags); |
Mayank Rana | a99689a | 2016-08-10 17:39:47 -0700 | [diff] [blame] | 1748 | pr_debug("%s: Exit\n", __func__); |
| 1749 | return ret; |
| 1750 | |
| 1751 | out: |
| 1752 | spin_unlock_irqrestore(&dwc->lock, flags); |
| 1753 | enable_irq(dwc->irq); |
| 1754 | |
| 1755 | out1: |
| 1756 | return ret; |
| 1757 | } |
| 1758 | |
| 1759 | static int dwc_gadget_func_wakeup(struct usb_gadget *g, int interface_id) |
| 1760 | { |
| 1761 | int ret = 0; |
| 1762 | struct dwc3 *dwc = gadget_to_dwc(g); |
| 1763 | |
| 1764 | if (!g || (g->speed != USB_SPEED_SUPER)) |
| 1765 | return -ENOTSUPP; |
| 1766 | |
| 1767 | if (dwc3_gadget_is_suspended(dwc)) { |
| 1768 | pr_debug("USB bus is suspended. Scheduling wakeup and returning -EAGAIN.\n"); |
| 1769 | dwc3_gadget_wakeup(&dwc->gadget); |
| 1770 | return -EAGAIN; |
| 1771 | } |
| 1772 | |
| 1773 | if (dwc->revision < DWC3_REVISION_220A) { |
| 1774 | ret = dwc3_send_gadget_generic_command(dwc, |
| 1775 | DWC3_DGCMD_XMIT_FUNCTION, interface_id); |
| 1776 | } else { |
| 1777 | ret = dwc3_send_gadget_generic_command(dwc, |
| 1778 | DWC3_DGCMD_XMIT_DEV, 0x1 | (interface_id << 4)); |
| 1779 | } |
| 1780 | |
| 1781 | if (ret) |
| 1782 | pr_err("Function wakeup HW command failed.\n"); |
| 1783 | else |
| 1784 | pr_debug("Function wakeup HW command succeeded.\n"); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1785 | |
| 1786 | return ret; |
| 1787 | } |
| 1788 | |
| 1789 | static int dwc3_gadget_set_selfpowered(struct usb_gadget *g, |
| 1790 | int is_selfpowered) |
| 1791 | { |
| 1792 | struct dwc3 *dwc = gadget_to_dwc(g); |
Paul Zimmerman | 249a456 | 2012-02-24 17:32:16 -0800 | [diff] [blame] | 1793 | unsigned long flags; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1794 | |
Paul Zimmerman | 249a456 | 2012-02-24 17:32:16 -0800 | [diff] [blame] | 1795 | spin_lock_irqsave(&dwc->lock, flags); |
Peter Chen | bcdea50 | 2015-01-28 16:32:40 +0800 | [diff] [blame] | 1796 | g->is_selfpowered = !!is_selfpowered; |
Paul Zimmerman | 249a456 | 2012-02-24 17:32:16 -0800 | [diff] [blame] | 1797 | spin_unlock_irqrestore(&dwc->lock, flags); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1798 | |
| 1799 | return 0; |
| 1800 | } |
| 1801 | |
Mayank Rana | a99689a | 2016-08-10 17:39:47 -0700 | [diff] [blame] | 1802 | #define DWC3_SOFT_RESET_TIMEOUT 10 /* 10 msec */ |
Felipe Balbi | 7b2a036 | 2013-12-19 13:43:19 -0600 | [diff] [blame] | 1803 | 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] | 1804 | { |
| 1805 | u32 reg; |
Mayank Rana | a99689a | 2016-08-10 17:39:47 -0700 | [diff] [blame] | 1806 | u32 timeout = 1500; |
Felipe Balbi | fc8bb91 | 2016-05-16 13:14:48 +0300 | [diff] [blame] | 1807 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1808 | reg = dwc3_readl(dwc->regs, DWC3_DCTL); |
Felipe Balbi | 8db7ed1 | 2012-01-18 18:32:29 +0200 | [diff] [blame] | 1809 | if (is_on) { |
Paul Zimmerman | 802fde9 | 2012-04-27 13:10:52 +0300 | [diff] [blame] | 1810 | if (dwc->revision <= DWC3_REVISION_187A) { |
| 1811 | reg &= ~DWC3_DCTL_TRGTULST_MASK; |
| 1812 | reg |= DWC3_DCTL_TRGTULST_RX_DET; |
| 1813 | } |
| 1814 | |
| 1815 | if (dwc->revision >= DWC3_REVISION_194A) |
| 1816 | reg &= ~DWC3_DCTL_KEEP_CONNECT; |
Mayank Rana | a99689a | 2016-08-10 17:39:47 -0700 | [diff] [blame] | 1817 | |
| 1818 | dwc3_event_buffers_setup(dwc); |
| 1819 | dwc3_gadget_restart(dwc); |
| 1820 | reg = dwc3_readl(dwc->regs, DWC3_DCTL); |
Paul Zimmerman | 802fde9 | 2012-04-27 13:10:52 +0300 | [diff] [blame] | 1821 | reg |= DWC3_DCTL_RUN_STOP; |
Felipe Balbi | 7b2a036 | 2013-12-19 13:43:19 -0600 | [diff] [blame] | 1822 | |
| 1823 | if (dwc->has_hibernation) |
| 1824 | reg |= DWC3_DCTL_KEEP_CONNECT; |
| 1825 | |
Felipe Balbi | 9fcb3bd | 2013-02-08 17:55:58 +0200 | [diff] [blame] | 1826 | dwc->pullups_connected = true; |
Felipe Balbi | 8db7ed1 | 2012-01-18 18:32:29 +0200 | [diff] [blame] | 1827 | } else { |
Mayank Rana | a99689a | 2016-08-10 17:39:47 -0700 | [diff] [blame] | 1828 | dwc3_gadget_disable_irq(dwc); |
| 1829 | __dwc3_gadget_ep_disable(dwc->eps[0]); |
| 1830 | __dwc3_gadget_ep_disable(dwc->eps[1]); |
| 1831 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1832 | reg &= ~DWC3_DCTL_RUN_STOP; |
Felipe Balbi | 7b2a036 | 2013-12-19 13:43:19 -0600 | [diff] [blame] | 1833 | |
| 1834 | if (dwc->has_hibernation && !suspend) |
| 1835 | reg &= ~DWC3_DCTL_KEEP_CONNECT; |
| 1836 | |
Felipe Balbi | 9fcb3bd | 2013-02-08 17:55:58 +0200 | [diff] [blame] | 1837 | dwc->pullups_connected = false; |
Felipe Balbi | 8db7ed1 | 2012-01-18 18:32:29 +0200 | [diff] [blame] | 1838 | } |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1839 | |
| 1840 | dwc3_writel(dwc->regs, DWC3_DCTL, reg); |
| 1841 | |
| 1842 | do { |
| 1843 | reg = dwc3_readl(dwc->regs, DWC3_DSTS); |
Felipe Balbi | b6d4e16 | 2016-06-09 16:47:05 +0300 | [diff] [blame] | 1844 | reg &= DWC3_DSTS_DEVCTRLHLT; |
| 1845 | } while (--timeout && !(!is_on ^ !reg)); |
Felipe Balbi | f2df679 | 2016-06-09 16:31:34 +0300 | [diff] [blame] | 1846 | |
Mayank Rana | a99689a | 2016-08-10 17:39:47 -0700 | [diff] [blame] | 1847 | if (!timeout) { |
| 1848 | dev_err(dwc->dev, "failed to %s controller\n", |
| 1849 | is_on ? "start" : "stop"); |
Felipe Balbi | f2df679 | 2016-06-09 16:31:34 +0300 | [diff] [blame] | 1850 | return -ETIMEDOUT; |
Mayank Rana | a99689a | 2016-08-10 17:39:47 -0700 | [diff] [blame] | 1851 | } |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1852 | |
Felipe Balbi | 7381528 | 2015-01-27 13:48:14 -0600 | [diff] [blame] | 1853 | dwc3_trace(trace_dwc3_gadget, "gadget %s data soft-%s", |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1854 | dwc->gadget_driver |
| 1855 | ? dwc->gadget_driver->function : "no-function", |
| 1856 | is_on ? "connect" : "disconnect"); |
Pratyush Anand | 6f17f74 | 2012-07-02 10:21:55 +0530 | [diff] [blame] | 1857 | |
| 1858 | return 0; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1859 | } |
| 1860 | |
Mayank Rana | a99689a | 2016-08-10 17:39:47 -0700 | [diff] [blame] | 1861 | static int dwc3_gadget_vbus_draw(struct usb_gadget *g, unsigned int mA) |
| 1862 | { |
| 1863 | struct dwc3 *dwc = gadget_to_dwc(g); |
| 1864 | |
| 1865 | dwc->vbus_draw = mA; |
| 1866 | dev_dbg(dwc->dev, "Notify controller from %s. mA = %u\n", __func__, mA); |
| 1867 | dwc3_notify_event(dwc, DWC3_CONTROLLER_SET_CURRENT_DRAW_EVENT); |
| 1868 | return 0; |
| 1869 | } |
| 1870 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1871 | static int dwc3_gadget_pullup(struct usb_gadget *g, int is_on) |
| 1872 | { |
| 1873 | struct dwc3 *dwc = gadget_to_dwc(g); |
| 1874 | unsigned long flags; |
Pratyush Anand | 6f17f74 | 2012-07-02 10:21:55 +0530 | [diff] [blame] | 1875 | int ret; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1876 | |
| 1877 | is_on = !!is_on; |
Mayank Rana | a99689a | 2016-08-10 17:39:47 -0700 | [diff] [blame] | 1878 | dwc->softconnect = is_on; |
| 1879 | if ((dwc->is_drd && !dwc->vbus_active) || !dwc->gadget_driver) { |
| 1880 | /* |
| 1881 | * Need to wait for vbus_session(on) from otg driver or to |
| 1882 | * the udc_start. |
| 1883 | */ |
| 1884 | return 0; |
| 1885 | } |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1886 | |
Mayank Rana | a99689a | 2016-08-10 17:39:47 -0700 | [diff] [blame] | 1887 | pm_runtime_get_sync(dwc->dev); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1888 | spin_lock_irqsave(&dwc->lock, flags); |
Mayank Rana | a99689a | 2016-08-10 17:39:47 -0700 | [diff] [blame] | 1889 | /* |
| 1890 | * If we are here after bus suspend notify otg state machine to |
| 1891 | * increment pm usage count of dwc to prevent pm_runtime_suspend |
| 1892 | * during enumeration. |
| 1893 | */ |
| 1894 | dwc->b_suspend = false; |
| 1895 | dwc3_notify_event(dwc, DWC3_CONTROLLER_NOTIFY_OTG_EVENT); |
Felipe Balbi | 7b2a036 | 2013-12-19 13:43:19 -0600 | [diff] [blame] | 1896 | ret = dwc3_gadget_run_stop(dwc, is_on, false); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1897 | spin_unlock_irqrestore(&dwc->lock, flags); |
| 1898 | |
Mayank Rana | a99689a | 2016-08-10 17:39:47 -0700 | [diff] [blame] | 1899 | pm_runtime_mark_last_busy(dwc->dev); |
| 1900 | pm_runtime_put_autosuspend(dwc->dev); |
| 1901 | |
Pratyush Anand | 6f17f74 | 2012-07-02 10:21:55 +0530 | [diff] [blame] | 1902 | return ret; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 1903 | } |
| 1904 | |
Mayank Rana | a99689a | 2016-08-10 17:39:47 -0700 | [diff] [blame] | 1905 | void dwc3_gadget_enable_irq(struct dwc3 *dwc) |
Felipe Balbi | 8698e2a | 2013-02-08 15:24:04 +0200 | [diff] [blame] | 1906 | { |
| 1907 | u32 reg; |
| 1908 | |
| 1909 | /* Enable all but Start and End of Frame IRQs */ |
| 1910 | reg = (DWC3_DEVTEN_VNDRDEVTSTRCVEDEN | |
| 1911 | DWC3_DEVTEN_EVNTOVERFLOWEN | |
| 1912 | DWC3_DEVTEN_CMDCMPLTEN | |
| 1913 | DWC3_DEVTEN_ERRTICERREN | |
| 1914 | DWC3_DEVTEN_WKUPEVTEN | |
Felipe Balbi | 8698e2a | 2013-02-08 15:24:04 +0200 | [diff] [blame] | 1915 | DWC3_DEVTEN_CONNECTDONEEN | |
| 1916 | DWC3_DEVTEN_USBRSTEN | |
| 1917 | DWC3_DEVTEN_DISCONNEVTEN); |
| 1918 | |
Mayank Rana | a99689a | 2016-08-10 17:39:47 -0700 | [diff] [blame] | 1919 | /* |
| 1920 | * Enable SUSPENDEVENT(BIT:6) for version 230A and above |
| 1921 | * else enable USB Link change event (BIT:3) for older version |
| 1922 | */ |
| 1923 | if (dwc->revision < DWC3_REVISION_230A) |
| 1924 | reg |= DWC3_DEVTEN_ULSTCNGEN; |
| 1925 | else |
| 1926 | reg |= DWC3_DEVTEN_SUSPEND; |
| 1927 | |
Felipe Balbi | 8698e2a | 2013-02-08 15:24:04 +0200 | [diff] [blame] | 1928 | dwc3_writel(dwc->regs, DWC3_DEVTEN, reg); |
| 1929 | } |
| 1930 | |
Mayank Rana | a99689a | 2016-08-10 17:39:47 -0700 | [diff] [blame] | 1931 | void dwc3_gadget_disable_irq(struct dwc3 *dwc) |
Felipe Balbi | 8698e2a | 2013-02-08 15:24:04 +0200 | [diff] [blame] | 1932 | { |
| 1933 | /* mask all interrupts */ |
| 1934 | dwc3_writel(dwc->regs, DWC3_DEVTEN, 0x00); |
| 1935 | } |
| 1936 | |
Felipe Balbi | b15a762 | 2011-06-30 16:57:15 +0300 | [diff] [blame] | 1937 | static irqreturn_t dwc3_thread_interrupt(int irq, void *_dwc); |
Mayank Rana | a99689a | 2016-08-10 17:39:47 -0700 | [diff] [blame] | 1938 | static void dwc3_gadget_disconnect_interrupt(struct dwc3 *dwc); |
Felipe Balbi | 8698e2a | 2013-02-08 15:24:04 +0200 | [diff] [blame] | 1939 | |
Felipe Balbi | 4e99472 | 2016-05-13 14:09:59 +0300 | [diff] [blame] | 1940 | /** |
| 1941 | * dwc3_gadget_setup_nump - Calculate and initialize NUMP field of DCFG |
| 1942 | * dwc: pointer to our context structure |
| 1943 | * |
| 1944 | * The following looks like complex but it's actually very simple. In order to |
| 1945 | * calculate the number of packets we can burst at once on OUT transfers, we're |
| 1946 | * gonna use RxFIFO size. |
| 1947 | * |
| 1948 | * To calculate RxFIFO size we need two numbers: |
| 1949 | * MDWIDTH = size, in bits, of the internal memory bus |
| 1950 | * RAM2_DEPTH = depth, in MDWIDTH, of internal RAM2 (where RxFIFO sits) |
| 1951 | * |
| 1952 | * Given these two numbers, the formula is simple: |
| 1953 | * |
| 1954 | * RxFIFO Size = (RAM2_DEPTH * MDWIDTH / 8) - 24 - 16; |
| 1955 | * |
| 1956 | * 24 bytes is for 3x SETUP packets |
| 1957 | * 16 bytes is a clock domain crossing tolerance |
| 1958 | * |
| 1959 | * Given RxFIFO Size, NUMP = RxFIFOSize / 1024; |
| 1960 | */ |
| 1961 | static void dwc3_gadget_setup_nump(struct dwc3 *dwc) |
| 1962 | { |
| 1963 | u32 ram2_depth; |
| 1964 | u32 mdwidth; |
| 1965 | u32 nump; |
| 1966 | u32 reg; |
| 1967 | |
| 1968 | ram2_depth = DWC3_GHWPARAMS7_RAM2_DEPTH(dwc->hwparams.hwparams7); |
| 1969 | mdwidth = DWC3_GHWPARAMS0_MDWIDTH(dwc->hwparams.hwparams0); |
| 1970 | |
| 1971 | nump = ((ram2_depth * mdwidth / 8) - 24 - 16) / 1024; |
| 1972 | nump = min_t(u32, nump, 16); |
| 1973 | |
| 1974 | /* update NumP */ |
| 1975 | reg = dwc3_readl(dwc->regs, DWC3_DCFG); |
| 1976 | reg &= ~DWC3_DCFG_NUMP_MASK; |
| 1977 | reg |= nump << DWC3_DCFG_NUMP_SHIFT; |
| 1978 | dwc3_writel(dwc->regs, DWC3_DCFG, reg); |
| 1979 | } |
| 1980 | |
Mayank Rana | a99689a | 2016-08-10 17:39:47 -0700 | [diff] [blame] | 1981 | static int dwc3_gadget_vbus_session(struct usb_gadget *_gadget, int is_active) |
| 1982 | { |
| 1983 | struct dwc3 *dwc = gadget_to_dwc(_gadget); |
| 1984 | unsigned long flags; |
| 1985 | |
| 1986 | if (!dwc->is_drd) |
| 1987 | return -EPERM; |
| 1988 | |
| 1989 | is_active = !!is_active; |
| 1990 | |
| 1991 | spin_lock_irqsave(&dwc->lock, flags); |
| 1992 | |
| 1993 | /* Mark that the vbus was powered */ |
| 1994 | dwc->vbus_active = is_active; |
| 1995 | |
| 1996 | /* |
| 1997 | * Check if upper level usb_gadget_driver was already registered with |
| 1998 | * this udc controller driver (if dwc3_gadget_start was called) |
| 1999 | */ |
| 2000 | if (dwc->gadget_driver && dwc->softconnect) { |
| 2001 | if (dwc->vbus_active) { |
| 2002 | /* |
| 2003 | * Both vbus was activated by otg and pullup was |
| 2004 | * signaled by the gadget driver. |
| 2005 | */ |
| 2006 | dwc3_gadget_run_stop(dwc, 1, false); |
| 2007 | } else { |
| 2008 | dwc3_gadget_run_stop(dwc, 0, false); |
| 2009 | } |
| 2010 | } |
| 2011 | |
| 2012 | /* |
| 2013 | * Clearing run/stop bit might occur before disconnect event is seen. |
| 2014 | * Make sure to let gadget driver know in that case. |
| 2015 | */ |
| 2016 | if (!dwc->vbus_active) { |
| 2017 | dev_dbg(dwc->dev, "calling disconnect from %s\n", __func__); |
| 2018 | dwc3_gadget_disconnect_interrupt(dwc); |
| 2019 | } |
| 2020 | |
| 2021 | spin_unlock_irqrestore(&dwc->lock, flags); |
| 2022 | return 0; |
| 2023 | } |
| 2024 | |
Felipe Balbi | d7be295 | 2016-05-04 15:49:37 +0300 | [diff] [blame] | 2025 | static int __dwc3_gadget_start(struct dwc3 *dwc) |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2026 | { |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2027 | struct dwc3_ep *dep; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2028 | int ret = 0; |
| 2029 | u32 reg; |
| 2030 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2031 | reg = dwc3_readl(dwc->regs, DWC3_DCFG); |
| 2032 | reg &= ~(DWC3_DCFG_SPEED_MASK); |
Felipe Balbi | 07e7f47 | 2012-03-23 12:20:31 +0200 | [diff] [blame] | 2033 | |
| 2034 | /** |
| 2035 | * WORKAROUND: DWC3 revision < 2.20a have an issue |
| 2036 | * which would cause metastability state on Run/Stop |
| 2037 | * bit if we try to force the IP to USB2-only mode. |
| 2038 | * |
| 2039 | * Because of that, we cannot configure the IP to any |
| 2040 | * speed other than the SuperSpeed |
| 2041 | * |
| 2042 | * Refers to: |
| 2043 | * |
| 2044 | * STAR#9000525659: Clock Domain Crossing on DCTL in |
| 2045 | * USB 2.0 Mode |
| 2046 | */ |
Felipe Balbi | f7e846f | 2013-06-30 14:29:51 +0300 | [diff] [blame] | 2047 | if (dwc->revision < DWC3_REVISION_220A) { |
Felipe Balbi | 07e7f47 | 2012-03-23 12:20:31 +0200 | [diff] [blame] | 2048 | reg |= DWC3_DCFG_SUPERSPEED; |
Felipe Balbi | f7e846f | 2013-06-30 14:29:51 +0300 | [diff] [blame] | 2049 | } else { |
| 2050 | switch (dwc->maximum_speed) { |
| 2051 | case USB_SPEED_LOW: |
John Youn | 2da9ad7 | 2016-05-20 16:34:26 -0700 | [diff] [blame] | 2052 | reg |= DWC3_DCFG_LOWSPEED; |
Felipe Balbi | f7e846f | 2013-06-30 14:29:51 +0300 | [diff] [blame] | 2053 | break; |
| 2054 | case USB_SPEED_FULL: |
John Youn | 2da9ad7 | 2016-05-20 16:34:26 -0700 | [diff] [blame] | 2055 | reg |= DWC3_DCFG_FULLSPEED1; |
Felipe Balbi | f7e846f | 2013-06-30 14:29:51 +0300 | [diff] [blame] | 2056 | break; |
| 2057 | case USB_SPEED_HIGH: |
John Youn | 2da9ad7 | 2016-05-20 16:34:26 -0700 | [diff] [blame] | 2058 | reg |= DWC3_DCFG_HIGHSPEED; |
Felipe Balbi | f7e846f | 2013-06-30 14:29:51 +0300 | [diff] [blame] | 2059 | break; |
John Youn | 7580862 | 2016-02-05 17:09:13 -0800 | [diff] [blame] | 2060 | case USB_SPEED_SUPER_PLUS: |
John Youn | 2da9ad7 | 2016-05-20 16:34:26 -0700 | [diff] [blame] | 2061 | reg |= DWC3_DCFG_SUPERSPEED_PLUS; |
John Youn | 7580862 | 2016-02-05 17:09:13 -0800 | [diff] [blame] | 2062 | break; |
Felipe Balbi | f7e846f | 2013-06-30 14:29:51 +0300 | [diff] [blame] | 2063 | default: |
John Youn | 77966eb | 2016-02-19 17:31:01 -0800 | [diff] [blame] | 2064 | dev_err(dwc->dev, "invalid dwc->maximum_speed (%d)\n", |
Mayank Rana | a99689a | 2016-08-10 17:39:47 -0700 | [diff] [blame] | 2065 | dwc->maximum_speed); |
John Youn | 77966eb | 2016-02-19 17:31:01 -0800 | [diff] [blame] | 2066 | /* fall through */ |
| 2067 | case USB_SPEED_SUPER: |
| 2068 | reg |= DWC3_DCFG_SUPERSPEED; |
| 2069 | break; |
Felipe Balbi | f7e846f | 2013-06-30 14:29:51 +0300 | [diff] [blame] | 2070 | } |
| 2071 | } |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2072 | dwc3_writel(dwc->regs, DWC3_DCFG, reg); |
| 2073 | |
Mayank Rana | a99689a | 2016-08-10 17:39:47 -0700 | [diff] [blame] | 2074 | /* Programs the number of outstanding pipelined transfer requests |
| 2075 | * the AXI master pushes to the AXI slave. |
Felipe Balbi | 2a58f9c | 2016-04-28 10:56:28 +0300 | [diff] [blame] | 2076 | */ |
Mayank Rana | a99689a | 2016-08-10 17:39:47 -0700 | [diff] [blame] | 2077 | if (dwc->revision >= DWC3_REVISION_270A) { |
| 2078 | reg = dwc3_readl(dwc->regs, DWC3_GSBUSCFG1); |
| 2079 | reg &= ~DWC3_GSBUSCFG1_PIPETRANSLIMIT_MASK; |
| 2080 | reg |= DWC3_GSBUSCFG1_PIPETRANSLIMIT(0xe); |
| 2081 | dwc3_writel(dwc->regs, DWC3_GSBUSCFG1, reg); |
| 2082 | } |
Felipe Balbi | 2a58f9c | 2016-04-28 10:56:28 +0300 | [diff] [blame] | 2083 | |
Felipe Balbi | 4e99472 | 2016-05-13 14:09:59 +0300 | [diff] [blame] | 2084 | dwc3_gadget_setup_nump(dwc); |
| 2085 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2086 | /* Start with SuperSpeed Default */ |
| 2087 | dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512); |
| 2088 | |
Mayank Rana | a99689a | 2016-08-10 17:39:47 -0700 | [diff] [blame] | 2089 | dwc->delayed_status = false; |
| 2090 | /* reinitialize physical ep0-1 */ |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2091 | dep = dwc->eps[0]; |
Mayank Rana | a99689a | 2016-08-10 17:39:47 -0700 | [diff] [blame] | 2092 | dep->flags = 0; |
| 2093 | dep->endpoint.maxburst = 1; |
Paul Zimmerman | 265b70a | 2013-12-19 12:38:49 -0600 | [diff] [blame] | 2094 | ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, false, |
| 2095 | false); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2096 | if (ret) { |
| 2097 | dev_err(dwc->dev, "failed to enable %s\n", dep->name); |
Mayank Rana | a99689a | 2016-08-10 17:39:47 -0700 | [diff] [blame] | 2098 | return ret; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2099 | } |
| 2100 | |
| 2101 | dep = dwc->eps[1]; |
Mayank Rana | a99689a | 2016-08-10 17:39:47 -0700 | [diff] [blame] | 2102 | dep->flags = 0; |
| 2103 | dep->endpoint.maxburst = 1; |
Paul Zimmerman | 265b70a | 2013-12-19 12:38:49 -0600 | [diff] [blame] | 2104 | ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, false, |
| 2105 | false); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2106 | if (ret) { |
| 2107 | dev_err(dwc->dev, "failed to enable %s\n", dep->name); |
Mayank Rana | a99689a | 2016-08-10 17:39:47 -0700 | [diff] [blame] | 2108 | __dwc3_gadget_ep_disable(dwc->eps[0]); |
| 2109 | return ret; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2110 | } |
| 2111 | |
| 2112 | /* begin to receive SETUP packets */ |
Felipe Balbi | c7fcdeb | 2011-08-27 22:28:36 +0300 | [diff] [blame] | 2113 | dwc->ep0state = EP0_SETUP_PHASE; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2114 | dwc3_ep0_out_start(dwc); |
| 2115 | |
Felipe Balbi | 8698e2a | 2013-02-08 15:24:04 +0200 | [diff] [blame] | 2116 | dwc3_gadget_enable_irq(dwc); |
| 2117 | |
Felipe Balbi | d7be295 | 2016-05-04 15:49:37 +0300 | [diff] [blame] | 2118 | return ret; |
| 2119 | } |
| 2120 | |
Mayank Rana | a99689a | 2016-08-10 17:39:47 -0700 | [diff] [blame] | 2121 | /* Required gadget re-initialization before switching to gadget in OTG mode */ |
| 2122 | void dwc3_gadget_restart(struct dwc3 *dwc) |
| 2123 | { |
| 2124 | __dwc3_gadget_start(dwc); |
| 2125 | } |
| 2126 | |
Felipe Balbi | d7be295 | 2016-05-04 15:49:37 +0300 | [diff] [blame] | 2127 | static int dwc3_gadget_start(struct usb_gadget *g, |
| 2128 | struct usb_gadget_driver *driver) |
| 2129 | { |
| 2130 | struct dwc3 *dwc = gadget_to_dwc(g); |
| 2131 | unsigned long flags; |
| 2132 | int ret = 0; |
Felipe Balbi | d7be295 | 2016-05-04 15:49:37 +0300 | [diff] [blame] | 2133 | |
| 2134 | spin_lock_irqsave(&dwc->lock, flags); |
Mayank Rana | a99689a | 2016-08-10 17:39:47 -0700 | [diff] [blame] | 2135 | |
Felipe Balbi | d7be295 | 2016-05-04 15:49:37 +0300 | [diff] [blame] | 2136 | if (dwc->gadget_driver) { |
| 2137 | dev_err(dwc->dev, "%s is already bound to %s\n", |
| 2138 | dwc->gadget.name, |
| 2139 | dwc->gadget_driver->driver.name); |
| 2140 | ret = -EBUSY; |
Mayank Rana | a99689a | 2016-08-10 17:39:47 -0700 | [diff] [blame] | 2141 | goto err0; |
Felipe Balbi | d7be295 | 2016-05-04 15:49:37 +0300 | [diff] [blame] | 2142 | } |
| 2143 | |
| 2144 | dwc->gadget_driver = driver; |
| 2145 | |
Mayank Rana | a99689a | 2016-08-10 17:39:47 -0700 | [diff] [blame] | 2146 | /* |
| 2147 | * For DRD, this might get called by gadget driver during bootup |
| 2148 | * even though host mode might be active. Don't actually perform |
| 2149 | * device-specific initialization until device mode is activated. |
| 2150 | * In that case dwc3_gadget_restart() will handle it. |
| 2151 | */ |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2152 | spin_unlock_irqrestore(&dwc->lock, flags); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2153 | return 0; |
| 2154 | |
Felipe Balbi | b0d7ffd | 2013-06-27 10:00:18 +0300 | [diff] [blame] | 2155 | err0: |
Mayank Rana | a99689a | 2016-08-10 17:39:47 -0700 | [diff] [blame] | 2156 | spin_unlock_irqrestore(&dwc->lock, flags); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2157 | return ret; |
| 2158 | } |
| 2159 | |
Felipe Balbi | d7be295 | 2016-05-04 15:49:37 +0300 | [diff] [blame] | 2160 | static void __dwc3_gadget_stop(struct dwc3 *dwc) |
| 2161 | { |
| 2162 | dwc3_gadget_disable_irq(dwc); |
| 2163 | __dwc3_gadget_ep_disable(dwc->eps[0]); |
| 2164 | __dwc3_gadget_ep_disable(dwc->eps[1]); |
| 2165 | } |
| 2166 | |
Felipe Balbi | 22835b8 | 2014-10-17 12:05:12 -0500 | [diff] [blame] | 2167 | static int dwc3_gadget_stop(struct usb_gadget *g) |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2168 | { |
Mayank Rana | a99689a | 2016-08-10 17:39:47 -0700 | [diff] [blame] | 2169 | struct dwc3 *dwc = gadget_to_dwc(g); |
| 2170 | unsigned long flags; |
| 2171 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2172 | spin_lock_irqsave(&dwc->lock, flags); |
Mayank Rana | a99689a | 2016-08-10 17:39:47 -0700 | [diff] [blame] | 2173 | dwc->gadget_driver = NULL; |
Mayank Rana | bb7c0d5 | 2016-11-10 10:15:44 -0800 | [diff] [blame] | 2174 | spin_unlock_irqrestore(&dwc->lock, flags); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2175 | |
| 2176 | return 0; |
| 2177 | } |
Paul Zimmerman | 802fde9 | 2012-04-27 13:10:52 +0300 | [diff] [blame] | 2178 | |
Mayank Rana | a99689a | 2016-08-10 17:39:47 -0700 | [diff] [blame] | 2179 | static int dwc3_gadget_restart_usb_session(struct usb_gadget *g) |
| 2180 | { |
| 2181 | struct dwc3 *dwc = gadget_to_dwc(g); |
| 2182 | |
| 2183 | return dwc3_notify_event(dwc, DWC3_CONTROLLER_RESTART_USB_SESSION); |
| 2184 | } |
| 2185 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2186 | static const struct usb_gadget_ops dwc3_gadget_ops = { |
| 2187 | .get_frame = dwc3_gadget_get_frame, |
| 2188 | .wakeup = dwc3_gadget_wakeup, |
Mayank Rana | a99689a | 2016-08-10 17:39:47 -0700 | [diff] [blame] | 2189 | .func_wakeup = dwc_gadget_func_wakeup, |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2190 | .set_selfpowered = dwc3_gadget_set_selfpowered, |
Mayank Rana | a99689a | 2016-08-10 17:39:47 -0700 | [diff] [blame] | 2191 | .vbus_session = dwc3_gadget_vbus_session, |
| 2192 | .vbus_draw = dwc3_gadget_vbus_draw, |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2193 | .pullup = dwc3_gadget_pullup, |
| 2194 | .udc_start = dwc3_gadget_start, |
| 2195 | .udc_stop = dwc3_gadget_stop, |
Mayank Rana | a99689a | 2016-08-10 17:39:47 -0700 | [diff] [blame] | 2196 | .restart = dwc3_gadget_restart_usb_session, |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2197 | }; |
| 2198 | |
| 2199 | /* -------------------------------------------------------------------------- */ |
| 2200 | |
Felipe Balbi | 6a1e3ef | 2011-05-05 16:21:59 +0300 | [diff] [blame] | 2201 | static int dwc3_gadget_init_hw_endpoints(struct dwc3 *dwc, |
| 2202 | u8 num, u32 direction) |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2203 | { |
| 2204 | struct dwc3_ep *dep; |
Felipe Balbi | 6a1e3ef | 2011-05-05 16:21:59 +0300 | [diff] [blame] | 2205 | u8 i; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2206 | |
Felipe Balbi | 6a1e3ef | 2011-05-05 16:21:59 +0300 | [diff] [blame] | 2207 | for (i = 0; i < num; i++) { |
John Youn | d07fa66 | 2016-05-23 11:32:43 -0700 | [diff] [blame] | 2208 | u8 epnum = (i << 1) | (direction ? 1 : 0); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2209 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2210 | dep = kzalloc(sizeof(*dep), GFP_KERNEL); |
Jingoo Han | 734d5a5 | 2014-07-17 12:45:11 +0900 | [diff] [blame] | 2211 | if (!dep) |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2212 | return -ENOMEM; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2213 | |
| 2214 | dep->dwc = dwc; |
| 2215 | dep->number = epnum; |
Felipe Balbi | 9aa62ae | 2013-07-12 19:10:59 +0300 | [diff] [blame] | 2216 | dep->direction = !!direction; |
Felipe Balbi | 2eb8801 | 2016-04-12 16:53:39 +0300 | [diff] [blame] | 2217 | dep->regs = dwc->regs + DWC3_DEP_BASE(epnum); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2218 | dwc->eps[epnum] = dep; |
| 2219 | |
| 2220 | snprintf(dep->name, sizeof(dep->name), "ep%d%s", epnum >> 1, |
| 2221 | (epnum & 1) ? "in" : "out"); |
Felipe Balbi | 6a1e3ef | 2011-05-05 16:21:59 +0300 | [diff] [blame] | 2222 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2223 | dep->endpoint.name = dep->name; |
Felipe Balbi | 74674cb | 2016-04-13 16:44:39 +0300 | [diff] [blame] | 2224 | spin_lock_init(&dep->lock); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2225 | |
Felipe Balbi | 7381528 | 2015-01-27 13:48:14 -0600 | [diff] [blame] | 2226 | dwc3_trace(trace_dwc3_gadget, "initializing %s", dep->name); |
Felipe Balbi | 653df35 | 2013-07-12 19:11:57 +0300 | [diff] [blame] | 2227 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2228 | if (epnum == 0 || epnum == 1) { |
Robert Baldyga | e117e74 | 2013-12-13 12:23:38 +0100 | [diff] [blame] | 2229 | usb_ep_set_maxpacket_limit(&dep->endpoint, 512); |
Pratyush Anand | 6048e4c | 2013-01-18 16:53:56 +0530 | [diff] [blame] | 2230 | dep->endpoint.maxburst = 1; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2231 | dep->endpoint.ops = &dwc3_gadget_ep0_ops; |
| 2232 | if (!epnum) |
| 2233 | dwc->gadget.ep0 = &dep->endpoint; |
| 2234 | } else { |
| 2235 | int ret; |
| 2236 | |
Robert Baldyga | e117e74 | 2013-12-13 12:23:38 +0100 | [diff] [blame] | 2237 | usb_ep_set_maxpacket_limit(&dep->endpoint, 1024); |
Sebastian Andrzej Siewior | 12d36c1 | 2011-11-03 20:27:50 +0100 | [diff] [blame] | 2238 | dep->endpoint.max_streams = 15; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2239 | dep->endpoint.ops = &dwc3_gadget_ep_ops; |
| 2240 | list_add_tail(&dep->endpoint.ep_list, |
| 2241 | &dwc->gadget.ep_list); |
| 2242 | |
| 2243 | ret = dwc3_alloc_trb_pool(dep); |
Felipe Balbi | 25b8ff6 | 2011-11-04 12:32:47 +0200 | [diff] [blame] | 2244 | if (ret) |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2245 | return ret; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2246 | } |
Felipe Balbi | 25b8ff6 | 2011-11-04 12:32:47 +0200 | [diff] [blame] | 2247 | |
Robert Baldyga | a474d3b | 2015-07-31 16:00:19 +0200 | [diff] [blame] | 2248 | if (epnum == 0 || epnum == 1) { |
| 2249 | dep->endpoint.caps.type_control = true; |
| 2250 | } else { |
| 2251 | dep->endpoint.caps.type_iso = true; |
| 2252 | dep->endpoint.caps.type_bulk = true; |
| 2253 | dep->endpoint.caps.type_int = true; |
| 2254 | } |
| 2255 | |
| 2256 | dep->endpoint.caps.dir_in = !!direction; |
| 2257 | dep->endpoint.caps.dir_out = !direction; |
| 2258 | |
Felipe Balbi | aa3342c | 2016-03-14 11:01:31 +0200 | [diff] [blame] | 2259 | INIT_LIST_HEAD(&dep->pending_list); |
| 2260 | INIT_LIST_HEAD(&dep->started_list); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2261 | } |
| 2262 | |
| 2263 | return 0; |
| 2264 | } |
| 2265 | |
Felipe Balbi | 6a1e3ef | 2011-05-05 16:21:59 +0300 | [diff] [blame] | 2266 | static int dwc3_gadget_init_endpoints(struct dwc3 *dwc) |
| 2267 | { |
| 2268 | int ret; |
| 2269 | |
| 2270 | INIT_LIST_HEAD(&dwc->gadget.ep_list); |
| 2271 | |
| 2272 | ret = dwc3_gadget_init_hw_endpoints(dwc, dwc->num_out_eps, 0); |
| 2273 | if (ret < 0) { |
Felipe Balbi | 7381528 | 2015-01-27 13:48:14 -0600 | [diff] [blame] | 2274 | dwc3_trace(trace_dwc3_gadget, |
| 2275 | "failed to allocate OUT endpoints"); |
Felipe Balbi | 6a1e3ef | 2011-05-05 16:21:59 +0300 | [diff] [blame] | 2276 | return ret; |
| 2277 | } |
| 2278 | |
| 2279 | ret = dwc3_gadget_init_hw_endpoints(dwc, dwc->num_in_eps, 1); |
| 2280 | if (ret < 0) { |
Felipe Balbi | 7381528 | 2015-01-27 13:48:14 -0600 | [diff] [blame] | 2281 | dwc3_trace(trace_dwc3_gadget, |
| 2282 | "failed to allocate IN endpoints"); |
Felipe Balbi | 6a1e3ef | 2011-05-05 16:21:59 +0300 | [diff] [blame] | 2283 | return ret; |
| 2284 | } |
| 2285 | |
| 2286 | return 0; |
| 2287 | } |
| 2288 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2289 | static void dwc3_gadget_free_endpoints(struct dwc3 *dwc) |
| 2290 | { |
| 2291 | struct dwc3_ep *dep; |
| 2292 | u8 epnum; |
| 2293 | |
| 2294 | for (epnum = 0; epnum < DWC3_ENDPOINTS_NUM; epnum++) { |
| 2295 | dep = dwc->eps[epnum]; |
Felipe Balbi | 6a1e3ef | 2011-05-05 16:21:59 +0300 | [diff] [blame] | 2296 | if (!dep) |
| 2297 | continue; |
George Cherian | 5bf8fae | 2013-05-27 14:35:49 +0530 | [diff] [blame] | 2298 | /* |
| 2299 | * Physical endpoints 0 and 1 are special; they form the |
| 2300 | * bi-directional USB endpoint 0. |
| 2301 | * |
| 2302 | * For those two physical endpoints, we don't allocate a TRB |
| 2303 | * pool nor do we add them the endpoints list. Due to that, we |
| 2304 | * shouldn't do these two operations otherwise we would end up |
| 2305 | * with all sorts of bugs when removing dwc3.ko. |
| 2306 | */ |
| 2307 | if (epnum != 0 && epnum != 1) { |
| 2308 | dwc3_free_trb_pool(dep); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2309 | list_del(&dep->endpoint.ep_list); |
George Cherian | 5bf8fae | 2013-05-27 14:35:49 +0530 | [diff] [blame] | 2310 | } |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2311 | |
| 2312 | kfree(dep); |
| 2313 | } |
| 2314 | } |
| 2315 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2316 | /* -------------------------------------------------------------------------- */ |
Felipe Balbi | e5caff6 | 2013-02-26 15:11:05 +0200 | [diff] [blame] | 2317 | |
Pratyush Anand | e5ba5ec | 2013-01-14 15:59:37 +0530 | [diff] [blame] | 2318 | static int __dwc3_cleanup_done_trbs(struct dwc3 *dwc, struct dwc3_ep *dep, |
| 2319 | struct dwc3_request *req, struct dwc3_trb *trb, |
Felipe Balbi | e5b36ae | 2016-08-10 11:13:26 +0300 | [diff] [blame] | 2320 | const struct dwc3_event_depevt *event, int status, |
| 2321 | int chain) |
Pratyush Anand | e5ba5ec | 2013-01-14 15:59:37 +0530 | [diff] [blame] | 2322 | { |
| 2323 | unsigned int count; |
| 2324 | unsigned int s_pkt = 0; |
| 2325 | unsigned int trb_status; |
| 2326 | |
Felipe Balbi | dc55c67 | 2016-08-12 13:20:32 +0300 | [diff] [blame] | 2327 | dwc3_ep_inc_deq(dep); |
Felipe Balbi | a9c3ca5 | 2016-10-05 14:24:37 +0300 | [diff] [blame] | 2328 | |
| 2329 | if (req->trb == trb) |
| 2330 | dep->queued_requests--; |
| 2331 | |
Felipe Balbi | 2c4cbe6e5 | 2014-04-30 17:45:10 -0500 | [diff] [blame] | 2332 | trace_dwc3_complete_trb(dep, trb); |
| 2333 | |
Felipe Balbi | e5b36ae | 2016-08-10 11:13:26 +0300 | [diff] [blame] | 2334 | /* |
| 2335 | * If we're in the middle of series of chained TRBs and we |
| 2336 | * receive a short transfer along the way, DWC3 will skip |
| 2337 | * through all TRBs including the last TRB in the chain (the |
| 2338 | * where CHN bit is zero. DWC3 will also avoid clearing HWO |
| 2339 | * bit and SW has to do it manually. |
| 2340 | * |
| 2341 | * We're going to do that here to avoid problems of HW trying |
| 2342 | * to use bogus TRBs for transfers. |
| 2343 | */ |
| 2344 | if (chain && (trb->ctrl & DWC3_TRB_CTRL_HWO)) |
| 2345 | trb->ctrl &= ~DWC3_TRB_CTRL_HWO; |
| 2346 | |
Pratyush Anand | e5ba5ec | 2013-01-14 15:59:37 +0530 | [diff] [blame] | 2347 | if ((trb->ctrl & DWC3_TRB_CTRL_HWO) && status != -ESHUTDOWN) |
Felipe Balbi | a0ad85a | 2016-08-10 18:07:46 +0300 | [diff] [blame] | 2348 | return 1; |
Felipe Balbi | e5b36ae | 2016-08-10 11:13:26 +0300 | [diff] [blame] | 2349 | |
Pratyush Anand | e5ba5ec | 2013-01-14 15:59:37 +0530 | [diff] [blame] | 2350 | count = trb->size & DWC3_TRB_SIZE_MASK; |
Felipe Balbi | dc55c67 | 2016-08-12 13:20:32 +0300 | [diff] [blame] | 2351 | req->request.actual += count; |
Pratyush Anand | e5ba5ec | 2013-01-14 15:59:37 +0530 | [diff] [blame] | 2352 | |
| 2353 | if (dep->direction) { |
| 2354 | if (count) { |
| 2355 | trb_status = DWC3_TRB_SIZE_TRBSTS(trb->size); |
| 2356 | if (trb_status == DWC3_TRBSTS_MISSED_ISOC) { |
Felipe Balbi | ec5e795 | 2015-11-16 16:04:13 -0600 | [diff] [blame] | 2357 | dwc3_trace(trace_dwc3_gadget, |
Felipe Balbi | 60cfb37 | 2016-05-24 13:45:17 +0300 | [diff] [blame] | 2358 | "%s: incomplete IN transfer", |
Pratyush Anand | e5ba5ec | 2013-01-14 15:59:37 +0530 | [diff] [blame] | 2359 | dep->name); |
| 2360 | /* |
| 2361 | * If missed isoc occurred and there is |
| 2362 | * no request queued then issue END |
| 2363 | * TRANSFER, so that core generates |
| 2364 | * next xfernotready and we will issue |
| 2365 | * a fresh START TRANSFER. |
| 2366 | * If there are still queued request |
| 2367 | * then wait, do not issue either END |
| 2368 | * or UPDATE TRANSFER, just attach next |
Felipe Balbi | aa3342c | 2016-03-14 11:01:31 +0200 | [diff] [blame] | 2369 | * request in pending_list during |
Pratyush Anand | e5ba5ec | 2013-01-14 15:59:37 +0530 | [diff] [blame] | 2370 | * giveback.If any future queued request |
| 2371 | * is successfully transferred then we |
| 2372 | * will issue UPDATE TRANSFER for all |
Felipe Balbi | aa3342c | 2016-03-14 11:01:31 +0200 | [diff] [blame] | 2373 | * request in the pending_list. |
Pratyush Anand | e5ba5ec | 2013-01-14 15:59:37 +0530 | [diff] [blame] | 2374 | */ |
| 2375 | dep->flags |= DWC3_EP_MISSED_ISOC; |
| 2376 | } else { |
| 2377 | dev_err(dwc->dev, "incomplete IN transfer %s\n", |
| 2378 | dep->name); |
| 2379 | status = -ECONNRESET; |
| 2380 | } |
| 2381 | } else { |
| 2382 | dep->flags &= ~DWC3_EP_MISSED_ISOC; |
| 2383 | } |
| 2384 | } else { |
| 2385 | if (count && (event->status & DEPEVT_STATUS_SHORT)) |
| 2386 | s_pkt = 1; |
| 2387 | } |
| 2388 | |
Felipe Balbi | 7c705df | 2016-08-10 12:35:30 +0300 | [diff] [blame] | 2389 | if (s_pkt && !chain) |
Pratyush Anand | e5ba5ec | 2013-01-14 15:59:37 +0530 | [diff] [blame] | 2390 | return 1; |
Felipe Balbi | f99f53f | 2016-08-12 13:19:20 +0300 | [diff] [blame] | 2391 | |
Pratyush Anand | e5ba5ec | 2013-01-14 15:59:37 +0530 | [diff] [blame] | 2392 | if ((event->status & DEPEVT_STATUS_IOC) && |
| 2393 | (trb->ctrl & DWC3_TRB_CTRL_IOC)) |
| 2394 | return 1; |
Felipe Balbi | f99f53f | 2016-08-12 13:19:20 +0300 | [diff] [blame] | 2395 | |
Pratyush Anand | e5ba5ec | 2013-01-14 15:59:37 +0530 | [diff] [blame] | 2396 | return 0; |
| 2397 | } |
| 2398 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2399 | static int dwc3_cleanup_done_reqs(struct dwc3 *dwc, struct dwc3_ep *dep, |
| 2400 | const struct dwc3_event_depevt *event, int status) |
| 2401 | { |
Felipe Balbi | 31162af | 2016-08-11 14:38:37 +0300 | [diff] [blame] | 2402 | struct dwc3_request *req, *n; |
Felipe Balbi | f6bafc6 | 2012-02-06 11:04:53 +0200 | [diff] [blame] | 2403 | struct dwc3_trb *trb; |
Arnd Bergmann | d6e10bf | 2016-09-09 12:01:51 +0200 | [diff] [blame] | 2404 | bool ioc = false; |
Pratyush Anand | e5ba5ec | 2013-01-14 15:59:37 +0530 | [diff] [blame] | 2405 | int ret; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2406 | |
Felipe Balbi | 31162af | 2016-08-11 14:38:37 +0300 | [diff] [blame] | 2407 | list_for_each_entry_safe(req, n, &dep->started_list, list) { |
Felipe Balbi | 1f51211 | 2016-08-12 13:17:27 +0300 | [diff] [blame] | 2408 | unsigned length; |
| 2409 | unsigned actual; |
Felipe Balbi | e5b36ae | 2016-08-10 11:13:26 +0300 | [diff] [blame] | 2410 | int chain; |
| 2411 | |
Felipe Balbi | 1f51211 | 2016-08-12 13:17:27 +0300 | [diff] [blame] | 2412 | length = req->request.length; |
| 2413 | chain = req->num_pending_sgs > 0; |
Felipe Balbi | 31162af | 2016-08-11 14:38:37 +0300 | [diff] [blame] | 2414 | if (chain) { |
Felipe Balbi | 1f51211 | 2016-08-12 13:17:27 +0300 | [diff] [blame] | 2415 | struct scatterlist *sg = req->sg; |
Felipe Balbi | 31162af | 2016-08-11 14:38:37 +0300 | [diff] [blame] | 2416 | struct scatterlist *s; |
Felipe Balbi | 1f51211 | 2016-08-12 13:17:27 +0300 | [diff] [blame] | 2417 | unsigned int pending = req->num_pending_sgs; |
Felipe Balbi | 31162af | 2016-08-11 14:38:37 +0300 | [diff] [blame] | 2418 | unsigned int i; |
Felipe Balbi | ac7bdcc | 2015-11-16 16:13:57 -0600 | [diff] [blame] | 2419 | |
Felipe Balbi | 1f51211 | 2016-08-12 13:17:27 +0300 | [diff] [blame] | 2420 | for_each_sg(sg, s, pending, i) { |
Felipe Balbi | 31162af | 2016-08-11 14:38:37 +0300 | [diff] [blame] | 2421 | trb = &dep->trb_pool[dep->trb_dequeue]; |
Felipe Balbi | c7de573 | 2016-07-29 03:17:58 +0300 | [diff] [blame] | 2422 | |
Felipe Balbi | 1f51211 | 2016-08-12 13:17:27 +0300 | [diff] [blame] | 2423 | req->sg = sg_next(s); |
| 2424 | req->num_pending_sgs--; |
| 2425 | |
Felipe Balbi | 31162af | 2016-08-11 14:38:37 +0300 | [diff] [blame] | 2426 | ret = __dwc3_cleanup_done_trbs(dwc, dep, req, trb, |
| 2427 | event, status, chain); |
Felipe Balbi | 1f51211 | 2016-08-12 13:17:27 +0300 | [diff] [blame] | 2428 | if (ret) |
| 2429 | break; |
Felipe Balbi | 31162af | 2016-08-11 14:38:37 +0300 | [diff] [blame] | 2430 | } |
| 2431 | } else { |
Felipe Balbi | 737f1ae | 2016-08-11 12:24:27 +0300 | [diff] [blame] | 2432 | trb = &dep->trb_pool[dep->trb_dequeue]; |
Ville Syrjälä | d115d70 | 2015-08-31 19:48:28 +0300 | [diff] [blame] | 2433 | ret = __dwc3_cleanup_done_trbs(dwc, dep, req, trb, |
Felipe Balbi | e5b36ae | 2016-08-10 11:13:26 +0300 | [diff] [blame] | 2434 | event, status, chain); |
Felipe Balbi | 31162af | 2016-08-11 14:38:37 +0300 | [diff] [blame] | 2435 | } |
Ville Syrjälä | d115d70 | 2015-08-31 19:48:28 +0300 | [diff] [blame] | 2436 | |
Felipe Balbi | c7de573 | 2016-07-29 03:17:58 +0300 | [diff] [blame] | 2437 | /* |
| 2438 | * We assume here we will always receive the entire data block |
| 2439 | * which we should receive. Meaning, if we program RX to |
| 2440 | * receive 4K but we receive only 2K, we assume that's all we |
| 2441 | * should receive and we simply bounce the request back to the |
| 2442 | * gadget driver for further processing. |
| 2443 | */ |
Felipe Balbi | 1f51211 | 2016-08-12 13:17:27 +0300 | [diff] [blame] | 2444 | actual = length - req->request.actual; |
| 2445 | req->request.actual = actual; |
| 2446 | |
| 2447 | if (ret && chain && (actual < length) && req->num_pending_sgs) |
| 2448 | return __dwc3_gadget_kick_transfer(dep, 0); |
| 2449 | |
Ville Syrjälä | d115d70 | 2015-08-31 19:48:28 +0300 | [diff] [blame] | 2450 | dwc3_gadget_giveback(dep, req, status); |
| 2451 | |
Arnd Bergmann | d6e10bf | 2016-09-09 12:01:51 +0200 | [diff] [blame] | 2452 | if (ret) { |
| 2453 | if ((event->status & DEPEVT_STATUS_IOC) && |
| 2454 | (trb->ctrl & DWC3_TRB_CTRL_IOC)) |
| 2455 | ioc = true; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2456 | break; |
Arnd Bergmann | d6e10bf | 2016-09-09 12:01:51 +0200 | [diff] [blame] | 2457 | } |
Felipe Balbi | 31162af | 2016-08-11 14:38:37 +0300 | [diff] [blame] | 2458 | } |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2459 | |
Felipe Balbi | 4cb4221 | 2016-05-18 12:37:21 +0300 | [diff] [blame] | 2460 | /* |
| 2461 | * Our endpoint might get disabled by another thread during |
| 2462 | * dwc3_gadget_giveback(). If that happens, we're just gonna return 1 |
| 2463 | * early on so DWC3_EP_BUSY flag gets cleared |
| 2464 | */ |
| 2465 | if (!dep->endpoint.desc) |
| 2466 | return 1; |
| 2467 | |
Pratyush Anand | cdc359d | 2013-01-14 15:59:34 +0530 | [diff] [blame] | 2468 | if (usb_endpoint_xfer_isoc(dep->endpoint.desc) && |
Felipe Balbi | aa3342c | 2016-03-14 11:01:31 +0200 | [diff] [blame] | 2469 | list_empty(&dep->started_list)) { |
| 2470 | if (list_empty(&dep->pending_list)) { |
Pratyush Anand | cdc359d | 2013-01-14 15:59:34 +0530 | [diff] [blame] | 2471 | /* |
| 2472 | * If there is no entry in request list then do |
| 2473 | * not issue END TRANSFER now. Just set PENDING |
| 2474 | * flag, so that END TRANSFER is issued when an |
| 2475 | * entry is added into request list. |
| 2476 | */ |
| 2477 | dep->flags = DWC3_EP_PENDING_REQUEST; |
| 2478 | } else { |
Paul Zimmerman | b992e68 | 2012-04-27 14:17:35 +0300 | [diff] [blame] | 2479 | dwc3_stop_active_transfer(dwc, dep->number, true); |
Pratyush Anand | cdc359d | 2013-01-14 15:59:34 +0530 | [diff] [blame] | 2480 | dep->flags = DWC3_EP_ENABLED; |
| 2481 | } |
Pratyush Anand | 7efea86 | 2013-01-14 15:59:32 +0530 | [diff] [blame] | 2482 | return 1; |
| 2483 | } |
| 2484 | |
Arnd Bergmann | d6e10bf | 2016-09-09 12:01:51 +0200 | [diff] [blame] | 2485 | if (usb_endpoint_xfer_isoc(dep->endpoint.desc) && ioc) |
| 2486 | return 0; |
| 2487 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2488 | return 1; |
| 2489 | } |
| 2490 | |
| 2491 | static void dwc3_endpoint_transfer_complete(struct dwc3 *dwc, |
Jingoo Han | 029d97f | 2014-07-04 15:00:51 +0900 | [diff] [blame] | 2492 | struct dwc3_ep *dep, const struct dwc3_event_depevt *event) |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2493 | { |
| 2494 | unsigned status = 0; |
| 2495 | int clean_busy; |
Felipe Balbi | e18b797 | 2015-05-29 10:06:38 -0500 | [diff] [blame] | 2496 | u32 is_xfer_complete; |
| 2497 | |
| 2498 | is_xfer_complete = (event->endpoint_event == DWC3_DEPEVT_XFERCOMPLETE); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2499 | |
| 2500 | if (event->status & DEPEVT_STATUS_BUSERR) |
| 2501 | status = -ECONNRESET; |
| 2502 | |
Paul Zimmerman | 1d04679 | 2012-02-15 18:56:56 -0800 | [diff] [blame] | 2503 | clean_busy = dwc3_cleanup_done_reqs(dwc, dep, event, status); |
Felipe Balbi | 4cb4221 | 2016-05-18 12:37:21 +0300 | [diff] [blame] | 2504 | if (clean_busy && (!dep->endpoint.desc || is_xfer_complete || |
Felipe Balbi | e18b797 | 2015-05-29 10:06:38 -0500 | [diff] [blame] | 2505 | usb_endpoint_xfer_isoc(dep->endpoint.desc))) |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2506 | dep->flags &= ~DWC3_EP_BUSY; |
Felipe Balbi | fae2b90 | 2011-10-14 13:00:30 +0300 | [diff] [blame] | 2507 | |
| 2508 | /* |
| 2509 | * WORKAROUND: This is the 2nd half of U1/U2 -> U0 workaround. |
| 2510 | * See dwc3_gadget_linksts_change_interrupt() for 1st half. |
| 2511 | */ |
| 2512 | if (dwc->revision < DWC3_REVISION_183A) { |
| 2513 | u32 reg; |
| 2514 | int i; |
| 2515 | |
| 2516 | for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) { |
Moiz Sonasath | 348e026 | 2012-08-01 14:08:30 -0500 | [diff] [blame] | 2517 | dep = dwc->eps[i]; |
Felipe Balbi | fae2b90 | 2011-10-14 13:00:30 +0300 | [diff] [blame] | 2518 | |
| 2519 | if (!(dep->flags & DWC3_EP_ENABLED)) |
| 2520 | continue; |
| 2521 | |
Felipe Balbi | aa3342c | 2016-03-14 11:01:31 +0200 | [diff] [blame] | 2522 | if (!list_empty(&dep->started_list)) |
Felipe Balbi | fae2b90 | 2011-10-14 13:00:30 +0300 | [diff] [blame] | 2523 | return; |
| 2524 | } |
| 2525 | |
| 2526 | reg = dwc3_readl(dwc->regs, DWC3_DCTL); |
| 2527 | reg |= dwc->u1u2; |
| 2528 | dwc3_writel(dwc->regs, DWC3_DCTL, reg); |
| 2529 | |
| 2530 | dwc->u1u2 = 0; |
| 2531 | } |
Felipe Balbi | 8a1a9c9 | 2015-09-21 14:32:00 -0500 | [diff] [blame] | 2532 | |
Felipe Balbi | 4cb4221 | 2016-05-18 12:37:21 +0300 | [diff] [blame] | 2533 | /* |
| 2534 | * Our endpoint might get disabled by another thread during |
| 2535 | * dwc3_gadget_giveback(). If that happens, we're just gonna return 1 |
| 2536 | * early on so DWC3_EP_BUSY flag gets cleared |
| 2537 | */ |
| 2538 | if (!dep->endpoint.desc) |
| 2539 | return; |
| 2540 | |
Felipe Balbi | e6e709b | 2015-09-28 15:16:56 -0500 | [diff] [blame] | 2541 | if (!usb_endpoint_xfer_isoc(dep->endpoint.desc)) { |
Felipe Balbi | 8a1a9c9 | 2015-09-21 14:32:00 -0500 | [diff] [blame] | 2542 | int ret; |
| 2543 | |
Felipe Balbi | 4fae2e3 | 2016-05-12 16:53:59 +0300 | [diff] [blame] | 2544 | ret = __dwc3_gadget_kick_transfer(dep, 0); |
Felipe Balbi | 8a1a9c9 | 2015-09-21 14:32:00 -0500 | [diff] [blame] | 2545 | if (!ret || ret == -EBUSY) |
| 2546 | return; |
| 2547 | } |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2548 | } |
| 2549 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2550 | static void dwc3_endpoint_interrupt(struct dwc3 *dwc, |
| 2551 | const struct dwc3_event_depevt *event) |
| 2552 | { |
| 2553 | struct dwc3_ep *dep; |
| 2554 | u8 epnum = event->endpoint_number; |
| 2555 | |
| 2556 | dep = dwc->eps[epnum]; |
| 2557 | |
Felipe Balbi | 3336abb | 2012-06-06 09:19:35 +0300 | [diff] [blame] | 2558 | if (!(dep->flags & DWC3_EP_ENABLED)) |
| 2559 | return; |
| 2560 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2561 | if (epnum == 0 || epnum == 1) { |
| 2562 | dwc3_ep0_interrupt(dwc, event); |
| 2563 | return; |
| 2564 | } |
| 2565 | |
| 2566 | switch (event->endpoint_event) { |
| 2567 | case DWC3_DEPEVT_XFERCOMPLETE: |
Felipe Balbi | b4996a8 | 2012-06-06 12:04:13 +0300 | [diff] [blame] | 2568 | dep->resource_index = 0; |
Paul Zimmerman | c2df85c | 2012-02-24 17:32:18 -0800 | [diff] [blame] | 2569 | |
Ido Shayevitz | 16e78db | 2012-03-12 20:25:24 +0200 | [diff] [blame] | 2570 | if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) { |
Felipe Balbi | ec5e795 | 2015-11-16 16:04:13 -0600 | [diff] [blame] | 2571 | dwc3_trace(trace_dwc3_gadget, |
Felipe Balbi | 60cfb37 | 2016-05-24 13:45:17 +0300 | [diff] [blame] | 2572 | "%s is an Isochronous endpoint", |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2573 | dep->name); |
| 2574 | return; |
| 2575 | } |
| 2576 | |
Jingoo Han | 029d97f | 2014-07-04 15:00:51 +0900 | [diff] [blame] | 2577 | dwc3_endpoint_transfer_complete(dwc, dep, event); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2578 | break; |
| 2579 | case DWC3_DEPEVT_XFERINPROGRESS: |
Jingoo Han | 029d97f | 2014-07-04 15:00:51 +0900 | [diff] [blame] | 2580 | dwc3_endpoint_transfer_complete(dwc, dep, event); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2581 | break; |
| 2582 | case DWC3_DEPEVT_XFERNOTREADY: |
Ido Shayevitz | 16e78db | 2012-03-12 20:25:24 +0200 | [diff] [blame] | 2583 | if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) { |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2584 | dwc3_gadget_start_isoc(dwc, dep, event); |
| 2585 | } else { |
Felipe Balbi | 6bb4fe1 | 2015-09-28 14:49:02 -0500 | [diff] [blame] | 2586 | int active; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2587 | int ret; |
| 2588 | |
Felipe Balbi | 6bb4fe1 | 2015-09-28 14:49:02 -0500 | [diff] [blame] | 2589 | active = event->status & DEPEVT_STATUS_TRANSFER_ACTIVE; |
| 2590 | |
Felipe Balbi | 7381528 | 2015-01-27 13:48:14 -0600 | [diff] [blame] | 2591 | dwc3_trace(trace_dwc3_gadget, "%s: reason %s", |
Felipe Balbi | 6bb4fe1 | 2015-09-28 14:49:02 -0500 | [diff] [blame] | 2592 | dep->name, active ? "Transfer Active" |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2593 | : "Transfer Not Active"); |
| 2594 | |
Felipe Balbi | 4fae2e3 | 2016-05-12 16:53:59 +0300 | [diff] [blame] | 2595 | ret = __dwc3_gadget_kick_transfer(dep, 0); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2596 | if (!ret || ret == -EBUSY) |
| 2597 | return; |
| 2598 | |
Felipe Balbi | ec5e795 | 2015-11-16 16:04:13 -0600 | [diff] [blame] | 2599 | dwc3_trace(trace_dwc3_gadget, |
Felipe Balbi | 60cfb37 | 2016-05-24 13:45:17 +0300 | [diff] [blame] | 2600 | "%s: failed to kick transfers", |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2601 | dep->name); |
| 2602 | } |
| 2603 | |
| 2604 | break; |
Felipe Balbi | 879631a | 2011-09-30 10:58:47 +0300 | [diff] [blame] | 2605 | case DWC3_DEPEVT_STREAMEVT: |
Ido Shayevitz | 16e78db | 2012-03-12 20:25:24 +0200 | [diff] [blame] | 2606 | if (!usb_endpoint_xfer_bulk(dep->endpoint.desc)) { |
Felipe Balbi | 879631a | 2011-09-30 10:58:47 +0300 | [diff] [blame] | 2607 | dev_err(dwc->dev, "Stream event for non-Bulk %s\n", |
| 2608 | dep->name); |
| 2609 | return; |
| 2610 | } |
| 2611 | |
| 2612 | switch (event->status) { |
| 2613 | case DEPEVT_STREAMEVT_FOUND: |
Felipe Balbi | 7381528 | 2015-01-27 13:48:14 -0600 | [diff] [blame] | 2614 | dwc3_trace(trace_dwc3_gadget, |
| 2615 | "Stream %d found and started", |
Felipe Balbi | 879631a | 2011-09-30 10:58:47 +0300 | [diff] [blame] | 2616 | event->parameters); |
| 2617 | |
| 2618 | break; |
| 2619 | case DEPEVT_STREAMEVT_NOTFOUND: |
| 2620 | /* FALLTHROUGH */ |
| 2621 | default: |
Felipe Balbi | ec5e795 | 2015-11-16 16:04:13 -0600 | [diff] [blame] | 2622 | dwc3_trace(trace_dwc3_gadget, |
Felipe Balbi | 60cfb37 | 2016-05-24 13:45:17 +0300 | [diff] [blame] | 2623 | "unable to find suitable stream"); |
Felipe Balbi | 879631a | 2011-09-30 10:58:47 +0300 | [diff] [blame] | 2624 | } |
| 2625 | break; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2626 | case DWC3_DEPEVT_RXTXFIFOEVT: |
Felipe Balbi | 60cfb37 | 2016-05-24 13:45:17 +0300 | [diff] [blame] | 2627 | dwc3_trace(trace_dwc3_gadget, "%s FIFO Overrun", dep->name); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2628 | break; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2629 | case DWC3_DEPEVT_EPCMDCMPLT: |
Felipe Balbi | 7381528 | 2015-01-27 13:48:14 -0600 | [diff] [blame] | 2630 | dwc3_trace(trace_dwc3_gadget, "Endpoint Command Complete"); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2631 | break; |
| 2632 | } |
| 2633 | } |
| 2634 | |
| 2635 | static void dwc3_disconnect_gadget(struct dwc3 *dwc) |
| 2636 | { |
| 2637 | if (dwc->gadget_driver && dwc->gadget_driver->disconnect) { |
| 2638 | spin_unlock(&dwc->lock); |
| 2639 | dwc->gadget_driver->disconnect(&dwc->gadget); |
| 2640 | spin_lock(&dwc->lock); |
| 2641 | } |
| 2642 | } |
| 2643 | |
Felipe Balbi | bc5ba2e | 2014-02-26 10:17:07 -0600 | [diff] [blame] | 2644 | static void dwc3_suspend_gadget(struct dwc3 *dwc) |
| 2645 | { |
Dan Carpenter | 73a30bf | 2014-03-07 14:19:57 +0300 | [diff] [blame] | 2646 | if (dwc->gadget_driver && dwc->gadget_driver->suspend) { |
Felipe Balbi | bc5ba2e | 2014-02-26 10:17:07 -0600 | [diff] [blame] | 2647 | spin_unlock(&dwc->lock); |
| 2648 | dwc->gadget_driver->suspend(&dwc->gadget); |
| 2649 | spin_lock(&dwc->lock); |
| 2650 | } |
| 2651 | } |
| 2652 | |
| 2653 | static void dwc3_resume_gadget(struct dwc3 *dwc) |
| 2654 | { |
Dan Carpenter | 73a30bf | 2014-03-07 14:19:57 +0300 | [diff] [blame] | 2655 | if (dwc->gadget_driver && dwc->gadget_driver->resume) { |
Felipe Balbi | bc5ba2e | 2014-02-26 10:17:07 -0600 | [diff] [blame] | 2656 | spin_unlock(&dwc->lock); |
| 2657 | dwc->gadget_driver->resume(&dwc->gadget); |
Felipe Balbi | 5c7b3b0 | 2015-01-29 10:29:18 -0600 | [diff] [blame] | 2658 | spin_lock(&dwc->lock); |
Felipe Balbi | 8e74475 | 2014-11-06 14:27:53 +0800 | [diff] [blame] | 2659 | } |
| 2660 | } |
| 2661 | |
| 2662 | static void dwc3_reset_gadget(struct dwc3 *dwc) |
| 2663 | { |
| 2664 | if (!dwc->gadget_driver) |
| 2665 | return; |
| 2666 | |
| 2667 | if (dwc->gadget.speed != USB_SPEED_UNKNOWN) { |
| 2668 | spin_unlock(&dwc->lock); |
| 2669 | usb_gadget_udc_reset(&dwc->gadget, dwc->gadget_driver); |
Felipe Balbi | bc5ba2e | 2014-02-26 10:17:07 -0600 | [diff] [blame] | 2670 | spin_lock(&dwc->lock); |
| 2671 | } |
| 2672 | } |
| 2673 | |
Mayank Rana | a99689a | 2016-08-10 17:39:47 -0700 | [diff] [blame] | 2674 | void dwc3_stop_active_transfer(struct dwc3 *dwc, u32 epnum, bool force) |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2675 | { |
| 2676 | struct dwc3_ep *dep; |
| 2677 | struct dwc3_gadget_ep_cmd_params params; |
| 2678 | u32 cmd; |
| 2679 | int ret; |
| 2680 | |
| 2681 | dep = dwc->eps[epnum]; |
| 2682 | |
Felipe Balbi | b4996a8 | 2012-06-06 12:04:13 +0300 | [diff] [blame] | 2683 | if (!dep->resource_index) |
Pratyush Anand | 3daf74d | 2012-06-23 02:23:08 +0530 | [diff] [blame] | 2684 | return; |
| 2685 | |
Pratyush Anand | 5791150 | 2012-07-06 15:19:10 +0530 | [diff] [blame] | 2686 | /* |
| 2687 | * NOTICE: We are violating what the Databook says about the |
| 2688 | * EndTransfer command. Ideally we would _always_ wait for the |
| 2689 | * EndTransfer Command Completion IRQ, but that's causing too |
| 2690 | * much trouble synchronizing between us and gadget driver. |
| 2691 | * |
| 2692 | * We have discussed this with the IP Provider and it was |
| 2693 | * suggested to giveback all requests here, but give HW some |
| 2694 | * extra time to synchronize with the interconnect. We're using |
Mickael Maison | dc93b41 | 2014-12-23 17:34:43 +0100 | [diff] [blame] | 2695 | * an arbitrary 100us delay for that. |
Pratyush Anand | 5791150 | 2012-07-06 15:19:10 +0530 | [diff] [blame] | 2696 | * |
| 2697 | * Note also that a similar handling was tested by Synopsys |
| 2698 | * (thanks a lot Paul) and nothing bad has come out of it. |
| 2699 | * In short, what we're doing is: |
| 2700 | * |
| 2701 | * - Issue EndTransfer WITH CMDIOC bit set |
| 2702 | * - Wait 100us |
John Youn | 06281d4 | 2016-08-22 15:39:13 -0700 | [diff] [blame] | 2703 | * |
| 2704 | * As of IP version 3.10a of the DWC_usb3 IP, the controller |
| 2705 | * supports a mode to work around the above limitation. The |
| 2706 | * software can poll the CMDACT bit in the DEPCMD register |
| 2707 | * after issuing a EndTransfer command. This mode is enabled |
| 2708 | * by writing GUCTL2[14]. This polling is already done in the |
| 2709 | * dwc3_send_gadget_ep_cmd() function so if the mode is |
| 2710 | * enabled, the EndTransfer command will have completed upon |
| 2711 | * returning from this function and we don't need to delay for |
| 2712 | * 100us. |
| 2713 | * |
| 2714 | * This mode is NOT available on the DWC_usb31 IP. |
Pratyush Anand | 5791150 | 2012-07-06 15:19:10 +0530 | [diff] [blame] | 2715 | */ |
| 2716 | |
Pratyush Anand | 3daf74d | 2012-06-23 02:23:08 +0530 | [diff] [blame] | 2717 | cmd = DWC3_DEPCMD_ENDTRANSFER; |
Paul Zimmerman | b992e68 | 2012-04-27 14:17:35 +0300 | [diff] [blame] | 2718 | cmd |= force ? DWC3_DEPCMD_HIPRI_FORCERM : 0; |
| 2719 | cmd |= DWC3_DEPCMD_CMDIOC; |
Felipe Balbi | b4996a8 | 2012-06-06 12:04:13 +0300 | [diff] [blame] | 2720 | cmd |= DWC3_DEPCMD_PARAM(dep->resource_index); |
Pratyush Anand | 3daf74d | 2012-06-23 02:23:08 +0530 | [diff] [blame] | 2721 | memset(¶ms, 0, sizeof(params)); |
Felipe Balbi | 2cd4718 | 2016-04-12 16:42:43 +0300 | [diff] [blame] | 2722 | ret = dwc3_send_gadget_ep_cmd(dep, cmd, ¶ms); |
Pratyush Anand | 3daf74d | 2012-06-23 02:23:08 +0530 | [diff] [blame] | 2723 | WARN_ON_ONCE(ret); |
Felipe Balbi | b4996a8 | 2012-06-06 12:04:13 +0300 | [diff] [blame] | 2724 | dep->resource_index = 0; |
Felipe Balbi | 041d81f | 2012-10-04 11:58:00 +0300 | [diff] [blame] | 2725 | dep->flags &= ~DWC3_EP_BUSY; |
John Youn | 06281d4 | 2016-08-22 15:39:13 -0700 | [diff] [blame] | 2726 | |
| 2727 | if (dwc3_is_usb31(dwc) || dwc->revision < DWC3_REVISION_310A) |
| 2728 | udelay(100); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2729 | } |
| 2730 | |
| 2731 | static void dwc3_stop_active_transfers(struct dwc3 *dwc) |
| 2732 | { |
| 2733 | u32 epnum; |
| 2734 | |
| 2735 | for (epnum = 2; epnum < DWC3_ENDPOINTS_NUM; epnum++) { |
| 2736 | struct dwc3_ep *dep; |
| 2737 | |
| 2738 | dep = dwc->eps[epnum]; |
Felipe Balbi | 6a1e3ef | 2011-05-05 16:21:59 +0300 | [diff] [blame] | 2739 | if (!dep) |
| 2740 | continue; |
| 2741 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2742 | if (!(dep->flags & DWC3_EP_ENABLED)) |
| 2743 | continue; |
| 2744 | |
Sebastian Andrzej Siewior | 624407f | 2011-08-29 13:56:37 +0200 | [diff] [blame] | 2745 | dwc3_remove_requests(dwc, dep); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2746 | } |
| 2747 | } |
| 2748 | |
| 2749 | static void dwc3_clear_stall_all_ep(struct dwc3 *dwc) |
| 2750 | { |
| 2751 | u32 epnum; |
| 2752 | |
| 2753 | for (epnum = 1; epnum < DWC3_ENDPOINTS_NUM; epnum++) { |
| 2754 | struct dwc3_ep *dep; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2755 | int ret; |
| 2756 | |
| 2757 | dep = dwc->eps[epnum]; |
Felipe Balbi | 6a1e3ef | 2011-05-05 16:21:59 +0300 | [diff] [blame] | 2758 | if (!dep) |
| 2759 | continue; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2760 | |
| 2761 | if (!(dep->flags & DWC3_EP_STALL)) |
| 2762 | continue; |
| 2763 | |
| 2764 | dep->flags &= ~DWC3_EP_STALL; |
| 2765 | |
John Youn | 50c763f | 2016-05-31 17:49:56 -0700 | [diff] [blame] | 2766 | ret = dwc3_send_clear_stall_ep_cmd(dep); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2767 | WARN_ON_ONCE(ret); |
| 2768 | } |
| 2769 | } |
| 2770 | |
| 2771 | static void dwc3_gadget_disconnect_interrupt(struct dwc3 *dwc) |
| 2772 | { |
Felipe Balbi | c4430a2 | 2012-05-24 10:30:01 +0300 | [diff] [blame] | 2773 | int reg; |
| 2774 | |
Mayank Rana | a99689a | 2016-08-10 17:39:47 -0700 | [diff] [blame] | 2775 | dev_dbg(dwc->dev, "Notify OTG from %s\n", __func__); |
| 2776 | dwc->b_suspend = false; |
| 2777 | dwc3_notify_event(dwc, DWC3_CONTROLLER_NOTIFY_OTG_EVENT); |
| 2778 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2779 | reg = dwc3_readl(dwc->regs, DWC3_DCTL); |
| 2780 | reg &= ~DWC3_DCTL_INITU1ENA; |
| 2781 | dwc3_writel(dwc->regs, DWC3_DCTL, reg); |
| 2782 | |
| 2783 | reg &= ~DWC3_DCTL_INITU2ENA; |
| 2784 | dwc3_writel(dwc->regs, DWC3_DCTL, reg); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2785 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2786 | dwc3_disconnect_gadget(dwc); |
| 2787 | |
| 2788 | dwc->gadget.speed = USB_SPEED_UNKNOWN; |
Felipe Balbi | df62df5 | 2011-10-14 15:11:49 +0300 | [diff] [blame] | 2789 | dwc->setup_packet_pending = false; |
Mayank Rana | a99689a | 2016-08-10 17:39:47 -0700 | [diff] [blame] | 2790 | dwc->link_state = DWC3_LINK_STATE_SS_DIS; |
Felipe Balbi | 06a374e | 2014-10-10 15:24:00 -0500 | [diff] [blame] | 2791 | usb_gadget_set_state(&dwc->gadget, USB_STATE_NOTATTACHED); |
Felipe Balbi | fc8bb91 | 2016-05-16 13:14:48 +0300 | [diff] [blame] | 2792 | |
| 2793 | dwc->connected = false; |
Mayank Rana | a99689a | 2016-08-10 17:39:47 -0700 | [diff] [blame] | 2794 | wake_up_interruptible(&dwc->wait_linkstate); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2795 | } |
| 2796 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2797 | static void dwc3_gadget_reset_interrupt(struct dwc3 *dwc) |
| 2798 | { |
| 2799 | u32 reg; |
| 2800 | |
Felipe Balbi | fc8bb91 | 2016-05-16 13:14:48 +0300 | [diff] [blame] | 2801 | dwc->connected = true; |
| 2802 | |
Felipe Balbi | df62df5 | 2011-10-14 15:11:49 +0300 | [diff] [blame] | 2803 | /* |
| 2804 | * WORKAROUND: DWC3 revisions <1.88a have an issue which |
| 2805 | * would cause a missing Disconnect Event if there's a |
| 2806 | * pending Setup Packet in the FIFO. |
| 2807 | * |
| 2808 | * There's no suggested workaround on the official Bug |
| 2809 | * report, which states that "unless the driver/application |
| 2810 | * is doing any special handling of a disconnect event, |
| 2811 | * there is no functional issue". |
| 2812 | * |
| 2813 | * Unfortunately, it turns out that we _do_ some special |
| 2814 | * handling of a disconnect event, namely complete all |
| 2815 | * pending transfers, notify gadget driver of the |
| 2816 | * disconnection, and so on. |
| 2817 | * |
| 2818 | * Our suggested workaround is to follow the Disconnect |
| 2819 | * Event steps here, instead, based on a setup_packet_pending |
Felipe Balbi | b5d335e | 2015-11-16 16:20:34 -0600 | [diff] [blame] | 2820 | * flag. Such flag gets set whenever we have a SETUP_PENDING |
| 2821 | * status for EP0 TRBs and gets cleared on XferComplete for the |
Felipe Balbi | df62df5 | 2011-10-14 15:11:49 +0300 | [diff] [blame] | 2822 | * same endpoint. |
| 2823 | * |
| 2824 | * Refers to: |
| 2825 | * |
| 2826 | * STAR#9000466709: RTL: Device : Disconnect event not |
| 2827 | * generated if setup packet pending in FIFO |
| 2828 | */ |
| 2829 | if (dwc->revision < DWC3_REVISION_188A) { |
| 2830 | if (dwc->setup_packet_pending) |
| 2831 | dwc3_gadget_disconnect_interrupt(dwc); |
| 2832 | } |
| 2833 | |
Mayank Rana | a99689a | 2016-08-10 17:39:47 -0700 | [diff] [blame] | 2834 | dev_dbg(dwc->dev, "Notify OTG from %s\n", __func__); |
| 2835 | dwc->b_suspend = false; |
| 2836 | dwc3_notify_event(dwc, DWC3_CONTROLLER_NOTIFY_OTG_EVENT); |
| 2837 | |
| 2838 | dwc3_usb3_phy_suspend(dwc, false); |
Hemant Kumar | d55fe95 | 2016-10-31 10:26:41 -0700 | [diff] [blame] | 2839 | usb_gadget_vbus_draw(&dwc->gadget, 100); |
Mayank Rana | a99689a | 2016-08-10 17:39:47 -0700 | [diff] [blame] | 2840 | |
Felipe Balbi | 8e74475 | 2014-11-06 14:27:53 +0800 | [diff] [blame] | 2841 | dwc3_reset_gadget(dwc); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2842 | |
| 2843 | reg = dwc3_readl(dwc->regs, DWC3_DCTL); |
| 2844 | reg &= ~DWC3_DCTL_TSTCTRL_MASK; |
| 2845 | dwc3_writel(dwc->regs, DWC3_DCTL, reg); |
Gerard Cauvy | 3b63736 | 2012-02-10 12:21:18 +0200 | [diff] [blame] | 2846 | dwc->test_mode = false; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2847 | |
| 2848 | dwc3_stop_active_transfers(dwc); |
| 2849 | dwc3_clear_stall_all_ep(dwc); |
| 2850 | |
| 2851 | /* Reset device address to zero */ |
| 2852 | reg = dwc3_readl(dwc->regs, DWC3_DCFG); |
| 2853 | reg &= ~(DWC3_DCFG_DEVADDR_MASK); |
| 2854 | dwc3_writel(dwc->regs, DWC3_DCFG, reg); |
Mayank Rana | a99689a | 2016-08-10 17:39:47 -0700 | [diff] [blame] | 2855 | |
| 2856 | dwc->gadget.speed = USB_SPEED_UNKNOWN; |
| 2857 | dwc->link_state = DWC3_LINK_STATE_U0; |
| 2858 | wake_up_interruptible(&dwc->wait_linkstate); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2859 | } |
| 2860 | |
| 2861 | static void dwc3_update_ram_clk_sel(struct dwc3 *dwc, u32 speed) |
| 2862 | { |
| 2863 | u32 reg; |
| 2864 | u32 usb30_clock = DWC3_GCTL_CLK_BUS; |
| 2865 | |
| 2866 | /* |
| 2867 | * We change the clock only at SS but I dunno why I would want to do |
| 2868 | * this. Maybe it becomes part of the power saving plan. |
| 2869 | */ |
| 2870 | |
John Youn | ee5cd41 | 2016-02-05 17:08:45 -0800 | [diff] [blame] | 2871 | if ((speed != DWC3_DSTS_SUPERSPEED) && |
| 2872 | (speed != DWC3_DSTS_SUPERSPEED_PLUS)) |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2873 | return; |
| 2874 | |
| 2875 | /* |
| 2876 | * RAMClkSel is reset to 0 after USB reset, so it must be reprogrammed |
| 2877 | * each time on Connect Done. |
| 2878 | */ |
| 2879 | if (!usb30_clock) |
| 2880 | return; |
| 2881 | |
| 2882 | reg = dwc3_readl(dwc->regs, DWC3_GCTL); |
| 2883 | reg |= DWC3_GCTL_RAMCLKSEL(usb30_clock); |
| 2884 | dwc3_writel(dwc->regs, DWC3_GCTL, reg); |
| 2885 | } |
| 2886 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2887 | static void dwc3_gadget_conndone_interrupt(struct dwc3 *dwc) |
| 2888 | { |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2889 | struct dwc3_ep *dep; |
| 2890 | int ret; |
| 2891 | u32 reg; |
| 2892 | u8 speed; |
| 2893 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2894 | reg = dwc3_readl(dwc->regs, DWC3_DSTS); |
| 2895 | speed = reg & DWC3_DSTS_CONNECTSPD; |
| 2896 | dwc->speed = speed; |
| 2897 | |
| 2898 | dwc3_update_ram_clk_sel(dwc, speed); |
| 2899 | |
| 2900 | switch (speed) { |
John Youn | 2da9ad7 | 2016-05-20 16:34:26 -0700 | [diff] [blame] | 2901 | case DWC3_DSTS_SUPERSPEED_PLUS: |
John Youn | 7580862 | 2016-02-05 17:09:13 -0800 | [diff] [blame] | 2902 | dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512); |
| 2903 | dwc->gadget.ep0->maxpacket = 512; |
| 2904 | dwc->gadget.speed = USB_SPEED_SUPER_PLUS; |
| 2905 | break; |
John Youn | 2da9ad7 | 2016-05-20 16:34:26 -0700 | [diff] [blame] | 2906 | case DWC3_DSTS_SUPERSPEED: |
Felipe Balbi | 05870c5 | 2011-10-14 14:51:38 +0300 | [diff] [blame] | 2907 | /* |
| 2908 | * WORKAROUND: DWC3 revisions <1.90a have an issue which |
| 2909 | * would cause a missing USB3 Reset event. |
| 2910 | * |
| 2911 | * In such situations, we should force a USB3 Reset |
| 2912 | * event by calling our dwc3_gadget_reset_interrupt() |
| 2913 | * routine. |
| 2914 | * |
| 2915 | * Refers to: |
| 2916 | * |
| 2917 | * STAR#9000483510: RTL: SS : USB3 reset event may |
| 2918 | * not be generated always when the link enters poll |
| 2919 | */ |
| 2920 | if (dwc->revision < DWC3_REVISION_190A) |
| 2921 | dwc3_gadget_reset_interrupt(dwc); |
| 2922 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2923 | dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512); |
| 2924 | dwc->gadget.ep0->maxpacket = 512; |
| 2925 | dwc->gadget.speed = USB_SPEED_SUPER; |
| 2926 | break; |
John Youn | 2da9ad7 | 2016-05-20 16:34:26 -0700 | [diff] [blame] | 2927 | case DWC3_DSTS_HIGHSPEED: |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2928 | dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64); |
| 2929 | dwc->gadget.ep0->maxpacket = 64; |
| 2930 | dwc->gadget.speed = USB_SPEED_HIGH; |
| 2931 | break; |
John Youn | 2da9ad7 | 2016-05-20 16:34:26 -0700 | [diff] [blame] | 2932 | case DWC3_DSTS_FULLSPEED2: |
| 2933 | case DWC3_DSTS_FULLSPEED1: |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2934 | dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64); |
| 2935 | dwc->gadget.ep0->maxpacket = 64; |
| 2936 | dwc->gadget.speed = USB_SPEED_FULL; |
| 2937 | break; |
John Youn | 2da9ad7 | 2016-05-20 16:34:26 -0700 | [diff] [blame] | 2938 | case DWC3_DSTS_LOWSPEED: |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2939 | dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(8); |
| 2940 | dwc->gadget.ep0->maxpacket = 8; |
| 2941 | dwc->gadget.speed = USB_SPEED_LOW; |
| 2942 | break; |
| 2943 | } |
| 2944 | |
Pratyush Anand | 2b75835 | 2013-01-14 15:59:31 +0530 | [diff] [blame] | 2945 | /* Enable USB2 LPM Capability */ |
| 2946 | |
John Youn | ee5cd41 | 2016-02-05 17:08:45 -0800 | [diff] [blame] | 2947 | if ((dwc->revision > DWC3_REVISION_194A) && |
John Youn | 2da9ad7 | 2016-05-20 16:34:26 -0700 | [diff] [blame] | 2948 | (speed != DWC3_DSTS_SUPERSPEED) && |
| 2949 | (speed != DWC3_DSTS_SUPERSPEED_PLUS)) { |
Pratyush Anand | 2b75835 | 2013-01-14 15:59:31 +0530 | [diff] [blame] | 2950 | reg = dwc3_readl(dwc->regs, DWC3_DCFG); |
| 2951 | reg |= DWC3_DCFG_LPM_CAP; |
| 2952 | dwc3_writel(dwc->regs, DWC3_DCFG, reg); |
| 2953 | |
| 2954 | reg = dwc3_readl(dwc->regs, DWC3_DCTL); |
| 2955 | reg &= ~(DWC3_DCTL_HIRD_THRES_MASK | DWC3_DCTL_L1_HIBER_EN); |
| 2956 | |
Huang Rui | 460d098 | 2014-10-31 11:11:18 +0800 | [diff] [blame] | 2957 | reg |= DWC3_DCTL_HIRD_THRES(dwc->hird_threshold); |
Pratyush Anand | 2b75835 | 2013-01-14 15:59:31 +0530 | [diff] [blame] | 2958 | |
Huang Rui | 80caf7d | 2014-10-28 19:54:26 +0800 | [diff] [blame] | 2959 | /* |
| 2960 | * When dwc3 revisions >= 2.40a, LPM Erratum is enabled and |
| 2961 | * DCFG.LPMCap is set, core responses with an ACK and the |
| 2962 | * BESL value in the LPM token is less than or equal to LPM |
| 2963 | * NYET threshold. |
| 2964 | */ |
| 2965 | WARN_ONCE(dwc->revision < DWC3_REVISION_240A |
| 2966 | && dwc->has_lpm_erratum, |
| 2967 | "LPM Erratum not available on dwc3 revisisions < 2.40a\n"); |
| 2968 | |
| 2969 | if (dwc->has_lpm_erratum && dwc->revision >= DWC3_REVISION_240A) |
| 2970 | reg |= DWC3_DCTL_LPM_ERRATA(dwc->lpm_nyet_threshold); |
| 2971 | |
Pratyush Anand | 2b75835 | 2013-01-14 15:59:31 +0530 | [diff] [blame] | 2972 | dwc3_writel(dwc->regs, DWC3_DCTL, reg); |
Felipe Balbi | 356363b | 2013-12-19 16:37:05 -0600 | [diff] [blame] | 2973 | } else { |
| 2974 | reg = dwc3_readl(dwc->regs, DWC3_DCTL); |
| 2975 | reg &= ~DWC3_DCTL_HIRD_THRES_MASK; |
| 2976 | dwc3_writel(dwc->regs, DWC3_DCTL, reg); |
Pratyush Anand | 2b75835 | 2013-01-14 15:59:31 +0530 | [diff] [blame] | 2977 | } |
| 2978 | |
Mayank Rana | a99689a | 2016-08-10 17:39:47 -0700 | [diff] [blame] | 2979 | |
| 2980 | /* |
| 2981 | * In HS mode this allows SS phy suspend. In SS mode this allows ss phy |
| 2982 | * suspend in P3 state and generates IN_P3 power event irq. |
| 2983 | */ |
| 2984 | dwc3_usb3_phy_suspend(dwc, true); |
| 2985 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2986 | dep = dwc->eps[0]; |
Paul Zimmerman | 265b70a | 2013-12-19 12:38:49 -0600 | [diff] [blame] | 2987 | ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, true, |
| 2988 | false); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2989 | if (ret) { |
| 2990 | dev_err(dwc->dev, "failed to enable %s\n", dep->name); |
| 2991 | return; |
| 2992 | } |
| 2993 | |
| 2994 | dep = dwc->eps[1]; |
Paul Zimmerman | 265b70a | 2013-12-19 12:38:49 -0600 | [diff] [blame] | 2995 | ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, true, |
| 2996 | false); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 2997 | if (ret) { |
| 2998 | dev_err(dwc->dev, "failed to enable %s\n", dep->name); |
| 2999 | return; |
| 3000 | } |
| 3001 | |
Mayank Rana | a99689a | 2016-08-10 17:39:47 -0700 | [diff] [blame] | 3002 | dwc3_notify_event(dwc, DWC3_CONTROLLER_CONNDONE_EVENT); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3003 | /* |
| 3004 | * Configure PHY via GUSB3PIPECTLn if required. |
| 3005 | * |
| 3006 | * Update GTXFIFOSIZn |
| 3007 | * |
| 3008 | * In both cases reset values should be sufficient. |
| 3009 | */ |
| 3010 | } |
| 3011 | |
Mayank Rana | a99689a | 2016-08-10 17:39:47 -0700 | [diff] [blame] | 3012 | static void dwc3_gadget_wakeup_interrupt(struct dwc3 *dwc, bool remote_wakeup) |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3013 | { |
Mayank Rana | a99689a | 2016-08-10 17:39:47 -0700 | [diff] [blame] | 3014 | bool perform_resume = true; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3015 | |
Mayank Rana | a99689a | 2016-08-10 17:39:47 -0700 | [diff] [blame] | 3016 | dev_dbg(dwc->dev, "%s\n", __func__); |
| 3017 | |
| 3018 | /* |
| 3019 | * Identify if it is called from wakeup_interrupt() context for bus |
| 3020 | * resume or as part of remote wakeup. And based on that check for |
| 3021 | * U3 state. as we need to handle case of L1 resume i.e. where we |
| 3022 | * don't want to perform resume. |
| 3023 | */ |
| 3024 | if (!remote_wakeup && dwc->link_state != DWC3_LINK_STATE_U3) |
| 3025 | perform_resume = false; |
| 3026 | |
| 3027 | /* Only perform resume from L2 or Early Suspend states */ |
| 3028 | if (perform_resume) { |
| 3029 | |
| 3030 | /* |
| 3031 | * In case of remote wake up dwc3_gadget_wakeup_work() |
| 3032 | * is doing pm_runtime_get_sync(). |
| 3033 | */ |
| 3034 | dev_dbg(dwc->dev, "Notify OTG from %s\n", __func__); |
| 3035 | dwc->b_suspend = false; |
| 3036 | dwc3_notify_event(dwc, DWC3_CONTROLLER_NOTIFY_OTG_EVENT); |
| 3037 | |
| 3038 | /* |
| 3039 | * set state to U0 as function level resume is trying to queue |
| 3040 | * notification over USB interrupt endpoint which would fail |
| 3041 | * due to state is not being updated. |
| 3042 | */ |
| 3043 | dwc->link_state = DWC3_LINK_STATE_U0; |
| 3044 | dwc3_resume_gadget(dwc); |
| 3045 | return; |
Jiebing Li | ad14d4e | 2014-12-11 13:26:29 +0800 | [diff] [blame] | 3046 | } |
Mayank Rana | a99689a | 2016-08-10 17:39:47 -0700 | [diff] [blame] | 3047 | |
| 3048 | dwc->link_state = DWC3_LINK_STATE_U0; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3049 | } |
| 3050 | |
| 3051 | static void dwc3_gadget_linksts_change_interrupt(struct dwc3 *dwc, |
| 3052 | unsigned int evtinfo) |
| 3053 | { |
Felipe Balbi | fae2b90 | 2011-10-14 13:00:30 +0300 | [diff] [blame] | 3054 | enum dwc3_link_state next = evtinfo & DWC3_LINK_STATE_MASK; |
Felipe Balbi | 0b0cc1c | 2012-09-18 21:39:24 +0300 | [diff] [blame] | 3055 | unsigned int pwropt; |
| 3056 | |
| 3057 | /* |
| 3058 | * WORKAROUND: DWC3 < 2.50a have an issue when configured without |
| 3059 | * Hibernation mode enabled which would show up when device detects |
| 3060 | * host-initiated U3 exit. |
| 3061 | * |
| 3062 | * In that case, device will generate a Link State Change Interrupt |
| 3063 | * from U3 to RESUME which is only necessary if Hibernation is |
| 3064 | * configured in. |
| 3065 | * |
| 3066 | * There are no functional changes due to such spurious event and we |
| 3067 | * just need to ignore it. |
| 3068 | * |
| 3069 | * Refers to: |
| 3070 | * |
| 3071 | * STAR#9000570034 RTL: SS Resume event generated in non-Hibernation |
| 3072 | * operational mode |
| 3073 | */ |
| 3074 | pwropt = DWC3_GHWPARAMS1_EN_PWROPT(dwc->hwparams.hwparams1); |
| 3075 | if ((dwc->revision < DWC3_REVISION_250A) && |
| 3076 | (pwropt != DWC3_GHWPARAMS1_EN_PWROPT_HIB)) { |
| 3077 | if ((dwc->link_state == DWC3_LINK_STATE_U3) && |
| 3078 | (next == DWC3_LINK_STATE_RESUME)) { |
Felipe Balbi | 7381528 | 2015-01-27 13:48:14 -0600 | [diff] [blame] | 3079 | dwc3_trace(trace_dwc3_gadget, |
| 3080 | "ignoring transition U3 -> Resume"); |
Felipe Balbi | 0b0cc1c | 2012-09-18 21:39:24 +0300 | [diff] [blame] | 3081 | return; |
| 3082 | } |
| 3083 | } |
Felipe Balbi | fae2b90 | 2011-10-14 13:00:30 +0300 | [diff] [blame] | 3084 | |
| 3085 | /* |
| 3086 | * WORKAROUND: DWC3 Revisions <1.83a have an issue which, depending |
| 3087 | * on the link partner, the USB session might do multiple entry/exit |
| 3088 | * of low power states before a transfer takes place. |
| 3089 | * |
| 3090 | * Due to this problem, we might experience lower throughput. The |
| 3091 | * suggested workaround is to disable DCTL[12:9] bits if we're |
| 3092 | * transitioning from U1/U2 to U0 and enable those bits again |
| 3093 | * after a transfer completes and there are no pending transfers |
| 3094 | * on any of the enabled endpoints. |
| 3095 | * |
| 3096 | * This is the first half of that workaround. |
| 3097 | * |
| 3098 | * Refers to: |
| 3099 | * |
| 3100 | * STAR#9000446952: RTL: Device SS : if U1/U2 ->U0 takes >128us |
| 3101 | * core send LGO_Ux entering U0 |
| 3102 | */ |
| 3103 | if (dwc->revision < DWC3_REVISION_183A) { |
| 3104 | if (next == DWC3_LINK_STATE_U0) { |
| 3105 | u32 u1u2; |
| 3106 | u32 reg; |
| 3107 | |
| 3108 | switch (dwc->link_state) { |
| 3109 | case DWC3_LINK_STATE_U1: |
| 3110 | case DWC3_LINK_STATE_U2: |
| 3111 | reg = dwc3_readl(dwc->regs, DWC3_DCTL); |
| 3112 | u1u2 = reg & (DWC3_DCTL_INITU2ENA |
| 3113 | | DWC3_DCTL_ACCEPTU2ENA |
| 3114 | | DWC3_DCTL_INITU1ENA |
| 3115 | | DWC3_DCTL_ACCEPTU1ENA); |
| 3116 | |
| 3117 | if (!dwc->u1u2) |
| 3118 | dwc->u1u2 = reg & u1u2; |
| 3119 | |
| 3120 | reg &= ~u1u2; |
| 3121 | |
| 3122 | dwc3_writel(dwc->regs, DWC3_DCTL, reg); |
| 3123 | break; |
| 3124 | default: |
| 3125 | /* do nothing */ |
| 3126 | break; |
| 3127 | } |
| 3128 | } |
| 3129 | } |
| 3130 | |
Felipe Balbi | bc5ba2e | 2014-02-26 10:17:07 -0600 | [diff] [blame] | 3131 | switch (next) { |
| 3132 | case DWC3_LINK_STATE_U1: |
| 3133 | if (dwc->speed == USB_SPEED_SUPER) |
| 3134 | dwc3_suspend_gadget(dwc); |
| 3135 | break; |
| 3136 | case DWC3_LINK_STATE_U2: |
| 3137 | case DWC3_LINK_STATE_U3: |
| 3138 | dwc3_suspend_gadget(dwc); |
| 3139 | break; |
| 3140 | case DWC3_LINK_STATE_RESUME: |
| 3141 | dwc3_resume_gadget(dwc); |
| 3142 | break; |
| 3143 | default: |
| 3144 | /* do nothing */ |
| 3145 | break; |
| 3146 | } |
| 3147 | |
Mayank Rana | a99689a | 2016-08-10 17:39:47 -0700 | [diff] [blame] | 3148 | dev_dbg(dwc->dev, "Going from (%d)--->(%d)\n", dwc->link_state, next); |
Felipe Balbi | e57ebc1 | 2014-04-22 13:20:12 -0500 | [diff] [blame] | 3149 | dwc->link_state = next; |
Mayank Rana | a99689a | 2016-08-10 17:39:47 -0700 | [diff] [blame] | 3150 | wake_up_interruptible(&dwc->wait_linkstate); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3151 | } |
| 3152 | |
Baolin Wang | 72704f8 | 2016-05-16 16:43:53 +0800 | [diff] [blame] | 3153 | static void dwc3_gadget_suspend_interrupt(struct dwc3 *dwc, |
Mayank Rana | a99689a | 2016-08-10 17:39:47 -0700 | [diff] [blame] | 3154 | unsigned int evtinfo) |
Baolin Wang | 72704f8 | 2016-05-16 16:43:53 +0800 | [diff] [blame] | 3155 | { |
Mayank Rana | a99689a | 2016-08-10 17:39:47 -0700 | [diff] [blame] | 3156 | enum dwc3_link_state next = evtinfo & DWC3_LINK_STATE_MASK; |
Baolin Wang | 72704f8 | 2016-05-16 16:43:53 +0800 | [diff] [blame] | 3157 | |
Mayank Rana | a99689a | 2016-08-10 17:39:47 -0700 | [diff] [blame] | 3158 | dev_dbg(dwc->dev, "%s Entry to %d\n", __func__, next); |
| 3159 | |
| 3160 | if (dwc->link_state != next && next == DWC3_LINK_STATE_U3) { |
| 3161 | /* |
| 3162 | * When first connecting the cable, even before the initial |
| 3163 | * DWC3_DEVICE_EVENT_RESET or DWC3_DEVICE_EVENT_CONNECT_DONE |
| 3164 | * events, the controller sees a DWC3_DEVICE_EVENT_SUSPEND |
| 3165 | * event. In such a case, ignore. |
| 3166 | * Ignore suspend event until device side usb is not into |
| 3167 | * CONFIGURED state. |
| 3168 | */ |
| 3169 | if (dwc->gadget.state != USB_STATE_CONFIGURED) { |
| 3170 | pr_err("%s(): state:%d. Ignore SUSPEND.\n", |
| 3171 | __func__, dwc->gadget.state); |
| 3172 | return; |
| 3173 | } |
| 3174 | |
Baolin Wang | 72704f8 | 2016-05-16 16:43:53 +0800 | [diff] [blame] | 3175 | dwc3_suspend_gadget(dwc); |
| 3176 | |
Mayank Rana | a99689a | 2016-08-10 17:39:47 -0700 | [diff] [blame] | 3177 | dev_dbg(dwc->dev, "Notify OTG from %s\n", __func__); |
| 3178 | dwc->b_suspend = true; |
| 3179 | dwc3_notify_event(dwc, DWC3_CONTROLLER_NOTIFY_OTG_EVENT); |
| 3180 | } |
| 3181 | |
Baolin Wang | 72704f8 | 2016-05-16 16:43:53 +0800 | [diff] [blame] | 3182 | dwc->link_state = next; |
Mayank Rana | a99689a | 2016-08-10 17:39:47 -0700 | [diff] [blame] | 3183 | dwc3_trace(trace_dwc3_gadget, "link state %d", dwc->link_state); |
Baolin Wang | 72704f8 | 2016-05-16 16:43:53 +0800 | [diff] [blame] | 3184 | } |
| 3185 | |
Felipe Balbi | e1dadd3 | 2014-02-25 14:47:54 -0600 | [diff] [blame] | 3186 | static void dwc3_gadget_hibernation_interrupt(struct dwc3 *dwc, |
| 3187 | unsigned int evtinfo) |
| 3188 | { |
| 3189 | unsigned int is_ss = evtinfo & BIT(4); |
| 3190 | |
| 3191 | /** |
| 3192 | * WORKAROUND: DWC3 revison 2.20a with hibernation support |
| 3193 | * have a known issue which can cause USB CV TD.9.23 to fail |
| 3194 | * randomly. |
| 3195 | * |
| 3196 | * Because of this issue, core could generate bogus hibernation |
| 3197 | * events which SW needs to ignore. |
| 3198 | * |
| 3199 | * Refers to: |
| 3200 | * |
| 3201 | * STAR#9000546576: Device Mode Hibernation: Issue in USB 2.0 |
| 3202 | * Device Fallback from SuperSpeed |
| 3203 | */ |
| 3204 | if (is_ss ^ (dwc->speed == USB_SPEED_SUPER)) |
| 3205 | return; |
| 3206 | |
| 3207 | /* enter hibernation here */ |
| 3208 | } |
| 3209 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3210 | static void dwc3_gadget_interrupt(struct dwc3 *dwc, |
| 3211 | const struct dwc3_event_devt *event) |
| 3212 | { |
| 3213 | switch (event->type) { |
| 3214 | case DWC3_DEVICE_EVENT_DISCONNECT: |
| 3215 | dwc3_gadget_disconnect_interrupt(dwc); |
| 3216 | break; |
| 3217 | case DWC3_DEVICE_EVENT_RESET: |
| 3218 | dwc3_gadget_reset_interrupt(dwc); |
| 3219 | break; |
| 3220 | case DWC3_DEVICE_EVENT_CONNECT_DONE: |
| 3221 | dwc3_gadget_conndone_interrupt(dwc); |
| 3222 | break; |
| 3223 | case DWC3_DEVICE_EVENT_WAKEUP: |
Mayank Rana | a99689a | 2016-08-10 17:39:47 -0700 | [diff] [blame] | 3224 | dwc3_gadget_wakeup_interrupt(dwc, false); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3225 | break; |
Felipe Balbi | e1dadd3 | 2014-02-25 14:47:54 -0600 | [diff] [blame] | 3226 | case DWC3_DEVICE_EVENT_HIBER_REQ: |
| 3227 | if (dev_WARN_ONCE(dwc->dev, !dwc->has_hibernation, |
| 3228 | "unexpected hibernation event\n")) |
| 3229 | break; |
| 3230 | |
| 3231 | dwc3_gadget_hibernation_interrupt(dwc, event->event_info); |
| 3232 | break; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3233 | case DWC3_DEVICE_EVENT_LINK_STATUS_CHANGE: |
| 3234 | dwc3_gadget_linksts_change_interrupt(dwc, event->event_info); |
| 3235 | break; |
Mayank Rana | a99689a | 2016-08-10 17:39:47 -0700 | [diff] [blame] | 3236 | case DWC3_DEVICE_EVENT_SUSPEND: |
Baolin Wang | 72704f8 | 2016-05-16 16:43:53 +0800 | [diff] [blame] | 3237 | if (dwc->revision < DWC3_REVISION_230A) { |
| 3238 | dwc3_trace(trace_dwc3_gadget, "End of Periodic Frame"); |
| 3239 | } else { |
| 3240 | dwc3_trace(trace_dwc3_gadget, "U3/L1-L2 Suspend Event"); |
| 3241 | |
| 3242 | /* |
| 3243 | * Ignore suspend event until the gadget enters into |
| 3244 | * USB_STATE_CONFIGURED state. |
| 3245 | */ |
| 3246 | if (dwc->gadget.state >= USB_STATE_CONFIGURED) |
| 3247 | dwc3_gadget_suspend_interrupt(dwc, |
| 3248 | event->event_info); |
| 3249 | } |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3250 | break; |
| 3251 | case DWC3_DEVICE_EVENT_SOF: |
Felipe Balbi | 7381528 | 2015-01-27 13:48:14 -0600 | [diff] [blame] | 3252 | dwc3_trace(trace_dwc3_gadget, "Start of Periodic Frame"); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3253 | break; |
| 3254 | case DWC3_DEVICE_EVENT_ERRATIC_ERROR: |
Felipe Balbi | 7381528 | 2015-01-27 13:48:14 -0600 | [diff] [blame] | 3255 | dwc3_trace(trace_dwc3_gadget, "Erratic Error"); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3256 | break; |
| 3257 | case DWC3_DEVICE_EVENT_CMD_CMPL: |
Felipe Balbi | 7381528 | 2015-01-27 13:48:14 -0600 | [diff] [blame] | 3258 | dwc3_trace(trace_dwc3_gadget, "Command Complete"); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3259 | break; |
| 3260 | case DWC3_DEVICE_EVENT_OVERFLOW: |
Felipe Balbi | 7381528 | 2015-01-27 13:48:14 -0600 | [diff] [blame] | 3261 | dwc3_trace(trace_dwc3_gadget, "Overflow"); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3262 | break; |
| 3263 | default: |
Felipe Balbi | e9f2aa87 | 2015-01-27 13:49:28 -0600 | [diff] [blame] | 3264 | dev_WARN(dwc->dev, "UNKNOWN IRQ %d\n", event->type); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3265 | } |
Mayank Rana | a99689a | 2016-08-10 17:39:47 -0700 | [diff] [blame] | 3266 | |
| 3267 | dwc->err_evt_seen = (event->type == DWC3_DEVICE_EVENT_ERRATIC_ERROR); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3268 | } |
| 3269 | |
| 3270 | static void dwc3_process_event_entry(struct dwc3 *dwc, |
| 3271 | const union dwc3_event *event) |
| 3272 | { |
Felipe Balbi | 2c4cbe6e5 | 2014-04-30 17:45:10 -0500 | [diff] [blame] | 3273 | trace_dwc3_event(event->raw); |
Mayank Rana | a99689a | 2016-08-10 17:39:47 -0700 | [diff] [blame] | 3274 | /* skip event processing in absence of vbus */ |
| 3275 | if (!dwc->vbus_active) { |
| 3276 | dev_err(dwc->dev, "SKIP EVT:%x", event->raw); |
| 3277 | return; |
| 3278 | } |
| 3279 | |
| 3280 | /* If run/stop is cleared don't process any more events */ |
| 3281 | if (!dwc->pullups_connected) { |
| 3282 | dev_err(dwc->dev, "SKIP_EVT_PULLUP:%x", event->raw); |
| 3283 | return; |
| 3284 | } |
Felipe Balbi | 2c4cbe6e5 | 2014-04-30 17:45:10 -0500 | [diff] [blame] | 3285 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3286 | /* Endpoint IRQ, handle it and return early */ |
| 3287 | if (event->type.is_devspec == 0) { |
| 3288 | /* depevt */ |
| 3289 | return dwc3_endpoint_interrupt(dwc, &event->depevt); |
| 3290 | } |
| 3291 | |
| 3292 | switch (event->type.type) { |
| 3293 | case DWC3_EVENT_TYPE_DEV: |
| 3294 | dwc3_gadget_interrupt(dwc, &event->devt); |
| 3295 | break; |
| 3296 | /* REVISIT what to do with Carkit and I2C events ? */ |
| 3297 | default: |
| 3298 | dev_err(dwc->dev, "UNKNOWN IRQ type %d\n", event->raw); |
| 3299 | } |
| 3300 | } |
| 3301 | |
Mayank Rana | a99689a | 2016-08-10 17:39:47 -0700 | [diff] [blame] | 3302 | static irqreturn_t dwc3_process_event_buf(struct dwc3 *dwc) |
Felipe Balbi | f42f244 | 2013-06-12 21:25:08 +0300 | [diff] [blame] | 3303 | { |
Mayank Rana | a99689a | 2016-08-10 17:39:47 -0700 | [diff] [blame] | 3304 | struct dwc3_event_buffer *evt; |
Felipe Balbi | f42f244 | 2013-06-12 21:25:08 +0300 | [diff] [blame] | 3305 | irqreturn_t ret = IRQ_NONE; |
| 3306 | int left; |
| 3307 | u32 reg; |
| 3308 | |
Mayank Rana | a99689a | 2016-08-10 17:39:47 -0700 | [diff] [blame] | 3309 | evt = dwc->ev_buf; |
Felipe Balbi | f42f244 | 2013-06-12 21:25:08 +0300 | [diff] [blame] | 3310 | left = evt->count; |
| 3311 | |
| 3312 | if (!(evt->flags & DWC3_EVENT_PENDING)) |
| 3313 | return IRQ_NONE; |
| 3314 | |
| 3315 | while (left > 0) { |
| 3316 | union dwc3_event event; |
| 3317 | |
| 3318 | event.raw = *(u32 *) (evt->buf + evt->lpos); |
| 3319 | |
| 3320 | dwc3_process_event_entry(dwc, &event); |
| 3321 | |
Mayank Rana | a99689a | 2016-08-10 17:39:47 -0700 | [diff] [blame] | 3322 | if (dwc->err_evt_seen) { |
| 3323 | /* |
| 3324 | * if erratic error, skip remaining events |
| 3325 | * while controller undergoes reset |
| 3326 | */ |
| 3327 | evt->lpos = (evt->lpos + left) % |
| 3328 | DWC3_EVENT_BUFFERS_SIZE; |
| 3329 | dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), left); |
| 3330 | if (dwc3_notify_event(dwc, DWC3_CONTROLLER_ERROR_EVENT)) |
| 3331 | dwc->err_evt_seen = 0; |
| 3332 | break; |
| 3333 | } |
| 3334 | |
Felipe Balbi | f42f244 | 2013-06-12 21:25:08 +0300 | [diff] [blame] | 3335 | /* |
| 3336 | * FIXME we wrap around correctly to the next entry as |
| 3337 | * almost all entries are 4 bytes in size. There is one |
| 3338 | * entry which has 12 bytes which is a regular entry |
| 3339 | * followed by 8 bytes data. ATM I don't know how |
| 3340 | * things are organized if we get next to the a |
| 3341 | * boundary so I worry about that once we try to handle |
| 3342 | * that. |
| 3343 | */ |
| 3344 | evt->lpos = (evt->lpos + 4) % DWC3_EVENT_BUFFERS_SIZE; |
| 3345 | left -= 4; |
| 3346 | |
Felipe Balbi | 660e9bd | 2016-03-30 09:26:24 +0300 | [diff] [blame] | 3347 | dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), 4); |
Felipe Balbi | f42f244 | 2013-06-12 21:25:08 +0300 | [diff] [blame] | 3348 | } |
| 3349 | |
Mayank Rana | a99689a | 2016-08-10 17:39:47 -0700 | [diff] [blame] | 3350 | dwc->bh_handled_evt_cnt[dwc->bh_dbg_index] += (evt->count / 4); |
Felipe Balbi | f42f244 | 2013-06-12 21:25:08 +0300 | [diff] [blame] | 3351 | evt->count = 0; |
| 3352 | evt->flags &= ~DWC3_EVENT_PENDING; |
| 3353 | ret = IRQ_HANDLED; |
| 3354 | |
| 3355 | /* Unmask interrupt */ |
Felipe Balbi | 660e9bd | 2016-03-30 09:26:24 +0300 | [diff] [blame] | 3356 | reg = dwc3_readl(dwc->regs, DWC3_GEVNTSIZ(0)); |
Felipe Balbi | f42f244 | 2013-06-12 21:25:08 +0300 | [diff] [blame] | 3357 | reg &= ~DWC3_GEVNTSIZ_INTMASK; |
Felipe Balbi | 660e9bd | 2016-03-30 09:26:24 +0300 | [diff] [blame] | 3358 | dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0), reg); |
Felipe Balbi | f42f244 | 2013-06-12 21:25:08 +0300 | [diff] [blame] | 3359 | |
| 3360 | return ret; |
| 3361 | } |
| 3362 | |
Mayank Rana | a99689a | 2016-08-10 17:39:47 -0700 | [diff] [blame] | 3363 | static irqreturn_t dwc3_thread_interrupt(int irq, void *_dwc) |
| 3364 | { |
| 3365 | struct dwc3 *dwc = _dwc; |
Felipe Balbi | e5f68b4 | 2015-10-12 13:25:44 -0500 | [diff] [blame] | 3366 | unsigned long flags; |
Felipe Balbi | b15a762 | 2011-06-30 16:57:15 +0300 | [diff] [blame] | 3367 | irqreturn_t ret = IRQ_NONE; |
Mayank Rana | a99689a | 2016-08-10 17:39:47 -0700 | [diff] [blame] | 3368 | unsigned int temp_time; |
| 3369 | ktime_t start_time; |
| 3370 | |
| 3371 | start_time = ktime_get(); |
Felipe Balbi | b15a762 | 2011-06-30 16:57:15 +0300 | [diff] [blame] | 3372 | |
Felipe Balbi | e5f68b4 | 2015-10-12 13:25:44 -0500 | [diff] [blame] | 3373 | spin_lock_irqsave(&dwc->lock, flags); |
Mayank Rana | a99689a | 2016-08-10 17:39:47 -0700 | [diff] [blame] | 3374 | dwc->bh_handled_evt_cnt[dwc->bh_dbg_index] = 0; |
| 3375 | |
| 3376 | ret = dwc3_process_event_buf(dwc); |
| 3377 | |
Felipe Balbi | e5f68b4 | 2015-10-12 13:25:44 -0500 | [diff] [blame] | 3378 | spin_unlock_irqrestore(&dwc->lock, flags); |
Felipe Balbi | b15a762 | 2011-06-30 16:57:15 +0300 | [diff] [blame] | 3379 | |
Mayank Rana | a99689a | 2016-08-10 17:39:47 -0700 | [diff] [blame] | 3380 | temp_time = ktime_to_us(ktime_sub(ktime_get(), start_time)); |
| 3381 | dwc->bh_completion_time[dwc->bh_dbg_index] = temp_time; |
| 3382 | dwc->bh_dbg_index = (dwc->bh_dbg_index + 1) % 10; |
| 3383 | |
Felipe Balbi | b15a762 | 2011-06-30 16:57:15 +0300 | [diff] [blame] | 3384 | return ret; |
| 3385 | } |
| 3386 | |
Mayank Rana | a99689a | 2016-08-10 17:39:47 -0700 | [diff] [blame] | 3387 | static irqreturn_t dwc3_check_event_buf(struct dwc3 *dwc) |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3388 | { |
Mayank Rana | a99689a | 2016-08-10 17:39:47 -0700 | [diff] [blame] | 3389 | struct dwc3_event_buffer *evt; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3390 | u32 count; |
Felipe Balbi | e8adfc3 | 2013-06-12 21:11:14 +0300 | [diff] [blame] | 3391 | u32 reg; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3392 | |
Mayank Rana | a99689a | 2016-08-10 17:39:47 -0700 | [diff] [blame] | 3393 | evt = dwc->ev_buf; |
Felipe Balbi | fc8bb91 | 2016-05-16 13:14:48 +0300 | [diff] [blame] | 3394 | |
Felipe Balbi | 660e9bd | 2016-03-30 09:26:24 +0300 | [diff] [blame] | 3395 | count = dwc3_readl(dwc->regs, DWC3_GEVNTCOUNT(0)); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3396 | count &= DWC3_GEVNTCOUNT_MASK; |
| 3397 | if (!count) |
| 3398 | return IRQ_NONE; |
| 3399 | |
Mayank Rana | a99689a | 2016-08-10 17:39:47 -0700 | [diff] [blame] | 3400 | if (count > evt->length) { |
| 3401 | dev_err(dwc->dev, "HUGE_EVCNT(%d)", count); |
| 3402 | evt->lpos = (evt->lpos + count) % DWC3_EVENT_BUFFERS_SIZE; |
| 3403 | dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), count); |
| 3404 | return IRQ_HANDLED; |
| 3405 | } |
| 3406 | |
Felipe Balbi | b15a762 | 2011-06-30 16:57:15 +0300 | [diff] [blame] | 3407 | evt->count = count; |
| 3408 | evt->flags |= DWC3_EVENT_PENDING; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3409 | |
Felipe Balbi | e8adfc3 | 2013-06-12 21:11:14 +0300 | [diff] [blame] | 3410 | /* Mask interrupt */ |
Felipe Balbi | 660e9bd | 2016-03-30 09:26:24 +0300 | [diff] [blame] | 3411 | reg = dwc3_readl(dwc->regs, DWC3_GEVNTSIZ(0)); |
Felipe Balbi | e8adfc3 | 2013-06-12 21:11:14 +0300 | [diff] [blame] | 3412 | reg |= DWC3_GEVNTSIZ_INTMASK; |
Felipe Balbi | 660e9bd | 2016-03-30 09:26:24 +0300 | [diff] [blame] | 3413 | dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(0), reg); |
Felipe Balbi | e8adfc3 | 2013-06-12 21:11:14 +0300 | [diff] [blame] | 3414 | |
Felipe Balbi | b15a762 | 2011-06-30 16:57:15 +0300 | [diff] [blame] | 3415 | return IRQ_WAKE_THREAD; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3416 | } |
| 3417 | |
Mayank Rana | a99689a | 2016-08-10 17:39:47 -0700 | [diff] [blame] | 3418 | irqreturn_t dwc3_interrupt(int irq, void *_dwc) |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3419 | { |
Mayank Rana | a99689a | 2016-08-10 17:39:47 -0700 | [diff] [blame] | 3420 | struct dwc3 *dwc = _dwc; |
| 3421 | irqreturn_t ret = IRQ_NONE; |
| 3422 | irqreturn_t status; |
| 3423 | unsigned int temp_cnt = 0; |
| 3424 | ktime_t start_time; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3425 | |
Mayank Rana | a99689a | 2016-08-10 17:39:47 -0700 | [diff] [blame] | 3426 | start_time = ktime_get(); |
| 3427 | dwc->irq_cnt++; |
| 3428 | |
| 3429 | /* controller reset is still pending */ |
| 3430 | if (dwc->err_evt_seen) |
| 3431 | return IRQ_HANDLED; |
| 3432 | |
| 3433 | status = dwc3_check_event_buf(dwc); |
| 3434 | if (status == IRQ_WAKE_THREAD) |
| 3435 | ret = status; |
| 3436 | |
| 3437 | dwc->irq_start_time[dwc->irq_dbg_index] = start_time; |
| 3438 | dwc->irq_completion_time[dwc->irq_dbg_index] = |
| 3439 | ktime_us_delta(ktime_get(), start_time); |
| 3440 | dwc->irq_event_count[dwc->irq_dbg_index] = temp_cnt / 4; |
| 3441 | dwc->irq_dbg_index = (dwc->irq_dbg_index + 1) % MAX_INTR_STATS; |
| 3442 | |
Hemant Kumar | 78c7c28 | 2016-08-09 12:28:55 -0700 | [diff] [blame] | 3443 | if (ret == IRQ_WAKE_THREAD) |
| 3444 | dwc3_thread_interrupt(dwc->irq, dwc); |
Mayank Rana | a99689a | 2016-08-10 17:39:47 -0700 | [diff] [blame] | 3445 | |
| 3446 | return IRQ_HANDLED; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3447 | } |
| 3448 | |
| 3449 | /** |
| 3450 | * dwc3_gadget_init - Initializes gadget related registers |
Paul Zimmerman | 1d04679 | 2012-02-15 18:56:56 -0800 | [diff] [blame] | 3451 | * @dwc: pointer to our controller context structure |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3452 | * |
| 3453 | * Returns 0 on success otherwise negative errno. |
| 3454 | */ |
Bill Pemberton | 41ac7b3 | 2012-11-19 13:21:48 -0500 | [diff] [blame] | 3455 | int dwc3_gadget_init(struct dwc3 *dwc) |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3456 | { |
Roger Quadros | 9522def | 2016-06-10 14:48:38 +0300 | [diff] [blame] | 3457 | int ret, irq; |
| 3458 | struct platform_device *dwc3_pdev = to_platform_device(dwc->dev); |
| 3459 | |
| 3460 | irq = platform_get_irq_byname(dwc3_pdev, "peripheral"); |
| 3461 | if (irq == -EPROBE_DEFER) |
| 3462 | return irq; |
| 3463 | |
| 3464 | if (irq <= 0) { |
| 3465 | irq = platform_get_irq_byname(dwc3_pdev, "dwc_usb3"); |
| 3466 | if (irq == -EPROBE_DEFER) |
| 3467 | return irq; |
| 3468 | |
| 3469 | if (irq <= 0) { |
| 3470 | irq = platform_get_irq(dwc3_pdev, 0); |
| 3471 | if (irq <= 0) { |
| 3472 | if (irq != -EPROBE_DEFER) { |
| 3473 | dev_err(dwc->dev, |
| 3474 | "missing peripheral IRQ\n"); |
| 3475 | } |
| 3476 | if (!irq) |
| 3477 | irq = -EINVAL; |
| 3478 | return irq; |
| 3479 | } |
| 3480 | } |
| 3481 | } |
| 3482 | |
| 3483 | dwc->irq_gadget = irq; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3484 | |
Mayank Rana | a99689a | 2016-08-10 17:39:47 -0700 | [diff] [blame] | 3485 | INIT_WORK(&dwc->wakeup_work, dwc3_gadget_wakeup_work); |
| 3486 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3487 | dwc->ctrl_req = dma_alloc_coherent(dwc->dev, sizeof(*dwc->ctrl_req), |
| 3488 | &dwc->ctrl_req_addr, GFP_KERNEL); |
| 3489 | if (!dwc->ctrl_req) { |
| 3490 | dev_err(dwc->dev, "failed to allocate ctrl request\n"); |
| 3491 | ret = -ENOMEM; |
| 3492 | goto err0; |
| 3493 | } |
| 3494 | |
Kishon Vijay Abraham I | 2abd9d5 | 2015-07-27 12:25:31 +0530 | [diff] [blame] | 3495 | dwc->ep0_trb = dma_alloc_coherent(dwc->dev, sizeof(*dwc->ep0_trb) * 2, |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3496 | &dwc->ep0_trb_addr, GFP_KERNEL); |
| 3497 | if (!dwc->ep0_trb) { |
| 3498 | dev_err(dwc->dev, "failed to allocate ep0 trb\n"); |
| 3499 | ret = -ENOMEM; |
| 3500 | goto err1; |
| 3501 | } |
| 3502 | |
Felipe Balbi | 3ef35fa | 2012-05-04 12:58:14 +0300 | [diff] [blame] | 3503 | dwc->setup_buf = kzalloc(DWC3_EP0_BOUNCE_SIZE, GFP_KERNEL); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3504 | if (!dwc->setup_buf) { |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3505 | ret = -ENOMEM; |
| 3506 | goto err2; |
| 3507 | } |
| 3508 | |
Felipe Balbi | 5812b1c | 2011-08-27 22:07:53 +0300 | [diff] [blame] | 3509 | dwc->ep0_bounce = dma_alloc_coherent(dwc->dev, |
Felipe Balbi | 3ef35fa | 2012-05-04 12:58:14 +0300 | [diff] [blame] | 3510 | DWC3_EP0_BOUNCE_SIZE, &dwc->ep0_bounce_addr, |
| 3511 | GFP_KERNEL); |
Felipe Balbi | 5812b1c | 2011-08-27 22:07:53 +0300 | [diff] [blame] | 3512 | if (!dwc->ep0_bounce) { |
| 3513 | dev_err(dwc->dev, "failed to allocate ep0 bounce buffer\n"); |
| 3514 | ret = -ENOMEM; |
| 3515 | goto err3; |
| 3516 | } |
| 3517 | |
Felipe Balbi | 04c03d1 | 2015-12-02 10:06:45 -0600 | [diff] [blame] | 3518 | dwc->zlp_buf = kzalloc(DWC3_ZLP_BUF_SIZE, GFP_KERNEL); |
| 3519 | if (!dwc->zlp_buf) { |
| 3520 | ret = -ENOMEM; |
| 3521 | goto err4; |
| 3522 | } |
| 3523 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3524 | dwc->gadget.ops = &dwc3_gadget_ops; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3525 | dwc->gadget.speed = USB_SPEED_UNKNOWN; |
Felipe Balbi | eeb720f | 2011-11-28 12:46:59 +0200 | [diff] [blame] | 3526 | dwc->gadget.sg_supported = true; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3527 | dwc->gadget.name = "dwc3-gadget"; |
Jianqiang Tang | 6a4290c | 2016-01-20 14:09:39 +0800 | [diff] [blame] | 3528 | dwc->gadget.is_otg = dwc->dr_mode == USB_DR_MODE_OTG; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3529 | |
| 3530 | /* |
Ben McCauley | b9e51b2 | 2015-11-16 10:47:24 -0600 | [diff] [blame] | 3531 | * FIXME We might be setting max_speed to <SUPER, however versions |
| 3532 | * <2.20a of dwc3 have an issue with metastability (documented |
| 3533 | * elsewhere in this driver) which tells us we can't set max speed to |
| 3534 | * anything lower than SUPER. |
| 3535 | * |
| 3536 | * Because gadget.max_speed is only used by composite.c and function |
| 3537 | * drivers (i.e. it won't go into dwc3's registers) we are allowing this |
| 3538 | * to happen so we avoid sending SuperSpeed Capability descriptor |
| 3539 | * together with our BOS descriptor as that could confuse host into |
| 3540 | * thinking we can handle super speed. |
| 3541 | * |
| 3542 | * Note that, in fact, we won't even support GetBOS requests when speed |
| 3543 | * is less than super speed because we don't have means, yet, to tell |
| 3544 | * composite.c that we are USB 2.0 + LPM ECN. |
| 3545 | */ |
| 3546 | if (dwc->revision < DWC3_REVISION_220A) |
| 3547 | dwc3_trace(trace_dwc3_gadget, |
Felipe Balbi | 60cfb37 | 2016-05-24 13:45:17 +0300 | [diff] [blame] | 3548 | "Changing max_speed on rev %08x", |
Ben McCauley | b9e51b2 | 2015-11-16 10:47:24 -0600 | [diff] [blame] | 3549 | dwc->revision); |
| 3550 | |
| 3551 | dwc->gadget.max_speed = dwc->maximum_speed; |
| 3552 | |
| 3553 | /* |
David Cohen | a4b9d94 | 2013-12-09 15:55:38 -0800 | [diff] [blame] | 3554 | * Per databook, DWC3 needs buffer size to be aligned to MaxPacketSize |
| 3555 | * on ep out. |
| 3556 | */ |
| 3557 | dwc->gadget.quirk_ep_out_aligned_size = true; |
| 3558 | |
| 3559 | /* |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3560 | * REVISIT: Here we should clear all pending IRQs to be |
| 3561 | * sure we're starting from a well known location. |
| 3562 | */ |
| 3563 | |
| 3564 | ret = dwc3_gadget_init_endpoints(dwc); |
| 3565 | if (ret) |
Felipe Balbi | 04c03d1 | 2015-12-02 10:06:45 -0600 | [diff] [blame] | 3566 | goto err5; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3567 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3568 | ret = usb_add_gadget_udc(dwc->dev, &dwc->gadget); |
| 3569 | if (ret) { |
| 3570 | dev_err(dwc->dev, "failed to register udc\n"); |
Felipe Balbi | 04c03d1 | 2015-12-02 10:06:45 -0600 | [diff] [blame] | 3571 | goto err5; |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3572 | } |
| 3573 | |
Mayank Rana | a99689a | 2016-08-10 17:39:47 -0700 | [diff] [blame] | 3574 | if (!dwc->is_drd) { |
| 3575 | pm_runtime_no_callbacks(&dwc->gadget.dev); |
| 3576 | pm_runtime_set_active(&dwc->gadget.dev); |
| 3577 | pm_runtime_enable(&dwc->gadget.dev); |
| 3578 | pm_runtime_get(&dwc->gadget.dev); |
| 3579 | } |
| 3580 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3581 | return 0; |
| 3582 | |
Felipe Balbi | 04c03d1 | 2015-12-02 10:06:45 -0600 | [diff] [blame] | 3583 | err5: |
| 3584 | kfree(dwc->zlp_buf); |
| 3585 | |
Felipe Balbi | 5812b1c | 2011-08-27 22:07:53 +0300 | [diff] [blame] | 3586 | err4: |
David Cohen | e1f8046 | 2013-09-11 17:42:47 -0700 | [diff] [blame] | 3587 | dwc3_gadget_free_endpoints(dwc); |
Felipe Balbi | 3ef35fa | 2012-05-04 12:58:14 +0300 | [diff] [blame] | 3588 | dma_free_coherent(dwc->dev, DWC3_EP0_BOUNCE_SIZE, |
| 3589 | dwc->ep0_bounce, dwc->ep0_bounce_addr); |
Felipe Balbi | 5812b1c | 2011-08-27 22:07:53 +0300 | [diff] [blame] | 3590 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3591 | err3: |
Felipe Balbi | 0fc9a1b | 2011-12-19 11:32:34 +0200 | [diff] [blame] | 3592 | kfree(dwc->setup_buf); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3593 | |
| 3594 | err2: |
Christophe JAILLET | 51fbc7c | 2016-10-07 22:12:39 +0200 | [diff] [blame] | 3595 | dma_free_coherent(dwc->dev, sizeof(*dwc->ep0_trb) * 2, |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3596 | dwc->ep0_trb, dwc->ep0_trb_addr); |
| 3597 | |
| 3598 | err1: |
| 3599 | dma_free_coherent(dwc->dev, sizeof(*dwc->ctrl_req), |
| 3600 | dwc->ctrl_req, dwc->ctrl_req_addr); |
| 3601 | |
| 3602 | err0: |
| 3603 | return ret; |
| 3604 | } |
| 3605 | |
Felipe Balbi | 7415f17 | 2012-04-30 14:56:33 +0300 | [diff] [blame] | 3606 | /* -------------------------------------------------------------------------- */ |
| 3607 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3608 | void dwc3_gadget_exit(struct dwc3 *dwc) |
| 3609 | { |
Mayank Rana | a99689a | 2016-08-10 17:39:47 -0700 | [diff] [blame] | 3610 | if (dwc->is_drd) { |
| 3611 | pm_runtime_put(&dwc->gadget.dev); |
| 3612 | pm_runtime_disable(&dwc->gadget.dev); |
| 3613 | } |
| 3614 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3615 | usb_del_gadget_udc(&dwc->gadget); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3616 | |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3617 | dwc3_gadget_free_endpoints(dwc); |
| 3618 | |
Felipe Balbi | 3ef35fa | 2012-05-04 12:58:14 +0300 | [diff] [blame] | 3619 | dma_free_coherent(dwc->dev, DWC3_EP0_BOUNCE_SIZE, |
| 3620 | dwc->ep0_bounce, dwc->ep0_bounce_addr); |
Felipe Balbi | 5812b1c | 2011-08-27 22:07:53 +0300 | [diff] [blame] | 3621 | |
Felipe Balbi | 0fc9a1b | 2011-12-19 11:32:34 +0200 | [diff] [blame] | 3622 | kfree(dwc->setup_buf); |
Felipe Balbi | 04c03d1 | 2015-12-02 10:06:45 -0600 | [diff] [blame] | 3623 | kfree(dwc->zlp_buf); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3624 | |
Christophe JAILLET | 51fbc7c | 2016-10-07 22:12:39 +0200 | [diff] [blame] | 3625 | dma_free_coherent(dwc->dev, sizeof(*dwc->ep0_trb) * 2, |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3626 | dwc->ep0_trb, dwc->ep0_trb_addr); |
| 3627 | |
| 3628 | dma_free_coherent(dwc->dev, sizeof(*dwc->ctrl_req), |
| 3629 | dwc->ctrl_req, dwc->ctrl_req_addr); |
Felipe Balbi | 72246da | 2011-08-19 18:10:58 +0300 | [diff] [blame] | 3630 | } |
Felipe Balbi | 7415f17 | 2012-04-30 14:56:33 +0300 | [diff] [blame] | 3631 | |
Felipe Balbi | 0b0231a | 2014-10-07 10:19:23 -0500 | [diff] [blame] | 3632 | int dwc3_gadget_suspend(struct dwc3 *dwc) |
Felipe Balbi | 7415f17 | 2012-04-30 14:56:33 +0300 | [diff] [blame] | 3633 | { |
Felipe Balbi | 9f8a67b | 2016-05-04 15:50:27 +0300 | [diff] [blame] | 3634 | int ret; |
| 3635 | |
Roger Quadros | 9772b47 | 2016-04-12 11:33:29 +0300 | [diff] [blame] | 3636 | if (!dwc->gadget_driver) |
| 3637 | return 0; |
| 3638 | |
Felipe Balbi | 9f8a67b | 2016-05-04 15:50:27 +0300 | [diff] [blame] | 3639 | ret = dwc3_gadget_run_stop(dwc, false, false); |
| 3640 | if (ret < 0) |
| 3641 | return ret; |
Felipe Balbi | 7415f17 | 2012-04-30 14:56:33 +0300 | [diff] [blame] | 3642 | |
Felipe Balbi | 9f8a67b | 2016-05-04 15:50:27 +0300 | [diff] [blame] | 3643 | dwc3_disconnect_gadget(dwc); |
| 3644 | __dwc3_gadget_stop(dwc); |
Felipe Balbi | 7415f17 | 2012-04-30 14:56:33 +0300 | [diff] [blame] | 3645 | |
| 3646 | return 0; |
| 3647 | } |
| 3648 | |
| 3649 | int dwc3_gadget_resume(struct dwc3 *dwc) |
| 3650 | { |
Felipe Balbi | 7415f17 | 2012-04-30 14:56:33 +0300 | [diff] [blame] | 3651 | int ret; |
| 3652 | |
Roger Quadros | 9772b47 | 2016-04-12 11:33:29 +0300 | [diff] [blame] | 3653 | if (!dwc->gadget_driver) |
| 3654 | return 0; |
| 3655 | |
Felipe Balbi | 9f8a67b | 2016-05-04 15:50:27 +0300 | [diff] [blame] | 3656 | ret = __dwc3_gadget_start(dwc); |
| 3657 | if (ret < 0) |
Felipe Balbi | 7415f17 | 2012-04-30 14:56:33 +0300 | [diff] [blame] | 3658 | goto err0; |
| 3659 | |
Felipe Balbi | 9f8a67b | 2016-05-04 15:50:27 +0300 | [diff] [blame] | 3660 | ret = dwc3_gadget_run_stop(dwc, true, false); |
| 3661 | if (ret < 0) |
Felipe Balbi | 7415f17 | 2012-04-30 14:56:33 +0300 | [diff] [blame] | 3662 | goto err1; |
| 3663 | |
Felipe Balbi | 7415f17 | 2012-04-30 14:56:33 +0300 | [diff] [blame] | 3664 | return 0; |
| 3665 | |
| 3666 | err1: |
Felipe Balbi | 9f8a67b | 2016-05-04 15:50:27 +0300 | [diff] [blame] | 3667 | __dwc3_gadget_stop(dwc); |
Felipe Balbi | 7415f17 | 2012-04-30 14:56:33 +0300 | [diff] [blame] | 3668 | |
| 3669 | err0: |
| 3670 | return ret; |
| 3671 | } |
Felipe Balbi | fc8bb91 | 2016-05-16 13:14:48 +0300 | [diff] [blame] | 3672 | |
| 3673 | void dwc3_gadget_process_pending_events(struct dwc3 *dwc) |
| 3674 | { |
| 3675 | if (dwc->pending_events) { |
| 3676 | dwc3_interrupt(dwc->irq_gadget, dwc->ev_buf); |
| 3677 | dwc->pending_events = false; |
| 3678 | enable_irq(dwc->irq_gadget); |
| 3679 | } |
| 3680 | } |