blob: e0b7910509305706d5aa963ec16a7932e22f3350 [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 *
Felipe Balbi5945f782013-06-30 14:15:11 +03009 * This program is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 of
11 * the License as published by the Free Software Foundation.
Felipe Balbi72246da2011-08-19 18:10:58 +030012 *
Felipe Balbi5945f782013-06-30 14:15:11 +030013 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
Felipe Balbi72246da2011-08-19 18:10:58 +030017 */
18
19#include <linux/kernel.h>
20#include <linux/delay.h>
21#include <linux/slab.h>
22#include <linux/spinlock.h>
23#include <linux/platform_device.h>
24#include <linux/pm_runtime.h>
25#include <linux/interrupt.h>
26#include <linux/io.h>
27#include <linux/list.h>
28#include <linux/dma-mapping.h>
29
30#include <linux/usb/ch9.h>
31#include <linux/usb/gadget.h>
32
33#include "core.h"
34#include "gadget.h"
35#include "io.h"
36
Felipe Balbi04a9bfc2012-01-02 18:25:43 +020037/**
38 * dwc3_gadget_set_test_mode - Enables USB2 Test Modes
39 * @dwc: pointer to our context structure
40 * @mode: the mode to set (J, K SE0 NAK, Force Enable)
41 *
42 * Caller should take care of locking. This function will
43 * return 0 on success or -EINVAL if wrong Test Selector
44 * is passed
45 */
46int dwc3_gadget_set_test_mode(struct dwc3 *dwc, int mode)
47{
48 u32 reg;
49
50 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
51 reg &= ~DWC3_DCTL_TSTCTRL_MASK;
52
53 switch (mode) {
54 case TEST_J:
55 case TEST_K:
56 case TEST_SE0_NAK:
57 case TEST_PACKET:
58 case TEST_FORCE_EN:
59 reg |= mode << 1;
60 break;
61 default:
62 return -EINVAL;
63 }
64
65 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
66
67 return 0;
68}
69
Felipe Balbi8598bde2012-01-02 18:55:57 +020070/**
Paul Zimmerman911f1f82012-04-27 13:35:15 +030071 * dwc3_gadget_get_link_state - Gets current state of USB Link
72 * @dwc: pointer to our context structure
73 *
74 * Caller should take care of locking. This function will
75 * return the link state on success (>= 0) or -ETIMEDOUT.
76 */
77int dwc3_gadget_get_link_state(struct dwc3 *dwc)
78{
79 u32 reg;
80
81 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
82
83 return DWC3_DSTS_USBLNKST(reg);
84}
85
86/**
Felipe Balbi8598bde2012-01-02 18:55:57 +020087 * dwc3_gadget_set_link_state - Sets USB Link to a particular State
88 * @dwc: pointer to our context structure
89 * @state: the state to put link into
90 *
91 * Caller should take care of locking. This function will
Paul Zimmermanaee63e32012-02-24 17:32:15 -080092 * return 0 on success or -ETIMEDOUT.
Felipe Balbi8598bde2012-01-02 18:55:57 +020093 */
94int dwc3_gadget_set_link_state(struct dwc3 *dwc, enum dwc3_link_state state)
95{
Paul Zimmermanaee63e32012-02-24 17:32:15 -080096 int retries = 10000;
Felipe Balbi8598bde2012-01-02 18:55:57 +020097 u32 reg;
98
Paul Zimmerman802fde92012-04-27 13:10:52 +030099 /*
100 * Wait until device controller is ready. Only applies to 1.94a and
101 * later RTL.
102 */
103 if (dwc->revision >= DWC3_REVISION_194A) {
104 while (--retries) {
105 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
106 if (reg & DWC3_DSTS_DCNRD)
107 udelay(5);
108 else
109 break;
110 }
111
112 if (retries <= 0)
113 return -ETIMEDOUT;
114 }
115
Felipe Balbi8598bde2012-01-02 18:55:57 +0200116 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
117 reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK;
118
119 /* set requested state */
120 reg |= DWC3_DCTL_ULSTCHNGREQ(state);
121 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
122
Paul Zimmerman802fde92012-04-27 13:10:52 +0300123 /*
124 * The following code is racy when called from dwc3_gadget_wakeup,
125 * and is not needed, at least on newer versions
126 */
127 if (dwc->revision >= DWC3_REVISION_194A)
128 return 0;
129
Felipe Balbi8598bde2012-01-02 18:55:57 +0200130 /* wait for a change in DSTS */
Paul Zimmermanaed430e2012-04-27 12:52:01 +0300131 retries = 10000;
Felipe Balbi8598bde2012-01-02 18:55:57 +0200132 while (--retries) {
133 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
134
Felipe Balbi8598bde2012-01-02 18:55:57 +0200135 if (DWC3_DSTS_USBLNKST(reg) == state)
136 return 0;
137
Paul Zimmermanaee63e32012-02-24 17:32:15 -0800138 udelay(5);
Felipe Balbi8598bde2012-01-02 18:55:57 +0200139 }
140
141 dev_vdbg(dwc->dev, "link state change request timed out\n");
142
143 return -ETIMEDOUT;
144}
145
Felipe Balbi457e84b2012-01-18 18:04:09 +0200146/**
147 * dwc3_gadget_resize_tx_fifos - reallocate fifo spaces for current use-case
148 * @dwc: pointer to our context structure
149 *
150 * This function will a best effort FIFO allocation in order
151 * to improve FIFO usage and throughput, while still allowing
152 * us to enable as many endpoints as possible.
153 *
154 * Keep in mind that this operation will be highly dependent
155 * on the configured size for RAM1 - which contains TxFifo -,
156 * the amount of endpoints enabled on coreConsultant tool, and
157 * the width of the Master Bus.
158 *
159 * In the ideal world, we would always be able to satisfy the
160 * following equation:
161 *
162 * ((512 + 2 * MDWIDTH-Bytes) + (Number of IN Endpoints - 1) * \
163 * (3 * (1024 + MDWIDTH-Bytes) + MDWIDTH-Bytes)) / MDWIDTH-Bytes
164 *
165 * Unfortunately, due to many variables that's not always the case.
166 */
167int dwc3_gadget_resize_tx_fifos(struct dwc3 *dwc)
168{
169 int last_fifo_depth = 0;
170 int ram1_depth;
171 int fifo_size;
172 int mdwidth;
173 int num;
174
175 if (!dwc->needs_fifo_resize)
176 return 0;
177
178 ram1_depth = DWC3_RAM1_DEPTH(dwc->hwparams.hwparams7);
179 mdwidth = DWC3_MDWIDTH(dwc->hwparams.hwparams0);
180
181 /* MDWIDTH is represented in bits, we need it in bytes */
182 mdwidth >>= 3;
183
184 /*
185 * FIXME For now we will only allocate 1 wMaxPacketSize space
186 * for each enabled endpoint, later patches will come to
187 * improve this algorithm so that we better use the internal
188 * FIFO space
189 */
190 for (num = 0; num < DWC3_ENDPOINTS_NUM; num++) {
191 struct dwc3_ep *dep = dwc->eps[num];
192 int fifo_number = dep->number >> 1;
Felipe Balbi2e81c362012-02-02 13:01:12 +0200193 int mult = 1;
Felipe Balbi457e84b2012-01-18 18:04:09 +0200194 int tmp;
195
196 if (!(dep->number & 1))
197 continue;
198
199 if (!(dep->flags & DWC3_EP_ENABLED))
200 continue;
201
Ido Shayevitz16e78db2012-03-12 20:25:24 +0200202 if (usb_endpoint_xfer_bulk(dep->endpoint.desc)
203 || usb_endpoint_xfer_isoc(dep->endpoint.desc))
Felipe Balbi2e81c362012-02-02 13:01:12 +0200204 mult = 3;
205
206 /*
207 * REVISIT: the following assumes we will always have enough
208 * space available on the FIFO RAM for all possible use cases.
209 * Make sure that's true somehow and change FIFO allocation
210 * accordingly.
211 *
212 * If we have Bulk or Isochronous endpoints, we want
213 * them to be able to be very, very fast. So we're giving
214 * those endpoints a fifo_size which is enough for 3 full
215 * packets
216 */
217 tmp = mult * (dep->endpoint.maxpacket + mdwidth);
Felipe Balbi457e84b2012-01-18 18:04:09 +0200218 tmp += mdwidth;
219
220 fifo_size = DIV_ROUND_UP(tmp, mdwidth);
Felipe Balbi2e81c362012-02-02 13:01:12 +0200221
Felipe Balbi457e84b2012-01-18 18:04:09 +0200222 fifo_size |= (last_fifo_depth << 16);
223
224 dev_vdbg(dwc->dev, "%s: Fifo Addr %04x Size %d\n",
225 dep->name, last_fifo_depth, fifo_size & 0xffff);
226
227 dwc3_writel(dwc->regs, DWC3_GTXFIFOSIZ(fifo_number),
228 fifo_size);
229
230 last_fifo_depth += (fifo_size & 0xffff);
231 }
232
233 return 0;
234}
235
Felipe Balbi72246da2011-08-19 18:10:58 +0300236void dwc3_gadget_giveback(struct dwc3_ep *dep, struct dwc3_request *req,
237 int status)
238{
239 struct dwc3 *dwc = dep->dwc;
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530240 int i;
Felipe Balbi72246da2011-08-19 18:10:58 +0300241
242 if (req->queued) {
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530243 i = 0;
244 do {
Felipe Balbieeb720f2011-11-28 12:46:59 +0200245 dep->busy_slot++;
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530246 /*
247 * Skip LINK TRB. We can't use req->trb and check for
248 * DWC3_TRBCTL_LINK_TRB because it points the TRB we
249 * just completed (not the LINK TRB).
250 */
251 if (((dep->busy_slot & DWC3_TRB_MASK) ==
252 DWC3_TRB_NUM- 1) &&
Ido Shayevitz16e78db2012-03-12 20:25:24 +0200253 usb_endpoint_xfer_isoc(dep->endpoint.desc))
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530254 dep->busy_slot++;
255 } while(++i < req->request.num_mapped_sgs);
Pratyush Anandc9fda7d2013-01-14 15:59:38 +0530256 req->queued = false;
Felipe Balbi72246da2011-08-19 18:10:58 +0300257 }
258 list_del(&req->list);
Felipe Balbieeb720f2011-11-28 12:46:59 +0200259 req->trb = NULL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300260
261 if (req->request.status == -EINPROGRESS)
262 req->request.status = status;
263
Pratyush Anand0416e492012-08-10 13:42:16 +0530264 if (dwc->ep0_bounced && dep->number == 0)
265 dwc->ep0_bounced = false;
266 else
267 usb_gadget_unmap_request(&dwc->gadget, &req->request,
268 req->direction);
Felipe Balbi72246da2011-08-19 18:10:58 +0300269
270 dev_dbg(dwc->dev, "request %p from %s completed %d/%d ===> %d\n",
271 req, dep->name, req->request.actual,
272 req->request.length, status);
273
274 spin_unlock(&dwc->lock);
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +0200275 req->request.complete(&dep->endpoint, &req->request);
Felipe Balbi72246da2011-08-19 18:10:58 +0300276 spin_lock(&dwc->lock);
277}
278
279static const char *dwc3_gadget_ep_cmd_string(u8 cmd)
280{
281 switch (cmd) {
282 case DWC3_DEPCMD_DEPSTARTCFG:
283 return "Start New Configuration";
284 case DWC3_DEPCMD_ENDTRANSFER:
285 return "End Transfer";
286 case DWC3_DEPCMD_UPDATETRANSFER:
287 return "Update Transfer";
288 case DWC3_DEPCMD_STARTTRANSFER:
289 return "Start Transfer";
290 case DWC3_DEPCMD_CLEARSTALL:
291 return "Clear Stall";
292 case DWC3_DEPCMD_SETSTALL:
293 return "Set Stall";
Paul Zimmerman802fde92012-04-27 13:10:52 +0300294 case DWC3_DEPCMD_GETEPSTATE:
295 return "Get Endpoint State";
Felipe Balbi72246da2011-08-19 18:10:58 +0300296 case DWC3_DEPCMD_SETTRANSFRESOURCE:
297 return "Set Endpoint Transfer Resource";
298 case DWC3_DEPCMD_SETEPCONFIG:
299 return "Set Endpoint Configuration";
300 default:
301 return "UNKNOWN command";
302 }
303}
304
Felipe Balbi427c3df2014-04-25 14:14:14 -0500305static const char *dwc3_gadget_generic_cmd_string(u8 cmd)
306{
307 switch (cmd) {
308 case DWC3_DGCMD_SET_LMP:
309 return "Set LMP";
310 case DWC3_DGCMD_SET_PERIODIC_PAR:
311 return "Set Periodic Parameters";
312 case DWC3_DGCMD_XMIT_FUNCTION:
313 return "Transmit Function Wake Device Notification";
314 case DWC3_DGCMD_SET_SCRATCHPAD_ADDR_LO:
315 return "Set Scratchpad Buffer Array Address Lo";
316 case DWC3_DGCMD_SET_SCRATCHPAD_ADDR_HI:
317 return "Set Scratchpad Buffer Array Address Hi";
318 case DWC3_DGCMD_SELECTED_FIFO_FLUSH:
319 return "Selected FIFO Flush";
320 case DWC3_DGCMD_ALL_FIFO_FLUSH:
321 return "All FIFO Flush";
322 case DWC3_DGCMD_SET_ENDPOINT_NRDY:
323 return "Set Endpoint NRDY";
324 case DWC3_DGCMD_RUN_SOC_BUS_LOOPBACK:
325 return "Run SoC Bus Loopback Test";
326 default:
327 return "UNKNOWN";
328 }
329}
330
Felipe Balbie57ebc12014-04-22 13:20:12 -0500331static const char *dwc3_gadget_link_string(enum dwc3_link_state link_state)
332{
333 switch (link_state) {
334 case DWC3_LINK_STATE_U0:
335 return "U0";
336 case DWC3_LINK_STATE_U1:
337 return "U1";
338 case DWC3_LINK_STATE_U2:
339 return "U2";
340 case DWC3_LINK_STATE_U3:
341 return "U3";
342 case DWC3_LINK_STATE_SS_DIS:
343 return "SS.Disabled";
344 case DWC3_LINK_STATE_RX_DET:
345 return "RX.Detect";
346 case DWC3_LINK_STATE_SS_INACT:
347 return "SS.Inactive";
348 case DWC3_LINK_STATE_POLL:
349 return "Polling";
350 case DWC3_LINK_STATE_RECOV:
351 return "Recovery";
352 case DWC3_LINK_STATE_HRESET:
353 return "Hot Reset";
354 case DWC3_LINK_STATE_CMPLY:
355 return "Compliance";
356 case DWC3_LINK_STATE_LPBK:
357 return "Loopback";
358 case DWC3_LINK_STATE_RESET:
359 return "Reset";
360 case DWC3_LINK_STATE_RESUME:
361 return "Resume";
362 default:
363 return "UNKNOWN link state\n";
364 }
365}
366
Felipe Balbib09bb642012-04-24 16:19:11 +0300367int dwc3_send_gadget_generic_command(struct dwc3 *dwc, int cmd, u32 param)
368{
369 u32 timeout = 500;
370 u32 reg;
371
Felipe Balbi427c3df2014-04-25 14:14:14 -0500372 dev_vdbg(dwc->dev, "generic cmd '%s' [%d] param %08x\n",
373 dwc3_gadget_generic_cmd_string(cmd), cmd, param);
374
Felipe Balbib09bb642012-04-24 16:19:11 +0300375 dwc3_writel(dwc->regs, DWC3_DGCMDPAR, param);
376 dwc3_writel(dwc->regs, DWC3_DGCMD, cmd | DWC3_DGCMD_CMDACT);
377
378 do {
379 reg = dwc3_readl(dwc->regs, DWC3_DGCMD);
380 if (!(reg & DWC3_DGCMD_CMDACT)) {
381 dev_vdbg(dwc->dev, "Command Complete --> %d\n",
382 DWC3_DGCMD_STATUS(reg));
383 return 0;
384 }
385
386 /*
387 * We can't sleep here, because it's also called from
388 * interrupt context.
389 */
390 timeout--;
391 if (!timeout)
392 return -ETIMEDOUT;
393 udelay(1);
394 } while (1);
395}
396
Felipe Balbi72246da2011-08-19 18:10:58 +0300397int dwc3_send_gadget_ep_cmd(struct dwc3 *dwc, unsigned ep,
398 unsigned cmd, struct dwc3_gadget_ep_cmd_params *params)
399{
400 struct dwc3_ep *dep = dwc->eps[ep];
Sebastian Andrzej Siewior61d58242011-08-29 16:46:38 +0200401 u32 timeout = 500;
Felipe Balbi72246da2011-08-19 18:10:58 +0300402 u32 reg;
403
Felipe Balbi40cc57c2014-04-25 14:10:02 -0500404 dev_vdbg(dwc->dev, "%s: cmd '%s' [%d] params %08x %08x %08x\n",
Felipe Balbi72246da2011-08-19 18:10:58 +0300405 dep->name,
Felipe Balbi40cc57c2014-04-25 14:10:02 -0500406 dwc3_gadget_ep_cmd_string(cmd), cmd, params->param0,
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300407 params->param1, params->param2);
Felipe Balbi72246da2011-08-19 18:10:58 +0300408
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300409 dwc3_writel(dwc->regs, DWC3_DEPCMDPAR0(ep), params->param0);
410 dwc3_writel(dwc->regs, DWC3_DEPCMDPAR1(ep), params->param1);
411 dwc3_writel(dwc->regs, DWC3_DEPCMDPAR2(ep), params->param2);
Felipe Balbi72246da2011-08-19 18:10:58 +0300412
413 dwc3_writel(dwc->regs, DWC3_DEPCMD(ep), cmd | DWC3_DEPCMD_CMDACT);
414 do {
415 reg = dwc3_readl(dwc->regs, DWC3_DEPCMD(ep));
416 if (!(reg & DWC3_DEPCMD_CMDACT)) {
Felipe Balbi164f6e12011-08-27 20:29:58 +0300417 dev_vdbg(dwc->dev, "Command Complete --> %d\n",
418 DWC3_DEPCMD_STATUS(reg));
Felipe Balbi72246da2011-08-19 18:10:58 +0300419 return 0;
420 }
421
422 /*
Felipe Balbi72246da2011-08-19 18:10:58 +0300423 * We can't sleep here, because it is also called from
424 * interrupt context.
425 */
426 timeout--;
427 if (!timeout)
428 return -ETIMEDOUT;
429
Sebastian Andrzej Siewior61d58242011-08-29 16:46:38 +0200430 udelay(1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300431 } while (1);
432}
433
434static dma_addr_t dwc3_trb_dma_offset(struct dwc3_ep *dep,
Felipe Balbif6bafc62012-02-06 11:04:53 +0200435 struct dwc3_trb *trb)
Felipe Balbi72246da2011-08-19 18:10:58 +0300436{
Paul Zimmermanc439ef82011-09-30 10:58:45 +0300437 u32 offset = (char *) trb - (char *) dep->trb_pool;
Felipe Balbi72246da2011-08-19 18:10:58 +0300438
439 return dep->trb_pool_dma + offset;
440}
441
442static int dwc3_alloc_trb_pool(struct dwc3_ep *dep)
443{
444 struct dwc3 *dwc = dep->dwc;
445
446 if (dep->trb_pool)
447 return 0;
448
449 if (dep->number == 0 || dep->number == 1)
450 return 0;
451
452 dep->trb_pool = dma_alloc_coherent(dwc->dev,
453 sizeof(struct dwc3_trb) * DWC3_TRB_NUM,
454 &dep->trb_pool_dma, GFP_KERNEL);
455 if (!dep->trb_pool) {
456 dev_err(dep->dwc->dev, "failed to allocate trb pool for %s\n",
457 dep->name);
458 return -ENOMEM;
459 }
460
461 return 0;
462}
463
464static void dwc3_free_trb_pool(struct dwc3_ep *dep)
465{
466 struct dwc3 *dwc = dep->dwc;
467
468 dma_free_coherent(dwc->dev, sizeof(struct dwc3_trb) * DWC3_TRB_NUM,
469 dep->trb_pool, dep->trb_pool_dma);
470
471 dep->trb_pool = NULL;
472 dep->trb_pool_dma = 0;
473}
474
475static int dwc3_gadget_start_config(struct dwc3 *dwc, struct dwc3_ep *dep)
476{
477 struct dwc3_gadget_ep_cmd_params params;
478 u32 cmd;
479
480 memset(&params, 0x00, sizeof(params));
481
482 if (dep->number != 1) {
483 cmd = DWC3_DEPCMD_DEPSTARTCFG;
484 /* XferRscIdx == 0 for ep0 and 2 for the remaining */
Paul Zimmermanb23c8432011-09-30 10:58:42 +0300485 if (dep->number > 1) {
486 if (dwc->start_config_issued)
487 return 0;
488 dwc->start_config_issued = true;
Felipe Balbi72246da2011-08-19 18:10:58 +0300489 cmd |= DWC3_DEPCMD_PARAM(2);
Paul Zimmermanb23c8432011-09-30 10:58:42 +0300490 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300491
492 return dwc3_send_gadget_ep_cmd(dwc, 0, cmd, &params);
493 }
494
495 return 0;
496}
497
498static int dwc3_gadget_set_ep_config(struct dwc3 *dwc, struct dwc3_ep *dep,
Felipe Balbic90bfae2011-11-29 13:11:21 +0200499 const struct usb_endpoint_descriptor *desc,
Felipe Balbi4b345c92012-07-16 14:08:16 +0300500 const struct usb_ss_ep_comp_descriptor *comp_desc,
Paul Zimmerman265b70a2013-12-19 12:38:49 -0600501 bool ignore, bool restore)
Felipe Balbi72246da2011-08-19 18:10:58 +0300502{
503 struct dwc3_gadget_ep_cmd_params params;
504
505 memset(&params, 0x00, sizeof(params));
506
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300507 params.param0 = DWC3_DEPCFG_EP_TYPE(usb_endpoint_type(desc))
Chanho Parkd2e9a132012-08-31 16:54:07 +0900508 | DWC3_DEPCFG_MAX_PACKET_SIZE(usb_endpoint_maxp(desc));
509
510 /* Burst size is only needed in SuperSpeed mode */
511 if (dwc->gadget.speed == USB_SPEED_SUPER) {
512 u32 burst = dep->endpoint.maxburst - 1;
513
514 params.param0 |= DWC3_DEPCFG_BURST_SIZE(burst);
515 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300516
Felipe Balbi4b345c92012-07-16 14:08:16 +0300517 if (ignore)
518 params.param0 |= DWC3_DEPCFG_IGN_SEQ_NUM;
519
Paul Zimmerman265b70a2013-12-19 12:38:49 -0600520 if (restore) {
521 params.param0 |= DWC3_DEPCFG_ACTION_RESTORE;
522 params.param2 |= dep->saved_state;
523 }
524
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300525 params.param1 = DWC3_DEPCFG_XFER_COMPLETE_EN
526 | DWC3_DEPCFG_XFER_NOT_READY_EN;
Felipe Balbi72246da2011-08-19 18:10:58 +0300527
Felipe Balbi18b7ede2012-01-02 13:35:41 +0200528 if (usb_ss_max_streams(comp_desc) && usb_endpoint_xfer_bulk(desc)) {
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300529 params.param1 |= DWC3_DEPCFG_STREAM_CAPABLE
530 | DWC3_DEPCFG_STREAM_EVENT_EN;
Felipe Balbi879631a2011-09-30 10:58:47 +0300531 dep->stream_capable = true;
532 }
533
Felipe Balbi72246da2011-08-19 18:10:58 +0300534 if (usb_endpoint_xfer_isoc(desc))
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300535 params.param1 |= DWC3_DEPCFG_XFER_IN_PROGRESS_EN;
Felipe Balbi72246da2011-08-19 18:10:58 +0300536
537 /*
538 * We are doing 1:1 mapping for endpoints, meaning
539 * Physical Endpoints 2 maps to Logical Endpoint 2 and
540 * so on. We consider the direction bit as part of the physical
541 * endpoint number. So USB endpoint 0x81 is 0x03.
542 */
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300543 params.param1 |= DWC3_DEPCFG_EP_NUMBER(dep->number);
Felipe Balbi72246da2011-08-19 18:10:58 +0300544
545 /*
546 * We must use the lower 16 TX FIFOs even though
547 * HW might have more
548 */
549 if (dep->direction)
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300550 params.param0 |= DWC3_DEPCFG_FIFO_NUMBER(dep->number >> 1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300551
552 if (desc->bInterval) {
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300553 params.param1 |= DWC3_DEPCFG_BINTERVAL_M1(desc->bInterval - 1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300554 dep->interval = 1 << (desc->bInterval - 1);
555 }
556
557 return dwc3_send_gadget_ep_cmd(dwc, dep->number,
558 DWC3_DEPCMD_SETEPCONFIG, &params);
559}
560
561static int dwc3_gadget_set_xfer_resource(struct dwc3 *dwc, struct dwc3_ep *dep)
562{
563 struct dwc3_gadget_ep_cmd_params params;
564
565 memset(&params, 0x00, sizeof(params));
566
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300567 params.param0 = DWC3_DEPXFERCFG_NUM_XFER_RES(1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300568
569 return dwc3_send_gadget_ep_cmd(dwc, dep->number,
570 DWC3_DEPCMD_SETTRANSFRESOURCE, &params);
571}
572
573/**
574 * __dwc3_gadget_ep_enable - Initializes a HW endpoint
575 * @dep: endpoint to be initialized
576 * @desc: USB Endpoint Descriptor
577 *
578 * Caller should take care of locking
579 */
580static int __dwc3_gadget_ep_enable(struct dwc3_ep *dep,
Felipe Balbic90bfae2011-11-29 13:11:21 +0200581 const struct usb_endpoint_descriptor *desc,
Felipe Balbi4b345c92012-07-16 14:08:16 +0300582 const struct usb_ss_ep_comp_descriptor *comp_desc,
Paul Zimmerman265b70a2013-12-19 12:38:49 -0600583 bool ignore, bool restore)
Felipe Balbi72246da2011-08-19 18:10:58 +0300584{
585 struct dwc3 *dwc = dep->dwc;
586 u32 reg;
587 int ret = -ENOMEM;
588
Felipe Balbiff62d6b2013-07-12 19:09:39 +0300589 dev_vdbg(dwc->dev, "Enabling %s\n", dep->name);
590
Felipe Balbi72246da2011-08-19 18:10:58 +0300591 if (!(dep->flags & DWC3_EP_ENABLED)) {
592 ret = dwc3_gadget_start_config(dwc, dep);
593 if (ret)
594 return ret;
595 }
596
Paul Zimmerman265b70a2013-12-19 12:38:49 -0600597 ret = dwc3_gadget_set_ep_config(dwc, dep, desc, comp_desc, ignore,
598 restore);
Felipe Balbi72246da2011-08-19 18:10:58 +0300599 if (ret)
600 return ret;
601
602 if (!(dep->flags & DWC3_EP_ENABLED)) {
Felipe Balbif6bafc62012-02-06 11:04:53 +0200603 struct dwc3_trb *trb_st_hw;
604 struct dwc3_trb *trb_link;
Felipe Balbi72246da2011-08-19 18:10:58 +0300605
606 ret = dwc3_gadget_set_xfer_resource(dwc, dep);
607 if (ret)
608 return ret;
609
Ido Shayevitz16e78db2012-03-12 20:25:24 +0200610 dep->endpoint.desc = desc;
Felipe Balbic90bfae2011-11-29 13:11:21 +0200611 dep->comp_desc = comp_desc;
Felipe Balbi72246da2011-08-19 18:10:58 +0300612 dep->type = usb_endpoint_type(desc);
613 dep->flags |= DWC3_EP_ENABLED;
614
615 reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
616 reg |= DWC3_DALEPENA_EP(dep->number);
617 dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
618
619 if (!usb_endpoint_xfer_isoc(desc))
620 return 0;
621
622 memset(&trb_link, 0, sizeof(trb_link));
623
Paul Zimmerman1d046792012-02-15 18:56:56 -0800624 /* Link TRB for ISOC. The HWO bit is never reset */
Felipe Balbi72246da2011-08-19 18:10:58 +0300625 trb_st_hw = &dep->trb_pool[0];
626
Felipe Balbif6bafc62012-02-06 11:04:53 +0200627 trb_link = &dep->trb_pool[DWC3_TRB_NUM - 1];
Felipe Balbi72246da2011-08-19 18:10:58 +0300628
Felipe Balbif6bafc62012-02-06 11:04:53 +0200629 trb_link->bpl = lower_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw));
630 trb_link->bph = upper_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw));
631 trb_link->ctrl |= DWC3_TRBCTL_LINK_TRB;
632 trb_link->ctrl |= DWC3_TRB_CTRL_HWO;
Felipe Balbi72246da2011-08-19 18:10:58 +0300633 }
634
635 return 0;
636}
637
Paul Zimmermanb992e682012-04-27 14:17:35 +0300638static void dwc3_stop_active_transfer(struct dwc3 *dwc, u32 epnum, bool force);
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200639static void dwc3_remove_requests(struct dwc3 *dwc, struct dwc3_ep *dep)
Felipe Balbi72246da2011-08-19 18:10:58 +0300640{
641 struct dwc3_request *req;
642
Felipe Balbiea53b882012-02-17 12:10:04 +0200643 if (!list_empty(&dep->req_queued)) {
Paul Zimmermanb992e682012-04-27 14:17:35 +0300644 dwc3_stop_active_transfer(dwc, dep->number, true);
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200645
Pratyush Anand57911502012-07-06 15:19:10 +0530646 /* - giveback all requests to gadget driver */
Pratyush Anand15916332012-06-15 11:54:36 +0530647 while (!list_empty(&dep->req_queued)) {
648 req = next_request(&dep->req_queued);
649
650 dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
651 }
Felipe Balbiea53b882012-02-17 12:10:04 +0200652 }
653
Felipe Balbi72246da2011-08-19 18:10:58 +0300654 while (!list_empty(&dep->request_list)) {
655 req = next_request(&dep->request_list);
656
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200657 dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
Felipe Balbi72246da2011-08-19 18:10:58 +0300658 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300659}
660
661/**
662 * __dwc3_gadget_ep_disable - Disables a HW endpoint
663 * @dep: the endpoint to disable
664 *
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200665 * This function also removes requests which are currently processed ny the
666 * hardware and those which are not yet scheduled.
667 * Caller should take care of locking.
Felipe Balbi72246da2011-08-19 18:10:58 +0300668 */
Felipe Balbi72246da2011-08-19 18:10:58 +0300669static int __dwc3_gadget_ep_disable(struct dwc3_ep *dep)
670{
671 struct dwc3 *dwc = dep->dwc;
672 u32 reg;
673
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200674 dwc3_remove_requests(dwc, dep);
Felipe Balbi72246da2011-08-19 18:10:58 +0300675
Felipe Balbi687ef982014-04-16 10:30:33 -0500676 /* make sure HW endpoint isn't stalled */
677 if (dep->flags & DWC3_EP_STALL)
678 __dwc3_gadget_ep_set_halt(dep, 0);
679
Felipe Balbi72246da2011-08-19 18:10:58 +0300680 reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
681 reg &= ~DWC3_DALEPENA_EP(dep->number);
682 dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
683
Felipe Balbi879631a2011-09-30 10:58:47 +0300684 dep->stream_capable = false;
Ido Shayevitzf9c56cd2012-02-08 13:56:48 +0200685 dep->endpoint.desc = NULL;
Felipe Balbic90bfae2011-11-29 13:11:21 +0200686 dep->comp_desc = NULL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300687 dep->type = 0;
Felipe Balbi879631a2011-09-30 10:58:47 +0300688 dep->flags = 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300689
690 return 0;
691}
692
693/* -------------------------------------------------------------------------- */
694
695static int dwc3_gadget_ep0_enable(struct usb_ep *ep,
696 const struct usb_endpoint_descriptor *desc)
697{
698 return -EINVAL;
699}
700
701static int dwc3_gadget_ep0_disable(struct usb_ep *ep)
702{
703 return -EINVAL;
704}
705
706/* -------------------------------------------------------------------------- */
707
708static int dwc3_gadget_ep_enable(struct usb_ep *ep,
709 const struct usb_endpoint_descriptor *desc)
710{
711 struct dwc3_ep *dep;
712 struct dwc3 *dwc;
713 unsigned long flags;
714 int ret;
715
716 if (!ep || !desc || desc->bDescriptorType != USB_DT_ENDPOINT) {
717 pr_debug("dwc3: invalid parameters\n");
718 return -EINVAL;
719 }
720
721 if (!desc->wMaxPacketSize) {
722 pr_debug("dwc3: missing wMaxPacketSize\n");
723 return -EINVAL;
724 }
725
726 dep = to_dwc3_ep(ep);
727 dwc = dep->dwc;
728
Felipe Balbic6f83f32012-08-15 12:28:29 +0300729 if (dep->flags & DWC3_EP_ENABLED) {
730 dev_WARN_ONCE(dwc->dev, true, "%s is already enabled\n",
731 dep->name);
732 return 0;
733 }
734
Felipe Balbi72246da2011-08-19 18:10:58 +0300735 switch (usb_endpoint_type(desc)) {
736 case USB_ENDPOINT_XFER_CONTROL:
Anton Tikhomirov27a78d62012-02-23 15:38:46 +0900737 strlcat(dep->name, "-control", sizeof(dep->name));
Felipe Balbi72246da2011-08-19 18:10:58 +0300738 break;
739 case USB_ENDPOINT_XFER_ISOC:
Anton Tikhomirov27a78d62012-02-23 15:38:46 +0900740 strlcat(dep->name, "-isoc", sizeof(dep->name));
Felipe Balbi72246da2011-08-19 18:10:58 +0300741 break;
742 case USB_ENDPOINT_XFER_BULK:
Anton Tikhomirov27a78d62012-02-23 15:38:46 +0900743 strlcat(dep->name, "-bulk", sizeof(dep->name));
Felipe Balbi72246da2011-08-19 18:10:58 +0300744 break;
745 case USB_ENDPOINT_XFER_INT:
Anton Tikhomirov27a78d62012-02-23 15:38:46 +0900746 strlcat(dep->name, "-int", sizeof(dep->name));
Felipe Balbi72246da2011-08-19 18:10:58 +0300747 break;
748 default:
749 dev_err(dwc->dev, "invalid endpoint transfer type\n");
750 }
751
Felipe Balbi72246da2011-08-19 18:10:58 +0300752 spin_lock_irqsave(&dwc->lock, flags);
Paul Zimmerman265b70a2013-12-19 12:38:49 -0600753 ret = __dwc3_gadget_ep_enable(dep, desc, ep->comp_desc, false, false);
Felipe Balbi72246da2011-08-19 18:10:58 +0300754 spin_unlock_irqrestore(&dwc->lock, flags);
755
756 return ret;
757}
758
759static int dwc3_gadget_ep_disable(struct usb_ep *ep)
760{
761 struct dwc3_ep *dep;
762 struct dwc3 *dwc;
763 unsigned long flags;
764 int ret;
765
766 if (!ep) {
767 pr_debug("dwc3: invalid parameters\n");
768 return -EINVAL;
769 }
770
771 dep = to_dwc3_ep(ep);
772 dwc = dep->dwc;
773
774 if (!(dep->flags & DWC3_EP_ENABLED)) {
775 dev_WARN_ONCE(dwc->dev, true, "%s is already disabled\n",
776 dep->name);
777 return 0;
778 }
779
780 snprintf(dep->name, sizeof(dep->name), "ep%d%s",
781 dep->number >> 1,
782 (dep->number & 1) ? "in" : "out");
783
784 spin_lock_irqsave(&dwc->lock, flags);
785 ret = __dwc3_gadget_ep_disable(dep);
786 spin_unlock_irqrestore(&dwc->lock, flags);
787
788 return ret;
789}
790
791static struct usb_request *dwc3_gadget_ep_alloc_request(struct usb_ep *ep,
792 gfp_t gfp_flags)
793{
794 struct dwc3_request *req;
795 struct dwc3_ep *dep = to_dwc3_ep(ep);
796 struct dwc3 *dwc = dep->dwc;
797
798 req = kzalloc(sizeof(*req), gfp_flags);
799 if (!req) {
800 dev_err(dwc->dev, "not enough memory\n");
801 return NULL;
802 }
803
804 req->epnum = dep->number;
805 req->dep = dep;
Felipe Balbi72246da2011-08-19 18:10:58 +0300806
807 return &req->request;
808}
809
810static void dwc3_gadget_ep_free_request(struct usb_ep *ep,
811 struct usb_request *request)
812{
813 struct dwc3_request *req = to_dwc3_request(request);
814
815 kfree(req);
816}
817
Felipe Balbic71fc372011-11-22 11:37:34 +0200818/**
819 * dwc3_prepare_one_trb - setup one TRB from one request
820 * @dep: endpoint for which this request is prepared
821 * @req: dwc3_request pointer
822 */
Felipe Balbi68e823e2011-11-28 12:25:01 +0200823static void dwc3_prepare_one_trb(struct dwc3_ep *dep,
Felipe Balbieeb720f2011-11-28 12:46:59 +0200824 struct dwc3_request *req, dma_addr_t dma,
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530825 unsigned length, unsigned last, unsigned chain, unsigned node)
Felipe Balbic71fc372011-11-22 11:37:34 +0200826{
Felipe Balbieeb720f2011-11-28 12:46:59 +0200827 struct dwc3 *dwc = dep->dwc;
Felipe Balbif6bafc62012-02-06 11:04:53 +0200828 struct dwc3_trb *trb;
Felipe Balbic71fc372011-11-22 11:37:34 +0200829
Felipe Balbieeb720f2011-11-28 12:46:59 +0200830 dev_vdbg(dwc->dev, "%s: req %p dma %08llx length %d%s%s\n",
831 dep->name, req, (unsigned long long) dma,
832 length, last ? " last" : "",
833 chain ? " chain" : "");
834
Felipe Balbic71fc372011-11-22 11:37:34 +0200835 /* Skip the LINK-TRB on ISOC */
Pratyush Anand915e2022013-01-14 15:59:35 +0530836 if (((dep->free_slot & DWC3_TRB_MASK) == DWC3_TRB_NUM - 1) &&
Ido Shayevitz16e78db2012-03-12 20:25:24 +0200837 usb_endpoint_xfer_isoc(dep->endpoint.desc))
Pratyush Anand915e2022013-01-14 15:59:35 +0530838 dep->free_slot++;
839
840 trb = &dep->trb_pool[dep->free_slot & DWC3_TRB_MASK];
Felipe Balbic71fc372011-11-22 11:37:34 +0200841
Felipe Balbieeb720f2011-11-28 12:46:59 +0200842 if (!req->trb) {
843 dwc3_gadget_move_request_queued(req);
Felipe Balbif6bafc62012-02-06 11:04:53 +0200844 req->trb = trb;
845 req->trb_dma = dwc3_trb_dma_offset(dep, trb);
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530846 req->start_slot = dep->free_slot & DWC3_TRB_MASK;
Felipe Balbieeb720f2011-11-28 12:46:59 +0200847 }
Felipe Balbic71fc372011-11-22 11:37:34 +0200848
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530849 dep->free_slot++;
850
Felipe Balbif6bafc62012-02-06 11:04:53 +0200851 trb->size = DWC3_TRB_SIZE_LENGTH(length);
852 trb->bpl = lower_32_bits(dma);
853 trb->bph = upper_32_bits(dma);
Felipe Balbic71fc372011-11-22 11:37:34 +0200854
Ido Shayevitz16e78db2012-03-12 20:25:24 +0200855 switch (usb_endpoint_type(dep->endpoint.desc)) {
Felipe Balbic71fc372011-11-22 11:37:34 +0200856 case USB_ENDPOINT_XFER_CONTROL:
Felipe Balbif6bafc62012-02-06 11:04:53 +0200857 trb->ctrl = DWC3_TRBCTL_CONTROL_SETUP;
Felipe Balbic71fc372011-11-22 11:37:34 +0200858 break;
859
860 case USB_ENDPOINT_XFER_ISOC:
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530861 if (!node)
862 trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS_FIRST;
863 else
864 trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS;
Felipe Balbic71fc372011-11-22 11:37:34 +0200865 break;
866
867 case USB_ENDPOINT_XFER_BULK:
868 case USB_ENDPOINT_XFER_INT:
Felipe Balbif6bafc62012-02-06 11:04:53 +0200869 trb->ctrl = DWC3_TRBCTL_NORMAL;
Felipe Balbic71fc372011-11-22 11:37:34 +0200870 break;
871 default:
872 /*
873 * This is only possible with faulty memory because we
874 * checked it already :)
875 */
876 BUG();
877 }
878
Felipe Balbif3af3652013-12-13 14:19:33 -0600879 if (!req->request.no_interrupt && !chain)
880 trb->ctrl |= DWC3_TRB_CTRL_IOC;
881
Ido Shayevitz16e78db2012-03-12 20:25:24 +0200882 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbif6bafc62012-02-06 11:04:53 +0200883 trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI;
884 trb->ctrl |= DWC3_TRB_CTRL_CSP;
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530885 } else if (last) {
886 trb->ctrl |= DWC3_TRB_CTRL_LST;
Felipe Balbif6bafc62012-02-06 11:04:53 +0200887 }
888
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530889 if (chain)
890 trb->ctrl |= DWC3_TRB_CTRL_CHN;
891
Ido Shayevitz16e78db2012-03-12 20:25:24 +0200892 if (usb_endpoint_xfer_bulk(dep->endpoint.desc) && dep->stream_capable)
Felipe Balbif6bafc62012-02-06 11:04:53 +0200893 trb->ctrl |= DWC3_TRB_CTRL_SID_SOFN(req->request.stream_id);
894
895 trb->ctrl |= DWC3_TRB_CTRL_HWO;
Felipe Balbic71fc372011-11-22 11:37:34 +0200896}
897
Felipe Balbi72246da2011-08-19 18:10:58 +0300898/*
899 * dwc3_prepare_trbs - setup TRBs from requests
900 * @dep: endpoint for which requests are being prepared
901 * @starting: true if the endpoint is idle and no requests are queued.
902 *
Paul Zimmerman1d046792012-02-15 18:56:56 -0800903 * The function goes through the requests list and sets up TRBs for the
904 * transfers. The function returns once there are no more TRBs available or
905 * it runs out of requests.
Felipe Balbi72246da2011-08-19 18:10:58 +0300906 */
Felipe Balbi68e823e2011-11-28 12:25:01 +0200907static void dwc3_prepare_trbs(struct dwc3_ep *dep, bool starting)
Felipe Balbi72246da2011-08-19 18:10:58 +0300908{
Felipe Balbi68e823e2011-11-28 12:25:01 +0200909 struct dwc3_request *req, *n;
Felipe Balbi72246da2011-08-19 18:10:58 +0300910 u32 trbs_left;
Paul Zimmerman8d62cd62012-02-15 13:35:06 +0200911 u32 max;
Felipe Balbic71fc372011-11-22 11:37:34 +0200912 unsigned int last_one = 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300913
914 BUILD_BUG_ON_NOT_POWER_OF_2(DWC3_TRB_NUM);
915
916 /* the first request must not be queued */
917 trbs_left = (dep->busy_slot - dep->free_slot) & DWC3_TRB_MASK;
Felipe Balbic71fc372011-11-22 11:37:34 +0200918
Paul Zimmerman8d62cd62012-02-15 13:35:06 +0200919 /* Can't wrap around on a non-isoc EP since there's no link TRB */
Ido Shayevitz16e78db2012-03-12 20:25:24 +0200920 if (!usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Paul Zimmerman8d62cd62012-02-15 13:35:06 +0200921 max = DWC3_TRB_NUM - (dep->free_slot & DWC3_TRB_MASK);
922 if (trbs_left > max)
923 trbs_left = max;
924 }
925
Felipe Balbi72246da2011-08-19 18:10:58 +0300926 /*
Paul Zimmerman1d046792012-02-15 18:56:56 -0800927 * If busy & slot are equal than it is either full or empty. If we are
928 * starting to process requests then we are empty. Otherwise we are
Felipe Balbi72246da2011-08-19 18:10:58 +0300929 * full and don't do anything
930 */
931 if (!trbs_left) {
932 if (!starting)
Felipe Balbi68e823e2011-11-28 12:25:01 +0200933 return;
Felipe Balbi72246da2011-08-19 18:10:58 +0300934 trbs_left = DWC3_TRB_NUM;
935 /*
936 * In case we start from scratch, we queue the ISOC requests
937 * starting from slot 1. This is done because we use ring
938 * buffer and have no LST bit to stop us. Instead, we place
Paul Zimmerman1d046792012-02-15 18:56:56 -0800939 * IOC bit every TRB_NUM/4. We try to avoid having an interrupt
Felipe Balbi72246da2011-08-19 18:10:58 +0300940 * after the first request so we start at slot 1 and have
941 * 7 requests proceed before we hit the first IOC.
942 * Other transfer types don't use the ring buffer and are
943 * processed from the first TRB until the last one. Since we
944 * don't wrap around we have to start at the beginning.
945 */
Ido Shayevitz16e78db2012-03-12 20:25:24 +0200946 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbi72246da2011-08-19 18:10:58 +0300947 dep->busy_slot = 1;
948 dep->free_slot = 1;
949 } else {
950 dep->busy_slot = 0;
951 dep->free_slot = 0;
952 }
953 }
954
955 /* The last TRB is a link TRB, not used for xfer */
Ido Shayevitz16e78db2012-03-12 20:25:24 +0200956 if ((trbs_left <= 1) && usb_endpoint_xfer_isoc(dep->endpoint.desc))
Felipe Balbi68e823e2011-11-28 12:25:01 +0200957 return;
Felipe Balbi72246da2011-08-19 18:10:58 +0300958
959 list_for_each_entry_safe(req, n, &dep->request_list, list) {
Felipe Balbieeb720f2011-11-28 12:46:59 +0200960 unsigned length;
961 dma_addr_t dma;
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530962 last_one = false;
Felipe Balbi72246da2011-08-19 18:10:58 +0300963
Felipe Balbieeb720f2011-11-28 12:46:59 +0200964 if (req->request.num_mapped_sgs > 0) {
965 struct usb_request *request = &req->request;
966 struct scatterlist *sg = request->sg;
967 struct scatterlist *s;
968 int i;
Felipe Balbi72246da2011-08-19 18:10:58 +0300969
Felipe Balbieeb720f2011-11-28 12:46:59 +0200970 for_each_sg(sg, s, request->num_mapped_sgs, i) {
971 unsigned chain = true;
Felipe Balbi72246da2011-08-19 18:10:58 +0300972
Felipe Balbieeb720f2011-11-28 12:46:59 +0200973 length = sg_dma_len(s);
974 dma = sg_dma_address(s);
Felipe Balbi72246da2011-08-19 18:10:58 +0300975
Paul Zimmerman1d046792012-02-15 18:56:56 -0800976 if (i == (request->num_mapped_sgs - 1) ||
977 sg_is_last(s)) {
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530978 if (list_is_last(&req->list,
979 &dep->request_list))
980 last_one = true;
Felipe Balbieeb720f2011-11-28 12:46:59 +0200981 chain = false;
982 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300983
Felipe Balbieeb720f2011-11-28 12:46:59 +0200984 trbs_left--;
985 if (!trbs_left)
986 last_one = true;
Felipe Balbi72246da2011-08-19 18:10:58 +0300987
Felipe Balbieeb720f2011-11-28 12:46:59 +0200988 if (last_one)
989 chain = false;
Felipe Balbi72246da2011-08-19 18:10:58 +0300990
Felipe Balbieeb720f2011-11-28 12:46:59 +0200991 dwc3_prepare_one_trb(dep, req, dma, length,
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530992 last_one, chain, i);
Felipe Balbi72246da2011-08-19 18:10:58 +0300993
Felipe Balbieeb720f2011-11-28 12:46:59 +0200994 if (last_one)
995 break;
996 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300997 } else {
Felipe Balbieeb720f2011-11-28 12:46:59 +0200998 dma = req->request.dma;
999 length = req->request.length;
1000 trbs_left--;
1001
1002 if (!trbs_left)
1003 last_one = 1;
1004
1005 /* Is this the last request? */
1006 if (list_is_last(&req->list, &dep->request_list))
1007 last_one = 1;
1008
1009 dwc3_prepare_one_trb(dep, req, dma, length,
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05301010 last_one, false, 0);
Felipe Balbieeb720f2011-11-28 12:46:59 +02001011
1012 if (last_one)
1013 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03001014 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001015 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001016}
1017
1018static int __dwc3_gadget_kick_transfer(struct dwc3_ep *dep, u16 cmd_param,
1019 int start_new)
1020{
1021 struct dwc3_gadget_ep_cmd_params params;
1022 struct dwc3_request *req;
1023 struct dwc3 *dwc = dep->dwc;
1024 int ret;
1025 u32 cmd;
1026
1027 if (start_new && (dep->flags & DWC3_EP_BUSY)) {
1028 dev_vdbg(dwc->dev, "%s: endpoint busy\n", dep->name);
1029 return -EBUSY;
1030 }
1031 dep->flags &= ~DWC3_EP_PENDING_REQUEST;
1032
1033 /*
1034 * If we are getting here after a short-out-packet we don't enqueue any
1035 * new requests as we try to set the IOC bit only on the last request.
1036 */
1037 if (start_new) {
1038 if (list_empty(&dep->req_queued))
1039 dwc3_prepare_trbs(dep, start_new);
1040
1041 /* req points to the first request which will be sent */
1042 req = next_request(&dep->req_queued);
1043 } else {
Felipe Balbi68e823e2011-11-28 12:25:01 +02001044 dwc3_prepare_trbs(dep, start_new);
1045
Felipe Balbi72246da2011-08-19 18:10:58 +03001046 /*
Paul Zimmerman1d046792012-02-15 18:56:56 -08001047 * req points to the first request where HWO changed from 0 to 1
Felipe Balbi72246da2011-08-19 18:10:58 +03001048 */
Felipe Balbi68e823e2011-11-28 12:25:01 +02001049 req = next_request(&dep->req_queued);
Felipe Balbi72246da2011-08-19 18:10:58 +03001050 }
1051 if (!req) {
1052 dep->flags |= DWC3_EP_PENDING_REQUEST;
1053 return 0;
1054 }
1055
1056 memset(&params, 0, sizeof(params));
Felipe Balbi72246da2011-08-19 18:10:58 +03001057
Pratyush Anand1877d6c2013-01-14 15:59:36 +05301058 if (start_new) {
1059 params.param0 = upper_32_bits(req->trb_dma);
1060 params.param1 = lower_32_bits(req->trb_dma);
Felipe Balbi72246da2011-08-19 18:10:58 +03001061 cmd = DWC3_DEPCMD_STARTTRANSFER;
Pratyush Anand1877d6c2013-01-14 15:59:36 +05301062 } else {
Felipe Balbi72246da2011-08-19 18:10:58 +03001063 cmd = DWC3_DEPCMD_UPDATETRANSFER;
Pratyush Anand1877d6c2013-01-14 15:59:36 +05301064 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001065
1066 cmd |= DWC3_DEPCMD_PARAM(cmd_param);
1067 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number, cmd, &params);
1068 if (ret < 0) {
1069 dev_dbg(dwc->dev, "failed to send STARTTRANSFER command\n");
1070
1071 /*
1072 * FIXME we need to iterate over the list of requests
1073 * here and stop, unmap, free and del each of the linked
Paul Zimmerman1d046792012-02-15 18:56:56 -08001074 * requests instead of what we do now.
Felipe Balbi72246da2011-08-19 18:10:58 +03001075 */
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02001076 usb_gadget_unmap_request(&dwc->gadget, &req->request,
1077 req->direction);
Felipe Balbi72246da2011-08-19 18:10:58 +03001078 list_del(&req->list);
1079 return ret;
1080 }
1081
1082 dep->flags |= DWC3_EP_BUSY;
Felipe Balbi25b8ff62011-11-04 12:32:47 +02001083
Paul Zimmermanf898ae02012-03-29 18:16:54 +00001084 if (start_new) {
Felipe Balbib4996a82012-06-06 12:04:13 +03001085 dep->resource_index = dwc3_gadget_ep_get_transfer_index(dwc,
Paul Zimmermanf898ae02012-03-29 18:16:54 +00001086 dep->number);
Felipe Balbib4996a82012-06-06 12:04:13 +03001087 WARN_ON_ONCE(!dep->resource_index);
Paul Zimmermanf898ae02012-03-29 18:16:54 +00001088 }
Felipe Balbi25b8ff62011-11-04 12:32:47 +02001089
Felipe Balbi72246da2011-08-19 18:10:58 +03001090 return 0;
1091}
1092
Pratyush Anandd6d6ec72012-05-25 18:54:56 +05301093static void __dwc3_gadget_start_isoc(struct dwc3 *dwc,
1094 struct dwc3_ep *dep, u32 cur_uf)
1095{
1096 u32 uf;
1097
1098 if (list_empty(&dep->request_list)) {
1099 dev_vdbg(dwc->dev, "ISOC ep %s run out for requests.\n",
1100 dep->name);
Pratyush Anandf4a53c52012-08-30 12:21:43 +05301101 dep->flags |= DWC3_EP_PENDING_REQUEST;
Pratyush Anandd6d6ec72012-05-25 18:54:56 +05301102 return;
1103 }
1104
1105 /* 4 micro frames in the future */
1106 uf = cur_uf + dep->interval * 4;
1107
1108 __dwc3_gadget_kick_transfer(dep, uf, 1);
1109}
1110
1111static void dwc3_gadget_start_isoc(struct dwc3 *dwc,
1112 struct dwc3_ep *dep, const struct dwc3_event_depevt *event)
1113{
1114 u32 cur_uf, mask;
1115
1116 mask = ~(dep->interval - 1);
1117 cur_uf = event->parameters & mask;
1118
1119 __dwc3_gadget_start_isoc(dwc, dep, cur_uf);
1120}
1121
Felipe Balbi72246da2011-08-19 18:10:58 +03001122static int __dwc3_gadget_ep_queue(struct dwc3_ep *dep, struct dwc3_request *req)
1123{
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02001124 struct dwc3 *dwc = dep->dwc;
1125 int ret;
1126
Felipe Balbi72246da2011-08-19 18:10:58 +03001127 req->request.actual = 0;
1128 req->request.status = -EINPROGRESS;
1129 req->direction = dep->direction;
1130 req->epnum = dep->number;
1131
1132 /*
1133 * We only add to our list of requests now and
1134 * start consuming the list once we get XferNotReady
1135 * IRQ.
1136 *
1137 * That way, we avoid doing anything that we don't need
1138 * to do now and defer it until the point we receive a
1139 * particular token from the Host side.
1140 *
1141 * This will also avoid Host cancelling URBs due to too
Paul Zimmerman1d046792012-02-15 18:56:56 -08001142 * many NAKs.
Felipe Balbi72246da2011-08-19 18:10:58 +03001143 */
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02001144 ret = usb_gadget_map_request(&dwc->gadget, &req->request,
1145 dep->direction);
1146 if (ret)
1147 return ret;
1148
Felipe Balbi72246da2011-08-19 18:10:58 +03001149 list_add_tail(&req->list, &dep->request_list);
1150
1151 /*
Felipe Balbib511e5e2012-06-06 12:00:50 +03001152 * There are a few special cases:
Felipe Balbi72246da2011-08-19 18:10:58 +03001153 *
Paul Zimmermanf898ae02012-03-29 18:16:54 +00001154 * 1. XferNotReady with empty list of requests. We need to kick the
1155 * transfer here in that situation, otherwise we will be NAKing
1156 * forever. If we get XferNotReady before gadget driver has a
1157 * chance to queue a request, we will ACK the IRQ but won't be
1158 * able to receive the data until the next request is queued.
1159 * The following code is handling exactly that.
1160 *
Felipe Balbi72246da2011-08-19 18:10:58 +03001161 */
1162 if (dep->flags & DWC3_EP_PENDING_REQUEST) {
Pratyush Anandf4a53c52012-08-30 12:21:43 +05301163 /*
1164 * If xfernotready is already elapsed and it is a case
1165 * of isoc transfer, then issue END TRANSFER, so that
1166 * you can receive xfernotready again and can have
1167 * notion of current microframe.
1168 */
1169 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Pratyush Anandcdc359d2013-01-14 15:59:34 +05301170 if (list_empty(&dep->req_queued)) {
Paul Zimmermanb992e682012-04-27 14:17:35 +03001171 dwc3_stop_active_transfer(dwc, dep->number, true);
Pratyush Anandcdc359d2013-01-14 15:59:34 +05301172 dep->flags = DWC3_EP_ENABLED;
1173 }
Pratyush Anandf4a53c52012-08-30 12:21:43 +05301174 return 0;
1175 }
1176
Felipe Balbib511e5e2012-06-06 12:00:50 +03001177 ret = __dwc3_gadget_kick_transfer(dep, 0, true);
Moiz Sonasath348e0262012-08-01 14:08:30 -05001178 if (ret && ret != -EBUSY)
Felipe Balbi72246da2011-08-19 18:10:58 +03001179 dev_dbg(dwc->dev, "%s: failed to kick transfers\n",
1180 dep->name);
Pratyush Anand15f86bd2013-01-14 15:59:33 +05301181 return ret;
Felipe Balbia0925322012-05-22 10:24:11 +03001182 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001183
Felipe Balbib511e5e2012-06-06 12:00:50 +03001184 /*
1185 * 2. XferInProgress on Isoc EP with an active transfer. We need to
1186 * kick the transfer here after queuing a request, otherwise the
1187 * core may not see the modified TRB(s).
1188 */
1189 if (usb_endpoint_xfer_isoc(dep->endpoint.desc) &&
Pratyush Anand79c90462012-08-07 16:54:18 +05301190 (dep->flags & DWC3_EP_BUSY) &&
1191 !(dep->flags & DWC3_EP_MISSED_ISOC)) {
Felipe Balbib4996a82012-06-06 12:04:13 +03001192 WARN_ON_ONCE(!dep->resource_index);
1193 ret = __dwc3_gadget_kick_transfer(dep, dep->resource_index,
Felipe Balbib511e5e2012-06-06 12:00:50 +03001194 false);
Moiz Sonasath348e0262012-08-01 14:08:30 -05001195 if (ret && ret != -EBUSY)
Felipe Balbib511e5e2012-06-06 12:00:50 +03001196 dev_dbg(dwc->dev, "%s: failed to kick transfers\n",
1197 dep->name);
Pratyush Anand15f86bd2013-01-14 15:59:33 +05301198 return ret;
Felipe Balbib511e5e2012-06-06 12:00:50 +03001199 }
1200
Felipe Balbib997ada2012-07-26 13:26:50 +03001201 /*
1202 * 4. Stream Capable Bulk Endpoints. We need to start the transfer
1203 * right away, otherwise host will not know we have streams to be
1204 * handled.
1205 */
1206 if (dep->stream_capable) {
1207 int ret;
1208
1209 ret = __dwc3_gadget_kick_transfer(dep, 0, true);
1210 if (ret && ret != -EBUSY) {
1211 struct dwc3 *dwc = dep->dwc;
1212
1213 dev_dbg(dwc->dev, "%s: failed to kick transfers\n",
1214 dep->name);
1215 }
1216 }
1217
Felipe Balbi72246da2011-08-19 18:10:58 +03001218 return 0;
1219}
1220
1221static int dwc3_gadget_ep_queue(struct usb_ep *ep, struct usb_request *request,
1222 gfp_t gfp_flags)
1223{
1224 struct dwc3_request *req = to_dwc3_request(request);
1225 struct dwc3_ep *dep = to_dwc3_ep(ep);
1226 struct dwc3 *dwc = dep->dwc;
1227
1228 unsigned long flags;
1229
1230 int ret;
1231
Ido Shayevitz16e78db2012-03-12 20:25:24 +02001232 if (!dep->endpoint.desc) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001233 dev_dbg(dwc->dev, "trying to queue request %p to disabled %s\n",
1234 request, ep->name);
1235 return -ESHUTDOWN;
1236 }
1237
1238 dev_vdbg(dwc->dev, "queing request %p to %s length %d\n",
1239 request, ep->name, request->length);
1240
1241 spin_lock_irqsave(&dwc->lock, flags);
1242 ret = __dwc3_gadget_ep_queue(dep, req);
1243 spin_unlock_irqrestore(&dwc->lock, flags);
1244
1245 return ret;
1246}
1247
1248static int dwc3_gadget_ep_dequeue(struct usb_ep *ep,
1249 struct usb_request *request)
1250{
1251 struct dwc3_request *req = to_dwc3_request(request);
1252 struct dwc3_request *r = NULL;
1253
1254 struct dwc3_ep *dep = to_dwc3_ep(ep);
1255 struct dwc3 *dwc = dep->dwc;
1256
1257 unsigned long flags;
1258 int ret = 0;
1259
1260 spin_lock_irqsave(&dwc->lock, flags);
1261
1262 list_for_each_entry(r, &dep->request_list, list) {
1263 if (r == req)
1264 break;
1265 }
1266
1267 if (r != req) {
1268 list_for_each_entry(r, &dep->req_queued, list) {
1269 if (r == req)
1270 break;
1271 }
1272 if (r == req) {
1273 /* wait until it is processed */
Paul Zimmermanb992e682012-04-27 14:17:35 +03001274 dwc3_stop_active_transfer(dwc, dep->number, true);
Pratyush Anande8d4e8b2012-06-15 11:54:00 +05301275 goto out1;
Felipe Balbi72246da2011-08-19 18:10:58 +03001276 }
1277 dev_err(dwc->dev, "request %p was not queued to %s\n",
1278 request, ep->name);
1279 ret = -EINVAL;
1280 goto out0;
1281 }
1282
Pratyush Anande8d4e8b2012-06-15 11:54:00 +05301283out1:
Felipe Balbi72246da2011-08-19 18:10:58 +03001284 /* giveback the request */
1285 dwc3_gadget_giveback(dep, req, -ECONNRESET);
1286
1287out0:
1288 spin_unlock_irqrestore(&dwc->lock, flags);
1289
1290 return ret;
1291}
1292
1293int __dwc3_gadget_ep_set_halt(struct dwc3_ep *dep, int value)
1294{
1295 struct dwc3_gadget_ep_cmd_params params;
1296 struct dwc3 *dwc = dep->dwc;
1297 int ret;
1298
1299 memset(&params, 0x00, sizeof(params));
1300
1301 if (value) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001302 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number,
1303 DWC3_DEPCMD_SETSTALL, &params);
1304 if (ret)
Dan Carpenter3f892042014-03-07 14:20:22 +03001305 dev_err(dwc->dev, "failed to set STALL on %s\n",
Felipe Balbi72246da2011-08-19 18:10:58 +03001306 dep->name);
1307 else
1308 dep->flags |= DWC3_EP_STALL;
1309 } else {
1310 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number,
1311 DWC3_DEPCMD_CLEARSTALL, &params);
1312 if (ret)
Dan Carpenter3f892042014-03-07 14:20:22 +03001313 dev_err(dwc->dev, "failed to clear STALL on %s\n",
Felipe Balbi72246da2011-08-19 18:10:58 +03001314 dep->name);
1315 else
Alan Sterna535d812013-11-01 12:05:12 -04001316 dep->flags &= ~(DWC3_EP_STALL | DWC3_EP_WEDGE);
Felipe Balbi72246da2011-08-19 18:10:58 +03001317 }
Paul Zimmerman52754552011-09-30 10:58:44 +03001318
Felipe Balbi72246da2011-08-19 18:10:58 +03001319 return ret;
1320}
1321
1322static int dwc3_gadget_ep_set_halt(struct usb_ep *ep, int value)
1323{
1324 struct dwc3_ep *dep = to_dwc3_ep(ep);
1325 struct dwc3 *dwc = dep->dwc;
1326
1327 unsigned long flags;
1328
1329 int ret;
1330
1331 spin_lock_irqsave(&dwc->lock, flags);
1332
Ido Shayevitz16e78db2012-03-12 20:25:24 +02001333 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001334 dev_err(dwc->dev, "%s is of Isochronous type\n", dep->name);
1335 ret = -EINVAL;
1336 goto out;
1337 }
1338
1339 ret = __dwc3_gadget_ep_set_halt(dep, value);
1340out:
1341 spin_unlock_irqrestore(&dwc->lock, flags);
1342
1343 return ret;
1344}
1345
1346static int dwc3_gadget_ep_set_wedge(struct usb_ep *ep)
1347{
1348 struct dwc3_ep *dep = to_dwc3_ep(ep);
Paul Zimmerman249a4562012-02-24 17:32:16 -08001349 struct dwc3 *dwc = dep->dwc;
1350 unsigned long flags;
Felipe Balbi72246da2011-08-19 18:10:58 +03001351
Paul Zimmerman249a4562012-02-24 17:32:16 -08001352 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001353 dep->flags |= DWC3_EP_WEDGE;
Paul Zimmerman249a4562012-02-24 17:32:16 -08001354 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001355
Pratyush Anand08f0d962012-06-25 22:40:43 +05301356 if (dep->number == 0 || dep->number == 1)
1357 return dwc3_gadget_ep0_set_halt(ep, 1);
1358 else
1359 return dwc3_gadget_ep_set_halt(ep, 1);
Felipe Balbi72246da2011-08-19 18:10:58 +03001360}
1361
1362/* -------------------------------------------------------------------------- */
1363
1364static struct usb_endpoint_descriptor dwc3_gadget_ep0_desc = {
1365 .bLength = USB_DT_ENDPOINT_SIZE,
1366 .bDescriptorType = USB_DT_ENDPOINT,
1367 .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
1368};
1369
1370static const struct usb_ep_ops dwc3_gadget_ep0_ops = {
1371 .enable = dwc3_gadget_ep0_enable,
1372 .disable = dwc3_gadget_ep0_disable,
1373 .alloc_request = dwc3_gadget_ep_alloc_request,
1374 .free_request = dwc3_gadget_ep_free_request,
1375 .queue = dwc3_gadget_ep0_queue,
1376 .dequeue = dwc3_gadget_ep_dequeue,
Pratyush Anand08f0d962012-06-25 22:40:43 +05301377 .set_halt = dwc3_gadget_ep0_set_halt,
Felipe Balbi72246da2011-08-19 18:10:58 +03001378 .set_wedge = dwc3_gadget_ep_set_wedge,
1379};
1380
1381static const struct usb_ep_ops dwc3_gadget_ep_ops = {
1382 .enable = dwc3_gadget_ep_enable,
1383 .disable = dwc3_gadget_ep_disable,
1384 .alloc_request = dwc3_gadget_ep_alloc_request,
1385 .free_request = dwc3_gadget_ep_free_request,
1386 .queue = dwc3_gadget_ep_queue,
1387 .dequeue = dwc3_gadget_ep_dequeue,
1388 .set_halt = dwc3_gadget_ep_set_halt,
1389 .set_wedge = dwc3_gadget_ep_set_wedge,
1390};
1391
1392/* -------------------------------------------------------------------------- */
1393
1394static int dwc3_gadget_get_frame(struct usb_gadget *g)
1395{
1396 struct dwc3 *dwc = gadget_to_dwc(g);
1397 u32 reg;
1398
1399 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1400 return DWC3_DSTS_SOFFN(reg);
1401}
1402
1403static int dwc3_gadget_wakeup(struct usb_gadget *g)
1404{
1405 struct dwc3 *dwc = gadget_to_dwc(g);
1406
1407 unsigned long timeout;
1408 unsigned long flags;
1409
1410 u32 reg;
1411
1412 int ret = 0;
1413
1414 u8 link_state;
1415 u8 speed;
1416
1417 spin_lock_irqsave(&dwc->lock, flags);
1418
1419 /*
1420 * According to the Databook Remote wakeup request should
1421 * be issued only when the device is in early suspend state.
1422 *
1423 * We can check that via USB Link State bits in DSTS register.
1424 */
1425 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1426
1427 speed = reg & DWC3_DSTS_CONNECTSPD;
1428 if (speed == DWC3_DSTS_SUPERSPEED) {
1429 dev_dbg(dwc->dev, "no wakeup on SuperSpeed\n");
1430 ret = -EINVAL;
1431 goto out;
1432 }
1433
1434 link_state = DWC3_DSTS_USBLNKST(reg);
1435
1436 switch (link_state) {
1437 case DWC3_LINK_STATE_RX_DET: /* in HS, means Early Suspend */
1438 case DWC3_LINK_STATE_U3: /* in HS, means SUSPEND */
1439 break;
1440 default:
1441 dev_dbg(dwc->dev, "can't wakeup from link state %d\n",
1442 link_state);
1443 ret = -EINVAL;
1444 goto out;
1445 }
1446
Felipe Balbi8598bde2012-01-02 18:55:57 +02001447 ret = dwc3_gadget_set_link_state(dwc, DWC3_LINK_STATE_RECOV);
1448 if (ret < 0) {
1449 dev_err(dwc->dev, "failed to put link in Recovery\n");
1450 goto out;
1451 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001452
Paul Zimmerman802fde92012-04-27 13:10:52 +03001453 /* Recent versions do this automatically */
1454 if (dwc->revision < DWC3_REVISION_194A) {
1455 /* write zeroes to Link Change Request */
Felipe Balbifcc023c2012-05-24 10:27:56 +03001456 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
Paul Zimmerman802fde92012-04-27 13:10:52 +03001457 reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK;
1458 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1459 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001460
Paul Zimmerman1d046792012-02-15 18:56:56 -08001461 /* poll until Link State changes to ON */
Felipe Balbi72246da2011-08-19 18:10:58 +03001462 timeout = jiffies + msecs_to_jiffies(100);
1463
Paul Zimmerman1d046792012-02-15 18:56:56 -08001464 while (!time_after(jiffies, timeout)) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001465 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1466
1467 /* in HS, means ON */
1468 if (DWC3_DSTS_USBLNKST(reg) == DWC3_LINK_STATE_U0)
1469 break;
1470 }
1471
1472 if (DWC3_DSTS_USBLNKST(reg) != DWC3_LINK_STATE_U0) {
1473 dev_err(dwc->dev, "failed to send remote wakeup\n");
1474 ret = -EINVAL;
1475 }
1476
1477out:
1478 spin_unlock_irqrestore(&dwc->lock, flags);
1479
1480 return ret;
1481}
1482
1483static int dwc3_gadget_set_selfpowered(struct usb_gadget *g,
1484 int is_selfpowered)
1485{
1486 struct dwc3 *dwc = gadget_to_dwc(g);
Paul Zimmerman249a4562012-02-24 17:32:16 -08001487 unsigned long flags;
Felipe Balbi72246da2011-08-19 18:10:58 +03001488
Paul Zimmerman249a4562012-02-24 17:32:16 -08001489 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001490 dwc->is_selfpowered = !!is_selfpowered;
Paul Zimmerman249a4562012-02-24 17:32:16 -08001491 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001492
1493 return 0;
1494}
1495
Felipe Balbi7b2a0362013-12-19 13:43:19 -06001496static int dwc3_gadget_run_stop(struct dwc3 *dwc, int is_on, int suspend)
Felipe Balbi72246da2011-08-19 18:10:58 +03001497{
1498 u32 reg;
Sebastian Andrzej Siewior61d58242011-08-29 16:46:38 +02001499 u32 timeout = 500;
Felipe Balbi72246da2011-08-19 18:10:58 +03001500
1501 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
Felipe Balbi8db7ed12012-01-18 18:32:29 +02001502 if (is_on) {
Paul Zimmerman802fde92012-04-27 13:10:52 +03001503 if (dwc->revision <= DWC3_REVISION_187A) {
1504 reg &= ~DWC3_DCTL_TRGTULST_MASK;
1505 reg |= DWC3_DCTL_TRGTULST_RX_DET;
1506 }
1507
1508 if (dwc->revision >= DWC3_REVISION_194A)
1509 reg &= ~DWC3_DCTL_KEEP_CONNECT;
1510 reg |= DWC3_DCTL_RUN_STOP;
Felipe Balbi7b2a0362013-12-19 13:43:19 -06001511
1512 if (dwc->has_hibernation)
1513 reg |= DWC3_DCTL_KEEP_CONNECT;
1514
Felipe Balbi9fcb3bd2013-02-08 17:55:58 +02001515 dwc->pullups_connected = true;
Felipe Balbi8db7ed12012-01-18 18:32:29 +02001516 } else {
Felipe Balbi72246da2011-08-19 18:10:58 +03001517 reg &= ~DWC3_DCTL_RUN_STOP;
Felipe Balbi7b2a0362013-12-19 13:43:19 -06001518
1519 if (dwc->has_hibernation && !suspend)
1520 reg &= ~DWC3_DCTL_KEEP_CONNECT;
1521
Felipe Balbi9fcb3bd2013-02-08 17:55:58 +02001522 dwc->pullups_connected = false;
Felipe Balbi8db7ed12012-01-18 18:32:29 +02001523 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001524
1525 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1526
1527 do {
1528 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1529 if (is_on) {
1530 if (!(reg & DWC3_DSTS_DEVCTRLHLT))
1531 break;
1532 } else {
1533 if (reg & DWC3_DSTS_DEVCTRLHLT)
1534 break;
1535 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001536 timeout--;
1537 if (!timeout)
Pratyush Anand6f17f742012-07-02 10:21:55 +05301538 return -ETIMEDOUT;
Sebastian Andrzej Siewior61d58242011-08-29 16:46:38 +02001539 udelay(1);
Felipe Balbi72246da2011-08-19 18:10:58 +03001540 } while (1);
1541
1542 dev_vdbg(dwc->dev, "gadget %s data soft-%s\n",
1543 dwc->gadget_driver
1544 ? dwc->gadget_driver->function : "no-function",
1545 is_on ? "connect" : "disconnect");
Pratyush Anand6f17f742012-07-02 10:21:55 +05301546
1547 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +03001548}
1549
1550static int dwc3_gadget_pullup(struct usb_gadget *g, int is_on)
1551{
1552 struct dwc3 *dwc = gadget_to_dwc(g);
1553 unsigned long flags;
Pratyush Anand6f17f742012-07-02 10:21:55 +05301554 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001555
1556 is_on = !!is_on;
1557
1558 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi7b2a0362013-12-19 13:43:19 -06001559 ret = dwc3_gadget_run_stop(dwc, is_on, false);
Felipe Balbi72246da2011-08-19 18:10:58 +03001560 spin_unlock_irqrestore(&dwc->lock, flags);
1561
Pratyush Anand6f17f742012-07-02 10:21:55 +05301562 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001563}
1564
Felipe Balbi8698e2a2013-02-08 15:24:04 +02001565static void dwc3_gadget_enable_irq(struct dwc3 *dwc)
1566{
1567 u32 reg;
1568
1569 /* Enable all but Start and End of Frame IRQs */
1570 reg = (DWC3_DEVTEN_VNDRDEVTSTRCVEDEN |
1571 DWC3_DEVTEN_EVNTOVERFLOWEN |
1572 DWC3_DEVTEN_CMDCMPLTEN |
1573 DWC3_DEVTEN_ERRTICERREN |
1574 DWC3_DEVTEN_WKUPEVTEN |
1575 DWC3_DEVTEN_ULSTCNGEN |
1576 DWC3_DEVTEN_CONNECTDONEEN |
1577 DWC3_DEVTEN_USBRSTEN |
1578 DWC3_DEVTEN_DISCONNEVTEN);
1579
1580 dwc3_writel(dwc->regs, DWC3_DEVTEN, reg);
1581}
1582
1583static void dwc3_gadget_disable_irq(struct dwc3 *dwc)
1584{
1585 /* mask all interrupts */
1586 dwc3_writel(dwc->regs, DWC3_DEVTEN, 0x00);
1587}
1588
1589static irqreturn_t dwc3_interrupt(int irq, void *_dwc);
Felipe Balbib15a7622011-06-30 16:57:15 +03001590static irqreturn_t dwc3_thread_interrupt(int irq, void *_dwc);
Felipe Balbi8698e2a2013-02-08 15:24:04 +02001591
Felipe Balbi72246da2011-08-19 18:10:58 +03001592static int dwc3_gadget_start(struct usb_gadget *g,
1593 struct usb_gadget_driver *driver)
1594{
1595 struct dwc3 *dwc = gadget_to_dwc(g);
1596 struct dwc3_ep *dep;
1597 unsigned long flags;
1598 int ret = 0;
Felipe Balbi8698e2a2013-02-08 15:24:04 +02001599 int irq;
Felipe Balbi72246da2011-08-19 18:10:58 +03001600 u32 reg;
1601
Felipe Balbib0d7ffd2013-06-27 10:00:18 +03001602 irq = platform_get_irq(to_platform_device(dwc->dev), 0);
1603 ret = request_threaded_irq(irq, dwc3_interrupt, dwc3_thread_interrupt,
Felipe Balbie8adfc32013-06-12 21:11:14 +03001604 IRQF_SHARED, "dwc3", dwc);
Felipe Balbib0d7ffd2013-06-27 10:00:18 +03001605 if (ret) {
1606 dev_err(dwc->dev, "failed to request irq #%d --> %d\n",
1607 irq, ret);
1608 goto err0;
1609 }
1610
Felipe Balbi72246da2011-08-19 18:10:58 +03001611 spin_lock_irqsave(&dwc->lock, flags);
1612
1613 if (dwc->gadget_driver) {
1614 dev_err(dwc->dev, "%s is already bound to %s\n",
1615 dwc->gadget.name,
1616 dwc->gadget_driver->driver.name);
1617 ret = -EBUSY;
Felipe Balbib0d7ffd2013-06-27 10:00:18 +03001618 goto err1;
Felipe Balbi72246da2011-08-19 18:10:58 +03001619 }
1620
1621 dwc->gadget_driver = driver;
Felipe Balbi72246da2011-08-19 18:10:58 +03001622
Felipe Balbi72246da2011-08-19 18:10:58 +03001623 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
1624 reg &= ~(DWC3_DCFG_SPEED_MASK);
Felipe Balbi07e7f472012-03-23 12:20:31 +02001625
1626 /**
1627 * WORKAROUND: DWC3 revision < 2.20a have an issue
1628 * which would cause metastability state on Run/Stop
1629 * bit if we try to force the IP to USB2-only mode.
1630 *
1631 * Because of that, we cannot configure the IP to any
1632 * speed other than the SuperSpeed
1633 *
1634 * Refers to:
1635 *
1636 * STAR#9000525659: Clock Domain Crossing on DCTL in
1637 * USB 2.0 Mode
1638 */
Felipe Balbif7e846f2013-06-30 14:29:51 +03001639 if (dwc->revision < DWC3_REVISION_220A) {
Felipe Balbi07e7f472012-03-23 12:20:31 +02001640 reg |= DWC3_DCFG_SUPERSPEED;
Felipe Balbif7e846f2013-06-30 14:29:51 +03001641 } else {
1642 switch (dwc->maximum_speed) {
1643 case USB_SPEED_LOW:
1644 reg |= DWC3_DSTS_LOWSPEED;
1645 break;
1646 case USB_SPEED_FULL:
1647 reg |= DWC3_DSTS_FULLSPEED1;
1648 break;
1649 case USB_SPEED_HIGH:
1650 reg |= DWC3_DSTS_HIGHSPEED;
1651 break;
1652 case USB_SPEED_SUPER: /* FALLTHROUGH */
1653 case USB_SPEED_UNKNOWN: /* FALTHROUGH */
1654 default:
1655 reg |= DWC3_DSTS_SUPERSPEED;
1656 }
1657 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001658 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
1659
Paul Zimmermanb23c8432011-09-30 10:58:42 +03001660 dwc->start_config_issued = false;
1661
Felipe Balbi72246da2011-08-19 18:10:58 +03001662 /* Start with SuperSpeed Default */
1663 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
1664
1665 dep = dwc->eps[0];
Paul Zimmerman265b70a2013-12-19 12:38:49 -06001666 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, false,
1667 false);
Felipe Balbi72246da2011-08-19 18:10:58 +03001668 if (ret) {
1669 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
Felipe Balbib0d7ffd2013-06-27 10:00:18 +03001670 goto err2;
Felipe Balbi72246da2011-08-19 18:10:58 +03001671 }
1672
1673 dep = dwc->eps[1];
Paul Zimmerman265b70a2013-12-19 12:38:49 -06001674 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, false,
1675 false);
Felipe Balbi72246da2011-08-19 18:10:58 +03001676 if (ret) {
1677 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
Felipe Balbib0d7ffd2013-06-27 10:00:18 +03001678 goto err3;
Felipe Balbi72246da2011-08-19 18:10:58 +03001679 }
1680
1681 /* begin to receive SETUP packets */
Felipe Balbic7fcdeb2011-08-27 22:28:36 +03001682 dwc->ep0state = EP0_SETUP_PHASE;
Felipe Balbi72246da2011-08-19 18:10:58 +03001683 dwc3_ep0_out_start(dwc);
1684
Felipe Balbi8698e2a2013-02-08 15:24:04 +02001685 dwc3_gadget_enable_irq(dwc);
1686
Felipe Balbi72246da2011-08-19 18:10:58 +03001687 spin_unlock_irqrestore(&dwc->lock, flags);
1688
1689 return 0;
1690
Felipe Balbib0d7ffd2013-06-27 10:00:18 +03001691err3:
Felipe Balbi72246da2011-08-19 18:10:58 +03001692 __dwc3_gadget_ep_disable(dwc->eps[0]);
1693
Felipe Balbib0d7ffd2013-06-27 10:00:18 +03001694err2:
Felipe Balbicdcedd62013-07-15 12:36:35 +03001695 dwc->gadget_driver = NULL;
Felipe Balbib0d7ffd2013-06-27 10:00:18 +03001696
1697err1:
Felipe Balbi72246da2011-08-19 18:10:58 +03001698 spin_unlock_irqrestore(&dwc->lock, flags);
1699
Felipe Balbib0d7ffd2013-06-27 10:00:18 +03001700 free_irq(irq, dwc);
1701
1702err0:
Felipe Balbi72246da2011-08-19 18:10:58 +03001703 return ret;
1704}
1705
1706static int dwc3_gadget_stop(struct usb_gadget *g,
1707 struct usb_gadget_driver *driver)
1708{
1709 struct dwc3 *dwc = gadget_to_dwc(g);
1710 unsigned long flags;
Felipe Balbi8698e2a2013-02-08 15:24:04 +02001711 int irq;
Felipe Balbi72246da2011-08-19 18:10:58 +03001712
1713 spin_lock_irqsave(&dwc->lock, flags);
1714
Felipe Balbi8698e2a2013-02-08 15:24:04 +02001715 dwc3_gadget_disable_irq(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +03001716 __dwc3_gadget_ep_disable(dwc->eps[0]);
1717 __dwc3_gadget_ep_disable(dwc->eps[1]);
1718
1719 dwc->gadget_driver = NULL;
Felipe Balbi72246da2011-08-19 18:10:58 +03001720
1721 spin_unlock_irqrestore(&dwc->lock, flags);
1722
Felipe Balbib0d7ffd2013-06-27 10:00:18 +03001723 irq = platform_get_irq(to_platform_device(dwc->dev), 0);
1724 free_irq(irq, dwc);
1725
Felipe Balbi72246da2011-08-19 18:10:58 +03001726 return 0;
1727}
Paul Zimmerman802fde92012-04-27 13:10:52 +03001728
Felipe Balbi72246da2011-08-19 18:10:58 +03001729static const struct usb_gadget_ops dwc3_gadget_ops = {
1730 .get_frame = dwc3_gadget_get_frame,
1731 .wakeup = dwc3_gadget_wakeup,
1732 .set_selfpowered = dwc3_gadget_set_selfpowered,
1733 .pullup = dwc3_gadget_pullup,
1734 .udc_start = dwc3_gadget_start,
1735 .udc_stop = dwc3_gadget_stop,
1736};
1737
1738/* -------------------------------------------------------------------------- */
1739
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03001740static int dwc3_gadget_init_hw_endpoints(struct dwc3 *dwc,
1741 u8 num, u32 direction)
Felipe Balbi72246da2011-08-19 18:10:58 +03001742{
1743 struct dwc3_ep *dep;
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03001744 u8 i;
Felipe Balbi72246da2011-08-19 18:10:58 +03001745
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03001746 for (i = 0; i < num; i++) {
1747 u8 epnum = (i << 1) | (!!direction);
Felipe Balbi72246da2011-08-19 18:10:58 +03001748
Felipe Balbi72246da2011-08-19 18:10:58 +03001749 dep = kzalloc(sizeof(*dep), GFP_KERNEL);
1750 if (!dep) {
1751 dev_err(dwc->dev, "can't allocate endpoint %d\n",
1752 epnum);
1753 return -ENOMEM;
1754 }
1755
1756 dep->dwc = dwc;
1757 dep->number = epnum;
Felipe Balbi9aa62ae2013-07-12 19:10:59 +03001758 dep->direction = !!direction;
Felipe Balbi72246da2011-08-19 18:10:58 +03001759 dwc->eps[epnum] = dep;
1760
1761 snprintf(dep->name, sizeof(dep->name), "ep%d%s", epnum >> 1,
1762 (epnum & 1) ? "in" : "out");
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03001763
Felipe Balbi72246da2011-08-19 18:10:58 +03001764 dep->endpoint.name = dep->name;
Felipe Balbi72246da2011-08-19 18:10:58 +03001765
Felipe Balbi653df352013-07-12 19:11:57 +03001766 dev_vdbg(dwc->dev, "initializing %s\n", dep->name);
1767
Felipe Balbi72246da2011-08-19 18:10:58 +03001768 if (epnum == 0 || epnum == 1) {
Robert Baldygae117e742013-12-13 12:23:38 +01001769 usb_ep_set_maxpacket_limit(&dep->endpoint, 512);
Pratyush Anand6048e4c2013-01-18 16:53:56 +05301770 dep->endpoint.maxburst = 1;
Felipe Balbi72246da2011-08-19 18:10:58 +03001771 dep->endpoint.ops = &dwc3_gadget_ep0_ops;
1772 if (!epnum)
1773 dwc->gadget.ep0 = &dep->endpoint;
1774 } else {
1775 int ret;
1776
Robert Baldygae117e742013-12-13 12:23:38 +01001777 usb_ep_set_maxpacket_limit(&dep->endpoint, 1024);
Sebastian Andrzej Siewior12d36c12011-11-03 20:27:50 +01001778 dep->endpoint.max_streams = 15;
Felipe Balbi72246da2011-08-19 18:10:58 +03001779 dep->endpoint.ops = &dwc3_gadget_ep_ops;
1780 list_add_tail(&dep->endpoint.ep_list,
1781 &dwc->gadget.ep_list);
1782
1783 ret = dwc3_alloc_trb_pool(dep);
Felipe Balbi25b8ff62011-11-04 12:32:47 +02001784 if (ret)
Felipe Balbi72246da2011-08-19 18:10:58 +03001785 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001786 }
Felipe Balbi25b8ff62011-11-04 12:32:47 +02001787
Felipe Balbi72246da2011-08-19 18:10:58 +03001788 INIT_LIST_HEAD(&dep->request_list);
1789 INIT_LIST_HEAD(&dep->req_queued);
1790 }
1791
1792 return 0;
1793}
1794
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03001795static int dwc3_gadget_init_endpoints(struct dwc3 *dwc)
1796{
1797 int ret;
1798
1799 INIT_LIST_HEAD(&dwc->gadget.ep_list);
1800
1801 ret = dwc3_gadget_init_hw_endpoints(dwc, dwc->num_out_eps, 0);
1802 if (ret < 0) {
1803 dev_vdbg(dwc->dev, "failed to allocate OUT endpoints\n");
1804 return ret;
1805 }
1806
1807 ret = dwc3_gadget_init_hw_endpoints(dwc, dwc->num_in_eps, 1);
1808 if (ret < 0) {
1809 dev_vdbg(dwc->dev, "failed to allocate IN endpoints\n");
1810 return ret;
1811 }
1812
1813 return 0;
1814}
1815
Felipe Balbi72246da2011-08-19 18:10:58 +03001816static void dwc3_gadget_free_endpoints(struct dwc3 *dwc)
1817{
1818 struct dwc3_ep *dep;
1819 u8 epnum;
1820
1821 for (epnum = 0; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
1822 dep = dwc->eps[epnum];
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03001823 if (!dep)
1824 continue;
George Cherian5bf8fae2013-05-27 14:35:49 +05301825 /*
1826 * Physical endpoints 0 and 1 are special; they form the
1827 * bi-directional USB endpoint 0.
1828 *
1829 * For those two physical endpoints, we don't allocate a TRB
1830 * pool nor do we add them the endpoints list. Due to that, we
1831 * shouldn't do these two operations otherwise we would end up
1832 * with all sorts of bugs when removing dwc3.ko.
1833 */
1834 if (epnum != 0 && epnum != 1) {
1835 dwc3_free_trb_pool(dep);
Felipe Balbi72246da2011-08-19 18:10:58 +03001836 list_del(&dep->endpoint.ep_list);
George Cherian5bf8fae2013-05-27 14:35:49 +05301837 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001838
1839 kfree(dep);
1840 }
1841}
1842
Felipe Balbi72246da2011-08-19 18:10:58 +03001843/* -------------------------------------------------------------------------- */
Felipe Balbie5caff62013-02-26 15:11:05 +02001844
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05301845static int __dwc3_cleanup_done_trbs(struct dwc3 *dwc, struct dwc3_ep *dep,
1846 struct dwc3_request *req, struct dwc3_trb *trb,
1847 const struct dwc3_event_depevt *event, int status)
1848{
1849 unsigned int count;
1850 unsigned int s_pkt = 0;
1851 unsigned int trb_status;
1852
1853 if ((trb->ctrl & DWC3_TRB_CTRL_HWO) && status != -ESHUTDOWN)
1854 /*
1855 * We continue despite the error. There is not much we
1856 * can do. If we don't clean it up we loop forever. If
1857 * we skip the TRB then it gets overwritten after a
1858 * while since we use them in a ring buffer. A BUG()
1859 * would help. Lets hope that if this occurs, someone
1860 * fixes the root cause instead of looking away :)
1861 */
1862 dev_err(dwc->dev, "%s's TRB (%p) still owned by HW\n",
1863 dep->name, trb);
1864 count = trb->size & DWC3_TRB_SIZE_MASK;
1865
1866 if (dep->direction) {
1867 if (count) {
1868 trb_status = DWC3_TRB_SIZE_TRBSTS(trb->size);
1869 if (trb_status == DWC3_TRBSTS_MISSED_ISOC) {
1870 dev_dbg(dwc->dev, "incomplete IN transfer %s\n",
1871 dep->name);
1872 /*
1873 * If missed isoc occurred and there is
1874 * no request queued then issue END
1875 * TRANSFER, so that core generates
1876 * next xfernotready and we will issue
1877 * a fresh START TRANSFER.
1878 * If there are still queued request
1879 * then wait, do not issue either END
1880 * or UPDATE TRANSFER, just attach next
1881 * request in request_list during
1882 * giveback.If any future queued request
1883 * is successfully transferred then we
1884 * will issue UPDATE TRANSFER for all
1885 * request in the request_list.
1886 */
1887 dep->flags |= DWC3_EP_MISSED_ISOC;
1888 } else {
1889 dev_err(dwc->dev, "incomplete IN transfer %s\n",
1890 dep->name);
1891 status = -ECONNRESET;
1892 }
1893 } else {
1894 dep->flags &= ~DWC3_EP_MISSED_ISOC;
1895 }
1896 } else {
1897 if (count && (event->status & DEPEVT_STATUS_SHORT))
1898 s_pkt = 1;
1899 }
1900
1901 /*
1902 * We assume here we will always receive the entire data block
1903 * which we should receive. Meaning, if we program RX to
1904 * receive 4K but we receive only 2K, we assume that's all we
1905 * should receive and we simply bounce the request back to the
1906 * gadget driver for further processing.
1907 */
1908 req->request.actual += req->request.length - count;
1909 if (s_pkt)
1910 return 1;
1911 if ((event->status & DEPEVT_STATUS_LST) &&
1912 (trb->ctrl & (DWC3_TRB_CTRL_LST |
1913 DWC3_TRB_CTRL_HWO)))
1914 return 1;
1915 if ((event->status & DEPEVT_STATUS_IOC) &&
1916 (trb->ctrl & DWC3_TRB_CTRL_IOC))
1917 return 1;
1918 return 0;
1919}
1920
Felipe Balbi72246da2011-08-19 18:10:58 +03001921static 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;
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05301926 unsigned int slot;
1927 unsigned int i;
1928 int ret;
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 }
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05301936 i = 0;
1937 do {
1938 slot = req->start_slot + i;
1939 if ((slot == DWC3_TRB_NUM - 1) &&
1940 usb_endpoint_xfer_isoc(dep->endpoint.desc))
1941 slot++;
1942 slot %= DWC3_TRB_NUM;
1943 trb = &dep->trb_pool[slot];
Felipe Balbi72246da2011-08-19 18:10:58 +03001944
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05301945 ret = __dwc3_cleanup_done_trbs(dwc, dep, req, trb,
1946 event, status);
1947 if (ret)
1948 break;
1949 }while (++i < req->request.num_mapped_sgs);
Felipe Balbi72246da2011-08-19 18:10:58 +03001950
Felipe Balbi72246da2011-08-19 18:10:58 +03001951 dwc3_gadget_giveback(dep, req, status);
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05301952
1953 if (ret)
Felipe Balbi72246da2011-08-19 18:10:58 +03001954 break;
1955 } while (1);
1956
Pratyush Anandcdc359d2013-01-14 15:59:34 +05301957 if (usb_endpoint_xfer_isoc(dep->endpoint.desc) &&
1958 list_empty(&dep->req_queued)) {
1959 if (list_empty(&dep->request_list)) {
1960 /*
1961 * If there is no entry in request list then do
1962 * not issue END TRANSFER now. Just set PENDING
1963 * flag, so that END TRANSFER is issued when an
1964 * entry is added into request list.
1965 */
1966 dep->flags = DWC3_EP_PENDING_REQUEST;
1967 } else {
Paul Zimmermanb992e682012-04-27 14:17:35 +03001968 dwc3_stop_active_transfer(dwc, dep->number, true);
Pratyush Anandcdc359d2013-01-14 15:59:34 +05301969 dep->flags = DWC3_EP_ENABLED;
1970 }
Pratyush Anand7efea862013-01-14 15:59:32 +05301971 return 1;
1972 }
1973
Felipe Balbi72246da2011-08-19 18:10:58 +03001974 return 1;
1975}
1976
1977static void dwc3_endpoint_transfer_complete(struct dwc3 *dwc,
1978 struct dwc3_ep *dep, const struct dwc3_event_depevt *event,
1979 int start_new)
1980{
1981 unsigned status = 0;
1982 int clean_busy;
1983
1984 if (event->status & DEPEVT_STATUS_BUSERR)
1985 status = -ECONNRESET;
1986
Paul Zimmerman1d046792012-02-15 18:56:56 -08001987 clean_busy = dwc3_cleanup_done_reqs(dwc, dep, event, status);
Paul Zimmermanc2df85c2012-02-24 17:32:18 -08001988 if (clean_busy)
Felipe Balbi72246da2011-08-19 18:10:58 +03001989 dep->flags &= ~DWC3_EP_BUSY;
Felipe Balbifae2b902011-10-14 13:00:30 +03001990
1991 /*
1992 * WORKAROUND: This is the 2nd half of U1/U2 -> U0 workaround.
1993 * See dwc3_gadget_linksts_change_interrupt() for 1st half.
1994 */
1995 if (dwc->revision < DWC3_REVISION_183A) {
1996 u32 reg;
1997 int i;
1998
1999 for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) {
Moiz Sonasath348e0262012-08-01 14:08:30 -05002000 dep = dwc->eps[i];
Felipe Balbifae2b902011-10-14 13:00:30 +03002001
2002 if (!(dep->flags & DWC3_EP_ENABLED))
2003 continue;
2004
2005 if (!list_empty(&dep->req_queued))
2006 return;
2007 }
2008
2009 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2010 reg |= dwc->u1u2;
2011 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2012
2013 dwc->u1u2 = 0;
2014 }
Felipe Balbi72246da2011-08-19 18:10:58 +03002015}
2016
Felipe Balbi72246da2011-08-19 18:10:58 +03002017static void dwc3_endpoint_interrupt(struct dwc3 *dwc,
2018 const struct dwc3_event_depevt *event)
2019{
2020 struct dwc3_ep *dep;
2021 u8 epnum = event->endpoint_number;
2022
2023 dep = dwc->eps[epnum];
2024
Felipe Balbi3336abb2012-06-06 09:19:35 +03002025 if (!(dep->flags & DWC3_EP_ENABLED))
2026 return;
2027
Felipe Balbi72246da2011-08-19 18:10:58 +03002028 dev_vdbg(dwc->dev, "%s: %s\n", dep->name,
2029 dwc3_ep_event_string(event->endpoint_event));
2030
2031 if (epnum == 0 || epnum == 1) {
2032 dwc3_ep0_interrupt(dwc, event);
2033 return;
2034 }
2035
2036 switch (event->endpoint_event) {
2037 case DWC3_DEPEVT_XFERCOMPLETE:
Felipe Balbib4996a82012-06-06 12:04:13 +03002038 dep->resource_index = 0;
Paul Zimmermanc2df85c2012-02-24 17:32:18 -08002039
Ido Shayevitz16e78db2012-03-12 20:25:24 +02002040 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbi72246da2011-08-19 18:10:58 +03002041 dev_dbg(dwc->dev, "%s is an Isochronous endpoint\n",
2042 dep->name);
2043 return;
2044 }
2045
2046 dwc3_endpoint_transfer_complete(dwc, dep, event, 1);
2047 break;
2048 case DWC3_DEPEVT_XFERINPROGRESS:
Ido Shayevitz16e78db2012-03-12 20:25:24 +02002049 if (!usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbi72246da2011-08-19 18:10:58 +03002050 dev_dbg(dwc->dev, "%s is not an Isochronous endpoint\n",
2051 dep->name);
2052 return;
2053 }
2054
2055 dwc3_endpoint_transfer_complete(dwc, dep, event, 0);
2056 break;
2057 case DWC3_DEPEVT_XFERNOTREADY:
Ido Shayevitz16e78db2012-03-12 20:25:24 +02002058 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbi72246da2011-08-19 18:10:58 +03002059 dwc3_gadget_start_isoc(dwc, dep, event);
2060 } else {
2061 int ret;
2062
2063 dev_vdbg(dwc->dev, "%s: reason %s\n",
Felipe Balbi40aa41fb2012-01-18 17:06:03 +02002064 dep->name, event->status &
2065 DEPEVT_STATUS_TRANSFER_ACTIVE
Felipe Balbi72246da2011-08-19 18:10:58 +03002066 ? "Transfer Active"
2067 : "Transfer Not Active");
2068
2069 ret = __dwc3_gadget_kick_transfer(dep, 0, 1);
2070 if (!ret || ret == -EBUSY)
2071 return;
2072
2073 dev_dbg(dwc->dev, "%s: failed to kick transfers\n",
2074 dep->name);
2075 }
2076
2077 break;
Felipe Balbi879631a2011-09-30 10:58:47 +03002078 case DWC3_DEPEVT_STREAMEVT:
Ido Shayevitz16e78db2012-03-12 20:25:24 +02002079 if (!usb_endpoint_xfer_bulk(dep->endpoint.desc)) {
Felipe Balbi879631a2011-09-30 10:58:47 +03002080 dev_err(dwc->dev, "Stream event for non-Bulk %s\n",
2081 dep->name);
2082 return;
2083 }
2084
2085 switch (event->status) {
2086 case DEPEVT_STREAMEVT_FOUND:
2087 dev_vdbg(dwc->dev, "Stream %d found and started\n",
2088 event->parameters);
2089
2090 break;
2091 case DEPEVT_STREAMEVT_NOTFOUND:
2092 /* FALLTHROUGH */
2093 default:
2094 dev_dbg(dwc->dev, "Couldn't find suitable stream\n");
2095 }
2096 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03002097 case DWC3_DEPEVT_RXTXFIFOEVT:
2098 dev_dbg(dwc->dev, "%s FIFO Overrun\n", dep->name);
2099 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03002100 case DWC3_DEPEVT_EPCMDCMPLT:
Felipe Balbiea53b882012-02-17 12:10:04 +02002101 dev_vdbg(dwc->dev, "Endpoint Command Complete\n");
Felipe Balbi72246da2011-08-19 18:10:58 +03002102 break;
2103 }
2104}
2105
2106static void dwc3_disconnect_gadget(struct dwc3 *dwc)
2107{
2108 if (dwc->gadget_driver && dwc->gadget_driver->disconnect) {
2109 spin_unlock(&dwc->lock);
2110 dwc->gadget_driver->disconnect(&dwc->gadget);
2111 spin_lock(&dwc->lock);
2112 }
2113}
2114
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06002115static void dwc3_suspend_gadget(struct dwc3 *dwc)
2116{
Dan Carpenter73a30bf2014-03-07 14:19:57 +03002117 if (dwc->gadget_driver && dwc->gadget_driver->suspend) {
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06002118 spin_unlock(&dwc->lock);
2119 dwc->gadget_driver->suspend(&dwc->gadget);
2120 spin_lock(&dwc->lock);
2121 }
2122}
2123
2124static void dwc3_resume_gadget(struct dwc3 *dwc)
2125{
Dan Carpenter73a30bf2014-03-07 14:19:57 +03002126 if (dwc->gadget_driver && dwc->gadget_driver->resume) {
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06002127 spin_unlock(&dwc->lock);
2128 dwc->gadget_driver->resume(&dwc->gadget);
2129 spin_lock(&dwc->lock);
2130 }
2131}
2132
Paul Zimmermanb992e682012-04-27 14:17:35 +03002133static void dwc3_stop_active_transfer(struct dwc3 *dwc, u32 epnum, bool force)
Felipe Balbi72246da2011-08-19 18:10:58 +03002134{
2135 struct dwc3_ep *dep;
2136 struct dwc3_gadget_ep_cmd_params params;
2137 u32 cmd;
2138 int ret;
2139
2140 dep = dwc->eps[epnum];
2141
Felipe Balbib4996a82012-06-06 12:04:13 +03002142 if (!dep->resource_index)
Pratyush Anand3daf74d2012-06-23 02:23:08 +05302143 return;
2144
Pratyush Anand57911502012-07-06 15:19:10 +05302145 /*
2146 * NOTICE: We are violating what the Databook says about the
2147 * EndTransfer command. Ideally we would _always_ wait for the
2148 * EndTransfer Command Completion IRQ, but that's causing too
2149 * much trouble synchronizing between us and gadget driver.
2150 *
2151 * We have discussed this with the IP Provider and it was
2152 * suggested to giveback all requests here, but give HW some
2153 * extra time to synchronize with the interconnect. We're using
2154 * an arbitraty 100us delay for that.
2155 *
2156 * Note also that a similar handling was tested by Synopsys
2157 * (thanks a lot Paul) and nothing bad has come out of it.
2158 * In short, what we're doing is:
2159 *
2160 * - Issue EndTransfer WITH CMDIOC bit set
2161 * - Wait 100us
2162 */
2163
Pratyush Anand3daf74d2012-06-23 02:23:08 +05302164 cmd = DWC3_DEPCMD_ENDTRANSFER;
Paul Zimmermanb992e682012-04-27 14:17:35 +03002165 cmd |= force ? DWC3_DEPCMD_HIPRI_FORCERM : 0;
2166 cmd |= DWC3_DEPCMD_CMDIOC;
Felipe Balbib4996a82012-06-06 12:04:13 +03002167 cmd |= DWC3_DEPCMD_PARAM(dep->resource_index);
Pratyush Anand3daf74d2012-06-23 02:23:08 +05302168 memset(&params, 0, sizeof(params));
2169 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number, cmd, &params);
2170 WARN_ON_ONCE(ret);
Felipe Balbib4996a82012-06-06 12:04:13 +03002171 dep->resource_index = 0;
Felipe Balbi041d81f2012-10-04 11:58:00 +03002172 dep->flags &= ~DWC3_EP_BUSY;
Pratyush Anand57911502012-07-06 15:19:10 +05302173 udelay(100);
Felipe Balbi72246da2011-08-19 18:10:58 +03002174}
2175
2176static void dwc3_stop_active_transfers(struct dwc3 *dwc)
2177{
2178 u32 epnum;
2179
2180 for (epnum = 2; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
2181 struct dwc3_ep *dep;
2182
2183 dep = dwc->eps[epnum];
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03002184 if (!dep)
2185 continue;
2186
Felipe Balbi72246da2011-08-19 18:10:58 +03002187 if (!(dep->flags & DWC3_EP_ENABLED))
2188 continue;
2189
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +02002190 dwc3_remove_requests(dwc, dep);
Felipe Balbi72246da2011-08-19 18:10:58 +03002191 }
2192}
2193
2194static void dwc3_clear_stall_all_ep(struct dwc3 *dwc)
2195{
2196 u32 epnum;
2197
2198 for (epnum = 1; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
2199 struct dwc3_ep *dep;
2200 struct dwc3_gadget_ep_cmd_params params;
2201 int ret;
2202
2203 dep = dwc->eps[epnum];
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03002204 if (!dep)
2205 continue;
Felipe Balbi72246da2011-08-19 18:10:58 +03002206
2207 if (!(dep->flags & DWC3_EP_STALL))
2208 continue;
2209
2210 dep->flags &= ~DWC3_EP_STALL;
2211
2212 memset(&params, 0, sizeof(params));
2213 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number,
2214 DWC3_DEPCMD_CLEARSTALL, &params);
2215 WARN_ON_ONCE(ret);
2216 }
2217}
2218
2219static void dwc3_gadget_disconnect_interrupt(struct dwc3 *dwc)
2220{
Felipe Balbic4430a22012-05-24 10:30:01 +03002221 int reg;
2222
Felipe Balbi72246da2011-08-19 18:10:58 +03002223 dev_vdbg(dwc->dev, "%s\n", __func__);
Felipe Balbi72246da2011-08-19 18:10:58 +03002224
2225 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2226 reg &= ~DWC3_DCTL_INITU1ENA;
2227 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2228
2229 reg &= ~DWC3_DCTL_INITU2ENA;
2230 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +03002231
Felipe Balbi72246da2011-08-19 18:10:58 +03002232 dwc3_disconnect_gadget(dwc);
Paul Zimmermanb23c8432011-09-30 10:58:42 +03002233 dwc->start_config_issued = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03002234
2235 dwc->gadget.speed = USB_SPEED_UNKNOWN;
Felipe Balbidf62df52011-10-14 15:11:49 +03002236 dwc->setup_packet_pending = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03002237}
2238
Felipe Balbi72246da2011-08-19 18:10:58 +03002239static void dwc3_gadget_reset_interrupt(struct dwc3 *dwc)
2240{
2241 u32 reg;
2242
2243 dev_vdbg(dwc->dev, "%s\n", __func__);
2244
Felipe Balbidf62df52011-10-14 15:11:49 +03002245 /*
2246 * WORKAROUND: DWC3 revisions <1.88a have an issue which
2247 * would cause a missing Disconnect Event if there's a
2248 * pending Setup Packet in the FIFO.
2249 *
2250 * There's no suggested workaround on the official Bug
2251 * report, which states that "unless the driver/application
2252 * is doing any special handling of a disconnect event,
2253 * there is no functional issue".
2254 *
2255 * Unfortunately, it turns out that we _do_ some special
2256 * handling of a disconnect event, namely complete all
2257 * pending transfers, notify gadget driver of the
2258 * disconnection, and so on.
2259 *
2260 * Our suggested workaround is to follow the Disconnect
2261 * Event steps here, instead, based on a setup_packet_pending
2262 * flag. Such flag gets set whenever we have a XferNotReady
2263 * event on EP0 and gets cleared on XferComplete for the
2264 * same endpoint.
2265 *
2266 * Refers to:
2267 *
2268 * STAR#9000466709: RTL: Device : Disconnect event not
2269 * generated if setup packet pending in FIFO
2270 */
2271 if (dwc->revision < DWC3_REVISION_188A) {
2272 if (dwc->setup_packet_pending)
2273 dwc3_gadget_disconnect_interrupt(dwc);
2274 }
2275
Felipe Balbi961906e2011-12-20 15:37:21 +02002276 /* after reset -> Default State */
Felipe Balbi14cd5922011-12-19 13:01:52 +02002277 usb_gadget_set_state(&dwc->gadget, USB_STATE_DEFAULT);
Felipe Balbi961906e2011-12-20 15:37:21 +02002278
Felipe Balbi72246da2011-08-19 18:10:58 +03002279 if (dwc->gadget.speed != USB_SPEED_UNKNOWN)
2280 dwc3_disconnect_gadget(dwc);
2281
2282 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2283 reg &= ~DWC3_DCTL_TSTCTRL_MASK;
2284 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Gerard Cauvy3b637362012-02-10 12:21:18 +02002285 dwc->test_mode = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03002286
2287 dwc3_stop_active_transfers(dwc);
2288 dwc3_clear_stall_all_ep(dwc);
Paul Zimmermanb23c8432011-09-30 10:58:42 +03002289 dwc->start_config_issued = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03002290
2291 /* Reset device address to zero */
2292 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2293 reg &= ~(DWC3_DCFG_DEVADDR_MASK);
2294 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +03002295}
2296
2297static void dwc3_update_ram_clk_sel(struct dwc3 *dwc, u32 speed)
2298{
2299 u32 reg;
2300 u32 usb30_clock = DWC3_GCTL_CLK_BUS;
2301
2302 /*
2303 * We change the clock only at SS but I dunno why I would want to do
2304 * this. Maybe it becomes part of the power saving plan.
2305 */
2306
2307 if (speed != DWC3_DSTS_SUPERSPEED)
2308 return;
2309
2310 /*
2311 * RAMClkSel is reset to 0 after USB reset, so it must be reprogrammed
2312 * each time on Connect Done.
2313 */
2314 if (!usb30_clock)
2315 return;
2316
2317 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
2318 reg |= DWC3_GCTL_RAMCLKSEL(usb30_clock);
2319 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
2320}
2321
Felipe Balbi72246da2011-08-19 18:10:58 +03002322static void dwc3_gadget_conndone_interrupt(struct dwc3 *dwc)
2323{
Felipe Balbi72246da2011-08-19 18:10:58 +03002324 struct dwc3_ep *dep;
2325 int ret;
2326 u32 reg;
2327 u8 speed;
2328
2329 dev_vdbg(dwc->dev, "%s\n", __func__);
2330
Felipe Balbi72246da2011-08-19 18:10:58 +03002331 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
2332 speed = reg & DWC3_DSTS_CONNECTSPD;
2333 dwc->speed = speed;
2334
2335 dwc3_update_ram_clk_sel(dwc, speed);
2336
2337 switch (speed) {
2338 case DWC3_DCFG_SUPERSPEED:
Felipe Balbi05870c52011-10-14 14:51:38 +03002339 /*
2340 * WORKAROUND: DWC3 revisions <1.90a have an issue which
2341 * would cause a missing USB3 Reset event.
2342 *
2343 * In such situations, we should force a USB3 Reset
2344 * event by calling our dwc3_gadget_reset_interrupt()
2345 * routine.
2346 *
2347 * Refers to:
2348 *
2349 * STAR#9000483510: RTL: SS : USB3 reset event may
2350 * not be generated always when the link enters poll
2351 */
2352 if (dwc->revision < DWC3_REVISION_190A)
2353 dwc3_gadget_reset_interrupt(dwc);
2354
Felipe Balbi72246da2011-08-19 18:10:58 +03002355 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
2356 dwc->gadget.ep0->maxpacket = 512;
2357 dwc->gadget.speed = USB_SPEED_SUPER;
2358 break;
2359 case DWC3_DCFG_HIGHSPEED:
2360 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
2361 dwc->gadget.ep0->maxpacket = 64;
2362 dwc->gadget.speed = USB_SPEED_HIGH;
2363 break;
2364 case DWC3_DCFG_FULLSPEED2:
2365 case DWC3_DCFG_FULLSPEED1:
2366 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
2367 dwc->gadget.ep0->maxpacket = 64;
2368 dwc->gadget.speed = USB_SPEED_FULL;
2369 break;
2370 case DWC3_DCFG_LOWSPEED:
2371 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(8);
2372 dwc->gadget.ep0->maxpacket = 8;
2373 dwc->gadget.speed = USB_SPEED_LOW;
2374 break;
2375 }
2376
Pratyush Anand2b758352013-01-14 15:59:31 +05302377 /* Enable USB2 LPM Capability */
2378
2379 if ((dwc->revision > DWC3_REVISION_194A)
2380 && (speed != DWC3_DCFG_SUPERSPEED)) {
2381 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2382 reg |= DWC3_DCFG_LPM_CAP;
2383 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
2384
2385 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2386 reg &= ~(DWC3_DCTL_HIRD_THRES_MASK | DWC3_DCTL_L1_HIBER_EN);
2387
Felipe Balbi1a947742013-01-24 11:56:11 +02002388 /*
2389 * TODO: This should be configurable. For now using
2390 * maximum allowed HIRD threshold value of 0b1100
2391 */
2392 reg |= DWC3_DCTL_HIRD_THRES(12);
Pratyush Anand2b758352013-01-14 15:59:31 +05302393
2394 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Felipe Balbi356363b2013-12-19 16:37:05 -06002395 } else {
2396 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2397 reg &= ~DWC3_DCTL_HIRD_THRES_MASK;
2398 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Pratyush Anand2b758352013-01-14 15:59:31 +05302399 }
2400
Felipe Balbi72246da2011-08-19 18:10:58 +03002401 dep = dwc->eps[0];
Paul Zimmerman265b70a2013-12-19 12:38:49 -06002402 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, true,
2403 false);
Felipe Balbi72246da2011-08-19 18:10:58 +03002404 if (ret) {
2405 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
2406 return;
2407 }
2408
2409 dep = dwc->eps[1];
Paul Zimmerman265b70a2013-12-19 12:38:49 -06002410 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, true,
2411 false);
Felipe Balbi72246da2011-08-19 18:10:58 +03002412 if (ret) {
2413 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
2414 return;
2415 }
2416
2417 /*
2418 * Configure PHY via GUSB3PIPECTLn if required.
2419 *
2420 * Update GTXFIFOSIZn
2421 *
2422 * In both cases reset values should be sufficient.
2423 */
2424}
2425
2426static void dwc3_gadget_wakeup_interrupt(struct dwc3 *dwc)
2427{
2428 dev_vdbg(dwc->dev, "%s\n", __func__);
2429
2430 /*
2431 * TODO take core out of low power mode when that's
2432 * implemented.
2433 */
2434
2435 dwc->gadget_driver->resume(&dwc->gadget);
2436}
2437
2438static void dwc3_gadget_linksts_change_interrupt(struct dwc3 *dwc,
2439 unsigned int evtinfo)
2440{
Felipe Balbifae2b902011-10-14 13:00:30 +03002441 enum dwc3_link_state next = evtinfo & DWC3_LINK_STATE_MASK;
Felipe Balbi0b0cc1c2012-09-18 21:39:24 +03002442 unsigned int pwropt;
2443
2444 /*
2445 * WORKAROUND: DWC3 < 2.50a have an issue when configured without
2446 * Hibernation mode enabled which would show up when device detects
2447 * host-initiated U3 exit.
2448 *
2449 * In that case, device will generate a Link State Change Interrupt
2450 * from U3 to RESUME which is only necessary if Hibernation is
2451 * configured in.
2452 *
2453 * There are no functional changes due to such spurious event and we
2454 * just need to ignore it.
2455 *
2456 * Refers to:
2457 *
2458 * STAR#9000570034 RTL: SS Resume event generated in non-Hibernation
2459 * operational mode
2460 */
2461 pwropt = DWC3_GHWPARAMS1_EN_PWROPT(dwc->hwparams.hwparams1);
2462 if ((dwc->revision < DWC3_REVISION_250A) &&
2463 (pwropt != DWC3_GHWPARAMS1_EN_PWROPT_HIB)) {
2464 if ((dwc->link_state == DWC3_LINK_STATE_U3) &&
2465 (next == DWC3_LINK_STATE_RESUME)) {
2466 dev_vdbg(dwc->dev, "ignoring transition U3 -> Resume\n");
2467 return;
2468 }
2469 }
Felipe Balbifae2b902011-10-14 13:00:30 +03002470
2471 /*
2472 * WORKAROUND: DWC3 Revisions <1.83a have an issue which, depending
2473 * on the link partner, the USB session might do multiple entry/exit
2474 * of low power states before a transfer takes place.
2475 *
2476 * Due to this problem, we might experience lower throughput. The
2477 * suggested workaround is to disable DCTL[12:9] bits if we're
2478 * transitioning from U1/U2 to U0 and enable those bits again
2479 * after a transfer completes and there are no pending transfers
2480 * on any of the enabled endpoints.
2481 *
2482 * This is the first half of that workaround.
2483 *
2484 * Refers to:
2485 *
2486 * STAR#9000446952: RTL: Device SS : if U1/U2 ->U0 takes >128us
2487 * core send LGO_Ux entering U0
2488 */
2489 if (dwc->revision < DWC3_REVISION_183A) {
2490 if (next == DWC3_LINK_STATE_U0) {
2491 u32 u1u2;
2492 u32 reg;
2493
2494 switch (dwc->link_state) {
2495 case DWC3_LINK_STATE_U1:
2496 case DWC3_LINK_STATE_U2:
2497 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2498 u1u2 = reg & (DWC3_DCTL_INITU2ENA
2499 | DWC3_DCTL_ACCEPTU2ENA
2500 | DWC3_DCTL_INITU1ENA
2501 | DWC3_DCTL_ACCEPTU1ENA);
2502
2503 if (!dwc->u1u2)
2504 dwc->u1u2 = reg & u1u2;
2505
2506 reg &= ~u1u2;
2507
2508 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2509 break;
2510 default:
2511 /* do nothing */
2512 break;
2513 }
2514 }
2515 }
2516
Felipe Balbibc5ba2e2014-02-26 10:17:07 -06002517 switch (next) {
2518 case DWC3_LINK_STATE_U1:
2519 if (dwc->speed == USB_SPEED_SUPER)
2520 dwc3_suspend_gadget(dwc);
2521 break;
2522 case DWC3_LINK_STATE_U2:
2523 case DWC3_LINK_STATE_U3:
2524 dwc3_suspend_gadget(dwc);
2525 break;
2526 case DWC3_LINK_STATE_RESUME:
2527 dwc3_resume_gadget(dwc);
2528 break;
2529 default:
2530 /* do nothing */
2531 break;
2532 }
2533
Felipe Balbie57ebc12014-04-22 13:20:12 -05002534 dev_vdbg(dwc->dev, "link change: %s [%d] -> %s [%d]\n",
2535 dwc3_gadget_link_string(dwc->link_state),
2536 dwc->link_state, dwc3_gadget_link_string(next), next);
2537
2538 dwc->link_state = next;
Felipe Balbi72246da2011-08-19 18:10:58 +03002539}
2540
Felipe Balbie1dadd32014-02-25 14:47:54 -06002541static void dwc3_gadget_hibernation_interrupt(struct dwc3 *dwc,
2542 unsigned int evtinfo)
2543{
2544 unsigned int is_ss = evtinfo & BIT(4);
2545
2546 /**
2547 * WORKAROUND: DWC3 revison 2.20a with hibernation support
2548 * have a known issue which can cause USB CV TD.9.23 to fail
2549 * randomly.
2550 *
2551 * Because of this issue, core could generate bogus hibernation
2552 * events which SW needs to ignore.
2553 *
2554 * Refers to:
2555 *
2556 * STAR#9000546576: Device Mode Hibernation: Issue in USB 2.0
2557 * Device Fallback from SuperSpeed
2558 */
2559 if (is_ss ^ (dwc->speed == USB_SPEED_SUPER))
2560 return;
2561
2562 /* enter hibernation here */
2563}
2564
Felipe Balbi72246da2011-08-19 18:10:58 +03002565static void dwc3_gadget_interrupt(struct dwc3 *dwc,
2566 const struct dwc3_event_devt *event)
2567{
2568 switch (event->type) {
2569 case DWC3_DEVICE_EVENT_DISCONNECT:
2570 dwc3_gadget_disconnect_interrupt(dwc);
2571 break;
2572 case DWC3_DEVICE_EVENT_RESET:
2573 dwc3_gadget_reset_interrupt(dwc);
2574 break;
2575 case DWC3_DEVICE_EVENT_CONNECT_DONE:
2576 dwc3_gadget_conndone_interrupt(dwc);
2577 break;
2578 case DWC3_DEVICE_EVENT_WAKEUP:
2579 dwc3_gadget_wakeup_interrupt(dwc);
2580 break;
Felipe Balbie1dadd32014-02-25 14:47:54 -06002581 case DWC3_DEVICE_EVENT_HIBER_REQ:
2582 if (dev_WARN_ONCE(dwc->dev, !dwc->has_hibernation,
2583 "unexpected hibernation event\n"))
2584 break;
2585
2586 dwc3_gadget_hibernation_interrupt(dwc, event->event_info);
2587 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03002588 case DWC3_DEVICE_EVENT_LINK_STATUS_CHANGE:
2589 dwc3_gadget_linksts_change_interrupt(dwc, event->event_info);
2590 break;
2591 case DWC3_DEVICE_EVENT_EOPF:
2592 dev_vdbg(dwc->dev, "End of Periodic Frame\n");
2593 break;
2594 case DWC3_DEVICE_EVENT_SOF:
2595 dev_vdbg(dwc->dev, "Start of Periodic Frame\n");
2596 break;
2597 case DWC3_DEVICE_EVENT_ERRATIC_ERROR:
2598 dev_vdbg(dwc->dev, "Erratic Error\n");
2599 break;
2600 case DWC3_DEVICE_EVENT_CMD_CMPL:
2601 dev_vdbg(dwc->dev, "Command Complete\n");
2602 break;
2603 case DWC3_DEVICE_EVENT_OVERFLOW:
2604 dev_vdbg(dwc->dev, "Overflow\n");
2605 break;
2606 default:
2607 dev_dbg(dwc->dev, "UNKNOWN IRQ %d\n", event->type);
2608 }
2609}
2610
2611static void dwc3_process_event_entry(struct dwc3 *dwc,
2612 const union dwc3_event *event)
2613{
2614 /* Endpoint IRQ, handle it and return early */
2615 if (event->type.is_devspec == 0) {
2616 /* depevt */
2617 return dwc3_endpoint_interrupt(dwc, &event->depevt);
2618 }
2619
2620 switch (event->type.type) {
2621 case DWC3_EVENT_TYPE_DEV:
2622 dwc3_gadget_interrupt(dwc, &event->devt);
2623 break;
2624 /* REVISIT what to do with Carkit and I2C events ? */
2625 default:
2626 dev_err(dwc->dev, "UNKNOWN IRQ type %d\n", event->raw);
2627 }
2628}
2629
Felipe Balbif42f2442013-06-12 21:25:08 +03002630static irqreturn_t dwc3_process_event_buf(struct dwc3 *dwc, u32 buf)
2631{
2632 struct dwc3_event_buffer *evt;
2633 irqreturn_t ret = IRQ_NONE;
2634 int left;
2635 u32 reg;
2636
2637 evt = dwc->ev_buffs[buf];
2638 left = evt->count;
2639
2640 if (!(evt->flags & DWC3_EVENT_PENDING))
2641 return IRQ_NONE;
2642
2643 while (left > 0) {
2644 union dwc3_event event;
2645
2646 event.raw = *(u32 *) (evt->buf + evt->lpos);
2647
2648 dwc3_process_event_entry(dwc, &event);
2649
2650 /*
2651 * FIXME we wrap around correctly to the next entry as
2652 * almost all entries are 4 bytes in size. There is one
2653 * entry which has 12 bytes which is a regular entry
2654 * followed by 8 bytes data. ATM I don't know how
2655 * things are organized if we get next to the a
2656 * boundary so I worry about that once we try to handle
2657 * that.
2658 */
2659 evt->lpos = (evt->lpos + 4) % DWC3_EVENT_BUFFERS_SIZE;
2660 left -= 4;
2661
2662 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(buf), 4);
2663 }
2664
2665 evt->count = 0;
2666 evt->flags &= ~DWC3_EVENT_PENDING;
2667 ret = IRQ_HANDLED;
2668
2669 /* Unmask interrupt */
2670 reg = dwc3_readl(dwc->regs, DWC3_GEVNTSIZ(buf));
2671 reg &= ~DWC3_GEVNTSIZ_INTMASK;
2672 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(buf), reg);
2673
2674 return ret;
2675}
2676
Felipe Balbib15a7622011-06-30 16:57:15 +03002677static irqreturn_t dwc3_thread_interrupt(int irq, void *_dwc)
2678{
2679 struct dwc3 *dwc = _dwc;
2680 unsigned long flags;
2681 irqreturn_t ret = IRQ_NONE;
2682 int i;
2683
2684 spin_lock_irqsave(&dwc->lock, flags);
2685
Felipe Balbif42f2442013-06-12 21:25:08 +03002686 for (i = 0; i < dwc->num_event_buffers; i++)
2687 ret |= dwc3_process_event_buf(dwc, i);
Felipe Balbib15a7622011-06-30 16:57:15 +03002688
2689 spin_unlock_irqrestore(&dwc->lock, flags);
2690
2691 return ret;
2692}
2693
Felipe Balbi7f97aa92013-06-12 21:16:11 +03002694static irqreturn_t dwc3_check_event_buf(struct dwc3 *dwc, u32 buf)
Felipe Balbi72246da2011-08-19 18:10:58 +03002695{
2696 struct dwc3_event_buffer *evt;
Felipe Balbi72246da2011-08-19 18:10:58 +03002697 u32 count;
Felipe Balbie8adfc32013-06-12 21:11:14 +03002698 u32 reg;
Felipe Balbi72246da2011-08-19 18:10:58 +03002699
Felipe Balbib15a7622011-06-30 16:57:15 +03002700 evt = dwc->ev_buffs[buf];
2701
Felipe Balbi72246da2011-08-19 18:10:58 +03002702 count = dwc3_readl(dwc->regs, DWC3_GEVNTCOUNT(buf));
2703 count &= DWC3_GEVNTCOUNT_MASK;
2704 if (!count)
2705 return IRQ_NONE;
2706
Felipe Balbib15a7622011-06-30 16:57:15 +03002707 evt->count = count;
2708 evt->flags |= DWC3_EVENT_PENDING;
Felipe Balbi72246da2011-08-19 18:10:58 +03002709
Felipe Balbie8adfc32013-06-12 21:11:14 +03002710 /* Mask interrupt */
2711 reg = dwc3_readl(dwc->regs, DWC3_GEVNTSIZ(buf));
2712 reg |= DWC3_GEVNTSIZ_INTMASK;
2713 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(buf), reg);
2714
Felipe Balbib15a7622011-06-30 16:57:15 +03002715 return IRQ_WAKE_THREAD;
Felipe Balbi72246da2011-08-19 18:10:58 +03002716}
2717
2718static irqreturn_t dwc3_interrupt(int irq, void *_dwc)
2719{
2720 struct dwc3 *dwc = _dwc;
2721 int i;
2722 irqreturn_t ret = IRQ_NONE;
2723
2724 spin_lock(&dwc->lock);
2725
Felipe Balbi9f622b22011-10-12 10:31:04 +03002726 for (i = 0; i < dwc->num_event_buffers; i++) {
Felipe Balbi72246da2011-08-19 18:10:58 +03002727 irqreturn_t status;
2728
Felipe Balbi7f97aa92013-06-12 21:16:11 +03002729 status = dwc3_check_event_buf(dwc, i);
Felipe Balbib15a7622011-06-30 16:57:15 +03002730 if (status == IRQ_WAKE_THREAD)
Felipe Balbi72246da2011-08-19 18:10:58 +03002731 ret = status;
2732 }
2733
2734 spin_unlock(&dwc->lock);
2735
2736 return ret;
2737}
2738
2739/**
2740 * dwc3_gadget_init - Initializes gadget related registers
Paul Zimmerman1d046792012-02-15 18:56:56 -08002741 * @dwc: pointer to our controller context structure
Felipe Balbi72246da2011-08-19 18:10:58 +03002742 *
2743 * Returns 0 on success otherwise negative errno.
2744 */
Bill Pemberton41ac7b32012-11-19 13:21:48 -05002745int dwc3_gadget_init(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +03002746{
Felipe Balbi72246da2011-08-19 18:10:58 +03002747 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03002748
2749 dwc->ctrl_req = dma_alloc_coherent(dwc->dev, sizeof(*dwc->ctrl_req),
2750 &dwc->ctrl_req_addr, GFP_KERNEL);
2751 if (!dwc->ctrl_req) {
2752 dev_err(dwc->dev, "failed to allocate ctrl request\n");
2753 ret = -ENOMEM;
2754 goto err0;
2755 }
2756
2757 dwc->ep0_trb = dma_alloc_coherent(dwc->dev, sizeof(*dwc->ep0_trb),
2758 &dwc->ep0_trb_addr, GFP_KERNEL);
2759 if (!dwc->ep0_trb) {
2760 dev_err(dwc->dev, "failed to allocate ep0 trb\n");
2761 ret = -ENOMEM;
2762 goto err1;
2763 }
2764
Felipe Balbi3ef35fa2012-05-04 12:58:14 +03002765 dwc->setup_buf = kzalloc(DWC3_EP0_BOUNCE_SIZE, GFP_KERNEL);
Felipe Balbi72246da2011-08-19 18:10:58 +03002766 if (!dwc->setup_buf) {
2767 dev_err(dwc->dev, "failed to allocate setup buffer\n");
2768 ret = -ENOMEM;
2769 goto err2;
2770 }
2771
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002772 dwc->ep0_bounce = dma_alloc_coherent(dwc->dev,
Felipe Balbi3ef35fa2012-05-04 12:58:14 +03002773 DWC3_EP0_BOUNCE_SIZE, &dwc->ep0_bounce_addr,
2774 GFP_KERNEL);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002775 if (!dwc->ep0_bounce) {
2776 dev_err(dwc->dev, "failed to allocate ep0 bounce buffer\n");
2777 ret = -ENOMEM;
2778 goto err3;
2779 }
2780
Felipe Balbi72246da2011-08-19 18:10:58 +03002781 dwc->gadget.ops = &dwc3_gadget_ops;
Michal Nazarewiczd327ab52011-11-19 18:27:37 +01002782 dwc->gadget.max_speed = USB_SPEED_SUPER;
Felipe Balbi72246da2011-08-19 18:10:58 +03002783 dwc->gadget.speed = USB_SPEED_UNKNOWN;
Felipe Balbieeb720f2011-11-28 12:46:59 +02002784 dwc->gadget.sg_supported = true;
Felipe Balbi72246da2011-08-19 18:10:58 +03002785 dwc->gadget.name = "dwc3-gadget";
2786
2787 /*
David Cohena4b9d942013-12-09 15:55:38 -08002788 * Per databook, DWC3 needs buffer size to be aligned to MaxPacketSize
2789 * on ep out.
2790 */
2791 dwc->gadget.quirk_ep_out_aligned_size = true;
2792
2793 /*
Felipe Balbi72246da2011-08-19 18:10:58 +03002794 * REVISIT: Here we should clear all pending IRQs to be
2795 * sure we're starting from a well known location.
2796 */
2797
2798 ret = dwc3_gadget_init_endpoints(dwc);
2799 if (ret)
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002800 goto err4;
Felipe Balbi72246da2011-08-19 18:10:58 +03002801
Felipe Balbi72246da2011-08-19 18:10:58 +03002802 ret = usb_add_gadget_udc(dwc->dev, &dwc->gadget);
2803 if (ret) {
2804 dev_err(dwc->dev, "failed to register udc\n");
David Cohene1f80462013-09-11 17:42:47 -07002805 goto err4;
Felipe Balbi72246da2011-08-19 18:10:58 +03002806 }
2807
2808 return 0;
2809
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002810err4:
David Cohene1f80462013-09-11 17:42:47 -07002811 dwc3_gadget_free_endpoints(dwc);
Felipe Balbi3ef35fa2012-05-04 12:58:14 +03002812 dma_free_coherent(dwc->dev, DWC3_EP0_BOUNCE_SIZE,
2813 dwc->ep0_bounce, dwc->ep0_bounce_addr);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002814
Felipe Balbi72246da2011-08-19 18:10:58 +03002815err3:
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02002816 kfree(dwc->setup_buf);
Felipe Balbi72246da2011-08-19 18:10:58 +03002817
2818err2:
2819 dma_free_coherent(dwc->dev, sizeof(*dwc->ep0_trb),
2820 dwc->ep0_trb, dwc->ep0_trb_addr);
2821
2822err1:
2823 dma_free_coherent(dwc->dev, sizeof(*dwc->ctrl_req),
2824 dwc->ctrl_req, dwc->ctrl_req_addr);
2825
2826err0:
2827 return ret;
2828}
2829
Felipe Balbi7415f172012-04-30 14:56:33 +03002830/* -------------------------------------------------------------------------- */
2831
Felipe Balbi72246da2011-08-19 18:10:58 +03002832void dwc3_gadget_exit(struct dwc3 *dwc)
2833{
Felipe Balbi72246da2011-08-19 18:10:58 +03002834 usb_del_gadget_udc(&dwc->gadget);
Felipe Balbi72246da2011-08-19 18:10:58 +03002835
Felipe Balbi72246da2011-08-19 18:10:58 +03002836 dwc3_gadget_free_endpoints(dwc);
2837
Felipe Balbi3ef35fa2012-05-04 12:58:14 +03002838 dma_free_coherent(dwc->dev, DWC3_EP0_BOUNCE_SIZE,
2839 dwc->ep0_bounce, dwc->ep0_bounce_addr);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002840
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02002841 kfree(dwc->setup_buf);
Felipe Balbi72246da2011-08-19 18:10:58 +03002842
2843 dma_free_coherent(dwc->dev, sizeof(*dwc->ep0_trb),
2844 dwc->ep0_trb, dwc->ep0_trb_addr);
2845
2846 dma_free_coherent(dwc->dev, sizeof(*dwc->ctrl_req),
2847 dwc->ctrl_req, dwc->ctrl_req_addr);
Felipe Balbi72246da2011-08-19 18:10:58 +03002848}
Felipe Balbi7415f172012-04-30 14:56:33 +03002849
2850int dwc3_gadget_prepare(struct dwc3 *dwc)
2851{
Felipe Balbi7b2a0362013-12-19 13:43:19 -06002852 if (dwc->pullups_connected) {
Felipe Balbi7415f172012-04-30 14:56:33 +03002853 dwc3_gadget_disable_irq(dwc);
Felipe Balbi7b2a0362013-12-19 13:43:19 -06002854 dwc3_gadget_run_stop(dwc, true, true);
2855 }
Felipe Balbi7415f172012-04-30 14:56:33 +03002856
2857 return 0;
2858}
2859
2860void dwc3_gadget_complete(struct dwc3 *dwc)
2861{
2862 if (dwc->pullups_connected) {
2863 dwc3_gadget_enable_irq(dwc);
Felipe Balbi7b2a0362013-12-19 13:43:19 -06002864 dwc3_gadget_run_stop(dwc, true, false);
Felipe Balbi7415f172012-04-30 14:56:33 +03002865 }
2866}
2867
2868int dwc3_gadget_suspend(struct dwc3 *dwc)
2869{
2870 __dwc3_gadget_ep_disable(dwc->eps[0]);
2871 __dwc3_gadget_ep_disable(dwc->eps[1]);
2872
2873 dwc->dcfg = dwc3_readl(dwc->regs, DWC3_DCFG);
2874
2875 return 0;
2876}
2877
2878int dwc3_gadget_resume(struct dwc3 *dwc)
2879{
2880 struct dwc3_ep *dep;
2881 int ret;
2882
2883 /* Start with SuperSpeed Default */
2884 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
2885
2886 dep = dwc->eps[0];
Paul Zimmerman265b70a2013-12-19 12:38:49 -06002887 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, false,
2888 false);
Felipe Balbi7415f172012-04-30 14:56:33 +03002889 if (ret)
2890 goto err0;
2891
2892 dep = dwc->eps[1];
Paul Zimmerman265b70a2013-12-19 12:38:49 -06002893 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, false,
2894 false);
Felipe Balbi7415f172012-04-30 14:56:33 +03002895 if (ret)
2896 goto err1;
2897
2898 /* begin to receive SETUP packets */
2899 dwc->ep0state = EP0_SETUP_PHASE;
2900 dwc3_ep0_out_start(dwc);
2901
2902 dwc3_writel(dwc->regs, DWC3_DCFG, dwc->dcfg);
2903
2904 return 0;
2905
2906err1:
2907 __dwc3_gadget_ep_disable(dwc->eps[0]);
2908
2909err0:
2910 return ret;
2911}