blob: 06643765c0e1b658b445278098a4417c17e7a9ff [file] [log] [blame]
Felipe Balbi72246da2011-08-19 18:10:58 +03001/**
2 * gadget.c - DesignWare USB3 DRD Controller Gadget Framework Link
3 *
4 * Copyright (C) 2010-2011 Texas Instruments Incorporated - http://www.ti.com
Felipe Balbi72246da2011-08-19 18:10:58 +03005 *
6 * Authors: Felipe Balbi <balbi@ti.com>,
7 * Sebastian Andrzej Siewior <bigeasy@linutronix.de>
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions, and the following disclaimer,
14 * without modification.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. The names of the above-listed copyright holders may not be used
19 * to endorse or promote products derived from this software without
20 * specific prior written permission.
21 *
22 * ALTERNATIVELY, this software may be distributed under the terms of the
23 * GNU General Public License ("GPL") version 2, as published by the Free
24 * Software Foundation.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
27 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
28 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
30 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 */
38
39#include <linux/kernel.h>
40#include <linux/delay.h>
41#include <linux/slab.h>
42#include <linux/spinlock.h>
43#include <linux/platform_device.h>
44#include <linux/pm_runtime.h>
45#include <linux/interrupt.h>
46#include <linux/io.h>
47#include <linux/list.h>
48#include <linux/dma-mapping.h>
49
50#include <linux/usb/ch9.h>
51#include <linux/usb/gadget.h>
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +020052#include <linux/usb/otg.h>
Felipe Balbi72246da2011-08-19 18:10:58 +030053
54#include "core.h"
55#include "gadget.h"
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +053056#include "debug.h"
Felipe Balbi72246da2011-08-19 18:10:58 +030057#include "io.h"
58
Felipe Balbi04a9bfc2012-01-02 18:25:43 +020059/**
60 * dwc3_gadget_set_test_mode - Enables USB2 Test Modes
61 * @dwc: pointer to our context structure
62 * @mode: the mode to set (J, K SE0 NAK, Force Enable)
63 *
64 * Caller should take care of locking. This function will
65 * return 0 on success or -EINVAL if wrong Test Selector
66 * is passed
67 */
68int dwc3_gadget_set_test_mode(struct dwc3 *dwc, int mode)
69{
70 u32 reg;
71
72 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
73 reg &= ~DWC3_DCTL_TSTCTRL_MASK;
74
75 switch (mode) {
76 case TEST_J:
77 case TEST_K:
78 case TEST_SE0_NAK:
79 case TEST_PACKET:
80 case TEST_FORCE_EN:
81 reg |= mode << 1;
82 break;
83 default:
84 return -EINVAL;
85 }
86
87 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
88
89 return 0;
90}
91
Felipe Balbi8598bde2012-01-02 18:55:57 +020092/**
93 * dwc3_gadget_set_link_state - Sets USB Link to a particular State
94 * @dwc: pointer to our context structure
95 * @state: the state to put link into
96 *
97 * Caller should take care of locking. This function will
Paul Zimmermanaee63e32012-02-24 17:32:15 -080098 * return 0 on success or -ETIMEDOUT.
Felipe Balbi8598bde2012-01-02 18:55:57 +020099 */
100int dwc3_gadget_set_link_state(struct dwc3 *dwc, enum dwc3_link_state state)
101{
Paul Zimmermanaee63e32012-02-24 17:32:15 -0800102 int retries = 10000;
Felipe Balbi8598bde2012-01-02 18:55:57 +0200103 u32 reg;
104
Paul Zimmerman88df4272012-04-27 13:10:52 +0300105 /*
106 * Wait until device controller is ready. Only applies to 1.94a and
107 * later RTL.
108 */
109 if (dwc->revision >= DWC3_REVISION_194A) {
110 while (--retries) {
111 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
112 if (reg & DWC3_DSTS_DCNRD)
113 udelay(5);
114 else
115 break;
116 }
117
118 if (retries <= 0)
119 return -ETIMEDOUT;
120 }
121
Felipe Balbi8598bde2012-01-02 18:55:57 +0200122 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
123 reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK;
124
125 /* set requested state */
126 reg |= DWC3_DCTL_ULSTCHNGREQ(state);
127 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
128
Paul Zimmerman88df4272012-04-27 13:10:52 +0300129 /*
130 * The following code is racy when called from dwc3_gadget_wakeup,
131 * and is not needed, at least on newer versions
132 */
133 if (dwc->revision >= DWC3_REVISION_194A)
134 return 0;
135
Felipe Balbi8598bde2012-01-02 18:55:57 +0200136 /* wait for a change in DSTS */
Paul Zimmerman8b9388f2012-04-27 12:52:01 +0300137 retries = 10000;
Felipe Balbi8598bde2012-01-02 18:55:57 +0200138 while (--retries) {
139 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
140
Felipe Balbi8598bde2012-01-02 18:55:57 +0200141 if (DWC3_DSTS_USBLNKST(reg) == state)
142 return 0;
143
Paul Zimmermanaee63e32012-02-24 17:32:15 -0800144 udelay(5);
Felipe Balbi8598bde2012-01-02 18:55:57 +0200145 }
146
147 dev_vdbg(dwc->dev, "link state change request timed out\n");
148
149 return -ETIMEDOUT;
150}
151
Felipe Balbi457e84b2012-01-18 18:04:09 +0200152/**
153 * dwc3_gadget_resize_tx_fifos - reallocate fifo spaces for current use-case
154 * @dwc: pointer to our context structure
155 *
156 * This function will a best effort FIFO allocation in order
157 * to improve FIFO usage and throughput, while still allowing
158 * us to enable as many endpoints as possible.
159 *
160 * Keep in mind that this operation will be highly dependent
161 * on the configured size for RAM1 - which contains TxFifo -,
162 * the amount of endpoints enabled on coreConsultant tool, and
163 * the width of the Master Bus.
164 *
165 * In the ideal world, we would always be able to satisfy the
166 * following equation:
167 *
168 * ((512 + 2 * MDWIDTH-Bytes) + (Number of IN Endpoints - 1) * \
169 * (3 * (1024 + MDWIDTH-Bytes) + MDWIDTH-Bytes)) / MDWIDTH-Bytes
170 *
171 * Unfortunately, due to many variables that's not always the case.
172 */
173int dwc3_gadget_resize_tx_fifos(struct dwc3 *dwc)
174{
175 int last_fifo_depth = 0;
176 int ram1_depth;
177 int fifo_size;
178 int mdwidth;
179 int num;
180
181 if (!dwc->needs_fifo_resize)
182 return 0;
183
184 ram1_depth = DWC3_RAM1_DEPTH(dwc->hwparams.hwparams7);
185 mdwidth = DWC3_MDWIDTH(dwc->hwparams.hwparams0);
186
187 /* MDWIDTH is represented in bits, we need it in bytes */
188 mdwidth >>= 3;
189
190 /*
191 * FIXME For now we will only allocate 1 wMaxPacketSize space
192 * for each enabled endpoint, later patches will come to
193 * improve this algorithm so that we better use the internal
194 * FIFO space
195 */
196 for (num = 0; num < DWC3_ENDPOINTS_NUM; num++) {
197 struct dwc3_ep *dep = dwc->eps[num];
198 int fifo_number = dep->number >> 1;
Felipe Balbi2e81c362012-02-02 13:01:12 +0200199 int mult = 1;
Felipe Balbi457e84b2012-01-18 18:04:09 +0200200 int tmp;
201
202 if (!(dep->number & 1))
203 continue;
204
205 if (!(dep->flags & DWC3_EP_ENABLED))
206 continue;
207
Ido Shayevitz57cdac12012-03-12 20:25:24 +0200208 if (usb_endpoint_xfer_bulk(dep->endpoint.desc)
209 || usb_endpoint_xfer_isoc(dep->endpoint.desc))
Felipe Balbi2e81c362012-02-02 13:01:12 +0200210 mult = 3;
211
212 /*
213 * REVISIT: the following assumes we will always have enough
214 * space available on the FIFO RAM for all possible use cases.
215 * Make sure that's true somehow and change FIFO allocation
216 * accordingly.
217 *
218 * If we have Bulk or Isochronous endpoints, we want
219 * them to be able to be very, very fast. So we're giving
220 * those endpoints a fifo_size which is enough for 3 full
221 * packets
222 */
223 tmp = mult * (dep->endpoint.maxpacket + mdwidth);
Felipe Balbi457e84b2012-01-18 18:04:09 +0200224 tmp += mdwidth;
225
226 fifo_size = DIV_ROUND_UP(tmp, mdwidth);
Felipe Balbi2e81c362012-02-02 13:01:12 +0200227
Felipe Balbi457e84b2012-01-18 18:04:09 +0200228 fifo_size |= (last_fifo_depth << 16);
229
230 dev_vdbg(dwc->dev, "%s: Fifo Addr %04x Size %d\n",
231 dep->name, last_fifo_depth, fifo_size & 0xffff);
232
233 dwc3_writel(dwc->regs, DWC3_GTXFIFOSIZ(fifo_number),
234 fifo_size);
235
236 last_fifo_depth += (fifo_size & 0xffff);
237 }
238
239 return 0;
240}
241
Felipe Balbi72246da2011-08-19 18:10:58 +0300242void dwc3_gadget_giveback(struct dwc3_ep *dep, struct dwc3_request *req,
243 int status)
244{
245 struct dwc3 *dwc = dep->dwc;
246
247 if (req->queued) {
Manu Gautam55d34222012-12-19 16:49:47 +0530248 req->queued = false;
249
Felipe Balbieeb720f2011-11-28 12:46:59 +0200250 if (req->request.num_mapped_sgs)
251 dep->busy_slot += req->request.num_mapped_sgs;
252 else
253 dep->busy_slot++;
254
Felipe Balbi72246da2011-08-19 18:10:58 +0300255 /*
256 * Skip LINK TRB. We can't use req->trb and check for
257 * DWC3_TRBCTL_LINK_TRB because it points the TRB we just
258 * completed (not the LINK TRB).
259 */
260 if (((dep->busy_slot & DWC3_TRB_MASK) == DWC3_TRB_NUM - 1) &&
Ido Shayevitz57cdac12012-03-12 20:25:24 +0200261 usb_endpoint_xfer_isoc(dep->endpoint.desc))
Felipe Balbi72246da2011-08-19 18:10:58 +0300262 dep->busy_slot++;
263 }
264 list_del(&req->list);
Felipe Balbieeb720f2011-11-28 12:46:59 +0200265 req->trb = NULL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300266
267 if (req->request.status == -EINPROGRESS)
268 req->request.status = status;
269
Pratyush Anand8d7bf592012-08-10 13:42:16 +0530270 if (dwc->ep0_bounced && dep->number == 0)
271 dwc->ep0_bounced = false;
272 else
273 usb_gadget_unmap_request(&dwc->gadget, &req->request,
274 req->direction);
Felipe Balbi72246da2011-08-19 18:10:58 +0300275
276 dev_dbg(dwc->dev, "request %p from %s completed %d/%d ===> %d\n",
277 req, dep->name, req->request.actual,
278 req->request.length, status);
279
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +0530280 dbg_done(dep->number, req->request.actual, req->request.status);
Felipe Balbi72246da2011-08-19 18:10:58 +0300281 spin_unlock(&dwc->lock);
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +0200282 req->request.complete(&dep->endpoint, &req->request);
Felipe Balbi72246da2011-08-19 18:10:58 +0300283 spin_lock(&dwc->lock);
284}
285
286static const char *dwc3_gadget_ep_cmd_string(u8 cmd)
287{
288 switch (cmd) {
289 case DWC3_DEPCMD_DEPSTARTCFG:
290 return "Start New Configuration";
291 case DWC3_DEPCMD_ENDTRANSFER:
292 return "End Transfer";
293 case DWC3_DEPCMD_UPDATETRANSFER:
294 return "Update Transfer";
295 case DWC3_DEPCMD_STARTTRANSFER:
296 return "Start Transfer";
297 case DWC3_DEPCMD_CLEARSTALL:
298 return "Clear Stall";
299 case DWC3_DEPCMD_SETSTALL:
300 return "Set Stall";
Paul Zimmerman88df4272012-04-27 13:10:52 +0300301 case DWC3_DEPCMD_GETEPSTATE:
302 return "Get Endpoint State";
Felipe Balbi72246da2011-08-19 18:10:58 +0300303 case DWC3_DEPCMD_SETTRANSFRESOURCE:
304 return "Set Endpoint Transfer Resource";
305 case DWC3_DEPCMD_SETEPCONFIG:
306 return "Set Endpoint Configuration";
307 default:
308 return "UNKNOWN command";
309 }
310}
311
Felipe Balbi573c2762012-04-24 16:19:11 +0300312int dwc3_send_gadget_generic_command(struct dwc3 *dwc, int cmd, u32 param)
313{
314 u32 timeout = 500;
315 u32 reg;
316
317 dwc3_writel(dwc->regs, DWC3_DGCMDPAR, param);
318 dwc3_writel(dwc->regs, DWC3_DGCMD, cmd | DWC3_DGCMD_CMDACT);
319
320 do {
321 reg = dwc3_readl(dwc->regs, DWC3_DGCMD);
322 if (!(reg & DWC3_DGCMD_CMDACT)) {
323 dev_vdbg(dwc->dev, "Command Complete --> %d\n",
324 DWC3_DGCMD_STATUS(reg));
325 return 0;
326 }
327
328 /*
329 * We can't sleep here, because it's also called from
330 * interrupt context.
331 */
332 timeout--;
333 if (!timeout)
334 return -ETIMEDOUT;
335 udelay(1);
336 } while (1);
337}
338
Felipe Balbi72246da2011-08-19 18:10:58 +0300339int dwc3_send_gadget_ep_cmd(struct dwc3 *dwc, unsigned ep,
340 unsigned cmd, struct dwc3_gadget_ep_cmd_params *params)
341{
342 struct dwc3_ep *dep = dwc->eps[ep];
Sebastian Andrzej Siewior61d58242011-08-29 16:46:38 +0200343 u32 timeout = 500;
Felipe Balbi72246da2011-08-19 18:10:58 +0300344 u32 reg;
345
346 dev_vdbg(dwc->dev, "%s: cmd '%s' params %08x %08x %08x\n",
347 dep->name,
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300348 dwc3_gadget_ep_cmd_string(cmd), params->param0,
349 params->param1, params->param2);
Felipe Balbi72246da2011-08-19 18:10:58 +0300350
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300351 dwc3_writel(dwc->regs, DWC3_DEPCMDPAR0(ep), params->param0);
352 dwc3_writel(dwc->regs, DWC3_DEPCMDPAR1(ep), params->param1);
353 dwc3_writel(dwc->regs, DWC3_DEPCMDPAR2(ep), params->param2);
Felipe Balbi72246da2011-08-19 18:10:58 +0300354
355 dwc3_writel(dwc->regs, DWC3_DEPCMD(ep), cmd | DWC3_DEPCMD_CMDACT);
356 do {
357 reg = dwc3_readl(dwc->regs, DWC3_DEPCMD(ep));
358 if (!(reg & DWC3_DEPCMD_CMDACT)) {
Felipe Balbi164f6e12011-08-27 20:29:58 +0300359 dev_vdbg(dwc->dev, "Command Complete --> %d\n",
360 DWC3_DEPCMD_STATUS(reg));
Vijayavardhan Vennapusa91ba6532013-01-30 17:35:45 +0530361 /* SW issues START TRANSFER command to isochronous ep
362 * with future frame interval. If future interval time
363 * has already passed when core recieves command, core
364 * will respond with an error(bit13 in Command complete
365 * event. Hence return error in this case.
366 */
367 if (reg & 0x2000)
368 return -EAGAIN;
369 else
370 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300371 }
372
373 /*
Felipe Balbi72246da2011-08-19 18:10:58 +0300374 * We can't sleep here, because it is also called from
375 * interrupt context.
376 */
377 timeout--;
378 if (!timeout)
379 return -ETIMEDOUT;
380
Sebastian Andrzej Siewior61d58242011-08-29 16:46:38 +0200381 udelay(1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300382 } while (1);
383}
384
Ido Shayevitzfa65a582012-06-06 14:39:54 +0300385dma_addr_t dwc3_trb_dma_offset(struct dwc3_ep *dep,
Felipe Balbif6bafc62012-02-06 11:04:53 +0200386 struct dwc3_trb *trb)
Felipe Balbi72246da2011-08-19 18:10:58 +0300387{
Paul Zimmermanc439ef82011-09-30 10:58:45 +0300388 u32 offset = (char *) trb - (char *) dep->trb_pool;
Felipe Balbi72246da2011-08-19 18:10:58 +0300389
390 return dep->trb_pool_dma + offset;
391}
392
393static int dwc3_alloc_trb_pool(struct dwc3_ep *dep)
394{
395 struct dwc3 *dwc = dep->dwc;
396
397 if (dep->trb_pool)
398 return 0;
399
400 if (dep->number == 0 || dep->number == 1)
401 return 0;
402
403 dep->trb_pool = dma_alloc_coherent(dwc->dev,
404 sizeof(struct dwc3_trb) * DWC3_TRB_NUM,
405 &dep->trb_pool_dma, GFP_KERNEL);
406 if (!dep->trb_pool) {
407 dev_err(dep->dwc->dev, "failed to allocate trb pool for %s\n",
408 dep->name);
409 return -ENOMEM;
410 }
411
412 return 0;
413}
414
415static void dwc3_free_trb_pool(struct dwc3_ep *dep)
416{
417 struct dwc3 *dwc = dep->dwc;
418
419 dma_free_coherent(dwc->dev, sizeof(struct dwc3_trb) * DWC3_TRB_NUM,
420 dep->trb_pool, dep->trb_pool_dma);
421
422 dep->trb_pool = NULL;
423 dep->trb_pool_dma = 0;
424}
425
426static int dwc3_gadget_start_config(struct dwc3 *dwc, struct dwc3_ep *dep)
427{
428 struct dwc3_gadget_ep_cmd_params params;
429 u32 cmd;
430
431 memset(&params, 0x00, sizeof(params));
432
433 if (dep->number != 1) {
434 cmd = DWC3_DEPCMD_DEPSTARTCFG;
435 /* XferRscIdx == 0 for ep0 and 2 for the remaining */
Paul Zimmermanb23c8432011-09-30 10:58:42 +0300436 if (dep->number > 1) {
437 if (dwc->start_config_issued)
438 return 0;
439 dwc->start_config_issued = true;
Felipe Balbi72246da2011-08-19 18:10:58 +0300440 cmd |= DWC3_DEPCMD_PARAM(2);
Paul Zimmermanb23c8432011-09-30 10:58:42 +0300441 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300442
443 return dwc3_send_gadget_ep_cmd(dwc, 0, cmd, &params);
444 }
445
446 return 0;
447}
448
449static int dwc3_gadget_set_ep_config(struct dwc3 *dwc, struct dwc3_ep *dep,
Felipe Balbic90bfae2011-11-29 13:11:21 +0200450 const struct usb_endpoint_descriptor *desc,
Felipe Balbi07e0ee82012-07-16 14:08:16 +0300451 const struct usb_ss_ep_comp_descriptor *comp_desc,
452 bool ignore)
Felipe Balbi72246da2011-08-19 18:10:58 +0300453{
454 struct dwc3_gadget_ep_cmd_params params;
455
456 memset(&params, 0x00, sizeof(params));
457
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300458 params.param0 = DWC3_DEPCFG_EP_TYPE(usb_endpoint_type(desc))
Chanho Parkf0ee6062012-08-31 16:54:07 +0900459 | DWC3_DEPCFG_MAX_PACKET_SIZE(usb_endpoint_maxp(desc));
460
461 /* Burst size is only needed in SuperSpeed mode */
462 if (dwc->gadget.speed == USB_SPEED_SUPER) {
463 u32 burst = dep->endpoint.maxburst - 1;
464
465 params.param0 |= DWC3_DEPCFG_BURST_SIZE(burst);
466 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300467
Felipe Balbi07e0ee82012-07-16 14:08:16 +0300468 if (ignore)
469 params.param0 |= DWC3_DEPCFG_IGN_SEQ_NUM;
Felipe Balbi72246da2011-08-19 18:10:58 +0300470
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300471 params.param1 = DWC3_DEPCFG_XFER_COMPLETE_EN
472 | DWC3_DEPCFG_XFER_NOT_READY_EN;
Felipe Balbi72246da2011-08-19 18:10:58 +0300473
Felipe Balbi18b7ede2012-01-02 13:35:41 +0200474 if (usb_ss_max_streams(comp_desc) && usb_endpoint_xfer_bulk(desc)) {
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300475 params.param1 |= DWC3_DEPCFG_STREAM_CAPABLE
476 | DWC3_DEPCFG_STREAM_EVENT_EN;
Felipe Balbi879631a2011-09-30 10:58:47 +0300477 dep->stream_capable = true;
478 }
479
Felipe Balbi72246da2011-08-19 18:10:58 +0300480 if (usb_endpoint_xfer_isoc(desc))
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300481 params.param1 |= DWC3_DEPCFG_XFER_IN_PROGRESS_EN;
Felipe Balbi72246da2011-08-19 18:10:58 +0300482
483 /*
484 * We are doing 1:1 mapping for endpoints, meaning
485 * Physical Endpoints 2 maps to Logical Endpoint 2 and
486 * so on. We consider the direction bit as part of the physical
487 * endpoint number. So USB endpoint 0x81 is 0x03.
488 */
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300489 params.param1 |= DWC3_DEPCFG_EP_NUMBER(dep->number);
Felipe Balbi72246da2011-08-19 18:10:58 +0300490
491 /*
492 * We must use the lower 16 TX FIFOs even though
493 * HW might have more
494 */
495 if (dep->direction)
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300496 params.param0 |= DWC3_DEPCFG_FIFO_NUMBER(dep->number >> 1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300497
498 if (desc->bInterval) {
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300499 params.param1 |= DWC3_DEPCFG_BINTERVAL_M1(desc->bInterval - 1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300500 dep->interval = 1 << (desc->bInterval - 1);
501 }
502
503 return dwc3_send_gadget_ep_cmd(dwc, dep->number,
504 DWC3_DEPCMD_SETEPCONFIG, &params);
505}
506
507static int dwc3_gadget_set_xfer_resource(struct dwc3 *dwc, struct dwc3_ep *dep)
508{
509 struct dwc3_gadget_ep_cmd_params params;
510
511 memset(&params, 0x00, sizeof(params));
512
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300513 params.param0 = DWC3_DEPXFERCFG_NUM_XFER_RES(1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300514
515 return dwc3_send_gadget_ep_cmd(dwc, dep->number,
516 DWC3_DEPCMD_SETTRANSFRESOURCE, &params);
517}
518
519/**
520 * __dwc3_gadget_ep_enable - Initializes a HW endpoint
521 * @dep: endpoint to be initialized
522 * @desc: USB Endpoint Descriptor
523 *
524 * Caller should take care of locking
525 */
526static int __dwc3_gadget_ep_enable(struct dwc3_ep *dep,
Felipe Balbic90bfae2011-11-29 13:11:21 +0200527 const struct usb_endpoint_descriptor *desc,
Felipe Balbi07e0ee82012-07-16 14:08:16 +0300528 const struct usb_ss_ep_comp_descriptor *comp_desc,
529 bool ignore)
Felipe Balbi72246da2011-08-19 18:10:58 +0300530{
531 struct dwc3 *dwc = dep->dwc;
532 u32 reg;
533 int ret = -ENOMEM;
534
535 if (!(dep->flags & DWC3_EP_ENABLED)) {
536 ret = dwc3_gadget_start_config(dwc, dep);
537 if (ret)
538 return ret;
539 }
540
Felipe Balbi07e0ee82012-07-16 14:08:16 +0300541 ret = dwc3_gadget_set_ep_config(dwc, dep, desc, comp_desc, ignore);
Felipe Balbi72246da2011-08-19 18:10:58 +0300542 if (ret)
543 return ret;
544
545 if (!(dep->flags & DWC3_EP_ENABLED)) {
Felipe Balbif6bafc62012-02-06 11:04:53 +0200546 struct dwc3_trb *trb_st_hw;
547 struct dwc3_trb *trb_link;
Felipe Balbi72246da2011-08-19 18:10:58 +0300548
549 ret = dwc3_gadget_set_xfer_resource(dwc, dep);
550 if (ret)
551 return ret;
552
Ido Shayevitz57cdac12012-03-12 20:25:24 +0200553 dep->endpoint.desc = desc;
Felipe Balbic90bfae2011-11-29 13:11:21 +0200554 dep->comp_desc = comp_desc;
Felipe Balbi72246da2011-08-19 18:10:58 +0300555 dep->type = usb_endpoint_type(desc);
556 dep->flags |= DWC3_EP_ENABLED;
557
558 reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
559 reg |= DWC3_DALEPENA_EP(dep->number);
560 dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
561
562 if (!usb_endpoint_xfer_isoc(desc))
563 return 0;
564
565 memset(&trb_link, 0, sizeof(trb_link));
566
Paul Zimmerman1d046792012-02-15 18:56:56 -0800567 /* Link TRB for ISOC. The HWO bit is never reset */
Felipe Balbi72246da2011-08-19 18:10:58 +0300568 trb_st_hw = &dep->trb_pool[0];
569
Felipe Balbif6bafc62012-02-06 11:04:53 +0200570 trb_link = &dep->trb_pool[DWC3_TRB_NUM - 1];
Felipe Balbi72246da2011-08-19 18:10:58 +0300571
Felipe Balbif6bafc62012-02-06 11:04:53 +0200572 trb_link->bpl = lower_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw));
573 trb_link->bph = upper_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw));
574 trb_link->ctrl |= DWC3_TRBCTL_LINK_TRB;
575 trb_link->ctrl |= DWC3_TRB_CTRL_HWO;
Felipe Balbi72246da2011-08-19 18:10:58 +0300576 }
577
578 return 0;
579}
580
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200581static void dwc3_stop_active_transfer(struct dwc3 *dwc, u32 epnum);
582static void dwc3_remove_requests(struct dwc3 *dwc, struct dwc3_ep *dep)
Felipe Balbi72246da2011-08-19 18:10:58 +0300583{
584 struct dwc3_request *req;
585
Felipe Balbib129eb72012-02-17 12:10:04 +0200586 if (!list_empty(&dep->req_queued)) {
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200587 dwc3_stop_active_transfer(dwc, dep->number);
588
Pratyush Anande67fdeb2012-07-06 15:19:10 +0530589 /* - giveback all requests to gadget driver */
Pratyush Anand110ff602012-06-15 11:54:36 +0530590 while (!list_empty(&dep->req_queued)) {
591 req = next_request(&dep->req_queued);
592
593 dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
594 }
Felipe Balbib129eb72012-02-17 12:10:04 +0200595 }
596
Felipe Balbi72246da2011-08-19 18:10:58 +0300597 while (!list_empty(&dep->request_list)) {
598 req = next_request(&dep->request_list);
599
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200600 dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
Felipe Balbi72246da2011-08-19 18:10:58 +0300601 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300602}
603
604/**
605 * __dwc3_gadget_ep_disable - Disables a HW endpoint
606 * @dep: the endpoint to disable
607 *
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200608 * This function also removes requests which are currently processed ny the
609 * hardware and those which are not yet scheduled.
610 * Caller should take care of locking.
Felipe Balbi72246da2011-08-19 18:10:58 +0300611 */
Felipe Balbi72246da2011-08-19 18:10:58 +0300612static int __dwc3_gadget_ep_disable(struct dwc3_ep *dep)
613{
614 struct dwc3 *dwc = dep->dwc;
615 u32 reg;
616
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200617 dwc3_remove_requests(dwc, dep);
Felipe Balbi72246da2011-08-19 18:10:58 +0300618
619 reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
620 reg &= ~DWC3_DALEPENA_EP(dep->number);
621 dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
622
Felipe Balbi879631a2011-09-30 10:58:47 +0300623 dep->stream_capable = false;
Ido Shayevitzf9c56cd2012-02-08 13:56:48 +0200624 dep->endpoint.desc = NULL;
Felipe Balbic90bfae2011-11-29 13:11:21 +0200625 dep->comp_desc = NULL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300626 dep->type = 0;
Felipe Balbi879631a2011-09-30 10:58:47 +0300627 dep->flags = 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300628
629 return 0;
630}
631
632/* -------------------------------------------------------------------------- */
633
634static int dwc3_gadget_ep0_enable(struct usb_ep *ep,
635 const struct usb_endpoint_descriptor *desc)
636{
637 return -EINVAL;
638}
639
640static int dwc3_gadget_ep0_disable(struct usb_ep *ep)
641{
642 return -EINVAL;
643}
644
645/* -------------------------------------------------------------------------- */
646
647static int dwc3_gadget_ep_enable(struct usb_ep *ep,
648 const struct usb_endpoint_descriptor *desc)
649{
650 struct dwc3_ep *dep;
651 struct dwc3 *dwc;
652 unsigned long flags;
653 int ret;
654
655 if (!ep || !desc || desc->bDescriptorType != USB_DT_ENDPOINT) {
656 pr_debug("dwc3: invalid parameters\n");
657 return -EINVAL;
658 }
659
660 if (!desc->wMaxPacketSize) {
661 pr_debug("dwc3: missing wMaxPacketSize\n");
662 return -EINVAL;
663 }
664
665 dep = to_dwc3_ep(ep);
666 dwc = dep->dwc;
667
Felipe Balbi14395072012-08-15 12:28:29 +0300668 if (dep->flags & DWC3_EP_ENABLED) {
669 dev_WARN_ONCE(dwc->dev, true, "%s is already enabled\n",
670 dep->name);
671 return 0;
672 }
673
Felipe Balbi72246da2011-08-19 18:10:58 +0300674 switch (usb_endpoint_type(desc)) {
675 case USB_ENDPOINT_XFER_CONTROL:
Anton Tikhomirov27a78d62012-02-23 15:38:46 +0900676 strlcat(dep->name, "-control", sizeof(dep->name));
Felipe Balbi72246da2011-08-19 18:10:58 +0300677 break;
678 case USB_ENDPOINT_XFER_ISOC:
Anton Tikhomirov27a78d62012-02-23 15:38:46 +0900679 strlcat(dep->name, "-isoc", sizeof(dep->name));
Felipe Balbi72246da2011-08-19 18:10:58 +0300680 break;
681 case USB_ENDPOINT_XFER_BULK:
Anton Tikhomirov27a78d62012-02-23 15:38:46 +0900682 strlcat(dep->name, "-bulk", sizeof(dep->name));
Felipe Balbi72246da2011-08-19 18:10:58 +0300683 break;
684 case USB_ENDPOINT_XFER_INT:
Anton Tikhomirov27a78d62012-02-23 15:38:46 +0900685 strlcat(dep->name, "-int", sizeof(dep->name));
Felipe Balbi72246da2011-08-19 18:10:58 +0300686 break;
687 default:
688 dev_err(dwc->dev, "invalid endpoint transfer type\n");
689 }
690
Felipe Balbi72246da2011-08-19 18:10:58 +0300691 dev_vdbg(dwc->dev, "Enabling %s\n", dep->name);
692
693 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi07e0ee82012-07-16 14:08:16 +0300694 ret = __dwc3_gadget_ep_enable(dep, desc, ep->comp_desc, false);
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +0530695 dbg_event(dep->number, "ENABLE", ret);
Felipe Balbi72246da2011-08-19 18:10:58 +0300696 spin_unlock_irqrestore(&dwc->lock, flags);
697
698 return ret;
699}
700
701static int dwc3_gadget_ep_disable(struct usb_ep *ep)
702{
703 struct dwc3_ep *dep;
704 struct dwc3 *dwc;
705 unsigned long flags;
706 int ret;
707
708 if (!ep) {
709 pr_debug("dwc3: invalid parameters\n");
710 return -EINVAL;
711 }
712
713 dep = to_dwc3_ep(ep);
714 dwc = dep->dwc;
715
716 if (!(dep->flags & DWC3_EP_ENABLED)) {
717 dev_WARN_ONCE(dwc->dev, true, "%s is already disabled\n",
718 dep->name);
719 return 0;
720 }
721
722 snprintf(dep->name, sizeof(dep->name), "ep%d%s",
723 dep->number >> 1,
724 (dep->number & 1) ? "in" : "out");
725
726 spin_lock_irqsave(&dwc->lock, flags);
727 ret = __dwc3_gadget_ep_disable(dep);
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +0530728 dbg_event(dep->number, "DISABLE", ret);
Felipe Balbi72246da2011-08-19 18:10:58 +0300729 spin_unlock_irqrestore(&dwc->lock, flags);
730
731 return ret;
732}
733
734static struct usb_request *dwc3_gadget_ep_alloc_request(struct usb_ep *ep,
735 gfp_t gfp_flags)
736{
737 struct dwc3_request *req;
738 struct dwc3_ep *dep = to_dwc3_ep(ep);
739 struct dwc3 *dwc = dep->dwc;
740
741 req = kzalloc(sizeof(*req), gfp_flags);
742 if (!req) {
743 dev_err(dwc->dev, "not enough memory\n");
744 return NULL;
745 }
746
747 req->epnum = dep->number;
748 req->dep = dep;
Felipe Balbi72246da2011-08-19 18:10:58 +0300749
750 return &req->request;
751}
752
753static void dwc3_gadget_ep_free_request(struct usb_ep *ep,
754 struct usb_request *request)
755{
756 struct dwc3_request *req = to_dwc3_request(request);
757
758 kfree(req);
759}
760
Felipe Balbic71fc372011-11-22 11:37:34 +0200761/**
762 * dwc3_prepare_one_trb - setup one TRB from one request
763 * @dep: endpoint for which this request is prepared
764 * @req: dwc3_request pointer
765 */
Felipe Balbi68e823e2011-11-28 12:25:01 +0200766static void dwc3_prepare_one_trb(struct dwc3_ep *dep,
Felipe Balbieeb720f2011-11-28 12:46:59 +0200767 struct dwc3_request *req, dma_addr_t dma,
768 unsigned length, unsigned last, unsigned chain)
Felipe Balbic71fc372011-11-22 11:37:34 +0200769{
Felipe Balbieeb720f2011-11-28 12:46:59 +0200770 struct dwc3 *dwc = dep->dwc;
Felipe Balbif6bafc62012-02-06 11:04:53 +0200771 struct dwc3_trb *trb;
Felipe Balbic71fc372011-11-22 11:37:34 +0200772
773 unsigned int cur_slot;
774
Felipe Balbieeb720f2011-11-28 12:46:59 +0200775 dev_vdbg(dwc->dev, "%s: req %p dma %08llx length %d%s%s\n",
776 dep->name, req, (unsigned long long) dma,
777 length, last ? " last" : "",
778 chain ? " chain" : "");
779
Felipe Balbif6bafc62012-02-06 11:04:53 +0200780 trb = &dep->trb_pool[dep->free_slot & DWC3_TRB_MASK];
Felipe Balbic71fc372011-11-22 11:37:34 +0200781 cur_slot = dep->free_slot;
782 dep->free_slot++;
783
784 /* Skip the LINK-TRB on ISOC */
Vijayavardhan Vennapusa2a444ad2013-02-01 13:20:59 +0530785 if (((dep->free_slot & DWC3_TRB_MASK) == DWC3_TRB_NUM - 1) &&
Ido Shayevitz57cdac12012-03-12 20:25:24 +0200786 usb_endpoint_xfer_isoc(dep->endpoint.desc))
Vijayavardhan Vennapusa2a444ad2013-02-01 13:20:59 +0530787 dep->free_slot++;
Felipe Balbic71fc372011-11-22 11:37:34 +0200788
Felipe Balbieeb720f2011-11-28 12:46:59 +0200789 if (!req->trb) {
790 dwc3_gadget_move_request_queued(req);
Felipe Balbif6bafc62012-02-06 11:04:53 +0200791 req->trb = trb;
792 req->trb_dma = dwc3_trb_dma_offset(dep, trb);
Felipe Balbieeb720f2011-11-28 12:46:59 +0200793 }
Felipe Balbic71fc372011-11-22 11:37:34 +0200794
Felipe Balbif6bafc62012-02-06 11:04:53 +0200795 trb->size = DWC3_TRB_SIZE_LENGTH(length);
796 trb->bpl = lower_32_bits(dma);
797 trb->bph = upper_32_bits(dma);
Felipe Balbic71fc372011-11-22 11:37:34 +0200798
Ido Shayevitz57cdac12012-03-12 20:25:24 +0200799 switch (usb_endpoint_type(dep->endpoint.desc)) {
Felipe Balbic71fc372011-11-22 11:37:34 +0200800 case USB_ENDPOINT_XFER_CONTROL:
Felipe Balbif6bafc62012-02-06 11:04:53 +0200801 trb->ctrl = DWC3_TRBCTL_CONTROL_SETUP;
Felipe Balbic71fc372011-11-22 11:37:34 +0200802 break;
803
804 case USB_ENDPOINT_XFER_ISOC:
Felipe Balbif6bafc62012-02-06 11:04:53 +0200805 trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS_FIRST;
Felipe Balbic71fc372011-11-22 11:37:34 +0200806
Pratyush Ananddf023422012-05-21 12:42:54 +0530807 if (!req->request.no_interrupt)
Felipe Balbif6bafc62012-02-06 11:04:53 +0200808 trb->ctrl |= DWC3_TRB_CTRL_IOC;
Felipe Balbic71fc372011-11-22 11:37:34 +0200809 break;
810
811 case USB_ENDPOINT_XFER_BULK:
812 case USB_ENDPOINT_XFER_INT:
Felipe Balbif6bafc62012-02-06 11:04:53 +0200813 trb->ctrl = DWC3_TRBCTL_NORMAL;
Felipe Balbic71fc372011-11-22 11:37:34 +0200814 break;
815 default:
816 /*
817 * This is only possible with faulty memory because we
818 * checked it already :)
819 */
820 BUG();
821 }
822
Ido Shayevitz57cdac12012-03-12 20:25:24 +0200823 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbif6bafc62012-02-06 11:04:53 +0200824 trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI;
825 trb->ctrl |= DWC3_TRB_CTRL_CSP;
826 } else {
827 if (chain)
828 trb->ctrl |= DWC3_TRB_CTRL_CHN;
Felipe Balbic71fc372011-11-22 11:37:34 +0200829
Felipe Balbif6bafc62012-02-06 11:04:53 +0200830 if (last)
831 trb->ctrl |= DWC3_TRB_CTRL_LST;
832 }
833
Ido Shayevitz57cdac12012-03-12 20:25:24 +0200834 if (usb_endpoint_xfer_bulk(dep->endpoint.desc) && dep->stream_capable)
Felipe Balbif6bafc62012-02-06 11:04:53 +0200835 trb->ctrl |= DWC3_TRB_CTRL_SID_SOFN(req->request.stream_id);
836
837 trb->ctrl |= DWC3_TRB_CTRL_HWO;
Felipe Balbic71fc372011-11-22 11:37:34 +0200838}
839
Felipe Balbi72246da2011-08-19 18:10:58 +0300840/*
841 * dwc3_prepare_trbs - setup TRBs from requests
842 * @dep: endpoint for which requests are being prepared
843 * @starting: true if the endpoint is idle and no requests are queued.
844 *
Paul Zimmerman1d046792012-02-15 18:56:56 -0800845 * The function goes through the requests list and sets up TRBs for the
846 * transfers. The function returns once there are no more TRBs available or
847 * it runs out of requests.
Felipe Balbi72246da2011-08-19 18:10:58 +0300848 */
Felipe Balbi68e823e2011-11-28 12:25:01 +0200849static void dwc3_prepare_trbs(struct dwc3_ep *dep, bool starting)
Felipe Balbi72246da2011-08-19 18:10:58 +0300850{
Felipe Balbi68e823e2011-11-28 12:25:01 +0200851 struct dwc3_request *req, *n;
Felipe Balbi72246da2011-08-19 18:10:58 +0300852 u32 trbs_left;
Paul Zimmerman8d62cd62012-02-15 13:35:06 +0200853 u32 max;
Felipe Balbic71fc372011-11-22 11:37:34 +0200854 unsigned int last_one = 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300855
856 BUILD_BUG_ON_NOT_POWER_OF_2(DWC3_TRB_NUM);
857
858 /* the first request must not be queued */
859 trbs_left = (dep->busy_slot - dep->free_slot) & DWC3_TRB_MASK;
Felipe Balbic71fc372011-11-22 11:37:34 +0200860
Paul Zimmerman8d62cd62012-02-15 13:35:06 +0200861 /* Can't wrap around on a non-isoc EP since there's no link TRB */
Ido Shayevitz57cdac12012-03-12 20:25:24 +0200862 if (!usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Paul Zimmerman8d62cd62012-02-15 13:35:06 +0200863 max = DWC3_TRB_NUM - (dep->free_slot & DWC3_TRB_MASK);
864 if (trbs_left > max)
865 trbs_left = max;
866 }
867
Felipe Balbi72246da2011-08-19 18:10:58 +0300868 /*
Paul Zimmerman1d046792012-02-15 18:56:56 -0800869 * If busy & slot are equal than it is either full or empty. If we are
870 * starting to process requests then we are empty. Otherwise we are
Felipe Balbi72246da2011-08-19 18:10:58 +0300871 * full and don't do anything
872 */
873 if (!trbs_left) {
874 if (!starting)
Felipe Balbi68e823e2011-11-28 12:25:01 +0200875 return;
Felipe Balbi72246da2011-08-19 18:10:58 +0300876 trbs_left = DWC3_TRB_NUM;
877 /*
878 * In case we start from scratch, we queue the ISOC requests
879 * starting from slot 1. This is done because we use ring
880 * buffer and have no LST bit to stop us. Instead, we place
Paul Zimmerman1d046792012-02-15 18:56:56 -0800881 * IOC bit every TRB_NUM/4. We try to avoid having an interrupt
Felipe Balbi72246da2011-08-19 18:10:58 +0300882 * after the first request so we start at slot 1 and have
883 * 7 requests proceed before we hit the first IOC.
884 * Other transfer types don't use the ring buffer and are
885 * processed from the first TRB until the last one. Since we
886 * don't wrap around we have to start at the beginning.
887 */
Ido Shayevitz57cdac12012-03-12 20:25:24 +0200888 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbi72246da2011-08-19 18:10:58 +0300889 dep->busy_slot = 1;
890 dep->free_slot = 1;
891 } else {
892 dep->busy_slot = 0;
893 dep->free_slot = 0;
894 }
895 }
896
897 /* The last TRB is a link TRB, not used for xfer */
Ido Shayevitz57cdac12012-03-12 20:25:24 +0200898 if ((trbs_left <= 1) && usb_endpoint_xfer_isoc(dep->endpoint.desc))
Felipe Balbi68e823e2011-11-28 12:25:01 +0200899 return;
Felipe Balbi72246da2011-08-19 18:10:58 +0300900
901 list_for_each_entry_safe(req, n, &dep->request_list, list) {
Felipe Balbieeb720f2011-11-28 12:46:59 +0200902 unsigned length;
903 dma_addr_t dma;
Felipe Balbi72246da2011-08-19 18:10:58 +0300904
Felipe Balbieeb720f2011-11-28 12:46:59 +0200905 if (req->request.num_mapped_sgs > 0) {
906 struct usb_request *request = &req->request;
907 struct scatterlist *sg = request->sg;
908 struct scatterlist *s;
909 int i;
Felipe Balbi72246da2011-08-19 18:10:58 +0300910
Felipe Balbieeb720f2011-11-28 12:46:59 +0200911 for_each_sg(sg, s, request->num_mapped_sgs, i) {
912 unsigned chain = true;
Felipe Balbi72246da2011-08-19 18:10:58 +0300913
Felipe Balbieeb720f2011-11-28 12:46:59 +0200914 length = sg_dma_len(s);
915 dma = sg_dma_address(s);
Felipe Balbi72246da2011-08-19 18:10:58 +0300916
Paul Zimmerman1d046792012-02-15 18:56:56 -0800917 if (i == (request->num_mapped_sgs - 1) ||
918 sg_is_last(s)) {
Felipe Balbieeb720f2011-11-28 12:46:59 +0200919 last_one = true;
920 chain = false;
921 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300922
Felipe Balbieeb720f2011-11-28 12:46:59 +0200923 trbs_left--;
924 if (!trbs_left)
925 last_one = true;
Felipe Balbi72246da2011-08-19 18:10:58 +0300926
Felipe Balbieeb720f2011-11-28 12:46:59 +0200927 if (last_one)
928 chain = false;
Felipe Balbi72246da2011-08-19 18:10:58 +0300929
Felipe Balbieeb720f2011-11-28 12:46:59 +0200930 dwc3_prepare_one_trb(dep, req, dma, length,
931 last_one, chain);
Felipe Balbi72246da2011-08-19 18:10:58 +0300932
Felipe Balbieeb720f2011-11-28 12:46:59 +0200933 if (last_one)
934 break;
935 }
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +0530936 dbg_queue(dep->number, &req->request, 0);
Felipe Balbi72246da2011-08-19 18:10:58 +0300937 } else {
Felipe Balbieeb720f2011-11-28 12:46:59 +0200938 dma = req->request.dma;
939 length = req->request.length;
940 trbs_left--;
941
942 if (!trbs_left)
943 last_one = 1;
944
945 /* Is this the last request? */
946 if (list_is_last(&req->list, &dep->request_list))
947 last_one = 1;
948
949 dwc3_prepare_one_trb(dep, req, dma, length,
950 last_one, false);
951
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +0530952 dbg_queue(dep->number, &req->request, 0);
Felipe Balbieeb720f2011-11-28 12:46:59 +0200953 if (last_one)
954 break;
Felipe Balbi72246da2011-08-19 18:10:58 +0300955 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300956 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300957}
958
959static int __dwc3_gadget_kick_transfer(struct dwc3_ep *dep, u16 cmd_param,
960 int start_new)
961{
962 struct dwc3_gadget_ep_cmd_params params;
Vijayavardhan Vennapusa91ba6532013-01-30 17:35:45 +0530963 struct dwc3_request *req, *req1, *n;
Felipe Balbi72246da2011-08-19 18:10:58 +0300964 struct dwc3 *dwc = dep->dwc;
965 int ret;
966 u32 cmd;
967
968 if (start_new && (dep->flags & DWC3_EP_BUSY)) {
969 dev_vdbg(dwc->dev, "%s: endpoint busy\n", dep->name);
970 return -EBUSY;
971 }
972 dep->flags &= ~DWC3_EP_PENDING_REQUEST;
973
974 /*
975 * If we are getting here after a short-out-packet we don't enqueue any
976 * new requests as we try to set the IOC bit only on the last request.
977 */
978 if (start_new) {
979 if (list_empty(&dep->req_queued))
980 dwc3_prepare_trbs(dep, start_new);
981
982 /* req points to the first request which will be sent */
983 req = next_request(&dep->req_queued);
984 } else {
Felipe Balbi68e823e2011-11-28 12:25:01 +0200985 dwc3_prepare_trbs(dep, start_new);
986
Felipe Balbi72246da2011-08-19 18:10:58 +0300987 /*
Paul Zimmerman1d046792012-02-15 18:56:56 -0800988 * req points to the first request where HWO changed from 0 to 1
Felipe Balbi72246da2011-08-19 18:10:58 +0300989 */
Felipe Balbi68e823e2011-11-28 12:25:01 +0200990 req = next_request(&dep->req_queued);
Felipe Balbi72246da2011-08-19 18:10:58 +0300991 }
992 if (!req) {
993 dep->flags |= DWC3_EP_PENDING_REQUEST;
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +0530994 dbg_event(dep->number, "NO REQ", 0);
Felipe Balbi72246da2011-08-19 18:10:58 +0300995 return 0;
996 }
997
998 memset(&params, 0, sizeof(params));
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300999 params.param0 = upper_32_bits(req->trb_dma);
1000 params.param1 = lower_32_bits(req->trb_dma);
Felipe Balbi72246da2011-08-19 18:10:58 +03001001
1002 if (start_new)
1003 cmd = DWC3_DEPCMD_STARTTRANSFER;
1004 else
1005 cmd = DWC3_DEPCMD_UPDATETRANSFER;
1006
1007 cmd |= DWC3_DEPCMD_PARAM(cmd_param);
1008 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number, cmd, &params);
1009 if (ret < 0) {
1010 dev_dbg(dwc->dev, "failed to send STARTTRANSFER command\n");
1011
Vijayavardhan Vennapusa91ba6532013-01-30 17:35:45 +05301012 if ((ret == -EAGAIN) && start_new &&
1013 usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
1014 /* If bit13 in Command complete event is set, software
1015 * must issue ENDTRANDFER command and wait for
1016 * Xfernotready event to queue the requests again.
1017 */
1018 if (!dep->resource_index) {
1019 dep->resource_index =
1020 dwc3_gadget_ep_get_transfer_index(dwc,
1021 dep->number);
1022 WARN_ON_ONCE(!dep->resource_index);
1023 }
1024 dwc3_stop_active_transfer(dwc, dep->number);
1025 list_for_each_entry_safe_reverse(req1, n,
1026 &dep->req_queued, list) {
1027 req1->trb = NULL;
1028 dwc3_gadget_move_request_list_front(req1);
1029 if (req->request.num_mapped_sgs)
1030 dep->busy_slot +=
1031 req->request.num_mapped_sgs;
1032 else
1033 dep->busy_slot++;
1034 if ((dep->busy_slot & DWC3_TRB_MASK) ==
1035 DWC3_TRB_NUM - 1)
1036 dep->busy_slot++;
1037 }
1038 return ret;
1039 } else {
1040 /*
1041 * FIXME we need to iterate over the list of requests
1042 * here and stop, unmap, free and del each of the linked
1043 * requests instead of what we do now.
1044 */
1045 usb_gadget_unmap_request(&dwc->gadget, &req->request,
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02001046 req->direction);
Vijayavardhan Vennapusa91ba6532013-01-30 17:35:45 +05301047 list_del(&req->list);
1048 return ret;
1049 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001050 }
1051
1052 dep->flags |= DWC3_EP_BUSY;
Felipe Balbi25b8ff62011-11-04 12:32:47 +02001053
Paul Zimmermanf39a37f2012-03-29 18:16:54 +00001054 if (start_new) {
Felipe Balbi4959cfc2012-06-06 12:04:13 +03001055 dep->resource_index = dwc3_gadget_ep_get_transfer_index(dwc,
Paul Zimmermanf39a37f2012-03-29 18:16:54 +00001056 dep->number);
Felipe Balbi4959cfc2012-06-06 12:04:13 +03001057 WARN_ON_ONCE(!dep->resource_index);
Paul Zimmermanf39a37f2012-03-29 18:16:54 +00001058 }
Felipe Balbi25b8ff62011-11-04 12:32:47 +02001059
Felipe Balbi72246da2011-08-19 18:10:58 +03001060 return 0;
1061}
1062
Pratyush Anand73939b02012-05-25 18:54:56 +05301063static void __dwc3_gadget_start_isoc(struct dwc3 *dwc,
1064 struct dwc3_ep *dep, u32 cur_uf)
1065{
1066 u32 uf;
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +05301067 int ret;
Pratyush Anand73939b02012-05-25 18:54:56 +05301068
Vijayavardhan Vennapusa91ba6532013-01-30 17:35:45 +05301069 dep->current_uf = cur_uf;
1070
Pratyush Anand73939b02012-05-25 18:54:56 +05301071 if (list_empty(&dep->request_list)) {
1072 dev_vdbg(dwc->dev, "ISOC ep %s run out for requests.\n",
1073 dep->name);
Pratyush Anandac417602012-08-30 12:21:43 +05301074 dep->flags |= DWC3_EP_PENDING_REQUEST;
Pratyush Anand73939b02012-05-25 18:54:56 +05301075 return;
1076 }
1077
1078 /* 4 micro frames in the future */
1079 uf = cur_uf + dep->interval * 4;
1080
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +05301081 ret = __dwc3_gadget_kick_transfer(dep, uf, 1);
1082 if (ret < 0)
1083 dbg_event(dep->number, "QUEUE", ret);
Pratyush Anand73939b02012-05-25 18:54:56 +05301084}
1085
1086static void dwc3_gadget_start_isoc(struct dwc3 *dwc,
1087 struct dwc3_ep *dep, const struct dwc3_event_depevt *event)
1088{
1089 u32 cur_uf, mask;
1090
1091 mask = ~(dep->interval - 1);
1092 cur_uf = event->parameters & mask;
1093
1094 __dwc3_gadget_start_isoc(dwc, dep, cur_uf);
1095}
1096
Felipe Balbi72246da2011-08-19 18:10:58 +03001097static int __dwc3_gadget_ep_queue(struct dwc3_ep *dep, struct dwc3_request *req)
1098{
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02001099 struct dwc3 *dwc = dep->dwc;
1100 int ret;
1101
Manu Gautamd2b99e12013-02-11 15:53:34 +05301102 if (req->request.status == -EINPROGRESS) {
1103 ret = -EBUSY;
1104 dev_err(dwc->dev, "%s: %p request already in queue",
1105 dep->name, req);
1106 return ret;
1107 }
1108
Felipe Balbi72246da2011-08-19 18:10:58 +03001109 req->request.actual = 0;
1110 req->request.status = -EINPROGRESS;
1111 req->direction = dep->direction;
1112 req->epnum = dep->number;
1113
1114 /*
1115 * We only add to our list of requests now and
1116 * start consuming the list once we get XferNotReady
1117 * IRQ.
1118 *
1119 * That way, we avoid doing anything that we don't need
1120 * to do now and defer it until the point we receive a
1121 * particular token from the Host side.
1122 *
1123 * This will also avoid Host cancelling URBs due to too
Paul Zimmerman1d046792012-02-15 18:56:56 -08001124 * many NAKs.
Felipe Balbi72246da2011-08-19 18:10:58 +03001125 */
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02001126 ret = usb_gadget_map_request(&dwc->gadget, &req->request,
1127 dep->direction);
1128 if (ret)
1129 return ret;
1130
Felipe Balbi72246da2011-08-19 18:10:58 +03001131 list_add_tail(&req->list, &dep->request_list);
1132
1133 /*
Felipe Balbi46485a02012-06-06 12:00:50 +03001134 * There are a few special cases:
Felipe Balbi72246da2011-08-19 18:10:58 +03001135 *
Paul Zimmermanf39a37f2012-03-29 18:16:54 +00001136 * 1. XferNotReady with empty list of requests. We need to kick the
1137 * transfer here in that situation, otherwise we will be NAKing
1138 * forever. If we get XferNotReady before gadget driver has a
1139 * chance to queue a request, we will ACK the IRQ but won't be
1140 * able to receive the data until the next request is queued.
1141 * The following code is handling exactly that.
1142 *
Felipe Balbi72246da2011-08-19 18:10:58 +03001143 */
1144 if (dep->flags & DWC3_EP_PENDING_REQUEST) {
Pratyush Anandac417602012-08-30 12:21:43 +05301145 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001146
Pratyush Anandac417602012-08-30 12:21:43 +05301147 /*
1148 * If xfernotready is already elapsed and it is a case
1149 * of isoc transfer, then issue END TRANSFER, so that
1150 * you can receive xfernotready again and can have
1151 * notion of current microframe.
1152 */
1153 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Vijayavardhan Vennapusa91ba6532013-01-30 17:35:45 +05301154 /* If xfernotready event is recieved before issuing
1155 * START TRANSFER command, don't issue END TRANSFER.
1156 * Rather start queueing the requests by issuing START
1157 * TRANSFER command.
1158 */
1159 if (list_empty(&dep->req_queued) && dep->resource_index)
Pratyush Anand18bbcb02013-01-14 15:59:34 +05301160 dwc3_stop_active_transfer(dwc, dep->number);
Vijayavardhan Vennapusa91ba6532013-01-30 17:35:45 +05301161 else
1162 __dwc3_gadget_start_isoc(dwc, dep,
1163 dep->current_uf);
1164 dep->flags &= ~DWC3_EP_PENDING_REQUEST;
Pratyush Anandac417602012-08-30 12:21:43 +05301165 return 0;
1166 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001167
Felipe Balbi46485a02012-06-06 12:00:50 +03001168 ret = __dwc3_gadget_kick_transfer(dep, 0, true);
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +05301169 if (ret && ret != -EBUSY) {
1170 dbg_event(dep->number, "QUEUE", ret);
Felipe Balbi72246da2011-08-19 18:10:58 +03001171 dev_dbg(dwc->dev, "%s: failed to kick transfers\n",
1172 dep->name);
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +05301173 }
Felipe Balbi5d409eb2012-05-22 10:24:11 +03001174 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001175
Felipe Balbi46485a02012-06-06 12:00:50 +03001176 /*
1177 * 2. XferInProgress on Isoc EP with an active transfer. We need to
1178 * kick the transfer here after queuing a request, otherwise the
1179 * core may not see the modified TRB(s).
1180 */
1181 if (usb_endpoint_xfer_isoc(dep->endpoint.desc) &&
Pratyush Anand053d3e52012-08-07 16:54:18 +05301182 (dep->flags & DWC3_EP_BUSY) &&
1183 !(dep->flags & DWC3_EP_MISSED_ISOC)) {
Felipe Balbi4959cfc2012-06-06 12:04:13 +03001184 WARN_ON_ONCE(!dep->resource_index);
1185 ret = __dwc3_gadget_kick_transfer(dep, dep->resource_index,
Felipe Balbi46485a02012-06-06 12:00:50 +03001186 false);
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +05301187 if (ret && ret != -EBUSY) {
1188 dbg_event(dep->number, "QUEUE", ret);
Felipe Balbi46485a02012-06-06 12:00:50 +03001189 dev_dbg(dwc->dev, "%s: failed to kick transfers\n",
1190 dep->name);
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +05301191 }
Felipe Balbi46485a02012-06-06 12:00:50 +03001192 }
1193
Felipe Balbi72246da2011-08-19 18:10:58 +03001194 return 0;
1195}
1196
1197static int dwc3_gadget_ep_queue(struct usb_ep *ep, struct usb_request *request,
1198 gfp_t gfp_flags)
1199{
1200 struct dwc3_request *req = to_dwc3_request(request);
1201 struct dwc3_ep *dep = to_dwc3_ep(ep);
1202 struct dwc3 *dwc = dep->dwc;
1203
1204 unsigned long flags;
1205
1206 int ret;
1207
Manu Gautam22f93042013-02-20 15:12:02 +05301208 spin_lock_irqsave(&dwc->lock, flags);
1209
Ido Shayevitz57cdac12012-03-12 20:25:24 +02001210 if (!dep->endpoint.desc) {
Manu Gautam22f93042013-02-20 15:12:02 +05301211 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001212 dev_dbg(dwc->dev, "trying to queue request %p to disabled %s\n",
1213 request, ep->name);
1214 return -ESHUTDOWN;
1215 }
1216
1217 dev_vdbg(dwc->dev, "queing request %p to %s length %d\n",
1218 request, ep->name, request->length);
1219
Manu Gautam1c4dbcb2012-10-05 13:16:00 +05301220 WARN(!dep->direction && (request->length % ep->desc->wMaxPacketSize),
1221 "trying to queue unaligned request (%d)\n", request->length);
1222
Felipe Balbi72246da2011-08-19 18:10:58 +03001223 ret = __dwc3_gadget_ep_queue(dep, req);
1224 spin_unlock_irqrestore(&dwc->lock, flags);
1225
1226 return ret;
1227}
1228
1229static int dwc3_gadget_ep_dequeue(struct usb_ep *ep,
1230 struct usb_request *request)
1231{
1232 struct dwc3_request *req = to_dwc3_request(request);
1233 struct dwc3_request *r = NULL;
1234
1235 struct dwc3_ep *dep = to_dwc3_ep(ep);
1236 struct dwc3 *dwc = dep->dwc;
1237
1238 unsigned long flags;
1239 int ret = 0;
1240
1241 spin_lock_irqsave(&dwc->lock, flags);
1242
1243 list_for_each_entry(r, &dep->request_list, list) {
1244 if (r == req)
1245 break;
1246 }
1247
1248 if (r != req) {
1249 list_for_each_entry(r, &dep->req_queued, list) {
1250 if (r == req)
1251 break;
1252 }
1253 if (r == req) {
1254 /* wait until it is processed */
1255 dwc3_stop_active_transfer(dwc, dep->number);
Pratyush Anandeaec3e92012-06-15 11:54:00 +05301256 goto out1;
Felipe Balbi72246da2011-08-19 18:10:58 +03001257 }
1258 dev_err(dwc->dev, "request %p was not queued to %s\n",
1259 request, ep->name);
1260 ret = -EINVAL;
1261 goto out0;
1262 }
1263
Pratyush Anandeaec3e92012-06-15 11:54:00 +05301264out1:
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +05301265 dbg_event(dep->number, "DEQUEUE", 0);
Felipe Balbi72246da2011-08-19 18:10:58 +03001266 /* giveback the request */
1267 dwc3_gadget_giveback(dep, req, -ECONNRESET);
1268
1269out0:
1270 spin_unlock_irqrestore(&dwc->lock, flags);
1271
1272 return ret;
1273}
1274
1275int __dwc3_gadget_ep_set_halt(struct dwc3_ep *dep, int value)
1276{
1277 struct dwc3_gadget_ep_cmd_params params;
1278 struct dwc3 *dwc = dep->dwc;
1279 int ret;
1280
1281 memset(&params, 0x00, sizeof(params));
1282
1283 if (value) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001284 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number,
1285 DWC3_DEPCMD_SETSTALL, &params);
1286 if (ret)
1287 dev_err(dwc->dev, "failed to %s STALL on %s\n",
1288 value ? "set" : "clear",
1289 dep->name);
1290 else
1291 dep->flags |= DWC3_EP_STALL;
1292 } else {
1293 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number,
1294 DWC3_DEPCMD_CLEARSTALL, &params);
1295 if (ret)
1296 dev_err(dwc->dev, "failed to %s STALL on %s\n",
1297 value ? "set" : "clear",
1298 dep->name);
1299 else
Vijayavardhan Vennapusa6008e262012-10-19 15:57:56 +05301300 dep->flags &= ~(DWC3_EP_STALL | DWC3_EP_WEDGE);
Felipe Balbi72246da2011-08-19 18:10:58 +03001301 }
Paul Zimmerman52754552011-09-30 10:58:44 +03001302
Felipe Balbi72246da2011-08-19 18:10:58 +03001303 return ret;
1304}
1305
1306static int dwc3_gadget_ep_set_halt(struct usb_ep *ep, int value)
1307{
1308 struct dwc3_ep *dep = to_dwc3_ep(ep);
1309 struct dwc3 *dwc = dep->dwc;
1310
1311 unsigned long flags;
1312
1313 int ret;
1314
1315 spin_lock_irqsave(&dwc->lock, flags);
1316
Ido Shayevitz57cdac12012-03-12 20:25:24 +02001317 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001318 dev_err(dwc->dev, "%s is of Isochronous type\n", dep->name);
1319 ret = -EINVAL;
1320 goto out;
1321 }
1322
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +05301323 dbg_event(dep->number, "HALT", value);
Felipe Balbi72246da2011-08-19 18:10:58 +03001324 ret = __dwc3_gadget_ep_set_halt(dep, value);
1325out:
1326 spin_unlock_irqrestore(&dwc->lock, flags);
1327
1328 return ret;
1329}
1330
1331static int dwc3_gadget_ep_set_wedge(struct usb_ep *ep)
1332{
1333 struct dwc3_ep *dep = to_dwc3_ep(ep);
Paul Zimmerman249a4562012-02-24 17:32:16 -08001334 struct dwc3 *dwc = dep->dwc;
1335 unsigned long flags;
Felipe Balbi72246da2011-08-19 18:10:58 +03001336
Paul Zimmerman249a4562012-02-24 17:32:16 -08001337 spin_lock_irqsave(&dwc->lock, flags);
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +05301338 dbg_event(dep->number, "WEDGE", 0);
Felipe Balbi72246da2011-08-19 18:10:58 +03001339 dep->flags |= DWC3_EP_WEDGE;
Paul Zimmerman249a4562012-02-24 17:32:16 -08001340 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001341
Pratyush Anandeb840752012-06-25 22:40:43 +05301342 if (dep->number == 0 || dep->number == 1)
1343 return dwc3_gadget_ep0_set_halt(ep, 1);
1344 else
1345 return dwc3_gadget_ep_set_halt(ep, 1);
Felipe Balbi72246da2011-08-19 18:10:58 +03001346}
1347
1348/* -------------------------------------------------------------------------- */
1349
1350static struct usb_endpoint_descriptor dwc3_gadget_ep0_desc = {
1351 .bLength = USB_DT_ENDPOINT_SIZE,
1352 .bDescriptorType = USB_DT_ENDPOINT,
1353 .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
1354};
1355
1356static const struct usb_ep_ops dwc3_gadget_ep0_ops = {
1357 .enable = dwc3_gadget_ep0_enable,
1358 .disable = dwc3_gadget_ep0_disable,
1359 .alloc_request = dwc3_gadget_ep_alloc_request,
1360 .free_request = dwc3_gadget_ep_free_request,
1361 .queue = dwc3_gadget_ep0_queue,
1362 .dequeue = dwc3_gadget_ep_dequeue,
Pratyush Anandeb840752012-06-25 22:40:43 +05301363 .set_halt = dwc3_gadget_ep0_set_halt,
Felipe Balbi72246da2011-08-19 18:10:58 +03001364 .set_wedge = dwc3_gadget_ep_set_wedge,
1365};
1366
1367static const struct usb_ep_ops dwc3_gadget_ep_ops = {
1368 .enable = dwc3_gadget_ep_enable,
1369 .disable = dwc3_gadget_ep_disable,
1370 .alloc_request = dwc3_gadget_ep_alloc_request,
1371 .free_request = dwc3_gadget_ep_free_request,
1372 .queue = dwc3_gadget_ep_queue,
1373 .dequeue = dwc3_gadget_ep_dequeue,
1374 .set_halt = dwc3_gadget_ep_set_halt,
1375 .set_wedge = dwc3_gadget_ep_set_wedge,
1376};
1377
1378/* -------------------------------------------------------------------------- */
1379
1380static int dwc3_gadget_get_frame(struct usb_gadget *g)
1381{
1382 struct dwc3 *dwc = gadget_to_dwc(g);
1383 u32 reg;
1384
1385 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1386 return DWC3_DSTS_SOFFN(reg);
1387}
1388
1389static int dwc3_gadget_wakeup(struct usb_gadget *g)
1390{
1391 struct dwc3 *dwc = gadget_to_dwc(g);
1392
1393 unsigned long timeout;
1394 unsigned long flags;
1395
1396 u32 reg;
1397
1398 int ret = 0;
1399
1400 u8 link_state;
1401 u8 speed;
1402
1403 spin_lock_irqsave(&dwc->lock, flags);
1404
1405 /*
1406 * According to the Databook Remote wakeup request should
1407 * be issued only when the device is in early suspend state.
1408 *
1409 * We can check that via USB Link State bits in DSTS register.
1410 */
1411 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1412
1413 speed = reg & DWC3_DSTS_CONNECTSPD;
1414 if (speed == DWC3_DSTS_SUPERSPEED) {
1415 dev_dbg(dwc->dev, "no wakeup on SuperSpeed\n");
1416 ret = -EINVAL;
1417 goto out;
1418 }
1419
1420 link_state = DWC3_DSTS_USBLNKST(reg);
1421
1422 switch (link_state) {
1423 case DWC3_LINK_STATE_RX_DET: /* in HS, means Early Suspend */
1424 case DWC3_LINK_STATE_U3: /* in HS, means SUSPEND */
1425 break;
1426 default:
1427 dev_dbg(dwc->dev, "can't wakeup from link state %d\n",
1428 link_state);
1429 ret = -EINVAL;
1430 goto out;
1431 }
1432
Felipe Balbi8598bde2012-01-02 18:55:57 +02001433 ret = dwc3_gadget_set_link_state(dwc, DWC3_LINK_STATE_RECOV);
1434 if (ret < 0) {
1435 dev_err(dwc->dev, "failed to put link in Recovery\n");
1436 goto out;
1437 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001438
Paul Zimmerman88df4272012-04-27 13:10:52 +03001439 /* Recent versions do this automatically */
1440 if (dwc->revision < DWC3_REVISION_194A) {
1441 /* write zeroes to Link Change Request */
Felipe Balbib4d04352012-05-24 10:27:56 +03001442 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
Paul Zimmerman88df4272012-04-27 13:10:52 +03001443 reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK;
1444 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1445 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001446
Paul Zimmerman1d046792012-02-15 18:56:56 -08001447 /* poll until Link State changes to ON */
Felipe Balbi72246da2011-08-19 18:10:58 +03001448 timeout = jiffies + msecs_to_jiffies(100);
1449
Paul Zimmerman1d046792012-02-15 18:56:56 -08001450 while (!time_after(jiffies, timeout)) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001451 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1452
1453 /* in HS, means ON */
1454 if (DWC3_DSTS_USBLNKST(reg) == DWC3_LINK_STATE_U0)
1455 break;
1456 }
1457
1458 if (DWC3_DSTS_USBLNKST(reg) != DWC3_LINK_STATE_U0) {
1459 dev_err(dwc->dev, "failed to send remote wakeup\n");
1460 ret = -EINVAL;
1461 }
1462
1463out:
1464 spin_unlock_irqrestore(&dwc->lock, flags);
1465
1466 return ret;
1467}
1468
1469static int dwc3_gadget_set_selfpowered(struct usb_gadget *g,
1470 int is_selfpowered)
1471{
1472 struct dwc3 *dwc = gadget_to_dwc(g);
Paul Zimmerman249a4562012-02-24 17:32:16 -08001473 unsigned long flags;
Felipe Balbi72246da2011-08-19 18:10:58 +03001474
Paul Zimmerman249a4562012-02-24 17:32:16 -08001475 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001476 dwc->is_selfpowered = !!is_selfpowered;
Paul Zimmerman249a4562012-02-24 17:32:16 -08001477 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001478
1479 return 0;
1480}
1481
Pratyush Anand77473f72012-07-02 10:21:55 +05301482static int dwc3_gadget_run_stop(struct dwc3 *dwc, int is_on)
Felipe Balbi72246da2011-08-19 18:10:58 +03001483{
1484 u32 reg;
Sebastian Andrzej Siewior61d58242011-08-29 16:46:38 +02001485 u32 timeout = 500;
Felipe Balbi72246da2011-08-19 18:10:58 +03001486
1487 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
Felipe Balbi8db7ed12012-01-18 18:32:29 +02001488 if (is_on) {
Paul Zimmerman88df4272012-04-27 13:10:52 +03001489 if (dwc->revision <= DWC3_REVISION_187A) {
1490 reg &= ~DWC3_DCTL_TRGTULST_MASK;
1491 reg |= DWC3_DCTL_TRGTULST_RX_DET;
1492 }
1493
1494 if (dwc->revision >= DWC3_REVISION_194A)
1495 reg &= ~DWC3_DCTL_KEEP_CONNECT;
1496 reg |= DWC3_DCTL_RUN_STOP;
Felipe Balbi8db7ed12012-01-18 18:32:29 +02001497 } else {
Felipe Balbi72246da2011-08-19 18:10:58 +03001498 reg &= ~DWC3_DCTL_RUN_STOP;
Felipe Balbi8db7ed12012-01-18 18:32:29 +02001499 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001500
1501 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1502
1503 do {
1504 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1505 if (is_on) {
1506 if (!(reg & DWC3_DSTS_DEVCTRLHLT))
1507 break;
1508 } else {
1509 if (reg & DWC3_DSTS_DEVCTRLHLT)
1510 break;
1511 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001512 timeout--;
1513 if (!timeout)
Pratyush Anand77473f72012-07-02 10:21:55 +05301514 return -ETIMEDOUT;
Sebastian Andrzej Siewior61d58242011-08-29 16:46:38 +02001515 udelay(1);
Felipe Balbi72246da2011-08-19 18:10:58 +03001516 } while (1);
1517
1518 dev_vdbg(dwc->dev, "gadget %s data soft-%s\n",
1519 dwc->gadget_driver
1520 ? dwc->gadget_driver->function : "no-function",
1521 is_on ? "connect" : "disconnect");
Pratyush Anand77473f72012-07-02 10:21:55 +05301522
1523 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +03001524}
1525
Vijayavardhan Vennapusa908f1ed2012-10-19 19:51:48 +05301526static int dwc3_gadget_vbus_draw(struct usb_gadget *g, unsigned mA)
1527{
1528 struct dwc3 *dwc = gadget_to_dwc(g);
1529 struct dwc3_otg *dotg = dwc->dotg;
1530
1531 if (dotg && dotg->otg.phy)
1532 return usb_phy_set_power(dotg->otg.phy, mA);
1533
1534 return -ENOTSUPP;
1535}
1536
Felipe Balbi72246da2011-08-19 18:10:58 +03001537static int dwc3_gadget_pullup(struct usb_gadget *g, int is_on)
1538{
1539 struct dwc3 *dwc = gadget_to_dwc(g);
1540 unsigned long flags;
Pratyush Anand77473f72012-07-02 10:21:55 +05301541 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001542
1543 is_on = !!is_on;
1544
1545 spin_lock_irqsave(&dwc->lock, flags);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +02001546
1547 dwc->softconnect = is_on;
1548
1549 if ((dwc->dotg && !dwc->vbus_active) ||
1550 !dwc->gadget_driver) {
1551
1552 spin_unlock_irqrestore(&dwc->lock, flags);
1553
1554 /*
1555 * Need to wait for vbus_session(on) from otg driver or to
1556 * the udc_start.
1557 */
1558 return 0;
1559 }
1560
Pratyush Anand77473f72012-07-02 10:21:55 +05301561 ret = dwc3_gadget_run_stop(dwc, is_on);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +02001562
1563 spin_unlock_irqrestore(&dwc->lock, flags);
1564
Pratyush Anand77473f72012-07-02 10:21:55 +05301565 return ret;
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +02001566}
1567
1568static int dwc3_gadget_vbus_session(struct usb_gadget *_gadget, int is_active)
1569{
1570 struct dwc3 *dwc = gadget_to_dwc(_gadget);
1571 unsigned long flags;
Vijayavardhan Vennapusa8ec31d22012-10-23 08:44:48 +05301572 int ret = 0;
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +02001573
1574 if (!dwc->dotg)
1575 return -EPERM;
1576
1577 is_active = !!is_active;
1578
1579 spin_lock_irqsave(&dwc->lock, flags);
1580
1581 /* Mark that the vbus was powered */
1582 dwc->vbus_active = is_active;
1583
1584 /*
1585 * Check if upper level usb_gadget_driver was already registerd with
1586 * this udc controller driver (if dwc3_gadget_start was called)
1587 */
1588 if (dwc->gadget_driver && dwc->softconnect) {
1589 if (dwc->vbus_active) {
1590 /*
1591 * Both vbus was activated by otg and pullup was
1592 * signaled by the gadget driver.
1593 */
Pratyush Anand77473f72012-07-02 10:21:55 +05301594 ret = dwc3_gadget_run_stop(dwc, 1);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +02001595 } else {
Pratyush Anand77473f72012-07-02 10:21:55 +05301596 ret = dwc3_gadget_run_stop(dwc, 0);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +02001597 }
Vijayavardhan Vennapusab666fb82012-11-08 16:02:51 +05301598 } else if (dwc->gadget_driver && !dwc->softconnect &&
1599 !dwc->vbus_active) {
1600 if (dwc->gadget_driver->disconnect) {
1601 spin_unlock_irqrestore(&dwc->lock, flags);
1602 dwc->gadget_driver->disconnect(&dwc->gadget);
1603 return 0;
1604 }
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +02001605 }
1606
Felipe Balbi72246da2011-08-19 18:10:58 +03001607 spin_unlock_irqrestore(&dwc->lock, flags);
1608
Pratyush Anand77473f72012-07-02 10:21:55 +05301609 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001610}
1611
Manu Gautamf1fceddf2012-10-12 14:02:50 +05301612/* Required gadget re-initialization before switching to gadget in OTG mode */
1613void dwc3_gadget_restart(struct dwc3 *dwc)
1614{
1615 struct dwc3_ep *dep;
1616 int ret = 0;
Vijayavardhan Vennapusab7434562012-12-12 16:48:49 +05301617 u32 reg;
Manu Gautamf1fceddf2012-10-12 14:02:50 +05301618
Vijayavardhan Vennapusab7434562012-12-12 16:48:49 +05301619 /* Enable all but Start and End of Frame IRQs */
1620 reg = (DWC3_DEVTEN_EVNTOVERFLOWEN |
1621 DWC3_DEVTEN_CMDCMPLTEN |
1622 DWC3_DEVTEN_ERRTICERREN |
1623 DWC3_DEVTEN_WKUPEVTEN |
1624 DWC3_DEVTEN_ULSTCNGEN |
1625 DWC3_DEVTEN_CONNECTDONEEN |
1626 DWC3_DEVTEN_USBRSTEN |
1627 DWC3_DEVTEN_DISCONNEVTEN);
1628 dwc3_writel(dwc->regs, DWC3_DEVTEN, reg);
1629
1630 /* Enable USB2 LPM and automatic phy suspend only on recent versions */
1631 if (dwc->revision >= DWC3_REVISION_194A) {
1632 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
1633 reg |= DWC3_DCFG_LPM_CAP;
1634 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
1635
1636 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
1637 reg &= ~(DWC3_DCTL_HIRD_THRES_MASK | DWC3_DCTL_L1_HIBER_EN);
1638
1639 /* TODO: This should be configurable */
1640 reg |= DWC3_DCTL_HIRD_THRES(28);
1641
1642 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1643 }
1644
1645 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
1646 reg &= ~(DWC3_DCFG_SPEED_MASK);
1647
1648 /**
1649 * WORKAROUND: DWC3 revision < 2.20a have an issue
1650 * which would cause metastability state on Run/Stop
1651 * bit if we try to force the IP to USB2-only mode.
1652 *
1653 * Because of that, we cannot configure the IP to any
1654 * speed other than the SuperSpeed
1655 *
1656 * Refers to:
1657 *
1658 * STAR#9000525659: Clock Domain Crossing on DCTL in
1659 * USB 2.0 Mode
1660 */
1661 if (dwc->revision < DWC3_REVISION_220A)
1662 reg |= DWC3_DCFG_SUPERSPEED;
1663 else
1664 reg |= dwc->maximum_speed;
1665 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
1666
1667 dwc->start_config_issued = false;
1668
1669 /* Start with SuperSpeed Default */
1670 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
Manu Gautamf1fceddf2012-10-12 14:02:50 +05301671
1672 dwc->delayed_status = false;
Vijayavardhan Vennapusab7434562012-12-12 16:48:49 +05301673 /* reinitialize physical ep0-1 */
Manu Gautamf1fceddf2012-10-12 14:02:50 +05301674 dep = dwc->eps[0];
1675 dep->flags = 0;
1676 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, false);
1677 if (ret) {
1678 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
1679 return;
1680 }
1681
1682 dep = dwc->eps[1];
1683 dep->flags = 0;
1684 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, false);
1685 if (ret) {
1686 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
1687 return;
1688 }
1689
1690 /* begin to receive SETUP packets */
1691 dwc->ep0state = EP0_SETUP_PHASE;
1692 dwc3_ep0_out_start(dwc);
1693}
1694
Felipe Balbi72246da2011-08-19 18:10:58 +03001695static int dwc3_gadget_start(struct usb_gadget *g,
1696 struct usb_gadget_driver *driver)
1697{
1698 struct dwc3 *dwc = gadget_to_dwc(g);
1699 struct dwc3_ep *dep;
1700 unsigned long flags;
1701 int ret = 0;
1702 u32 reg;
1703
1704 spin_lock_irqsave(&dwc->lock, flags);
1705
1706 if (dwc->gadget_driver) {
1707 dev_err(dwc->dev, "%s is already bound to %s\n",
1708 dwc->gadget.name,
1709 dwc->gadget_driver->driver.name);
1710 ret = -EBUSY;
1711 goto err0;
1712 }
1713
1714 dwc->gadget_driver = driver;
1715 dwc->gadget.dev.driver = &driver->driver;
1716
Felipe Balbi72246da2011-08-19 18:10:58 +03001717 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
1718 reg &= ~(DWC3_DCFG_SPEED_MASK);
Felipe Balbi38d2c6c2012-03-23 12:20:31 +02001719
1720 /**
1721 * WORKAROUND: DWC3 revision < 2.20a have an issue
1722 * which would cause metastability state on Run/Stop
1723 * bit if we try to force the IP to USB2-only mode.
1724 *
1725 * Because of that, we cannot configure the IP to any
1726 * speed other than the SuperSpeed
1727 *
1728 * Refers to:
1729 *
1730 * STAR#9000525659: Clock Domain Crossing on DCTL in
1731 * USB 2.0 Mode
1732 */
1733 if (dwc->revision < DWC3_REVISION_220A)
1734 reg |= DWC3_DCFG_SUPERSPEED;
1735 else
1736 reg |= dwc->maximum_speed;
Felipe Balbi72246da2011-08-19 18:10:58 +03001737 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
1738
Paul Zimmermanb23c8432011-09-30 10:58:42 +03001739 dwc->start_config_issued = false;
1740
Felipe Balbi72246da2011-08-19 18:10:58 +03001741 /* Start with SuperSpeed Default */
1742 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
1743
1744 dep = dwc->eps[0];
Felipe Balbi07e0ee82012-07-16 14:08:16 +03001745 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, false);
Felipe Balbi72246da2011-08-19 18:10:58 +03001746 if (ret) {
1747 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
1748 goto err0;
1749 }
1750
1751 dep = dwc->eps[1];
Felipe Balbi07e0ee82012-07-16 14:08:16 +03001752 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, false);
Felipe Balbi72246da2011-08-19 18:10:58 +03001753 if (ret) {
1754 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
1755 goto err1;
1756 }
1757
1758 /* begin to receive SETUP packets */
Felipe Balbic7fcdeb2011-08-27 22:28:36 +03001759 dwc->ep0state = EP0_SETUP_PHASE;
Felipe Balbi72246da2011-08-19 18:10:58 +03001760 dwc3_ep0_out_start(dwc);
1761
1762 spin_unlock_irqrestore(&dwc->lock, flags);
1763
1764 return 0;
1765
1766err1:
1767 __dwc3_gadget_ep_disable(dwc->eps[0]);
1768
1769err0:
1770 spin_unlock_irqrestore(&dwc->lock, flags);
1771
1772 return ret;
1773}
1774
1775static int dwc3_gadget_stop(struct usb_gadget *g,
1776 struct usb_gadget_driver *driver)
1777{
1778 struct dwc3 *dwc = gadget_to_dwc(g);
1779 unsigned long flags;
1780
1781 spin_lock_irqsave(&dwc->lock, flags);
1782
1783 __dwc3_gadget_ep_disable(dwc->eps[0]);
1784 __dwc3_gadget_ep_disable(dwc->eps[1]);
1785
1786 dwc->gadget_driver = NULL;
1787 dwc->gadget.dev.driver = NULL;
1788
1789 spin_unlock_irqrestore(&dwc->lock, flags);
1790
1791 return 0;
1792}
Paul Zimmerman88df4272012-04-27 13:10:52 +03001793
Felipe Balbi72246da2011-08-19 18:10:58 +03001794static const struct usb_gadget_ops dwc3_gadget_ops = {
1795 .get_frame = dwc3_gadget_get_frame,
1796 .wakeup = dwc3_gadget_wakeup,
1797 .set_selfpowered = dwc3_gadget_set_selfpowered,
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +02001798 .vbus_session = dwc3_gadget_vbus_session,
Vijayavardhan Vennapusa908f1ed2012-10-19 19:51:48 +05301799 .vbus_draw = dwc3_gadget_vbus_draw,
Felipe Balbi72246da2011-08-19 18:10:58 +03001800 .pullup = dwc3_gadget_pullup,
1801 .udc_start = dwc3_gadget_start,
1802 .udc_stop = dwc3_gadget_stop,
1803};
1804
1805/* -------------------------------------------------------------------------- */
1806
1807static int __devinit dwc3_gadget_init_endpoints(struct dwc3 *dwc)
1808{
1809 struct dwc3_ep *dep;
1810 u8 epnum;
1811
1812 INIT_LIST_HEAD(&dwc->gadget.ep_list);
1813
1814 for (epnum = 0; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
1815 dep = kzalloc(sizeof(*dep), GFP_KERNEL);
1816 if (!dep) {
1817 dev_err(dwc->dev, "can't allocate endpoint %d\n",
1818 epnum);
1819 return -ENOMEM;
1820 }
1821
1822 dep->dwc = dwc;
1823 dep->number = epnum;
1824 dwc->eps[epnum] = dep;
1825
1826 snprintf(dep->name, sizeof(dep->name), "ep%d%s", epnum >> 1,
1827 (epnum & 1) ? "in" : "out");
1828 dep->endpoint.name = dep->name;
1829 dep->direction = (epnum & 1);
1830
1831 if (epnum == 0 || epnum == 1) {
1832 dep->endpoint.maxpacket = 512;
1833 dep->endpoint.ops = &dwc3_gadget_ep0_ops;
1834 if (!epnum)
1835 dwc->gadget.ep0 = &dep->endpoint;
1836 } else {
1837 int ret;
1838
1839 dep->endpoint.maxpacket = 1024;
Sebastian Andrzej Siewior12d36c12011-11-03 20:27:50 +01001840 dep->endpoint.max_streams = 15;
Felipe Balbi72246da2011-08-19 18:10:58 +03001841 dep->endpoint.ops = &dwc3_gadget_ep_ops;
1842 list_add_tail(&dep->endpoint.ep_list,
1843 &dwc->gadget.ep_list);
1844
1845 ret = dwc3_alloc_trb_pool(dep);
Felipe Balbi25b8ff62011-11-04 12:32:47 +02001846 if (ret)
Felipe Balbi72246da2011-08-19 18:10:58 +03001847 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001848 }
Felipe Balbi25b8ff62011-11-04 12:32:47 +02001849
Felipe Balbi72246da2011-08-19 18:10:58 +03001850 INIT_LIST_HEAD(&dep->request_list);
1851 INIT_LIST_HEAD(&dep->req_queued);
1852 }
1853
1854 return 0;
1855}
1856
1857static void dwc3_gadget_free_endpoints(struct dwc3 *dwc)
1858{
1859 struct dwc3_ep *dep;
1860 u8 epnum;
1861
1862 for (epnum = 0; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
1863 dep = dwc->eps[epnum];
1864 dwc3_free_trb_pool(dep);
1865
1866 if (epnum != 0 && epnum != 1)
1867 list_del(&dep->endpoint.ep_list);
1868
1869 kfree(dep);
1870 }
1871}
1872
1873static void dwc3_gadget_release(struct device *dev)
1874{
1875 dev_dbg(dev, "%s\n", __func__);
1876}
1877
1878/* -------------------------------------------------------------------------- */
1879static int dwc3_cleanup_done_reqs(struct dwc3 *dwc, struct dwc3_ep *dep,
1880 const struct dwc3_event_depevt *event, int status)
1881{
1882 struct dwc3_request *req;
Felipe Balbif6bafc62012-02-06 11:04:53 +02001883 struct dwc3_trb *trb;
Felipe Balbi72246da2011-08-19 18:10:58 +03001884 unsigned int count;
1885 unsigned int s_pkt = 0;
Pratyush Anand73939b02012-05-25 18:54:56 +05301886 unsigned int trb_status;
Felipe Balbi72246da2011-08-19 18:10:58 +03001887
1888 do {
1889 req = next_request(&dep->req_queued);
Sebastian Andrzej Siewiord39ee7b2011-11-03 10:32:20 +01001890 if (!req) {
1891 WARN_ON_ONCE(1);
1892 return 1;
1893 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001894
Felipe Balbif6bafc62012-02-06 11:04:53 +02001895 trb = req->trb;
Felipe Balbi72246da2011-08-19 18:10:58 +03001896
Felipe Balbif6bafc62012-02-06 11:04:53 +02001897 if ((trb->ctrl & DWC3_TRB_CTRL_HWO) && status != -ESHUTDOWN)
Sebastian Andrzej Siewior0d2f4752011-08-19 19:59:12 +02001898 /*
1899 * We continue despite the error. There is not much we
Paul Zimmerman1d046792012-02-15 18:56:56 -08001900 * can do. If we don't clean it up we loop forever. If
1901 * we skip the TRB then it gets overwritten after a
1902 * while since we use them in a ring buffer. A BUG()
1903 * would help. Lets hope that if this occurs, someone
Sebastian Andrzej Siewior0d2f4752011-08-19 19:59:12 +02001904 * fixes the root cause instead of looking away :)
1905 */
Felipe Balbi72246da2011-08-19 18:10:58 +03001906 dev_err(dwc->dev, "%s's TRB (%p) still owned by HW\n",
1907 dep->name, req->trb);
Felipe Balbif6bafc62012-02-06 11:04:53 +02001908 count = trb->size & DWC3_TRB_SIZE_MASK;
Felipe Balbi72246da2011-08-19 18:10:58 +03001909
1910 if (dep->direction) {
1911 if (count) {
Pratyush Anand73939b02012-05-25 18:54:56 +05301912 trb_status = DWC3_TRB_SIZE_TRBSTS(trb->size);
1913 if (trb_status == DWC3_TRBSTS_MISSED_ISOC) {
1914 dev_dbg(dwc->dev, "incomplete IN transfer %s\n",
1915 dep->name);
Pratyush Anand921b0b82013-01-14 15:59:32 +05301916 /*
1917 * If missed isoc occurred and there is
1918 * no request queued then issue END
1919 * TRANSFER, so that core generates
1920 * next xfernotready and we will issue
1921 * a fresh START TRANSFER.
1922 * If there are still queued request
1923 * then wait, do not issue either END
1924 * or UPDATE TRANSFER, just attach next
1925 * request in request_list during
1926 * giveback.If any future queued request
1927 * is successfully transferred then we
1928 * will issue UPDATE TRANSFER for all
1929 * request in the request_list.
1930 */
Pratyush Anand73939b02012-05-25 18:54:56 +05301931 dep->flags |= DWC3_EP_MISSED_ISOC;
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +05301932 dbg_event(dep->number, "MISSED ISOC",
1933 status);
Pratyush Anand73939b02012-05-25 18:54:56 +05301934 } else {
1935 dev_err(dwc->dev, "incomplete IN transfer %s\n",
1936 dep->name);
1937 status = -ECONNRESET;
1938 }
Pratyush Anand921b0b82013-01-14 15:59:32 +05301939 } else {
1940 dep->flags &= ~DWC3_EP_MISSED_ISOC;
Felipe Balbi72246da2011-08-19 18:10:58 +03001941 }
1942 } else {
1943 if (count && (event->status & DEPEVT_STATUS_SHORT))
1944 s_pkt = 1;
1945 }
1946
1947 /*
1948 * We assume here we will always receive the entire data block
1949 * which we should receive. Meaning, if we program RX to
1950 * receive 4K but we receive only 2K, we assume that's all we
1951 * should receive and we simply bounce the request back to the
1952 * gadget driver for further processing.
1953 */
1954 req->request.actual += req->request.length - count;
1955 dwc3_gadget_giveback(dep, req, status);
1956 if (s_pkt)
1957 break;
Felipe Balbif6bafc62012-02-06 11:04:53 +02001958 if ((event->status & DEPEVT_STATUS_LST) &&
Pratyush Anand413dba62012-06-03 19:43:19 +05301959 (trb->ctrl & (DWC3_TRB_CTRL_LST |
1960 DWC3_TRB_CTRL_HWO)))
Felipe Balbi72246da2011-08-19 18:10:58 +03001961 break;
Felipe Balbif6bafc62012-02-06 11:04:53 +02001962 if ((event->status & DEPEVT_STATUS_IOC) &&
1963 (trb->ctrl & DWC3_TRB_CTRL_IOC))
Felipe Balbi72246da2011-08-19 18:10:58 +03001964 break;
1965 } while (1);
1966
Pratyush Anand18bbcb02013-01-14 15:59:34 +05301967 if (usb_endpoint_xfer_isoc(dep->endpoint.desc) &&
1968 list_empty(&dep->req_queued)) {
Vijayavardhan Vennapusa91ba6532013-01-30 17:35:45 +05301969 if (list_empty(&dep->request_list))
Pratyush Anand18bbcb02013-01-14 15:59:34 +05301970 /*
1971 * If there is no entry in request list then do
1972 * not issue END TRANSFER now. Just set PENDING
1973 * flag, so that END TRANSFER is issued when an
1974 * entry is added into request list.
1975 */
Vijayavardhan Vennapusa91ba6532013-01-30 17:35:45 +05301976 dep->flags |= DWC3_EP_PENDING_REQUEST;
1977 else
Pratyush Anand18bbcb02013-01-14 15:59:34 +05301978 dwc3_stop_active_transfer(dwc, dep->number);
Vijayavardhan Vennapusa91ba6532013-01-30 17:35:45 +05301979 dep->flags &= ~DWC3_EP_MISSED_ISOC;
Pratyush Anand921b0b82013-01-14 15:59:32 +05301980 return 1;
1981 }
1982
Felipe Balbif6bafc62012-02-06 11:04:53 +02001983 if ((event->status & DEPEVT_STATUS_IOC) &&
1984 (trb->ctrl & DWC3_TRB_CTRL_IOC))
Felipe Balbi72246da2011-08-19 18:10:58 +03001985 return 0;
1986 return 1;
1987}
1988
1989static void dwc3_endpoint_transfer_complete(struct dwc3 *dwc,
1990 struct dwc3_ep *dep, const struct dwc3_event_depevt *event,
1991 int start_new)
1992{
1993 unsigned status = 0;
1994 int clean_busy;
1995
1996 if (event->status & DEPEVT_STATUS_BUSERR)
1997 status = -ECONNRESET;
1998
Paul Zimmerman1d046792012-02-15 18:56:56 -08001999 clean_busy = dwc3_cleanup_done_reqs(dwc, dep, event, status);
Paul Zimmermanc2df85c2012-02-24 17:32:18 -08002000 if (clean_busy)
Felipe Balbi72246da2011-08-19 18:10:58 +03002001 dep->flags &= ~DWC3_EP_BUSY;
Felipe Balbifae2b902011-10-14 13:00:30 +03002002
2003 /*
2004 * WORKAROUND: This is the 2nd half of U1/U2 -> U0 workaround.
2005 * See dwc3_gadget_linksts_change_interrupt() for 1st half.
2006 */
2007 if (dwc->revision < DWC3_REVISION_183A) {
2008 u32 reg;
2009 int i;
2010
2011 for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) {
Moiz Sonasatheed03f12012-08-01 14:08:30 -05002012 dep = dwc->eps[i];
Felipe Balbifae2b902011-10-14 13:00:30 +03002013
2014 if (!(dep->flags & DWC3_EP_ENABLED))
2015 continue;
2016
2017 if (!list_empty(&dep->req_queued))
2018 return;
2019 }
2020
2021 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2022 reg |= dwc->u1u2;
2023 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2024
2025 dwc->u1u2 = 0;
2026 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002027}
2028
Felipe Balbi72246da2011-08-19 18:10:58 +03002029static void dwc3_endpoint_interrupt(struct dwc3 *dwc,
2030 const struct dwc3_event_depevt *event)
2031{
2032 struct dwc3_ep *dep;
2033 u8 epnum = event->endpoint_number;
2034
2035 dep = dwc->eps[epnum];
2036
Felipe Balbia09be0a2012-06-06 09:19:35 +03002037 if (!(dep->flags & DWC3_EP_ENABLED))
2038 return;
2039
Felipe Balbi72246da2011-08-19 18:10:58 +03002040 dev_vdbg(dwc->dev, "%s: %s\n", dep->name,
2041 dwc3_ep_event_string(event->endpoint_event));
2042
2043 if (epnum == 0 || epnum == 1) {
2044 dwc3_ep0_interrupt(dwc, event);
2045 return;
2046 }
2047
2048 switch (event->endpoint_event) {
2049 case DWC3_DEPEVT_XFERCOMPLETE:
Felipe Balbi4959cfc2012-06-06 12:04:13 +03002050 dep->resource_index = 0;
Paul Zimmermanc2df85c2012-02-24 17:32:18 -08002051
Ido Shayevitz57cdac12012-03-12 20:25:24 +02002052 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbi72246da2011-08-19 18:10:58 +03002053 dev_dbg(dwc->dev, "%s is an Isochronous endpoint\n",
2054 dep->name);
2055 return;
2056 }
2057
2058 dwc3_endpoint_transfer_complete(dwc, dep, event, 1);
2059 break;
2060 case DWC3_DEPEVT_XFERINPROGRESS:
Ido Shayevitz57cdac12012-03-12 20:25:24 +02002061 if (!usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbi72246da2011-08-19 18:10:58 +03002062 dev_dbg(dwc->dev, "%s is not an Isochronous endpoint\n",
2063 dep->name);
2064 return;
2065 }
2066
2067 dwc3_endpoint_transfer_complete(dwc, dep, event, 0);
2068 break;
2069 case DWC3_DEPEVT_XFERNOTREADY:
Ido Shayevitz57cdac12012-03-12 20:25:24 +02002070 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbi72246da2011-08-19 18:10:58 +03002071 dwc3_gadget_start_isoc(dwc, dep, event);
2072 } else {
2073 int ret;
2074
2075 dev_vdbg(dwc->dev, "%s: reason %s\n",
Felipe Balbi40aa41f2012-01-18 17:06:03 +02002076 dep->name, event->status &
2077 DEPEVT_STATUS_TRANSFER_ACTIVE
Felipe Balbi72246da2011-08-19 18:10:58 +03002078 ? "Transfer Active"
2079 : "Transfer Not Active");
2080
2081 ret = __dwc3_gadget_kick_transfer(dep, 0, 1);
2082 if (!ret || ret == -EBUSY)
2083 return;
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +05302084 else
2085 dbg_event(dep->number, "QUEUE", ret);
Felipe Balbi72246da2011-08-19 18:10:58 +03002086
2087 dev_dbg(dwc->dev, "%s: failed to kick transfers\n",
2088 dep->name);
2089 }
2090
2091 break;
Felipe Balbi879631a2011-09-30 10:58:47 +03002092 case DWC3_DEPEVT_STREAMEVT:
Ido Shayevitz57cdac12012-03-12 20:25:24 +02002093 if (!usb_endpoint_xfer_bulk(dep->endpoint.desc)) {
Felipe Balbi879631a2011-09-30 10:58:47 +03002094 dev_err(dwc->dev, "Stream event for non-Bulk %s\n",
2095 dep->name);
2096 return;
2097 }
2098
2099 switch (event->status) {
2100 case DEPEVT_STREAMEVT_FOUND:
2101 dev_vdbg(dwc->dev, "Stream %d found and started\n",
2102 event->parameters);
2103
2104 break;
2105 case DEPEVT_STREAMEVT_NOTFOUND:
2106 /* FALLTHROUGH */
2107 default:
2108 dev_dbg(dwc->dev, "Couldn't find suitable stream\n");
2109 }
2110 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03002111 case DWC3_DEPEVT_RXTXFIFOEVT:
2112 dev_dbg(dwc->dev, "%s FIFO Overrun\n", dep->name);
2113 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03002114 case DWC3_DEPEVT_EPCMDCMPLT:
Felipe Balbib129eb72012-02-17 12:10:04 +02002115 dev_vdbg(dwc->dev, "Endpoint Command Complete\n");
Felipe Balbi72246da2011-08-19 18:10:58 +03002116 break;
2117 }
2118}
2119
2120static void dwc3_disconnect_gadget(struct dwc3 *dwc)
2121{
2122 if (dwc->gadget_driver && dwc->gadget_driver->disconnect) {
2123 spin_unlock(&dwc->lock);
2124 dwc->gadget_driver->disconnect(&dwc->gadget);
2125 spin_lock(&dwc->lock);
2126 }
2127}
2128
2129static void dwc3_stop_active_transfer(struct dwc3 *dwc, u32 epnum)
2130{
2131 struct dwc3_ep *dep;
2132 struct dwc3_gadget_ep_cmd_params params;
2133 u32 cmd;
2134 int ret;
2135
2136 dep = dwc->eps[epnum];
2137
Felipe Balbi4959cfc2012-06-06 12:04:13 +03002138 if (!dep->resource_index)
Pratyush Anand6263ebe2012-06-23 02:23:08 +05302139 return;
2140
Pratyush Anande67fdeb2012-07-06 15:19:10 +05302141 /*
2142 * NOTICE: We are violating what the Databook says about the
2143 * EndTransfer command. Ideally we would _always_ wait for the
2144 * EndTransfer Command Completion IRQ, but that's causing too
2145 * much trouble synchronizing between us and gadget driver.
2146 *
2147 * We have discussed this with the IP Provider and it was
2148 * suggested to giveback all requests here, but give HW some
2149 * extra time to synchronize with the interconnect. We're using
2150 * an arbitraty 100us delay for that.
2151 *
2152 * Note also that a similar handling was tested by Synopsys
2153 * (thanks a lot Paul) and nothing bad has come out of it.
2154 * In short, what we're doing is:
2155 *
2156 * - Issue EndTransfer WITH CMDIOC bit set
2157 * - Wait 100us
2158 */
2159
Pratyush Anand6263ebe2012-06-23 02:23:08 +05302160 cmd = DWC3_DEPCMD_ENDTRANSFER;
2161 cmd |= DWC3_DEPCMD_HIPRI_FORCERM | DWC3_DEPCMD_CMDIOC;
Felipe Balbi4959cfc2012-06-06 12:04:13 +03002162 cmd |= DWC3_DEPCMD_PARAM(dep->resource_index);
Pratyush Anand6263ebe2012-06-23 02:23:08 +05302163 memset(&params, 0, sizeof(params));
2164 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number, cmd, &params);
2165 WARN_ON_ONCE(ret);
Felipe Balbi4959cfc2012-06-06 12:04:13 +03002166 dep->resource_index = 0;
Pratyush Anande67fdeb2012-07-06 15:19:10 +05302167
2168 udelay(100);
Felipe Balbi72246da2011-08-19 18:10:58 +03002169}
2170
2171static void dwc3_stop_active_transfers(struct dwc3 *dwc)
2172{
2173 u32 epnum;
2174
2175 for (epnum = 2; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
2176 struct dwc3_ep *dep;
2177
2178 dep = dwc->eps[epnum];
2179 if (!(dep->flags & DWC3_EP_ENABLED))
2180 continue;
2181
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +02002182 dwc3_remove_requests(dwc, dep);
Felipe Balbi72246da2011-08-19 18:10:58 +03002183 }
2184}
2185
2186static void dwc3_clear_stall_all_ep(struct dwc3 *dwc)
2187{
2188 u32 epnum;
2189
2190 for (epnum = 1; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
2191 struct dwc3_ep *dep;
2192 struct dwc3_gadget_ep_cmd_params params;
2193 int ret;
2194
2195 dep = dwc->eps[epnum];
2196
2197 if (!(dep->flags & DWC3_EP_STALL))
2198 continue;
2199
2200 dep->flags &= ~DWC3_EP_STALL;
2201
2202 memset(&params, 0, sizeof(params));
2203 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number,
2204 DWC3_DEPCMD_CLEARSTALL, &params);
2205 WARN_ON_ONCE(ret);
2206 }
2207}
2208
2209static void dwc3_gadget_disconnect_interrupt(struct dwc3 *dwc)
2210{
Felipe Balbi34d548c2012-05-24 10:30:01 +03002211 int reg;
2212
Felipe Balbi72246da2011-08-19 18:10:58 +03002213 dev_vdbg(dwc->dev, "%s\n", __func__);
Felipe Balbi72246da2011-08-19 18:10:58 +03002214
2215 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2216 reg &= ~DWC3_DCTL_INITU1ENA;
2217 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2218
2219 reg &= ~DWC3_DCTL_INITU2ENA;
2220 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +03002221
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +05302222 dbg_event(0xFF, "DISCONNECT", 0);
Felipe Balbi72246da2011-08-19 18:10:58 +03002223 dwc3_disconnect_gadget(dwc);
Paul Zimmermanb23c8432011-09-30 10:58:42 +03002224 dwc->start_config_issued = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03002225
2226 dwc->gadget.speed = USB_SPEED_UNKNOWN;
Felipe Balbidf62df52011-10-14 15:11:49 +03002227 dwc->setup_packet_pending = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03002228}
2229
Paul Zimmermandffb81b2012-04-27 12:54:05 +03002230static void dwc3_gadget_usb3_phy_suspend(struct dwc3 *dwc, int suspend)
Felipe Balbi72246da2011-08-19 18:10:58 +03002231{
2232 u32 reg;
2233
2234 reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0));
2235
Paul Zimmermandffb81b2012-04-27 12:54:05 +03002236 if (suspend)
Felipe Balbi72246da2011-08-19 18:10:58 +03002237 reg |= DWC3_GUSB3PIPECTL_SUSPHY;
Paul Zimmermandffb81b2012-04-27 12:54:05 +03002238 else
2239 reg &= ~DWC3_GUSB3PIPECTL_SUSPHY;
Felipe Balbi72246da2011-08-19 18:10:58 +03002240
2241 dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg);
2242}
2243
Paul Zimmermandffb81b2012-04-27 12:54:05 +03002244static void dwc3_gadget_usb2_phy_suspend(struct dwc3 *dwc, int suspend)
Felipe Balbi72246da2011-08-19 18:10:58 +03002245{
2246 u32 reg;
2247
2248 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
2249
Paul Zimmermandffb81b2012-04-27 12:54:05 +03002250 if (suspend)
Felipe Balbi72246da2011-08-19 18:10:58 +03002251 reg |= DWC3_GUSB2PHYCFG_SUSPHY;
Paul Zimmermandffb81b2012-04-27 12:54:05 +03002252 else
2253 reg &= ~DWC3_GUSB2PHYCFG_SUSPHY;
Felipe Balbi72246da2011-08-19 18:10:58 +03002254
2255 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
2256}
2257
2258static void dwc3_gadget_reset_interrupt(struct dwc3 *dwc)
2259{
2260 u32 reg;
Vijayavardhan Vennapusa908f1ed2012-10-19 19:51:48 +05302261 struct dwc3_otg *dotg = dwc->dotg;
Felipe Balbi72246da2011-08-19 18:10:58 +03002262
2263 dev_vdbg(dwc->dev, "%s\n", __func__);
2264
Felipe Balbidf62df52011-10-14 15:11:49 +03002265 /*
2266 * WORKAROUND: DWC3 revisions <1.88a have an issue which
2267 * would cause a missing Disconnect Event if there's a
2268 * pending Setup Packet in the FIFO.
2269 *
2270 * There's no suggested workaround on the official Bug
2271 * report, which states that "unless the driver/application
2272 * is doing any special handling of a disconnect event,
2273 * there is no functional issue".
2274 *
2275 * Unfortunately, it turns out that we _do_ some special
2276 * handling of a disconnect event, namely complete all
2277 * pending transfers, notify gadget driver of the
2278 * disconnection, and so on.
2279 *
2280 * Our suggested workaround is to follow the Disconnect
2281 * Event steps here, instead, based on a setup_packet_pending
2282 * flag. Such flag gets set whenever we have a XferNotReady
2283 * event on EP0 and gets cleared on XferComplete for the
2284 * same endpoint.
2285 *
2286 * Refers to:
2287 *
2288 * STAR#9000466709: RTL: Device : Disconnect event not
2289 * generated if setup packet pending in FIFO
2290 */
2291 if (dwc->revision < DWC3_REVISION_188A) {
2292 if (dwc->setup_packet_pending)
2293 dwc3_gadget_disconnect_interrupt(dwc);
2294 }
2295
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +05302296 dbg_event(0xFF, "BUS RST", 0);
Felipe Balbi961906e2011-12-20 15:37:21 +02002297 /* after reset -> Default State */
2298 dwc->dev_state = DWC3_DEFAULT_STATE;
2299
Paul Zimmerman88df4272012-04-27 13:10:52 +03002300 /* Recent versions support automatic phy suspend and don't need this */
2301 if (dwc->revision < DWC3_REVISION_194A) {
2302 /* Resume PHYs */
2303 dwc3_gadget_usb2_phy_suspend(dwc, false);
2304 dwc3_gadget_usb3_phy_suspend(dwc, false);
2305 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002306
Vijayavardhan Vennapusa908f1ed2012-10-19 19:51:48 +05302307 if (dotg && dotg->otg.phy)
2308 usb_phy_set_power(dotg->otg.phy, 0);
2309
Felipe Balbi72246da2011-08-19 18:10:58 +03002310 if (dwc->gadget.speed != USB_SPEED_UNKNOWN)
2311 dwc3_disconnect_gadget(dwc);
2312
2313 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2314 reg &= ~DWC3_DCTL_TSTCTRL_MASK;
2315 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Gerard Cauvy3b637362012-02-10 12:21:18 +02002316 dwc->test_mode = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03002317
2318 dwc3_stop_active_transfers(dwc);
2319 dwc3_clear_stall_all_ep(dwc);
Paul Zimmermanb23c8432011-09-30 10:58:42 +03002320 dwc->start_config_issued = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03002321
2322 /* Reset device address to zero */
2323 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2324 reg &= ~(DWC3_DCFG_DEVADDR_MASK);
2325 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +03002326}
2327
2328static void dwc3_update_ram_clk_sel(struct dwc3 *dwc, u32 speed)
2329{
2330 u32 reg;
2331 u32 usb30_clock = DWC3_GCTL_CLK_BUS;
2332
2333 /*
2334 * We change the clock only at SS but I dunno why I would want to do
2335 * this. Maybe it becomes part of the power saving plan.
2336 */
2337
2338 if (speed != DWC3_DSTS_SUPERSPEED)
2339 return;
2340
2341 /*
2342 * RAMClkSel is reset to 0 after USB reset, so it must be reprogrammed
2343 * each time on Connect Done.
2344 */
2345 if (!usb30_clock)
2346 return;
2347
2348 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
2349 reg |= DWC3_GCTL_RAMCLKSEL(usb30_clock);
2350 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
2351}
2352
Paul Zimmermandffb81b2012-04-27 12:54:05 +03002353static void dwc3_gadget_phy_suspend(struct dwc3 *dwc, u8 speed)
Felipe Balbi72246da2011-08-19 18:10:58 +03002354{
2355 switch (speed) {
2356 case USB_SPEED_SUPER:
Paul Zimmermandffb81b2012-04-27 12:54:05 +03002357 dwc3_gadget_usb2_phy_suspend(dwc, true);
Felipe Balbi72246da2011-08-19 18:10:58 +03002358 break;
2359 case USB_SPEED_HIGH:
2360 case USB_SPEED_FULL:
2361 case USB_SPEED_LOW:
Paul Zimmermandffb81b2012-04-27 12:54:05 +03002362 dwc3_gadget_usb3_phy_suspend(dwc, true);
Felipe Balbi72246da2011-08-19 18:10:58 +03002363 break;
2364 }
2365}
2366
2367static void dwc3_gadget_conndone_interrupt(struct dwc3 *dwc)
2368{
2369 struct dwc3_gadget_ep_cmd_params params;
2370 struct dwc3_ep *dep;
2371 int ret;
2372 u32 reg;
2373 u8 speed;
2374
2375 dev_vdbg(dwc->dev, "%s\n", __func__);
2376
2377 memset(&params, 0x00, sizeof(params));
2378
Felipe Balbi72246da2011-08-19 18:10:58 +03002379 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
2380 speed = reg & DWC3_DSTS_CONNECTSPD;
2381 dwc->speed = speed;
2382
2383 dwc3_update_ram_clk_sel(dwc, speed);
2384
2385 switch (speed) {
2386 case DWC3_DCFG_SUPERSPEED:
Felipe Balbi05870c52011-10-14 14:51:38 +03002387 /*
2388 * WORKAROUND: DWC3 revisions <1.90a have an issue which
2389 * would cause a missing USB3 Reset event.
2390 *
2391 * In such situations, we should force a USB3 Reset
2392 * event by calling our dwc3_gadget_reset_interrupt()
2393 * routine.
2394 *
2395 * Refers to:
2396 *
2397 * STAR#9000483510: RTL: SS : USB3 reset event may
2398 * not be generated always when the link enters poll
2399 */
2400 if (dwc->revision < DWC3_REVISION_190A)
2401 dwc3_gadget_reset_interrupt(dwc);
2402
Felipe Balbi72246da2011-08-19 18:10:58 +03002403 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
2404 dwc->gadget.ep0->maxpacket = 512;
2405 dwc->gadget.speed = USB_SPEED_SUPER;
2406 break;
2407 case DWC3_DCFG_HIGHSPEED:
2408 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
2409 dwc->gadget.ep0->maxpacket = 64;
2410 dwc->gadget.speed = USB_SPEED_HIGH;
2411 break;
2412 case DWC3_DCFG_FULLSPEED2:
2413 case DWC3_DCFG_FULLSPEED1:
2414 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
2415 dwc->gadget.ep0->maxpacket = 64;
2416 dwc->gadget.speed = USB_SPEED_FULL;
2417 break;
2418 case DWC3_DCFG_LOWSPEED:
2419 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(8);
2420 dwc->gadget.ep0->maxpacket = 8;
2421 dwc->gadget.speed = USB_SPEED_LOW;
2422 break;
2423 }
2424
Paul Zimmerman88df4272012-04-27 13:10:52 +03002425 /* Recent versions support automatic phy suspend and don't need this */
2426 if (dwc->revision < DWC3_REVISION_194A) {
2427 /* Suspend unneeded PHY */
2428 dwc3_gadget_phy_suspend(dwc, dwc->gadget.speed);
2429 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002430
2431 dep = dwc->eps[0];
Felipe Balbi07e0ee82012-07-16 14:08:16 +03002432 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, true);
Felipe Balbi72246da2011-08-19 18:10:58 +03002433 if (ret) {
2434 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
2435 return;
2436 }
2437
2438 dep = dwc->eps[1];
Felipe Balbi07e0ee82012-07-16 14:08:16 +03002439 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, true);
Felipe Balbi72246da2011-08-19 18:10:58 +03002440 if (ret) {
2441 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
2442 return;
2443 }
2444
2445 /*
2446 * Configure PHY via GUSB3PIPECTLn if required.
2447 *
2448 * Update GTXFIFOSIZn
2449 *
2450 * In both cases reset values should be sufficient.
2451 */
2452}
2453
2454static void dwc3_gadget_wakeup_interrupt(struct dwc3 *dwc)
2455{
2456 dev_vdbg(dwc->dev, "%s\n", __func__);
2457
2458 /*
2459 * TODO take core out of low power mode when that's
2460 * implemented.
2461 */
2462
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +05302463 dbg_event(0xFF, "WAKEUP", 0);
Felipe Balbi72246da2011-08-19 18:10:58 +03002464 dwc->gadget_driver->resume(&dwc->gadget);
2465}
2466
2467static void dwc3_gadget_linksts_change_interrupt(struct dwc3 *dwc,
2468 unsigned int evtinfo)
2469{
Felipe Balbifae2b902011-10-14 13:00:30 +03002470 enum dwc3_link_state next = evtinfo & DWC3_LINK_STATE_MASK;
2471
2472 /*
2473 * WORKAROUND: DWC3 Revisions <1.83a have an issue which, depending
2474 * on the link partner, the USB session might do multiple entry/exit
2475 * of low power states before a transfer takes place.
2476 *
2477 * Due to this problem, we might experience lower throughput. The
2478 * suggested workaround is to disable DCTL[12:9] bits if we're
2479 * transitioning from U1/U2 to U0 and enable those bits again
2480 * after a transfer completes and there are no pending transfers
2481 * on any of the enabled endpoints.
2482 *
2483 * This is the first half of that workaround.
2484 *
2485 * Refers to:
2486 *
2487 * STAR#9000446952: RTL: Device SS : if U1/U2 ->U0 takes >128us
2488 * core send LGO_Ux entering U0
2489 */
2490 if (dwc->revision < DWC3_REVISION_183A) {
2491 if (next == DWC3_LINK_STATE_U0) {
2492 u32 u1u2;
2493 u32 reg;
2494
2495 switch (dwc->link_state) {
2496 case DWC3_LINK_STATE_U1:
2497 case DWC3_LINK_STATE_U2:
2498 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2499 u1u2 = reg & (DWC3_DCTL_INITU2ENA
2500 | DWC3_DCTL_ACCEPTU2ENA
2501 | DWC3_DCTL_INITU1ENA
2502 | DWC3_DCTL_ACCEPTU1ENA);
2503
2504 if (!dwc->u1u2)
2505 dwc->u1u2 = reg & u1u2;
2506
2507 reg &= ~u1u2;
2508
2509 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2510 break;
2511 default:
2512 /* do nothing */
2513 break;
2514 }
2515 }
2516 }
2517
Vijayavardhan Vennapusa54be1d62012-10-06 18:32:06 +05302518 if (next == DWC3_LINK_STATE_U0) {
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +05302519 if (dwc->link_state == DWC3_LINK_STATE_U3) {
2520 dbg_event(0xFF, "RESUME", 0);
Vijayavardhan Vennapusa54be1d62012-10-06 18:32:06 +05302521 dwc->gadget_driver->resume(&dwc->gadget);
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +05302522 }
Vijayavardhan Vennapusa54be1d62012-10-06 18:32:06 +05302523 } else if (next == DWC3_LINK_STATE_U3) {
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +05302524 dbg_event(0xFF, "SUSPEND", 0);
Vijayavardhan Vennapusa54be1d62012-10-06 18:32:06 +05302525 dwc->gadget_driver->suspend(&dwc->gadget);
2526 }
2527
Felipe Balbifae2b902011-10-14 13:00:30 +03002528 dwc->link_state = next;
Felipe Balbi019ac832011-09-08 21:18:47 +03002529
2530 dev_vdbg(dwc->dev, "%s link %d\n", __func__, dwc->link_state);
Felipe Balbi72246da2011-08-19 18:10:58 +03002531}
2532
2533static void dwc3_gadget_interrupt(struct dwc3 *dwc,
2534 const struct dwc3_event_devt *event)
2535{
2536 switch (event->type) {
2537 case DWC3_DEVICE_EVENT_DISCONNECT:
2538 dwc3_gadget_disconnect_interrupt(dwc);
2539 break;
2540 case DWC3_DEVICE_EVENT_RESET:
2541 dwc3_gadget_reset_interrupt(dwc);
2542 break;
2543 case DWC3_DEVICE_EVENT_CONNECT_DONE:
2544 dwc3_gadget_conndone_interrupt(dwc);
2545 break;
2546 case DWC3_DEVICE_EVENT_WAKEUP:
2547 dwc3_gadget_wakeup_interrupt(dwc);
2548 break;
2549 case DWC3_DEVICE_EVENT_LINK_STATUS_CHANGE:
2550 dwc3_gadget_linksts_change_interrupt(dwc, event->event_info);
2551 break;
2552 case DWC3_DEVICE_EVENT_EOPF:
2553 dev_vdbg(dwc->dev, "End of Periodic Frame\n");
2554 break;
2555 case DWC3_DEVICE_EVENT_SOF:
2556 dev_vdbg(dwc->dev, "Start of Periodic Frame\n");
2557 break;
2558 case DWC3_DEVICE_EVENT_ERRATIC_ERROR:
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +05302559 dbg_event(0xFF, "ERROR", 0);
Felipe Balbi72246da2011-08-19 18:10:58 +03002560 dev_vdbg(dwc->dev, "Erratic Error\n");
2561 break;
2562 case DWC3_DEVICE_EVENT_CMD_CMPL:
2563 dev_vdbg(dwc->dev, "Command Complete\n");
2564 break;
2565 case DWC3_DEVICE_EVENT_OVERFLOW:
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +05302566 dbg_event(0xFF, "OVERFL", 0);
Felipe Balbi72246da2011-08-19 18:10:58 +03002567 dev_vdbg(dwc->dev, "Overflow\n");
Pavankumar Kondetid393e172012-06-12 16:07:29 +05302568 /*
2569 * Controllers prior to 2.30a revision has a bug where
2570 * Overflow Event may overwrite an unacknowledged event
2571 * in the event buffer. The severity of the issue depends
2572 * on the overwritten event type. Add a warning message
2573 * saying that an event is overwritten.
2574 *
2575 * TODO: In future we may need to see if we can re-enumerate
2576 * with host.
2577 */
2578 if (dwc->revision < DWC3_REVISION_230A)
2579 dev_warn(dwc->dev, "Unacknowledged event overwritten\n");
Felipe Balbi72246da2011-08-19 18:10:58 +03002580 break;
Pavankumar Kondeti33fe6f12012-06-12 16:21:46 +05302581 case DWC3_DEVICE_EVENT_VENDOR_DEV_TEST_LMP:
2582 /*
2583 * Controllers prior to 2.30a revision has a bug, due to which
2584 * a vendor device test LMP event can not be filtered. But
2585 * this event is not handled in the current code. This is a
2586 * special event and 8 bytes of data will follow the event.
2587 * Handling this event is tricky when event buffer is almost
2588 * full. Moreover this event will not occur in normal scenario
2589 * and can only happen with special hosts in testing scenarios.
2590 * Add a warning message to indicate that this event is received
2591 * which means that event buffer might have corrupted.
2592 */
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +05302593 dbg_event(0xFF, "TSTLMP", 0);
Pavankumar Kondeti33fe6f12012-06-12 16:21:46 +05302594 if (dwc->revision < DWC3_REVISION_230A)
2595 dev_warn(dwc->dev, "Vendor Device Test LMP Received\n");
2596 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03002597 default:
2598 dev_dbg(dwc->dev, "UNKNOWN IRQ %d\n", event->type);
2599 }
2600}
2601
2602static void dwc3_process_event_entry(struct dwc3 *dwc,
2603 const union dwc3_event *event)
2604{
2605 /* Endpoint IRQ, handle it and return early */
2606 if (event->type.is_devspec == 0) {
2607 /* depevt */
2608 return dwc3_endpoint_interrupt(dwc, &event->depevt);
2609 }
2610
2611 switch (event->type.type) {
2612 case DWC3_EVENT_TYPE_DEV:
2613 dwc3_gadget_interrupt(dwc, &event->devt);
2614 break;
2615 /* REVISIT what to do with Carkit and I2C events ? */
2616 default:
2617 dev_err(dwc->dev, "UNKNOWN IRQ type %d\n", event->raw);
2618 }
2619}
2620
2621static irqreturn_t dwc3_process_event_buf(struct dwc3 *dwc, u32 buf)
2622{
2623 struct dwc3_event_buffer *evt;
2624 int left;
2625 u32 count;
2626
2627 count = dwc3_readl(dwc->regs, DWC3_GEVNTCOUNT(buf));
2628 count &= DWC3_GEVNTCOUNT_MASK;
2629 if (!count)
2630 return IRQ_NONE;
2631
2632 evt = dwc->ev_buffs[buf];
2633 left = count;
2634
2635 while (left > 0) {
2636 union dwc3_event event;
2637
Felipe Balbid70d8442012-02-06 13:40:17 +02002638 event.raw = *(u32 *) (evt->buf + evt->lpos);
2639
Felipe Balbi72246da2011-08-19 18:10:58 +03002640 dwc3_process_event_entry(dwc, &event);
2641 /*
2642 * XXX we wrap around correctly to the next entry as almost all
2643 * entries are 4 bytes in size. There is one entry which has 12
2644 * bytes which is a regular entry followed by 8 bytes data. ATM
2645 * I don't know how things are organized if were get next to the
2646 * a boundary so I worry about that once we try to handle that.
2647 */
2648 evt->lpos = (evt->lpos + 4) % DWC3_EVENT_BUFFERS_SIZE;
2649 left -= 4;
2650
2651 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(buf), 4);
2652 }
2653
2654 return IRQ_HANDLED;
2655}
2656
2657static irqreturn_t dwc3_interrupt(int irq, void *_dwc)
2658{
2659 struct dwc3 *dwc = _dwc;
2660 int i;
2661 irqreturn_t ret = IRQ_NONE;
2662
2663 spin_lock(&dwc->lock);
2664
Felipe Balbi9f622b22011-10-12 10:31:04 +03002665 for (i = 0; i < dwc->num_event_buffers; i++) {
Felipe Balbi72246da2011-08-19 18:10:58 +03002666 irqreturn_t status;
2667
2668 status = dwc3_process_event_buf(dwc, i);
2669 if (status == IRQ_HANDLED)
2670 ret = status;
2671 }
2672
2673 spin_unlock(&dwc->lock);
2674
2675 return ret;
2676}
2677
2678/**
2679 * dwc3_gadget_init - Initializes gadget related registers
Paul Zimmerman1d046792012-02-15 18:56:56 -08002680 * @dwc: pointer to our controller context structure
Felipe Balbi72246da2011-08-19 18:10:58 +03002681 *
2682 * Returns 0 on success otherwise negative errno.
2683 */
2684int __devinit dwc3_gadget_init(struct dwc3 *dwc)
2685{
2686 u32 reg;
2687 int ret;
2688 int irq;
2689
2690 dwc->ctrl_req = dma_alloc_coherent(dwc->dev, sizeof(*dwc->ctrl_req),
2691 &dwc->ctrl_req_addr, GFP_KERNEL);
2692 if (!dwc->ctrl_req) {
2693 dev_err(dwc->dev, "failed to allocate ctrl request\n");
2694 ret = -ENOMEM;
2695 goto err0;
2696 }
2697
2698 dwc->ep0_trb = dma_alloc_coherent(dwc->dev, sizeof(*dwc->ep0_trb),
2699 &dwc->ep0_trb_addr, GFP_KERNEL);
2700 if (!dwc->ep0_trb) {
2701 dev_err(dwc->dev, "failed to allocate ep0 trb\n");
2702 ret = -ENOMEM;
2703 goto err1;
2704 }
2705
Felipe Balbib0791fb2012-05-04 12:58:14 +03002706 dwc->setup_buf = kzalloc(DWC3_EP0_BOUNCE_SIZE, GFP_KERNEL);
Felipe Balbi72246da2011-08-19 18:10:58 +03002707 if (!dwc->setup_buf) {
2708 dev_err(dwc->dev, "failed to allocate setup buffer\n");
2709 ret = -ENOMEM;
2710 goto err2;
2711 }
2712
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002713 dwc->ep0_bounce = dma_alloc_coherent(dwc->dev,
Felipe Balbib0791fb2012-05-04 12:58:14 +03002714 DWC3_EP0_BOUNCE_SIZE, &dwc->ep0_bounce_addr,
2715 GFP_KERNEL);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002716 if (!dwc->ep0_bounce) {
2717 dev_err(dwc->dev, "failed to allocate ep0 bounce buffer\n");
2718 ret = -ENOMEM;
2719 goto err3;
2720 }
2721
Felipe Balbi72246da2011-08-19 18:10:58 +03002722 dev_set_name(&dwc->gadget.dev, "gadget");
2723
2724 dwc->gadget.ops = &dwc3_gadget_ops;
Manu Gautama7b082a2012-11-06 09:50:09 +05302725 dwc->gadget.max_speed = USB_SPEED_SUPER;
Felipe Balbi72246da2011-08-19 18:10:58 +03002726 dwc->gadget.speed = USB_SPEED_UNKNOWN;
2727 dwc->gadget.dev.parent = dwc->dev;
Felipe Balbieeb720f2011-11-28 12:46:59 +02002728 dwc->gadget.sg_supported = true;
Felipe Balbi72246da2011-08-19 18:10:58 +03002729
2730 dma_set_coherent_mask(&dwc->gadget.dev, dwc->dev->coherent_dma_mask);
2731
2732 dwc->gadget.dev.dma_parms = dwc->dev->dma_parms;
2733 dwc->gadget.dev.dma_mask = dwc->dev->dma_mask;
2734 dwc->gadget.dev.release = dwc3_gadget_release;
2735 dwc->gadget.name = "dwc3-gadget";
2736
2737 /*
2738 * REVISIT: Here we should clear all pending IRQs to be
2739 * sure we're starting from a well known location.
2740 */
2741
2742 ret = dwc3_gadget_init_endpoints(dwc);
2743 if (ret)
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002744 goto err4;
Felipe Balbi72246da2011-08-19 18:10:58 +03002745
2746 irq = platform_get_irq(to_platform_device(dwc->dev), 0);
2747
2748 ret = request_irq(irq, dwc3_interrupt, IRQF_SHARED,
2749 "dwc3", dwc);
2750 if (ret) {
2751 dev_err(dwc->dev, "failed to request irq #%d --> %d\n",
2752 irq, ret);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002753 goto err5;
Felipe Balbi72246da2011-08-19 18:10:58 +03002754 }
2755
Sebastian Andrzej Siewiorbb8b8a32011-09-13 17:54:39 +02002756 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2757 reg |= DWC3_DCFG_LPM_CAP;
2758 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
2759
Felipe Balbi72246da2011-08-19 18:10:58 +03002760 /* Enable all but Start and End of Frame IRQs */
Pavankumar Kondeti33fe6f12012-06-12 16:21:46 +05302761 reg = (DWC3_DEVTEN_EVNTOVERFLOWEN |
Felipe Balbi72246da2011-08-19 18:10:58 +03002762 DWC3_DEVTEN_CMDCMPLTEN |
2763 DWC3_DEVTEN_ERRTICERREN |
2764 DWC3_DEVTEN_WKUPEVTEN |
2765 DWC3_DEVTEN_ULSTCNGEN |
2766 DWC3_DEVTEN_CONNECTDONEEN |
2767 DWC3_DEVTEN_USBRSTEN |
2768 DWC3_DEVTEN_DISCONNEVTEN);
2769 dwc3_writel(dwc->regs, DWC3_DEVTEN, reg);
2770
Paul Zimmerman88df4272012-04-27 13:10:52 +03002771 /* Enable USB2 LPM and automatic phy suspend only on recent versions */
2772 if (dwc->revision >= DWC3_REVISION_194A) {
2773 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2774 reg |= DWC3_DCFG_LPM_CAP;
2775 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
2776
2777 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2778 reg &= ~(DWC3_DCTL_HIRD_THRES_MASK | DWC3_DCTL_L1_HIBER_EN);
2779
2780 /* TODO: This should be configurable */
Pratyush Anandd69dcdd2012-07-02 10:21:52 +05302781 reg |= DWC3_DCTL_HIRD_THRES(28);
Paul Zimmerman88df4272012-04-27 13:10:52 +03002782
2783 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2784
Pratyush Anand50ed8342012-06-06 19:36:17 +05302785 dwc3_gadget_usb2_phy_suspend(dwc, false);
2786 dwc3_gadget_usb3_phy_suspend(dwc, false);
Paul Zimmerman88df4272012-04-27 13:10:52 +03002787 }
2788
Felipe Balbi72246da2011-08-19 18:10:58 +03002789 ret = device_register(&dwc->gadget.dev);
2790 if (ret) {
2791 dev_err(dwc->dev, "failed to register gadget device\n");
2792 put_device(&dwc->gadget.dev);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002793 goto err6;
Felipe Balbi72246da2011-08-19 18:10:58 +03002794 }
2795
2796 ret = usb_add_gadget_udc(dwc->dev, &dwc->gadget);
2797 if (ret) {
2798 dev_err(dwc->dev, "failed to register udc\n");
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002799 goto err7;
Felipe Balbi72246da2011-08-19 18:10:58 +03002800 }
2801
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +02002802 if (dwc->dotg) {
2803 /* dwc3 otg driver is active (DRD mode + SRPSupport=1) */
2804 ret = otg_set_peripheral(&dwc->dotg->otg, &dwc->gadget);
2805 if (ret) {
2806 dev_err(dwc->dev, "failed to set peripheral to otg\n");
2807 goto err7;
2808 }
Manu Gautamb5067272012-07-02 09:53:41 +05302809 } else {
2810 pm_runtime_no_callbacks(&dwc->gadget.dev);
2811 pm_runtime_set_active(&dwc->gadget.dev);
2812 pm_runtime_enable(&dwc->gadget.dev);
2813 pm_runtime_get(&dwc->gadget.dev);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +02002814 }
2815
Felipe Balbi72246da2011-08-19 18:10:58 +03002816 return 0;
2817
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002818err7:
Felipe Balbi72246da2011-08-19 18:10:58 +03002819 device_unregister(&dwc->gadget.dev);
2820
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002821err6:
Felipe Balbi72246da2011-08-19 18:10:58 +03002822 dwc3_writel(dwc->regs, DWC3_DEVTEN, 0x00);
2823 free_irq(irq, dwc);
2824
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002825err5:
Felipe Balbi72246da2011-08-19 18:10:58 +03002826 dwc3_gadget_free_endpoints(dwc);
2827
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002828err4:
Felipe Balbib0791fb2012-05-04 12:58:14 +03002829 dma_free_coherent(dwc->dev, DWC3_EP0_BOUNCE_SIZE,
2830 dwc->ep0_bounce, dwc->ep0_bounce_addr);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002831
Felipe Balbi72246da2011-08-19 18:10:58 +03002832err3:
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02002833 kfree(dwc->setup_buf);
Felipe Balbi72246da2011-08-19 18:10:58 +03002834
2835err2:
2836 dma_free_coherent(dwc->dev, sizeof(*dwc->ep0_trb),
2837 dwc->ep0_trb, dwc->ep0_trb_addr);
2838
2839err1:
2840 dma_free_coherent(dwc->dev, sizeof(*dwc->ctrl_req),
2841 dwc->ctrl_req, dwc->ctrl_req_addr);
2842
2843err0:
2844 return ret;
2845}
2846
2847void dwc3_gadget_exit(struct dwc3 *dwc)
2848{
2849 int irq;
Felipe Balbi72246da2011-08-19 18:10:58 +03002850
Manu Gautamb5067272012-07-02 09:53:41 +05302851 if (dwc->dotg) {
2852 pm_runtime_put(&dwc->gadget.dev);
2853 pm_runtime_disable(&dwc->gadget.dev);
2854 }
2855
Felipe Balbi72246da2011-08-19 18:10:58 +03002856 usb_del_gadget_udc(&dwc->gadget);
2857 irq = platform_get_irq(to_platform_device(dwc->dev), 0);
2858
2859 dwc3_writel(dwc->regs, DWC3_DEVTEN, 0x00);
2860 free_irq(irq, dwc);
2861
Felipe Balbi72246da2011-08-19 18:10:58 +03002862 dwc3_gadget_free_endpoints(dwc);
2863
Felipe Balbib0791fb2012-05-04 12:58:14 +03002864 dma_free_coherent(dwc->dev, DWC3_EP0_BOUNCE_SIZE,
2865 dwc->ep0_bounce, dwc->ep0_bounce_addr);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002866
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02002867 kfree(dwc->setup_buf);
Felipe Balbi72246da2011-08-19 18:10:58 +03002868
2869 dma_free_coherent(dwc->dev, sizeof(*dwc->ep0_trb),
2870 dwc->ep0_trb, dwc->ep0_trb_addr);
2871
2872 dma_free_coherent(dwc->dev, sizeof(*dwc->ctrl_req),
2873 dwc->ctrl_req, dwc->ctrl_req_addr);
2874
2875 device_unregister(&dwc->gadget.dev);
2876}