blob: 8cf8a6d98babae5527b2586939efe3d1bd965adf [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
Vijayavardhan Vennapusadbe59bf2013-06-07 15:09:36 +053039#include <linux/module.h>
Felipe Balbi72246da2011-08-19 18:10:58 +030040#include <linux/kernel.h>
41#include <linux/delay.h>
42#include <linux/slab.h>
43#include <linux/spinlock.h>
44#include <linux/platform_device.h>
45#include <linux/pm_runtime.h>
Wesley Cheng446ad8d2013-06-05 16:15:01 +053046#include <linux/ratelimit.h>
Felipe Balbi72246da2011-08-19 18:10:58 +030047#include <linux/interrupt.h>
48#include <linux/io.h>
49#include <linux/list.h>
50#include <linux/dma-mapping.h>
51
52#include <linux/usb/ch9.h>
53#include <linux/usb/gadget.h>
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +020054#include <linux/usb/otg.h>
Felipe Balbi72246da2011-08-19 18:10:58 +030055
56#include "core.h"
57#include "gadget.h"
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +053058#include "debug.h"
Felipe Balbi72246da2011-08-19 18:10:58 +030059#include "io.h"
60
Vijayavardhan Vennapusadbe59bf2013-06-07 15:09:36 +053061static bool tx_fifo_resize_enable;
62module_param(tx_fifo_resize_enable, bool, S_IRUGO|S_IWUSR);
63MODULE_PARM_DESC(tx_fifo_resize_enable,
64 "Enable allocating Tx fifo for endpoints");
65
Vijayavardhan Vennapusaba79f6b2013-07-30 13:39:52 +053066static void dwc3_gadget_usb2_phy_suspend(struct dwc3 *dwc, int suspend);
67static void dwc3_gadget_usb3_phy_suspend(struct dwc3 *dwc, int suspend);
68
Felipe Balbi04a9bfc2012-01-02 18:25:43 +020069/**
70 * dwc3_gadget_set_test_mode - Enables USB2 Test Modes
71 * @dwc: pointer to our context structure
72 * @mode: the mode to set (J, K SE0 NAK, Force Enable)
73 *
74 * Caller should take care of locking. This function will
75 * return 0 on success or -EINVAL if wrong Test Selector
76 * is passed
77 */
78int dwc3_gadget_set_test_mode(struct dwc3 *dwc, int mode)
79{
80 u32 reg;
81
82 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
83 reg &= ~DWC3_DCTL_TSTCTRL_MASK;
84
85 switch (mode) {
86 case TEST_J:
87 case TEST_K:
88 case TEST_SE0_NAK:
89 case TEST_PACKET:
90 case TEST_FORCE_EN:
91 reg |= mode << 1;
92 break;
93 default:
94 return -EINVAL;
95 }
96
97 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
98
99 return 0;
100}
101
Felipe Balbi8598bde2012-01-02 18:55:57 +0200102/**
103 * dwc3_gadget_set_link_state - Sets USB Link to a particular State
104 * @dwc: pointer to our context structure
105 * @state: the state to put link into
106 *
107 * Caller should take care of locking. This function will
Paul Zimmermanaee63e32012-02-24 17:32:15 -0800108 * return 0 on success or -ETIMEDOUT.
Felipe Balbi8598bde2012-01-02 18:55:57 +0200109 */
110int dwc3_gadget_set_link_state(struct dwc3 *dwc, enum dwc3_link_state state)
111{
Paul Zimmermanaee63e32012-02-24 17:32:15 -0800112 int retries = 10000;
Felipe Balbi8598bde2012-01-02 18:55:57 +0200113 u32 reg;
114
Paul Zimmerman88df4272012-04-27 13:10:52 +0300115 /*
116 * Wait until device controller is ready. Only applies to 1.94a and
117 * later RTL.
118 */
119 if (dwc->revision >= DWC3_REVISION_194A) {
120 while (--retries) {
121 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
122 if (reg & DWC3_DSTS_DCNRD)
123 udelay(5);
124 else
125 break;
126 }
127
128 if (retries <= 0)
129 return -ETIMEDOUT;
130 }
131
Felipe Balbi8598bde2012-01-02 18:55:57 +0200132 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
133 reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK;
134
135 /* set requested state */
136 reg |= DWC3_DCTL_ULSTCHNGREQ(state);
137 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
138
Paul Zimmerman88df4272012-04-27 13:10:52 +0300139 /*
140 * The following code is racy when called from dwc3_gadget_wakeup,
141 * and is not needed, at least on newer versions
142 */
143 if (dwc->revision >= DWC3_REVISION_194A)
144 return 0;
145
Felipe Balbi8598bde2012-01-02 18:55:57 +0200146 /* wait for a change in DSTS */
Paul Zimmerman8b9388f2012-04-27 12:52:01 +0300147 retries = 10000;
Felipe Balbi8598bde2012-01-02 18:55:57 +0200148 while (--retries) {
149 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
150
Felipe Balbi8598bde2012-01-02 18:55:57 +0200151 if (DWC3_DSTS_USBLNKST(reg) == state)
152 return 0;
153
Paul Zimmermanaee63e32012-02-24 17:32:15 -0800154 udelay(5);
Felipe Balbi8598bde2012-01-02 18:55:57 +0200155 }
156
157 dev_vdbg(dwc->dev, "link state change request timed out\n");
158
159 return -ETIMEDOUT;
160}
161
Felipe Balbi457e84b2012-01-18 18:04:09 +0200162/**
163 * dwc3_gadget_resize_tx_fifos - reallocate fifo spaces for current use-case
164 * @dwc: pointer to our context structure
165 *
166 * This function will a best effort FIFO allocation in order
167 * to improve FIFO usage and throughput, while still allowing
168 * us to enable as many endpoints as possible.
169 *
170 * Keep in mind that this operation will be highly dependent
171 * on the configured size for RAM1 - which contains TxFifo -,
172 * the amount of endpoints enabled on coreConsultant tool, and
173 * the width of the Master Bus.
174 *
175 * In the ideal world, we would always be able to satisfy the
176 * following equation:
177 *
178 * ((512 + 2 * MDWIDTH-Bytes) + (Number of IN Endpoints - 1) * \
179 * (3 * (1024 + MDWIDTH-Bytes) + MDWIDTH-Bytes)) / MDWIDTH-Bytes
180 *
181 * Unfortunately, due to many variables that's not always the case.
182 */
183int dwc3_gadget_resize_tx_fifos(struct dwc3 *dwc)
184{
185 int last_fifo_depth = 0;
186 int ram1_depth;
187 int fifo_size;
188 int mdwidth;
189 int num;
190
Vijayavardhan Vennapusadbe59bf2013-06-07 15:09:36 +0530191 if (!dwc->needs_fifo_resize && !tx_fifo_resize_enable)
Felipe Balbi457e84b2012-01-18 18:04:09 +0200192 return 0;
193
194 ram1_depth = DWC3_RAM1_DEPTH(dwc->hwparams.hwparams7);
195 mdwidth = DWC3_MDWIDTH(dwc->hwparams.hwparams0);
196
197 /* MDWIDTH is represented in bits, we need it in bytes */
198 mdwidth >>= 3;
199
200 /*
201 * FIXME For now we will only allocate 1 wMaxPacketSize space
202 * for each enabled endpoint, later patches will come to
203 * improve this algorithm so that we better use the internal
Vijayavardhan Vennapusadaf082c2013-03-01 13:08:59 +0530204 * FIFO space. Also consider the case where TxFIFO RAM space
205 * may change dynamically based on the USB configuration.
Felipe Balbi457e84b2012-01-18 18:04:09 +0200206 */
207 for (num = 0; num < DWC3_ENDPOINTS_NUM; num++) {
208 struct dwc3_ep *dep = dwc->eps[num];
209 int fifo_number = dep->number >> 1;
Felipe Balbi2e81c362012-02-02 13:01:12 +0200210 int mult = 1;
Felipe Balbi457e84b2012-01-18 18:04:09 +0200211 int tmp;
212
213 if (!(dep->number & 1))
214 continue;
215
216 if (!(dep->flags & DWC3_EP_ENABLED))
217 continue;
218
Vijayavardhan Vennapusadaf082c2013-03-01 13:08:59 +0530219 if (((dep->endpoint.maxburst > 1) &&
220 usb_endpoint_xfer_bulk(dep->endpoint.desc))
Ido Shayevitz57cdac12012-03-12 20:25:24 +0200221 || usb_endpoint_xfer_isoc(dep->endpoint.desc))
Felipe Balbi2e81c362012-02-02 13:01:12 +0200222 mult = 3;
223
224 /*
225 * REVISIT: the following assumes we will always have enough
226 * space available on the FIFO RAM for all possible use cases.
227 * Make sure that's true somehow and change FIFO allocation
228 * accordingly.
229 *
Vijayavardhan Vennapusadaf082c2013-03-01 13:08:59 +0530230 * If we have Bulk (burst only) or Isochronous endpoints, we
231 * want them to be able to be very, very fast. So we're giving
Felipe Balbi2e81c362012-02-02 13:01:12 +0200232 * those endpoints a fifo_size which is enough for 3 full
233 * packets
234 */
235 tmp = mult * (dep->endpoint.maxpacket + mdwidth);
Felipe Balbi457e84b2012-01-18 18:04:09 +0200236 tmp += mdwidth;
237
238 fifo_size = DIV_ROUND_UP(tmp, mdwidth);
Felipe Balbi2e81c362012-02-02 13:01:12 +0200239
Felipe Balbi457e84b2012-01-18 18:04:09 +0200240 fifo_size |= (last_fifo_depth << 16);
241
242 dev_vdbg(dwc->dev, "%s: Fifo Addr %04x Size %d\n",
243 dep->name, last_fifo_depth, fifo_size & 0xffff);
244
245 dwc3_writel(dwc->regs, DWC3_GTXFIFOSIZ(fifo_number),
246 fifo_size);
247
248 last_fifo_depth += (fifo_size & 0xffff);
249 }
250
251 return 0;
252}
253
Felipe Balbi72246da2011-08-19 18:10:58 +0300254void dwc3_gadget_giveback(struct dwc3_ep *dep, struct dwc3_request *req,
255 int status)
256{
257 struct dwc3 *dwc = dep->dwc;
258
259 if (req->queued) {
Manu Gautam55d34222012-12-19 16:49:47 +0530260 req->queued = false;
261
Felipe Balbieeb720f2011-11-28 12:46:59 +0200262 if (req->request.num_mapped_sgs)
263 dep->busy_slot += req->request.num_mapped_sgs;
264 else
265 dep->busy_slot++;
266
Felipe Balbi72246da2011-08-19 18:10:58 +0300267 /*
268 * Skip LINK TRB. We can't use req->trb and check for
269 * DWC3_TRBCTL_LINK_TRB because it points the TRB we just
270 * completed (not the LINK TRB).
271 */
272 if (((dep->busy_slot & DWC3_TRB_MASK) == DWC3_TRB_NUM - 1) &&
Ido Shayevitz57cdac12012-03-12 20:25:24 +0200273 usb_endpoint_xfer_isoc(dep->endpoint.desc))
Felipe Balbi72246da2011-08-19 18:10:58 +0300274 dep->busy_slot++;
275 }
276 list_del(&req->list);
Felipe Balbieeb720f2011-11-28 12:46:59 +0200277 req->trb = NULL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300278
279 if (req->request.status == -EINPROGRESS)
280 req->request.status = status;
281
Pratyush Anand8d7bf592012-08-10 13:42:16 +0530282 if (dwc->ep0_bounced && dep->number == 0)
283 dwc->ep0_bounced = false;
284 else
285 usb_gadget_unmap_request(&dwc->gadget, &req->request,
286 req->direction);
Felipe Balbi72246da2011-08-19 18:10:58 +0300287
288 dev_dbg(dwc->dev, "request %p from %s completed %d/%d ===> %d\n",
289 req, dep->name, req->request.actual,
290 req->request.length, status);
291
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +0530292 dbg_done(dep->number, req->request.actual, req->request.status);
Felipe Balbi72246da2011-08-19 18:10:58 +0300293 spin_unlock(&dwc->lock);
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +0200294 req->request.complete(&dep->endpoint, &req->request);
Felipe Balbi72246da2011-08-19 18:10:58 +0300295 spin_lock(&dwc->lock);
296}
297
298static const char *dwc3_gadget_ep_cmd_string(u8 cmd)
299{
300 switch (cmd) {
301 case DWC3_DEPCMD_DEPSTARTCFG:
302 return "Start New Configuration";
303 case DWC3_DEPCMD_ENDTRANSFER:
304 return "End Transfer";
305 case DWC3_DEPCMD_UPDATETRANSFER:
306 return "Update Transfer";
307 case DWC3_DEPCMD_STARTTRANSFER:
308 return "Start Transfer";
309 case DWC3_DEPCMD_CLEARSTALL:
310 return "Clear Stall";
311 case DWC3_DEPCMD_SETSTALL:
312 return "Set Stall";
Paul Zimmerman88df4272012-04-27 13:10:52 +0300313 case DWC3_DEPCMD_GETEPSTATE:
314 return "Get Endpoint State";
Felipe Balbi72246da2011-08-19 18:10:58 +0300315 case DWC3_DEPCMD_SETTRANSFRESOURCE:
316 return "Set Endpoint Transfer Resource";
317 case DWC3_DEPCMD_SETEPCONFIG:
318 return "Set Endpoint Configuration";
319 default:
320 return "UNKNOWN command";
321 }
322}
323
Felipe Balbi573c2762012-04-24 16:19:11 +0300324int dwc3_send_gadget_generic_command(struct dwc3 *dwc, int cmd, u32 param)
325{
326 u32 timeout = 500;
327 u32 reg;
Vijayavardhan Vennapusaba79f6b2013-07-30 13:39:52 +0530328 bool hsphy_suspend_enabled;
329 int ret;
330
331 /* Commands to controller will work only if PHY is not suspended */
332 hsphy_suspend_enabled = (dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0)) &
333 DWC3_GUSB2PHYCFG_SUSPHY);
334
335 /* Disable suspend of the USB2 PHY */
336 if (hsphy_suspend_enabled)
337 dwc3_gadget_usb2_phy_suspend(dwc, false);
Felipe Balbi573c2762012-04-24 16:19:11 +0300338
339 dwc3_writel(dwc->regs, DWC3_DGCMDPAR, param);
340 dwc3_writel(dwc->regs, DWC3_DGCMD, cmd | DWC3_DGCMD_CMDACT);
341
342 do {
343 reg = dwc3_readl(dwc->regs, DWC3_DGCMD);
344 if (!(reg & DWC3_DGCMD_CMDACT)) {
345 dev_vdbg(dwc->dev, "Command Complete --> %d\n",
346 DWC3_DGCMD_STATUS(reg));
Vijayavardhan Vennapusaba79f6b2013-07-30 13:39:52 +0530347 ret = 0;
348 break;
Felipe Balbi573c2762012-04-24 16:19:11 +0300349 }
350
351 /*
352 * We can't sleep here, because it's also called from
353 * interrupt context.
354 */
355 timeout--;
Vijayavardhan Vennapusaba79f6b2013-07-30 13:39:52 +0530356 if (!timeout) {
357 ret = -ETIMEDOUT;
358 break;
359 }
Felipe Balbi573c2762012-04-24 16:19:11 +0300360 udelay(1);
361 } while (1);
Vijayavardhan Vennapusaba79f6b2013-07-30 13:39:52 +0530362
363 /* Enable suspend of the USB2 PHY */
364 if (hsphy_suspend_enabled)
365 dwc3_gadget_usb2_phy_suspend(dwc, true);
366
367 return ret;
Felipe Balbi573c2762012-04-24 16:19:11 +0300368}
369
Felipe Balbi72246da2011-08-19 18:10:58 +0300370int dwc3_send_gadget_ep_cmd(struct dwc3 *dwc, unsigned ep,
371 unsigned cmd, struct dwc3_gadget_ep_cmd_params *params)
372{
373 struct dwc3_ep *dep = dwc->eps[ep];
Sebastian Andrzej Siewior61d58242011-08-29 16:46:38 +0200374 u32 timeout = 500;
Felipe Balbi72246da2011-08-19 18:10:58 +0300375 u32 reg;
Vijayavardhan Vennapusaba79f6b2013-07-30 13:39:52 +0530376 bool hsphy_suspend_enabled;
377 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300378
379 dev_vdbg(dwc->dev, "%s: cmd '%s' params %08x %08x %08x\n",
380 dep->name,
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300381 dwc3_gadget_ep_cmd_string(cmd), params->param0,
382 params->param1, params->param2);
Felipe Balbi72246da2011-08-19 18:10:58 +0300383
Vijayavardhan Vennapusaba79f6b2013-07-30 13:39:52 +0530384 /* Commands to controller will work only if PHY is not suspended */
385 hsphy_suspend_enabled = (dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0)) &
386 DWC3_GUSB2PHYCFG_SUSPHY);
387
388 /* Disable suspend of the USB2 PHY */
389 if (hsphy_suspend_enabled)
390 dwc3_gadget_usb2_phy_suspend(dwc, false);
391
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300392 dwc3_writel(dwc->regs, DWC3_DEPCMDPAR0(ep), params->param0);
393 dwc3_writel(dwc->regs, DWC3_DEPCMDPAR1(ep), params->param1);
394 dwc3_writel(dwc->regs, DWC3_DEPCMDPAR2(ep), params->param2);
Felipe Balbi72246da2011-08-19 18:10:58 +0300395
396 dwc3_writel(dwc->regs, DWC3_DEPCMD(ep), cmd | DWC3_DEPCMD_CMDACT);
397 do {
398 reg = dwc3_readl(dwc->regs, DWC3_DEPCMD(ep));
399 if (!(reg & DWC3_DEPCMD_CMDACT)) {
Felipe Balbi164f6e12011-08-27 20:29:58 +0300400 dev_vdbg(dwc->dev, "Command Complete --> %d\n",
401 DWC3_DEPCMD_STATUS(reg));
Vijayavardhan Vennapusa91ba6532013-01-30 17:35:45 +0530402 /* SW issues START TRANSFER command to isochronous ep
403 * with future frame interval. If future interval time
404 * has already passed when core recieves command, core
405 * will respond with an error(bit13 in Command complete
406 * event. Hence return error in this case.
407 */
408 if (reg & 0x2000)
Vijayavardhan Vennapusaba79f6b2013-07-30 13:39:52 +0530409 ret = -EAGAIN;
Vijayavardhan Vennapusa91ba6532013-01-30 17:35:45 +0530410 else
Vijayavardhan Vennapusaba79f6b2013-07-30 13:39:52 +0530411 ret = 0;
412 break;
Felipe Balbi72246da2011-08-19 18:10:58 +0300413 }
414
415 /*
Felipe Balbi72246da2011-08-19 18:10:58 +0300416 * We can't sleep here, because it is also called from
417 * interrupt context.
418 */
419 timeout--;
Vijayavardhan Vennapusaba79f6b2013-07-30 13:39:52 +0530420 if (!timeout) {
421 ret = -ETIMEDOUT;
422 break;
423 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300424
Sebastian Andrzej Siewior61d58242011-08-29 16:46:38 +0200425 udelay(1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300426 } while (1);
Vijayavardhan Vennapusaba79f6b2013-07-30 13:39:52 +0530427
428 /* Enable suspend of the USB2 PHY */
429 if (hsphy_suspend_enabled)
430 dwc3_gadget_usb2_phy_suspend(dwc, true);
431
432 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +0300433}
434
Ido Shayevitzfa65a582012-06-06 14:39:54 +0300435dma_addr_t dwc3_trb_dma_offset(struct dwc3_ep *dep,
Felipe Balbif6bafc62012-02-06 11:04:53 +0200436 struct dwc3_trb *trb)
Felipe Balbi72246da2011-08-19 18:10:58 +0300437{
Paul Zimmermanc439ef82011-09-30 10:58:45 +0300438 u32 offset = (char *) trb - (char *) dep->trb_pool;
Felipe Balbi72246da2011-08-19 18:10:58 +0300439
440 return dep->trb_pool_dma + offset;
441}
442
443static int dwc3_alloc_trb_pool(struct dwc3_ep *dep)
444{
445 struct dwc3 *dwc = dep->dwc;
446
447 if (dep->trb_pool)
448 return 0;
449
450 if (dep->number == 0 || dep->number == 1)
451 return 0;
452
453 dep->trb_pool = dma_alloc_coherent(dwc->dev,
454 sizeof(struct dwc3_trb) * DWC3_TRB_NUM,
455 &dep->trb_pool_dma, GFP_KERNEL);
456 if (!dep->trb_pool) {
457 dev_err(dep->dwc->dev, "failed to allocate trb pool for %s\n",
458 dep->name);
459 return -ENOMEM;
460 }
461
462 return 0;
463}
464
465static void dwc3_free_trb_pool(struct dwc3_ep *dep)
466{
467 struct dwc3 *dwc = dep->dwc;
468
469 dma_free_coherent(dwc->dev, sizeof(struct dwc3_trb) * DWC3_TRB_NUM,
470 dep->trb_pool, dep->trb_pool_dma);
471
472 dep->trb_pool = NULL;
473 dep->trb_pool_dma = 0;
474}
475
476static int dwc3_gadget_start_config(struct dwc3 *dwc, struct dwc3_ep *dep)
477{
478 struct dwc3_gadget_ep_cmd_params params;
479 u32 cmd;
480
481 memset(&params, 0x00, sizeof(params));
482
483 if (dep->number != 1) {
484 cmd = DWC3_DEPCMD_DEPSTARTCFG;
485 /* XferRscIdx == 0 for ep0 and 2 for the remaining */
Paul Zimmermanb23c8432011-09-30 10:58:42 +0300486 if (dep->number > 1) {
487 if (dwc->start_config_issued)
488 return 0;
489 dwc->start_config_issued = true;
Felipe Balbi72246da2011-08-19 18:10:58 +0300490 cmd |= DWC3_DEPCMD_PARAM(2);
Paul Zimmermanb23c8432011-09-30 10:58:42 +0300491 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300492
493 return dwc3_send_gadget_ep_cmd(dwc, 0, cmd, &params);
494 }
495
496 return 0;
497}
498
499static int dwc3_gadget_set_ep_config(struct dwc3 *dwc, struct dwc3_ep *dep,
Felipe Balbic90bfae2011-11-29 13:11:21 +0200500 const struct usb_endpoint_descriptor *desc,
Felipe Balbi07e0ee82012-07-16 14:08:16 +0300501 const struct usb_ss_ep_comp_descriptor *comp_desc,
502 bool ignore)
Felipe Balbi72246da2011-08-19 18:10:58 +0300503{
504 struct dwc3_gadget_ep_cmd_params params;
505
506 memset(&params, 0x00, sizeof(params));
507
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300508 params.param0 = DWC3_DEPCFG_EP_TYPE(usb_endpoint_type(desc))
Chanho Parkf0ee6062012-08-31 16:54:07 +0900509 | DWC3_DEPCFG_MAX_PACKET_SIZE(usb_endpoint_maxp(desc));
510
511 /* Burst size is only needed in SuperSpeed mode */
512 if (dwc->gadget.speed == USB_SPEED_SUPER) {
513 u32 burst = dep->endpoint.maxburst - 1;
514
515 params.param0 |= DWC3_DEPCFG_BURST_SIZE(burst);
516 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300517
Felipe Balbi07e0ee82012-07-16 14:08:16 +0300518 if (ignore)
519 params.param0 |= DWC3_DEPCFG_IGN_SEQ_NUM;
Felipe Balbi72246da2011-08-19 18:10:58 +0300520
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300521 params.param1 = DWC3_DEPCFG_XFER_COMPLETE_EN
522 | DWC3_DEPCFG_XFER_NOT_READY_EN;
Felipe Balbi72246da2011-08-19 18:10:58 +0300523
Felipe Balbi18b7ede2012-01-02 13:35:41 +0200524 if (usb_ss_max_streams(comp_desc) && usb_endpoint_xfer_bulk(desc)) {
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300525 params.param1 |= DWC3_DEPCFG_STREAM_CAPABLE
526 | DWC3_DEPCFG_STREAM_EVENT_EN;
Felipe Balbi879631a2011-09-30 10:58:47 +0300527 dep->stream_capable = true;
528 }
529
Felipe Balbi72246da2011-08-19 18:10:58 +0300530 if (usb_endpoint_xfer_isoc(desc))
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300531 params.param1 |= DWC3_DEPCFG_XFER_IN_PROGRESS_EN;
Felipe Balbi72246da2011-08-19 18:10:58 +0300532
533 /*
534 * We are doing 1:1 mapping for endpoints, meaning
535 * Physical Endpoints 2 maps to Logical Endpoint 2 and
536 * so on. We consider the direction bit as part of the physical
537 * endpoint number. So USB endpoint 0x81 is 0x03.
538 */
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300539 params.param1 |= DWC3_DEPCFG_EP_NUMBER(dep->number);
Felipe Balbi72246da2011-08-19 18:10:58 +0300540
541 /*
542 * We must use the lower 16 TX FIFOs even though
543 * HW might have more
544 */
545 if (dep->direction)
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300546 params.param0 |= DWC3_DEPCFG_FIFO_NUMBER(dep->number >> 1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300547
548 if (desc->bInterval) {
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300549 params.param1 |= DWC3_DEPCFG_BINTERVAL_M1(desc->bInterval - 1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300550 dep->interval = 1 << (desc->bInterval - 1);
551 }
552
553 return dwc3_send_gadget_ep_cmd(dwc, dep->number,
554 DWC3_DEPCMD_SETEPCONFIG, &params);
555}
556
557static int dwc3_gadget_set_xfer_resource(struct dwc3 *dwc, struct dwc3_ep *dep)
558{
559 struct dwc3_gadget_ep_cmd_params params;
560
561 memset(&params, 0x00, sizeof(params));
562
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300563 params.param0 = DWC3_DEPXFERCFG_NUM_XFER_RES(1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300564
565 return dwc3_send_gadget_ep_cmd(dwc, dep->number,
566 DWC3_DEPCMD_SETTRANSFRESOURCE, &params);
567}
568
569/**
570 * __dwc3_gadget_ep_enable - Initializes a HW endpoint
571 * @dep: endpoint to be initialized
572 * @desc: USB Endpoint Descriptor
573 *
574 * Caller should take care of locking
575 */
576static int __dwc3_gadget_ep_enable(struct dwc3_ep *dep,
Felipe Balbic90bfae2011-11-29 13:11:21 +0200577 const struct usb_endpoint_descriptor *desc,
Felipe Balbi07e0ee82012-07-16 14:08:16 +0300578 const struct usb_ss_ep_comp_descriptor *comp_desc,
579 bool ignore)
Felipe Balbi72246da2011-08-19 18:10:58 +0300580{
581 struct dwc3 *dwc = dep->dwc;
582 u32 reg;
583 int ret = -ENOMEM;
584
585 if (!(dep->flags & DWC3_EP_ENABLED)) {
586 ret = dwc3_gadget_start_config(dwc, dep);
587 if (ret)
588 return ret;
589 }
590
Felipe Balbi07e0ee82012-07-16 14:08:16 +0300591 ret = dwc3_gadget_set_ep_config(dwc, dep, desc, comp_desc, ignore);
Felipe Balbi72246da2011-08-19 18:10:58 +0300592 if (ret)
593 return ret;
594
595 if (!(dep->flags & DWC3_EP_ENABLED)) {
Felipe Balbif6bafc62012-02-06 11:04:53 +0200596 struct dwc3_trb *trb_st_hw;
597 struct dwc3_trb *trb_link;
Felipe Balbi72246da2011-08-19 18:10:58 +0300598
599 ret = dwc3_gadget_set_xfer_resource(dwc, dep);
600 if (ret)
601 return ret;
602
Ido Shayevitz57cdac12012-03-12 20:25:24 +0200603 dep->endpoint.desc = desc;
Felipe Balbic90bfae2011-11-29 13:11:21 +0200604 dep->comp_desc = comp_desc;
Felipe Balbi72246da2011-08-19 18:10:58 +0300605 dep->type = usb_endpoint_type(desc);
606 dep->flags |= DWC3_EP_ENABLED;
607
608 reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
609 reg |= DWC3_DALEPENA_EP(dep->number);
610 dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
611
612 if (!usb_endpoint_xfer_isoc(desc))
613 return 0;
614
615 memset(&trb_link, 0, sizeof(trb_link));
616
Paul Zimmerman1d046792012-02-15 18:56:56 -0800617 /* Link TRB for ISOC. The HWO bit is never reset */
Felipe Balbi72246da2011-08-19 18:10:58 +0300618 trb_st_hw = &dep->trb_pool[0];
619
Felipe Balbif6bafc62012-02-06 11:04:53 +0200620 trb_link = &dep->trb_pool[DWC3_TRB_NUM - 1];
Felipe Balbi72246da2011-08-19 18:10:58 +0300621
Felipe Balbif6bafc62012-02-06 11:04:53 +0200622 trb_link->bpl = lower_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw));
623 trb_link->bph = upper_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw));
624 trb_link->ctrl |= DWC3_TRBCTL_LINK_TRB;
625 trb_link->ctrl |= DWC3_TRB_CTRL_HWO;
Felipe Balbi72246da2011-08-19 18:10:58 +0300626 }
627
628 return 0;
629}
630
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200631static void dwc3_stop_active_transfer(struct dwc3 *dwc, u32 epnum);
632static void dwc3_remove_requests(struct dwc3 *dwc, struct dwc3_ep *dep)
Felipe Balbi72246da2011-08-19 18:10:58 +0300633{
634 struct dwc3_request *req;
635
Felipe Balbib129eb72012-02-17 12:10:04 +0200636 if (!list_empty(&dep->req_queued)) {
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200637 dwc3_stop_active_transfer(dwc, dep->number);
638
Pratyush Anande67fdeb2012-07-06 15:19:10 +0530639 /* - giveback all requests to gadget driver */
Pratyush Anand110ff602012-06-15 11:54:36 +0530640 while (!list_empty(&dep->req_queued)) {
641 req = next_request(&dep->req_queued);
642
643 dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
644 }
Felipe Balbib129eb72012-02-17 12:10:04 +0200645 }
646
Felipe Balbi72246da2011-08-19 18:10:58 +0300647 while (!list_empty(&dep->request_list)) {
648 req = next_request(&dep->request_list);
649
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200650 dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
Felipe Balbi72246da2011-08-19 18:10:58 +0300651 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300652}
653
654/**
655 * __dwc3_gadget_ep_disable - Disables a HW endpoint
656 * @dep: the endpoint to disable
657 *
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200658 * This function also removes requests which are currently processed ny the
659 * hardware and those which are not yet scheduled.
660 * Caller should take care of locking.
Felipe Balbi72246da2011-08-19 18:10:58 +0300661 */
Felipe Balbi72246da2011-08-19 18:10:58 +0300662static int __dwc3_gadget_ep_disable(struct dwc3_ep *dep)
663{
664 struct dwc3 *dwc = dep->dwc;
665 u32 reg;
666
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200667 dwc3_remove_requests(dwc, dep);
Felipe Balbi72246da2011-08-19 18:10:58 +0300668
669 reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
670 reg &= ~DWC3_DALEPENA_EP(dep->number);
671 dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
672
Felipe Balbi879631a2011-09-30 10:58:47 +0300673 dep->stream_capable = false;
Ido Shayevitzf9c56cd2012-02-08 13:56:48 +0200674 dep->endpoint.desc = NULL;
Felipe Balbic90bfae2011-11-29 13:11:21 +0200675 dep->comp_desc = NULL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300676 dep->type = 0;
Felipe Balbi879631a2011-09-30 10:58:47 +0300677 dep->flags = 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300678
679 return 0;
680}
681
682/* -------------------------------------------------------------------------- */
683
684static int dwc3_gadget_ep0_enable(struct usb_ep *ep,
685 const struct usb_endpoint_descriptor *desc)
686{
687 return -EINVAL;
688}
689
690static int dwc3_gadget_ep0_disable(struct usb_ep *ep)
691{
692 return -EINVAL;
693}
694
695/* -------------------------------------------------------------------------- */
696
697static int dwc3_gadget_ep_enable(struct usb_ep *ep,
698 const struct usb_endpoint_descriptor *desc)
699{
700 struct dwc3_ep *dep;
701 struct dwc3 *dwc;
702 unsigned long flags;
703 int ret;
704
705 if (!ep || !desc || desc->bDescriptorType != USB_DT_ENDPOINT) {
706 pr_debug("dwc3: invalid parameters\n");
707 return -EINVAL;
708 }
709
710 if (!desc->wMaxPacketSize) {
711 pr_debug("dwc3: missing wMaxPacketSize\n");
712 return -EINVAL;
713 }
714
715 dep = to_dwc3_ep(ep);
716 dwc = dep->dwc;
717
Felipe Balbi14395072012-08-15 12:28:29 +0300718 if (dep->flags & DWC3_EP_ENABLED) {
719 dev_WARN_ONCE(dwc->dev, true, "%s is already enabled\n",
720 dep->name);
721 return 0;
722 }
723
Felipe Balbi72246da2011-08-19 18:10:58 +0300724 switch (usb_endpoint_type(desc)) {
725 case USB_ENDPOINT_XFER_CONTROL:
Anton Tikhomirov27a78d62012-02-23 15:38:46 +0900726 strlcat(dep->name, "-control", sizeof(dep->name));
Felipe Balbi72246da2011-08-19 18:10:58 +0300727 break;
728 case USB_ENDPOINT_XFER_ISOC:
Anton Tikhomirov27a78d62012-02-23 15:38:46 +0900729 strlcat(dep->name, "-isoc", sizeof(dep->name));
Felipe Balbi72246da2011-08-19 18:10:58 +0300730 break;
731 case USB_ENDPOINT_XFER_BULK:
Anton Tikhomirov27a78d62012-02-23 15:38:46 +0900732 strlcat(dep->name, "-bulk", sizeof(dep->name));
Felipe Balbi72246da2011-08-19 18:10:58 +0300733 break;
734 case USB_ENDPOINT_XFER_INT:
Anton Tikhomirov27a78d62012-02-23 15:38:46 +0900735 strlcat(dep->name, "-int", sizeof(dep->name));
Felipe Balbi72246da2011-08-19 18:10:58 +0300736 break;
737 default:
738 dev_err(dwc->dev, "invalid endpoint transfer type\n");
739 }
740
Felipe Balbi72246da2011-08-19 18:10:58 +0300741 dev_vdbg(dwc->dev, "Enabling %s\n", dep->name);
742
743 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi07e0ee82012-07-16 14:08:16 +0300744 ret = __dwc3_gadget_ep_enable(dep, desc, ep->comp_desc, false);
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +0530745 dbg_event(dep->number, "ENABLE", ret);
Felipe Balbi72246da2011-08-19 18:10:58 +0300746 spin_unlock_irqrestore(&dwc->lock, flags);
747
748 return ret;
749}
750
751static int dwc3_gadget_ep_disable(struct usb_ep *ep)
752{
753 struct dwc3_ep *dep;
754 struct dwc3 *dwc;
755 unsigned long flags;
756 int ret;
757
758 if (!ep) {
759 pr_debug("dwc3: invalid parameters\n");
760 return -EINVAL;
761 }
762
763 dep = to_dwc3_ep(ep);
764 dwc = dep->dwc;
765
766 if (!(dep->flags & DWC3_EP_ENABLED)) {
767 dev_WARN_ONCE(dwc->dev, true, "%s is already disabled\n",
768 dep->name);
769 return 0;
770 }
771
772 snprintf(dep->name, sizeof(dep->name), "ep%d%s",
773 dep->number >> 1,
774 (dep->number & 1) ? "in" : "out");
775
776 spin_lock_irqsave(&dwc->lock, flags);
777 ret = __dwc3_gadget_ep_disable(dep);
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +0530778 dbg_event(dep->number, "DISABLE", ret);
Felipe Balbi72246da2011-08-19 18:10:58 +0300779 spin_unlock_irqrestore(&dwc->lock, flags);
780
781 return ret;
782}
783
784static struct usb_request *dwc3_gadget_ep_alloc_request(struct usb_ep *ep,
785 gfp_t gfp_flags)
786{
787 struct dwc3_request *req;
788 struct dwc3_ep *dep = to_dwc3_ep(ep);
789 struct dwc3 *dwc = dep->dwc;
790
791 req = kzalloc(sizeof(*req), gfp_flags);
792 if (!req) {
793 dev_err(dwc->dev, "not enough memory\n");
794 return NULL;
795 }
796
797 req->epnum = dep->number;
798 req->dep = dep;
Felipe Balbi72246da2011-08-19 18:10:58 +0300799
800 return &req->request;
801}
802
803static void dwc3_gadget_ep_free_request(struct usb_ep *ep,
804 struct usb_request *request)
805{
806 struct dwc3_request *req = to_dwc3_request(request);
807
808 kfree(req);
809}
810
Felipe Balbic71fc372011-11-22 11:37:34 +0200811/**
812 * dwc3_prepare_one_trb - setup one TRB from one request
813 * @dep: endpoint for which this request is prepared
814 * @req: dwc3_request pointer
815 */
Felipe Balbi68e823e2011-11-28 12:25:01 +0200816static void dwc3_prepare_one_trb(struct dwc3_ep *dep,
Felipe Balbieeb720f2011-11-28 12:46:59 +0200817 struct dwc3_request *req, dma_addr_t dma,
818 unsigned length, unsigned last, unsigned chain)
Felipe Balbic71fc372011-11-22 11:37:34 +0200819{
Felipe Balbieeb720f2011-11-28 12:46:59 +0200820 struct dwc3 *dwc = dep->dwc;
Felipe Balbif6bafc62012-02-06 11:04:53 +0200821 struct dwc3_trb *trb;
Felipe Balbic71fc372011-11-22 11:37:34 +0200822
823 unsigned int cur_slot;
824
Felipe Balbieeb720f2011-11-28 12:46:59 +0200825 dev_vdbg(dwc->dev, "%s: req %p dma %08llx length %d%s%s\n",
826 dep->name, req, (unsigned long long) dma,
827 length, last ? " last" : "",
828 chain ? " chain" : "");
829
Felipe Balbif6bafc62012-02-06 11:04:53 +0200830 trb = &dep->trb_pool[dep->free_slot & DWC3_TRB_MASK];
Felipe Balbic71fc372011-11-22 11:37:34 +0200831 cur_slot = dep->free_slot;
832 dep->free_slot++;
833
834 /* Skip the LINK-TRB on ISOC */
Vijayavardhan Vennapusa2a444ad2013-02-01 13:20:59 +0530835 if (((dep->free_slot & DWC3_TRB_MASK) == DWC3_TRB_NUM - 1) &&
Ido Shayevitz57cdac12012-03-12 20:25:24 +0200836 usb_endpoint_xfer_isoc(dep->endpoint.desc))
Vijayavardhan Vennapusa2a444ad2013-02-01 13:20:59 +0530837 dep->free_slot++;
Felipe Balbic71fc372011-11-22 11:37:34 +0200838
Felipe Balbieeb720f2011-11-28 12:46:59 +0200839 if (!req->trb) {
840 dwc3_gadget_move_request_queued(req);
Felipe Balbif6bafc62012-02-06 11:04:53 +0200841 req->trb = trb;
842 req->trb_dma = dwc3_trb_dma_offset(dep, trb);
Felipe Balbieeb720f2011-11-28 12:46:59 +0200843 }
Felipe Balbic71fc372011-11-22 11:37:34 +0200844
Felipe Balbif6bafc62012-02-06 11:04:53 +0200845 trb->size = DWC3_TRB_SIZE_LENGTH(length);
846 trb->bpl = lower_32_bits(dma);
847 trb->bph = upper_32_bits(dma);
Felipe Balbic71fc372011-11-22 11:37:34 +0200848
Ido Shayevitz57cdac12012-03-12 20:25:24 +0200849 switch (usb_endpoint_type(dep->endpoint.desc)) {
Felipe Balbic71fc372011-11-22 11:37:34 +0200850 case USB_ENDPOINT_XFER_CONTROL:
Felipe Balbif6bafc62012-02-06 11:04:53 +0200851 trb->ctrl = DWC3_TRBCTL_CONTROL_SETUP;
Felipe Balbic71fc372011-11-22 11:37:34 +0200852 break;
853
854 case USB_ENDPOINT_XFER_ISOC:
Felipe Balbif6bafc62012-02-06 11:04:53 +0200855 trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS_FIRST;
Felipe Balbic71fc372011-11-22 11:37:34 +0200856
Pratyush Ananddf023422012-05-21 12:42:54 +0530857 if (!req->request.no_interrupt)
Felipe Balbif6bafc62012-02-06 11:04:53 +0200858 trb->ctrl |= DWC3_TRB_CTRL_IOC;
Felipe Balbic71fc372011-11-22 11:37:34 +0200859 break;
860
861 case USB_ENDPOINT_XFER_BULK:
862 case USB_ENDPOINT_XFER_INT:
Felipe Balbif6bafc62012-02-06 11:04:53 +0200863 trb->ctrl = DWC3_TRBCTL_NORMAL;
Felipe Balbic71fc372011-11-22 11:37:34 +0200864 break;
865 default:
866 /*
867 * This is only possible with faulty memory because we
868 * checked it already :)
869 */
870 BUG();
871 }
872
Ido Shayevitz57cdac12012-03-12 20:25:24 +0200873 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbif6bafc62012-02-06 11:04:53 +0200874 trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI;
875 trb->ctrl |= DWC3_TRB_CTRL_CSP;
876 } else {
877 if (chain)
878 trb->ctrl |= DWC3_TRB_CTRL_CHN;
Felipe Balbic71fc372011-11-22 11:37:34 +0200879
Felipe Balbif6bafc62012-02-06 11:04:53 +0200880 if (last)
881 trb->ctrl |= DWC3_TRB_CTRL_LST;
882 }
883
Ido Shayevitz57cdac12012-03-12 20:25:24 +0200884 if (usb_endpoint_xfer_bulk(dep->endpoint.desc) && dep->stream_capable)
Felipe Balbif6bafc62012-02-06 11:04:53 +0200885 trb->ctrl |= DWC3_TRB_CTRL_SID_SOFN(req->request.stream_id);
886
887 trb->ctrl |= DWC3_TRB_CTRL_HWO;
Felipe Balbic71fc372011-11-22 11:37:34 +0200888}
889
Felipe Balbi72246da2011-08-19 18:10:58 +0300890/*
891 * dwc3_prepare_trbs - setup TRBs from requests
892 * @dep: endpoint for which requests are being prepared
893 * @starting: true if the endpoint is idle and no requests are queued.
894 *
Paul Zimmerman1d046792012-02-15 18:56:56 -0800895 * The function goes through the requests list and sets up TRBs for the
896 * transfers. The function returns once there are no more TRBs available or
897 * it runs out of requests.
Felipe Balbi72246da2011-08-19 18:10:58 +0300898 */
Felipe Balbi68e823e2011-11-28 12:25:01 +0200899static void dwc3_prepare_trbs(struct dwc3_ep *dep, bool starting)
Felipe Balbi72246da2011-08-19 18:10:58 +0300900{
Felipe Balbi68e823e2011-11-28 12:25:01 +0200901 struct dwc3_request *req, *n;
Felipe Balbi72246da2011-08-19 18:10:58 +0300902 u32 trbs_left;
Paul Zimmerman8d62cd62012-02-15 13:35:06 +0200903 u32 max;
Felipe Balbic71fc372011-11-22 11:37:34 +0200904 unsigned int last_one = 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300905
906 BUILD_BUG_ON_NOT_POWER_OF_2(DWC3_TRB_NUM);
907
908 /* the first request must not be queued */
909 trbs_left = (dep->busy_slot - dep->free_slot) & DWC3_TRB_MASK;
Felipe Balbic71fc372011-11-22 11:37:34 +0200910
Paul Zimmerman8d62cd62012-02-15 13:35:06 +0200911 /* Can't wrap around on a non-isoc EP since there's no link TRB */
Ido Shayevitz57cdac12012-03-12 20:25:24 +0200912 if (!usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Paul Zimmerman8d62cd62012-02-15 13:35:06 +0200913 max = DWC3_TRB_NUM - (dep->free_slot & DWC3_TRB_MASK);
914 if (trbs_left > max)
915 trbs_left = max;
916 }
917
Felipe Balbi72246da2011-08-19 18:10:58 +0300918 /*
Paul Zimmerman1d046792012-02-15 18:56:56 -0800919 * If busy & slot are equal than it is either full or empty. If we are
920 * starting to process requests then we are empty. Otherwise we are
Felipe Balbi72246da2011-08-19 18:10:58 +0300921 * full and don't do anything
922 */
923 if (!trbs_left) {
924 if (!starting)
Felipe Balbi68e823e2011-11-28 12:25:01 +0200925 return;
Felipe Balbi72246da2011-08-19 18:10:58 +0300926 trbs_left = DWC3_TRB_NUM;
927 /*
928 * In case we start from scratch, we queue the ISOC requests
929 * starting from slot 1. This is done because we use ring
930 * buffer and have no LST bit to stop us. Instead, we place
Paul Zimmerman1d046792012-02-15 18:56:56 -0800931 * IOC bit every TRB_NUM/4. We try to avoid having an interrupt
Felipe Balbi72246da2011-08-19 18:10:58 +0300932 * after the first request so we start at slot 1 and have
933 * 7 requests proceed before we hit the first IOC.
934 * Other transfer types don't use the ring buffer and are
935 * processed from the first TRB until the last one. Since we
936 * don't wrap around we have to start at the beginning.
937 */
Ido Shayevitz57cdac12012-03-12 20:25:24 +0200938 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbi72246da2011-08-19 18:10:58 +0300939 dep->busy_slot = 1;
940 dep->free_slot = 1;
941 } else {
942 dep->busy_slot = 0;
943 dep->free_slot = 0;
944 }
945 }
946
947 /* The last TRB is a link TRB, not used for xfer */
Ido Shayevitz57cdac12012-03-12 20:25:24 +0200948 if ((trbs_left <= 1) && usb_endpoint_xfer_isoc(dep->endpoint.desc))
Felipe Balbi68e823e2011-11-28 12:25:01 +0200949 return;
Felipe Balbi72246da2011-08-19 18:10:58 +0300950
951 list_for_each_entry_safe(req, n, &dep->request_list, list) {
Felipe Balbieeb720f2011-11-28 12:46:59 +0200952 unsigned length;
953 dma_addr_t dma;
Felipe Balbi72246da2011-08-19 18:10:58 +0300954
Felipe Balbieeb720f2011-11-28 12:46:59 +0200955 if (req->request.num_mapped_sgs > 0) {
956 struct usb_request *request = &req->request;
957 struct scatterlist *sg = request->sg;
958 struct scatterlist *s;
959 int i;
Felipe Balbi72246da2011-08-19 18:10:58 +0300960
Felipe Balbieeb720f2011-11-28 12:46:59 +0200961 for_each_sg(sg, s, request->num_mapped_sgs, i) {
962 unsigned chain = true;
Felipe Balbi72246da2011-08-19 18:10:58 +0300963
Felipe Balbieeb720f2011-11-28 12:46:59 +0200964 length = sg_dma_len(s);
965 dma = sg_dma_address(s);
Felipe Balbi72246da2011-08-19 18:10:58 +0300966
Paul Zimmerman1d046792012-02-15 18:56:56 -0800967 if (i == (request->num_mapped_sgs - 1) ||
968 sg_is_last(s)) {
Felipe Balbieeb720f2011-11-28 12:46:59 +0200969 last_one = true;
970 chain = false;
971 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300972
Felipe Balbieeb720f2011-11-28 12:46:59 +0200973 trbs_left--;
974 if (!trbs_left)
975 last_one = true;
Felipe Balbi72246da2011-08-19 18:10:58 +0300976
Felipe Balbieeb720f2011-11-28 12:46:59 +0200977 if (last_one)
978 chain = false;
Felipe Balbi72246da2011-08-19 18:10:58 +0300979
Felipe Balbieeb720f2011-11-28 12:46:59 +0200980 dwc3_prepare_one_trb(dep, req, dma, length,
981 last_one, chain);
Felipe Balbi72246da2011-08-19 18:10:58 +0300982
Felipe Balbieeb720f2011-11-28 12:46:59 +0200983 if (last_one)
984 break;
985 }
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +0530986 dbg_queue(dep->number, &req->request, 0);
Felipe Balbi72246da2011-08-19 18:10:58 +0300987 } else {
Felipe Balbieeb720f2011-11-28 12:46:59 +0200988 dma = req->request.dma;
989 length = req->request.length;
990 trbs_left--;
991
992 if (!trbs_left)
993 last_one = 1;
994
995 /* Is this the last request? */
996 if (list_is_last(&req->list, &dep->request_list))
997 last_one = 1;
998
999 dwc3_prepare_one_trb(dep, req, dma, length,
1000 last_one, false);
1001
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +05301002 dbg_queue(dep->number, &req->request, 0);
Felipe Balbieeb720f2011-11-28 12:46:59 +02001003 if (last_one)
1004 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03001005 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001006 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001007}
1008
1009static int __dwc3_gadget_kick_transfer(struct dwc3_ep *dep, u16 cmd_param,
1010 int start_new)
1011{
1012 struct dwc3_gadget_ep_cmd_params params;
Vijayavardhan Vennapusa91ba6532013-01-30 17:35:45 +05301013 struct dwc3_request *req, *req1, *n;
Felipe Balbi72246da2011-08-19 18:10:58 +03001014 struct dwc3 *dwc = dep->dwc;
1015 int ret;
1016 u32 cmd;
1017
1018 if (start_new && (dep->flags & DWC3_EP_BUSY)) {
1019 dev_vdbg(dwc->dev, "%s: endpoint busy\n", dep->name);
1020 return -EBUSY;
1021 }
1022 dep->flags &= ~DWC3_EP_PENDING_REQUEST;
1023
1024 /*
1025 * If we are getting here after a short-out-packet we don't enqueue any
1026 * new requests as we try to set the IOC bit only on the last request.
1027 */
1028 if (start_new) {
1029 if (list_empty(&dep->req_queued))
1030 dwc3_prepare_trbs(dep, start_new);
1031
1032 /* req points to the first request which will be sent */
1033 req = next_request(&dep->req_queued);
1034 } else {
Felipe Balbi68e823e2011-11-28 12:25:01 +02001035 dwc3_prepare_trbs(dep, start_new);
1036
Felipe Balbi72246da2011-08-19 18:10:58 +03001037 /*
Paul Zimmerman1d046792012-02-15 18:56:56 -08001038 * req points to the first request where HWO changed from 0 to 1
Felipe Balbi72246da2011-08-19 18:10:58 +03001039 */
Felipe Balbi68e823e2011-11-28 12:25:01 +02001040 req = next_request(&dep->req_queued);
Felipe Balbi72246da2011-08-19 18:10:58 +03001041 }
1042 if (!req) {
1043 dep->flags |= DWC3_EP_PENDING_REQUEST;
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +05301044 dbg_event(dep->number, "NO REQ", 0);
Felipe Balbi72246da2011-08-19 18:10:58 +03001045 return 0;
1046 }
1047
1048 memset(&params, 0, sizeof(params));
Felipe Balbidc1c70a2011-09-30 10:58:51 +03001049 params.param0 = upper_32_bits(req->trb_dma);
1050 params.param1 = lower_32_bits(req->trb_dma);
Felipe Balbi72246da2011-08-19 18:10:58 +03001051
1052 if (start_new)
1053 cmd = DWC3_DEPCMD_STARTTRANSFER;
1054 else
1055 cmd = DWC3_DEPCMD_UPDATETRANSFER;
1056
1057 cmd |= DWC3_DEPCMD_PARAM(cmd_param);
1058 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number, cmd, &params);
1059 if (ret < 0) {
1060 dev_dbg(dwc->dev, "failed to send STARTTRANSFER command\n");
1061
Vijayavardhan Vennapusa91ba6532013-01-30 17:35:45 +05301062 if ((ret == -EAGAIN) && start_new &&
1063 usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
1064 /* If bit13 in Command complete event is set, software
1065 * must issue ENDTRANDFER command and wait for
1066 * Xfernotready event to queue the requests again.
1067 */
1068 if (!dep->resource_index) {
1069 dep->resource_index =
1070 dwc3_gadget_ep_get_transfer_index(dwc,
1071 dep->number);
1072 WARN_ON_ONCE(!dep->resource_index);
1073 }
1074 dwc3_stop_active_transfer(dwc, dep->number);
1075 list_for_each_entry_safe_reverse(req1, n,
1076 &dep->req_queued, list) {
1077 req1->trb = NULL;
1078 dwc3_gadget_move_request_list_front(req1);
1079 if (req->request.num_mapped_sgs)
1080 dep->busy_slot +=
1081 req->request.num_mapped_sgs;
1082 else
1083 dep->busy_slot++;
1084 if ((dep->busy_slot & DWC3_TRB_MASK) ==
1085 DWC3_TRB_NUM - 1)
1086 dep->busy_slot++;
1087 }
1088 return ret;
1089 } else {
1090 /*
1091 * FIXME we need to iterate over the list of requests
1092 * here and stop, unmap, free and del each of the linked
1093 * requests instead of what we do now.
1094 */
1095 usb_gadget_unmap_request(&dwc->gadget, &req->request,
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02001096 req->direction);
Vijayavardhan Vennapusa91ba6532013-01-30 17:35:45 +05301097 list_del(&req->list);
1098 return ret;
1099 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001100 }
1101
1102 dep->flags |= DWC3_EP_BUSY;
Felipe Balbi25b8ff62011-11-04 12:32:47 +02001103
Paul Zimmermanf39a37f2012-03-29 18:16:54 +00001104 if (start_new) {
Felipe Balbi4959cfc2012-06-06 12:04:13 +03001105 dep->resource_index = dwc3_gadget_ep_get_transfer_index(dwc,
Paul Zimmermanf39a37f2012-03-29 18:16:54 +00001106 dep->number);
Felipe Balbi4959cfc2012-06-06 12:04:13 +03001107 WARN_ON_ONCE(!dep->resource_index);
Paul Zimmermanf39a37f2012-03-29 18:16:54 +00001108 }
Felipe Balbi25b8ff62011-11-04 12:32:47 +02001109
Felipe Balbi72246da2011-08-19 18:10:58 +03001110 return 0;
1111}
1112
Pratyush Anand73939b02012-05-25 18:54:56 +05301113static void __dwc3_gadget_start_isoc(struct dwc3 *dwc,
1114 struct dwc3_ep *dep, u32 cur_uf)
1115{
1116 u32 uf;
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +05301117 int ret;
Pratyush Anand73939b02012-05-25 18:54:56 +05301118
Vijayavardhan Vennapusa91ba6532013-01-30 17:35:45 +05301119 dep->current_uf = cur_uf;
1120
Pratyush Anand73939b02012-05-25 18:54:56 +05301121 if (list_empty(&dep->request_list)) {
1122 dev_vdbg(dwc->dev, "ISOC ep %s run out for requests.\n",
1123 dep->name);
Pratyush Anandac417602012-08-30 12:21:43 +05301124 dep->flags |= DWC3_EP_PENDING_REQUEST;
Pratyush Anand73939b02012-05-25 18:54:56 +05301125 return;
1126 }
1127
1128 /* 4 micro frames in the future */
1129 uf = cur_uf + dep->interval * 4;
1130
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +05301131 ret = __dwc3_gadget_kick_transfer(dep, uf, 1);
1132 if (ret < 0)
1133 dbg_event(dep->number, "QUEUE", ret);
Pratyush Anand73939b02012-05-25 18:54:56 +05301134}
1135
1136static void dwc3_gadget_start_isoc(struct dwc3 *dwc,
1137 struct dwc3_ep *dep, const struct dwc3_event_depevt *event)
1138{
1139 u32 cur_uf, mask;
1140
1141 mask = ~(dep->interval - 1);
1142 cur_uf = event->parameters & mask;
1143
1144 __dwc3_gadget_start_isoc(dwc, dep, cur_uf);
1145}
1146
Felipe Balbi72246da2011-08-19 18:10:58 +03001147static int __dwc3_gadget_ep_queue(struct dwc3_ep *dep, struct dwc3_request *req)
1148{
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02001149 struct dwc3 *dwc = dep->dwc;
1150 int ret;
1151
Manu Gautamd2b99e12013-02-11 15:53:34 +05301152 if (req->request.status == -EINPROGRESS) {
1153 ret = -EBUSY;
1154 dev_err(dwc->dev, "%s: %p request already in queue",
1155 dep->name, req);
1156 return ret;
1157 }
1158
Felipe Balbi72246da2011-08-19 18:10:58 +03001159 req->request.actual = 0;
1160 req->request.status = -EINPROGRESS;
1161 req->direction = dep->direction;
1162 req->epnum = dep->number;
1163
1164 /*
1165 * We only add to our list of requests now and
1166 * start consuming the list once we get XferNotReady
1167 * IRQ.
1168 *
1169 * That way, we avoid doing anything that we don't need
1170 * to do now and defer it until the point we receive a
1171 * particular token from the Host side.
1172 *
1173 * This will also avoid Host cancelling URBs due to too
Paul Zimmerman1d046792012-02-15 18:56:56 -08001174 * many NAKs.
Felipe Balbi72246da2011-08-19 18:10:58 +03001175 */
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02001176 ret = usb_gadget_map_request(&dwc->gadget, &req->request,
1177 dep->direction);
1178 if (ret)
1179 return ret;
1180
Felipe Balbi72246da2011-08-19 18:10:58 +03001181 list_add_tail(&req->list, &dep->request_list);
1182
1183 /*
Felipe Balbi46485a02012-06-06 12:00:50 +03001184 * There are a few special cases:
Felipe Balbi72246da2011-08-19 18:10:58 +03001185 *
Paul Zimmermanf39a37f2012-03-29 18:16:54 +00001186 * 1. XferNotReady with empty list of requests. We need to kick the
1187 * transfer here in that situation, otherwise we will be NAKing
1188 * forever. If we get XferNotReady before gadget driver has a
1189 * chance to queue a request, we will ACK the IRQ but won't be
1190 * able to receive the data until the next request is queued.
1191 * The following code is handling exactly that.
1192 *
Felipe Balbi72246da2011-08-19 18:10:58 +03001193 */
1194 if (dep->flags & DWC3_EP_PENDING_REQUEST) {
Pratyush Anandac417602012-08-30 12:21:43 +05301195 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001196
Pratyush Anandac417602012-08-30 12:21:43 +05301197 /*
1198 * If xfernotready is already elapsed and it is a case
1199 * of isoc transfer, then issue END TRANSFER, so that
1200 * you can receive xfernotready again and can have
1201 * notion of current microframe.
1202 */
1203 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Vijayavardhan Vennapusa91ba6532013-01-30 17:35:45 +05301204 /* If xfernotready event is recieved before issuing
1205 * START TRANSFER command, don't issue END TRANSFER.
1206 * Rather start queueing the requests by issuing START
1207 * TRANSFER command.
1208 */
1209 if (list_empty(&dep->req_queued) && dep->resource_index)
Pratyush Anand18bbcb02013-01-14 15:59:34 +05301210 dwc3_stop_active_transfer(dwc, dep->number);
Vijayavardhan Vennapusa91ba6532013-01-30 17:35:45 +05301211 else
1212 __dwc3_gadget_start_isoc(dwc, dep,
1213 dep->current_uf);
1214 dep->flags &= ~DWC3_EP_PENDING_REQUEST;
Pratyush Anandac417602012-08-30 12:21:43 +05301215 return 0;
1216 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001217
Felipe Balbi46485a02012-06-06 12:00:50 +03001218 ret = __dwc3_gadget_kick_transfer(dep, 0, true);
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +05301219 if (ret && ret != -EBUSY) {
1220 dbg_event(dep->number, "QUEUE", ret);
Felipe Balbi72246da2011-08-19 18:10:58 +03001221 dev_dbg(dwc->dev, "%s: failed to kick transfers\n",
1222 dep->name);
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +05301223 }
Felipe Balbi5d409eb2012-05-22 10:24:11 +03001224 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001225
Felipe Balbi46485a02012-06-06 12:00:50 +03001226 /*
1227 * 2. XferInProgress on Isoc EP with an active transfer. We need to
1228 * kick the transfer here after queuing a request, otherwise the
1229 * core may not see the modified TRB(s).
1230 */
1231 if (usb_endpoint_xfer_isoc(dep->endpoint.desc) &&
Pratyush Anand053d3e52012-08-07 16:54:18 +05301232 (dep->flags & DWC3_EP_BUSY) &&
1233 !(dep->flags & DWC3_EP_MISSED_ISOC)) {
Felipe Balbi4959cfc2012-06-06 12:04:13 +03001234 WARN_ON_ONCE(!dep->resource_index);
1235 ret = __dwc3_gadget_kick_transfer(dep, dep->resource_index,
Felipe Balbi46485a02012-06-06 12:00:50 +03001236 false);
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +05301237 if (ret && ret != -EBUSY) {
1238 dbg_event(dep->number, "QUEUE", ret);
Felipe Balbi46485a02012-06-06 12:00:50 +03001239 dev_dbg(dwc->dev, "%s: failed to kick transfers\n",
1240 dep->name);
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +05301241 }
Felipe Balbi46485a02012-06-06 12:00:50 +03001242 }
1243
Felipe Balbi72246da2011-08-19 18:10:58 +03001244 return 0;
1245}
1246
1247static int dwc3_gadget_ep_queue(struct usb_ep *ep, struct usb_request *request,
1248 gfp_t gfp_flags)
1249{
1250 struct dwc3_request *req = to_dwc3_request(request);
1251 struct dwc3_ep *dep = to_dwc3_ep(ep);
1252 struct dwc3 *dwc = dep->dwc;
1253
1254 unsigned long flags;
1255
1256 int ret;
1257
Manu Gautam22f93042013-02-20 15:12:02 +05301258 spin_lock_irqsave(&dwc->lock, flags);
1259
Ido Shayevitz57cdac12012-03-12 20:25:24 +02001260 if (!dep->endpoint.desc) {
Manu Gautam22f93042013-02-20 15:12:02 +05301261 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001262 dev_dbg(dwc->dev, "trying to queue request %p to disabled %s\n",
1263 request, ep->name);
1264 return -ESHUTDOWN;
1265 }
1266
1267 dev_vdbg(dwc->dev, "queing request %p to %s length %d\n",
1268 request, ep->name, request->length);
1269
Manu Gautam1c4dbcb2012-10-05 13:16:00 +05301270 WARN(!dep->direction && (request->length % ep->desc->wMaxPacketSize),
1271 "trying to queue unaligned request (%d)\n", request->length);
1272
Felipe Balbi72246da2011-08-19 18:10:58 +03001273 ret = __dwc3_gadget_ep_queue(dep, req);
1274 spin_unlock_irqrestore(&dwc->lock, flags);
1275
1276 return ret;
1277}
1278
1279static int dwc3_gadget_ep_dequeue(struct usb_ep *ep,
1280 struct usb_request *request)
1281{
1282 struct dwc3_request *req = to_dwc3_request(request);
1283 struct dwc3_request *r = NULL;
1284
1285 struct dwc3_ep *dep = to_dwc3_ep(ep);
1286 struct dwc3 *dwc = dep->dwc;
1287
1288 unsigned long flags;
1289 int ret = 0;
1290
1291 spin_lock_irqsave(&dwc->lock, flags);
1292
1293 list_for_each_entry(r, &dep->request_list, list) {
1294 if (r == req)
1295 break;
1296 }
1297
1298 if (r != req) {
1299 list_for_each_entry(r, &dep->req_queued, list) {
1300 if (r == req)
1301 break;
1302 }
1303 if (r == req) {
1304 /* wait until it is processed */
1305 dwc3_stop_active_transfer(dwc, dep->number);
Pratyush Anandeaec3e92012-06-15 11:54:00 +05301306 goto out1;
Felipe Balbi72246da2011-08-19 18:10:58 +03001307 }
1308 dev_err(dwc->dev, "request %p was not queued to %s\n",
1309 request, ep->name);
1310 ret = -EINVAL;
1311 goto out0;
1312 }
1313
Pratyush Anandeaec3e92012-06-15 11:54:00 +05301314out1:
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +05301315 dbg_event(dep->number, "DEQUEUE", 0);
Felipe Balbi72246da2011-08-19 18:10:58 +03001316 /* giveback the request */
1317 dwc3_gadget_giveback(dep, req, -ECONNRESET);
1318
1319out0:
1320 spin_unlock_irqrestore(&dwc->lock, flags);
1321
1322 return ret;
1323}
1324
1325int __dwc3_gadget_ep_set_halt(struct dwc3_ep *dep, int value)
1326{
1327 struct dwc3_gadget_ep_cmd_params params;
1328 struct dwc3 *dwc = dep->dwc;
1329 int ret;
1330
1331 memset(&params, 0x00, sizeof(params));
1332
1333 if (value) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001334 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number,
1335 DWC3_DEPCMD_SETSTALL, &params);
1336 if (ret)
1337 dev_err(dwc->dev, "failed to %s STALL on %s\n",
1338 value ? "set" : "clear",
1339 dep->name);
1340 else
1341 dep->flags |= DWC3_EP_STALL;
1342 } else {
1343 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number,
1344 DWC3_DEPCMD_CLEARSTALL, &params);
1345 if (ret)
1346 dev_err(dwc->dev, "failed to %s STALL on %s\n",
1347 value ? "set" : "clear",
1348 dep->name);
1349 else
Vijayavardhan Vennapusa6008e262012-10-19 15:57:56 +05301350 dep->flags &= ~(DWC3_EP_STALL | DWC3_EP_WEDGE);
Felipe Balbi72246da2011-08-19 18:10:58 +03001351 }
Paul Zimmerman52754552011-09-30 10:58:44 +03001352
Felipe Balbi72246da2011-08-19 18:10:58 +03001353 return ret;
1354}
1355
1356static int dwc3_gadget_ep_set_halt(struct usb_ep *ep, int value)
1357{
1358 struct dwc3_ep *dep = to_dwc3_ep(ep);
1359 struct dwc3 *dwc = dep->dwc;
1360
1361 unsigned long flags;
1362
1363 int ret;
1364
1365 spin_lock_irqsave(&dwc->lock, flags);
1366
Ido Shayevitz57cdac12012-03-12 20:25:24 +02001367 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001368 dev_err(dwc->dev, "%s is of Isochronous type\n", dep->name);
1369 ret = -EINVAL;
1370 goto out;
1371 }
1372
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +05301373 dbg_event(dep->number, "HALT", value);
Felipe Balbi72246da2011-08-19 18:10:58 +03001374 ret = __dwc3_gadget_ep_set_halt(dep, value);
1375out:
1376 spin_unlock_irqrestore(&dwc->lock, flags);
1377
1378 return ret;
1379}
1380
1381static int dwc3_gadget_ep_set_wedge(struct usb_ep *ep)
1382{
1383 struct dwc3_ep *dep = to_dwc3_ep(ep);
Paul Zimmerman249a4562012-02-24 17:32:16 -08001384 struct dwc3 *dwc = dep->dwc;
1385 unsigned long flags;
Felipe Balbi72246da2011-08-19 18:10:58 +03001386
Paul Zimmerman249a4562012-02-24 17:32:16 -08001387 spin_lock_irqsave(&dwc->lock, flags);
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +05301388 dbg_event(dep->number, "WEDGE", 0);
Felipe Balbi72246da2011-08-19 18:10:58 +03001389 dep->flags |= DWC3_EP_WEDGE;
Paul Zimmerman249a4562012-02-24 17:32:16 -08001390 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001391
Pratyush Anandeb840752012-06-25 22:40:43 +05301392 if (dep->number == 0 || dep->number == 1)
1393 return dwc3_gadget_ep0_set_halt(ep, 1);
1394 else
1395 return dwc3_gadget_ep_set_halt(ep, 1);
Felipe Balbi72246da2011-08-19 18:10:58 +03001396}
1397
1398/* -------------------------------------------------------------------------- */
1399
1400static struct usb_endpoint_descriptor dwc3_gadget_ep0_desc = {
1401 .bLength = USB_DT_ENDPOINT_SIZE,
1402 .bDescriptorType = USB_DT_ENDPOINT,
1403 .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
1404};
1405
1406static const struct usb_ep_ops dwc3_gadget_ep0_ops = {
1407 .enable = dwc3_gadget_ep0_enable,
1408 .disable = dwc3_gadget_ep0_disable,
1409 .alloc_request = dwc3_gadget_ep_alloc_request,
1410 .free_request = dwc3_gadget_ep_free_request,
1411 .queue = dwc3_gadget_ep0_queue,
1412 .dequeue = dwc3_gadget_ep_dequeue,
Pratyush Anandeb840752012-06-25 22:40:43 +05301413 .set_halt = dwc3_gadget_ep0_set_halt,
Felipe Balbi72246da2011-08-19 18:10:58 +03001414 .set_wedge = dwc3_gadget_ep_set_wedge,
1415};
1416
1417static const struct usb_ep_ops dwc3_gadget_ep_ops = {
1418 .enable = dwc3_gadget_ep_enable,
1419 .disable = dwc3_gadget_ep_disable,
1420 .alloc_request = dwc3_gadget_ep_alloc_request,
1421 .free_request = dwc3_gadget_ep_free_request,
1422 .queue = dwc3_gadget_ep_queue,
1423 .dequeue = dwc3_gadget_ep_dequeue,
1424 .set_halt = dwc3_gadget_ep_set_halt,
1425 .set_wedge = dwc3_gadget_ep_set_wedge,
1426};
1427
1428/* -------------------------------------------------------------------------- */
1429
1430static int dwc3_gadget_get_frame(struct usb_gadget *g)
1431{
1432 struct dwc3 *dwc = gadget_to_dwc(g);
1433 u32 reg;
1434
1435 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1436 return DWC3_DSTS_SOFFN(reg);
1437}
1438
1439static int dwc3_gadget_wakeup(struct usb_gadget *g)
1440{
1441 struct dwc3 *dwc = gadget_to_dwc(g);
1442
1443 unsigned long timeout;
1444 unsigned long flags;
1445
1446 u32 reg;
1447
1448 int ret = 0;
1449
1450 u8 link_state;
1451 u8 speed;
1452
1453 spin_lock_irqsave(&dwc->lock, flags);
1454
1455 /*
1456 * According to the Databook Remote wakeup request should
1457 * be issued only when the device is in early suspend state.
1458 *
1459 * We can check that via USB Link State bits in DSTS register.
1460 */
1461 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1462
1463 speed = reg & DWC3_DSTS_CONNECTSPD;
1464 if (speed == DWC3_DSTS_SUPERSPEED) {
1465 dev_dbg(dwc->dev, "no wakeup on SuperSpeed\n");
1466 ret = -EINVAL;
1467 goto out;
1468 }
1469
1470 link_state = DWC3_DSTS_USBLNKST(reg);
1471
1472 switch (link_state) {
1473 case DWC3_LINK_STATE_RX_DET: /* in HS, means Early Suspend */
1474 case DWC3_LINK_STATE_U3: /* in HS, means SUSPEND */
1475 break;
1476 default:
1477 dev_dbg(dwc->dev, "can't wakeup from link state %d\n",
1478 link_state);
1479 ret = -EINVAL;
1480 goto out;
1481 }
1482
Felipe Balbi8598bde2012-01-02 18:55:57 +02001483 ret = dwc3_gadget_set_link_state(dwc, DWC3_LINK_STATE_RECOV);
1484 if (ret < 0) {
1485 dev_err(dwc->dev, "failed to put link in Recovery\n");
1486 goto out;
1487 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001488
Paul Zimmerman88df4272012-04-27 13:10:52 +03001489 /* Recent versions do this automatically */
1490 if (dwc->revision < DWC3_REVISION_194A) {
1491 /* write zeroes to Link Change Request */
Felipe Balbib4d04352012-05-24 10:27:56 +03001492 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
Paul Zimmerman88df4272012-04-27 13:10:52 +03001493 reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK;
1494 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1495 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001496
Paul Zimmerman1d046792012-02-15 18:56:56 -08001497 /* poll until Link State changes to ON */
Felipe Balbi72246da2011-08-19 18:10:58 +03001498 timeout = jiffies + msecs_to_jiffies(100);
1499
Paul Zimmerman1d046792012-02-15 18:56:56 -08001500 while (!time_after(jiffies, timeout)) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001501 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1502
1503 /* in HS, means ON */
1504 if (DWC3_DSTS_USBLNKST(reg) == DWC3_LINK_STATE_U0)
1505 break;
1506 }
1507
1508 if (DWC3_DSTS_USBLNKST(reg) != DWC3_LINK_STATE_U0) {
1509 dev_err(dwc->dev, "failed to send remote wakeup\n");
1510 ret = -EINVAL;
1511 }
1512
1513out:
1514 spin_unlock_irqrestore(&dwc->lock, flags);
1515
1516 return ret;
1517}
1518
1519static int dwc3_gadget_set_selfpowered(struct usb_gadget *g,
1520 int is_selfpowered)
1521{
1522 struct dwc3 *dwc = gadget_to_dwc(g);
Paul Zimmerman249a4562012-02-24 17:32:16 -08001523 unsigned long flags;
Felipe Balbi72246da2011-08-19 18:10:58 +03001524
Paul Zimmerman249a4562012-02-24 17:32:16 -08001525 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001526 dwc->is_selfpowered = !!is_selfpowered;
Paul Zimmerman249a4562012-02-24 17:32:16 -08001527 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001528
1529 return 0;
1530}
1531
Wesley Cheng446ad8d2013-06-05 16:15:01 +05301532#define DWC3_SOFT_RESET_TIMEOUT 10 /* 10 msec */
Pratyush Anand77473f72012-07-02 10:21:55 +05301533static int dwc3_gadget_run_stop(struct dwc3 *dwc, int is_on)
Felipe Balbi72246da2011-08-19 18:10:58 +03001534{
1535 u32 reg;
Sebastian Andrzej Siewior61d58242011-08-29 16:46:38 +02001536 u32 timeout = 500;
Wesley Cheng446ad8d2013-06-05 16:15:01 +05301537 ktime_t start, diff;
Felipe Balbi72246da2011-08-19 18:10:58 +03001538
1539 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
Felipe Balbi8db7ed12012-01-18 18:32:29 +02001540 if (is_on) {
Paul Zimmerman88df4272012-04-27 13:10:52 +03001541 if (dwc->revision <= DWC3_REVISION_187A) {
1542 reg &= ~DWC3_DCTL_TRGTULST_MASK;
1543 reg |= DWC3_DCTL_TRGTULST_RX_DET;
1544 }
1545
1546 if (dwc->revision >= DWC3_REVISION_194A)
1547 reg &= ~DWC3_DCTL_KEEP_CONNECT;
Wesley Cheng446ad8d2013-06-05 16:15:01 +05301548
1549 start = ktime_get();
1550 /* issue device SoftReset */
1551 dwc3_writel(dwc->regs, DWC3_DCTL, reg | DWC3_DCTL_CSFTRST);
1552 do {
1553 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
1554 if (!(reg & DWC3_DCTL_CSFTRST))
1555 break;
1556
1557 diff = ktime_sub(ktime_get(), start);
1558 /* poll for max. 10ms */
1559 if (ktime_to_ms(diff) > DWC3_SOFT_RESET_TIMEOUT) {
1560 printk_ratelimited(KERN_ERR
1561 "%s:core Reset Timed Out\n", __func__);
1562 break;
1563 }
1564 cpu_relax();
1565 } while (true);
1566
1567
1568 dwc3_event_buffers_setup(dwc);
1569 dwc3_gadget_restart(dwc);
1570 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
Paul Zimmerman88df4272012-04-27 13:10:52 +03001571 reg |= DWC3_DCTL_RUN_STOP;
Felipe Balbi8db7ed12012-01-18 18:32:29 +02001572 } else {
Felipe Balbi72246da2011-08-19 18:10:58 +03001573 reg &= ~DWC3_DCTL_RUN_STOP;
Felipe Balbi8db7ed12012-01-18 18:32:29 +02001574 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001575
1576 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1577
1578 do {
1579 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1580 if (is_on) {
1581 if (!(reg & DWC3_DSTS_DEVCTRLHLT))
1582 break;
1583 } else {
1584 if (reg & DWC3_DSTS_DEVCTRLHLT)
1585 break;
1586 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001587 timeout--;
1588 if (!timeout)
Pratyush Anand77473f72012-07-02 10:21:55 +05301589 return -ETIMEDOUT;
Sebastian Andrzej Siewior61d58242011-08-29 16:46:38 +02001590 udelay(1);
Felipe Balbi72246da2011-08-19 18:10:58 +03001591 } while (1);
1592
1593 dev_vdbg(dwc->dev, "gadget %s data soft-%s\n",
1594 dwc->gadget_driver
1595 ? dwc->gadget_driver->function : "no-function",
1596 is_on ? "connect" : "disconnect");
Pratyush Anand77473f72012-07-02 10:21:55 +05301597
1598 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +03001599}
1600
Vijayavardhan Vennapusa908f1ed2012-10-19 19:51:48 +05301601static int dwc3_gadget_vbus_draw(struct usb_gadget *g, unsigned mA)
1602{
1603 struct dwc3 *dwc = gadget_to_dwc(g);
1604 struct dwc3_otg *dotg = dwc->dotg;
1605
1606 if (dotg && dotg->otg.phy)
1607 return usb_phy_set_power(dotg->otg.phy, mA);
1608
1609 return -ENOTSUPP;
1610}
1611
Felipe Balbi72246da2011-08-19 18:10:58 +03001612static int dwc3_gadget_pullup(struct usb_gadget *g, int is_on)
1613{
1614 struct dwc3 *dwc = gadget_to_dwc(g);
1615 unsigned long flags;
Pratyush Anand77473f72012-07-02 10:21:55 +05301616 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001617
1618 is_on = !!is_on;
1619
1620 spin_lock_irqsave(&dwc->lock, flags);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +02001621
1622 dwc->softconnect = is_on;
1623
1624 if ((dwc->dotg && !dwc->vbus_active) ||
1625 !dwc->gadget_driver) {
1626
1627 spin_unlock_irqrestore(&dwc->lock, flags);
1628
1629 /*
1630 * Need to wait for vbus_session(on) from otg driver or to
1631 * the udc_start.
1632 */
1633 return 0;
1634 }
1635
Pratyush Anand77473f72012-07-02 10:21:55 +05301636 ret = dwc3_gadget_run_stop(dwc, is_on);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +02001637
1638 spin_unlock_irqrestore(&dwc->lock, flags);
1639
Pratyush Anand77473f72012-07-02 10:21:55 +05301640 return ret;
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +02001641}
1642
Jack Pham09e5c8e2013-03-06 18:53:43 -08001643static void dwc3_gadget_disconnect_interrupt(struct dwc3 *dwc);
1644
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +02001645static int dwc3_gadget_vbus_session(struct usb_gadget *_gadget, int is_active)
1646{
1647 struct dwc3 *dwc = gadget_to_dwc(_gadget);
1648 unsigned long flags;
Vijayavardhan Vennapusa8ec31d22012-10-23 08:44:48 +05301649 int ret = 0;
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +02001650
1651 if (!dwc->dotg)
1652 return -EPERM;
1653
1654 is_active = !!is_active;
1655
1656 spin_lock_irqsave(&dwc->lock, flags);
1657
1658 /* Mark that the vbus was powered */
1659 dwc->vbus_active = is_active;
1660
1661 /*
1662 * Check if upper level usb_gadget_driver was already registerd with
1663 * this udc controller driver (if dwc3_gadget_start was called)
1664 */
1665 if (dwc->gadget_driver && dwc->softconnect) {
1666 if (dwc->vbus_active) {
1667 /*
1668 * Both vbus was activated by otg and pullup was
1669 * signaled by the gadget driver.
1670 */
Pratyush Anand77473f72012-07-02 10:21:55 +05301671 ret = dwc3_gadget_run_stop(dwc, 1);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +02001672 } else {
Pratyush Anand77473f72012-07-02 10:21:55 +05301673 ret = dwc3_gadget_run_stop(dwc, 0);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +02001674 }
Jack Pham09e5c8e2013-03-06 18:53:43 -08001675 }
1676
1677 /*
1678 * Clearing run/stop bit might occur before disconnect event is seen.
1679 * Make sure to let gadget driver know in that case.
1680 */
1681 if (!dwc->vbus_active && dwc->start_config_issued) {
1682 dev_dbg(dwc->dev, "calling disconnect from %s\n", __func__);
1683 dwc3_gadget_disconnect_interrupt(dwc);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +02001684 }
1685
Felipe Balbi72246da2011-08-19 18:10:58 +03001686 spin_unlock_irqrestore(&dwc->lock, flags);
Pratyush Anand77473f72012-07-02 10:21:55 +05301687 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001688}
1689
Manu Gautamf1fceddf2012-10-12 14:02:50 +05301690/* Required gadget re-initialization before switching to gadget in OTG mode */
1691void dwc3_gadget_restart(struct dwc3 *dwc)
1692{
1693 struct dwc3_ep *dep;
1694 int ret = 0;
Vijayavardhan Vennapusab7434562012-12-12 16:48:49 +05301695 u32 reg;
Manu Gautamf1fceddf2012-10-12 14:02:50 +05301696
Vijayavardhan Vennapusab7434562012-12-12 16:48:49 +05301697 /* Enable all but Start and End of Frame IRQs */
1698 reg = (DWC3_DEVTEN_EVNTOVERFLOWEN |
1699 DWC3_DEVTEN_CMDCMPLTEN |
1700 DWC3_DEVTEN_ERRTICERREN |
1701 DWC3_DEVTEN_WKUPEVTEN |
1702 DWC3_DEVTEN_ULSTCNGEN |
1703 DWC3_DEVTEN_CONNECTDONEEN |
1704 DWC3_DEVTEN_USBRSTEN |
1705 DWC3_DEVTEN_DISCONNEVTEN);
1706 dwc3_writel(dwc->regs, DWC3_DEVTEN, reg);
1707
1708 /* Enable USB2 LPM and automatic phy suspend only on recent versions */
1709 if (dwc->revision >= DWC3_REVISION_194A) {
1710 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
1711 reg |= DWC3_DCFG_LPM_CAP;
1712 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
1713
1714 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
1715 reg &= ~(DWC3_DCTL_HIRD_THRES_MASK | DWC3_DCTL_L1_HIBER_EN);
1716
1717 /* TODO: This should be configurable */
1718 reg |= DWC3_DCTL_HIRD_THRES(28);
1719
1720 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Vijayavardhan Vennapusaba79f6b2013-07-30 13:39:52 +05301721
1722 dwc3_gadget_usb2_phy_suspend(dwc, true);
1723 dwc3_gadget_usb3_phy_suspend(dwc, true);
Vijayavardhan Vennapusab7434562012-12-12 16:48:49 +05301724 }
1725
1726 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
1727 reg &= ~(DWC3_DCFG_SPEED_MASK);
1728
1729 /**
1730 * WORKAROUND: DWC3 revision < 2.20a have an issue
1731 * which would cause metastability state on Run/Stop
1732 * bit if we try to force the IP to USB2-only mode.
1733 *
1734 * Because of that, we cannot configure the IP to any
1735 * speed other than the SuperSpeed
1736 *
1737 * Refers to:
1738 *
1739 * STAR#9000525659: Clock Domain Crossing on DCTL in
1740 * USB 2.0 Mode
1741 */
1742 if (dwc->revision < DWC3_REVISION_220A)
1743 reg |= DWC3_DCFG_SUPERSPEED;
1744 else
1745 reg |= dwc->maximum_speed;
1746 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
1747
1748 dwc->start_config_issued = false;
1749
1750 /* Start with SuperSpeed Default */
1751 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
Manu Gautamf1fceddf2012-10-12 14:02:50 +05301752
1753 dwc->delayed_status = false;
Vijayavardhan Vennapusab7434562012-12-12 16:48:49 +05301754 /* reinitialize physical ep0-1 */
Manu Gautamf1fceddf2012-10-12 14:02:50 +05301755 dep = dwc->eps[0];
1756 dep->flags = 0;
1757 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, false);
1758 if (ret) {
1759 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
1760 return;
1761 }
1762
1763 dep = dwc->eps[1];
1764 dep->flags = 0;
1765 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, false);
1766 if (ret) {
1767 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
1768 return;
1769 }
1770
1771 /* begin to receive SETUP packets */
1772 dwc->ep0state = EP0_SETUP_PHASE;
1773 dwc3_ep0_out_start(dwc);
1774}
1775
Felipe Balbi72246da2011-08-19 18:10:58 +03001776static int dwc3_gadget_start(struct usb_gadget *g,
1777 struct usb_gadget_driver *driver)
1778{
1779 struct dwc3 *dwc = gadget_to_dwc(g);
1780 struct dwc3_ep *dep;
1781 unsigned long flags;
1782 int ret = 0;
1783 u32 reg;
1784
Vijayavardhan Vennapusa0557c9b2013-05-17 13:24:49 +05301785 pm_runtime_get_sync(dwc->dev);
Felipe Balbi72246da2011-08-19 18:10:58 +03001786 spin_lock_irqsave(&dwc->lock, flags);
1787
1788 if (dwc->gadget_driver) {
1789 dev_err(dwc->dev, "%s is already bound to %s\n",
1790 dwc->gadget.name,
1791 dwc->gadget_driver->driver.name);
1792 ret = -EBUSY;
1793 goto err0;
1794 }
1795
1796 dwc->gadget_driver = driver;
1797 dwc->gadget.dev.driver = &driver->driver;
1798
Felipe Balbi72246da2011-08-19 18:10:58 +03001799 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
1800 reg &= ~(DWC3_DCFG_SPEED_MASK);
Felipe Balbi38d2c6c2012-03-23 12:20:31 +02001801
1802 /**
1803 * WORKAROUND: DWC3 revision < 2.20a have an issue
1804 * which would cause metastability state on Run/Stop
1805 * bit if we try to force the IP to USB2-only mode.
1806 *
1807 * Because of that, we cannot configure the IP to any
1808 * speed other than the SuperSpeed
1809 *
1810 * Refers to:
1811 *
1812 * STAR#9000525659: Clock Domain Crossing on DCTL in
1813 * USB 2.0 Mode
1814 */
1815 if (dwc->revision < DWC3_REVISION_220A)
1816 reg |= DWC3_DCFG_SUPERSPEED;
1817 else
1818 reg |= dwc->maximum_speed;
Felipe Balbi72246da2011-08-19 18:10:58 +03001819 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
1820
Paul Zimmermanb23c8432011-09-30 10:58:42 +03001821 dwc->start_config_issued = false;
1822
Felipe Balbi72246da2011-08-19 18:10:58 +03001823 /* Start with SuperSpeed Default */
1824 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
1825
1826 dep = dwc->eps[0];
Wesley Cheng446ad8d2013-06-05 16:15:01 +05301827 dep->endpoint.maxburst = 1;
Felipe Balbi07e0ee82012-07-16 14:08:16 +03001828 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, false);
Felipe Balbi72246da2011-08-19 18:10:58 +03001829 if (ret) {
1830 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
1831 goto err0;
1832 }
1833
1834 dep = dwc->eps[1];
Wesley Cheng446ad8d2013-06-05 16:15:01 +05301835 dep->endpoint.maxburst = 1;
Felipe Balbi07e0ee82012-07-16 14:08:16 +03001836 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, false);
Felipe Balbi72246da2011-08-19 18:10:58 +03001837 if (ret) {
1838 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
1839 goto err1;
1840 }
1841
1842 /* begin to receive SETUP packets */
Felipe Balbic7fcdeb2011-08-27 22:28:36 +03001843 dwc->ep0state = EP0_SETUP_PHASE;
Felipe Balbi72246da2011-08-19 18:10:58 +03001844 dwc3_ep0_out_start(dwc);
1845
1846 spin_unlock_irqrestore(&dwc->lock, flags);
Vijayavardhan Vennapusa0557c9b2013-05-17 13:24:49 +05301847 pm_runtime_put(dwc->dev);
Felipe Balbi72246da2011-08-19 18:10:58 +03001848
1849 return 0;
1850
1851err1:
1852 __dwc3_gadget_ep_disable(dwc->eps[0]);
1853
1854err0:
1855 spin_unlock_irqrestore(&dwc->lock, flags);
Vijayavardhan Vennapusa0557c9b2013-05-17 13:24:49 +05301856 pm_runtime_put(dwc->dev);
Felipe Balbi72246da2011-08-19 18:10:58 +03001857
1858 return ret;
1859}
1860
1861static int dwc3_gadget_stop(struct usb_gadget *g,
1862 struct usb_gadget_driver *driver)
1863{
1864 struct dwc3 *dwc = gadget_to_dwc(g);
1865 unsigned long flags;
1866
1867 spin_lock_irqsave(&dwc->lock, flags);
1868
1869 __dwc3_gadget_ep_disable(dwc->eps[0]);
1870 __dwc3_gadget_ep_disable(dwc->eps[1]);
1871
1872 dwc->gadget_driver = NULL;
1873 dwc->gadget.dev.driver = NULL;
1874
1875 spin_unlock_irqrestore(&dwc->lock, flags);
1876
1877 return 0;
1878}
Paul Zimmerman88df4272012-04-27 13:10:52 +03001879
Felipe Balbi72246da2011-08-19 18:10:58 +03001880static const struct usb_gadget_ops dwc3_gadget_ops = {
1881 .get_frame = dwc3_gadget_get_frame,
1882 .wakeup = dwc3_gadget_wakeup,
1883 .set_selfpowered = dwc3_gadget_set_selfpowered,
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +02001884 .vbus_session = dwc3_gadget_vbus_session,
Vijayavardhan Vennapusa908f1ed2012-10-19 19:51:48 +05301885 .vbus_draw = dwc3_gadget_vbus_draw,
Felipe Balbi72246da2011-08-19 18:10:58 +03001886 .pullup = dwc3_gadget_pullup,
1887 .udc_start = dwc3_gadget_start,
1888 .udc_stop = dwc3_gadget_stop,
1889};
1890
1891/* -------------------------------------------------------------------------- */
1892
1893static int __devinit dwc3_gadget_init_endpoints(struct dwc3 *dwc)
1894{
1895 struct dwc3_ep *dep;
1896 u8 epnum;
1897
1898 INIT_LIST_HEAD(&dwc->gadget.ep_list);
1899
1900 for (epnum = 0; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
1901 dep = kzalloc(sizeof(*dep), GFP_KERNEL);
1902 if (!dep) {
1903 dev_err(dwc->dev, "can't allocate endpoint %d\n",
1904 epnum);
1905 return -ENOMEM;
1906 }
1907
1908 dep->dwc = dwc;
1909 dep->number = epnum;
1910 dwc->eps[epnum] = dep;
1911
1912 snprintf(dep->name, sizeof(dep->name), "ep%d%s", epnum >> 1,
1913 (epnum & 1) ? "in" : "out");
1914 dep->endpoint.name = dep->name;
1915 dep->direction = (epnum & 1);
1916
1917 if (epnum == 0 || epnum == 1) {
1918 dep->endpoint.maxpacket = 512;
1919 dep->endpoint.ops = &dwc3_gadget_ep0_ops;
1920 if (!epnum)
1921 dwc->gadget.ep0 = &dep->endpoint;
1922 } else {
1923 int ret;
1924
1925 dep->endpoint.maxpacket = 1024;
Sebastian Andrzej Siewior12d36c12011-11-03 20:27:50 +01001926 dep->endpoint.max_streams = 15;
Felipe Balbi72246da2011-08-19 18:10:58 +03001927 dep->endpoint.ops = &dwc3_gadget_ep_ops;
1928 list_add_tail(&dep->endpoint.ep_list,
1929 &dwc->gadget.ep_list);
1930
1931 ret = dwc3_alloc_trb_pool(dep);
Felipe Balbi25b8ff62011-11-04 12:32:47 +02001932 if (ret)
Felipe Balbi72246da2011-08-19 18:10:58 +03001933 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001934 }
Felipe Balbi25b8ff62011-11-04 12:32:47 +02001935
Felipe Balbi72246da2011-08-19 18:10:58 +03001936 INIT_LIST_HEAD(&dep->request_list);
1937 INIT_LIST_HEAD(&dep->req_queued);
1938 }
1939
1940 return 0;
1941}
1942
1943static void dwc3_gadget_free_endpoints(struct dwc3 *dwc)
1944{
1945 struct dwc3_ep *dep;
1946 u8 epnum;
1947
1948 for (epnum = 0; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
1949 dep = dwc->eps[epnum];
1950 dwc3_free_trb_pool(dep);
1951
1952 if (epnum != 0 && epnum != 1)
1953 list_del(&dep->endpoint.ep_list);
1954
1955 kfree(dep);
1956 }
1957}
1958
1959static void dwc3_gadget_release(struct device *dev)
1960{
1961 dev_dbg(dev, "%s\n", __func__);
1962}
1963
1964/* -------------------------------------------------------------------------- */
1965static int dwc3_cleanup_done_reqs(struct dwc3 *dwc, struct dwc3_ep *dep,
1966 const struct dwc3_event_depevt *event, int status)
1967{
1968 struct dwc3_request *req;
Felipe Balbif6bafc62012-02-06 11:04:53 +02001969 struct dwc3_trb *trb;
Felipe Balbi72246da2011-08-19 18:10:58 +03001970 unsigned int count;
1971 unsigned int s_pkt = 0;
Pratyush Anand73939b02012-05-25 18:54:56 +05301972 unsigned int trb_status;
Felipe Balbi72246da2011-08-19 18:10:58 +03001973
1974 do {
1975 req = next_request(&dep->req_queued);
Sebastian Andrzej Siewiord39ee7b2011-11-03 10:32:20 +01001976 if (!req) {
1977 WARN_ON_ONCE(1);
1978 return 1;
1979 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001980
Felipe Balbif6bafc62012-02-06 11:04:53 +02001981 trb = req->trb;
Felipe Balbi72246da2011-08-19 18:10:58 +03001982
Felipe Balbif6bafc62012-02-06 11:04:53 +02001983 if ((trb->ctrl & DWC3_TRB_CTRL_HWO) && status != -ESHUTDOWN)
Sebastian Andrzej Siewior0d2f4752011-08-19 19:59:12 +02001984 /*
1985 * We continue despite the error. There is not much we
Paul Zimmerman1d046792012-02-15 18:56:56 -08001986 * can do. If we don't clean it up we loop forever. If
1987 * we skip the TRB then it gets overwritten after a
1988 * while since we use them in a ring buffer. A BUG()
1989 * would help. Lets hope that if this occurs, someone
Sebastian Andrzej Siewior0d2f4752011-08-19 19:59:12 +02001990 * fixes the root cause instead of looking away :)
1991 */
Felipe Balbi72246da2011-08-19 18:10:58 +03001992 dev_err(dwc->dev, "%s's TRB (%p) still owned by HW\n",
1993 dep->name, req->trb);
Felipe Balbif6bafc62012-02-06 11:04:53 +02001994 count = trb->size & DWC3_TRB_SIZE_MASK;
Felipe Balbi72246da2011-08-19 18:10:58 +03001995
1996 if (dep->direction) {
1997 if (count) {
Pratyush Anand73939b02012-05-25 18:54:56 +05301998 trb_status = DWC3_TRB_SIZE_TRBSTS(trb->size);
1999 if (trb_status == DWC3_TRBSTS_MISSED_ISOC) {
2000 dev_dbg(dwc->dev, "incomplete IN transfer %s\n",
2001 dep->name);
Pratyush Anand921b0b82013-01-14 15:59:32 +05302002 /*
2003 * If missed isoc occurred and there is
2004 * no request queued then issue END
2005 * TRANSFER, so that core generates
2006 * next xfernotready and we will issue
2007 * a fresh START TRANSFER.
2008 * If there are still queued request
2009 * then wait, do not issue either END
2010 * or UPDATE TRANSFER, just attach next
2011 * request in request_list during
2012 * giveback.If any future queued request
2013 * is successfully transferred then we
2014 * will issue UPDATE TRANSFER for all
2015 * request in the request_list.
2016 */
Pratyush Anand73939b02012-05-25 18:54:56 +05302017 dep->flags |= DWC3_EP_MISSED_ISOC;
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +05302018 dbg_event(dep->number, "MISSED ISOC",
2019 status);
Pratyush Anand73939b02012-05-25 18:54:56 +05302020 } else {
2021 dev_err(dwc->dev, "incomplete IN transfer %s\n",
2022 dep->name);
2023 status = -ECONNRESET;
2024 }
Pratyush Anand921b0b82013-01-14 15:59:32 +05302025 } else {
2026 dep->flags &= ~DWC3_EP_MISSED_ISOC;
Felipe Balbi72246da2011-08-19 18:10:58 +03002027 }
2028 } else {
2029 if (count && (event->status & DEPEVT_STATUS_SHORT))
2030 s_pkt = 1;
2031 }
2032
2033 /*
2034 * We assume here we will always receive the entire data block
2035 * which we should receive. Meaning, if we program RX to
2036 * receive 4K but we receive only 2K, we assume that's all we
2037 * should receive and we simply bounce the request back to the
2038 * gadget driver for further processing.
2039 */
2040 req->request.actual += req->request.length - count;
2041 dwc3_gadget_giveback(dep, req, status);
2042 if (s_pkt)
2043 break;
Felipe Balbif6bafc62012-02-06 11:04:53 +02002044 if ((event->status & DEPEVT_STATUS_LST) &&
Pratyush Anand413dba62012-06-03 19:43:19 +05302045 (trb->ctrl & (DWC3_TRB_CTRL_LST |
2046 DWC3_TRB_CTRL_HWO)))
Felipe Balbi72246da2011-08-19 18:10:58 +03002047 break;
Felipe Balbif6bafc62012-02-06 11:04:53 +02002048 if ((event->status & DEPEVT_STATUS_IOC) &&
2049 (trb->ctrl & DWC3_TRB_CTRL_IOC))
Felipe Balbi72246da2011-08-19 18:10:58 +03002050 break;
2051 } while (1);
2052
Pratyush Anand18bbcb02013-01-14 15:59:34 +05302053 if (usb_endpoint_xfer_isoc(dep->endpoint.desc) &&
2054 list_empty(&dep->req_queued)) {
Vijayavardhan Vennapusa91ba6532013-01-30 17:35:45 +05302055 if (list_empty(&dep->request_list))
Pratyush Anand18bbcb02013-01-14 15:59:34 +05302056 /*
2057 * If there is no entry in request list then do
2058 * not issue END TRANSFER now. Just set PENDING
2059 * flag, so that END TRANSFER is issued when an
2060 * entry is added into request list.
2061 */
Vijayavardhan Vennapusa91ba6532013-01-30 17:35:45 +05302062 dep->flags |= DWC3_EP_PENDING_REQUEST;
2063 else
Pratyush Anand18bbcb02013-01-14 15:59:34 +05302064 dwc3_stop_active_transfer(dwc, dep->number);
Vijayavardhan Vennapusa91ba6532013-01-30 17:35:45 +05302065 dep->flags &= ~DWC3_EP_MISSED_ISOC;
Pratyush Anand921b0b82013-01-14 15:59:32 +05302066 return 1;
2067 }
2068
Felipe Balbif6bafc62012-02-06 11:04:53 +02002069 if ((event->status & DEPEVT_STATUS_IOC) &&
2070 (trb->ctrl & DWC3_TRB_CTRL_IOC))
Felipe Balbi72246da2011-08-19 18:10:58 +03002071 return 0;
2072 return 1;
2073}
2074
2075static void dwc3_endpoint_transfer_complete(struct dwc3 *dwc,
2076 struct dwc3_ep *dep, const struct dwc3_event_depevt *event,
2077 int start_new)
2078{
2079 unsigned status = 0;
2080 int clean_busy;
2081
2082 if (event->status & DEPEVT_STATUS_BUSERR)
2083 status = -ECONNRESET;
2084
Paul Zimmerman1d046792012-02-15 18:56:56 -08002085 clean_busy = dwc3_cleanup_done_reqs(dwc, dep, event, status);
Paul Zimmermanc2df85c2012-02-24 17:32:18 -08002086 if (clean_busy)
Felipe Balbi72246da2011-08-19 18:10:58 +03002087 dep->flags &= ~DWC3_EP_BUSY;
Felipe Balbifae2b902011-10-14 13:00:30 +03002088
2089 /*
2090 * WORKAROUND: This is the 2nd half of U1/U2 -> U0 workaround.
2091 * See dwc3_gadget_linksts_change_interrupt() for 1st half.
2092 */
2093 if (dwc->revision < DWC3_REVISION_183A) {
2094 u32 reg;
2095 int i;
2096
2097 for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) {
Moiz Sonasatheed03f12012-08-01 14:08:30 -05002098 dep = dwc->eps[i];
Felipe Balbifae2b902011-10-14 13:00:30 +03002099
2100 if (!(dep->flags & DWC3_EP_ENABLED))
2101 continue;
2102
2103 if (!list_empty(&dep->req_queued))
2104 return;
2105 }
2106
2107 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2108 reg |= dwc->u1u2;
2109 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2110
2111 dwc->u1u2 = 0;
2112 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002113}
2114
Felipe Balbi72246da2011-08-19 18:10:58 +03002115static void dwc3_endpoint_interrupt(struct dwc3 *dwc,
2116 const struct dwc3_event_depevt *event)
2117{
2118 struct dwc3_ep *dep;
2119 u8 epnum = event->endpoint_number;
2120
2121 dep = dwc->eps[epnum];
2122
Felipe Balbia09be0a2012-06-06 09:19:35 +03002123 if (!(dep->flags & DWC3_EP_ENABLED))
2124 return;
2125
Felipe Balbi72246da2011-08-19 18:10:58 +03002126 dev_vdbg(dwc->dev, "%s: %s\n", dep->name,
2127 dwc3_ep_event_string(event->endpoint_event));
2128
2129 if (epnum == 0 || epnum == 1) {
2130 dwc3_ep0_interrupt(dwc, event);
2131 return;
2132 }
2133
2134 switch (event->endpoint_event) {
2135 case DWC3_DEPEVT_XFERCOMPLETE:
Felipe Balbi4959cfc2012-06-06 12:04:13 +03002136 dep->resource_index = 0;
Paul Zimmermanc2df85c2012-02-24 17:32:18 -08002137
Ido Shayevitz57cdac12012-03-12 20:25:24 +02002138 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbi72246da2011-08-19 18:10:58 +03002139 dev_dbg(dwc->dev, "%s is an Isochronous endpoint\n",
2140 dep->name);
2141 return;
2142 }
2143
2144 dwc3_endpoint_transfer_complete(dwc, dep, event, 1);
2145 break;
2146 case DWC3_DEPEVT_XFERINPROGRESS:
Ido Shayevitz57cdac12012-03-12 20:25:24 +02002147 if (!usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbi72246da2011-08-19 18:10:58 +03002148 dev_dbg(dwc->dev, "%s is not an Isochronous endpoint\n",
2149 dep->name);
2150 return;
2151 }
2152
2153 dwc3_endpoint_transfer_complete(dwc, dep, event, 0);
2154 break;
2155 case DWC3_DEPEVT_XFERNOTREADY:
Ido Shayevitz57cdac12012-03-12 20:25:24 +02002156 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbi72246da2011-08-19 18:10:58 +03002157 dwc3_gadget_start_isoc(dwc, dep, event);
2158 } else {
2159 int ret;
2160
2161 dev_vdbg(dwc->dev, "%s: reason %s\n",
Felipe Balbi40aa41f2012-01-18 17:06:03 +02002162 dep->name, event->status &
2163 DEPEVT_STATUS_TRANSFER_ACTIVE
Felipe Balbi72246da2011-08-19 18:10:58 +03002164 ? "Transfer Active"
2165 : "Transfer Not Active");
2166
2167 ret = __dwc3_gadget_kick_transfer(dep, 0, 1);
2168 if (!ret || ret == -EBUSY)
2169 return;
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +05302170 else
2171 dbg_event(dep->number, "QUEUE", ret);
Felipe Balbi72246da2011-08-19 18:10:58 +03002172
2173 dev_dbg(dwc->dev, "%s: failed to kick transfers\n",
2174 dep->name);
2175 }
2176
2177 break;
Felipe Balbi879631a2011-09-30 10:58:47 +03002178 case DWC3_DEPEVT_STREAMEVT:
Ido Shayevitz57cdac12012-03-12 20:25:24 +02002179 if (!usb_endpoint_xfer_bulk(dep->endpoint.desc)) {
Felipe Balbi879631a2011-09-30 10:58:47 +03002180 dev_err(dwc->dev, "Stream event for non-Bulk %s\n",
2181 dep->name);
2182 return;
2183 }
2184
2185 switch (event->status) {
2186 case DEPEVT_STREAMEVT_FOUND:
2187 dev_vdbg(dwc->dev, "Stream %d found and started\n",
2188 event->parameters);
2189
2190 break;
2191 case DEPEVT_STREAMEVT_NOTFOUND:
2192 /* FALLTHROUGH */
2193 default:
2194 dev_dbg(dwc->dev, "Couldn't find suitable stream\n");
2195 }
2196 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03002197 case DWC3_DEPEVT_RXTXFIFOEVT:
2198 dev_dbg(dwc->dev, "%s FIFO Overrun\n", dep->name);
2199 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03002200 case DWC3_DEPEVT_EPCMDCMPLT:
Felipe Balbib129eb72012-02-17 12:10:04 +02002201 dev_vdbg(dwc->dev, "Endpoint Command Complete\n");
Felipe Balbi72246da2011-08-19 18:10:58 +03002202 break;
2203 }
2204}
2205
2206static void dwc3_disconnect_gadget(struct dwc3 *dwc)
2207{
2208 if (dwc->gadget_driver && dwc->gadget_driver->disconnect) {
2209 spin_unlock(&dwc->lock);
2210 dwc->gadget_driver->disconnect(&dwc->gadget);
2211 spin_lock(&dwc->lock);
2212 }
2213}
2214
2215static void dwc3_stop_active_transfer(struct dwc3 *dwc, u32 epnum)
2216{
2217 struct dwc3_ep *dep;
2218 struct dwc3_gadget_ep_cmd_params params;
2219 u32 cmd;
2220 int ret;
2221
2222 dep = dwc->eps[epnum];
2223
Felipe Balbi4959cfc2012-06-06 12:04:13 +03002224 if (!dep->resource_index)
Pratyush Anand6263ebe2012-06-23 02:23:08 +05302225 return;
2226
Pratyush Anande67fdeb2012-07-06 15:19:10 +05302227 /*
2228 * NOTICE: We are violating what the Databook says about the
2229 * EndTransfer command. Ideally we would _always_ wait for the
2230 * EndTransfer Command Completion IRQ, but that's causing too
2231 * much trouble synchronizing between us and gadget driver.
2232 *
2233 * We have discussed this with the IP Provider and it was
2234 * suggested to giveback all requests here, but give HW some
2235 * extra time to synchronize with the interconnect. We're using
2236 * an arbitraty 100us delay for that.
2237 *
2238 * Note also that a similar handling was tested by Synopsys
2239 * (thanks a lot Paul) and nothing bad has come out of it.
2240 * In short, what we're doing is:
2241 *
2242 * - Issue EndTransfer WITH CMDIOC bit set
2243 * - Wait 100us
2244 */
2245
Pratyush Anand6263ebe2012-06-23 02:23:08 +05302246 cmd = DWC3_DEPCMD_ENDTRANSFER;
2247 cmd |= DWC3_DEPCMD_HIPRI_FORCERM | DWC3_DEPCMD_CMDIOC;
Felipe Balbi4959cfc2012-06-06 12:04:13 +03002248 cmd |= DWC3_DEPCMD_PARAM(dep->resource_index);
Pratyush Anand6263ebe2012-06-23 02:23:08 +05302249 memset(&params, 0, sizeof(params));
2250 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number, cmd, &params);
2251 WARN_ON_ONCE(ret);
Felipe Balbi4959cfc2012-06-06 12:04:13 +03002252 dep->resource_index = 0;
Felipe Balbi1df89b62012-10-04 11:58:00 +03002253 dep->flags &= ~DWC3_EP_BUSY;
Pratyush Anande67fdeb2012-07-06 15:19:10 +05302254 udelay(100);
Felipe Balbi72246da2011-08-19 18:10:58 +03002255}
2256
2257static void dwc3_stop_active_transfers(struct dwc3 *dwc)
2258{
2259 u32 epnum;
2260
2261 for (epnum = 2; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
2262 struct dwc3_ep *dep;
2263
2264 dep = dwc->eps[epnum];
2265 if (!(dep->flags & DWC3_EP_ENABLED))
2266 continue;
2267
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +02002268 dwc3_remove_requests(dwc, dep);
Felipe Balbi72246da2011-08-19 18:10:58 +03002269 }
2270}
2271
2272static void dwc3_clear_stall_all_ep(struct dwc3 *dwc)
2273{
2274 u32 epnum;
2275
2276 for (epnum = 1; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
2277 struct dwc3_ep *dep;
2278 struct dwc3_gadget_ep_cmd_params params;
2279 int ret;
2280
2281 dep = dwc->eps[epnum];
2282
2283 if (!(dep->flags & DWC3_EP_STALL))
2284 continue;
2285
2286 dep->flags &= ~DWC3_EP_STALL;
2287
2288 memset(&params, 0, sizeof(params));
2289 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number,
2290 DWC3_DEPCMD_CLEARSTALL, &params);
2291 WARN_ON_ONCE(ret);
2292 }
2293}
2294
2295static void dwc3_gadget_disconnect_interrupt(struct dwc3 *dwc)
2296{
Felipe Balbi34d548c2012-05-24 10:30:01 +03002297 int reg;
2298
Felipe Balbi72246da2011-08-19 18:10:58 +03002299 dev_vdbg(dwc->dev, "%s\n", __func__);
Felipe Balbi72246da2011-08-19 18:10:58 +03002300
2301 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2302 reg &= ~DWC3_DCTL_INITU1ENA;
2303 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2304
2305 reg &= ~DWC3_DCTL_INITU2ENA;
2306 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +03002307
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +05302308 dbg_event(0xFF, "DISCONNECT", 0);
Felipe Balbi72246da2011-08-19 18:10:58 +03002309 dwc3_disconnect_gadget(dwc);
Paul Zimmermanb23c8432011-09-30 10:58:42 +03002310 dwc->start_config_issued = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03002311
2312 dwc->gadget.speed = USB_SPEED_UNKNOWN;
Felipe Balbidf62df52011-10-14 15:11:49 +03002313 dwc->setup_packet_pending = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03002314}
2315
Paul Zimmermandffb81b2012-04-27 12:54:05 +03002316static void dwc3_gadget_usb3_phy_suspend(struct dwc3 *dwc, int suspend)
Felipe Balbi72246da2011-08-19 18:10:58 +03002317{
2318 u32 reg;
2319
2320 reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0));
2321
Paul Zimmermandffb81b2012-04-27 12:54:05 +03002322 if (suspend)
Felipe Balbi72246da2011-08-19 18:10:58 +03002323 reg |= DWC3_GUSB3PIPECTL_SUSPHY;
Paul Zimmermandffb81b2012-04-27 12:54:05 +03002324 else
2325 reg &= ~DWC3_GUSB3PIPECTL_SUSPHY;
Felipe Balbi72246da2011-08-19 18:10:58 +03002326
2327 dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg);
2328}
2329
Paul Zimmermandffb81b2012-04-27 12:54:05 +03002330static void dwc3_gadget_usb2_phy_suspend(struct dwc3 *dwc, int suspend)
Felipe Balbi72246da2011-08-19 18:10:58 +03002331{
2332 u32 reg;
2333
2334 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
2335
Paul Zimmermandffb81b2012-04-27 12:54:05 +03002336 if (suspend)
Felipe Balbi72246da2011-08-19 18:10:58 +03002337 reg |= DWC3_GUSB2PHYCFG_SUSPHY;
Paul Zimmermandffb81b2012-04-27 12:54:05 +03002338 else
2339 reg &= ~DWC3_GUSB2PHYCFG_SUSPHY;
Felipe Balbi72246da2011-08-19 18:10:58 +03002340
2341 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
2342}
2343
2344static void dwc3_gadget_reset_interrupt(struct dwc3 *dwc)
2345{
2346 u32 reg;
Vijayavardhan Vennapusa908f1ed2012-10-19 19:51:48 +05302347 struct dwc3_otg *dotg = dwc->dotg;
Felipe Balbi72246da2011-08-19 18:10:58 +03002348
2349 dev_vdbg(dwc->dev, "%s\n", __func__);
2350
Felipe Balbidf62df52011-10-14 15:11:49 +03002351 /*
2352 * WORKAROUND: DWC3 revisions <1.88a have an issue which
2353 * would cause a missing Disconnect Event if there's a
2354 * pending Setup Packet in the FIFO.
2355 *
2356 * There's no suggested workaround on the official Bug
2357 * report, which states that "unless the driver/application
2358 * is doing any special handling of a disconnect event,
2359 * there is no functional issue".
2360 *
2361 * Unfortunately, it turns out that we _do_ some special
2362 * handling of a disconnect event, namely complete all
2363 * pending transfers, notify gadget driver of the
2364 * disconnection, and so on.
2365 *
2366 * Our suggested workaround is to follow the Disconnect
2367 * Event steps here, instead, based on a setup_packet_pending
2368 * flag. Such flag gets set whenever we have a XferNotReady
2369 * event on EP0 and gets cleared on XferComplete for the
2370 * same endpoint.
2371 *
2372 * Refers to:
2373 *
2374 * STAR#9000466709: RTL: Device : Disconnect event not
2375 * generated if setup packet pending in FIFO
2376 */
2377 if (dwc->revision < DWC3_REVISION_188A) {
2378 if (dwc->setup_packet_pending)
2379 dwc3_gadget_disconnect_interrupt(dwc);
2380 }
2381
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +05302382 dbg_event(0xFF, "BUS RST", 0);
Felipe Balbi961906e2011-12-20 15:37:21 +02002383 /* after reset -> Default State */
2384 dwc->dev_state = DWC3_DEFAULT_STATE;
2385
Paul Zimmerman88df4272012-04-27 13:10:52 +03002386 /* Recent versions support automatic phy suspend and don't need this */
2387 if (dwc->revision < DWC3_REVISION_194A) {
2388 /* Resume PHYs */
2389 dwc3_gadget_usb2_phy_suspend(dwc, false);
2390 dwc3_gadget_usb3_phy_suspend(dwc, false);
2391 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002392
Vijayavardhan Vennapusa908f1ed2012-10-19 19:51:48 +05302393 if (dotg && dotg->otg.phy)
2394 usb_phy_set_power(dotg->otg.phy, 0);
2395
Felipe Balbi72246da2011-08-19 18:10:58 +03002396 if (dwc->gadget.speed != USB_SPEED_UNKNOWN)
2397 dwc3_disconnect_gadget(dwc);
2398
2399 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2400 reg &= ~DWC3_DCTL_TSTCTRL_MASK;
2401 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Gerard Cauvy3b637362012-02-10 12:21:18 +02002402 dwc->test_mode = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03002403
2404 dwc3_stop_active_transfers(dwc);
2405 dwc3_clear_stall_all_ep(dwc);
Paul Zimmermanb23c8432011-09-30 10:58:42 +03002406 dwc->start_config_issued = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03002407
2408 /* Reset device address to zero */
2409 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2410 reg &= ~(DWC3_DCFG_DEVADDR_MASK);
2411 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +03002412}
2413
2414static void dwc3_update_ram_clk_sel(struct dwc3 *dwc, u32 speed)
2415{
2416 u32 reg;
2417 u32 usb30_clock = DWC3_GCTL_CLK_BUS;
2418
2419 /*
2420 * We change the clock only at SS but I dunno why I would want to do
2421 * this. Maybe it becomes part of the power saving plan.
2422 */
2423
2424 if (speed != DWC3_DSTS_SUPERSPEED)
2425 return;
2426
2427 /*
2428 * RAMClkSel is reset to 0 after USB reset, so it must be reprogrammed
2429 * each time on Connect Done.
2430 */
2431 if (!usb30_clock)
2432 return;
2433
2434 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
2435 reg |= DWC3_GCTL_RAMCLKSEL(usb30_clock);
2436 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
2437}
2438
Paul Zimmermandffb81b2012-04-27 12:54:05 +03002439static void dwc3_gadget_phy_suspend(struct dwc3 *dwc, u8 speed)
Felipe Balbi72246da2011-08-19 18:10:58 +03002440{
2441 switch (speed) {
2442 case USB_SPEED_SUPER:
Paul Zimmermandffb81b2012-04-27 12:54:05 +03002443 dwc3_gadget_usb2_phy_suspend(dwc, true);
Felipe Balbi72246da2011-08-19 18:10:58 +03002444 break;
2445 case USB_SPEED_HIGH:
2446 case USB_SPEED_FULL:
2447 case USB_SPEED_LOW:
Paul Zimmermandffb81b2012-04-27 12:54:05 +03002448 dwc3_gadget_usb3_phy_suspend(dwc, true);
Felipe Balbi72246da2011-08-19 18:10:58 +03002449 break;
2450 }
2451}
2452
2453static void dwc3_gadget_conndone_interrupt(struct dwc3 *dwc)
2454{
2455 struct dwc3_gadget_ep_cmd_params params;
2456 struct dwc3_ep *dep;
2457 int ret;
2458 u32 reg;
2459 u8 speed;
2460
2461 dev_vdbg(dwc->dev, "%s\n", __func__);
2462
2463 memset(&params, 0x00, sizeof(params));
2464
Felipe Balbi72246da2011-08-19 18:10:58 +03002465 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
2466 speed = reg & DWC3_DSTS_CONNECTSPD;
2467 dwc->speed = speed;
2468
2469 dwc3_update_ram_clk_sel(dwc, speed);
2470
2471 switch (speed) {
2472 case DWC3_DCFG_SUPERSPEED:
Felipe Balbi05870c52011-10-14 14:51:38 +03002473 /*
2474 * WORKAROUND: DWC3 revisions <1.90a have an issue which
2475 * would cause a missing USB3 Reset event.
2476 *
2477 * In such situations, we should force a USB3 Reset
2478 * event by calling our dwc3_gadget_reset_interrupt()
2479 * routine.
2480 *
2481 * Refers to:
2482 *
2483 * STAR#9000483510: RTL: SS : USB3 reset event may
2484 * not be generated always when the link enters poll
2485 */
2486 if (dwc->revision < DWC3_REVISION_190A)
2487 dwc3_gadget_reset_interrupt(dwc);
2488
Felipe Balbi72246da2011-08-19 18:10:58 +03002489 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
2490 dwc->gadget.ep0->maxpacket = 512;
2491 dwc->gadget.speed = USB_SPEED_SUPER;
2492 break;
2493 case DWC3_DCFG_HIGHSPEED:
2494 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
2495 dwc->gadget.ep0->maxpacket = 64;
2496 dwc->gadget.speed = USB_SPEED_HIGH;
2497 break;
2498 case DWC3_DCFG_FULLSPEED2:
2499 case DWC3_DCFG_FULLSPEED1:
2500 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
2501 dwc->gadget.ep0->maxpacket = 64;
2502 dwc->gadget.speed = USB_SPEED_FULL;
2503 break;
2504 case DWC3_DCFG_LOWSPEED:
2505 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(8);
2506 dwc->gadget.ep0->maxpacket = 8;
2507 dwc->gadget.speed = USB_SPEED_LOW;
2508 break;
2509 }
2510
Paul Zimmerman88df4272012-04-27 13:10:52 +03002511 /* Recent versions support automatic phy suspend and don't need this */
2512 if (dwc->revision < DWC3_REVISION_194A) {
2513 /* Suspend unneeded PHY */
2514 dwc3_gadget_phy_suspend(dwc, dwc->gadget.speed);
2515 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002516
2517 dep = dwc->eps[0];
Felipe Balbi07e0ee82012-07-16 14:08:16 +03002518 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, true);
Felipe Balbi72246da2011-08-19 18:10:58 +03002519 if (ret) {
2520 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
2521 return;
2522 }
2523
2524 dep = dwc->eps[1];
Felipe Balbi07e0ee82012-07-16 14:08:16 +03002525 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, true);
Felipe Balbi72246da2011-08-19 18:10:58 +03002526 if (ret) {
2527 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
2528 return;
2529 }
2530
2531 /*
2532 * Configure PHY via GUSB3PIPECTLn if required.
2533 *
2534 * Update GTXFIFOSIZn
2535 *
2536 * In both cases reset values should be sufficient.
2537 */
2538}
2539
2540static void dwc3_gadget_wakeup_interrupt(struct dwc3 *dwc)
2541{
2542 dev_vdbg(dwc->dev, "%s\n", __func__);
2543
2544 /*
2545 * TODO take core out of low power mode when that's
2546 * implemented.
2547 */
2548
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +05302549 dbg_event(0xFF, "WAKEUP", 0);
Felipe Balbi72246da2011-08-19 18:10:58 +03002550 dwc->gadget_driver->resume(&dwc->gadget);
2551}
2552
2553static void dwc3_gadget_linksts_change_interrupt(struct dwc3 *dwc,
2554 unsigned int evtinfo)
2555{
Felipe Balbifae2b902011-10-14 13:00:30 +03002556 enum dwc3_link_state next = evtinfo & DWC3_LINK_STATE_MASK;
2557
2558 /*
2559 * WORKAROUND: DWC3 Revisions <1.83a have an issue which, depending
2560 * on the link partner, the USB session might do multiple entry/exit
2561 * of low power states before a transfer takes place.
2562 *
2563 * Due to this problem, we might experience lower throughput. The
2564 * suggested workaround is to disable DCTL[12:9] bits if we're
2565 * transitioning from U1/U2 to U0 and enable those bits again
2566 * after a transfer completes and there are no pending transfers
2567 * on any of the enabled endpoints.
2568 *
2569 * This is the first half of that workaround.
2570 *
2571 * Refers to:
2572 *
2573 * STAR#9000446952: RTL: Device SS : if U1/U2 ->U0 takes >128us
2574 * core send LGO_Ux entering U0
2575 */
2576 if (dwc->revision < DWC3_REVISION_183A) {
2577 if (next == DWC3_LINK_STATE_U0) {
2578 u32 u1u2;
2579 u32 reg;
2580
2581 switch (dwc->link_state) {
2582 case DWC3_LINK_STATE_U1:
2583 case DWC3_LINK_STATE_U2:
2584 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2585 u1u2 = reg & (DWC3_DCTL_INITU2ENA
2586 | DWC3_DCTL_ACCEPTU2ENA
2587 | DWC3_DCTL_INITU1ENA
2588 | DWC3_DCTL_ACCEPTU1ENA);
2589
2590 if (!dwc->u1u2)
2591 dwc->u1u2 = reg & u1u2;
2592
2593 reg &= ~u1u2;
2594
2595 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2596 break;
2597 default:
2598 /* do nothing */
2599 break;
2600 }
2601 }
2602 }
2603
Vijayavardhan Vennapusa54be1d62012-10-06 18:32:06 +05302604 if (next == DWC3_LINK_STATE_U0) {
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +05302605 if (dwc->link_state == DWC3_LINK_STATE_U3) {
2606 dbg_event(0xFF, "RESUME", 0);
Vijayavardhan Vennapusa54be1d62012-10-06 18:32:06 +05302607 dwc->gadget_driver->resume(&dwc->gadget);
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +05302608 }
Vijayavardhan Vennapusa54be1d62012-10-06 18:32:06 +05302609 } else if (next == DWC3_LINK_STATE_U3) {
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +05302610 dbg_event(0xFF, "SUSPEND", 0);
Vijayavardhan Vennapusa54be1d62012-10-06 18:32:06 +05302611 dwc->gadget_driver->suspend(&dwc->gadget);
2612 }
2613
Felipe Balbifae2b902011-10-14 13:00:30 +03002614 dwc->link_state = next;
Felipe Balbi019ac832011-09-08 21:18:47 +03002615
2616 dev_vdbg(dwc->dev, "%s link %d\n", __func__, dwc->link_state);
Felipe Balbi72246da2011-08-19 18:10:58 +03002617}
2618
Vijayavardhan Vennapusa8a011c92013-07-29 09:06:48 +05302619static void dwc3_dump_reg_info(struct dwc3 *dwc)
2620{
2621 dbg_event(0xFF, "REGDUMP", 0);
2622
2623 dbg_print_reg("GUSB3PIPCTL", dwc3_readl(dwc->regs,
2624 DWC3_GUSB3PIPECTL(0)));
2625 dbg_print_reg("GUSB2PHYCONFIG", dwc3_readl(dwc->regs,
2626 DWC3_GUSB2PHYCFG(0)));
2627 dbg_print_reg("GCTL", dwc3_readl(dwc->regs, DWC3_GCTL));
2628 dbg_print_reg("GUCTL", dwc3_readl(dwc->regs, DWC3_GUCTL));
2629 dbg_print_reg("GDBGLTSSM", dwc3_readl(dwc->regs, DWC3_GDBGLTSSM));
2630 dbg_print_reg("DCFG", dwc3_readl(dwc->regs, DWC3_DCFG));
2631 dbg_print_reg("DCTL", dwc3_readl(dwc->regs, DWC3_DCTL));
2632 dbg_print_reg("DEVTEN", dwc3_readl(dwc->regs, DWC3_DEVTEN));
2633 dbg_print_reg("DSTS", dwc3_readl(dwc->regs, DWC3_DSTS));
2634 dbg_print_reg("DALPENA", dwc3_readl(dwc->regs, DWC3_DALEPENA));
2635 dbg_print_reg("DGCMD", dwc3_readl(dwc->regs, DWC3_DGCMD));
2636
2637 dbg_print_reg("OCFG", dwc3_readl(dwc->regs, DWC3_OCFG));
2638 dbg_print_reg("OCTL", dwc3_readl(dwc->regs, DWC3_OCTL));
2639 dbg_print_reg("OEVT", dwc3_readl(dwc->regs, DWC3_OEVT));
2640 dbg_print_reg("OSTS", dwc3_readl(dwc->regs, DWC3_OSTS));
2641
2642 dwc3_notify_event(dwc, DWC3_CONTROLLER_ERROR_EVENT);
2643
2644 panic("DWC3 Erratic error\n");
2645
2646}
2647
Felipe Balbi72246da2011-08-19 18:10:58 +03002648static void dwc3_gadget_interrupt(struct dwc3 *dwc,
2649 const struct dwc3_event_devt *event)
2650{
2651 switch (event->type) {
2652 case DWC3_DEVICE_EVENT_DISCONNECT:
2653 dwc3_gadget_disconnect_interrupt(dwc);
2654 break;
2655 case DWC3_DEVICE_EVENT_RESET:
2656 dwc3_gadget_reset_interrupt(dwc);
2657 break;
2658 case DWC3_DEVICE_EVENT_CONNECT_DONE:
2659 dwc3_gadget_conndone_interrupt(dwc);
2660 break;
2661 case DWC3_DEVICE_EVENT_WAKEUP:
2662 dwc3_gadget_wakeup_interrupt(dwc);
2663 break;
2664 case DWC3_DEVICE_EVENT_LINK_STATUS_CHANGE:
2665 dwc3_gadget_linksts_change_interrupt(dwc, event->event_info);
2666 break;
2667 case DWC3_DEVICE_EVENT_EOPF:
2668 dev_vdbg(dwc->dev, "End of Periodic Frame\n");
2669 break;
2670 case DWC3_DEVICE_EVENT_SOF:
2671 dev_vdbg(dwc->dev, "Start of Periodic Frame\n");
2672 break;
2673 case DWC3_DEVICE_EVENT_ERRATIC_ERROR:
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +05302674 dbg_event(0xFF, "ERROR", 0);
Felipe Balbi72246da2011-08-19 18:10:58 +03002675 dev_vdbg(dwc->dev, "Erratic Error\n");
Vijayavardhan Vennapusa8a011c92013-07-29 09:06:48 +05302676 dwc3_dump_reg_info(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +03002677 break;
2678 case DWC3_DEVICE_EVENT_CMD_CMPL:
2679 dev_vdbg(dwc->dev, "Command Complete\n");
2680 break;
2681 case DWC3_DEVICE_EVENT_OVERFLOW:
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +05302682 dbg_event(0xFF, "OVERFL", 0);
Felipe Balbi72246da2011-08-19 18:10:58 +03002683 dev_vdbg(dwc->dev, "Overflow\n");
Pavankumar Kondetid393e172012-06-12 16:07:29 +05302684 /*
2685 * Controllers prior to 2.30a revision has a bug where
2686 * Overflow Event may overwrite an unacknowledged event
2687 * in the event buffer. The severity of the issue depends
2688 * on the overwritten event type. Add a warning message
2689 * saying that an event is overwritten.
2690 *
2691 * TODO: In future we may need to see if we can re-enumerate
2692 * with host.
2693 */
2694 if (dwc->revision < DWC3_REVISION_230A)
2695 dev_warn(dwc->dev, "Unacknowledged event overwritten\n");
Felipe Balbi72246da2011-08-19 18:10:58 +03002696 break;
Pavankumar Kondeti33fe6f12012-06-12 16:21:46 +05302697 case DWC3_DEVICE_EVENT_VENDOR_DEV_TEST_LMP:
2698 /*
2699 * Controllers prior to 2.30a revision has a bug, due to which
2700 * a vendor device test LMP event can not be filtered. But
2701 * this event is not handled in the current code. This is a
2702 * special event and 8 bytes of data will follow the event.
2703 * Handling this event is tricky when event buffer is almost
2704 * full. Moreover this event will not occur in normal scenario
2705 * and can only happen with special hosts in testing scenarios.
2706 * Add a warning message to indicate that this event is received
2707 * which means that event buffer might have corrupted.
2708 */
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +05302709 dbg_event(0xFF, "TSTLMP", 0);
Pavankumar Kondeti33fe6f12012-06-12 16:21:46 +05302710 if (dwc->revision < DWC3_REVISION_230A)
2711 dev_warn(dwc->dev, "Vendor Device Test LMP Received\n");
2712 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03002713 default:
2714 dev_dbg(dwc->dev, "UNKNOWN IRQ %d\n", event->type);
2715 }
2716}
2717
2718static void dwc3_process_event_entry(struct dwc3 *dwc,
2719 const union dwc3_event *event)
2720{
2721 /* Endpoint IRQ, handle it and return early */
2722 if (event->type.is_devspec == 0) {
2723 /* depevt */
2724 return dwc3_endpoint_interrupt(dwc, &event->depevt);
2725 }
2726
2727 switch (event->type.type) {
2728 case DWC3_EVENT_TYPE_DEV:
2729 dwc3_gadget_interrupt(dwc, &event->devt);
2730 break;
2731 /* REVISIT what to do with Carkit and I2C events ? */
2732 default:
2733 dev_err(dwc->dev, "UNKNOWN IRQ type %d\n", event->raw);
2734 }
2735}
2736
2737static irqreturn_t dwc3_process_event_buf(struct dwc3 *dwc, u32 buf)
2738{
2739 struct dwc3_event_buffer *evt;
2740 int left;
2741 u32 count;
2742
2743 count = dwc3_readl(dwc->regs, DWC3_GEVNTCOUNT(buf));
2744 count &= DWC3_GEVNTCOUNT_MASK;
2745 if (!count)
2746 return IRQ_NONE;
2747
2748 evt = dwc->ev_buffs[buf];
2749 left = count;
2750
2751 while (left > 0) {
2752 union dwc3_event event;
2753
Felipe Balbid70d8442012-02-06 13:40:17 +02002754 event.raw = *(u32 *) (evt->buf + evt->lpos);
2755
Felipe Balbi72246da2011-08-19 18:10:58 +03002756 dwc3_process_event_entry(dwc, &event);
2757 /*
2758 * XXX we wrap around correctly to the next entry as almost all
2759 * entries are 4 bytes in size. There is one entry which has 12
2760 * bytes which is a regular entry followed by 8 bytes data. ATM
2761 * I don't know how things are organized if were get next to the
2762 * a boundary so I worry about that once we try to handle that.
2763 */
2764 evt->lpos = (evt->lpos + 4) % DWC3_EVENT_BUFFERS_SIZE;
2765 left -= 4;
2766
2767 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(buf), 4);
2768 }
2769
2770 return IRQ_HANDLED;
2771}
2772
2773static irqreturn_t dwc3_interrupt(int irq, void *_dwc)
2774{
2775 struct dwc3 *dwc = _dwc;
2776 int i;
2777 irqreturn_t ret = IRQ_NONE;
2778
2779 spin_lock(&dwc->lock);
2780
Felipe Balbi9f622b22011-10-12 10:31:04 +03002781 for (i = 0; i < dwc->num_event_buffers; i++) {
Felipe Balbi72246da2011-08-19 18:10:58 +03002782 irqreturn_t status;
2783
2784 status = dwc3_process_event_buf(dwc, i);
2785 if (status == IRQ_HANDLED)
2786 ret = status;
2787 }
2788
2789 spin_unlock(&dwc->lock);
2790
2791 return ret;
2792}
2793
2794/**
2795 * dwc3_gadget_init - Initializes gadget related registers
Paul Zimmerman1d046792012-02-15 18:56:56 -08002796 * @dwc: pointer to our controller context structure
Felipe Balbi72246da2011-08-19 18:10:58 +03002797 *
2798 * Returns 0 on success otherwise negative errno.
2799 */
2800int __devinit dwc3_gadget_init(struct dwc3 *dwc)
2801{
2802 u32 reg;
2803 int ret;
2804 int irq;
2805
2806 dwc->ctrl_req = dma_alloc_coherent(dwc->dev, sizeof(*dwc->ctrl_req),
2807 &dwc->ctrl_req_addr, GFP_KERNEL);
2808 if (!dwc->ctrl_req) {
2809 dev_err(dwc->dev, "failed to allocate ctrl request\n");
2810 ret = -ENOMEM;
2811 goto err0;
2812 }
2813
2814 dwc->ep0_trb = dma_alloc_coherent(dwc->dev, sizeof(*dwc->ep0_trb),
2815 &dwc->ep0_trb_addr, GFP_KERNEL);
2816 if (!dwc->ep0_trb) {
2817 dev_err(dwc->dev, "failed to allocate ep0 trb\n");
2818 ret = -ENOMEM;
2819 goto err1;
2820 }
2821
Felipe Balbib0791fb2012-05-04 12:58:14 +03002822 dwc->setup_buf = kzalloc(DWC3_EP0_BOUNCE_SIZE, GFP_KERNEL);
Felipe Balbi72246da2011-08-19 18:10:58 +03002823 if (!dwc->setup_buf) {
2824 dev_err(dwc->dev, "failed to allocate setup buffer\n");
2825 ret = -ENOMEM;
2826 goto err2;
2827 }
2828
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002829 dwc->ep0_bounce = dma_alloc_coherent(dwc->dev,
Felipe Balbib0791fb2012-05-04 12:58:14 +03002830 DWC3_EP0_BOUNCE_SIZE, &dwc->ep0_bounce_addr,
2831 GFP_KERNEL);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002832 if (!dwc->ep0_bounce) {
2833 dev_err(dwc->dev, "failed to allocate ep0 bounce buffer\n");
2834 ret = -ENOMEM;
2835 goto err3;
2836 }
2837
Felipe Balbi72246da2011-08-19 18:10:58 +03002838 dev_set_name(&dwc->gadget.dev, "gadget");
2839
2840 dwc->gadget.ops = &dwc3_gadget_ops;
Manu Gautama7b082a2012-11-06 09:50:09 +05302841 dwc->gadget.max_speed = USB_SPEED_SUPER;
Felipe Balbi72246da2011-08-19 18:10:58 +03002842 dwc->gadget.speed = USB_SPEED_UNKNOWN;
2843 dwc->gadget.dev.parent = dwc->dev;
Felipe Balbieeb720f2011-11-28 12:46:59 +02002844 dwc->gadget.sg_supported = true;
Felipe Balbi72246da2011-08-19 18:10:58 +03002845
2846 dma_set_coherent_mask(&dwc->gadget.dev, dwc->dev->coherent_dma_mask);
2847
2848 dwc->gadget.dev.dma_parms = dwc->dev->dma_parms;
2849 dwc->gadget.dev.dma_mask = dwc->dev->dma_mask;
2850 dwc->gadget.dev.release = dwc3_gadget_release;
2851 dwc->gadget.name = "dwc3-gadget";
2852
2853 /*
2854 * REVISIT: Here we should clear all pending IRQs to be
2855 * sure we're starting from a well known location.
2856 */
2857
2858 ret = dwc3_gadget_init_endpoints(dwc);
2859 if (ret)
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002860 goto err4;
Felipe Balbi72246da2011-08-19 18:10:58 +03002861
2862 irq = platform_get_irq(to_platform_device(dwc->dev), 0);
2863
2864 ret = request_irq(irq, dwc3_interrupt, IRQF_SHARED,
2865 "dwc3", dwc);
2866 if (ret) {
2867 dev_err(dwc->dev, "failed to request irq #%d --> %d\n",
2868 irq, ret);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002869 goto err5;
Felipe Balbi72246da2011-08-19 18:10:58 +03002870 }
2871
Sebastian Andrzej Siewiorbb8b8a32011-09-13 17:54:39 +02002872 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2873 reg |= DWC3_DCFG_LPM_CAP;
2874 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
2875
Felipe Balbi72246da2011-08-19 18:10:58 +03002876 /* Enable all but Start and End of Frame IRQs */
Pavankumar Kondeti33fe6f12012-06-12 16:21:46 +05302877 reg = (DWC3_DEVTEN_EVNTOVERFLOWEN |
Felipe Balbi72246da2011-08-19 18:10:58 +03002878 DWC3_DEVTEN_CMDCMPLTEN |
2879 DWC3_DEVTEN_ERRTICERREN |
2880 DWC3_DEVTEN_WKUPEVTEN |
2881 DWC3_DEVTEN_ULSTCNGEN |
2882 DWC3_DEVTEN_CONNECTDONEEN |
2883 DWC3_DEVTEN_USBRSTEN |
2884 DWC3_DEVTEN_DISCONNEVTEN);
2885 dwc3_writel(dwc->regs, DWC3_DEVTEN, reg);
2886
Paul Zimmerman88df4272012-04-27 13:10:52 +03002887 /* Enable USB2 LPM and automatic phy suspend only on recent versions */
2888 if (dwc->revision >= DWC3_REVISION_194A) {
2889 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2890 reg |= DWC3_DCFG_LPM_CAP;
2891 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
2892
2893 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2894 reg &= ~(DWC3_DCTL_HIRD_THRES_MASK | DWC3_DCTL_L1_HIBER_EN);
2895
2896 /* TODO: This should be configurable */
Pratyush Anandd69dcdd2012-07-02 10:21:52 +05302897 reg |= DWC3_DCTL_HIRD_THRES(28);
Paul Zimmerman88df4272012-04-27 13:10:52 +03002898
2899 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2900
Vijayavardhan Vennapusaba79f6b2013-07-30 13:39:52 +05302901 dwc3_gadget_usb2_phy_suspend(dwc, true);
2902 dwc3_gadget_usb3_phy_suspend(dwc, true);
Paul Zimmerman88df4272012-04-27 13:10:52 +03002903 }
2904
Felipe Balbi72246da2011-08-19 18:10:58 +03002905 ret = device_register(&dwc->gadget.dev);
2906 if (ret) {
2907 dev_err(dwc->dev, "failed to register gadget device\n");
2908 put_device(&dwc->gadget.dev);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002909 goto err6;
Felipe Balbi72246da2011-08-19 18:10:58 +03002910 }
2911
2912 ret = usb_add_gadget_udc(dwc->dev, &dwc->gadget);
2913 if (ret) {
2914 dev_err(dwc->dev, "failed to register udc\n");
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002915 goto err7;
Felipe Balbi72246da2011-08-19 18:10:58 +03002916 }
2917
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +02002918 if (dwc->dotg) {
2919 /* dwc3 otg driver is active (DRD mode + SRPSupport=1) */
2920 ret = otg_set_peripheral(&dwc->dotg->otg, &dwc->gadget);
2921 if (ret) {
2922 dev_err(dwc->dev, "failed to set peripheral to otg\n");
2923 goto err7;
2924 }
Manu Gautamb5067272012-07-02 09:53:41 +05302925 } else {
2926 pm_runtime_no_callbacks(&dwc->gadget.dev);
2927 pm_runtime_set_active(&dwc->gadget.dev);
2928 pm_runtime_enable(&dwc->gadget.dev);
2929 pm_runtime_get(&dwc->gadget.dev);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +02002930 }
2931
Felipe Balbi72246da2011-08-19 18:10:58 +03002932 return 0;
2933
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002934err7:
Felipe Balbi72246da2011-08-19 18:10:58 +03002935 device_unregister(&dwc->gadget.dev);
2936
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002937err6:
Felipe Balbi72246da2011-08-19 18:10:58 +03002938 dwc3_writel(dwc->regs, DWC3_DEVTEN, 0x00);
2939 free_irq(irq, dwc);
2940
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002941err5:
Felipe Balbi72246da2011-08-19 18:10:58 +03002942 dwc3_gadget_free_endpoints(dwc);
2943
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002944err4:
Felipe Balbib0791fb2012-05-04 12:58:14 +03002945 dma_free_coherent(dwc->dev, DWC3_EP0_BOUNCE_SIZE,
2946 dwc->ep0_bounce, dwc->ep0_bounce_addr);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002947
Felipe Balbi72246da2011-08-19 18:10:58 +03002948err3:
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02002949 kfree(dwc->setup_buf);
Felipe Balbi72246da2011-08-19 18:10:58 +03002950
2951err2:
2952 dma_free_coherent(dwc->dev, sizeof(*dwc->ep0_trb),
2953 dwc->ep0_trb, dwc->ep0_trb_addr);
2954
2955err1:
2956 dma_free_coherent(dwc->dev, sizeof(*dwc->ctrl_req),
2957 dwc->ctrl_req, dwc->ctrl_req_addr);
2958
2959err0:
2960 return ret;
2961}
2962
2963void dwc3_gadget_exit(struct dwc3 *dwc)
2964{
2965 int irq;
Felipe Balbi72246da2011-08-19 18:10:58 +03002966
Manu Gautamb5067272012-07-02 09:53:41 +05302967 if (dwc->dotg) {
2968 pm_runtime_put(&dwc->gadget.dev);
2969 pm_runtime_disable(&dwc->gadget.dev);
2970 }
2971
Felipe Balbi72246da2011-08-19 18:10:58 +03002972 usb_del_gadget_udc(&dwc->gadget);
2973 irq = platform_get_irq(to_platform_device(dwc->dev), 0);
2974
2975 dwc3_writel(dwc->regs, DWC3_DEVTEN, 0x00);
2976 free_irq(irq, dwc);
2977
Felipe Balbi72246da2011-08-19 18:10:58 +03002978 dwc3_gadget_free_endpoints(dwc);
2979
Felipe Balbib0791fb2012-05-04 12:58:14 +03002980 dma_free_coherent(dwc->dev, DWC3_EP0_BOUNCE_SIZE,
2981 dwc->ep0_bounce, dwc->ep0_bounce_addr);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002982
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02002983 kfree(dwc->setup_buf);
Felipe Balbi72246da2011-08-19 18:10:58 +03002984
2985 dma_free_coherent(dwc->dev, sizeof(*dwc->ep0_trb),
2986 dwc->ep0_trb, dwc->ep0_trb_addr);
2987
2988 dma_free_coherent(dwc->dev, sizeof(*dwc->ctrl_req),
2989 dwc->ctrl_req, dwc->ctrl_req_addr);
2990
2991 device_unregister(&dwc->gadget.dev);
2992}