blob: f62629b029fc38da2f9c9e7fc93e39ab3ba971c4 [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:
1396 spin_unlock_irqrestore(&dwc->lock, flags);
1397
1398 return ret;
1399}
1400
1401static int dwc3_gadget_stop(struct usb_gadget *g,
1402 struct usb_gadget_driver *driver)
1403{
1404 struct dwc3 *dwc = gadget_to_dwc(g);
1405 unsigned long flags;
1406
1407 spin_lock_irqsave(&dwc->lock, flags);
1408
1409 __dwc3_gadget_ep_disable(dwc->eps[0]);
1410 __dwc3_gadget_ep_disable(dwc->eps[1]);
1411
1412 dwc->gadget_driver = NULL;
1413 dwc->gadget.dev.driver = NULL;
1414
1415 spin_unlock_irqrestore(&dwc->lock, flags);
1416
1417 return 0;
1418}
1419static const struct usb_gadget_ops dwc3_gadget_ops = {
1420 .get_frame = dwc3_gadget_get_frame,
1421 .wakeup = dwc3_gadget_wakeup,
1422 .set_selfpowered = dwc3_gadget_set_selfpowered,
1423 .pullup = dwc3_gadget_pullup,
1424 .udc_start = dwc3_gadget_start,
1425 .udc_stop = dwc3_gadget_stop,
1426};
1427
1428/* -------------------------------------------------------------------------- */
1429
1430static int __devinit dwc3_gadget_init_endpoints(struct dwc3 *dwc)
1431{
1432 struct dwc3_ep *dep;
1433 u8 epnum;
1434
1435 INIT_LIST_HEAD(&dwc->gadget.ep_list);
1436
1437 for (epnum = 0; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
1438 dep = kzalloc(sizeof(*dep), GFP_KERNEL);
1439 if (!dep) {
1440 dev_err(dwc->dev, "can't allocate endpoint %d\n",
1441 epnum);
1442 return -ENOMEM;
1443 }
1444
1445 dep->dwc = dwc;
1446 dep->number = epnum;
1447 dwc->eps[epnum] = dep;
1448
1449 snprintf(dep->name, sizeof(dep->name), "ep%d%s", epnum >> 1,
1450 (epnum & 1) ? "in" : "out");
1451 dep->endpoint.name = dep->name;
1452 dep->direction = (epnum & 1);
1453
1454 if (epnum == 0 || epnum == 1) {
1455 dep->endpoint.maxpacket = 512;
Pratyush Anand462434e2013-01-18 16:53:56 +05301456 dep->endpoint.maxburst = 1;
Felipe Balbi72246da2011-08-19 18:10:58 +03001457 dep->endpoint.ops = &dwc3_gadget_ep0_ops;
1458 if (!epnum)
1459 dwc->gadget.ep0 = &dep->endpoint;
1460 } else {
1461 int ret;
1462
1463 dep->endpoint.maxpacket = 1024;
Sebastian Andrzej Siewior12d36c12011-11-03 20:27:50 +01001464 dep->endpoint.max_streams = 15;
Felipe Balbi72246da2011-08-19 18:10:58 +03001465 dep->endpoint.ops = &dwc3_gadget_ep_ops;
1466 list_add_tail(&dep->endpoint.ep_list,
1467 &dwc->gadget.ep_list);
1468
1469 ret = dwc3_alloc_trb_pool(dep);
Felipe Balbi25b8ff62011-11-04 12:32:47 +02001470 if (ret)
Felipe Balbi72246da2011-08-19 18:10:58 +03001471 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001472 }
Felipe Balbi25b8ff62011-11-04 12:32:47 +02001473
Felipe Balbi72246da2011-08-19 18:10:58 +03001474 INIT_LIST_HEAD(&dep->request_list);
1475 INIT_LIST_HEAD(&dep->req_queued);
1476 }
1477
1478 return 0;
1479}
1480
1481static void dwc3_gadget_free_endpoints(struct dwc3 *dwc)
1482{
1483 struct dwc3_ep *dep;
1484 u8 epnum;
1485
1486 for (epnum = 0; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
1487 dep = dwc->eps[epnum];
1488 dwc3_free_trb_pool(dep);
1489
1490 if (epnum != 0 && epnum != 1)
1491 list_del(&dep->endpoint.ep_list);
1492
1493 kfree(dep);
1494 }
1495}
1496
1497static void dwc3_gadget_release(struct device *dev)
1498{
1499 dev_dbg(dev, "%s\n", __func__);
1500}
1501
1502/* -------------------------------------------------------------------------- */
1503static int dwc3_cleanup_done_reqs(struct dwc3 *dwc, struct dwc3_ep *dep,
1504 const struct dwc3_event_depevt *event, int status)
1505{
1506 struct dwc3_request *req;
Felipe Balbif6bafc62012-02-06 11:04:53 +02001507 struct dwc3_trb *trb;
Felipe Balbi72246da2011-08-19 18:10:58 +03001508 unsigned int count;
1509 unsigned int s_pkt = 0;
1510
1511 do {
1512 req = next_request(&dep->req_queued);
Sebastian Andrzej Siewiord39ee7b2011-11-03 10:32:20 +01001513 if (!req) {
1514 WARN_ON_ONCE(1);
1515 return 1;
1516 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001517
Felipe Balbif6bafc62012-02-06 11:04:53 +02001518 trb = req->trb;
Felipe Balbi72246da2011-08-19 18:10:58 +03001519
Felipe Balbif6bafc62012-02-06 11:04:53 +02001520 if ((trb->ctrl & DWC3_TRB_CTRL_HWO) && status != -ESHUTDOWN)
Sebastian Andrzej Siewior0d2f4752011-08-19 19:59:12 +02001521 /*
1522 * We continue despite the error. There is not much we
Paul Zimmerman1d046792012-02-15 18:56:56 -08001523 * can do. If we don't clean it up we loop forever. If
1524 * we skip the TRB then it gets overwritten after a
1525 * while since we use them in a ring buffer. A BUG()
1526 * would help. Lets hope that if this occurs, someone
Sebastian Andrzej Siewior0d2f4752011-08-19 19:59:12 +02001527 * fixes the root cause instead of looking away :)
1528 */
Felipe Balbi72246da2011-08-19 18:10:58 +03001529 dev_err(dwc->dev, "%s's TRB (%p) still owned by HW\n",
1530 dep->name, req->trb);
Felipe Balbif6bafc62012-02-06 11:04:53 +02001531 count = trb->size & DWC3_TRB_SIZE_MASK;
Felipe Balbi72246da2011-08-19 18:10:58 +03001532
1533 if (dep->direction) {
1534 if (count) {
1535 dev_err(dwc->dev, "incomplete IN transfer %s\n",
1536 dep->name);
1537 status = -ECONNRESET;
1538 }
1539 } else {
1540 if (count && (event->status & DEPEVT_STATUS_SHORT))
1541 s_pkt = 1;
1542 }
1543
1544 /*
1545 * We assume here we will always receive the entire data block
1546 * which we should receive. Meaning, if we program RX to
1547 * receive 4K but we receive only 2K, we assume that's all we
1548 * should receive and we simply bounce the request back to the
1549 * gadget driver for further processing.
1550 */
1551 req->request.actual += req->request.length - count;
1552 dwc3_gadget_giveback(dep, req, status);
1553 if (s_pkt)
1554 break;
Felipe Balbif6bafc62012-02-06 11:04:53 +02001555 if ((event->status & DEPEVT_STATUS_LST) &&
1556 (trb->ctrl & DWC3_TRB_CTRL_LST))
Felipe Balbi72246da2011-08-19 18:10:58 +03001557 break;
Felipe Balbif6bafc62012-02-06 11:04:53 +02001558 if ((event->status & DEPEVT_STATUS_IOC) &&
1559 (trb->ctrl & DWC3_TRB_CTRL_IOC))
Felipe Balbi72246da2011-08-19 18:10:58 +03001560 break;
1561 } while (1);
1562
Felipe Balbif6bafc62012-02-06 11:04:53 +02001563 if ((event->status & DEPEVT_STATUS_IOC) &&
1564 (trb->ctrl & DWC3_TRB_CTRL_IOC))
Felipe Balbi72246da2011-08-19 18:10:58 +03001565 return 0;
1566 return 1;
1567}
1568
1569static void dwc3_endpoint_transfer_complete(struct dwc3 *dwc,
1570 struct dwc3_ep *dep, const struct dwc3_event_depevt *event,
1571 int start_new)
1572{
1573 unsigned status = 0;
1574 int clean_busy;
1575
1576 if (event->status & DEPEVT_STATUS_BUSERR)
1577 status = -ECONNRESET;
1578
Paul Zimmerman1d046792012-02-15 18:56:56 -08001579 clean_busy = dwc3_cleanup_done_reqs(dwc, dep, event, status);
Paul Zimmermanc2df85c2012-02-24 17:32:18 -08001580 if (clean_busy)
Felipe Balbi72246da2011-08-19 18:10:58 +03001581 dep->flags &= ~DWC3_EP_BUSY;
Felipe Balbifae2b902011-10-14 13:00:30 +03001582
1583 /*
1584 * WORKAROUND: This is the 2nd half of U1/U2 -> U0 workaround.
1585 * See dwc3_gadget_linksts_change_interrupt() for 1st half.
1586 */
1587 if (dwc->revision < DWC3_REVISION_183A) {
1588 u32 reg;
1589 int i;
1590
1591 for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) {
1592 struct dwc3_ep *dep = dwc->eps[i];
1593
1594 if (!(dep->flags & DWC3_EP_ENABLED))
1595 continue;
1596
1597 if (!list_empty(&dep->req_queued))
1598 return;
1599 }
1600
1601 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
1602 reg |= dwc->u1u2;
1603 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1604
1605 dwc->u1u2 = 0;
1606 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001607}
1608
1609static void dwc3_gadget_start_isoc(struct dwc3 *dwc,
1610 struct dwc3_ep *dep, const struct dwc3_event_depevt *event)
1611{
Paul Zimmerman9bafa562012-02-17 14:10:16 -08001612 u32 uf, mask;
Felipe Balbi72246da2011-08-19 18:10:58 +03001613
1614 if (list_empty(&dep->request_list)) {
1615 dev_vdbg(dwc->dev, "ISOC ep %s run out for requests.\n",
1616 dep->name);
1617 return;
1618 }
1619
Paul Zimmerman9bafa562012-02-17 14:10:16 -08001620 mask = ~(dep->interval - 1);
1621 uf = event->parameters & mask;
1622 /* 4 micro frames in the future */
1623 uf += dep->interval * 4;
Felipe Balbi72246da2011-08-19 18:10:58 +03001624
1625 __dwc3_gadget_kick_transfer(dep, uf, 1);
1626}
1627
1628static void dwc3_process_ep_cmd_complete(struct dwc3_ep *dep,
1629 const struct dwc3_event_depevt *event)
1630{
1631 struct dwc3 *dwc = dep->dwc;
1632 struct dwc3_event_depevt mod_ev = *event;
1633
1634 /*
Paul Zimmerman1d046792012-02-15 18:56:56 -08001635 * We were asked to remove one request. It is possible that this
1636 * request and a few others were started together and have the same
Felipe Balbi72246da2011-08-19 18:10:58 +03001637 * transfer index. Since we stopped the complete endpoint we don't
1638 * know how many requests were already completed (and not yet)
1639 * reported and how could be done (later). We purge them all until
1640 * the end of the list.
1641 */
1642 mod_ev.status = DEPEVT_STATUS_LST;
1643 dwc3_cleanup_done_reqs(dwc, dep, &mod_ev, -ESHUTDOWN);
1644 dep->flags &= ~DWC3_EP_BUSY;
Paul Zimmerman1d046792012-02-15 18:56:56 -08001645 /* pending requests are ignored and are queued on XferNotReady */
Felipe Balbi72246da2011-08-19 18:10:58 +03001646}
1647
1648static void dwc3_ep_cmd_compl(struct dwc3_ep *dep,
1649 const struct dwc3_event_depevt *event)
1650{
1651 u32 param = event->parameters;
1652 u32 cmd_type = (param >> 8) & ((1 << 5) - 1);
1653
1654 switch (cmd_type) {
1655 case DWC3_DEPCMD_ENDTRANSFER:
1656 dwc3_process_ep_cmd_complete(dep, event);
1657 break;
1658 case DWC3_DEPCMD_STARTTRANSFER:
1659 dep->res_trans_idx = param & 0x7f;
1660 break;
1661 default:
1662 printk(KERN_ERR "%s() unknown /unexpected type: %d\n",
1663 __func__, cmd_type);
1664 break;
1665 };
1666}
1667
1668static void dwc3_endpoint_interrupt(struct dwc3 *dwc,
1669 const struct dwc3_event_depevt *event)
1670{
1671 struct dwc3_ep *dep;
1672 u8 epnum = event->endpoint_number;
1673
1674 dep = dwc->eps[epnum];
1675
1676 dev_vdbg(dwc->dev, "%s: %s\n", dep->name,
1677 dwc3_ep_event_string(event->endpoint_event));
1678
1679 if (epnum == 0 || epnum == 1) {
1680 dwc3_ep0_interrupt(dwc, event);
1681 return;
1682 }
1683
1684 switch (event->endpoint_event) {
1685 case DWC3_DEPEVT_XFERCOMPLETE:
Paul Zimmermanc2df85c2012-02-24 17:32:18 -08001686 dep->res_trans_idx = 0;
1687
Felipe Balbi72246da2011-08-19 18:10:58 +03001688 if (usb_endpoint_xfer_isoc(dep->desc)) {
1689 dev_dbg(dwc->dev, "%s is an Isochronous endpoint\n",
1690 dep->name);
1691 return;
1692 }
1693
1694 dwc3_endpoint_transfer_complete(dwc, dep, event, 1);
1695 break;
1696 case DWC3_DEPEVT_XFERINPROGRESS:
1697 if (!usb_endpoint_xfer_isoc(dep->desc)) {
1698 dev_dbg(dwc->dev, "%s is not an Isochronous endpoint\n",
1699 dep->name);
1700 return;
1701 }
1702
1703 dwc3_endpoint_transfer_complete(dwc, dep, event, 0);
1704 break;
1705 case DWC3_DEPEVT_XFERNOTREADY:
1706 if (usb_endpoint_xfer_isoc(dep->desc)) {
1707 dwc3_gadget_start_isoc(dwc, dep, event);
1708 } else {
1709 int ret;
1710
1711 dev_vdbg(dwc->dev, "%s: reason %s\n",
Felipe Balbi40aa41f2012-01-18 17:06:03 +02001712 dep->name, event->status &
1713 DEPEVT_STATUS_TRANSFER_ACTIVE
Felipe Balbi72246da2011-08-19 18:10:58 +03001714 ? "Transfer Active"
1715 : "Transfer Not Active");
1716
1717 ret = __dwc3_gadget_kick_transfer(dep, 0, 1);
1718 if (!ret || ret == -EBUSY)
1719 return;
1720
1721 dev_dbg(dwc->dev, "%s: failed to kick transfers\n",
1722 dep->name);
1723 }
1724
1725 break;
Felipe Balbi879631a2011-09-30 10:58:47 +03001726 case DWC3_DEPEVT_STREAMEVT:
1727 if (!usb_endpoint_xfer_bulk(dep->desc)) {
1728 dev_err(dwc->dev, "Stream event for non-Bulk %s\n",
1729 dep->name);
1730 return;
1731 }
1732
1733 switch (event->status) {
1734 case DEPEVT_STREAMEVT_FOUND:
1735 dev_vdbg(dwc->dev, "Stream %d found and started\n",
1736 event->parameters);
1737
1738 break;
1739 case DEPEVT_STREAMEVT_NOTFOUND:
1740 /* FALLTHROUGH */
1741 default:
1742 dev_dbg(dwc->dev, "Couldn't find suitable stream\n");
1743 }
1744 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03001745 case DWC3_DEPEVT_RXTXFIFOEVT:
1746 dev_dbg(dwc->dev, "%s FIFO Overrun\n", dep->name);
1747 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03001748 case DWC3_DEPEVT_EPCMDCMPLT:
1749 dwc3_ep_cmd_compl(dep, event);
1750 break;
1751 }
1752}
1753
1754static void dwc3_disconnect_gadget(struct dwc3 *dwc)
1755{
1756 if (dwc->gadget_driver && dwc->gadget_driver->disconnect) {
1757 spin_unlock(&dwc->lock);
1758 dwc->gadget_driver->disconnect(&dwc->gadget);
1759 spin_lock(&dwc->lock);
1760 }
1761}
1762
1763static void dwc3_stop_active_transfer(struct dwc3 *dwc, u32 epnum)
1764{
1765 struct dwc3_ep *dep;
1766 struct dwc3_gadget_ep_cmd_params params;
1767 u32 cmd;
1768 int ret;
1769
1770 dep = dwc->eps[epnum];
1771
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +02001772 WARN_ON(!dep->res_trans_idx);
Felipe Balbi72246da2011-08-19 18:10:58 +03001773 if (dep->res_trans_idx) {
1774 cmd = DWC3_DEPCMD_ENDTRANSFER;
1775 cmd |= DWC3_DEPCMD_HIPRI_FORCERM | DWC3_DEPCMD_CMDIOC;
1776 cmd |= DWC3_DEPCMD_PARAM(dep->res_trans_idx);
1777 memset(&params, 0, sizeof(params));
1778 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number, cmd, &params);
1779 WARN_ON_ONCE(ret);
Sebastian Andrzej Siewiora1ae9be2011-08-22 17:42:18 +02001780 dep->res_trans_idx = 0;
Felipe Balbibfbd61e2012-10-04 11:58:00 +03001781 dep->flags &= ~DWC3_EP_BUSY;
Felipe Balbi72246da2011-08-19 18:10:58 +03001782 }
1783}
1784
1785static void dwc3_stop_active_transfers(struct dwc3 *dwc)
1786{
1787 u32 epnum;
1788
1789 for (epnum = 2; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
1790 struct dwc3_ep *dep;
1791
1792 dep = dwc->eps[epnum];
1793 if (!(dep->flags & DWC3_EP_ENABLED))
1794 continue;
1795
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +02001796 dwc3_remove_requests(dwc, dep);
Felipe Balbi72246da2011-08-19 18:10:58 +03001797 }
1798}
1799
1800static void dwc3_clear_stall_all_ep(struct dwc3 *dwc)
1801{
1802 u32 epnum;
1803
1804 for (epnum = 1; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
1805 struct dwc3_ep *dep;
1806 struct dwc3_gadget_ep_cmd_params params;
1807 int ret;
1808
1809 dep = dwc->eps[epnum];
1810
1811 if (!(dep->flags & DWC3_EP_STALL))
1812 continue;
1813
1814 dep->flags &= ~DWC3_EP_STALL;
1815
1816 memset(&params, 0, sizeof(params));
1817 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number,
1818 DWC3_DEPCMD_CLEARSTALL, &params);
1819 WARN_ON_ONCE(ret);
1820 }
1821}
1822
1823static void dwc3_gadget_disconnect_interrupt(struct dwc3 *dwc)
1824{
1825 dev_vdbg(dwc->dev, "%s\n", __func__);
1826#if 0
1827 XXX
1828 U1/U2 is powersave optimization. Skip it for now. Anyway we need to
1829 enable it before we can disable it.
1830
1831 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
1832 reg &= ~DWC3_DCTL_INITU1ENA;
1833 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1834
1835 reg &= ~DWC3_DCTL_INITU2ENA;
1836 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1837#endif
1838
1839 dwc3_stop_active_transfers(dwc);
1840 dwc3_disconnect_gadget(dwc);
Paul Zimmermanb23c8432011-09-30 10:58:42 +03001841 dwc->start_config_issued = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03001842
1843 dwc->gadget.speed = USB_SPEED_UNKNOWN;
Felipe Balbidf62df52011-10-14 15:11:49 +03001844 dwc->setup_packet_pending = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03001845}
1846
1847static void dwc3_gadget_usb3_phy_power(struct dwc3 *dwc, int on)
1848{
1849 u32 reg;
1850
1851 reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0));
1852
1853 if (on)
1854 reg &= ~DWC3_GUSB3PIPECTL_SUSPHY;
1855 else
1856 reg |= DWC3_GUSB3PIPECTL_SUSPHY;
1857
1858 dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg);
1859}
1860
1861static void dwc3_gadget_usb2_phy_power(struct dwc3 *dwc, int on)
1862{
1863 u32 reg;
1864
1865 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
1866
1867 if (on)
1868 reg &= ~DWC3_GUSB2PHYCFG_SUSPHY;
1869 else
1870 reg |= DWC3_GUSB2PHYCFG_SUSPHY;
1871
1872 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
1873}
1874
1875static void dwc3_gadget_reset_interrupt(struct dwc3 *dwc)
1876{
1877 u32 reg;
1878
1879 dev_vdbg(dwc->dev, "%s\n", __func__);
1880
Felipe Balbidf62df52011-10-14 15:11:49 +03001881 /*
1882 * WORKAROUND: DWC3 revisions <1.88a have an issue which
1883 * would cause a missing Disconnect Event if there's a
1884 * pending Setup Packet in the FIFO.
1885 *
1886 * There's no suggested workaround on the official Bug
1887 * report, which states that "unless the driver/application
1888 * is doing any special handling of a disconnect event,
1889 * there is no functional issue".
1890 *
1891 * Unfortunately, it turns out that we _do_ some special
1892 * handling of a disconnect event, namely complete all
1893 * pending transfers, notify gadget driver of the
1894 * disconnection, and so on.
1895 *
1896 * Our suggested workaround is to follow the Disconnect
1897 * Event steps here, instead, based on a setup_packet_pending
1898 * flag. Such flag gets set whenever we have a XferNotReady
1899 * event on EP0 and gets cleared on XferComplete for the
1900 * same endpoint.
1901 *
1902 * Refers to:
1903 *
1904 * STAR#9000466709: RTL: Device : Disconnect event not
1905 * generated if setup packet pending in FIFO
1906 */
1907 if (dwc->revision < DWC3_REVISION_188A) {
1908 if (dwc->setup_packet_pending)
1909 dwc3_gadget_disconnect_interrupt(dwc);
1910 }
1911
Felipe Balbi961906e2011-12-20 15:37:21 +02001912 /* after reset -> Default State */
1913 dwc->dev_state = DWC3_DEFAULT_STATE;
1914
Felipe Balbi72246da2011-08-19 18:10:58 +03001915 /* Enable PHYs */
1916 dwc3_gadget_usb2_phy_power(dwc, true);
1917 dwc3_gadget_usb3_phy_power(dwc, true);
1918
1919 if (dwc->gadget.speed != USB_SPEED_UNKNOWN)
1920 dwc3_disconnect_gadget(dwc);
1921
1922 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
1923 reg &= ~DWC3_DCTL_TSTCTRL_MASK;
1924 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Gerard Cauvy3b637362012-02-10 12:21:18 +02001925 dwc->test_mode = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03001926
1927 dwc3_stop_active_transfers(dwc);
1928 dwc3_clear_stall_all_ep(dwc);
Paul Zimmermanb23c8432011-09-30 10:58:42 +03001929 dwc->start_config_issued = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03001930
1931 /* Reset device address to zero */
1932 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
1933 reg &= ~(DWC3_DCFG_DEVADDR_MASK);
1934 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +03001935}
1936
1937static void dwc3_update_ram_clk_sel(struct dwc3 *dwc, u32 speed)
1938{
1939 u32 reg;
1940 u32 usb30_clock = DWC3_GCTL_CLK_BUS;
1941
1942 /*
1943 * We change the clock only at SS but I dunno why I would want to do
1944 * this. Maybe it becomes part of the power saving plan.
1945 */
1946
1947 if (speed != DWC3_DSTS_SUPERSPEED)
1948 return;
1949
1950 /*
1951 * RAMClkSel is reset to 0 after USB reset, so it must be reprogrammed
1952 * each time on Connect Done.
1953 */
1954 if (!usb30_clock)
1955 return;
1956
1957 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
1958 reg |= DWC3_GCTL_RAMCLKSEL(usb30_clock);
1959 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
1960}
1961
1962static void dwc3_gadget_disable_phy(struct dwc3 *dwc, u8 speed)
1963{
1964 switch (speed) {
1965 case USB_SPEED_SUPER:
1966 dwc3_gadget_usb2_phy_power(dwc, false);
1967 break;
1968 case USB_SPEED_HIGH:
1969 case USB_SPEED_FULL:
1970 case USB_SPEED_LOW:
1971 dwc3_gadget_usb3_phy_power(dwc, false);
1972 break;
1973 }
1974}
1975
1976static void dwc3_gadget_conndone_interrupt(struct dwc3 *dwc)
1977{
1978 struct dwc3_gadget_ep_cmd_params params;
1979 struct dwc3_ep *dep;
1980 int ret;
1981 u32 reg;
1982 u8 speed;
1983
1984 dev_vdbg(dwc->dev, "%s\n", __func__);
1985
1986 memset(&params, 0x00, sizeof(params));
1987
Felipe Balbi72246da2011-08-19 18:10:58 +03001988 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1989 speed = reg & DWC3_DSTS_CONNECTSPD;
1990 dwc->speed = speed;
1991
1992 dwc3_update_ram_clk_sel(dwc, speed);
1993
1994 switch (speed) {
1995 case DWC3_DCFG_SUPERSPEED:
Felipe Balbi05870c52011-10-14 14:51:38 +03001996 /*
1997 * WORKAROUND: DWC3 revisions <1.90a have an issue which
1998 * would cause a missing USB3 Reset event.
1999 *
2000 * In such situations, we should force a USB3 Reset
2001 * event by calling our dwc3_gadget_reset_interrupt()
2002 * routine.
2003 *
2004 * Refers to:
2005 *
2006 * STAR#9000483510: RTL: SS : USB3 reset event may
2007 * not be generated always when the link enters poll
2008 */
2009 if (dwc->revision < DWC3_REVISION_190A)
2010 dwc3_gadget_reset_interrupt(dwc);
2011
Felipe Balbi72246da2011-08-19 18:10:58 +03002012 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
2013 dwc->gadget.ep0->maxpacket = 512;
2014 dwc->gadget.speed = USB_SPEED_SUPER;
2015 break;
2016 case DWC3_DCFG_HIGHSPEED:
2017 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
2018 dwc->gadget.ep0->maxpacket = 64;
2019 dwc->gadget.speed = USB_SPEED_HIGH;
2020 break;
2021 case DWC3_DCFG_FULLSPEED2:
2022 case DWC3_DCFG_FULLSPEED1:
2023 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
2024 dwc->gadget.ep0->maxpacket = 64;
2025 dwc->gadget.speed = USB_SPEED_FULL;
2026 break;
2027 case DWC3_DCFG_LOWSPEED:
2028 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(8);
2029 dwc->gadget.ep0->maxpacket = 8;
2030 dwc->gadget.speed = USB_SPEED_LOW;
2031 break;
2032 }
2033
2034 /* Disable unneded PHY */
2035 dwc3_gadget_disable_phy(dwc, dwc->gadget.speed);
2036
2037 dep = dwc->eps[0];
Felipe Balbic90bfae2011-11-29 13:11:21 +02002038 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL);
Felipe Balbi72246da2011-08-19 18:10:58 +03002039 if (ret) {
2040 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
2041 return;
2042 }
2043
2044 dep = dwc->eps[1];
Felipe Balbic90bfae2011-11-29 13:11:21 +02002045 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL);
Felipe Balbi72246da2011-08-19 18:10:58 +03002046 if (ret) {
2047 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
2048 return;
2049 }
2050
2051 /*
2052 * Configure PHY via GUSB3PIPECTLn if required.
2053 *
2054 * Update GTXFIFOSIZn
2055 *
2056 * In both cases reset values should be sufficient.
2057 */
2058}
2059
2060static void dwc3_gadget_wakeup_interrupt(struct dwc3 *dwc)
2061{
2062 dev_vdbg(dwc->dev, "%s\n", __func__);
2063
2064 /*
2065 * TODO take core out of low power mode when that's
2066 * implemented.
2067 */
2068
2069 dwc->gadget_driver->resume(&dwc->gadget);
2070}
2071
2072static void dwc3_gadget_linksts_change_interrupt(struct dwc3 *dwc,
2073 unsigned int evtinfo)
2074{
Felipe Balbifae2b902011-10-14 13:00:30 +03002075 enum dwc3_link_state next = evtinfo & DWC3_LINK_STATE_MASK;
2076
2077 /*
2078 * WORKAROUND: DWC3 Revisions <1.83a have an issue which, depending
2079 * on the link partner, the USB session might do multiple entry/exit
2080 * of low power states before a transfer takes place.
2081 *
2082 * Due to this problem, we might experience lower throughput. The
2083 * suggested workaround is to disable DCTL[12:9] bits if we're
2084 * transitioning from U1/U2 to U0 and enable those bits again
2085 * after a transfer completes and there are no pending transfers
2086 * on any of the enabled endpoints.
2087 *
2088 * This is the first half of that workaround.
2089 *
2090 * Refers to:
2091 *
2092 * STAR#9000446952: RTL: Device SS : if U1/U2 ->U0 takes >128us
2093 * core send LGO_Ux entering U0
2094 */
2095 if (dwc->revision < DWC3_REVISION_183A) {
2096 if (next == DWC3_LINK_STATE_U0) {
2097 u32 u1u2;
2098 u32 reg;
2099
2100 switch (dwc->link_state) {
2101 case DWC3_LINK_STATE_U1:
2102 case DWC3_LINK_STATE_U2:
2103 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2104 u1u2 = reg & (DWC3_DCTL_INITU2ENA
2105 | DWC3_DCTL_ACCEPTU2ENA
2106 | DWC3_DCTL_INITU1ENA
2107 | DWC3_DCTL_ACCEPTU1ENA);
2108
2109 if (!dwc->u1u2)
2110 dwc->u1u2 = reg & u1u2;
2111
2112 reg &= ~u1u2;
2113
2114 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2115 break;
2116 default:
2117 /* do nothing */
2118 break;
2119 }
2120 }
2121 }
2122
2123 dwc->link_state = next;
Felipe Balbi019ac832011-09-08 21:18:47 +03002124
2125 dev_vdbg(dwc->dev, "%s link %d\n", __func__, dwc->link_state);
Felipe Balbi72246da2011-08-19 18:10:58 +03002126}
2127
2128static void dwc3_gadget_interrupt(struct dwc3 *dwc,
2129 const struct dwc3_event_devt *event)
2130{
2131 switch (event->type) {
2132 case DWC3_DEVICE_EVENT_DISCONNECT:
2133 dwc3_gadget_disconnect_interrupt(dwc);
2134 break;
2135 case DWC3_DEVICE_EVENT_RESET:
2136 dwc3_gadget_reset_interrupt(dwc);
2137 break;
2138 case DWC3_DEVICE_EVENT_CONNECT_DONE:
2139 dwc3_gadget_conndone_interrupt(dwc);
2140 break;
2141 case DWC3_DEVICE_EVENT_WAKEUP:
2142 dwc3_gadget_wakeup_interrupt(dwc);
2143 break;
2144 case DWC3_DEVICE_EVENT_LINK_STATUS_CHANGE:
2145 dwc3_gadget_linksts_change_interrupt(dwc, event->event_info);
2146 break;
2147 case DWC3_DEVICE_EVENT_EOPF:
2148 dev_vdbg(dwc->dev, "End of Periodic Frame\n");
2149 break;
2150 case DWC3_DEVICE_EVENT_SOF:
2151 dev_vdbg(dwc->dev, "Start of Periodic Frame\n");
2152 break;
2153 case DWC3_DEVICE_EVENT_ERRATIC_ERROR:
2154 dev_vdbg(dwc->dev, "Erratic Error\n");
2155 break;
2156 case DWC3_DEVICE_EVENT_CMD_CMPL:
2157 dev_vdbg(dwc->dev, "Command Complete\n");
2158 break;
2159 case DWC3_DEVICE_EVENT_OVERFLOW:
2160 dev_vdbg(dwc->dev, "Overflow\n");
2161 break;
2162 default:
2163 dev_dbg(dwc->dev, "UNKNOWN IRQ %d\n", event->type);
2164 }
2165}
2166
2167static void dwc3_process_event_entry(struct dwc3 *dwc,
2168 const union dwc3_event *event)
2169{
2170 /* Endpoint IRQ, handle it and return early */
2171 if (event->type.is_devspec == 0) {
2172 /* depevt */
2173 return dwc3_endpoint_interrupt(dwc, &event->depevt);
2174 }
2175
2176 switch (event->type.type) {
2177 case DWC3_EVENT_TYPE_DEV:
2178 dwc3_gadget_interrupt(dwc, &event->devt);
2179 break;
2180 /* REVISIT what to do with Carkit and I2C events ? */
2181 default:
2182 dev_err(dwc->dev, "UNKNOWN IRQ type %d\n", event->raw);
2183 }
2184}
2185
2186static irqreturn_t dwc3_process_event_buf(struct dwc3 *dwc, u32 buf)
2187{
2188 struct dwc3_event_buffer *evt;
2189 int left;
2190 u32 count;
2191
2192 count = dwc3_readl(dwc->regs, DWC3_GEVNTCOUNT(buf));
2193 count &= DWC3_GEVNTCOUNT_MASK;
2194 if (!count)
2195 return IRQ_NONE;
2196
2197 evt = dwc->ev_buffs[buf];
2198 left = count;
2199
2200 while (left > 0) {
2201 union dwc3_event event;
2202
Felipe Balbid70d8442012-02-06 13:40:17 +02002203 event.raw = *(u32 *) (evt->buf + evt->lpos);
2204
Felipe Balbi72246da2011-08-19 18:10:58 +03002205 dwc3_process_event_entry(dwc, &event);
2206 /*
2207 * XXX we wrap around correctly to the next entry as almost all
2208 * entries are 4 bytes in size. There is one entry which has 12
2209 * bytes which is a regular entry followed by 8 bytes data. ATM
2210 * I don't know how things are organized if were get next to the
2211 * a boundary so I worry about that once we try to handle that.
2212 */
2213 evt->lpos = (evt->lpos + 4) % DWC3_EVENT_BUFFERS_SIZE;
2214 left -= 4;
2215
2216 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(buf), 4);
2217 }
2218
2219 return IRQ_HANDLED;
2220}
2221
2222static irqreturn_t dwc3_interrupt(int irq, void *_dwc)
2223{
2224 struct dwc3 *dwc = _dwc;
2225 int i;
2226 irqreturn_t ret = IRQ_NONE;
2227
2228 spin_lock(&dwc->lock);
2229
Felipe Balbi9f622b22011-10-12 10:31:04 +03002230 for (i = 0; i < dwc->num_event_buffers; i++) {
Felipe Balbi72246da2011-08-19 18:10:58 +03002231 irqreturn_t status;
2232
2233 status = dwc3_process_event_buf(dwc, i);
2234 if (status == IRQ_HANDLED)
2235 ret = status;
2236 }
2237
2238 spin_unlock(&dwc->lock);
2239
2240 return ret;
2241}
2242
2243/**
2244 * dwc3_gadget_init - Initializes gadget related registers
Paul Zimmerman1d046792012-02-15 18:56:56 -08002245 * @dwc: pointer to our controller context structure
Felipe Balbi72246da2011-08-19 18:10:58 +03002246 *
2247 * Returns 0 on success otherwise negative errno.
2248 */
2249int __devinit dwc3_gadget_init(struct dwc3 *dwc)
2250{
2251 u32 reg;
2252 int ret;
2253 int irq;
2254
2255 dwc->ctrl_req = dma_alloc_coherent(dwc->dev, sizeof(*dwc->ctrl_req),
2256 &dwc->ctrl_req_addr, GFP_KERNEL);
2257 if (!dwc->ctrl_req) {
2258 dev_err(dwc->dev, "failed to allocate ctrl request\n");
2259 ret = -ENOMEM;
2260 goto err0;
2261 }
2262
2263 dwc->ep0_trb = dma_alloc_coherent(dwc->dev, sizeof(*dwc->ep0_trb),
2264 &dwc->ep0_trb_addr, GFP_KERNEL);
2265 if (!dwc->ep0_trb) {
2266 dev_err(dwc->dev, "failed to allocate ep0 trb\n");
2267 ret = -ENOMEM;
2268 goto err1;
2269 }
2270
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02002271 dwc->setup_buf = kzalloc(sizeof(*dwc->setup_buf) * 2,
2272 GFP_KERNEL);
Felipe Balbi72246da2011-08-19 18:10:58 +03002273 if (!dwc->setup_buf) {
2274 dev_err(dwc->dev, "failed to allocate setup buffer\n");
2275 ret = -ENOMEM;
2276 goto err2;
2277 }
2278
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002279 dwc->ep0_bounce = dma_alloc_coherent(dwc->dev,
2280 512, &dwc->ep0_bounce_addr, GFP_KERNEL);
2281 if (!dwc->ep0_bounce) {
2282 dev_err(dwc->dev, "failed to allocate ep0 bounce buffer\n");
2283 ret = -ENOMEM;
2284 goto err3;
2285 }
2286
Felipe Balbi72246da2011-08-19 18:10:58 +03002287 dev_set_name(&dwc->gadget.dev, "gadget");
2288
2289 dwc->gadget.ops = &dwc3_gadget_ops;
Michal Nazarewiczd327ab52011-11-19 18:27:37 +01002290 dwc->gadget.max_speed = USB_SPEED_SUPER;
Felipe Balbi72246da2011-08-19 18:10:58 +03002291 dwc->gadget.speed = USB_SPEED_UNKNOWN;
2292 dwc->gadget.dev.parent = dwc->dev;
Felipe Balbieeb720f2011-11-28 12:46:59 +02002293 dwc->gadget.sg_supported = true;
Felipe Balbi72246da2011-08-19 18:10:58 +03002294
2295 dma_set_coherent_mask(&dwc->gadget.dev, dwc->dev->coherent_dma_mask);
2296
2297 dwc->gadget.dev.dma_parms = dwc->dev->dma_parms;
2298 dwc->gadget.dev.dma_mask = dwc->dev->dma_mask;
2299 dwc->gadget.dev.release = dwc3_gadget_release;
2300 dwc->gadget.name = "dwc3-gadget";
2301
2302 /*
2303 * REVISIT: Here we should clear all pending IRQs to be
2304 * sure we're starting from a well known location.
2305 */
2306
2307 ret = dwc3_gadget_init_endpoints(dwc);
2308 if (ret)
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002309 goto err4;
Felipe Balbi72246da2011-08-19 18:10:58 +03002310
2311 irq = platform_get_irq(to_platform_device(dwc->dev), 0);
2312
2313 ret = request_irq(irq, dwc3_interrupt, IRQF_SHARED,
2314 "dwc3", dwc);
2315 if (ret) {
2316 dev_err(dwc->dev, "failed to request irq #%d --> %d\n",
2317 irq, ret);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002318 goto err5;
Felipe Balbi72246da2011-08-19 18:10:58 +03002319 }
2320
2321 /* Enable all but Start and End of Frame IRQs */
2322 reg = (DWC3_DEVTEN_VNDRDEVTSTRCVEDEN |
2323 DWC3_DEVTEN_EVNTOVERFLOWEN |
2324 DWC3_DEVTEN_CMDCMPLTEN |
2325 DWC3_DEVTEN_ERRTICERREN |
2326 DWC3_DEVTEN_WKUPEVTEN |
2327 DWC3_DEVTEN_ULSTCNGEN |
2328 DWC3_DEVTEN_CONNECTDONEEN |
2329 DWC3_DEVTEN_USBRSTEN |
2330 DWC3_DEVTEN_DISCONNEVTEN);
2331 dwc3_writel(dwc->regs, DWC3_DEVTEN, reg);
2332
2333 ret = device_register(&dwc->gadget.dev);
2334 if (ret) {
2335 dev_err(dwc->dev, "failed to register gadget device\n");
2336 put_device(&dwc->gadget.dev);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002337 goto err6;
Felipe Balbi72246da2011-08-19 18:10:58 +03002338 }
2339
2340 ret = usb_add_gadget_udc(dwc->dev, &dwc->gadget);
2341 if (ret) {
2342 dev_err(dwc->dev, "failed to register udc\n");
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002343 goto err7;
Felipe Balbi72246da2011-08-19 18:10:58 +03002344 }
2345
2346 return 0;
2347
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002348err7:
Felipe Balbi72246da2011-08-19 18:10:58 +03002349 device_unregister(&dwc->gadget.dev);
2350
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002351err6:
Felipe Balbi72246da2011-08-19 18:10:58 +03002352 dwc3_writel(dwc->regs, DWC3_DEVTEN, 0x00);
2353 free_irq(irq, dwc);
2354
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002355err5:
Felipe Balbi72246da2011-08-19 18:10:58 +03002356 dwc3_gadget_free_endpoints(dwc);
2357
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002358err4:
2359 dma_free_coherent(dwc->dev, 512, dwc->ep0_bounce,
2360 dwc->ep0_bounce_addr);
2361
Felipe Balbi72246da2011-08-19 18:10:58 +03002362err3:
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02002363 kfree(dwc->setup_buf);
Felipe Balbi72246da2011-08-19 18:10:58 +03002364
2365err2:
2366 dma_free_coherent(dwc->dev, sizeof(*dwc->ep0_trb),
2367 dwc->ep0_trb, dwc->ep0_trb_addr);
2368
2369err1:
2370 dma_free_coherent(dwc->dev, sizeof(*dwc->ctrl_req),
2371 dwc->ctrl_req, dwc->ctrl_req_addr);
2372
2373err0:
2374 return ret;
2375}
2376
2377void dwc3_gadget_exit(struct dwc3 *dwc)
2378{
2379 int irq;
Felipe Balbi72246da2011-08-19 18:10:58 +03002380
2381 usb_del_gadget_udc(&dwc->gadget);
2382 irq = platform_get_irq(to_platform_device(dwc->dev), 0);
2383
2384 dwc3_writel(dwc->regs, DWC3_DEVTEN, 0x00);
2385 free_irq(irq, dwc);
2386
Felipe Balbi72246da2011-08-19 18:10:58 +03002387 dwc3_gadget_free_endpoints(dwc);
2388
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002389 dma_free_coherent(dwc->dev, 512, dwc->ep0_bounce,
2390 dwc->ep0_bounce_addr);
2391
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02002392 kfree(dwc->setup_buf);
Felipe Balbi72246da2011-08-19 18:10:58 +03002393
2394 dma_free_coherent(dwc->dev, sizeof(*dwc->ep0_trb),
2395 dwc->ep0_trb, dwc->ep0_trb_addr);
2396
2397 dma_free_coherent(dwc->dev, sizeof(*dwc->ctrl_req),
2398 dwc->ctrl_req, dwc->ctrl_req_addr);
2399
2400 device_unregister(&dwc->gadget.dev);
2401}