blob: 8fa4774446f739258a4ee0006347bfaba9ddbc94 [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 &&
232 (usb_endpoint_xfer_bulk(dep->endpoint.desc)
233 || usb_endpoint_xfer_isoc(dep->endpoint.desc)))
234 /*
235 * Allocate 3KB fifo size for bulk and isochronous TX
236 * endpoints irrespective of speed. For interrupt
237 * endpoint, allocate fifo size of endpoint maxpacket.
238 */
239 tmp = 3 * (1024 + mdwidth);
240
Felipe Balbi457e84b2012-01-18 18:04:09 +0200241 tmp += mdwidth;
242
243 fifo_size = DIV_ROUND_UP(tmp, mdwidth);
Felipe Balbi2e81c362012-02-02 13:01:12 +0200244
Felipe Balbi457e84b2012-01-18 18:04:09 +0200245 fifo_size |= (last_fifo_depth << 16);
246
247 dev_vdbg(dwc->dev, "%s: Fifo Addr %04x Size %d\n",
248 dep->name, last_fifo_depth, fifo_size & 0xffff);
249
Vijayavardhan Vennapusafc3db602013-08-20 17:54:54 +0530250 last_fifo_depth += (fifo_size & 0xffff);
251 if (dwc->tx_fifo_size &&
252 (last_fifo_depth >= dwc->tx_fifo_size)) {
253 /*
254 * Fifo size allocated exceeded available RAM size.
255 * Hence return error.
256 */
257 dev_err(dwc->dev, "Fifosize(%d) > available RAM(%d)\n",
258 last_fifo_depth, dwc->tx_fifo_size);
259 return -ENOMEM;
260 }
261
Felipe Balbi457e84b2012-01-18 18:04:09 +0200262 dwc3_writel(dwc->regs, DWC3_GTXFIFOSIZ(fifo_number),
263 fifo_size);
264
Felipe Balbi457e84b2012-01-18 18:04:09 +0200265 }
266
267 return 0;
268}
269
Felipe Balbi72246da2011-08-19 18:10:58 +0300270void dwc3_gadget_giveback(struct dwc3_ep *dep, struct dwc3_request *req,
271 int status)
272{
273 struct dwc3 *dwc = dep->dwc;
274
275 if (req->queued) {
Manu Gautam55d34222012-12-19 16:49:47 +0530276 req->queued = false;
277
Felipe Balbieeb720f2011-11-28 12:46:59 +0200278 if (req->request.num_mapped_sgs)
279 dep->busy_slot += req->request.num_mapped_sgs;
280 else
281 dep->busy_slot++;
282
Felipe Balbi72246da2011-08-19 18:10:58 +0300283 /*
284 * Skip LINK TRB. We can't use req->trb and check for
285 * DWC3_TRBCTL_LINK_TRB because it points the TRB we just
286 * completed (not the LINK TRB).
287 */
288 if (((dep->busy_slot & DWC3_TRB_MASK) == DWC3_TRB_NUM - 1) &&
Ido Shayevitz57cdac12012-03-12 20:25:24 +0200289 usb_endpoint_xfer_isoc(dep->endpoint.desc))
Felipe Balbi72246da2011-08-19 18:10:58 +0300290 dep->busy_slot++;
291 }
292 list_del(&req->list);
Felipe Balbieeb720f2011-11-28 12:46:59 +0200293 req->trb = NULL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300294
295 if (req->request.status == -EINPROGRESS)
296 req->request.status = status;
297
Pratyush Anand8d7bf592012-08-10 13:42:16 +0530298 if (dwc->ep0_bounced && dep->number == 0)
299 dwc->ep0_bounced = false;
300 else
301 usb_gadget_unmap_request(&dwc->gadget, &req->request,
302 req->direction);
Felipe Balbi72246da2011-08-19 18:10:58 +0300303
304 dev_dbg(dwc->dev, "request %p from %s completed %d/%d ===> %d\n",
305 req, dep->name, req->request.actual,
306 req->request.length, status);
307
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +0530308 dbg_done(dep->number, req->request.actual, req->request.status);
Felipe Balbi72246da2011-08-19 18:10:58 +0300309 spin_unlock(&dwc->lock);
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +0200310 req->request.complete(&dep->endpoint, &req->request);
Felipe Balbi72246da2011-08-19 18:10:58 +0300311 spin_lock(&dwc->lock);
312}
313
314static const char *dwc3_gadget_ep_cmd_string(u8 cmd)
315{
316 switch (cmd) {
317 case DWC3_DEPCMD_DEPSTARTCFG:
318 return "Start New Configuration";
319 case DWC3_DEPCMD_ENDTRANSFER:
320 return "End Transfer";
321 case DWC3_DEPCMD_UPDATETRANSFER:
322 return "Update Transfer";
323 case DWC3_DEPCMD_STARTTRANSFER:
324 return "Start Transfer";
325 case DWC3_DEPCMD_CLEARSTALL:
326 return "Clear Stall";
327 case DWC3_DEPCMD_SETSTALL:
328 return "Set Stall";
Paul Zimmerman88df4272012-04-27 13:10:52 +0300329 case DWC3_DEPCMD_GETEPSTATE:
330 return "Get Endpoint State";
Felipe Balbi72246da2011-08-19 18:10:58 +0300331 case DWC3_DEPCMD_SETTRANSFRESOURCE:
332 return "Set Endpoint Transfer Resource";
333 case DWC3_DEPCMD_SETEPCONFIG:
334 return "Set Endpoint Configuration";
335 default:
336 return "UNKNOWN command";
337 }
338}
339
Felipe Balbi573c2762012-04-24 16:19:11 +0300340int dwc3_send_gadget_generic_command(struct dwc3 *dwc, int cmd, u32 param)
341{
342 u32 timeout = 500;
343 u32 reg;
Vijayavardhan Vennapusaba79f6b2013-07-30 13:39:52 +0530344 bool hsphy_suspend_enabled;
345 int ret;
346
347 /* Commands to controller will work only if PHY is not suspended */
348 hsphy_suspend_enabled = (dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0)) &
349 DWC3_GUSB2PHYCFG_SUSPHY);
350
351 /* Disable suspend of the USB2 PHY */
352 if (hsphy_suspend_enabled)
353 dwc3_gadget_usb2_phy_suspend(dwc, false);
Felipe Balbi573c2762012-04-24 16:19:11 +0300354
355 dwc3_writel(dwc->regs, DWC3_DGCMDPAR, param);
356 dwc3_writel(dwc->regs, DWC3_DGCMD, cmd | DWC3_DGCMD_CMDACT);
357
358 do {
359 reg = dwc3_readl(dwc->regs, DWC3_DGCMD);
360 if (!(reg & DWC3_DGCMD_CMDACT)) {
361 dev_vdbg(dwc->dev, "Command Complete --> %d\n",
362 DWC3_DGCMD_STATUS(reg));
Vijayavardhan Vennapusaba79f6b2013-07-30 13:39:52 +0530363 ret = 0;
364 break;
Felipe Balbi573c2762012-04-24 16:19:11 +0300365 }
366
367 /*
368 * We can't sleep here, because it's also called from
369 * interrupt context.
370 */
371 timeout--;
Vijayavardhan Vennapusaba79f6b2013-07-30 13:39:52 +0530372 if (!timeout) {
373 ret = -ETIMEDOUT;
374 break;
375 }
Felipe Balbi573c2762012-04-24 16:19:11 +0300376 udelay(1);
377 } while (1);
Vijayavardhan Vennapusaba79f6b2013-07-30 13:39:52 +0530378
379 /* Enable suspend of the USB2 PHY */
380 if (hsphy_suspend_enabled)
381 dwc3_gadget_usb2_phy_suspend(dwc, true);
382
383 return ret;
Felipe Balbi573c2762012-04-24 16:19:11 +0300384}
385
Felipe Balbi72246da2011-08-19 18:10:58 +0300386int dwc3_send_gadget_ep_cmd(struct dwc3 *dwc, unsigned ep,
387 unsigned cmd, struct dwc3_gadget_ep_cmd_params *params)
388{
389 struct dwc3_ep *dep = dwc->eps[ep];
Sebastian Andrzej Siewior61d58242011-08-29 16:46:38 +0200390 u32 timeout = 500;
Felipe Balbi72246da2011-08-19 18:10:58 +0300391 u32 reg;
Vijayavardhan Vennapusaba79f6b2013-07-30 13:39:52 +0530392 bool hsphy_suspend_enabled;
393 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300394
395 dev_vdbg(dwc->dev, "%s: cmd '%s' params %08x %08x %08x\n",
396 dep->name,
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300397 dwc3_gadget_ep_cmd_string(cmd), params->param0,
398 params->param1, params->param2);
Felipe Balbi72246da2011-08-19 18:10:58 +0300399
Vijayavardhan Vennapusaba79f6b2013-07-30 13:39:52 +0530400 /* Commands to controller will work only if PHY is not suspended */
401 hsphy_suspend_enabled = (dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0)) &
402 DWC3_GUSB2PHYCFG_SUSPHY);
403
404 /* Disable suspend of the USB2 PHY */
405 if (hsphy_suspend_enabled)
406 dwc3_gadget_usb2_phy_suspend(dwc, false);
407
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300408 dwc3_writel(dwc->regs, DWC3_DEPCMDPAR0(ep), params->param0);
409 dwc3_writel(dwc->regs, DWC3_DEPCMDPAR1(ep), params->param1);
410 dwc3_writel(dwc->regs, DWC3_DEPCMDPAR2(ep), params->param2);
Felipe Balbi72246da2011-08-19 18:10:58 +0300411
412 dwc3_writel(dwc->regs, DWC3_DEPCMD(ep), cmd | DWC3_DEPCMD_CMDACT);
413 do {
414 reg = dwc3_readl(dwc->regs, DWC3_DEPCMD(ep));
415 if (!(reg & DWC3_DEPCMD_CMDACT)) {
Felipe Balbi164f6e12011-08-27 20:29:58 +0300416 dev_vdbg(dwc->dev, "Command Complete --> %d\n",
417 DWC3_DEPCMD_STATUS(reg));
Vijayavardhan Vennapusa91ba6532013-01-30 17:35:45 +0530418 /* SW issues START TRANSFER command to isochronous ep
419 * with future frame interval. If future interval time
420 * has already passed when core recieves command, core
421 * will respond with an error(bit13 in Command complete
422 * event. Hence return error in this case.
423 */
424 if (reg & 0x2000)
Vijayavardhan Vennapusaba79f6b2013-07-30 13:39:52 +0530425 ret = -EAGAIN;
Vijayavardhan Vennapusa91ba6532013-01-30 17:35:45 +0530426 else
Vijayavardhan Vennapusaba79f6b2013-07-30 13:39:52 +0530427 ret = 0;
428 break;
Felipe Balbi72246da2011-08-19 18:10:58 +0300429 }
430
431 /*
Felipe Balbi72246da2011-08-19 18:10:58 +0300432 * We can't sleep here, because it is also called from
433 * interrupt context.
434 */
435 timeout--;
Vijayavardhan Vennapusaba79f6b2013-07-30 13:39:52 +0530436 if (!timeout) {
437 ret = -ETIMEDOUT;
438 break;
439 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300440
Sebastian Andrzej Siewior61d58242011-08-29 16:46:38 +0200441 udelay(1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300442 } while (1);
Vijayavardhan Vennapusaba79f6b2013-07-30 13:39:52 +0530443
444 /* Enable suspend of the USB2 PHY */
445 if (hsphy_suspend_enabled)
446 dwc3_gadget_usb2_phy_suspend(dwc, true);
447
448 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300449}
450
Ido Shayevitzfa65a582012-06-06 14:39:54 +0300451dma_addr_t dwc3_trb_dma_offset(struct dwc3_ep *dep,
Felipe Balbif6bafc62012-02-06 11:04:53 +0200452 struct dwc3_trb *trb)
Felipe Balbi72246da2011-08-19 18:10:58 +0300453{
Paul Zimmermanc439ef82011-09-30 10:58:45 +0300454 u32 offset = (char *) trb - (char *) dep->trb_pool;
Felipe Balbi72246da2011-08-19 18:10:58 +0300455
456 return dep->trb_pool_dma + offset;
457}
458
459static int dwc3_alloc_trb_pool(struct dwc3_ep *dep)
460{
461 struct dwc3 *dwc = dep->dwc;
462
463 if (dep->trb_pool)
464 return 0;
465
466 if (dep->number == 0 || dep->number == 1)
467 return 0;
468
469 dep->trb_pool = dma_alloc_coherent(dwc->dev,
470 sizeof(struct dwc3_trb) * DWC3_TRB_NUM,
471 &dep->trb_pool_dma, GFP_KERNEL);
472 if (!dep->trb_pool) {
473 dev_err(dep->dwc->dev, "failed to allocate trb pool for %s\n",
474 dep->name);
475 return -ENOMEM;
476 }
477
478 return 0;
479}
480
481static void dwc3_free_trb_pool(struct dwc3_ep *dep)
482{
483 struct dwc3 *dwc = dep->dwc;
484
485 dma_free_coherent(dwc->dev, sizeof(struct dwc3_trb) * DWC3_TRB_NUM,
486 dep->trb_pool, dep->trb_pool_dma);
487
488 dep->trb_pool = NULL;
489 dep->trb_pool_dma = 0;
490}
491
492static int dwc3_gadget_start_config(struct dwc3 *dwc, struct dwc3_ep *dep)
493{
494 struct dwc3_gadget_ep_cmd_params params;
495 u32 cmd;
496
497 memset(&params, 0x00, sizeof(params));
498
499 if (dep->number != 1) {
500 cmd = DWC3_DEPCMD_DEPSTARTCFG;
501 /* XferRscIdx == 0 for ep0 and 2 for the remaining */
Paul Zimmermanb23c8432011-09-30 10:58:42 +0300502 if (dep->number > 1) {
503 if (dwc->start_config_issued)
504 return 0;
505 dwc->start_config_issued = true;
Felipe Balbi72246da2011-08-19 18:10:58 +0300506 cmd |= DWC3_DEPCMD_PARAM(2);
Paul Zimmermanb23c8432011-09-30 10:58:42 +0300507 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300508
509 return dwc3_send_gadget_ep_cmd(dwc, 0, cmd, &params);
510 }
511
512 return 0;
513}
514
515static int dwc3_gadget_set_ep_config(struct dwc3 *dwc, struct dwc3_ep *dep,
Felipe Balbic90bfae2011-11-29 13:11:21 +0200516 const struct usb_endpoint_descriptor *desc,
Felipe Balbi07e0ee82012-07-16 14:08:16 +0300517 const struct usb_ss_ep_comp_descriptor *comp_desc,
518 bool ignore)
Felipe Balbi72246da2011-08-19 18:10:58 +0300519{
520 struct dwc3_gadget_ep_cmd_params params;
521
522 memset(&params, 0x00, sizeof(params));
523
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300524 params.param0 = DWC3_DEPCFG_EP_TYPE(usb_endpoint_type(desc))
Chanho Parkf0ee6062012-08-31 16:54:07 +0900525 | DWC3_DEPCFG_MAX_PACKET_SIZE(usb_endpoint_maxp(desc));
526
527 /* Burst size is only needed in SuperSpeed mode */
528 if (dwc->gadget.speed == USB_SPEED_SUPER) {
529 u32 burst = dep->endpoint.maxburst - 1;
530
531 params.param0 |= DWC3_DEPCFG_BURST_SIZE(burst);
532 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300533
Felipe Balbi07e0ee82012-07-16 14:08:16 +0300534 if (ignore)
535 params.param0 |= DWC3_DEPCFG_IGN_SEQ_NUM;
Felipe Balbi72246da2011-08-19 18:10:58 +0300536
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300537 params.param1 = DWC3_DEPCFG_XFER_COMPLETE_EN
538 | DWC3_DEPCFG_XFER_NOT_READY_EN;
Felipe Balbi72246da2011-08-19 18:10:58 +0300539
Felipe Balbi18b7ede2012-01-02 13:35:41 +0200540 if (usb_ss_max_streams(comp_desc) && usb_endpoint_xfer_bulk(desc)) {
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300541 params.param1 |= DWC3_DEPCFG_STREAM_CAPABLE
542 | DWC3_DEPCFG_STREAM_EVENT_EN;
Felipe Balbi879631a2011-09-30 10:58:47 +0300543 dep->stream_capable = true;
544 }
545
Felipe Balbi72246da2011-08-19 18:10:58 +0300546 if (usb_endpoint_xfer_isoc(desc))
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300547 params.param1 |= DWC3_DEPCFG_XFER_IN_PROGRESS_EN;
Felipe Balbi72246da2011-08-19 18:10:58 +0300548
549 /*
550 * We are doing 1:1 mapping for endpoints, meaning
551 * Physical Endpoints 2 maps to Logical Endpoint 2 and
552 * so on. We consider the direction bit as part of the physical
553 * endpoint number. So USB endpoint 0x81 is 0x03.
554 */
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300555 params.param1 |= DWC3_DEPCFG_EP_NUMBER(dep->number);
Felipe Balbi72246da2011-08-19 18:10:58 +0300556
557 /*
558 * We must use the lower 16 TX FIFOs even though
559 * HW might have more
560 */
561 if (dep->direction)
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300562 params.param0 |= DWC3_DEPCFG_FIFO_NUMBER(dep->number >> 1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300563
564 if (desc->bInterval) {
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300565 params.param1 |= DWC3_DEPCFG_BINTERVAL_M1(desc->bInterval - 1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300566 dep->interval = 1 << (desc->bInterval - 1);
567 }
568
569 return dwc3_send_gadget_ep_cmd(dwc, dep->number,
570 DWC3_DEPCMD_SETEPCONFIG, &params);
571}
572
573static int dwc3_gadget_set_xfer_resource(struct dwc3 *dwc, struct dwc3_ep *dep)
574{
575 struct dwc3_gadget_ep_cmd_params params;
576
577 memset(&params, 0x00, sizeof(params));
578
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300579 params.param0 = DWC3_DEPXFERCFG_NUM_XFER_RES(1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300580
581 return dwc3_send_gadget_ep_cmd(dwc, dep->number,
582 DWC3_DEPCMD_SETTRANSFRESOURCE, &params);
583}
584
585/**
586 * __dwc3_gadget_ep_enable - Initializes a HW endpoint
587 * @dep: endpoint to be initialized
588 * @desc: USB Endpoint Descriptor
589 *
590 * Caller should take care of locking
591 */
592static int __dwc3_gadget_ep_enable(struct dwc3_ep *dep,
Felipe Balbic90bfae2011-11-29 13:11:21 +0200593 const struct usb_endpoint_descriptor *desc,
Felipe Balbi07e0ee82012-07-16 14:08:16 +0300594 const struct usb_ss_ep_comp_descriptor *comp_desc,
595 bool ignore)
Felipe Balbi72246da2011-08-19 18:10:58 +0300596{
597 struct dwc3 *dwc = dep->dwc;
598 u32 reg;
599 int ret = -ENOMEM;
600
601 if (!(dep->flags & DWC3_EP_ENABLED)) {
602 ret = dwc3_gadget_start_config(dwc, dep);
603 if (ret)
604 return ret;
605 }
606
Felipe Balbi07e0ee82012-07-16 14:08:16 +0300607 ret = dwc3_gadget_set_ep_config(dwc, dep, desc, comp_desc, ignore);
Felipe Balbi72246da2011-08-19 18:10:58 +0300608 if (ret)
609 return ret;
610
611 if (!(dep->flags & DWC3_EP_ENABLED)) {
Felipe Balbif6bafc62012-02-06 11:04:53 +0200612 struct dwc3_trb *trb_st_hw;
613 struct dwc3_trb *trb_link;
Felipe Balbi72246da2011-08-19 18:10:58 +0300614
615 ret = dwc3_gadget_set_xfer_resource(dwc, dep);
616 if (ret)
617 return ret;
618
Ido Shayevitz57cdac12012-03-12 20:25:24 +0200619 dep->endpoint.desc = desc;
Felipe Balbic90bfae2011-11-29 13:11:21 +0200620 dep->comp_desc = comp_desc;
Felipe Balbi72246da2011-08-19 18:10:58 +0300621 dep->type = usb_endpoint_type(desc);
622 dep->flags |= DWC3_EP_ENABLED;
623
624 reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
625 reg |= DWC3_DALEPENA_EP(dep->number);
626 dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
627
628 if (!usb_endpoint_xfer_isoc(desc))
629 return 0;
630
631 memset(&trb_link, 0, sizeof(trb_link));
632
Paul Zimmerman1d046792012-02-15 18:56:56 -0800633 /* Link TRB for ISOC. The HWO bit is never reset */
Felipe Balbi72246da2011-08-19 18:10:58 +0300634 trb_st_hw = &dep->trb_pool[0];
635
Felipe Balbif6bafc62012-02-06 11:04:53 +0200636 trb_link = &dep->trb_pool[DWC3_TRB_NUM - 1];
Felipe Balbi72246da2011-08-19 18:10:58 +0300637
Felipe Balbif6bafc62012-02-06 11:04:53 +0200638 trb_link->bpl = lower_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw));
639 trb_link->bph = upper_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw));
640 trb_link->ctrl |= DWC3_TRBCTL_LINK_TRB;
641 trb_link->ctrl |= DWC3_TRB_CTRL_HWO;
Felipe Balbi72246da2011-08-19 18:10:58 +0300642 }
643
644 return 0;
645}
646
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200647static void dwc3_stop_active_transfer(struct dwc3 *dwc, u32 epnum);
648static void dwc3_remove_requests(struct dwc3 *dwc, struct dwc3_ep *dep)
Felipe Balbi72246da2011-08-19 18:10:58 +0300649{
650 struct dwc3_request *req;
651
Felipe Balbib129eb72012-02-17 12:10:04 +0200652 if (!list_empty(&dep->req_queued)) {
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200653 dwc3_stop_active_transfer(dwc, dep->number);
654
Pratyush Anande67fdeb2012-07-06 15:19:10 +0530655 /* - giveback all requests to gadget driver */
Pratyush Anand110ff602012-06-15 11:54:36 +0530656 while (!list_empty(&dep->req_queued)) {
657 req = next_request(&dep->req_queued);
658
659 dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
660 }
Felipe Balbib129eb72012-02-17 12:10:04 +0200661 }
662
Felipe Balbi72246da2011-08-19 18:10:58 +0300663 while (!list_empty(&dep->request_list)) {
664 req = next_request(&dep->request_list);
665
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200666 dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
Felipe Balbi72246da2011-08-19 18:10:58 +0300667 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300668}
669
670/**
671 * __dwc3_gadget_ep_disable - Disables a HW endpoint
672 * @dep: the endpoint to disable
673 *
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200674 * This function also removes requests which are currently processed ny the
675 * hardware and those which are not yet scheduled.
676 * Caller should take care of locking.
Felipe Balbi72246da2011-08-19 18:10:58 +0300677 */
Felipe Balbi72246da2011-08-19 18:10:58 +0300678static int __dwc3_gadget_ep_disable(struct dwc3_ep *dep)
679{
680 struct dwc3 *dwc = dep->dwc;
681 u32 reg;
682
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200683 dwc3_remove_requests(dwc, dep);
Felipe Balbi72246da2011-08-19 18:10:58 +0300684
685 reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
686 reg &= ~DWC3_DALEPENA_EP(dep->number);
687 dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
688
Felipe Balbi879631a2011-09-30 10:58:47 +0300689 dep->stream_capable = false;
Ido Shayevitzf9c56cd2012-02-08 13:56:48 +0200690 dep->endpoint.desc = NULL;
Felipe Balbic90bfae2011-11-29 13:11:21 +0200691 dep->comp_desc = NULL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300692 dep->type = 0;
Felipe Balbi879631a2011-09-30 10:58:47 +0300693 dep->flags = 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300694
695 return 0;
696}
697
698/* -------------------------------------------------------------------------- */
699
700static int dwc3_gadget_ep0_enable(struct usb_ep *ep,
701 const struct usb_endpoint_descriptor *desc)
702{
703 return -EINVAL;
704}
705
706static int dwc3_gadget_ep0_disable(struct usb_ep *ep)
707{
708 return -EINVAL;
709}
710
711/* -------------------------------------------------------------------------- */
712
713static int dwc3_gadget_ep_enable(struct usb_ep *ep,
714 const struct usb_endpoint_descriptor *desc)
715{
716 struct dwc3_ep *dep;
717 struct dwc3 *dwc;
718 unsigned long flags;
719 int ret;
720
721 if (!ep || !desc || desc->bDescriptorType != USB_DT_ENDPOINT) {
722 pr_debug("dwc3: invalid parameters\n");
723 return -EINVAL;
724 }
725
726 if (!desc->wMaxPacketSize) {
727 pr_debug("dwc3: missing wMaxPacketSize\n");
728 return -EINVAL;
729 }
730
731 dep = to_dwc3_ep(ep);
732 dwc = dep->dwc;
733
Felipe Balbi14395072012-08-15 12:28:29 +0300734 if (dep->flags & DWC3_EP_ENABLED) {
735 dev_WARN_ONCE(dwc->dev, true, "%s is already enabled\n",
736 dep->name);
737 return 0;
738 }
739
Felipe Balbi72246da2011-08-19 18:10:58 +0300740 switch (usb_endpoint_type(desc)) {
741 case USB_ENDPOINT_XFER_CONTROL:
Anton Tikhomirov27a78d62012-02-23 15:38:46 +0900742 strlcat(dep->name, "-control", sizeof(dep->name));
Felipe Balbi72246da2011-08-19 18:10:58 +0300743 break;
744 case USB_ENDPOINT_XFER_ISOC:
Anton Tikhomirov27a78d62012-02-23 15:38:46 +0900745 strlcat(dep->name, "-isoc", sizeof(dep->name));
Felipe Balbi72246da2011-08-19 18:10:58 +0300746 break;
747 case USB_ENDPOINT_XFER_BULK:
Anton Tikhomirov27a78d62012-02-23 15:38:46 +0900748 strlcat(dep->name, "-bulk", sizeof(dep->name));
Felipe Balbi72246da2011-08-19 18:10:58 +0300749 break;
750 case USB_ENDPOINT_XFER_INT:
Anton Tikhomirov27a78d62012-02-23 15:38:46 +0900751 strlcat(dep->name, "-int", sizeof(dep->name));
Felipe Balbi72246da2011-08-19 18:10:58 +0300752 break;
753 default:
754 dev_err(dwc->dev, "invalid endpoint transfer type\n");
755 }
756
Felipe Balbi72246da2011-08-19 18:10:58 +0300757 dev_vdbg(dwc->dev, "Enabling %s\n", dep->name);
758
759 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi07e0ee82012-07-16 14:08:16 +0300760 ret = __dwc3_gadget_ep_enable(dep, desc, ep->comp_desc, false);
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +0530761 dbg_event(dep->number, "ENABLE", ret);
Felipe Balbi72246da2011-08-19 18:10:58 +0300762 spin_unlock_irqrestore(&dwc->lock, flags);
763
764 return ret;
765}
766
767static int dwc3_gadget_ep_disable(struct usb_ep *ep)
768{
769 struct dwc3_ep *dep;
770 struct dwc3 *dwc;
771 unsigned long flags;
772 int ret;
773
774 if (!ep) {
775 pr_debug("dwc3: invalid parameters\n");
776 return -EINVAL;
777 }
778
779 dep = to_dwc3_ep(ep);
780 dwc = dep->dwc;
781
782 if (!(dep->flags & DWC3_EP_ENABLED)) {
783 dev_WARN_ONCE(dwc->dev, true, "%s is already disabled\n",
784 dep->name);
785 return 0;
786 }
787
788 snprintf(dep->name, sizeof(dep->name), "ep%d%s",
789 dep->number >> 1,
790 (dep->number & 1) ? "in" : "out");
791
792 spin_lock_irqsave(&dwc->lock, flags);
793 ret = __dwc3_gadget_ep_disable(dep);
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +0530794 dbg_event(dep->number, "DISABLE", ret);
Felipe Balbi72246da2011-08-19 18:10:58 +0300795 spin_unlock_irqrestore(&dwc->lock, flags);
796
797 return ret;
798}
799
800static struct usb_request *dwc3_gadget_ep_alloc_request(struct usb_ep *ep,
801 gfp_t gfp_flags)
802{
803 struct dwc3_request *req;
804 struct dwc3_ep *dep = to_dwc3_ep(ep);
805 struct dwc3 *dwc = dep->dwc;
806
807 req = kzalloc(sizeof(*req), gfp_flags);
808 if (!req) {
809 dev_err(dwc->dev, "not enough memory\n");
810 return NULL;
811 }
812
813 req->epnum = dep->number;
814 req->dep = dep;
Felipe Balbi72246da2011-08-19 18:10:58 +0300815
816 return &req->request;
817}
818
819static void dwc3_gadget_ep_free_request(struct usb_ep *ep,
820 struct usb_request *request)
821{
822 struct dwc3_request *req = to_dwc3_request(request);
823
824 kfree(req);
825}
826
Felipe Balbic71fc372011-11-22 11:37:34 +0200827/**
828 * dwc3_prepare_one_trb - setup one TRB from one request
829 * @dep: endpoint for which this request is prepared
830 * @req: dwc3_request pointer
831 */
Felipe Balbi68e823e2011-11-28 12:25:01 +0200832static void dwc3_prepare_one_trb(struct dwc3_ep *dep,
Felipe Balbieeb720f2011-11-28 12:46:59 +0200833 struct dwc3_request *req, dma_addr_t dma,
834 unsigned length, unsigned last, unsigned chain)
Felipe Balbic71fc372011-11-22 11:37:34 +0200835{
Felipe Balbieeb720f2011-11-28 12:46:59 +0200836 struct dwc3 *dwc = dep->dwc;
Felipe Balbif6bafc62012-02-06 11:04:53 +0200837 struct dwc3_trb *trb;
Felipe Balbic71fc372011-11-22 11:37:34 +0200838
839 unsigned int cur_slot;
840
Felipe Balbieeb720f2011-11-28 12:46:59 +0200841 dev_vdbg(dwc->dev, "%s: req %p dma %08llx length %d%s%s\n",
842 dep->name, req, (unsigned long long) dma,
843 length, last ? " last" : "",
844 chain ? " chain" : "");
845
Felipe Balbif6bafc62012-02-06 11:04:53 +0200846 trb = &dep->trb_pool[dep->free_slot & DWC3_TRB_MASK];
Felipe Balbic71fc372011-11-22 11:37:34 +0200847 cur_slot = dep->free_slot;
848 dep->free_slot++;
849
850 /* Skip the LINK-TRB on ISOC */
Vijayavardhan Vennapusa2a444ad2013-02-01 13:20:59 +0530851 if (((dep->free_slot & DWC3_TRB_MASK) == DWC3_TRB_NUM - 1) &&
Ido Shayevitz57cdac12012-03-12 20:25:24 +0200852 usb_endpoint_xfer_isoc(dep->endpoint.desc))
Vijayavardhan Vennapusa2a444ad2013-02-01 13:20:59 +0530853 dep->free_slot++;
Felipe Balbic71fc372011-11-22 11:37:34 +0200854
Felipe Balbieeb720f2011-11-28 12:46:59 +0200855 if (!req->trb) {
856 dwc3_gadget_move_request_queued(req);
Felipe Balbif6bafc62012-02-06 11:04:53 +0200857 req->trb = trb;
858 req->trb_dma = dwc3_trb_dma_offset(dep, trb);
Felipe Balbieeb720f2011-11-28 12:46:59 +0200859 }
Felipe Balbic71fc372011-11-22 11:37:34 +0200860
Felipe Balbif6bafc62012-02-06 11:04:53 +0200861 trb->size = DWC3_TRB_SIZE_LENGTH(length);
862 trb->bpl = lower_32_bits(dma);
863 trb->bph = upper_32_bits(dma);
Felipe Balbic71fc372011-11-22 11:37:34 +0200864
Ido Shayevitz57cdac12012-03-12 20:25:24 +0200865 switch (usb_endpoint_type(dep->endpoint.desc)) {
Felipe Balbic71fc372011-11-22 11:37:34 +0200866 case USB_ENDPOINT_XFER_CONTROL:
Felipe Balbif6bafc62012-02-06 11:04:53 +0200867 trb->ctrl = DWC3_TRBCTL_CONTROL_SETUP;
Felipe Balbic71fc372011-11-22 11:37:34 +0200868 break;
869
870 case USB_ENDPOINT_XFER_ISOC:
Felipe Balbif6bafc62012-02-06 11:04:53 +0200871 trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS_FIRST;
Felipe Balbic71fc372011-11-22 11:37:34 +0200872
Pratyush Ananddf023422012-05-21 12:42:54 +0530873 if (!req->request.no_interrupt)
Felipe Balbif6bafc62012-02-06 11:04:53 +0200874 trb->ctrl |= DWC3_TRB_CTRL_IOC;
Felipe Balbic71fc372011-11-22 11:37:34 +0200875 break;
876
877 case USB_ENDPOINT_XFER_BULK:
878 case USB_ENDPOINT_XFER_INT:
Felipe Balbif6bafc62012-02-06 11:04:53 +0200879 trb->ctrl = DWC3_TRBCTL_NORMAL;
Felipe Balbic71fc372011-11-22 11:37:34 +0200880 break;
881 default:
882 /*
883 * This is only possible with faulty memory because we
884 * checked it already :)
885 */
886 BUG();
887 }
888
Ido Shayevitz57cdac12012-03-12 20:25:24 +0200889 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbif6bafc62012-02-06 11:04:53 +0200890 trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI;
891 trb->ctrl |= DWC3_TRB_CTRL_CSP;
892 } else {
893 if (chain)
894 trb->ctrl |= DWC3_TRB_CTRL_CHN;
Felipe Balbic71fc372011-11-22 11:37:34 +0200895
Felipe Balbif6bafc62012-02-06 11:04:53 +0200896 if (last)
897 trb->ctrl |= DWC3_TRB_CTRL_LST;
898 }
899
Ido Shayevitz57cdac12012-03-12 20:25:24 +0200900 if (usb_endpoint_xfer_bulk(dep->endpoint.desc) && dep->stream_capable)
Felipe Balbif6bafc62012-02-06 11:04:53 +0200901 trb->ctrl |= DWC3_TRB_CTRL_SID_SOFN(req->request.stream_id);
902
903 trb->ctrl |= DWC3_TRB_CTRL_HWO;
Felipe Balbic71fc372011-11-22 11:37:34 +0200904}
905
Felipe Balbi72246da2011-08-19 18:10:58 +0300906/*
907 * dwc3_prepare_trbs - setup TRBs from requests
908 * @dep: endpoint for which requests are being prepared
909 * @starting: true if the endpoint is idle and no requests are queued.
910 *
Paul Zimmerman1d046792012-02-15 18:56:56 -0800911 * The function goes through the requests list and sets up TRBs for the
912 * transfers. The function returns once there are no more TRBs available or
913 * it runs out of requests.
Felipe Balbi72246da2011-08-19 18:10:58 +0300914 */
Felipe Balbi68e823e2011-11-28 12:25:01 +0200915static void dwc3_prepare_trbs(struct dwc3_ep *dep, bool starting)
Felipe Balbi72246da2011-08-19 18:10:58 +0300916{
Felipe Balbi68e823e2011-11-28 12:25:01 +0200917 struct dwc3_request *req, *n;
Felipe Balbi72246da2011-08-19 18:10:58 +0300918 u32 trbs_left;
Paul Zimmerman8d62cd62012-02-15 13:35:06 +0200919 u32 max;
Felipe Balbic71fc372011-11-22 11:37:34 +0200920 unsigned int last_one = 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300921
922 BUILD_BUG_ON_NOT_POWER_OF_2(DWC3_TRB_NUM);
923
924 /* the first request must not be queued */
925 trbs_left = (dep->busy_slot - dep->free_slot) & DWC3_TRB_MASK;
Felipe Balbic71fc372011-11-22 11:37:34 +0200926
Paul Zimmerman8d62cd62012-02-15 13:35:06 +0200927 /* Can't wrap around on a non-isoc EP since there's no link TRB */
Ido Shayevitz57cdac12012-03-12 20:25:24 +0200928 if (!usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Paul Zimmerman8d62cd62012-02-15 13:35:06 +0200929 max = DWC3_TRB_NUM - (dep->free_slot & DWC3_TRB_MASK);
930 if (trbs_left > max)
931 trbs_left = max;
932 }
933
Felipe Balbi72246da2011-08-19 18:10:58 +0300934 /*
Paul Zimmerman1d046792012-02-15 18:56:56 -0800935 * If busy & slot are equal than it is either full or empty. If we are
936 * starting to process requests then we are empty. Otherwise we are
Felipe Balbi72246da2011-08-19 18:10:58 +0300937 * full and don't do anything
938 */
939 if (!trbs_left) {
940 if (!starting)
Felipe Balbi68e823e2011-11-28 12:25:01 +0200941 return;
Felipe Balbi72246da2011-08-19 18:10:58 +0300942 trbs_left = DWC3_TRB_NUM;
943 /*
944 * In case we start from scratch, we queue the ISOC requests
945 * starting from slot 1. This is done because we use ring
946 * buffer and have no LST bit to stop us. Instead, we place
Paul Zimmerman1d046792012-02-15 18:56:56 -0800947 * IOC bit every TRB_NUM/4. We try to avoid having an interrupt
Felipe Balbi72246da2011-08-19 18:10:58 +0300948 * after the first request so we start at slot 1 and have
949 * 7 requests proceed before we hit the first IOC.
950 * Other transfer types don't use the ring buffer and are
951 * processed from the first TRB until the last one. Since we
952 * don't wrap around we have to start at the beginning.
953 */
Ido Shayevitz57cdac12012-03-12 20:25:24 +0200954 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbi72246da2011-08-19 18:10:58 +0300955 dep->busy_slot = 1;
956 dep->free_slot = 1;
957 } else {
958 dep->busy_slot = 0;
959 dep->free_slot = 0;
960 }
961 }
962
963 /* The last TRB is a link TRB, not used for xfer */
Ido Shayevitz57cdac12012-03-12 20:25:24 +0200964 if ((trbs_left <= 1) && usb_endpoint_xfer_isoc(dep->endpoint.desc))
Felipe Balbi68e823e2011-11-28 12:25:01 +0200965 return;
Felipe Balbi72246da2011-08-19 18:10:58 +0300966
967 list_for_each_entry_safe(req, n, &dep->request_list, list) {
Felipe Balbieeb720f2011-11-28 12:46:59 +0200968 unsigned length;
969 dma_addr_t dma;
Felipe Balbi72246da2011-08-19 18:10:58 +0300970
Felipe Balbieeb720f2011-11-28 12:46:59 +0200971 if (req->request.num_mapped_sgs > 0) {
972 struct usb_request *request = &req->request;
973 struct scatterlist *sg = request->sg;
974 struct scatterlist *s;
975 int i;
Felipe Balbi72246da2011-08-19 18:10:58 +0300976
Felipe Balbieeb720f2011-11-28 12:46:59 +0200977 for_each_sg(sg, s, request->num_mapped_sgs, i) {
978 unsigned chain = true;
Felipe Balbi72246da2011-08-19 18:10:58 +0300979
Felipe Balbieeb720f2011-11-28 12:46:59 +0200980 length = sg_dma_len(s);
981 dma = sg_dma_address(s);
Felipe Balbi72246da2011-08-19 18:10:58 +0300982
Paul Zimmerman1d046792012-02-15 18:56:56 -0800983 if (i == (request->num_mapped_sgs - 1) ||
984 sg_is_last(s)) {
Felipe Balbieeb720f2011-11-28 12:46:59 +0200985 last_one = true;
986 chain = false;
987 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300988
Felipe Balbieeb720f2011-11-28 12:46:59 +0200989 trbs_left--;
990 if (!trbs_left)
991 last_one = true;
Felipe Balbi72246da2011-08-19 18:10:58 +0300992
Felipe Balbieeb720f2011-11-28 12:46:59 +0200993 if (last_one)
994 chain = false;
Felipe Balbi72246da2011-08-19 18:10:58 +0300995
Felipe Balbieeb720f2011-11-28 12:46:59 +0200996 dwc3_prepare_one_trb(dep, req, dma, length,
997 last_one, chain);
Felipe Balbi72246da2011-08-19 18:10:58 +0300998
Felipe Balbieeb720f2011-11-28 12:46:59 +0200999 if (last_one)
1000 break;
1001 }
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +05301002 dbg_queue(dep->number, &req->request, 0);
Felipe Balbi72246da2011-08-19 18:10:58 +03001003 } else {
Felipe Balbieeb720f2011-11-28 12:46:59 +02001004 dma = req->request.dma;
1005 length = req->request.length;
1006 trbs_left--;
1007
1008 if (!trbs_left)
1009 last_one = 1;
1010
1011 /* Is this the last request? */
1012 if (list_is_last(&req->list, &dep->request_list))
1013 last_one = 1;
1014
1015 dwc3_prepare_one_trb(dep, req, dma, length,
1016 last_one, false);
1017
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +05301018 dbg_queue(dep->number, &req->request, 0);
Felipe Balbieeb720f2011-11-28 12:46:59 +02001019 if (last_one)
1020 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03001021 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001022 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001023}
1024
1025static int __dwc3_gadget_kick_transfer(struct dwc3_ep *dep, u16 cmd_param,
1026 int start_new)
1027{
1028 struct dwc3_gadget_ep_cmd_params params;
Vijayavardhan Vennapusa91ba6532013-01-30 17:35:45 +05301029 struct dwc3_request *req, *req1, *n;
Felipe Balbi72246da2011-08-19 18:10:58 +03001030 struct dwc3 *dwc = dep->dwc;
1031 int ret;
1032 u32 cmd;
1033
1034 if (start_new && (dep->flags & DWC3_EP_BUSY)) {
1035 dev_vdbg(dwc->dev, "%s: endpoint busy\n", dep->name);
1036 return -EBUSY;
1037 }
1038 dep->flags &= ~DWC3_EP_PENDING_REQUEST;
1039
1040 /*
1041 * If we are getting here after a short-out-packet we don't enqueue any
1042 * new requests as we try to set the IOC bit only on the last request.
1043 */
1044 if (start_new) {
1045 if (list_empty(&dep->req_queued))
1046 dwc3_prepare_trbs(dep, start_new);
1047
1048 /* req points to the first request which will be sent */
1049 req = next_request(&dep->req_queued);
1050 } else {
Felipe Balbi68e823e2011-11-28 12:25:01 +02001051 dwc3_prepare_trbs(dep, start_new);
1052
Felipe Balbi72246da2011-08-19 18:10:58 +03001053 /*
Paul Zimmerman1d046792012-02-15 18:56:56 -08001054 * req points to the first request where HWO changed from 0 to 1
Felipe Balbi72246da2011-08-19 18:10:58 +03001055 */
Felipe Balbi68e823e2011-11-28 12:25:01 +02001056 req = next_request(&dep->req_queued);
Felipe Balbi72246da2011-08-19 18:10:58 +03001057 }
1058 if (!req) {
1059 dep->flags |= DWC3_EP_PENDING_REQUEST;
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +05301060 dbg_event(dep->number, "NO REQ", 0);
Felipe Balbi72246da2011-08-19 18:10:58 +03001061 return 0;
1062 }
1063
1064 memset(&params, 0, sizeof(params));
Felipe Balbidc1c70a2011-09-30 10:58:51 +03001065 params.param0 = upper_32_bits(req->trb_dma);
1066 params.param1 = lower_32_bits(req->trb_dma);
Felipe Balbi72246da2011-08-19 18:10:58 +03001067
1068 if (start_new)
1069 cmd = DWC3_DEPCMD_STARTTRANSFER;
1070 else
1071 cmd = DWC3_DEPCMD_UPDATETRANSFER;
1072
1073 cmd |= DWC3_DEPCMD_PARAM(cmd_param);
1074 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number, cmd, &params);
1075 if (ret < 0) {
1076 dev_dbg(dwc->dev, "failed to send STARTTRANSFER command\n");
1077
Vijayavardhan Vennapusa91ba6532013-01-30 17:35:45 +05301078 if ((ret == -EAGAIN) && start_new &&
1079 usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
1080 /* If bit13 in Command complete event is set, software
1081 * must issue ENDTRANDFER command and wait for
1082 * Xfernotready event to queue the requests again.
1083 */
1084 if (!dep->resource_index) {
1085 dep->resource_index =
1086 dwc3_gadget_ep_get_transfer_index(dwc,
1087 dep->number);
1088 WARN_ON_ONCE(!dep->resource_index);
1089 }
1090 dwc3_stop_active_transfer(dwc, dep->number);
1091 list_for_each_entry_safe_reverse(req1, n,
1092 &dep->req_queued, list) {
1093 req1->trb = NULL;
1094 dwc3_gadget_move_request_list_front(req1);
1095 if (req->request.num_mapped_sgs)
1096 dep->busy_slot +=
1097 req->request.num_mapped_sgs;
1098 else
1099 dep->busy_slot++;
1100 if ((dep->busy_slot & DWC3_TRB_MASK) ==
1101 DWC3_TRB_NUM - 1)
1102 dep->busy_slot++;
1103 }
1104 return ret;
1105 } else {
1106 /*
1107 * FIXME we need to iterate over the list of requests
1108 * here and stop, unmap, free and del each of the linked
1109 * requests instead of what we do now.
1110 */
1111 usb_gadget_unmap_request(&dwc->gadget, &req->request,
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02001112 req->direction);
Vijayavardhan Vennapusa91ba6532013-01-30 17:35:45 +05301113 list_del(&req->list);
1114 return ret;
1115 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001116 }
1117
1118 dep->flags |= DWC3_EP_BUSY;
Felipe Balbi25b8ff62011-11-04 12:32:47 +02001119
Paul Zimmermanf39a37f2012-03-29 18:16:54 +00001120 if (start_new) {
Felipe Balbi4959cfc2012-06-06 12:04:13 +03001121 dep->resource_index = dwc3_gadget_ep_get_transfer_index(dwc,
Paul Zimmermanf39a37f2012-03-29 18:16:54 +00001122 dep->number);
Felipe Balbi4959cfc2012-06-06 12:04:13 +03001123 WARN_ON_ONCE(!dep->resource_index);
Paul Zimmermanf39a37f2012-03-29 18:16:54 +00001124 }
Felipe Balbi25b8ff62011-11-04 12:32:47 +02001125
Felipe Balbi72246da2011-08-19 18:10:58 +03001126 return 0;
1127}
1128
Pratyush Anand73939b02012-05-25 18:54:56 +05301129static void __dwc3_gadget_start_isoc(struct dwc3 *dwc,
1130 struct dwc3_ep *dep, u32 cur_uf)
1131{
1132 u32 uf;
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +05301133 int ret;
Pratyush Anand73939b02012-05-25 18:54:56 +05301134
Vijayavardhan Vennapusa91ba6532013-01-30 17:35:45 +05301135 dep->current_uf = cur_uf;
1136
Pratyush Anand73939b02012-05-25 18:54:56 +05301137 if (list_empty(&dep->request_list)) {
1138 dev_vdbg(dwc->dev, "ISOC ep %s run out for requests.\n",
1139 dep->name);
Pratyush Anandac417602012-08-30 12:21:43 +05301140 dep->flags |= DWC3_EP_PENDING_REQUEST;
Pratyush Anand73939b02012-05-25 18:54:56 +05301141 return;
1142 }
1143
1144 /* 4 micro frames in the future */
1145 uf = cur_uf + dep->interval * 4;
1146
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +05301147 ret = __dwc3_gadget_kick_transfer(dep, uf, 1);
1148 if (ret < 0)
1149 dbg_event(dep->number, "QUEUE", ret);
Pratyush Anand73939b02012-05-25 18:54:56 +05301150}
1151
1152static void dwc3_gadget_start_isoc(struct dwc3 *dwc,
1153 struct dwc3_ep *dep, const struct dwc3_event_depevt *event)
1154{
1155 u32 cur_uf, mask;
1156
1157 mask = ~(dep->interval - 1);
1158 cur_uf = event->parameters & mask;
1159
1160 __dwc3_gadget_start_isoc(dwc, dep, cur_uf);
1161}
1162
Felipe Balbi72246da2011-08-19 18:10:58 +03001163static int __dwc3_gadget_ep_queue(struct dwc3_ep *dep, struct dwc3_request *req)
1164{
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02001165 struct dwc3 *dwc = dep->dwc;
1166 int ret;
1167
Manu Gautamd2b99e12013-02-11 15:53:34 +05301168 if (req->request.status == -EINPROGRESS) {
1169 ret = -EBUSY;
1170 dev_err(dwc->dev, "%s: %p request already in queue",
1171 dep->name, req);
1172 return ret;
1173 }
1174
Felipe Balbi72246da2011-08-19 18:10:58 +03001175 req->request.actual = 0;
1176 req->request.status = -EINPROGRESS;
1177 req->direction = dep->direction;
1178 req->epnum = dep->number;
1179
1180 /*
1181 * We only add to our list of requests now and
1182 * start consuming the list once we get XferNotReady
1183 * IRQ.
1184 *
1185 * That way, we avoid doing anything that we don't need
1186 * to do now and defer it until the point we receive a
1187 * particular token from the Host side.
1188 *
1189 * This will also avoid Host cancelling URBs due to too
Paul Zimmerman1d046792012-02-15 18:56:56 -08001190 * many NAKs.
Felipe Balbi72246da2011-08-19 18:10:58 +03001191 */
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02001192 ret = usb_gadget_map_request(&dwc->gadget, &req->request,
1193 dep->direction);
1194 if (ret)
1195 return ret;
1196
Felipe Balbi72246da2011-08-19 18:10:58 +03001197 list_add_tail(&req->list, &dep->request_list);
1198
1199 /*
Felipe Balbi46485a02012-06-06 12:00:50 +03001200 * There are a few special cases:
Felipe Balbi72246da2011-08-19 18:10:58 +03001201 *
Paul Zimmermanf39a37f2012-03-29 18:16:54 +00001202 * 1. XferNotReady with empty list of requests. We need to kick the
1203 * transfer here in that situation, otherwise we will be NAKing
1204 * forever. If we get XferNotReady before gadget driver has a
1205 * chance to queue a request, we will ACK the IRQ but won't be
1206 * able to receive the data until the next request is queued.
1207 * The following code is handling exactly that.
1208 *
Felipe Balbi72246da2011-08-19 18:10:58 +03001209 */
1210 if (dep->flags & DWC3_EP_PENDING_REQUEST) {
Pratyush Anandac417602012-08-30 12:21:43 +05301211 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001212
Pratyush Anandac417602012-08-30 12:21:43 +05301213 /*
1214 * If xfernotready is already elapsed and it is a case
1215 * of isoc transfer, then issue END TRANSFER, so that
1216 * you can receive xfernotready again and can have
1217 * notion of current microframe.
1218 */
1219 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Vijayavardhan Vennapusa91ba6532013-01-30 17:35:45 +05301220 /* If xfernotready event is recieved before issuing
1221 * START TRANSFER command, don't issue END TRANSFER.
1222 * Rather start queueing the requests by issuing START
1223 * TRANSFER command.
1224 */
1225 if (list_empty(&dep->req_queued) && dep->resource_index)
Pratyush Anand18bbcb02013-01-14 15:59:34 +05301226 dwc3_stop_active_transfer(dwc, dep->number);
Vijayavardhan Vennapusa91ba6532013-01-30 17:35:45 +05301227 else
1228 __dwc3_gadget_start_isoc(dwc, dep,
1229 dep->current_uf);
1230 dep->flags &= ~DWC3_EP_PENDING_REQUEST;
Pratyush Anandac417602012-08-30 12:21:43 +05301231 return 0;
1232 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001233
Felipe Balbi46485a02012-06-06 12:00:50 +03001234 ret = __dwc3_gadget_kick_transfer(dep, 0, true);
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +05301235 if (ret && ret != -EBUSY) {
1236 dbg_event(dep->number, "QUEUE", ret);
Felipe Balbi72246da2011-08-19 18:10:58 +03001237 dev_dbg(dwc->dev, "%s: failed to kick transfers\n",
1238 dep->name);
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +05301239 }
Felipe Balbi5d409eb2012-05-22 10:24:11 +03001240 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001241
Felipe Balbi46485a02012-06-06 12:00:50 +03001242 /*
1243 * 2. XferInProgress on Isoc EP with an active transfer. We need to
1244 * kick the transfer here after queuing a request, otherwise the
1245 * core may not see the modified TRB(s).
1246 */
1247 if (usb_endpoint_xfer_isoc(dep->endpoint.desc) &&
Pratyush Anand053d3e52012-08-07 16:54:18 +05301248 (dep->flags & DWC3_EP_BUSY) &&
1249 !(dep->flags & DWC3_EP_MISSED_ISOC)) {
Felipe Balbi4959cfc2012-06-06 12:04:13 +03001250 WARN_ON_ONCE(!dep->resource_index);
1251 ret = __dwc3_gadget_kick_transfer(dep, dep->resource_index,
Felipe Balbi46485a02012-06-06 12:00:50 +03001252 false);
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +05301253 if (ret && ret != -EBUSY) {
1254 dbg_event(dep->number, "QUEUE", ret);
Felipe Balbi46485a02012-06-06 12:00:50 +03001255 dev_dbg(dwc->dev, "%s: failed to kick transfers\n",
1256 dep->name);
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +05301257 }
Felipe Balbi46485a02012-06-06 12:00:50 +03001258 }
1259
Felipe Balbi72246da2011-08-19 18:10:58 +03001260 return 0;
1261}
1262
1263static int dwc3_gadget_ep_queue(struct usb_ep *ep, struct usb_request *request,
1264 gfp_t gfp_flags)
1265{
1266 struct dwc3_request *req = to_dwc3_request(request);
1267 struct dwc3_ep *dep = to_dwc3_ep(ep);
1268 struct dwc3 *dwc = dep->dwc;
1269
1270 unsigned long flags;
1271
1272 int ret;
1273
Manu Gautam22f93042013-02-20 15:12:02 +05301274 spin_lock_irqsave(&dwc->lock, flags);
1275
Ido Shayevitz57cdac12012-03-12 20:25:24 +02001276 if (!dep->endpoint.desc) {
Manu Gautam22f93042013-02-20 15:12:02 +05301277 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001278 dev_dbg(dwc->dev, "trying to queue request %p to disabled %s\n",
1279 request, ep->name);
1280 return -ESHUTDOWN;
1281 }
1282
1283 dev_vdbg(dwc->dev, "queing request %p to %s length %d\n",
1284 request, ep->name, request->length);
1285
Manu Gautam1c4dbcb2012-10-05 13:16:00 +05301286 WARN(!dep->direction && (request->length % ep->desc->wMaxPacketSize),
1287 "trying to queue unaligned request (%d)\n", request->length);
1288
Felipe Balbi72246da2011-08-19 18:10:58 +03001289 ret = __dwc3_gadget_ep_queue(dep, req);
1290 spin_unlock_irqrestore(&dwc->lock, flags);
1291
1292 return ret;
1293}
1294
1295static int dwc3_gadget_ep_dequeue(struct usb_ep *ep,
1296 struct usb_request *request)
1297{
1298 struct dwc3_request *req = to_dwc3_request(request);
1299 struct dwc3_request *r = NULL;
1300
1301 struct dwc3_ep *dep = to_dwc3_ep(ep);
1302 struct dwc3 *dwc = dep->dwc;
1303
1304 unsigned long flags;
1305 int ret = 0;
1306
1307 spin_lock_irqsave(&dwc->lock, flags);
1308
1309 list_for_each_entry(r, &dep->request_list, list) {
1310 if (r == req)
1311 break;
1312 }
1313
1314 if (r != req) {
1315 list_for_each_entry(r, &dep->req_queued, list) {
1316 if (r == req)
1317 break;
1318 }
1319 if (r == req) {
1320 /* wait until it is processed */
1321 dwc3_stop_active_transfer(dwc, dep->number);
Pratyush Anandeaec3e92012-06-15 11:54:00 +05301322 goto out1;
Felipe Balbi72246da2011-08-19 18:10:58 +03001323 }
1324 dev_err(dwc->dev, "request %p was not queued to %s\n",
1325 request, ep->name);
1326 ret = -EINVAL;
1327 goto out0;
1328 }
1329
Pratyush Anandeaec3e92012-06-15 11:54:00 +05301330out1:
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +05301331 dbg_event(dep->number, "DEQUEUE", 0);
Felipe Balbi72246da2011-08-19 18:10:58 +03001332 /* giveback the request */
1333 dwc3_gadget_giveback(dep, req, -ECONNRESET);
1334
1335out0:
1336 spin_unlock_irqrestore(&dwc->lock, flags);
1337
1338 return ret;
1339}
1340
1341int __dwc3_gadget_ep_set_halt(struct dwc3_ep *dep, int value)
1342{
1343 struct dwc3_gadget_ep_cmd_params params;
1344 struct dwc3 *dwc = dep->dwc;
1345 int ret;
1346
1347 memset(&params, 0x00, sizeof(params));
1348
1349 if (value) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001350 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number,
1351 DWC3_DEPCMD_SETSTALL, &params);
1352 if (ret)
1353 dev_err(dwc->dev, "failed to %s STALL on %s\n",
1354 value ? "set" : "clear",
1355 dep->name);
1356 else
1357 dep->flags |= DWC3_EP_STALL;
1358 } else {
1359 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number,
1360 DWC3_DEPCMD_CLEARSTALL, &params);
1361 if (ret)
1362 dev_err(dwc->dev, "failed to %s STALL on %s\n",
1363 value ? "set" : "clear",
1364 dep->name);
1365 else
Vijayavardhan Vennapusa6008e262012-10-19 15:57:56 +05301366 dep->flags &= ~(DWC3_EP_STALL | DWC3_EP_WEDGE);
Felipe Balbi72246da2011-08-19 18:10:58 +03001367 }
Paul Zimmerman52754552011-09-30 10:58:44 +03001368
Felipe Balbi72246da2011-08-19 18:10:58 +03001369 return ret;
1370}
1371
1372static int dwc3_gadget_ep_set_halt(struct usb_ep *ep, int value)
1373{
1374 struct dwc3_ep *dep = to_dwc3_ep(ep);
1375 struct dwc3 *dwc = dep->dwc;
1376
1377 unsigned long flags;
1378
1379 int ret;
1380
1381 spin_lock_irqsave(&dwc->lock, flags);
1382
Ido Shayevitz57cdac12012-03-12 20:25:24 +02001383 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001384 dev_err(dwc->dev, "%s is of Isochronous type\n", dep->name);
1385 ret = -EINVAL;
1386 goto out;
1387 }
1388
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +05301389 dbg_event(dep->number, "HALT", value);
Felipe Balbi72246da2011-08-19 18:10:58 +03001390 ret = __dwc3_gadget_ep_set_halt(dep, value);
1391out:
1392 spin_unlock_irqrestore(&dwc->lock, flags);
1393
1394 return ret;
1395}
1396
1397static int dwc3_gadget_ep_set_wedge(struct usb_ep *ep)
1398{
1399 struct dwc3_ep *dep = to_dwc3_ep(ep);
Paul Zimmerman249a4562012-02-24 17:32:16 -08001400 struct dwc3 *dwc = dep->dwc;
1401 unsigned long flags;
Felipe Balbi72246da2011-08-19 18:10:58 +03001402
Paul Zimmerman249a4562012-02-24 17:32:16 -08001403 spin_lock_irqsave(&dwc->lock, flags);
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +05301404 dbg_event(dep->number, "WEDGE", 0);
Felipe Balbi72246da2011-08-19 18:10:58 +03001405 dep->flags |= DWC3_EP_WEDGE;
Paul Zimmerman249a4562012-02-24 17:32:16 -08001406 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001407
Pratyush Anandeb840752012-06-25 22:40:43 +05301408 if (dep->number == 0 || dep->number == 1)
1409 return dwc3_gadget_ep0_set_halt(ep, 1);
1410 else
1411 return dwc3_gadget_ep_set_halt(ep, 1);
Felipe Balbi72246da2011-08-19 18:10:58 +03001412}
1413
1414/* -------------------------------------------------------------------------- */
1415
1416static struct usb_endpoint_descriptor dwc3_gadget_ep0_desc = {
1417 .bLength = USB_DT_ENDPOINT_SIZE,
1418 .bDescriptorType = USB_DT_ENDPOINT,
1419 .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
1420};
1421
1422static const struct usb_ep_ops dwc3_gadget_ep0_ops = {
1423 .enable = dwc3_gadget_ep0_enable,
1424 .disable = dwc3_gadget_ep0_disable,
1425 .alloc_request = dwc3_gadget_ep_alloc_request,
1426 .free_request = dwc3_gadget_ep_free_request,
1427 .queue = dwc3_gadget_ep0_queue,
1428 .dequeue = dwc3_gadget_ep_dequeue,
Pratyush Anandeb840752012-06-25 22:40:43 +05301429 .set_halt = dwc3_gadget_ep0_set_halt,
Felipe Balbi72246da2011-08-19 18:10:58 +03001430 .set_wedge = dwc3_gadget_ep_set_wedge,
1431};
1432
1433static const struct usb_ep_ops dwc3_gadget_ep_ops = {
1434 .enable = dwc3_gadget_ep_enable,
1435 .disable = dwc3_gadget_ep_disable,
1436 .alloc_request = dwc3_gadget_ep_alloc_request,
1437 .free_request = dwc3_gadget_ep_free_request,
1438 .queue = dwc3_gadget_ep_queue,
1439 .dequeue = dwc3_gadget_ep_dequeue,
1440 .set_halt = dwc3_gadget_ep_set_halt,
1441 .set_wedge = dwc3_gadget_ep_set_wedge,
1442};
1443
1444/* -------------------------------------------------------------------------- */
1445
1446static int dwc3_gadget_get_frame(struct usb_gadget *g)
1447{
1448 struct dwc3 *dwc = gadget_to_dwc(g);
1449 u32 reg;
1450
1451 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1452 return DWC3_DSTS_SOFFN(reg);
1453}
1454
1455static int dwc3_gadget_wakeup(struct usb_gadget *g)
1456{
1457 struct dwc3 *dwc = gadget_to_dwc(g);
1458
1459 unsigned long timeout;
1460 unsigned long flags;
1461
1462 u32 reg;
1463
1464 int ret = 0;
1465
1466 u8 link_state;
1467 u8 speed;
1468
1469 spin_lock_irqsave(&dwc->lock, flags);
1470
1471 /*
1472 * According to the Databook Remote wakeup request should
1473 * be issued only when the device is in early suspend state.
1474 *
1475 * We can check that via USB Link State bits in DSTS register.
1476 */
1477 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1478
1479 speed = reg & DWC3_DSTS_CONNECTSPD;
1480 if (speed == DWC3_DSTS_SUPERSPEED) {
1481 dev_dbg(dwc->dev, "no wakeup on SuperSpeed\n");
1482 ret = -EINVAL;
1483 goto out;
1484 }
1485
1486 link_state = DWC3_DSTS_USBLNKST(reg);
1487
1488 switch (link_state) {
1489 case DWC3_LINK_STATE_RX_DET: /* in HS, means Early Suspend */
1490 case DWC3_LINK_STATE_U3: /* in HS, means SUSPEND */
1491 break;
1492 default:
1493 dev_dbg(dwc->dev, "can't wakeup from link state %d\n",
1494 link_state);
1495 ret = -EINVAL;
1496 goto out;
1497 }
1498
Felipe Balbi8598bde2012-01-02 18:55:57 +02001499 ret = dwc3_gadget_set_link_state(dwc, DWC3_LINK_STATE_RECOV);
1500 if (ret < 0) {
1501 dev_err(dwc->dev, "failed to put link in Recovery\n");
1502 goto out;
1503 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001504
Paul Zimmerman88df4272012-04-27 13:10:52 +03001505 /* Recent versions do this automatically */
1506 if (dwc->revision < DWC3_REVISION_194A) {
1507 /* write zeroes to Link Change Request */
Felipe Balbib4d04352012-05-24 10:27:56 +03001508 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
Paul Zimmerman88df4272012-04-27 13:10:52 +03001509 reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK;
1510 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1511 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001512
Paul Zimmerman1d046792012-02-15 18:56:56 -08001513 /* poll until Link State changes to ON */
Felipe Balbi72246da2011-08-19 18:10:58 +03001514 timeout = jiffies + msecs_to_jiffies(100);
1515
Paul Zimmerman1d046792012-02-15 18:56:56 -08001516 while (!time_after(jiffies, timeout)) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001517 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1518
1519 /* in HS, means ON */
1520 if (DWC3_DSTS_USBLNKST(reg) == DWC3_LINK_STATE_U0)
1521 break;
1522 }
1523
1524 if (DWC3_DSTS_USBLNKST(reg) != DWC3_LINK_STATE_U0) {
1525 dev_err(dwc->dev, "failed to send remote wakeup\n");
1526 ret = -EINVAL;
1527 }
1528
1529out:
1530 spin_unlock_irqrestore(&dwc->lock, flags);
1531
1532 return ret;
1533}
1534
1535static int dwc3_gadget_set_selfpowered(struct usb_gadget *g,
1536 int is_selfpowered)
1537{
1538 struct dwc3 *dwc = gadget_to_dwc(g);
Paul Zimmerman249a4562012-02-24 17:32:16 -08001539 unsigned long flags;
Felipe Balbi72246da2011-08-19 18:10:58 +03001540
Paul Zimmerman249a4562012-02-24 17:32:16 -08001541 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001542 dwc->is_selfpowered = !!is_selfpowered;
Paul Zimmerman249a4562012-02-24 17:32:16 -08001543 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001544
1545 return 0;
1546}
1547
Wesley Cheng446ad8d2013-06-05 16:15:01 +05301548#define DWC3_SOFT_RESET_TIMEOUT 10 /* 10 msec */
Pratyush Anand77473f72012-07-02 10:21:55 +05301549static int dwc3_gadget_run_stop(struct dwc3 *dwc, int is_on)
Felipe Balbi72246da2011-08-19 18:10:58 +03001550{
1551 u32 reg;
Sebastian Andrzej Siewior61d58242011-08-29 16:46:38 +02001552 u32 timeout = 500;
Wesley Cheng446ad8d2013-06-05 16:15:01 +05301553 ktime_t start, diff;
Felipe Balbi72246da2011-08-19 18:10:58 +03001554
1555 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
Felipe Balbi8db7ed12012-01-18 18:32:29 +02001556 if (is_on) {
Paul Zimmerman88df4272012-04-27 13:10:52 +03001557 if (dwc->revision <= DWC3_REVISION_187A) {
1558 reg &= ~DWC3_DCTL_TRGTULST_MASK;
1559 reg |= DWC3_DCTL_TRGTULST_RX_DET;
1560 }
1561
1562 if (dwc->revision >= DWC3_REVISION_194A)
1563 reg &= ~DWC3_DCTL_KEEP_CONNECT;
Wesley Cheng446ad8d2013-06-05 16:15:01 +05301564
1565 start = ktime_get();
1566 /* issue device SoftReset */
1567 dwc3_writel(dwc->regs, DWC3_DCTL, reg | DWC3_DCTL_CSFTRST);
1568 do {
1569 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
1570 if (!(reg & DWC3_DCTL_CSFTRST))
1571 break;
1572
1573 diff = ktime_sub(ktime_get(), start);
1574 /* poll for max. 10ms */
1575 if (ktime_to_ms(diff) > DWC3_SOFT_RESET_TIMEOUT) {
1576 printk_ratelimited(KERN_ERR
1577 "%s:core Reset Timed Out\n", __func__);
1578 break;
1579 }
1580 cpu_relax();
1581 } while (true);
1582
1583
1584 dwc3_event_buffers_setup(dwc);
1585 dwc3_gadget_restart(dwc);
1586 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
Paul Zimmerman88df4272012-04-27 13:10:52 +03001587 reg |= DWC3_DCTL_RUN_STOP;
Felipe Balbi8db7ed12012-01-18 18:32:29 +02001588 } else {
Felipe Balbi72246da2011-08-19 18:10:58 +03001589 reg &= ~DWC3_DCTL_RUN_STOP;
Felipe Balbi8db7ed12012-01-18 18:32:29 +02001590 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001591
1592 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1593
1594 do {
1595 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1596 if (is_on) {
1597 if (!(reg & DWC3_DSTS_DEVCTRLHLT))
1598 break;
1599 } else {
1600 if (reg & DWC3_DSTS_DEVCTRLHLT)
1601 break;
1602 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001603 timeout--;
1604 if (!timeout)
Pratyush Anand77473f72012-07-02 10:21:55 +05301605 return -ETIMEDOUT;
Sebastian Andrzej Siewior61d58242011-08-29 16:46:38 +02001606 udelay(1);
Felipe Balbi72246da2011-08-19 18:10:58 +03001607 } while (1);
1608
1609 dev_vdbg(dwc->dev, "gadget %s data soft-%s\n",
1610 dwc->gadget_driver
1611 ? dwc->gadget_driver->function : "no-function",
1612 is_on ? "connect" : "disconnect");
Pratyush Anand77473f72012-07-02 10:21:55 +05301613
1614 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +03001615}
1616
Vijayavardhan Vennapusa908f1ed2012-10-19 19:51:48 +05301617static int dwc3_gadget_vbus_draw(struct usb_gadget *g, unsigned mA)
1618{
1619 struct dwc3 *dwc = gadget_to_dwc(g);
1620 struct dwc3_otg *dotg = dwc->dotg;
1621
1622 if (dotg && dotg->otg.phy)
1623 return usb_phy_set_power(dotg->otg.phy, mA);
1624
1625 return -ENOTSUPP;
1626}
1627
Felipe Balbi72246da2011-08-19 18:10:58 +03001628static int dwc3_gadget_pullup(struct usb_gadget *g, int is_on)
1629{
1630 struct dwc3 *dwc = gadget_to_dwc(g);
1631 unsigned long flags;
Pratyush Anand77473f72012-07-02 10:21:55 +05301632 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001633
1634 is_on = !!is_on;
1635
1636 spin_lock_irqsave(&dwc->lock, flags);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +02001637
1638 dwc->softconnect = is_on;
1639
1640 if ((dwc->dotg && !dwc->vbus_active) ||
1641 !dwc->gadget_driver) {
1642
1643 spin_unlock_irqrestore(&dwc->lock, flags);
1644
1645 /*
1646 * Need to wait for vbus_session(on) from otg driver or to
1647 * the udc_start.
1648 */
1649 return 0;
1650 }
1651
Pratyush Anand77473f72012-07-02 10:21:55 +05301652 ret = dwc3_gadget_run_stop(dwc, is_on);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +02001653
1654 spin_unlock_irqrestore(&dwc->lock, flags);
1655
Pratyush Anand77473f72012-07-02 10:21:55 +05301656 return ret;
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +02001657}
1658
Jack Pham09e5c8e2013-03-06 18:53:43 -08001659static void dwc3_gadget_disconnect_interrupt(struct dwc3 *dwc);
1660
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +02001661static int dwc3_gadget_vbus_session(struct usb_gadget *_gadget, int is_active)
1662{
1663 struct dwc3 *dwc = gadget_to_dwc(_gadget);
1664 unsigned long flags;
Vijayavardhan Vennapusa8ec31d22012-10-23 08:44:48 +05301665 int ret = 0;
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +02001666
1667 if (!dwc->dotg)
1668 return -EPERM;
1669
1670 is_active = !!is_active;
1671
1672 spin_lock_irqsave(&dwc->lock, flags);
1673
1674 /* Mark that the vbus was powered */
1675 dwc->vbus_active = is_active;
1676
1677 /*
1678 * Check if upper level usb_gadget_driver was already registerd with
1679 * this udc controller driver (if dwc3_gadget_start was called)
1680 */
1681 if (dwc->gadget_driver && dwc->softconnect) {
1682 if (dwc->vbus_active) {
1683 /*
1684 * Both vbus was activated by otg and pullup was
1685 * signaled by the gadget driver.
1686 */
Pratyush Anand77473f72012-07-02 10:21:55 +05301687 ret = dwc3_gadget_run_stop(dwc, 1);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +02001688 } else {
Pratyush Anand77473f72012-07-02 10:21:55 +05301689 ret = dwc3_gadget_run_stop(dwc, 0);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +02001690 }
Jack Pham09e5c8e2013-03-06 18:53:43 -08001691 }
1692
1693 /*
1694 * Clearing run/stop bit might occur before disconnect event is seen.
1695 * Make sure to let gadget driver know in that case.
1696 */
1697 if (!dwc->vbus_active && dwc->start_config_issued) {
1698 dev_dbg(dwc->dev, "calling disconnect from %s\n", __func__);
1699 dwc3_gadget_disconnect_interrupt(dwc);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +02001700 }
1701
Felipe Balbi72246da2011-08-19 18:10:58 +03001702 spin_unlock_irqrestore(&dwc->lock, flags);
Pratyush Anand77473f72012-07-02 10:21:55 +05301703 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001704}
1705
Manu Gautamf1fceddf2012-10-12 14:02:50 +05301706/* Required gadget re-initialization before switching to gadget in OTG mode */
1707void dwc3_gadget_restart(struct dwc3 *dwc)
1708{
1709 struct dwc3_ep *dep;
1710 int ret = 0;
Vijayavardhan Vennapusab7434562012-12-12 16:48:49 +05301711 u32 reg;
Manu Gautamf1fceddf2012-10-12 14:02:50 +05301712
Vijayavardhan Vennapusab7434562012-12-12 16:48:49 +05301713 /* Enable all but Start and End of Frame IRQs */
1714 reg = (DWC3_DEVTEN_EVNTOVERFLOWEN |
1715 DWC3_DEVTEN_CMDCMPLTEN |
1716 DWC3_DEVTEN_ERRTICERREN |
1717 DWC3_DEVTEN_WKUPEVTEN |
1718 DWC3_DEVTEN_ULSTCNGEN |
1719 DWC3_DEVTEN_CONNECTDONEEN |
1720 DWC3_DEVTEN_USBRSTEN |
1721 DWC3_DEVTEN_DISCONNEVTEN);
1722 dwc3_writel(dwc->regs, DWC3_DEVTEN, reg);
1723
1724 /* Enable USB2 LPM and automatic phy suspend only on recent versions */
1725 if (dwc->revision >= DWC3_REVISION_194A) {
1726 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
1727 reg |= DWC3_DCFG_LPM_CAP;
1728 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
1729
1730 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
1731 reg &= ~(DWC3_DCTL_HIRD_THRES_MASK | DWC3_DCTL_L1_HIBER_EN);
1732
1733 /* TODO: This should be configurable */
1734 reg |= DWC3_DCTL_HIRD_THRES(28);
1735
1736 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Vijayavardhan Vennapusaba79f6b2013-07-30 13:39:52 +05301737
1738 dwc3_gadget_usb2_phy_suspend(dwc, true);
1739 dwc3_gadget_usb3_phy_suspend(dwc, true);
Vijayavardhan Vennapusab7434562012-12-12 16:48:49 +05301740 }
1741
1742 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
1743 reg &= ~(DWC3_DCFG_SPEED_MASK);
1744
1745 /**
1746 * WORKAROUND: DWC3 revision < 2.20a have an issue
1747 * which would cause metastability state on Run/Stop
1748 * bit if we try to force the IP to USB2-only mode.
1749 *
1750 * Because of that, we cannot configure the IP to any
1751 * speed other than the SuperSpeed
1752 *
1753 * Refers to:
1754 *
1755 * STAR#9000525659: Clock Domain Crossing on DCTL in
1756 * USB 2.0 Mode
1757 */
1758 if (dwc->revision < DWC3_REVISION_220A)
1759 reg |= DWC3_DCFG_SUPERSPEED;
1760 else
1761 reg |= dwc->maximum_speed;
1762 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
1763
1764 dwc->start_config_issued = false;
1765
1766 /* Start with SuperSpeed Default */
1767 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
Manu Gautamf1fceddf2012-10-12 14:02:50 +05301768
1769 dwc->delayed_status = false;
Vijayavardhan Vennapusab7434562012-12-12 16:48:49 +05301770 /* reinitialize physical ep0-1 */
Manu Gautamf1fceddf2012-10-12 14:02:50 +05301771 dep = dwc->eps[0];
1772 dep->flags = 0;
1773 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, false);
1774 if (ret) {
1775 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
1776 return;
1777 }
1778
1779 dep = dwc->eps[1];
1780 dep->flags = 0;
1781 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, false);
1782 if (ret) {
1783 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
1784 return;
1785 }
1786
1787 /* begin to receive SETUP packets */
1788 dwc->ep0state = EP0_SETUP_PHASE;
1789 dwc3_ep0_out_start(dwc);
1790}
1791
Felipe Balbi72246da2011-08-19 18:10:58 +03001792static int dwc3_gadget_start(struct usb_gadget *g,
1793 struct usb_gadget_driver *driver)
1794{
1795 struct dwc3 *dwc = gadget_to_dwc(g);
1796 struct dwc3_ep *dep;
1797 unsigned long flags;
1798 int ret = 0;
1799 u32 reg;
1800
Vijayavardhan Vennapusa0557c9b2013-05-17 13:24:49 +05301801 pm_runtime_get_sync(dwc->dev);
Felipe Balbi72246da2011-08-19 18:10:58 +03001802 spin_lock_irqsave(&dwc->lock, flags);
1803
1804 if (dwc->gadget_driver) {
1805 dev_err(dwc->dev, "%s is already bound to %s\n",
1806 dwc->gadget.name,
1807 dwc->gadget_driver->driver.name);
1808 ret = -EBUSY;
1809 goto err0;
1810 }
1811
1812 dwc->gadget_driver = driver;
1813 dwc->gadget.dev.driver = &driver->driver;
1814
Felipe Balbi72246da2011-08-19 18:10:58 +03001815 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
1816 reg &= ~(DWC3_DCFG_SPEED_MASK);
Felipe Balbi38d2c6c2012-03-23 12:20:31 +02001817
1818 /**
1819 * WORKAROUND: DWC3 revision < 2.20a have an issue
1820 * which would cause metastability state on Run/Stop
1821 * bit if we try to force the IP to USB2-only mode.
1822 *
1823 * Because of that, we cannot configure the IP to any
1824 * speed other than the SuperSpeed
1825 *
1826 * Refers to:
1827 *
1828 * STAR#9000525659: Clock Domain Crossing on DCTL in
1829 * USB 2.0 Mode
1830 */
1831 if (dwc->revision < DWC3_REVISION_220A)
1832 reg |= DWC3_DCFG_SUPERSPEED;
1833 else
1834 reg |= dwc->maximum_speed;
Felipe Balbi72246da2011-08-19 18:10:58 +03001835 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
1836
Paul Zimmermanb23c8432011-09-30 10:58:42 +03001837 dwc->start_config_issued = false;
1838
Felipe Balbi72246da2011-08-19 18:10:58 +03001839 /* Start with SuperSpeed Default */
1840 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
1841
1842 dep = dwc->eps[0];
Wesley Cheng446ad8d2013-06-05 16:15:01 +05301843 dep->endpoint.maxburst = 1;
Felipe Balbi07e0ee82012-07-16 14:08:16 +03001844 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, false);
Felipe Balbi72246da2011-08-19 18:10:58 +03001845 if (ret) {
1846 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
1847 goto err0;
1848 }
1849
1850 dep = dwc->eps[1];
Wesley Cheng446ad8d2013-06-05 16:15:01 +05301851 dep->endpoint.maxburst = 1;
Felipe Balbi07e0ee82012-07-16 14:08:16 +03001852 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, false);
Felipe Balbi72246da2011-08-19 18:10:58 +03001853 if (ret) {
1854 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
1855 goto err1;
1856 }
1857
1858 /* begin to receive SETUP packets */
Felipe Balbic7fcdeb2011-08-27 22:28:36 +03001859 dwc->ep0state = EP0_SETUP_PHASE;
Felipe Balbi72246da2011-08-19 18:10:58 +03001860 dwc3_ep0_out_start(dwc);
1861
1862 spin_unlock_irqrestore(&dwc->lock, flags);
Vijayavardhan Vennapusa0557c9b2013-05-17 13:24:49 +05301863 pm_runtime_put(dwc->dev);
Felipe Balbi72246da2011-08-19 18:10:58 +03001864
1865 return 0;
1866
1867err1:
1868 __dwc3_gadget_ep_disable(dwc->eps[0]);
1869
1870err0:
1871 spin_unlock_irqrestore(&dwc->lock, flags);
Vijayavardhan Vennapusa0557c9b2013-05-17 13:24:49 +05301872 pm_runtime_put(dwc->dev);
Felipe Balbi72246da2011-08-19 18:10:58 +03001873
1874 return ret;
1875}
1876
1877static int dwc3_gadget_stop(struct usb_gadget *g,
1878 struct usb_gadget_driver *driver)
1879{
1880 struct dwc3 *dwc = gadget_to_dwc(g);
1881 unsigned long flags;
1882
1883 spin_lock_irqsave(&dwc->lock, flags);
1884
1885 __dwc3_gadget_ep_disable(dwc->eps[0]);
1886 __dwc3_gadget_ep_disable(dwc->eps[1]);
1887
1888 dwc->gadget_driver = NULL;
1889 dwc->gadget.dev.driver = NULL;
1890
1891 spin_unlock_irqrestore(&dwc->lock, flags);
1892
1893 return 0;
1894}
Paul Zimmerman88df4272012-04-27 13:10:52 +03001895
Felipe Balbi72246da2011-08-19 18:10:58 +03001896static const struct usb_gadget_ops dwc3_gadget_ops = {
1897 .get_frame = dwc3_gadget_get_frame,
1898 .wakeup = dwc3_gadget_wakeup,
1899 .set_selfpowered = dwc3_gadget_set_selfpowered,
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +02001900 .vbus_session = dwc3_gadget_vbus_session,
Vijayavardhan Vennapusa908f1ed2012-10-19 19:51:48 +05301901 .vbus_draw = dwc3_gadget_vbus_draw,
Felipe Balbi72246da2011-08-19 18:10:58 +03001902 .pullup = dwc3_gadget_pullup,
1903 .udc_start = dwc3_gadget_start,
1904 .udc_stop = dwc3_gadget_stop,
1905};
1906
1907/* -------------------------------------------------------------------------- */
1908
1909static int __devinit dwc3_gadget_init_endpoints(struct dwc3 *dwc)
1910{
1911 struct dwc3_ep *dep;
1912 u8 epnum;
1913
1914 INIT_LIST_HEAD(&dwc->gadget.ep_list);
1915
1916 for (epnum = 0; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
1917 dep = kzalloc(sizeof(*dep), GFP_KERNEL);
1918 if (!dep) {
1919 dev_err(dwc->dev, "can't allocate endpoint %d\n",
1920 epnum);
1921 return -ENOMEM;
1922 }
1923
1924 dep->dwc = dwc;
1925 dep->number = epnum;
1926 dwc->eps[epnum] = dep;
1927
1928 snprintf(dep->name, sizeof(dep->name), "ep%d%s", epnum >> 1,
1929 (epnum & 1) ? "in" : "out");
1930 dep->endpoint.name = dep->name;
1931 dep->direction = (epnum & 1);
1932
1933 if (epnum == 0 || epnum == 1) {
1934 dep->endpoint.maxpacket = 512;
1935 dep->endpoint.ops = &dwc3_gadget_ep0_ops;
1936 if (!epnum)
1937 dwc->gadget.ep0 = &dep->endpoint;
1938 } else {
1939 int ret;
1940
1941 dep->endpoint.maxpacket = 1024;
Sebastian Andrzej Siewior12d36c12011-11-03 20:27:50 +01001942 dep->endpoint.max_streams = 15;
Felipe Balbi72246da2011-08-19 18:10:58 +03001943 dep->endpoint.ops = &dwc3_gadget_ep_ops;
1944 list_add_tail(&dep->endpoint.ep_list,
1945 &dwc->gadget.ep_list);
1946
1947 ret = dwc3_alloc_trb_pool(dep);
Felipe Balbi25b8ff62011-11-04 12:32:47 +02001948 if (ret)
Felipe Balbi72246da2011-08-19 18:10:58 +03001949 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001950 }
Felipe Balbi25b8ff62011-11-04 12:32:47 +02001951
Felipe Balbi72246da2011-08-19 18:10:58 +03001952 INIT_LIST_HEAD(&dep->request_list);
1953 INIT_LIST_HEAD(&dep->req_queued);
1954 }
1955
1956 return 0;
1957}
1958
1959static void dwc3_gadget_free_endpoints(struct dwc3 *dwc)
1960{
1961 struct dwc3_ep *dep;
1962 u8 epnum;
1963
1964 for (epnum = 0; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
1965 dep = dwc->eps[epnum];
1966 dwc3_free_trb_pool(dep);
1967
1968 if (epnum != 0 && epnum != 1)
1969 list_del(&dep->endpoint.ep_list);
1970
1971 kfree(dep);
1972 }
1973}
1974
1975static void dwc3_gadget_release(struct device *dev)
1976{
1977 dev_dbg(dev, "%s\n", __func__);
1978}
1979
1980/* -------------------------------------------------------------------------- */
1981static int dwc3_cleanup_done_reqs(struct dwc3 *dwc, struct dwc3_ep *dep,
1982 const struct dwc3_event_depevt *event, int status)
1983{
1984 struct dwc3_request *req;
Felipe Balbif6bafc62012-02-06 11:04:53 +02001985 struct dwc3_trb *trb;
Felipe Balbi72246da2011-08-19 18:10:58 +03001986 unsigned int count;
1987 unsigned int s_pkt = 0;
Pratyush Anand73939b02012-05-25 18:54:56 +05301988 unsigned int trb_status;
Felipe Balbi72246da2011-08-19 18:10:58 +03001989
1990 do {
1991 req = next_request(&dep->req_queued);
Sebastian Andrzej Siewiord39ee7b2011-11-03 10:32:20 +01001992 if (!req) {
1993 WARN_ON_ONCE(1);
1994 return 1;
1995 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001996
Felipe Balbif6bafc62012-02-06 11:04:53 +02001997 trb = req->trb;
Felipe Balbi72246da2011-08-19 18:10:58 +03001998
Felipe Balbif6bafc62012-02-06 11:04:53 +02001999 if ((trb->ctrl & DWC3_TRB_CTRL_HWO) && status != -ESHUTDOWN)
Sebastian Andrzej Siewior0d2f4752011-08-19 19:59:12 +02002000 /*
2001 * We continue despite the error. There is not much we
Paul Zimmerman1d046792012-02-15 18:56:56 -08002002 * can do. If we don't clean it up we loop forever. If
2003 * we skip the TRB then it gets overwritten after a
2004 * while since we use them in a ring buffer. A BUG()
2005 * would help. Lets hope that if this occurs, someone
Sebastian Andrzej Siewior0d2f4752011-08-19 19:59:12 +02002006 * fixes the root cause instead of looking away :)
2007 */
Felipe Balbi72246da2011-08-19 18:10:58 +03002008 dev_err(dwc->dev, "%s's TRB (%p) still owned by HW\n",
2009 dep->name, req->trb);
Felipe Balbif6bafc62012-02-06 11:04:53 +02002010 count = trb->size & DWC3_TRB_SIZE_MASK;
Felipe Balbi72246da2011-08-19 18:10:58 +03002011
2012 if (dep->direction) {
2013 if (count) {
Pratyush Anand73939b02012-05-25 18:54:56 +05302014 trb_status = DWC3_TRB_SIZE_TRBSTS(trb->size);
2015 if (trb_status == DWC3_TRBSTS_MISSED_ISOC) {
2016 dev_dbg(dwc->dev, "incomplete IN transfer %s\n",
2017 dep->name);
Pratyush Anand921b0b82013-01-14 15:59:32 +05302018 /*
2019 * If missed isoc occurred and there is
2020 * no request queued then issue END
2021 * TRANSFER, so that core generates
2022 * next xfernotready and we will issue
2023 * a fresh START TRANSFER.
2024 * If there are still queued request
2025 * then wait, do not issue either END
2026 * or UPDATE TRANSFER, just attach next
2027 * request in request_list during
2028 * giveback.If any future queued request
2029 * is successfully transferred then we
2030 * will issue UPDATE TRANSFER for all
2031 * request in the request_list.
2032 */
Pratyush Anand73939b02012-05-25 18:54:56 +05302033 dep->flags |= DWC3_EP_MISSED_ISOC;
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +05302034 dbg_event(dep->number, "MISSED ISOC",
2035 status);
Pratyush Anand73939b02012-05-25 18:54:56 +05302036 } else {
2037 dev_err(dwc->dev, "incomplete IN transfer %s\n",
2038 dep->name);
2039 status = -ECONNRESET;
2040 }
Pratyush Anand921b0b82013-01-14 15:59:32 +05302041 } else {
2042 dep->flags &= ~DWC3_EP_MISSED_ISOC;
Felipe Balbi72246da2011-08-19 18:10:58 +03002043 }
2044 } else {
2045 if (count && (event->status & DEPEVT_STATUS_SHORT))
2046 s_pkt = 1;
2047 }
2048
2049 /*
2050 * We assume here we will always receive the entire data block
2051 * which we should receive. Meaning, if we program RX to
2052 * receive 4K but we receive only 2K, we assume that's all we
2053 * should receive and we simply bounce the request back to the
2054 * gadget driver for further processing.
2055 */
2056 req->request.actual += req->request.length - count;
2057 dwc3_gadget_giveback(dep, req, status);
2058 if (s_pkt)
2059 break;
Felipe Balbif6bafc62012-02-06 11:04:53 +02002060 if ((event->status & DEPEVT_STATUS_LST) &&
Pratyush Anand413dba62012-06-03 19:43:19 +05302061 (trb->ctrl & (DWC3_TRB_CTRL_LST |
2062 DWC3_TRB_CTRL_HWO)))
Felipe Balbi72246da2011-08-19 18:10:58 +03002063 break;
Felipe Balbif6bafc62012-02-06 11:04:53 +02002064 if ((event->status & DEPEVT_STATUS_IOC) &&
2065 (trb->ctrl & DWC3_TRB_CTRL_IOC))
Felipe Balbi72246da2011-08-19 18:10:58 +03002066 break;
2067 } while (1);
2068
Pratyush Anand18bbcb02013-01-14 15:59:34 +05302069 if (usb_endpoint_xfer_isoc(dep->endpoint.desc) &&
2070 list_empty(&dep->req_queued)) {
Vijayavardhan Vennapusa91ba6532013-01-30 17:35:45 +05302071 if (list_empty(&dep->request_list))
Pratyush Anand18bbcb02013-01-14 15:59:34 +05302072 /*
2073 * If there is no entry in request list then do
2074 * not issue END TRANSFER now. Just set PENDING
2075 * flag, so that END TRANSFER is issued when an
2076 * entry is added into request list.
2077 */
Vijayavardhan Vennapusa91ba6532013-01-30 17:35:45 +05302078 dep->flags |= DWC3_EP_PENDING_REQUEST;
2079 else
Pratyush Anand18bbcb02013-01-14 15:59:34 +05302080 dwc3_stop_active_transfer(dwc, dep->number);
Vijayavardhan Vennapusa91ba6532013-01-30 17:35:45 +05302081 dep->flags &= ~DWC3_EP_MISSED_ISOC;
Pratyush Anand921b0b82013-01-14 15:59:32 +05302082 return 1;
2083 }
2084
Felipe Balbif6bafc62012-02-06 11:04:53 +02002085 if ((event->status & DEPEVT_STATUS_IOC) &&
2086 (trb->ctrl & DWC3_TRB_CTRL_IOC))
Felipe Balbi72246da2011-08-19 18:10:58 +03002087 return 0;
2088 return 1;
2089}
2090
2091static void dwc3_endpoint_transfer_complete(struct dwc3 *dwc,
2092 struct dwc3_ep *dep, const struct dwc3_event_depevt *event,
2093 int start_new)
2094{
2095 unsigned status = 0;
2096 int clean_busy;
2097
2098 if (event->status & DEPEVT_STATUS_BUSERR)
2099 status = -ECONNRESET;
2100
Paul Zimmerman1d046792012-02-15 18:56:56 -08002101 clean_busy = dwc3_cleanup_done_reqs(dwc, dep, event, status);
Paul Zimmermanc2df85c2012-02-24 17:32:18 -08002102 if (clean_busy)
Felipe Balbi72246da2011-08-19 18:10:58 +03002103 dep->flags &= ~DWC3_EP_BUSY;
Felipe Balbifae2b902011-10-14 13:00:30 +03002104
2105 /*
2106 * WORKAROUND: This is the 2nd half of U1/U2 -> U0 workaround.
2107 * See dwc3_gadget_linksts_change_interrupt() for 1st half.
2108 */
2109 if (dwc->revision < DWC3_REVISION_183A) {
2110 u32 reg;
2111 int i;
2112
2113 for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) {
Moiz Sonasatheed03f12012-08-01 14:08:30 -05002114 dep = dwc->eps[i];
Felipe Balbifae2b902011-10-14 13:00:30 +03002115
2116 if (!(dep->flags & DWC3_EP_ENABLED))
2117 continue;
2118
2119 if (!list_empty(&dep->req_queued))
2120 return;
2121 }
2122
2123 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2124 reg |= dwc->u1u2;
2125 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2126
2127 dwc->u1u2 = 0;
2128 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002129}
2130
Felipe Balbi72246da2011-08-19 18:10:58 +03002131static void dwc3_endpoint_interrupt(struct dwc3 *dwc,
2132 const struct dwc3_event_depevt *event)
2133{
2134 struct dwc3_ep *dep;
2135 u8 epnum = event->endpoint_number;
2136
2137 dep = dwc->eps[epnum];
2138
Felipe Balbia09be0a2012-06-06 09:19:35 +03002139 if (!(dep->flags & DWC3_EP_ENABLED))
2140 return;
2141
Felipe Balbi72246da2011-08-19 18:10:58 +03002142 dev_vdbg(dwc->dev, "%s: %s\n", dep->name,
2143 dwc3_ep_event_string(event->endpoint_event));
2144
2145 if (epnum == 0 || epnum == 1) {
2146 dwc3_ep0_interrupt(dwc, event);
2147 return;
2148 }
2149
2150 switch (event->endpoint_event) {
2151 case DWC3_DEPEVT_XFERCOMPLETE:
Felipe Balbi4959cfc2012-06-06 12:04:13 +03002152 dep->resource_index = 0;
Paul Zimmermanc2df85c2012-02-24 17:32:18 -08002153
Ido Shayevitz57cdac12012-03-12 20:25:24 +02002154 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbi72246da2011-08-19 18:10:58 +03002155 dev_dbg(dwc->dev, "%s is an Isochronous endpoint\n",
2156 dep->name);
2157 return;
2158 }
2159
2160 dwc3_endpoint_transfer_complete(dwc, dep, event, 1);
2161 break;
2162 case DWC3_DEPEVT_XFERINPROGRESS:
Ido Shayevitz57cdac12012-03-12 20:25:24 +02002163 if (!usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbi72246da2011-08-19 18:10:58 +03002164 dev_dbg(dwc->dev, "%s is not an Isochronous endpoint\n",
2165 dep->name);
2166 return;
2167 }
2168
2169 dwc3_endpoint_transfer_complete(dwc, dep, event, 0);
2170 break;
2171 case DWC3_DEPEVT_XFERNOTREADY:
Ido Shayevitz57cdac12012-03-12 20:25:24 +02002172 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbi72246da2011-08-19 18:10:58 +03002173 dwc3_gadget_start_isoc(dwc, dep, event);
2174 } else {
2175 int ret;
2176
2177 dev_vdbg(dwc->dev, "%s: reason %s\n",
Felipe Balbi40aa41f2012-01-18 17:06:03 +02002178 dep->name, event->status &
2179 DEPEVT_STATUS_TRANSFER_ACTIVE
Felipe Balbi72246da2011-08-19 18:10:58 +03002180 ? "Transfer Active"
2181 : "Transfer Not Active");
2182
2183 ret = __dwc3_gadget_kick_transfer(dep, 0, 1);
2184 if (!ret || ret == -EBUSY)
2185 return;
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +05302186 else
2187 dbg_event(dep->number, "QUEUE", ret);
Felipe Balbi72246da2011-08-19 18:10:58 +03002188
2189 dev_dbg(dwc->dev, "%s: failed to kick transfers\n",
2190 dep->name);
2191 }
2192
2193 break;
Felipe Balbi879631a2011-09-30 10:58:47 +03002194 case DWC3_DEPEVT_STREAMEVT:
Ido Shayevitz57cdac12012-03-12 20:25:24 +02002195 if (!usb_endpoint_xfer_bulk(dep->endpoint.desc)) {
Felipe Balbi879631a2011-09-30 10:58:47 +03002196 dev_err(dwc->dev, "Stream event for non-Bulk %s\n",
2197 dep->name);
2198 return;
2199 }
2200
2201 switch (event->status) {
2202 case DEPEVT_STREAMEVT_FOUND:
2203 dev_vdbg(dwc->dev, "Stream %d found and started\n",
2204 event->parameters);
2205
2206 break;
2207 case DEPEVT_STREAMEVT_NOTFOUND:
2208 /* FALLTHROUGH */
2209 default:
2210 dev_dbg(dwc->dev, "Couldn't find suitable stream\n");
2211 }
2212 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03002213 case DWC3_DEPEVT_RXTXFIFOEVT:
2214 dev_dbg(dwc->dev, "%s FIFO Overrun\n", dep->name);
2215 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03002216 case DWC3_DEPEVT_EPCMDCMPLT:
Felipe Balbib129eb72012-02-17 12:10:04 +02002217 dev_vdbg(dwc->dev, "Endpoint Command Complete\n");
Felipe Balbi72246da2011-08-19 18:10:58 +03002218 break;
2219 }
2220}
2221
2222static void dwc3_disconnect_gadget(struct dwc3 *dwc)
2223{
2224 if (dwc->gadget_driver && dwc->gadget_driver->disconnect) {
2225 spin_unlock(&dwc->lock);
2226 dwc->gadget_driver->disconnect(&dwc->gadget);
2227 spin_lock(&dwc->lock);
2228 }
2229}
2230
2231static void dwc3_stop_active_transfer(struct dwc3 *dwc, u32 epnum)
2232{
2233 struct dwc3_ep *dep;
2234 struct dwc3_gadget_ep_cmd_params params;
2235 u32 cmd;
2236 int ret;
2237
2238 dep = dwc->eps[epnum];
2239
Felipe Balbi4959cfc2012-06-06 12:04:13 +03002240 if (!dep->resource_index)
Pratyush Anand6263ebe2012-06-23 02:23:08 +05302241 return;
2242
Pratyush Anande67fdeb2012-07-06 15:19:10 +05302243 /*
2244 * NOTICE: We are violating what the Databook says about the
2245 * EndTransfer command. Ideally we would _always_ wait for the
2246 * EndTransfer Command Completion IRQ, but that's causing too
2247 * much trouble synchronizing between us and gadget driver.
2248 *
2249 * We have discussed this with the IP Provider and it was
2250 * suggested to giveback all requests here, but give HW some
2251 * extra time to synchronize with the interconnect. We're using
2252 * an arbitraty 100us delay for that.
2253 *
2254 * Note also that a similar handling was tested by Synopsys
2255 * (thanks a lot Paul) and nothing bad has come out of it.
2256 * In short, what we're doing is:
2257 *
2258 * - Issue EndTransfer WITH CMDIOC bit set
2259 * - Wait 100us
2260 */
2261
Pratyush Anand6263ebe2012-06-23 02:23:08 +05302262 cmd = DWC3_DEPCMD_ENDTRANSFER;
2263 cmd |= DWC3_DEPCMD_HIPRI_FORCERM | DWC3_DEPCMD_CMDIOC;
Felipe Balbi4959cfc2012-06-06 12:04:13 +03002264 cmd |= DWC3_DEPCMD_PARAM(dep->resource_index);
Pratyush Anand6263ebe2012-06-23 02:23:08 +05302265 memset(&params, 0, sizeof(params));
2266 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number, cmd, &params);
2267 WARN_ON_ONCE(ret);
Felipe Balbi4959cfc2012-06-06 12:04:13 +03002268 dep->resource_index = 0;
Felipe Balbi1df89b62012-10-04 11:58:00 +03002269 dep->flags &= ~DWC3_EP_BUSY;
Pratyush Anande67fdeb2012-07-06 15:19:10 +05302270 udelay(100);
Felipe Balbi72246da2011-08-19 18:10:58 +03002271}
2272
2273static void dwc3_stop_active_transfers(struct dwc3 *dwc)
2274{
2275 u32 epnum;
2276
2277 for (epnum = 2; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
2278 struct dwc3_ep *dep;
2279
2280 dep = dwc->eps[epnum];
2281 if (!(dep->flags & DWC3_EP_ENABLED))
2282 continue;
2283
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +02002284 dwc3_remove_requests(dwc, dep);
Felipe Balbi72246da2011-08-19 18:10:58 +03002285 }
2286}
2287
2288static void dwc3_clear_stall_all_ep(struct dwc3 *dwc)
2289{
2290 u32 epnum;
2291
2292 for (epnum = 1; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
2293 struct dwc3_ep *dep;
2294 struct dwc3_gadget_ep_cmd_params params;
2295 int ret;
2296
2297 dep = dwc->eps[epnum];
2298
2299 if (!(dep->flags & DWC3_EP_STALL))
2300 continue;
2301
2302 dep->flags &= ~DWC3_EP_STALL;
2303
2304 memset(&params, 0, sizeof(params));
2305 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number,
2306 DWC3_DEPCMD_CLEARSTALL, &params);
2307 WARN_ON_ONCE(ret);
2308 }
2309}
2310
2311static void dwc3_gadget_disconnect_interrupt(struct dwc3 *dwc)
2312{
Felipe Balbi34d548c2012-05-24 10:30:01 +03002313 int reg;
2314
Felipe Balbi72246da2011-08-19 18:10:58 +03002315 dev_vdbg(dwc->dev, "%s\n", __func__);
Felipe Balbi72246da2011-08-19 18:10:58 +03002316
2317 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2318 reg &= ~DWC3_DCTL_INITU1ENA;
2319 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2320
2321 reg &= ~DWC3_DCTL_INITU2ENA;
2322 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +03002323
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +05302324 dbg_event(0xFF, "DISCONNECT", 0);
Felipe Balbi72246da2011-08-19 18:10:58 +03002325 dwc3_disconnect_gadget(dwc);
Paul Zimmermanb23c8432011-09-30 10:58:42 +03002326 dwc->start_config_issued = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03002327
2328 dwc->gadget.speed = USB_SPEED_UNKNOWN;
Felipe Balbidf62df52011-10-14 15:11:49 +03002329 dwc->setup_packet_pending = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03002330}
2331
Paul Zimmermandffb81b2012-04-27 12:54:05 +03002332static void dwc3_gadget_usb3_phy_suspend(struct dwc3 *dwc, int suspend)
Felipe Balbi72246da2011-08-19 18:10:58 +03002333{
2334 u32 reg;
2335
2336 reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0));
2337
Paul Zimmermandffb81b2012-04-27 12:54:05 +03002338 if (suspend)
Felipe Balbi72246da2011-08-19 18:10:58 +03002339 reg |= DWC3_GUSB3PIPECTL_SUSPHY;
Paul Zimmermandffb81b2012-04-27 12:54:05 +03002340 else
2341 reg &= ~DWC3_GUSB3PIPECTL_SUSPHY;
Felipe Balbi72246da2011-08-19 18:10:58 +03002342
2343 dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg);
2344}
2345
Paul Zimmermandffb81b2012-04-27 12:54:05 +03002346static void dwc3_gadget_usb2_phy_suspend(struct dwc3 *dwc, int suspend)
Felipe Balbi72246da2011-08-19 18:10:58 +03002347{
2348 u32 reg;
2349
2350 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
2351
Paul Zimmermandffb81b2012-04-27 12:54:05 +03002352 if (suspend)
Felipe Balbi72246da2011-08-19 18:10:58 +03002353 reg |= DWC3_GUSB2PHYCFG_SUSPHY;
Paul Zimmermandffb81b2012-04-27 12:54:05 +03002354 else
2355 reg &= ~DWC3_GUSB2PHYCFG_SUSPHY;
Felipe Balbi72246da2011-08-19 18:10:58 +03002356
2357 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
2358}
2359
2360static void dwc3_gadget_reset_interrupt(struct dwc3 *dwc)
2361{
2362 u32 reg;
Vijayavardhan Vennapusa908f1ed2012-10-19 19:51:48 +05302363 struct dwc3_otg *dotg = dwc->dotg;
Felipe Balbi72246da2011-08-19 18:10:58 +03002364
2365 dev_vdbg(dwc->dev, "%s\n", __func__);
2366
Felipe Balbidf62df52011-10-14 15:11:49 +03002367 /*
2368 * WORKAROUND: DWC3 revisions <1.88a have an issue which
2369 * would cause a missing Disconnect Event if there's a
2370 * pending Setup Packet in the FIFO.
2371 *
2372 * There's no suggested workaround on the official Bug
2373 * report, which states that "unless the driver/application
2374 * is doing any special handling of a disconnect event,
2375 * there is no functional issue".
2376 *
2377 * Unfortunately, it turns out that we _do_ some special
2378 * handling of a disconnect event, namely complete all
2379 * pending transfers, notify gadget driver of the
2380 * disconnection, and so on.
2381 *
2382 * Our suggested workaround is to follow the Disconnect
2383 * Event steps here, instead, based on a setup_packet_pending
2384 * flag. Such flag gets set whenever we have a XferNotReady
2385 * event on EP0 and gets cleared on XferComplete for the
2386 * same endpoint.
2387 *
2388 * Refers to:
2389 *
2390 * STAR#9000466709: RTL: Device : Disconnect event not
2391 * generated if setup packet pending in FIFO
2392 */
2393 if (dwc->revision < DWC3_REVISION_188A) {
2394 if (dwc->setup_packet_pending)
2395 dwc3_gadget_disconnect_interrupt(dwc);
2396 }
2397
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +05302398 dbg_event(0xFF, "BUS RST", 0);
Felipe Balbi961906e2011-12-20 15:37:21 +02002399 /* after reset -> Default State */
2400 dwc->dev_state = DWC3_DEFAULT_STATE;
2401
Paul Zimmerman88df4272012-04-27 13:10:52 +03002402 /* Recent versions support automatic phy suspend and don't need this */
2403 if (dwc->revision < DWC3_REVISION_194A) {
2404 /* Resume PHYs */
2405 dwc3_gadget_usb2_phy_suspend(dwc, false);
2406 dwc3_gadget_usb3_phy_suspend(dwc, false);
2407 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002408
Vijayavardhan Vennapusa908f1ed2012-10-19 19:51:48 +05302409 if (dotg && dotg->otg.phy)
2410 usb_phy_set_power(dotg->otg.phy, 0);
2411
Felipe Balbi72246da2011-08-19 18:10:58 +03002412 if (dwc->gadget.speed != USB_SPEED_UNKNOWN)
2413 dwc3_disconnect_gadget(dwc);
2414
2415 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2416 reg &= ~DWC3_DCTL_TSTCTRL_MASK;
2417 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Gerard Cauvy3b637362012-02-10 12:21:18 +02002418 dwc->test_mode = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03002419
2420 dwc3_stop_active_transfers(dwc);
2421 dwc3_clear_stall_all_ep(dwc);
Paul Zimmermanb23c8432011-09-30 10:58:42 +03002422 dwc->start_config_issued = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03002423
2424 /* Reset device address to zero */
2425 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2426 reg &= ~(DWC3_DCFG_DEVADDR_MASK);
2427 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +03002428}
2429
2430static void dwc3_update_ram_clk_sel(struct dwc3 *dwc, u32 speed)
2431{
2432 u32 reg;
2433 u32 usb30_clock = DWC3_GCTL_CLK_BUS;
2434
2435 /*
2436 * We change the clock only at SS but I dunno why I would want to do
2437 * this. Maybe it becomes part of the power saving plan.
2438 */
2439
2440 if (speed != DWC3_DSTS_SUPERSPEED)
2441 return;
2442
2443 /*
2444 * RAMClkSel is reset to 0 after USB reset, so it must be reprogrammed
2445 * each time on Connect Done.
2446 */
2447 if (!usb30_clock)
2448 return;
2449
2450 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
2451 reg |= DWC3_GCTL_RAMCLKSEL(usb30_clock);
2452 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
2453}
2454
Paul Zimmermandffb81b2012-04-27 12:54:05 +03002455static void dwc3_gadget_phy_suspend(struct dwc3 *dwc, u8 speed)
Felipe Balbi72246da2011-08-19 18:10:58 +03002456{
2457 switch (speed) {
2458 case USB_SPEED_SUPER:
Paul Zimmermandffb81b2012-04-27 12:54:05 +03002459 dwc3_gadget_usb2_phy_suspend(dwc, true);
Felipe Balbi72246da2011-08-19 18:10:58 +03002460 break;
2461 case USB_SPEED_HIGH:
2462 case USB_SPEED_FULL:
2463 case USB_SPEED_LOW:
Paul Zimmermandffb81b2012-04-27 12:54:05 +03002464 dwc3_gadget_usb3_phy_suspend(dwc, true);
Felipe Balbi72246da2011-08-19 18:10:58 +03002465 break;
2466 }
2467}
2468
2469static void dwc3_gadget_conndone_interrupt(struct dwc3 *dwc)
2470{
2471 struct dwc3_gadget_ep_cmd_params params;
2472 struct dwc3_ep *dep;
2473 int ret;
2474 u32 reg;
2475 u8 speed;
2476
2477 dev_vdbg(dwc->dev, "%s\n", __func__);
2478
2479 memset(&params, 0x00, sizeof(params));
2480
Felipe Balbi72246da2011-08-19 18:10:58 +03002481 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
2482 speed = reg & DWC3_DSTS_CONNECTSPD;
2483 dwc->speed = speed;
2484
2485 dwc3_update_ram_clk_sel(dwc, speed);
2486
2487 switch (speed) {
2488 case DWC3_DCFG_SUPERSPEED:
Felipe Balbi05870c52011-10-14 14:51:38 +03002489 /*
2490 * WORKAROUND: DWC3 revisions <1.90a have an issue which
2491 * would cause a missing USB3 Reset event.
2492 *
2493 * In such situations, we should force a USB3 Reset
2494 * event by calling our dwc3_gadget_reset_interrupt()
2495 * routine.
2496 *
2497 * Refers to:
2498 *
2499 * STAR#9000483510: RTL: SS : USB3 reset event may
2500 * not be generated always when the link enters poll
2501 */
2502 if (dwc->revision < DWC3_REVISION_190A)
2503 dwc3_gadget_reset_interrupt(dwc);
2504
Felipe Balbi72246da2011-08-19 18:10:58 +03002505 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
2506 dwc->gadget.ep0->maxpacket = 512;
2507 dwc->gadget.speed = USB_SPEED_SUPER;
2508 break;
2509 case DWC3_DCFG_HIGHSPEED:
2510 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
2511 dwc->gadget.ep0->maxpacket = 64;
2512 dwc->gadget.speed = USB_SPEED_HIGH;
2513 break;
2514 case DWC3_DCFG_FULLSPEED2:
2515 case DWC3_DCFG_FULLSPEED1:
2516 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
2517 dwc->gadget.ep0->maxpacket = 64;
2518 dwc->gadget.speed = USB_SPEED_FULL;
2519 break;
2520 case DWC3_DCFG_LOWSPEED:
2521 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(8);
2522 dwc->gadget.ep0->maxpacket = 8;
2523 dwc->gadget.speed = USB_SPEED_LOW;
2524 break;
2525 }
2526
Paul Zimmerman88df4272012-04-27 13:10:52 +03002527 /* Recent versions support automatic phy suspend and don't need this */
2528 if (dwc->revision < DWC3_REVISION_194A) {
2529 /* Suspend unneeded PHY */
2530 dwc3_gadget_phy_suspend(dwc, dwc->gadget.speed);
2531 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002532
2533 dep = dwc->eps[0];
Felipe Balbi07e0ee82012-07-16 14:08:16 +03002534 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, true);
Felipe Balbi72246da2011-08-19 18:10:58 +03002535 if (ret) {
2536 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
2537 return;
2538 }
2539
2540 dep = dwc->eps[1];
Felipe Balbi07e0ee82012-07-16 14:08:16 +03002541 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, true);
Felipe Balbi72246da2011-08-19 18:10:58 +03002542 if (ret) {
2543 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
2544 return;
2545 }
2546
2547 /*
2548 * Configure PHY via GUSB3PIPECTLn if required.
2549 *
2550 * Update GTXFIFOSIZn
2551 *
2552 * In both cases reset values should be sufficient.
2553 */
2554}
2555
2556static void dwc3_gadget_wakeup_interrupt(struct dwc3 *dwc)
2557{
2558 dev_vdbg(dwc->dev, "%s\n", __func__);
2559
2560 /*
2561 * TODO take core out of low power mode when that's
2562 * implemented.
2563 */
2564
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +05302565 dbg_event(0xFF, "WAKEUP", 0);
Felipe Balbi72246da2011-08-19 18:10:58 +03002566 dwc->gadget_driver->resume(&dwc->gadget);
2567}
2568
2569static void dwc3_gadget_linksts_change_interrupt(struct dwc3 *dwc,
2570 unsigned int evtinfo)
2571{
Felipe Balbifae2b902011-10-14 13:00:30 +03002572 enum dwc3_link_state next = evtinfo & DWC3_LINK_STATE_MASK;
2573
2574 /*
2575 * WORKAROUND: DWC3 Revisions <1.83a have an issue which, depending
2576 * on the link partner, the USB session might do multiple entry/exit
2577 * of low power states before a transfer takes place.
2578 *
2579 * Due to this problem, we might experience lower throughput. The
2580 * suggested workaround is to disable DCTL[12:9] bits if we're
2581 * transitioning from U1/U2 to U0 and enable those bits again
2582 * after a transfer completes and there are no pending transfers
2583 * on any of the enabled endpoints.
2584 *
2585 * This is the first half of that workaround.
2586 *
2587 * Refers to:
2588 *
2589 * STAR#9000446952: RTL: Device SS : if U1/U2 ->U0 takes >128us
2590 * core send LGO_Ux entering U0
2591 */
2592 if (dwc->revision < DWC3_REVISION_183A) {
2593 if (next == DWC3_LINK_STATE_U0) {
2594 u32 u1u2;
2595 u32 reg;
2596
2597 switch (dwc->link_state) {
2598 case DWC3_LINK_STATE_U1:
2599 case DWC3_LINK_STATE_U2:
2600 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2601 u1u2 = reg & (DWC3_DCTL_INITU2ENA
2602 | DWC3_DCTL_ACCEPTU2ENA
2603 | DWC3_DCTL_INITU1ENA
2604 | DWC3_DCTL_ACCEPTU1ENA);
2605
2606 if (!dwc->u1u2)
2607 dwc->u1u2 = reg & u1u2;
2608
2609 reg &= ~u1u2;
2610
2611 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2612 break;
2613 default:
2614 /* do nothing */
2615 break;
2616 }
2617 }
2618 }
2619
Vijayavardhan Vennapusa54be1d62012-10-06 18:32:06 +05302620 if (next == DWC3_LINK_STATE_U0) {
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +05302621 if (dwc->link_state == DWC3_LINK_STATE_U3) {
2622 dbg_event(0xFF, "RESUME", 0);
Vijayavardhan Vennapusa54be1d62012-10-06 18:32:06 +05302623 dwc->gadget_driver->resume(&dwc->gadget);
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +05302624 }
Vijayavardhan Vennapusa54be1d62012-10-06 18:32:06 +05302625 } else if (next == DWC3_LINK_STATE_U3) {
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +05302626 dbg_event(0xFF, "SUSPEND", 0);
Vijayavardhan Vennapusa54be1d62012-10-06 18:32:06 +05302627 dwc->gadget_driver->suspend(&dwc->gadget);
2628 }
2629
Felipe Balbifae2b902011-10-14 13:00:30 +03002630 dwc->link_state = next;
Felipe Balbi019ac832011-09-08 21:18:47 +03002631
2632 dev_vdbg(dwc->dev, "%s link %d\n", __func__, dwc->link_state);
Felipe Balbi72246da2011-08-19 18:10:58 +03002633}
2634
Vijayavardhan Vennapusa8a011c92013-07-29 09:06:48 +05302635static void dwc3_dump_reg_info(struct dwc3 *dwc)
2636{
2637 dbg_event(0xFF, "REGDUMP", 0);
2638
2639 dbg_print_reg("GUSB3PIPCTL", dwc3_readl(dwc->regs,
2640 DWC3_GUSB3PIPECTL(0)));
2641 dbg_print_reg("GUSB2PHYCONFIG", dwc3_readl(dwc->regs,
2642 DWC3_GUSB2PHYCFG(0)));
2643 dbg_print_reg("GCTL", dwc3_readl(dwc->regs, DWC3_GCTL));
2644 dbg_print_reg("GUCTL", dwc3_readl(dwc->regs, DWC3_GUCTL));
2645 dbg_print_reg("GDBGLTSSM", dwc3_readl(dwc->regs, DWC3_GDBGLTSSM));
2646 dbg_print_reg("DCFG", dwc3_readl(dwc->regs, DWC3_DCFG));
2647 dbg_print_reg("DCTL", dwc3_readl(dwc->regs, DWC3_DCTL));
2648 dbg_print_reg("DEVTEN", dwc3_readl(dwc->regs, DWC3_DEVTEN));
2649 dbg_print_reg("DSTS", dwc3_readl(dwc->regs, DWC3_DSTS));
2650 dbg_print_reg("DALPENA", dwc3_readl(dwc->regs, DWC3_DALEPENA));
2651 dbg_print_reg("DGCMD", dwc3_readl(dwc->regs, DWC3_DGCMD));
2652
2653 dbg_print_reg("OCFG", dwc3_readl(dwc->regs, DWC3_OCFG));
2654 dbg_print_reg("OCTL", dwc3_readl(dwc->regs, DWC3_OCTL));
2655 dbg_print_reg("OEVT", dwc3_readl(dwc->regs, DWC3_OEVT));
2656 dbg_print_reg("OSTS", dwc3_readl(dwc->regs, DWC3_OSTS));
2657
2658 dwc3_notify_event(dwc, DWC3_CONTROLLER_ERROR_EVENT);
Vijayavardhan Vennapusa8a011c92013-07-29 09:06:48 +05302659}
2660
Felipe Balbi72246da2011-08-19 18:10:58 +03002661static void dwc3_gadget_interrupt(struct dwc3 *dwc,
2662 const struct dwc3_event_devt *event)
2663{
2664 switch (event->type) {
2665 case DWC3_DEVICE_EVENT_DISCONNECT:
2666 dwc3_gadget_disconnect_interrupt(dwc);
2667 break;
2668 case DWC3_DEVICE_EVENT_RESET:
2669 dwc3_gadget_reset_interrupt(dwc);
2670 break;
2671 case DWC3_DEVICE_EVENT_CONNECT_DONE:
2672 dwc3_gadget_conndone_interrupt(dwc);
2673 break;
2674 case DWC3_DEVICE_EVENT_WAKEUP:
2675 dwc3_gadget_wakeup_interrupt(dwc);
2676 break;
2677 case DWC3_DEVICE_EVENT_LINK_STATUS_CHANGE:
2678 dwc3_gadget_linksts_change_interrupt(dwc, event->event_info);
2679 break;
2680 case DWC3_DEVICE_EVENT_EOPF:
2681 dev_vdbg(dwc->dev, "End of Periodic Frame\n");
2682 break;
2683 case DWC3_DEVICE_EVENT_SOF:
2684 dev_vdbg(dwc->dev, "Start of Periodic Frame\n");
2685 break;
2686 case DWC3_DEVICE_EVENT_ERRATIC_ERROR:
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +05302687 dbg_event(0xFF, "ERROR", 0);
Felipe Balbi72246da2011-08-19 18:10:58 +03002688 dev_vdbg(dwc->dev, "Erratic Error\n");
Vijayavardhan Vennapusa8a011c92013-07-29 09:06:48 +05302689 dwc3_dump_reg_info(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +03002690 break;
2691 case DWC3_DEVICE_EVENT_CMD_CMPL:
2692 dev_vdbg(dwc->dev, "Command Complete\n");
2693 break;
2694 case DWC3_DEVICE_EVENT_OVERFLOW:
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +05302695 dbg_event(0xFF, "OVERFL", 0);
Felipe Balbi72246da2011-08-19 18:10:58 +03002696 dev_vdbg(dwc->dev, "Overflow\n");
Pavankumar Kondetid393e172012-06-12 16:07:29 +05302697 /*
2698 * Controllers prior to 2.30a revision has a bug where
2699 * Overflow Event may overwrite an unacknowledged event
2700 * in the event buffer. The severity of the issue depends
2701 * on the overwritten event type. Add a warning message
2702 * saying that an event is overwritten.
2703 *
2704 * TODO: In future we may need to see if we can re-enumerate
2705 * with host.
2706 */
2707 if (dwc->revision < DWC3_REVISION_230A)
2708 dev_warn(dwc->dev, "Unacknowledged event overwritten\n");
Felipe Balbi72246da2011-08-19 18:10:58 +03002709 break;
Pavankumar Kondeti33fe6f12012-06-12 16:21:46 +05302710 case DWC3_DEVICE_EVENT_VENDOR_DEV_TEST_LMP:
2711 /*
2712 * Controllers prior to 2.30a revision has a bug, due to which
2713 * a vendor device test LMP event can not be filtered. But
2714 * this event is not handled in the current code. This is a
2715 * special event and 8 bytes of data will follow the event.
2716 * Handling this event is tricky when event buffer is almost
2717 * full. Moreover this event will not occur in normal scenario
2718 * and can only happen with special hosts in testing scenarios.
2719 * Add a warning message to indicate that this event is received
2720 * which means that event buffer might have corrupted.
2721 */
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +05302722 dbg_event(0xFF, "TSTLMP", 0);
Pavankumar Kondeti33fe6f12012-06-12 16:21:46 +05302723 if (dwc->revision < DWC3_REVISION_230A)
2724 dev_warn(dwc->dev, "Vendor Device Test LMP Received\n");
2725 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03002726 default:
2727 dev_dbg(dwc->dev, "UNKNOWN IRQ %d\n", event->type);
2728 }
2729}
2730
2731static void dwc3_process_event_entry(struct dwc3 *dwc,
2732 const union dwc3_event *event)
2733{
2734 /* Endpoint IRQ, handle it and return early */
2735 if (event->type.is_devspec == 0) {
2736 /* depevt */
2737 return dwc3_endpoint_interrupt(dwc, &event->depevt);
2738 }
2739
2740 switch (event->type.type) {
2741 case DWC3_EVENT_TYPE_DEV:
2742 dwc3_gadget_interrupt(dwc, &event->devt);
2743 break;
2744 /* REVISIT what to do with Carkit and I2C events ? */
2745 default:
2746 dev_err(dwc->dev, "UNKNOWN IRQ type %d\n", event->raw);
2747 }
2748}
2749
2750static irqreturn_t dwc3_process_event_buf(struct dwc3 *dwc, u32 buf)
2751{
2752 struct dwc3_event_buffer *evt;
2753 int left;
2754 u32 count;
2755
2756 count = dwc3_readl(dwc->regs, DWC3_GEVNTCOUNT(buf));
2757 count &= DWC3_GEVNTCOUNT_MASK;
2758 if (!count)
2759 return IRQ_NONE;
2760
2761 evt = dwc->ev_buffs[buf];
2762 left = count;
2763
2764 while (left > 0) {
2765 union dwc3_event event;
2766
Felipe Balbid70d8442012-02-06 13:40:17 +02002767 event.raw = *(u32 *) (evt->buf + evt->lpos);
2768
Felipe Balbi72246da2011-08-19 18:10:58 +03002769 dwc3_process_event_entry(dwc, &event);
2770 /*
2771 * XXX we wrap around correctly to the next entry as almost all
2772 * entries are 4 bytes in size. There is one entry which has 12
2773 * bytes which is a regular entry followed by 8 bytes data. ATM
2774 * I don't know how things are organized if were get next to the
2775 * a boundary so I worry about that once we try to handle that.
2776 */
2777 evt->lpos = (evt->lpos + 4) % DWC3_EVENT_BUFFERS_SIZE;
2778 left -= 4;
2779
2780 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(buf), 4);
2781 }
2782
2783 return IRQ_HANDLED;
2784}
2785
2786static irqreturn_t dwc3_interrupt(int irq, void *_dwc)
2787{
2788 struct dwc3 *dwc = _dwc;
2789 int i;
2790 irqreturn_t ret = IRQ_NONE;
2791
2792 spin_lock(&dwc->lock);
2793
Felipe Balbi9f622b22011-10-12 10:31:04 +03002794 for (i = 0; i < dwc->num_event_buffers; i++) {
Felipe Balbi72246da2011-08-19 18:10:58 +03002795 irqreturn_t status;
2796
2797 status = dwc3_process_event_buf(dwc, i);
2798 if (status == IRQ_HANDLED)
2799 ret = status;
2800 }
2801
2802 spin_unlock(&dwc->lock);
2803
2804 return ret;
2805}
2806
2807/**
2808 * dwc3_gadget_init - Initializes gadget related registers
Paul Zimmerman1d046792012-02-15 18:56:56 -08002809 * @dwc: pointer to our controller context structure
Felipe Balbi72246da2011-08-19 18:10:58 +03002810 *
2811 * Returns 0 on success otherwise negative errno.
2812 */
2813int __devinit dwc3_gadget_init(struct dwc3 *dwc)
2814{
2815 u32 reg;
2816 int ret;
2817 int irq;
2818
2819 dwc->ctrl_req = dma_alloc_coherent(dwc->dev, sizeof(*dwc->ctrl_req),
2820 &dwc->ctrl_req_addr, GFP_KERNEL);
2821 if (!dwc->ctrl_req) {
2822 dev_err(dwc->dev, "failed to allocate ctrl request\n");
2823 ret = -ENOMEM;
2824 goto err0;
2825 }
2826
2827 dwc->ep0_trb = dma_alloc_coherent(dwc->dev, sizeof(*dwc->ep0_trb),
2828 &dwc->ep0_trb_addr, GFP_KERNEL);
2829 if (!dwc->ep0_trb) {
2830 dev_err(dwc->dev, "failed to allocate ep0 trb\n");
2831 ret = -ENOMEM;
2832 goto err1;
2833 }
2834
Felipe Balbib0791fb2012-05-04 12:58:14 +03002835 dwc->setup_buf = kzalloc(DWC3_EP0_BOUNCE_SIZE, GFP_KERNEL);
Felipe Balbi72246da2011-08-19 18:10:58 +03002836 if (!dwc->setup_buf) {
2837 dev_err(dwc->dev, "failed to allocate setup buffer\n");
2838 ret = -ENOMEM;
2839 goto err2;
2840 }
2841
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002842 dwc->ep0_bounce = dma_alloc_coherent(dwc->dev,
Felipe Balbib0791fb2012-05-04 12:58:14 +03002843 DWC3_EP0_BOUNCE_SIZE, &dwc->ep0_bounce_addr,
2844 GFP_KERNEL);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002845 if (!dwc->ep0_bounce) {
2846 dev_err(dwc->dev, "failed to allocate ep0 bounce buffer\n");
2847 ret = -ENOMEM;
2848 goto err3;
2849 }
2850
Felipe Balbi72246da2011-08-19 18:10:58 +03002851 dev_set_name(&dwc->gadget.dev, "gadget");
2852
2853 dwc->gadget.ops = &dwc3_gadget_ops;
Manu Gautama7b082a2012-11-06 09:50:09 +05302854 dwc->gadget.max_speed = USB_SPEED_SUPER;
Felipe Balbi72246da2011-08-19 18:10:58 +03002855 dwc->gadget.speed = USB_SPEED_UNKNOWN;
2856 dwc->gadget.dev.parent = dwc->dev;
Felipe Balbieeb720f2011-11-28 12:46:59 +02002857 dwc->gadget.sg_supported = true;
Felipe Balbi72246da2011-08-19 18:10:58 +03002858
2859 dma_set_coherent_mask(&dwc->gadget.dev, dwc->dev->coherent_dma_mask);
2860
2861 dwc->gadget.dev.dma_parms = dwc->dev->dma_parms;
2862 dwc->gadget.dev.dma_mask = dwc->dev->dma_mask;
2863 dwc->gadget.dev.release = dwc3_gadget_release;
2864 dwc->gadget.name = "dwc3-gadget";
2865
2866 /*
2867 * REVISIT: Here we should clear all pending IRQs to be
2868 * sure we're starting from a well known location.
2869 */
2870
2871 ret = dwc3_gadget_init_endpoints(dwc);
2872 if (ret)
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002873 goto err4;
Felipe Balbi72246da2011-08-19 18:10:58 +03002874
2875 irq = platform_get_irq(to_platform_device(dwc->dev), 0);
2876
2877 ret = request_irq(irq, dwc3_interrupt, IRQF_SHARED,
2878 "dwc3", dwc);
2879 if (ret) {
2880 dev_err(dwc->dev, "failed to request irq #%d --> %d\n",
2881 irq, ret);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002882 goto err5;
Felipe Balbi72246da2011-08-19 18:10:58 +03002883 }
2884
Sebastian Andrzej Siewiorbb8b8a32011-09-13 17:54:39 +02002885 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2886 reg |= DWC3_DCFG_LPM_CAP;
2887 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
2888
Felipe Balbi72246da2011-08-19 18:10:58 +03002889 /* Enable all but Start and End of Frame IRQs */
Pavankumar Kondeti33fe6f12012-06-12 16:21:46 +05302890 reg = (DWC3_DEVTEN_EVNTOVERFLOWEN |
Felipe Balbi72246da2011-08-19 18:10:58 +03002891 DWC3_DEVTEN_CMDCMPLTEN |
2892 DWC3_DEVTEN_ERRTICERREN |
2893 DWC3_DEVTEN_WKUPEVTEN |
2894 DWC3_DEVTEN_ULSTCNGEN |
2895 DWC3_DEVTEN_CONNECTDONEEN |
2896 DWC3_DEVTEN_USBRSTEN |
2897 DWC3_DEVTEN_DISCONNEVTEN);
2898 dwc3_writel(dwc->regs, DWC3_DEVTEN, reg);
2899
Paul Zimmerman88df4272012-04-27 13:10:52 +03002900 /* Enable USB2 LPM and automatic phy suspend only on recent versions */
2901 if (dwc->revision >= DWC3_REVISION_194A) {
2902 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2903 reg |= DWC3_DCFG_LPM_CAP;
2904 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
2905
2906 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2907 reg &= ~(DWC3_DCTL_HIRD_THRES_MASK | DWC3_DCTL_L1_HIBER_EN);
2908
2909 /* TODO: This should be configurable */
Pratyush Anandd69dcdd2012-07-02 10:21:52 +05302910 reg |= DWC3_DCTL_HIRD_THRES(28);
Paul Zimmerman88df4272012-04-27 13:10:52 +03002911
2912 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2913
Vijayavardhan Vennapusaba79f6b2013-07-30 13:39:52 +05302914 dwc3_gadget_usb2_phy_suspend(dwc, true);
2915 dwc3_gadget_usb3_phy_suspend(dwc, true);
Paul Zimmerman88df4272012-04-27 13:10:52 +03002916 }
2917
Felipe Balbi72246da2011-08-19 18:10:58 +03002918 ret = device_register(&dwc->gadget.dev);
2919 if (ret) {
2920 dev_err(dwc->dev, "failed to register gadget device\n");
2921 put_device(&dwc->gadget.dev);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002922 goto err6;
Felipe Balbi72246da2011-08-19 18:10:58 +03002923 }
2924
2925 ret = usb_add_gadget_udc(dwc->dev, &dwc->gadget);
2926 if (ret) {
2927 dev_err(dwc->dev, "failed to register udc\n");
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002928 goto err7;
Felipe Balbi72246da2011-08-19 18:10:58 +03002929 }
2930
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +02002931 if (dwc->dotg) {
2932 /* dwc3 otg driver is active (DRD mode + SRPSupport=1) */
2933 ret = otg_set_peripheral(&dwc->dotg->otg, &dwc->gadget);
2934 if (ret) {
2935 dev_err(dwc->dev, "failed to set peripheral to otg\n");
2936 goto err7;
2937 }
Manu Gautamb5067272012-07-02 09:53:41 +05302938 } else {
2939 pm_runtime_no_callbacks(&dwc->gadget.dev);
2940 pm_runtime_set_active(&dwc->gadget.dev);
2941 pm_runtime_enable(&dwc->gadget.dev);
2942 pm_runtime_get(&dwc->gadget.dev);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +02002943 }
2944
Felipe Balbi72246da2011-08-19 18:10:58 +03002945 return 0;
2946
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002947err7:
Felipe Balbi72246da2011-08-19 18:10:58 +03002948 device_unregister(&dwc->gadget.dev);
2949
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002950err6:
Felipe Balbi72246da2011-08-19 18:10:58 +03002951 dwc3_writel(dwc->regs, DWC3_DEVTEN, 0x00);
2952 free_irq(irq, dwc);
2953
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002954err5:
Felipe Balbi72246da2011-08-19 18:10:58 +03002955 dwc3_gadget_free_endpoints(dwc);
2956
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002957err4:
Felipe Balbib0791fb2012-05-04 12:58:14 +03002958 dma_free_coherent(dwc->dev, DWC3_EP0_BOUNCE_SIZE,
2959 dwc->ep0_bounce, dwc->ep0_bounce_addr);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002960
Felipe Balbi72246da2011-08-19 18:10:58 +03002961err3:
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02002962 kfree(dwc->setup_buf);
Felipe Balbi72246da2011-08-19 18:10:58 +03002963
2964err2:
2965 dma_free_coherent(dwc->dev, sizeof(*dwc->ep0_trb),
2966 dwc->ep0_trb, dwc->ep0_trb_addr);
2967
2968err1:
2969 dma_free_coherent(dwc->dev, sizeof(*dwc->ctrl_req),
2970 dwc->ctrl_req, dwc->ctrl_req_addr);
2971
2972err0:
2973 return ret;
2974}
2975
2976void dwc3_gadget_exit(struct dwc3 *dwc)
2977{
2978 int irq;
Felipe Balbi72246da2011-08-19 18:10:58 +03002979
Manu Gautamb5067272012-07-02 09:53:41 +05302980 if (dwc->dotg) {
2981 pm_runtime_put(&dwc->gadget.dev);
2982 pm_runtime_disable(&dwc->gadget.dev);
2983 }
2984
Felipe Balbi72246da2011-08-19 18:10:58 +03002985 usb_del_gadget_udc(&dwc->gadget);
2986 irq = platform_get_irq(to_platform_device(dwc->dev), 0);
2987
2988 dwc3_writel(dwc->regs, DWC3_DEVTEN, 0x00);
2989 free_irq(irq, dwc);
2990
Felipe Balbi72246da2011-08-19 18:10:58 +03002991 dwc3_gadget_free_endpoints(dwc);
2992
Felipe Balbib0791fb2012-05-04 12:58:14 +03002993 dma_free_coherent(dwc->dev, DWC3_EP0_BOUNCE_SIZE,
2994 dwc->ep0_bounce, dwc->ep0_bounce_addr);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002995
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02002996 kfree(dwc->setup_buf);
Felipe Balbi72246da2011-08-19 18:10:58 +03002997
2998 dma_free_coherent(dwc->dev, sizeof(*dwc->ep0_trb),
2999 dwc->ep0_trb, dwc->ep0_trb_addr);
3000
3001 dma_free_coherent(dwc->dev, sizeof(*dwc->ctrl_req),
3002 dwc->ctrl_req, dwc->ctrl_req_addr);
3003
3004 device_unregister(&dwc->gadget.dev);
3005}