blob: 561625a1284d333ddf0dd9ae941eb18bdd73a700 [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
Ido Shayevitz16e78db2012-03-12 20:25:24 +0200181 if (usb_endpoint_xfer_bulk(dep->endpoint.desc)
182 || usb_endpoint_xfer_isoc(dep->endpoint.desc))
Felipe Balbi2e81c362012-02-02 13:01:12 +0200183 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) &&
Ido Shayevitz16e78db2012-03-12 20:25:24 +0200232 usb_endpoint_xfer_isoc(dep->endpoint.desc))
Felipe Balbi72246da2011-08-19 18:10:58 +0300233 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
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +0200241 usb_gadget_unmap_request(&dwc->gadget, &req->request,
242 req->direction);
Felipe Balbi72246da2011-08-19 18:10:58 +0300243
244 dev_dbg(dwc->dev, "request %p from %s completed %d/%d ===> %d\n",
245 req, dep->name, req->request.actual,
246 req->request.length, status);
247
248 spin_unlock(&dwc->lock);
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +0200249 req->request.complete(&dep->endpoint, &req->request);
Felipe Balbi72246da2011-08-19 18:10:58 +0300250 spin_lock(&dwc->lock);
251}
252
253static const char *dwc3_gadget_ep_cmd_string(u8 cmd)
254{
255 switch (cmd) {
256 case DWC3_DEPCMD_DEPSTARTCFG:
257 return "Start New Configuration";
258 case DWC3_DEPCMD_ENDTRANSFER:
259 return "End Transfer";
260 case DWC3_DEPCMD_UPDATETRANSFER:
261 return "Update Transfer";
262 case DWC3_DEPCMD_STARTTRANSFER:
263 return "Start Transfer";
264 case DWC3_DEPCMD_CLEARSTALL:
265 return "Clear Stall";
266 case DWC3_DEPCMD_SETSTALL:
267 return "Set Stall";
268 case DWC3_DEPCMD_GETSEQNUMBER:
269 return "Get Data Sequence Number";
270 case DWC3_DEPCMD_SETTRANSFRESOURCE:
271 return "Set Endpoint Transfer Resource";
272 case DWC3_DEPCMD_SETEPCONFIG:
273 return "Set Endpoint Configuration";
274 default:
275 return "UNKNOWN command";
276 }
277}
278
279int dwc3_send_gadget_ep_cmd(struct dwc3 *dwc, unsigned ep,
280 unsigned cmd, struct dwc3_gadget_ep_cmd_params *params)
281{
282 struct dwc3_ep *dep = dwc->eps[ep];
Sebastian Andrzej Siewior61d58242011-08-29 16:46:38 +0200283 u32 timeout = 500;
Felipe Balbi72246da2011-08-19 18:10:58 +0300284 u32 reg;
285
286 dev_vdbg(dwc->dev, "%s: cmd '%s' params %08x %08x %08x\n",
287 dep->name,
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300288 dwc3_gadget_ep_cmd_string(cmd), params->param0,
289 params->param1, params->param2);
Felipe Balbi72246da2011-08-19 18:10:58 +0300290
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300291 dwc3_writel(dwc->regs, DWC3_DEPCMDPAR0(ep), params->param0);
292 dwc3_writel(dwc->regs, DWC3_DEPCMDPAR1(ep), params->param1);
293 dwc3_writel(dwc->regs, DWC3_DEPCMDPAR2(ep), params->param2);
Felipe Balbi72246da2011-08-19 18:10:58 +0300294
295 dwc3_writel(dwc->regs, DWC3_DEPCMD(ep), cmd | DWC3_DEPCMD_CMDACT);
296 do {
297 reg = dwc3_readl(dwc->regs, DWC3_DEPCMD(ep));
298 if (!(reg & DWC3_DEPCMD_CMDACT)) {
Felipe Balbi164f6e12011-08-27 20:29:58 +0300299 dev_vdbg(dwc->dev, "Command Complete --> %d\n",
300 DWC3_DEPCMD_STATUS(reg));
Felipe Balbi72246da2011-08-19 18:10:58 +0300301 return 0;
302 }
303
304 /*
Felipe Balbi72246da2011-08-19 18:10:58 +0300305 * We can't sleep here, because it is also called from
306 * interrupt context.
307 */
308 timeout--;
309 if (!timeout)
310 return -ETIMEDOUT;
311
Sebastian Andrzej Siewior61d58242011-08-29 16:46:38 +0200312 udelay(1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300313 } while (1);
314}
315
316static dma_addr_t dwc3_trb_dma_offset(struct dwc3_ep *dep,
Felipe Balbif6bafc62012-02-06 11:04:53 +0200317 struct dwc3_trb *trb)
Felipe Balbi72246da2011-08-19 18:10:58 +0300318{
Paul Zimmermanc439ef82011-09-30 10:58:45 +0300319 u32 offset = (char *) trb - (char *) dep->trb_pool;
Felipe Balbi72246da2011-08-19 18:10:58 +0300320
321 return dep->trb_pool_dma + offset;
322}
323
324static int dwc3_alloc_trb_pool(struct dwc3_ep *dep)
325{
326 struct dwc3 *dwc = dep->dwc;
327
328 if (dep->trb_pool)
329 return 0;
330
331 if (dep->number == 0 || dep->number == 1)
332 return 0;
333
334 dep->trb_pool = dma_alloc_coherent(dwc->dev,
335 sizeof(struct dwc3_trb) * DWC3_TRB_NUM,
336 &dep->trb_pool_dma, GFP_KERNEL);
337 if (!dep->trb_pool) {
338 dev_err(dep->dwc->dev, "failed to allocate trb pool for %s\n",
339 dep->name);
340 return -ENOMEM;
341 }
342
343 return 0;
344}
345
346static void dwc3_free_trb_pool(struct dwc3_ep *dep)
347{
348 struct dwc3 *dwc = dep->dwc;
349
350 dma_free_coherent(dwc->dev, sizeof(struct dwc3_trb) * DWC3_TRB_NUM,
351 dep->trb_pool, dep->trb_pool_dma);
352
353 dep->trb_pool = NULL;
354 dep->trb_pool_dma = 0;
355}
356
357static int dwc3_gadget_start_config(struct dwc3 *dwc, struct dwc3_ep *dep)
358{
359 struct dwc3_gadget_ep_cmd_params params;
360 u32 cmd;
361
362 memset(&params, 0x00, sizeof(params));
363
364 if (dep->number != 1) {
365 cmd = DWC3_DEPCMD_DEPSTARTCFG;
366 /* XferRscIdx == 0 for ep0 and 2 for the remaining */
Paul Zimmermanb23c8432011-09-30 10:58:42 +0300367 if (dep->number > 1) {
368 if (dwc->start_config_issued)
369 return 0;
370 dwc->start_config_issued = true;
Felipe Balbi72246da2011-08-19 18:10:58 +0300371 cmd |= DWC3_DEPCMD_PARAM(2);
Paul Zimmermanb23c8432011-09-30 10:58:42 +0300372 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300373
374 return dwc3_send_gadget_ep_cmd(dwc, 0, cmd, &params);
375 }
376
377 return 0;
378}
379
380static int dwc3_gadget_set_ep_config(struct dwc3 *dwc, struct dwc3_ep *dep,
Felipe Balbic90bfae2011-11-29 13:11:21 +0200381 const struct usb_endpoint_descriptor *desc,
382 const struct usb_ss_ep_comp_descriptor *comp_desc)
Felipe Balbi72246da2011-08-19 18:10:58 +0300383{
384 struct dwc3_gadget_ep_cmd_params params;
385
386 memset(&params, 0x00, sizeof(params));
387
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300388 params.param0 = DWC3_DEPCFG_EP_TYPE(usb_endpoint_type(desc))
389 | DWC3_DEPCFG_MAX_PACKET_SIZE(usb_endpoint_maxp(desc))
390 | DWC3_DEPCFG_BURST_SIZE(dep->endpoint.maxburst);
Felipe Balbi72246da2011-08-19 18:10:58 +0300391
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300392 params.param1 = DWC3_DEPCFG_XFER_COMPLETE_EN
393 | DWC3_DEPCFG_XFER_NOT_READY_EN;
Felipe Balbi72246da2011-08-19 18:10:58 +0300394
Felipe Balbi18b7ede2012-01-02 13:35:41 +0200395 if (usb_ss_max_streams(comp_desc) && usb_endpoint_xfer_bulk(desc)) {
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300396 params.param1 |= DWC3_DEPCFG_STREAM_CAPABLE
397 | DWC3_DEPCFG_STREAM_EVENT_EN;
Felipe Balbi879631a2011-09-30 10:58:47 +0300398 dep->stream_capable = true;
399 }
400
Felipe Balbi72246da2011-08-19 18:10:58 +0300401 if (usb_endpoint_xfer_isoc(desc))
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300402 params.param1 |= DWC3_DEPCFG_XFER_IN_PROGRESS_EN;
Felipe Balbi72246da2011-08-19 18:10:58 +0300403
404 /*
405 * We are doing 1:1 mapping for endpoints, meaning
406 * Physical Endpoints 2 maps to Logical Endpoint 2 and
407 * so on. We consider the direction bit as part of the physical
408 * endpoint number. So USB endpoint 0x81 is 0x03.
409 */
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300410 params.param1 |= DWC3_DEPCFG_EP_NUMBER(dep->number);
Felipe Balbi72246da2011-08-19 18:10:58 +0300411
412 /*
413 * We must use the lower 16 TX FIFOs even though
414 * HW might have more
415 */
416 if (dep->direction)
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300417 params.param0 |= DWC3_DEPCFG_FIFO_NUMBER(dep->number >> 1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300418
419 if (desc->bInterval) {
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300420 params.param1 |= DWC3_DEPCFG_BINTERVAL_M1(desc->bInterval - 1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300421 dep->interval = 1 << (desc->bInterval - 1);
422 }
423
424 return dwc3_send_gadget_ep_cmd(dwc, dep->number,
425 DWC3_DEPCMD_SETEPCONFIG, &params);
426}
427
428static int dwc3_gadget_set_xfer_resource(struct dwc3 *dwc, struct dwc3_ep *dep)
429{
430 struct dwc3_gadget_ep_cmd_params params;
431
432 memset(&params, 0x00, sizeof(params));
433
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300434 params.param0 = DWC3_DEPXFERCFG_NUM_XFER_RES(1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300435
436 return dwc3_send_gadget_ep_cmd(dwc, dep->number,
437 DWC3_DEPCMD_SETTRANSFRESOURCE, &params);
438}
439
440/**
441 * __dwc3_gadget_ep_enable - Initializes a HW endpoint
442 * @dep: endpoint to be initialized
443 * @desc: USB Endpoint Descriptor
444 *
445 * Caller should take care of locking
446 */
447static int __dwc3_gadget_ep_enable(struct dwc3_ep *dep,
Felipe Balbic90bfae2011-11-29 13:11:21 +0200448 const struct usb_endpoint_descriptor *desc,
449 const struct usb_ss_ep_comp_descriptor *comp_desc)
Felipe Balbi72246da2011-08-19 18:10:58 +0300450{
451 struct dwc3 *dwc = dep->dwc;
452 u32 reg;
453 int ret = -ENOMEM;
454
455 if (!(dep->flags & DWC3_EP_ENABLED)) {
456 ret = dwc3_gadget_start_config(dwc, dep);
457 if (ret)
458 return ret;
459 }
460
Felipe Balbic90bfae2011-11-29 13:11:21 +0200461 ret = dwc3_gadget_set_ep_config(dwc, dep, desc, comp_desc);
Felipe Balbi72246da2011-08-19 18:10:58 +0300462 if (ret)
463 return ret;
464
465 if (!(dep->flags & DWC3_EP_ENABLED)) {
Felipe Balbif6bafc62012-02-06 11:04:53 +0200466 struct dwc3_trb *trb_st_hw;
467 struct dwc3_trb *trb_link;
Felipe Balbi72246da2011-08-19 18:10:58 +0300468
469 ret = dwc3_gadget_set_xfer_resource(dwc, dep);
470 if (ret)
471 return ret;
472
Ido Shayevitz16e78db2012-03-12 20:25:24 +0200473 dep->endpoint.desc = desc;
Felipe Balbic90bfae2011-11-29 13:11:21 +0200474 dep->comp_desc = comp_desc;
Felipe Balbi72246da2011-08-19 18:10:58 +0300475 dep->type = usb_endpoint_type(desc);
476 dep->flags |= DWC3_EP_ENABLED;
477
478 reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
479 reg |= DWC3_DALEPENA_EP(dep->number);
480 dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
481
482 if (!usb_endpoint_xfer_isoc(desc))
483 return 0;
484
485 memset(&trb_link, 0, sizeof(trb_link));
486
Paul Zimmerman1d046792012-02-15 18:56:56 -0800487 /* Link TRB for ISOC. The HWO bit is never reset */
Felipe Balbi72246da2011-08-19 18:10:58 +0300488 trb_st_hw = &dep->trb_pool[0];
489
Felipe Balbif6bafc62012-02-06 11:04:53 +0200490 trb_link = &dep->trb_pool[DWC3_TRB_NUM - 1];
Felipe Balbi72246da2011-08-19 18:10:58 +0300491
Felipe Balbif6bafc62012-02-06 11:04:53 +0200492 trb_link->bpl = lower_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw));
493 trb_link->bph = upper_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw));
494 trb_link->ctrl |= DWC3_TRBCTL_LINK_TRB;
495 trb_link->ctrl |= DWC3_TRB_CTRL_HWO;
Felipe Balbi72246da2011-08-19 18:10:58 +0300496 }
497
498 return 0;
499}
500
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200501static void dwc3_stop_active_transfer(struct dwc3 *dwc, u32 epnum);
502static void dwc3_remove_requests(struct dwc3 *dwc, struct dwc3_ep *dep)
Felipe Balbi72246da2011-08-19 18:10:58 +0300503{
504 struct dwc3_request *req;
505
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200506 if (!list_empty(&dep->req_queued))
507 dwc3_stop_active_transfer(dwc, dep->number);
508
Felipe Balbi72246da2011-08-19 18:10:58 +0300509 while (!list_empty(&dep->request_list)) {
510 req = next_request(&dep->request_list);
511
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200512 dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
Felipe Balbi72246da2011-08-19 18:10:58 +0300513 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300514}
515
516/**
517 * __dwc3_gadget_ep_disable - Disables a HW endpoint
518 * @dep: the endpoint to disable
519 *
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200520 * This function also removes requests which are currently processed ny the
521 * hardware and those which are not yet scheduled.
522 * Caller should take care of locking.
Felipe Balbi72246da2011-08-19 18:10:58 +0300523 */
Felipe Balbi72246da2011-08-19 18:10:58 +0300524static int __dwc3_gadget_ep_disable(struct dwc3_ep *dep)
525{
526 struct dwc3 *dwc = dep->dwc;
527 u32 reg;
528
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200529 dwc3_remove_requests(dwc, dep);
Felipe Balbi72246da2011-08-19 18:10:58 +0300530
531 reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
532 reg &= ~DWC3_DALEPENA_EP(dep->number);
533 dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
534
Felipe Balbi879631a2011-09-30 10:58:47 +0300535 dep->stream_capable = false;
Ido Shayevitzf9c56cd2012-02-08 13:56:48 +0200536 dep->endpoint.desc = NULL;
Felipe Balbic90bfae2011-11-29 13:11:21 +0200537 dep->comp_desc = NULL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300538 dep->type = 0;
Felipe Balbi879631a2011-09-30 10:58:47 +0300539 dep->flags = 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300540
541 return 0;
542}
543
544/* -------------------------------------------------------------------------- */
545
546static int dwc3_gadget_ep0_enable(struct usb_ep *ep,
547 const struct usb_endpoint_descriptor *desc)
548{
549 return -EINVAL;
550}
551
552static int dwc3_gadget_ep0_disable(struct usb_ep *ep)
553{
554 return -EINVAL;
555}
556
557/* -------------------------------------------------------------------------- */
558
559static int dwc3_gadget_ep_enable(struct usb_ep *ep,
560 const struct usb_endpoint_descriptor *desc)
561{
562 struct dwc3_ep *dep;
563 struct dwc3 *dwc;
564 unsigned long flags;
565 int ret;
566
567 if (!ep || !desc || desc->bDescriptorType != USB_DT_ENDPOINT) {
568 pr_debug("dwc3: invalid parameters\n");
569 return -EINVAL;
570 }
571
572 if (!desc->wMaxPacketSize) {
573 pr_debug("dwc3: missing wMaxPacketSize\n");
574 return -EINVAL;
575 }
576
577 dep = to_dwc3_ep(ep);
578 dwc = dep->dwc;
579
580 switch (usb_endpoint_type(desc)) {
581 case USB_ENDPOINT_XFER_CONTROL:
Anton Tikhomirov27a78d62012-02-23 15:38:46 +0900582 strlcat(dep->name, "-control", sizeof(dep->name));
Felipe Balbi72246da2011-08-19 18:10:58 +0300583 break;
584 case USB_ENDPOINT_XFER_ISOC:
Anton Tikhomirov27a78d62012-02-23 15:38:46 +0900585 strlcat(dep->name, "-isoc", sizeof(dep->name));
Felipe Balbi72246da2011-08-19 18:10:58 +0300586 break;
587 case USB_ENDPOINT_XFER_BULK:
Anton Tikhomirov27a78d62012-02-23 15:38:46 +0900588 strlcat(dep->name, "-bulk", sizeof(dep->name));
Felipe Balbi72246da2011-08-19 18:10:58 +0300589 break;
590 case USB_ENDPOINT_XFER_INT:
Anton Tikhomirov27a78d62012-02-23 15:38:46 +0900591 strlcat(dep->name, "-int", sizeof(dep->name));
Felipe Balbi72246da2011-08-19 18:10:58 +0300592 break;
593 default:
594 dev_err(dwc->dev, "invalid endpoint transfer type\n");
595 }
596
597 if (dep->flags & DWC3_EP_ENABLED) {
598 dev_WARN_ONCE(dwc->dev, true, "%s is already enabled\n",
599 dep->name);
600 return 0;
601 }
602
603 dev_vdbg(dwc->dev, "Enabling %s\n", dep->name);
604
605 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbic90bfae2011-11-29 13:11:21 +0200606 ret = __dwc3_gadget_ep_enable(dep, desc, ep->comp_desc);
Felipe Balbi72246da2011-08-19 18:10:58 +0300607 spin_unlock_irqrestore(&dwc->lock, flags);
608
609 return ret;
610}
611
612static int dwc3_gadget_ep_disable(struct usb_ep *ep)
613{
614 struct dwc3_ep *dep;
615 struct dwc3 *dwc;
616 unsigned long flags;
617 int ret;
618
619 if (!ep) {
620 pr_debug("dwc3: invalid parameters\n");
621 return -EINVAL;
622 }
623
624 dep = to_dwc3_ep(ep);
625 dwc = dep->dwc;
626
627 if (!(dep->flags & DWC3_EP_ENABLED)) {
628 dev_WARN_ONCE(dwc->dev, true, "%s is already disabled\n",
629 dep->name);
630 return 0;
631 }
632
633 snprintf(dep->name, sizeof(dep->name), "ep%d%s",
634 dep->number >> 1,
635 (dep->number & 1) ? "in" : "out");
636
637 spin_lock_irqsave(&dwc->lock, flags);
638 ret = __dwc3_gadget_ep_disable(dep);
639 spin_unlock_irqrestore(&dwc->lock, flags);
640
641 return ret;
642}
643
644static struct usb_request *dwc3_gadget_ep_alloc_request(struct usb_ep *ep,
645 gfp_t gfp_flags)
646{
647 struct dwc3_request *req;
648 struct dwc3_ep *dep = to_dwc3_ep(ep);
649 struct dwc3 *dwc = dep->dwc;
650
651 req = kzalloc(sizeof(*req), gfp_flags);
652 if (!req) {
653 dev_err(dwc->dev, "not enough memory\n");
654 return NULL;
655 }
656
657 req->epnum = dep->number;
658 req->dep = dep;
Felipe Balbi72246da2011-08-19 18:10:58 +0300659
660 return &req->request;
661}
662
663static void dwc3_gadget_ep_free_request(struct usb_ep *ep,
664 struct usb_request *request)
665{
666 struct dwc3_request *req = to_dwc3_request(request);
667
668 kfree(req);
669}
670
Felipe Balbic71fc372011-11-22 11:37:34 +0200671/**
672 * dwc3_prepare_one_trb - setup one TRB from one request
673 * @dep: endpoint for which this request is prepared
674 * @req: dwc3_request pointer
675 */
Felipe Balbi68e823e2011-11-28 12:25:01 +0200676static void dwc3_prepare_one_trb(struct dwc3_ep *dep,
Felipe Balbieeb720f2011-11-28 12:46:59 +0200677 struct dwc3_request *req, dma_addr_t dma,
678 unsigned length, unsigned last, unsigned chain)
Felipe Balbic71fc372011-11-22 11:37:34 +0200679{
Felipe Balbieeb720f2011-11-28 12:46:59 +0200680 struct dwc3 *dwc = dep->dwc;
Felipe Balbif6bafc62012-02-06 11:04:53 +0200681 struct dwc3_trb *trb;
Felipe Balbic71fc372011-11-22 11:37:34 +0200682
683 unsigned int cur_slot;
684
Felipe Balbieeb720f2011-11-28 12:46:59 +0200685 dev_vdbg(dwc->dev, "%s: req %p dma %08llx length %d%s%s\n",
686 dep->name, req, (unsigned long long) dma,
687 length, last ? " last" : "",
688 chain ? " chain" : "");
689
Felipe Balbif6bafc62012-02-06 11:04:53 +0200690 trb = &dep->trb_pool[dep->free_slot & DWC3_TRB_MASK];
Felipe Balbic71fc372011-11-22 11:37:34 +0200691 cur_slot = dep->free_slot;
692 dep->free_slot++;
693
694 /* Skip the LINK-TRB on ISOC */
695 if (((cur_slot & DWC3_TRB_MASK) == DWC3_TRB_NUM - 1) &&
Ido Shayevitz16e78db2012-03-12 20:25:24 +0200696 usb_endpoint_xfer_isoc(dep->endpoint.desc))
Felipe Balbi68e823e2011-11-28 12:25:01 +0200697 return;
Felipe Balbic71fc372011-11-22 11:37:34 +0200698
Felipe Balbieeb720f2011-11-28 12:46:59 +0200699 if (!req->trb) {
700 dwc3_gadget_move_request_queued(req);
Felipe Balbif6bafc62012-02-06 11:04:53 +0200701 req->trb = trb;
702 req->trb_dma = dwc3_trb_dma_offset(dep, trb);
Felipe Balbieeb720f2011-11-28 12:46:59 +0200703 }
Felipe Balbic71fc372011-11-22 11:37:34 +0200704
Felipe Balbif6bafc62012-02-06 11:04:53 +0200705 trb->size = DWC3_TRB_SIZE_LENGTH(length);
706 trb->bpl = lower_32_bits(dma);
707 trb->bph = upper_32_bits(dma);
Felipe Balbic71fc372011-11-22 11:37:34 +0200708
Ido Shayevitz16e78db2012-03-12 20:25:24 +0200709 switch (usb_endpoint_type(dep->endpoint.desc)) {
Felipe Balbic71fc372011-11-22 11:37:34 +0200710 case USB_ENDPOINT_XFER_CONTROL:
Felipe Balbif6bafc62012-02-06 11:04:53 +0200711 trb->ctrl = DWC3_TRBCTL_CONTROL_SETUP;
Felipe Balbic71fc372011-11-22 11:37:34 +0200712 break;
713
714 case USB_ENDPOINT_XFER_ISOC:
Felipe Balbif6bafc62012-02-06 11:04:53 +0200715 trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS_FIRST;
Felipe Balbic71fc372011-11-22 11:37:34 +0200716
717 /* IOC every DWC3_TRB_NUM / 4 so we can refill */
718 if (!(cur_slot % (DWC3_TRB_NUM / 4)))
Felipe Balbif6bafc62012-02-06 11:04:53 +0200719 trb->ctrl |= DWC3_TRB_CTRL_IOC;
Felipe Balbic71fc372011-11-22 11:37:34 +0200720 break;
721
722 case USB_ENDPOINT_XFER_BULK:
723 case USB_ENDPOINT_XFER_INT:
Felipe Balbif6bafc62012-02-06 11:04:53 +0200724 trb->ctrl = DWC3_TRBCTL_NORMAL;
Felipe Balbic71fc372011-11-22 11:37:34 +0200725 break;
726 default:
727 /*
728 * This is only possible with faulty memory because we
729 * checked it already :)
730 */
731 BUG();
732 }
733
Ido Shayevitz16e78db2012-03-12 20:25:24 +0200734 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbif6bafc62012-02-06 11:04:53 +0200735 trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI;
736 trb->ctrl |= DWC3_TRB_CTRL_CSP;
737 } else {
738 if (chain)
739 trb->ctrl |= DWC3_TRB_CTRL_CHN;
Felipe Balbic71fc372011-11-22 11:37:34 +0200740
Felipe Balbif6bafc62012-02-06 11:04:53 +0200741 if (last)
742 trb->ctrl |= DWC3_TRB_CTRL_LST;
743 }
744
Ido Shayevitz16e78db2012-03-12 20:25:24 +0200745 if (usb_endpoint_xfer_bulk(dep->endpoint.desc) && dep->stream_capable)
Felipe Balbif6bafc62012-02-06 11:04:53 +0200746 trb->ctrl |= DWC3_TRB_CTRL_SID_SOFN(req->request.stream_id);
747
748 trb->ctrl |= DWC3_TRB_CTRL_HWO;
Felipe Balbic71fc372011-11-22 11:37:34 +0200749}
750
Felipe Balbi72246da2011-08-19 18:10:58 +0300751/*
752 * dwc3_prepare_trbs - setup TRBs from requests
753 * @dep: endpoint for which requests are being prepared
754 * @starting: true if the endpoint is idle and no requests are queued.
755 *
Paul Zimmerman1d046792012-02-15 18:56:56 -0800756 * The function goes through the requests list and sets up TRBs for the
757 * transfers. The function returns once there are no more TRBs available or
758 * it runs out of requests.
Felipe Balbi72246da2011-08-19 18:10:58 +0300759 */
Felipe Balbi68e823e2011-11-28 12:25:01 +0200760static void dwc3_prepare_trbs(struct dwc3_ep *dep, bool starting)
Felipe Balbi72246da2011-08-19 18:10:58 +0300761{
Felipe Balbi68e823e2011-11-28 12:25:01 +0200762 struct dwc3_request *req, *n;
Felipe Balbi72246da2011-08-19 18:10:58 +0300763 u32 trbs_left;
Paul Zimmerman8d62cd62012-02-15 13:35:06 +0200764 u32 max;
Felipe Balbic71fc372011-11-22 11:37:34 +0200765 unsigned int last_one = 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300766
767 BUILD_BUG_ON_NOT_POWER_OF_2(DWC3_TRB_NUM);
768
769 /* the first request must not be queued */
770 trbs_left = (dep->busy_slot - dep->free_slot) & DWC3_TRB_MASK;
Felipe Balbic71fc372011-11-22 11:37:34 +0200771
Paul Zimmerman8d62cd62012-02-15 13:35:06 +0200772 /* Can't wrap around on a non-isoc EP since there's no link TRB */
Ido Shayevitz16e78db2012-03-12 20:25:24 +0200773 if (!usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Paul Zimmerman8d62cd62012-02-15 13:35:06 +0200774 max = DWC3_TRB_NUM - (dep->free_slot & DWC3_TRB_MASK);
775 if (trbs_left > max)
776 trbs_left = max;
777 }
778
Felipe Balbi72246da2011-08-19 18:10:58 +0300779 /*
Paul Zimmerman1d046792012-02-15 18:56:56 -0800780 * If busy & slot are equal than it is either full or empty. If we are
781 * starting to process requests then we are empty. Otherwise we are
Felipe Balbi72246da2011-08-19 18:10:58 +0300782 * full and don't do anything
783 */
784 if (!trbs_left) {
785 if (!starting)
Felipe Balbi68e823e2011-11-28 12:25:01 +0200786 return;
Felipe Balbi72246da2011-08-19 18:10:58 +0300787 trbs_left = DWC3_TRB_NUM;
788 /*
789 * In case we start from scratch, we queue the ISOC requests
790 * starting from slot 1. This is done because we use ring
791 * buffer and have no LST bit to stop us. Instead, we place
Paul Zimmerman1d046792012-02-15 18:56:56 -0800792 * IOC bit every TRB_NUM/4. We try to avoid having an interrupt
Felipe Balbi72246da2011-08-19 18:10:58 +0300793 * after the first request so we start at slot 1 and have
794 * 7 requests proceed before we hit the first IOC.
795 * Other transfer types don't use the ring buffer and are
796 * processed from the first TRB until the last one. Since we
797 * don't wrap around we have to start at the beginning.
798 */
Ido Shayevitz16e78db2012-03-12 20:25:24 +0200799 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbi72246da2011-08-19 18:10:58 +0300800 dep->busy_slot = 1;
801 dep->free_slot = 1;
802 } else {
803 dep->busy_slot = 0;
804 dep->free_slot = 0;
805 }
806 }
807
808 /* The last TRB is a link TRB, not used for xfer */
Ido Shayevitz16e78db2012-03-12 20:25:24 +0200809 if ((trbs_left <= 1) && usb_endpoint_xfer_isoc(dep->endpoint.desc))
Felipe Balbi68e823e2011-11-28 12:25:01 +0200810 return;
Felipe Balbi72246da2011-08-19 18:10:58 +0300811
812 list_for_each_entry_safe(req, n, &dep->request_list, list) {
Felipe Balbieeb720f2011-11-28 12:46:59 +0200813 unsigned length;
814 dma_addr_t dma;
Felipe Balbi72246da2011-08-19 18:10:58 +0300815
Felipe Balbieeb720f2011-11-28 12:46:59 +0200816 if (req->request.num_mapped_sgs > 0) {
817 struct usb_request *request = &req->request;
818 struct scatterlist *sg = request->sg;
819 struct scatterlist *s;
820 int i;
Felipe Balbi72246da2011-08-19 18:10:58 +0300821
Felipe Balbieeb720f2011-11-28 12:46:59 +0200822 for_each_sg(sg, s, request->num_mapped_sgs, i) {
823 unsigned chain = true;
Felipe Balbi72246da2011-08-19 18:10:58 +0300824
Felipe Balbieeb720f2011-11-28 12:46:59 +0200825 length = sg_dma_len(s);
826 dma = sg_dma_address(s);
Felipe Balbi72246da2011-08-19 18:10:58 +0300827
Paul Zimmerman1d046792012-02-15 18:56:56 -0800828 if (i == (request->num_mapped_sgs - 1) ||
829 sg_is_last(s)) {
Felipe Balbieeb720f2011-11-28 12:46:59 +0200830 last_one = true;
831 chain = false;
832 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300833
Felipe Balbieeb720f2011-11-28 12:46:59 +0200834 trbs_left--;
835 if (!trbs_left)
836 last_one = true;
Felipe Balbi72246da2011-08-19 18:10:58 +0300837
Felipe Balbieeb720f2011-11-28 12:46:59 +0200838 if (last_one)
839 chain = false;
Felipe Balbi72246da2011-08-19 18:10:58 +0300840
Felipe Balbieeb720f2011-11-28 12:46:59 +0200841 dwc3_prepare_one_trb(dep, req, dma, length,
842 last_one, chain);
Felipe Balbi72246da2011-08-19 18:10:58 +0300843
Felipe Balbieeb720f2011-11-28 12:46:59 +0200844 if (last_one)
845 break;
846 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300847 } else {
Felipe Balbieeb720f2011-11-28 12:46:59 +0200848 dma = req->request.dma;
849 length = req->request.length;
850 trbs_left--;
851
852 if (!trbs_left)
853 last_one = 1;
854
855 /* Is this the last request? */
856 if (list_is_last(&req->list, &dep->request_list))
857 last_one = 1;
858
859 dwc3_prepare_one_trb(dep, req, dma, length,
860 last_one, false);
861
862 if (last_one)
863 break;
Felipe Balbi72246da2011-08-19 18:10:58 +0300864 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300865 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300866}
867
868static int __dwc3_gadget_kick_transfer(struct dwc3_ep *dep, u16 cmd_param,
869 int start_new)
870{
871 struct dwc3_gadget_ep_cmd_params params;
872 struct dwc3_request *req;
873 struct dwc3 *dwc = dep->dwc;
874 int ret;
875 u32 cmd;
876
877 if (start_new && (dep->flags & DWC3_EP_BUSY)) {
878 dev_vdbg(dwc->dev, "%s: endpoint busy\n", dep->name);
879 return -EBUSY;
880 }
881 dep->flags &= ~DWC3_EP_PENDING_REQUEST;
882
883 /*
884 * If we are getting here after a short-out-packet we don't enqueue any
885 * new requests as we try to set the IOC bit only on the last request.
886 */
887 if (start_new) {
888 if (list_empty(&dep->req_queued))
889 dwc3_prepare_trbs(dep, start_new);
890
891 /* req points to the first request which will be sent */
892 req = next_request(&dep->req_queued);
893 } else {
Felipe Balbi68e823e2011-11-28 12:25:01 +0200894 dwc3_prepare_trbs(dep, start_new);
895
Felipe Balbi72246da2011-08-19 18:10:58 +0300896 /*
Paul Zimmerman1d046792012-02-15 18:56:56 -0800897 * req points to the first request where HWO changed from 0 to 1
Felipe Balbi72246da2011-08-19 18:10:58 +0300898 */
Felipe Balbi68e823e2011-11-28 12:25:01 +0200899 req = next_request(&dep->req_queued);
Felipe Balbi72246da2011-08-19 18:10:58 +0300900 }
901 if (!req) {
902 dep->flags |= DWC3_EP_PENDING_REQUEST;
903 return 0;
904 }
905
906 memset(&params, 0, sizeof(params));
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300907 params.param0 = upper_32_bits(req->trb_dma);
908 params.param1 = lower_32_bits(req->trb_dma);
Felipe Balbi72246da2011-08-19 18:10:58 +0300909
910 if (start_new)
911 cmd = DWC3_DEPCMD_STARTTRANSFER;
912 else
913 cmd = DWC3_DEPCMD_UPDATETRANSFER;
914
915 cmd |= DWC3_DEPCMD_PARAM(cmd_param);
916 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number, cmd, &params);
917 if (ret < 0) {
918 dev_dbg(dwc->dev, "failed to send STARTTRANSFER command\n");
919
920 /*
921 * FIXME we need to iterate over the list of requests
922 * here and stop, unmap, free and del each of the linked
Paul Zimmerman1d046792012-02-15 18:56:56 -0800923 * requests instead of what we do now.
Felipe Balbi72246da2011-08-19 18:10:58 +0300924 */
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +0200925 usb_gadget_unmap_request(&dwc->gadget, &req->request,
926 req->direction);
Felipe Balbi72246da2011-08-19 18:10:58 +0300927 list_del(&req->list);
928 return ret;
929 }
930
931 dep->flags |= DWC3_EP_BUSY;
932 dep->res_trans_idx = dwc3_gadget_ep_get_transfer_index(dwc,
933 dep->number);
Felipe Balbi25b8ff62011-11-04 12:32:47 +0200934
935 WARN_ON_ONCE(!dep->res_trans_idx);
936
Felipe Balbi72246da2011-08-19 18:10:58 +0300937 return 0;
938}
939
940static int __dwc3_gadget_ep_queue(struct dwc3_ep *dep, struct dwc3_request *req)
941{
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +0200942 struct dwc3 *dwc = dep->dwc;
943 int ret;
944
Felipe Balbi72246da2011-08-19 18:10:58 +0300945 req->request.actual = 0;
946 req->request.status = -EINPROGRESS;
947 req->direction = dep->direction;
948 req->epnum = dep->number;
949
950 /*
951 * We only add to our list of requests now and
952 * start consuming the list once we get XferNotReady
953 * IRQ.
954 *
955 * That way, we avoid doing anything that we don't need
956 * to do now and defer it until the point we receive a
957 * particular token from the Host side.
958 *
959 * This will also avoid Host cancelling URBs due to too
Paul Zimmerman1d046792012-02-15 18:56:56 -0800960 * many NAKs.
Felipe Balbi72246da2011-08-19 18:10:58 +0300961 */
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +0200962 ret = usb_gadget_map_request(&dwc->gadget, &req->request,
963 dep->direction);
964 if (ret)
965 return ret;
966
Felipe Balbi72246da2011-08-19 18:10:58 +0300967 list_add_tail(&req->list, &dep->request_list);
968
969 /*
970 * There is one special case: XferNotReady with
971 * empty list of requests. We need to kick the
972 * transfer here in that situation, otherwise
973 * we will be NAKing forever.
974 *
975 * If we get XferNotReady before gadget driver
976 * has a chance to queue a request, we will ACK
977 * the IRQ but won't be able to receive the data
978 * until the next request is queued. The following
979 * code is handling exactly that.
980 */
981 if (dep->flags & DWC3_EP_PENDING_REQUEST) {
982 int ret;
983 int start_trans;
984
985 start_trans = 1;
Ido Shayevitz16e78db2012-03-12 20:25:24 +0200986 if (usb_endpoint_xfer_isoc(dep->endpoint.desc) &&
Paul Zimmerman1d046792012-02-15 18:56:56 -0800987 (dep->flags & DWC3_EP_BUSY))
Felipe Balbi72246da2011-08-19 18:10:58 +0300988 start_trans = 0;
989
Paul Zimmerman1d046792012-02-15 18:56:56 -0800990 ret = __dwc3_gadget_kick_transfer(dep, 0, start_trans);
Felipe Balbi72246da2011-08-19 18:10:58 +0300991 if (ret && ret != -EBUSY) {
992 struct dwc3 *dwc = dep->dwc;
993
994 dev_dbg(dwc->dev, "%s: failed to kick transfers\n",
995 dep->name);
996 }
997 };
998
999 return 0;
1000}
1001
1002static int dwc3_gadget_ep_queue(struct usb_ep *ep, struct usb_request *request,
1003 gfp_t gfp_flags)
1004{
1005 struct dwc3_request *req = to_dwc3_request(request);
1006 struct dwc3_ep *dep = to_dwc3_ep(ep);
1007 struct dwc3 *dwc = dep->dwc;
1008
1009 unsigned long flags;
1010
1011 int ret;
1012
Ido Shayevitz16e78db2012-03-12 20:25:24 +02001013 if (!dep->endpoint.desc) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001014 dev_dbg(dwc->dev, "trying to queue request %p to disabled %s\n",
1015 request, ep->name);
1016 return -ESHUTDOWN;
1017 }
1018
1019 dev_vdbg(dwc->dev, "queing request %p to %s length %d\n",
1020 request, ep->name, request->length);
1021
1022 spin_lock_irqsave(&dwc->lock, flags);
1023 ret = __dwc3_gadget_ep_queue(dep, req);
1024 spin_unlock_irqrestore(&dwc->lock, flags);
1025
1026 return ret;
1027}
1028
1029static int dwc3_gadget_ep_dequeue(struct usb_ep *ep,
1030 struct usb_request *request)
1031{
1032 struct dwc3_request *req = to_dwc3_request(request);
1033 struct dwc3_request *r = NULL;
1034
1035 struct dwc3_ep *dep = to_dwc3_ep(ep);
1036 struct dwc3 *dwc = dep->dwc;
1037
1038 unsigned long flags;
1039 int ret = 0;
1040
1041 spin_lock_irqsave(&dwc->lock, flags);
1042
1043 list_for_each_entry(r, &dep->request_list, list) {
1044 if (r == req)
1045 break;
1046 }
1047
1048 if (r != req) {
1049 list_for_each_entry(r, &dep->req_queued, list) {
1050 if (r == req)
1051 break;
1052 }
1053 if (r == req) {
1054 /* wait until it is processed */
1055 dwc3_stop_active_transfer(dwc, dep->number);
1056 goto out0;
1057 }
1058 dev_err(dwc->dev, "request %p was not queued to %s\n",
1059 request, ep->name);
1060 ret = -EINVAL;
1061 goto out0;
1062 }
1063
1064 /* giveback the request */
1065 dwc3_gadget_giveback(dep, req, -ECONNRESET);
1066
1067out0:
1068 spin_unlock_irqrestore(&dwc->lock, flags);
1069
1070 return ret;
1071}
1072
1073int __dwc3_gadget_ep_set_halt(struct dwc3_ep *dep, int value)
1074{
1075 struct dwc3_gadget_ep_cmd_params params;
1076 struct dwc3 *dwc = dep->dwc;
1077 int ret;
1078
1079 memset(&params, 0x00, sizeof(params));
1080
1081 if (value) {
Felipe Balbi0b7836a2011-08-30 15:48:08 +03001082 if (dep->number == 0 || dep->number == 1) {
1083 /*
1084 * Whenever EP0 is stalled, we will restart
1085 * the state machine, thus moving back to
1086 * Setup Phase
1087 */
1088 dwc->ep0state = EP0_SETUP_PHASE;
1089 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001090
1091 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number,
1092 DWC3_DEPCMD_SETSTALL, &params);
1093 if (ret)
1094 dev_err(dwc->dev, "failed to %s STALL on %s\n",
1095 value ? "set" : "clear",
1096 dep->name);
1097 else
1098 dep->flags |= DWC3_EP_STALL;
1099 } else {
Paul Zimmerman52754552011-09-30 10:58:44 +03001100 if (dep->flags & DWC3_EP_WEDGE)
1101 return 0;
1102
Felipe Balbi72246da2011-08-19 18:10:58 +03001103 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number,
1104 DWC3_DEPCMD_CLEARSTALL, &params);
1105 if (ret)
1106 dev_err(dwc->dev, "failed to %s STALL on %s\n",
1107 value ? "set" : "clear",
1108 dep->name);
1109 else
1110 dep->flags &= ~DWC3_EP_STALL;
1111 }
Paul Zimmerman52754552011-09-30 10:58:44 +03001112
Felipe Balbi72246da2011-08-19 18:10:58 +03001113 return ret;
1114}
1115
1116static int dwc3_gadget_ep_set_halt(struct usb_ep *ep, int value)
1117{
1118 struct dwc3_ep *dep = to_dwc3_ep(ep);
1119 struct dwc3 *dwc = dep->dwc;
1120
1121 unsigned long flags;
1122
1123 int ret;
1124
1125 spin_lock_irqsave(&dwc->lock, flags);
1126
Ido Shayevitz16e78db2012-03-12 20:25:24 +02001127 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001128 dev_err(dwc->dev, "%s is of Isochronous type\n", dep->name);
1129 ret = -EINVAL;
1130 goto out;
1131 }
1132
1133 ret = __dwc3_gadget_ep_set_halt(dep, value);
1134out:
1135 spin_unlock_irqrestore(&dwc->lock, flags);
1136
1137 return ret;
1138}
1139
1140static int dwc3_gadget_ep_set_wedge(struct usb_ep *ep)
1141{
1142 struct dwc3_ep *dep = to_dwc3_ep(ep);
Paul Zimmerman249a4562012-02-24 17:32:16 -08001143 struct dwc3 *dwc = dep->dwc;
1144 unsigned long flags;
Felipe Balbi72246da2011-08-19 18:10:58 +03001145
Paul Zimmerman249a4562012-02-24 17:32:16 -08001146 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001147 dep->flags |= DWC3_EP_WEDGE;
Paul Zimmerman249a4562012-02-24 17:32:16 -08001148 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001149
Paul Zimmerman52754552011-09-30 10:58:44 +03001150 return dwc3_gadget_ep_set_halt(ep, 1);
Felipe Balbi72246da2011-08-19 18:10:58 +03001151}
1152
1153/* -------------------------------------------------------------------------- */
1154
1155static struct usb_endpoint_descriptor dwc3_gadget_ep0_desc = {
1156 .bLength = USB_DT_ENDPOINT_SIZE,
1157 .bDescriptorType = USB_DT_ENDPOINT,
1158 .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
1159};
1160
1161static const struct usb_ep_ops dwc3_gadget_ep0_ops = {
1162 .enable = dwc3_gadget_ep0_enable,
1163 .disable = dwc3_gadget_ep0_disable,
1164 .alloc_request = dwc3_gadget_ep_alloc_request,
1165 .free_request = dwc3_gadget_ep_free_request,
1166 .queue = dwc3_gadget_ep0_queue,
1167 .dequeue = dwc3_gadget_ep_dequeue,
1168 .set_halt = dwc3_gadget_ep_set_halt,
1169 .set_wedge = dwc3_gadget_ep_set_wedge,
1170};
1171
1172static const struct usb_ep_ops dwc3_gadget_ep_ops = {
1173 .enable = dwc3_gadget_ep_enable,
1174 .disable = dwc3_gadget_ep_disable,
1175 .alloc_request = dwc3_gadget_ep_alloc_request,
1176 .free_request = dwc3_gadget_ep_free_request,
1177 .queue = dwc3_gadget_ep_queue,
1178 .dequeue = dwc3_gadget_ep_dequeue,
1179 .set_halt = dwc3_gadget_ep_set_halt,
1180 .set_wedge = dwc3_gadget_ep_set_wedge,
1181};
1182
1183/* -------------------------------------------------------------------------- */
1184
1185static int dwc3_gadget_get_frame(struct usb_gadget *g)
1186{
1187 struct dwc3 *dwc = gadget_to_dwc(g);
1188 u32 reg;
1189
1190 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1191 return DWC3_DSTS_SOFFN(reg);
1192}
1193
1194static int dwc3_gadget_wakeup(struct usb_gadget *g)
1195{
1196 struct dwc3 *dwc = gadget_to_dwc(g);
1197
1198 unsigned long timeout;
1199 unsigned long flags;
1200
1201 u32 reg;
1202
1203 int ret = 0;
1204
1205 u8 link_state;
1206 u8 speed;
1207
1208 spin_lock_irqsave(&dwc->lock, flags);
1209
1210 /*
1211 * According to the Databook Remote wakeup request should
1212 * be issued only when the device is in early suspend state.
1213 *
1214 * We can check that via USB Link State bits in DSTS register.
1215 */
1216 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1217
1218 speed = reg & DWC3_DSTS_CONNECTSPD;
1219 if (speed == DWC3_DSTS_SUPERSPEED) {
1220 dev_dbg(dwc->dev, "no wakeup on SuperSpeed\n");
1221 ret = -EINVAL;
1222 goto out;
1223 }
1224
1225 link_state = DWC3_DSTS_USBLNKST(reg);
1226
1227 switch (link_state) {
1228 case DWC3_LINK_STATE_RX_DET: /* in HS, means Early Suspend */
1229 case DWC3_LINK_STATE_U3: /* in HS, means SUSPEND */
1230 break;
1231 default:
1232 dev_dbg(dwc->dev, "can't wakeup from link state %d\n",
1233 link_state);
1234 ret = -EINVAL;
1235 goto out;
1236 }
1237
Felipe Balbi8598bde2012-01-02 18:55:57 +02001238 ret = dwc3_gadget_set_link_state(dwc, DWC3_LINK_STATE_RECOV);
1239 if (ret < 0) {
1240 dev_err(dwc->dev, "failed to put link in Recovery\n");
1241 goto out;
1242 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001243
1244 /* write zeroes to Link Change Request */
1245 reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK;
1246 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1247
Paul Zimmerman1d046792012-02-15 18:56:56 -08001248 /* poll until Link State changes to ON */
Felipe Balbi72246da2011-08-19 18:10:58 +03001249 timeout = jiffies + msecs_to_jiffies(100);
1250
Paul Zimmerman1d046792012-02-15 18:56:56 -08001251 while (!time_after(jiffies, timeout)) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001252 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1253
1254 /* in HS, means ON */
1255 if (DWC3_DSTS_USBLNKST(reg) == DWC3_LINK_STATE_U0)
1256 break;
1257 }
1258
1259 if (DWC3_DSTS_USBLNKST(reg) != DWC3_LINK_STATE_U0) {
1260 dev_err(dwc->dev, "failed to send remote wakeup\n");
1261 ret = -EINVAL;
1262 }
1263
1264out:
1265 spin_unlock_irqrestore(&dwc->lock, flags);
1266
1267 return ret;
1268}
1269
1270static int dwc3_gadget_set_selfpowered(struct usb_gadget *g,
1271 int is_selfpowered)
1272{
1273 struct dwc3 *dwc = gadget_to_dwc(g);
Paul Zimmerman249a4562012-02-24 17:32:16 -08001274 unsigned long flags;
Felipe Balbi72246da2011-08-19 18:10:58 +03001275
Paul Zimmerman249a4562012-02-24 17:32:16 -08001276 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001277 dwc->is_selfpowered = !!is_selfpowered;
Paul Zimmerman249a4562012-02-24 17:32:16 -08001278 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001279
1280 return 0;
1281}
1282
1283static void dwc3_gadget_run_stop(struct dwc3 *dwc, int is_on)
1284{
1285 u32 reg;
Sebastian Andrzej Siewior61d58242011-08-29 16:46:38 +02001286 u32 timeout = 500;
Felipe Balbi72246da2011-08-19 18:10:58 +03001287
1288 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
Felipe Balbi8db7ed12012-01-18 18:32:29 +02001289 if (is_on) {
1290 reg &= ~DWC3_DCTL_TRGTULST_MASK;
1291 reg |= (DWC3_DCTL_RUN_STOP
1292 | DWC3_DCTL_TRGTULST_RX_DET);
1293 } else {
Felipe Balbi72246da2011-08-19 18:10:58 +03001294 reg &= ~DWC3_DCTL_RUN_STOP;
Felipe Balbi8db7ed12012-01-18 18:32:29 +02001295 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001296
1297 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1298
1299 do {
1300 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1301 if (is_on) {
1302 if (!(reg & DWC3_DSTS_DEVCTRLHLT))
1303 break;
1304 } else {
1305 if (reg & DWC3_DSTS_DEVCTRLHLT)
1306 break;
1307 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001308 timeout--;
1309 if (!timeout)
1310 break;
Sebastian Andrzej Siewior61d58242011-08-29 16:46:38 +02001311 udelay(1);
Felipe Balbi72246da2011-08-19 18:10:58 +03001312 } while (1);
1313
1314 dev_vdbg(dwc->dev, "gadget %s data soft-%s\n",
1315 dwc->gadget_driver
1316 ? dwc->gadget_driver->function : "no-function",
1317 is_on ? "connect" : "disconnect");
1318}
1319
1320static int dwc3_gadget_pullup(struct usb_gadget *g, int is_on)
1321{
1322 struct dwc3 *dwc = gadget_to_dwc(g);
1323 unsigned long flags;
1324
1325 is_on = !!is_on;
1326
1327 spin_lock_irqsave(&dwc->lock, flags);
1328 dwc3_gadget_run_stop(dwc, is_on);
1329 spin_unlock_irqrestore(&dwc->lock, flags);
1330
1331 return 0;
1332}
1333
1334static int dwc3_gadget_start(struct usb_gadget *g,
1335 struct usb_gadget_driver *driver)
1336{
1337 struct dwc3 *dwc = gadget_to_dwc(g);
1338 struct dwc3_ep *dep;
1339 unsigned long flags;
1340 int ret = 0;
1341 u32 reg;
1342
1343 spin_lock_irqsave(&dwc->lock, flags);
1344
1345 if (dwc->gadget_driver) {
1346 dev_err(dwc->dev, "%s is already bound to %s\n",
1347 dwc->gadget.name,
1348 dwc->gadget_driver->driver.name);
1349 ret = -EBUSY;
1350 goto err0;
1351 }
1352
1353 dwc->gadget_driver = driver;
1354 dwc->gadget.dev.driver = &driver->driver;
1355
Felipe Balbi72246da2011-08-19 18:10:58 +03001356 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
1357 reg &= ~(DWC3_DCFG_SPEED_MASK);
Felipe Balbi6c167fc2011-10-07 22:55:04 +03001358 reg |= dwc->maximum_speed;
Felipe Balbi72246da2011-08-19 18:10:58 +03001359 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
1360
Paul Zimmermanb23c8432011-09-30 10:58:42 +03001361 dwc->start_config_issued = false;
1362
Felipe Balbi72246da2011-08-19 18:10:58 +03001363 /* Start with SuperSpeed Default */
1364 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
1365
1366 dep = dwc->eps[0];
Felipe Balbic90bfae2011-11-29 13:11:21 +02001367 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL);
Felipe Balbi72246da2011-08-19 18:10:58 +03001368 if (ret) {
1369 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
1370 goto err0;
1371 }
1372
1373 dep = dwc->eps[1];
Felipe Balbic90bfae2011-11-29 13:11:21 +02001374 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL);
Felipe Balbi72246da2011-08-19 18:10:58 +03001375 if (ret) {
1376 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
1377 goto err1;
1378 }
1379
1380 /* begin to receive SETUP packets */
Felipe Balbic7fcdeb2011-08-27 22:28:36 +03001381 dwc->ep0state = EP0_SETUP_PHASE;
Felipe Balbi72246da2011-08-19 18:10:58 +03001382 dwc3_ep0_out_start(dwc);
1383
1384 spin_unlock_irqrestore(&dwc->lock, flags);
1385
1386 return 0;
1387
1388err1:
1389 __dwc3_gadget_ep_disable(dwc->eps[0]);
1390
1391err0:
1392 spin_unlock_irqrestore(&dwc->lock, flags);
1393
1394 return ret;
1395}
1396
1397static int dwc3_gadget_stop(struct usb_gadget *g,
1398 struct usb_gadget_driver *driver)
1399{
1400 struct dwc3 *dwc = gadget_to_dwc(g);
1401 unsigned long flags;
1402
1403 spin_lock_irqsave(&dwc->lock, flags);
1404
1405 __dwc3_gadget_ep_disable(dwc->eps[0]);
1406 __dwc3_gadget_ep_disable(dwc->eps[1]);
1407
1408 dwc->gadget_driver = NULL;
1409 dwc->gadget.dev.driver = NULL;
1410
1411 spin_unlock_irqrestore(&dwc->lock, flags);
1412
1413 return 0;
1414}
1415static const struct usb_gadget_ops dwc3_gadget_ops = {
1416 .get_frame = dwc3_gadget_get_frame,
1417 .wakeup = dwc3_gadget_wakeup,
1418 .set_selfpowered = dwc3_gadget_set_selfpowered,
1419 .pullup = dwc3_gadget_pullup,
1420 .udc_start = dwc3_gadget_start,
1421 .udc_stop = dwc3_gadget_stop,
1422};
1423
1424/* -------------------------------------------------------------------------- */
1425
1426static int __devinit dwc3_gadget_init_endpoints(struct dwc3 *dwc)
1427{
1428 struct dwc3_ep *dep;
1429 u8 epnum;
1430
1431 INIT_LIST_HEAD(&dwc->gadget.ep_list);
1432
1433 for (epnum = 0; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
1434 dep = kzalloc(sizeof(*dep), GFP_KERNEL);
1435 if (!dep) {
1436 dev_err(dwc->dev, "can't allocate endpoint %d\n",
1437 epnum);
1438 return -ENOMEM;
1439 }
1440
1441 dep->dwc = dwc;
1442 dep->number = epnum;
1443 dwc->eps[epnum] = dep;
1444
1445 snprintf(dep->name, sizeof(dep->name), "ep%d%s", epnum >> 1,
1446 (epnum & 1) ? "in" : "out");
1447 dep->endpoint.name = dep->name;
1448 dep->direction = (epnum & 1);
1449
1450 if (epnum == 0 || epnum == 1) {
1451 dep->endpoint.maxpacket = 512;
1452 dep->endpoint.ops = &dwc3_gadget_ep0_ops;
1453 if (!epnum)
1454 dwc->gadget.ep0 = &dep->endpoint;
1455 } else {
1456 int ret;
1457
1458 dep->endpoint.maxpacket = 1024;
Sebastian Andrzej Siewior12d36c12011-11-03 20:27:50 +01001459 dep->endpoint.max_streams = 15;
Felipe Balbi72246da2011-08-19 18:10:58 +03001460 dep->endpoint.ops = &dwc3_gadget_ep_ops;
1461 list_add_tail(&dep->endpoint.ep_list,
1462 &dwc->gadget.ep_list);
1463
1464 ret = dwc3_alloc_trb_pool(dep);
Felipe Balbi25b8ff62011-11-04 12:32:47 +02001465 if (ret)
Felipe Balbi72246da2011-08-19 18:10:58 +03001466 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001467 }
Felipe Balbi25b8ff62011-11-04 12:32:47 +02001468
Felipe Balbi72246da2011-08-19 18:10:58 +03001469 INIT_LIST_HEAD(&dep->request_list);
1470 INIT_LIST_HEAD(&dep->req_queued);
1471 }
1472
1473 return 0;
1474}
1475
1476static void dwc3_gadget_free_endpoints(struct dwc3 *dwc)
1477{
1478 struct dwc3_ep *dep;
1479 u8 epnum;
1480
1481 for (epnum = 0; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
1482 dep = dwc->eps[epnum];
1483 dwc3_free_trb_pool(dep);
1484
1485 if (epnum != 0 && epnum != 1)
1486 list_del(&dep->endpoint.ep_list);
1487
1488 kfree(dep);
1489 }
1490}
1491
1492static void dwc3_gadget_release(struct device *dev)
1493{
1494 dev_dbg(dev, "%s\n", __func__);
1495}
1496
1497/* -------------------------------------------------------------------------- */
1498static int dwc3_cleanup_done_reqs(struct dwc3 *dwc, struct dwc3_ep *dep,
1499 const struct dwc3_event_depevt *event, int status)
1500{
1501 struct dwc3_request *req;
Felipe Balbif6bafc62012-02-06 11:04:53 +02001502 struct dwc3_trb *trb;
Felipe Balbi72246da2011-08-19 18:10:58 +03001503 unsigned int count;
1504 unsigned int s_pkt = 0;
1505
1506 do {
1507 req = next_request(&dep->req_queued);
Sebastian Andrzej Siewiord39ee7b2011-11-03 10:32:20 +01001508 if (!req) {
1509 WARN_ON_ONCE(1);
1510 return 1;
1511 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001512
Felipe Balbif6bafc62012-02-06 11:04:53 +02001513 trb = req->trb;
Felipe Balbi72246da2011-08-19 18:10:58 +03001514
Felipe Balbif6bafc62012-02-06 11:04:53 +02001515 if ((trb->ctrl & DWC3_TRB_CTRL_HWO) && status != -ESHUTDOWN)
Sebastian Andrzej Siewior0d2f4752011-08-19 19:59:12 +02001516 /*
1517 * We continue despite the error. There is not much we
Paul Zimmerman1d046792012-02-15 18:56:56 -08001518 * can do. If we don't clean it up we loop forever. If
1519 * we skip the TRB then it gets overwritten after a
1520 * while since we use them in a ring buffer. A BUG()
1521 * would help. Lets hope that if this occurs, someone
Sebastian Andrzej Siewior0d2f4752011-08-19 19:59:12 +02001522 * fixes the root cause instead of looking away :)
1523 */
Felipe Balbi72246da2011-08-19 18:10:58 +03001524 dev_err(dwc->dev, "%s's TRB (%p) still owned by HW\n",
1525 dep->name, req->trb);
Felipe Balbif6bafc62012-02-06 11:04:53 +02001526 count = trb->size & DWC3_TRB_SIZE_MASK;
Felipe Balbi72246da2011-08-19 18:10:58 +03001527
1528 if (dep->direction) {
1529 if (count) {
1530 dev_err(dwc->dev, "incomplete IN transfer %s\n",
1531 dep->name);
1532 status = -ECONNRESET;
1533 }
1534 } else {
1535 if (count && (event->status & DEPEVT_STATUS_SHORT))
1536 s_pkt = 1;
1537 }
1538
1539 /*
1540 * We assume here we will always receive the entire data block
1541 * which we should receive. Meaning, if we program RX to
1542 * receive 4K but we receive only 2K, we assume that's all we
1543 * should receive and we simply bounce the request back to the
1544 * gadget driver for further processing.
1545 */
1546 req->request.actual += req->request.length - count;
1547 dwc3_gadget_giveback(dep, req, status);
1548 if (s_pkt)
1549 break;
Felipe Balbif6bafc62012-02-06 11:04:53 +02001550 if ((event->status & DEPEVT_STATUS_LST) &&
1551 (trb->ctrl & DWC3_TRB_CTRL_LST))
Felipe Balbi72246da2011-08-19 18:10:58 +03001552 break;
Felipe Balbif6bafc62012-02-06 11:04:53 +02001553 if ((event->status & DEPEVT_STATUS_IOC) &&
1554 (trb->ctrl & DWC3_TRB_CTRL_IOC))
Felipe Balbi72246da2011-08-19 18:10:58 +03001555 break;
1556 } while (1);
1557
Felipe Balbif6bafc62012-02-06 11:04:53 +02001558 if ((event->status & DEPEVT_STATUS_IOC) &&
1559 (trb->ctrl & DWC3_TRB_CTRL_IOC))
Felipe Balbi72246da2011-08-19 18:10:58 +03001560 return 0;
1561 return 1;
1562}
1563
1564static void dwc3_endpoint_transfer_complete(struct dwc3 *dwc,
1565 struct dwc3_ep *dep, const struct dwc3_event_depevt *event,
1566 int start_new)
1567{
1568 unsigned status = 0;
1569 int clean_busy;
1570
1571 if (event->status & DEPEVT_STATUS_BUSERR)
1572 status = -ECONNRESET;
1573
Paul Zimmerman1d046792012-02-15 18:56:56 -08001574 clean_busy = dwc3_cleanup_done_reqs(dwc, dep, event, status);
Paul Zimmermanc2df85c2012-02-24 17:32:18 -08001575 if (clean_busy)
Felipe Balbi72246da2011-08-19 18:10:58 +03001576 dep->flags &= ~DWC3_EP_BUSY;
Felipe Balbifae2b902011-10-14 13:00:30 +03001577
1578 /*
1579 * WORKAROUND: This is the 2nd half of U1/U2 -> U0 workaround.
1580 * See dwc3_gadget_linksts_change_interrupt() for 1st half.
1581 */
1582 if (dwc->revision < DWC3_REVISION_183A) {
1583 u32 reg;
1584 int i;
1585
1586 for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) {
1587 struct dwc3_ep *dep = dwc->eps[i];
1588
1589 if (!(dep->flags & DWC3_EP_ENABLED))
1590 continue;
1591
1592 if (!list_empty(&dep->req_queued))
1593 return;
1594 }
1595
1596 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
1597 reg |= dwc->u1u2;
1598 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1599
1600 dwc->u1u2 = 0;
1601 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001602}
1603
1604static void dwc3_gadget_start_isoc(struct dwc3 *dwc,
1605 struct dwc3_ep *dep, const struct dwc3_event_depevt *event)
1606{
Paul Zimmerman9bafa562012-02-17 14:10:16 -08001607 u32 uf, mask;
Felipe Balbi72246da2011-08-19 18:10:58 +03001608
1609 if (list_empty(&dep->request_list)) {
1610 dev_vdbg(dwc->dev, "ISOC ep %s run out for requests.\n",
1611 dep->name);
1612 return;
1613 }
1614
Paul Zimmerman9bafa562012-02-17 14:10:16 -08001615 mask = ~(dep->interval - 1);
1616 uf = event->parameters & mask;
1617 /* 4 micro frames in the future */
1618 uf += dep->interval * 4;
Felipe Balbi72246da2011-08-19 18:10:58 +03001619
1620 __dwc3_gadget_kick_transfer(dep, uf, 1);
1621}
1622
1623static void dwc3_process_ep_cmd_complete(struct dwc3_ep *dep,
1624 const struct dwc3_event_depevt *event)
1625{
1626 struct dwc3 *dwc = dep->dwc;
1627 struct dwc3_event_depevt mod_ev = *event;
1628
1629 /*
Paul Zimmerman1d046792012-02-15 18:56:56 -08001630 * We were asked to remove one request. It is possible that this
1631 * request and a few others were started together and have the same
Felipe Balbi72246da2011-08-19 18:10:58 +03001632 * transfer index. Since we stopped the complete endpoint we don't
1633 * know how many requests were already completed (and not yet)
1634 * reported and how could be done (later). We purge them all until
1635 * the end of the list.
1636 */
1637 mod_ev.status = DEPEVT_STATUS_LST;
1638 dwc3_cleanup_done_reqs(dwc, dep, &mod_ev, -ESHUTDOWN);
1639 dep->flags &= ~DWC3_EP_BUSY;
Paul Zimmerman1d046792012-02-15 18:56:56 -08001640 /* pending requests are ignored and are queued on XferNotReady */
Felipe Balbi72246da2011-08-19 18:10:58 +03001641}
1642
1643static void dwc3_ep_cmd_compl(struct dwc3_ep *dep,
1644 const struct dwc3_event_depevt *event)
1645{
1646 u32 param = event->parameters;
1647 u32 cmd_type = (param >> 8) & ((1 << 5) - 1);
1648
1649 switch (cmd_type) {
1650 case DWC3_DEPCMD_ENDTRANSFER:
1651 dwc3_process_ep_cmd_complete(dep, event);
1652 break;
1653 case DWC3_DEPCMD_STARTTRANSFER:
1654 dep->res_trans_idx = param & 0x7f;
1655 break;
1656 default:
1657 printk(KERN_ERR "%s() unknown /unexpected type: %d\n",
1658 __func__, cmd_type);
1659 break;
1660 };
1661}
1662
1663static void dwc3_endpoint_interrupt(struct dwc3 *dwc,
1664 const struct dwc3_event_depevt *event)
1665{
1666 struct dwc3_ep *dep;
1667 u8 epnum = event->endpoint_number;
1668
1669 dep = dwc->eps[epnum];
1670
1671 dev_vdbg(dwc->dev, "%s: %s\n", dep->name,
1672 dwc3_ep_event_string(event->endpoint_event));
1673
1674 if (epnum == 0 || epnum == 1) {
1675 dwc3_ep0_interrupt(dwc, event);
1676 return;
1677 }
1678
1679 switch (event->endpoint_event) {
1680 case DWC3_DEPEVT_XFERCOMPLETE:
Paul Zimmermanc2df85c2012-02-24 17:32:18 -08001681 dep->res_trans_idx = 0;
1682
Ido Shayevitz16e78db2012-03-12 20:25:24 +02001683 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001684 dev_dbg(dwc->dev, "%s is an Isochronous endpoint\n",
1685 dep->name);
1686 return;
1687 }
1688
1689 dwc3_endpoint_transfer_complete(dwc, dep, event, 1);
1690 break;
1691 case DWC3_DEPEVT_XFERINPROGRESS:
Ido Shayevitz16e78db2012-03-12 20:25:24 +02001692 if (!usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001693 dev_dbg(dwc->dev, "%s is not an Isochronous endpoint\n",
1694 dep->name);
1695 return;
1696 }
1697
1698 dwc3_endpoint_transfer_complete(dwc, dep, event, 0);
1699 break;
1700 case DWC3_DEPEVT_XFERNOTREADY:
Ido Shayevitz16e78db2012-03-12 20:25:24 +02001701 if (usb_endpoint_xfer_isoc(dep->endpoint.desc)) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001702 dwc3_gadget_start_isoc(dwc, dep, event);
1703 } else {
1704 int ret;
1705
1706 dev_vdbg(dwc->dev, "%s: reason %s\n",
Felipe Balbi40aa41f2012-01-18 17:06:03 +02001707 dep->name, event->status &
1708 DEPEVT_STATUS_TRANSFER_ACTIVE
Felipe Balbi72246da2011-08-19 18:10:58 +03001709 ? "Transfer Active"
1710 : "Transfer Not Active");
1711
1712 ret = __dwc3_gadget_kick_transfer(dep, 0, 1);
1713 if (!ret || ret == -EBUSY)
1714 return;
1715
1716 dev_dbg(dwc->dev, "%s: failed to kick transfers\n",
1717 dep->name);
1718 }
1719
1720 break;
Felipe Balbi879631a2011-09-30 10:58:47 +03001721 case DWC3_DEPEVT_STREAMEVT:
Ido Shayevitz16e78db2012-03-12 20:25:24 +02001722 if (!usb_endpoint_xfer_bulk(dep->endpoint.desc)) {
Felipe Balbi879631a2011-09-30 10:58:47 +03001723 dev_err(dwc->dev, "Stream event for non-Bulk %s\n",
1724 dep->name);
1725 return;
1726 }
1727
1728 switch (event->status) {
1729 case DEPEVT_STREAMEVT_FOUND:
1730 dev_vdbg(dwc->dev, "Stream %d found and started\n",
1731 event->parameters);
1732
1733 break;
1734 case DEPEVT_STREAMEVT_NOTFOUND:
1735 /* FALLTHROUGH */
1736 default:
1737 dev_dbg(dwc->dev, "Couldn't find suitable stream\n");
1738 }
1739 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03001740 case DWC3_DEPEVT_RXTXFIFOEVT:
1741 dev_dbg(dwc->dev, "%s FIFO Overrun\n", dep->name);
1742 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03001743 case DWC3_DEPEVT_EPCMDCMPLT:
1744 dwc3_ep_cmd_compl(dep, event);
1745 break;
1746 }
1747}
1748
1749static void dwc3_disconnect_gadget(struct dwc3 *dwc)
1750{
1751 if (dwc->gadget_driver && dwc->gadget_driver->disconnect) {
1752 spin_unlock(&dwc->lock);
1753 dwc->gadget_driver->disconnect(&dwc->gadget);
1754 spin_lock(&dwc->lock);
1755 }
1756}
1757
1758static void dwc3_stop_active_transfer(struct dwc3 *dwc, u32 epnum)
1759{
1760 struct dwc3_ep *dep;
1761 struct dwc3_gadget_ep_cmd_params params;
1762 u32 cmd;
1763 int ret;
1764
1765 dep = dwc->eps[epnum];
1766
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +02001767 WARN_ON(!dep->res_trans_idx);
Felipe Balbi72246da2011-08-19 18:10:58 +03001768 if (dep->res_trans_idx) {
1769 cmd = DWC3_DEPCMD_ENDTRANSFER;
1770 cmd |= DWC3_DEPCMD_HIPRI_FORCERM | DWC3_DEPCMD_CMDIOC;
1771 cmd |= DWC3_DEPCMD_PARAM(dep->res_trans_idx);
1772 memset(&params, 0, sizeof(params));
1773 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number, cmd, &params);
1774 WARN_ON_ONCE(ret);
Sebastian Andrzej Siewiora1ae9be2011-08-22 17:42:18 +02001775 dep->res_trans_idx = 0;
Felipe Balbi72246da2011-08-19 18:10:58 +03001776 }
1777}
1778
1779static void dwc3_stop_active_transfers(struct dwc3 *dwc)
1780{
1781 u32 epnum;
1782
1783 for (epnum = 2; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
1784 struct dwc3_ep *dep;
1785
1786 dep = dwc->eps[epnum];
1787 if (!(dep->flags & DWC3_EP_ENABLED))
1788 continue;
1789
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +02001790 dwc3_remove_requests(dwc, dep);
Felipe Balbi72246da2011-08-19 18:10:58 +03001791 }
1792}
1793
1794static void dwc3_clear_stall_all_ep(struct dwc3 *dwc)
1795{
1796 u32 epnum;
1797
1798 for (epnum = 1; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
1799 struct dwc3_ep *dep;
1800 struct dwc3_gadget_ep_cmd_params params;
1801 int ret;
1802
1803 dep = dwc->eps[epnum];
1804
1805 if (!(dep->flags & DWC3_EP_STALL))
1806 continue;
1807
1808 dep->flags &= ~DWC3_EP_STALL;
1809
1810 memset(&params, 0, sizeof(params));
1811 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number,
1812 DWC3_DEPCMD_CLEARSTALL, &params);
1813 WARN_ON_ONCE(ret);
1814 }
1815}
1816
1817static void dwc3_gadget_disconnect_interrupt(struct dwc3 *dwc)
1818{
1819 dev_vdbg(dwc->dev, "%s\n", __func__);
1820#if 0
1821 XXX
1822 U1/U2 is powersave optimization. Skip it for now. Anyway we need to
1823 enable it before we can disable it.
1824
1825 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
1826 reg &= ~DWC3_DCTL_INITU1ENA;
1827 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1828
1829 reg &= ~DWC3_DCTL_INITU2ENA;
1830 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1831#endif
1832
1833 dwc3_stop_active_transfers(dwc);
1834 dwc3_disconnect_gadget(dwc);
Paul Zimmermanb23c8432011-09-30 10:58:42 +03001835 dwc->start_config_issued = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03001836
1837 dwc->gadget.speed = USB_SPEED_UNKNOWN;
Felipe Balbidf62df52011-10-14 15:11:49 +03001838 dwc->setup_packet_pending = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03001839}
1840
1841static void dwc3_gadget_usb3_phy_power(struct dwc3 *dwc, int on)
1842{
1843 u32 reg;
1844
1845 reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0));
1846
1847 if (on)
1848 reg &= ~DWC3_GUSB3PIPECTL_SUSPHY;
1849 else
1850 reg |= DWC3_GUSB3PIPECTL_SUSPHY;
1851
1852 dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg);
1853}
1854
1855static void dwc3_gadget_usb2_phy_power(struct dwc3 *dwc, int on)
1856{
1857 u32 reg;
1858
1859 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
1860
1861 if (on)
1862 reg &= ~DWC3_GUSB2PHYCFG_SUSPHY;
1863 else
1864 reg |= DWC3_GUSB2PHYCFG_SUSPHY;
1865
1866 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
1867}
1868
1869static void dwc3_gadget_reset_interrupt(struct dwc3 *dwc)
1870{
1871 u32 reg;
1872
1873 dev_vdbg(dwc->dev, "%s\n", __func__);
1874
Felipe Balbidf62df52011-10-14 15:11:49 +03001875 /*
1876 * WORKAROUND: DWC3 revisions <1.88a have an issue which
1877 * would cause a missing Disconnect Event if there's a
1878 * pending Setup Packet in the FIFO.
1879 *
1880 * There's no suggested workaround on the official Bug
1881 * report, which states that "unless the driver/application
1882 * is doing any special handling of a disconnect event,
1883 * there is no functional issue".
1884 *
1885 * Unfortunately, it turns out that we _do_ some special
1886 * handling of a disconnect event, namely complete all
1887 * pending transfers, notify gadget driver of the
1888 * disconnection, and so on.
1889 *
1890 * Our suggested workaround is to follow the Disconnect
1891 * Event steps here, instead, based on a setup_packet_pending
1892 * flag. Such flag gets set whenever we have a XferNotReady
1893 * event on EP0 and gets cleared on XferComplete for the
1894 * same endpoint.
1895 *
1896 * Refers to:
1897 *
1898 * STAR#9000466709: RTL: Device : Disconnect event not
1899 * generated if setup packet pending in FIFO
1900 */
1901 if (dwc->revision < DWC3_REVISION_188A) {
1902 if (dwc->setup_packet_pending)
1903 dwc3_gadget_disconnect_interrupt(dwc);
1904 }
1905
Felipe Balbi961906e2011-12-20 15:37:21 +02001906 /* after reset -> Default State */
1907 dwc->dev_state = DWC3_DEFAULT_STATE;
1908
Felipe Balbi72246da2011-08-19 18:10:58 +03001909 /* Enable PHYs */
1910 dwc3_gadget_usb2_phy_power(dwc, true);
1911 dwc3_gadget_usb3_phy_power(dwc, true);
1912
1913 if (dwc->gadget.speed != USB_SPEED_UNKNOWN)
1914 dwc3_disconnect_gadget(dwc);
1915
1916 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
1917 reg &= ~DWC3_DCTL_TSTCTRL_MASK;
1918 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Gerard Cauvy3b637362012-02-10 12:21:18 +02001919 dwc->test_mode = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03001920
1921 dwc3_stop_active_transfers(dwc);
1922 dwc3_clear_stall_all_ep(dwc);
Paul Zimmermanb23c8432011-09-30 10:58:42 +03001923 dwc->start_config_issued = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03001924
1925 /* Reset device address to zero */
1926 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
1927 reg &= ~(DWC3_DCFG_DEVADDR_MASK);
1928 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +03001929}
1930
1931static void dwc3_update_ram_clk_sel(struct dwc3 *dwc, u32 speed)
1932{
1933 u32 reg;
1934 u32 usb30_clock = DWC3_GCTL_CLK_BUS;
1935
1936 /*
1937 * We change the clock only at SS but I dunno why I would want to do
1938 * this. Maybe it becomes part of the power saving plan.
1939 */
1940
1941 if (speed != DWC3_DSTS_SUPERSPEED)
1942 return;
1943
1944 /*
1945 * RAMClkSel is reset to 0 after USB reset, so it must be reprogrammed
1946 * each time on Connect Done.
1947 */
1948 if (!usb30_clock)
1949 return;
1950
1951 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
1952 reg |= DWC3_GCTL_RAMCLKSEL(usb30_clock);
1953 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
1954}
1955
1956static void dwc3_gadget_disable_phy(struct dwc3 *dwc, u8 speed)
1957{
1958 switch (speed) {
1959 case USB_SPEED_SUPER:
1960 dwc3_gadget_usb2_phy_power(dwc, false);
1961 break;
1962 case USB_SPEED_HIGH:
1963 case USB_SPEED_FULL:
1964 case USB_SPEED_LOW:
1965 dwc3_gadget_usb3_phy_power(dwc, false);
1966 break;
1967 }
1968}
1969
1970static void dwc3_gadget_conndone_interrupt(struct dwc3 *dwc)
1971{
1972 struct dwc3_gadget_ep_cmd_params params;
1973 struct dwc3_ep *dep;
1974 int ret;
1975 u32 reg;
1976 u8 speed;
1977
1978 dev_vdbg(dwc->dev, "%s\n", __func__);
1979
1980 memset(&params, 0x00, sizeof(params));
1981
Felipe Balbi72246da2011-08-19 18:10:58 +03001982 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1983 speed = reg & DWC3_DSTS_CONNECTSPD;
1984 dwc->speed = speed;
1985
1986 dwc3_update_ram_clk_sel(dwc, speed);
1987
1988 switch (speed) {
1989 case DWC3_DCFG_SUPERSPEED:
Felipe Balbi05870c5b2011-10-14 14:51:38 +03001990 /*
1991 * WORKAROUND: DWC3 revisions <1.90a have an issue which
1992 * would cause a missing USB3 Reset event.
1993 *
1994 * In such situations, we should force a USB3 Reset
1995 * event by calling our dwc3_gadget_reset_interrupt()
1996 * routine.
1997 *
1998 * Refers to:
1999 *
2000 * STAR#9000483510: RTL: SS : USB3 reset event may
2001 * not be generated always when the link enters poll
2002 */
2003 if (dwc->revision < DWC3_REVISION_190A)
2004 dwc3_gadget_reset_interrupt(dwc);
2005
Felipe Balbi72246da2011-08-19 18:10:58 +03002006 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
2007 dwc->gadget.ep0->maxpacket = 512;
2008 dwc->gadget.speed = USB_SPEED_SUPER;
2009 break;
2010 case DWC3_DCFG_HIGHSPEED:
2011 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
2012 dwc->gadget.ep0->maxpacket = 64;
2013 dwc->gadget.speed = USB_SPEED_HIGH;
2014 break;
2015 case DWC3_DCFG_FULLSPEED2:
2016 case DWC3_DCFG_FULLSPEED1:
2017 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
2018 dwc->gadget.ep0->maxpacket = 64;
2019 dwc->gadget.speed = USB_SPEED_FULL;
2020 break;
2021 case DWC3_DCFG_LOWSPEED:
2022 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(8);
2023 dwc->gadget.ep0->maxpacket = 8;
2024 dwc->gadget.speed = USB_SPEED_LOW;
2025 break;
2026 }
2027
2028 /* Disable unneded PHY */
2029 dwc3_gadget_disable_phy(dwc, dwc->gadget.speed);
2030
2031 dep = dwc->eps[0];
Felipe Balbic90bfae2011-11-29 13:11:21 +02002032 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL);
Felipe Balbi72246da2011-08-19 18:10:58 +03002033 if (ret) {
2034 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
2035 return;
2036 }
2037
2038 dep = dwc->eps[1];
Felipe Balbic90bfae2011-11-29 13:11:21 +02002039 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL);
Felipe Balbi72246da2011-08-19 18:10:58 +03002040 if (ret) {
2041 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
2042 return;
2043 }
2044
2045 /*
2046 * Configure PHY via GUSB3PIPECTLn if required.
2047 *
2048 * Update GTXFIFOSIZn
2049 *
2050 * In both cases reset values should be sufficient.
2051 */
2052}
2053
2054static void dwc3_gadget_wakeup_interrupt(struct dwc3 *dwc)
2055{
2056 dev_vdbg(dwc->dev, "%s\n", __func__);
2057
2058 /*
2059 * TODO take core out of low power mode when that's
2060 * implemented.
2061 */
2062
2063 dwc->gadget_driver->resume(&dwc->gadget);
2064}
2065
2066static void dwc3_gadget_linksts_change_interrupt(struct dwc3 *dwc,
2067 unsigned int evtinfo)
2068{
Felipe Balbifae2b902011-10-14 13:00:30 +03002069 enum dwc3_link_state next = evtinfo & DWC3_LINK_STATE_MASK;
2070
2071 /*
2072 * WORKAROUND: DWC3 Revisions <1.83a have an issue which, depending
2073 * on the link partner, the USB session might do multiple entry/exit
2074 * of low power states before a transfer takes place.
2075 *
2076 * Due to this problem, we might experience lower throughput. The
2077 * suggested workaround is to disable DCTL[12:9] bits if we're
2078 * transitioning from U1/U2 to U0 and enable those bits again
2079 * after a transfer completes and there are no pending transfers
2080 * on any of the enabled endpoints.
2081 *
2082 * This is the first half of that workaround.
2083 *
2084 * Refers to:
2085 *
2086 * STAR#9000446952: RTL: Device SS : if U1/U2 ->U0 takes >128us
2087 * core send LGO_Ux entering U0
2088 */
2089 if (dwc->revision < DWC3_REVISION_183A) {
2090 if (next == DWC3_LINK_STATE_U0) {
2091 u32 u1u2;
2092 u32 reg;
2093
2094 switch (dwc->link_state) {
2095 case DWC3_LINK_STATE_U1:
2096 case DWC3_LINK_STATE_U2:
2097 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2098 u1u2 = reg & (DWC3_DCTL_INITU2ENA
2099 | DWC3_DCTL_ACCEPTU2ENA
2100 | DWC3_DCTL_INITU1ENA
2101 | DWC3_DCTL_ACCEPTU1ENA);
2102
2103 if (!dwc->u1u2)
2104 dwc->u1u2 = reg & u1u2;
2105
2106 reg &= ~u1u2;
2107
2108 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2109 break;
2110 default:
2111 /* do nothing */
2112 break;
2113 }
2114 }
2115 }
2116
2117 dwc->link_state = next;
Felipe Balbi019ac832011-09-08 21:18:47 +03002118
2119 dev_vdbg(dwc->dev, "%s link %d\n", __func__, dwc->link_state);
Felipe Balbi72246da2011-08-19 18:10:58 +03002120}
2121
2122static void dwc3_gadget_interrupt(struct dwc3 *dwc,
2123 const struct dwc3_event_devt *event)
2124{
2125 switch (event->type) {
2126 case DWC3_DEVICE_EVENT_DISCONNECT:
2127 dwc3_gadget_disconnect_interrupt(dwc);
2128 break;
2129 case DWC3_DEVICE_EVENT_RESET:
2130 dwc3_gadget_reset_interrupt(dwc);
2131 break;
2132 case DWC3_DEVICE_EVENT_CONNECT_DONE:
2133 dwc3_gadget_conndone_interrupt(dwc);
2134 break;
2135 case DWC3_DEVICE_EVENT_WAKEUP:
2136 dwc3_gadget_wakeup_interrupt(dwc);
2137 break;
2138 case DWC3_DEVICE_EVENT_LINK_STATUS_CHANGE:
2139 dwc3_gadget_linksts_change_interrupt(dwc, event->event_info);
2140 break;
2141 case DWC3_DEVICE_EVENT_EOPF:
2142 dev_vdbg(dwc->dev, "End of Periodic Frame\n");
2143 break;
2144 case DWC3_DEVICE_EVENT_SOF:
2145 dev_vdbg(dwc->dev, "Start of Periodic Frame\n");
2146 break;
2147 case DWC3_DEVICE_EVENT_ERRATIC_ERROR:
2148 dev_vdbg(dwc->dev, "Erratic Error\n");
2149 break;
2150 case DWC3_DEVICE_EVENT_CMD_CMPL:
2151 dev_vdbg(dwc->dev, "Command Complete\n");
2152 break;
2153 case DWC3_DEVICE_EVENT_OVERFLOW:
2154 dev_vdbg(dwc->dev, "Overflow\n");
2155 break;
2156 default:
2157 dev_dbg(dwc->dev, "UNKNOWN IRQ %d\n", event->type);
2158 }
2159}
2160
2161static void dwc3_process_event_entry(struct dwc3 *dwc,
2162 const union dwc3_event *event)
2163{
2164 /* Endpoint IRQ, handle it and return early */
2165 if (event->type.is_devspec == 0) {
2166 /* depevt */
2167 return dwc3_endpoint_interrupt(dwc, &event->depevt);
2168 }
2169
2170 switch (event->type.type) {
2171 case DWC3_EVENT_TYPE_DEV:
2172 dwc3_gadget_interrupt(dwc, &event->devt);
2173 break;
2174 /* REVISIT what to do with Carkit and I2C events ? */
2175 default:
2176 dev_err(dwc->dev, "UNKNOWN IRQ type %d\n", event->raw);
2177 }
2178}
2179
2180static irqreturn_t dwc3_process_event_buf(struct dwc3 *dwc, u32 buf)
2181{
2182 struct dwc3_event_buffer *evt;
2183 int left;
2184 u32 count;
2185
2186 count = dwc3_readl(dwc->regs, DWC3_GEVNTCOUNT(buf));
2187 count &= DWC3_GEVNTCOUNT_MASK;
2188 if (!count)
2189 return IRQ_NONE;
2190
2191 evt = dwc->ev_buffs[buf];
2192 left = count;
2193
2194 while (left > 0) {
2195 union dwc3_event event;
2196
Felipe Balbid70d8442012-02-06 13:40:17 +02002197 event.raw = *(u32 *) (evt->buf + evt->lpos);
2198
Felipe Balbi72246da2011-08-19 18:10:58 +03002199 dwc3_process_event_entry(dwc, &event);
2200 /*
2201 * XXX we wrap around correctly to the next entry as almost all
2202 * entries are 4 bytes in size. There is one entry which has 12
2203 * bytes which is a regular entry followed by 8 bytes data. ATM
2204 * I don't know how things are organized if were get next to the
2205 * a boundary so I worry about that once we try to handle that.
2206 */
2207 evt->lpos = (evt->lpos + 4) % DWC3_EVENT_BUFFERS_SIZE;
2208 left -= 4;
2209
2210 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(buf), 4);
2211 }
2212
2213 return IRQ_HANDLED;
2214}
2215
2216static irqreturn_t dwc3_interrupt(int irq, void *_dwc)
2217{
2218 struct dwc3 *dwc = _dwc;
2219 int i;
2220 irqreturn_t ret = IRQ_NONE;
2221
2222 spin_lock(&dwc->lock);
2223
Felipe Balbi9f622b22011-10-12 10:31:04 +03002224 for (i = 0; i < dwc->num_event_buffers; i++) {
Felipe Balbi72246da2011-08-19 18:10:58 +03002225 irqreturn_t status;
2226
2227 status = dwc3_process_event_buf(dwc, i);
2228 if (status == IRQ_HANDLED)
2229 ret = status;
2230 }
2231
2232 spin_unlock(&dwc->lock);
2233
2234 return ret;
2235}
2236
2237/**
2238 * dwc3_gadget_init - Initializes gadget related registers
Paul Zimmerman1d046792012-02-15 18:56:56 -08002239 * @dwc: pointer to our controller context structure
Felipe Balbi72246da2011-08-19 18:10:58 +03002240 *
2241 * Returns 0 on success otherwise negative errno.
2242 */
2243int __devinit dwc3_gadget_init(struct dwc3 *dwc)
2244{
2245 u32 reg;
2246 int ret;
2247 int irq;
2248
2249 dwc->ctrl_req = dma_alloc_coherent(dwc->dev, sizeof(*dwc->ctrl_req),
2250 &dwc->ctrl_req_addr, GFP_KERNEL);
2251 if (!dwc->ctrl_req) {
2252 dev_err(dwc->dev, "failed to allocate ctrl request\n");
2253 ret = -ENOMEM;
2254 goto err0;
2255 }
2256
2257 dwc->ep0_trb = dma_alloc_coherent(dwc->dev, sizeof(*dwc->ep0_trb),
2258 &dwc->ep0_trb_addr, GFP_KERNEL);
2259 if (!dwc->ep0_trb) {
2260 dev_err(dwc->dev, "failed to allocate ep0 trb\n");
2261 ret = -ENOMEM;
2262 goto err1;
2263 }
2264
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02002265 dwc->setup_buf = kzalloc(sizeof(*dwc->setup_buf) * 2,
2266 GFP_KERNEL);
Felipe Balbi72246da2011-08-19 18:10:58 +03002267 if (!dwc->setup_buf) {
2268 dev_err(dwc->dev, "failed to allocate setup buffer\n");
2269 ret = -ENOMEM;
2270 goto err2;
2271 }
2272
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002273 dwc->ep0_bounce = dma_alloc_coherent(dwc->dev,
2274 512, &dwc->ep0_bounce_addr, GFP_KERNEL);
2275 if (!dwc->ep0_bounce) {
2276 dev_err(dwc->dev, "failed to allocate ep0 bounce buffer\n");
2277 ret = -ENOMEM;
2278 goto err3;
2279 }
2280
Felipe Balbi72246da2011-08-19 18:10:58 +03002281 dev_set_name(&dwc->gadget.dev, "gadget");
2282
2283 dwc->gadget.ops = &dwc3_gadget_ops;
Michal Nazarewiczd327ab52011-11-19 18:27:37 +01002284 dwc->gadget.max_speed = USB_SPEED_SUPER;
Felipe Balbi72246da2011-08-19 18:10:58 +03002285 dwc->gadget.speed = USB_SPEED_UNKNOWN;
2286 dwc->gadget.dev.parent = dwc->dev;
Felipe Balbieeb720f2011-11-28 12:46:59 +02002287 dwc->gadget.sg_supported = true;
Felipe Balbi72246da2011-08-19 18:10:58 +03002288
2289 dma_set_coherent_mask(&dwc->gadget.dev, dwc->dev->coherent_dma_mask);
2290
2291 dwc->gadget.dev.dma_parms = dwc->dev->dma_parms;
2292 dwc->gadget.dev.dma_mask = dwc->dev->dma_mask;
2293 dwc->gadget.dev.release = dwc3_gadget_release;
2294 dwc->gadget.name = "dwc3-gadget";
2295
2296 /*
2297 * REVISIT: Here we should clear all pending IRQs to be
2298 * sure we're starting from a well known location.
2299 */
2300
2301 ret = dwc3_gadget_init_endpoints(dwc);
2302 if (ret)
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002303 goto err4;
Felipe Balbi72246da2011-08-19 18:10:58 +03002304
2305 irq = platform_get_irq(to_platform_device(dwc->dev), 0);
2306
2307 ret = request_irq(irq, dwc3_interrupt, IRQF_SHARED,
2308 "dwc3", dwc);
2309 if (ret) {
2310 dev_err(dwc->dev, "failed to request irq #%d --> %d\n",
2311 irq, ret);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002312 goto err5;
Felipe Balbi72246da2011-08-19 18:10:58 +03002313 }
2314
2315 /* Enable all but Start and End of Frame IRQs */
2316 reg = (DWC3_DEVTEN_VNDRDEVTSTRCVEDEN |
2317 DWC3_DEVTEN_EVNTOVERFLOWEN |
2318 DWC3_DEVTEN_CMDCMPLTEN |
2319 DWC3_DEVTEN_ERRTICERREN |
2320 DWC3_DEVTEN_WKUPEVTEN |
2321 DWC3_DEVTEN_ULSTCNGEN |
2322 DWC3_DEVTEN_CONNECTDONEEN |
2323 DWC3_DEVTEN_USBRSTEN |
2324 DWC3_DEVTEN_DISCONNEVTEN);
2325 dwc3_writel(dwc->regs, DWC3_DEVTEN, reg);
2326
2327 ret = device_register(&dwc->gadget.dev);
2328 if (ret) {
2329 dev_err(dwc->dev, "failed to register gadget device\n");
2330 put_device(&dwc->gadget.dev);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002331 goto err6;
Felipe Balbi72246da2011-08-19 18:10:58 +03002332 }
2333
2334 ret = usb_add_gadget_udc(dwc->dev, &dwc->gadget);
2335 if (ret) {
2336 dev_err(dwc->dev, "failed to register udc\n");
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002337 goto err7;
Felipe Balbi72246da2011-08-19 18:10:58 +03002338 }
2339
2340 return 0;
2341
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002342err7:
Felipe Balbi72246da2011-08-19 18:10:58 +03002343 device_unregister(&dwc->gadget.dev);
2344
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002345err6:
Felipe Balbi72246da2011-08-19 18:10:58 +03002346 dwc3_writel(dwc->regs, DWC3_DEVTEN, 0x00);
2347 free_irq(irq, dwc);
2348
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002349err5:
Felipe Balbi72246da2011-08-19 18:10:58 +03002350 dwc3_gadget_free_endpoints(dwc);
2351
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002352err4:
2353 dma_free_coherent(dwc->dev, 512, dwc->ep0_bounce,
2354 dwc->ep0_bounce_addr);
2355
Felipe Balbi72246da2011-08-19 18:10:58 +03002356err3:
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02002357 kfree(dwc->setup_buf);
Felipe Balbi72246da2011-08-19 18:10:58 +03002358
2359err2:
2360 dma_free_coherent(dwc->dev, sizeof(*dwc->ep0_trb),
2361 dwc->ep0_trb, dwc->ep0_trb_addr);
2362
2363err1:
2364 dma_free_coherent(dwc->dev, sizeof(*dwc->ctrl_req),
2365 dwc->ctrl_req, dwc->ctrl_req_addr);
2366
2367err0:
2368 return ret;
2369}
2370
2371void dwc3_gadget_exit(struct dwc3 *dwc)
2372{
2373 int irq;
Felipe Balbi72246da2011-08-19 18:10:58 +03002374
2375 usb_del_gadget_udc(&dwc->gadget);
2376 irq = platform_get_irq(to_platform_device(dwc->dev), 0);
2377
2378 dwc3_writel(dwc->regs, DWC3_DEVTEN, 0x00);
2379 free_irq(irq, dwc);
2380
Felipe Balbi72246da2011-08-19 18:10:58 +03002381 dwc3_gadget_free_endpoints(dwc);
2382
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002383 dma_free_coherent(dwc->dev, 512, dwc->ep0_bounce,
2384 dwc->ep0_bounce_addr);
2385
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02002386 kfree(dwc->setup_buf);
Felipe Balbi72246da2011-08-19 18:10:58 +03002387
2388 dma_free_coherent(dwc->dev, sizeof(*dwc->ep0_trb),
2389 dwc->ep0_trb, dwc->ep0_trb_addr);
2390
2391 dma_free_coherent(dwc->dev, sizeof(*dwc->ctrl_req),
2392 dwc->ctrl_req, dwc->ctrl_req_addr);
2393
2394 device_unregister(&dwc->gadget.dev);
2395}