blob: 13a53cb74dd5fac94e7b3a8d75c126a150742a68 [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
Pratyush Anand73939b02012-05-25 18:54:56 +05301079 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
1080 if (dep->flags & DWC3_EP_BUSY) {
1081 dep->flags |= DWC3_EP_PENDING_REQUEST;
1082 } else if (dep->flags & DWC3_EP_MISSED_ISOC) {
1083 __dwc3_gadget_start_isoc(dwc, dep, dep->current_uf);
1084 dep->flags &= ~DWC3_EP_MISSED_ISOC;
1085 }
1086 }
Paul Zimmermanf39a37f2012-03-29 18:16:54 +00001087
Felipe Balbi72246da2011-08-19 18:10:58 +03001088 /*
Paul Zimmermanf39a37f2012-03-29 18:16:54 +00001089 * There are two special cases:
Felipe Balbi72246da2011-08-19 18:10:58 +03001090 *
Paul Zimmermanf39a37f2012-03-29 18:16:54 +00001091 * 1. XferNotReady with empty list of requests. We need to kick the
1092 * transfer here in that situation, otherwise we will be NAKing
1093 * forever. If we get XferNotReady before gadget driver has a
1094 * chance to queue a request, we will ACK the IRQ but won't be
1095 * able to receive the data until the next request is queued.
1096 * The following code is handling exactly that.
1097 *
1098 * 2. XferInProgress on Isoc EP with an active transfer. We need to
1099 * kick the transfer here after queuing a request, otherwise the
1100 * core may not see the modified TRB(s).
Felipe Balbi72246da2011-08-19 18:10:58 +03001101 */
1102 if (dep->flags & DWC3_EP_PENDING_REQUEST) {
Paul Zimmermanf39a37f2012-03-29 18:16:54 +00001103 int ret;
1104 int start_trans = 1;
1105 u8 trans_idx = dep->res_trans_idx;
Felipe Balbi72246da2011-08-19 18:10:58 +03001106
Ido Shayevitz57cdac12012-03-12 20:25:24 +02001107 if (usb_endpoint_xfer_isoc(dep->endpoint.desc) &&
Paul Zimmermanf39a37f2012-03-29 18:16:54 +00001108 (dep->flags & DWC3_EP_BUSY)) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001109 start_trans = 0;
Paul Zimmermanf39a37f2012-03-29 18:16:54 +00001110 WARN_ON_ONCE(!trans_idx);
1111 } else {
1112 trans_idx = 0;
1113 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001114
Paul Zimmermanf39a37f2012-03-29 18:16:54 +00001115 ret = __dwc3_gadget_kick_transfer(dep, trans_idx, start_trans);
Felipe Balbi72246da2011-08-19 18:10:58 +03001116 if (ret && ret != -EBUSY) {
1117 struct dwc3 *dwc = dep->dwc;
1118
1119 dev_dbg(dwc->dev, "%s: failed to kick transfers\n",
1120 dep->name);
1121 }
Felipe Balbi5d409eb2012-05-22 10:24:11 +03001122 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001123
1124 return 0;
1125}
1126
1127static int dwc3_gadget_ep_queue(struct usb_ep *ep, struct usb_request *request,
1128 gfp_t gfp_flags)
1129{
1130 struct dwc3_request *req = to_dwc3_request(request);
1131 struct dwc3_ep *dep = to_dwc3_ep(ep);
1132 struct dwc3 *dwc = dep->dwc;
1133
1134 unsigned long flags;
1135
1136 int ret;
1137
Ido Shayevitz57cdac12012-03-12 20:25:24 +02001138 if (!dep->endpoint.desc) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001139 dev_dbg(dwc->dev, "trying to queue request %p to disabled %s\n",
1140 request, ep->name);
1141 return -ESHUTDOWN;
1142 }
1143
1144 dev_vdbg(dwc->dev, "queing request %p to %s length %d\n",
1145 request, ep->name, request->length);
1146
Manu Gautam1c4dbcb2012-10-05 13:16:00 +05301147 WARN(!dep->direction && (request->length % ep->desc->wMaxPacketSize),
1148 "trying to queue unaligned request (%d)\n", request->length);
1149
Felipe Balbi72246da2011-08-19 18:10:58 +03001150 spin_lock_irqsave(&dwc->lock, flags);
1151 ret = __dwc3_gadget_ep_queue(dep, req);
1152 spin_unlock_irqrestore(&dwc->lock, flags);
1153
1154 return ret;
1155}
1156
1157static int dwc3_gadget_ep_dequeue(struct usb_ep *ep,
1158 struct usb_request *request)
1159{
1160 struct dwc3_request *req = to_dwc3_request(request);
1161 struct dwc3_request *r = NULL;
1162
1163 struct dwc3_ep *dep = to_dwc3_ep(ep);
1164 struct dwc3 *dwc = dep->dwc;
1165
1166 unsigned long flags;
1167 int ret = 0;
1168
1169 spin_lock_irqsave(&dwc->lock, flags);
1170
1171 list_for_each_entry(r, &dep->request_list, list) {
1172 if (r == req)
1173 break;
1174 }
1175
1176 if (r != req) {
1177 list_for_each_entry(r, &dep->req_queued, list) {
1178 if (r == req)
1179 break;
1180 }
1181 if (r == req) {
1182 /* wait until it is processed */
1183 dwc3_stop_active_transfer(dwc, dep->number);
Pratyush Anandeaec3e92012-06-15 11:54:00 +05301184 goto out1;
Felipe Balbi72246da2011-08-19 18:10:58 +03001185 }
1186 dev_err(dwc->dev, "request %p was not queued to %s\n",
1187 request, ep->name);
1188 ret = -EINVAL;
1189 goto out0;
1190 }
1191
Pratyush Anandeaec3e92012-06-15 11:54:00 +05301192out1:
Felipe Balbi72246da2011-08-19 18:10:58 +03001193 /* giveback the request */
1194 dwc3_gadget_giveback(dep, req, -ECONNRESET);
1195
1196out0:
1197 spin_unlock_irqrestore(&dwc->lock, flags);
1198
1199 return ret;
1200}
1201
1202int __dwc3_gadget_ep_set_halt(struct dwc3_ep *dep, int value)
1203{
1204 struct dwc3_gadget_ep_cmd_params params;
1205 struct dwc3 *dwc = dep->dwc;
1206 int ret;
1207
1208 memset(&params, 0x00, sizeof(params));
1209
1210 if (value) {
Felipe Balbi0b7836a2011-08-30 15:48:08 +03001211 if (dep->number == 0 || dep->number == 1) {
1212 /*
1213 * Whenever EP0 is stalled, we will restart
1214 * the state machine, thus moving back to
1215 * Setup Phase
1216 */
1217 dwc->ep0state = EP0_SETUP_PHASE;
1218 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001219
1220 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number,
1221 DWC3_DEPCMD_SETSTALL, &params);
1222 if (ret)
1223 dev_err(dwc->dev, "failed to %s STALL on %s\n",
1224 value ? "set" : "clear",
1225 dep->name);
1226 else
1227 dep->flags |= DWC3_EP_STALL;
1228 } else {
Paul Zimmerman52754552011-09-30 10:58:44 +03001229 if (dep->flags & DWC3_EP_WEDGE)
1230 return 0;
1231
Felipe Balbi72246da2011-08-19 18:10:58 +03001232 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number,
1233 DWC3_DEPCMD_CLEARSTALL, &params);
1234 if (ret)
1235 dev_err(dwc->dev, "failed to %s STALL on %s\n",
1236 value ? "set" : "clear",
1237 dep->name);
1238 else
1239 dep->flags &= ~DWC3_EP_STALL;
1240 }
Paul Zimmerman52754552011-09-30 10:58:44 +03001241
Felipe Balbi72246da2011-08-19 18:10:58 +03001242 return ret;
1243}
1244
1245static int dwc3_gadget_ep_set_halt(struct usb_ep *ep, int value)
1246{
1247 struct dwc3_ep *dep = to_dwc3_ep(ep);
1248 struct dwc3 *dwc = dep->dwc;
1249
1250 unsigned long flags;
1251
1252 int ret;
1253
1254 spin_lock_irqsave(&dwc->lock, flags);
1255
Ido Shayevitz57cdac12012-03-12 20:25:24 +02001256 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001257 dev_err(dwc->dev, "%s is of Isochronous type\n", dep->name);
1258 ret = -EINVAL;
1259 goto out;
1260 }
1261
1262 ret = __dwc3_gadget_ep_set_halt(dep, value);
1263out:
1264 spin_unlock_irqrestore(&dwc->lock, flags);
1265
1266 return ret;
1267}
1268
1269static int dwc3_gadget_ep_set_wedge(struct usb_ep *ep)
1270{
1271 struct dwc3_ep *dep = to_dwc3_ep(ep);
Paul Zimmerman249a4562012-02-24 17:32:16 -08001272 struct dwc3 *dwc = dep->dwc;
1273 unsigned long flags;
Felipe Balbi72246da2011-08-19 18:10:58 +03001274
Paul Zimmerman249a4562012-02-24 17:32:16 -08001275 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001276 dep->flags |= DWC3_EP_WEDGE;
Paul Zimmerman249a4562012-02-24 17:32:16 -08001277 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001278
Paul Zimmerman52754552011-09-30 10:58:44 +03001279 return dwc3_gadget_ep_set_halt(ep, 1);
Felipe Balbi72246da2011-08-19 18:10:58 +03001280}
1281
1282/* -------------------------------------------------------------------------- */
1283
1284static struct usb_endpoint_descriptor dwc3_gadget_ep0_desc = {
1285 .bLength = USB_DT_ENDPOINT_SIZE,
1286 .bDescriptorType = USB_DT_ENDPOINT,
1287 .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
1288};
1289
1290static const struct usb_ep_ops dwc3_gadget_ep0_ops = {
1291 .enable = dwc3_gadget_ep0_enable,
1292 .disable = dwc3_gadget_ep0_disable,
1293 .alloc_request = dwc3_gadget_ep_alloc_request,
1294 .free_request = dwc3_gadget_ep_free_request,
1295 .queue = dwc3_gadget_ep0_queue,
1296 .dequeue = dwc3_gadget_ep_dequeue,
1297 .set_halt = dwc3_gadget_ep_set_halt,
1298 .set_wedge = dwc3_gadget_ep_set_wedge,
1299};
1300
1301static const struct usb_ep_ops dwc3_gadget_ep_ops = {
1302 .enable = dwc3_gadget_ep_enable,
1303 .disable = dwc3_gadget_ep_disable,
1304 .alloc_request = dwc3_gadget_ep_alloc_request,
1305 .free_request = dwc3_gadget_ep_free_request,
1306 .queue = dwc3_gadget_ep_queue,
1307 .dequeue = dwc3_gadget_ep_dequeue,
1308 .set_halt = dwc3_gadget_ep_set_halt,
1309 .set_wedge = dwc3_gadget_ep_set_wedge,
1310};
1311
1312/* -------------------------------------------------------------------------- */
1313
1314static int dwc3_gadget_get_frame(struct usb_gadget *g)
1315{
1316 struct dwc3 *dwc = gadget_to_dwc(g);
1317 u32 reg;
1318
1319 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1320 return DWC3_DSTS_SOFFN(reg);
1321}
1322
1323static int dwc3_gadget_wakeup(struct usb_gadget *g)
1324{
1325 struct dwc3 *dwc = gadget_to_dwc(g);
1326
1327 unsigned long timeout;
1328 unsigned long flags;
1329
1330 u32 reg;
1331
1332 int ret = 0;
1333
1334 u8 link_state;
1335 u8 speed;
1336
1337 spin_lock_irqsave(&dwc->lock, flags);
1338
1339 /*
1340 * According to the Databook Remote wakeup request should
1341 * be issued only when the device is in early suspend state.
1342 *
1343 * We can check that via USB Link State bits in DSTS register.
1344 */
1345 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1346
1347 speed = reg & DWC3_DSTS_CONNECTSPD;
1348 if (speed == DWC3_DSTS_SUPERSPEED) {
1349 dev_dbg(dwc->dev, "no wakeup on SuperSpeed\n");
1350 ret = -EINVAL;
1351 goto out;
1352 }
1353
1354 link_state = DWC3_DSTS_USBLNKST(reg);
1355
1356 switch (link_state) {
1357 case DWC3_LINK_STATE_RX_DET: /* in HS, means Early Suspend */
1358 case DWC3_LINK_STATE_U3: /* in HS, means SUSPEND */
1359 break;
1360 default:
1361 dev_dbg(dwc->dev, "can't wakeup from link state %d\n",
1362 link_state);
1363 ret = -EINVAL;
1364 goto out;
1365 }
1366
Felipe Balbi8598bde2012-01-02 18:55:57 +02001367 ret = dwc3_gadget_set_link_state(dwc, DWC3_LINK_STATE_RECOV);
1368 if (ret < 0) {
1369 dev_err(dwc->dev, "failed to put link in Recovery\n");
1370 goto out;
1371 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001372
Paul Zimmerman88df4272012-04-27 13:10:52 +03001373 /* Recent versions do this automatically */
1374 if (dwc->revision < DWC3_REVISION_194A) {
1375 /* write zeroes to Link Change Request */
Felipe Balbib4d04352012-05-24 10:27:56 +03001376 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
Paul Zimmerman88df4272012-04-27 13:10:52 +03001377 reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK;
1378 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1379 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001380
Paul Zimmerman1d046792012-02-15 18:56:56 -08001381 /* poll until Link State changes to ON */
Felipe Balbi72246da2011-08-19 18:10:58 +03001382 timeout = jiffies + msecs_to_jiffies(100);
1383
Paul Zimmerman1d046792012-02-15 18:56:56 -08001384 while (!time_after(jiffies, timeout)) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001385 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1386
1387 /* in HS, means ON */
1388 if (DWC3_DSTS_USBLNKST(reg) == DWC3_LINK_STATE_U0)
1389 break;
1390 }
1391
1392 if (DWC3_DSTS_USBLNKST(reg) != DWC3_LINK_STATE_U0) {
1393 dev_err(dwc->dev, "failed to send remote wakeup\n");
1394 ret = -EINVAL;
1395 }
1396
1397out:
1398 spin_unlock_irqrestore(&dwc->lock, flags);
1399
1400 return ret;
1401}
1402
1403static int dwc3_gadget_set_selfpowered(struct usb_gadget *g,
1404 int is_selfpowered)
1405{
1406 struct dwc3 *dwc = gadget_to_dwc(g);
Paul Zimmerman249a4562012-02-24 17:32:16 -08001407 unsigned long flags;
Felipe Balbi72246da2011-08-19 18:10:58 +03001408
Paul Zimmerman249a4562012-02-24 17:32:16 -08001409 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001410 dwc->is_selfpowered = !!is_selfpowered;
Paul Zimmerman249a4562012-02-24 17:32:16 -08001411 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001412
1413 return 0;
1414}
1415
1416static void dwc3_gadget_run_stop(struct dwc3 *dwc, int is_on)
1417{
1418 u32 reg;
Sebastian Andrzej Siewior61d58242011-08-29 16:46:38 +02001419 u32 timeout = 500;
Felipe Balbi72246da2011-08-19 18:10:58 +03001420
1421 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
Felipe Balbi8db7ed12012-01-18 18:32:29 +02001422 if (is_on) {
Paul Zimmerman88df4272012-04-27 13:10:52 +03001423 if (dwc->revision <= DWC3_REVISION_187A) {
1424 reg &= ~DWC3_DCTL_TRGTULST_MASK;
1425 reg |= DWC3_DCTL_TRGTULST_RX_DET;
1426 }
1427
1428 if (dwc->revision >= DWC3_REVISION_194A)
1429 reg &= ~DWC3_DCTL_KEEP_CONNECT;
1430 reg |= DWC3_DCTL_RUN_STOP;
Felipe Balbi8db7ed12012-01-18 18:32:29 +02001431 } else {
Felipe Balbi72246da2011-08-19 18:10:58 +03001432 reg &= ~DWC3_DCTL_RUN_STOP;
Felipe Balbi8db7ed12012-01-18 18:32:29 +02001433 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001434
1435 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1436
1437 do {
1438 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1439 if (is_on) {
1440 if (!(reg & DWC3_DSTS_DEVCTRLHLT))
1441 break;
1442 } else {
1443 if (reg & DWC3_DSTS_DEVCTRLHLT)
1444 break;
1445 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001446 timeout--;
1447 if (!timeout)
1448 break;
Sebastian Andrzej Siewior61d58242011-08-29 16:46:38 +02001449 udelay(1);
Felipe Balbi72246da2011-08-19 18:10:58 +03001450 } while (1);
1451
1452 dev_vdbg(dwc->dev, "gadget %s data soft-%s\n",
1453 dwc->gadget_driver
1454 ? dwc->gadget_driver->function : "no-function",
1455 is_on ? "connect" : "disconnect");
1456}
1457
1458static int dwc3_gadget_pullup(struct usb_gadget *g, int is_on)
1459{
1460 struct dwc3 *dwc = gadget_to_dwc(g);
1461 unsigned long flags;
1462
1463 is_on = !!is_on;
1464
1465 spin_lock_irqsave(&dwc->lock, flags);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +02001466
1467 dwc->softconnect = is_on;
1468
1469 if ((dwc->dotg && !dwc->vbus_active) ||
1470 !dwc->gadget_driver) {
1471
1472 spin_unlock_irqrestore(&dwc->lock, flags);
1473
1474 /*
1475 * Need to wait for vbus_session(on) from otg driver or to
1476 * the udc_start.
1477 */
1478 return 0;
1479 }
1480
Felipe Balbi72246da2011-08-19 18:10:58 +03001481 dwc3_gadget_run_stop(dwc, is_on);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +02001482
1483 spin_unlock_irqrestore(&dwc->lock, flags);
1484
1485 return 0;
1486}
1487
1488static int dwc3_gadget_vbus_session(struct usb_gadget *_gadget, int is_active)
1489{
1490 struct dwc3 *dwc = gadget_to_dwc(_gadget);
1491 unsigned long flags;
1492
1493 if (!dwc->dotg)
1494 return -EPERM;
1495
1496 is_active = !!is_active;
1497
1498 spin_lock_irqsave(&dwc->lock, flags);
1499
1500 /* Mark that the vbus was powered */
1501 dwc->vbus_active = is_active;
1502
1503 /*
1504 * Check if upper level usb_gadget_driver was already registerd with
1505 * this udc controller driver (if dwc3_gadget_start was called)
1506 */
1507 if (dwc->gadget_driver && dwc->softconnect) {
1508 if (dwc->vbus_active) {
1509 /*
1510 * Both vbus was activated by otg and pullup was
1511 * signaled by the gadget driver.
1512 */
1513 dwc3_gadget_run_stop(dwc, 1);
1514 } else {
1515 dwc3_gadget_run_stop(dwc, 0);
1516 }
1517 }
1518
Felipe Balbi72246da2011-08-19 18:10:58 +03001519 spin_unlock_irqrestore(&dwc->lock, flags);
1520
1521 return 0;
1522}
1523
1524static int dwc3_gadget_start(struct usb_gadget *g,
1525 struct usb_gadget_driver *driver)
1526{
1527 struct dwc3 *dwc = gadget_to_dwc(g);
1528 struct dwc3_ep *dep;
1529 unsigned long flags;
1530 int ret = 0;
1531 u32 reg;
1532
1533 spin_lock_irqsave(&dwc->lock, flags);
1534
1535 if (dwc->gadget_driver) {
1536 dev_err(dwc->dev, "%s is already bound to %s\n",
1537 dwc->gadget.name,
1538 dwc->gadget_driver->driver.name);
1539 ret = -EBUSY;
1540 goto err0;
1541 }
1542
1543 dwc->gadget_driver = driver;
1544 dwc->gadget.dev.driver = &driver->driver;
1545
Felipe Balbi72246da2011-08-19 18:10:58 +03001546 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
1547 reg &= ~(DWC3_DCFG_SPEED_MASK);
Felipe Balbi38d2c6c2012-03-23 12:20:31 +02001548
1549 /**
1550 * WORKAROUND: DWC3 revision < 2.20a have an issue
1551 * which would cause metastability state on Run/Stop
1552 * bit if we try to force the IP to USB2-only mode.
1553 *
1554 * Because of that, we cannot configure the IP to any
1555 * speed other than the SuperSpeed
1556 *
1557 * Refers to:
1558 *
1559 * STAR#9000525659: Clock Domain Crossing on DCTL in
1560 * USB 2.0 Mode
1561 */
1562 if (dwc->revision < DWC3_REVISION_220A)
1563 reg |= DWC3_DCFG_SUPERSPEED;
1564 else
1565 reg |= dwc->maximum_speed;
Felipe Balbi72246da2011-08-19 18:10:58 +03001566 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
1567
Paul Zimmermanb23c8432011-09-30 10:58:42 +03001568 dwc->start_config_issued = false;
1569
Felipe Balbi72246da2011-08-19 18:10:58 +03001570 /* Start with SuperSpeed Default */
1571 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
1572
1573 dep = dwc->eps[0];
Felipe Balbic90bfae2011-11-29 13:11:21 +02001574 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL);
Felipe Balbi72246da2011-08-19 18:10:58 +03001575 if (ret) {
1576 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
1577 goto err0;
1578 }
1579
1580 dep = dwc->eps[1];
Felipe Balbic90bfae2011-11-29 13:11:21 +02001581 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL);
Felipe Balbi72246da2011-08-19 18:10:58 +03001582 if (ret) {
1583 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
1584 goto err1;
1585 }
1586
1587 /* begin to receive SETUP packets */
Felipe Balbic7fcdeb2011-08-27 22:28:36 +03001588 dwc->ep0state = EP0_SETUP_PHASE;
Felipe Balbi72246da2011-08-19 18:10:58 +03001589 dwc3_ep0_out_start(dwc);
1590
1591 spin_unlock_irqrestore(&dwc->lock, flags);
1592
1593 return 0;
1594
1595err1:
1596 __dwc3_gadget_ep_disable(dwc->eps[0]);
1597
1598err0:
1599 spin_unlock_irqrestore(&dwc->lock, flags);
1600
1601 return ret;
1602}
1603
1604static int dwc3_gadget_stop(struct usb_gadget *g,
1605 struct usb_gadget_driver *driver)
1606{
1607 struct dwc3 *dwc = gadget_to_dwc(g);
1608 unsigned long flags;
1609
1610 spin_lock_irqsave(&dwc->lock, flags);
1611
1612 __dwc3_gadget_ep_disable(dwc->eps[0]);
1613 __dwc3_gadget_ep_disable(dwc->eps[1]);
1614
1615 dwc->gadget_driver = NULL;
1616 dwc->gadget.dev.driver = NULL;
1617
1618 spin_unlock_irqrestore(&dwc->lock, flags);
1619
1620 return 0;
1621}
Paul Zimmerman88df4272012-04-27 13:10:52 +03001622
Felipe Balbi72246da2011-08-19 18:10:58 +03001623static const struct usb_gadget_ops dwc3_gadget_ops = {
1624 .get_frame = dwc3_gadget_get_frame,
1625 .wakeup = dwc3_gadget_wakeup,
1626 .set_selfpowered = dwc3_gadget_set_selfpowered,
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +02001627 .vbus_session = dwc3_gadget_vbus_session,
Felipe Balbi72246da2011-08-19 18:10:58 +03001628 .pullup = dwc3_gadget_pullup,
1629 .udc_start = dwc3_gadget_start,
1630 .udc_stop = dwc3_gadget_stop,
1631};
1632
1633/* -------------------------------------------------------------------------- */
1634
1635static int __devinit dwc3_gadget_init_endpoints(struct dwc3 *dwc)
1636{
1637 struct dwc3_ep *dep;
1638 u8 epnum;
1639
1640 INIT_LIST_HEAD(&dwc->gadget.ep_list);
1641
1642 for (epnum = 0; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
1643 dep = kzalloc(sizeof(*dep), GFP_KERNEL);
1644 if (!dep) {
1645 dev_err(dwc->dev, "can't allocate endpoint %d\n",
1646 epnum);
1647 return -ENOMEM;
1648 }
1649
1650 dep->dwc = dwc;
1651 dep->number = epnum;
1652 dwc->eps[epnum] = dep;
1653
1654 snprintf(dep->name, sizeof(dep->name), "ep%d%s", epnum >> 1,
1655 (epnum & 1) ? "in" : "out");
1656 dep->endpoint.name = dep->name;
1657 dep->direction = (epnum & 1);
1658
1659 if (epnum == 0 || epnum == 1) {
1660 dep->endpoint.maxpacket = 512;
1661 dep->endpoint.ops = &dwc3_gadget_ep0_ops;
1662 if (!epnum)
1663 dwc->gadget.ep0 = &dep->endpoint;
1664 } else {
1665 int ret;
1666
1667 dep->endpoint.maxpacket = 1024;
Sebastian Andrzej Siewior12d36c12011-11-03 20:27:50 +01001668 dep->endpoint.max_streams = 15;
Felipe Balbi72246da2011-08-19 18:10:58 +03001669 dep->endpoint.ops = &dwc3_gadget_ep_ops;
1670 list_add_tail(&dep->endpoint.ep_list,
1671 &dwc->gadget.ep_list);
1672
1673 ret = dwc3_alloc_trb_pool(dep);
Felipe Balbi25b8ff62011-11-04 12:32:47 +02001674 if (ret)
Felipe Balbi72246da2011-08-19 18:10:58 +03001675 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001676 }
Felipe Balbi25b8ff62011-11-04 12:32:47 +02001677
Felipe Balbi72246da2011-08-19 18:10:58 +03001678 INIT_LIST_HEAD(&dep->request_list);
1679 INIT_LIST_HEAD(&dep->req_queued);
1680 }
1681
1682 return 0;
1683}
1684
1685static void dwc3_gadget_free_endpoints(struct dwc3 *dwc)
1686{
1687 struct dwc3_ep *dep;
1688 u8 epnum;
1689
1690 for (epnum = 0; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
1691 dep = dwc->eps[epnum];
1692 dwc3_free_trb_pool(dep);
1693
1694 if (epnum != 0 && epnum != 1)
1695 list_del(&dep->endpoint.ep_list);
1696
1697 kfree(dep);
1698 }
1699}
1700
1701static void dwc3_gadget_release(struct device *dev)
1702{
1703 dev_dbg(dev, "%s\n", __func__);
1704}
1705
1706/* -------------------------------------------------------------------------- */
1707static int dwc3_cleanup_done_reqs(struct dwc3 *dwc, struct dwc3_ep *dep,
1708 const struct dwc3_event_depevt *event, int status)
1709{
1710 struct dwc3_request *req;
Felipe Balbif6bafc62012-02-06 11:04:53 +02001711 struct dwc3_trb *trb;
Felipe Balbi72246da2011-08-19 18:10:58 +03001712 unsigned int count;
1713 unsigned int s_pkt = 0;
Pratyush Anand73939b02012-05-25 18:54:56 +05301714 unsigned int trb_status;
Felipe Balbi72246da2011-08-19 18:10:58 +03001715
1716 do {
1717 req = next_request(&dep->req_queued);
Sebastian Andrzej Siewiord39ee7b2011-11-03 10:32:20 +01001718 if (!req) {
1719 WARN_ON_ONCE(1);
1720 return 1;
1721 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001722
Felipe Balbif6bafc62012-02-06 11:04:53 +02001723 trb = req->trb;
Felipe Balbi72246da2011-08-19 18:10:58 +03001724
Felipe Balbif6bafc62012-02-06 11:04:53 +02001725 if ((trb->ctrl & DWC3_TRB_CTRL_HWO) && status != -ESHUTDOWN)
Sebastian Andrzej Siewior0d2f4752011-08-19 19:59:12 +02001726 /*
1727 * We continue despite the error. There is not much we
Paul Zimmerman1d046792012-02-15 18:56:56 -08001728 * can do. If we don't clean it up we loop forever. If
1729 * we skip the TRB then it gets overwritten after a
1730 * while since we use them in a ring buffer. A BUG()
1731 * would help. Lets hope that if this occurs, someone
Sebastian Andrzej Siewior0d2f4752011-08-19 19:59:12 +02001732 * fixes the root cause instead of looking away :)
1733 */
Felipe Balbi72246da2011-08-19 18:10:58 +03001734 dev_err(dwc->dev, "%s's TRB (%p) still owned by HW\n",
1735 dep->name, req->trb);
Felipe Balbif6bafc62012-02-06 11:04:53 +02001736 count = trb->size & DWC3_TRB_SIZE_MASK;
Felipe Balbi72246da2011-08-19 18:10:58 +03001737
1738 if (dep->direction) {
1739 if (count) {
Pratyush Anand73939b02012-05-25 18:54:56 +05301740 trb_status = DWC3_TRB_SIZE_TRBSTS(trb->size);
1741 if (trb_status == DWC3_TRBSTS_MISSED_ISOC) {
1742 dev_dbg(dwc->dev, "incomplete IN transfer %s\n",
1743 dep->name);
1744 dep->current_uf = event->parameters &
1745 ~(dep->interval - 1);
1746 dep->flags |= DWC3_EP_MISSED_ISOC;
1747 } else {
1748 dev_err(dwc->dev, "incomplete IN transfer %s\n",
1749 dep->name);
1750 status = -ECONNRESET;
1751 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001752 }
1753 } else {
1754 if (count && (event->status & DEPEVT_STATUS_SHORT))
1755 s_pkt = 1;
1756 }
1757
1758 /*
1759 * We assume here we will always receive the entire data block
1760 * which we should receive. Meaning, if we program RX to
1761 * receive 4K but we receive only 2K, we assume that's all we
1762 * should receive and we simply bounce the request back to the
1763 * gadget driver for further processing.
1764 */
1765 req->request.actual += req->request.length - count;
1766 dwc3_gadget_giveback(dep, req, status);
1767 if (s_pkt)
1768 break;
Felipe Balbif6bafc62012-02-06 11:04:53 +02001769 if ((event->status & DEPEVT_STATUS_LST) &&
Pratyush Anand413dba62012-06-03 19:43:19 +05301770 (trb->ctrl & (DWC3_TRB_CTRL_LST |
1771 DWC3_TRB_CTRL_HWO)))
Felipe Balbi72246da2011-08-19 18:10:58 +03001772 break;
Felipe Balbif6bafc62012-02-06 11:04:53 +02001773 if ((event->status & DEPEVT_STATUS_IOC) &&
1774 (trb->ctrl & DWC3_TRB_CTRL_IOC))
Felipe Balbi72246da2011-08-19 18:10:58 +03001775 break;
1776 } while (1);
1777
Felipe Balbif6bafc62012-02-06 11:04:53 +02001778 if ((event->status & DEPEVT_STATUS_IOC) &&
1779 (trb->ctrl & DWC3_TRB_CTRL_IOC))
Felipe Balbi72246da2011-08-19 18:10:58 +03001780 return 0;
1781 return 1;
1782}
1783
1784static void dwc3_endpoint_transfer_complete(struct dwc3 *dwc,
1785 struct dwc3_ep *dep, const struct dwc3_event_depevt *event,
1786 int start_new)
1787{
1788 unsigned status = 0;
1789 int clean_busy;
1790
1791 if (event->status & DEPEVT_STATUS_BUSERR)
1792 status = -ECONNRESET;
1793
Paul Zimmerman1d046792012-02-15 18:56:56 -08001794 clean_busy = dwc3_cleanup_done_reqs(dwc, dep, event, status);
Paul Zimmermanc2df85c2012-02-24 17:32:18 -08001795 if (clean_busy)
Felipe Balbi72246da2011-08-19 18:10:58 +03001796 dep->flags &= ~DWC3_EP_BUSY;
Felipe Balbifae2b902011-10-14 13:00:30 +03001797
1798 /*
1799 * WORKAROUND: This is the 2nd half of U1/U2 -> U0 workaround.
1800 * See dwc3_gadget_linksts_change_interrupt() for 1st half.
1801 */
1802 if (dwc->revision < DWC3_REVISION_183A) {
1803 u32 reg;
1804 int i;
1805
1806 for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) {
1807 struct dwc3_ep *dep = dwc->eps[i];
1808
1809 if (!(dep->flags & DWC3_EP_ENABLED))
1810 continue;
1811
1812 if (!list_empty(&dep->req_queued))
1813 return;
1814 }
1815
1816 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
1817 reg |= dwc->u1u2;
1818 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1819
1820 dwc->u1u2 = 0;
1821 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001822}
1823
Felipe Balbi72246da2011-08-19 18:10:58 +03001824static void dwc3_endpoint_interrupt(struct dwc3 *dwc,
1825 const struct dwc3_event_depevt *event)
1826{
1827 struct dwc3_ep *dep;
1828 u8 epnum = event->endpoint_number;
1829
1830 dep = dwc->eps[epnum];
1831
Felipe Balbia09be0a2012-06-06 09:19:35 +03001832 if (!(dep->flags & DWC3_EP_ENABLED))
1833 return;
1834
Felipe Balbi72246da2011-08-19 18:10:58 +03001835 dev_vdbg(dwc->dev, "%s: %s\n", dep->name,
1836 dwc3_ep_event_string(event->endpoint_event));
1837
1838 if (epnum == 0 || epnum == 1) {
1839 dwc3_ep0_interrupt(dwc, event);
1840 return;
1841 }
1842
1843 switch (event->endpoint_event) {
1844 case DWC3_DEPEVT_XFERCOMPLETE:
Paul Zimmermanc2df85c2012-02-24 17:32:18 -08001845 dep->res_trans_idx = 0;
1846
Ido Shayevitz57cdac12012-03-12 20:25:24 +02001847 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001848 dev_dbg(dwc->dev, "%s is an Isochronous endpoint\n",
1849 dep->name);
1850 return;
1851 }
1852
1853 dwc3_endpoint_transfer_complete(dwc, dep, event, 1);
1854 break;
1855 case DWC3_DEPEVT_XFERINPROGRESS:
Ido Shayevitz57cdac12012-03-12 20:25:24 +02001856 if (!usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001857 dev_dbg(dwc->dev, "%s is not an Isochronous endpoint\n",
1858 dep->name);
1859 return;
1860 }
1861
1862 dwc3_endpoint_transfer_complete(dwc, dep, event, 0);
1863 break;
1864 case DWC3_DEPEVT_XFERNOTREADY:
Ido Shayevitz57cdac12012-03-12 20:25:24 +02001865 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001866 dwc3_gadget_start_isoc(dwc, dep, event);
1867 } else {
1868 int ret;
1869
1870 dev_vdbg(dwc->dev, "%s: reason %s\n",
Felipe Balbi40aa41f2012-01-18 17:06:03 +02001871 dep->name, event->status &
1872 DEPEVT_STATUS_TRANSFER_ACTIVE
Felipe Balbi72246da2011-08-19 18:10:58 +03001873 ? "Transfer Active"
1874 : "Transfer Not Active");
1875
1876 ret = __dwc3_gadget_kick_transfer(dep, 0, 1);
1877 if (!ret || ret == -EBUSY)
1878 return;
1879
1880 dev_dbg(dwc->dev, "%s: failed to kick transfers\n",
1881 dep->name);
1882 }
1883
1884 break;
Felipe Balbi879631a2011-09-30 10:58:47 +03001885 case DWC3_DEPEVT_STREAMEVT:
Ido Shayevitz57cdac12012-03-12 20:25:24 +02001886 if (!usb_endpoint_xfer_bulk(dep->endpoint.desc)) {
Felipe Balbi879631a2011-09-30 10:58:47 +03001887 dev_err(dwc->dev, "Stream event for non-Bulk %s\n",
1888 dep->name);
1889 return;
1890 }
1891
1892 switch (event->status) {
1893 case DEPEVT_STREAMEVT_FOUND:
1894 dev_vdbg(dwc->dev, "Stream %d found and started\n",
1895 event->parameters);
1896
1897 break;
1898 case DEPEVT_STREAMEVT_NOTFOUND:
1899 /* FALLTHROUGH */
1900 default:
1901 dev_dbg(dwc->dev, "Couldn't find suitable stream\n");
1902 }
1903 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03001904 case DWC3_DEPEVT_RXTXFIFOEVT:
1905 dev_dbg(dwc->dev, "%s FIFO Overrun\n", dep->name);
1906 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03001907 case DWC3_DEPEVT_EPCMDCMPLT:
Felipe Balbib129eb72012-02-17 12:10:04 +02001908 dev_vdbg(dwc->dev, "Endpoint Command Complete\n");
Felipe Balbi72246da2011-08-19 18:10:58 +03001909 break;
1910 }
1911}
1912
1913static void dwc3_disconnect_gadget(struct dwc3 *dwc)
1914{
1915 if (dwc->gadget_driver && dwc->gadget_driver->disconnect) {
1916 spin_unlock(&dwc->lock);
1917 dwc->gadget_driver->disconnect(&dwc->gadget);
1918 spin_lock(&dwc->lock);
1919 }
1920}
1921
1922static void dwc3_stop_active_transfer(struct dwc3 *dwc, u32 epnum)
1923{
1924 struct dwc3_ep *dep;
1925 struct dwc3_gadget_ep_cmd_params params;
1926 u32 cmd;
1927 int ret;
1928
1929 dep = dwc->eps[epnum];
1930
Pratyush Anand6263ebe2012-06-23 02:23:08 +05301931 if (!dep->res_trans_idx)
1932 return;
1933
1934 cmd = DWC3_DEPCMD_ENDTRANSFER;
1935 cmd |= DWC3_DEPCMD_HIPRI_FORCERM | DWC3_DEPCMD_CMDIOC;
1936 cmd |= DWC3_DEPCMD_PARAM(dep->res_trans_idx);
1937 memset(&params, 0, sizeof(params));
1938 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number, cmd, &params);
1939 WARN_ON_ONCE(ret);
1940 dep->res_trans_idx = 0;
Felipe Balbi72246da2011-08-19 18:10:58 +03001941}
1942
1943static void dwc3_stop_active_transfers(struct dwc3 *dwc)
1944{
1945 u32 epnum;
1946
1947 for (epnum = 2; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
1948 struct dwc3_ep *dep;
1949
1950 dep = dwc->eps[epnum];
1951 if (!(dep->flags & DWC3_EP_ENABLED))
1952 continue;
1953
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +02001954 dwc3_remove_requests(dwc, dep);
Felipe Balbi72246da2011-08-19 18:10:58 +03001955 }
1956}
1957
1958static void dwc3_clear_stall_all_ep(struct dwc3 *dwc)
1959{
1960 u32 epnum;
1961
1962 for (epnum = 1; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
1963 struct dwc3_ep *dep;
1964 struct dwc3_gadget_ep_cmd_params params;
1965 int ret;
1966
1967 dep = dwc->eps[epnum];
1968
1969 if (!(dep->flags & DWC3_EP_STALL))
1970 continue;
1971
1972 dep->flags &= ~DWC3_EP_STALL;
1973
1974 memset(&params, 0, sizeof(params));
1975 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number,
1976 DWC3_DEPCMD_CLEARSTALL, &params);
1977 WARN_ON_ONCE(ret);
1978 }
1979}
1980
1981static void dwc3_gadget_disconnect_interrupt(struct dwc3 *dwc)
1982{
Felipe Balbi34d548c2012-05-24 10:30:01 +03001983 int reg;
1984
Felipe Balbi72246da2011-08-19 18:10:58 +03001985 dev_vdbg(dwc->dev, "%s\n", __func__);
Felipe Balbi72246da2011-08-19 18:10:58 +03001986
1987 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
1988 reg &= ~DWC3_DCTL_INITU1ENA;
1989 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1990
1991 reg &= ~DWC3_DCTL_INITU2ENA;
1992 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +03001993
Felipe Balbi72246da2011-08-19 18:10:58 +03001994 dwc3_disconnect_gadget(dwc);
Paul Zimmermanb23c8432011-09-30 10:58:42 +03001995 dwc->start_config_issued = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03001996
1997 dwc->gadget.speed = USB_SPEED_UNKNOWN;
Felipe Balbidf62df52011-10-14 15:11:49 +03001998 dwc->setup_packet_pending = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03001999}
2000
Paul Zimmermandffb81b2012-04-27 12:54:05 +03002001static void dwc3_gadget_usb3_phy_suspend(struct dwc3 *dwc, int suspend)
Felipe Balbi72246da2011-08-19 18:10:58 +03002002{
2003 u32 reg;
2004
2005 reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0));
2006
Paul Zimmermandffb81b2012-04-27 12:54:05 +03002007 if (suspend)
Felipe Balbi72246da2011-08-19 18:10:58 +03002008 reg |= DWC3_GUSB3PIPECTL_SUSPHY;
Paul Zimmermandffb81b2012-04-27 12:54:05 +03002009 else
2010 reg &= ~DWC3_GUSB3PIPECTL_SUSPHY;
Felipe Balbi72246da2011-08-19 18:10:58 +03002011
2012 dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg);
2013}
2014
Paul Zimmermandffb81b2012-04-27 12:54:05 +03002015static void dwc3_gadget_usb2_phy_suspend(struct dwc3 *dwc, int suspend)
Felipe Balbi72246da2011-08-19 18:10:58 +03002016{
2017 u32 reg;
2018
2019 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
2020
Paul Zimmermandffb81b2012-04-27 12:54:05 +03002021 if (suspend)
Felipe Balbi72246da2011-08-19 18:10:58 +03002022 reg |= DWC3_GUSB2PHYCFG_SUSPHY;
Paul Zimmermandffb81b2012-04-27 12:54:05 +03002023 else
2024 reg &= ~DWC3_GUSB2PHYCFG_SUSPHY;
Felipe Balbi72246da2011-08-19 18:10:58 +03002025
2026 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
2027}
2028
2029static void dwc3_gadget_reset_interrupt(struct dwc3 *dwc)
2030{
2031 u32 reg;
2032
2033 dev_vdbg(dwc->dev, "%s\n", __func__);
2034
Felipe Balbidf62df52011-10-14 15:11:49 +03002035 /*
2036 * WORKAROUND: DWC3 revisions <1.88a have an issue which
2037 * would cause a missing Disconnect Event if there's a
2038 * pending Setup Packet in the FIFO.
2039 *
2040 * There's no suggested workaround on the official Bug
2041 * report, which states that "unless the driver/application
2042 * is doing any special handling of a disconnect event,
2043 * there is no functional issue".
2044 *
2045 * Unfortunately, it turns out that we _do_ some special
2046 * handling of a disconnect event, namely complete all
2047 * pending transfers, notify gadget driver of the
2048 * disconnection, and so on.
2049 *
2050 * Our suggested workaround is to follow the Disconnect
2051 * Event steps here, instead, based on a setup_packet_pending
2052 * flag. Such flag gets set whenever we have a XferNotReady
2053 * event on EP0 and gets cleared on XferComplete for the
2054 * same endpoint.
2055 *
2056 * Refers to:
2057 *
2058 * STAR#9000466709: RTL: Device : Disconnect event not
2059 * generated if setup packet pending in FIFO
2060 */
2061 if (dwc->revision < DWC3_REVISION_188A) {
2062 if (dwc->setup_packet_pending)
2063 dwc3_gadget_disconnect_interrupt(dwc);
2064 }
2065
Felipe Balbi961906e2011-12-20 15:37:21 +02002066 /* after reset -> Default State */
2067 dwc->dev_state = DWC3_DEFAULT_STATE;
2068
Paul Zimmerman88df4272012-04-27 13:10:52 +03002069 /* Recent versions support automatic phy suspend and don't need this */
2070 if (dwc->revision < DWC3_REVISION_194A) {
2071 /* Resume PHYs */
2072 dwc3_gadget_usb2_phy_suspend(dwc, false);
2073 dwc3_gadget_usb3_phy_suspend(dwc, false);
2074 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002075
2076 if (dwc->gadget.speed != USB_SPEED_UNKNOWN)
2077 dwc3_disconnect_gadget(dwc);
2078
2079 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2080 reg &= ~DWC3_DCTL_TSTCTRL_MASK;
Sebastian Andrzej Siewiorbb8b8a32011-09-13 17:54:39 +02002081 reg &= ~(DWC3_DCTL_INITU1ENA | DWC3_DCTL_INITU2ENA);
Gerard CAUVYd8bb4f72012-05-24 12:47:36 +03002082 reg |= (DWC3_DCTL_ACCEPTU1ENA | DWC3_DCTL_ACCEPTU2ENA);
Felipe Balbi72246da2011-08-19 18:10:58 +03002083 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Gerard Cauvy3b637362012-02-10 12:21:18 +02002084 dwc->test_mode = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03002085
2086 dwc3_stop_active_transfers(dwc);
2087 dwc3_clear_stall_all_ep(dwc);
Paul Zimmermanb23c8432011-09-30 10:58:42 +03002088 dwc->start_config_issued = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03002089
2090 /* Reset device address to zero */
2091 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2092 reg &= ~(DWC3_DCFG_DEVADDR_MASK);
2093 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +03002094}
2095
2096static void dwc3_update_ram_clk_sel(struct dwc3 *dwc, u32 speed)
2097{
2098 u32 reg;
2099 u32 usb30_clock = DWC3_GCTL_CLK_BUS;
2100
2101 /*
2102 * We change the clock only at SS but I dunno why I would want to do
2103 * this. Maybe it becomes part of the power saving plan.
2104 */
2105
2106 if (speed != DWC3_DSTS_SUPERSPEED)
2107 return;
2108
2109 /*
2110 * RAMClkSel is reset to 0 after USB reset, so it must be reprogrammed
2111 * each time on Connect Done.
2112 */
2113 if (!usb30_clock)
2114 return;
2115
2116 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
2117 reg |= DWC3_GCTL_RAMCLKSEL(usb30_clock);
2118 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
2119}
2120
Paul Zimmermandffb81b2012-04-27 12:54:05 +03002121static void dwc3_gadget_phy_suspend(struct dwc3 *dwc, u8 speed)
Felipe Balbi72246da2011-08-19 18:10:58 +03002122{
2123 switch (speed) {
2124 case USB_SPEED_SUPER:
Paul Zimmermandffb81b2012-04-27 12:54:05 +03002125 dwc3_gadget_usb2_phy_suspend(dwc, true);
Felipe Balbi72246da2011-08-19 18:10:58 +03002126 break;
2127 case USB_SPEED_HIGH:
2128 case USB_SPEED_FULL:
2129 case USB_SPEED_LOW:
Paul Zimmermandffb81b2012-04-27 12:54:05 +03002130 dwc3_gadget_usb3_phy_suspend(dwc, true);
Felipe Balbi72246da2011-08-19 18:10:58 +03002131 break;
2132 }
2133}
2134
2135static void dwc3_gadget_conndone_interrupt(struct dwc3 *dwc)
2136{
2137 struct dwc3_gadget_ep_cmd_params params;
2138 struct dwc3_ep *dep;
2139 int ret;
2140 u32 reg;
2141 u8 speed;
2142
2143 dev_vdbg(dwc->dev, "%s\n", __func__);
2144
2145 memset(&params, 0x00, sizeof(params));
2146
Felipe Balbi72246da2011-08-19 18:10:58 +03002147 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
2148 speed = reg & DWC3_DSTS_CONNECTSPD;
2149 dwc->speed = speed;
2150
2151 dwc3_update_ram_clk_sel(dwc, speed);
2152
2153 switch (speed) {
2154 case DWC3_DCFG_SUPERSPEED:
Felipe Balbi05870c52011-10-14 14:51:38 +03002155 /*
2156 * WORKAROUND: DWC3 revisions <1.90a have an issue which
2157 * would cause a missing USB3 Reset event.
2158 *
2159 * In such situations, we should force a USB3 Reset
2160 * event by calling our dwc3_gadget_reset_interrupt()
2161 * routine.
2162 *
2163 * Refers to:
2164 *
2165 * STAR#9000483510: RTL: SS : USB3 reset event may
2166 * not be generated always when the link enters poll
2167 */
2168 if (dwc->revision < DWC3_REVISION_190A)
2169 dwc3_gadget_reset_interrupt(dwc);
2170
Felipe Balbi72246da2011-08-19 18:10:58 +03002171 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
2172 dwc->gadget.ep0->maxpacket = 512;
2173 dwc->gadget.speed = USB_SPEED_SUPER;
2174 break;
2175 case DWC3_DCFG_HIGHSPEED:
2176 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
2177 dwc->gadget.ep0->maxpacket = 64;
2178 dwc->gadget.speed = USB_SPEED_HIGH;
2179 break;
2180 case DWC3_DCFG_FULLSPEED2:
2181 case DWC3_DCFG_FULLSPEED1:
2182 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
2183 dwc->gadget.ep0->maxpacket = 64;
2184 dwc->gadget.speed = USB_SPEED_FULL;
2185 break;
2186 case DWC3_DCFG_LOWSPEED:
2187 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(8);
2188 dwc->gadget.ep0->maxpacket = 8;
2189 dwc->gadget.speed = USB_SPEED_LOW;
2190 break;
2191 }
2192
Paul Zimmerman88df4272012-04-27 13:10:52 +03002193 /* Recent versions support automatic phy suspend and don't need this */
2194 if (dwc->revision < DWC3_REVISION_194A) {
2195 /* Suspend unneeded PHY */
2196 dwc3_gadget_phy_suspend(dwc, dwc->gadget.speed);
2197 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002198
2199 dep = dwc->eps[0];
Felipe Balbic90bfae2011-11-29 13:11:21 +02002200 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL);
Felipe Balbi72246da2011-08-19 18:10:58 +03002201 if (ret) {
2202 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
2203 return;
2204 }
2205
2206 dep = dwc->eps[1];
Felipe Balbic90bfae2011-11-29 13:11:21 +02002207 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL);
Felipe Balbi72246da2011-08-19 18:10:58 +03002208 if (ret) {
2209 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
2210 return;
2211 }
2212
2213 /*
2214 * Configure PHY via GUSB3PIPECTLn if required.
2215 *
2216 * Update GTXFIFOSIZn
2217 *
2218 * In both cases reset values should be sufficient.
2219 */
2220}
2221
2222static void dwc3_gadget_wakeup_interrupt(struct dwc3 *dwc)
2223{
2224 dev_vdbg(dwc->dev, "%s\n", __func__);
2225
2226 /*
2227 * TODO take core out of low power mode when that's
2228 * implemented.
2229 */
2230
2231 dwc->gadget_driver->resume(&dwc->gadget);
2232}
2233
2234static void dwc3_gadget_linksts_change_interrupt(struct dwc3 *dwc,
2235 unsigned int evtinfo)
2236{
Felipe Balbifae2b902011-10-14 13:00:30 +03002237 enum dwc3_link_state next = evtinfo & DWC3_LINK_STATE_MASK;
2238
2239 /*
2240 * WORKAROUND: DWC3 Revisions <1.83a have an issue which, depending
2241 * on the link partner, the USB session might do multiple entry/exit
2242 * of low power states before a transfer takes place.
2243 *
2244 * Due to this problem, we might experience lower throughput. The
2245 * suggested workaround is to disable DCTL[12:9] bits if we're
2246 * transitioning from U1/U2 to U0 and enable those bits again
2247 * after a transfer completes and there are no pending transfers
2248 * on any of the enabled endpoints.
2249 *
2250 * This is the first half of that workaround.
2251 *
2252 * Refers to:
2253 *
2254 * STAR#9000446952: RTL: Device SS : if U1/U2 ->U0 takes >128us
2255 * core send LGO_Ux entering U0
2256 */
2257 if (dwc->revision < DWC3_REVISION_183A) {
2258 if (next == DWC3_LINK_STATE_U0) {
2259 u32 u1u2;
2260 u32 reg;
2261
2262 switch (dwc->link_state) {
2263 case DWC3_LINK_STATE_U1:
2264 case DWC3_LINK_STATE_U2:
2265 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2266 u1u2 = reg & (DWC3_DCTL_INITU2ENA
2267 | DWC3_DCTL_ACCEPTU2ENA
2268 | DWC3_DCTL_INITU1ENA
2269 | DWC3_DCTL_ACCEPTU1ENA);
2270
2271 if (!dwc->u1u2)
2272 dwc->u1u2 = reg & u1u2;
2273
2274 reg &= ~u1u2;
2275
2276 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2277 break;
2278 default:
2279 /* do nothing */
2280 break;
2281 }
2282 }
2283 }
2284
2285 dwc->link_state = next;
Felipe Balbi019ac832011-09-08 21:18:47 +03002286
2287 dev_vdbg(dwc->dev, "%s link %d\n", __func__, dwc->link_state);
Felipe Balbi72246da2011-08-19 18:10:58 +03002288}
2289
2290static void dwc3_gadget_interrupt(struct dwc3 *dwc,
2291 const struct dwc3_event_devt *event)
2292{
2293 switch (event->type) {
2294 case DWC3_DEVICE_EVENT_DISCONNECT:
2295 dwc3_gadget_disconnect_interrupt(dwc);
2296 break;
2297 case DWC3_DEVICE_EVENT_RESET:
2298 dwc3_gadget_reset_interrupt(dwc);
2299 break;
2300 case DWC3_DEVICE_EVENT_CONNECT_DONE:
2301 dwc3_gadget_conndone_interrupt(dwc);
2302 break;
2303 case DWC3_DEVICE_EVENT_WAKEUP:
2304 dwc3_gadget_wakeup_interrupt(dwc);
2305 break;
2306 case DWC3_DEVICE_EVENT_LINK_STATUS_CHANGE:
2307 dwc3_gadget_linksts_change_interrupt(dwc, event->event_info);
2308 break;
2309 case DWC3_DEVICE_EVENT_EOPF:
2310 dev_vdbg(dwc->dev, "End of Periodic Frame\n");
2311 break;
2312 case DWC3_DEVICE_EVENT_SOF:
2313 dev_vdbg(dwc->dev, "Start of Periodic Frame\n");
2314 break;
2315 case DWC3_DEVICE_EVENT_ERRATIC_ERROR:
2316 dev_vdbg(dwc->dev, "Erratic Error\n");
2317 break;
2318 case DWC3_DEVICE_EVENT_CMD_CMPL:
2319 dev_vdbg(dwc->dev, "Command Complete\n");
2320 break;
2321 case DWC3_DEVICE_EVENT_OVERFLOW:
2322 dev_vdbg(dwc->dev, "Overflow\n");
Pavankumar Kondetid393e172012-06-12 16:07:29 +05302323 /*
2324 * Controllers prior to 2.30a revision has a bug where
2325 * Overflow Event may overwrite an unacknowledged event
2326 * in the event buffer. The severity of the issue depends
2327 * on the overwritten event type. Add a warning message
2328 * saying that an event is overwritten.
2329 *
2330 * TODO: In future we may need to see if we can re-enumerate
2331 * with host.
2332 */
2333 if (dwc->revision < DWC3_REVISION_230A)
2334 dev_warn(dwc->dev, "Unacknowledged event overwritten\n");
Felipe Balbi72246da2011-08-19 18:10:58 +03002335 break;
Pavankumar Kondeti33fe6f12012-06-12 16:21:46 +05302336 case DWC3_DEVICE_EVENT_VENDOR_DEV_TEST_LMP:
2337 /*
2338 * Controllers prior to 2.30a revision has a bug, due to which
2339 * a vendor device test LMP event can not be filtered. But
2340 * this event is not handled in the current code. This is a
2341 * special event and 8 bytes of data will follow the event.
2342 * Handling this event is tricky when event buffer is almost
2343 * full. Moreover this event will not occur in normal scenario
2344 * and can only happen with special hosts in testing scenarios.
2345 * Add a warning message to indicate that this event is received
2346 * which means that event buffer might have corrupted.
2347 */
2348 if (dwc->revision < DWC3_REVISION_230A)
2349 dev_warn(dwc->dev, "Vendor Device Test LMP Received\n");
2350 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03002351 default:
2352 dev_dbg(dwc->dev, "UNKNOWN IRQ %d\n", event->type);
2353 }
2354}
2355
2356static void dwc3_process_event_entry(struct dwc3 *dwc,
2357 const union dwc3_event *event)
2358{
2359 /* Endpoint IRQ, handle it and return early */
2360 if (event->type.is_devspec == 0) {
2361 /* depevt */
2362 return dwc3_endpoint_interrupt(dwc, &event->depevt);
2363 }
2364
2365 switch (event->type.type) {
2366 case DWC3_EVENT_TYPE_DEV:
2367 dwc3_gadget_interrupt(dwc, &event->devt);
2368 break;
2369 /* REVISIT what to do with Carkit and I2C events ? */
2370 default:
2371 dev_err(dwc->dev, "UNKNOWN IRQ type %d\n", event->raw);
2372 }
2373}
2374
2375static irqreturn_t dwc3_process_event_buf(struct dwc3 *dwc, u32 buf)
2376{
2377 struct dwc3_event_buffer *evt;
2378 int left;
2379 u32 count;
2380
2381 count = dwc3_readl(dwc->regs, DWC3_GEVNTCOUNT(buf));
2382 count &= DWC3_GEVNTCOUNT_MASK;
2383 if (!count)
2384 return IRQ_NONE;
2385
2386 evt = dwc->ev_buffs[buf];
2387 left = count;
2388
2389 while (left > 0) {
2390 union dwc3_event event;
2391
Felipe Balbid70d8442012-02-06 13:40:17 +02002392 event.raw = *(u32 *) (evt->buf + evt->lpos);
2393
Felipe Balbi72246da2011-08-19 18:10:58 +03002394 dwc3_process_event_entry(dwc, &event);
2395 /*
2396 * XXX we wrap around correctly to the next entry as almost all
2397 * entries are 4 bytes in size. There is one entry which has 12
2398 * bytes which is a regular entry followed by 8 bytes data. ATM
2399 * I don't know how things are organized if were get next to the
2400 * a boundary so I worry about that once we try to handle that.
2401 */
2402 evt->lpos = (evt->lpos + 4) % DWC3_EVENT_BUFFERS_SIZE;
2403 left -= 4;
2404
2405 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(buf), 4);
2406 }
2407
2408 return IRQ_HANDLED;
2409}
2410
2411static irqreturn_t dwc3_interrupt(int irq, void *_dwc)
2412{
2413 struct dwc3 *dwc = _dwc;
2414 int i;
2415 irqreturn_t ret = IRQ_NONE;
2416
2417 spin_lock(&dwc->lock);
2418
Felipe Balbi9f622b22011-10-12 10:31:04 +03002419 for (i = 0; i < dwc->num_event_buffers; i++) {
Felipe Balbi72246da2011-08-19 18:10:58 +03002420 irqreturn_t status;
2421
2422 status = dwc3_process_event_buf(dwc, i);
2423 if (status == IRQ_HANDLED)
2424 ret = status;
2425 }
2426
2427 spin_unlock(&dwc->lock);
2428
2429 return ret;
2430}
2431
2432/**
2433 * dwc3_gadget_init - Initializes gadget related registers
Paul Zimmerman1d046792012-02-15 18:56:56 -08002434 * @dwc: pointer to our controller context structure
Felipe Balbi72246da2011-08-19 18:10:58 +03002435 *
2436 * Returns 0 on success otherwise negative errno.
2437 */
2438int __devinit dwc3_gadget_init(struct dwc3 *dwc)
2439{
2440 u32 reg;
2441 int ret;
2442 int irq;
2443
2444 dwc->ctrl_req = dma_alloc_coherent(dwc->dev, sizeof(*dwc->ctrl_req),
2445 &dwc->ctrl_req_addr, GFP_KERNEL);
2446 if (!dwc->ctrl_req) {
2447 dev_err(dwc->dev, "failed to allocate ctrl request\n");
2448 ret = -ENOMEM;
2449 goto err0;
2450 }
2451
2452 dwc->ep0_trb = dma_alloc_coherent(dwc->dev, sizeof(*dwc->ep0_trb),
2453 &dwc->ep0_trb_addr, GFP_KERNEL);
2454 if (!dwc->ep0_trb) {
2455 dev_err(dwc->dev, "failed to allocate ep0 trb\n");
2456 ret = -ENOMEM;
2457 goto err1;
2458 }
2459
Felipe Balbib0791fb2012-05-04 12:58:14 +03002460 dwc->setup_buf = kzalloc(DWC3_EP0_BOUNCE_SIZE, GFP_KERNEL);
Felipe Balbi72246da2011-08-19 18:10:58 +03002461 if (!dwc->setup_buf) {
2462 dev_err(dwc->dev, "failed to allocate setup buffer\n");
2463 ret = -ENOMEM;
2464 goto err2;
2465 }
2466
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002467 dwc->ep0_bounce = dma_alloc_coherent(dwc->dev,
Felipe Balbib0791fb2012-05-04 12:58:14 +03002468 DWC3_EP0_BOUNCE_SIZE, &dwc->ep0_bounce_addr,
2469 GFP_KERNEL);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002470 if (!dwc->ep0_bounce) {
2471 dev_err(dwc->dev, "failed to allocate ep0 bounce buffer\n");
2472 ret = -ENOMEM;
2473 goto err3;
2474 }
2475
Felipe Balbi72246da2011-08-19 18:10:58 +03002476 dev_set_name(&dwc->gadget.dev, "gadget");
2477
2478 dwc->gadget.ops = &dwc3_gadget_ops;
Michal Nazarewiczd327ab52011-11-19 18:27:37 +01002479 dwc->gadget.max_speed = USB_SPEED_SUPER;
Felipe Balbi72246da2011-08-19 18:10:58 +03002480 dwc->gadget.speed = USB_SPEED_UNKNOWN;
2481 dwc->gadget.dev.parent = dwc->dev;
Felipe Balbieeb720f2011-11-28 12:46:59 +02002482 dwc->gadget.sg_supported = true;
Felipe Balbi72246da2011-08-19 18:10:58 +03002483
2484 dma_set_coherent_mask(&dwc->gadget.dev, dwc->dev->coherent_dma_mask);
2485
2486 dwc->gadget.dev.dma_parms = dwc->dev->dma_parms;
2487 dwc->gadget.dev.dma_mask = dwc->dev->dma_mask;
2488 dwc->gadget.dev.release = dwc3_gadget_release;
2489 dwc->gadget.name = "dwc3-gadget";
2490
2491 /*
2492 * REVISIT: Here we should clear all pending IRQs to be
2493 * sure we're starting from a well known location.
2494 */
2495
2496 ret = dwc3_gadget_init_endpoints(dwc);
2497 if (ret)
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002498 goto err4;
Felipe Balbi72246da2011-08-19 18:10:58 +03002499
2500 irq = platform_get_irq(to_platform_device(dwc->dev), 0);
2501
2502 ret = request_irq(irq, dwc3_interrupt, IRQF_SHARED,
2503 "dwc3", dwc);
2504 if (ret) {
2505 dev_err(dwc->dev, "failed to request irq #%d --> %d\n",
2506 irq, ret);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002507 goto err5;
Felipe Balbi72246da2011-08-19 18:10:58 +03002508 }
2509
Sebastian Andrzej Siewiorbb8b8a32011-09-13 17:54:39 +02002510 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2511 reg |= DWC3_DCFG_LPM_CAP;
2512 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
2513
Felipe Balbi72246da2011-08-19 18:10:58 +03002514 /* Enable all but Start and End of Frame IRQs */
Pavankumar Kondeti33fe6f12012-06-12 16:21:46 +05302515 reg = (DWC3_DEVTEN_EVNTOVERFLOWEN |
Felipe Balbi72246da2011-08-19 18:10:58 +03002516 DWC3_DEVTEN_CMDCMPLTEN |
2517 DWC3_DEVTEN_ERRTICERREN |
2518 DWC3_DEVTEN_WKUPEVTEN |
2519 DWC3_DEVTEN_ULSTCNGEN |
2520 DWC3_DEVTEN_CONNECTDONEEN |
2521 DWC3_DEVTEN_USBRSTEN |
2522 DWC3_DEVTEN_DISCONNEVTEN);
2523 dwc3_writel(dwc->regs, DWC3_DEVTEN, reg);
2524
Paul Zimmerman88df4272012-04-27 13:10:52 +03002525 /* Enable USB2 LPM and automatic phy suspend only on recent versions */
2526 if (dwc->revision >= DWC3_REVISION_194A) {
2527 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2528 reg |= DWC3_DCFG_LPM_CAP;
2529 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
2530
2531 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2532 reg &= ~(DWC3_DCTL_HIRD_THRES_MASK | DWC3_DCTL_L1_HIBER_EN);
2533
2534 /* TODO: This should be configurable */
2535 reg |= DWC3_DCTL_HIRD_THRES(31);
2536
2537 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2538
Pratyush Anand50ed8342012-06-06 19:36:17 +05302539 dwc3_gadget_usb2_phy_suspend(dwc, false);
2540 dwc3_gadget_usb3_phy_suspend(dwc, false);
Paul Zimmerman88df4272012-04-27 13:10:52 +03002541 }
2542
Felipe Balbi72246da2011-08-19 18:10:58 +03002543 ret = device_register(&dwc->gadget.dev);
2544 if (ret) {
2545 dev_err(dwc->dev, "failed to register gadget device\n");
2546 put_device(&dwc->gadget.dev);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002547 goto err6;
Felipe Balbi72246da2011-08-19 18:10:58 +03002548 }
2549
2550 ret = usb_add_gadget_udc(dwc->dev, &dwc->gadget);
2551 if (ret) {
2552 dev_err(dwc->dev, "failed to register udc\n");
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002553 goto err7;
Felipe Balbi72246da2011-08-19 18:10:58 +03002554 }
2555
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +02002556 if (dwc->dotg) {
2557 /* dwc3 otg driver is active (DRD mode + SRPSupport=1) */
2558 ret = otg_set_peripheral(&dwc->dotg->otg, &dwc->gadget);
2559 if (ret) {
2560 dev_err(dwc->dev, "failed to set peripheral to otg\n");
2561 goto err7;
2562 }
Manu Gautamb5067272012-07-02 09:53:41 +05302563 } else {
2564 pm_runtime_no_callbacks(&dwc->gadget.dev);
2565 pm_runtime_set_active(&dwc->gadget.dev);
2566 pm_runtime_enable(&dwc->gadget.dev);
2567 pm_runtime_get(&dwc->gadget.dev);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +02002568 }
2569
Felipe Balbi72246da2011-08-19 18:10:58 +03002570 return 0;
2571
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002572err7:
Felipe Balbi72246da2011-08-19 18:10:58 +03002573 device_unregister(&dwc->gadget.dev);
2574
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002575err6:
Felipe Balbi72246da2011-08-19 18:10:58 +03002576 dwc3_writel(dwc->regs, DWC3_DEVTEN, 0x00);
2577 free_irq(irq, dwc);
2578
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002579err5:
Felipe Balbi72246da2011-08-19 18:10:58 +03002580 dwc3_gadget_free_endpoints(dwc);
2581
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002582err4:
Felipe Balbib0791fb2012-05-04 12:58:14 +03002583 dma_free_coherent(dwc->dev, DWC3_EP0_BOUNCE_SIZE,
2584 dwc->ep0_bounce, dwc->ep0_bounce_addr);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002585
Felipe Balbi72246da2011-08-19 18:10:58 +03002586err3:
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02002587 kfree(dwc->setup_buf);
Felipe Balbi72246da2011-08-19 18:10:58 +03002588
2589err2:
2590 dma_free_coherent(dwc->dev, sizeof(*dwc->ep0_trb),
2591 dwc->ep0_trb, dwc->ep0_trb_addr);
2592
2593err1:
2594 dma_free_coherent(dwc->dev, sizeof(*dwc->ctrl_req),
2595 dwc->ctrl_req, dwc->ctrl_req_addr);
2596
2597err0:
2598 return ret;
2599}
2600
2601void dwc3_gadget_exit(struct dwc3 *dwc)
2602{
2603 int irq;
Felipe Balbi72246da2011-08-19 18:10:58 +03002604
Manu Gautamb5067272012-07-02 09:53:41 +05302605 if (dwc->dotg) {
2606 pm_runtime_put(&dwc->gadget.dev);
2607 pm_runtime_disable(&dwc->gadget.dev);
2608 }
2609
Felipe Balbi72246da2011-08-19 18:10:58 +03002610 usb_del_gadget_udc(&dwc->gadget);
2611 irq = platform_get_irq(to_platform_device(dwc->dev), 0);
2612
2613 dwc3_writel(dwc->regs, DWC3_DEVTEN, 0x00);
2614 free_irq(irq, dwc);
2615
Felipe Balbi72246da2011-08-19 18:10:58 +03002616 dwc3_gadget_free_endpoints(dwc);
2617
Felipe Balbib0791fb2012-05-04 12:58:14 +03002618 dma_free_coherent(dwc->dev, DWC3_EP0_BOUNCE_SIZE,
2619 dwc->ep0_bounce, dwc->ep0_bounce_addr);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002620
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02002621 kfree(dwc->setup_buf);
Felipe Balbi72246da2011-08-19 18:10:58 +03002622
2623 dma_free_coherent(dwc->dev, sizeof(*dwc->ep0_trb),
2624 dwc->ep0_trb, dwc->ep0_trb_addr);
2625
2626 dma_free_coherent(dwc->dev, sizeof(*dwc->ctrl_req),
2627 dwc->ctrl_req, dwc->ctrl_req_addr);
2628
2629 device_unregister(&dwc->gadget.dev);
2630}