blob: 7672bc963c02a5e64856151a9228a9166dd99117 [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"
56#include "io.h"
57
Felipe Balbi04a9bfc2012-01-02 18:25:43 +020058/**
59 * dwc3_gadget_set_test_mode - Enables USB2 Test Modes
60 * @dwc: pointer to our context structure
61 * @mode: the mode to set (J, K SE0 NAK, Force Enable)
62 *
63 * Caller should take care of locking. This function will
64 * return 0 on success or -EINVAL if wrong Test Selector
65 * is passed
66 */
67int dwc3_gadget_set_test_mode(struct dwc3 *dwc, int mode)
68{
69 u32 reg;
70
71 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
72 reg &= ~DWC3_DCTL_TSTCTRL_MASK;
73
74 switch (mode) {
75 case TEST_J:
76 case TEST_K:
77 case TEST_SE0_NAK:
78 case TEST_PACKET:
79 case TEST_FORCE_EN:
80 reg |= mode << 1;
81 break;
82 default:
83 return -EINVAL;
84 }
85
86 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
87
88 return 0;
89}
90
Felipe Balbi8598bde2012-01-02 18:55:57 +020091/**
92 * dwc3_gadget_set_link_state - Sets USB Link to a particular State
93 * @dwc: pointer to our context structure
94 * @state: the state to put link into
95 *
96 * Caller should take care of locking. This function will
Paul Zimmermanaee63e32012-02-24 17:32:15 -080097 * return 0 on success or -ETIMEDOUT.
Felipe Balbi8598bde2012-01-02 18:55:57 +020098 */
99int dwc3_gadget_set_link_state(struct dwc3 *dwc, enum dwc3_link_state state)
100{
Paul Zimmermanaee63e32012-02-24 17:32:15 -0800101 int retries = 10000;
Felipe Balbi8598bde2012-01-02 18:55:57 +0200102 u32 reg;
103
Paul Zimmerman88df4272012-04-27 13:10:52 +0300104 /*
105 * Wait until device controller is ready. Only applies to 1.94a and
106 * later RTL.
107 */
108 if (dwc->revision >= DWC3_REVISION_194A) {
109 while (--retries) {
110 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
111 if (reg & DWC3_DSTS_DCNRD)
112 udelay(5);
113 else
114 break;
115 }
116
117 if (retries <= 0)
118 return -ETIMEDOUT;
119 }
120
Felipe Balbi8598bde2012-01-02 18:55:57 +0200121 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
122 reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK;
123
124 /* set requested state */
125 reg |= DWC3_DCTL_ULSTCHNGREQ(state);
126 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
127
Paul Zimmerman88df4272012-04-27 13:10:52 +0300128 /*
129 * The following code is racy when called from dwc3_gadget_wakeup,
130 * and is not needed, at least on newer versions
131 */
132 if (dwc->revision >= DWC3_REVISION_194A)
133 return 0;
134
Felipe Balbi8598bde2012-01-02 18:55:57 +0200135 /* wait for a change in DSTS */
Paul Zimmerman8b9388f2012-04-27 12:52:01 +0300136 retries = 10000;
Felipe Balbi8598bde2012-01-02 18:55:57 +0200137 while (--retries) {
138 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
139
Felipe Balbi8598bde2012-01-02 18:55:57 +0200140 if (DWC3_DSTS_USBLNKST(reg) == state)
141 return 0;
142
Paul Zimmermanaee63e32012-02-24 17:32:15 -0800143 udelay(5);
Felipe Balbi8598bde2012-01-02 18:55:57 +0200144 }
145
146 dev_vdbg(dwc->dev, "link state change request timed out\n");
147
148 return -ETIMEDOUT;
149}
150
Felipe Balbi457e84b2012-01-18 18:04:09 +0200151/**
152 * dwc3_gadget_resize_tx_fifos - reallocate fifo spaces for current use-case
153 * @dwc: pointer to our context structure
154 *
155 * This function will a best effort FIFO allocation in order
156 * to improve FIFO usage and throughput, while still allowing
157 * us to enable as many endpoints as possible.
158 *
159 * Keep in mind that this operation will be highly dependent
160 * on the configured size for RAM1 - which contains TxFifo -,
161 * the amount of endpoints enabled on coreConsultant tool, and
162 * the width of the Master Bus.
163 *
164 * In the ideal world, we would always be able to satisfy the
165 * following equation:
166 *
167 * ((512 + 2 * MDWIDTH-Bytes) + (Number of IN Endpoints - 1) * \
168 * (3 * (1024 + MDWIDTH-Bytes) + MDWIDTH-Bytes)) / MDWIDTH-Bytes
169 *
170 * Unfortunately, due to many variables that's not always the case.
171 */
172int dwc3_gadget_resize_tx_fifos(struct dwc3 *dwc)
173{
174 int last_fifo_depth = 0;
175 int ram1_depth;
176 int fifo_size;
177 int mdwidth;
178 int num;
179
180 if (!dwc->needs_fifo_resize)
181 return 0;
182
183 ram1_depth = DWC3_RAM1_DEPTH(dwc->hwparams.hwparams7);
184 mdwidth = DWC3_MDWIDTH(dwc->hwparams.hwparams0);
185
186 /* MDWIDTH is represented in bits, we need it in bytes */
187 mdwidth >>= 3;
188
189 /*
190 * FIXME For now we will only allocate 1 wMaxPacketSize space
191 * for each enabled endpoint, later patches will come to
192 * improve this algorithm so that we better use the internal
193 * FIFO space
194 */
195 for (num = 0; num < DWC3_ENDPOINTS_NUM; num++) {
196 struct dwc3_ep *dep = dwc->eps[num];
197 int fifo_number = dep->number >> 1;
Felipe Balbi2e81c362012-02-02 13:01:12 +0200198 int mult = 1;
Felipe Balbi457e84b2012-01-18 18:04:09 +0200199 int tmp;
200
201 if (!(dep->number & 1))
202 continue;
203
204 if (!(dep->flags & DWC3_EP_ENABLED))
205 continue;
206
Ido Shayevitz57cdac12012-03-12 20:25:24 +0200207 if (usb_endpoint_xfer_bulk(dep->endpoint.desc)
208 || usb_endpoint_xfer_isoc(dep->endpoint.desc))
Felipe Balbi2e81c362012-02-02 13:01:12 +0200209 mult = 3;
210
211 /*
212 * REVISIT: the following assumes we will always have enough
213 * space available on the FIFO RAM for all possible use cases.
214 * Make sure that's true somehow and change FIFO allocation
215 * accordingly.
216 *
217 * If we have Bulk or Isochronous endpoints, we want
218 * them to be able to be very, very fast. So we're giving
219 * those endpoints a fifo_size which is enough for 3 full
220 * packets
221 */
222 tmp = mult * (dep->endpoint.maxpacket + mdwidth);
Felipe Balbi457e84b2012-01-18 18:04:09 +0200223 tmp += mdwidth;
224
225 fifo_size = DIV_ROUND_UP(tmp, mdwidth);
Felipe Balbi2e81c362012-02-02 13:01:12 +0200226
Felipe Balbi457e84b2012-01-18 18:04:09 +0200227 fifo_size |= (last_fifo_depth << 16);
228
229 dev_vdbg(dwc->dev, "%s: Fifo Addr %04x Size %d\n",
230 dep->name, last_fifo_depth, fifo_size & 0xffff);
231
232 dwc3_writel(dwc->regs, DWC3_GTXFIFOSIZ(fifo_number),
233 fifo_size);
234
235 last_fifo_depth += (fifo_size & 0xffff);
236 }
237
238 return 0;
239}
240
Felipe Balbi72246da2011-08-19 18:10:58 +0300241void dwc3_gadget_giveback(struct dwc3_ep *dep, struct dwc3_request *req,
242 int status)
243{
244 struct dwc3 *dwc = dep->dwc;
245
246 if (req->queued) {
Felipe Balbieeb720f2011-11-28 12:46:59 +0200247 if (req->request.num_mapped_sgs)
248 dep->busy_slot += req->request.num_mapped_sgs;
249 else
250 dep->busy_slot++;
251
Felipe Balbi72246da2011-08-19 18:10:58 +0300252 /*
253 * Skip LINK TRB. We can't use req->trb and check for
254 * DWC3_TRBCTL_LINK_TRB because it points the TRB we just
255 * completed (not the LINK TRB).
256 */
257 if (((dep->busy_slot & DWC3_TRB_MASK) == DWC3_TRB_NUM - 1) &&
Ido Shayevitz57cdac12012-03-12 20:25:24 +0200258 usb_endpoint_xfer_isoc(dep->endpoint.desc))
Felipe Balbi72246da2011-08-19 18:10:58 +0300259 dep->busy_slot++;
260 }
261 list_del(&req->list);
Felipe Balbieeb720f2011-11-28 12:46:59 +0200262 req->trb = NULL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300263
264 if (req->request.status == -EINPROGRESS)
265 req->request.status = status;
266
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +0200267 usb_gadget_unmap_request(&dwc->gadget, &req->request,
268 req->direction);
Felipe Balbi72246da2011-08-19 18:10:58 +0300269
270 dev_dbg(dwc->dev, "request %p from %s completed %d/%d ===> %d\n",
271 req, dep->name, req->request.actual,
272 req->request.length, status);
273
274 spin_unlock(&dwc->lock);
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +0200275 req->request.complete(&dep->endpoint, &req->request);
Felipe Balbi72246da2011-08-19 18:10:58 +0300276 spin_lock(&dwc->lock);
277}
278
279static const char *dwc3_gadget_ep_cmd_string(u8 cmd)
280{
281 switch (cmd) {
282 case DWC3_DEPCMD_DEPSTARTCFG:
283 return "Start New Configuration";
284 case DWC3_DEPCMD_ENDTRANSFER:
285 return "End Transfer";
286 case DWC3_DEPCMD_UPDATETRANSFER:
287 return "Update Transfer";
288 case DWC3_DEPCMD_STARTTRANSFER:
289 return "Start Transfer";
290 case DWC3_DEPCMD_CLEARSTALL:
291 return "Clear Stall";
292 case DWC3_DEPCMD_SETSTALL:
293 return "Set Stall";
Paul Zimmerman88df4272012-04-27 13:10:52 +0300294 case DWC3_DEPCMD_GETEPSTATE:
295 return "Get Endpoint State";
Felipe Balbi72246da2011-08-19 18:10:58 +0300296 case DWC3_DEPCMD_SETTRANSFRESOURCE:
297 return "Set Endpoint Transfer Resource";
298 case DWC3_DEPCMD_SETEPCONFIG:
299 return "Set Endpoint Configuration";
300 default:
301 return "UNKNOWN command";
302 }
303}
304
Felipe Balbi573c2762012-04-24 16:19:11 +0300305int dwc3_send_gadget_generic_command(struct dwc3 *dwc, int cmd, u32 param)
306{
307 u32 timeout = 500;
308 u32 reg;
309
310 dwc3_writel(dwc->regs, DWC3_DGCMDPAR, param);
311 dwc3_writel(dwc->regs, DWC3_DGCMD, cmd | DWC3_DGCMD_CMDACT);
312
313 do {
314 reg = dwc3_readl(dwc->regs, DWC3_DGCMD);
315 if (!(reg & DWC3_DGCMD_CMDACT)) {
316 dev_vdbg(dwc->dev, "Command Complete --> %d\n",
317 DWC3_DGCMD_STATUS(reg));
318 return 0;
319 }
320
321 /*
322 * We can't sleep here, because it's also called from
323 * interrupt context.
324 */
325 timeout--;
326 if (!timeout)
327 return -ETIMEDOUT;
328 udelay(1);
329 } while (1);
330}
331
Felipe Balbi72246da2011-08-19 18:10:58 +0300332int dwc3_send_gadget_ep_cmd(struct dwc3 *dwc, unsigned ep,
333 unsigned cmd, struct dwc3_gadget_ep_cmd_params *params)
334{
335 struct dwc3_ep *dep = dwc->eps[ep];
Sebastian Andrzej Siewior61d58242011-08-29 16:46:38 +0200336 u32 timeout = 500;
Felipe Balbi72246da2011-08-19 18:10:58 +0300337 u32 reg;
338
339 dev_vdbg(dwc->dev, "%s: cmd '%s' params %08x %08x %08x\n",
340 dep->name,
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300341 dwc3_gadget_ep_cmd_string(cmd), params->param0,
342 params->param1, params->param2);
Felipe Balbi72246da2011-08-19 18:10:58 +0300343
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300344 dwc3_writel(dwc->regs, DWC3_DEPCMDPAR0(ep), params->param0);
345 dwc3_writel(dwc->regs, DWC3_DEPCMDPAR1(ep), params->param1);
346 dwc3_writel(dwc->regs, DWC3_DEPCMDPAR2(ep), params->param2);
Felipe Balbi72246da2011-08-19 18:10:58 +0300347
348 dwc3_writel(dwc->regs, DWC3_DEPCMD(ep), cmd | DWC3_DEPCMD_CMDACT);
349 do {
350 reg = dwc3_readl(dwc->regs, DWC3_DEPCMD(ep));
351 if (!(reg & DWC3_DEPCMD_CMDACT)) {
Felipe Balbi164f6e12011-08-27 20:29:58 +0300352 dev_vdbg(dwc->dev, "Command Complete --> %d\n",
353 DWC3_DEPCMD_STATUS(reg));
Felipe Balbi72246da2011-08-19 18:10:58 +0300354 return 0;
355 }
356
357 /*
Felipe Balbi72246da2011-08-19 18:10:58 +0300358 * We can't sleep here, because it is also called from
359 * interrupt context.
360 */
361 timeout--;
362 if (!timeout)
363 return -ETIMEDOUT;
364
Sebastian Andrzej Siewior61d58242011-08-29 16:46:38 +0200365 udelay(1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300366 } while (1);
367}
368
Ido Shayevitzfa65a582012-06-06 14:39:54 +0300369dma_addr_t dwc3_trb_dma_offset(struct dwc3_ep *dep,
Felipe Balbif6bafc62012-02-06 11:04:53 +0200370 struct dwc3_trb *trb)
Felipe Balbi72246da2011-08-19 18:10:58 +0300371{
Paul Zimmermanc439ef82011-09-30 10:58:45 +0300372 u32 offset = (char *) trb - (char *) dep->trb_pool;
Felipe Balbi72246da2011-08-19 18:10:58 +0300373
374 return dep->trb_pool_dma + offset;
375}
376
377static int dwc3_alloc_trb_pool(struct dwc3_ep *dep)
378{
379 struct dwc3 *dwc = dep->dwc;
380
381 if (dep->trb_pool)
382 return 0;
383
384 if (dep->number == 0 || dep->number == 1)
385 return 0;
386
387 dep->trb_pool = dma_alloc_coherent(dwc->dev,
388 sizeof(struct dwc3_trb) * DWC3_TRB_NUM,
389 &dep->trb_pool_dma, GFP_KERNEL);
390 if (!dep->trb_pool) {
391 dev_err(dep->dwc->dev, "failed to allocate trb pool for %s\n",
392 dep->name);
393 return -ENOMEM;
394 }
395
396 return 0;
397}
398
399static void dwc3_free_trb_pool(struct dwc3_ep *dep)
400{
401 struct dwc3 *dwc = dep->dwc;
402
403 dma_free_coherent(dwc->dev, sizeof(struct dwc3_trb) * DWC3_TRB_NUM,
404 dep->trb_pool, dep->trb_pool_dma);
405
406 dep->trb_pool = NULL;
407 dep->trb_pool_dma = 0;
408}
409
410static int dwc3_gadget_start_config(struct dwc3 *dwc, struct dwc3_ep *dep)
411{
412 struct dwc3_gadget_ep_cmd_params params;
413 u32 cmd;
414
415 memset(&params, 0x00, sizeof(params));
416
417 if (dep->number != 1) {
418 cmd = DWC3_DEPCMD_DEPSTARTCFG;
419 /* XferRscIdx == 0 for ep0 and 2 for the remaining */
Paul Zimmermanb23c8432011-09-30 10:58:42 +0300420 if (dep->number > 1) {
421 if (dwc->start_config_issued)
422 return 0;
423 dwc->start_config_issued = true;
Felipe Balbi72246da2011-08-19 18:10:58 +0300424 cmd |= DWC3_DEPCMD_PARAM(2);
Paul Zimmermanb23c8432011-09-30 10:58:42 +0300425 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300426
427 return dwc3_send_gadget_ep_cmd(dwc, 0, cmd, &params);
428 }
429
430 return 0;
431}
432
433static int dwc3_gadget_set_ep_config(struct dwc3 *dwc, struct dwc3_ep *dep,
Felipe Balbic90bfae2011-11-29 13:11:21 +0200434 const struct usb_endpoint_descriptor *desc,
435 const struct usb_ss_ep_comp_descriptor *comp_desc)
Felipe Balbi72246da2011-08-19 18:10:58 +0300436{
437 struct dwc3_gadget_ep_cmd_params params;
438
439 memset(&params, 0x00, sizeof(params));
440
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300441 params.param0 = DWC3_DEPCFG_EP_TYPE(usb_endpoint_type(desc))
442 | DWC3_DEPCFG_MAX_PACKET_SIZE(usb_endpoint_maxp(desc))
Felipe Balbibccd6802012-06-06 10:20:23 +0300443 | DWC3_DEPCFG_BURST_SIZE(dep->endpoint.maxburst - 1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300444
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300445 params.param1 = DWC3_DEPCFG_XFER_COMPLETE_EN
446 | DWC3_DEPCFG_XFER_NOT_READY_EN;
Felipe Balbi72246da2011-08-19 18:10:58 +0300447
Felipe Balbi18b7ede2012-01-02 13:35:41 +0200448 if (usb_ss_max_streams(comp_desc) && usb_endpoint_xfer_bulk(desc)) {
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300449 params.param1 |= DWC3_DEPCFG_STREAM_CAPABLE
450 | DWC3_DEPCFG_STREAM_EVENT_EN;
Felipe Balbi879631a2011-09-30 10:58:47 +0300451 dep->stream_capable = true;
452 }
453
Felipe Balbi72246da2011-08-19 18:10:58 +0300454 if (usb_endpoint_xfer_isoc(desc))
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300455 params.param1 |= DWC3_DEPCFG_XFER_IN_PROGRESS_EN;
Felipe Balbi72246da2011-08-19 18:10:58 +0300456
457 /*
458 * We are doing 1:1 mapping for endpoints, meaning
459 * Physical Endpoints 2 maps to Logical Endpoint 2 and
460 * so on. We consider the direction bit as part of the physical
461 * endpoint number. So USB endpoint 0x81 is 0x03.
462 */
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300463 params.param1 |= DWC3_DEPCFG_EP_NUMBER(dep->number);
Felipe Balbi72246da2011-08-19 18:10:58 +0300464
465 /*
466 * We must use the lower 16 TX FIFOs even though
467 * HW might have more
468 */
469 if (dep->direction)
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300470 params.param0 |= DWC3_DEPCFG_FIFO_NUMBER(dep->number >> 1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300471
472 if (desc->bInterval) {
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300473 params.param1 |= DWC3_DEPCFG_BINTERVAL_M1(desc->bInterval - 1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300474 dep->interval = 1 << (desc->bInterval - 1);
475 }
476
477 return dwc3_send_gadget_ep_cmd(dwc, dep->number,
478 DWC3_DEPCMD_SETEPCONFIG, &params);
479}
480
481static int dwc3_gadget_set_xfer_resource(struct dwc3 *dwc, struct dwc3_ep *dep)
482{
483 struct dwc3_gadget_ep_cmd_params params;
484
485 memset(&params, 0x00, sizeof(params));
486
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300487 params.param0 = DWC3_DEPXFERCFG_NUM_XFER_RES(1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300488
489 return dwc3_send_gadget_ep_cmd(dwc, dep->number,
490 DWC3_DEPCMD_SETTRANSFRESOURCE, &params);
491}
492
493/**
494 * __dwc3_gadget_ep_enable - Initializes a HW endpoint
495 * @dep: endpoint to be initialized
496 * @desc: USB Endpoint Descriptor
497 *
498 * Caller should take care of locking
499 */
500static int __dwc3_gadget_ep_enable(struct dwc3_ep *dep,
Felipe Balbic90bfae2011-11-29 13:11:21 +0200501 const struct usb_endpoint_descriptor *desc,
502 const struct usb_ss_ep_comp_descriptor *comp_desc)
Felipe Balbi72246da2011-08-19 18:10:58 +0300503{
504 struct dwc3 *dwc = dep->dwc;
505 u32 reg;
506 int ret = -ENOMEM;
507
508 if (!(dep->flags & DWC3_EP_ENABLED)) {
509 ret = dwc3_gadget_start_config(dwc, dep);
510 if (ret)
511 return ret;
512 }
513
Felipe Balbic90bfae2011-11-29 13:11:21 +0200514 ret = dwc3_gadget_set_ep_config(dwc, dep, desc, comp_desc);
Felipe Balbi72246da2011-08-19 18:10:58 +0300515 if (ret)
516 return ret;
517
518 if (!(dep->flags & DWC3_EP_ENABLED)) {
Felipe Balbif6bafc62012-02-06 11:04:53 +0200519 struct dwc3_trb *trb_st_hw;
520 struct dwc3_trb *trb_link;
Felipe Balbi72246da2011-08-19 18:10:58 +0300521
522 ret = dwc3_gadget_set_xfer_resource(dwc, dep);
523 if (ret)
524 return ret;
525
Ido Shayevitz57cdac12012-03-12 20:25:24 +0200526 dep->endpoint.desc = desc;
Felipe Balbic90bfae2011-11-29 13:11:21 +0200527 dep->comp_desc = comp_desc;
Felipe Balbi72246da2011-08-19 18:10:58 +0300528 dep->type = usb_endpoint_type(desc);
529 dep->flags |= DWC3_EP_ENABLED;
530
531 reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
532 reg |= DWC3_DALEPENA_EP(dep->number);
533 dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
534
535 if (!usb_endpoint_xfer_isoc(desc))
536 return 0;
537
538 memset(&trb_link, 0, sizeof(trb_link));
539
Paul Zimmerman1d046792012-02-15 18:56:56 -0800540 /* Link TRB for ISOC. The HWO bit is never reset */
Felipe Balbi72246da2011-08-19 18:10:58 +0300541 trb_st_hw = &dep->trb_pool[0];
542
Felipe Balbif6bafc62012-02-06 11:04:53 +0200543 trb_link = &dep->trb_pool[DWC3_TRB_NUM - 1];
Felipe Balbi72246da2011-08-19 18:10:58 +0300544
Felipe Balbif6bafc62012-02-06 11:04:53 +0200545 trb_link->bpl = lower_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw));
546 trb_link->bph = upper_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw));
547 trb_link->ctrl |= DWC3_TRBCTL_LINK_TRB;
548 trb_link->ctrl |= DWC3_TRB_CTRL_HWO;
Felipe Balbi72246da2011-08-19 18:10:58 +0300549 }
550
551 return 0;
552}
553
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200554static void dwc3_stop_active_transfer(struct dwc3 *dwc, u32 epnum);
555static void dwc3_remove_requests(struct dwc3 *dwc, struct dwc3_ep *dep)
Felipe Balbi72246da2011-08-19 18:10:58 +0300556{
557 struct dwc3_request *req;
558
Felipe Balbib129eb72012-02-17 12:10:04 +0200559 if (!list_empty(&dep->req_queued)) {
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200560 dwc3_stop_active_transfer(dwc, dep->number);
561
Felipe Balbib129eb72012-02-17 12:10:04 +0200562 /*
563 * NOTICE: We are violating what the Databook says about the
564 * EndTransfer command. Ideally we would _always_ wait for the
565 * EndTransfer Command Completion IRQ, but that's causing too
566 * much trouble synchronizing between us and gadget driver.
567 *
568 * We have discussed this with the IP Provider and it was
569 * suggested to giveback all requests here, but give HW some
570 * extra time to synchronize with the interconnect. We're using
571 * an arbitraty 100us delay for that.
572 *
573 * Note also that a similar handling was tested by Synopsys
574 * (thanks a lot Paul) and nothing bad has come out of it.
575 * In short, what we're doing is:
576 *
577 * - Issue EndTransfer WITH CMDIOC bit set
578 * - Wait 100us
579 * - giveback all requests to gadget driver
580 */
581 udelay(100);
582
Pratyush Anand110ff602012-06-15 11:54:36 +0530583 while (!list_empty(&dep->req_queued)) {
584 req = next_request(&dep->req_queued);
585
586 dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
587 }
Felipe Balbib129eb72012-02-17 12:10:04 +0200588 }
589
Felipe Balbi72246da2011-08-19 18:10:58 +0300590 while (!list_empty(&dep->request_list)) {
591 req = next_request(&dep->request_list);
592
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200593 dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
Felipe Balbi72246da2011-08-19 18:10:58 +0300594 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300595}
596
597/**
598 * __dwc3_gadget_ep_disable - Disables a HW endpoint
599 * @dep: the endpoint to disable
600 *
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200601 * This function also removes requests which are currently processed ny the
602 * hardware and those which are not yet scheduled.
603 * Caller should take care of locking.
Felipe Balbi72246da2011-08-19 18:10:58 +0300604 */
Felipe Balbi72246da2011-08-19 18:10:58 +0300605static int __dwc3_gadget_ep_disable(struct dwc3_ep *dep)
606{
607 struct dwc3 *dwc = dep->dwc;
608 u32 reg;
609
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200610 dwc3_remove_requests(dwc, dep);
Felipe Balbi72246da2011-08-19 18:10:58 +0300611
612 reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
613 reg &= ~DWC3_DALEPENA_EP(dep->number);
614 dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
615
Felipe Balbi879631a2011-09-30 10:58:47 +0300616 dep->stream_capable = false;
Ido Shayevitzf9c56cd2012-02-08 13:56:48 +0200617 dep->endpoint.desc = NULL;
Felipe Balbic90bfae2011-11-29 13:11:21 +0200618 dep->comp_desc = NULL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300619 dep->type = 0;
Felipe Balbi879631a2011-09-30 10:58:47 +0300620 dep->flags = 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300621
622 return 0;
623}
624
625/* -------------------------------------------------------------------------- */
626
627static int dwc3_gadget_ep0_enable(struct usb_ep *ep,
628 const struct usb_endpoint_descriptor *desc)
629{
630 return -EINVAL;
631}
632
633static int dwc3_gadget_ep0_disable(struct usb_ep *ep)
634{
635 return -EINVAL;
636}
637
638/* -------------------------------------------------------------------------- */
639
640static int dwc3_gadget_ep_enable(struct usb_ep *ep,
641 const struct usb_endpoint_descriptor *desc)
642{
643 struct dwc3_ep *dep;
644 struct dwc3 *dwc;
645 unsigned long flags;
646 int ret;
647
648 if (!ep || !desc || desc->bDescriptorType != USB_DT_ENDPOINT) {
649 pr_debug("dwc3: invalid parameters\n");
650 return -EINVAL;
651 }
652
653 if (!desc->wMaxPacketSize) {
654 pr_debug("dwc3: missing wMaxPacketSize\n");
655 return -EINVAL;
656 }
657
658 dep = to_dwc3_ep(ep);
659 dwc = dep->dwc;
660
661 switch (usb_endpoint_type(desc)) {
662 case USB_ENDPOINT_XFER_CONTROL:
Anton Tikhomirov27a78d62012-02-23 15:38:46 +0900663 strlcat(dep->name, "-control", sizeof(dep->name));
Felipe Balbi72246da2011-08-19 18:10:58 +0300664 break;
665 case USB_ENDPOINT_XFER_ISOC:
Anton Tikhomirov27a78d62012-02-23 15:38:46 +0900666 strlcat(dep->name, "-isoc", sizeof(dep->name));
Felipe Balbi72246da2011-08-19 18:10:58 +0300667 break;
668 case USB_ENDPOINT_XFER_BULK:
Anton Tikhomirov27a78d62012-02-23 15:38:46 +0900669 strlcat(dep->name, "-bulk", sizeof(dep->name));
Felipe Balbi72246da2011-08-19 18:10:58 +0300670 break;
671 case USB_ENDPOINT_XFER_INT:
Anton Tikhomirov27a78d62012-02-23 15:38:46 +0900672 strlcat(dep->name, "-int", sizeof(dep->name));
Felipe Balbi72246da2011-08-19 18:10:58 +0300673 break;
674 default:
675 dev_err(dwc->dev, "invalid endpoint transfer type\n");
676 }
677
678 if (dep->flags & DWC3_EP_ENABLED) {
679 dev_WARN_ONCE(dwc->dev, true, "%s is already enabled\n",
680 dep->name);
681 return 0;
682 }
683
684 dev_vdbg(dwc->dev, "Enabling %s\n", dep->name);
685
686 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbic90bfae2011-11-29 13:11:21 +0200687 ret = __dwc3_gadget_ep_enable(dep, desc, ep->comp_desc);
Felipe Balbi72246da2011-08-19 18:10:58 +0300688 spin_unlock_irqrestore(&dwc->lock, flags);
689
690 return ret;
691}
692
693static int dwc3_gadget_ep_disable(struct usb_ep *ep)
694{
695 struct dwc3_ep *dep;
696 struct dwc3 *dwc;
697 unsigned long flags;
698 int ret;
699
700 if (!ep) {
701 pr_debug("dwc3: invalid parameters\n");
702 return -EINVAL;
703 }
704
705 dep = to_dwc3_ep(ep);
706 dwc = dep->dwc;
707
708 if (!(dep->flags & DWC3_EP_ENABLED)) {
709 dev_WARN_ONCE(dwc->dev, true, "%s is already disabled\n",
710 dep->name);
711 return 0;
712 }
713
714 snprintf(dep->name, sizeof(dep->name), "ep%d%s",
715 dep->number >> 1,
716 (dep->number & 1) ? "in" : "out");
717
718 spin_lock_irqsave(&dwc->lock, flags);
719 ret = __dwc3_gadget_ep_disable(dep);
720 spin_unlock_irqrestore(&dwc->lock, flags);
721
722 return ret;
723}
724
725static struct usb_request *dwc3_gadget_ep_alloc_request(struct usb_ep *ep,
726 gfp_t gfp_flags)
727{
728 struct dwc3_request *req;
729 struct dwc3_ep *dep = to_dwc3_ep(ep);
730 struct dwc3 *dwc = dep->dwc;
731
732 req = kzalloc(sizeof(*req), gfp_flags);
733 if (!req) {
734 dev_err(dwc->dev, "not enough memory\n");
735 return NULL;
736 }
737
738 req->epnum = dep->number;
739 req->dep = dep;
Felipe Balbi72246da2011-08-19 18:10:58 +0300740
741 return &req->request;
742}
743
744static void dwc3_gadget_ep_free_request(struct usb_ep *ep,
745 struct usb_request *request)
746{
747 struct dwc3_request *req = to_dwc3_request(request);
748
749 kfree(req);
750}
751
Felipe Balbic71fc372011-11-22 11:37:34 +0200752/**
753 * dwc3_prepare_one_trb - setup one TRB from one request
754 * @dep: endpoint for which this request is prepared
755 * @req: dwc3_request pointer
756 */
Felipe Balbi68e823e2011-11-28 12:25:01 +0200757static void dwc3_prepare_one_trb(struct dwc3_ep *dep,
Felipe Balbieeb720f2011-11-28 12:46:59 +0200758 struct dwc3_request *req, dma_addr_t dma,
759 unsigned length, unsigned last, unsigned chain)
Felipe Balbic71fc372011-11-22 11:37:34 +0200760{
Felipe Balbieeb720f2011-11-28 12:46:59 +0200761 struct dwc3 *dwc = dep->dwc;
Felipe Balbif6bafc62012-02-06 11:04:53 +0200762 struct dwc3_trb *trb;
Felipe Balbic71fc372011-11-22 11:37:34 +0200763
764 unsigned int cur_slot;
765
Felipe Balbieeb720f2011-11-28 12:46:59 +0200766 dev_vdbg(dwc->dev, "%s: req %p dma %08llx length %d%s%s\n",
767 dep->name, req, (unsigned long long) dma,
768 length, last ? " last" : "",
769 chain ? " chain" : "");
770
Felipe Balbif6bafc62012-02-06 11:04:53 +0200771 trb = &dep->trb_pool[dep->free_slot & DWC3_TRB_MASK];
Felipe Balbic71fc372011-11-22 11:37:34 +0200772 cur_slot = dep->free_slot;
773 dep->free_slot++;
774
775 /* Skip the LINK-TRB on ISOC */
776 if (((cur_slot & DWC3_TRB_MASK) == DWC3_TRB_NUM - 1) &&
Ido Shayevitz57cdac12012-03-12 20:25:24 +0200777 usb_endpoint_xfer_isoc(dep->endpoint.desc))
Felipe Balbi68e823e2011-11-28 12:25:01 +0200778 return;
Felipe Balbic71fc372011-11-22 11:37:34 +0200779
Felipe Balbieeb720f2011-11-28 12:46:59 +0200780 if (!req->trb) {
781 dwc3_gadget_move_request_queued(req);
Felipe Balbif6bafc62012-02-06 11:04:53 +0200782 req->trb = trb;
783 req->trb_dma = dwc3_trb_dma_offset(dep, trb);
Felipe Balbieeb720f2011-11-28 12:46:59 +0200784 }
Felipe Balbic71fc372011-11-22 11:37:34 +0200785
Felipe Balbif6bafc62012-02-06 11:04:53 +0200786 trb->size = DWC3_TRB_SIZE_LENGTH(length);
787 trb->bpl = lower_32_bits(dma);
788 trb->bph = upper_32_bits(dma);
Felipe Balbic71fc372011-11-22 11:37:34 +0200789
Ido Shayevitz57cdac12012-03-12 20:25:24 +0200790 switch (usb_endpoint_type(dep->endpoint.desc)) {
Felipe Balbic71fc372011-11-22 11:37:34 +0200791 case USB_ENDPOINT_XFER_CONTROL:
Felipe Balbif6bafc62012-02-06 11:04:53 +0200792 trb->ctrl = DWC3_TRBCTL_CONTROL_SETUP;
Felipe Balbic71fc372011-11-22 11:37:34 +0200793 break;
794
795 case USB_ENDPOINT_XFER_ISOC:
Felipe Balbif6bafc62012-02-06 11:04:53 +0200796 trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS_FIRST;
Felipe Balbic71fc372011-11-22 11:37:34 +0200797
Pratyush Ananddf023422012-05-21 12:42:54 +0530798 if (!req->request.no_interrupt)
Felipe Balbif6bafc62012-02-06 11:04:53 +0200799 trb->ctrl |= DWC3_TRB_CTRL_IOC;
Felipe Balbic71fc372011-11-22 11:37:34 +0200800 break;
801
802 case USB_ENDPOINT_XFER_BULK:
803 case USB_ENDPOINT_XFER_INT:
Felipe Balbif6bafc62012-02-06 11:04:53 +0200804 trb->ctrl = DWC3_TRBCTL_NORMAL;
Felipe Balbic71fc372011-11-22 11:37:34 +0200805 break;
806 default:
807 /*
808 * This is only possible with faulty memory because we
809 * checked it already :)
810 */
811 BUG();
812 }
813
Ido Shayevitz57cdac12012-03-12 20:25:24 +0200814 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbif6bafc62012-02-06 11:04:53 +0200815 trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI;
816 trb->ctrl |= DWC3_TRB_CTRL_CSP;
817 } else {
818 if (chain)
819 trb->ctrl |= DWC3_TRB_CTRL_CHN;
Felipe Balbic71fc372011-11-22 11:37:34 +0200820
Felipe Balbif6bafc62012-02-06 11:04:53 +0200821 if (last)
822 trb->ctrl |= DWC3_TRB_CTRL_LST;
823 }
824
Ido Shayevitz57cdac12012-03-12 20:25:24 +0200825 if (usb_endpoint_xfer_bulk(dep->endpoint.desc) && dep->stream_capable)
Felipe Balbif6bafc62012-02-06 11:04:53 +0200826 trb->ctrl |= DWC3_TRB_CTRL_SID_SOFN(req->request.stream_id);
827
828 trb->ctrl |= DWC3_TRB_CTRL_HWO;
Felipe Balbic71fc372011-11-22 11:37:34 +0200829}
830
Felipe Balbi72246da2011-08-19 18:10:58 +0300831/*
832 * dwc3_prepare_trbs - setup TRBs from requests
833 * @dep: endpoint for which requests are being prepared
834 * @starting: true if the endpoint is idle and no requests are queued.
835 *
Paul Zimmerman1d046792012-02-15 18:56:56 -0800836 * The function goes through the requests list and sets up TRBs for the
837 * transfers. The function returns once there are no more TRBs available or
838 * it runs out of requests.
Felipe Balbi72246da2011-08-19 18:10:58 +0300839 */
Felipe Balbi68e823e2011-11-28 12:25:01 +0200840static void dwc3_prepare_trbs(struct dwc3_ep *dep, bool starting)
Felipe Balbi72246da2011-08-19 18:10:58 +0300841{
Felipe Balbi68e823e2011-11-28 12:25:01 +0200842 struct dwc3_request *req, *n;
Felipe Balbi72246da2011-08-19 18:10:58 +0300843 u32 trbs_left;
Paul Zimmerman8d62cd62012-02-15 13:35:06 +0200844 u32 max;
Felipe Balbic71fc372011-11-22 11:37:34 +0200845 unsigned int last_one = 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300846
847 BUILD_BUG_ON_NOT_POWER_OF_2(DWC3_TRB_NUM);
848
849 /* the first request must not be queued */
850 trbs_left = (dep->busy_slot - dep->free_slot) & DWC3_TRB_MASK;
Felipe Balbic71fc372011-11-22 11:37:34 +0200851
Paul Zimmerman8d62cd62012-02-15 13:35:06 +0200852 /* Can't wrap around on a non-isoc EP since there's no link TRB */
Ido Shayevitz57cdac12012-03-12 20:25:24 +0200853 if (!usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Paul Zimmerman8d62cd62012-02-15 13:35:06 +0200854 max = DWC3_TRB_NUM - (dep->free_slot & DWC3_TRB_MASK);
855 if (trbs_left > max)
856 trbs_left = max;
857 }
858
Felipe Balbi72246da2011-08-19 18:10:58 +0300859 /*
Paul Zimmerman1d046792012-02-15 18:56:56 -0800860 * If busy & slot are equal than it is either full or empty. If we are
861 * starting to process requests then we are empty. Otherwise we are
Felipe Balbi72246da2011-08-19 18:10:58 +0300862 * full and don't do anything
863 */
864 if (!trbs_left) {
865 if (!starting)
Felipe Balbi68e823e2011-11-28 12:25:01 +0200866 return;
Felipe Balbi72246da2011-08-19 18:10:58 +0300867 trbs_left = DWC3_TRB_NUM;
868 /*
869 * In case we start from scratch, we queue the ISOC requests
870 * starting from slot 1. This is done because we use ring
871 * buffer and have no LST bit to stop us. Instead, we place
Paul Zimmerman1d046792012-02-15 18:56:56 -0800872 * IOC bit every TRB_NUM/4. We try to avoid having an interrupt
Felipe Balbi72246da2011-08-19 18:10:58 +0300873 * after the first request so we start at slot 1 and have
874 * 7 requests proceed before we hit the first IOC.
875 * Other transfer types don't use the ring buffer and are
876 * processed from the first TRB until the last one. Since we
877 * don't wrap around we have to start at the beginning.
878 */
Ido Shayevitz57cdac12012-03-12 20:25:24 +0200879 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbi72246da2011-08-19 18:10:58 +0300880 dep->busy_slot = 1;
881 dep->free_slot = 1;
882 } else {
883 dep->busy_slot = 0;
884 dep->free_slot = 0;
885 }
886 }
887
888 /* The last TRB is a link TRB, not used for xfer */
Ido Shayevitz57cdac12012-03-12 20:25:24 +0200889 if ((trbs_left <= 1) && usb_endpoint_xfer_isoc(dep->endpoint.desc))
Felipe Balbi68e823e2011-11-28 12:25:01 +0200890 return;
Felipe Balbi72246da2011-08-19 18:10:58 +0300891
892 list_for_each_entry_safe(req, n, &dep->request_list, list) {
Felipe Balbieeb720f2011-11-28 12:46:59 +0200893 unsigned length;
894 dma_addr_t dma;
Felipe Balbi72246da2011-08-19 18:10:58 +0300895
Felipe Balbieeb720f2011-11-28 12:46:59 +0200896 if (req->request.num_mapped_sgs > 0) {
897 struct usb_request *request = &req->request;
898 struct scatterlist *sg = request->sg;
899 struct scatterlist *s;
900 int i;
Felipe Balbi72246da2011-08-19 18:10:58 +0300901
Felipe Balbieeb720f2011-11-28 12:46:59 +0200902 for_each_sg(sg, s, request->num_mapped_sgs, i) {
903 unsigned chain = true;
Felipe Balbi72246da2011-08-19 18:10:58 +0300904
Felipe Balbieeb720f2011-11-28 12:46:59 +0200905 length = sg_dma_len(s);
906 dma = sg_dma_address(s);
Felipe Balbi72246da2011-08-19 18:10:58 +0300907
Paul Zimmerman1d046792012-02-15 18:56:56 -0800908 if (i == (request->num_mapped_sgs - 1) ||
909 sg_is_last(s)) {
Felipe Balbieeb720f2011-11-28 12:46:59 +0200910 last_one = true;
911 chain = false;
912 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300913
Felipe Balbieeb720f2011-11-28 12:46:59 +0200914 trbs_left--;
915 if (!trbs_left)
916 last_one = true;
Felipe Balbi72246da2011-08-19 18:10:58 +0300917
Felipe Balbieeb720f2011-11-28 12:46:59 +0200918 if (last_one)
919 chain = false;
Felipe Balbi72246da2011-08-19 18:10:58 +0300920
Felipe Balbieeb720f2011-11-28 12:46:59 +0200921 dwc3_prepare_one_trb(dep, req, dma, length,
922 last_one, chain);
Felipe Balbi72246da2011-08-19 18:10:58 +0300923
Felipe Balbieeb720f2011-11-28 12:46:59 +0200924 if (last_one)
925 break;
926 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300927 } else {
Felipe Balbieeb720f2011-11-28 12:46:59 +0200928 dma = req->request.dma;
929 length = req->request.length;
930 trbs_left--;
931
932 if (!trbs_left)
933 last_one = 1;
934
935 /* Is this the last request? */
936 if (list_is_last(&req->list, &dep->request_list))
937 last_one = 1;
938
939 dwc3_prepare_one_trb(dep, req, dma, length,
940 last_one, false);
941
942 if (last_one)
943 break;
Felipe Balbi72246da2011-08-19 18:10:58 +0300944 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300945 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300946}
947
948static int __dwc3_gadget_kick_transfer(struct dwc3_ep *dep, u16 cmd_param,
949 int start_new)
950{
951 struct dwc3_gadget_ep_cmd_params params;
952 struct dwc3_request *req;
953 struct dwc3 *dwc = dep->dwc;
954 int ret;
955 u32 cmd;
956
957 if (start_new && (dep->flags & DWC3_EP_BUSY)) {
958 dev_vdbg(dwc->dev, "%s: endpoint busy\n", dep->name);
959 return -EBUSY;
960 }
961 dep->flags &= ~DWC3_EP_PENDING_REQUEST;
962
963 /*
964 * If we are getting here after a short-out-packet we don't enqueue any
965 * new requests as we try to set the IOC bit only on the last request.
966 */
967 if (start_new) {
968 if (list_empty(&dep->req_queued))
969 dwc3_prepare_trbs(dep, start_new);
970
971 /* req points to the first request which will be sent */
972 req = next_request(&dep->req_queued);
973 } else {
Felipe Balbi68e823e2011-11-28 12:25:01 +0200974 dwc3_prepare_trbs(dep, start_new);
975
Felipe Balbi72246da2011-08-19 18:10:58 +0300976 /*
Paul Zimmerman1d046792012-02-15 18:56:56 -0800977 * req points to the first request where HWO changed from 0 to 1
Felipe Balbi72246da2011-08-19 18:10:58 +0300978 */
Felipe Balbi68e823e2011-11-28 12:25:01 +0200979 req = next_request(&dep->req_queued);
Felipe Balbi72246da2011-08-19 18:10:58 +0300980 }
981 if (!req) {
982 dep->flags |= DWC3_EP_PENDING_REQUEST;
983 return 0;
984 }
985
986 memset(&params, 0, sizeof(params));
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300987 params.param0 = upper_32_bits(req->trb_dma);
988 params.param1 = lower_32_bits(req->trb_dma);
Felipe Balbi72246da2011-08-19 18:10:58 +0300989
990 if (start_new)
991 cmd = DWC3_DEPCMD_STARTTRANSFER;
992 else
993 cmd = DWC3_DEPCMD_UPDATETRANSFER;
994
995 cmd |= DWC3_DEPCMD_PARAM(cmd_param);
996 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number, cmd, &params);
997 if (ret < 0) {
998 dev_dbg(dwc->dev, "failed to send STARTTRANSFER command\n");
999
1000 /*
1001 * FIXME we need to iterate over the list of requests
1002 * here and stop, unmap, free and del each of the linked
Paul Zimmerman1d046792012-02-15 18:56:56 -08001003 * requests instead of what we do now.
Felipe Balbi72246da2011-08-19 18:10:58 +03001004 */
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02001005 usb_gadget_unmap_request(&dwc->gadget, &req->request,
1006 req->direction);
Felipe Balbi72246da2011-08-19 18:10:58 +03001007 list_del(&req->list);
1008 return ret;
1009 }
1010
1011 dep->flags |= DWC3_EP_BUSY;
Felipe Balbi25b8ff62011-11-04 12:32:47 +02001012
Paul Zimmermanf39a37f2012-03-29 18:16:54 +00001013 if (start_new) {
1014 dep->res_trans_idx = dwc3_gadget_ep_get_transfer_index(dwc,
1015 dep->number);
1016 WARN_ON_ONCE(!dep->res_trans_idx);
1017 }
Felipe Balbi25b8ff62011-11-04 12:32:47 +02001018
Felipe Balbi72246da2011-08-19 18:10:58 +03001019 return 0;
1020}
1021
Pratyush Anand73939b02012-05-25 18:54:56 +05301022static void __dwc3_gadget_start_isoc(struct dwc3 *dwc,
1023 struct dwc3_ep *dep, u32 cur_uf)
1024{
1025 u32 uf;
1026
1027 if (list_empty(&dep->request_list)) {
1028 dev_vdbg(dwc->dev, "ISOC ep %s run out for requests.\n",
1029 dep->name);
1030 return;
1031 }
1032
1033 /* 4 micro frames in the future */
1034 uf = cur_uf + dep->interval * 4;
1035
1036 __dwc3_gadget_kick_transfer(dep, uf, 1);
1037}
1038
1039static void dwc3_gadget_start_isoc(struct dwc3 *dwc,
1040 struct dwc3_ep *dep, const struct dwc3_event_depevt *event)
1041{
1042 u32 cur_uf, mask;
1043
1044 mask = ~(dep->interval - 1);
1045 cur_uf = event->parameters & mask;
1046
1047 __dwc3_gadget_start_isoc(dwc, dep, cur_uf);
1048}
1049
Felipe Balbi72246da2011-08-19 18:10:58 +03001050static int __dwc3_gadget_ep_queue(struct dwc3_ep *dep, struct dwc3_request *req)
1051{
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02001052 struct dwc3 *dwc = dep->dwc;
1053 int ret;
1054
Felipe Balbi72246da2011-08-19 18:10:58 +03001055 req->request.actual = 0;
1056 req->request.status = -EINPROGRESS;
1057 req->direction = dep->direction;
1058 req->epnum = dep->number;
1059
1060 /*
1061 * We only add to our list of requests now and
1062 * start consuming the list once we get XferNotReady
1063 * IRQ.
1064 *
1065 * That way, we avoid doing anything that we don't need
1066 * to do now and defer it until the point we receive a
1067 * particular token from the Host side.
1068 *
1069 * This will also avoid Host cancelling URBs due to too
Paul Zimmerman1d046792012-02-15 18:56:56 -08001070 * many NAKs.
Felipe Balbi72246da2011-08-19 18:10:58 +03001071 */
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02001072 ret = usb_gadget_map_request(&dwc->gadget, &req->request,
1073 dep->direction);
1074 if (ret)
1075 return ret;
1076
Felipe Balbi72246da2011-08-19 18:10:58 +03001077 list_add_tail(&req->list, &dep->request_list);
1078
1079 /*
Felipe Balbi46485a02012-06-06 12:00:50 +03001080 * There are a few special cases:
Felipe Balbi72246da2011-08-19 18:10:58 +03001081 *
Paul Zimmermanf39a37f2012-03-29 18:16:54 +00001082 * 1. XferNotReady with empty list of requests. We need to kick the
1083 * transfer here in that situation, otherwise we will be NAKing
1084 * forever. If we get XferNotReady before gadget driver has a
1085 * chance to queue a request, we will ACK the IRQ but won't be
1086 * able to receive the data until the next request is queued.
1087 * The following code is handling exactly that.
1088 *
Felipe Balbi72246da2011-08-19 18:10:58 +03001089 */
1090 if (dep->flags & DWC3_EP_PENDING_REQUEST) {
Paul Zimmermanf39a37f2012-03-29 18:16:54 +00001091 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001092
Felipe Balbi46485a02012-06-06 12:00:50 +03001093 ret = __dwc3_gadget_kick_transfer(dep, 0, true);
Felipe Balbi72246da2011-08-19 18:10:58 +03001094 if (ret && ret != -EBUSY) {
1095 struct dwc3 *dwc = dep->dwc;
1096
1097 dev_dbg(dwc->dev, "%s: failed to kick transfers\n",
1098 dep->name);
1099 }
Felipe Balbi5d409eb2012-05-22 10:24:11 +03001100 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001101
Felipe Balbi46485a02012-06-06 12:00:50 +03001102 /*
1103 * 2. XferInProgress on Isoc EP with an active transfer. We need to
1104 * kick the transfer here after queuing a request, otherwise the
1105 * core may not see the modified TRB(s).
1106 */
1107 if (usb_endpoint_xfer_isoc(dep->endpoint.desc) &&
1108 (dep->flags & DWC3_EP_BUSY)) {
1109 WARN_ON_ONCE(!dep->res_trans_idx);
1110 ret = __dwc3_gadget_kick_transfer(dep, dep->res_trans_idx,
1111 false);
1112 if (ret && ret != -EBUSY) {
1113 struct dwc3 *dwc = dep->dwc;
1114
1115 dev_dbg(dwc->dev, "%s: failed to kick transfers\n",
1116 dep->name);
1117 }
1118 }
1119
1120 /*
1121 * 3. Missed ISOC Handling. We need to start isoc transfer on the saved
1122 * uframe number.
1123 */
1124 if (usb_endpoint_xfer_isoc(dep->endpoint.desc) &&
1125 (dep->flags & DWC3_EP_MISSED_ISOC)) {
1126 __dwc3_gadget_start_isoc(dwc, dep, dep->current_uf);
1127 dep->flags &= ~DWC3_EP_MISSED_ISOC;
1128 }
1129
Felipe Balbi72246da2011-08-19 18:10:58 +03001130 return 0;
1131}
1132
1133static int dwc3_gadget_ep_queue(struct usb_ep *ep, struct usb_request *request,
1134 gfp_t gfp_flags)
1135{
1136 struct dwc3_request *req = to_dwc3_request(request);
1137 struct dwc3_ep *dep = to_dwc3_ep(ep);
1138 struct dwc3 *dwc = dep->dwc;
1139
1140 unsigned long flags;
1141
1142 int ret;
1143
Ido Shayevitz57cdac12012-03-12 20:25:24 +02001144 if (!dep->endpoint.desc) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001145 dev_dbg(dwc->dev, "trying to queue request %p to disabled %s\n",
1146 request, ep->name);
1147 return -ESHUTDOWN;
1148 }
1149
1150 dev_vdbg(dwc->dev, "queing request %p to %s length %d\n",
1151 request, ep->name, request->length);
1152
Manu Gautam1c4dbcb2012-10-05 13:16:00 +05301153 WARN(!dep->direction && (request->length % ep->desc->wMaxPacketSize),
1154 "trying to queue unaligned request (%d)\n", request->length);
1155
Felipe Balbi72246da2011-08-19 18:10:58 +03001156 spin_lock_irqsave(&dwc->lock, flags);
1157 ret = __dwc3_gadget_ep_queue(dep, req);
1158 spin_unlock_irqrestore(&dwc->lock, flags);
1159
1160 return ret;
1161}
1162
1163static int dwc3_gadget_ep_dequeue(struct usb_ep *ep,
1164 struct usb_request *request)
1165{
1166 struct dwc3_request *req = to_dwc3_request(request);
1167 struct dwc3_request *r = NULL;
1168
1169 struct dwc3_ep *dep = to_dwc3_ep(ep);
1170 struct dwc3 *dwc = dep->dwc;
1171
1172 unsigned long flags;
1173 int ret = 0;
1174
1175 spin_lock_irqsave(&dwc->lock, flags);
1176
1177 list_for_each_entry(r, &dep->request_list, list) {
1178 if (r == req)
1179 break;
1180 }
1181
1182 if (r != req) {
1183 list_for_each_entry(r, &dep->req_queued, list) {
1184 if (r == req)
1185 break;
1186 }
1187 if (r == req) {
1188 /* wait until it is processed */
1189 dwc3_stop_active_transfer(dwc, dep->number);
Pratyush Anandeaec3e92012-06-15 11:54:00 +05301190 goto out1;
Felipe Balbi72246da2011-08-19 18:10:58 +03001191 }
1192 dev_err(dwc->dev, "request %p was not queued to %s\n",
1193 request, ep->name);
1194 ret = -EINVAL;
1195 goto out0;
1196 }
1197
Pratyush Anandeaec3e92012-06-15 11:54:00 +05301198out1:
Felipe Balbi72246da2011-08-19 18:10:58 +03001199 /* giveback the request */
1200 dwc3_gadget_giveback(dep, req, -ECONNRESET);
1201
1202out0:
1203 spin_unlock_irqrestore(&dwc->lock, flags);
1204
1205 return ret;
1206}
1207
1208int __dwc3_gadget_ep_set_halt(struct dwc3_ep *dep, int value)
1209{
1210 struct dwc3_gadget_ep_cmd_params params;
1211 struct dwc3 *dwc = dep->dwc;
1212 int ret;
1213
1214 memset(&params, 0x00, sizeof(params));
1215
1216 if (value) {
Felipe Balbi0b7836a2011-08-30 15:48:08 +03001217 if (dep->number == 0 || dep->number == 1) {
1218 /*
1219 * Whenever EP0 is stalled, we will restart
1220 * the state machine, thus moving back to
1221 * Setup Phase
1222 */
1223 dwc->ep0state = EP0_SETUP_PHASE;
1224 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001225
1226 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number,
1227 DWC3_DEPCMD_SETSTALL, &params);
1228 if (ret)
1229 dev_err(dwc->dev, "failed to %s STALL on %s\n",
1230 value ? "set" : "clear",
1231 dep->name);
1232 else
1233 dep->flags |= DWC3_EP_STALL;
1234 } else {
Paul Zimmerman52754552011-09-30 10:58:44 +03001235 if (dep->flags & DWC3_EP_WEDGE)
1236 return 0;
1237
Felipe Balbi72246da2011-08-19 18:10:58 +03001238 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number,
1239 DWC3_DEPCMD_CLEARSTALL, &params);
1240 if (ret)
1241 dev_err(dwc->dev, "failed to %s STALL on %s\n",
1242 value ? "set" : "clear",
1243 dep->name);
1244 else
1245 dep->flags &= ~DWC3_EP_STALL;
1246 }
Paul Zimmerman52754552011-09-30 10:58:44 +03001247
Felipe Balbi72246da2011-08-19 18:10:58 +03001248 return ret;
1249}
1250
1251static int dwc3_gadget_ep_set_halt(struct usb_ep *ep, int value)
1252{
1253 struct dwc3_ep *dep = to_dwc3_ep(ep);
1254 struct dwc3 *dwc = dep->dwc;
1255
1256 unsigned long flags;
1257
1258 int ret;
1259
1260 spin_lock_irqsave(&dwc->lock, flags);
1261
Ido Shayevitz57cdac12012-03-12 20:25:24 +02001262 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001263 dev_err(dwc->dev, "%s is of Isochronous type\n", dep->name);
1264 ret = -EINVAL;
1265 goto out;
1266 }
1267
1268 ret = __dwc3_gadget_ep_set_halt(dep, value);
1269out:
1270 spin_unlock_irqrestore(&dwc->lock, flags);
1271
1272 return ret;
1273}
1274
1275static int dwc3_gadget_ep_set_wedge(struct usb_ep *ep)
1276{
1277 struct dwc3_ep *dep = to_dwc3_ep(ep);
Paul Zimmerman249a4562012-02-24 17:32:16 -08001278 struct dwc3 *dwc = dep->dwc;
1279 unsigned long flags;
Felipe Balbi72246da2011-08-19 18:10:58 +03001280
Paul Zimmerman249a4562012-02-24 17:32:16 -08001281 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001282 dep->flags |= DWC3_EP_WEDGE;
Paul Zimmerman249a4562012-02-24 17:32:16 -08001283 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001284
Paul Zimmerman52754552011-09-30 10:58:44 +03001285 return dwc3_gadget_ep_set_halt(ep, 1);
Felipe Balbi72246da2011-08-19 18:10:58 +03001286}
1287
1288/* -------------------------------------------------------------------------- */
1289
1290static struct usb_endpoint_descriptor dwc3_gadget_ep0_desc = {
1291 .bLength = USB_DT_ENDPOINT_SIZE,
1292 .bDescriptorType = USB_DT_ENDPOINT,
1293 .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
1294};
1295
1296static const struct usb_ep_ops dwc3_gadget_ep0_ops = {
1297 .enable = dwc3_gadget_ep0_enable,
1298 .disable = dwc3_gadget_ep0_disable,
1299 .alloc_request = dwc3_gadget_ep_alloc_request,
1300 .free_request = dwc3_gadget_ep_free_request,
1301 .queue = dwc3_gadget_ep0_queue,
1302 .dequeue = dwc3_gadget_ep_dequeue,
1303 .set_halt = dwc3_gadget_ep_set_halt,
1304 .set_wedge = dwc3_gadget_ep_set_wedge,
1305};
1306
1307static const struct usb_ep_ops dwc3_gadget_ep_ops = {
1308 .enable = dwc3_gadget_ep_enable,
1309 .disable = dwc3_gadget_ep_disable,
1310 .alloc_request = dwc3_gadget_ep_alloc_request,
1311 .free_request = dwc3_gadget_ep_free_request,
1312 .queue = dwc3_gadget_ep_queue,
1313 .dequeue = dwc3_gadget_ep_dequeue,
1314 .set_halt = dwc3_gadget_ep_set_halt,
1315 .set_wedge = dwc3_gadget_ep_set_wedge,
1316};
1317
1318/* -------------------------------------------------------------------------- */
1319
1320static int dwc3_gadget_get_frame(struct usb_gadget *g)
1321{
1322 struct dwc3 *dwc = gadget_to_dwc(g);
1323 u32 reg;
1324
1325 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1326 return DWC3_DSTS_SOFFN(reg);
1327}
1328
1329static int dwc3_gadget_wakeup(struct usb_gadget *g)
1330{
1331 struct dwc3 *dwc = gadget_to_dwc(g);
1332
1333 unsigned long timeout;
1334 unsigned long flags;
1335
1336 u32 reg;
1337
1338 int ret = 0;
1339
1340 u8 link_state;
1341 u8 speed;
1342
1343 spin_lock_irqsave(&dwc->lock, flags);
1344
1345 /*
1346 * According to the Databook Remote wakeup request should
1347 * be issued only when the device is in early suspend state.
1348 *
1349 * We can check that via USB Link State bits in DSTS register.
1350 */
1351 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1352
1353 speed = reg & DWC3_DSTS_CONNECTSPD;
1354 if (speed == DWC3_DSTS_SUPERSPEED) {
1355 dev_dbg(dwc->dev, "no wakeup on SuperSpeed\n");
1356 ret = -EINVAL;
1357 goto out;
1358 }
1359
1360 link_state = DWC3_DSTS_USBLNKST(reg);
1361
1362 switch (link_state) {
1363 case DWC3_LINK_STATE_RX_DET: /* in HS, means Early Suspend */
1364 case DWC3_LINK_STATE_U3: /* in HS, means SUSPEND */
1365 break;
1366 default:
1367 dev_dbg(dwc->dev, "can't wakeup from link state %d\n",
1368 link_state);
1369 ret = -EINVAL;
1370 goto out;
1371 }
1372
Felipe Balbi8598bde2012-01-02 18:55:57 +02001373 ret = dwc3_gadget_set_link_state(dwc, DWC3_LINK_STATE_RECOV);
1374 if (ret < 0) {
1375 dev_err(dwc->dev, "failed to put link in Recovery\n");
1376 goto out;
1377 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001378
Paul Zimmerman88df4272012-04-27 13:10:52 +03001379 /* Recent versions do this automatically */
1380 if (dwc->revision < DWC3_REVISION_194A) {
1381 /* write zeroes to Link Change Request */
Felipe Balbib4d04352012-05-24 10:27:56 +03001382 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
Paul Zimmerman88df4272012-04-27 13:10:52 +03001383 reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK;
1384 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1385 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001386
Paul Zimmerman1d046792012-02-15 18:56:56 -08001387 /* poll until Link State changes to ON */
Felipe Balbi72246da2011-08-19 18:10:58 +03001388 timeout = jiffies + msecs_to_jiffies(100);
1389
Paul Zimmerman1d046792012-02-15 18:56:56 -08001390 while (!time_after(jiffies, timeout)) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001391 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1392
1393 /* in HS, means ON */
1394 if (DWC3_DSTS_USBLNKST(reg) == DWC3_LINK_STATE_U0)
1395 break;
1396 }
1397
1398 if (DWC3_DSTS_USBLNKST(reg) != DWC3_LINK_STATE_U0) {
1399 dev_err(dwc->dev, "failed to send remote wakeup\n");
1400 ret = -EINVAL;
1401 }
1402
1403out:
1404 spin_unlock_irqrestore(&dwc->lock, flags);
1405
1406 return ret;
1407}
1408
1409static int dwc3_gadget_set_selfpowered(struct usb_gadget *g,
1410 int is_selfpowered)
1411{
1412 struct dwc3 *dwc = gadget_to_dwc(g);
Paul Zimmerman249a4562012-02-24 17:32:16 -08001413 unsigned long flags;
Felipe Balbi72246da2011-08-19 18:10:58 +03001414
Paul Zimmerman249a4562012-02-24 17:32:16 -08001415 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001416 dwc->is_selfpowered = !!is_selfpowered;
Paul Zimmerman249a4562012-02-24 17:32:16 -08001417 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001418
1419 return 0;
1420}
1421
1422static void dwc3_gadget_run_stop(struct dwc3 *dwc, int is_on)
1423{
1424 u32 reg;
Sebastian Andrzej Siewior61d58242011-08-29 16:46:38 +02001425 u32 timeout = 500;
Felipe Balbi72246da2011-08-19 18:10:58 +03001426
1427 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
Felipe Balbi8db7ed12012-01-18 18:32:29 +02001428 if (is_on) {
Paul Zimmerman88df4272012-04-27 13:10:52 +03001429 if (dwc->revision <= DWC3_REVISION_187A) {
1430 reg &= ~DWC3_DCTL_TRGTULST_MASK;
1431 reg |= DWC3_DCTL_TRGTULST_RX_DET;
1432 }
1433
1434 if (dwc->revision >= DWC3_REVISION_194A)
1435 reg &= ~DWC3_DCTL_KEEP_CONNECT;
1436 reg |= DWC3_DCTL_RUN_STOP;
Felipe Balbi8db7ed12012-01-18 18:32:29 +02001437 } else {
Felipe Balbi72246da2011-08-19 18:10:58 +03001438 reg &= ~DWC3_DCTL_RUN_STOP;
Felipe Balbi8db7ed12012-01-18 18:32:29 +02001439 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001440
1441 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1442
1443 do {
1444 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1445 if (is_on) {
1446 if (!(reg & DWC3_DSTS_DEVCTRLHLT))
1447 break;
1448 } else {
1449 if (reg & DWC3_DSTS_DEVCTRLHLT)
1450 break;
1451 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001452 timeout--;
1453 if (!timeout)
1454 break;
Sebastian Andrzej Siewior61d58242011-08-29 16:46:38 +02001455 udelay(1);
Felipe Balbi72246da2011-08-19 18:10:58 +03001456 } while (1);
1457
1458 dev_vdbg(dwc->dev, "gadget %s data soft-%s\n",
1459 dwc->gadget_driver
1460 ? dwc->gadget_driver->function : "no-function",
1461 is_on ? "connect" : "disconnect");
1462}
1463
1464static int dwc3_gadget_pullup(struct usb_gadget *g, int is_on)
1465{
1466 struct dwc3 *dwc = gadget_to_dwc(g);
1467 unsigned long flags;
1468
1469 is_on = !!is_on;
1470
1471 spin_lock_irqsave(&dwc->lock, flags);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +02001472
1473 dwc->softconnect = is_on;
1474
1475 if ((dwc->dotg && !dwc->vbus_active) ||
1476 !dwc->gadget_driver) {
1477
1478 spin_unlock_irqrestore(&dwc->lock, flags);
1479
1480 /*
1481 * Need to wait for vbus_session(on) from otg driver or to
1482 * the udc_start.
1483 */
1484 return 0;
1485 }
1486
Felipe Balbi72246da2011-08-19 18:10:58 +03001487 dwc3_gadget_run_stop(dwc, is_on);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +02001488
1489 spin_unlock_irqrestore(&dwc->lock, flags);
1490
1491 return 0;
1492}
1493
1494static int dwc3_gadget_vbus_session(struct usb_gadget *_gadget, int is_active)
1495{
1496 struct dwc3 *dwc = gadget_to_dwc(_gadget);
1497 unsigned long flags;
1498
1499 if (!dwc->dotg)
1500 return -EPERM;
1501
1502 is_active = !!is_active;
1503
1504 spin_lock_irqsave(&dwc->lock, flags);
1505
1506 /* Mark that the vbus was powered */
1507 dwc->vbus_active = is_active;
1508
1509 /*
1510 * Check if upper level usb_gadget_driver was already registerd with
1511 * this udc controller driver (if dwc3_gadget_start was called)
1512 */
1513 if (dwc->gadget_driver && dwc->softconnect) {
1514 if (dwc->vbus_active) {
1515 /*
1516 * Both vbus was activated by otg and pullup was
1517 * signaled by the gadget driver.
1518 */
1519 dwc3_gadget_run_stop(dwc, 1);
1520 } else {
1521 dwc3_gadget_run_stop(dwc, 0);
1522 }
1523 }
1524
Felipe Balbi72246da2011-08-19 18:10:58 +03001525 spin_unlock_irqrestore(&dwc->lock, flags);
1526
1527 return 0;
1528}
1529
1530static int dwc3_gadget_start(struct usb_gadget *g,
1531 struct usb_gadget_driver *driver)
1532{
1533 struct dwc3 *dwc = gadget_to_dwc(g);
1534 struct dwc3_ep *dep;
1535 unsigned long flags;
1536 int ret = 0;
1537 u32 reg;
1538
1539 spin_lock_irqsave(&dwc->lock, flags);
1540
1541 if (dwc->gadget_driver) {
1542 dev_err(dwc->dev, "%s is already bound to %s\n",
1543 dwc->gadget.name,
1544 dwc->gadget_driver->driver.name);
1545 ret = -EBUSY;
1546 goto err0;
1547 }
1548
1549 dwc->gadget_driver = driver;
1550 dwc->gadget.dev.driver = &driver->driver;
1551
Felipe Balbi72246da2011-08-19 18:10:58 +03001552 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
1553 reg &= ~(DWC3_DCFG_SPEED_MASK);
Felipe Balbi38d2c6c2012-03-23 12:20:31 +02001554
1555 /**
1556 * WORKAROUND: DWC3 revision < 2.20a have an issue
1557 * which would cause metastability state on Run/Stop
1558 * bit if we try to force the IP to USB2-only mode.
1559 *
1560 * Because of that, we cannot configure the IP to any
1561 * speed other than the SuperSpeed
1562 *
1563 * Refers to:
1564 *
1565 * STAR#9000525659: Clock Domain Crossing on DCTL in
1566 * USB 2.0 Mode
1567 */
1568 if (dwc->revision < DWC3_REVISION_220A)
1569 reg |= DWC3_DCFG_SUPERSPEED;
1570 else
1571 reg |= dwc->maximum_speed;
Felipe Balbi72246da2011-08-19 18:10:58 +03001572 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
1573
Paul Zimmermanb23c8432011-09-30 10:58:42 +03001574 dwc->start_config_issued = false;
1575
Felipe Balbi72246da2011-08-19 18:10:58 +03001576 /* Start with SuperSpeed Default */
1577 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
1578
1579 dep = dwc->eps[0];
Felipe Balbic90bfae2011-11-29 13:11:21 +02001580 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL);
Felipe Balbi72246da2011-08-19 18:10:58 +03001581 if (ret) {
1582 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
1583 goto err0;
1584 }
1585
1586 dep = dwc->eps[1];
Felipe Balbic90bfae2011-11-29 13:11:21 +02001587 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL);
Felipe Balbi72246da2011-08-19 18:10:58 +03001588 if (ret) {
1589 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
1590 goto err1;
1591 }
1592
1593 /* begin to receive SETUP packets */
Felipe Balbic7fcdeb2011-08-27 22:28:36 +03001594 dwc->ep0state = EP0_SETUP_PHASE;
Felipe Balbi72246da2011-08-19 18:10:58 +03001595 dwc3_ep0_out_start(dwc);
1596
1597 spin_unlock_irqrestore(&dwc->lock, flags);
1598
1599 return 0;
1600
1601err1:
1602 __dwc3_gadget_ep_disable(dwc->eps[0]);
1603
1604err0:
1605 spin_unlock_irqrestore(&dwc->lock, flags);
1606
1607 return ret;
1608}
1609
1610static int dwc3_gadget_stop(struct usb_gadget *g,
1611 struct usb_gadget_driver *driver)
1612{
1613 struct dwc3 *dwc = gadget_to_dwc(g);
1614 unsigned long flags;
1615
1616 spin_lock_irqsave(&dwc->lock, flags);
1617
1618 __dwc3_gadget_ep_disable(dwc->eps[0]);
1619 __dwc3_gadget_ep_disable(dwc->eps[1]);
1620
1621 dwc->gadget_driver = NULL;
1622 dwc->gadget.dev.driver = NULL;
1623
1624 spin_unlock_irqrestore(&dwc->lock, flags);
1625
1626 return 0;
1627}
Paul Zimmerman88df4272012-04-27 13:10:52 +03001628
Felipe Balbi72246da2011-08-19 18:10:58 +03001629static const struct usb_gadget_ops dwc3_gadget_ops = {
1630 .get_frame = dwc3_gadget_get_frame,
1631 .wakeup = dwc3_gadget_wakeup,
1632 .set_selfpowered = dwc3_gadget_set_selfpowered,
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +02001633 .vbus_session = dwc3_gadget_vbus_session,
Felipe Balbi72246da2011-08-19 18:10:58 +03001634 .pullup = dwc3_gadget_pullup,
1635 .udc_start = dwc3_gadget_start,
1636 .udc_stop = dwc3_gadget_stop,
1637};
1638
1639/* -------------------------------------------------------------------------- */
1640
1641static int __devinit dwc3_gadget_init_endpoints(struct dwc3 *dwc)
1642{
1643 struct dwc3_ep *dep;
1644 u8 epnum;
1645
1646 INIT_LIST_HEAD(&dwc->gadget.ep_list);
1647
1648 for (epnum = 0; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
1649 dep = kzalloc(sizeof(*dep), GFP_KERNEL);
1650 if (!dep) {
1651 dev_err(dwc->dev, "can't allocate endpoint %d\n",
1652 epnum);
1653 return -ENOMEM;
1654 }
1655
1656 dep->dwc = dwc;
1657 dep->number = epnum;
1658 dwc->eps[epnum] = dep;
1659
1660 snprintf(dep->name, sizeof(dep->name), "ep%d%s", epnum >> 1,
1661 (epnum & 1) ? "in" : "out");
1662 dep->endpoint.name = dep->name;
1663 dep->direction = (epnum & 1);
1664
1665 if (epnum == 0 || epnum == 1) {
1666 dep->endpoint.maxpacket = 512;
1667 dep->endpoint.ops = &dwc3_gadget_ep0_ops;
1668 if (!epnum)
1669 dwc->gadget.ep0 = &dep->endpoint;
1670 } else {
1671 int ret;
1672
1673 dep->endpoint.maxpacket = 1024;
Sebastian Andrzej Siewior12d36c12011-11-03 20:27:50 +01001674 dep->endpoint.max_streams = 15;
Felipe Balbi72246da2011-08-19 18:10:58 +03001675 dep->endpoint.ops = &dwc3_gadget_ep_ops;
1676 list_add_tail(&dep->endpoint.ep_list,
1677 &dwc->gadget.ep_list);
1678
1679 ret = dwc3_alloc_trb_pool(dep);
Felipe Balbi25b8ff62011-11-04 12:32:47 +02001680 if (ret)
Felipe Balbi72246da2011-08-19 18:10:58 +03001681 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001682 }
Felipe Balbi25b8ff62011-11-04 12:32:47 +02001683
Felipe Balbi72246da2011-08-19 18:10:58 +03001684 INIT_LIST_HEAD(&dep->request_list);
1685 INIT_LIST_HEAD(&dep->req_queued);
1686 }
1687
1688 return 0;
1689}
1690
1691static void dwc3_gadget_free_endpoints(struct dwc3 *dwc)
1692{
1693 struct dwc3_ep *dep;
1694 u8 epnum;
1695
1696 for (epnum = 0; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
1697 dep = dwc->eps[epnum];
1698 dwc3_free_trb_pool(dep);
1699
1700 if (epnum != 0 && epnum != 1)
1701 list_del(&dep->endpoint.ep_list);
1702
1703 kfree(dep);
1704 }
1705}
1706
1707static void dwc3_gadget_release(struct device *dev)
1708{
1709 dev_dbg(dev, "%s\n", __func__);
1710}
1711
1712/* -------------------------------------------------------------------------- */
1713static int dwc3_cleanup_done_reqs(struct dwc3 *dwc, struct dwc3_ep *dep,
1714 const struct dwc3_event_depevt *event, int status)
1715{
1716 struct dwc3_request *req;
Felipe Balbif6bafc62012-02-06 11:04:53 +02001717 struct dwc3_trb *trb;
Felipe Balbi72246da2011-08-19 18:10:58 +03001718 unsigned int count;
1719 unsigned int s_pkt = 0;
Pratyush Anand73939b02012-05-25 18:54:56 +05301720 unsigned int trb_status;
Felipe Balbi72246da2011-08-19 18:10:58 +03001721
1722 do {
1723 req = next_request(&dep->req_queued);
Sebastian Andrzej Siewiord39ee7b2011-11-03 10:32:20 +01001724 if (!req) {
1725 WARN_ON_ONCE(1);
1726 return 1;
1727 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001728
Felipe Balbif6bafc62012-02-06 11:04:53 +02001729 trb = req->trb;
Felipe Balbi72246da2011-08-19 18:10:58 +03001730
Felipe Balbif6bafc62012-02-06 11:04:53 +02001731 if ((trb->ctrl & DWC3_TRB_CTRL_HWO) && status != -ESHUTDOWN)
Sebastian Andrzej Siewior0d2f4752011-08-19 19:59:12 +02001732 /*
1733 * We continue despite the error. There is not much we
Paul Zimmerman1d046792012-02-15 18:56:56 -08001734 * can do. If we don't clean it up we loop forever. If
1735 * we skip the TRB then it gets overwritten after a
1736 * while since we use them in a ring buffer. A BUG()
1737 * would help. Lets hope that if this occurs, someone
Sebastian Andrzej Siewior0d2f4752011-08-19 19:59:12 +02001738 * fixes the root cause instead of looking away :)
1739 */
Felipe Balbi72246da2011-08-19 18:10:58 +03001740 dev_err(dwc->dev, "%s's TRB (%p) still owned by HW\n",
1741 dep->name, req->trb);
Felipe Balbif6bafc62012-02-06 11:04:53 +02001742 count = trb->size & DWC3_TRB_SIZE_MASK;
Felipe Balbi72246da2011-08-19 18:10:58 +03001743
1744 if (dep->direction) {
1745 if (count) {
Pratyush Anand73939b02012-05-25 18:54:56 +05301746 trb_status = DWC3_TRB_SIZE_TRBSTS(trb->size);
1747 if (trb_status == DWC3_TRBSTS_MISSED_ISOC) {
1748 dev_dbg(dwc->dev, "incomplete IN transfer %s\n",
1749 dep->name);
1750 dep->current_uf = event->parameters &
1751 ~(dep->interval - 1);
1752 dep->flags |= DWC3_EP_MISSED_ISOC;
1753 } else {
1754 dev_err(dwc->dev, "incomplete IN transfer %s\n",
1755 dep->name);
1756 status = -ECONNRESET;
1757 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001758 }
1759 } else {
1760 if (count && (event->status & DEPEVT_STATUS_SHORT))
1761 s_pkt = 1;
1762 }
1763
1764 /*
1765 * We assume here we will always receive the entire data block
1766 * which we should receive. Meaning, if we program RX to
1767 * receive 4K but we receive only 2K, we assume that's all we
1768 * should receive and we simply bounce the request back to the
1769 * gadget driver for further processing.
1770 */
1771 req->request.actual += req->request.length - count;
1772 dwc3_gadget_giveback(dep, req, status);
1773 if (s_pkt)
1774 break;
Felipe Balbif6bafc62012-02-06 11:04:53 +02001775 if ((event->status & DEPEVT_STATUS_LST) &&
Pratyush Anand413dba62012-06-03 19:43:19 +05301776 (trb->ctrl & (DWC3_TRB_CTRL_LST |
1777 DWC3_TRB_CTRL_HWO)))
Felipe Balbi72246da2011-08-19 18:10:58 +03001778 break;
Felipe Balbif6bafc62012-02-06 11:04:53 +02001779 if ((event->status & DEPEVT_STATUS_IOC) &&
1780 (trb->ctrl & DWC3_TRB_CTRL_IOC))
Felipe Balbi72246da2011-08-19 18:10:58 +03001781 break;
1782 } while (1);
1783
Felipe Balbif6bafc62012-02-06 11:04:53 +02001784 if ((event->status & DEPEVT_STATUS_IOC) &&
1785 (trb->ctrl & DWC3_TRB_CTRL_IOC))
Felipe Balbi72246da2011-08-19 18:10:58 +03001786 return 0;
1787 return 1;
1788}
1789
1790static void dwc3_endpoint_transfer_complete(struct dwc3 *dwc,
1791 struct dwc3_ep *dep, const struct dwc3_event_depevt *event,
1792 int start_new)
1793{
1794 unsigned status = 0;
1795 int clean_busy;
1796
1797 if (event->status & DEPEVT_STATUS_BUSERR)
1798 status = -ECONNRESET;
1799
Paul Zimmerman1d046792012-02-15 18:56:56 -08001800 clean_busy = dwc3_cleanup_done_reqs(dwc, dep, event, status);
Paul Zimmermanc2df85c2012-02-24 17:32:18 -08001801 if (clean_busy)
Felipe Balbi72246da2011-08-19 18:10:58 +03001802 dep->flags &= ~DWC3_EP_BUSY;
Felipe Balbifae2b902011-10-14 13:00:30 +03001803
1804 /*
1805 * WORKAROUND: This is the 2nd half of U1/U2 -> U0 workaround.
1806 * See dwc3_gadget_linksts_change_interrupt() for 1st half.
1807 */
1808 if (dwc->revision < DWC3_REVISION_183A) {
1809 u32 reg;
1810 int i;
1811
1812 for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) {
1813 struct dwc3_ep *dep = dwc->eps[i];
1814
1815 if (!(dep->flags & DWC3_EP_ENABLED))
1816 continue;
1817
1818 if (!list_empty(&dep->req_queued))
1819 return;
1820 }
1821
1822 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
1823 reg |= dwc->u1u2;
1824 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1825
1826 dwc->u1u2 = 0;
1827 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001828}
1829
Felipe Balbi72246da2011-08-19 18:10:58 +03001830static void dwc3_endpoint_interrupt(struct dwc3 *dwc,
1831 const struct dwc3_event_depevt *event)
1832{
1833 struct dwc3_ep *dep;
1834 u8 epnum = event->endpoint_number;
1835
1836 dep = dwc->eps[epnum];
1837
Felipe Balbia09be0a2012-06-06 09:19:35 +03001838 if (!(dep->flags & DWC3_EP_ENABLED))
1839 return;
1840
Felipe Balbi72246da2011-08-19 18:10:58 +03001841 dev_vdbg(dwc->dev, "%s: %s\n", dep->name,
1842 dwc3_ep_event_string(event->endpoint_event));
1843
1844 if (epnum == 0 || epnum == 1) {
1845 dwc3_ep0_interrupt(dwc, event);
1846 return;
1847 }
1848
1849 switch (event->endpoint_event) {
1850 case DWC3_DEPEVT_XFERCOMPLETE:
Paul Zimmermanc2df85c2012-02-24 17:32:18 -08001851 dep->res_trans_idx = 0;
1852
Ido Shayevitz57cdac12012-03-12 20:25:24 +02001853 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001854 dev_dbg(dwc->dev, "%s is an Isochronous endpoint\n",
1855 dep->name);
1856 return;
1857 }
1858
1859 dwc3_endpoint_transfer_complete(dwc, dep, event, 1);
1860 break;
1861 case DWC3_DEPEVT_XFERINPROGRESS:
Ido Shayevitz57cdac12012-03-12 20:25:24 +02001862 if (!usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001863 dev_dbg(dwc->dev, "%s is not an Isochronous endpoint\n",
1864 dep->name);
1865 return;
1866 }
1867
1868 dwc3_endpoint_transfer_complete(dwc, dep, event, 0);
1869 break;
1870 case DWC3_DEPEVT_XFERNOTREADY:
Ido Shayevitz57cdac12012-03-12 20:25:24 +02001871 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001872 dwc3_gadget_start_isoc(dwc, dep, event);
1873 } else {
1874 int ret;
1875
1876 dev_vdbg(dwc->dev, "%s: reason %s\n",
Felipe Balbi40aa41f2012-01-18 17:06:03 +02001877 dep->name, event->status &
1878 DEPEVT_STATUS_TRANSFER_ACTIVE
Felipe Balbi72246da2011-08-19 18:10:58 +03001879 ? "Transfer Active"
1880 : "Transfer Not Active");
1881
1882 ret = __dwc3_gadget_kick_transfer(dep, 0, 1);
1883 if (!ret || ret == -EBUSY)
1884 return;
1885
1886 dev_dbg(dwc->dev, "%s: failed to kick transfers\n",
1887 dep->name);
1888 }
1889
1890 break;
Felipe Balbi879631a2011-09-30 10:58:47 +03001891 case DWC3_DEPEVT_STREAMEVT:
Ido Shayevitz57cdac12012-03-12 20:25:24 +02001892 if (!usb_endpoint_xfer_bulk(dep->endpoint.desc)) {
Felipe Balbi879631a2011-09-30 10:58:47 +03001893 dev_err(dwc->dev, "Stream event for non-Bulk %s\n",
1894 dep->name);
1895 return;
1896 }
1897
1898 switch (event->status) {
1899 case DEPEVT_STREAMEVT_FOUND:
1900 dev_vdbg(dwc->dev, "Stream %d found and started\n",
1901 event->parameters);
1902
1903 break;
1904 case DEPEVT_STREAMEVT_NOTFOUND:
1905 /* FALLTHROUGH */
1906 default:
1907 dev_dbg(dwc->dev, "Couldn't find suitable stream\n");
1908 }
1909 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03001910 case DWC3_DEPEVT_RXTXFIFOEVT:
1911 dev_dbg(dwc->dev, "%s FIFO Overrun\n", dep->name);
1912 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03001913 case DWC3_DEPEVT_EPCMDCMPLT:
Felipe Balbib129eb72012-02-17 12:10:04 +02001914 dev_vdbg(dwc->dev, "Endpoint Command Complete\n");
Felipe Balbi72246da2011-08-19 18:10:58 +03001915 break;
1916 }
1917}
1918
1919static void dwc3_disconnect_gadget(struct dwc3 *dwc)
1920{
1921 if (dwc->gadget_driver && dwc->gadget_driver->disconnect) {
1922 spin_unlock(&dwc->lock);
1923 dwc->gadget_driver->disconnect(&dwc->gadget);
1924 spin_lock(&dwc->lock);
1925 }
1926}
1927
1928static void dwc3_stop_active_transfer(struct dwc3 *dwc, u32 epnum)
1929{
1930 struct dwc3_ep *dep;
1931 struct dwc3_gadget_ep_cmd_params params;
1932 u32 cmd;
1933 int ret;
1934
1935 dep = dwc->eps[epnum];
1936
Pratyush Anand6263ebe2012-06-23 02:23:08 +05301937 if (!dep->res_trans_idx)
1938 return;
1939
1940 cmd = DWC3_DEPCMD_ENDTRANSFER;
1941 cmd |= DWC3_DEPCMD_HIPRI_FORCERM | DWC3_DEPCMD_CMDIOC;
1942 cmd |= DWC3_DEPCMD_PARAM(dep->res_trans_idx);
1943 memset(&params, 0, sizeof(params));
1944 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number, cmd, &params);
1945 WARN_ON_ONCE(ret);
1946 dep->res_trans_idx = 0;
Felipe Balbi72246da2011-08-19 18:10:58 +03001947}
1948
1949static void dwc3_stop_active_transfers(struct dwc3 *dwc)
1950{
1951 u32 epnum;
1952
1953 for (epnum = 2; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
1954 struct dwc3_ep *dep;
1955
1956 dep = dwc->eps[epnum];
1957 if (!(dep->flags & DWC3_EP_ENABLED))
1958 continue;
1959
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +02001960 dwc3_remove_requests(dwc, dep);
Felipe Balbi72246da2011-08-19 18:10:58 +03001961 }
1962}
1963
1964static void dwc3_clear_stall_all_ep(struct dwc3 *dwc)
1965{
1966 u32 epnum;
1967
1968 for (epnum = 1; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
1969 struct dwc3_ep *dep;
1970 struct dwc3_gadget_ep_cmd_params params;
1971 int ret;
1972
1973 dep = dwc->eps[epnum];
1974
1975 if (!(dep->flags & DWC3_EP_STALL))
1976 continue;
1977
1978 dep->flags &= ~DWC3_EP_STALL;
1979
1980 memset(&params, 0, sizeof(params));
1981 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number,
1982 DWC3_DEPCMD_CLEARSTALL, &params);
1983 WARN_ON_ONCE(ret);
1984 }
1985}
1986
1987static void dwc3_gadget_disconnect_interrupt(struct dwc3 *dwc)
1988{
Felipe Balbi34d548c2012-05-24 10:30:01 +03001989 int reg;
1990
Felipe Balbi72246da2011-08-19 18:10:58 +03001991 dev_vdbg(dwc->dev, "%s\n", __func__);
Felipe Balbi72246da2011-08-19 18:10:58 +03001992
1993 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
1994 reg &= ~DWC3_DCTL_INITU1ENA;
1995 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1996
1997 reg &= ~DWC3_DCTL_INITU2ENA;
1998 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +03001999
Felipe Balbi72246da2011-08-19 18:10:58 +03002000 dwc3_disconnect_gadget(dwc);
Paul Zimmermanb23c8432011-09-30 10:58:42 +03002001 dwc->start_config_issued = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03002002
2003 dwc->gadget.speed = USB_SPEED_UNKNOWN;
Felipe Balbidf62df52011-10-14 15:11:49 +03002004 dwc->setup_packet_pending = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03002005}
2006
Paul Zimmermandffb81b2012-04-27 12:54:05 +03002007static void dwc3_gadget_usb3_phy_suspend(struct dwc3 *dwc, int suspend)
Felipe Balbi72246da2011-08-19 18:10:58 +03002008{
2009 u32 reg;
2010
2011 reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0));
2012
Paul Zimmermandffb81b2012-04-27 12:54:05 +03002013 if (suspend)
Felipe Balbi72246da2011-08-19 18:10:58 +03002014 reg |= DWC3_GUSB3PIPECTL_SUSPHY;
Paul Zimmermandffb81b2012-04-27 12:54:05 +03002015 else
2016 reg &= ~DWC3_GUSB3PIPECTL_SUSPHY;
Felipe Balbi72246da2011-08-19 18:10:58 +03002017
2018 dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg);
2019}
2020
Paul Zimmermandffb81b2012-04-27 12:54:05 +03002021static void dwc3_gadget_usb2_phy_suspend(struct dwc3 *dwc, int suspend)
Felipe Balbi72246da2011-08-19 18:10:58 +03002022{
2023 u32 reg;
2024
2025 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
2026
Paul Zimmermandffb81b2012-04-27 12:54:05 +03002027 if (suspend)
Felipe Balbi72246da2011-08-19 18:10:58 +03002028 reg |= DWC3_GUSB2PHYCFG_SUSPHY;
Paul Zimmermandffb81b2012-04-27 12:54:05 +03002029 else
2030 reg &= ~DWC3_GUSB2PHYCFG_SUSPHY;
Felipe Balbi72246da2011-08-19 18:10:58 +03002031
2032 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
2033}
2034
2035static void dwc3_gadget_reset_interrupt(struct dwc3 *dwc)
2036{
2037 u32 reg;
2038
2039 dev_vdbg(dwc->dev, "%s\n", __func__);
2040
Felipe Balbidf62df52011-10-14 15:11:49 +03002041 /*
2042 * WORKAROUND: DWC3 revisions <1.88a have an issue which
2043 * would cause a missing Disconnect Event if there's a
2044 * pending Setup Packet in the FIFO.
2045 *
2046 * There's no suggested workaround on the official Bug
2047 * report, which states that "unless the driver/application
2048 * is doing any special handling of a disconnect event,
2049 * there is no functional issue".
2050 *
2051 * Unfortunately, it turns out that we _do_ some special
2052 * handling of a disconnect event, namely complete all
2053 * pending transfers, notify gadget driver of the
2054 * disconnection, and so on.
2055 *
2056 * Our suggested workaround is to follow the Disconnect
2057 * Event steps here, instead, based on a setup_packet_pending
2058 * flag. Such flag gets set whenever we have a XferNotReady
2059 * event on EP0 and gets cleared on XferComplete for the
2060 * same endpoint.
2061 *
2062 * Refers to:
2063 *
2064 * STAR#9000466709: RTL: Device : Disconnect event not
2065 * generated if setup packet pending in FIFO
2066 */
2067 if (dwc->revision < DWC3_REVISION_188A) {
2068 if (dwc->setup_packet_pending)
2069 dwc3_gadget_disconnect_interrupt(dwc);
2070 }
2071
Felipe Balbi961906e2011-12-20 15:37:21 +02002072 /* after reset -> Default State */
2073 dwc->dev_state = DWC3_DEFAULT_STATE;
2074
Paul Zimmerman88df4272012-04-27 13:10:52 +03002075 /* Recent versions support automatic phy suspend and don't need this */
2076 if (dwc->revision < DWC3_REVISION_194A) {
2077 /* Resume PHYs */
2078 dwc3_gadget_usb2_phy_suspend(dwc, false);
2079 dwc3_gadget_usb3_phy_suspend(dwc, false);
2080 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002081
2082 if (dwc->gadget.speed != USB_SPEED_UNKNOWN)
2083 dwc3_disconnect_gadget(dwc);
2084
2085 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2086 reg &= ~DWC3_DCTL_TSTCTRL_MASK;
Sebastian Andrzej Siewiorbb8b8a32011-09-13 17:54:39 +02002087 reg &= ~(DWC3_DCTL_INITU1ENA | DWC3_DCTL_INITU2ENA);
Gerard CAUVYd8bb4f72012-05-24 12:47:36 +03002088 reg |= (DWC3_DCTL_ACCEPTU1ENA | DWC3_DCTL_ACCEPTU2ENA);
Felipe Balbi72246da2011-08-19 18:10:58 +03002089 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Gerard Cauvy3b637362012-02-10 12:21:18 +02002090 dwc->test_mode = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03002091
2092 dwc3_stop_active_transfers(dwc);
2093 dwc3_clear_stall_all_ep(dwc);
Paul Zimmermanb23c8432011-09-30 10:58:42 +03002094 dwc->start_config_issued = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03002095
2096 /* Reset device address to zero */
2097 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2098 reg &= ~(DWC3_DCFG_DEVADDR_MASK);
2099 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +03002100}
2101
2102static void dwc3_update_ram_clk_sel(struct dwc3 *dwc, u32 speed)
2103{
2104 u32 reg;
2105 u32 usb30_clock = DWC3_GCTL_CLK_BUS;
2106
2107 /*
2108 * We change the clock only at SS but I dunno why I would want to do
2109 * this. Maybe it becomes part of the power saving plan.
2110 */
2111
2112 if (speed != DWC3_DSTS_SUPERSPEED)
2113 return;
2114
2115 /*
2116 * RAMClkSel is reset to 0 after USB reset, so it must be reprogrammed
2117 * each time on Connect Done.
2118 */
2119 if (!usb30_clock)
2120 return;
2121
2122 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
2123 reg |= DWC3_GCTL_RAMCLKSEL(usb30_clock);
2124 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
2125}
2126
Paul Zimmermandffb81b2012-04-27 12:54:05 +03002127static void dwc3_gadget_phy_suspend(struct dwc3 *dwc, u8 speed)
Felipe Balbi72246da2011-08-19 18:10:58 +03002128{
2129 switch (speed) {
2130 case USB_SPEED_SUPER:
Paul Zimmermandffb81b2012-04-27 12:54:05 +03002131 dwc3_gadget_usb2_phy_suspend(dwc, true);
Felipe Balbi72246da2011-08-19 18:10:58 +03002132 break;
2133 case USB_SPEED_HIGH:
2134 case USB_SPEED_FULL:
2135 case USB_SPEED_LOW:
Paul Zimmermandffb81b2012-04-27 12:54:05 +03002136 dwc3_gadget_usb3_phy_suspend(dwc, true);
Felipe Balbi72246da2011-08-19 18:10:58 +03002137 break;
2138 }
2139}
2140
2141static void dwc3_gadget_conndone_interrupt(struct dwc3 *dwc)
2142{
2143 struct dwc3_gadget_ep_cmd_params params;
2144 struct dwc3_ep *dep;
2145 int ret;
2146 u32 reg;
2147 u8 speed;
2148
2149 dev_vdbg(dwc->dev, "%s\n", __func__);
2150
2151 memset(&params, 0x00, sizeof(params));
2152
Felipe Balbi72246da2011-08-19 18:10:58 +03002153 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
2154 speed = reg & DWC3_DSTS_CONNECTSPD;
2155 dwc->speed = speed;
2156
2157 dwc3_update_ram_clk_sel(dwc, speed);
2158
2159 switch (speed) {
2160 case DWC3_DCFG_SUPERSPEED:
Felipe Balbi05870c52011-10-14 14:51:38 +03002161 /*
2162 * WORKAROUND: DWC3 revisions <1.90a have an issue which
2163 * would cause a missing USB3 Reset event.
2164 *
2165 * In such situations, we should force a USB3 Reset
2166 * event by calling our dwc3_gadget_reset_interrupt()
2167 * routine.
2168 *
2169 * Refers to:
2170 *
2171 * STAR#9000483510: RTL: SS : USB3 reset event may
2172 * not be generated always when the link enters poll
2173 */
2174 if (dwc->revision < DWC3_REVISION_190A)
2175 dwc3_gadget_reset_interrupt(dwc);
2176
Felipe Balbi72246da2011-08-19 18:10:58 +03002177 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
2178 dwc->gadget.ep0->maxpacket = 512;
2179 dwc->gadget.speed = USB_SPEED_SUPER;
2180 break;
2181 case DWC3_DCFG_HIGHSPEED:
2182 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
2183 dwc->gadget.ep0->maxpacket = 64;
2184 dwc->gadget.speed = USB_SPEED_HIGH;
2185 break;
2186 case DWC3_DCFG_FULLSPEED2:
2187 case DWC3_DCFG_FULLSPEED1:
2188 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
2189 dwc->gadget.ep0->maxpacket = 64;
2190 dwc->gadget.speed = USB_SPEED_FULL;
2191 break;
2192 case DWC3_DCFG_LOWSPEED:
2193 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(8);
2194 dwc->gadget.ep0->maxpacket = 8;
2195 dwc->gadget.speed = USB_SPEED_LOW;
2196 break;
2197 }
2198
Paul Zimmerman88df4272012-04-27 13:10:52 +03002199 /* Recent versions support automatic phy suspend and don't need this */
2200 if (dwc->revision < DWC3_REVISION_194A) {
2201 /* Suspend unneeded PHY */
2202 dwc3_gadget_phy_suspend(dwc, dwc->gadget.speed);
2203 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002204
2205 dep = dwc->eps[0];
Felipe Balbic90bfae2011-11-29 13:11:21 +02002206 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL);
Felipe Balbi72246da2011-08-19 18:10:58 +03002207 if (ret) {
2208 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
2209 return;
2210 }
2211
2212 dep = dwc->eps[1];
Felipe Balbic90bfae2011-11-29 13:11:21 +02002213 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL);
Felipe Balbi72246da2011-08-19 18:10:58 +03002214 if (ret) {
2215 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
2216 return;
2217 }
2218
2219 /*
2220 * Configure PHY via GUSB3PIPECTLn if required.
2221 *
2222 * Update GTXFIFOSIZn
2223 *
2224 * In both cases reset values should be sufficient.
2225 */
2226}
2227
2228static void dwc3_gadget_wakeup_interrupt(struct dwc3 *dwc)
2229{
2230 dev_vdbg(dwc->dev, "%s\n", __func__);
2231
2232 /*
2233 * TODO take core out of low power mode when that's
2234 * implemented.
2235 */
2236
2237 dwc->gadget_driver->resume(&dwc->gadget);
2238}
2239
2240static void dwc3_gadget_linksts_change_interrupt(struct dwc3 *dwc,
2241 unsigned int evtinfo)
2242{
Felipe Balbifae2b902011-10-14 13:00:30 +03002243 enum dwc3_link_state next = evtinfo & DWC3_LINK_STATE_MASK;
2244
2245 /*
2246 * WORKAROUND: DWC3 Revisions <1.83a have an issue which, depending
2247 * on the link partner, the USB session might do multiple entry/exit
2248 * of low power states before a transfer takes place.
2249 *
2250 * Due to this problem, we might experience lower throughput. The
2251 * suggested workaround is to disable DCTL[12:9] bits if we're
2252 * transitioning from U1/U2 to U0 and enable those bits again
2253 * after a transfer completes and there are no pending transfers
2254 * on any of the enabled endpoints.
2255 *
2256 * This is the first half of that workaround.
2257 *
2258 * Refers to:
2259 *
2260 * STAR#9000446952: RTL: Device SS : if U1/U2 ->U0 takes >128us
2261 * core send LGO_Ux entering U0
2262 */
2263 if (dwc->revision < DWC3_REVISION_183A) {
2264 if (next == DWC3_LINK_STATE_U0) {
2265 u32 u1u2;
2266 u32 reg;
2267
2268 switch (dwc->link_state) {
2269 case DWC3_LINK_STATE_U1:
2270 case DWC3_LINK_STATE_U2:
2271 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2272 u1u2 = reg & (DWC3_DCTL_INITU2ENA
2273 | DWC3_DCTL_ACCEPTU2ENA
2274 | DWC3_DCTL_INITU1ENA
2275 | DWC3_DCTL_ACCEPTU1ENA);
2276
2277 if (!dwc->u1u2)
2278 dwc->u1u2 = reg & u1u2;
2279
2280 reg &= ~u1u2;
2281
2282 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2283 break;
2284 default:
2285 /* do nothing */
2286 break;
2287 }
2288 }
2289 }
2290
2291 dwc->link_state = next;
Felipe Balbi019ac832011-09-08 21:18:47 +03002292
2293 dev_vdbg(dwc->dev, "%s link %d\n", __func__, dwc->link_state);
Felipe Balbi72246da2011-08-19 18:10:58 +03002294}
2295
2296static void dwc3_gadget_interrupt(struct dwc3 *dwc,
2297 const struct dwc3_event_devt *event)
2298{
2299 switch (event->type) {
2300 case DWC3_DEVICE_EVENT_DISCONNECT:
2301 dwc3_gadget_disconnect_interrupt(dwc);
2302 break;
2303 case DWC3_DEVICE_EVENT_RESET:
2304 dwc3_gadget_reset_interrupt(dwc);
2305 break;
2306 case DWC3_DEVICE_EVENT_CONNECT_DONE:
2307 dwc3_gadget_conndone_interrupt(dwc);
2308 break;
2309 case DWC3_DEVICE_EVENT_WAKEUP:
2310 dwc3_gadget_wakeup_interrupt(dwc);
2311 break;
2312 case DWC3_DEVICE_EVENT_LINK_STATUS_CHANGE:
2313 dwc3_gadget_linksts_change_interrupt(dwc, event->event_info);
2314 break;
2315 case DWC3_DEVICE_EVENT_EOPF:
2316 dev_vdbg(dwc->dev, "End of Periodic Frame\n");
2317 break;
2318 case DWC3_DEVICE_EVENT_SOF:
2319 dev_vdbg(dwc->dev, "Start of Periodic Frame\n");
2320 break;
2321 case DWC3_DEVICE_EVENT_ERRATIC_ERROR:
2322 dev_vdbg(dwc->dev, "Erratic Error\n");
2323 break;
2324 case DWC3_DEVICE_EVENT_CMD_CMPL:
2325 dev_vdbg(dwc->dev, "Command Complete\n");
2326 break;
2327 case DWC3_DEVICE_EVENT_OVERFLOW:
2328 dev_vdbg(dwc->dev, "Overflow\n");
Pavankumar Kondetid393e172012-06-12 16:07:29 +05302329 /*
2330 * Controllers prior to 2.30a revision has a bug where
2331 * Overflow Event may overwrite an unacknowledged event
2332 * in the event buffer. The severity of the issue depends
2333 * on the overwritten event type. Add a warning message
2334 * saying that an event is overwritten.
2335 *
2336 * TODO: In future we may need to see if we can re-enumerate
2337 * with host.
2338 */
2339 if (dwc->revision < DWC3_REVISION_230A)
2340 dev_warn(dwc->dev, "Unacknowledged event overwritten\n");
Felipe Balbi72246da2011-08-19 18:10:58 +03002341 break;
Pavankumar Kondeti33fe6f12012-06-12 16:21:46 +05302342 case DWC3_DEVICE_EVENT_VENDOR_DEV_TEST_LMP:
2343 /*
2344 * Controllers prior to 2.30a revision has a bug, due to which
2345 * a vendor device test LMP event can not be filtered. But
2346 * this event is not handled in the current code. This is a
2347 * special event and 8 bytes of data will follow the event.
2348 * Handling this event is tricky when event buffer is almost
2349 * full. Moreover this event will not occur in normal scenario
2350 * and can only happen with special hosts in testing scenarios.
2351 * Add a warning message to indicate that this event is received
2352 * which means that event buffer might have corrupted.
2353 */
2354 if (dwc->revision < DWC3_REVISION_230A)
2355 dev_warn(dwc->dev, "Vendor Device Test LMP Received\n");
2356 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03002357 default:
2358 dev_dbg(dwc->dev, "UNKNOWN IRQ %d\n", event->type);
2359 }
2360}
2361
2362static void dwc3_process_event_entry(struct dwc3 *dwc,
2363 const union dwc3_event *event)
2364{
2365 /* Endpoint IRQ, handle it and return early */
2366 if (event->type.is_devspec == 0) {
2367 /* depevt */
2368 return dwc3_endpoint_interrupt(dwc, &event->depevt);
2369 }
2370
2371 switch (event->type.type) {
2372 case DWC3_EVENT_TYPE_DEV:
2373 dwc3_gadget_interrupt(dwc, &event->devt);
2374 break;
2375 /* REVISIT what to do with Carkit and I2C events ? */
2376 default:
2377 dev_err(dwc->dev, "UNKNOWN IRQ type %d\n", event->raw);
2378 }
2379}
2380
2381static irqreturn_t dwc3_process_event_buf(struct dwc3 *dwc, u32 buf)
2382{
2383 struct dwc3_event_buffer *evt;
2384 int left;
2385 u32 count;
2386
2387 count = dwc3_readl(dwc->regs, DWC3_GEVNTCOUNT(buf));
2388 count &= DWC3_GEVNTCOUNT_MASK;
2389 if (!count)
2390 return IRQ_NONE;
2391
2392 evt = dwc->ev_buffs[buf];
2393 left = count;
2394
2395 while (left > 0) {
2396 union dwc3_event event;
2397
Felipe Balbid70d8442012-02-06 13:40:17 +02002398 event.raw = *(u32 *) (evt->buf + evt->lpos);
2399
Felipe Balbi72246da2011-08-19 18:10:58 +03002400 dwc3_process_event_entry(dwc, &event);
2401 /*
2402 * XXX we wrap around correctly to the next entry as almost all
2403 * entries are 4 bytes in size. There is one entry which has 12
2404 * bytes which is a regular entry followed by 8 bytes data. ATM
2405 * I don't know how things are organized if were get next to the
2406 * a boundary so I worry about that once we try to handle that.
2407 */
2408 evt->lpos = (evt->lpos + 4) % DWC3_EVENT_BUFFERS_SIZE;
2409 left -= 4;
2410
2411 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(buf), 4);
2412 }
2413
2414 return IRQ_HANDLED;
2415}
2416
2417static irqreturn_t dwc3_interrupt(int irq, void *_dwc)
2418{
2419 struct dwc3 *dwc = _dwc;
2420 int i;
2421 irqreturn_t ret = IRQ_NONE;
2422
2423 spin_lock(&dwc->lock);
2424
Felipe Balbi9f622b22011-10-12 10:31:04 +03002425 for (i = 0; i < dwc->num_event_buffers; i++) {
Felipe Balbi72246da2011-08-19 18:10:58 +03002426 irqreturn_t status;
2427
2428 status = dwc3_process_event_buf(dwc, i);
2429 if (status == IRQ_HANDLED)
2430 ret = status;
2431 }
2432
2433 spin_unlock(&dwc->lock);
2434
2435 return ret;
2436}
2437
2438/**
2439 * dwc3_gadget_init - Initializes gadget related registers
Paul Zimmerman1d046792012-02-15 18:56:56 -08002440 * @dwc: pointer to our controller context structure
Felipe Balbi72246da2011-08-19 18:10:58 +03002441 *
2442 * Returns 0 on success otherwise negative errno.
2443 */
2444int __devinit dwc3_gadget_init(struct dwc3 *dwc)
2445{
2446 u32 reg;
2447 int ret;
2448 int irq;
2449
2450 dwc->ctrl_req = dma_alloc_coherent(dwc->dev, sizeof(*dwc->ctrl_req),
2451 &dwc->ctrl_req_addr, GFP_KERNEL);
2452 if (!dwc->ctrl_req) {
2453 dev_err(dwc->dev, "failed to allocate ctrl request\n");
2454 ret = -ENOMEM;
2455 goto err0;
2456 }
2457
2458 dwc->ep0_trb = dma_alloc_coherent(dwc->dev, sizeof(*dwc->ep0_trb),
2459 &dwc->ep0_trb_addr, GFP_KERNEL);
2460 if (!dwc->ep0_trb) {
2461 dev_err(dwc->dev, "failed to allocate ep0 trb\n");
2462 ret = -ENOMEM;
2463 goto err1;
2464 }
2465
Felipe Balbib0791fb2012-05-04 12:58:14 +03002466 dwc->setup_buf = kzalloc(DWC3_EP0_BOUNCE_SIZE, GFP_KERNEL);
Felipe Balbi72246da2011-08-19 18:10:58 +03002467 if (!dwc->setup_buf) {
2468 dev_err(dwc->dev, "failed to allocate setup buffer\n");
2469 ret = -ENOMEM;
2470 goto err2;
2471 }
2472
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002473 dwc->ep0_bounce = dma_alloc_coherent(dwc->dev,
Felipe Balbib0791fb2012-05-04 12:58:14 +03002474 DWC3_EP0_BOUNCE_SIZE, &dwc->ep0_bounce_addr,
2475 GFP_KERNEL);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002476 if (!dwc->ep0_bounce) {
2477 dev_err(dwc->dev, "failed to allocate ep0 bounce buffer\n");
2478 ret = -ENOMEM;
2479 goto err3;
2480 }
2481
Felipe Balbi72246da2011-08-19 18:10:58 +03002482 dev_set_name(&dwc->gadget.dev, "gadget");
2483
2484 dwc->gadget.ops = &dwc3_gadget_ops;
Michal Nazarewiczd327ab52011-11-19 18:27:37 +01002485 dwc->gadget.max_speed = USB_SPEED_SUPER;
Felipe Balbi72246da2011-08-19 18:10:58 +03002486 dwc->gadget.speed = USB_SPEED_UNKNOWN;
2487 dwc->gadget.dev.parent = dwc->dev;
Felipe Balbieeb720f2011-11-28 12:46:59 +02002488 dwc->gadget.sg_supported = true;
Felipe Balbi72246da2011-08-19 18:10:58 +03002489
2490 dma_set_coherent_mask(&dwc->gadget.dev, dwc->dev->coherent_dma_mask);
2491
2492 dwc->gadget.dev.dma_parms = dwc->dev->dma_parms;
2493 dwc->gadget.dev.dma_mask = dwc->dev->dma_mask;
2494 dwc->gadget.dev.release = dwc3_gadget_release;
2495 dwc->gadget.name = "dwc3-gadget";
2496
2497 /*
2498 * REVISIT: Here we should clear all pending IRQs to be
2499 * sure we're starting from a well known location.
2500 */
2501
2502 ret = dwc3_gadget_init_endpoints(dwc);
2503 if (ret)
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002504 goto err4;
Felipe Balbi72246da2011-08-19 18:10:58 +03002505
2506 irq = platform_get_irq(to_platform_device(dwc->dev), 0);
2507
2508 ret = request_irq(irq, dwc3_interrupt, IRQF_SHARED,
2509 "dwc3", dwc);
2510 if (ret) {
2511 dev_err(dwc->dev, "failed to request irq #%d --> %d\n",
2512 irq, ret);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002513 goto err5;
Felipe Balbi72246da2011-08-19 18:10:58 +03002514 }
2515
Sebastian Andrzej Siewiorbb8b8a32011-09-13 17:54:39 +02002516 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2517 reg |= DWC3_DCFG_LPM_CAP;
2518 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
2519
Felipe Balbi72246da2011-08-19 18:10:58 +03002520 /* Enable all but Start and End of Frame IRQs */
Pavankumar Kondeti33fe6f12012-06-12 16:21:46 +05302521 reg = (DWC3_DEVTEN_EVNTOVERFLOWEN |
Felipe Balbi72246da2011-08-19 18:10:58 +03002522 DWC3_DEVTEN_CMDCMPLTEN |
2523 DWC3_DEVTEN_ERRTICERREN |
2524 DWC3_DEVTEN_WKUPEVTEN |
2525 DWC3_DEVTEN_ULSTCNGEN |
2526 DWC3_DEVTEN_CONNECTDONEEN |
2527 DWC3_DEVTEN_USBRSTEN |
2528 DWC3_DEVTEN_DISCONNEVTEN);
2529 dwc3_writel(dwc->regs, DWC3_DEVTEN, reg);
2530
Paul Zimmerman88df4272012-04-27 13:10:52 +03002531 /* Enable USB2 LPM and automatic phy suspend only on recent versions */
2532 if (dwc->revision >= DWC3_REVISION_194A) {
2533 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2534 reg |= DWC3_DCFG_LPM_CAP;
2535 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
2536
2537 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2538 reg &= ~(DWC3_DCTL_HIRD_THRES_MASK | DWC3_DCTL_L1_HIBER_EN);
2539
2540 /* TODO: This should be configurable */
2541 reg |= DWC3_DCTL_HIRD_THRES(31);
2542
2543 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2544
Pratyush Anand50ed8342012-06-06 19:36:17 +05302545 dwc3_gadget_usb2_phy_suspend(dwc, false);
2546 dwc3_gadget_usb3_phy_suspend(dwc, false);
Paul Zimmerman88df4272012-04-27 13:10:52 +03002547 }
2548
Felipe Balbi72246da2011-08-19 18:10:58 +03002549 ret = device_register(&dwc->gadget.dev);
2550 if (ret) {
2551 dev_err(dwc->dev, "failed to register gadget device\n");
2552 put_device(&dwc->gadget.dev);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002553 goto err6;
Felipe Balbi72246da2011-08-19 18:10:58 +03002554 }
2555
2556 ret = usb_add_gadget_udc(dwc->dev, &dwc->gadget);
2557 if (ret) {
2558 dev_err(dwc->dev, "failed to register udc\n");
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002559 goto err7;
Felipe Balbi72246da2011-08-19 18:10:58 +03002560 }
2561
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +02002562 if (dwc->dotg) {
2563 /* dwc3 otg driver is active (DRD mode + SRPSupport=1) */
2564 ret = otg_set_peripheral(&dwc->dotg->otg, &dwc->gadget);
2565 if (ret) {
2566 dev_err(dwc->dev, "failed to set peripheral to otg\n");
2567 goto err7;
2568 }
Manu Gautamb5067272012-07-02 09:53:41 +05302569 } else {
2570 pm_runtime_no_callbacks(&dwc->gadget.dev);
2571 pm_runtime_set_active(&dwc->gadget.dev);
2572 pm_runtime_enable(&dwc->gadget.dev);
2573 pm_runtime_get(&dwc->gadget.dev);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +02002574 }
2575
Felipe Balbi72246da2011-08-19 18:10:58 +03002576 return 0;
2577
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002578err7:
Felipe Balbi72246da2011-08-19 18:10:58 +03002579 device_unregister(&dwc->gadget.dev);
2580
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002581err6:
Felipe Balbi72246da2011-08-19 18:10:58 +03002582 dwc3_writel(dwc->regs, DWC3_DEVTEN, 0x00);
2583 free_irq(irq, dwc);
2584
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002585err5:
Felipe Balbi72246da2011-08-19 18:10:58 +03002586 dwc3_gadget_free_endpoints(dwc);
2587
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002588err4:
Felipe Balbib0791fb2012-05-04 12:58:14 +03002589 dma_free_coherent(dwc->dev, DWC3_EP0_BOUNCE_SIZE,
2590 dwc->ep0_bounce, dwc->ep0_bounce_addr);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002591
Felipe Balbi72246da2011-08-19 18:10:58 +03002592err3:
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02002593 kfree(dwc->setup_buf);
Felipe Balbi72246da2011-08-19 18:10:58 +03002594
2595err2:
2596 dma_free_coherent(dwc->dev, sizeof(*dwc->ep0_trb),
2597 dwc->ep0_trb, dwc->ep0_trb_addr);
2598
2599err1:
2600 dma_free_coherent(dwc->dev, sizeof(*dwc->ctrl_req),
2601 dwc->ctrl_req, dwc->ctrl_req_addr);
2602
2603err0:
2604 return ret;
2605}
2606
2607void dwc3_gadget_exit(struct dwc3 *dwc)
2608{
2609 int irq;
Felipe Balbi72246da2011-08-19 18:10:58 +03002610
Manu Gautamb5067272012-07-02 09:53:41 +05302611 if (dwc->dotg) {
2612 pm_runtime_put(&dwc->gadget.dev);
2613 pm_runtime_disable(&dwc->gadget.dev);
2614 }
2615
Felipe Balbi72246da2011-08-19 18:10:58 +03002616 usb_del_gadget_udc(&dwc->gadget);
2617 irq = platform_get_irq(to_platform_device(dwc->dev), 0);
2618
2619 dwc3_writel(dwc->regs, DWC3_DEVTEN, 0x00);
2620 free_irq(irq, dwc);
2621
Felipe Balbi72246da2011-08-19 18:10:58 +03002622 dwc3_gadget_free_endpoints(dwc);
2623
Felipe Balbib0791fb2012-05-04 12:58:14 +03002624 dma_free_coherent(dwc->dev, DWC3_EP0_BOUNCE_SIZE,
2625 dwc->ep0_bounce, dwc->ep0_bounce_addr);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002626
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02002627 kfree(dwc->setup_buf);
Felipe Balbi72246da2011-08-19 18:10:58 +03002628
2629 dma_free_coherent(dwc->dev, sizeof(*dwc->ep0_trb),
2630 dwc->ep0_trb, dwc->ep0_trb_addr);
2631
2632 dma_free_coherent(dwc->dev, sizeof(*dwc->ctrl_req),
2633 dwc->ctrl_req, dwc->ctrl_req_addr);
2634
2635 device_unregister(&dwc->gadget.dev);
2636}