blob: 7f4e6dd63c00067120ab293dc893e88cfca15d0a [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/**
71 * dwc3_gadget_set_link_state - Sets USB Link to a particular State
72 * @dwc: pointer to our context structure
73 * @state: the state to put link into
74 *
75 * Caller should take care of locking. This function will
Paul Zimmermanaee63e32012-02-24 17:32:15 -080076 * return 0 on success or -ETIMEDOUT.
Felipe Balbi8598bde2012-01-02 18:55:57 +020077 */
78int dwc3_gadget_set_link_state(struct dwc3 *dwc, enum dwc3_link_state state)
79{
Paul Zimmermanaee63e32012-02-24 17:32:15 -080080 int retries = 10000;
Felipe Balbi8598bde2012-01-02 18:55:57 +020081 u32 reg;
82
Paul Zimmerman802fde92012-04-27 13:10:52 +030083 /*
84 * Wait until device controller is ready. Only applies to 1.94a and
85 * later RTL.
86 */
87 if (dwc->revision >= DWC3_REVISION_194A) {
88 while (--retries) {
89 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
90 if (reg & DWC3_DSTS_DCNRD)
91 udelay(5);
92 else
93 break;
94 }
95
96 if (retries <= 0)
97 return -ETIMEDOUT;
98 }
99
Felipe Balbi8598bde2012-01-02 18:55:57 +0200100 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
101 reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK;
102
103 /* set requested state */
104 reg |= DWC3_DCTL_ULSTCHNGREQ(state);
105 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
106
Paul Zimmerman802fde92012-04-27 13:10:52 +0300107 /*
108 * The following code is racy when called from dwc3_gadget_wakeup,
109 * and is not needed, at least on newer versions
110 */
111 if (dwc->revision >= DWC3_REVISION_194A)
112 return 0;
113
Felipe Balbi8598bde2012-01-02 18:55:57 +0200114 /* wait for a change in DSTS */
Paul Zimmermanaed430e2012-04-27 12:52:01 +0300115 retries = 10000;
Felipe Balbi8598bde2012-01-02 18:55:57 +0200116 while (--retries) {
117 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
118
Felipe Balbi8598bde2012-01-02 18:55:57 +0200119 if (DWC3_DSTS_USBLNKST(reg) == state)
120 return 0;
121
Paul Zimmermanaee63e32012-02-24 17:32:15 -0800122 udelay(5);
Felipe Balbi8598bde2012-01-02 18:55:57 +0200123 }
124
125 dev_vdbg(dwc->dev, "link state change request timed out\n");
126
127 return -ETIMEDOUT;
128}
129
Felipe Balbi457e84b2012-01-18 18:04:09 +0200130/**
131 * dwc3_gadget_resize_tx_fifos - reallocate fifo spaces for current use-case
132 * @dwc: pointer to our context structure
133 *
134 * This function will a best effort FIFO allocation in order
135 * to improve FIFO usage and throughput, while still allowing
136 * us to enable as many endpoints as possible.
137 *
138 * Keep in mind that this operation will be highly dependent
139 * on the configured size for RAM1 - which contains TxFifo -,
140 * the amount of endpoints enabled on coreConsultant tool, and
141 * the width of the Master Bus.
142 *
143 * In the ideal world, we would always be able to satisfy the
144 * following equation:
145 *
146 * ((512 + 2 * MDWIDTH-Bytes) + (Number of IN Endpoints - 1) * \
147 * (3 * (1024 + MDWIDTH-Bytes) + MDWIDTH-Bytes)) / MDWIDTH-Bytes
148 *
149 * Unfortunately, due to many variables that's not always the case.
150 */
151int dwc3_gadget_resize_tx_fifos(struct dwc3 *dwc)
152{
153 int last_fifo_depth = 0;
154 int ram1_depth;
155 int fifo_size;
156 int mdwidth;
157 int num;
158
159 if (!dwc->needs_fifo_resize)
160 return 0;
161
162 ram1_depth = DWC3_RAM1_DEPTH(dwc->hwparams.hwparams7);
163 mdwidth = DWC3_MDWIDTH(dwc->hwparams.hwparams0);
164
165 /* MDWIDTH is represented in bits, we need it in bytes */
166 mdwidth >>= 3;
167
168 /*
169 * FIXME For now we will only allocate 1 wMaxPacketSize space
170 * for each enabled endpoint, later patches will come to
171 * improve this algorithm so that we better use the internal
172 * FIFO space
173 */
174 for (num = 0; num < DWC3_ENDPOINTS_NUM; num++) {
175 struct dwc3_ep *dep = dwc->eps[num];
176 int fifo_number = dep->number >> 1;
Felipe Balbi2e81c362012-02-02 13:01:12 +0200177 int mult = 1;
Felipe Balbi457e84b2012-01-18 18:04:09 +0200178 int tmp;
179
180 if (!(dep->number & 1))
181 continue;
182
183 if (!(dep->flags & DWC3_EP_ENABLED))
184 continue;
185
Ido Shayevitz16e78db2012-03-12 20:25:24 +0200186 if (usb_endpoint_xfer_bulk(dep->endpoint.desc)
187 || usb_endpoint_xfer_isoc(dep->endpoint.desc))
Felipe Balbi2e81c362012-02-02 13:01:12 +0200188 mult = 3;
189
190 /*
191 * REVISIT: the following assumes we will always have enough
192 * space available on the FIFO RAM for all possible use cases.
193 * Make sure that's true somehow and change FIFO allocation
194 * accordingly.
195 *
196 * If we have Bulk or Isochronous endpoints, we want
197 * them to be able to be very, very fast. So we're giving
198 * those endpoints a fifo_size which is enough for 3 full
199 * packets
200 */
201 tmp = mult * (dep->endpoint.maxpacket + mdwidth);
Felipe Balbi457e84b2012-01-18 18:04:09 +0200202 tmp += mdwidth;
203
204 fifo_size = DIV_ROUND_UP(tmp, mdwidth);
Felipe Balbi2e81c362012-02-02 13:01:12 +0200205
Felipe Balbi457e84b2012-01-18 18:04:09 +0200206 fifo_size |= (last_fifo_depth << 16);
207
208 dev_vdbg(dwc->dev, "%s: Fifo Addr %04x Size %d\n",
209 dep->name, last_fifo_depth, fifo_size & 0xffff);
210
211 dwc3_writel(dwc->regs, DWC3_GTXFIFOSIZ(fifo_number),
212 fifo_size);
213
214 last_fifo_depth += (fifo_size & 0xffff);
215 }
216
217 return 0;
218}
219
Felipe Balbi72246da2011-08-19 18:10:58 +0300220void dwc3_gadget_giveback(struct dwc3_ep *dep, struct dwc3_request *req,
221 int status)
222{
223 struct dwc3 *dwc = dep->dwc;
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530224 int i;
Felipe Balbi72246da2011-08-19 18:10:58 +0300225
226 if (req->queued) {
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530227 i = 0;
228 do {
Felipe Balbieeb720f2011-11-28 12:46:59 +0200229 dep->busy_slot++;
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530230 /*
231 * Skip LINK TRB. We can't use req->trb and check for
232 * DWC3_TRBCTL_LINK_TRB because it points the TRB we
233 * just completed (not the LINK TRB).
234 */
235 if (((dep->busy_slot & DWC3_TRB_MASK) ==
236 DWC3_TRB_NUM- 1) &&
Ido Shayevitz16e78db2012-03-12 20:25:24 +0200237 usb_endpoint_xfer_isoc(dep->endpoint.desc))
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530238 dep->busy_slot++;
239 } while(++i < req->request.num_mapped_sgs);
Pratyush Anandc9fda7d2013-01-14 15:59:38 +0530240 req->queued = false;
Felipe Balbi72246da2011-08-19 18:10:58 +0300241 }
242 list_del(&req->list);
Felipe Balbieeb720f2011-11-28 12:46:59 +0200243 req->trb = NULL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300244
245 if (req->request.status == -EINPROGRESS)
246 req->request.status = status;
247
Pratyush Anand0416e492012-08-10 13:42:16 +0530248 if (dwc->ep0_bounced && dep->number == 0)
249 dwc->ep0_bounced = false;
250 else
251 usb_gadget_unmap_request(&dwc->gadget, &req->request,
252 req->direction);
Felipe Balbi72246da2011-08-19 18:10:58 +0300253
254 dev_dbg(dwc->dev, "request %p from %s completed %d/%d ===> %d\n",
255 req, dep->name, req->request.actual,
256 req->request.length, status);
257
258 spin_unlock(&dwc->lock);
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +0200259 req->request.complete(&dep->endpoint, &req->request);
Felipe Balbi72246da2011-08-19 18:10:58 +0300260 spin_lock(&dwc->lock);
261}
262
263static const char *dwc3_gadget_ep_cmd_string(u8 cmd)
264{
265 switch (cmd) {
266 case DWC3_DEPCMD_DEPSTARTCFG:
267 return "Start New Configuration";
268 case DWC3_DEPCMD_ENDTRANSFER:
269 return "End Transfer";
270 case DWC3_DEPCMD_UPDATETRANSFER:
271 return "Update Transfer";
272 case DWC3_DEPCMD_STARTTRANSFER:
273 return "Start Transfer";
274 case DWC3_DEPCMD_CLEARSTALL:
275 return "Clear Stall";
276 case DWC3_DEPCMD_SETSTALL:
277 return "Set Stall";
Paul Zimmerman802fde92012-04-27 13:10:52 +0300278 case DWC3_DEPCMD_GETEPSTATE:
279 return "Get Endpoint State";
Felipe Balbi72246da2011-08-19 18:10:58 +0300280 case DWC3_DEPCMD_SETTRANSFRESOURCE:
281 return "Set Endpoint Transfer Resource";
282 case DWC3_DEPCMD_SETEPCONFIG:
283 return "Set Endpoint Configuration";
284 default:
285 return "UNKNOWN command";
286 }
287}
288
Felipe Balbib09bb642012-04-24 16:19:11 +0300289int dwc3_send_gadget_generic_command(struct dwc3 *dwc, int cmd, u32 param)
290{
291 u32 timeout = 500;
292 u32 reg;
293
294 dwc3_writel(dwc->regs, DWC3_DGCMDPAR, param);
295 dwc3_writel(dwc->regs, DWC3_DGCMD, cmd | DWC3_DGCMD_CMDACT);
296
297 do {
298 reg = dwc3_readl(dwc->regs, DWC3_DGCMD);
299 if (!(reg & DWC3_DGCMD_CMDACT)) {
300 dev_vdbg(dwc->dev, "Command Complete --> %d\n",
301 DWC3_DGCMD_STATUS(reg));
302 return 0;
303 }
304
305 /*
306 * We can't sleep here, because it's also called from
307 * interrupt context.
308 */
309 timeout--;
310 if (!timeout)
311 return -ETIMEDOUT;
312 udelay(1);
313 } while (1);
314}
315
Felipe Balbi72246da2011-08-19 18:10:58 +0300316int dwc3_send_gadget_ep_cmd(struct dwc3 *dwc, unsigned ep,
317 unsigned cmd, struct dwc3_gadget_ep_cmd_params *params)
318{
319 struct dwc3_ep *dep = dwc->eps[ep];
Sebastian Andrzej Siewior61d58242011-08-29 16:46:38 +0200320 u32 timeout = 500;
Felipe Balbi72246da2011-08-19 18:10:58 +0300321 u32 reg;
322
323 dev_vdbg(dwc->dev, "%s: cmd '%s' params %08x %08x %08x\n",
324 dep->name,
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300325 dwc3_gadget_ep_cmd_string(cmd), params->param0,
326 params->param1, params->param2);
Felipe Balbi72246da2011-08-19 18:10:58 +0300327
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300328 dwc3_writel(dwc->regs, DWC3_DEPCMDPAR0(ep), params->param0);
329 dwc3_writel(dwc->regs, DWC3_DEPCMDPAR1(ep), params->param1);
330 dwc3_writel(dwc->regs, DWC3_DEPCMDPAR2(ep), params->param2);
Felipe Balbi72246da2011-08-19 18:10:58 +0300331
332 dwc3_writel(dwc->regs, DWC3_DEPCMD(ep), cmd | DWC3_DEPCMD_CMDACT);
333 do {
334 reg = dwc3_readl(dwc->regs, DWC3_DEPCMD(ep));
335 if (!(reg & DWC3_DEPCMD_CMDACT)) {
Felipe Balbi164f6e12011-08-27 20:29:58 +0300336 dev_vdbg(dwc->dev, "Command Complete --> %d\n",
337 DWC3_DEPCMD_STATUS(reg));
Felipe Balbi72246da2011-08-19 18:10:58 +0300338 return 0;
339 }
340
341 /*
Felipe Balbi72246da2011-08-19 18:10:58 +0300342 * We can't sleep here, because it is also called from
343 * interrupt context.
344 */
345 timeout--;
346 if (!timeout)
347 return -ETIMEDOUT;
348
Sebastian Andrzej Siewior61d58242011-08-29 16:46:38 +0200349 udelay(1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300350 } while (1);
351}
352
353static dma_addr_t dwc3_trb_dma_offset(struct dwc3_ep *dep,
Felipe Balbif6bafc62012-02-06 11:04:53 +0200354 struct dwc3_trb *trb)
Felipe Balbi72246da2011-08-19 18:10:58 +0300355{
Paul Zimmermanc439ef82011-09-30 10:58:45 +0300356 u32 offset = (char *) trb - (char *) dep->trb_pool;
Felipe Balbi72246da2011-08-19 18:10:58 +0300357
358 return dep->trb_pool_dma + offset;
359}
360
361static int dwc3_alloc_trb_pool(struct dwc3_ep *dep)
362{
363 struct dwc3 *dwc = dep->dwc;
364
365 if (dep->trb_pool)
366 return 0;
367
368 if (dep->number == 0 || dep->number == 1)
369 return 0;
370
371 dep->trb_pool = dma_alloc_coherent(dwc->dev,
372 sizeof(struct dwc3_trb) * DWC3_TRB_NUM,
373 &dep->trb_pool_dma, GFP_KERNEL);
374 if (!dep->trb_pool) {
375 dev_err(dep->dwc->dev, "failed to allocate trb pool for %s\n",
376 dep->name);
377 return -ENOMEM;
378 }
379
380 return 0;
381}
382
383static void dwc3_free_trb_pool(struct dwc3_ep *dep)
384{
385 struct dwc3 *dwc = dep->dwc;
386
387 dma_free_coherent(dwc->dev, sizeof(struct dwc3_trb) * DWC3_TRB_NUM,
388 dep->trb_pool, dep->trb_pool_dma);
389
390 dep->trb_pool = NULL;
391 dep->trb_pool_dma = 0;
392}
393
394static int dwc3_gadget_start_config(struct dwc3 *dwc, struct dwc3_ep *dep)
395{
396 struct dwc3_gadget_ep_cmd_params params;
397 u32 cmd;
398
399 memset(&params, 0x00, sizeof(params));
400
401 if (dep->number != 1) {
402 cmd = DWC3_DEPCMD_DEPSTARTCFG;
403 /* XferRscIdx == 0 for ep0 and 2 for the remaining */
Paul Zimmermanb23c8432011-09-30 10:58:42 +0300404 if (dep->number > 1) {
405 if (dwc->start_config_issued)
406 return 0;
407 dwc->start_config_issued = true;
Felipe Balbi72246da2011-08-19 18:10:58 +0300408 cmd |= DWC3_DEPCMD_PARAM(2);
Paul Zimmermanb23c8432011-09-30 10:58:42 +0300409 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300410
411 return dwc3_send_gadget_ep_cmd(dwc, 0, cmd, &params);
412 }
413
414 return 0;
415}
416
417static int dwc3_gadget_set_ep_config(struct dwc3 *dwc, struct dwc3_ep *dep,
Felipe Balbic90bfae2011-11-29 13:11:21 +0200418 const struct usb_endpoint_descriptor *desc,
Felipe Balbi4b345c92012-07-16 14:08:16 +0300419 const struct usb_ss_ep_comp_descriptor *comp_desc,
420 bool ignore)
Felipe Balbi72246da2011-08-19 18:10:58 +0300421{
422 struct dwc3_gadget_ep_cmd_params params;
423
424 memset(&params, 0x00, sizeof(params));
425
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300426 params.param0 = DWC3_DEPCFG_EP_TYPE(usb_endpoint_type(desc))
Chanho Parkd2e9a132012-08-31 16:54:07 +0900427 | DWC3_DEPCFG_MAX_PACKET_SIZE(usb_endpoint_maxp(desc));
428
429 /* Burst size is only needed in SuperSpeed mode */
430 if (dwc->gadget.speed == USB_SPEED_SUPER) {
431 u32 burst = dep->endpoint.maxburst - 1;
432
433 params.param0 |= DWC3_DEPCFG_BURST_SIZE(burst);
434 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300435
Felipe Balbi4b345c92012-07-16 14:08:16 +0300436 if (ignore)
437 params.param0 |= DWC3_DEPCFG_IGN_SEQ_NUM;
438
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300439 params.param1 = DWC3_DEPCFG_XFER_COMPLETE_EN
440 | DWC3_DEPCFG_XFER_NOT_READY_EN;
Felipe Balbi72246da2011-08-19 18:10:58 +0300441
Felipe Balbi18b7ede2012-01-02 13:35:41 +0200442 if (usb_ss_max_streams(comp_desc) && usb_endpoint_xfer_bulk(desc)) {
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300443 params.param1 |= DWC3_DEPCFG_STREAM_CAPABLE
444 | DWC3_DEPCFG_STREAM_EVENT_EN;
Felipe Balbi879631a2011-09-30 10:58:47 +0300445 dep->stream_capable = true;
446 }
447
Felipe Balbi72246da2011-08-19 18:10:58 +0300448 if (usb_endpoint_xfer_isoc(desc))
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300449 params.param1 |= DWC3_DEPCFG_XFER_IN_PROGRESS_EN;
Felipe Balbi72246da2011-08-19 18:10:58 +0300450
451 /*
452 * We are doing 1:1 mapping for endpoints, meaning
453 * Physical Endpoints 2 maps to Logical Endpoint 2 and
454 * so on. We consider the direction bit as part of the physical
455 * endpoint number. So USB endpoint 0x81 is 0x03.
456 */
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300457 params.param1 |= DWC3_DEPCFG_EP_NUMBER(dep->number);
Felipe Balbi72246da2011-08-19 18:10:58 +0300458
459 /*
460 * We must use the lower 16 TX FIFOs even though
461 * HW might have more
462 */
463 if (dep->direction)
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300464 params.param0 |= DWC3_DEPCFG_FIFO_NUMBER(dep->number >> 1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300465
466 if (desc->bInterval) {
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300467 params.param1 |= DWC3_DEPCFG_BINTERVAL_M1(desc->bInterval - 1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300468 dep->interval = 1 << (desc->bInterval - 1);
469 }
470
471 return dwc3_send_gadget_ep_cmd(dwc, dep->number,
472 DWC3_DEPCMD_SETEPCONFIG, &params);
473}
474
475static int dwc3_gadget_set_xfer_resource(struct dwc3 *dwc, struct dwc3_ep *dep)
476{
477 struct dwc3_gadget_ep_cmd_params params;
478
479 memset(&params, 0x00, sizeof(params));
480
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300481 params.param0 = DWC3_DEPXFERCFG_NUM_XFER_RES(1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300482
483 return dwc3_send_gadget_ep_cmd(dwc, dep->number,
484 DWC3_DEPCMD_SETTRANSFRESOURCE, &params);
485}
486
487/**
488 * __dwc3_gadget_ep_enable - Initializes a HW endpoint
489 * @dep: endpoint to be initialized
490 * @desc: USB Endpoint Descriptor
491 *
492 * Caller should take care of locking
493 */
494static int __dwc3_gadget_ep_enable(struct dwc3_ep *dep,
Felipe Balbic90bfae2011-11-29 13:11:21 +0200495 const struct usb_endpoint_descriptor *desc,
Felipe Balbi4b345c92012-07-16 14:08:16 +0300496 const struct usb_ss_ep_comp_descriptor *comp_desc,
497 bool ignore)
Felipe Balbi72246da2011-08-19 18:10:58 +0300498{
499 struct dwc3 *dwc = dep->dwc;
500 u32 reg;
501 int ret = -ENOMEM;
502
Felipe Balbiff62d6b2013-07-12 19:09:39 +0300503 dev_vdbg(dwc->dev, "Enabling %s\n", dep->name);
504
Felipe Balbi72246da2011-08-19 18:10:58 +0300505 if (!(dep->flags & DWC3_EP_ENABLED)) {
506 ret = dwc3_gadget_start_config(dwc, dep);
507 if (ret)
508 return ret;
509 }
510
Felipe Balbi4b345c92012-07-16 14:08:16 +0300511 ret = dwc3_gadget_set_ep_config(dwc, dep, desc, comp_desc, ignore);
Felipe Balbi72246da2011-08-19 18:10:58 +0300512 if (ret)
513 return ret;
514
515 if (!(dep->flags & DWC3_EP_ENABLED)) {
Felipe Balbif6bafc62012-02-06 11:04:53 +0200516 struct dwc3_trb *trb_st_hw;
517 struct dwc3_trb *trb_link;
Felipe Balbi72246da2011-08-19 18:10:58 +0300518
519 ret = dwc3_gadget_set_xfer_resource(dwc, dep);
520 if (ret)
521 return ret;
522
Ido Shayevitz16e78db2012-03-12 20:25:24 +0200523 dep->endpoint.desc = desc;
Felipe Balbic90bfae2011-11-29 13:11:21 +0200524 dep->comp_desc = comp_desc;
Felipe Balbi72246da2011-08-19 18:10:58 +0300525 dep->type = usb_endpoint_type(desc);
526 dep->flags |= DWC3_EP_ENABLED;
527
528 reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
529 reg |= DWC3_DALEPENA_EP(dep->number);
530 dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
531
532 if (!usb_endpoint_xfer_isoc(desc))
533 return 0;
534
535 memset(&trb_link, 0, sizeof(trb_link));
536
Paul Zimmerman1d046792012-02-15 18:56:56 -0800537 /* Link TRB for ISOC. The HWO bit is never reset */
Felipe Balbi72246da2011-08-19 18:10:58 +0300538 trb_st_hw = &dep->trb_pool[0];
539
Felipe Balbif6bafc62012-02-06 11:04:53 +0200540 trb_link = &dep->trb_pool[DWC3_TRB_NUM - 1];
Felipe Balbi72246da2011-08-19 18:10:58 +0300541
Felipe Balbif6bafc62012-02-06 11:04:53 +0200542 trb_link->bpl = lower_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw));
543 trb_link->bph = upper_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw));
544 trb_link->ctrl |= DWC3_TRBCTL_LINK_TRB;
545 trb_link->ctrl |= DWC3_TRB_CTRL_HWO;
Felipe Balbi72246da2011-08-19 18:10:58 +0300546 }
547
548 return 0;
549}
550
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200551static void dwc3_stop_active_transfer(struct dwc3 *dwc, u32 epnum);
552static void dwc3_remove_requests(struct dwc3 *dwc, struct dwc3_ep *dep)
Felipe Balbi72246da2011-08-19 18:10:58 +0300553{
554 struct dwc3_request *req;
555
Felipe Balbiea53b882012-02-17 12:10:04 +0200556 if (!list_empty(&dep->req_queued)) {
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200557 dwc3_stop_active_transfer(dwc, dep->number);
558
Pratyush Anand57911502012-07-06 15:19:10 +0530559 /* - giveback all requests to gadget driver */
Pratyush Anand15916332012-06-15 11:54:36 +0530560 while (!list_empty(&dep->req_queued)) {
561 req = next_request(&dep->req_queued);
562
563 dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
564 }
Felipe Balbiea53b882012-02-17 12:10:04 +0200565 }
566
Felipe Balbi72246da2011-08-19 18:10:58 +0300567 while (!list_empty(&dep->request_list)) {
568 req = next_request(&dep->request_list);
569
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200570 dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
Felipe Balbi72246da2011-08-19 18:10:58 +0300571 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300572}
573
574/**
575 * __dwc3_gadget_ep_disable - Disables a HW endpoint
576 * @dep: the endpoint to disable
577 *
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200578 * This function also removes requests which are currently processed ny the
579 * hardware and those which are not yet scheduled.
580 * Caller should take care of locking.
Felipe Balbi72246da2011-08-19 18:10:58 +0300581 */
Felipe Balbi72246da2011-08-19 18:10:58 +0300582static int __dwc3_gadget_ep_disable(struct dwc3_ep *dep)
583{
584 struct dwc3 *dwc = dep->dwc;
585 u32 reg;
586
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200587 dwc3_remove_requests(dwc, dep);
Felipe Balbi72246da2011-08-19 18:10:58 +0300588
589 reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
590 reg &= ~DWC3_DALEPENA_EP(dep->number);
591 dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
592
Felipe Balbi879631a2011-09-30 10:58:47 +0300593 dep->stream_capable = false;
Ido Shayevitzf9c56cd2012-02-08 13:56:48 +0200594 dep->endpoint.desc = NULL;
Felipe Balbic90bfae2011-11-29 13:11:21 +0200595 dep->comp_desc = NULL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300596 dep->type = 0;
Felipe Balbi879631a2011-09-30 10:58:47 +0300597 dep->flags = 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300598
599 return 0;
600}
601
602/* -------------------------------------------------------------------------- */
603
604static int dwc3_gadget_ep0_enable(struct usb_ep *ep,
605 const struct usb_endpoint_descriptor *desc)
606{
607 return -EINVAL;
608}
609
610static int dwc3_gadget_ep0_disable(struct usb_ep *ep)
611{
612 return -EINVAL;
613}
614
615/* -------------------------------------------------------------------------- */
616
617static int dwc3_gadget_ep_enable(struct usb_ep *ep,
618 const struct usb_endpoint_descriptor *desc)
619{
620 struct dwc3_ep *dep;
621 struct dwc3 *dwc;
622 unsigned long flags;
623 int ret;
624
625 if (!ep || !desc || desc->bDescriptorType != USB_DT_ENDPOINT) {
626 pr_debug("dwc3: invalid parameters\n");
627 return -EINVAL;
628 }
629
630 if (!desc->wMaxPacketSize) {
631 pr_debug("dwc3: missing wMaxPacketSize\n");
632 return -EINVAL;
633 }
634
635 dep = to_dwc3_ep(ep);
636 dwc = dep->dwc;
637
Felipe Balbic6f83f32012-08-15 12:28:29 +0300638 if (dep->flags & DWC3_EP_ENABLED) {
639 dev_WARN_ONCE(dwc->dev, true, "%s is already enabled\n",
640 dep->name);
641 return 0;
642 }
643
Felipe Balbi72246da2011-08-19 18:10:58 +0300644 switch (usb_endpoint_type(desc)) {
645 case USB_ENDPOINT_XFER_CONTROL:
Anton Tikhomirov27a78d62012-02-23 15:38:46 +0900646 strlcat(dep->name, "-control", sizeof(dep->name));
Felipe Balbi72246da2011-08-19 18:10:58 +0300647 break;
648 case USB_ENDPOINT_XFER_ISOC:
Anton Tikhomirov27a78d62012-02-23 15:38:46 +0900649 strlcat(dep->name, "-isoc", sizeof(dep->name));
Felipe Balbi72246da2011-08-19 18:10:58 +0300650 break;
651 case USB_ENDPOINT_XFER_BULK:
Anton Tikhomirov27a78d62012-02-23 15:38:46 +0900652 strlcat(dep->name, "-bulk", sizeof(dep->name));
Felipe Balbi72246da2011-08-19 18:10:58 +0300653 break;
654 case USB_ENDPOINT_XFER_INT:
Anton Tikhomirov27a78d62012-02-23 15:38:46 +0900655 strlcat(dep->name, "-int", sizeof(dep->name));
Felipe Balbi72246da2011-08-19 18:10:58 +0300656 break;
657 default:
658 dev_err(dwc->dev, "invalid endpoint transfer type\n");
659 }
660
Felipe Balbi72246da2011-08-19 18:10:58 +0300661 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi4b345c92012-07-16 14:08:16 +0300662 ret = __dwc3_gadget_ep_enable(dep, desc, ep->comp_desc, false);
Felipe Balbi72246da2011-08-19 18:10:58 +0300663 spin_unlock_irqrestore(&dwc->lock, flags);
664
665 return ret;
666}
667
668static int dwc3_gadget_ep_disable(struct usb_ep *ep)
669{
670 struct dwc3_ep *dep;
671 struct dwc3 *dwc;
672 unsigned long flags;
673 int ret;
674
675 if (!ep) {
676 pr_debug("dwc3: invalid parameters\n");
677 return -EINVAL;
678 }
679
680 dep = to_dwc3_ep(ep);
681 dwc = dep->dwc;
682
683 if (!(dep->flags & DWC3_EP_ENABLED)) {
684 dev_WARN_ONCE(dwc->dev, true, "%s is already disabled\n",
685 dep->name);
686 return 0;
687 }
688
689 snprintf(dep->name, sizeof(dep->name), "ep%d%s",
690 dep->number >> 1,
691 (dep->number & 1) ? "in" : "out");
692
693 spin_lock_irqsave(&dwc->lock, flags);
694 ret = __dwc3_gadget_ep_disable(dep);
695 spin_unlock_irqrestore(&dwc->lock, flags);
696
697 return ret;
698}
699
700static struct usb_request *dwc3_gadget_ep_alloc_request(struct usb_ep *ep,
701 gfp_t gfp_flags)
702{
703 struct dwc3_request *req;
704 struct dwc3_ep *dep = to_dwc3_ep(ep);
705 struct dwc3 *dwc = dep->dwc;
706
707 req = kzalloc(sizeof(*req), gfp_flags);
708 if (!req) {
709 dev_err(dwc->dev, "not enough memory\n");
710 return NULL;
711 }
712
713 req->epnum = dep->number;
714 req->dep = dep;
Felipe Balbi72246da2011-08-19 18:10:58 +0300715
716 return &req->request;
717}
718
719static void dwc3_gadget_ep_free_request(struct usb_ep *ep,
720 struct usb_request *request)
721{
722 struct dwc3_request *req = to_dwc3_request(request);
723
724 kfree(req);
725}
726
Felipe Balbic71fc372011-11-22 11:37:34 +0200727/**
728 * dwc3_prepare_one_trb - setup one TRB from one request
729 * @dep: endpoint for which this request is prepared
730 * @req: dwc3_request pointer
731 */
Felipe Balbi68e823e2011-11-28 12:25:01 +0200732static void dwc3_prepare_one_trb(struct dwc3_ep *dep,
Felipe Balbieeb720f2011-11-28 12:46:59 +0200733 struct dwc3_request *req, dma_addr_t dma,
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530734 unsigned length, unsigned last, unsigned chain, unsigned node)
Felipe Balbic71fc372011-11-22 11:37:34 +0200735{
Felipe Balbieeb720f2011-11-28 12:46:59 +0200736 struct dwc3 *dwc = dep->dwc;
Felipe Balbif6bafc62012-02-06 11:04:53 +0200737 struct dwc3_trb *trb;
Felipe Balbic71fc372011-11-22 11:37:34 +0200738
Felipe Balbieeb720f2011-11-28 12:46:59 +0200739 dev_vdbg(dwc->dev, "%s: req %p dma %08llx length %d%s%s\n",
740 dep->name, req, (unsigned long long) dma,
741 length, last ? " last" : "",
742 chain ? " chain" : "");
743
Felipe Balbic71fc372011-11-22 11:37:34 +0200744 /* Skip the LINK-TRB on ISOC */
Pratyush Anand915e2022013-01-14 15:59:35 +0530745 if (((dep->free_slot & DWC3_TRB_MASK) == DWC3_TRB_NUM - 1) &&
Ido Shayevitz16e78db2012-03-12 20:25:24 +0200746 usb_endpoint_xfer_isoc(dep->endpoint.desc))
Pratyush Anand915e2022013-01-14 15:59:35 +0530747 dep->free_slot++;
748
749 trb = &dep->trb_pool[dep->free_slot & DWC3_TRB_MASK];
Felipe Balbic71fc372011-11-22 11:37:34 +0200750
Felipe Balbieeb720f2011-11-28 12:46:59 +0200751 if (!req->trb) {
752 dwc3_gadget_move_request_queued(req);
Felipe Balbif6bafc62012-02-06 11:04:53 +0200753 req->trb = trb;
754 req->trb_dma = dwc3_trb_dma_offset(dep, trb);
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530755 req->start_slot = dep->free_slot & DWC3_TRB_MASK;
Felipe Balbieeb720f2011-11-28 12:46:59 +0200756 }
Felipe Balbic71fc372011-11-22 11:37:34 +0200757
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530758 dep->free_slot++;
759
Felipe Balbif6bafc62012-02-06 11:04:53 +0200760 trb->size = DWC3_TRB_SIZE_LENGTH(length);
761 trb->bpl = lower_32_bits(dma);
762 trb->bph = upper_32_bits(dma);
Felipe Balbic71fc372011-11-22 11:37:34 +0200763
Ido Shayevitz16e78db2012-03-12 20:25:24 +0200764 switch (usb_endpoint_type(dep->endpoint.desc)) {
Felipe Balbic71fc372011-11-22 11:37:34 +0200765 case USB_ENDPOINT_XFER_CONTROL:
Felipe Balbif6bafc62012-02-06 11:04:53 +0200766 trb->ctrl = DWC3_TRBCTL_CONTROL_SETUP;
Felipe Balbic71fc372011-11-22 11:37:34 +0200767 break;
768
769 case USB_ENDPOINT_XFER_ISOC:
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530770 if (!node)
771 trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS_FIRST;
772 else
773 trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS;
Felipe Balbic71fc372011-11-22 11:37:34 +0200774 break;
775
776 case USB_ENDPOINT_XFER_BULK:
777 case USB_ENDPOINT_XFER_INT:
Felipe Balbif6bafc62012-02-06 11:04:53 +0200778 trb->ctrl = DWC3_TRBCTL_NORMAL;
Felipe Balbic71fc372011-11-22 11:37:34 +0200779 break;
780 default:
781 /*
782 * This is only possible with faulty memory because we
783 * checked it already :)
784 */
785 BUG();
786 }
787
Felipe Balbif3af3652013-12-13 14:19:33 -0600788 if (!req->request.no_interrupt && !chain)
789 trb->ctrl |= DWC3_TRB_CTRL_IOC;
790
Ido Shayevitz16e78db2012-03-12 20:25:24 +0200791 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbif6bafc62012-02-06 11:04:53 +0200792 trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI;
793 trb->ctrl |= DWC3_TRB_CTRL_CSP;
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530794 } else if (last) {
795 trb->ctrl |= DWC3_TRB_CTRL_LST;
Felipe Balbif6bafc62012-02-06 11:04:53 +0200796 }
797
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530798 if (chain)
799 trb->ctrl |= DWC3_TRB_CTRL_CHN;
800
Ido Shayevitz16e78db2012-03-12 20:25:24 +0200801 if (usb_endpoint_xfer_bulk(dep->endpoint.desc) && dep->stream_capable)
Felipe Balbif6bafc62012-02-06 11:04:53 +0200802 trb->ctrl |= DWC3_TRB_CTRL_SID_SOFN(req->request.stream_id);
803
804 trb->ctrl |= DWC3_TRB_CTRL_HWO;
Felipe Balbic71fc372011-11-22 11:37:34 +0200805}
806
Felipe Balbi72246da2011-08-19 18:10:58 +0300807/*
808 * dwc3_prepare_trbs - setup TRBs from requests
809 * @dep: endpoint for which requests are being prepared
810 * @starting: true if the endpoint is idle and no requests are queued.
811 *
Paul Zimmerman1d046792012-02-15 18:56:56 -0800812 * The function goes through the requests list and sets up TRBs for the
813 * transfers. The function returns once there are no more TRBs available or
814 * it runs out of requests.
Felipe Balbi72246da2011-08-19 18:10:58 +0300815 */
Felipe Balbi68e823e2011-11-28 12:25:01 +0200816static void dwc3_prepare_trbs(struct dwc3_ep *dep, bool starting)
Felipe Balbi72246da2011-08-19 18:10:58 +0300817{
Felipe Balbi68e823e2011-11-28 12:25:01 +0200818 struct dwc3_request *req, *n;
Felipe Balbi72246da2011-08-19 18:10:58 +0300819 u32 trbs_left;
Paul Zimmerman8d62cd62012-02-15 13:35:06 +0200820 u32 max;
Felipe Balbic71fc372011-11-22 11:37:34 +0200821 unsigned int last_one = 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300822
823 BUILD_BUG_ON_NOT_POWER_OF_2(DWC3_TRB_NUM);
824
825 /* the first request must not be queued */
826 trbs_left = (dep->busy_slot - dep->free_slot) & DWC3_TRB_MASK;
Felipe Balbic71fc372011-11-22 11:37:34 +0200827
Paul Zimmerman8d62cd62012-02-15 13:35:06 +0200828 /* Can't wrap around on a non-isoc EP since there's no link TRB */
Ido Shayevitz16e78db2012-03-12 20:25:24 +0200829 if (!usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Paul Zimmerman8d62cd62012-02-15 13:35:06 +0200830 max = DWC3_TRB_NUM - (dep->free_slot & DWC3_TRB_MASK);
831 if (trbs_left > max)
832 trbs_left = max;
833 }
834
Felipe Balbi72246da2011-08-19 18:10:58 +0300835 /*
Paul Zimmerman1d046792012-02-15 18:56:56 -0800836 * If busy & slot are equal than it is either full or empty. If we are
837 * starting to process requests then we are empty. Otherwise we are
Felipe Balbi72246da2011-08-19 18:10:58 +0300838 * full and don't do anything
839 */
840 if (!trbs_left) {
841 if (!starting)
Felipe Balbi68e823e2011-11-28 12:25:01 +0200842 return;
Felipe Balbi72246da2011-08-19 18:10:58 +0300843 trbs_left = DWC3_TRB_NUM;
844 /*
845 * In case we start from scratch, we queue the ISOC requests
846 * starting from slot 1. This is done because we use ring
847 * buffer and have no LST bit to stop us. Instead, we place
Paul Zimmerman1d046792012-02-15 18:56:56 -0800848 * IOC bit every TRB_NUM/4. We try to avoid having an interrupt
Felipe Balbi72246da2011-08-19 18:10:58 +0300849 * after the first request so we start at slot 1 and have
850 * 7 requests proceed before we hit the first IOC.
851 * Other transfer types don't use the ring buffer and are
852 * processed from the first TRB until the last one. Since we
853 * don't wrap around we have to start at the beginning.
854 */
Ido Shayevitz16e78db2012-03-12 20:25:24 +0200855 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbi72246da2011-08-19 18:10:58 +0300856 dep->busy_slot = 1;
857 dep->free_slot = 1;
858 } else {
859 dep->busy_slot = 0;
860 dep->free_slot = 0;
861 }
862 }
863
864 /* The last TRB is a link TRB, not used for xfer */
Ido Shayevitz16e78db2012-03-12 20:25:24 +0200865 if ((trbs_left <= 1) && usb_endpoint_xfer_isoc(dep->endpoint.desc))
Felipe Balbi68e823e2011-11-28 12:25:01 +0200866 return;
Felipe Balbi72246da2011-08-19 18:10:58 +0300867
868 list_for_each_entry_safe(req, n, &dep->request_list, list) {
Felipe Balbieeb720f2011-11-28 12:46:59 +0200869 unsigned length;
870 dma_addr_t dma;
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530871 last_one = false;
Felipe Balbi72246da2011-08-19 18:10:58 +0300872
Felipe Balbieeb720f2011-11-28 12:46:59 +0200873 if (req->request.num_mapped_sgs > 0) {
874 struct usb_request *request = &req->request;
875 struct scatterlist *sg = request->sg;
876 struct scatterlist *s;
877 int i;
Felipe Balbi72246da2011-08-19 18:10:58 +0300878
Felipe Balbieeb720f2011-11-28 12:46:59 +0200879 for_each_sg(sg, s, request->num_mapped_sgs, i) {
880 unsigned chain = true;
Felipe Balbi72246da2011-08-19 18:10:58 +0300881
Felipe Balbieeb720f2011-11-28 12:46:59 +0200882 length = sg_dma_len(s);
883 dma = sg_dma_address(s);
Felipe Balbi72246da2011-08-19 18:10:58 +0300884
Paul Zimmerman1d046792012-02-15 18:56:56 -0800885 if (i == (request->num_mapped_sgs - 1) ||
886 sg_is_last(s)) {
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530887 if (list_is_last(&req->list,
888 &dep->request_list))
889 last_one = true;
Felipe Balbieeb720f2011-11-28 12:46:59 +0200890 chain = false;
891 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300892
Felipe Balbieeb720f2011-11-28 12:46:59 +0200893 trbs_left--;
894 if (!trbs_left)
895 last_one = true;
Felipe Balbi72246da2011-08-19 18:10:58 +0300896
Felipe Balbieeb720f2011-11-28 12:46:59 +0200897 if (last_one)
898 chain = false;
Felipe Balbi72246da2011-08-19 18:10:58 +0300899
Felipe Balbieeb720f2011-11-28 12:46:59 +0200900 dwc3_prepare_one_trb(dep, req, dma, length,
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530901 last_one, chain, i);
Felipe Balbi72246da2011-08-19 18:10:58 +0300902
Felipe Balbieeb720f2011-11-28 12:46:59 +0200903 if (last_one)
904 break;
905 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300906 } else {
Felipe Balbieeb720f2011-11-28 12:46:59 +0200907 dma = req->request.dma;
908 length = req->request.length;
909 trbs_left--;
910
911 if (!trbs_left)
912 last_one = 1;
913
914 /* Is this the last request? */
915 if (list_is_last(&req->list, &dep->request_list))
916 last_one = 1;
917
918 dwc3_prepare_one_trb(dep, req, dma, length,
Pratyush Anande5ba5ec2013-01-14 15:59:37 +0530919 last_one, false, 0);
Felipe Balbieeb720f2011-11-28 12:46:59 +0200920
921 if (last_one)
922 break;
Felipe Balbi72246da2011-08-19 18:10:58 +0300923 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300924 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300925}
926
927static int __dwc3_gadget_kick_transfer(struct dwc3_ep *dep, u16 cmd_param,
928 int start_new)
929{
930 struct dwc3_gadget_ep_cmd_params params;
931 struct dwc3_request *req;
932 struct dwc3 *dwc = dep->dwc;
933 int ret;
934 u32 cmd;
935
936 if (start_new && (dep->flags & DWC3_EP_BUSY)) {
937 dev_vdbg(dwc->dev, "%s: endpoint busy\n", dep->name);
938 return -EBUSY;
939 }
940 dep->flags &= ~DWC3_EP_PENDING_REQUEST;
941
942 /*
943 * If we are getting here after a short-out-packet we don't enqueue any
944 * new requests as we try to set the IOC bit only on the last request.
945 */
946 if (start_new) {
947 if (list_empty(&dep->req_queued))
948 dwc3_prepare_trbs(dep, start_new);
949
950 /* req points to the first request which will be sent */
951 req = next_request(&dep->req_queued);
952 } else {
Felipe Balbi68e823e2011-11-28 12:25:01 +0200953 dwc3_prepare_trbs(dep, start_new);
954
Felipe Balbi72246da2011-08-19 18:10:58 +0300955 /*
Paul Zimmerman1d046792012-02-15 18:56:56 -0800956 * req points to the first request where HWO changed from 0 to 1
Felipe Balbi72246da2011-08-19 18:10:58 +0300957 */
Felipe Balbi68e823e2011-11-28 12:25:01 +0200958 req = next_request(&dep->req_queued);
Felipe Balbi72246da2011-08-19 18:10:58 +0300959 }
960 if (!req) {
961 dep->flags |= DWC3_EP_PENDING_REQUEST;
962 return 0;
963 }
964
965 memset(&params, 0, sizeof(params));
Felipe Balbi72246da2011-08-19 18:10:58 +0300966
Pratyush Anand1877d6c2013-01-14 15:59:36 +0530967 if (start_new) {
968 params.param0 = upper_32_bits(req->trb_dma);
969 params.param1 = lower_32_bits(req->trb_dma);
Felipe Balbi72246da2011-08-19 18:10:58 +0300970 cmd = DWC3_DEPCMD_STARTTRANSFER;
Pratyush Anand1877d6c2013-01-14 15:59:36 +0530971 } else {
Felipe Balbi72246da2011-08-19 18:10:58 +0300972 cmd = DWC3_DEPCMD_UPDATETRANSFER;
Pratyush Anand1877d6c2013-01-14 15:59:36 +0530973 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300974
975 cmd |= DWC3_DEPCMD_PARAM(cmd_param);
976 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number, cmd, &params);
977 if (ret < 0) {
978 dev_dbg(dwc->dev, "failed to send STARTTRANSFER command\n");
979
980 /*
981 * FIXME we need to iterate over the list of requests
982 * here and stop, unmap, free and del each of the linked
Paul Zimmerman1d046792012-02-15 18:56:56 -0800983 * requests instead of what we do now.
Felipe Balbi72246da2011-08-19 18:10:58 +0300984 */
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +0200985 usb_gadget_unmap_request(&dwc->gadget, &req->request,
986 req->direction);
Felipe Balbi72246da2011-08-19 18:10:58 +0300987 list_del(&req->list);
988 return ret;
989 }
990
991 dep->flags |= DWC3_EP_BUSY;
Felipe Balbi25b8ff62011-11-04 12:32:47 +0200992
Paul Zimmermanf898ae02012-03-29 18:16:54 +0000993 if (start_new) {
Felipe Balbib4996a82012-06-06 12:04:13 +0300994 dep->resource_index = dwc3_gadget_ep_get_transfer_index(dwc,
Paul Zimmermanf898ae02012-03-29 18:16:54 +0000995 dep->number);
Felipe Balbib4996a82012-06-06 12:04:13 +0300996 WARN_ON_ONCE(!dep->resource_index);
Paul Zimmermanf898ae02012-03-29 18:16:54 +0000997 }
Felipe Balbi25b8ff62011-11-04 12:32:47 +0200998
Felipe Balbi72246da2011-08-19 18:10:58 +0300999 return 0;
1000}
1001
Pratyush Anandd6d6ec72012-05-25 18:54:56 +05301002static void __dwc3_gadget_start_isoc(struct dwc3 *dwc,
1003 struct dwc3_ep *dep, u32 cur_uf)
1004{
1005 u32 uf;
1006
1007 if (list_empty(&dep->request_list)) {
1008 dev_vdbg(dwc->dev, "ISOC ep %s run out for requests.\n",
1009 dep->name);
Pratyush Anandf4a53c52012-08-30 12:21:43 +05301010 dep->flags |= DWC3_EP_PENDING_REQUEST;
Pratyush Anandd6d6ec72012-05-25 18:54:56 +05301011 return;
1012 }
1013
1014 /* 4 micro frames in the future */
1015 uf = cur_uf + dep->interval * 4;
1016
1017 __dwc3_gadget_kick_transfer(dep, uf, 1);
1018}
1019
1020static void dwc3_gadget_start_isoc(struct dwc3 *dwc,
1021 struct dwc3_ep *dep, const struct dwc3_event_depevt *event)
1022{
1023 u32 cur_uf, mask;
1024
1025 mask = ~(dep->interval - 1);
1026 cur_uf = event->parameters & mask;
1027
1028 __dwc3_gadget_start_isoc(dwc, dep, cur_uf);
1029}
1030
Felipe Balbi72246da2011-08-19 18:10:58 +03001031static int __dwc3_gadget_ep_queue(struct dwc3_ep *dep, struct dwc3_request *req)
1032{
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02001033 struct dwc3 *dwc = dep->dwc;
1034 int ret;
1035
Felipe Balbi72246da2011-08-19 18:10:58 +03001036 req->request.actual = 0;
1037 req->request.status = -EINPROGRESS;
1038 req->direction = dep->direction;
1039 req->epnum = dep->number;
1040
1041 /*
1042 * We only add to our list of requests now and
1043 * start consuming the list once we get XferNotReady
1044 * IRQ.
1045 *
1046 * That way, we avoid doing anything that we don't need
1047 * to do now and defer it until the point we receive a
1048 * particular token from the Host side.
1049 *
1050 * This will also avoid Host cancelling URBs due to too
Paul Zimmerman1d046792012-02-15 18:56:56 -08001051 * many NAKs.
Felipe Balbi72246da2011-08-19 18:10:58 +03001052 */
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02001053 ret = usb_gadget_map_request(&dwc->gadget, &req->request,
1054 dep->direction);
1055 if (ret)
1056 return ret;
1057
Felipe Balbi72246da2011-08-19 18:10:58 +03001058 list_add_tail(&req->list, &dep->request_list);
1059
1060 /*
Felipe Balbib511e5e2012-06-06 12:00:50 +03001061 * There are a few special cases:
Felipe Balbi72246da2011-08-19 18:10:58 +03001062 *
Paul Zimmermanf898ae02012-03-29 18:16:54 +00001063 * 1. XferNotReady with empty list of requests. We need to kick the
1064 * transfer here in that situation, otherwise we will be NAKing
1065 * forever. If we get XferNotReady before gadget driver has a
1066 * chance to queue a request, we will ACK the IRQ but won't be
1067 * able to receive the data until the next request is queued.
1068 * The following code is handling exactly that.
1069 *
Felipe Balbi72246da2011-08-19 18:10:58 +03001070 */
1071 if (dep->flags & DWC3_EP_PENDING_REQUEST) {
Pratyush Anandf4a53c52012-08-30 12:21:43 +05301072 /*
1073 * If xfernotready is already elapsed and it is a case
1074 * of isoc transfer, then issue END TRANSFER, so that
1075 * you can receive xfernotready again and can have
1076 * notion of current microframe.
1077 */
1078 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Pratyush Anandcdc359d2013-01-14 15:59:34 +05301079 if (list_empty(&dep->req_queued)) {
1080 dwc3_stop_active_transfer(dwc, dep->number);
1081 dep->flags = DWC3_EP_ENABLED;
1082 }
Pratyush Anandf4a53c52012-08-30 12:21:43 +05301083 return 0;
1084 }
1085
Felipe Balbib511e5e2012-06-06 12:00:50 +03001086 ret = __dwc3_gadget_kick_transfer(dep, 0, true);
Moiz Sonasath348e0262012-08-01 14:08:30 -05001087 if (ret && ret != -EBUSY)
Felipe Balbi72246da2011-08-19 18:10:58 +03001088 dev_dbg(dwc->dev, "%s: failed to kick transfers\n",
1089 dep->name);
Pratyush Anand15f86bd2013-01-14 15:59:33 +05301090 return ret;
Felipe Balbia0925322012-05-22 10:24:11 +03001091 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001092
Felipe Balbib511e5e2012-06-06 12:00:50 +03001093 /*
1094 * 2. XferInProgress on Isoc EP with an active transfer. We need to
1095 * kick the transfer here after queuing a request, otherwise the
1096 * core may not see the modified TRB(s).
1097 */
1098 if (usb_endpoint_xfer_isoc(dep->endpoint.desc) &&
Pratyush Anand79c90462012-08-07 16:54:18 +05301099 (dep->flags & DWC3_EP_BUSY) &&
1100 !(dep->flags & DWC3_EP_MISSED_ISOC)) {
Felipe Balbib4996a82012-06-06 12:04:13 +03001101 WARN_ON_ONCE(!dep->resource_index);
1102 ret = __dwc3_gadget_kick_transfer(dep, dep->resource_index,
Felipe Balbib511e5e2012-06-06 12:00:50 +03001103 false);
Moiz Sonasath348e0262012-08-01 14:08:30 -05001104 if (ret && ret != -EBUSY)
Felipe Balbib511e5e2012-06-06 12:00:50 +03001105 dev_dbg(dwc->dev, "%s: failed to kick transfers\n",
1106 dep->name);
Pratyush Anand15f86bd2013-01-14 15:59:33 +05301107 return ret;
Felipe Balbib511e5e2012-06-06 12:00:50 +03001108 }
1109
Felipe Balbi72246da2011-08-19 18:10:58 +03001110 return 0;
1111}
1112
1113static int dwc3_gadget_ep_queue(struct usb_ep *ep, struct usb_request *request,
1114 gfp_t gfp_flags)
1115{
1116 struct dwc3_request *req = to_dwc3_request(request);
1117 struct dwc3_ep *dep = to_dwc3_ep(ep);
1118 struct dwc3 *dwc = dep->dwc;
1119
1120 unsigned long flags;
1121
1122 int ret;
1123
Ido Shayevitz16e78db2012-03-12 20:25:24 +02001124 if (!dep->endpoint.desc) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001125 dev_dbg(dwc->dev, "trying to queue request %p to disabled %s\n",
1126 request, ep->name);
1127 return -ESHUTDOWN;
1128 }
1129
1130 dev_vdbg(dwc->dev, "queing request %p to %s length %d\n",
1131 request, ep->name, request->length);
1132
1133 spin_lock_irqsave(&dwc->lock, flags);
1134 ret = __dwc3_gadget_ep_queue(dep, req);
1135 spin_unlock_irqrestore(&dwc->lock, flags);
1136
1137 return ret;
1138}
1139
1140static int dwc3_gadget_ep_dequeue(struct usb_ep *ep,
1141 struct usb_request *request)
1142{
1143 struct dwc3_request *req = to_dwc3_request(request);
1144 struct dwc3_request *r = NULL;
1145
1146 struct dwc3_ep *dep = to_dwc3_ep(ep);
1147 struct dwc3 *dwc = dep->dwc;
1148
1149 unsigned long flags;
1150 int ret = 0;
1151
1152 spin_lock_irqsave(&dwc->lock, flags);
1153
1154 list_for_each_entry(r, &dep->request_list, list) {
1155 if (r == req)
1156 break;
1157 }
1158
1159 if (r != req) {
1160 list_for_each_entry(r, &dep->req_queued, list) {
1161 if (r == req)
1162 break;
1163 }
1164 if (r == req) {
1165 /* wait until it is processed */
1166 dwc3_stop_active_transfer(dwc, dep->number);
Pratyush Anande8d4e8b2012-06-15 11:54:00 +05301167 goto out1;
Felipe Balbi72246da2011-08-19 18:10:58 +03001168 }
1169 dev_err(dwc->dev, "request %p was not queued to %s\n",
1170 request, ep->name);
1171 ret = -EINVAL;
1172 goto out0;
1173 }
1174
Pratyush Anande8d4e8b2012-06-15 11:54:00 +05301175out1:
Felipe Balbi72246da2011-08-19 18:10:58 +03001176 /* giveback the request */
1177 dwc3_gadget_giveback(dep, req, -ECONNRESET);
1178
1179out0:
1180 spin_unlock_irqrestore(&dwc->lock, flags);
1181
1182 return ret;
1183}
1184
1185int __dwc3_gadget_ep_set_halt(struct dwc3_ep *dep, int value)
1186{
1187 struct dwc3_gadget_ep_cmd_params params;
1188 struct dwc3 *dwc = dep->dwc;
1189 int ret;
1190
1191 memset(&params, 0x00, sizeof(params));
1192
1193 if (value) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001194 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number,
1195 DWC3_DEPCMD_SETSTALL, &params);
1196 if (ret)
1197 dev_err(dwc->dev, "failed to %s STALL on %s\n",
1198 value ? "set" : "clear",
1199 dep->name);
1200 else
1201 dep->flags |= DWC3_EP_STALL;
1202 } else {
1203 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number,
1204 DWC3_DEPCMD_CLEARSTALL, &params);
1205 if (ret)
1206 dev_err(dwc->dev, "failed to %s STALL on %s\n",
1207 value ? "set" : "clear",
1208 dep->name);
1209 else
Alan Sterna535d812013-11-01 12:05:12 -04001210 dep->flags &= ~(DWC3_EP_STALL | DWC3_EP_WEDGE);
Felipe Balbi72246da2011-08-19 18:10:58 +03001211 }
Paul Zimmerman52754552011-09-30 10:58:44 +03001212
Felipe Balbi72246da2011-08-19 18:10:58 +03001213 return ret;
1214}
1215
1216static int dwc3_gadget_ep_set_halt(struct usb_ep *ep, int value)
1217{
1218 struct dwc3_ep *dep = to_dwc3_ep(ep);
1219 struct dwc3 *dwc = dep->dwc;
1220
1221 unsigned long flags;
1222
1223 int ret;
1224
1225 spin_lock_irqsave(&dwc->lock, flags);
1226
Ido Shayevitz16e78db2012-03-12 20:25:24 +02001227 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001228 dev_err(dwc->dev, "%s is of Isochronous type\n", dep->name);
1229 ret = -EINVAL;
1230 goto out;
1231 }
1232
1233 ret = __dwc3_gadget_ep_set_halt(dep, value);
1234out:
1235 spin_unlock_irqrestore(&dwc->lock, flags);
1236
1237 return ret;
1238}
1239
1240static int dwc3_gadget_ep_set_wedge(struct usb_ep *ep)
1241{
1242 struct dwc3_ep *dep = to_dwc3_ep(ep);
Paul Zimmerman249a4562012-02-24 17:32:16 -08001243 struct dwc3 *dwc = dep->dwc;
1244 unsigned long flags;
Felipe Balbi72246da2011-08-19 18:10:58 +03001245
Paul Zimmerman249a4562012-02-24 17:32:16 -08001246 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001247 dep->flags |= DWC3_EP_WEDGE;
Paul Zimmerman249a4562012-02-24 17:32:16 -08001248 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001249
Pratyush Anand08f0d962012-06-25 22:40:43 +05301250 if (dep->number == 0 || dep->number == 1)
1251 return dwc3_gadget_ep0_set_halt(ep, 1);
1252 else
1253 return dwc3_gadget_ep_set_halt(ep, 1);
Felipe Balbi72246da2011-08-19 18:10:58 +03001254}
1255
1256/* -------------------------------------------------------------------------- */
1257
1258static struct usb_endpoint_descriptor dwc3_gadget_ep0_desc = {
1259 .bLength = USB_DT_ENDPOINT_SIZE,
1260 .bDescriptorType = USB_DT_ENDPOINT,
1261 .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
1262};
1263
1264static const struct usb_ep_ops dwc3_gadget_ep0_ops = {
1265 .enable = dwc3_gadget_ep0_enable,
1266 .disable = dwc3_gadget_ep0_disable,
1267 .alloc_request = dwc3_gadget_ep_alloc_request,
1268 .free_request = dwc3_gadget_ep_free_request,
1269 .queue = dwc3_gadget_ep0_queue,
1270 .dequeue = dwc3_gadget_ep_dequeue,
Pratyush Anand08f0d962012-06-25 22:40:43 +05301271 .set_halt = dwc3_gadget_ep0_set_halt,
Felipe Balbi72246da2011-08-19 18:10:58 +03001272 .set_wedge = dwc3_gadget_ep_set_wedge,
1273};
1274
1275static const struct usb_ep_ops dwc3_gadget_ep_ops = {
1276 .enable = dwc3_gadget_ep_enable,
1277 .disable = dwc3_gadget_ep_disable,
1278 .alloc_request = dwc3_gadget_ep_alloc_request,
1279 .free_request = dwc3_gadget_ep_free_request,
1280 .queue = dwc3_gadget_ep_queue,
1281 .dequeue = dwc3_gadget_ep_dequeue,
1282 .set_halt = dwc3_gadget_ep_set_halt,
1283 .set_wedge = dwc3_gadget_ep_set_wedge,
1284};
1285
1286/* -------------------------------------------------------------------------- */
1287
1288static int dwc3_gadget_get_frame(struct usb_gadget *g)
1289{
1290 struct dwc3 *dwc = gadget_to_dwc(g);
1291 u32 reg;
1292
1293 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1294 return DWC3_DSTS_SOFFN(reg);
1295}
1296
1297static int dwc3_gadget_wakeup(struct usb_gadget *g)
1298{
1299 struct dwc3 *dwc = gadget_to_dwc(g);
1300
1301 unsigned long timeout;
1302 unsigned long flags;
1303
1304 u32 reg;
1305
1306 int ret = 0;
1307
1308 u8 link_state;
1309 u8 speed;
1310
1311 spin_lock_irqsave(&dwc->lock, flags);
1312
1313 /*
1314 * According to the Databook Remote wakeup request should
1315 * be issued only when the device is in early suspend state.
1316 *
1317 * We can check that via USB Link State bits in DSTS register.
1318 */
1319 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1320
1321 speed = reg & DWC3_DSTS_CONNECTSPD;
1322 if (speed == DWC3_DSTS_SUPERSPEED) {
1323 dev_dbg(dwc->dev, "no wakeup on SuperSpeed\n");
1324 ret = -EINVAL;
1325 goto out;
1326 }
1327
1328 link_state = DWC3_DSTS_USBLNKST(reg);
1329
1330 switch (link_state) {
1331 case DWC3_LINK_STATE_RX_DET: /* in HS, means Early Suspend */
1332 case DWC3_LINK_STATE_U3: /* in HS, means SUSPEND */
1333 break;
1334 default:
1335 dev_dbg(dwc->dev, "can't wakeup from link state %d\n",
1336 link_state);
1337 ret = -EINVAL;
1338 goto out;
1339 }
1340
Felipe Balbi8598bde2012-01-02 18:55:57 +02001341 ret = dwc3_gadget_set_link_state(dwc, DWC3_LINK_STATE_RECOV);
1342 if (ret < 0) {
1343 dev_err(dwc->dev, "failed to put link in Recovery\n");
1344 goto out;
1345 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001346
Paul Zimmerman802fde92012-04-27 13:10:52 +03001347 /* Recent versions do this automatically */
1348 if (dwc->revision < DWC3_REVISION_194A) {
1349 /* write zeroes to Link Change Request */
Felipe Balbifcc023c2012-05-24 10:27:56 +03001350 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
Paul Zimmerman802fde92012-04-27 13:10:52 +03001351 reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK;
1352 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1353 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001354
Paul Zimmerman1d046792012-02-15 18:56:56 -08001355 /* poll until Link State changes to ON */
Felipe Balbi72246da2011-08-19 18:10:58 +03001356 timeout = jiffies + msecs_to_jiffies(100);
1357
Paul Zimmerman1d046792012-02-15 18:56:56 -08001358 while (!time_after(jiffies, timeout)) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001359 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1360
1361 /* in HS, means ON */
1362 if (DWC3_DSTS_USBLNKST(reg) == DWC3_LINK_STATE_U0)
1363 break;
1364 }
1365
1366 if (DWC3_DSTS_USBLNKST(reg) != DWC3_LINK_STATE_U0) {
1367 dev_err(dwc->dev, "failed to send remote wakeup\n");
1368 ret = -EINVAL;
1369 }
1370
1371out:
1372 spin_unlock_irqrestore(&dwc->lock, flags);
1373
1374 return ret;
1375}
1376
1377static int dwc3_gadget_set_selfpowered(struct usb_gadget *g,
1378 int is_selfpowered)
1379{
1380 struct dwc3 *dwc = gadget_to_dwc(g);
Paul Zimmerman249a4562012-02-24 17:32:16 -08001381 unsigned long flags;
Felipe Balbi72246da2011-08-19 18:10:58 +03001382
Paul Zimmerman249a4562012-02-24 17:32:16 -08001383 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001384 dwc->is_selfpowered = !!is_selfpowered;
Paul Zimmerman249a4562012-02-24 17:32:16 -08001385 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001386
1387 return 0;
1388}
1389
Pratyush Anand6f17f742012-07-02 10:21:55 +05301390static int dwc3_gadget_run_stop(struct dwc3 *dwc, int is_on)
Felipe Balbi72246da2011-08-19 18:10:58 +03001391{
1392 u32 reg;
Sebastian Andrzej Siewior61d58242011-08-29 16:46:38 +02001393 u32 timeout = 500;
Felipe Balbi72246da2011-08-19 18:10:58 +03001394
1395 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
Felipe Balbi8db7ed12012-01-18 18:32:29 +02001396 if (is_on) {
Paul Zimmerman802fde92012-04-27 13:10:52 +03001397 if (dwc->revision <= DWC3_REVISION_187A) {
1398 reg &= ~DWC3_DCTL_TRGTULST_MASK;
1399 reg |= DWC3_DCTL_TRGTULST_RX_DET;
1400 }
1401
1402 if (dwc->revision >= DWC3_REVISION_194A)
1403 reg &= ~DWC3_DCTL_KEEP_CONNECT;
1404 reg |= DWC3_DCTL_RUN_STOP;
Felipe Balbi9fcb3bd2013-02-08 17:55:58 +02001405 dwc->pullups_connected = true;
Felipe Balbi8db7ed12012-01-18 18:32:29 +02001406 } else {
Felipe Balbi72246da2011-08-19 18:10:58 +03001407 reg &= ~DWC3_DCTL_RUN_STOP;
Felipe Balbi9fcb3bd2013-02-08 17:55:58 +02001408 dwc->pullups_connected = false;
Felipe Balbi8db7ed12012-01-18 18:32:29 +02001409 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001410
1411 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1412
1413 do {
1414 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1415 if (is_on) {
1416 if (!(reg & DWC3_DSTS_DEVCTRLHLT))
1417 break;
1418 } else {
1419 if (reg & DWC3_DSTS_DEVCTRLHLT)
1420 break;
1421 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001422 timeout--;
1423 if (!timeout)
Pratyush Anand6f17f742012-07-02 10:21:55 +05301424 return -ETIMEDOUT;
Sebastian Andrzej Siewior61d58242011-08-29 16:46:38 +02001425 udelay(1);
Felipe Balbi72246da2011-08-19 18:10:58 +03001426 } while (1);
1427
1428 dev_vdbg(dwc->dev, "gadget %s data soft-%s\n",
1429 dwc->gadget_driver
1430 ? dwc->gadget_driver->function : "no-function",
1431 is_on ? "connect" : "disconnect");
Pratyush Anand6f17f742012-07-02 10:21:55 +05301432
1433 return 0;
Felipe Balbi72246da2011-08-19 18:10:58 +03001434}
1435
1436static int dwc3_gadget_pullup(struct usb_gadget *g, int is_on)
1437{
1438 struct dwc3 *dwc = gadget_to_dwc(g);
1439 unsigned long flags;
Pratyush Anand6f17f742012-07-02 10:21:55 +05301440 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001441
1442 is_on = !!is_on;
1443
1444 spin_lock_irqsave(&dwc->lock, flags);
Pratyush Anand6f17f742012-07-02 10:21:55 +05301445 ret = dwc3_gadget_run_stop(dwc, is_on);
Felipe Balbi72246da2011-08-19 18:10:58 +03001446 spin_unlock_irqrestore(&dwc->lock, flags);
1447
Pratyush Anand6f17f742012-07-02 10:21:55 +05301448 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001449}
1450
Felipe Balbi8698e2a2013-02-08 15:24:04 +02001451static void dwc3_gadget_enable_irq(struct dwc3 *dwc)
1452{
1453 u32 reg;
1454
1455 /* Enable all but Start and End of Frame IRQs */
1456 reg = (DWC3_DEVTEN_VNDRDEVTSTRCVEDEN |
1457 DWC3_DEVTEN_EVNTOVERFLOWEN |
1458 DWC3_DEVTEN_CMDCMPLTEN |
1459 DWC3_DEVTEN_ERRTICERREN |
1460 DWC3_DEVTEN_WKUPEVTEN |
1461 DWC3_DEVTEN_ULSTCNGEN |
1462 DWC3_DEVTEN_CONNECTDONEEN |
1463 DWC3_DEVTEN_USBRSTEN |
1464 DWC3_DEVTEN_DISCONNEVTEN);
1465
1466 dwc3_writel(dwc->regs, DWC3_DEVTEN, reg);
1467}
1468
1469static void dwc3_gadget_disable_irq(struct dwc3 *dwc)
1470{
1471 /* mask all interrupts */
1472 dwc3_writel(dwc->regs, DWC3_DEVTEN, 0x00);
1473}
1474
1475static irqreturn_t dwc3_interrupt(int irq, void *_dwc);
Felipe Balbib15a7622011-06-30 16:57:15 +03001476static irqreturn_t dwc3_thread_interrupt(int irq, void *_dwc);
Felipe Balbi8698e2a2013-02-08 15:24:04 +02001477
Felipe Balbi72246da2011-08-19 18:10:58 +03001478static int dwc3_gadget_start(struct usb_gadget *g,
1479 struct usb_gadget_driver *driver)
1480{
1481 struct dwc3 *dwc = gadget_to_dwc(g);
1482 struct dwc3_ep *dep;
1483 unsigned long flags;
1484 int ret = 0;
Felipe Balbi8698e2a2013-02-08 15:24:04 +02001485 int irq;
Felipe Balbi72246da2011-08-19 18:10:58 +03001486 u32 reg;
1487
Felipe Balbib0d7ffd2013-06-27 10:00:18 +03001488 irq = platform_get_irq(to_platform_device(dwc->dev), 0);
1489 ret = request_threaded_irq(irq, dwc3_interrupt, dwc3_thread_interrupt,
Felipe Balbie8adfc32013-06-12 21:11:14 +03001490 IRQF_SHARED, "dwc3", dwc);
Felipe Balbib0d7ffd2013-06-27 10:00:18 +03001491 if (ret) {
1492 dev_err(dwc->dev, "failed to request irq #%d --> %d\n",
1493 irq, ret);
1494 goto err0;
1495 }
1496
Felipe Balbi72246da2011-08-19 18:10:58 +03001497 spin_lock_irqsave(&dwc->lock, flags);
1498
1499 if (dwc->gadget_driver) {
1500 dev_err(dwc->dev, "%s is already bound to %s\n",
1501 dwc->gadget.name,
1502 dwc->gadget_driver->driver.name);
1503 ret = -EBUSY;
Felipe Balbib0d7ffd2013-06-27 10:00:18 +03001504 goto err1;
Felipe Balbi72246da2011-08-19 18:10:58 +03001505 }
1506
1507 dwc->gadget_driver = driver;
Felipe Balbi72246da2011-08-19 18:10:58 +03001508
Felipe Balbi72246da2011-08-19 18:10:58 +03001509 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
1510 reg &= ~(DWC3_DCFG_SPEED_MASK);
Felipe Balbi07e7f472012-03-23 12:20:31 +02001511
1512 /**
1513 * WORKAROUND: DWC3 revision < 2.20a have an issue
1514 * which would cause metastability state on Run/Stop
1515 * bit if we try to force the IP to USB2-only mode.
1516 *
1517 * Because of that, we cannot configure the IP to any
1518 * speed other than the SuperSpeed
1519 *
1520 * Refers to:
1521 *
1522 * STAR#9000525659: Clock Domain Crossing on DCTL in
1523 * USB 2.0 Mode
1524 */
Felipe Balbif7e846f2013-06-30 14:29:51 +03001525 if (dwc->revision < DWC3_REVISION_220A) {
Felipe Balbi07e7f472012-03-23 12:20:31 +02001526 reg |= DWC3_DCFG_SUPERSPEED;
Felipe Balbif7e846f2013-06-30 14:29:51 +03001527 } else {
1528 switch (dwc->maximum_speed) {
1529 case USB_SPEED_LOW:
1530 reg |= DWC3_DSTS_LOWSPEED;
1531 break;
1532 case USB_SPEED_FULL:
1533 reg |= DWC3_DSTS_FULLSPEED1;
1534 break;
1535 case USB_SPEED_HIGH:
1536 reg |= DWC3_DSTS_HIGHSPEED;
1537 break;
1538 case USB_SPEED_SUPER: /* FALLTHROUGH */
1539 case USB_SPEED_UNKNOWN: /* FALTHROUGH */
1540 default:
1541 reg |= DWC3_DSTS_SUPERSPEED;
1542 }
1543 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001544 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
1545
Paul Zimmermanb23c8432011-09-30 10:58:42 +03001546 dwc->start_config_issued = false;
1547
Felipe Balbi72246da2011-08-19 18:10:58 +03001548 /* Start with SuperSpeed Default */
1549 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
1550
1551 dep = dwc->eps[0];
Felipe Balbi4b345c92012-07-16 14:08:16 +03001552 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, false);
Felipe Balbi72246da2011-08-19 18:10:58 +03001553 if (ret) {
1554 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
Felipe Balbib0d7ffd2013-06-27 10:00:18 +03001555 goto err2;
Felipe Balbi72246da2011-08-19 18:10:58 +03001556 }
1557
1558 dep = dwc->eps[1];
Felipe Balbi4b345c92012-07-16 14:08:16 +03001559 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, false);
Felipe Balbi72246da2011-08-19 18:10:58 +03001560 if (ret) {
1561 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
Felipe Balbib0d7ffd2013-06-27 10:00:18 +03001562 goto err3;
Felipe Balbi72246da2011-08-19 18:10:58 +03001563 }
1564
1565 /* begin to receive SETUP packets */
Felipe Balbic7fcdeb2011-08-27 22:28:36 +03001566 dwc->ep0state = EP0_SETUP_PHASE;
Felipe Balbi72246da2011-08-19 18:10:58 +03001567 dwc3_ep0_out_start(dwc);
1568
Felipe Balbi8698e2a2013-02-08 15:24:04 +02001569 dwc3_gadget_enable_irq(dwc);
1570
Felipe Balbi72246da2011-08-19 18:10:58 +03001571 spin_unlock_irqrestore(&dwc->lock, flags);
1572
1573 return 0;
1574
Felipe Balbib0d7ffd2013-06-27 10:00:18 +03001575err3:
Felipe Balbi72246da2011-08-19 18:10:58 +03001576 __dwc3_gadget_ep_disable(dwc->eps[0]);
1577
Felipe Balbib0d7ffd2013-06-27 10:00:18 +03001578err2:
Felipe Balbicdcedd62013-07-15 12:36:35 +03001579 dwc->gadget_driver = NULL;
Felipe Balbib0d7ffd2013-06-27 10:00:18 +03001580
1581err1:
Felipe Balbi72246da2011-08-19 18:10:58 +03001582 spin_unlock_irqrestore(&dwc->lock, flags);
1583
Felipe Balbib0d7ffd2013-06-27 10:00:18 +03001584 free_irq(irq, dwc);
1585
1586err0:
Felipe Balbi72246da2011-08-19 18:10:58 +03001587 return ret;
1588}
1589
1590static int dwc3_gadget_stop(struct usb_gadget *g,
1591 struct usb_gadget_driver *driver)
1592{
1593 struct dwc3 *dwc = gadget_to_dwc(g);
1594 unsigned long flags;
Felipe Balbi8698e2a2013-02-08 15:24:04 +02001595 int irq;
Felipe Balbi72246da2011-08-19 18:10:58 +03001596
1597 spin_lock_irqsave(&dwc->lock, flags);
1598
Felipe Balbi8698e2a2013-02-08 15:24:04 +02001599 dwc3_gadget_disable_irq(dwc);
Felipe Balbi72246da2011-08-19 18:10:58 +03001600 __dwc3_gadget_ep_disable(dwc->eps[0]);
1601 __dwc3_gadget_ep_disable(dwc->eps[1]);
1602
1603 dwc->gadget_driver = NULL;
Felipe Balbi72246da2011-08-19 18:10:58 +03001604
1605 spin_unlock_irqrestore(&dwc->lock, flags);
1606
Felipe Balbib0d7ffd2013-06-27 10:00:18 +03001607 irq = platform_get_irq(to_platform_device(dwc->dev), 0);
1608 free_irq(irq, dwc);
1609
Felipe Balbi72246da2011-08-19 18:10:58 +03001610 return 0;
1611}
Paul Zimmerman802fde92012-04-27 13:10:52 +03001612
Felipe Balbi72246da2011-08-19 18:10:58 +03001613static const struct usb_gadget_ops dwc3_gadget_ops = {
1614 .get_frame = dwc3_gadget_get_frame,
1615 .wakeup = dwc3_gadget_wakeup,
1616 .set_selfpowered = dwc3_gadget_set_selfpowered,
1617 .pullup = dwc3_gadget_pullup,
1618 .udc_start = dwc3_gadget_start,
1619 .udc_stop = dwc3_gadget_stop,
1620};
1621
1622/* -------------------------------------------------------------------------- */
1623
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03001624static int dwc3_gadget_init_hw_endpoints(struct dwc3 *dwc,
1625 u8 num, u32 direction)
Felipe Balbi72246da2011-08-19 18:10:58 +03001626{
1627 struct dwc3_ep *dep;
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03001628 u8 i;
Felipe Balbi72246da2011-08-19 18:10:58 +03001629
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03001630 for (i = 0; i < num; i++) {
1631 u8 epnum = (i << 1) | (!!direction);
Felipe Balbi72246da2011-08-19 18:10:58 +03001632
Felipe Balbi72246da2011-08-19 18:10:58 +03001633 dep = kzalloc(sizeof(*dep), GFP_KERNEL);
1634 if (!dep) {
1635 dev_err(dwc->dev, "can't allocate endpoint %d\n",
1636 epnum);
1637 return -ENOMEM;
1638 }
1639
1640 dep->dwc = dwc;
1641 dep->number = epnum;
Felipe Balbi9aa62ae2013-07-12 19:10:59 +03001642 dep->direction = !!direction;
Felipe Balbi72246da2011-08-19 18:10:58 +03001643 dwc->eps[epnum] = dep;
1644
1645 snprintf(dep->name, sizeof(dep->name), "ep%d%s", epnum >> 1,
1646 (epnum & 1) ? "in" : "out");
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03001647
Felipe Balbi72246da2011-08-19 18:10:58 +03001648 dep->endpoint.name = dep->name;
Felipe Balbi72246da2011-08-19 18:10:58 +03001649
Felipe Balbi653df352013-07-12 19:11:57 +03001650 dev_vdbg(dwc->dev, "initializing %s\n", dep->name);
1651
Felipe Balbi72246da2011-08-19 18:10:58 +03001652 if (epnum == 0 || epnum == 1) {
Robert Baldygae117e742013-12-13 12:23:38 +01001653 usb_ep_set_maxpacket_limit(&dep->endpoint, 512);
Pratyush Anand6048e4c2013-01-18 16:53:56 +05301654 dep->endpoint.maxburst = 1;
Felipe Balbi72246da2011-08-19 18:10:58 +03001655 dep->endpoint.ops = &dwc3_gadget_ep0_ops;
1656 if (!epnum)
1657 dwc->gadget.ep0 = &dep->endpoint;
1658 } else {
1659 int ret;
1660
Robert Baldygae117e742013-12-13 12:23:38 +01001661 usb_ep_set_maxpacket_limit(&dep->endpoint, 1024);
Sebastian Andrzej Siewior12d36c12011-11-03 20:27:50 +01001662 dep->endpoint.max_streams = 15;
Felipe Balbi72246da2011-08-19 18:10:58 +03001663 dep->endpoint.ops = &dwc3_gadget_ep_ops;
1664 list_add_tail(&dep->endpoint.ep_list,
1665 &dwc->gadget.ep_list);
1666
1667 ret = dwc3_alloc_trb_pool(dep);
Felipe Balbi25b8ff62011-11-04 12:32:47 +02001668 if (ret)
Felipe Balbi72246da2011-08-19 18:10:58 +03001669 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001670 }
Felipe Balbi25b8ff62011-11-04 12:32:47 +02001671
Felipe Balbi72246da2011-08-19 18:10:58 +03001672 INIT_LIST_HEAD(&dep->request_list);
1673 INIT_LIST_HEAD(&dep->req_queued);
1674 }
1675
1676 return 0;
1677}
1678
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03001679static int dwc3_gadget_init_endpoints(struct dwc3 *dwc)
1680{
1681 int ret;
1682
1683 INIT_LIST_HEAD(&dwc->gadget.ep_list);
1684
1685 ret = dwc3_gadget_init_hw_endpoints(dwc, dwc->num_out_eps, 0);
1686 if (ret < 0) {
1687 dev_vdbg(dwc->dev, "failed to allocate OUT endpoints\n");
1688 return ret;
1689 }
1690
1691 ret = dwc3_gadget_init_hw_endpoints(dwc, dwc->num_in_eps, 1);
1692 if (ret < 0) {
1693 dev_vdbg(dwc->dev, "failed to allocate IN endpoints\n");
1694 return ret;
1695 }
1696
1697 return 0;
1698}
1699
Felipe Balbi72246da2011-08-19 18:10:58 +03001700static void dwc3_gadget_free_endpoints(struct dwc3 *dwc)
1701{
1702 struct dwc3_ep *dep;
1703 u8 epnum;
1704
1705 for (epnum = 0; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
1706 dep = dwc->eps[epnum];
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03001707 if (!dep)
1708 continue;
George Cherian5bf8fae2013-05-27 14:35:49 +05301709 /*
1710 * Physical endpoints 0 and 1 are special; they form the
1711 * bi-directional USB endpoint 0.
1712 *
1713 * For those two physical endpoints, we don't allocate a TRB
1714 * pool nor do we add them the endpoints list. Due to that, we
1715 * shouldn't do these two operations otherwise we would end up
1716 * with all sorts of bugs when removing dwc3.ko.
1717 */
1718 if (epnum != 0 && epnum != 1) {
1719 dwc3_free_trb_pool(dep);
Felipe Balbi72246da2011-08-19 18:10:58 +03001720 list_del(&dep->endpoint.ep_list);
George Cherian5bf8fae2013-05-27 14:35:49 +05301721 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001722
1723 kfree(dep);
1724 }
1725}
1726
Felipe Balbi72246da2011-08-19 18:10:58 +03001727/* -------------------------------------------------------------------------- */
Felipe Balbie5caff62013-02-26 15:11:05 +02001728
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05301729static int __dwc3_cleanup_done_trbs(struct dwc3 *dwc, struct dwc3_ep *dep,
1730 struct dwc3_request *req, struct dwc3_trb *trb,
1731 const struct dwc3_event_depevt *event, int status)
1732{
1733 unsigned int count;
1734 unsigned int s_pkt = 0;
1735 unsigned int trb_status;
1736
1737 if ((trb->ctrl & DWC3_TRB_CTRL_HWO) && status != -ESHUTDOWN)
1738 /*
1739 * We continue despite the error. There is not much we
1740 * can do. If we don't clean it up we loop forever. If
1741 * we skip the TRB then it gets overwritten after a
1742 * while since we use them in a ring buffer. A BUG()
1743 * would help. Lets hope that if this occurs, someone
1744 * fixes the root cause instead of looking away :)
1745 */
1746 dev_err(dwc->dev, "%s's TRB (%p) still owned by HW\n",
1747 dep->name, trb);
1748 count = trb->size & DWC3_TRB_SIZE_MASK;
1749
1750 if (dep->direction) {
1751 if (count) {
1752 trb_status = DWC3_TRB_SIZE_TRBSTS(trb->size);
1753 if (trb_status == DWC3_TRBSTS_MISSED_ISOC) {
1754 dev_dbg(dwc->dev, "incomplete IN transfer %s\n",
1755 dep->name);
1756 /*
1757 * If missed isoc occurred and there is
1758 * no request queued then issue END
1759 * TRANSFER, so that core generates
1760 * next xfernotready and we will issue
1761 * a fresh START TRANSFER.
1762 * If there are still queued request
1763 * then wait, do not issue either END
1764 * or UPDATE TRANSFER, just attach next
1765 * request in request_list during
1766 * giveback.If any future queued request
1767 * is successfully transferred then we
1768 * will issue UPDATE TRANSFER for all
1769 * request in the request_list.
1770 */
1771 dep->flags |= DWC3_EP_MISSED_ISOC;
1772 } else {
1773 dev_err(dwc->dev, "incomplete IN transfer %s\n",
1774 dep->name);
1775 status = -ECONNRESET;
1776 }
1777 } else {
1778 dep->flags &= ~DWC3_EP_MISSED_ISOC;
1779 }
1780 } else {
1781 if (count && (event->status & DEPEVT_STATUS_SHORT))
1782 s_pkt = 1;
1783 }
1784
1785 /*
1786 * We assume here we will always receive the entire data block
1787 * which we should receive. Meaning, if we program RX to
1788 * receive 4K but we receive only 2K, we assume that's all we
1789 * should receive and we simply bounce the request back to the
1790 * gadget driver for further processing.
1791 */
1792 req->request.actual += req->request.length - count;
1793 if (s_pkt)
1794 return 1;
1795 if ((event->status & DEPEVT_STATUS_LST) &&
1796 (trb->ctrl & (DWC3_TRB_CTRL_LST |
1797 DWC3_TRB_CTRL_HWO)))
1798 return 1;
1799 if ((event->status & DEPEVT_STATUS_IOC) &&
1800 (trb->ctrl & DWC3_TRB_CTRL_IOC))
1801 return 1;
1802 return 0;
1803}
1804
Felipe Balbi72246da2011-08-19 18:10:58 +03001805static int dwc3_cleanup_done_reqs(struct dwc3 *dwc, struct dwc3_ep *dep,
1806 const struct dwc3_event_depevt *event, int status)
1807{
1808 struct dwc3_request *req;
Felipe Balbif6bafc62012-02-06 11:04:53 +02001809 struct dwc3_trb *trb;
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05301810 unsigned int slot;
1811 unsigned int i;
1812 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001813
1814 do {
1815 req = next_request(&dep->req_queued);
Sebastian Andrzej Siewiord39ee7b2011-11-03 10:32:20 +01001816 if (!req) {
1817 WARN_ON_ONCE(1);
1818 return 1;
1819 }
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05301820 i = 0;
1821 do {
1822 slot = req->start_slot + i;
1823 if ((slot == DWC3_TRB_NUM - 1) &&
1824 usb_endpoint_xfer_isoc(dep->endpoint.desc))
1825 slot++;
1826 slot %= DWC3_TRB_NUM;
1827 trb = &dep->trb_pool[slot];
Felipe Balbi72246da2011-08-19 18:10:58 +03001828
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05301829 ret = __dwc3_cleanup_done_trbs(dwc, dep, req, trb,
1830 event, status);
1831 if (ret)
1832 break;
1833 }while (++i < req->request.num_mapped_sgs);
Felipe Balbi72246da2011-08-19 18:10:58 +03001834
Felipe Balbi72246da2011-08-19 18:10:58 +03001835 dwc3_gadget_giveback(dep, req, status);
Pratyush Anande5ba5ec2013-01-14 15:59:37 +05301836
1837 if (ret)
Felipe Balbi72246da2011-08-19 18:10:58 +03001838 break;
1839 } while (1);
1840
Pratyush Anandcdc359d2013-01-14 15:59:34 +05301841 if (usb_endpoint_xfer_isoc(dep->endpoint.desc) &&
1842 list_empty(&dep->req_queued)) {
1843 if (list_empty(&dep->request_list)) {
1844 /*
1845 * If there is no entry in request list then do
1846 * not issue END TRANSFER now. Just set PENDING
1847 * flag, so that END TRANSFER is issued when an
1848 * entry is added into request list.
1849 */
1850 dep->flags = DWC3_EP_PENDING_REQUEST;
1851 } else {
1852 dwc3_stop_active_transfer(dwc, dep->number);
1853 dep->flags = DWC3_EP_ENABLED;
1854 }
Pratyush Anand7efea862013-01-14 15:59:32 +05301855 return 1;
1856 }
1857
Felipe Balbi72246da2011-08-19 18:10:58 +03001858 return 1;
1859}
1860
1861static void dwc3_endpoint_transfer_complete(struct dwc3 *dwc,
1862 struct dwc3_ep *dep, const struct dwc3_event_depevt *event,
1863 int start_new)
1864{
1865 unsigned status = 0;
1866 int clean_busy;
1867
1868 if (event->status & DEPEVT_STATUS_BUSERR)
1869 status = -ECONNRESET;
1870
Paul Zimmerman1d046792012-02-15 18:56:56 -08001871 clean_busy = dwc3_cleanup_done_reqs(dwc, dep, event, status);
Paul Zimmermanc2df85c2012-02-24 17:32:18 -08001872 if (clean_busy)
Felipe Balbi72246da2011-08-19 18:10:58 +03001873 dep->flags &= ~DWC3_EP_BUSY;
Felipe Balbifae2b902011-10-14 13:00:30 +03001874
1875 /*
1876 * WORKAROUND: This is the 2nd half of U1/U2 -> U0 workaround.
1877 * See dwc3_gadget_linksts_change_interrupt() for 1st half.
1878 */
1879 if (dwc->revision < DWC3_REVISION_183A) {
1880 u32 reg;
1881 int i;
1882
1883 for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) {
Moiz Sonasath348e0262012-08-01 14:08:30 -05001884 dep = dwc->eps[i];
Felipe Balbifae2b902011-10-14 13:00:30 +03001885
1886 if (!(dep->flags & DWC3_EP_ENABLED))
1887 continue;
1888
1889 if (!list_empty(&dep->req_queued))
1890 return;
1891 }
1892
1893 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
1894 reg |= dwc->u1u2;
1895 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1896
1897 dwc->u1u2 = 0;
1898 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001899}
1900
Felipe Balbi72246da2011-08-19 18:10:58 +03001901static void dwc3_endpoint_interrupt(struct dwc3 *dwc,
1902 const struct dwc3_event_depevt *event)
1903{
1904 struct dwc3_ep *dep;
1905 u8 epnum = event->endpoint_number;
1906
1907 dep = dwc->eps[epnum];
1908
Felipe Balbi3336abb2012-06-06 09:19:35 +03001909 if (!(dep->flags & DWC3_EP_ENABLED))
1910 return;
1911
Felipe Balbi72246da2011-08-19 18:10:58 +03001912 dev_vdbg(dwc->dev, "%s: %s\n", dep->name,
1913 dwc3_ep_event_string(event->endpoint_event));
1914
1915 if (epnum == 0 || epnum == 1) {
1916 dwc3_ep0_interrupt(dwc, event);
1917 return;
1918 }
1919
1920 switch (event->endpoint_event) {
1921 case DWC3_DEPEVT_XFERCOMPLETE:
Felipe Balbib4996a82012-06-06 12:04:13 +03001922 dep->resource_index = 0;
Paul Zimmermanc2df85c2012-02-24 17:32:18 -08001923
Ido Shayevitz16e78db2012-03-12 20:25:24 +02001924 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001925 dev_dbg(dwc->dev, "%s is an Isochronous endpoint\n",
1926 dep->name);
1927 return;
1928 }
1929
1930 dwc3_endpoint_transfer_complete(dwc, dep, event, 1);
1931 break;
1932 case DWC3_DEPEVT_XFERINPROGRESS:
Ido Shayevitz16e78db2012-03-12 20:25:24 +02001933 if (!usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001934 dev_dbg(dwc->dev, "%s is not an Isochronous endpoint\n",
1935 dep->name);
1936 return;
1937 }
1938
1939 dwc3_endpoint_transfer_complete(dwc, dep, event, 0);
1940 break;
1941 case DWC3_DEPEVT_XFERNOTREADY:
Ido Shayevitz16e78db2012-03-12 20:25:24 +02001942 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001943 dwc3_gadget_start_isoc(dwc, dep, event);
1944 } else {
1945 int ret;
1946
1947 dev_vdbg(dwc->dev, "%s: reason %s\n",
Felipe Balbi40aa41f2012-01-18 17:06:03 +02001948 dep->name, event->status &
1949 DEPEVT_STATUS_TRANSFER_ACTIVE
Felipe Balbi72246da2011-08-19 18:10:58 +03001950 ? "Transfer Active"
1951 : "Transfer Not Active");
1952
1953 ret = __dwc3_gadget_kick_transfer(dep, 0, 1);
1954 if (!ret || ret == -EBUSY)
1955 return;
1956
1957 dev_dbg(dwc->dev, "%s: failed to kick transfers\n",
1958 dep->name);
1959 }
1960
1961 break;
Felipe Balbi879631a2011-09-30 10:58:47 +03001962 case DWC3_DEPEVT_STREAMEVT:
Ido Shayevitz16e78db2012-03-12 20:25:24 +02001963 if (!usb_endpoint_xfer_bulk(dep->endpoint.desc)) {
Felipe Balbi879631a2011-09-30 10:58:47 +03001964 dev_err(dwc->dev, "Stream event for non-Bulk %s\n",
1965 dep->name);
1966 return;
1967 }
1968
1969 switch (event->status) {
1970 case DEPEVT_STREAMEVT_FOUND:
1971 dev_vdbg(dwc->dev, "Stream %d found and started\n",
1972 event->parameters);
1973
1974 break;
1975 case DEPEVT_STREAMEVT_NOTFOUND:
1976 /* FALLTHROUGH */
1977 default:
1978 dev_dbg(dwc->dev, "Couldn't find suitable stream\n");
1979 }
1980 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03001981 case DWC3_DEPEVT_RXTXFIFOEVT:
1982 dev_dbg(dwc->dev, "%s FIFO Overrun\n", dep->name);
1983 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03001984 case DWC3_DEPEVT_EPCMDCMPLT:
Felipe Balbiea53b882012-02-17 12:10:04 +02001985 dev_vdbg(dwc->dev, "Endpoint Command Complete\n");
Felipe Balbi72246da2011-08-19 18:10:58 +03001986 break;
1987 }
1988}
1989
1990static void dwc3_disconnect_gadget(struct dwc3 *dwc)
1991{
1992 if (dwc->gadget_driver && dwc->gadget_driver->disconnect) {
1993 spin_unlock(&dwc->lock);
1994 dwc->gadget_driver->disconnect(&dwc->gadget);
1995 spin_lock(&dwc->lock);
1996 }
1997}
1998
1999static void dwc3_stop_active_transfer(struct dwc3 *dwc, u32 epnum)
2000{
2001 struct dwc3_ep *dep;
2002 struct dwc3_gadget_ep_cmd_params params;
2003 u32 cmd;
2004 int ret;
2005
2006 dep = dwc->eps[epnum];
2007
Felipe Balbib4996a82012-06-06 12:04:13 +03002008 if (!dep->resource_index)
Pratyush Anand3daf74d2012-06-23 02:23:08 +05302009 return;
2010
Pratyush Anand57911502012-07-06 15:19:10 +05302011 /*
2012 * NOTICE: We are violating what the Databook says about the
2013 * EndTransfer command. Ideally we would _always_ wait for the
2014 * EndTransfer Command Completion IRQ, but that's causing too
2015 * much trouble synchronizing between us and gadget driver.
2016 *
2017 * We have discussed this with the IP Provider and it was
2018 * suggested to giveback all requests here, but give HW some
2019 * extra time to synchronize with the interconnect. We're using
2020 * an arbitraty 100us delay for that.
2021 *
2022 * Note also that a similar handling was tested by Synopsys
2023 * (thanks a lot Paul) and nothing bad has come out of it.
2024 * In short, what we're doing is:
2025 *
2026 * - Issue EndTransfer WITH CMDIOC bit set
2027 * - Wait 100us
2028 */
2029
Pratyush Anand3daf74d2012-06-23 02:23:08 +05302030 cmd = DWC3_DEPCMD_ENDTRANSFER;
2031 cmd |= DWC3_DEPCMD_HIPRI_FORCERM | DWC3_DEPCMD_CMDIOC;
Felipe Balbib4996a82012-06-06 12:04:13 +03002032 cmd |= DWC3_DEPCMD_PARAM(dep->resource_index);
Pratyush Anand3daf74d2012-06-23 02:23:08 +05302033 memset(&params, 0, sizeof(params));
2034 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number, cmd, &params);
2035 WARN_ON_ONCE(ret);
Felipe Balbib4996a82012-06-06 12:04:13 +03002036 dep->resource_index = 0;
Felipe Balbi041d81f2012-10-04 11:58:00 +03002037 dep->flags &= ~DWC3_EP_BUSY;
Pratyush Anand57911502012-07-06 15:19:10 +05302038 udelay(100);
Felipe Balbi72246da2011-08-19 18:10:58 +03002039}
2040
2041static void dwc3_stop_active_transfers(struct dwc3 *dwc)
2042{
2043 u32 epnum;
2044
2045 for (epnum = 2; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
2046 struct dwc3_ep *dep;
2047
2048 dep = dwc->eps[epnum];
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03002049 if (!dep)
2050 continue;
2051
Felipe Balbi72246da2011-08-19 18:10:58 +03002052 if (!(dep->flags & DWC3_EP_ENABLED))
2053 continue;
2054
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +02002055 dwc3_remove_requests(dwc, dep);
Felipe Balbi72246da2011-08-19 18:10:58 +03002056 }
2057}
2058
2059static void dwc3_clear_stall_all_ep(struct dwc3 *dwc)
2060{
2061 u32 epnum;
2062
2063 for (epnum = 1; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
2064 struct dwc3_ep *dep;
2065 struct dwc3_gadget_ep_cmd_params params;
2066 int ret;
2067
2068 dep = dwc->eps[epnum];
Felipe Balbi6a1e3ef2011-05-05 16:21:59 +03002069 if (!dep)
2070 continue;
Felipe Balbi72246da2011-08-19 18:10:58 +03002071
2072 if (!(dep->flags & DWC3_EP_STALL))
2073 continue;
2074
2075 dep->flags &= ~DWC3_EP_STALL;
2076
2077 memset(&params, 0, sizeof(params));
2078 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number,
2079 DWC3_DEPCMD_CLEARSTALL, &params);
2080 WARN_ON_ONCE(ret);
2081 }
2082}
2083
2084static void dwc3_gadget_disconnect_interrupt(struct dwc3 *dwc)
2085{
Felipe Balbic4430a22012-05-24 10:30:01 +03002086 int reg;
2087
Felipe Balbi72246da2011-08-19 18:10:58 +03002088 dev_vdbg(dwc->dev, "%s\n", __func__);
Felipe Balbi72246da2011-08-19 18:10:58 +03002089
2090 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2091 reg &= ~DWC3_DCTL_INITU1ENA;
2092 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2093
2094 reg &= ~DWC3_DCTL_INITU2ENA;
2095 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +03002096
Felipe Balbi72246da2011-08-19 18:10:58 +03002097 dwc3_disconnect_gadget(dwc);
Paul Zimmermanb23c8432011-09-30 10:58:42 +03002098 dwc->start_config_issued = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03002099
2100 dwc->gadget.speed = USB_SPEED_UNKNOWN;
Felipe Balbidf62df52011-10-14 15:11:49 +03002101 dwc->setup_packet_pending = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03002102}
2103
Felipe Balbi72246da2011-08-19 18:10:58 +03002104static void dwc3_gadget_reset_interrupt(struct dwc3 *dwc)
2105{
2106 u32 reg;
2107
2108 dev_vdbg(dwc->dev, "%s\n", __func__);
2109
Felipe Balbidf62df52011-10-14 15:11:49 +03002110 /*
2111 * WORKAROUND: DWC3 revisions <1.88a have an issue which
2112 * would cause a missing Disconnect Event if there's a
2113 * pending Setup Packet in the FIFO.
2114 *
2115 * There's no suggested workaround on the official Bug
2116 * report, which states that "unless the driver/application
2117 * is doing any special handling of a disconnect event,
2118 * there is no functional issue".
2119 *
2120 * Unfortunately, it turns out that we _do_ some special
2121 * handling of a disconnect event, namely complete all
2122 * pending transfers, notify gadget driver of the
2123 * disconnection, and so on.
2124 *
2125 * Our suggested workaround is to follow the Disconnect
2126 * Event steps here, instead, based on a setup_packet_pending
2127 * flag. Such flag gets set whenever we have a XferNotReady
2128 * event on EP0 and gets cleared on XferComplete for the
2129 * same endpoint.
2130 *
2131 * Refers to:
2132 *
2133 * STAR#9000466709: RTL: Device : Disconnect event not
2134 * generated if setup packet pending in FIFO
2135 */
2136 if (dwc->revision < DWC3_REVISION_188A) {
2137 if (dwc->setup_packet_pending)
2138 dwc3_gadget_disconnect_interrupt(dwc);
2139 }
2140
Felipe Balbi961906e2011-12-20 15:37:21 +02002141 /* after reset -> Default State */
Felipe Balbi14cd5922011-12-19 13:01:52 +02002142 usb_gadget_set_state(&dwc->gadget, USB_STATE_DEFAULT);
Felipe Balbi961906e2011-12-20 15:37:21 +02002143
Felipe Balbi72246da2011-08-19 18:10:58 +03002144 if (dwc->gadget.speed != USB_SPEED_UNKNOWN)
2145 dwc3_disconnect_gadget(dwc);
2146
2147 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2148 reg &= ~DWC3_DCTL_TSTCTRL_MASK;
2149 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Gerard Cauvy3b637362012-02-10 12:21:18 +02002150 dwc->test_mode = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03002151
2152 dwc3_stop_active_transfers(dwc);
2153 dwc3_clear_stall_all_ep(dwc);
Paul Zimmermanb23c8432011-09-30 10:58:42 +03002154 dwc->start_config_issued = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03002155
2156 /* Reset device address to zero */
2157 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2158 reg &= ~(DWC3_DCFG_DEVADDR_MASK);
2159 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +03002160}
2161
2162static void dwc3_update_ram_clk_sel(struct dwc3 *dwc, u32 speed)
2163{
2164 u32 reg;
2165 u32 usb30_clock = DWC3_GCTL_CLK_BUS;
2166
2167 /*
2168 * We change the clock only at SS but I dunno why I would want to do
2169 * this. Maybe it becomes part of the power saving plan.
2170 */
2171
2172 if (speed != DWC3_DSTS_SUPERSPEED)
2173 return;
2174
2175 /*
2176 * RAMClkSel is reset to 0 after USB reset, so it must be reprogrammed
2177 * each time on Connect Done.
2178 */
2179 if (!usb30_clock)
2180 return;
2181
2182 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
2183 reg |= DWC3_GCTL_RAMCLKSEL(usb30_clock);
2184 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
2185}
2186
Felipe Balbi72246da2011-08-19 18:10:58 +03002187static void dwc3_gadget_conndone_interrupt(struct dwc3 *dwc)
2188{
Felipe Balbi72246da2011-08-19 18:10:58 +03002189 struct dwc3_ep *dep;
2190 int ret;
2191 u32 reg;
2192 u8 speed;
2193
2194 dev_vdbg(dwc->dev, "%s\n", __func__);
2195
Felipe Balbi72246da2011-08-19 18:10:58 +03002196 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
2197 speed = reg & DWC3_DSTS_CONNECTSPD;
2198 dwc->speed = speed;
2199
2200 dwc3_update_ram_clk_sel(dwc, speed);
2201
2202 switch (speed) {
2203 case DWC3_DCFG_SUPERSPEED:
Felipe Balbi05870c52011-10-14 14:51:38 +03002204 /*
2205 * WORKAROUND: DWC3 revisions <1.90a have an issue which
2206 * would cause a missing USB3 Reset event.
2207 *
2208 * In such situations, we should force a USB3 Reset
2209 * event by calling our dwc3_gadget_reset_interrupt()
2210 * routine.
2211 *
2212 * Refers to:
2213 *
2214 * STAR#9000483510: RTL: SS : USB3 reset event may
2215 * not be generated always when the link enters poll
2216 */
2217 if (dwc->revision < DWC3_REVISION_190A)
2218 dwc3_gadget_reset_interrupt(dwc);
2219
Felipe Balbi72246da2011-08-19 18:10:58 +03002220 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
2221 dwc->gadget.ep0->maxpacket = 512;
2222 dwc->gadget.speed = USB_SPEED_SUPER;
2223 break;
2224 case DWC3_DCFG_HIGHSPEED:
2225 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
2226 dwc->gadget.ep0->maxpacket = 64;
2227 dwc->gadget.speed = USB_SPEED_HIGH;
2228 break;
2229 case DWC3_DCFG_FULLSPEED2:
2230 case DWC3_DCFG_FULLSPEED1:
2231 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
2232 dwc->gadget.ep0->maxpacket = 64;
2233 dwc->gadget.speed = USB_SPEED_FULL;
2234 break;
2235 case DWC3_DCFG_LOWSPEED:
2236 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(8);
2237 dwc->gadget.ep0->maxpacket = 8;
2238 dwc->gadget.speed = USB_SPEED_LOW;
2239 break;
2240 }
2241
Pratyush Anand2b758352013-01-14 15:59:31 +05302242 /* Enable USB2 LPM Capability */
2243
2244 if ((dwc->revision > DWC3_REVISION_194A)
2245 && (speed != DWC3_DCFG_SUPERSPEED)) {
2246 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
2247 reg |= DWC3_DCFG_LPM_CAP;
2248 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
2249
2250 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2251 reg &= ~(DWC3_DCTL_HIRD_THRES_MASK | DWC3_DCTL_L1_HIBER_EN);
2252
Felipe Balbi1a947742013-01-24 11:56:11 +02002253 /*
2254 * TODO: This should be configurable. For now using
2255 * maximum allowed HIRD threshold value of 0b1100
2256 */
2257 reg |= DWC3_DCTL_HIRD_THRES(12);
Pratyush Anand2b758352013-01-14 15:59:31 +05302258
2259 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2260 }
2261
Felipe Balbi72246da2011-08-19 18:10:58 +03002262 dep = dwc->eps[0];
Felipe Balbi4b345c92012-07-16 14:08:16 +03002263 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, true);
Felipe Balbi72246da2011-08-19 18:10:58 +03002264 if (ret) {
2265 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
2266 return;
2267 }
2268
2269 dep = dwc->eps[1];
Felipe Balbi4b345c92012-07-16 14:08:16 +03002270 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, true);
Felipe Balbi72246da2011-08-19 18:10:58 +03002271 if (ret) {
2272 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
2273 return;
2274 }
2275
2276 /*
2277 * Configure PHY via GUSB3PIPECTLn if required.
2278 *
2279 * Update GTXFIFOSIZn
2280 *
2281 * In both cases reset values should be sufficient.
2282 */
2283}
2284
2285static void dwc3_gadget_wakeup_interrupt(struct dwc3 *dwc)
2286{
2287 dev_vdbg(dwc->dev, "%s\n", __func__);
2288
2289 /*
2290 * TODO take core out of low power mode when that's
2291 * implemented.
2292 */
2293
2294 dwc->gadget_driver->resume(&dwc->gadget);
2295}
2296
2297static void dwc3_gadget_linksts_change_interrupt(struct dwc3 *dwc,
2298 unsigned int evtinfo)
2299{
Felipe Balbifae2b902011-10-14 13:00:30 +03002300 enum dwc3_link_state next = evtinfo & DWC3_LINK_STATE_MASK;
Felipe Balbi0b0cc1c2012-09-18 21:39:24 +03002301 unsigned int pwropt;
2302
2303 /*
2304 * WORKAROUND: DWC3 < 2.50a have an issue when configured without
2305 * Hibernation mode enabled which would show up when device detects
2306 * host-initiated U3 exit.
2307 *
2308 * In that case, device will generate a Link State Change Interrupt
2309 * from U3 to RESUME which is only necessary if Hibernation is
2310 * configured in.
2311 *
2312 * There are no functional changes due to such spurious event and we
2313 * just need to ignore it.
2314 *
2315 * Refers to:
2316 *
2317 * STAR#9000570034 RTL: SS Resume event generated in non-Hibernation
2318 * operational mode
2319 */
2320 pwropt = DWC3_GHWPARAMS1_EN_PWROPT(dwc->hwparams.hwparams1);
2321 if ((dwc->revision < DWC3_REVISION_250A) &&
2322 (pwropt != DWC3_GHWPARAMS1_EN_PWROPT_HIB)) {
2323 if ((dwc->link_state == DWC3_LINK_STATE_U3) &&
2324 (next == DWC3_LINK_STATE_RESUME)) {
2325 dev_vdbg(dwc->dev, "ignoring transition U3 -> Resume\n");
2326 return;
2327 }
2328 }
Felipe Balbifae2b902011-10-14 13:00:30 +03002329
2330 /*
2331 * WORKAROUND: DWC3 Revisions <1.83a have an issue which, depending
2332 * on the link partner, the USB session might do multiple entry/exit
2333 * of low power states before a transfer takes place.
2334 *
2335 * Due to this problem, we might experience lower throughput. The
2336 * suggested workaround is to disable DCTL[12:9] bits if we're
2337 * transitioning from U1/U2 to U0 and enable those bits again
2338 * after a transfer completes and there are no pending transfers
2339 * on any of the enabled endpoints.
2340 *
2341 * This is the first half of that workaround.
2342 *
2343 * Refers to:
2344 *
2345 * STAR#9000446952: RTL: Device SS : if U1/U2 ->U0 takes >128us
2346 * core send LGO_Ux entering U0
2347 */
2348 if (dwc->revision < DWC3_REVISION_183A) {
2349 if (next == DWC3_LINK_STATE_U0) {
2350 u32 u1u2;
2351 u32 reg;
2352
2353 switch (dwc->link_state) {
2354 case DWC3_LINK_STATE_U1:
2355 case DWC3_LINK_STATE_U2:
2356 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2357 u1u2 = reg & (DWC3_DCTL_INITU2ENA
2358 | DWC3_DCTL_ACCEPTU2ENA
2359 | DWC3_DCTL_INITU1ENA
2360 | DWC3_DCTL_ACCEPTU1ENA);
2361
2362 if (!dwc->u1u2)
2363 dwc->u1u2 = reg & u1u2;
2364
2365 reg &= ~u1u2;
2366
2367 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2368 break;
2369 default:
2370 /* do nothing */
2371 break;
2372 }
2373 }
2374 }
2375
2376 dwc->link_state = next;
Felipe Balbi019ac832011-09-08 21:18:47 +03002377
2378 dev_vdbg(dwc->dev, "%s link %d\n", __func__, dwc->link_state);
Felipe Balbi72246da2011-08-19 18:10:58 +03002379}
2380
Felipe Balbie1dadd32014-02-25 14:47:54 -06002381static void dwc3_gadget_hibernation_interrupt(struct dwc3 *dwc,
2382 unsigned int evtinfo)
2383{
2384 unsigned int is_ss = evtinfo & BIT(4);
2385
2386 /**
2387 * WORKAROUND: DWC3 revison 2.20a with hibernation support
2388 * have a known issue which can cause USB CV TD.9.23 to fail
2389 * randomly.
2390 *
2391 * Because of this issue, core could generate bogus hibernation
2392 * events which SW needs to ignore.
2393 *
2394 * Refers to:
2395 *
2396 * STAR#9000546576: Device Mode Hibernation: Issue in USB 2.0
2397 * Device Fallback from SuperSpeed
2398 */
2399 if (is_ss ^ (dwc->speed == USB_SPEED_SUPER))
2400 return;
2401
2402 /* enter hibernation here */
2403}
2404
Felipe Balbi72246da2011-08-19 18:10:58 +03002405static void dwc3_gadget_interrupt(struct dwc3 *dwc,
2406 const struct dwc3_event_devt *event)
2407{
2408 switch (event->type) {
2409 case DWC3_DEVICE_EVENT_DISCONNECT:
2410 dwc3_gadget_disconnect_interrupt(dwc);
2411 break;
2412 case DWC3_DEVICE_EVENT_RESET:
2413 dwc3_gadget_reset_interrupt(dwc);
2414 break;
2415 case DWC3_DEVICE_EVENT_CONNECT_DONE:
2416 dwc3_gadget_conndone_interrupt(dwc);
2417 break;
2418 case DWC3_DEVICE_EVENT_WAKEUP:
2419 dwc3_gadget_wakeup_interrupt(dwc);
2420 break;
Felipe Balbie1dadd32014-02-25 14:47:54 -06002421 case DWC3_DEVICE_EVENT_HIBER_REQ:
2422 if (dev_WARN_ONCE(dwc->dev, !dwc->has_hibernation,
2423 "unexpected hibernation event\n"))
2424 break;
2425
2426 dwc3_gadget_hibernation_interrupt(dwc, event->event_info);
2427 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03002428 case DWC3_DEVICE_EVENT_LINK_STATUS_CHANGE:
2429 dwc3_gadget_linksts_change_interrupt(dwc, event->event_info);
2430 break;
2431 case DWC3_DEVICE_EVENT_EOPF:
2432 dev_vdbg(dwc->dev, "End of Periodic Frame\n");
2433 break;
2434 case DWC3_DEVICE_EVENT_SOF:
2435 dev_vdbg(dwc->dev, "Start of Periodic Frame\n");
2436 break;
2437 case DWC3_DEVICE_EVENT_ERRATIC_ERROR:
2438 dev_vdbg(dwc->dev, "Erratic Error\n");
2439 break;
2440 case DWC3_DEVICE_EVENT_CMD_CMPL:
2441 dev_vdbg(dwc->dev, "Command Complete\n");
2442 break;
2443 case DWC3_DEVICE_EVENT_OVERFLOW:
2444 dev_vdbg(dwc->dev, "Overflow\n");
2445 break;
2446 default:
2447 dev_dbg(dwc->dev, "UNKNOWN IRQ %d\n", event->type);
2448 }
2449}
2450
2451static void dwc3_process_event_entry(struct dwc3 *dwc,
2452 const union dwc3_event *event)
2453{
2454 /* Endpoint IRQ, handle it and return early */
2455 if (event->type.is_devspec == 0) {
2456 /* depevt */
2457 return dwc3_endpoint_interrupt(dwc, &event->depevt);
2458 }
2459
2460 switch (event->type.type) {
2461 case DWC3_EVENT_TYPE_DEV:
2462 dwc3_gadget_interrupt(dwc, &event->devt);
2463 break;
2464 /* REVISIT what to do with Carkit and I2C events ? */
2465 default:
2466 dev_err(dwc->dev, "UNKNOWN IRQ type %d\n", event->raw);
2467 }
2468}
2469
Felipe Balbif42f2442013-06-12 21:25:08 +03002470static irqreturn_t dwc3_process_event_buf(struct dwc3 *dwc, u32 buf)
2471{
2472 struct dwc3_event_buffer *evt;
2473 irqreturn_t ret = IRQ_NONE;
2474 int left;
2475 u32 reg;
2476
2477 evt = dwc->ev_buffs[buf];
2478 left = evt->count;
2479
2480 if (!(evt->flags & DWC3_EVENT_PENDING))
2481 return IRQ_NONE;
2482
2483 while (left > 0) {
2484 union dwc3_event event;
2485
2486 event.raw = *(u32 *) (evt->buf + evt->lpos);
2487
2488 dwc3_process_event_entry(dwc, &event);
2489
2490 /*
2491 * FIXME we wrap around correctly to the next entry as
2492 * almost all entries are 4 bytes in size. There is one
2493 * entry which has 12 bytes which is a regular entry
2494 * followed by 8 bytes data. ATM I don't know how
2495 * things are organized if we get next to the a
2496 * boundary so I worry about that once we try to handle
2497 * that.
2498 */
2499 evt->lpos = (evt->lpos + 4) % DWC3_EVENT_BUFFERS_SIZE;
2500 left -= 4;
2501
2502 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(buf), 4);
2503 }
2504
2505 evt->count = 0;
2506 evt->flags &= ~DWC3_EVENT_PENDING;
2507 ret = IRQ_HANDLED;
2508
2509 /* Unmask interrupt */
2510 reg = dwc3_readl(dwc->regs, DWC3_GEVNTSIZ(buf));
2511 reg &= ~DWC3_GEVNTSIZ_INTMASK;
2512 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(buf), reg);
2513
2514 return ret;
2515}
2516
Felipe Balbib15a7622011-06-30 16:57:15 +03002517static irqreturn_t dwc3_thread_interrupt(int irq, void *_dwc)
2518{
2519 struct dwc3 *dwc = _dwc;
2520 unsigned long flags;
2521 irqreturn_t ret = IRQ_NONE;
2522 int i;
2523
2524 spin_lock_irqsave(&dwc->lock, flags);
2525
Felipe Balbif42f2442013-06-12 21:25:08 +03002526 for (i = 0; i < dwc->num_event_buffers; i++)
2527 ret |= dwc3_process_event_buf(dwc, i);
Felipe Balbib15a7622011-06-30 16:57:15 +03002528
2529 spin_unlock_irqrestore(&dwc->lock, flags);
2530
2531 return ret;
2532}
2533
Felipe Balbi7f97aa92013-06-12 21:16:11 +03002534static irqreturn_t dwc3_check_event_buf(struct dwc3 *dwc, u32 buf)
Felipe Balbi72246da2011-08-19 18:10:58 +03002535{
2536 struct dwc3_event_buffer *evt;
Felipe Balbi72246da2011-08-19 18:10:58 +03002537 u32 count;
Felipe Balbie8adfc32013-06-12 21:11:14 +03002538 u32 reg;
Felipe Balbi72246da2011-08-19 18:10:58 +03002539
Felipe Balbib15a7622011-06-30 16:57:15 +03002540 evt = dwc->ev_buffs[buf];
2541
Felipe Balbi72246da2011-08-19 18:10:58 +03002542 count = dwc3_readl(dwc->regs, DWC3_GEVNTCOUNT(buf));
2543 count &= DWC3_GEVNTCOUNT_MASK;
2544 if (!count)
2545 return IRQ_NONE;
2546
Felipe Balbib15a7622011-06-30 16:57:15 +03002547 evt->count = count;
2548 evt->flags |= DWC3_EVENT_PENDING;
Felipe Balbi72246da2011-08-19 18:10:58 +03002549
Felipe Balbie8adfc32013-06-12 21:11:14 +03002550 /* Mask interrupt */
2551 reg = dwc3_readl(dwc->regs, DWC3_GEVNTSIZ(buf));
2552 reg |= DWC3_GEVNTSIZ_INTMASK;
2553 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(buf), reg);
2554
Felipe Balbib15a7622011-06-30 16:57:15 +03002555 return IRQ_WAKE_THREAD;
Felipe Balbi72246da2011-08-19 18:10:58 +03002556}
2557
2558static irqreturn_t dwc3_interrupt(int irq, void *_dwc)
2559{
2560 struct dwc3 *dwc = _dwc;
2561 int i;
2562 irqreturn_t ret = IRQ_NONE;
2563
2564 spin_lock(&dwc->lock);
2565
Felipe Balbi9f622b22011-10-12 10:31:04 +03002566 for (i = 0; i < dwc->num_event_buffers; i++) {
Felipe Balbi72246da2011-08-19 18:10:58 +03002567 irqreturn_t status;
2568
Felipe Balbi7f97aa92013-06-12 21:16:11 +03002569 status = dwc3_check_event_buf(dwc, i);
Felipe Balbib15a7622011-06-30 16:57:15 +03002570 if (status == IRQ_WAKE_THREAD)
Felipe Balbi72246da2011-08-19 18:10:58 +03002571 ret = status;
2572 }
2573
2574 spin_unlock(&dwc->lock);
2575
2576 return ret;
2577}
2578
2579/**
2580 * dwc3_gadget_init - Initializes gadget related registers
Paul Zimmerman1d046792012-02-15 18:56:56 -08002581 * @dwc: pointer to our controller context structure
Felipe Balbi72246da2011-08-19 18:10:58 +03002582 *
2583 * Returns 0 on success otherwise negative errno.
2584 */
Bill Pemberton41ac7b32012-11-19 13:21:48 -05002585int dwc3_gadget_init(struct dwc3 *dwc)
Felipe Balbi72246da2011-08-19 18:10:58 +03002586{
Felipe Balbi72246da2011-08-19 18:10:58 +03002587 int ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03002588
2589 dwc->ctrl_req = dma_alloc_coherent(dwc->dev, sizeof(*dwc->ctrl_req),
2590 &dwc->ctrl_req_addr, GFP_KERNEL);
2591 if (!dwc->ctrl_req) {
2592 dev_err(dwc->dev, "failed to allocate ctrl request\n");
2593 ret = -ENOMEM;
2594 goto err0;
2595 }
2596
2597 dwc->ep0_trb = dma_alloc_coherent(dwc->dev, sizeof(*dwc->ep0_trb),
2598 &dwc->ep0_trb_addr, GFP_KERNEL);
2599 if (!dwc->ep0_trb) {
2600 dev_err(dwc->dev, "failed to allocate ep0 trb\n");
2601 ret = -ENOMEM;
2602 goto err1;
2603 }
2604
Felipe Balbi3ef35fa2012-05-04 12:58:14 +03002605 dwc->setup_buf = kzalloc(DWC3_EP0_BOUNCE_SIZE, GFP_KERNEL);
Felipe Balbi72246da2011-08-19 18:10:58 +03002606 if (!dwc->setup_buf) {
2607 dev_err(dwc->dev, "failed to allocate setup buffer\n");
2608 ret = -ENOMEM;
2609 goto err2;
2610 }
2611
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002612 dwc->ep0_bounce = dma_alloc_coherent(dwc->dev,
Felipe Balbi3ef35fa2012-05-04 12:58:14 +03002613 DWC3_EP0_BOUNCE_SIZE, &dwc->ep0_bounce_addr,
2614 GFP_KERNEL);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002615 if (!dwc->ep0_bounce) {
2616 dev_err(dwc->dev, "failed to allocate ep0 bounce buffer\n");
2617 ret = -ENOMEM;
2618 goto err3;
2619 }
2620
Felipe Balbi72246da2011-08-19 18:10:58 +03002621 dwc->gadget.ops = &dwc3_gadget_ops;
Michal Nazarewiczd327ab52011-11-19 18:27:37 +01002622 dwc->gadget.max_speed = USB_SPEED_SUPER;
Felipe Balbi72246da2011-08-19 18:10:58 +03002623 dwc->gadget.speed = USB_SPEED_UNKNOWN;
Felipe Balbieeb720f2011-11-28 12:46:59 +02002624 dwc->gadget.sg_supported = true;
Felipe Balbi72246da2011-08-19 18:10:58 +03002625 dwc->gadget.name = "dwc3-gadget";
2626
2627 /*
David Cohena4b9d942013-12-09 15:55:38 -08002628 * Per databook, DWC3 needs buffer size to be aligned to MaxPacketSize
2629 * on ep out.
2630 */
2631 dwc->gadget.quirk_ep_out_aligned_size = true;
2632
2633 /*
Felipe Balbi72246da2011-08-19 18:10:58 +03002634 * REVISIT: Here we should clear all pending IRQs to be
2635 * sure we're starting from a well known location.
2636 */
2637
2638 ret = dwc3_gadget_init_endpoints(dwc);
2639 if (ret)
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002640 goto err4;
Felipe Balbi72246da2011-08-19 18:10:58 +03002641
Felipe Balbi72246da2011-08-19 18:10:58 +03002642 ret = usb_add_gadget_udc(dwc->dev, &dwc->gadget);
2643 if (ret) {
2644 dev_err(dwc->dev, "failed to register udc\n");
David Cohene1f80462013-09-11 17:42:47 -07002645 goto err4;
Felipe Balbi72246da2011-08-19 18:10:58 +03002646 }
2647
2648 return 0;
2649
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002650err4:
David Cohene1f80462013-09-11 17:42:47 -07002651 dwc3_gadget_free_endpoints(dwc);
Felipe Balbi3ef35fa2012-05-04 12:58:14 +03002652 dma_free_coherent(dwc->dev, DWC3_EP0_BOUNCE_SIZE,
2653 dwc->ep0_bounce, dwc->ep0_bounce_addr);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002654
Felipe Balbi72246da2011-08-19 18:10:58 +03002655err3:
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02002656 kfree(dwc->setup_buf);
Felipe Balbi72246da2011-08-19 18:10:58 +03002657
2658err2:
2659 dma_free_coherent(dwc->dev, sizeof(*dwc->ep0_trb),
2660 dwc->ep0_trb, dwc->ep0_trb_addr);
2661
2662err1:
2663 dma_free_coherent(dwc->dev, sizeof(*dwc->ctrl_req),
2664 dwc->ctrl_req, dwc->ctrl_req_addr);
2665
2666err0:
2667 return ret;
2668}
2669
Felipe Balbi7415f172012-04-30 14:56:33 +03002670/* -------------------------------------------------------------------------- */
2671
Felipe Balbi72246da2011-08-19 18:10:58 +03002672void dwc3_gadget_exit(struct dwc3 *dwc)
2673{
Felipe Balbi72246da2011-08-19 18:10:58 +03002674 usb_del_gadget_udc(&dwc->gadget);
Felipe Balbi72246da2011-08-19 18:10:58 +03002675
Felipe Balbi72246da2011-08-19 18:10:58 +03002676 dwc3_gadget_free_endpoints(dwc);
2677
Felipe Balbi3ef35fa2012-05-04 12:58:14 +03002678 dma_free_coherent(dwc->dev, DWC3_EP0_BOUNCE_SIZE,
2679 dwc->ep0_bounce, dwc->ep0_bounce_addr);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002680
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02002681 kfree(dwc->setup_buf);
Felipe Balbi72246da2011-08-19 18:10:58 +03002682
2683 dma_free_coherent(dwc->dev, sizeof(*dwc->ep0_trb),
2684 dwc->ep0_trb, dwc->ep0_trb_addr);
2685
2686 dma_free_coherent(dwc->dev, sizeof(*dwc->ctrl_req),
2687 dwc->ctrl_req, dwc->ctrl_req_addr);
Felipe Balbi72246da2011-08-19 18:10:58 +03002688}
Felipe Balbi7415f172012-04-30 14:56:33 +03002689
2690int dwc3_gadget_prepare(struct dwc3 *dwc)
2691{
2692 if (dwc->pullups_connected)
2693 dwc3_gadget_disable_irq(dwc);
2694
2695 return 0;
2696}
2697
2698void dwc3_gadget_complete(struct dwc3 *dwc)
2699{
2700 if (dwc->pullups_connected) {
2701 dwc3_gadget_enable_irq(dwc);
2702 dwc3_gadget_run_stop(dwc, true);
2703 }
2704}
2705
2706int dwc3_gadget_suspend(struct dwc3 *dwc)
2707{
2708 __dwc3_gadget_ep_disable(dwc->eps[0]);
2709 __dwc3_gadget_ep_disable(dwc->eps[1]);
2710
2711 dwc->dcfg = dwc3_readl(dwc->regs, DWC3_DCFG);
2712
2713 return 0;
2714}
2715
2716int dwc3_gadget_resume(struct dwc3 *dwc)
2717{
2718 struct dwc3_ep *dep;
2719 int ret;
2720
2721 /* Start with SuperSpeed Default */
2722 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
2723
2724 dep = dwc->eps[0];
2725 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, false);
2726 if (ret)
2727 goto err0;
2728
2729 dep = dwc->eps[1];
2730 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL, false);
2731 if (ret)
2732 goto err1;
2733
2734 /* begin to receive SETUP packets */
2735 dwc->ep0state = EP0_SETUP_PHASE;
2736 dwc3_ep0_out_start(dwc);
2737
2738 dwc3_writel(dwc->regs, DWC3_DCFG, dwc->dcfg);
2739
2740 return 0;
2741
2742err1:
2743 __dwc3_gadget_ep_disable(dwc->eps[0]);
2744
2745err0:
2746 return ret;
2747}