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