blob: 77a0013c9dd3cd12112a7910861a9af4d58bf0aa [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>
52
53#include "core.h"
54#include "gadget.h"
55#include "io.h"
56
Felipe Balbi04a9bfc2012-01-02 18:25:43 +020057/**
58 * dwc3_gadget_set_test_mode - Enables USB2 Test Modes
59 * @dwc: pointer to our context structure
60 * @mode: the mode to set (J, K SE0 NAK, Force Enable)
61 *
62 * Caller should take care of locking. This function will
63 * return 0 on success or -EINVAL if wrong Test Selector
64 * is passed
65 */
66int dwc3_gadget_set_test_mode(struct dwc3 *dwc, int mode)
67{
68 u32 reg;
69
70 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
71 reg &= ~DWC3_DCTL_TSTCTRL_MASK;
72
73 switch (mode) {
74 case TEST_J:
75 case TEST_K:
76 case TEST_SE0_NAK:
77 case TEST_PACKET:
78 case TEST_FORCE_EN:
79 reg |= mode << 1;
80 break;
81 default:
82 return -EINVAL;
83 }
84
85 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
86
87 return 0;
88}
89
Felipe Balbi8598bde2012-01-02 18:55:57 +020090/**
91 * dwc3_gadget_set_link_state - Sets USB Link to a particular State
92 * @dwc: pointer to our context structure
93 * @state: the state to put link into
94 *
95 * Caller should take care of locking. This function will
Paul Zimmermanaee63e32012-02-24 17:32:15 -080096 * return 0 on success or -ETIMEDOUT.
Felipe Balbi8598bde2012-01-02 18:55:57 +020097 */
98int dwc3_gadget_set_link_state(struct dwc3 *dwc, enum dwc3_link_state state)
99{
Paul Zimmermanaee63e32012-02-24 17:32:15 -0800100 int retries = 10000;
Felipe Balbi8598bde2012-01-02 18:55:57 +0200101 u32 reg;
102
Paul Zimmerman802fde92012-04-27 13:10:52 +0300103 /*
104 * Wait until device controller is ready. Only applies to 1.94a and
105 * later RTL.
106 */
107 if (dwc->revision >= DWC3_REVISION_194A) {
108 while (--retries) {
109 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
110 if (reg & DWC3_DSTS_DCNRD)
111 udelay(5);
112 else
113 break;
114 }
115
116 if (retries <= 0)
117 return -ETIMEDOUT;
118 }
119
Felipe Balbi8598bde2012-01-02 18:55:57 +0200120 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
121 reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK;
122
123 /* set requested state */
124 reg |= DWC3_DCTL_ULSTCHNGREQ(state);
125 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
126
Paul Zimmerman802fde92012-04-27 13:10:52 +0300127 /*
128 * The following code is racy when called from dwc3_gadget_wakeup,
129 * and is not needed, at least on newer versions
130 */
131 if (dwc->revision >= DWC3_REVISION_194A)
132 return 0;
133
Felipe Balbi8598bde2012-01-02 18:55:57 +0200134 /* wait for a change in DSTS */
Paul Zimmermanaed430e2012-04-27 12:52:01 +0300135 retries = 10000;
Felipe Balbi8598bde2012-01-02 18:55:57 +0200136 while (--retries) {
137 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
138
Felipe Balbi8598bde2012-01-02 18:55:57 +0200139 if (DWC3_DSTS_USBLNKST(reg) == state)
140 return 0;
141
Paul Zimmermanaee63e32012-02-24 17:32:15 -0800142 udelay(5);
Felipe Balbi8598bde2012-01-02 18:55:57 +0200143 }
144
145 dev_vdbg(dwc->dev, "link state change request timed out\n");
146
147 return -ETIMEDOUT;
148}
149
Felipe Balbi457e84b2012-01-18 18:04:09 +0200150/**
151 * dwc3_gadget_resize_tx_fifos - reallocate fifo spaces for current use-case
152 * @dwc: pointer to our context structure
153 *
154 * This function will a best effort FIFO allocation in order
155 * to improve FIFO usage and throughput, while still allowing
156 * us to enable as many endpoints as possible.
157 *
158 * Keep in mind that this operation will be highly dependent
159 * on the configured size for RAM1 - which contains TxFifo -,
160 * the amount of endpoints enabled on coreConsultant tool, and
161 * the width of the Master Bus.
162 *
163 * In the ideal world, we would always be able to satisfy the
164 * following equation:
165 *
166 * ((512 + 2 * MDWIDTH-Bytes) + (Number of IN Endpoints - 1) * \
167 * (3 * (1024 + MDWIDTH-Bytes) + MDWIDTH-Bytes)) / MDWIDTH-Bytes
168 *
169 * Unfortunately, due to many variables that's not always the case.
170 */
171int dwc3_gadget_resize_tx_fifos(struct dwc3 *dwc)
172{
173 int last_fifo_depth = 0;
174 int ram1_depth;
175 int fifo_size;
176 int mdwidth;
177 int num;
178
179 if (!dwc->needs_fifo_resize)
180 return 0;
181
182 ram1_depth = DWC3_RAM1_DEPTH(dwc->hwparams.hwparams7);
183 mdwidth = DWC3_MDWIDTH(dwc->hwparams.hwparams0);
184
185 /* MDWIDTH is represented in bits, we need it in bytes */
186 mdwidth >>= 3;
187
188 /*
189 * FIXME For now we will only allocate 1 wMaxPacketSize space
190 * for each enabled endpoint, later patches will come to
191 * improve this algorithm so that we better use the internal
192 * FIFO space
193 */
194 for (num = 0; num < DWC3_ENDPOINTS_NUM; num++) {
195 struct dwc3_ep *dep = dwc->eps[num];
196 int fifo_number = dep->number >> 1;
Felipe Balbi2e81c362012-02-02 13:01:12 +0200197 int mult = 1;
Felipe Balbi457e84b2012-01-18 18:04:09 +0200198 int tmp;
199
200 if (!(dep->number & 1))
201 continue;
202
203 if (!(dep->flags & DWC3_EP_ENABLED))
204 continue;
205
Ido Shayevitz16e78db2012-03-12 20:25:24 +0200206 if (usb_endpoint_xfer_bulk(dep->endpoint.desc)
207 || usb_endpoint_xfer_isoc(dep->endpoint.desc))
Felipe Balbi2e81c362012-02-02 13:01:12 +0200208 mult = 3;
209
210 /*
211 * REVISIT: the following assumes we will always have enough
212 * space available on the FIFO RAM for all possible use cases.
213 * Make sure that's true somehow and change FIFO allocation
214 * accordingly.
215 *
216 * If we have Bulk or Isochronous endpoints, we want
217 * them to be able to be very, very fast. So we're giving
218 * those endpoints a fifo_size which is enough for 3 full
219 * packets
220 */
221 tmp = mult * (dep->endpoint.maxpacket + mdwidth);
Felipe Balbi457e84b2012-01-18 18:04:09 +0200222 tmp += mdwidth;
223
224 fifo_size = DIV_ROUND_UP(tmp, mdwidth);
Felipe Balbi2e81c362012-02-02 13:01:12 +0200225
Felipe Balbi457e84b2012-01-18 18:04:09 +0200226 fifo_size |= (last_fifo_depth << 16);
227
228 dev_vdbg(dwc->dev, "%s: Fifo Addr %04x Size %d\n",
229 dep->name, last_fifo_depth, fifo_size & 0xffff);
230
231 dwc3_writel(dwc->regs, DWC3_GTXFIFOSIZ(fifo_number),
232 fifo_size);
233
234 last_fifo_depth += (fifo_size & 0xffff);
235 }
236
237 return 0;
238}
239
Felipe Balbi72246da2011-08-19 18:10:58 +0300240void dwc3_gadget_giveback(struct dwc3_ep *dep, struct dwc3_request *req,
241 int status)
242{
243 struct dwc3 *dwc = dep->dwc;
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530244 int i;
Felipe Balbi72246da2011-08-19 18:10:58 +0300245
246 if (req->queued) {
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530247 i = 0;
248 do {
Felipe Balbieeb720f2011-11-28 12:46:59 +0200249 dep->busy_slot++;
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530250 /*
251 * Skip LINK TRB. We can't use req->trb and check for
252 * DWC3_TRBCTL_LINK_TRB because it points the TRB we
253 * just completed (not the LINK TRB).
254 */
255 if (((dep->busy_slot & DWC3_TRB_MASK) ==
256 DWC3_TRB_NUM- 1) &&
Ido Shayevitz16e78db2012-03-12 20:25:24 +0200257 usb_endpoint_xfer_isoc(dep->endpoint.desc))
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530258 dep->busy_slot++;
259 } while(++i < req->request.num_mapped_sgs);
Pratyush Anandc9fda7d2013-01-14 15:59:38 +0530260 req->queued = false;
Felipe Balbi72246da2011-08-19 18:10:58 +0300261 }
262 list_del(&req->list);
Felipe Balbieeb720f2011-11-28 12:46:59 +0200263 req->trb = NULL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300264
265 if (req->request.status == -EINPROGRESS)
266 req->request.status = status;
267
Pratyush Anand0416e492012-08-10 13:42:16 +0530268 if (dwc->ep0_bounced && dep->number == 0)
269 dwc->ep0_bounced = false;
270 else
271 usb_gadget_unmap_request(&dwc->gadget, &req->request,
272 req->direction);
Felipe Balbi72246da2011-08-19 18:10:58 +0300273
274 dev_dbg(dwc->dev, "request %p from %s completed %d/%d ===> %d\n",
275 req, dep->name, req->request.actual,
276 req->request.length, status);
277
278 spin_unlock(&dwc->lock);
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +0200279 req->request.complete(&dep->endpoint, &req->request);
Felipe Balbi72246da2011-08-19 18:10:58 +0300280 spin_lock(&dwc->lock);
281}
282
283static const char *dwc3_gadget_ep_cmd_string(u8 cmd)
284{
285 switch (cmd) {
286 case DWC3_DEPCMD_DEPSTARTCFG:
287 return "Start New Configuration";
288 case DWC3_DEPCMD_ENDTRANSFER:
289 return "End Transfer";
290 case DWC3_DEPCMD_UPDATETRANSFER:
291 return "Update Transfer";
292 case DWC3_DEPCMD_STARTTRANSFER:
293 return "Start Transfer";
294 case DWC3_DEPCMD_CLEARSTALL:
295 return "Clear Stall";
296 case DWC3_DEPCMD_SETSTALL:
297 return "Set Stall";
Paul Zimmerman802fde92012-04-27 13:10:52 +0300298 case DWC3_DEPCMD_GETEPSTATE:
299 return "Get Endpoint State";
Felipe Balbi72246da2011-08-19 18:10:58 +0300300 case DWC3_DEPCMD_SETTRANSFRESOURCE:
301 return "Set Endpoint Transfer Resource";
302 case DWC3_DEPCMD_SETEPCONFIG:
303 return "Set Endpoint Configuration";
304 default:
305 return "UNKNOWN command";
306 }
307}
308
Felipe Balbib09bb642012-04-24 16:19:11 +0300309int dwc3_send_gadget_generic_command(struct dwc3 *dwc, int cmd, u32 param)
310{
311 u32 timeout = 500;
312 u32 reg;
313
314 dwc3_writel(dwc->regs, DWC3_DGCMDPAR, param);
315 dwc3_writel(dwc->regs, DWC3_DGCMD, cmd | DWC3_DGCMD_CMDACT);
316
317 do {
318 reg = dwc3_readl(dwc->regs, DWC3_DGCMD);
319 if (!(reg & DWC3_DGCMD_CMDACT)) {
320 dev_vdbg(dwc->dev, "Command Complete --> %d\n",
321 DWC3_DGCMD_STATUS(reg));
322 return 0;
323 }
324
325 /*
326 * We can't sleep here, because it's also called from
327 * interrupt context.
328 */
329 timeout--;
330 if (!timeout)
331 return -ETIMEDOUT;
332 udelay(1);
333 } while (1);
334}
335
Felipe Balbi72246da2011-08-19 18:10:58 +0300336int dwc3_send_gadget_ep_cmd(struct dwc3 *dwc, unsigned ep,
337 unsigned cmd, struct dwc3_gadget_ep_cmd_params *params)
338{
339 struct dwc3_ep *dep = dwc->eps[ep];
Sebastian Andrzej Siewior61d58242011-08-29 16:46:38 +0200340 u32 timeout = 500;
Felipe Balbi72246da2011-08-19 18:10:58 +0300341 u32 reg;
342
343 dev_vdbg(dwc->dev, "%s: cmd '%s' params %08x %08x %08x\n",
344 dep->name,
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300345 dwc3_gadget_ep_cmd_string(cmd), params->param0,
346 params->param1, params->param2);
Felipe Balbi72246da2011-08-19 18:10:58 +0300347
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300348 dwc3_writel(dwc->regs, DWC3_DEPCMDPAR0(ep), params->param0);
349 dwc3_writel(dwc->regs, DWC3_DEPCMDPAR1(ep), params->param1);
350 dwc3_writel(dwc->regs, DWC3_DEPCMDPAR2(ep), params->param2);
Felipe Balbi72246da2011-08-19 18:10:58 +0300351
352 dwc3_writel(dwc->regs, DWC3_DEPCMD(ep), cmd | DWC3_DEPCMD_CMDACT);
353 do {
354 reg = dwc3_readl(dwc->regs, DWC3_DEPCMD(ep));
355 if (!(reg & DWC3_DEPCMD_CMDACT)) {
Felipe Balbi164f6e12011-08-27 20:29:58 +0300356 dev_vdbg(dwc->dev, "Command Complete --> %d\n",
357 DWC3_DEPCMD_STATUS(reg));
Felipe Balbi72246da2011-08-19 18:10:58 +0300358 return 0;
359 }
360
361 /*
Felipe Balbi72246da2011-08-19 18:10:58 +0300362 * We can't sleep here, because it is also called from
363 * interrupt context.
364 */
365 timeout--;
366 if (!timeout)
367 return -ETIMEDOUT;
368
Sebastian Andrzej Siewior61d58242011-08-29 16:46:38 +0200369 udelay(1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300370 } while (1);
371}
372
373static dma_addr_t dwc3_trb_dma_offset(struct dwc3_ep *dep,
Felipe Balbif6bafc62012-02-06 11:04:53 +0200374 struct dwc3_trb *trb)
Felipe Balbi72246da2011-08-19 18:10:58 +0300375{
Paul Zimmermanc439ef82011-09-30 10:58:45 +0300376 u32 offset = (char *) trb - (char *) dep->trb_pool;
Felipe Balbi72246da2011-08-19 18:10:58 +0300377
378 return dep->trb_pool_dma + offset;
379}
380
381static int dwc3_alloc_trb_pool(struct dwc3_ep *dep)
382{
383 struct dwc3 *dwc = dep->dwc;
384
385 if (dep->trb_pool)
386 return 0;
387
388 if (dep->number == 0 || dep->number == 1)
389 return 0;
390
391 dep->trb_pool = dma_alloc_coherent(dwc->dev,
392 sizeof(struct dwc3_trb) * DWC3_TRB_NUM,
393 &dep->trb_pool_dma, GFP_KERNEL);
394 if (!dep->trb_pool) {
395 dev_err(dep->dwc->dev, "failed to allocate trb pool for %s\n",
396 dep->name);
397 return -ENOMEM;
398 }
399
400 return 0;
401}
402
403static void dwc3_free_trb_pool(struct dwc3_ep *dep)
404{
405 struct dwc3 *dwc = dep->dwc;
406
407 dma_free_coherent(dwc->dev, sizeof(struct dwc3_trb) * DWC3_TRB_NUM,
408 dep->trb_pool, dep->trb_pool_dma);
409
410 dep->trb_pool = NULL;
411 dep->trb_pool_dma = 0;
412}
413
414static int dwc3_gadget_start_config(struct dwc3 *dwc, struct dwc3_ep *dep)
415{
416 struct dwc3_gadget_ep_cmd_params params;
417 u32 cmd;
418
419 memset(&params, 0x00, sizeof(params));
420
421 if (dep->number != 1) {
422 cmd = DWC3_DEPCMD_DEPSTARTCFG;
423 /* XferRscIdx == 0 for ep0 and 2 for the remaining */
Paul Zimmermanb23c8432011-09-30 10:58:42 +0300424 if (dep->number > 1) {
425 if (dwc->start_config_issued)
426 return 0;
427 dwc->start_config_issued = true;
Felipe Balbi72246da2011-08-19 18:10:58 +0300428 cmd |= DWC3_DEPCMD_PARAM(2);
Paul Zimmermanb23c8432011-09-30 10:58:42 +0300429 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300430
431 return dwc3_send_gadget_ep_cmd(dwc, 0, cmd, &params);
432 }
433
434 return 0;
435}
436
437static int dwc3_gadget_set_ep_config(struct dwc3 *dwc, struct dwc3_ep *dep,
Felipe Balbic90bfae2011-11-29 13:11:21 +0200438 const struct usb_endpoint_descriptor *desc,
Felipe Balbi4b345c92012-07-16 14:08:16 +0300439 const struct usb_ss_ep_comp_descriptor *comp_desc,
440 bool ignore)
Felipe Balbi72246da2011-08-19 18:10:58 +0300441{
442 struct dwc3_gadget_ep_cmd_params params;
443
444 memset(&params, 0x00, sizeof(params));
445
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300446 params.param0 = DWC3_DEPCFG_EP_TYPE(usb_endpoint_type(desc))
Chanho Parkd2e9a132012-08-31 16:54:07 +0900447 | DWC3_DEPCFG_MAX_PACKET_SIZE(usb_endpoint_maxp(desc));
448
449 /* Burst size is only needed in SuperSpeed mode */
450 if (dwc->gadget.speed == USB_SPEED_SUPER) {
451 u32 burst = dep->endpoint.maxburst - 1;
452
453 params.param0 |= DWC3_DEPCFG_BURST_SIZE(burst);
454 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300455
Felipe Balbi4b345c92012-07-16 14:08:16 +0300456 if (ignore)
457 params.param0 |= DWC3_DEPCFG_IGN_SEQ_NUM;
458
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300459 params.param1 = DWC3_DEPCFG_XFER_COMPLETE_EN
460 | DWC3_DEPCFG_XFER_NOT_READY_EN;
Felipe Balbi72246da2011-08-19 18:10:58 +0300461
Felipe Balbi18b7ede2012-01-02 13:35:41 +0200462 if (usb_ss_max_streams(comp_desc) && usb_endpoint_xfer_bulk(desc)) {
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300463 params.param1 |= DWC3_DEPCFG_STREAM_CAPABLE
464 | DWC3_DEPCFG_STREAM_EVENT_EN;
Felipe Balbi879631a2011-09-30 10:58:47 +0300465 dep->stream_capable = true;
466 }
467
Felipe Balbi72246da2011-08-19 18:10:58 +0300468 if (usb_endpoint_xfer_isoc(desc))
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300469 params.param1 |= DWC3_DEPCFG_XFER_IN_PROGRESS_EN;
Felipe Balbi72246da2011-08-19 18:10:58 +0300470
471 /*
472 * We are doing 1:1 mapping for endpoints, meaning
473 * Physical Endpoints 2 maps to Logical Endpoint 2 and
474 * so on. We consider the direction bit as part of the physical
475 * endpoint number. So USB endpoint 0x81 is 0x03.
476 */
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300477 params.param1 |= DWC3_DEPCFG_EP_NUMBER(dep->number);
Felipe Balbi72246da2011-08-19 18:10:58 +0300478
479 /*
480 * We must use the lower 16 TX FIFOs even though
481 * HW might have more
482 */
483 if (dep->direction)
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300484 params.param0 |= DWC3_DEPCFG_FIFO_NUMBER(dep->number >> 1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300485
486 if (desc->bInterval) {
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300487 params.param1 |= DWC3_DEPCFG_BINTERVAL_M1(desc->bInterval - 1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300488 dep->interval = 1 << (desc->bInterval - 1);
489 }
490
491 return dwc3_send_gadget_ep_cmd(dwc, dep->number,
492 DWC3_DEPCMD_SETEPCONFIG, &params);
493}
494
495static int dwc3_gadget_set_xfer_resource(struct dwc3 *dwc, struct dwc3_ep *dep)
496{
497 struct dwc3_gadget_ep_cmd_params params;
498
499 memset(&params, 0x00, sizeof(params));
500
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300501 params.param0 = DWC3_DEPXFERCFG_NUM_XFER_RES(1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300502
503 return dwc3_send_gadget_ep_cmd(dwc, dep->number,
504 DWC3_DEPCMD_SETTRANSFRESOURCE, &params);
505}
506
507/**
508 * __dwc3_gadget_ep_enable - Initializes a HW endpoint
509 * @dep: endpoint to be initialized
510 * @desc: USB Endpoint Descriptor
511 *
512 * Caller should take care of locking
513 */
514static int __dwc3_gadget_ep_enable(struct dwc3_ep *dep,
Felipe Balbic90bfae2011-11-29 13:11:21 +0200515 const struct usb_endpoint_descriptor *desc,
Felipe Balbi4b345c92012-07-16 14:08:16 +0300516 const struct usb_ss_ep_comp_descriptor *comp_desc,
517 bool ignore)
Felipe Balbi72246da2011-08-19 18:10:58 +0300518{
519 struct dwc3 *dwc = dep->dwc;
520 u32 reg;
521 int ret = -ENOMEM;
522
523 if (!(dep->flags & DWC3_EP_ENABLED)) {
524 ret = dwc3_gadget_start_config(dwc, dep);
525 if (ret)
526 return ret;
527 }
528
Felipe Balbi4b345c92012-07-16 14:08:16 +0300529 ret = dwc3_gadget_set_ep_config(dwc, dep, desc, comp_desc, ignore);
Felipe Balbi72246da2011-08-19 18:10:58 +0300530 if (ret)
531 return ret;
532
533 if (!(dep->flags & DWC3_EP_ENABLED)) {
Felipe Balbif6bafc62012-02-06 11:04:53 +0200534 struct dwc3_trb *trb_st_hw;
535 struct dwc3_trb *trb_link;
Felipe Balbi72246da2011-08-19 18:10:58 +0300536
537 ret = dwc3_gadget_set_xfer_resource(dwc, dep);
538 if (ret)
539 return ret;
540
Ido Shayevitz16e78db2012-03-12 20:25:24 +0200541 dep->endpoint.desc = desc;
Felipe Balbic90bfae2011-11-29 13:11:21 +0200542 dep->comp_desc = comp_desc;
Felipe Balbi72246da2011-08-19 18:10:58 +0300543 dep->type = usb_endpoint_type(desc);
544 dep->flags |= DWC3_EP_ENABLED;
545
546 reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
547 reg |= DWC3_DALEPENA_EP(dep->number);
548 dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
549
550 if (!usb_endpoint_xfer_isoc(desc))
551 return 0;
552
553 memset(&trb_link, 0, sizeof(trb_link));
554
Paul Zimmerman1d046792012-02-15 18:56:56 -0800555 /* Link TRB for ISOC. The HWO bit is never reset */
Felipe Balbi72246da2011-08-19 18:10:58 +0300556 trb_st_hw = &dep->trb_pool[0];
557
Felipe Balbif6bafc62012-02-06 11:04:53 +0200558 trb_link = &dep->trb_pool[DWC3_TRB_NUM - 1];
Felipe Balbi72246da2011-08-19 18:10:58 +0300559
Felipe Balbif6bafc62012-02-06 11:04:53 +0200560 trb_link->bpl = lower_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw));
561 trb_link->bph = upper_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw));
562 trb_link->ctrl |= DWC3_TRBCTL_LINK_TRB;
563 trb_link->ctrl |= DWC3_TRB_CTRL_HWO;
Felipe Balbi72246da2011-08-19 18:10:58 +0300564 }
565
566 return 0;
567}
568
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200569static void dwc3_stop_active_transfer(struct dwc3 *dwc, u32 epnum);
570static void dwc3_remove_requests(struct dwc3 *dwc, struct dwc3_ep *dep)
Felipe Balbi72246da2011-08-19 18:10:58 +0300571{
572 struct dwc3_request *req;
573
Felipe Balbiea53b882012-02-17 12:10:04 +0200574 if (!list_empty(&dep->req_queued)) {
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200575 dwc3_stop_active_transfer(dwc, dep->number);
576
Pratyush Anand57911502012-07-06 15:19:10 +0530577 /* - giveback all requests to gadget driver */
Pratyush Anand15916332012-06-15 11:54:36 +0530578 while (!list_empty(&dep->req_queued)) {
579 req = next_request(&dep->req_queued);
580
581 dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
582 }
Felipe Balbiea53b882012-02-17 12:10:04 +0200583 }
584
Felipe Balbi72246da2011-08-19 18:10:58 +0300585 while (!list_empty(&dep->request_list)) {
586 req = next_request(&dep->request_list);
587
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200588 dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
Felipe Balbi72246da2011-08-19 18:10:58 +0300589 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300590}
591
592/**
593 * __dwc3_gadget_ep_disable - Disables a HW endpoint
594 * @dep: the endpoint to disable
595 *
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200596 * This function also removes requests which are currently processed ny the
597 * hardware and those which are not yet scheduled.
598 * Caller should take care of locking.
Felipe Balbi72246da2011-08-19 18:10:58 +0300599 */
Felipe Balbi72246da2011-08-19 18:10:58 +0300600static int __dwc3_gadget_ep_disable(struct dwc3_ep *dep)
601{
602 struct dwc3 *dwc = dep->dwc;
603 u32 reg;
604
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200605 dwc3_remove_requests(dwc, dep);
Felipe Balbi72246da2011-08-19 18:10:58 +0300606
607 reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
608 reg &= ~DWC3_DALEPENA_EP(dep->number);
609 dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
610
Felipe Balbi879631a2011-09-30 10:58:47 +0300611 dep->stream_capable = false;
Ido Shayevitzf9c56cd2012-02-08 13:56:48 +0200612 dep->endpoint.desc = NULL;
Felipe Balbic90bfae2011-11-29 13:11:21 +0200613 dep->comp_desc = NULL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300614 dep->type = 0;
Felipe Balbi879631a2011-09-30 10:58:47 +0300615 dep->flags = 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300616
617 return 0;
618}
619
620/* -------------------------------------------------------------------------- */
621
622static int dwc3_gadget_ep0_enable(struct usb_ep *ep,
623 const struct usb_endpoint_descriptor *desc)
624{
625 return -EINVAL;
626}
627
628static int dwc3_gadget_ep0_disable(struct usb_ep *ep)
629{
630 return -EINVAL;
631}
632
633/* -------------------------------------------------------------------------- */
634
635static int dwc3_gadget_ep_enable(struct usb_ep *ep,
636 const struct usb_endpoint_descriptor *desc)
637{
638 struct dwc3_ep *dep;
639 struct dwc3 *dwc;
640 unsigned long flags;
641 int ret;
642
643 if (!ep || !desc || desc->bDescriptorType != USB_DT_ENDPOINT) {
644 pr_debug("dwc3: invalid parameters\n");
645 return -EINVAL;
646 }
647
648 if (!desc->wMaxPacketSize) {
649 pr_debug("dwc3: missing wMaxPacketSize\n");
650 return -EINVAL;
651 }
652
653 dep = to_dwc3_ep(ep);
654 dwc = dep->dwc;
655
Felipe Balbic6f83f32012-08-15 12:28:29 +0300656 if (dep->flags & DWC3_EP_ENABLED) {
657 dev_WARN_ONCE(dwc->dev, true, "%s is already enabled\n",
658 dep->name);
659 return 0;
660 }
661
Felipe Balbi72246da2011-08-19 18:10:58 +0300662 switch (usb_endpoint_type(desc)) {
663 case USB_ENDPOINT_XFER_CONTROL:
Anton Tikhomirov27a78d62012-02-23 15:38:46 +0900664 strlcat(dep->name, "-control", sizeof(dep->name));
Felipe Balbi72246da2011-08-19 18:10:58 +0300665 break;
666 case USB_ENDPOINT_XFER_ISOC:
Anton Tikhomirov27a78d62012-02-23 15:38:46 +0900667 strlcat(dep->name, "-isoc", sizeof(dep->name));
Felipe Balbi72246da2011-08-19 18:10:58 +0300668 break;
669 case USB_ENDPOINT_XFER_BULK:
Anton Tikhomirov27a78d62012-02-23 15:38:46 +0900670 strlcat(dep->name, "-bulk", sizeof(dep->name));
Felipe Balbi72246da2011-08-19 18:10:58 +0300671 break;
672 case USB_ENDPOINT_XFER_INT:
Anton Tikhomirov27a78d62012-02-23 15:38:46 +0900673 strlcat(dep->name, "-int", sizeof(dep->name));
Felipe Balbi72246da2011-08-19 18:10:58 +0300674 break;
675 default:
676 dev_err(dwc->dev, "invalid endpoint transfer type\n");
677 }
678
Felipe Balbi72246da2011-08-19 18:10:58 +0300679 dev_vdbg(dwc->dev, "Enabling %s\n", dep->name);
680
681 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi4b345c92012-07-16 14:08:16 +0300682 ret = __dwc3_gadget_ep_enable(dep, desc, ep->comp_desc, false);
Felipe Balbi72246da2011-08-19 18:10:58 +0300683 spin_unlock_irqrestore(&dwc->lock, flags);
684
685 return ret;
686}
687
688static int dwc3_gadget_ep_disable(struct usb_ep *ep)
689{
690 struct dwc3_ep *dep;
691 struct dwc3 *dwc;
692 unsigned long flags;
693 int ret;
694
695 if (!ep) {
696 pr_debug("dwc3: invalid parameters\n");
697 return -EINVAL;
698 }
699
700 dep = to_dwc3_ep(ep);
701 dwc = dep->dwc;
702
703 if (!(dep->flags & DWC3_EP_ENABLED)) {
704 dev_WARN_ONCE(dwc->dev, true, "%s is already disabled\n",
705 dep->name);
706 return 0;
707 }
708
709 snprintf(dep->name, sizeof(dep->name), "ep%d%s",
710 dep->number >> 1,
711 (dep->number & 1) ? "in" : "out");
712
713 spin_lock_irqsave(&dwc->lock, flags);
714 ret = __dwc3_gadget_ep_disable(dep);
715 spin_unlock_irqrestore(&dwc->lock, flags);
716
717 return ret;
718}
719
720static struct usb_request *dwc3_gadget_ep_alloc_request(struct usb_ep *ep,
721 gfp_t gfp_flags)
722{
723 struct dwc3_request *req;
724 struct dwc3_ep *dep = to_dwc3_ep(ep);
725 struct dwc3 *dwc = dep->dwc;
726
727 req = kzalloc(sizeof(*req), gfp_flags);
728 if (!req) {
729 dev_err(dwc->dev, "not enough memory\n");
730 return NULL;
731 }
732
733 req->epnum = dep->number;
734 req->dep = dep;
Felipe Balbi72246da2011-08-19 18:10:58 +0300735
736 return &req->request;
737}
738
739static void dwc3_gadget_ep_free_request(struct usb_ep *ep,
740 struct usb_request *request)
741{
742 struct dwc3_request *req = to_dwc3_request(request);
743
744 kfree(req);
745}
746
Felipe Balbic71fc372011-11-22 11:37:34 +0200747/**
748 * dwc3_prepare_one_trb - setup one TRB from one request
749 * @dep: endpoint for which this request is prepared
750 * @req: dwc3_request pointer
751 */
Felipe Balbi68e823e2011-11-28 12:25:01 +0200752static void dwc3_prepare_one_trb(struct dwc3_ep *dep,
Felipe Balbieeb720f2011-11-28 12:46:59 +0200753 struct dwc3_request *req, dma_addr_t dma,
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530754 unsigned length, unsigned last, unsigned chain, unsigned node)
Felipe Balbic71fc372011-11-22 11:37:34 +0200755{
Felipe Balbieeb720f2011-11-28 12:46:59 +0200756 struct dwc3 *dwc = dep->dwc;
Felipe Balbif6bafc62012-02-06 11:04:53 +0200757 struct dwc3_trb *trb;
Felipe Balbic71fc372011-11-22 11:37:34 +0200758
Felipe Balbieeb720f2011-11-28 12:46:59 +0200759 dev_vdbg(dwc->dev, "%s: req %p dma %08llx length %d%s%s\n",
760 dep->name, req, (unsigned long long) dma,
761 length, last ? " last" : "",
762 chain ? " chain" : "");
763
Felipe Balbic71fc372011-11-22 11:37:34 +0200764 /* Skip the LINK-TRB on ISOC */
Pratyush Anand915e2022013-01-14 15:59:35 +0530765 if (((dep->free_slot & DWC3_TRB_MASK) == DWC3_TRB_NUM - 1) &&
Ido Shayevitz16e78db2012-03-12 20:25:24 +0200766 usb_endpoint_xfer_isoc(dep->endpoint.desc))
Pratyush Anand915e2022013-01-14 15:59:35 +0530767 dep->free_slot++;
768
769 trb = &dep->trb_pool[dep->free_slot & DWC3_TRB_MASK];
Felipe Balbic71fc372011-11-22 11:37:34 +0200770
Felipe Balbieeb720f2011-11-28 12:46:59 +0200771 if (!req->trb) {
772 dwc3_gadget_move_request_queued(req);
Felipe Balbif6bafc62012-02-06 11:04:53 +0200773 req->trb = trb;
774 req->trb_dma = dwc3_trb_dma_offset(dep, trb);
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530775 req->start_slot = dep->free_slot & DWC3_TRB_MASK;
Felipe Balbieeb720f2011-11-28 12:46:59 +0200776 }
Felipe Balbic71fc372011-11-22 11:37:34 +0200777
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530778 dep->free_slot++;
779
Felipe Balbif6bafc62012-02-06 11:04:53 +0200780 trb->size = DWC3_TRB_SIZE_LENGTH(length);
781 trb->bpl = lower_32_bits(dma);
782 trb->bph = upper_32_bits(dma);
Felipe Balbic71fc372011-11-22 11:37:34 +0200783
Ido Shayevitz16e78db2012-03-12 20:25:24 +0200784 switch (usb_endpoint_type(dep->endpoint.desc)) {
Felipe Balbic71fc372011-11-22 11:37:34 +0200785 case USB_ENDPOINT_XFER_CONTROL:
Felipe Balbif6bafc62012-02-06 11:04:53 +0200786 trb->ctrl = DWC3_TRBCTL_CONTROL_SETUP;
Felipe Balbic71fc372011-11-22 11:37:34 +0200787 break;
788
789 case USB_ENDPOINT_XFER_ISOC:
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530790 if (!node)
791 trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS_FIRST;
792 else
793 trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS;
Felipe Balbic71fc372011-11-22 11:37:34 +0200794
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530795 if (!req->request.no_interrupt && !chain)
Felipe Balbif6bafc62012-02-06 11:04:53 +0200796 trb->ctrl |= DWC3_TRB_CTRL_IOC;
Felipe Balbic71fc372011-11-22 11:37:34 +0200797 break;
798
799 case USB_ENDPOINT_XFER_BULK:
800 case USB_ENDPOINT_XFER_INT:
Felipe Balbif6bafc62012-02-06 11:04:53 +0200801 trb->ctrl = DWC3_TRBCTL_NORMAL;
Felipe Balbic71fc372011-11-22 11:37:34 +0200802 break;
803 default:
804 /*
805 * This is only possible with faulty memory because we
806 * checked it already :)
807 */
808 BUG();
809 }
810
Ido Shayevitz16e78db2012-03-12 20:25:24 +0200811 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbif6bafc62012-02-06 11:04:53 +0200812 trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI;
813 trb->ctrl |= DWC3_TRB_CTRL_CSP;
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530814 } else if (last) {
815 trb->ctrl |= DWC3_TRB_CTRL_LST;
Felipe Balbif6bafc62012-02-06 11:04:53 +0200816 }
817
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530818 if (chain)
819 trb->ctrl |= DWC3_TRB_CTRL_CHN;
820
Ido Shayevitz16e78db2012-03-12 20:25:24 +0200821 if (usb_endpoint_xfer_bulk(dep->endpoint.desc) && dep->stream_capable)
Felipe Balbif6bafc62012-02-06 11:04:53 +0200822 trb->ctrl |= DWC3_TRB_CTRL_SID_SOFN(req->request.stream_id);
823
824 trb->ctrl |= DWC3_TRB_CTRL_HWO;
Felipe Balbic71fc372011-11-22 11:37:34 +0200825}
826
Felipe Balbi72246da2011-08-19 18:10:58 +0300827/*
828 * dwc3_prepare_trbs - setup TRBs from requests
829 * @dep: endpoint for which requests are being prepared
830 * @starting: true if the endpoint is idle and no requests are queued.
831 *
Paul Zimmerman1d046792012-02-15 18:56:56 -0800832 * The function goes through the requests list and sets up TRBs for the
833 * transfers. The function returns once there are no more TRBs available or
834 * it runs out of requests.
Felipe Balbi72246da2011-08-19 18:10:58 +0300835 */
Felipe Balbi68e823e2011-11-28 12:25:01 +0200836static void dwc3_prepare_trbs(struct dwc3_ep *dep, bool starting)
Felipe Balbi72246da2011-08-19 18:10:58 +0300837{
Felipe Balbi68e823e2011-11-28 12:25:01 +0200838 struct dwc3_request *req, *n;
Felipe Balbi72246da2011-08-19 18:10:58 +0300839 u32 trbs_left;
Paul Zimmerman8d62cd62012-02-15 13:35:06 +0200840 u32 max;
Felipe Balbic71fc372011-11-22 11:37:34 +0200841 unsigned int last_one = 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300842
843 BUILD_BUG_ON_NOT_POWER_OF_2(DWC3_TRB_NUM);
844
845 /* the first request must not be queued */
846 trbs_left = (dep->busy_slot - dep->free_slot) & DWC3_TRB_MASK;
Felipe Balbic71fc372011-11-22 11:37:34 +0200847
Paul Zimmerman8d62cd62012-02-15 13:35:06 +0200848 /* Can't wrap around on a non-isoc EP since there's no link TRB */
Ido Shayevitz16e78db2012-03-12 20:25:24 +0200849 if (!usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Paul Zimmerman8d62cd62012-02-15 13:35:06 +0200850 max = DWC3_TRB_NUM - (dep->free_slot & DWC3_TRB_MASK);
851 if (trbs_left > max)
852 trbs_left = max;
853 }
854
Felipe Balbi72246da2011-08-19 18:10:58 +0300855 /*
Paul Zimmerman1d046792012-02-15 18:56:56 -0800856 * If busy & slot are equal than it is either full or empty. If we are
857 * starting to process requests then we are empty. Otherwise we are
Felipe Balbi72246da2011-08-19 18:10:58 +0300858 * full and don't do anything
859 */
860 if (!trbs_left) {
861 if (!starting)
Felipe Balbi68e823e2011-11-28 12:25:01 +0200862 return;
Felipe Balbi72246da2011-08-19 18:10:58 +0300863 trbs_left = DWC3_TRB_NUM;
864 /*
865 * In case we start from scratch, we queue the ISOC requests
866 * starting from slot 1. This is done because we use ring
867 * buffer and have no LST bit to stop us. Instead, we place
Paul Zimmerman1d046792012-02-15 18:56:56 -0800868 * IOC bit every TRB_NUM/4. We try to avoid having an interrupt
Felipe Balbi72246da2011-08-19 18:10:58 +0300869 * after the first request so we start at slot 1 and have
870 * 7 requests proceed before we hit the first IOC.
871 * Other transfer types don't use the ring buffer and are
872 * processed from the first TRB until the last one. Since we
873 * don't wrap around we have to start at the beginning.
874 */
Ido Shayevitz16e78db2012-03-12 20:25:24 +0200875 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbi72246da2011-08-19 18:10:58 +0300876 dep->busy_slot = 1;
877 dep->free_slot = 1;
878 } else {
879 dep->busy_slot = 0;
880 dep->free_slot = 0;
881 }
882 }
883
884 /* The last TRB is a link TRB, not used for xfer */
Ido Shayevitz16e78db2012-03-12 20:25:24 +0200885 if ((trbs_left <= 1) && usb_endpoint_xfer_isoc(dep->endpoint.desc))
Felipe Balbi68e823e2011-11-28 12:25:01 +0200886 return;
Felipe Balbi72246da2011-08-19 18:10:58 +0300887
888 list_for_each_entry_safe(req, n, &dep->request_list, list) {
Felipe Balbieeb720f2011-11-28 12:46:59 +0200889 unsigned length;
890 dma_addr_t dma;
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530891 last_one = false;
Felipe Balbi72246da2011-08-19 18:10:58 +0300892
Felipe Balbieeb720f2011-11-28 12:46:59 +0200893 if (req->request.num_mapped_sgs > 0) {
894 struct usb_request *request = &req->request;
895 struct scatterlist *sg = request->sg;
896 struct scatterlist *s;
897 int i;
Felipe Balbi72246da2011-08-19 18:10:58 +0300898
Felipe Balbieeb720f2011-11-28 12:46:59 +0200899 for_each_sg(sg, s, request->num_mapped_sgs, i) {
900 unsigned chain = true;
Felipe Balbi72246da2011-08-19 18:10:58 +0300901
Felipe Balbieeb720f2011-11-28 12:46:59 +0200902 length = sg_dma_len(s);
903 dma = sg_dma_address(s);
Felipe Balbi72246da2011-08-19 18:10:58 +0300904
Paul Zimmerman1d046792012-02-15 18:56:56 -0800905 if (i == (request->num_mapped_sgs - 1) ||
906 sg_is_last(s)) {
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530907 if (list_is_last(&req->list,
908 &dep->request_list))
909 last_one = true;
Felipe Balbieeb720f2011-11-28 12:46:59 +0200910 chain = false;
911 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300912
Felipe Balbieeb720f2011-11-28 12:46:59 +0200913 trbs_left--;
914 if (!trbs_left)
915 last_one = true;
Felipe Balbi72246da2011-08-19 18:10:58 +0300916
Felipe Balbieeb720f2011-11-28 12:46:59 +0200917 if (last_one)
918 chain = false;
Felipe Balbi72246da2011-08-19 18:10:58 +0300919
Felipe Balbieeb720f2011-11-28 12:46:59 +0200920 dwc3_prepare_one_trb(dep, req, dma, length,
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530921 last_one, chain, i);
Felipe Balbi72246da2011-08-19 18:10:58 +0300922
Felipe Balbieeb720f2011-11-28 12:46:59 +0200923 if (last_one)
924 break;
925 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300926 } else {
Felipe Balbieeb720f2011-11-28 12:46:59 +0200927 dma = req->request.dma;
928 length = req->request.length;
929 trbs_left--;
930
931 if (!trbs_left)
932 last_one = 1;
933
934 /* Is this the last request? */
935 if (list_is_last(&req->list, &dep->request_list))
936 last_one = 1;
937
938 dwc3_prepare_one_trb(dep, req, dma, length,
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530939 last_one, false, 0);
Felipe Balbieeb720f2011-11-28 12:46:59 +0200940
941 if (last_one)
942 break;
Felipe Balbi72246da2011-08-19 18:10:58 +0300943 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300944 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300945}
946
947static int __dwc3_gadget_kick_transfer(struct dwc3_ep *dep, u16 cmd_param,
948 int start_new)
949{
950 struct dwc3_gadget_ep_cmd_params params;
951 struct dwc3_request *req;
952 struct dwc3 *dwc = dep->dwc;
953 int ret;
954 u32 cmd;
955
956 if (start_new && (dep->flags & DWC3_EP_BUSY)) {
957 dev_vdbg(dwc->dev, "%s: endpoint busy\n", dep->name);
958 return -EBUSY;
959 }
960 dep->flags &= ~DWC3_EP_PENDING_REQUEST;
961
962 /*
963 * If we are getting here after a short-out-packet we don't enqueue any
964 * new requests as we try to set the IOC bit only on the last request.
965 */
966 if (start_new) {
967 if (list_empty(&dep->req_queued))
968 dwc3_prepare_trbs(dep, start_new);
969
970 /* req points to the first request which will be sent */
971 req = next_request(&dep->req_queued);
972 } else {
Felipe Balbi68e823e2011-11-28 12:25:01 +0200973 dwc3_prepare_trbs(dep, start_new);
974
Felipe Balbi72246da2011-08-19 18:10:58 +0300975 /*
Paul Zimmerman1d046792012-02-15 18:56:56 -0800976 * req points to the first request where HWO changed from 0 to 1
Felipe Balbi72246da2011-08-19 18:10:58 +0300977 */
Felipe Balbi68e823e2011-11-28 12:25:01 +0200978 req = next_request(&dep->req_queued);
Felipe Balbi72246da2011-08-19 18:10:58 +0300979 }
980 if (!req) {
981 dep->flags |= DWC3_EP_PENDING_REQUEST;
982 return 0;
983 }
984
985 memset(&params, 0, sizeof(params));
Felipe Balbi72246da2011-08-19 18:10:58 +0300986
Pratyush Anand1877d6c2013-01-14 15:59:36 +0530987 if (start_new) {
988 params.param0 = upper_32_bits(req->trb_dma);
989 params.param1 = lower_32_bits(req->trb_dma);
Felipe Balbi72246da2011-08-19 18:10:58 +0300990 cmd = DWC3_DEPCMD_STARTTRANSFER;
Pratyush Anand1877d6c2013-01-14 15:59:36 +0530991 } else {
Felipe Balbi72246da2011-08-19 18:10:58 +0300992 cmd = DWC3_DEPCMD_UPDATETRANSFER;
Pratyush Anand1877d6c2013-01-14 15:59:36 +0530993 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300994
995 cmd |= DWC3_DEPCMD_PARAM(cmd_param);
996 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number, cmd, &params);
997 if (ret < 0) {
998 dev_dbg(dwc->dev, "failed to send STARTTRANSFER command\n");
999
1000 /*
1001 * FIXME we need to iterate over the list of requests
1002 * here and stop, unmap, free and del each of the linked
Paul Zimmerman1d046792012-02-15 18:56:56 -08001003 * requests instead of what we do now.
Felipe Balbi72246da2011-08-19 18:10:58 +03001004 */
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02001005 usb_gadget_unmap_request(&dwc->gadget, &req->request,
1006 req->direction);
Felipe Balbi72246da2011-08-19 18:10:58 +03001007 list_del(&req->list);
1008 return ret;
1009 }
1010
1011 dep->flags |= DWC3_EP_BUSY;
Felipe Balbi25b8ff62011-11-04 12:32:47 +02001012
Paul Zimmermanf898ae02012-03-29 18:16:54 +00001013 if (start_new) {
Felipe Balbib4996a82012-06-06 12:04:13 +03001014 dep->resource_index = dwc3_gadget_ep_get_transfer_index(dwc,
Paul Zimmermanf898ae02012-03-29 18:16:54 +00001015 dep->number);
Felipe Balbib4996a82012-06-06 12:04:13 +03001016 WARN_ON_ONCE(!dep->resource_index);
Paul Zimmermanf898ae02012-03-29 18:16:54 +00001017 }
Felipe Balbi25b8ff62011-11-04 12:32:47 +02001018
Felipe Balbi72246da2011-08-19 18:10:58 +03001019 return 0;
1020}
1021
Pratyush Anandd6d6ec72012-05-25 18:54:56 +05301022static void __dwc3_gadget_start_isoc(struct dwc3 *dwc,
1023 struct dwc3_ep *dep, u32 cur_uf)
1024{
1025 u32 uf;
1026
1027 if (list_empty(&dep->request_list)) {
1028 dev_vdbg(dwc->dev, "ISOC ep %s run out for requests.\n",
1029 dep->name);
Pratyush Anandf4a53c52012-08-30 12:21:43 +05301030 dep->flags |= DWC3_EP_PENDING_REQUEST;
Pratyush Anandd6d6ec72012-05-25 18:54:56 +05301031 return;
1032 }
1033
1034 /* 4 micro frames in the future */
1035 uf = cur_uf + dep->interval * 4;
1036
1037 __dwc3_gadget_kick_transfer(dep, uf, 1);
1038}
1039
1040static void dwc3_gadget_start_isoc(struct dwc3 *dwc,
1041 struct dwc3_ep *dep, const struct dwc3_event_depevt *event)
1042{
1043 u32 cur_uf, mask;
1044
1045 mask = ~(dep->interval - 1);
1046 cur_uf = event->parameters & mask;
1047
1048 __dwc3_gadget_start_isoc(dwc, dep, cur_uf);
1049}
1050
Felipe Balbi72246da2011-08-19 18:10:58 +03001051static int __dwc3_gadget_ep_queue(struct dwc3_ep *dep, struct dwc3_request *req)
1052{
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02001053 struct dwc3 *dwc = dep->dwc;
1054 int ret;
1055
Felipe Balbi72246da2011-08-19 18:10:58 +03001056 req->request.actual = 0;
1057 req->request.status = -EINPROGRESS;
1058 req->direction = dep->direction;
1059 req->epnum = dep->number;
1060
1061 /*
1062 * We only add to our list of requests now and
1063 * start consuming the list once we get XferNotReady
1064 * IRQ.
1065 *
1066 * That way, we avoid doing anything that we don't need
1067 * to do now and defer it until the point we receive a
1068 * particular token from the Host side.
1069 *
1070 * This will also avoid Host cancelling URBs due to too
Paul Zimmerman1d046792012-02-15 18:56:56 -08001071 * many NAKs.
Felipe Balbi72246da2011-08-19 18:10:58 +03001072 */
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02001073 ret = usb_gadget_map_request(&dwc->gadget, &req->request,
1074 dep->direction);
1075 if (ret)
1076 return ret;
1077
Felipe Balbi72246da2011-08-19 18:10:58 +03001078 list_add_tail(&req->list, &dep->request_list);
1079
1080 /*
Felipe Balbib511e5e2012-06-06 12:00:50 +03001081 * There are a few special cases:
Felipe Balbi72246da2011-08-19 18:10:58 +03001082 *
Paul Zimmermanf898ae02012-03-29 18:16:54 +00001083 * 1. XferNotReady with empty list of requests. We need to kick the
1084 * transfer here in that situation, otherwise we will be NAKing
1085 * forever. If we get XferNotReady before gadget driver has a
1086 * chance to queue a request, we will ACK the IRQ but won't be
1087 * able to receive the data until the next request is queued.
1088 * The following code is handling exactly that.
1089 *
Felipe Balbi72246da2011-08-19 18:10:58 +03001090 */
1091 if (dep->flags & DWC3_EP_PENDING_REQUEST) {
Pratyush Anandf4a53c52012-08-30 12:21:43 +05301092 /*
1093 * If xfernotready is already elapsed and it is a case
1094 * of isoc transfer, then issue END TRANSFER, so that
1095 * you can receive xfernotready again and can have
1096 * notion of current microframe.
1097 */
1098 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Pratyush Anandcdc359d2013-01-14 15:59:34 +05301099 if (list_empty(&dep->req_queued)) {
1100 dwc3_stop_active_transfer(dwc, dep->number);
1101 dep->flags = DWC3_EP_ENABLED;
1102 }
Pratyush Anandf4a53c52012-08-30 12:21:43 +05301103 return 0;
1104 }
1105
Felipe Balbib511e5e2012-06-06 12:00:50 +03001106 ret = __dwc3_gadget_kick_transfer(dep, 0, true);
Moiz Sonasath348e0262012-08-01 14:08:30 -05001107 if (ret && ret != -EBUSY)
Felipe Balbi72246da2011-08-19 18:10:58 +03001108 dev_dbg(dwc->dev, "%s: failed to kick transfers\n",
1109 dep->name);
Pratyush Anand15f86bd2013-01-14 15:59:33 +05301110 return ret;
Felipe Balbia0925322012-05-22 10:24:11 +03001111 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001112
Felipe Balbib511e5e2012-06-06 12:00:50 +03001113 /*
1114 * 2. XferInProgress on Isoc EP with an active transfer. We need to
1115 * kick the transfer here after queuing a request, otherwise the
1116 * core may not see the modified TRB(s).
1117 */
1118 if (usb_endpoint_xfer_isoc(dep->endpoint.desc) &&
Pratyush Anand79c90462012-08-07 16:54:18 +05301119 (dep->flags & DWC3_EP_BUSY) &&
1120 !(dep->flags & DWC3_EP_MISSED_ISOC)) {
Felipe Balbib4996a82012-06-06 12:04:13 +03001121 WARN_ON_ONCE(!dep->resource_index);
1122 ret = __dwc3_gadget_kick_transfer(dep, dep->resource_index,
Felipe Balbib511e5e2012-06-06 12:00:50 +03001123 false);
Moiz Sonasath348e0262012-08-01 14:08:30 -05001124 if (ret && ret != -EBUSY)
Felipe Balbib511e5e2012-06-06 12:00:50 +03001125 dev_dbg(dwc->dev, "%s: failed to kick transfers\n",
1126 dep->name);
Pratyush Anand15f86bd2013-01-14 15:59:33 +05301127 return ret;
Felipe Balbib511e5e2012-06-06 12:00:50 +03001128 }
1129
Felipe Balbi72246da2011-08-19 18:10:58 +03001130 return 0;
1131}
1132
1133static int dwc3_gadget_ep_queue(struct usb_ep *ep, struct usb_request *request,
1134 gfp_t gfp_flags)
1135{
1136 struct dwc3_request *req = to_dwc3_request(request);
1137 struct dwc3_ep *dep = to_dwc3_ep(ep);
1138 struct dwc3 *dwc = dep->dwc;
1139
1140 unsigned long flags;
1141
1142 int ret;
1143
Ido Shayevitz16e78db2012-03-12 20:25:24 +02001144 if (!dep->endpoint.desc) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001145 dev_dbg(dwc->dev, "trying to queue request %p to disabled %s\n",
1146 request, ep->name);
1147 return -ESHUTDOWN;
1148 }
1149
1150 dev_vdbg(dwc->dev, "queing request %p to %s length %d\n",
1151 request, ep->name, request->length);
1152
1153 spin_lock_irqsave(&dwc->lock, flags);
1154 ret = __dwc3_gadget_ep_queue(dep, req);
1155 spin_unlock_irqrestore(&dwc->lock, flags);
1156
1157 return ret;
1158}
1159
1160static int dwc3_gadget_ep_dequeue(struct usb_ep *ep,
1161 struct usb_request *request)
1162{
1163 struct dwc3_request *req = to_dwc3_request(request);
1164 struct dwc3_request *r = NULL;
1165
1166 struct dwc3_ep *dep = to_dwc3_ep(ep);
1167 struct dwc3 *dwc = dep->dwc;
1168
1169 unsigned long flags;
1170 int ret = 0;
1171
1172 spin_lock_irqsave(&dwc->lock, flags);
1173
1174 list_for_each_entry(r, &dep->request_list, list) {
1175 if (r == req)
1176 break;
1177 }
1178
1179 if (r != req) {
1180 list_for_each_entry(r, &dep->req_queued, list) {
1181 if (r == req)
1182 break;
1183 }
1184 if (r == req) {
1185 /* wait until it is processed */
1186 dwc3_stop_active_transfer(dwc, dep->number);
Pratyush Anande8d4e8b2012-06-15 11:54:00 +05301187 goto out1;
Felipe Balbi72246da2011-08-19 18:10:58 +03001188 }
1189 dev_err(dwc->dev, "request %p was not queued to %s\n",
1190 request, ep->name);
1191 ret = -EINVAL;
1192 goto out0;
1193 }
1194
Pratyush Anande8d4e8b2012-06-15 11:54:00 +05301195out1:
Felipe Balbi72246da2011-08-19 18:10:58 +03001196 /* giveback the request */
1197 dwc3_gadget_giveback(dep, req, -ECONNRESET);
1198
1199out0:
1200 spin_unlock_irqrestore(&dwc->lock, flags);
1201
1202 return ret;
1203}
1204
1205int __dwc3_gadget_ep_set_halt(struct dwc3_ep *dep, int value)
1206{
1207 struct dwc3_gadget_ep_cmd_params params;
1208 struct dwc3 *dwc = dep->dwc;
1209 int ret;
1210
1211 memset(&params, 0x00, sizeof(params));
1212
1213 if (value) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001214 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number,
1215 DWC3_DEPCMD_SETSTALL, &params);
1216 if (ret)
1217 dev_err(dwc->dev, "failed to %s STALL on %s\n",
1218 value ? "set" : "clear",
1219 dep->name);
1220 else
1221 dep->flags |= DWC3_EP_STALL;
1222 } else {
Paul Zimmerman52754552011-09-30 10:58:44 +03001223 if (dep->flags & DWC3_EP_WEDGE)
1224 return 0;
1225
Felipe Balbi72246da2011-08-19 18:10:58 +03001226 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number,
1227 DWC3_DEPCMD_CLEARSTALL, &params);
1228 if (ret)
1229 dev_err(dwc->dev, "failed to %s STALL on %s\n",
1230 value ? "set" : "clear",
1231 dep->name);
1232 else
1233 dep->flags &= ~DWC3_EP_STALL;
1234 }
Paul Zimmerman52754552011-09-30 10:58:44 +03001235
Felipe Balbi72246da2011-08-19 18:10:58 +03001236 return ret;
1237}
1238
1239static int dwc3_gadget_ep_set_halt(struct usb_ep *ep, int value)
1240{
1241 struct dwc3_ep *dep = to_dwc3_ep(ep);
1242 struct dwc3 *dwc = dep->dwc;
1243
1244 unsigned long flags;
1245
1246 int ret;
1247
1248 spin_lock_irqsave(&dwc->lock, flags);
1249
Ido Shayevitz16e78db2012-03-12 20:25:24 +02001250 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001251 dev_err(dwc->dev, "%s is of Isochronous type\n", dep->name);
1252 ret = -EINVAL;
1253 goto out;
1254 }
1255
1256 ret = __dwc3_gadget_ep_set_halt(dep, value);
1257out:
1258 spin_unlock_irqrestore(&dwc->lock, flags);
1259
1260 return ret;
1261}
1262
1263static int dwc3_gadget_ep_set_wedge(struct usb_ep *ep)
1264{
1265 struct dwc3_ep *dep = to_dwc3_ep(ep);
Paul Zimmerman249a4562012-02-24 17:32:16 -08001266 struct dwc3 *dwc = dep->dwc;
1267 unsigned long flags;
Felipe Balbi72246da2011-08-19 18:10:58 +03001268
Paul Zimmerman249a4562012-02-24 17:32:16 -08001269 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001270 dep->flags |= DWC3_EP_WEDGE;
Paul Zimmerman249a4562012-02-24 17:32:16 -08001271 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001272
Pratyush Anand08f0d962012-06-25 22:40:43 +05301273 if (dep->number == 0 || dep->number == 1)
1274 return dwc3_gadget_ep0_set_halt(ep, 1);
1275 else
1276 return dwc3_gadget_ep_set_halt(ep, 1);
Felipe Balbi72246da2011-08-19 18:10:58 +03001277}
1278
1279/* -------------------------------------------------------------------------- */
1280
1281static struct usb_endpoint_descriptor dwc3_gadget_ep0_desc = {
1282 .bLength = USB_DT_ENDPOINT_SIZE,
1283 .bDescriptorType = USB_DT_ENDPOINT,
1284 .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
1285};
1286
1287static const struct usb_ep_ops dwc3_gadget_ep0_ops = {
1288 .enable = dwc3_gadget_ep0_enable,
1289 .disable = dwc3_gadget_ep0_disable,
1290 .alloc_request = dwc3_gadget_ep_alloc_request,
1291 .free_request = dwc3_gadget_ep_free_request,
1292 .queue = dwc3_gadget_ep0_queue,
1293 .dequeue = dwc3_gadget_ep_dequeue,
Pratyush Anand08f0d962012-06-25 22:40:43 +05301294 .set_halt = dwc3_gadget_ep0_set_halt,
Felipe Balbi72246da2011-08-19 18:10:58 +03001295 .set_wedge = dwc3_gadget_ep_set_wedge,
1296};
1297
1298static const struct usb_ep_ops dwc3_gadget_ep_ops = {
1299 .enable = dwc3_gadget_ep_enable,
1300 .disable = dwc3_gadget_ep_disable,
1301 .alloc_request = dwc3_gadget_ep_alloc_request,
1302 .free_request = dwc3_gadget_ep_free_request,
1303 .queue = dwc3_gadget_ep_queue,
1304 .dequeue = dwc3_gadget_ep_dequeue,
1305 .set_halt = dwc3_gadget_ep_set_halt,
1306 .set_wedge = dwc3_gadget_ep_set_wedge,
1307};
1308
1309/* -------------------------------------------------------------------------- */
1310
1311static int dwc3_gadget_get_frame(struct usb_gadget *g)
1312{
1313 struct dwc3 *dwc = gadget_to_dwc(g);
1314 u32 reg;
1315
1316 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1317 return DWC3_DSTS_SOFFN(reg);
1318}
1319
1320static int dwc3_gadget_wakeup(struct usb_gadget *g)
1321{
1322 struct dwc3 *dwc = gadget_to_dwc(g);
1323
1324 unsigned long timeout;
1325 unsigned long flags;
1326
1327 u32 reg;
1328
1329 int ret = 0;
1330
1331 u8 link_state;
1332 u8 speed;
1333
1334 spin_lock_irqsave(&dwc->lock, flags);
1335
1336 /*
1337 * According to the Databook Remote wakeup request should
1338 * be issued only when the device is in early suspend state.
1339 *
1340 * We can check that via USB Link State bits in DSTS register.
1341 */
1342 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1343
1344 speed = reg & DWC3_DSTS_CONNECTSPD;
1345 if (speed == DWC3_DSTS_SUPERSPEED) {
1346 dev_dbg(dwc->dev, "no wakeup on SuperSpeed\n");
1347 ret = -EINVAL;
1348 goto out;
1349 }
1350
1351 link_state = DWC3_DSTS_USBLNKST(reg);
1352
1353 switch (link_state) {
1354 case DWC3_LINK_STATE_RX_DET: /* in HS, means Early Suspend */
1355 case DWC3_LINK_STATE_U3: /* in HS, means SUSPEND */
1356 break;
1357 default:
1358 dev_dbg(dwc->dev, "can't wakeup from link state %d\n",
1359 link_state);
1360 ret = -EINVAL;
1361 goto out;
1362 }
1363
Felipe Balbi8598bde2012-01-02 18:55:57 +02001364 ret = dwc3_gadget_set_link_state(dwc, DWC3_LINK_STATE_RECOV);
1365 if (ret < 0) {
1366 dev_err(dwc->dev, "failed to put link in Recovery\n");
1367 goto out;
1368 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001369
Paul Zimmerman802fde92012-04-27 13:10:52 +03001370 /* Recent versions do this automatically */
1371 if (dwc->revision < DWC3_REVISION_194A) {
1372 /* write zeroes to Link Change Request */
Felipe Balbifcc023c2012-05-24 10:27:56 +03001373 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
Paul Zimmerman802fde92012-04-27 13:10:52 +03001374 reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK;
1375 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1376 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001377
Paul Zimmerman1d046792012-02-15 18:56:56 -08001378 /* poll until Link State changes to ON */
Felipe Balbi72246da2011-08-19 18:10:58 +03001379 timeout = jiffies + msecs_to_jiffies(100);
1380
Paul Zimmerman1d046792012-02-15 18:56:56 -08001381 while (!time_after(jiffies, timeout)) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001382 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1383
1384 /* in HS, means ON */
1385 if (DWC3_DSTS_USBLNKST(reg) == DWC3_LINK_STATE_U0)
1386 break;
1387 }
1388
1389 if (DWC3_DSTS_USBLNKST(reg) != DWC3_LINK_STATE_U0) {
1390 dev_err(dwc->dev, "failed to send remote wakeup\n");
1391 ret = -EINVAL;
1392 }
1393
1394out:
1395 spin_unlock_irqrestore(&dwc->lock, flags);
1396
1397 return ret;
1398}
1399
1400static int dwc3_gadget_set_selfpowered(struct usb_gadget *g,
1401 int is_selfpowered)
1402{
1403 struct dwc3 *dwc = gadget_to_dwc(g);
Paul Zimmerman249a4562012-02-24 17:32:16 -08001404 unsigned long flags;
Felipe Balbi72246da2011-08-19 18:10:58 +03001405
Paul Zimmerman249a4562012-02-24 17:32:16 -08001406 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001407 dwc->is_selfpowered = !!is_selfpowered;
Paul Zimmerman249a4562012-02-24 17:32:16 -08001408 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001409
1410 return 0;
1411}
1412
Pratyush Anand6f17f742012-07-02 10:21:55 +05301413static int dwc3_gadget_run_stop(struct dwc3 *dwc, int is_on)
Felipe Balbi72246da2011-08-19 18:10:58 +03001414{
1415 u32 reg;
Sebastian Andrzej Siewior61d58242011-08-29 16:46:38 +02001416 u32 timeout = 500;
Felipe Balbi72246da2011-08-19 18:10:58 +03001417
1418 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
Felipe Balbi8db7ed12012-01-18 18:32:29 +02001419 if (is_on) {
Paul Zimmerman802fde92012-04-27 13:10:52 +03001420 if (dwc->revision <= DWC3_REVISION_187A) {
1421 reg &= ~DWC3_DCTL_TRGTULST_MASK;
1422 reg |= DWC3_DCTL_TRGTULST_RX_DET;
1423 }
1424
1425 if (dwc->revision >= DWC3_REVISION_194A)
1426 reg &= ~DWC3_DCTL_KEEP_CONNECT;
1427 reg |= DWC3_DCTL_RUN_STOP;
Felipe Balbi8db7ed12012-01-18 18:32:29 +02001428 } else {
Felipe Balbi72246da2011-08-19 18:10:58 +03001429 reg &= ~DWC3_DCTL_RUN_STOP;
Felipe Balbi8db7ed12012-01-18 18:32:29 +02001430 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001431
1432 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1433
1434 do {
1435 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1436 if (is_on) {
1437 if (!(reg & DWC3_DSTS_DEVCTRLHLT))
1438 break;
1439 } else {
1440 if (reg & DWC3_DSTS_DEVCTRLHLT)
1441 break;
1442 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001443 timeout--;
1444 if (!timeout)
Pratyush Anand6f17f742012-07-02 10:21:55 +05301445 return -ETIMEDOUT;
Sebastian Andrzej Siewior61d58242011-08-29 16:46:38 +02001446 udelay(1);
Felipe Balbi72246da2011-08-19 18:10:58 +03001447 } while (1);
1448
1449 dev_vdbg(dwc->dev, "gadget %s data soft-%s\n",
1450 dwc->gadget_driver
1451 ? dwc->gadget_driver->function : "no-function",
1452 is_on ? "connect" : "disconnect");
Pratyush Anand6f17f742012-07-02 10:21:55 +05301453
1454 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +03001455}
1456
1457static int dwc3_gadget_pullup(struct usb_gadget *g, int is_on)
1458{
1459 struct dwc3 *dwc = gadget_to_dwc(g);
1460 unsigned long flags;
Pratyush Anand6f17f742012-07-02 10:21:55 +05301461 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001462
1463 is_on = !!is_on;
1464
1465 spin_lock_irqsave(&dwc->lock, flags);
Pratyush Anand6f17f742012-07-02 10:21:55 +05301466 ret = dwc3_gadget_run_stop(dwc, is_on);
Felipe Balbi72246da2011-08-19 18:10:58 +03001467 spin_unlock_irqrestore(&dwc->lock, flags);
1468
Pratyush Anand6f17f742012-07-02 10:21:55 +05301469 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001470}
1471
1472static int dwc3_gadget_start(struct usb_gadget *g,
1473 struct usb_gadget_driver *driver)
1474{
1475 struct dwc3 *dwc = gadget_to_dwc(g);
1476 struct dwc3_ep *dep;
1477 unsigned long flags;
1478 int ret = 0;
1479 u32 reg;
1480
1481 spin_lock_irqsave(&dwc->lock, flags);
1482
1483 if (dwc->gadget_driver) {
1484 dev_err(dwc->dev, "%s is already bound to %s\n",
1485 dwc->gadget.name,
1486 dwc->gadget_driver->driver.name);
1487 ret = -EBUSY;
1488 goto err0;
1489 }
1490
1491 dwc->gadget_driver = driver;
1492 dwc->gadget.dev.driver = &driver->driver;
1493
Felipe Balbi72246da2011-08-19 18:10:58 +03001494 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
1495 reg &= ~(DWC3_DCFG_SPEED_MASK);
Felipe Balbi07e7f472012-03-23 12:20:31 +02001496
1497 /**
1498 * WORKAROUND: DWC3 revision < 2.20a have an issue
1499 * which would cause metastability state on Run/Stop
1500 * bit if we try to force the IP to USB2-only mode.
1501 *
1502 * Because of that, we cannot configure the IP to any
1503 * speed other than the SuperSpeed
1504 *
1505 * Refers to:
1506 *
1507 * STAR#9000525659: Clock Domain Crossing on DCTL in
1508 * USB 2.0 Mode
1509 */
1510 if (dwc->revision < DWC3_REVISION_220A)
1511 reg |= DWC3_DCFG_SUPERSPEED;
1512 else
1513 reg |= dwc->maximum_speed;
Felipe Balbi72246da2011-08-19 18:10:58 +03001514 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
1515
Paul Zimmermanb23c8432011-09-30 10:58:42 +03001516 dwc->start_config_issued = false;
1517
Felipe Balbi72246da2011-08-19 18:10:58 +03001518 /* Start with SuperSpeed Default */
1519 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
1520
1521 dep = dwc->eps[0];
Felipe Balbi4b345c92012-07-16 14:08:16 +03001522 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, false);
Felipe Balbi72246da2011-08-19 18:10:58 +03001523 if (ret) {
1524 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
1525 goto err0;
1526 }
1527
1528 dep = dwc->eps[1];
Felipe Balbi4b345c92012-07-16 14:08:16 +03001529 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, false);
Felipe Balbi72246da2011-08-19 18:10:58 +03001530 if (ret) {
1531 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
1532 goto err1;
1533 }
1534
1535 /* begin to receive SETUP packets */
Felipe Balbic7fcdeb2011-08-27 22:28:36 +03001536 dwc->ep0state = EP0_SETUP_PHASE;
Felipe Balbi72246da2011-08-19 18:10:58 +03001537 dwc3_ep0_out_start(dwc);
1538
1539 spin_unlock_irqrestore(&dwc->lock, flags);
1540
1541 return 0;
1542
1543err1:
1544 __dwc3_gadget_ep_disable(dwc->eps[0]);
1545
1546err0:
1547 spin_unlock_irqrestore(&dwc->lock, flags);
1548
1549 return ret;
1550}
1551
1552static int dwc3_gadget_stop(struct usb_gadget *g,
1553 struct usb_gadget_driver *driver)
1554{
1555 struct dwc3 *dwc = gadget_to_dwc(g);
1556 unsigned long flags;
1557
1558 spin_lock_irqsave(&dwc->lock, flags);
1559
1560 __dwc3_gadget_ep_disable(dwc->eps[0]);
1561 __dwc3_gadget_ep_disable(dwc->eps[1]);
1562
1563 dwc->gadget_driver = NULL;
1564 dwc->gadget.dev.driver = NULL;
1565
1566 spin_unlock_irqrestore(&dwc->lock, flags);
1567
1568 return 0;
1569}
Paul Zimmerman802fde92012-04-27 13:10:52 +03001570
Felipe Balbi72246da2011-08-19 18:10:58 +03001571static const struct usb_gadget_ops dwc3_gadget_ops = {
1572 .get_frame = dwc3_gadget_get_frame,
1573 .wakeup = dwc3_gadget_wakeup,
1574 .set_selfpowered = dwc3_gadget_set_selfpowered,
1575 .pullup = dwc3_gadget_pullup,
1576 .udc_start = dwc3_gadget_start,
1577 .udc_stop = dwc3_gadget_stop,
1578};
1579
1580/* -------------------------------------------------------------------------- */
1581
Bill Pemberton41ac7b32012-11-19 13:21:48 -05001582static int dwc3_gadget_init_endpoints(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +03001583{
1584 struct dwc3_ep *dep;
1585 u8 epnum;
1586
1587 INIT_LIST_HEAD(&dwc->gadget.ep_list);
1588
1589 for (epnum = 0; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
1590 dep = kzalloc(sizeof(*dep), GFP_KERNEL);
1591 if (!dep) {
1592 dev_err(dwc->dev, "can't allocate endpoint %d\n",
1593 epnum);
1594 return -ENOMEM;
1595 }
1596
1597 dep->dwc = dwc;
1598 dep->number = epnum;
1599 dwc->eps[epnum] = dep;
1600
1601 snprintf(dep->name, sizeof(dep->name), "ep%d%s", epnum >> 1,
1602 (epnum & 1) ? "in" : "out");
1603 dep->endpoint.name = dep->name;
1604 dep->direction = (epnum & 1);
1605
1606 if (epnum == 0 || epnum == 1) {
1607 dep->endpoint.maxpacket = 512;
1608 dep->endpoint.ops = &dwc3_gadget_ep0_ops;
1609 if (!epnum)
1610 dwc->gadget.ep0 = &dep->endpoint;
1611 } else {
1612 int ret;
1613
1614 dep->endpoint.maxpacket = 1024;
Sebastian Andrzej Siewior12d36c12011-11-03 20:27:50 +01001615 dep->endpoint.max_streams = 15;
Felipe Balbi72246da2011-08-19 18:10:58 +03001616 dep->endpoint.ops = &dwc3_gadget_ep_ops;
1617 list_add_tail(&dep->endpoint.ep_list,
1618 &dwc->gadget.ep_list);
1619
1620 ret = dwc3_alloc_trb_pool(dep);
Felipe Balbi25b8ff62011-11-04 12:32:47 +02001621 if (ret)
Felipe Balbi72246da2011-08-19 18:10:58 +03001622 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001623 }
Felipe Balbi25b8ff62011-11-04 12:32:47 +02001624
Felipe Balbi72246da2011-08-19 18:10:58 +03001625 INIT_LIST_HEAD(&dep->request_list);
1626 INIT_LIST_HEAD(&dep->req_queued);
1627 }
1628
1629 return 0;
1630}
1631
1632static void dwc3_gadget_free_endpoints(struct dwc3 *dwc)
1633{
1634 struct dwc3_ep *dep;
1635 u8 epnum;
1636
1637 for (epnum = 0; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
1638 dep = dwc->eps[epnum];
1639 dwc3_free_trb_pool(dep);
1640
1641 if (epnum != 0 && epnum != 1)
1642 list_del(&dep->endpoint.ep_list);
1643
1644 kfree(dep);
1645 }
1646}
1647
1648static void dwc3_gadget_release(struct device *dev)
1649{
1650 dev_dbg(dev, "%s\n", __func__);
1651}
1652
1653/* -------------------------------------------------------------------------- */
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05301654static int __dwc3_cleanup_done_trbs(struct dwc3 *dwc, struct dwc3_ep *dep,
1655 struct dwc3_request *req, struct dwc3_trb *trb,
1656 const struct dwc3_event_depevt *event, int status)
1657{
1658 unsigned int count;
1659 unsigned int s_pkt = 0;
1660 unsigned int trb_status;
1661
1662 if ((trb->ctrl & DWC3_TRB_CTRL_HWO) && status != -ESHUTDOWN)
1663 /*
1664 * We continue despite the error. There is not much we
1665 * can do. If we don't clean it up we loop forever. If
1666 * we skip the TRB then it gets overwritten after a
1667 * while since we use them in a ring buffer. A BUG()
1668 * would help. Lets hope that if this occurs, someone
1669 * fixes the root cause instead of looking away :)
1670 */
1671 dev_err(dwc->dev, "%s's TRB (%p) still owned by HW\n",
1672 dep->name, trb);
1673 count = trb->size & DWC3_TRB_SIZE_MASK;
1674
1675 if (dep->direction) {
1676 if (count) {
1677 trb_status = DWC3_TRB_SIZE_TRBSTS(trb->size);
1678 if (trb_status == DWC3_TRBSTS_MISSED_ISOC) {
1679 dev_dbg(dwc->dev, "incomplete IN transfer %s\n",
1680 dep->name);
1681 /*
1682 * If missed isoc occurred and there is
1683 * no request queued then issue END
1684 * TRANSFER, so that core generates
1685 * next xfernotready and we will issue
1686 * a fresh START TRANSFER.
1687 * If there are still queued request
1688 * then wait, do not issue either END
1689 * or UPDATE TRANSFER, just attach next
1690 * request in request_list during
1691 * giveback.If any future queued request
1692 * is successfully transferred then we
1693 * will issue UPDATE TRANSFER for all
1694 * request in the request_list.
1695 */
1696 dep->flags |= DWC3_EP_MISSED_ISOC;
1697 } else {
1698 dev_err(dwc->dev, "incomplete IN transfer %s\n",
1699 dep->name);
1700 status = -ECONNRESET;
1701 }
1702 } else {
1703 dep->flags &= ~DWC3_EP_MISSED_ISOC;
1704 }
1705 } else {
1706 if (count && (event->status & DEPEVT_STATUS_SHORT))
1707 s_pkt = 1;
1708 }
1709
1710 /*
1711 * We assume here we will always receive the entire data block
1712 * which we should receive. Meaning, if we program RX to
1713 * receive 4K but we receive only 2K, we assume that's all we
1714 * should receive and we simply bounce the request back to the
1715 * gadget driver for further processing.
1716 */
1717 req->request.actual += req->request.length - count;
1718 if (s_pkt)
1719 return 1;
1720 if ((event->status & DEPEVT_STATUS_LST) &&
1721 (trb->ctrl & (DWC3_TRB_CTRL_LST |
1722 DWC3_TRB_CTRL_HWO)))
1723 return 1;
1724 if ((event->status & DEPEVT_STATUS_IOC) &&
1725 (trb->ctrl & DWC3_TRB_CTRL_IOC))
1726 return 1;
1727 return 0;
1728}
1729
Felipe Balbi72246da2011-08-19 18:10:58 +03001730static int dwc3_cleanup_done_reqs(struct dwc3 *dwc, struct dwc3_ep *dep,
1731 const struct dwc3_event_depevt *event, int status)
1732{
1733 struct dwc3_request *req;
Felipe Balbif6bafc62012-02-06 11:04:53 +02001734 struct dwc3_trb *trb;
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05301735 unsigned int slot;
1736 unsigned int i;
1737 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001738
1739 do {
1740 req = next_request(&dep->req_queued);
Sebastian Andrzej Siewiord39ee7b2011-11-03 10:32:20 +01001741 if (!req) {
1742 WARN_ON_ONCE(1);
1743 return 1;
1744 }
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05301745 i = 0;
1746 do {
1747 slot = req->start_slot + i;
1748 if ((slot == DWC3_TRB_NUM - 1) &&
1749 usb_endpoint_xfer_isoc(dep->endpoint.desc))
1750 slot++;
1751 slot %= DWC3_TRB_NUM;
1752 trb = &dep->trb_pool[slot];
Felipe Balbi72246da2011-08-19 18:10:58 +03001753
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05301754 ret = __dwc3_cleanup_done_trbs(dwc, dep, req, trb,
1755 event, status);
1756 if (ret)
1757 break;
1758 }while (++i < req->request.num_mapped_sgs);
Felipe Balbi72246da2011-08-19 18:10:58 +03001759
Felipe Balbi72246da2011-08-19 18:10:58 +03001760 dwc3_gadget_giveback(dep, req, status);
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05301761
1762 if (ret)
Felipe Balbi72246da2011-08-19 18:10:58 +03001763 break;
1764 } while (1);
1765
Pratyush Anandcdc359d2013-01-14 15:59:34 +05301766 if (usb_endpoint_xfer_isoc(dep->endpoint.desc) &&
1767 list_empty(&dep->req_queued)) {
1768 if (list_empty(&dep->request_list)) {
1769 /*
1770 * If there is no entry in request list then do
1771 * not issue END TRANSFER now. Just set PENDING
1772 * flag, so that END TRANSFER is issued when an
1773 * entry is added into request list.
1774 */
1775 dep->flags = DWC3_EP_PENDING_REQUEST;
1776 } else {
1777 dwc3_stop_active_transfer(dwc, dep->number);
1778 dep->flags = DWC3_EP_ENABLED;
1779 }
Pratyush Anand7efea862013-01-14 15:59:32 +05301780 return 1;
1781 }
1782
Felipe Balbif6bafc62012-02-06 11:04:53 +02001783 if ((event->status & DEPEVT_STATUS_IOC) &&
1784 (trb->ctrl & DWC3_TRB_CTRL_IOC))
Felipe Balbi72246da2011-08-19 18:10:58 +03001785 return 0;
1786 return 1;
1787}
1788
1789static void dwc3_endpoint_transfer_complete(struct dwc3 *dwc,
1790 struct dwc3_ep *dep, const struct dwc3_event_depevt *event,
1791 int start_new)
1792{
1793 unsigned status = 0;
1794 int clean_busy;
1795
1796 if (event->status & DEPEVT_STATUS_BUSERR)
1797 status = -ECONNRESET;
1798
Paul Zimmerman1d046792012-02-15 18:56:56 -08001799 clean_busy = dwc3_cleanup_done_reqs(dwc, dep, event, status);
Paul Zimmermanc2df85c2012-02-24 17:32:18 -08001800 if (clean_busy)
Felipe Balbi72246da2011-08-19 18:10:58 +03001801 dep->flags &= ~DWC3_EP_BUSY;
Felipe Balbifae2b902011-10-14 13:00:30 +03001802
1803 /*
1804 * WORKAROUND: This is the 2nd half of U1/U2 -> U0 workaround.
1805 * See dwc3_gadget_linksts_change_interrupt() for 1st half.
1806 */
1807 if (dwc->revision < DWC3_REVISION_183A) {
1808 u32 reg;
1809 int i;
1810
1811 for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) {
Moiz Sonasath348e0262012-08-01 14:08:30 -05001812 dep = dwc->eps[i];
Felipe Balbifae2b902011-10-14 13:00:30 +03001813
1814 if (!(dep->flags & DWC3_EP_ENABLED))
1815 continue;
1816
1817 if (!list_empty(&dep->req_queued))
1818 return;
1819 }
1820
1821 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
1822 reg |= dwc->u1u2;
1823 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1824
1825 dwc->u1u2 = 0;
1826 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001827}
1828
Felipe Balbi72246da2011-08-19 18:10:58 +03001829static void dwc3_endpoint_interrupt(struct dwc3 *dwc,
1830 const struct dwc3_event_depevt *event)
1831{
1832 struct dwc3_ep *dep;
1833 u8 epnum = event->endpoint_number;
1834
1835 dep = dwc->eps[epnum];
1836
Felipe Balbi3336abb2012-06-06 09:19:35 +03001837 if (!(dep->flags & DWC3_EP_ENABLED))
1838 return;
1839
Felipe Balbi72246da2011-08-19 18:10:58 +03001840 dev_vdbg(dwc->dev, "%s: %s\n", dep->name,
1841 dwc3_ep_event_string(event->endpoint_event));
1842
1843 if (epnum == 0 || epnum == 1) {
1844 dwc3_ep0_interrupt(dwc, event);
1845 return;
1846 }
1847
1848 switch (event->endpoint_event) {
1849 case DWC3_DEPEVT_XFERCOMPLETE:
Felipe Balbib4996a82012-06-06 12:04:13 +03001850 dep->resource_index = 0;
Paul Zimmermanc2df85c2012-02-24 17:32:18 -08001851
Ido Shayevitz16e78db2012-03-12 20:25:24 +02001852 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001853 dev_dbg(dwc->dev, "%s is an Isochronous endpoint\n",
1854 dep->name);
1855 return;
1856 }
1857
1858 dwc3_endpoint_transfer_complete(dwc, dep, event, 1);
1859 break;
1860 case DWC3_DEPEVT_XFERINPROGRESS:
Ido Shayevitz16e78db2012-03-12 20:25:24 +02001861 if (!usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001862 dev_dbg(dwc->dev, "%s is not an Isochronous endpoint\n",
1863 dep->name);
1864 return;
1865 }
1866
1867 dwc3_endpoint_transfer_complete(dwc, dep, event, 0);
1868 break;
1869 case DWC3_DEPEVT_XFERNOTREADY:
Ido Shayevitz16e78db2012-03-12 20:25:24 +02001870 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001871 dwc3_gadget_start_isoc(dwc, dep, event);
1872 } else {
1873 int ret;
1874
1875 dev_vdbg(dwc->dev, "%s: reason %s\n",
Felipe Balbi40aa41fb2012-01-18 17:06:03 +02001876 dep->name, event->status &
1877 DEPEVT_STATUS_TRANSFER_ACTIVE
Felipe Balbi72246da2011-08-19 18:10:58 +03001878 ? "Transfer Active"
1879 : "Transfer Not Active");
1880
1881 ret = __dwc3_gadget_kick_transfer(dep, 0, 1);
1882 if (!ret || ret == -EBUSY)
1883 return;
1884
1885 dev_dbg(dwc->dev, "%s: failed to kick transfers\n",
1886 dep->name);
1887 }
1888
1889 break;
Felipe Balbi879631a2011-09-30 10:58:47 +03001890 case DWC3_DEPEVT_STREAMEVT:
Ido Shayevitz16e78db2012-03-12 20:25:24 +02001891 if (!usb_endpoint_xfer_bulk(dep->endpoint.desc)) {
Felipe Balbi879631a2011-09-30 10:58:47 +03001892 dev_err(dwc->dev, "Stream event for non-Bulk %s\n",
1893 dep->name);
1894 return;
1895 }
1896
1897 switch (event->status) {
1898 case DEPEVT_STREAMEVT_FOUND:
1899 dev_vdbg(dwc->dev, "Stream %d found and started\n",
1900 event->parameters);
1901
1902 break;
1903 case DEPEVT_STREAMEVT_NOTFOUND:
1904 /* FALLTHROUGH */
1905 default:
1906 dev_dbg(dwc->dev, "Couldn't find suitable stream\n");
1907 }
1908 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03001909 case DWC3_DEPEVT_RXTXFIFOEVT:
1910 dev_dbg(dwc->dev, "%s FIFO Overrun\n", dep->name);
1911 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03001912 case DWC3_DEPEVT_EPCMDCMPLT:
Felipe Balbiea53b882012-02-17 12:10:04 +02001913 dev_vdbg(dwc->dev, "Endpoint Command Complete\n");
Felipe Balbi72246da2011-08-19 18:10:58 +03001914 break;
1915 }
1916}
1917
1918static void dwc3_disconnect_gadget(struct dwc3 *dwc)
1919{
1920 if (dwc->gadget_driver && dwc->gadget_driver->disconnect) {
1921 spin_unlock(&dwc->lock);
1922 dwc->gadget_driver->disconnect(&dwc->gadget);
1923 spin_lock(&dwc->lock);
1924 }
1925}
1926
1927static void dwc3_stop_active_transfer(struct dwc3 *dwc, u32 epnum)
1928{
1929 struct dwc3_ep *dep;
1930 struct dwc3_gadget_ep_cmd_params params;
1931 u32 cmd;
1932 int ret;
1933
1934 dep = dwc->eps[epnum];
1935
Felipe Balbib4996a82012-06-06 12:04:13 +03001936 if (!dep->resource_index)
Pratyush Anand3daf74d2012-06-23 02:23:08 +05301937 return;
1938
Pratyush Anand57911502012-07-06 15:19:10 +05301939 /*
1940 * NOTICE: We are violating what the Databook says about the
1941 * EndTransfer command. Ideally we would _always_ wait for the
1942 * EndTransfer Command Completion IRQ, but that's causing too
1943 * much trouble synchronizing between us and gadget driver.
1944 *
1945 * We have discussed this with the IP Provider and it was
1946 * suggested to giveback all requests here, but give HW some
1947 * extra time to synchronize with the interconnect. We're using
1948 * an arbitraty 100us delay for that.
1949 *
1950 * Note also that a similar handling was tested by Synopsys
1951 * (thanks a lot Paul) and nothing bad has come out of it.
1952 * In short, what we're doing is:
1953 *
1954 * - Issue EndTransfer WITH CMDIOC bit set
1955 * - Wait 100us
1956 */
1957
Pratyush Anand3daf74d2012-06-23 02:23:08 +05301958 cmd = DWC3_DEPCMD_ENDTRANSFER;
1959 cmd |= DWC3_DEPCMD_HIPRI_FORCERM | DWC3_DEPCMD_CMDIOC;
Felipe Balbib4996a82012-06-06 12:04:13 +03001960 cmd |= DWC3_DEPCMD_PARAM(dep->resource_index);
Pratyush Anand3daf74d2012-06-23 02:23:08 +05301961 memset(&params, 0, sizeof(params));
1962 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number, cmd, &params);
1963 WARN_ON_ONCE(ret);
Felipe Balbib4996a82012-06-06 12:04:13 +03001964 dep->resource_index = 0;
Felipe Balbi041d81f2012-10-04 11:58:00 +03001965 dep->flags &= ~DWC3_EP_BUSY;
Pratyush Anand57911502012-07-06 15:19:10 +05301966 udelay(100);
Felipe Balbi72246da2011-08-19 18:10:58 +03001967}
1968
1969static void dwc3_stop_active_transfers(struct dwc3 *dwc)
1970{
1971 u32 epnum;
1972
1973 for (epnum = 2; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
1974 struct dwc3_ep *dep;
1975
1976 dep = dwc->eps[epnum];
1977 if (!(dep->flags & DWC3_EP_ENABLED))
1978 continue;
1979
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +02001980 dwc3_remove_requests(dwc, dep);
Felipe Balbi72246da2011-08-19 18:10:58 +03001981 }
1982}
1983
1984static void dwc3_clear_stall_all_ep(struct dwc3 *dwc)
1985{
1986 u32 epnum;
1987
1988 for (epnum = 1; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
1989 struct dwc3_ep *dep;
1990 struct dwc3_gadget_ep_cmd_params params;
1991 int ret;
1992
1993 dep = dwc->eps[epnum];
1994
1995 if (!(dep->flags & DWC3_EP_STALL))
1996 continue;
1997
1998 dep->flags &= ~DWC3_EP_STALL;
1999
2000 memset(&params, 0, sizeof(params));
2001 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number,
2002 DWC3_DEPCMD_CLEARSTALL, &params);
2003 WARN_ON_ONCE(ret);
2004 }
2005}
2006
2007static void dwc3_gadget_disconnect_interrupt(struct dwc3 *dwc)
2008{
Felipe Balbic4430a22012-05-24 10:30:01 +03002009 int reg;
2010
Felipe Balbi72246da2011-08-19 18:10:58 +03002011 dev_vdbg(dwc->dev, "%s\n", __func__);
Felipe Balbi72246da2011-08-19 18:10:58 +03002012
2013 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2014 reg &= ~DWC3_DCTL_INITU1ENA;
2015 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2016
2017 reg &= ~DWC3_DCTL_INITU2ENA;
2018 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +03002019
Felipe Balbi72246da2011-08-19 18:10:58 +03002020 dwc3_disconnect_gadget(dwc);
Paul Zimmermanb23c8432011-09-30 10:58:42 +03002021 dwc->start_config_issued = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03002022
2023 dwc->gadget.speed = USB_SPEED_UNKNOWN;
Felipe Balbidf62df52011-10-14 15:11:49 +03002024 dwc->setup_packet_pending = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03002025}
2026
Paul Zimmermand7a46a82012-04-27 12:54:05 +03002027static void dwc3_gadget_usb3_phy_suspend(struct dwc3 *dwc, int suspend)
Felipe Balbi72246da2011-08-19 18:10:58 +03002028{
2029 u32 reg;
2030
2031 reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0));
2032
Paul Zimmermand7a46a82012-04-27 12:54:05 +03002033 if (suspend)
Felipe Balbi72246da2011-08-19 18:10:58 +03002034 reg |= DWC3_GUSB3PIPECTL_SUSPHY;
Paul Zimmermand7a46a82012-04-27 12:54:05 +03002035 else
2036 reg &= ~DWC3_GUSB3PIPECTL_SUSPHY;
Felipe Balbi72246da2011-08-19 18:10:58 +03002037
2038 dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg);
2039}
2040
Paul Zimmermand7a46a82012-04-27 12:54:05 +03002041static void dwc3_gadget_usb2_phy_suspend(struct dwc3 *dwc, int suspend)
Felipe Balbi72246da2011-08-19 18:10:58 +03002042{
2043 u32 reg;
2044
2045 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
2046
Paul Zimmermand7a46a82012-04-27 12:54:05 +03002047 if (suspend)
Felipe Balbi72246da2011-08-19 18:10:58 +03002048 reg |= DWC3_GUSB2PHYCFG_SUSPHY;
Paul Zimmermand7a46a82012-04-27 12:54:05 +03002049 else
2050 reg &= ~DWC3_GUSB2PHYCFG_SUSPHY;
Felipe Balbi72246da2011-08-19 18:10:58 +03002051
2052 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
2053}
2054
2055static void dwc3_gadget_reset_interrupt(struct dwc3 *dwc)
2056{
2057 u32 reg;
2058
2059 dev_vdbg(dwc->dev, "%s\n", __func__);
2060
Felipe Balbidf62df52011-10-14 15:11:49 +03002061 /*
2062 * WORKAROUND: DWC3 revisions <1.88a have an issue which
2063 * would cause a missing Disconnect Event if there's a
2064 * pending Setup Packet in the FIFO.
2065 *
2066 * There's no suggested workaround on the official Bug
2067 * report, which states that "unless the driver/application
2068 * is doing any special handling of a disconnect event,
2069 * there is no functional issue".
2070 *
2071 * Unfortunately, it turns out that we _do_ some special
2072 * handling of a disconnect event, namely complete all
2073 * pending transfers, notify gadget driver of the
2074 * disconnection, and so on.
2075 *
2076 * Our suggested workaround is to follow the Disconnect
2077 * Event steps here, instead, based on a setup_packet_pending
2078 * flag. Such flag gets set whenever we have a XferNotReady
2079 * event on EP0 and gets cleared on XferComplete for the
2080 * same endpoint.
2081 *
2082 * Refers to:
2083 *
2084 * STAR#9000466709: RTL: Device : Disconnect event not
2085 * generated if setup packet pending in FIFO
2086 */
2087 if (dwc->revision < DWC3_REVISION_188A) {
2088 if (dwc->setup_packet_pending)
2089 dwc3_gadget_disconnect_interrupt(dwc);
2090 }
2091
Felipe Balbi961906e2011-12-20 15:37:21 +02002092 /* after reset -> Default State */
2093 dwc->dev_state = DWC3_DEFAULT_STATE;
2094
Paul Zimmerman802fde92012-04-27 13:10:52 +03002095 /* Recent versions support automatic phy suspend and don't need this */
2096 if (dwc->revision < DWC3_REVISION_194A) {
2097 /* Resume PHYs */
2098 dwc3_gadget_usb2_phy_suspend(dwc, false);
2099 dwc3_gadget_usb3_phy_suspend(dwc, false);
2100 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002101
2102 if (dwc->gadget.speed != USB_SPEED_UNKNOWN)
2103 dwc3_disconnect_gadget(dwc);
2104
2105 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2106 reg &= ~DWC3_DCTL_TSTCTRL_MASK;
2107 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Gerard Cauvy3b637362012-02-10 12:21:18 +02002108 dwc->test_mode = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03002109
2110 dwc3_stop_active_transfers(dwc);
2111 dwc3_clear_stall_all_ep(dwc);
Paul Zimmermanb23c8432011-09-30 10:58:42 +03002112 dwc->start_config_issued = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03002113
2114 /* Reset device address to zero */
2115 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2116 reg &= ~(DWC3_DCFG_DEVADDR_MASK);
2117 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +03002118}
2119
2120static void dwc3_update_ram_clk_sel(struct dwc3 *dwc, u32 speed)
2121{
2122 u32 reg;
2123 u32 usb30_clock = DWC3_GCTL_CLK_BUS;
2124
2125 /*
2126 * We change the clock only at SS but I dunno why I would want to do
2127 * this. Maybe it becomes part of the power saving plan.
2128 */
2129
2130 if (speed != DWC3_DSTS_SUPERSPEED)
2131 return;
2132
2133 /*
2134 * RAMClkSel is reset to 0 after USB reset, so it must be reprogrammed
2135 * each time on Connect Done.
2136 */
2137 if (!usb30_clock)
2138 return;
2139
2140 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
2141 reg |= DWC3_GCTL_RAMCLKSEL(usb30_clock);
2142 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
2143}
2144
Paul Zimmermand7a46a82012-04-27 12:54:05 +03002145static void dwc3_gadget_phy_suspend(struct dwc3 *dwc, u8 speed)
Felipe Balbi72246da2011-08-19 18:10:58 +03002146{
2147 switch (speed) {
2148 case USB_SPEED_SUPER:
Paul Zimmermand7a46a82012-04-27 12:54:05 +03002149 dwc3_gadget_usb2_phy_suspend(dwc, true);
Felipe Balbi72246da2011-08-19 18:10:58 +03002150 break;
2151 case USB_SPEED_HIGH:
2152 case USB_SPEED_FULL:
2153 case USB_SPEED_LOW:
Paul Zimmermand7a46a82012-04-27 12:54:05 +03002154 dwc3_gadget_usb3_phy_suspend(dwc, true);
Felipe Balbi72246da2011-08-19 18:10:58 +03002155 break;
2156 }
2157}
2158
2159static void dwc3_gadget_conndone_interrupt(struct dwc3 *dwc)
2160{
2161 struct dwc3_gadget_ep_cmd_params params;
2162 struct dwc3_ep *dep;
2163 int ret;
2164 u32 reg;
2165 u8 speed;
2166
2167 dev_vdbg(dwc->dev, "%s\n", __func__);
2168
2169 memset(&params, 0x00, sizeof(params));
2170
Felipe Balbi72246da2011-08-19 18:10:58 +03002171 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
2172 speed = reg & DWC3_DSTS_CONNECTSPD;
2173 dwc->speed = speed;
2174
2175 dwc3_update_ram_clk_sel(dwc, speed);
2176
2177 switch (speed) {
2178 case DWC3_DCFG_SUPERSPEED:
Felipe Balbi05870c52011-10-14 14:51:38 +03002179 /*
2180 * WORKAROUND: DWC3 revisions <1.90a have an issue which
2181 * would cause a missing USB3 Reset event.
2182 *
2183 * In such situations, we should force a USB3 Reset
2184 * event by calling our dwc3_gadget_reset_interrupt()
2185 * routine.
2186 *
2187 * Refers to:
2188 *
2189 * STAR#9000483510: RTL: SS : USB3 reset event may
2190 * not be generated always when the link enters poll
2191 */
2192 if (dwc->revision < DWC3_REVISION_190A)
2193 dwc3_gadget_reset_interrupt(dwc);
2194
Felipe Balbi72246da2011-08-19 18:10:58 +03002195 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
2196 dwc->gadget.ep0->maxpacket = 512;
2197 dwc->gadget.speed = USB_SPEED_SUPER;
2198 break;
2199 case DWC3_DCFG_HIGHSPEED:
2200 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
2201 dwc->gadget.ep0->maxpacket = 64;
2202 dwc->gadget.speed = USB_SPEED_HIGH;
2203 break;
2204 case DWC3_DCFG_FULLSPEED2:
2205 case DWC3_DCFG_FULLSPEED1:
2206 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
2207 dwc->gadget.ep0->maxpacket = 64;
2208 dwc->gadget.speed = USB_SPEED_FULL;
2209 break;
2210 case DWC3_DCFG_LOWSPEED:
2211 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(8);
2212 dwc->gadget.ep0->maxpacket = 8;
2213 dwc->gadget.speed = USB_SPEED_LOW;
2214 break;
2215 }
2216
Pratyush Anand2b758352013-01-14 15:59:31 +05302217 /* Enable USB2 LPM Capability */
2218
2219 if ((dwc->revision > DWC3_REVISION_194A)
2220 && (speed != DWC3_DCFG_SUPERSPEED)) {
2221 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2222 reg |= DWC3_DCFG_LPM_CAP;
2223 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
2224
2225 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2226 reg &= ~(DWC3_DCTL_HIRD_THRES_MASK | DWC3_DCTL_L1_HIBER_EN);
2227
Felipe Balbi1a947742013-01-24 11:56:11 +02002228 /*
2229 * TODO: This should be configurable. For now using
2230 * maximum allowed HIRD threshold value of 0b1100
2231 */
2232 reg |= DWC3_DCTL_HIRD_THRES(12);
Pratyush Anand2b758352013-01-14 15:59:31 +05302233
2234 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2235 }
2236
Paul Zimmerman802fde92012-04-27 13:10:52 +03002237 /* Recent versions support automatic phy suspend and don't need this */
2238 if (dwc->revision < DWC3_REVISION_194A) {
2239 /* Suspend unneeded PHY */
2240 dwc3_gadget_phy_suspend(dwc, dwc->gadget.speed);
2241 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002242
2243 dep = dwc->eps[0];
Felipe Balbi4b345c92012-07-16 14:08:16 +03002244 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, true);
Felipe Balbi72246da2011-08-19 18:10:58 +03002245 if (ret) {
2246 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
2247 return;
2248 }
2249
2250 dep = dwc->eps[1];
Felipe Balbi4b345c92012-07-16 14:08:16 +03002251 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, true);
Felipe Balbi72246da2011-08-19 18:10:58 +03002252 if (ret) {
2253 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
2254 return;
2255 }
2256
2257 /*
2258 * Configure PHY via GUSB3PIPECTLn if required.
2259 *
2260 * Update GTXFIFOSIZn
2261 *
2262 * In both cases reset values should be sufficient.
2263 */
2264}
2265
2266static void dwc3_gadget_wakeup_interrupt(struct dwc3 *dwc)
2267{
2268 dev_vdbg(dwc->dev, "%s\n", __func__);
2269
2270 /*
2271 * TODO take core out of low power mode when that's
2272 * implemented.
2273 */
2274
2275 dwc->gadget_driver->resume(&dwc->gadget);
2276}
2277
2278static void dwc3_gadget_linksts_change_interrupt(struct dwc3 *dwc,
2279 unsigned int evtinfo)
2280{
Felipe Balbifae2b902011-10-14 13:00:30 +03002281 enum dwc3_link_state next = evtinfo & DWC3_LINK_STATE_MASK;
2282
2283 /*
2284 * WORKAROUND: DWC3 Revisions <1.83a have an issue which, depending
2285 * on the link partner, the USB session might do multiple entry/exit
2286 * of low power states before a transfer takes place.
2287 *
2288 * Due to this problem, we might experience lower throughput. The
2289 * suggested workaround is to disable DCTL[12:9] bits if we're
2290 * transitioning from U1/U2 to U0 and enable those bits again
2291 * after a transfer completes and there are no pending transfers
2292 * on any of the enabled endpoints.
2293 *
2294 * This is the first half of that workaround.
2295 *
2296 * Refers to:
2297 *
2298 * STAR#9000446952: RTL: Device SS : if U1/U2 ->U0 takes >128us
2299 * core send LGO_Ux entering U0
2300 */
2301 if (dwc->revision < DWC3_REVISION_183A) {
2302 if (next == DWC3_LINK_STATE_U0) {
2303 u32 u1u2;
2304 u32 reg;
2305
2306 switch (dwc->link_state) {
2307 case DWC3_LINK_STATE_U1:
2308 case DWC3_LINK_STATE_U2:
2309 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2310 u1u2 = reg & (DWC3_DCTL_INITU2ENA
2311 | DWC3_DCTL_ACCEPTU2ENA
2312 | DWC3_DCTL_INITU1ENA
2313 | DWC3_DCTL_ACCEPTU1ENA);
2314
2315 if (!dwc->u1u2)
2316 dwc->u1u2 = reg & u1u2;
2317
2318 reg &= ~u1u2;
2319
2320 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2321 break;
2322 default:
2323 /* do nothing */
2324 break;
2325 }
2326 }
2327 }
2328
2329 dwc->link_state = next;
Felipe Balbi019ac832011-09-08 21:18:47 +03002330
2331 dev_vdbg(dwc->dev, "%s link %d\n", __func__, dwc->link_state);
Felipe Balbi72246da2011-08-19 18:10:58 +03002332}
2333
2334static void dwc3_gadget_interrupt(struct dwc3 *dwc,
2335 const struct dwc3_event_devt *event)
2336{
2337 switch (event->type) {
2338 case DWC3_DEVICE_EVENT_DISCONNECT:
2339 dwc3_gadget_disconnect_interrupt(dwc);
2340 break;
2341 case DWC3_DEVICE_EVENT_RESET:
2342 dwc3_gadget_reset_interrupt(dwc);
2343 break;
2344 case DWC3_DEVICE_EVENT_CONNECT_DONE:
2345 dwc3_gadget_conndone_interrupt(dwc);
2346 break;
2347 case DWC3_DEVICE_EVENT_WAKEUP:
2348 dwc3_gadget_wakeup_interrupt(dwc);
2349 break;
2350 case DWC3_DEVICE_EVENT_LINK_STATUS_CHANGE:
2351 dwc3_gadget_linksts_change_interrupt(dwc, event->event_info);
2352 break;
2353 case DWC3_DEVICE_EVENT_EOPF:
2354 dev_vdbg(dwc->dev, "End of Periodic Frame\n");
2355 break;
2356 case DWC3_DEVICE_EVENT_SOF:
2357 dev_vdbg(dwc->dev, "Start of Periodic Frame\n");
2358 break;
2359 case DWC3_DEVICE_EVENT_ERRATIC_ERROR:
2360 dev_vdbg(dwc->dev, "Erratic Error\n");
2361 break;
2362 case DWC3_DEVICE_EVENT_CMD_CMPL:
2363 dev_vdbg(dwc->dev, "Command Complete\n");
2364 break;
2365 case DWC3_DEVICE_EVENT_OVERFLOW:
2366 dev_vdbg(dwc->dev, "Overflow\n");
2367 break;
2368 default:
2369 dev_dbg(dwc->dev, "UNKNOWN IRQ %d\n", event->type);
2370 }
2371}
2372
2373static void dwc3_process_event_entry(struct dwc3 *dwc,
2374 const union dwc3_event *event)
2375{
2376 /* Endpoint IRQ, handle it and return early */
2377 if (event->type.is_devspec == 0) {
2378 /* depevt */
2379 return dwc3_endpoint_interrupt(dwc, &event->depevt);
2380 }
2381
2382 switch (event->type.type) {
2383 case DWC3_EVENT_TYPE_DEV:
2384 dwc3_gadget_interrupt(dwc, &event->devt);
2385 break;
2386 /* REVISIT what to do with Carkit and I2C events ? */
2387 default:
2388 dev_err(dwc->dev, "UNKNOWN IRQ type %d\n", event->raw);
2389 }
2390}
2391
2392static irqreturn_t dwc3_process_event_buf(struct dwc3 *dwc, u32 buf)
2393{
2394 struct dwc3_event_buffer *evt;
2395 int left;
2396 u32 count;
2397
2398 count = dwc3_readl(dwc->regs, DWC3_GEVNTCOUNT(buf));
2399 count &= DWC3_GEVNTCOUNT_MASK;
2400 if (!count)
2401 return IRQ_NONE;
2402
2403 evt = dwc->ev_buffs[buf];
2404 left = count;
2405
2406 while (left > 0) {
2407 union dwc3_event event;
2408
Felipe Balbid70d8442012-02-06 13:40:17 +02002409 event.raw = *(u32 *) (evt->buf + evt->lpos);
2410
Felipe Balbi72246da2011-08-19 18:10:58 +03002411 dwc3_process_event_entry(dwc, &event);
2412 /*
2413 * XXX we wrap around correctly to the next entry as almost all
2414 * entries are 4 bytes in size. There is one entry which has 12
2415 * bytes which is a regular entry followed by 8 bytes data. ATM
2416 * I don't know how things are organized if were get next to the
2417 * a boundary so I worry about that once we try to handle that.
2418 */
2419 evt->lpos = (evt->lpos + 4) % DWC3_EVENT_BUFFERS_SIZE;
2420 left -= 4;
2421
2422 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(buf), 4);
2423 }
2424
2425 return IRQ_HANDLED;
2426}
2427
2428static irqreturn_t dwc3_interrupt(int irq, void *_dwc)
2429{
2430 struct dwc3 *dwc = _dwc;
2431 int i;
2432 irqreturn_t ret = IRQ_NONE;
2433
2434 spin_lock(&dwc->lock);
2435
Felipe Balbi9f622b22011-10-12 10:31:04 +03002436 for (i = 0; i < dwc->num_event_buffers; i++) {
Felipe Balbi72246da2011-08-19 18:10:58 +03002437 irqreturn_t status;
2438
2439 status = dwc3_process_event_buf(dwc, i);
2440 if (status == IRQ_HANDLED)
2441 ret = status;
2442 }
2443
2444 spin_unlock(&dwc->lock);
2445
2446 return ret;
2447}
2448
2449/**
2450 * dwc3_gadget_init - Initializes gadget related registers
Paul Zimmerman1d046792012-02-15 18:56:56 -08002451 * @dwc: pointer to our controller context structure
Felipe Balbi72246da2011-08-19 18:10:58 +03002452 *
2453 * Returns 0 on success otherwise negative errno.
2454 */
Bill Pemberton41ac7b32012-11-19 13:21:48 -05002455int dwc3_gadget_init(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +03002456{
2457 u32 reg;
2458 int ret;
2459 int irq;
2460
2461 dwc->ctrl_req = dma_alloc_coherent(dwc->dev, sizeof(*dwc->ctrl_req),
2462 &dwc->ctrl_req_addr, GFP_KERNEL);
2463 if (!dwc->ctrl_req) {
2464 dev_err(dwc->dev, "failed to allocate ctrl request\n");
2465 ret = -ENOMEM;
2466 goto err0;
2467 }
2468
2469 dwc->ep0_trb = dma_alloc_coherent(dwc->dev, sizeof(*dwc->ep0_trb),
2470 &dwc->ep0_trb_addr, GFP_KERNEL);
2471 if (!dwc->ep0_trb) {
2472 dev_err(dwc->dev, "failed to allocate ep0 trb\n");
2473 ret = -ENOMEM;
2474 goto err1;
2475 }
2476
Felipe Balbi3ef35fa2012-05-04 12:58:14 +03002477 dwc->setup_buf = kzalloc(DWC3_EP0_BOUNCE_SIZE, GFP_KERNEL);
Felipe Balbi72246da2011-08-19 18:10:58 +03002478 if (!dwc->setup_buf) {
2479 dev_err(dwc->dev, "failed to allocate setup buffer\n");
2480 ret = -ENOMEM;
2481 goto err2;
2482 }
2483
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002484 dwc->ep0_bounce = dma_alloc_coherent(dwc->dev,
Felipe Balbi3ef35fa2012-05-04 12:58:14 +03002485 DWC3_EP0_BOUNCE_SIZE, &dwc->ep0_bounce_addr,
2486 GFP_KERNEL);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002487 if (!dwc->ep0_bounce) {
2488 dev_err(dwc->dev, "failed to allocate ep0 bounce buffer\n");
2489 ret = -ENOMEM;
2490 goto err3;
2491 }
2492
Felipe Balbi72246da2011-08-19 18:10:58 +03002493 dev_set_name(&dwc->gadget.dev, "gadget");
2494
2495 dwc->gadget.ops = &dwc3_gadget_ops;
Michal Nazarewiczd327ab52011-11-19 18:27:37 +01002496 dwc->gadget.max_speed = USB_SPEED_SUPER;
Felipe Balbi72246da2011-08-19 18:10:58 +03002497 dwc->gadget.speed = USB_SPEED_UNKNOWN;
2498 dwc->gadget.dev.parent = dwc->dev;
Felipe Balbieeb720f2011-11-28 12:46:59 +02002499 dwc->gadget.sg_supported = true;
Felipe Balbi72246da2011-08-19 18:10:58 +03002500
2501 dma_set_coherent_mask(&dwc->gadget.dev, dwc->dev->coherent_dma_mask);
2502
2503 dwc->gadget.dev.dma_parms = dwc->dev->dma_parms;
2504 dwc->gadget.dev.dma_mask = dwc->dev->dma_mask;
2505 dwc->gadget.dev.release = dwc3_gadget_release;
2506 dwc->gadget.name = "dwc3-gadget";
2507
2508 /*
2509 * REVISIT: Here we should clear all pending IRQs to be
2510 * sure we're starting from a well known location.
2511 */
2512
2513 ret = dwc3_gadget_init_endpoints(dwc);
2514 if (ret)
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002515 goto err4;
Felipe Balbi72246da2011-08-19 18:10:58 +03002516
2517 irq = platform_get_irq(to_platform_device(dwc->dev), 0);
2518
2519 ret = request_irq(irq, dwc3_interrupt, IRQF_SHARED,
2520 "dwc3", dwc);
2521 if (ret) {
2522 dev_err(dwc->dev, "failed to request irq #%d --> %d\n",
2523 irq, ret);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002524 goto err5;
Felipe Balbi72246da2011-08-19 18:10:58 +03002525 }
2526
Sebastian Andrzej Siewiore6a3b5e2011-09-13 17:54:39 +02002527 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2528 reg |= DWC3_DCFG_LPM_CAP;
2529 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
2530
Felipe Balbi72246da2011-08-19 18:10:58 +03002531 /* Enable all but Start and End of Frame IRQs */
2532 reg = (DWC3_DEVTEN_VNDRDEVTSTRCVEDEN |
2533 DWC3_DEVTEN_EVNTOVERFLOWEN |
2534 DWC3_DEVTEN_CMDCMPLTEN |
2535 DWC3_DEVTEN_ERRTICERREN |
2536 DWC3_DEVTEN_WKUPEVTEN |
2537 DWC3_DEVTEN_ULSTCNGEN |
2538 DWC3_DEVTEN_CONNECTDONEEN |
2539 DWC3_DEVTEN_USBRSTEN |
2540 DWC3_DEVTEN_DISCONNEVTEN);
2541 dwc3_writel(dwc->regs, DWC3_DEVTEN, reg);
2542
Pratyush Anand2b758352013-01-14 15:59:31 +05302543 /* automatic phy suspend only on recent versions */
Paul Zimmerman802fde92012-04-27 13:10:52 +03002544 if (dwc->revision >= DWC3_REVISION_194A) {
Pratyush Ananddcae3572012-06-06 19:36:17 +05302545 dwc3_gadget_usb2_phy_suspend(dwc, false);
2546 dwc3_gadget_usb3_phy_suspend(dwc, false);
Paul Zimmerman802fde92012-04-27 13:10:52 +03002547 }
2548
Felipe Balbi72246da2011-08-19 18:10:58 +03002549 ret = device_register(&dwc->gadget.dev);
2550 if (ret) {
2551 dev_err(dwc->dev, "failed to register gadget device\n");
2552 put_device(&dwc->gadget.dev);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002553 goto err6;
Felipe Balbi72246da2011-08-19 18:10:58 +03002554 }
2555
2556 ret = usb_add_gadget_udc(dwc->dev, &dwc->gadget);
2557 if (ret) {
2558 dev_err(dwc->dev, "failed to register udc\n");
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002559 goto err7;
Felipe Balbi72246da2011-08-19 18:10:58 +03002560 }
2561
2562 return 0;
2563
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002564err7:
Felipe Balbi72246da2011-08-19 18:10:58 +03002565 device_unregister(&dwc->gadget.dev);
2566
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002567err6:
Felipe Balbi72246da2011-08-19 18:10:58 +03002568 dwc3_writel(dwc->regs, DWC3_DEVTEN, 0x00);
2569 free_irq(irq, dwc);
2570
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002571err5:
Felipe Balbi72246da2011-08-19 18:10:58 +03002572 dwc3_gadget_free_endpoints(dwc);
2573
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002574err4:
Felipe Balbi3ef35fa2012-05-04 12:58:14 +03002575 dma_free_coherent(dwc->dev, DWC3_EP0_BOUNCE_SIZE,
2576 dwc->ep0_bounce, dwc->ep0_bounce_addr);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002577
Felipe Balbi72246da2011-08-19 18:10:58 +03002578err3:
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02002579 kfree(dwc->setup_buf);
Felipe Balbi72246da2011-08-19 18:10:58 +03002580
2581err2:
2582 dma_free_coherent(dwc->dev, sizeof(*dwc->ep0_trb),
2583 dwc->ep0_trb, dwc->ep0_trb_addr);
2584
2585err1:
2586 dma_free_coherent(dwc->dev, sizeof(*dwc->ctrl_req),
2587 dwc->ctrl_req, dwc->ctrl_req_addr);
2588
2589err0:
2590 return ret;
2591}
2592
2593void dwc3_gadget_exit(struct dwc3 *dwc)
2594{
2595 int irq;
Felipe Balbi72246da2011-08-19 18:10:58 +03002596
2597 usb_del_gadget_udc(&dwc->gadget);
2598 irq = platform_get_irq(to_platform_device(dwc->dev), 0);
2599
2600 dwc3_writel(dwc->regs, DWC3_DEVTEN, 0x00);
2601 free_irq(irq, dwc);
2602
Felipe Balbi72246da2011-08-19 18:10:58 +03002603 dwc3_gadget_free_endpoints(dwc);
2604
Felipe Balbi3ef35fa2012-05-04 12:58:14 +03002605 dma_free_coherent(dwc->dev, DWC3_EP0_BOUNCE_SIZE,
2606 dwc->ep0_bounce, dwc->ep0_bounce_addr);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002607
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02002608 kfree(dwc->setup_buf);
Felipe Balbi72246da2011-08-19 18:10:58 +03002609
2610 dma_free_coherent(dwc->dev, sizeof(*dwc->ep0_trb),
2611 dwc->ep0_trb, dwc->ep0_trb_addr);
2612
2613 dma_free_coherent(dwc->dev, sizeof(*dwc->ctrl_req),
2614 dwc->ctrl_req, dwc->ctrl_req_addr);
2615
2616 device_unregister(&dwc->gadget.dev);
2617}