blob: 2db9eea203f100667202105a7b2090e97ddf5193 [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
Ido Shayevitz57cdac12012-03-12 20:25:24 +02001208 if (!dep->endpoint.desc) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001209 dev_dbg(dwc->dev, "trying to queue request %p to disabled %s\n",
1210 request, ep->name);
1211 return -ESHUTDOWN;
1212 }
1213
1214 dev_vdbg(dwc->dev, "queing request %p to %s length %d\n",
1215 request, ep->name, request->length);
1216
Manu Gautam1c4dbcb2012-10-05 13:16:00 +05301217 WARN(!dep->direction && (request->length % ep->desc->wMaxPacketSize),
1218 "trying to queue unaligned request (%d)\n", request->length);
1219
Felipe Balbi72246da2011-08-19 18:10:58 +03001220 spin_lock_irqsave(&dwc->lock, flags);
1221 ret = __dwc3_gadget_ep_queue(dep, req);
1222 spin_unlock_irqrestore(&dwc->lock, flags);
1223
1224 return ret;
1225}
1226
1227static int dwc3_gadget_ep_dequeue(struct usb_ep *ep,
1228 struct usb_request *request)
1229{
1230 struct dwc3_request *req = to_dwc3_request(request);
1231 struct dwc3_request *r = NULL;
1232
1233 struct dwc3_ep *dep = to_dwc3_ep(ep);
1234 struct dwc3 *dwc = dep->dwc;
1235
1236 unsigned long flags;
1237 int ret = 0;
1238
1239 spin_lock_irqsave(&dwc->lock, flags);
1240
1241 list_for_each_entry(r, &dep->request_list, list) {
1242 if (r == req)
1243 break;
1244 }
1245
1246 if (r != req) {
1247 list_for_each_entry(r, &dep->req_queued, list) {
1248 if (r == req)
1249 break;
1250 }
1251 if (r == req) {
1252 /* wait until it is processed */
1253 dwc3_stop_active_transfer(dwc, dep->number);
Pratyush Anandeaec3e92012-06-15 11:54:00 +05301254 goto out1;
Felipe Balbi72246da2011-08-19 18:10:58 +03001255 }
1256 dev_err(dwc->dev, "request %p was not queued to %s\n",
1257 request, ep->name);
1258 ret = -EINVAL;
1259 goto out0;
1260 }
1261
Pratyush Anandeaec3e92012-06-15 11:54:00 +05301262out1:
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +05301263 dbg_event(dep->number, "DEQUEUE", 0);
Felipe Balbi72246da2011-08-19 18:10:58 +03001264 /* giveback the request */
1265 dwc3_gadget_giveback(dep, req, -ECONNRESET);
1266
1267out0:
1268 spin_unlock_irqrestore(&dwc->lock, flags);
1269
1270 return ret;
1271}
1272
1273int __dwc3_gadget_ep_set_halt(struct dwc3_ep *dep, int value)
1274{
1275 struct dwc3_gadget_ep_cmd_params params;
1276 struct dwc3 *dwc = dep->dwc;
1277 int ret;
1278
1279 memset(&params, 0x00, sizeof(params));
1280
1281 if (value) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001282 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number,
1283 DWC3_DEPCMD_SETSTALL, &params);
1284 if (ret)
1285 dev_err(dwc->dev, "failed to %s STALL on %s\n",
1286 value ? "set" : "clear",
1287 dep->name);
1288 else
1289 dep->flags |= DWC3_EP_STALL;
1290 } else {
1291 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number,
1292 DWC3_DEPCMD_CLEARSTALL, &params);
1293 if (ret)
1294 dev_err(dwc->dev, "failed to %s STALL on %s\n",
1295 value ? "set" : "clear",
1296 dep->name);
1297 else
Vijayavardhan Vennapusa6008e262012-10-19 15:57:56 +05301298 dep->flags &= ~(DWC3_EP_STALL | DWC3_EP_WEDGE);
Felipe Balbi72246da2011-08-19 18:10:58 +03001299 }
Paul Zimmerman52754552011-09-30 10:58:44 +03001300
Felipe Balbi72246da2011-08-19 18:10:58 +03001301 return ret;
1302}
1303
1304static int dwc3_gadget_ep_set_halt(struct usb_ep *ep, int value)
1305{
1306 struct dwc3_ep *dep = to_dwc3_ep(ep);
1307 struct dwc3 *dwc = dep->dwc;
1308
1309 unsigned long flags;
1310
1311 int ret;
1312
1313 spin_lock_irqsave(&dwc->lock, flags);
1314
Ido Shayevitz57cdac12012-03-12 20:25:24 +02001315 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001316 dev_err(dwc->dev, "%s is of Isochronous type\n", dep->name);
1317 ret = -EINVAL;
1318 goto out;
1319 }
1320
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +05301321 dbg_event(dep->number, "HALT", value);
Felipe Balbi72246da2011-08-19 18:10:58 +03001322 ret = __dwc3_gadget_ep_set_halt(dep, value);
1323out:
1324 spin_unlock_irqrestore(&dwc->lock, flags);
1325
1326 return ret;
1327}
1328
1329static int dwc3_gadget_ep_set_wedge(struct usb_ep *ep)
1330{
1331 struct dwc3_ep *dep = to_dwc3_ep(ep);
Paul Zimmerman249a4562012-02-24 17:32:16 -08001332 struct dwc3 *dwc = dep->dwc;
1333 unsigned long flags;
Felipe Balbi72246da2011-08-19 18:10:58 +03001334
Paul Zimmerman249a4562012-02-24 17:32:16 -08001335 spin_lock_irqsave(&dwc->lock, flags);
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +05301336 dbg_event(dep->number, "WEDGE", 0);
Felipe Balbi72246da2011-08-19 18:10:58 +03001337 dep->flags |= DWC3_EP_WEDGE;
Paul Zimmerman249a4562012-02-24 17:32:16 -08001338 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001339
Pratyush Anandeb840752012-06-25 22:40:43 +05301340 if (dep->number == 0 || dep->number == 1)
1341 return dwc3_gadget_ep0_set_halt(ep, 1);
1342 else
1343 return dwc3_gadget_ep_set_halt(ep, 1);
Felipe Balbi72246da2011-08-19 18:10:58 +03001344}
1345
1346/* -------------------------------------------------------------------------- */
1347
1348static struct usb_endpoint_descriptor dwc3_gadget_ep0_desc = {
1349 .bLength = USB_DT_ENDPOINT_SIZE,
1350 .bDescriptorType = USB_DT_ENDPOINT,
1351 .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
1352};
1353
1354static const struct usb_ep_ops dwc3_gadget_ep0_ops = {
1355 .enable = dwc3_gadget_ep0_enable,
1356 .disable = dwc3_gadget_ep0_disable,
1357 .alloc_request = dwc3_gadget_ep_alloc_request,
1358 .free_request = dwc3_gadget_ep_free_request,
1359 .queue = dwc3_gadget_ep0_queue,
1360 .dequeue = dwc3_gadget_ep_dequeue,
Pratyush Anandeb840752012-06-25 22:40:43 +05301361 .set_halt = dwc3_gadget_ep0_set_halt,
Felipe Balbi72246da2011-08-19 18:10:58 +03001362 .set_wedge = dwc3_gadget_ep_set_wedge,
1363};
1364
1365static const struct usb_ep_ops dwc3_gadget_ep_ops = {
1366 .enable = dwc3_gadget_ep_enable,
1367 .disable = dwc3_gadget_ep_disable,
1368 .alloc_request = dwc3_gadget_ep_alloc_request,
1369 .free_request = dwc3_gadget_ep_free_request,
1370 .queue = dwc3_gadget_ep_queue,
1371 .dequeue = dwc3_gadget_ep_dequeue,
1372 .set_halt = dwc3_gadget_ep_set_halt,
1373 .set_wedge = dwc3_gadget_ep_set_wedge,
1374};
1375
1376/* -------------------------------------------------------------------------- */
1377
1378static int dwc3_gadget_get_frame(struct usb_gadget *g)
1379{
1380 struct dwc3 *dwc = gadget_to_dwc(g);
1381 u32 reg;
1382
1383 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1384 return DWC3_DSTS_SOFFN(reg);
1385}
1386
1387static int dwc3_gadget_wakeup(struct usb_gadget *g)
1388{
1389 struct dwc3 *dwc = gadget_to_dwc(g);
1390
1391 unsigned long timeout;
1392 unsigned long flags;
1393
1394 u32 reg;
1395
1396 int ret = 0;
1397
1398 u8 link_state;
1399 u8 speed;
1400
1401 spin_lock_irqsave(&dwc->lock, flags);
1402
1403 /*
1404 * According to the Databook Remote wakeup request should
1405 * be issued only when the device is in early suspend state.
1406 *
1407 * We can check that via USB Link State bits in DSTS register.
1408 */
1409 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1410
1411 speed = reg & DWC3_DSTS_CONNECTSPD;
1412 if (speed == DWC3_DSTS_SUPERSPEED) {
1413 dev_dbg(dwc->dev, "no wakeup on SuperSpeed\n");
1414 ret = -EINVAL;
1415 goto out;
1416 }
1417
1418 link_state = DWC3_DSTS_USBLNKST(reg);
1419
1420 switch (link_state) {
1421 case DWC3_LINK_STATE_RX_DET: /* in HS, means Early Suspend */
1422 case DWC3_LINK_STATE_U3: /* in HS, means SUSPEND */
1423 break;
1424 default:
1425 dev_dbg(dwc->dev, "can't wakeup from link state %d\n",
1426 link_state);
1427 ret = -EINVAL;
1428 goto out;
1429 }
1430
Felipe Balbi8598bde2012-01-02 18:55:57 +02001431 ret = dwc3_gadget_set_link_state(dwc, DWC3_LINK_STATE_RECOV);
1432 if (ret < 0) {
1433 dev_err(dwc->dev, "failed to put link in Recovery\n");
1434 goto out;
1435 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001436
Paul Zimmerman88df4272012-04-27 13:10:52 +03001437 /* Recent versions do this automatically */
1438 if (dwc->revision < DWC3_REVISION_194A) {
1439 /* write zeroes to Link Change Request */
Felipe Balbib4d04352012-05-24 10:27:56 +03001440 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
Paul Zimmerman88df4272012-04-27 13:10:52 +03001441 reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK;
1442 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1443 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001444
Paul Zimmerman1d046792012-02-15 18:56:56 -08001445 /* poll until Link State changes to ON */
Felipe Balbi72246da2011-08-19 18:10:58 +03001446 timeout = jiffies + msecs_to_jiffies(100);
1447
Paul Zimmerman1d046792012-02-15 18:56:56 -08001448 while (!time_after(jiffies, timeout)) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001449 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1450
1451 /* in HS, means ON */
1452 if (DWC3_DSTS_USBLNKST(reg) == DWC3_LINK_STATE_U0)
1453 break;
1454 }
1455
1456 if (DWC3_DSTS_USBLNKST(reg) != DWC3_LINK_STATE_U0) {
1457 dev_err(dwc->dev, "failed to send remote wakeup\n");
1458 ret = -EINVAL;
1459 }
1460
1461out:
1462 spin_unlock_irqrestore(&dwc->lock, flags);
1463
1464 return ret;
1465}
1466
1467static int dwc3_gadget_set_selfpowered(struct usb_gadget *g,
1468 int is_selfpowered)
1469{
1470 struct dwc3 *dwc = gadget_to_dwc(g);
Paul Zimmerman249a4562012-02-24 17:32:16 -08001471 unsigned long flags;
Felipe Balbi72246da2011-08-19 18:10:58 +03001472
Paul Zimmerman249a4562012-02-24 17:32:16 -08001473 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001474 dwc->is_selfpowered = !!is_selfpowered;
Paul Zimmerman249a4562012-02-24 17:32:16 -08001475 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001476
1477 return 0;
1478}
1479
Pratyush Anand77473f72012-07-02 10:21:55 +05301480static int dwc3_gadget_run_stop(struct dwc3 *dwc, int is_on)
Felipe Balbi72246da2011-08-19 18:10:58 +03001481{
1482 u32 reg;
Sebastian Andrzej Siewior61d58242011-08-29 16:46:38 +02001483 u32 timeout = 500;
Felipe Balbi72246da2011-08-19 18:10:58 +03001484
1485 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
Felipe Balbi8db7ed12012-01-18 18:32:29 +02001486 if (is_on) {
Paul Zimmerman88df4272012-04-27 13:10:52 +03001487 if (dwc->revision <= DWC3_REVISION_187A) {
1488 reg &= ~DWC3_DCTL_TRGTULST_MASK;
1489 reg |= DWC3_DCTL_TRGTULST_RX_DET;
1490 }
1491
1492 if (dwc->revision >= DWC3_REVISION_194A)
1493 reg &= ~DWC3_DCTL_KEEP_CONNECT;
1494 reg |= DWC3_DCTL_RUN_STOP;
Felipe Balbi8db7ed12012-01-18 18:32:29 +02001495 } else {
Felipe Balbi72246da2011-08-19 18:10:58 +03001496 reg &= ~DWC3_DCTL_RUN_STOP;
Felipe Balbi8db7ed12012-01-18 18:32:29 +02001497 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001498
1499 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1500
1501 do {
1502 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1503 if (is_on) {
1504 if (!(reg & DWC3_DSTS_DEVCTRLHLT))
1505 break;
1506 } else {
1507 if (reg & DWC3_DSTS_DEVCTRLHLT)
1508 break;
1509 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001510 timeout--;
1511 if (!timeout)
Pratyush Anand77473f72012-07-02 10:21:55 +05301512 return -ETIMEDOUT;
Sebastian Andrzej Siewior61d58242011-08-29 16:46:38 +02001513 udelay(1);
Felipe Balbi72246da2011-08-19 18:10:58 +03001514 } while (1);
1515
1516 dev_vdbg(dwc->dev, "gadget %s data soft-%s\n",
1517 dwc->gadget_driver
1518 ? dwc->gadget_driver->function : "no-function",
1519 is_on ? "connect" : "disconnect");
Pratyush Anand77473f72012-07-02 10:21:55 +05301520
1521 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +03001522}
1523
Vijayavardhan Vennapusa908f1ed2012-10-19 19:51:48 +05301524static int dwc3_gadget_vbus_draw(struct usb_gadget *g, unsigned mA)
1525{
1526 struct dwc3 *dwc = gadget_to_dwc(g);
1527 struct dwc3_otg *dotg = dwc->dotg;
1528
1529 if (dotg && dotg->otg.phy)
1530 return usb_phy_set_power(dotg->otg.phy, mA);
1531
1532 return -ENOTSUPP;
1533}
1534
Felipe Balbi72246da2011-08-19 18:10:58 +03001535static int dwc3_gadget_pullup(struct usb_gadget *g, int is_on)
1536{
1537 struct dwc3 *dwc = gadget_to_dwc(g);
1538 unsigned long flags;
Pratyush Anand77473f72012-07-02 10:21:55 +05301539 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001540
1541 is_on = !!is_on;
1542
1543 spin_lock_irqsave(&dwc->lock, flags);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +02001544
1545 dwc->softconnect = is_on;
1546
1547 if ((dwc->dotg && !dwc->vbus_active) ||
1548 !dwc->gadget_driver) {
1549
1550 spin_unlock_irqrestore(&dwc->lock, flags);
1551
1552 /*
1553 * Need to wait for vbus_session(on) from otg driver or to
1554 * the udc_start.
1555 */
1556 return 0;
1557 }
1558
Pratyush Anand77473f72012-07-02 10:21:55 +05301559 ret = dwc3_gadget_run_stop(dwc, is_on);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +02001560
1561 spin_unlock_irqrestore(&dwc->lock, flags);
1562
Pratyush Anand77473f72012-07-02 10:21:55 +05301563 return ret;
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +02001564}
1565
1566static int dwc3_gadget_vbus_session(struct usb_gadget *_gadget, int is_active)
1567{
1568 struct dwc3 *dwc = gadget_to_dwc(_gadget);
1569 unsigned long flags;
Vijayavardhan Vennapusa8ec31d22012-10-23 08:44:48 +05301570 int ret = 0;
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +02001571
1572 if (!dwc->dotg)
1573 return -EPERM;
1574
1575 is_active = !!is_active;
1576
1577 spin_lock_irqsave(&dwc->lock, flags);
1578
1579 /* Mark that the vbus was powered */
1580 dwc->vbus_active = is_active;
1581
1582 /*
1583 * Check if upper level usb_gadget_driver was already registerd with
1584 * this udc controller driver (if dwc3_gadget_start was called)
1585 */
1586 if (dwc->gadget_driver && dwc->softconnect) {
1587 if (dwc->vbus_active) {
1588 /*
1589 * Both vbus was activated by otg and pullup was
1590 * signaled by the gadget driver.
1591 */
Pratyush Anand77473f72012-07-02 10:21:55 +05301592 ret = dwc3_gadget_run_stop(dwc, 1);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +02001593 } else {
Pratyush Anand77473f72012-07-02 10:21:55 +05301594 ret = dwc3_gadget_run_stop(dwc, 0);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +02001595 }
Vijayavardhan Vennapusab666fb82012-11-08 16:02:51 +05301596 } else if (dwc->gadget_driver && !dwc->softconnect &&
1597 !dwc->vbus_active) {
1598 if (dwc->gadget_driver->disconnect) {
1599 spin_unlock_irqrestore(&dwc->lock, flags);
1600 dwc->gadget_driver->disconnect(&dwc->gadget);
1601 return 0;
1602 }
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +02001603 }
1604
Felipe Balbi72246da2011-08-19 18:10:58 +03001605 spin_unlock_irqrestore(&dwc->lock, flags);
1606
Pratyush Anand77473f72012-07-02 10:21:55 +05301607 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001608}
1609
Manu Gautamf1fceddf2012-10-12 14:02:50 +05301610/* Required gadget re-initialization before switching to gadget in OTG mode */
1611void dwc3_gadget_restart(struct dwc3 *dwc)
1612{
1613 struct dwc3_ep *dep;
1614 int ret = 0;
Vijayavardhan Vennapusab7434562012-12-12 16:48:49 +05301615 u32 reg;
Manu Gautamf1fceddf2012-10-12 14:02:50 +05301616
Vijayavardhan Vennapusab7434562012-12-12 16:48:49 +05301617 /* Enable all but Start and End of Frame IRQs */
1618 reg = (DWC3_DEVTEN_EVNTOVERFLOWEN |
1619 DWC3_DEVTEN_CMDCMPLTEN |
1620 DWC3_DEVTEN_ERRTICERREN |
1621 DWC3_DEVTEN_WKUPEVTEN |
1622 DWC3_DEVTEN_ULSTCNGEN |
1623 DWC3_DEVTEN_CONNECTDONEEN |
1624 DWC3_DEVTEN_USBRSTEN |
1625 DWC3_DEVTEN_DISCONNEVTEN);
1626 dwc3_writel(dwc->regs, DWC3_DEVTEN, reg);
1627
1628 /* Enable USB2 LPM and automatic phy suspend only on recent versions */
1629 if (dwc->revision >= DWC3_REVISION_194A) {
1630 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
1631 reg |= DWC3_DCFG_LPM_CAP;
1632 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
1633
1634 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
1635 reg &= ~(DWC3_DCTL_HIRD_THRES_MASK | DWC3_DCTL_L1_HIBER_EN);
1636
1637 /* TODO: This should be configurable */
1638 reg |= DWC3_DCTL_HIRD_THRES(28);
1639
1640 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1641 }
1642
1643 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
1644 reg &= ~(DWC3_DCFG_SPEED_MASK);
1645
1646 /**
1647 * WORKAROUND: DWC3 revision < 2.20a have an issue
1648 * which would cause metastability state on Run/Stop
1649 * bit if we try to force the IP to USB2-only mode.
1650 *
1651 * Because of that, we cannot configure the IP to any
1652 * speed other than the SuperSpeed
1653 *
1654 * Refers to:
1655 *
1656 * STAR#9000525659: Clock Domain Crossing on DCTL in
1657 * USB 2.0 Mode
1658 */
1659 if (dwc->revision < DWC3_REVISION_220A)
1660 reg |= DWC3_DCFG_SUPERSPEED;
1661 else
1662 reg |= dwc->maximum_speed;
1663 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
1664
1665 dwc->start_config_issued = false;
1666
1667 /* Start with SuperSpeed Default */
1668 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
Manu Gautamf1fceddf2012-10-12 14:02:50 +05301669
1670 dwc->delayed_status = false;
Vijayavardhan Vennapusab7434562012-12-12 16:48:49 +05301671 /* reinitialize physical ep0-1 */
Manu Gautamf1fceddf2012-10-12 14:02:50 +05301672 dep = dwc->eps[0];
1673 dep->flags = 0;
1674 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, false);
1675 if (ret) {
1676 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
1677 return;
1678 }
1679
1680 dep = dwc->eps[1];
1681 dep->flags = 0;
1682 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, false);
1683 if (ret) {
1684 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
1685 return;
1686 }
1687
1688 /* begin to receive SETUP packets */
1689 dwc->ep0state = EP0_SETUP_PHASE;
1690 dwc3_ep0_out_start(dwc);
1691}
1692
Felipe Balbi72246da2011-08-19 18:10:58 +03001693static int dwc3_gadget_start(struct usb_gadget *g,
1694 struct usb_gadget_driver *driver)
1695{
1696 struct dwc3 *dwc = gadget_to_dwc(g);
1697 struct dwc3_ep *dep;
1698 unsigned long flags;
1699 int ret = 0;
1700 u32 reg;
1701
1702 spin_lock_irqsave(&dwc->lock, flags);
1703
1704 if (dwc->gadget_driver) {
1705 dev_err(dwc->dev, "%s is already bound to %s\n",
1706 dwc->gadget.name,
1707 dwc->gadget_driver->driver.name);
1708 ret = -EBUSY;
1709 goto err0;
1710 }
1711
1712 dwc->gadget_driver = driver;
1713 dwc->gadget.dev.driver = &driver->driver;
1714
Felipe Balbi72246da2011-08-19 18:10:58 +03001715 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
1716 reg &= ~(DWC3_DCFG_SPEED_MASK);
Felipe Balbi38d2c6c2012-03-23 12:20:31 +02001717
1718 /**
1719 * WORKAROUND: DWC3 revision < 2.20a have an issue
1720 * which would cause metastability state on Run/Stop
1721 * bit if we try to force the IP to USB2-only mode.
1722 *
1723 * Because of that, we cannot configure the IP to any
1724 * speed other than the SuperSpeed
1725 *
1726 * Refers to:
1727 *
1728 * STAR#9000525659: Clock Domain Crossing on DCTL in
1729 * USB 2.0 Mode
1730 */
1731 if (dwc->revision < DWC3_REVISION_220A)
1732 reg |= DWC3_DCFG_SUPERSPEED;
1733 else
1734 reg |= dwc->maximum_speed;
Felipe Balbi72246da2011-08-19 18:10:58 +03001735 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
1736
Paul Zimmermanb23c8432011-09-30 10:58:42 +03001737 dwc->start_config_issued = false;
1738
Felipe Balbi72246da2011-08-19 18:10:58 +03001739 /* Start with SuperSpeed Default */
1740 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
1741
1742 dep = dwc->eps[0];
Felipe Balbi07e0ee82012-07-16 14:08:16 +03001743 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, false);
Felipe Balbi72246da2011-08-19 18:10:58 +03001744 if (ret) {
1745 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
1746 goto err0;
1747 }
1748
1749 dep = dwc->eps[1];
Felipe Balbi07e0ee82012-07-16 14:08:16 +03001750 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, false);
Felipe Balbi72246da2011-08-19 18:10:58 +03001751 if (ret) {
1752 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
1753 goto err1;
1754 }
1755
1756 /* begin to receive SETUP packets */
Felipe Balbic7fcdeb2011-08-27 22:28:36 +03001757 dwc->ep0state = EP0_SETUP_PHASE;
Felipe Balbi72246da2011-08-19 18:10:58 +03001758 dwc3_ep0_out_start(dwc);
1759
1760 spin_unlock_irqrestore(&dwc->lock, flags);
1761
1762 return 0;
1763
1764err1:
1765 __dwc3_gadget_ep_disable(dwc->eps[0]);
1766
1767err0:
1768 spin_unlock_irqrestore(&dwc->lock, flags);
1769
1770 return ret;
1771}
1772
1773static int dwc3_gadget_stop(struct usb_gadget *g,
1774 struct usb_gadget_driver *driver)
1775{
1776 struct dwc3 *dwc = gadget_to_dwc(g);
1777 unsigned long flags;
1778
1779 spin_lock_irqsave(&dwc->lock, flags);
1780
1781 __dwc3_gadget_ep_disable(dwc->eps[0]);
1782 __dwc3_gadget_ep_disable(dwc->eps[1]);
1783
1784 dwc->gadget_driver = NULL;
1785 dwc->gadget.dev.driver = NULL;
1786
1787 spin_unlock_irqrestore(&dwc->lock, flags);
1788
1789 return 0;
1790}
Paul Zimmerman88df4272012-04-27 13:10:52 +03001791
Felipe Balbi72246da2011-08-19 18:10:58 +03001792static const struct usb_gadget_ops dwc3_gadget_ops = {
1793 .get_frame = dwc3_gadget_get_frame,
1794 .wakeup = dwc3_gadget_wakeup,
1795 .set_selfpowered = dwc3_gadget_set_selfpowered,
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +02001796 .vbus_session = dwc3_gadget_vbus_session,
Vijayavardhan Vennapusa908f1ed2012-10-19 19:51:48 +05301797 .vbus_draw = dwc3_gadget_vbus_draw,
Felipe Balbi72246da2011-08-19 18:10:58 +03001798 .pullup = dwc3_gadget_pullup,
1799 .udc_start = dwc3_gadget_start,
1800 .udc_stop = dwc3_gadget_stop,
1801};
1802
1803/* -------------------------------------------------------------------------- */
1804
1805static int __devinit dwc3_gadget_init_endpoints(struct dwc3 *dwc)
1806{
1807 struct dwc3_ep *dep;
1808 u8 epnum;
1809
1810 INIT_LIST_HEAD(&dwc->gadget.ep_list);
1811
1812 for (epnum = 0; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
1813 dep = kzalloc(sizeof(*dep), GFP_KERNEL);
1814 if (!dep) {
1815 dev_err(dwc->dev, "can't allocate endpoint %d\n",
1816 epnum);
1817 return -ENOMEM;
1818 }
1819
1820 dep->dwc = dwc;
1821 dep->number = epnum;
1822 dwc->eps[epnum] = dep;
1823
1824 snprintf(dep->name, sizeof(dep->name), "ep%d%s", epnum >> 1,
1825 (epnum & 1) ? "in" : "out");
1826 dep->endpoint.name = dep->name;
1827 dep->direction = (epnum & 1);
1828
1829 if (epnum == 0 || epnum == 1) {
1830 dep->endpoint.maxpacket = 512;
1831 dep->endpoint.ops = &dwc3_gadget_ep0_ops;
1832 if (!epnum)
1833 dwc->gadget.ep0 = &dep->endpoint;
1834 } else {
1835 int ret;
1836
1837 dep->endpoint.maxpacket = 1024;
Sebastian Andrzej Siewior12d36c12011-11-03 20:27:50 +01001838 dep->endpoint.max_streams = 15;
Felipe Balbi72246da2011-08-19 18:10:58 +03001839 dep->endpoint.ops = &dwc3_gadget_ep_ops;
1840 list_add_tail(&dep->endpoint.ep_list,
1841 &dwc->gadget.ep_list);
1842
1843 ret = dwc3_alloc_trb_pool(dep);
Felipe Balbi25b8ff62011-11-04 12:32:47 +02001844 if (ret)
Felipe Balbi72246da2011-08-19 18:10:58 +03001845 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001846 }
Felipe Balbi25b8ff62011-11-04 12:32:47 +02001847
Felipe Balbi72246da2011-08-19 18:10:58 +03001848 INIT_LIST_HEAD(&dep->request_list);
1849 INIT_LIST_HEAD(&dep->req_queued);
1850 }
1851
1852 return 0;
1853}
1854
1855static void dwc3_gadget_free_endpoints(struct dwc3 *dwc)
1856{
1857 struct dwc3_ep *dep;
1858 u8 epnum;
1859
1860 for (epnum = 0; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
1861 dep = dwc->eps[epnum];
1862 dwc3_free_trb_pool(dep);
1863
1864 if (epnum != 0 && epnum != 1)
1865 list_del(&dep->endpoint.ep_list);
1866
1867 kfree(dep);
1868 }
1869}
1870
1871static void dwc3_gadget_release(struct device *dev)
1872{
1873 dev_dbg(dev, "%s\n", __func__);
1874}
1875
1876/* -------------------------------------------------------------------------- */
1877static int dwc3_cleanup_done_reqs(struct dwc3 *dwc, struct dwc3_ep *dep,
1878 const struct dwc3_event_depevt *event, int status)
1879{
1880 struct dwc3_request *req;
Felipe Balbif6bafc62012-02-06 11:04:53 +02001881 struct dwc3_trb *trb;
Felipe Balbi72246da2011-08-19 18:10:58 +03001882 unsigned int count;
1883 unsigned int s_pkt = 0;
Pratyush Anand73939b02012-05-25 18:54:56 +05301884 unsigned int trb_status;
Felipe Balbi72246da2011-08-19 18:10:58 +03001885
1886 do {
1887 req = next_request(&dep->req_queued);
Sebastian Andrzej Siewiord39ee7b2011-11-03 10:32:20 +01001888 if (!req) {
1889 WARN_ON_ONCE(1);
1890 return 1;
1891 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001892
Felipe Balbif6bafc62012-02-06 11:04:53 +02001893 trb = req->trb;
Felipe Balbi72246da2011-08-19 18:10:58 +03001894
Felipe Balbif6bafc62012-02-06 11:04:53 +02001895 if ((trb->ctrl & DWC3_TRB_CTRL_HWO) && status != -ESHUTDOWN)
Sebastian Andrzej Siewior0d2f4752011-08-19 19:59:12 +02001896 /*
1897 * We continue despite the error. There is not much we
Paul Zimmerman1d046792012-02-15 18:56:56 -08001898 * can do. If we don't clean it up we loop forever. If
1899 * we skip the TRB then it gets overwritten after a
1900 * while since we use them in a ring buffer. A BUG()
1901 * would help. Lets hope that if this occurs, someone
Sebastian Andrzej Siewior0d2f4752011-08-19 19:59:12 +02001902 * fixes the root cause instead of looking away :)
1903 */
Felipe Balbi72246da2011-08-19 18:10:58 +03001904 dev_err(dwc->dev, "%s's TRB (%p) still owned by HW\n",
1905 dep->name, req->trb);
Felipe Balbif6bafc62012-02-06 11:04:53 +02001906 count = trb->size & DWC3_TRB_SIZE_MASK;
Felipe Balbi72246da2011-08-19 18:10:58 +03001907
1908 if (dep->direction) {
1909 if (count) {
Pratyush Anand73939b02012-05-25 18:54:56 +05301910 trb_status = DWC3_TRB_SIZE_TRBSTS(trb->size);
1911 if (trb_status == DWC3_TRBSTS_MISSED_ISOC) {
1912 dev_dbg(dwc->dev, "incomplete IN transfer %s\n",
1913 dep->name);
Pratyush Anand921b0b82013-01-14 15:59:32 +05301914 /*
1915 * If missed isoc occurred and there is
1916 * no request queued then issue END
1917 * TRANSFER, so that core generates
1918 * next xfernotready and we will issue
1919 * a fresh START TRANSFER.
1920 * If there are still queued request
1921 * then wait, do not issue either END
1922 * or UPDATE TRANSFER, just attach next
1923 * request in request_list during
1924 * giveback.If any future queued request
1925 * is successfully transferred then we
1926 * will issue UPDATE TRANSFER for all
1927 * request in the request_list.
1928 */
Pratyush Anand73939b02012-05-25 18:54:56 +05301929 dep->flags |= DWC3_EP_MISSED_ISOC;
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +05301930 dbg_event(dep->number, "MISSED ISOC",
1931 status);
Pratyush Anand73939b02012-05-25 18:54:56 +05301932 } else {
1933 dev_err(dwc->dev, "incomplete IN transfer %s\n",
1934 dep->name);
1935 status = -ECONNRESET;
1936 }
Pratyush Anand921b0b82013-01-14 15:59:32 +05301937 } else {
1938 dep->flags &= ~DWC3_EP_MISSED_ISOC;
Felipe Balbi72246da2011-08-19 18:10:58 +03001939 }
1940 } else {
1941 if (count && (event->status & DEPEVT_STATUS_SHORT))
1942 s_pkt = 1;
1943 }
1944
1945 /*
1946 * We assume here we will always receive the entire data block
1947 * which we should receive. Meaning, if we program RX to
1948 * receive 4K but we receive only 2K, we assume that's all we
1949 * should receive and we simply bounce the request back to the
1950 * gadget driver for further processing.
1951 */
1952 req->request.actual += req->request.length - count;
1953 dwc3_gadget_giveback(dep, req, status);
1954 if (s_pkt)
1955 break;
Felipe Balbif6bafc62012-02-06 11:04:53 +02001956 if ((event->status & DEPEVT_STATUS_LST) &&
Pratyush Anand413dba62012-06-03 19:43:19 +05301957 (trb->ctrl & (DWC3_TRB_CTRL_LST |
1958 DWC3_TRB_CTRL_HWO)))
Felipe Balbi72246da2011-08-19 18:10:58 +03001959 break;
Felipe Balbif6bafc62012-02-06 11:04:53 +02001960 if ((event->status & DEPEVT_STATUS_IOC) &&
1961 (trb->ctrl & DWC3_TRB_CTRL_IOC))
Felipe Balbi72246da2011-08-19 18:10:58 +03001962 break;
1963 } while (1);
1964
Pratyush Anand18bbcb02013-01-14 15:59:34 +05301965 if (usb_endpoint_xfer_isoc(dep->endpoint.desc) &&
1966 list_empty(&dep->req_queued)) {
Vijayavardhan Vennapusa91ba6532013-01-30 17:35:45 +05301967 if (list_empty(&dep->request_list))
Pratyush Anand18bbcb02013-01-14 15:59:34 +05301968 /*
1969 * If there is no entry in request list then do
1970 * not issue END TRANSFER now. Just set PENDING
1971 * flag, so that END TRANSFER is issued when an
1972 * entry is added into request list.
1973 */
Vijayavardhan Vennapusa91ba6532013-01-30 17:35:45 +05301974 dep->flags |= DWC3_EP_PENDING_REQUEST;
1975 else
Pratyush Anand18bbcb02013-01-14 15:59:34 +05301976 dwc3_stop_active_transfer(dwc, dep->number);
Vijayavardhan Vennapusa91ba6532013-01-30 17:35:45 +05301977 dep->flags &= ~DWC3_EP_MISSED_ISOC;
Pratyush Anand921b0b82013-01-14 15:59:32 +05301978 return 1;
1979 }
1980
Felipe Balbif6bafc62012-02-06 11:04:53 +02001981 if ((event->status & DEPEVT_STATUS_IOC) &&
1982 (trb->ctrl & DWC3_TRB_CTRL_IOC))
Felipe Balbi72246da2011-08-19 18:10:58 +03001983 return 0;
1984 return 1;
1985}
1986
1987static void dwc3_endpoint_transfer_complete(struct dwc3 *dwc,
1988 struct dwc3_ep *dep, const struct dwc3_event_depevt *event,
1989 int start_new)
1990{
1991 unsigned status = 0;
1992 int clean_busy;
1993
1994 if (event->status & DEPEVT_STATUS_BUSERR)
1995 status = -ECONNRESET;
1996
Paul Zimmerman1d046792012-02-15 18:56:56 -08001997 clean_busy = dwc3_cleanup_done_reqs(dwc, dep, event, status);
Paul Zimmermanc2df85c2012-02-24 17:32:18 -08001998 if (clean_busy)
Felipe Balbi72246da2011-08-19 18:10:58 +03001999 dep->flags &= ~DWC3_EP_BUSY;
Felipe Balbifae2b902011-10-14 13:00:30 +03002000
2001 /*
2002 * WORKAROUND: This is the 2nd half of U1/U2 -> U0 workaround.
2003 * See dwc3_gadget_linksts_change_interrupt() for 1st half.
2004 */
2005 if (dwc->revision < DWC3_REVISION_183A) {
2006 u32 reg;
2007 int i;
2008
2009 for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) {
Moiz Sonasatheed03f12012-08-01 14:08:30 -05002010 dep = dwc->eps[i];
Felipe Balbifae2b902011-10-14 13:00:30 +03002011
2012 if (!(dep->flags & DWC3_EP_ENABLED))
2013 continue;
2014
2015 if (!list_empty(&dep->req_queued))
2016 return;
2017 }
2018
2019 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2020 reg |= dwc->u1u2;
2021 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2022
2023 dwc->u1u2 = 0;
2024 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002025}
2026
Felipe Balbi72246da2011-08-19 18:10:58 +03002027static void dwc3_endpoint_interrupt(struct dwc3 *dwc,
2028 const struct dwc3_event_depevt *event)
2029{
2030 struct dwc3_ep *dep;
2031 u8 epnum = event->endpoint_number;
2032
2033 dep = dwc->eps[epnum];
2034
Felipe Balbia09be0a2012-06-06 09:19:35 +03002035 if (!(dep->flags & DWC3_EP_ENABLED))
2036 return;
2037
Felipe Balbi72246da2011-08-19 18:10:58 +03002038 dev_vdbg(dwc->dev, "%s: %s\n", dep->name,
2039 dwc3_ep_event_string(event->endpoint_event));
2040
2041 if (epnum == 0 || epnum == 1) {
2042 dwc3_ep0_interrupt(dwc, event);
2043 return;
2044 }
2045
2046 switch (event->endpoint_event) {
2047 case DWC3_DEPEVT_XFERCOMPLETE:
Felipe Balbi4959cfc2012-06-06 12:04:13 +03002048 dep->resource_index = 0;
Paul Zimmermanc2df85c2012-02-24 17:32:18 -08002049
Ido Shayevitz57cdac12012-03-12 20:25:24 +02002050 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbi72246da2011-08-19 18:10:58 +03002051 dev_dbg(dwc->dev, "%s is an Isochronous endpoint\n",
2052 dep->name);
2053 return;
2054 }
2055
2056 dwc3_endpoint_transfer_complete(dwc, dep, event, 1);
2057 break;
2058 case DWC3_DEPEVT_XFERINPROGRESS:
Ido Shayevitz57cdac12012-03-12 20:25:24 +02002059 if (!usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbi72246da2011-08-19 18:10:58 +03002060 dev_dbg(dwc->dev, "%s is not an Isochronous endpoint\n",
2061 dep->name);
2062 return;
2063 }
2064
2065 dwc3_endpoint_transfer_complete(dwc, dep, event, 0);
2066 break;
2067 case DWC3_DEPEVT_XFERNOTREADY:
Ido Shayevitz57cdac12012-03-12 20:25:24 +02002068 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbi72246da2011-08-19 18:10:58 +03002069 dwc3_gadget_start_isoc(dwc, dep, event);
2070 } else {
2071 int ret;
2072
2073 dev_vdbg(dwc->dev, "%s: reason %s\n",
Felipe Balbi40aa41f2012-01-18 17:06:03 +02002074 dep->name, event->status &
2075 DEPEVT_STATUS_TRANSFER_ACTIVE
Felipe Balbi72246da2011-08-19 18:10:58 +03002076 ? "Transfer Active"
2077 : "Transfer Not Active");
2078
2079 ret = __dwc3_gadget_kick_transfer(dep, 0, 1);
2080 if (!ret || ret == -EBUSY)
2081 return;
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +05302082 else
2083 dbg_event(dep->number, "QUEUE", ret);
Felipe Balbi72246da2011-08-19 18:10:58 +03002084
2085 dev_dbg(dwc->dev, "%s: failed to kick transfers\n",
2086 dep->name);
2087 }
2088
2089 break;
Felipe Balbi879631a2011-09-30 10:58:47 +03002090 case DWC3_DEPEVT_STREAMEVT:
Ido Shayevitz57cdac12012-03-12 20:25:24 +02002091 if (!usb_endpoint_xfer_bulk(dep->endpoint.desc)) {
Felipe Balbi879631a2011-09-30 10:58:47 +03002092 dev_err(dwc->dev, "Stream event for non-Bulk %s\n",
2093 dep->name);
2094 return;
2095 }
2096
2097 switch (event->status) {
2098 case DEPEVT_STREAMEVT_FOUND:
2099 dev_vdbg(dwc->dev, "Stream %d found and started\n",
2100 event->parameters);
2101
2102 break;
2103 case DEPEVT_STREAMEVT_NOTFOUND:
2104 /* FALLTHROUGH */
2105 default:
2106 dev_dbg(dwc->dev, "Couldn't find suitable stream\n");
2107 }
2108 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03002109 case DWC3_DEPEVT_RXTXFIFOEVT:
2110 dev_dbg(dwc->dev, "%s FIFO Overrun\n", dep->name);
2111 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03002112 case DWC3_DEPEVT_EPCMDCMPLT:
Felipe Balbib129eb72012-02-17 12:10:04 +02002113 dev_vdbg(dwc->dev, "Endpoint Command Complete\n");
Felipe Balbi72246da2011-08-19 18:10:58 +03002114 break;
2115 }
2116}
2117
2118static void dwc3_disconnect_gadget(struct dwc3 *dwc)
2119{
2120 if (dwc->gadget_driver && dwc->gadget_driver->disconnect) {
2121 spin_unlock(&dwc->lock);
2122 dwc->gadget_driver->disconnect(&dwc->gadget);
2123 spin_lock(&dwc->lock);
2124 }
2125}
2126
2127static void dwc3_stop_active_transfer(struct dwc3 *dwc, u32 epnum)
2128{
2129 struct dwc3_ep *dep;
2130 struct dwc3_gadget_ep_cmd_params params;
2131 u32 cmd;
2132 int ret;
2133
2134 dep = dwc->eps[epnum];
2135
Felipe Balbi4959cfc2012-06-06 12:04:13 +03002136 if (!dep->resource_index)
Pratyush Anand6263ebe2012-06-23 02:23:08 +05302137 return;
2138
Pratyush Anande67fdeb2012-07-06 15:19:10 +05302139 /*
2140 * NOTICE: We are violating what the Databook says about the
2141 * EndTransfer command. Ideally we would _always_ wait for the
2142 * EndTransfer Command Completion IRQ, but that's causing too
2143 * much trouble synchronizing between us and gadget driver.
2144 *
2145 * We have discussed this with the IP Provider and it was
2146 * suggested to giveback all requests here, but give HW some
2147 * extra time to synchronize with the interconnect. We're using
2148 * an arbitraty 100us delay for that.
2149 *
2150 * Note also that a similar handling was tested by Synopsys
2151 * (thanks a lot Paul) and nothing bad has come out of it.
2152 * In short, what we're doing is:
2153 *
2154 * - Issue EndTransfer WITH CMDIOC bit set
2155 * - Wait 100us
2156 */
2157
Pratyush Anand6263ebe2012-06-23 02:23:08 +05302158 cmd = DWC3_DEPCMD_ENDTRANSFER;
2159 cmd |= DWC3_DEPCMD_HIPRI_FORCERM | DWC3_DEPCMD_CMDIOC;
Felipe Balbi4959cfc2012-06-06 12:04:13 +03002160 cmd |= DWC3_DEPCMD_PARAM(dep->resource_index);
Pratyush Anand6263ebe2012-06-23 02:23:08 +05302161 memset(&params, 0, sizeof(params));
2162 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number, cmd, &params);
2163 WARN_ON_ONCE(ret);
Felipe Balbi4959cfc2012-06-06 12:04:13 +03002164 dep->resource_index = 0;
Pratyush Anande67fdeb2012-07-06 15:19:10 +05302165
2166 udelay(100);
Felipe Balbi72246da2011-08-19 18:10:58 +03002167}
2168
2169static void dwc3_stop_active_transfers(struct dwc3 *dwc)
2170{
2171 u32 epnum;
2172
2173 for (epnum = 2; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
2174 struct dwc3_ep *dep;
2175
2176 dep = dwc->eps[epnum];
2177 if (!(dep->flags & DWC3_EP_ENABLED))
2178 continue;
2179
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +02002180 dwc3_remove_requests(dwc, dep);
Felipe Balbi72246da2011-08-19 18:10:58 +03002181 }
2182}
2183
2184static void dwc3_clear_stall_all_ep(struct dwc3 *dwc)
2185{
2186 u32 epnum;
2187
2188 for (epnum = 1; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
2189 struct dwc3_ep *dep;
2190 struct dwc3_gadget_ep_cmd_params params;
2191 int ret;
2192
2193 dep = dwc->eps[epnum];
2194
2195 if (!(dep->flags & DWC3_EP_STALL))
2196 continue;
2197
2198 dep->flags &= ~DWC3_EP_STALL;
2199
2200 memset(&params, 0, sizeof(params));
2201 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number,
2202 DWC3_DEPCMD_CLEARSTALL, &params);
2203 WARN_ON_ONCE(ret);
2204 }
2205}
2206
2207static void dwc3_gadget_disconnect_interrupt(struct dwc3 *dwc)
2208{
Felipe Balbi34d548c2012-05-24 10:30:01 +03002209 int reg;
2210
Felipe Balbi72246da2011-08-19 18:10:58 +03002211 dev_vdbg(dwc->dev, "%s\n", __func__);
Felipe Balbi72246da2011-08-19 18:10:58 +03002212
2213 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2214 reg &= ~DWC3_DCTL_INITU1ENA;
2215 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2216
2217 reg &= ~DWC3_DCTL_INITU2ENA;
2218 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +03002219
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +05302220 dbg_event(0xFF, "DISCONNECT", 0);
Felipe Balbi72246da2011-08-19 18:10:58 +03002221 dwc3_disconnect_gadget(dwc);
Paul Zimmermanb23c8432011-09-30 10:58:42 +03002222 dwc->start_config_issued = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03002223
2224 dwc->gadget.speed = USB_SPEED_UNKNOWN;
Felipe Balbidf62df52011-10-14 15:11:49 +03002225 dwc->setup_packet_pending = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03002226}
2227
Paul Zimmermandffb81b2012-04-27 12:54:05 +03002228static void dwc3_gadget_usb3_phy_suspend(struct dwc3 *dwc, int suspend)
Felipe Balbi72246da2011-08-19 18:10:58 +03002229{
2230 u32 reg;
2231
2232 reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0));
2233
Paul Zimmermandffb81b2012-04-27 12:54:05 +03002234 if (suspend)
Felipe Balbi72246da2011-08-19 18:10:58 +03002235 reg |= DWC3_GUSB3PIPECTL_SUSPHY;
Paul Zimmermandffb81b2012-04-27 12:54:05 +03002236 else
2237 reg &= ~DWC3_GUSB3PIPECTL_SUSPHY;
Felipe Balbi72246da2011-08-19 18:10:58 +03002238
2239 dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg);
2240}
2241
Paul Zimmermandffb81b2012-04-27 12:54:05 +03002242static void dwc3_gadget_usb2_phy_suspend(struct dwc3 *dwc, int suspend)
Felipe Balbi72246da2011-08-19 18:10:58 +03002243{
2244 u32 reg;
2245
2246 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
2247
Paul Zimmermandffb81b2012-04-27 12:54:05 +03002248 if (suspend)
Felipe Balbi72246da2011-08-19 18:10:58 +03002249 reg |= DWC3_GUSB2PHYCFG_SUSPHY;
Paul Zimmermandffb81b2012-04-27 12:54:05 +03002250 else
2251 reg &= ~DWC3_GUSB2PHYCFG_SUSPHY;
Felipe Balbi72246da2011-08-19 18:10:58 +03002252
2253 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
2254}
2255
2256static void dwc3_gadget_reset_interrupt(struct dwc3 *dwc)
2257{
2258 u32 reg;
Vijayavardhan Vennapusa908f1ed2012-10-19 19:51:48 +05302259 struct dwc3_otg *dotg = dwc->dotg;
Felipe Balbi72246da2011-08-19 18:10:58 +03002260
2261 dev_vdbg(dwc->dev, "%s\n", __func__);
2262
Felipe Balbidf62df52011-10-14 15:11:49 +03002263 /*
2264 * WORKAROUND: DWC3 revisions <1.88a have an issue which
2265 * would cause a missing Disconnect Event if there's a
2266 * pending Setup Packet in the FIFO.
2267 *
2268 * There's no suggested workaround on the official Bug
2269 * report, which states that "unless the driver/application
2270 * is doing any special handling of a disconnect event,
2271 * there is no functional issue".
2272 *
2273 * Unfortunately, it turns out that we _do_ some special
2274 * handling of a disconnect event, namely complete all
2275 * pending transfers, notify gadget driver of the
2276 * disconnection, and so on.
2277 *
2278 * Our suggested workaround is to follow the Disconnect
2279 * Event steps here, instead, based on a setup_packet_pending
2280 * flag. Such flag gets set whenever we have a XferNotReady
2281 * event on EP0 and gets cleared on XferComplete for the
2282 * same endpoint.
2283 *
2284 * Refers to:
2285 *
2286 * STAR#9000466709: RTL: Device : Disconnect event not
2287 * generated if setup packet pending in FIFO
2288 */
2289 if (dwc->revision < DWC3_REVISION_188A) {
2290 if (dwc->setup_packet_pending)
2291 dwc3_gadget_disconnect_interrupt(dwc);
2292 }
2293
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +05302294 dbg_event(0xFF, "BUS RST", 0);
Felipe Balbi961906e2011-12-20 15:37:21 +02002295 /* after reset -> Default State */
2296 dwc->dev_state = DWC3_DEFAULT_STATE;
2297
Paul Zimmerman88df4272012-04-27 13:10:52 +03002298 /* Recent versions support automatic phy suspend and don't need this */
2299 if (dwc->revision < DWC3_REVISION_194A) {
2300 /* Resume PHYs */
2301 dwc3_gadget_usb2_phy_suspend(dwc, false);
2302 dwc3_gadget_usb3_phy_suspend(dwc, false);
2303 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002304
Vijayavardhan Vennapusa908f1ed2012-10-19 19:51:48 +05302305 if (dotg && dotg->otg.phy)
2306 usb_phy_set_power(dotg->otg.phy, 0);
2307
Felipe Balbi72246da2011-08-19 18:10:58 +03002308 if (dwc->gadget.speed != USB_SPEED_UNKNOWN)
2309 dwc3_disconnect_gadget(dwc);
2310
2311 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2312 reg &= ~DWC3_DCTL_TSTCTRL_MASK;
2313 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Gerard Cauvy3b637362012-02-10 12:21:18 +02002314 dwc->test_mode = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03002315
2316 dwc3_stop_active_transfers(dwc);
2317 dwc3_clear_stall_all_ep(dwc);
Paul Zimmermanb23c8432011-09-30 10:58:42 +03002318 dwc->start_config_issued = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03002319
2320 /* Reset device address to zero */
2321 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2322 reg &= ~(DWC3_DCFG_DEVADDR_MASK);
2323 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +03002324}
2325
2326static void dwc3_update_ram_clk_sel(struct dwc3 *dwc, u32 speed)
2327{
2328 u32 reg;
2329 u32 usb30_clock = DWC3_GCTL_CLK_BUS;
2330
2331 /*
2332 * We change the clock only at SS but I dunno why I would want to do
2333 * this. Maybe it becomes part of the power saving plan.
2334 */
2335
2336 if (speed != DWC3_DSTS_SUPERSPEED)
2337 return;
2338
2339 /*
2340 * RAMClkSel is reset to 0 after USB reset, so it must be reprogrammed
2341 * each time on Connect Done.
2342 */
2343 if (!usb30_clock)
2344 return;
2345
2346 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
2347 reg |= DWC3_GCTL_RAMCLKSEL(usb30_clock);
2348 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
2349}
2350
Paul Zimmermandffb81b2012-04-27 12:54:05 +03002351static void dwc3_gadget_phy_suspend(struct dwc3 *dwc, u8 speed)
Felipe Balbi72246da2011-08-19 18:10:58 +03002352{
2353 switch (speed) {
2354 case USB_SPEED_SUPER:
Paul Zimmermandffb81b2012-04-27 12:54:05 +03002355 dwc3_gadget_usb2_phy_suspend(dwc, true);
Felipe Balbi72246da2011-08-19 18:10:58 +03002356 break;
2357 case USB_SPEED_HIGH:
2358 case USB_SPEED_FULL:
2359 case USB_SPEED_LOW:
Paul Zimmermandffb81b2012-04-27 12:54:05 +03002360 dwc3_gadget_usb3_phy_suspend(dwc, true);
Felipe Balbi72246da2011-08-19 18:10:58 +03002361 break;
2362 }
2363}
2364
2365static void dwc3_gadget_conndone_interrupt(struct dwc3 *dwc)
2366{
2367 struct dwc3_gadget_ep_cmd_params params;
2368 struct dwc3_ep *dep;
2369 int ret;
2370 u32 reg;
2371 u8 speed;
2372
2373 dev_vdbg(dwc->dev, "%s\n", __func__);
2374
2375 memset(&params, 0x00, sizeof(params));
2376
Felipe Balbi72246da2011-08-19 18:10:58 +03002377 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
2378 speed = reg & DWC3_DSTS_CONNECTSPD;
2379 dwc->speed = speed;
2380
2381 dwc3_update_ram_clk_sel(dwc, speed);
2382
2383 switch (speed) {
2384 case DWC3_DCFG_SUPERSPEED:
Felipe Balbi05870c52011-10-14 14:51:38 +03002385 /*
2386 * WORKAROUND: DWC3 revisions <1.90a have an issue which
2387 * would cause a missing USB3 Reset event.
2388 *
2389 * In such situations, we should force a USB3 Reset
2390 * event by calling our dwc3_gadget_reset_interrupt()
2391 * routine.
2392 *
2393 * Refers to:
2394 *
2395 * STAR#9000483510: RTL: SS : USB3 reset event may
2396 * not be generated always when the link enters poll
2397 */
2398 if (dwc->revision < DWC3_REVISION_190A)
2399 dwc3_gadget_reset_interrupt(dwc);
2400
Felipe Balbi72246da2011-08-19 18:10:58 +03002401 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
2402 dwc->gadget.ep0->maxpacket = 512;
2403 dwc->gadget.speed = USB_SPEED_SUPER;
2404 break;
2405 case DWC3_DCFG_HIGHSPEED:
2406 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
2407 dwc->gadget.ep0->maxpacket = 64;
2408 dwc->gadget.speed = USB_SPEED_HIGH;
2409 break;
2410 case DWC3_DCFG_FULLSPEED2:
2411 case DWC3_DCFG_FULLSPEED1:
2412 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
2413 dwc->gadget.ep0->maxpacket = 64;
2414 dwc->gadget.speed = USB_SPEED_FULL;
2415 break;
2416 case DWC3_DCFG_LOWSPEED:
2417 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(8);
2418 dwc->gadget.ep0->maxpacket = 8;
2419 dwc->gadget.speed = USB_SPEED_LOW;
2420 break;
2421 }
2422
Paul Zimmerman88df4272012-04-27 13:10:52 +03002423 /* Recent versions support automatic phy suspend and don't need this */
2424 if (dwc->revision < DWC3_REVISION_194A) {
2425 /* Suspend unneeded PHY */
2426 dwc3_gadget_phy_suspend(dwc, dwc->gadget.speed);
2427 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002428
2429 dep = dwc->eps[0];
Felipe Balbi07e0ee82012-07-16 14:08:16 +03002430 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, true);
Felipe Balbi72246da2011-08-19 18:10:58 +03002431 if (ret) {
2432 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
2433 return;
2434 }
2435
2436 dep = dwc->eps[1];
Felipe Balbi07e0ee82012-07-16 14:08:16 +03002437 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, true);
Felipe Balbi72246da2011-08-19 18:10:58 +03002438 if (ret) {
2439 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
2440 return;
2441 }
2442
2443 /*
2444 * Configure PHY via GUSB3PIPECTLn if required.
2445 *
2446 * Update GTXFIFOSIZn
2447 *
2448 * In both cases reset values should be sufficient.
2449 */
2450}
2451
2452static void dwc3_gadget_wakeup_interrupt(struct dwc3 *dwc)
2453{
2454 dev_vdbg(dwc->dev, "%s\n", __func__);
2455
2456 /*
2457 * TODO take core out of low power mode when that's
2458 * implemented.
2459 */
2460
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +05302461 dbg_event(0xFF, "WAKEUP", 0);
Felipe Balbi72246da2011-08-19 18:10:58 +03002462 dwc->gadget_driver->resume(&dwc->gadget);
2463}
2464
2465static void dwc3_gadget_linksts_change_interrupt(struct dwc3 *dwc,
2466 unsigned int evtinfo)
2467{
Felipe Balbifae2b902011-10-14 13:00:30 +03002468 enum dwc3_link_state next = evtinfo & DWC3_LINK_STATE_MASK;
2469
2470 /*
2471 * WORKAROUND: DWC3 Revisions <1.83a have an issue which, depending
2472 * on the link partner, the USB session might do multiple entry/exit
2473 * of low power states before a transfer takes place.
2474 *
2475 * Due to this problem, we might experience lower throughput. The
2476 * suggested workaround is to disable DCTL[12:9] bits if we're
2477 * transitioning from U1/U2 to U0 and enable those bits again
2478 * after a transfer completes and there are no pending transfers
2479 * on any of the enabled endpoints.
2480 *
2481 * This is the first half of that workaround.
2482 *
2483 * Refers to:
2484 *
2485 * STAR#9000446952: RTL: Device SS : if U1/U2 ->U0 takes >128us
2486 * core send LGO_Ux entering U0
2487 */
2488 if (dwc->revision < DWC3_REVISION_183A) {
2489 if (next == DWC3_LINK_STATE_U0) {
2490 u32 u1u2;
2491 u32 reg;
2492
2493 switch (dwc->link_state) {
2494 case DWC3_LINK_STATE_U1:
2495 case DWC3_LINK_STATE_U2:
2496 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2497 u1u2 = reg & (DWC3_DCTL_INITU2ENA
2498 | DWC3_DCTL_ACCEPTU2ENA
2499 | DWC3_DCTL_INITU1ENA
2500 | DWC3_DCTL_ACCEPTU1ENA);
2501
2502 if (!dwc->u1u2)
2503 dwc->u1u2 = reg & u1u2;
2504
2505 reg &= ~u1u2;
2506
2507 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2508 break;
2509 default:
2510 /* do nothing */
2511 break;
2512 }
2513 }
2514 }
2515
Vijayavardhan Vennapusa54be1d62012-10-06 18:32:06 +05302516 if (next == DWC3_LINK_STATE_U0) {
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +05302517 if (dwc->link_state == DWC3_LINK_STATE_U3) {
2518 dbg_event(0xFF, "RESUME", 0);
Vijayavardhan Vennapusa54be1d62012-10-06 18:32:06 +05302519 dwc->gadget_driver->resume(&dwc->gadget);
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +05302520 }
Vijayavardhan Vennapusa54be1d62012-10-06 18:32:06 +05302521 } else if (next == DWC3_LINK_STATE_U3) {
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +05302522 dbg_event(0xFF, "SUSPEND", 0);
Vijayavardhan Vennapusa54be1d62012-10-06 18:32:06 +05302523 dwc->gadget_driver->suspend(&dwc->gadget);
2524 }
2525
Felipe Balbifae2b902011-10-14 13:00:30 +03002526 dwc->link_state = next;
Felipe Balbi019ac832011-09-08 21:18:47 +03002527
2528 dev_vdbg(dwc->dev, "%s link %d\n", __func__, dwc->link_state);
Felipe Balbi72246da2011-08-19 18:10:58 +03002529}
2530
2531static void dwc3_gadget_interrupt(struct dwc3 *dwc,
2532 const struct dwc3_event_devt *event)
2533{
2534 switch (event->type) {
2535 case DWC3_DEVICE_EVENT_DISCONNECT:
2536 dwc3_gadget_disconnect_interrupt(dwc);
2537 break;
2538 case DWC3_DEVICE_EVENT_RESET:
2539 dwc3_gadget_reset_interrupt(dwc);
2540 break;
2541 case DWC3_DEVICE_EVENT_CONNECT_DONE:
2542 dwc3_gadget_conndone_interrupt(dwc);
2543 break;
2544 case DWC3_DEVICE_EVENT_WAKEUP:
2545 dwc3_gadget_wakeup_interrupt(dwc);
2546 break;
2547 case DWC3_DEVICE_EVENT_LINK_STATUS_CHANGE:
2548 dwc3_gadget_linksts_change_interrupt(dwc, event->event_info);
2549 break;
2550 case DWC3_DEVICE_EVENT_EOPF:
2551 dev_vdbg(dwc->dev, "End of Periodic Frame\n");
2552 break;
2553 case DWC3_DEVICE_EVENT_SOF:
2554 dev_vdbg(dwc->dev, "Start of Periodic Frame\n");
2555 break;
2556 case DWC3_DEVICE_EVENT_ERRATIC_ERROR:
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +05302557 dbg_event(0xFF, "ERROR", 0);
Felipe Balbi72246da2011-08-19 18:10:58 +03002558 dev_vdbg(dwc->dev, "Erratic Error\n");
2559 break;
2560 case DWC3_DEVICE_EVENT_CMD_CMPL:
2561 dev_vdbg(dwc->dev, "Command Complete\n");
2562 break;
2563 case DWC3_DEVICE_EVENT_OVERFLOW:
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +05302564 dbg_event(0xFF, "OVERFL", 0);
Felipe Balbi72246da2011-08-19 18:10:58 +03002565 dev_vdbg(dwc->dev, "Overflow\n");
Pavankumar Kondetid393e172012-06-12 16:07:29 +05302566 /*
2567 * Controllers prior to 2.30a revision has a bug where
2568 * Overflow Event may overwrite an unacknowledged event
2569 * in the event buffer. The severity of the issue depends
2570 * on the overwritten event type. Add a warning message
2571 * saying that an event is overwritten.
2572 *
2573 * TODO: In future we may need to see if we can re-enumerate
2574 * with host.
2575 */
2576 if (dwc->revision < DWC3_REVISION_230A)
2577 dev_warn(dwc->dev, "Unacknowledged event overwritten\n");
Felipe Balbi72246da2011-08-19 18:10:58 +03002578 break;
Pavankumar Kondeti33fe6f12012-06-12 16:21:46 +05302579 case DWC3_DEVICE_EVENT_VENDOR_DEV_TEST_LMP:
2580 /*
2581 * Controllers prior to 2.30a revision has a bug, due to which
2582 * a vendor device test LMP event can not be filtered. But
2583 * this event is not handled in the current code. This is a
2584 * special event and 8 bytes of data will follow the event.
2585 * Handling this event is tricky when event buffer is almost
2586 * full. Moreover this event will not occur in normal scenario
2587 * and can only happen with special hosts in testing scenarios.
2588 * Add a warning message to indicate that this event is received
2589 * which means that event buffer might have corrupted.
2590 */
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +05302591 dbg_event(0xFF, "TSTLMP", 0);
Pavankumar Kondeti33fe6f12012-06-12 16:21:46 +05302592 if (dwc->revision < DWC3_REVISION_230A)
2593 dev_warn(dwc->dev, "Vendor Device Test LMP Received\n");
2594 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03002595 default:
2596 dev_dbg(dwc->dev, "UNKNOWN IRQ %d\n", event->type);
2597 }
2598}
2599
2600static void dwc3_process_event_entry(struct dwc3 *dwc,
2601 const union dwc3_event *event)
2602{
2603 /* Endpoint IRQ, handle it and return early */
2604 if (event->type.is_devspec == 0) {
2605 /* depevt */
2606 return dwc3_endpoint_interrupt(dwc, &event->depevt);
2607 }
2608
2609 switch (event->type.type) {
2610 case DWC3_EVENT_TYPE_DEV:
2611 dwc3_gadget_interrupt(dwc, &event->devt);
2612 break;
2613 /* REVISIT what to do with Carkit and I2C events ? */
2614 default:
2615 dev_err(dwc->dev, "UNKNOWN IRQ type %d\n", event->raw);
2616 }
2617}
2618
2619static irqreturn_t dwc3_process_event_buf(struct dwc3 *dwc, u32 buf)
2620{
2621 struct dwc3_event_buffer *evt;
2622 int left;
2623 u32 count;
2624
2625 count = dwc3_readl(dwc->regs, DWC3_GEVNTCOUNT(buf));
2626 count &= DWC3_GEVNTCOUNT_MASK;
2627 if (!count)
2628 return IRQ_NONE;
2629
2630 evt = dwc->ev_buffs[buf];
2631 left = count;
2632
2633 while (left > 0) {
2634 union dwc3_event event;
2635
Felipe Balbid70d8442012-02-06 13:40:17 +02002636 event.raw = *(u32 *) (evt->buf + evt->lpos);
2637
Felipe Balbi72246da2011-08-19 18:10:58 +03002638 dwc3_process_event_entry(dwc, &event);
2639 /*
2640 * XXX we wrap around correctly to the next entry as almost all
2641 * entries are 4 bytes in size. There is one entry which has 12
2642 * bytes which is a regular entry followed by 8 bytes data. ATM
2643 * I don't know how things are organized if were get next to the
2644 * a boundary so I worry about that once we try to handle that.
2645 */
2646 evt->lpos = (evt->lpos + 4) % DWC3_EVENT_BUFFERS_SIZE;
2647 left -= 4;
2648
2649 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(buf), 4);
2650 }
2651
2652 return IRQ_HANDLED;
2653}
2654
2655static irqreturn_t dwc3_interrupt(int irq, void *_dwc)
2656{
2657 struct dwc3 *dwc = _dwc;
2658 int i;
2659 irqreturn_t ret = IRQ_NONE;
2660
2661 spin_lock(&dwc->lock);
2662
Felipe Balbi9f622b22011-10-12 10:31:04 +03002663 for (i = 0; i < dwc->num_event_buffers; i++) {
Felipe Balbi72246da2011-08-19 18:10:58 +03002664 irqreturn_t status;
2665
2666 status = dwc3_process_event_buf(dwc, i);
2667 if (status == IRQ_HANDLED)
2668 ret = status;
2669 }
2670
2671 spin_unlock(&dwc->lock);
2672
2673 return ret;
2674}
2675
2676/**
2677 * dwc3_gadget_init - Initializes gadget related registers
Paul Zimmerman1d046792012-02-15 18:56:56 -08002678 * @dwc: pointer to our controller context structure
Felipe Balbi72246da2011-08-19 18:10:58 +03002679 *
2680 * Returns 0 on success otherwise negative errno.
2681 */
2682int __devinit dwc3_gadget_init(struct dwc3 *dwc)
2683{
2684 u32 reg;
2685 int ret;
2686 int irq;
2687
2688 dwc->ctrl_req = dma_alloc_coherent(dwc->dev, sizeof(*dwc->ctrl_req),
2689 &dwc->ctrl_req_addr, GFP_KERNEL);
2690 if (!dwc->ctrl_req) {
2691 dev_err(dwc->dev, "failed to allocate ctrl request\n");
2692 ret = -ENOMEM;
2693 goto err0;
2694 }
2695
2696 dwc->ep0_trb = dma_alloc_coherent(dwc->dev, sizeof(*dwc->ep0_trb),
2697 &dwc->ep0_trb_addr, GFP_KERNEL);
2698 if (!dwc->ep0_trb) {
2699 dev_err(dwc->dev, "failed to allocate ep0 trb\n");
2700 ret = -ENOMEM;
2701 goto err1;
2702 }
2703
Felipe Balbib0791fb2012-05-04 12:58:14 +03002704 dwc->setup_buf = kzalloc(DWC3_EP0_BOUNCE_SIZE, GFP_KERNEL);
Felipe Balbi72246da2011-08-19 18:10:58 +03002705 if (!dwc->setup_buf) {
2706 dev_err(dwc->dev, "failed to allocate setup buffer\n");
2707 ret = -ENOMEM;
2708 goto err2;
2709 }
2710
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002711 dwc->ep0_bounce = dma_alloc_coherent(dwc->dev,
Felipe Balbib0791fb2012-05-04 12:58:14 +03002712 DWC3_EP0_BOUNCE_SIZE, &dwc->ep0_bounce_addr,
2713 GFP_KERNEL);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002714 if (!dwc->ep0_bounce) {
2715 dev_err(dwc->dev, "failed to allocate ep0 bounce buffer\n");
2716 ret = -ENOMEM;
2717 goto err3;
2718 }
2719
Felipe Balbi72246da2011-08-19 18:10:58 +03002720 dev_set_name(&dwc->gadget.dev, "gadget");
2721
2722 dwc->gadget.ops = &dwc3_gadget_ops;
Manu Gautama7b082a2012-11-06 09:50:09 +05302723 dwc->gadget.max_speed = USB_SPEED_SUPER;
Felipe Balbi72246da2011-08-19 18:10:58 +03002724 dwc->gadget.speed = USB_SPEED_UNKNOWN;
2725 dwc->gadget.dev.parent = dwc->dev;
Felipe Balbieeb720f2011-11-28 12:46:59 +02002726 dwc->gadget.sg_supported = true;
Felipe Balbi72246da2011-08-19 18:10:58 +03002727
2728 dma_set_coherent_mask(&dwc->gadget.dev, dwc->dev->coherent_dma_mask);
2729
2730 dwc->gadget.dev.dma_parms = dwc->dev->dma_parms;
2731 dwc->gadget.dev.dma_mask = dwc->dev->dma_mask;
2732 dwc->gadget.dev.release = dwc3_gadget_release;
2733 dwc->gadget.name = "dwc3-gadget";
2734
2735 /*
2736 * REVISIT: Here we should clear all pending IRQs to be
2737 * sure we're starting from a well known location.
2738 */
2739
2740 ret = dwc3_gadget_init_endpoints(dwc);
2741 if (ret)
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002742 goto err4;
Felipe Balbi72246da2011-08-19 18:10:58 +03002743
2744 irq = platform_get_irq(to_platform_device(dwc->dev), 0);
2745
2746 ret = request_irq(irq, dwc3_interrupt, IRQF_SHARED,
2747 "dwc3", dwc);
2748 if (ret) {
2749 dev_err(dwc->dev, "failed to request irq #%d --> %d\n",
2750 irq, ret);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002751 goto err5;
Felipe Balbi72246da2011-08-19 18:10:58 +03002752 }
2753
Sebastian Andrzej Siewiorbb8b8a32011-09-13 17:54:39 +02002754 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2755 reg |= DWC3_DCFG_LPM_CAP;
2756 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
2757
Felipe Balbi72246da2011-08-19 18:10:58 +03002758 /* Enable all but Start and End of Frame IRQs */
Pavankumar Kondeti33fe6f12012-06-12 16:21:46 +05302759 reg = (DWC3_DEVTEN_EVNTOVERFLOWEN |
Felipe Balbi72246da2011-08-19 18:10:58 +03002760 DWC3_DEVTEN_CMDCMPLTEN |
2761 DWC3_DEVTEN_ERRTICERREN |
2762 DWC3_DEVTEN_WKUPEVTEN |
2763 DWC3_DEVTEN_ULSTCNGEN |
2764 DWC3_DEVTEN_CONNECTDONEEN |
2765 DWC3_DEVTEN_USBRSTEN |
2766 DWC3_DEVTEN_DISCONNEVTEN);
2767 dwc3_writel(dwc->regs, DWC3_DEVTEN, reg);
2768
Paul Zimmerman88df4272012-04-27 13:10:52 +03002769 /* Enable USB2 LPM and automatic phy suspend only on recent versions */
2770 if (dwc->revision >= DWC3_REVISION_194A) {
2771 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2772 reg |= DWC3_DCFG_LPM_CAP;
2773 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
2774
2775 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2776 reg &= ~(DWC3_DCTL_HIRD_THRES_MASK | DWC3_DCTL_L1_HIBER_EN);
2777
2778 /* TODO: This should be configurable */
Pratyush Anandd69dcdd2012-07-02 10:21:52 +05302779 reg |= DWC3_DCTL_HIRD_THRES(28);
Paul Zimmerman88df4272012-04-27 13:10:52 +03002780
2781 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2782
Pratyush Anand50ed8342012-06-06 19:36:17 +05302783 dwc3_gadget_usb2_phy_suspend(dwc, false);
2784 dwc3_gadget_usb3_phy_suspend(dwc, false);
Paul Zimmerman88df4272012-04-27 13:10:52 +03002785 }
2786
Felipe Balbi72246da2011-08-19 18:10:58 +03002787 ret = device_register(&dwc->gadget.dev);
2788 if (ret) {
2789 dev_err(dwc->dev, "failed to register gadget device\n");
2790 put_device(&dwc->gadget.dev);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002791 goto err6;
Felipe Balbi72246da2011-08-19 18:10:58 +03002792 }
2793
2794 ret = usb_add_gadget_udc(dwc->dev, &dwc->gadget);
2795 if (ret) {
2796 dev_err(dwc->dev, "failed to register udc\n");
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002797 goto err7;
Felipe Balbi72246da2011-08-19 18:10:58 +03002798 }
2799
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +02002800 if (dwc->dotg) {
2801 /* dwc3 otg driver is active (DRD mode + SRPSupport=1) */
2802 ret = otg_set_peripheral(&dwc->dotg->otg, &dwc->gadget);
2803 if (ret) {
2804 dev_err(dwc->dev, "failed to set peripheral to otg\n");
2805 goto err7;
2806 }
Manu Gautamb5067272012-07-02 09:53:41 +05302807 } else {
2808 pm_runtime_no_callbacks(&dwc->gadget.dev);
2809 pm_runtime_set_active(&dwc->gadget.dev);
2810 pm_runtime_enable(&dwc->gadget.dev);
2811 pm_runtime_get(&dwc->gadget.dev);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +02002812 }
2813
Felipe Balbi72246da2011-08-19 18:10:58 +03002814 return 0;
2815
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002816err7:
Felipe Balbi72246da2011-08-19 18:10:58 +03002817 device_unregister(&dwc->gadget.dev);
2818
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002819err6:
Felipe Balbi72246da2011-08-19 18:10:58 +03002820 dwc3_writel(dwc->regs, DWC3_DEVTEN, 0x00);
2821 free_irq(irq, dwc);
2822
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002823err5:
Felipe Balbi72246da2011-08-19 18:10:58 +03002824 dwc3_gadget_free_endpoints(dwc);
2825
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002826err4:
Felipe Balbib0791fb2012-05-04 12:58:14 +03002827 dma_free_coherent(dwc->dev, DWC3_EP0_BOUNCE_SIZE,
2828 dwc->ep0_bounce, dwc->ep0_bounce_addr);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002829
Felipe Balbi72246da2011-08-19 18:10:58 +03002830err3:
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02002831 kfree(dwc->setup_buf);
Felipe Balbi72246da2011-08-19 18:10:58 +03002832
2833err2:
2834 dma_free_coherent(dwc->dev, sizeof(*dwc->ep0_trb),
2835 dwc->ep0_trb, dwc->ep0_trb_addr);
2836
2837err1:
2838 dma_free_coherent(dwc->dev, sizeof(*dwc->ctrl_req),
2839 dwc->ctrl_req, dwc->ctrl_req_addr);
2840
2841err0:
2842 return ret;
2843}
2844
2845void dwc3_gadget_exit(struct dwc3 *dwc)
2846{
2847 int irq;
Felipe Balbi72246da2011-08-19 18:10:58 +03002848
Manu Gautamb5067272012-07-02 09:53:41 +05302849 if (dwc->dotg) {
2850 pm_runtime_put(&dwc->gadget.dev);
2851 pm_runtime_disable(&dwc->gadget.dev);
2852 }
2853
Felipe Balbi72246da2011-08-19 18:10:58 +03002854 usb_del_gadget_udc(&dwc->gadget);
2855 irq = platform_get_irq(to_platform_device(dwc->dev), 0);
2856
2857 dwc3_writel(dwc->regs, DWC3_DEVTEN, 0x00);
2858 free_irq(irq, dwc);
2859
Felipe Balbi72246da2011-08-19 18:10:58 +03002860 dwc3_gadget_free_endpoints(dwc);
2861
Felipe Balbib0791fb2012-05-04 12:58:14 +03002862 dma_free_coherent(dwc->dev, DWC3_EP0_BOUNCE_SIZE,
2863 dwc->ep0_bounce, dwc->ep0_bounce_addr);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002864
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02002865 kfree(dwc->setup_buf);
Felipe Balbi72246da2011-08-19 18:10:58 +03002866
2867 dma_free_coherent(dwc->dev, sizeof(*dwc->ep0_trb),
2868 dwc->ep0_trb, dwc->ep0_trb_addr);
2869
2870 dma_free_coherent(dwc->dev, sizeof(*dwc->ctrl_req),
2871 dwc->ctrl_req, dwc->ctrl_req_addr);
2872
2873 device_unregister(&dwc->gadget.dev);
2874}