blob: 63698de5cc5f45c1a5d18f92064c53a10c6d1ebc [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
Vijayavardhan Vennapusadbe59bf2013-06-07 15:09:36 +053039#include <linux/module.h>
Felipe Balbi72246da2011-08-19 18:10:58 +030040#include <linux/kernel.h>
41#include <linux/delay.h>
42#include <linux/slab.h>
43#include <linux/spinlock.h>
44#include <linux/platform_device.h>
45#include <linux/pm_runtime.h>
46#include <linux/interrupt.h>
47#include <linux/io.h>
48#include <linux/list.h>
49#include <linux/dma-mapping.h>
50
51#include <linux/usb/ch9.h>
52#include <linux/usb/gadget.h>
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +020053#include <linux/usb/otg.h>
Felipe Balbi72246da2011-08-19 18:10:58 +030054
55#include "core.h"
56#include "gadget.h"
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +053057#include "debug.h"
Felipe Balbi72246da2011-08-19 18:10:58 +030058#include "io.h"
59
Vijayavardhan Vennapusadbe59bf2013-06-07 15:09:36 +053060static bool tx_fifo_resize_enable;
61module_param(tx_fifo_resize_enable, bool, S_IRUGO|S_IWUSR);
62MODULE_PARM_DESC(tx_fifo_resize_enable,
63 "Enable allocating Tx fifo for endpoints");
64
Felipe Balbi04a9bfc2012-01-02 18:25:43 +020065/**
66 * dwc3_gadget_set_test_mode - Enables USB2 Test Modes
67 * @dwc: pointer to our context structure
68 * @mode: the mode to set (J, K SE0 NAK, Force Enable)
69 *
70 * Caller should take care of locking. This function will
71 * return 0 on success or -EINVAL if wrong Test Selector
72 * is passed
73 */
74int dwc3_gadget_set_test_mode(struct dwc3 *dwc, int mode)
75{
76 u32 reg;
77
78 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
79 reg &= ~DWC3_DCTL_TSTCTRL_MASK;
80
81 switch (mode) {
82 case TEST_J:
83 case TEST_K:
84 case TEST_SE0_NAK:
85 case TEST_PACKET:
86 case TEST_FORCE_EN:
87 reg |= mode << 1;
88 break;
89 default:
90 return -EINVAL;
91 }
92
93 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
94
95 return 0;
96}
97
Felipe Balbi8598bde2012-01-02 18:55:57 +020098/**
99 * dwc3_gadget_set_link_state - Sets USB Link to a particular State
100 * @dwc: pointer to our context structure
101 * @state: the state to put link into
102 *
103 * Caller should take care of locking. This function will
Paul Zimmermanaee63e32012-02-24 17:32:15 -0800104 * return 0 on success or -ETIMEDOUT.
Felipe Balbi8598bde2012-01-02 18:55:57 +0200105 */
106int dwc3_gadget_set_link_state(struct dwc3 *dwc, enum dwc3_link_state state)
107{
Paul Zimmermanaee63e32012-02-24 17:32:15 -0800108 int retries = 10000;
Felipe Balbi8598bde2012-01-02 18:55:57 +0200109 u32 reg;
110
Paul Zimmerman88df4272012-04-27 13:10:52 +0300111 /*
112 * Wait until device controller is ready. Only applies to 1.94a and
113 * later RTL.
114 */
115 if (dwc->revision >= DWC3_REVISION_194A) {
116 while (--retries) {
117 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
118 if (reg & DWC3_DSTS_DCNRD)
119 udelay(5);
120 else
121 break;
122 }
123
124 if (retries <= 0)
125 return -ETIMEDOUT;
126 }
127
Felipe Balbi8598bde2012-01-02 18:55:57 +0200128 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
129 reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK;
130
131 /* set requested state */
132 reg |= DWC3_DCTL_ULSTCHNGREQ(state);
133 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
134
Paul Zimmerman88df4272012-04-27 13:10:52 +0300135 /*
136 * The following code is racy when called from dwc3_gadget_wakeup,
137 * and is not needed, at least on newer versions
138 */
139 if (dwc->revision >= DWC3_REVISION_194A)
140 return 0;
141
Felipe Balbi8598bde2012-01-02 18:55:57 +0200142 /* wait for a change in DSTS */
Paul Zimmerman8b9388f2012-04-27 12:52:01 +0300143 retries = 10000;
Felipe Balbi8598bde2012-01-02 18:55:57 +0200144 while (--retries) {
145 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
146
Felipe Balbi8598bde2012-01-02 18:55:57 +0200147 if (DWC3_DSTS_USBLNKST(reg) == state)
148 return 0;
149
Paul Zimmermanaee63e32012-02-24 17:32:15 -0800150 udelay(5);
Felipe Balbi8598bde2012-01-02 18:55:57 +0200151 }
152
153 dev_vdbg(dwc->dev, "link state change request timed out\n");
154
155 return -ETIMEDOUT;
156}
157
Felipe Balbi457e84b2012-01-18 18:04:09 +0200158/**
159 * dwc3_gadget_resize_tx_fifos - reallocate fifo spaces for current use-case
160 * @dwc: pointer to our context structure
161 *
162 * This function will a best effort FIFO allocation in order
163 * to improve FIFO usage and throughput, while still allowing
164 * us to enable as many endpoints as possible.
165 *
166 * Keep in mind that this operation will be highly dependent
167 * on the configured size for RAM1 - which contains TxFifo -,
168 * the amount of endpoints enabled on coreConsultant tool, and
169 * the width of the Master Bus.
170 *
171 * In the ideal world, we would always be able to satisfy the
172 * following equation:
173 *
174 * ((512 + 2 * MDWIDTH-Bytes) + (Number of IN Endpoints - 1) * \
175 * (3 * (1024 + MDWIDTH-Bytes) + MDWIDTH-Bytes)) / MDWIDTH-Bytes
176 *
177 * Unfortunately, due to many variables that's not always the case.
178 */
179int dwc3_gadget_resize_tx_fifos(struct dwc3 *dwc)
180{
181 int last_fifo_depth = 0;
182 int ram1_depth;
183 int fifo_size;
184 int mdwidth;
185 int num;
186
Vijayavardhan Vennapusadbe59bf2013-06-07 15:09:36 +0530187 if (!dwc->needs_fifo_resize && !tx_fifo_resize_enable)
Felipe Balbi457e84b2012-01-18 18:04:09 +0200188 return 0;
189
190 ram1_depth = DWC3_RAM1_DEPTH(dwc->hwparams.hwparams7);
191 mdwidth = DWC3_MDWIDTH(dwc->hwparams.hwparams0);
192
193 /* MDWIDTH is represented in bits, we need it in bytes */
194 mdwidth >>= 3;
195
196 /*
197 * FIXME For now we will only allocate 1 wMaxPacketSize space
198 * for each enabled endpoint, later patches will come to
199 * improve this algorithm so that we better use the internal
Vijayavardhan Vennapusadaf082c2013-03-01 13:08:59 +0530200 * FIFO space. Also consider the case where TxFIFO RAM space
201 * may change dynamically based on the USB configuration.
Felipe Balbi457e84b2012-01-18 18:04:09 +0200202 */
203 for (num = 0; num < DWC3_ENDPOINTS_NUM; num++) {
204 struct dwc3_ep *dep = dwc->eps[num];
205 int fifo_number = dep->number >> 1;
Felipe Balbi2e81c362012-02-02 13:01:12 +0200206 int mult = 1;
Felipe Balbi457e84b2012-01-18 18:04:09 +0200207 int tmp;
208
209 if (!(dep->number & 1))
210 continue;
211
212 if (!(dep->flags & DWC3_EP_ENABLED))
213 continue;
214
Vijayavardhan Vennapusadaf082c2013-03-01 13:08:59 +0530215 if (((dep->endpoint.maxburst > 1) &&
216 usb_endpoint_xfer_bulk(dep->endpoint.desc))
Ido Shayevitz57cdac12012-03-12 20:25:24 +0200217 || usb_endpoint_xfer_isoc(dep->endpoint.desc))
Felipe Balbi2e81c362012-02-02 13:01:12 +0200218 mult = 3;
219
220 /*
221 * REVISIT: the following assumes we will always have enough
222 * space available on the FIFO RAM for all possible use cases.
223 * Make sure that's true somehow and change FIFO allocation
224 * accordingly.
225 *
Vijayavardhan Vennapusadaf082c2013-03-01 13:08:59 +0530226 * If we have Bulk (burst only) or Isochronous endpoints, we
227 * want them to be able to be very, very fast. So we're giving
Felipe Balbi2e81c362012-02-02 13:01:12 +0200228 * those endpoints a fifo_size which is enough for 3 full
229 * packets
230 */
231 tmp = mult * (dep->endpoint.maxpacket + mdwidth);
Felipe Balbi457e84b2012-01-18 18:04:09 +0200232 tmp += mdwidth;
233
234 fifo_size = DIV_ROUND_UP(tmp, mdwidth);
Felipe Balbi2e81c362012-02-02 13:01:12 +0200235
Felipe Balbi457e84b2012-01-18 18:04:09 +0200236 fifo_size |= (last_fifo_depth << 16);
237
238 dev_vdbg(dwc->dev, "%s: Fifo Addr %04x Size %d\n",
239 dep->name, last_fifo_depth, fifo_size & 0xffff);
240
241 dwc3_writel(dwc->regs, DWC3_GTXFIFOSIZ(fifo_number),
242 fifo_size);
243
244 last_fifo_depth += (fifo_size & 0xffff);
245 }
246
247 return 0;
248}
249
Felipe Balbi72246da2011-08-19 18:10:58 +0300250void dwc3_gadget_giveback(struct dwc3_ep *dep, struct dwc3_request *req,
251 int status)
252{
253 struct dwc3 *dwc = dep->dwc;
254
255 if (req->queued) {
Manu Gautam55d34222012-12-19 16:49:47 +0530256 req->queued = false;
257
Felipe Balbieeb720f2011-11-28 12:46:59 +0200258 if (req->request.num_mapped_sgs)
259 dep->busy_slot += req->request.num_mapped_sgs;
260 else
261 dep->busy_slot++;
262
Felipe Balbi72246da2011-08-19 18:10:58 +0300263 /*
264 * Skip LINK TRB. We can't use req->trb and check for
265 * DWC3_TRBCTL_LINK_TRB because it points the TRB we just
266 * completed (not the LINK TRB).
267 */
268 if (((dep->busy_slot & DWC3_TRB_MASK) == DWC3_TRB_NUM - 1) &&
Ido Shayevitz57cdac12012-03-12 20:25:24 +0200269 usb_endpoint_xfer_isoc(dep->endpoint.desc))
Felipe Balbi72246da2011-08-19 18:10:58 +0300270 dep->busy_slot++;
271 }
272 list_del(&req->list);
Felipe Balbieeb720f2011-11-28 12:46:59 +0200273 req->trb = NULL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300274
275 if (req->request.status == -EINPROGRESS)
276 req->request.status = status;
277
Pratyush Anand8d7bf592012-08-10 13:42:16 +0530278 if (dwc->ep0_bounced && dep->number == 0)
279 dwc->ep0_bounced = false;
280 else
281 usb_gadget_unmap_request(&dwc->gadget, &req->request,
282 req->direction);
Felipe Balbi72246da2011-08-19 18:10:58 +0300283
284 dev_dbg(dwc->dev, "request %p from %s completed %d/%d ===> %d\n",
285 req, dep->name, req->request.actual,
286 req->request.length, status);
287
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +0530288 dbg_done(dep->number, req->request.actual, req->request.status);
Felipe Balbi72246da2011-08-19 18:10:58 +0300289 spin_unlock(&dwc->lock);
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +0200290 req->request.complete(&dep->endpoint, &req->request);
Felipe Balbi72246da2011-08-19 18:10:58 +0300291 spin_lock(&dwc->lock);
292}
293
294static const char *dwc3_gadget_ep_cmd_string(u8 cmd)
295{
296 switch (cmd) {
297 case DWC3_DEPCMD_DEPSTARTCFG:
298 return "Start New Configuration";
299 case DWC3_DEPCMD_ENDTRANSFER:
300 return "End Transfer";
301 case DWC3_DEPCMD_UPDATETRANSFER:
302 return "Update Transfer";
303 case DWC3_DEPCMD_STARTTRANSFER:
304 return "Start Transfer";
305 case DWC3_DEPCMD_CLEARSTALL:
306 return "Clear Stall";
307 case DWC3_DEPCMD_SETSTALL:
308 return "Set Stall";
Paul Zimmerman88df4272012-04-27 13:10:52 +0300309 case DWC3_DEPCMD_GETEPSTATE:
310 return "Get Endpoint State";
Felipe Balbi72246da2011-08-19 18:10:58 +0300311 case DWC3_DEPCMD_SETTRANSFRESOURCE:
312 return "Set Endpoint Transfer Resource";
313 case DWC3_DEPCMD_SETEPCONFIG:
314 return "Set Endpoint Configuration";
315 default:
316 return "UNKNOWN command";
317 }
318}
319
Felipe Balbi573c2762012-04-24 16:19:11 +0300320int dwc3_send_gadget_generic_command(struct dwc3 *dwc, int cmd, u32 param)
321{
322 u32 timeout = 500;
323 u32 reg;
324
325 dwc3_writel(dwc->regs, DWC3_DGCMDPAR, param);
326 dwc3_writel(dwc->regs, DWC3_DGCMD, cmd | DWC3_DGCMD_CMDACT);
327
328 do {
329 reg = dwc3_readl(dwc->regs, DWC3_DGCMD);
330 if (!(reg & DWC3_DGCMD_CMDACT)) {
331 dev_vdbg(dwc->dev, "Command Complete --> %d\n",
332 DWC3_DGCMD_STATUS(reg));
333 return 0;
334 }
335
336 /*
337 * We can't sleep here, because it's also called from
338 * interrupt context.
339 */
340 timeout--;
341 if (!timeout)
342 return -ETIMEDOUT;
343 udelay(1);
344 } while (1);
345}
346
Felipe Balbi72246da2011-08-19 18:10:58 +0300347int dwc3_send_gadget_ep_cmd(struct dwc3 *dwc, unsigned ep,
348 unsigned cmd, struct dwc3_gadget_ep_cmd_params *params)
349{
350 struct dwc3_ep *dep = dwc->eps[ep];
Sebastian Andrzej Siewior61d58242011-08-29 16:46:38 +0200351 u32 timeout = 500;
Felipe Balbi72246da2011-08-19 18:10:58 +0300352 u32 reg;
353
354 dev_vdbg(dwc->dev, "%s: cmd '%s' params %08x %08x %08x\n",
355 dep->name,
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300356 dwc3_gadget_ep_cmd_string(cmd), params->param0,
357 params->param1, params->param2);
Felipe Balbi72246da2011-08-19 18:10:58 +0300358
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300359 dwc3_writel(dwc->regs, DWC3_DEPCMDPAR0(ep), params->param0);
360 dwc3_writel(dwc->regs, DWC3_DEPCMDPAR1(ep), params->param1);
361 dwc3_writel(dwc->regs, DWC3_DEPCMDPAR2(ep), params->param2);
Felipe Balbi72246da2011-08-19 18:10:58 +0300362
363 dwc3_writel(dwc->regs, DWC3_DEPCMD(ep), cmd | DWC3_DEPCMD_CMDACT);
364 do {
365 reg = dwc3_readl(dwc->regs, DWC3_DEPCMD(ep));
366 if (!(reg & DWC3_DEPCMD_CMDACT)) {
Felipe Balbi164f6e12011-08-27 20:29:58 +0300367 dev_vdbg(dwc->dev, "Command Complete --> %d\n",
368 DWC3_DEPCMD_STATUS(reg));
Vijayavardhan Vennapusa91ba6532013-01-30 17:35:45 +0530369 /* SW issues START TRANSFER command to isochronous ep
370 * with future frame interval. If future interval time
371 * has already passed when core recieves command, core
372 * will respond with an error(bit13 in Command complete
373 * event. Hence return error in this case.
374 */
375 if (reg & 0x2000)
376 return -EAGAIN;
377 else
378 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300379 }
380
381 /*
Felipe Balbi72246da2011-08-19 18:10:58 +0300382 * We can't sleep here, because it is also called from
383 * interrupt context.
384 */
385 timeout--;
386 if (!timeout)
387 return -ETIMEDOUT;
388
Sebastian Andrzej Siewior61d58242011-08-29 16:46:38 +0200389 udelay(1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300390 } while (1);
391}
392
Ido Shayevitzfa65a582012-06-06 14:39:54 +0300393dma_addr_t dwc3_trb_dma_offset(struct dwc3_ep *dep,
Felipe Balbif6bafc62012-02-06 11:04:53 +0200394 struct dwc3_trb *trb)
Felipe Balbi72246da2011-08-19 18:10:58 +0300395{
Paul Zimmermanc439ef82011-09-30 10:58:45 +0300396 u32 offset = (char *) trb - (char *) dep->trb_pool;
Felipe Balbi72246da2011-08-19 18:10:58 +0300397
398 return dep->trb_pool_dma + offset;
399}
400
401static int dwc3_alloc_trb_pool(struct dwc3_ep *dep)
402{
403 struct dwc3 *dwc = dep->dwc;
404
405 if (dep->trb_pool)
406 return 0;
407
408 if (dep->number == 0 || dep->number == 1)
409 return 0;
410
411 dep->trb_pool = dma_alloc_coherent(dwc->dev,
412 sizeof(struct dwc3_trb) * DWC3_TRB_NUM,
413 &dep->trb_pool_dma, GFP_KERNEL);
414 if (!dep->trb_pool) {
415 dev_err(dep->dwc->dev, "failed to allocate trb pool for %s\n",
416 dep->name);
417 return -ENOMEM;
418 }
419
420 return 0;
421}
422
423static void dwc3_free_trb_pool(struct dwc3_ep *dep)
424{
425 struct dwc3 *dwc = dep->dwc;
426
427 dma_free_coherent(dwc->dev, sizeof(struct dwc3_trb) * DWC3_TRB_NUM,
428 dep->trb_pool, dep->trb_pool_dma);
429
430 dep->trb_pool = NULL;
431 dep->trb_pool_dma = 0;
432}
433
434static int dwc3_gadget_start_config(struct dwc3 *dwc, struct dwc3_ep *dep)
435{
436 struct dwc3_gadget_ep_cmd_params params;
437 u32 cmd;
438
439 memset(&params, 0x00, sizeof(params));
440
441 if (dep->number != 1) {
442 cmd = DWC3_DEPCMD_DEPSTARTCFG;
443 /* XferRscIdx == 0 for ep0 and 2 for the remaining */
Paul Zimmermanb23c8432011-09-30 10:58:42 +0300444 if (dep->number > 1) {
445 if (dwc->start_config_issued)
446 return 0;
447 dwc->start_config_issued = true;
Felipe Balbi72246da2011-08-19 18:10:58 +0300448 cmd |= DWC3_DEPCMD_PARAM(2);
Paul Zimmermanb23c8432011-09-30 10:58:42 +0300449 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300450
451 return dwc3_send_gadget_ep_cmd(dwc, 0, cmd, &params);
452 }
453
454 return 0;
455}
456
457static int dwc3_gadget_set_ep_config(struct dwc3 *dwc, struct dwc3_ep *dep,
Felipe Balbic90bfae2011-11-29 13:11:21 +0200458 const struct usb_endpoint_descriptor *desc,
Felipe Balbi07e0ee82012-07-16 14:08:16 +0300459 const struct usb_ss_ep_comp_descriptor *comp_desc,
460 bool ignore)
Felipe Balbi72246da2011-08-19 18:10:58 +0300461{
462 struct dwc3_gadget_ep_cmd_params params;
463
464 memset(&params, 0x00, sizeof(params));
465
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300466 params.param0 = DWC3_DEPCFG_EP_TYPE(usb_endpoint_type(desc))
Chanho Parkf0ee6062012-08-31 16:54:07 +0900467 | DWC3_DEPCFG_MAX_PACKET_SIZE(usb_endpoint_maxp(desc));
468
469 /* Burst size is only needed in SuperSpeed mode */
470 if (dwc->gadget.speed == USB_SPEED_SUPER) {
471 u32 burst = dep->endpoint.maxburst - 1;
472
473 params.param0 |= DWC3_DEPCFG_BURST_SIZE(burst);
474 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300475
Felipe Balbi07e0ee82012-07-16 14:08:16 +0300476 if (ignore)
477 params.param0 |= DWC3_DEPCFG_IGN_SEQ_NUM;
Felipe Balbi72246da2011-08-19 18:10:58 +0300478
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300479 params.param1 = DWC3_DEPCFG_XFER_COMPLETE_EN
480 | DWC3_DEPCFG_XFER_NOT_READY_EN;
Felipe Balbi72246da2011-08-19 18:10:58 +0300481
Felipe Balbi18b7ede2012-01-02 13:35:41 +0200482 if (usb_ss_max_streams(comp_desc) && usb_endpoint_xfer_bulk(desc)) {
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300483 params.param1 |= DWC3_DEPCFG_STREAM_CAPABLE
484 | DWC3_DEPCFG_STREAM_EVENT_EN;
Felipe Balbi879631a2011-09-30 10:58:47 +0300485 dep->stream_capable = true;
486 }
487
Felipe Balbi72246da2011-08-19 18:10:58 +0300488 if (usb_endpoint_xfer_isoc(desc))
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300489 params.param1 |= DWC3_DEPCFG_XFER_IN_PROGRESS_EN;
Felipe Balbi72246da2011-08-19 18:10:58 +0300490
491 /*
492 * We are doing 1:1 mapping for endpoints, meaning
493 * Physical Endpoints 2 maps to Logical Endpoint 2 and
494 * so on. We consider the direction bit as part of the physical
495 * endpoint number. So USB endpoint 0x81 is 0x03.
496 */
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300497 params.param1 |= DWC3_DEPCFG_EP_NUMBER(dep->number);
Felipe Balbi72246da2011-08-19 18:10:58 +0300498
499 /*
500 * We must use the lower 16 TX FIFOs even though
501 * HW might have more
502 */
503 if (dep->direction)
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300504 params.param0 |= DWC3_DEPCFG_FIFO_NUMBER(dep->number >> 1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300505
506 if (desc->bInterval) {
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300507 params.param1 |= DWC3_DEPCFG_BINTERVAL_M1(desc->bInterval - 1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300508 dep->interval = 1 << (desc->bInterval - 1);
509 }
510
511 return dwc3_send_gadget_ep_cmd(dwc, dep->number,
512 DWC3_DEPCMD_SETEPCONFIG, &params);
513}
514
515static int dwc3_gadget_set_xfer_resource(struct dwc3 *dwc, struct dwc3_ep *dep)
516{
517 struct dwc3_gadget_ep_cmd_params params;
518
519 memset(&params, 0x00, sizeof(params));
520
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300521 params.param0 = DWC3_DEPXFERCFG_NUM_XFER_RES(1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300522
523 return dwc3_send_gadget_ep_cmd(dwc, dep->number,
524 DWC3_DEPCMD_SETTRANSFRESOURCE, &params);
525}
526
527/**
528 * __dwc3_gadget_ep_enable - Initializes a HW endpoint
529 * @dep: endpoint to be initialized
530 * @desc: USB Endpoint Descriptor
531 *
532 * Caller should take care of locking
533 */
534static int __dwc3_gadget_ep_enable(struct dwc3_ep *dep,
Felipe Balbic90bfae2011-11-29 13:11:21 +0200535 const struct usb_endpoint_descriptor *desc,
Felipe Balbi07e0ee82012-07-16 14:08:16 +0300536 const struct usb_ss_ep_comp_descriptor *comp_desc,
537 bool ignore)
Felipe Balbi72246da2011-08-19 18:10:58 +0300538{
539 struct dwc3 *dwc = dep->dwc;
540 u32 reg;
541 int ret = -ENOMEM;
542
543 if (!(dep->flags & DWC3_EP_ENABLED)) {
544 ret = dwc3_gadget_start_config(dwc, dep);
545 if (ret)
546 return ret;
547 }
548
Felipe Balbi07e0ee82012-07-16 14:08:16 +0300549 ret = dwc3_gadget_set_ep_config(dwc, dep, desc, comp_desc, ignore);
Felipe Balbi72246da2011-08-19 18:10:58 +0300550 if (ret)
551 return ret;
552
553 if (!(dep->flags & DWC3_EP_ENABLED)) {
Felipe Balbif6bafc62012-02-06 11:04:53 +0200554 struct dwc3_trb *trb_st_hw;
555 struct dwc3_trb *trb_link;
Felipe Balbi72246da2011-08-19 18:10:58 +0300556
557 ret = dwc3_gadget_set_xfer_resource(dwc, dep);
558 if (ret)
559 return ret;
560
Ido Shayevitz57cdac12012-03-12 20:25:24 +0200561 dep->endpoint.desc = desc;
Felipe Balbic90bfae2011-11-29 13:11:21 +0200562 dep->comp_desc = comp_desc;
Felipe Balbi72246da2011-08-19 18:10:58 +0300563 dep->type = usb_endpoint_type(desc);
564 dep->flags |= DWC3_EP_ENABLED;
565
566 reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
567 reg |= DWC3_DALEPENA_EP(dep->number);
568 dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
569
570 if (!usb_endpoint_xfer_isoc(desc))
571 return 0;
572
573 memset(&trb_link, 0, sizeof(trb_link));
574
Paul Zimmerman1d046792012-02-15 18:56:56 -0800575 /* Link TRB for ISOC. The HWO bit is never reset */
Felipe Balbi72246da2011-08-19 18:10:58 +0300576 trb_st_hw = &dep->trb_pool[0];
577
Felipe Balbif6bafc62012-02-06 11:04:53 +0200578 trb_link = &dep->trb_pool[DWC3_TRB_NUM - 1];
Felipe Balbi72246da2011-08-19 18:10:58 +0300579
Felipe Balbif6bafc62012-02-06 11:04:53 +0200580 trb_link->bpl = lower_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw));
581 trb_link->bph = upper_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw));
582 trb_link->ctrl |= DWC3_TRBCTL_LINK_TRB;
583 trb_link->ctrl |= DWC3_TRB_CTRL_HWO;
Felipe Balbi72246da2011-08-19 18:10:58 +0300584 }
585
586 return 0;
587}
588
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200589static void dwc3_stop_active_transfer(struct dwc3 *dwc, u32 epnum);
590static void dwc3_remove_requests(struct dwc3 *dwc, struct dwc3_ep *dep)
Felipe Balbi72246da2011-08-19 18:10:58 +0300591{
592 struct dwc3_request *req;
593
Felipe Balbib129eb72012-02-17 12:10:04 +0200594 if (!list_empty(&dep->req_queued)) {
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200595 dwc3_stop_active_transfer(dwc, dep->number);
596
Pratyush Anande67fdeb2012-07-06 15:19:10 +0530597 /* - giveback all requests to gadget driver */
Pratyush Anand110ff602012-06-15 11:54:36 +0530598 while (!list_empty(&dep->req_queued)) {
599 req = next_request(&dep->req_queued);
600
601 dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
602 }
Felipe Balbib129eb72012-02-17 12:10:04 +0200603 }
604
Felipe Balbi72246da2011-08-19 18:10:58 +0300605 while (!list_empty(&dep->request_list)) {
606 req = next_request(&dep->request_list);
607
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200608 dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
Felipe Balbi72246da2011-08-19 18:10:58 +0300609 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300610}
611
612/**
613 * __dwc3_gadget_ep_disable - Disables a HW endpoint
614 * @dep: the endpoint to disable
615 *
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200616 * This function also removes requests which are currently processed ny the
617 * hardware and those which are not yet scheduled.
618 * Caller should take care of locking.
Felipe Balbi72246da2011-08-19 18:10:58 +0300619 */
Felipe Balbi72246da2011-08-19 18:10:58 +0300620static int __dwc3_gadget_ep_disable(struct dwc3_ep *dep)
621{
622 struct dwc3 *dwc = dep->dwc;
623 u32 reg;
624
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200625 dwc3_remove_requests(dwc, dep);
Felipe Balbi72246da2011-08-19 18:10:58 +0300626
627 reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
628 reg &= ~DWC3_DALEPENA_EP(dep->number);
629 dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
630
Felipe Balbi879631a2011-09-30 10:58:47 +0300631 dep->stream_capable = false;
Ido Shayevitzf9c56cd2012-02-08 13:56:48 +0200632 dep->endpoint.desc = NULL;
Felipe Balbic90bfae2011-11-29 13:11:21 +0200633 dep->comp_desc = NULL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300634 dep->type = 0;
Felipe Balbi879631a2011-09-30 10:58:47 +0300635 dep->flags = 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300636
637 return 0;
638}
639
640/* -------------------------------------------------------------------------- */
641
642static int dwc3_gadget_ep0_enable(struct usb_ep *ep,
643 const struct usb_endpoint_descriptor *desc)
644{
645 return -EINVAL;
646}
647
648static int dwc3_gadget_ep0_disable(struct usb_ep *ep)
649{
650 return -EINVAL;
651}
652
653/* -------------------------------------------------------------------------- */
654
655static int dwc3_gadget_ep_enable(struct usb_ep *ep,
656 const struct usb_endpoint_descriptor *desc)
657{
658 struct dwc3_ep *dep;
659 struct dwc3 *dwc;
660 unsigned long flags;
661 int ret;
662
663 if (!ep || !desc || desc->bDescriptorType != USB_DT_ENDPOINT) {
664 pr_debug("dwc3: invalid parameters\n");
665 return -EINVAL;
666 }
667
668 if (!desc->wMaxPacketSize) {
669 pr_debug("dwc3: missing wMaxPacketSize\n");
670 return -EINVAL;
671 }
672
673 dep = to_dwc3_ep(ep);
674 dwc = dep->dwc;
675
Felipe Balbi14395072012-08-15 12:28:29 +0300676 if (dep->flags & DWC3_EP_ENABLED) {
677 dev_WARN_ONCE(dwc->dev, true, "%s is already enabled\n",
678 dep->name);
679 return 0;
680 }
681
Felipe Balbi72246da2011-08-19 18:10:58 +0300682 switch (usb_endpoint_type(desc)) {
683 case USB_ENDPOINT_XFER_CONTROL:
Anton Tikhomirov27a78d62012-02-23 15:38:46 +0900684 strlcat(dep->name, "-control", sizeof(dep->name));
Felipe Balbi72246da2011-08-19 18:10:58 +0300685 break;
686 case USB_ENDPOINT_XFER_ISOC:
Anton Tikhomirov27a78d62012-02-23 15:38:46 +0900687 strlcat(dep->name, "-isoc", sizeof(dep->name));
Felipe Balbi72246da2011-08-19 18:10:58 +0300688 break;
689 case USB_ENDPOINT_XFER_BULK:
Anton Tikhomirov27a78d62012-02-23 15:38:46 +0900690 strlcat(dep->name, "-bulk", sizeof(dep->name));
Felipe Balbi72246da2011-08-19 18:10:58 +0300691 break;
692 case USB_ENDPOINT_XFER_INT:
Anton Tikhomirov27a78d62012-02-23 15:38:46 +0900693 strlcat(dep->name, "-int", sizeof(dep->name));
Felipe Balbi72246da2011-08-19 18:10:58 +0300694 break;
695 default:
696 dev_err(dwc->dev, "invalid endpoint transfer type\n");
697 }
698
Felipe Balbi72246da2011-08-19 18:10:58 +0300699 dev_vdbg(dwc->dev, "Enabling %s\n", dep->name);
700
701 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi07e0ee82012-07-16 14:08:16 +0300702 ret = __dwc3_gadget_ep_enable(dep, desc, ep->comp_desc, false);
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +0530703 dbg_event(dep->number, "ENABLE", ret);
Felipe Balbi72246da2011-08-19 18:10:58 +0300704 spin_unlock_irqrestore(&dwc->lock, flags);
705
706 return ret;
707}
708
709static int dwc3_gadget_ep_disable(struct usb_ep *ep)
710{
711 struct dwc3_ep *dep;
712 struct dwc3 *dwc;
713 unsigned long flags;
714 int ret;
715
716 if (!ep) {
717 pr_debug("dwc3: invalid parameters\n");
718 return -EINVAL;
719 }
720
721 dep = to_dwc3_ep(ep);
722 dwc = dep->dwc;
723
724 if (!(dep->flags & DWC3_EP_ENABLED)) {
725 dev_WARN_ONCE(dwc->dev, true, "%s is already disabled\n",
726 dep->name);
727 return 0;
728 }
729
730 snprintf(dep->name, sizeof(dep->name), "ep%d%s",
731 dep->number >> 1,
732 (dep->number & 1) ? "in" : "out");
733
734 spin_lock_irqsave(&dwc->lock, flags);
735 ret = __dwc3_gadget_ep_disable(dep);
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +0530736 dbg_event(dep->number, "DISABLE", ret);
Felipe Balbi72246da2011-08-19 18:10:58 +0300737 spin_unlock_irqrestore(&dwc->lock, flags);
738
739 return ret;
740}
741
742static struct usb_request *dwc3_gadget_ep_alloc_request(struct usb_ep *ep,
743 gfp_t gfp_flags)
744{
745 struct dwc3_request *req;
746 struct dwc3_ep *dep = to_dwc3_ep(ep);
747 struct dwc3 *dwc = dep->dwc;
748
749 req = kzalloc(sizeof(*req), gfp_flags);
750 if (!req) {
751 dev_err(dwc->dev, "not enough memory\n");
752 return NULL;
753 }
754
755 req->epnum = dep->number;
756 req->dep = dep;
Felipe Balbi72246da2011-08-19 18:10:58 +0300757
758 return &req->request;
759}
760
761static void dwc3_gadget_ep_free_request(struct usb_ep *ep,
762 struct usb_request *request)
763{
764 struct dwc3_request *req = to_dwc3_request(request);
765
766 kfree(req);
767}
768
Felipe Balbic71fc372011-11-22 11:37:34 +0200769/**
770 * dwc3_prepare_one_trb - setup one TRB from one request
771 * @dep: endpoint for which this request is prepared
772 * @req: dwc3_request pointer
773 */
Felipe Balbi68e823e2011-11-28 12:25:01 +0200774static void dwc3_prepare_one_trb(struct dwc3_ep *dep,
Felipe Balbieeb720f2011-11-28 12:46:59 +0200775 struct dwc3_request *req, dma_addr_t dma,
776 unsigned length, unsigned last, unsigned chain)
Felipe Balbic71fc372011-11-22 11:37:34 +0200777{
Felipe Balbieeb720f2011-11-28 12:46:59 +0200778 struct dwc3 *dwc = dep->dwc;
Felipe Balbif6bafc62012-02-06 11:04:53 +0200779 struct dwc3_trb *trb;
Felipe Balbic71fc372011-11-22 11:37:34 +0200780
781 unsigned int cur_slot;
782
Felipe Balbieeb720f2011-11-28 12:46:59 +0200783 dev_vdbg(dwc->dev, "%s: req %p dma %08llx length %d%s%s\n",
784 dep->name, req, (unsigned long long) dma,
785 length, last ? " last" : "",
786 chain ? " chain" : "");
787
Felipe Balbif6bafc62012-02-06 11:04:53 +0200788 trb = &dep->trb_pool[dep->free_slot & DWC3_TRB_MASK];
Felipe Balbic71fc372011-11-22 11:37:34 +0200789 cur_slot = dep->free_slot;
790 dep->free_slot++;
791
792 /* Skip the LINK-TRB on ISOC */
Vijayavardhan Vennapusa2a444ad2013-02-01 13:20:59 +0530793 if (((dep->free_slot & DWC3_TRB_MASK) == DWC3_TRB_NUM - 1) &&
Ido Shayevitz57cdac12012-03-12 20:25:24 +0200794 usb_endpoint_xfer_isoc(dep->endpoint.desc))
Vijayavardhan Vennapusa2a444ad2013-02-01 13:20:59 +0530795 dep->free_slot++;
Felipe Balbic71fc372011-11-22 11:37:34 +0200796
Felipe Balbieeb720f2011-11-28 12:46:59 +0200797 if (!req->trb) {
798 dwc3_gadget_move_request_queued(req);
Felipe Balbif6bafc62012-02-06 11:04:53 +0200799 req->trb = trb;
800 req->trb_dma = dwc3_trb_dma_offset(dep, trb);
Felipe Balbieeb720f2011-11-28 12:46:59 +0200801 }
Felipe Balbic71fc372011-11-22 11:37:34 +0200802
Felipe Balbif6bafc62012-02-06 11:04:53 +0200803 trb->size = DWC3_TRB_SIZE_LENGTH(length);
804 trb->bpl = lower_32_bits(dma);
805 trb->bph = upper_32_bits(dma);
Felipe Balbic71fc372011-11-22 11:37:34 +0200806
Ido Shayevitz57cdac12012-03-12 20:25:24 +0200807 switch (usb_endpoint_type(dep->endpoint.desc)) {
Felipe Balbic71fc372011-11-22 11:37:34 +0200808 case USB_ENDPOINT_XFER_CONTROL:
Felipe Balbif6bafc62012-02-06 11:04:53 +0200809 trb->ctrl = DWC3_TRBCTL_CONTROL_SETUP;
Felipe Balbic71fc372011-11-22 11:37:34 +0200810 break;
811
812 case USB_ENDPOINT_XFER_ISOC:
Felipe Balbif6bafc62012-02-06 11:04:53 +0200813 trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS_FIRST;
Felipe Balbic71fc372011-11-22 11:37:34 +0200814
Pratyush Ananddf023422012-05-21 12:42:54 +0530815 if (!req->request.no_interrupt)
Felipe Balbif6bafc62012-02-06 11:04:53 +0200816 trb->ctrl |= DWC3_TRB_CTRL_IOC;
Felipe Balbic71fc372011-11-22 11:37:34 +0200817 break;
818
819 case USB_ENDPOINT_XFER_BULK:
820 case USB_ENDPOINT_XFER_INT:
Felipe Balbif6bafc62012-02-06 11:04:53 +0200821 trb->ctrl = DWC3_TRBCTL_NORMAL;
Felipe Balbic71fc372011-11-22 11:37:34 +0200822 break;
823 default:
824 /*
825 * This is only possible with faulty memory because we
826 * checked it already :)
827 */
828 BUG();
829 }
830
Ido Shayevitz57cdac12012-03-12 20:25:24 +0200831 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbif6bafc62012-02-06 11:04:53 +0200832 trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI;
833 trb->ctrl |= DWC3_TRB_CTRL_CSP;
834 } else {
835 if (chain)
836 trb->ctrl |= DWC3_TRB_CTRL_CHN;
Felipe Balbic71fc372011-11-22 11:37:34 +0200837
Felipe Balbif6bafc62012-02-06 11:04:53 +0200838 if (last)
839 trb->ctrl |= DWC3_TRB_CTRL_LST;
840 }
841
Ido Shayevitz57cdac12012-03-12 20:25:24 +0200842 if (usb_endpoint_xfer_bulk(dep->endpoint.desc) && dep->stream_capable)
Felipe Balbif6bafc62012-02-06 11:04:53 +0200843 trb->ctrl |= DWC3_TRB_CTRL_SID_SOFN(req->request.stream_id);
844
845 trb->ctrl |= DWC3_TRB_CTRL_HWO;
Felipe Balbic71fc372011-11-22 11:37:34 +0200846}
847
Felipe Balbi72246da2011-08-19 18:10:58 +0300848/*
849 * dwc3_prepare_trbs - setup TRBs from requests
850 * @dep: endpoint for which requests are being prepared
851 * @starting: true if the endpoint is idle and no requests are queued.
852 *
Paul Zimmerman1d046792012-02-15 18:56:56 -0800853 * The function goes through the requests list and sets up TRBs for the
854 * transfers. The function returns once there are no more TRBs available or
855 * it runs out of requests.
Felipe Balbi72246da2011-08-19 18:10:58 +0300856 */
Felipe Balbi68e823e2011-11-28 12:25:01 +0200857static void dwc3_prepare_trbs(struct dwc3_ep *dep, bool starting)
Felipe Balbi72246da2011-08-19 18:10:58 +0300858{
Felipe Balbi68e823e2011-11-28 12:25:01 +0200859 struct dwc3_request *req, *n;
Felipe Balbi72246da2011-08-19 18:10:58 +0300860 u32 trbs_left;
Paul Zimmerman8d62cd62012-02-15 13:35:06 +0200861 u32 max;
Felipe Balbic71fc372011-11-22 11:37:34 +0200862 unsigned int last_one = 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300863
864 BUILD_BUG_ON_NOT_POWER_OF_2(DWC3_TRB_NUM);
865
866 /* the first request must not be queued */
867 trbs_left = (dep->busy_slot - dep->free_slot) & DWC3_TRB_MASK;
Felipe Balbic71fc372011-11-22 11:37:34 +0200868
Paul Zimmerman8d62cd62012-02-15 13:35:06 +0200869 /* Can't wrap around on a non-isoc EP since there's no link TRB */
Ido Shayevitz57cdac12012-03-12 20:25:24 +0200870 if (!usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Paul Zimmerman8d62cd62012-02-15 13:35:06 +0200871 max = DWC3_TRB_NUM - (dep->free_slot & DWC3_TRB_MASK);
872 if (trbs_left > max)
873 trbs_left = max;
874 }
875
Felipe Balbi72246da2011-08-19 18:10:58 +0300876 /*
Paul Zimmerman1d046792012-02-15 18:56:56 -0800877 * If busy & slot are equal than it is either full or empty. If we are
878 * starting to process requests then we are empty. Otherwise we are
Felipe Balbi72246da2011-08-19 18:10:58 +0300879 * full and don't do anything
880 */
881 if (!trbs_left) {
882 if (!starting)
Felipe Balbi68e823e2011-11-28 12:25:01 +0200883 return;
Felipe Balbi72246da2011-08-19 18:10:58 +0300884 trbs_left = DWC3_TRB_NUM;
885 /*
886 * In case we start from scratch, we queue the ISOC requests
887 * starting from slot 1. This is done because we use ring
888 * buffer and have no LST bit to stop us. Instead, we place
Paul Zimmerman1d046792012-02-15 18:56:56 -0800889 * IOC bit every TRB_NUM/4. We try to avoid having an interrupt
Felipe Balbi72246da2011-08-19 18:10:58 +0300890 * after the first request so we start at slot 1 and have
891 * 7 requests proceed before we hit the first IOC.
892 * Other transfer types don't use the ring buffer and are
893 * processed from the first TRB until the last one. Since we
894 * don't wrap around we have to start at the beginning.
895 */
Ido Shayevitz57cdac12012-03-12 20:25:24 +0200896 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbi72246da2011-08-19 18:10:58 +0300897 dep->busy_slot = 1;
898 dep->free_slot = 1;
899 } else {
900 dep->busy_slot = 0;
901 dep->free_slot = 0;
902 }
903 }
904
905 /* The last TRB is a link TRB, not used for xfer */
Ido Shayevitz57cdac12012-03-12 20:25:24 +0200906 if ((trbs_left <= 1) && usb_endpoint_xfer_isoc(dep->endpoint.desc))
Felipe Balbi68e823e2011-11-28 12:25:01 +0200907 return;
Felipe Balbi72246da2011-08-19 18:10:58 +0300908
909 list_for_each_entry_safe(req, n, &dep->request_list, list) {
Felipe Balbieeb720f2011-11-28 12:46:59 +0200910 unsigned length;
911 dma_addr_t dma;
Felipe Balbi72246da2011-08-19 18:10:58 +0300912
Felipe Balbieeb720f2011-11-28 12:46:59 +0200913 if (req->request.num_mapped_sgs > 0) {
914 struct usb_request *request = &req->request;
915 struct scatterlist *sg = request->sg;
916 struct scatterlist *s;
917 int i;
Felipe Balbi72246da2011-08-19 18:10:58 +0300918
Felipe Balbieeb720f2011-11-28 12:46:59 +0200919 for_each_sg(sg, s, request->num_mapped_sgs, i) {
920 unsigned chain = true;
Felipe Balbi72246da2011-08-19 18:10:58 +0300921
Felipe Balbieeb720f2011-11-28 12:46:59 +0200922 length = sg_dma_len(s);
923 dma = sg_dma_address(s);
Felipe Balbi72246da2011-08-19 18:10:58 +0300924
Paul Zimmerman1d046792012-02-15 18:56:56 -0800925 if (i == (request->num_mapped_sgs - 1) ||
926 sg_is_last(s)) {
Felipe Balbieeb720f2011-11-28 12:46:59 +0200927 last_one = true;
928 chain = false;
929 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300930
Felipe Balbieeb720f2011-11-28 12:46:59 +0200931 trbs_left--;
932 if (!trbs_left)
933 last_one = true;
Felipe Balbi72246da2011-08-19 18:10:58 +0300934
Felipe Balbieeb720f2011-11-28 12:46:59 +0200935 if (last_one)
936 chain = false;
Felipe Balbi72246da2011-08-19 18:10:58 +0300937
Felipe Balbieeb720f2011-11-28 12:46:59 +0200938 dwc3_prepare_one_trb(dep, req, dma, length,
939 last_one, chain);
Felipe Balbi72246da2011-08-19 18:10:58 +0300940
Felipe Balbieeb720f2011-11-28 12:46:59 +0200941 if (last_one)
942 break;
943 }
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +0530944 dbg_queue(dep->number, &req->request, 0);
Felipe Balbi72246da2011-08-19 18:10:58 +0300945 } else {
Felipe Balbieeb720f2011-11-28 12:46:59 +0200946 dma = req->request.dma;
947 length = req->request.length;
948 trbs_left--;
949
950 if (!trbs_left)
951 last_one = 1;
952
953 /* Is this the last request? */
954 if (list_is_last(&req->list, &dep->request_list))
955 last_one = 1;
956
957 dwc3_prepare_one_trb(dep, req, dma, length,
958 last_one, false);
959
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +0530960 dbg_queue(dep->number, &req->request, 0);
Felipe Balbieeb720f2011-11-28 12:46:59 +0200961 if (last_one)
962 break;
Felipe Balbi72246da2011-08-19 18:10:58 +0300963 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300964 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300965}
966
967static int __dwc3_gadget_kick_transfer(struct dwc3_ep *dep, u16 cmd_param,
968 int start_new)
969{
970 struct dwc3_gadget_ep_cmd_params params;
Vijayavardhan Vennapusa91ba6532013-01-30 17:35:45 +0530971 struct dwc3_request *req, *req1, *n;
Felipe Balbi72246da2011-08-19 18:10:58 +0300972 struct dwc3 *dwc = dep->dwc;
973 int ret;
974 u32 cmd;
975
976 if (start_new && (dep->flags & DWC3_EP_BUSY)) {
977 dev_vdbg(dwc->dev, "%s: endpoint busy\n", dep->name);
978 return -EBUSY;
979 }
980 dep->flags &= ~DWC3_EP_PENDING_REQUEST;
981
982 /*
983 * If we are getting here after a short-out-packet we don't enqueue any
984 * new requests as we try to set the IOC bit only on the last request.
985 */
986 if (start_new) {
987 if (list_empty(&dep->req_queued))
988 dwc3_prepare_trbs(dep, start_new);
989
990 /* req points to the first request which will be sent */
991 req = next_request(&dep->req_queued);
992 } else {
Felipe Balbi68e823e2011-11-28 12:25:01 +0200993 dwc3_prepare_trbs(dep, start_new);
994
Felipe Balbi72246da2011-08-19 18:10:58 +0300995 /*
Paul Zimmerman1d046792012-02-15 18:56:56 -0800996 * req points to the first request where HWO changed from 0 to 1
Felipe Balbi72246da2011-08-19 18:10:58 +0300997 */
Felipe Balbi68e823e2011-11-28 12:25:01 +0200998 req = next_request(&dep->req_queued);
Felipe Balbi72246da2011-08-19 18:10:58 +0300999 }
1000 if (!req) {
1001 dep->flags |= DWC3_EP_PENDING_REQUEST;
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +05301002 dbg_event(dep->number, "NO REQ", 0);
Felipe Balbi72246da2011-08-19 18:10:58 +03001003 return 0;
1004 }
1005
1006 memset(&params, 0, sizeof(params));
Felipe Balbidc1c70a2011-09-30 10:58:51 +03001007 params.param0 = upper_32_bits(req->trb_dma);
1008 params.param1 = lower_32_bits(req->trb_dma);
Felipe Balbi72246da2011-08-19 18:10:58 +03001009
1010 if (start_new)
1011 cmd = DWC3_DEPCMD_STARTTRANSFER;
1012 else
1013 cmd = DWC3_DEPCMD_UPDATETRANSFER;
1014
1015 cmd |= DWC3_DEPCMD_PARAM(cmd_param);
1016 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number, cmd, &params);
1017 if (ret < 0) {
1018 dev_dbg(dwc->dev, "failed to send STARTTRANSFER command\n");
1019
Vijayavardhan Vennapusa91ba6532013-01-30 17:35:45 +05301020 if ((ret == -EAGAIN) && start_new &&
1021 usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
1022 /* If bit13 in Command complete event is set, software
1023 * must issue ENDTRANDFER command and wait for
1024 * Xfernotready event to queue the requests again.
1025 */
1026 if (!dep->resource_index) {
1027 dep->resource_index =
1028 dwc3_gadget_ep_get_transfer_index(dwc,
1029 dep->number);
1030 WARN_ON_ONCE(!dep->resource_index);
1031 }
1032 dwc3_stop_active_transfer(dwc, dep->number);
1033 list_for_each_entry_safe_reverse(req1, n,
1034 &dep->req_queued, list) {
1035 req1->trb = NULL;
1036 dwc3_gadget_move_request_list_front(req1);
1037 if (req->request.num_mapped_sgs)
1038 dep->busy_slot +=
1039 req->request.num_mapped_sgs;
1040 else
1041 dep->busy_slot++;
1042 if ((dep->busy_slot & DWC3_TRB_MASK) ==
1043 DWC3_TRB_NUM - 1)
1044 dep->busy_slot++;
1045 }
1046 return ret;
1047 } else {
1048 /*
1049 * FIXME we need to iterate over the list of requests
1050 * here and stop, unmap, free and del each of the linked
1051 * requests instead of what we do now.
1052 */
1053 usb_gadget_unmap_request(&dwc->gadget, &req->request,
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02001054 req->direction);
Vijayavardhan Vennapusa91ba6532013-01-30 17:35:45 +05301055 list_del(&req->list);
1056 return ret;
1057 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001058 }
1059
1060 dep->flags |= DWC3_EP_BUSY;
Felipe Balbi25b8ff62011-11-04 12:32:47 +02001061
Paul Zimmermanf39a37f2012-03-29 18:16:54 +00001062 if (start_new) {
Felipe Balbi4959cfc2012-06-06 12:04:13 +03001063 dep->resource_index = dwc3_gadget_ep_get_transfer_index(dwc,
Paul Zimmermanf39a37f2012-03-29 18:16:54 +00001064 dep->number);
Felipe Balbi4959cfc2012-06-06 12:04:13 +03001065 WARN_ON_ONCE(!dep->resource_index);
Paul Zimmermanf39a37f2012-03-29 18:16:54 +00001066 }
Felipe Balbi25b8ff62011-11-04 12:32:47 +02001067
Felipe Balbi72246da2011-08-19 18:10:58 +03001068 return 0;
1069}
1070
Pratyush Anand73939b02012-05-25 18:54:56 +05301071static void __dwc3_gadget_start_isoc(struct dwc3 *dwc,
1072 struct dwc3_ep *dep, u32 cur_uf)
1073{
1074 u32 uf;
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +05301075 int ret;
Pratyush Anand73939b02012-05-25 18:54:56 +05301076
Vijayavardhan Vennapusa91ba6532013-01-30 17:35:45 +05301077 dep->current_uf = cur_uf;
1078
Pratyush Anand73939b02012-05-25 18:54:56 +05301079 if (list_empty(&dep->request_list)) {
1080 dev_vdbg(dwc->dev, "ISOC ep %s run out for requests.\n",
1081 dep->name);
Pratyush Anandac417602012-08-30 12:21:43 +05301082 dep->flags |= DWC3_EP_PENDING_REQUEST;
Pratyush Anand73939b02012-05-25 18:54:56 +05301083 return;
1084 }
1085
1086 /* 4 micro frames in the future */
1087 uf = cur_uf + dep->interval * 4;
1088
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +05301089 ret = __dwc3_gadget_kick_transfer(dep, uf, 1);
1090 if (ret < 0)
1091 dbg_event(dep->number, "QUEUE", ret);
Pratyush Anand73939b02012-05-25 18:54:56 +05301092}
1093
1094static void dwc3_gadget_start_isoc(struct dwc3 *dwc,
1095 struct dwc3_ep *dep, const struct dwc3_event_depevt *event)
1096{
1097 u32 cur_uf, mask;
1098
1099 mask = ~(dep->interval - 1);
1100 cur_uf = event->parameters & mask;
1101
1102 __dwc3_gadget_start_isoc(dwc, dep, cur_uf);
1103}
1104
Felipe Balbi72246da2011-08-19 18:10:58 +03001105static int __dwc3_gadget_ep_queue(struct dwc3_ep *dep, struct dwc3_request *req)
1106{
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02001107 struct dwc3 *dwc = dep->dwc;
1108 int ret;
1109
Manu Gautamd2b99e12013-02-11 15:53:34 +05301110 if (req->request.status == -EINPROGRESS) {
1111 ret = -EBUSY;
1112 dev_err(dwc->dev, "%s: %p request already in queue",
1113 dep->name, req);
1114 return ret;
1115 }
1116
Felipe Balbi72246da2011-08-19 18:10:58 +03001117 req->request.actual = 0;
1118 req->request.status = -EINPROGRESS;
1119 req->direction = dep->direction;
1120 req->epnum = dep->number;
1121
1122 /*
1123 * We only add to our list of requests now and
1124 * start consuming the list once we get XferNotReady
1125 * IRQ.
1126 *
1127 * That way, we avoid doing anything that we don't need
1128 * to do now and defer it until the point we receive a
1129 * particular token from the Host side.
1130 *
1131 * This will also avoid Host cancelling URBs due to too
Paul Zimmerman1d046792012-02-15 18:56:56 -08001132 * many NAKs.
Felipe Balbi72246da2011-08-19 18:10:58 +03001133 */
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02001134 ret = usb_gadget_map_request(&dwc->gadget, &req->request,
1135 dep->direction);
1136 if (ret)
1137 return ret;
1138
Felipe Balbi72246da2011-08-19 18:10:58 +03001139 list_add_tail(&req->list, &dep->request_list);
1140
1141 /*
Felipe Balbi46485a02012-06-06 12:00:50 +03001142 * There are a few special cases:
Felipe Balbi72246da2011-08-19 18:10:58 +03001143 *
Paul Zimmermanf39a37f2012-03-29 18:16:54 +00001144 * 1. XferNotReady with empty list of requests. We need to kick the
1145 * transfer here in that situation, otherwise we will be NAKing
1146 * forever. If we get XferNotReady before gadget driver has a
1147 * chance to queue a request, we will ACK the IRQ but won't be
1148 * able to receive the data until the next request is queued.
1149 * The following code is handling exactly that.
1150 *
Felipe Balbi72246da2011-08-19 18:10:58 +03001151 */
1152 if (dep->flags & DWC3_EP_PENDING_REQUEST) {
Pratyush Anandac417602012-08-30 12:21:43 +05301153 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001154
Pratyush Anandac417602012-08-30 12:21:43 +05301155 /*
1156 * If xfernotready is already elapsed and it is a case
1157 * of isoc transfer, then issue END TRANSFER, so that
1158 * you can receive xfernotready again and can have
1159 * notion of current microframe.
1160 */
1161 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Vijayavardhan Vennapusa91ba6532013-01-30 17:35:45 +05301162 /* If xfernotready event is recieved before issuing
1163 * START TRANSFER command, don't issue END TRANSFER.
1164 * Rather start queueing the requests by issuing START
1165 * TRANSFER command.
1166 */
1167 if (list_empty(&dep->req_queued) && dep->resource_index)
Pratyush Anand18bbcb02013-01-14 15:59:34 +05301168 dwc3_stop_active_transfer(dwc, dep->number);
Vijayavardhan Vennapusa91ba6532013-01-30 17:35:45 +05301169 else
1170 __dwc3_gadget_start_isoc(dwc, dep,
1171 dep->current_uf);
1172 dep->flags &= ~DWC3_EP_PENDING_REQUEST;
Pratyush Anandac417602012-08-30 12:21:43 +05301173 return 0;
1174 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001175
Felipe Balbi46485a02012-06-06 12:00:50 +03001176 ret = __dwc3_gadget_kick_transfer(dep, 0, true);
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +05301177 if (ret && ret != -EBUSY) {
1178 dbg_event(dep->number, "QUEUE", ret);
Felipe Balbi72246da2011-08-19 18:10:58 +03001179 dev_dbg(dwc->dev, "%s: failed to kick transfers\n",
1180 dep->name);
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +05301181 }
Felipe Balbi5d409eb2012-05-22 10:24:11 +03001182 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001183
Felipe Balbi46485a02012-06-06 12:00:50 +03001184 /*
1185 * 2. XferInProgress on Isoc EP with an active transfer. We need to
1186 * kick the transfer here after queuing a request, otherwise the
1187 * core may not see the modified TRB(s).
1188 */
1189 if (usb_endpoint_xfer_isoc(dep->endpoint.desc) &&
Pratyush Anand053d3e52012-08-07 16:54:18 +05301190 (dep->flags & DWC3_EP_BUSY) &&
1191 !(dep->flags & DWC3_EP_MISSED_ISOC)) {
Felipe Balbi4959cfc2012-06-06 12:04:13 +03001192 WARN_ON_ONCE(!dep->resource_index);
1193 ret = __dwc3_gadget_kick_transfer(dep, dep->resource_index,
Felipe Balbi46485a02012-06-06 12:00:50 +03001194 false);
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +05301195 if (ret && ret != -EBUSY) {
1196 dbg_event(dep->number, "QUEUE", ret);
Felipe Balbi46485a02012-06-06 12:00:50 +03001197 dev_dbg(dwc->dev, "%s: failed to kick transfers\n",
1198 dep->name);
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +05301199 }
Felipe Balbi46485a02012-06-06 12:00:50 +03001200 }
1201
Felipe Balbi72246da2011-08-19 18:10:58 +03001202 return 0;
1203}
1204
1205static int dwc3_gadget_ep_queue(struct usb_ep *ep, struct usb_request *request,
1206 gfp_t gfp_flags)
1207{
1208 struct dwc3_request *req = to_dwc3_request(request);
1209 struct dwc3_ep *dep = to_dwc3_ep(ep);
1210 struct dwc3 *dwc = dep->dwc;
1211
1212 unsigned long flags;
1213
1214 int ret;
1215
Manu Gautam22f93042013-02-20 15:12:02 +05301216 spin_lock_irqsave(&dwc->lock, flags);
1217
Ido Shayevitz57cdac12012-03-12 20:25:24 +02001218 if (!dep->endpoint.desc) {
Manu Gautam22f93042013-02-20 15:12:02 +05301219 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001220 dev_dbg(dwc->dev, "trying to queue request %p to disabled %s\n",
1221 request, ep->name);
1222 return -ESHUTDOWN;
1223 }
1224
1225 dev_vdbg(dwc->dev, "queing request %p to %s length %d\n",
1226 request, ep->name, request->length);
1227
Manu Gautam1c4dbcb2012-10-05 13:16:00 +05301228 WARN(!dep->direction && (request->length % ep->desc->wMaxPacketSize),
1229 "trying to queue unaligned request (%d)\n", request->length);
1230
Felipe Balbi72246da2011-08-19 18:10:58 +03001231 ret = __dwc3_gadget_ep_queue(dep, req);
1232 spin_unlock_irqrestore(&dwc->lock, flags);
1233
1234 return ret;
1235}
1236
1237static int dwc3_gadget_ep_dequeue(struct usb_ep *ep,
1238 struct usb_request *request)
1239{
1240 struct dwc3_request *req = to_dwc3_request(request);
1241 struct dwc3_request *r = NULL;
1242
1243 struct dwc3_ep *dep = to_dwc3_ep(ep);
1244 struct dwc3 *dwc = dep->dwc;
1245
1246 unsigned long flags;
1247 int ret = 0;
1248
1249 spin_lock_irqsave(&dwc->lock, flags);
1250
1251 list_for_each_entry(r, &dep->request_list, list) {
1252 if (r == req)
1253 break;
1254 }
1255
1256 if (r != req) {
1257 list_for_each_entry(r, &dep->req_queued, list) {
1258 if (r == req)
1259 break;
1260 }
1261 if (r == req) {
1262 /* wait until it is processed */
1263 dwc3_stop_active_transfer(dwc, dep->number);
Pratyush Anandeaec3e92012-06-15 11:54:00 +05301264 goto out1;
Felipe Balbi72246da2011-08-19 18:10:58 +03001265 }
1266 dev_err(dwc->dev, "request %p was not queued to %s\n",
1267 request, ep->name);
1268 ret = -EINVAL;
1269 goto out0;
1270 }
1271
Pratyush Anandeaec3e92012-06-15 11:54:00 +05301272out1:
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +05301273 dbg_event(dep->number, "DEQUEUE", 0);
Felipe Balbi72246da2011-08-19 18:10:58 +03001274 /* giveback the request */
1275 dwc3_gadget_giveback(dep, req, -ECONNRESET);
1276
1277out0:
1278 spin_unlock_irqrestore(&dwc->lock, flags);
1279
1280 return ret;
1281}
1282
1283int __dwc3_gadget_ep_set_halt(struct dwc3_ep *dep, int value)
1284{
1285 struct dwc3_gadget_ep_cmd_params params;
1286 struct dwc3 *dwc = dep->dwc;
1287 int ret;
1288
1289 memset(&params, 0x00, sizeof(params));
1290
1291 if (value) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001292 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number,
1293 DWC3_DEPCMD_SETSTALL, &params);
1294 if (ret)
1295 dev_err(dwc->dev, "failed to %s STALL on %s\n",
1296 value ? "set" : "clear",
1297 dep->name);
1298 else
1299 dep->flags |= DWC3_EP_STALL;
1300 } else {
1301 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number,
1302 DWC3_DEPCMD_CLEARSTALL, &params);
1303 if (ret)
1304 dev_err(dwc->dev, "failed to %s STALL on %s\n",
1305 value ? "set" : "clear",
1306 dep->name);
1307 else
Vijayavardhan Vennapusa6008e262012-10-19 15:57:56 +05301308 dep->flags &= ~(DWC3_EP_STALL | DWC3_EP_WEDGE);
Felipe Balbi72246da2011-08-19 18:10:58 +03001309 }
Paul Zimmerman52754552011-09-30 10:58:44 +03001310
Felipe Balbi72246da2011-08-19 18:10:58 +03001311 return ret;
1312}
1313
1314static int dwc3_gadget_ep_set_halt(struct usb_ep *ep, int value)
1315{
1316 struct dwc3_ep *dep = to_dwc3_ep(ep);
1317 struct dwc3 *dwc = dep->dwc;
1318
1319 unsigned long flags;
1320
1321 int ret;
1322
1323 spin_lock_irqsave(&dwc->lock, flags);
1324
Ido Shayevitz57cdac12012-03-12 20:25:24 +02001325 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001326 dev_err(dwc->dev, "%s is of Isochronous type\n", dep->name);
1327 ret = -EINVAL;
1328 goto out;
1329 }
1330
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +05301331 dbg_event(dep->number, "HALT", value);
Felipe Balbi72246da2011-08-19 18:10:58 +03001332 ret = __dwc3_gadget_ep_set_halt(dep, value);
1333out:
1334 spin_unlock_irqrestore(&dwc->lock, flags);
1335
1336 return ret;
1337}
1338
1339static int dwc3_gadget_ep_set_wedge(struct usb_ep *ep)
1340{
1341 struct dwc3_ep *dep = to_dwc3_ep(ep);
Paul Zimmerman249a4562012-02-24 17:32:16 -08001342 struct dwc3 *dwc = dep->dwc;
1343 unsigned long flags;
Felipe Balbi72246da2011-08-19 18:10:58 +03001344
Paul Zimmerman249a4562012-02-24 17:32:16 -08001345 spin_lock_irqsave(&dwc->lock, flags);
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +05301346 dbg_event(dep->number, "WEDGE", 0);
Felipe Balbi72246da2011-08-19 18:10:58 +03001347 dep->flags |= DWC3_EP_WEDGE;
Paul Zimmerman249a4562012-02-24 17:32:16 -08001348 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001349
Pratyush Anandeb840752012-06-25 22:40:43 +05301350 if (dep->number == 0 || dep->number == 1)
1351 return dwc3_gadget_ep0_set_halt(ep, 1);
1352 else
1353 return dwc3_gadget_ep_set_halt(ep, 1);
Felipe Balbi72246da2011-08-19 18:10:58 +03001354}
1355
1356/* -------------------------------------------------------------------------- */
1357
1358static struct usb_endpoint_descriptor dwc3_gadget_ep0_desc = {
1359 .bLength = USB_DT_ENDPOINT_SIZE,
1360 .bDescriptorType = USB_DT_ENDPOINT,
1361 .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
1362};
1363
1364static const struct usb_ep_ops dwc3_gadget_ep0_ops = {
1365 .enable = dwc3_gadget_ep0_enable,
1366 .disable = dwc3_gadget_ep0_disable,
1367 .alloc_request = dwc3_gadget_ep_alloc_request,
1368 .free_request = dwc3_gadget_ep_free_request,
1369 .queue = dwc3_gadget_ep0_queue,
1370 .dequeue = dwc3_gadget_ep_dequeue,
Pratyush Anandeb840752012-06-25 22:40:43 +05301371 .set_halt = dwc3_gadget_ep0_set_halt,
Felipe Balbi72246da2011-08-19 18:10:58 +03001372 .set_wedge = dwc3_gadget_ep_set_wedge,
1373};
1374
1375static const struct usb_ep_ops dwc3_gadget_ep_ops = {
1376 .enable = dwc3_gadget_ep_enable,
1377 .disable = dwc3_gadget_ep_disable,
1378 .alloc_request = dwc3_gadget_ep_alloc_request,
1379 .free_request = dwc3_gadget_ep_free_request,
1380 .queue = dwc3_gadget_ep_queue,
1381 .dequeue = dwc3_gadget_ep_dequeue,
1382 .set_halt = dwc3_gadget_ep_set_halt,
1383 .set_wedge = dwc3_gadget_ep_set_wedge,
1384};
1385
1386/* -------------------------------------------------------------------------- */
1387
1388static int dwc3_gadget_get_frame(struct usb_gadget *g)
1389{
1390 struct dwc3 *dwc = gadget_to_dwc(g);
1391 u32 reg;
1392
1393 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1394 return DWC3_DSTS_SOFFN(reg);
1395}
1396
1397static int dwc3_gadget_wakeup(struct usb_gadget *g)
1398{
1399 struct dwc3 *dwc = gadget_to_dwc(g);
1400
1401 unsigned long timeout;
1402 unsigned long flags;
1403
1404 u32 reg;
1405
1406 int ret = 0;
1407
1408 u8 link_state;
1409 u8 speed;
1410
1411 spin_lock_irqsave(&dwc->lock, flags);
1412
1413 /*
1414 * According to the Databook Remote wakeup request should
1415 * be issued only when the device is in early suspend state.
1416 *
1417 * We can check that via USB Link State bits in DSTS register.
1418 */
1419 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1420
1421 speed = reg & DWC3_DSTS_CONNECTSPD;
1422 if (speed == DWC3_DSTS_SUPERSPEED) {
1423 dev_dbg(dwc->dev, "no wakeup on SuperSpeed\n");
1424 ret = -EINVAL;
1425 goto out;
1426 }
1427
1428 link_state = DWC3_DSTS_USBLNKST(reg);
1429
1430 switch (link_state) {
1431 case DWC3_LINK_STATE_RX_DET: /* in HS, means Early Suspend */
1432 case DWC3_LINK_STATE_U3: /* in HS, means SUSPEND */
1433 break;
1434 default:
1435 dev_dbg(dwc->dev, "can't wakeup from link state %d\n",
1436 link_state);
1437 ret = -EINVAL;
1438 goto out;
1439 }
1440
Felipe Balbi8598bde2012-01-02 18:55:57 +02001441 ret = dwc3_gadget_set_link_state(dwc, DWC3_LINK_STATE_RECOV);
1442 if (ret < 0) {
1443 dev_err(dwc->dev, "failed to put link in Recovery\n");
1444 goto out;
1445 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001446
Paul Zimmerman88df4272012-04-27 13:10:52 +03001447 /* Recent versions do this automatically */
1448 if (dwc->revision < DWC3_REVISION_194A) {
1449 /* write zeroes to Link Change Request */
Felipe Balbib4d04352012-05-24 10:27:56 +03001450 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
Paul Zimmerman88df4272012-04-27 13:10:52 +03001451 reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK;
1452 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1453 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001454
Paul Zimmerman1d046792012-02-15 18:56:56 -08001455 /* poll until Link State changes to ON */
Felipe Balbi72246da2011-08-19 18:10:58 +03001456 timeout = jiffies + msecs_to_jiffies(100);
1457
Paul Zimmerman1d046792012-02-15 18:56:56 -08001458 while (!time_after(jiffies, timeout)) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001459 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1460
1461 /* in HS, means ON */
1462 if (DWC3_DSTS_USBLNKST(reg) == DWC3_LINK_STATE_U0)
1463 break;
1464 }
1465
1466 if (DWC3_DSTS_USBLNKST(reg) != DWC3_LINK_STATE_U0) {
1467 dev_err(dwc->dev, "failed to send remote wakeup\n");
1468 ret = -EINVAL;
1469 }
1470
1471out:
1472 spin_unlock_irqrestore(&dwc->lock, flags);
1473
1474 return ret;
1475}
1476
1477static int dwc3_gadget_set_selfpowered(struct usb_gadget *g,
1478 int is_selfpowered)
1479{
1480 struct dwc3 *dwc = gadget_to_dwc(g);
Paul Zimmerman249a4562012-02-24 17:32:16 -08001481 unsigned long flags;
Felipe Balbi72246da2011-08-19 18:10:58 +03001482
Paul Zimmerman249a4562012-02-24 17:32:16 -08001483 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001484 dwc->is_selfpowered = !!is_selfpowered;
Paul Zimmerman249a4562012-02-24 17:32:16 -08001485 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001486
1487 return 0;
1488}
1489
Pratyush Anand77473f72012-07-02 10:21:55 +05301490static int dwc3_gadget_run_stop(struct dwc3 *dwc, int is_on)
Felipe Balbi72246da2011-08-19 18:10:58 +03001491{
1492 u32 reg;
Sebastian Andrzej Siewior61d58242011-08-29 16:46:38 +02001493 u32 timeout = 500;
Felipe Balbi72246da2011-08-19 18:10:58 +03001494
1495 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
Felipe Balbi8db7ed12012-01-18 18:32:29 +02001496 if (is_on) {
Paul Zimmerman88df4272012-04-27 13:10:52 +03001497 if (dwc->revision <= DWC3_REVISION_187A) {
1498 reg &= ~DWC3_DCTL_TRGTULST_MASK;
1499 reg |= DWC3_DCTL_TRGTULST_RX_DET;
1500 }
1501
1502 if (dwc->revision >= DWC3_REVISION_194A)
1503 reg &= ~DWC3_DCTL_KEEP_CONNECT;
1504 reg |= DWC3_DCTL_RUN_STOP;
Felipe Balbi8db7ed12012-01-18 18:32:29 +02001505 } else {
Felipe Balbi72246da2011-08-19 18:10:58 +03001506 reg &= ~DWC3_DCTL_RUN_STOP;
Felipe Balbi8db7ed12012-01-18 18:32:29 +02001507 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001508
1509 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1510
1511 do {
1512 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1513 if (is_on) {
1514 if (!(reg & DWC3_DSTS_DEVCTRLHLT))
1515 break;
1516 } else {
1517 if (reg & DWC3_DSTS_DEVCTRLHLT)
1518 break;
1519 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001520 timeout--;
1521 if (!timeout)
Pratyush Anand77473f72012-07-02 10:21:55 +05301522 return -ETIMEDOUT;
Sebastian Andrzej Siewior61d58242011-08-29 16:46:38 +02001523 udelay(1);
Felipe Balbi72246da2011-08-19 18:10:58 +03001524 } while (1);
1525
1526 dev_vdbg(dwc->dev, "gadget %s data soft-%s\n",
1527 dwc->gadget_driver
1528 ? dwc->gadget_driver->function : "no-function",
1529 is_on ? "connect" : "disconnect");
Pratyush Anand77473f72012-07-02 10:21:55 +05301530
1531 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +03001532}
1533
Vijayavardhan Vennapusa908f1ed2012-10-19 19:51:48 +05301534static int dwc3_gadget_vbus_draw(struct usb_gadget *g, unsigned mA)
1535{
1536 struct dwc3 *dwc = gadget_to_dwc(g);
1537 struct dwc3_otg *dotg = dwc->dotg;
1538
1539 if (dotg && dotg->otg.phy)
1540 return usb_phy_set_power(dotg->otg.phy, mA);
1541
1542 return -ENOTSUPP;
1543}
1544
Felipe Balbi72246da2011-08-19 18:10:58 +03001545static int dwc3_gadget_pullup(struct usb_gadget *g, int is_on)
1546{
1547 struct dwc3 *dwc = gadget_to_dwc(g);
1548 unsigned long flags;
Pratyush Anand77473f72012-07-02 10:21:55 +05301549 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001550
1551 is_on = !!is_on;
1552
1553 spin_lock_irqsave(&dwc->lock, flags);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +02001554
1555 dwc->softconnect = is_on;
1556
1557 if ((dwc->dotg && !dwc->vbus_active) ||
1558 !dwc->gadget_driver) {
1559
1560 spin_unlock_irqrestore(&dwc->lock, flags);
1561
1562 /*
1563 * Need to wait for vbus_session(on) from otg driver or to
1564 * the udc_start.
1565 */
1566 return 0;
1567 }
1568
Pratyush Anand77473f72012-07-02 10:21:55 +05301569 ret = dwc3_gadget_run_stop(dwc, is_on);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +02001570
1571 spin_unlock_irqrestore(&dwc->lock, flags);
1572
Pratyush Anand77473f72012-07-02 10:21:55 +05301573 return ret;
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +02001574}
1575
Jack Pham09e5c8e2013-03-06 18:53:43 -08001576static void dwc3_gadget_disconnect_interrupt(struct dwc3 *dwc);
1577
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +02001578static int dwc3_gadget_vbus_session(struct usb_gadget *_gadget, int is_active)
1579{
1580 struct dwc3 *dwc = gadget_to_dwc(_gadget);
1581 unsigned long flags;
Vijayavardhan Vennapusa8ec31d22012-10-23 08:44:48 +05301582 int ret = 0;
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +02001583
1584 if (!dwc->dotg)
1585 return -EPERM;
1586
1587 is_active = !!is_active;
1588
1589 spin_lock_irqsave(&dwc->lock, flags);
1590
1591 /* Mark that the vbus was powered */
1592 dwc->vbus_active = is_active;
1593
1594 /*
1595 * Check if upper level usb_gadget_driver was already registerd with
1596 * this udc controller driver (if dwc3_gadget_start was called)
1597 */
1598 if (dwc->gadget_driver && dwc->softconnect) {
1599 if (dwc->vbus_active) {
1600 /*
1601 * Both vbus was activated by otg and pullup was
1602 * signaled by the gadget driver.
1603 */
Pratyush Anand77473f72012-07-02 10:21:55 +05301604 ret = dwc3_gadget_run_stop(dwc, 1);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +02001605 } else {
Pratyush Anand77473f72012-07-02 10:21:55 +05301606 ret = dwc3_gadget_run_stop(dwc, 0);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +02001607 }
Jack Pham09e5c8e2013-03-06 18:53:43 -08001608 }
1609
1610 /*
1611 * Clearing run/stop bit might occur before disconnect event is seen.
1612 * Make sure to let gadget driver know in that case.
1613 */
1614 if (!dwc->vbus_active && dwc->start_config_issued) {
1615 dev_dbg(dwc->dev, "calling disconnect from %s\n", __func__);
1616 dwc3_gadget_disconnect_interrupt(dwc);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +02001617 }
1618
Felipe Balbi72246da2011-08-19 18:10:58 +03001619 spin_unlock_irqrestore(&dwc->lock, flags);
Pratyush Anand77473f72012-07-02 10:21:55 +05301620 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001621}
1622
Manu Gautamf1fceddf2012-10-12 14:02:50 +05301623/* Required gadget re-initialization before switching to gadget in OTG mode */
1624void dwc3_gadget_restart(struct dwc3 *dwc)
1625{
1626 struct dwc3_ep *dep;
1627 int ret = 0;
Vijayavardhan Vennapusab7434562012-12-12 16:48:49 +05301628 u32 reg;
Manu Gautamf1fceddf2012-10-12 14:02:50 +05301629
Vijayavardhan Vennapusab7434562012-12-12 16:48:49 +05301630 /* Enable all but Start and End of Frame IRQs */
1631 reg = (DWC3_DEVTEN_EVNTOVERFLOWEN |
1632 DWC3_DEVTEN_CMDCMPLTEN |
1633 DWC3_DEVTEN_ERRTICERREN |
1634 DWC3_DEVTEN_WKUPEVTEN |
1635 DWC3_DEVTEN_ULSTCNGEN |
1636 DWC3_DEVTEN_CONNECTDONEEN |
1637 DWC3_DEVTEN_USBRSTEN |
1638 DWC3_DEVTEN_DISCONNEVTEN);
1639 dwc3_writel(dwc->regs, DWC3_DEVTEN, reg);
1640
1641 /* Enable USB2 LPM and automatic phy suspend only on recent versions */
1642 if (dwc->revision >= DWC3_REVISION_194A) {
1643 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
1644 reg |= DWC3_DCFG_LPM_CAP;
1645 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
1646
1647 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
1648 reg &= ~(DWC3_DCTL_HIRD_THRES_MASK | DWC3_DCTL_L1_HIBER_EN);
1649
1650 /* TODO: This should be configurable */
1651 reg |= DWC3_DCTL_HIRD_THRES(28);
1652
1653 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1654 }
1655
1656 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
1657 reg &= ~(DWC3_DCFG_SPEED_MASK);
1658
1659 /**
1660 * WORKAROUND: DWC3 revision < 2.20a have an issue
1661 * which would cause metastability state on Run/Stop
1662 * bit if we try to force the IP to USB2-only mode.
1663 *
1664 * Because of that, we cannot configure the IP to any
1665 * speed other than the SuperSpeed
1666 *
1667 * Refers to:
1668 *
1669 * STAR#9000525659: Clock Domain Crossing on DCTL in
1670 * USB 2.0 Mode
1671 */
1672 if (dwc->revision < DWC3_REVISION_220A)
1673 reg |= DWC3_DCFG_SUPERSPEED;
1674 else
1675 reg |= dwc->maximum_speed;
1676 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
1677
1678 dwc->start_config_issued = false;
1679
1680 /* Start with SuperSpeed Default */
1681 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
Manu Gautamf1fceddf2012-10-12 14:02:50 +05301682
1683 dwc->delayed_status = false;
Vijayavardhan Vennapusab7434562012-12-12 16:48:49 +05301684 /* reinitialize physical ep0-1 */
Manu Gautamf1fceddf2012-10-12 14:02:50 +05301685 dep = dwc->eps[0];
1686 dep->flags = 0;
1687 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, false);
1688 if (ret) {
1689 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
1690 return;
1691 }
1692
1693 dep = dwc->eps[1];
1694 dep->flags = 0;
1695 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, false);
1696 if (ret) {
1697 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
1698 return;
1699 }
1700
1701 /* begin to receive SETUP packets */
1702 dwc->ep0state = EP0_SETUP_PHASE;
1703 dwc3_ep0_out_start(dwc);
1704}
1705
Felipe Balbi72246da2011-08-19 18:10:58 +03001706static int dwc3_gadget_start(struct usb_gadget *g,
1707 struct usb_gadget_driver *driver)
1708{
1709 struct dwc3 *dwc = gadget_to_dwc(g);
1710 struct dwc3_ep *dep;
1711 unsigned long flags;
1712 int ret = 0;
1713 u32 reg;
1714
Vijayavardhan Vennapusa0557c9b2013-05-17 13:24:49 +05301715 pm_runtime_get_sync(dwc->dev);
Felipe Balbi72246da2011-08-19 18:10:58 +03001716 spin_lock_irqsave(&dwc->lock, flags);
1717
1718 if (dwc->gadget_driver) {
1719 dev_err(dwc->dev, "%s is already bound to %s\n",
1720 dwc->gadget.name,
1721 dwc->gadget_driver->driver.name);
1722 ret = -EBUSY;
1723 goto err0;
1724 }
1725
1726 dwc->gadget_driver = driver;
1727 dwc->gadget.dev.driver = &driver->driver;
1728
Felipe Balbi72246da2011-08-19 18:10:58 +03001729 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
1730 reg &= ~(DWC3_DCFG_SPEED_MASK);
Felipe Balbi38d2c6c2012-03-23 12:20:31 +02001731
1732 /**
1733 * WORKAROUND: DWC3 revision < 2.20a have an issue
1734 * which would cause metastability state on Run/Stop
1735 * bit if we try to force the IP to USB2-only mode.
1736 *
1737 * Because of that, we cannot configure the IP to any
1738 * speed other than the SuperSpeed
1739 *
1740 * Refers to:
1741 *
1742 * STAR#9000525659: Clock Domain Crossing on DCTL in
1743 * USB 2.0 Mode
1744 */
1745 if (dwc->revision < DWC3_REVISION_220A)
1746 reg |= DWC3_DCFG_SUPERSPEED;
1747 else
1748 reg |= dwc->maximum_speed;
Felipe Balbi72246da2011-08-19 18:10:58 +03001749 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
1750
Paul Zimmermanb23c8432011-09-30 10:58:42 +03001751 dwc->start_config_issued = false;
1752
Felipe Balbi72246da2011-08-19 18:10:58 +03001753 /* Start with SuperSpeed Default */
1754 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
1755
1756 dep = dwc->eps[0];
Felipe Balbi07e0ee82012-07-16 14:08:16 +03001757 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, false);
Felipe Balbi72246da2011-08-19 18:10:58 +03001758 if (ret) {
1759 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
1760 goto err0;
1761 }
1762
1763 dep = dwc->eps[1];
Felipe Balbi07e0ee82012-07-16 14:08:16 +03001764 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, false);
Felipe Balbi72246da2011-08-19 18:10:58 +03001765 if (ret) {
1766 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
1767 goto err1;
1768 }
1769
1770 /* begin to receive SETUP packets */
Felipe Balbic7fcdeb2011-08-27 22:28:36 +03001771 dwc->ep0state = EP0_SETUP_PHASE;
Felipe Balbi72246da2011-08-19 18:10:58 +03001772 dwc3_ep0_out_start(dwc);
1773
1774 spin_unlock_irqrestore(&dwc->lock, flags);
Vijayavardhan Vennapusa0557c9b2013-05-17 13:24:49 +05301775 pm_runtime_put(dwc->dev);
Felipe Balbi72246da2011-08-19 18:10:58 +03001776
1777 return 0;
1778
1779err1:
1780 __dwc3_gadget_ep_disable(dwc->eps[0]);
1781
1782err0:
1783 spin_unlock_irqrestore(&dwc->lock, flags);
Vijayavardhan Vennapusa0557c9b2013-05-17 13:24:49 +05301784 pm_runtime_put(dwc->dev);
Felipe Balbi72246da2011-08-19 18:10:58 +03001785
1786 return ret;
1787}
1788
1789static int dwc3_gadget_stop(struct usb_gadget *g,
1790 struct usb_gadget_driver *driver)
1791{
1792 struct dwc3 *dwc = gadget_to_dwc(g);
1793 unsigned long flags;
1794
1795 spin_lock_irqsave(&dwc->lock, flags);
1796
1797 __dwc3_gadget_ep_disable(dwc->eps[0]);
1798 __dwc3_gadget_ep_disable(dwc->eps[1]);
1799
1800 dwc->gadget_driver = NULL;
1801 dwc->gadget.dev.driver = NULL;
1802
1803 spin_unlock_irqrestore(&dwc->lock, flags);
1804
1805 return 0;
1806}
Paul Zimmerman88df4272012-04-27 13:10:52 +03001807
Felipe Balbi72246da2011-08-19 18:10:58 +03001808static const struct usb_gadget_ops dwc3_gadget_ops = {
1809 .get_frame = dwc3_gadget_get_frame,
1810 .wakeup = dwc3_gadget_wakeup,
1811 .set_selfpowered = dwc3_gadget_set_selfpowered,
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +02001812 .vbus_session = dwc3_gadget_vbus_session,
Vijayavardhan Vennapusa908f1ed2012-10-19 19:51:48 +05301813 .vbus_draw = dwc3_gadget_vbus_draw,
Felipe Balbi72246da2011-08-19 18:10:58 +03001814 .pullup = dwc3_gadget_pullup,
1815 .udc_start = dwc3_gadget_start,
1816 .udc_stop = dwc3_gadget_stop,
1817};
1818
1819/* -------------------------------------------------------------------------- */
1820
1821static int __devinit dwc3_gadget_init_endpoints(struct dwc3 *dwc)
1822{
1823 struct dwc3_ep *dep;
1824 u8 epnum;
1825
1826 INIT_LIST_HEAD(&dwc->gadget.ep_list);
1827
1828 for (epnum = 0; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
1829 dep = kzalloc(sizeof(*dep), GFP_KERNEL);
1830 if (!dep) {
1831 dev_err(dwc->dev, "can't allocate endpoint %d\n",
1832 epnum);
1833 return -ENOMEM;
1834 }
1835
1836 dep->dwc = dwc;
1837 dep->number = epnum;
1838 dwc->eps[epnum] = dep;
1839
1840 snprintf(dep->name, sizeof(dep->name), "ep%d%s", epnum >> 1,
1841 (epnum & 1) ? "in" : "out");
1842 dep->endpoint.name = dep->name;
1843 dep->direction = (epnum & 1);
1844
1845 if (epnum == 0 || epnum == 1) {
1846 dep->endpoint.maxpacket = 512;
1847 dep->endpoint.ops = &dwc3_gadget_ep0_ops;
1848 if (!epnum)
1849 dwc->gadget.ep0 = &dep->endpoint;
1850 } else {
1851 int ret;
1852
1853 dep->endpoint.maxpacket = 1024;
Sebastian Andrzej Siewior12d36c12011-11-03 20:27:50 +01001854 dep->endpoint.max_streams = 15;
Felipe Balbi72246da2011-08-19 18:10:58 +03001855 dep->endpoint.ops = &dwc3_gadget_ep_ops;
1856 list_add_tail(&dep->endpoint.ep_list,
1857 &dwc->gadget.ep_list);
1858
1859 ret = dwc3_alloc_trb_pool(dep);
Felipe Balbi25b8ff62011-11-04 12:32:47 +02001860 if (ret)
Felipe Balbi72246da2011-08-19 18:10:58 +03001861 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001862 }
Felipe Balbi25b8ff62011-11-04 12:32:47 +02001863
Felipe Balbi72246da2011-08-19 18:10:58 +03001864 INIT_LIST_HEAD(&dep->request_list);
1865 INIT_LIST_HEAD(&dep->req_queued);
1866 }
1867
1868 return 0;
1869}
1870
1871static void dwc3_gadget_free_endpoints(struct dwc3 *dwc)
1872{
1873 struct dwc3_ep *dep;
1874 u8 epnum;
1875
1876 for (epnum = 0; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
1877 dep = dwc->eps[epnum];
1878 dwc3_free_trb_pool(dep);
1879
1880 if (epnum != 0 && epnum != 1)
1881 list_del(&dep->endpoint.ep_list);
1882
1883 kfree(dep);
1884 }
1885}
1886
1887static void dwc3_gadget_release(struct device *dev)
1888{
1889 dev_dbg(dev, "%s\n", __func__);
1890}
1891
1892/* -------------------------------------------------------------------------- */
1893static int dwc3_cleanup_done_reqs(struct dwc3 *dwc, struct dwc3_ep *dep,
1894 const struct dwc3_event_depevt *event, int status)
1895{
1896 struct dwc3_request *req;
Felipe Balbif6bafc62012-02-06 11:04:53 +02001897 struct dwc3_trb *trb;
Felipe Balbi72246da2011-08-19 18:10:58 +03001898 unsigned int count;
1899 unsigned int s_pkt = 0;
Pratyush Anand73939b02012-05-25 18:54:56 +05301900 unsigned int trb_status;
Felipe Balbi72246da2011-08-19 18:10:58 +03001901
1902 do {
1903 req = next_request(&dep->req_queued);
Sebastian Andrzej Siewiord39ee7b2011-11-03 10:32:20 +01001904 if (!req) {
1905 WARN_ON_ONCE(1);
1906 return 1;
1907 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001908
Felipe Balbif6bafc62012-02-06 11:04:53 +02001909 trb = req->trb;
Felipe Balbi72246da2011-08-19 18:10:58 +03001910
Felipe Balbif6bafc62012-02-06 11:04:53 +02001911 if ((trb->ctrl & DWC3_TRB_CTRL_HWO) && status != -ESHUTDOWN)
Sebastian Andrzej Siewior0d2f4752011-08-19 19:59:12 +02001912 /*
1913 * We continue despite the error. There is not much we
Paul Zimmerman1d046792012-02-15 18:56:56 -08001914 * can do. If we don't clean it up we loop forever. If
1915 * we skip the TRB then it gets overwritten after a
1916 * while since we use them in a ring buffer. A BUG()
1917 * would help. Lets hope that if this occurs, someone
Sebastian Andrzej Siewior0d2f4752011-08-19 19:59:12 +02001918 * fixes the root cause instead of looking away :)
1919 */
Felipe Balbi72246da2011-08-19 18:10:58 +03001920 dev_err(dwc->dev, "%s's TRB (%p) still owned by HW\n",
1921 dep->name, req->trb);
Felipe Balbif6bafc62012-02-06 11:04:53 +02001922 count = trb->size & DWC3_TRB_SIZE_MASK;
Felipe Balbi72246da2011-08-19 18:10:58 +03001923
1924 if (dep->direction) {
1925 if (count) {
Pratyush Anand73939b02012-05-25 18:54:56 +05301926 trb_status = DWC3_TRB_SIZE_TRBSTS(trb->size);
1927 if (trb_status == DWC3_TRBSTS_MISSED_ISOC) {
1928 dev_dbg(dwc->dev, "incomplete IN transfer %s\n",
1929 dep->name);
Pratyush Anand921b0b82013-01-14 15:59:32 +05301930 /*
1931 * If missed isoc occurred and there is
1932 * no request queued then issue END
1933 * TRANSFER, so that core generates
1934 * next xfernotready and we will issue
1935 * a fresh START TRANSFER.
1936 * If there are still queued request
1937 * then wait, do not issue either END
1938 * or UPDATE TRANSFER, just attach next
1939 * request in request_list during
1940 * giveback.If any future queued request
1941 * is successfully transferred then we
1942 * will issue UPDATE TRANSFER for all
1943 * request in the request_list.
1944 */
Pratyush Anand73939b02012-05-25 18:54:56 +05301945 dep->flags |= DWC3_EP_MISSED_ISOC;
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +05301946 dbg_event(dep->number, "MISSED ISOC",
1947 status);
Pratyush Anand73939b02012-05-25 18:54:56 +05301948 } else {
1949 dev_err(dwc->dev, "incomplete IN transfer %s\n",
1950 dep->name);
1951 status = -ECONNRESET;
1952 }
Pratyush Anand921b0b82013-01-14 15:59:32 +05301953 } else {
1954 dep->flags &= ~DWC3_EP_MISSED_ISOC;
Felipe Balbi72246da2011-08-19 18:10:58 +03001955 }
1956 } else {
1957 if (count && (event->status & DEPEVT_STATUS_SHORT))
1958 s_pkt = 1;
1959 }
1960
1961 /*
1962 * We assume here we will always receive the entire data block
1963 * which we should receive. Meaning, if we program RX to
1964 * receive 4K but we receive only 2K, we assume that's all we
1965 * should receive and we simply bounce the request back to the
1966 * gadget driver for further processing.
1967 */
1968 req->request.actual += req->request.length - count;
1969 dwc3_gadget_giveback(dep, req, status);
1970 if (s_pkt)
1971 break;
Felipe Balbif6bafc62012-02-06 11:04:53 +02001972 if ((event->status & DEPEVT_STATUS_LST) &&
Pratyush Anand413dba62012-06-03 19:43:19 +05301973 (trb->ctrl & (DWC3_TRB_CTRL_LST |
1974 DWC3_TRB_CTRL_HWO)))
Felipe Balbi72246da2011-08-19 18:10:58 +03001975 break;
Felipe Balbif6bafc62012-02-06 11:04:53 +02001976 if ((event->status & DEPEVT_STATUS_IOC) &&
1977 (trb->ctrl & DWC3_TRB_CTRL_IOC))
Felipe Balbi72246da2011-08-19 18:10:58 +03001978 break;
1979 } while (1);
1980
Pratyush Anand18bbcb02013-01-14 15:59:34 +05301981 if (usb_endpoint_xfer_isoc(dep->endpoint.desc) &&
1982 list_empty(&dep->req_queued)) {
Vijayavardhan Vennapusa91ba6532013-01-30 17:35:45 +05301983 if (list_empty(&dep->request_list))
Pratyush Anand18bbcb02013-01-14 15:59:34 +05301984 /*
1985 * If there is no entry in request list then do
1986 * not issue END TRANSFER now. Just set PENDING
1987 * flag, so that END TRANSFER is issued when an
1988 * entry is added into request list.
1989 */
Vijayavardhan Vennapusa91ba6532013-01-30 17:35:45 +05301990 dep->flags |= DWC3_EP_PENDING_REQUEST;
1991 else
Pratyush Anand18bbcb02013-01-14 15:59:34 +05301992 dwc3_stop_active_transfer(dwc, dep->number);
Vijayavardhan Vennapusa91ba6532013-01-30 17:35:45 +05301993 dep->flags &= ~DWC3_EP_MISSED_ISOC;
Pratyush Anand921b0b82013-01-14 15:59:32 +05301994 return 1;
1995 }
1996
Felipe Balbif6bafc62012-02-06 11:04:53 +02001997 if ((event->status & DEPEVT_STATUS_IOC) &&
1998 (trb->ctrl & DWC3_TRB_CTRL_IOC))
Felipe Balbi72246da2011-08-19 18:10:58 +03001999 return 0;
2000 return 1;
2001}
2002
2003static void dwc3_endpoint_transfer_complete(struct dwc3 *dwc,
2004 struct dwc3_ep *dep, const struct dwc3_event_depevt *event,
2005 int start_new)
2006{
2007 unsigned status = 0;
2008 int clean_busy;
2009
2010 if (event->status & DEPEVT_STATUS_BUSERR)
2011 status = -ECONNRESET;
2012
Paul Zimmerman1d046792012-02-15 18:56:56 -08002013 clean_busy = dwc3_cleanup_done_reqs(dwc, dep, event, status);
Paul Zimmermanc2df85c2012-02-24 17:32:18 -08002014 if (clean_busy)
Felipe Balbi72246da2011-08-19 18:10:58 +03002015 dep->flags &= ~DWC3_EP_BUSY;
Felipe Balbifae2b902011-10-14 13:00:30 +03002016
2017 /*
2018 * WORKAROUND: This is the 2nd half of U1/U2 -> U0 workaround.
2019 * See dwc3_gadget_linksts_change_interrupt() for 1st half.
2020 */
2021 if (dwc->revision < DWC3_REVISION_183A) {
2022 u32 reg;
2023 int i;
2024
2025 for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) {
Moiz Sonasatheed03f12012-08-01 14:08:30 -05002026 dep = dwc->eps[i];
Felipe Balbifae2b902011-10-14 13:00:30 +03002027
2028 if (!(dep->flags & DWC3_EP_ENABLED))
2029 continue;
2030
2031 if (!list_empty(&dep->req_queued))
2032 return;
2033 }
2034
2035 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2036 reg |= dwc->u1u2;
2037 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2038
2039 dwc->u1u2 = 0;
2040 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002041}
2042
Felipe Balbi72246da2011-08-19 18:10:58 +03002043static void dwc3_endpoint_interrupt(struct dwc3 *dwc,
2044 const struct dwc3_event_depevt *event)
2045{
2046 struct dwc3_ep *dep;
2047 u8 epnum = event->endpoint_number;
2048
2049 dep = dwc->eps[epnum];
2050
Felipe Balbia09be0a2012-06-06 09:19:35 +03002051 if (!(dep->flags & DWC3_EP_ENABLED))
2052 return;
2053
Felipe Balbi72246da2011-08-19 18:10:58 +03002054 dev_vdbg(dwc->dev, "%s: %s\n", dep->name,
2055 dwc3_ep_event_string(event->endpoint_event));
2056
2057 if (epnum == 0 || epnum == 1) {
2058 dwc3_ep0_interrupt(dwc, event);
2059 return;
2060 }
2061
2062 switch (event->endpoint_event) {
2063 case DWC3_DEPEVT_XFERCOMPLETE:
Felipe Balbi4959cfc2012-06-06 12:04:13 +03002064 dep->resource_index = 0;
Paul Zimmermanc2df85c2012-02-24 17:32:18 -08002065
Ido Shayevitz57cdac12012-03-12 20:25:24 +02002066 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbi72246da2011-08-19 18:10:58 +03002067 dev_dbg(dwc->dev, "%s is an Isochronous endpoint\n",
2068 dep->name);
2069 return;
2070 }
2071
2072 dwc3_endpoint_transfer_complete(dwc, dep, event, 1);
2073 break;
2074 case DWC3_DEPEVT_XFERINPROGRESS:
Ido Shayevitz57cdac12012-03-12 20:25:24 +02002075 if (!usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbi72246da2011-08-19 18:10:58 +03002076 dev_dbg(dwc->dev, "%s is not an Isochronous endpoint\n",
2077 dep->name);
2078 return;
2079 }
2080
2081 dwc3_endpoint_transfer_complete(dwc, dep, event, 0);
2082 break;
2083 case DWC3_DEPEVT_XFERNOTREADY:
Ido Shayevitz57cdac12012-03-12 20:25:24 +02002084 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbi72246da2011-08-19 18:10:58 +03002085 dwc3_gadget_start_isoc(dwc, dep, event);
2086 } else {
2087 int ret;
2088
2089 dev_vdbg(dwc->dev, "%s: reason %s\n",
Felipe Balbi40aa41f2012-01-18 17:06:03 +02002090 dep->name, event->status &
2091 DEPEVT_STATUS_TRANSFER_ACTIVE
Felipe Balbi72246da2011-08-19 18:10:58 +03002092 ? "Transfer Active"
2093 : "Transfer Not Active");
2094
2095 ret = __dwc3_gadget_kick_transfer(dep, 0, 1);
2096 if (!ret || ret == -EBUSY)
2097 return;
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +05302098 else
2099 dbg_event(dep->number, "QUEUE", ret);
Felipe Balbi72246da2011-08-19 18:10:58 +03002100
2101 dev_dbg(dwc->dev, "%s: failed to kick transfers\n",
2102 dep->name);
2103 }
2104
2105 break;
Felipe Balbi879631a2011-09-30 10:58:47 +03002106 case DWC3_DEPEVT_STREAMEVT:
Ido Shayevitz57cdac12012-03-12 20:25:24 +02002107 if (!usb_endpoint_xfer_bulk(dep->endpoint.desc)) {
Felipe Balbi879631a2011-09-30 10:58:47 +03002108 dev_err(dwc->dev, "Stream event for non-Bulk %s\n",
2109 dep->name);
2110 return;
2111 }
2112
2113 switch (event->status) {
2114 case DEPEVT_STREAMEVT_FOUND:
2115 dev_vdbg(dwc->dev, "Stream %d found and started\n",
2116 event->parameters);
2117
2118 break;
2119 case DEPEVT_STREAMEVT_NOTFOUND:
2120 /* FALLTHROUGH */
2121 default:
2122 dev_dbg(dwc->dev, "Couldn't find suitable stream\n");
2123 }
2124 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03002125 case DWC3_DEPEVT_RXTXFIFOEVT:
2126 dev_dbg(dwc->dev, "%s FIFO Overrun\n", dep->name);
2127 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03002128 case DWC3_DEPEVT_EPCMDCMPLT:
Felipe Balbib129eb72012-02-17 12:10:04 +02002129 dev_vdbg(dwc->dev, "Endpoint Command Complete\n");
Felipe Balbi72246da2011-08-19 18:10:58 +03002130 break;
2131 }
2132}
2133
2134static void dwc3_disconnect_gadget(struct dwc3 *dwc)
2135{
2136 if (dwc->gadget_driver && dwc->gadget_driver->disconnect) {
2137 spin_unlock(&dwc->lock);
2138 dwc->gadget_driver->disconnect(&dwc->gadget);
2139 spin_lock(&dwc->lock);
2140 }
2141}
2142
2143static void dwc3_stop_active_transfer(struct dwc3 *dwc, u32 epnum)
2144{
2145 struct dwc3_ep *dep;
2146 struct dwc3_gadget_ep_cmd_params params;
2147 u32 cmd;
2148 int ret;
2149
2150 dep = dwc->eps[epnum];
2151
Felipe Balbi4959cfc2012-06-06 12:04:13 +03002152 if (!dep->resource_index)
Pratyush Anand6263ebe2012-06-23 02:23:08 +05302153 return;
2154
Pratyush Anande67fdeb2012-07-06 15:19:10 +05302155 /*
2156 * NOTICE: We are violating what the Databook says about the
2157 * EndTransfer command. Ideally we would _always_ wait for the
2158 * EndTransfer Command Completion IRQ, but that's causing too
2159 * much trouble synchronizing between us and gadget driver.
2160 *
2161 * We have discussed this with the IP Provider and it was
2162 * suggested to giveback all requests here, but give HW some
2163 * extra time to synchronize with the interconnect. We're using
2164 * an arbitraty 100us delay for that.
2165 *
2166 * Note also that a similar handling was tested by Synopsys
2167 * (thanks a lot Paul) and nothing bad has come out of it.
2168 * In short, what we're doing is:
2169 *
2170 * - Issue EndTransfer WITH CMDIOC bit set
2171 * - Wait 100us
2172 */
2173
Pratyush Anand6263ebe2012-06-23 02:23:08 +05302174 cmd = DWC3_DEPCMD_ENDTRANSFER;
2175 cmd |= DWC3_DEPCMD_HIPRI_FORCERM | DWC3_DEPCMD_CMDIOC;
Felipe Balbi4959cfc2012-06-06 12:04:13 +03002176 cmd |= DWC3_DEPCMD_PARAM(dep->resource_index);
Pratyush Anand6263ebe2012-06-23 02:23:08 +05302177 memset(&params, 0, sizeof(params));
2178 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number, cmd, &params);
2179 WARN_ON_ONCE(ret);
Felipe Balbi4959cfc2012-06-06 12:04:13 +03002180 dep->resource_index = 0;
Felipe Balbi1df89b62012-10-04 11:58:00 +03002181 dep->flags &= ~DWC3_EP_BUSY;
Pratyush Anande67fdeb2012-07-06 15:19:10 +05302182 udelay(100);
Felipe Balbi72246da2011-08-19 18:10:58 +03002183}
2184
2185static void dwc3_stop_active_transfers(struct dwc3 *dwc)
2186{
2187 u32 epnum;
2188
2189 for (epnum = 2; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
2190 struct dwc3_ep *dep;
2191
2192 dep = dwc->eps[epnum];
2193 if (!(dep->flags & DWC3_EP_ENABLED))
2194 continue;
2195
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +02002196 dwc3_remove_requests(dwc, dep);
Felipe Balbi72246da2011-08-19 18:10:58 +03002197 }
2198}
2199
2200static void dwc3_clear_stall_all_ep(struct dwc3 *dwc)
2201{
2202 u32 epnum;
2203
2204 for (epnum = 1; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
2205 struct dwc3_ep *dep;
2206 struct dwc3_gadget_ep_cmd_params params;
2207 int ret;
2208
2209 dep = dwc->eps[epnum];
2210
2211 if (!(dep->flags & DWC3_EP_STALL))
2212 continue;
2213
2214 dep->flags &= ~DWC3_EP_STALL;
2215
2216 memset(&params, 0, sizeof(params));
2217 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number,
2218 DWC3_DEPCMD_CLEARSTALL, &params);
2219 WARN_ON_ONCE(ret);
2220 }
2221}
2222
2223static void dwc3_gadget_disconnect_interrupt(struct dwc3 *dwc)
2224{
Felipe Balbi34d548c2012-05-24 10:30:01 +03002225 int reg;
2226
Felipe Balbi72246da2011-08-19 18:10:58 +03002227 dev_vdbg(dwc->dev, "%s\n", __func__);
Felipe Balbi72246da2011-08-19 18:10:58 +03002228
2229 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2230 reg &= ~DWC3_DCTL_INITU1ENA;
2231 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2232
2233 reg &= ~DWC3_DCTL_INITU2ENA;
2234 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +03002235
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +05302236 dbg_event(0xFF, "DISCONNECT", 0);
Felipe Balbi72246da2011-08-19 18:10:58 +03002237 dwc3_disconnect_gadget(dwc);
Paul Zimmermanb23c8432011-09-30 10:58:42 +03002238 dwc->start_config_issued = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03002239
2240 dwc->gadget.speed = USB_SPEED_UNKNOWN;
Felipe Balbidf62df52011-10-14 15:11:49 +03002241 dwc->setup_packet_pending = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03002242}
2243
Paul Zimmermandffb81b2012-04-27 12:54:05 +03002244static void dwc3_gadget_usb3_phy_suspend(struct dwc3 *dwc, int suspend)
Felipe Balbi72246da2011-08-19 18:10:58 +03002245{
2246 u32 reg;
2247
2248 reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0));
2249
Paul Zimmermandffb81b2012-04-27 12:54:05 +03002250 if (suspend)
Felipe Balbi72246da2011-08-19 18:10:58 +03002251 reg |= DWC3_GUSB3PIPECTL_SUSPHY;
Paul Zimmermandffb81b2012-04-27 12:54:05 +03002252 else
2253 reg &= ~DWC3_GUSB3PIPECTL_SUSPHY;
Felipe Balbi72246da2011-08-19 18:10:58 +03002254
2255 dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg);
2256}
2257
Paul Zimmermandffb81b2012-04-27 12:54:05 +03002258static void dwc3_gadget_usb2_phy_suspend(struct dwc3 *dwc, int suspend)
Felipe Balbi72246da2011-08-19 18:10:58 +03002259{
2260 u32 reg;
2261
2262 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
2263
Paul Zimmermandffb81b2012-04-27 12:54:05 +03002264 if (suspend)
Felipe Balbi72246da2011-08-19 18:10:58 +03002265 reg |= DWC3_GUSB2PHYCFG_SUSPHY;
Paul Zimmermandffb81b2012-04-27 12:54:05 +03002266 else
2267 reg &= ~DWC3_GUSB2PHYCFG_SUSPHY;
Felipe Balbi72246da2011-08-19 18:10:58 +03002268
2269 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
2270}
2271
2272static void dwc3_gadget_reset_interrupt(struct dwc3 *dwc)
2273{
2274 u32 reg;
Vijayavardhan Vennapusa908f1ed2012-10-19 19:51:48 +05302275 struct dwc3_otg *dotg = dwc->dotg;
Felipe Balbi72246da2011-08-19 18:10:58 +03002276
2277 dev_vdbg(dwc->dev, "%s\n", __func__);
2278
Felipe Balbidf62df52011-10-14 15:11:49 +03002279 /*
2280 * WORKAROUND: DWC3 revisions <1.88a have an issue which
2281 * would cause a missing Disconnect Event if there's a
2282 * pending Setup Packet in the FIFO.
2283 *
2284 * There's no suggested workaround on the official Bug
2285 * report, which states that "unless the driver/application
2286 * is doing any special handling of a disconnect event,
2287 * there is no functional issue".
2288 *
2289 * Unfortunately, it turns out that we _do_ some special
2290 * handling of a disconnect event, namely complete all
2291 * pending transfers, notify gadget driver of the
2292 * disconnection, and so on.
2293 *
2294 * Our suggested workaround is to follow the Disconnect
2295 * Event steps here, instead, based on a setup_packet_pending
2296 * flag. Such flag gets set whenever we have a XferNotReady
2297 * event on EP0 and gets cleared on XferComplete for the
2298 * same endpoint.
2299 *
2300 * Refers to:
2301 *
2302 * STAR#9000466709: RTL: Device : Disconnect event not
2303 * generated if setup packet pending in FIFO
2304 */
2305 if (dwc->revision < DWC3_REVISION_188A) {
2306 if (dwc->setup_packet_pending)
2307 dwc3_gadget_disconnect_interrupt(dwc);
2308 }
2309
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +05302310 dbg_event(0xFF, "BUS RST", 0);
Felipe Balbi961906e2011-12-20 15:37:21 +02002311 /* after reset -> Default State */
2312 dwc->dev_state = DWC3_DEFAULT_STATE;
2313
Paul Zimmerman88df4272012-04-27 13:10:52 +03002314 /* Recent versions support automatic phy suspend and don't need this */
2315 if (dwc->revision < DWC3_REVISION_194A) {
2316 /* Resume PHYs */
2317 dwc3_gadget_usb2_phy_suspend(dwc, false);
2318 dwc3_gadget_usb3_phy_suspend(dwc, false);
2319 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002320
Vijayavardhan Vennapusa908f1ed2012-10-19 19:51:48 +05302321 if (dotg && dotg->otg.phy)
2322 usb_phy_set_power(dotg->otg.phy, 0);
2323
Felipe Balbi72246da2011-08-19 18:10:58 +03002324 if (dwc->gadget.speed != USB_SPEED_UNKNOWN)
2325 dwc3_disconnect_gadget(dwc);
2326
2327 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2328 reg &= ~DWC3_DCTL_TSTCTRL_MASK;
2329 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Gerard Cauvy3b637362012-02-10 12:21:18 +02002330 dwc->test_mode = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03002331
2332 dwc3_stop_active_transfers(dwc);
2333 dwc3_clear_stall_all_ep(dwc);
Paul Zimmermanb23c8432011-09-30 10:58:42 +03002334 dwc->start_config_issued = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03002335
2336 /* Reset device address to zero */
2337 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2338 reg &= ~(DWC3_DCFG_DEVADDR_MASK);
2339 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +03002340}
2341
2342static void dwc3_update_ram_clk_sel(struct dwc3 *dwc, u32 speed)
2343{
2344 u32 reg;
2345 u32 usb30_clock = DWC3_GCTL_CLK_BUS;
2346
2347 /*
2348 * We change the clock only at SS but I dunno why I would want to do
2349 * this. Maybe it becomes part of the power saving plan.
2350 */
2351
2352 if (speed != DWC3_DSTS_SUPERSPEED)
2353 return;
2354
2355 /*
2356 * RAMClkSel is reset to 0 after USB reset, so it must be reprogrammed
2357 * each time on Connect Done.
2358 */
2359 if (!usb30_clock)
2360 return;
2361
2362 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
2363 reg |= DWC3_GCTL_RAMCLKSEL(usb30_clock);
2364 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
2365}
2366
Paul Zimmermandffb81b2012-04-27 12:54:05 +03002367static void dwc3_gadget_phy_suspend(struct dwc3 *dwc, u8 speed)
Felipe Balbi72246da2011-08-19 18:10:58 +03002368{
2369 switch (speed) {
2370 case USB_SPEED_SUPER:
Paul Zimmermandffb81b2012-04-27 12:54:05 +03002371 dwc3_gadget_usb2_phy_suspend(dwc, true);
Felipe Balbi72246da2011-08-19 18:10:58 +03002372 break;
2373 case USB_SPEED_HIGH:
2374 case USB_SPEED_FULL:
2375 case USB_SPEED_LOW:
Paul Zimmermandffb81b2012-04-27 12:54:05 +03002376 dwc3_gadget_usb3_phy_suspend(dwc, true);
Felipe Balbi72246da2011-08-19 18:10:58 +03002377 break;
2378 }
2379}
2380
2381static void dwc3_gadget_conndone_interrupt(struct dwc3 *dwc)
2382{
2383 struct dwc3_gadget_ep_cmd_params params;
2384 struct dwc3_ep *dep;
2385 int ret;
2386 u32 reg;
2387 u8 speed;
2388
2389 dev_vdbg(dwc->dev, "%s\n", __func__);
2390
2391 memset(&params, 0x00, sizeof(params));
2392
Felipe Balbi72246da2011-08-19 18:10:58 +03002393 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
2394 speed = reg & DWC3_DSTS_CONNECTSPD;
2395 dwc->speed = speed;
2396
2397 dwc3_update_ram_clk_sel(dwc, speed);
2398
2399 switch (speed) {
2400 case DWC3_DCFG_SUPERSPEED:
Felipe Balbi05870c52011-10-14 14:51:38 +03002401 /*
2402 * WORKAROUND: DWC3 revisions <1.90a have an issue which
2403 * would cause a missing USB3 Reset event.
2404 *
2405 * In such situations, we should force a USB3 Reset
2406 * event by calling our dwc3_gadget_reset_interrupt()
2407 * routine.
2408 *
2409 * Refers to:
2410 *
2411 * STAR#9000483510: RTL: SS : USB3 reset event may
2412 * not be generated always when the link enters poll
2413 */
2414 if (dwc->revision < DWC3_REVISION_190A)
2415 dwc3_gadget_reset_interrupt(dwc);
2416
Felipe Balbi72246da2011-08-19 18:10:58 +03002417 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
2418 dwc->gadget.ep0->maxpacket = 512;
2419 dwc->gadget.speed = USB_SPEED_SUPER;
2420 break;
2421 case DWC3_DCFG_HIGHSPEED:
2422 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
2423 dwc->gadget.ep0->maxpacket = 64;
2424 dwc->gadget.speed = USB_SPEED_HIGH;
2425 break;
2426 case DWC3_DCFG_FULLSPEED2:
2427 case DWC3_DCFG_FULLSPEED1:
2428 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
2429 dwc->gadget.ep0->maxpacket = 64;
2430 dwc->gadget.speed = USB_SPEED_FULL;
2431 break;
2432 case DWC3_DCFG_LOWSPEED:
2433 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(8);
2434 dwc->gadget.ep0->maxpacket = 8;
2435 dwc->gadget.speed = USB_SPEED_LOW;
2436 break;
2437 }
2438
Paul Zimmerman88df4272012-04-27 13:10:52 +03002439 /* Recent versions support automatic phy suspend and don't need this */
2440 if (dwc->revision < DWC3_REVISION_194A) {
2441 /* Suspend unneeded PHY */
2442 dwc3_gadget_phy_suspend(dwc, dwc->gadget.speed);
2443 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002444
2445 dep = dwc->eps[0];
Felipe Balbi07e0ee82012-07-16 14:08:16 +03002446 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, true);
Felipe Balbi72246da2011-08-19 18:10:58 +03002447 if (ret) {
2448 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
2449 return;
2450 }
2451
2452 dep = dwc->eps[1];
Felipe Balbi07e0ee82012-07-16 14:08:16 +03002453 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, true);
Felipe Balbi72246da2011-08-19 18:10:58 +03002454 if (ret) {
2455 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
2456 return;
2457 }
2458
2459 /*
2460 * Configure PHY via GUSB3PIPECTLn if required.
2461 *
2462 * Update GTXFIFOSIZn
2463 *
2464 * In both cases reset values should be sufficient.
2465 */
2466}
2467
2468static void dwc3_gadget_wakeup_interrupt(struct dwc3 *dwc)
2469{
2470 dev_vdbg(dwc->dev, "%s\n", __func__);
2471
2472 /*
2473 * TODO take core out of low power mode when that's
2474 * implemented.
2475 */
2476
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +05302477 dbg_event(0xFF, "WAKEUP", 0);
Felipe Balbi72246da2011-08-19 18:10:58 +03002478 dwc->gadget_driver->resume(&dwc->gadget);
2479}
2480
2481static void dwc3_gadget_linksts_change_interrupt(struct dwc3 *dwc,
2482 unsigned int evtinfo)
2483{
Felipe Balbifae2b902011-10-14 13:00:30 +03002484 enum dwc3_link_state next = evtinfo & DWC3_LINK_STATE_MASK;
2485
2486 /*
2487 * WORKAROUND: DWC3 Revisions <1.83a have an issue which, depending
2488 * on the link partner, the USB session might do multiple entry/exit
2489 * of low power states before a transfer takes place.
2490 *
2491 * Due to this problem, we might experience lower throughput. The
2492 * suggested workaround is to disable DCTL[12:9] bits if we're
2493 * transitioning from U1/U2 to U0 and enable those bits again
2494 * after a transfer completes and there are no pending transfers
2495 * on any of the enabled endpoints.
2496 *
2497 * This is the first half of that workaround.
2498 *
2499 * Refers to:
2500 *
2501 * STAR#9000446952: RTL: Device SS : if U1/U2 ->U0 takes >128us
2502 * core send LGO_Ux entering U0
2503 */
2504 if (dwc->revision < DWC3_REVISION_183A) {
2505 if (next == DWC3_LINK_STATE_U0) {
2506 u32 u1u2;
2507 u32 reg;
2508
2509 switch (dwc->link_state) {
2510 case DWC3_LINK_STATE_U1:
2511 case DWC3_LINK_STATE_U2:
2512 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2513 u1u2 = reg & (DWC3_DCTL_INITU2ENA
2514 | DWC3_DCTL_ACCEPTU2ENA
2515 | DWC3_DCTL_INITU1ENA
2516 | DWC3_DCTL_ACCEPTU1ENA);
2517
2518 if (!dwc->u1u2)
2519 dwc->u1u2 = reg & u1u2;
2520
2521 reg &= ~u1u2;
2522
2523 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2524 break;
2525 default:
2526 /* do nothing */
2527 break;
2528 }
2529 }
2530 }
2531
Vijayavardhan Vennapusa54be1d62012-10-06 18:32:06 +05302532 if (next == DWC3_LINK_STATE_U0) {
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +05302533 if (dwc->link_state == DWC3_LINK_STATE_U3) {
2534 dbg_event(0xFF, "RESUME", 0);
Vijayavardhan Vennapusa54be1d62012-10-06 18:32:06 +05302535 dwc->gadget_driver->resume(&dwc->gadget);
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +05302536 }
Vijayavardhan Vennapusa54be1d62012-10-06 18:32:06 +05302537 } else if (next == DWC3_LINK_STATE_U3) {
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +05302538 dbg_event(0xFF, "SUSPEND", 0);
Vijayavardhan Vennapusa54be1d62012-10-06 18:32:06 +05302539 dwc->gadget_driver->suspend(&dwc->gadget);
2540 }
2541
Felipe Balbifae2b902011-10-14 13:00:30 +03002542 dwc->link_state = next;
Felipe Balbi019ac832011-09-08 21:18:47 +03002543
2544 dev_vdbg(dwc->dev, "%s link %d\n", __func__, dwc->link_state);
Felipe Balbi72246da2011-08-19 18:10:58 +03002545}
2546
2547static void dwc3_gadget_interrupt(struct dwc3 *dwc,
2548 const struct dwc3_event_devt *event)
2549{
2550 switch (event->type) {
2551 case DWC3_DEVICE_EVENT_DISCONNECT:
2552 dwc3_gadget_disconnect_interrupt(dwc);
2553 break;
2554 case DWC3_DEVICE_EVENT_RESET:
2555 dwc3_gadget_reset_interrupt(dwc);
2556 break;
2557 case DWC3_DEVICE_EVENT_CONNECT_DONE:
2558 dwc3_gadget_conndone_interrupt(dwc);
2559 break;
2560 case DWC3_DEVICE_EVENT_WAKEUP:
2561 dwc3_gadget_wakeup_interrupt(dwc);
2562 break;
2563 case DWC3_DEVICE_EVENT_LINK_STATUS_CHANGE:
2564 dwc3_gadget_linksts_change_interrupt(dwc, event->event_info);
2565 break;
2566 case DWC3_DEVICE_EVENT_EOPF:
2567 dev_vdbg(dwc->dev, "End of Periodic Frame\n");
2568 break;
2569 case DWC3_DEVICE_EVENT_SOF:
2570 dev_vdbg(dwc->dev, "Start of Periodic Frame\n");
2571 break;
2572 case DWC3_DEVICE_EVENT_ERRATIC_ERROR:
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +05302573 dbg_event(0xFF, "ERROR", 0);
Felipe Balbi72246da2011-08-19 18:10:58 +03002574 dev_vdbg(dwc->dev, "Erratic Error\n");
2575 break;
2576 case DWC3_DEVICE_EVENT_CMD_CMPL:
2577 dev_vdbg(dwc->dev, "Command Complete\n");
2578 break;
2579 case DWC3_DEVICE_EVENT_OVERFLOW:
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +05302580 dbg_event(0xFF, "OVERFL", 0);
Felipe Balbi72246da2011-08-19 18:10:58 +03002581 dev_vdbg(dwc->dev, "Overflow\n");
Pavankumar Kondetid393e172012-06-12 16:07:29 +05302582 /*
2583 * Controllers prior to 2.30a revision has a bug where
2584 * Overflow Event may overwrite an unacknowledged event
2585 * in the event buffer. The severity of the issue depends
2586 * on the overwritten event type. Add a warning message
2587 * saying that an event is overwritten.
2588 *
2589 * TODO: In future we may need to see if we can re-enumerate
2590 * with host.
2591 */
2592 if (dwc->revision < DWC3_REVISION_230A)
2593 dev_warn(dwc->dev, "Unacknowledged event overwritten\n");
Felipe Balbi72246da2011-08-19 18:10:58 +03002594 break;
Pavankumar Kondeti33fe6f12012-06-12 16:21:46 +05302595 case DWC3_DEVICE_EVENT_VENDOR_DEV_TEST_LMP:
2596 /*
2597 * Controllers prior to 2.30a revision has a bug, due to which
2598 * a vendor device test LMP event can not be filtered. But
2599 * this event is not handled in the current code. This is a
2600 * special event and 8 bytes of data will follow the event.
2601 * Handling this event is tricky when event buffer is almost
2602 * full. Moreover this event will not occur in normal scenario
2603 * and can only happen with special hosts in testing scenarios.
2604 * Add a warning message to indicate that this event is received
2605 * which means that event buffer might have corrupted.
2606 */
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +05302607 dbg_event(0xFF, "TSTLMP", 0);
Pavankumar Kondeti33fe6f12012-06-12 16:21:46 +05302608 if (dwc->revision < DWC3_REVISION_230A)
2609 dev_warn(dwc->dev, "Vendor Device Test LMP Received\n");
2610 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03002611 default:
2612 dev_dbg(dwc->dev, "UNKNOWN IRQ %d\n", event->type);
2613 }
2614}
2615
2616static void dwc3_process_event_entry(struct dwc3 *dwc,
2617 const union dwc3_event *event)
2618{
2619 /* Endpoint IRQ, handle it and return early */
2620 if (event->type.is_devspec == 0) {
2621 /* depevt */
2622 return dwc3_endpoint_interrupt(dwc, &event->depevt);
2623 }
2624
2625 switch (event->type.type) {
2626 case DWC3_EVENT_TYPE_DEV:
2627 dwc3_gadget_interrupt(dwc, &event->devt);
2628 break;
2629 /* REVISIT what to do with Carkit and I2C events ? */
2630 default:
2631 dev_err(dwc->dev, "UNKNOWN IRQ type %d\n", event->raw);
2632 }
2633}
2634
2635static irqreturn_t dwc3_process_event_buf(struct dwc3 *dwc, u32 buf)
2636{
2637 struct dwc3_event_buffer *evt;
2638 int left;
2639 u32 count;
2640
2641 count = dwc3_readl(dwc->regs, DWC3_GEVNTCOUNT(buf));
2642 count &= DWC3_GEVNTCOUNT_MASK;
2643 if (!count)
2644 return IRQ_NONE;
2645
2646 evt = dwc->ev_buffs[buf];
2647 left = count;
2648
2649 while (left > 0) {
2650 union dwc3_event event;
2651
Felipe Balbid70d8442012-02-06 13:40:17 +02002652 event.raw = *(u32 *) (evt->buf + evt->lpos);
2653
Felipe Balbi72246da2011-08-19 18:10:58 +03002654 dwc3_process_event_entry(dwc, &event);
2655 /*
2656 * XXX we wrap around correctly to the next entry as almost all
2657 * entries are 4 bytes in size. There is one entry which has 12
2658 * bytes which is a regular entry followed by 8 bytes data. ATM
2659 * I don't know how things are organized if were get next to the
2660 * a boundary so I worry about that once we try to handle that.
2661 */
2662 evt->lpos = (evt->lpos + 4) % DWC3_EVENT_BUFFERS_SIZE;
2663 left -= 4;
2664
2665 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(buf), 4);
2666 }
2667
2668 return IRQ_HANDLED;
2669}
2670
2671static irqreturn_t dwc3_interrupt(int irq, void *_dwc)
2672{
2673 struct dwc3 *dwc = _dwc;
2674 int i;
2675 irqreturn_t ret = IRQ_NONE;
2676
2677 spin_lock(&dwc->lock);
2678
Felipe Balbi9f622b22011-10-12 10:31:04 +03002679 for (i = 0; i < dwc->num_event_buffers; i++) {
Felipe Balbi72246da2011-08-19 18:10:58 +03002680 irqreturn_t status;
2681
2682 status = dwc3_process_event_buf(dwc, i);
2683 if (status == IRQ_HANDLED)
2684 ret = status;
2685 }
2686
2687 spin_unlock(&dwc->lock);
2688
2689 return ret;
2690}
2691
2692/**
2693 * dwc3_gadget_init - Initializes gadget related registers
Paul Zimmerman1d046792012-02-15 18:56:56 -08002694 * @dwc: pointer to our controller context structure
Felipe Balbi72246da2011-08-19 18:10:58 +03002695 *
2696 * Returns 0 on success otherwise negative errno.
2697 */
2698int __devinit dwc3_gadget_init(struct dwc3 *dwc)
2699{
2700 u32 reg;
2701 int ret;
2702 int irq;
2703
2704 dwc->ctrl_req = dma_alloc_coherent(dwc->dev, sizeof(*dwc->ctrl_req),
2705 &dwc->ctrl_req_addr, GFP_KERNEL);
2706 if (!dwc->ctrl_req) {
2707 dev_err(dwc->dev, "failed to allocate ctrl request\n");
2708 ret = -ENOMEM;
2709 goto err0;
2710 }
2711
2712 dwc->ep0_trb = dma_alloc_coherent(dwc->dev, sizeof(*dwc->ep0_trb),
2713 &dwc->ep0_trb_addr, GFP_KERNEL);
2714 if (!dwc->ep0_trb) {
2715 dev_err(dwc->dev, "failed to allocate ep0 trb\n");
2716 ret = -ENOMEM;
2717 goto err1;
2718 }
2719
Felipe Balbib0791fb2012-05-04 12:58:14 +03002720 dwc->setup_buf = kzalloc(DWC3_EP0_BOUNCE_SIZE, GFP_KERNEL);
Felipe Balbi72246da2011-08-19 18:10:58 +03002721 if (!dwc->setup_buf) {
2722 dev_err(dwc->dev, "failed to allocate setup buffer\n");
2723 ret = -ENOMEM;
2724 goto err2;
2725 }
2726
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002727 dwc->ep0_bounce = dma_alloc_coherent(dwc->dev,
Felipe Balbib0791fb2012-05-04 12:58:14 +03002728 DWC3_EP0_BOUNCE_SIZE, &dwc->ep0_bounce_addr,
2729 GFP_KERNEL);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002730 if (!dwc->ep0_bounce) {
2731 dev_err(dwc->dev, "failed to allocate ep0 bounce buffer\n");
2732 ret = -ENOMEM;
2733 goto err3;
2734 }
2735
Felipe Balbi72246da2011-08-19 18:10:58 +03002736 dev_set_name(&dwc->gadget.dev, "gadget");
2737
2738 dwc->gadget.ops = &dwc3_gadget_ops;
Manu Gautama7b082a2012-11-06 09:50:09 +05302739 dwc->gadget.max_speed = USB_SPEED_SUPER;
Felipe Balbi72246da2011-08-19 18:10:58 +03002740 dwc->gadget.speed = USB_SPEED_UNKNOWN;
2741 dwc->gadget.dev.parent = dwc->dev;
Felipe Balbieeb720f2011-11-28 12:46:59 +02002742 dwc->gadget.sg_supported = true;
Felipe Balbi72246da2011-08-19 18:10:58 +03002743
2744 dma_set_coherent_mask(&dwc->gadget.dev, dwc->dev->coherent_dma_mask);
2745
2746 dwc->gadget.dev.dma_parms = dwc->dev->dma_parms;
2747 dwc->gadget.dev.dma_mask = dwc->dev->dma_mask;
2748 dwc->gadget.dev.release = dwc3_gadget_release;
2749 dwc->gadget.name = "dwc3-gadget";
2750
2751 /*
2752 * REVISIT: Here we should clear all pending IRQs to be
2753 * sure we're starting from a well known location.
2754 */
2755
2756 ret = dwc3_gadget_init_endpoints(dwc);
2757 if (ret)
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002758 goto err4;
Felipe Balbi72246da2011-08-19 18:10:58 +03002759
2760 irq = platform_get_irq(to_platform_device(dwc->dev), 0);
2761
2762 ret = request_irq(irq, dwc3_interrupt, IRQF_SHARED,
2763 "dwc3", dwc);
2764 if (ret) {
2765 dev_err(dwc->dev, "failed to request irq #%d --> %d\n",
2766 irq, ret);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002767 goto err5;
Felipe Balbi72246da2011-08-19 18:10:58 +03002768 }
2769
Sebastian Andrzej Siewiorbb8b8a32011-09-13 17:54:39 +02002770 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2771 reg |= DWC3_DCFG_LPM_CAP;
2772 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
2773
Felipe Balbi72246da2011-08-19 18:10:58 +03002774 /* Enable all but Start and End of Frame IRQs */
Pavankumar Kondeti33fe6f12012-06-12 16:21:46 +05302775 reg = (DWC3_DEVTEN_EVNTOVERFLOWEN |
Felipe Balbi72246da2011-08-19 18:10:58 +03002776 DWC3_DEVTEN_CMDCMPLTEN |
2777 DWC3_DEVTEN_ERRTICERREN |
2778 DWC3_DEVTEN_WKUPEVTEN |
2779 DWC3_DEVTEN_ULSTCNGEN |
2780 DWC3_DEVTEN_CONNECTDONEEN |
2781 DWC3_DEVTEN_USBRSTEN |
2782 DWC3_DEVTEN_DISCONNEVTEN);
2783 dwc3_writel(dwc->regs, DWC3_DEVTEN, reg);
2784
Paul Zimmerman88df4272012-04-27 13:10:52 +03002785 /* Enable USB2 LPM and automatic phy suspend only on recent versions */
2786 if (dwc->revision >= DWC3_REVISION_194A) {
2787 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2788 reg |= DWC3_DCFG_LPM_CAP;
2789 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
2790
2791 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2792 reg &= ~(DWC3_DCTL_HIRD_THRES_MASK | DWC3_DCTL_L1_HIBER_EN);
2793
2794 /* TODO: This should be configurable */
Pratyush Anandd69dcdd2012-07-02 10:21:52 +05302795 reg |= DWC3_DCTL_HIRD_THRES(28);
Paul Zimmerman88df4272012-04-27 13:10:52 +03002796
2797 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2798
Pratyush Anand50ed8342012-06-06 19:36:17 +05302799 dwc3_gadget_usb2_phy_suspend(dwc, false);
2800 dwc3_gadget_usb3_phy_suspend(dwc, false);
Paul Zimmerman88df4272012-04-27 13:10:52 +03002801 }
2802
Felipe Balbi72246da2011-08-19 18:10:58 +03002803 ret = device_register(&dwc->gadget.dev);
2804 if (ret) {
2805 dev_err(dwc->dev, "failed to register gadget device\n");
2806 put_device(&dwc->gadget.dev);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002807 goto err6;
Felipe Balbi72246da2011-08-19 18:10:58 +03002808 }
2809
2810 ret = usb_add_gadget_udc(dwc->dev, &dwc->gadget);
2811 if (ret) {
2812 dev_err(dwc->dev, "failed to register udc\n");
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002813 goto err7;
Felipe Balbi72246da2011-08-19 18:10:58 +03002814 }
2815
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +02002816 if (dwc->dotg) {
2817 /* dwc3 otg driver is active (DRD mode + SRPSupport=1) */
2818 ret = otg_set_peripheral(&dwc->dotg->otg, &dwc->gadget);
2819 if (ret) {
2820 dev_err(dwc->dev, "failed to set peripheral to otg\n");
2821 goto err7;
2822 }
Manu Gautamb5067272012-07-02 09:53:41 +05302823 } else {
2824 pm_runtime_no_callbacks(&dwc->gadget.dev);
2825 pm_runtime_set_active(&dwc->gadget.dev);
2826 pm_runtime_enable(&dwc->gadget.dev);
2827 pm_runtime_get(&dwc->gadget.dev);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +02002828 }
2829
Felipe Balbi72246da2011-08-19 18:10:58 +03002830 return 0;
2831
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002832err7:
Felipe Balbi72246da2011-08-19 18:10:58 +03002833 device_unregister(&dwc->gadget.dev);
2834
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002835err6:
Felipe Balbi72246da2011-08-19 18:10:58 +03002836 dwc3_writel(dwc->regs, DWC3_DEVTEN, 0x00);
2837 free_irq(irq, dwc);
2838
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002839err5:
Felipe Balbi72246da2011-08-19 18:10:58 +03002840 dwc3_gadget_free_endpoints(dwc);
2841
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002842err4:
Felipe Balbib0791fb2012-05-04 12:58:14 +03002843 dma_free_coherent(dwc->dev, DWC3_EP0_BOUNCE_SIZE,
2844 dwc->ep0_bounce, dwc->ep0_bounce_addr);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002845
Felipe Balbi72246da2011-08-19 18:10:58 +03002846err3:
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02002847 kfree(dwc->setup_buf);
Felipe Balbi72246da2011-08-19 18:10:58 +03002848
2849err2:
2850 dma_free_coherent(dwc->dev, sizeof(*dwc->ep0_trb),
2851 dwc->ep0_trb, dwc->ep0_trb_addr);
2852
2853err1:
2854 dma_free_coherent(dwc->dev, sizeof(*dwc->ctrl_req),
2855 dwc->ctrl_req, dwc->ctrl_req_addr);
2856
2857err0:
2858 return ret;
2859}
2860
2861void dwc3_gadget_exit(struct dwc3 *dwc)
2862{
2863 int irq;
Felipe Balbi72246da2011-08-19 18:10:58 +03002864
Manu Gautamb5067272012-07-02 09:53:41 +05302865 if (dwc->dotg) {
2866 pm_runtime_put(&dwc->gadget.dev);
2867 pm_runtime_disable(&dwc->gadget.dev);
2868 }
2869
Felipe Balbi72246da2011-08-19 18:10:58 +03002870 usb_del_gadget_udc(&dwc->gadget);
2871 irq = platform_get_irq(to_platform_device(dwc->dev), 0);
2872
2873 dwc3_writel(dwc->regs, DWC3_DEVTEN, 0x00);
2874 free_irq(irq, dwc);
2875
Felipe Balbi72246da2011-08-19 18:10:58 +03002876 dwc3_gadget_free_endpoints(dwc);
2877
Felipe Balbib0791fb2012-05-04 12:58:14 +03002878 dma_free_coherent(dwc->dev, DWC3_EP0_BOUNCE_SIZE,
2879 dwc->ep0_bounce, dwc->ep0_bounce_addr);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002880
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02002881 kfree(dwc->setup_buf);
Felipe Balbi72246da2011-08-19 18:10:58 +03002882
2883 dma_free_coherent(dwc->dev, sizeof(*dwc->ep0_trb),
2884 dwc->ep0_trb, dwc->ep0_trb_addr);
2885
2886 dma_free_coherent(dwc->dev, sizeof(*dwc->ctrl_req),
2887 dwc->ctrl_req, dwc->ctrl_req_addr);
2888
2889 device_unregister(&dwc->gadget.dev);
2890}