blob: 7439c450625e7654185b73df24dfbeb3193f7afc [file] [log] [blame]
Felipe Balbi72246da2011-08-19 18:10:58 +03001/**
2 * gadget.c - DesignWare USB3 DRD Controller Gadget Framework Link
3 *
4 * Copyright (C) 2010-2011 Texas Instruments Incorporated - http://www.ti.com
Felipe Balbi72246da2011-08-19 18:10:58 +03005 *
6 * Authors: Felipe Balbi <balbi@ti.com>,
7 * Sebastian Andrzej Siewior <bigeasy@linutronix.de>
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions, and the following disclaimer,
14 * without modification.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. The names of the above-listed copyright holders may not be used
19 * to endorse or promote products derived from this software without
20 * specific prior written permission.
21 *
22 * ALTERNATIVELY, this software may be distributed under the terms of the
23 * GNU General Public License ("GPL") version 2, as published by the Free
24 * Software Foundation.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
27 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
28 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
30 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 */
38
39#include <linux/kernel.h>
40#include <linux/delay.h>
41#include <linux/slab.h>
42#include <linux/spinlock.h>
43#include <linux/platform_device.h>
44#include <linux/pm_runtime.h>
Wesley Cheng446ad8d2013-06-05 16:15:01 +053045#include <linux/ratelimit.h>
Felipe Balbi72246da2011-08-19 18:10:58 +030046#include <linux/interrupt.h>
47#include <linux/io.h>
48#include <linux/list.h>
49#include <linux/dma-mapping.h>
50
51#include <linux/usb/ch9.h>
52#include <linux/usb/gadget.h>
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +020053#include <linux/usb/otg.h>
Felipe Balbi72246da2011-08-19 18:10:58 +030054
55#include "core.h"
56#include "gadget.h"
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +053057#include "debug.h"
Felipe Balbi72246da2011-08-19 18:10:58 +030058#include "io.h"
59
Vijayavardhan Vennapusaba79f6b2013-07-30 13:39:52 +053060static void dwc3_gadget_usb2_phy_suspend(struct dwc3 *dwc, int suspend);
61static void dwc3_gadget_usb3_phy_suspend(struct dwc3 *dwc, int suspend);
62
Felipe Balbi04a9bfc2012-01-02 18:25:43 +020063/**
64 * dwc3_gadget_set_test_mode - Enables USB2 Test Modes
65 * @dwc: pointer to our context structure
66 * @mode: the mode to set (J, K SE0 NAK, Force Enable)
67 *
68 * Caller should take care of locking. This function will
69 * return 0 on success or -EINVAL if wrong Test Selector
70 * is passed
71 */
72int dwc3_gadget_set_test_mode(struct dwc3 *dwc, int mode)
73{
74 u32 reg;
75
76 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
77 reg &= ~DWC3_DCTL_TSTCTRL_MASK;
78
79 switch (mode) {
80 case TEST_J:
81 case TEST_K:
82 case TEST_SE0_NAK:
83 case TEST_PACKET:
84 case TEST_FORCE_EN:
85 reg |= mode << 1;
86 break;
87 default:
88 return -EINVAL;
89 }
90
91 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
92
93 return 0;
94}
95
Felipe Balbi8598bde2012-01-02 18:55:57 +020096/**
97 * dwc3_gadget_set_link_state - Sets USB Link to a particular State
98 * @dwc: pointer to our context structure
99 * @state: the state to put link into
100 *
101 * Caller should take care of locking. This function will
Paul Zimmermanaee63e32012-02-24 17:32:15 -0800102 * return 0 on success or -ETIMEDOUT.
Felipe Balbi8598bde2012-01-02 18:55:57 +0200103 */
104int dwc3_gadget_set_link_state(struct dwc3 *dwc, enum dwc3_link_state state)
105{
Paul Zimmermanaee63e32012-02-24 17:32:15 -0800106 int retries = 10000;
Felipe Balbi8598bde2012-01-02 18:55:57 +0200107 u32 reg;
108
Paul Zimmerman88df4272012-04-27 13:10:52 +0300109 /*
110 * Wait until device controller is ready. Only applies to 1.94a and
111 * later RTL.
112 */
113 if (dwc->revision >= DWC3_REVISION_194A) {
114 while (--retries) {
115 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
116 if (reg & DWC3_DSTS_DCNRD)
117 udelay(5);
118 else
119 break;
120 }
121
122 if (retries <= 0)
123 return -ETIMEDOUT;
124 }
125
Felipe Balbi8598bde2012-01-02 18:55:57 +0200126 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
127 reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK;
128
129 /* set requested state */
130 reg |= DWC3_DCTL_ULSTCHNGREQ(state);
131 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
132
Paul Zimmerman88df4272012-04-27 13:10:52 +0300133 /*
134 * The following code is racy when called from dwc3_gadget_wakeup,
135 * and is not needed, at least on newer versions
136 */
137 if (dwc->revision >= DWC3_REVISION_194A)
138 return 0;
139
Felipe Balbi8598bde2012-01-02 18:55:57 +0200140 /* wait for a change in DSTS */
Paul Zimmerman8b9388f2012-04-27 12:52:01 +0300141 retries = 10000;
Felipe Balbi8598bde2012-01-02 18:55:57 +0200142 while (--retries) {
143 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
144
Felipe Balbi8598bde2012-01-02 18:55:57 +0200145 if (DWC3_DSTS_USBLNKST(reg) == state)
146 return 0;
147
Paul Zimmermanaee63e32012-02-24 17:32:15 -0800148 udelay(5);
Felipe Balbi8598bde2012-01-02 18:55:57 +0200149 }
150
151 dev_vdbg(dwc->dev, "link state change request timed out\n");
152
153 return -ETIMEDOUT;
154}
155
Felipe Balbi457e84b2012-01-18 18:04:09 +0200156/**
157 * dwc3_gadget_resize_tx_fifos - reallocate fifo spaces for current use-case
158 * @dwc: pointer to our context structure
159 *
160 * This function will a best effort FIFO allocation in order
161 * to improve FIFO usage and throughput, while still allowing
162 * us to enable as many endpoints as possible.
163 *
164 * Keep in mind that this operation will be highly dependent
165 * on the configured size for RAM1 - which contains TxFifo -,
166 * the amount of endpoints enabled on coreConsultant tool, and
167 * the width of the Master Bus.
168 *
169 * In the ideal world, we would always be able to satisfy the
170 * following equation:
171 *
172 * ((512 + 2 * MDWIDTH-Bytes) + (Number of IN Endpoints - 1) * \
173 * (3 * (1024 + MDWIDTH-Bytes) + MDWIDTH-Bytes)) / MDWIDTH-Bytes
174 *
175 * Unfortunately, due to many variables that's not always the case.
176 */
177int dwc3_gadget_resize_tx_fifos(struct dwc3 *dwc)
178{
179 int last_fifo_depth = 0;
180 int ram1_depth;
181 int fifo_size;
182 int mdwidth;
183 int num;
184
Vijayavardhan Vennapusa540ee162013-08-20 18:21:42 +0530185 if (!dwc->needs_fifo_resize)
Felipe Balbi457e84b2012-01-18 18:04:09 +0200186 return 0;
187
188 ram1_depth = DWC3_RAM1_DEPTH(dwc->hwparams.hwparams7);
189 mdwidth = DWC3_MDWIDTH(dwc->hwparams.hwparams0);
190
191 /* MDWIDTH is represented in bits, we need it in bytes */
192 mdwidth >>= 3;
193
194 /*
195 * FIXME For now we will only allocate 1 wMaxPacketSize space
196 * for each enabled endpoint, later patches will come to
197 * improve this algorithm so that we better use the internal
Vijayavardhan Vennapusadaf082c2013-03-01 13:08:59 +0530198 * FIFO space. Also consider the case where TxFIFO RAM space
199 * may change dynamically based on the USB configuration.
Felipe Balbi457e84b2012-01-18 18:04:09 +0200200 */
201 for (num = 0; num < DWC3_ENDPOINTS_NUM; num++) {
202 struct dwc3_ep *dep = dwc->eps[num];
203 int fifo_number = dep->number >> 1;
Felipe Balbi2e81c362012-02-02 13:01:12 +0200204 int mult = 1;
Felipe Balbi457e84b2012-01-18 18:04:09 +0200205 int tmp;
206
207 if (!(dep->number & 1))
208 continue;
209
210 if (!(dep->flags & DWC3_EP_ENABLED))
211 continue;
212
Vijayavardhan Vennapusadaf082c2013-03-01 13:08:59 +0530213 if (((dep->endpoint.maxburst > 1) &&
214 usb_endpoint_xfer_bulk(dep->endpoint.desc))
Ido Shayevitz57cdac12012-03-12 20:25:24 +0200215 || usb_endpoint_xfer_isoc(dep->endpoint.desc))
Felipe Balbi2e81c362012-02-02 13:01:12 +0200216 mult = 3;
217
218 /*
219 * REVISIT: the following assumes we will always have enough
220 * space available on the FIFO RAM for all possible use cases.
221 * Make sure that's true somehow and change FIFO allocation
222 * accordingly.
223 *
Vijayavardhan Vennapusadaf082c2013-03-01 13:08:59 +0530224 * If we have Bulk (burst only) or Isochronous endpoints, we
225 * want them to be able to be very, very fast. So we're giving
Felipe Balbi2e81c362012-02-02 13:01:12 +0200226 * those endpoints a fifo_size which is enough for 3 full
227 * packets
228 */
229 tmp = mult * (dep->endpoint.maxpacket + mdwidth);
Vijayavardhan Vennapusafc3db602013-08-20 17:54:54 +0530230
231 if (dwc->tx_fifo_size &&
Vijayavardhan Vennapusab77e1432013-10-03 18:17:50 +0530232 (usb_endpoint_xfer_bulk(dep->endpoint.desc)
233 || usb_endpoint_xfer_isoc(dep->endpoint.desc))) {
Vijayavardhan Vennapusafc3db602013-08-20 17:54:54 +0530234 /*
235 * Allocate 3KB fifo size for bulk and isochronous TX
Vijayavardhan Vennapusab77e1432013-10-03 18:17:50 +0530236 * endpoints irrespective of speed if tx_fifo is not
237 * reduced. Otherwise allocate 1KB for endpoints in HS
238 * mode and for non burst endpoints in SS mode. For
239 * interrupt ep, allocate fifo size of ep maxpacket.
Vijayavardhan Vennapusafc3db602013-08-20 17:54:54 +0530240 */
Vijayavardhan Vennapusab77e1432013-10-03 18:17:50 +0530241 if (!dwc->tx_fifo_reduced)
242 tmp = 3 * (1024 + mdwidth);
243 else
244 tmp = mult * (1024 + mdwidth);
245 }
Vijayavardhan Vennapusafc3db602013-08-20 17:54:54 +0530246
Felipe Balbi457e84b2012-01-18 18:04:09 +0200247 tmp += mdwidth;
248
249 fifo_size = DIV_ROUND_UP(tmp, mdwidth);
Felipe Balbi2e81c362012-02-02 13:01:12 +0200250
Felipe Balbi457e84b2012-01-18 18:04:09 +0200251 fifo_size |= (last_fifo_depth << 16);
252
253 dev_vdbg(dwc->dev, "%s: Fifo Addr %04x Size %d\n",
254 dep->name, last_fifo_depth, fifo_size & 0xffff);
255
Vijayavardhan Vennapusafc3db602013-08-20 17:54:54 +0530256 last_fifo_depth += (fifo_size & 0xffff);
257 if (dwc->tx_fifo_size &&
258 (last_fifo_depth >= dwc->tx_fifo_size)) {
259 /*
260 * Fifo size allocated exceeded available RAM size.
261 * Hence return error.
262 */
263 dev_err(dwc->dev, "Fifosize(%d) > available RAM(%d)\n",
264 last_fifo_depth, dwc->tx_fifo_size);
265 return -ENOMEM;
266 }
267
Felipe Balbi457e84b2012-01-18 18:04:09 +0200268 dwc3_writel(dwc->regs, DWC3_GTXFIFOSIZ(fifo_number),
269 fifo_size);
270
Felipe Balbi457e84b2012-01-18 18:04:09 +0200271 }
272
273 return 0;
274}
275
Felipe Balbi72246da2011-08-19 18:10:58 +0300276void dwc3_gadget_giveback(struct dwc3_ep *dep, struct dwc3_request *req,
277 int status)
278{
279 struct dwc3 *dwc = dep->dwc;
280
281 if (req->queued) {
Manu Gautam55d34222012-12-19 16:49:47 +0530282 req->queued = false;
283
Felipe Balbieeb720f2011-11-28 12:46:59 +0200284 if (req->request.num_mapped_sgs)
285 dep->busy_slot += req->request.num_mapped_sgs;
286 else
287 dep->busy_slot++;
288
Felipe Balbi72246da2011-08-19 18:10:58 +0300289 /*
290 * Skip LINK TRB. We can't use req->trb and check for
291 * DWC3_TRBCTL_LINK_TRB because it points the TRB we just
292 * completed (not the LINK TRB).
293 */
294 if (((dep->busy_slot & DWC3_TRB_MASK) == DWC3_TRB_NUM - 1) &&
Ido Shayevitz57cdac12012-03-12 20:25:24 +0200295 usb_endpoint_xfer_isoc(dep->endpoint.desc))
Felipe Balbi72246da2011-08-19 18:10:58 +0300296 dep->busy_slot++;
Vijayavardhan Vennapusa5f5c3e62013-11-13 19:53:13 +0530297
298 if (req->request.zero && req->ztrb) {
299 dep->busy_slot++;
300 req->ztrb = NULL;
301 if (((dep->busy_slot & DWC3_TRB_MASK) ==
302 DWC3_TRB_NUM - 1) &&
303 usb_endpoint_xfer_isoc(dep->endpoint.desc))
304 dep->busy_slot++;
305 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300306 }
307 list_del(&req->list);
Felipe Balbieeb720f2011-11-28 12:46:59 +0200308 req->trb = NULL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300309
310 if (req->request.status == -EINPROGRESS)
311 req->request.status = status;
312
Pratyush Anand8d7bf592012-08-10 13:42:16 +0530313 if (dwc->ep0_bounced && dep->number == 0)
314 dwc->ep0_bounced = false;
315 else
316 usb_gadget_unmap_request(&dwc->gadget, &req->request,
317 req->direction);
Felipe Balbi72246da2011-08-19 18:10:58 +0300318
319 dev_dbg(dwc->dev, "request %p from %s completed %d/%d ===> %d\n",
320 req, dep->name, req->request.actual,
321 req->request.length, status);
322
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +0530323 dbg_done(dep->number, req->request.actual, req->request.status);
Felipe Balbi72246da2011-08-19 18:10:58 +0300324 spin_unlock(&dwc->lock);
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +0200325 req->request.complete(&dep->endpoint, &req->request);
Felipe Balbi72246da2011-08-19 18:10:58 +0300326 spin_lock(&dwc->lock);
327}
328
329static const char *dwc3_gadget_ep_cmd_string(u8 cmd)
330{
331 switch (cmd) {
332 case DWC3_DEPCMD_DEPSTARTCFG:
333 return "Start New Configuration";
334 case DWC3_DEPCMD_ENDTRANSFER:
335 return "End Transfer";
336 case DWC3_DEPCMD_UPDATETRANSFER:
337 return "Update Transfer";
338 case DWC3_DEPCMD_STARTTRANSFER:
339 return "Start Transfer";
340 case DWC3_DEPCMD_CLEARSTALL:
341 return "Clear Stall";
342 case DWC3_DEPCMD_SETSTALL:
343 return "Set Stall";
Paul Zimmerman88df4272012-04-27 13:10:52 +0300344 case DWC3_DEPCMD_GETEPSTATE:
345 return "Get Endpoint State";
Felipe Balbi72246da2011-08-19 18:10:58 +0300346 case DWC3_DEPCMD_SETTRANSFRESOURCE:
347 return "Set Endpoint Transfer Resource";
348 case DWC3_DEPCMD_SETEPCONFIG:
349 return "Set Endpoint Configuration";
350 default:
351 return "UNKNOWN command";
352 }
353}
354
Felipe Balbi573c2762012-04-24 16:19:11 +0300355int dwc3_send_gadget_generic_command(struct dwc3 *dwc, int cmd, u32 param)
356{
357 u32 timeout = 500;
358 u32 reg;
Vijayavardhan Vennapusaba79f6b2013-07-30 13:39:52 +0530359 bool hsphy_suspend_enabled;
360 int ret;
361
362 /* Commands to controller will work only if PHY is not suspended */
363 hsphy_suspend_enabled = (dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0)) &
364 DWC3_GUSB2PHYCFG_SUSPHY);
365
366 /* Disable suspend of the USB2 PHY */
367 if (hsphy_suspend_enabled)
368 dwc3_gadget_usb2_phy_suspend(dwc, false);
Felipe Balbi573c2762012-04-24 16:19:11 +0300369
370 dwc3_writel(dwc->regs, DWC3_DGCMDPAR, param);
371 dwc3_writel(dwc->regs, DWC3_DGCMD, cmd | DWC3_DGCMD_CMDACT);
372
373 do {
374 reg = dwc3_readl(dwc->regs, DWC3_DGCMD);
375 if (!(reg & DWC3_DGCMD_CMDACT)) {
376 dev_vdbg(dwc->dev, "Command Complete --> %d\n",
377 DWC3_DGCMD_STATUS(reg));
Vijayavardhan Vennapusaba79f6b2013-07-30 13:39:52 +0530378 ret = 0;
379 break;
Felipe Balbi573c2762012-04-24 16:19:11 +0300380 }
381
382 /*
383 * We can't sleep here, because it's also called from
384 * interrupt context.
385 */
386 timeout--;
Vijayavardhan Vennapusaba79f6b2013-07-30 13:39:52 +0530387 if (!timeout) {
388 ret = -ETIMEDOUT;
389 break;
390 }
Felipe Balbi573c2762012-04-24 16:19:11 +0300391 udelay(1);
392 } while (1);
Vijayavardhan Vennapusaba79f6b2013-07-30 13:39:52 +0530393
394 /* Enable suspend of the USB2 PHY */
395 if (hsphy_suspend_enabled)
396 dwc3_gadget_usb2_phy_suspend(dwc, true);
397
398 return ret;
Felipe Balbi573c2762012-04-24 16:19:11 +0300399}
400
Felipe Balbi72246da2011-08-19 18:10:58 +0300401int dwc3_send_gadget_ep_cmd(struct dwc3 *dwc, unsigned ep,
402 unsigned cmd, struct dwc3_gadget_ep_cmd_params *params)
403{
404 struct dwc3_ep *dep = dwc->eps[ep];
Sebastian Andrzej Siewior61d58242011-08-29 16:46:38 +0200405 u32 timeout = 500;
Felipe Balbi72246da2011-08-19 18:10:58 +0300406 u32 reg;
Vijayavardhan Vennapusaba79f6b2013-07-30 13:39:52 +0530407 bool hsphy_suspend_enabled;
408 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300409
410 dev_vdbg(dwc->dev, "%s: cmd '%s' params %08x %08x %08x\n",
411 dep->name,
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300412 dwc3_gadget_ep_cmd_string(cmd), params->param0,
413 params->param1, params->param2);
Felipe Balbi72246da2011-08-19 18:10:58 +0300414
Vijayavardhan Vennapusaba79f6b2013-07-30 13:39:52 +0530415 /* Commands to controller will work only if PHY is not suspended */
416 hsphy_suspend_enabled = (dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0)) &
417 DWC3_GUSB2PHYCFG_SUSPHY);
418
419 /* Disable suspend of the USB2 PHY */
420 if (hsphy_suspend_enabled)
421 dwc3_gadget_usb2_phy_suspend(dwc, false);
422
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300423 dwc3_writel(dwc->regs, DWC3_DEPCMDPAR0(ep), params->param0);
424 dwc3_writel(dwc->regs, DWC3_DEPCMDPAR1(ep), params->param1);
425 dwc3_writel(dwc->regs, DWC3_DEPCMDPAR2(ep), params->param2);
Felipe Balbi72246da2011-08-19 18:10:58 +0300426
427 dwc3_writel(dwc->regs, DWC3_DEPCMD(ep), cmd | DWC3_DEPCMD_CMDACT);
428 do {
429 reg = dwc3_readl(dwc->regs, DWC3_DEPCMD(ep));
430 if (!(reg & DWC3_DEPCMD_CMDACT)) {
Felipe Balbi164f6e12011-08-27 20:29:58 +0300431 dev_vdbg(dwc->dev, "Command Complete --> %d\n",
432 DWC3_DEPCMD_STATUS(reg));
Vijayavardhan Vennapusa91ba6532013-01-30 17:35:45 +0530433 /* SW issues START TRANSFER command to isochronous ep
434 * with future frame interval. If future interval time
435 * has already passed when core recieves command, core
436 * will respond with an error(bit13 in Command complete
437 * event. Hence return error in this case.
438 */
439 if (reg & 0x2000)
Vijayavardhan Vennapusaba79f6b2013-07-30 13:39:52 +0530440 ret = -EAGAIN;
Vijayavardhan Vennapusa91ba6532013-01-30 17:35:45 +0530441 else
Vijayavardhan Vennapusaba79f6b2013-07-30 13:39:52 +0530442 ret = 0;
443 break;
Felipe Balbi72246da2011-08-19 18:10:58 +0300444 }
445
446 /*
Felipe Balbi72246da2011-08-19 18:10:58 +0300447 * We can't sleep here, because it is also called from
448 * interrupt context.
449 */
450 timeout--;
Vijayavardhan Vennapusaba79f6b2013-07-30 13:39:52 +0530451 if (!timeout) {
452 ret = -ETIMEDOUT;
453 break;
454 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300455
Sebastian Andrzej Siewior61d58242011-08-29 16:46:38 +0200456 udelay(1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300457 } while (1);
Vijayavardhan Vennapusaba79f6b2013-07-30 13:39:52 +0530458
459 /* Enable suspend of the USB2 PHY */
460 if (hsphy_suspend_enabled)
461 dwc3_gadget_usb2_phy_suspend(dwc, true);
462
463 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300464}
465
Ido Shayevitzfa65a582012-06-06 14:39:54 +0300466dma_addr_t dwc3_trb_dma_offset(struct dwc3_ep *dep,
Felipe Balbif6bafc62012-02-06 11:04:53 +0200467 struct dwc3_trb *trb)
Felipe Balbi72246da2011-08-19 18:10:58 +0300468{
Paul Zimmermanc439ef82011-09-30 10:58:45 +0300469 u32 offset = (char *) trb - (char *) dep->trb_pool;
Felipe Balbi72246da2011-08-19 18:10:58 +0300470
471 return dep->trb_pool_dma + offset;
472}
473
474static int dwc3_alloc_trb_pool(struct dwc3_ep *dep)
475{
476 struct dwc3 *dwc = dep->dwc;
477
478 if (dep->trb_pool)
479 return 0;
480
481 if (dep->number == 0 || dep->number == 1)
482 return 0;
483
484 dep->trb_pool = dma_alloc_coherent(dwc->dev,
485 sizeof(struct dwc3_trb) * DWC3_TRB_NUM,
486 &dep->trb_pool_dma, GFP_KERNEL);
487 if (!dep->trb_pool) {
488 dev_err(dep->dwc->dev, "failed to allocate trb pool for %s\n",
489 dep->name);
490 return -ENOMEM;
491 }
492
493 return 0;
494}
495
496static void dwc3_free_trb_pool(struct dwc3_ep *dep)
497{
498 struct dwc3 *dwc = dep->dwc;
499
500 dma_free_coherent(dwc->dev, sizeof(struct dwc3_trb) * DWC3_TRB_NUM,
501 dep->trb_pool, dep->trb_pool_dma);
502
503 dep->trb_pool = NULL;
504 dep->trb_pool_dma = 0;
505}
506
507static int dwc3_gadget_start_config(struct dwc3 *dwc, struct dwc3_ep *dep)
508{
509 struct dwc3_gadget_ep_cmd_params params;
510 u32 cmd;
511
512 memset(&params, 0x00, sizeof(params));
513
514 if (dep->number != 1) {
515 cmd = DWC3_DEPCMD_DEPSTARTCFG;
516 /* XferRscIdx == 0 for ep0 and 2 for the remaining */
Paul Zimmermanb23c8432011-09-30 10:58:42 +0300517 if (dep->number > 1) {
518 if (dwc->start_config_issued)
519 return 0;
520 dwc->start_config_issued = true;
Felipe Balbi72246da2011-08-19 18:10:58 +0300521 cmd |= DWC3_DEPCMD_PARAM(2);
Paul Zimmermanb23c8432011-09-30 10:58:42 +0300522 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300523
524 return dwc3_send_gadget_ep_cmd(dwc, 0, cmd, &params);
525 }
526
527 return 0;
528}
529
530static int dwc3_gadget_set_ep_config(struct dwc3 *dwc, struct dwc3_ep *dep,
Felipe Balbic90bfae2011-11-29 13:11:21 +0200531 const struct usb_endpoint_descriptor *desc,
Felipe Balbi07e0ee82012-07-16 14:08:16 +0300532 const struct usb_ss_ep_comp_descriptor *comp_desc,
533 bool ignore)
Felipe Balbi72246da2011-08-19 18:10:58 +0300534{
535 struct dwc3_gadget_ep_cmd_params params;
536
537 memset(&params, 0x00, sizeof(params));
538
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300539 params.param0 = DWC3_DEPCFG_EP_TYPE(usb_endpoint_type(desc))
Chanho Parkf0ee6062012-08-31 16:54:07 +0900540 | DWC3_DEPCFG_MAX_PACKET_SIZE(usb_endpoint_maxp(desc));
541
542 /* Burst size is only needed in SuperSpeed mode */
543 if (dwc->gadget.speed == USB_SPEED_SUPER) {
544 u32 burst = dep->endpoint.maxburst - 1;
545
546 params.param0 |= DWC3_DEPCFG_BURST_SIZE(burst);
547 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300548
Felipe Balbi07e0ee82012-07-16 14:08:16 +0300549 if (ignore)
550 params.param0 |= DWC3_DEPCFG_IGN_SEQ_NUM;
Felipe Balbi72246da2011-08-19 18:10:58 +0300551
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300552 params.param1 = DWC3_DEPCFG_XFER_COMPLETE_EN
553 | DWC3_DEPCFG_XFER_NOT_READY_EN;
Felipe Balbi72246da2011-08-19 18:10:58 +0300554
Felipe Balbi18b7ede2012-01-02 13:35:41 +0200555 if (usb_ss_max_streams(comp_desc) && usb_endpoint_xfer_bulk(desc)) {
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300556 params.param1 |= DWC3_DEPCFG_STREAM_CAPABLE
557 | DWC3_DEPCFG_STREAM_EVENT_EN;
Felipe Balbi879631a2011-09-30 10:58:47 +0300558 dep->stream_capable = true;
559 }
560
Felipe Balbi72246da2011-08-19 18:10:58 +0300561 if (usb_endpoint_xfer_isoc(desc))
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300562 params.param1 |= DWC3_DEPCFG_XFER_IN_PROGRESS_EN;
Felipe Balbi72246da2011-08-19 18:10:58 +0300563
564 /*
565 * We are doing 1:1 mapping for endpoints, meaning
566 * Physical Endpoints 2 maps to Logical Endpoint 2 and
567 * so on. We consider the direction bit as part of the physical
568 * endpoint number. So USB endpoint 0x81 is 0x03.
569 */
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300570 params.param1 |= DWC3_DEPCFG_EP_NUMBER(dep->number);
Felipe Balbi72246da2011-08-19 18:10:58 +0300571
572 /*
573 * We must use the lower 16 TX FIFOs even though
574 * HW might have more
575 */
576 if (dep->direction)
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300577 params.param0 |= DWC3_DEPCFG_FIFO_NUMBER(dep->number >> 1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300578
579 if (desc->bInterval) {
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300580 params.param1 |= DWC3_DEPCFG_BINTERVAL_M1(desc->bInterval - 1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300581 dep->interval = 1 << (desc->bInterval - 1);
582 }
583
584 return dwc3_send_gadget_ep_cmd(dwc, dep->number,
585 DWC3_DEPCMD_SETEPCONFIG, &params);
586}
587
588static int dwc3_gadget_set_xfer_resource(struct dwc3 *dwc, struct dwc3_ep *dep)
589{
590 struct dwc3_gadget_ep_cmd_params params;
591
592 memset(&params, 0x00, sizeof(params));
593
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300594 params.param0 = DWC3_DEPXFERCFG_NUM_XFER_RES(1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300595
596 return dwc3_send_gadget_ep_cmd(dwc, dep->number,
597 DWC3_DEPCMD_SETTRANSFRESOURCE, &params);
598}
599
600/**
601 * __dwc3_gadget_ep_enable - Initializes a HW endpoint
602 * @dep: endpoint to be initialized
603 * @desc: USB Endpoint Descriptor
604 *
605 * Caller should take care of locking
606 */
607static int __dwc3_gadget_ep_enable(struct dwc3_ep *dep,
Felipe Balbic90bfae2011-11-29 13:11:21 +0200608 const struct usb_endpoint_descriptor *desc,
Felipe Balbi07e0ee82012-07-16 14:08:16 +0300609 const struct usb_ss_ep_comp_descriptor *comp_desc,
610 bool ignore)
Felipe Balbi72246da2011-08-19 18:10:58 +0300611{
612 struct dwc3 *dwc = dep->dwc;
613 u32 reg;
614 int ret = -ENOMEM;
615
616 if (!(dep->flags & DWC3_EP_ENABLED)) {
617 ret = dwc3_gadget_start_config(dwc, dep);
618 if (ret)
619 return ret;
620 }
621
Felipe Balbi07e0ee82012-07-16 14:08:16 +0300622 ret = dwc3_gadget_set_ep_config(dwc, dep, desc, comp_desc, ignore);
Felipe Balbi72246da2011-08-19 18:10:58 +0300623 if (ret)
624 return ret;
625
626 if (!(dep->flags & DWC3_EP_ENABLED)) {
Felipe Balbif6bafc62012-02-06 11:04:53 +0200627 struct dwc3_trb *trb_st_hw;
628 struct dwc3_trb *trb_link;
Felipe Balbi72246da2011-08-19 18:10:58 +0300629
630 ret = dwc3_gadget_set_xfer_resource(dwc, dep);
631 if (ret)
632 return ret;
633
Ido Shayevitz57cdac12012-03-12 20:25:24 +0200634 dep->endpoint.desc = desc;
Felipe Balbic90bfae2011-11-29 13:11:21 +0200635 dep->comp_desc = comp_desc;
Felipe Balbi72246da2011-08-19 18:10:58 +0300636 dep->type = usb_endpoint_type(desc);
637 dep->flags |= DWC3_EP_ENABLED;
638
639 reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
640 reg |= DWC3_DALEPENA_EP(dep->number);
641 dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
642
643 if (!usb_endpoint_xfer_isoc(desc))
644 return 0;
645
646 memset(&trb_link, 0, sizeof(trb_link));
647
Paul Zimmerman1d046792012-02-15 18:56:56 -0800648 /* Link TRB for ISOC. The HWO bit is never reset */
Felipe Balbi72246da2011-08-19 18:10:58 +0300649 trb_st_hw = &dep->trb_pool[0];
650
Felipe Balbif6bafc62012-02-06 11:04:53 +0200651 trb_link = &dep->trb_pool[DWC3_TRB_NUM - 1];
Felipe Balbi72246da2011-08-19 18:10:58 +0300652
Felipe Balbif6bafc62012-02-06 11:04:53 +0200653 trb_link->bpl = lower_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw));
654 trb_link->bph = upper_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw));
655 trb_link->ctrl |= DWC3_TRBCTL_LINK_TRB;
656 trb_link->ctrl |= DWC3_TRB_CTRL_HWO;
Felipe Balbi72246da2011-08-19 18:10:58 +0300657 }
658
659 return 0;
660}
661
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200662static void dwc3_stop_active_transfer(struct dwc3 *dwc, u32 epnum);
663static void dwc3_remove_requests(struct dwc3 *dwc, struct dwc3_ep *dep)
Felipe Balbi72246da2011-08-19 18:10:58 +0300664{
665 struct dwc3_request *req;
666
Felipe Balbib129eb72012-02-17 12:10:04 +0200667 if (!list_empty(&dep->req_queued)) {
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200668 dwc3_stop_active_transfer(dwc, dep->number);
669
Pratyush Anande67fdeb2012-07-06 15:19:10 +0530670 /* - giveback all requests to gadget driver */
Pratyush Anand110ff602012-06-15 11:54:36 +0530671 while (!list_empty(&dep->req_queued)) {
672 req = next_request(&dep->req_queued);
673
674 dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
675 }
Felipe Balbib129eb72012-02-17 12:10:04 +0200676 }
677
Felipe Balbi72246da2011-08-19 18:10:58 +0300678 while (!list_empty(&dep->request_list)) {
679 req = next_request(&dep->request_list);
680
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200681 dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
Felipe Balbi72246da2011-08-19 18:10:58 +0300682 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300683}
684
685/**
686 * __dwc3_gadget_ep_disable - Disables a HW endpoint
687 * @dep: the endpoint to disable
688 *
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200689 * This function also removes requests which are currently processed ny the
690 * hardware and those which are not yet scheduled.
691 * Caller should take care of locking.
Felipe Balbi72246da2011-08-19 18:10:58 +0300692 */
Felipe Balbi72246da2011-08-19 18:10:58 +0300693static int __dwc3_gadget_ep_disable(struct dwc3_ep *dep)
694{
695 struct dwc3 *dwc = dep->dwc;
696 u32 reg;
697
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200698 dwc3_remove_requests(dwc, dep);
Felipe Balbi72246da2011-08-19 18:10:58 +0300699
700 reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
701 reg &= ~DWC3_DALEPENA_EP(dep->number);
702 dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
703
Felipe Balbi879631a2011-09-30 10:58:47 +0300704 dep->stream_capable = false;
Ido Shayevitzf9c56cd2012-02-08 13:56:48 +0200705 dep->endpoint.desc = NULL;
Felipe Balbic90bfae2011-11-29 13:11:21 +0200706 dep->comp_desc = NULL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300707 dep->type = 0;
Felipe Balbi879631a2011-09-30 10:58:47 +0300708 dep->flags = 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300709
710 return 0;
711}
712
713/* -------------------------------------------------------------------------- */
714
715static int dwc3_gadget_ep0_enable(struct usb_ep *ep,
716 const struct usb_endpoint_descriptor *desc)
717{
718 return -EINVAL;
719}
720
721static int dwc3_gadget_ep0_disable(struct usb_ep *ep)
722{
723 return -EINVAL;
724}
725
726/* -------------------------------------------------------------------------- */
727
728static int dwc3_gadget_ep_enable(struct usb_ep *ep,
729 const struct usb_endpoint_descriptor *desc)
730{
731 struct dwc3_ep *dep;
732 struct dwc3 *dwc;
733 unsigned long flags;
734 int ret;
735
736 if (!ep || !desc || desc->bDescriptorType != USB_DT_ENDPOINT) {
737 pr_debug("dwc3: invalid parameters\n");
738 return -EINVAL;
739 }
740
741 if (!desc->wMaxPacketSize) {
742 pr_debug("dwc3: missing wMaxPacketSize\n");
743 return -EINVAL;
744 }
745
746 dep = to_dwc3_ep(ep);
747 dwc = dep->dwc;
748
Felipe Balbi14395072012-08-15 12:28:29 +0300749 if (dep->flags & DWC3_EP_ENABLED) {
750 dev_WARN_ONCE(dwc->dev, true, "%s is already enabled\n",
751 dep->name);
752 return 0;
753 }
754
Felipe Balbi72246da2011-08-19 18:10:58 +0300755 switch (usb_endpoint_type(desc)) {
756 case USB_ENDPOINT_XFER_CONTROL:
Anton Tikhomirov27a78d62012-02-23 15:38:46 +0900757 strlcat(dep->name, "-control", sizeof(dep->name));
Felipe Balbi72246da2011-08-19 18:10:58 +0300758 break;
759 case USB_ENDPOINT_XFER_ISOC:
Anton Tikhomirov27a78d62012-02-23 15:38:46 +0900760 strlcat(dep->name, "-isoc", sizeof(dep->name));
Felipe Balbi72246da2011-08-19 18:10:58 +0300761 break;
762 case USB_ENDPOINT_XFER_BULK:
Anton Tikhomirov27a78d62012-02-23 15:38:46 +0900763 strlcat(dep->name, "-bulk", sizeof(dep->name));
Felipe Balbi72246da2011-08-19 18:10:58 +0300764 break;
765 case USB_ENDPOINT_XFER_INT:
Anton Tikhomirov27a78d62012-02-23 15:38:46 +0900766 strlcat(dep->name, "-int", sizeof(dep->name));
Felipe Balbi72246da2011-08-19 18:10:58 +0300767 break;
768 default:
769 dev_err(dwc->dev, "invalid endpoint transfer type\n");
770 }
771
Felipe Balbi72246da2011-08-19 18:10:58 +0300772 dev_vdbg(dwc->dev, "Enabling %s\n", dep->name);
773
774 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi07e0ee82012-07-16 14:08:16 +0300775 ret = __dwc3_gadget_ep_enable(dep, desc, ep->comp_desc, false);
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +0530776 dbg_event(dep->number, "ENABLE", ret);
Felipe Balbi72246da2011-08-19 18:10:58 +0300777 spin_unlock_irqrestore(&dwc->lock, flags);
778
779 return ret;
780}
781
782static int dwc3_gadget_ep_disable(struct usb_ep *ep)
783{
784 struct dwc3_ep *dep;
785 struct dwc3 *dwc;
786 unsigned long flags;
787 int ret;
788
789 if (!ep) {
790 pr_debug("dwc3: invalid parameters\n");
791 return -EINVAL;
792 }
793
794 dep = to_dwc3_ep(ep);
795 dwc = dep->dwc;
796
797 if (!(dep->flags & DWC3_EP_ENABLED)) {
798 dev_WARN_ONCE(dwc->dev, true, "%s is already disabled\n",
799 dep->name);
800 return 0;
801 }
802
803 snprintf(dep->name, sizeof(dep->name), "ep%d%s",
804 dep->number >> 1,
805 (dep->number & 1) ? "in" : "out");
806
807 spin_lock_irqsave(&dwc->lock, flags);
808 ret = __dwc3_gadget_ep_disable(dep);
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +0530809 dbg_event(dep->number, "DISABLE", ret);
Felipe Balbi72246da2011-08-19 18:10:58 +0300810 spin_unlock_irqrestore(&dwc->lock, flags);
811
812 return ret;
813}
814
815static struct usb_request *dwc3_gadget_ep_alloc_request(struct usb_ep *ep,
816 gfp_t gfp_flags)
817{
818 struct dwc3_request *req;
819 struct dwc3_ep *dep = to_dwc3_ep(ep);
820 struct dwc3 *dwc = dep->dwc;
821
822 req = kzalloc(sizeof(*req), gfp_flags);
823 if (!req) {
824 dev_err(dwc->dev, "not enough memory\n");
825 return NULL;
826 }
827
828 req->epnum = dep->number;
829 req->dep = dep;
Felipe Balbi72246da2011-08-19 18:10:58 +0300830
831 return &req->request;
832}
833
834static void dwc3_gadget_ep_free_request(struct usb_ep *ep,
835 struct usb_request *request)
836{
837 struct dwc3_request *req = to_dwc3_request(request);
838
839 kfree(req);
840}
841
Felipe Balbic71fc372011-11-22 11:37:34 +0200842/**
843 * dwc3_prepare_one_trb - setup one TRB from one request
844 * @dep: endpoint for which this request is prepared
845 * @req: dwc3_request pointer
846 */
Felipe Balbi68e823e2011-11-28 12:25:01 +0200847static void dwc3_prepare_one_trb(struct dwc3_ep *dep,
Felipe Balbieeb720f2011-11-28 12:46:59 +0200848 struct dwc3_request *req, dma_addr_t dma,
849 unsigned length, unsigned last, unsigned chain)
Felipe Balbic71fc372011-11-22 11:37:34 +0200850{
Felipe Balbieeb720f2011-11-28 12:46:59 +0200851 struct dwc3 *dwc = dep->dwc;
Felipe Balbif6bafc62012-02-06 11:04:53 +0200852 struct dwc3_trb *trb;
Felipe Balbic71fc372011-11-22 11:37:34 +0200853
854 unsigned int cur_slot;
855
Felipe Balbieeb720f2011-11-28 12:46:59 +0200856 dev_vdbg(dwc->dev, "%s: req %p dma %08llx length %d%s%s\n",
857 dep->name, req, (unsigned long long) dma,
858 length, last ? " last" : "",
859 chain ? " chain" : "");
860
Felipe Balbif6bafc62012-02-06 11:04:53 +0200861 trb = &dep->trb_pool[dep->free_slot & DWC3_TRB_MASK];
Felipe Balbic71fc372011-11-22 11:37:34 +0200862 cur_slot = dep->free_slot;
863 dep->free_slot++;
864
865 /* Skip the LINK-TRB on ISOC */
Vijayavardhan Vennapusa2a444ad2013-02-01 13:20:59 +0530866 if (((dep->free_slot & DWC3_TRB_MASK) == DWC3_TRB_NUM - 1) &&
Ido Shayevitz57cdac12012-03-12 20:25:24 +0200867 usb_endpoint_xfer_isoc(dep->endpoint.desc))
Vijayavardhan Vennapusa2a444ad2013-02-01 13:20:59 +0530868 dep->free_slot++;
Felipe Balbic71fc372011-11-22 11:37:34 +0200869
Felipe Balbieeb720f2011-11-28 12:46:59 +0200870 if (!req->trb) {
871 dwc3_gadget_move_request_queued(req);
Felipe Balbif6bafc62012-02-06 11:04:53 +0200872 req->trb = trb;
873 req->trb_dma = dwc3_trb_dma_offset(dep, trb);
Felipe Balbieeb720f2011-11-28 12:46:59 +0200874 }
Felipe Balbic71fc372011-11-22 11:37:34 +0200875
Vijayavardhan Vennapusa5f5c3e62013-11-13 19:53:13 +0530876update_trb:
Felipe Balbif6bafc62012-02-06 11:04:53 +0200877 trb->size = DWC3_TRB_SIZE_LENGTH(length);
878 trb->bpl = lower_32_bits(dma);
879 trb->bph = upper_32_bits(dma);
Felipe Balbic71fc372011-11-22 11:37:34 +0200880
Ido Shayevitz57cdac12012-03-12 20:25:24 +0200881 switch (usb_endpoint_type(dep->endpoint.desc)) {
Felipe Balbic71fc372011-11-22 11:37:34 +0200882 case USB_ENDPOINT_XFER_CONTROL:
Felipe Balbif6bafc62012-02-06 11:04:53 +0200883 trb->ctrl = DWC3_TRBCTL_CONTROL_SETUP;
Felipe Balbic71fc372011-11-22 11:37:34 +0200884 break;
885
886 case USB_ENDPOINT_XFER_ISOC:
Felipe Balbif6bafc62012-02-06 11:04:53 +0200887 trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS_FIRST;
Felipe Balbic71fc372011-11-22 11:37:34 +0200888
Pratyush Ananddf023422012-05-21 12:42:54 +0530889 if (!req->request.no_interrupt)
Felipe Balbif6bafc62012-02-06 11:04:53 +0200890 trb->ctrl |= DWC3_TRB_CTRL_IOC;
Felipe Balbic71fc372011-11-22 11:37:34 +0200891 break;
892
893 case USB_ENDPOINT_XFER_BULK:
894 case USB_ENDPOINT_XFER_INT:
Felipe Balbif6bafc62012-02-06 11:04:53 +0200895 trb->ctrl = DWC3_TRBCTL_NORMAL;
Felipe Balbic71fc372011-11-22 11:37:34 +0200896 break;
897 default:
898 /*
899 * This is only possible with faulty memory because we
900 * checked it already :)
901 */
902 BUG();
903 }
904
Ido Shayevitz57cdac12012-03-12 20:25:24 +0200905 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbif6bafc62012-02-06 11:04:53 +0200906 trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI;
907 trb->ctrl |= DWC3_TRB_CTRL_CSP;
908 } else {
909 if (chain)
910 trb->ctrl |= DWC3_TRB_CTRL_CHN;
Felipe Balbif6bafc62012-02-06 11:04:53 +0200911 }
912
Ido Shayevitz57cdac12012-03-12 20:25:24 +0200913 if (usb_endpoint_xfer_bulk(dep->endpoint.desc) && dep->stream_capable)
Felipe Balbif6bafc62012-02-06 11:04:53 +0200914 trb->ctrl |= DWC3_TRB_CTRL_SID_SOFN(req->request.stream_id);
915
916 trb->ctrl |= DWC3_TRB_CTRL_HWO;
Vijayavardhan Vennapusa5f5c3e62013-11-13 19:53:13 +0530917
918 if (req->request.zero && length &&
919 (length % usb_endpoint_maxp(dep->endpoint.desc) == 0)) {
920 trb = &dep->trb_pool[dep->free_slot & DWC3_TRB_MASK];
921 dep->free_slot++;
922
923 /* Skip the LINK-TRB on ISOC */
924 if (((dep->free_slot & DWC3_TRB_MASK) == DWC3_TRB_NUM - 1) &&
925 usb_endpoint_xfer_isoc(dep->endpoint.desc))
926 dep->free_slot++;
927
928 req->ztrb = trb;
929 length = 0;
930
931 goto update_trb;
932 }
933
934 if (!usb_endpoint_xfer_isoc(dep->endpoint.desc) && last)
935 trb->ctrl |= DWC3_TRB_CTRL_LST;
Felipe Balbic71fc372011-11-22 11:37:34 +0200936}
937
Felipe Balbi72246da2011-08-19 18:10:58 +0300938/*
939 * dwc3_prepare_trbs - setup TRBs from requests
940 * @dep: endpoint for which requests are being prepared
941 * @starting: true if the endpoint is idle and no requests are queued.
942 *
Paul Zimmerman1d046792012-02-15 18:56:56 -0800943 * The function goes through the requests list and sets up TRBs for the
944 * transfers. The function returns once there are no more TRBs available or
945 * it runs out of requests.
Felipe Balbi72246da2011-08-19 18:10:58 +0300946 */
Felipe Balbi68e823e2011-11-28 12:25:01 +0200947static void dwc3_prepare_trbs(struct dwc3_ep *dep, bool starting)
Felipe Balbi72246da2011-08-19 18:10:58 +0300948{
Felipe Balbi68e823e2011-11-28 12:25:01 +0200949 struct dwc3_request *req, *n;
Felipe Balbi72246da2011-08-19 18:10:58 +0300950 u32 trbs_left;
Paul Zimmerman8d62cd62012-02-15 13:35:06 +0200951 u32 max;
Felipe Balbic71fc372011-11-22 11:37:34 +0200952 unsigned int last_one = 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300953
954 BUILD_BUG_ON_NOT_POWER_OF_2(DWC3_TRB_NUM);
955
956 /* the first request must not be queued */
957 trbs_left = (dep->busy_slot - dep->free_slot) & DWC3_TRB_MASK;
Felipe Balbic71fc372011-11-22 11:37:34 +0200958
Paul Zimmerman8d62cd62012-02-15 13:35:06 +0200959 /* Can't wrap around on a non-isoc EP since there's no link TRB */
Ido Shayevitz57cdac12012-03-12 20:25:24 +0200960 if (!usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Paul Zimmerman8d62cd62012-02-15 13:35:06 +0200961 max = DWC3_TRB_NUM - (dep->free_slot & DWC3_TRB_MASK);
962 if (trbs_left > max)
963 trbs_left = max;
964 }
965
Felipe Balbi72246da2011-08-19 18:10:58 +0300966 /*
Paul Zimmerman1d046792012-02-15 18:56:56 -0800967 * If busy & slot are equal than it is either full or empty. If we are
968 * starting to process requests then we are empty. Otherwise we are
Felipe Balbi72246da2011-08-19 18:10:58 +0300969 * full and don't do anything
970 */
971 if (!trbs_left) {
972 if (!starting)
Felipe Balbi68e823e2011-11-28 12:25:01 +0200973 return;
Felipe Balbi72246da2011-08-19 18:10:58 +0300974 trbs_left = DWC3_TRB_NUM;
975 /*
976 * In case we start from scratch, we queue the ISOC requests
977 * starting from slot 1. This is done because we use ring
978 * buffer and have no LST bit to stop us. Instead, we place
Paul Zimmerman1d046792012-02-15 18:56:56 -0800979 * IOC bit every TRB_NUM/4. We try to avoid having an interrupt
Felipe Balbi72246da2011-08-19 18:10:58 +0300980 * after the first request so we start at slot 1 and have
981 * 7 requests proceed before we hit the first IOC.
982 * Other transfer types don't use the ring buffer and are
983 * processed from the first TRB until the last one. Since we
984 * don't wrap around we have to start at the beginning.
985 */
Ido Shayevitz57cdac12012-03-12 20:25:24 +0200986 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbi72246da2011-08-19 18:10:58 +0300987 dep->busy_slot = 1;
988 dep->free_slot = 1;
989 } else {
990 dep->busy_slot = 0;
991 dep->free_slot = 0;
992 }
993 }
994
995 /* The last TRB is a link TRB, not used for xfer */
Ido Shayevitz57cdac12012-03-12 20:25:24 +0200996 if ((trbs_left <= 1) && usb_endpoint_xfer_isoc(dep->endpoint.desc))
Felipe Balbi68e823e2011-11-28 12:25:01 +0200997 return;
Felipe Balbi72246da2011-08-19 18:10:58 +0300998
999 list_for_each_entry_safe(req, n, &dep->request_list, list) {
Felipe Balbieeb720f2011-11-28 12:46:59 +02001000 unsigned length;
1001 dma_addr_t dma;
Felipe Balbi72246da2011-08-19 18:10:58 +03001002
Felipe Balbieeb720f2011-11-28 12:46:59 +02001003 if (req->request.num_mapped_sgs > 0) {
1004 struct usb_request *request = &req->request;
1005 struct scatterlist *sg = request->sg;
1006 struct scatterlist *s;
1007 int i;
Felipe Balbi72246da2011-08-19 18:10:58 +03001008
Felipe Balbieeb720f2011-11-28 12:46:59 +02001009 for_each_sg(sg, s, request->num_mapped_sgs, i) {
1010 unsigned chain = true;
Felipe Balbi72246da2011-08-19 18:10:58 +03001011
Felipe Balbieeb720f2011-11-28 12:46:59 +02001012 length = sg_dma_len(s);
1013 dma = sg_dma_address(s);
Felipe Balbi72246da2011-08-19 18:10:58 +03001014
Paul Zimmerman1d046792012-02-15 18:56:56 -08001015 if (i == (request->num_mapped_sgs - 1) ||
1016 sg_is_last(s)) {
Felipe Balbieeb720f2011-11-28 12:46:59 +02001017 last_one = true;
1018 chain = false;
1019 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001020
Felipe Balbieeb720f2011-11-28 12:46:59 +02001021 trbs_left--;
1022 if (!trbs_left)
1023 last_one = true;
Felipe Balbi72246da2011-08-19 18:10:58 +03001024
Felipe Balbieeb720f2011-11-28 12:46:59 +02001025 if (last_one)
1026 chain = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03001027
Felipe Balbieeb720f2011-11-28 12:46:59 +02001028 dwc3_prepare_one_trb(dep, req, dma, length,
1029 last_one, chain);
Felipe Balbi72246da2011-08-19 18:10:58 +03001030
Felipe Balbieeb720f2011-11-28 12:46:59 +02001031 if (last_one)
1032 break;
1033 }
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +05301034 dbg_queue(dep->number, &req->request, 0);
Felipe Balbi72246da2011-08-19 18:10:58 +03001035 } else {
Vijayavardhan Vennapusa5f5c3e62013-11-13 19:53:13 +05301036 struct dwc3_request *req1;
1037 int maxpkt_size = usb_endpoint_maxp(dep->endpoint.desc);
1038
Felipe Balbieeb720f2011-11-28 12:46:59 +02001039 dma = req->request.dma;
1040 length = req->request.length;
1041 trbs_left--;
1042
Vijayavardhan Vennapusa5f5c3e62013-11-13 19:53:13 +05301043 if (req->request.zero && length &&
1044 (length % maxpkt_size == 0))
1045 trbs_left--;
1046
1047 if (!trbs_left) {
Felipe Balbieeb720f2011-11-28 12:46:59 +02001048 last_one = 1;
Vijayavardhan Vennapusa5f5c3e62013-11-13 19:53:13 +05301049 } else if (dep->direction && (trbs_left <= 1)) {
1050 req1 = next_request(&req->list);
1051 if (req1->request.zero && req1->request.length
1052 && (req1->request.length % maxpkt_size == 0))
1053 last_one = 1;
1054 }
Felipe Balbieeb720f2011-11-28 12:46:59 +02001055
1056 /* Is this the last request? */
1057 if (list_is_last(&req->list, &dep->request_list))
1058 last_one = 1;
1059
1060 dwc3_prepare_one_trb(dep, req, dma, length,
1061 last_one, false);
1062
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +05301063 dbg_queue(dep->number, &req->request, 0);
Felipe Balbieeb720f2011-11-28 12:46:59 +02001064 if (last_one)
1065 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03001066 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001067 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001068}
1069
1070static int __dwc3_gadget_kick_transfer(struct dwc3_ep *dep, u16 cmd_param,
1071 int start_new)
1072{
1073 struct dwc3_gadget_ep_cmd_params params;
Vijayavardhan Vennapusa91ba6532013-01-30 17:35:45 +05301074 struct dwc3_request *req, *req1, *n;
Felipe Balbi72246da2011-08-19 18:10:58 +03001075 struct dwc3 *dwc = dep->dwc;
1076 int ret;
1077 u32 cmd;
1078
1079 if (start_new && (dep->flags & DWC3_EP_BUSY)) {
1080 dev_vdbg(dwc->dev, "%s: endpoint busy\n", dep->name);
1081 return -EBUSY;
1082 }
1083 dep->flags &= ~DWC3_EP_PENDING_REQUEST;
1084
1085 /*
1086 * If we are getting here after a short-out-packet we don't enqueue any
1087 * new requests as we try to set the IOC bit only on the last request.
1088 */
1089 if (start_new) {
1090 if (list_empty(&dep->req_queued))
1091 dwc3_prepare_trbs(dep, start_new);
1092
1093 /* req points to the first request which will be sent */
1094 req = next_request(&dep->req_queued);
1095 } else {
Felipe Balbi68e823e2011-11-28 12:25:01 +02001096 dwc3_prepare_trbs(dep, start_new);
1097
Felipe Balbi72246da2011-08-19 18:10:58 +03001098 /*
Paul Zimmerman1d046792012-02-15 18:56:56 -08001099 * req points to the first request where HWO changed from 0 to 1
Felipe Balbi72246da2011-08-19 18:10:58 +03001100 */
Felipe Balbi68e823e2011-11-28 12:25:01 +02001101 req = next_request(&dep->req_queued);
Felipe Balbi72246da2011-08-19 18:10:58 +03001102 }
1103 if (!req) {
1104 dep->flags |= DWC3_EP_PENDING_REQUEST;
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +05301105 dbg_event(dep->number, "NO REQ", 0);
Felipe Balbi72246da2011-08-19 18:10:58 +03001106 return 0;
1107 }
1108
1109 memset(&params, 0, sizeof(params));
Felipe Balbidc1c70a2011-09-30 10:58:51 +03001110 params.param0 = upper_32_bits(req->trb_dma);
1111 params.param1 = lower_32_bits(req->trb_dma);
Felipe Balbi72246da2011-08-19 18:10:58 +03001112
1113 if (start_new)
1114 cmd = DWC3_DEPCMD_STARTTRANSFER;
1115 else
1116 cmd = DWC3_DEPCMD_UPDATETRANSFER;
1117
1118 cmd |= DWC3_DEPCMD_PARAM(cmd_param);
1119 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number, cmd, &params);
1120 if (ret < 0) {
1121 dev_dbg(dwc->dev, "failed to send STARTTRANSFER command\n");
1122
Vijayavardhan Vennapusa91ba6532013-01-30 17:35:45 +05301123 if ((ret == -EAGAIN) && start_new &&
1124 usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
1125 /* If bit13 in Command complete event is set, software
1126 * must issue ENDTRANDFER command and wait for
1127 * Xfernotready event to queue the requests again.
1128 */
1129 if (!dep->resource_index) {
1130 dep->resource_index =
1131 dwc3_gadget_ep_get_transfer_index(dwc,
1132 dep->number);
1133 WARN_ON_ONCE(!dep->resource_index);
1134 }
1135 dwc3_stop_active_transfer(dwc, dep->number);
1136 list_for_each_entry_safe_reverse(req1, n,
1137 &dep->req_queued, list) {
1138 req1->trb = NULL;
1139 dwc3_gadget_move_request_list_front(req1);
1140 if (req->request.num_mapped_sgs)
1141 dep->busy_slot +=
1142 req->request.num_mapped_sgs;
1143 else
1144 dep->busy_slot++;
1145 if ((dep->busy_slot & DWC3_TRB_MASK) ==
1146 DWC3_TRB_NUM - 1)
1147 dep->busy_slot++;
1148 }
1149 return ret;
1150 } else {
1151 /*
1152 * FIXME we need to iterate over the list of requests
1153 * here and stop, unmap, free and del each of the linked
1154 * requests instead of what we do now.
1155 */
1156 usb_gadget_unmap_request(&dwc->gadget, &req->request,
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02001157 req->direction);
Vijayavardhan Vennapusa91ba6532013-01-30 17:35:45 +05301158 list_del(&req->list);
1159 return ret;
1160 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001161 }
1162
1163 dep->flags |= DWC3_EP_BUSY;
Felipe Balbi25b8ff62011-11-04 12:32:47 +02001164
Paul Zimmermanf39a37f2012-03-29 18:16:54 +00001165 if (start_new) {
Felipe Balbi4959cfc2012-06-06 12:04:13 +03001166 dep->resource_index = dwc3_gadget_ep_get_transfer_index(dwc,
Paul Zimmermanf39a37f2012-03-29 18:16:54 +00001167 dep->number);
Felipe Balbi4959cfc2012-06-06 12:04:13 +03001168 WARN_ON_ONCE(!dep->resource_index);
Paul Zimmermanf39a37f2012-03-29 18:16:54 +00001169 }
Felipe Balbi25b8ff62011-11-04 12:32:47 +02001170
Felipe Balbi72246da2011-08-19 18:10:58 +03001171 return 0;
1172}
1173
Pratyush Anand73939b02012-05-25 18:54:56 +05301174static void __dwc3_gadget_start_isoc(struct dwc3 *dwc,
1175 struct dwc3_ep *dep, u32 cur_uf)
1176{
1177 u32 uf;
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +05301178 int ret;
Pratyush Anand73939b02012-05-25 18:54:56 +05301179
Vijayavardhan Vennapusa91ba6532013-01-30 17:35:45 +05301180 dep->current_uf = cur_uf;
1181
Pratyush Anand73939b02012-05-25 18:54:56 +05301182 if (list_empty(&dep->request_list)) {
1183 dev_vdbg(dwc->dev, "ISOC ep %s run out for requests.\n",
1184 dep->name);
Pratyush Anandac417602012-08-30 12:21:43 +05301185 dep->flags |= DWC3_EP_PENDING_REQUEST;
Pratyush Anand73939b02012-05-25 18:54:56 +05301186 return;
1187 }
1188
1189 /* 4 micro frames in the future */
1190 uf = cur_uf + dep->interval * 4;
1191
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +05301192 ret = __dwc3_gadget_kick_transfer(dep, uf, 1);
1193 if (ret < 0)
1194 dbg_event(dep->number, "QUEUE", ret);
Pratyush Anand73939b02012-05-25 18:54:56 +05301195}
1196
1197static void dwc3_gadget_start_isoc(struct dwc3 *dwc,
1198 struct dwc3_ep *dep, const struct dwc3_event_depevt *event)
1199{
1200 u32 cur_uf, mask;
1201
1202 mask = ~(dep->interval - 1);
1203 cur_uf = event->parameters & mask;
1204
1205 __dwc3_gadget_start_isoc(dwc, dep, cur_uf);
1206}
1207
Felipe Balbi72246da2011-08-19 18:10:58 +03001208static int __dwc3_gadget_ep_queue(struct dwc3_ep *dep, struct dwc3_request *req)
1209{
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02001210 struct dwc3 *dwc = dep->dwc;
1211 int ret;
1212
Manu Gautamd2b99e12013-02-11 15:53:34 +05301213 if (req->request.status == -EINPROGRESS) {
1214 ret = -EBUSY;
1215 dev_err(dwc->dev, "%s: %p request already in queue",
1216 dep->name, req);
1217 return ret;
1218 }
1219
Felipe Balbi72246da2011-08-19 18:10:58 +03001220 req->request.actual = 0;
1221 req->request.status = -EINPROGRESS;
1222 req->direction = dep->direction;
1223 req->epnum = dep->number;
1224
1225 /*
1226 * We only add to our list of requests now and
1227 * start consuming the list once we get XferNotReady
1228 * IRQ.
1229 *
1230 * That way, we avoid doing anything that we don't need
1231 * to do now and defer it until the point we receive a
1232 * particular token from the Host side.
1233 *
1234 * This will also avoid Host cancelling URBs due to too
Paul Zimmerman1d046792012-02-15 18:56:56 -08001235 * many NAKs.
Felipe Balbi72246da2011-08-19 18:10:58 +03001236 */
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02001237 ret = usb_gadget_map_request(&dwc->gadget, &req->request,
1238 dep->direction);
1239 if (ret)
1240 return ret;
1241
Felipe Balbi72246da2011-08-19 18:10:58 +03001242 list_add_tail(&req->list, &dep->request_list);
1243
1244 /*
Felipe Balbi46485a02012-06-06 12:00:50 +03001245 * There are a few special cases:
Felipe Balbi72246da2011-08-19 18:10:58 +03001246 *
Paul Zimmermanf39a37f2012-03-29 18:16:54 +00001247 * 1. XferNotReady with empty list of requests. We need to kick the
1248 * transfer here in that situation, otherwise we will be NAKing
1249 * forever. If we get XferNotReady before gadget driver has a
1250 * chance to queue a request, we will ACK the IRQ but won't be
1251 * able to receive the data until the next request is queued.
1252 * The following code is handling exactly that.
1253 *
Felipe Balbi72246da2011-08-19 18:10:58 +03001254 */
1255 if (dep->flags & DWC3_EP_PENDING_REQUEST) {
Pratyush Anandac417602012-08-30 12:21:43 +05301256 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001257
Pratyush Anandac417602012-08-30 12:21:43 +05301258 /*
1259 * If xfernotready is already elapsed and it is a case
1260 * of isoc transfer, then issue END TRANSFER, so that
1261 * you can receive xfernotready again and can have
1262 * notion of current microframe.
1263 */
1264 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Vijayavardhan Vennapusa91ba6532013-01-30 17:35:45 +05301265 /* If xfernotready event is recieved before issuing
1266 * START TRANSFER command, don't issue END TRANSFER.
1267 * Rather start queueing the requests by issuing START
1268 * TRANSFER command.
1269 */
1270 if (list_empty(&dep->req_queued) && dep->resource_index)
Pratyush Anand18bbcb02013-01-14 15:59:34 +05301271 dwc3_stop_active_transfer(dwc, dep->number);
Vijayavardhan Vennapusa91ba6532013-01-30 17:35:45 +05301272 else
1273 __dwc3_gadget_start_isoc(dwc, dep,
1274 dep->current_uf);
1275 dep->flags &= ~DWC3_EP_PENDING_REQUEST;
Pratyush Anandac417602012-08-30 12:21:43 +05301276 return 0;
1277 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001278
Felipe Balbi46485a02012-06-06 12:00:50 +03001279 ret = __dwc3_gadget_kick_transfer(dep, 0, true);
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +05301280 if (ret && ret != -EBUSY) {
1281 dbg_event(dep->number, "QUEUE", ret);
Felipe Balbi72246da2011-08-19 18:10:58 +03001282 dev_dbg(dwc->dev, "%s: failed to kick transfers\n",
1283 dep->name);
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +05301284 }
Felipe Balbi5d409eb2012-05-22 10:24:11 +03001285 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001286
Felipe Balbi46485a02012-06-06 12:00:50 +03001287 /*
1288 * 2. XferInProgress on Isoc EP with an active transfer. We need to
1289 * kick the transfer here after queuing a request, otherwise the
1290 * core may not see the modified TRB(s).
1291 */
1292 if (usb_endpoint_xfer_isoc(dep->endpoint.desc) &&
Pratyush Anand053d3e52012-08-07 16:54:18 +05301293 (dep->flags & DWC3_EP_BUSY) &&
1294 !(dep->flags & DWC3_EP_MISSED_ISOC)) {
Felipe Balbi4959cfc2012-06-06 12:04:13 +03001295 WARN_ON_ONCE(!dep->resource_index);
1296 ret = __dwc3_gadget_kick_transfer(dep, dep->resource_index,
Felipe Balbi46485a02012-06-06 12:00:50 +03001297 false);
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +05301298 if (ret && ret != -EBUSY) {
1299 dbg_event(dep->number, "QUEUE", ret);
Felipe Balbi46485a02012-06-06 12:00:50 +03001300 dev_dbg(dwc->dev, "%s: failed to kick transfers\n",
1301 dep->name);
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +05301302 }
Felipe Balbi46485a02012-06-06 12:00:50 +03001303 }
1304
Felipe Balbi72246da2011-08-19 18:10:58 +03001305 return 0;
1306}
1307
1308static int dwc3_gadget_ep_queue(struct usb_ep *ep, struct usb_request *request,
1309 gfp_t gfp_flags)
1310{
1311 struct dwc3_request *req = to_dwc3_request(request);
1312 struct dwc3_ep *dep = to_dwc3_ep(ep);
1313 struct dwc3 *dwc = dep->dwc;
1314
1315 unsigned long flags;
1316
1317 int ret;
1318
Manu Gautam22f93042013-02-20 15:12:02 +05301319 spin_lock_irqsave(&dwc->lock, flags);
1320
Ido Shayevitz57cdac12012-03-12 20:25:24 +02001321 if (!dep->endpoint.desc) {
Manu Gautam22f93042013-02-20 15:12:02 +05301322 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001323 dev_dbg(dwc->dev, "trying to queue request %p to disabled %s\n",
1324 request, ep->name);
1325 return -ESHUTDOWN;
1326 }
1327
1328 dev_vdbg(dwc->dev, "queing request %p to %s length %d\n",
1329 request, ep->name, request->length);
1330
Manu Gautam1c4dbcb2012-10-05 13:16:00 +05301331 WARN(!dep->direction && (request->length % ep->desc->wMaxPacketSize),
1332 "trying to queue unaligned request (%d)\n", request->length);
1333
Felipe Balbi72246da2011-08-19 18:10:58 +03001334 ret = __dwc3_gadget_ep_queue(dep, req);
1335 spin_unlock_irqrestore(&dwc->lock, flags);
1336
1337 return ret;
1338}
1339
1340static int dwc3_gadget_ep_dequeue(struct usb_ep *ep,
1341 struct usb_request *request)
1342{
1343 struct dwc3_request *req = to_dwc3_request(request);
1344 struct dwc3_request *r = NULL;
1345
1346 struct dwc3_ep *dep = to_dwc3_ep(ep);
1347 struct dwc3 *dwc = dep->dwc;
1348
1349 unsigned long flags;
1350 int ret = 0;
1351
1352 spin_lock_irqsave(&dwc->lock, flags);
1353
1354 list_for_each_entry(r, &dep->request_list, list) {
1355 if (r == req)
1356 break;
1357 }
1358
1359 if (r != req) {
1360 list_for_each_entry(r, &dep->req_queued, list) {
1361 if (r == req)
1362 break;
1363 }
1364 if (r == req) {
1365 /* wait until it is processed */
1366 dwc3_stop_active_transfer(dwc, dep->number);
Pratyush Anandeaec3e92012-06-15 11:54:00 +05301367 goto out1;
Felipe Balbi72246da2011-08-19 18:10:58 +03001368 }
1369 dev_err(dwc->dev, "request %p was not queued to %s\n",
1370 request, ep->name);
1371 ret = -EINVAL;
1372 goto out0;
1373 }
1374
Pratyush Anandeaec3e92012-06-15 11:54:00 +05301375out1:
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +05301376 dbg_event(dep->number, "DEQUEUE", 0);
Felipe Balbi72246da2011-08-19 18:10:58 +03001377 /* giveback the request */
1378 dwc3_gadget_giveback(dep, req, -ECONNRESET);
1379
1380out0:
1381 spin_unlock_irqrestore(&dwc->lock, flags);
1382
1383 return ret;
1384}
1385
1386int __dwc3_gadget_ep_set_halt(struct dwc3_ep *dep, int value)
1387{
1388 struct dwc3_gadget_ep_cmd_params params;
1389 struct dwc3 *dwc = dep->dwc;
1390 int ret;
1391
1392 memset(&params, 0x00, sizeof(params));
1393
1394 if (value) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001395 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number,
1396 DWC3_DEPCMD_SETSTALL, &params);
1397 if (ret)
1398 dev_err(dwc->dev, "failed to %s STALL on %s\n",
1399 value ? "set" : "clear",
1400 dep->name);
1401 else
1402 dep->flags |= DWC3_EP_STALL;
1403 } else {
1404 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number,
1405 DWC3_DEPCMD_CLEARSTALL, &params);
1406 if (ret)
1407 dev_err(dwc->dev, "failed to %s STALL on %s\n",
1408 value ? "set" : "clear",
1409 dep->name);
1410 else
Vijayavardhan Vennapusa6008e262012-10-19 15:57:56 +05301411 dep->flags &= ~(DWC3_EP_STALL | DWC3_EP_WEDGE);
Felipe Balbi72246da2011-08-19 18:10:58 +03001412 }
Paul Zimmerman52754552011-09-30 10:58:44 +03001413
Felipe Balbi72246da2011-08-19 18:10:58 +03001414 return ret;
1415}
1416
1417static int dwc3_gadget_ep_set_halt(struct usb_ep *ep, int value)
1418{
1419 struct dwc3_ep *dep = to_dwc3_ep(ep);
1420 struct dwc3 *dwc = dep->dwc;
1421
1422 unsigned long flags;
1423
1424 int ret;
1425
1426 spin_lock_irqsave(&dwc->lock, flags);
1427
Ido Shayevitz57cdac12012-03-12 20:25:24 +02001428 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001429 dev_err(dwc->dev, "%s is of Isochronous type\n", dep->name);
1430 ret = -EINVAL;
1431 goto out;
1432 }
1433
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +05301434 dbg_event(dep->number, "HALT", value);
Felipe Balbi72246da2011-08-19 18:10:58 +03001435 ret = __dwc3_gadget_ep_set_halt(dep, value);
1436out:
1437 spin_unlock_irqrestore(&dwc->lock, flags);
1438
1439 return ret;
1440}
1441
1442static int dwc3_gadget_ep_set_wedge(struct usb_ep *ep)
1443{
1444 struct dwc3_ep *dep = to_dwc3_ep(ep);
Paul Zimmerman249a4562012-02-24 17:32:16 -08001445 struct dwc3 *dwc = dep->dwc;
1446 unsigned long flags;
Felipe Balbi72246da2011-08-19 18:10:58 +03001447
Paul Zimmerman249a4562012-02-24 17:32:16 -08001448 spin_lock_irqsave(&dwc->lock, flags);
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +05301449 dbg_event(dep->number, "WEDGE", 0);
Felipe Balbi72246da2011-08-19 18:10:58 +03001450 dep->flags |= DWC3_EP_WEDGE;
Paul Zimmerman249a4562012-02-24 17:32:16 -08001451 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001452
Pratyush Anandeb840752012-06-25 22:40:43 +05301453 if (dep->number == 0 || dep->number == 1)
1454 return dwc3_gadget_ep0_set_halt(ep, 1);
1455 else
1456 return dwc3_gadget_ep_set_halt(ep, 1);
Felipe Balbi72246da2011-08-19 18:10:58 +03001457}
1458
1459/* -------------------------------------------------------------------------- */
1460
1461static struct usb_endpoint_descriptor dwc3_gadget_ep0_desc = {
1462 .bLength = USB_DT_ENDPOINT_SIZE,
1463 .bDescriptorType = USB_DT_ENDPOINT,
1464 .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
1465};
1466
1467static const struct usb_ep_ops dwc3_gadget_ep0_ops = {
1468 .enable = dwc3_gadget_ep0_enable,
1469 .disable = dwc3_gadget_ep0_disable,
1470 .alloc_request = dwc3_gadget_ep_alloc_request,
1471 .free_request = dwc3_gadget_ep_free_request,
1472 .queue = dwc3_gadget_ep0_queue,
1473 .dequeue = dwc3_gadget_ep_dequeue,
Pratyush Anandeb840752012-06-25 22:40:43 +05301474 .set_halt = dwc3_gadget_ep0_set_halt,
Felipe Balbi72246da2011-08-19 18:10:58 +03001475 .set_wedge = dwc3_gadget_ep_set_wedge,
1476};
1477
1478static const struct usb_ep_ops dwc3_gadget_ep_ops = {
1479 .enable = dwc3_gadget_ep_enable,
1480 .disable = dwc3_gadget_ep_disable,
1481 .alloc_request = dwc3_gadget_ep_alloc_request,
1482 .free_request = dwc3_gadget_ep_free_request,
1483 .queue = dwc3_gadget_ep_queue,
1484 .dequeue = dwc3_gadget_ep_dequeue,
1485 .set_halt = dwc3_gadget_ep_set_halt,
1486 .set_wedge = dwc3_gadget_ep_set_wedge,
1487};
1488
1489/* -------------------------------------------------------------------------- */
1490
1491static int dwc3_gadget_get_frame(struct usb_gadget *g)
1492{
1493 struct dwc3 *dwc = gadget_to_dwc(g);
1494 u32 reg;
1495
1496 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1497 return DWC3_DSTS_SOFFN(reg);
1498}
1499
1500static int dwc3_gadget_wakeup(struct usb_gadget *g)
1501{
1502 struct dwc3 *dwc = gadget_to_dwc(g);
1503
1504 unsigned long timeout;
1505 unsigned long flags;
1506
1507 u32 reg;
1508
1509 int ret = 0;
1510
1511 u8 link_state;
1512 u8 speed;
1513
1514 spin_lock_irqsave(&dwc->lock, flags);
1515
1516 /*
1517 * According to the Databook Remote wakeup request should
1518 * be issued only when the device is in early suspend state.
1519 *
1520 * We can check that via USB Link State bits in DSTS register.
1521 */
1522 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1523
1524 speed = reg & DWC3_DSTS_CONNECTSPD;
1525 if (speed == DWC3_DSTS_SUPERSPEED) {
1526 dev_dbg(dwc->dev, "no wakeup on SuperSpeed\n");
1527 ret = -EINVAL;
1528 goto out;
1529 }
1530
1531 link_state = DWC3_DSTS_USBLNKST(reg);
1532
1533 switch (link_state) {
1534 case DWC3_LINK_STATE_RX_DET: /* in HS, means Early Suspend */
1535 case DWC3_LINK_STATE_U3: /* in HS, means SUSPEND */
1536 break;
1537 default:
1538 dev_dbg(dwc->dev, "can't wakeup from link state %d\n",
1539 link_state);
1540 ret = -EINVAL;
1541 goto out;
1542 }
1543
Felipe Balbi8598bde2012-01-02 18:55:57 +02001544 ret = dwc3_gadget_set_link_state(dwc, DWC3_LINK_STATE_RECOV);
1545 if (ret < 0) {
1546 dev_err(dwc->dev, "failed to put link in Recovery\n");
1547 goto out;
1548 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001549
Paul Zimmerman88df4272012-04-27 13:10:52 +03001550 /* Recent versions do this automatically */
1551 if (dwc->revision < DWC3_REVISION_194A) {
1552 /* write zeroes to Link Change Request */
Felipe Balbib4d04352012-05-24 10:27:56 +03001553 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
Paul Zimmerman88df4272012-04-27 13:10:52 +03001554 reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK;
1555 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1556 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001557
Paul Zimmerman1d046792012-02-15 18:56:56 -08001558 /* poll until Link State changes to ON */
Felipe Balbi72246da2011-08-19 18:10:58 +03001559 timeout = jiffies + msecs_to_jiffies(100);
1560
Paul Zimmerman1d046792012-02-15 18:56:56 -08001561 while (!time_after(jiffies, timeout)) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001562 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1563
1564 /* in HS, means ON */
1565 if (DWC3_DSTS_USBLNKST(reg) == DWC3_LINK_STATE_U0)
1566 break;
1567 }
1568
1569 if (DWC3_DSTS_USBLNKST(reg) != DWC3_LINK_STATE_U0) {
1570 dev_err(dwc->dev, "failed to send remote wakeup\n");
1571 ret = -EINVAL;
1572 }
1573
1574out:
1575 spin_unlock_irqrestore(&dwc->lock, flags);
1576
1577 return ret;
1578}
1579
1580static int dwc3_gadget_set_selfpowered(struct usb_gadget *g,
1581 int is_selfpowered)
1582{
1583 struct dwc3 *dwc = gadget_to_dwc(g);
Paul Zimmerman249a4562012-02-24 17:32:16 -08001584 unsigned long flags;
Felipe Balbi72246da2011-08-19 18:10:58 +03001585
Paul Zimmerman249a4562012-02-24 17:32:16 -08001586 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001587 dwc->is_selfpowered = !!is_selfpowered;
Paul Zimmerman249a4562012-02-24 17:32:16 -08001588 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001589
1590 return 0;
1591}
1592
Wesley Cheng446ad8d2013-06-05 16:15:01 +05301593#define DWC3_SOFT_RESET_TIMEOUT 10 /* 10 msec */
Pratyush Anand77473f72012-07-02 10:21:55 +05301594static int dwc3_gadget_run_stop(struct dwc3 *dwc, int is_on)
Felipe Balbi72246da2011-08-19 18:10:58 +03001595{
1596 u32 reg;
Sebastian Andrzej Siewior61d58242011-08-29 16:46:38 +02001597 u32 timeout = 500;
Wesley Cheng446ad8d2013-06-05 16:15:01 +05301598 ktime_t start, diff;
Felipe Balbi72246da2011-08-19 18:10:58 +03001599
1600 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
Felipe Balbi8db7ed12012-01-18 18:32:29 +02001601 if (is_on) {
Paul Zimmerman88df4272012-04-27 13:10:52 +03001602 if (dwc->revision <= DWC3_REVISION_187A) {
1603 reg &= ~DWC3_DCTL_TRGTULST_MASK;
1604 reg |= DWC3_DCTL_TRGTULST_RX_DET;
1605 }
1606
1607 if (dwc->revision >= DWC3_REVISION_194A)
1608 reg &= ~DWC3_DCTL_KEEP_CONNECT;
Wesley Cheng446ad8d2013-06-05 16:15:01 +05301609
1610 start = ktime_get();
1611 /* issue device SoftReset */
1612 dwc3_writel(dwc->regs, DWC3_DCTL, reg | DWC3_DCTL_CSFTRST);
1613 do {
1614 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
1615 if (!(reg & DWC3_DCTL_CSFTRST))
1616 break;
1617
1618 diff = ktime_sub(ktime_get(), start);
1619 /* poll for max. 10ms */
1620 if (ktime_to_ms(diff) > DWC3_SOFT_RESET_TIMEOUT) {
1621 printk_ratelimited(KERN_ERR
1622 "%s:core Reset Timed Out\n", __func__);
1623 break;
1624 }
1625 cpu_relax();
1626 } while (true);
1627
1628
1629 dwc3_event_buffers_setup(dwc);
1630 dwc3_gadget_restart(dwc);
1631 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
Paul Zimmerman88df4272012-04-27 13:10:52 +03001632 reg |= DWC3_DCTL_RUN_STOP;
Felipe Balbi8db7ed12012-01-18 18:32:29 +02001633 } else {
Felipe Balbi72246da2011-08-19 18:10:58 +03001634 reg &= ~DWC3_DCTL_RUN_STOP;
Felipe Balbi8db7ed12012-01-18 18:32:29 +02001635 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001636
1637 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1638
1639 do {
1640 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1641 if (is_on) {
1642 if (!(reg & DWC3_DSTS_DEVCTRLHLT))
1643 break;
1644 } else {
1645 if (reg & DWC3_DSTS_DEVCTRLHLT)
1646 break;
1647 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001648 timeout--;
1649 if (!timeout)
Pratyush Anand77473f72012-07-02 10:21:55 +05301650 return -ETIMEDOUT;
Sebastian Andrzej Siewior61d58242011-08-29 16:46:38 +02001651 udelay(1);
Felipe Balbi72246da2011-08-19 18:10:58 +03001652 } while (1);
1653
1654 dev_vdbg(dwc->dev, "gadget %s data soft-%s\n",
1655 dwc->gadget_driver
1656 ? dwc->gadget_driver->function : "no-function",
1657 is_on ? "connect" : "disconnect");
Pratyush Anand77473f72012-07-02 10:21:55 +05301658
1659 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +03001660}
1661
Vijayavardhan Vennapusa908f1ed2012-10-19 19:51:48 +05301662static int dwc3_gadget_vbus_draw(struct usb_gadget *g, unsigned mA)
1663{
1664 struct dwc3 *dwc = gadget_to_dwc(g);
1665 struct dwc3_otg *dotg = dwc->dotg;
1666
1667 if (dotg && dotg->otg.phy)
1668 return usb_phy_set_power(dotg->otg.phy, mA);
1669
1670 return -ENOTSUPP;
1671}
1672
Felipe Balbi72246da2011-08-19 18:10:58 +03001673static int dwc3_gadget_pullup(struct usb_gadget *g, int is_on)
1674{
1675 struct dwc3 *dwc = gadget_to_dwc(g);
1676 unsigned long flags;
Pratyush Anand77473f72012-07-02 10:21:55 +05301677 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001678
1679 is_on = !!is_on;
1680
1681 spin_lock_irqsave(&dwc->lock, flags);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +02001682
1683 dwc->softconnect = is_on;
1684
1685 if ((dwc->dotg && !dwc->vbus_active) ||
1686 !dwc->gadget_driver) {
1687
1688 spin_unlock_irqrestore(&dwc->lock, flags);
1689
1690 /*
1691 * Need to wait for vbus_session(on) from otg driver or to
1692 * the udc_start.
1693 */
1694 return 0;
1695 }
1696
Pratyush Anand77473f72012-07-02 10:21:55 +05301697 ret = dwc3_gadget_run_stop(dwc, is_on);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +02001698
1699 spin_unlock_irqrestore(&dwc->lock, flags);
1700
Pratyush Anand77473f72012-07-02 10:21:55 +05301701 return ret;
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +02001702}
1703
Jack Pham09e5c8e2013-03-06 18:53:43 -08001704static void dwc3_gadget_disconnect_interrupt(struct dwc3 *dwc);
1705
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +02001706static int dwc3_gadget_vbus_session(struct usb_gadget *_gadget, int is_active)
1707{
1708 struct dwc3 *dwc = gadget_to_dwc(_gadget);
1709 unsigned long flags;
Vijayavardhan Vennapusa8ec31d22012-10-23 08:44:48 +05301710 int ret = 0;
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +02001711
1712 if (!dwc->dotg)
1713 return -EPERM;
1714
1715 is_active = !!is_active;
1716
1717 spin_lock_irqsave(&dwc->lock, flags);
1718
1719 /* Mark that the vbus was powered */
1720 dwc->vbus_active = is_active;
1721
1722 /*
1723 * Check if upper level usb_gadget_driver was already registerd with
1724 * this udc controller driver (if dwc3_gadget_start was called)
1725 */
1726 if (dwc->gadget_driver && dwc->softconnect) {
1727 if (dwc->vbus_active) {
1728 /*
1729 * Both vbus was activated by otg and pullup was
1730 * signaled by the gadget driver.
1731 */
Pratyush Anand77473f72012-07-02 10:21:55 +05301732 ret = dwc3_gadget_run_stop(dwc, 1);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +02001733 } else {
Pratyush Anand77473f72012-07-02 10:21:55 +05301734 ret = dwc3_gadget_run_stop(dwc, 0);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +02001735 }
Jack Pham09e5c8e2013-03-06 18:53:43 -08001736 }
1737
1738 /*
1739 * Clearing run/stop bit might occur before disconnect event is seen.
1740 * Make sure to let gadget driver know in that case.
1741 */
1742 if (!dwc->vbus_active && dwc->start_config_issued) {
1743 dev_dbg(dwc->dev, "calling disconnect from %s\n", __func__);
1744 dwc3_gadget_disconnect_interrupt(dwc);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +02001745 }
1746
Felipe Balbi72246da2011-08-19 18:10:58 +03001747 spin_unlock_irqrestore(&dwc->lock, flags);
Pratyush Anand77473f72012-07-02 10:21:55 +05301748 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001749}
1750
Manu Gautamf1fceddf2012-10-12 14:02:50 +05301751/* Required gadget re-initialization before switching to gadget in OTG mode */
1752void dwc3_gadget_restart(struct dwc3 *dwc)
1753{
1754 struct dwc3_ep *dep;
1755 int ret = 0;
Vijayavardhan Vennapusab7434562012-12-12 16:48:49 +05301756 u32 reg;
Manu Gautamf1fceddf2012-10-12 14:02:50 +05301757
Vijayavardhan Vennapusab7434562012-12-12 16:48:49 +05301758 /* Enable all but Start and End of Frame IRQs */
1759 reg = (DWC3_DEVTEN_EVNTOVERFLOWEN |
1760 DWC3_DEVTEN_CMDCMPLTEN |
1761 DWC3_DEVTEN_ERRTICERREN |
1762 DWC3_DEVTEN_WKUPEVTEN |
1763 DWC3_DEVTEN_ULSTCNGEN |
1764 DWC3_DEVTEN_CONNECTDONEEN |
1765 DWC3_DEVTEN_USBRSTEN |
1766 DWC3_DEVTEN_DISCONNEVTEN);
1767 dwc3_writel(dwc->regs, DWC3_DEVTEN, reg);
1768
1769 /* Enable USB2 LPM and automatic phy suspend only on recent versions */
1770 if (dwc->revision >= DWC3_REVISION_194A) {
1771 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
1772 reg |= DWC3_DCFG_LPM_CAP;
1773 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
1774
1775 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
1776 reg &= ~(DWC3_DCTL_HIRD_THRES_MASK | DWC3_DCTL_L1_HIBER_EN);
1777
1778 /* TODO: This should be configurable */
1779 reg |= DWC3_DCTL_HIRD_THRES(28);
1780
1781 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Vijayavardhan Vennapusaba79f6b2013-07-30 13:39:52 +05301782
1783 dwc3_gadget_usb2_phy_suspend(dwc, true);
1784 dwc3_gadget_usb3_phy_suspend(dwc, true);
Vijayavardhan Vennapusab7434562012-12-12 16:48:49 +05301785 }
1786
1787 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
1788 reg &= ~(DWC3_DCFG_SPEED_MASK);
1789
1790 /**
1791 * WORKAROUND: DWC3 revision < 2.20a have an issue
1792 * which would cause metastability state on Run/Stop
1793 * bit if we try to force the IP to USB2-only mode.
1794 *
1795 * Because of that, we cannot configure the IP to any
1796 * speed other than the SuperSpeed
1797 *
1798 * Refers to:
1799 *
1800 * STAR#9000525659: Clock Domain Crossing on DCTL in
1801 * USB 2.0 Mode
1802 */
1803 if (dwc->revision < DWC3_REVISION_220A)
1804 reg |= DWC3_DCFG_SUPERSPEED;
1805 else
1806 reg |= dwc->maximum_speed;
1807 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
1808
1809 dwc->start_config_issued = false;
1810
1811 /* Start with SuperSpeed Default */
1812 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
Manu Gautamf1fceddf2012-10-12 14:02:50 +05301813
1814 dwc->delayed_status = false;
Vijayavardhan Vennapusab7434562012-12-12 16:48:49 +05301815 /* reinitialize physical ep0-1 */
Manu Gautamf1fceddf2012-10-12 14:02:50 +05301816 dep = dwc->eps[0];
1817 dep->flags = 0;
1818 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, false);
1819 if (ret) {
1820 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
1821 return;
1822 }
1823
1824 dep = dwc->eps[1];
1825 dep->flags = 0;
1826 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, false);
1827 if (ret) {
1828 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
1829 return;
1830 }
1831
1832 /* begin to receive SETUP packets */
1833 dwc->ep0state = EP0_SETUP_PHASE;
1834 dwc3_ep0_out_start(dwc);
1835}
1836
Felipe Balbi72246da2011-08-19 18:10:58 +03001837static int dwc3_gadget_start(struct usb_gadget *g,
1838 struct usb_gadget_driver *driver)
1839{
1840 struct dwc3 *dwc = gadget_to_dwc(g);
1841 struct dwc3_ep *dep;
1842 unsigned long flags;
1843 int ret = 0;
1844 u32 reg;
1845
Vijayavardhan Vennapusa0557c9b2013-05-17 13:24:49 +05301846 pm_runtime_get_sync(dwc->dev);
Felipe Balbi72246da2011-08-19 18:10:58 +03001847 spin_lock_irqsave(&dwc->lock, flags);
1848
1849 if (dwc->gadget_driver) {
1850 dev_err(dwc->dev, "%s is already bound to %s\n",
1851 dwc->gadget.name,
1852 dwc->gadget_driver->driver.name);
1853 ret = -EBUSY;
1854 goto err0;
1855 }
1856
1857 dwc->gadget_driver = driver;
1858 dwc->gadget.dev.driver = &driver->driver;
1859
Felipe Balbi72246da2011-08-19 18:10:58 +03001860 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
1861 reg &= ~(DWC3_DCFG_SPEED_MASK);
Felipe Balbi38d2c6c2012-03-23 12:20:31 +02001862
1863 /**
1864 * WORKAROUND: DWC3 revision < 2.20a have an issue
1865 * which would cause metastability state on Run/Stop
1866 * bit if we try to force the IP to USB2-only mode.
1867 *
1868 * Because of that, we cannot configure the IP to any
1869 * speed other than the SuperSpeed
1870 *
1871 * Refers to:
1872 *
1873 * STAR#9000525659: Clock Domain Crossing on DCTL in
1874 * USB 2.0 Mode
1875 */
1876 if (dwc->revision < DWC3_REVISION_220A)
1877 reg |= DWC3_DCFG_SUPERSPEED;
1878 else
1879 reg |= dwc->maximum_speed;
Felipe Balbi72246da2011-08-19 18:10:58 +03001880 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
1881
Paul Zimmermanb23c8432011-09-30 10:58:42 +03001882 dwc->start_config_issued = false;
1883
Felipe Balbi72246da2011-08-19 18:10:58 +03001884 /* Start with SuperSpeed Default */
1885 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
1886
1887 dep = dwc->eps[0];
Wesley Cheng446ad8d2013-06-05 16:15:01 +05301888 dep->endpoint.maxburst = 1;
Felipe Balbi07e0ee82012-07-16 14:08:16 +03001889 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, false);
Felipe Balbi72246da2011-08-19 18:10:58 +03001890 if (ret) {
1891 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
1892 goto err0;
1893 }
1894
1895 dep = dwc->eps[1];
Wesley Cheng446ad8d2013-06-05 16:15:01 +05301896 dep->endpoint.maxburst = 1;
Felipe Balbi07e0ee82012-07-16 14:08:16 +03001897 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, false);
Felipe Balbi72246da2011-08-19 18:10:58 +03001898 if (ret) {
1899 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
1900 goto err1;
1901 }
1902
1903 /* begin to receive SETUP packets */
Felipe Balbic7fcdeb2011-08-27 22:28:36 +03001904 dwc->ep0state = EP0_SETUP_PHASE;
Felipe Balbi72246da2011-08-19 18:10:58 +03001905 dwc3_ep0_out_start(dwc);
1906
1907 spin_unlock_irqrestore(&dwc->lock, flags);
Vijayavardhan Vennapusa0557c9b2013-05-17 13:24:49 +05301908 pm_runtime_put(dwc->dev);
Felipe Balbi72246da2011-08-19 18:10:58 +03001909
1910 return 0;
1911
1912err1:
1913 __dwc3_gadget_ep_disable(dwc->eps[0]);
1914
1915err0:
1916 spin_unlock_irqrestore(&dwc->lock, flags);
Vijayavardhan Vennapusa0557c9b2013-05-17 13:24:49 +05301917 pm_runtime_put(dwc->dev);
Felipe Balbi72246da2011-08-19 18:10:58 +03001918
1919 return ret;
1920}
1921
1922static int dwc3_gadget_stop(struct usb_gadget *g,
1923 struct usb_gadget_driver *driver)
1924{
1925 struct dwc3 *dwc = gadget_to_dwc(g);
1926 unsigned long flags;
1927
1928 spin_lock_irqsave(&dwc->lock, flags);
1929
1930 __dwc3_gadget_ep_disable(dwc->eps[0]);
1931 __dwc3_gadget_ep_disable(dwc->eps[1]);
1932
1933 dwc->gadget_driver = NULL;
1934 dwc->gadget.dev.driver = NULL;
1935
1936 spin_unlock_irqrestore(&dwc->lock, flags);
1937
1938 return 0;
1939}
Paul Zimmerman88df4272012-04-27 13:10:52 +03001940
Felipe Balbi72246da2011-08-19 18:10:58 +03001941static const struct usb_gadget_ops dwc3_gadget_ops = {
1942 .get_frame = dwc3_gadget_get_frame,
1943 .wakeup = dwc3_gadget_wakeup,
1944 .set_selfpowered = dwc3_gadget_set_selfpowered,
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +02001945 .vbus_session = dwc3_gadget_vbus_session,
Vijayavardhan Vennapusa908f1ed2012-10-19 19:51:48 +05301946 .vbus_draw = dwc3_gadget_vbus_draw,
Felipe Balbi72246da2011-08-19 18:10:58 +03001947 .pullup = dwc3_gadget_pullup,
1948 .udc_start = dwc3_gadget_start,
1949 .udc_stop = dwc3_gadget_stop,
1950};
1951
1952/* -------------------------------------------------------------------------- */
1953
1954static int __devinit dwc3_gadget_init_endpoints(struct dwc3 *dwc)
1955{
1956 struct dwc3_ep *dep;
1957 u8 epnum;
1958
1959 INIT_LIST_HEAD(&dwc->gadget.ep_list);
1960
1961 for (epnum = 0; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
1962 dep = kzalloc(sizeof(*dep), GFP_KERNEL);
1963 if (!dep) {
1964 dev_err(dwc->dev, "can't allocate endpoint %d\n",
1965 epnum);
1966 return -ENOMEM;
1967 }
1968
1969 dep->dwc = dwc;
1970 dep->number = epnum;
1971 dwc->eps[epnum] = dep;
1972
1973 snprintf(dep->name, sizeof(dep->name), "ep%d%s", epnum >> 1,
1974 (epnum & 1) ? "in" : "out");
1975 dep->endpoint.name = dep->name;
1976 dep->direction = (epnum & 1);
1977
1978 if (epnum == 0 || epnum == 1) {
1979 dep->endpoint.maxpacket = 512;
1980 dep->endpoint.ops = &dwc3_gadget_ep0_ops;
1981 if (!epnum)
1982 dwc->gadget.ep0 = &dep->endpoint;
1983 } else {
1984 int ret;
1985
1986 dep->endpoint.maxpacket = 1024;
Sebastian Andrzej Siewior12d36c12011-11-03 20:27:50 +01001987 dep->endpoint.max_streams = 15;
Felipe Balbi72246da2011-08-19 18:10:58 +03001988 dep->endpoint.ops = &dwc3_gadget_ep_ops;
1989 list_add_tail(&dep->endpoint.ep_list,
1990 &dwc->gadget.ep_list);
1991
1992 ret = dwc3_alloc_trb_pool(dep);
Felipe Balbi25b8ff62011-11-04 12:32:47 +02001993 if (ret)
Felipe Balbi72246da2011-08-19 18:10:58 +03001994 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001995 }
Felipe Balbi25b8ff62011-11-04 12:32:47 +02001996
Felipe Balbi72246da2011-08-19 18:10:58 +03001997 INIT_LIST_HEAD(&dep->request_list);
1998 INIT_LIST_HEAD(&dep->req_queued);
1999 }
2000
2001 return 0;
2002}
2003
2004static void dwc3_gadget_free_endpoints(struct dwc3 *dwc)
2005{
2006 struct dwc3_ep *dep;
2007 u8 epnum;
2008
2009 for (epnum = 0; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
2010 dep = dwc->eps[epnum];
2011 dwc3_free_trb_pool(dep);
2012
2013 if (epnum != 0 && epnum != 1)
2014 list_del(&dep->endpoint.ep_list);
2015
2016 kfree(dep);
2017 }
2018}
2019
2020static void dwc3_gadget_release(struct device *dev)
2021{
2022 dev_dbg(dev, "%s\n", __func__);
2023}
2024
2025/* -------------------------------------------------------------------------- */
2026static int dwc3_cleanup_done_reqs(struct dwc3 *dwc, struct dwc3_ep *dep,
2027 const struct dwc3_event_depevt *event, int status)
2028{
2029 struct dwc3_request *req;
Felipe Balbif6bafc62012-02-06 11:04:53 +02002030 struct dwc3_trb *trb;
Felipe Balbi72246da2011-08-19 18:10:58 +03002031 unsigned int count;
2032 unsigned int s_pkt = 0;
Pratyush Anand73939b02012-05-25 18:54:56 +05302033 unsigned int trb_status;
Felipe Balbi72246da2011-08-19 18:10:58 +03002034
2035 do {
2036 req = next_request(&dep->req_queued);
Sebastian Andrzej Siewiord39ee7b2011-11-03 10:32:20 +01002037 if (!req) {
2038 WARN_ON_ONCE(1);
2039 return 1;
2040 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002041
Felipe Balbif6bafc62012-02-06 11:04:53 +02002042 trb = req->trb;
Felipe Balbi72246da2011-08-19 18:10:58 +03002043
Felipe Balbif6bafc62012-02-06 11:04:53 +02002044 if ((trb->ctrl & DWC3_TRB_CTRL_HWO) && status != -ESHUTDOWN)
Sebastian Andrzej Siewior0d2f4752011-08-19 19:59:12 +02002045 /*
2046 * We continue despite the error. There is not much we
Paul Zimmerman1d046792012-02-15 18:56:56 -08002047 * can do. If we don't clean it up we loop forever. If
2048 * we skip the TRB then it gets overwritten after a
2049 * while since we use them in a ring buffer. A BUG()
2050 * would help. Lets hope that if this occurs, someone
Sebastian Andrzej Siewior0d2f4752011-08-19 19:59:12 +02002051 * fixes the root cause instead of looking away :)
2052 */
Felipe Balbi72246da2011-08-19 18:10:58 +03002053 dev_err(dwc->dev, "%s's TRB (%p) still owned by HW\n",
2054 dep->name, req->trb);
Felipe Balbif6bafc62012-02-06 11:04:53 +02002055 count = trb->size & DWC3_TRB_SIZE_MASK;
Felipe Balbi72246da2011-08-19 18:10:58 +03002056
2057 if (dep->direction) {
2058 if (count) {
Pratyush Anand73939b02012-05-25 18:54:56 +05302059 trb_status = DWC3_TRB_SIZE_TRBSTS(trb->size);
2060 if (trb_status == DWC3_TRBSTS_MISSED_ISOC) {
2061 dev_dbg(dwc->dev, "incomplete IN transfer %s\n",
2062 dep->name);
Pratyush Anand921b0b82013-01-14 15:59:32 +05302063 /*
2064 * If missed isoc occurred and there is
2065 * no request queued then issue END
2066 * TRANSFER, so that core generates
2067 * next xfernotready and we will issue
2068 * a fresh START TRANSFER.
2069 * If there are still queued request
2070 * then wait, do not issue either END
2071 * or UPDATE TRANSFER, just attach next
2072 * request in request_list during
2073 * giveback.If any future queued request
2074 * is successfully transferred then we
2075 * will issue UPDATE TRANSFER for all
2076 * request in the request_list.
2077 */
Pratyush Anand73939b02012-05-25 18:54:56 +05302078 dep->flags |= DWC3_EP_MISSED_ISOC;
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +05302079 dbg_event(dep->number, "MISSED ISOC",
2080 status);
Pratyush Anand73939b02012-05-25 18:54:56 +05302081 } else {
2082 dev_err(dwc->dev, "incomplete IN transfer %s\n",
2083 dep->name);
2084 status = -ECONNRESET;
2085 }
Pratyush Anand921b0b82013-01-14 15:59:32 +05302086 } else {
2087 dep->flags &= ~DWC3_EP_MISSED_ISOC;
Felipe Balbi72246da2011-08-19 18:10:58 +03002088 }
2089 } else {
2090 if (count && (event->status & DEPEVT_STATUS_SHORT))
2091 s_pkt = 1;
2092 }
2093
Vijayavardhan Vennapusa5f5c3e62013-11-13 19:53:13 +05302094 if (req->ztrb)
2095 trb = req->ztrb;
Felipe Balbi72246da2011-08-19 18:10:58 +03002096 /*
2097 * We assume here we will always receive the entire data block
2098 * which we should receive. Meaning, if we program RX to
2099 * receive 4K but we receive only 2K, we assume that's all we
2100 * should receive and we simply bounce the request back to the
2101 * gadget driver for further processing.
2102 */
2103 req->request.actual += req->request.length - count;
2104 dwc3_gadget_giveback(dep, req, status);
2105 if (s_pkt)
2106 break;
Felipe Balbif6bafc62012-02-06 11:04:53 +02002107 if ((event->status & DEPEVT_STATUS_LST) &&
Pratyush Anand413dba62012-06-03 19:43:19 +05302108 (trb->ctrl & (DWC3_TRB_CTRL_LST |
2109 DWC3_TRB_CTRL_HWO)))
Felipe Balbi72246da2011-08-19 18:10:58 +03002110 break;
Felipe Balbif6bafc62012-02-06 11:04:53 +02002111 if ((event->status & DEPEVT_STATUS_IOC) &&
2112 (trb->ctrl & DWC3_TRB_CTRL_IOC))
Felipe Balbi72246da2011-08-19 18:10:58 +03002113 break;
2114 } while (1);
2115
Pratyush Anand18bbcb02013-01-14 15:59:34 +05302116 if (usb_endpoint_xfer_isoc(dep->endpoint.desc) &&
2117 list_empty(&dep->req_queued)) {
Vijayavardhan Vennapusa91ba6532013-01-30 17:35:45 +05302118 if (list_empty(&dep->request_list))
Pratyush Anand18bbcb02013-01-14 15:59:34 +05302119 /*
2120 * If there is no entry in request list then do
2121 * not issue END TRANSFER now. Just set PENDING
2122 * flag, so that END TRANSFER is issued when an
2123 * entry is added into request list.
2124 */
Vijayavardhan Vennapusa91ba6532013-01-30 17:35:45 +05302125 dep->flags |= DWC3_EP_PENDING_REQUEST;
2126 else
Pratyush Anand18bbcb02013-01-14 15:59:34 +05302127 dwc3_stop_active_transfer(dwc, dep->number);
Vijayavardhan Vennapusa91ba6532013-01-30 17:35:45 +05302128 dep->flags &= ~DWC3_EP_MISSED_ISOC;
Pratyush Anand921b0b82013-01-14 15:59:32 +05302129 return 1;
2130 }
2131
Felipe Balbif6bafc62012-02-06 11:04:53 +02002132 if ((event->status & DEPEVT_STATUS_IOC) &&
2133 (trb->ctrl & DWC3_TRB_CTRL_IOC))
Felipe Balbi72246da2011-08-19 18:10:58 +03002134 return 0;
2135 return 1;
2136}
2137
2138static void dwc3_endpoint_transfer_complete(struct dwc3 *dwc,
2139 struct dwc3_ep *dep, const struct dwc3_event_depevt *event,
2140 int start_new)
2141{
2142 unsigned status = 0;
2143 int clean_busy;
2144
2145 if (event->status & DEPEVT_STATUS_BUSERR)
2146 status = -ECONNRESET;
2147
Paul Zimmerman1d046792012-02-15 18:56:56 -08002148 clean_busy = dwc3_cleanup_done_reqs(dwc, dep, event, status);
Paul Zimmermanc2df85c2012-02-24 17:32:18 -08002149 if (clean_busy)
Felipe Balbi72246da2011-08-19 18:10:58 +03002150 dep->flags &= ~DWC3_EP_BUSY;
Felipe Balbifae2b902011-10-14 13:00:30 +03002151
2152 /*
2153 * WORKAROUND: This is the 2nd half of U1/U2 -> U0 workaround.
2154 * See dwc3_gadget_linksts_change_interrupt() for 1st half.
2155 */
2156 if (dwc->revision < DWC3_REVISION_183A) {
2157 u32 reg;
2158 int i;
2159
2160 for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) {
Moiz Sonasatheed03f12012-08-01 14:08:30 -05002161 dep = dwc->eps[i];
Felipe Balbifae2b902011-10-14 13:00:30 +03002162
2163 if (!(dep->flags & DWC3_EP_ENABLED))
2164 continue;
2165
2166 if (!list_empty(&dep->req_queued))
2167 return;
2168 }
2169
2170 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2171 reg |= dwc->u1u2;
2172 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2173
2174 dwc->u1u2 = 0;
2175 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002176}
2177
Felipe Balbi72246da2011-08-19 18:10:58 +03002178static void dwc3_endpoint_interrupt(struct dwc3 *dwc,
2179 const struct dwc3_event_depevt *event)
2180{
2181 struct dwc3_ep *dep;
2182 u8 epnum = event->endpoint_number;
2183
2184 dep = dwc->eps[epnum];
2185
Felipe Balbia09be0a2012-06-06 09:19:35 +03002186 if (!(dep->flags & DWC3_EP_ENABLED))
2187 return;
2188
Felipe Balbi72246da2011-08-19 18:10:58 +03002189 dev_vdbg(dwc->dev, "%s: %s\n", dep->name,
2190 dwc3_ep_event_string(event->endpoint_event));
2191
2192 if (epnum == 0 || epnum == 1) {
2193 dwc3_ep0_interrupt(dwc, event);
2194 return;
2195 }
2196
2197 switch (event->endpoint_event) {
2198 case DWC3_DEPEVT_XFERCOMPLETE:
Felipe Balbi4959cfc2012-06-06 12:04:13 +03002199 dep->resource_index = 0;
Paul Zimmermanc2df85c2012-02-24 17:32:18 -08002200
Ido Shayevitz57cdac12012-03-12 20:25:24 +02002201 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbi72246da2011-08-19 18:10:58 +03002202 dev_dbg(dwc->dev, "%s is an Isochronous endpoint\n",
2203 dep->name);
2204 return;
2205 }
2206
2207 dwc3_endpoint_transfer_complete(dwc, dep, event, 1);
2208 break;
2209 case DWC3_DEPEVT_XFERINPROGRESS:
Ido Shayevitz57cdac12012-03-12 20:25:24 +02002210 if (!usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbi72246da2011-08-19 18:10:58 +03002211 dev_dbg(dwc->dev, "%s is not an Isochronous endpoint\n",
2212 dep->name);
2213 return;
2214 }
2215
2216 dwc3_endpoint_transfer_complete(dwc, dep, event, 0);
2217 break;
2218 case DWC3_DEPEVT_XFERNOTREADY:
Ido Shayevitz57cdac12012-03-12 20:25:24 +02002219 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbi72246da2011-08-19 18:10:58 +03002220 dwc3_gadget_start_isoc(dwc, dep, event);
2221 } else {
2222 int ret;
2223
2224 dev_vdbg(dwc->dev, "%s: reason %s\n",
Felipe Balbi40aa41f2012-01-18 17:06:03 +02002225 dep->name, event->status &
2226 DEPEVT_STATUS_TRANSFER_ACTIVE
Felipe Balbi72246da2011-08-19 18:10:58 +03002227 ? "Transfer Active"
2228 : "Transfer Not Active");
2229
2230 ret = __dwc3_gadget_kick_transfer(dep, 0, 1);
2231 if (!ret || ret == -EBUSY)
2232 return;
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +05302233 else
2234 dbg_event(dep->number, "QUEUE", ret);
Felipe Balbi72246da2011-08-19 18:10:58 +03002235
2236 dev_dbg(dwc->dev, "%s: failed to kick transfers\n",
2237 dep->name);
2238 }
2239
2240 break;
Felipe Balbi879631a2011-09-30 10:58:47 +03002241 case DWC3_DEPEVT_STREAMEVT:
Ido Shayevitz57cdac12012-03-12 20:25:24 +02002242 if (!usb_endpoint_xfer_bulk(dep->endpoint.desc)) {
Felipe Balbi879631a2011-09-30 10:58:47 +03002243 dev_err(dwc->dev, "Stream event for non-Bulk %s\n",
2244 dep->name);
2245 return;
2246 }
2247
2248 switch (event->status) {
2249 case DEPEVT_STREAMEVT_FOUND:
2250 dev_vdbg(dwc->dev, "Stream %d found and started\n",
2251 event->parameters);
2252
2253 break;
2254 case DEPEVT_STREAMEVT_NOTFOUND:
2255 /* FALLTHROUGH */
2256 default:
2257 dev_dbg(dwc->dev, "Couldn't find suitable stream\n");
2258 }
2259 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03002260 case DWC3_DEPEVT_RXTXFIFOEVT:
2261 dev_dbg(dwc->dev, "%s FIFO Overrun\n", dep->name);
2262 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03002263 case DWC3_DEPEVT_EPCMDCMPLT:
Felipe Balbib129eb72012-02-17 12:10:04 +02002264 dev_vdbg(dwc->dev, "Endpoint Command Complete\n");
Felipe Balbi72246da2011-08-19 18:10:58 +03002265 break;
2266 }
2267}
2268
2269static void dwc3_disconnect_gadget(struct dwc3 *dwc)
2270{
2271 if (dwc->gadget_driver && dwc->gadget_driver->disconnect) {
2272 spin_unlock(&dwc->lock);
2273 dwc->gadget_driver->disconnect(&dwc->gadget);
2274 spin_lock(&dwc->lock);
2275 }
2276}
2277
2278static void dwc3_stop_active_transfer(struct dwc3 *dwc, u32 epnum)
2279{
2280 struct dwc3_ep *dep;
2281 struct dwc3_gadget_ep_cmd_params params;
2282 u32 cmd;
2283 int ret;
2284
2285 dep = dwc->eps[epnum];
2286
Felipe Balbi4959cfc2012-06-06 12:04:13 +03002287 if (!dep->resource_index)
Pratyush Anand6263ebe2012-06-23 02:23:08 +05302288 return;
2289
Pratyush Anande67fdeb2012-07-06 15:19:10 +05302290 /*
2291 * NOTICE: We are violating what the Databook says about the
2292 * EndTransfer command. Ideally we would _always_ wait for the
2293 * EndTransfer Command Completion IRQ, but that's causing too
2294 * much trouble synchronizing between us and gadget driver.
2295 *
2296 * We have discussed this with the IP Provider and it was
2297 * suggested to giveback all requests here, but give HW some
2298 * extra time to synchronize with the interconnect. We're using
2299 * an arbitraty 100us delay for that.
2300 *
2301 * Note also that a similar handling was tested by Synopsys
2302 * (thanks a lot Paul) and nothing bad has come out of it.
2303 * In short, what we're doing is:
2304 *
2305 * - Issue EndTransfer WITH CMDIOC bit set
2306 * - Wait 100us
2307 */
2308
Pratyush Anand6263ebe2012-06-23 02:23:08 +05302309 cmd = DWC3_DEPCMD_ENDTRANSFER;
2310 cmd |= DWC3_DEPCMD_HIPRI_FORCERM | DWC3_DEPCMD_CMDIOC;
Felipe Balbi4959cfc2012-06-06 12:04:13 +03002311 cmd |= DWC3_DEPCMD_PARAM(dep->resource_index);
Pratyush Anand6263ebe2012-06-23 02:23:08 +05302312 memset(&params, 0, sizeof(params));
2313 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number, cmd, &params);
2314 WARN_ON_ONCE(ret);
Felipe Balbi4959cfc2012-06-06 12:04:13 +03002315 dep->resource_index = 0;
Felipe Balbi1df89b62012-10-04 11:58:00 +03002316 dep->flags &= ~DWC3_EP_BUSY;
Pratyush Anande67fdeb2012-07-06 15:19:10 +05302317 udelay(100);
Felipe Balbi72246da2011-08-19 18:10:58 +03002318}
2319
2320static void dwc3_stop_active_transfers(struct dwc3 *dwc)
2321{
2322 u32 epnum;
2323
2324 for (epnum = 2; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
2325 struct dwc3_ep *dep;
2326
2327 dep = dwc->eps[epnum];
2328 if (!(dep->flags & DWC3_EP_ENABLED))
2329 continue;
2330
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +02002331 dwc3_remove_requests(dwc, dep);
Felipe Balbi72246da2011-08-19 18:10:58 +03002332 }
2333}
2334
2335static void dwc3_clear_stall_all_ep(struct dwc3 *dwc)
2336{
2337 u32 epnum;
2338
2339 for (epnum = 1; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
2340 struct dwc3_ep *dep;
2341 struct dwc3_gadget_ep_cmd_params params;
2342 int ret;
2343
2344 dep = dwc->eps[epnum];
2345
2346 if (!(dep->flags & DWC3_EP_STALL))
2347 continue;
2348
2349 dep->flags &= ~DWC3_EP_STALL;
2350
2351 memset(&params, 0, sizeof(params));
2352 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number,
2353 DWC3_DEPCMD_CLEARSTALL, &params);
2354 WARN_ON_ONCE(ret);
2355 }
2356}
2357
2358static void dwc3_gadget_disconnect_interrupt(struct dwc3 *dwc)
2359{
Felipe Balbi34d548c2012-05-24 10:30:01 +03002360 int reg;
2361
Felipe Balbi72246da2011-08-19 18:10:58 +03002362 dev_vdbg(dwc->dev, "%s\n", __func__);
Felipe Balbi72246da2011-08-19 18:10:58 +03002363
2364 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2365 reg &= ~DWC3_DCTL_INITU1ENA;
2366 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2367
2368 reg &= ~DWC3_DCTL_INITU2ENA;
2369 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +03002370
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +05302371 dbg_event(0xFF, "DISCONNECT", 0);
Felipe Balbi72246da2011-08-19 18:10:58 +03002372 dwc3_disconnect_gadget(dwc);
Paul Zimmermanb23c8432011-09-30 10:58:42 +03002373 dwc->start_config_issued = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03002374
2375 dwc->gadget.speed = USB_SPEED_UNKNOWN;
Felipe Balbidf62df52011-10-14 15:11:49 +03002376 dwc->setup_packet_pending = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03002377}
2378
Paul Zimmermandffb81b2012-04-27 12:54:05 +03002379static void dwc3_gadget_usb3_phy_suspend(struct dwc3 *dwc, int suspend)
Felipe Balbi72246da2011-08-19 18:10:58 +03002380{
2381 u32 reg;
2382
2383 reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0));
2384
Paul Zimmermandffb81b2012-04-27 12:54:05 +03002385 if (suspend)
Felipe Balbi72246da2011-08-19 18:10:58 +03002386 reg |= DWC3_GUSB3PIPECTL_SUSPHY;
Paul Zimmermandffb81b2012-04-27 12:54:05 +03002387 else
2388 reg &= ~DWC3_GUSB3PIPECTL_SUSPHY;
Felipe Balbi72246da2011-08-19 18:10:58 +03002389
2390 dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg);
2391}
2392
Paul Zimmermandffb81b2012-04-27 12:54:05 +03002393static void dwc3_gadget_usb2_phy_suspend(struct dwc3 *dwc, int suspend)
Felipe Balbi72246da2011-08-19 18:10:58 +03002394{
2395 u32 reg;
2396
2397 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
2398
Paul Zimmermandffb81b2012-04-27 12:54:05 +03002399 if (suspend)
Felipe Balbi72246da2011-08-19 18:10:58 +03002400 reg |= DWC3_GUSB2PHYCFG_SUSPHY;
Paul Zimmermandffb81b2012-04-27 12:54:05 +03002401 else
2402 reg &= ~DWC3_GUSB2PHYCFG_SUSPHY;
Felipe Balbi72246da2011-08-19 18:10:58 +03002403
2404 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
2405}
2406
2407static void dwc3_gadget_reset_interrupt(struct dwc3 *dwc)
2408{
2409 u32 reg;
Vijayavardhan Vennapusa908f1ed2012-10-19 19:51:48 +05302410 struct dwc3_otg *dotg = dwc->dotg;
Felipe Balbi72246da2011-08-19 18:10:58 +03002411
2412 dev_vdbg(dwc->dev, "%s\n", __func__);
2413
Felipe Balbidf62df52011-10-14 15:11:49 +03002414 /*
2415 * WORKAROUND: DWC3 revisions <1.88a have an issue which
2416 * would cause a missing Disconnect Event if there's a
2417 * pending Setup Packet in the FIFO.
2418 *
2419 * There's no suggested workaround on the official Bug
2420 * report, which states that "unless the driver/application
2421 * is doing any special handling of a disconnect event,
2422 * there is no functional issue".
2423 *
2424 * Unfortunately, it turns out that we _do_ some special
2425 * handling of a disconnect event, namely complete all
2426 * pending transfers, notify gadget driver of the
2427 * disconnection, and so on.
2428 *
2429 * Our suggested workaround is to follow the Disconnect
2430 * Event steps here, instead, based on a setup_packet_pending
2431 * flag. Such flag gets set whenever we have a XferNotReady
2432 * event on EP0 and gets cleared on XferComplete for the
2433 * same endpoint.
2434 *
2435 * Refers to:
2436 *
2437 * STAR#9000466709: RTL: Device : Disconnect event not
2438 * generated if setup packet pending in FIFO
2439 */
2440 if (dwc->revision < DWC3_REVISION_188A) {
2441 if (dwc->setup_packet_pending)
2442 dwc3_gadget_disconnect_interrupt(dwc);
2443 }
2444
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +05302445 dbg_event(0xFF, "BUS RST", 0);
Felipe Balbi961906e2011-12-20 15:37:21 +02002446 /* after reset -> Default State */
2447 dwc->dev_state = DWC3_DEFAULT_STATE;
2448
Paul Zimmerman88df4272012-04-27 13:10:52 +03002449 /* Recent versions support automatic phy suspend and don't need this */
2450 if (dwc->revision < DWC3_REVISION_194A) {
2451 /* Resume PHYs */
2452 dwc3_gadget_usb2_phy_suspend(dwc, false);
2453 dwc3_gadget_usb3_phy_suspend(dwc, false);
2454 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002455
Vijayavardhan Vennapusa908f1ed2012-10-19 19:51:48 +05302456 if (dotg && dotg->otg.phy)
2457 usb_phy_set_power(dotg->otg.phy, 0);
2458
Felipe Balbi72246da2011-08-19 18:10:58 +03002459 if (dwc->gadget.speed != USB_SPEED_UNKNOWN)
2460 dwc3_disconnect_gadget(dwc);
2461
2462 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2463 reg &= ~DWC3_DCTL_TSTCTRL_MASK;
2464 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Gerard Cauvy3b637362012-02-10 12:21:18 +02002465 dwc->test_mode = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03002466
2467 dwc3_stop_active_transfers(dwc);
2468 dwc3_clear_stall_all_ep(dwc);
Paul Zimmermanb23c8432011-09-30 10:58:42 +03002469 dwc->start_config_issued = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03002470
2471 /* Reset device address to zero */
2472 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2473 reg &= ~(DWC3_DCFG_DEVADDR_MASK);
2474 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +03002475}
2476
2477static void dwc3_update_ram_clk_sel(struct dwc3 *dwc, u32 speed)
2478{
2479 u32 reg;
2480 u32 usb30_clock = DWC3_GCTL_CLK_BUS;
2481
2482 /*
2483 * We change the clock only at SS but I dunno why I would want to do
2484 * this. Maybe it becomes part of the power saving plan.
2485 */
2486
2487 if (speed != DWC3_DSTS_SUPERSPEED)
2488 return;
2489
2490 /*
2491 * RAMClkSel is reset to 0 after USB reset, so it must be reprogrammed
2492 * each time on Connect Done.
2493 */
2494 if (!usb30_clock)
2495 return;
2496
2497 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
2498 reg |= DWC3_GCTL_RAMCLKSEL(usb30_clock);
2499 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
2500}
2501
Paul Zimmermandffb81b2012-04-27 12:54:05 +03002502static void dwc3_gadget_phy_suspend(struct dwc3 *dwc, u8 speed)
Felipe Balbi72246da2011-08-19 18:10:58 +03002503{
2504 switch (speed) {
2505 case USB_SPEED_SUPER:
Paul Zimmermandffb81b2012-04-27 12:54:05 +03002506 dwc3_gadget_usb2_phy_suspend(dwc, true);
Felipe Balbi72246da2011-08-19 18:10:58 +03002507 break;
2508 case USB_SPEED_HIGH:
2509 case USB_SPEED_FULL:
2510 case USB_SPEED_LOW:
Paul Zimmermandffb81b2012-04-27 12:54:05 +03002511 dwc3_gadget_usb3_phy_suspend(dwc, true);
Felipe Balbi72246da2011-08-19 18:10:58 +03002512 break;
2513 }
2514}
2515
2516static void dwc3_gadget_conndone_interrupt(struct dwc3 *dwc)
2517{
2518 struct dwc3_gadget_ep_cmd_params params;
2519 struct dwc3_ep *dep;
2520 int ret;
2521 u32 reg;
2522 u8 speed;
2523
2524 dev_vdbg(dwc->dev, "%s\n", __func__);
2525
2526 memset(&params, 0x00, sizeof(params));
2527
Felipe Balbi72246da2011-08-19 18:10:58 +03002528 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
2529 speed = reg & DWC3_DSTS_CONNECTSPD;
2530 dwc->speed = speed;
2531
2532 dwc3_update_ram_clk_sel(dwc, speed);
2533
2534 switch (speed) {
2535 case DWC3_DCFG_SUPERSPEED:
Felipe Balbi05870c52011-10-14 14:51:38 +03002536 /*
2537 * WORKAROUND: DWC3 revisions <1.90a have an issue which
2538 * would cause a missing USB3 Reset event.
2539 *
2540 * In such situations, we should force a USB3 Reset
2541 * event by calling our dwc3_gadget_reset_interrupt()
2542 * routine.
2543 *
2544 * Refers to:
2545 *
2546 * STAR#9000483510: RTL: SS : USB3 reset event may
2547 * not be generated always when the link enters poll
2548 */
2549 if (dwc->revision < DWC3_REVISION_190A)
2550 dwc3_gadget_reset_interrupt(dwc);
2551
Felipe Balbi72246da2011-08-19 18:10:58 +03002552 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
2553 dwc->gadget.ep0->maxpacket = 512;
2554 dwc->gadget.speed = USB_SPEED_SUPER;
2555 break;
2556 case DWC3_DCFG_HIGHSPEED:
2557 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
2558 dwc->gadget.ep0->maxpacket = 64;
2559 dwc->gadget.speed = USB_SPEED_HIGH;
2560 break;
2561 case DWC3_DCFG_FULLSPEED2:
2562 case DWC3_DCFG_FULLSPEED1:
2563 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
2564 dwc->gadget.ep0->maxpacket = 64;
2565 dwc->gadget.speed = USB_SPEED_FULL;
2566 break;
2567 case DWC3_DCFG_LOWSPEED:
2568 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(8);
2569 dwc->gadget.ep0->maxpacket = 8;
2570 dwc->gadget.speed = USB_SPEED_LOW;
2571 break;
2572 }
2573
Paul Zimmerman88df4272012-04-27 13:10:52 +03002574 /* Recent versions support automatic phy suspend and don't need this */
2575 if (dwc->revision < DWC3_REVISION_194A) {
2576 /* Suspend unneeded PHY */
2577 dwc3_gadget_phy_suspend(dwc, dwc->gadget.speed);
2578 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002579
2580 dep = dwc->eps[0];
Felipe Balbi07e0ee82012-07-16 14:08:16 +03002581 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, true);
Felipe Balbi72246da2011-08-19 18:10:58 +03002582 if (ret) {
2583 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
2584 return;
2585 }
2586
2587 dep = dwc->eps[1];
Felipe Balbi07e0ee82012-07-16 14:08:16 +03002588 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, true);
Felipe Balbi72246da2011-08-19 18:10:58 +03002589 if (ret) {
2590 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
2591 return;
2592 }
2593
2594 /*
2595 * Configure PHY via GUSB3PIPECTLn if required.
2596 *
2597 * Update GTXFIFOSIZn
2598 *
2599 * In both cases reset values should be sufficient.
2600 */
2601}
2602
2603static void dwc3_gadget_wakeup_interrupt(struct dwc3 *dwc)
2604{
2605 dev_vdbg(dwc->dev, "%s\n", __func__);
2606
2607 /*
2608 * TODO take core out of low power mode when that's
2609 * implemented.
2610 */
2611
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +05302612 dbg_event(0xFF, "WAKEUP", 0);
Felipe Balbi72246da2011-08-19 18:10:58 +03002613 dwc->gadget_driver->resume(&dwc->gadget);
2614}
2615
2616static void dwc3_gadget_linksts_change_interrupt(struct dwc3 *dwc,
2617 unsigned int evtinfo)
2618{
Felipe Balbifae2b902011-10-14 13:00:30 +03002619 enum dwc3_link_state next = evtinfo & DWC3_LINK_STATE_MASK;
2620
2621 /*
2622 * WORKAROUND: DWC3 Revisions <1.83a have an issue which, depending
2623 * on the link partner, the USB session might do multiple entry/exit
2624 * of low power states before a transfer takes place.
2625 *
2626 * Due to this problem, we might experience lower throughput. The
2627 * suggested workaround is to disable DCTL[12:9] bits if we're
2628 * transitioning from U1/U2 to U0 and enable those bits again
2629 * after a transfer completes and there are no pending transfers
2630 * on any of the enabled endpoints.
2631 *
2632 * This is the first half of that workaround.
2633 *
2634 * Refers to:
2635 *
2636 * STAR#9000446952: RTL: Device SS : if U1/U2 ->U0 takes >128us
2637 * core send LGO_Ux entering U0
2638 */
2639 if (dwc->revision < DWC3_REVISION_183A) {
2640 if (next == DWC3_LINK_STATE_U0) {
2641 u32 u1u2;
2642 u32 reg;
2643
2644 switch (dwc->link_state) {
2645 case DWC3_LINK_STATE_U1:
2646 case DWC3_LINK_STATE_U2:
2647 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2648 u1u2 = reg & (DWC3_DCTL_INITU2ENA
2649 | DWC3_DCTL_ACCEPTU2ENA
2650 | DWC3_DCTL_INITU1ENA
2651 | DWC3_DCTL_ACCEPTU1ENA);
2652
2653 if (!dwc->u1u2)
2654 dwc->u1u2 = reg & u1u2;
2655
2656 reg &= ~u1u2;
2657
2658 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2659 break;
2660 default:
2661 /* do nothing */
2662 break;
2663 }
2664 }
2665 }
2666
Vijayavardhan Vennapusa54be1d62012-10-06 18:32:06 +05302667 if (next == DWC3_LINK_STATE_U0) {
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +05302668 if (dwc->link_state == DWC3_LINK_STATE_U3) {
2669 dbg_event(0xFF, "RESUME", 0);
Vijayavardhan Vennapusa8594e862013-11-01 16:44:31 +05302670 dwc->gadget_driver->resume(&dwc->gadget);
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +05302671 }
Vijayavardhan Vennapusa54be1d62012-10-06 18:32:06 +05302672 } else if (next == DWC3_LINK_STATE_U3) {
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +05302673 dbg_event(0xFF, "SUSPEND", 0);
Vijayavardhan Vennapusa54be1d62012-10-06 18:32:06 +05302674 dwc->gadget_driver->suspend(&dwc->gadget);
2675 }
2676
Felipe Balbifae2b902011-10-14 13:00:30 +03002677 dwc->link_state = next;
Felipe Balbi019ac832011-09-08 21:18:47 +03002678
2679 dev_vdbg(dwc->dev, "%s link %d\n", __func__, dwc->link_state);
Felipe Balbi72246da2011-08-19 18:10:58 +03002680}
2681
Vijayavardhan Vennapusa8a011c92013-07-29 09:06:48 +05302682static void dwc3_dump_reg_info(struct dwc3 *dwc)
2683{
2684 dbg_event(0xFF, "REGDUMP", 0);
2685
2686 dbg_print_reg("GUSB3PIPCTL", dwc3_readl(dwc->regs,
2687 DWC3_GUSB3PIPECTL(0)));
2688 dbg_print_reg("GUSB2PHYCONFIG", dwc3_readl(dwc->regs,
2689 DWC3_GUSB2PHYCFG(0)));
2690 dbg_print_reg("GCTL", dwc3_readl(dwc->regs, DWC3_GCTL));
2691 dbg_print_reg("GUCTL", dwc3_readl(dwc->regs, DWC3_GUCTL));
2692 dbg_print_reg("GDBGLTSSM", dwc3_readl(dwc->regs, DWC3_GDBGLTSSM));
2693 dbg_print_reg("DCFG", dwc3_readl(dwc->regs, DWC3_DCFG));
2694 dbg_print_reg("DCTL", dwc3_readl(dwc->regs, DWC3_DCTL));
2695 dbg_print_reg("DEVTEN", dwc3_readl(dwc->regs, DWC3_DEVTEN));
2696 dbg_print_reg("DSTS", dwc3_readl(dwc->regs, DWC3_DSTS));
2697 dbg_print_reg("DALPENA", dwc3_readl(dwc->regs, DWC3_DALEPENA));
2698 dbg_print_reg("DGCMD", dwc3_readl(dwc->regs, DWC3_DGCMD));
2699
2700 dbg_print_reg("OCFG", dwc3_readl(dwc->regs, DWC3_OCFG));
2701 dbg_print_reg("OCTL", dwc3_readl(dwc->regs, DWC3_OCTL));
2702 dbg_print_reg("OEVT", dwc3_readl(dwc->regs, DWC3_OEVT));
2703 dbg_print_reg("OSTS", dwc3_readl(dwc->regs, DWC3_OSTS));
2704
2705 dwc3_notify_event(dwc, DWC3_CONTROLLER_ERROR_EVENT);
Vijayavardhan Vennapusa8a011c92013-07-29 09:06:48 +05302706}
2707
Felipe Balbi72246da2011-08-19 18:10:58 +03002708static void dwc3_gadget_interrupt(struct dwc3 *dwc,
2709 const struct dwc3_event_devt *event)
2710{
2711 switch (event->type) {
2712 case DWC3_DEVICE_EVENT_DISCONNECT:
2713 dwc3_gadget_disconnect_interrupt(dwc);
2714 break;
2715 case DWC3_DEVICE_EVENT_RESET:
2716 dwc3_gadget_reset_interrupt(dwc);
2717 break;
2718 case DWC3_DEVICE_EVENT_CONNECT_DONE:
2719 dwc3_gadget_conndone_interrupt(dwc);
2720 break;
2721 case DWC3_DEVICE_EVENT_WAKEUP:
2722 dwc3_gadget_wakeup_interrupt(dwc);
2723 break;
2724 case DWC3_DEVICE_EVENT_LINK_STATUS_CHANGE:
2725 dwc3_gadget_linksts_change_interrupt(dwc, event->event_info);
2726 break;
2727 case DWC3_DEVICE_EVENT_EOPF:
2728 dev_vdbg(dwc->dev, "End of Periodic Frame\n");
2729 break;
2730 case DWC3_DEVICE_EVENT_SOF:
2731 dev_vdbg(dwc->dev, "Start of Periodic Frame\n");
2732 break;
2733 case DWC3_DEVICE_EVENT_ERRATIC_ERROR:
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +05302734 dbg_event(0xFF, "ERROR", 0);
Felipe Balbi72246da2011-08-19 18:10:58 +03002735 dev_vdbg(dwc->dev, "Erratic Error\n");
Vijayavardhan Vennapusa8a011c92013-07-29 09:06:48 +05302736 dwc3_dump_reg_info(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +03002737 break;
2738 case DWC3_DEVICE_EVENT_CMD_CMPL:
2739 dev_vdbg(dwc->dev, "Command Complete\n");
2740 break;
2741 case DWC3_DEVICE_EVENT_OVERFLOW:
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +05302742 dbg_event(0xFF, "OVERFL", 0);
Felipe Balbi72246da2011-08-19 18:10:58 +03002743 dev_vdbg(dwc->dev, "Overflow\n");
Pavankumar Kondetid393e172012-06-12 16:07:29 +05302744 /*
2745 * Controllers prior to 2.30a revision has a bug where
2746 * Overflow Event may overwrite an unacknowledged event
2747 * in the event buffer. The severity of the issue depends
2748 * on the overwritten event type. Add a warning message
2749 * saying that an event is overwritten.
2750 *
2751 * TODO: In future we may need to see if we can re-enumerate
2752 * with host.
2753 */
2754 if (dwc->revision < DWC3_REVISION_230A)
2755 dev_warn(dwc->dev, "Unacknowledged event overwritten\n");
Felipe Balbi72246da2011-08-19 18:10:58 +03002756 break;
Pavankumar Kondeti33fe6f12012-06-12 16:21:46 +05302757 case DWC3_DEVICE_EVENT_VENDOR_DEV_TEST_LMP:
2758 /*
2759 * Controllers prior to 2.30a revision has a bug, due to which
2760 * a vendor device test LMP event can not be filtered. But
2761 * this event is not handled in the current code. This is a
2762 * special event and 8 bytes of data will follow the event.
2763 * Handling this event is tricky when event buffer is almost
2764 * full. Moreover this event will not occur in normal scenario
2765 * and can only happen with special hosts in testing scenarios.
2766 * Add a warning message to indicate that this event is received
2767 * which means that event buffer might have corrupted.
2768 */
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +05302769 dbg_event(0xFF, "TSTLMP", 0);
Pavankumar Kondeti33fe6f12012-06-12 16:21:46 +05302770 if (dwc->revision < DWC3_REVISION_230A)
2771 dev_warn(dwc->dev, "Vendor Device Test LMP Received\n");
2772 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03002773 default:
2774 dev_dbg(dwc->dev, "UNKNOWN IRQ %d\n", event->type);
2775 }
2776}
2777
2778static void dwc3_process_event_entry(struct dwc3 *dwc,
2779 const union dwc3_event *event)
2780{
2781 /* Endpoint IRQ, handle it and return early */
2782 if (event->type.is_devspec == 0) {
2783 /* depevt */
2784 return dwc3_endpoint_interrupt(dwc, &event->depevt);
2785 }
2786
2787 switch (event->type.type) {
2788 case DWC3_EVENT_TYPE_DEV:
2789 dwc3_gadget_interrupt(dwc, &event->devt);
2790 break;
2791 /* REVISIT what to do with Carkit and I2C events ? */
2792 default:
2793 dev_err(dwc->dev, "UNKNOWN IRQ type %d\n", event->raw);
2794 }
2795}
2796
2797static irqreturn_t dwc3_process_event_buf(struct dwc3 *dwc, u32 buf)
2798{
2799 struct dwc3_event_buffer *evt;
2800 int left;
2801 u32 count;
2802
2803 count = dwc3_readl(dwc->regs, DWC3_GEVNTCOUNT(buf));
2804 count &= DWC3_GEVNTCOUNT_MASK;
2805 if (!count)
2806 return IRQ_NONE;
2807
2808 evt = dwc->ev_buffs[buf];
2809 left = count;
2810
2811 while (left > 0) {
2812 union dwc3_event event;
2813
Felipe Balbid70d8442012-02-06 13:40:17 +02002814 event.raw = *(u32 *) (evt->buf + evt->lpos);
2815
Felipe Balbi72246da2011-08-19 18:10:58 +03002816 dwc3_process_event_entry(dwc, &event);
2817 /*
2818 * XXX we wrap around correctly to the next entry as almost all
2819 * entries are 4 bytes in size. There is one entry which has 12
2820 * bytes which is a regular entry followed by 8 bytes data. ATM
2821 * I don't know how things are organized if were get next to the
2822 * a boundary so I worry about that once we try to handle that.
2823 */
2824 evt->lpos = (evt->lpos + 4) % DWC3_EVENT_BUFFERS_SIZE;
2825 left -= 4;
2826
2827 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(buf), 4);
2828 }
2829
2830 return IRQ_HANDLED;
2831}
2832
2833static irqreturn_t dwc3_interrupt(int irq, void *_dwc)
2834{
2835 struct dwc3 *dwc = _dwc;
2836 int i;
2837 irqreturn_t ret = IRQ_NONE;
2838
2839 spin_lock(&dwc->lock);
2840
Felipe Balbi9f622b22011-10-12 10:31:04 +03002841 for (i = 0; i < dwc->num_event_buffers; i++) {
Felipe Balbi72246da2011-08-19 18:10:58 +03002842 irqreturn_t status;
2843
2844 status = dwc3_process_event_buf(dwc, i);
2845 if (status == IRQ_HANDLED)
2846 ret = status;
2847 }
2848
2849 spin_unlock(&dwc->lock);
2850
2851 return ret;
2852}
2853
2854/**
2855 * dwc3_gadget_init - Initializes gadget related registers
Paul Zimmerman1d046792012-02-15 18:56:56 -08002856 * @dwc: pointer to our controller context structure
Felipe Balbi72246da2011-08-19 18:10:58 +03002857 *
2858 * Returns 0 on success otherwise negative errno.
2859 */
2860int __devinit dwc3_gadget_init(struct dwc3 *dwc)
2861{
2862 u32 reg;
2863 int ret;
2864 int irq;
2865
2866 dwc->ctrl_req = dma_alloc_coherent(dwc->dev, sizeof(*dwc->ctrl_req),
2867 &dwc->ctrl_req_addr, GFP_KERNEL);
2868 if (!dwc->ctrl_req) {
2869 dev_err(dwc->dev, "failed to allocate ctrl request\n");
2870 ret = -ENOMEM;
2871 goto err0;
2872 }
2873
2874 dwc->ep0_trb = dma_alloc_coherent(dwc->dev, sizeof(*dwc->ep0_trb),
2875 &dwc->ep0_trb_addr, GFP_KERNEL);
2876 if (!dwc->ep0_trb) {
2877 dev_err(dwc->dev, "failed to allocate ep0 trb\n");
2878 ret = -ENOMEM;
2879 goto err1;
2880 }
2881
Felipe Balbib0791fb2012-05-04 12:58:14 +03002882 dwc->setup_buf = kzalloc(DWC3_EP0_BOUNCE_SIZE, GFP_KERNEL);
Felipe Balbi72246da2011-08-19 18:10:58 +03002883 if (!dwc->setup_buf) {
2884 dev_err(dwc->dev, "failed to allocate setup buffer\n");
2885 ret = -ENOMEM;
2886 goto err2;
2887 }
2888
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002889 dwc->ep0_bounce = dma_alloc_coherent(dwc->dev,
Felipe Balbib0791fb2012-05-04 12:58:14 +03002890 DWC3_EP0_BOUNCE_SIZE, &dwc->ep0_bounce_addr,
2891 GFP_KERNEL);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002892 if (!dwc->ep0_bounce) {
2893 dev_err(dwc->dev, "failed to allocate ep0 bounce buffer\n");
2894 ret = -ENOMEM;
2895 goto err3;
2896 }
2897
Felipe Balbi72246da2011-08-19 18:10:58 +03002898 dev_set_name(&dwc->gadget.dev, "gadget");
2899
2900 dwc->gadget.ops = &dwc3_gadget_ops;
Manu Gautama7b082a2012-11-06 09:50:09 +05302901 dwc->gadget.max_speed = USB_SPEED_SUPER;
Felipe Balbi72246da2011-08-19 18:10:58 +03002902 dwc->gadget.speed = USB_SPEED_UNKNOWN;
2903 dwc->gadget.dev.parent = dwc->dev;
Felipe Balbieeb720f2011-11-28 12:46:59 +02002904 dwc->gadget.sg_supported = true;
Felipe Balbi72246da2011-08-19 18:10:58 +03002905
2906 dma_set_coherent_mask(&dwc->gadget.dev, dwc->dev->coherent_dma_mask);
2907
2908 dwc->gadget.dev.dma_parms = dwc->dev->dma_parms;
2909 dwc->gadget.dev.dma_mask = dwc->dev->dma_mask;
2910 dwc->gadget.dev.release = dwc3_gadget_release;
2911 dwc->gadget.name = "dwc3-gadget";
2912
2913 /*
2914 * REVISIT: Here we should clear all pending IRQs to be
2915 * sure we're starting from a well known location.
2916 */
2917
2918 ret = dwc3_gadget_init_endpoints(dwc);
2919 if (ret)
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002920 goto err4;
Felipe Balbi72246da2011-08-19 18:10:58 +03002921
2922 irq = platform_get_irq(to_platform_device(dwc->dev), 0);
2923
2924 ret = request_irq(irq, dwc3_interrupt, IRQF_SHARED,
2925 "dwc3", dwc);
2926 if (ret) {
2927 dev_err(dwc->dev, "failed to request irq #%d --> %d\n",
2928 irq, ret);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002929 goto err5;
Felipe Balbi72246da2011-08-19 18:10:58 +03002930 }
2931
Sebastian Andrzej Siewiorbb8b8a32011-09-13 17:54:39 +02002932 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2933 reg |= DWC3_DCFG_LPM_CAP;
2934 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
2935
Felipe Balbi72246da2011-08-19 18:10:58 +03002936 /* Enable all but Start and End of Frame IRQs */
Pavankumar Kondeti33fe6f12012-06-12 16:21:46 +05302937 reg = (DWC3_DEVTEN_EVNTOVERFLOWEN |
Felipe Balbi72246da2011-08-19 18:10:58 +03002938 DWC3_DEVTEN_CMDCMPLTEN |
2939 DWC3_DEVTEN_ERRTICERREN |
2940 DWC3_DEVTEN_WKUPEVTEN |
2941 DWC3_DEVTEN_ULSTCNGEN |
2942 DWC3_DEVTEN_CONNECTDONEEN |
2943 DWC3_DEVTEN_USBRSTEN |
2944 DWC3_DEVTEN_DISCONNEVTEN);
2945 dwc3_writel(dwc->regs, DWC3_DEVTEN, reg);
2946
Paul Zimmerman88df4272012-04-27 13:10:52 +03002947 /* Enable USB2 LPM and automatic phy suspend only on recent versions */
2948 if (dwc->revision >= DWC3_REVISION_194A) {
2949 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2950 reg |= DWC3_DCFG_LPM_CAP;
2951 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
2952
2953 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2954 reg &= ~(DWC3_DCTL_HIRD_THRES_MASK | DWC3_DCTL_L1_HIBER_EN);
2955
2956 /* TODO: This should be configurable */
Pratyush Anandd69dcdd2012-07-02 10:21:52 +05302957 reg |= DWC3_DCTL_HIRD_THRES(28);
Paul Zimmerman88df4272012-04-27 13:10:52 +03002958
2959 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2960
Vijayavardhan Vennapusa47786ef2013-11-11 13:03:19 +05302961 /*
2962 * Clear autosuspend bit in dwc3 register for USB2. It will be
2963 * enabled before setting run/stop bit.
2964 */
2965 dwc3_gadget_usb2_phy_suspend(dwc, false);
Vijayavardhan Vennapusaba79f6b2013-07-30 13:39:52 +05302966 dwc3_gadget_usb3_phy_suspend(dwc, true);
Paul Zimmerman88df4272012-04-27 13:10:52 +03002967 }
2968
Felipe Balbi72246da2011-08-19 18:10:58 +03002969 ret = device_register(&dwc->gadget.dev);
2970 if (ret) {
2971 dev_err(dwc->dev, "failed to register gadget device\n");
2972 put_device(&dwc->gadget.dev);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002973 goto err6;
Felipe Balbi72246da2011-08-19 18:10:58 +03002974 }
2975
2976 ret = usb_add_gadget_udc(dwc->dev, &dwc->gadget);
2977 if (ret) {
2978 dev_err(dwc->dev, "failed to register udc\n");
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002979 goto err7;
Felipe Balbi72246da2011-08-19 18:10:58 +03002980 }
2981
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +02002982 if (dwc->dotg) {
2983 /* dwc3 otg driver is active (DRD mode + SRPSupport=1) */
2984 ret = otg_set_peripheral(&dwc->dotg->otg, &dwc->gadget);
2985 if (ret) {
2986 dev_err(dwc->dev, "failed to set peripheral to otg\n");
2987 goto err7;
2988 }
Manu Gautamb5067272012-07-02 09:53:41 +05302989 } else {
2990 pm_runtime_no_callbacks(&dwc->gadget.dev);
2991 pm_runtime_set_active(&dwc->gadget.dev);
2992 pm_runtime_enable(&dwc->gadget.dev);
2993 pm_runtime_get(&dwc->gadget.dev);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +02002994 }
2995
Felipe Balbi72246da2011-08-19 18:10:58 +03002996 return 0;
2997
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002998err7:
Felipe Balbi72246da2011-08-19 18:10:58 +03002999 device_unregister(&dwc->gadget.dev);
3000
Felipe Balbi5812b1c2011-08-27 22:07:53 +03003001err6:
Felipe Balbi72246da2011-08-19 18:10:58 +03003002 dwc3_writel(dwc->regs, DWC3_DEVTEN, 0x00);
3003 free_irq(irq, dwc);
3004
Felipe Balbi5812b1c2011-08-27 22:07:53 +03003005err5:
Felipe Balbi72246da2011-08-19 18:10:58 +03003006 dwc3_gadget_free_endpoints(dwc);
3007
Felipe Balbi5812b1c2011-08-27 22:07:53 +03003008err4:
Felipe Balbib0791fb2012-05-04 12:58:14 +03003009 dma_free_coherent(dwc->dev, DWC3_EP0_BOUNCE_SIZE,
3010 dwc->ep0_bounce, dwc->ep0_bounce_addr);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03003011
Felipe Balbi72246da2011-08-19 18:10:58 +03003012err3:
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02003013 kfree(dwc->setup_buf);
Felipe Balbi72246da2011-08-19 18:10:58 +03003014
3015err2:
3016 dma_free_coherent(dwc->dev, sizeof(*dwc->ep0_trb),
3017 dwc->ep0_trb, dwc->ep0_trb_addr);
3018
3019err1:
3020 dma_free_coherent(dwc->dev, sizeof(*dwc->ctrl_req),
3021 dwc->ctrl_req, dwc->ctrl_req_addr);
3022
3023err0:
3024 return ret;
3025}
3026
3027void dwc3_gadget_exit(struct dwc3 *dwc)
3028{
3029 int irq;
Felipe Balbi72246da2011-08-19 18:10:58 +03003030
Manu Gautamb5067272012-07-02 09:53:41 +05303031 if (dwc->dotg) {
3032 pm_runtime_put(&dwc->gadget.dev);
3033 pm_runtime_disable(&dwc->gadget.dev);
3034 }
3035
Felipe Balbi72246da2011-08-19 18:10:58 +03003036 usb_del_gadget_udc(&dwc->gadget);
3037 irq = platform_get_irq(to_platform_device(dwc->dev), 0);
3038
3039 dwc3_writel(dwc->regs, DWC3_DEVTEN, 0x00);
3040 free_irq(irq, dwc);
3041
Felipe Balbi72246da2011-08-19 18:10:58 +03003042 dwc3_gadget_free_endpoints(dwc);
3043
Felipe Balbib0791fb2012-05-04 12:58:14 +03003044 dma_free_coherent(dwc->dev, DWC3_EP0_BOUNCE_SIZE,
3045 dwc->ep0_bounce, dwc->ep0_bounce_addr);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03003046
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02003047 kfree(dwc->setup_buf);
Felipe Balbi72246da2011-08-19 18:10:58 +03003048
3049 dma_free_coherent(dwc->dev, sizeof(*dwc->ep0_trb),
3050 dwc->ep0_trb, dwc->ep0_trb_addr);
3051
3052 dma_free_coherent(dwc->dev, sizeof(*dwc->ctrl_req),
3053 dwc->ctrl_req, dwc->ctrl_req_addr);
3054
3055 device_unregister(&dwc->gadget.dev);
3056}