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