blob: 8ed83b938a7795fdca75932159960435e03c8460 [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];
George Cherianfba59fb2013-05-27 14:35:49 +05301486 /*
1487 * Physical endpoints 0 and 1 are special; they form the
1488 * bi-directional USB endpoint 0.
1489 *
1490 * For those two physical endpoints, we don't allocate a TRB
1491 * pool nor do we add them the endpoints list. Due to that, we
1492 * shouldn't do these two operations otherwise we would end up
1493 * with all sorts of bugs when removing dwc3.ko.
1494 */
1495 if (epnum != 0 && epnum != 1) {
1496 dwc3_free_trb_pool(dep);
Felipe Balbi72246da2011-08-19 18:10:58 +03001497 list_del(&dep->endpoint.ep_list);
George Cherianfba59fb2013-05-27 14:35:49 +05301498 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001499
1500 kfree(dep);
1501 }
1502}
1503
1504static void dwc3_gadget_release(struct device *dev)
1505{
1506 dev_dbg(dev, "%s\n", __func__);
1507}
1508
1509/* -------------------------------------------------------------------------- */
1510static int dwc3_cleanup_done_reqs(struct dwc3 *dwc, struct dwc3_ep *dep,
1511 const struct dwc3_event_depevt *event, int status)
1512{
1513 struct dwc3_request *req;
Felipe Balbif6bafc62012-02-06 11:04:53 +02001514 struct dwc3_trb *trb;
Felipe Balbi72246da2011-08-19 18:10:58 +03001515 unsigned int count;
1516 unsigned int s_pkt = 0;
1517
1518 do {
1519 req = next_request(&dep->req_queued);
Sebastian Andrzej Siewiord39ee7b2011-11-03 10:32:20 +01001520 if (!req) {
1521 WARN_ON_ONCE(1);
1522 return 1;
1523 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001524
Felipe Balbif6bafc62012-02-06 11:04:53 +02001525 trb = req->trb;
Felipe Balbi72246da2011-08-19 18:10:58 +03001526
Felipe Balbif6bafc62012-02-06 11:04:53 +02001527 if ((trb->ctrl & DWC3_TRB_CTRL_HWO) && status != -ESHUTDOWN)
Sebastian Andrzej Siewior0d2f4752011-08-19 19:59:12 +02001528 /*
1529 * We continue despite the error. There is not much we
Paul Zimmerman1d046792012-02-15 18:56:56 -08001530 * can do. If we don't clean it up we loop forever. If
1531 * we skip the TRB then it gets overwritten after a
1532 * while since we use them in a ring buffer. A BUG()
1533 * would help. Lets hope that if this occurs, someone
Sebastian Andrzej Siewior0d2f4752011-08-19 19:59:12 +02001534 * fixes the root cause instead of looking away :)
1535 */
Felipe Balbi72246da2011-08-19 18:10:58 +03001536 dev_err(dwc->dev, "%s's TRB (%p) still owned by HW\n",
1537 dep->name, req->trb);
Felipe Balbif6bafc62012-02-06 11:04:53 +02001538 count = trb->size & DWC3_TRB_SIZE_MASK;
Felipe Balbi72246da2011-08-19 18:10:58 +03001539
1540 if (dep->direction) {
1541 if (count) {
1542 dev_err(dwc->dev, "incomplete IN transfer %s\n",
1543 dep->name);
1544 status = -ECONNRESET;
1545 }
1546 } else {
1547 if (count && (event->status & DEPEVT_STATUS_SHORT))
1548 s_pkt = 1;
1549 }
1550
1551 /*
1552 * We assume here we will always receive the entire data block
1553 * which we should receive. Meaning, if we program RX to
1554 * receive 4K but we receive only 2K, we assume that's all we
1555 * should receive and we simply bounce the request back to the
1556 * gadget driver for further processing.
1557 */
1558 req->request.actual += req->request.length - count;
1559 dwc3_gadget_giveback(dep, req, status);
1560 if (s_pkt)
1561 break;
Felipe Balbif6bafc62012-02-06 11:04:53 +02001562 if ((event->status & DEPEVT_STATUS_LST) &&
1563 (trb->ctrl & DWC3_TRB_CTRL_LST))
Felipe Balbi72246da2011-08-19 18:10:58 +03001564 break;
Felipe Balbif6bafc62012-02-06 11:04:53 +02001565 if ((event->status & DEPEVT_STATUS_IOC) &&
1566 (trb->ctrl & DWC3_TRB_CTRL_IOC))
Felipe Balbi72246da2011-08-19 18:10:58 +03001567 break;
1568 } while (1);
1569
Felipe Balbif6bafc62012-02-06 11:04:53 +02001570 if ((event->status & DEPEVT_STATUS_IOC) &&
1571 (trb->ctrl & DWC3_TRB_CTRL_IOC))
Felipe Balbi72246da2011-08-19 18:10:58 +03001572 return 0;
1573 return 1;
1574}
1575
1576static void dwc3_endpoint_transfer_complete(struct dwc3 *dwc,
1577 struct dwc3_ep *dep, const struct dwc3_event_depevt *event,
1578 int start_new)
1579{
1580 unsigned status = 0;
1581 int clean_busy;
1582
1583 if (event->status & DEPEVT_STATUS_BUSERR)
1584 status = -ECONNRESET;
1585
Paul Zimmerman1d046792012-02-15 18:56:56 -08001586 clean_busy = dwc3_cleanup_done_reqs(dwc, dep, event, status);
Paul Zimmermanc2df85c2012-02-24 17:32:18 -08001587 if (clean_busy)
Felipe Balbi72246da2011-08-19 18:10:58 +03001588 dep->flags &= ~DWC3_EP_BUSY;
Felipe Balbifae2b902011-10-14 13:00:30 +03001589
1590 /*
1591 * WORKAROUND: This is the 2nd half of U1/U2 -> U0 workaround.
1592 * See dwc3_gadget_linksts_change_interrupt() for 1st half.
1593 */
1594 if (dwc->revision < DWC3_REVISION_183A) {
1595 u32 reg;
1596 int i;
1597
1598 for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) {
1599 struct dwc3_ep *dep = dwc->eps[i];
1600
1601 if (!(dep->flags & DWC3_EP_ENABLED))
1602 continue;
1603
1604 if (!list_empty(&dep->req_queued))
1605 return;
1606 }
1607
1608 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
1609 reg |= dwc->u1u2;
1610 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1611
1612 dwc->u1u2 = 0;
1613 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001614}
1615
1616static void dwc3_gadget_start_isoc(struct dwc3 *dwc,
1617 struct dwc3_ep *dep, const struct dwc3_event_depevt *event)
1618{
Paul Zimmerman9bafa562012-02-17 14:10:16 -08001619 u32 uf, mask;
Felipe Balbi72246da2011-08-19 18:10:58 +03001620
1621 if (list_empty(&dep->request_list)) {
1622 dev_vdbg(dwc->dev, "ISOC ep %s run out for requests.\n",
1623 dep->name);
1624 return;
1625 }
1626
Paul Zimmerman9bafa562012-02-17 14:10:16 -08001627 mask = ~(dep->interval - 1);
1628 uf = event->parameters & mask;
1629 /* 4 micro frames in the future */
1630 uf += dep->interval * 4;
Felipe Balbi72246da2011-08-19 18:10:58 +03001631
1632 __dwc3_gadget_kick_transfer(dep, uf, 1);
1633}
1634
1635static void dwc3_process_ep_cmd_complete(struct dwc3_ep *dep,
1636 const struct dwc3_event_depevt *event)
1637{
1638 struct dwc3 *dwc = dep->dwc;
1639 struct dwc3_event_depevt mod_ev = *event;
1640
1641 /*
Paul Zimmerman1d046792012-02-15 18:56:56 -08001642 * We were asked to remove one request. It is possible that this
1643 * request and a few others were started together and have the same
Felipe Balbi72246da2011-08-19 18:10:58 +03001644 * transfer index. Since we stopped the complete endpoint we don't
1645 * know how many requests were already completed (and not yet)
1646 * reported and how could be done (later). We purge them all until
1647 * the end of the list.
1648 */
1649 mod_ev.status = DEPEVT_STATUS_LST;
1650 dwc3_cleanup_done_reqs(dwc, dep, &mod_ev, -ESHUTDOWN);
1651 dep->flags &= ~DWC3_EP_BUSY;
Paul Zimmerman1d046792012-02-15 18:56:56 -08001652 /* pending requests are ignored and are queued on XferNotReady */
Felipe Balbi72246da2011-08-19 18:10:58 +03001653}
1654
1655static void dwc3_ep_cmd_compl(struct dwc3_ep *dep,
1656 const struct dwc3_event_depevt *event)
1657{
1658 u32 param = event->parameters;
1659 u32 cmd_type = (param >> 8) & ((1 << 5) - 1);
1660
1661 switch (cmd_type) {
1662 case DWC3_DEPCMD_ENDTRANSFER:
1663 dwc3_process_ep_cmd_complete(dep, event);
1664 break;
1665 case DWC3_DEPCMD_STARTTRANSFER:
1666 dep->res_trans_idx = param & 0x7f;
1667 break;
1668 default:
1669 printk(KERN_ERR "%s() unknown /unexpected type: %d\n",
1670 __func__, cmd_type);
1671 break;
1672 };
1673}
1674
1675static void dwc3_endpoint_interrupt(struct dwc3 *dwc,
1676 const struct dwc3_event_depevt *event)
1677{
1678 struct dwc3_ep *dep;
1679 u8 epnum = event->endpoint_number;
1680
1681 dep = dwc->eps[epnum];
1682
1683 dev_vdbg(dwc->dev, "%s: %s\n", dep->name,
1684 dwc3_ep_event_string(event->endpoint_event));
1685
1686 if (epnum == 0 || epnum == 1) {
1687 dwc3_ep0_interrupt(dwc, event);
1688 return;
1689 }
1690
1691 switch (event->endpoint_event) {
1692 case DWC3_DEPEVT_XFERCOMPLETE:
Paul Zimmermanc2df85c2012-02-24 17:32:18 -08001693 dep->res_trans_idx = 0;
1694
Felipe Balbi72246da2011-08-19 18:10:58 +03001695 if (usb_endpoint_xfer_isoc(dep->desc)) {
1696 dev_dbg(dwc->dev, "%s is an Isochronous endpoint\n",
1697 dep->name);
1698 return;
1699 }
1700
1701 dwc3_endpoint_transfer_complete(dwc, dep, event, 1);
1702 break;
1703 case DWC3_DEPEVT_XFERINPROGRESS:
1704 if (!usb_endpoint_xfer_isoc(dep->desc)) {
1705 dev_dbg(dwc->dev, "%s is not an Isochronous endpoint\n",
1706 dep->name);
1707 return;
1708 }
1709
1710 dwc3_endpoint_transfer_complete(dwc, dep, event, 0);
1711 break;
1712 case DWC3_DEPEVT_XFERNOTREADY:
1713 if (usb_endpoint_xfer_isoc(dep->desc)) {
1714 dwc3_gadget_start_isoc(dwc, dep, event);
1715 } else {
1716 int ret;
1717
1718 dev_vdbg(dwc->dev, "%s: reason %s\n",
Felipe Balbi40aa41f2012-01-18 17:06:03 +02001719 dep->name, event->status &
1720 DEPEVT_STATUS_TRANSFER_ACTIVE
Felipe Balbi72246da2011-08-19 18:10:58 +03001721 ? "Transfer Active"
1722 : "Transfer Not Active");
1723
1724 ret = __dwc3_gadget_kick_transfer(dep, 0, 1);
1725 if (!ret || ret == -EBUSY)
1726 return;
1727
1728 dev_dbg(dwc->dev, "%s: failed to kick transfers\n",
1729 dep->name);
1730 }
1731
1732 break;
Felipe Balbi879631a2011-09-30 10:58:47 +03001733 case DWC3_DEPEVT_STREAMEVT:
1734 if (!usb_endpoint_xfer_bulk(dep->desc)) {
1735 dev_err(dwc->dev, "Stream event for non-Bulk %s\n",
1736 dep->name);
1737 return;
1738 }
1739
1740 switch (event->status) {
1741 case DEPEVT_STREAMEVT_FOUND:
1742 dev_vdbg(dwc->dev, "Stream %d found and started\n",
1743 event->parameters);
1744
1745 break;
1746 case DEPEVT_STREAMEVT_NOTFOUND:
1747 /* FALLTHROUGH */
1748 default:
1749 dev_dbg(dwc->dev, "Couldn't find suitable stream\n");
1750 }
1751 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03001752 case DWC3_DEPEVT_RXTXFIFOEVT:
1753 dev_dbg(dwc->dev, "%s FIFO Overrun\n", dep->name);
1754 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03001755 case DWC3_DEPEVT_EPCMDCMPLT:
1756 dwc3_ep_cmd_compl(dep, event);
1757 break;
1758 }
1759}
1760
1761static void dwc3_disconnect_gadget(struct dwc3 *dwc)
1762{
1763 if (dwc->gadget_driver && dwc->gadget_driver->disconnect) {
1764 spin_unlock(&dwc->lock);
1765 dwc->gadget_driver->disconnect(&dwc->gadget);
1766 spin_lock(&dwc->lock);
1767 }
1768}
1769
1770static void dwc3_stop_active_transfer(struct dwc3 *dwc, u32 epnum)
1771{
1772 struct dwc3_ep *dep;
1773 struct dwc3_gadget_ep_cmd_params params;
1774 u32 cmd;
1775 int ret;
1776
1777 dep = dwc->eps[epnum];
1778
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +02001779 WARN_ON(!dep->res_trans_idx);
Felipe Balbi72246da2011-08-19 18:10:58 +03001780 if (dep->res_trans_idx) {
1781 cmd = DWC3_DEPCMD_ENDTRANSFER;
1782 cmd |= DWC3_DEPCMD_HIPRI_FORCERM | DWC3_DEPCMD_CMDIOC;
1783 cmd |= DWC3_DEPCMD_PARAM(dep->res_trans_idx);
1784 memset(&params, 0, sizeof(params));
1785 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number, cmd, &params);
1786 WARN_ON_ONCE(ret);
Sebastian Andrzej Siewiora1ae9be2011-08-22 17:42:18 +02001787 dep->res_trans_idx = 0;
Felipe Balbibfbd61e2012-10-04 11:58:00 +03001788 dep->flags &= ~DWC3_EP_BUSY;
Felipe Balbi72246da2011-08-19 18:10:58 +03001789 }
1790}
1791
1792static void dwc3_stop_active_transfers(struct dwc3 *dwc)
1793{
1794 u32 epnum;
1795
1796 for (epnum = 2; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
1797 struct dwc3_ep *dep;
1798
1799 dep = dwc->eps[epnum];
1800 if (!(dep->flags & DWC3_EP_ENABLED))
1801 continue;
1802
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +02001803 dwc3_remove_requests(dwc, dep);
Felipe Balbi72246da2011-08-19 18:10:58 +03001804 }
1805}
1806
1807static void dwc3_clear_stall_all_ep(struct dwc3 *dwc)
1808{
1809 u32 epnum;
1810
1811 for (epnum = 1; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
1812 struct dwc3_ep *dep;
1813 struct dwc3_gadget_ep_cmd_params params;
1814 int ret;
1815
1816 dep = dwc->eps[epnum];
1817
1818 if (!(dep->flags & DWC3_EP_STALL))
1819 continue;
1820
1821 dep->flags &= ~DWC3_EP_STALL;
1822
1823 memset(&params, 0, sizeof(params));
1824 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number,
1825 DWC3_DEPCMD_CLEARSTALL, &params);
1826 WARN_ON_ONCE(ret);
1827 }
1828}
1829
1830static void dwc3_gadget_disconnect_interrupt(struct dwc3 *dwc)
1831{
1832 dev_vdbg(dwc->dev, "%s\n", __func__);
1833#if 0
1834 XXX
1835 U1/U2 is powersave optimization. Skip it for now. Anyway we need to
1836 enable it before we can disable it.
1837
1838 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
1839 reg &= ~DWC3_DCTL_INITU1ENA;
1840 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1841
1842 reg &= ~DWC3_DCTL_INITU2ENA;
1843 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1844#endif
1845
1846 dwc3_stop_active_transfers(dwc);
1847 dwc3_disconnect_gadget(dwc);
Paul Zimmermanb23c8432011-09-30 10:58:42 +03001848 dwc->start_config_issued = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03001849
1850 dwc->gadget.speed = USB_SPEED_UNKNOWN;
Felipe Balbidf62df52011-10-14 15:11:49 +03001851 dwc->setup_packet_pending = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03001852}
1853
1854static void dwc3_gadget_usb3_phy_power(struct dwc3 *dwc, int on)
1855{
1856 u32 reg;
1857
1858 reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0));
1859
1860 if (on)
1861 reg &= ~DWC3_GUSB3PIPECTL_SUSPHY;
1862 else
1863 reg |= DWC3_GUSB3PIPECTL_SUSPHY;
1864
1865 dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg);
1866}
1867
1868static void dwc3_gadget_usb2_phy_power(struct dwc3 *dwc, int on)
1869{
1870 u32 reg;
1871
1872 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
1873
1874 if (on)
1875 reg &= ~DWC3_GUSB2PHYCFG_SUSPHY;
1876 else
1877 reg |= DWC3_GUSB2PHYCFG_SUSPHY;
1878
1879 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
1880}
1881
1882static void dwc3_gadget_reset_interrupt(struct dwc3 *dwc)
1883{
1884 u32 reg;
1885
1886 dev_vdbg(dwc->dev, "%s\n", __func__);
1887
Felipe Balbidf62df52011-10-14 15:11:49 +03001888 /*
1889 * WORKAROUND: DWC3 revisions <1.88a have an issue which
1890 * would cause a missing Disconnect Event if there's a
1891 * pending Setup Packet in the FIFO.
1892 *
1893 * There's no suggested workaround on the official Bug
1894 * report, which states that "unless the driver/application
1895 * is doing any special handling of a disconnect event,
1896 * there is no functional issue".
1897 *
1898 * Unfortunately, it turns out that we _do_ some special
1899 * handling of a disconnect event, namely complete all
1900 * pending transfers, notify gadget driver of the
1901 * disconnection, and so on.
1902 *
1903 * Our suggested workaround is to follow the Disconnect
1904 * Event steps here, instead, based on a setup_packet_pending
1905 * flag. Such flag gets set whenever we have a XferNotReady
1906 * event on EP0 and gets cleared on XferComplete for the
1907 * same endpoint.
1908 *
1909 * Refers to:
1910 *
1911 * STAR#9000466709: RTL: Device : Disconnect event not
1912 * generated if setup packet pending in FIFO
1913 */
1914 if (dwc->revision < DWC3_REVISION_188A) {
1915 if (dwc->setup_packet_pending)
1916 dwc3_gadget_disconnect_interrupt(dwc);
1917 }
1918
Felipe Balbi961906e2011-12-20 15:37:21 +02001919 /* after reset -> Default State */
1920 dwc->dev_state = DWC3_DEFAULT_STATE;
1921
Felipe Balbi72246da2011-08-19 18:10:58 +03001922 /* Enable PHYs */
1923 dwc3_gadget_usb2_phy_power(dwc, true);
1924 dwc3_gadget_usb3_phy_power(dwc, true);
1925
1926 if (dwc->gadget.speed != USB_SPEED_UNKNOWN)
1927 dwc3_disconnect_gadget(dwc);
1928
1929 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
1930 reg &= ~DWC3_DCTL_TSTCTRL_MASK;
1931 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Gerard Cauvy3b637362012-02-10 12:21:18 +02001932 dwc->test_mode = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03001933
1934 dwc3_stop_active_transfers(dwc);
1935 dwc3_clear_stall_all_ep(dwc);
Paul Zimmermanb23c8432011-09-30 10:58:42 +03001936 dwc->start_config_issued = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03001937
1938 /* Reset device address to zero */
1939 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
1940 reg &= ~(DWC3_DCFG_DEVADDR_MASK);
1941 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +03001942}
1943
1944static void dwc3_update_ram_clk_sel(struct dwc3 *dwc, u32 speed)
1945{
1946 u32 reg;
1947 u32 usb30_clock = DWC3_GCTL_CLK_BUS;
1948
1949 /*
1950 * We change the clock only at SS but I dunno why I would want to do
1951 * this. Maybe it becomes part of the power saving plan.
1952 */
1953
1954 if (speed != DWC3_DSTS_SUPERSPEED)
1955 return;
1956
1957 /*
1958 * RAMClkSel is reset to 0 after USB reset, so it must be reprogrammed
1959 * each time on Connect Done.
1960 */
1961 if (!usb30_clock)
1962 return;
1963
1964 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
1965 reg |= DWC3_GCTL_RAMCLKSEL(usb30_clock);
1966 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
1967}
1968
1969static void dwc3_gadget_disable_phy(struct dwc3 *dwc, u8 speed)
1970{
1971 switch (speed) {
1972 case USB_SPEED_SUPER:
1973 dwc3_gadget_usb2_phy_power(dwc, false);
1974 break;
1975 case USB_SPEED_HIGH:
1976 case USB_SPEED_FULL:
1977 case USB_SPEED_LOW:
1978 dwc3_gadget_usb3_phy_power(dwc, false);
1979 break;
1980 }
1981}
1982
1983static void dwc3_gadget_conndone_interrupt(struct dwc3 *dwc)
1984{
1985 struct dwc3_gadget_ep_cmd_params params;
1986 struct dwc3_ep *dep;
1987 int ret;
1988 u32 reg;
1989 u8 speed;
1990
1991 dev_vdbg(dwc->dev, "%s\n", __func__);
1992
1993 memset(&params, 0x00, sizeof(params));
1994
Felipe Balbi72246da2011-08-19 18:10:58 +03001995 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1996 speed = reg & DWC3_DSTS_CONNECTSPD;
1997 dwc->speed = speed;
1998
1999 dwc3_update_ram_clk_sel(dwc, speed);
2000
2001 switch (speed) {
2002 case DWC3_DCFG_SUPERSPEED:
Felipe Balbi05870c52011-10-14 14:51:38 +03002003 /*
2004 * WORKAROUND: DWC3 revisions <1.90a have an issue which
2005 * would cause a missing USB3 Reset event.
2006 *
2007 * In such situations, we should force a USB3 Reset
2008 * event by calling our dwc3_gadget_reset_interrupt()
2009 * routine.
2010 *
2011 * Refers to:
2012 *
2013 * STAR#9000483510: RTL: SS : USB3 reset event may
2014 * not be generated always when the link enters poll
2015 */
2016 if (dwc->revision < DWC3_REVISION_190A)
2017 dwc3_gadget_reset_interrupt(dwc);
2018
Felipe Balbi72246da2011-08-19 18:10:58 +03002019 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
2020 dwc->gadget.ep0->maxpacket = 512;
2021 dwc->gadget.speed = USB_SPEED_SUPER;
2022 break;
2023 case DWC3_DCFG_HIGHSPEED:
2024 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
2025 dwc->gadget.ep0->maxpacket = 64;
2026 dwc->gadget.speed = USB_SPEED_HIGH;
2027 break;
2028 case DWC3_DCFG_FULLSPEED2:
2029 case DWC3_DCFG_FULLSPEED1:
2030 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
2031 dwc->gadget.ep0->maxpacket = 64;
2032 dwc->gadget.speed = USB_SPEED_FULL;
2033 break;
2034 case DWC3_DCFG_LOWSPEED:
2035 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(8);
2036 dwc->gadget.ep0->maxpacket = 8;
2037 dwc->gadget.speed = USB_SPEED_LOW;
2038 break;
2039 }
2040
2041 /* Disable unneded PHY */
2042 dwc3_gadget_disable_phy(dwc, dwc->gadget.speed);
2043
2044 dep = dwc->eps[0];
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 dep = dwc->eps[1];
Felipe Balbic90bfae2011-11-29 13:11:21 +02002052 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL);
Felipe Balbi72246da2011-08-19 18:10:58 +03002053 if (ret) {
2054 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
2055 return;
2056 }
2057
2058 /*
2059 * Configure PHY via GUSB3PIPECTLn if required.
2060 *
2061 * Update GTXFIFOSIZn
2062 *
2063 * In both cases reset values should be sufficient.
2064 */
2065}
2066
2067static void dwc3_gadget_wakeup_interrupt(struct dwc3 *dwc)
2068{
2069 dev_vdbg(dwc->dev, "%s\n", __func__);
2070
2071 /*
2072 * TODO take core out of low power mode when that's
2073 * implemented.
2074 */
2075
2076 dwc->gadget_driver->resume(&dwc->gadget);
2077}
2078
2079static void dwc3_gadget_linksts_change_interrupt(struct dwc3 *dwc,
2080 unsigned int evtinfo)
2081{
Felipe Balbifae2b902011-10-14 13:00:30 +03002082 enum dwc3_link_state next = evtinfo & DWC3_LINK_STATE_MASK;
2083
2084 /*
2085 * WORKAROUND: DWC3 Revisions <1.83a have an issue which, depending
2086 * on the link partner, the USB session might do multiple entry/exit
2087 * of low power states before a transfer takes place.
2088 *
2089 * Due to this problem, we might experience lower throughput. The
2090 * suggested workaround is to disable DCTL[12:9] bits if we're
2091 * transitioning from U1/U2 to U0 and enable those bits again
2092 * after a transfer completes and there are no pending transfers
2093 * on any of the enabled endpoints.
2094 *
2095 * This is the first half of that workaround.
2096 *
2097 * Refers to:
2098 *
2099 * STAR#9000446952: RTL: Device SS : if U1/U2 ->U0 takes >128us
2100 * core send LGO_Ux entering U0
2101 */
2102 if (dwc->revision < DWC3_REVISION_183A) {
2103 if (next == DWC3_LINK_STATE_U0) {
2104 u32 u1u2;
2105 u32 reg;
2106
2107 switch (dwc->link_state) {
2108 case DWC3_LINK_STATE_U1:
2109 case DWC3_LINK_STATE_U2:
2110 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2111 u1u2 = reg & (DWC3_DCTL_INITU2ENA
2112 | DWC3_DCTL_ACCEPTU2ENA
2113 | DWC3_DCTL_INITU1ENA
2114 | DWC3_DCTL_ACCEPTU1ENA);
2115
2116 if (!dwc->u1u2)
2117 dwc->u1u2 = reg & u1u2;
2118
2119 reg &= ~u1u2;
2120
2121 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2122 break;
2123 default:
2124 /* do nothing */
2125 break;
2126 }
2127 }
2128 }
2129
2130 dwc->link_state = next;
Felipe Balbi019ac832011-09-08 21:18:47 +03002131
2132 dev_vdbg(dwc->dev, "%s link %d\n", __func__, dwc->link_state);
Felipe Balbi72246da2011-08-19 18:10:58 +03002133}
2134
2135static void dwc3_gadget_interrupt(struct dwc3 *dwc,
2136 const struct dwc3_event_devt *event)
2137{
2138 switch (event->type) {
2139 case DWC3_DEVICE_EVENT_DISCONNECT:
2140 dwc3_gadget_disconnect_interrupt(dwc);
2141 break;
2142 case DWC3_DEVICE_EVENT_RESET:
2143 dwc3_gadget_reset_interrupt(dwc);
2144 break;
2145 case DWC3_DEVICE_EVENT_CONNECT_DONE:
2146 dwc3_gadget_conndone_interrupt(dwc);
2147 break;
2148 case DWC3_DEVICE_EVENT_WAKEUP:
2149 dwc3_gadget_wakeup_interrupt(dwc);
2150 break;
2151 case DWC3_DEVICE_EVENT_LINK_STATUS_CHANGE:
2152 dwc3_gadget_linksts_change_interrupt(dwc, event->event_info);
2153 break;
2154 case DWC3_DEVICE_EVENT_EOPF:
2155 dev_vdbg(dwc->dev, "End of Periodic Frame\n");
2156 break;
2157 case DWC3_DEVICE_EVENT_SOF:
2158 dev_vdbg(dwc->dev, "Start of Periodic Frame\n");
2159 break;
2160 case DWC3_DEVICE_EVENT_ERRATIC_ERROR:
2161 dev_vdbg(dwc->dev, "Erratic Error\n");
2162 break;
2163 case DWC3_DEVICE_EVENT_CMD_CMPL:
2164 dev_vdbg(dwc->dev, "Command Complete\n");
2165 break;
2166 case DWC3_DEVICE_EVENT_OVERFLOW:
2167 dev_vdbg(dwc->dev, "Overflow\n");
2168 break;
2169 default:
2170 dev_dbg(dwc->dev, "UNKNOWN IRQ %d\n", event->type);
2171 }
2172}
2173
2174static void dwc3_process_event_entry(struct dwc3 *dwc,
2175 const union dwc3_event *event)
2176{
2177 /* Endpoint IRQ, handle it and return early */
2178 if (event->type.is_devspec == 0) {
2179 /* depevt */
2180 return dwc3_endpoint_interrupt(dwc, &event->depevt);
2181 }
2182
2183 switch (event->type.type) {
2184 case DWC3_EVENT_TYPE_DEV:
2185 dwc3_gadget_interrupt(dwc, &event->devt);
2186 break;
2187 /* REVISIT what to do with Carkit and I2C events ? */
2188 default:
2189 dev_err(dwc->dev, "UNKNOWN IRQ type %d\n", event->raw);
2190 }
2191}
2192
2193static irqreturn_t dwc3_process_event_buf(struct dwc3 *dwc, u32 buf)
2194{
2195 struct dwc3_event_buffer *evt;
2196 int left;
2197 u32 count;
2198
2199 count = dwc3_readl(dwc->regs, DWC3_GEVNTCOUNT(buf));
2200 count &= DWC3_GEVNTCOUNT_MASK;
2201 if (!count)
2202 return IRQ_NONE;
2203
2204 evt = dwc->ev_buffs[buf];
2205 left = count;
2206
2207 while (left > 0) {
2208 union dwc3_event event;
2209
Felipe Balbid70d8442012-02-06 13:40:17 +02002210 event.raw = *(u32 *) (evt->buf + evt->lpos);
2211
Felipe Balbi72246da2011-08-19 18:10:58 +03002212 dwc3_process_event_entry(dwc, &event);
2213 /*
2214 * XXX we wrap around correctly to the next entry as almost all
2215 * entries are 4 bytes in size. There is one entry which has 12
2216 * bytes which is a regular entry followed by 8 bytes data. ATM
2217 * I don't know how things are organized if were get next to the
2218 * a boundary so I worry about that once we try to handle that.
2219 */
2220 evt->lpos = (evt->lpos + 4) % DWC3_EVENT_BUFFERS_SIZE;
2221 left -= 4;
2222
2223 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(buf), 4);
2224 }
2225
2226 return IRQ_HANDLED;
2227}
2228
2229static irqreturn_t dwc3_interrupt(int irq, void *_dwc)
2230{
2231 struct dwc3 *dwc = _dwc;
2232 int i;
2233 irqreturn_t ret = IRQ_NONE;
2234
2235 spin_lock(&dwc->lock);
2236
Felipe Balbi9f622b22011-10-12 10:31:04 +03002237 for (i = 0; i < dwc->num_event_buffers; i++) {
Felipe Balbi72246da2011-08-19 18:10:58 +03002238 irqreturn_t status;
2239
2240 status = dwc3_process_event_buf(dwc, i);
2241 if (status == IRQ_HANDLED)
2242 ret = status;
2243 }
2244
2245 spin_unlock(&dwc->lock);
2246
2247 return ret;
2248}
2249
2250/**
2251 * dwc3_gadget_init - Initializes gadget related registers
Paul Zimmerman1d046792012-02-15 18:56:56 -08002252 * @dwc: pointer to our controller context structure
Felipe Balbi72246da2011-08-19 18:10:58 +03002253 *
2254 * Returns 0 on success otherwise negative errno.
2255 */
2256int __devinit dwc3_gadget_init(struct dwc3 *dwc)
2257{
2258 u32 reg;
2259 int ret;
2260 int irq;
2261
2262 dwc->ctrl_req = dma_alloc_coherent(dwc->dev, sizeof(*dwc->ctrl_req),
2263 &dwc->ctrl_req_addr, GFP_KERNEL);
2264 if (!dwc->ctrl_req) {
2265 dev_err(dwc->dev, "failed to allocate ctrl request\n");
2266 ret = -ENOMEM;
2267 goto err0;
2268 }
2269
2270 dwc->ep0_trb = dma_alloc_coherent(dwc->dev, sizeof(*dwc->ep0_trb),
2271 &dwc->ep0_trb_addr, GFP_KERNEL);
2272 if (!dwc->ep0_trb) {
2273 dev_err(dwc->dev, "failed to allocate ep0 trb\n");
2274 ret = -ENOMEM;
2275 goto err1;
2276 }
2277
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02002278 dwc->setup_buf = kzalloc(sizeof(*dwc->setup_buf) * 2,
2279 GFP_KERNEL);
Felipe Balbi72246da2011-08-19 18:10:58 +03002280 if (!dwc->setup_buf) {
2281 dev_err(dwc->dev, "failed to allocate setup buffer\n");
2282 ret = -ENOMEM;
2283 goto err2;
2284 }
2285
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002286 dwc->ep0_bounce = dma_alloc_coherent(dwc->dev,
2287 512, &dwc->ep0_bounce_addr, GFP_KERNEL);
2288 if (!dwc->ep0_bounce) {
2289 dev_err(dwc->dev, "failed to allocate ep0 bounce buffer\n");
2290 ret = -ENOMEM;
2291 goto err3;
2292 }
2293
Felipe Balbi72246da2011-08-19 18:10:58 +03002294 dev_set_name(&dwc->gadget.dev, "gadget");
2295
2296 dwc->gadget.ops = &dwc3_gadget_ops;
Michal Nazarewiczd327ab52011-11-19 18:27:37 +01002297 dwc->gadget.max_speed = USB_SPEED_SUPER;
Felipe Balbi72246da2011-08-19 18:10:58 +03002298 dwc->gadget.speed = USB_SPEED_UNKNOWN;
2299 dwc->gadget.dev.parent = dwc->dev;
Felipe Balbieeb720f2011-11-28 12:46:59 +02002300 dwc->gadget.sg_supported = true;
Felipe Balbi72246da2011-08-19 18:10:58 +03002301
2302 dma_set_coherent_mask(&dwc->gadget.dev, dwc->dev->coherent_dma_mask);
2303
2304 dwc->gadget.dev.dma_parms = dwc->dev->dma_parms;
2305 dwc->gadget.dev.dma_mask = dwc->dev->dma_mask;
2306 dwc->gadget.dev.release = dwc3_gadget_release;
2307 dwc->gadget.name = "dwc3-gadget";
2308
2309 /*
2310 * REVISIT: Here we should clear all pending IRQs to be
2311 * sure we're starting from a well known location.
2312 */
2313
2314 ret = dwc3_gadget_init_endpoints(dwc);
2315 if (ret)
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002316 goto err4;
Felipe Balbi72246da2011-08-19 18:10:58 +03002317
2318 irq = platform_get_irq(to_platform_device(dwc->dev), 0);
2319
2320 ret = request_irq(irq, dwc3_interrupt, IRQF_SHARED,
2321 "dwc3", dwc);
2322 if (ret) {
2323 dev_err(dwc->dev, "failed to request irq #%d --> %d\n",
2324 irq, ret);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002325 goto err5;
Felipe Balbi72246da2011-08-19 18:10:58 +03002326 }
2327
2328 /* Enable all but Start and End of Frame IRQs */
2329 reg = (DWC3_DEVTEN_VNDRDEVTSTRCVEDEN |
2330 DWC3_DEVTEN_EVNTOVERFLOWEN |
2331 DWC3_DEVTEN_CMDCMPLTEN |
2332 DWC3_DEVTEN_ERRTICERREN |
2333 DWC3_DEVTEN_WKUPEVTEN |
2334 DWC3_DEVTEN_ULSTCNGEN |
2335 DWC3_DEVTEN_CONNECTDONEEN |
2336 DWC3_DEVTEN_USBRSTEN |
2337 DWC3_DEVTEN_DISCONNEVTEN);
2338 dwc3_writel(dwc->regs, DWC3_DEVTEN, reg);
2339
2340 ret = device_register(&dwc->gadget.dev);
2341 if (ret) {
2342 dev_err(dwc->dev, "failed to register gadget device\n");
2343 put_device(&dwc->gadget.dev);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002344 goto err6;
Felipe Balbi72246da2011-08-19 18:10:58 +03002345 }
2346
2347 ret = usb_add_gadget_udc(dwc->dev, &dwc->gadget);
2348 if (ret) {
2349 dev_err(dwc->dev, "failed to register udc\n");
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002350 goto err7;
Felipe Balbi72246da2011-08-19 18:10:58 +03002351 }
2352
2353 return 0;
2354
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002355err7:
Felipe Balbi72246da2011-08-19 18:10:58 +03002356 device_unregister(&dwc->gadget.dev);
2357
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002358err6:
Felipe Balbi72246da2011-08-19 18:10:58 +03002359 dwc3_writel(dwc->regs, DWC3_DEVTEN, 0x00);
2360 free_irq(irq, dwc);
2361
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002362err5:
Felipe Balbi72246da2011-08-19 18:10:58 +03002363 dwc3_gadget_free_endpoints(dwc);
2364
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002365err4:
2366 dma_free_coherent(dwc->dev, 512, dwc->ep0_bounce,
2367 dwc->ep0_bounce_addr);
2368
Felipe Balbi72246da2011-08-19 18:10:58 +03002369err3:
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02002370 kfree(dwc->setup_buf);
Felipe Balbi72246da2011-08-19 18:10:58 +03002371
2372err2:
2373 dma_free_coherent(dwc->dev, sizeof(*dwc->ep0_trb),
2374 dwc->ep0_trb, dwc->ep0_trb_addr);
2375
2376err1:
2377 dma_free_coherent(dwc->dev, sizeof(*dwc->ctrl_req),
2378 dwc->ctrl_req, dwc->ctrl_req_addr);
2379
2380err0:
2381 return ret;
2382}
2383
2384void dwc3_gadget_exit(struct dwc3 *dwc)
2385{
2386 int irq;
Felipe Balbi72246da2011-08-19 18:10:58 +03002387
2388 usb_del_gadget_udc(&dwc->gadget);
2389 irq = platform_get_irq(to_platform_device(dwc->dev), 0);
2390
2391 dwc3_writel(dwc->regs, DWC3_DEVTEN, 0x00);
2392 free_irq(irq, dwc);
2393
Felipe Balbi72246da2011-08-19 18:10:58 +03002394 dwc3_gadget_free_endpoints(dwc);
2395
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002396 dma_free_coherent(dwc->dev, 512, dwc->ep0_bounce,
2397 dwc->ep0_bounce_addr);
2398
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02002399 kfree(dwc->setup_buf);
Felipe Balbi72246da2011-08-19 18:10:58 +03002400
2401 dma_free_coherent(dwc->dev, sizeof(*dwc->ep0_trb),
2402 dwc->ep0_trb, dwc->ep0_trb_addr);
2403
2404 dma_free_coherent(dwc->dev, sizeof(*dwc->ctrl_req),
2405 dwc->ctrl_req, dwc->ctrl_req_addr);
2406
2407 device_unregister(&dwc->gadget.dev);
2408}