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