blob: 9fa1ab4679e1fa73f08658fc0a5dff6d554e9a82 [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) {
Manu Gautam55d34222012-12-19 16:49:47 +0530247 req->queued = false;
248
Felipe Balbieeb720f2011-11-28 12:46:59 +0200249 if (req->request.num_mapped_sgs)
250 dep->busy_slot += req->request.num_mapped_sgs;
251 else
252 dep->busy_slot++;
253
Felipe Balbi72246da2011-08-19 18:10:58 +0300254 /*
255 * Skip LINK TRB. We can't use req->trb and check for
256 * DWC3_TRBCTL_LINK_TRB because it points the TRB we just
257 * completed (not the LINK TRB).
258 */
259 if (((dep->busy_slot & DWC3_TRB_MASK) == DWC3_TRB_NUM - 1) &&
Ido Shayevitz57cdac12012-03-12 20:25:24 +0200260 usb_endpoint_xfer_isoc(dep->endpoint.desc))
Felipe Balbi72246da2011-08-19 18:10:58 +0300261 dep->busy_slot++;
262 }
263 list_del(&req->list);
Felipe Balbieeb720f2011-11-28 12:46:59 +0200264 req->trb = NULL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300265
266 if (req->request.status == -EINPROGRESS)
267 req->request.status = status;
268
Pratyush Anand8d7bf592012-08-10 13:42:16 +0530269 if (dwc->ep0_bounced && dep->number == 0)
270 dwc->ep0_bounced = false;
271 else
272 usb_gadget_unmap_request(&dwc->gadget, &req->request,
273 req->direction);
Felipe Balbi72246da2011-08-19 18:10:58 +0300274
275 dev_dbg(dwc->dev, "request %p from %s completed %d/%d ===> %d\n",
276 req, dep->name, req->request.actual,
277 req->request.length, status);
278
279 spin_unlock(&dwc->lock);
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +0200280 req->request.complete(&dep->endpoint, &req->request);
Felipe Balbi72246da2011-08-19 18:10:58 +0300281 spin_lock(&dwc->lock);
282}
283
284static const char *dwc3_gadget_ep_cmd_string(u8 cmd)
285{
286 switch (cmd) {
287 case DWC3_DEPCMD_DEPSTARTCFG:
288 return "Start New Configuration";
289 case DWC3_DEPCMD_ENDTRANSFER:
290 return "End Transfer";
291 case DWC3_DEPCMD_UPDATETRANSFER:
292 return "Update Transfer";
293 case DWC3_DEPCMD_STARTTRANSFER:
294 return "Start Transfer";
295 case DWC3_DEPCMD_CLEARSTALL:
296 return "Clear Stall";
297 case DWC3_DEPCMD_SETSTALL:
298 return "Set Stall";
Paul Zimmerman88df4272012-04-27 13:10:52 +0300299 case DWC3_DEPCMD_GETEPSTATE:
300 return "Get Endpoint State";
Felipe Balbi72246da2011-08-19 18:10:58 +0300301 case DWC3_DEPCMD_SETTRANSFRESOURCE:
302 return "Set Endpoint Transfer Resource";
303 case DWC3_DEPCMD_SETEPCONFIG:
304 return "Set Endpoint Configuration";
305 default:
306 return "UNKNOWN command";
307 }
308}
309
Felipe Balbi573c2762012-04-24 16:19:11 +0300310int dwc3_send_gadget_generic_command(struct dwc3 *dwc, int cmd, u32 param)
311{
312 u32 timeout = 500;
313 u32 reg;
314
315 dwc3_writel(dwc->regs, DWC3_DGCMDPAR, param);
316 dwc3_writel(dwc->regs, DWC3_DGCMD, cmd | DWC3_DGCMD_CMDACT);
317
318 do {
319 reg = dwc3_readl(dwc->regs, DWC3_DGCMD);
320 if (!(reg & DWC3_DGCMD_CMDACT)) {
321 dev_vdbg(dwc->dev, "Command Complete --> %d\n",
322 DWC3_DGCMD_STATUS(reg));
323 return 0;
324 }
325
326 /*
327 * We can't sleep here, because it's also called from
328 * interrupt context.
329 */
330 timeout--;
331 if (!timeout)
332 return -ETIMEDOUT;
333 udelay(1);
334 } while (1);
335}
336
Felipe Balbi72246da2011-08-19 18:10:58 +0300337int dwc3_send_gadget_ep_cmd(struct dwc3 *dwc, unsigned ep,
338 unsigned cmd, struct dwc3_gadget_ep_cmd_params *params)
339{
340 struct dwc3_ep *dep = dwc->eps[ep];
Sebastian Andrzej Siewior61d58242011-08-29 16:46:38 +0200341 u32 timeout = 500;
Felipe Balbi72246da2011-08-19 18:10:58 +0300342 u32 reg;
343
344 dev_vdbg(dwc->dev, "%s: cmd '%s' params %08x %08x %08x\n",
345 dep->name,
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300346 dwc3_gadget_ep_cmd_string(cmd), params->param0,
347 params->param1, params->param2);
Felipe Balbi72246da2011-08-19 18:10:58 +0300348
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300349 dwc3_writel(dwc->regs, DWC3_DEPCMDPAR0(ep), params->param0);
350 dwc3_writel(dwc->regs, DWC3_DEPCMDPAR1(ep), params->param1);
351 dwc3_writel(dwc->regs, DWC3_DEPCMDPAR2(ep), params->param2);
Felipe Balbi72246da2011-08-19 18:10:58 +0300352
353 dwc3_writel(dwc->regs, DWC3_DEPCMD(ep), cmd | DWC3_DEPCMD_CMDACT);
354 do {
355 reg = dwc3_readl(dwc->regs, DWC3_DEPCMD(ep));
356 if (!(reg & DWC3_DEPCMD_CMDACT)) {
Felipe Balbi164f6e12011-08-27 20:29:58 +0300357 dev_vdbg(dwc->dev, "Command Complete --> %d\n",
358 DWC3_DEPCMD_STATUS(reg));
Vijayavardhan Vennapusa91ba6532013-01-30 17:35:45 +0530359 /* SW issues START TRANSFER command to isochronous ep
360 * with future frame interval. If future interval time
361 * has already passed when core recieves command, core
362 * will respond with an error(bit13 in Command complete
363 * event. Hence return error in this case.
364 */
365 if (reg & 0x2000)
366 return -EAGAIN;
367 else
368 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300369 }
370
371 /*
Felipe Balbi72246da2011-08-19 18:10:58 +0300372 * We can't sleep here, because it is also called from
373 * interrupt context.
374 */
375 timeout--;
376 if (!timeout)
377 return -ETIMEDOUT;
378
Sebastian Andrzej Siewior61d58242011-08-29 16:46:38 +0200379 udelay(1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300380 } while (1);
381}
382
Ido Shayevitzfa65a582012-06-06 14:39:54 +0300383dma_addr_t dwc3_trb_dma_offset(struct dwc3_ep *dep,
Felipe Balbif6bafc62012-02-06 11:04:53 +0200384 struct dwc3_trb *trb)
Felipe Balbi72246da2011-08-19 18:10:58 +0300385{
Paul Zimmermanc439ef82011-09-30 10:58:45 +0300386 u32 offset = (char *) trb - (char *) dep->trb_pool;
Felipe Balbi72246da2011-08-19 18:10:58 +0300387
388 return dep->trb_pool_dma + offset;
389}
390
391static int dwc3_alloc_trb_pool(struct dwc3_ep *dep)
392{
393 struct dwc3 *dwc = dep->dwc;
394
395 if (dep->trb_pool)
396 return 0;
397
398 if (dep->number == 0 || dep->number == 1)
399 return 0;
400
401 dep->trb_pool = dma_alloc_coherent(dwc->dev,
402 sizeof(struct dwc3_trb) * DWC3_TRB_NUM,
403 &dep->trb_pool_dma, GFP_KERNEL);
404 if (!dep->trb_pool) {
405 dev_err(dep->dwc->dev, "failed to allocate trb pool for %s\n",
406 dep->name);
407 return -ENOMEM;
408 }
409
410 return 0;
411}
412
413static void dwc3_free_trb_pool(struct dwc3_ep *dep)
414{
415 struct dwc3 *dwc = dep->dwc;
416
417 dma_free_coherent(dwc->dev, sizeof(struct dwc3_trb) * DWC3_TRB_NUM,
418 dep->trb_pool, dep->trb_pool_dma);
419
420 dep->trb_pool = NULL;
421 dep->trb_pool_dma = 0;
422}
423
424static int dwc3_gadget_start_config(struct dwc3 *dwc, struct dwc3_ep *dep)
425{
426 struct dwc3_gadget_ep_cmd_params params;
427 u32 cmd;
428
429 memset(&params, 0x00, sizeof(params));
430
431 if (dep->number != 1) {
432 cmd = DWC3_DEPCMD_DEPSTARTCFG;
433 /* XferRscIdx == 0 for ep0 and 2 for the remaining */
Paul Zimmermanb23c8432011-09-30 10:58:42 +0300434 if (dep->number > 1) {
435 if (dwc->start_config_issued)
436 return 0;
437 dwc->start_config_issued = true;
Felipe Balbi72246da2011-08-19 18:10:58 +0300438 cmd |= DWC3_DEPCMD_PARAM(2);
Paul Zimmermanb23c8432011-09-30 10:58:42 +0300439 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300440
441 return dwc3_send_gadget_ep_cmd(dwc, 0, cmd, &params);
442 }
443
444 return 0;
445}
446
447static int dwc3_gadget_set_ep_config(struct dwc3 *dwc, struct dwc3_ep *dep,
Felipe Balbic90bfae2011-11-29 13:11:21 +0200448 const struct usb_endpoint_descriptor *desc,
Felipe Balbi07e0ee82012-07-16 14:08:16 +0300449 const struct usb_ss_ep_comp_descriptor *comp_desc,
450 bool ignore)
Felipe Balbi72246da2011-08-19 18:10:58 +0300451{
452 struct dwc3_gadget_ep_cmd_params params;
453
454 memset(&params, 0x00, sizeof(params));
455
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300456 params.param0 = DWC3_DEPCFG_EP_TYPE(usb_endpoint_type(desc))
Chanho Parkf0ee6062012-08-31 16:54:07 +0900457 | DWC3_DEPCFG_MAX_PACKET_SIZE(usb_endpoint_maxp(desc));
458
459 /* Burst size is only needed in SuperSpeed mode */
460 if (dwc->gadget.speed == USB_SPEED_SUPER) {
461 u32 burst = dep->endpoint.maxburst - 1;
462
463 params.param0 |= DWC3_DEPCFG_BURST_SIZE(burst);
464 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300465
Felipe Balbi07e0ee82012-07-16 14:08:16 +0300466 if (ignore)
467 params.param0 |= DWC3_DEPCFG_IGN_SEQ_NUM;
Felipe Balbi72246da2011-08-19 18:10:58 +0300468
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300469 params.param1 = DWC3_DEPCFG_XFER_COMPLETE_EN
470 | DWC3_DEPCFG_XFER_NOT_READY_EN;
Felipe Balbi72246da2011-08-19 18:10:58 +0300471
Felipe Balbi18b7ede2012-01-02 13:35:41 +0200472 if (usb_ss_max_streams(comp_desc) && usb_endpoint_xfer_bulk(desc)) {
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300473 params.param1 |= DWC3_DEPCFG_STREAM_CAPABLE
474 | DWC3_DEPCFG_STREAM_EVENT_EN;
Felipe Balbi879631a2011-09-30 10:58:47 +0300475 dep->stream_capable = true;
476 }
477
Felipe Balbi72246da2011-08-19 18:10:58 +0300478 if (usb_endpoint_xfer_isoc(desc))
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300479 params.param1 |= DWC3_DEPCFG_XFER_IN_PROGRESS_EN;
Felipe Balbi72246da2011-08-19 18:10:58 +0300480
481 /*
482 * We are doing 1:1 mapping for endpoints, meaning
483 * Physical Endpoints 2 maps to Logical Endpoint 2 and
484 * so on. We consider the direction bit as part of the physical
485 * endpoint number. So USB endpoint 0x81 is 0x03.
486 */
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300487 params.param1 |= DWC3_DEPCFG_EP_NUMBER(dep->number);
Felipe Balbi72246da2011-08-19 18:10:58 +0300488
489 /*
490 * We must use the lower 16 TX FIFOs even though
491 * HW might have more
492 */
493 if (dep->direction)
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300494 params.param0 |= DWC3_DEPCFG_FIFO_NUMBER(dep->number >> 1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300495
496 if (desc->bInterval) {
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300497 params.param1 |= DWC3_DEPCFG_BINTERVAL_M1(desc->bInterval - 1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300498 dep->interval = 1 << (desc->bInterval - 1);
499 }
500
501 return dwc3_send_gadget_ep_cmd(dwc, dep->number,
502 DWC3_DEPCMD_SETEPCONFIG, &params);
503}
504
505static int dwc3_gadget_set_xfer_resource(struct dwc3 *dwc, struct dwc3_ep *dep)
506{
507 struct dwc3_gadget_ep_cmd_params params;
508
509 memset(&params, 0x00, sizeof(params));
510
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300511 params.param0 = DWC3_DEPXFERCFG_NUM_XFER_RES(1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300512
513 return dwc3_send_gadget_ep_cmd(dwc, dep->number,
514 DWC3_DEPCMD_SETTRANSFRESOURCE, &params);
515}
516
517/**
518 * __dwc3_gadget_ep_enable - Initializes a HW endpoint
519 * @dep: endpoint to be initialized
520 * @desc: USB Endpoint Descriptor
521 *
522 * Caller should take care of locking
523 */
524static int __dwc3_gadget_ep_enable(struct dwc3_ep *dep,
Felipe Balbic90bfae2011-11-29 13:11:21 +0200525 const struct usb_endpoint_descriptor *desc,
Felipe Balbi07e0ee82012-07-16 14:08:16 +0300526 const struct usb_ss_ep_comp_descriptor *comp_desc,
527 bool ignore)
Felipe Balbi72246da2011-08-19 18:10:58 +0300528{
529 struct dwc3 *dwc = dep->dwc;
530 u32 reg;
531 int ret = -ENOMEM;
532
533 if (!(dep->flags & DWC3_EP_ENABLED)) {
534 ret = dwc3_gadget_start_config(dwc, dep);
535 if (ret)
536 return ret;
537 }
538
Felipe Balbi07e0ee82012-07-16 14:08:16 +0300539 ret = dwc3_gadget_set_ep_config(dwc, dep, desc, comp_desc, ignore);
Felipe Balbi72246da2011-08-19 18:10:58 +0300540 if (ret)
541 return ret;
542
543 if (!(dep->flags & DWC3_EP_ENABLED)) {
Felipe Balbif6bafc62012-02-06 11:04:53 +0200544 struct dwc3_trb *trb_st_hw;
545 struct dwc3_trb *trb_link;
Felipe Balbi72246da2011-08-19 18:10:58 +0300546
547 ret = dwc3_gadget_set_xfer_resource(dwc, dep);
548 if (ret)
549 return ret;
550
Ido Shayevitz57cdac12012-03-12 20:25:24 +0200551 dep->endpoint.desc = desc;
Felipe Balbic90bfae2011-11-29 13:11:21 +0200552 dep->comp_desc = comp_desc;
Felipe Balbi72246da2011-08-19 18:10:58 +0300553 dep->type = usb_endpoint_type(desc);
554 dep->flags |= DWC3_EP_ENABLED;
555
556 reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
557 reg |= DWC3_DALEPENA_EP(dep->number);
558 dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
559
560 if (!usb_endpoint_xfer_isoc(desc))
561 return 0;
562
563 memset(&trb_link, 0, sizeof(trb_link));
564
Paul Zimmerman1d046792012-02-15 18:56:56 -0800565 /* Link TRB for ISOC. The HWO bit is never reset */
Felipe Balbi72246da2011-08-19 18:10:58 +0300566 trb_st_hw = &dep->trb_pool[0];
567
Felipe Balbif6bafc62012-02-06 11:04:53 +0200568 trb_link = &dep->trb_pool[DWC3_TRB_NUM - 1];
Felipe Balbi72246da2011-08-19 18:10:58 +0300569
Felipe Balbif6bafc62012-02-06 11:04:53 +0200570 trb_link->bpl = lower_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw));
571 trb_link->bph = upper_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw));
572 trb_link->ctrl |= DWC3_TRBCTL_LINK_TRB;
573 trb_link->ctrl |= DWC3_TRB_CTRL_HWO;
Felipe Balbi72246da2011-08-19 18:10:58 +0300574 }
575
576 return 0;
577}
578
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200579static void dwc3_stop_active_transfer(struct dwc3 *dwc, u32 epnum);
580static void dwc3_remove_requests(struct dwc3 *dwc, struct dwc3_ep *dep)
Felipe Balbi72246da2011-08-19 18:10:58 +0300581{
582 struct dwc3_request *req;
583
Felipe Balbib129eb72012-02-17 12:10:04 +0200584 if (!list_empty(&dep->req_queued)) {
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200585 dwc3_stop_active_transfer(dwc, dep->number);
586
Pratyush Anande67fdeb2012-07-06 15:19:10 +0530587 /* - giveback all requests to gadget driver */
Pratyush Anand110ff602012-06-15 11:54:36 +0530588 while (!list_empty(&dep->req_queued)) {
589 req = next_request(&dep->req_queued);
590
591 dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
592 }
Felipe Balbib129eb72012-02-17 12:10:04 +0200593 }
594
Felipe Balbi72246da2011-08-19 18:10:58 +0300595 while (!list_empty(&dep->request_list)) {
596 req = next_request(&dep->request_list);
597
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200598 dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
Felipe Balbi72246da2011-08-19 18:10:58 +0300599 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300600}
601
602/**
603 * __dwc3_gadget_ep_disable - Disables a HW endpoint
604 * @dep: the endpoint to disable
605 *
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200606 * This function also removes requests which are currently processed ny the
607 * hardware and those which are not yet scheduled.
608 * Caller should take care of locking.
Felipe Balbi72246da2011-08-19 18:10:58 +0300609 */
Felipe Balbi72246da2011-08-19 18:10:58 +0300610static int __dwc3_gadget_ep_disable(struct dwc3_ep *dep)
611{
612 struct dwc3 *dwc = dep->dwc;
613 u32 reg;
614
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200615 dwc3_remove_requests(dwc, dep);
Felipe Balbi72246da2011-08-19 18:10:58 +0300616
617 reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
618 reg &= ~DWC3_DALEPENA_EP(dep->number);
619 dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
620
Felipe Balbi879631a2011-09-30 10:58:47 +0300621 dep->stream_capable = false;
Ido Shayevitzf9c56cd2012-02-08 13:56:48 +0200622 dep->endpoint.desc = NULL;
Felipe Balbic90bfae2011-11-29 13:11:21 +0200623 dep->comp_desc = NULL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300624 dep->type = 0;
Felipe Balbi879631a2011-09-30 10:58:47 +0300625 dep->flags = 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300626
627 return 0;
628}
629
630/* -------------------------------------------------------------------------- */
631
632static int dwc3_gadget_ep0_enable(struct usb_ep *ep,
633 const struct usb_endpoint_descriptor *desc)
634{
635 return -EINVAL;
636}
637
638static int dwc3_gadget_ep0_disable(struct usb_ep *ep)
639{
640 return -EINVAL;
641}
642
643/* -------------------------------------------------------------------------- */
644
645static int dwc3_gadget_ep_enable(struct usb_ep *ep,
646 const struct usb_endpoint_descriptor *desc)
647{
648 struct dwc3_ep *dep;
649 struct dwc3 *dwc;
650 unsigned long flags;
651 int ret;
652
653 if (!ep || !desc || desc->bDescriptorType != USB_DT_ENDPOINT) {
654 pr_debug("dwc3: invalid parameters\n");
655 return -EINVAL;
656 }
657
658 if (!desc->wMaxPacketSize) {
659 pr_debug("dwc3: missing wMaxPacketSize\n");
660 return -EINVAL;
661 }
662
663 dep = to_dwc3_ep(ep);
664 dwc = dep->dwc;
665
Felipe Balbi14395072012-08-15 12:28:29 +0300666 if (dep->flags & DWC3_EP_ENABLED) {
667 dev_WARN_ONCE(dwc->dev, true, "%s is already enabled\n",
668 dep->name);
669 return 0;
670 }
671
Felipe Balbi72246da2011-08-19 18:10:58 +0300672 switch (usb_endpoint_type(desc)) {
673 case USB_ENDPOINT_XFER_CONTROL:
Anton Tikhomirov27a78d62012-02-23 15:38:46 +0900674 strlcat(dep->name, "-control", sizeof(dep->name));
Felipe Balbi72246da2011-08-19 18:10:58 +0300675 break;
676 case USB_ENDPOINT_XFER_ISOC:
Anton Tikhomirov27a78d62012-02-23 15:38:46 +0900677 strlcat(dep->name, "-isoc", sizeof(dep->name));
Felipe Balbi72246da2011-08-19 18:10:58 +0300678 break;
679 case USB_ENDPOINT_XFER_BULK:
Anton Tikhomirov27a78d62012-02-23 15:38:46 +0900680 strlcat(dep->name, "-bulk", sizeof(dep->name));
Felipe Balbi72246da2011-08-19 18:10:58 +0300681 break;
682 case USB_ENDPOINT_XFER_INT:
Anton Tikhomirov27a78d62012-02-23 15:38:46 +0900683 strlcat(dep->name, "-int", sizeof(dep->name));
Felipe Balbi72246da2011-08-19 18:10:58 +0300684 break;
685 default:
686 dev_err(dwc->dev, "invalid endpoint transfer type\n");
687 }
688
Felipe Balbi72246da2011-08-19 18:10:58 +0300689 dev_vdbg(dwc->dev, "Enabling %s\n", dep->name);
690
691 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi07e0ee82012-07-16 14:08:16 +0300692 ret = __dwc3_gadget_ep_enable(dep, desc, ep->comp_desc, false);
Felipe Balbi72246da2011-08-19 18:10:58 +0300693 spin_unlock_irqrestore(&dwc->lock, flags);
694
695 return ret;
696}
697
698static int dwc3_gadget_ep_disable(struct usb_ep *ep)
699{
700 struct dwc3_ep *dep;
701 struct dwc3 *dwc;
702 unsigned long flags;
703 int ret;
704
705 if (!ep) {
706 pr_debug("dwc3: invalid parameters\n");
707 return -EINVAL;
708 }
709
710 dep = to_dwc3_ep(ep);
711 dwc = dep->dwc;
712
713 if (!(dep->flags & DWC3_EP_ENABLED)) {
714 dev_WARN_ONCE(dwc->dev, true, "%s is already disabled\n",
715 dep->name);
716 return 0;
717 }
718
719 snprintf(dep->name, sizeof(dep->name), "ep%d%s",
720 dep->number >> 1,
721 (dep->number & 1) ? "in" : "out");
722
723 spin_lock_irqsave(&dwc->lock, flags);
724 ret = __dwc3_gadget_ep_disable(dep);
725 spin_unlock_irqrestore(&dwc->lock, flags);
726
727 return ret;
728}
729
730static struct usb_request *dwc3_gadget_ep_alloc_request(struct usb_ep *ep,
731 gfp_t gfp_flags)
732{
733 struct dwc3_request *req;
734 struct dwc3_ep *dep = to_dwc3_ep(ep);
735 struct dwc3 *dwc = dep->dwc;
736
737 req = kzalloc(sizeof(*req), gfp_flags);
738 if (!req) {
739 dev_err(dwc->dev, "not enough memory\n");
740 return NULL;
741 }
742
743 req->epnum = dep->number;
744 req->dep = dep;
Felipe Balbi72246da2011-08-19 18:10:58 +0300745
746 return &req->request;
747}
748
749static void dwc3_gadget_ep_free_request(struct usb_ep *ep,
750 struct usb_request *request)
751{
752 struct dwc3_request *req = to_dwc3_request(request);
753
754 kfree(req);
755}
756
Felipe Balbic71fc372011-11-22 11:37:34 +0200757/**
758 * dwc3_prepare_one_trb - setup one TRB from one request
759 * @dep: endpoint for which this request is prepared
760 * @req: dwc3_request pointer
761 */
Felipe Balbi68e823e2011-11-28 12:25:01 +0200762static void dwc3_prepare_one_trb(struct dwc3_ep *dep,
Felipe Balbieeb720f2011-11-28 12:46:59 +0200763 struct dwc3_request *req, dma_addr_t dma,
764 unsigned length, unsigned last, unsigned chain)
Felipe Balbic71fc372011-11-22 11:37:34 +0200765{
Felipe Balbieeb720f2011-11-28 12:46:59 +0200766 struct dwc3 *dwc = dep->dwc;
Felipe Balbif6bafc62012-02-06 11:04:53 +0200767 struct dwc3_trb *trb;
Felipe Balbic71fc372011-11-22 11:37:34 +0200768
769 unsigned int cur_slot;
770
Felipe Balbieeb720f2011-11-28 12:46:59 +0200771 dev_vdbg(dwc->dev, "%s: req %p dma %08llx length %d%s%s\n",
772 dep->name, req, (unsigned long long) dma,
773 length, last ? " last" : "",
774 chain ? " chain" : "");
775
Felipe Balbif6bafc62012-02-06 11:04:53 +0200776 trb = &dep->trb_pool[dep->free_slot & DWC3_TRB_MASK];
Felipe Balbic71fc372011-11-22 11:37:34 +0200777 cur_slot = dep->free_slot;
778 dep->free_slot++;
779
780 /* Skip the LINK-TRB on ISOC */
Vijayavardhan Vennapusa2a444ad2013-02-01 13:20:59 +0530781 if (((dep->free_slot & DWC3_TRB_MASK) == DWC3_TRB_NUM - 1) &&
Ido Shayevitz57cdac12012-03-12 20:25:24 +0200782 usb_endpoint_xfer_isoc(dep->endpoint.desc))
Vijayavardhan Vennapusa2a444ad2013-02-01 13:20:59 +0530783 dep->free_slot++;
Felipe Balbic71fc372011-11-22 11:37:34 +0200784
Felipe Balbieeb720f2011-11-28 12:46:59 +0200785 if (!req->trb) {
786 dwc3_gadget_move_request_queued(req);
Felipe Balbif6bafc62012-02-06 11:04:53 +0200787 req->trb = trb;
788 req->trb_dma = dwc3_trb_dma_offset(dep, trb);
Felipe Balbieeb720f2011-11-28 12:46:59 +0200789 }
Felipe Balbic71fc372011-11-22 11:37:34 +0200790
Felipe Balbif6bafc62012-02-06 11:04:53 +0200791 trb->size = DWC3_TRB_SIZE_LENGTH(length);
792 trb->bpl = lower_32_bits(dma);
793 trb->bph = upper_32_bits(dma);
Felipe Balbic71fc372011-11-22 11:37:34 +0200794
Ido Shayevitz57cdac12012-03-12 20:25:24 +0200795 switch (usb_endpoint_type(dep->endpoint.desc)) {
Felipe Balbic71fc372011-11-22 11:37:34 +0200796 case USB_ENDPOINT_XFER_CONTROL:
Felipe Balbif6bafc62012-02-06 11:04:53 +0200797 trb->ctrl = DWC3_TRBCTL_CONTROL_SETUP;
Felipe Balbic71fc372011-11-22 11:37:34 +0200798 break;
799
800 case USB_ENDPOINT_XFER_ISOC:
Felipe Balbif6bafc62012-02-06 11:04:53 +0200801 trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS_FIRST;
Felipe Balbic71fc372011-11-22 11:37:34 +0200802
Pratyush Ananddf023422012-05-21 12:42:54 +0530803 if (!req->request.no_interrupt)
Felipe Balbif6bafc62012-02-06 11:04:53 +0200804 trb->ctrl |= DWC3_TRB_CTRL_IOC;
Felipe Balbic71fc372011-11-22 11:37:34 +0200805 break;
806
807 case USB_ENDPOINT_XFER_BULK:
808 case USB_ENDPOINT_XFER_INT:
Felipe Balbif6bafc62012-02-06 11:04:53 +0200809 trb->ctrl = DWC3_TRBCTL_NORMAL;
Felipe Balbic71fc372011-11-22 11:37:34 +0200810 break;
811 default:
812 /*
813 * This is only possible with faulty memory because we
814 * checked it already :)
815 */
816 BUG();
817 }
818
Ido Shayevitz57cdac12012-03-12 20:25:24 +0200819 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbif6bafc62012-02-06 11:04:53 +0200820 trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI;
821 trb->ctrl |= DWC3_TRB_CTRL_CSP;
822 } else {
823 if (chain)
824 trb->ctrl |= DWC3_TRB_CTRL_CHN;
Felipe Balbic71fc372011-11-22 11:37:34 +0200825
Felipe Balbif6bafc62012-02-06 11:04:53 +0200826 if (last)
827 trb->ctrl |= DWC3_TRB_CTRL_LST;
828 }
829
Ido Shayevitz57cdac12012-03-12 20:25:24 +0200830 if (usb_endpoint_xfer_bulk(dep->endpoint.desc) && dep->stream_capable)
Felipe Balbif6bafc62012-02-06 11:04:53 +0200831 trb->ctrl |= DWC3_TRB_CTRL_SID_SOFN(req->request.stream_id);
832
833 trb->ctrl |= DWC3_TRB_CTRL_HWO;
Felipe Balbic71fc372011-11-22 11:37:34 +0200834}
835
Felipe Balbi72246da2011-08-19 18:10:58 +0300836/*
837 * dwc3_prepare_trbs - setup TRBs from requests
838 * @dep: endpoint for which requests are being prepared
839 * @starting: true if the endpoint is idle and no requests are queued.
840 *
Paul Zimmerman1d046792012-02-15 18:56:56 -0800841 * The function goes through the requests list and sets up TRBs for the
842 * transfers. The function returns once there are no more TRBs available or
843 * it runs out of requests.
Felipe Balbi72246da2011-08-19 18:10:58 +0300844 */
Felipe Balbi68e823e2011-11-28 12:25:01 +0200845static void dwc3_prepare_trbs(struct dwc3_ep *dep, bool starting)
Felipe Balbi72246da2011-08-19 18:10:58 +0300846{
Felipe Balbi68e823e2011-11-28 12:25:01 +0200847 struct dwc3_request *req, *n;
Felipe Balbi72246da2011-08-19 18:10:58 +0300848 u32 trbs_left;
Paul Zimmerman8d62cd62012-02-15 13:35:06 +0200849 u32 max;
Felipe Balbic71fc372011-11-22 11:37:34 +0200850 unsigned int last_one = 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300851
852 BUILD_BUG_ON_NOT_POWER_OF_2(DWC3_TRB_NUM);
853
854 /* the first request must not be queued */
855 trbs_left = (dep->busy_slot - dep->free_slot) & DWC3_TRB_MASK;
Felipe Balbic71fc372011-11-22 11:37:34 +0200856
Paul Zimmerman8d62cd62012-02-15 13:35:06 +0200857 /* Can't wrap around on a non-isoc EP since there's no link TRB */
Ido Shayevitz57cdac12012-03-12 20:25:24 +0200858 if (!usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Paul Zimmerman8d62cd62012-02-15 13:35:06 +0200859 max = DWC3_TRB_NUM - (dep->free_slot & DWC3_TRB_MASK);
860 if (trbs_left > max)
861 trbs_left = max;
862 }
863
Felipe Balbi72246da2011-08-19 18:10:58 +0300864 /*
Paul Zimmerman1d046792012-02-15 18:56:56 -0800865 * If busy & slot are equal than it is either full or empty. If we are
866 * starting to process requests then we are empty. Otherwise we are
Felipe Balbi72246da2011-08-19 18:10:58 +0300867 * full and don't do anything
868 */
869 if (!trbs_left) {
870 if (!starting)
Felipe Balbi68e823e2011-11-28 12:25:01 +0200871 return;
Felipe Balbi72246da2011-08-19 18:10:58 +0300872 trbs_left = DWC3_TRB_NUM;
873 /*
874 * In case we start from scratch, we queue the ISOC requests
875 * starting from slot 1. This is done because we use ring
876 * buffer and have no LST bit to stop us. Instead, we place
Paul Zimmerman1d046792012-02-15 18:56:56 -0800877 * IOC bit every TRB_NUM/4. We try to avoid having an interrupt
Felipe Balbi72246da2011-08-19 18:10:58 +0300878 * after the first request so we start at slot 1 and have
879 * 7 requests proceed before we hit the first IOC.
880 * Other transfer types don't use the ring buffer and are
881 * processed from the first TRB until the last one. Since we
882 * don't wrap around we have to start at the beginning.
883 */
Ido Shayevitz57cdac12012-03-12 20:25:24 +0200884 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbi72246da2011-08-19 18:10:58 +0300885 dep->busy_slot = 1;
886 dep->free_slot = 1;
887 } else {
888 dep->busy_slot = 0;
889 dep->free_slot = 0;
890 }
891 }
892
893 /* The last TRB is a link TRB, not used for xfer */
Ido Shayevitz57cdac12012-03-12 20:25:24 +0200894 if ((trbs_left <= 1) && usb_endpoint_xfer_isoc(dep->endpoint.desc))
Felipe Balbi68e823e2011-11-28 12:25:01 +0200895 return;
Felipe Balbi72246da2011-08-19 18:10:58 +0300896
897 list_for_each_entry_safe(req, n, &dep->request_list, list) {
Felipe Balbieeb720f2011-11-28 12:46:59 +0200898 unsigned length;
899 dma_addr_t dma;
Felipe Balbi72246da2011-08-19 18:10:58 +0300900
Felipe Balbieeb720f2011-11-28 12:46:59 +0200901 if (req->request.num_mapped_sgs > 0) {
902 struct usb_request *request = &req->request;
903 struct scatterlist *sg = request->sg;
904 struct scatterlist *s;
905 int i;
Felipe Balbi72246da2011-08-19 18:10:58 +0300906
Felipe Balbieeb720f2011-11-28 12:46:59 +0200907 for_each_sg(sg, s, request->num_mapped_sgs, i) {
908 unsigned chain = true;
Felipe Balbi72246da2011-08-19 18:10:58 +0300909
Felipe Balbieeb720f2011-11-28 12:46:59 +0200910 length = sg_dma_len(s);
911 dma = sg_dma_address(s);
Felipe Balbi72246da2011-08-19 18:10:58 +0300912
Paul Zimmerman1d046792012-02-15 18:56:56 -0800913 if (i == (request->num_mapped_sgs - 1) ||
914 sg_is_last(s)) {
Felipe Balbieeb720f2011-11-28 12:46:59 +0200915 last_one = true;
916 chain = false;
917 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300918
Felipe Balbieeb720f2011-11-28 12:46:59 +0200919 trbs_left--;
920 if (!trbs_left)
921 last_one = true;
Felipe Balbi72246da2011-08-19 18:10:58 +0300922
Felipe Balbieeb720f2011-11-28 12:46:59 +0200923 if (last_one)
924 chain = false;
Felipe Balbi72246da2011-08-19 18:10:58 +0300925
Felipe Balbieeb720f2011-11-28 12:46:59 +0200926 dwc3_prepare_one_trb(dep, req, dma, length,
927 last_one, chain);
Felipe Balbi72246da2011-08-19 18:10:58 +0300928
Felipe Balbieeb720f2011-11-28 12:46:59 +0200929 if (last_one)
930 break;
931 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300932 } else {
Felipe Balbieeb720f2011-11-28 12:46:59 +0200933 dma = req->request.dma;
934 length = req->request.length;
935 trbs_left--;
936
937 if (!trbs_left)
938 last_one = 1;
939
940 /* Is this the last request? */
941 if (list_is_last(&req->list, &dep->request_list))
942 last_one = 1;
943
944 dwc3_prepare_one_trb(dep, req, dma, length,
945 last_one, false);
946
947 if (last_one)
948 break;
Felipe Balbi72246da2011-08-19 18:10:58 +0300949 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300950 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300951}
952
953static int __dwc3_gadget_kick_transfer(struct dwc3_ep *dep, u16 cmd_param,
954 int start_new)
955{
956 struct dwc3_gadget_ep_cmd_params params;
Vijayavardhan Vennapusa91ba6532013-01-30 17:35:45 +0530957 struct dwc3_request *req, *req1, *n;
Felipe Balbi72246da2011-08-19 18:10:58 +0300958 struct dwc3 *dwc = dep->dwc;
959 int ret;
960 u32 cmd;
961
962 if (start_new && (dep->flags & DWC3_EP_BUSY)) {
963 dev_vdbg(dwc->dev, "%s: endpoint busy\n", dep->name);
964 return -EBUSY;
965 }
966 dep->flags &= ~DWC3_EP_PENDING_REQUEST;
967
968 /*
969 * If we are getting here after a short-out-packet we don't enqueue any
970 * new requests as we try to set the IOC bit only on the last request.
971 */
972 if (start_new) {
973 if (list_empty(&dep->req_queued))
974 dwc3_prepare_trbs(dep, start_new);
975
976 /* req points to the first request which will be sent */
977 req = next_request(&dep->req_queued);
978 } else {
Felipe Balbi68e823e2011-11-28 12:25:01 +0200979 dwc3_prepare_trbs(dep, start_new);
980
Felipe Balbi72246da2011-08-19 18:10:58 +0300981 /*
Paul Zimmerman1d046792012-02-15 18:56:56 -0800982 * req points to the first request where HWO changed from 0 to 1
Felipe Balbi72246da2011-08-19 18:10:58 +0300983 */
Felipe Balbi68e823e2011-11-28 12:25:01 +0200984 req = next_request(&dep->req_queued);
Felipe Balbi72246da2011-08-19 18:10:58 +0300985 }
986 if (!req) {
987 dep->flags |= DWC3_EP_PENDING_REQUEST;
988 return 0;
989 }
990
991 memset(&params, 0, sizeof(params));
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300992 params.param0 = upper_32_bits(req->trb_dma);
993 params.param1 = lower_32_bits(req->trb_dma);
Felipe Balbi72246da2011-08-19 18:10:58 +0300994
995 if (start_new)
996 cmd = DWC3_DEPCMD_STARTTRANSFER;
997 else
998 cmd = DWC3_DEPCMD_UPDATETRANSFER;
999
1000 cmd |= DWC3_DEPCMD_PARAM(cmd_param);
1001 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number, cmd, &params);
1002 if (ret < 0) {
1003 dev_dbg(dwc->dev, "failed to send STARTTRANSFER command\n");
1004
Vijayavardhan Vennapusa91ba6532013-01-30 17:35:45 +05301005 if ((ret == -EAGAIN) && start_new &&
1006 usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
1007 /* If bit13 in Command complete event is set, software
1008 * must issue ENDTRANDFER command and wait for
1009 * Xfernotready event to queue the requests again.
1010 */
1011 if (!dep->resource_index) {
1012 dep->resource_index =
1013 dwc3_gadget_ep_get_transfer_index(dwc,
1014 dep->number);
1015 WARN_ON_ONCE(!dep->resource_index);
1016 }
1017 dwc3_stop_active_transfer(dwc, dep->number);
1018 list_for_each_entry_safe_reverse(req1, n,
1019 &dep->req_queued, list) {
1020 req1->trb = NULL;
1021 dwc3_gadget_move_request_list_front(req1);
1022 if (req->request.num_mapped_sgs)
1023 dep->busy_slot +=
1024 req->request.num_mapped_sgs;
1025 else
1026 dep->busy_slot++;
1027 if ((dep->busy_slot & DWC3_TRB_MASK) ==
1028 DWC3_TRB_NUM - 1)
1029 dep->busy_slot++;
1030 }
1031 return ret;
1032 } else {
1033 /*
1034 * FIXME we need to iterate over the list of requests
1035 * here and stop, unmap, free and del each of the linked
1036 * requests instead of what we do now.
1037 */
1038 usb_gadget_unmap_request(&dwc->gadget, &req->request,
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02001039 req->direction);
Vijayavardhan Vennapusa91ba6532013-01-30 17:35:45 +05301040 list_del(&req->list);
1041 return ret;
1042 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001043 }
1044
1045 dep->flags |= DWC3_EP_BUSY;
Felipe Balbi25b8ff62011-11-04 12:32:47 +02001046
Paul Zimmermanf39a37f2012-03-29 18:16:54 +00001047 if (start_new) {
Felipe Balbi4959cfc2012-06-06 12:04:13 +03001048 dep->resource_index = dwc3_gadget_ep_get_transfer_index(dwc,
Paul Zimmermanf39a37f2012-03-29 18:16:54 +00001049 dep->number);
Felipe Balbi4959cfc2012-06-06 12:04:13 +03001050 WARN_ON_ONCE(!dep->resource_index);
Paul Zimmermanf39a37f2012-03-29 18:16:54 +00001051 }
Felipe Balbi25b8ff62011-11-04 12:32:47 +02001052
Felipe Balbi72246da2011-08-19 18:10:58 +03001053 return 0;
1054}
1055
Pratyush Anand73939b02012-05-25 18:54:56 +05301056static void __dwc3_gadget_start_isoc(struct dwc3 *dwc,
1057 struct dwc3_ep *dep, u32 cur_uf)
1058{
1059 u32 uf;
1060
Vijayavardhan Vennapusa91ba6532013-01-30 17:35:45 +05301061 dep->current_uf = cur_uf;
1062
Pratyush Anand73939b02012-05-25 18:54:56 +05301063 if (list_empty(&dep->request_list)) {
1064 dev_vdbg(dwc->dev, "ISOC ep %s run out for requests.\n",
1065 dep->name);
Pratyush Anandac417602012-08-30 12:21:43 +05301066 dep->flags |= DWC3_EP_PENDING_REQUEST;
Pratyush Anand73939b02012-05-25 18:54:56 +05301067 return;
1068 }
1069
1070 /* 4 micro frames in the future */
1071 uf = cur_uf + dep->interval * 4;
1072
1073 __dwc3_gadget_kick_transfer(dep, uf, 1);
1074}
1075
1076static void dwc3_gadget_start_isoc(struct dwc3 *dwc,
1077 struct dwc3_ep *dep, const struct dwc3_event_depevt *event)
1078{
1079 u32 cur_uf, mask;
1080
1081 mask = ~(dep->interval - 1);
1082 cur_uf = event->parameters & mask;
1083
1084 __dwc3_gadget_start_isoc(dwc, dep, cur_uf);
1085}
1086
Felipe Balbi72246da2011-08-19 18:10:58 +03001087static int __dwc3_gadget_ep_queue(struct dwc3_ep *dep, struct dwc3_request *req)
1088{
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02001089 struct dwc3 *dwc = dep->dwc;
1090 int ret;
1091
Manu Gautamd2b99e12013-02-11 15:53:34 +05301092 if (req->request.status == -EINPROGRESS) {
1093 ret = -EBUSY;
1094 dev_err(dwc->dev, "%s: %p request already in queue",
1095 dep->name, req);
1096 return ret;
1097 }
1098
Felipe Balbi72246da2011-08-19 18:10:58 +03001099 req->request.actual = 0;
1100 req->request.status = -EINPROGRESS;
1101 req->direction = dep->direction;
1102 req->epnum = dep->number;
1103
1104 /*
1105 * We only add to our list of requests now and
1106 * start consuming the list once we get XferNotReady
1107 * IRQ.
1108 *
1109 * That way, we avoid doing anything that we don't need
1110 * to do now and defer it until the point we receive a
1111 * particular token from the Host side.
1112 *
1113 * This will also avoid Host cancelling URBs due to too
Paul Zimmerman1d046792012-02-15 18:56:56 -08001114 * many NAKs.
Felipe Balbi72246da2011-08-19 18:10:58 +03001115 */
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02001116 ret = usb_gadget_map_request(&dwc->gadget, &req->request,
1117 dep->direction);
1118 if (ret)
1119 return ret;
1120
Felipe Balbi72246da2011-08-19 18:10:58 +03001121 list_add_tail(&req->list, &dep->request_list);
1122
1123 /*
Felipe Balbi46485a02012-06-06 12:00:50 +03001124 * There are a few special cases:
Felipe Balbi72246da2011-08-19 18:10:58 +03001125 *
Paul Zimmermanf39a37f2012-03-29 18:16:54 +00001126 * 1. XferNotReady with empty list of requests. We need to kick the
1127 * transfer here in that situation, otherwise we will be NAKing
1128 * forever. If we get XferNotReady before gadget driver has a
1129 * chance to queue a request, we will ACK the IRQ but won't be
1130 * able to receive the data until the next request is queued.
1131 * The following code is handling exactly that.
1132 *
Felipe Balbi72246da2011-08-19 18:10:58 +03001133 */
1134 if (dep->flags & DWC3_EP_PENDING_REQUEST) {
Pratyush Anandac417602012-08-30 12:21:43 +05301135 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001136
Pratyush Anandac417602012-08-30 12:21:43 +05301137 /*
1138 * If xfernotready is already elapsed and it is a case
1139 * of isoc transfer, then issue END TRANSFER, so that
1140 * you can receive xfernotready again and can have
1141 * notion of current microframe.
1142 */
1143 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Vijayavardhan Vennapusa91ba6532013-01-30 17:35:45 +05301144 /* If xfernotready event is recieved before issuing
1145 * START TRANSFER command, don't issue END TRANSFER.
1146 * Rather start queueing the requests by issuing START
1147 * TRANSFER command.
1148 */
1149 if (list_empty(&dep->req_queued) && dep->resource_index)
Pratyush Anand18bbcb02013-01-14 15:59:34 +05301150 dwc3_stop_active_transfer(dwc, dep->number);
Vijayavardhan Vennapusa91ba6532013-01-30 17:35:45 +05301151 else
1152 __dwc3_gadget_start_isoc(dwc, dep,
1153 dep->current_uf);
1154 dep->flags &= ~DWC3_EP_PENDING_REQUEST;
Pratyush Anandac417602012-08-30 12:21:43 +05301155 return 0;
1156 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001157
Felipe Balbi46485a02012-06-06 12:00:50 +03001158 ret = __dwc3_gadget_kick_transfer(dep, 0, true);
Moiz Sonasatheed03f12012-08-01 14:08:30 -05001159 if (ret && ret != -EBUSY)
Felipe Balbi72246da2011-08-19 18:10:58 +03001160 dev_dbg(dwc->dev, "%s: failed to kick transfers\n",
1161 dep->name);
Felipe Balbi5d409eb2012-05-22 10:24:11 +03001162 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001163
Felipe Balbi46485a02012-06-06 12:00:50 +03001164 /*
1165 * 2. XferInProgress on Isoc EP with an active transfer. We need to
1166 * kick the transfer here after queuing a request, otherwise the
1167 * core may not see the modified TRB(s).
1168 */
1169 if (usb_endpoint_xfer_isoc(dep->endpoint.desc) &&
Pratyush Anand053d3e52012-08-07 16:54:18 +05301170 (dep->flags & DWC3_EP_BUSY) &&
1171 !(dep->flags & DWC3_EP_MISSED_ISOC)) {
Felipe Balbi4959cfc2012-06-06 12:04:13 +03001172 WARN_ON_ONCE(!dep->resource_index);
1173 ret = __dwc3_gadget_kick_transfer(dep, dep->resource_index,
Felipe Balbi46485a02012-06-06 12:00:50 +03001174 false);
Moiz Sonasatheed03f12012-08-01 14:08:30 -05001175 if (ret && ret != -EBUSY)
Felipe Balbi46485a02012-06-06 12:00:50 +03001176 dev_dbg(dwc->dev, "%s: failed to kick transfers\n",
1177 dep->name);
Felipe Balbi46485a02012-06-06 12:00:50 +03001178 }
1179
Felipe Balbi72246da2011-08-19 18:10:58 +03001180 return 0;
1181}
1182
1183static int dwc3_gadget_ep_queue(struct usb_ep *ep, struct usb_request *request,
1184 gfp_t gfp_flags)
1185{
1186 struct dwc3_request *req = to_dwc3_request(request);
1187 struct dwc3_ep *dep = to_dwc3_ep(ep);
1188 struct dwc3 *dwc = dep->dwc;
1189
1190 unsigned long flags;
1191
1192 int ret;
1193
Ido Shayevitz57cdac12012-03-12 20:25:24 +02001194 if (!dep->endpoint.desc) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001195 dev_dbg(dwc->dev, "trying to queue request %p to disabled %s\n",
1196 request, ep->name);
1197 return -ESHUTDOWN;
1198 }
1199
1200 dev_vdbg(dwc->dev, "queing request %p to %s length %d\n",
1201 request, ep->name, request->length);
1202
Manu Gautam1c4dbcb2012-10-05 13:16:00 +05301203 WARN(!dep->direction && (request->length % ep->desc->wMaxPacketSize),
1204 "trying to queue unaligned request (%d)\n", request->length);
1205
Felipe Balbi72246da2011-08-19 18:10:58 +03001206 spin_lock_irqsave(&dwc->lock, flags);
1207 ret = __dwc3_gadget_ep_queue(dep, req);
1208 spin_unlock_irqrestore(&dwc->lock, flags);
1209
1210 return ret;
1211}
1212
1213static int dwc3_gadget_ep_dequeue(struct usb_ep *ep,
1214 struct usb_request *request)
1215{
1216 struct dwc3_request *req = to_dwc3_request(request);
1217 struct dwc3_request *r = NULL;
1218
1219 struct dwc3_ep *dep = to_dwc3_ep(ep);
1220 struct dwc3 *dwc = dep->dwc;
1221
1222 unsigned long flags;
1223 int ret = 0;
1224
1225 spin_lock_irqsave(&dwc->lock, flags);
1226
1227 list_for_each_entry(r, &dep->request_list, list) {
1228 if (r == req)
1229 break;
1230 }
1231
1232 if (r != req) {
1233 list_for_each_entry(r, &dep->req_queued, list) {
1234 if (r == req)
1235 break;
1236 }
1237 if (r == req) {
1238 /* wait until it is processed */
1239 dwc3_stop_active_transfer(dwc, dep->number);
Pratyush Anandeaec3e92012-06-15 11:54:00 +05301240 goto out1;
Felipe Balbi72246da2011-08-19 18:10:58 +03001241 }
1242 dev_err(dwc->dev, "request %p was not queued to %s\n",
1243 request, ep->name);
1244 ret = -EINVAL;
1245 goto out0;
1246 }
1247
Pratyush Anandeaec3e92012-06-15 11:54:00 +05301248out1:
Felipe Balbi72246da2011-08-19 18:10:58 +03001249 /* giveback the request */
1250 dwc3_gadget_giveback(dep, req, -ECONNRESET);
1251
1252out0:
1253 spin_unlock_irqrestore(&dwc->lock, flags);
1254
1255 return ret;
1256}
1257
1258int __dwc3_gadget_ep_set_halt(struct dwc3_ep *dep, int value)
1259{
1260 struct dwc3_gadget_ep_cmd_params params;
1261 struct dwc3 *dwc = dep->dwc;
1262 int ret;
1263
1264 memset(&params, 0x00, sizeof(params));
1265
1266 if (value) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001267 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number,
1268 DWC3_DEPCMD_SETSTALL, &params);
1269 if (ret)
1270 dev_err(dwc->dev, "failed to %s STALL on %s\n",
1271 value ? "set" : "clear",
1272 dep->name);
1273 else
1274 dep->flags |= DWC3_EP_STALL;
1275 } else {
1276 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number,
1277 DWC3_DEPCMD_CLEARSTALL, &params);
1278 if (ret)
1279 dev_err(dwc->dev, "failed to %s STALL on %s\n",
1280 value ? "set" : "clear",
1281 dep->name);
1282 else
Vijayavardhan Vennapusa6008e262012-10-19 15:57:56 +05301283 dep->flags &= ~(DWC3_EP_STALL | DWC3_EP_WEDGE);
Felipe Balbi72246da2011-08-19 18:10:58 +03001284 }
Paul Zimmerman52754552011-09-30 10:58:44 +03001285
Felipe Balbi72246da2011-08-19 18:10:58 +03001286 return ret;
1287}
1288
1289static int dwc3_gadget_ep_set_halt(struct usb_ep *ep, int value)
1290{
1291 struct dwc3_ep *dep = to_dwc3_ep(ep);
1292 struct dwc3 *dwc = dep->dwc;
1293
1294 unsigned long flags;
1295
1296 int ret;
1297
1298 spin_lock_irqsave(&dwc->lock, flags);
1299
Ido Shayevitz57cdac12012-03-12 20:25:24 +02001300 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001301 dev_err(dwc->dev, "%s is of Isochronous type\n", dep->name);
1302 ret = -EINVAL;
1303 goto out;
1304 }
1305
1306 ret = __dwc3_gadget_ep_set_halt(dep, value);
1307out:
1308 spin_unlock_irqrestore(&dwc->lock, flags);
1309
1310 return ret;
1311}
1312
1313static int dwc3_gadget_ep_set_wedge(struct usb_ep *ep)
1314{
1315 struct dwc3_ep *dep = to_dwc3_ep(ep);
Paul Zimmerman249a4562012-02-24 17:32:16 -08001316 struct dwc3 *dwc = dep->dwc;
1317 unsigned long flags;
Felipe Balbi72246da2011-08-19 18:10:58 +03001318
Paul Zimmerman249a4562012-02-24 17:32:16 -08001319 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001320 dep->flags |= DWC3_EP_WEDGE;
Paul Zimmerman249a4562012-02-24 17:32:16 -08001321 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001322
Pratyush Anandeb840752012-06-25 22:40:43 +05301323 if (dep->number == 0 || dep->number == 1)
1324 return dwc3_gadget_ep0_set_halt(ep, 1);
1325 else
1326 return dwc3_gadget_ep_set_halt(ep, 1);
Felipe Balbi72246da2011-08-19 18:10:58 +03001327}
1328
1329/* -------------------------------------------------------------------------- */
1330
1331static struct usb_endpoint_descriptor dwc3_gadget_ep0_desc = {
1332 .bLength = USB_DT_ENDPOINT_SIZE,
1333 .bDescriptorType = USB_DT_ENDPOINT,
1334 .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
1335};
1336
1337static const struct usb_ep_ops dwc3_gadget_ep0_ops = {
1338 .enable = dwc3_gadget_ep0_enable,
1339 .disable = dwc3_gadget_ep0_disable,
1340 .alloc_request = dwc3_gadget_ep_alloc_request,
1341 .free_request = dwc3_gadget_ep_free_request,
1342 .queue = dwc3_gadget_ep0_queue,
1343 .dequeue = dwc3_gadget_ep_dequeue,
Pratyush Anandeb840752012-06-25 22:40:43 +05301344 .set_halt = dwc3_gadget_ep0_set_halt,
Felipe Balbi72246da2011-08-19 18:10:58 +03001345 .set_wedge = dwc3_gadget_ep_set_wedge,
1346};
1347
1348static const struct usb_ep_ops dwc3_gadget_ep_ops = {
1349 .enable = dwc3_gadget_ep_enable,
1350 .disable = dwc3_gadget_ep_disable,
1351 .alloc_request = dwc3_gadget_ep_alloc_request,
1352 .free_request = dwc3_gadget_ep_free_request,
1353 .queue = dwc3_gadget_ep_queue,
1354 .dequeue = dwc3_gadget_ep_dequeue,
1355 .set_halt = dwc3_gadget_ep_set_halt,
1356 .set_wedge = dwc3_gadget_ep_set_wedge,
1357};
1358
1359/* -------------------------------------------------------------------------- */
1360
1361static int dwc3_gadget_get_frame(struct usb_gadget *g)
1362{
1363 struct dwc3 *dwc = gadget_to_dwc(g);
1364 u32 reg;
1365
1366 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1367 return DWC3_DSTS_SOFFN(reg);
1368}
1369
1370static int dwc3_gadget_wakeup(struct usb_gadget *g)
1371{
1372 struct dwc3 *dwc = gadget_to_dwc(g);
1373
1374 unsigned long timeout;
1375 unsigned long flags;
1376
1377 u32 reg;
1378
1379 int ret = 0;
1380
1381 u8 link_state;
1382 u8 speed;
1383
1384 spin_lock_irqsave(&dwc->lock, flags);
1385
1386 /*
1387 * According to the Databook Remote wakeup request should
1388 * be issued only when the device is in early suspend state.
1389 *
1390 * We can check that via USB Link State bits in DSTS register.
1391 */
1392 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1393
1394 speed = reg & DWC3_DSTS_CONNECTSPD;
1395 if (speed == DWC3_DSTS_SUPERSPEED) {
1396 dev_dbg(dwc->dev, "no wakeup on SuperSpeed\n");
1397 ret = -EINVAL;
1398 goto out;
1399 }
1400
1401 link_state = DWC3_DSTS_USBLNKST(reg);
1402
1403 switch (link_state) {
1404 case DWC3_LINK_STATE_RX_DET: /* in HS, means Early Suspend */
1405 case DWC3_LINK_STATE_U3: /* in HS, means SUSPEND */
1406 break;
1407 default:
1408 dev_dbg(dwc->dev, "can't wakeup from link state %d\n",
1409 link_state);
1410 ret = -EINVAL;
1411 goto out;
1412 }
1413
Felipe Balbi8598bde2012-01-02 18:55:57 +02001414 ret = dwc3_gadget_set_link_state(dwc, DWC3_LINK_STATE_RECOV);
1415 if (ret < 0) {
1416 dev_err(dwc->dev, "failed to put link in Recovery\n");
1417 goto out;
1418 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001419
Paul Zimmerman88df4272012-04-27 13:10:52 +03001420 /* Recent versions do this automatically */
1421 if (dwc->revision < DWC3_REVISION_194A) {
1422 /* write zeroes to Link Change Request */
Felipe Balbib4d04352012-05-24 10:27:56 +03001423 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
Paul Zimmerman88df4272012-04-27 13:10:52 +03001424 reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK;
1425 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1426 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001427
Paul Zimmerman1d046792012-02-15 18:56:56 -08001428 /* poll until Link State changes to ON */
Felipe Balbi72246da2011-08-19 18:10:58 +03001429 timeout = jiffies + msecs_to_jiffies(100);
1430
Paul Zimmerman1d046792012-02-15 18:56:56 -08001431 while (!time_after(jiffies, timeout)) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001432 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1433
1434 /* in HS, means ON */
1435 if (DWC3_DSTS_USBLNKST(reg) == DWC3_LINK_STATE_U0)
1436 break;
1437 }
1438
1439 if (DWC3_DSTS_USBLNKST(reg) != DWC3_LINK_STATE_U0) {
1440 dev_err(dwc->dev, "failed to send remote wakeup\n");
1441 ret = -EINVAL;
1442 }
1443
1444out:
1445 spin_unlock_irqrestore(&dwc->lock, flags);
1446
1447 return ret;
1448}
1449
1450static int dwc3_gadget_set_selfpowered(struct usb_gadget *g,
1451 int is_selfpowered)
1452{
1453 struct dwc3 *dwc = gadget_to_dwc(g);
Paul Zimmerman249a4562012-02-24 17:32:16 -08001454 unsigned long flags;
Felipe Balbi72246da2011-08-19 18:10:58 +03001455
Paul Zimmerman249a4562012-02-24 17:32:16 -08001456 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001457 dwc->is_selfpowered = !!is_selfpowered;
Paul Zimmerman249a4562012-02-24 17:32:16 -08001458 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001459
1460 return 0;
1461}
1462
Pratyush Anand77473f72012-07-02 10:21:55 +05301463static int dwc3_gadget_run_stop(struct dwc3 *dwc, int is_on)
Felipe Balbi72246da2011-08-19 18:10:58 +03001464{
1465 u32 reg;
Sebastian Andrzej Siewior61d58242011-08-29 16:46:38 +02001466 u32 timeout = 500;
Felipe Balbi72246da2011-08-19 18:10:58 +03001467
1468 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
Felipe Balbi8db7ed12012-01-18 18:32:29 +02001469 if (is_on) {
Paul Zimmerman88df4272012-04-27 13:10:52 +03001470 if (dwc->revision <= DWC3_REVISION_187A) {
1471 reg &= ~DWC3_DCTL_TRGTULST_MASK;
1472 reg |= DWC3_DCTL_TRGTULST_RX_DET;
1473 }
1474
1475 if (dwc->revision >= DWC3_REVISION_194A)
1476 reg &= ~DWC3_DCTL_KEEP_CONNECT;
1477 reg |= DWC3_DCTL_RUN_STOP;
Felipe Balbi8db7ed12012-01-18 18:32:29 +02001478 } else {
Felipe Balbi72246da2011-08-19 18:10:58 +03001479 reg &= ~DWC3_DCTL_RUN_STOP;
Felipe Balbi8db7ed12012-01-18 18:32:29 +02001480 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001481
1482 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1483
1484 do {
1485 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1486 if (is_on) {
1487 if (!(reg & DWC3_DSTS_DEVCTRLHLT))
1488 break;
1489 } else {
1490 if (reg & DWC3_DSTS_DEVCTRLHLT)
1491 break;
1492 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001493 timeout--;
1494 if (!timeout)
Pratyush Anand77473f72012-07-02 10:21:55 +05301495 return -ETIMEDOUT;
Sebastian Andrzej Siewior61d58242011-08-29 16:46:38 +02001496 udelay(1);
Felipe Balbi72246da2011-08-19 18:10:58 +03001497 } while (1);
1498
1499 dev_vdbg(dwc->dev, "gadget %s data soft-%s\n",
1500 dwc->gadget_driver
1501 ? dwc->gadget_driver->function : "no-function",
1502 is_on ? "connect" : "disconnect");
Pratyush Anand77473f72012-07-02 10:21:55 +05301503
1504 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +03001505}
1506
Vijayavardhan Vennapusa908f1ed2012-10-19 19:51:48 +05301507static int dwc3_gadget_vbus_draw(struct usb_gadget *g, unsigned mA)
1508{
1509 struct dwc3 *dwc = gadget_to_dwc(g);
1510 struct dwc3_otg *dotg = dwc->dotg;
1511
1512 if (dotg && dotg->otg.phy)
1513 return usb_phy_set_power(dotg->otg.phy, mA);
1514
1515 return -ENOTSUPP;
1516}
1517
Felipe Balbi72246da2011-08-19 18:10:58 +03001518static int dwc3_gadget_pullup(struct usb_gadget *g, int is_on)
1519{
1520 struct dwc3 *dwc = gadget_to_dwc(g);
1521 unsigned long flags;
Pratyush Anand77473f72012-07-02 10:21:55 +05301522 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001523
1524 is_on = !!is_on;
1525
1526 spin_lock_irqsave(&dwc->lock, flags);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +02001527
1528 dwc->softconnect = is_on;
1529
1530 if ((dwc->dotg && !dwc->vbus_active) ||
1531 !dwc->gadget_driver) {
1532
1533 spin_unlock_irqrestore(&dwc->lock, flags);
1534
1535 /*
1536 * Need to wait for vbus_session(on) from otg driver or to
1537 * the udc_start.
1538 */
1539 return 0;
1540 }
1541
Pratyush Anand77473f72012-07-02 10:21:55 +05301542 ret = dwc3_gadget_run_stop(dwc, is_on);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +02001543
1544 spin_unlock_irqrestore(&dwc->lock, flags);
1545
Pratyush Anand77473f72012-07-02 10:21:55 +05301546 return ret;
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +02001547}
1548
1549static int dwc3_gadget_vbus_session(struct usb_gadget *_gadget, int is_active)
1550{
1551 struct dwc3 *dwc = gadget_to_dwc(_gadget);
1552 unsigned long flags;
Vijayavardhan Vennapusa8ec31d22012-10-23 08:44:48 +05301553 int ret = 0;
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +02001554
1555 if (!dwc->dotg)
1556 return -EPERM;
1557
1558 is_active = !!is_active;
1559
1560 spin_lock_irqsave(&dwc->lock, flags);
1561
1562 /* Mark that the vbus was powered */
1563 dwc->vbus_active = is_active;
1564
1565 /*
1566 * Check if upper level usb_gadget_driver was already registerd with
1567 * this udc controller driver (if dwc3_gadget_start was called)
1568 */
1569 if (dwc->gadget_driver && dwc->softconnect) {
1570 if (dwc->vbus_active) {
1571 /*
1572 * Both vbus was activated by otg and pullup was
1573 * signaled by the gadget driver.
1574 */
Pratyush Anand77473f72012-07-02 10:21:55 +05301575 ret = dwc3_gadget_run_stop(dwc, 1);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +02001576 } else {
Pratyush Anand77473f72012-07-02 10:21:55 +05301577 ret = dwc3_gadget_run_stop(dwc, 0);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +02001578 }
Vijayavardhan Vennapusab666fb82012-11-08 16:02:51 +05301579 } else if (dwc->gadget_driver && !dwc->softconnect &&
1580 !dwc->vbus_active) {
1581 if (dwc->gadget_driver->disconnect) {
1582 spin_unlock_irqrestore(&dwc->lock, flags);
1583 dwc->gadget_driver->disconnect(&dwc->gadget);
1584 return 0;
1585 }
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +02001586 }
1587
Felipe Balbi72246da2011-08-19 18:10:58 +03001588 spin_unlock_irqrestore(&dwc->lock, flags);
1589
Pratyush Anand77473f72012-07-02 10:21:55 +05301590 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001591}
1592
Manu Gautamf1fceddf2012-10-12 14:02:50 +05301593/* Required gadget re-initialization before switching to gadget in OTG mode */
1594void dwc3_gadget_restart(struct dwc3 *dwc)
1595{
1596 struct dwc3_ep *dep;
1597 int ret = 0;
Vijayavardhan Vennapusab7434562012-12-12 16:48:49 +05301598 u32 reg;
Manu Gautamf1fceddf2012-10-12 14:02:50 +05301599
Vijayavardhan Vennapusab7434562012-12-12 16:48:49 +05301600 /* Enable all but Start and End of Frame IRQs */
1601 reg = (DWC3_DEVTEN_EVNTOVERFLOWEN |
1602 DWC3_DEVTEN_CMDCMPLTEN |
1603 DWC3_DEVTEN_ERRTICERREN |
1604 DWC3_DEVTEN_WKUPEVTEN |
1605 DWC3_DEVTEN_ULSTCNGEN |
1606 DWC3_DEVTEN_CONNECTDONEEN |
1607 DWC3_DEVTEN_USBRSTEN |
1608 DWC3_DEVTEN_DISCONNEVTEN);
1609 dwc3_writel(dwc->regs, DWC3_DEVTEN, reg);
1610
1611 /* Enable USB2 LPM and automatic phy suspend only on recent versions */
1612 if (dwc->revision >= DWC3_REVISION_194A) {
1613 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
1614 reg |= DWC3_DCFG_LPM_CAP;
1615 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
1616
1617 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
1618 reg &= ~(DWC3_DCTL_HIRD_THRES_MASK | DWC3_DCTL_L1_HIBER_EN);
1619
1620 /* TODO: This should be configurable */
1621 reg |= DWC3_DCTL_HIRD_THRES(28);
1622
1623 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1624 }
1625
1626 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
1627 reg &= ~(DWC3_DCFG_SPEED_MASK);
1628
1629 /**
1630 * WORKAROUND: DWC3 revision < 2.20a have an issue
1631 * which would cause metastability state on Run/Stop
1632 * bit if we try to force the IP to USB2-only mode.
1633 *
1634 * Because of that, we cannot configure the IP to any
1635 * speed other than the SuperSpeed
1636 *
1637 * Refers to:
1638 *
1639 * STAR#9000525659: Clock Domain Crossing on DCTL in
1640 * USB 2.0 Mode
1641 */
1642 if (dwc->revision < DWC3_REVISION_220A)
1643 reg |= DWC3_DCFG_SUPERSPEED;
1644 else
1645 reg |= dwc->maximum_speed;
1646 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
1647
1648 dwc->start_config_issued = false;
1649
1650 /* Start with SuperSpeed Default */
1651 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
Manu Gautamf1fceddf2012-10-12 14:02:50 +05301652
1653 dwc->delayed_status = false;
Vijayavardhan Vennapusab7434562012-12-12 16:48:49 +05301654 /* reinitialize physical ep0-1 */
Manu Gautamf1fceddf2012-10-12 14:02:50 +05301655 dep = dwc->eps[0];
1656 dep->flags = 0;
1657 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, false);
1658 if (ret) {
1659 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
1660 return;
1661 }
1662
1663 dep = dwc->eps[1];
1664 dep->flags = 0;
1665 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, false);
1666 if (ret) {
1667 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
1668 return;
1669 }
1670
1671 /* begin to receive SETUP packets */
1672 dwc->ep0state = EP0_SETUP_PHASE;
1673 dwc3_ep0_out_start(dwc);
1674}
1675
Felipe Balbi72246da2011-08-19 18:10:58 +03001676static int dwc3_gadget_start(struct usb_gadget *g,
1677 struct usb_gadget_driver *driver)
1678{
1679 struct dwc3 *dwc = gadget_to_dwc(g);
1680 struct dwc3_ep *dep;
1681 unsigned long flags;
1682 int ret = 0;
1683 u32 reg;
1684
1685 spin_lock_irqsave(&dwc->lock, flags);
1686
1687 if (dwc->gadget_driver) {
1688 dev_err(dwc->dev, "%s is already bound to %s\n",
1689 dwc->gadget.name,
1690 dwc->gadget_driver->driver.name);
1691 ret = -EBUSY;
1692 goto err0;
1693 }
1694
1695 dwc->gadget_driver = driver;
1696 dwc->gadget.dev.driver = &driver->driver;
1697
Felipe Balbi72246da2011-08-19 18:10:58 +03001698 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
1699 reg &= ~(DWC3_DCFG_SPEED_MASK);
Felipe Balbi38d2c6c2012-03-23 12:20:31 +02001700
1701 /**
1702 * WORKAROUND: DWC3 revision < 2.20a have an issue
1703 * which would cause metastability state on Run/Stop
1704 * bit if we try to force the IP to USB2-only mode.
1705 *
1706 * Because of that, we cannot configure the IP to any
1707 * speed other than the SuperSpeed
1708 *
1709 * Refers to:
1710 *
1711 * STAR#9000525659: Clock Domain Crossing on DCTL in
1712 * USB 2.0 Mode
1713 */
1714 if (dwc->revision < DWC3_REVISION_220A)
1715 reg |= DWC3_DCFG_SUPERSPEED;
1716 else
1717 reg |= dwc->maximum_speed;
Felipe Balbi72246da2011-08-19 18:10:58 +03001718 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
1719
Paul Zimmermanb23c8432011-09-30 10:58:42 +03001720 dwc->start_config_issued = false;
1721
Felipe Balbi72246da2011-08-19 18:10:58 +03001722 /* Start with SuperSpeed Default */
1723 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
1724
1725 dep = dwc->eps[0];
Felipe Balbi07e0ee82012-07-16 14:08:16 +03001726 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, false);
Felipe Balbi72246da2011-08-19 18:10:58 +03001727 if (ret) {
1728 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
1729 goto err0;
1730 }
1731
1732 dep = dwc->eps[1];
Felipe Balbi07e0ee82012-07-16 14:08:16 +03001733 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, false);
Felipe Balbi72246da2011-08-19 18:10:58 +03001734 if (ret) {
1735 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
1736 goto err1;
1737 }
1738
1739 /* begin to receive SETUP packets */
Felipe Balbic7fcdeb2011-08-27 22:28:36 +03001740 dwc->ep0state = EP0_SETUP_PHASE;
Felipe Balbi72246da2011-08-19 18:10:58 +03001741 dwc3_ep0_out_start(dwc);
1742
1743 spin_unlock_irqrestore(&dwc->lock, flags);
1744
1745 return 0;
1746
1747err1:
1748 __dwc3_gadget_ep_disable(dwc->eps[0]);
1749
1750err0:
1751 spin_unlock_irqrestore(&dwc->lock, flags);
1752
1753 return ret;
1754}
1755
1756static int dwc3_gadget_stop(struct usb_gadget *g,
1757 struct usb_gadget_driver *driver)
1758{
1759 struct dwc3 *dwc = gadget_to_dwc(g);
1760 unsigned long flags;
1761
1762 spin_lock_irqsave(&dwc->lock, flags);
1763
1764 __dwc3_gadget_ep_disable(dwc->eps[0]);
1765 __dwc3_gadget_ep_disable(dwc->eps[1]);
1766
1767 dwc->gadget_driver = NULL;
1768 dwc->gadget.dev.driver = NULL;
1769
1770 spin_unlock_irqrestore(&dwc->lock, flags);
1771
1772 return 0;
1773}
Paul Zimmerman88df4272012-04-27 13:10:52 +03001774
Felipe Balbi72246da2011-08-19 18:10:58 +03001775static const struct usb_gadget_ops dwc3_gadget_ops = {
1776 .get_frame = dwc3_gadget_get_frame,
1777 .wakeup = dwc3_gadget_wakeup,
1778 .set_selfpowered = dwc3_gadget_set_selfpowered,
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +02001779 .vbus_session = dwc3_gadget_vbus_session,
Vijayavardhan Vennapusa908f1ed2012-10-19 19:51:48 +05301780 .vbus_draw = dwc3_gadget_vbus_draw,
Felipe Balbi72246da2011-08-19 18:10:58 +03001781 .pullup = dwc3_gadget_pullup,
1782 .udc_start = dwc3_gadget_start,
1783 .udc_stop = dwc3_gadget_stop,
1784};
1785
1786/* -------------------------------------------------------------------------- */
1787
1788static int __devinit dwc3_gadget_init_endpoints(struct dwc3 *dwc)
1789{
1790 struct dwc3_ep *dep;
1791 u8 epnum;
1792
1793 INIT_LIST_HEAD(&dwc->gadget.ep_list);
1794
1795 for (epnum = 0; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
1796 dep = kzalloc(sizeof(*dep), GFP_KERNEL);
1797 if (!dep) {
1798 dev_err(dwc->dev, "can't allocate endpoint %d\n",
1799 epnum);
1800 return -ENOMEM;
1801 }
1802
1803 dep->dwc = dwc;
1804 dep->number = epnum;
1805 dwc->eps[epnum] = dep;
1806
1807 snprintf(dep->name, sizeof(dep->name), "ep%d%s", epnum >> 1,
1808 (epnum & 1) ? "in" : "out");
1809 dep->endpoint.name = dep->name;
1810 dep->direction = (epnum & 1);
1811
1812 if (epnum == 0 || epnum == 1) {
1813 dep->endpoint.maxpacket = 512;
1814 dep->endpoint.ops = &dwc3_gadget_ep0_ops;
1815 if (!epnum)
1816 dwc->gadget.ep0 = &dep->endpoint;
1817 } else {
1818 int ret;
1819
1820 dep->endpoint.maxpacket = 1024;
Sebastian Andrzej Siewior12d36c12011-11-03 20:27:50 +01001821 dep->endpoint.max_streams = 15;
Felipe Balbi72246da2011-08-19 18:10:58 +03001822 dep->endpoint.ops = &dwc3_gadget_ep_ops;
1823 list_add_tail(&dep->endpoint.ep_list,
1824 &dwc->gadget.ep_list);
1825
1826 ret = dwc3_alloc_trb_pool(dep);
Felipe Balbi25b8ff62011-11-04 12:32:47 +02001827 if (ret)
Felipe Balbi72246da2011-08-19 18:10:58 +03001828 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001829 }
Felipe Balbi25b8ff62011-11-04 12:32:47 +02001830
Felipe Balbi72246da2011-08-19 18:10:58 +03001831 INIT_LIST_HEAD(&dep->request_list);
1832 INIT_LIST_HEAD(&dep->req_queued);
1833 }
1834
1835 return 0;
1836}
1837
1838static void dwc3_gadget_free_endpoints(struct dwc3 *dwc)
1839{
1840 struct dwc3_ep *dep;
1841 u8 epnum;
1842
1843 for (epnum = 0; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
1844 dep = dwc->eps[epnum];
1845 dwc3_free_trb_pool(dep);
1846
1847 if (epnum != 0 && epnum != 1)
1848 list_del(&dep->endpoint.ep_list);
1849
1850 kfree(dep);
1851 }
1852}
1853
1854static void dwc3_gadget_release(struct device *dev)
1855{
1856 dev_dbg(dev, "%s\n", __func__);
1857}
1858
1859/* -------------------------------------------------------------------------- */
1860static int dwc3_cleanup_done_reqs(struct dwc3 *dwc, struct dwc3_ep *dep,
1861 const struct dwc3_event_depevt *event, int status)
1862{
1863 struct dwc3_request *req;
Felipe Balbif6bafc62012-02-06 11:04:53 +02001864 struct dwc3_trb *trb;
Felipe Balbi72246da2011-08-19 18:10:58 +03001865 unsigned int count;
1866 unsigned int s_pkt = 0;
Pratyush Anand73939b02012-05-25 18:54:56 +05301867 unsigned int trb_status;
Felipe Balbi72246da2011-08-19 18:10:58 +03001868
1869 do {
1870 req = next_request(&dep->req_queued);
Sebastian Andrzej Siewiord39ee7b2011-11-03 10:32:20 +01001871 if (!req) {
1872 WARN_ON_ONCE(1);
1873 return 1;
1874 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001875
Felipe Balbif6bafc62012-02-06 11:04:53 +02001876 trb = req->trb;
Felipe Balbi72246da2011-08-19 18:10:58 +03001877
Felipe Balbif6bafc62012-02-06 11:04:53 +02001878 if ((trb->ctrl & DWC3_TRB_CTRL_HWO) && status != -ESHUTDOWN)
Sebastian Andrzej Siewior0d2f4752011-08-19 19:59:12 +02001879 /*
1880 * We continue despite the error. There is not much we
Paul Zimmerman1d046792012-02-15 18:56:56 -08001881 * can do. If we don't clean it up we loop forever. If
1882 * we skip the TRB then it gets overwritten after a
1883 * while since we use them in a ring buffer. A BUG()
1884 * would help. Lets hope that if this occurs, someone
Sebastian Andrzej Siewior0d2f4752011-08-19 19:59:12 +02001885 * fixes the root cause instead of looking away :)
1886 */
Felipe Balbi72246da2011-08-19 18:10:58 +03001887 dev_err(dwc->dev, "%s's TRB (%p) still owned by HW\n",
1888 dep->name, req->trb);
Felipe Balbif6bafc62012-02-06 11:04:53 +02001889 count = trb->size & DWC3_TRB_SIZE_MASK;
Felipe Balbi72246da2011-08-19 18:10:58 +03001890
1891 if (dep->direction) {
1892 if (count) {
Pratyush Anand73939b02012-05-25 18:54:56 +05301893 trb_status = DWC3_TRB_SIZE_TRBSTS(trb->size);
1894 if (trb_status == DWC3_TRBSTS_MISSED_ISOC) {
1895 dev_dbg(dwc->dev, "incomplete IN transfer %s\n",
1896 dep->name);
Pratyush Anand921b0b82013-01-14 15:59:32 +05301897 /*
1898 * If missed isoc occurred and there is
1899 * no request queued then issue END
1900 * TRANSFER, so that core generates
1901 * next xfernotready and we will issue
1902 * a fresh START TRANSFER.
1903 * If there are still queued request
1904 * then wait, do not issue either END
1905 * or UPDATE TRANSFER, just attach next
1906 * request in request_list during
1907 * giveback.If any future queued request
1908 * is successfully transferred then we
1909 * will issue UPDATE TRANSFER for all
1910 * request in the request_list.
1911 */
Pratyush Anand73939b02012-05-25 18:54:56 +05301912 dep->flags |= DWC3_EP_MISSED_ISOC;
1913 } else {
1914 dev_err(dwc->dev, "incomplete IN transfer %s\n",
1915 dep->name);
1916 status = -ECONNRESET;
1917 }
Pratyush Anand921b0b82013-01-14 15:59:32 +05301918 } else {
1919 dep->flags &= ~DWC3_EP_MISSED_ISOC;
Felipe Balbi72246da2011-08-19 18:10:58 +03001920 }
1921 } else {
1922 if (count && (event->status & DEPEVT_STATUS_SHORT))
1923 s_pkt = 1;
1924 }
1925
1926 /*
1927 * We assume here we will always receive the entire data block
1928 * which we should receive. Meaning, if we program RX to
1929 * receive 4K but we receive only 2K, we assume that's all we
1930 * should receive and we simply bounce the request back to the
1931 * gadget driver for further processing.
1932 */
1933 req->request.actual += req->request.length - count;
1934 dwc3_gadget_giveback(dep, req, status);
1935 if (s_pkt)
1936 break;
Felipe Balbif6bafc62012-02-06 11:04:53 +02001937 if ((event->status & DEPEVT_STATUS_LST) &&
Pratyush Anand413dba62012-06-03 19:43:19 +05301938 (trb->ctrl & (DWC3_TRB_CTRL_LST |
1939 DWC3_TRB_CTRL_HWO)))
Felipe Balbi72246da2011-08-19 18:10:58 +03001940 break;
Felipe Balbif6bafc62012-02-06 11:04:53 +02001941 if ((event->status & DEPEVT_STATUS_IOC) &&
1942 (trb->ctrl & DWC3_TRB_CTRL_IOC))
Felipe Balbi72246da2011-08-19 18:10:58 +03001943 break;
1944 } while (1);
1945
Pratyush Anand18bbcb02013-01-14 15:59:34 +05301946 if (usb_endpoint_xfer_isoc(dep->endpoint.desc) &&
1947 list_empty(&dep->req_queued)) {
Vijayavardhan Vennapusa91ba6532013-01-30 17:35:45 +05301948 if (list_empty(&dep->request_list))
Pratyush Anand18bbcb02013-01-14 15:59:34 +05301949 /*
1950 * If there is no entry in request list then do
1951 * not issue END TRANSFER now. Just set PENDING
1952 * flag, so that END TRANSFER is issued when an
1953 * entry is added into request list.
1954 */
Vijayavardhan Vennapusa91ba6532013-01-30 17:35:45 +05301955 dep->flags |= DWC3_EP_PENDING_REQUEST;
1956 else
Pratyush Anand18bbcb02013-01-14 15:59:34 +05301957 dwc3_stop_active_transfer(dwc, dep->number);
Vijayavardhan Vennapusa91ba6532013-01-30 17:35:45 +05301958 dep->flags &= ~DWC3_EP_MISSED_ISOC;
Pratyush Anand921b0b82013-01-14 15:59:32 +05301959 return 1;
1960 }
1961
Felipe Balbif6bafc62012-02-06 11:04:53 +02001962 if ((event->status & DEPEVT_STATUS_IOC) &&
1963 (trb->ctrl & DWC3_TRB_CTRL_IOC))
Felipe Balbi72246da2011-08-19 18:10:58 +03001964 return 0;
1965 return 1;
1966}
1967
1968static void dwc3_endpoint_transfer_complete(struct dwc3 *dwc,
1969 struct dwc3_ep *dep, const struct dwc3_event_depevt *event,
1970 int start_new)
1971{
1972 unsigned status = 0;
1973 int clean_busy;
1974
1975 if (event->status & DEPEVT_STATUS_BUSERR)
1976 status = -ECONNRESET;
1977
Paul Zimmerman1d046792012-02-15 18:56:56 -08001978 clean_busy = dwc3_cleanup_done_reqs(dwc, dep, event, status);
Paul Zimmermanc2df85c2012-02-24 17:32:18 -08001979 if (clean_busy)
Felipe Balbi72246da2011-08-19 18:10:58 +03001980 dep->flags &= ~DWC3_EP_BUSY;
Felipe Balbifae2b902011-10-14 13:00:30 +03001981
1982 /*
1983 * WORKAROUND: This is the 2nd half of U1/U2 -> U0 workaround.
1984 * See dwc3_gadget_linksts_change_interrupt() for 1st half.
1985 */
1986 if (dwc->revision < DWC3_REVISION_183A) {
1987 u32 reg;
1988 int i;
1989
1990 for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) {
Moiz Sonasatheed03f12012-08-01 14:08:30 -05001991 dep = dwc->eps[i];
Felipe Balbifae2b902011-10-14 13:00:30 +03001992
1993 if (!(dep->flags & DWC3_EP_ENABLED))
1994 continue;
1995
1996 if (!list_empty(&dep->req_queued))
1997 return;
1998 }
1999
2000 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2001 reg |= dwc->u1u2;
2002 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2003
2004 dwc->u1u2 = 0;
2005 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002006}
2007
Felipe Balbi72246da2011-08-19 18:10:58 +03002008static void dwc3_endpoint_interrupt(struct dwc3 *dwc,
2009 const struct dwc3_event_depevt *event)
2010{
2011 struct dwc3_ep *dep;
2012 u8 epnum = event->endpoint_number;
2013
2014 dep = dwc->eps[epnum];
2015
Felipe Balbia09be0a2012-06-06 09:19:35 +03002016 if (!(dep->flags & DWC3_EP_ENABLED))
2017 return;
2018
Felipe Balbi72246da2011-08-19 18:10:58 +03002019 dev_vdbg(dwc->dev, "%s: %s\n", dep->name,
2020 dwc3_ep_event_string(event->endpoint_event));
2021
2022 if (epnum == 0 || epnum == 1) {
2023 dwc3_ep0_interrupt(dwc, event);
2024 return;
2025 }
2026
2027 switch (event->endpoint_event) {
2028 case DWC3_DEPEVT_XFERCOMPLETE:
Felipe Balbi4959cfc2012-06-06 12:04:13 +03002029 dep->resource_index = 0;
Paul Zimmermanc2df85c2012-02-24 17:32:18 -08002030
Ido Shayevitz57cdac12012-03-12 20:25:24 +02002031 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbi72246da2011-08-19 18:10:58 +03002032 dev_dbg(dwc->dev, "%s is an Isochronous endpoint\n",
2033 dep->name);
2034 return;
2035 }
2036
2037 dwc3_endpoint_transfer_complete(dwc, dep, event, 1);
2038 break;
2039 case DWC3_DEPEVT_XFERINPROGRESS:
Ido Shayevitz57cdac12012-03-12 20:25:24 +02002040 if (!usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbi72246da2011-08-19 18:10:58 +03002041 dev_dbg(dwc->dev, "%s is not an Isochronous endpoint\n",
2042 dep->name);
2043 return;
2044 }
2045
2046 dwc3_endpoint_transfer_complete(dwc, dep, event, 0);
2047 break;
2048 case DWC3_DEPEVT_XFERNOTREADY:
Ido Shayevitz57cdac12012-03-12 20:25:24 +02002049 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbi72246da2011-08-19 18:10:58 +03002050 dwc3_gadget_start_isoc(dwc, dep, event);
2051 } else {
2052 int ret;
2053
2054 dev_vdbg(dwc->dev, "%s: reason %s\n",
Felipe Balbi40aa41f2012-01-18 17:06:03 +02002055 dep->name, event->status &
2056 DEPEVT_STATUS_TRANSFER_ACTIVE
Felipe Balbi72246da2011-08-19 18:10:58 +03002057 ? "Transfer Active"
2058 : "Transfer Not Active");
2059
2060 ret = __dwc3_gadget_kick_transfer(dep, 0, 1);
2061 if (!ret || ret == -EBUSY)
2062 return;
2063
2064 dev_dbg(dwc->dev, "%s: failed to kick transfers\n",
2065 dep->name);
2066 }
2067
2068 break;
Felipe Balbi879631a2011-09-30 10:58:47 +03002069 case DWC3_DEPEVT_STREAMEVT:
Ido Shayevitz57cdac12012-03-12 20:25:24 +02002070 if (!usb_endpoint_xfer_bulk(dep->endpoint.desc)) {
Felipe Balbi879631a2011-09-30 10:58:47 +03002071 dev_err(dwc->dev, "Stream event for non-Bulk %s\n",
2072 dep->name);
2073 return;
2074 }
2075
2076 switch (event->status) {
2077 case DEPEVT_STREAMEVT_FOUND:
2078 dev_vdbg(dwc->dev, "Stream %d found and started\n",
2079 event->parameters);
2080
2081 break;
2082 case DEPEVT_STREAMEVT_NOTFOUND:
2083 /* FALLTHROUGH */
2084 default:
2085 dev_dbg(dwc->dev, "Couldn't find suitable stream\n");
2086 }
2087 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03002088 case DWC3_DEPEVT_RXTXFIFOEVT:
2089 dev_dbg(dwc->dev, "%s FIFO Overrun\n", dep->name);
2090 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03002091 case DWC3_DEPEVT_EPCMDCMPLT:
Felipe Balbib129eb72012-02-17 12:10:04 +02002092 dev_vdbg(dwc->dev, "Endpoint Command Complete\n");
Felipe Balbi72246da2011-08-19 18:10:58 +03002093 break;
2094 }
2095}
2096
2097static void dwc3_disconnect_gadget(struct dwc3 *dwc)
2098{
2099 if (dwc->gadget_driver && dwc->gadget_driver->disconnect) {
2100 spin_unlock(&dwc->lock);
2101 dwc->gadget_driver->disconnect(&dwc->gadget);
2102 spin_lock(&dwc->lock);
2103 }
2104}
2105
2106static void dwc3_stop_active_transfer(struct dwc3 *dwc, u32 epnum)
2107{
2108 struct dwc3_ep *dep;
2109 struct dwc3_gadget_ep_cmd_params params;
2110 u32 cmd;
2111 int ret;
2112
2113 dep = dwc->eps[epnum];
2114
Felipe Balbi4959cfc2012-06-06 12:04:13 +03002115 if (!dep->resource_index)
Pratyush Anand6263ebe2012-06-23 02:23:08 +05302116 return;
2117
Pratyush Anande67fdeb2012-07-06 15:19:10 +05302118 /*
2119 * NOTICE: We are violating what the Databook says about the
2120 * EndTransfer command. Ideally we would _always_ wait for the
2121 * EndTransfer Command Completion IRQ, but that's causing too
2122 * much trouble synchronizing between us and gadget driver.
2123 *
2124 * We have discussed this with the IP Provider and it was
2125 * suggested to giveback all requests here, but give HW some
2126 * extra time to synchronize with the interconnect. We're using
2127 * an arbitraty 100us delay for that.
2128 *
2129 * Note also that a similar handling was tested by Synopsys
2130 * (thanks a lot Paul) and nothing bad has come out of it.
2131 * In short, what we're doing is:
2132 *
2133 * - Issue EndTransfer WITH CMDIOC bit set
2134 * - Wait 100us
2135 */
2136
Pratyush Anand6263ebe2012-06-23 02:23:08 +05302137 cmd = DWC3_DEPCMD_ENDTRANSFER;
2138 cmd |= DWC3_DEPCMD_HIPRI_FORCERM | DWC3_DEPCMD_CMDIOC;
Felipe Balbi4959cfc2012-06-06 12:04:13 +03002139 cmd |= DWC3_DEPCMD_PARAM(dep->resource_index);
Pratyush Anand6263ebe2012-06-23 02:23:08 +05302140 memset(&params, 0, sizeof(params));
2141 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number, cmd, &params);
2142 WARN_ON_ONCE(ret);
Felipe Balbi4959cfc2012-06-06 12:04:13 +03002143 dep->resource_index = 0;
Pratyush Anande67fdeb2012-07-06 15:19:10 +05302144
2145 udelay(100);
Felipe Balbi72246da2011-08-19 18:10:58 +03002146}
2147
2148static void dwc3_stop_active_transfers(struct dwc3 *dwc)
2149{
2150 u32 epnum;
2151
2152 for (epnum = 2; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
2153 struct dwc3_ep *dep;
2154
2155 dep = dwc->eps[epnum];
2156 if (!(dep->flags & DWC3_EP_ENABLED))
2157 continue;
2158
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +02002159 dwc3_remove_requests(dwc, dep);
Felipe Balbi72246da2011-08-19 18:10:58 +03002160 }
2161}
2162
2163static void dwc3_clear_stall_all_ep(struct dwc3 *dwc)
2164{
2165 u32 epnum;
2166
2167 for (epnum = 1; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
2168 struct dwc3_ep *dep;
2169 struct dwc3_gadget_ep_cmd_params params;
2170 int ret;
2171
2172 dep = dwc->eps[epnum];
2173
2174 if (!(dep->flags & DWC3_EP_STALL))
2175 continue;
2176
2177 dep->flags &= ~DWC3_EP_STALL;
2178
2179 memset(&params, 0, sizeof(params));
2180 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number,
2181 DWC3_DEPCMD_CLEARSTALL, &params);
2182 WARN_ON_ONCE(ret);
2183 }
2184}
2185
2186static void dwc3_gadget_disconnect_interrupt(struct dwc3 *dwc)
2187{
Felipe Balbi34d548c2012-05-24 10:30:01 +03002188 int reg;
2189
Felipe Balbi72246da2011-08-19 18:10:58 +03002190 dev_vdbg(dwc->dev, "%s\n", __func__);
Felipe Balbi72246da2011-08-19 18:10:58 +03002191
2192 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2193 reg &= ~DWC3_DCTL_INITU1ENA;
2194 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2195
2196 reg &= ~DWC3_DCTL_INITU2ENA;
2197 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +03002198
Felipe Balbi72246da2011-08-19 18:10:58 +03002199 dwc3_disconnect_gadget(dwc);
Paul Zimmermanb23c8432011-09-30 10:58:42 +03002200 dwc->start_config_issued = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03002201
2202 dwc->gadget.speed = USB_SPEED_UNKNOWN;
Felipe Balbidf62df52011-10-14 15:11:49 +03002203 dwc->setup_packet_pending = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03002204}
2205
Paul Zimmermandffb81b2012-04-27 12:54:05 +03002206static void dwc3_gadget_usb3_phy_suspend(struct dwc3 *dwc, int suspend)
Felipe Balbi72246da2011-08-19 18:10:58 +03002207{
2208 u32 reg;
2209
2210 reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0));
2211
Paul Zimmermandffb81b2012-04-27 12:54:05 +03002212 if (suspend)
Felipe Balbi72246da2011-08-19 18:10:58 +03002213 reg |= DWC3_GUSB3PIPECTL_SUSPHY;
Paul Zimmermandffb81b2012-04-27 12:54:05 +03002214 else
2215 reg &= ~DWC3_GUSB3PIPECTL_SUSPHY;
Felipe Balbi72246da2011-08-19 18:10:58 +03002216
2217 dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg);
2218}
2219
Paul Zimmermandffb81b2012-04-27 12:54:05 +03002220static void dwc3_gadget_usb2_phy_suspend(struct dwc3 *dwc, int suspend)
Felipe Balbi72246da2011-08-19 18:10:58 +03002221{
2222 u32 reg;
2223
2224 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
2225
Paul Zimmermandffb81b2012-04-27 12:54:05 +03002226 if (suspend)
Felipe Balbi72246da2011-08-19 18:10:58 +03002227 reg |= DWC3_GUSB2PHYCFG_SUSPHY;
Paul Zimmermandffb81b2012-04-27 12:54:05 +03002228 else
2229 reg &= ~DWC3_GUSB2PHYCFG_SUSPHY;
Felipe Balbi72246da2011-08-19 18:10:58 +03002230
2231 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
2232}
2233
2234static void dwc3_gadget_reset_interrupt(struct dwc3 *dwc)
2235{
2236 u32 reg;
Vijayavardhan Vennapusa908f1ed2012-10-19 19:51:48 +05302237 struct dwc3_otg *dotg = dwc->dotg;
Felipe Balbi72246da2011-08-19 18:10:58 +03002238
2239 dev_vdbg(dwc->dev, "%s\n", __func__);
2240
Felipe Balbidf62df52011-10-14 15:11:49 +03002241 /*
2242 * WORKAROUND: DWC3 revisions <1.88a have an issue which
2243 * would cause a missing Disconnect Event if there's a
2244 * pending Setup Packet in the FIFO.
2245 *
2246 * There's no suggested workaround on the official Bug
2247 * report, which states that "unless the driver/application
2248 * is doing any special handling of a disconnect event,
2249 * there is no functional issue".
2250 *
2251 * Unfortunately, it turns out that we _do_ some special
2252 * handling of a disconnect event, namely complete all
2253 * pending transfers, notify gadget driver of the
2254 * disconnection, and so on.
2255 *
2256 * Our suggested workaround is to follow the Disconnect
2257 * Event steps here, instead, based on a setup_packet_pending
2258 * flag. Such flag gets set whenever we have a XferNotReady
2259 * event on EP0 and gets cleared on XferComplete for the
2260 * same endpoint.
2261 *
2262 * Refers to:
2263 *
2264 * STAR#9000466709: RTL: Device : Disconnect event not
2265 * generated if setup packet pending in FIFO
2266 */
2267 if (dwc->revision < DWC3_REVISION_188A) {
2268 if (dwc->setup_packet_pending)
2269 dwc3_gadget_disconnect_interrupt(dwc);
2270 }
2271
Felipe Balbi961906e2011-12-20 15:37:21 +02002272 /* after reset -> Default State */
2273 dwc->dev_state = DWC3_DEFAULT_STATE;
2274
Paul Zimmerman88df4272012-04-27 13:10:52 +03002275 /* Recent versions support automatic phy suspend and don't need this */
2276 if (dwc->revision < DWC3_REVISION_194A) {
2277 /* Resume PHYs */
2278 dwc3_gadget_usb2_phy_suspend(dwc, false);
2279 dwc3_gadget_usb3_phy_suspend(dwc, false);
2280 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002281
Vijayavardhan Vennapusa908f1ed2012-10-19 19:51:48 +05302282 if (dotg && dotg->otg.phy)
2283 usb_phy_set_power(dotg->otg.phy, 0);
2284
Felipe Balbi72246da2011-08-19 18:10:58 +03002285 if (dwc->gadget.speed != USB_SPEED_UNKNOWN)
2286 dwc3_disconnect_gadget(dwc);
2287
2288 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2289 reg &= ~DWC3_DCTL_TSTCTRL_MASK;
2290 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Gerard Cauvy3b637362012-02-10 12:21:18 +02002291 dwc->test_mode = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03002292
2293 dwc3_stop_active_transfers(dwc);
2294 dwc3_clear_stall_all_ep(dwc);
Paul Zimmermanb23c8432011-09-30 10:58:42 +03002295 dwc->start_config_issued = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03002296
2297 /* Reset device address to zero */
2298 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2299 reg &= ~(DWC3_DCFG_DEVADDR_MASK);
2300 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +03002301}
2302
2303static void dwc3_update_ram_clk_sel(struct dwc3 *dwc, u32 speed)
2304{
2305 u32 reg;
2306 u32 usb30_clock = DWC3_GCTL_CLK_BUS;
2307
2308 /*
2309 * We change the clock only at SS but I dunno why I would want to do
2310 * this. Maybe it becomes part of the power saving plan.
2311 */
2312
2313 if (speed != DWC3_DSTS_SUPERSPEED)
2314 return;
2315
2316 /*
2317 * RAMClkSel is reset to 0 after USB reset, so it must be reprogrammed
2318 * each time on Connect Done.
2319 */
2320 if (!usb30_clock)
2321 return;
2322
2323 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
2324 reg |= DWC3_GCTL_RAMCLKSEL(usb30_clock);
2325 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
2326}
2327
Paul Zimmermandffb81b2012-04-27 12:54:05 +03002328static void dwc3_gadget_phy_suspend(struct dwc3 *dwc, u8 speed)
Felipe Balbi72246da2011-08-19 18:10:58 +03002329{
2330 switch (speed) {
2331 case USB_SPEED_SUPER:
Paul Zimmermandffb81b2012-04-27 12:54:05 +03002332 dwc3_gadget_usb2_phy_suspend(dwc, true);
Felipe Balbi72246da2011-08-19 18:10:58 +03002333 break;
2334 case USB_SPEED_HIGH:
2335 case USB_SPEED_FULL:
2336 case USB_SPEED_LOW:
Paul Zimmermandffb81b2012-04-27 12:54:05 +03002337 dwc3_gadget_usb3_phy_suspend(dwc, true);
Felipe Balbi72246da2011-08-19 18:10:58 +03002338 break;
2339 }
2340}
2341
2342static void dwc3_gadget_conndone_interrupt(struct dwc3 *dwc)
2343{
2344 struct dwc3_gadget_ep_cmd_params params;
2345 struct dwc3_ep *dep;
2346 int ret;
2347 u32 reg;
2348 u8 speed;
2349
2350 dev_vdbg(dwc->dev, "%s\n", __func__);
2351
2352 memset(&params, 0x00, sizeof(params));
2353
Felipe Balbi72246da2011-08-19 18:10:58 +03002354 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
2355 speed = reg & DWC3_DSTS_CONNECTSPD;
2356 dwc->speed = speed;
2357
2358 dwc3_update_ram_clk_sel(dwc, speed);
2359
2360 switch (speed) {
2361 case DWC3_DCFG_SUPERSPEED:
Felipe Balbi05870c52011-10-14 14:51:38 +03002362 /*
2363 * WORKAROUND: DWC3 revisions <1.90a have an issue which
2364 * would cause a missing USB3 Reset event.
2365 *
2366 * In such situations, we should force a USB3 Reset
2367 * event by calling our dwc3_gadget_reset_interrupt()
2368 * routine.
2369 *
2370 * Refers to:
2371 *
2372 * STAR#9000483510: RTL: SS : USB3 reset event may
2373 * not be generated always when the link enters poll
2374 */
2375 if (dwc->revision < DWC3_REVISION_190A)
2376 dwc3_gadget_reset_interrupt(dwc);
2377
Felipe Balbi72246da2011-08-19 18:10:58 +03002378 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
2379 dwc->gadget.ep0->maxpacket = 512;
2380 dwc->gadget.speed = USB_SPEED_SUPER;
2381 break;
2382 case DWC3_DCFG_HIGHSPEED:
2383 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
2384 dwc->gadget.ep0->maxpacket = 64;
2385 dwc->gadget.speed = USB_SPEED_HIGH;
2386 break;
2387 case DWC3_DCFG_FULLSPEED2:
2388 case DWC3_DCFG_FULLSPEED1:
2389 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
2390 dwc->gadget.ep0->maxpacket = 64;
2391 dwc->gadget.speed = USB_SPEED_FULL;
2392 break;
2393 case DWC3_DCFG_LOWSPEED:
2394 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(8);
2395 dwc->gadget.ep0->maxpacket = 8;
2396 dwc->gadget.speed = USB_SPEED_LOW;
2397 break;
2398 }
2399
Paul Zimmerman88df4272012-04-27 13:10:52 +03002400 /* Recent versions support automatic phy suspend and don't need this */
2401 if (dwc->revision < DWC3_REVISION_194A) {
2402 /* Suspend unneeded PHY */
2403 dwc3_gadget_phy_suspend(dwc, dwc->gadget.speed);
2404 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002405
2406 dep = dwc->eps[0];
Felipe Balbi07e0ee82012-07-16 14:08:16 +03002407 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, true);
Felipe Balbi72246da2011-08-19 18:10:58 +03002408 if (ret) {
2409 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
2410 return;
2411 }
2412
2413 dep = dwc->eps[1];
Felipe Balbi07e0ee82012-07-16 14:08:16 +03002414 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, true);
Felipe Balbi72246da2011-08-19 18:10:58 +03002415 if (ret) {
2416 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
2417 return;
2418 }
2419
2420 /*
2421 * Configure PHY via GUSB3PIPECTLn if required.
2422 *
2423 * Update GTXFIFOSIZn
2424 *
2425 * In both cases reset values should be sufficient.
2426 */
2427}
2428
2429static void dwc3_gadget_wakeup_interrupt(struct dwc3 *dwc)
2430{
2431 dev_vdbg(dwc->dev, "%s\n", __func__);
2432
2433 /*
2434 * TODO take core out of low power mode when that's
2435 * implemented.
2436 */
2437
2438 dwc->gadget_driver->resume(&dwc->gadget);
2439}
2440
2441static void dwc3_gadget_linksts_change_interrupt(struct dwc3 *dwc,
2442 unsigned int evtinfo)
2443{
Felipe Balbifae2b902011-10-14 13:00:30 +03002444 enum dwc3_link_state next = evtinfo & DWC3_LINK_STATE_MASK;
2445
2446 /*
2447 * WORKAROUND: DWC3 Revisions <1.83a have an issue which, depending
2448 * on the link partner, the USB session might do multiple entry/exit
2449 * of low power states before a transfer takes place.
2450 *
2451 * Due to this problem, we might experience lower throughput. The
2452 * suggested workaround is to disable DCTL[12:9] bits if we're
2453 * transitioning from U1/U2 to U0 and enable those bits again
2454 * after a transfer completes and there are no pending transfers
2455 * on any of the enabled endpoints.
2456 *
2457 * This is the first half of that workaround.
2458 *
2459 * Refers to:
2460 *
2461 * STAR#9000446952: RTL: Device SS : if U1/U2 ->U0 takes >128us
2462 * core send LGO_Ux entering U0
2463 */
2464 if (dwc->revision < DWC3_REVISION_183A) {
2465 if (next == DWC3_LINK_STATE_U0) {
2466 u32 u1u2;
2467 u32 reg;
2468
2469 switch (dwc->link_state) {
2470 case DWC3_LINK_STATE_U1:
2471 case DWC3_LINK_STATE_U2:
2472 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2473 u1u2 = reg & (DWC3_DCTL_INITU2ENA
2474 | DWC3_DCTL_ACCEPTU2ENA
2475 | DWC3_DCTL_INITU1ENA
2476 | DWC3_DCTL_ACCEPTU1ENA);
2477
2478 if (!dwc->u1u2)
2479 dwc->u1u2 = reg & u1u2;
2480
2481 reg &= ~u1u2;
2482
2483 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2484 break;
2485 default:
2486 /* do nothing */
2487 break;
2488 }
2489 }
2490 }
2491
Vijayavardhan Vennapusa54be1d62012-10-06 18:32:06 +05302492 if (next == DWC3_LINK_STATE_U0) {
2493 if (dwc->link_state == DWC3_LINK_STATE_U3)
2494 dwc->gadget_driver->resume(&dwc->gadget);
2495 } else if (next == DWC3_LINK_STATE_U3) {
2496 dwc->gadget_driver->suspend(&dwc->gadget);
2497 }
2498
Felipe Balbifae2b902011-10-14 13:00:30 +03002499 dwc->link_state = next;
Felipe Balbi019ac832011-09-08 21:18:47 +03002500
2501 dev_vdbg(dwc->dev, "%s link %d\n", __func__, dwc->link_state);
Felipe Balbi72246da2011-08-19 18:10:58 +03002502}
2503
2504static void dwc3_gadget_interrupt(struct dwc3 *dwc,
2505 const struct dwc3_event_devt *event)
2506{
2507 switch (event->type) {
2508 case DWC3_DEVICE_EVENT_DISCONNECT:
2509 dwc3_gadget_disconnect_interrupt(dwc);
2510 break;
2511 case DWC3_DEVICE_EVENT_RESET:
2512 dwc3_gadget_reset_interrupt(dwc);
2513 break;
2514 case DWC3_DEVICE_EVENT_CONNECT_DONE:
2515 dwc3_gadget_conndone_interrupt(dwc);
2516 break;
2517 case DWC3_DEVICE_EVENT_WAKEUP:
2518 dwc3_gadget_wakeup_interrupt(dwc);
2519 break;
2520 case DWC3_DEVICE_EVENT_LINK_STATUS_CHANGE:
2521 dwc3_gadget_linksts_change_interrupt(dwc, event->event_info);
2522 break;
2523 case DWC3_DEVICE_EVENT_EOPF:
2524 dev_vdbg(dwc->dev, "End of Periodic Frame\n");
2525 break;
2526 case DWC3_DEVICE_EVENT_SOF:
2527 dev_vdbg(dwc->dev, "Start of Periodic Frame\n");
2528 break;
2529 case DWC3_DEVICE_EVENT_ERRATIC_ERROR:
2530 dev_vdbg(dwc->dev, "Erratic Error\n");
2531 break;
2532 case DWC3_DEVICE_EVENT_CMD_CMPL:
2533 dev_vdbg(dwc->dev, "Command Complete\n");
2534 break;
2535 case DWC3_DEVICE_EVENT_OVERFLOW:
2536 dev_vdbg(dwc->dev, "Overflow\n");
Pavankumar Kondetid393e172012-06-12 16:07:29 +05302537 /*
2538 * Controllers prior to 2.30a revision has a bug where
2539 * Overflow Event may overwrite an unacknowledged event
2540 * in the event buffer. The severity of the issue depends
2541 * on the overwritten event type. Add a warning message
2542 * saying that an event is overwritten.
2543 *
2544 * TODO: In future we may need to see if we can re-enumerate
2545 * with host.
2546 */
2547 if (dwc->revision < DWC3_REVISION_230A)
2548 dev_warn(dwc->dev, "Unacknowledged event overwritten\n");
Felipe Balbi72246da2011-08-19 18:10:58 +03002549 break;
Pavankumar Kondeti33fe6f12012-06-12 16:21:46 +05302550 case DWC3_DEVICE_EVENT_VENDOR_DEV_TEST_LMP:
2551 /*
2552 * Controllers prior to 2.30a revision has a bug, due to which
2553 * a vendor device test LMP event can not be filtered. But
2554 * this event is not handled in the current code. This is a
2555 * special event and 8 bytes of data will follow the event.
2556 * Handling this event is tricky when event buffer is almost
2557 * full. Moreover this event will not occur in normal scenario
2558 * and can only happen with special hosts in testing scenarios.
2559 * Add a warning message to indicate that this event is received
2560 * which means that event buffer might have corrupted.
2561 */
2562 if (dwc->revision < DWC3_REVISION_230A)
2563 dev_warn(dwc->dev, "Vendor Device Test LMP Received\n");
2564 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03002565 default:
2566 dev_dbg(dwc->dev, "UNKNOWN IRQ %d\n", event->type);
2567 }
2568}
2569
2570static void dwc3_process_event_entry(struct dwc3 *dwc,
2571 const union dwc3_event *event)
2572{
2573 /* Endpoint IRQ, handle it and return early */
2574 if (event->type.is_devspec == 0) {
2575 /* depevt */
2576 return dwc3_endpoint_interrupt(dwc, &event->depevt);
2577 }
2578
2579 switch (event->type.type) {
2580 case DWC3_EVENT_TYPE_DEV:
2581 dwc3_gadget_interrupt(dwc, &event->devt);
2582 break;
2583 /* REVISIT what to do with Carkit and I2C events ? */
2584 default:
2585 dev_err(dwc->dev, "UNKNOWN IRQ type %d\n", event->raw);
2586 }
2587}
2588
2589static irqreturn_t dwc3_process_event_buf(struct dwc3 *dwc, u32 buf)
2590{
2591 struct dwc3_event_buffer *evt;
2592 int left;
2593 u32 count;
2594
2595 count = dwc3_readl(dwc->regs, DWC3_GEVNTCOUNT(buf));
2596 count &= DWC3_GEVNTCOUNT_MASK;
2597 if (!count)
2598 return IRQ_NONE;
2599
2600 evt = dwc->ev_buffs[buf];
2601 left = count;
2602
2603 while (left > 0) {
2604 union dwc3_event event;
2605
Felipe Balbid70d8442012-02-06 13:40:17 +02002606 event.raw = *(u32 *) (evt->buf + evt->lpos);
2607
Felipe Balbi72246da2011-08-19 18:10:58 +03002608 dwc3_process_event_entry(dwc, &event);
2609 /*
2610 * XXX we wrap around correctly to the next entry as almost all
2611 * entries are 4 bytes in size. There is one entry which has 12
2612 * bytes which is a regular entry followed by 8 bytes data. ATM
2613 * I don't know how things are organized if were get next to the
2614 * a boundary so I worry about that once we try to handle that.
2615 */
2616 evt->lpos = (evt->lpos + 4) % DWC3_EVENT_BUFFERS_SIZE;
2617 left -= 4;
2618
2619 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(buf), 4);
2620 }
2621
2622 return IRQ_HANDLED;
2623}
2624
2625static irqreturn_t dwc3_interrupt(int irq, void *_dwc)
2626{
2627 struct dwc3 *dwc = _dwc;
2628 int i;
2629 irqreturn_t ret = IRQ_NONE;
2630
2631 spin_lock(&dwc->lock);
2632
Felipe Balbi9f622b22011-10-12 10:31:04 +03002633 for (i = 0; i < dwc->num_event_buffers; i++) {
Felipe Balbi72246da2011-08-19 18:10:58 +03002634 irqreturn_t status;
2635
2636 status = dwc3_process_event_buf(dwc, i);
2637 if (status == IRQ_HANDLED)
2638 ret = status;
2639 }
2640
2641 spin_unlock(&dwc->lock);
2642
2643 return ret;
2644}
2645
2646/**
2647 * dwc3_gadget_init - Initializes gadget related registers
Paul Zimmerman1d046792012-02-15 18:56:56 -08002648 * @dwc: pointer to our controller context structure
Felipe Balbi72246da2011-08-19 18:10:58 +03002649 *
2650 * Returns 0 on success otherwise negative errno.
2651 */
2652int __devinit dwc3_gadget_init(struct dwc3 *dwc)
2653{
2654 u32 reg;
2655 int ret;
2656 int irq;
2657
2658 dwc->ctrl_req = dma_alloc_coherent(dwc->dev, sizeof(*dwc->ctrl_req),
2659 &dwc->ctrl_req_addr, GFP_KERNEL);
2660 if (!dwc->ctrl_req) {
2661 dev_err(dwc->dev, "failed to allocate ctrl request\n");
2662 ret = -ENOMEM;
2663 goto err0;
2664 }
2665
2666 dwc->ep0_trb = dma_alloc_coherent(dwc->dev, sizeof(*dwc->ep0_trb),
2667 &dwc->ep0_trb_addr, GFP_KERNEL);
2668 if (!dwc->ep0_trb) {
2669 dev_err(dwc->dev, "failed to allocate ep0 trb\n");
2670 ret = -ENOMEM;
2671 goto err1;
2672 }
2673
Felipe Balbib0791fb2012-05-04 12:58:14 +03002674 dwc->setup_buf = kzalloc(DWC3_EP0_BOUNCE_SIZE, GFP_KERNEL);
Felipe Balbi72246da2011-08-19 18:10:58 +03002675 if (!dwc->setup_buf) {
2676 dev_err(dwc->dev, "failed to allocate setup buffer\n");
2677 ret = -ENOMEM;
2678 goto err2;
2679 }
2680
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002681 dwc->ep0_bounce = dma_alloc_coherent(dwc->dev,
Felipe Balbib0791fb2012-05-04 12:58:14 +03002682 DWC3_EP0_BOUNCE_SIZE, &dwc->ep0_bounce_addr,
2683 GFP_KERNEL);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002684 if (!dwc->ep0_bounce) {
2685 dev_err(dwc->dev, "failed to allocate ep0 bounce buffer\n");
2686 ret = -ENOMEM;
2687 goto err3;
2688 }
2689
Felipe Balbi72246da2011-08-19 18:10:58 +03002690 dev_set_name(&dwc->gadget.dev, "gadget");
2691
2692 dwc->gadget.ops = &dwc3_gadget_ops;
Manu Gautama7b082a2012-11-06 09:50:09 +05302693 dwc->gadget.max_speed = USB_SPEED_SUPER;
Felipe Balbi72246da2011-08-19 18:10:58 +03002694 dwc->gadget.speed = USB_SPEED_UNKNOWN;
2695 dwc->gadget.dev.parent = dwc->dev;
Felipe Balbieeb720f2011-11-28 12:46:59 +02002696 dwc->gadget.sg_supported = true;
Felipe Balbi72246da2011-08-19 18:10:58 +03002697
2698 dma_set_coherent_mask(&dwc->gadget.dev, dwc->dev->coherent_dma_mask);
2699
2700 dwc->gadget.dev.dma_parms = dwc->dev->dma_parms;
2701 dwc->gadget.dev.dma_mask = dwc->dev->dma_mask;
2702 dwc->gadget.dev.release = dwc3_gadget_release;
2703 dwc->gadget.name = "dwc3-gadget";
2704
2705 /*
2706 * REVISIT: Here we should clear all pending IRQs to be
2707 * sure we're starting from a well known location.
2708 */
2709
2710 ret = dwc3_gadget_init_endpoints(dwc);
2711 if (ret)
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002712 goto err4;
Felipe Balbi72246da2011-08-19 18:10:58 +03002713
2714 irq = platform_get_irq(to_platform_device(dwc->dev), 0);
2715
2716 ret = request_irq(irq, dwc3_interrupt, IRQF_SHARED,
2717 "dwc3", dwc);
2718 if (ret) {
2719 dev_err(dwc->dev, "failed to request irq #%d --> %d\n",
2720 irq, ret);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002721 goto err5;
Felipe Balbi72246da2011-08-19 18:10:58 +03002722 }
2723
Sebastian Andrzej Siewiorbb8b8a32011-09-13 17:54:39 +02002724 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2725 reg |= DWC3_DCFG_LPM_CAP;
2726 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
2727
Felipe Balbi72246da2011-08-19 18:10:58 +03002728 /* Enable all but Start and End of Frame IRQs */
Pavankumar Kondeti33fe6f12012-06-12 16:21:46 +05302729 reg = (DWC3_DEVTEN_EVNTOVERFLOWEN |
Felipe Balbi72246da2011-08-19 18:10:58 +03002730 DWC3_DEVTEN_CMDCMPLTEN |
2731 DWC3_DEVTEN_ERRTICERREN |
2732 DWC3_DEVTEN_WKUPEVTEN |
2733 DWC3_DEVTEN_ULSTCNGEN |
2734 DWC3_DEVTEN_CONNECTDONEEN |
2735 DWC3_DEVTEN_USBRSTEN |
2736 DWC3_DEVTEN_DISCONNEVTEN);
2737 dwc3_writel(dwc->regs, DWC3_DEVTEN, reg);
2738
Paul Zimmerman88df4272012-04-27 13:10:52 +03002739 /* Enable USB2 LPM and automatic phy suspend only on recent versions */
2740 if (dwc->revision >= DWC3_REVISION_194A) {
2741 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2742 reg |= DWC3_DCFG_LPM_CAP;
2743 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
2744
2745 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2746 reg &= ~(DWC3_DCTL_HIRD_THRES_MASK | DWC3_DCTL_L1_HIBER_EN);
2747
2748 /* TODO: This should be configurable */
Pratyush Anandd69dcdd2012-07-02 10:21:52 +05302749 reg |= DWC3_DCTL_HIRD_THRES(28);
Paul Zimmerman88df4272012-04-27 13:10:52 +03002750
2751 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2752
Pratyush Anand50ed8342012-06-06 19:36:17 +05302753 dwc3_gadget_usb2_phy_suspend(dwc, false);
2754 dwc3_gadget_usb3_phy_suspend(dwc, false);
Paul Zimmerman88df4272012-04-27 13:10:52 +03002755 }
2756
Felipe Balbi72246da2011-08-19 18:10:58 +03002757 ret = device_register(&dwc->gadget.dev);
2758 if (ret) {
2759 dev_err(dwc->dev, "failed to register gadget device\n");
2760 put_device(&dwc->gadget.dev);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002761 goto err6;
Felipe Balbi72246da2011-08-19 18:10:58 +03002762 }
2763
2764 ret = usb_add_gadget_udc(dwc->dev, &dwc->gadget);
2765 if (ret) {
2766 dev_err(dwc->dev, "failed to register udc\n");
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002767 goto err7;
Felipe Balbi72246da2011-08-19 18:10:58 +03002768 }
2769
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +02002770 if (dwc->dotg) {
2771 /* dwc3 otg driver is active (DRD mode + SRPSupport=1) */
2772 ret = otg_set_peripheral(&dwc->dotg->otg, &dwc->gadget);
2773 if (ret) {
2774 dev_err(dwc->dev, "failed to set peripheral to otg\n");
2775 goto err7;
2776 }
Manu Gautamb5067272012-07-02 09:53:41 +05302777 } else {
2778 pm_runtime_no_callbacks(&dwc->gadget.dev);
2779 pm_runtime_set_active(&dwc->gadget.dev);
2780 pm_runtime_enable(&dwc->gadget.dev);
2781 pm_runtime_get(&dwc->gadget.dev);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +02002782 }
2783
Felipe Balbi72246da2011-08-19 18:10:58 +03002784 return 0;
2785
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002786err7:
Felipe Balbi72246da2011-08-19 18:10:58 +03002787 device_unregister(&dwc->gadget.dev);
2788
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002789err6:
Felipe Balbi72246da2011-08-19 18:10:58 +03002790 dwc3_writel(dwc->regs, DWC3_DEVTEN, 0x00);
2791 free_irq(irq, dwc);
2792
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002793err5:
Felipe Balbi72246da2011-08-19 18:10:58 +03002794 dwc3_gadget_free_endpoints(dwc);
2795
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002796err4:
Felipe Balbib0791fb2012-05-04 12:58:14 +03002797 dma_free_coherent(dwc->dev, DWC3_EP0_BOUNCE_SIZE,
2798 dwc->ep0_bounce, dwc->ep0_bounce_addr);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002799
Felipe Balbi72246da2011-08-19 18:10:58 +03002800err3:
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02002801 kfree(dwc->setup_buf);
Felipe Balbi72246da2011-08-19 18:10:58 +03002802
2803err2:
2804 dma_free_coherent(dwc->dev, sizeof(*dwc->ep0_trb),
2805 dwc->ep0_trb, dwc->ep0_trb_addr);
2806
2807err1:
2808 dma_free_coherent(dwc->dev, sizeof(*dwc->ctrl_req),
2809 dwc->ctrl_req, dwc->ctrl_req_addr);
2810
2811err0:
2812 return ret;
2813}
2814
2815void dwc3_gadget_exit(struct dwc3 *dwc)
2816{
2817 int irq;
Felipe Balbi72246da2011-08-19 18:10:58 +03002818
Manu Gautamb5067272012-07-02 09:53:41 +05302819 if (dwc->dotg) {
2820 pm_runtime_put(&dwc->gadget.dev);
2821 pm_runtime_disable(&dwc->gadget.dev);
2822 }
2823
Felipe Balbi72246da2011-08-19 18:10:58 +03002824 usb_del_gadget_udc(&dwc->gadget);
2825 irq = platform_get_irq(to_platform_device(dwc->dev), 0);
2826
2827 dwc3_writel(dwc->regs, DWC3_DEVTEN, 0x00);
2828 free_irq(irq, dwc);
2829
Felipe Balbi72246da2011-08-19 18:10:58 +03002830 dwc3_gadget_free_endpoints(dwc);
2831
Felipe Balbib0791fb2012-05-04 12:58:14 +03002832 dma_free_coherent(dwc->dev, DWC3_EP0_BOUNCE_SIZE,
2833 dwc->ep0_bounce, dwc->ep0_bounce_addr);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002834
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02002835 kfree(dwc->setup_buf);
Felipe Balbi72246da2011-08-19 18:10:58 +03002836
2837 dma_free_coherent(dwc->dev, sizeof(*dwc->ep0_trb),
2838 dwc->ep0_trb, dwc->ep0_trb_addr);
2839
2840 dma_free_coherent(dwc->dev, sizeof(*dwc->ctrl_req),
2841 dwc->ctrl_req, dwc->ctrl_req_addr);
2842
2843 device_unregister(&dwc->gadget.dev);
2844}