blob: eefbfda69c4b742fe7a90f9a594979e40aef630e [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>
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +020052#include <linux/usb/otg.h>
Felipe Balbi72246da2011-08-19 18:10:58 +030053
54#include "core.h"
55#include "gadget.h"
56#include "io.h"
57
Felipe Balbi04a9bfc2012-01-02 18:25:43 +020058/**
59 * dwc3_gadget_set_test_mode - Enables USB2 Test Modes
60 * @dwc: pointer to our context structure
61 * @mode: the mode to set (J, K SE0 NAK, Force Enable)
62 *
63 * Caller should take care of locking. This function will
64 * return 0 on success or -EINVAL if wrong Test Selector
65 * is passed
66 */
67int dwc3_gadget_set_test_mode(struct dwc3 *dwc, int mode)
68{
69 u32 reg;
70
71 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
72 reg &= ~DWC3_DCTL_TSTCTRL_MASK;
73
74 switch (mode) {
75 case TEST_J:
76 case TEST_K:
77 case TEST_SE0_NAK:
78 case TEST_PACKET:
79 case TEST_FORCE_EN:
80 reg |= mode << 1;
81 break;
82 default:
83 return -EINVAL;
84 }
85
86 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
87
88 return 0;
89}
90
Felipe Balbi8598bde2012-01-02 18:55:57 +020091/**
92 * dwc3_gadget_set_link_state - Sets USB Link to a particular State
93 * @dwc: pointer to our context structure
94 * @state: the state to put link into
95 *
96 * Caller should take care of locking. This function will
Paul Zimmermanaee63e32012-02-24 17:32:15 -080097 * return 0 on success or -ETIMEDOUT.
Felipe Balbi8598bde2012-01-02 18:55:57 +020098 */
99int dwc3_gadget_set_link_state(struct dwc3 *dwc, enum dwc3_link_state state)
100{
Paul Zimmermanaee63e32012-02-24 17:32:15 -0800101 int retries = 10000;
Felipe Balbi8598bde2012-01-02 18:55:57 +0200102 u32 reg;
103
104 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
105 reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK;
106
107 /* set requested state */
108 reg |= DWC3_DCTL_ULSTCHNGREQ(state);
109 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
110
111 /* wait for a change in DSTS */
112 while (--retries) {
113 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
114
Felipe Balbi8598bde2012-01-02 18:55:57 +0200115 if (DWC3_DSTS_USBLNKST(reg) == state)
116 return 0;
117
Paul Zimmermanaee63e32012-02-24 17:32:15 -0800118 udelay(5);
Felipe Balbi8598bde2012-01-02 18:55:57 +0200119 }
120
121 dev_vdbg(dwc->dev, "link state change request timed out\n");
122
123 return -ETIMEDOUT;
124}
125
Felipe Balbi457e84b2012-01-18 18:04:09 +0200126/**
127 * dwc3_gadget_resize_tx_fifos - reallocate fifo spaces for current use-case
128 * @dwc: pointer to our context structure
129 *
130 * This function will a best effort FIFO allocation in order
131 * to improve FIFO usage and throughput, while still allowing
132 * us to enable as many endpoints as possible.
133 *
134 * Keep in mind that this operation will be highly dependent
135 * on the configured size for RAM1 - which contains TxFifo -,
136 * the amount of endpoints enabled on coreConsultant tool, and
137 * the width of the Master Bus.
138 *
139 * In the ideal world, we would always be able to satisfy the
140 * following equation:
141 *
142 * ((512 + 2 * MDWIDTH-Bytes) + (Number of IN Endpoints - 1) * \
143 * (3 * (1024 + MDWIDTH-Bytes) + MDWIDTH-Bytes)) / MDWIDTH-Bytes
144 *
145 * Unfortunately, due to many variables that's not always the case.
146 */
147int dwc3_gadget_resize_tx_fifos(struct dwc3 *dwc)
148{
149 int last_fifo_depth = 0;
150 int ram1_depth;
151 int fifo_size;
152 int mdwidth;
153 int num;
154
155 if (!dwc->needs_fifo_resize)
156 return 0;
157
158 ram1_depth = DWC3_RAM1_DEPTH(dwc->hwparams.hwparams7);
159 mdwidth = DWC3_MDWIDTH(dwc->hwparams.hwparams0);
160
161 /* MDWIDTH is represented in bits, we need it in bytes */
162 mdwidth >>= 3;
163
164 /*
165 * FIXME For now we will only allocate 1 wMaxPacketSize space
166 * for each enabled endpoint, later patches will come to
167 * improve this algorithm so that we better use the internal
168 * FIFO space
169 */
170 for (num = 0; num < DWC3_ENDPOINTS_NUM; num++) {
171 struct dwc3_ep *dep = dwc->eps[num];
172 int fifo_number = dep->number >> 1;
Felipe Balbi2e81c362012-02-02 13:01:12 +0200173 int mult = 1;
Felipe Balbi457e84b2012-01-18 18:04:09 +0200174 int tmp;
175
176 if (!(dep->number & 1))
177 continue;
178
179 if (!(dep->flags & DWC3_EP_ENABLED))
180 continue;
181
Felipe Balbi2e81c362012-02-02 13:01:12 +0200182 if (usb_endpoint_xfer_bulk(dep->desc)
183 || usb_endpoint_xfer_isoc(dep->desc))
184 mult = 3;
185
186 /*
187 * REVISIT: the following assumes we will always have enough
188 * space available on the FIFO RAM for all possible use cases.
189 * Make sure that's true somehow and change FIFO allocation
190 * accordingly.
191 *
192 * If we have Bulk or Isochronous endpoints, we want
193 * them to be able to be very, very fast. So we're giving
194 * those endpoints a fifo_size which is enough for 3 full
195 * packets
196 */
197 tmp = mult * (dep->endpoint.maxpacket + mdwidth);
Felipe Balbi457e84b2012-01-18 18:04:09 +0200198 tmp += mdwidth;
199
200 fifo_size = DIV_ROUND_UP(tmp, mdwidth);
Felipe Balbi2e81c362012-02-02 13:01:12 +0200201
Felipe Balbi457e84b2012-01-18 18:04:09 +0200202 fifo_size |= (last_fifo_depth << 16);
203
204 dev_vdbg(dwc->dev, "%s: Fifo Addr %04x Size %d\n",
205 dep->name, last_fifo_depth, fifo_size & 0xffff);
206
207 dwc3_writel(dwc->regs, DWC3_GTXFIFOSIZ(fifo_number),
208 fifo_size);
209
210 last_fifo_depth += (fifo_size & 0xffff);
211 }
212
213 return 0;
214}
215
Felipe Balbi72246da2011-08-19 18:10:58 +0300216void dwc3_gadget_giveback(struct dwc3_ep *dep, struct dwc3_request *req,
217 int status)
218{
219 struct dwc3 *dwc = dep->dwc;
220
221 if (req->queued) {
Felipe Balbieeb720f2011-11-28 12:46:59 +0200222 if (req->request.num_mapped_sgs)
223 dep->busy_slot += req->request.num_mapped_sgs;
224 else
225 dep->busy_slot++;
226
Felipe Balbi72246da2011-08-19 18:10:58 +0300227 /*
228 * Skip LINK TRB. We can't use req->trb and check for
229 * DWC3_TRBCTL_LINK_TRB because it points the TRB we just
230 * completed (not the LINK TRB).
231 */
232 if (((dep->busy_slot & DWC3_TRB_MASK) == DWC3_TRB_NUM - 1) &&
233 usb_endpoint_xfer_isoc(dep->desc))
234 dep->busy_slot++;
235 }
236 list_del(&req->list);
Felipe Balbieeb720f2011-11-28 12:46:59 +0200237 req->trb = NULL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300238
239 if (req->request.status == -EINPROGRESS)
240 req->request.status = status;
241
Pratyush Anand7acab782012-08-10 13:42:16 +0530242 if (dwc->ep0_bounced && dep->number == 0)
243 dwc->ep0_bounced = false;
244 else
245 usb_gadget_unmap_request(&dwc->gadget, &req->request,
246 req->direction);
Felipe Balbi72246da2011-08-19 18:10:58 +0300247
248 dev_dbg(dwc->dev, "request %p from %s completed %d/%d ===> %d\n",
249 req, dep->name, req->request.actual,
250 req->request.length, status);
251
252 spin_unlock(&dwc->lock);
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +0200253 req->request.complete(&dep->endpoint, &req->request);
Felipe Balbi72246da2011-08-19 18:10:58 +0300254 spin_lock(&dwc->lock);
255}
256
257static const char *dwc3_gadget_ep_cmd_string(u8 cmd)
258{
259 switch (cmd) {
260 case DWC3_DEPCMD_DEPSTARTCFG:
261 return "Start New Configuration";
262 case DWC3_DEPCMD_ENDTRANSFER:
263 return "End Transfer";
264 case DWC3_DEPCMD_UPDATETRANSFER:
265 return "Update Transfer";
266 case DWC3_DEPCMD_STARTTRANSFER:
267 return "Start Transfer";
268 case DWC3_DEPCMD_CLEARSTALL:
269 return "Clear Stall";
270 case DWC3_DEPCMD_SETSTALL:
271 return "Set Stall";
272 case DWC3_DEPCMD_GETSEQNUMBER:
273 return "Get Data Sequence Number";
274 case DWC3_DEPCMD_SETTRANSFRESOURCE:
275 return "Set Endpoint Transfer Resource";
276 case DWC3_DEPCMD_SETEPCONFIG:
277 return "Set Endpoint Configuration";
278 default:
279 return "UNKNOWN command";
280 }
281}
282
283int dwc3_send_gadget_ep_cmd(struct dwc3 *dwc, unsigned ep,
284 unsigned cmd, struct dwc3_gadget_ep_cmd_params *params)
285{
286 struct dwc3_ep *dep = dwc->eps[ep];
Sebastian Andrzej Siewior61d58242011-08-29 16:46:38 +0200287 u32 timeout = 500;
Felipe Balbi72246da2011-08-19 18:10:58 +0300288 u32 reg;
289
290 dev_vdbg(dwc->dev, "%s: cmd '%s' params %08x %08x %08x\n",
291 dep->name,
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300292 dwc3_gadget_ep_cmd_string(cmd), params->param0,
293 params->param1, params->param2);
Felipe Balbi72246da2011-08-19 18:10:58 +0300294
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300295 dwc3_writel(dwc->regs, DWC3_DEPCMDPAR0(ep), params->param0);
296 dwc3_writel(dwc->regs, DWC3_DEPCMDPAR1(ep), params->param1);
297 dwc3_writel(dwc->regs, DWC3_DEPCMDPAR2(ep), params->param2);
Felipe Balbi72246da2011-08-19 18:10:58 +0300298
299 dwc3_writel(dwc->regs, DWC3_DEPCMD(ep), cmd | DWC3_DEPCMD_CMDACT);
300 do {
301 reg = dwc3_readl(dwc->regs, DWC3_DEPCMD(ep));
302 if (!(reg & DWC3_DEPCMD_CMDACT)) {
Felipe Balbi164f6e12011-08-27 20:29:58 +0300303 dev_vdbg(dwc->dev, "Command Complete --> %d\n",
304 DWC3_DEPCMD_STATUS(reg));
Felipe Balbi72246da2011-08-19 18:10:58 +0300305 return 0;
306 }
307
308 /*
Felipe Balbi72246da2011-08-19 18:10:58 +0300309 * We can't sleep here, because it is also called from
310 * interrupt context.
311 */
312 timeout--;
313 if (!timeout)
314 return -ETIMEDOUT;
315
Sebastian Andrzej Siewior61d58242011-08-29 16:46:38 +0200316 udelay(1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300317 } while (1);
318}
319
Ido Shayevitzfa65a582012-06-06 14:39:54 +0300320dma_addr_t dwc3_trb_dma_offset(struct dwc3_ep *dep,
Felipe Balbif6bafc62012-02-06 11:04:53 +0200321 struct dwc3_trb *trb)
Felipe Balbi72246da2011-08-19 18:10:58 +0300322{
Paul Zimmermanc439ef82011-09-30 10:58:45 +0300323 u32 offset = (char *) trb - (char *) dep->trb_pool;
Felipe Balbi72246da2011-08-19 18:10:58 +0300324
325 return dep->trb_pool_dma + offset;
326}
327
328static int dwc3_alloc_trb_pool(struct dwc3_ep *dep)
329{
330 struct dwc3 *dwc = dep->dwc;
331
332 if (dep->trb_pool)
333 return 0;
334
335 if (dep->number == 0 || dep->number == 1)
336 return 0;
337
338 dep->trb_pool = dma_alloc_coherent(dwc->dev,
339 sizeof(struct dwc3_trb) * DWC3_TRB_NUM,
340 &dep->trb_pool_dma, GFP_KERNEL);
341 if (!dep->trb_pool) {
342 dev_err(dep->dwc->dev, "failed to allocate trb pool for %s\n",
343 dep->name);
344 return -ENOMEM;
345 }
346
347 return 0;
348}
349
350static void dwc3_free_trb_pool(struct dwc3_ep *dep)
351{
352 struct dwc3 *dwc = dep->dwc;
353
354 dma_free_coherent(dwc->dev, sizeof(struct dwc3_trb) * DWC3_TRB_NUM,
355 dep->trb_pool, dep->trb_pool_dma);
356
357 dep->trb_pool = NULL;
358 dep->trb_pool_dma = 0;
359}
360
361static int dwc3_gadget_start_config(struct dwc3 *dwc, struct dwc3_ep *dep)
362{
363 struct dwc3_gadget_ep_cmd_params params;
364 u32 cmd;
365
366 memset(&params, 0x00, sizeof(params));
367
368 if (dep->number != 1) {
369 cmd = DWC3_DEPCMD_DEPSTARTCFG;
370 /* XferRscIdx == 0 for ep0 and 2 for the remaining */
Paul Zimmermanb23c8432011-09-30 10:58:42 +0300371 if (dep->number > 1) {
372 if (dwc->start_config_issued)
373 return 0;
374 dwc->start_config_issued = true;
Felipe Balbi72246da2011-08-19 18:10:58 +0300375 cmd |= DWC3_DEPCMD_PARAM(2);
Paul Zimmermanb23c8432011-09-30 10:58:42 +0300376 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300377
378 return dwc3_send_gadget_ep_cmd(dwc, 0, cmd, &params);
379 }
380
381 return 0;
382}
383
384static int dwc3_gadget_set_ep_config(struct dwc3 *dwc, struct dwc3_ep *dep,
Felipe Balbic90bfae2011-11-29 13:11:21 +0200385 const struct usb_endpoint_descriptor *desc,
386 const struct usb_ss_ep_comp_descriptor *comp_desc)
Felipe Balbi72246da2011-08-19 18:10:58 +0300387{
388 struct dwc3_gadget_ep_cmd_params params;
389
390 memset(&params, 0x00, sizeof(params));
391
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300392 params.param0 = DWC3_DEPCFG_EP_TYPE(usb_endpoint_type(desc))
393 | DWC3_DEPCFG_MAX_PACKET_SIZE(usb_endpoint_maxp(desc))
394 | DWC3_DEPCFG_BURST_SIZE(dep->endpoint.maxburst);
Felipe Balbi72246da2011-08-19 18:10:58 +0300395
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300396 params.param1 = DWC3_DEPCFG_XFER_COMPLETE_EN
397 | DWC3_DEPCFG_XFER_NOT_READY_EN;
Felipe Balbi72246da2011-08-19 18:10:58 +0300398
Felipe Balbi18b7ede2012-01-02 13:35:41 +0200399 if (usb_ss_max_streams(comp_desc) && usb_endpoint_xfer_bulk(desc)) {
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300400 params.param1 |= DWC3_DEPCFG_STREAM_CAPABLE
401 | DWC3_DEPCFG_STREAM_EVENT_EN;
Felipe Balbi879631a2011-09-30 10:58:47 +0300402 dep->stream_capable = true;
403 }
404
Felipe Balbi72246da2011-08-19 18:10:58 +0300405 if (usb_endpoint_xfer_isoc(desc))
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300406 params.param1 |= DWC3_DEPCFG_XFER_IN_PROGRESS_EN;
Felipe Balbi72246da2011-08-19 18:10:58 +0300407
408 /*
409 * We are doing 1:1 mapping for endpoints, meaning
410 * Physical Endpoints 2 maps to Logical Endpoint 2 and
411 * so on. We consider the direction bit as part of the physical
412 * endpoint number. So USB endpoint 0x81 is 0x03.
413 */
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300414 params.param1 |= DWC3_DEPCFG_EP_NUMBER(dep->number);
Felipe Balbi72246da2011-08-19 18:10:58 +0300415
416 /*
417 * We must use the lower 16 TX FIFOs even though
418 * HW might have more
419 */
420 if (dep->direction)
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300421 params.param0 |= DWC3_DEPCFG_FIFO_NUMBER(dep->number >> 1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300422
423 if (desc->bInterval) {
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300424 params.param1 |= DWC3_DEPCFG_BINTERVAL_M1(desc->bInterval - 1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300425 dep->interval = 1 << (desc->bInterval - 1);
426 }
427
428 return dwc3_send_gadget_ep_cmd(dwc, dep->number,
429 DWC3_DEPCMD_SETEPCONFIG, &params);
430}
431
432static int dwc3_gadget_set_xfer_resource(struct dwc3 *dwc, struct dwc3_ep *dep)
433{
434 struct dwc3_gadget_ep_cmd_params params;
435
436 memset(&params, 0x00, sizeof(params));
437
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300438 params.param0 = DWC3_DEPXFERCFG_NUM_XFER_RES(1);
Felipe Balbi72246da2011-08-19 18:10:58 +0300439
440 return dwc3_send_gadget_ep_cmd(dwc, dep->number,
441 DWC3_DEPCMD_SETTRANSFRESOURCE, &params);
442}
443
444/**
445 * __dwc3_gadget_ep_enable - Initializes a HW endpoint
446 * @dep: endpoint to be initialized
447 * @desc: USB Endpoint Descriptor
448 *
449 * Caller should take care of locking
450 */
451static int __dwc3_gadget_ep_enable(struct dwc3_ep *dep,
Felipe Balbic90bfae2011-11-29 13:11:21 +0200452 const struct usb_endpoint_descriptor *desc,
453 const struct usb_ss_ep_comp_descriptor *comp_desc)
Felipe Balbi72246da2011-08-19 18:10:58 +0300454{
455 struct dwc3 *dwc = dep->dwc;
456 u32 reg;
457 int ret = -ENOMEM;
458
459 if (!(dep->flags & DWC3_EP_ENABLED)) {
460 ret = dwc3_gadget_start_config(dwc, dep);
461 if (ret)
462 return ret;
463 }
464
Felipe Balbic90bfae2011-11-29 13:11:21 +0200465 ret = dwc3_gadget_set_ep_config(dwc, dep, desc, comp_desc);
Felipe Balbi72246da2011-08-19 18:10:58 +0300466 if (ret)
467 return ret;
468
469 if (!(dep->flags & DWC3_EP_ENABLED)) {
Felipe Balbif6bafc62012-02-06 11:04:53 +0200470 struct dwc3_trb *trb_st_hw;
471 struct dwc3_trb *trb_link;
Felipe Balbi72246da2011-08-19 18:10:58 +0300472
473 ret = dwc3_gadget_set_xfer_resource(dwc, dep);
474 if (ret)
475 return ret;
476
477 dep->desc = desc;
Felipe Balbic90bfae2011-11-29 13:11:21 +0200478 dep->comp_desc = comp_desc;
Felipe Balbi72246da2011-08-19 18:10:58 +0300479 dep->type = usb_endpoint_type(desc);
480 dep->flags |= DWC3_EP_ENABLED;
481
482 reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
483 reg |= DWC3_DALEPENA_EP(dep->number);
484 dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
485
486 if (!usb_endpoint_xfer_isoc(desc))
487 return 0;
488
489 memset(&trb_link, 0, sizeof(trb_link));
490
Paul Zimmerman1d046792012-02-15 18:56:56 -0800491 /* Link TRB for ISOC. The HWO bit is never reset */
Felipe Balbi72246da2011-08-19 18:10:58 +0300492 trb_st_hw = &dep->trb_pool[0];
493
Felipe Balbif6bafc62012-02-06 11:04:53 +0200494 trb_link = &dep->trb_pool[DWC3_TRB_NUM - 1];
Felipe Balbi72246da2011-08-19 18:10:58 +0300495
Felipe Balbif6bafc62012-02-06 11:04:53 +0200496 trb_link->bpl = lower_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw));
497 trb_link->bph = upper_32_bits(dwc3_trb_dma_offset(dep, trb_st_hw));
498 trb_link->ctrl |= DWC3_TRBCTL_LINK_TRB;
499 trb_link->ctrl |= DWC3_TRB_CTRL_HWO;
Felipe Balbi72246da2011-08-19 18:10:58 +0300500 }
501
502 return 0;
503}
504
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200505static void dwc3_stop_active_transfer(struct dwc3 *dwc, u32 epnum);
506static void dwc3_remove_requests(struct dwc3 *dwc, struct dwc3_ep *dep)
Felipe Balbi72246da2011-08-19 18:10:58 +0300507{
508 struct dwc3_request *req;
509
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200510 if (!list_empty(&dep->req_queued))
511 dwc3_stop_active_transfer(dwc, dep->number);
512
Felipe Balbi72246da2011-08-19 18:10:58 +0300513 while (!list_empty(&dep->request_list)) {
514 req = next_request(&dep->request_list);
515
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200516 dwc3_gadget_giveback(dep, req, -ESHUTDOWN);
Felipe Balbi72246da2011-08-19 18:10:58 +0300517 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300518}
519
520/**
521 * __dwc3_gadget_ep_disable - Disables a HW endpoint
522 * @dep: the endpoint to disable
523 *
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200524 * This function also removes requests which are currently processed ny the
525 * hardware and those which are not yet scheduled.
526 * Caller should take care of locking.
Felipe Balbi72246da2011-08-19 18:10:58 +0300527 */
Felipe Balbi72246da2011-08-19 18:10:58 +0300528static int __dwc3_gadget_ep_disable(struct dwc3_ep *dep)
529{
530 struct dwc3 *dwc = dep->dwc;
531 u32 reg;
532
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +0200533 dwc3_remove_requests(dwc, dep);
Felipe Balbi72246da2011-08-19 18:10:58 +0300534
Felipe Balbi4cbd8a22014-04-16 10:30:33 -0500535 /* make sure HW endpoint isn't stalled */
536 if (dep->flags & DWC3_EP_STALL)
537 __dwc3_gadget_ep_set_halt(dep, 0);
538
Felipe Balbi72246da2011-08-19 18:10:58 +0300539 reg = dwc3_readl(dwc->regs, DWC3_DALEPENA);
540 reg &= ~DWC3_DALEPENA_EP(dep->number);
541 dwc3_writel(dwc->regs, DWC3_DALEPENA, reg);
542
Felipe Balbi879631a2011-09-30 10:58:47 +0300543 dep->stream_capable = false;
Felipe Balbi72246da2011-08-19 18:10:58 +0300544 dep->desc = NULL;
Ido Shayevitzf9c56cd2012-02-08 13:56:48 +0200545 dep->endpoint.desc = NULL;
Felipe Balbic90bfae2011-11-29 13:11:21 +0200546 dep->comp_desc = NULL;
Felipe Balbi72246da2011-08-19 18:10:58 +0300547 dep->type = 0;
Felipe Balbi879631a2011-09-30 10:58:47 +0300548 dep->flags = 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300549
550 return 0;
551}
552
553/* -------------------------------------------------------------------------- */
554
555static int dwc3_gadget_ep0_enable(struct usb_ep *ep,
556 const struct usb_endpoint_descriptor *desc)
557{
558 return -EINVAL;
559}
560
561static int dwc3_gadget_ep0_disable(struct usb_ep *ep)
562{
563 return -EINVAL;
564}
565
566/* -------------------------------------------------------------------------- */
567
568static int dwc3_gadget_ep_enable(struct usb_ep *ep,
569 const struct usb_endpoint_descriptor *desc)
570{
571 struct dwc3_ep *dep;
572 struct dwc3 *dwc;
573 unsigned long flags;
574 int ret;
575
576 if (!ep || !desc || desc->bDescriptorType != USB_DT_ENDPOINT) {
577 pr_debug("dwc3: invalid parameters\n");
578 return -EINVAL;
579 }
580
581 if (!desc->wMaxPacketSize) {
582 pr_debug("dwc3: missing wMaxPacketSize\n");
583 return -EINVAL;
584 }
585
586 dep = to_dwc3_ep(ep);
587 dwc = dep->dwc;
588
589 switch (usb_endpoint_type(desc)) {
590 case USB_ENDPOINT_XFER_CONTROL:
Anton Tikhomirov27a78d62012-02-23 15:38:46 +0900591 strlcat(dep->name, "-control", sizeof(dep->name));
Felipe Balbi72246da2011-08-19 18:10:58 +0300592 break;
593 case USB_ENDPOINT_XFER_ISOC:
Anton Tikhomirov27a78d62012-02-23 15:38:46 +0900594 strlcat(dep->name, "-isoc", sizeof(dep->name));
Felipe Balbi72246da2011-08-19 18:10:58 +0300595 break;
596 case USB_ENDPOINT_XFER_BULK:
Anton Tikhomirov27a78d62012-02-23 15:38:46 +0900597 strlcat(dep->name, "-bulk", sizeof(dep->name));
Felipe Balbi72246da2011-08-19 18:10:58 +0300598 break;
599 case USB_ENDPOINT_XFER_INT:
Anton Tikhomirov27a78d62012-02-23 15:38:46 +0900600 strlcat(dep->name, "-int", sizeof(dep->name));
Felipe Balbi72246da2011-08-19 18:10:58 +0300601 break;
602 default:
603 dev_err(dwc->dev, "invalid endpoint transfer type\n");
604 }
605
606 if (dep->flags & DWC3_EP_ENABLED) {
607 dev_WARN_ONCE(dwc->dev, true, "%s is already enabled\n",
608 dep->name);
609 return 0;
610 }
611
612 dev_vdbg(dwc->dev, "Enabling %s\n", dep->name);
613
614 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbic90bfae2011-11-29 13:11:21 +0200615 ret = __dwc3_gadget_ep_enable(dep, desc, ep->comp_desc);
Felipe Balbi72246da2011-08-19 18:10:58 +0300616 spin_unlock_irqrestore(&dwc->lock, flags);
617
618 return ret;
619}
620
621static int dwc3_gadget_ep_disable(struct usb_ep *ep)
622{
623 struct dwc3_ep *dep;
624 struct dwc3 *dwc;
625 unsigned long flags;
626 int ret;
627
628 if (!ep) {
629 pr_debug("dwc3: invalid parameters\n");
630 return -EINVAL;
631 }
632
633 dep = to_dwc3_ep(ep);
634 dwc = dep->dwc;
635
636 if (!(dep->flags & DWC3_EP_ENABLED)) {
637 dev_WARN_ONCE(dwc->dev, true, "%s is already disabled\n",
638 dep->name);
639 return 0;
640 }
641
642 snprintf(dep->name, sizeof(dep->name), "ep%d%s",
643 dep->number >> 1,
644 (dep->number & 1) ? "in" : "out");
645
646 spin_lock_irqsave(&dwc->lock, flags);
647 ret = __dwc3_gadget_ep_disable(dep);
648 spin_unlock_irqrestore(&dwc->lock, flags);
649
650 return ret;
651}
652
653static struct usb_request *dwc3_gadget_ep_alloc_request(struct usb_ep *ep,
654 gfp_t gfp_flags)
655{
656 struct dwc3_request *req;
657 struct dwc3_ep *dep = to_dwc3_ep(ep);
658 struct dwc3 *dwc = dep->dwc;
659
660 req = kzalloc(sizeof(*req), gfp_flags);
661 if (!req) {
662 dev_err(dwc->dev, "not enough memory\n");
663 return NULL;
664 }
665
666 req->epnum = dep->number;
667 req->dep = dep;
Felipe Balbi72246da2011-08-19 18:10:58 +0300668
669 return &req->request;
670}
671
672static void dwc3_gadget_ep_free_request(struct usb_ep *ep,
673 struct usb_request *request)
674{
675 struct dwc3_request *req = to_dwc3_request(request);
676
677 kfree(req);
678}
679
Felipe Balbic71fc372011-11-22 11:37:34 +0200680/**
681 * dwc3_prepare_one_trb - setup one TRB from one request
682 * @dep: endpoint for which this request is prepared
683 * @req: dwc3_request pointer
684 */
Felipe Balbi68e823e2011-11-28 12:25:01 +0200685static void dwc3_prepare_one_trb(struct dwc3_ep *dep,
Felipe Balbieeb720f2011-11-28 12:46:59 +0200686 struct dwc3_request *req, dma_addr_t dma,
687 unsigned length, unsigned last, unsigned chain)
Felipe Balbic71fc372011-11-22 11:37:34 +0200688{
Felipe Balbieeb720f2011-11-28 12:46:59 +0200689 struct dwc3 *dwc = dep->dwc;
Felipe Balbif6bafc62012-02-06 11:04:53 +0200690 struct dwc3_trb *trb;
Felipe Balbic71fc372011-11-22 11:37:34 +0200691
692 unsigned int cur_slot;
693
Felipe Balbieeb720f2011-11-28 12:46:59 +0200694 dev_vdbg(dwc->dev, "%s: req %p dma %08llx length %d%s%s\n",
695 dep->name, req, (unsigned long long) dma,
696 length, last ? " last" : "",
697 chain ? " chain" : "");
698
Felipe Balbif6bafc62012-02-06 11:04:53 +0200699 trb = &dep->trb_pool[dep->free_slot & DWC3_TRB_MASK];
Felipe Balbic71fc372011-11-22 11:37:34 +0200700 cur_slot = dep->free_slot;
701 dep->free_slot++;
702
703 /* Skip the LINK-TRB on ISOC */
704 if (((cur_slot & DWC3_TRB_MASK) == DWC3_TRB_NUM - 1) &&
705 usb_endpoint_xfer_isoc(dep->desc))
Felipe Balbi68e823e2011-11-28 12:25:01 +0200706 return;
Felipe Balbic71fc372011-11-22 11:37:34 +0200707
Felipe Balbieeb720f2011-11-28 12:46:59 +0200708 if (!req->trb) {
709 dwc3_gadget_move_request_queued(req);
Felipe Balbif6bafc62012-02-06 11:04:53 +0200710 req->trb = trb;
711 req->trb_dma = dwc3_trb_dma_offset(dep, trb);
Felipe Balbieeb720f2011-11-28 12:46:59 +0200712 }
Felipe Balbic71fc372011-11-22 11:37:34 +0200713
Felipe Balbif6bafc62012-02-06 11:04:53 +0200714 trb->size = DWC3_TRB_SIZE_LENGTH(length);
715 trb->bpl = lower_32_bits(dma);
716 trb->bph = upper_32_bits(dma);
Felipe Balbic71fc372011-11-22 11:37:34 +0200717
718 switch (usb_endpoint_type(dep->desc)) {
719 case USB_ENDPOINT_XFER_CONTROL:
Felipe Balbif6bafc62012-02-06 11:04:53 +0200720 trb->ctrl = DWC3_TRBCTL_CONTROL_SETUP;
Felipe Balbic71fc372011-11-22 11:37:34 +0200721 break;
722
723 case USB_ENDPOINT_XFER_ISOC:
Felipe Balbif6bafc62012-02-06 11:04:53 +0200724 trb->ctrl = DWC3_TRBCTL_ISOCHRONOUS_FIRST;
Felipe Balbic71fc372011-11-22 11:37:34 +0200725
726 /* IOC every DWC3_TRB_NUM / 4 so we can refill */
727 if (!(cur_slot % (DWC3_TRB_NUM / 4)))
Felipe Balbif6bafc62012-02-06 11:04:53 +0200728 trb->ctrl |= DWC3_TRB_CTRL_IOC;
Felipe Balbic71fc372011-11-22 11:37:34 +0200729 break;
730
731 case USB_ENDPOINT_XFER_BULK:
732 case USB_ENDPOINT_XFER_INT:
Felipe Balbif6bafc62012-02-06 11:04:53 +0200733 trb->ctrl = DWC3_TRBCTL_NORMAL;
Felipe Balbic71fc372011-11-22 11:37:34 +0200734 break;
735 default:
736 /*
737 * This is only possible with faulty memory because we
738 * checked it already :)
739 */
740 BUG();
741 }
742
Felipe Balbif6bafc62012-02-06 11:04:53 +0200743 if (usb_endpoint_xfer_isoc(dep->desc)) {
744 trb->ctrl |= DWC3_TRB_CTRL_ISP_IMI;
745 trb->ctrl |= DWC3_TRB_CTRL_CSP;
746 } else {
747 if (chain)
748 trb->ctrl |= DWC3_TRB_CTRL_CHN;
Felipe Balbic71fc372011-11-22 11:37:34 +0200749
Felipe Balbif6bafc62012-02-06 11:04:53 +0200750 if (last)
751 trb->ctrl |= DWC3_TRB_CTRL_LST;
752 }
753
754 if (usb_endpoint_xfer_bulk(dep->desc) && dep->stream_capable)
755 trb->ctrl |= DWC3_TRB_CTRL_SID_SOFN(req->request.stream_id);
756
757 trb->ctrl |= DWC3_TRB_CTRL_HWO;
Felipe Balbic71fc372011-11-22 11:37:34 +0200758}
759
Felipe Balbi72246da2011-08-19 18:10:58 +0300760/*
761 * dwc3_prepare_trbs - setup TRBs from requests
762 * @dep: endpoint for which requests are being prepared
763 * @starting: true if the endpoint is idle and no requests are queued.
764 *
Paul Zimmerman1d046792012-02-15 18:56:56 -0800765 * The function goes through the requests list and sets up TRBs for the
766 * transfers. The function returns once there are no more TRBs available or
767 * it runs out of requests.
Felipe Balbi72246da2011-08-19 18:10:58 +0300768 */
Felipe Balbi68e823e2011-11-28 12:25:01 +0200769static void dwc3_prepare_trbs(struct dwc3_ep *dep, bool starting)
Felipe Balbi72246da2011-08-19 18:10:58 +0300770{
Felipe Balbi68e823e2011-11-28 12:25:01 +0200771 struct dwc3_request *req, *n;
Felipe Balbi72246da2011-08-19 18:10:58 +0300772 u32 trbs_left;
Paul Zimmerman8d62cd62012-02-15 13:35:06 +0200773 u32 max;
Felipe Balbic71fc372011-11-22 11:37:34 +0200774 unsigned int last_one = 0;
Felipe Balbi72246da2011-08-19 18:10:58 +0300775
776 BUILD_BUG_ON_NOT_POWER_OF_2(DWC3_TRB_NUM);
777
778 /* the first request must not be queued */
779 trbs_left = (dep->busy_slot - dep->free_slot) & DWC3_TRB_MASK;
Felipe Balbic71fc372011-11-22 11:37:34 +0200780
Paul Zimmerman8d62cd62012-02-15 13:35:06 +0200781 /* Can't wrap around on a non-isoc EP since there's no link TRB */
782 if (!usb_endpoint_xfer_isoc(dep->desc)) {
783 max = DWC3_TRB_NUM - (dep->free_slot & DWC3_TRB_MASK);
784 if (trbs_left > max)
785 trbs_left = max;
786 }
787
Felipe Balbi72246da2011-08-19 18:10:58 +0300788 /*
Paul Zimmerman1d046792012-02-15 18:56:56 -0800789 * If busy & slot are equal than it is either full or empty. If we are
790 * starting to process requests then we are empty. Otherwise we are
Felipe Balbi72246da2011-08-19 18:10:58 +0300791 * full and don't do anything
792 */
793 if (!trbs_left) {
794 if (!starting)
Felipe Balbi68e823e2011-11-28 12:25:01 +0200795 return;
Felipe Balbi72246da2011-08-19 18:10:58 +0300796 trbs_left = DWC3_TRB_NUM;
797 /*
798 * In case we start from scratch, we queue the ISOC requests
799 * starting from slot 1. This is done because we use ring
800 * buffer and have no LST bit to stop us. Instead, we place
Paul Zimmerman1d046792012-02-15 18:56:56 -0800801 * IOC bit every TRB_NUM/4. We try to avoid having an interrupt
Felipe Balbi72246da2011-08-19 18:10:58 +0300802 * after the first request so we start at slot 1 and have
803 * 7 requests proceed before we hit the first IOC.
804 * Other transfer types don't use the ring buffer and are
805 * processed from the first TRB until the last one. Since we
806 * don't wrap around we have to start at the beginning.
807 */
808 if (usb_endpoint_xfer_isoc(dep->desc)) {
809 dep->busy_slot = 1;
810 dep->free_slot = 1;
811 } else {
812 dep->busy_slot = 0;
813 dep->free_slot = 0;
814 }
815 }
816
817 /* The last TRB is a link TRB, not used for xfer */
818 if ((trbs_left <= 1) && usb_endpoint_xfer_isoc(dep->desc))
Felipe Balbi68e823e2011-11-28 12:25:01 +0200819 return;
Felipe Balbi72246da2011-08-19 18:10:58 +0300820
821 list_for_each_entry_safe(req, n, &dep->request_list, list) {
Felipe Balbieeb720f2011-11-28 12:46:59 +0200822 unsigned length;
823 dma_addr_t dma;
Felipe Balbi72246da2011-08-19 18:10:58 +0300824
Felipe Balbieeb720f2011-11-28 12:46:59 +0200825 if (req->request.num_mapped_sgs > 0) {
826 struct usb_request *request = &req->request;
827 struct scatterlist *sg = request->sg;
828 struct scatterlist *s;
829 int i;
Felipe Balbi72246da2011-08-19 18:10:58 +0300830
Felipe Balbieeb720f2011-11-28 12:46:59 +0200831 for_each_sg(sg, s, request->num_mapped_sgs, i) {
832 unsigned chain = true;
Felipe Balbi72246da2011-08-19 18:10:58 +0300833
Felipe Balbieeb720f2011-11-28 12:46:59 +0200834 length = sg_dma_len(s);
835 dma = sg_dma_address(s);
Felipe Balbi72246da2011-08-19 18:10:58 +0300836
Paul Zimmerman1d046792012-02-15 18:56:56 -0800837 if (i == (request->num_mapped_sgs - 1) ||
838 sg_is_last(s)) {
Felipe Balbieeb720f2011-11-28 12:46:59 +0200839 last_one = true;
840 chain = false;
841 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300842
Felipe Balbieeb720f2011-11-28 12:46:59 +0200843 trbs_left--;
844 if (!trbs_left)
845 last_one = true;
Felipe Balbi72246da2011-08-19 18:10:58 +0300846
Felipe Balbieeb720f2011-11-28 12:46:59 +0200847 if (last_one)
848 chain = false;
Felipe Balbi72246da2011-08-19 18:10:58 +0300849
Felipe Balbieeb720f2011-11-28 12:46:59 +0200850 dwc3_prepare_one_trb(dep, req, dma, length,
851 last_one, chain);
Felipe Balbi72246da2011-08-19 18:10:58 +0300852
Felipe Balbieeb720f2011-11-28 12:46:59 +0200853 if (last_one)
854 break;
855 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300856 } else {
Felipe Balbieeb720f2011-11-28 12:46:59 +0200857 dma = req->request.dma;
858 length = req->request.length;
859 trbs_left--;
860
861 if (!trbs_left)
862 last_one = 1;
863
864 /* Is this the last request? */
865 if (list_is_last(&req->list, &dep->request_list))
866 last_one = 1;
867
868 dwc3_prepare_one_trb(dep, req, dma, length,
869 last_one, false);
870
871 if (last_one)
872 break;
Felipe Balbi72246da2011-08-19 18:10:58 +0300873 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300874 }
Felipe Balbi72246da2011-08-19 18:10:58 +0300875}
876
877static int __dwc3_gadget_kick_transfer(struct dwc3_ep *dep, u16 cmd_param,
878 int start_new)
879{
880 struct dwc3_gadget_ep_cmd_params params;
881 struct dwc3_request *req;
882 struct dwc3 *dwc = dep->dwc;
883 int ret;
884 u32 cmd;
885
886 if (start_new && (dep->flags & DWC3_EP_BUSY)) {
887 dev_vdbg(dwc->dev, "%s: endpoint busy\n", dep->name);
888 return -EBUSY;
889 }
890 dep->flags &= ~DWC3_EP_PENDING_REQUEST;
891
892 /*
893 * If we are getting here after a short-out-packet we don't enqueue any
894 * new requests as we try to set the IOC bit only on the last request.
895 */
896 if (start_new) {
897 if (list_empty(&dep->req_queued))
898 dwc3_prepare_trbs(dep, start_new);
899
900 /* req points to the first request which will be sent */
901 req = next_request(&dep->req_queued);
902 } else {
Felipe Balbi68e823e2011-11-28 12:25:01 +0200903 dwc3_prepare_trbs(dep, start_new);
904
Felipe Balbi72246da2011-08-19 18:10:58 +0300905 /*
Paul Zimmerman1d046792012-02-15 18:56:56 -0800906 * req points to the first request where HWO changed from 0 to 1
Felipe Balbi72246da2011-08-19 18:10:58 +0300907 */
Felipe Balbi68e823e2011-11-28 12:25:01 +0200908 req = next_request(&dep->req_queued);
Felipe Balbi72246da2011-08-19 18:10:58 +0300909 }
910 if (!req) {
911 dep->flags |= DWC3_EP_PENDING_REQUEST;
912 return 0;
913 }
914
915 memset(&params, 0, sizeof(params));
Felipe Balbidc1c70a2011-09-30 10:58:51 +0300916 params.param0 = upper_32_bits(req->trb_dma);
917 params.param1 = lower_32_bits(req->trb_dma);
Felipe Balbi72246da2011-08-19 18:10:58 +0300918
919 if (start_new)
920 cmd = DWC3_DEPCMD_STARTTRANSFER;
921 else
922 cmd = DWC3_DEPCMD_UPDATETRANSFER;
923
924 cmd |= DWC3_DEPCMD_PARAM(cmd_param);
925 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number, cmd, &params);
926 if (ret < 0) {
927 dev_dbg(dwc->dev, "failed to send STARTTRANSFER command\n");
928
929 /*
930 * FIXME we need to iterate over the list of requests
931 * here and stop, unmap, free and del each of the linked
Paul Zimmerman1d046792012-02-15 18:56:56 -0800932 * requests instead of what we do now.
Felipe Balbi72246da2011-08-19 18:10:58 +0300933 */
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +0200934 usb_gadget_unmap_request(&dwc->gadget, &req->request,
935 req->direction);
Felipe Balbi72246da2011-08-19 18:10:58 +0300936 list_del(&req->list);
937 return ret;
938 }
939
940 dep->flags |= DWC3_EP_BUSY;
941 dep->res_trans_idx = dwc3_gadget_ep_get_transfer_index(dwc,
942 dep->number);
Felipe Balbi25b8ff62011-11-04 12:32:47 +0200943
944 WARN_ON_ONCE(!dep->res_trans_idx);
945
Felipe Balbi72246da2011-08-19 18:10:58 +0300946 return 0;
947}
948
949static int __dwc3_gadget_ep_queue(struct dwc3_ep *dep, struct dwc3_request *req)
950{
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +0200951 struct dwc3 *dwc = dep->dwc;
952 int ret;
953
Felipe Balbi72246da2011-08-19 18:10:58 +0300954 req->request.actual = 0;
955 req->request.status = -EINPROGRESS;
956 req->direction = dep->direction;
957 req->epnum = dep->number;
958
959 /*
960 * We only add to our list of requests now and
961 * start consuming the list once we get XferNotReady
962 * IRQ.
963 *
964 * That way, we avoid doing anything that we don't need
965 * to do now and defer it until the point we receive a
966 * particular token from the Host side.
967 *
968 * This will also avoid Host cancelling URBs due to too
Paul Zimmerman1d046792012-02-15 18:56:56 -0800969 * many NAKs.
Felipe Balbi72246da2011-08-19 18:10:58 +0300970 */
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +0200971 ret = usb_gadget_map_request(&dwc->gadget, &req->request,
972 dep->direction);
973 if (ret)
974 return ret;
975
Felipe Balbi72246da2011-08-19 18:10:58 +0300976 list_add_tail(&req->list, &dep->request_list);
977
978 /*
979 * There is one special case: XferNotReady with
980 * empty list of requests. We need to kick the
981 * transfer here in that situation, otherwise
982 * we will be NAKing forever.
983 *
984 * If we get XferNotReady before gadget driver
985 * has a chance to queue a request, we will ACK
986 * the IRQ but won't be able to receive the data
987 * until the next request is queued. The following
988 * code is handling exactly that.
989 */
990 if (dep->flags & DWC3_EP_PENDING_REQUEST) {
991 int ret;
992 int start_trans;
993
994 start_trans = 1;
Felipe Balbi7b7dd022012-01-18 17:09:17 +0200995 if (usb_endpoint_xfer_isoc(dep->desc) &&
Paul Zimmerman1d046792012-02-15 18:56:56 -0800996 (dep->flags & DWC3_EP_BUSY))
Felipe Balbi72246da2011-08-19 18:10:58 +0300997 start_trans = 0;
998
Paul Zimmerman1d046792012-02-15 18:56:56 -0800999 ret = __dwc3_gadget_kick_transfer(dep, 0, start_trans);
Felipe Balbi72246da2011-08-19 18:10:58 +03001000 if (ret && ret != -EBUSY) {
1001 struct dwc3 *dwc = dep->dwc;
1002
1003 dev_dbg(dwc->dev, "%s: failed to kick transfers\n",
1004 dep->name);
1005 }
1006 };
1007
1008 return 0;
1009}
1010
1011static int dwc3_gadget_ep_queue(struct usb_ep *ep, struct usb_request *request,
1012 gfp_t gfp_flags)
1013{
1014 struct dwc3_request *req = to_dwc3_request(request);
1015 struct dwc3_ep *dep = to_dwc3_ep(ep);
1016 struct dwc3 *dwc = dep->dwc;
1017
1018 unsigned long flags;
1019
1020 int ret;
1021
1022 if (!dep->desc) {
1023 dev_dbg(dwc->dev, "trying to queue request %p to disabled %s\n",
1024 request, ep->name);
1025 return -ESHUTDOWN;
1026 }
1027
1028 dev_vdbg(dwc->dev, "queing request %p to %s length %d\n",
1029 request, ep->name, request->length);
1030
1031 spin_lock_irqsave(&dwc->lock, flags);
1032 ret = __dwc3_gadget_ep_queue(dep, req);
1033 spin_unlock_irqrestore(&dwc->lock, flags);
1034
1035 return ret;
1036}
1037
1038static int dwc3_gadget_ep_dequeue(struct usb_ep *ep,
1039 struct usb_request *request)
1040{
1041 struct dwc3_request *req = to_dwc3_request(request);
1042 struct dwc3_request *r = NULL;
1043
1044 struct dwc3_ep *dep = to_dwc3_ep(ep);
1045 struct dwc3 *dwc = dep->dwc;
1046
1047 unsigned long flags;
1048 int ret = 0;
1049
1050 spin_lock_irqsave(&dwc->lock, flags);
1051
1052 list_for_each_entry(r, &dep->request_list, list) {
1053 if (r == req)
1054 break;
1055 }
1056
1057 if (r != req) {
1058 list_for_each_entry(r, &dep->req_queued, list) {
1059 if (r == req)
1060 break;
1061 }
1062 if (r == req) {
1063 /* wait until it is processed */
1064 dwc3_stop_active_transfer(dwc, dep->number);
1065 goto out0;
1066 }
1067 dev_err(dwc->dev, "request %p was not queued to %s\n",
1068 request, ep->name);
1069 ret = -EINVAL;
1070 goto out0;
1071 }
1072
1073 /* giveback the request */
1074 dwc3_gadget_giveback(dep, req, -ECONNRESET);
1075
1076out0:
1077 spin_unlock_irqrestore(&dwc->lock, flags);
1078
1079 return ret;
1080}
1081
1082int __dwc3_gadget_ep_set_halt(struct dwc3_ep *dep, int value)
1083{
1084 struct dwc3_gadget_ep_cmd_params params;
1085 struct dwc3 *dwc = dep->dwc;
1086 int ret;
1087
1088 memset(&params, 0x00, sizeof(params));
1089
1090 if (value) {
Felipe Balbi0b7836a2011-08-30 15:48:08 +03001091 if (dep->number == 0 || dep->number == 1) {
1092 /*
1093 * Whenever EP0 is stalled, we will restart
1094 * the state machine, thus moving back to
1095 * Setup Phase
1096 */
1097 dwc->ep0state = EP0_SETUP_PHASE;
1098 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001099
1100 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number,
1101 DWC3_DEPCMD_SETSTALL, &params);
1102 if (ret)
1103 dev_err(dwc->dev, "failed to %s STALL on %s\n",
1104 value ? "set" : "clear",
1105 dep->name);
1106 else
1107 dep->flags |= DWC3_EP_STALL;
1108 } else {
1109 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number,
1110 DWC3_DEPCMD_CLEARSTALL, &params);
1111 if (ret)
1112 dev_err(dwc->dev, "failed to %s STALL on %s\n",
1113 value ? "set" : "clear",
1114 dep->name);
1115 else
Alan Sternf7557c72013-11-01 12:05:12 -04001116 dep->flags &= ~(DWC3_EP_STALL | DWC3_EP_WEDGE);
Felipe Balbi72246da2011-08-19 18:10:58 +03001117 }
Paul Zimmerman52754552011-09-30 10:58:44 +03001118
Felipe Balbi72246da2011-08-19 18:10:58 +03001119 return ret;
1120}
1121
1122static int dwc3_gadget_ep_set_halt(struct usb_ep *ep, int value)
1123{
1124 struct dwc3_ep *dep = to_dwc3_ep(ep);
1125 struct dwc3 *dwc = dep->dwc;
1126
1127 unsigned long flags;
1128
1129 int ret;
1130
1131 spin_lock_irqsave(&dwc->lock, flags);
1132
1133 if (usb_endpoint_xfer_isoc(dep->desc)) {
1134 dev_err(dwc->dev, "%s is of Isochronous type\n", dep->name);
1135 ret = -EINVAL;
1136 goto out;
1137 }
1138
1139 ret = __dwc3_gadget_ep_set_halt(dep, value);
1140out:
1141 spin_unlock_irqrestore(&dwc->lock, flags);
1142
1143 return ret;
1144}
1145
1146static int dwc3_gadget_ep_set_wedge(struct usb_ep *ep)
1147{
1148 struct dwc3_ep *dep = to_dwc3_ep(ep);
Paul Zimmerman249a4562012-02-24 17:32:16 -08001149 struct dwc3 *dwc = dep->dwc;
1150 unsigned long flags;
Felipe Balbi72246da2011-08-19 18:10:58 +03001151
Paul Zimmerman249a4562012-02-24 17:32:16 -08001152 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001153 dep->flags |= DWC3_EP_WEDGE;
Paul Zimmerman249a4562012-02-24 17:32:16 -08001154 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001155
Paul Zimmerman52754552011-09-30 10:58:44 +03001156 return dwc3_gadget_ep_set_halt(ep, 1);
Felipe Balbi72246da2011-08-19 18:10:58 +03001157}
1158
1159/* -------------------------------------------------------------------------- */
1160
1161static struct usb_endpoint_descriptor dwc3_gadget_ep0_desc = {
1162 .bLength = USB_DT_ENDPOINT_SIZE,
1163 .bDescriptorType = USB_DT_ENDPOINT,
1164 .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
1165};
1166
1167static const struct usb_ep_ops dwc3_gadget_ep0_ops = {
1168 .enable = dwc3_gadget_ep0_enable,
1169 .disable = dwc3_gadget_ep0_disable,
1170 .alloc_request = dwc3_gadget_ep_alloc_request,
1171 .free_request = dwc3_gadget_ep_free_request,
1172 .queue = dwc3_gadget_ep0_queue,
1173 .dequeue = dwc3_gadget_ep_dequeue,
1174 .set_halt = dwc3_gadget_ep_set_halt,
1175 .set_wedge = dwc3_gadget_ep_set_wedge,
1176};
1177
1178static const struct usb_ep_ops dwc3_gadget_ep_ops = {
1179 .enable = dwc3_gadget_ep_enable,
1180 .disable = dwc3_gadget_ep_disable,
1181 .alloc_request = dwc3_gadget_ep_alloc_request,
1182 .free_request = dwc3_gadget_ep_free_request,
1183 .queue = dwc3_gadget_ep_queue,
1184 .dequeue = dwc3_gadget_ep_dequeue,
1185 .set_halt = dwc3_gadget_ep_set_halt,
1186 .set_wedge = dwc3_gadget_ep_set_wedge,
1187};
1188
1189/* -------------------------------------------------------------------------- */
1190
1191static int dwc3_gadget_get_frame(struct usb_gadget *g)
1192{
1193 struct dwc3 *dwc = gadget_to_dwc(g);
1194 u32 reg;
1195
1196 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1197 return DWC3_DSTS_SOFFN(reg);
1198}
1199
1200static int dwc3_gadget_wakeup(struct usb_gadget *g)
1201{
1202 struct dwc3 *dwc = gadget_to_dwc(g);
1203
1204 unsigned long timeout;
1205 unsigned long flags;
1206
1207 u32 reg;
1208
1209 int ret = 0;
1210
1211 u8 link_state;
1212 u8 speed;
1213
1214 spin_lock_irqsave(&dwc->lock, flags);
1215
1216 /*
1217 * According to the Databook Remote wakeup request should
1218 * be issued only when the device is in early suspend state.
1219 *
1220 * We can check that via USB Link State bits in DSTS register.
1221 */
1222 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1223
1224 speed = reg & DWC3_DSTS_CONNECTSPD;
1225 if (speed == DWC3_DSTS_SUPERSPEED) {
1226 dev_dbg(dwc->dev, "no wakeup on SuperSpeed\n");
1227 ret = -EINVAL;
1228 goto out;
1229 }
1230
1231 link_state = DWC3_DSTS_USBLNKST(reg);
1232
1233 switch (link_state) {
1234 case DWC3_LINK_STATE_RX_DET: /* in HS, means Early Suspend */
1235 case DWC3_LINK_STATE_U3: /* in HS, means SUSPEND */
1236 break;
1237 default:
1238 dev_dbg(dwc->dev, "can't wakeup from link state %d\n",
1239 link_state);
1240 ret = -EINVAL;
1241 goto out;
1242 }
1243
Felipe Balbi8598bde2012-01-02 18:55:57 +02001244 ret = dwc3_gadget_set_link_state(dwc, DWC3_LINK_STATE_RECOV);
1245 if (ret < 0) {
1246 dev_err(dwc->dev, "failed to put link in Recovery\n");
1247 goto out;
1248 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001249
1250 /* write zeroes to Link Change Request */
1251 reg &= ~DWC3_DCTL_ULSTCHNGREQ_MASK;
1252 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1253
Paul Zimmerman1d046792012-02-15 18:56:56 -08001254 /* poll until Link State changes to ON */
Felipe Balbi72246da2011-08-19 18:10:58 +03001255 timeout = jiffies + msecs_to_jiffies(100);
1256
Paul Zimmerman1d046792012-02-15 18:56:56 -08001257 while (!time_after(jiffies, timeout)) {
Felipe Balbi72246da2011-08-19 18:10:58 +03001258 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1259
1260 /* in HS, means ON */
1261 if (DWC3_DSTS_USBLNKST(reg) == DWC3_LINK_STATE_U0)
1262 break;
1263 }
1264
1265 if (DWC3_DSTS_USBLNKST(reg) != DWC3_LINK_STATE_U0) {
1266 dev_err(dwc->dev, "failed to send remote wakeup\n");
1267 ret = -EINVAL;
1268 }
1269
1270out:
1271 spin_unlock_irqrestore(&dwc->lock, flags);
1272
1273 return ret;
1274}
1275
1276static int dwc3_gadget_set_selfpowered(struct usb_gadget *g,
1277 int is_selfpowered)
1278{
1279 struct dwc3 *dwc = gadget_to_dwc(g);
Paul Zimmerman249a4562012-02-24 17:32:16 -08001280 unsigned long flags;
Felipe Balbi72246da2011-08-19 18:10:58 +03001281
Paul Zimmerman249a4562012-02-24 17:32:16 -08001282 spin_lock_irqsave(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001283 dwc->is_selfpowered = !!is_selfpowered;
Paul Zimmerman249a4562012-02-24 17:32:16 -08001284 spin_unlock_irqrestore(&dwc->lock, flags);
Felipe Balbi72246da2011-08-19 18:10:58 +03001285
1286 return 0;
1287}
1288
1289static void dwc3_gadget_run_stop(struct dwc3 *dwc, int is_on)
1290{
1291 u32 reg;
Sebastian Andrzej Siewior61d58242011-08-29 16:46:38 +02001292 u32 timeout = 500;
Felipe Balbi72246da2011-08-19 18:10:58 +03001293
1294 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
Felipe Balbi8db7ed12012-01-18 18:32:29 +02001295 if (is_on) {
1296 reg &= ~DWC3_DCTL_TRGTULST_MASK;
1297 reg |= (DWC3_DCTL_RUN_STOP
1298 | DWC3_DCTL_TRGTULST_RX_DET);
1299 } else {
Felipe Balbi72246da2011-08-19 18:10:58 +03001300 reg &= ~DWC3_DCTL_RUN_STOP;
Felipe Balbi8db7ed12012-01-18 18:32:29 +02001301 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001302
1303 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1304
1305 do {
1306 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
1307 if (is_on) {
1308 if (!(reg & DWC3_DSTS_DEVCTRLHLT))
1309 break;
1310 } else {
1311 if (reg & DWC3_DSTS_DEVCTRLHLT)
1312 break;
1313 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001314 timeout--;
1315 if (!timeout)
1316 break;
Sebastian Andrzej Siewior61d58242011-08-29 16:46:38 +02001317 udelay(1);
Felipe Balbi72246da2011-08-19 18:10:58 +03001318 } while (1);
1319
1320 dev_vdbg(dwc->dev, "gadget %s data soft-%s\n",
1321 dwc->gadget_driver
1322 ? dwc->gadget_driver->function : "no-function",
1323 is_on ? "connect" : "disconnect");
1324}
1325
1326static int dwc3_gadget_pullup(struct usb_gadget *g, int is_on)
1327{
1328 struct dwc3 *dwc = gadget_to_dwc(g);
1329 unsigned long flags;
1330
1331 is_on = !!is_on;
1332
1333 spin_lock_irqsave(&dwc->lock, flags);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +02001334
1335 dwc->softconnect = is_on;
1336
1337 if ((dwc->dotg && !dwc->vbus_active) ||
1338 !dwc->gadget_driver) {
1339
1340 spin_unlock_irqrestore(&dwc->lock, flags);
1341
1342 /*
1343 * Need to wait for vbus_session(on) from otg driver or to
1344 * the udc_start.
1345 */
1346 return 0;
1347 }
1348
Felipe Balbi72246da2011-08-19 18:10:58 +03001349 dwc3_gadget_run_stop(dwc, is_on);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +02001350
1351 spin_unlock_irqrestore(&dwc->lock, flags);
1352
1353 return 0;
1354}
1355
1356static int dwc3_gadget_vbus_session(struct usb_gadget *_gadget, int is_active)
1357{
1358 struct dwc3 *dwc = gadget_to_dwc(_gadget);
1359 unsigned long flags;
1360
1361 if (!dwc->dotg)
1362 return -EPERM;
1363
1364 is_active = !!is_active;
1365
1366 spin_lock_irqsave(&dwc->lock, flags);
1367
1368 /* Mark that the vbus was powered */
1369 dwc->vbus_active = is_active;
1370
1371 /*
1372 * Check if upper level usb_gadget_driver was already registerd with
1373 * this udc controller driver (if dwc3_gadget_start was called)
1374 */
1375 if (dwc->gadget_driver && dwc->softconnect) {
1376 if (dwc->vbus_active) {
1377 /*
1378 * Both vbus was activated by otg and pullup was
1379 * signaled by the gadget driver.
1380 */
1381 dwc3_gadget_run_stop(dwc, 1);
1382 } else {
1383 dwc3_gadget_run_stop(dwc, 0);
1384 }
1385 }
1386
Felipe Balbi72246da2011-08-19 18:10:58 +03001387 spin_unlock_irqrestore(&dwc->lock, flags);
1388
1389 return 0;
1390}
1391
1392static int dwc3_gadget_start(struct usb_gadget *g,
1393 struct usb_gadget_driver *driver)
1394{
1395 struct dwc3 *dwc = gadget_to_dwc(g);
1396 struct dwc3_ep *dep;
1397 unsigned long flags;
1398 int ret = 0;
1399 u32 reg;
1400
1401 spin_lock_irqsave(&dwc->lock, flags);
1402
1403 if (dwc->gadget_driver) {
1404 dev_err(dwc->dev, "%s is already bound to %s\n",
1405 dwc->gadget.name,
1406 dwc->gadget_driver->driver.name);
1407 ret = -EBUSY;
1408 goto err0;
1409 }
1410
1411 dwc->gadget_driver = driver;
1412 dwc->gadget.dev.driver = &driver->driver;
1413
Felipe Balbi72246da2011-08-19 18:10:58 +03001414 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
1415 reg &= ~(DWC3_DCFG_SPEED_MASK);
Felipe Balbi6c167fc2011-10-07 22:55:04 +03001416 reg |= dwc->maximum_speed;
Felipe Balbi72246da2011-08-19 18:10:58 +03001417 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
1418
Paul Zimmermanb23c8432011-09-30 10:58:42 +03001419 dwc->start_config_issued = false;
1420
Felipe Balbi72246da2011-08-19 18:10:58 +03001421 /* Start with SuperSpeed Default */
1422 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
1423
1424 dep = dwc->eps[0];
Felipe Balbic90bfae2011-11-29 13:11:21 +02001425 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL);
Felipe Balbi72246da2011-08-19 18:10:58 +03001426 if (ret) {
1427 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
1428 goto err0;
1429 }
1430
1431 dep = dwc->eps[1];
Felipe Balbic90bfae2011-11-29 13:11:21 +02001432 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL);
Felipe Balbi72246da2011-08-19 18:10:58 +03001433 if (ret) {
1434 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
1435 goto err1;
1436 }
1437
1438 /* begin to receive SETUP packets */
Felipe Balbic7fcdeb2011-08-27 22:28:36 +03001439 dwc->ep0state = EP0_SETUP_PHASE;
Felipe Balbi72246da2011-08-19 18:10:58 +03001440 dwc3_ep0_out_start(dwc);
1441
1442 spin_unlock_irqrestore(&dwc->lock, flags);
1443
1444 return 0;
1445
1446err1:
1447 __dwc3_gadget_ep_disable(dwc->eps[0]);
1448
1449err0:
Felipe Balbi1b5c94b2013-07-15 12:36:35 +03001450 dwc->gadget_driver = NULL;
Felipe Balbi72246da2011-08-19 18:10:58 +03001451 spin_unlock_irqrestore(&dwc->lock, flags);
1452
1453 return ret;
1454}
1455
1456static int dwc3_gadget_stop(struct usb_gadget *g,
1457 struct usb_gadget_driver *driver)
1458{
1459 struct dwc3 *dwc = gadget_to_dwc(g);
1460 unsigned long flags;
1461
1462 spin_lock_irqsave(&dwc->lock, flags);
1463
1464 __dwc3_gadget_ep_disable(dwc->eps[0]);
1465 __dwc3_gadget_ep_disable(dwc->eps[1]);
1466
1467 dwc->gadget_driver = NULL;
1468 dwc->gadget.dev.driver = NULL;
1469
1470 spin_unlock_irqrestore(&dwc->lock, flags);
1471
1472 return 0;
1473}
1474static const struct usb_gadget_ops dwc3_gadget_ops = {
1475 .get_frame = dwc3_gadget_get_frame,
1476 .wakeup = dwc3_gadget_wakeup,
1477 .set_selfpowered = dwc3_gadget_set_selfpowered,
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +02001478 .vbus_session = dwc3_gadget_vbus_session,
Felipe Balbi72246da2011-08-19 18:10:58 +03001479 .pullup = dwc3_gadget_pullup,
1480 .udc_start = dwc3_gadget_start,
1481 .udc_stop = dwc3_gadget_stop,
1482};
1483
1484/* -------------------------------------------------------------------------- */
1485
1486static int __devinit dwc3_gadget_init_endpoints(struct dwc3 *dwc)
1487{
1488 struct dwc3_ep *dep;
1489 u8 epnum;
1490
1491 INIT_LIST_HEAD(&dwc->gadget.ep_list);
1492
1493 for (epnum = 0; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
1494 dep = kzalloc(sizeof(*dep), GFP_KERNEL);
1495 if (!dep) {
1496 dev_err(dwc->dev, "can't allocate endpoint %d\n",
1497 epnum);
1498 return -ENOMEM;
1499 }
1500
1501 dep->dwc = dwc;
1502 dep->number = epnum;
1503 dwc->eps[epnum] = dep;
1504
1505 snprintf(dep->name, sizeof(dep->name), "ep%d%s", epnum >> 1,
1506 (epnum & 1) ? "in" : "out");
1507 dep->endpoint.name = dep->name;
1508 dep->direction = (epnum & 1);
1509
1510 if (epnum == 0 || epnum == 1) {
1511 dep->endpoint.maxpacket = 512;
Pratyush Anand462434e2013-01-18 16:53:56 +05301512 dep->endpoint.maxburst = 1;
Felipe Balbi72246da2011-08-19 18:10:58 +03001513 dep->endpoint.ops = &dwc3_gadget_ep0_ops;
1514 if (!epnum)
1515 dwc->gadget.ep0 = &dep->endpoint;
1516 } else {
1517 int ret;
1518
1519 dep->endpoint.maxpacket = 1024;
Sebastian Andrzej Siewior12d36c12011-11-03 20:27:50 +01001520 dep->endpoint.max_streams = 15;
Felipe Balbi72246da2011-08-19 18:10:58 +03001521 dep->endpoint.ops = &dwc3_gadget_ep_ops;
1522 list_add_tail(&dep->endpoint.ep_list,
1523 &dwc->gadget.ep_list);
1524
1525 ret = dwc3_alloc_trb_pool(dep);
Felipe Balbi25b8ff62011-11-04 12:32:47 +02001526 if (ret)
Felipe Balbi72246da2011-08-19 18:10:58 +03001527 return ret;
Felipe Balbi72246da2011-08-19 18:10:58 +03001528 }
Felipe Balbi25b8ff62011-11-04 12:32:47 +02001529
Felipe Balbi72246da2011-08-19 18:10:58 +03001530 INIT_LIST_HEAD(&dep->request_list);
1531 INIT_LIST_HEAD(&dep->req_queued);
1532 }
1533
1534 return 0;
1535}
1536
1537static void dwc3_gadget_free_endpoints(struct dwc3 *dwc)
1538{
1539 struct dwc3_ep *dep;
1540 u8 epnum;
1541
1542 for (epnum = 0; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
1543 dep = dwc->eps[epnum];
George Cherianfba59fb2013-05-27 14:35:49 +05301544 /*
1545 * Physical endpoints 0 and 1 are special; they form the
1546 * bi-directional USB endpoint 0.
1547 *
1548 * For those two physical endpoints, we don't allocate a TRB
1549 * pool nor do we add them the endpoints list. Due to that, we
1550 * shouldn't do these two operations otherwise we would end up
1551 * with all sorts of bugs when removing dwc3.ko.
1552 */
1553 if (epnum != 0 && epnum != 1) {
1554 dwc3_free_trb_pool(dep);
Felipe Balbi72246da2011-08-19 18:10:58 +03001555 list_del(&dep->endpoint.ep_list);
George Cherianfba59fb2013-05-27 14:35:49 +05301556 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001557
1558 kfree(dep);
1559 }
1560}
1561
1562static void dwc3_gadget_release(struct device *dev)
1563{
1564 dev_dbg(dev, "%s\n", __func__);
1565}
1566
1567/* -------------------------------------------------------------------------- */
1568static int dwc3_cleanup_done_reqs(struct dwc3 *dwc, struct dwc3_ep *dep,
1569 const struct dwc3_event_depevt *event, int status)
1570{
1571 struct dwc3_request *req;
Felipe Balbif6bafc62012-02-06 11:04:53 +02001572 struct dwc3_trb *trb;
Felipe Balbi72246da2011-08-19 18:10:58 +03001573 unsigned int count;
1574 unsigned int s_pkt = 0;
1575
1576 do {
1577 req = next_request(&dep->req_queued);
Sebastian Andrzej Siewiord39ee7b2011-11-03 10:32:20 +01001578 if (!req) {
1579 WARN_ON_ONCE(1);
1580 return 1;
1581 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001582
Felipe Balbif6bafc62012-02-06 11:04:53 +02001583 trb = req->trb;
Felipe Balbi72246da2011-08-19 18:10:58 +03001584
Felipe Balbif6bafc62012-02-06 11:04:53 +02001585 if ((trb->ctrl & DWC3_TRB_CTRL_HWO) && status != -ESHUTDOWN)
Sebastian Andrzej Siewior0d2f4752011-08-19 19:59:12 +02001586 /*
1587 * We continue despite the error. There is not much we
Paul Zimmerman1d046792012-02-15 18:56:56 -08001588 * can do. If we don't clean it up we loop forever. If
1589 * we skip the TRB then it gets overwritten after a
1590 * while since we use them in a ring buffer. A BUG()
1591 * would help. Lets hope that if this occurs, someone
Sebastian Andrzej Siewior0d2f4752011-08-19 19:59:12 +02001592 * fixes the root cause instead of looking away :)
1593 */
Felipe Balbi72246da2011-08-19 18:10:58 +03001594 dev_err(dwc->dev, "%s's TRB (%p) still owned by HW\n",
1595 dep->name, req->trb);
Felipe Balbif6bafc62012-02-06 11:04:53 +02001596 count = trb->size & DWC3_TRB_SIZE_MASK;
Felipe Balbi72246da2011-08-19 18:10:58 +03001597
1598 if (dep->direction) {
1599 if (count) {
1600 dev_err(dwc->dev, "incomplete IN transfer %s\n",
1601 dep->name);
1602 status = -ECONNRESET;
1603 }
1604 } else {
1605 if (count && (event->status & DEPEVT_STATUS_SHORT))
1606 s_pkt = 1;
1607 }
1608
1609 /*
1610 * We assume here we will always receive the entire data block
1611 * which we should receive. Meaning, if we program RX to
1612 * receive 4K but we receive only 2K, we assume that's all we
1613 * should receive and we simply bounce the request back to the
1614 * gadget driver for further processing.
1615 */
1616 req->request.actual += req->request.length - count;
1617 dwc3_gadget_giveback(dep, req, status);
1618 if (s_pkt)
1619 break;
Felipe Balbif6bafc62012-02-06 11:04:53 +02001620 if ((event->status & DEPEVT_STATUS_LST) &&
1621 (trb->ctrl & DWC3_TRB_CTRL_LST))
Felipe Balbi72246da2011-08-19 18:10:58 +03001622 break;
Felipe Balbif6bafc62012-02-06 11:04:53 +02001623 if ((event->status & DEPEVT_STATUS_IOC) &&
1624 (trb->ctrl & DWC3_TRB_CTRL_IOC))
Felipe Balbi72246da2011-08-19 18:10:58 +03001625 break;
1626 } while (1);
1627
Felipe Balbif6bafc62012-02-06 11:04:53 +02001628 if ((event->status & DEPEVT_STATUS_IOC) &&
1629 (trb->ctrl & DWC3_TRB_CTRL_IOC))
Felipe Balbi72246da2011-08-19 18:10:58 +03001630 return 0;
1631 return 1;
1632}
1633
1634static void dwc3_endpoint_transfer_complete(struct dwc3 *dwc,
1635 struct dwc3_ep *dep, const struct dwc3_event_depevt *event,
1636 int start_new)
1637{
1638 unsigned status = 0;
1639 int clean_busy;
1640
1641 if (event->status & DEPEVT_STATUS_BUSERR)
1642 status = -ECONNRESET;
1643
Paul Zimmerman1d046792012-02-15 18:56:56 -08001644 clean_busy = dwc3_cleanup_done_reqs(dwc, dep, event, status);
Paul Zimmermanc2df85c2012-02-24 17:32:18 -08001645 if (clean_busy)
Felipe Balbi72246da2011-08-19 18:10:58 +03001646 dep->flags &= ~DWC3_EP_BUSY;
Felipe Balbifae2b902011-10-14 13:00:30 +03001647
1648 /*
1649 * WORKAROUND: This is the 2nd half of U1/U2 -> U0 workaround.
1650 * See dwc3_gadget_linksts_change_interrupt() for 1st half.
1651 */
1652 if (dwc->revision < DWC3_REVISION_183A) {
1653 u32 reg;
1654 int i;
1655
1656 for (i = 0; i < DWC3_ENDPOINTS_NUM; i++) {
1657 struct dwc3_ep *dep = dwc->eps[i];
1658
1659 if (!(dep->flags & DWC3_EP_ENABLED))
1660 continue;
1661
1662 if (!list_empty(&dep->req_queued))
1663 return;
1664 }
1665
1666 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
1667 reg |= dwc->u1u2;
1668 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1669
1670 dwc->u1u2 = 0;
1671 }
Felipe Balbi72246da2011-08-19 18:10:58 +03001672}
1673
1674static void dwc3_gadget_start_isoc(struct dwc3 *dwc,
1675 struct dwc3_ep *dep, const struct dwc3_event_depevt *event)
1676{
Paul Zimmerman9bafa562012-02-17 14:10:16 -08001677 u32 uf, mask;
Felipe Balbi72246da2011-08-19 18:10:58 +03001678
1679 if (list_empty(&dep->request_list)) {
1680 dev_vdbg(dwc->dev, "ISOC ep %s run out for requests.\n",
1681 dep->name);
1682 return;
1683 }
1684
Paul Zimmerman9bafa562012-02-17 14:10:16 -08001685 mask = ~(dep->interval - 1);
1686 uf = event->parameters & mask;
1687 /* 4 micro frames in the future */
1688 uf += dep->interval * 4;
Felipe Balbi72246da2011-08-19 18:10:58 +03001689
1690 __dwc3_gadget_kick_transfer(dep, uf, 1);
1691}
1692
1693static void dwc3_process_ep_cmd_complete(struct dwc3_ep *dep,
1694 const struct dwc3_event_depevt *event)
1695{
1696 struct dwc3 *dwc = dep->dwc;
1697 struct dwc3_event_depevt mod_ev = *event;
1698
1699 /*
Paul Zimmerman1d046792012-02-15 18:56:56 -08001700 * We were asked to remove one request. It is possible that this
1701 * request and a few others were started together and have the same
Felipe Balbi72246da2011-08-19 18:10:58 +03001702 * transfer index. Since we stopped the complete endpoint we don't
1703 * know how many requests were already completed (and not yet)
1704 * reported and how could be done (later). We purge them all until
1705 * the end of the list.
1706 */
1707 mod_ev.status = DEPEVT_STATUS_LST;
1708 dwc3_cleanup_done_reqs(dwc, dep, &mod_ev, -ESHUTDOWN);
1709 dep->flags &= ~DWC3_EP_BUSY;
Paul Zimmerman1d046792012-02-15 18:56:56 -08001710 /* pending requests are ignored and are queued on XferNotReady */
Felipe Balbi72246da2011-08-19 18:10:58 +03001711}
1712
1713static void dwc3_ep_cmd_compl(struct dwc3_ep *dep,
1714 const struct dwc3_event_depevt *event)
1715{
1716 u32 param = event->parameters;
1717 u32 cmd_type = (param >> 8) & ((1 << 5) - 1);
1718
1719 switch (cmd_type) {
1720 case DWC3_DEPCMD_ENDTRANSFER:
1721 dwc3_process_ep_cmd_complete(dep, event);
1722 break;
1723 case DWC3_DEPCMD_STARTTRANSFER:
1724 dep->res_trans_idx = param & 0x7f;
1725 break;
1726 default:
1727 printk(KERN_ERR "%s() unknown /unexpected type: %d\n",
1728 __func__, cmd_type);
1729 break;
1730 };
1731}
1732
1733static void dwc3_endpoint_interrupt(struct dwc3 *dwc,
1734 const struct dwc3_event_depevt *event)
1735{
1736 struct dwc3_ep *dep;
1737 u8 epnum = event->endpoint_number;
1738
1739 dep = dwc->eps[epnum];
1740
1741 dev_vdbg(dwc->dev, "%s: %s\n", dep->name,
1742 dwc3_ep_event_string(event->endpoint_event));
1743
1744 if (epnum == 0 || epnum == 1) {
1745 dwc3_ep0_interrupt(dwc, event);
1746 return;
1747 }
1748
1749 switch (event->endpoint_event) {
1750 case DWC3_DEPEVT_XFERCOMPLETE:
Paul Zimmermanc2df85c2012-02-24 17:32:18 -08001751 dep->res_trans_idx = 0;
1752
Felipe Balbi72246da2011-08-19 18:10:58 +03001753 if (usb_endpoint_xfer_isoc(dep->desc)) {
1754 dev_dbg(dwc->dev, "%s is an Isochronous endpoint\n",
1755 dep->name);
1756 return;
1757 }
1758
1759 dwc3_endpoint_transfer_complete(dwc, dep, event, 1);
1760 break;
1761 case DWC3_DEPEVT_XFERINPROGRESS:
1762 if (!usb_endpoint_xfer_isoc(dep->desc)) {
1763 dev_dbg(dwc->dev, "%s is not an Isochronous endpoint\n",
1764 dep->name);
1765 return;
1766 }
1767
1768 dwc3_endpoint_transfer_complete(dwc, dep, event, 0);
1769 break;
1770 case DWC3_DEPEVT_XFERNOTREADY:
1771 if (usb_endpoint_xfer_isoc(dep->desc)) {
1772 dwc3_gadget_start_isoc(dwc, dep, event);
1773 } else {
1774 int ret;
1775
1776 dev_vdbg(dwc->dev, "%s: reason %s\n",
Felipe Balbi40aa41f2012-01-18 17:06:03 +02001777 dep->name, event->status &
1778 DEPEVT_STATUS_TRANSFER_ACTIVE
Felipe Balbi72246da2011-08-19 18:10:58 +03001779 ? "Transfer Active"
1780 : "Transfer Not Active");
1781
1782 ret = __dwc3_gadget_kick_transfer(dep, 0, 1);
1783 if (!ret || ret == -EBUSY)
1784 return;
1785
1786 dev_dbg(dwc->dev, "%s: failed to kick transfers\n",
1787 dep->name);
1788 }
1789
1790 break;
Felipe Balbi879631a2011-09-30 10:58:47 +03001791 case DWC3_DEPEVT_STREAMEVT:
1792 if (!usb_endpoint_xfer_bulk(dep->desc)) {
1793 dev_err(dwc->dev, "Stream event for non-Bulk %s\n",
1794 dep->name);
1795 return;
1796 }
1797
1798 switch (event->status) {
1799 case DEPEVT_STREAMEVT_FOUND:
1800 dev_vdbg(dwc->dev, "Stream %d found and started\n",
1801 event->parameters);
1802
1803 break;
1804 case DEPEVT_STREAMEVT_NOTFOUND:
1805 /* FALLTHROUGH */
1806 default:
1807 dev_dbg(dwc->dev, "Couldn't find suitable stream\n");
1808 }
1809 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03001810 case DWC3_DEPEVT_RXTXFIFOEVT:
1811 dev_dbg(dwc->dev, "%s FIFO Overrun\n", dep->name);
1812 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03001813 case DWC3_DEPEVT_EPCMDCMPLT:
1814 dwc3_ep_cmd_compl(dep, event);
1815 break;
1816 }
1817}
1818
1819static void dwc3_disconnect_gadget(struct dwc3 *dwc)
1820{
1821 if (dwc->gadget_driver && dwc->gadget_driver->disconnect) {
1822 spin_unlock(&dwc->lock);
1823 dwc->gadget_driver->disconnect(&dwc->gadget);
1824 spin_lock(&dwc->lock);
1825 }
1826}
1827
1828static void dwc3_stop_active_transfer(struct dwc3 *dwc, u32 epnum)
1829{
1830 struct dwc3_ep *dep;
1831 struct dwc3_gadget_ep_cmd_params params;
1832 u32 cmd;
1833 int ret;
1834
1835 dep = dwc->eps[epnum];
1836
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +02001837 WARN_ON(!dep->res_trans_idx);
Felipe Balbi72246da2011-08-19 18:10:58 +03001838 if (dep->res_trans_idx) {
1839 cmd = DWC3_DEPCMD_ENDTRANSFER;
1840 cmd |= DWC3_DEPCMD_HIPRI_FORCERM | DWC3_DEPCMD_CMDIOC;
1841 cmd |= DWC3_DEPCMD_PARAM(dep->res_trans_idx);
1842 memset(&params, 0, sizeof(params));
1843 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number, cmd, &params);
1844 WARN_ON_ONCE(ret);
Sebastian Andrzej Siewiora1ae9be2011-08-22 17:42:18 +02001845 dep->res_trans_idx = 0;
Felipe Balbibfbd61e2012-10-04 11:58:00 +03001846 dep->flags &= ~DWC3_EP_BUSY;
Felipe Balbi72246da2011-08-19 18:10:58 +03001847 }
1848}
1849
1850static void dwc3_stop_active_transfers(struct dwc3 *dwc)
1851{
1852 u32 epnum;
1853
1854 for (epnum = 2; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
1855 struct dwc3_ep *dep;
1856
1857 dep = dwc->eps[epnum];
1858 if (!(dep->flags & DWC3_EP_ENABLED))
1859 continue;
1860
Sebastian Andrzej Siewior624407f2011-08-29 13:56:37 +02001861 dwc3_remove_requests(dwc, dep);
Felipe Balbi72246da2011-08-19 18:10:58 +03001862 }
1863}
1864
1865static void dwc3_clear_stall_all_ep(struct dwc3 *dwc)
1866{
1867 u32 epnum;
1868
1869 for (epnum = 1; epnum < DWC3_ENDPOINTS_NUM; epnum++) {
1870 struct dwc3_ep *dep;
1871 struct dwc3_gadget_ep_cmd_params params;
1872 int ret;
1873
1874 dep = dwc->eps[epnum];
1875
1876 if (!(dep->flags & DWC3_EP_STALL))
1877 continue;
1878
1879 dep->flags &= ~DWC3_EP_STALL;
1880
1881 memset(&params, 0, sizeof(params));
1882 ret = dwc3_send_gadget_ep_cmd(dwc, dep->number,
1883 DWC3_DEPCMD_CLEARSTALL, &params);
1884 WARN_ON_ONCE(ret);
1885 }
1886}
1887
1888static void dwc3_gadget_disconnect_interrupt(struct dwc3 *dwc)
1889{
1890 dev_vdbg(dwc->dev, "%s\n", __func__);
1891#if 0
1892 XXX
1893 U1/U2 is powersave optimization. Skip it for now. Anyway we need to
1894 enable it before we can disable it.
1895
1896 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
1897 reg &= ~DWC3_DCTL_INITU1ENA;
1898 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1899
1900 reg &= ~DWC3_DCTL_INITU2ENA;
1901 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
1902#endif
1903
1904 dwc3_stop_active_transfers(dwc);
1905 dwc3_disconnect_gadget(dwc);
Paul Zimmermanb23c8432011-09-30 10:58:42 +03001906 dwc->start_config_issued = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03001907
1908 dwc->gadget.speed = USB_SPEED_UNKNOWN;
Felipe Balbidf62df52011-10-14 15:11:49 +03001909 dwc->setup_packet_pending = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03001910}
1911
1912static void dwc3_gadget_usb3_phy_power(struct dwc3 *dwc, int on)
1913{
1914 u32 reg;
1915
1916 reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0));
1917
1918 if (on)
1919 reg &= ~DWC3_GUSB3PIPECTL_SUSPHY;
1920 else
1921 reg |= DWC3_GUSB3PIPECTL_SUSPHY;
1922
1923 dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg);
1924}
1925
1926static void dwc3_gadget_usb2_phy_power(struct dwc3 *dwc, int on)
1927{
1928 u32 reg;
1929
1930 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
1931
1932 if (on)
1933 reg &= ~DWC3_GUSB2PHYCFG_SUSPHY;
1934 else
1935 reg |= DWC3_GUSB2PHYCFG_SUSPHY;
1936
1937 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
1938}
1939
1940static void dwc3_gadget_reset_interrupt(struct dwc3 *dwc)
1941{
1942 u32 reg;
1943
1944 dev_vdbg(dwc->dev, "%s\n", __func__);
1945
Felipe Balbidf62df52011-10-14 15:11:49 +03001946 /*
1947 * WORKAROUND: DWC3 revisions <1.88a have an issue which
1948 * would cause a missing Disconnect Event if there's a
1949 * pending Setup Packet in the FIFO.
1950 *
1951 * There's no suggested workaround on the official Bug
1952 * report, which states that "unless the driver/application
1953 * is doing any special handling of a disconnect event,
1954 * there is no functional issue".
1955 *
1956 * Unfortunately, it turns out that we _do_ some special
1957 * handling of a disconnect event, namely complete all
1958 * pending transfers, notify gadget driver of the
1959 * disconnection, and so on.
1960 *
1961 * Our suggested workaround is to follow the Disconnect
1962 * Event steps here, instead, based on a setup_packet_pending
1963 * flag. Such flag gets set whenever we have a XferNotReady
1964 * event on EP0 and gets cleared on XferComplete for the
1965 * same endpoint.
1966 *
1967 * Refers to:
1968 *
1969 * STAR#9000466709: RTL: Device : Disconnect event not
1970 * generated if setup packet pending in FIFO
1971 */
1972 if (dwc->revision < DWC3_REVISION_188A) {
1973 if (dwc->setup_packet_pending)
1974 dwc3_gadget_disconnect_interrupt(dwc);
1975 }
1976
Felipe Balbi961906e2011-12-20 15:37:21 +02001977 /* after reset -> Default State */
1978 dwc->dev_state = DWC3_DEFAULT_STATE;
1979
Felipe Balbi72246da2011-08-19 18:10:58 +03001980 /* Enable PHYs */
1981 dwc3_gadget_usb2_phy_power(dwc, true);
1982 dwc3_gadget_usb3_phy_power(dwc, true);
1983
1984 if (dwc->gadget.speed != USB_SPEED_UNKNOWN)
1985 dwc3_disconnect_gadget(dwc);
1986
1987 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
1988 reg &= ~DWC3_DCTL_TSTCTRL_MASK;
1989 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
Gerard Cauvy3b637362012-02-10 12:21:18 +02001990 dwc->test_mode = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03001991
1992 dwc3_stop_active_transfers(dwc);
1993 dwc3_clear_stall_all_ep(dwc);
Paul Zimmermanb23c8432011-09-30 10:58:42 +03001994 dwc->start_config_issued = false;
Felipe Balbi72246da2011-08-19 18:10:58 +03001995
1996 /* Reset device address to zero */
1997 reg = dwc3_readl(dwc->regs, DWC3_DCFG);
1998 reg &= ~(DWC3_DCFG_DEVADDR_MASK);
1999 dwc3_writel(dwc->regs, DWC3_DCFG, reg);
Felipe Balbi72246da2011-08-19 18:10:58 +03002000}
2001
2002static void dwc3_update_ram_clk_sel(struct dwc3 *dwc, u32 speed)
2003{
2004 u32 reg;
2005 u32 usb30_clock = DWC3_GCTL_CLK_BUS;
2006
2007 /*
2008 * We change the clock only at SS but I dunno why I would want to do
2009 * this. Maybe it becomes part of the power saving plan.
2010 */
2011
2012 if (speed != DWC3_DSTS_SUPERSPEED)
2013 return;
2014
2015 /*
2016 * RAMClkSel is reset to 0 after USB reset, so it must be reprogrammed
2017 * each time on Connect Done.
2018 */
2019 if (!usb30_clock)
2020 return;
2021
2022 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
2023 reg |= DWC3_GCTL_RAMCLKSEL(usb30_clock);
2024 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
2025}
2026
2027static void dwc3_gadget_disable_phy(struct dwc3 *dwc, u8 speed)
2028{
2029 switch (speed) {
2030 case USB_SPEED_SUPER:
2031 dwc3_gadget_usb2_phy_power(dwc, false);
2032 break;
2033 case USB_SPEED_HIGH:
2034 case USB_SPEED_FULL:
2035 case USB_SPEED_LOW:
2036 dwc3_gadget_usb3_phy_power(dwc, false);
2037 break;
2038 }
2039}
2040
2041static void dwc3_gadget_conndone_interrupt(struct dwc3 *dwc)
2042{
2043 struct dwc3_gadget_ep_cmd_params params;
2044 struct dwc3_ep *dep;
2045 int ret;
2046 u32 reg;
2047 u8 speed;
2048
2049 dev_vdbg(dwc->dev, "%s\n", __func__);
2050
2051 memset(&params, 0x00, sizeof(params));
2052
Felipe Balbi72246da2011-08-19 18:10:58 +03002053 reg = dwc3_readl(dwc->regs, DWC3_DSTS);
2054 speed = reg & DWC3_DSTS_CONNECTSPD;
2055 dwc->speed = speed;
2056
2057 dwc3_update_ram_clk_sel(dwc, speed);
2058
2059 switch (speed) {
2060 case DWC3_DCFG_SUPERSPEED:
Felipe Balbi05870c52011-10-14 14:51:38 +03002061 /*
2062 * WORKAROUND: DWC3 revisions <1.90a have an issue which
2063 * would cause a missing USB3 Reset event.
2064 *
2065 * In such situations, we should force a USB3 Reset
2066 * event by calling our dwc3_gadget_reset_interrupt()
2067 * routine.
2068 *
2069 * Refers to:
2070 *
2071 * STAR#9000483510: RTL: SS : USB3 reset event may
2072 * not be generated always when the link enters poll
2073 */
2074 if (dwc->revision < DWC3_REVISION_190A)
2075 dwc3_gadget_reset_interrupt(dwc);
2076
Felipe Balbi72246da2011-08-19 18:10:58 +03002077 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(512);
2078 dwc->gadget.ep0->maxpacket = 512;
2079 dwc->gadget.speed = USB_SPEED_SUPER;
2080 break;
2081 case DWC3_DCFG_HIGHSPEED:
2082 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
2083 dwc->gadget.ep0->maxpacket = 64;
2084 dwc->gadget.speed = USB_SPEED_HIGH;
2085 break;
2086 case DWC3_DCFG_FULLSPEED2:
2087 case DWC3_DCFG_FULLSPEED1:
2088 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(64);
2089 dwc->gadget.ep0->maxpacket = 64;
2090 dwc->gadget.speed = USB_SPEED_FULL;
2091 break;
2092 case DWC3_DCFG_LOWSPEED:
2093 dwc3_gadget_ep0_desc.wMaxPacketSize = cpu_to_le16(8);
2094 dwc->gadget.ep0->maxpacket = 8;
2095 dwc->gadget.speed = USB_SPEED_LOW;
2096 break;
2097 }
2098
2099 /* Disable unneded PHY */
2100 dwc3_gadget_disable_phy(dwc, dwc->gadget.speed);
2101
2102 dep = dwc->eps[0];
Felipe Balbic90bfae2011-11-29 13:11:21 +02002103 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL);
Felipe Balbi72246da2011-08-19 18:10:58 +03002104 if (ret) {
2105 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
2106 return;
2107 }
2108
2109 dep = dwc->eps[1];
Felipe Balbic90bfae2011-11-29 13:11:21 +02002110 ret = __dwc3_gadget_ep_enable(dep, &dwc3_gadget_ep0_desc, NULL);
Felipe Balbi72246da2011-08-19 18:10:58 +03002111 if (ret) {
2112 dev_err(dwc->dev, "failed to enable %s\n", dep->name);
2113 return;
2114 }
2115
2116 /*
2117 * Configure PHY via GUSB3PIPECTLn if required.
2118 *
2119 * Update GTXFIFOSIZn
2120 *
2121 * In both cases reset values should be sufficient.
2122 */
2123}
2124
2125static void dwc3_gadget_wakeup_interrupt(struct dwc3 *dwc)
2126{
2127 dev_vdbg(dwc->dev, "%s\n", __func__);
2128
2129 /*
2130 * TODO take core out of low power mode when that's
2131 * implemented.
2132 */
2133
2134 dwc->gadget_driver->resume(&dwc->gadget);
2135}
2136
2137static void dwc3_gadget_linksts_change_interrupt(struct dwc3 *dwc,
2138 unsigned int evtinfo)
2139{
Felipe Balbifae2b902011-10-14 13:00:30 +03002140 enum dwc3_link_state next = evtinfo & DWC3_LINK_STATE_MASK;
2141
2142 /*
2143 * WORKAROUND: DWC3 Revisions <1.83a have an issue which, depending
2144 * on the link partner, the USB session might do multiple entry/exit
2145 * of low power states before a transfer takes place.
2146 *
2147 * Due to this problem, we might experience lower throughput. The
2148 * suggested workaround is to disable DCTL[12:9] bits if we're
2149 * transitioning from U1/U2 to U0 and enable those bits again
2150 * after a transfer completes and there are no pending transfers
2151 * on any of the enabled endpoints.
2152 *
2153 * This is the first half of that workaround.
2154 *
2155 * Refers to:
2156 *
2157 * STAR#9000446952: RTL: Device SS : if U1/U2 ->U0 takes >128us
2158 * core send LGO_Ux entering U0
2159 */
2160 if (dwc->revision < DWC3_REVISION_183A) {
2161 if (next == DWC3_LINK_STATE_U0) {
2162 u32 u1u2;
2163 u32 reg;
2164
2165 switch (dwc->link_state) {
2166 case DWC3_LINK_STATE_U1:
2167 case DWC3_LINK_STATE_U2:
2168 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
2169 u1u2 = reg & (DWC3_DCTL_INITU2ENA
2170 | DWC3_DCTL_ACCEPTU2ENA
2171 | DWC3_DCTL_INITU1ENA
2172 | DWC3_DCTL_ACCEPTU1ENA);
2173
2174 if (!dwc->u1u2)
2175 dwc->u1u2 = reg & u1u2;
2176
2177 reg &= ~u1u2;
2178
2179 dwc3_writel(dwc->regs, DWC3_DCTL, reg);
2180 break;
2181 default:
2182 /* do nothing */
2183 break;
2184 }
2185 }
2186 }
2187
2188 dwc->link_state = next;
Felipe Balbi019ac832011-09-08 21:18:47 +03002189
2190 dev_vdbg(dwc->dev, "%s link %d\n", __func__, dwc->link_state);
Felipe Balbi72246da2011-08-19 18:10:58 +03002191}
2192
2193static void dwc3_gadget_interrupt(struct dwc3 *dwc,
2194 const struct dwc3_event_devt *event)
2195{
2196 switch (event->type) {
2197 case DWC3_DEVICE_EVENT_DISCONNECT:
2198 dwc3_gadget_disconnect_interrupt(dwc);
2199 break;
2200 case DWC3_DEVICE_EVENT_RESET:
2201 dwc3_gadget_reset_interrupt(dwc);
2202 break;
2203 case DWC3_DEVICE_EVENT_CONNECT_DONE:
2204 dwc3_gadget_conndone_interrupt(dwc);
2205 break;
2206 case DWC3_DEVICE_EVENT_WAKEUP:
2207 dwc3_gadget_wakeup_interrupt(dwc);
2208 break;
2209 case DWC3_DEVICE_EVENT_LINK_STATUS_CHANGE:
2210 dwc3_gadget_linksts_change_interrupt(dwc, event->event_info);
2211 break;
2212 case DWC3_DEVICE_EVENT_EOPF:
2213 dev_vdbg(dwc->dev, "End of Periodic Frame\n");
2214 break;
2215 case DWC3_DEVICE_EVENT_SOF:
2216 dev_vdbg(dwc->dev, "Start of Periodic Frame\n");
2217 break;
2218 case DWC3_DEVICE_EVENT_ERRATIC_ERROR:
2219 dev_vdbg(dwc->dev, "Erratic Error\n");
2220 break;
2221 case DWC3_DEVICE_EVENT_CMD_CMPL:
2222 dev_vdbg(dwc->dev, "Command Complete\n");
2223 break;
2224 case DWC3_DEVICE_EVENT_OVERFLOW:
2225 dev_vdbg(dwc->dev, "Overflow\n");
Pavankumar Kondetid393e172012-06-12 16:07:29 +05302226 /*
2227 * Controllers prior to 2.30a revision has a bug where
2228 * Overflow Event may overwrite an unacknowledged event
2229 * in the event buffer. The severity of the issue depends
2230 * on the overwritten event type. Add a warning message
2231 * saying that an event is overwritten.
2232 *
2233 * TODO: In future we may need to see if we can re-enumerate
2234 * with host.
2235 */
2236 if (dwc->revision < DWC3_REVISION_230A)
2237 dev_warn(dwc->dev, "Unacknowledged event overwritten\n");
Felipe Balbi72246da2011-08-19 18:10:58 +03002238 break;
Pavankumar Kondeti33fe6f12012-06-12 16:21:46 +05302239 case DWC3_DEVICE_EVENT_VENDOR_DEV_TEST_LMP:
2240 /*
2241 * Controllers prior to 2.30a revision has a bug, due to which
2242 * a vendor device test LMP event can not be filtered. But
2243 * this event is not handled in the current code. This is a
2244 * special event and 8 bytes of data will follow the event.
2245 * Handling this event is tricky when event buffer is almost
2246 * full. Moreover this event will not occur in normal scenario
2247 * and can only happen with special hosts in testing scenarios.
2248 * Add a warning message to indicate that this event is received
2249 * which means that event buffer might have corrupted.
2250 */
2251 if (dwc->revision < DWC3_REVISION_230A)
2252 dev_warn(dwc->dev, "Vendor Device Test LMP Received\n");
2253 break;
Felipe Balbi72246da2011-08-19 18:10:58 +03002254 default:
2255 dev_dbg(dwc->dev, "UNKNOWN IRQ %d\n", event->type);
2256 }
2257}
2258
2259static void dwc3_process_event_entry(struct dwc3 *dwc,
2260 const union dwc3_event *event)
2261{
2262 /* Endpoint IRQ, handle it and return early */
2263 if (event->type.is_devspec == 0) {
2264 /* depevt */
2265 return dwc3_endpoint_interrupt(dwc, &event->depevt);
2266 }
2267
2268 switch (event->type.type) {
2269 case DWC3_EVENT_TYPE_DEV:
2270 dwc3_gadget_interrupt(dwc, &event->devt);
2271 break;
2272 /* REVISIT what to do with Carkit and I2C events ? */
2273 default:
2274 dev_err(dwc->dev, "UNKNOWN IRQ type %d\n", event->raw);
2275 }
2276}
2277
2278static irqreturn_t dwc3_process_event_buf(struct dwc3 *dwc, u32 buf)
2279{
2280 struct dwc3_event_buffer *evt;
2281 int left;
2282 u32 count;
2283
2284 count = dwc3_readl(dwc->regs, DWC3_GEVNTCOUNT(buf));
2285 count &= DWC3_GEVNTCOUNT_MASK;
2286 if (!count)
2287 return IRQ_NONE;
2288
2289 evt = dwc->ev_buffs[buf];
2290 left = count;
2291
2292 while (left > 0) {
2293 union dwc3_event event;
2294
Felipe Balbid70d8442012-02-06 13:40:17 +02002295 event.raw = *(u32 *) (evt->buf + evt->lpos);
2296
Felipe Balbi72246da2011-08-19 18:10:58 +03002297 dwc3_process_event_entry(dwc, &event);
2298 /*
2299 * XXX we wrap around correctly to the next entry as almost all
2300 * entries are 4 bytes in size. There is one entry which has 12
2301 * bytes which is a regular entry followed by 8 bytes data. ATM
2302 * I don't know how things are organized if were get next to the
2303 * a boundary so I worry about that once we try to handle that.
2304 */
2305 evt->lpos = (evt->lpos + 4) % DWC3_EVENT_BUFFERS_SIZE;
2306 left -= 4;
2307
2308 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(buf), 4);
2309 }
2310
2311 return IRQ_HANDLED;
2312}
2313
2314static irqreturn_t dwc3_interrupt(int irq, void *_dwc)
2315{
2316 struct dwc3 *dwc = _dwc;
2317 int i;
2318 irqreturn_t ret = IRQ_NONE;
2319
2320 spin_lock(&dwc->lock);
2321
Felipe Balbi9f622b22011-10-12 10:31:04 +03002322 for (i = 0; i < dwc->num_event_buffers; i++) {
Felipe Balbi72246da2011-08-19 18:10:58 +03002323 irqreturn_t status;
2324
2325 status = dwc3_process_event_buf(dwc, i);
2326 if (status == IRQ_HANDLED)
2327 ret = status;
2328 }
2329
2330 spin_unlock(&dwc->lock);
2331
2332 return ret;
2333}
2334
2335/**
2336 * dwc3_gadget_init - Initializes gadget related registers
Paul Zimmerman1d046792012-02-15 18:56:56 -08002337 * @dwc: pointer to our controller context structure
Felipe Balbi72246da2011-08-19 18:10:58 +03002338 *
2339 * Returns 0 on success otherwise negative errno.
2340 */
2341int __devinit dwc3_gadget_init(struct dwc3 *dwc)
2342{
2343 u32 reg;
2344 int ret;
2345 int irq;
2346
2347 dwc->ctrl_req = dma_alloc_coherent(dwc->dev, sizeof(*dwc->ctrl_req),
2348 &dwc->ctrl_req_addr, GFP_KERNEL);
2349 if (!dwc->ctrl_req) {
2350 dev_err(dwc->dev, "failed to allocate ctrl request\n");
2351 ret = -ENOMEM;
2352 goto err0;
2353 }
2354
2355 dwc->ep0_trb = dma_alloc_coherent(dwc->dev, sizeof(*dwc->ep0_trb),
2356 &dwc->ep0_trb_addr, GFP_KERNEL);
2357 if (!dwc->ep0_trb) {
2358 dev_err(dwc->dev, "failed to allocate ep0 trb\n");
2359 ret = -ENOMEM;
2360 goto err1;
2361 }
2362
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02002363 dwc->setup_buf = kzalloc(sizeof(*dwc->setup_buf) * 2,
2364 GFP_KERNEL);
Felipe Balbi72246da2011-08-19 18:10:58 +03002365 if (!dwc->setup_buf) {
2366 dev_err(dwc->dev, "failed to allocate setup buffer\n");
2367 ret = -ENOMEM;
2368 goto err2;
2369 }
2370
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002371 dwc->ep0_bounce = dma_alloc_coherent(dwc->dev,
2372 512, &dwc->ep0_bounce_addr, GFP_KERNEL);
2373 if (!dwc->ep0_bounce) {
2374 dev_err(dwc->dev, "failed to allocate ep0 bounce buffer\n");
2375 ret = -ENOMEM;
2376 goto err3;
2377 }
2378
Felipe Balbi72246da2011-08-19 18:10:58 +03002379 dev_set_name(&dwc->gadget.dev, "gadget");
2380
2381 dwc->gadget.ops = &dwc3_gadget_ops;
Michal Nazarewiczd327ab52011-11-19 18:27:37 +01002382 dwc->gadget.max_speed = USB_SPEED_SUPER;
Felipe Balbi72246da2011-08-19 18:10:58 +03002383 dwc->gadget.speed = USB_SPEED_UNKNOWN;
2384 dwc->gadget.dev.parent = dwc->dev;
Felipe Balbieeb720f2011-11-28 12:46:59 +02002385 dwc->gadget.sg_supported = true;
Felipe Balbi72246da2011-08-19 18:10:58 +03002386
2387 dma_set_coherent_mask(&dwc->gadget.dev, dwc->dev->coherent_dma_mask);
2388
2389 dwc->gadget.dev.dma_parms = dwc->dev->dma_parms;
2390 dwc->gadget.dev.dma_mask = dwc->dev->dma_mask;
2391 dwc->gadget.dev.release = dwc3_gadget_release;
2392 dwc->gadget.name = "dwc3-gadget";
2393
2394 /*
2395 * REVISIT: Here we should clear all pending IRQs to be
2396 * sure we're starting from a well known location.
2397 */
2398
2399 ret = dwc3_gadget_init_endpoints(dwc);
2400 if (ret)
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002401 goto err4;
Felipe Balbi72246da2011-08-19 18:10:58 +03002402
2403 irq = platform_get_irq(to_platform_device(dwc->dev), 0);
2404
2405 ret = request_irq(irq, dwc3_interrupt, IRQF_SHARED,
2406 "dwc3", dwc);
2407 if (ret) {
2408 dev_err(dwc->dev, "failed to request irq #%d --> %d\n",
2409 irq, ret);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002410 goto err5;
Felipe Balbi72246da2011-08-19 18:10:58 +03002411 }
2412
2413 /* Enable all but Start and End of Frame IRQs */
Pavankumar Kondeti33fe6f12012-06-12 16:21:46 +05302414 reg = (DWC3_DEVTEN_EVNTOVERFLOWEN |
Felipe Balbi72246da2011-08-19 18:10:58 +03002415 DWC3_DEVTEN_CMDCMPLTEN |
2416 DWC3_DEVTEN_ERRTICERREN |
2417 DWC3_DEVTEN_WKUPEVTEN |
2418 DWC3_DEVTEN_ULSTCNGEN |
2419 DWC3_DEVTEN_CONNECTDONEEN |
2420 DWC3_DEVTEN_USBRSTEN |
2421 DWC3_DEVTEN_DISCONNEVTEN);
2422 dwc3_writel(dwc->regs, DWC3_DEVTEN, reg);
2423
2424 ret = device_register(&dwc->gadget.dev);
2425 if (ret) {
2426 dev_err(dwc->dev, "failed to register gadget device\n");
2427 put_device(&dwc->gadget.dev);
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002428 goto err6;
Felipe Balbi72246da2011-08-19 18:10:58 +03002429 }
2430
2431 ret = usb_add_gadget_udc(dwc->dev, &dwc->gadget);
2432 if (ret) {
2433 dev_err(dwc->dev, "failed to register udc\n");
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002434 goto err7;
Felipe Balbi72246da2011-08-19 18:10:58 +03002435 }
2436
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +02002437 if (dwc->dotg) {
2438 /* dwc3 otg driver is active (DRD mode + SRPSupport=1) */
2439 ret = otg_set_peripheral(&dwc->dotg->otg, &dwc->gadget);
2440 if (ret) {
2441 dev_err(dwc->dev, "failed to set peripheral to otg\n");
2442 goto err7;
2443 }
Manu Gautamb5067272012-07-02 09:53:41 +05302444 } else {
2445 pm_runtime_no_callbacks(&dwc->gadget.dev);
2446 pm_runtime_set_active(&dwc->gadget.dev);
2447 pm_runtime_enable(&dwc->gadget.dev);
2448 pm_runtime_get(&dwc->gadget.dev);
Ido Shayevitzcdeef4c2012-05-29 13:17:41 +02002449 }
2450
Felipe Balbi72246da2011-08-19 18:10:58 +03002451 return 0;
2452
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002453err7:
Felipe Balbi72246da2011-08-19 18:10:58 +03002454 device_unregister(&dwc->gadget.dev);
2455
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002456err6:
Felipe Balbi72246da2011-08-19 18:10:58 +03002457 dwc3_writel(dwc->regs, DWC3_DEVTEN, 0x00);
2458 free_irq(irq, dwc);
2459
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002460err5:
Felipe Balbi72246da2011-08-19 18:10:58 +03002461 dwc3_gadget_free_endpoints(dwc);
2462
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002463err4:
2464 dma_free_coherent(dwc->dev, 512, dwc->ep0_bounce,
2465 dwc->ep0_bounce_addr);
2466
Felipe Balbi72246da2011-08-19 18:10:58 +03002467err3:
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02002468 kfree(dwc->setup_buf);
Felipe Balbi72246da2011-08-19 18:10:58 +03002469
2470err2:
2471 dma_free_coherent(dwc->dev, sizeof(*dwc->ep0_trb),
2472 dwc->ep0_trb, dwc->ep0_trb_addr);
2473
2474err1:
2475 dma_free_coherent(dwc->dev, sizeof(*dwc->ctrl_req),
2476 dwc->ctrl_req, dwc->ctrl_req_addr);
2477
2478err0:
2479 return ret;
2480}
2481
2482void dwc3_gadget_exit(struct dwc3 *dwc)
2483{
2484 int irq;
Felipe Balbi72246da2011-08-19 18:10:58 +03002485
Manu Gautamb5067272012-07-02 09:53:41 +05302486 if (dwc->dotg) {
2487 pm_runtime_put(&dwc->gadget.dev);
2488 pm_runtime_disable(&dwc->gadget.dev);
2489 }
2490
Felipe Balbi72246da2011-08-19 18:10:58 +03002491 usb_del_gadget_udc(&dwc->gadget);
2492 irq = platform_get_irq(to_platform_device(dwc->dev), 0);
2493
2494 dwc3_writel(dwc->regs, DWC3_DEVTEN, 0x00);
2495 free_irq(irq, dwc);
2496
Felipe Balbi72246da2011-08-19 18:10:58 +03002497 dwc3_gadget_free_endpoints(dwc);
2498
Felipe Balbi5812b1c2011-08-27 22:07:53 +03002499 dma_free_coherent(dwc->dev, 512, dwc->ep0_bounce,
2500 dwc->ep0_bounce_addr);
2501
Felipe Balbi0fc9a1b2011-12-19 11:32:34 +02002502 kfree(dwc->setup_buf);
Felipe Balbi72246da2011-08-19 18:10:58 +03002503
2504 dma_free_coherent(dwc->dev, sizeof(*dwc->ep0_trb),
2505 dwc->ep0_trb, dwc->ep0_trb_addr);
2506
2507 dma_free_coherent(dwc->dev, sizeof(*dwc->ctrl_req),
2508 dwc->ctrl_req, dwc->ctrl_req_addr);
2509
2510 device_unregister(&dwc->gadget.dev);
2511}