blob: 6d6fb88913c883e92a8b9b157f336b05e6881147 [file] [log] [blame]
Felipe Balbi72246da2011-08-19 18:10:58 +03001/**
2 * gadget.c - DesignWare USB3 DRD Controller Gadget Framework Link
3 *
4 * Copyright (C) 2010-2011 Texas Instruments Incorporated - http://www.ti.com
Felipe Balbi72246da2011-08-19 18:10:58 +03005 *
6 * Authors: Felipe Balbi <balbi@ti.com>,
7 * Sebastian Andrzej Siewior <bigeasy@linutronix.de>
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions, and the following disclaimer,
14 * without modification.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. The names of the above-listed copyright holders may not be used
19 * to endorse or promote products derived from this software without
20 * specific prior written permission.
21 *
22 * ALTERNATIVELY, this software may be distributed under the terms of the
23 * GNU General Public License ("GPL") version 2, as published by the Free
24 * Software Foundation.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
27 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
28 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
30 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 */
38
39#include <linux/kernel.h>
40#include <linux/delay.h>
41#include <linux/slab.h>
42#include <linux/spinlock.h>
43#include <linux/platform_device.h>
44#include <linux/pm_runtime.h>
45#include <linux/interrupt.h>
46#include <linux/io.h>
47#include <linux/list.h>
48#include <linux/dma-mapping.h>
49
50#include <linux/usb/ch9.h>
51#include <linux/usb/gadget.h>
52
53#include "core.h"
54#include "gadget.h"
55#include "io.h"
56
Felipe Balbi04a9bfc2012-01-02 18:25:43 +020057/**
58 * dwc3_gadget_set_test_mode - Enables USB2 Test Modes
59 * @dwc: pointer to our context structure
60 * @mode: the mode to set (J, K SE0 NAK, Force Enable)
61 *
62 * Caller should take care of locking. This function will
63 * return 0 on success or -EINVAL if wrong Test Selector
64 * is passed
65 */
66int dwc3_gadget_set_test_mode(struct dwc3 *dwc, int mode)
67{
68 u32 reg;
69
70 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
71 reg &= ~DWC3_DCTL_TSTCTRL_MASK;
72
73 switch (mode) {
74 case TEST_J:
75 case TEST_K:
76 case TEST_SE0_NAK:
77 case TEST_PACKET:
78 case TEST_FORCE_EN:
79 reg |= mode << 1;
80 break;
81 default:
82 return -EINVAL;
83 }
84
85 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
86
87 return 0;
88}
89
Felipe Balbi8598bde2012-01-02 18:55:57 +020090/**
91 * dwc3_gadget_set_link_state - Sets USB Link to a particular State
92 * @dwc: pointer to our context structure
93 * @state: the state to put link into
94 *
95 * Caller should take care of locking. This function will
Paul Zimmermanaee63e32012-02-24 17:32:15 -080096 * return 0 on success or -ETIMEDOUT.
Felipe Balbi8598bde2012-01-02 18:55:57 +020097 */
98int dwc3_gadget_set_link_state(struct dwc3 *dwc, enum dwc3_link_state state)
99{
Paul Zimmermanaee63e32012-02-24 17:32:15 -0800100 int retries = 10000;
Felipe Balbi8598bde2012-01-02 18:55:57 +0200101 u32 reg;
102
103 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
104 reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK;
105
106 /* set requested state */
107 reg |= DWC3_DCTL_ULSTCHNGREQ(state);
108 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
109
110 /* wait for a change in DSTS */
111 while (--retries) {
112 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
113
Felipe Balbi8598bde2012-01-02 18:55:57 +0200114 if (DWC3_DSTS_USBLNKST(reg) == state)
115 return 0;
116
Paul Zimmermanaee63e32012-02-24 17:32:15 -0800117 udelay(5);
Felipe Balbi8598bde2012-01-02 18:55:57 +0200118 }
119
120 dev_vdbg(dwc->dev, "link state change request timed out\n");
121
122 return -ETIMEDOUT;
123}
124
Felipe Balbi457e84b2012-01-18 18:04:09 +0200125/**
126 * dwc3_gadget_resize_tx_fifos - reallocate fifo spaces for current use-case
127 * @dwc: pointer to our context structure
128 *
129 * This function will a best effort FIFO allocation in order
130 * to improve FIFO usage and throughput, while still allowing
131 * us to enable as many endpoints as possible.
132 *
133 * Keep in mind that this operation will be highly dependent
134 * on the configured size for RAM1 - which contains TxFifo -,
135 * the amount of endpoints enabled on coreConsultant tool, and
136 * the width of the Master Bus.
137 *
138 * In the ideal world, we would always be able to satisfy the
139 * following equation:
140 *
141 * ((512 + 2 * MDWIDTH-Bytes) + (Number of IN Endpoints - 1) * \
142 * (3 * (1024 + MDWIDTH-Bytes) + MDWIDTH-Bytes)) / MDWIDTH-Bytes
143 *
144 * Unfortunately, due to many variables that's not always the case.
145 */
146int dwc3_gadget_resize_tx_fifos(struct dwc3 *dwc)
147{
148 int last_fifo_depth = 0;
149 int ram1_depth;
150 int fifo_size;
151 int mdwidth;
152 int num;
153
154 if (!dwc->needs_fifo_resize)
155 return 0;
156
157 ram1_depth = DWC3_RAM1_DEPTH(dwc->hwparams.hwparams7);
158 mdwidth = DWC3_MDWIDTH(dwc->hwparams.hwparams0);
159
160 /* MDWIDTH is represented in bits, we need it in bytes */
161 mdwidth >>= 3;
162
163 /*
164 * FIXME For now we will only allocate 1 wMaxPacketSize space
165 * for each enabled endpoint, later patches will come to
166 * improve this algorithm so that we better use the internal
167 * FIFO space
168 */
169 for (num = 0; num < DWC3_ENDPOINTS_NUM; num++) {
170 struct dwc3_ep *dep = dwc->eps[num];
171 int fifo_number = dep->number >> 1;
Felipe Balbi2e81c362012-02-02 13:01:12 +0200172 int mult = 1;
Felipe Balbi457e84b2012-01-18 18:04:09 +0200173 int tmp;
174
175 if (!(dep->number & 1))
176 continue;
177
178 if (!(dep->flags & DWC3_EP_ENABLED))
179 continue;
180
Felipe Balbi2e81c362012-02-02 13:01:12 +0200181 if (usb_endpoint_xfer_bulk(dep->desc)
182 || usb_endpoint_xfer_isoc(dep->desc))
183 mult = 3;
184
185 /*
186 * REVISIT: the following assumes we will always have enough
187 * space available on the FIFO RAM for all possible use cases.
188 * Make sure that's true somehow and change FIFO allocation
189 * accordingly.
190 *
191 * If we have Bulk or Isochronous endpoints, we want
192 * them to be able to be very, very fast. So we're giving
193 * those endpoints a fifo_size which is enough for 3 full
194 * packets
195 */
196 tmp = mult * (dep->endpoint.maxpacket + mdwidth);
Felipe Balbi457e84b2012-01-18 18:04:09 +0200197 tmp += mdwidth;
198
199 fifo_size = DIV_ROUND_UP(tmp, mdwidth);
Felipe Balbi2e81c362012-02-02 13:01:12 +0200200
Felipe Balbi457e84b2012-01-18 18:04:09 +0200201 fifo_size |= (last_fifo_depth << 16);
202
203 dev_vdbg(dwc->dev, "%s: Fifo Addr %04x Size %d\n",
204 dep->name, last_fifo_depth, fifo_size & 0xffff);
205
206 dwc3_writel(dwc->regs, DWC3_GTXFIFOSIZ(fifo_number),
207 fifo_size);
208
209 last_fifo_depth += (fifo_size & 0xffff);
210 }
211
212 return 0;
213}
214
Felipe Balbi72246da2011-08-19 18:10:58 +0300215void dwc3_gadget_giveback(struct dwc3_ep *dep, struct dwc3_request *req,
216 int status)
217{
218 struct dwc3 *dwc = dep->dwc;
219
220 if (req->queued) {
Felipe Balbieeb720f2011-11-28 12:46:59 +0200221 if (req->request.num_mapped_sgs)
222 dep->busy_slot += req->request.num_mapped_sgs;
223 else
224 dep->busy_slot++;
225
Felipe Balbi72246da2011-08-19 18:10:58 +0300226 /*
227 * Skip LINK TRB. We can't use req->trb and check for
228 * DWC3_TRBCTL_LINK_TRB because it points the TRB we just
229 * completed (not the LINK TRB).
230 */
231 if (((dep->busy_slot & DWC3_TRB_MASK) == DWC3_TRB_NUM - 1) &&
232 usb_endpoint_xfer_isoc(dep->desc))
233 dep->busy_slot++;
234 }
235 list_del(&req->list);
Felipe Balbieeb720f2011-11-28 12:46:59 +0200236 req->trb = NULL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300237
238 if (req->request.status == -EINPROGRESS)
239 req->request.status = status;
240
Pratyush Anand7acab782012-08-10 13:42:16 +0530241 if (dwc->ep0_bounced && dep->number == 0)
242 dwc->ep0_bounced = false;
243 else
244 usb_gadget_unmap_request(&dwc->gadget, &req->request,
245 req->direction);
Felipe Balbi72246da2011-08-19 18:10:58 +0300246
247 dev_dbg(dwc->dev, "request %p from %s completed %d/%d ===> %d\n",
248 req, dep->name, req->request.actual,
249 req->request.length, status);
250
251 spin_unlock(&dwc->lock);
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +0200252 req->request.complete(&dep->endpoint, &req->request);
Felipe Balbi72246da2011-08-19 18:10:58 +0300253 spin_lock(&dwc->lock);
254}
255
256static const char *dwc3_gadget_ep_cmd_string(u8 cmd)
257{
258 switch (cmd) {
259 case DWC3_DEPCMD_DEPSTARTCFG:
260 return "Start New Configuration";
261 case DWC3_DEPCMD_ENDTRANSFER:
262 return "End Transfer";
263 case DWC3_DEPCMD_UPDATETRANSFER:
264 return "Update Transfer";
265 case DWC3_DEPCMD_STARTTRANSFER:
266 return "Start Transfer";
267 case DWC3_DEPCMD_CLEARSTALL:
268 return "Clear Stall";
269 case DWC3_DEPCMD_SETSTALL:
270 return "Set Stall";
271 case DWC3_DEPCMD_GETSEQNUMBER:
272 return "Get Data Sequence Number";
273 case DWC3_DEPCMD_SETTRANSFRESOURCE:
274 return "Set Endpoint Transfer Resource";
275 case DWC3_DEPCMD_SETEPCONFIG:
276 return "Set Endpoint Configuration";
277 default:
278 return "UNKNOWN command";
279 }
280}
281
282int dwc3_send_gadget_ep_cmd(struct dwc3 *dwc, unsigned ep,
283 unsigned cmd, struct dwc3_gadget_ep_cmd_params *params)
284{
285 struct dwc3_ep *dep = dwc->eps[ep];
Sebastian Andrzej Siewior61d58242011-08-29 16:46:38 +0200286 u32 timeout = 500;
Felipe Balbi72246da2011-08-19 18:10:58 +0300287 u32 reg;
288
289 dev_vdbg(dwc->dev, "%s: cmd '%s' params %08x %08x %08x\n",
290 dep->name,
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300291 dwc3_gadget_ep_cmd_string(cmd), params->param0,
292 params->param1, params->param2);
Felipe Balbi72246da2011-08-19 18:10:58 +0300293
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300294 dwc3_writel(dwc->regs, DWC3_DEPCMDPAR0(ep), params->param0);
295 dwc3_writel(dwc->regs, DWC3_DEPCMDPAR1(ep), params->param1);
296 dwc3_writel(dwc->regs, DWC3_DEPCMDPAR2(ep), params->param2);
Felipe Balbi72246da2011-08-19 18:10:58 +0300297
298 dwc3_writel(dwc->regs, DWC3_DEPCMD(ep), cmd | DWC3_DEPCMD_CMDACT);
299 do {
300 reg = dwc3_readl(dwc->regs, DWC3_DEPCMD(ep));
301 if (!(reg & DWC3_DEPCMD_CMDACT)) {
Felipe Balbi164f6e12011-08-27 20:29:58 +0300302 dev_vdbg(dwc->dev, "Command Complete --> %d\n",
303 DWC3_DEPCMD_STATUS(reg));
Felipe Balbi72246da2011-08-19 18:10:58 +0300304 return 0;
305 }
306
307 /*
Felipe Balbi72246da2011-08-19 18:10:58 +0300308 * We can't sleep here, because it is also called from
309 * interrupt context.
310 */
311 timeout--;
312 if (!timeout)
313 return -ETIMEDOUT;
314
Sebastian Andrzej Siewior61d58242011-08-29 16:46:38 +0200315 udelay(1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300316 } while (1);
317}
318
319static dma_addr_t dwc3_trb_dma_offset(struct dwc3_ep *dep,
Felipe Balbif6bafc62012-02-06 11:04:53 +0200320 struct dwc3_trb *trb)
Felipe Balbi72246da2011-08-19 18:10:58 +0300321{
Paul Zimmermanc439ef82011-09-30 10:58:45 +0300322 u32 offset = (char *) trb - (char *) dep->trb_pool;
Felipe Balbi72246da2011-08-19 18:10:58 +0300323
324 return dep->trb_pool_dma + offset;
325}
326
327static int dwc3_alloc_trb_pool(struct dwc3_ep *dep)
328{
329 struct dwc3 *dwc = dep->dwc;
330
331 if (dep->trb_pool)
332 return 0;
333
334 if (dep->number == 0 || dep->number == 1)
335 return 0;
336
337 dep->trb_pool = dma_alloc_coherent(dwc->dev,
338 sizeof(struct dwc3_trb) * DWC3_TRB_NUM,
339 &dep->trb_pool_dma, GFP_KERNEL);
340 if (!dep->trb_pool) {
341 dev_err(dep->dwc->dev, "failed to allocate trb pool for %s\n",
342 dep->name);
343 return -ENOMEM;
344 }
345
346 return 0;
347}
348
349static void dwc3_free_trb_pool(struct dwc3_ep *dep)
350{
351 struct dwc3 *dwc = dep->dwc;
352
353 dma_free_coherent(dwc->dev, sizeof(struct dwc3_trb) * DWC3_TRB_NUM,
354 dep->trb_pool, dep->trb_pool_dma);
355
356 dep->trb_pool = NULL;
357 dep->trb_pool_dma = 0;
358}
359
360static int dwc3_gadget_start_config(struct dwc3 *dwc, struct dwc3_ep *dep)
361{
362 struct dwc3_gadget_ep_cmd_params params;
363 u32 cmd;
364
365 memset(&params, 0x00, sizeof(params));
366
367 if (dep->number != 1) {
368 cmd = DWC3_DEPCMD_DEPSTARTCFG;
369 /* XferRscIdx == 0 for ep0 and 2 for the remaining */
Paul Zimmermanb23c8432011-09-30 10:58:42 +0300370 if (dep->number > 1) {
371 if (dwc->start_config_issued)
372 return 0;
373 dwc->start_config_issued = true;
Felipe Balbi72246da2011-08-19 18:10:58 +0300374 cmd |= DWC3_DEPCMD_PARAM(2);
Paul Zimmermanb23c8432011-09-30 10:58:42 +0300375 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300376
377 return dwc3_send_gadget_ep_cmd(dwc, 0, cmd, &params);
378 }
379
380 return 0;
381}
382
383static int dwc3_gadget_set_ep_config(struct dwc3 *dwc, struct dwc3_ep *dep,
Felipe Balbic90bfae2011-11-29 13:11:21 +0200384 const struct usb_endpoint_descriptor *desc,
385 const struct usb_ss_ep_comp_descriptor *comp_desc)
Felipe Balbi72246da2011-08-19 18:10:58 +0300386{
387 struct dwc3_gadget_ep_cmd_params params;
388
389 memset(&params, 0x00, sizeof(params));
390
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300391 params.param0 = DWC3_DEPCFG_EP_TYPE(usb_endpoint_type(desc))
392 | DWC3_DEPCFG_MAX_PACKET_SIZE(usb_endpoint_maxp(desc))
393 | DWC3_DEPCFG_BURST_SIZE(dep->endpoint.maxburst);
Felipe Balbi72246da2011-08-19 18:10:58 +0300394
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300395 params.param1 = DWC3_DEPCFG_XFER_COMPLETE_EN
396 | DWC3_DEPCFG_XFER_NOT_READY_EN;
Felipe Balbi72246da2011-08-19 18:10:58 +0300397
Felipe Balbi18b7ede2012-01-02 13:35:41 +0200398 if (usb_ss_max_streams(comp_desc) && usb_endpoint_xfer_bulk(desc)) {
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300399 params.param1 |= DWC3_DEPCFG_STREAM_CAPABLE
400 | DWC3_DEPCFG_STREAM_EVENT_EN;
Felipe Balbi879631a2011-09-30 10:58:47 +0300401 dep->stream_capable = true;
402 }
403
Felipe Balbi72246da2011-08-19 18:10:58 +0300404 if (usb_endpoint_xfer_isoc(desc))
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300405 params.param1 |= DWC3_DEPCFG_XFER_IN_PROGRESS_EN;
Felipe Balbi72246da2011-08-19 18:10:58 +0300406
407 /*
408 * We are doing 1:1 mapping for endpoints, meaning
409 * Physical Endpoints 2 maps to Logical Endpoint 2 and
410 * so on. We consider the direction bit as part of the physical
411 * endpoint number. So USB endpoint 0x81 is 0x03.
412 */
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300413 params.param1 |= DWC3_DEPCFG_EP_NUMBER(dep->number);
Felipe Balbi72246da2011-08-19 18:10:58 +0300414
415 /*
416 * We must use the lower 16 TX FIFOs even though
417 * HW might have more
418 */
419 if (dep->direction)
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300420 params.param0 |= DWC3_DEPCFG_FIFO_NUMBER(dep->number >> 1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300421
422 if (desc->bInterval) {
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300423 params.param1 |= DWC3_DEPCFG_BINTERVAL_M1(desc->bInterval - 1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300424 dep->interval = 1 << (desc->bInterval - 1);
425 }
426
427 return dwc3_send_gadget_ep_cmd(dwc, dep->number,
428 DWC3_DEPCMD_SETEPCONFIG, &params);
429}
430
431static int dwc3_gadget_set_xfer_resource(struct dwc3 *dwc, struct dwc3_ep *dep)
432{
433 struct dwc3_gadget_ep_cmd_params params;
434
435 memset(&params, 0x00, sizeof(params));
436
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300437 params.param0 = DWC3_DEPXFERCFG_NUM_XFER_RES(1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300438
439 return dwc3_send_gadget_ep_cmd(dwc, dep->number,
440 DWC3_DEPCMD_SETTRANSFRESOURCE, &params);
441}
442
443/**
444 * __dwc3_gadget_ep_enable - Initializes a HW endpoint
445 * @dep: endpoint to be initialized
446 * @desc: USB Endpoint Descriptor
447 *
448 * Caller should take care of locking
449 */
450static int __dwc3_gadget_ep_enable(struct dwc3_ep *dep,
Felipe Balbic90bfae2011-11-29 13:11:21 +0200451 const struct usb_endpoint_descriptor *desc,
452 const struct usb_ss_ep_comp_descriptor *comp_desc)
Felipe Balbi72246da2011-08-19 18:10:58 +0300453{
454 struct dwc3 *dwc = dep->dwc;
455 u32 reg;
456 int ret = -ENOMEM;
457
458 if (!(dep->flags & DWC3_EP_ENABLED)) {
459 ret = dwc3_gadget_start_config(dwc, dep);
460 if (ret)
461 return ret;
462 }
463
Felipe Balbic90bfae2011-11-29 13:11:21 +0200464 ret = dwc3_gadget_set_ep_config(dwc, dep, desc, comp_desc);
Felipe Balbi72246da2011-08-19 18:10:58 +0300465 if (ret)
466 return ret;
467
468 if (!(dep->flags & DWC3_EP_ENABLED)) {
Felipe Balbif6bafc62012-02-06 11:04:53 +0200469 struct dwc3_trb *trb_st_hw;
470 struct dwc3_trb *trb_link;
Felipe Balbi72246da2011-08-19 18:10:58 +0300471
472 ret = dwc3_gadget_set_xfer_resource(dwc, dep);
473 if (ret)
474 return ret;
475
476 dep->desc = desc;
Felipe Balbic90bfae2011-11-29 13:11:21 +0200477 dep->comp_desc = comp_desc;
Felipe Balbi72246da2011-08-19 18:10:58 +0300478 dep->type = usb_endpoint_type(desc);
479 dep->flags |= DWC3_EP_ENABLED;
480
481 reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
482 reg |= DWC3_DALEPENA_EP(dep->number);
483 dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
484
485 if (!usb_endpoint_xfer_isoc(desc))
486 return 0;
487
488 memset(&trb_link, 0, sizeof(trb_link));
489
Paul Zimmerman1d046792012-02-15 18:56:56 -0800490 /* Link TRB for ISOC. The HWO bit is never reset */
Felipe Balbi72246da2011-08-19 18:10:58 +0300491 trb_st_hw = &dep->trb_pool[0];
492
Felipe Balbif6bafc62012-02-06 11:04:53 +0200493 trb_link = &dep->trb_pool[DWC3_TRB_NUM - 1];
Felipe Balbi72246da2011-08-19 18:10:58 +0300494
Felipe Balbif6bafc62012-02-06 11:04:53 +0200495 trb_link->bpl = lower_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw));
496 trb_link->bph = upper_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw));
497 trb_link->ctrl |= DWC3_TRBCTL_LINK_TRB;
498 trb_link->ctrl |= DWC3_TRB_CTRL_HWO;
Felipe Balbi72246da2011-08-19 18:10:58 +0300499 }
500
501 return 0;
502}
503
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200504static void dwc3_stop_active_transfer(struct dwc3 *dwc, u32 epnum);
505static void dwc3_remove_requests(struct dwc3 *dwc, struct dwc3_ep *dep)
Felipe Balbi72246da2011-08-19 18:10:58 +0300506{
507 struct dwc3_request *req;
508
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200509 if (!list_empty(&dep->req_queued))
510 dwc3_stop_active_transfer(dwc, dep->number);
511
Felipe Balbi72246da2011-08-19 18:10:58 +0300512 while (!list_empty(&dep->request_list)) {
513 req = next_request(&dep->request_list);
514
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200515 dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
Felipe Balbi72246da2011-08-19 18:10:58 +0300516 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300517}
518
519/**
520 * __dwc3_gadget_ep_disable - Disables a HW endpoint
521 * @dep: the endpoint to disable
522 *
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200523 * This function also removes requests which are currently processed ny the
524 * hardware and those which are not yet scheduled.
525 * Caller should take care of locking.
Felipe Balbi72246da2011-08-19 18:10:58 +0300526 */
Felipe Balbi72246da2011-08-19 18:10:58 +0300527static int __dwc3_gadget_ep_disable(struct dwc3_ep *dep)
528{
529 struct dwc3 *dwc = dep->dwc;
530 u32 reg;
531
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200532 dwc3_remove_requests(dwc, dep);
Felipe Balbi72246da2011-08-19 18:10:58 +0300533
534 reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
535 reg &= ~DWC3_DALEPENA_EP(dep->number);
536 dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
537
Felipe Balbi879631a2011-09-30 10:58:47 +0300538 dep->stream_capable = false;
Felipe Balbi72246da2011-08-19 18:10:58 +0300539 dep->desc = NULL;
Ido Shayevitzf9c56cd2012-02-08 13:56:48 +0200540 dep->endpoint.desc = NULL;
Felipe Balbic90bfae2011-11-29 13:11:21 +0200541 dep->comp_desc = NULL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300542 dep->type = 0;
Felipe Balbi879631a2011-09-30 10:58:47 +0300543 dep->flags = 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300544
545 return 0;
546}
547
548/* -------------------------------------------------------------------------- */
549
550static int dwc3_gadget_ep0_enable(struct usb_ep *ep,
551 const struct usb_endpoint_descriptor *desc)
552{
553 return -EINVAL;
554}
555
556static int dwc3_gadget_ep0_disable(struct usb_ep *ep)
557{
558 return -EINVAL;
559}
560
561/* -------------------------------------------------------------------------- */
562
563static int dwc3_gadget_ep_enable(struct usb_ep *ep,
564 const struct usb_endpoint_descriptor *desc)
565{
566 struct dwc3_ep *dep;
567 struct dwc3 *dwc;
568 unsigned long flags;
569 int ret;
570
571 if (!ep || !desc || desc->bDescriptorType != USB_DT_ENDPOINT) {
572 pr_debug("dwc3: invalid parameters\n");
573 return -EINVAL;
574 }
575
576 if (!desc->wMaxPacketSize) {
577 pr_debug("dwc3: missing wMaxPacketSize\n");
578 return -EINVAL;
579 }
580
581 dep = to_dwc3_ep(ep);
582 dwc = dep->dwc;
583
584 switch (usb_endpoint_type(desc)) {
585 case USB_ENDPOINT_XFER_CONTROL:
Anton Tikhomirov27a78d62012-02-23 15:38:46 +0900586 strlcat(dep->name, "-control", sizeof(dep->name));
Felipe Balbi72246da2011-08-19 18:10:58 +0300587 break;
588 case USB_ENDPOINT_XFER_ISOC:
Anton Tikhomirov27a78d62012-02-23 15:38:46 +0900589 strlcat(dep->name, "-isoc", sizeof(dep->name));
Felipe Balbi72246da2011-08-19 18:10:58 +0300590 break;
591 case USB_ENDPOINT_XFER_BULK:
Anton Tikhomirov27a78d62012-02-23 15:38:46 +0900592 strlcat(dep->name, "-bulk", sizeof(dep->name));
Felipe Balbi72246da2011-08-19 18:10:58 +0300593 break;
594 case USB_ENDPOINT_XFER_INT:
Anton Tikhomirov27a78d62012-02-23 15:38:46 +0900595 strlcat(dep->name, "-int", sizeof(dep->name));
Felipe Balbi72246da2011-08-19 18:10:58 +0300596 break;
597 default:
598 dev_err(dwc->dev, "invalid endpoint transfer type\n");
599 }
600
601 if (dep->flags & DWC3_EP_ENABLED) {
602 dev_WARN_ONCE(dwc->dev, true, "%s is already enabled\n",
603 dep->name);
604 return 0;
605 }
606
607 dev_vdbg(dwc->dev, "Enabling %s\n", dep->name);
608
609 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbic90bfae2011-11-29 13:11:21 +0200610 ret = __dwc3_gadget_ep_enable(dep, desc, ep->comp_desc);
Felipe Balbi72246da2011-08-19 18:10:58 +0300611 spin_unlock_irqrestore(&dwc->lock, flags);
612
613 return ret;
614}
615
616static int dwc3_gadget_ep_disable(struct usb_ep *ep)
617{
618 struct dwc3_ep *dep;
619 struct dwc3 *dwc;
620 unsigned long flags;
621 int ret;
622
623 if (!ep) {
624 pr_debug("dwc3: invalid parameters\n");
625 return -EINVAL;
626 }
627
628 dep = to_dwc3_ep(ep);
629 dwc = dep->dwc;
630
631 if (!(dep->flags & DWC3_EP_ENABLED)) {
632 dev_WARN_ONCE(dwc->dev, true, "%s is already disabled\n",
633 dep->name);
634 return 0;
635 }
636
637 snprintf(dep->name, sizeof(dep->name), "ep%d%s",
638 dep->number >> 1,
639 (dep->number & 1) ? "in" : "out");
640
641 spin_lock_irqsave(&dwc->lock, flags);
642 ret = __dwc3_gadget_ep_disable(dep);
643 spin_unlock_irqrestore(&dwc->lock, flags);
644
645 return ret;
646}
647
648static struct usb_request *dwc3_gadget_ep_alloc_request(struct usb_ep *ep,
649 gfp_t gfp_flags)
650{
651 struct dwc3_request *req;
652 struct dwc3_ep *dep = to_dwc3_ep(ep);
653 struct dwc3 *dwc = dep->dwc;
654
655 req = kzalloc(sizeof(*req), gfp_flags);
656 if (!req) {
657 dev_err(dwc->dev, "not enough memory\n");
658 return NULL;
659 }
660
661 req->epnum = dep->number;
662 req->dep = dep;
Felipe Balbi72246da2011-08-19 18:10:58 +0300663
664 return &req->request;
665}
666
667static void dwc3_gadget_ep_free_request(struct usb_ep *ep,
668 struct usb_request *request)
669{
670 struct dwc3_request *req = to_dwc3_request(request);
671
672 kfree(req);
673}
674
Felipe Balbic71fc372011-11-22 11:37:34 +0200675/**
676 * dwc3_prepare_one_trb - setup one TRB from one request
677 * @dep: endpoint for which this request is prepared
678 * @req: dwc3_request pointer
679 */
Felipe Balbi68e823e2011-11-28 12:25:01 +0200680static void dwc3_prepare_one_trb(struct dwc3_ep *dep,
Felipe Balbieeb720f2011-11-28 12:46:59 +0200681 struct dwc3_request *req, dma_addr_t dma,
682 unsigned length, unsigned last, unsigned chain)
Felipe Balbic71fc372011-11-22 11:37:34 +0200683{
Felipe Balbieeb720f2011-11-28 12:46:59 +0200684 struct dwc3 *dwc = dep->dwc;
Felipe Balbif6bafc62012-02-06 11:04:53 +0200685 struct dwc3_trb *trb;
Felipe Balbic71fc372011-11-22 11:37:34 +0200686
687 unsigned int cur_slot;
688
Felipe Balbieeb720f2011-11-28 12:46:59 +0200689 dev_vdbg(dwc->dev, "%s: req %p dma %08llx length %d%s%s\n",
690 dep->name, req, (unsigned long long) dma,
691 length, last ? " last" : "",
692 chain ? " chain" : "");
693
Felipe Balbif6bafc62012-02-06 11:04:53 +0200694 trb = &dep->trb_pool[dep->free_slot & DWC3_TRB_MASK];
Felipe Balbic71fc372011-11-22 11:37:34 +0200695 cur_slot = dep->free_slot;
696 dep->free_slot++;
697
698 /* Skip the LINK-TRB on ISOC */
699 if (((cur_slot & DWC3_TRB_MASK) == DWC3_TRB_NUM - 1) &&
700 usb_endpoint_xfer_isoc(dep->desc))
Felipe Balbi68e823e2011-11-28 12:25:01 +0200701 return;
Felipe Balbic71fc372011-11-22 11:37:34 +0200702
Felipe Balbieeb720f2011-11-28 12:46:59 +0200703 if (!req->trb) {
704 dwc3_gadget_move_request_queued(req);
Felipe Balbif6bafc62012-02-06 11:04:53 +0200705 req->trb = trb;
706 req->trb_dma = dwc3_trb_dma_offset(dep, trb);
Felipe Balbieeb720f2011-11-28 12:46:59 +0200707 }
Felipe Balbic71fc372011-11-22 11:37:34 +0200708
Felipe Balbif6bafc62012-02-06 11:04:53 +0200709 trb->size = DWC3_TRB_SIZE_LENGTH(length);
710 trb->bpl = lower_32_bits(dma);
711 trb->bph = upper_32_bits(dma);
Felipe Balbic71fc372011-11-22 11:37:34 +0200712
713 switch (usb_endpoint_type(dep->desc)) {
714 case USB_ENDPOINT_XFER_CONTROL:
Felipe Balbif6bafc62012-02-06 11:04:53 +0200715 trb->ctrl = DWC3_TRBCTL_CONTROL_SETUP;
Felipe Balbic71fc372011-11-22 11:37:34 +0200716 break;
717
718 case USB_ENDPOINT_XFER_ISOC:
Felipe Balbif6bafc62012-02-06 11:04:53 +0200719 trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS_FIRST;
Felipe Balbic71fc372011-11-22 11:37:34 +0200720
721 /* IOC every DWC3_TRB_NUM / 4 so we can refill */
722 if (!(cur_slot % (DWC3_TRB_NUM / 4)))
Felipe Balbif6bafc62012-02-06 11:04:53 +0200723 trb->ctrl |= DWC3_TRB_CTRL_IOC;
Felipe Balbic71fc372011-11-22 11:37:34 +0200724 break;
725
726 case USB_ENDPOINT_XFER_BULK:
727 case USB_ENDPOINT_XFER_INT:
Felipe Balbif6bafc62012-02-06 11:04:53 +0200728 trb->ctrl = DWC3_TRBCTL_NORMAL;
Felipe Balbic71fc372011-11-22 11:37:34 +0200729 break;
730 default:
731 /*
732 * This is only possible with faulty memory because we
733 * checked it already :)
734 */
735 BUG();
736 }
737
Felipe Balbif6bafc62012-02-06 11:04:53 +0200738 if (usb_endpoint_xfer_isoc(dep->desc)) {
739 trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI;
740 trb->ctrl |= DWC3_TRB_CTRL_CSP;
741 } else {
742 if (chain)
743 trb->ctrl |= DWC3_TRB_CTRL_CHN;
Felipe Balbic71fc372011-11-22 11:37:34 +0200744
Felipe Balbif6bafc62012-02-06 11:04:53 +0200745 if (last)
746 trb->ctrl |= DWC3_TRB_CTRL_LST;
747 }
748
749 if (usb_endpoint_xfer_bulk(dep->desc) && dep->stream_capable)
750 trb->ctrl |= DWC3_TRB_CTRL_SID_SOFN(req->request.stream_id);
751
752 trb->ctrl |= DWC3_TRB_CTRL_HWO;
Felipe Balbic71fc372011-11-22 11:37:34 +0200753}
754
Felipe Balbi72246da2011-08-19 18:10:58 +0300755/*
756 * dwc3_prepare_trbs - setup TRBs from requests
757 * @dep: endpoint for which requests are being prepared
758 * @starting: true if the endpoint is idle and no requests are queued.
759 *
Paul Zimmerman1d046792012-02-15 18:56:56 -0800760 * The function goes through the requests list and sets up TRBs for the
761 * transfers. The function returns once there are no more TRBs available or
762 * it runs out of requests.
Felipe Balbi72246da2011-08-19 18:10:58 +0300763 */
Felipe Balbi68e823e2011-11-28 12:25:01 +0200764static void dwc3_prepare_trbs(struct dwc3_ep *dep, bool starting)
Felipe Balbi72246da2011-08-19 18:10:58 +0300765{
Felipe Balbi68e823e2011-11-28 12:25:01 +0200766 struct dwc3_request *req, *n;
Felipe Balbi72246da2011-08-19 18:10:58 +0300767 u32 trbs_left;
Paul Zimmerman8d62cd62012-02-15 13:35:06 +0200768 u32 max;
Felipe Balbic71fc372011-11-22 11:37:34 +0200769 unsigned int last_one = 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300770
771 BUILD_BUG_ON_NOT_POWER_OF_2(DWC3_TRB_NUM);
772
773 /* the first request must not be queued */
774 trbs_left = (dep->busy_slot - dep->free_slot) & DWC3_TRB_MASK;
Felipe Balbic71fc372011-11-22 11:37:34 +0200775
Paul Zimmerman8d62cd62012-02-15 13:35:06 +0200776 /* Can't wrap around on a non-isoc EP since there's no link TRB */
777 if (!usb_endpoint_xfer_isoc(dep->desc)) {
778 max = DWC3_TRB_NUM - (dep->free_slot & DWC3_TRB_MASK);
779 if (trbs_left > max)
780 trbs_left = max;
781 }
782
Felipe Balbi72246da2011-08-19 18:10:58 +0300783 /*
Paul Zimmerman1d046792012-02-15 18:56:56 -0800784 * If busy & slot are equal than it is either full or empty. If we are
785 * starting to process requests then we are empty. Otherwise we are
Felipe Balbi72246da2011-08-19 18:10:58 +0300786 * full and don't do anything
787 */
788 if (!trbs_left) {
789 if (!starting)
Felipe Balbi68e823e2011-11-28 12:25:01 +0200790 return;
Felipe Balbi72246da2011-08-19 18:10:58 +0300791 trbs_left = DWC3_TRB_NUM;
792 /*
793 * In case we start from scratch, we queue the ISOC requests
794 * starting from slot 1. This is done because we use ring
795 * buffer and have no LST bit to stop us. Instead, we place
Paul Zimmerman1d046792012-02-15 18:56:56 -0800796 * IOC bit every TRB_NUM/4. We try to avoid having an interrupt
Felipe Balbi72246da2011-08-19 18:10:58 +0300797 * after the first request so we start at slot 1 and have
798 * 7 requests proceed before we hit the first IOC.
799 * Other transfer types don't use the ring buffer and are
800 * processed from the first TRB until the last one. Since we
801 * don't wrap around we have to start at the beginning.
802 */
803 if (usb_endpoint_xfer_isoc(dep->desc)) {
804 dep->busy_slot = 1;
805 dep->free_slot = 1;
806 } else {
807 dep->busy_slot = 0;
808 dep->free_slot = 0;
809 }
810 }
811
812 /* The last TRB is a link TRB, not used for xfer */
813 if ((trbs_left <= 1) && usb_endpoint_xfer_isoc(dep->desc))
Felipe Balbi68e823e2011-11-28 12:25:01 +0200814 return;
Felipe Balbi72246da2011-08-19 18:10:58 +0300815
816 list_for_each_entry_safe(req, n, &dep->request_list, list) {
Felipe Balbieeb720f2011-11-28 12:46:59 +0200817 unsigned length;
818 dma_addr_t dma;
Felipe Balbi72246da2011-08-19 18:10:58 +0300819
Felipe Balbieeb720f2011-11-28 12:46:59 +0200820 if (req->request.num_mapped_sgs > 0) {
821 struct usb_request *request = &req->request;
822 struct scatterlist *sg = request->sg;
823 struct scatterlist *s;
824 int i;
Felipe Balbi72246da2011-08-19 18:10:58 +0300825
Felipe Balbieeb720f2011-11-28 12:46:59 +0200826 for_each_sg(sg, s, request->num_mapped_sgs, i) {
827 unsigned chain = true;
Felipe Balbi72246da2011-08-19 18:10:58 +0300828
Felipe Balbieeb720f2011-11-28 12:46:59 +0200829 length = sg_dma_len(s);
830 dma = sg_dma_address(s);
Felipe Balbi72246da2011-08-19 18:10:58 +0300831
Paul Zimmerman1d046792012-02-15 18:56:56 -0800832 if (i == (request->num_mapped_sgs - 1) ||
833 sg_is_last(s)) {
Felipe Balbieeb720f2011-11-28 12:46:59 +0200834 last_one = true;
835 chain = false;
836 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300837
Felipe Balbieeb720f2011-11-28 12:46:59 +0200838 trbs_left--;
839 if (!trbs_left)
840 last_one = true;
Felipe Balbi72246da2011-08-19 18:10:58 +0300841
Felipe Balbieeb720f2011-11-28 12:46:59 +0200842 if (last_one)
843 chain = false;
Felipe Balbi72246da2011-08-19 18:10:58 +0300844
Felipe Balbieeb720f2011-11-28 12:46:59 +0200845 dwc3_prepare_one_trb(dep, req, dma, length,
846 last_one, chain);
Felipe Balbi72246da2011-08-19 18:10:58 +0300847
Felipe Balbieeb720f2011-11-28 12:46:59 +0200848 if (last_one)
849 break;
850 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300851 } else {
Felipe Balbieeb720f2011-11-28 12:46:59 +0200852 dma = req->request.dma;
853 length = req->request.length;
854 trbs_left--;
855
856 if (!trbs_left)
857 last_one = 1;
858
859 /* Is this the last request? */
860 if (list_is_last(&req->list, &dep->request_list))
861 last_one = 1;
862
863 dwc3_prepare_one_trb(dep, req, dma, length,
864 last_one, false);
865
866 if (last_one)
867 break;
Felipe Balbi72246da2011-08-19 18:10:58 +0300868 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300869 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300870}
871
872static int __dwc3_gadget_kick_transfer(struct dwc3_ep *dep, u16 cmd_param,
873 int start_new)
874{
875 struct dwc3_gadget_ep_cmd_params params;
876 struct dwc3_request *req;
877 struct dwc3 *dwc = dep->dwc;
878 int ret;
879 u32 cmd;
880
881 if (start_new && (dep->flags & DWC3_EP_BUSY)) {
882 dev_vdbg(dwc->dev, "%s: endpoint busy\n", dep->name);
883 return -EBUSY;
884 }
885 dep->flags &= ~DWC3_EP_PENDING_REQUEST;
886
887 /*
888 * If we are getting here after a short-out-packet we don't enqueue any
889 * new requests as we try to set the IOC bit only on the last request.
890 */
891 if (start_new) {
892 if (list_empty(&dep->req_queued))
893 dwc3_prepare_trbs(dep, start_new);
894
895 /* req points to the first request which will be sent */
896 req = next_request(&dep->req_queued);
897 } else {
Felipe Balbi68e823e2011-11-28 12:25:01 +0200898 dwc3_prepare_trbs(dep, start_new);
899
Felipe Balbi72246da2011-08-19 18:10:58 +0300900 /*
Paul Zimmerman1d046792012-02-15 18:56:56 -0800901 * req points to the first request where HWO changed from 0 to 1
Felipe Balbi72246da2011-08-19 18:10:58 +0300902 */
Felipe Balbi68e823e2011-11-28 12:25:01 +0200903 req = next_request(&dep->req_queued);
Felipe Balbi72246da2011-08-19 18:10:58 +0300904 }
905 if (!req) {
906 dep->flags |= DWC3_EP_PENDING_REQUEST;
907 return 0;
908 }
909
910 memset(&params, 0, sizeof(params));
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300911 params.param0 = upper_32_bits(req->trb_dma);
912 params.param1 = lower_32_bits(req->trb_dma);
Felipe Balbi72246da2011-08-19 18:10:58 +0300913
914 if (start_new)
915 cmd = DWC3_DEPCMD_STARTTRANSFER;
916 else
917 cmd = DWC3_DEPCMD_UPDATETRANSFER;
918
919 cmd |= DWC3_DEPCMD_PARAM(cmd_param);
920 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number, cmd, &params);
921 if (ret < 0) {
922 dev_dbg(dwc->dev, "failed to send STARTTRANSFER command\n");
923
924 /*
925 * FIXME we need to iterate over the list of requests
926 * here and stop, unmap, free and del each of the linked
Paul Zimmerman1d046792012-02-15 18:56:56 -0800927 * requests instead of what we do now.
Felipe Balbi72246da2011-08-19 18:10:58 +0300928 */
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +0200929 usb_gadget_unmap_request(&dwc->gadget, &req->request,
930 req->direction);
Felipe Balbi72246da2011-08-19 18:10:58 +0300931 list_del(&req->list);
932 return ret;
933 }
934
935 dep->flags |= DWC3_EP_BUSY;
936 dep->res_trans_idx = dwc3_gadget_ep_get_transfer_index(dwc,
937 dep->number);
Felipe Balbi25b8ff62011-11-04 12:32:47 +0200938
939 WARN_ON_ONCE(!dep->res_trans_idx);
940
Felipe Balbi72246da2011-08-19 18:10:58 +0300941 return 0;
942}
943
944static int __dwc3_gadget_ep_queue(struct dwc3_ep *dep, struct dwc3_request *req)
945{
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +0200946 struct dwc3 *dwc = dep->dwc;
947 int ret;
948
Felipe Balbi72246da2011-08-19 18:10:58 +0300949 req->request.actual = 0;
950 req->request.status = -EINPROGRESS;
951 req->direction = dep->direction;
952 req->epnum = dep->number;
953
954 /*
955 * We only add to our list of requests now and
956 * start consuming the list once we get XferNotReady
957 * IRQ.
958 *
959 * That way, we avoid doing anything that we don't need
960 * to do now and defer it until the point we receive a
961 * particular token from the Host side.
962 *
963 * This will also avoid Host cancelling URBs due to too
Paul Zimmerman1d046792012-02-15 18:56:56 -0800964 * many NAKs.
Felipe Balbi72246da2011-08-19 18:10:58 +0300965 */
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +0200966 ret = usb_gadget_map_request(&dwc->gadget, &req->request,
967 dep->direction);
968 if (ret)
969 return ret;
970
Felipe Balbi72246da2011-08-19 18:10:58 +0300971 list_add_tail(&req->list, &dep->request_list);
972
973 /*
974 * There is one special case: XferNotReady with
975 * empty list of requests. We need to kick the
976 * transfer here in that situation, otherwise
977 * we will be NAKing forever.
978 *
979 * If we get XferNotReady before gadget driver
980 * has a chance to queue a request, we will ACK
981 * the IRQ but won't be able to receive the data
982 * until the next request is queued. The following
983 * code is handling exactly that.
984 */
985 if (dep->flags & DWC3_EP_PENDING_REQUEST) {
986 int ret;
987 int start_trans;
988
989 start_trans = 1;
Felipe Balbi7b7dd022012-01-18 17:09:17 +0200990 if (usb_endpoint_xfer_isoc(dep->desc) &&
Paul Zimmerman1d046792012-02-15 18:56:56 -0800991 (dep->flags & DWC3_EP_BUSY))
Felipe Balbi72246da2011-08-19 18:10:58 +0300992 start_trans = 0;
993
Paul Zimmerman1d046792012-02-15 18:56:56 -0800994 ret = __dwc3_gadget_kick_transfer(dep, 0, start_trans);
Felipe Balbi72246da2011-08-19 18:10:58 +0300995 if (ret && ret != -EBUSY) {
996 struct dwc3 *dwc = dep->dwc;
997
998 dev_dbg(dwc->dev, "%s: failed to kick transfers\n",
999 dep->name);
1000 }
1001 };
1002
1003 return 0;
1004}
1005
1006static int dwc3_gadget_ep_queue(struct usb_ep *ep, struct usb_request *request,
1007 gfp_t gfp_flags)
1008{
1009 struct dwc3_request *req = to_dwc3_request(request);
1010 struct dwc3_ep *dep = to_dwc3_ep(ep);
1011 struct dwc3 *dwc = dep->dwc;
1012
1013 unsigned long flags;
1014
1015 int ret;
1016
1017 if (!dep->desc) {
1018 dev_dbg(dwc->dev, "trying to queue request %p to disabled %s\n",
1019 request, ep->name);
1020 return -ESHUTDOWN;
1021 }
1022
1023 dev_vdbg(dwc->dev, "queing request %p to %s length %d\n",
1024 request, ep->name, request->length);
1025
1026 spin_lock_irqsave(&dwc->lock, flags);
1027 ret = __dwc3_gadget_ep_queue(dep, req);
1028 spin_unlock_irqrestore(&dwc->lock, flags);
1029
1030 return ret;
1031}
1032
1033static int dwc3_gadget_ep_dequeue(struct usb_ep *ep,
1034 struct usb_request *request)
1035{
1036 struct dwc3_request *req = to_dwc3_request(request);
1037 struct dwc3_request *r = NULL;
1038
1039 struct dwc3_ep *dep = to_dwc3_ep(ep);
1040 struct dwc3 *dwc = dep->dwc;
1041
1042 unsigned long flags;
1043 int ret = 0;
1044
1045 spin_lock_irqsave(&dwc->lock, flags);
1046
1047 list_for_each_entry(r, &dep->request_list, list) {
1048 if (r == req)
1049 break;
1050 }
1051
1052 if (r != req) {
1053 list_for_each_entry(r, &dep->req_queued, list) {
1054 if (r == req)
1055 break;
1056 }
1057 if (r == req) {
1058 /* wait until it is processed */
1059 dwc3_stop_active_transfer(dwc, dep->number);
1060 goto out0;
1061 }
1062 dev_err(dwc->dev, "request %p was not queued to %s\n",
1063 request, ep->name);
1064 ret = -EINVAL;
1065 goto out0;
1066 }
1067
1068 /* giveback the request */
1069 dwc3_gadget_giveback(dep, req, -ECONNRESET);
1070
1071out0:
1072 spin_unlock_irqrestore(&dwc->lock, flags);
1073
1074 return ret;
1075}
1076
1077int __dwc3_gadget_ep_set_halt(struct dwc3_ep *dep, int value)
1078{
1079 struct dwc3_gadget_ep_cmd_params params;
1080 struct dwc3 *dwc = dep->dwc;
1081 int ret;
1082
1083 memset(&params, 0x00, sizeof(params));
1084
1085 if (value) {
Felipe Balbi0b7836a2011-08-30 15:48:08 +03001086 if (dep->number == 0 || dep->number == 1) {
1087 /*
1088 * Whenever EP0 is stalled, we will restart
1089 * the state machine, thus moving back to
1090 * Setup Phase
1091 */
1092 dwc->ep0state = EP0_SETUP_PHASE;
1093 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001094
1095 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number,
1096 DWC3_DEPCMD_SETSTALL, &params);
1097 if (ret)
1098 dev_err(dwc->dev, "failed to %s STALL on %s\n",
1099 value ? "set" : "clear",
1100 dep->name);
1101 else
1102 dep->flags |= DWC3_EP_STALL;
1103 } else {
Paul Zimmerman52754552011-09-30 10:58:44 +03001104 if (dep->flags & DWC3_EP_WEDGE)
1105 return 0;
1106
Felipe Balbi72246da2011-08-19 18:10:58 +03001107 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number,
1108 DWC3_DEPCMD_CLEARSTALL, &params);
1109 if (ret)
1110 dev_err(dwc->dev, "failed to %s STALL on %s\n",
1111 value ? "set" : "clear",
1112 dep->name);
1113 else
1114 dep->flags &= ~DWC3_EP_STALL;
1115 }
Paul Zimmerman52754552011-09-30 10:58:44 +03001116
Felipe Balbi72246da2011-08-19 18:10:58 +03001117 return ret;
1118}
1119
1120static int dwc3_gadget_ep_set_halt(struct usb_ep *ep, int value)
1121{
1122 struct dwc3_ep *dep = to_dwc3_ep(ep);
1123 struct dwc3 *dwc = dep->dwc;
1124
1125 unsigned long flags;
1126
1127 int ret;
1128
1129 spin_lock_irqsave(&dwc->lock, flags);
1130
1131 if (usb_endpoint_xfer_isoc(dep->desc)) {
1132 dev_err(dwc->dev, "%s is of Isochronous type\n", dep->name);
1133 ret = -EINVAL;
1134 goto out;
1135 }
1136
1137 ret = __dwc3_gadget_ep_set_halt(dep, value);
1138out:
1139 spin_unlock_irqrestore(&dwc->lock, flags);
1140
1141 return ret;
1142}
1143
1144static int dwc3_gadget_ep_set_wedge(struct usb_ep *ep)
1145{
1146 struct dwc3_ep *dep = to_dwc3_ep(ep);
Paul Zimmerman249a4562012-02-24 17:32:16 -08001147 struct dwc3 *dwc = dep->dwc;
1148 unsigned long flags;
Felipe Balbi72246da2011-08-19 18:10:58 +03001149
Paul Zimmerman249a4562012-02-24 17:32:16 -08001150 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001151 dep->flags |= DWC3_EP_WEDGE;
Paul Zimmerman249a4562012-02-24 17:32:16 -08001152 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001153
Paul Zimmerman52754552011-09-30 10:58:44 +03001154 return dwc3_gadget_ep_set_halt(ep, 1);
Felipe Balbi72246da2011-08-19 18:10:58 +03001155}
1156
1157/* -------------------------------------------------------------------------- */
1158
1159static struct usb_endpoint_descriptor dwc3_gadget_ep0_desc = {
1160 .bLength = USB_DT_ENDPOINT_SIZE,
1161 .bDescriptorType = USB_DT_ENDPOINT,
1162 .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
1163};
1164
1165static const struct usb_ep_ops dwc3_gadget_ep0_ops = {
1166 .enable = dwc3_gadget_ep0_enable,
1167 .disable = dwc3_gadget_ep0_disable,
1168 .alloc_request = dwc3_gadget_ep_alloc_request,
1169 .free_request = dwc3_gadget_ep_free_request,
1170 .queue = dwc3_gadget_ep0_queue,
1171 .dequeue = dwc3_gadget_ep_dequeue,
1172 .set_halt = dwc3_gadget_ep_set_halt,
1173 .set_wedge = dwc3_gadget_ep_set_wedge,
1174};
1175
1176static const struct usb_ep_ops dwc3_gadget_ep_ops = {
1177 .enable = dwc3_gadget_ep_enable,
1178 .disable = dwc3_gadget_ep_disable,
1179 .alloc_request = dwc3_gadget_ep_alloc_request,
1180 .free_request = dwc3_gadget_ep_free_request,
1181 .queue = dwc3_gadget_ep_queue,
1182 .dequeue = dwc3_gadget_ep_dequeue,
1183 .set_halt = dwc3_gadget_ep_set_halt,
1184 .set_wedge = dwc3_gadget_ep_set_wedge,
1185};
1186
1187/* -------------------------------------------------------------------------- */
1188
1189static int dwc3_gadget_get_frame(struct usb_gadget *g)
1190{
1191 struct dwc3 *dwc = gadget_to_dwc(g);
1192 u32 reg;
1193
1194 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1195 return DWC3_DSTS_SOFFN(reg);
1196}
1197
1198static int dwc3_gadget_wakeup(struct usb_gadget *g)
1199{
1200 struct dwc3 *dwc = gadget_to_dwc(g);
1201
1202 unsigned long timeout;
1203 unsigned long flags;
1204
1205 u32 reg;
1206
1207 int ret = 0;
1208
1209 u8 link_state;
1210 u8 speed;
1211
1212 spin_lock_irqsave(&dwc->lock, flags);
1213
1214 /*
1215 * According to the Databook Remote wakeup request should
1216 * be issued only when the device is in early suspend state.
1217 *
1218 * We can check that via USB Link State bits in DSTS register.
1219 */
1220 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1221
1222 speed = reg & DWC3_DSTS_CONNECTSPD;
1223 if (speed == DWC3_DSTS_SUPERSPEED) {
1224 dev_dbg(dwc->dev, "no wakeup on SuperSpeed\n");
1225 ret = -EINVAL;
1226 goto out;
1227 }
1228
1229 link_state = DWC3_DSTS_USBLNKST(reg);
1230
1231 switch (link_state) {
1232 case DWC3_LINK_STATE_RX_DET: /* in HS, means Early Suspend */
1233 case DWC3_LINK_STATE_U3: /* in HS, means SUSPEND */
1234 break;
1235 default:
1236 dev_dbg(dwc->dev, "can't wakeup from link state %d\n",
1237 link_state);
1238 ret = -EINVAL;
1239 goto out;
1240 }
1241
Felipe Balbi8598bde2012-01-02 18:55:57 +02001242 ret = dwc3_gadget_set_link_state(dwc, DWC3_LINK_STATE_RECOV);
1243 if (ret < 0) {
1244 dev_err(dwc->dev, "failed to put link in Recovery\n");
1245 goto out;
1246 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001247
1248 /* write zeroes to Link Change Request */
1249 reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK;
1250 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1251
Paul Zimmerman1d046792012-02-15 18:56:56 -08001252 /* poll until Link State changes to ON */
Felipe Balbi72246da2011-08-19 18:10:58 +03001253 timeout = jiffies + msecs_to_jiffies(100);
1254
Paul Zimmerman1d046792012-02-15 18:56:56 -08001255 while (!time_after(jiffies, timeout)) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001256 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1257
1258 /* in HS, means ON */
1259 if (DWC3_DSTS_USBLNKST(reg) == DWC3_LINK_STATE_U0)
1260 break;
1261 }
1262
1263 if (DWC3_DSTS_USBLNKST(reg) != DWC3_LINK_STATE_U0) {
1264 dev_err(dwc->dev, "failed to send remote wakeup\n");
1265 ret = -EINVAL;
1266 }
1267
1268out:
1269 spin_unlock_irqrestore(&dwc->lock, flags);
1270
1271 return ret;
1272}
1273
1274static int dwc3_gadget_set_selfpowered(struct usb_gadget *g,
1275 int is_selfpowered)
1276{
1277 struct dwc3 *dwc = gadget_to_dwc(g);
Paul Zimmerman249a4562012-02-24 17:32:16 -08001278 unsigned long flags;
Felipe Balbi72246da2011-08-19 18:10:58 +03001279
Paul Zimmerman249a4562012-02-24 17:32:16 -08001280 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001281 dwc->is_selfpowered = !!is_selfpowered;
Paul Zimmerman249a4562012-02-24 17:32:16 -08001282 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001283
1284 return 0;
1285}
1286
1287static void dwc3_gadget_run_stop(struct dwc3 *dwc, int is_on)
1288{
1289 u32 reg;
Sebastian Andrzej Siewior61d58242011-08-29 16:46:38 +02001290 u32 timeout = 500;
Felipe Balbi72246da2011-08-19 18:10:58 +03001291
1292 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
Felipe Balbi8db7ed12012-01-18 18:32:29 +02001293 if (is_on) {
1294 reg &= ~DWC3_DCTL_TRGTULST_MASK;
1295 reg |= (DWC3_DCTL_RUN_STOP
1296 | DWC3_DCTL_TRGTULST_RX_DET);
1297 } else {
Felipe Balbi72246da2011-08-19 18:10:58 +03001298 reg &= ~DWC3_DCTL_RUN_STOP;
Felipe Balbi8db7ed12012-01-18 18:32:29 +02001299 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001300
1301 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1302
1303 do {
1304 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1305 if (is_on) {
1306 if (!(reg & DWC3_DSTS_DEVCTRLHLT))
1307 break;
1308 } else {
1309 if (reg & DWC3_DSTS_DEVCTRLHLT)
1310 break;
1311 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001312 timeout--;
1313 if (!timeout)
1314 break;
Sebastian Andrzej Siewior61d58242011-08-29 16:46:38 +02001315 udelay(1);
Felipe Balbi72246da2011-08-19 18:10:58 +03001316 } while (1);
1317
1318 dev_vdbg(dwc->dev, "gadget %s data soft-%s\n",
1319 dwc->gadget_driver
1320 ? dwc->gadget_driver->function : "no-function",
1321 is_on ? "connect" : "disconnect");
1322}
1323
1324static int dwc3_gadget_pullup(struct usb_gadget *g, int is_on)
1325{
1326 struct dwc3 *dwc = gadget_to_dwc(g);
1327 unsigned long flags;
1328
1329 is_on = !!is_on;
1330
1331 spin_lock_irqsave(&dwc->lock, flags);
1332 dwc3_gadget_run_stop(dwc, is_on);
1333 spin_unlock_irqrestore(&dwc->lock, flags);
1334
1335 return 0;
1336}
1337
1338static int dwc3_gadget_start(struct usb_gadget *g,
1339 struct usb_gadget_driver *driver)
1340{
1341 struct dwc3 *dwc = gadget_to_dwc(g);
1342 struct dwc3_ep *dep;
1343 unsigned long flags;
1344 int ret = 0;
1345 u32 reg;
1346
1347 spin_lock_irqsave(&dwc->lock, flags);
1348
1349 if (dwc->gadget_driver) {
1350 dev_err(dwc->dev, "%s is already bound to %s\n",
1351 dwc->gadget.name,
1352 dwc->gadget_driver->driver.name);
1353 ret = -EBUSY;
1354 goto err0;
1355 }
1356
1357 dwc->gadget_driver = driver;
1358 dwc->gadget.dev.driver = &driver->driver;
1359
Felipe Balbi72246da2011-08-19 18:10:58 +03001360 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
1361 reg &= ~(DWC3_DCFG_SPEED_MASK);
Felipe Balbi6c167fc2011-10-07 22:55:04 +03001362 reg |= dwc->maximum_speed;
Felipe Balbi72246da2011-08-19 18:10:58 +03001363 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
1364
Paul Zimmermanb23c8432011-09-30 10:58:42 +03001365 dwc->start_config_issued = false;
1366
Felipe Balbi72246da2011-08-19 18:10:58 +03001367 /* Start with SuperSpeed Default */
1368 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
1369
1370 dep = dwc->eps[0];
Felipe Balbic90bfae2011-11-29 13:11:21 +02001371 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL);
Felipe Balbi72246da2011-08-19 18:10:58 +03001372 if (ret) {
1373 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
1374 goto err0;
1375 }
1376
1377 dep = dwc->eps[1];
Felipe Balbic90bfae2011-11-29 13:11:21 +02001378 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL);
Felipe Balbi72246da2011-08-19 18:10:58 +03001379 if (ret) {
1380 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
1381 goto err1;
1382 }
1383
1384 /* begin to receive SETUP packets */
Felipe Balbic7fcdeb2011-08-27 22:28:36 +03001385 dwc->ep0state = EP0_SETUP_PHASE;
Felipe Balbi72246da2011-08-19 18:10:58 +03001386 dwc3_ep0_out_start(dwc);
1387
1388 spin_unlock_irqrestore(&dwc->lock, flags);
1389
1390 return 0;
1391
1392err1:
1393 __dwc3_gadget_ep_disable(dwc->eps[0]);
1394
1395err0:
Felipe Balbi1b5c94b2013-07-15 12:36:35 +03001396 dwc->gadget_driver = NULL;
Felipe Balbi72246da2011-08-19 18:10:58 +03001397 spin_unlock_irqrestore(&dwc->lock, flags);
1398
1399 return ret;
1400}
1401
1402static int dwc3_gadget_stop(struct usb_gadget *g,
1403 struct usb_gadget_driver *driver)
1404{
1405 struct dwc3 *dwc = gadget_to_dwc(g);
1406 unsigned long flags;
1407
1408 spin_lock_irqsave(&dwc->lock, flags);
1409
1410 __dwc3_gadget_ep_disable(dwc->eps[0]);
1411 __dwc3_gadget_ep_disable(dwc->eps[1]);
1412
1413 dwc->gadget_driver = NULL;
1414 dwc->gadget.dev.driver = NULL;
1415
1416 spin_unlock_irqrestore(&dwc->lock, flags);
1417
1418 return 0;
1419}
1420static const struct usb_gadget_ops dwc3_gadget_ops = {
1421 .get_frame = dwc3_gadget_get_frame,
1422 .wakeup = dwc3_gadget_wakeup,
1423 .set_selfpowered = dwc3_gadget_set_selfpowered,
1424 .pullup = dwc3_gadget_pullup,
1425 .udc_start = dwc3_gadget_start,
1426 .udc_stop = dwc3_gadget_stop,
1427};
1428
1429/* -------------------------------------------------------------------------- */
1430
1431static int __devinit dwc3_gadget_init_endpoints(struct dwc3 *dwc)
1432{
1433 struct dwc3_ep *dep;
1434 u8 epnum;
1435
1436 INIT_LIST_HEAD(&dwc->gadget.ep_list);
1437
1438 for (epnum = 0; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
1439 dep = kzalloc(sizeof(*dep), GFP_KERNEL);
1440 if (!dep) {
1441 dev_err(dwc->dev, "can't allocate endpoint %d\n",
1442 epnum);
1443 return -ENOMEM;
1444 }
1445
1446 dep->dwc = dwc;
1447 dep->number = epnum;
1448 dwc->eps[epnum] = dep;
1449
1450 snprintf(dep->name, sizeof(dep->name), "ep%d%s", epnum >> 1,
1451 (epnum & 1) ? "in" : "out");
1452 dep->endpoint.name = dep->name;
1453 dep->direction = (epnum & 1);
1454
1455 if (epnum == 0 || epnum == 1) {
1456 dep->endpoint.maxpacket = 512;
Pratyush Anand462434e2013-01-18 16:53:56 +05301457 dep->endpoint.maxburst = 1;
Felipe Balbi72246da2011-08-19 18:10:58 +03001458 dep->endpoint.ops = &dwc3_gadget_ep0_ops;
1459 if (!epnum)
1460 dwc->gadget.ep0 = &dep->endpoint;
1461 } else {
1462 int ret;
1463
1464 dep->endpoint.maxpacket = 1024;
Sebastian Andrzej Siewior12d36c12011-11-03 20:27:50 +01001465 dep->endpoint.max_streams = 15;
Felipe Balbi72246da2011-08-19 18:10:58 +03001466 dep->endpoint.ops = &dwc3_gadget_ep_ops;
1467 list_add_tail(&dep->endpoint.ep_list,
1468 &dwc->gadget.ep_list);
1469
1470 ret = dwc3_alloc_trb_pool(dep);
Felipe Balbi25b8ff62011-11-04 12:32:47 +02001471 if (ret)
Felipe Balbi72246da2011-08-19 18:10:58 +03001472 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001473 }
Felipe Balbi25b8ff62011-11-04 12:32:47 +02001474
Felipe Balbi72246da2011-08-19 18:10:58 +03001475 INIT_LIST_HEAD(&dep->request_list);
1476 INIT_LIST_HEAD(&dep->req_queued);
1477 }
1478
1479 return 0;
1480}
1481
1482static void dwc3_gadget_free_endpoints(struct dwc3 *dwc)
1483{
1484 struct dwc3_ep *dep;
1485 u8 epnum;
1486
1487 for (epnum = 0; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
1488 dep = dwc->eps[epnum];
1489 dwc3_free_trb_pool(dep);
1490
1491 if (epnum != 0 && epnum != 1)
1492 list_del(&dep->endpoint.ep_list);
1493
1494 kfree(dep);
1495 }
1496}
1497
1498static void dwc3_gadget_release(struct device *dev)
1499{
1500 dev_dbg(dev, "%s\n", __func__);
1501}
1502
1503/* -------------------------------------------------------------------------- */
1504static int dwc3_cleanup_done_reqs(struct dwc3 *dwc, struct dwc3_ep *dep,
1505 const struct dwc3_event_depevt *event, int status)
1506{
1507 struct dwc3_request *req;
Felipe Balbif6bafc62012-02-06 11:04:53 +02001508 struct dwc3_trb *trb;
Felipe Balbi72246da2011-08-19 18:10:58 +03001509 unsigned int count;
1510 unsigned int s_pkt = 0;
1511
1512 do {
1513 req = next_request(&dep->req_queued);
Sebastian Andrzej Siewiord39ee7b2011-11-03 10:32:20 +01001514 if (!req) {
1515 WARN_ON_ONCE(1);
1516 return 1;
1517 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001518
Felipe Balbif6bafc62012-02-06 11:04:53 +02001519 trb = req->trb;
Felipe Balbi72246da2011-08-19 18:10:58 +03001520
Felipe Balbif6bafc62012-02-06 11:04:53 +02001521 if ((trb->ctrl & DWC3_TRB_CTRL_HWO) && status != -ESHUTDOWN)
Sebastian Andrzej Siewior0d2f4752011-08-19 19:59:12 +02001522 /*
1523 * We continue despite the error. There is not much we
Paul Zimmerman1d046792012-02-15 18:56:56 -08001524 * can do. If we don't clean it up we loop forever. If
1525 * we skip the TRB then it gets overwritten after a
1526 * while since we use them in a ring buffer. A BUG()
1527 * would help. Lets hope that if this occurs, someone
Sebastian Andrzej Siewior0d2f4752011-08-19 19:59:12 +02001528 * fixes the root cause instead of looking away :)
1529 */
Felipe Balbi72246da2011-08-19 18:10:58 +03001530 dev_err(dwc->dev, "%s's TRB (%p) still owned by HW\n",
1531 dep->name, req->trb);
Felipe Balbif6bafc62012-02-06 11:04:53 +02001532 count = trb->size & DWC3_TRB_SIZE_MASK;
Felipe Balbi72246da2011-08-19 18:10:58 +03001533
1534 if (dep->direction) {
1535 if (count) {
1536 dev_err(dwc->dev, "incomplete IN transfer %s\n",
1537 dep->name);
1538 status = -ECONNRESET;
1539 }
1540 } else {
1541 if (count && (event->status & DEPEVT_STATUS_SHORT))
1542 s_pkt = 1;
1543 }
1544
1545 /*
1546 * We assume here we will always receive the entire data block
1547 * which we should receive. Meaning, if we program RX to
1548 * receive 4K but we receive only 2K, we assume that's all we
1549 * should receive and we simply bounce the request back to the
1550 * gadget driver for further processing.
1551 */
1552 req->request.actual += req->request.length - count;
1553 dwc3_gadget_giveback(dep, req, status);
1554 if (s_pkt)
1555 break;
Felipe Balbif6bafc62012-02-06 11:04:53 +02001556 if ((event->status & DEPEVT_STATUS_LST) &&
1557 (trb->ctrl & DWC3_TRB_CTRL_LST))
Felipe Balbi72246da2011-08-19 18:10:58 +03001558 break;
Felipe Balbif6bafc62012-02-06 11:04:53 +02001559 if ((event->status & DEPEVT_STATUS_IOC) &&
1560 (trb->ctrl & DWC3_TRB_CTRL_IOC))
Felipe Balbi72246da2011-08-19 18:10:58 +03001561 break;
1562 } while (1);
1563
Felipe Balbif6bafc62012-02-06 11:04:53 +02001564 if ((event->status & DEPEVT_STATUS_IOC) &&
1565 (trb->ctrl & DWC3_TRB_CTRL_IOC))
Felipe Balbi72246da2011-08-19 18:10:58 +03001566 return 0;
1567 return 1;
1568}
1569
1570static void dwc3_endpoint_transfer_complete(struct dwc3 *dwc,
1571 struct dwc3_ep *dep, const struct dwc3_event_depevt *event,
1572 int start_new)
1573{
1574 unsigned status = 0;
1575 int clean_busy;
1576
1577 if (event->status & DEPEVT_STATUS_BUSERR)
1578 status = -ECONNRESET;
1579
Paul Zimmerman1d046792012-02-15 18:56:56 -08001580 clean_busy = dwc3_cleanup_done_reqs(dwc, dep, event, status);
Paul Zimmermanc2df85c2012-02-24 17:32:18 -08001581 if (clean_busy)
Felipe Balbi72246da2011-08-19 18:10:58 +03001582 dep->flags &= ~DWC3_EP_BUSY;
Felipe Balbifae2b902011-10-14 13:00:30 +03001583
1584 /*
1585 * WORKAROUND: This is the 2nd half of U1/U2 -> U0 workaround.
1586 * See dwc3_gadget_linksts_change_interrupt() for 1st half.
1587 */
1588 if (dwc->revision < DWC3_REVISION_183A) {
1589 u32 reg;
1590 int i;
1591
1592 for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) {
1593 struct dwc3_ep *dep = dwc->eps[i];
1594
1595 if (!(dep->flags & DWC3_EP_ENABLED))
1596 continue;
1597
1598 if (!list_empty(&dep->req_queued))
1599 return;
1600 }
1601
1602 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
1603 reg |= dwc->u1u2;
1604 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1605
1606 dwc->u1u2 = 0;
1607 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001608}
1609
1610static void dwc3_gadget_start_isoc(struct dwc3 *dwc,
1611 struct dwc3_ep *dep, const struct dwc3_event_depevt *event)
1612{
Paul Zimmerman9bafa562012-02-17 14:10:16 -08001613 u32 uf, mask;
Felipe Balbi72246da2011-08-19 18:10:58 +03001614
1615 if (list_empty(&dep->request_list)) {
1616 dev_vdbg(dwc->dev, "ISOC ep %s run out for requests.\n",
1617 dep->name);
1618 return;
1619 }
1620
Paul Zimmerman9bafa562012-02-17 14:10:16 -08001621 mask = ~(dep->interval - 1);
1622 uf = event->parameters & mask;
1623 /* 4 micro frames in the future */
1624 uf += dep->interval * 4;
Felipe Balbi72246da2011-08-19 18:10:58 +03001625
1626 __dwc3_gadget_kick_transfer(dep, uf, 1);
1627}
1628
1629static void dwc3_process_ep_cmd_complete(struct dwc3_ep *dep,
1630 const struct dwc3_event_depevt *event)
1631{
1632 struct dwc3 *dwc = dep->dwc;
1633 struct dwc3_event_depevt mod_ev = *event;
1634
1635 /*
Paul Zimmerman1d046792012-02-15 18:56:56 -08001636 * We were asked to remove one request. It is possible that this
1637 * request and a few others were started together and have the same
Felipe Balbi72246da2011-08-19 18:10:58 +03001638 * transfer index. Since we stopped the complete endpoint we don't
1639 * know how many requests were already completed (and not yet)
1640 * reported and how could be done (later). We purge them all until
1641 * the end of the list.
1642 */
1643 mod_ev.status = DEPEVT_STATUS_LST;
1644 dwc3_cleanup_done_reqs(dwc, dep, &mod_ev, -ESHUTDOWN);
1645 dep->flags &= ~DWC3_EP_BUSY;
Paul Zimmerman1d046792012-02-15 18:56:56 -08001646 /* pending requests are ignored and are queued on XferNotReady */
Felipe Balbi72246da2011-08-19 18:10:58 +03001647}
1648
1649static void dwc3_ep_cmd_compl(struct dwc3_ep *dep,
1650 const struct dwc3_event_depevt *event)
1651{
1652 u32 param = event->parameters;
1653 u32 cmd_type = (param >> 8) & ((1 << 5) - 1);
1654
1655 switch (cmd_type) {
1656 case DWC3_DEPCMD_ENDTRANSFER:
1657 dwc3_process_ep_cmd_complete(dep, event);
1658 break;
1659 case DWC3_DEPCMD_STARTTRANSFER:
1660 dep->res_trans_idx = param & 0x7f;
1661 break;
1662 default:
1663 printk(KERN_ERR "%s() unknown /unexpected type: %d\n",
1664 __func__, cmd_type);
1665 break;
1666 };
1667}
1668
1669static void dwc3_endpoint_interrupt(struct dwc3 *dwc,
1670 const struct dwc3_event_depevt *event)
1671{
1672 struct dwc3_ep *dep;
1673 u8 epnum = event->endpoint_number;
1674
1675 dep = dwc->eps[epnum];
1676
1677 dev_vdbg(dwc->dev, "%s: %s\n", dep->name,
1678 dwc3_ep_event_string(event->endpoint_event));
1679
1680 if (epnum == 0 || epnum == 1) {
1681 dwc3_ep0_interrupt(dwc, event);
1682 return;
1683 }
1684
1685 switch (event->endpoint_event) {
1686 case DWC3_DEPEVT_XFERCOMPLETE:
Paul Zimmermanc2df85c2012-02-24 17:32:18 -08001687 dep->res_trans_idx = 0;
1688
Felipe Balbi72246da2011-08-19 18:10:58 +03001689 if (usb_endpoint_xfer_isoc(dep->desc)) {
1690 dev_dbg(dwc->dev, "%s is an Isochronous endpoint\n",
1691 dep->name);
1692 return;
1693 }
1694
1695 dwc3_endpoint_transfer_complete(dwc, dep, event, 1);
1696 break;
1697 case DWC3_DEPEVT_XFERINPROGRESS:
1698 if (!usb_endpoint_xfer_isoc(dep->desc)) {
1699 dev_dbg(dwc->dev, "%s is not an Isochronous endpoint\n",
1700 dep->name);
1701 return;
1702 }
1703
1704 dwc3_endpoint_transfer_complete(dwc, dep, event, 0);
1705 break;
1706 case DWC3_DEPEVT_XFERNOTREADY:
1707 if (usb_endpoint_xfer_isoc(dep->desc)) {
1708 dwc3_gadget_start_isoc(dwc, dep, event);
1709 } else {
1710 int ret;
1711
1712 dev_vdbg(dwc->dev, "%s: reason %s\n",
Felipe Balbi40aa41f2012-01-18 17:06:03 +02001713 dep->name, event->status &
1714 DEPEVT_STATUS_TRANSFER_ACTIVE
Felipe Balbi72246da2011-08-19 18:10:58 +03001715 ? "Transfer Active"
1716 : "Transfer Not Active");
1717
1718 ret = __dwc3_gadget_kick_transfer(dep, 0, 1);
1719 if (!ret || ret == -EBUSY)
1720 return;
1721
1722 dev_dbg(dwc->dev, "%s: failed to kick transfers\n",
1723 dep->name);
1724 }
1725
1726 break;
Felipe Balbi879631a2011-09-30 10:58:47 +03001727 case DWC3_DEPEVT_STREAMEVT:
1728 if (!usb_endpoint_xfer_bulk(dep->desc)) {
1729 dev_err(dwc->dev, "Stream event for non-Bulk %s\n",
1730 dep->name);
1731 return;
1732 }
1733
1734 switch (event->status) {
1735 case DEPEVT_STREAMEVT_FOUND:
1736 dev_vdbg(dwc->dev, "Stream %d found and started\n",
1737 event->parameters);
1738
1739 break;
1740 case DEPEVT_STREAMEVT_NOTFOUND:
1741 /* FALLTHROUGH */
1742 default:
1743 dev_dbg(dwc->dev, "Couldn't find suitable stream\n");
1744 }
1745 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03001746 case DWC3_DEPEVT_RXTXFIFOEVT:
1747 dev_dbg(dwc->dev, "%s FIFO Overrun\n", dep->name);
1748 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03001749 case DWC3_DEPEVT_EPCMDCMPLT:
1750 dwc3_ep_cmd_compl(dep, event);
1751 break;
1752 }
1753}
1754
1755static void dwc3_disconnect_gadget(struct dwc3 *dwc)
1756{
1757 if (dwc->gadget_driver && dwc->gadget_driver->disconnect) {
1758 spin_unlock(&dwc->lock);
1759 dwc->gadget_driver->disconnect(&dwc->gadget);
1760 spin_lock(&dwc->lock);
1761 }
1762}
1763
1764static void dwc3_stop_active_transfer(struct dwc3 *dwc, u32 epnum)
1765{
1766 struct dwc3_ep *dep;
1767 struct dwc3_gadget_ep_cmd_params params;
1768 u32 cmd;
1769 int ret;
1770
1771 dep = dwc->eps[epnum];
1772
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +02001773 WARN_ON(!dep->res_trans_idx);
Felipe Balbi72246da2011-08-19 18:10:58 +03001774 if (dep->res_trans_idx) {
1775 cmd = DWC3_DEPCMD_ENDTRANSFER;
1776 cmd |= DWC3_DEPCMD_HIPRI_FORCERM | DWC3_DEPCMD_CMDIOC;
1777 cmd |= DWC3_DEPCMD_PARAM(dep->res_trans_idx);
1778 memset(&params, 0, sizeof(params));
1779 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number, cmd, &params);
1780 WARN_ON_ONCE(ret);
Sebastian Andrzej Siewiora1ae9be2011-08-22 17:42:18 +02001781 dep->res_trans_idx = 0;
Felipe Balbibfbd61e2012-10-04 11:58:00 +03001782 dep->flags &= ~DWC3_EP_BUSY;
Felipe Balbi72246da2011-08-19 18:10:58 +03001783 }
1784}
1785
1786static void dwc3_stop_active_transfers(struct dwc3 *dwc)
1787{
1788 u32 epnum;
1789
1790 for (epnum = 2; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
1791 struct dwc3_ep *dep;
1792
1793 dep = dwc->eps[epnum];
1794 if (!(dep->flags & DWC3_EP_ENABLED))
1795 continue;
1796
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +02001797 dwc3_remove_requests(dwc, dep);
Felipe Balbi72246da2011-08-19 18:10:58 +03001798 }
1799}
1800
1801static void dwc3_clear_stall_all_ep(struct dwc3 *dwc)
1802{
1803 u32 epnum;
1804
1805 for (epnum = 1; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
1806 struct dwc3_ep *dep;
1807 struct dwc3_gadget_ep_cmd_params params;
1808 int ret;
1809
1810 dep = dwc->eps[epnum];
1811
1812 if (!(dep->flags & DWC3_EP_STALL))
1813 continue;
1814
1815 dep->flags &= ~DWC3_EP_STALL;
1816
1817 memset(&params, 0, sizeof(params));
1818 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number,
1819 DWC3_DEPCMD_CLEARSTALL, &params);
1820 WARN_ON_ONCE(ret);
1821 }
1822}
1823
1824static void dwc3_gadget_disconnect_interrupt(struct dwc3 *dwc)
1825{
1826 dev_vdbg(dwc->dev, "%s\n", __func__);
1827#if 0
1828 XXX
1829 U1/U2 is powersave optimization. Skip it for now. Anyway we need to
1830 enable it before we can disable it.
1831
1832 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
1833 reg &= ~DWC3_DCTL_INITU1ENA;
1834 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1835
1836 reg &= ~DWC3_DCTL_INITU2ENA;
1837 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1838#endif
1839
1840 dwc3_stop_active_transfers(dwc);
1841 dwc3_disconnect_gadget(dwc);
Paul Zimmermanb23c8432011-09-30 10:58:42 +03001842 dwc->start_config_issued = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03001843
1844 dwc->gadget.speed = USB_SPEED_UNKNOWN;
Felipe Balbidf62df52011-10-14 15:11:49 +03001845 dwc->setup_packet_pending = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03001846}
1847
1848static void dwc3_gadget_usb3_phy_power(struct dwc3 *dwc, int on)
1849{
1850 u32 reg;
1851
1852 reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0));
1853
1854 if (on)
1855 reg &= ~DWC3_GUSB3PIPECTL_SUSPHY;
1856 else
1857 reg |= DWC3_GUSB3PIPECTL_SUSPHY;
1858
1859 dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg);
1860}
1861
1862static void dwc3_gadget_usb2_phy_power(struct dwc3 *dwc, int on)
1863{
1864 u32 reg;
1865
1866 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
1867
1868 if (on)
1869 reg &= ~DWC3_GUSB2PHYCFG_SUSPHY;
1870 else
1871 reg |= DWC3_GUSB2PHYCFG_SUSPHY;
1872
1873 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
1874}
1875
1876static void dwc3_gadget_reset_interrupt(struct dwc3 *dwc)
1877{
1878 u32 reg;
1879
1880 dev_vdbg(dwc->dev, "%s\n", __func__);
1881
Felipe Balbidf62df52011-10-14 15:11:49 +03001882 /*
1883 * WORKAROUND: DWC3 revisions <1.88a have an issue which
1884 * would cause a missing Disconnect Event if there's a
1885 * pending Setup Packet in the FIFO.
1886 *
1887 * There's no suggested workaround on the official Bug
1888 * report, which states that "unless the driver/application
1889 * is doing any special handling of a disconnect event,
1890 * there is no functional issue".
1891 *
1892 * Unfortunately, it turns out that we _do_ some special
1893 * handling of a disconnect event, namely complete all
1894 * pending transfers, notify gadget driver of the
1895 * disconnection, and so on.
1896 *
1897 * Our suggested workaround is to follow the Disconnect
1898 * Event steps here, instead, based on a setup_packet_pending
1899 * flag. Such flag gets set whenever we have a XferNotReady
1900 * event on EP0 and gets cleared on XferComplete for the
1901 * same endpoint.
1902 *
1903 * Refers to:
1904 *
1905 * STAR#9000466709: RTL: Device : Disconnect event not
1906 * generated if setup packet pending in FIFO
1907 */
1908 if (dwc->revision < DWC3_REVISION_188A) {
1909 if (dwc->setup_packet_pending)
1910 dwc3_gadget_disconnect_interrupt(dwc);
1911 }
1912
Felipe Balbi961906e2011-12-20 15:37:21 +02001913 /* after reset -> Default State */
1914 dwc->dev_state = DWC3_DEFAULT_STATE;
1915
Felipe Balbi72246da2011-08-19 18:10:58 +03001916 /* Enable PHYs */
1917 dwc3_gadget_usb2_phy_power(dwc, true);
1918 dwc3_gadget_usb3_phy_power(dwc, true);
1919
1920 if (dwc->gadget.speed != USB_SPEED_UNKNOWN)
1921 dwc3_disconnect_gadget(dwc);
1922
1923 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
1924 reg &= ~DWC3_DCTL_TSTCTRL_MASK;
1925 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Gerard Cauvy3b637362012-02-10 12:21:18 +02001926 dwc->test_mode = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03001927
1928 dwc3_stop_active_transfers(dwc);
1929 dwc3_clear_stall_all_ep(dwc);
Paul Zimmermanb23c8432011-09-30 10:58:42 +03001930 dwc->start_config_issued = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03001931
1932 /* Reset device address to zero */
1933 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
1934 reg &= ~(DWC3_DCFG_DEVADDR_MASK);
1935 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +03001936}
1937
1938static void dwc3_update_ram_clk_sel(struct dwc3 *dwc, u32 speed)
1939{
1940 u32 reg;
1941 u32 usb30_clock = DWC3_GCTL_CLK_BUS;
1942
1943 /*
1944 * We change the clock only at SS but I dunno why I would want to do
1945 * this. Maybe it becomes part of the power saving plan.
1946 */
1947
1948 if (speed != DWC3_DSTS_SUPERSPEED)
1949 return;
1950
1951 /*
1952 * RAMClkSel is reset to 0 after USB reset, so it must be reprogrammed
1953 * each time on Connect Done.
1954 */
1955 if (!usb30_clock)
1956 return;
1957
1958 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
1959 reg |= DWC3_GCTL_RAMCLKSEL(usb30_clock);
1960 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
1961}
1962
1963static void dwc3_gadget_disable_phy(struct dwc3 *dwc, u8 speed)
1964{
1965 switch (speed) {
1966 case USB_SPEED_SUPER:
1967 dwc3_gadget_usb2_phy_power(dwc, false);
1968 break;
1969 case USB_SPEED_HIGH:
1970 case USB_SPEED_FULL:
1971 case USB_SPEED_LOW:
1972 dwc3_gadget_usb3_phy_power(dwc, false);
1973 break;
1974 }
1975}
1976
1977static void dwc3_gadget_conndone_interrupt(struct dwc3 *dwc)
1978{
1979 struct dwc3_gadget_ep_cmd_params params;
1980 struct dwc3_ep *dep;
1981 int ret;
1982 u32 reg;
1983 u8 speed;
1984
1985 dev_vdbg(dwc->dev, "%s\n", __func__);
1986
1987 memset(&params, 0x00, sizeof(params));
1988
Felipe Balbi72246da2011-08-19 18:10:58 +03001989 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1990 speed = reg & DWC3_DSTS_CONNECTSPD;
1991 dwc->speed = speed;
1992
1993 dwc3_update_ram_clk_sel(dwc, speed);
1994
1995 switch (speed) {
1996 case DWC3_DCFG_SUPERSPEED:
Felipe Balbi05870c52011-10-14 14:51:38 +03001997 /*
1998 * WORKAROUND: DWC3 revisions <1.90a have an issue which
1999 * would cause a missing USB3 Reset event.
2000 *
2001 * In such situations, we should force a USB3 Reset
2002 * event by calling our dwc3_gadget_reset_interrupt()
2003 * routine.
2004 *
2005 * Refers to:
2006 *
2007 * STAR#9000483510: RTL: SS : USB3 reset event may
2008 * not be generated always when the link enters poll
2009 */
2010 if (dwc->revision < DWC3_REVISION_190A)
2011 dwc3_gadget_reset_interrupt(dwc);
2012
Felipe Balbi72246da2011-08-19 18:10:58 +03002013 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
2014 dwc->gadget.ep0->maxpacket = 512;
2015 dwc->gadget.speed = USB_SPEED_SUPER;
2016 break;
2017 case DWC3_DCFG_HIGHSPEED:
2018 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
2019 dwc->gadget.ep0->maxpacket = 64;
2020 dwc->gadget.speed = USB_SPEED_HIGH;
2021 break;
2022 case DWC3_DCFG_FULLSPEED2:
2023 case DWC3_DCFG_FULLSPEED1:
2024 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
2025 dwc->gadget.ep0->maxpacket = 64;
2026 dwc->gadget.speed = USB_SPEED_FULL;
2027 break;
2028 case DWC3_DCFG_LOWSPEED:
2029 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(8);
2030 dwc->gadget.ep0->maxpacket = 8;
2031 dwc->gadget.speed = USB_SPEED_LOW;
2032 break;
2033 }
2034
2035 /* Disable unneded PHY */
2036 dwc3_gadget_disable_phy(dwc, dwc->gadget.speed);
2037
2038 dep = dwc->eps[0];
Felipe Balbic90bfae2011-11-29 13:11:21 +02002039 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL);
Felipe Balbi72246da2011-08-19 18:10:58 +03002040 if (ret) {
2041 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
2042 return;
2043 }
2044
2045 dep = dwc->eps[1];
Felipe Balbic90bfae2011-11-29 13:11:21 +02002046 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL);
Felipe Balbi72246da2011-08-19 18:10:58 +03002047 if (ret) {
2048 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
2049 return;
2050 }
2051
2052 /*
2053 * Configure PHY via GUSB3PIPECTLn if required.
2054 *
2055 * Update GTXFIFOSIZn
2056 *
2057 * In both cases reset values should be sufficient.
2058 */
2059}
2060
2061static void dwc3_gadget_wakeup_interrupt(struct dwc3 *dwc)
2062{
2063 dev_vdbg(dwc->dev, "%s\n", __func__);
2064
2065 /*
2066 * TODO take core out of low power mode when that's
2067 * implemented.
2068 */
2069
2070 dwc->gadget_driver->resume(&dwc->gadget);
2071}
2072
2073static void dwc3_gadget_linksts_change_interrupt(struct dwc3 *dwc,
2074 unsigned int evtinfo)
2075{
Felipe Balbifae2b902011-10-14 13:00:30 +03002076 enum dwc3_link_state next = evtinfo & DWC3_LINK_STATE_MASK;
2077
2078 /*
2079 * WORKAROUND: DWC3 Revisions <1.83a have an issue which, depending
2080 * on the link partner, the USB session might do multiple entry/exit
2081 * of low power states before a transfer takes place.
2082 *
2083 * Due to this problem, we might experience lower throughput. The
2084 * suggested workaround is to disable DCTL[12:9] bits if we're
2085 * transitioning from U1/U2 to U0 and enable those bits again
2086 * after a transfer completes and there are no pending transfers
2087 * on any of the enabled endpoints.
2088 *
2089 * This is the first half of that workaround.
2090 *
2091 * Refers to:
2092 *
2093 * STAR#9000446952: RTL: Device SS : if U1/U2 ->U0 takes >128us
2094 * core send LGO_Ux entering U0
2095 */
2096 if (dwc->revision < DWC3_REVISION_183A) {
2097 if (next == DWC3_LINK_STATE_U0) {
2098 u32 u1u2;
2099 u32 reg;
2100
2101 switch (dwc->link_state) {
2102 case DWC3_LINK_STATE_U1:
2103 case DWC3_LINK_STATE_U2:
2104 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2105 u1u2 = reg & (DWC3_DCTL_INITU2ENA
2106 | DWC3_DCTL_ACCEPTU2ENA
2107 | DWC3_DCTL_INITU1ENA
2108 | DWC3_DCTL_ACCEPTU1ENA);
2109
2110 if (!dwc->u1u2)
2111 dwc->u1u2 = reg & u1u2;
2112
2113 reg &= ~u1u2;
2114
2115 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2116 break;
2117 default:
2118 /* do nothing */
2119 break;
2120 }
2121 }
2122 }
2123
2124 dwc->link_state = next;
Felipe Balbi019ac832011-09-08 21:18:47 +03002125
2126 dev_vdbg(dwc->dev, "%s link %d\n", __func__, dwc->link_state);
Felipe Balbi72246da2011-08-19 18:10:58 +03002127}
2128
2129static void dwc3_gadget_interrupt(struct dwc3 *dwc,
2130 const struct dwc3_event_devt *event)
2131{
2132 switch (event->type) {
2133 case DWC3_DEVICE_EVENT_DISCONNECT:
2134 dwc3_gadget_disconnect_interrupt(dwc);
2135 break;
2136 case DWC3_DEVICE_EVENT_RESET:
2137 dwc3_gadget_reset_interrupt(dwc);
2138 break;
2139 case DWC3_DEVICE_EVENT_CONNECT_DONE:
2140 dwc3_gadget_conndone_interrupt(dwc);
2141 break;
2142 case DWC3_DEVICE_EVENT_WAKEUP:
2143 dwc3_gadget_wakeup_interrupt(dwc);
2144 break;
2145 case DWC3_DEVICE_EVENT_LINK_STATUS_CHANGE:
2146 dwc3_gadget_linksts_change_interrupt(dwc, event->event_info);
2147 break;
2148 case DWC3_DEVICE_EVENT_EOPF:
2149 dev_vdbg(dwc->dev, "End of Periodic Frame\n");
2150 break;
2151 case DWC3_DEVICE_EVENT_SOF:
2152 dev_vdbg(dwc->dev, "Start of Periodic Frame\n");
2153 break;
2154 case DWC3_DEVICE_EVENT_ERRATIC_ERROR:
2155 dev_vdbg(dwc->dev, "Erratic Error\n");
2156 break;
2157 case DWC3_DEVICE_EVENT_CMD_CMPL:
2158 dev_vdbg(dwc->dev, "Command Complete\n");
2159 break;
2160 case DWC3_DEVICE_EVENT_OVERFLOW:
2161 dev_vdbg(dwc->dev, "Overflow\n");
2162 break;
2163 default:
2164 dev_dbg(dwc->dev, "UNKNOWN IRQ %d\n", event->type);
2165 }
2166}
2167
2168static void dwc3_process_event_entry(struct dwc3 *dwc,
2169 const union dwc3_event *event)
2170{
2171 /* Endpoint IRQ, handle it and return early */
2172 if (event->type.is_devspec == 0) {
2173 /* depevt */
2174 return dwc3_endpoint_interrupt(dwc, &event->depevt);
2175 }
2176
2177 switch (event->type.type) {
2178 case DWC3_EVENT_TYPE_DEV:
2179 dwc3_gadget_interrupt(dwc, &event->devt);
2180 break;
2181 /* REVISIT what to do with Carkit and I2C events ? */
2182 default:
2183 dev_err(dwc->dev, "UNKNOWN IRQ type %d\n", event->raw);
2184 }
2185}
2186
2187static irqreturn_t dwc3_process_event_buf(struct dwc3 *dwc, u32 buf)
2188{
2189 struct dwc3_event_buffer *evt;
2190 int left;
2191 u32 count;
2192
2193 count = dwc3_readl(dwc->regs, DWC3_GEVNTCOUNT(buf));
2194 count &= DWC3_GEVNTCOUNT_MASK;
2195 if (!count)
2196 return IRQ_NONE;
2197
2198 evt = dwc->ev_buffs[buf];
2199 left = count;
2200
2201 while (left > 0) {
2202 union dwc3_event event;
2203
Felipe Balbid70d8442012-02-06 13:40:17 +02002204 event.raw = *(u32 *) (evt->buf + evt->lpos);
2205
Felipe Balbi72246da2011-08-19 18:10:58 +03002206 dwc3_process_event_entry(dwc, &event);
2207 /*
2208 * XXX we wrap around correctly to the next entry as almost all
2209 * entries are 4 bytes in size. There is one entry which has 12
2210 * bytes which is a regular entry followed by 8 bytes data. ATM
2211 * I don't know how things are organized if were get next to the
2212 * a boundary so I worry about that once we try to handle that.
2213 */
2214 evt->lpos = (evt->lpos + 4) % DWC3_EVENT_BUFFERS_SIZE;
2215 left -= 4;
2216
2217 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(buf), 4);
2218 }
2219
2220 return IRQ_HANDLED;
2221}
2222
2223static irqreturn_t dwc3_interrupt(int irq, void *_dwc)
2224{
2225 struct dwc3 *dwc = _dwc;
2226 int i;
2227 irqreturn_t ret = IRQ_NONE;
2228
2229 spin_lock(&dwc->lock);
2230
Felipe Balbi9f622b22011-10-12 10:31:04 +03002231 for (i = 0; i < dwc->num_event_buffers; i++) {
Felipe Balbi72246da2011-08-19 18:10:58 +03002232 irqreturn_t status;
2233
2234 status = dwc3_process_event_buf(dwc, i);
2235 if (status == IRQ_HANDLED)
2236 ret = status;
2237 }
2238
2239 spin_unlock(&dwc->lock);
2240
2241 return ret;
2242}
2243
2244/**
2245 * dwc3_gadget_init - Initializes gadget related registers
Paul Zimmerman1d046792012-02-15 18:56:56 -08002246 * @dwc: pointer to our controller context structure
Felipe Balbi72246da2011-08-19 18:10:58 +03002247 *
2248 * Returns 0 on success otherwise negative errno.
2249 */
2250int __devinit dwc3_gadget_init(struct dwc3 *dwc)
2251{
2252 u32 reg;
2253 int ret;
2254 int irq;
2255
2256 dwc->ctrl_req = dma_alloc_coherent(dwc->dev, sizeof(*dwc->ctrl_req),
2257 &dwc->ctrl_req_addr, GFP_KERNEL);
2258 if (!dwc->ctrl_req) {
2259 dev_err(dwc->dev, "failed to allocate ctrl request\n");
2260 ret = -ENOMEM;
2261 goto err0;
2262 }
2263
2264 dwc->ep0_trb = dma_alloc_coherent(dwc->dev, sizeof(*dwc->ep0_trb),
2265 &dwc->ep0_trb_addr, GFP_KERNEL);
2266 if (!dwc->ep0_trb) {
2267 dev_err(dwc->dev, "failed to allocate ep0 trb\n");
2268 ret = -ENOMEM;
2269 goto err1;
2270 }
2271
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02002272 dwc->setup_buf = kzalloc(sizeof(*dwc->setup_buf) * 2,
2273 GFP_KERNEL);
Felipe Balbi72246da2011-08-19 18:10:58 +03002274 if (!dwc->setup_buf) {
2275 dev_err(dwc->dev, "failed to allocate setup buffer\n");
2276 ret = -ENOMEM;
2277 goto err2;
2278 }
2279
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002280 dwc->ep0_bounce = dma_alloc_coherent(dwc->dev,
2281 512, &dwc->ep0_bounce_addr, GFP_KERNEL);
2282 if (!dwc->ep0_bounce) {
2283 dev_err(dwc->dev, "failed to allocate ep0 bounce buffer\n");
2284 ret = -ENOMEM;
2285 goto err3;
2286 }
2287
Felipe Balbi72246da2011-08-19 18:10:58 +03002288 dev_set_name(&dwc->gadget.dev, "gadget");
2289
2290 dwc->gadget.ops = &dwc3_gadget_ops;
Michal Nazarewiczd327ab52011-11-19 18:27:37 +01002291 dwc->gadget.max_speed = USB_SPEED_SUPER;
Felipe Balbi72246da2011-08-19 18:10:58 +03002292 dwc->gadget.speed = USB_SPEED_UNKNOWN;
2293 dwc->gadget.dev.parent = dwc->dev;
Felipe Balbieeb720f2011-11-28 12:46:59 +02002294 dwc->gadget.sg_supported = true;
Felipe Balbi72246da2011-08-19 18:10:58 +03002295
2296 dma_set_coherent_mask(&dwc->gadget.dev, dwc->dev->coherent_dma_mask);
2297
2298 dwc->gadget.dev.dma_parms = dwc->dev->dma_parms;
2299 dwc->gadget.dev.dma_mask = dwc->dev->dma_mask;
2300 dwc->gadget.dev.release = dwc3_gadget_release;
2301 dwc->gadget.name = "dwc3-gadget";
2302
2303 /*
2304 * REVISIT: Here we should clear all pending IRQs to be
2305 * sure we're starting from a well known location.
2306 */
2307
2308 ret = dwc3_gadget_init_endpoints(dwc);
2309 if (ret)
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002310 goto err4;
Felipe Balbi72246da2011-08-19 18:10:58 +03002311
2312 irq = platform_get_irq(to_platform_device(dwc->dev), 0);
2313
2314 ret = request_irq(irq, dwc3_interrupt, IRQF_SHARED,
2315 "dwc3", dwc);
2316 if (ret) {
2317 dev_err(dwc->dev, "failed to request irq #%d --> %d\n",
2318 irq, ret);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002319 goto err5;
Felipe Balbi72246da2011-08-19 18:10:58 +03002320 }
2321
2322 /* Enable all but Start and End of Frame IRQs */
2323 reg = (DWC3_DEVTEN_VNDRDEVTSTRCVEDEN |
2324 DWC3_DEVTEN_EVNTOVERFLOWEN |
2325 DWC3_DEVTEN_CMDCMPLTEN |
2326 DWC3_DEVTEN_ERRTICERREN |
2327 DWC3_DEVTEN_WKUPEVTEN |
2328 DWC3_DEVTEN_ULSTCNGEN |
2329 DWC3_DEVTEN_CONNECTDONEEN |
2330 DWC3_DEVTEN_USBRSTEN |
2331 DWC3_DEVTEN_DISCONNEVTEN);
2332 dwc3_writel(dwc->regs, DWC3_DEVTEN, reg);
2333
2334 ret = device_register(&dwc->gadget.dev);
2335 if (ret) {
2336 dev_err(dwc->dev, "failed to register gadget device\n");
2337 put_device(&dwc->gadget.dev);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002338 goto err6;
Felipe Balbi72246da2011-08-19 18:10:58 +03002339 }
2340
2341 ret = usb_add_gadget_udc(dwc->dev, &dwc->gadget);
2342 if (ret) {
2343 dev_err(dwc->dev, "failed to register udc\n");
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002344 goto err7;
Felipe Balbi72246da2011-08-19 18:10:58 +03002345 }
2346
2347 return 0;
2348
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002349err7:
Felipe Balbi72246da2011-08-19 18:10:58 +03002350 device_unregister(&dwc->gadget.dev);
2351
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002352err6:
Felipe Balbi72246da2011-08-19 18:10:58 +03002353 dwc3_writel(dwc->regs, DWC3_DEVTEN, 0x00);
2354 free_irq(irq, dwc);
2355
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002356err5:
Felipe Balbi72246da2011-08-19 18:10:58 +03002357 dwc3_gadget_free_endpoints(dwc);
2358
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002359err4:
2360 dma_free_coherent(dwc->dev, 512, dwc->ep0_bounce,
2361 dwc->ep0_bounce_addr);
2362
Felipe Balbi72246da2011-08-19 18:10:58 +03002363err3:
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02002364 kfree(dwc->setup_buf);
Felipe Balbi72246da2011-08-19 18:10:58 +03002365
2366err2:
2367 dma_free_coherent(dwc->dev, sizeof(*dwc->ep0_trb),
2368 dwc->ep0_trb, dwc->ep0_trb_addr);
2369
2370err1:
2371 dma_free_coherent(dwc->dev, sizeof(*dwc->ctrl_req),
2372 dwc->ctrl_req, dwc->ctrl_req_addr);
2373
2374err0:
2375 return ret;
2376}
2377
2378void dwc3_gadget_exit(struct dwc3 *dwc)
2379{
2380 int irq;
Felipe Balbi72246da2011-08-19 18:10:58 +03002381
2382 usb_del_gadget_udc(&dwc->gadget);
2383 irq = platform_get_irq(to_platform_device(dwc->dev), 0);
2384
2385 dwc3_writel(dwc->regs, DWC3_DEVTEN, 0x00);
2386 free_irq(irq, dwc);
2387
Felipe Balbi72246da2011-08-19 18:10:58 +03002388 dwc3_gadget_free_endpoints(dwc);
2389
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002390 dma_free_coherent(dwc->dev, 512, dwc->ep0_bounce,
2391 dwc->ep0_bounce_addr);
2392
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02002393 kfree(dwc->setup_buf);
Felipe Balbi72246da2011-08-19 18:10:58 +03002394
2395 dma_free_coherent(dwc->dev, sizeof(*dwc->ep0_trb),
2396 dwc->ep0_trb, dwc->ep0_trb_addr);
2397
2398 dma_free_coherent(dwc->dev, sizeof(*dwc->ctrl_req),
2399 dwc->ctrl_req, dwc->ctrl_req_addr);
2400
2401 device_unregister(&dwc->gadget.dev);
2402}