Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Kishon Vijay Abraham I | 85d5e70 | 2015-02-23 18:39:50 +0530 | [diff] [blame] | 2 | /** |
| 3 | * gadget.c - DesignWare USB3 DRD Controller Gadget Framework Link |
| 4 | * |
Nishanth Menon | a94a407 | 2023-11-01 15:56:03 -0500 | [diff] [blame] | 5 | * Copyright (C) 2015 Texas Instruments Incorporated - https://www.ti.com |
Kishon Vijay Abraham I | 85d5e70 | 2015-02-23 18:39:50 +0530 | [diff] [blame] | 6 | * |
| 7 | * Authors: Felipe Balbi <balbi@ti.com>, |
| 8 | * Sebastian Andrzej Siewior <bigeasy@linutronix.de> |
| 9 | * |
Kishon Vijay Abraham I | 30c31d5 | 2015-02-23 18:39:52 +0530 | [diff] [blame] | 10 | * Taken from Linux Kernel v3.19-rc1 (drivers/usb/dwc3/gadget.c) and ported |
| 11 | * to uboot. |
Kishon Vijay Abraham I | 85d5e70 | 2015-02-23 18:39:50 +0530 | [diff] [blame] | 12 | * |
Kishon Vijay Abraham I | 30c31d5 | 2015-02-23 18:39:52 +0530 | [diff] [blame] | 13 | * commit 8e74475b0e : usb: dwc3: gadget: use udc-core's reset notifier |
Kishon Vijay Abraham I | 85d5e70 | 2015-02-23 18:39:50 +0530 | [diff] [blame] | 14 | */ |
| 15 | |
Simon Glass | 1eb69ae | 2019-11-14 12:57:39 -0700 | [diff] [blame] | 16 | #include <cpu_func.h> |
Simon Glass | f7ae49f | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 17 | #include <log.h> |
Kishon Vijay Abraham I | 747a0a5 | 2015-02-23 18:39:58 +0530 | [diff] [blame] | 18 | #include <malloc.h> |
Sean Anderson | df5eabc | 2020-09-15 10:45:16 -0400 | [diff] [blame] | 19 | #include <dm.h> |
Simon Glass | 336d461 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 20 | #include <dm/device_compat.h> |
Simon Glass | 61b29b8 | 2020-02-03 07:36:15 -0700 | [diff] [blame] | 21 | #include <dm/devres.h> |
Masahiro Yamada | 84b8bf6 | 2016-01-24 23:27:48 +0900 | [diff] [blame] | 22 | #include <linux/bug.h> |
Simon Glass | c05ed00 | 2020-05-10 11:40:11 -0600 | [diff] [blame] | 23 | #include <linux/delay.h> |
Masahiro Yamada | 9d86b89 | 2020-02-14 16:40:19 +0900 | [diff] [blame] | 24 | #include <linux/dma-mapping.h> |
Kishon Vijay Abraham I | 85d5e70 | 2015-02-23 18:39:50 +0530 | [diff] [blame] | 25 | #include <linux/list.h> |
Simon Glass | 1e94b46 | 2023-09-14 18:21:46 -0600 | [diff] [blame] | 26 | #include <linux/printk.h> |
Kishon Vijay Abraham I | 85d5e70 | 2015-02-23 18:39:50 +0530 | [diff] [blame] | 27 | |
| 28 | #include <linux/usb/ch9.h> |
| 29 | #include <linux/usb/gadget.h> |
| 30 | |
Kishon Vijay Abraham I | 85d5e70 | 2015-02-23 18:39:50 +0530 | [diff] [blame] | 31 | #include "core.h" |
| 32 | #include "gadget.h" |
| 33 | #include "io.h" |
| 34 | |
Kishon Vijay Abraham I | 747a0a5 | 2015-02-23 18:39:58 +0530 | [diff] [blame] | 35 | #include "linux-compat.h" |
| 36 | |
Kishon Vijay Abraham I | 85d5e70 | 2015-02-23 18:39:50 +0530 | [diff] [blame] | 37 | /** |
| 38 | * dwc3_gadget_set_test_mode - Enables USB2 Test Modes |
| 39 | * @dwc: pointer to our context structure |
| 40 | * @mode: the mode to set (J, K SE0 NAK, Force Enable) |
| 41 | * |
| 42 | * Caller should take care of locking. This function will |
| 43 | * return 0 on success or -EINVAL if wrong Test Selector |
| 44 | * is passed |
| 45 | */ |
| 46 | int dwc3_gadget_set_test_mode(struct dwc3 *dwc, int mode) |
| 47 | { |
| 48 | u32 reg; |
| 49 | |
| 50 | reg = dwc3_readl(dwc->regs, DWC3_DCTL); |
| 51 | reg &= ~DWC3_DCTL_TSTCTRL_MASK; |
| 52 | |
| 53 | switch (mode) { |
| 54 | case TEST_J: |
| 55 | case TEST_K: |
| 56 | case TEST_SE0_NAK: |
| 57 | case TEST_PACKET: |
| 58 | case TEST_FORCE_EN: |
| 59 | reg |= mode << 1; |
| 60 | break; |
| 61 | default: |
| 62 | return -EINVAL; |
| 63 | } |
| 64 | |
| 65 | dwc3_writel(dwc->regs, DWC3_DCTL, reg); |
| 66 | |
| 67 | return 0; |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * dwc3_gadget_get_link_state - Gets current state of USB Link |
| 72 | * @dwc: pointer to our context structure |
| 73 | * |
| 74 | * Caller should take care of locking. This function will |
| 75 | * return the link state on success (>= 0) or -ETIMEDOUT. |
| 76 | */ |
| 77 | int dwc3_gadget_get_link_state(struct dwc3 *dwc) |
| 78 | { |
| 79 | u32 reg; |
| 80 | |
| 81 | reg = dwc3_readl(dwc->regs, DWC3_DSTS); |
| 82 | |
| 83 | return DWC3_DSTS_USBLNKST(reg); |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * dwc3_gadget_set_link_state - Sets USB Link to a particular State |
| 88 | * @dwc: pointer to our context structure |
| 89 | * @state: the state to put link into |
| 90 | * |
| 91 | * Caller should take care of locking. This function will |
| 92 | * return 0 on success or -ETIMEDOUT. |
| 93 | */ |
| 94 | int dwc3_gadget_set_link_state(struct dwc3 *dwc, enum dwc3_link_state state) |
| 95 | { |
| 96 | int retries = 10000; |
| 97 | u32 reg; |
| 98 | |
| 99 | /* |
| 100 | * Wait until device controller is ready. Only applies to 1.94a and |
| 101 | * later RTL. |
| 102 | */ |
| 103 | if (dwc->revision >= DWC3_REVISION_194A) { |
| 104 | while (--retries) { |
| 105 | reg = dwc3_readl(dwc->regs, DWC3_DSTS); |
| 106 | if (reg & DWC3_DSTS_DCNRD) |
| 107 | udelay(5); |
| 108 | else |
| 109 | break; |
| 110 | } |
| 111 | |
| 112 | if (retries <= 0) |
| 113 | return -ETIMEDOUT; |
| 114 | } |
| 115 | |
| 116 | reg = dwc3_readl(dwc->regs, DWC3_DCTL); |
| 117 | reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK; |
| 118 | |
| 119 | /* set requested state */ |
| 120 | reg |= DWC3_DCTL_ULSTCHNGREQ(state); |
| 121 | dwc3_writel(dwc->regs, DWC3_DCTL, reg); |
| 122 | |
| 123 | /* |
| 124 | * The following code is racy when called from dwc3_gadget_wakeup, |
| 125 | * and is not needed, at least on newer versions |
| 126 | */ |
| 127 | if (dwc->revision >= DWC3_REVISION_194A) |
| 128 | return 0; |
| 129 | |
| 130 | /* wait for a change in DSTS */ |
| 131 | retries = 10000; |
| 132 | while (--retries) { |
| 133 | reg = dwc3_readl(dwc->regs, DWC3_DSTS); |
| 134 | |
| 135 | if (DWC3_DSTS_USBLNKST(reg) == state) |
| 136 | return 0; |
| 137 | |
| 138 | udelay(5); |
| 139 | } |
| 140 | |
| 141 | dev_vdbg(dwc->dev, "link state change request timed out\n"); |
| 142 | |
| 143 | return -ETIMEDOUT; |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * dwc3_gadget_resize_tx_fifos - reallocate fifo spaces for current use-case |
| 148 | * @dwc: pointer to our context structure |
| 149 | * |
| 150 | * This function will a best effort FIFO allocation in order |
| 151 | * to improve FIFO usage and throughput, while still allowing |
| 152 | * us to enable as many endpoints as possible. |
| 153 | * |
| 154 | * Keep in mind that this operation will be highly dependent |
| 155 | * on the configured size for RAM1 - which contains TxFifo -, |
| 156 | * the amount of endpoints enabled on coreConsultant tool, and |
| 157 | * the width of the Master Bus. |
| 158 | * |
| 159 | * In the ideal world, we would always be able to satisfy the |
| 160 | * following equation: |
| 161 | * |
| 162 | * ((512 + 2 * MDWIDTH-Bytes) + (Number of IN Endpoints - 1) * \ |
| 163 | * (3 * (1024 + MDWIDTH-Bytes) + MDWIDTH-Bytes)) / MDWIDTH-Bytes |
| 164 | * |
| 165 | * Unfortunately, due to many variables that's not always the case. |
| 166 | */ |
| 167 | int dwc3_gadget_resize_tx_fifos(struct dwc3 *dwc) |
| 168 | { |
| 169 | int last_fifo_depth = 0; |
Kishon Vijay Abraham I | 85d5e70 | 2015-02-23 18:39:50 +0530 | [diff] [blame] | 170 | int fifo_size; |
| 171 | int mdwidth; |
| 172 | int num; |
| 173 | |
| 174 | if (!dwc->needs_fifo_resize) |
| 175 | return 0; |
| 176 | |
Kishon Vijay Abraham I | 85d5e70 | 2015-02-23 18:39:50 +0530 | [diff] [blame] | 177 | mdwidth = DWC3_MDWIDTH(dwc->hwparams.hwparams0); |
| 178 | |
| 179 | /* MDWIDTH is represented in bits, we need it in bytes */ |
| 180 | mdwidth >>= 3; |
| 181 | |
| 182 | /* |
| 183 | * FIXME For now we will only allocate 1 wMaxPacketSize space |
| 184 | * for each enabled endpoint, later patches will come to |
| 185 | * improve this algorithm so that we better use the internal |
| 186 | * FIFO space |
| 187 | */ |
| 188 | for (num = 0; num < dwc->num_in_eps; num++) { |
| 189 | /* bit0 indicates direction; 1 means IN ep */ |
| 190 | struct dwc3_ep *dep = dwc->eps[(num << 1) | 1]; |
| 191 | int mult = 1; |
| 192 | int tmp; |
| 193 | |
| 194 | if (!(dep->flags & DWC3_EP_ENABLED)) |
| 195 | continue; |
| 196 | |
| 197 | if (usb_endpoint_xfer_bulk(dep->endpoint.desc) |
| 198 | || usb_endpoint_xfer_isoc(dep->endpoint.desc)) |
| 199 | mult = 3; |
| 200 | |
| 201 | /* |
| 202 | * REVISIT: the following assumes we will always have enough |
| 203 | * space available on the FIFO RAM for all possible use cases. |
| 204 | * Make sure that's true somehow and change FIFO allocation |
| 205 | * accordingly. |
| 206 | * |
| 207 | * If we have Bulk or Isochronous endpoints, we want |
| 208 | * them to be able to be very, very fast. So we're giving |
| 209 | * those endpoints a fifo_size which is enough for 3 full |
| 210 | * packets |
| 211 | */ |
| 212 | tmp = mult * (dep->endpoint.maxpacket + mdwidth); |
| 213 | tmp += mdwidth; |
| 214 | |
| 215 | fifo_size = DIV_ROUND_UP(tmp, mdwidth); |
| 216 | |
| 217 | fifo_size |= (last_fifo_depth << 16); |
| 218 | |
| 219 | dev_vdbg(dwc->dev, "%s: Fifo Addr %04x Size %d\n", |
| 220 | dep->name, last_fifo_depth, fifo_size & 0xffff); |
| 221 | |
| 222 | dwc3_writel(dwc->regs, DWC3_GTXFIFOSIZ(num), fifo_size); |
| 223 | |
| 224 | last_fifo_depth += (fifo_size & 0xffff); |
| 225 | } |
| 226 | |
| 227 | return 0; |
| 228 | } |
| 229 | |
| 230 | void dwc3_gadget_giveback(struct dwc3_ep *dep, struct dwc3_request *req, |
| 231 | int status) |
| 232 | { |
| 233 | struct dwc3 *dwc = dep->dwc; |
Kishon Vijay Abraham I | 85d5e70 | 2015-02-23 18:39:50 +0530 | [diff] [blame] | 234 | |
| 235 | if (req->queued) { |
Kishon Vijay Abraham I | 747a0a5 | 2015-02-23 18:39:58 +0530 | [diff] [blame] | 236 | dep->busy_slot++; |
| 237 | /* |
| 238 | * Skip LINK TRB. We can't use req->trb and check for |
| 239 | * DWC3_TRBCTL_LINK_TRB because it points the TRB we |
| 240 | * just completed (not the LINK TRB). |
| 241 | */ |
| 242 | if (((dep->busy_slot & DWC3_TRB_MASK) == |
| 243 | DWC3_TRB_NUM- 1) && |
| 244 | usb_endpoint_xfer_isoc(dep->endpoint.desc)) |
Kishon Vijay Abraham I | 85d5e70 | 2015-02-23 18:39:50 +0530 | [diff] [blame] | 245 | dep->busy_slot++; |
Kishon Vijay Abraham I | 85d5e70 | 2015-02-23 18:39:50 +0530 | [diff] [blame] | 246 | req->queued = false; |
| 247 | } |
Kishon Vijay Abraham I | 747a0a5 | 2015-02-23 18:39:58 +0530 | [diff] [blame] | 248 | |
Kishon Vijay Abraham I | 85d5e70 | 2015-02-23 18:39:50 +0530 | [diff] [blame] | 249 | list_del(&req->list); |
| 250 | req->trb = NULL; |
Marek Szyprowski | fd15b58 | 2019-10-02 14:19:14 +0200 | [diff] [blame] | 251 | if (req->request.length) |
| 252 | dwc3_flush_cache((uintptr_t)req->request.dma, req->request.length); |
Kishon Vijay Abraham I | 85d5e70 | 2015-02-23 18:39:50 +0530 | [diff] [blame] | 253 | |
| 254 | if (req->request.status == -EINPROGRESS) |
| 255 | req->request.status = status; |
| 256 | |
| 257 | if (dwc->ep0_bounced && dep->number == 0) |
| 258 | dwc->ep0_bounced = false; |
| 259 | else |
| 260 | usb_gadget_unmap_request(&dwc->gadget, &req->request, |
| 261 | req->direction); |
| 262 | |
| 263 | dev_dbg(dwc->dev, "request %p from %s completed %d/%d ===> %d\n", |
| 264 | req, dep->name, req->request.actual, |
| 265 | req->request.length, status); |
Kishon Vijay Abraham I | 85d5e70 | 2015-02-23 18:39:50 +0530 | [diff] [blame] | 266 | |
| 267 | spin_unlock(&dwc->lock); |
| 268 | usb_gadget_giveback_request(&dep->endpoint, &req->request); |
| 269 | spin_lock(&dwc->lock); |
| 270 | } |
| 271 | |
| 272 | int dwc3_send_gadget_generic_command(struct dwc3 *dwc, unsigned cmd, u32 param) |
| 273 | { |
| 274 | u32 timeout = 500; |
| 275 | u32 reg; |
| 276 | |
Kishon Vijay Abraham I | 85d5e70 | 2015-02-23 18:39:50 +0530 | [diff] [blame] | 277 | dwc3_writel(dwc->regs, DWC3_DGCMDPAR, param); |
| 278 | dwc3_writel(dwc->regs, DWC3_DGCMD, cmd | DWC3_DGCMD_CMDACT); |
| 279 | |
| 280 | do { |
| 281 | reg = dwc3_readl(dwc->regs, DWC3_DGCMD); |
| 282 | if (!(reg & DWC3_DGCMD_CMDACT)) { |
| 283 | dev_vdbg(dwc->dev, "Command Complete --> %d\n", |
| 284 | DWC3_DGCMD_STATUS(reg)); |
| 285 | return 0; |
| 286 | } |
| 287 | |
| 288 | /* |
| 289 | * We can't sleep here, because it's also called from |
| 290 | * interrupt context. |
| 291 | */ |
| 292 | timeout--; |
| 293 | if (!timeout) |
| 294 | return -ETIMEDOUT; |
| 295 | udelay(1); |
| 296 | } while (1); |
| 297 | } |
| 298 | |
| 299 | int dwc3_send_gadget_ep_cmd(struct dwc3 *dwc, unsigned ep, |
| 300 | unsigned cmd, struct dwc3_gadget_ep_cmd_params *params) |
| 301 | { |
Kishon Vijay Abraham I | 85d5e70 | 2015-02-23 18:39:50 +0530 | [diff] [blame] | 302 | u32 timeout = 500; |
| 303 | u32 reg; |
Felipe Balbi | 1339550 | 2024-04-12 22:26:01 +0200 | [diff] [blame^] | 304 | int ret = -EINVAL; |
Kishon Vijay Abraham I | 85d5e70 | 2015-02-23 18:39:50 +0530 | [diff] [blame] | 305 | |
Kishon Vijay Abraham I | 85d5e70 | 2015-02-23 18:39:50 +0530 | [diff] [blame] | 306 | dwc3_writel(dwc->regs, DWC3_DEPCMDPAR0(ep), params->param0); |
| 307 | dwc3_writel(dwc->regs, DWC3_DEPCMDPAR1(ep), params->param1); |
| 308 | dwc3_writel(dwc->regs, DWC3_DEPCMDPAR2(ep), params->param2); |
| 309 | |
| 310 | dwc3_writel(dwc->regs, DWC3_DEPCMD(ep), cmd | DWC3_DEPCMD_CMDACT); |
| 311 | do { |
| 312 | reg = dwc3_readl(dwc->regs, DWC3_DEPCMD(ep)); |
| 313 | if (!(reg & DWC3_DEPCMD_CMDACT)) { |
| 314 | dev_vdbg(dwc->dev, "Command Complete --> %d\n", |
| 315 | DWC3_DEPCMD_STATUS(reg)); |
Felipe Balbi | 1339550 | 2024-04-12 22:26:01 +0200 | [diff] [blame^] | 316 | ret = 0; |
| 317 | break; |
Kishon Vijay Abraham I | 85d5e70 | 2015-02-23 18:39:50 +0530 | [diff] [blame] | 318 | } |
| 319 | |
| 320 | /* |
| 321 | * We can't sleep here, because it is also called from |
| 322 | * interrupt context. |
| 323 | */ |
| 324 | timeout--; |
Felipe Balbi | 1339550 | 2024-04-12 22:26:01 +0200 | [diff] [blame^] | 325 | if (!timeout) { |
| 326 | ret = -ETIMEDOUT; |
| 327 | break; |
| 328 | } |
Kishon Vijay Abraham I | 85d5e70 | 2015-02-23 18:39:50 +0530 | [diff] [blame] | 329 | |
| 330 | udelay(1); |
| 331 | } while (1); |
Felipe Balbi | 1339550 | 2024-04-12 22:26:01 +0200 | [diff] [blame^] | 332 | |
| 333 | return ret; |
Kishon Vijay Abraham I | 85d5e70 | 2015-02-23 18:39:50 +0530 | [diff] [blame] | 334 | } |
| 335 | |
| 336 | static dma_addr_t dwc3_trb_dma_offset(struct dwc3_ep *dep, |
| 337 | struct dwc3_trb *trb) |
| 338 | { |
| 339 | u32 offset = (char *) trb - (char *) dep->trb_pool; |
| 340 | |
| 341 | return dep->trb_pool_dma + offset; |
| 342 | } |
| 343 | |
| 344 | static int dwc3_alloc_trb_pool(struct dwc3_ep *dep) |
| 345 | { |
Kishon Vijay Abraham I | 85d5e70 | 2015-02-23 18:39:50 +0530 | [diff] [blame] | 346 | if (dep->trb_pool) |
| 347 | return 0; |
| 348 | |
| 349 | if (dep->number == 0 || dep->number == 1) |
| 350 | return 0; |
| 351 | |
Kishon Vijay Abraham I | 747a0a5 | 2015-02-23 18:39:58 +0530 | [diff] [blame] | 352 | dep->trb_pool = dma_alloc_coherent(sizeof(struct dwc3_trb) * |
| 353 | DWC3_TRB_NUM, |
| 354 | (unsigned long *)&dep->trb_pool_dma); |
Kishon Vijay Abraham I | 85d5e70 | 2015-02-23 18:39:50 +0530 | [diff] [blame] | 355 | if (!dep->trb_pool) { |
| 356 | dev_err(dep->dwc->dev, "failed to allocate trb pool for %s\n", |
| 357 | dep->name); |
| 358 | return -ENOMEM; |
| 359 | } |
| 360 | |
| 361 | return 0; |
| 362 | } |
| 363 | |
| 364 | static void dwc3_free_trb_pool(struct dwc3_ep *dep) |
| 365 | { |
Kishon Vijay Abraham I | 747a0a5 | 2015-02-23 18:39:58 +0530 | [diff] [blame] | 366 | dma_free_coherent(dep->trb_pool); |
Kishon Vijay Abraham I | 85d5e70 | 2015-02-23 18:39:50 +0530 | [diff] [blame] | 367 | |
| 368 | dep->trb_pool = NULL; |
| 369 | dep->trb_pool_dma = 0; |
| 370 | } |
| 371 | |
| 372 | static int dwc3_gadget_start_config(struct dwc3 *dwc, struct dwc3_ep *dep) |
| 373 | { |
| 374 | struct dwc3_gadget_ep_cmd_params params; |
| 375 | u32 cmd; |
| 376 | |
| 377 | memset(¶ms, 0x00, sizeof(params)); |
| 378 | |
| 379 | if (dep->number != 1) { |
| 380 | cmd = DWC3_DEPCMD_DEPSTARTCFG; |
| 381 | /* XferRscIdx == 0 for ep0 and 2 for the remaining */ |
| 382 | if (dep->number > 1) { |
| 383 | if (dwc->start_config_issued) |
| 384 | return 0; |
| 385 | dwc->start_config_issued = true; |
| 386 | cmd |= DWC3_DEPCMD_PARAM(2); |
| 387 | } |
| 388 | |
| 389 | return dwc3_send_gadget_ep_cmd(dwc, 0, cmd, ¶ms); |
| 390 | } |
| 391 | |
| 392 | return 0; |
| 393 | } |
| 394 | |
| 395 | static int dwc3_gadget_set_ep_config(struct dwc3 *dwc, struct dwc3_ep *dep, |
| 396 | const struct usb_endpoint_descriptor *desc, |
| 397 | const struct usb_ss_ep_comp_descriptor *comp_desc, |
| 398 | bool ignore, bool restore) |
| 399 | { |
| 400 | struct dwc3_gadget_ep_cmd_params params; |
| 401 | |
| 402 | memset(¶ms, 0x00, sizeof(params)); |
| 403 | |
| 404 | params.param0 = DWC3_DEPCFG_EP_TYPE(usb_endpoint_type(desc)) |
| 405 | | DWC3_DEPCFG_MAX_PACKET_SIZE(usb_endpoint_maxp(desc)); |
| 406 | |
| 407 | /* Burst size is only needed in SuperSpeed mode */ |
| 408 | if (dwc->gadget.speed == USB_SPEED_SUPER) { |
| 409 | u32 burst = dep->endpoint.maxburst - 1; |
| 410 | |
| 411 | params.param0 |= DWC3_DEPCFG_BURST_SIZE(burst); |
| 412 | } |
| 413 | |
| 414 | if (ignore) |
| 415 | params.param0 |= DWC3_DEPCFG_IGN_SEQ_NUM; |
| 416 | |
| 417 | if (restore) { |
| 418 | params.param0 |= DWC3_DEPCFG_ACTION_RESTORE; |
| 419 | params.param2 |= dep->saved_state; |
| 420 | } |
| 421 | |
| 422 | params.param1 = DWC3_DEPCFG_XFER_COMPLETE_EN |
| 423 | | DWC3_DEPCFG_XFER_NOT_READY_EN; |
| 424 | |
| 425 | if (usb_ss_max_streams(comp_desc) && usb_endpoint_xfer_bulk(desc)) { |
| 426 | params.param1 |= DWC3_DEPCFG_STREAM_CAPABLE |
| 427 | | DWC3_DEPCFG_STREAM_EVENT_EN; |
| 428 | dep->stream_capable = true; |
| 429 | } |
| 430 | |
| 431 | if (!usb_endpoint_xfer_control(desc)) |
| 432 | params.param1 |= DWC3_DEPCFG_XFER_IN_PROGRESS_EN; |
| 433 | |
| 434 | /* |
| 435 | * We are doing 1:1 mapping for endpoints, meaning |
| 436 | * Physical Endpoints 2 maps to Logical Endpoint 2 and |
| 437 | * so on. We consider the direction bit as part of the physical |
| 438 | * endpoint number. So USB endpoint 0x81 is 0x03. |
| 439 | */ |
| 440 | params.param1 |= DWC3_DEPCFG_EP_NUMBER(dep->number); |
| 441 | |
| 442 | /* |
| 443 | * We must use the lower 16 TX FIFOs even though |
| 444 | * HW might have more |
| 445 | */ |
| 446 | if (dep->direction) |
| 447 | params.param0 |= DWC3_DEPCFG_FIFO_NUMBER(dep->number >> 1); |
| 448 | |
| 449 | if (desc->bInterval) { |
| 450 | params.param1 |= DWC3_DEPCFG_BINTERVAL_M1(desc->bInterval - 1); |
| 451 | dep->interval = 1 << (desc->bInterval - 1); |
| 452 | } |
| 453 | |
| 454 | return dwc3_send_gadget_ep_cmd(dwc, dep->number, |
| 455 | DWC3_DEPCMD_SETEPCONFIG, ¶ms); |
| 456 | } |
| 457 | |
| 458 | static int dwc3_gadget_set_xfer_resource(struct dwc3 *dwc, struct dwc3_ep *dep) |
| 459 | { |
| 460 | struct dwc3_gadget_ep_cmd_params params; |
| 461 | |
| 462 | memset(¶ms, 0x00, sizeof(params)); |
| 463 | |
| 464 | params.param0 = DWC3_DEPXFERCFG_NUM_XFER_RES(1); |
| 465 | |
| 466 | return dwc3_send_gadget_ep_cmd(dwc, dep->number, |
| 467 | DWC3_DEPCMD_SETTRANSFRESOURCE, ¶ms); |
| 468 | } |
| 469 | |
| 470 | /** |
| 471 | * __dwc3_gadget_ep_enable - Initializes a HW endpoint |
| 472 | * @dep: endpoint to be initialized |
| 473 | * @desc: USB Endpoint Descriptor |
| 474 | * |
| 475 | * Caller should take care of locking |
| 476 | */ |
| 477 | static int __dwc3_gadget_ep_enable(struct dwc3_ep *dep, |
| 478 | const struct usb_endpoint_descriptor *desc, |
| 479 | const struct usb_ss_ep_comp_descriptor *comp_desc, |
| 480 | bool ignore, bool restore) |
| 481 | { |
| 482 | struct dwc3 *dwc = dep->dwc; |
| 483 | u32 reg; |
| 484 | int ret; |
| 485 | |
| 486 | dev_vdbg(dwc->dev, "Enabling %s\n", dep->name); |
| 487 | |
| 488 | if (!(dep->flags & DWC3_EP_ENABLED)) { |
| 489 | ret = dwc3_gadget_start_config(dwc, dep); |
| 490 | if (ret) |
| 491 | return ret; |
| 492 | } |
| 493 | |
| 494 | ret = dwc3_gadget_set_ep_config(dwc, dep, desc, comp_desc, ignore, |
| 495 | restore); |
| 496 | if (ret) |
| 497 | return ret; |
| 498 | |
| 499 | if (!(dep->flags & DWC3_EP_ENABLED)) { |
| 500 | struct dwc3_trb *trb_st_hw; |
| 501 | struct dwc3_trb *trb_link; |
| 502 | |
| 503 | ret = dwc3_gadget_set_xfer_resource(dwc, dep); |
| 504 | if (ret) |
| 505 | return ret; |
| 506 | |
| 507 | dep->endpoint.desc = desc; |
| 508 | dep->comp_desc = comp_desc; |
| 509 | dep->type = usb_endpoint_type(desc); |
| 510 | dep->flags |= DWC3_EP_ENABLED; |
| 511 | |
| 512 | reg = dwc3_readl(dwc->regs, DWC3_DALEPENA); |
| 513 | reg |= DWC3_DALEPENA_EP(dep->number); |
| 514 | dwc3_writel(dwc->regs, DWC3_DALEPENA, reg); |
| 515 | |
| 516 | if (!usb_endpoint_xfer_isoc(desc)) |
| 517 | return 0; |
| 518 | |
| 519 | /* Link TRB for ISOC. The HWO bit is never reset */ |
| 520 | trb_st_hw = &dep->trb_pool[0]; |
| 521 | |
| 522 | trb_link = &dep->trb_pool[DWC3_TRB_NUM - 1]; |
| 523 | memset(trb_link, 0, sizeof(*trb_link)); |
| 524 | |
| 525 | trb_link->bpl = lower_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw)); |
| 526 | trb_link->bph = upper_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw)); |
| 527 | trb_link->ctrl |= DWC3_TRBCTL_LINK_TRB; |
| 528 | trb_link->ctrl |= DWC3_TRB_CTRL_HWO; |
| 529 | } |
| 530 | |
| 531 | return 0; |
| 532 | } |
| 533 | |
| 534 | static void dwc3_stop_active_transfer(struct dwc3 *dwc, u32 epnum, bool force); |
| 535 | static void dwc3_remove_requests(struct dwc3 *dwc, struct dwc3_ep *dep) |
| 536 | { |
| 537 | struct dwc3_request *req; |
| 538 | |
| 539 | if (!list_empty(&dep->req_queued)) { |
| 540 | dwc3_stop_active_transfer(dwc, dep->number, true); |
| 541 | |
| 542 | /* - giveback all requests to gadget driver */ |
| 543 | while (!list_empty(&dep->req_queued)) { |
| 544 | req = next_request(&dep->req_queued); |
| 545 | |
| 546 | dwc3_gadget_giveback(dep, req, -ESHUTDOWN); |
| 547 | } |
| 548 | } |
| 549 | |
| 550 | while (!list_empty(&dep->request_list)) { |
| 551 | req = next_request(&dep->request_list); |
| 552 | |
| 553 | dwc3_gadget_giveback(dep, req, -ESHUTDOWN); |
| 554 | } |
| 555 | } |
| 556 | |
| 557 | /** |
| 558 | * __dwc3_gadget_ep_disable - Disables a HW endpoint |
| 559 | * @dep: the endpoint to disable |
| 560 | * |
| 561 | * This function also removes requests which are currently processed ny the |
| 562 | * hardware and those which are not yet scheduled. |
| 563 | * Caller should take care of locking. |
| 564 | */ |
| 565 | static int __dwc3_gadget_ep_disable(struct dwc3_ep *dep) |
| 566 | { |
| 567 | struct dwc3 *dwc = dep->dwc; |
| 568 | u32 reg; |
| 569 | |
| 570 | dwc3_remove_requests(dwc, dep); |
| 571 | |
| 572 | /* make sure HW endpoint isn't stalled */ |
| 573 | if (dep->flags & DWC3_EP_STALL) |
| 574 | __dwc3_gadget_ep_set_halt(dep, 0, false); |
| 575 | |
| 576 | reg = dwc3_readl(dwc->regs, DWC3_DALEPENA); |
| 577 | reg &= ~DWC3_DALEPENA_EP(dep->number); |
| 578 | dwc3_writel(dwc->regs, DWC3_DALEPENA, reg); |
| 579 | |
| 580 | dep->stream_capable = false; |
| 581 | dep->endpoint.desc = NULL; |
| 582 | dep->comp_desc = NULL; |
| 583 | dep->type = 0; |
| 584 | dep->flags = 0; |
| 585 | |
| 586 | return 0; |
| 587 | } |
| 588 | |
| 589 | /* -------------------------------------------------------------------------- */ |
| 590 | |
| 591 | static int dwc3_gadget_ep0_enable(struct usb_ep *ep, |
| 592 | const struct usb_endpoint_descriptor *desc) |
| 593 | { |
| 594 | return -EINVAL; |
| 595 | } |
| 596 | |
| 597 | static int dwc3_gadget_ep0_disable(struct usb_ep *ep) |
| 598 | { |
| 599 | return -EINVAL; |
| 600 | } |
| 601 | |
| 602 | /* -------------------------------------------------------------------------- */ |
| 603 | |
| 604 | static int dwc3_gadget_ep_enable(struct usb_ep *ep, |
| 605 | const struct usb_endpoint_descriptor *desc) |
| 606 | { |
| 607 | struct dwc3_ep *dep; |
Kishon Vijay Abraham I | 85d5e70 | 2015-02-23 18:39:50 +0530 | [diff] [blame] | 608 | unsigned long flags; |
| 609 | int ret; |
| 610 | |
| 611 | if (!ep || !desc || desc->bDescriptorType != USB_DT_ENDPOINT) { |
| 612 | pr_debug("dwc3: invalid parameters\n"); |
| 613 | return -EINVAL; |
| 614 | } |
| 615 | |
| 616 | if (!desc->wMaxPacketSize) { |
| 617 | pr_debug("dwc3: missing wMaxPacketSize\n"); |
| 618 | return -EINVAL; |
| 619 | } |
| 620 | |
| 621 | dep = to_dwc3_ep(ep); |
Kishon Vijay Abraham I | 85d5e70 | 2015-02-23 18:39:50 +0530 | [diff] [blame] | 622 | |
| 623 | if (dep->flags & DWC3_EP_ENABLED) { |
Kishon Vijay Abraham I | 747a0a5 | 2015-02-23 18:39:58 +0530 | [diff] [blame] | 624 | WARN(true, "%s is already enabled\n", |
Kishon Vijay Abraham I | 85d5e70 | 2015-02-23 18:39:50 +0530 | [diff] [blame] | 625 | dep->name); |
| 626 | return 0; |
| 627 | } |
| 628 | |
| 629 | switch (usb_endpoint_type(desc)) { |
| 630 | case USB_ENDPOINT_XFER_CONTROL: |
| 631 | strlcat(dep->name, "-control", sizeof(dep->name)); |
| 632 | break; |
| 633 | case USB_ENDPOINT_XFER_ISOC: |
| 634 | strlcat(dep->name, "-isoc", sizeof(dep->name)); |
| 635 | break; |
| 636 | case USB_ENDPOINT_XFER_BULK: |
| 637 | strlcat(dep->name, "-bulk", sizeof(dep->name)); |
| 638 | break; |
| 639 | case USB_ENDPOINT_XFER_INT: |
| 640 | strlcat(dep->name, "-int", sizeof(dep->name)); |
| 641 | break; |
| 642 | default: |
Sean Anderson | df5eabc | 2020-09-15 10:45:16 -0400 | [diff] [blame] | 643 | dev_err(dep->dwc->dev, "invalid endpoint transfer type\n"); |
Kishon Vijay Abraham I | 85d5e70 | 2015-02-23 18:39:50 +0530 | [diff] [blame] | 644 | } |
| 645 | |
| 646 | spin_lock_irqsave(&dwc->lock, flags); |
| 647 | ret = __dwc3_gadget_ep_enable(dep, desc, ep->comp_desc, false, false); |
| 648 | spin_unlock_irqrestore(&dwc->lock, flags); |
| 649 | |
| 650 | return ret; |
| 651 | } |
| 652 | |
| 653 | static int dwc3_gadget_ep_disable(struct usb_ep *ep) |
| 654 | { |
| 655 | struct dwc3_ep *dep; |
Kishon Vijay Abraham I | 85d5e70 | 2015-02-23 18:39:50 +0530 | [diff] [blame] | 656 | unsigned long flags; |
| 657 | int ret; |
| 658 | |
| 659 | if (!ep) { |
| 660 | pr_debug("dwc3: invalid parameters\n"); |
| 661 | return -EINVAL; |
| 662 | } |
| 663 | |
| 664 | dep = to_dwc3_ep(ep); |
Kishon Vijay Abraham I | 85d5e70 | 2015-02-23 18:39:50 +0530 | [diff] [blame] | 665 | |
| 666 | if (!(dep->flags & DWC3_EP_ENABLED)) { |
Kishon Vijay Abraham I | 747a0a5 | 2015-02-23 18:39:58 +0530 | [diff] [blame] | 667 | WARN(true, "%s is already disabled\n", |
Kishon Vijay Abraham I | 85d5e70 | 2015-02-23 18:39:50 +0530 | [diff] [blame] | 668 | dep->name); |
| 669 | return 0; |
| 670 | } |
| 671 | |
| 672 | snprintf(dep->name, sizeof(dep->name), "ep%d%s", |
| 673 | dep->number >> 1, |
| 674 | (dep->number & 1) ? "in" : "out"); |
| 675 | |
| 676 | spin_lock_irqsave(&dwc->lock, flags); |
| 677 | ret = __dwc3_gadget_ep_disable(dep); |
| 678 | spin_unlock_irqrestore(&dwc->lock, flags); |
| 679 | |
| 680 | return ret; |
| 681 | } |
| 682 | |
| 683 | static struct usb_request *dwc3_gadget_ep_alloc_request(struct usb_ep *ep, |
| 684 | gfp_t gfp_flags) |
| 685 | { |
| 686 | struct dwc3_request *req; |
| 687 | struct dwc3_ep *dep = to_dwc3_ep(ep); |
| 688 | |
| 689 | req = kzalloc(sizeof(*req), gfp_flags); |
| 690 | if (!req) |
| 691 | return NULL; |
| 692 | |
| 693 | req->epnum = dep->number; |
| 694 | req->dep = dep; |
| 695 | |
Kishon Vijay Abraham I | 85d5e70 | 2015-02-23 18:39:50 +0530 | [diff] [blame] | 696 | return &req->request; |
| 697 | } |
| 698 | |
| 699 | static void dwc3_gadget_ep_free_request(struct usb_ep *ep, |
| 700 | struct usb_request *request) |
| 701 | { |
| 702 | struct dwc3_request *req = to_dwc3_request(request); |
| 703 | |
Kishon Vijay Abraham I | 85d5e70 | 2015-02-23 18:39:50 +0530 | [diff] [blame] | 704 | kfree(req); |
| 705 | } |
| 706 | |
| 707 | /** |
| 708 | * dwc3_prepare_one_trb - setup one TRB from one request |
| 709 | * @dep: endpoint for which this request is prepared |
| 710 | * @req: dwc3_request pointer |
| 711 | */ |
| 712 | static void dwc3_prepare_one_trb(struct dwc3_ep *dep, |
| 713 | struct dwc3_request *req, dma_addr_t dma, |
| 714 | unsigned length, unsigned last, unsigned chain, unsigned node) |
| 715 | { |
Kishon Vijay Abraham I | 85d5e70 | 2015-02-23 18:39:50 +0530 | [diff] [blame] | 716 | struct dwc3_trb *trb; |
| 717 | |
Sean Anderson | df5eabc | 2020-09-15 10:45:16 -0400 | [diff] [blame] | 718 | dev_vdbg(dep->dwc->dev, "%s: req %p dma %08llx length %d%s%s\n", |
| 719 | dep->name, req, (unsigned long long)dma, |
| 720 | length, last ? " last" : "", chain ? " chain" : ""); |
Kishon Vijay Abraham I | 85d5e70 | 2015-02-23 18:39:50 +0530 | [diff] [blame] | 721 | |
| 722 | |
| 723 | trb = &dep->trb_pool[dep->free_slot & DWC3_TRB_MASK]; |
| 724 | |
| 725 | if (!req->trb) { |
| 726 | dwc3_gadget_move_request_queued(req); |
| 727 | req->trb = trb; |
| 728 | req->trb_dma = dwc3_trb_dma_offset(dep, trb); |
| 729 | req->start_slot = dep->free_slot & DWC3_TRB_MASK; |
| 730 | } |
| 731 | |
| 732 | dep->free_slot++; |
| 733 | /* Skip the LINK-TRB on ISOC */ |
| 734 | if (((dep->free_slot & DWC3_TRB_MASK) == DWC3_TRB_NUM - 1) && |
| 735 | usb_endpoint_xfer_isoc(dep->endpoint.desc)) |
| 736 | dep->free_slot++; |
| 737 | |
| 738 | trb->size = DWC3_TRB_SIZE_LENGTH(length); |
| 739 | trb->bpl = lower_32_bits(dma); |
| 740 | trb->bph = upper_32_bits(dma); |
| 741 | |
| 742 | switch (usb_endpoint_type(dep->endpoint.desc)) { |
| 743 | case USB_ENDPOINT_XFER_CONTROL: |
| 744 | trb->ctrl = DWC3_TRBCTL_CONTROL_SETUP; |
| 745 | break; |
| 746 | |
| 747 | case USB_ENDPOINT_XFER_ISOC: |
| 748 | if (!node) |
| 749 | trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS_FIRST; |
| 750 | else |
| 751 | trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS; |
| 752 | break; |
| 753 | |
| 754 | case USB_ENDPOINT_XFER_BULK: |
| 755 | case USB_ENDPOINT_XFER_INT: |
| 756 | trb->ctrl = DWC3_TRBCTL_NORMAL; |
| 757 | break; |
| 758 | default: |
| 759 | /* |
| 760 | * This is only possible with faulty memory because we |
| 761 | * checked it already :) |
| 762 | */ |
| 763 | BUG(); |
| 764 | } |
| 765 | |
| 766 | if (!req->request.no_interrupt && !chain) |
| 767 | trb->ctrl |= DWC3_TRB_CTRL_IOC; |
| 768 | |
| 769 | if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) { |
| 770 | trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI; |
| 771 | trb->ctrl |= DWC3_TRB_CTRL_CSP; |
| 772 | } else if (last) { |
| 773 | trb->ctrl |= DWC3_TRB_CTRL_LST; |
| 774 | } |
| 775 | |
| 776 | if (chain) |
| 777 | trb->ctrl |= DWC3_TRB_CTRL_CHN; |
| 778 | |
| 779 | if (usb_endpoint_xfer_bulk(dep->endpoint.desc) && dep->stream_capable) |
| 780 | trb->ctrl |= DWC3_TRB_CTRL_SID_SOFN(req->request.stream_id); |
| 781 | |
| 782 | trb->ctrl |= DWC3_TRB_CTRL_HWO; |
Kishon Vijay Abraham I | 526a50f | 2015-02-23 18:40:13 +0530 | [diff] [blame] | 783 | |
Philipp Tomsich | b7bf4a9 | 2017-04-06 16:58:52 +0200 | [diff] [blame] | 784 | dwc3_flush_cache((uintptr_t)dma, length); |
| 785 | dwc3_flush_cache((uintptr_t)trb, sizeof(*trb)); |
Kishon Vijay Abraham I | 85d5e70 | 2015-02-23 18:39:50 +0530 | [diff] [blame] | 786 | } |
| 787 | |
| 788 | /* |
| 789 | * dwc3_prepare_trbs - setup TRBs from requests |
| 790 | * @dep: endpoint for which requests are being prepared |
| 791 | * @starting: true if the endpoint is idle and no requests are queued. |
| 792 | * |
| 793 | * The function goes through the requests list and sets up TRBs for the |
| 794 | * transfers. The function returns once there are no more TRBs available or |
| 795 | * it runs out of requests. |
| 796 | */ |
| 797 | static void dwc3_prepare_trbs(struct dwc3_ep *dep, bool starting) |
| 798 | { |
| 799 | struct dwc3_request *req, *n; |
| 800 | u32 trbs_left; |
| 801 | u32 max; |
Kishon Vijay Abraham I | 85d5e70 | 2015-02-23 18:39:50 +0530 | [diff] [blame] | 802 | |
| 803 | BUILD_BUG_ON_NOT_POWER_OF_2(DWC3_TRB_NUM); |
| 804 | |
| 805 | /* the first request must not be queued */ |
| 806 | trbs_left = (dep->busy_slot - dep->free_slot) & DWC3_TRB_MASK; |
| 807 | |
| 808 | /* Can't wrap around on a non-isoc EP since there's no link TRB */ |
| 809 | if (!usb_endpoint_xfer_isoc(dep->endpoint.desc)) { |
| 810 | max = DWC3_TRB_NUM - (dep->free_slot & DWC3_TRB_MASK); |
| 811 | if (trbs_left > max) |
| 812 | trbs_left = max; |
| 813 | } |
| 814 | |
| 815 | /* |
| 816 | * If busy & slot are equal than it is either full or empty. If we are |
| 817 | * starting to process requests then we are empty. Otherwise we are |
| 818 | * full and don't do anything |
| 819 | */ |
| 820 | if (!trbs_left) { |
| 821 | if (!starting) |
| 822 | return; |
| 823 | trbs_left = DWC3_TRB_NUM; |
| 824 | /* |
| 825 | * In case we start from scratch, we queue the ISOC requests |
| 826 | * starting from slot 1. This is done because we use ring |
| 827 | * buffer and have no LST bit to stop us. Instead, we place |
| 828 | * IOC bit every TRB_NUM/4. We try to avoid having an interrupt |
| 829 | * after the first request so we start at slot 1 and have |
| 830 | * 7 requests proceed before we hit the first IOC. |
| 831 | * Other transfer types don't use the ring buffer and are |
| 832 | * processed from the first TRB until the last one. Since we |
| 833 | * don't wrap around we have to start at the beginning. |
| 834 | */ |
| 835 | if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) { |
| 836 | dep->busy_slot = 1; |
| 837 | dep->free_slot = 1; |
| 838 | } else { |
| 839 | dep->busy_slot = 0; |
| 840 | dep->free_slot = 0; |
| 841 | } |
| 842 | } |
| 843 | |
| 844 | /* The last TRB is a link TRB, not used for xfer */ |
| 845 | if ((trbs_left <= 1) && usb_endpoint_xfer_isoc(dep->endpoint.desc)) |
| 846 | return; |
| 847 | |
| 848 | list_for_each_entry_safe(req, n, &dep->request_list, list) { |
| 849 | unsigned length; |
| 850 | dma_addr_t dma; |
Kishon Vijay Abraham I | 85d5e70 | 2015-02-23 18:39:50 +0530 | [diff] [blame] | 851 | |
Kishon Vijay Abraham I | 747a0a5 | 2015-02-23 18:39:58 +0530 | [diff] [blame] | 852 | dma = req->request.dma; |
| 853 | length = req->request.length; |
Kishon Vijay Abraham I | 85d5e70 | 2015-02-23 18:39:50 +0530 | [diff] [blame] | 854 | |
Kishon Vijay Abraham I | 747a0a5 | 2015-02-23 18:39:58 +0530 | [diff] [blame] | 855 | dwc3_prepare_one_trb(dep, req, dma, length, |
Lukasz Majewski | 29e7fc1 | 2015-03-03 17:32:13 +0100 | [diff] [blame] | 856 | true, false, 0); |
Kishon Vijay Abraham I | 85d5e70 | 2015-02-23 18:39:50 +0530 | [diff] [blame] | 857 | |
Lukasz Majewski | 29e7fc1 | 2015-03-03 17:32:13 +0100 | [diff] [blame] | 858 | break; |
Kishon Vijay Abraham I | 85d5e70 | 2015-02-23 18:39:50 +0530 | [diff] [blame] | 859 | } |
| 860 | } |
| 861 | |
| 862 | static int __dwc3_gadget_kick_transfer(struct dwc3_ep *dep, u16 cmd_param, |
| 863 | int start_new) |
| 864 | { |
| 865 | struct dwc3_gadget_ep_cmd_params params; |
| 866 | struct dwc3_request *req; |
| 867 | struct dwc3 *dwc = dep->dwc; |
| 868 | int ret; |
| 869 | u32 cmd; |
| 870 | |
| 871 | if (start_new && (dep->flags & DWC3_EP_BUSY)) { |
| 872 | dev_vdbg(dwc->dev, "%s: endpoint busy\n", dep->name); |
| 873 | return -EBUSY; |
| 874 | } |
| 875 | dep->flags &= ~DWC3_EP_PENDING_REQUEST; |
| 876 | |
| 877 | /* |
| 878 | * If we are getting here after a short-out-packet we don't enqueue any |
| 879 | * new requests as we try to set the IOC bit only on the last request. |
| 880 | */ |
| 881 | if (start_new) { |
| 882 | if (list_empty(&dep->req_queued)) |
| 883 | dwc3_prepare_trbs(dep, start_new); |
| 884 | |
| 885 | /* req points to the first request which will be sent */ |
| 886 | req = next_request(&dep->req_queued); |
| 887 | } else { |
| 888 | dwc3_prepare_trbs(dep, start_new); |
| 889 | |
| 890 | /* |
| 891 | * req points to the first request where HWO changed from 0 to 1 |
| 892 | */ |
| 893 | req = next_request(&dep->req_queued); |
| 894 | } |
| 895 | if (!req) { |
| 896 | dep->flags |= DWC3_EP_PENDING_REQUEST; |
| 897 | return 0; |
| 898 | } |
| 899 | |
| 900 | memset(¶ms, 0, sizeof(params)); |
| 901 | |
| 902 | if (start_new) { |
| 903 | params.param0 = upper_32_bits(req->trb_dma); |
| 904 | params.param1 = lower_32_bits(req->trb_dma); |
| 905 | cmd = DWC3_DEPCMD_STARTTRANSFER; |
| 906 | } else { |
| 907 | cmd = DWC3_DEPCMD_UPDATETRANSFER; |
| 908 | } |
| 909 | |
| 910 | cmd |= DWC3_DEPCMD_PARAM(cmd_param); |
| 911 | ret = dwc3_send_gadget_ep_cmd(dwc, dep->number, cmd, ¶ms); |
| 912 | if (ret < 0) { |
| 913 | dev_dbg(dwc->dev, "failed to send STARTTRANSFER command\n"); |
| 914 | |
| 915 | /* |
| 916 | * FIXME we need to iterate over the list of requests |
| 917 | * here and stop, unmap, free and del each of the linked |
| 918 | * requests instead of what we do now. |
| 919 | */ |
| 920 | usb_gadget_unmap_request(&dwc->gadget, &req->request, |
| 921 | req->direction); |
| 922 | list_del(&req->list); |
| 923 | return ret; |
| 924 | } |
| 925 | |
| 926 | dep->flags |= DWC3_EP_BUSY; |
| 927 | |
| 928 | if (start_new) { |
| 929 | dep->resource_index = dwc3_gadget_ep_get_transfer_index(dwc, |
| 930 | dep->number); |
| 931 | WARN_ON_ONCE(!dep->resource_index); |
| 932 | } |
| 933 | |
| 934 | return 0; |
| 935 | } |
| 936 | |
| 937 | static void __dwc3_gadget_start_isoc(struct dwc3 *dwc, |
| 938 | struct dwc3_ep *dep, u32 cur_uf) |
| 939 | { |
| 940 | u32 uf; |
| 941 | |
| 942 | if (list_empty(&dep->request_list)) { |
| 943 | dev_vdbg(dwc->dev, "ISOC ep %s run out for requests.\n", |
| 944 | dep->name); |
| 945 | dep->flags |= DWC3_EP_PENDING_REQUEST; |
| 946 | return; |
| 947 | } |
| 948 | |
| 949 | /* 4 micro frames in the future */ |
| 950 | uf = cur_uf + dep->interval * 4; |
| 951 | |
| 952 | __dwc3_gadget_kick_transfer(dep, uf, 1); |
| 953 | } |
| 954 | |
| 955 | static void dwc3_gadget_start_isoc(struct dwc3 *dwc, |
| 956 | struct dwc3_ep *dep, const struct dwc3_event_depevt *event) |
| 957 | { |
| 958 | u32 cur_uf, mask; |
| 959 | |
| 960 | mask = ~(dep->interval - 1); |
| 961 | cur_uf = event->parameters & mask; |
| 962 | |
| 963 | __dwc3_gadget_start_isoc(dwc, dep, cur_uf); |
| 964 | } |
| 965 | |
| 966 | static int __dwc3_gadget_ep_queue(struct dwc3_ep *dep, struct dwc3_request *req) |
| 967 | { |
| 968 | struct dwc3 *dwc = dep->dwc; |
| 969 | int ret; |
| 970 | |
| 971 | req->request.actual = 0; |
| 972 | req->request.status = -EINPROGRESS; |
| 973 | req->direction = dep->direction; |
| 974 | req->epnum = dep->number; |
| 975 | |
| 976 | /* |
Marek Szyprowski | 5dc4538 | 2015-03-03 17:32:10 +0100 | [diff] [blame] | 977 | * DWC3 hangs on OUT requests smaller than maxpacket size, |
| 978 | * so HACK the request length |
| 979 | */ |
| 980 | if (dep->direction == 0 && |
| 981 | req->request.length < dep->endpoint.maxpacket) |
| 982 | req->request.length = dep->endpoint.maxpacket; |
| 983 | |
| 984 | /* |
Kishon Vijay Abraham I | 85d5e70 | 2015-02-23 18:39:50 +0530 | [diff] [blame] | 985 | * We only add to our list of requests now and |
| 986 | * start consuming the list once we get XferNotReady |
| 987 | * IRQ. |
| 988 | * |
| 989 | * That way, we avoid doing anything that we don't need |
| 990 | * to do now and defer it until the point we receive a |
| 991 | * particular token from the Host side. |
| 992 | * |
| 993 | * This will also avoid Host cancelling URBs due to too |
| 994 | * many NAKs. |
| 995 | */ |
| 996 | ret = usb_gadget_map_request(&dwc->gadget, &req->request, |
| 997 | dep->direction); |
| 998 | if (ret) |
| 999 | return ret; |
| 1000 | |
| 1001 | list_add_tail(&req->list, &dep->request_list); |
| 1002 | |
| 1003 | /* |
| 1004 | * There are a few special cases: |
| 1005 | * |
| 1006 | * 1. XferNotReady with empty list of requests. We need to kick the |
| 1007 | * transfer here in that situation, otherwise we will be NAKing |
| 1008 | * forever. If we get XferNotReady before gadget driver has a |
| 1009 | * chance to queue a request, we will ACK the IRQ but won't be |
| 1010 | * able to receive the data until the next request is queued. |
| 1011 | * The following code is handling exactly that. |
| 1012 | * |
| 1013 | */ |
| 1014 | if (dep->flags & DWC3_EP_PENDING_REQUEST) { |
| 1015 | /* |
| 1016 | * If xfernotready is already elapsed and it is a case |
| 1017 | * of isoc transfer, then issue END TRANSFER, so that |
| 1018 | * you can receive xfernotready again and can have |
| 1019 | * notion of current microframe. |
| 1020 | */ |
| 1021 | if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) { |
| 1022 | if (list_empty(&dep->req_queued)) { |
| 1023 | dwc3_stop_active_transfer(dwc, dep->number, true); |
| 1024 | dep->flags = DWC3_EP_ENABLED; |
| 1025 | } |
| 1026 | return 0; |
| 1027 | } |
| 1028 | |
| 1029 | ret = __dwc3_gadget_kick_transfer(dep, 0, true); |
| 1030 | if (ret && ret != -EBUSY) |
| 1031 | dev_dbg(dwc->dev, "%s: failed to kick transfers\n", |
| 1032 | dep->name); |
| 1033 | return ret; |
| 1034 | } |
| 1035 | |
| 1036 | /* |
| 1037 | * 2. XferInProgress on Isoc EP with an active transfer. We need to |
| 1038 | * kick the transfer here after queuing a request, otherwise the |
| 1039 | * core may not see the modified TRB(s). |
| 1040 | */ |
| 1041 | if (usb_endpoint_xfer_isoc(dep->endpoint.desc) && |
| 1042 | (dep->flags & DWC3_EP_BUSY) && |
| 1043 | !(dep->flags & DWC3_EP_MISSED_ISOC)) { |
| 1044 | WARN_ON_ONCE(!dep->resource_index); |
| 1045 | ret = __dwc3_gadget_kick_transfer(dep, dep->resource_index, |
| 1046 | false); |
| 1047 | if (ret && ret != -EBUSY) |
| 1048 | dev_dbg(dwc->dev, "%s: failed to kick transfers\n", |
| 1049 | dep->name); |
| 1050 | return ret; |
| 1051 | } |
| 1052 | |
| 1053 | /* |
| 1054 | * 4. Stream Capable Bulk Endpoints. We need to start the transfer |
| 1055 | * right away, otherwise host will not know we have streams to be |
| 1056 | * handled. |
| 1057 | */ |
| 1058 | if (dep->stream_capable) { |
| 1059 | int ret; |
| 1060 | |
| 1061 | ret = __dwc3_gadget_kick_transfer(dep, 0, true); |
| 1062 | if (ret && ret != -EBUSY) { |
Kishon Vijay Abraham I | 85d5e70 | 2015-02-23 18:39:50 +0530 | [diff] [blame] | 1063 | dev_dbg(dwc->dev, "%s: failed to kick transfers\n", |
| 1064 | dep->name); |
| 1065 | } |
| 1066 | } |
| 1067 | |
| 1068 | return 0; |
| 1069 | } |
| 1070 | |
| 1071 | static int dwc3_gadget_ep_queue(struct usb_ep *ep, struct usb_request *request, |
| 1072 | gfp_t gfp_flags) |
| 1073 | { |
| 1074 | struct dwc3_request *req = to_dwc3_request(request); |
| 1075 | struct dwc3_ep *dep = to_dwc3_ep(ep); |
Kishon Vijay Abraham I | 85d5e70 | 2015-02-23 18:39:50 +0530 | [diff] [blame] | 1076 | |
| 1077 | unsigned long flags; |
| 1078 | |
| 1079 | int ret; |
| 1080 | |
| 1081 | spin_lock_irqsave(&dwc->lock, flags); |
| 1082 | if (!dep->endpoint.desc) { |
Sean Anderson | df5eabc | 2020-09-15 10:45:16 -0400 | [diff] [blame] | 1083 | dev_dbg(dep->dwc->dev, |
| 1084 | "trying to queue request %p to disabled %s\n", request, |
| 1085 | ep->name); |
Kishon Vijay Abraham I | 85d5e70 | 2015-02-23 18:39:50 +0530 | [diff] [blame] | 1086 | ret = -ESHUTDOWN; |
| 1087 | goto out; |
| 1088 | } |
| 1089 | |
Kishon Vijay Abraham I | 747a0a5 | 2015-02-23 18:39:58 +0530 | [diff] [blame] | 1090 | if (req->dep != dep) { |
Sean Anderson | df5eabc | 2020-09-15 10:45:16 -0400 | [diff] [blame] | 1091 | WARN(true, "request %p belongs to '%s'\n", request, |
| 1092 | req->dep->name); |
Kishon Vijay Abraham I | 85d5e70 | 2015-02-23 18:39:50 +0530 | [diff] [blame] | 1093 | ret = -EINVAL; |
| 1094 | goto out; |
| 1095 | } |
| 1096 | |
Sean Anderson | df5eabc | 2020-09-15 10:45:16 -0400 | [diff] [blame] | 1097 | dev_vdbg(dep->dwc->dev, "queing request %p to %s length %d\n", |
| 1098 | request, ep->name, request->length); |
Kishon Vijay Abraham I | 85d5e70 | 2015-02-23 18:39:50 +0530 | [diff] [blame] | 1099 | |
| 1100 | ret = __dwc3_gadget_ep_queue(dep, req); |
| 1101 | |
| 1102 | out: |
| 1103 | spin_unlock_irqrestore(&dwc->lock, flags); |
| 1104 | |
| 1105 | return ret; |
| 1106 | } |
| 1107 | |
| 1108 | static int dwc3_gadget_ep_dequeue(struct usb_ep *ep, |
| 1109 | struct usb_request *request) |
| 1110 | { |
| 1111 | struct dwc3_request *req = to_dwc3_request(request); |
| 1112 | struct dwc3_request *r = NULL; |
| 1113 | |
| 1114 | struct dwc3_ep *dep = to_dwc3_ep(ep); |
| 1115 | struct dwc3 *dwc = dep->dwc; |
| 1116 | |
| 1117 | unsigned long flags; |
| 1118 | int ret = 0; |
| 1119 | |
Kishon Vijay Abraham I | 85d5e70 | 2015-02-23 18:39:50 +0530 | [diff] [blame] | 1120 | spin_lock_irqsave(&dwc->lock, flags); |
| 1121 | |
| 1122 | list_for_each_entry(r, &dep->request_list, list) { |
| 1123 | if (r == req) |
| 1124 | break; |
| 1125 | } |
| 1126 | |
| 1127 | if (r != req) { |
| 1128 | list_for_each_entry(r, &dep->req_queued, list) { |
| 1129 | if (r == req) |
| 1130 | break; |
| 1131 | } |
| 1132 | if (r == req) { |
| 1133 | /* wait until it is processed */ |
| 1134 | dwc3_stop_active_transfer(dwc, dep->number, true); |
| 1135 | goto out1; |
| 1136 | } |
| 1137 | dev_err(dwc->dev, "request %p was not queued to %s\n", |
| 1138 | request, ep->name); |
| 1139 | ret = -EINVAL; |
| 1140 | goto out0; |
| 1141 | } |
| 1142 | |
| 1143 | out1: |
| 1144 | /* giveback the request */ |
| 1145 | dwc3_gadget_giveback(dep, req, -ECONNRESET); |
| 1146 | |
| 1147 | out0: |
| 1148 | spin_unlock_irqrestore(&dwc->lock, flags); |
| 1149 | |
| 1150 | return ret; |
| 1151 | } |
| 1152 | |
| 1153 | int __dwc3_gadget_ep_set_halt(struct dwc3_ep *dep, int value, int protocol) |
| 1154 | { |
| 1155 | struct dwc3_gadget_ep_cmd_params params; |
| 1156 | struct dwc3 *dwc = dep->dwc; |
| 1157 | int ret; |
| 1158 | |
| 1159 | if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) { |
| 1160 | dev_err(dwc->dev, "%s is of Isochronous type\n", dep->name); |
| 1161 | return -EINVAL; |
| 1162 | } |
| 1163 | |
| 1164 | memset(¶ms, 0x00, sizeof(params)); |
| 1165 | |
| 1166 | if (value) { |
| 1167 | if (!protocol && ((dep->direction && dep->flags & DWC3_EP_BUSY) || |
| 1168 | (!list_empty(&dep->req_queued) || |
| 1169 | !list_empty(&dep->request_list)))) { |
| 1170 | dev_dbg(dwc->dev, "%s: pending request, cannot halt\n", |
| 1171 | dep->name); |
| 1172 | return -EAGAIN; |
| 1173 | } |
| 1174 | |
| 1175 | ret = dwc3_send_gadget_ep_cmd(dwc, dep->number, |
| 1176 | DWC3_DEPCMD_SETSTALL, ¶ms); |
| 1177 | if (ret) |
| 1178 | dev_err(dwc->dev, "failed to set STALL on %s\n", |
| 1179 | dep->name); |
| 1180 | else |
| 1181 | dep->flags |= DWC3_EP_STALL; |
| 1182 | } else { |
| 1183 | ret = dwc3_send_gadget_ep_cmd(dwc, dep->number, |
| 1184 | DWC3_DEPCMD_CLEARSTALL, ¶ms); |
| 1185 | if (ret) |
| 1186 | dev_err(dwc->dev, "failed to clear STALL on %s\n", |
| 1187 | dep->name); |
| 1188 | else |
| 1189 | dep->flags &= ~(DWC3_EP_STALL | DWC3_EP_WEDGE); |
| 1190 | } |
| 1191 | |
| 1192 | return ret; |
| 1193 | } |
| 1194 | |
| 1195 | static int dwc3_gadget_ep_set_halt(struct usb_ep *ep, int value) |
| 1196 | { |
| 1197 | struct dwc3_ep *dep = to_dwc3_ep(ep); |
Kishon Vijay Abraham I | 85d5e70 | 2015-02-23 18:39:50 +0530 | [diff] [blame] | 1198 | |
| 1199 | unsigned long flags; |
| 1200 | |
| 1201 | int ret; |
| 1202 | |
| 1203 | spin_lock_irqsave(&dwc->lock, flags); |
| 1204 | ret = __dwc3_gadget_ep_set_halt(dep, value, false); |
| 1205 | spin_unlock_irqrestore(&dwc->lock, flags); |
| 1206 | |
| 1207 | return ret; |
| 1208 | } |
| 1209 | |
| 1210 | static int dwc3_gadget_ep_set_wedge(struct usb_ep *ep) |
| 1211 | { |
| 1212 | struct dwc3_ep *dep = to_dwc3_ep(ep); |
Kishon Vijay Abraham I | 85d5e70 | 2015-02-23 18:39:50 +0530 | [diff] [blame] | 1213 | unsigned long flags; |
| 1214 | int ret; |
| 1215 | |
| 1216 | spin_lock_irqsave(&dwc->lock, flags); |
| 1217 | dep->flags |= DWC3_EP_WEDGE; |
| 1218 | |
| 1219 | if (dep->number == 0 || dep->number == 1) |
| 1220 | ret = __dwc3_gadget_ep0_set_halt(ep, 1); |
| 1221 | else |
| 1222 | ret = __dwc3_gadget_ep_set_halt(dep, 1, false); |
| 1223 | spin_unlock_irqrestore(&dwc->lock, flags); |
| 1224 | |
| 1225 | return ret; |
| 1226 | } |
| 1227 | |
| 1228 | /* -------------------------------------------------------------------------- */ |
| 1229 | |
| 1230 | static struct usb_endpoint_descriptor dwc3_gadget_ep0_desc = { |
| 1231 | .bLength = USB_DT_ENDPOINT_SIZE, |
| 1232 | .bDescriptorType = USB_DT_ENDPOINT, |
| 1233 | .bmAttributes = USB_ENDPOINT_XFER_CONTROL, |
| 1234 | }; |
| 1235 | |
| 1236 | static const struct usb_ep_ops dwc3_gadget_ep0_ops = { |
| 1237 | .enable = dwc3_gadget_ep0_enable, |
| 1238 | .disable = dwc3_gadget_ep0_disable, |
| 1239 | .alloc_request = dwc3_gadget_ep_alloc_request, |
| 1240 | .free_request = dwc3_gadget_ep_free_request, |
| 1241 | .queue = dwc3_gadget_ep0_queue, |
| 1242 | .dequeue = dwc3_gadget_ep_dequeue, |
| 1243 | .set_halt = dwc3_gadget_ep0_set_halt, |
| 1244 | .set_wedge = dwc3_gadget_ep_set_wedge, |
| 1245 | }; |
| 1246 | |
| 1247 | static const struct usb_ep_ops dwc3_gadget_ep_ops = { |
| 1248 | .enable = dwc3_gadget_ep_enable, |
| 1249 | .disable = dwc3_gadget_ep_disable, |
| 1250 | .alloc_request = dwc3_gadget_ep_alloc_request, |
| 1251 | .free_request = dwc3_gadget_ep_free_request, |
| 1252 | .queue = dwc3_gadget_ep_queue, |
| 1253 | .dequeue = dwc3_gadget_ep_dequeue, |
| 1254 | .set_halt = dwc3_gadget_ep_set_halt, |
| 1255 | .set_wedge = dwc3_gadget_ep_set_wedge, |
| 1256 | }; |
| 1257 | |
| 1258 | /* -------------------------------------------------------------------------- */ |
| 1259 | |
| 1260 | static int dwc3_gadget_get_frame(struct usb_gadget *g) |
| 1261 | { |
| 1262 | struct dwc3 *dwc = gadget_to_dwc(g); |
| 1263 | u32 reg; |
| 1264 | |
| 1265 | reg = dwc3_readl(dwc->regs, DWC3_DSTS); |
| 1266 | return DWC3_DSTS_SOFFN(reg); |
| 1267 | } |
| 1268 | |
| 1269 | static int dwc3_gadget_wakeup(struct usb_gadget *g) |
| 1270 | { |
| 1271 | struct dwc3 *dwc = gadget_to_dwc(g); |
| 1272 | |
| 1273 | unsigned long timeout; |
| 1274 | unsigned long flags; |
| 1275 | |
| 1276 | u32 reg; |
| 1277 | |
| 1278 | int ret = 0; |
| 1279 | |
| 1280 | u8 link_state; |
| 1281 | u8 speed; |
| 1282 | |
| 1283 | spin_lock_irqsave(&dwc->lock, flags); |
| 1284 | |
| 1285 | /* |
| 1286 | * According to the Databook Remote wakeup request should |
| 1287 | * be issued only when the device is in early suspend state. |
| 1288 | * |
| 1289 | * We can check that via USB Link State bits in DSTS register. |
| 1290 | */ |
| 1291 | reg = dwc3_readl(dwc->regs, DWC3_DSTS); |
| 1292 | |
| 1293 | speed = reg & DWC3_DSTS_CONNECTSPD; |
| 1294 | if (speed == DWC3_DSTS_SUPERSPEED) { |
| 1295 | dev_dbg(dwc->dev, "no wakeup on SuperSpeed\n"); |
| 1296 | ret = -EINVAL; |
| 1297 | goto out; |
| 1298 | } |
| 1299 | |
| 1300 | link_state = DWC3_DSTS_USBLNKST(reg); |
| 1301 | |
| 1302 | switch (link_state) { |
| 1303 | case DWC3_LINK_STATE_RX_DET: /* in HS, means Early Suspend */ |
| 1304 | case DWC3_LINK_STATE_U3: /* in HS, means SUSPEND */ |
| 1305 | break; |
| 1306 | default: |
| 1307 | dev_dbg(dwc->dev, "can't wakeup from link state %d\n", |
| 1308 | link_state); |
| 1309 | ret = -EINVAL; |
| 1310 | goto out; |
| 1311 | } |
| 1312 | |
| 1313 | ret = dwc3_gadget_set_link_state(dwc, DWC3_LINK_STATE_RECOV); |
| 1314 | if (ret < 0) { |
| 1315 | dev_err(dwc->dev, "failed to put link in Recovery\n"); |
| 1316 | goto out; |
| 1317 | } |
| 1318 | |
| 1319 | /* Recent versions do this automatically */ |
| 1320 | if (dwc->revision < DWC3_REVISION_194A) { |
| 1321 | /* write zeroes to Link Change Request */ |
| 1322 | reg = dwc3_readl(dwc->regs, DWC3_DCTL); |
| 1323 | reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK; |
| 1324 | dwc3_writel(dwc->regs, DWC3_DCTL, reg); |
| 1325 | } |
| 1326 | |
| 1327 | /* poll until Link State changes to ON */ |
Kishon Vijay Abraham I | 747a0a5 | 2015-02-23 18:39:58 +0530 | [diff] [blame] | 1328 | timeout = 1000; |
Kishon Vijay Abraham I | 85d5e70 | 2015-02-23 18:39:50 +0530 | [diff] [blame] | 1329 | |
Kishon Vijay Abraham I | 747a0a5 | 2015-02-23 18:39:58 +0530 | [diff] [blame] | 1330 | while (timeout--) { |
Kishon Vijay Abraham I | 85d5e70 | 2015-02-23 18:39:50 +0530 | [diff] [blame] | 1331 | reg = dwc3_readl(dwc->regs, DWC3_DSTS); |
| 1332 | |
| 1333 | /* in HS, means ON */ |
| 1334 | if (DWC3_DSTS_USBLNKST(reg) == DWC3_LINK_STATE_U0) |
| 1335 | break; |
| 1336 | } |
| 1337 | |
| 1338 | if (DWC3_DSTS_USBLNKST(reg) != DWC3_LINK_STATE_U0) { |
| 1339 | dev_err(dwc->dev, "failed to send remote wakeup\n"); |
| 1340 | ret = -EINVAL; |
| 1341 | } |
| 1342 | |
| 1343 | out: |
| 1344 | spin_unlock_irqrestore(&dwc->lock, flags); |
| 1345 | |
| 1346 | return ret; |
| 1347 | } |
| 1348 | |
| 1349 | static int dwc3_gadget_set_selfpowered(struct usb_gadget *g, |
| 1350 | int is_selfpowered) |
| 1351 | { |
| 1352 | struct dwc3 *dwc = gadget_to_dwc(g); |
| 1353 | unsigned long flags; |
| 1354 | |
| 1355 | spin_lock_irqsave(&dwc->lock, flags); |
| 1356 | dwc->is_selfpowered = !!is_selfpowered; |
| 1357 | spin_unlock_irqrestore(&dwc->lock, flags); |
| 1358 | |
| 1359 | return 0; |
| 1360 | } |
| 1361 | |
| 1362 | static int dwc3_gadget_run_stop(struct dwc3 *dwc, int is_on, int suspend) |
| 1363 | { |
| 1364 | u32 reg; |
| 1365 | u32 timeout = 500; |
| 1366 | |
| 1367 | reg = dwc3_readl(dwc->regs, DWC3_DCTL); |
| 1368 | if (is_on) { |
| 1369 | if (dwc->revision <= DWC3_REVISION_187A) { |
| 1370 | reg &= ~DWC3_DCTL_TRGTULST_MASK; |
| 1371 | reg |= DWC3_DCTL_TRGTULST_RX_DET; |
| 1372 | } |
| 1373 | |
| 1374 | if (dwc->revision >= DWC3_REVISION_194A) |
| 1375 | reg &= ~DWC3_DCTL_KEEP_CONNECT; |
| 1376 | reg |= DWC3_DCTL_RUN_STOP; |
| 1377 | |
| 1378 | if (dwc->has_hibernation) |
| 1379 | reg |= DWC3_DCTL_KEEP_CONNECT; |
| 1380 | |
| 1381 | dwc->pullups_connected = true; |
| 1382 | } else { |
| 1383 | reg &= ~DWC3_DCTL_RUN_STOP; |
| 1384 | |
| 1385 | if (dwc->has_hibernation && !suspend) |
| 1386 | reg &= ~DWC3_DCTL_KEEP_CONNECT; |
| 1387 | |
| 1388 | dwc->pullups_connected = false; |
| 1389 | } |
| 1390 | |
| 1391 | dwc3_writel(dwc->regs, DWC3_DCTL, reg); |
| 1392 | |
| 1393 | do { |
| 1394 | reg = dwc3_readl(dwc->regs, DWC3_DSTS); |
| 1395 | if (is_on) { |
| 1396 | if (!(reg & DWC3_DSTS_DEVCTRLHLT)) |
| 1397 | break; |
| 1398 | } else { |
| 1399 | if (reg & DWC3_DSTS_DEVCTRLHLT) |
| 1400 | break; |
| 1401 | } |
| 1402 | timeout--; |
| 1403 | if (!timeout) |
| 1404 | return -ETIMEDOUT; |
| 1405 | udelay(1); |
| 1406 | } while (1); |
| 1407 | |
| 1408 | dev_vdbg(dwc->dev, "gadget %s data soft-%s\n", |
| 1409 | dwc->gadget_driver |
| 1410 | ? dwc->gadget_driver->function : "no-function", |
| 1411 | is_on ? "connect" : "disconnect"); |
| 1412 | |
| 1413 | return 0; |
| 1414 | } |
| 1415 | |
| 1416 | static int dwc3_gadget_pullup(struct usb_gadget *g, int is_on) |
| 1417 | { |
| 1418 | struct dwc3 *dwc = gadget_to_dwc(g); |
| 1419 | unsigned long flags; |
| 1420 | int ret; |
| 1421 | |
| 1422 | is_on = !!is_on; |
| 1423 | |
| 1424 | spin_lock_irqsave(&dwc->lock, flags); |
| 1425 | ret = dwc3_gadget_run_stop(dwc, is_on, false); |
| 1426 | spin_unlock_irqrestore(&dwc->lock, flags); |
| 1427 | |
| 1428 | return ret; |
| 1429 | } |
| 1430 | |
| 1431 | static void dwc3_gadget_enable_irq(struct dwc3 *dwc) |
| 1432 | { |
| 1433 | u32 reg; |
| 1434 | |
| 1435 | /* Enable all but Start and End of Frame IRQs */ |
| 1436 | reg = (DWC3_DEVTEN_VNDRDEVTSTRCVEDEN | |
| 1437 | DWC3_DEVTEN_EVNTOVERFLOWEN | |
| 1438 | DWC3_DEVTEN_CMDCMPLTEN | |
| 1439 | DWC3_DEVTEN_ERRTICERREN | |
| 1440 | DWC3_DEVTEN_WKUPEVTEN | |
| 1441 | DWC3_DEVTEN_ULSTCNGEN | |
| 1442 | DWC3_DEVTEN_CONNECTDONEEN | |
| 1443 | DWC3_DEVTEN_USBRSTEN | |
| 1444 | DWC3_DEVTEN_DISCONNEVTEN); |
| 1445 | |
| 1446 | dwc3_writel(dwc->regs, DWC3_DEVTEN, reg); |
| 1447 | } |
| 1448 | |
| 1449 | static void dwc3_gadget_disable_irq(struct dwc3 *dwc) |
| 1450 | { |
| 1451 | /* mask all interrupts */ |
| 1452 | dwc3_writel(dwc->regs, DWC3_DEVTEN, 0x00); |
| 1453 | } |
| 1454 | |
Kishon Vijay Abraham I | 85d5e70 | 2015-02-23 18:39:50 +0530 | [diff] [blame] | 1455 | static int dwc3_gadget_start(struct usb_gadget *g, |
| 1456 | struct usb_gadget_driver *driver) |
| 1457 | { |
| 1458 | struct dwc3 *dwc = gadget_to_dwc(g); |
| 1459 | struct dwc3_ep *dep; |
| 1460 | unsigned long flags; |
| 1461 | int ret = 0; |
Kishon Vijay Abraham I | 85d5e70 | 2015-02-23 18:39:50 +0530 | [diff] [blame] | 1462 | u32 reg; |
| 1463 | |
Kishon Vijay Abraham I | 85d5e70 | 2015-02-23 18:39:50 +0530 | [diff] [blame] | 1464 | spin_lock_irqsave(&dwc->lock, flags); |
| 1465 | |
| 1466 | if (dwc->gadget_driver) { |
| 1467 | dev_err(dwc->dev, "%s is already bound to %s\n", |
| 1468 | dwc->gadget.name, |
Kishon Vijay Abraham I | 747a0a5 | 2015-02-23 18:39:58 +0530 | [diff] [blame] | 1469 | dwc->gadget_driver->function); |
Kishon Vijay Abraham I | 85d5e70 | 2015-02-23 18:39:50 +0530 | [diff] [blame] | 1470 | ret = -EBUSY; |
| 1471 | goto err1; |
| 1472 | } |
| 1473 | |
| 1474 | dwc->gadget_driver = driver; |
| 1475 | |
| 1476 | reg = dwc3_readl(dwc->regs, DWC3_DCFG); |
| 1477 | reg &= ~(DWC3_DCFG_SPEED_MASK); |
| 1478 | |
| 1479 | /** |
| 1480 | * WORKAROUND: DWC3 revision < 2.20a have an issue |
| 1481 | * which would cause metastability state on Run/Stop |
| 1482 | * bit if we try to force the IP to USB2-only mode. |
| 1483 | * |
| 1484 | * Because of that, we cannot configure the IP to any |
| 1485 | * speed other than the SuperSpeed |
| 1486 | * |
| 1487 | * Refers to: |
| 1488 | * |
| 1489 | * STAR#9000525659: Clock Domain Crossing on DCTL in |
| 1490 | * USB 2.0 Mode |
| 1491 | */ |
| 1492 | if (dwc->revision < DWC3_REVISION_220A) { |
| 1493 | reg |= DWC3_DCFG_SUPERSPEED; |
| 1494 | } else { |
| 1495 | switch (dwc->maximum_speed) { |
| 1496 | case USB_SPEED_LOW: |
| 1497 | reg |= DWC3_DSTS_LOWSPEED; |
| 1498 | break; |
| 1499 | case USB_SPEED_FULL: |
| 1500 | reg |= DWC3_DSTS_FULLSPEED1; |
| 1501 | break; |
| 1502 | case USB_SPEED_HIGH: |
| 1503 | reg |= DWC3_DSTS_HIGHSPEED; |
| 1504 | break; |
| 1505 | case USB_SPEED_SUPER: /* FALLTHROUGH */ |
| 1506 | case USB_SPEED_UNKNOWN: /* FALTHROUGH */ |
| 1507 | default: |
| 1508 | reg |= DWC3_DSTS_SUPERSPEED; |
| 1509 | } |
| 1510 | } |
| 1511 | dwc3_writel(dwc->regs, DWC3_DCFG, reg); |
| 1512 | |
| 1513 | dwc->start_config_issued = false; |
| 1514 | |
| 1515 | /* Start with SuperSpeed Default */ |
| 1516 | dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512); |
| 1517 | |
| 1518 | dep = dwc->eps[0]; |
| 1519 | ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, false, |
| 1520 | false); |
| 1521 | if (ret) { |
| 1522 | dev_err(dwc->dev, "failed to enable %s\n", dep->name); |
| 1523 | goto err2; |
| 1524 | } |
| 1525 | |
| 1526 | dep = dwc->eps[1]; |
| 1527 | ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, false, |
| 1528 | false); |
| 1529 | if (ret) { |
| 1530 | dev_err(dwc->dev, "failed to enable %s\n", dep->name); |
| 1531 | goto err3; |
| 1532 | } |
| 1533 | |
| 1534 | /* begin to receive SETUP packets */ |
| 1535 | dwc->ep0state = EP0_SETUP_PHASE; |
| 1536 | dwc3_ep0_out_start(dwc); |
| 1537 | |
| 1538 | dwc3_gadget_enable_irq(dwc); |
| 1539 | |
| 1540 | spin_unlock_irqrestore(&dwc->lock, flags); |
| 1541 | |
| 1542 | return 0; |
| 1543 | |
| 1544 | err3: |
| 1545 | __dwc3_gadget_ep_disable(dwc->eps[0]); |
| 1546 | |
| 1547 | err2: |
| 1548 | dwc->gadget_driver = NULL; |
| 1549 | |
| 1550 | err1: |
| 1551 | spin_unlock_irqrestore(&dwc->lock, flags); |
| 1552 | |
Kishon Vijay Abraham I | 85d5e70 | 2015-02-23 18:39:50 +0530 | [diff] [blame] | 1553 | return ret; |
| 1554 | } |
| 1555 | |
| 1556 | static int dwc3_gadget_stop(struct usb_gadget *g) |
| 1557 | { |
| 1558 | struct dwc3 *dwc = gadget_to_dwc(g); |
| 1559 | unsigned long flags; |
Kishon Vijay Abraham I | 85d5e70 | 2015-02-23 18:39:50 +0530 | [diff] [blame] | 1560 | |
| 1561 | spin_lock_irqsave(&dwc->lock, flags); |
| 1562 | |
| 1563 | dwc3_gadget_disable_irq(dwc); |
| 1564 | __dwc3_gadget_ep_disable(dwc->eps[0]); |
| 1565 | __dwc3_gadget_ep_disable(dwc->eps[1]); |
| 1566 | |
| 1567 | dwc->gadget_driver = NULL; |
| 1568 | |
| 1569 | spin_unlock_irqrestore(&dwc->lock, flags); |
| 1570 | |
Kishon Vijay Abraham I | 85d5e70 | 2015-02-23 18:39:50 +0530 | [diff] [blame] | 1571 | return 0; |
| 1572 | } |
| 1573 | |
| 1574 | static const struct usb_gadget_ops dwc3_gadget_ops = { |
| 1575 | .get_frame = dwc3_gadget_get_frame, |
| 1576 | .wakeup = dwc3_gadget_wakeup, |
| 1577 | .set_selfpowered = dwc3_gadget_set_selfpowered, |
| 1578 | .pullup = dwc3_gadget_pullup, |
| 1579 | .udc_start = dwc3_gadget_start, |
| 1580 | .udc_stop = dwc3_gadget_stop, |
| 1581 | }; |
| 1582 | |
| 1583 | /* -------------------------------------------------------------------------- */ |
| 1584 | |
| 1585 | static int dwc3_gadget_init_hw_endpoints(struct dwc3 *dwc, |
| 1586 | u8 num, u32 direction) |
| 1587 | { |
| 1588 | struct dwc3_ep *dep; |
| 1589 | u8 i; |
| 1590 | |
| 1591 | for (i = 0; i < num; i++) { |
| 1592 | u8 epnum = (i << 1) | (!!direction); |
| 1593 | |
| 1594 | dep = kzalloc(sizeof(*dep), GFP_KERNEL); |
| 1595 | if (!dep) |
| 1596 | return -ENOMEM; |
| 1597 | |
| 1598 | dep->dwc = dwc; |
| 1599 | dep->number = epnum; |
| 1600 | dep->direction = !!direction; |
| 1601 | dwc->eps[epnum] = dep; |
| 1602 | |
| 1603 | snprintf(dep->name, sizeof(dep->name), "ep%d%s", epnum >> 1, |
| 1604 | (epnum & 1) ? "in" : "out"); |
| 1605 | |
| 1606 | dep->endpoint.name = dep->name; |
| 1607 | |
| 1608 | dev_vdbg(dwc->dev, "initializing %s\n", dep->name); |
| 1609 | |
| 1610 | if (epnum == 0 || epnum == 1) { |
| 1611 | usb_ep_set_maxpacket_limit(&dep->endpoint, 512); |
| 1612 | dep->endpoint.maxburst = 1; |
| 1613 | dep->endpoint.ops = &dwc3_gadget_ep0_ops; |
| 1614 | if (!epnum) |
| 1615 | dwc->gadget.ep0 = &dep->endpoint; |
| 1616 | } else { |
| 1617 | int ret; |
| 1618 | |
Lukasz Majewski | afa093b | 2015-03-03 17:32:14 +0100 | [diff] [blame] | 1619 | usb_ep_set_maxpacket_limit(&dep->endpoint, 512); |
Kishon Vijay Abraham I | 85d5e70 | 2015-02-23 18:39:50 +0530 | [diff] [blame] | 1620 | dep->endpoint.max_streams = 15; |
| 1621 | dep->endpoint.ops = &dwc3_gadget_ep_ops; |
| 1622 | list_add_tail(&dep->endpoint.ep_list, |
| 1623 | &dwc->gadget.ep_list); |
| 1624 | |
| 1625 | ret = dwc3_alloc_trb_pool(dep); |
| 1626 | if (ret) |
| 1627 | return ret; |
| 1628 | } |
| 1629 | |
| 1630 | INIT_LIST_HEAD(&dep->request_list); |
| 1631 | INIT_LIST_HEAD(&dep->req_queued); |
| 1632 | } |
| 1633 | |
| 1634 | return 0; |
| 1635 | } |
| 1636 | |
| 1637 | static int dwc3_gadget_init_endpoints(struct dwc3 *dwc) |
| 1638 | { |
| 1639 | int ret; |
| 1640 | |
| 1641 | INIT_LIST_HEAD(&dwc->gadget.ep_list); |
| 1642 | |
| 1643 | ret = dwc3_gadget_init_hw_endpoints(dwc, dwc->num_out_eps, 0); |
| 1644 | if (ret < 0) { |
| 1645 | dev_vdbg(dwc->dev, "failed to allocate OUT endpoints\n"); |
| 1646 | return ret; |
| 1647 | } |
| 1648 | |
| 1649 | ret = dwc3_gadget_init_hw_endpoints(dwc, dwc->num_in_eps, 1); |
| 1650 | if (ret < 0) { |
| 1651 | dev_vdbg(dwc->dev, "failed to allocate IN endpoints\n"); |
| 1652 | return ret; |
| 1653 | } |
| 1654 | |
| 1655 | return 0; |
| 1656 | } |
| 1657 | |
| 1658 | static void dwc3_gadget_free_endpoints(struct dwc3 *dwc) |
| 1659 | { |
| 1660 | struct dwc3_ep *dep; |
| 1661 | u8 epnum; |
| 1662 | |
| 1663 | for (epnum = 0; epnum < DWC3_ENDPOINTS_NUM; epnum++) { |
| 1664 | dep = dwc->eps[epnum]; |
| 1665 | if (!dep) |
| 1666 | continue; |
| 1667 | /* |
| 1668 | * Physical endpoints 0 and 1 are special; they form the |
| 1669 | * bi-directional USB endpoint 0. |
| 1670 | * |
| 1671 | * For those two physical endpoints, we don't allocate a TRB |
| 1672 | * pool nor do we add them the endpoints list. Due to that, we |
| 1673 | * shouldn't do these two operations otherwise we would end up |
| 1674 | * with all sorts of bugs when removing dwc3.ko. |
| 1675 | */ |
| 1676 | if (epnum != 0 && epnum != 1) { |
| 1677 | dwc3_free_trb_pool(dep); |
| 1678 | list_del(&dep->endpoint.ep_list); |
| 1679 | } |
| 1680 | |
| 1681 | kfree(dep); |
| 1682 | } |
| 1683 | } |
| 1684 | |
| 1685 | /* -------------------------------------------------------------------------- */ |
| 1686 | |
| 1687 | static int __dwc3_cleanup_done_trbs(struct dwc3 *dwc, struct dwc3_ep *dep, |
| 1688 | struct dwc3_request *req, struct dwc3_trb *trb, |
| 1689 | const struct dwc3_event_depevt *event, int status) |
| 1690 | { |
| 1691 | unsigned int count; |
| 1692 | unsigned int s_pkt = 0; |
| 1693 | unsigned int trb_status; |
| 1694 | |
Kishon Vijay Abraham I | 85d5e70 | 2015-02-23 18:39:50 +0530 | [diff] [blame] | 1695 | if ((trb->ctrl & DWC3_TRB_CTRL_HWO) && status != -ESHUTDOWN) |
| 1696 | /* |
| 1697 | * We continue despite the error. There is not much we |
| 1698 | * can do. If we don't clean it up we loop forever. If |
| 1699 | * we skip the TRB then it gets overwritten after a |
| 1700 | * while since we use them in a ring buffer. A BUG() |
| 1701 | * would help. Lets hope that if this occurs, someone |
| 1702 | * fixes the root cause instead of looking away :) |
| 1703 | */ |
| 1704 | dev_err(dwc->dev, "%s's TRB (%p) still owned by HW\n", |
| 1705 | dep->name, trb); |
| 1706 | count = trb->size & DWC3_TRB_SIZE_MASK; |
| 1707 | |
| 1708 | if (dep->direction) { |
| 1709 | if (count) { |
| 1710 | trb_status = DWC3_TRB_SIZE_TRBSTS(trb->size); |
| 1711 | if (trb_status == DWC3_TRBSTS_MISSED_ISOC) { |
| 1712 | dev_dbg(dwc->dev, "incomplete IN transfer %s\n", |
| 1713 | dep->name); |
| 1714 | /* |
| 1715 | * If missed isoc occurred and there is |
| 1716 | * no request queued then issue END |
| 1717 | * TRANSFER, so that core generates |
| 1718 | * next xfernotready and we will issue |
| 1719 | * a fresh START TRANSFER. |
| 1720 | * If there are still queued request |
| 1721 | * then wait, do not issue either END |
| 1722 | * or UPDATE TRANSFER, just attach next |
| 1723 | * request in request_list during |
| 1724 | * giveback.If any future queued request |
| 1725 | * is successfully transferred then we |
| 1726 | * will issue UPDATE TRANSFER for all |
| 1727 | * request in the request_list. |
| 1728 | */ |
| 1729 | dep->flags |= DWC3_EP_MISSED_ISOC; |
| 1730 | } else { |
| 1731 | dev_err(dwc->dev, "incomplete IN transfer %s\n", |
| 1732 | dep->name); |
| 1733 | status = -ECONNRESET; |
| 1734 | } |
| 1735 | } else { |
| 1736 | dep->flags &= ~DWC3_EP_MISSED_ISOC; |
| 1737 | } |
| 1738 | } else { |
| 1739 | if (count && (event->status & DEPEVT_STATUS_SHORT)) |
| 1740 | s_pkt = 1; |
| 1741 | } |
| 1742 | |
| 1743 | /* |
| 1744 | * We assume here we will always receive the entire data block |
| 1745 | * which we should receive. Meaning, if we program RX to |
| 1746 | * receive 4K but we receive only 2K, we assume that's all we |
| 1747 | * should receive and we simply bounce the request back to the |
| 1748 | * gadget driver for further processing. |
| 1749 | */ |
| 1750 | req->request.actual += req->request.length - count; |
| 1751 | if (s_pkt) |
| 1752 | return 1; |
| 1753 | if ((event->status & DEPEVT_STATUS_LST) && |
| 1754 | (trb->ctrl & (DWC3_TRB_CTRL_LST | |
| 1755 | DWC3_TRB_CTRL_HWO))) |
| 1756 | return 1; |
| 1757 | if ((event->status & DEPEVT_STATUS_IOC) && |
| 1758 | (trb->ctrl & DWC3_TRB_CTRL_IOC)) |
| 1759 | return 1; |
| 1760 | return 0; |
| 1761 | } |
| 1762 | |
| 1763 | static int dwc3_cleanup_done_reqs(struct dwc3 *dwc, struct dwc3_ep *dep, |
| 1764 | const struct dwc3_event_depevt *event, int status) |
| 1765 | { |
| 1766 | struct dwc3_request *req; |
| 1767 | struct dwc3_trb *trb; |
| 1768 | unsigned int slot; |
Kishon Vijay Abraham I | 85d5e70 | 2015-02-23 18:39:50 +0530 | [diff] [blame] | 1769 | |
Lukasz Majewski | 3621b3b | 2015-03-03 17:32:15 +0100 | [diff] [blame] | 1770 | req = next_request(&dep->req_queued); |
| 1771 | if (!req) { |
| 1772 | WARN_ON_ONCE(1); |
| 1773 | return 1; |
| 1774 | } |
Kishon Vijay Abraham I | 85d5e70 | 2015-02-23 18:39:50 +0530 | [diff] [blame] | 1775 | |
Lukasz Majewski | 3621b3b | 2015-03-03 17:32:15 +0100 | [diff] [blame] | 1776 | slot = req->start_slot; |
| 1777 | if ((slot == DWC3_TRB_NUM - 1) && |
| 1778 | usb_endpoint_xfer_isoc(dep->endpoint.desc)) |
| 1779 | slot++; |
| 1780 | slot %= DWC3_TRB_NUM; |
| 1781 | trb = &dep->trb_pool[slot]; |
Kishon Vijay Abraham I | 747a0a5 | 2015-02-23 18:39:58 +0530 | [diff] [blame] | 1782 | |
Philipp Tomsich | b7bf4a9 | 2017-04-06 16:58:52 +0200 | [diff] [blame] | 1783 | dwc3_flush_cache((uintptr_t)trb, sizeof(*trb)); |
Lukasz Majewski | 3621b3b | 2015-03-03 17:32:15 +0100 | [diff] [blame] | 1784 | __dwc3_cleanup_done_trbs(dwc, dep, req, trb, event, status); |
| 1785 | dwc3_gadget_giveback(dep, req, status); |
Kishon Vijay Abraham I | 85d5e70 | 2015-02-23 18:39:50 +0530 | [diff] [blame] | 1786 | |
| 1787 | if (usb_endpoint_xfer_isoc(dep->endpoint.desc) && |
| 1788 | list_empty(&dep->req_queued)) { |
| 1789 | if (list_empty(&dep->request_list)) { |
| 1790 | /* |
| 1791 | * If there is no entry in request list then do |
| 1792 | * not issue END TRANSFER now. Just set PENDING |
| 1793 | * flag, so that END TRANSFER is issued when an |
| 1794 | * entry is added into request list. |
| 1795 | */ |
| 1796 | dep->flags = DWC3_EP_PENDING_REQUEST; |
| 1797 | } else { |
| 1798 | dwc3_stop_active_transfer(dwc, dep->number, true); |
| 1799 | dep->flags = DWC3_EP_ENABLED; |
| 1800 | } |
| 1801 | return 1; |
| 1802 | } |
| 1803 | |
| 1804 | return 1; |
| 1805 | } |
| 1806 | |
| 1807 | static void dwc3_endpoint_transfer_complete(struct dwc3 *dwc, |
| 1808 | struct dwc3_ep *dep, const struct dwc3_event_depevt *event) |
| 1809 | { |
| 1810 | unsigned status = 0; |
| 1811 | int clean_busy; |
| 1812 | |
| 1813 | if (event->status & DEPEVT_STATUS_BUSERR) |
| 1814 | status = -ECONNRESET; |
| 1815 | |
| 1816 | clean_busy = dwc3_cleanup_done_reqs(dwc, dep, event, status); |
| 1817 | if (clean_busy) |
| 1818 | dep->flags &= ~DWC3_EP_BUSY; |
| 1819 | |
| 1820 | /* |
| 1821 | * WORKAROUND: This is the 2nd half of U1/U2 -> U0 workaround. |
| 1822 | * See dwc3_gadget_linksts_change_interrupt() for 1st half. |
| 1823 | */ |
| 1824 | if (dwc->revision < DWC3_REVISION_183A) { |
| 1825 | u32 reg; |
| 1826 | int i; |
| 1827 | |
| 1828 | for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) { |
| 1829 | dep = dwc->eps[i]; |
| 1830 | |
| 1831 | if (!(dep->flags & DWC3_EP_ENABLED)) |
| 1832 | continue; |
| 1833 | |
| 1834 | if (!list_empty(&dep->req_queued)) |
| 1835 | return; |
| 1836 | } |
| 1837 | |
| 1838 | reg = dwc3_readl(dwc->regs, DWC3_DCTL); |
| 1839 | reg |= dwc->u1u2; |
| 1840 | dwc3_writel(dwc->regs, DWC3_DCTL, reg); |
| 1841 | |
| 1842 | dwc->u1u2 = 0; |
| 1843 | } |
| 1844 | } |
| 1845 | |
| 1846 | static void dwc3_endpoint_interrupt(struct dwc3 *dwc, |
| 1847 | const struct dwc3_event_depevt *event) |
| 1848 | { |
| 1849 | struct dwc3_ep *dep; |
| 1850 | u8 epnum = event->endpoint_number; |
| 1851 | |
| 1852 | dep = dwc->eps[epnum]; |
| 1853 | |
| 1854 | if (!(dep->flags & DWC3_EP_ENABLED)) |
| 1855 | return; |
| 1856 | |
| 1857 | if (epnum == 0 || epnum == 1) { |
| 1858 | dwc3_ep0_interrupt(dwc, event); |
| 1859 | return; |
| 1860 | } |
| 1861 | |
| 1862 | switch (event->endpoint_event) { |
| 1863 | case DWC3_DEPEVT_XFERCOMPLETE: |
| 1864 | dep->resource_index = 0; |
| 1865 | |
| 1866 | if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) { |
| 1867 | dev_dbg(dwc->dev, "%s is an Isochronous endpoint\n", |
| 1868 | dep->name); |
| 1869 | return; |
| 1870 | } |
| 1871 | |
| 1872 | dwc3_endpoint_transfer_complete(dwc, dep, event); |
| 1873 | break; |
| 1874 | case DWC3_DEPEVT_XFERINPROGRESS: |
| 1875 | dwc3_endpoint_transfer_complete(dwc, dep, event); |
| 1876 | break; |
| 1877 | case DWC3_DEPEVT_XFERNOTREADY: |
| 1878 | if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) { |
| 1879 | dwc3_gadget_start_isoc(dwc, dep, event); |
| 1880 | } else { |
| 1881 | int ret; |
| 1882 | |
| 1883 | dev_vdbg(dwc->dev, "%s: reason %s\n", |
| 1884 | dep->name, event->status & |
| 1885 | DEPEVT_STATUS_TRANSFER_ACTIVE |
| 1886 | ? "Transfer Active" |
| 1887 | : "Transfer Not Active"); |
| 1888 | |
| 1889 | ret = __dwc3_gadget_kick_transfer(dep, 0, 1); |
| 1890 | if (!ret || ret == -EBUSY) |
| 1891 | return; |
| 1892 | |
| 1893 | dev_dbg(dwc->dev, "%s: failed to kick transfers\n", |
| 1894 | dep->name); |
| 1895 | } |
| 1896 | |
| 1897 | break; |
| 1898 | case DWC3_DEPEVT_STREAMEVT: |
| 1899 | if (!usb_endpoint_xfer_bulk(dep->endpoint.desc)) { |
| 1900 | dev_err(dwc->dev, "Stream event for non-Bulk %s\n", |
| 1901 | dep->name); |
| 1902 | return; |
| 1903 | } |
| 1904 | |
| 1905 | switch (event->status) { |
| 1906 | case DEPEVT_STREAMEVT_FOUND: |
| 1907 | dev_vdbg(dwc->dev, "Stream %d found and started\n", |
| 1908 | event->parameters); |
| 1909 | |
| 1910 | break; |
| 1911 | case DEPEVT_STREAMEVT_NOTFOUND: |
| 1912 | /* FALLTHROUGH */ |
| 1913 | default: |
| 1914 | dev_dbg(dwc->dev, "Couldn't find suitable stream\n"); |
| 1915 | } |
| 1916 | break; |
| 1917 | case DWC3_DEPEVT_RXTXFIFOEVT: |
| 1918 | dev_dbg(dwc->dev, "%s FIFO Overrun\n", dep->name); |
| 1919 | break; |
| 1920 | case DWC3_DEPEVT_EPCMDCMPLT: |
| 1921 | dev_vdbg(dwc->dev, "Endpoint Command Complete\n"); |
| 1922 | break; |
| 1923 | } |
| 1924 | } |
| 1925 | |
| 1926 | static void dwc3_disconnect_gadget(struct dwc3 *dwc) |
| 1927 | { |
| 1928 | if (dwc->gadget_driver && dwc->gadget_driver->disconnect) { |
| 1929 | spin_unlock(&dwc->lock); |
| 1930 | dwc->gadget_driver->disconnect(&dwc->gadget); |
| 1931 | spin_lock(&dwc->lock); |
| 1932 | } |
| 1933 | } |
| 1934 | |
| 1935 | static void dwc3_suspend_gadget(struct dwc3 *dwc) |
| 1936 | { |
| 1937 | if (dwc->gadget_driver && dwc->gadget_driver->suspend) { |
| 1938 | spin_unlock(&dwc->lock); |
| 1939 | dwc->gadget_driver->suspend(&dwc->gadget); |
| 1940 | spin_lock(&dwc->lock); |
| 1941 | } |
| 1942 | } |
| 1943 | |
| 1944 | static void dwc3_resume_gadget(struct dwc3 *dwc) |
| 1945 | { |
| 1946 | if (dwc->gadget_driver && dwc->gadget_driver->resume) { |
| 1947 | spin_unlock(&dwc->lock); |
| 1948 | dwc->gadget_driver->resume(&dwc->gadget); |
| 1949 | } |
| 1950 | } |
| 1951 | |
| 1952 | static void dwc3_reset_gadget(struct dwc3 *dwc) |
| 1953 | { |
| 1954 | if (!dwc->gadget_driver) |
| 1955 | return; |
| 1956 | |
| 1957 | if (dwc->gadget.speed != USB_SPEED_UNKNOWN) { |
| 1958 | spin_unlock(&dwc->lock); |
| 1959 | usb_gadget_udc_reset(&dwc->gadget, dwc->gadget_driver); |
| 1960 | spin_lock(&dwc->lock); |
| 1961 | } |
| 1962 | } |
| 1963 | |
| 1964 | static void dwc3_stop_active_transfer(struct dwc3 *dwc, u32 epnum, bool force) |
| 1965 | { |
| 1966 | struct dwc3_ep *dep; |
| 1967 | struct dwc3_gadget_ep_cmd_params params; |
| 1968 | u32 cmd; |
| 1969 | int ret; |
| 1970 | |
| 1971 | dep = dwc->eps[epnum]; |
| 1972 | |
| 1973 | if (!dep->resource_index) |
| 1974 | return; |
| 1975 | |
| 1976 | /* |
| 1977 | * NOTICE: We are violating what the Databook says about the |
| 1978 | * EndTransfer command. Ideally we would _always_ wait for the |
| 1979 | * EndTransfer Command Completion IRQ, but that's causing too |
| 1980 | * much trouble synchronizing between us and gadget driver. |
| 1981 | * |
| 1982 | * We have discussed this with the IP Provider and it was |
| 1983 | * suggested to giveback all requests here, but give HW some |
| 1984 | * extra time to synchronize with the interconnect. We're using |
| 1985 | * an arbitraty 100us delay for that. |
| 1986 | * |
| 1987 | * Note also that a similar handling was tested by Synopsys |
| 1988 | * (thanks a lot Paul) and nothing bad has come out of it. |
| 1989 | * In short, what we're doing is: |
| 1990 | * |
| 1991 | * - Issue EndTransfer WITH CMDIOC bit set |
| 1992 | * - Wait 100us |
| 1993 | */ |
| 1994 | |
| 1995 | cmd = DWC3_DEPCMD_ENDTRANSFER; |
| 1996 | cmd |= force ? DWC3_DEPCMD_HIPRI_FORCERM : 0; |
| 1997 | cmd |= DWC3_DEPCMD_CMDIOC; |
| 1998 | cmd |= DWC3_DEPCMD_PARAM(dep->resource_index); |
| 1999 | memset(¶ms, 0, sizeof(params)); |
| 2000 | ret = dwc3_send_gadget_ep_cmd(dwc, dep->number, cmd, ¶ms); |
| 2001 | WARN_ON_ONCE(ret); |
| 2002 | dep->resource_index = 0; |
| 2003 | dep->flags &= ~DWC3_EP_BUSY; |
| 2004 | udelay(100); |
| 2005 | } |
| 2006 | |
| 2007 | static void dwc3_stop_active_transfers(struct dwc3 *dwc) |
| 2008 | { |
| 2009 | u32 epnum; |
| 2010 | |
| 2011 | for (epnum = 2; epnum < DWC3_ENDPOINTS_NUM; epnum++) { |
| 2012 | struct dwc3_ep *dep; |
| 2013 | |
| 2014 | dep = dwc->eps[epnum]; |
| 2015 | if (!dep) |
| 2016 | continue; |
| 2017 | |
| 2018 | if (!(dep->flags & DWC3_EP_ENABLED)) |
| 2019 | continue; |
| 2020 | |
| 2021 | dwc3_remove_requests(dwc, dep); |
| 2022 | } |
| 2023 | } |
| 2024 | |
| 2025 | static void dwc3_clear_stall_all_ep(struct dwc3 *dwc) |
| 2026 | { |
| 2027 | u32 epnum; |
| 2028 | |
| 2029 | for (epnum = 1; epnum < DWC3_ENDPOINTS_NUM; epnum++) { |
| 2030 | struct dwc3_ep *dep; |
| 2031 | struct dwc3_gadget_ep_cmd_params params; |
| 2032 | int ret; |
| 2033 | |
| 2034 | dep = dwc->eps[epnum]; |
| 2035 | if (!dep) |
| 2036 | continue; |
| 2037 | |
| 2038 | if (!(dep->flags & DWC3_EP_STALL)) |
| 2039 | continue; |
| 2040 | |
| 2041 | dep->flags &= ~DWC3_EP_STALL; |
| 2042 | |
| 2043 | memset(¶ms, 0, sizeof(params)); |
| 2044 | ret = dwc3_send_gadget_ep_cmd(dwc, dep->number, |
| 2045 | DWC3_DEPCMD_CLEARSTALL, ¶ms); |
| 2046 | WARN_ON_ONCE(ret); |
| 2047 | } |
| 2048 | } |
| 2049 | |
| 2050 | static void dwc3_gadget_disconnect_interrupt(struct dwc3 *dwc) |
| 2051 | { |
| 2052 | int reg; |
| 2053 | |
| 2054 | reg = dwc3_readl(dwc->regs, DWC3_DCTL); |
| 2055 | reg &= ~DWC3_DCTL_INITU1ENA; |
| 2056 | dwc3_writel(dwc->regs, DWC3_DCTL, reg); |
| 2057 | |
| 2058 | reg &= ~DWC3_DCTL_INITU2ENA; |
| 2059 | dwc3_writel(dwc->regs, DWC3_DCTL, reg); |
| 2060 | |
| 2061 | dwc3_disconnect_gadget(dwc); |
| 2062 | dwc->start_config_issued = false; |
| 2063 | |
| 2064 | dwc->gadget.speed = USB_SPEED_UNKNOWN; |
| 2065 | dwc->setup_packet_pending = false; |
| 2066 | usb_gadget_set_state(&dwc->gadget, USB_STATE_NOTATTACHED); |
| 2067 | } |
| 2068 | |
| 2069 | static void dwc3_gadget_reset_interrupt(struct dwc3 *dwc) |
| 2070 | { |
| 2071 | u32 reg; |
| 2072 | |
| 2073 | /* |
| 2074 | * WORKAROUND: DWC3 revisions <1.88a have an issue which |
| 2075 | * would cause a missing Disconnect Event if there's a |
| 2076 | * pending Setup Packet in the FIFO. |
| 2077 | * |
| 2078 | * There's no suggested workaround on the official Bug |
| 2079 | * report, which states that "unless the driver/application |
| 2080 | * is doing any special handling of a disconnect event, |
| 2081 | * there is no functional issue". |
| 2082 | * |
| 2083 | * Unfortunately, it turns out that we _do_ some special |
| 2084 | * handling of a disconnect event, namely complete all |
| 2085 | * pending transfers, notify gadget driver of the |
| 2086 | * disconnection, and so on. |
| 2087 | * |
| 2088 | * Our suggested workaround is to follow the Disconnect |
| 2089 | * Event steps here, instead, based on a setup_packet_pending |
| 2090 | * flag. Such flag gets set whenever we have a XferNotReady |
| 2091 | * event on EP0 and gets cleared on XferComplete for the |
| 2092 | * same endpoint. |
| 2093 | * |
| 2094 | * Refers to: |
| 2095 | * |
| 2096 | * STAR#9000466709: RTL: Device : Disconnect event not |
| 2097 | * generated if setup packet pending in FIFO |
| 2098 | */ |
| 2099 | if (dwc->revision < DWC3_REVISION_188A) { |
| 2100 | if (dwc->setup_packet_pending) |
| 2101 | dwc3_gadget_disconnect_interrupt(dwc); |
| 2102 | } |
| 2103 | |
| 2104 | dwc3_reset_gadget(dwc); |
| 2105 | |
| 2106 | reg = dwc3_readl(dwc->regs, DWC3_DCTL); |
| 2107 | reg &= ~DWC3_DCTL_TSTCTRL_MASK; |
| 2108 | dwc3_writel(dwc->regs, DWC3_DCTL, reg); |
| 2109 | dwc->test_mode = false; |
| 2110 | |
| 2111 | dwc3_stop_active_transfers(dwc); |
| 2112 | dwc3_clear_stall_all_ep(dwc); |
| 2113 | dwc->start_config_issued = false; |
| 2114 | |
| 2115 | /* Reset device address to zero */ |
| 2116 | reg = dwc3_readl(dwc->regs, DWC3_DCFG); |
| 2117 | reg &= ~(DWC3_DCFG_DEVADDR_MASK); |
| 2118 | dwc3_writel(dwc->regs, DWC3_DCFG, reg); |
| 2119 | } |
| 2120 | |
| 2121 | static void dwc3_update_ram_clk_sel(struct dwc3 *dwc, u32 speed) |
| 2122 | { |
| 2123 | u32 reg; |
| 2124 | u32 usb30_clock = DWC3_GCTL_CLK_BUS; |
| 2125 | |
| 2126 | /* |
| 2127 | * We change the clock only at SS but I dunno why I would want to do |
| 2128 | * this. Maybe it becomes part of the power saving plan. |
| 2129 | */ |
| 2130 | |
| 2131 | if (speed != DWC3_DSTS_SUPERSPEED) |
| 2132 | return; |
| 2133 | |
| 2134 | /* |
| 2135 | * RAMClkSel is reset to 0 after USB reset, so it must be reprogrammed |
| 2136 | * each time on Connect Done. |
| 2137 | */ |
| 2138 | if (!usb30_clock) |
| 2139 | return; |
| 2140 | |
| 2141 | reg = dwc3_readl(dwc->regs, DWC3_GCTL); |
| 2142 | reg |= DWC3_GCTL_RAMCLKSEL(usb30_clock); |
| 2143 | dwc3_writel(dwc->regs, DWC3_GCTL, reg); |
| 2144 | } |
| 2145 | |
| 2146 | static void dwc3_gadget_conndone_interrupt(struct dwc3 *dwc) |
| 2147 | { |
| 2148 | struct dwc3_ep *dep; |
| 2149 | int ret; |
| 2150 | u32 reg; |
| 2151 | u8 speed; |
| 2152 | |
| 2153 | reg = dwc3_readl(dwc->regs, DWC3_DSTS); |
| 2154 | speed = reg & DWC3_DSTS_CONNECTSPD; |
| 2155 | dwc->speed = speed; |
| 2156 | |
| 2157 | dwc3_update_ram_clk_sel(dwc, speed); |
| 2158 | |
| 2159 | switch (speed) { |
| 2160 | case DWC3_DCFG_SUPERSPEED: |
| 2161 | /* |
| 2162 | * WORKAROUND: DWC3 revisions <1.90a have an issue which |
| 2163 | * would cause a missing USB3 Reset event. |
| 2164 | * |
| 2165 | * In such situations, we should force a USB3 Reset |
| 2166 | * event by calling our dwc3_gadget_reset_interrupt() |
| 2167 | * routine. |
| 2168 | * |
| 2169 | * Refers to: |
| 2170 | * |
| 2171 | * STAR#9000483510: RTL: SS : USB3 reset event may |
| 2172 | * not be generated always when the link enters poll |
| 2173 | */ |
| 2174 | if (dwc->revision < DWC3_REVISION_190A) |
| 2175 | dwc3_gadget_reset_interrupt(dwc); |
| 2176 | |
| 2177 | dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512); |
| 2178 | dwc->gadget.ep0->maxpacket = 512; |
| 2179 | dwc->gadget.speed = USB_SPEED_SUPER; |
| 2180 | break; |
| 2181 | case DWC3_DCFG_HIGHSPEED: |
| 2182 | dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64); |
| 2183 | dwc->gadget.ep0->maxpacket = 64; |
| 2184 | dwc->gadget.speed = USB_SPEED_HIGH; |
| 2185 | break; |
| 2186 | case DWC3_DCFG_FULLSPEED2: |
| 2187 | case DWC3_DCFG_FULLSPEED1: |
| 2188 | dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64); |
| 2189 | dwc->gadget.ep0->maxpacket = 64; |
| 2190 | dwc->gadget.speed = USB_SPEED_FULL; |
| 2191 | break; |
| 2192 | case DWC3_DCFG_LOWSPEED: |
| 2193 | dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(8); |
| 2194 | dwc->gadget.ep0->maxpacket = 8; |
| 2195 | dwc->gadget.speed = USB_SPEED_LOW; |
| 2196 | break; |
| 2197 | } |
| 2198 | |
| 2199 | /* Enable USB2 LPM Capability */ |
| 2200 | |
| 2201 | if ((dwc->revision > DWC3_REVISION_194A) |
| 2202 | && (speed != DWC3_DCFG_SUPERSPEED)) { |
| 2203 | reg = dwc3_readl(dwc->regs, DWC3_DCFG); |
| 2204 | reg |= DWC3_DCFG_LPM_CAP; |
| 2205 | dwc3_writel(dwc->regs, DWC3_DCFG, reg); |
| 2206 | |
| 2207 | reg = dwc3_readl(dwc->regs, DWC3_DCTL); |
| 2208 | reg &= ~(DWC3_DCTL_HIRD_THRES_MASK | DWC3_DCTL_L1_HIBER_EN); |
| 2209 | |
| 2210 | reg |= DWC3_DCTL_HIRD_THRES(dwc->hird_threshold); |
| 2211 | |
| 2212 | /* |
| 2213 | * When dwc3 revisions >= 2.40a, LPM Erratum is enabled and |
| 2214 | * DCFG.LPMCap is set, core responses with an ACK and the |
| 2215 | * BESL value in the LPM token is less than or equal to LPM |
| 2216 | * NYET threshold. |
| 2217 | */ |
Wolfgang Denk | 0cf207e | 2021-09-27 17:42:39 +0200 | [diff] [blame] | 2218 | if (dwc->revision < DWC3_REVISION_240A && dwc->has_lpm_erratum) |
Kishon Vijay Abraham I | 747a0a5 | 2015-02-23 18:39:58 +0530 | [diff] [blame] | 2219 | WARN(true, "LPM Erratum not available on dwc3 revisisions < 2.40a\n"); |
Kishon Vijay Abraham I | 85d5e70 | 2015-02-23 18:39:50 +0530 | [diff] [blame] | 2220 | |
| 2221 | if (dwc->has_lpm_erratum && dwc->revision >= DWC3_REVISION_240A) |
| 2222 | reg |= DWC3_DCTL_LPM_ERRATA(dwc->lpm_nyet_threshold); |
| 2223 | |
| 2224 | dwc3_writel(dwc->regs, DWC3_DCTL, reg); |
| 2225 | } else { |
| 2226 | reg = dwc3_readl(dwc->regs, DWC3_DCTL); |
| 2227 | reg &= ~DWC3_DCTL_HIRD_THRES_MASK; |
| 2228 | dwc3_writel(dwc->regs, DWC3_DCTL, reg); |
| 2229 | } |
| 2230 | |
| 2231 | dep = dwc->eps[0]; |
| 2232 | ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, true, |
| 2233 | false); |
| 2234 | if (ret) { |
| 2235 | dev_err(dwc->dev, "failed to enable %s\n", dep->name); |
| 2236 | return; |
| 2237 | } |
| 2238 | |
| 2239 | dep = dwc->eps[1]; |
| 2240 | ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, true, |
| 2241 | false); |
| 2242 | if (ret) { |
| 2243 | dev_err(dwc->dev, "failed to enable %s\n", dep->name); |
| 2244 | return; |
| 2245 | } |
| 2246 | |
| 2247 | /* |
| 2248 | * Configure PHY via GUSB3PIPECTLn if required. |
| 2249 | * |
| 2250 | * Update GTXFIFOSIZn |
| 2251 | * |
| 2252 | * In both cases reset values should be sufficient. |
| 2253 | */ |
| 2254 | } |
| 2255 | |
| 2256 | static void dwc3_gadget_wakeup_interrupt(struct dwc3 *dwc) |
| 2257 | { |
| 2258 | /* |
| 2259 | * TODO take core out of low power mode when that's |
| 2260 | * implemented. |
| 2261 | */ |
| 2262 | |
| 2263 | dwc->gadget_driver->resume(&dwc->gadget); |
| 2264 | } |
| 2265 | |
| 2266 | static void dwc3_gadget_linksts_change_interrupt(struct dwc3 *dwc, |
| 2267 | unsigned int evtinfo) |
| 2268 | { |
| 2269 | enum dwc3_link_state next = evtinfo & DWC3_LINK_STATE_MASK; |
| 2270 | unsigned int pwropt; |
| 2271 | |
| 2272 | /* |
| 2273 | * WORKAROUND: DWC3 < 2.50a have an issue when configured without |
| 2274 | * Hibernation mode enabled which would show up when device detects |
| 2275 | * host-initiated U3 exit. |
| 2276 | * |
| 2277 | * In that case, device will generate a Link State Change Interrupt |
| 2278 | * from U3 to RESUME which is only necessary if Hibernation is |
| 2279 | * configured in. |
| 2280 | * |
| 2281 | * There are no functional changes due to such spurious event and we |
| 2282 | * just need to ignore it. |
| 2283 | * |
| 2284 | * Refers to: |
| 2285 | * |
| 2286 | * STAR#9000570034 RTL: SS Resume event generated in non-Hibernation |
| 2287 | * operational mode |
| 2288 | */ |
| 2289 | pwropt = DWC3_GHWPARAMS1_EN_PWROPT(dwc->hwparams.hwparams1); |
| 2290 | if ((dwc->revision < DWC3_REVISION_250A) && |
| 2291 | (pwropt != DWC3_GHWPARAMS1_EN_PWROPT_HIB)) { |
| 2292 | if ((dwc->link_state == DWC3_LINK_STATE_U3) && |
| 2293 | (next == DWC3_LINK_STATE_RESUME)) { |
| 2294 | dev_vdbg(dwc->dev, "ignoring transition U3 -> Resume\n"); |
| 2295 | return; |
| 2296 | } |
| 2297 | } |
| 2298 | |
| 2299 | /* |
| 2300 | * WORKAROUND: DWC3 Revisions <1.83a have an issue which, depending |
| 2301 | * on the link partner, the USB session might do multiple entry/exit |
| 2302 | * of low power states before a transfer takes place. |
| 2303 | * |
| 2304 | * Due to this problem, we might experience lower throughput. The |
| 2305 | * suggested workaround is to disable DCTL[12:9] bits if we're |
| 2306 | * transitioning from U1/U2 to U0 and enable those bits again |
| 2307 | * after a transfer completes and there are no pending transfers |
| 2308 | * on any of the enabled endpoints. |
| 2309 | * |
| 2310 | * This is the first half of that workaround. |
| 2311 | * |
| 2312 | * Refers to: |
| 2313 | * |
| 2314 | * STAR#9000446952: RTL: Device SS : if U1/U2 ->U0 takes >128us |
| 2315 | * core send LGO_Ux entering U0 |
| 2316 | */ |
| 2317 | if (dwc->revision < DWC3_REVISION_183A) { |
| 2318 | if (next == DWC3_LINK_STATE_U0) { |
| 2319 | u32 u1u2; |
| 2320 | u32 reg; |
| 2321 | |
| 2322 | switch (dwc->link_state) { |
| 2323 | case DWC3_LINK_STATE_U1: |
| 2324 | case DWC3_LINK_STATE_U2: |
| 2325 | reg = dwc3_readl(dwc->regs, DWC3_DCTL); |
| 2326 | u1u2 = reg & (DWC3_DCTL_INITU2ENA |
| 2327 | | DWC3_DCTL_ACCEPTU2ENA |
| 2328 | | DWC3_DCTL_INITU1ENA |
| 2329 | | DWC3_DCTL_ACCEPTU1ENA); |
| 2330 | |
| 2331 | if (!dwc->u1u2) |
| 2332 | dwc->u1u2 = reg & u1u2; |
| 2333 | |
| 2334 | reg &= ~u1u2; |
| 2335 | |
| 2336 | dwc3_writel(dwc->regs, DWC3_DCTL, reg); |
| 2337 | break; |
| 2338 | default: |
| 2339 | /* do nothing */ |
| 2340 | break; |
| 2341 | } |
| 2342 | } |
| 2343 | } |
| 2344 | |
| 2345 | switch (next) { |
| 2346 | case DWC3_LINK_STATE_U1: |
| 2347 | if (dwc->speed == USB_SPEED_SUPER) |
| 2348 | dwc3_suspend_gadget(dwc); |
| 2349 | break; |
| 2350 | case DWC3_LINK_STATE_U2: |
| 2351 | case DWC3_LINK_STATE_U3: |
| 2352 | dwc3_suspend_gadget(dwc); |
| 2353 | break; |
| 2354 | case DWC3_LINK_STATE_RESUME: |
| 2355 | dwc3_resume_gadget(dwc); |
| 2356 | break; |
| 2357 | default: |
| 2358 | /* do nothing */ |
| 2359 | break; |
| 2360 | } |
| 2361 | |
| 2362 | dwc->link_state = next; |
| 2363 | } |
| 2364 | |
| 2365 | static void dwc3_gadget_hibernation_interrupt(struct dwc3 *dwc, |
| 2366 | unsigned int evtinfo) |
| 2367 | { |
Lukasz Majewski | 2252d15 | 2015-03-03 17:32:08 +0100 | [diff] [blame] | 2368 | unsigned int is_ss = evtinfo & (1UL << 4); |
Kishon Vijay Abraham I | 85d5e70 | 2015-02-23 18:39:50 +0530 | [diff] [blame] | 2369 | |
| 2370 | /** |
| 2371 | * WORKAROUND: DWC3 revison 2.20a with hibernation support |
| 2372 | * have a known issue which can cause USB CV TD.9.23 to fail |
| 2373 | * randomly. |
| 2374 | * |
| 2375 | * Because of this issue, core could generate bogus hibernation |
| 2376 | * events which SW needs to ignore. |
| 2377 | * |
| 2378 | * Refers to: |
| 2379 | * |
| 2380 | * STAR#9000546576: Device Mode Hibernation: Issue in USB 2.0 |
| 2381 | * Device Fallback from SuperSpeed |
| 2382 | */ |
| 2383 | if (is_ss ^ (dwc->speed == USB_SPEED_SUPER)) |
| 2384 | return; |
| 2385 | |
| 2386 | /* enter hibernation here */ |
| 2387 | } |
| 2388 | |
| 2389 | static void dwc3_gadget_interrupt(struct dwc3 *dwc, |
| 2390 | const struct dwc3_event_devt *event) |
| 2391 | { |
| 2392 | switch (event->type) { |
| 2393 | case DWC3_DEVICE_EVENT_DISCONNECT: |
| 2394 | dwc3_gadget_disconnect_interrupt(dwc); |
| 2395 | break; |
| 2396 | case DWC3_DEVICE_EVENT_RESET: |
| 2397 | dwc3_gadget_reset_interrupt(dwc); |
| 2398 | break; |
| 2399 | case DWC3_DEVICE_EVENT_CONNECT_DONE: |
| 2400 | dwc3_gadget_conndone_interrupt(dwc); |
| 2401 | break; |
| 2402 | case DWC3_DEVICE_EVENT_WAKEUP: |
| 2403 | dwc3_gadget_wakeup_interrupt(dwc); |
| 2404 | break; |
| 2405 | case DWC3_DEVICE_EVENT_HIBER_REQ: |
Kishon Vijay Abraham I | 747a0a5 | 2015-02-23 18:39:58 +0530 | [diff] [blame] | 2406 | if (!dwc->has_hibernation) { |
| 2407 | WARN(1 ,"unexpected hibernation event\n"); |
Kishon Vijay Abraham I | 85d5e70 | 2015-02-23 18:39:50 +0530 | [diff] [blame] | 2408 | break; |
Kishon Vijay Abraham I | 747a0a5 | 2015-02-23 18:39:58 +0530 | [diff] [blame] | 2409 | } |
Kishon Vijay Abraham I | 85d5e70 | 2015-02-23 18:39:50 +0530 | [diff] [blame] | 2410 | dwc3_gadget_hibernation_interrupt(dwc, event->event_info); |
| 2411 | break; |
| 2412 | case DWC3_DEVICE_EVENT_LINK_STATUS_CHANGE: |
| 2413 | dwc3_gadget_linksts_change_interrupt(dwc, event->event_info); |
| 2414 | break; |
| 2415 | case DWC3_DEVICE_EVENT_EOPF: |
| 2416 | dev_vdbg(dwc->dev, "End of Periodic Frame\n"); |
| 2417 | break; |
| 2418 | case DWC3_DEVICE_EVENT_SOF: |
| 2419 | dev_vdbg(dwc->dev, "Start of Periodic Frame\n"); |
| 2420 | break; |
| 2421 | case DWC3_DEVICE_EVENT_ERRATIC_ERROR: |
| 2422 | dev_vdbg(dwc->dev, "Erratic Error\n"); |
| 2423 | break; |
| 2424 | case DWC3_DEVICE_EVENT_CMD_CMPL: |
| 2425 | dev_vdbg(dwc->dev, "Command Complete\n"); |
| 2426 | break; |
| 2427 | case DWC3_DEVICE_EVENT_OVERFLOW: |
| 2428 | dev_vdbg(dwc->dev, "Overflow\n"); |
| 2429 | break; |
| 2430 | default: |
| 2431 | dev_dbg(dwc->dev, "UNKNOWN IRQ %d\n", event->type); |
| 2432 | } |
| 2433 | } |
| 2434 | |
| 2435 | static void dwc3_process_event_entry(struct dwc3 *dwc, |
| 2436 | const union dwc3_event *event) |
| 2437 | { |
Kishon Vijay Abraham I | 85d5e70 | 2015-02-23 18:39:50 +0530 | [diff] [blame] | 2438 | /* Endpoint IRQ, handle it and return early */ |
| 2439 | if (event->type.is_devspec == 0) { |
| 2440 | /* depevt */ |
| 2441 | return dwc3_endpoint_interrupt(dwc, &event->depevt); |
| 2442 | } |
| 2443 | |
| 2444 | switch (event->type.type) { |
| 2445 | case DWC3_EVENT_TYPE_DEV: |
| 2446 | dwc3_gadget_interrupt(dwc, &event->devt); |
| 2447 | break; |
| 2448 | /* REVISIT what to do with Carkit and I2C events ? */ |
| 2449 | default: |
| 2450 | dev_err(dwc->dev, "UNKNOWN IRQ type %d\n", event->raw); |
| 2451 | } |
| 2452 | } |
| 2453 | |
| 2454 | static irqreturn_t dwc3_process_event_buf(struct dwc3 *dwc, u32 buf) |
| 2455 | { |
| 2456 | struct dwc3_event_buffer *evt; |
| 2457 | irqreturn_t ret = IRQ_NONE; |
| 2458 | int left; |
| 2459 | u32 reg; |
| 2460 | |
| 2461 | evt = dwc->ev_buffs[buf]; |
| 2462 | left = evt->count; |
| 2463 | |
| 2464 | if (!(evt->flags & DWC3_EVENT_PENDING)) |
| 2465 | return IRQ_NONE; |
| 2466 | |
| 2467 | while (left > 0) { |
| 2468 | union dwc3_event event; |
| 2469 | |
| 2470 | event.raw = *(u32 *) (evt->buf + evt->lpos); |
| 2471 | |
| 2472 | dwc3_process_event_entry(dwc, &event); |
| 2473 | |
| 2474 | /* |
| 2475 | * FIXME we wrap around correctly to the next entry as |
| 2476 | * almost all entries are 4 bytes in size. There is one |
| 2477 | * entry which has 12 bytes which is a regular entry |
| 2478 | * followed by 8 bytes data. ATM I don't know how |
| 2479 | * things are organized if we get next to the a |
| 2480 | * boundary so I worry about that once we try to handle |
| 2481 | * that. |
| 2482 | */ |
| 2483 | evt->lpos = (evt->lpos + 4) % DWC3_EVENT_BUFFERS_SIZE; |
| 2484 | left -= 4; |
| 2485 | |
| 2486 | dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(buf), 4); |
| 2487 | } |
| 2488 | |
| 2489 | evt->count = 0; |
| 2490 | evt->flags &= ~DWC3_EVENT_PENDING; |
| 2491 | ret = IRQ_HANDLED; |
| 2492 | |
| 2493 | /* Unmask interrupt */ |
| 2494 | reg = dwc3_readl(dwc->regs, DWC3_GEVNTSIZ(buf)); |
| 2495 | reg &= ~DWC3_GEVNTSIZ_INTMASK; |
| 2496 | dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(buf), reg); |
| 2497 | |
| 2498 | return ret; |
| 2499 | } |
| 2500 | |
| 2501 | static irqreturn_t dwc3_thread_interrupt(int irq, void *_dwc) |
| 2502 | { |
| 2503 | struct dwc3 *dwc = _dwc; |
| 2504 | unsigned long flags; |
| 2505 | irqreturn_t ret = IRQ_NONE; |
| 2506 | int i; |
| 2507 | |
| 2508 | spin_lock_irqsave(&dwc->lock, flags); |
| 2509 | |
| 2510 | for (i = 0; i < dwc->num_event_buffers; i++) |
| 2511 | ret |= dwc3_process_event_buf(dwc, i); |
| 2512 | |
| 2513 | spin_unlock_irqrestore(&dwc->lock, flags); |
| 2514 | |
| 2515 | return ret; |
| 2516 | } |
| 2517 | |
| 2518 | static irqreturn_t dwc3_check_event_buf(struct dwc3 *dwc, u32 buf) |
| 2519 | { |
| 2520 | struct dwc3_event_buffer *evt; |
| 2521 | u32 count; |
| 2522 | u32 reg; |
| 2523 | |
| 2524 | evt = dwc->ev_buffs[buf]; |
| 2525 | |
| 2526 | count = dwc3_readl(dwc->regs, DWC3_GEVNTCOUNT(buf)); |
| 2527 | count &= DWC3_GEVNTCOUNT_MASK; |
| 2528 | if (!count) |
| 2529 | return IRQ_NONE; |
| 2530 | |
| 2531 | evt->count = count; |
| 2532 | evt->flags |= DWC3_EVENT_PENDING; |
| 2533 | |
| 2534 | /* Mask interrupt */ |
| 2535 | reg = dwc3_readl(dwc->regs, DWC3_GEVNTSIZ(buf)); |
| 2536 | reg |= DWC3_GEVNTSIZ_INTMASK; |
| 2537 | dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(buf), reg); |
| 2538 | |
| 2539 | return IRQ_WAKE_THREAD; |
| 2540 | } |
| 2541 | |
| 2542 | static irqreturn_t dwc3_interrupt(int irq, void *_dwc) |
| 2543 | { |
| 2544 | struct dwc3 *dwc = _dwc; |
| 2545 | int i; |
| 2546 | irqreturn_t ret = IRQ_NONE; |
| 2547 | |
| 2548 | spin_lock(&dwc->lock); |
| 2549 | |
| 2550 | for (i = 0; i < dwc->num_event_buffers; i++) { |
| 2551 | irqreturn_t status; |
| 2552 | |
| 2553 | status = dwc3_check_event_buf(dwc, i); |
| 2554 | if (status == IRQ_WAKE_THREAD) |
| 2555 | ret = status; |
| 2556 | } |
| 2557 | |
| 2558 | spin_unlock(&dwc->lock); |
| 2559 | |
| 2560 | return ret; |
| 2561 | } |
| 2562 | |
| 2563 | /** |
| 2564 | * dwc3_gadget_init - Initializes gadget related registers |
| 2565 | * @dwc: pointer to our controller context structure |
| 2566 | * |
| 2567 | * Returns 0 on success otherwise negative errno. |
| 2568 | */ |
| 2569 | int dwc3_gadget_init(struct dwc3 *dwc) |
| 2570 | { |
| 2571 | int ret; |
| 2572 | |
Kishon Vijay Abraham I | 747a0a5 | 2015-02-23 18:39:58 +0530 | [diff] [blame] | 2573 | dwc->ctrl_req = dma_alloc_coherent(sizeof(*dwc->ctrl_req), |
| 2574 | (unsigned long *)&dwc->ctrl_req_addr); |
Kishon Vijay Abraham I | 85d5e70 | 2015-02-23 18:39:50 +0530 | [diff] [blame] | 2575 | if (!dwc->ctrl_req) { |
| 2576 | dev_err(dwc->dev, "failed to allocate ctrl request\n"); |
| 2577 | ret = -ENOMEM; |
| 2578 | goto err0; |
| 2579 | } |
| 2580 | |
Kishon Vijay Abraham I | 8d488f3 | 2015-02-23 18:40:15 +0530 | [diff] [blame] | 2581 | dwc->ep0_trb = dma_alloc_coherent(sizeof(*dwc->ep0_trb) * 2, |
Kishon Vijay Abraham I | 747a0a5 | 2015-02-23 18:39:58 +0530 | [diff] [blame] | 2582 | (unsigned long *)&dwc->ep0_trb_addr); |
Kishon Vijay Abraham I | 85d5e70 | 2015-02-23 18:39:50 +0530 | [diff] [blame] | 2583 | if (!dwc->ep0_trb) { |
| 2584 | dev_err(dwc->dev, "failed to allocate ep0 trb\n"); |
| 2585 | ret = -ENOMEM; |
| 2586 | goto err1; |
| 2587 | } |
| 2588 | |
Kishon Vijay Abraham I | 526a50f | 2015-02-23 18:40:13 +0530 | [diff] [blame] | 2589 | dwc->setup_buf = memalign(CONFIG_SYS_CACHELINE_SIZE, |
| 2590 | DWC3_EP0_BOUNCE_SIZE); |
Kishon Vijay Abraham I | 85d5e70 | 2015-02-23 18:39:50 +0530 | [diff] [blame] | 2591 | if (!dwc->setup_buf) { |
| 2592 | ret = -ENOMEM; |
| 2593 | goto err2; |
| 2594 | } |
| 2595 | |
Kishon Vijay Abraham I | 747a0a5 | 2015-02-23 18:39:58 +0530 | [diff] [blame] | 2596 | dwc->ep0_bounce = dma_alloc_coherent(DWC3_EP0_BOUNCE_SIZE, |
| 2597 | (unsigned long *)&dwc->ep0_bounce_addr); |
Kishon Vijay Abraham I | 85d5e70 | 2015-02-23 18:39:50 +0530 | [diff] [blame] | 2598 | if (!dwc->ep0_bounce) { |
| 2599 | dev_err(dwc->dev, "failed to allocate ep0 bounce buffer\n"); |
| 2600 | ret = -ENOMEM; |
| 2601 | goto err3; |
| 2602 | } |
| 2603 | |
| 2604 | dwc->gadget.ops = &dwc3_gadget_ops; |
| 2605 | dwc->gadget.max_speed = USB_SPEED_SUPER; |
| 2606 | dwc->gadget.speed = USB_SPEED_UNKNOWN; |
Kishon Vijay Abraham I | 85d5e70 | 2015-02-23 18:39:50 +0530 | [diff] [blame] | 2607 | dwc->gadget.name = "dwc3-gadget"; |
| 2608 | |
| 2609 | /* |
| 2610 | * Per databook, DWC3 needs buffer size to be aligned to MaxPacketSize |
| 2611 | * on ep out. |
| 2612 | */ |
| 2613 | dwc->gadget.quirk_ep_out_aligned_size = true; |
| 2614 | |
| 2615 | /* |
| 2616 | * REVISIT: Here we should clear all pending IRQs to be |
| 2617 | * sure we're starting from a well known location. |
| 2618 | */ |
| 2619 | |
| 2620 | ret = dwc3_gadget_init_endpoints(dwc); |
| 2621 | if (ret) |
| 2622 | goto err4; |
| 2623 | |
Mugunthan V N | 23ba2d6 | 2018-05-18 13:15:04 +0200 | [diff] [blame] | 2624 | ret = usb_add_gadget_udc((struct device *)dwc->dev, &dwc->gadget); |
Kishon Vijay Abraham I | 85d5e70 | 2015-02-23 18:39:50 +0530 | [diff] [blame] | 2625 | if (ret) { |
| 2626 | dev_err(dwc->dev, "failed to register udc\n"); |
| 2627 | goto err4; |
| 2628 | } |
| 2629 | |
| 2630 | return 0; |
| 2631 | |
| 2632 | err4: |
| 2633 | dwc3_gadget_free_endpoints(dwc); |
Kishon Vijay Abraham I | 747a0a5 | 2015-02-23 18:39:58 +0530 | [diff] [blame] | 2634 | dma_free_coherent(dwc->ep0_bounce); |
Kishon Vijay Abraham I | 85d5e70 | 2015-02-23 18:39:50 +0530 | [diff] [blame] | 2635 | |
| 2636 | err3: |
| 2637 | kfree(dwc->setup_buf); |
| 2638 | |
| 2639 | err2: |
Kishon Vijay Abraham I | 747a0a5 | 2015-02-23 18:39:58 +0530 | [diff] [blame] | 2640 | dma_free_coherent(dwc->ep0_trb); |
Kishon Vijay Abraham I | 85d5e70 | 2015-02-23 18:39:50 +0530 | [diff] [blame] | 2641 | |
| 2642 | err1: |
Kishon Vijay Abraham I | 747a0a5 | 2015-02-23 18:39:58 +0530 | [diff] [blame] | 2643 | dma_free_coherent(dwc->ctrl_req); |
Kishon Vijay Abraham I | 85d5e70 | 2015-02-23 18:39:50 +0530 | [diff] [blame] | 2644 | |
| 2645 | err0: |
| 2646 | return ret; |
| 2647 | } |
| 2648 | |
| 2649 | /* -------------------------------------------------------------------------- */ |
| 2650 | |
| 2651 | void dwc3_gadget_exit(struct dwc3 *dwc) |
| 2652 | { |
| 2653 | usb_del_gadget_udc(&dwc->gadget); |
| 2654 | |
| 2655 | dwc3_gadget_free_endpoints(dwc); |
| 2656 | |
Kishon Vijay Abraham I | 747a0a5 | 2015-02-23 18:39:58 +0530 | [diff] [blame] | 2657 | dma_free_coherent(dwc->ep0_bounce); |
Kishon Vijay Abraham I | 85d5e70 | 2015-02-23 18:39:50 +0530 | [diff] [blame] | 2658 | |
| 2659 | kfree(dwc->setup_buf); |
| 2660 | |
Kishon Vijay Abraham I | 747a0a5 | 2015-02-23 18:39:58 +0530 | [diff] [blame] | 2661 | dma_free_coherent(dwc->ep0_trb); |
Kishon Vijay Abraham I | 85d5e70 | 2015-02-23 18:39:50 +0530 | [diff] [blame] | 2662 | |
Kishon Vijay Abraham I | 747a0a5 | 2015-02-23 18:39:58 +0530 | [diff] [blame] | 2663 | dma_free_coherent(dwc->ctrl_req); |
| 2664 | } |
| 2665 | |
| 2666 | /** |
| 2667 | * dwc3_gadget_uboot_handle_interrupt - handle dwc3 gadget interrupt |
| 2668 | * @dwc: struct dwce * |
| 2669 | * |
| 2670 | * Handles ep0 and gadget interrupt |
| 2671 | * |
| 2672 | * Should be called from dwc3 core. |
| 2673 | */ |
| 2674 | void dwc3_gadget_uboot_handle_interrupt(struct dwc3 *dwc) |
| 2675 | { |
Marek Szyprowski | 137f7c5 | 2015-03-03 17:32:12 +0100 | [diff] [blame] | 2676 | int ret = dwc3_interrupt(0, dwc); |
| 2677 | |
| 2678 | if (ret == IRQ_WAKE_THREAD) { |
| 2679 | int i; |
| 2680 | struct dwc3_event_buffer *evt; |
| 2681 | |
Philipp Tomsich | 889239d | 2017-04-06 16:58:53 +0200 | [diff] [blame] | 2682 | dwc3_thread_interrupt(0, dwc); |
| 2683 | |
| 2684 | /* Clean + Invalidate the buffers after touching them */ |
Marek Szyprowski | 137f7c5 | 2015-03-03 17:32:12 +0100 | [diff] [blame] | 2685 | for (i = 0; i < dwc->num_event_buffers; i++) { |
| 2686 | evt = dwc->ev_buffs[i]; |
Philipp Tomsich | b7bf4a9 | 2017-04-06 16:58:52 +0200 | [diff] [blame] | 2687 | dwc3_flush_cache((uintptr_t)evt->buf, evt->length); |
Marek Szyprowski | 137f7c5 | 2015-03-03 17:32:12 +0100 | [diff] [blame] | 2688 | } |
Marek Szyprowski | 137f7c5 | 2015-03-03 17:32:12 +0100 | [diff] [blame] | 2689 | } |
Kishon Vijay Abraham I | 85d5e70 | 2015-02-23 18:39:50 +0530 | [diff] [blame] | 2690 | } |