blob: e60e72c57663b5883ec5edbb4e311a995d5ca605 [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;
244
245 if (req->queued) {
Felipe Balbieeb720f2011-11-28 12:46:59 +0200246 if (req->request.num_mapped_sgs)
247 dep->busy_slot += req->request.num_mapped_sgs;
248 else
249 dep->busy_slot++;
250
Felipe Balbi72246da2011-08-19 18:10:58 +0300251 /*
252 * Skip LINK TRB. We can't use req->trb and check for
253 * DWC3_TRBCTL_LINK_TRB because it points the TRB we just
254 * completed (not the LINK TRB).
255 */
256 if (((dep->busy_slot & DWC3_TRB_MASK) == DWC3_TRB_NUM - 1) &&
Ido Shayevitz16e78db2012-03-12 20:25:24 +0200257 usb_endpoint_xfer_isoc(dep->endpoint.desc))
Felipe Balbi72246da2011-08-19 18:10:58 +0300258 dep->busy_slot++;
259 }
260 list_del(&req->list);
Felipe Balbieeb720f2011-11-28 12:46:59 +0200261 req->trb = NULL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300262
263 if (req->request.status == -EINPROGRESS)
264 req->request.status = status;
265
Pratyush Anand0416e492012-08-10 13:42:16 +0530266 if (dwc->ep0_bounced && dep->number == 0)
267 dwc->ep0_bounced = false;
268 else
269 usb_gadget_unmap_request(&dwc->gadget, &req->request,
270 req->direction);
Felipe Balbi72246da2011-08-19 18:10:58 +0300271
272 dev_dbg(dwc->dev, "request %p from %s completed %d/%d ===> %d\n",
273 req, dep->name, req->request.actual,
274 req->request.length, status);
275
276 spin_unlock(&dwc->lock);
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +0200277 req->request.complete(&dep->endpoint, &req->request);
Felipe Balbi72246da2011-08-19 18:10:58 +0300278 spin_lock(&dwc->lock);
279}
280
281static const char *dwc3_gadget_ep_cmd_string(u8 cmd)
282{
283 switch (cmd) {
284 case DWC3_DEPCMD_DEPSTARTCFG:
285 return "Start New Configuration";
286 case DWC3_DEPCMD_ENDTRANSFER:
287 return "End Transfer";
288 case DWC3_DEPCMD_UPDATETRANSFER:
289 return "Update Transfer";
290 case DWC3_DEPCMD_STARTTRANSFER:
291 return "Start Transfer";
292 case DWC3_DEPCMD_CLEARSTALL:
293 return "Clear Stall";
294 case DWC3_DEPCMD_SETSTALL:
295 return "Set Stall";
Paul Zimmerman802fde92012-04-27 13:10:52 +0300296 case DWC3_DEPCMD_GETEPSTATE:
297 return "Get Endpoint State";
Felipe Balbi72246da2011-08-19 18:10:58 +0300298 case DWC3_DEPCMD_SETTRANSFRESOURCE:
299 return "Set Endpoint Transfer Resource";
300 case DWC3_DEPCMD_SETEPCONFIG:
301 return "Set Endpoint Configuration";
302 default:
303 return "UNKNOWN command";
304 }
305}
306
Felipe Balbib09bb642012-04-24 16:19:11 +0300307int dwc3_send_gadget_generic_command(struct dwc3 *dwc, int cmd, u32 param)
308{
309 u32 timeout = 500;
310 u32 reg;
311
312 dwc3_writel(dwc->regs, DWC3_DGCMDPAR, param);
313 dwc3_writel(dwc->regs, DWC3_DGCMD, cmd | DWC3_DGCMD_CMDACT);
314
315 do {
316 reg = dwc3_readl(dwc->regs, DWC3_DGCMD);
317 if (!(reg & DWC3_DGCMD_CMDACT)) {
318 dev_vdbg(dwc->dev, "Command Complete --> %d\n",
319 DWC3_DGCMD_STATUS(reg));
320 return 0;
321 }
322
323 /*
324 * We can't sleep here, because it's also called from
325 * interrupt context.
326 */
327 timeout--;
328 if (!timeout)
329 return -ETIMEDOUT;
330 udelay(1);
331 } while (1);
332}
333
Felipe Balbi72246da2011-08-19 18:10:58 +0300334int dwc3_send_gadget_ep_cmd(struct dwc3 *dwc, unsigned ep,
335 unsigned cmd, struct dwc3_gadget_ep_cmd_params *params)
336{
337 struct dwc3_ep *dep = dwc->eps[ep];
Sebastian Andrzej Siewior61d58242011-08-29 16:46:38 +0200338 u32 timeout = 500;
Felipe Balbi72246da2011-08-19 18:10:58 +0300339 u32 reg;
340
341 dev_vdbg(dwc->dev, "%s: cmd '%s' params %08x %08x %08x\n",
342 dep->name,
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300343 dwc3_gadget_ep_cmd_string(cmd), params->param0,
344 params->param1, params->param2);
Felipe Balbi72246da2011-08-19 18:10:58 +0300345
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300346 dwc3_writel(dwc->regs, DWC3_DEPCMDPAR0(ep), params->param0);
347 dwc3_writel(dwc->regs, DWC3_DEPCMDPAR1(ep), params->param1);
348 dwc3_writel(dwc->regs, DWC3_DEPCMDPAR2(ep), params->param2);
Felipe Balbi72246da2011-08-19 18:10:58 +0300349
350 dwc3_writel(dwc->regs, DWC3_DEPCMD(ep), cmd | DWC3_DEPCMD_CMDACT);
351 do {
352 reg = dwc3_readl(dwc->regs, DWC3_DEPCMD(ep));
353 if (!(reg & DWC3_DEPCMD_CMDACT)) {
Felipe Balbi164f6e12011-08-27 20:29:58 +0300354 dev_vdbg(dwc->dev, "Command Complete --> %d\n",
355 DWC3_DEPCMD_STATUS(reg));
Felipe Balbi72246da2011-08-19 18:10:58 +0300356 return 0;
357 }
358
359 /*
Felipe Balbi72246da2011-08-19 18:10:58 +0300360 * We can't sleep here, because it is also called from
361 * interrupt context.
362 */
363 timeout--;
364 if (!timeout)
365 return -ETIMEDOUT;
366
Sebastian Andrzej Siewior61d58242011-08-29 16:46:38 +0200367 udelay(1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300368 } while (1);
369}
370
371static dma_addr_t dwc3_trb_dma_offset(struct dwc3_ep *dep,
Felipe Balbif6bafc62012-02-06 11:04:53 +0200372 struct dwc3_trb *trb)
Felipe Balbi72246da2011-08-19 18:10:58 +0300373{
Paul Zimmermanc439ef82011-09-30 10:58:45 +0300374 u32 offset = (char *) trb - (char *) dep->trb_pool;
Felipe Balbi72246da2011-08-19 18:10:58 +0300375
376 return dep->trb_pool_dma + offset;
377}
378
379static int dwc3_alloc_trb_pool(struct dwc3_ep *dep)
380{
381 struct dwc3 *dwc = dep->dwc;
382
383 if (dep->trb_pool)
384 return 0;
385
386 if (dep->number == 0 || dep->number == 1)
387 return 0;
388
389 dep->trb_pool = dma_alloc_coherent(dwc->dev,
390 sizeof(struct dwc3_trb) * DWC3_TRB_NUM,
391 &dep->trb_pool_dma, GFP_KERNEL);
392 if (!dep->trb_pool) {
393 dev_err(dep->dwc->dev, "failed to allocate trb pool for %s\n",
394 dep->name);
395 return -ENOMEM;
396 }
397
398 return 0;
399}
400
401static void dwc3_free_trb_pool(struct dwc3_ep *dep)
402{
403 struct dwc3 *dwc = dep->dwc;
404
405 dma_free_coherent(dwc->dev, sizeof(struct dwc3_trb) * DWC3_TRB_NUM,
406 dep->trb_pool, dep->trb_pool_dma);
407
408 dep->trb_pool = NULL;
409 dep->trb_pool_dma = 0;
410}
411
412static int dwc3_gadget_start_config(struct dwc3 *dwc, struct dwc3_ep *dep)
413{
414 struct dwc3_gadget_ep_cmd_params params;
415 u32 cmd;
416
417 memset(&params, 0x00, sizeof(params));
418
419 if (dep->number != 1) {
420 cmd = DWC3_DEPCMD_DEPSTARTCFG;
421 /* XferRscIdx == 0 for ep0 and 2 for the remaining */
Paul Zimmermanb23c8432011-09-30 10:58:42 +0300422 if (dep->number > 1) {
423 if (dwc->start_config_issued)
424 return 0;
425 dwc->start_config_issued = true;
Felipe Balbi72246da2011-08-19 18:10:58 +0300426 cmd |= DWC3_DEPCMD_PARAM(2);
Paul Zimmermanb23c8432011-09-30 10:58:42 +0300427 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300428
429 return dwc3_send_gadget_ep_cmd(dwc, 0, cmd, &params);
430 }
431
432 return 0;
433}
434
435static int dwc3_gadget_set_ep_config(struct dwc3 *dwc, struct dwc3_ep *dep,
Felipe Balbic90bfae2011-11-29 13:11:21 +0200436 const struct usb_endpoint_descriptor *desc,
Felipe Balbi4b345c92012-07-16 14:08:16 +0300437 const struct usb_ss_ep_comp_descriptor *comp_desc,
438 bool ignore)
Felipe Balbi72246da2011-08-19 18:10:58 +0300439{
440 struct dwc3_gadget_ep_cmd_params params;
441
442 memset(&params, 0x00, sizeof(params));
443
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300444 params.param0 = DWC3_DEPCFG_EP_TYPE(usb_endpoint_type(desc))
Chanho Parkd2e9a132012-08-31 16:54:07 +0900445 | DWC3_DEPCFG_MAX_PACKET_SIZE(usb_endpoint_maxp(desc));
446
447 /* Burst size is only needed in SuperSpeed mode */
448 if (dwc->gadget.speed == USB_SPEED_SUPER) {
449 u32 burst = dep->endpoint.maxburst - 1;
450
451 params.param0 |= DWC3_DEPCFG_BURST_SIZE(burst);
452 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300453
Felipe Balbi4b345c92012-07-16 14:08:16 +0300454 if (ignore)
455 params.param0 |= DWC3_DEPCFG_IGN_SEQ_NUM;
456
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300457 params.param1 = DWC3_DEPCFG_XFER_COMPLETE_EN
458 | DWC3_DEPCFG_XFER_NOT_READY_EN;
Felipe Balbi72246da2011-08-19 18:10:58 +0300459
Felipe Balbi18b7ede2012-01-02 13:35:41 +0200460 if (usb_ss_max_streams(comp_desc) && usb_endpoint_xfer_bulk(desc)) {
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300461 params.param1 |= DWC3_DEPCFG_STREAM_CAPABLE
462 | DWC3_DEPCFG_STREAM_EVENT_EN;
Felipe Balbi879631a2011-09-30 10:58:47 +0300463 dep->stream_capable = true;
464 }
465
Felipe Balbi72246da2011-08-19 18:10:58 +0300466 if (usb_endpoint_xfer_isoc(desc))
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300467 params.param1 |= DWC3_DEPCFG_XFER_IN_PROGRESS_EN;
Felipe Balbi72246da2011-08-19 18:10:58 +0300468
469 /*
470 * We are doing 1:1 mapping for endpoints, meaning
471 * Physical Endpoints 2 maps to Logical Endpoint 2 and
472 * so on. We consider the direction bit as part of the physical
473 * endpoint number. So USB endpoint 0x81 is 0x03.
474 */
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300475 params.param1 |= DWC3_DEPCFG_EP_NUMBER(dep->number);
Felipe Balbi72246da2011-08-19 18:10:58 +0300476
477 /*
478 * We must use the lower 16 TX FIFOs even though
479 * HW might have more
480 */
481 if (dep->direction)
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300482 params.param0 |= DWC3_DEPCFG_FIFO_NUMBER(dep->number >> 1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300483
484 if (desc->bInterval) {
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300485 params.param1 |= DWC3_DEPCFG_BINTERVAL_M1(desc->bInterval - 1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300486 dep->interval = 1 << (desc->bInterval - 1);
487 }
488
489 return dwc3_send_gadget_ep_cmd(dwc, dep->number,
490 DWC3_DEPCMD_SETEPCONFIG, &params);
491}
492
493static int dwc3_gadget_set_xfer_resource(struct dwc3 *dwc, struct dwc3_ep *dep)
494{
495 struct dwc3_gadget_ep_cmd_params params;
496
497 memset(&params, 0x00, sizeof(params));
498
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300499 params.param0 = DWC3_DEPXFERCFG_NUM_XFER_RES(1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300500
501 return dwc3_send_gadget_ep_cmd(dwc, dep->number,
502 DWC3_DEPCMD_SETTRANSFRESOURCE, &params);
503}
504
505/**
506 * __dwc3_gadget_ep_enable - Initializes a HW endpoint
507 * @dep: endpoint to be initialized
508 * @desc: USB Endpoint Descriptor
509 *
510 * Caller should take care of locking
511 */
512static int __dwc3_gadget_ep_enable(struct dwc3_ep *dep,
Felipe Balbic90bfae2011-11-29 13:11:21 +0200513 const struct usb_endpoint_descriptor *desc,
Felipe Balbi4b345c92012-07-16 14:08:16 +0300514 const struct usb_ss_ep_comp_descriptor *comp_desc,
515 bool ignore)
Felipe Balbi72246da2011-08-19 18:10:58 +0300516{
517 struct dwc3 *dwc = dep->dwc;
518 u32 reg;
519 int ret = -ENOMEM;
520
521 if (!(dep->flags & DWC3_EP_ENABLED)) {
522 ret = dwc3_gadget_start_config(dwc, dep);
523 if (ret)
524 return ret;
525 }
526
Felipe Balbi4b345c92012-07-16 14:08:16 +0300527 ret = dwc3_gadget_set_ep_config(dwc, dep, desc, comp_desc, ignore);
Felipe Balbi72246da2011-08-19 18:10:58 +0300528 if (ret)
529 return ret;
530
531 if (!(dep->flags & DWC3_EP_ENABLED)) {
Felipe Balbif6bafc62012-02-06 11:04:53 +0200532 struct dwc3_trb *trb_st_hw;
533 struct dwc3_trb *trb_link;
Felipe Balbi72246da2011-08-19 18:10:58 +0300534
535 ret = dwc3_gadget_set_xfer_resource(dwc, dep);
536 if (ret)
537 return ret;
538
Ido Shayevitz16e78db2012-03-12 20:25:24 +0200539 dep->endpoint.desc = desc;
Felipe Balbic90bfae2011-11-29 13:11:21 +0200540 dep->comp_desc = comp_desc;
Felipe Balbi72246da2011-08-19 18:10:58 +0300541 dep->type = usb_endpoint_type(desc);
542 dep->flags |= DWC3_EP_ENABLED;
543
544 reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
545 reg |= DWC3_DALEPENA_EP(dep->number);
546 dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
547
548 if (!usb_endpoint_xfer_isoc(desc))
549 return 0;
550
551 memset(&trb_link, 0, sizeof(trb_link));
552
Paul Zimmerman1d046792012-02-15 18:56:56 -0800553 /* Link TRB for ISOC. The HWO bit is never reset */
Felipe Balbi72246da2011-08-19 18:10:58 +0300554 trb_st_hw = &dep->trb_pool[0];
555
Felipe Balbif6bafc62012-02-06 11:04:53 +0200556 trb_link = &dep->trb_pool[DWC3_TRB_NUM - 1];
Felipe Balbi72246da2011-08-19 18:10:58 +0300557
Felipe Balbif6bafc62012-02-06 11:04:53 +0200558 trb_link->bpl = lower_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw));
559 trb_link->bph = upper_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw));
560 trb_link->ctrl |= DWC3_TRBCTL_LINK_TRB;
561 trb_link->ctrl |= DWC3_TRB_CTRL_HWO;
Felipe Balbi72246da2011-08-19 18:10:58 +0300562 }
563
564 return 0;
565}
566
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200567static void dwc3_stop_active_transfer(struct dwc3 *dwc, u32 epnum);
568static void dwc3_remove_requests(struct dwc3 *dwc, struct dwc3_ep *dep)
Felipe Balbi72246da2011-08-19 18:10:58 +0300569{
570 struct dwc3_request *req;
571
Felipe Balbiea53b882012-02-17 12:10:04 +0200572 if (!list_empty(&dep->req_queued)) {
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200573 dwc3_stop_active_transfer(dwc, dep->number);
574
Pratyush Anand57911502012-07-06 15:19:10 +0530575 /* - giveback all requests to gadget driver */
Pratyush Anand15916332012-06-15 11:54:36 +0530576 while (!list_empty(&dep->req_queued)) {
577 req = next_request(&dep->req_queued);
578
579 dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
580 }
Felipe Balbiea53b882012-02-17 12:10:04 +0200581 }
582
Felipe Balbi72246da2011-08-19 18:10:58 +0300583 while (!list_empty(&dep->request_list)) {
584 req = next_request(&dep->request_list);
585
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200586 dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
Felipe Balbi72246da2011-08-19 18:10:58 +0300587 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300588}
589
590/**
591 * __dwc3_gadget_ep_disable - Disables a HW endpoint
592 * @dep: the endpoint to disable
593 *
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200594 * This function also removes requests which are currently processed ny the
595 * hardware and those which are not yet scheduled.
596 * Caller should take care of locking.
Felipe Balbi72246da2011-08-19 18:10:58 +0300597 */
Felipe Balbi72246da2011-08-19 18:10:58 +0300598static int __dwc3_gadget_ep_disable(struct dwc3_ep *dep)
599{
600 struct dwc3 *dwc = dep->dwc;
601 u32 reg;
602
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200603 dwc3_remove_requests(dwc, dep);
Felipe Balbi72246da2011-08-19 18:10:58 +0300604
605 reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
606 reg &= ~DWC3_DALEPENA_EP(dep->number);
607 dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
608
Felipe Balbi879631a2011-09-30 10:58:47 +0300609 dep->stream_capable = false;
Ido Shayevitzf9c56cd2012-02-08 13:56:48 +0200610 dep->endpoint.desc = NULL;
Felipe Balbic90bfae2011-11-29 13:11:21 +0200611 dep->comp_desc = NULL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300612 dep->type = 0;
Felipe Balbi879631a2011-09-30 10:58:47 +0300613 dep->flags = 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300614
615 return 0;
616}
617
618/* -------------------------------------------------------------------------- */
619
620static int dwc3_gadget_ep0_enable(struct usb_ep *ep,
621 const struct usb_endpoint_descriptor *desc)
622{
623 return -EINVAL;
624}
625
626static int dwc3_gadget_ep0_disable(struct usb_ep *ep)
627{
628 return -EINVAL;
629}
630
631/* -------------------------------------------------------------------------- */
632
633static int dwc3_gadget_ep_enable(struct usb_ep *ep,
634 const struct usb_endpoint_descriptor *desc)
635{
636 struct dwc3_ep *dep;
637 struct dwc3 *dwc;
638 unsigned long flags;
639 int ret;
640
641 if (!ep || !desc || desc->bDescriptorType != USB_DT_ENDPOINT) {
642 pr_debug("dwc3: invalid parameters\n");
643 return -EINVAL;
644 }
645
646 if (!desc->wMaxPacketSize) {
647 pr_debug("dwc3: missing wMaxPacketSize\n");
648 return -EINVAL;
649 }
650
651 dep = to_dwc3_ep(ep);
652 dwc = dep->dwc;
653
Felipe Balbic6f83f32012-08-15 12:28:29 +0300654 if (dep->flags & DWC3_EP_ENABLED) {
655 dev_WARN_ONCE(dwc->dev, true, "%s is already enabled\n",
656 dep->name);
657 return 0;
658 }
659
Felipe Balbi72246da2011-08-19 18:10:58 +0300660 switch (usb_endpoint_type(desc)) {
661 case USB_ENDPOINT_XFER_CONTROL:
Anton Tikhomirov27a78d62012-02-23 15:38:46 +0900662 strlcat(dep->name, "-control", sizeof(dep->name));
Felipe Balbi72246da2011-08-19 18:10:58 +0300663 break;
664 case USB_ENDPOINT_XFER_ISOC:
Anton Tikhomirov27a78d62012-02-23 15:38:46 +0900665 strlcat(dep->name, "-isoc", sizeof(dep->name));
Felipe Balbi72246da2011-08-19 18:10:58 +0300666 break;
667 case USB_ENDPOINT_XFER_BULK:
Anton Tikhomirov27a78d62012-02-23 15:38:46 +0900668 strlcat(dep->name, "-bulk", sizeof(dep->name));
Felipe Balbi72246da2011-08-19 18:10:58 +0300669 break;
670 case USB_ENDPOINT_XFER_INT:
Anton Tikhomirov27a78d62012-02-23 15:38:46 +0900671 strlcat(dep->name, "-int", sizeof(dep->name));
Felipe Balbi72246da2011-08-19 18:10:58 +0300672 break;
673 default:
674 dev_err(dwc->dev, "invalid endpoint transfer type\n");
675 }
676
Felipe Balbi72246da2011-08-19 18:10:58 +0300677 dev_vdbg(dwc->dev, "Enabling %s\n", dep->name);
678
679 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi4b345c92012-07-16 14:08:16 +0300680 ret = __dwc3_gadget_ep_enable(dep, desc, ep->comp_desc, false);
Felipe Balbi72246da2011-08-19 18:10:58 +0300681 spin_unlock_irqrestore(&dwc->lock, flags);
682
683 return ret;
684}
685
686static int dwc3_gadget_ep_disable(struct usb_ep *ep)
687{
688 struct dwc3_ep *dep;
689 struct dwc3 *dwc;
690 unsigned long flags;
691 int ret;
692
693 if (!ep) {
694 pr_debug("dwc3: invalid parameters\n");
695 return -EINVAL;
696 }
697
698 dep = to_dwc3_ep(ep);
699 dwc = dep->dwc;
700
701 if (!(dep->flags & DWC3_EP_ENABLED)) {
702 dev_WARN_ONCE(dwc->dev, true, "%s is already disabled\n",
703 dep->name);
704 return 0;
705 }
706
707 snprintf(dep->name, sizeof(dep->name), "ep%d%s",
708 dep->number >> 1,
709 (dep->number & 1) ? "in" : "out");
710
711 spin_lock_irqsave(&dwc->lock, flags);
712 ret = __dwc3_gadget_ep_disable(dep);
713 spin_unlock_irqrestore(&dwc->lock, flags);
714
715 return ret;
716}
717
718static struct usb_request *dwc3_gadget_ep_alloc_request(struct usb_ep *ep,
719 gfp_t gfp_flags)
720{
721 struct dwc3_request *req;
722 struct dwc3_ep *dep = to_dwc3_ep(ep);
723 struct dwc3 *dwc = dep->dwc;
724
725 req = kzalloc(sizeof(*req), gfp_flags);
726 if (!req) {
727 dev_err(dwc->dev, "not enough memory\n");
728 return NULL;
729 }
730
731 req->epnum = dep->number;
732 req->dep = dep;
Felipe Balbi72246da2011-08-19 18:10:58 +0300733
734 return &req->request;
735}
736
737static void dwc3_gadget_ep_free_request(struct usb_ep *ep,
738 struct usb_request *request)
739{
740 struct dwc3_request *req = to_dwc3_request(request);
741
742 kfree(req);
743}
744
Felipe Balbic71fc372011-11-22 11:37:34 +0200745/**
746 * dwc3_prepare_one_trb - setup one TRB from one request
747 * @dep: endpoint for which this request is prepared
748 * @req: dwc3_request pointer
749 */
Felipe Balbi68e823e2011-11-28 12:25:01 +0200750static void dwc3_prepare_one_trb(struct dwc3_ep *dep,
Felipe Balbieeb720f2011-11-28 12:46:59 +0200751 struct dwc3_request *req, dma_addr_t dma,
752 unsigned length, unsigned last, unsigned chain)
Felipe Balbic71fc372011-11-22 11:37:34 +0200753{
Felipe Balbieeb720f2011-11-28 12:46:59 +0200754 struct dwc3 *dwc = dep->dwc;
Felipe Balbif6bafc62012-02-06 11:04:53 +0200755 struct dwc3_trb *trb;
Felipe Balbic71fc372011-11-22 11:37:34 +0200756
757 unsigned int cur_slot;
758
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 Balbif6bafc62012-02-06 11:04:53 +0200764 trb = &dep->trb_pool[dep->free_slot & DWC3_TRB_MASK];
Felipe Balbic71fc372011-11-22 11:37:34 +0200765 cur_slot = dep->free_slot;
766 dep->free_slot++;
767
768 /* Skip the LINK-TRB on ISOC */
769 if (((cur_slot & DWC3_TRB_MASK) == DWC3_TRB_NUM - 1) &&
Ido Shayevitz16e78db2012-03-12 20:25:24 +0200770 usb_endpoint_xfer_isoc(dep->endpoint.desc))
Felipe Balbi68e823e2011-11-28 12:25:01 +0200771 return;
Felipe Balbic71fc372011-11-22 11:37:34 +0200772
Felipe Balbieeb720f2011-11-28 12:46:59 +0200773 if (!req->trb) {
774 dwc3_gadget_move_request_queued(req);
Felipe Balbif6bafc62012-02-06 11:04:53 +0200775 req->trb = trb;
776 req->trb_dma = dwc3_trb_dma_offset(dep, trb);
Felipe Balbieeb720f2011-11-28 12:46:59 +0200777 }
Felipe Balbic71fc372011-11-22 11:37:34 +0200778
Felipe Balbif6bafc62012-02-06 11:04:53 +0200779 trb->size = DWC3_TRB_SIZE_LENGTH(length);
780 trb->bpl = lower_32_bits(dma);
781 trb->bph = upper_32_bits(dma);
Felipe Balbic71fc372011-11-22 11:37:34 +0200782
Ido Shayevitz16e78db2012-03-12 20:25:24 +0200783 switch (usb_endpoint_type(dep->endpoint.desc)) {
Felipe Balbic71fc372011-11-22 11:37:34 +0200784 case USB_ENDPOINT_XFER_CONTROL:
Felipe Balbif6bafc62012-02-06 11:04:53 +0200785 trb->ctrl = DWC3_TRBCTL_CONTROL_SETUP;
Felipe Balbic71fc372011-11-22 11:37:34 +0200786 break;
787
788 case USB_ENDPOINT_XFER_ISOC:
Felipe Balbif6bafc62012-02-06 11:04:53 +0200789 trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS_FIRST;
Felipe Balbic71fc372011-11-22 11:37:34 +0200790
Pratyush Anand206dd692012-05-21 12:42:54 +0530791 if (!req->request.no_interrupt)
Felipe Balbif6bafc62012-02-06 11:04:53 +0200792 trb->ctrl |= DWC3_TRB_CTRL_IOC;
Felipe Balbic71fc372011-11-22 11:37:34 +0200793 break;
794
795 case USB_ENDPOINT_XFER_BULK:
796 case USB_ENDPOINT_XFER_INT:
Felipe Balbif6bafc62012-02-06 11:04:53 +0200797 trb->ctrl = DWC3_TRBCTL_NORMAL;
Felipe Balbic71fc372011-11-22 11:37:34 +0200798 break;
799 default:
800 /*
801 * This is only possible with faulty memory because we
802 * checked it already :)
803 */
804 BUG();
805 }
806
Ido Shayevitz16e78db2012-03-12 20:25:24 +0200807 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbif6bafc62012-02-06 11:04:53 +0200808 trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI;
809 trb->ctrl |= DWC3_TRB_CTRL_CSP;
810 } else {
811 if (chain)
812 trb->ctrl |= DWC3_TRB_CTRL_CHN;
Felipe Balbic71fc372011-11-22 11:37:34 +0200813
Felipe Balbif6bafc62012-02-06 11:04:53 +0200814 if (last)
815 trb->ctrl |= DWC3_TRB_CTRL_LST;
816 }
817
Ido Shayevitz16e78db2012-03-12 20:25:24 +0200818 if (usb_endpoint_xfer_bulk(dep->endpoint.desc) && dep->stream_capable)
Felipe Balbif6bafc62012-02-06 11:04:53 +0200819 trb->ctrl |= DWC3_TRB_CTRL_SID_SOFN(req->request.stream_id);
820
821 trb->ctrl |= DWC3_TRB_CTRL_HWO;
Felipe Balbic71fc372011-11-22 11:37:34 +0200822}
823
Felipe Balbi72246da2011-08-19 18:10:58 +0300824/*
825 * dwc3_prepare_trbs - setup TRBs from requests
826 * @dep: endpoint for which requests are being prepared
827 * @starting: true if the endpoint is idle and no requests are queued.
828 *
Paul Zimmerman1d046792012-02-15 18:56:56 -0800829 * The function goes through the requests list and sets up TRBs for the
830 * transfers. The function returns once there are no more TRBs available or
831 * it runs out of requests.
Felipe Balbi72246da2011-08-19 18:10:58 +0300832 */
Felipe Balbi68e823e2011-11-28 12:25:01 +0200833static void dwc3_prepare_trbs(struct dwc3_ep *dep, bool starting)
Felipe Balbi72246da2011-08-19 18:10:58 +0300834{
Felipe Balbi68e823e2011-11-28 12:25:01 +0200835 struct dwc3_request *req, *n;
Felipe Balbi72246da2011-08-19 18:10:58 +0300836 u32 trbs_left;
Paul Zimmerman8d62cd62012-02-15 13:35:06 +0200837 u32 max;
Felipe Balbic71fc372011-11-22 11:37:34 +0200838 unsigned int last_one = 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300839
840 BUILD_BUG_ON_NOT_POWER_OF_2(DWC3_TRB_NUM);
841
842 /* the first request must not be queued */
843 trbs_left = (dep->busy_slot - dep->free_slot) & DWC3_TRB_MASK;
Felipe Balbic71fc372011-11-22 11:37:34 +0200844
Paul Zimmerman8d62cd62012-02-15 13:35:06 +0200845 /* Can't wrap around on a non-isoc EP since there's no link TRB */
Ido Shayevitz16e78db2012-03-12 20:25:24 +0200846 if (!usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Paul Zimmerman8d62cd62012-02-15 13:35:06 +0200847 max = DWC3_TRB_NUM - (dep->free_slot & DWC3_TRB_MASK);
848 if (trbs_left > max)
849 trbs_left = max;
850 }
851
Felipe Balbi72246da2011-08-19 18:10:58 +0300852 /*
Paul Zimmerman1d046792012-02-15 18:56:56 -0800853 * If busy & slot are equal than it is either full or empty. If we are
854 * starting to process requests then we are empty. Otherwise we are
Felipe Balbi72246da2011-08-19 18:10:58 +0300855 * full and don't do anything
856 */
857 if (!trbs_left) {
858 if (!starting)
Felipe Balbi68e823e2011-11-28 12:25:01 +0200859 return;
Felipe Balbi72246da2011-08-19 18:10:58 +0300860 trbs_left = DWC3_TRB_NUM;
861 /*
862 * In case we start from scratch, we queue the ISOC requests
863 * starting from slot 1. This is done because we use ring
864 * buffer and have no LST bit to stop us. Instead, we place
Paul Zimmerman1d046792012-02-15 18:56:56 -0800865 * IOC bit every TRB_NUM/4. We try to avoid having an interrupt
Felipe Balbi72246da2011-08-19 18:10:58 +0300866 * after the first request so we start at slot 1 and have
867 * 7 requests proceed before we hit the first IOC.
868 * Other transfer types don't use the ring buffer and are
869 * processed from the first TRB until the last one. Since we
870 * don't wrap around we have to start at the beginning.
871 */
Ido Shayevitz16e78db2012-03-12 20:25:24 +0200872 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbi72246da2011-08-19 18:10:58 +0300873 dep->busy_slot = 1;
874 dep->free_slot = 1;
875 } else {
876 dep->busy_slot = 0;
877 dep->free_slot = 0;
878 }
879 }
880
881 /* The last TRB is a link TRB, not used for xfer */
Ido Shayevitz16e78db2012-03-12 20:25:24 +0200882 if ((trbs_left <= 1) && usb_endpoint_xfer_isoc(dep->endpoint.desc))
Felipe Balbi68e823e2011-11-28 12:25:01 +0200883 return;
Felipe Balbi72246da2011-08-19 18:10:58 +0300884
885 list_for_each_entry_safe(req, n, &dep->request_list, list) {
Felipe Balbieeb720f2011-11-28 12:46:59 +0200886 unsigned length;
887 dma_addr_t dma;
Felipe Balbi72246da2011-08-19 18:10:58 +0300888
Felipe Balbieeb720f2011-11-28 12:46:59 +0200889 if (req->request.num_mapped_sgs > 0) {
890 struct usb_request *request = &req->request;
891 struct scatterlist *sg = request->sg;
892 struct scatterlist *s;
893 int i;
Felipe Balbi72246da2011-08-19 18:10:58 +0300894
Felipe Balbieeb720f2011-11-28 12:46:59 +0200895 for_each_sg(sg, s, request->num_mapped_sgs, i) {
896 unsigned chain = true;
Felipe Balbi72246da2011-08-19 18:10:58 +0300897
Felipe Balbieeb720f2011-11-28 12:46:59 +0200898 length = sg_dma_len(s);
899 dma = sg_dma_address(s);
Felipe Balbi72246da2011-08-19 18:10:58 +0300900
Paul Zimmerman1d046792012-02-15 18:56:56 -0800901 if (i == (request->num_mapped_sgs - 1) ||
902 sg_is_last(s)) {
Felipe Balbieeb720f2011-11-28 12:46:59 +0200903 last_one = true;
904 chain = false;
905 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300906
Felipe Balbieeb720f2011-11-28 12:46:59 +0200907 trbs_left--;
908 if (!trbs_left)
909 last_one = true;
Felipe Balbi72246da2011-08-19 18:10:58 +0300910
Felipe Balbieeb720f2011-11-28 12:46:59 +0200911 if (last_one)
912 chain = false;
Felipe Balbi72246da2011-08-19 18:10:58 +0300913
Felipe Balbieeb720f2011-11-28 12:46:59 +0200914 dwc3_prepare_one_trb(dep, req, dma, length,
915 last_one, chain);
Felipe Balbi72246da2011-08-19 18:10:58 +0300916
Felipe Balbieeb720f2011-11-28 12:46:59 +0200917 if (last_one)
918 break;
919 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300920 } else {
Felipe Balbieeb720f2011-11-28 12:46:59 +0200921 dma = req->request.dma;
922 length = req->request.length;
923 trbs_left--;
924
925 if (!trbs_left)
926 last_one = 1;
927
928 /* Is this the last request? */
929 if (list_is_last(&req->list, &dep->request_list))
930 last_one = 1;
931
932 dwc3_prepare_one_trb(dep, req, dma, length,
933 last_one, false);
934
935 if (last_one)
936 break;
Felipe Balbi72246da2011-08-19 18:10:58 +0300937 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300938 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300939}
940
941static int __dwc3_gadget_kick_transfer(struct dwc3_ep *dep, u16 cmd_param,
942 int start_new)
943{
944 struct dwc3_gadget_ep_cmd_params params;
945 struct dwc3_request *req;
946 struct dwc3 *dwc = dep->dwc;
947 int ret;
948 u32 cmd;
949
950 if (start_new && (dep->flags & DWC3_EP_BUSY)) {
951 dev_vdbg(dwc->dev, "%s: endpoint busy\n", dep->name);
952 return -EBUSY;
953 }
954 dep->flags &= ~DWC3_EP_PENDING_REQUEST;
955
956 /*
957 * If we are getting here after a short-out-packet we don't enqueue any
958 * new requests as we try to set the IOC bit only on the last request.
959 */
960 if (start_new) {
961 if (list_empty(&dep->req_queued))
962 dwc3_prepare_trbs(dep, start_new);
963
964 /* req points to the first request which will be sent */
965 req = next_request(&dep->req_queued);
966 } else {
Felipe Balbi68e823e2011-11-28 12:25:01 +0200967 dwc3_prepare_trbs(dep, start_new);
968
Felipe Balbi72246da2011-08-19 18:10:58 +0300969 /*
Paul Zimmerman1d046792012-02-15 18:56:56 -0800970 * req points to the first request where HWO changed from 0 to 1
Felipe Balbi72246da2011-08-19 18:10:58 +0300971 */
Felipe Balbi68e823e2011-11-28 12:25:01 +0200972 req = next_request(&dep->req_queued);
Felipe Balbi72246da2011-08-19 18:10:58 +0300973 }
974 if (!req) {
975 dep->flags |= DWC3_EP_PENDING_REQUEST;
976 return 0;
977 }
978
979 memset(&params, 0, sizeof(params));
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300980 params.param0 = upper_32_bits(req->trb_dma);
981 params.param1 = lower_32_bits(req->trb_dma);
Felipe Balbi72246da2011-08-19 18:10:58 +0300982
983 if (start_new)
984 cmd = DWC3_DEPCMD_STARTTRANSFER;
985 else
986 cmd = DWC3_DEPCMD_UPDATETRANSFER;
987
988 cmd |= DWC3_DEPCMD_PARAM(cmd_param);
989 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number, cmd, &params);
990 if (ret < 0) {
991 dev_dbg(dwc->dev, "failed to send STARTTRANSFER command\n");
992
993 /*
994 * FIXME we need to iterate over the list of requests
995 * here and stop, unmap, free and del each of the linked
Paul Zimmerman1d046792012-02-15 18:56:56 -0800996 * requests instead of what we do now.
Felipe Balbi72246da2011-08-19 18:10:58 +0300997 */
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +0200998 usb_gadget_unmap_request(&dwc->gadget, &req->request,
999 req->direction);
Felipe Balbi72246da2011-08-19 18:10:58 +03001000 list_del(&req->list);
1001 return ret;
1002 }
1003
1004 dep->flags |= DWC3_EP_BUSY;
Felipe Balbi25b8ff62011-11-04 12:32:47 +02001005
Paul Zimmermanf898ae02012-03-29 18:16:54 +00001006 if (start_new) {
Felipe Balbib4996a82012-06-06 12:04:13 +03001007 dep->resource_index = dwc3_gadget_ep_get_transfer_index(dwc,
Paul Zimmermanf898ae02012-03-29 18:16:54 +00001008 dep->number);
Felipe Balbib4996a82012-06-06 12:04:13 +03001009 WARN_ON_ONCE(!dep->resource_index);
Paul Zimmermanf898ae02012-03-29 18:16:54 +00001010 }
Felipe Balbi25b8ff62011-11-04 12:32:47 +02001011
Felipe Balbi72246da2011-08-19 18:10:58 +03001012 return 0;
1013}
1014
Pratyush Anandd6d6ec72012-05-25 18:54:56 +05301015static void __dwc3_gadget_start_isoc(struct dwc3 *dwc,
1016 struct dwc3_ep *dep, u32 cur_uf)
1017{
1018 u32 uf;
1019
1020 if (list_empty(&dep->request_list)) {
1021 dev_vdbg(dwc->dev, "ISOC ep %s run out for requests.\n",
1022 dep->name);
Pratyush Anandf4a53c52012-08-30 12:21:43 +05301023 dep->flags |= DWC3_EP_PENDING_REQUEST;
Pratyush Anandd6d6ec72012-05-25 18:54:56 +05301024 return;
1025 }
1026
1027 /* 4 micro frames in the future */
1028 uf = cur_uf + dep->interval * 4;
1029
1030 __dwc3_gadget_kick_transfer(dep, uf, 1);
1031}
1032
1033static void dwc3_gadget_start_isoc(struct dwc3 *dwc,
1034 struct dwc3_ep *dep, const struct dwc3_event_depevt *event)
1035{
1036 u32 cur_uf, mask;
1037
1038 mask = ~(dep->interval - 1);
1039 cur_uf = event->parameters & mask;
1040
1041 __dwc3_gadget_start_isoc(dwc, dep, cur_uf);
1042}
1043
Felipe Balbi72246da2011-08-19 18:10:58 +03001044static int __dwc3_gadget_ep_queue(struct dwc3_ep *dep, struct dwc3_request *req)
1045{
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02001046 struct dwc3 *dwc = dep->dwc;
1047 int ret;
1048
Felipe Balbi72246da2011-08-19 18:10:58 +03001049 req->request.actual = 0;
1050 req->request.status = -EINPROGRESS;
1051 req->direction = dep->direction;
1052 req->epnum = dep->number;
1053
1054 /*
1055 * We only add to our list of requests now and
1056 * start consuming the list once we get XferNotReady
1057 * IRQ.
1058 *
1059 * That way, we avoid doing anything that we don't need
1060 * to do now and defer it until the point we receive a
1061 * particular token from the Host side.
1062 *
1063 * This will also avoid Host cancelling URBs due to too
Paul Zimmerman1d046792012-02-15 18:56:56 -08001064 * many NAKs.
Felipe Balbi72246da2011-08-19 18:10:58 +03001065 */
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02001066 ret = usb_gadget_map_request(&dwc->gadget, &req->request,
1067 dep->direction);
1068 if (ret)
1069 return ret;
1070
Felipe Balbi72246da2011-08-19 18:10:58 +03001071 list_add_tail(&req->list, &dep->request_list);
1072
1073 /*
Felipe Balbib511e5e2012-06-06 12:00:50 +03001074 * There are a few special cases:
Felipe Balbi72246da2011-08-19 18:10:58 +03001075 *
Paul Zimmermanf898ae02012-03-29 18:16:54 +00001076 * 1. XferNotReady with empty list of requests. We need to kick the
1077 * transfer here in that situation, otherwise we will be NAKing
1078 * forever. If we get XferNotReady before gadget driver has a
1079 * chance to queue a request, we will ACK the IRQ but won't be
1080 * able to receive the data until the next request is queued.
1081 * The following code is handling exactly that.
1082 *
Felipe Balbi72246da2011-08-19 18:10:58 +03001083 */
1084 if (dep->flags & DWC3_EP_PENDING_REQUEST) {
Pratyush Anandf4a53c52012-08-30 12:21:43 +05301085 /*
1086 * If xfernotready is already elapsed and it is a case
1087 * of isoc transfer, then issue END TRANSFER, so that
1088 * you can receive xfernotready again and can have
1089 * notion of current microframe.
1090 */
1091 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Pratyush Anandcdc359d2013-01-14 15:59:34 +05301092 if (list_empty(&dep->req_queued)) {
1093 dwc3_stop_active_transfer(dwc, dep->number);
1094 dep->flags = DWC3_EP_ENABLED;
1095 }
Pratyush Anandf4a53c52012-08-30 12:21:43 +05301096 return 0;
1097 }
1098
Felipe Balbib511e5e2012-06-06 12:00:50 +03001099 ret = __dwc3_gadget_kick_transfer(dep, 0, true);
Moiz Sonasath348e0262012-08-01 14:08:30 -05001100 if (ret && ret != -EBUSY)
Felipe Balbi72246da2011-08-19 18:10:58 +03001101 dev_dbg(dwc->dev, "%s: failed to kick transfers\n",
1102 dep->name);
Pratyush Anand15f86bd2013-01-14 15:59:33 +05301103 return ret;
Felipe Balbia0925322012-05-22 10:24:11 +03001104 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001105
Felipe Balbib511e5e2012-06-06 12:00:50 +03001106 /*
1107 * 2. XferInProgress on Isoc EP with an active transfer. We need to
1108 * kick the transfer here after queuing a request, otherwise the
1109 * core may not see the modified TRB(s).
1110 */
1111 if (usb_endpoint_xfer_isoc(dep->endpoint.desc) &&
Pratyush Anand79c90462012-08-07 16:54:18 +05301112 (dep->flags & DWC3_EP_BUSY) &&
1113 !(dep->flags & DWC3_EP_MISSED_ISOC)) {
Felipe Balbib4996a82012-06-06 12:04:13 +03001114 WARN_ON_ONCE(!dep->resource_index);
1115 ret = __dwc3_gadget_kick_transfer(dep, dep->resource_index,
Felipe Balbib511e5e2012-06-06 12:00:50 +03001116 false);
Moiz Sonasath348e0262012-08-01 14:08:30 -05001117 if (ret && ret != -EBUSY)
Felipe Balbib511e5e2012-06-06 12:00:50 +03001118 dev_dbg(dwc->dev, "%s: failed to kick transfers\n",
1119 dep->name);
Pratyush Anand15f86bd2013-01-14 15:59:33 +05301120 return ret;
Felipe Balbib511e5e2012-06-06 12:00:50 +03001121 }
1122
Felipe Balbi72246da2011-08-19 18:10:58 +03001123 return 0;
1124}
1125
1126static int dwc3_gadget_ep_queue(struct usb_ep *ep, struct usb_request *request,
1127 gfp_t gfp_flags)
1128{
1129 struct dwc3_request *req = to_dwc3_request(request);
1130 struct dwc3_ep *dep = to_dwc3_ep(ep);
1131 struct dwc3 *dwc = dep->dwc;
1132
1133 unsigned long flags;
1134
1135 int ret;
1136
Ido Shayevitz16e78db2012-03-12 20:25:24 +02001137 if (!dep->endpoint.desc) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001138 dev_dbg(dwc->dev, "trying to queue request %p to disabled %s\n",
1139 request, ep->name);
1140 return -ESHUTDOWN;
1141 }
1142
1143 dev_vdbg(dwc->dev, "queing request %p to %s length %d\n",
1144 request, ep->name, request->length);
1145
1146 spin_lock_irqsave(&dwc->lock, flags);
1147 ret = __dwc3_gadget_ep_queue(dep, req);
1148 spin_unlock_irqrestore(&dwc->lock, flags);
1149
1150 return ret;
1151}
1152
1153static int dwc3_gadget_ep_dequeue(struct usb_ep *ep,
1154 struct usb_request *request)
1155{
1156 struct dwc3_request *req = to_dwc3_request(request);
1157 struct dwc3_request *r = NULL;
1158
1159 struct dwc3_ep *dep = to_dwc3_ep(ep);
1160 struct dwc3 *dwc = dep->dwc;
1161
1162 unsigned long flags;
1163 int ret = 0;
1164
1165 spin_lock_irqsave(&dwc->lock, flags);
1166
1167 list_for_each_entry(r, &dep->request_list, list) {
1168 if (r == req)
1169 break;
1170 }
1171
1172 if (r != req) {
1173 list_for_each_entry(r, &dep->req_queued, list) {
1174 if (r == req)
1175 break;
1176 }
1177 if (r == req) {
1178 /* wait until it is processed */
1179 dwc3_stop_active_transfer(dwc, dep->number);
Pratyush Anande8d4e8b2012-06-15 11:54:00 +05301180 goto out1;
Felipe Balbi72246da2011-08-19 18:10:58 +03001181 }
1182 dev_err(dwc->dev, "request %p was not queued to %s\n",
1183 request, ep->name);
1184 ret = -EINVAL;
1185 goto out0;
1186 }
1187
Pratyush Anande8d4e8b2012-06-15 11:54:00 +05301188out1:
Felipe Balbi72246da2011-08-19 18:10:58 +03001189 /* giveback the request */
1190 dwc3_gadget_giveback(dep, req, -ECONNRESET);
1191
1192out0:
1193 spin_unlock_irqrestore(&dwc->lock, flags);
1194
1195 return ret;
1196}
1197
1198int __dwc3_gadget_ep_set_halt(struct dwc3_ep *dep, int value)
1199{
1200 struct dwc3_gadget_ep_cmd_params params;
1201 struct dwc3 *dwc = dep->dwc;
1202 int ret;
1203
1204 memset(&params, 0x00, sizeof(params));
1205
1206 if (value) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001207 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number,
1208 DWC3_DEPCMD_SETSTALL, &params);
1209 if (ret)
1210 dev_err(dwc->dev, "failed to %s STALL on %s\n",
1211 value ? "set" : "clear",
1212 dep->name);
1213 else
1214 dep->flags |= DWC3_EP_STALL;
1215 } else {
Paul Zimmerman52754552011-09-30 10:58:44 +03001216 if (dep->flags & DWC3_EP_WEDGE)
1217 return 0;
1218
Felipe Balbi72246da2011-08-19 18:10:58 +03001219 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number,
1220 DWC3_DEPCMD_CLEARSTALL, &params);
1221 if (ret)
1222 dev_err(dwc->dev, "failed to %s STALL on %s\n",
1223 value ? "set" : "clear",
1224 dep->name);
1225 else
1226 dep->flags &= ~DWC3_EP_STALL;
1227 }
Paul Zimmerman52754552011-09-30 10:58:44 +03001228
Felipe Balbi72246da2011-08-19 18:10:58 +03001229 return ret;
1230}
1231
1232static int dwc3_gadget_ep_set_halt(struct usb_ep *ep, int value)
1233{
1234 struct dwc3_ep *dep = to_dwc3_ep(ep);
1235 struct dwc3 *dwc = dep->dwc;
1236
1237 unsigned long flags;
1238
1239 int ret;
1240
1241 spin_lock_irqsave(&dwc->lock, flags);
1242
Ido Shayevitz16e78db2012-03-12 20:25:24 +02001243 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001244 dev_err(dwc->dev, "%s is of Isochronous type\n", dep->name);
1245 ret = -EINVAL;
1246 goto out;
1247 }
1248
1249 ret = __dwc3_gadget_ep_set_halt(dep, value);
1250out:
1251 spin_unlock_irqrestore(&dwc->lock, flags);
1252
1253 return ret;
1254}
1255
1256static int dwc3_gadget_ep_set_wedge(struct usb_ep *ep)
1257{
1258 struct dwc3_ep *dep = to_dwc3_ep(ep);
Paul Zimmerman249a4562012-02-24 17:32:16 -08001259 struct dwc3 *dwc = dep->dwc;
1260 unsigned long flags;
Felipe Balbi72246da2011-08-19 18:10:58 +03001261
Paul Zimmerman249a4562012-02-24 17:32:16 -08001262 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001263 dep->flags |= DWC3_EP_WEDGE;
Paul Zimmerman249a4562012-02-24 17:32:16 -08001264 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001265
Pratyush Anand08f0d962012-06-25 22:40:43 +05301266 if (dep->number == 0 || dep->number == 1)
1267 return dwc3_gadget_ep0_set_halt(ep, 1);
1268 else
1269 return dwc3_gadget_ep_set_halt(ep, 1);
Felipe Balbi72246da2011-08-19 18:10:58 +03001270}
1271
1272/* -------------------------------------------------------------------------- */
1273
1274static struct usb_endpoint_descriptor dwc3_gadget_ep0_desc = {
1275 .bLength = USB_DT_ENDPOINT_SIZE,
1276 .bDescriptorType = USB_DT_ENDPOINT,
1277 .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
1278};
1279
1280static const struct usb_ep_ops dwc3_gadget_ep0_ops = {
1281 .enable = dwc3_gadget_ep0_enable,
1282 .disable = dwc3_gadget_ep0_disable,
1283 .alloc_request = dwc3_gadget_ep_alloc_request,
1284 .free_request = dwc3_gadget_ep_free_request,
1285 .queue = dwc3_gadget_ep0_queue,
1286 .dequeue = dwc3_gadget_ep_dequeue,
Pratyush Anand08f0d962012-06-25 22:40:43 +05301287 .set_halt = dwc3_gadget_ep0_set_halt,
Felipe Balbi72246da2011-08-19 18:10:58 +03001288 .set_wedge = dwc3_gadget_ep_set_wedge,
1289};
1290
1291static const struct usb_ep_ops dwc3_gadget_ep_ops = {
1292 .enable = dwc3_gadget_ep_enable,
1293 .disable = dwc3_gadget_ep_disable,
1294 .alloc_request = dwc3_gadget_ep_alloc_request,
1295 .free_request = dwc3_gadget_ep_free_request,
1296 .queue = dwc3_gadget_ep_queue,
1297 .dequeue = dwc3_gadget_ep_dequeue,
1298 .set_halt = dwc3_gadget_ep_set_halt,
1299 .set_wedge = dwc3_gadget_ep_set_wedge,
1300};
1301
1302/* -------------------------------------------------------------------------- */
1303
1304static int dwc3_gadget_get_frame(struct usb_gadget *g)
1305{
1306 struct dwc3 *dwc = gadget_to_dwc(g);
1307 u32 reg;
1308
1309 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1310 return DWC3_DSTS_SOFFN(reg);
1311}
1312
1313static int dwc3_gadget_wakeup(struct usb_gadget *g)
1314{
1315 struct dwc3 *dwc = gadget_to_dwc(g);
1316
1317 unsigned long timeout;
1318 unsigned long flags;
1319
1320 u32 reg;
1321
1322 int ret = 0;
1323
1324 u8 link_state;
1325 u8 speed;
1326
1327 spin_lock_irqsave(&dwc->lock, flags);
1328
1329 /*
1330 * According to the Databook Remote wakeup request should
1331 * be issued only when the device is in early suspend state.
1332 *
1333 * We can check that via USB Link State bits in DSTS register.
1334 */
1335 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1336
1337 speed = reg & DWC3_DSTS_CONNECTSPD;
1338 if (speed == DWC3_DSTS_SUPERSPEED) {
1339 dev_dbg(dwc->dev, "no wakeup on SuperSpeed\n");
1340 ret = -EINVAL;
1341 goto out;
1342 }
1343
1344 link_state = DWC3_DSTS_USBLNKST(reg);
1345
1346 switch (link_state) {
1347 case DWC3_LINK_STATE_RX_DET: /* in HS, means Early Suspend */
1348 case DWC3_LINK_STATE_U3: /* in HS, means SUSPEND */
1349 break;
1350 default:
1351 dev_dbg(dwc->dev, "can't wakeup from link state %d\n",
1352 link_state);
1353 ret = -EINVAL;
1354 goto out;
1355 }
1356
Felipe Balbi8598bde2012-01-02 18:55:57 +02001357 ret = dwc3_gadget_set_link_state(dwc, DWC3_LINK_STATE_RECOV);
1358 if (ret < 0) {
1359 dev_err(dwc->dev, "failed to put link in Recovery\n");
1360 goto out;
1361 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001362
Paul Zimmerman802fde92012-04-27 13:10:52 +03001363 /* Recent versions do this automatically */
1364 if (dwc->revision < DWC3_REVISION_194A) {
1365 /* write zeroes to Link Change Request */
Felipe Balbifcc023c2012-05-24 10:27:56 +03001366 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
Paul Zimmerman802fde92012-04-27 13:10:52 +03001367 reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK;
1368 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1369 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001370
Paul Zimmerman1d046792012-02-15 18:56:56 -08001371 /* poll until Link State changes to ON */
Felipe Balbi72246da2011-08-19 18:10:58 +03001372 timeout = jiffies + msecs_to_jiffies(100);
1373
Paul Zimmerman1d046792012-02-15 18:56:56 -08001374 while (!time_after(jiffies, timeout)) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001375 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1376
1377 /* in HS, means ON */
1378 if (DWC3_DSTS_USBLNKST(reg) == DWC3_LINK_STATE_U0)
1379 break;
1380 }
1381
1382 if (DWC3_DSTS_USBLNKST(reg) != DWC3_LINK_STATE_U0) {
1383 dev_err(dwc->dev, "failed to send remote wakeup\n");
1384 ret = -EINVAL;
1385 }
1386
1387out:
1388 spin_unlock_irqrestore(&dwc->lock, flags);
1389
1390 return ret;
1391}
1392
1393static int dwc3_gadget_set_selfpowered(struct usb_gadget *g,
1394 int is_selfpowered)
1395{
1396 struct dwc3 *dwc = gadget_to_dwc(g);
Paul Zimmerman249a4562012-02-24 17:32:16 -08001397 unsigned long flags;
Felipe Balbi72246da2011-08-19 18:10:58 +03001398
Paul Zimmerman249a4562012-02-24 17:32:16 -08001399 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001400 dwc->is_selfpowered = !!is_selfpowered;
Paul Zimmerman249a4562012-02-24 17:32:16 -08001401 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001402
1403 return 0;
1404}
1405
Pratyush Anand6f17f742012-07-02 10:21:55 +05301406static int dwc3_gadget_run_stop(struct dwc3 *dwc, int is_on)
Felipe Balbi72246da2011-08-19 18:10:58 +03001407{
1408 u32 reg;
Sebastian Andrzej Siewior61d58242011-08-29 16:46:38 +02001409 u32 timeout = 500;
Felipe Balbi72246da2011-08-19 18:10:58 +03001410
1411 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
Felipe Balbi8db7ed12012-01-18 18:32:29 +02001412 if (is_on) {
Paul Zimmerman802fde92012-04-27 13:10:52 +03001413 if (dwc->revision <= DWC3_REVISION_187A) {
1414 reg &= ~DWC3_DCTL_TRGTULST_MASK;
1415 reg |= DWC3_DCTL_TRGTULST_RX_DET;
1416 }
1417
1418 if (dwc->revision >= DWC3_REVISION_194A)
1419 reg &= ~DWC3_DCTL_KEEP_CONNECT;
1420 reg |= DWC3_DCTL_RUN_STOP;
Felipe Balbi8db7ed12012-01-18 18:32:29 +02001421 } else {
Felipe Balbi72246da2011-08-19 18:10:58 +03001422 reg &= ~DWC3_DCTL_RUN_STOP;
Felipe Balbi8db7ed12012-01-18 18:32:29 +02001423 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001424
1425 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1426
1427 do {
1428 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1429 if (is_on) {
1430 if (!(reg & DWC3_DSTS_DEVCTRLHLT))
1431 break;
1432 } else {
1433 if (reg & DWC3_DSTS_DEVCTRLHLT)
1434 break;
1435 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001436 timeout--;
1437 if (!timeout)
Pratyush Anand6f17f742012-07-02 10:21:55 +05301438 return -ETIMEDOUT;
Sebastian Andrzej Siewior61d58242011-08-29 16:46:38 +02001439 udelay(1);
Felipe Balbi72246da2011-08-19 18:10:58 +03001440 } while (1);
1441
1442 dev_vdbg(dwc->dev, "gadget %s data soft-%s\n",
1443 dwc->gadget_driver
1444 ? dwc->gadget_driver->function : "no-function",
1445 is_on ? "connect" : "disconnect");
Pratyush Anand6f17f742012-07-02 10:21:55 +05301446
1447 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +03001448}
1449
1450static int dwc3_gadget_pullup(struct usb_gadget *g, int is_on)
1451{
1452 struct dwc3 *dwc = gadget_to_dwc(g);
1453 unsigned long flags;
Pratyush Anand6f17f742012-07-02 10:21:55 +05301454 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001455
1456 is_on = !!is_on;
1457
1458 spin_lock_irqsave(&dwc->lock, flags);
Pratyush Anand6f17f742012-07-02 10:21:55 +05301459 ret = dwc3_gadget_run_stop(dwc, is_on);
Felipe Balbi72246da2011-08-19 18:10:58 +03001460 spin_unlock_irqrestore(&dwc->lock, flags);
1461
Pratyush Anand6f17f742012-07-02 10:21:55 +05301462 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001463}
1464
1465static int dwc3_gadget_start(struct usb_gadget *g,
1466 struct usb_gadget_driver *driver)
1467{
1468 struct dwc3 *dwc = gadget_to_dwc(g);
1469 struct dwc3_ep *dep;
1470 unsigned long flags;
1471 int ret = 0;
1472 u32 reg;
1473
1474 spin_lock_irqsave(&dwc->lock, flags);
1475
1476 if (dwc->gadget_driver) {
1477 dev_err(dwc->dev, "%s is already bound to %s\n",
1478 dwc->gadget.name,
1479 dwc->gadget_driver->driver.name);
1480 ret = -EBUSY;
1481 goto err0;
1482 }
1483
1484 dwc->gadget_driver = driver;
1485 dwc->gadget.dev.driver = &driver->driver;
1486
Felipe Balbi72246da2011-08-19 18:10:58 +03001487 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
1488 reg &= ~(DWC3_DCFG_SPEED_MASK);
Felipe Balbi07e7f472012-03-23 12:20:31 +02001489
1490 /**
1491 * WORKAROUND: DWC3 revision < 2.20a have an issue
1492 * which would cause metastability state on Run/Stop
1493 * bit if we try to force the IP to USB2-only mode.
1494 *
1495 * Because of that, we cannot configure the IP to any
1496 * speed other than the SuperSpeed
1497 *
1498 * Refers to:
1499 *
1500 * STAR#9000525659: Clock Domain Crossing on DCTL in
1501 * USB 2.0 Mode
1502 */
1503 if (dwc->revision < DWC3_REVISION_220A)
1504 reg |= DWC3_DCFG_SUPERSPEED;
1505 else
1506 reg |= dwc->maximum_speed;
Felipe Balbi72246da2011-08-19 18:10:58 +03001507 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
1508
Paul Zimmermanb23c8432011-09-30 10:58:42 +03001509 dwc->start_config_issued = false;
1510
Felipe Balbi72246da2011-08-19 18:10:58 +03001511 /* Start with SuperSpeed Default */
1512 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
1513
1514 dep = dwc->eps[0];
Felipe Balbi4b345c92012-07-16 14:08:16 +03001515 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, false);
Felipe Balbi72246da2011-08-19 18:10:58 +03001516 if (ret) {
1517 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
1518 goto err0;
1519 }
1520
1521 dep = dwc->eps[1];
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 err1;
1526 }
1527
1528 /* begin to receive SETUP packets */
Felipe Balbic7fcdeb2011-08-27 22:28:36 +03001529 dwc->ep0state = EP0_SETUP_PHASE;
Felipe Balbi72246da2011-08-19 18:10:58 +03001530 dwc3_ep0_out_start(dwc);
1531
1532 spin_unlock_irqrestore(&dwc->lock, flags);
1533
1534 return 0;
1535
1536err1:
1537 __dwc3_gadget_ep_disable(dwc->eps[0]);
1538
1539err0:
1540 spin_unlock_irqrestore(&dwc->lock, flags);
1541
1542 return ret;
1543}
1544
1545static int dwc3_gadget_stop(struct usb_gadget *g,
1546 struct usb_gadget_driver *driver)
1547{
1548 struct dwc3 *dwc = gadget_to_dwc(g);
1549 unsigned long flags;
1550
1551 spin_lock_irqsave(&dwc->lock, flags);
1552
1553 __dwc3_gadget_ep_disable(dwc->eps[0]);
1554 __dwc3_gadget_ep_disable(dwc->eps[1]);
1555
1556 dwc->gadget_driver = NULL;
1557 dwc->gadget.dev.driver = NULL;
1558
1559 spin_unlock_irqrestore(&dwc->lock, flags);
1560
1561 return 0;
1562}
Paul Zimmerman802fde92012-04-27 13:10:52 +03001563
Felipe Balbi72246da2011-08-19 18:10:58 +03001564static const struct usb_gadget_ops dwc3_gadget_ops = {
1565 .get_frame = dwc3_gadget_get_frame,
1566 .wakeup = dwc3_gadget_wakeup,
1567 .set_selfpowered = dwc3_gadget_set_selfpowered,
1568 .pullup = dwc3_gadget_pullup,
1569 .udc_start = dwc3_gadget_start,
1570 .udc_stop = dwc3_gadget_stop,
1571};
1572
1573/* -------------------------------------------------------------------------- */
1574
Bill Pemberton41ac7b32012-11-19 13:21:48 -05001575static int dwc3_gadget_init_endpoints(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +03001576{
1577 struct dwc3_ep *dep;
1578 u8 epnum;
1579
1580 INIT_LIST_HEAD(&dwc->gadget.ep_list);
1581
1582 for (epnum = 0; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
1583 dep = kzalloc(sizeof(*dep), GFP_KERNEL);
1584 if (!dep) {
1585 dev_err(dwc->dev, "can't allocate endpoint %d\n",
1586 epnum);
1587 return -ENOMEM;
1588 }
1589
1590 dep->dwc = dwc;
1591 dep->number = epnum;
1592 dwc->eps[epnum] = dep;
1593
1594 snprintf(dep->name, sizeof(dep->name), "ep%d%s", epnum >> 1,
1595 (epnum & 1) ? "in" : "out");
1596 dep->endpoint.name = dep->name;
1597 dep->direction = (epnum & 1);
1598
1599 if (epnum == 0 || epnum == 1) {
1600 dep->endpoint.maxpacket = 512;
1601 dep->endpoint.ops = &dwc3_gadget_ep0_ops;
1602 if (!epnum)
1603 dwc->gadget.ep0 = &dep->endpoint;
1604 } else {
1605 int ret;
1606
1607 dep->endpoint.maxpacket = 1024;
Sebastian Andrzej Siewior12d36c12011-11-03 20:27:50 +01001608 dep->endpoint.max_streams = 15;
Felipe Balbi72246da2011-08-19 18:10:58 +03001609 dep->endpoint.ops = &dwc3_gadget_ep_ops;
1610 list_add_tail(&dep->endpoint.ep_list,
1611 &dwc->gadget.ep_list);
1612
1613 ret = dwc3_alloc_trb_pool(dep);
Felipe Balbi25b8ff62011-11-04 12:32:47 +02001614 if (ret)
Felipe Balbi72246da2011-08-19 18:10:58 +03001615 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001616 }
Felipe Balbi25b8ff62011-11-04 12:32:47 +02001617
Felipe Balbi72246da2011-08-19 18:10:58 +03001618 INIT_LIST_HEAD(&dep->request_list);
1619 INIT_LIST_HEAD(&dep->req_queued);
1620 }
1621
1622 return 0;
1623}
1624
1625static void dwc3_gadget_free_endpoints(struct dwc3 *dwc)
1626{
1627 struct dwc3_ep *dep;
1628 u8 epnum;
1629
1630 for (epnum = 0; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
1631 dep = dwc->eps[epnum];
1632 dwc3_free_trb_pool(dep);
1633
1634 if (epnum != 0 && epnum != 1)
1635 list_del(&dep->endpoint.ep_list);
1636
1637 kfree(dep);
1638 }
1639}
1640
1641static void dwc3_gadget_release(struct device *dev)
1642{
1643 dev_dbg(dev, "%s\n", __func__);
1644}
1645
1646/* -------------------------------------------------------------------------- */
1647static int dwc3_cleanup_done_reqs(struct dwc3 *dwc, struct dwc3_ep *dep,
1648 const struct dwc3_event_depevt *event, int status)
1649{
1650 struct dwc3_request *req;
Felipe Balbif6bafc62012-02-06 11:04:53 +02001651 struct dwc3_trb *trb;
Felipe Balbi72246da2011-08-19 18:10:58 +03001652 unsigned int count;
1653 unsigned int s_pkt = 0;
Pratyush Anandd6d6ec72012-05-25 18:54:56 +05301654 unsigned int trb_status;
Felipe Balbi72246da2011-08-19 18:10:58 +03001655
1656 do {
1657 req = next_request(&dep->req_queued);
Sebastian Andrzej Siewiord39ee7b2011-11-03 10:32:20 +01001658 if (!req) {
1659 WARN_ON_ONCE(1);
1660 return 1;
1661 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001662
Felipe Balbif6bafc62012-02-06 11:04:53 +02001663 trb = req->trb;
Felipe Balbi72246da2011-08-19 18:10:58 +03001664
Felipe Balbif6bafc62012-02-06 11:04:53 +02001665 if ((trb->ctrl & DWC3_TRB_CTRL_HWO) && status != -ESHUTDOWN)
Sebastian Andrzej Siewior0d2f4752011-08-19 19:59:12 +02001666 /*
1667 * We continue despite the error. There is not much we
Paul Zimmerman1d046792012-02-15 18:56:56 -08001668 * can do. If we don't clean it up we loop forever. If
1669 * we skip the TRB then it gets overwritten after a
1670 * while since we use them in a ring buffer. A BUG()
1671 * would help. Lets hope that if this occurs, someone
Sebastian Andrzej Siewior0d2f4752011-08-19 19:59:12 +02001672 * fixes the root cause instead of looking away :)
1673 */
Felipe Balbi72246da2011-08-19 18:10:58 +03001674 dev_err(dwc->dev, "%s's TRB (%p) still owned by HW\n",
1675 dep->name, req->trb);
Felipe Balbif6bafc62012-02-06 11:04:53 +02001676 count = trb->size & DWC3_TRB_SIZE_MASK;
Felipe Balbi72246da2011-08-19 18:10:58 +03001677
1678 if (dep->direction) {
1679 if (count) {
Pratyush Anandd6d6ec72012-05-25 18:54:56 +05301680 trb_status = DWC3_TRB_SIZE_TRBSTS(trb->size);
1681 if (trb_status == DWC3_TRBSTS_MISSED_ISOC) {
1682 dev_dbg(dwc->dev, "incomplete IN transfer %s\n",
1683 dep->name);
Pratyush Anand7efea862013-01-14 15:59:32 +05301684 /*
1685 * If missed isoc occurred and there is
1686 * no request queued then issue END
1687 * TRANSFER, so that core generates
1688 * next xfernotready and we will issue
1689 * a fresh START TRANSFER.
1690 * If there are still queued request
1691 * then wait, do not issue either END
1692 * or UPDATE TRANSFER, just attach next
1693 * request in request_list during
1694 * giveback.If any future queued request
1695 * is successfully transferred then we
1696 * will issue UPDATE TRANSFER for all
1697 * request in the request_list.
1698 */
Pratyush Anandd6d6ec72012-05-25 18:54:56 +05301699 dep->flags |= DWC3_EP_MISSED_ISOC;
1700 } else {
1701 dev_err(dwc->dev, "incomplete IN transfer %s\n",
1702 dep->name);
1703 status = -ECONNRESET;
1704 }
Pratyush Anand7efea862013-01-14 15:59:32 +05301705 } else {
1706 dep->flags &= ~DWC3_EP_MISSED_ISOC;
Felipe Balbi72246da2011-08-19 18:10:58 +03001707 }
1708 } else {
1709 if (count && (event->status & DEPEVT_STATUS_SHORT))
1710 s_pkt = 1;
1711 }
1712
1713 /*
1714 * We assume here we will always receive the entire data block
1715 * which we should receive. Meaning, if we program RX to
1716 * receive 4K but we receive only 2K, we assume that's all we
1717 * should receive and we simply bounce the request back to the
1718 * gadget driver for further processing.
1719 */
1720 req->request.actual += req->request.length - count;
1721 dwc3_gadget_giveback(dep, req, status);
1722 if (s_pkt)
1723 break;
Felipe Balbif6bafc62012-02-06 11:04:53 +02001724 if ((event->status & DEPEVT_STATUS_LST) &&
Pratyush Anand70b674b2012-06-03 19:43:19 +05301725 (trb->ctrl & (DWC3_TRB_CTRL_LST |
1726 DWC3_TRB_CTRL_HWO)))
Felipe Balbi72246da2011-08-19 18:10:58 +03001727 break;
Felipe Balbif6bafc62012-02-06 11:04:53 +02001728 if ((event->status & DEPEVT_STATUS_IOC) &&
1729 (trb->ctrl & DWC3_TRB_CTRL_IOC))
Felipe Balbi72246da2011-08-19 18:10:58 +03001730 break;
1731 } while (1);
1732
Pratyush Anandcdc359d2013-01-14 15:59:34 +05301733 if (usb_endpoint_xfer_isoc(dep->endpoint.desc) &&
1734 list_empty(&dep->req_queued)) {
1735 if (list_empty(&dep->request_list)) {
1736 /*
1737 * If there is no entry in request list then do
1738 * not issue END TRANSFER now. Just set PENDING
1739 * flag, so that END TRANSFER is issued when an
1740 * entry is added into request list.
1741 */
1742 dep->flags = DWC3_EP_PENDING_REQUEST;
1743 } else {
1744 dwc3_stop_active_transfer(dwc, dep->number);
1745 dep->flags = DWC3_EP_ENABLED;
1746 }
Pratyush Anand7efea862013-01-14 15:59:32 +05301747 return 1;
1748 }
1749
Felipe Balbif6bafc62012-02-06 11:04:53 +02001750 if ((event->status & DEPEVT_STATUS_IOC) &&
1751 (trb->ctrl & DWC3_TRB_CTRL_IOC))
Felipe Balbi72246da2011-08-19 18:10:58 +03001752 return 0;
1753 return 1;
1754}
1755
1756static void dwc3_endpoint_transfer_complete(struct dwc3 *dwc,
1757 struct dwc3_ep *dep, const struct dwc3_event_depevt *event,
1758 int start_new)
1759{
1760 unsigned status = 0;
1761 int clean_busy;
1762
1763 if (event->status & DEPEVT_STATUS_BUSERR)
1764 status = -ECONNRESET;
1765
Paul Zimmerman1d046792012-02-15 18:56:56 -08001766 clean_busy = dwc3_cleanup_done_reqs(dwc, dep, event, status);
Paul Zimmermanc2df85c2012-02-24 17:32:18 -08001767 if (clean_busy)
Felipe Balbi72246da2011-08-19 18:10:58 +03001768 dep->flags &= ~DWC3_EP_BUSY;
Felipe Balbifae2b902011-10-14 13:00:30 +03001769
1770 /*
1771 * WORKAROUND: This is the 2nd half of U1/U2 -> U0 workaround.
1772 * See dwc3_gadget_linksts_change_interrupt() for 1st half.
1773 */
1774 if (dwc->revision < DWC3_REVISION_183A) {
1775 u32 reg;
1776 int i;
1777
1778 for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) {
Moiz Sonasath348e0262012-08-01 14:08:30 -05001779 dep = dwc->eps[i];
Felipe Balbifae2b902011-10-14 13:00:30 +03001780
1781 if (!(dep->flags & DWC3_EP_ENABLED))
1782 continue;
1783
1784 if (!list_empty(&dep->req_queued))
1785 return;
1786 }
1787
1788 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
1789 reg |= dwc->u1u2;
1790 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1791
1792 dwc->u1u2 = 0;
1793 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001794}
1795
Felipe Balbi72246da2011-08-19 18:10:58 +03001796static void dwc3_endpoint_interrupt(struct dwc3 *dwc,
1797 const struct dwc3_event_depevt *event)
1798{
1799 struct dwc3_ep *dep;
1800 u8 epnum = event->endpoint_number;
1801
1802 dep = dwc->eps[epnum];
1803
Felipe Balbi3336abb2012-06-06 09:19:35 +03001804 if (!(dep->flags & DWC3_EP_ENABLED))
1805 return;
1806
Felipe Balbi72246da2011-08-19 18:10:58 +03001807 dev_vdbg(dwc->dev, "%s: %s\n", dep->name,
1808 dwc3_ep_event_string(event->endpoint_event));
1809
1810 if (epnum == 0 || epnum == 1) {
1811 dwc3_ep0_interrupt(dwc, event);
1812 return;
1813 }
1814
1815 switch (event->endpoint_event) {
1816 case DWC3_DEPEVT_XFERCOMPLETE:
Felipe Balbib4996a82012-06-06 12:04:13 +03001817 dep->resource_index = 0;
Paul Zimmermanc2df85c2012-02-24 17:32:18 -08001818
Ido Shayevitz16e78db2012-03-12 20:25:24 +02001819 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001820 dev_dbg(dwc->dev, "%s is an Isochronous endpoint\n",
1821 dep->name);
1822 return;
1823 }
1824
1825 dwc3_endpoint_transfer_complete(dwc, dep, event, 1);
1826 break;
1827 case DWC3_DEPEVT_XFERINPROGRESS:
Ido Shayevitz16e78db2012-03-12 20:25:24 +02001828 if (!usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001829 dev_dbg(dwc->dev, "%s is not an Isochronous endpoint\n",
1830 dep->name);
1831 return;
1832 }
1833
1834 dwc3_endpoint_transfer_complete(dwc, dep, event, 0);
1835 break;
1836 case DWC3_DEPEVT_XFERNOTREADY:
Ido Shayevitz16e78db2012-03-12 20:25:24 +02001837 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001838 dwc3_gadget_start_isoc(dwc, dep, event);
1839 } else {
1840 int ret;
1841
1842 dev_vdbg(dwc->dev, "%s: reason %s\n",
Felipe Balbi40aa41f2012-01-18 17:06:03 +02001843 dep->name, event->status &
1844 DEPEVT_STATUS_TRANSFER_ACTIVE
Felipe Balbi72246da2011-08-19 18:10:58 +03001845 ? "Transfer Active"
1846 : "Transfer Not Active");
1847
1848 ret = __dwc3_gadget_kick_transfer(dep, 0, 1);
1849 if (!ret || ret == -EBUSY)
1850 return;
1851
1852 dev_dbg(dwc->dev, "%s: failed to kick transfers\n",
1853 dep->name);
1854 }
1855
1856 break;
Felipe Balbi879631a2011-09-30 10:58:47 +03001857 case DWC3_DEPEVT_STREAMEVT:
Ido Shayevitz16e78db2012-03-12 20:25:24 +02001858 if (!usb_endpoint_xfer_bulk(dep->endpoint.desc)) {
Felipe Balbi879631a2011-09-30 10:58:47 +03001859 dev_err(dwc->dev, "Stream event for non-Bulk %s\n",
1860 dep->name);
1861 return;
1862 }
1863
1864 switch (event->status) {
1865 case DEPEVT_STREAMEVT_FOUND:
1866 dev_vdbg(dwc->dev, "Stream %d found and started\n",
1867 event->parameters);
1868
1869 break;
1870 case DEPEVT_STREAMEVT_NOTFOUND:
1871 /* FALLTHROUGH */
1872 default:
1873 dev_dbg(dwc->dev, "Couldn't find suitable stream\n");
1874 }
1875 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03001876 case DWC3_DEPEVT_RXTXFIFOEVT:
1877 dev_dbg(dwc->dev, "%s FIFO Overrun\n", dep->name);
1878 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03001879 case DWC3_DEPEVT_EPCMDCMPLT:
Felipe Balbiea53b882012-02-17 12:10:04 +02001880 dev_vdbg(dwc->dev, "Endpoint Command Complete\n");
Felipe Balbi72246da2011-08-19 18:10:58 +03001881 break;
1882 }
1883}
1884
1885static void dwc3_disconnect_gadget(struct dwc3 *dwc)
1886{
1887 if (dwc->gadget_driver && dwc->gadget_driver->disconnect) {
1888 spin_unlock(&dwc->lock);
1889 dwc->gadget_driver->disconnect(&dwc->gadget);
1890 spin_lock(&dwc->lock);
1891 }
1892}
1893
1894static void dwc3_stop_active_transfer(struct dwc3 *dwc, u32 epnum)
1895{
1896 struct dwc3_ep *dep;
1897 struct dwc3_gadget_ep_cmd_params params;
1898 u32 cmd;
1899 int ret;
1900
1901 dep = dwc->eps[epnum];
1902
Felipe Balbib4996a82012-06-06 12:04:13 +03001903 if (!dep->resource_index)
Pratyush Anand3daf74d2012-06-23 02:23:08 +05301904 return;
1905
Pratyush Anand57911502012-07-06 15:19:10 +05301906 /*
1907 * NOTICE: We are violating what the Databook says about the
1908 * EndTransfer command. Ideally we would _always_ wait for the
1909 * EndTransfer Command Completion IRQ, but that's causing too
1910 * much trouble synchronizing between us and gadget driver.
1911 *
1912 * We have discussed this with the IP Provider and it was
1913 * suggested to giveback all requests here, but give HW some
1914 * extra time to synchronize with the interconnect. We're using
1915 * an arbitraty 100us delay for that.
1916 *
1917 * Note also that a similar handling was tested by Synopsys
1918 * (thanks a lot Paul) and nothing bad has come out of it.
1919 * In short, what we're doing is:
1920 *
1921 * - Issue EndTransfer WITH CMDIOC bit set
1922 * - Wait 100us
1923 */
1924
Pratyush Anand3daf74d2012-06-23 02:23:08 +05301925 cmd = DWC3_DEPCMD_ENDTRANSFER;
1926 cmd |= DWC3_DEPCMD_HIPRI_FORCERM | DWC3_DEPCMD_CMDIOC;
Felipe Balbib4996a82012-06-06 12:04:13 +03001927 cmd |= DWC3_DEPCMD_PARAM(dep->resource_index);
Pratyush Anand3daf74d2012-06-23 02:23:08 +05301928 memset(&params, 0, sizeof(params));
1929 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number, cmd, &params);
1930 WARN_ON_ONCE(ret);
Felipe Balbib4996a82012-06-06 12:04:13 +03001931 dep->resource_index = 0;
Felipe Balbi041d81f2012-10-04 11:58:00 +03001932 dep->flags &= ~DWC3_EP_BUSY;
Pratyush Anand57911502012-07-06 15:19:10 +05301933 udelay(100);
Felipe Balbi72246da2011-08-19 18:10:58 +03001934}
1935
1936static void dwc3_stop_active_transfers(struct dwc3 *dwc)
1937{
1938 u32 epnum;
1939
1940 for (epnum = 2; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
1941 struct dwc3_ep *dep;
1942
1943 dep = dwc->eps[epnum];
1944 if (!(dep->flags & DWC3_EP_ENABLED))
1945 continue;
1946
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +02001947 dwc3_remove_requests(dwc, dep);
Felipe Balbi72246da2011-08-19 18:10:58 +03001948 }
1949}
1950
1951static void dwc3_clear_stall_all_ep(struct dwc3 *dwc)
1952{
1953 u32 epnum;
1954
1955 for (epnum = 1; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
1956 struct dwc3_ep *dep;
1957 struct dwc3_gadget_ep_cmd_params params;
1958 int ret;
1959
1960 dep = dwc->eps[epnum];
1961
1962 if (!(dep->flags & DWC3_EP_STALL))
1963 continue;
1964
1965 dep->flags &= ~DWC3_EP_STALL;
1966
1967 memset(&params, 0, sizeof(params));
1968 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number,
1969 DWC3_DEPCMD_CLEARSTALL, &params);
1970 WARN_ON_ONCE(ret);
1971 }
1972}
1973
1974static void dwc3_gadget_disconnect_interrupt(struct dwc3 *dwc)
1975{
Felipe Balbic4430a22012-05-24 10:30:01 +03001976 int reg;
1977
Felipe Balbi72246da2011-08-19 18:10:58 +03001978 dev_vdbg(dwc->dev, "%s\n", __func__);
Felipe Balbi72246da2011-08-19 18:10:58 +03001979
1980 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
1981 reg &= ~DWC3_DCTL_INITU1ENA;
1982 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1983
1984 reg &= ~DWC3_DCTL_INITU2ENA;
1985 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +03001986
Felipe Balbi72246da2011-08-19 18:10:58 +03001987 dwc3_disconnect_gadget(dwc);
Paul Zimmermanb23c8432011-09-30 10:58:42 +03001988 dwc->start_config_issued = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03001989
1990 dwc->gadget.speed = USB_SPEED_UNKNOWN;
Felipe Balbidf62df52011-10-14 15:11:49 +03001991 dwc->setup_packet_pending = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03001992}
1993
Paul Zimmermand7a46a82012-04-27 12:54:05 +03001994static void dwc3_gadget_usb3_phy_suspend(struct dwc3 *dwc, int suspend)
Felipe Balbi72246da2011-08-19 18:10:58 +03001995{
1996 u32 reg;
1997
1998 reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0));
1999
Paul Zimmermand7a46a82012-04-27 12:54:05 +03002000 if (suspend)
Felipe Balbi72246da2011-08-19 18:10:58 +03002001 reg |= DWC3_GUSB3PIPECTL_SUSPHY;
Paul Zimmermand7a46a82012-04-27 12:54:05 +03002002 else
2003 reg &= ~DWC3_GUSB3PIPECTL_SUSPHY;
Felipe Balbi72246da2011-08-19 18:10:58 +03002004
2005 dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg);
2006}
2007
Paul Zimmermand7a46a82012-04-27 12:54:05 +03002008static void dwc3_gadget_usb2_phy_suspend(struct dwc3 *dwc, int suspend)
Felipe Balbi72246da2011-08-19 18:10:58 +03002009{
2010 u32 reg;
2011
2012 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
2013
Paul Zimmermand7a46a82012-04-27 12:54:05 +03002014 if (suspend)
Felipe Balbi72246da2011-08-19 18:10:58 +03002015 reg |= DWC3_GUSB2PHYCFG_SUSPHY;
Paul Zimmermand7a46a82012-04-27 12:54:05 +03002016 else
2017 reg &= ~DWC3_GUSB2PHYCFG_SUSPHY;
Felipe Balbi72246da2011-08-19 18:10:58 +03002018
2019 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
2020}
2021
2022static void dwc3_gadget_reset_interrupt(struct dwc3 *dwc)
2023{
2024 u32 reg;
2025
2026 dev_vdbg(dwc->dev, "%s\n", __func__);
2027
Felipe Balbidf62df52011-10-14 15:11:49 +03002028 /*
2029 * WORKAROUND: DWC3 revisions <1.88a have an issue which
2030 * would cause a missing Disconnect Event if there's a
2031 * pending Setup Packet in the FIFO.
2032 *
2033 * There's no suggested workaround on the official Bug
2034 * report, which states that "unless the driver/application
2035 * is doing any special handling of a disconnect event,
2036 * there is no functional issue".
2037 *
2038 * Unfortunately, it turns out that we _do_ some special
2039 * handling of a disconnect event, namely complete all
2040 * pending transfers, notify gadget driver of the
2041 * disconnection, and so on.
2042 *
2043 * Our suggested workaround is to follow the Disconnect
2044 * Event steps here, instead, based on a setup_packet_pending
2045 * flag. Such flag gets set whenever we have a XferNotReady
2046 * event on EP0 and gets cleared on XferComplete for the
2047 * same endpoint.
2048 *
2049 * Refers to:
2050 *
2051 * STAR#9000466709: RTL: Device : Disconnect event not
2052 * generated if setup packet pending in FIFO
2053 */
2054 if (dwc->revision < DWC3_REVISION_188A) {
2055 if (dwc->setup_packet_pending)
2056 dwc3_gadget_disconnect_interrupt(dwc);
2057 }
2058
Felipe Balbi961906e2011-12-20 15:37:21 +02002059 /* after reset -> Default State */
2060 dwc->dev_state = DWC3_DEFAULT_STATE;
2061
Paul Zimmerman802fde92012-04-27 13:10:52 +03002062 /* Recent versions support automatic phy suspend and don't need this */
2063 if (dwc->revision < DWC3_REVISION_194A) {
2064 /* Resume PHYs */
2065 dwc3_gadget_usb2_phy_suspend(dwc, false);
2066 dwc3_gadget_usb3_phy_suspend(dwc, false);
2067 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002068
2069 if (dwc->gadget.speed != USB_SPEED_UNKNOWN)
2070 dwc3_disconnect_gadget(dwc);
2071
2072 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2073 reg &= ~DWC3_DCTL_TSTCTRL_MASK;
2074 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Gerard Cauvy3b637362012-02-10 12:21:18 +02002075 dwc->test_mode = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03002076
2077 dwc3_stop_active_transfers(dwc);
2078 dwc3_clear_stall_all_ep(dwc);
Paul Zimmermanb23c8432011-09-30 10:58:42 +03002079 dwc->start_config_issued = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03002080
2081 /* Reset device address to zero */
2082 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2083 reg &= ~(DWC3_DCFG_DEVADDR_MASK);
2084 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +03002085}
2086
2087static void dwc3_update_ram_clk_sel(struct dwc3 *dwc, u32 speed)
2088{
2089 u32 reg;
2090 u32 usb30_clock = DWC3_GCTL_CLK_BUS;
2091
2092 /*
2093 * We change the clock only at SS but I dunno why I would want to do
2094 * this. Maybe it becomes part of the power saving plan.
2095 */
2096
2097 if (speed != DWC3_DSTS_SUPERSPEED)
2098 return;
2099
2100 /*
2101 * RAMClkSel is reset to 0 after USB reset, so it must be reprogrammed
2102 * each time on Connect Done.
2103 */
2104 if (!usb30_clock)
2105 return;
2106
2107 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
2108 reg |= DWC3_GCTL_RAMCLKSEL(usb30_clock);
2109 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
2110}
2111
Paul Zimmermand7a46a82012-04-27 12:54:05 +03002112static void dwc3_gadget_phy_suspend(struct dwc3 *dwc, u8 speed)
Felipe Balbi72246da2011-08-19 18:10:58 +03002113{
2114 switch (speed) {
2115 case USB_SPEED_SUPER:
Paul Zimmermand7a46a82012-04-27 12:54:05 +03002116 dwc3_gadget_usb2_phy_suspend(dwc, true);
Felipe Balbi72246da2011-08-19 18:10:58 +03002117 break;
2118 case USB_SPEED_HIGH:
2119 case USB_SPEED_FULL:
2120 case USB_SPEED_LOW:
Paul Zimmermand7a46a82012-04-27 12:54:05 +03002121 dwc3_gadget_usb3_phy_suspend(dwc, true);
Felipe Balbi72246da2011-08-19 18:10:58 +03002122 break;
2123 }
2124}
2125
2126static void dwc3_gadget_conndone_interrupt(struct dwc3 *dwc)
2127{
2128 struct dwc3_gadget_ep_cmd_params params;
2129 struct dwc3_ep *dep;
2130 int ret;
2131 u32 reg;
2132 u8 speed;
2133
2134 dev_vdbg(dwc->dev, "%s\n", __func__);
2135
2136 memset(&params, 0x00, sizeof(params));
2137
Felipe Balbi72246da2011-08-19 18:10:58 +03002138 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
2139 speed = reg & DWC3_DSTS_CONNECTSPD;
2140 dwc->speed = speed;
2141
2142 dwc3_update_ram_clk_sel(dwc, speed);
2143
2144 switch (speed) {
2145 case DWC3_DCFG_SUPERSPEED:
Felipe Balbi05870c52011-10-14 14:51:38 +03002146 /*
2147 * WORKAROUND: DWC3 revisions <1.90a have an issue which
2148 * would cause a missing USB3 Reset event.
2149 *
2150 * In such situations, we should force a USB3 Reset
2151 * event by calling our dwc3_gadget_reset_interrupt()
2152 * routine.
2153 *
2154 * Refers to:
2155 *
2156 * STAR#9000483510: RTL: SS : USB3 reset event may
2157 * not be generated always when the link enters poll
2158 */
2159 if (dwc->revision < DWC3_REVISION_190A)
2160 dwc3_gadget_reset_interrupt(dwc);
2161
Felipe Balbi72246da2011-08-19 18:10:58 +03002162 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
2163 dwc->gadget.ep0->maxpacket = 512;
2164 dwc->gadget.speed = USB_SPEED_SUPER;
2165 break;
2166 case DWC3_DCFG_HIGHSPEED:
2167 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
2168 dwc->gadget.ep0->maxpacket = 64;
2169 dwc->gadget.speed = USB_SPEED_HIGH;
2170 break;
2171 case DWC3_DCFG_FULLSPEED2:
2172 case DWC3_DCFG_FULLSPEED1:
2173 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
2174 dwc->gadget.ep0->maxpacket = 64;
2175 dwc->gadget.speed = USB_SPEED_FULL;
2176 break;
2177 case DWC3_DCFG_LOWSPEED:
2178 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(8);
2179 dwc->gadget.ep0->maxpacket = 8;
2180 dwc->gadget.speed = USB_SPEED_LOW;
2181 break;
2182 }
2183
Pratyush Anand2b758352013-01-14 15:59:31 +05302184 /* Enable USB2 LPM Capability */
2185
2186 if ((dwc->revision > DWC3_REVISION_194A)
2187 && (speed != DWC3_DCFG_SUPERSPEED)) {
2188 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2189 reg |= DWC3_DCFG_LPM_CAP;
2190 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
2191
2192 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2193 reg &= ~(DWC3_DCTL_HIRD_THRES_MASK | DWC3_DCTL_L1_HIBER_EN);
2194
2195 /* TODO: This should be configurable */
2196 reg |= DWC3_DCTL_HIRD_THRES(28);
2197
2198 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2199 }
2200
Paul Zimmerman802fde92012-04-27 13:10:52 +03002201 /* Recent versions support automatic phy suspend and don't need this */
2202 if (dwc->revision < DWC3_REVISION_194A) {
2203 /* Suspend unneeded PHY */
2204 dwc3_gadget_phy_suspend(dwc, dwc->gadget.speed);
2205 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002206
2207 dep = dwc->eps[0];
Felipe Balbi4b345c92012-07-16 14:08:16 +03002208 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, true);
Felipe Balbi72246da2011-08-19 18:10:58 +03002209 if (ret) {
2210 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
2211 return;
2212 }
2213
2214 dep = dwc->eps[1];
Felipe Balbi4b345c92012-07-16 14:08:16 +03002215 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, true);
Felipe Balbi72246da2011-08-19 18:10:58 +03002216 if (ret) {
2217 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
2218 return;
2219 }
2220
2221 /*
2222 * Configure PHY via GUSB3PIPECTLn if required.
2223 *
2224 * Update GTXFIFOSIZn
2225 *
2226 * In both cases reset values should be sufficient.
2227 */
2228}
2229
2230static void dwc3_gadget_wakeup_interrupt(struct dwc3 *dwc)
2231{
2232 dev_vdbg(dwc->dev, "%s\n", __func__);
2233
2234 /*
2235 * TODO take core out of low power mode when that's
2236 * implemented.
2237 */
2238
2239 dwc->gadget_driver->resume(&dwc->gadget);
2240}
2241
2242static void dwc3_gadget_linksts_change_interrupt(struct dwc3 *dwc,
2243 unsigned int evtinfo)
2244{
Felipe Balbifae2b902011-10-14 13:00:30 +03002245 enum dwc3_link_state next = evtinfo & DWC3_LINK_STATE_MASK;
2246
2247 /*
2248 * WORKAROUND: DWC3 Revisions <1.83a have an issue which, depending
2249 * on the link partner, the USB session might do multiple entry/exit
2250 * of low power states before a transfer takes place.
2251 *
2252 * Due to this problem, we might experience lower throughput. The
2253 * suggested workaround is to disable DCTL[12:9] bits if we're
2254 * transitioning from U1/U2 to U0 and enable those bits again
2255 * after a transfer completes and there are no pending transfers
2256 * on any of the enabled endpoints.
2257 *
2258 * This is the first half of that workaround.
2259 *
2260 * Refers to:
2261 *
2262 * STAR#9000446952: RTL: Device SS : if U1/U2 ->U0 takes >128us
2263 * core send LGO_Ux entering U0
2264 */
2265 if (dwc->revision < DWC3_REVISION_183A) {
2266 if (next == DWC3_LINK_STATE_U0) {
2267 u32 u1u2;
2268 u32 reg;
2269
2270 switch (dwc->link_state) {
2271 case DWC3_LINK_STATE_U1:
2272 case DWC3_LINK_STATE_U2:
2273 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2274 u1u2 = reg & (DWC3_DCTL_INITU2ENA
2275 | DWC3_DCTL_ACCEPTU2ENA
2276 | DWC3_DCTL_INITU1ENA
2277 | DWC3_DCTL_ACCEPTU1ENA);
2278
2279 if (!dwc->u1u2)
2280 dwc->u1u2 = reg & u1u2;
2281
2282 reg &= ~u1u2;
2283
2284 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2285 break;
2286 default:
2287 /* do nothing */
2288 break;
2289 }
2290 }
2291 }
2292
2293 dwc->link_state = next;
Felipe Balbi019ac832011-09-08 21:18:47 +03002294
2295 dev_vdbg(dwc->dev, "%s link %d\n", __func__, dwc->link_state);
Felipe Balbi72246da2011-08-19 18:10:58 +03002296}
2297
2298static void dwc3_gadget_interrupt(struct dwc3 *dwc,
2299 const struct dwc3_event_devt *event)
2300{
2301 switch (event->type) {
2302 case DWC3_DEVICE_EVENT_DISCONNECT:
2303 dwc3_gadget_disconnect_interrupt(dwc);
2304 break;
2305 case DWC3_DEVICE_EVENT_RESET:
2306 dwc3_gadget_reset_interrupt(dwc);
2307 break;
2308 case DWC3_DEVICE_EVENT_CONNECT_DONE:
2309 dwc3_gadget_conndone_interrupt(dwc);
2310 break;
2311 case DWC3_DEVICE_EVENT_WAKEUP:
2312 dwc3_gadget_wakeup_interrupt(dwc);
2313 break;
2314 case DWC3_DEVICE_EVENT_LINK_STATUS_CHANGE:
2315 dwc3_gadget_linksts_change_interrupt(dwc, event->event_info);
2316 break;
2317 case DWC3_DEVICE_EVENT_EOPF:
2318 dev_vdbg(dwc->dev, "End of Periodic Frame\n");
2319 break;
2320 case DWC3_DEVICE_EVENT_SOF:
2321 dev_vdbg(dwc->dev, "Start of Periodic Frame\n");
2322 break;
2323 case DWC3_DEVICE_EVENT_ERRATIC_ERROR:
2324 dev_vdbg(dwc->dev, "Erratic Error\n");
2325 break;
2326 case DWC3_DEVICE_EVENT_CMD_CMPL:
2327 dev_vdbg(dwc->dev, "Command Complete\n");
2328 break;
2329 case DWC3_DEVICE_EVENT_OVERFLOW:
2330 dev_vdbg(dwc->dev, "Overflow\n");
2331 break;
2332 default:
2333 dev_dbg(dwc->dev, "UNKNOWN IRQ %d\n", event->type);
2334 }
2335}
2336
2337static void dwc3_process_event_entry(struct dwc3 *dwc,
2338 const union dwc3_event *event)
2339{
2340 /* Endpoint IRQ, handle it and return early */
2341 if (event->type.is_devspec == 0) {
2342 /* depevt */
2343 return dwc3_endpoint_interrupt(dwc, &event->depevt);
2344 }
2345
2346 switch (event->type.type) {
2347 case DWC3_EVENT_TYPE_DEV:
2348 dwc3_gadget_interrupt(dwc, &event->devt);
2349 break;
2350 /* REVISIT what to do with Carkit and I2C events ? */
2351 default:
2352 dev_err(dwc->dev, "UNKNOWN IRQ type %d\n", event->raw);
2353 }
2354}
2355
2356static irqreturn_t dwc3_process_event_buf(struct dwc3 *dwc, u32 buf)
2357{
2358 struct dwc3_event_buffer *evt;
2359 int left;
2360 u32 count;
2361
2362 count = dwc3_readl(dwc->regs, DWC3_GEVNTCOUNT(buf));
2363 count &= DWC3_GEVNTCOUNT_MASK;
2364 if (!count)
2365 return IRQ_NONE;
2366
2367 evt = dwc->ev_buffs[buf];
2368 left = count;
2369
2370 while (left > 0) {
2371 union dwc3_event event;
2372
Felipe Balbid70d8442012-02-06 13:40:17 +02002373 event.raw = *(u32 *) (evt->buf + evt->lpos);
2374
Felipe Balbi72246da2011-08-19 18:10:58 +03002375 dwc3_process_event_entry(dwc, &event);
2376 /*
2377 * XXX we wrap around correctly to the next entry as almost all
2378 * entries are 4 bytes in size. There is one entry which has 12
2379 * bytes which is a regular entry followed by 8 bytes data. ATM
2380 * I don't know how things are organized if were get next to the
2381 * a boundary so I worry about that once we try to handle that.
2382 */
2383 evt->lpos = (evt->lpos + 4) % DWC3_EVENT_BUFFERS_SIZE;
2384 left -= 4;
2385
2386 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(buf), 4);
2387 }
2388
2389 return IRQ_HANDLED;
2390}
2391
2392static irqreturn_t dwc3_interrupt(int irq, void *_dwc)
2393{
2394 struct dwc3 *dwc = _dwc;
2395 int i;
2396 irqreturn_t ret = IRQ_NONE;
2397
2398 spin_lock(&dwc->lock);
2399
Felipe Balbi9f622b22011-10-12 10:31:04 +03002400 for (i = 0; i < dwc->num_event_buffers; i++) {
Felipe Balbi72246da2011-08-19 18:10:58 +03002401 irqreturn_t status;
2402
2403 status = dwc3_process_event_buf(dwc, i);
2404 if (status == IRQ_HANDLED)
2405 ret = status;
2406 }
2407
2408 spin_unlock(&dwc->lock);
2409
2410 return ret;
2411}
2412
2413/**
2414 * dwc3_gadget_init - Initializes gadget related registers
Paul Zimmerman1d046792012-02-15 18:56:56 -08002415 * @dwc: pointer to our controller context structure
Felipe Balbi72246da2011-08-19 18:10:58 +03002416 *
2417 * Returns 0 on success otherwise negative errno.
2418 */
Bill Pemberton41ac7b32012-11-19 13:21:48 -05002419int dwc3_gadget_init(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +03002420{
2421 u32 reg;
2422 int ret;
2423 int irq;
2424
2425 dwc->ctrl_req = dma_alloc_coherent(dwc->dev, sizeof(*dwc->ctrl_req),
2426 &dwc->ctrl_req_addr, GFP_KERNEL);
2427 if (!dwc->ctrl_req) {
2428 dev_err(dwc->dev, "failed to allocate ctrl request\n");
2429 ret = -ENOMEM;
2430 goto err0;
2431 }
2432
2433 dwc->ep0_trb = dma_alloc_coherent(dwc->dev, sizeof(*dwc->ep0_trb),
2434 &dwc->ep0_trb_addr, GFP_KERNEL);
2435 if (!dwc->ep0_trb) {
2436 dev_err(dwc->dev, "failed to allocate ep0 trb\n");
2437 ret = -ENOMEM;
2438 goto err1;
2439 }
2440
Felipe Balbi3ef35fa2012-05-04 12:58:14 +03002441 dwc->setup_buf = kzalloc(DWC3_EP0_BOUNCE_SIZE, GFP_KERNEL);
Felipe Balbi72246da2011-08-19 18:10:58 +03002442 if (!dwc->setup_buf) {
2443 dev_err(dwc->dev, "failed to allocate setup buffer\n");
2444 ret = -ENOMEM;
2445 goto err2;
2446 }
2447
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002448 dwc->ep0_bounce = dma_alloc_coherent(dwc->dev,
Felipe Balbi3ef35fa2012-05-04 12:58:14 +03002449 DWC3_EP0_BOUNCE_SIZE, &dwc->ep0_bounce_addr,
2450 GFP_KERNEL);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002451 if (!dwc->ep0_bounce) {
2452 dev_err(dwc->dev, "failed to allocate ep0 bounce buffer\n");
2453 ret = -ENOMEM;
2454 goto err3;
2455 }
2456
Felipe Balbi72246da2011-08-19 18:10:58 +03002457 dev_set_name(&dwc->gadget.dev, "gadget");
2458
2459 dwc->gadget.ops = &dwc3_gadget_ops;
Michal Nazarewiczd327ab52011-11-19 18:27:37 +01002460 dwc->gadget.max_speed = USB_SPEED_SUPER;
Felipe Balbi72246da2011-08-19 18:10:58 +03002461 dwc->gadget.speed = USB_SPEED_UNKNOWN;
2462 dwc->gadget.dev.parent = dwc->dev;
Felipe Balbieeb720f2011-11-28 12:46:59 +02002463 dwc->gadget.sg_supported = true;
Felipe Balbi72246da2011-08-19 18:10:58 +03002464
2465 dma_set_coherent_mask(&dwc->gadget.dev, dwc->dev->coherent_dma_mask);
2466
2467 dwc->gadget.dev.dma_parms = dwc->dev->dma_parms;
2468 dwc->gadget.dev.dma_mask = dwc->dev->dma_mask;
2469 dwc->gadget.dev.release = dwc3_gadget_release;
2470 dwc->gadget.name = "dwc3-gadget";
2471
2472 /*
2473 * REVISIT: Here we should clear all pending IRQs to be
2474 * sure we're starting from a well known location.
2475 */
2476
2477 ret = dwc3_gadget_init_endpoints(dwc);
2478 if (ret)
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002479 goto err4;
Felipe Balbi72246da2011-08-19 18:10:58 +03002480
2481 irq = platform_get_irq(to_platform_device(dwc->dev), 0);
2482
2483 ret = request_irq(irq, dwc3_interrupt, IRQF_SHARED,
2484 "dwc3", dwc);
2485 if (ret) {
2486 dev_err(dwc->dev, "failed to request irq #%d --> %d\n",
2487 irq, ret);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002488 goto err5;
Felipe Balbi72246da2011-08-19 18:10:58 +03002489 }
2490
Sebastian Andrzej Siewiore6a3b5e2011-09-13 17:54:39 +02002491 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2492 reg |= DWC3_DCFG_LPM_CAP;
2493 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
2494
Felipe Balbi72246da2011-08-19 18:10:58 +03002495 /* Enable all but Start and End of Frame IRQs */
2496 reg = (DWC3_DEVTEN_VNDRDEVTSTRCVEDEN |
2497 DWC3_DEVTEN_EVNTOVERFLOWEN |
2498 DWC3_DEVTEN_CMDCMPLTEN |
2499 DWC3_DEVTEN_ERRTICERREN |
2500 DWC3_DEVTEN_WKUPEVTEN |
2501 DWC3_DEVTEN_ULSTCNGEN |
2502 DWC3_DEVTEN_CONNECTDONEEN |
2503 DWC3_DEVTEN_USBRSTEN |
2504 DWC3_DEVTEN_DISCONNEVTEN);
2505 dwc3_writel(dwc->regs, DWC3_DEVTEN, reg);
2506
Pratyush Anand2b758352013-01-14 15:59:31 +05302507 /* automatic phy suspend only on recent versions */
Paul Zimmerman802fde92012-04-27 13:10:52 +03002508 if (dwc->revision >= DWC3_REVISION_194A) {
Pratyush Ananddcae3572012-06-06 19:36:17 +05302509 dwc3_gadget_usb2_phy_suspend(dwc, false);
2510 dwc3_gadget_usb3_phy_suspend(dwc, false);
Paul Zimmerman802fde92012-04-27 13:10:52 +03002511 }
2512
Felipe Balbi72246da2011-08-19 18:10:58 +03002513 ret = device_register(&dwc->gadget.dev);
2514 if (ret) {
2515 dev_err(dwc->dev, "failed to register gadget device\n");
2516 put_device(&dwc->gadget.dev);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002517 goto err6;
Felipe Balbi72246da2011-08-19 18:10:58 +03002518 }
2519
2520 ret = usb_add_gadget_udc(dwc->dev, &dwc->gadget);
2521 if (ret) {
2522 dev_err(dwc->dev, "failed to register udc\n");
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002523 goto err7;
Felipe Balbi72246da2011-08-19 18:10:58 +03002524 }
2525
2526 return 0;
2527
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002528err7:
Felipe Balbi72246da2011-08-19 18:10:58 +03002529 device_unregister(&dwc->gadget.dev);
2530
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002531err6:
Felipe Balbi72246da2011-08-19 18:10:58 +03002532 dwc3_writel(dwc->regs, DWC3_DEVTEN, 0x00);
2533 free_irq(irq, dwc);
2534
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002535err5:
Felipe Balbi72246da2011-08-19 18:10:58 +03002536 dwc3_gadget_free_endpoints(dwc);
2537
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002538err4:
Felipe Balbi3ef35fa2012-05-04 12:58:14 +03002539 dma_free_coherent(dwc->dev, DWC3_EP0_BOUNCE_SIZE,
2540 dwc->ep0_bounce, dwc->ep0_bounce_addr);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002541
Felipe Balbi72246da2011-08-19 18:10:58 +03002542err3:
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02002543 kfree(dwc->setup_buf);
Felipe Balbi72246da2011-08-19 18:10:58 +03002544
2545err2:
2546 dma_free_coherent(dwc->dev, sizeof(*dwc->ep0_trb),
2547 dwc->ep0_trb, dwc->ep0_trb_addr);
2548
2549err1:
2550 dma_free_coherent(dwc->dev, sizeof(*dwc->ctrl_req),
2551 dwc->ctrl_req, dwc->ctrl_req_addr);
2552
2553err0:
2554 return ret;
2555}
2556
2557void dwc3_gadget_exit(struct dwc3 *dwc)
2558{
2559 int irq;
Felipe Balbi72246da2011-08-19 18:10:58 +03002560
2561 usb_del_gadget_udc(&dwc->gadget);
2562 irq = platform_get_irq(to_platform_device(dwc->dev), 0);
2563
2564 dwc3_writel(dwc->regs, DWC3_DEVTEN, 0x00);
2565 free_irq(irq, dwc);
2566
Felipe Balbi72246da2011-08-19 18:10:58 +03002567 dwc3_gadget_free_endpoints(dwc);
2568
Felipe Balbi3ef35fa2012-05-04 12:58:14 +03002569 dma_free_coherent(dwc->dev, DWC3_EP0_BOUNCE_SIZE,
2570 dwc->ep0_bounce, dwc->ep0_bounce_addr);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002571
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02002572 kfree(dwc->setup_buf);
Felipe Balbi72246da2011-08-19 18:10:58 +03002573
2574 dma_free_coherent(dwc->dev, sizeof(*dwc->ep0_trb),
2575 dwc->ep0_trb, dwc->ep0_trb_addr);
2576
2577 dma_free_coherent(dwc->dev, sizeof(*dwc->ctrl_req),
2578 dwc->ctrl_req, dwc->ctrl_req_addr);
2579
2580 device_unregister(&dwc->gadget.dev);
2581}