blob: 80e3094c8e1922ccf9882216cee5824383bac33c [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 {
1104 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number,
1105 DWC3_DEPCMD_CLEARSTALL, &params);
1106 if (ret)
1107 dev_err(dwc->dev, "failed to %s STALL on %s\n",
1108 value ? "set" : "clear",
1109 dep->name);
1110 else
Alan Sternf7557c72013-11-01 12:05:12 -04001111 dep->flags &= ~(DWC3_EP_STALL | DWC3_EP_WEDGE);
Felipe Balbi72246da2011-08-19 18:10:58 +03001112 }
Paul Zimmerman52754552011-09-30 10:58:44 +03001113
Felipe Balbi72246da2011-08-19 18:10:58 +03001114 return ret;
1115}
1116
1117static int dwc3_gadget_ep_set_halt(struct usb_ep *ep, int value)
1118{
1119 struct dwc3_ep *dep = to_dwc3_ep(ep);
1120 struct dwc3 *dwc = dep->dwc;
1121
1122 unsigned long flags;
1123
1124 int ret;
1125
1126 spin_lock_irqsave(&dwc->lock, flags);
1127
1128 if (usb_endpoint_xfer_isoc(dep->desc)) {
1129 dev_err(dwc->dev, "%s is of Isochronous type\n", dep->name);
1130 ret = -EINVAL;
1131 goto out;
1132 }
1133
1134 ret = __dwc3_gadget_ep_set_halt(dep, value);
1135out:
1136 spin_unlock_irqrestore(&dwc->lock, flags);
1137
1138 return ret;
1139}
1140
1141static int dwc3_gadget_ep_set_wedge(struct usb_ep *ep)
1142{
1143 struct dwc3_ep *dep = to_dwc3_ep(ep);
Paul Zimmerman249a4562012-02-24 17:32:16 -08001144 struct dwc3 *dwc = dep->dwc;
1145 unsigned long flags;
Felipe Balbi72246da2011-08-19 18:10:58 +03001146
Paul Zimmerman249a4562012-02-24 17:32:16 -08001147 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001148 dep->flags |= DWC3_EP_WEDGE;
Paul Zimmerman249a4562012-02-24 17:32:16 -08001149 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001150
Paul Zimmerman52754552011-09-30 10:58:44 +03001151 return dwc3_gadget_ep_set_halt(ep, 1);
Felipe Balbi72246da2011-08-19 18:10:58 +03001152}
1153
1154/* -------------------------------------------------------------------------- */
1155
1156static struct usb_endpoint_descriptor dwc3_gadget_ep0_desc = {
1157 .bLength = USB_DT_ENDPOINT_SIZE,
1158 .bDescriptorType = USB_DT_ENDPOINT,
1159 .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
1160};
1161
1162static const struct usb_ep_ops dwc3_gadget_ep0_ops = {
1163 .enable = dwc3_gadget_ep0_enable,
1164 .disable = dwc3_gadget_ep0_disable,
1165 .alloc_request = dwc3_gadget_ep_alloc_request,
1166 .free_request = dwc3_gadget_ep_free_request,
1167 .queue = dwc3_gadget_ep0_queue,
1168 .dequeue = dwc3_gadget_ep_dequeue,
1169 .set_halt = dwc3_gadget_ep_set_halt,
1170 .set_wedge = dwc3_gadget_ep_set_wedge,
1171};
1172
1173static const struct usb_ep_ops dwc3_gadget_ep_ops = {
1174 .enable = dwc3_gadget_ep_enable,
1175 .disable = dwc3_gadget_ep_disable,
1176 .alloc_request = dwc3_gadget_ep_alloc_request,
1177 .free_request = dwc3_gadget_ep_free_request,
1178 .queue = dwc3_gadget_ep_queue,
1179 .dequeue = dwc3_gadget_ep_dequeue,
1180 .set_halt = dwc3_gadget_ep_set_halt,
1181 .set_wedge = dwc3_gadget_ep_set_wedge,
1182};
1183
1184/* -------------------------------------------------------------------------- */
1185
1186static int dwc3_gadget_get_frame(struct usb_gadget *g)
1187{
1188 struct dwc3 *dwc = gadget_to_dwc(g);
1189 u32 reg;
1190
1191 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1192 return DWC3_DSTS_SOFFN(reg);
1193}
1194
1195static int dwc3_gadget_wakeup(struct usb_gadget *g)
1196{
1197 struct dwc3 *dwc = gadget_to_dwc(g);
1198
1199 unsigned long timeout;
1200 unsigned long flags;
1201
1202 u32 reg;
1203
1204 int ret = 0;
1205
1206 u8 link_state;
1207 u8 speed;
1208
1209 spin_lock_irqsave(&dwc->lock, flags);
1210
1211 /*
1212 * According to the Databook Remote wakeup request should
1213 * be issued only when the device is in early suspend state.
1214 *
1215 * We can check that via USB Link State bits in DSTS register.
1216 */
1217 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1218
1219 speed = reg & DWC3_DSTS_CONNECTSPD;
1220 if (speed == DWC3_DSTS_SUPERSPEED) {
1221 dev_dbg(dwc->dev, "no wakeup on SuperSpeed\n");
1222 ret = -EINVAL;
1223 goto out;
1224 }
1225
1226 link_state = DWC3_DSTS_USBLNKST(reg);
1227
1228 switch (link_state) {
1229 case DWC3_LINK_STATE_RX_DET: /* in HS, means Early Suspend */
1230 case DWC3_LINK_STATE_U3: /* in HS, means SUSPEND */
1231 break;
1232 default:
1233 dev_dbg(dwc->dev, "can't wakeup from link state %d\n",
1234 link_state);
1235 ret = -EINVAL;
1236 goto out;
1237 }
1238
Felipe Balbi8598bde2012-01-02 18:55:57 +02001239 ret = dwc3_gadget_set_link_state(dwc, DWC3_LINK_STATE_RECOV);
1240 if (ret < 0) {
1241 dev_err(dwc->dev, "failed to put link in Recovery\n");
1242 goto out;
1243 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001244
1245 /* write zeroes to Link Change Request */
1246 reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK;
1247 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1248
Paul Zimmerman1d046792012-02-15 18:56:56 -08001249 /* poll until Link State changes to ON */
Felipe Balbi72246da2011-08-19 18:10:58 +03001250 timeout = jiffies + msecs_to_jiffies(100);
1251
Paul Zimmerman1d046792012-02-15 18:56:56 -08001252 while (!time_after(jiffies, timeout)) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001253 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1254
1255 /* in HS, means ON */
1256 if (DWC3_DSTS_USBLNKST(reg) == DWC3_LINK_STATE_U0)
1257 break;
1258 }
1259
1260 if (DWC3_DSTS_USBLNKST(reg) != DWC3_LINK_STATE_U0) {
1261 dev_err(dwc->dev, "failed to send remote wakeup\n");
1262 ret = -EINVAL;
1263 }
1264
1265out:
1266 spin_unlock_irqrestore(&dwc->lock, flags);
1267
1268 return ret;
1269}
1270
1271static int dwc3_gadget_set_selfpowered(struct usb_gadget *g,
1272 int is_selfpowered)
1273{
1274 struct dwc3 *dwc = gadget_to_dwc(g);
Paul Zimmerman249a4562012-02-24 17:32:16 -08001275 unsigned long flags;
Felipe Balbi72246da2011-08-19 18:10:58 +03001276
Paul Zimmerman249a4562012-02-24 17:32:16 -08001277 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001278 dwc->is_selfpowered = !!is_selfpowered;
Paul Zimmerman249a4562012-02-24 17:32:16 -08001279 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001280
1281 return 0;
1282}
1283
1284static void dwc3_gadget_run_stop(struct dwc3 *dwc, int is_on)
1285{
1286 u32 reg;
Sebastian Andrzej Siewior61d58242011-08-29 16:46:38 +02001287 u32 timeout = 500;
Felipe Balbi72246da2011-08-19 18:10:58 +03001288
1289 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
Felipe Balbi8db7ed12012-01-18 18:32:29 +02001290 if (is_on) {
1291 reg &= ~DWC3_DCTL_TRGTULST_MASK;
1292 reg |= (DWC3_DCTL_RUN_STOP
1293 | DWC3_DCTL_TRGTULST_RX_DET);
1294 } else {
Felipe Balbi72246da2011-08-19 18:10:58 +03001295 reg &= ~DWC3_DCTL_RUN_STOP;
Felipe Balbi8db7ed12012-01-18 18:32:29 +02001296 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001297
1298 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1299
1300 do {
1301 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1302 if (is_on) {
1303 if (!(reg & DWC3_DSTS_DEVCTRLHLT))
1304 break;
1305 } else {
1306 if (reg & DWC3_DSTS_DEVCTRLHLT)
1307 break;
1308 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001309 timeout--;
1310 if (!timeout)
1311 break;
Sebastian Andrzej Siewior61d58242011-08-29 16:46:38 +02001312 udelay(1);
Felipe Balbi72246da2011-08-19 18:10:58 +03001313 } while (1);
1314
1315 dev_vdbg(dwc->dev, "gadget %s data soft-%s\n",
1316 dwc->gadget_driver
1317 ? dwc->gadget_driver->function : "no-function",
1318 is_on ? "connect" : "disconnect");
1319}
1320
1321static int dwc3_gadget_pullup(struct usb_gadget *g, int is_on)
1322{
1323 struct dwc3 *dwc = gadget_to_dwc(g);
1324 unsigned long flags;
1325
1326 is_on = !!is_on;
1327
1328 spin_lock_irqsave(&dwc->lock, flags);
1329 dwc3_gadget_run_stop(dwc, is_on);
1330 spin_unlock_irqrestore(&dwc->lock, flags);
1331
1332 return 0;
1333}
1334
1335static int dwc3_gadget_start(struct usb_gadget *g,
1336 struct usb_gadget_driver *driver)
1337{
1338 struct dwc3 *dwc = gadget_to_dwc(g);
1339 struct dwc3_ep *dep;
1340 unsigned long flags;
1341 int ret = 0;
1342 u32 reg;
1343
1344 spin_lock_irqsave(&dwc->lock, flags);
1345
1346 if (dwc->gadget_driver) {
1347 dev_err(dwc->dev, "%s is already bound to %s\n",
1348 dwc->gadget.name,
1349 dwc->gadget_driver->driver.name);
1350 ret = -EBUSY;
1351 goto err0;
1352 }
1353
1354 dwc->gadget_driver = driver;
1355 dwc->gadget.dev.driver = &driver->driver;
1356
Felipe Balbi72246da2011-08-19 18:10:58 +03001357 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
1358 reg &= ~(DWC3_DCFG_SPEED_MASK);
Felipe Balbi6c167fc2011-10-07 22:55:04 +03001359 reg |= dwc->maximum_speed;
Felipe Balbi72246da2011-08-19 18:10:58 +03001360 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
1361
Paul Zimmermanb23c8432011-09-30 10:58:42 +03001362 dwc->start_config_issued = false;
1363
Felipe Balbi72246da2011-08-19 18:10:58 +03001364 /* Start with SuperSpeed Default */
1365 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
1366
1367 dep = dwc->eps[0];
Felipe Balbic90bfae2011-11-29 13:11:21 +02001368 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL);
Felipe Balbi72246da2011-08-19 18:10:58 +03001369 if (ret) {
1370 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
1371 goto err0;
1372 }
1373
1374 dep = dwc->eps[1];
Felipe Balbic90bfae2011-11-29 13:11:21 +02001375 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL);
Felipe Balbi72246da2011-08-19 18:10:58 +03001376 if (ret) {
1377 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
1378 goto err1;
1379 }
1380
1381 /* begin to receive SETUP packets */
Felipe Balbic7fcdeb2011-08-27 22:28:36 +03001382 dwc->ep0state = EP0_SETUP_PHASE;
Felipe Balbi72246da2011-08-19 18:10:58 +03001383 dwc3_ep0_out_start(dwc);
1384
1385 spin_unlock_irqrestore(&dwc->lock, flags);
1386
1387 return 0;
1388
1389err1:
1390 __dwc3_gadget_ep_disable(dwc->eps[0]);
1391
1392err0:
Felipe Balbi1b5c94b2013-07-15 12:36:35 +03001393 dwc->gadget_driver = NULL;
Felipe Balbi72246da2011-08-19 18:10:58 +03001394 spin_unlock_irqrestore(&dwc->lock, flags);
1395
1396 return ret;
1397}
1398
1399static int dwc3_gadget_stop(struct usb_gadget *g,
1400 struct usb_gadget_driver *driver)
1401{
1402 struct dwc3 *dwc = gadget_to_dwc(g);
1403 unsigned long flags;
1404
1405 spin_lock_irqsave(&dwc->lock, flags);
1406
1407 __dwc3_gadget_ep_disable(dwc->eps[0]);
1408 __dwc3_gadget_ep_disable(dwc->eps[1]);
1409
1410 dwc->gadget_driver = NULL;
1411 dwc->gadget.dev.driver = NULL;
1412
1413 spin_unlock_irqrestore(&dwc->lock, flags);
1414
1415 return 0;
1416}
1417static const struct usb_gadget_ops dwc3_gadget_ops = {
1418 .get_frame = dwc3_gadget_get_frame,
1419 .wakeup = dwc3_gadget_wakeup,
1420 .set_selfpowered = dwc3_gadget_set_selfpowered,
1421 .pullup = dwc3_gadget_pullup,
1422 .udc_start = dwc3_gadget_start,
1423 .udc_stop = dwc3_gadget_stop,
1424};
1425
1426/* -------------------------------------------------------------------------- */
1427
1428static int __devinit dwc3_gadget_init_endpoints(struct dwc3 *dwc)
1429{
1430 struct dwc3_ep *dep;
1431 u8 epnum;
1432
1433 INIT_LIST_HEAD(&dwc->gadget.ep_list);
1434
1435 for (epnum = 0; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
1436 dep = kzalloc(sizeof(*dep), GFP_KERNEL);
1437 if (!dep) {
1438 dev_err(dwc->dev, "can't allocate endpoint %d\n",
1439 epnum);
1440 return -ENOMEM;
1441 }
1442
1443 dep->dwc = dwc;
1444 dep->number = epnum;
1445 dwc->eps[epnum] = dep;
1446
1447 snprintf(dep->name, sizeof(dep->name), "ep%d%s", epnum >> 1,
1448 (epnum & 1) ? "in" : "out");
1449 dep->endpoint.name = dep->name;
1450 dep->direction = (epnum & 1);
1451
1452 if (epnum == 0 || epnum == 1) {
1453 dep->endpoint.maxpacket = 512;
Pratyush Anand462434e2013-01-18 16:53:56 +05301454 dep->endpoint.maxburst = 1;
Felipe Balbi72246da2011-08-19 18:10:58 +03001455 dep->endpoint.ops = &dwc3_gadget_ep0_ops;
1456 if (!epnum)
1457 dwc->gadget.ep0 = &dep->endpoint;
1458 } else {
1459 int ret;
1460
1461 dep->endpoint.maxpacket = 1024;
Sebastian Andrzej Siewior12d36c12011-11-03 20:27:50 +01001462 dep->endpoint.max_streams = 15;
Felipe Balbi72246da2011-08-19 18:10:58 +03001463 dep->endpoint.ops = &dwc3_gadget_ep_ops;
1464 list_add_tail(&dep->endpoint.ep_list,
1465 &dwc->gadget.ep_list);
1466
1467 ret = dwc3_alloc_trb_pool(dep);
Felipe Balbi25b8ff62011-11-04 12:32:47 +02001468 if (ret)
Felipe Balbi72246da2011-08-19 18:10:58 +03001469 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001470 }
Felipe Balbi25b8ff62011-11-04 12:32:47 +02001471
Felipe Balbi72246da2011-08-19 18:10:58 +03001472 INIT_LIST_HEAD(&dep->request_list);
1473 INIT_LIST_HEAD(&dep->req_queued);
1474 }
1475
1476 return 0;
1477}
1478
1479static void dwc3_gadget_free_endpoints(struct dwc3 *dwc)
1480{
1481 struct dwc3_ep *dep;
1482 u8 epnum;
1483
1484 for (epnum = 0; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
1485 dep = dwc->eps[epnum];
1486 dwc3_free_trb_pool(dep);
1487
1488 if (epnum != 0 && epnum != 1)
1489 list_del(&dep->endpoint.ep_list);
1490
1491 kfree(dep);
1492 }
1493}
1494
1495static void dwc3_gadget_release(struct device *dev)
1496{
1497 dev_dbg(dev, "%s\n", __func__);
1498}
1499
1500/* -------------------------------------------------------------------------- */
1501static int dwc3_cleanup_done_reqs(struct dwc3 *dwc, struct dwc3_ep *dep,
1502 const struct dwc3_event_depevt *event, int status)
1503{
1504 struct dwc3_request *req;
Felipe Balbif6bafc62012-02-06 11:04:53 +02001505 struct dwc3_trb *trb;
Felipe Balbi72246da2011-08-19 18:10:58 +03001506 unsigned int count;
1507 unsigned int s_pkt = 0;
1508
1509 do {
1510 req = next_request(&dep->req_queued);
Sebastian Andrzej Siewiord39ee7b2011-11-03 10:32:20 +01001511 if (!req) {
1512 WARN_ON_ONCE(1);
1513 return 1;
1514 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001515
Felipe Balbif6bafc62012-02-06 11:04:53 +02001516 trb = req->trb;
Felipe Balbi72246da2011-08-19 18:10:58 +03001517
Felipe Balbif6bafc62012-02-06 11:04:53 +02001518 if ((trb->ctrl & DWC3_TRB_CTRL_HWO) && status != -ESHUTDOWN)
Sebastian Andrzej Siewior0d2f4752011-08-19 19:59:12 +02001519 /*
1520 * We continue despite the error. There is not much we
Paul Zimmerman1d046792012-02-15 18:56:56 -08001521 * can do. If we don't clean it up we loop forever. If
1522 * we skip the TRB then it gets overwritten after a
1523 * while since we use them in a ring buffer. A BUG()
1524 * would help. Lets hope that if this occurs, someone
Sebastian Andrzej Siewior0d2f4752011-08-19 19:59:12 +02001525 * fixes the root cause instead of looking away :)
1526 */
Felipe Balbi72246da2011-08-19 18:10:58 +03001527 dev_err(dwc->dev, "%s's TRB (%p) still owned by HW\n",
1528 dep->name, req->trb);
Felipe Balbif6bafc62012-02-06 11:04:53 +02001529 count = trb->size & DWC3_TRB_SIZE_MASK;
Felipe Balbi72246da2011-08-19 18:10:58 +03001530
1531 if (dep->direction) {
1532 if (count) {
1533 dev_err(dwc->dev, "incomplete IN transfer %s\n",
1534 dep->name);
1535 status = -ECONNRESET;
1536 }
1537 } else {
1538 if (count && (event->status & DEPEVT_STATUS_SHORT))
1539 s_pkt = 1;
1540 }
1541
1542 /*
1543 * We assume here we will always receive the entire data block
1544 * which we should receive. Meaning, if we program RX to
1545 * receive 4K but we receive only 2K, we assume that's all we
1546 * should receive and we simply bounce the request back to the
1547 * gadget driver for further processing.
1548 */
1549 req->request.actual += req->request.length - count;
1550 dwc3_gadget_giveback(dep, req, status);
1551 if (s_pkt)
1552 break;
Felipe Balbif6bafc62012-02-06 11:04:53 +02001553 if ((event->status & DEPEVT_STATUS_LST) &&
1554 (trb->ctrl & DWC3_TRB_CTRL_LST))
Felipe Balbi72246da2011-08-19 18:10:58 +03001555 break;
Felipe Balbif6bafc62012-02-06 11:04:53 +02001556 if ((event->status & DEPEVT_STATUS_IOC) &&
1557 (trb->ctrl & DWC3_TRB_CTRL_IOC))
Felipe Balbi72246da2011-08-19 18:10:58 +03001558 break;
1559 } while (1);
1560
Felipe Balbif6bafc62012-02-06 11:04:53 +02001561 if ((event->status & DEPEVT_STATUS_IOC) &&
1562 (trb->ctrl & DWC3_TRB_CTRL_IOC))
Felipe Balbi72246da2011-08-19 18:10:58 +03001563 return 0;
1564 return 1;
1565}
1566
1567static void dwc3_endpoint_transfer_complete(struct dwc3 *dwc,
1568 struct dwc3_ep *dep, const struct dwc3_event_depevt *event,
1569 int start_new)
1570{
1571 unsigned status = 0;
1572 int clean_busy;
1573
1574 if (event->status & DEPEVT_STATUS_BUSERR)
1575 status = -ECONNRESET;
1576
Paul Zimmerman1d046792012-02-15 18:56:56 -08001577 clean_busy = dwc3_cleanup_done_reqs(dwc, dep, event, status);
Paul Zimmermanc2df85c2012-02-24 17:32:18 -08001578 if (clean_busy)
Felipe Balbi72246da2011-08-19 18:10:58 +03001579 dep->flags &= ~DWC3_EP_BUSY;
Felipe Balbifae2b902011-10-14 13:00:30 +03001580
1581 /*
1582 * WORKAROUND: This is the 2nd half of U1/U2 -> U0 workaround.
1583 * See dwc3_gadget_linksts_change_interrupt() for 1st half.
1584 */
1585 if (dwc->revision < DWC3_REVISION_183A) {
1586 u32 reg;
1587 int i;
1588
1589 for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) {
1590 struct dwc3_ep *dep = dwc->eps[i];
1591
1592 if (!(dep->flags & DWC3_EP_ENABLED))
1593 continue;
1594
1595 if (!list_empty(&dep->req_queued))
1596 return;
1597 }
1598
1599 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
1600 reg |= dwc->u1u2;
1601 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1602
1603 dwc->u1u2 = 0;
1604 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001605}
1606
1607static void dwc3_gadget_start_isoc(struct dwc3 *dwc,
1608 struct dwc3_ep *dep, const struct dwc3_event_depevt *event)
1609{
Paul Zimmerman9bafa562012-02-17 14:10:16 -08001610 u32 uf, mask;
Felipe Balbi72246da2011-08-19 18:10:58 +03001611
1612 if (list_empty(&dep->request_list)) {
1613 dev_vdbg(dwc->dev, "ISOC ep %s run out for requests.\n",
1614 dep->name);
1615 return;
1616 }
1617
Paul Zimmerman9bafa562012-02-17 14:10:16 -08001618 mask = ~(dep->interval - 1);
1619 uf = event->parameters & mask;
1620 /* 4 micro frames in the future */
1621 uf += dep->interval * 4;
Felipe Balbi72246da2011-08-19 18:10:58 +03001622
1623 __dwc3_gadget_kick_transfer(dep, uf, 1);
1624}
1625
1626static void dwc3_process_ep_cmd_complete(struct dwc3_ep *dep,
1627 const struct dwc3_event_depevt *event)
1628{
1629 struct dwc3 *dwc = dep->dwc;
1630 struct dwc3_event_depevt mod_ev = *event;
1631
1632 /*
Paul Zimmerman1d046792012-02-15 18:56:56 -08001633 * We were asked to remove one request. It is possible that this
1634 * request and a few others were started together and have the same
Felipe Balbi72246da2011-08-19 18:10:58 +03001635 * transfer index. Since we stopped the complete endpoint we don't
1636 * know how many requests were already completed (and not yet)
1637 * reported and how could be done (later). We purge them all until
1638 * the end of the list.
1639 */
1640 mod_ev.status = DEPEVT_STATUS_LST;
1641 dwc3_cleanup_done_reqs(dwc, dep, &mod_ev, -ESHUTDOWN);
1642 dep->flags &= ~DWC3_EP_BUSY;
Paul Zimmerman1d046792012-02-15 18:56:56 -08001643 /* pending requests are ignored and are queued on XferNotReady */
Felipe Balbi72246da2011-08-19 18:10:58 +03001644}
1645
1646static void dwc3_ep_cmd_compl(struct dwc3_ep *dep,
1647 const struct dwc3_event_depevt *event)
1648{
1649 u32 param = event->parameters;
1650 u32 cmd_type = (param >> 8) & ((1 << 5) - 1);
1651
1652 switch (cmd_type) {
1653 case DWC3_DEPCMD_ENDTRANSFER:
1654 dwc3_process_ep_cmd_complete(dep, event);
1655 break;
1656 case DWC3_DEPCMD_STARTTRANSFER:
1657 dep->res_trans_idx = param & 0x7f;
1658 break;
1659 default:
1660 printk(KERN_ERR "%s() unknown /unexpected type: %d\n",
1661 __func__, cmd_type);
1662 break;
1663 };
1664}
1665
1666static void dwc3_endpoint_interrupt(struct dwc3 *dwc,
1667 const struct dwc3_event_depevt *event)
1668{
1669 struct dwc3_ep *dep;
1670 u8 epnum = event->endpoint_number;
1671
1672 dep = dwc->eps[epnum];
1673
1674 dev_vdbg(dwc->dev, "%s: %s\n", dep->name,
1675 dwc3_ep_event_string(event->endpoint_event));
1676
1677 if (epnum == 0 || epnum == 1) {
1678 dwc3_ep0_interrupt(dwc, event);
1679 return;
1680 }
1681
1682 switch (event->endpoint_event) {
1683 case DWC3_DEPEVT_XFERCOMPLETE:
Paul Zimmermanc2df85c2012-02-24 17:32:18 -08001684 dep->res_trans_idx = 0;
1685
Felipe Balbi72246da2011-08-19 18:10:58 +03001686 if (usb_endpoint_xfer_isoc(dep->desc)) {
1687 dev_dbg(dwc->dev, "%s is an Isochronous endpoint\n",
1688 dep->name);
1689 return;
1690 }
1691
1692 dwc3_endpoint_transfer_complete(dwc, dep, event, 1);
1693 break;
1694 case DWC3_DEPEVT_XFERINPROGRESS:
1695 if (!usb_endpoint_xfer_isoc(dep->desc)) {
1696 dev_dbg(dwc->dev, "%s is not an Isochronous endpoint\n",
1697 dep->name);
1698 return;
1699 }
1700
1701 dwc3_endpoint_transfer_complete(dwc, dep, event, 0);
1702 break;
1703 case DWC3_DEPEVT_XFERNOTREADY:
1704 if (usb_endpoint_xfer_isoc(dep->desc)) {
1705 dwc3_gadget_start_isoc(dwc, dep, event);
1706 } else {
1707 int ret;
1708
1709 dev_vdbg(dwc->dev, "%s: reason %s\n",
Felipe Balbi40aa41f2012-01-18 17:06:03 +02001710 dep->name, event->status &
1711 DEPEVT_STATUS_TRANSFER_ACTIVE
Felipe Balbi72246da2011-08-19 18:10:58 +03001712 ? "Transfer Active"
1713 : "Transfer Not Active");
1714
1715 ret = __dwc3_gadget_kick_transfer(dep, 0, 1);
1716 if (!ret || ret == -EBUSY)
1717 return;
1718
1719 dev_dbg(dwc->dev, "%s: failed to kick transfers\n",
1720 dep->name);
1721 }
1722
1723 break;
Felipe Balbi879631a2011-09-30 10:58:47 +03001724 case DWC3_DEPEVT_STREAMEVT:
1725 if (!usb_endpoint_xfer_bulk(dep->desc)) {
1726 dev_err(dwc->dev, "Stream event for non-Bulk %s\n",
1727 dep->name);
1728 return;
1729 }
1730
1731 switch (event->status) {
1732 case DEPEVT_STREAMEVT_FOUND:
1733 dev_vdbg(dwc->dev, "Stream %d found and started\n",
1734 event->parameters);
1735
1736 break;
1737 case DEPEVT_STREAMEVT_NOTFOUND:
1738 /* FALLTHROUGH */
1739 default:
1740 dev_dbg(dwc->dev, "Couldn't find suitable stream\n");
1741 }
1742 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03001743 case DWC3_DEPEVT_RXTXFIFOEVT:
1744 dev_dbg(dwc->dev, "%s FIFO Overrun\n", dep->name);
1745 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03001746 case DWC3_DEPEVT_EPCMDCMPLT:
1747 dwc3_ep_cmd_compl(dep, event);
1748 break;
1749 }
1750}
1751
1752static void dwc3_disconnect_gadget(struct dwc3 *dwc)
1753{
1754 if (dwc->gadget_driver && dwc->gadget_driver->disconnect) {
1755 spin_unlock(&dwc->lock);
1756 dwc->gadget_driver->disconnect(&dwc->gadget);
1757 spin_lock(&dwc->lock);
1758 }
1759}
1760
1761static void dwc3_stop_active_transfer(struct dwc3 *dwc, u32 epnum)
1762{
1763 struct dwc3_ep *dep;
1764 struct dwc3_gadget_ep_cmd_params params;
1765 u32 cmd;
1766 int ret;
1767
1768 dep = dwc->eps[epnum];
1769
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +02001770 WARN_ON(!dep->res_trans_idx);
Felipe Balbi72246da2011-08-19 18:10:58 +03001771 if (dep->res_trans_idx) {
1772 cmd = DWC3_DEPCMD_ENDTRANSFER;
1773 cmd |= DWC3_DEPCMD_HIPRI_FORCERM | DWC3_DEPCMD_CMDIOC;
1774 cmd |= DWC3_DEPCMD_PARAM(dep->res_trans_idx);
1775 memset(&params, 0, sizeof(params));
1776 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number, cmd, &params);
1777 WARN_ON_ONCE(ret);
Sebastian Andrzej Siewiora1ae9be2011-08-22 17:42:18 +02001778 dep->res_trans_idx = 0;
Felipe Balbibfbd61e2012-10-04 11:58:00 +03001779 dep->flags &= ~DWC3_EP_BUSY;
Felipe Balbi72246da2011-08-19 18:10:58 +03001780 }
1781}
1782
1783static void dwc3_stop_active_transfers(struct dwc3 *dwc)
1784{
1785 u32 epnum;
1786
1787 for (epnum = 2; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
1788 struct dwc3_ep *dep;
1789
1790 dep = dwc->eps[epnum];
1791 if (!(dep->flags & DWC3_EP_ENABLED))
1792 continue;
1793
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +02001794 dwc3_remove_requests(dwc, dep);
Felipe Balbi72246da2011-08-19 18:10:58 +03001795 }
1796}
1797
1798static void dwc3_clear_stall_all_ep(struct dwc3 *dwc)
1799{
1800 u32 epnum;
1801
1802 for (epnum = 1; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
1803 struct dwc3_ep *dep;
1804 struct dwc3_gadget_ep_cmd_params params;
1805 int ret;
1806
1807 dep = dwc->eps[epnum];
1808
1809 if (!(dep->flags & DWC3_EP_STALL))
1810 continue;
1811
1812 dep->flags &= ~DWC3_EP_STALL;
1813
1814 memset(&params, 0, sizeof(params));
1815 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number,
1816 DWC3_DEPCMD_CLEARSTALL, &params);
1817 WARN_ON_ONCE(ret);
1818 }
1819}
1820
1821static void dwc3_gadget_disconnect_interrupt(struct dwc3 *dwc)
1822{
1823 dev_vdbg(dwc->dev, "%s\n", __func__);
1824#if 0
1825 XXX
1826 U1/U2 is powersave optimization. Skip it for now. Anyway we need to
1827 enable it before we can disable it.
1828
1829 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
1830 reg &= ~DWC3_DCTL_INITU1ENA;
1831 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1832
1833 reg &= ~DWC3_DCTL_INITU2ENA;
1834 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1835#endif
1836
1837 dwc3_stop_active_transfers(dwc);
1838 dwc3_disconnect_gadget(dwc);
Paul Zimmermanb23c8432011-09-30 10:58:42 +03001839 dwc->start_config_issued = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03001840
1841 dwc->gadget.speed = USB_SPEED_UNKNOWN;
Felipe Balbidf62df52011-10-14 15:11:49 +03001842 dwc->setup_packet_pending = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03001843}
1844
1845static void dwc3_gadget_usb3_phy_power(struct dwc3 *dwc, int on)
1846{
1847 u32 reg;
1848
1849 reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0));
1850
1851 if (on)
1852 reg &= ~DWC3_GUSB3PIPECTL_SUSPHY;
1853 else
1854 reg |= DWC3_GUSB3PIPECTL_SUSPHY;
1855
1856 dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg);
1857}
1858
1859static void dwc3_gadget_usb2_phy_power(struct dwc3 *dwc, int on)
1860{
1861 u32 reg;
1862
1863 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
1864
1865 if (on)
1866 reg &= ~DWC3_GUSB2PHYCFG_SUSPHY;
1867 else
1868 reg |= DWC3_GUSB2PHYCFG_SUSPHY;
1869
1870 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
1871}
1872
1873static void dwc3_gadget_reset_interrupt(struct dwc3 *dwc)
1874{
1875 u32 reg;
1876
1877 dev_vdbg(dwc->dev, "%s\n", __func__);
1878
Felipe Balbidf62df52011-10-14 15:11:49 +03001879 /*
1880 * WORKAROUND: DWC3 revisions <1.88a have an issue which
1881 * would cause a missing Disconnect Event if there's a
1882 * pending Setup Packet in the FIFO.
1883 *
1884 * There's no suggested workaround on the official Bug
1885 * report, which states that "unless the driver/application
1886 * is doing any special handling of a disconnect event,
1887 * there is no functional issue".
1888 *
1889 * Unfortunately, it turns out that we _do_ some special
1890 * handling of a disconnect event, namely complete all
1891 * pending transfers, notify gadget driver of the
1892 * disconnection, and so on.
1893 *
1894 * Our suggested workaround is to follow the Disconnect
1895 * Event steps here, instead, based on a setup_packet_pending
1896 * flag. Such flag gets set whenever we have a XferNotReady
1897 * event on EP0 and gets cleared on XferComplete for the
1898 * same endpoint.
1899 *
1900 * Refers to:
1901 *
1902 * STAR#9000466709: RTL: Device : Disconnect event not
1903 * generated if setup packet pending in FIFO
1904 */
1905 if (dwc->revision < DWC3_REVISION_188A) {
1906 if (dwc->setup_packet_pending)
1907 dwc3_gadget_disconnect_interrupt(dwc);
1908 }
1909
Felipe Balbi961906e2011-12-20 15:37:21 +02001910 /* after reset -> Default State */
1911 dwc->dev_state = DWC3_DEFAULT_STATE;
1912
Felipe Balbi72246da2011-08-19 18:10:58 +03001913 /* Enable PHYs */
1914 dwc3_gadget_usb2_phy_power(dwc, true);
1915 dwc3_gadget_usb3_phy_power(dwc, true);
1916
1917 if (dwc->gadget.speed != USB_SPEED_UNKNOWN)
1918 dwc3_disconnect_gadget(dwc);
1919
1920 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
1921 reg &= ~DWC3_DCTL_TSTCTRL_MASK;
1922 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Gerard Cauvy3b637362012-02-10 12:21:18 +02001923 dwc->test_mode = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03001924
1925 dwc3_stop_active_transfers(dwc);
1926 dwc3_clear_stall_all_ep(dwc);
Paul Zimmermanb23c8432011-09-30 10:58:42 +03001927 dwc->start_config_issued = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03001928
1929 /* Reset device address to zero */
1930 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
1931 reg &= ~(DWC3_DCFG_DEVADDR_MASK);
1932 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +03001933}
1934
1935static void dwc3_update_ram_clk_sel(struct dwc3 *dwc, u32 speed)
1936{
1937 u32 reg;
1938 u32 usb30_clock = DWC3_GCTL_CLK_BUS;
1939
1940 /*
1941 * We change the clock only at SS but I dunno why I would want to do
1942 * this. Maybe it becomes part of the power saving plan.
1943 */
1944
1945 if (speed != DWC3_DSTS_SUPERSPEED)
1946 return;
1947
1948 /*
1949 * RAMClkSel is reset to 0 after USB reset, so it must be reprogrammed
1950 * each time on Connect Done.
1951 */
1952 if (!usb30_clock)
1953 return;
1954
1955 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
1956 reg |= DWC3_GCTL_RAMCLKSEL(usb30_clock);
1957 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
1958}
1959
1960static void dwc3_gadget_disable_phy(struct dwc3 *dwc, u8 speed)
1961{
1962 switch (speed) {
1963 case USB_SPEED_SUPER:
1964 dwc3_gadget_usb2_phy_power(dwc, false);
1965 break;
1966 case USB_SPEED_HIGH:
1967 case USB_SPEED_FULL:
1968 case USB_SPEED_LOW:
1969 dwc3_gadget_usb3_phy_power(dwc, false);
1970 break;
1971 }
1972}
1973
1974static void dwc3_gadget_conndone_interrupt(struct dwc3 *dwc)
1975{
1976 struct dwc3_gadget_ep_cmd_params params;
1977 struct dwc3_ep *dep;
1978 int ret;
1979 u32 reg;
1980 u8 speed;
1981
1982 dev_vdbg(dwc->dev, "%s\n", __func__);
1983
1984 memset(&params, 0x00, sizeof(params));
1985
Felipe Balbi72246da2011-08-19 18:10:58 +03001986 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1987 speed = reg & DWC3_DSTS_CONNECTSPD;
1988 dwc->speed = speed;
1989
1990 dwc3_update_ram_clk_sel(dwc, speed);
1991
1992 switch (speed) {
1993 case DWC3_DCFG_SUPERSPEED:
Felipe Balbi05870c52011-10-14 14:51:38 +03001994 /*
1995 * WORKAROUND: DWC3 revisions <1.90a have an issue which
1996 * would cause a missing USB3 Reset event.
1997 *
1998 * In such situations, we should force a USB3 Reset
1999 * event by calling our dwc3_gadget_reset_interrupt()
2000 * routine.
2001 *
2002 * Refers to:
2003 *
2004 * STAR#9000483510: RTL: SS : USB3 reset event may
2005 * not be generated always when the link enters poll
2006 */
2007 if (dwc->revision < DWC3_REVISION_190A)
2008 dwc3_gadget_reset_interrupt(dwc);
2009
Felipe Balbi72246da2011-08-19 18:10:58 +03002010 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
2011 dwc->gadget.ep0->maxpacket = 512;
2012 dwc->gadget.speed = USB_SPEED_SUPER;
2013 break;
2014 case DWC3_DCFG_HIGHSPEED:
2015 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
2016 dwc->gadget.ep0->maxpacket = 64;
2017 dwc->gadget.speed = USB_SPEED_HIGH;
2018 break;
2019 case DWC3_DCFG_FULLSPEED2:
2020 case DWC3_DCFG_FULLSPEED1:
2021 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
2022 dwc->gadget.ep0->maxpacket = 64;
2023 dwc->gadget.speed = USB_SPEED_FULL;
2024 break;
2025 case DWC3_DCFG_LOWSPEED:
2026 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(8);
2027 dwc->gadget.ep0->maxpacket = 8;
2028 dwc->gadget.speed = USB_SPEED_LOW;
2029 break;
2030 }
2031
2032 /* Disable unneded PHY */
2033 dwc3_gadget_disable_phy(dwc, dwc->gadget.speed);
2034
2035 dep = dwc->eps[0];
Felipe Balbic90bfae2011-11-29 13:11:21 +02002036 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL);
Felipe Balbi72246da2011-08-19 18:10:58 +03002037 if (ret) {
2038 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
2039 return;
2040 }
2041
2042 dep = dwc->eps[1];
Felipe Balbic90bfae2011-11-29 13:11:21 +02002043 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL);
Felipe Balbi72246da2011-08-19 18:10:58 +03002044 if (ret) {
2045 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
2046 return;
2047 }
2048
2049 /*
2050 * Configure PHY via GUSB3PIPECTLn if required.
2051 *
2052 * Update GTXFIFOSIZn
2053 *
2054 * In both cases reset values should be sufficient.
2055 */
2056}
2057
2058static void dwc3_gadget_wakeup_interrupt(struct dwc3 *dwc)
2059{
2060 dev_vdbg(dwc->dev, "%s\n", __func__);
2061
2062 /*
2063 * TODO take core out of low power mode when that's
2064 * implemented.
2065 */
2066
2067 dwc->gadget_driver->resume(&dwc->gadget);
2068}
2069
2070static void dwc3_gadget_linksts_change_interrupt(struct dwc3 *dwc,
2071 unsigned int evtinfo)
2072{
Felipe Balbifae2b902011-10-14 13:00:30 +03002073 enum dwc3_link_state next = evtinfo & DWC3_LINK_STATE_MASK;
2074
2075 /*
2076 * WORKAROUND: DWC3 Revisions <1.83a have an issue which, depending
2077 * on the link partner, the USB session might do multiple entry/exit
2078 * of low power states before a transfer takes place.
2079 *
2080 * Due to this problem, we might experience lower throughput. The
2081 * suggested workaround is to disable DCTL[12:9] bits if we're
2082 * transitioning from U1/U2 to U0 and enable those bits again
2083 * after a transfer completes and there are no pending transfers
2084 * on any of the enabled endpoints.
2085 *
2086 * This is the first half of that workaround.
2087 *
2088 * Refers to:
2089 *
2090 * STAR#9000446952: RTL: Device SS : if U1/U2 ->U0 takes >128us
2091 * core send LGO_Ux entering U0
2092 */
2093 if (dwc->revision < DWC3_REVISION_183A) {
2094 if (next == DWC3_LINK_STATE_U0) {
2095 u32 u1u2;
2096 u32 reg;
2097
2098 switch (dwc->link_state) {
2099 case DWC3_LINK_STATE_U1:
2100 case DWC3_LINK_STATE_U2:
2101 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2102 u1u2 = reg & (DWC3_DCTL_INITU2ENA
2103 | DWC3_DCTL_ACCEPTU2ENA
2104 | DWC3_DCTL_INITU1ENA
2105 | DWC3_DCTL_ACCEPTU1ENA);
2106
2107 if (!dwc->u1u2)
2108 dwc->u1u2 = reg & u1u2;
2109
2110 reg &= ~u1u2;
2111
2112 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2113 break;
2114 default:
2115 /* do nothing */
2116 break;
2117 }
2118 }
2119 }
2120
2121 dwc->link_state = next;
Felipe Balbi019ac832011-09-08 21:18:47 +03002122
2123 dev_vdbg(dwc->dev, "%s link %d\n", __func__, dwc->link_state);
Felipe Balbi72246da2011-08-19 18:10:58 +03002124}
2125
2126static void dwc3_gadget_interrupt(struct dwc3 *dwc,
2127 const struct dwc3_event_devt *event)
2128{
2129 switch (event->type) {
2130 case DWC3_DEVICE_EVENT_DISCONNECT:
2131 dwc3_gadget_disconnect_interrupt(dwc);
2132 break;
2133 case DWC3_DEVICE_EVENT_RESET:
2134 dwc3_gadget_reset_interrupt(dwc);
2135 break;
2136 case DWC3_DEVICE_EVENT_CONNECT_DONE:
2137 dwc3_gadget_conndone_interrupt(dwc);
2138 break;
2139 case DWC3_DEVICE_EVENT_WAKEUP:
2140 dwc3_gadget_wakeup_interrupt(dwc);
2141 break;
2142 case DWC3_DEVICE_EVENT_LINK_STATUS_CHANGE:
2143 dwc3_gadget_linksts_change_interrupt(dwc, event->event_info);
2144 break;
2145 case DWC3_DEVICE_EVENT_EOPF:
2146 dev_vdbg(dwc->dev, "End of Periodic Frame\n");
2147 break;
2148 case DWC3_DEVICE_EVENT_SOF:
2149 dev_vdbg(dwc->dev, "Start of Periodic Frame\n");
2150 break;
2151 case DWC3_DEVICE_EVENT_ERRATIC_ERROR:
2152 dev_vdbg(dwc->dev, "Erratic Error\n");
2153 break;
2154 case DWC3_DEVICE_EVENT_CMD_CMPL:
2155 dev_vdbg(dwc->dev, "Command Complete\n");
2156 break;
2157 case DWC3_DEVICE_EVENT_OVERFLOW:
2158 dev_vdbg(dwc->dev, "Overflow\n");
2159 break;
2160 default:
2161 dev_dbg(dwc->dev, "UNKNOWN IRQ %d\n", event->type);
2162 }
2163}
2164
2165static void dwc3_process_event_entry(struct dwc3 *dwc,
2166 const union dwc3_event *event)
2167{
2168 /* Endpoint IRQ, handle it and return early */
2169 if (event->type.is_devspec == 0) {
2170 /* depevt */
2171 return dwc3_endpoint_interrupt(dwc, &event->depevt);
2172 }
2173
2174 switch (event->type.type) {
2175 case DWC3_EVENT_TYPE_DEV:
2176 dwc3_gadget_interrupt(dwc, &event->devt);
2177 break;
2178 /* REVISIT what to do with Carkit and I2C events ? */
2179 default:
2180 dev_err(dwc->dev, "UNKNOWN IRQ type %d\n", event->raw);
2181 }
2182}
2183
2184static irqreturn_t dwc3_process_event_buf(struct dwc3 *dwc, u32 buf)
2185{
2186 struct dwc3_event_buffer *evt;
2187 int left;
2188 u32 count;
2189
2190 count = dwc3_readl(dwc->regs, DWC3_GEVNTCOUNT(buf));
2191 count &= DWC3_GEVNTCOUNT_MASK;
2192 if (!count)
2193 return IRQ_NONE;
2194
2195 evt = dwc->ev_buffs[buf];
2196 left = count;
2197
2198 while (left > 0) {
2199 union dwc3_event event;
2200
Felipe Balbid70d8442012-02-06 13:40:17 +02002201 event.raw = *(u32 *) (evt->buf + evt->lpos);
2202
Felipe Balbi72246da2011-08-19 18:10:58 +03002203 dwc3_process_event_entry(dwc, &event);
2204 /*
2205 * XXX we wrap around correctly to the next entry as almost all
2206 * entries are 4 bytes in size. There is one entry which has 12
2207 * bytes which is a regular entry followed by 8 bytes data. ATM
2208 * I don't know how things are organized if were get next to the
2209 * a boundary so I worry about that once we try to handle that.
2210 */
2211 evt->lpos = (evt->lpos + 4) % DWC3_EVENT_BUFFERS_SIZE;
2212 left -= 4;
2213
2214 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(buf), 4);
2215 }
2216
2217 return IRQ_HANDLED;
2218}
2219
2220static irqreturn_t dwc3_interrupt(int irq, void *_dwc)
2221{
2222 struct dwc3 *dwc = _dwc;
2223 int i;
2224 irqreturn_t ret = IRQ_NONE;
2225
2226 spin_lock(&dwc->lock);
2227
Felipe Balbi9f622b22011-10-12 10:31:04 +03002228 for (i = 0; i < dwc->num_event_buffers; i++) {
Felipe Balbi72246da2011-08-19 18:10:58 +03002229 irqreturn_t status;
2230
2231 status = dwc3_process_event_buf(dwc, i);
2232 if (status == IRQ_HANDLED)
2233 ret = status;
2234 }
2235
2236 spin_unlock(&dwc->lock);
2237
2238 return ret;
2239}
2240
2241/**
2242 * dwc3_gadget_init - Initializes gadget related registers
Paul Zimmerman1d046792012-02-15 18:56:56 -08002243 * @dwc: pointer to our controller context structure
Felipe Balbi72246da2011-08-19 18:10:58 +03002244 *
2245 * Returns 0 on success otherwise negative errno.
2246 */
2247int __devinit dwc3_gadget_init(struct dwc3 *dwc)
2248{
2249 u32 reg;
2250 int ret;
2251 int irq;
2252
2253 dwc->ctrl_req = dma_alloc_coherent(dwc->dev, sizeof(*dwc->ctrl_req),
2254 &dwc->ctrl_req_addr, GFP_KERNEL);
2255 if (!dwc->ctrl_req) {
2256 dev_err(dwc->dev, "failed to allocate ctrl request\n");
2257 ret = -ENOMEM;
2258 goto err0;
2259 }
2260
2261 dwc->ep0_trb = dma_alloc_coherent(dwc->dev, sizeof(*dwc->ep0_trb),
2262 &dwc->ep0_trb_addr, GFP_KERNEL);
2263 if (!dwc->ep0_trb) {
2264 dev_err(dwc->dev, "failed to allocate ep0 trb\n");
2265 ret = -ENOMEM;
2266 goto err1;
2267 }
2268
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02002269 dwc->setup_buf = kzalloc(sizeof(*dwc->setup_buf) * 2,
2270 GFP_KERNEL);
Felipe Balbi72246da2011-08-19 18:10:58 +03002271 if (!dwc->setup_buf) {
2272 dev_err(dwc->dev, "failed to allocate setup buffer\n");
2273 ret = -ENOMEM;
2274 goto err2;
2275 }
2276
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002277 dwc->ep0_bounce = dma_alloc_coherent(dwc->dev,
2278 512, &dwc->ep0_bounce_addr, GFP_KERNEL);
2279 if (!dwc->ep0_bounce) {
2280 dev_err(dwc->dev, "failed to allocate ep0 bounce buffer\n");
2281 ret = -ENOMEM;
2282 goto err3;
2283 }
2284
Felipe Balbi72246da2011-08-19 18:10:58 +03002285 dev_set_name(&dwc->gadget.dev, "gadget");
2286
2287 dwc->gadget.ops = &dwc3_gadget_ops;
Michal Nazarewiczd327ab52011-11-19 18:27:37 +01002288 dwc->gadget.max_speed = USB_SPEED_SUPER;
Felipe Balbi72246da2011-08-19 18:10:58 +03002289 dwc->gadget.speed = USB_SPEED_UNKNOWN;
2290 dwc->gadget.dev.parent = dwc->dev;
Felipe Balbieeb720f2011-11-28 12:46:59 +02002291 dwc->gadget.sg_supported = true;
Felipe Balbi72246da2011-08-19 18:10:58 +03002292
2293 dma_set_coherent_mask(&dwc->gadget.dev, dwc->dev->coherent_dma_mask);
2294
2295 dwc->gadget.dev.dma_parms = dwc->dev->dma_parms;
2296 dwc->gadget.dev.dma_mask = dwc->dev->dma_mask;
2297 dwc->gadget.dev.release = dwc3_gadget_release;
2298 dwc->gadget.name = "dwc3-gadget";
2299
2300 /*
2301 * REVISIT: Here we should clear all pending IRQs to be
2302 * sure we're starting from a well known location.
2303 */
2304
2305 ret = dwc3_gadget_init_endpoints(dwc);
2306 if (ret)
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002307 goto err4;
Felipe Balbi72246da2011-08-19 18:10:58 +03002308
2309 irq = platform_get_irq(to_platform_device(dwc->dev), 0);
2310
2311 ret = request_irq(irq, dwc3_interrupt, IRQF_SHARED,
2312 "dwc3", dwc);
2313 if (ret) {
2314 dev_err(dwc->dev, "failed to request irq #%d --> %d\n",
2315 irq, ret);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002316 goto err5;
Felipe Balbi72246da2011-08-19 18:10:58 +03002317 }
2318
2319 /* Enable all but Start and End of Frame IRQs */
2320 reg = (DWC3_DEVTEN_VNDRDEVTSTRCVEDEN |
2321 DWC3_DEVTEN_EVNTOVERFLOWEN |
2322 DWC3_DEVTEN_CMDCMPLTEN |
2323 DWC3_DEVTEN_ERRTICERREN |
2324 DWC3_DEVTEN_WKUPEVTEN |
2325 DWC3_DEVTEN_ULSTCNGEN |
2326 DWC3_DEVTEN_CONNECTDONEEN |
2327 DWC3_DEVTEN_USBRSTEN |
2328 DWC3_DEVTEN_DISCONNEVTEN);
2329 dwc3_writel(dwc->regs, DWC3_DEVTEN, reg);
2330
2331 ret = device_register(&dwc->gadget.dev);
2332 if (ret) {
2333 dev_err(dwc->dev, "failed to register gadget device\n");
2334 put_device(&dwc->gadget.dev);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002335 goto err6;
Felipe Balbi72246da2011-08-19 18:10:58 +03002336 }
2337
2338 ret = usb_add_gadget_udc(dwc->dev, &dwc->gadget);
2339 if (ret) {
2340 dev_err(dwc->dev, "failed to register udc\n");
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002341 goto err7;
Felipe Balbi72246da2011-08-19 18:10:58 +03002342 }
2343
2344 return 0;
2345
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002346err7:
Felipe Balbi72246da2011-08-19 18:10:58 +03002347 device_unregister(&dwc->gadget.dev);
2348
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002349err6:
Felipe Balbi72246da2011-08-19 18:10:58 +03002350 dwc3_writel(dwc->regs, DWC3_DEVTEN, 0x00);
2351 free_irq(irq, dwc);
2352
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002353err5:
Felipe Balbi72246da2011-08-19 18:10:58 +03002354 dwc3_gadget_free_endpoints(dwc);
2355
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002356err4:
2357 dma_free_coherent(dwc->dev, 512, dwc->ep0_bounce,
2358 dwc->ep0_bounce_addr);
2359
Felipe Balbi72246da2011-08-19 18:10:58 +03002360err3:
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02002361 kfree(dwc->setup_buf);
Felipe Balbi72246da2011-08-19 18:10:58 +03002362
2363err2:
2364 dma_free_coherent(dwc->dev, sizeof(*dwc->ep0_trb),
2365 dwc->ep0_trb, dwc->ep0_trb_addr);
2366
2367err1:
2368 dma_free_coherent(dwc->dev, sizeof(*dwc->ctrl_req),
2369 dwc->ctrl_req, dwc->ctrl_req_addr);
2370
2371err0:
2372 return ret;
2373}
2374
2375void dwc3_gadget_exit(struct dwc3 *dwc)
2376{
2377 int irq;
Felipe Balbi72246da2011-08-19 18:10:58 +03002378
2379 usb_del_gadget_udc(&dwc->gadget);
2380 irq = platform_get_irq(to_platform_device(dwc->dev), 0);
2381
2382 dwc3_writel(dwc->regs, DWC3_DEVTEN, 0x00);
2383 free_irq(irq, dwc);
2384
Felipe Balbi72246da2011-08-19 18:10:58 +03002385 dwc3_gadget_free_endpoints(dwc);
2386
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002387 dma_free_coherent(dwc->dev, 512, dwc->ep0_bounce,
2388 dwc->ep0_bounce_addr);
2389
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02002390 kfree(dwc->setup_buf);
Felipe Balbi72246da2011-08-19 18:10:58 +03002391
2392 dma_free_coherent(dwc->dev, sizeof(*dwc->ep0_trb),
2393 dwc->ep0_trb, dwc->ep0_trb_addr);
2394
2395 dma_free_coherent(dwc->dev, sizeof(*dwc->ctrl_req),
2396 dwc->ctrl_req, dwc->ctrl_req_addr);
2397
2398 device_unregister(&dwc->gadget.dev);
2399}