blob: acda980f0abdff0298cf19ab438897b2291ece9c [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>
Wesley Cheng446ad8d2013-06-05 16:15:01 +053046#include <linux/ratelimit.h>
Felipe Balbi72246da2011-08-19 18:10:58 +030047#include <linux/interrupt.h>
48#include <linux/io.h>
49#include <linux/list.h>
50#include <linux/dma-mapping.h>
51
52#include <linux/usb/ch9.h>
53#include <linux/usb/gadget.h>
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +020054#include <linux/usb/otg.h>
Felipe Balbi72246da2011-08-19 18:10:58 +030055
56#include "core.h"
57#include "gadget.h"
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +053058#include "debug.h"
Felipe Balbi72246da2011-08-19 18:10:58 +030059#include "io.h"
60
Vijayavardhan Vennapusadbe59bf2013-06-07 15:09:36 +053061static bool tx_fifo_resize_enable;
62module_param(tx_fifo_resize_enable, bool, S_IRUGO|S_IWUSR);
63MODULE_PARM_DESC(tx_fifo_resize_enable,
64 "Enable allocating Tx fifo for endpoints");
65
Felipe Balbi04a9bfc2012-01-02 18:25:43 +020066/**
67 * dwc3_gadget_set_test_mode - Enables USB2 Test Modes
68 * @dwc: pointer to our context structure
69 * @mode: the mode to set (J, K SE0 NAK, Force Enable)
70 *
71 * Caller should take care of locking. This function will
72 * return 0 on success or -EINVAL if wrong Test Selector
73 * is passed
74 */
75int dwc3_gadget_set_test_mode(struct dwc3 *dwc, int mode)
76{
77 u32 reg;
78
79 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
80 reg &= ~DWC3_DCTL_TSTCTRL_MASK;
81
82 switch (mode) {
83 case TEST_J:
84 case TEST_K:
85 case TEST_SE0_NAK:
86 case TEST_PACKET:
87 case TEST_FORCE_EN:
88 reg |= mode << 1;
89 break;
90 default:
91 return -EINVAL;
92 }
93
94 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
95
96 return 0;
97}
98
Felipe Balbi8598bde2012-01-02 18:55:57 +020099/**
100 * dwc3_gadget_set_link_state - Sets USB Link to a particular State
101 * @dwc: pointer to our context structure
102 * @state: the state to put link into
103 *
104 * Caller should take care of locking. This function will
Paul Zimmermanaee63e32012-02-24 17:32:15 -0800105 * return 0 on success or -ETIMEDOUT.
Felipe Balbi8598bde2012-01-02 18:55:57 +0200106 */
107int dwc3_gadget_set_link_state(struct dwc3 *dwc, enum dwc3_link_state state)
108{
Paul Zimmermanaee63e32012-02-24 17:32:15 -0800109 int retries = 10000;
Felipe Balbi8598bde2012-01-02 18:55:57 +0200110 u32 reg;
111
Paul Zimmerman88df4272012-04-27 13:10:52 +0300112 /*
113 * Wait until device controller is ready. Only applies to 1.94a and
114 * later RTL.
115 */
116 if (dwc->revision >= DWC3_REVISION_194A) {
117 while (--retries) {
118 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
119 if (reg & DWC3_DSTS_DCNRD)
120 udelay(5);
121 else
122 break;
123 }
124
125 if (retries <= 0)
126 return -ETIMEDOUT;
127 }
128
Felipe Balbi8598bde2012-01-02 18:55:57 +0200129 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
130 reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK;
131
132 /* set requested state */
133 reg |= DWC3_DCTL_ULSTCHNGREQ(state);
134 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
135
Paul Zimmerman88df4272012-04-27 13:10:52 +0300136 /*
137 * The following code is racy when called from dwc3_gadget_wakeup,
138 * and is not needed, at least on newer versions
139 */
140 if (dwc->revision >= DWC3_REVISION_194A)
141 return 0;
142
Felipe Balbi8598bde2012-01-02 18:55:57 +0200143 /* wait for a change in DSTS */
Paul Zimmerman8b9388f2012-04-27 12:52:01 +0300144 retries = 10000;
Felipe Balbi8598bde2012-01-02 18:55:57 +0200145 while (--retries) {
146 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
147
Felipe Balbi8598bde2012-01-02 18:55:57 +0200148 if (DWC3_DSTS_USBLNKST(reg) == state)
149 return 0;
150
Paul Zimmermanaee63e32012-02-24 17:32:15 -0800151 udelay(5);
Felipe Balbi8598bde2012-01-02 18:55:57 +0200152 }
153
154 dev_vdbg(dwc->dev, "link state change request timed out\n");
155
156 return -ETIMEDOUT;
157}
158
Felipe Balbi457e84b2012-01-18 18:04:09 +0200159/**
160 * dwc3_gadget_resize_tx_fifos - reallocate fifo spaces for current use-case
161 * @dwc: pointer to our context structure
162 *
163 * This function will a best effort FIFO allocation in order
164 * to improve FIFO usage and throughput, while still allowing
165 * us to enable as many endpoints as possible.
166 *
167 * Keep in mind that this operation will be highly dependent
168 * on the configured size for RAM1 - which contains TxFifo -,
169 * the amount of endpoints enabled on coreConsultant tool, and
170 * the width of the Master Bus.
171 *
172 * In the ideal world, we would always be able to satisfy the
173 * following equation:
174 *
175 * ((512 + 2 * MDWIDTH-Bytes) + (Number of IN Endpoints - 1) * \
176 * (3 * (1024 + MDWIDTH-Bytes) + MDWIDTH-Bytes)) / MDWIDTH-Bytes
177 *
178 * Unfortunately, due to many variables that's not always the case.
179 */
180int dwc3_gadget_resize_tx_fifos(struct dwc3 *dwc)
181{
182 int last_fifo_depth = 0;
183 int ram1_depth;
184 int fifo_size;
185 int mdwidth;
186 int num;
187
Vijayavardhan Vennapusadbe59bf2013-06-07 15:09:36 +0530188 if (!dwc->needs_fifo_resize && !tx_fifo_resize_enable)
Felipe Balbi457e84b2012-01-18 18:04:09 +0200189 return 0;
190
191 ram1_depth = DWC3_RAM1_DEPTH(dwc->hwparams.hwparams7);
192 mdwidth = DWC3_MDWIDTH(dwc->hwparams.hwparams0);
193
194 /* MDWIDTH is represented in bits, we need it in bytes */
195 mdwidth >>= 3;
196
197 /*
198 * FIXME For now we will only allocate 1 wMaxPacketSize space
199 * for each enabled endpoint, later patches will come to
200 * improve this algorithm so that we better use the internal
Vijayavardhan Vennapusadaf082c2013-03-01 13:08:59 +0530201 * FIFO space. Also consider the case where TxFIFO RAM space
202 * may change dynamically based on the USB configuration.
Felipe Balbi457e84b2012-01-18 18:04:09 +0200203 */
204 for (num = 0; num < DWC3_ENDPOINTS_NUM; num++) {
205 struct dwc3_ep *dep = dwc->eps[num];
206 int fifo_number = dep->number >> 1;
Felipe Balbi2e81c362012-02-02 13:01:12 +0200207 int mult = 1;
Felipe Balbi457e84b2012-01-18 18:04:09 +0200208 int tmp;
209
210 if (!(dep->number & 1))
211 continue;
212
213 if (!(dep->flags & DWC3_EP_ENABLED))
214 continue;
215
Vijayavardhan Vennapusadaf082c2013-03-01 13:08:59 +0530216 if (((dep->endpoint.maxburst > 1) &&
217 usb_endpoint_xfer_bulk(dep->endpoint.desc))
Ido Shayevitz57cdac12012-03-12 20:25:24 +0200218 || usb_endpoint_xfer_isoc(dep->endpoint.desc))
Felipe Balbi2e81c362012-02-02 13:01:12 +0200219 mult = 3;
220
221 /*
222 * REVISIT: the following assumes we will always have enough
223 * space available on the FIFO RAM for all possible use cases.
224 * Make sure that's true somehow and change FIFO allocation
225 * accordingly.
226 *
Vijayavardhan Vennapusadaf082c2013-03-01 13:08:59 +0530227 * If we have Bulk (burst only) or Isochronous endpoints, we
228 * want them to be able to be very, very fast. So we're giving
Felipe Balbi2e81c362012-02-02 13:01:12 +0200229 * those endpoints a fifo_size which is enough for 3 full
230 * packets
231 */
232 tmp = mult * (dep->endpoint.maxpacket + mdwidth);
Felipe Balbi457e84b2012-01-18 18:04:09 +0200233 tmp += mdwidth;
234
235 fifo_size = DIV_ROUND_UP(tmp, mdwidth);
Felipe Balbi2e81c362012-02-02 13:01:12 +0200236
Felipe Balbi457e84b2012-01-18 18:04:09 +0200237 fifo_size |= (last_fifo_depth << 16);
238
239 dev_vdbg(dwc->dev, "%s: Fifo Addr %04x Size %d\n",
240 dep->name, last_fifo_depth, fifo_size & 0xffff);
241
242 dwc3_writel(dwc->regs, DWC3_GTXFIFOSIZ(fifo_number),
243 fifo_size);
244
245 last_fifo_depth += (fifo_size & 0xffff);
246 }
247
248 return 0;
249}
250
Felipe Balbi72246da2011-08-19 18:10:58 +0300251void dwc3_gadget_giveback(struct dwc3_ep *dep, struct dwc3_request *req,
252 int status)
253{
254 struct dwc3 *dwc = dep->dwc;
255
256 if (req->queued) {
Manu Gautam55d34222012-12-19 16:49:47 +0530257 req->queued = false;
258
Felipe Balbieeb720f2011-11-28 12:46:59 +0200259 if (req->request.num_mapped_sgs)
260 dep->busy_slot += req->request.num_mapped_sgs;
261 else
262 dep->busy_slot++;
263
Felipe Balbi72246da2011-08-19 18:10:58 +0300264 /*
265 * Skip LINK TRB. We can't use req->trb and check for
266 * DWC3_TRBCTL_LINK_TRB because it points the TRB we just
267 * completed (not the LINK TRB).
268 */
269 if (((dep->busy_slot & DWC3_TRB_MASK) == DWC3_TRB_NUM - 1) &&
Ido Shayevitz57cdac12012-03-12 20:25:24 +0200270 usb_endpoint_xfer_isoc(dep->endpoint.desc))
Felipe Balbi72246da2011-08-19 18:10:58 +0300271 dep->busy_slot++;
272 }
273 list_del(&req->list);
Felipe Balbieeb720f2011-11-28 12:46:59 +0200274 req->trb = NULL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300275
276 if (req->request.status == -EINPROGRESS)
277 req->request.status = status;
278
Pratyush Anand8d7bf592012-08-10 13:42:16 +0530279 if (dwc->ep0_bounced && dep->number == 0)
280 dwc->ep0_bounced = false;
281 else
282 usb_gadget_unmap_request(&dwc->gadget, &req->request,
283 req->direction);
Felipe Balbi72246da2011-08-19 18:10:58 +0300284
285 dev_dbg(dwc->dev, "request %p from %s completed %d/%d ===> %d\n",
286 req, dep->name, req->request.actual,
287 req->request.length, status);
288
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +0530289 dbg_done(dep->number, req->request.actual, req->request.status);
Felipe Balbi72246da2011-08-19 18:10:58 +0300290 spin_unlock(&dwc->lock);
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +0200291 req->request.complete(&dep->endpoint, &req->request);
Felipe Balbi72246da2011-08-19 18:10:58 +0300292 spin_lock(&dwc->lock);
293}
294
295static const char *dwc3_gadget_ep_cmd_string(u8 cmd)
296{
297 switch (cmd) {
298 case DWC3_DEPCMD_DEPSTARTCFG:
299 return "Start New Configuration";
300 case DWC3_DEPCMD_ENDTRANSFER:
301 return "End Transfer";
302 case DWC3_DEPCMD_UPDATETRANSFER:
303 return "Update Transfer";
304 case DWC3_DEPCMD_STARTTRANSFER:
305 return "Start Transfer";
306 case DWC3_DEPCMD_CLEARSTALL:
307 return "Clear Stall";
308 case DWC3_DEPCMD_SETSTALL:
309 return "Set Stall";
Paul Zimmerman88df4272012-04-27 13:10:52 +0300310 case DWC3_DEPCMD_GETEPSTATE:
311 return "Get Endpoint State";
Felipe Balbi72246da2011-08-19 18:10:58 +0300312 case DWC3_DEPCMD_SETTRANSFRESOURCE:
313 return "Set Endpoint Transfer Resource";
314 case DWC3_DEPCMD_SETEPCONFIG:
315 return "Set Endpoint Configuration";
316 default:
317 return "UNKNOWN command";
318 }
319}
320
Felipe Balbi573c2762012-04-24 16:19:11 +0300321int dwc3_send_gadget_generic_command(struct dwc3 *dwc, int cmd, u32 param)
322{
323 u32 timeout = 500;
324 u32 reg;
325
326 dwc3_writel(dwc->regs, DWC3_DGCMDPAR, param);
327 dwc3_writel(dwc->regs, DWC3_DGCMD, cmd | DWC3_DGCMD_CMDACT);
328
329 do {
330 reg = dwc3_readl(dwc->regs, DWC3_DGCMD);
331 if (!(reg & DWC3_DGCMD_CMDACT)) {
332 dev_vdbg(dwc->dev, "Command Complete --> %d\n",
333 DWC3_DGCMD_STATUS(reg));
334 return 0;
335 }
336
337 /*
338 * We can't sleep here, because it's also called from
339 * interrupt context.
340 */
341 timeout--;
342 if (!timeout)
343 return -ETIMEDOUT;
344 udelay(1);
345 } while (1);
346}
347
Felipe Balbi72246da2011-08-19 18:10:58 +0300348int dwc3_send_gadget_ep_cmd(struct dwc3 *dwc, unsigned ep,
349 unsigned cmd, struct dwc3_gadget_ep_cmd_params *params)
350{
351 struct dwc3_ep *dep = dwc->eps[ep];
Sebastian Andrzej Siewior61d58242011-08-29 16:46:38 +0200352 u32 timeout = 500;
Felipe Balbi72246da2011-08-19 18:10:58 +0300353 u32 reg;
354
355 dev_vdbg(dwc->dev, "%s: cmd '%s' params %08x %08x %08x\n",
356 dep->name,
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300357 dwc3_gadget_ep_cmd_string(cmd), params->param0,
358 params->param1, params->param2);
Felipe Balbi72246da2011-08-19 18:10:58 +0300359
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300360 dwc3_writel(dwc->regs, DWC3_DEPCMDPAR0(ep), params->param0);
361 dwc3_writel(dwc->regs, DWC3_DEPCMDPAR1(ep), params->param1);
362 dwc3_writel(dwc->regs, DWC3_DEPCMDPAR2(ep), params->param2);
Felipe Balbi72246da2011-08-19 18:10:58 +0300363
364 dwc3_writel(dwc->regs, DWC3_DEPCMD(ep), cmd | DWC3_DEPCMD_CMDACT);
365 do {
366 reg = dwc3_readl(dwc->regs, DWC3_DEPCMD(ep));
367 if (!(reg & DWC3_DEPCMD_CMDACT)) {
Felipe Balbi164f6e12011-08-27 20:29:58 +0300368 dev_vdbg(dwc->dev, "Command Complete --> %d\n",
369 DWC3_DEPCMD_STATUS(reg));
Vijayavardhan Vennapusa91ba6532013-01-30 17:35:45 +0530370 /* SW issues START TRANSFER command to isochronous ep
371 * with future frame interval. If future interval time
372 * has already passed when core recieves command, core
373 * will respond with an error(bit13 in Command complete
374 * event. Hence return error in this case.
375 */
376 if (reg & 0x2000)
377 return -EAGAIN;
378 else
379 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300380 }
381
382 /*
Felipe Balbi72246da2011-08-19 18:10:58 +0300383 * We can't sleep here, because it is also called from
384 * interrupt context.
385 */
386 timeout--;
387 if (!timeout)
388 return -ETIMEDOUT;
389
Sebastian Andrzej Siewior61d58242011-08-29 16:46:38 +0200390 udelay(1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300391 } while (1);
392}
393
Ido Shayevitzfa65a582012-06-06 14:39:54 +0300394dma_addr_t dwc3_trb_dma_offset(struct dwc3_ep *dep,
Felipe Balbif6bafc62012-02-06 11:04:53 +0200395 struct dwc3_trb *trb)
Felipe Balbi72246da2011-08-19 18:10:58 +0300396{
Paul Zimmermanc439ef82011-09-30 10:58:45 +0300397 u32 offset = (char *) trb - (char *) dep->trb_pool;
Felipe Balbi72246da2011-08-19 18:10:58 +0300398
399 return dep->trb_pool_dma + offset;
400}
401
402static int dwc3_alloc_trb_pool(struct dwc3_ep *dep)
403{
404 struct dwc3 *dwc = dep->dwc;
405
406 if (dep->trb_pool)
407 return 0;
408
409 if (dep->number == 0 || dep->number == 1)
410 return 0;
411
412 dep->trb_pool = dma_alloc_coherent(dwc->dev,
413 sizeof(struct dwc3_trb) * DWC3_TRB_NUM,
414 &dep->trb_pool_dma, GFP_KERNEL);
415 if (!dep->trb_pool) {
416 dev_err(dep->dwc->dev, "failed to allocate trb pool for %s\n",
417 dep->name);
418 return -ENOMEM;
419 }
420
421 return 0;
422}
423
424static void dwc3_free_trb_pool(struct dwc3_ep *dep)
425{
426 struct dwc3 *dwc = dep->dwc;
427
428 dma_free_coherent(dwc->dev, sizeof(struct dwc3_trb) * DWC3_TRB_NUM,
429 dep->trb_pool, dep->trb_pool_dma);
430
431 dep->trb_pool = NULL;
432 dep->trb_pool_dma = 0;
433}
434
435static int dwc3_gadget_start_config(struct dwc3 *dwc, struct dwc3_ep *dep)
436{
437 struct dwc3_gadget_ep_cmd_params params;
438 u32 cmd;
439
440 memset(&params, 0x00, sizeof(params));
441
442 if (dep->number != 1) {
443 cmd = DWC3_DEPCMD_DEPSTARTCFG;
444 /* XferRscIdx == 0 for ep0 and 2 for the remaining */
Paul Zimmermanb23c8432011-09-30 10:58:42 +0300445 if (dep->number > 1) {
446 if (dwc->start_config_issued)
447 return 0;
448 dwc->start_config_issued = true;
Felipe Balbi72246da2011-08-19 18:10:58 +0300449 cmd |= DWC3_DEPCMD_PARAM(2);
Paul Zimmermanb23c8432011-09-30 10:58:42 +0300450 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300451
452 return dwc3_send_gadget_ep_cmd(dwc, 0, cmd, &params);
453 }
454
455 return 0;
456}
457
458static int dwc3_gadget_set_ep_config(struct dwc3 *dwc, struct dwc3_ep *dep,
Felipe Balbic90bfae2011-11-29 13:11:21 +0200459 const struct usb_endpoint_descriptor *desc,
Felipe Balbi07e0ee82012-07-16 14:08:16 +0300460 const struct usb_ss_ep_comp_descriptor *comp_desc,
461 bool ignore)
Felipe Balbi72246da2011-08-19 18:10:58 +0300462{
463 struct dwc3_gadget_ep_cmd_params params;
464
465 memset(&params, 0x00, sizeof(params));
466
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300467 params.param0 = DWC3_DEPCFG_EP_TYPE(usb_endpoint_type(desc))
Chanho Parkf0ee6062012-08-31 16:54:07 +0900468 | DWC3_DEPCFG_MAX_PACKET_SIZE(usb_endpoint_maxp(desc));
469
470 /* Burst size is only needed in SuperSpeed mode */
471 if (dwc->gadget.speed == USB_SPEED_SUPER) {
472 u32 burst = dep->endpoint.maxburst - 1;
473
474 params.param0 |= DWC3_DEPCFG_BURST_SIZE(burst);
475 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300476
Felipe Balbi07e0ee82012-07-16 14:08:16 +0300477 if (ignore)
478 params.param0 |= DWC3_DEPCFG_IGN_SEQ_NUM;
Felipe Balbi72246da2011-08-19 18:10:58 +0300479
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300480 params.param1 = DWC3_DEPCFG_XFER_COMPLETE_EN
481 | DWC3_DEPCFG_XFER_NOT_READY_EN;
Felipe Balbi72246da2011-08-19 18:10:58 +0300482
Felipe Balbi18b7ede2012-01-02 13:35:41 +0200483 if (usb_ss_max_streams(comp_desc) && usb_endpoint_xfer_bulk(desc)) {
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300484 params.param1 |= DWC3_DEPCFG_STREAM_CAPABLE
485 | DWC3_DEPCFG_STREAM_EVENT_EN;
Felipe Balbi879631a2011-09-30 10:58:47 +0300486 dep->stream_capable = true;
487 }
488
Felipe Balbi72246da2011-08-19 18:10:58 +0300489 if (usb_endpoint_xfer_isoc(desc))
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300490 params.param1 |= DWC3_DEPCFG_XFER_IN_PROGRESS_EN;
Felipe Balbi72246da2011-08-19 18:10:58 +0300491
492 /*
493 * We are doing 1:1 mapping for endpoints, meaning
494 * Physical Endpoints 2 maps to Logical Endpoint 2 and
495 * so on. We consider the direction bit as part of the physical
496 * endpoint number. So USB endpoint 0x81 is 0x03.
497 */
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300498 params.param1 |= DWC3_DEPCFG_EP_NUMBER(dep->number);
Felipe Balbi72246da2011-08-19 18:10:58 +0300499
500 /*
501 * We must use the lower 16 TX FIFOs even though
502 * HW might have more
503 */
504 if (dep->direction)
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300505 params.param0 |= DWC3_DEPCFG_FIFO_NUMBER(dep->number >> 1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300506
507 if (desc->bInterval) {
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300508 params.param1 |= DWC3_DEPCFG_BINTERVAL_M1(desc->bInterval - 1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300509 dep->interval = 1 << (desc->bInterval - 1);
510 }
511
512 return dwc3_send_gadget_ep_cmd(dwc, dep->number,
513 DWC3_DEPCMD_SETEPCONFIG, &params);
514}
515
516static int dwc3_gadget_set_xfer_resource(struct dwc3 *dwc, struct dwc3_ep *dep)
517{
518 struct dwc3_gadget_ep_cmd_params params;
519
520 memset(&params, 0x00, sizeof(params));
521
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300522 params.param0 = DWC3_DEPXFERCFG_NUM_XFER_RES(1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300523
524 return dwc3_send_gadget_ep_cmd(dwc, dep->number,
525 DWC3_DEPCMD_SETTRANSFRESOURCE, &params);
526}
527
528/**
529 * __dwc3_gadget_ep_enable - Initializes a HW endpoint
530 * @dep: endpoint to be initialized
531 * @desc: USB Endpoint Descriptor
532 *
533 * Caller should take care of locking
534 */
535static int __dwc3_gadget_ep_enable(struct dwc3_ep *dep,
Felipe Balbic90bfae2011-11-29 13:11:21 +0200536 const struct usb_endpoint_descriptor *desc,
Felipe Balbi07e0ee82012-07-16 14:08:16 +0300537 const struct usb_ss_ep_comp_descriptor *comp_desc,
538 bool ignore)
Felipe Balbi72246da2011-08-19 18:10:58 +0300539{
540 struct dwc3 *dwc = dep->dwc;
541 u32 reg;
542 int ret = -ENOMEM;
543
544 if (!(dep->flags & DWC3_EP_ENABLED)) {
545 ret = dwc3_gadget_start_config(dwc, dep);
546 if (ret)
547 return ret;
548 }
549
Felipe Balbi07e0ee82012-07-16 14:08:16 +0300550 ret = dwc3_gadget_set_ep_config(dwc, dep, desc, comp_desc, ignore);
Felipe Balbi72246da2011-08-19 18:10:58 +0300551 if (ret)
552 return ret;
553
554 if (!(dep->flags & DWC3_EP_ENABLED)) {
Felipe Balbif6bafc62012-02-06 11:04:53 +0200555 struct dwc3_trb *trb_st_hw;
556 struct dwc3_trb *trb_link;
Felipe Balbi72246da2011-08-19 18:10:58 +0300557
558 ret = dwc3_gadget_set_xfer_resource(dwc, dep);
559 if (ret)
560 return ret;
561
Ido Shayevitz57cdac12012-03-12 20:25:24 +0200562 dep->endpoint.desc = desc;
Felipe Balbic90bfae2011-11-29 13:11:21 +0200563 dep->comp_desc = comp_desc;
Felipe Balbi72246da2011-08-19 18:10:58 +0300564 dep->type = usb_endpoint_type(desc);
565 dep->flags |= DWC3_EP_ENABLED;
566
567 reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
568 reg |= DWC3_DALEPENA_EP(dep->number);
569 dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
570
571 if (!usb_endpoint_xfer_isoc(desc))
572 return 0;
573
574 memset(&trb_link, 0, sizeof(trb_link));
575
Paul Zimmerman1d046792012-02-15 18:56:56 -0800576 /* Link TRB for ISOC. The HWO bit is never reset */
Felipe Balbi72246da2011-08-19 18:10:58 +0300577 trb_st_hw = &dep->trb_pool[0];
578
Felipe Balbif6bafc62012-02-06 11:04:53 +0200579 trb_link = &dep->trb_pool[DWC3_TRB_NUM - 1];
Felipe Balbi72246da2011-08-19 18:10:58 +0300580
Felipe Balbif6bafc62012-02-06 11:04:53 +0200581 trb_link->bpl = lower_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw));
582 trb_link->bph = upper_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw));
583 trb_link->ctrl |= DWC3_TRBCTL_LINK_TRB;
584 trb_link->ctrl |= DWC3_TRB_CTRL_HWO;
Felipe Balbi72246da2011-08-19 18:10:58 +0300585 }
586
587 return 0;
588}
589
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200590static void dwc3_stop_active_transfer(struct dwc3 *dwc, u32 epnum);
591static void dwc3_remove_requests(struct dwc3 *dwc, struct dwc3_ep *dep)
Felipe Balbi72246da2011-08-19 18:10:58 +0300592{
593 struct dwc3_request *req;
594
Felipe Balbib129eb72012-02-17 12:10:04 +0200595 if (!list_empty(&dep->req_queued)) {
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200596 dwc3_stop_active_transfer(dwc, dep->number);
597
Pratyush Anande67fdeb2012-07-06 15:19:10 +0530598 /* - giveback all requests to gadget driver */
Pratyush Anand110ff602012-06-15 11:54:36 +0530599 while (!list_empty(&dep->req_queued)) {
600 req = next_request(&dep->req_queued);
601
602 dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
603 }
Felipe Balbib129eb72012-02-17 12:10:04 +0200604 }
605
Felipe Balbi72246da2011-08-19 18:10:58 +0300606 while (!list_empty(&dep->request_list)) {
607 req = next_request(&dep->request_list);
608
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200609 dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
Felipe Balbi72246da2011-08-19 18:10:58 +0300610 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300611}
612
613/**
614 * __dwc3_gadget_ep_disable - Disables a HW endpoint
615 * @dep: the endpoint to disable
616 *
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200617 * This function also removes requests which are currently processed ny the
618 * hardware and those which are not yet scheduled.
619 * Caller should take care of locking.
Felipe Balbi72246da2011-08-19 18:10:58 +0300620 */
Felipe Balbi72246da2011-08-19 18:10:58 +0300621static int __dwc3_gadget_ep_disable(struct dwc3_ep *dep)
622{
623 struct dwc3 *dwc = dep->dwc;
624 u32 reg;
625
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200626 dwc3_remove_requests(dwc, dep);
Felipe Balbi72246da2011-08-19 18:10:58 +0300627
628 reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
629 reg &= ~DWC3_DALEPENA_EP(dep->number);
630 dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
631
Felipe Balbi879631a2011-09-30 10:58:47 +0300632 dep->stream_capable = false;
Ido Shayevitzf9c56cd2012-02-08 13:56:48 +0200633 dep->endpoint.desc = NULL;
Felipe Balbic90bfae2011-11-29 13:11:21 +0200634 dep->comp_desc = NULL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300635 dep->type = 0;
Felipe Balbi879631a2011-09-30 10:58:47 +0300636 dep->flags = 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300637
638 return 0;
639}
640
641/* -------------------------------------------------------------------------- */
642
643static int dwc3_gadget_ep0_enable(struct usb_ep *ep,
644 const struct usb_endpoint_descriptor *desc)
645{
646 return -EINVAL;
647}
648
649static int dwc3_gadget_ep0_disable(struct usb_ep *ep)
650{
651 return -EINVAL;
652}
653
654/* -------------------------------------------------------------------------- */
655
656static int dwc3_gadget_ep_enable(struct usb_ep *ep,
657 const struct usb_endpoint_descriptor *desc)
658{
659 struct dwc3_ep *dep;
660 struct dwc3 *dwc;
661 unsigned long flags;
662 int ret;
663
664 if (!ep || !desc || desc->bDescriptorType != USB_DT_ENDPOINT) {
665 pr_debug("dwc3: invalid parameters\n");
666 return -EINVAL;
667 }
668
669 if (!desc->wMaxPacketSize) {
670 pr_debug("dwc3: missing wMaxPacketSize\n");
671 return -EINVAL;
672 }
673
674 dep = to_dwc3_ep(ep);
675 dwc = dep->dwc;
676
Felipe Balbi14395072012-08-15 12:28:29 +0300677 if (dep->flags & DWC3_EP_ENABLED) {
678 dev_WARN_ONCE(dwc->dev, true, "%s is already enabled\n",
679 dep->name);
680 return 0;
681 }
682
Felipe Balbi72246da2011-08-19 18:10:58 +0300683 switch (usb_endpoint_type(desc)) {
684 case USB_ENDPOINT_XFER_CONTROL:
Anton Tikhomirov27a78d62012-02-23 15:38:46 +0900685 strlcat(dep->name, "-control", sizeof(dep->name));
Felipe Balbi72246da2011-08-19 18:10:58 +0300686 break;
687 case USB_ENDPOINT_XFER_ISOC:
Anton Tikhomirov27a78d62012-02-23 15:38:46 +0900688 strlcat(dep->name, "-isoc", sizeof(dep->name));
Felipe Balbi72246da2011-08-19 18:10:58 +0300689 break;
690 case USB_ENDPOINT_XFER_BULK:
Anton Tikhomirov27a78d62012-02-23 15:38:46 +0900691 strlcat(dep->name, "-bulk", sizeof(dep->name));
Felipe Balbi72246da2011-08-19 18:10:58 +0300692 break;
693 case USB_ENDPOINT_XFER_INT:
Anton Tikhomirov27a78d62012-02-23 15:38:46 +0900694 strlcat(dep->name, "-int", sizeof(dep->name));
Felipe Balbi72246da2011-08-19 18:10:58 +0300695 break;
696 default:
697 dev_err(dwc->dev, "invalid endpoint transfer type\n");
698 }
699
Felipe Balbi72246da2011-08-19 18:10:58 +0300700 dev_vdbg(dwc->dev, "Enabling %s\n", dep->name);
701
702 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi07e0ee82012-07-16 14:08:16 +0300703 ret = __dwc3_gadget_ep_enable(dep, desc, ep->comp_desc, false);
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +0530704 dbg_event(dep->number, "ENABLE", ret);
Felipe Balbi72246da2011-08-19 18:10:58 +0300705 spin_unlock_irqrestore(&dwc->lock, flags);
706
707 return ret;
708}
709
710static int dwc3_gadget_ep_disable(struct usb_ep *ep)
711{
712 struct dwc3_ep *dep;
713 struct dwc3 *dwc;
714 unsigned long flags;
715 int ret;
716
717 if (!ep) {
718 pr_debug("dwc3: invalid parameters\n");
719 return -EINVAL;
720 }
721
722 dep = to_dwc3_ep(ep);
723 dwc = dep->dwc;
724
725 if (!(dep->flags & DWC3_EP_ENABLED)) {
726 dev_WARN_ONCE(dwc->dev, true, "%s is already disabled\n",
727 dep->name);
728 return 0;
729 }
730
731 snprintf(dep->name, sizeof(dep->name), "ep%d%s",
732 dep->number >> 1,
733 (dep->number & 1) ? "in" : "out");
734
735 spin_lock_irqsave(&dwc->lock, flags);
736 ret = __dwc3_gadget_ep_disable(dep);
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +0530737 dbg_event(dep->number, "DISABLE", ret);
Felipe Balbi72246da2011-08-19 18:10:58 +0300738 spin_unlock_irqrestore(&dwc->lock, flags);
739
740 return ret;
741}
742
743static struct usb_request *dwc3_gadget_ep_alloc_request(struct usb_ep *ep,
744 gfp_t gfp_flags)
745{
746 struct dwc3_request *req;
747 struct dwc3_ep *dep = to_dwc3_ep(ep);
748 struct dwc3 *dwc = dep->dwc;
749
750 req = kzalloc(sizeof(*req), gfp_flags);
751 if (!req) {
752 dev_err(dwc->dev, "not enough memory\n");
753 return NULL;
754 }
755
756 req->epnum = dep->number;
757 req->dep = dep;
Felipe Balbi72246da2011-08-19 18:10:58 +0300758
759 return &req->request;
760}
761
762static void dwc3_gadget_ep_free_request(struct usb_ep *ep,
763 struct usb_request *request)
764{
765 struct dwc3_request *req = to_dwc3_request(request);
766
767 kfree(req);
768}
769
Felipe Balbic71fc372011-11-22 11:37:34 +0200770/**
771 * dwc3_prepare_one_trb - setup one TRB from one request
772 * @dep: endpoint for which this request is prepared
773 * @req: dwc3_request pointer
774 */
Felipe Balbi68e823e2011-11-28 12:25:01 +0200775static void dwc3_prepare_one_trb(struct dwc3_ep *dep,
Felipe Balbieeb720f2011-11-28 12:46:59 +0200776 struct dwc3_request *req, dma_addr_t dma,
777 unsigned length, unsigned last, unsigned chain)
Felipe Balbic71fc372011-11-22 11:37:34 +0200778{
Felipe Balbieeb720f2011-11-28 12:46:59 +0200779 struct dwc3 *dwc = dep->dwc;
Felipe Balbif6bafc62012-02-06 11:04:53 +0200780 struct dwc3_trb *trb;
Felipe Balbic71fc372011-11-22 11:37:34 +0200781
782 unsigned int cur_slot;
783
Felipe Balbieeb720f2011-11-28 12:46:59 +0200784 dev_vdbg(dwc->dev, "%s: req %p dma %08llx length %d%s%s\n",
785 dep->name, req, (unsigned long long) dma,
786 length, last ? " last" : "",
787 chain ? " chain" : "");
788
Felipe Balbif6bafc62012-02-06 11:04:53 +0200789 trb = &dep->trb_pool[dep->free_slot & DWC3_TRB_MASK];
Felipe Balbic71fc372011-11-22 11:37:34 +0200790 cur_slot = dep->free_slot;
791 dep->free_slot++;
792
793 /* Skip the LINK-TRB on ISOC */
Vijayavardhan Vennapusa2a444ad2013-02-01 13:20:59 +0530794 if (((dep->free_slot & DWC3_TRB_MASK) == DWC3_TRB_NUM - 1) &&
Ido Shayevitz57cdac12012-03-12 20:25:24 +0200795 usb_endpoint_xfer_isoc(dep->endpoint.desc))
Vijayavardhan Vennapusa2a444ad2013-02-01 13:20:59 +0530796 dep->free_slot++;
Felipe Balbic71fc372011-11-22 11:37:34 +0200797
Felipe Balbieeb720f2011-11-28 12:46:59 +0200798 if (!req->trb) {
799 dwc3_gadget_move_request_queued(req);
Felipe Balbif6bafc62012-02-06 11:04:53 +0200800 req->trb = trb;
801 req->trb_dma = dwc3_trb_dma_offset(dep, trb);
Felipe Balbieeb720f2011-11-28 12:46:59 +0200802 }
Felipe Balbic71fc372011-11-22 11:37:34 +0200803
Felipe Balbif6bafc62012-02-06 11:04:53 +0200804 trb->size = DWC3_TRB_SIZE_LENGTH(length);
805 trb->bpl = lower_32_bits(dma);
806 trb->bph = upper_32_bits(dma);
Felipe Balbic71fc372011-11-22 11:37:34 +0200807
Ido Shayevitz57cdac12012-03-12 20:25:24 +0200808 switch (usb_endpoint_type(dep->endpoint.desc)) {
Felipe Balbic71fc372011-11-22 11:37:34 +0200809 case USB_ENDPOINT_XFER_CONTROL:
Felipe Balbif6bafc62012-02-06 11:04:53 +0200810 trb->ctrl = DWC3_TRBCTL_CONTROL_SETUP;
Felipe Balbic71fc372011-11-22 11:37:34 +0200811 break;
812
813 case USB_ENDPOINT_XFER_ISOC:
Felipe Balbif6bafc62012-02-06 11:04:53 +0200814 trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS_FIRST;
Felipe Balbic71fc372011-11-22 11:37:34 +0200815
Pratyush Ananddf023422012-05-21 12:42:54 +0530816 if (!req->request.no_interrupt)
Felipe Balbif6bafc62012-02-06 11:04:53 +0200817 trb->ctrl |= DWC3_TRB_CTRL_IOC;
Felipe Balbic71fc372011-11-22 11:37:34 +0200818 break;
819
820 case USB_ENDPOINT_XFER_BULK:
821 case USB_ENDPOINT_XFER_INT:
Felipe Balbif6bafc62012-02-06 11:04:53 +0200822 trb->ctrl = DWC3_TRBCTL_NORMAL;
Felipe Balbic71fc372011-11-22 11:37:34 +0200823 break;
824 default:
825 /*
826 * This is only possible with faulty memory because we
827 * checked it already :)
828 */
829 BUG();
830 }
831
Ido Shayevitz57cdac12012-03-12 20:25:24 +0200832 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbif6bafc62012-02-06 11:04:53 +0200833 trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI;
834 trb->ctrl |= DWC3_TRB_CTRL_CSP;
835 } else {
836 if (chain)
837 trb->ctrl |= DWC3_TRB_CTRL_CHN;
Felipe Balbic71fc372011-11-22 11:37:34 +0200838
Felipe Balbif6bafc62012-02-06 11:04:53 +0200839 if (last)
840 trb->ctrl |= DWC3_TRB_CTRL_LST;
841 }
842
Ido Shayevitz57cdac12012-03-12 20:25:24 +0200843 if (usb_endpoint_xfer_bulk(dep->endpoint.desc) && dep->stream_capable)
Felipe Balbif6bafc62012-02-06 11:04:53 +0200844 trb->ctrl |= DWC3_TRB_CTRL_SID_SOFN(req->request.stream_id);
845
846 trb->ctrl |= DWC3_TRB_CTRL_HWO;
Felipe Balbic71fc372011-11-22 11:37:34 +0200847}
848
Felipe Balbi72246da2011-08-19 18:10:58 +0300849/*
850 * dwc3_prepare_trbs - setup TRBs from requests
851 * @dep: endpoint for which requests are being prepared
852 * @starting: true if the endpoint is idle and no requests are queued.
853 *
Paul Zimmerman1d046792012-02-15 18:56:56 -0800854 * The function goes through the requests list and sets up TRBs for the
855 * transfers. The function returns once there are no more TRBs available or
856 * it runs out of requests.
Felipe Balbi72246da2011-08-19 18:10:58 +0300857 */
Felipe Balbi68e823e2011-11-28 12:25:01 +0200858static void dwc3_prepare_trbs(struct dwc3_ep *dep, bool starting)
Felipe Balbi72246da2011-08-19 18:10:58 +0300859{
Felipe Balbi68e823e2011-11-28 12:25:01 +0200860 struct dwc3_request *req, *n;
Felipe Balbi72246da2011-08-19 18:10:58 +0300861 u32 trbs_left;
Paul Zimmerman8d62cd62012-02-15 13:35:06 +0200862 u32 max;
Felipe Balbic71fc372011-11-22 11:37:34 +0200863 unsigned int last_one = 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300864
865 BUILD_BUG_ON_NOT_POWER_OF_2(DWC3_TRB_NUM);
866
867 /* the first request must not be queued */
868 trbs_left = (dep->busy_slot - dep->free_slot) & DWC3_TRB_MASK;
Felipe Balbic71fc372011-11-22 11:37:34 +0200869
Paul Zimmerman8d62cd62012-02-15 13:35:06 +0200870 /* Can't wrap around on a non-isoc EP since there's no link TRB */
Ido Shayevitz57cdac12012-03-12 20:25:24 +0200871 if (!usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Paul Zimmerman8d62cd62012-02-15 13:35:06 +0200872 max = DWC3_TRB_NUM - (dep->free_slot & DWC3_TRB_MASK);
873 if (trbs_left > max)
874 trbs_left = max;
875 }
876
Felipe Balbi72246da2011-08-19 18:10:58 +0300877 /*
Paul Zimmerman1d046792012-02-15 18:56:56 -0800878 * If busy & slot are equal than it is either full or empty. If we are
879 * starting to process requests then we are empty. Otherwise we are
Felipe Balbi72246da2011-08-19 18:10:58 +0300880 * full and don't do anything
881 */
882 if (!trbs_left) {
883 if (!starting)
Felipe Balbi68e823e2011-11-28 12:25:01 +0200884 return;
Felipe Balbi72246da2011-08-19 18:10:58 +0300885 trbs_left = DWC3_TRB_NUM;
886 /*
887 * In case we start from scratch, we queue the ISOC requests
888 * starting from slot 1. This is done because we use ring
889 * buffer and have no LST bit to stop us. Instead, we place
Paul Zimmerman1d046792012-02-15 18:56:56 -0800890 * IOC bit every TRB_NUM/4. We try to avoid having an interrupt
Felipe Balbi72246da2011-08-19 18:10:58 +0300891 * after the first request so we start at slot 1 and have
892 * 7 requests proceed before we hit the first IOC.
893 * Other transfer types don't use the ring buffer and are
894 * processed from the first TRB until the last one. Since we
895 * don't wrap around we have to start at the beginning.
896 */
Ido Shayevitz57cdac12012-03-12 20:25:24 +0200897 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbi72246da2011-08-19 18:10:58 +0300898 dep->busy_slot = 1;
899 dep->free_slot = 1;
900 } else {
901 dep->busy_slot = 0;
902 dep->free_slot = 0;
903 }
904 }
905
906 /* The last TRB is a link TRB, not used for xfer */
Ido Shayevitz57cdac12012-03-12 20:25:24 +0200907 if ((trbs_left <= 1) && usb_endpoint_xfer_isoc(dep->endpoint.desc))
Felipe Balbi68e823e2011-11-28 12:25:01 +0200908 return;
Felipe Balbi72246da2011-08-19 18:10:58 +0300909
910 list_for_each_entry_safe(req, n, &dep->request_list, list) {
Felipe Balbieeb720f2011-11-28 12:46:59 +0200911 unsigned length;
912 dma_addr_t dma;
Felipe Balbi72246da2011-08-19 18:10:58 +0300913
Felipe Balbieeb720f2011-11-28 12:46:59 +0200914 if (req->request.num_mapped_sgs > 0) {
915 struct usb_request *request = &req->request;
916 struct scatterlist *sg = request->sg;
917 struct scatterlist *s;
918 int i;
Felipe Balbi72246da2011-08-19 18:10:58 +0300919
Felipe Balbieeb720f2011-11-28 12:46:59 +0200920 for_each_sg(sg, s, request->num_mapped_sgs, i) {
921 unsigned chain = true;
Felipe Balbi72246da2011-08-19 18:10:58 +0300922
Felipe Balbieeb720f2011-11-28 12:46:59 +0200923 length = sg_dma_len(s);
924 dma = sg_dma_address(s);
Felipe Balbi72246da2011-08-19 18:10:58 +0300925
Paul Zimmerman1d046792012-02-15 18:56:56 -0800926 if (i == (request->num_mapped_sgs - 1) ||
927 sg_is_last(s)) {
Felipe Balbieeb720f2011-11-28 12:46:59 +0200928 last_one = true;
929 chain = false;
930 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300931
Felipe Balbieeb720f2011-11-28 12:46:59 +0200932 trbs_left--;
933 if (!trbs_left)
934 last_one = true;
Felipe Balbi72246da2011-08-19 18:10:58 +0300935
Felipe Balbieeb720f2011-11-28 12:46:59 +0200936 if (last_one)
937 chain = false;
Felipe Balbi72246da2011-08-19 18:10:58 +0300938
Felipe Balbieeb720f2011-11-28 12:46:59 +0200939 dwc3_prepare_one_trb(dep, req, dma, length,
940 last_one, chain);
Felipe Balbi72246da2011-08-19 18:10:58 +0300941
Felipe Balbieeb720f2011-11-28 12:46:59 +0200942 if (last_one)
943 break;
944 }
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +0530945 dbg_queue(dep->number, &req->request, 0);
Felipe Balbi72246da2011-08-19 18:10:58 +0300946 } else {
Felipe Balbieeb720f2011-11-28 12:46:59 +0200947 dma = req->request.dma;
948 length = req->request.length;
949 trbs_left--;
950
951 if (!trbs_left)
952 last_one = 1;
953
954 /* Is this the last request? */
955 if (list_is_last(&req->list, &dep->request_list))
956 last_one = 1;
957
958 dwc3_prepare_one_trb(dep, req, dma, length,
959 last_one, false);
960
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +0530961 dbg_queue(dep->number, &req->request, 0);
Felipe Balbieeb720f2011-11-28 12:46:59 +0200962 if (last_one)
963 break;
Felipe Balbi72246da2011-08-19 18:10:58 +0300964 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300965 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300966}
967
968static int __dwc3_gadget_kick_transfer(struct dwc3_ep *dep, u16 cmd_param,
969 int start_new)
970{
971 struct dwc3_gadget_ep_cmd_params params;
Vijayavardhan Vennapusa91ba6532013-01-30 17:35:45 +0530972 struct dwc3_request *req, *req1, *n;
Felipe Balbi72246da2011-08-19 18:10:58 +0300973 struct dwc3 *dwc = dep->dwc;
974 int ret;
975 u32 cmd;
976
977 if (start_new && (dep->flags & DWC3_EP_BUSY)) {
978 dev_vdbg(dwc->dev, "%s: endpoint busy\n", dep->name);
979 return -EBUSY;
980 }
981 dep->flags &= ~DWC3_EP_PENDING_REQUEST;
982
983 /*
984 * If we are getting here after a short-out-packet we don't enqueue any
985 * new requests as we try to set the IOC bit only on the last request.
986 */
987 if (start_new) {
988 if (list_empty(&dep->req_queued))
989 dwc3_prepare_trbs(dep, start_new);
990
991 /* req points to the first request which will be sent */
992 req = next_request(&dep->req_queued);
993 } else {
Felipe Balbi68e823e2011-11-28 12:25:01 +0200994 dwc3_prepare_trbs(dep, start_new);
995
Felipe Balbi72246da2011-08-19 18:10:58 +0300996 /*
Paul Zimmerman1d046792012-02-15 18:56:56 -0800997 * req points to the first request where HWO changed from 0 to 1
Felipe Balbi72246da2011-08-19 18:10:58 +0300998 */
Felipe Balbi68e823e2011-11-28 12:25:01 +0200999 req = next_request(&dep->req_queued);
Felipe Balbi72246da2011-08-19 18:10:58 +03001000 }
1001 if (!req) {
1002 dep->flags |= DWC3_EP_PENDING_REQUEST;
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +05301003 dbg_event(dep->number, "NO REQ", 0);
Felipe Balbi72246da2011-08-19 18:10:58 +03001004 return 0;
1005 }
1006
1007 memset(&params, 0, sizeof(params));
Felipe Balbidc1c70a2011-09-30 10:58:51 +03001008 params.param0 = upper_32_bits(req->trb_dma);
1009 params.param1 = lower_32_bits(req->trb_dma);
Felipe Balbi72246da2011-08-19 18:10:58 +03001010
1011 if (start_new)
1012 cmd = DWC3_DEPCMD_STARTTRANSFER;
1013 else
1014 cmd = DWC3_DEPCMD_UPDATETRANSFER;
1015
1016 cmd |= DWC3_DEPCMD_PARAM(cmd_param);
1017 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number, cmd, &params);
1018 if (ret < 0) {
1019 dev_dbg(dwc->dev, "failed to send STARTTRANSFER command\n");
1020
Vijayavardhan Vennapusa91ba6532013-01-30 17:35:45 +05301021 if ((ret == -EAGAIN) && start_new &&
1022 usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
1023 /* If bit13 in Command complete event is set, software
1024 * must issue ENDTRANDFER command and wait for
1025 * Xfernotready event to queue the requests again.
1026 */
1027 if (!dep->resource_index) {
1028 dep->resource_index =
1029 dwc3_gadget_ep_get_transfer_index(dwc,
1030 dep->number);
1031 WARN_ON_ONCE(!dep->resource_index);
1032 }
1033 dwc3_stop_active_transfer(dwc, dep->number);
1034 list_for_each_entry_safe_reverse(req1, n,
1035 &dep->req_queued, list) {
1036 req1->trb = NULL;
1037 dwc3_gadget_move_request_list_front(req1);
1038 if (req->request.num_mapped_sgs)
1039 dep->busy_slot +=
1040 req->request.num_mapped_sgs;
1041 else
1042 dep->busy_slot++;
1043 if ((dep->busy_slot & DWC3_TRB_MASK) ==
1044 DWC3_TRB_NUM - 1)
1045 dep->busy_slot++;
1046 }
1047 return ret;
1048 } else {
1049 /*
1050 * FIXME we need to iterate over the list of requests
1051 * here and stop, unmap, free and del each of the linked
1052 * requests instead of what we do now.
1053 */
1054 usb_gadget_unmap_request(&dwc->gadget, &req->request,
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02001055 req->direction);
Vijayavardhan Vennapusa91ba6532013-01-30 17:35:45 +05301056 list_del(&req->list);
1057 return ret;
1058 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001059 }
1060
1061 dep->flags |= DWC3_EP_BUSY;
Felipe Balbi25b8ff62011-11-04 12:32:47 +02001062
Paul Zimmermanf39a37f2012-03-29 18:16:54 +00001063 if (start_new) {
Felipe Balbi4959cfc2012-06-06 12:04:13 +03001064 dep->resource_index = dwc3_gadget_ep_get_transfer_index(dwc,
Paul Zimmermanf39a37f2012-03-29 18:16:54 +00001065 dep->number);
Felipe Balbi4959cfc2012-06-06 12:04:13 +03001066 WARN_ON_ONCE(!dep->resource_index);
Paul Zimmermanf39a37f2012-03-29 18:16:54 +00001067 }
Felipe Balbi25b8ff62011-11-04 12:32:47 +02001068
Felipe Balbi72246da2011-08-19 18:10:58 +03001069 return 0;
1070}
1071
Pratyush Anand73939b02012-05-25 18:54:56 +05301072static void __dwc3_gadget_start_isoc(struct dwc3 *dwc,
1073 struct dwc3_ep *dep, u32 cur_uf)
1074{
1075 u32 uf;
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +05301076 int ret;
Pratyush Anand73939b02012-05-25 18:54:56 +05301077
Vijayavardhan Vennapusa91ba6532013-01-30 17:35:45 +05301078 dep->current_uf = cur_uf;
1079
Pratyush Anand73939b02012-05-25 18:54:56 +05301080 if (list_empty(&dep->request_list)) {
1081 dev_vdbg(dwc->dev, "ISOC ep %s run out for requests.\n",
1082 dep->name);
Pratyush Anandac417602012-08-30 12:21:43 +05301083 dep->flags |= DWC3_EP_PENDING_REQUEST;
Pratyush Anand73939b02012-05-25 18:54:56 +05301084 return;
1085 }
1086
1087 /* 4 micro frames in the future */
1088 uf = cur_uf + dep->interval * 4;
1089
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +05301090 ret = __dwc3_gadget_kick_transfer(dep, uf, 1);
1091 if (ret < 0)
1092 dbg_event(dep->number, "QUEUE", ret);
Pratyush Anand73939b02012-05-25 18:54:56 +05301093}
1094
1095static void dwc3_gadget_start_isoc(struct dwc3 *dwc,
1096 struct dwc3_ep *dep, const struct dwc3_event_depevt *event)
1097{
1098 u32 cur_uf, mask;
1099
1100 mask = ~(dep->interval - 1);
1101 cur_uf = event->parameters & mask;
1102
1103 __dwc3_gadget_start_isoc(dwc, dep, cur_uf);
1104}
1105
Felipe Balbi72246da2011-08-19 18:10:58 +03001106static int __dwc3_gadget_ep_queue(struct dwc3_ep *dep, struct dwc3_request *req)
1107{
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02001108 struct dwc3 *dwc = dep->dwc;
1109 int ret;
1110
Manu Gautamd2b99e12013-02-11 15:53:34 +05301111 if (req->request.status == -EINPROGRESS) {
1112 ret = -EBUSY;
1113 dev_err(dwc->dev, "%s: %p request already in queue",
1114 dep->name, req);
1115 return ret;
1116 }
1117
Felipe Balbi72246da2011-08-19 18:10:58 +03001118 req->request.actual = 0;
1119 req->request.status = -EINPROGRESS;
1120 req->direction = dep->direction;
1121 req->epnum = dep->number;
1122
1123 /*
1124 * We only add to our list of requests now and
1125 * start consuming the list once we get XferNotReady
1126 * IRQ.
1127 *
1128 * That way, we avoid doing anything that we don't need
1129 * to do now and defer it until the point we receive a
1130 * particular token from the Host side.
1131 *
1132 * This will also avoid Host cancelling URBs due to too
Paul Zimmerman1d046792012-02-15 18:56:56 -08001133 * many NAKs.
Felipe Balbi72246da2011-08-19 18:10:58 +03001134 */
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02001135 ret = usb_gadget_map_request(&dwc->gadget, &req->request,
1136 dep->direction);
1137 if (ret)
1138 return ret;
1139
Felipe Balbi72246da2011-08-19 18:10:58 +03001140 list_add_tail(&req->list, &dep->request_list);
1141
1142 /*
Felipe Balbi46485a02012-06-06 12:00:50 +03001143 * There are a few special cases:
Felipe Balbi72246da2011-08-19 18:10:58 +03001144 *
Paul Zimmermanf39a37f2012-03-29 18:16:54 +00001145 * 1. XferNotReady with empty list of requests. We need to kick the
1146 * transfer here in that situation, otherwise we will be NAKing
1147 * forever. If we get XferNotReady before gadget driver has a
1148 * chance to queue a request, we will ACK the IRQ but won't be
1149 * able to receive the data until the next request is queued.
1150 * The following code is handling exactly that.
1151 *
Felipe Balbi72246da2011-08-19 18:10:58 +03001152 */
1153 if (dep->flags & DWC3_EP_PENDING_REQUEST) {
Pratyush Anandac417602012-08-30 12:21:43 +05301154 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001155
Pratyush Anandac417602012-08-30 12:21:43 +05301156 /*
1157 * If xfernotready is already elapsed and it is a case
1158 * of isoc transfer, then issue END TRANSFER, so that
1159 * you can receive xfernotready again and can have
1160 * notion of current microframe.
1161 */
1162 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Vijayavardhan Vennapusa91ba6532013-01-30 17:35:45 +05301163 /* If xfernotready event is recieved before issuing
1164 * START TRANSFER command, don't issue END TRANSFER.
1165 * Rather start queueing the requests by issuing START
1166 * TRANSFER command.
1167 */
1168 if (list_empty(&dep->req_queued) && dep->resource_index)
Pratyush Anand18bbcb02013-01-14 15:59:34 +05301169 dwc3_stop_active_transfer(dwc, dep->number);
Vijayavardhan Vennapusa91ba6532013-01-30 17:35:45 +05301170 else
1171 __dwc3_gadget_start_isoc(dwc, dep,
1172 dep->current_uf);
1173 dep->flags &= ~DWC3_EP_PENDING_REQUEST;
Pratyush Anandac417602012-08-30 12:21:43 +05301174 return 0;
1175 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001176
Felipe Balbi46485a02012-06-06 12:00:50 +03001177 ret = __dwc3_gadget_kick_transfer(dep, 0, true);
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +05301178 if (ret && ret != -EBUSY) {
1179 dbg_event(dep->number, "QUEUE", ret);
Felipe Balbi72246da2011-08-19 18:10:58 +03001180 dev_dbg(dwc->dev, "%s: failed to kick transfers\n",
1181 dep->name);
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +05301182 }
Felipe Balbi5d409eb2012-05-22 10:24:11 +03001183 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001184
Felipe Balbi46485a02012-06-06 12:00:50 +03001185 /*
1186 * 2. XferInProgress on Isoc EP with an active transfer. We need to
1187 * kick the transfer here after queuing a request, otherwise the
1188 * core may not see the modified TRB(s).
1189 */
1190 if (usb_endpoint_xfer_isoc(dep->endpoint.desc) &&
Pratyush Anand053d3e52012-08-07 16:54:18 +05301191 (dep->flags & DWC3_EP_BUSY) &&
1192 !(dep->flags & DWC3_EP_MISSED_ISOC)) {
Felipe Balbi4959cfc2012-06-06 12:04:13 +03001193 WARN_ON_ONCE(!dep->resource_index);
1194 ret = __dwc3_gadget_kick_transfer(dep, dep->resource_index,
Felipe Balbi46485a02012-06-06 12:00:50 +03001195 false);
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +05301196 if (ret && ret != -EBUSY) {
1197 dbg_event(dep->number, "QUEUE", ret);
Felipe Balbi46485a02012-06-06 12:00:50 +03001198 dev_dbg(dwc->dev, "%s: failed to kick transfers\n",
1199 dep->name);
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +05301200 }
Felipe Balbi46485a02012-06-06 12:00:50 +03001201 }
1202
Felipe Balbi72246da2011-08-19 18:10:58 +03001203 return 0;
1204}
1205
1206static int dwc3_gadget_ep_queue(struct usb_ep *ep, struct usb_request *request,
1207 gfp_t gfp_flags)
1208{
1209 struct dwc3_request *req = to_dwc3_request(request);
1210 struct dwc3_ep *dep = to_dwc3_ep(ep);
1211 struct dwc3 *dwc = dep->dwc;
1212
1213 unsigned long flags;
1214
1215 int ret;
1216
Manu Gautam22f93042013-02-20 15:12:02 +05301217 spin_lock_irqsave(&dwc->lock, flags);
1218
Ido Shayevitz57cdac12012-03-12 20:25:24 +02001219 if (!dep->endpoint.desc) {
Manu Gautam22f93042013-02-20 15:12:02 +05301220 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001221 dev_dbg(dwc->dev, "trying to queue request %p to disabled %s\n",
1222 request, ep->name);
1223 return -ESHUTDOWN;
1224 }
1225
1226 dev_vdbg(dwc->dev, "queing request %p to %s length %d\n",
1227 request, ep->name, request->length);
1228
Manu Gautam1c4dbcb2012-10-05 13:16:00 +05301229 WARN(!dep->direction && (request->length % ep->desc->wMaxPacketSize),
1230 "trying to queue unaligned request (%d)\n", request->length);
1231
Felipe Balbi72246da2011-08-19 18:10:58 +03001232 ret = __dwc3_gadget_ep_queue(dep, req);
1233 spin_unlock_irqrestore(&dwc->lock, flags);
1234
1235 return ret;
1236}
1237
1238static int dwc3_gadget_ep_dequeue(struct usb_ep *ep,
1239 struct usb_request *request)
1240{
1241 struct dwc3_request *req = to_dwc3_request(request);
1242 struct dwc3_request *r = NULL;
1243
1244 struct dwc3_ep *dep = to_dwc3_ep(ep);
1245 struct dwc3 *dwc = dep->dwc;
1246
1247 unsigned long flags;
1248 int ret = 0;
1249
1250 spin_lock_irqsave(&dwc->lock, flags);
1251
1252 list_for_each_entry(r, &dep->request_list, list) {
1253 if (r == req)
1254 break;
1255 }
1256
1257 if (r != req) {
1258 list_for_each_entry(r, &dep->req_queued, list) {
1259 if (r == req)
1260 break;
1261 }
1262 if (r == req) {
1263 /* wait until it is processed */
1264 dwc3_stop_active_transfer(dwc, dep->number);
Pratyush Anandeaec3e92012-06-15 11:54:00 +05301265 goto out1;
Felipe Balbi72246da2011-08-19 18:10:58 +03001266 }
1267 dev_err(dwc->dev, "request %p was not queued to %s\n",
1268 request, ep->name);
1269 ret = -EINVAL;
1270 goto out0;
1271 }
1272
Pratyush Anandeaec3e92012-06-15 11:54:00 +05301273out1:
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +05301274 dbg_event(dep->number, "DEQUEUE", 0);
Felipe Balbi72246da2011-08-19 18:10:58 +03001275 /* giveback the request */
1276 dwc3_gadget_giveback(dep, req, -ECONNRESET);
1277
1278out0:
1279 spin_unlock_irqrestore(&dwc->lock, flags);
1280
1281 return ret;
1282}
1283
1284int __dwc3_gadget_ep_set_halt(struct dwc3_ep *dep, int value)
1285{
1286 struct dwc3_gadget_ep_cmd_params params;
1287 struct dwc3 *dwc = dep->dwc;
1288 int ret;
1289
1290 memset(&params, 0x00, sizeof(params));
1291
1292 if (value) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001293 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number,
1294 DWC3_DEPCMD_SETSTALL, &params);
1295 if (ret)
1296 dev_err(dwc->dev, "failed to %s STALL on %s\n",
1297 value ? "set" : "clear",
1298 dep->name);
1299 else
1300 dep->flags |= DWC3_EP_STALL;
1301 } else {
1302 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number,
1303 DWC3_DEPCMD_CLEARSTALL, &params);
1304 if (ret)
1305 dev_err(dwc->dev, "failed to %s STALL on %s\n",
1306 value ? "set" : "clear",
1307 dep->name);
1308 else
Vijayavardhan Vennapusa6008e262012-10-19 15:57:56 +05301309 dep->flags &= ~(DWC3_EP_STALL | DWC3_EP_WEDGE);
Felipe Balbi72246da2011-08-19 18:10:58 +03001310 }
Paul Zimmerman52754552011-09-30 10:58:44 +03001311
Felipe Balbi72246da2011-08-19 18:10:58 +03001312 return ret;
1313}
1314
1315static int dwc3_gadget_ep_set_halt(struct usb_ep *ep, int value)
1316{
1317 struct dwc3_ep *dep = to_dwc3_ep(ep);
1318 struct dwc3 *dwc = dep->dwc;
1319
1320 unsigned long flags;
1321
1322 int ret;
1323
1324 spin_lock_irqsave(&dwc->lock, flags);
1325
Ido Shayevitz57cdac12012-03-12 20:25:24 +02001326 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001327 dev_err(dwc->dev, "%s is of Isochronous type\n", dep->name);
1328 ret = -EINVAL;
1329 goto out;
1330 }
1331
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +05301332 dbg_event(dep->number, "HALT", value);
Felipe Balbi72246da2011-08-19 18:10:58 +03001333 ret = __dwc3_gadget_ep_set_halt(dep, value);
1334out:
1335 spin_unlock_irqrestore(&dwc->lock, flags);
1336
1337 return ret;
1338}
1339
1340static int dwc3_gadget_ep_set_wedge(struct usb_ep *ep)
1341{
1342 struct dwc3_ep *dep = to_dwc3_ep(ep);
Paul Zimmerman249a4562012-02-24 17:32:16 -08001343 struct dwc3 *dwc = dep->dwc;
1344 unsigned long flags;
Felipe Balbi72246da2011-08-19 18:10:58 +03001345
Paul Zimmerman249a4562012-02-24 17:32:16 -08001346 spin_lock_irqsave(&dwc->lock, flags);
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +05301347 dbg_event(dep->number, "WEDGE", 0);
Felipe Balbi72246da2011-08-19 18:10:58 +03001348 dep->flags |= DWC3_EP_WEDGE;
Paul Zimmerman249a4562012-02-24 17:32:16 -08001349 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001350
Pratyush Anandeb840752012-06-25 22:40:43 +05301351 if (dep->number == 0 || dep->number == 1)
1352 return dwc3_gadget_ep0_set_halt(ep, 1);
1353 else
1354 return dwc3_gadget_ep_set_halt(ep, 1);
Felipe Balbi72246da2011-08-19 18:10:58 +03001355}
1356
1357/* -------------------------------------------------------------------------- */
1358
1359static struct usb_endpoint_descriptor dwc3_gadget_ep0_desc = {
1360 .bLength = USB_DT_ENDPOINT_SIZE,
1361 .bDescriptorType = USB_DT_ENDPOINT,
1362 .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
1363};
1364
1365static const struct usb_ep_ops dwc3_gadget_ep0_ops = {
1366 .enable = dwc3_gadget_ep0_enable,
1367 .disable = dwc3_gadget_ep0_disable,
1368 .alloc_request = dwc3_gadget_ep_alloc_request,
1369 .free_request = dwc3_gadget_ep_free_request,
1370 .queue = dwc3_gadget_ep0_queue,
1371 .dequeue = dwc3_gadget_ep_dequeue,
Pratyush Anandeb840752012-06-25 22:40:43 +05301372 .set_halt = dwc3_gadget_ep0_set_halt,
Felipe Balbi72246da2011-08-19 18:10:58 +03001373 .set_wedge = dwc3_gadget_ep_set_wedge,
1374};
1375
1376static const struct usb_ep_ops dwc3_gadget_ep_ops = {
1377 .enable = dwc3_gadget_ep_enable,
1378 .disable = dwc3_gadget_ep_disable,
1379 .alloc_request = dwc3_gadget_ep_alloc_request,
1380 .free_request = dwc3_gadget_ep_free_request,
1381 .queue = dwc3_gadget_ep_queue,
1382 .dequeue = dwc3_gadget_ep_dequeue,
1383 .set_halt = dwc3_gadget_ep_set_halt,
1384 .set_wedge = dwc3_gadget_ep_set_wedge,
1385};
1386
1387/* -------------------------------------------------------------------------- */
1388
1389static int dwc3_gadget_get_frame(struct usb_gadget *g)
1390{
1391 struct dwc3 *dwc = gadget_to_dwc(g);
1392 u32 reg;
1393
1394 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1395 return DWC3_DSTS_SOFFN(reg);
1396}
1397
1398static int dwc3_gadget_wakeup(struct usb_gadget *g)
1399{
1400 struct dwc3 *dwc = gadget_to_dwc(g);
1401
1402 unsigned long timeout;
1403 unsigned long flags;
1404
1405 u32 reg;
1406
1407 int ret = 0;
1408
1409 u8 link_state;
1410 u8 speed;
1411
1412 spin_lock_irqsave(&dwc->lock, flags);
1413
1414 /*
1415 * According to the Databook Remote wakeup request should
1416 * be issued only when the device is in early suspend state.
1417 *
1418 * We can check that via USB Link State bits in DSTS register.
1419 */
1420 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1421
1422 speed = reg & DWC3_DSTS_CONNECTSPD;
1423 if (speed == DWC3_DSTS_SUPERSPEED) {
1424 dev_dbg(dwc->dev, "no wakeup on SuperSpeed\n");
1425 ret = -EINVAL;
1426 goto out;
1427 }
1428
1429 link_state = DWC3_DSTS_USBLNKST(reg);
1430
1431 switch (link_state) {
1432 case DWC3_LINK_STATE_RX_DET: /* in HS, means Early Suspend */
1433 case DWC3_LINK_STATE_U3: /* in HS, means SUSPEND */
1434 break;
1435 default:
1436 dev_dbg(dwc->dev, "can't wakeup from link state %d\n",
1437 link_state);
1438 ret = -EINVAL;
1439 goto out;
1440 }
1441
Felipe Balbi8598bde2012-01-02 18:55:57 +02001442 ret = dwc3_gadget_set_link_state(dwc, DWC3_LINK_STATE_RECOV);
1443 if (ret < 0) {
1444 dev_err(dwc->dev, "failed to put link in Recovery\n");
1445 goto out;
1446 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001447
Paul Zimmerman88df4272012-04-27 13:10:52 +03001448 /* Recent versions do this automatically */
1449 if (dwc->revision < DWC3_REVISION_194A) {
1450 /* write zeroes to Link Change Request */
Felipe Balbib4d04352012-05-24 10:27:56 +03001451 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
Paul Zimmerman88df4272012-04-27 13:10:52 +03001452 reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK;
1453 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1454 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001455
Paul Zimmerman1d046792012-02-15 18:56:56 -08001456 /* poll until Link State changes to ON */
Felipe Balbi72246da2011-08-19 18:10:58 +03001457 timeout = jiffies + msecs_to_jiffies(100);
1458
Paul Zimmerman1d046792012-02-15 18:56:56 -08001459 while (!time_after(jiffies, timeout)) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001460 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1461
1462 /* in HS, means ON */
1463 if (DWC3_DSTS_USBLNKST(reg) == DWC3_LINK_STATE_U0)
1464 break;
1465 }
1466
1467 if (DWC3_DSTS_USBLNKST(reg) != DWC3_LINK_STATE_U0) {
1468 dev_err(dwc->dev, "failed to send remote wakeup\n");
1469 ret = -EINVAL;
1470 }
1471
1472out:
1473 spin_unlock_irqrestore(&dwc->lock, flags);
1474
1475 return ret;
1476}
1477
1478static int dwc3_gadget_set_selfpowered(struct usb_gadget *g,
1479 int is_selfpowered)
1480{
1481 struct dwc3 *dwc = gadget_to_dwc(g);
Paul Zimmerman249a4562012-02-24 17:32:16 -08001482 unsigned long flags;
Felipe Balbi72246da2011-08-19 18:10:58 +03001483
Paul Zimmerman249a4562012-02-24 17:32:16 -08001484 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001485 dwc->is_selfpowered = !!is_selfpowered;
Paul Zimmerman249a4562012-02-24 17:32:16 -08001486 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001487
1488 return 0;
1489}
1490
Wesley Cheng446ad8d2013-06-05 16:15:01 +05301491#define DWC3_SOFT_RESET_TIMEOUT 10 /* 10 msec */
Pratyush Anand77473f72012-07-02 10:21:55 +05301492static int dwc3_gadget_run_stop(struct dwc3 *dwc, int is_on)
Felipe Balbi72246da2011-08-19 18:10:58 +03001493{
1494 u32 reg;
Sebastian Andrzej Siewior61d58242011-08-29 16:46:38 +02001495 u32 timeout = 500;
Wesley Cheng446ad8d2013-06-05 16:15:01 +05301496 ktime_t start, diff;
Felipe Balbi72246da2011-08-19 18:10:58 +03001497
1498 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
Felipe Balbi8db7ed12012-01-18 18:32:29 +02001499 if (is_on) {
Paul Zimmerman88df4272012-04-27 13:10:52 +03001500 if (dwc->revision <= DWC3_REVISION_187A) {
1501 reg &= ~DWC3_DCTL_TRGTULST_MASK;
1502 reg |= DWC3_DCTL_TRGTULST_RX_DET;
1503 }
1504
1505 if (dwc->revision >= DWC3_REVISION_194A)
1506 reg &= ~DWC3_DCTL_KEEP_CONNECT;
Wesley Cheng446ad8d2013-06-05 16:15:01 +05301507
1508 start = ktime_get();
1509 /* issue device SoftReset */
1510 dwc3_writel(dwc->regs, DWC3_DCTL, reg | DWC3_DCTL_CSFTRST);
1511 do {
1512 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
1513 if (!(reg & DWC3_DCTL_CSFTRST))
1514 break;
1515
1516 diff = ktime_sub(ktime_get(), start);
1517 /* poll for max. 10ms */
1518 if (ktime_to_ms(diff) > DWC3_SOFT_RESET_TIMEOUT) {
1519 printk_ratelimited(KERN_ERR
1520 "%s:core Reset Timed Out\n", __func__);
1521 break;
1522 }
1523 cpu_relax();
1524 } while (true);
1525
1526
1527 dwc3_event_buffers_setup(dwc);
1528 dwc3_gadget_restart(dwc);
1529 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
Paul Zimmerman88df4272012-04-27 13:10:52 +03001530 reg |= DWC3_DCTL_RUN_STOP;
Felipe Balbi8db7ed12012-01-18 18:32:29 +02001531 } else {
Felipe Balbi72246da2011-08-19 18:10:58 +03001532 reg &= ~DWC3_DCTL_RUN_STOP;
Felipe Balbi8db7ed12012-01-18 18:32:29 +02001533 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001534
1535 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1536
1537 do {
1538 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1539 if (is_on) {
1540 if (!(reg & DWC3_DSTS_DEVCTRLHLT))
1541 break;
1542 } else {
1543 if (reg & DWC3_DSTS_DEVCTRLHLT)
1544 break;
1545 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001546 timeout--;
1547 if (!timeout)
Pratyush Anand77473f72012-07-02 10:21:55 +05301548 return -ETIMEDOUT;
Sebastian Andrzej Siewior61d58242011-08-29 16:46:38 +02001549 udelay(1);
Felipe Balbi72246da2011-08-19 18:10:58 +03001550 } while (1);
1551
1552 dev_vdbg(dwc->dev, "gadget %s data soft-%s\n",
1553 dwc->gadget_driver
1554 ? dwc->gadget_driver->function : "no-function",
1555 is_on ? "connect" : "disconnect");
Pratyush Anand77473f72012-07-02 10:21:55 +05301556
1557 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +03001558}
1559
Vijayavardhan Vennapusa908f1ed2012-10-19 19:51:48 +05301560static int dwc3_gadget_vbus_draw(struct usb_gadget *g, unsigned mA)
1561{
1562 struct dwc3 *dwc = gadget_to_dwc(g);
1563 struct dwc3_otg *dotg = dwc->dotg;
1564
1565 if (dotg && dotg->otg.phy)
1566 return usb_phy_set_power(dotg->otg.phy, mA);
1567
1568 return -ENOTSUPP;
1569}
1570
Felipe Balbi72246da2011-08-19 18:10:58 +03001571static int dwc3_gadget_pullup(struct usb_gadget *g, int is_on)
1572{
1573 struct dwc3 *dwc = gadget_to_dwc(g);
1574 unsigned long flags;
Pratyush Anand77473f72012-07-02 10:21:55 +05301575 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001576
1577 is_on = !!is_on;
1578
1579 spin_lock_irqsave(&dwc->lock, flags);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +02001580
1581 dwc->softconnect = is_on;
1582
1583 if ((dwc->dotg && !dwc->vbus_active) ||
1584 !dwc->gadget_driver) {
1585
1586 spin_unlock_irqrestore(&dwc->lock, flags);
1587
1588 /*
1589 * Need to wait for vbus_session(on) from otg driver or to
1590 * the udc_start.
1591 */
1592 return 0;
1593 }
1594
Pratyush Anand77473f72012-07-02 10:21:55 +05301595 ret = dwc3_gadget_run_stop(dwc, is_on);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +02001596
1597 spin_unlock_irqrestore(&dwc->lock, flags);
1598
Pratyush Anand77473f72012-07-02 10:21:55 +05301599 return ret;
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +02001600}
1601
Jack Pham09e5c8e2013-03-06 18:53:43 -08001602static void dwc3_gadget_disconnect_interrupt(struct dwc3 *dwc);
1603
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +02001604static int dwc3_gadget_vbus_session(struct usb_gadget *_gadget, int is_active)
1605{
1606 struct dwc3 *dwc = gadget_to_dwc(_gadget);
1607 unsigned long flags;
Vijayavardhan Vennapusa8ec31d22012-10-23 08:44:48 +05301608 int ret = 0;
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +02001609
1610 if (!dwc->dotg)
1611 return -EPERM;
1612
1613 is_active = !!is_active;
1614
1615 spin_lock_irqsave(&dwc->lock, flags);
1616
1617 /* Mark that the vbus was powered */
1618 dwc->vbus_active = is_active;
1619
1620 /*
1621 * Check if upper level usb_gadget_driver was already registerd with
1622 * this udc controller driver (if dwc3_gadget_start was called)
1623 */
1624 if (dwc->gadget_driver && dwc->softconnect) {
1625 if (dwc->vbus_active) {
1626 /*
1627 * Both vbus was activated by otg and pullup was
1628 * signaled by the gadget driver.
1629 */
Pratyush Anand77473f72012-07-02 10:21:55 +05301630 ret = dwc3_gadget_run_stop(dwc, 1);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +02001631 } else {
Pratyush Anand77473f72012-07-02 10:21:55 +05301632 ret = dwc3_gadget_run_stop(dwc, 0);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +02001633 }
Jack Pham09e5c8e2013-03-06 18:53:43 -08001634 }
1635
1636 /*
1637 * Clearing run/stop bit might occur before disconnect event is seen.
1638 * Make sure to let gadget driver know in that case.
1639 */
1640 if (!dwc->vbus_active && dwc->start_config_issued) {
1641 dev_dbg(dwc->dev, "calling disconnect from %s\n", __func__);
1642 dwc3_gadget_disconnect_interrupt(dwc);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +02001643 }
1644
Felipe Balbi72246da2011-08-19 18:10:58 +03001645 spin_unlock_irqrestore(&dwc->lock, flags);
Pratyush Anand77473f72012-07-02 10:21:55 +05301646 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001647}
1648
Manu Gautamf1fceddf2012-10-12 14:02:50 +05301649/* Required gadget re-initialization before switching to gadget in OTG mode */
1650void dwc3_gadget_restart(struct dwc3 *dwc)
1651{
1652 struct dwc3_ep *dep;
1653 int ret = 0;
Vijayavardhan Vennapusab7434562012-12-12 16:48:49 +05301654 u32 reg;
Manu Gautamf1fceddf2012-10-12 14:02:50 +05301655
Vijayavardhan Vennapusab7434562012-12-12 16:48:49 +05301656 /* Enable all but Start and End of Frame IRQs */
1657 reg = (DWC3_DEVTEN_EVNTOVERFLOWEN |
1658 DWC3_DEVTEN_CMDCMPLTEN |
1659 DWC3_DEVTEN_ERRTICERREN |
1660 DWC3_DEVTEN_WKUPEVTEN |
1661 DWC3_DEVTEN_ULSTCNGEN |
1662 DWC3_DEVTEN_CONNECTDONEEN |
1663 DWC3_DEVTEN_USBRSTEN |
1664 DWC3_DEVTEN_DISCONNEVTEN);
1665 dwc3_writel(dwc->regs, DWC3_DEVTEN, reg);
1666
1667 /* Enable USB2 LPM and automatic phy suspend only on recent versions */
1668 if (dwc->revision >= DWC3_REVISION_194A) {
1669 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
1670 reg |= DWC3_DCFG_LPM_CAP;
1671 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
1672
1673 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
1674 reg &= ~(DWC3_DCTL_HIRD_THRES_MASK | DWC3_DCTL_L1_HIBER_EN);
1675
1676 /* TODO: This should be configurable */
1677 reg |= DWC3_DCTL_HIRD_THRES(28);
1678
1679 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1680 }
1681
1682 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
1683 reg &= ~(DWC3_DCFG_SPEED_MASK);
1684
1685 /**
1686 * WORKAROUND: DWC3 revision < 2.20a have an issue
1687 * which would cause metastability state on Run/Stop
1688 * bit if we try to force the IP to USB2-only mode.
1689 *
1690 * Because of that, we cannot configure the IP to any
1691 * speed other than the SuperSpeed
1692 *
1693 * Refers to:
1694 *
1695 * STAR#9000525659: Clock Domain Crossing on DCTL in
1696 * USB 2.0 Mode
1697 */
1698 if (dwc->revision < DWC3_REVISION_220A)
1699 reg |= DWC3_DCFG_SUPERSPEED;
1700 else
1701 reg |= dwc->maximum_speed;
1702 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
1703
1704 dwc->start_config_issued = false;
1705
1706 /* Start with SuperSpeed Default */
1707 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
Manu Gautamf1fceddf2012-10-12 14:02:50 +05301708
1709 dwc->delayed_status = false;
Vijayavardhan Vennapusab7434562012-12-12 16:48:49 +05301710 /* reinitialize physical ep0-1 */
Manu Gautamf1fceddf2012-10-12 14:02:50 +05301711 dep = dwc->eps[0];
1712 dep->flags = 0;
1713 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, false);
1714 if (ret) {
1715 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
1716 return;
1717 }
1718
1719 dep = dwc->eps[1];
1720 dep->flags = 0;
1721 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, false);
1722 if (ret) {
1723 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
1724 return;
1725 }
1726
1727 /* begin to receive SETUP packets */
1728 dwc->ep0state = EP0_SETUP_PHASE;
1729 dwc3_ep0_out_start(dwc);
1730}
1731
Felipe Balbi72246da2011-08-19 18:10:58 +03001732static int dwc3_gadget_start(struct usb_gadget *g,
1733 struct usb_gadget_driver *driver)
1734{
1735 struct dwc3 *dwc = gadget_to_dwc(g);
1736 struct dwc3_ep *dep;
1737 unsigned long flags;
1738 int ret = 0;
1739 u32 reg;
1740
Vijayavardhan Vennapusa0557c9b2013-05-17 13:24:49 +05301741 pm_runtime_get_sync(dwc->dev);
Felipe Balbi72246da2011-08-19 18:10:58 +03001742 spin_lock_irqsave(&dwc->lock, flags);
1743
1744 if (dwc->gadget_driver) {
1745 dev_err(dwc->dev, "%s is already bound to %s\n",
1746 dwc->gadget.name,
1747 dwc->gadget_driver->driver.name);
1748 ret = -EBUSY;
1749 goto err0;
1750 }
1751
1752 dwc->gadget_driver = driver;
1753 dwc->gadget.dev.driver = &driver->driver;
1754
Felipe Balbi72246da2011-08-19 18:10:58 +03001755 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
1756 reg &= ~(DWC3_DCFG_SPEED_MASK);
Felipe Balbi38d2c6c2012-03-23 12:20:31 +02001757
1758 /**
1759 * WORKAROUND: DWC3 revision < 2.20a have an issue
1760 * which would cause metastability state on Run/Stop
1761 * bit if we try to force the IP to USB2-only mode.
1762 *
1763 * Because of that, we cannot configure the IP to any
1764 * speed other than the SuperSpeed
1765 *
1766 * Refers to:
1767 *
1768 * STAR#9000525659: Clock Domain Crossing on DCTL in
1769 * USB 2.0 Mode
1770 */
1771 if (dwc->revision < DWC3_REVISION_220A)
1772 reg |= DWC3_DCFG_SUPERSPEED;
1773 else
1774 reg |= dwc->maximum_speed;
Felipe Balbi72246da2011-08-19 18:10:58 +03001775 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
1776
Paul Zimmermanb23c8432011-09-30 10:58:42 +03001777 dwc->start_config_issued = false;
1778
Felipe Balbi72246da2011-08-19 18:10:58 +03001779 /* Start with SuperSpeed Default */
1780 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
1781
1782 dep = dwc->eps[0];
Wesley Cheng446ad8d2013-06-05 16:15:01 +05301783 dep->endpoint.maxburst = 1;
Felipe Balbi07e0ee82012-07-16 14:08:16 +03001784 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, false);
Felipe Balbi72246da2011-08-19 18:10:58 +03001785 if (ret) {
1786 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
1787 goto err0;
1788 }
1789
1790 dep = dwc->eps[1];
Wesley Cheng446ad8d2013-06-05 16:15:01 +05301791 dep->endpoint.maxburst = 1;
Felipe Balbi07e0ee82012-07-16 14:08:16 +03001792 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, false);
Felipe Balbi72246da2011-08-19 18:10:58 +03001793 if (ret) {
1794 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
1795 goto err1;
1796 }
1797
1798 /* begin to receive SETUP packets */
Felipe Balbic7fcdeb2011-08-27 22:28:36 +03001799 dwc->ep0state = EP0_SETUP_PHASE;
Felipe Balbi72246da2011-08-19 18:10:58 +03001800 dwc3_ep0_out_start(dwc);
1801
1802 spin_unlock_irqrestore(&dwc->lock, flags);
Vijayavardhan Vennapusa0557c9b2013-05-17 13:24:49 +05301803 pm_runtime_put(dwc->dev);
Felipe Balbi72246da2011-08-19 18:10:58 +03001804
1805 return 0;
1806
1807err1:
1808 __dwc3_gadget_ep_disable(dwc->eps[0]);
1809
1810err0:
1811 spin_unlock_irqrestore(&dwc->lock, flags);
Vijayavardhan Vennapusa0557c9b2013-05-17 13:24:49 +05301812 pm_runtime_put(dwc->dev);
Felipe Balbi72246da2011-08-19 18:10:58 +03001813
1814 return ret;
1815}
1816
1817static int dwc3_gadget_stop(struct usb_gadget *g,
1818 struct usb_gadget_driver *driver)
1819{
1820 struct dwc3 *dwc = gadget_to_dwc(g);
1821 unsigned long flags;
1822
1823 spin_lock_irqsave(&dwc->lock, flags);
1824
1825 __dwc3_gadget_ep_disable(dwc->eps[0]);
1826 __dwc3_gadget_ep_disable(dwc->eps[1]);
1827
1828 dwc->gadget_driver = NULL;
1829 dwc->gadget.dev.driver = NULL;
1830
1831 spin_unlock_irqrestore(&dwc->lock, flags);
1832
1833 return 0;
1834}
Paul Zimmerman88df4272012-04-27 13:10:52 +03001835
Felipe Balbi72246da2011-08-19 18:10:58 +03001836static const struct usb_gadget_ops dwc3_gadget_ops = {
1837 .get_frame = dwc3_gadget_get_frame,
1838 .wakeup = dwc3_gadget_wakeup,
1839 .set_selfpowered = dwc3_gadget_set_selfpowered,
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +02001840 .vbus_session = dwc3_gadget_vbus_session,
Vijayavardhan Vennapusa908f1ed2012-10-19 19:51:48 +05301841 .vbus_draw = dwc3_gadget_vbus_draw,
Felipe Balbi72246da2011-08-19 18:10:58 +03001842 .pullup = dwc3_gadget_pullup,
1843 .udc_start = dwc3_gadget_start,
1844 .udc_stop = dwc3_gadget_stop,
1845};
1846
1847/* -------------------------------------------------------------------------- */
1848
1849static int __devinit dwc3_gadget_init_endpoints(struct dwc3 *dwc)
1850{
1851 struct dwc3_ep *dep;
1852 u8 epnum;
1853
1854 INIT_LIST_HEAD(&dwc->gadget.ep_list);
1855
1856 for (epnum = 0; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
1857 dep = kzalloc(sizeof(*dep), GFP_KERNEL);
1858 if (!dep) {
1859 dev_err(dwc->dev, "can't allocate endpoint %d\n",
1860 epnum);
1861 return -ENOMEM;
1862 }
1863
1864 dep->dwc = dwc;
1865 dep->number = epnum;
1866 dwc->eps[epnum] = dep;
1867
1868 snprintf(dep->name, sizeof(dep->name), "ep%d%s", epnum >> 1,
1869 (epnum & 1) ? "in" : "out");
1870 dep->endpoint.name = dep->name;
1871 dep->direction = (epnum & 1);
1872
1873 if (epnum == 0 || epnum == 1) {
1874 dep->endpoint.maxpacket = 512;
1875 dep->endpoint.ops = &dwc3_gadget_ep0_ops;
1876 if (!epnum)
1877 dwc->gadget.ep0 = &dep->endpoint;
1878 } else {
1879 int ret;
1880
1881 dep->endpoint.maxpacket = 1024;
Sebastian Andrzej Siewior12d36c12011-11-03 20:27:50 +01001882 dep->endpoint.max_streams = 15;
Felipe Balbi72246da2011-08-19 18:10:58 +03001883 dep->endpoint.ops = &dwc3_gadget_ep_ops;
1884 list_add_tail(&dep->endpoint.ep_list,
1885 &dwc->gadget.ep_list);
1886
1887 ret = dwc3_alloc_trb_pool(dep);
Felipe Balbi25b8ff62011-11-04 12:32:47 +02001888 if (ret)
Felipe Balbi72246da2011-08-19 18:10:58 +03001889 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001890 }
Felipe Balbi25b8ff62011-11-04 12:32:47 +02001891
Felipe Balbi72246da2011-08-19 18:10:58 +03001892 INIT_LIST_HEAD(&dep->request_list);
1893 INIT_LIST_HEAD(&dep->req_queued);
1894 }
1895
1896 return 0;
1897}
1898
1899static void dwc3_gadget_free_endpoints(struct dwc3 *dwc)
1900{
1901 struct dwc3_ep *dep;
1902 u8 epnum;
1903
1904 for (epnum = 0; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
1905 dep = dwc->eps[epnum];
1906 dwc3_free_trb_pool(dep);
1907
1908 if (epnum != 0 && epnum != 1)
1909 list_del(&dep->endpoint.ep_list);
1910
1911 kfree(dep);
1912 }
1913}
1914
1915static void dwc3_gadget_release(struct device *dev)
1916{
1917 dev_dbg(dev, "%s\n", __func__);
1918}
1919
1920/* -------------------------------------------------------------------------- */
1921static int dwc3_cleanup_done_reqs(struct dwc3 *dwc, struct dwc3_ep *dep,
1922 const struct dwc3_event_depevt *event, int status)
1923{
1924 struct dwc3_request *req;
Felipe Balbif6bafc62012-02-06 11:04:53 +02001925 struct dwc3_trb *trb;
Felipe Balbi72246da2011-08-19 18:10:58 +03001926 unsigned int count;
1927 unsigned int s_pkt = 0;
Pratyush Anand73939b02012-05-25 18:54:56 +05301928 unsigned int trb_status;
Felipe Balbi72246da2011-08-19 18:10:58 +03001929
1930 do {
1931 req = next_request(&dep->req_queued);
Sebastian Andrzej Siewiord39ee7b2011-11-03 10:32:20 +01001932 if (!req) {
1933 WARN_ON_ONCE(1);
1934 return 1;
1935 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001936
Felipe Balbif6bafc62012-02-06 11:04:53 +02001937 trb = req->trb;
Felipe Balbi72246da2011-08-19 18:10:58 +03001938
Felipe Balbif6bafc62012-02-06 11:04:53 +02001939 if ((trb->ctrl & DWC3_TRB_CTRL_HWO) && status != -ESHUTDOWN)
Sebastian Andrzej Siewior0d2f4752011-08-19 19:59:12 +02001940 /*
1941 * We continue despite the error. There is not much we
Paul Zimmerman1d046792012-02-15 18:56:56 -08001942 * can do. If we don't clean it up we loop forever. If
1943 * we skip the TRB then it gets overwritten after a
1944 * while since we use them in a ring buffer. A BUG()
1945 * would help. Lets hope that if this occurs, someone
Sebastian Andrzej Siewior0d2f4752011-08-19 19:59:12 +02001946 * fixes the root cause instead of looking away :)
1947 */
Felipe Balbi72246da2011-08-19 18:10:58 +03001948 dev_err(dwc->dev, "%s's TRB (%p) still owned by HW\n",
1949 dep->name, req->trb);
Felipe Balbif6bafc62012-02-06 11:04:53 +02001950 count = trb->size & DWC3_TRB_SIZE_MASK;
Felipe Balbi72246da2011-08-19 18:10:58 +03001951
1952 if (dep->direction) {
1953 if (count) {
Pratyush Anand73939b02012-05-25 18:54:56 +05301954 trb_status = DWC3_TRB_SIZE_TRBSTS(trb->size);
1955 if (trb_status == DWC3_TRBSTS_MISSED_ISOC) {
1956 dev_dbg(dwc->dev, "incomplete IN transfer %s\n",
1957 dep->name);
Pratyush Anand921b0b82013-01-14 15:59:32 +05301958 /*
1959 * If missed isoc occurred and there is
1960 * no request queued then issue END
1961 * TRANSFER, so that core generates
1962 * next xfernotready and we will issue
1963 * a fresh START TRANSFER.
1964 * If there are still queued request
1965 * then wait, do not issue either END
1966 * or UPDATE TRANSFER, just attach next
1967 * request in request_list during
1968 * giveback.If any future queued request
1969 * is successfully transferred then we
1970 * will issue UPDATE TRANSFER for all
1971 * request in the request_list.
1972 */
Pratyush Anand73939b02012-05-25 18:54:56 +05301973 dep->flags |= DWC3_EP_MISSED_ISOC;
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +05301974 dbg_event(dep->number, "MISSED ISOC",
1975 status);
Pratyush Anand73939b02012-05-25 18:54:56 +05301976 } else {
1977 dev_err(dwc->dev, "incomplete IN transfer %s\n",
1978 dep->name);
1979 status = -ECONNRESET;
1980 }
Pratyush Anand921b0b82013-01-14 15:59:32 +05301981 } else {
1982 dep->flags &= ~DWC3_EP_MISSED_ISOC;
Felipe Balbi72246da2011-08-19 18:10:58 +03001983 }
1984 } else {
1985 if (count && (event->status & DEPEVT_STATUS_SHORT))
1986 s_pkt = 1;
1987 }
1988
1989 /*
1990 * We assume here we will always receive the entire data block
1991 * which we should receive. Meaning, if we program RX to
1992 * receive 4K but we receive only 2K, we assume that's all we
1993 * should receive and we simply bounce the request back to the
1994 * gadget driver for further processing.
1995 */
1996 req->request.actual += req->request.length - count;
1997 dwc3_gadget_giveback(dep, req, status);
1998 if (s_pkt)
1999 break;
Felipe Balbif6bafc62012-02-06 11:04:53 +02002000 if ((event->status & DEPEVT_STATUS_LST) &&
Pratyush Anand413dba62012-06-03 19:43:19 +05302001 (trb->ctrl & (DWC3_TRB_CTRL_LST |
2002 DWC3_TRB_CTRL_HWO)))
Felipe Balbi72246da2011-08-19 18:10:58 +03002003 break;
Felipe Balbif6bafc62012-02-06 11:04:53 +02002004 if ((event->status & DEPEVT_STATUS_IOC) &&
2005 (trb->ctrl & DWC3_TRB_CTRL_IOC))
Felipe Balbi72246da2011-08-19 18:10:58 +03002006 break;
2007 } while (1);
2008
Pratyush Anand18bbcb02013-01-14 15:59:34 +05302009 if (usb_endpoint_xfer_isoc(dep->endpoint.desc) &&
2010 list_empty(&dep->req_queued)) {
Vijayavardhan Vennapusa91ba6532013-01-30 17:35:45 +05302011 if (list_empty(&dep->request_list))
Pratyush Anand18bbcb02013-01-14 15:59:34 +05302012 /*
2013 * If there is no entry in request list then do
2014 * not issue END TRANSFER now. Just set PENDING
2015 * flag, so that END TRANSFER is issued when an
2016 * entry is added into request list.
2017 */
Vijayavardhan Vennapusa91ba6532013-01-30 17:35:45 +05302018 dep->flags |= DWC3_EP_PENDING_REQUEST;
2019 else
Pratyush Anand18bbcb02013-01-14 15:59:34 +05302020 dwc3_stop_active_transfer(dwc, dep->number);
Vijayavardhan Vennapusa91ba6532013-01-30 17:35:45 +05302021 dep->flags &= ~DWC3_EP_MISSED_ISOC;
Pratyush Anand921b0b82013-01-14 15:59:32 +05302022 return 1;
2023 }
2024
Felipe Balbif6bafc62012-02-06 11:04:53 +02002025 if ((event->status & DEPEVT_STATUS_IOC) &&
2026 (trb->ctrl & DWC3_TRB_CTRL_IOC))
Felipe Balbi72246da2011-08-19 18:10:58 +03002027 return 0;
2028 return 1;
2029}
2030
2031static void dwc3_endpoint_transfer_complete(struct dwc3 *dwc,
2032 struct dwc3_ep *dep, const struct dwc3_event_depevt *event,
2033 int start_new)
2034{
2035 unsigned status = 0;
2036 int clean_busy;
2037
2038 if (event->status & DEPEVT_STATUS_BUSERR)
2039 status = -ECONNRESET;
2040
Paul Zimmerman1d046792012-02-15 18:56:56 -08002041 clean_busy = dwc3_cleanup_done_reqs(dwc, dep, event, status);
Paul Zimmermanc2df85c2012-02-24 17:32:18 -08002042 if (clean_busy)
Felipe Balbi72246da2011-08-19 18:10:58 +03002043 dep->flags &= ~DWC3_EP_BUSY;
Felipe Balbifae2b902011-10-14 13:00:30 +03002044
2045 /*
2046 * WORKAROUND: This is the 2nd half of U1/U2 -> U0 workaround.
2047 * See dwc3_gadget_linksts_change_interrupt() for 1st half.
2048 */
2049 if (dwc->revision < DWC3_REVISION_183A) {
2050 u32 reg;
2051 int i;
2052
2053 for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) {
Moiz Sonasatheed03f12012-08-01 14:08:30 -05002054 dep = dwc->eps[i];
Felipe Balbifae2b902011-10-14 13:00:30 +03002055
2056 if (!(dep->flags & DWC3_EP_ENABLED))
2057 continue;
2058
2059 if (!list_empty(&dep->req_queued))
2060 return;
2061 }
2062
2063 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2064 reg |= dwc->u1u2;
2065 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2066
2067 dwc->u1u2 = 0;
2068 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002069}
2070
Felipe Balbi72246da2011-08-19 18:10:58 +03002071static void dwc3_endpoint_interrupt(struct dwc3 *dwc,
2072 const struct dwc3_event_depevt *event)
2073{
2074 struct dwc3_ep *dep;
2075 u8 epnum = event->endpoint_number;
2076
2077 dep = dwc->eps[epnum];
2078
Felipe Balbia09be0a2012-06-06 09:19:35 +03002079 if (!(dep->flags & DWC3_EP_ENABLED))
2080 return;
2081
Felipe Balbi72246da2011-08-19 18:10:58 +03002082 dev_vdbg(dwc->dev, "%s: %s\n", dep->name,
2083 dwc3_ep_event_string(event->endpoint_event));
2084
2085 if (epnum == 0 || epnum == 1) {
2086 dwc3_ep0_interrupt(dwc, event);
2087 return;
2088 }
2089
2090 switch (event->endpoint_event) {
2091 case DWC3_DEPEVT_XFERCOMPLETE:
Felipe Balbi4959cfc2012-06-06 12:04:13 +03002092 dep->resource_index = 0;
Paul Zimmermanc2df85c2012-02-24 17:32:18 -08002093
Ido Shayevitz57cdac12012-03-12 20:25:24 +02002094 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbi72246da2011-08-19 18:10:58 +03002095 dev_dbg(dwc->dev, "%s is an Isochronous endpoint\n",
2096 dep->name);
2097 return;
2098 }
2099
2100 dwc3_endpoint_transfer_complete(dwc, dep, event, 1);
2101 break;
2102 case DWC3_DEPEVT_XFERINPROGRESS:
Ido Shayevitz57cdac12012-03-12 20:25:24 +02002103 if (!usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbi72246da2011-08-19 18:10:58 +03002104 dev_dbg(dwc->dev, "%s is not an Isochronous endpoint\n",
2105 dep->name);
2106 return;
2107 }
2108
2109 dwc3_endpoint_transfer_complete(dwc, dep, event, 0);
2110 break;
2111 case DWC3_DEPEVT_XFERNOTREADY:
Ido Shayevitz57cdac12012-03-12 20:25:24 +02002112 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbi72246da2011-08-19 18:10:58 +03002113 dwc3_gadget_start_isoc(dwc, dep, event);
2114 } else {
2115 int ret;
2116
2117 dev_vdbg(dwc->dev, "%s: reason %s\n",
Felipe Balbi40aa41f2012-01-18 17:06:03 +02002118 dep->name, event->status &
2119 DEPEVT_STATUS_TRANSFER_ACTIVE
Felipe Balbi72246da2011-08-19 18:10:58 +03002120 ? "Transfer Active"
2121 : "Transfer Not Active");
2122
2123 ret = __dwc3_gadget_kick_transfer(dep, 0, 1);
2124 if (!ret || ret == -EBUSY)
2125 return;
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +05302126 else
2127 dbg_event(dep->number, "QUEUE", ret);
Felipe Balbi72246da2011-08-19 18:10:58 +03002128
2129 dev_dbg(dwc->dev, "%s: failed to kick transfers\n",
2130 dep->name);
2131 }
2132
2133 break;
Felipe Balbi879631a2011-09-30 10:58:47 +03002134 case DWC3_DEPEVT_STREAMEVT:
Ido Shayevitz57cdac12012-03-12 20:25:24 +02002135 if (!usb_endpoint_xfer_bulk(dep->endpoint.desc)) {
Felipe Balbi879631a2011-09-30 10:58:47 +03002136 dev_err(dwc->dev, "Stream event for non-Bulk %s\n",
2137 dep->name);
2138 return;
2139 }
2140
2141 switch (event->status) {
2142 case DEPEVT_STREAMEVT_FOUND:
2143 dev_vdbg(dwc->dev, "Stream %d found and started\n",
2144 event->parameters);
2145
2146 break;
2147 case DEPEVT_STREAMEVT_NOTFOUND:
2148 /* FALLTHROUGH */
2149 default:
2150 dev_dbg(dwc->dev, "Couldn't find suitable stream\n");
2151 }
2152 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03002153 case DWC3_DEPEVT_RXTXFIFOEVT:
2154 dev_dbg(dwc->dev, "%s FIFO Overrun\n", dep->name);
2155 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03002156 case DWC3_DEPEVT_EPCMDCMPLT:
Felipe Balbib129eb72012-02-17 12:10:04 +02002157 dev_vdbg(dwc->dev, "Endpoint Command Complete\n");
Felipe Balbi72246da2011-08-19 18:10:58 +03002158 break;
2159 }
2160}
2161
2162static void dwc3_disconnect_gadget(struct dwc3 *dwc)
2163{
2164 if (dwc->gadget_driver && dwc->gadget_driver->disconnect) {
2165 spin_unlock(&dwc->lock);
2166 dwc->gadget_driver->disconnect(&dwc->gadget);
2167 spin_lock(&dwc->lock);
2168 }
2169}
2170
2171static void dwc3_stop_active_transfer(struct dwc3 *dwc, u32 epnum)
2172{
2173 struct dwc3_ep *dep;
2174 struct dwc3_gadget_ep_cmd_params params;
2175 u32 cmd;
2176 int ret;
2177
2178 dep = dwc->eps[epnum];
2179
Felipe Balbi4959cfc2012-06-06 12:04:13 +03002180 if (!dep->resource_index)
Pratyush Anand6263ebe2012-06-23 02:23:08 +05302181 return;
2182
Pratyush Anande67fdeb2012-07-06 15:19:10 +05302183 /*
2184 * NOTICE: We are violating what the Databook says about the
2185 * EndTransfer command. Ideally we would _always_ wait for the
2186 * EndTransfer Command Completion IRQ, but that's causing too
2187 * much trouble synchronizing between us and gadget driver.
2188 *
2189 * We have discussed this with the IP Provider and it was
2190 * suggested to giveback all requests here, but give HW some
2191 * extra time to synchronize with the interconnect. We're using
2192 * an arbitraty 100us delay for that.
2193 *
2194 * Note also that a similar handling was tested by Synopsys
2195 * (thanks a lot Paul) and nothing bad has come out of it.
2196 * In short, what we're doing is:
2197 *
2198 * - Issue EndTransfer WITH CMDIOC bit set
2199 * - Wait 100us
2200 */
2201
Pratyush Anand6263ebe2012-06-23 02:23:08 +05302202 cmd = DWC3_DEPCMD_ENDTRANSFER;
2203 cmd |= DWC3_DEPCMD_HIPRI_FORCERM | DWC3_DEPCMD_CMDIOC;
Felipe Balbi4959cfc2012-06-06 12:04:13 +03002204 cmd |= DWC3_DEPCMD_PARAM(dep->resource_index);
Pratyush Anand6263ebe2012-06-23 02:23:08 +05302205 memset(&params, 0, sizeof(params));
2206 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number, cmd, &params);
2207 WARN_ON_ONCE(ret);
Felipe Balbi4959cfc2012-06-06 12:04:13 +03002208 dep->resource_index = 0;
Felipe Balbi1df89b62012-10-04 11:58:00 +03002209 dep->flags &= ~DWC3_EP_BUSY;
Pratyush Anande67fdeb2012-07-06 15:19:10 +05302210 udelay(100);
Felipe Balbi72246da2011-08-19 18:10:58 +03002211}
2212
2213static void dwc3_stop_active_transfers(struct dwc3 *dwc)
2214{
2215 u32 epnum;
2216
2217 for (epnum = 2; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
2218 struct dwc3_ep *dep;
2219
2220 dep = dwc->eps[epnum];
2221 if (!(dep->flags & DWC3_EP_ENABLED))
2222 continue;
2223
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +02002224 dwc3_remove_requests(dwc, dep);
Felipe Balbi72246da2011-08-19 18:10:58 +03002225 }
2226}
2227
2228static void dwc3_clear_stall_all_ep(struct dwc3 *dwc)
2229{
2230 u32 epnum;
2231
2232 for (epnum = 1; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
2233 struct dwc3_ep *dep;
2234 struct dwc3_gadget_ep_cmd_params params;
2235 int ret;
2236
2237 dep = dwc->eps[epnum];
2238
2239 if (!(dep->flags & DWC3_EP_STALL))
2240 continue;
2241
2242 dep->flags &= ~DWC3_EP_STALL;
2243
2244 memset(&params, 0, sizeof(params));
2245 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number,
2246 DWC3_DEPCMD_CLEARSTALL, &params);
2247 WARN_ON_ONCE(ret);
2248 }
2249}
2250
2251static void dwc3_gadget_disconnect_interrupt(struct dwc3 *dwc)
2252{
Felipe Balbi34d548c2012-05-24 10:30:01 +03002253 int reg;
2254
Felipe Balbi72246da2011-08-19 18:10:58 +03002255 dev_vdbg(dwc->dev, "%s\n", __func__);
Felipe Balbi72246da2011-08-19 18:10:58 +03002256
2257 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2258 reg &= ~DWC3_DCTL_INITU1ENA;
2259 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2260
2261 reg &= ~DWC3_DCTL_INITU2ENA;
2262 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +03002263
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +05302264 dbg_event(0xFF, "DISCONNECT", 0);
Felipe Balbi72246da2011-08-19 18:10:58 +03002265 dwc3_disconnect_gadget(dwc);
Paul Zimmermanb23c8432011-09-30 10:58:42 +03002266 dwc->start_config_issued = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03002267
2268 dwc->gadget.speed = USB_SPEED_UNKNOWN;
Felipe Balbidf62df52011-10-14 15:11:49 +03002269 dwc->setup_packet_pending = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03002270}
2271
Paul Zimmermandffb81b2012-04-27 12:54:05 +03002272static void dwc3_gadget_usb3_phy_suspend(struct dwc3 *dwc, int suspend)
Felipe Balbi72246da2011-08-19 18:10:58 +03002273{
2274 u32 reg;
2275
2276 reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0));
2277
Paul Zimmermandffb81b2012-04-27 12:54:05 +03002278 if (suspend)
Felipe Balbi72246da2011-08-19 18:10:58 +03002279 reg |= DWC3_GUSB3PIPECTL_SUSPHY;
Paul Zimmermandffb81b2012-04-27 12:54:05 +03002280 else
2281 reg &= ~DWC3_GUSB3PIPECTL_SUSPHY;
Felipe Balbi72246da2011-08-19 18:10:58 +03002282
2283 dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg);
2284}
2285
Paul Zimmermandffb81b2012-04-27 12:54:05 +03002286static void dwc3_gadget_usb2_phy_suspend(struct dwc3 *dwc, int suspend)
Felipe Balbi72246da2011-08-19 18:10:58 +03002287{
2288 u32 reg;
2289
2290 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
2291
Paul Zimmermandffb81b2012-04-27 12:54:05 +03002292 if (suspend)
Felipe Balbi72246da2011-08-19 18:10:58 +03002293 reg |= DWC3_GUSB2PHYCFG_SUSPHY;
Paul Zimmermandffb81b2012-04-27 12:54:05 +03002294 else
2295 reg &= ~DWC3_GUSB2PHYCFG_SUSPHY;
Felipe Balbi72246da2011-08-19 18:10:58 +03002296
2297 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
2298}
2299
2300static void dwc3_gadget_reset_interrupt(struct dwc3 *dwc)
2301{
2302 u32 reg;
Vijayavardhan Vennapusa908f1ed2012-10-19 19:51:48 +05302303 struct dwc3_otg *dotg = dwc->dotg;
Felipe Balbi72246da2011-08-19 18:10:58 +03002304
2305 dev_vdbg(dwc->dev, "%s\n", __func__);
2306
Felipe Balbidf62df52011-10-14 15:11:49 +03002307 /*
2308 * WORKAROUND: DWC3 revisions <1.88a have an issue which
2309 * would cause a missing Disconnect Event if there's a
2310 * pending Setup Packet in the FIFO.
2311 *
2312 * There's no suggested workaround on the official Bug
2313 * report, which states that "unless the driver/application
2314 * is doing any special handling of a disconnect event,
2315 * there is no functional issue".
2316 *
2317 * Unfortunately, it turns out that we _do_ some special
2318 * handling of a disconnect event, namely complete all
2319 * pending transfers, notify gadget driver of the
2320 * disconnection, and so on.
2321 *
2322 * Our suggested workaround is to follow the Disconnect
2323 * Event steps here, instead, based on a setup_packet_pending
2324 * flag. Such flag gets set whenever we have a XferNotReady
2325 * event on EP0 and gets cleared on XferComplete for the
2326 * same endpoint.
2327 *
2328 * Refers to:
2329 *
2330 * STAR#9000466709: RTL: Device : Disconnect event not
2331 * generated if setup packet pending in FIFO
2332 */
2333 if (dwc->revision < DWC3_REVISION_188A) {
2334 if (dwc->setup_packet_pending)
2335 dwc3_gadget_disconnect_interrupt(dwc);
2336 }
2337
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +05302338 dbg_event(0xFF, "BUS RST", 0);
Felipe Balbi961906e2011-12-20 15:37:21 +02002339 /* after reset -> Default State */
2340 dwc->dev_state = DWC3_DEFAULT_STATE;
2341
Paul Zimmerman88df4272012-04-27 13:10:52 +03002342 /* Recent versions support automatic phy suspend and don't need this */
2343 if (dwc->revision < DWC3_REVISION_194A) {
2344 /* Resume PHYs */
2345 dwc3_gadget_usb2_phy_suspend(dwc, false);
2346 dwc3_gadget_usb3_phy_suspend(dwc, false);
2347 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002348
Vijayavardhan Vennapusa908f1ed2012-10-19 19:51:48 +05302349 if (dotg && dotg->otg.phy)
2350 usb_phy_set_power(dotg->otg.phy, 0);
2351
Felipe Balbi72246da2011-08-19 18:10:58 +03002352 if (dwc->gadget.speed != USB_SPEED_UNKNOWN)
2353 dwc3_disconnect_gadget(dwc);
2354
2355 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2356 reg &= ~DWC3_DCTL_TSTCTRL_MASK;
2357 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Gerard Cauvy3b637362012-02-10 12:21:18 +02002358 dwc->test_mode = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03002359
2360 dwc3_stop_active_transfers(dwc);
2361 dwc3_clear_stall_all_ep(dwc);
Paul Zimmermanb23c8432011-09-30 10:58:42 +03002362 dwc->start_config_issued = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03002363
2364 /* Reset device address to zero */
2365 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2366 reg &= ~(DWC3_DCFG_DEVADDR_MASK);
2367 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +03002368}
2369
2370static void dwc3_update_ram_clk_sel(struct dwc3 *dwc, u32 speed)
2371{
2372 u32 reg;
2373 u32 usb30_clock = DWC3_GCTL_CLK_BUS;
2374
2375 /*
2376 * We change the clock only at SS but I dunno why I would want to do
2377 * this. Maybe it becomes part of the power saving plan.
2378 */
2379
2380 if (speed != DWC3_DSTS_SUPERSPEED)
2381 return;
2382
2383 /*
2384 * RAMClkSel is reset to 0 after USB reset, so it must be reprogrammed
2385 * each time on Connect Done.
2386 */
2387 if (!usb30_clock)
2388 return;
2389
2390 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
2391 reg |= DWC3_GCTL_RAMCLKSEL(usb30_clock);
2392 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
2393}
2394
Paul Zimmermandffb81b2012-04-27 12:54:05 +03002395static void dwc3_gadget_phy_suspend(struct dwc3 *dwc, u8 speed)
Felipe Balbi72246da2011-08-19 18:10:58 +03002396{
2397 switch (speed) {
2398 case USB_SPEED_SUPER:
Paul Zimmermandffb81b2012-04-27 12:54:05 +03002399 dwc3_gadget_usb2_phy_suspend(dwc, true);
Felipe Balbi72246da2011-08-19 18:10:58 +03002400 break;
2401 case USB_SPEED_HIGH:
2402 case USB_SPEED_FULL:
2403 case USB_SPEED_LOW:
Paul Zimmermandffb81b2012-04-27 12:54:05 +03002404 dwc3_gadget_usb3_phy_suspend(dwc, true);
Felipe Balbi72246da2011-08-19 18:10:58 +03002405 break;
2406 }
2407}
2408
2409static void dwc3_gadget_conndone_interrupt(struct dwc3 *dwc)
2410{
2411 struct dwc3_gadget_ep_cmd_params params;
2412 struct dwc3_ep *dep;
2413 int ret;
2414 u32 reg;
2415 u8 speed;
2416
2417 dev_vdbg(dwc->dev, "%s\n", __func__);
2418
2419 memset(&params, 0x00, sizeof(params));
2420
Felipe Balbi72246da2011-08-19 18:10:58 +03002421 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
2422 speed = reg & DWC3_DSTS_CONNECTSPD;
2423 dwc->speed = speed;
2424
2425 dwc3_update_ram_clk_sel(dwc, speed);
2426
2427 switch (speed) {
2428 case DWC3_DCFG_SUPERSPEED:
Felipe Balbi05870c52011-10-14 14:51:38 +03002429 /*
2430 * WORKAROUND: DWC3 revisions <1.90a have an issue which
2431 * would cause a missing USB3 Reset event.
2432 *
2433 * In such situations, we should force a USB3 Reset
2434 * event by calling our dwc3_gadget_reset_interrupt()
2435 * routine.
2436 *
2437 * Refers to:
2438 *
2439 * STAR#9000483510: RTL: SS : USB3 reset event may
2440 * not be generated always when the link enters poll
2441 */
2442 if (dwc->revision < DWC3_REVISION_190A)
2443 dwc3_gadget_reset_interrupt(dwc);
2444
Felipe Balbi72246da2011-08-19 18:10:58 +03002445 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
2446 dwc->gadget.ep0->maxpacket = 512;
2447 dwc->gadget.speed = USB_SPEED_SUPER;
2448 break;
2449 case DWC3_DCFG_HIGHSPEED:
2450 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
2451 dwc->gadget.ep0->maxpacket = 64;
2452 dwc->gadget.speed = USB_SPEED_HIGH;
2453 break;
2454 case DWC3_DCFG_FULLSPEED2:
2455 case DWC3_DCFG_FULLSPEED1:
2456 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
2457 dwc->gadget.ep0->maxpacket = 64;
2458 dwc->gadget.speed = USB_SPEED_FULL;
2459 break;
2460 case DWC3_DCFG_LOWSPEED:
2461 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(8);
2462 dwc->gadget.ep0->maxpacket = 8;
2463 dwc->gadget.speed = USB_SPEED_LOW;
2464 break;
2465 }
2466
Paul Zimmerman88df4272012-04-27 13:10:52 +03002467 /* Recent versions support automatic phy suspend and don't need this */
2468 if (dwc->revision < DWC3_REVISION_194A) {
2469 /* Suspend unneeded PHY */
2470 dwc3_gadget_phy_suspend(dwc, dwc->gadget.speed);
2471 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002472
2473 dep = dwc->eps[0];
Felipe Balbi07e0ee82012-07-16 14:08:16 +03002474 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, true);
Felipe Balbi72246da2011-08-19 18:10:58 +03002475 if (ret) {
2476 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
2477 return;
2478 }
2479
2480 dep = dwc->eps[1];
Felipe Balbi07e0ee82012-07-16 14:08:16 +03002481 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, true);
Felipe Balbi72246da2011-08-19 18:10:58 +03002482 if (ret) {
2483 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
2484 return;
2485 }
2486
2487 /*
2488 * Configure PHY via GUSB3PIPECTLn if required.
2489 *
2490 * Update GTXFIFOSIZn
2491 *
2492 * In both cases reset values should be sufficient.
2493 */
2494}
2495
2496static void dwc3_gadget_wakeup_interrupt(struct dwc3 *dwc)
2497{
2498 dev_vdbg(dwc->dev, "%s\n", __func__);
2499
2500 /*
2501 * TODO take core out of low power mode when that's
2502 * implemented.
2503 */
2504
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +05302505 dbg_event(0xFF, "WAKEUP", 0);
Felipe Balbi72246da2011-08-19 18:10:58 +03002506 dwc->gadget_driver->resume(&dwc->gadget);
2507}
2508
2509static void dwc3_gadget_linksts_change_interrupt(struct dwc3 *dwc,
2510 unsigned int evtinfo)
2511{
Felipe Balbifae2b902011-10-14 13:00:30 +03002512 enum dwc3_link_state next = evtinfo & DWC3_LINK_STATE_MASK;
2513
2514 /*
2515 * WORKAROUND: DWC3 Revisions <1.83a have an issue which, depending
2516 * on the link partner, the USB session might do multiple entry/exit
2517 * of low power states before a transfer takes place.
2518 *
2519 * Due to this problem, we might experience lower throughput. The
2520 * suggested workaround is to disable DCTL[12:9] bits if we're
2521 * transitioning from U1/U2 to U0 and enable those bits again
2522 * after a transfer completes and there are no pending transfers
2523 * on any of the enabled endpoints.
2524 *
2525 * This is the first half of that workaround.
2526 *
2527 * Refers to:
2528 *
2529 * STAR#9000446952: RTL: Device SS : if U1/U2 ->U0 takes >128us
2530 * core send LGO_Ux entering U0
2531 */
2532 if (dwc->revision < DWC3_REVISION_183A) {
2533 if (next == DWC3_LINK_STATE_U0) {
2534 u32 u1u2;
2535 u32 reg;
2536
2537 switch (dwc->link_state) {
2538 case DWC3_LINK_STATE_U1:
2539 case DWC3_LINK_STATE_U2:
2540 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2541 u1u2 = reg & (DWC3_DCTL_INITU2ENA
2542 | DWC3_DCTL_ACCEPTU2ENA
2543 | DWC3_DCTL_INITU1ENA
2544 | DWC3_DCTL_ACCEPTU1ENA);
2545
2546 if (!dwc->u1u2)
2547 dwc->u1u2 = reg & u1u2;
2548
2549 reg &= ~u1u2;
2550
2551 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2552 break;
2553 default:
2554 /* do nothing */
2555 break;
2556 }
2557 }
2558 }
2559
Vijayavardhan Vennapusa54be1d62012-10-06 18:32:06 +05302560 if (next == DWC3_LINK_STATE_U0) {
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +05302561 if (dwc->link_state == DWC3_LINK_STATE_U3) {
2562 dbg_event(0xFF, "RESUME", 0);
Vijayavardhan Vennapusa54be1d62012-10-06 18:32:06 +05302563 dwc->gadget_driver->resume(&dwc->gadget);
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +05302564 }
Vijayavardhan Vennapusa54be1d62012-10-06 18:32:06 +05302565 } else if (next == DWC3_LINK_STATE_U3) {
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +05302566 dbg_event(0xFF, "SUSPEND", 0);
Vijayavardhan Vennapusa54be1d62012-10-06 18:32:06 +05302567 dwc->gadget_driver->suspend(&dwc->gadget);
2568 }
2569
Felipe Balbifae2b902011-10-14 13:00:30 +03002570 dwc->link_state = next;
Felipe Balbi019ac832011-09-08 21:18:47 +03002571
2572 dev_vdbg(dwc->dev, "%s link %d\n", __func__, dwc->link_state);
Felipe Balbi72246da2011-08-19 18:10:58 +03002573}
2574
2575static void dwc3_gadget_interrupt(struct dwc3 *dwc,
2576 const struct dwc3_event_devt *event)
2577{
2578 switch (event->type) {
2579 case DWC3_DEVICE_EVENT_DISCONNECT:
2580 dwc3_gadget_disconnect_interrupt(dwc);
2581 break;
2582 case DWC3_DEVICE_EVENT_RESET:
2583 dwc3_gadget_reset_interrupt(dwc);
2584 break;
2585 case DWC3_DEVICE_EVENT_CONNECT_DONE:
2586 dwc3_gadget_conndone_interrupt(dwc);
2587 break;
2588 case DWC3_DEVICE_EVENT_WAKEUP:
2589 dwc3_gadget_wakeup_interrupt(dwc);
2590 break;
2591 case DWC3_DEVICE_EVENT_LINK_STATUS_CHANGE:
2592 dwc3_gadget_linksts_change_interrupt(dwc, event->event_info);
2593 break;
2594 case DWC3_DEVICE_EVENT_EOPF:
2595 dev_vdbg(dwc->dev, "End of Periodic Frame\n");
2596 break;
2597 case DWC3_DEVICE_EVENT_SOF:
2598 dev_vdbg(dwc->dev, "Start of Periodic Frame\n");
2599 break;
2600 case DWC3_DEVICE_EVENT_ERRATIC_ERROR:
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +05302601 dbg_event(0xFF, "ERROR", 0);
Felipe Balbi72246da2011-08-19 18:10:58 +03002602 dev_vdbg(dwc->dev, "Erratic Error\n");
2603 break;
2604 case DWC3_DEVICE_EVENT_CMD_CMPL:
2605 dev_vdbg(dwc->dev, "Command Complete\n");
2606 break;
2607 case DWC3_DEVICE_EVENT_OVERFLOW:
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +05302608 dbg_event(0xFF, "OVERFL", 0);
Felipe Balbi72246da2011-08-19 18:10:58 +03002609 dev_vdbg(dwc->dev, "Overflow\n");
Pavankumar Kondetid393e172012-06-12 16:07:29 +05302610 /*
2611 * Controllers prior to 2.30a revision has a bug where
2612 * Overflow Event may overwrite an unacknowledged event
2613 * in the event buffer. The severity of the issue depends
2614 * on the overwritten event type. Add a warning message
2615 * saying that an event is overwritten.
2616 *
2617 * TODO: In future we may need to see if we can re-enumerate
2618 * with host.
2619 */
2620 if (dwc->revision < DWC3_REVISION_230A)
2621 dev_warn(dwc->dev, "Unacknowledged event overwritten\n");
Felipe Balbi72246da2011-08-19 18:10:58 +03002622 break;
Pavankumar Kondeti33fe6f12012-06-12 16:21:46 +05302623 case DWC3_DEVICE_EVENT_VENDOR_DEV_TEST_LMP:
2624 /*
2625 * Controllers prior to 2.30a revision has a bug, due to which
2626 * a vendor device test LMP event can not be filtered. But
2627 * this event is not handled in the current code. This is a
2628 * special event and 8 bytes of data will follow the event.
2629 * Handling this event is tricky when event buffer is almost
2630 * full. Moreover this event will not occur in normal scenario
2631 * and can only happen with special hosts in testing scenarios.
2632 * Add a warning message to indicate that this event is received
2633 * which means that event buffer might have corrupted.
2634 */
Vijayavardhan Vennapusaffeb26b2013-02-14 16:33:30 +05302635 dbg_event(0xFF, "TSTLMP", 0);
Pavankumar Kondeti33fe6f12012-06-12 16:21:46 +05302636 if (dwc->revision < DWC3_REVISION_230A)
2637 dev_warn(dwc->dev, "Vendor Device Test LMP Received\n");
2638 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03002639 default:
2640 dev_dbg(dwc->dev, "UNKNOWN IRQ %d\n", event->type);
2641 }
2642}
2643
2644static void dwc3_process_event_entry(struct dwc3 *dwc,
2645 const union dwc3_event *event)
2646{
2647 /* Endpoint IRQ, handle it and return early */
2648 if (event->type.is_devspec == 0) {
2649 /* depevt */
2650 return dwc3_endpoint_interrupt(dwc, &event->depevt);
2651 }
2652
2653 switch (event->type.type) {
2654 case DWC3_EVENT_TYPE_DEV:
2655 dwc3_gadget_interrupt(dwc, &event->devt);
2656 break;
2657 /* REVISIT what to do with Carkit and I2C events ? */
2658 default:
2659 dev_err(dwc->dev, "UNKNOWN IRQ type %d\n", event->raw);
2660 }
2661}
2662
2663static irqreturn_t dwc3_process_event_buf(struct dwc3 *dwc, u32 buf)
2664{
2665 struct dwc3_event_buffer *evt;
2666 int left;
2667 u32 count;
2668
2669 count = dwc3_readl(dwc->regs, DWC3_GEVNTCOUNT(buf));
2670 count &= DWC3_GEVNTCOUNT_MASK;
2671 if (!count)
2672 return IRQ_NONE;
2673
2674 evt = dwc->ev_buffs[buf];
2675 left = count;
2676
2677 while (left > 0) {
2678 union dwc3_event event;
2679
Felipe Balbid70d8442012-02-06 13:40:17 +02002680 event.raw = *(u32 *) (evt->buf + evt->lpos);
2681
Felipe Balbi72246da2011-08-19 18:10:58 +03002682 dwc3_process_event_entry(dwc, &event);
2683 /*
2684 * XXX we wrap around correctly to the next entry as almost all
2685 * entries are 4 bytes in size. There is one entry which has 12
2686 * bytes which is a regular entry followed by 8 bytes data. ATM
2687 * I don't know how things are organized if were get next to the
2688 * a boundary so I worry about that once we try to handle that.
2689 */
2690 evt->lpos = (evt->lpos + 4) % DWC3_EVENT_BUFFERS_SIZE;
2691 left -= 4;
2692
2693 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(buf), 4);
2694 }
2695
2696 return IRQ_HANDLED;
2697}
2698
2699static irqreturn_t dwc3_interrupt(int irq, void *_dwc)
2700{
2701 struct dwc3 *dwc = _dwc;
2702 int i;
2703 irqreturn_t ret = IRQ_NONE;
2704
2705 spin_lock(&dwc->lock);
2706
Felipe Balbi9f622b22011-10-12 10:31:04 +03002707 for (i = 0; i < dwc->num_event_buffers; i++) {
Felipe Balbi72246da2011-08-19 18:10:58 +03002708 irqreturn_t status;
2709
2710 status = dwc3_process_event_buf(dwc, i);
2711 if (status == IRQ_HANDLED)
2712 ret = status;
2713 }
2714
2715 spin_unlock(&dwc->lock);
2716
2717 return ret;
2718}
2719
2720/**
2721 * dwc3_gadget_init - Initializes gadget related registers
Paul Zimmerman1d046792012-02-15 18:56:56 -08002722 * @dwc: pointer to our controller context structure
Felipe Balbi72246da2011-08-19 18:10:58 +03002723 *
2724 * Returns 0 on success otherwise negative errno.
2725 */
2726int __devinit dwc3_gadget_init(struct dwc3 *dwc)
2727{
2728 u32 reg;
2729 int ret;
2730 int irq;
2731
2732 dwc->ctrl_req = dma_alloc_coherent(dwc->dev, sizeof(*dwc->ctrl_req),
2733 &dwc->ctrl_req_addr, GFP_KERNEL);
2734 if (!dwc->ctrl_req) {
2735 dev_err(dwc->dev, "failed to allocate ctrl request\n");
2736 ret = -ENOMEM;
2737 goto err0;
2738 }
2739
2740 dwc->ep0_trb = dma_alloc_coherent(dwc->dev, sizeof(*dwc->ep0_trb),
2741 &dwc->ep0_trb_addr, GFP_KERNEL);
2742 if (!dwc->ep0_trb) {
2743 dev_err(dwc->dev, "failed to allocate ep0 trb\n");
2744 ret = -ENOMEM;
2745 goto err1;
2746 }
2747
Felipe Balbib0791fb2012-05-04 12:58:14 +03002748 dwc->setup_buf = kzalloc(DWC3_EP0_BOUNCE_SIZE, GFP_KERNEL);
Felipe Balbi72246da2011-08-19 18:10:58 +03002749 if (!dwc->setup_buf) {
2750 dev_err(dwc->dev, "failed to allocate setup buffer\n");
2751 ret = -ENOMEM;
2752 goto err2;
2753 }
2754
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002755 dwc->ep0_bounce = dma_alloc_coherent(dwc->dev,
Felipe Balbib0791fb2012-05-04 12:58:14 +03002756 DWC3_EP0_BOUNCE_SIZE, &dwc->ep0_bounce_addr,
2757 GFP_KERNEL);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002758 if (!dwc->ep0_bounce) {
2759 dev_err(dwc->dev, "failed to allocate ep0 bounce buffer\n");
2760 ret = -ENOMEM;
2761 goto err3;
2762 }
2763
Felipe Balbi72246da2011-08-19 18:10:58 +03002764 dev_set_name(&dwc->gadget.dev, "gadget");
2765
2766 dwc->gadget.ops = &dwc3_gadget_ops;
Manu Gautama7b082a2012-11-06 09:50:09 +05302767 dwc->gadget.max_speed = USB_SPEED_SUPER;
Felipe Balbi72246da2011-08-19 18:10:58 +03002768 dwc->gadget.speed = USB_SPEED_UNKNOWN;
2769 dwc->gadget.dev.parent = dwc->dev;
Felipe Balbieeb720f2011-11-28 12:46:59 +02002770 dwc->gadget.sg_supported = true;
Felipe Balbi72246da2011-08-19 18:10:58 +03002771
2772 dma_set_coherent_mask(&dwc->gadget.dev, dwc->dev->coherent_dma_mask);
2773
2774 dwc->gadget.dev.dma_parms = dwc->dev->dma_parms;
2775 dwc->gadget.dev.dma_mask = dwc->dev->dma_mask;
2776 dwc->gadget.dev.release = dwc3_gadget_release;
2777 dwc->gadget.name = "dwc3-gadget";
2778
2779 /*
2780 * REVISIT: Here we should clear all pending IRQs to be
2781 * sure we're starting from a well known location.
2782 */
2783
2784 ret = dwc3_gadget_init_endpoints(dwc);
2785 if (ret)
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002786 goto err4;
Felipe Balbi72246da2011-08-19 18:10:58 +03002787
2788 irq = platform_get_irq(to_platform_device(dwc->dev), 0);
2789
2790 ret = request_irq(irq, dwc3_interrupt, IRQF_SHARED,
2791 "dwc3", dwc);
2792 if (ret) {
2793 dev_err(dwc->dev, "failed to request irq #%d --> %d\n",
2794 irq, ret);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002795 goto err5;
Felipe Balbi72246da2011-08-19 18:10:58 +03002796 }
2797
Sebastian Andrzej Siewiorbb8b8a32011-09-13 17:54:39 +02002798 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2799 reg |= DWC3_DCFG_LPM_CAP;
2800 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
2801
Felipe Balbi72246da2011-08-19 18:10:58 +03002802 /* Enable all but Start and End of Frame IRQs */
Pavankumar Kondeti33fe6f12012-06-12 16:21:46 +05302803 reg = (DWC3_DEVTEN_EVNTOVERFLOWEN |
Felipe Balbi72246da2011-08-19 18:10:58 +03002804 DWC3_DEVTEN_CMDCMPLTEN |
2805 DWC3_DEVTEN_ERRTICERREN |
2806 DWC3_DEVTEN_WKUPEVTEN |
2807 DWC3_DEVTEN_ULSTCNGEN |
2808 DWC3_DEVTEN_CONNECTDONEEN |
2809 DWC3_DEVTEN_USBRSTEN |
2810 DWC3_DEVTEN_DISCONNEVTEN);
2811 dwc3_writel(dwc->regs, DWC3_DEVTEN, reg);
2812
Paul Zimmerman88df4272012-04-27 13:10:52 +03002813 /* Enable USB2 LPM and automatic phy suspend only on recent versions */
2814 if (dwc->revision >= DWC3_REVISION_194A) {
2815 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2816 reg |= DWC3_DCFG_LPM_CAP;
2817 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
2818
2819 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2820 reg &= ~(DWC3_DCTL_HIRD_THRES_MASK | DWC3_DCTL_L1_HIBER_EN);
2821
2822 /* TODO: This should be configurable */
Pratyush Anandd69dcdd2012-07-02 10:21:52 +05302823 reg |= DWC3_DCTL_HIRD_THRES(28);
Paul Zimmerman88df4272012-04-27 13:10:52 +03002824
2825 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2826
Pratyush Anand50ed8342012-06-06 19:36:17 +05302827 dwc3_gadget_usb2_phy_suspend(dwc, false);
2828 dwc3_gadget_usb3_phy_suspend(dwc, false);
Paul Zimmerman88df4272012-04-27 13:10:52 +03002829 }
2830
Felipe Balbi72246da2011-08-19 18:10:58 +03002831 ret = device_register(&dwc->gadget.dev);
2832 if (ret) {
2833 dev_err(dwc->dev, "failed to register gadget device\n");
2834 put_device(&dwc->gadget.dev);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002835 goto err6;
Felipe Balbi72246da2011-08-19 18:10:58 +03002836 }
2837
2838 ret = usb_add_gadget_udc(dwc->dev, &dwc->gadget);
2839 if (ret) {
2840 dev_err(dwc->dev, "failed to register udc\n");
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002841 goto err7;
Felipe Balbi72246da2011-08-19 18:10:58 +03002842 }
2843
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +02002844 if (dwc->dotg) {
2845 /* dwc3 otg driver is active (DRD mode + SRPSupport=1) */
2846 ret = otg_set_peripheral(&dwc->dotg->otg, &dwc->gadget);
2847 if (ret) {
2848 dev_err(dwc->dev, "failed to set peripheral to otg\n");
2849 goto err7;
2850 }
Manu Gautamb5067272012-07-02 09:53:41 +05302851 } else {
2852 pm_runtime_no_callbacks(&dwc->gadget.dev);
2853 pm_runtime_set_active(&dwc->gadget.dev);
2854 pm_runtime_enable(&dwc->gadget.dev);
2855 pm_runtime_get(&dwc->gadget.dev);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +02002856 }
2857
Felipe Balbi72246da2011-08-19 18:10:58 +03002858 return 0;
2859
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002860err7:
Felipe Balbi72246da2011-08-19 18:10:58 +03002861 device_unregister(&dwc->gadget.dev);
2862
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002863err6:
Felipe Balbi72246da2011-08-19 18:10:58 +03002864 dwc3_writel(dwc->regs, DWC3_DEVTEN, 0x00);
2865 free_irq(irq, dwc);
2866
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002867err5:
Felipe Balbi72246da2011-08-19 18:10:58 +03002868 dwc3_gadget_free_endpoints(dwc);
2869
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002870err4:
Felipe Balbib0791fb2012-05-04 12:58:14 +03002871 dma_free_coherent(dwc->dev, DWC3_EP0_BOUNCE_SIZE,
2872 dwc->ep0_bounce, dwc->ep0_bounce_addr);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002873
Felipe Balbi72246da2011-08-19 18:10:58 +03002874err3:
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02002875 kfree(dwc->setup_buf);
Felipe Balbi72246da2011-08-19 18:10:58 +03002876
2877err2:
2878 dma_free_coherent(dwc->dev, sizeof(*dwc->ep0_trb),
2879 dwc->ep0_trb, dwc->ep0_trb_addr);
2880
2881err1:
2882 dma_free_coherent(dwc->dev, sizeof(*dwc->ctrl_req),
2883 dwc->ctrl_req, dwc->ctrl_req_addr);
2884
2885err0:
2886 return ret;
2887}
2888
2889void dwc3_gadget_exit(struct dwc3 *dwc)
2890{
2891 int irq;
Felipe Balbi72246da2011-08-19 18:10:58 +03002892
Manu Gautamb5067272012-07-02 09:53:41 +05302893 if (dwc->dotg) {
2894 pm_runtime_put(&dwc->gadget.dev);
2895 pm_runtime_disable(&dwc->gadget.dev);
2896 }
2897
Felipe Balbi72246da2011-08-19 18:10:58 +03002898 usb_del_gadget_udc(&dwc->gadget);
2899 irq = platform_get_irq(to_platform_device(dwc->dev), 0);
2900
2901 dwc3_writel(dwc->regs, DWC3_DEVTEN, 0x00);
2902 free_irq(irq, dwc);
2903
Felipe Balbi72246da2011-08-19 18:10:58 +03002904 dwc3_gadget_free_endpoints(dwc);
2905
Felipe Balbib0791fb2012-05-04 12:58:14 +03002906 dma_free_coherent(dwc->dev, DWC3_EP0_BOUNCE_SIZE,
2907 dwc->ep0_bounce, dwc->ep0_bounce_addr);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002908
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02002909 kfree(dwc->setup_buf);
Felipe Balbi72246da2011-08-19 18:10:58 +03002910
2911 dma_free_coherent(dwc->dev, sizeof(*dwc->ep0_trb),
2912 dwc->ep0_trb, dwc->ep0_trb_addr);
2913
2914 dma_free_coherent(dwc->dev, sizeof(*dwc->ctrl_req),
2915 dwc->ctrl_req, dwc->ctrl_req_addr);
2916
2917 device_unregister(&dwc->gadget.dev);
2918}